svn: r2946
This commit is contained in:
Alex Roitman 2004-03-02 00:36:53 +00:00
parent d4d0db9e65
commit acc4cae477

View File

@ -73,14 +73,15 @@ def cm2u(cm):
# Spacing in points (distance between the bottoms of two adjacent lines) # Spacing in points (distance between the bottoms of two adjacent lines)
_LINE_SPACING = 20 _LINE_SPACING = 20
# An inch for each margin so far (should use styles later)
_LEFT_MARGIN = cm2u(2.54) # Font constants -- specific for gnome-print
_RIGHT_MARGIN = cm2u(2.54) _FONT_SANS_SERIF = "Serif"
_TOP_MARGIN = cm2u(2.54) _FONT_SERIF = "Sans"
_BOTTOM_MARGIN = cm2u(2.54) _FONT_MONOSPACE = "Monospace"
# Default font _FONT_BOLD = "Bold"
_FONT_SIZE = 12 _FONT_ITALIC = "Italic"
_FONT_NAME = 'Serif' _FONT_BOLD_ITALIC = "Bold Italic"
_FONT_REGULAR = "Regular"
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -93,15 +94,15 @@ class LPRDoc(BaseDoc.BaseDoc):
def open(self,filename): def open(self,filename):
"""Sets up initialization""" """Sets up initialization"""
#set up variables needed to keep track of which state we are in #set up variables needed to keep track of which state we are in
self.__in_table=0 self.__in_table = 0
self.__in_cell=0 self.__in_cell = 0
self.__in_paragraph=0 self.__in_paragraph = 0
self.__page_count=0 self.__page_count = 0
self.__page_open=0 self.__page_open = 0
self.__paragraph_data="" self.__paragraph_data = ""
self.__cell_data="" self.__cell_data = ""
self.__table_data=[] self.__table_data = []
#create main variables for this print job #create main variables for this print job
self.__job = gnomeprint.Job(gnomeprint.config_default()) self.__job = gnomeprint.Job(gnomeprint.config_default())
@ -109,20 +110,37 @@ class LPRDoc(BaseDoc.BaseDoc):
#find out what the width and height of the page is #find out what the width and height of the page is
__width, __height = gnomeprint.job_get_page_size_from_config(self.__job.get_config()) __width, __height = gnomeprint.job_get_page_size_from_config(self.__job.get_config())
self.__left_margin = _LEFT_MARGIN
self.__right_margin = __width - _RIGHT_MARGIN
self.__top_margin = __height - _TOP_MARGIN
self.__bottom_margin = _BOTTOM_MARGIN
#set what fonts we will use self.__left_margin = cm2u(self.get_left_margin())
self.__regular_font=gnomeprint.font_find_closest( self.__right_margin = __width - cm2u(self.get_right_margin())
"%s Regular" % _FONT_NAME, _FONT_SIZE) self.__top_margin = __height - cm2u(self.get_top_margin())
self.__bold_font=gnomeprint.font_find_closest( self.__bottom_margin = cm2u(self.get_bottom_margin())
"%s Bold" % _FONT_NAME, _FONT_SIZE)
self.__font=self.__regular_font
self.start_page(self) self.start_page(self)
def find_font_from_fontstyle(self,fontstyle):
"""
"""
if fontstyle.get_type_face() == BaseDoc.FONT_SANS_SERIF:
face = _FONT_SANS_SERIF
elif fontstyle.get_type_face() == BaseDoc.FONT_SERIF:
face = _FONT_SERIF
elif fontstyle.get_type_face() == BaseDoc.FONT_MONOSPACE:
face = _FONT_MONOSPACE
if fontstyle.get_bold():
modifier = _FONT_BOLD
if fontstyle.get_italic():
modifier = _FONT_BOLD_ITALIC
elif fontstyle.get_italic():
modifier = _FONT_ITALIC
else:
modifier = _FONT_REGULAR
size = fontstyle.get_size()
return gnomeprint.font_find_closest("%s %s" % (face, modifier),size)
def close(self): def close(self):
"""Clean up and close the document""" """Clean up and close the document"""
#print "close doc" #print "close doc"
@ -154,7 +172,6 @@ class LPRDoc(BaseDoc.BaseDoc):
self.__y=self.__top_margin self.__y=self.__top_margin
self.__pc.beginpage(str(self.__page_count)) self.__pc.beginpage(str(self.__page_count))
self.__pc.setfont(self.__font)
self.__pc.moveto(self.__x, self.__y) self.__pc.moveto(self.__x, self.__y)
def end_page(self): def end_page(self):
@ -170,8 +187,12 @@ class LPRDoc(BaseDoc.BaseDoc):
We assume a linebreak at the end of each paragraph.""" We assume a linebreak at the end of each paragraph."""
#print "start paragraph" #print "start paragraph"
#set paragraph variables so we know that we are in a paragraph #set paragraph variables so we know that we are in a paragraph
self.__in_paragraph=1 self.__in_paragraph = 1
self.__paragraph_data="" self.__paragraph_data = ""
if self.__in_table:
self.__paragrapgh_styles[self.rownum][self.cellnum] = self.style_list[style_name]
else:
self.__paragraph_style = self.style_list[style_name]
def end_paragraph(self): def end_paragraph(self):
"""End the current paragraph""" """End the current paragraph"""
@ -179,24 +200,33 @@ class LPRDoc(BaseDoc.BaseDoc):
self.__in_paragraph=0 self.__in_paragraph=0
#print text in paragraph if any data exists #print text in paragraph if any data exists
if len(self.__paragraph_data) > 0: if len(self.__paragraph_data) > 0:
fontstyle = self.__paragraph_style.get_font()
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
self.__pc.moveto(self.__x, self.__y)
self.__x, self.__y=self.__print_text(self.__paragraph_data, self.__x, self.__y=self.__print_text(self.__paragraph_data,
self.__x, self.__y, self.__x, self.__y,
self.__left_margin, self.__left_margin,
self.__right_margin) self.__right_margin,
fontstyle)
self.__paragraph_data="" self.__paragraph_data=""
self.__y=self.__advance_line(self.__y) self.__y=self.__advance_line(self.__y)
#### FIXME BEGIN #########
# The following two functions don't work at the moment. The problem is
# in that the writing is deferred when in tables and/or paragraphs.
# Paragraph text is accumulated, so afterwards, when it's tim to write,
# one would need some pointers as to when to change the font (and where
# to change it back
#===========================
def start_bold(self): def start_bold(self):
"""Bold face""" """Bold face"""
#print "start bold" pass
self.__font=self.__bold_font
self.__pc.setfont(self.__font)
def end_bold(self): def end_bold(self):
"""End bold face""" """End bold face"""
#print "end bold" pass
self.__font=self.__regular_font #==========================
self.__pc.setfont(self.__font) #### FIXME END #########
def start_superscript(self): def start_superscript(self):
pass pass
@ -224,6 +254,8 @@ class LPRDoc(BaseDoc.BaseDoc):
self.__in_table=1 self.__in_table=1
self.__tbl_style = self.table_styles[style_name] self.__tbl_style = self.table_styles[style_name]
self.__ncols = self.__tbl_style.get_columns() self.__ncols = self.__tbl_style.get_columns()
self.rownum = -1
self.__paragrapgh_styles = [[None] * self.__ncols]
table_width = (self.__right_margin - self.__left_margin) * \ table_width = (self.__right_margin - self.__left_margin) * \
self.__tbl_style.get_width() / 100.0 self.__tbl_style.get_width() / 100.0
self.cell_widths = [0] * self.__ncols self.cell_widths = [0] * self.__ncols
@ -245,6 +277,9 @@ class LPRDoc(BaseDoc.BaseDoc):
#print "start row" #print "start row"
#reset this state, so we can get data from user #reset this state, so we can get data from user
self.__row_data=[] self.__row_data=[]
self.rownum = self.rownum + 1
self.cellnum = -1
self.__paragrapgh_styles.append([None] * self.__ncols)
def end_row(self): def end_row(self):
"""End the row (new line)""" """End the row (new line)"""
@ -260,6 +295,7 @@ class LPRDoc(BaseDoc.BaseDoc):
#reset this state #reset this state
self.__in_cell=1 self.__in_cell=1
self.__cell_data="" self.__cell_data=""
self.cellnum = self.cellnum + span
def end_cell(self): def end_cell(self):
"""Prepares for next cell""" """Prepares for next cell"""
@ -295,19 +331,29 @@ class LPRDoc(BaseDoc.BaseDoc):
pass pass
def draw_box(self,style,text,x,y): def draw_box(self,style,text,x,y):
box_style = self.draw_styles[style]
para_style = box_style.get_paragraph_style()
fontstyle = para_style.get_font()
#assuming that we start drawing box from current position #assuming that we start drawing box from current position
__width=x-self.__x __width=x-self.__x
__height=y-self.__y __height=y-self.__y
self.__pc.rect_stroked(self.__x, self.__y) self.__pc.rect_stroked(self.__x, self.__y)
if text != None: if text != None:
__text_width=self.__get_text_width(text) __text_width=self.__get_text_width(text,fontstyle)
#try to center text in box #try to center text in box
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
self.__pc.moveto(self.__x+(__width/2)-(__text_width/2), self.__pc.moveto(self.__x+(__width/2)-(__text_width/2),
self.__y+(__height/2)) self.__y+(__height/2))
self.__pc.show(text) self.__pc.show(text)
def write_at (self, style, text, x, y): def write_at (self, style, text, x, y):
box_style = self.draw_styles[style]
para_style = box_style.get_paragraph_style()
fontstyle = para_style.get_font()
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
self.__pc.moveto(x, y) self.__pc.moveto(x, y)
self.__pc.show(text) self.__pc.show(text)
@ -316,16 +362,26 @@ class LPRDoc(BaseDoc.BaseDoc):
self.__pc.lineto(x2, y2) self.__pc.lineto(x2, y2)
def draw_text(self,style,text,x1,y1): def draw_text(self,style,text,x1,y1):
box_style = self.draw_styles[style]
para_style = box_style.get_paragraph_style()
fontstyle = para_style.get_font()
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
self.__pc.moveto(x1,y1) self.__pc.moveto(x1,y1)
self.__pc.show(text) self.__pc.show(text)
def center_text(self,style,text,x1,y1): def center_text(self,style,text,x1,y1):
box_style = self.draw_styles[style]
para_style = box_style.get_paragraph_style()
fontstyle = para_style.get_font()
#not sure how x1, y1 fit into this #not sure how x1, y1 fit into this
#should we assume x1 y1 is the starting location #should we assume x1 y1 is the starting location
#and that the right margin is the right edge? #and that the right margin is the right edge?
__width=self.get_text_width(text) __width=self.get_text_width(text)
__center=self.__right_margin-self.__left_margin __center=self.__right_margin-self.__left_margin
__center-=__width/2 __center-=__width/2
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
self.__pc.moveto(__center, self.__y) self.__pc.moveto(__center, self.__y)
self.__pc.show(text) self.__pc.show(text)
@ -364,16 +420,17 @@ class LPRDoc(BaseDoc.BaseDoc):
return y - _LINE_SPACING return y - _LINE_SPACING
#function to determine the width of text #function to determine the width of text
def __text_width(self, text): def __text_width(self, text, fontstyle):
return self.__font.get_width_utf8(text) font = self.find_font_from_fontstyle(fontstyle)
return font.get_width_utf8(text)
#this function tells us the minimum size that a column can be #this function tells us the minimum size that a column can be
#by returning the width of the largest word in the text #by returning the width of the largest word in the text
def __min_column_size (self, text): def __min_column_size (self, text,fontstyle):
__textlist=string.split(text, " ") __textlist=string.split(text, " ")
__max_word_size=0 __max_word_size=0
for __word in __textlist: for __word in __textlist:
__length=self.__text_width(__word+" "*3) __length=self.__text_width(__word+" "*3,fontstyle)
if __length > __max_word_size: if __length > __max_word_size:
__max_word_size=__length __max_word_size=__length
@ -381,16 +438,16 @@ class LPRDoc(BaseDoc.BaseDoc):
#function to fund out the height of the text between left_margin #function to fund out the height of the text between left_margin
# and right_margin -- kinda like __print_text, but without printing. # and right_margin -- kinda like __print_text, but without printing.
def __text_height(self, text, width): def __text_height(self, text, width, fontstyle):
nlines = 1 nlines = 1
if width < self.__text_width(text): if width < self.__text_width(text,fontstyle):
#divide up text and print #divide up text and print
textlist = string.split(text) textlist = string.split(text)
text = "" text = ""
for element in textlist: for element in textlist:
if self.__text_width(text + element + " ") < width: if self.__text_width(text + element + " ",fontstyle) < width:
text = text + element + " " text = text + element + " "
else: else:
#__text contains as many words as this __width allows #__text contains as many words as this __width allows
@ -403,13 +460,14 @@ class LPRDoc(BaseDoc.BaseDoc):
return nlines * _LINE_SPACING return nlines * _LINE_SPACING
def __print_text(self, text, x, y, left_margin, right_margin): def __print_text(self, text, x, y, left_margin, right_margin,fontstyle):
__width=right_margin-left_margin __width=right_margin-left_margin
self.__pc.setfont(self.find_font_from_fontstyle(fontstyle))
#all text will fit within the width provided #all text will fit within the width provided
if __width >= self.__text_width(text): if __width >= self.__text_width(text,fontstyle):
self.__pc.moveto(left_margin, y) self.__pc.moveto(left_margin, y)
x=left_margin+self.__text_width(text) x=left_margin+self.__text_width(text,fontstyle)
self.__pc.show(text) self.__pc.show(text)
y=self.__advance_line(y) y=self.__advance_line(y)
else: else:
@ -417,7 +475,7 @@ class LPRDoc(BaseDoc.BaseDoc):
__textlist=string.split(text, " ") __textlist=string.split(text, " ")
__text="" __text=""
for __element in __textlist: for __element in __textlist:
if self.__text_width(__text+__element+" ") < __width: if self.__text_width(__text+__element+" ",fontstyle) < __width:
__text=__text+__element+" " __text=__text+__element+" "
else: else:
#__text contains as many words as this __width allows #__text contains as many words as this __width allows
@ -451,11 +509,15 @@ class LPRDoc(BaseDoc.BaseDoc):
__row = self.__table_data[__row_num][:] __row = self.__table_data[__row_num][:]
#do calcs on each __row and keep track on max length of each column #do calcs on each __row and keep track on max length of each column
for __col in range(self.__ncols): for __col in range(self.__ncols):
__min = self.__min_column_size(__row[__col]+" "*3) fontstyle = self.__paragrapgh_styles[__row_num][__col].get_font()
__min = self.__min_column_size(__row[__col]+" "*3,fontstyle)
if __min < __min_col_size[__col]: if __min < __min_col_size[__col]:
__min_col_size[__col] = __min __min_col_size[__col] = __min
__max = self.__text_height(__row[__col], self.cell_widths[__col]) __max = self.__text_height(__row[__col],
self.cell_widths[__col],
fontstyle)
if __max > __max_vspace[__row_num]: if __max > __max_vspace[__row_num]:
__max_vspace[__row_num] = __max __max_vspace[__row_num] = __max
@ -469,40 +531,6 @@ class LPRDoc(BaseDoc.BaseDoc):
#is table width larger than the width of the paper? #is table width larger than the width of the paper?
if __min_table_width > (self.__right_margin - self.__left_margin): if __min_table_width > (self.__right_margin - self.__left_margin):
print "Table does not fit onto the page.\n" print "Table does not fit onto the page.\n"
#BILLY:
# I have commented this block since the table width is now determined
# from the start_table's style argument. Here we only detect if we
# have minimum column sizes exceeding the page width -- should not
# happen really. If it does, there's no good way to fix it,
# so I'm not even attempting. It's like fitting table
# with three cells, each containing a word occupying the whole line.
#
# #figure out the largest our table can be to fit on a page
# __width=self.__right_margin - self.__left_margin
# #find out how much larger our table is than what is allowed
# __extra_length=__min_table_width - __width
# #for each column, substract the extra width off so that
# #each column gets deduced a width determined by its
# #size compared to the other columns
# #(larger columns get more taken off, smaller columns get
# # less width taken off)
#
# __temp = 0
# while __extra_length > 1:
# print "Extra length:", __extra_length
# for __col in range(self.__ncols):
# if __extra_length<=1: break
# if __max_col_size[__col] > __min_col_size[__col]:
# __temp = self.cell_widths[__col] \
# - (self.cell_widths[__col]/__min_table_width) \
# * __extra_length
# if __temp >= __min_col_size[__col]:
# __max_col_size[__col]=__temp
#
# __min_table_width=0
# for __value in __max_col_size:
# __min_table_width+=__value
# __extra_length=__min_table_width - __width
#for now we will assume left justification of tables #for now we will assume left justification of tables
#output data in table #output data in table
@ -518,9 +546,12 @@ class LPRDoc(BaseDoc.BaseDoc):
__min_y=self.__y __min_y=self.__y
for __col in range(self.__ncols): for __col in range(self.__ncols):
fontstyle = self.__paragrapgh_styles[__row_num][__col].get_font()
__nothing, __y=self.__print_text(__row[__col], __nothing, __y=self.__print_text(__row[__col],
self.__x, self.__y, self.__x, self.__y,
__x, __x+self.cell_widths[__col]) __x, __x+self.cell_widths[__col],
fontstyle)
__x=__x+self.cell_widths[__col] # set up margin for this row __x=__x+self.cell_widths[__col] # set up margin for this row
if __y < __min_y: # if we go below current lowest if __y < __min_y: # if we go below current lowest
@ -528,12 +559,6 @@ class LPRDoc(BaseDoc.BaseDoc):
self.__y=__min_y #reset so that we do not overwrite self.__y=__min_y #reset so that we do not overwrite
#see if we are about to go off the page, if so, create new page
#if self.__y < self.__bottom_margin:
# self.end_page()
# self.start_page()
# __min_y=self.__y
#function to print text to a printer #function to print text to a printer
def __do_print(self,dialog, job): def __do_print(self,dialog, job):
__pc = gnomeprint.Context(dialog.get_config()) __pc = gnomeprint.Context(dialog.get_config())
@ -576,6 +601,6 @@ Plugins.register_text_doc(
classref=LPRDoc, classref=LPRDoc,
table=1, table=1,
paper=1, paper=1,
style=0, style=1,
ext="" ext=""
) )