gramps/gramps2/src/docgen/PSDrawDoc.py

253 lines
8.8 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# 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 string
2003-01-31 09:58:39 +05:30
import cStringIO
2002-10-20 19:55:16 +05:30
import Plugins
from intl import gettext as _
2002-10-20 19:55:16 +05:30
import TextDoc
import DrawDoc
2002-10-20 19:55:16 +05:30
2003-02-05 19:23:00 +05:30
def pt2cm(val):
return (float(val)/72.0)*2.54
2002-10-20 19:55:16 +05:30
class PSDrawDoc(DrawDoc.DrawDoc):
2002-10-20 19:55:16 +05:30
def __init__(self,styles,type,orientation):
DrawDoc.DrawDoc.__init__(self,styles,type,orientation)
2002-10-20 19:55:16 +05:30
self.f = None
self.filename = None
self.level = 0
self.page = 0
def translate(self,x,y):
return (x,self.height-y)
def fontdef(self,para):
font = para.get_font()
if font.get_type_face() == TextDoc.FONT_SERIF:
2002-10-20 19:55:16 +05:30
if font.get_bold():
if font.get_italic():
font_name = "/Times-BoldItalic"
else:
font_name = "/Times-Bold"
else:
if font.get_italic():
font_name = "/Times-Italic"
else:
font_name = "/Times-Roman"
else:
if font.get_bold():
if font.get_italic():
font_name = "/Helvetica-BoldOblique"
else:
font_name = "/Helvetica-Bold"
else:
if font.get_italic():
font_name = "/Helvetica-Oblique"
else:
font_name = "/Helvetica"
return "%s findfont %d scalefont setfont\n" % (font_name,font.get_size())
def open(self,filename):
if filename[-3:] != ".ps":
self.filename = filename + ".ps"
else:
self.filename = filename
2003-01-31 09:58:39 +05:30
self.f = open(self.filename,"w")
2002-10-20 19:55:16 +05:30
self.f.write('%!PS-Adobe-3.0\n')
self.f.write('%%LanguageLevel: 2\n')
self.f.write('%%Pages: (atend)\n')
self.f.write('%%PageOrder: Ascend\n')
if self.orientation != TextDoc.PAPER_PORTRAIT:
self.f.write('%%Orientation: Landscape\n')
2002-10-20 19:55:16 +05:30
self.f.write('%%EndComments\n')
self.f.write('/cm { 28.34 mul } def\n')
def close(self):
self.f.write('%%Trailer\n')
self.f.write('%%Pages: ')
self.f.write('%d\n' % self.page)
self.f.write('%%EOF\n')
self.f.close()
def start_paragraph(self,style_name):
pass
def end_paragraph(self):
pass
def write_text(self,text):
pass
def start_page(self,orientation=None):
self.page = self.page + 1
self.f.write("%%Page:")
self.f.write("%d %d\n" % (self.page,self.page))
if self.orientation != TextDoc.PAPER_PORTRAIT:
self.f.write('90 rotate %5.2f cm %5.2f cm translate\n' % (0,-1*self.height))
2002-10-20 19:55:16 +05:30
def end_page(self):
self.f.write('showpage\n')
self.f.write('%%PageTrailer\n')
2003-01-31 09:58:39 +05:30
def draw_text(self,style,text,x1,y1):
stype = self.draw_styles[style]
para_name = stype.get_paragraph_style()
p = self.style_list[para_name]
2003-02-05 19:23:00 +05:30
x1 = x1 + self.lmargin
y1 = y1 + self.tmargin + pt2cm(p.get_font().get_size())
2003-01-31 09:58:39 +05:30
self.f.write('gsave\n')
self.f.write('%f cm %f cm moveto\n' % self.translate(x1,y1))
self.f.write(self.fontdef(p))
2003-02-09 05:14:44 +05:30
self.f.write('(%s) show grestore\n' % text)
2003-01-31 09:58:39 +05:30
2003-02-08 22:58:41 +05:30
def draw_path(self,style,path):
2003-02-05 19:23:00 +05:30
stype = self.draw_styles[style]
self.f.write('gsave\n')
self.f.write('newpath\n')
2003-02-09 05:14:44 +05:30
self.f.write('%.4f setlinewidth\n' % stype.get_line_width())
2003-02-05 19:23:00 +05:30
if stype.get_line_style() == DrawDoc.SOLID:
self.f.write('[] 0 setdash\n')
else:
self.f.write('[2 4] 0 setdash\n')
point = path[0]
x1 = point[0]+self.lmargin
y1 = point[1]+self.tmargin
self.f.write('%f cm %f cm moveto\n' % self.translate(x1,y1))
for point in path[1:]:
x1 = point[0]+self.lmargin
y1 = point[1]+self.tmargin
self.f.write('%f cm %f cm lineto\n' % self.translate(x1,y1))
self.f.write('closepath\n')
2003-02-09 05:14:44 +05:30
color = stype.get_fill_color()
if (color[0] != 255 or color[1] != 255 or color[2] != 255) :
self.f.write('%.4f %.4f %.4f setrgbcolor fill\n' % rgb_color(color))
self.f.write('%.4f %.4f %.4f setrgbcolor stroke\n' % rgb_color(stype.get_color()))
2003-02-05 19:23:00 +05:30
self.f.write('grestore\n')
2002-10-20 19:55:16 +05:30
def draw_line(self,style,x1,y1,x2,y2):
2003-01-31 09:58:39 +05:30
x1 = x1 + self.lmargin
x2 = x2 + self.lmargin
y1 = y1 + self.tmargin
y2 = y2 + self.tmargin
stype = self.draw_styles[style]
2003-02-09 05:14:44 +05:30
self.f.write('gsave newpath\n')
2002-10-20 19:55:16 +05:30
self.f.write('%f cm %f cm moveto\n' % self.translate(x1,y1))
self.f.write('%f cm %f cm lineto\n' % self.translate(x2,y2))
2003-02-09 05:14:44 +05:30
self.f.write('%.4f setlinewidth\n' % stype.get_line_width())
2003-02-04 00:54:27 +05:30
if stype.get_line_style() == DrawDoc.SOLID:
self.f.write('[] 0 setdash\n')
else:
self.f.write('[2 4] 0 setdash\n')
2002-10-20 19:55:16 +05:30
self.f.write('2 setlinecap\n')
2003-02-09 05:14:44 +05:30
self.f.write('%.4f %.4f %.4f setrgbcolor stroke\n' % rgb_color(stype.get_color()))
2002-10-20 19:55:16 +05:30
self.f.write('grestore\n')
2003-01-31 09:58:39 +05:30
def patch_text(self,text):
return text.encode('iso-8859-1')
def draw_bar(self,style,x1,y1,x2,y2):
x1 = x1 + self.lmargin
x2 = x2 + self.lmargin
y1 = y1 + self.tmargin
y2 = y2 + self.tmargin
box_type = self.draw_styles[style]
self.f.write('gsave\n')
self.f.write("%f cm %f cm moveto\n" % self.translate(x1,y1))
self.f.write("0 %f cm rlineto\n" % (y2-y1))
self.f.write("%f cm 0 rlineto\n" % (x2-x1))
self.f.write("0 %f cm rlineto\n" % (y1-y2))
self.f.write('closepath\n')
2003-02-09 05:14:44 +05:30
self.f.write("%.4f setlinewidth\n" % box_type.get_line_width())
self.f.write('%.4f %.4f %.4f setrgbcolor stroke\n' % rgb_color(box_type.get_color()))
2003-01-31 09:58:39 +05:30
self.f.write('grestore\n')
2002-10-20 19:55:16 +05:30
def draw_box(self,style,text,x,y):
2003-01-31 09:58:39 +05:30
x = x + self.lmargin
y = y + self.tmargin
2002-10-20 19:55:16 +05:30
box_style = self.draw_styles[style]
para_name = box_style.get_paragraph_style()
p = self.style_list[para_name]
2003-01-31 09:58:39 +05:30
text = self.patch_text(text)
2002-10-20 19:55:16 +05:30
bh = box_style.get_height()
bw = box_style.get_width()
self.f.write('gsave\n')
2003-01-31 09:58:39 +05:30
# if box_style.get_shadow():
2002-10-20 19:55:16 +05:30
self.f.write('newpath\n')
self.f.write('%f cm %f cm moveto\n' % self.translate(x+0.15,y+0.15))
self.f.write('0 -%f cm rlineto\n' % bh)
self.f.write('%f cm 0 rlineto\n' % bw)
self.f.write('0 %f cm rlineto\n' % bh)
self.f.write('closepath\n')
self.f.write('.5 setgray\n')
self.f.write('fill\n')
self.f.write('newpath\n')
self.f.write('%f cm %f cm moveto\n' % self.translate(x,y))
self.f.write('0 -%f cm rlineto\n' % bh)
self.f.write('%f cm 0 rlineto\n' % bw)
self.f.write('0 %f cm rlineto\n' % bh)
self.f.write('closepath\n')
self.f.write('1 setgray\n')
self.f.write('fill\n')
self.f.write('newpath\n')
self.f.write('%f cm %f cm moveto\n' % self.translate(x,y))
self.f.write('0 -%f cm rlineto\n' % bh)
self.f.write('%f cm 0 rlineto\n' % bw)
self.f.write('0 %f cm rlineto\n' % bh)
self.f.write('closepath\n')
2003-02-09 05:14:44 +05:30
self.f.write('%.4f setlinewidth\n' % box_style.get_line_width())
self.f.write('%.4f %.4f %.4f setrgbcolor stroke\n' % rgb_color(box_style.get_color()))
2002-10-20 19:55:16 +05:30
if text != "":
self.f.write(self.fontdef(p))
lines = string.split(text,'\n')
nlines = len(lines)
mar = 10/28.35
f_in_cm = p.get_font().get_size()/28.35
fs = f_in_cm * 1.2
center = y + (bh + fs)/2.0 + (fs*0.2)
ystart = center - (fs/2.0) * nlines
for i in range(nlines):
ypos = ystart + (i * fs)
self.f.write('%f cm %f cm moveto\n' % self.translate(x+mar,ypos))
self.f.write("(%s) show\n" % lines[i])
self.f.write('grestore\n')
2003-02-09 05:14:44 +05:30
def rgb_color(color):
r = float(color[0])/255.0
g = float(color[1])/255.0
b = float(color[2])/255.0
return (r,g,b)
2002-10-20 19:55:16 +05:30
Plugins.register_draw_doc(_("PostScript"),PSDrawDoc,1,1,".ps");