# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2003 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 TextDoc from latin_utf8 import latin_to_utf8 import time import StringIO import string import gzip import Errors from TarFile import TarFile import Plugins import ImgManip from intl import gettext as _ def points(val): inch = float(val)/2.54 return (int(inch*72)) #------------------------------------------------------------------------ # # # #------------------------------------------------------------------------ class KwordDoc(TextDoc.TextDoc): def open(self,filename): self.photo_list = [] 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('') self.f.write('') self.f.write('\n') self.mtime = time.time() if self.paper.name == "A3": self.f.write('\n') self.f.write('' % points(self.lmargin)) self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.cell_row= 0 self.cell_col= 0 self.frameset_flg= 1 self.table_no= 0 self.cell_style= "" self.cell_span= 1 def close(self): if self.frameset_flg == 1: self.f.write('\n') self.frameset_flg= 0 for p in self.photo_list: self.f.write('\n' % p[1]) self.f.write('\n') self.f.write('\n') self.f.write('\n' % a[0]) self.f.write('\n') 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' % a[0]) self.f.write('\n') self.f.write('\n') try: tar = TarFile(self.filename) except IOError, msg: text = _("Could not open %s") % self.filename Errors.ReportError(text + "\n" + str(msg)) return except: Errors.ReportError(_("Could not open %s") % self.filename) return tar.add_file("documentinfo.xml",self.mtime,self.m) tar.add_file("maindoc.xml",self.mtime,self.f) for file in self.photo_list: f = open(file[0],"r") tar.add_file(file[1],self.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() == TextDoc.FONT_SERIF: self.font_face = "Arial" else: self.font_face = "Times New Roman" if leader != None: self.text = leader + '\t' txt = '\n' % (len(leader)+1) txt = txt + '\n\n' % self.font_face self.format_list.append(txt) self.bold_stop = len(self.text) def end_paragraph(self): if self.frameset_flg == 0: self.f.write('\n') self.frameset_flg= 1 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') 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) pad = points(self.p.get_padding())/2 self.f.write('\n' % (pad,pad)) if self.p.get_alignment() == TextDoc.PARA_ALIGN_CENTER: self.f.write('\n') elif self.p.get_alignment() == TextDoc.PARA_ALIGN_JUSTIFY: self.f.write('\n') elif self.p.get_alignment() == TextDoc.PARA_ALIGN_RIGHT: self.f.write('\n') else: self.f.write('\n') first = self.p.get_first_indent() left = self.p.get_left_margin() right = self.p.get_right_margin() self.f.write('\n' % (points(left),points(right))) 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' % points(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' % (self.bold_stop,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): self.tbl= self.table_styles[style_name] self.cell_left= (self.lmargin * 72)/ 2.54 self.tbl_width= ((self.width - self.lmargin - self.rmargin) * 72 ) / 2.54 if self.frameset_flg == 1: self.f.write(' \n') self.cell_row= 0 self.cell_col= 0 self.frameset_flg= 0 def end_table(self): self.table_no= self.table_no + 1 def start_row(self): pass def end_row(self): self.cell_row= self.cell_row + 1 self.cell_col= 0 self.cell_left= (self.lmargin * 72)/ 2.54 def start_cell(self,style_name,span=1): self.cell_span= span self.cell_style= style_name self.cell_right = self.cell_left for i in range(0,span): col_width = self.tbl.get_column_width(self.cell_col+i) spc = (self.tbl_width * col_width) / 100 self.cell_right = self.cell_right + spc self.f.write('\n' % (self.table_no, self.cell_row, self.cell_col)) self.f.write('\n' % (self.cell_row*23+self.table_no*125+95)) self.frameset_flg= 1 self.cell_col = self.cell_col + span - 1 def end_cell(self): self.f.write('\n') self.cell_col= self.cell_col + 1 self.frameset_flg= 0 self.cell_left= self.cell_right def add_photo(self,name,pos,x_cm,y_cm): im = ImgManip.ImgManip(name) (x,y)= im.size() ratio = float(x_cm)*float(y)/(float(y_cm)*float(x)) if ratio < 1: act_width = x_cm act_height = y_cm*ratio else: act_height = y_cm act_width = x_cm/ratio index = len(self.photo_list)+1 tag = 'pictures/picture%d.jpeg' % index self.photo_list.append((name,tag,act_width,act_height)) txt = '\n' % len(self.text) txt = txt + '\n' % tag txt = txt + '\n' self.bold_stop = len(self.text) self.format_list.append(txt) self.text = self.text + '#' def horizontal_line(self): pass def write_text(self,text): text = text.replace('&','&'); # Must be first text = text.replace('<','<'); text = text.replace('>','>'); pos = text.find('<super>') if pos >= 0: self.start_pos = len(self.text)+pos text = text.replace('<super>','') pos = text.find('</super>') if pos >= 0: end = len(self.text)+pos - self.start_pos text = text.replace('<super>','') txt = '\n' % (self.start_pos, end) txt = txt + '\n' text = text.replace('</super>','') self.format_list.append(txt) self.text = self.text + text Plugins.register_text_doc(_("KWord"),KwordDoc,1,1,1,".kwd")