# # 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): self.photolist = [] 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') if self.paper.name == "A3": self.f.write('\n') self.f.write('' % sizes(self.lmargin)) 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') for file in self.photo_list: self.f.write('\n' % file) 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) for file in self.photo_list: f = open(file[0],"r") tar.add_file(file[1],mtime,f) f.close() 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,leader=None): self.format_list = [] self.bold_start = 0 self.text = "" self.style_name = style_name self.p = self.style_list[self.style_name] self.font = self.p.get_font() if self.font.get_type_face() == FONT_SERIF: self.font_face = "times" else: self.font_face = "helvetica" if leader != None: self.text = leader + chr(1) txt = '\n' % len(leader) txt = txt + '\n\n' % self.font_face txt = txt + '\n' % len(leader) txt = txt + '\n\n' % self.font_face self.format_list.append(txt) self.bold_stop = len(self.text) def end_paragraph(self): if self.bold_start != 0 and self.bold_stop != len(self.text): txt = '\n\n\n' % self.font_face self.format_list.append(txt) self.f.write('\n') self.f.write('') self.f.write(latin_to_utf8(self.text)) self.f.write('\n') old_pos = 0 self.f.write('\n') for format in self.format_list: self.f.write(format) self.f.write('\n') self.f.write('\n') self.f.write('\n' % self.style_name) padding = self.p.get_padding() self.f.write('\n' % sizes(padding)) if self.p.get_alignment() == PARA_ALIGN_CENTER: self.f.write('\n') elif self.p.get_alignment() == PARA_ALIGN_JUSTIFY: self.f.write('\n') elif self.p.get_alignment() == PARA_ALIGN_RIGHT: self.f.write('\n') first = self.p.get_first_indent() right = self.p.get_right_margin() left = self.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)) self.f.write('\n') self.f.write('\n' % self.font_face) self.f.write('\n' % self.font.get_size()) self.f.write('\n' % self.font.get_color()) if self.font.get_bold(): self.f.write('\n') if self.font.get_italic(): self.f.write('\n') if self.font.get_underline(): self.f.write('\n') if self.p.get_top_border(): self.f.write('\n') if self.p.get_bottom_border(): self.f.write('\n') if self.p.get_right_border(): self.f.write('\n') if self.p.get_left_border(): 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): self.bold_start = len(self.text) if self.bold_stop != self.bold_start: length = self.bold_stop - self.bold_start txt = '\n' % length txt = txt + '\n\n' % self.font_face self.format_list.append(txt) def end_bold(self): self.bold_stop = len(self.text) length = self.bold_stop - self.bold_start txt = '\n' % (self.bold_start,length) txt = txt + '\n\n\n' % self.font_face self.format_list.append(txt) 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 # index = len(self.photo_list)+1 # self.photo_list.append((name,'pictures/picture%d.jpeg' % index)) # txt = '\n' % len(self.text) # txt = txt + '\n\n' % name # # self.bold_stop = len(self.text) # self.format_list.append(txt) # # self.text = self.text + chr(1) def horizontal_line(self): pass def write_text(self,text): self.text = self.text + text if __name__ == "__main__": paper = PaperStyle("Letter",27.94,21.59) styles = StyleSheet() foo = FontStyle() foo.set_type_face(FONT_SANS_SERIF) foo.set_color((255,0,0)) foo.set_size(24) foo.set_underline(1) foo.set_bold(1) foo.set_italic(1) para = ParagraphStyle() para.set_alignment(PARA_ALIGN_RIGHT) para.set_font(foo) styles.add_style("Title",para) foo = FontStyle() foo.set_type_face(FONT_SERIF) foo.set_size(12) para = ParagraphStyle() para.set_font(foo) styles.add_style("Normal",para) doc = KwordDoc(styles,paper,PAPER_PORTRAIT) doc.open("/home/dona/test") doc.start_paragraph("Title") doc.write_text("My Title") doc.end_paragraph() doc.start_paragraph("Normal") doc.write_text("Hello there. This is fun") doc.end_paragraph() doc.start_paragraph("Normal") doc.add_photo("/home/dona/dad.jpg",200,200) doc.end_paragraph() doc.close()