# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2006 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 # # $Id$ #------------------------------------------------------------------------ # # Python modules # #------------------------------------------------------------------------ import time import cStringIO import os import tarfile from gettext import gettext as _ #------------------------------------------------------------------------ # # Gramps modules # #------------------------------------------------------------------------ import BaseDoc import Errors from PluginUtils import register_text_doc import ImgManip import Mime #------------------------------------------------------------------------ # # # #------------------------------------------------------------------------ def points(val): inch = float(val)/2.54 return (int(inch*72)) #------------------------------------------------------------------------ # # # #------------------------------------------------------------------------ class KwordDoc(BaseDoc.BaseDoc): def open(self,filename): self.media_list = [] if filename[-4:] != ".kwd": self.filename = filename + ".kwd" else: self.filename = filename self.f = cStringIO.StringIO() self.m = cStringIO.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.media_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 filedata in self.media_list: self.f.write('\n' % a[0]) self.f.write('\n') self.f.write('\n') try: archive = tarfile.open(self.filename,'w:gz') 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 tarinfo = tarfile.TarInfo('documentinfo.xml') tarinfo.size = len(self.m.getvalue()) tarinfo.mtime = self.mtime if os.sys.platform != "win32": tarinfo.uid = os.getuid() tarinfo.gid = os.getgid() self.m.seek(0) archive.addfile(tarinfo,self.m) tarinfo = tarfile.TarInfo('maindoc.xml') tarinfo.size = len(self.f.getvalue()) tarinfo.mtime = self.mtime if os.sys.platform != "win32": tarinfo.uid = os.getuid() tarinfo.gid = os.getgid() self.f.seek(0) archive.addfile(tarinfo,self.f) for filedata in self.media_list: archive.add(filedata[0]) archive.close() self.f.close() self.m.close() if self.print_req: apptype = 'application/x-kword' app = Mime.get_application(apptype) os.environ["FILE"] = self.filename os.system ('%s "$FILE" &' % app[0]) 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() == BaseDoc.FONT_SERIF: self.font_face = "Bitstream Vera Serif" else: self.font_face = "Bitstream Vera Sans" 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(unicode(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) tpad = points(self.p.get_top_margin()) bpad = points(self.p.get_bottom_margin()) self.f.write('\n' % (tpad,bpad)) if self.p.get_alignment() == BaseDoc.PARA_ALIGN_CENTER: self.f.write('\n') elif self.p.get_alignment() == BaseDoc.PARA_ALIGN_JUSTIFY: self.f.write('\n') elif self.p.get_alignment() == BaseDoc.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_media_object(self,name,pos,x_cm,y_cm): try: im = ImgManip.ImgManip(name) except: return (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.media_list)+1 tag = 'pictures/picture%d.jpeg' % index self.media_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_note(self,text,format,style_name): if format == 1: self.start_paragraph(style_name) self.write_text(text) self.end_paragraph() elif format == 0: for line in text.split('\n\n'): self.start_paragraph(style_name) line = line.replace('\n',' ') line = ' '.join(line.split()) self.write_text(line) self.end_paragraph() def write_text(self,text,mark=None): 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 #------------------------------------------------------------------------ # # Register the document generator with the GRAMPS plugin system # #------------------------------------------------------------------------ try: import Utils prog = Mime.get_application("application/x-kword") mtype = Mime.get_description("application/x-kword") if prog and Utils.search_for(prog[0]): print_label=_("Open in %s") % prog[1] else: print_label=None if mtype == _("unknown"): mtype = _('KWord') register_text_doc(mtype, KwordDoc, 1, 1, 1, ".kwd", print_label) except: register_text_doc(_('KWord'), KwordDoc, 1, 1, 1, ".kwd", print_label)