Skip to content

Commit

Permalink
Changes while writing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina7479 committed Feb 11, 2014
1 parent b94fd22 commit 3abba93
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ v0.1.15, 02-06-2014 -- Fix import error
v0.1.16, 02-06-2014 -- Really fixed import error
v0.1.17, 02-07-2014 -- Bug Fix
v0.1.18, 02-10-2014 -- Loaded fonts into memory, rather then pickle.
v0.1.19, 02-11-2014 -- Changes based on writing documentation.
2 changes: 1 addition & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ README.txt
setup.py
bin/runexamples.py
pypdflite/__init__.py
pypdflite/load_fonts.py
pypdflite/font_loader.py
pypdflite/pdfdocument.py
pypdflite/pdflite.py
pypdflite/session.py
Expand Down
4 changes: 2 additions & 2 deletions bin/jpgtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def JPGTest():
"""

#Create PDFLITE object, initialize with path & filename.
writer = PDFLite("JPGTest.pdf")
writer = PDFLite("generated/JPGTest.pdf")

# If desired (in production code), set compression
# writer.setCompression(True)
Expand All @@ -23,7 +23,7 @@ def JPGTest():
document.add_text("This should be before the image.")
document.add_newline(1)

mylogo = document.add_image("example.jpg")
mylogo = document.add_image("bin/example.jpg")
document.draw_image(mylogo)
document.add_newline(1)

Expand Down
34 changes: 17 additions & 17 deletions bin/landscapetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ def LandscapeTest():
document.add_newline()
document.add_page()
document.add_text(
"""Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras et erat dolor. Nullam id aliquam neque. Vivamus nec nibh
orci. Nam faucibus dignissim diam eget tempor. Aenean neque sem,
euismod sit amet tellus nec, elementum varius diam. Vestibulum
in ultricies enim. Fusce imperdiet tempus lacus facilisis
vestibulum. Vestibulum urna magna, dignissim vel venenatis in,
pulvinar ac orci. Etiam vitae tempor metus, eu tristique mauris.
Donec tincidunt purus et scelerisque sagittis. Proin semper
"""Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
Cras et erat dolor. Nullam id aliquam neque. Vivamus nec nibh\
orci. Nam faucibus dignissim diam eget tempor. Aenean neque sem,\
euismod sit amet tellus nec, elementum varius diam. Vestibulum\
in ultricies enim. Fusce imperdiet tempus lacus facilisis\
vestibulum. Vestibulum urna magna, dignissim vel venenatis in,\
pulvinar ac orci. Etiam vitae tempor metus, eu tristique mauris.\
Donec tincidunt purus et scelerisque sagittis. Proin semper\
facilisis vehicula.""")
document.add_text(
"""Pellentesque rhoncus vestibulum turpis ut varius. Nunc a rutrum
est. Etiam sollicitudin rhoncus nisl, quis scelerisque felis
dignissim vitae. Maecenas rutrum quam at risus mattis congue. Sed
hendrerit nulla ac nunc consectetur suscipit. Fusce elementum
interdum nibh, et fermentum lacus egestas non. Sed consectetur
mollis tortor, eu aliquam leo tristique sit amet. Etiam nec lectus
magna. Nam faucibus scelerisque velit nec cursus. Ut a dolor
accumsan, gravida nunc vitae, luctus quam. Vestibulum quis gravida
quam. Proin feugiat urna ut rutrum facilisis. Vivamus gravida iaculis
"""Pellentesque rhoncus vestibulum turpis ut varius. Nunc a rutrum\
est. Etiam sollicitudin rhoncus nisl, quis scelerisque felis\
dignissim vitae. Maecenas rutrum quam at risus mattis congue. Sed\
hendrerit nulla ac nunc consectetur suscipit. Fusce elementum\
interdum nibh, et fermentum lacus egestas non. Sed consectetur\
mollis tortor, eu aliquam leo tristique sit amet. Etiam nec lectus\
magna. Nam faucibus scelerisque velit nec cursus. Ut a dolor\
accumsan, gravida nunc vitae, luctus quam. Vestibulum quis gravida\
quam. Proin feugiat urna ut rutrum facilisis. Vivamus gravida iaculis\
nibh at feugiat.""")
# Close writer
writer.close()
Expand Down
2 changes: 1 addition & 1 deletion bin/linetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def LinesTest():
document.draw_rectangle(150, 500, 300, 600, style='B')

# Draw Dashed Line
document.add_line(400, 250, 300, 500, style="dashed")
document.add_line(400, 250, 300, 500, style="dots")

# Close Document
writer.close()
Expand Down
4 changes: 2 additions & 2 deletions bin/pagenumbertest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def PageNumberTest():
"""

#Create PDFLITE object, initialize with path & filename.
writer = PDFLite("PageNumberTest.pdf")
writer = PDFLite("generated/PageNumberTest.pdf")

# If desired (in production code), set compression
# writer.setCompression(True)
Expand Down Expand Up @@ -121,7 +121,7 @@ def PageNumberTest():
quam. Proin feugiat urna ut rutrum facilisis. Vivamus gravida iaculis\
nibh at feugiat.""")

document.add_page_numbers(font=normal_font, color=PDFColor())
document.add_page_numbers(font=normal_font, color=PDFColor(), text2=' of %s')
# Close writer
writer.close()

Expand Down
6 changes: 3 additions & 3 deletions bin/pngtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def PNGTest():
document.add_text("This should be before the image.")
document.add_newline(1)

mylogo = document.add_image("testing_colors.png")
mylogo = document.add_image("bin/testing_colors.png")
document.draw_image(mylogo)
document.add_newline(1)

Expand All @@ -35,12 +35,12 @@ def PNGTest():
document.add_text("There it is without a newline")

document.add_newline(3)
apple = document.add_image('apple_logo.png')
apple = document.add_image('bin/apple_logo.png')
document.draw_image(apple)

document.add_page()

document.set_background_image("background.png")
document.set_background_image("bin/background.png")

document.add_text("This text, ")
document.add_text("And this text")
Expand Down
13 changes: 13 additions & 0 deletions bin/testcursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pypdflite import PDFCursor

def TestCursor():
cursor1 = PDFCursor(20, 30)
cursor2 = PDFCursor(22, 50)

#print "Should be True:", cursor1 < cursor2
print cursor1
print "Should be False: ", cursor1 > cursor2
print "Should be False: ", cursor1 == cursor2

if __name__ == '__main__':
TestCursor()
2 changes: 1 addition & 1 deletion bin/texttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def TextTest():
"""

#Create PDFLITE object, initialize with path & filename.
writer = PDFLite("TextTest.pdf")
writer = PDFLite("generated/TextTest.pdf")

# If desired (in production code), set compression
# writer.setCompression(True)
Expand Down
44 changes: 31 additions & 13 deletions pypdflite/pdfdocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def _set_default_font(self):
self.fonts.append(self.font)
self.fontkeys.append(self.font.font_key)

def get_color(self):
return PDFColor()

# Public methods, main interface
# Pages
def add_page(self, page=None):
Expand Down Expand Up @@ -125,6 +128,8 @@ def set_margins(self, left, top=None, right=None, bottom=None):
self.margins = left
else:
self.margins = PDFMargin(left, top, right, bottom)
if self.page is not None:
self.page.set_margins(self.margins)

def add_page_numbers(self, location='right', font=None, cursor=None, color=None, text1="Page %s", text2=None):
self.page_numbers = location
Expand All @@ -134,11 +139,17 @@ def add_page_numbers(self, location='right', font=None, cursor=None, color=None,
self.page_numbers_text1 = text1
self.page_numbers_text2 = text2

def change_page_orientation(self):
self.page._change_orientation()

# Cursor
def get_new_cursor(self):
" Returns a new default cursor "
return PDFCursor()

def get_cursor(self):
return self.page.cursor.copy()

def set_cursor(self, x=None, y=None):
if x is not None and isinstance(x, PDFCursor):
self.page.cursor = x
Expand Down Expand Up @@ -209,12 +220,6 @@ def set_font_size(self, size):
else:
self.font._set_size(size)

def get_available_tt(self):
ttfont = PDFTTFont()
famlies = ttfont._get_available_font_families()
print famlies
return famlies

# Writing
def add_text(self, text, cursor=None):
""" Input text, short or long. Writes in order, within the
Expand Down Expand Up @@ -293,9 +298,20 @@ def add_line(self, x1=None, y1=None, x2=None, y2=None,
if cursor1 is not None:
if cursor2 is not None:
pass
else:
elif x2 is not None and y2 is not None:
cursor2 = PDFCursor(x2, y2)
else:
raise Exception("Line not fully specified")
elif x1 is not None and y1 is not None:
cursor1 = PDFCursor(x1, y1)
cursor2 = PDFCursor(x2, y2)
if cursor2 is not None:
pass
elif x2 is not None and y2 is not None:
cursor2 = PDFCursor(x2, y2)
else:
raise Exception("Line not fully specified")
else:
raise Exception("Line not specified")

myline = PDFLine(self.session, self.page, cursor1, cursor2, self.draw_color, style)
myline._draw()
Expand Down Expand Up @@ -328,6 +344,8 @@ def draw_rectangle(self, x1=None, y1=None, x2=None, y2=None,
elif width is not None and height is not None:
dims = PDFCursor(width, height)
cursor2 = cursor1.add(dims)
elif cursor2 is not None:
pass
else:
raise Exception("Rectangle not defined")
else:
Expand Down Expand Up @@ -355,15 +373,15 @@ def draw_table(self, table):
else:
raise Exception("Invalid Table")

def add_cell_format(self, data=None, default_font=None):
if default_font is None:
default_font = self.font
def add_cell_format(self, data=None, cell_font=None):
if cell_font is None:
cell_font = self.font
if data is None:
data = {}
format = PDFCellFormat(data, default_font)
format = PDFCellFormat(data, cell_font)
return format

def add_image(self, image=None, name=None):
def add_image(self, image, name=None):
if isinstance(image, PDFImage): # It's an image object
myimage = self._get_image(image.name)
if not myimage:
Expand Down
9 changes: 1 addition & 8 deletions pypdflite/pdflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, filepath, orientation="P", layout="letter", font_list=None, f
self.set_information()
self.set_compression()

def set_compression(self, value=False):
def set_compression(self, value=True):
# False is easier to read with a text editor.
self.session._set_compression(value)

Expand Down Expand Up @@ -121,13 +121,6 @@ def close(self):
output = None
return output

# Font loading helpers
def load_fonts(self):
load_fonts.load_fonts()

def remove_fonts(self):
load_fonts.remove_fonts()

# Private Methods for building the PDF
def _put_header(self):
" Standard first line in a PDF. "
Expand Down
4 changes: 2 additions & 2 deletions pypdflite/pdfobjects/pdfcellformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class PDFCellFormat(object):
in a dictionary of key / value pairs.
"""
def __init__(self, data, default_font):
def __init__(self, data, cell_font):
self.dict = {}
self.available_keys = {'font': default_font, # PDFFont
self.available_keys = {'font': cell_font, # PDFFont
'num_format': None, # ('percent', #decimals), ('decimal', #d), ('money' or '$', #d), ('comma', #d), ('$comma' or 'money_comma') # Tuples
'align': "left", # Left, right, center
'valign': 'center', # Top, center, bottom
Expand Down
4 changes: 2 additions & 2 deletions pypdflite/pdfobjects/pdfcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class PDFColor(object):
def __init__(self, name=None, r=0, g=0, b=0):
def __init__(self, r=0, g=0, b=0, name=None):
# Get color dictionary
self.color_dict = color_reference

Expand Down Expand Up @@ -34,7 +34,7 @@ def set_color_by_number(self, r, g, b):
self.name = None

def copy(self):
new_color = PDFColor(self.name, self.red, self.green, self.blue)
new_color = PDFColor(self.red, self.green, self.blue, self.name)
new_color._set_type(self.color_type)
return new_color

Expand Down
12 changes: 6 additions & 6 deletions pypdflite/pdfobjects/pdfcursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def y_is_greater_than(self, test_ordinate):
else:
return False

def is_greater_than(self, test_ordinate):
def __gt__(self, test_ordinate):
" Comparison for both ordinates, prioritizing the y direction. "
self._is_coordinate(test_ordinate)
if self.y > test_ordinate.y:
Expand All @@ -141,7 +141,7 @@ def is_greater_than(self, test_ordinate):
else:
return False

def is_equal_to(self, test_ordinate):
def __eq__(self, test_ordinate):
" Test to see if coordinates are equal to each other. "
self._is_coordinate(test_ordinate)
if self.y == test_ordinate.y:
Expand All @@ -150,7 +150,7 @@ def is_equal_to(self, test_ordinate):
else:
return False

def is_less_than(self, test_ordinate):
def __lt__(self, test_ordinate):
" Comparison for both ordinates, prioritizing the y direction."
self._is_coordinate(test_ordinate)
if self.y < test_ordinate.y:
Expand All @@ -169,21 +169,21 @@ def copy(self):
new_cursor.set_deltas(self.dx, self.dy)
return new_cursor

def add(self, add_ordinate):
def __add__(self, add_ordinate):
" Add two coordinates to each other, return new coordinate. "
self._is_coordinate(add_ordinate)
x = self.x + add_ordinate.x
y = self.y + add_ordinate.y
return self.__class__(x, y)

def subtract(self, sub_ordinate):
def __sub__(self, sub_ordinate):
" Subtract two coordinates from each other, suppress negatives. "
self._is_coordinate(sub_ordinate)
x = abs(self.x - sub_ordinate.x)
y = abs(self.y - sub_ordinate.y)
return self.__class__(x, y)

def scale(self, scale):
def __mul__(self, scale):
" Return scaled coordinate"
scale = float(scale)
x = self.x * scale
Expand Down
2 changes: 1 addition & 1 deletion pypdflite/pdfobjects/pdffont.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class PDFFont(object):

def __init__(self, session, family='helvetica', style='', size=20):
def __init__(self, session, family='helvetica', style='', size=12):
self.session = session

self.is_set = False
Expand Down
6 changes: 5 additions & 1 deletion pypdflite/pdfobjects/pdfline.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def _set_size(self, line_size=1):
def _set_style(self, style=None):
if style == "dashed" or style == 1:
self.style = "dashed"
elif style == 'dots' or style == 2:
self.style = 'dots'
else:
self.style = "solid"

Expand All @@ -32,8 +34,10 @@ def _draw_color(self):
def _draw_style(self):
if self.style == "dashed":
self.session._out('[%s] %s d' % (3, 0), self.page)
elif (self.style == "solid"):
elif self.style == "solid":
self.session._out('[] 0 d', self.page)
elif self.style == 'dots':
self.session._out('[%s] %s d' % (1, 1), self.page)

def _draw_line_size(self):
self.session._out('%.2f w' % self.line_size, self.page)
Expand Down
Loading

0 comments on commit 3abba93

Please sign in to comment.