2001-05-13 07:26:57 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 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
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2001-05-18 08:44:35 +05:30
|
|
|
import base64
|
2001-05-13 07:26:57 +05:30
|
|
|
|
|
|
|
from TextDoc import *
|
|
|
|
from latin_utf8 import latin_to_utf8
|
|
|
|
import const
|
2001-05-18 08:44:35 +05:30
|
|
|
import string
|
2001-05-13 07:26:57 +05:30
|
|
|
|
|
|
|
class AbiWordDoc(TextDoc):
|
|
|
|
|
|
|
|
def __init__(self,type,orientation):
|
|
|
|
TextDoc.__init__(self,type,orientation)
|
|
|
|
self.f = None
|
|
|
|
self.level = 0
|
|
|
|
|
|
|
|
def open(self,filename):
|
|
|
|
|
|
|
|
if filename[-4:] != ".abw":
|
|
|
|
self.filename = filename + ".abw"
|
|
|
|
else:
|
|
|
|
self.filename = filename
|
2001-05-19 09:33:15 +05:30
|
|
|
|
2001-05-19 08:28:18 +05:30
|
|
|
self.f = open(self.filename,"w")
|
2001-05-13 07:26:57 +05:30
|
|
|
self.f.write('<?xml version="1.0" encoding="ISO-8859-1"?>\n')
|
2001-05-19 08:28:18 +05:30
|
|
|
self.f.write('<abiword version="0.7.14" fileformat="1.0">\n')
|
2001-05-13 07:26:57 +05:30
|
|
|
self.f.write('<pagesize ')
|
2001-05-18 08:44:35 +05:30
|
|
|
self.f.write('pagetype="%s" ' % self.paper.get_name())
|
2001-05-13 07:26:57 +05:30
|
|
|
if self.orientation == PAPER_PORTRAIT:
|
|
|
|
self.f.write('orientation="portrait" ')
|
|
|
|
else:
|
|
|
|
self.f.write('orientation="landscape" ')
|
|
|
|
self.f.write('width="%.4f" ' % (self.width/2.54))
|
|
|
|
self.f.write('height="%.4f" ' % (self.height/2.54))
|
|
|
|
self.f.write('units="inch" page-scale="1.000000"/>\n')
|
|
|
|
self.f.write('<section ')
|
|
|
|
rmargin = float(self.rmargin)/2.54
|
|
|
|
lmargin = float(self.lmargin)/2.54
|
|
|
|
self.f.write('props="page-margin-right:%.4fin; ' % rmargin)
|
|
|
|
self.f.write('page-margin-left:%.4fin"' % lmargin)
|
|
|
|
self.f.write('>\n')
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.f.write('</section>\n')
|
2001-05-18 08:44:35 +05:30
|
|
|
if len(self.photo_list) > 0:
|
|
|
|
self.f.write('<data>\n')
|
|
|
|
for file_tuple in self.photo_list:
|
|
|
|
file = file_tuple[0]
|
|
|
|
width = file_tuple[1]
|
|
|
|
height = file_tuple[2]
|
2001-05-19 08:28:18 +05:30
|
|
|
base = "/tmp/%s.png" % os.path.basename(file)
|
2001-05-18 08:44:35 +05:30
|
|
|
tag = string.replace(base,'.','_')
|
|
|
|
cmd = "%s -size %dx%d %s %s" % (const.convert,width,height,file,base)
|
|
|
|
os.system(cmd)
|
|
|
|
self.f.write('<d name="')
|
|
|
|
self.f.write(tag)
|
2001-05-19 08:28:18 +05:30
|
|
|
self.f.write('" mime-type="image/png" base64="yes">\n')
|
2001-05-18 08:44:35 +05:30
|
|
|
f = open(base,"rb")
|
|
|
|
base64.encode(f,self.f)
|
|
|
|
f.close()
|
|
|
|
os.unlink(base)
|
|
|
|
self.f.write('</d>\n')
|
|
|
|
self.f.write('</data>\n')
|
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
self.f.write('</abiword>\n')
|
|
|
|
self.f.close()
|
|
|
|
|
2001-05-18 08:44:35 +05:30
|
|
|
def add_photo(self,name,x,y):
|
|
|
|
import GdkImlib
|
|
|
|
|
|
|
|
image = GdkImlib.Image(name)
|
|
|
|
scale = float(y)/float(image.rgb_height)
|
|
|
|
act_width = int(image.rgb_width * scale)
|
|
|
|
act_height = int(image.rgb_height * scale)
|
|
|
|
|
|
|
|
self.photo_list.append((name,act_width,act_height))
|
|
|
|
|
2001-05-19 08:28:18 +05:30
|
|
|
base = "/tmp/%s.png" % os.path.basename(name)
|
2001-05-18 08:44:35 +05:30
|
|
|
tag = string.replace(base,'.','_')
|
|
|
|
|
2001-05-19 08:28:18 +05:30
|
|
|
self.f.write('<image dataid="')
|
2001-05-18 08:44:35 +05:30
|
|
|
self.f.write(tag)
|
|
|
|
self.f.write('" props="width:%.3fin; ' % ((float(act_width)/72.0)/2.54))
|
2001-05-19 08:28:18 +05:30
|
|
|
self.f.write('height:%.3fin"/>' % ((float(act_height)/72.0)/2.54))
|
2001-05-18 08:44:35 +05:30
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
def start_paragraph(self,style_name):
|
|
|
|
style = self.style_list[style_name]
|
|
|
|
self.f.write('<p props="')
|
|
|
|
if style.get_alignment() == PARA_ALIGN_RIGHT:
|
|
|
|
self.f.write('text-align:right;')
|
|
|
|
elif style.get_alignment() == PARA_ALIGN_LEFT:
|
|
|
|
self.f.write('text-align:left;')
|
|
|
|
elif style.get_alignment() == 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(' text-indent:%.4fin' % indent)
|
|
|
|
self.f.write('">')
|
|
|
|
font = style.get_font()
|
|
|
|
self.f.write('<c props="font-family:')
|
|
|
|
if font.get_type_face() == FONT_SANS_SERIF:
|
|
|
|
self.f.write('Arial;')
|
|
|
|
else:
|
|
|
|
self.f.write('Times New Roman;')
|
|
|
|
self.f.write('font-size:' + str(font.get_size()) + 'pt')
|
|
|
|
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' % color)
|
|
|
|
self.f.write('">')
|
|
|
|
|
|
|
|
def end_paragraph(self):
|
|
|
|
self.f.write('</c></p>\n')
|
|
|
|
|
|
|
|
def write_text(self,text):
|
|
|
|
self.f.write(text)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2001-05-18 08:44:35 +05:30
|
|
|
paper = PaperStyle("Letter",27.94,21.59)
|
|
|
|
|
|
|
|
doc = AbiWordDoc(paper,PAPER_PORTRAIT)
|
2001-05-13 07:26:57 +05:30
|
|
|
foo = FontStyle()
|
|
|
|
foo.set_type_face(FONT_SANS_SERIF)
|
|
|
|
foo.set_color((255,0,0))
|
|
|
|
foo.set_size(24)
|
|
|
|
|
|
|
|
para = ParagraphStyle()
|
|
|
|
doc.add_style("Normal",para)
|
2001-05-19 08:28:18 +05:30
|
|
|
|
|
|
|
doc.open("test")
|
2001-05-18 08:44:35 +05:30
|
|
|
|
|
|
|
doc.start_paragraph("Normal")
|
2001-05-19 08:28:18 +05:30
|
|
|
doc.add_photo("image.jpg",200,200)
|
2001-05-18 08:44:35 +05:30
|
|
|
doc.end_paragraph()
|
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
doc.close()
|
|
|
|
|
2001-05-18 08:44:35 +05:30
|
|
|
|