Optimize docgen backends by consolidating calls to write methods

svn: r15527
This commit is contained in:
Gerald Britton 2010-06-04 17:19:26 +00:00
parent 0901feda9e
commit 7244e05a25
4 changed files with 1309 additions and 1087 deletions

File diff suppressed because it is too large Load Diff

View File

@ -66,29 +66,27 @@ class PSDrawDoc(BaseDoc, DrawDoc):
def fontdef(self, para): def fontdef(self, para):
font = para.get_font() font = para.get_font()
if font.get_type_face() == FONT_SERIF: if font.get_type_face() == FONT_SERIF:
font_name = "/Times"
if font.get_bold(): if font.get_bold():
font_name += "-Bold"
if font.get_italic(): if font.get_italic():
font_name = "/Times-BoldItalic" font_name += "Italic"
elif font.get_italic():
font_name += "-Italic"
else: else:
font_name = "/Times-Bold" font_name += "-Roman"
else:
if font.get_italic():
font_name = "/Times-Italic"
else:
font_name = "/Times-Roman"
else:
if font.get_bold():
if font.get_italic():
font_name = "/Helvetica-BoldOblique"
else:
font_name = "/Helvetica-Bold"
else:
if font.get_italic():
font_name = "/Helvetica-Oblique"
else:
font_name = "/Helvetica"
return "%s find-latin-font %d scalefont setfont\n" % (font_name, font.get_size()) else: # Use a font without serifs
font_name = "/Helvetica"
if font.get_bold():
font_name += "-Bold"
if font.get_italic():
font_name += "Oblique"
elif font.get_italic():
font_name += "-Oblique"
return "%s find-latin-font %d scalefont setfont\n" % \
(font_name, font.get_size())
def translate(self, x, y): def translate(self, x, y):
return (x, self.paper.get_size().get_height() - y) return (x, self.paper.get_size().get_height() - y)
@ -99,10 +97,9 @@ class PSDrawDoc(BaseDoc, DrawDoc):
@param filename: path name of the file to create @param filename: path name of the file to create
""" """
if filename[-3:] != ".ps":
self.filename = filename + ".ps"
else:
self.filename = filename self.filename = filename
if not filename.endswith(".ps"):
self.filename += ".ps"
try: try:
self.file = open(self.filename,"w") self.file = open(self.filename,"w")
@ -112,36 +109,43 @@ class PSDrawDoc(BaseDoc, DrawDoc):
except: except:
raise Errors.ReportError(_("Could not create %s") % self.filename) raise Errors.ReportError(_("Could not create %s") % self.filename)
self.file.write('%!PS-Adobe-3.0\n') self.file.write(
self.file.write('%%LanguageLevel: 2\n') '%!PS-Adobe-3.0\n'
self.file.write('%%Pages: (atend)\n') '%%LanguageLevel: 2\n'
self.file.write('%%PageOrder: Ascend\n') '%%Pages: (atend)\n'
'%%PageOrder: Ascend\n'
'%%Orientation: '
)
if self.paper.get_orientation() != PAPER_PORTRAIT: if self.paper.get_orientation() != PAPER_PORTRAIT:
self.file.write('%%Orientation: Landscape\n') self.file.write('Landscape\n')
else: else:
self.file.write('%%Orientation: Portrait\n') self.file.write('Portrait\n')
self.file.write('%%EndComments\n') self.file.write(
self.file.write('/cm { 28.34 mul } def\n') '%%EndComments\n'
self.file.write('% build iso-latin-1 version of a font\n') '/cm { 28.34 mul } def\n'
self.file.write('/font-to-iso-latin-1 { % <font> font-to-iso-latin-1 <font>\n') '% build iso-latin-1 version of a font\n'
self.file.write('%% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1\n') '/font-to-iso-latin-1 { % <font> font-to-iso-latin-1 <font>\n'
self.file.write('dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall\n') '%% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1\n'
self.file.write('/Encoding ISOLatin1Encoding def currentdict end\n') 'dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall\n'
self.file.write('dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn \n') '/Encoding ISOLatin1Encoding def currentdict end\n'
self.file.write('exch definefont\n') 'dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn \n'
self.file.write('} def\n') 'exch definefont\n'
self.file.write('\n') '} def\n'
self.file.write('/find-latin-font { % <name> find-latin-font <font>\n') '\n'
self.file.write('findfont font-to-iso-latin-1\n') '/find-latin-font { % <name> find-latin-font <font>\n'
self.file.write('} def\n') 'findfont font-to-iso-latin-1\n'
'} def\n'
)
self.filename = filename self.filename = filename
def close(self): def close(self):
self.file.write('%%Trailer\n') self.file.write(
self.file.write('%%Pages: ') '%%Trailer\n'
self.file.write('%d\n' % self.page) '%%Pages: ' +
self.file.write('%%EOF\n') '%d\n' % self.page +
'%%EOF\n'
)
self.file.close() self.file.close()
if self.open_req: if self.open_req:
open_file_with_default_application(self.filename) open_file_with_default_application(self.filename)
@ -150,16 +154,20 @@ class PSDrawDoc(BaseDoc, DrawDoc):
pass pass
def start_page(self): def start_page(self):
self.page = self.page + 1 self.page += 1
self.file.write("%%Page:") self.file.write(
self.file.write("%d %d\n" % (self.page, self.page)) "%%Page:" +
"%d %d\n" % (self.page, self.page)
)
if self.paper.get_orientation() != PAPER_PORTRAIT: if self.paper.get_orientation() != PAPER_PORTRAIT:
self.file.write('90 rotate %s cm %s cm translate\n' % ( self.file.write('90 rotate %s cm %s cm translate\n' % (
gformat(0),gformat(-1*self.paper.get_size().get_height()))) gformat(0),gformat(-1*self.paper.get_size().get_height())))
def end_page(self): def end_page(self):
self.file.write('showpage\n') self.file.write(
self.file.write('%%PageTrailer\n') 'showpage\n'
'%%PageTrailer\n'
)
def encode(self, text): def encode(self, text):
try: try:
@ -181,17 +189,19 @@ class PSDrawDoc(BaseDoc, DrawDoc):
p = style_sheet.get_paragraph_style(pname) p = style_sheet.get_paragraph_style(pname)
x += self.paper.get_left_margin() x += self.paper.get_left_margin()
y = y + self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size()) y += self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size())
(text, fdef) = self.encode_text(p, text) (text, fdef) = self.encode_text(p, text)
self.file.write('gsave\n') self.file.write(
self.file.write('%s %s %s setrgbcolor\n' % lrgb(stype.get_color())) 'gsave\n'
self.file.write(fdef) '%s %s %s setrgbcolor\n' % lrgb(stype.get_color()) +
self.file.write('(%s) dup stringwidth pop -2 div ' % text) fdef +
self.file.write('%s cm add %s cm moveto ' % coords(self.translate(x, y))) '(%s) dup stringwidth pop -2 div ' % text +
self.file.write('show\n') '%s cm add %s cm moveto ' % coords(self.translate(x, y)) +
self.file.write('grestore\n') 'show\n'
'grestore\n'
)
def draw_text(self, style, text, x1, y1): def draw_text(self, style, text, x1, y1):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
@ -199,15 +209,17 @@ class PSDrawDoc(BaseDoc, DrawDoc):
pname = stype.get_paragraph_style() pname = stype.get_paragraph_style()
p = style_sheet.get_paragraph_style(pname) p = style_sheet.get_paragraph_style(pname)
x1 = x1 + self.paper.get_left_margin() x1 += self.paper.get_left_margin()
y1 = y1 + self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size()) y1 += self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size())
(text, fdef) = self.encode_text(p, text) (text, fdef) = self.encode_text(p, text)
self.file.write('gsave\n') self.file.write(
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1))) 'gsave\n'
self.file.write(fdef) '%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) +
self.file.write('(%s) show grestore\n' % text) fdef +
'(%s) show grestore\n' % text
)
def rotate_text(self, style, text, x, y, angle): def rotate_text(self, style, text, x, y, angle):
@ -224,22 +236,24 @@ class PSDrawDoc(BaseDoc, DrawDoc):
(new_text, fdef) = self.encode_text(p, text[0]) (new_text, fdef) = self.encode_text(p, text[0])
self.file.write('gsave\n')
self.file.write(fdef)
coords = self.translate(x, y) coords = self.translate(x, y)
self.file.write('%s cm %s cm translate\n' % ( self.file.write(
gformat(coords[0]),gformat(coords[1]))) 'gsave\n' +
self.file.write('%s rotate\n' % gformat(-angle)) fdef +
'%s cm %s cm translate\n' % (
gformat(coords[0]), gformat(coords[1])) +
'%s rotate\n' % gformat(-angle) +
'%s %s %s setrgbcolor\n' % lrgb(stype.get_color())
)
self.file.write('%s %s %s setrgbcolor\n' % lrgb(stype.get_color())) y = ((size * len(text)) / 2.0) - size
val = len(text)
y = ((size * val)/2.0) - size
for line in text: for line in text:
self.file.write('(%s) dup stringwidth pop -2 div ' self.file.write(
% self.encode(line)) '(%s) dup stringwidth pop -2 div '
self.file.write("%s moveto show\n" % gformat(y)) % self.encode(line) +
"%s moveto show\n" % gformat(y)
)
y -= size y -= size
self.file.write('grestore\n') self.file.write('grestore\n')
@ -247,9 +261,11 @@ class PSDrawDoc(BaseDoc, DrawDoc):
def draw_path(self, style, path): def draw_path(self, style, path):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style) stype = style_sheet.get_draw_style(style)
self.file.write('gsave\n') self.file.write(
self.file.write('newpath\n') 'gsave\n'
self.file.write('%s setlinewidth\n' % gformat(stype.get_line_width())) 'newpath\n'
'%s setlinewidth\n' % gformat(stype.get_line_width())
)
if stype.get_line_style() == SOLID: if stype.get_line_style() == SOLID:
self.file.write('[] 0 setdash\n') self.file.write('[] 0 setdash\n')
else: else:
@ -258,42 +274,52 @@ class PSDrawDoc(BaseDoc, DrawDoc):
point = path[0] point = path[0]
x1 = point[0] + self.paper.get_left_margin() x1 = point[0] + self.paper.get_left_margin()
y1 = point[1] + self.paper.get_top_margin() y1 = point[1] + self.paper.get_top_margin()
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1))) self.file.write(
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1))
)
for point in path[1:]: for point in path[1:]:
x1 = point[0] + self.paper.get_left_margin() x1 = point[0] + self.paper.get_left_margin()
y1 = point[1] + self.paper.get_top_margin() y1 = point[1] + self.paper.get_top_margin()
self.file.write('%s cm %s cm lineto\n' % coords(self.translate(x1, y1))) self.file.write(
'%s cm %s cm lineto\n' % coords(self.translate(x1, y1))
)
self.file.write('closepath\n') self.file.write('closepath\n')
color = stype.get_fill_color() color = stype.get_fill_color()
self.file.write('gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(color)) self.file.write(
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color())) 'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(color) +
self.file.write('grestore\n') '%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) +
'grestore\n'
)
def draw_line(self, style, x1, y1, x2, y2): def draw_line(self, style, x1, y1, x2, y2):
x1 = x1 + self.paper.get_left_margin() x1 += self.paper.get_left_margin()
x2 = x2 + self.paper.get_left_margin() x2 += self.paper.get_left_margin()
y1 = y1 + self.paper.get_top_margin() y1 += self.paper.get_top_margin()
y2 = y2 + self.paper.get_top_margin() y2 += self.paper.get_top_margin()
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style) stype = style_sheet.get_draw_style(style)
self.file.write('gsave newpath\n') self.file.write(
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1))) 'gsave newpath\n'
self.file.write('%s cm %s cm lineto\n' % coords(self.translate(x2, y2))) '%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) +
self.file.write('%s setlinewidth\n' % gformat(stype.get_line_width())) '%s cm %s cm lineto\n' % coords(self.translate(x2, y2)) +
'%s setlinewidth\n' % gformat(stype.get_line_width())
)
if stype.get_line_style() == SOLID: if stype.get_line_style() == SOLID:
self.file.write('[] 0 setdash\n') self.file.write('[] 0 setdash\n')
else: else:
self.file.write('[2 4] 0 setdash\n') self.file.write('[2 4] 0 setdash\n')
self.file.write('2 setlinecap\n') self.file.write(
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color())) '2 setlinecap\n' +
self.file.write('grestore\n') '%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) +
'grestore\n'
)
def draw_box(self, style, text, x, y, w, h): def draw_box(self, style, text, x, y, w, h):
x = x + self.paper.get_left_margin() x += self.paper.get_left_margin()
y = y + self.paper.get_top_margin() y += self.paper.get_top_margin()
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
@ -302,36 +328,45 @@ class PSDrawDoc(BaseDoc, DrawDoc):
shadsize = box_style.get_shadow_space() shadsize = box_style.get_shadow_space()
if box_style.get_shadow(): if box_style.get_shadow():
self.file.write('newpath\n') self.file.write(
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x+shadsize, y+shadsize))) 'newpath\n'
self.file.write('0 -%s cm rlineto\n' % gformat(h)) '%s cm %s cm moveto\n'
self.file.write('%s cm 0 rlineto\n' % gformat(w)) % coords(self.translate(x+shadsize, y+shadsize)) +
self.file.write('0 %s cm rlineto\n' % gformat(h)) '0 -%s cm rlineto\n' % gformat(h) +
self.file.write('closepath\n') '%s cm 0 rlineto\n' % gformat(w) +
self.file.write('.5 setgray\n') '0 %s cm rlineto\n' % gformat(h) +
self.file.write('fill\n') 'closepath\n'
self.file.write('newpath\n') '.5 setgray\n'
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x, y))) 'fill\n'
self.file.write('0 -%s cm rlineto\n' % gformat(h)) )
self.file.write('%s cm 0 rlineto\n' % gformat(w)) self.file.write(
self.file.write('0 %s cm rlineto\n' % gformat(h)) 'newpath\n'
self.file.write('closepath\n') '%s cm %s cm moveto\n' % coords(self.translate(x, y)) +
'0 -%s cm rlineto\n' % gformat(h) +
'%s cm 0 rlineto\n' % gformat(w) +
'0 %s cm rlineto\n' % gformat(h) +
'closepath\n'
)
fill_color = box_style.get_fill_color() fill_color = box_style.get_fill_color()
color = box_style.get_color() color = box_style.get_color()
self.file.write('gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(fill_color)) self.file.write(
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(color)) 'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(fill_color) +
'%s %s %s setrgbcolor stroke\n' % lrgb(color)
)
self.file.write('newpath\n') self.file.write('newpath\n')
if box_style.get_line_width(): if box_style.get_line_width():
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x, y))) self.file.write(
self.file.write('0 -%s cm rlineto\n' % gformat(h)) '%s cm %s cm moveto\n' % coords(self.translate(x, y)) +
self.file.write('%s cm 0 rlineto\n' % gformat(w)) '0 -%s cm rlineto\n' % gformat(h) +
self.file.write('0 %s cm rlineto\n' % gformat(h)) '%s cm 0 rlineto\n' % gformat(w) +
self.file.write('closepath\n') '0 %s cm rlineto\n' % gformat(h) +
self.file.write('%s setlinewidth\n' % gformat(box_style.get_line_width())) 'closepath\n' +
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(box_style.get_color())) '%s setlinewidth\n' % gformat(box_style.get_line_width()) +
if text != "": '%s %s %s setrgbcolor stroke\n' % lrgb(box_style.get_color())
)
if text:
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
assert( para_name != '' ) assert( para_name != '' )
p = style_sheet.get_paragraph_style(para_name) p = style_sheet.get_paragraph_style(para_name)
@ -339,14 +374,16 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write(fdef) self.file.write(fdef)
lines = text.split('\n') lines = text.split('\n')
nlines = len(lines)
mar = 10/28.35 mar = 10/28.35
f_in_cm = p.get_font().get_size()/28.35 f_in_cm = p.get_font().get_size()/28.35
fs = f_in_cm * 1.2 fs = f_in_cm * 1.2
center = y + (h + fs)/2.0 + (fs*shadsize) center = y + (h + fs)/2.0 + (fs*shadsize)
ystart = center - (fs/2.0) * nlines ystart = center - (fs/2.0) * len(lines)
for i in range(nlines): for i, line in enumerate(lines):
ypos = ystart + (i * fs) ypos = ystart + (i * fs)
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x+mar, ypos))) self.file.write(
self.file.write("(%s) show\n" % lines[i]) '%s cm %s cm moveto\n'
% coords(self.translate(x+mar, ypos)) +
"(%s) show\n" % lines[i]
)
self.file.write('grestore\n') self.file.write('grestore\n')

View File

@ -87,11 +87,14 @@ class RTFDoc(BaseDoc,TextDoc):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
self.f.write('{\\rtf1\\ansi\\ansicpg1252\\deff0\n') self.f.write(
self.f.write('{\\fonttbl\n') '{\\rtf1\\ansi\\ansicpg1252\\deff0\n'
self.f.write('{\\f0\\froman\\fcharset0\\fprq0 Times New Roman;}\n') '{\\fonttbl\n'
self.f.write('{\\f1\\fswiss\\fcharset0\\fprq0 Arial;}}\n') '{\\f0\\froman\\fcharset0\\fprq0 Times New Roman;}\n'
self.f.write('{\colortbl\n') '{\\f1\\fswiss\\fcharset0\\fprq0 Arial;}}\n'
'{\colortbl\n'
)
self.color_map = {} self.color_map = {}
index = 1 index = 1
self.color_map[(0,0,0)] = 0 self.color_map[(0,0,0)] = 0
@ -109,14 +112,16 @@ class RTFDoc(BaseDoc,TextDoc):
self.color_map[bgcolor] = index self.color_map[bgcolor] = index
index += 1 index += 1
self.f.write('}\n') self.f.write('}\n')
self.f.write('\\kerning0\\cf0\\viewkind1') self.f.write(
self.f.write('\\paperw%d' % twips(self.paper.get_size().get_width())) '\\kerning0\\cf0\\viewkind1' +
self.f.write('\\paperh%d' % twips(self.paper.get_size().get_height())) '\\paperw%d' % twips(self.paper.get_size().get_width()) +
self.f.write('\\margl%d' % twips(self.paper.get_left_margin())) '\\paperh%d' % twips(self.paper.get_size().get_height()) +
self.f.write('\\margr%d' % twips(self.paper.get_right_margin())) '\\margl%d' % twips(self.paper.get_left_margin()) +
self.f.write('\\margt%d' % twips(self.paper.get_top_margin())) '\\margr%d' % twips(self.paper.get_right_margin()) +
self.f.write('\\margb%d' % twips(self.paper.get_bottom_margin())) '\\margt%d' % twips(self.paper.get_top_margin()) +
self.f.write('\\widowctl\n') '\\margb%d' % twips(self.paper.get_bottom_margin()) +
'\\widowctl\n'
)
self.in_table = 0 self.in_table = 0
self.text = "" self.text = ""
@ -159,9 +164,11 @@ class RTFDoc(BaseDoc,TextDoc):
bgindex = self.color_map[p.get_background_color()] bgindex = self.color_map[p.get_background_color()]
fgindex = self.color_map[f.get_color()] fgindex = self.color_map[f.get_color()]
if f.get_type_face() == FONT_SERIF: if f.get_type_face() == FONT_SERIF:
self.font_type = '\\f0\\fs%d\\cf%d\\cb%d' % (size,fgindex,bgindex) self.font_type = '\\f0'
else: else:
self.font_type = '\\f1\\fs%d\\cf%d\\cb%d' % (size,fgindex,bgindex) self.font_type = '\\f1'
self.font_type += '\\fs%d\\cf%d\\cb%d' % (size,fgindex,bgindex)
if f.get_bold(): if f.get_bold():
self.font_type += "\\b" self.font_type += "\\b"
if f.get_underline(): if f.get_underline():
@ -177,9 +184,11 @@ class RTFDoc(BaseDoc,TextDoc):
self.f.write('\\qr') self.f.write('\\qr')
elif p.get_alignment() == PARA_ALIGN_CENTER: elif p.get_alignment() == PARA_ALIGN_CENTER:
self.f.write('\\qc') self.f.write('\\qc')
self.f.write('\\ri%d' % twips(p.get_right_margin())) self.f.write(
self.f.write('\\li%d' % twips(p.get_left_margin())) '\\ri%d' % twips(p.get_right_margin()) +
self.f.write('\\fi%d' % twips(p.get_first_indent())) '\\li%d' % twips(p.get_left_margin()) +
'\\fi%d' % twips(p.get_first_indent())
)
if p.get_alignment() == PARA_ALIGN_JUSTIFY: if p.get_alignment() == PARA_ALIGN_JUSTIFY:
self.f.write('\\qj') self.f.write('\\qj')
if p.get_padding(): if p.get_padding():

View File

@ -66,7 +66,7 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
pass pass
def start_page(self): def start_page(self):
self.page = self.page + 1 self.page += 1
if self.page != 1: if self.page != 1:
name = "%s-%d.svg" % (self.root, self.page) name = "%s-%d.svg" % (self.root, self.page)
else: else:
@ -81,11 +81,15 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
self.t = StringIO.StringIO() self.t = StringIO.StringIO()
self.f.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') self.f.write(
self.f.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" ') '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' +
self.f.write('"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n') '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" ' +
self.f.write('<svg width="%5.2fcm" height="%5.2fcm" ' % (self.paper.get_size().get_width(), self.paper.get_size().get_height())) '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n' +
self.f.write('xmlns="http://www.w3.org/2000/svg">\n') '<svg width="%5.2fcm" height="%5.2fcm" '
% (self.paper.get_size().get_width(),
self.paper.get_size().get_height()) +
'xmlns="http://www.w3.org/2000/svg">\n'
)
def rotate_text(self, style, text, x, y, angle): def rotate_text(self, style, text, x, y, angle):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
@ -95,8 +99,7 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
font = p.get_font() font = p.get_font()
size = font.get_size() size = font.get_size()
width = 0 width = height = 0
height = 0
for line in text: for line in text:
width = max(width, self.string_width(font, line)) width = max(width, self.string_width(font, line))
height += size height += size
@ -106,10 +109,12 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
xpos = (centerx - (width/2.0)) xpos = (centerx - (width/2.0))
ypos = (centery - (height/2.0)) ypos = (centery - (height/2.0))
self.t.write('<text ') self.t.write(
self.t.write('x="%4.2f" y="%4.2f" ' % (xpos, ypos)) '<text ' +
self.t.write('transform="rotate(%d %4.2f %4.2f)" ' % (angle, centerx, centery)) 'x="%4.2f" y="%4.2f" ' % (xpos, ypos) +
self.t.write('style="fill:#%02x%02x%02x; '% font.get_color()) 'transform="rotate(%d %4.2f %4.2f)" ' % (angle, centerx, centery) +
'style="fill:#%02x%02x%02x; '% font.get_color()
)
if font.get_bold(): if font.get_bold():
self.t.write('font-weight:bold;') self.t.write('font-weight:bold;')
if font.get_italic(): if font.get_italic():
@ -124,9 +129,11 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
for line in text: for line in text:
# Center this line relative to the rest of the text # Center this line relative to the rest of the text
linex = xpos + (width - self.string_width(font, line) ) / 2 linex = xpos + (width - self.string_width(font, line) ) / 2
self.t.write('<tspan x="%4.2f" dy="%d">' % (linex, size)) self.t.write(
self.t.write(line) '<tspan x="%4.2f" dy="%d">' % (linex, size) +
self.t.write('</tspan>') line +
'</tspan>'
)
self.t.write('</text>\n') self.t.write('</text>\n')
def end_page(self): def end_page(self):
@ -138,72 +145,89 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
self.f.close() self.f.close()
def draw_line(self, style, x1, y1, x2, y2): def draw_line(self, style, x1, y1, x2, y2):
x1 = x1 + self.paper.get_left_margin() x1 += self.paper.get_left_margin()
x2 = x2 + self.paper.get_left_margin() x2 += self.paper.get_left_margin()
y1 = y1 + self.paper.get_top_margin() y1 += self.paper.get_top_margin()
y2 = y2 + self.paper.get_top_margin() y2 += self.paper.get_top_margin()
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
s = style_sheet.get_draw_style(style) s = style_sheet.get_draw_style(style)
self.f.write('<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1, y1)) self.f.write(
self.f.write('x2="%4.2fcm" y2="%4.2fcm" ' % (x2, y2)) '<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1, y1) +
self.f.write('style="stroke:#%02x%02x%02x; ' % s.get_color()) 'x2="%4.2fcm" y2="%4.2fcm" ' % (x2, y2) +
self.f.write('stroke-width:%.2fpt;"/>\n' % s.get_line_width()) 'style="stroke:#%02x%02x%02x; ' % s.get_color() +
'stroke-width:%.2fpt;"/>\n' % s.get_line_width()
)
def draw_path(self, style, path): def draw_path(self, style, path):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style) stype = style_sheet.get_draw_style(style)
point = path[0] point = path[0]
self.f.write('<polygon fill="#%02x%02x%02x"' % stype.get_fill_color()) self.f.write(
self.f.write(' style="stroke:#%02x%02x%02x; ' % stype.get_color()) '<polygon fill="#%02x%02x%02x"' % stype.get_fill_color() +
self.f.write(' stroke-width:%.2fpt;"' % stype.get_line_width()) ' style="stroke:#%02x%02x%02x; ' % stype.get_color() +
self.f.write(' points="%.2f,%.2f' % units((point[0]+self.paper.get_left_margin(), point[1]+self.paper.get_top_margin()))) ' stroke-width:%.2fpt;"' % stype.get_line_width() +
' points="%.2f,%.2f'
% units((point[0]+self.paper.get_left_margin(),
point[1]+self.paper.get_top_margin()))
)
for point in path[1:]: for point in path[1:]:
self.f.write(' %.2f,%.2f' % units((point[0]+self.paper.get_left_margin(), point[1]+self.paper.get_top_margin()))) self.f.write(
' %.2f,%.2f'
% units((point[0]+self.paper.get_left_margin(),
point[1]+self.paper.get_top_margin()))
)
self.f.write('"/>\n') self.f.write('"/>\n')
def draw_box(self, style, text, x, y, w, h): def draw_box(self, style, text, x, y, w, h):
x = x + self.paper.get_left_margin() x += self.paper.get_left_margin()
y = y + self.paper.get_top_margin() y += self.paper.get_top_margin()
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
if box_style.get_shadow(): if box_style.get_shadow():
self.f.write('<rect ') self.f.write(
self.f.write('x="%4.2fcm" ' % (x+0.15)) '<rect ' +
self.f.write('y="%4.2fcm" ' % (y+0.15)) 'x="%4.2fcm" ' % (x+0.15) +
self.f.write('width="%4.2fcm" ' % w) 'y="%4.2fcm" ' % (y+0.15) +
self.f.write('height="%4.2fcm" ' % h) 'width="%4.2fcm" ' % w +
self.f.write('style="fill:#808080; stroke:#808080; stroke-width:1;"/>\n') 'height="%4.2fcm" ' % h +
self.f.write('<rect ') 'style="fill:#808080; stroke:#808080; stroke-width:1;"/>\n'
self.f.write('x="%4.2fcm" ' % x) )
self.f.write('y="%4.2fcm" ' % y)
self.f.write('width="%4.2fcm" ' % w) self.f.write(
self.f.write('height="%4.2fcm" ' % h) '<rect '
self.f.write('style="fill:#%02x%02x%02x; ' % box_style.get_fill_color()) 'x="%4.2fcm" ' % x +
self.f.write('stroke:#%02x%02x%02x; ' % box_style.get_color()) 'y="%4.2fcm" ' % y +
self.f.write('stroke-width:%f;"/>\n' % box_style.get_line_width()) 'width="%4.2fcm" ' % w +
if text != "": 'height="%4.2fcm" ' % h +
'style="fill:#%02x%02x%02x; ' % box_style.get_fill_color() +
'stroke:#%02x%02x%02x; ' % box_style.get_color() +
'stroke-width:%f;"/>\n' % box_style.get_line_width()
)
if text:
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
assert( para_name != '' ) assert( para_name != '' )
p = style_sheet.get_paragraph_style(para_name) p = style_sheet.get_paragraph_style(para_name)
font = p.get_font() font = p.get_font()
font_size = font.get_size() font_size = font.get_size()
lines = text.split('\n') lines = text.split('\n')
nlines = len(lines)
mar = 10/28.35 mar = 10/28.35
fs = (font_size/28.35) * 1.2 fs = (font_size/28.35) * 1.2
center = y + (h + fs)/2.0 + (fs*0.2) center = y + (h + fs)/2.0 + (fs*0.2)
ystart = center - (fs/2.0) * nlines ystart = center - (fs/2.0) * len(lines)
for i in range(nlines): for i, line in enumerate(lines):
ypos = ystart + (i * fs) ypos = ystart + (i * fs)
self.t.write('<text ') self.t.write(
self.t.write('x="%4.2fcm" ' % (x+mar)) '<text ' +
self.t.write('y="%4.2fcm" ' % ypos) 'x="%4.2fcm" ' % (x+mar) +
self.t.write('style="fill:#%02x%02x%02x; '% font.get_color()) 'y="%4.2fcm" ' % ypos +
'style="fill:#%02x%02x%02x; '% font.get_color()
)
if font.get_bold(): if font.get_bold():
self.t.write(' font-weight:bold;') self.t.write(' font-weight:bold;')
if font.get_italic(): if font.get_italic():
@ -213,13 +237,15 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
self.t.write(' font-family:sans-serif;') self.t.write(' font-family:sans-serif;')
else: else:
self.t.write(' font-family:serif;') self.t.write(' font-family:serif;')
self.t.write('">') self.t.write(
self.t.write(lines[i]) '">' +
self.t.write('</text>\n') line +
'</text>\n'
)
def draw_text(self, style, text, x, y): def draw_text(self, style, text, x, y):
x = x + self.paper.get_left_margin() x += self.paper.get_left_margin()
y = y + self.paper.get_top_margin() y += self.paper.get_top_margin()
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
@ -229,10 +255,12 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
font = p.get_font() font = p.get_font()
font_size = font.get_size() font_size = font.get_size()
fs = (font_size/28.35) * 1.2 fs = (font_size/28.35) * 1.2
self.t.write('<text ') self.t.write(
self.t.write('x="%4.2fcm" ' % x) '<text ' +
self.t.write('y="%4.2fcm" ' % (y+fs)) 'x="%4.2fcm" ' % x +
self.t.write('style="fill:#%02x%02x%02x;'% font.get_color()) 'y="%4.2fcm" ' % (y+fs) +
'style="fill:#%02x%02x%02x;'% font.get_color()
)
if font.get_bold(): if font.get_bold():
self.t.write('font-weight:bold;') self.t.write('font-weight:bold;')
if font.get_italic(): if font.get_italic():
@ -242,9 +270,11 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
self.t.write('font-family:sans-serif;') self.t.write('font-family:sans-serif;')
else: else:
self.t.write('font-family:serif;') self.t.write('font-family:serif;')
self.t.write('">') self.t.write(
self.t.write(text) '">' +
self.t.write('</text>\n') text +
'</text>\n'
)
def center_text(self, style, text, x, y): def center_text(self, style, text, x, y):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()