#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
# Copyright (C) 2005-2006 Serge Noiraud
# 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$
#-------------------------------------------------------------------------
#
# Standard Python Modules
#
#-------------------------------------------------------------------------
import os
try:
from hashlib import md5
except ImportError:
from md5 import md5
import zipfile
import time
import locale
from cStringIO import StringIO
from math import pi, cos, sin
from xml.sax.saxutils import escape
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from gen.plug import PluginManager, DocGenPlugin
from gen.plug.docgen import (BaseDoc, TextDoc, DrawDoc,
FONT_SANS_SERIF, DASHED, PAPER_PORTRAIT,
INDEX_TYPE_TOC, PARA_ALIGN_CENTER, PARA_ALIGN_LEFT,
INDEX_TYPE_ALP, PARA_ALIGN_RIGHT)
from gen.plug.docgen.fontscale import string_width
import const
from ReportBase import ReportUtils
import ImgManip
import Utils
import Errors
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
from gettext import gettext as _
_apptype = 'application/vnd.oasis.opendocument.text'
_esc_map = {
'\x1a' : '',
'\x0c' : '',
'\n' : '',
'\t' : '',
}
#-------------------------------------------------------------------------
#
# ODFDoc
#
#-------------------------------------------------------------------------
class ODFDoc(BaseDoc, TextDoc, DrawDoc):
def __init__(self, styles, type):
BaseDoc.__init__(self, styles, type)
self.media_list = []
self.cntnt = None
self.filename = None
self.level = 0
self.time = "0000-00-00T00:00:00"
self.new_page = 0
self.new_cell = 0
self.page = 0
self.first_page = 1
def open(self, filename):
t = time.localtime(time.time())
self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % \
(t[0], t[1], t[2], t[3], t[4], t[5])
if filename[-4:] != ".odt":
self.filename = filename + ".odt"
else:
self.filename = filename
self.filename = os.path.normpath(os.path.abspath(self.filename))
self.cntnt = StringIO()
def init(self):
assert (not self.init_called)
self.init_called = True
current_locale = locale.getlocale()
self.lang = current_locale[0]
if self.lang:
self.lang = self.lang.replace('_', '-')
else:
self.lang = "en-US"
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('')
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.write('')
self.cntnt.write('')
self.cntnt.write('\n')
styles = self.get_style_sheet()
for style_name in styles.get_draw_style_names():
style = styles.get_draw_style(style_name)
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
# Graphic style for items with a clear background
self.cntnt.write('\n')
self.cntnt.write('\t')
self.cntnt.write('\n')
for style_name in styles.get_paragraph_style_names():
style = styles.get_paragraph_style(style_name)
self.cntnt.write('\n' % style_name)
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write(' 0:
self.cntnt.write('fo:keep-with-next="true" ')
align = style.get_alignment()
if align == PARA_ALIGN_LEFT:
self.cntnt.write('fo:text-align="start" ')
elif align == PARA_ALIGN_RIGHT:
self.cntnt.write('fo:text-align="end" ')
elif align == PARA_ALIGN_CENTER:
self.cntnt.write('fo:text-align="center" ')
self.cntnt.write('style:justify-single-word="false" ')
else:
self.cntnt.write('fo:text-align="justify" ')
self.cntnt.write('style:justify-single-word="false" ')
font = style.get_font()
if font.get_type_face() == FONT_SANS_SERIF:
self.cntnt.write('style:font-name="Arial" ')
else:
self.cntnt.write('style:font-name="Times New Roman" ')
self.cntnt.write('fo:font-size="%.2fpt" ' % font.get_size())
self.cntnt.write('style:font-size-asian="%.2fpt" ' % font.get_size())
color = font.get_color()
self.cntnt.write('fo:color="#%02x%02x%02x" ' % color)
if font.get_bold():
self.cntnt.write('fo:font-weight="bold" ')
if font.get_italic():
self.cntnt.write('fo:font-style="italic" ')
if font.get_underline():
self.cntnt.write('style:text-underline="single" ')
self.cntnt.write('style:text-underline-color="font-color" ')
self.cntnt.write('fo:text-indent="%.2fcm"\n' % style.get_first_indent())
self.cntnt.write('fo:margin-right="%.2fcm"\n' % style.get_right_margin())
self.cntnt.write('fo:margin-left="%.2fcm"\n' % style.get_left_margin())
self.cntnt.write('fo:margin-top="0.00cm"\n')
self.cntnt.write('fo:margin-bottom="0.212cm" ')
self.cntnt.write('/>\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write(' ' % font.get_size())
self.cntnt.write('\n')
for style_name in styles.get_table_style_names():
style = styles.get_table_style(style_name)
self.cntnt.write('\n')
table_width = float(self.get_usable_width())
table_width_str = "%.2f" % table_width
self.cntnt.write('\n')
self.cntnt.write('\n')
for col in range(0, style.get_columns()):
self.cntnt.write('')
width = table_width * float(style.get_column_width(col)/100.0)
width_str = "%.4f" % width
self.cntnt.write('' % width_str)
self.cntnt.write('\n')
for cell in styles.get_cell_style_names():
cell_style = styles.get_cell_style(cell)
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
#Begin photo style
self.cntnt.write('')
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.write('')
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.write(' ')
self.cntnt.write(' ')
self.cntnt.write('\n')
self.cntnt.write('')
self.cntnt.write('')
self.cntnt.write('\n')
#end of Photo style edits
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write(' \n')
self.cntnt.write(' \n')
def close(self):
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('\n')
self._write_styles_file()
self._write_manifest()
self._write_meta_file()
self._write_mimetype_file()
self._write_zip()
if self.open_req:
Utils.open_file_with_default_application(self.filename)
def add_media_object(self, file_name, pos, x_cm, y_cm, alt=''):
# try to open the image. If the open fails, it probably wasn't
# a valid image (could be a PDF, or a non-image)
(x, y) = ImgManip.image_size(file_name)
if (x, y) == (0, 0):
return
ratio = float(x_cm)*float(y)/(float(y_cm)*float(x))
if ratio < 1:
act_width = x_cm
act_height = y_cm*ratio
else:
act_height = y_cm
act_width = x_cm/ratio
not_extension, extension = os.path.splitext(file_name)
odf_name = md5(file_name).hexdigest() + extension
media_list_item = (file_name, odf_name)
if not media_list_item in self.media_list:
self.media_list.append(media_list_item)
base = escape(os.path.basename(file_name))
tag = base.replace('.', '_')
if self.new_cell:
self.cntnt.write('')
if pos == "left":
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.write('\n')
if self.new_cell:
self.cntnt.write('\n')
def start_table(self, name, style_name):
self.cntnt.write('\n' % style_name)
styles = self.get_style_sheet()
table = styles.get_table_style(style_name)
for col in range(0, table.get_columns()):
self.cntnt.write('\n')
def end_table(self):
self.cntnt.write('\n')
def start_row(self):
self.cntnt.write('\n')
def end_row(self):
self.cntnt.write('\n')
def start_cell(self, style_name, span=1):
self.span = span
self.cntnt.write(' 1:
self.cntnt.write(' table:number-columns-spanned="%s">\n' % span)
else:
self.cntnt.write('>\n')
self.new_cell = 1
def end_cell(self):
self.cntnt.write('\n')
#for col in range(1, self.span):
# self.cntnt.write('\n')
self.new_cell = 0
def start_bold(self):
self.cntnt.write('')
def end_bold(self):
self.cntnt.write('')
def start_superscript(self):
self.cntnt.write('')
def end_superscript(self):
self.cntnt.write('')
def _add_zip(self, zfile, name, data, t):
zipinfo = zipfile.ZipInfo(name.encode('utf-8'))
zipinfo.date_time = t
zipinfo.compress_type = zipfile.ZIP_DEFLATED
zipinfo.external_attr = 0644 << 16L
zfile.writestr(zipinfo, data)
def _write_zip(self):
try:
zfile = zipfile.ZipFile(self.filename, "w", zipfile.ZIP_DEFLATED)
except IOError, msg:
errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg)
raise Errors.ReportError(errmsg)
except:
raise Errors.ReportError(_("Could not create %s") % self.filename)
t = time.localtime(time.time())[:6]
self._add_zip(zfile, "META-INF/manifest.xml", self.mfile.getvalue(), t)
self._add_zip(zfile, "content.xml", self.cntnt.getvalue(), t)
self._add_zip(zfile, "meta.xml", self.meta.getvalue(), t)
self._add_zip(zfile, "styles.xml", self.sfile.getvalue(), t)
self._add_zip(zfile, "mimetype", self.mimetype.getvalue(), t)
self.mfile.close()
self.cntnt.close()
self.meta.close()
self.sfile.close()
self.mimetype.close()
for image in self.media_list:
try:
ifile = open(image[0], mode='rb')
self._add_zip(zfile, "Pictures/%s" % image[1], ifile.read(), t)
ifile.close()
except:
print "Could not open %s" % image[0]
zfile.close()
def _write_styles_file(self):
self.sfile = StringIO()
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('')
self.sfile.write('')
self.sfile.write(' ')
self.sfile.write(' ')
self.sfile.write('')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write(' \n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write(' ')
self.sfile.write(' ')
self.sfile.write('\n')
self.sfile.write(' ')
self.sfile.write(' ')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
styles = self.get_style_sheet()
for style_name in styles.get_paragraph_style_names():
style = styles.get_paragraph_style(style_name)
self.sfile.write('\n')
self.sfile.write(' 0:
self.sfile.write('fo:keep-with-next="always" ')
align = style.get_alignment()
if align == PARA_ALIGN_LEFT:
self.sfile.write('fo:text-align="start" ')
self.sfile.write('style:justify-single-word="false" ')
elif align == PARA_ALIGN_RIGHT:
self.sfile.write('fo:text-align="end" ')
elif align == PARA_ALIGN_CENTER:
self.sfile.write('fo:text-align="center" ')
self.sfile.write('style:justify-single-word="false" ')
else:
self.sfile.write('fo:text-align="justify" ')
self.sfile.write('style:justify-single-word="false" ')
self.sfile.write('fo:text-indent="%.2fcm" ' % style.get_first_indent())
self.sfile.write('style:auto-text-indent="false"/> ')
self.sfile.write('\n')
self.sfile.write('\n')
# Current no leading number format for headers
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
#self.sfile.write('\n')
self.sfile.write(' ')
self.sfile.write(' ')
self.sfile.write(' ')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('')
self.sfile.write('')
self.sfile.write('\n')
self.sfile.write('')
self.sfile.write('')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
# header
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
# footer
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
#
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
self.sfile.write('\n')
# header
#self.sfile.write('')
#self.sfile.write('')
#self.sfile.write(' TITRE : %s' % self.title) # How to get the document title here ?
#self.sfile.write('')
#self.sfile.write('')
# footer
#self.sfile.write('')
#self.sfile.write('')
#self.sfile.write('1')
#self.sfile.write('/')
#self.sfile.write('1')
#self.sfile.write('')
#self.sfile.write('')
#self.sfile.write('')
#
self.sfile.write('')
self.sfile.write('\n')
self.sfile.write('\n')
def page_break(self):
self.new_page = 1
def start_page(self):
self.cntnt.write('\n')
def end_page(self):
self.cntnt.write('\n')
def start_paragraph(self, style_name, leader=None):
style_sheet = self.get_style_sheet()
style = style_sheet.get_paragraph_style(style_name)
self.level = style.get_header_level()
if self.new_page == 1:
self.new_page = 0
name = "NL%s" % style_name
else:
name = style_name
if self.level == 0:
self.cntnt.write('' % name)
else:
self.cntnt.write('')
if leader is not None:
self.cntnt.write(leader)
self.cntnt.write('')
self.new_cell = 0
def end_paragraph(self):
if self.level == 0:
self.cntnt.write('\n')
else:
self.cntnt.write('\n')
self.new_cell = 1
def write_note(self, text, format, style_name):
if format == 1:
text = escape(text, _esc_map)
# Replace multiple spaces: have to go from the largest number down
for n in range(text.count(' '), 1, -1):
text = text.replace(' '*n, ' ' % (n-1) )
self.start_paragraph(style_name)
self.cntnt.write('')
self.cntnt.write(text)
self.cntnt.write('')
self.end_paragraph()
elif format == 0:
for line in text.split('\n\n'):
self.start_paragraph(style_name)
line = line.replace('\n', ' ')
line = ' '.join(line.split())
self.write_text(line)
self.end_paragraph()
def write_text(self, text, mark=None):
"""
Uses the xml.sax.saxutils.escape function to convert XML
entities. The _esc_map dictionary allows us to add our own
mappings.
"""
if mark:
key = escape(mark.key, _esc_map)
key = key.replace('"', '"')
if mark.type == INDEX_TYPE_ALP:
self.cntnt.write('' % key)
elif mark.type == INDEX_TYPE_TOC:
self.cntnt.write('' % mark.level)
self.cntnt.write(escape(text, _esc_map))
def _write_manifest(self):
self.mfile = StringIO()
self.mfile.write('\n')
self.mfile.write('')
self.mfile.write('')
for image in self.media_list:
self.mfile.write('')
self.mfile.write('')
self.mfile.write('')
self.mfile.write('')
self.mfile.write('')
self.mfile.write('\n')
def _write_mimetype_file(self):
self.mimetype = StringIO()
self.mimetype.write('application/vnd.oasis.opendocument.text')
def _write_meta_file(self):
self.meta = StringIO()
self.meta.write('\n')
self.meta.write('\n');
self.meta.write('\n')
self.meta.write('')
self.meta.write(const.PROGRAM_NAME + ' ' + const.VERSION)
self.meta.write('\n')
self.meta.write('')
# It should be reasonable to have a true document title. but how ?
# self.title ?
#self.meta.write(_("Summary of %s") % self.name)
self.meta.write('\n')
self.meta.write('')
#self.meta.write(_("Summary of %s") % name)
self.meta.write('\n')
self.meta.write('')
self.meta.write('\n')
self.meta.write('')
self.meta.write(self.get_creator())
self.meta.write('\n')
self.meta.write('')
self.meta.write(self.time)
self.meta.write('\n')
self.meta.write('')
self.meta.write(self.get_creator())
self.meta.write('\n')
self.meta.write('')
self.meta.write(self.time)
self.meta.write('\n')
self.meta.write('')
self.meta.write('\n')
self.meta.write('0-00-00T00:00:00\n')
self.meta.write('%s\n' % self.lang)
self.meta.write('1\n')
self.meta.write('PT0S\n')
self.meta.write('http://gramps-project.org')
self.meta.write('\n')
self.meta.write('\n')
self.meta.write('\n')
self.meta.write('\n')
self.meta.write('\n')
self.meta.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()
height = size*(len(text))
width = 0
for line in text:
width = max(width, string_width(font, line))
wcm = ReportUtils.pt2cm(width)
hcm = ReportUtils.pt2cm(height)
rangle = (pi/180.0) * angle
self.cntnt.write('\n' % (xloc, yloc))
self.cntnt.write('\n')
self.cntnt.write('' % pname)
self.cntnt.write('' % pname)
self.cntnt.write(escape('\n'.join(text), _esc_map))
self.cntnt.write('\n\n')
self.cntnt.write('\n')
def draw_path(self, style, path):
minx = 9e12
miny = 9e12
maxx = 0
maxy = 0
for point in path:
minx = min(point[0], minx)
miny = min(point[1], miny)
maxx = max(point[0], maxx)
maxy = max(point[1], maxy)
self.cntnt.write('\n')
def draw_line(self, style, x1, y1, x2, y2):
self.cntnt.write('' % y2)
self.cntnt.write('\n')
self.cntnt.write('\n')
def draw_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()
pstyle = style_sheet.get_paragraph_style(para_name)
font = pstyle.get_font()
sw = ReportUtils.pt2cm(string_width(font, text))*1.3
self.cntnt.write('' % float(y))
self.cntnt.write(' ' )
self.cntnt.write('' % para_name)
self.cntnt.write('' % font.get_size() )
self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.write('\n')
def draw_box(self, style, text, x, y, w, h):
style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style)
para_name = box_style.get_paragraph_style()
shadow_width = box_style.get_shadow_space()
if box_style.get_shadow():
self.cntnt.write('\n' % (float(y)+shadow_width))
self.cntnt.write('\n')
self.cntnt.write('\n' % float(y))
if text != "":
self.cntnt.write('' % para_name)
self.cntnt.write('' % para_name)
self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('')
self.cntnt.write('\n')
self.cntnt.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()
pstyle = style_sheet.get_paragraph_style(para_name)
font = pstyle.get_font()
size = (string_width(font, text)/72.0) * 2.54
self.cntnt.write('\n' % float(y))
if text != "":
self.cntnt.write('')
self.cntnt.write('' % para_name)
self.cntnt.write('' % para_name)
self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('\n')
self.cntnt.write('\n')
self.cntnt.write('')
self.cntnt.write('\n')
#------------------------------------------------------------------------
#
# register_plugin
#
#------------------------------------------------------------------------
def register_plugin():
"""
Register the document generator with the GRAMPS plugin system.
"""
pmgr = PluginManager.get_instance()
plugin = DocGenPlugin(name = _('Open Document Text'),
description = _("Generates documents in Open "
"Document Text format (.odt)."),
basedoc = ODFDoc,
paper = True,
style = True,
extension = "odt" )
pmgr.register_plugin(plugin)
register_plugin()