Optimize docgen backends by consolidating calls to write methods
svn: r15527
This commit is contained in:
parent
0901feda9e
commit
7244e05a25
File diff suppressed because it is too large
Load Diff
@ -66,32 +66,30 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
def fontdef(self, para):
|
||||
font = para.get_font()
|
||||
if font.get_type_face() == FONT_SERIF:
|
||||
font_name = "/Times"
|
||||
if font.get_bold():
|
||||
font_name += "-Bold"
|
||||
if font.get_italic():
|
||||
font_name = "/Times-BoldItalic"
|
||||
else:
|
||||
font_name = "/Times-Bold"
|
||||
font_name += "Italic"
|
||||
elif font.get_italic():
|
||||
font_name += "-Italic"
|
||||
else:
|
||||
if font.get_italic():
|
||||
font_name = "/Times-Italic"
|
||||
else:
|
||||
font_name = "/Times-Roman"
|
||||
else:
|
||||
font_name += "-Roman"
|
||||
|
||||
else: # Use a font without serifs
|
||||
font_name = "/Helvetica"
|
||||
if font.get_bold():
|
||||
font_name += "-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"
|
||||
font_name += "Oblique"
|
||||
elif font.get_italic():
|
||||
font_name += "-Oblique"
|
||||
|
||||
return "%s find-latin-font %d scalefont setfont\n" % (font_name, font.get_size())
|
||||
return "%s find-latin-font %d scalefont setfont\n" % \
|
||||
(font_name, font.get_size())
|
||||
|
||||
def translate(self, x, y):
|
||||
return (x, self.paper.get_size().get_height()-y)
|
||||
return (x, self.paper.get_size().get_height() - y)
|
||||
|
||||
def open(self, filename):
|
||||
"""
|
||||
@ -99,10 +97,9 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
|
||||
@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:
|
||||
self.file = open(self.filename,"w")
|
||||
@ -112,36 +109,43 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
except:
|
||||
raise Errors.ReportError(_("Could not create %s") % self.filename)
|
||||
|
||||
self.file.write('%!PS-Adobe-3.0\n')
|
||||
self.file.write('%%LanguageLevel: 2\n')
|
||||
self.file.write('%%Pages: (atend)\n')
|
||||
self.file.write('%%PageOrder: Ascend\n')
|
||||
self.file.write(
|
||||
'%!PS-Adobe-3.0\n'
|
||||
'%%LanguageLevel: 2\n'
|
||||
'%%Pages: (atend)\n'
|
||||
'%%PageOrder: Ascend\n'
|
||||
'%%Orientation: '
|
||||
)
|
||||
if self.paper.get_orientation() != PAPER_PORTRAIT:
|
||||
self.file.write('%%Orientation: Landscape\n')
|
||||
self.file.write('Landscape\n')
|
||||
else:
|
||||
self.file.write('%%Orientation: Portrait\n')
|
||||
self.file.write('%%EndComments\n')
|
||||
self.file.write('/cm { 28.34 mul } def\n')
|
||||
self.file.write('% build iso-latin-1 version of a font\n')
|
||||
self.file.write('/font-to-iso-latin-1 { % <font> font-to-iso-latin-1 <font>\n')
|
||||
self.file.write('%% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1\n')
|
||||
self.file.write('dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall\n')
|
||||
self.file.write('/Encoding ISOLatin1Encoding def currentdict end\n')
|
||||
self.file.write('dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn \n')
|
||||
self.file.write('exch definefont\n')
|
||||
self.file.write('} def\n')
|
||||
self.file.write('\n')
|
||||
self.file.write('/find-latin-font { % <name> find-latin-font <font>\n')
|
||||
self.file.write('findfont font-to-iso-latin-1\n')
|
||||
self.file.write('} def\n')
|
||||
self.file.write('Portrait\n')
|
||||
self.file.write(
|
||||
'%%EndComments\n'
|
||||
'/cm { 28.34 mul } def\n'
|
||||
'% build iso-latin-1 version of a font\n'
|
||||
'/font-to-iso-latin-1 { % <font> font-to-iso-latin-1 <font>\n'
|
||||
'%% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1\n'
|
||||
'dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall\n'
|
||||
'/Encoding ISOLatin1Encoding def currentdict end\n'
|
||||
'dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn \n'
|
||||
'exch definefont\n'
|
||||
'} def\n'
|
||||
'\n'
|
||||
'/find-latin-font { % <name> find-latin-font <font>\n'
|
||||
'findfont font-to-iso-latin-1\n'
|
||||
'} def\n'
|
||||
)
|
||||
|
||||
self.filename = filename
|
||||
|
||||
def close(self):
|
||||
self.file.write('%%Trailer\n')
|
||||
self.file.write('%%Pages: ')
|
||||
self.file.write('%d\n' % self.page)
|
||||
self.file.write('%%EOF\n')
|
||||
self.file.write(
|
||||
'%%Trailer\n'
|
||||
'%%Pages: ' +
|
||||
'%d\n' % self.page +
|
||||
'%%EOF\n'
|
||||
)
|
||||
self.file.close()
|
||||
if self.open_req:
|
||||
open_file_with_default_application(self.filename)
|
||||
@ -150,23 +154,27 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
pass
|
||||
|
||||
def start_page(self):
|
||||
self.page = self.page + 1
|
||||
self.file.write("%%Page:")
|
||||
self.file.write("%d %d\n" % (self.page, self.page))
|
||||
self.page += 1
|
||||
self.file.write(
|
||||
"%%Page:" +
|
||||
"%d %d\n" % (self.page, self.page)
|
||||
)
|
||||
if self.paper.get_orientation() != PAPER_PORTRAIT:
|
||||
self.file.write('90 rotate %s cm %s cm translate\n' % (
|
||||
gformat(0),gformat(-1*self.paper.get_size().get_height())))
|
||||
|
||||
def end_page(self):
|
||||
self.file.write('showpage\n')
|
||||
self.file.write('%%PageTrailer\n')
|
||||
self.file.write(
|
||||
'showpage\n'
|
||||
'%%PageTrailer\n'
|
||||
)
|
||||
|
||||
def encode(self, text):
|
||||
try:
|
||||
orig = unicode(text)
|
||||
new_text = orig.encode('iso-8859-1')
|
||||
except:
|
||||
new_text = "?"*len(text)
|
||||
new_text = "?" * len(text)
|
||||
return new_text
|
||||
|
||||
def encode_text(self, p, text):
|
||||
@ -181,17 +189,19 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
p = style_sheet.get_paragraph_style(pname)
|
||||
|
||||
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)
|
||||
|
||||
self.file.write('gsave\n')
|
||||
self.file.write('%s %s %s setrgbcolor\n' % lrgb(stype.get_color()))
|
||||
self.file.write(fdef)
|
||||
self.file.write('(%s) dup stringwidth pop -2 div ' % text)
|
||||
self.file.write('%s cm add %s cm moveto ' % coords(self.translate(x, y)))
|
||||
self.file.write('show\n')
|
||||
self.file.write('grestore\n')
|
||||
self.file.write(
|
||||
'gsave\n'
|
||||
'%s %s %s setrgbcolor\n' % lrgb(stype.get_color()) +
|
||||
fdef +
|
||||
'(%s) dup stringwidth pop -2 div ' % text +
|
||||
'%s cm add %s cm moveto ' % coords(self.translate(x, y)) +
|
||||
'show\n'
|
||||
'grestore\n'
|
||||
)
|
||||
|
||||
def draw_text(self, style, text, x1, y1):
|
||||
style_sheet = self.get_style_sheet()
|
||||
@ -199,15 +209,17 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
pname = stype.get_paragraph_style()
|
||||
p = style_sheet.get_paragraph_style(pname)
|
||||
|
||||
x1 = x1 + self.paper.get_left_margin()
|
||||
y1 = y1 + self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size())
|
||||
x1 += self.paper.get_left_margin()
|
||||
y1 += self.paper.get_top_margin() + ReportUtils.pt2cm(p.get_font().get_size())
|
||||
|
||||
(text, fdef) = self.encode_text(p, text)
|
||||
|
||||
self.file.write('gsave\n')
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1)))
|
||||
self.file.write(fdef)
|
||||
self.file.write('(%s) show grestore\n' % text)
|
||||
self.file.write(
|
||||
'gsave\n'
|
||||
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) +
|
||||
fdef +
|
||||
'(%s) show grestore\n' % text
|
||||
)
|
||||
|
||||
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])
|
||||
|
||||
self.file.write('gsave\n')
|
||||
self.file.write(fdef)
|
||||
coords = self.translate(x, y)
|
||||
self.file.write('%s cm %s cm translate\n' % (
|
||||
gformat(coords[0]),gformat(coords[1])))
|
||||
self.file.write('%s rotate\n' % gformat(-angle))
|
||||
self.file.write(
|
||||
'gsave\n' +
|
||||
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()))
|
||||
|
||||
val = len(text)
|
||||
y = ((size * val)/2.0) - size
|
||||
y = ((size * len(text)) / 2.0) - size
|
||||
|
||||
for line in text:
|
||||
self.file.write('(%s) dup stringwidth pop -2 div '
|
||||
% self.encode(line))
|
||||
self.file.write("%s moveto show\n" % gformat(y))
|
||||
self.file.write(
|
||||
'(%s) dup stringwidth pop -2 div '
|
||||
% self.encode(line) +
|
||||
"%s moveto show\n" % gformat(y)
|
||||
)
|
||||
y -= size
|
||||
|
||||
self.file.write('grestore\n')
|
||||
@ -247,53 +261,65 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
def draw_path(self, style, path):
|
||||
style_sheet = self.get_style_sheet()
|
||||
stype = style_sheet.get_draw_style(style)
|
||||
self.file.write('gsave\n')
|
||||
self.file.write('newpath\n')
|
||||
self.file.write('%s setlinewidth\n' % gformat(stype.get_line_width()))
|
||||
self.file.write(
|
||||
'gsave\n'
|
||||
'newpath\n'
|
||||
'%s setlinewidth\n' % gformat(stype.get_line_width())
|
||||
)
|
||||
if stype.get_line_style() == SOLID:
|
||||
self.file.write('[] 0 setdash\n')
|
||||
else:
|
||||
self.file.write('[2 4] 0 setdash\n')
|
||||
|
||||
point = path[0]
|
||||
x1 = point[0]+self.paper.get_left_margin()
|
||||
y1 = point[1]+self.paper.get_top_margin()
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1)))
|
||||
x1 = point[0] + self.paper.get_left_margin()
|
||||
y1 = point[1] + self.paper.get_top_margin()
|
||||
self.file.write(
|
||||
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1))
|
||||
)
|
||||
|
||||
for point in path[1:]:
|
||||
x1 = point[0]+self.paper.get_left_margin()
|
||||
y1 = point[1]+self.paper.get_top_margin()
|
||||
self.file.write('%s cm %s cm lineto\n' % coords(self.translate(x1, y1)))
|
||||
x1 = point[0] + self.paper.get_left_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('closepath\n')
|
||||
|
||||
color = stype.get_fill_color()
|
||||
self.file.write('gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(color))
|
||||
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()))
|
||||
self.file.write('grestore\n')
|
||||
self.file.write(
|
||||
'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(color) +
|
||||
'%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) +
|
||||
'grestore\n'
|
||||
)
|
||||
|
||||
def draw_line(self, style, x1, y1, x2, y2):
|
||||
x1 = x1 + self.paper.get_left_margin()
|
||||
x2 = x2 + self.paper.get_left_margin()
|
||||
y1 = y1 + self.paper.get_top_margin()
|
||||
y2 = y2 + self.paper.get_top_margin()
|
||||
x1 += self.paper.get_left_margin()
|
||||
x2 += self.paper.get_left_margin()
|
||||
y1 += self.paper.get_top_margin()
|
||||
y2 += self.paper.get_top_margin()
|
||||
style_sheet = self.get_style_sheet()
|
||||
stype = style_sheet.get_draw_style(style)
|
||||
self.file.write('gsave newpath\n')
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x1, y1)))
|
||||
self.file.write('%s cm %s cm lineto\n' % coords(self.translate(x2, y2)))
|
||||
self.file.write('%s setlinewidth\n' % gformat(stype.get_line_width()))
|
||||
self.file.write(
|
||||
'gsave newpath\n'
|
||||
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) +
|
||||
'%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:
|
||||
self.file.write('[] 0 setdash\n')
|
||||
else:
|
||||
self.file.write('[2 4] 0 setdash\n')
|
||||
|
||||
self.file.write('2 setlinecap\n')
|
||||
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()))
|
||||
self.file.write('grestore\n')
|
||||
self.file.write(
|
||||
'2 setlinecap\n' +
|
||||
'%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) +
|
||||
'grestore\n'
|
||||
)
|
||||
|
||||
def draw_box(self, style, text, x, y, w, h):
|
||||
x = x + self.paper.get_left_margin()
|
||||
y = y + self.paper.get_top_margin()
|
||||
x += self.paper.get_left_margin()
|
||||
y += self.paper.get_top_margin()
|
||||
|
||||
style_sheet = self.get_style_sheet()
|
||||
box_style = style_sheet.get_draw_style(style)
|
||||
@ -302,36 +328,45 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
|
||||
shadsize = box_style.get_shadow_space()
|
||||
if box_style.get_shadow():
|
||||
self.file.write('newpath\n')
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x+shadsize, y+shadsize)))
|
||||
self.file.write('0 -%s cm rlineto\n' % gformat(h))
|
||||
self.file.write('%s cm 0 rlineto\n' % gformat(w))
|
||||
self.file.write('0 %s cm rlineto\n' % gformat(h))
|
||||
self.file.write('closepath\n')
|
||||
self.file.write('.5 setgray\n')
|
||||
self.file.write('fill\n')
|
||||
self.file.write('newpath\n')
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x, y)))
|
||||
self.file.write('0 -%s cm rlineto\n' % gformat(h))
|
||||
self.file.write('%s cm 0 rlineto\n' % gformat(w))
|
||||
self.file.write('0 %s cm rlineto\n' % gformat(h))
|
||||
self.file.write('closepath\n')
|
||||
self.file.write(
|
||||
'newpath\n'
|
||||
'%s cm %s cm moveto\n'
|
||||
% coords(self.translate(x+shadsize, y+shadsize)) +
|
||||
'0 -%s cm rlineto\n' % gformat(h) +
|
||||
'%s cm 0 rlineto\n' % gformat(w) +
|
||||
'0 %s cm rlineto\n' % gformat(h) +
|
||||
'closepath\n'
|
||||
'.5 setgray\n'
|
||||
'fill\n'
|
||||
)
|
||||
self.file.write(
|
||||
'newpath\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()
|
||||
color = box_style.get_color()
|
||||
self.file.write('gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(fill_color))
|
||||
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(color))
|
||||
self.file.write(
|
||||
'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(fill_color) +
|
||||
'%s %s %s setrgbcolor stroke\n' % lrgb(color)
|
||||
)
|
||||
|
||||
self.file.write('newpath\n')
|
||||
if box_style.get_line_width():
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x, y)))
|
||||
self.file.write('0 -%s cm rlineto\n' % gformat(h))
|
||||
self.file.write('%s cm 0 rlineto\n' % gformat(w))
|
||||
self.file.write('0 %s cm rlineto\n' % gformat(h))
|
||||
self.file.write('closepath\n')
|
||||
self.file.write('%s setlinewidth\n' % gformat(box_style.get_line_width()))
|
||||
self.file.write('%s %s %s setrgbcolor stroke\n' % lrgb(box_style.get_color()))
|
||||
if text != "":
|
||||
self.file.write(
|
||||
'%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' +
|
||||
'%s setlinewidth\n' % gformat(box_style.get_line_width()) +
|
||||
'%s %s %s setrgbcolor stroke\n' % lrgb(box_style.get_color())
|
||||
)
|
||||
if text:
|
||||
para_name = box_style.get_paragraph_style()
|
||||
assert( para_name != '' )
|
||||
p = style_sheet.get_paragraph_style(para_name)
|
||||
@ -339,14 +374,16 @@ class PSDrawDoc(BaseDoc, DrawDoc):
|
||||
self.file.write(fdef)
|
||||
lines = text.split('\n')
|
||||
|
||||
nlines = len(lines)
|
||||
mar = 10/28.35
|
||||
f_in_cm = p.get_font().get_size()/28.35
|
||||
fs = f_in_cm * 1.2
|
||||
center = y + (h + fs)/2.0 + (fs*shadsize)
|
||||
ystart = center - (fs/2.0) * nlines
|
||||
for i in range(nlines):
|
||||
ystart = center - (fs/2.0) * len(lines)
|
||||
for i, line in enumerate(lines):
|
||||
ypos = ystart + (i * fs)
|
||||
self.file.write('%s cm %s cm moveto\n' % coords(self.translate(x+mar, ypos)))
|
||||
self.file.write("(%s) show\n" % lines[i])
|
||||
self.file.write(
|
||||
'%s cm %s cm moveto\n'
|
||||
% coords(self.translate(x+mar, ypos)) +
|
||||
"(%s) show\n" % lines[i]
|
||||
)
|
||||
self.file.write('grestore\n')
|
||||
|
@ -87,11 +87,14 @@ class RTFDoc(BaseDoc,TextDoc):
|
||||
|
||||
style_sheet = self.get_style_sheet()
|
||||
|
||||
self.f.write('{\\rtf1\\ansi\\ansicpg1252\\deff0\n')
|
||||
self.f.write('{\\fonttbl\n')
|
||||
self.f.write('{\\f0\\froman\\fcharset0\\fprq0 Times New Roman;}\n')
|
||||
self.f.write('{\\f1\\fswiss\\fcharset0\\fprq0 Arial;}}\n')
|
||||
self.f.write('{\colortbl\n')
|
||||
self.f.write(
|
||||
'{\\rtf1\\ansi\\ansicpg1252\\deff0\n'
|
||||
'{\\fonttbl\n'
|
||||
'{\\f0\\froman\\fcharset0\\fprq0 Times New Roman;}\n'
|
||||
'{\\f1\\fswiss\\fcharset0\\fprq0 Arial;}}\n'
|
||||
'{\colortbl\n'
|
||||
)
|
||||
|
||||
self.color_map = {}
|
||||
index = 1
|
||||
self.color_map[(0,0,0)] = 0
|
||||
@ -109,14 +112,16 @@ class RTFDoc(BaseDoc,TextDoc):
|
||||
self.color_map[bgcolor] = index
|
||||
index += 1
|
||||
self.f.write('}\n')
|
||||
self.f.write('\\kerning0\\cf0\\viewkind1')
|
||||
self.f.write('\\paperw%d' % twips(self.paper.get_size().get_width()))
|
||||
self.f.write('\\paperh%d' % twips(self.paper.get_size().get_height()))
|
||||
self.f.write('\\margl%d' % twips(self.paper.get_left_margin()))
|
||||
self.f.write('\\margr%d' % twips(self.paper.get_right_margin()))
|
||||
self.f.write('\\margt%d' % twips(self.paper.get_top_margin()))
|
||||
self.f.write('\\margb%d' % twips(self.paper.get_bottom_margin()))
|
||||
self.f.write('\\widowctl\n')
|
||||
self.f.write(
|
||||
'\\kerning0\\cf0\\viewkind1' +
|
||||
'\\paperw%d' % twips(self.paper.get_size().get_width()) +
|
||||
'\\paperh%d' % twips(self.paper.get_size().get_height()) +
|
||||
'\\margl%d' % twips(self.paper.get_left_margin()) +
|
||||
'\\margr%d' % twips(self.paper.get_right_margin()) +
|
||||
'\\margt%d' % twips(self.paper.get_top_margin()) +
|
||||
'\\margb%d' % twips(self.paper.get_bottom_margin()) +
|
||||
'\\widowctl\n'
|
||||
)
|
||||
self.in_table = 0
|
||||
self.text = ""
|
||||
|
||||
@ -159,9 +164,11 @@ class RTFDoc(BaseDoc,TextDoc):
|
||||
bgindex = self.color_map[p.get_background_color()]
|
||||
fgindex = self.color_map[f.get_color()]
|
||||
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:
|
||||
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():
|
||||
self.font_type += "\\b"
|
||||
if f.get_underline():
|
||||
@ -177,9 +184,11 @@ class RTFDoc(BaseDoc,TextDoc):
|
||||
self.f.write('\\qr')
|
||||
elif p.get_alignment() == PARA_ALIGN_CENTER:
|
||||
self.f.write('\\qc')
|
||||
self.f.write('\\ri%d' % twips(p.get_right_margin()))
|
||||
self.f.write('\\li%d' % twips(p.get_left_margin()))
|
||||
self.f.write('\\fi%d' % twips(p.get_first_indent()))
|
||||
self.f.write(
|
||||
'\\ri%d' % twips(p.get_right_margin()) +
|
||||
'\\li%d' % twips(p.get_left_margin()) +
|
||||
'\\fi%d' % twips(p.get_first_indent())
|
||||
)
|
||||
if p.get_alignment() == PARA_ALIGN_JUSTIFY:
|
||||
self.f.write('\\qj')
|
||||
if p.get_padding():
|
||||
|
@ -66,7 +66,7 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
pass
|
||||
|
||||
def start_page(self):
|
||||
self.page = self.page + 1
|
||||
self.page += 1
|
||||
if self.page != 1:
|
||||
name = "%s-%d.svg" % (self.root, self.page)
|
||||
else:
|
||||
@ -81,11 +81,15 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
|
||||
self.t = StringIO.StringIO()
|
||||
|
||||
self.f.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
|
||||
self.f.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" ')
|
||||
self.f.write('"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n')
|
||||
self.f.write('<svg width="%5.2fcm" height="%5.2fcm" ' % (self.paper.get_size().get_width(), self.paper.get_size().get_height()))
|
||||
self.f.write('xmlns="http://www.w3.org/2000/svg">\n')
|
||||
self.f.write(
|
||||
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' +
|
||||
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" ' +
|
||||
'"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\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):
|
||||
style_sheet = self.get_style_sheet()
|
||||
@ -95,8 +99,7 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
font = p.get_font()
|
||||
size = font.get_size()
|
||||
|
||||
width = 0
|
||||
height = 0
|
||||
width = height = 0
|
||||
for line in text:
|
||||
width = max(width, self.string_width(font, line))
|
||||
height += size
|
||||
@ -106,10 +109,12 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
xpos = (centerx - (width/2.0))
|
||||
ypos = (centery - (height/2.0))
|
||||
|
||||
self.t.write('<text ')
|
||||
self.t.write('x="%4.2f" y="%4.2f" ' % (xpos, ypos))
|
||||
self.t.write('transform="rotate(%d %4.2f %4.2f)" ' % (angle, centerx, centery))
|
||||
self.t.write('style="fill:#%02x%02x%02x; '% font.get_color())
|
||||
self.t.write(
|
||||
'<text ' +
|
||||
'x="%4.2f" y="%4.2f" ' % (xpos, ypos) +
|
||||
'transform="rotate(%d %4.2f %4.2f)" ' % (angle, centerx, centery) +
|
||||
'style="fill:#%02x%02x%02x; '% font.get_color()
|
||||
)
|
||||
if font.get_bold():
|
||||
self.t.write('font-weight:bold;')
|
||||
if font.get_italic():
|
||||
@ -124,9 +129,11 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
for line in text:
|
||||
# Center this line relative to the rest of the text
|
||||
linex = xpos + (width - self.string_width(font, line) ) / 2
|
||||
self.t.write('<tspan x="%4.2f" dy="%d">' % (linex, size))
|
||||
self.t.write(line)
|
||||
self.t.write('</tspan>')
|
||||
self.t.write(
|
||||
'<tspan x="%4.2f" dy="%d">' % (linex, size) +
|
||||
line +
|
||||
'</tspan>'
|
||||
)
|
||||
self.t.write('</text>\n')
|
||||
|
||||
def end_page(self):
|
||||
@ -138,72 +145,89 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
self.f.close()
|
||||
|
||||
def draw_line(self, style, x1, y1, x2, y2):
|
||||
x1 = x1 + self.paper.get_left_margin()
|
||||
x2 = x2 + self.paper.get_left_margin()
|
||||
y1 = y1 + self.paper.get_top_margin()
|
||||
y2 = y2 + self.paper.get_top_margin()
|
||||
x1 += self.paper.get_left_margin()
|
||||
x2 += self.paper.get_left_margin()
|
||||
y1 += self.paper.get_top_margin()
|
||||
y2 += self.paper.get_top_margin()
|
||||
|
||||
style_sheet = self.get_style_sheet()
|
||||
s = style_sheet.get_draw_style(style)
|
||||
|
||||
self.f.write('<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1, y1))
|
||||
self.f.write('x2="%4.2fcm" y2="%4.2fcm" ' % (x2, y2))
|
||||
self.f.write('style="stroke:#%02x%02x%02x; ' % s.get_color())
|
||||
self.f.write('stroke-width:%.2fpt;"/>\n' % s.get_line_width())
|
||||
self.f.write(
|
||||
'<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1, y1) +
|
||||
'x2="%4.2fcm" y2="%4.2fcm" ' % (x2, y2) +
|
||||
'style="stroke:#%02x%02x%02x; ' % s.get_color() +
|
||||
'stroke-width:%.2fpt;"/>\n' % s.get_line_width()
|
||||
)
|
||||
|
||||
def draw_path(self, style, path):
|
||||
style_sheet = self.get_style_sheet()
|
||||
stype = style_sheet.get_draw_style(style)
|
||||
|
||||
point = path[0]
|
||||
self.f.write('<polygon fill="#%02x%02x%02x"' % stype.get_fill_color())
|
||||
self.f.write(' style="stroke:#%02x%02x%02x; ' % stype.get_color())
|
||||
self.f.write(' stroke-width:%.2fpt;"' % stype.get_line_width())
|
||||
self.f.write(' points="%.2f,%.2f' % units((point[0]+self.paper.get_left_margin(), point[1]+self.paper.get_top_margin())))
|
||||
self.f.write(
|
||||
'<polygon fill="#%02x%02x%02x"' % stype.get_fill_color() +
|
||||
' style="stroke:#%02x%02x%02x; ' % stype.get_color() +
|
||||
' 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:]:
|
||||
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')
|
||||
|
||||
def draw_box(self, style, text, x, y, w, h):
|
||||
x = x + self.paper.get_left_margin()
|
||||
y = y + self.paper.get_top_margin()
|
||||
x += self.paper.get_left_margin()
|
||||
y += self.paper.get_top_margin()
|
||||
|
||||
style_sheet = self.get_style_sheet()
|
||||
box_style = style_sheet.get_draw_style(style)
|
||||
|
||||
if box_style.get_shadow():
|
||||
self.f.write('<rect ')
|
||||
self.f.write('x="%4.2fcm" ' % (x+0.15))
|
||||
self.f.write('y="%4.2fcm" ' % (y+0.15))
|
||||
self.f.write('width="%4.2fcm" ' % w)
|
||||
self.f.write('height="%4.2fcm" ' % h)
|
||||
self.f.write('style="fill:#808080; stroke:#808080; stroke-width:1;"/>\n')
|
||||
self.f.write('<rect ')
|
||||
self.f.write('x="%4.2fcm" ' % x)
|
||||
self.f.write('y="%4.2fcm" ' % y)
|
||||
self.f.write('width="%4.2fcm" ' % w)
|
||||
self.f.write('height="%4.2fcm" ' % h)
|
||||
self.f.write('style="fill:#%02x%02x%02x; ' % box_style.get_fill_color())
|
||||
self.f.write('stroke:#%02x%02x%02x; ' % box_style.get_color())
|
||||
self.f.write('stroke-width:%f;"/>\n' % box_style.get_line_width())
|
||||
if text != "":
|
||||
self.f.write(
|
||||
'<rect ' +
|
||||
'x="%4.2fcm" ' % (x+0.15) +
|
||||
'y="%4.2fcm" ' % (y+0.15) +
|
||||
'width="%4.2fcm" ' % w +
|
||||
'height="%4.2fcm" ' % h +
|
||||
'style="fill:#808080; stroke:#808080; stroke-width:1;"/>\n'
|
||||
)
|
||||
|
||||
self.f.write(
|
||||
'<rect '
|
||||
'x="%4.2fcm" ' % x +
|
||||
'y="%4.2fcm" ' % y +
|
||||
'width="%4.2fcm" ' % w +
|
||||
'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()
|
||||
assert( para_name != '' )
|
||||
p = style_sheet.get_paragraph_style(para_name)
|
||||
font = p.get_font()
|
||||
font_size = font.get_size()
|
||||
lines = text.split('\n')
|
||||
nlines = len(lines)
|
||||
mar = 10/28.35
|
||||
fs = (font_size/28.35) * 1.2
|
||||
center = y + (h + fs)/2.0 + (fs*0.2)
|
||||
ystart = center - (fs/2.0) * nlines
|
||||
for i in range(nlines):
|
||||
ystart = center - (fs/2.0) * len(lines)
|
||||
for i, line in enumerate(lines):
|
||||
ypos = ystart + (i * fs)
|
||||
self.t.write('<text ')
|
||||
self.t.write('x="%4.2fcm" ' % (x+mar))
|
||||
self.t.write('y="%4.2fcm" ' % ypos)
|
||||
self.t.write('style="fill:#%02x%02x%02x; '% font.get_color())
|
||||
self.t.write(
|
||||
'<text ' +
|
||||
'x="%4.2fcm" ' % (x+mar) +
|
||||
'y="%4.2fcm" ' % ypos +
|
||||
'style="fill:#%02x%02x%02x; '% font.get_color()
|
||||
)
|
||||
if font.get_bold():
|
||||
self.t.write(' font-weight:bold;')
|
||||
if font.get_italic():
|
||||
@ -213,13 +237,15 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
self.t.write(' font-family:sans-serif;')
|
||||
else:
|
||||
self.t.write(' font-family:serif;')
|
||||
self.t.write('">')
|
||||
self.t.write(lines[i])
|
||||
self.t.write('</text>\n')
|
||||
self.t.write(
|
||||
'">' +
|
||||
line +
|
||||
'</text>\n'
|
||||
)
|
||||
|
||||
def draw_text(self, style, text, x, y):
|
||||
x = x + self.paper.get_left_margin()
|
||||
y = y + self.paper.get_top_margin()
|
||||
x += self.paper.get_left_margin()
|
||||
y += self.paper.get_top_margin()
|
||||
|
||||
style_sheet = self.get_style_sheet()
|
||||
box_style = style_sheet.get_draw_style(style)
|
||||
@ -229,10 +255,12 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
font = p.get_font()
|
||||
font_size = font.get_size()
|
||||
fs = (font_size/28.35) * 1.2
|
||||
self.t.write('<text ')
|
||||
self.t.write('x="%4.2fcm" ' % x)
|
||||
self.t.write('y="%4.2fcm" ' % (y+fs))
|
||||
self.t.write('style="fill:#%02x%02x%02x;'% font.get_color())
|
||||
self.t.write(
|
||||
'<text ' +
|
||||
'x="%4.2fcm" ' % x +
|
||||
'y="%4.2fcm" ' % (y+fs) +
|
||||
'style="fill:#%02x%02x%02x;'% font.get_color()
|
||||
)
|
||||
if font.get_bold():
|
||||
self.t.write('font-weight:bold;')
|
||||
if font.get_italic():
|
||||
@ -242,9 +270,11 @@ class SvgDrawDoc(BaseDoc, DrawDoc):
|
||||
self.t.write('font-family:sans-serif;')
|
||||
else:
|
||||
self.t.write('font-family:serif;')
|
||||
self.t.write('">')
|
||||
self.t.write(text)
|
||||
self.t.write('</text>\n')
|
||||
self.t.write(
|
||||
'">' +
|
||||
text +
|
||||
'</text>\n'
|
||||
)
|
||||
|
||||
def center_text(self, style, text, x, y):
|
||||
style_sheet = self.get_style_sheet()
|
||||
|
Loading…
Reference in New Issue
Block a user