svn: r8957

This commit is contained in:
Don Allingham 2007-09-10 17:59:54 +00:00
parent ef3811668f
commit 59bb4c9ea9
5 changed files with 0 additions and 3833 deletions

View File

@ -1,351 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
# Copyright (C) 2007 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$
"""
Provides a BaseDoc based interface to the AbiWord document format.
"""
#-------------------------------------------------------------------------
#
# Python Modules
#
#-------------------------------------------------------------------------
import base64
from gettext import gettext as _
#-------------------------------------------------------------------------
#
# Gramps Modules
#
#-------------------------------------------------------------------------
import BaseDoc
import Errors
from PluginUtils import register_text_doc
import ImgManip
import Mime
import Utils
mime_type = ""
#-------------------------------------------------------------------------
#
# Class Definitions
#
#-------------------------------------------------------------------------
class AbiWordDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
"""AbiWord document generator. Inherits from the BaseDoc generic
document interface class."""
def __init__(self,styles,type,template):
"""Initializes the AbiWordDoc class, calling the __init__ routine
of the parent BaseDoc class"""
BaseDoc.BaseDoc.__init__(self,styles,type,template)
self.media_list = []
self.f = None
self.level = 0
self.new_page = 0
self.in_table = 0
self.in_paragraph = 0
def open(self,filename):
"""Opens the document, writing the necessary header information.
AbiWord uses an XML format, so the document format is pretty easy
to understand"""
if filename[-4:] != ".abw":
self.filename = "%s.abw" % filename
else:
self.filename = filename
try:
self.f = open(self.filename,"w")
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)
# doctype
self.f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
self.f.write('<!DOCTYPE abiword PUBLIC "-//ABISOURCE//DTD AWML 1.0 Strict//EN" ')
self.f.write('"http://www.abisource.com/awml.dtd">\n')
self.f.write('<abiword template="false" styles="unlocked" xmlns:fo="http://www.w3.org/1999/XSL/Format" ')
self.f.write('xmlns:svg="http://www.w3.org/2000/svg" xmlns:dc="http://purl.org/dc/elements/1.1/" ')
self.f.write('fileformat="1.1" xmlns:math="http://www.w3.org/1998/Math/MathML" ')
self.f.write('xmlns:awml="http://www.abisource.com/awml.dtd" xmlns="http://www.abisource.com/awml.dtd" ')
self.f.write('xmlns:xlink="http://www.w3.org/1999/xlink" version="1.9.1" xml:space="preserve" ')
self.f.write('props="lang:%s; dom-dir:ltr">\n' % Utils.xml_lang())
# metadata section
self.f.write('<metadata>\n')
self.f.write('<m key="dc.format">application/x-abiword</m>\n')
self.f.write('<m key="abiword.generator">AbiWord</m>\n')
self.f.write('<m key="abiword.date_last_changed">Mon May 19 14:16:24 2003</m>\n')
self.f.write('</metadata>\n')
self.write_styles()
# page size section
self.f.write('<pagesize ')
self.f.write('pagetype="%s" ' % self.paper.get_size().get_name())
if self.paper.get_orientation() == BaseDoc.PAPER_PORTRAIT:
self.f.write('orientation="portrait" ')
else:
self.f.write('orientation="landscape" ')
self.f.write('width="%.4f" ' % (self.paper.get_size().get_width()/2.54))
self.f.write('height="%.4f" ' % (self.paper.get_size().get_height()/2.54))
self.f.write('units="inch" page-scale="1.000000"/>\n')
self.f.write('<section ')
rmargin = float(self.paper.get_right_margin())/2.54
lmargin = float(self.paper.get_left_margin())/2.54
self.f.write('props="page-margin-right:%.4fin; ' % self.paper.get_right_margin())
self.f.write('page-margin-left:%.4fin"' % lmargin)
self.f.write('>\n')
def write_styles(self):
self.f.write('<styles>\n')
styles = self.get_style_sheet()
for style_name in styles.get_paragraph_style_names():
style = styles.get_paragraph_style(style_name)
self.current_style = style
self.f.write('<s type="P" name="%s" basedon="" followedby="" props="' % style_name)
self.f.write('margin-top:%.4fin; ' % (float(style.get_top_margin())/2.54))
self.f.write('margin-bottom:%.4fin; ' % (float(style.get_bottom_margin())/2.54))
if style.get_alignment() == BaseDoc.PARA_ALIGN_RIGHT:
self.f.write('text-align:right;')
elif style.get_alignment() == BaseDoc.PARA_ALIGN_LEFT:
self.f.write('text-align:left;')
elif style.get_alignment() == BaseDoc.PARA_ALIGN_CENTER:
self.f.write('text-align:center;')
else:
self.f.write('text-align:justify;')
rmargin = float(style.get_right_margin())/2.54
lmargin = float(style.get_left_margin())/2.54
indent = float(style.get_first_indent())/2.54
self.f.write(' margin-right:%.4fin;' % rmargin)
self.f.write(' margin-left:%.4fin;' % lmargin)
self.f.write(' tabstops:%.4fin/L;' % lmargin)
self.f.write(' text-indent:%.4fin;' % indent)
font = style.get_font()
self.f.write(' font-family:')
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
self.f.write('Arial; ')
else:
self.f.write('Times New Roman; ')
self.f.write('font-size:%dpt' % font.get_size())
if font.get_bold():
self.f.write('; font-weight:bold')
if font.get_italic():
self.f.write('; font-style:italic')
color = font.get_color()
if color != (0,0,0):
self.f.write('; color:%2x%2x%2x' % color)
if font.get_underline():
self.f.write('; text-decoration:underline')
self.f.write('"/>\n')
self.f.write('</styles>\n')
def close(self):
"""Write the trailing information and closes the file"""
self.f.write('</section>\n')
if self.media_list:
self.f.write('<data>\n')
for tag_number in range(len(self.media_list)):
name = self.media_list[tag_number]
img = ImgManip.ImgManip(name)
buf = img.png_data()
self.f.write(
'<d name="image%d" mime-type="image/png" base64="yes">\n'
% tag_number)
self.f.write(base64.encodestring(buf))
self.f.write('</d>\n')
self.f.write('</data>\n')
self.f.write('</abiword>\n')
self.f.close()
if self.print_req:
try:
app = Mime.get_application(mime_type)[0]
Utils.launch(app,self.filename)
except:
pass
def add_media_object(self,name,pos,x_cm,y_cm):
try:
image = ImgManip.ImgManip(name)
(x,y) = image.size()
except:
return
if y:
aspect_ratio = float(x)/float(y)
else:
aspect_ratio = 1
if aspect_ratio > x_cm/y_cm:
act_width = x_cm
act_height = y_cm/aspect_ratio
else:
act_height = y_cm
act_width = x_cm*aspect_ratio
if name in self.media_list:
tag_number = self.media_list.index(name)
else:
tag_number = len(self.media_list)
self.media_list.append(name)
if self.in_paragraph: # We cannot insert photo
start_p = end_p = '' # outside text paragraph.
else: # So if not in paragraph, insert one.
start_p = '<p>'
end_p = '</p>'
self.f.write('%s<image dataid="image%d" '
'props="height:%.3fcm; width:%.3fcm"/>%s '
% (start_p,tag_number,act_height,act_width,end_p))
def start_superscript(self):
self.f.write('<c props="text-position:superscript">')
def end_superscript(self):
self.f.write('</c>')
def start_paragraph(self,style_name,leader=None):
self.in_paragraph = 1
styles = self.get_style_sheet()
style = styles.get_paragraph_style(style_name)
self.current_style = style
self.f.write('<p style="%s">' % style_name)
if self.new_page == 1:
self.new_page = 0
self.f.write('<pbr/>')
if leader != None:
self.f.write(leader)
self.f.write('\t')
def page_break(self):
self.new_page = 1
def end_paragraph(self):
self.in_paragraph = 0
self.f.write('</p>\n')
def write_note(self,text,format,style_name):
if format == 1:
self.start_paragraph(style_name)
self.f.write('<c props="font-family:Courier">')
self.write_text(text)
self.f.write('</c>')
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):
text = text.replace('&','&amp;'); # Must be first
text = text.replace('<','&lt;');
text = text.replace('>','&gt;');
text = text.replace('&lt;super&gt;','<c props="text-position:superscript">')
text = text.replace('&lt;/super&gt;','</c>')
self.f.write(text)
def start_bold(self):
self.f.write('<c props="font-weight:bold">')
def end_bold(self):
self.f.write('</c>')
def start_table(self,name,style_name):
self.in_table = 1
styles = self.get_style_sheet()
self.tblstyle = styles.get_table_style(style_name)
self.f.write('<table props="table-column-props:')
width = float(self.paper.get_usable_width())
for col in range(0,self.tblstyle.get_columns()):
self.f.write("%.2fcm/" % ((self.tblstyle.get_column_width(col)/100.0) * width))
self.f.write('">\n')
self.current_row = -1
def end_table(self):
self.in_table = 0
self.f.write('</table>\n')
def start_row(self):
self.ledge = 0.0
self.col = 0
self.current_row += 1
def end_row(self):
pass
def start_cell(self,style_name,span=1):
styles = self.get_style_sheet()
self.cstyle = styles.get_cell_style(style_name)
self.f.write('<cell props="')
if not self.cstyle.get_top_border():
self.f.write('top-style:0; top-style:0;')
if not self.cstyle.get_bottom_border():
self.f.write('bot-style:0; bot-style:0;')
if not self.cstyle.get_right_border():
self.f.write('right-style:0; right-style:0;')
if not self.cstyle.get_left_border():
self.f.write('left-style:0; left-style:0;')
self.f.write('bot-attach:%d; ' % (self.current_row+1))
self.f.write('top-attach:%d; ' % self.current_row)
self.f.write('left-attach:%d; ' % self.col)
self.f.write('right-attach:%d"' % (self.col+span))
self.f.write('>\n')
self.col += span
def end_cell(self):
self.f.write('</cell>\n')
#--------------------------------------------------------------------------
#
# Register plugins
#
#--------------------------------------------------------------------------
try:
if Mime.mime_type_is_defined("application/x-abiword"):
mime_type = "application/x-abiword"
elif Mime.mime_type_is_defined("application/abiword"):
mime_type = "application/abiword"
prog = Mime.get_application(mime_type)
mtype = Mime.get_description(mime_type)
if Utils.search_for(prog[0]):
print_label=_("Open in %s") % prog[1]
else:
print_label=None
register_text_doc(mtype,AbiWordDoc,1,1,1,".abw", print_label)
except:
register_text_doc(_('AbiWord document'),AbiWordDoc,1,1,1,".abw", None)

View File

@ -1,541 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
# Copyright (C) 2007 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$
#------------------------------------------------------------------------
#
# Python modules
#
#------------------------------------------------------------------------
import time
import cStringIO
import os
import tarfile
from gettext import gettext as _
#------------------------------------------------------------------------
#
# Gramps modules
#
#------------------------------------------------------------------------
import BaseDoc
import Errors
from PluginUtils import register_text_doc
import ImgManip
import Mime
#------------------------------------------------------------------------
#
#
#
#------------------------------------------------------------------------
def points(val):
inch = float(val)/2.54
return (int(inch*72))
#------------------------------------------------------------------------
#
#
#
#------------------------------------------------------------------------
class KwordDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
def open(self,filename):
self.media_list = []
if filename[-4:] != ".kwd":
self.filename = filename + ".kwd"
else:
self.filename = filename
self.f = cStringIO.StringIO()
self.m = cStringIO.StringIO()
self.m.write('<?xml version="1.0" encoding="UTF-8"?>')
self.m.write('<!DOCTYPE document-info ><document-info>\n')
self.m.write('<log>\n')
self.m.write('<text></text>\n')
self.m.write('</log>\n')
self.m.write('<author>\n')
self.m.write('<full-name></full-name>\n')
self.m.write('<title></title>\n')
self.m.write('<company></company>\n')
self.m.write('<email></email>\n')
self.m.write('<telephone></telephone>\n')
self.m.write('<fax></fax>\n')
self.m.write('<country></country>\n')
self.m.write('<postal-code></postal-code>\n')
self.m.write('<city></city>\n')
self.m.write('<street></street>\n')
self.m.write('</author>\n')
self.m.write('<about>\n')
self.m.write('<abstract><![CDATA[]]></abstract>\n')
self.m.write('<title></title>\n')
self.m.write('</about>\n')
self.m.write('</document-info>\n')
self.f.write('<?xml version="1.0" encoding="UTF-8"?>')
self.f.write('<!DOCTYPE DOC >')
self.f.write('<DOC mime="application/x-kword" syntaxVersion="2" ')
self.f.write('editor="KWord" >\n')
self.mtime = time.time()
paper_name = self.paper.get_size().get_name()
if paper_name == "A3":
self.f.write('<PAPER format="0" ')
elif paper_name == "A4":
self.f.write('<PAPER format="1" ')
elif paper_name == "A5":
self.f.write('<PAPER format="2" ')
elif paper_name == "Letter":
self.f.write('<PAPER format="3" ')
elif paper_name == "Legal":
self.f.write('<PAPER format="4" ')
elif paper_name == "B5":
self.f.write('<PAPER format="7" ')
else:
self.f.write('<PAPER format="6" ')
self.f.write('width="%d" ' % points(self.paper.get_size().get_width()))
self.f.write('height="%d" ' % points(self.paper.get_size().get_height()))
if self.paper.get_orientation() == BaseDoc.PAPER_PORTRAIT:
self.f.write('orientation="0" ')
else:
self.f.write('orientation="1" ')
self.f.write('columns="1" ')
self.f.write('columnspacing="2.83" ')
self.f.write('hType="0" ')
self.f.write('fType="0" ')
self.f.write('spHeadBody="9" ')
self.f.write('spFootBody="9">\n')
self.f.write('<PAPERBORDERS ')
self.f.write('top="%d" ' % points(self.paper.get_top_margin()))
self.f.write('right="%d" ' % points(self.paper.get_right_margin()))
self.f.write('bottom="%d" ' % points(self.paper.get_bottom_margin()))
self.f.write('left="%d"/>' % points(self.paper.get_left_margin()))
self.f.write('</PAPER>\n')
self.f.write('<ATTRIBUTES processing="0" ')
self.f.write('standardpage="1" ')
self.f.write('hasTOC="0" ')
self.f.write('hasHeader="0" ')
self.f.write('hasFooter="0" ')
self.f.write('unit="mm"/>\n')
self.f.write('<FRAMESETS>\n')
self.f.write('<FRAMESET frameType="1" ')
self.f.write('frameInfo="0" ')
self.f.write('name="Frameset 1">\n')
self.f.write('<FRAME left="%d" ' % points(self.paper.get_left_margin()))
self.f.write('top="%d" ' % points(self.paper.get_top_margin()))
self.f.write('right="%d" ' % points(self.paper.get_size().get_width()-self.paper.get_right_margin()))
self.f.write('bottom="%d" ' % points(self.paper.get_size().get_height()-self.paper.get_bottom_margin()))
self.f.write('runaround="1" />\n')
self.cell_row= 0
self.cell_col= 0
self.frameset_flg= 1
self.table_no= 0
self.cell_style= ""
self.cell_span= 1
def close(self):
if self.frameset_flg == 1:
self.f.write('</FRAMESET>\n')
self.frameset_flg= 0
for p in self.media_list:
self.f.write('<FRAMESET frameType="2" frameInfo="0" ')
self.f.write('name="%s" visible="1">\n' % p[1])
self.f.write('<FRAME runaround="1" copy="0" newFrameBehaviour="1" ')
self.f.write('right="%d" ' % p[2])
self.f.write('left="0" ')
self.f.write('bottom="%d" ' % p[3])
self.f.write('top="0" ')
self.f.write('runaroundGap="2.8"/>\n')
self.f.write('<IMAGE keepAspectRatio="true">\n')
self.f.write('<KEY filename="%s" ' % p[1])
a = time.localtime(self.mtime)
self.f.write('msec="%d" ' % a[6])
self.f.write('second="%d" ' % a[5])
self.f.write('minute="%d" ' % a[4])
self.f.write('hour="%d" ' % a[3])
self.f.write('day="%d" ' % a[2])
self.f.write('month="%d" ' % a[1])
self.f.write('year="%d"/>\n' % a[0])
self.f.write('</IMAGE>\n')
self.f.write('</FRAMESET>\n')
self.f.write('</FRAMESETS>\n')
self.f.write('<STYLES>\n')
style_sheet = self.get_style_sheet()
for name in style_sheet.get_paragraph_style_names():
self.f.write('<STYLE>\n')
self.f.write('<NAME value="%s"/>\n' % name)
p = style_sheet.get_paragraph_style(name)
tpad = points(p.get_top_margin())
bpad = points(p.get_bottom_margin())
self.f.write('<OFFSETS before="%.4f" after="%.4f"/>\n' % (tpad,bpad))
if p.get_alignment() == BaseDoc.PARA_ALIGN_CENTER:
self.f.write('<FLOW value="center"/>\n')
elif p.get_alignment() == BaseDoc.PARA_ALIGN_JUSTIFY:
self.f.write('<FLOW value="justify"/>\n')
elif p.get_alignment() == BaseDoc.PARA_ALIGN_RIGHT:
self.f.write('<FLOW value="right"/>\n')
else:
self.f.write('<FLOW value="left"/>\n')
first = p.get_first_indent()
left = p.get_left_margin()
right = p.get_right_margin()
self.f.write('<INDENTS first="%d" ' % points(first))
self.f.write('left="%d" right="%d"/>\n' % (points(left),points(right)))
font = p.get_font()
self.f.write('<FORMAT>\n')
if font.get_type_face==BaseDoc.FONT_SANS_SERIF:
self.f.write('<FONT name="Bitstream Vera Serif"/>\n')
else:
self.f.write('<FONT name="Bitstream Vera Sans"/>\n')
self.f.write('<SIZE value="%d"/>\n' % font.get_size())
self.f.write('<COLOR red="%d" green="%d" blue="%d"/>\n' % font.get_color())
if font.get_bold():
self.f.write('<WEIGHT value="75"/>\n')
if font.get_italic():
self.f.write('<ITALIC value="1"/>\n')
if font.get_underline():
self.f.write('<UNDERLINE value="1"/>\n')
self.f.write('</FORMAT>\n')
if p.get_top_border():
self.f.write('<TOPBORDER red="0" green="0"')
self.f.write('blue="0" style="0" width="1"/>\n')
if p.get_bottom_border():
self.f.write('<BOTTOMBORDER red="0" green="0" ')
self.f.write('blue="0" style="0" width="1"/>\n')
if p.get_right_border():
self.f.write('<RIGHTBORDER red="0" green="0" ')
self.f.write('blue="0" style="0" width="1"/>\n')
if p.get_left_border():
self.f.write('<LEFTBORDER red="0" green="0" ')
self.f.write('blue="0" style="0" width="1"/>\n')
if left != 0:
self.f.write('<TABULATOR ptpos="%d" type="0"/>\n' % points(left))
self.f.write('</STYLE>\n')
self.f.write('</STYLES>\n')
self.f.write('<PIXMAPS>\n')
for filedata in self.media_list:
self.f.write('<KEY name="%s" filename="%s" ' % (filedata[1],filedata[1]))
a = time.localtime(self.mtime)
self.f.write('msec="%d" ' % a[6])
self.f.write('second="%d" ' % a[5])
self.f.write('minute="%d" ' % a[4])
self.f.write('hour="%d" ' % a[3])
self.f.write('day="%d" ' % a[2])
self.f.write('month="%d" ' % a[1])
self.f.write('year="%d"/>\n' % a[0])
self.f.write('</PIXMAPS>\n')
self.f.write('</DOC>\n')
try:
archive = tarfile.open(self.filename,'w:gz')
except IOError, msg:
text = _("Could not open %s") % self.filename
Errors.ReportError(text + "\n" + str(msg))
return
except:
Errors.ReportError(_("Could not open %s") % self.filename)
return
tarinfo = tarfile.TarInfo('documentinfo.xml')
tarinfo.size = len(self.m.getvalue())
tarinfo.mtime = self.mtime
if os.sys.platform != "win32":
tarinfo.uid = os.getuid()
tarinfo.gid = os.getgid()
self.m.seek(0)
archive.addfile(tarinfo,self.m)
tarinfo = tarfile.TarInfo('maindoc.xml')
tarinfo.size = len(self.f.getvalue())
tarinfo.mtime = self.mtime
if os.sys.platform != "win32":
tarinfo.uid = os.getuid()
tarinfo.gid = os.getgid()
self.f.seek(0)
archive.addfile(tarinfo,self.f)
for filedata in self.media_list:
archive.add(filedata[0])
archive.close()
self.f.close()
self.m.close()
if self.print_req:
apptype = 'application/x-kword'
app = Mime.get_application(apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app[0])
def start_paragraph(self,style_name,leader=None):
self.format_list = []
self.bold_start = 0
self.text = ""
self.style_name = style_name
style_sheet = self.get_style_sheet()
self.p = style_sheet.get_paragraph_style(self.style_name)
self.font = self.p.get_font()
if self.font.get_type_face() == BaseDoc.FONT_SERIF:
self.font_face = "Bitstream Vera Serif"
else:
self.font_face = "Bitstream Vera Sans"
if leader != None:
self.text = leader + '\t'
txt = '<FORMAT id="1" pos="0" len="%d">\n' % (len(leader)+1)
txt = txt + '<FONT name="%s"/>\n</FORMAT>\n' % self.font_face
self.format_list.append(txt)
self.bold_stop = len(self.text)
def end_paragraph(self):
if self.frameset_flg == 0:
self.f.write('<FRAMESET>\n')
self.frameset_flg= 1
if self.bold_start != 0 and self.bold_stop != len(self.text):
txt = '<FORMAT>\n<FONT name="%s"/>\n</FORMAT>\n' % self.font_face
self.format_list.append(txt)
self.f.write('<PARAGRAPH>\n')
self.f.write('<TEXT>')
self.f.write(unicode(self.text))
self.f.write('</TEXT>\n')
self.f.write('<FORMATS>\n')
for format in self.format_list:
self.f.write(format)
self.f.write('</FORMATS>\n')
self.f.write('<LAYOUT>\n')
self.f.write('<NAME value="%s"/>\n' % self.style_name)
tpad = points(self.p.get_top_margin())
bpad = points(self.p.get_bottom_margin())
self.f.write('<OFFSETS before="%.4f" after="%.4f"/>\n' % (tpad,bpad))
if self.p.get_alignment() == BaseDoc.PARA_ALIGN_CENTER:
self.f.write('<FLOW value="center"/>\n')
elif self.p.get_alignment() == BaseDoc.PARA_ALIGN_JUSTIFY:
self.f.write('<FLOW value="justify"/>\n')
elif self.p.get_alignment() == BaseDoc.PARA_ALIGN_RIGHT:
self.f.write('<FLOW value="right"/>\n')
else:
self.f.write('<FLOW value="left"/>\n')
first = self.p.get_first_indent()
left = self.p.get_left_margin()
right = self.p.get_right_margin()
self.f.write('<INDENTS first="%d" ' % points(first))
self.f.write('left="%d" right="%d"/>\n' % (points(left),points(right)))
self.f.write('<FORMAT>\n')
self.f.write('<FONT name="%s"/>\n' % self.font_face)
self.f.write('<SIZE value="%d"/>\n' % self.font.get_size())
self.f.write('<COLOR red="%d" green="%d" blue="%d"/>\n' % self.font.get_color())
if self.font.get_bold():
self.f.write('<WEIGHT value="75"/>\n')
if self.font.get_italic():
self.f.write('<ITALIC value="1"/>\n')
if self.font.get_underline():
self.f.write('<UNDERLINE value="1"/>\n')
if self.p.get_top_border():
self.f.write('<TOPBORDER red="0" green="0" blue="0" style="0" width="1"/>\n')
if self.p.get_bottom_border():
self.f.write('<BOTTOMBORDER red="0" green="0" blue="0" style="0" width="1"/>\n')
if self.p.get_right_border():
self.f.write('<RIGHTBORDER red="0" green="0" blue="0" style="0" width="1"/>\n')
if self.p.get_left_border():
self.f.write('<LEFTBORDER red="0" green="0" blue="0" style="0" width="1"/>\n')
self.f.write('</FORMAT>\n')
if left != 0:
self.f.write('<TABULATOR ptpos="%d" type="0"/>\n' % points(left))
self.f.write('</LAYOUT>\n')
self.f.write('</PARAGRAPH>\n')
def start_bold(self):
self.bold_start = len(self.text)
if self.bold_stop != self.bold_start:
length = self.bold_stop - self.bold_start
txt = '<FORMAT id="1" pos="%d" len="%d">\n' % (self.bold_stop,length)
txt = txt + '<FONT name="%s"/>\n</FORMAT>\n' % self.font_face
self.format_list.append(txt)
def end_bold(self):
self.bold_stop = len(self.text)
length = self.bold_stop - self.bold_start
txt = '<FORMAT id="1" pos="%d" len="%d">\n' % (self.bold_start,length)
txt = txt + '<FONT name="%s"/>\n<WEIGHT value="75"/>\n</FORMAT>\n' % self.font_face
self.format_list.append(txt)
def start_superscript(self):
self.sup_start = len(self.text)
def end_superscript(self):
length = len(self.text) - self.sup_start
txt = '<FORMAT id="1" pos="%d" len="%d">\n' % (self.sup_start, length)
txt = txt + '<VERTALIGN value="2"/></FORMAT>\n'
self.format_list.append(txt)
def start_table(self,name,style_name):
styles = self.get_style_sheet()
self.tbl = styles.get_table_style(style_name)
self.cell_left= (self.paper.get_left_margin() * 72)/ 2.54
self.tbl_width= ((self.paper.get_size().get_width() - self.paper.get_left_margin() - self.paper.get_right_margin()) * 72 ) / 2.54
if self.frameset_flg == 1:
self.f.write(' </FRAMESET> \n')
self.cell_row= 0
self.cell_col= 0
self.frameset_flg= 0
def end_table(self):
self.table_no= self.table_no + 1
def start_row(self):
pass
def end_row(self):
self.cell_row= self.cell_row + 1
self.cell_col= 0
self.cell_left= (self.paper.get_left_margin() * 72)/ 2.54
def start_cell(self,style_name,span=1):
self.cell_span= span
self.cell_style= style_name
self.cell_right = self.cell_left
for i in range(0,span):
col_width = self.tbl.get_column_width(self.cell_col+i)
spc = (self.tbl_width * col_width) / 100
self.cell_right = self.cell_right + spc
self.f.write('<FRAMESET removable="0" cols="%d" rows="1" ' % span)
self.f.write('grpMgr="Table %d" frameType="1" ' % self.table_no)
self.f.write('frameInfo="0" row="%d" col="%d" ' % (self.cell_row,self.cell_col))
self.f.write('name="Table %d Cell %d,%d" visible="1" >\n' % (self.table_no, self.cell_row, self.cell_col))
self.f.write('<FRAME bleftpt="2.83465" ')
self.f.write('copy="0" bbottompt="2.83465" btoppt="2.83465" ')
self.f.write('right="%d" left="%d" ' % (self.cell_right, self.cell_left))
self.f.write('newFrameBehaviour="1" brightpt="2.83465" ')
self.f.write('bottom="%d" runaroundGap="2.83465" ' % (self.cell_row*23+self.table_no*125+117))
self.f.write(' top="%d" autoCreateNewFrame="0" />\n' % (self.cell_row*23+self.table_no*125+95))
self.frameset_flg= 1
self.cell_col = self.cell_col + span - 1
def end_cell(self):
self.f.write('</FRAMESET>\n')
self.cell_col= self.cell_col + 1
self.frameset_flg= 0
self.cell_left= self.cell_right
def add_media_object(self,name,pos,x_cm,y_cm):
try:
im = ImgManip.ImgManip(name)
except:
return
(x,y)= im.size()
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
index = len(self.media_list)+1
tag = 'pictures/picture%d.jpeg' % index
self.media_list.append((name,tag,act_width,act_height))
txt = '<FORMAT id="6" pos="%d" len="1">\n' % len(self.text)
txt = txt + '<ANCHOR type="frameset" instance="%s"/>\n' % tag
txt = txt + '</FORMAT>\n'
self.bold_stop = len(self.text)
self.format_list.append(txt)
self.text = self.text + '#'
def write_note(self,text,format,style_name):
if format == 1:
self.start_paragraph(style_name)
self.write_text(text)
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):
text = text.replace('&','&amp;'); # Must be first
text = text.replace('<','&lt;');
text = text.replace('>','&gt;');
pos = text.find('&lt;super&gt;')
if pos >= 0:
self.start_pos = len(self.text)+pos
text = text.replace('&lt;super&gt;','')
pos = text.find('&lt;/super&gt;')
if pos >= 0:
end = len(self.text)+pos - self.start_pos
text = text.replace('&lt;super&gt;','')
txt = '<FORMAT id="1" pos="%d" len="%d">\n' % (self.start_pos, end)
txt = txt + '<VERTALIGN value="2"/></FORMAT>\n'
text = text.replace('&lt;/super&gt;','')
self.format_list.append(txt)
self.text = self.text + text
#------------------------------------------------------------------------
#
# Register the document generator with the GRAMPS plugin system
#
#------------------------------------------------------------------------
try:
import Utils
prog = Mime.get_application("application/x-kword")
mtype = Mime.get_description("application/x-kword")
if prog and Utils.search_for(prog[0]):
print_label=_("Open in %s") % prog[1]
else:
print_label=None
if mtype == _("unknown"):
mtype = _('KWord')
register_text_doc(mtype, KwordDoc, 1, 1, 1, ".kwd", print_label)
except:
register_text_doc(_('KWord'), KwordDoc, 1, 1, 1, ".kwd", print_label)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,682 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
# Copyright (C) 2007 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$
#------------------------------------------------------------------------
#
# Python modules
#
#------------------------------------------------------------------------
from gettext import gettext as _
#------------------------------------------------------------------------
#
# gramps modules
#
#------------------------------------------------------------------------
import BaseDoc
from PluginUtils import register_text_doc, register_draw_doc, register_book_doc
import Errors
from QuestionDialog import ErrorDialog
import ImgManip
import Mime
_H = 'Helvetica'
_HB = 'Helvetica-Bold'
_HO = 'Helvetica-Oblique'
_HBO = 'Helvetica-BoldOblique'
_T = 'Times-Roman'
_TB = 'Times-Bold'
_TI = 'Times-Italic'
_TBI = 'Times-BoldItalic'
#------------------------------------------------------------------------
#
# Set up logging
#
#------------------------------------------------------------------------
import logging
log = logging.getLogger(".PdfDoc")
#------------------------------------------------------------------------
#
# ReportLab python/PDF modules
#
#------------------------------------------------------------------------
try:
import reportlab.platypus.tables
from reportlab.platypus import *
from reportlab.lib.units import cm
from reportlab.lib.colors import Color
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
import reportlab.graphics.shapes
import reportlab.lib.styles
from reportlab.pdfbase.pdfmetrics import *
from reportlab.platypus.doctemplate import LayoutError
for faceName in reportlab.pdfbase.pdfmetrics.standardFonts:
reportlab.pdfbase.pdfmetrics.registerTypeFace(
reportlab.pdfbase.pdfmetrics.TypeFace(faceName))
except ImportError:
raise Errors.UnavailableError(_("Cannot be loaded because ReportLab is not installed"))
# Old reportlab versions < 2.0 did not work with utf8 or unicode
# so for those we need to encode text into latin1
# For the utf8-capable reportlab we should not.
def enc_latin1(s):
try:
new_s = s
return new_s.encode('iso-8859-1')
except:
return str(s)
def pass_through(s):
return s
from reportlab import Version as reportlab_version
version_tuple = tuple( [int(item) for item in reportlab_version.split('.')] )
if version_tuple < (2,0):
enc = enc_latin1
else:
enc = pass_through
# Only announce PIL warning once per use of Gramps
_pil_warn = True
#------------------------------------------------------------------------
#
# GrampsDocTemplate
#
#------------------------------------------------------------------------
class GrampsDocTemplate(BaseDocTemplate):
"""A document template for the ReportLab routines."""
def build(self,flowables):
"""Override the default build routine, to recalculate
for any changes in the document (margins, etc.)"""
self._calc()
BaseDocTemplate.build(self,flowables)
#------------------------------------------------------------------------
#
#
#
#------------------------------------------------------------------------
class PdfDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def open(self,filename):
if filename[-4:] != ".pdf":
self.filename = "%s.pdf" % filename
else:
self.filename = filename
page_w = self.paper.get_size().get_width() * cm
page_h = self.paper.get_size().get_height() * cm
self.pagesize = (page_w,page_h)
self.doc = GrampsDocTemplate(self.filename,
pagesize=self.pagesize,
allowSplitting=1,
_pageBreakQuick=0,
leftMargin=self.paper.get_left_margin()*cm,
rightMargin=self.paper.get_right_margin()*cm,
topMargin=self.paper.get_top_margin()*cm,
bottomMargin=self.paper.get_bottom_margin()*cm)
frameT = Frame(0,0,page_w,page_h,
self.paper.get_left_margin()*cm, self.paper.get_bottom_margin()*cm,
self.paper.get_right_margin()*cm,self.paper.get_top_margin()*cm,
id='normal')
ptemp = PageTemplate(frames=frameT,pagesize=self.pagesize)
self.doc.addPageTemplates([ptemp])
self.pdfstyles = {}
style_sheet = self.get_style_sheet()
for style_name in style_sheet.get_paragraph_style_names():
style = style_sheet.get_paragraph_style(style_name)
font = style.get_font()
pdf_style = reportlab.lib.styles.ParagraphStyle(name=style_name)
pdf_style.fontSize = font.get_size()
pdf_style.bulletFontSize = font.get_size()
pdf_style.leading = font.get_size()*1.2
pdf_style.fontName = self.pdf_set_font(font)
pdf_style.bulletFontName = pdf_style.fontName
pdf_style.rightIndent = style.get_right_margin()*cm
pdf_style.leftIndent = style.get_left_margin()*cm
pdf_style.firstLineIndent = style.get_first_indent()*cm
pdf_style.bulletIndent = pdf_style.firstLineIndent + \
pdf_style.leftIndent
align = style.get_alignment()
if align == BaseDoc.PARA_ALIGN_RIGHT:
pdf_style.alignment = TA_RIGHT
elif align == BaseDoc.PARA_ALIGN_LEFT:
pdf_style.alignment = TA_LEFT
elif align == BaseDoc.PARA_ALIGN_CENTER:
pdf_style.alignment = TA_CENTER
else:
pdf_style.alignment = TA_JUSTIFY
pdf_style.spaceBefore = style.get_top_margin()*cm
pdf_style.spaceAfter = style.get_bottom_margin()*cm
pdf_style.textColor = make_color(font.get_color())
self.pdfstyles[style_name] = pdf_style
self.story = []
self.in_table = 0
def close(self):
try:
self.doc.build(self.story)
except IOError,msg:
errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg)
raise Errors.ReportError(errmsg)
except LayoutError,msg:
if str(msg).rfind("too large on page") > -1:
errmsg = "Reportlab is unable to layout your report. " + \
"This is probably because you have some text that " + \
"is too large to fit on one page or in one cell. " + \
"Try changing some report options or use a " + \
"different output format."
else:
errmsg = "Reportlab is unable to layout your report. " + \
"Try changing some report options or use a " + \
"different output format."
raise Errors.ReportError(errmsg)
if self.print_req:
apptype = 'application/pdf'
app = Mime.get_application(apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app[0])
def page_break(self):
self.story.append(PageBreak())
def start_paragraph(self,style_name,leader=None):
style_sheet = self.get_style_sheet()
self.current_para = self.pdfstyles[style_name]
self.my_para = style_sheet.get_paragraph_style(style_name)
self.super = "<font size=%d><super>" \
% (self.my_para.get_font().get_size()-2)
if leader==None:
self.text = ''
else:
self.current_para.firstLineIndent = 0
self.text = '<bullet>%s</bullet>' % leader
def end_paragraph(self):
if self.in_table:
self.cur_cell.append(Paragraph(enc(self.text),self.current_para))
else:
self.story.append(Paragraph(enc(self.text),self.current_para))
def start_bold(self):
self.text = self.text + '<b>'
def end_bold(self):
self.text = self.text + '</b>'
def start_superscript(self):
fsize = self.my_para.get_font().get_size()
self.text = self.text + '<font size=%d><super>' % (fsize-2)
def end_superscript(self):
self.text = self.text + '</super></font>'
def start_table(self,name,style_name):
self.in_table = 1
styles = self.get_style_sheet()
self.cur_table = styles.get_table_style(style_name)
self.row = -1
self.col = 0
self.cur_row = []
self.table_data = []
self.tblstyle = []
self.text = ""
def end_table(self):
# Calculate optimal widths
self.cur_table_cols = []
width = float(self.cur_table.get_width()/100.0) * self.get_usable_width()
for val in range(self.cur_table.get_columns()):
percent = float(self.cur_table.get_column_width(val))/100.0
self.cur_table_cols.append(int(width * percent * cm))
ts = reportlab.platypus.tables.TableStyle(self.tblstyle)
tbl = reportlab.platypus.tables.Table(data=self.table_data,
colWidths=self.cur_table_cols,
style=ts)
self.story.append(tbl)
self.in_table = 0
def start_row(self):
self.row = self.row + 1
self.col = 0
self.cur_row = []
self.cur_table_cols = []
def end_row(self):
self.table_data.append(self.cur_row)
def start_cell(self,style_name,span=1):
self.span = span
styles = self.get_style_sheet()
self.my_table_style = styles.get_cell_style(style_name)
self.cur_cell = []
def end_cell(self):
if self.cur_cell:
self.cur_row.append(self.cur_cell)
else:
self.cur_row.append("")
# Fill in cells that this cell spans over
for val in range(1,self.span):
self.cur_row.append("")
p = self.my_para
f = p.get_font()
fn = self.pdf_set_font(f)
black = Color(0,0,0)
for inc in range(self.col,self.col+self.span):
loc = (inc,self.row)
self.tblstyle.append(('FONT', loc, loc, fn, f.get_size()))
if self.span == 1 or inc == self.col + self.span - 1:
if self.my_table_style.get_right_border():
self.tblstyle.append(('LINEAFTER', loc, loc, 1, black))
if self.span == 1 or inc == self.col:
if self.my_table_style.get_left_border():
self.tblstyle.append(('LINEBEFORE', loc, loc, 1, black))
if self.my_table_style.get_top_border():
self.tblstyle.append(('LINEABOVE', loc, loc, 1, black))
if self.my_table_style.get_bottom_border():
self.tblstyle.append(('LINEBELOW', loc, loc, 1, black))
# Set the alignment
if p.get_alignment() == BaseDoc.PARA_ALIGN_LEFT:
self.tblstyle.append(('ALIGN', loc, loc, 'LEFT'))
elif p.get_alignment() == BaseDoc.PARA_ALIGN_RIGHT:
self.tblstyle.append(('ALIGN', loc, loc, 'RIGHT'))
else:
self.tblstyle.append(('ALIGN', loc, loc, 'CENTER'))
self.tblstyle.append(('VALIGN', loc, loc, 'TOP'))
# The following lines will enable the span feature.
# This is nice, except when spanning, lines that would have overfilled
# their cells still increase the height of the cell to make room for the
# wrapped text (even though the text does not actually wrap because it
# is spanned)
#if self.span != 1:
# self.tblstyle.append(('SPAN', (self.col, self.row), (self.col + self.span - 1, self.row ) ))
# The following lines will enable the span feature.
# This is nice, except when spanning, lines that would have overfilled
# their cells still increase the height of the cell to make room for the
# wrapped text (even though the text does not actually wrap because it is spanned)
#if self.span != 1:
# self.tblstyle.append(('SPAN', (self.col, self.row), (self.col + self.span - 1, self.row ) ))
self.col = self.col + self.span
self.text = ""
def add_media_object(self,name,pos,x_cm,y_cm):
try:
img = ImgManip.ImgManip(name)
except:
return
x,y = img.size()
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
try:
# Reportlab uses PIL to layout images. Make sure it is installed.
import PIL
except:
global _pil_warn
if _pil_warn:
ErrorDialog(
_("You do not have the Python Imaging Library installed "
"Images will not be added to this report"))
_pil_warn = False
return
try:
im = Image(str(name),act_width*cm,act_height*cm)
except:
ErrorDialog( _("Reportlab is unable to add this image: %s") % name )
return
if pos in ['left','right','center']:
im.hAlign = pos.upper()
else:
im.hAlign = 'LEFT'
if self.in_table:
self.cur_cell.append(Spacer(1,0.5*cm))
self.cur_cell.append(im)
self.cur_cell.append(Spacer(1,0.5*cm))
else:
self.story.append(Spacer(1,0.5*cm))
self.story.append(im)
self.story.append(Spacer(1,0.5*cm))
def write_note(self,text,format,style_name):
text = enc(text)
current_para = self.pdfstyles[style_name]
style_sheet = self.get_style_sheet()
self.my_para = style_sheet.get_paragraph_style(style_name)
self.super = "<font size=%d><super>" \
% (self.my_para.get_font().get_size()-2)
text = text.replace('&','&amp;') # Must be first
text = text.replace('<','&lt;')
text = text.replace('>','&gt;')
text = text.replace('&lt;super&gt;',self.super)
text = text.replace('&lt;/super&gt;','</super></font>')
if format == 1:
text = '<para firstLineIndent="0" fontname="Courier">%s</para>' \
% text.replace('\t',' '*8)
if self.in_table:
self.cur_cell.append(XPreformatted(text,current_para))
else:
self.story.append(XPreformatted(text,current_para))
elif format == 0:
for line in text.split('\n\n'):
if self.in_table:
self.cur_cell.append(Paragraph(line,current_para))
else:
self.story.append(Paragraph(line,current_para))
def write_text(self,text,mark=None):
text = text.replace('&','&amp;') # Must be first
text = text.replace('<','&lt;')
text = text.replace('>','&gt;')
text = text.replace('&lt;super&gt;',self.super)
text = text.replace('&lt;/super&gt;','</super></font>')
self.text = self.text + text.replace('\n','<br>')
def start_page(self):
x = self.get_usable_width()*cm
y = self.get_usable_height()*cm
self.drawing = reportlab.graphics.shapes.Drawing(x,y)
def end_page(self):
self.story.append(self.drawing)
def draw_line(self,style,x1,y1,x2,y2):
y1 = self.get_usable_height() - y1
y2 = self.get_usable_height() - y2
style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style)
if stype.get_line_style() == BaseDoc.SOLID:
line_array = None
else:
line_array = [2,4]
l = reportlab.graphics.shapes.Line(x1*cm,y1*cm,
x2*cm,y2*cm,
strokeWidth=stype.get_line_width(),
strokeDashArray=line_array)
self.drawing.add(l)
def draw_path(self,style,path):
style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style)
color = make_color(stype.get_fill_color())
y = self.get_usable_height()*cm
if stype.get_line_style() == BaseDoc.SOLID:
line_array = None
else:
line_array = [2,4]
scol = make_color(stype.get_color())
p = reportlab.graphics.shapes.Path(strokeWidth=stype.get_line_width(),
strokeDashArray=line_array,
fillColor=color,
strokeColor=scol)
point = path[0]
p.moveTo(point[0]*cm,y-point[1]*cm)
for point in path[1:]:
p.lineTo(point[0]*cm,y-point[1]*cm)
p.closePath()
self.drawing.add(p)
def draw_box(self,style,text,x,y, w, h):
y = self.get_usable_height() - y
style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style)
sspace = box_style.get_shadow_space()
if box_style.get_shadow():
col = make_color((0xc0,0xc0,0xc0))
r = reportlab.graphics.shapes.Rect((x+sspace)*cm,
(y-sspace)*cm-(h*cm),
w*cm,h*cm,
fillColor=col,
strokeColor=col)
self.drawing.add(r)
sw = box_style.get_line_width()
fc = box_style.get_fill_color()
sc = box_style.get_color()
r = reportlab.graphics.shapes.Rect((x)*cm,(y*cm)-(h*cm),w*cm,h*cm,
strokeWidth=sw,
fillColor=fc,
strokeColor=sc)
self.drawing.add(r)
if text != "":
para_name = box_style.get_paragraph_style()
p = style_sheet.get_paragraph_style(para_name)
size = p.get_font().get_size()
x = x + sspace
lines = text.split('\n')
self.left_print(lines,p.get_font(),x*cm,y*cm - size)
def draw_text(self,style,text,x,y):
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()
y = (self.get_usable_height()*cm)-(y*cm)
sc = make_color(font.get_color())
fc = make_color(font.get_color())
fnt = self.pdf_set_font(font)
if p.get_alignment() == BaseDoc.PARA_ALIGN_CENTER:
twidth = ((self.string_width(font,enc(text)))/2.0)*cm
xcm = (stype.get_width() - x) - twidth
else:
xcm = x * cm
s = reportlab.graphics.shapes.String(xcm,
y - size,
enc(text),
strokeColor=sc,
fillColor=fc,
fontName=fnt,
fontSize=size)
self.drawing.add(s)
def pdf_set_font(self, font):
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
if font.get_bold() and font.get_italic():
fn = _HBO
elif font.get_bold():
fn = _HB
elif font.get_italic():
fn = _HO
else:
fn = _H
else:
if font.get_bold() and font.get_italic():
fn = _TBI
elif font.get_bold():
fn = _TB
elif font.get_italic():
fn = _TI
else:
fn = _T
return fn
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()
yt = (self.get_usable_height()*cm) - (y*cm)
yval = 0
g = reportlab.graphics.shapes.Group()
fnt = self.pdf_set_font(font)
sc = make_color(font.get_color())
fc = make_color(font.get_color())
for line in text:
s = reportlab.graphics.shapes.String(0,yval,enc(line),
fontName=fnt,
fontSize=size,
strokeColor=sc,
fillColor=fc,
textAnchor='middle')
yval -= size
g.add(s)
g.translate(x*cm,yt)
g.rotate(-angle)
self.drawing.add(g)
def center_text(self,style,text,x,y):
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()
yt = (self.get_usable_height()*cm) - (y*cm)
size = font.get_size()
fnt = self.pdf_set_font(font)
sc = make_color(font.get_color())
fc = make_color(font.get_color())
s = reportlab.graphics.shapes.String(x*cm,
yt - size,
enc(text),
fontName=fnt,
fontSize=font.get_size(),
strokeColor=sc,
fillColor=fc,
textAnchor='middle')
self.drawing.add(s)
def center_print(self,lines,font,x,y,w,h):
l = len(lines)
size = font.get_size()
start_y = (y + h/2.0 + l/2.0 + l) - ((l*size) + ((l-1)*0.2))/2.0
start_x = (x + w/2.0)
fnt = self.pdf_set_font(font)
size = font.get_size()
sc = make_color(font.get_color())
fc = make_color(font.get_color())
for text in lines:
s = reportlab.graphics.shapes.String(start_x*cm,
start_y*cm,
enc(text),
fontName=fnt,
fontSize=size,
strokeColor=sc,
fillColor=fc)
self.drawing.add(s)
start_y = start_y - size*1.2
def left_print(self,lines,font,x,y):
size = font.get_size()
start_y = y
start_x = x
fnt = self.pdf_set_font(font)
sc = make_color(font.get_color())
fc = make_color(font.get_color())
for text in lines:
s = reportlab.graphics.shapes.String(start_x,
start_y,
enc(text),
fontSize=size,
strokeColor=sc,
fillColor=fc,
fontName=fnt)
self.drawing.add(s)
start_y = start_y - size*1.2
#------------------------------------------------------------------------
#
# Convert an RGB color tulple to a Color instance
#
#------------------------------------------------------------------------
def make_color(c):
return Color(float(c[0])/255.0, float(c[1])/255.0, float(c[2])/255.0)
#------------------------------------------------------------------------
#
# Register the document generator with the GRAMPS plugin system
#
#------------------------------------------------------------------------
print_label = None
try:
import Utils
mprog = Mime.get_application("application/pdf")
mtype = Mime.get_description("application/pdf")
if Utils.search_for(mprog[0]):
print_label=_("Open in %s") % mprog[1]
else:
print_label=None
register_text_doc(mtype, PdfDoc, 1, 1, 1, ".pdf", print_label)
register_draw_doc(mtype, PdfDoc, 1, 1, ".pdf", print_label)
register_book_doc(mtype,classref=PdfDoc,
table=1,paper=1,style=1,ext=".pdf")
except:
register_text_doc(_('PDF document'), PdfDoc,1, 1, 1,".pdf", None)
register_draw_doc(_('PDF document'), PdfDoc,1, 1, ".pdf", None)
register_book_doc(name=_("PDF document"),classref=PdfDoc,
table=1,paper=1,style=1,ext=".pdf")