#
# 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
import tempfile
import string
from TextDoc import *
from DrawDoc import *
from latin_utf8 import latin_to_utf8
import const
try:
from codecs import *
except:
def EncodedFile(a,b,c):
return a
class OpenDrawDoc(DrawDoc):
def __init__(self,type,orientation):
DrawDoc.__init__(self,type,orientation)
self.f = None
self.filename = None
self.level = 0
self.time = "0000-00-00T00:00:00"
self.page = 0
def open(self,filename):
import time
t = time.localtime(time.time())
self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % \
(t[0],t[1],t[2],t[3],t[4],t[5])
if filename[-4:] != ".sxd":
self.filename = filename + ".sxd"
else:
self.filename = filename
tempfile.tempdir = "/tmp"
self.tempdir = tempfile.mktemp()
os.mkdir(self.tempdir,0700)
os.mkdir(self.tempdir + os.sep + "Pictures")
os.mkdir(self.tempdir + os.sep + "META-INF")
fname = self.tempdir + os.sep + "content.xml"
self.f = EncodedFile(open(fname,"wb"),'latin-1','utf-8')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
for key in self.paragraph_styles.keys():
style = self.paragraph_styles[key]
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
def close(self):
self.f.write('\n')
self.f.write('\n')
self.f.close()
self._write_styles_file()
self._write_manifest()
self._write_meta_file()
self._write_zip()
def _write_zip(self):
if os.path.isfile(self.filename):
os.unlink(self.filename)
os.system("cd " + self.tempdir + "; " + const.zipcmd + " " \
+ self.filename + " .")
os.unlink(self.tempdir + os.sep + "META-INF" + os.sep + "manifest.xml")
os.unlink(self.tempdir + os.sep + "content.xml")
os.unlink(self.tempdir + os.sep + "meta.xml")
os.unlink(self.tempdir + os.sep + "styles.xml")
# for image in self.image_list:
# os.unlink(self.tempdir + os.sep + "Pictures" + os.sep + image)
os.rmdir(self.tempdir + os.sep + "Pictures")
os.rmdir(self.tempdir + os.sep + "META-INF")
os.rmdir(self.tempdir)
def _write_styles_file(self):
file = self.tempdir + os.sep + "styles.xml"
self.f = EncodedFile(open(file,"wb"),'latin-1','utf-8')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
for style_name in self.draw_styles.keys():
style = self.draw_styles[style_name]
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
for key in self.paragraph_styles.keys():
style = self.paragraph_styles[key]
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
# Current no leading number format for headers
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n' % self.rmargin)
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.close()
def start_paragraph(self,style_name):
self.f.write('' % style_name)
def end_paragraph(self):
self.f.write('\n')
def write_text(self,text):
text = string.replace(text,'\t','')
text = string.replace(text,'\n','')
self.f.write(latin_to_utf8(text))
def _write_manifest(self):
file = self.tempdir + os.sep + "META-INF" + os.sep + "manifest.xml"
self.f = EncodedFile(open(file,"wb"),'latin-1','utf-8')
self.f.write('\n')
self.f.write('')
self.f.write('')
self.f.write('')
self.f.write('')
self.f.write('')
self.f.write('')
#self.f.write('')
self.f.write('\n')
self.f.close()
def _write_meta_file(self):
file = self.tempdir + os.sep + "meta.xml"
name = latin_to_utf8(self.name)
self.f = EncodedFile(open(file,"wb"),'latin-1','utf-8')
self.f.write('\n')
self.f.write('\n');
self.f.write('\n')
self.f.write('')
self.f.write(const.progName + ' ' + const.version)
self.f.write('\n')
self.f.write('')
self.f.write(name)
self.f.write('\n')
self.f.write('')
self.f.write(self.time)
self.f.write('\n')
self.f.write('')
self.f.write(name)
self.f.write('\n')
self.f.write('')
self.f.write(self.time)
self.f.write('\n')
self.f.write('0-00-00T00:00:00\n')
self.f.write('en-US\n')
self.f.write('1\n')
self.f.write('PT0S\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.write('\n')
self.f.close()
def start_page(self,orientation=None):
self.page = self.page + 1
self.f.write('\n')
def end_page(self):
self.f.write('\n')
def draw_line(self,style,x1,y1,x2,y2):
self.f.write('\n' % y2)
def draw_box(self,style,text,x,y):
box_style = self.draw_styles[style]
para_name = box_style.get_paragraph_style()
self.f.write('')
text = latin_to_utf8(string.replace(text,'\n',''))
self.f.write('>\n')
self.f.write('')
self.f.write(''+text+'\n')
self.f.write('\n')
else:
self.f.write('/>\n')
if __name__ == "__main__":
s = PaperStyle("Junk",27.94,21.59)
x = OpenDrawDoc(s,PAPER_PORTRAIT)
f = FontStyle()
f.set_type_face(FONT_SANS_SERIF)
f.set_size(14)
p = ParagraphStyle()
p.set_font(f)
x.add_paragraph_style("mytest",p)
g = GraphicsStyle()
g.set_width(2)
g.set_height(2)
g.set_paragraph_style("mytest")
g.set_shadow(1)
x.add_draw_style("mybox",g)
x.open("/home/dona/test")
x.start_page()
x.draw_box("mybox","Hello",4,4)
x.end_page()
x.close()