# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2006 Donald N. Allingham # # 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 import os from gettext import gettext as _ #------------------------------------------------------------------------- # # Gramps Modules # #------------------------------------------------------------------------- import BaseDoc import Errors from PluginUtils import register_text_doc import ImgManip import Mime import Utils #------------------------------------------------------------------------- # # Class Definitions # #------------------------------------------------------------------------- class AbiWordDoc(BaseDoc.BaseDoc): """AbiWord document generator. Inherits from the BaseDoc generic document interface class.""" def __init__(self,styles,type,template,orientation): """Initializes the AbiWordDoc class, calling the __init__ routine of the parent BaseDoc class""" BaseDoc.BaseDoc.__init__(self,styles,type,template,orientation) 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('\n') self.f.write('\n') self.f.write('\n' % Utils.xml_lang()) # metadata section self.f.write('\n') self.f.write('application/x-abiword\n') self.f.write('AbiWord\n') self.f.write('Mon May 19 14:16:24 2003\n') self.f.write('\n') self.write_styles() # page size section self.f.write('\n') self.f.write('
\n') def write_styles(self): self.f.write('\n') for style_name in self.style_list.keys(): style = self.style_list[style_name] self.current_style = style self.f.write('\n') self.f.write('\n') def close(self): """Write the trailing information and closes the file""" self.f.write('
\n') if self.media_list: self.f.write('\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( '\n' % tag_number) self.f.write(base64.encodestring(buf)) self.f.write('\n') self.f.write('\n') self.f.write('
\n') self.f.close() if self.print_req: apptype = 'application/x-abiword' try: app = Mime.get_application(apptype)[0] os.environ["FILE"] = self.filename os.system ('%s "$FILE" &' % app) 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 = '

' end_p = '

' self.f.write('%s%s ' % (start_p,tag_number,act_height,act_width,end_p)) def start_superscript(self): self.f.write('') def end_superscript(self): self.f.write('') def start_paragraph(self,style_name,leader=None): self.in_paragraph = 1 style = self.style_list[style_name] self.current_style = style self.f.write('

' % style_name) if self.new_page == 1: self.new_page = 0 self.f.write('') 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('

\n') def write_note(self,text,format,style_name): if format == 1: self.start_paragraph(style_name) self.f.write('') self.write_text(text) self.f.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): text = text.replace('&','&'); # Must be first text = text.replace('<','<'); text = text.replace('>','>'); text = text.replace('<super>','') text = text.replace('</super>','') self.f.write(text) def start_bold(self): self.f.write('') def end_bold(self): self.f.write('') def start_table(self,name,style_name): self.in_table = 1 self.tblstyle = self.table_styles[style_name] self.f.write('\n') self.current_row = -1 def end_table(self): self.in_table = 0 self.f.write('
\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): self.cstyle = self.cell_styles[style_name] self.f.write('\n') self.col += span def end_cell(self): self.f.write('\n') #-------------------------------------------------------------------------- # # Register plugins # #-------------------------------------------------------------------------- try: prog = Mime.get_application("application/x-abiword") mtype = Mime.get_description('application/x-abiword') 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)