#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2004 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.
"""
#-------------------------------------------------------------------------
#
# Imported Modules
#
#-------------------------------------------------------------------------
import base64
import string
import os
import BaseDoc
import Errors
import Plugins
import ImgManip
import grampslib
from latin_utf8 import latin_to_utf8
from gettext import gettext as _
#-------------------------------------------------------------------------
#
# 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.icount = 0;
self.imap = {}
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')
# 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 len(self.media_list) > 0:
self.f.write('\n')
for file_tuple in self.media_list:
tag = self.imap[file_tuple[0]]
img = ImgManip.ImgManip(file_tuple[0])
buf = img.png_data()
self.f.write('\n' % tag)
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'
app = grampslib.default_application_command(apptype)
os.environ["FILE"] = self.filename
os.system ('%s "$FILE" &' % app)
def add_media_object(self,name,pos,x_cm,y_cm):
try:
image = ImgManip.ImgManip(name)
(x,y) = image.size()
except:
return
aspect_ratio = float(x)/float(y)
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
self.media_list.append((name,act_width,act_height))
tag = "image%d" % self.icount
self.f.write('' % y_cm)
self.imap[name] = tag
self.icount += 1
def start_superscript(self):
self.text = self.text + ''
def end_superscript(self):
self.text = self.text + ''
def start_paragraph(self,style_name,leader=None):
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.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 = string.join(string.split(line))
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
#
#--------------------------------------------------------------------------
print_label = None
try:
import Utils
prog = grampslib.default_application_command("application/x-abiword")
if Utils.search_for(prog):
print_label=_("Open in AbiWord")
except:
pass
Plugins.register_text_doc(_("AbiWord"),AbiWordDoc,1,1,1,".abw", print_label)