From 5fe16608bc2631f127aa7f3f6bb3761f444b007a Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Fri, 8 Jun 2001 01:27:48 +0000 Subject: [PATCH] Start of KWord (KOffice) support svn: r103 --- gramps/src/KwordDoc.py | 347 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 347 insertions(+) create mode 100644 gramps/src/KwordDoc.py diff --git a/gramps/src/KwordDoc.py b/gramps/src/KwordDoc.py new file mode 100644 index 000000000..3835cf9f9 --- /dev/null +++ b/gramps/src/KwordDoc.py @@ -0,0 +1,347 @@ +# +# 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 +# + +from TextDoc import * +from latin_utf8 import latin_to_utf8 +import utils +import time +import StringIO +import os +import gzip + +_BLKSIZE=512 + + +nul = '\0' + +#------------------------------------------------------------------------ +# +# +# +#------------------------------------------------------------------------ +class TarFile: + def __init__(self,name): + self.name = name + self.f = gzip.open(name,"wb") + self.pos = 0 + + def add_file(self,filename,mtime,iobuf): + iobuf.seek(0,2) + length = iobuf.tell() + iobuf.seek(0) + + buf = filename + buf = buf + '\0'*(100-len(filename)) + buf = buf + "0100664" + nul + buf = buf + "0000764" + nul + buf = buf + "0000764" + nul + buf = buf + "%011o" % length + nul + buf = buf + "%011o" % mtime + nul + buf = buf + "%s" + buf = buf + "0" + '\0'*100 + 'ustar \0' + buf = buf + '\0'*32 + buf = buf + '\0'*32 + buf = buf + '\0'*183 + + chksum = 0 + blank = " " + temp = buf % (blank) + for c in temp: + chksum = chksum + ord(c) + sum = "%06o " % chksum + sum = sum + nul + buf = buf % sum + + self.pos = self.pos + len(buf) + self.f.write(buf) + + buf = iobuf.read(length) + self.f.write(buf) + self.pos = self.pos + length + rem = _BLKSIZE - (self.pos % _BLKSIZE) + if rem != 0: + self.f.write('\0' * rem) + self.pos = self.pos + rem + + + def close(self): + rem = (_BLKSIZE*20) - (self.pos % (_BLKSIZE*20)) + if rem != 0: + self.f.write('\0' * rem) + self.f.close() + +#------------------------------------------------------------------------ +# +# +# +#------------------------------------------------------------------------ +def sizes(val): + mm = val*10 + inch = val/2.54 + points = int(inch*72) + return (points,utils.fl2txt("%.6f",mm),utils.fl2txt("%.6f",inch)) + +#------------------------------------------------------------------------ +# +# +# +#------------------------------------------------------------------------ +class KwordDoc(TextDoc): + + def open(self,filename): + if filename[-4:] != ".kwd": + self.filename = filename + ".kwd" + else: + self.filename = filename + + self.f = StringIO.StringIO() + self.m = StringIO.StringIO() + + self.m.write('') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.write('\n') + self.m.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') + + def close(self): + self.f.write('\n') + self.f.write('\n') + self.f.write('\n') + for name in self.style_list.keys(): + 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') + + mtime = time.time() + tar = TarFile(self.filename) + tar.add_file("documentinfo.xml",mtime,self.m) + tar.add_file("maindoc.xml",mtime,self.f) + tar.close() + + self.f.close() + self.m.close() + + def start_page(self,orientation=None): + pass + + def end_page(self): + pass + + def start_paragraph(self,style_name): + self.text = "" + self.style_name = style_name + pass + + def end_paragraph(self): + self.f.write('\n') + self.f.write('') + self.f.write(latin_to_utf8(self.text)) + self.f.write('\n') + self.f.write('\n') + self.f.write('\n' % self.style_name) + + p = self.style_list[self.style_name] + + padding = p.get_padding() + self.f.write('\n' % sizes(padding)) + + if p.get_alignment() == PARA_ALIGN_CENTER: + self.f.write('\n') + elif p.get_alignment() == PARA_ALIGN_JUSTIFY: + self.f.write('\n') + elif p.get_alignment() == PARA_ALIGN_RIGHT: + self.f.write('\n') + + first = p.get_first_indent() + right = p.get_right_margin() + left = p.get_left_margin() + + first = left+first + if first != 0: + self.f.write('\n' % sizes(first)) + if left != 0: + self.f.write('\n' % sizes(left)) + + font = self.style_list[self.style_name].get_font() + self.f.write('\n') + if font.get_type_face==FONT_SANS_SERIF: + self.f.write('\n') + else: + self.f.write('\n') + self.f.write('\n' % font.get_size()) + if font.get_bold(): + self.f.write('\n') + if font.get_italic(): + self.f.write('\n') + self.f.write('\n') + if left != 0: + self.f.write('\n' % sizes(left)) + self.f.write('\n') + self.f.write('\n') + + def start_bold(self): + pass + + def end_bold(self): + pass + + def start_table(self,name,style_name): + pass + + def end_table(self): + pass + + def start_row(self): + pass + + def end_row(self): + pass + + def start_cell(self,style_name,span=1): + pass + + def end_cell(self): + pass + + def add_photo(self,name,x,y): + pass + + def horizontal_line(self): + pass + + def write_text(self,text): + self.text = self.text + text +