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
|
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$
|
|
|
|
|
2003-02-04 00:54:27 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2007-06-15 09:37:41 +05:30
|
|
|
import StringIO
|
2003-02-04 00:54:27 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-03-19 07:54:29 +05:30
|
|
|
from gen.plug import PluginManager, DocGenPlugin
|
2009-05-31 20:29:56 +05:30
|
|
|
from gen.plug.docgen import BaseDoc, DrawDoc,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
|
|
|
|
2007-02-25 02:45:21 +05:30
|
|
|
def __init__(self,styles,type,template):
|
2009-05-30 03:55:44 +05:30
|
|
|
BaseDoc.__init__(self,styles,type,template)
|
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
|
|
|
|
|
|
|
def open(self,filename):
|
|
|
|
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):
|
2007-06-15 09:37:41 +05:30
|
|
|
self.page = self.page + 1
|
2002-10-20 19:55:16 +05:30
|
|
|
if self.page != 1:
|
|
|
|
name = "%s-%d.svg" % (self.root,self.page)
|
|
|
|
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
|
|
|
|
2002-12-01 09:10:47 +05:30
|
|
|
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')
|
2007-02-25 02:45:21 +05:30
|
|
|
self.f.write('<svg width="%5.2fcm" height="%5.2fcm" ' % (self.paper.get_size().get_width(),self.paper.get_size().get_height()))
|
2002-12-01 09:10:47 +05:30
|
|
|
self.f.write('xmlns="http://www.w3.org/2000/svg">\n')
|
2003-06-13 09:30:24 +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()
|
|
|
|
|
|
|
|
width = 0
|
2007-06-15 09:37:41 +05:30
|
|
|
height = 0
|
2003-06-13 09:30:24 +05:30
|
|
|
for line in text:
|
2004-08-21 02:56:51 +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
|
|
|
|
2007-06-15 09:37:41 +05:30
|
|
|
centerx,centery = units(( x+self.paper.get_left_margin(),
|
|
|
|
y+self.paper.get_top_margin() ))
|
|
|
|
xpos = (centerx - (width/2.0))
|
|
|
|
ypos = (centery - (height/2.0))
|
2003-06-13 09:30:24 +05:30
|
|
|
|
2007-06-15 09:37:41 +05:30
|
|
|
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())
|
|
|
|
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
|
|
|
|
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')
|
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()
|
|
|
|
|
|
|
|
def draw_line(self,style,x1,y1,x2,y2):
|
2007-02-25 02:45:21 +05:30
|
|
|
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()
|
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
|
|
|
|
2003-02-09 05:14:44 +05:30
|
|
|
self.f.write('<line x1="%4.2fcm" y1="%4.2fcm" ' % (x1,y1))
|
|
|
|
self.f.write('x2="%4.2fcm" y2="%4.2fcm" ' % (x2,y2))
|
2005-01-20 09:41:13 +05:30
|
|
|
self.f.write('style="stroke:#%02x%02x%02x; ' % s.get_color())
|
|
|
|
self.f.write('stroke-width:%.2fpt;"/>\n' % s.get_line_width())
|
2003-02-09 05:14:44 +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]
|
|
|
|
self.f.write('<polygon fill="#%02x%02x%02x"' % stype.get_fill_color())
|
|
|
|
self.f.write(' style="stroke:#%02x%02x%02x; ' % stype.get_color())
|
2003-06-13 09:30:24 +05:30
|
|
|
self.f.write(' stroke-width:%.2fpt;"' % stype.get_line_width())
|
2007-02-25 02:45:21 +05:30
|
|
|
self.f.write(' points="%.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
|
|
|
for point in path[1:]:
|
2007-02-25 02:45:21 +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
|
|
|
|
2007-02-26 03:23:38 +05:30
|
|
|
def draw_box(self,style,text,x,y, w, h):
|
2007-02-25 02:45:21 +05:30
|
|
|
x = x + self.paper.get_left_margin()
|
|
|
|
y = 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():
|
|
|
|
self.f.write('<rect ')
|
|
|
|
self.f.write('x="%4.2fcm" ' % (x+0.15))
|
|
|
|
self.f.write('y="%4.2fcm" ' % (y+0.15))
|
2007-02-26 03:23:38 +05:30
|
|
|
self.f.write('width="%4.2fcm" ' % w)
|
|
|
|
self.f.write('height="%4.2fcm" ' % h)
|
2005-01-20 09:41:13 +05:30
|
|
|
self.f.write('style="fill:#808080; stroke:#808080; stroke-width:1;"/>\n')
|
2002-10-20 19:55:16 +05:30
|
|
|
self.f.write('<rect ')
|
|
|
|
self.f.write('x="%4.2fcm" ' % x)
|
|
|
|
self.f.write('y="%4.2fcm" ' % y)
|
2007-02-26 03:23:38 +05:30
|
|
|
self.f.write('width="%4.2fcm" ' % w)
|
|
|
|
self.f.write('height="%4.2fcm" ' % h)
|
2005-01-20 09:41:13 +05:30
|
|
|
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())
|
2002-10-20 19:55:16 +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
|
|
|
nlines = len(lines)
|
|
|
|
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)
|
2002-10-20 19:55:16 +05:30
|
|
|
ystart = center - (fs/2.0) * nlines
|
|
|
|
for i in range(nlines):
|
|
|
|
ypos = ystart + (i * fs)
|
2007-06-15 09:37:41 +05:30
|
|
|
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())
|
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;')
|
|
|
|
self.t.write('">')
|
2008-01-21 09:52:51 +05:30
|
|
|
self.t.write(lines[i])
|
2007-06-15 09:37:41 +05:30
|
|
|
self.t.write('</text>\n')
|
2003-02-04 00:54:27 +05:30
|
|
|
|
|
|
|
def draw_text(self,style,text,x,y):
|
2007-02-25 02:45:21 +05:30
|
|
|
x = x + self.paper.get_left_margin()
|
|
|
|
y = 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
|
2007-06-15 09:37:41 +05:30
|
|
|
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())
|
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;')
|
|
|
|
self.t.write('">')
|
|
|
|
self.t.write(text)
|
|
|
|
self.t.write('</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()
|
|
|
|
font_size = font.get_size()
|
|
|
|
width = self.string_width(font,text) / 72
|
|
|
|
x = x - width
|
|
|
|
self.draw_text(style,text,x,y)
|
|
|
|
|
2003-02-09 05:14:44 +05:30
|
|
|
def units(val):
|
|
|
|
return (val[0]*35.433, val[1]*35.433)
|
|
|
|
|
2009-03-19 07:54:29 +05:30
|
|
|
#------------------------------------------------------------------------
|
2003-02-04 00:54:27 +05:30
|
|
|
#
|
2009-03-19 07:54:29 +05:30
|
|
|
# register_plugin
|
2003-02-04 00:54:27 +05:30
|
|
|
#
|
2009-03-19 07:54:29 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
def register_plugin():
|
|
|
|
"""
|
|
|
|
Register the document generator with the GRAMPS plugin system.
|
|
|
|
"""
|
|
|
|
pmgr = PluginManager.get_instance()
|
|
|
|
plugin = DocGenPlugin(name = _("SVG (Scalable Vector Graphics)"),
|
|
|
|
description = _("Generates documents in Scalable "
|
|
|
|
"Vector Graphics format (.svg)."),
|
|
|
|
basedoc = SvgDrawDoc,
|
|
|
|
paper = True,
|
|
|
|
style = True,
|
|
|
|
extension = "svg" )
|
|
|
|
pmgr.register_plugin(plugin)
|
|
|
|
|
|
|
|
register_plugin()
|