# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2006 Donald N. Allingham # Copyright (C) 2007-2009 Brian G. Matherly # # 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 # # $Id$ """ SVG document generator. """ #------------------------------------------------------------------------- # # python modules # #------------------------------------------------------------------------- from gettext import gettext as _ import StringIO #------------------------------------------------------------------------- # # Gramps modules # #------------------------------------------------------------------------- from gen.plug.docgen import BaseDoc, DrawDoc, FONT_SANS_SERIF import Errors #------------------------------------------------------------------------- # # SvgDrawDoc # #------------------------------------------------------------------------- class SvgDrawDoc(BaseDoc, DrawDoc): def __init__(self, styles, type): BaseDoc.__init__(self, styles, type) self.f = None self.filename = None self.level = 0 self.time = "0000-00-00T00:00:00" self.page = 0 def open(self, filename): if filename[-4:] != ".svg": self.root = filename else: self.root = filename[:-4] def close(self): pass def start_page(self): self.page = self.page + 1 if self.page != 1: name = "%s-%d.svg" % (self.root, self.page) else: name = "%s.svg" % self.root 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) self.t = StringIO.StringIO() self.f.write('\n') self.f.write('\n') self.f.write('\n') def rotate_text(self, style, text, x, y, angle): style_sheet = self.get_style_sheet() stype = style_sheet.get_draw_style(style) pname = stype.get_paragraph_style() p = style_sheet.get_paragraph_style(pname) font = p.get_font() size = font.get_size() width = 0 height = 0 for line in text: width = max(width, self.string_width(font, line)) height += size 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)) self.t.write('') 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('' % (linex, size)) self.t.write(line) self.t.write('') self.t.write('\n') 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('\n') 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() style_sheet = self.get_style_sheet() s = style_sheet.get_draw_style(style) self.f.write('\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('\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() style_sheet = self.get_style_sheet() box_style = style_sheet.get_draw_style(style) if box_style.get_shadow(): self.f.write('\n') self.f.write('\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): ypos = ystart + (i * fs) self.t.write('') self.t.write(lines[i]) self.t.write('\n') def draw_text(self, style, text, x, y): x = x + self.paper.get_left_margin() y = y + self.paper.get_top_margin() 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) font = p.get_font() font_size = font.get_size() fs = (font_size/28.35) * 1.2 self.t.write('') self.t.write(text) self.t.write('\n') def center_text(self, style, text, x, y): 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) font = p.get_font() width = self.string_width(font, text) / 72 x -= width self.draw_text(style, text, x, y) def units(val): return (val[0]*35.433, val[1]*35.433)