# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2006 Donald N. Allingham # Copyright (C) 2005-2009 Serge Noiraud # Copyright (C) 2007-2009 Brian G. Matherly # Copyright (C) 2010 Peter Landgren # Copyright (C) 2010 Jakim Friant # Copyright (C) 2011 Adam Stein # Copyright (C) 2012 Paul Franklin # # 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 # """ ODFDoc : used to generate Open Office Document """ #------------------------------------------------------------------------- # # pylint : disable messages ... # #------------------------------------------------------------------------- # disable-msg=C0302 # Too many lines in module # pylint: disable-msg=C0302 # disable-msg # Regular expression which should only match # pylint: disable-msg=C0103 # disable-msg=R0902 # Too many instance attributes # pylint: disable-msg=R0902 # disable-msg=R0904 # Too many public methods # pylint: disable-msg=R0904 # disable-msg=R0912 # Too many branches # pylint: disable-msg=R0912 # disable-msg=R0913 # Too many arguments # pylint: disable-msg=R0913 # disable-msg=R0914 # Too many local variables # pylint: disable-msg=R0914 # disable-msg=R0915 # Too many statements # pylint: disable-msg=R0915 # warnings : # disable-msg=W0613 # Unused argument # pylint: disable-msg=W0613 # errors : # disable-msg=E1101 # has no member # pylint: disable-msg=E1101 #------------------------------------------------------------------------- # # Standard Python Modules # #------------------------------------------------------------------------- import os try: from hashlib import md5 except ImportError: from md5 import md5 import zipfile import time import sys if sys.version_info[0] < 3: from cStringIO import StringIO else: from io import StringIO from math import pi, cos, sin, degrees, radians from xml.sax.saxutils import escape #------------------------------------------------------------------------- # # Gramps modules # #------------------------------------------------------------------------- from gramps.gen.plug.docgen import (BaseDoc, TextDoc, DrawDoc, graphicstyle, FONT_SANS_SERIF, SOLID, PAPER_PORTRAIT, INDEX_TYPE_TOC, PARA_ALIGN_CENTER, PARA_ALIGN_LEFT, INDEX_TYPE_ALP, PARA_ALIGN_RIGHT, URL_PATTERN, LOCAL_HYPERLINK, LOCAL_TARGET) from gramps.gen.plug.docgen.fontscale import string_width from gramps.plugins.lib.libodfbackend import OdfBackend from gramps.gen.const import PROGRAM_NAME, URL_HOMEPAGE from gramps.version import VERSION from gramps.gen.plug.report import utils as ReportUtils from gramps.gen.utils.image import (image_size, image_dpi, image_actual_size, crop_percentage_to_subpixel) from gramps.gen.errors import ReportError #------------------------------------------------------------------------- # # internationalization # #------------------------------------------------------------------------- from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext _apptype = 'application/vnd.oasis.opendocument.text' _esc_map = { '\x1a' : '', '\x0c' : '', '\n' : '', '\t' : '', } #------------------------------------------------------------------------- # # regexp for Styled Notes ... # #------------------------------------------------------------------------- import re # Hyphen is added because it is used to replace spaces in the font name NewStyle = re.compile('style-name="([a-zA-Z0-9]*)__([#a-zA-Z0-9 -]*)__">') #------------------------------------------------------------------------- # # Constants # #------------------------------------------------------------------------- _XMLNS = '''\ xmlns:office="%(urn)soffice:1.0" xmlns:style="%(urn)sstyle:1.0" xmlns:text="%(urn)stext:1.0" xmlns:table="%(urn)stable:1.0" xmlns:draw="%(urn)sdrawing:1.0" xmlns:fo="%(urn)sxsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="%(urn)smeta:1.0" xmlns:number="%(urn)sdatastyle:1.0" xmlns:svg="%(urn)ssvg-compatible:1.0" xmlns:chart="%(urn)schart:1.0" xmlns:dr3d="%(urn)sdr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="%(urn)sform:1.0" xmlns:script="%(urn)sscript:1.0" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" ''' % {"urn": "urn:oasis:names:tc:opendocument:xmlns:"} _FONTS = '''\ ''' _META_XML = '''\ %(generator)s %(creator)s %(date)s %(creator)s %(date)s 0-00-00T00:00:00 %(lang)s 1 PT0S %(gramps_home_url)s ''' _STYLES = '''\ ''' _AUTOMATIC_STYLES = '''\ ''' _CLEAR_STYLE = '''\ \n \n ''' _OTHER_STYLES = '''\ \n \n \n \n \n \n \n \n \n \n \n \n ''' _SHEADER_FOOTER = '''\ \n \n ''' _CLICKABLE = r'''\1''' #------------------------------------------------------------------------- # # ODFDoc # #------------------------------------------------------------------------- class ODFDoc(BaseDoc, TextDoc, DrawDoc): """ The ODF document class """ def __init__(self, styles, ftype): """ Class init """ BaseDoc.__init__(self, styles, ftype) self.media_list = [] self.init_called = False self.cntnt = None self.cntnt1 = None self.cntnt2 = None self.cntntx = None self.sfile = None self.mimetype = None self.meta = None self.mfile = None self.stfile = None self.filename = None self.lang = None self._backend = None self.span = 0 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 self.StyleList_notes = [] # styles to create depending on styled notes. self.StyleList_photos = [] # styles to create depending on clipped images. def open(self, filename): """ Open the new document """ t = time.localtime(time.time()) self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % t[:6] self.filename = filename if not filename.endswith("odt"): self.filename += ".odt" self.filename = os.path.normpath(os.path.abspath(self.filename)) self._backend = OdfBackend() self.cntnt = StringIO() self.cntnt1 = StringIO() self.cntnt2 = StringIO() def init(self): """ Create the document header """ assert (not self.init_called) self.init_called = True wrt = self.cntnt.write wrt1, wrt2 = self.cntnt1.write, self.cntnt2.write self.lang = glocale.lang self.lang = self.lang.replace('_', '-') if self.lang else "en-US" self.StyleList_notes = [] # styles to create depending on styled notes. wrt1('\n' '\n' + '\n' ) wrt1('\n' + _FONTS ) wrt2( '\n' + '\n' + _AUTOMATIC_STYLES ) styles = self.get_style_sheet() for style_name in styles.get_draw_style_names(): style = styles.get_draw_style(style_name) wrt( '\n' '\n' '\n' ) wrt( '\n' '\n' '\n' ) # Graphic style for items with a clear background wrt( _CLEAR_STYLE ) for style_name in styles.get_paragraph_style_names(): style = styles.get_paragraph_style(style_name) wrt( '\n' % style_name + '\n' + '\n' + '\n' + ' 0: wrt('fo:keep-with-next="auto" ') align = style.get_alignment() if align == PARA_ALIGN_LEFT: wrt('fo:text-align="start" ') elif align == PARA_ALIGN_RIGHT: wrt('fo:text-align="end" ') elif align == PARA_ALIGN_CENTER: wrt( 'fo:text-align="center" ' 'style:justify-single-word="false" ' ) else: wrt( 'fo:text-align="justify" ' 'style:justify-single-word="false" ' ) font = style.get_font() wrt('style:font-name="%s" ' % ("Arial" if font.get_type_face() == FONT_SANS_SERIF else "Times New Roman") ) wrt( 'fo:font-size="%.2fpt" ' % font.get_size() + 'style:font-size-asian="%.2fpt" ' % font.get_size() + 'fo:color="#%02x%02x%02x" ' % font.get_color() ) if font.get_bold(): wrt('fo:font-weight="bold" ') if font.get_italic(): wrt('fo:font-style="italic" ') if font.get_underline(): wrt( 'style:text-underline="single" ' 'style:text-underline-color="font-color" ' ) wrt( 'fo:text-indent="%.2fcm"\n' % style.get_first_indent() + 'fo:margin-right="%.2fcm"\n' % style.get_right_margin() + 'fo:margin-left="%.2fcm"\n' % style.get_left_margin() + 'fo:margin-top="%.2fcm"\n' % style.get_top_margin() + 'fo:margin-bottom="%.2fcm"\n' % style.get_bottom_margin() + '/>\n' + '\n' ) wrt( '\n' + ' ' % font.get_size() + '\n' ) for style_name in styles.get_table_style_names(): style = styles.get_table_style(style_name) table_width = float(self.get_usable_width()) table_width_str = "%.2f" % table_width wrt( '\n' '\n' + '\n' ) for col in range(0, min(style.get_columns(), 50)): width = table_width * float(style.get_column_width(col) / 100.0) width_str = "%.4f" % width wrt( '' + '' % width_str + '\n' ) for cell in styles.get_cell_style_names(): cell_style = styles.get_cell_style(cell) wrt( '\n' + '\n' '\n' ) wrt( _OTHER_STYLES ) wrt( '\n' '\n' ' \n' ' \n' ) def uniq(self, list_, funct=None): """ We want no duplicate in the list """ # order preserving funct = funct or (lambda x:x) seen = set() result = [] for item in list_: marker = funct(item[0]) if marker in seen: continue seen.add(marker) result.append(item) return result def finish_cntnt_creation(self): """ We have finished the document. So me must integrate the new fonts and styles where they should be. The content.xml file is closed. """ self.cntntx = StringIO() self.StyleList_notes = self.uniq(self.StyleList_notes) self.add_styled_notes_fonts() self.add_styled_notes_styles() self.add_styled_photo_styles() self.cntntx.write(self.cntnt1.getvalue()) self.cntntx.write(self.cntnt2.getvalue()) self.cntntx.write(self.cntnt.getvalue()) self.cntnt1.close() self.cntnt2.close() self.cntnt.close() def close(self): """ Close the document and create the odt file """ self.cntnt.write( '\n' '\n' '\n' ) self.finish_cntnt_creation() self._write_styles_file() self._write_manifest() self._write_settings() self._write_meta_file() self._write_mimetype_file() self._write_zip() def add_styled_notes_fonts(self): """ Add the new fonts for Styled notes in the font-face-decls section. """ # Need to add new font for styled notes here. wrt1 = self.cntnt1.write for style in self.StyleList_notes: if style[1] == "FontFace": # Restore any spaces that were replaced by hyphens in # libodfbackend wrt1( '\n\n' ) def add_styled_notes_styles(self): """ Add the new styles for Styled notes in the automatic-styles section. """ # Need to add new style for styled notes here. wrt2 = self.cntnt2.write for style in self.StyleList_notes: if style[1] == "FontSize": wrt2( '\n' + ' \n' % style[2] + '\n\n' ) elif style[1] == "FontColor": # Restore the hash at the start that was removed by # libodfbackend wrt2( '\n' + ' \n' % style[2] + '\n\n' ) elif style[1] == "FontHighlight": wrt2( '\n' + ' \n' % style[2] + '\n\n' ) elif style[1] == "FontFace": # Restore any spaces that were replaced by hyphens in # libodfbackend wrt2( '\n' + ' \n' + '\n\n' ) def add_styled_photo_styles(self): """ Add the new styles for clipped images in the automatic-styles section. """ wrt2 = self.cntnt2.write for style in self.StyleList_photos: if style[0] == "Left": wrt2( '' + '' + '\n' ) elif style[0] == "Right": wrt2( '' + '' + '\n' ) elif style[0] == "Single": wrt2( '' + '\n' ) else: wrt2( '' + '' + '\n' ) def add_media_object(self, file_name, pos, x_cm, y_cm, alt='', style_name=None, crop=None): """ Add multi-media documents : photos """ # 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) = image_size(file_name) if (x, y) == (0, 0): return not_extension, extension = os.path.splitext(file_name) file_name_hash = file_name if sys.version_info[0] >= 3: file_name_hash = file_name_hash.encode('utf-8') odf_name = md5(file_name_hash).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('') pos = pos.title() if pos in ['left', 'right', 'single'] else 'Row' if crop: (start_x, start_y, end_x, end_y ) = crop_percentage_to_subpixel(x, y, crop) # Need to keep the ratio intact, otherwise scaled images look stretched # if the dimensions aren't close in size (act_width, act_height) = image_actual_size( x_cm, y_cm, int(end_x-start_x), int(end_y-start_y) ) dpi = image_dpi(file_name) # ODF wants crop measurements in inch and as margins from each side left = start_x/dpi[0] right = (x - end_x)/dpi[0] top = start_y/dpi[1] bottom = (y - end_y)/dpi[1] crop = (top, right, bottom, left) self.StyleList_photos.append( [pos, crop] ) pos += "_" + str(crop) else: (act_width, act_height) = image_actual_size(x_cm, y_cm, x, y) if len(alt): self.cntnt.write( ' ' + ' ' % act_height + '' % style_name ) self.cntnt.write( '' + '\n' + '\n' ) if len(alt): self.cntnt.write( '%s' % ''.join(alt) + '' + '' + '' ) if self.new_cell: self.cntnt.write('\n') def start_table(self, name, style_name): """ open a table """ self.cntnt.write( '\n' % style_name ) styles = self.get_style_sheet() table = styles.get_table_style(style_name) for col in range(table.get_columns()): self.cntnt.write( '\n' % (style_name, str(chr(ord('A')+col))) ) def end_table(self): """ close a table """ self.cntnt.write('\n') def start_row(self): """ open a row """ self.cntnt.write('\n') def end_row(self): """ close a row """ self.cntnt.write('\n') def start_cell(self, style_name, span=1): """ open a cell """ 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): """ close a cell """ self.cntnt.write('\n') #for col in range(1, self.span): # self.cntnt.write('\n') self.new_cell = 0 def start_bold(self): """ open bold """ self.cntnt.write('') def end_bold(self): """ close bold """ self.cntnt.write('') def start_superscript(self): """ open superscript """ self.cntnt.write('') def end_superscript(self): """ close superscript """ self.cntnt.write('') def _add_zip(self, zfile, name, data, t): """ Add a zip file to an archive """ if sys.version_info[0] < 3: name = name.encode('utf-8') zipinfo = zipfile.ZipInfo(name) zipinfo.date_time = t zipinfo.compress_type = zipfile.ZIP_DEFLATED zipinfo.external_attr = 0o644 << 16 zfile.writestr(zipinfo, data) def _write_zip(self): """ Create the odt file. This is a zip file """ try: zfile = zipfile.ZipFile(self.filename, "w", zipfile.ZIP_DEFLATED) except IOError as msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg) raise ReportError(errmsg) except: raise 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.cntntx.getvalue(), t) self._add_zip(zfile, "meta.xml", self.meta.getvalue(), t) self._add_zip(zfile, "settings.xml", self.stfile.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.stfile.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: errmsg = "%s\n%s" % (_("Could not open %s") % image[0], msg) raise ReportError(errmsg) zfile.close() def _write_styles_file(self): """ create the styles.xml file """ self.sfile = StringIO() wrtf = self.sfile.write wrtf('\n') wrtf('\n' ) wrtf('\n' + _FONTS + '\n' ) wrtf('\n' + _STYLES ) styles = self.get_style_sheet() for style_name in styles.get_paragraph_style_names(): style = styles.get_paragraph_style(style_name) wrtf( '\n' + ' 0: wrtf('fo:keep-with-next="auto" ') align = style.get_alignment() if align == PARA_ALIGN_LEFT: wrtf( 'fo:text-align="start" ' 'style:justify-single-word="false" ' ) elif align == PARA_ALIGN_RIGHT: wrtf('fo:text-align="end" ') elif align == PARA_ALIGN_CENTER: wrtf( 'fo:text-align="center" ' 'style:justify-single-word="false" ' ) else: wrtf( 'fo:text-align="justify" ' 'style:justify-single-word="false" ' ) wrtf( 'fo:text-indent="%.2fcm" ' % style.get_first_indent() + 'style:auto-text-indent="false"/> ' + '\n' '\n' ) # Dash lengths are based on the OpenOffice Ultrafine Dashed line style. for line_style in graphicstyle.line_style_names: dash_array = graphicstyle.get_line_style_by_name(line_style) wrtf('\n' % (line_style, dash_array[0], dash_array[0], dash_array[1] * 0.051)) # Current no leading number format for headers #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') #wrtf('\n') wrtf( ' ' ) wrtf( ' ' ) wrtf( ' ' ) wrtf('\n') wrtf( '\n' + _SHEADER_FOOTER + '\n' + '\n' + '\n' + '\n' ) # header wrtf( '\n' '\n' '\n' ) # footer wrtf( '\n' '\n' '\n' ) # End of page layout wrtf( '\n' '\n' ) # Master Styles wrtf( '\n' '\n' # header #'' #'' # How to get the document title here ? #' TITRE : %s' % self.title #'' #'' # footer #'' #'' #'1' #'/' #'1' #'' #'' #'' # '' '\n' ) # End of document styles wrtf('\n') def page_break(self): """ prepare a new page """ self.new_page = 1 def start_page(self): """ create a new page """ self.cntnt.write('\n') def end_page(self): """ close the page """ self.cntnt.write('\n') def start_paragraph(self, style_name, leader=None): """ open a new paragraph """ 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( '' % str(self.level) ) if leader is not None: self.cntnt.write(leader + '') self.new_cell = 0 def end_paragraph(self): """ close a paragraph """ self.cntnt.write( '\n' % ('p' if self.level == 0 else 'h') ) self.new_cell = 1 def write_styled_note(self, styledtext, format, style_name, contains_html=False, links=False): """ Convenience function to write a styledtext to the ODF doc. styledtext : assumed a StyledText object to write format : = 0 : Flowed, = 1 : Preformatted style_name : name of the style to use for default presentation contains_html: bool, the backend should not check if html is present. If contains_html=True, then the textdoc is free to handle that in some way. Eg, a textdoc could remove all tags, or could make sure a link is clickable. ODFDoc prints the html without handling it links: bool, make URLs clickable if True """ text = str(styledtext) s_tags = styledtext.get_tags() markuptext = self._backend.add_markup_from_styled(text, s_tags, '\n') if links == True: markuptext = re.sub(URL_PATTERN, _CLICKABLE, markuptext) # we need to know if we have new styles to add. # if markuptext contains : FontColor, FontFace, FontSize ... # we must prepare the new styles for the styles.xml file. # We are looking for the following format : # style-name="([a-zA-Z0-9]*)__([a-zA-Z0-9 ])"> # The first element is the StyleType and the second one is the value start = 0 while 1: m = NewStyle.search(markuptext, start) if not m: break self.StyleList_notes.append([m.group(1)+m.group(2), m.group(1), m.group(2)]) start = m.end() linenb = 1 self.start_paragraph(style_name) for line in markuptext.split('\n'): [line, sigcount] = process_spaces(line, format) if sigcount == 0: self.end_paragraph() self.start_paragraph(style_name) linenb = 1 else: if ( linenb > 1 ): self.cntnt.write('') self.cntnt.write(line) linenb += 1 self.end_paragraph() def write_text(self, text, mark=None, links=False): """ Uses the xml.sax.saxutils.escape function to convert XML entities. The _esc_map dictionary allows us to add our own mappings. @param mark: IndexMark to use for indexing """ text = escape(text, _esc_map) if links == True: text = re.sub(URL_PATTERN, _CLICKABLE, text) self._write_mark(mark, text) self.cntnt.write(text) def _write_mark(self, mark, text): """ Insert a mark at this point in the document. """ 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 ) elif mark.type == LOCAL_HYPERLINK: self.cntnt.write( '' % key) self.cntnt.write(text) self.cntnt.write('') return elif mark.type == LOCAL_TARGET: self.cntnt.write( '' % key) def insert_toc(self): """ Insert a Table of Contents at this point in the document. """ title = self.toc_title self.cntnt.write('') self.cntnt.write('') self.cntnt.write('' + title) self.cntnt.write('') for level in range(1, 4): self.cntnt.write('' % level) self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('%s' % title) self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') def insert_index(self): """ Insert an Alphabetical Index at this point in the document. """ title = self.index_title self.cntnt.write('') self.cntnt.write('') self.cntnt.write('' + title) self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') self.cntnt.write('%s' % title) self.cntnt.write('') self.cntnt.write('') self.cntnt.write('') def _write_manifest(self): """ create the manifest.xml file """ self.mfile = StringIO() # Header self.mfile.write( '\n' + '' + '' ) # Images for image in self.media_list: self.mfile.write( '' ) # Footer self.mfile.write( '' '' '' '' '' '\n' ) def _write_settings(self): """ create the settings.xml file """ self.stfile = StringIO() # This minimal settings file has been taken from # http://mashupguide.net/1.0/html/ch17s03.xhtml (Creative commons # licence): http://mashupguide.net/1.0/html/apas02.xhtml self.stfile.write( '\n' + '' ) def _write_mimetype_file(self): """ create the mimetype.xml file """ self.mimetype = StringIO() self.mimetype.write('application/vnd.oasis.opendocument.text') def _write_meta_file(self): """ create the meta.xml file """ self.meta = StringIO() generator = PROGRAM_NAME + ' ' + VERSION creator = self.get_creator() date = self.time lang = self.lang gramps_home_url = URL_HOMEPAGE self.meta.write( _META_XML % locals() ) def rotate_text(self, style, text, x, y, angle, mark=None): """ Used to rotate a text with an angle. @param mark: IndexMark to use for indexing """ 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 = radians(angle) xloc = x - (wcm / 2.0) * cos(rangle) + (hcm / 2.0) * sin(rangle) yloc = y - (hcm / 2.0) * cos(rangle) - (wcm / 2.0) * sin(rangle) self._write_mark(mark, text) self.cntnt.write( '\n' % (xloc, yloc) + '\n' + '' % pname + '' % pname + escape('\n'.join(text), _esc_map) + '\n\n' + '\n') def draw_path(self, style, path): """ Draw a 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): """ Draw a line """ self.cntnt.write( '' % y2 + '\n' + '\n' ) def draw_text(self, style, text, x, y, mark=None): """ Draw a text @param mark: IndexMark to use for indexing """ 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._write_mark(mark, text) self.cntnt.write( '' % float(y) + ' ' + '' % para_name + '' % para_name + #' fo:max-height="%.2f">' % font.get_size() + escape(text, _esc_map) + '' + '' + '\n' + '\n' ) def draw_box(self, style, text, x, y, w, h, mark=None): """ Draw a box @param mark: IndexMark to use for indexing """ 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() self._write_mark(mark, text) if box_style.get_shadow(): self.cntnt.write( '\n' % (float(y) + shadow_width) + '\n' ) self.cntnt.write( '\n' % float(y) ) if text: self.cntnt.write( '' % para_name + '' % para_name + escape(text, _esc_map) + '' '\n' ) self.cntnt.write('\n') def center_text(self, style, text, x, y, mark=None): """ Center a text in a cell, a row, a line, ... @param mark: IndexMark to use for indexing """ 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._write_mark(mark, text) self.cntnt.write( '\n' % float(y) ) if text: self.cntnt.write( '' + '' % para_name + '' % para_name + escape(text, _esc_map) + '\n' + '\n' + '' ) self.cntnt.write('\n') def process_spaces(line, format): """ Function to process spaces in text lines for flowed and pre-formatted notes. line : text to process format : = 0 : Flowed, = 1 : Preformatted If the text is flowed (format==0), then leading spaces (after ignoring XML) are removed. Embedded multiple spaces are reduced to one by ODF If the text is pre-formatted (format==1). then all spaces (after ignoring XML) are replaced by "" Returns the processed text, and the number of significant (i.e. non-white-space) chars. """ txt = "" xml = False sigcount = 0 # we loop through every character, which is very inefficient, but an attempt to use # a regex replace didn't always work. This was the code that was replaced. # Problem, we may not replace ' ' in xml tags, so we use a regex # self.cntnt.write(re.sub(' (?=([^(<|>)]*<[^>]*>)*[^>]*$)', # "", line)) for char in line: if char == '<' and xml == False: xml = True txt += char elif char == '>' and xml == True: xml = False txt += char elif xml == True: txt += char elif char == " " or char == "\t": if format == 0 and sigcount == 0: pass elif format == 1: #preformatted, section White-space characters of # http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1-html/OpenDocument-v1.1.html#5.1.1.White-space%20Characters|outline txt += "" else: txt += char else: sigcount += 1 txt += char return [txt, sigcount]