From 374e418735088a706b28a1c9ec335d06939fd5d8 Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Wed, 11 Jun 2003 02:59:01 +0000 Subject: [PATCH] * src/docgen/PdfDrawDoc.py: Support for rotated text and wedges * src/docgen/PSDrawDoc.py: Support for rotated text and wedges svn: r1692 --- gramps2/src/docgen/PSDrawDoc.py | 114 +++++++++++++++++++++++++++-- gramps2/src/docgen/PdfDrawDoc.py | 121 +++++++++++++++++++++++-------- 2 files changed, 197 insertions(+), 38 deletions(-) diff --git a/gramps2/src/docgen/PSDrawDoc.py b/gramps2/src/docgen/PSDrawDoc.py index 867ec02bc..6639d4e23 100644 --- a/gramps2/src/docgen/PSDrawDoc.py +++ b/gramps2/src/docgen/PSDrawDoc.py @@ -19,6 +19,8 @@ # import string +from math import pi, cos, sin + import cStringIO import Plugins from intl import gettext as _ @@ -38,9 +40,6 @@ class PSDrawDoc(DrawDoc.DrawDoc): 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: @@ -67,7 +66,10 @@ class PSDrawDoc(DrawDoc.DrawDoc): font_name = "/Helvetica" return "%s findfont %d scalefont setfont\n" % (font_name,font.get_size()) - + + def translate(self,x,y): + return (x,self.height-y) + def open(self,filename): if filename[-3:] != ".ps": self.filename = filename + ".ps" @@ -88,6 +90,8 @@ class PSDrawDoc(DrawDoc.DrawDoc): self.f.write('%%PageOrder: Ascend\n') if self.orientation != TextDoc.PAPER_PORTRAIT: self.f.write('%%Orientation: Landscape\n') + else: + self.f.write('%%Orientation: Portrait\n') self.f.write('%%EndComments\n') self.f.write('/cm { 28.34 mul } def\n') @@ -118,6 +122,23 @@ class PSDrawDoc(DrawDoc.DrawDoc): self.f.write('showpage\n') self.f.write('%%PageTrailer\n') + def center_text(self,style,text,x,y): + x += self.lmargin + y += self.tmargin + + stype = self.draw_styles[style] + pname = stype.get_paragraph_style() + p = self.style_list[pname] + font = p.get_font() + + self.f.write('gsave\n') + self.f.write('%.4f %.4f %.4f setrgbcolor\n' % rgb_color(stype.get_color())) + self.f.write(self.fontdef(p)) + self.f.write('(%s) dup stringwidth pop -2 div ' % text.encode('iso-8859-1')) + self.f.write('%.4f cm add %.4f cm moveto ' % self.translate(x,y)) + self.f.write('show\n') + self.f.write('grestore\n') + def draw_text(self,style,text,x1,y1): stype = self.draw_styles[style] para_name = stype.get_paragraph_style() @@ -131,6 +152,88 @@ class PSDrawDoc(DrawDoc.DrawDoc): self.f.write(self.fontdef(p)) self.f.write('(%s) show grestore\n' % text) + def draw_wedge(self, style, centerx, centery, radius, start_angle, + end_angle, short_radius=0): + + while end_angle < start_angle: + end_angle += 360 + + p = [] + + degreestoradians = pi/180.0 + radiansdelta = degreestoradians/2 + sangle = start_angle*degreestoradians + eangle = end_angle*degreestoradians + while eangle=sangle: + x = centerx + cos(angle)*short_radius + y = centery + sin(angle)*short_radius + p.append((x,y)) + angle = angle-radiansdelta + self.draw_path(style,p) + + delta = (eangle - sangle)/2.0 + rad = short_radius + (radius-short_radius)/2.0 + + return ( (centerx + cos(sangle+delta) * rad), + (centery + sin(sangle+delta) * rad)) +# return ( (centerx + cos(sangle+delta) * rad)-self.lmargin, +# (centery + sin(sangle+delta) * rad)-self.tmargin) + + def rotate_text(self,style,text,x,y,angle): + + x += self.lmargin + y += self.tmargin + + stype = self.draw_styles[style] + pname = stype.get_paragraph_style() + p = self.style_list[pname] + font = p.get_font() + + size = font.get_size() + + self.f.write('gsave\n') + self.f.write(self.fontdef(p)) + self.f.write('%4.2f cm %4.2f cm translate\n' % self.translate(x,y)) + self.f.write('%4.2f rotate\n' % -angle) + + self.f.write('%.4f %.4f %.4f setrgbcolor\n' % rgb_color(stype.get_color())) + + val = len(text) + y = ((size * val)/2.0) - size + + for line in text: + self.f.write('(%s) dup stringwidth pop -2 div '% line.encode('iso-8859-1')) + self.f.write("%.4f moveto show\n" % y) + y -= size + + self.f.write('grestore\n') + def draw_path(self,style,path): stype = self.draw_styles[style] self.f.write('gsave\n') @@ -153,8 +256,7 @@ class PSDrawDoc(DrawDoc.DrawDoc): self.f.write('closepath\n') 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('gsave %.4f %.4f %.4f setrgbcolor fill grestore\n' % rgb_color(color)) self.f.write('%.4f %.4f %.4f setrgbcolor stroke\n' % rgb_color(stype.get_color())) self.f.write('grestore\n') diff --git a/gramps2/src/docgen/PdfDrawDoc.py b/gramps2/src/docgen/PdfDrawDoc.py index 61ae7f325..d53a76584 100644 --- a/gramps2/src/docgen/PdfDrawDoc.py +++ b/gramps2/src/docgen/PdfDrawDoc.py @@ -23,7 +23,7 @@ # Standard python libraries # #------------------------------------------------------------------------- -import string +from math import sin, cos, pi #------------------------------------------------------------------------- # @@ -49,6 +49,12 @@ try: except ImportError: raise Errors.PluginError( _("The ReportLab modules are not installed")) + +_H = 'Helvetica' +_HB = 'Helvetica-Bold' +_T = 'Times-Roman' +_TB = 'Times-Bold' + #------------------------------------------------------------------------- # # PdfDrawDoc @@ -136,7 +142,8 @@ class PdfDrawDoc(DrawDoc.DrawDoc): color = stype.get_fill_color() - self.f.setFillColor((float(color[0])/255.0,float(color[1])/255.0,float(color[2])/255.0)) + self.f.setFillColor((float(color[0])/255.0,float(color[1])/255.0, + float(color[2])/255.0)) p = self.f.beginPath() point = path[0] @@ -152,11 +159,11 @@ class PdfDrawDoc(DrawDoc.DrawDoc): else: self.f.drawPath(p,stroke=1,fill=0) - def draw_wedge(self, style, centerx, centery, radius, start_angle, end_angle, short_radius=0): + def draw_wedge(self, style, centerx, centery, radius, start_angle, + end_angle, short_radius=0): centerx += self.lmargin centery += self.bmargin - from math import sin, cos, pi def rnd(val): return val*cm @@ -171,7 +178,7 @@ class PdfDrawDoc(DrawDoc.DrawDoc): self.f.setDash([2,4],0) degreestoradians = pi/180.0 - radiansdelta = degreestoradians + radiansdelta = degreestoradians/2 sangle = start_angle*degreestoradians eangle = end_angle*degreestoradians while eangle