2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-11 06:42:06 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2009-03-19 07:54:29 +05:30
|
|
|
# Copyright (C) 2007-2009 Brian G. Matherly
|
2010-09-17 17:38:38 +05:30
|
|
|
# Copyright (C) 2010 Jakim Friant
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2004-08-21 02:56:51 +05:30
|
|
|
# $Id$
|
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
"""
|
|
|
|
SVG document generator.
|
|
|
|
"""
|
|
|
|
|
2003-02-04 00:54:27 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2007-06-15 09:37:41 +05:30
|
|
|
import StringIO
|
2003-02-04 00:54:27 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-09-17 17:38:38 +05:30
|
|
|
from gen.plug.docgen import BaseDoc, DrawDoc, SOLID, FONT_SANS_SERIF
|
2003-05-16 07:19:50 +05:30
|
|
|
import Errors
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-02-04 00:54:27 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# SvgDrawDoc
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-30 03:55:44 +05:30
|
|
|
class SvgDrawDoc(BaseDoc, DrawDoc):
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def __init__(self, styles, type):
|
|
|
|
BaseDoc.__init__(self, styles, type)
|
2002-10-20 19:55:16 +05:30
|
|
|
self.f = None
|
|
|
|
self.filename = None
|
|
|
|
self.level = 0
|
|
|
|
self.time = "0000-00-00T00:00:00"
|
2007-06-15 09:37:41 +05:30
|
|
|
self.page = 0
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def open(self, filename):
|
2002-10-20 19:55:16 +05:30
|
|
|
if filename[-4:] != ".svg":
|
|
|
|
self.root = filename
|
|
|
|
else:
|
|
|
|
self.root = filename[:-4]
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
2004-01-13 10:35:39 +05:30
|
|
|
def start_page(self):
|
2010-06-04 22:49:26 +05:30
|
|
|
self.page += 1
|
2002-10-20 19:55:16 +05:30
|
|
|
if self.page != 1:
|
2009-08-09 00:45:19 +05:30
|
|
|
name = "%s-%d.svg" % (self.root, self.page)
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
|
|
|
name = "%s.svg" % self.root
|
2003-05-16 07:19:50 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
self.f = open(name,"w")
|
|
|
|
except IOError,msg:
|
|
|
|
raise Errors.ReportError(_("Could not create %s") % name, msg)
|
|
|
|
except:
|
|
|
|
raise Errors.ReportError(_("Could not create %s") % name)
|
2007-06-15 09:37:41 +05:30
|
|
|
|
|
|
|
self.t = StringIO.StringIO()
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2010-06-04 22:49:26 +05:30
|
|
|
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'
|
|
|
|
)
|
2003-06-13 09:30:24 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def rotate_text(self, style, text, x, y, angle):
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
stype = style_sheet.get_draw_style(style)
|
2003-06-13 09:30:24 +05:30
|
|
|
pname = stype.get_paragraph_style()
|
2007-04-23 17:16:26 +05:30
|
|
|
p = style_sheet.get_paragraph_style(pname)
|
|
|
|
font = p.get_font()
|
2003-06-13 09:30:24 +05:30
|
|
|
size = font.get_size()
|
|
|
|
|
2010-06-04 22:49:26 +05:30
|
|
|
width = height = 0
|
2003-06-13 09:30:24 +05:30
|
|
|
for line in text:
|
2009-08-09 00:45:19 +05:30
|
|
|
width = max(width, self.string_width(font, line))
|
2007-06-15 09:37:41 +05:30
|
|
|
height += size
|
2003-06-13 09:30:24 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
centerx, centery = units(( x+self.paper.get_left_margin(),
|
2007-06-15 09:37:41 +05:30
|
|
|
y+self.paper.get_top_margin() ))
|
|
|
|
xpos = (centerx - (width/2.0))
|
|
|
|
ypos = (centery - (height/2.0))
|
2003-06-13 09:30:24 +05:30
|
|
|
|
2010-06-04 22:49:26 +05:30
|
|
|
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()
|
|
|
|
)
|
2007-06-15 09:37:41 +05:30
|
|
|
if font.get_bold():
|
2008-11-15 15:46:31 +05:30
|
|
|
self.t.write('font-weight:bold;')
|
2007-06-15 09:37:41 +05:30
|
|
|
if font.get_italic():
|
2008-11-15 15:46:31 +05:30
|
|
|
self.t.write('font-style:italic;')
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-size:%d; ' % size)
|
2009-05-30 03:55:44 +05:30
|
|
|
if font.get_type_face() == FONT_SANS_SERIF:
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-family:sans-serif;')
|
|
|
|
else:
|
|
|
|
self.t.write('font-family:serif;')
|
|
|
|
self.t.write('">')
|
|
|
|
|
2003-06-13 09:30:24 +05:30
|
|
|
for line in text:
|
2007-06-15 09:37:41 +05:30
|
|
|
# Center this line relative to the rest of the text
|
2009-08-09 00:45:19 +05:30
|
|
|
linex = xpos + (width - self.string_width(font, line) ) / 2
|
2010-06-04 22:49:26 +05:30
|
|
|
self.t.write(
|
|
|
|
'<tspan x="%4.2f" dy="%d">' % (linex, size) +
|
|
|
|
line +
|
|
|
|
'</tspan>'
|
|
|
|
)
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('</text>\n')
|
2003-06-13 09:30:24 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
def end_page(self):
|
2007-06-15 09:37:41 +05:30
|
|
|
# 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()
|
2002-10-20 19:55:16 +05:30
|
|
|
self.f.write('</svg>\n')
|
|
|
|
self.f.close()
|
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def draw_line(self, style, x1, y1, x2, y2):
|
2010-06-04 22:49:26 +05:30
|
|
|
x1 += self.paper.get_left_margin()
|
|
|
|
x2 += self.paper.get_left_margin()
|
|
|
|
y1 += self.paper.get_top_margin()
|
|
|
|
y2 += self.paper.get_top_margin()
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
s = style_sheet.get_draw_style(style)
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2010-09-17 17:38:38 +05:30
|
|
|
line_out = '<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1, y1)
|
|
|
|
line_out += 'x2="%4.2fcm" y2="%4.2fcm" ' % (x2, y2)
|
|
|
|
line_out += 'style="stroke:#%02x%02x%02x; ' % s.get_color()
|
|
|
|
if s.get_line_style() != SOLID:
|
|
|
|
line_out += 'stroke-dasharray: %s; ' % (",".join([str(d) for d in s.get_dash_style()]))
|
|
|
|
line_out += 'stroke-width:%.2fpt;"/>\n' % s.get_line_width()
|
|
|
|
self.f.write(line_out)
|
2003-02-09 05:14:44 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def draw_path(self, style, path):
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
stype = style_sheet.get_draw_style(style)
|
2003-02-09 05:14:44 +05:30
|
|
|
|
|
|
|
point = path[0]
|
2010-09-17 17:38:38 +05:30
|
|
|
line_out = '<polygon fill="#%02x%02x%02x"' % stype.get_fill_color()
|
|
|
|
line_out += ' style="stroke:#%02x%02x%02x; ' % stype.get_color()
|
|
|
|
if stype.get_line_style() != SOLID:
|
|
|
|
line_out += 'stroke-dasharray: %s; ' % (",".join([str(d) for d in stype.get_dash_style()]))
|
|
|
|
line_out += ' stroke-width:%.2fpt;"' % stype.get_line_width()
|
|
|
|
line_out += ' points="%.2f,%.2f' % units((point[0]+self.paper.get_left_margin(),
|
|
|
|
point[1]+self.paper.get_top_margin()))
|
|
|
|
self.f.write(line_out)
|
2003-02-09 05:14:44 +05:30
|
|
|
for point in path[1:]:
|
2010-06-04 22:49:26 +05:30
|
|
|
self.f.write(
|
|
|
|
' %.2f,%.2f'
|
|
|
|
% units((point[0]+self.paper.get_left_margin(),
|
|
|
|
point[1]+self.paper.get_top_margin()))
|
|
|
|
)
|
2003-02-09 05:14:44 +05:30
|
|
|
self.f.write('"/>\n')
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def draw_box(self, style, text, x, y, w, h):
|
2010-06-04 22:49:26 +05:30
|
|
|
x += self.paper.get_left_margin()
|
|
|
|
y += self.paper.get_top_margin()
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
box_style = style_sheet.get_draw_style(style)
|
2007-02-26 03:23:38 +05:30
|
|
|
|
2005-01-20 09:41:13 +05:30
|
|
|
if box_style.get_shadow():
|
2010-06-04 22:49:26 +05:30
|
|
|
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'
|
|
|
|
)
|
|
|
|
|
2010-09-17 17:38:38 +05:30
|
|
|
line_out = '<rect '
|
|
|
|
line_out += 'x="%4.2fcm" ' % x
|
|
|
|
line_out += 'y="%4.2fcm" ' % y
|
|
|
|
line_out += 'width="%4.2fcm" ' % w
|
|
|
|
line_out += 'height="%4.2fcm" ' % h
|
|
|
|
line_out += 'style="fill:#%02x%02x%02x; ' % box_style.get_fill_color()
|
|
|
|
line_out += 'stroke:#%02x%02x%02x; ' % box_style.get_color()
|
|
|
|
if box_style.get_line_style() != SOLID:
|
|
|
|
line_out += 'stroke-dasharray: %s; ' % (",".join([str(d) for d in box_style.get_dash_style()]))
|
|
|
|
line_out += 'stroke-width:%f;"/>\n' % box_style.get_line_width()
|
|
|
|
self.f.write(line_out)
|
2010-06-04 22:49:26 +05:30
|
|
|
|
|
|
|
if text:
|
2007-02-27 09:40:43 +05:30
|
|
|
para_name = box_style.get_paragraph_style()
|
|
|
|
assert( para_name != '' )
|
2007-04-23 17:16:26 +05:30
|
|
|
p = style_sheet.get_paragraph_style(para_name)
|
2002-10-20 19:55:16 +05:30
|
|
|
font = p.get_font()
|
|
|
|
font_size = font.get_size()
|
2006-03-11 06:42:06 +05:30
|
|
|
lines = text.split('\n')
|
2002-10-20 19:55:16 +05:30
|
|
|
mar = 10/28.35
|
|
|
|
fs = (font_size/28.35) * 1.2
|
2007-02-26 03:23:38 +05:30
|
|
|
center = y + (h + fs)/2.0 + (fs*0.2)
|
2010-06-04 22:49:26 +05:30
|
|
|
ystart = center - (fs/2.0) * len(lines)
|
|
|
|
for i, line in enumerate(lines):
|
2002-10-20 19:55:16 +05:30
|
|
|
ypos = ystart + (i * fs)
|
2010-06-04 22:49:26 +05:30
|
|
|
self.t.write(
|
|
|
|
'<text ' +
|
|
|
|
'x="%4.2fcm" ' % (x+mar) +
|
|
|
|
'y="%4.2fcm" ' % ypos +
|
|
|
|
'style="fill:#%02x%02x%02x; '% font.get_color()
|
|
|
|
)
|
2002-10-20 19:55:16 +05:30
|
|
|
if font.get_bold():
|
2008-11-15 15:46:31 +05:30
|
|
|
self.t.write(' font-weight:bold;')
|
2002-10-20 19:55:16 +05:30
|
|
|
if font.get_italic():
|
2008-11-15 15:46:31 +05:30
|
|
|
self.t.write(' font-style:italic;')
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write(' font-size:%d;' % font_size)
|
2009-05-30 03:55:44 +05:30
|
|
|
if font.get_type_face() == FONT_SANS_SERIF:
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write(' font-family:sans-serif;')
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write(' font-family:serif;')
|
2010-06-04 22:49:26 +05:30
|
|
|
self.t.write(
|
|
|
|
'">' +
|
|
|
|
line +
|
|
|
|
'</text>\n'
|
|
|
|
)
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2009-08-09 00:45:19 +05:30
|
|
|
def draw_text(self, style, text, x, y):
|
2010-06-04 22:49:26 +05:30
|
|
|
x += self.paper.get_left_margin()
|
|
|
|
y += self.paper.get_top_margin()
|
2003-02-04 00:54:27 +05:30
|
|
|
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
box_style = style_sheet.get_draw_style(style)
|
|
|
|
para_name = box_style.get_paragraph_style()
|
|
|
|
p = style_sheet.get_paragraph_style(para_name)
|
2003-02-04 00:54:27 +05:30
|
|
|
|
|
|
|
font = p.get_font()
|
|
|
|
font_size = font.get_size()
|
|
|
|
fs = (font_size/28.35) * 1.2
|
2010-06-04 22:49:26 +05:30
|
|
|
self.t.write(
|
|
|
|
'<text ' +
|
|
|
|
'x="%4.2fcm" ' % x +
|
|
|
|
'y="%4.2fcm" ' % (y+fs) +
|
|
|
|
'style="fill:#%02x%02x%02x;'% font.get_color()
|
|
|
|
)
|
2003-02-04 00:54:27 +05:30
|
|
|
if font.get_bold():
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-weight:bold;')
|
2003-02-04 00:54:27 +05:30
|
|
|
if font.get_italic():
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-style:italic;')
|
|
|
|
self.t.write('font-size:%d; ' % font_size)
|
2009-05-30 03:55:44 +05:30
|
|
|
if font.get_type_face() == FONT_SANS_SERIF:
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-family:sans-serif;')
|
2003-02-04 00:54:27 +05:30
|
|
|
else:
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('font-family:serif;')
|
2010-06-04 22:49:26 +05:30
|
|
|
self.t.write(
|
|
|
|
'">' +
|
|
|
|
text +
|
|
|
|
'</text>\n'
|
|
|
|
)
|
2003-02-09 05:14:44 +05:30
|
|
|
|
2007-02-12 10:25:11 +05:30
|
|
|
def center_text(self, style, text, x, y):
|
2007-04-23 17:16:26 +05:30
|
|
|
style_sheet = self.get_style_sheet()
|
|
|
|
box_style = style_sheet.get_draw_style(style)
|
2007-02-12 10:25:11 +05:30
|
|
|
para_name = box_style.get_paragraph_style()
|
2007-04-23 17:16:26 +05:30
|
|
|
p = style_sheet.get_paragraph_style(para_name)
|
2007-02-12 10:25:11 +05:30
|
|
|
font = p.get_font()
|
2009-08-09 00:45:19 +05:30
|
|
|
width = self.string_width(font, text) / 72
|
2009-09-11 00:19:48 +05:30
|
|
|
x -= width
|
2009-08-09 00:45:19 +05:30
|
|
|
self.draw_text(style, text, x, y)
|
2007-02-12 10:25:11 +05:30
|
|
|
|
2003-02-09 05:14:44 +05:30
|
|
|
def units(val):
|
|
|
|
return (val[0]*35.433, val[1]*35.433)
|