Improve text rendering for SVG reports (#0001072).

svn: r8553
This commit is contained in:
Brian Matherly 2007-06-15 04:07:41 +00:00
parent 9347d4c6ff
commit eb5135310f
2 changed files with 62 additions and 55 deletions

View File

@ -1,3 +1,6 @@
2007-06-14 Brian Matherly <brian@gramps-project.org>
* src/docgen/SvgDrawDoc.py: improve text rendering (#0001072)
2007-06-13 Alex Roitman <shura@gramps-project.org> 2007-06-13 Alex Roitman <shura@gramps-project.org>
* src/ViewManager.py: Enable recent files. * src/ViewManager.py: Enable recent files.
* src/DisplayState.py (RecentDocsMenu.load): work out directories; * src/DisplayState.py (RecentDocsMenu.load): work out directories;

View File

@ -27,6 +27,7 @@
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gettext import gettext as _ from gettext import gettext as _
import StringIO
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -75,6 +76,8 @@ class SvgDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
except: except:
raise Errors.ReportError(_("Could not create %s") % name) raise Errors.ReportError(_("Could not create %s") % name)
self.t = StringIO.StringIO()
self.f.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') 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('<!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('"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n')
@ -90,43 +93,44 @@ class SvgDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
size = font.get_size() size = font.get_size()
width = 0 width = 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
# rangle = -((pi/180.0) * angle) centerx,centery = units(( x+self.paper.get_left_margin(),
centerx,centery = units((x+self.paper.get_left_margin(),y+self.paper.get_top_margin())) y+self.paper.get_top_margin() ))
xpos = (centerx - (width/2.0))
ypos = (centery - (height/2.0))
yh = 0 self.t.write('<text ')
for line in text: self.t.write('x="%4.2f" y="%4.2f" ' % (xpos,ypos))
xw = self.string_width(font,line) self.t.write('transform="rotate(%d %4.2f %4.2f)" ' % (angle,centerx,centery))
self.t.write('style="fill:#%02x%02x%02x; '% font.get_color())
xpos = (centerx - (xw/2.0))
ypos = (centery)
xd = 0
yd = yh
# xd = yh * sin(-rangle)
# yd = yh * cos(-rangle)
self.f.write('<text ')
self.f.write('x="%4.2f" y="%4.2f" ' % (xpos+xd,ypos+yd))
# self.f.write('transform="rotate(%d) ' % angle)
# self.f.write(' translate(%.8f,%.8f)" ' % (-xpos,-ypos))
self.f.write('style="fill:#%02x%02x%02x; '% font.get_color())
if font.get_bold(): if font.get_bold():
self.f.write('font-weight:"bold";') self.t.write('font-weight:"bold";')
if font.get_italic(): if font.get_italic():
self.f.write('font-style:"italic";') self.t.write('font-style:"italic";')
self.f.write('font-size:%d; ' % size) self.t.write('font-size:%d; ' % size)
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF: if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
self.f.write('font-family:sans-serif;') self.t.write('font-family:sans-serif;')
else: else:
self.f.write('font-family:serif;') self.t.write('font-family:serif;')
self.f.write('">') self.t.write('">')
self.f.write(line)
self.f.write('</text>\n') for line in text:
yh += size # 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('</text>\n')
def end_page(self): def end_page(self):
# Print the text last for each page so that it is rendered on top of
# other graphic elements.
self.f.write(self.t.getvalue())
self.t.close()
self.f.write('</svg>\n') self.f.write('</svg>\n')
self.f.close() self.f.close()
@ -193,22 +197,22 @@ class SvgDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
ystart = center - (fs/2.0) * nlines ystart = center - (fs/2.0) * nlines
for i in range(nlines): for i in range(nlines):
ypos = ystart + (i * fs) ypos = ystart + (i * fs)
self.f.write('<text ') self.t.write('<text ')
self.f.write('x="%4.2fcm" ' % (x+mar)) self.t.write('x="%4.2fcm" ' % (x+mar))
self.f.write('y="%4.2fcm" ' % ypos) self.t.write('y="%4.2fcm" ' % ypos)
self.f.write('style="fill:#%02x%02x%02x; '% font.get_color()) self.t.write('style="fill:#%02x%02x%02x; '% font.get_color())
if font.get_bold(): if font.get_bold():
self.f.write(' font-weight:"bold";') self.t.write(' font-weight:"bold";')
if font.get_italic(): if font.get_italic():
self.f.write(' font-style:"italic";') self.t.write(' font-style:"italic";')
self.f.write(' font-size:%d;' % font_size) self.t.write(' font-size:%d;' % font_size)
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF: if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
self.f.write(' font-family:sans-serif;') self.t.write(' font-family:sans-serif;')
else: else:
self.f.write(' font-family:serif;') self.t.write(' font-family:serif;')
self.f.write('">') self.t.write('">')
self.f.write(lines[i]) self.f.write(lines[i])
self.f.write('</text>\n') self.t.write('</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 = x + self.paper.get_left_margin()
@ -222,22 +226,22 @@ class SvgDrawDoc(BaseDoc.BaseDoc,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.f.write('<text ') self.t.write('<text ')
self.f.write('x="%4.2fcm" ' % x) self.t.write('x="%4.2fcm" ' % x)
self.f.write('y="%4.2fcm" ' % (y+fs)) self.t.write('y="%4.2fcm" ' % (y+fs))
self.f.write('style="fill:#%02x%02x%02x;'% font.get_color()) self.t.write('style="fill:#%02x%02x%02x;'% font.get_color())
if font.get_bold(): if font.get_bold():
self.f.write('font-weight:bold;') self.t.write('font-weight:bold;')
if font.get_italic(): if font.get_italic():
self.f.write('font-style:italic;') self.t.write('font-style:italic;')
self.f.write('font-size:%d; ' % font_size) self.t.write('font-size:%d; ' % font_size)
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF: if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
self.f.write('font-family:sans-serif;') self.t.write('font-family:sans-serif;')
else: else:
self.f.write('font-family:serif;') self.t.write('font-family:serif;')
self.f.write('">') self.t.write('">')
self.f.write(text) self.t.write(text)
self.f.write('</text>\n') self.t.write('</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()