* src/docgen/PdfDrawDoc.py: Support for rotated text and wedges

* src/docgen/PSDrawDoc.py: Support for rotated text and wedges


svn: r1692
This commit is contained in:
Don Allingham 2003-06-11 02:59:01 +00:00
parent 04726226c8
commit 4630bb94eb
2 changed files with 197 additions and 38 deletions

View File

@ -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:
eangle = eangle+2*pi
angle = sangle
if short_radius == 0:
p.append((centerx,centery))
else:
origx = (centerx + cos(angle)*short_radius)
origy = (centery + sin(angle)*short_radius)
p.append((origx, origy))
while angle<eangle:
x = centerx + cos(angle)*radius
y = centery + sin(angle)*radius
p.append((x,y))
angle = angle+radiansdelta
x = centerx + cos(eangle)*radius
y = centery + sin(eangle)*radius
p.append((x,y))
if short_radius:
x = centerx + cos(eangle)*short_radius
y = centery + sin(eangle)*short_radius
p.append((x,y))
angle = eangle
while angle>=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')

View File

@ -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<sangle:
@ -179,7 +186,8 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
angle = sangle
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()
if short_radius == 0:
@ -212,6 +220,13 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
p.close()
self.f.drawPath(p,stroke=1,fill=1)
delta = (eangle - sangle)/2.0
rad = short_radius + (radius-short_radius)/2.0
return ( (centerx + cos(sangle+delta) * rad)-self.lmargin,
(centery + sin(sangle+delta) * rad)-self.tmargin)
def draw_box(self,style,text,x,y):
x = x + self.lmargin
y = y + self.tmargin
@ -238,7 +253,7 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
if text != "":
lines = text.split('\n')
self.center_print(lines,font,x*cm,y*cm,w,h)
def draw_text(self,style,text,x,y):
x = x + self.lmargin
y = y + self.tmargin
@ -250,6 +265,66 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
self.f.setStrokeColor(make_color(font.get_color()))
self.left_print(text,font,x*cm,(y*cm)+font.get_size())
def pdf_set_font(self,font):
size = font.get_size()
if font.get_type_face() == TextDoc.FONT_SANS_SERIF:
if font.get_bold():
self.f.setFont(_HB,size)
else:
self.f.setFont(_H,size)
else:
if font.get_bold():
self.f.setFont(_TB,size)
else:
self.f.setFont(_T,size)
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.saveState()
self.pdf_set_font(font)
self.f.translate(x*cm,y*cm)
self.f.rotate(angle)
self.f.setStrokeColor(make_color(font.get_color()))
self.f.setFillColor(make_color(font.get_color()))
val = len(text)
y = ((-font.get_size() * val)/2.0) + font.get_size()
for line in text:
self.f.drawCentredString(0,y,line.encode('iso-8859-1'))
y += font.get_size()
self.f.restoreState()
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.saveState()
self.f.setStrokeColor(make_color(font.get_color()))
self.f.setFillColor(make_color(font.get_color()))
self.pdf_set_font(font)
self.f.drawCentredString(x*cm,y*cm,text.encode('iso-8859-1'))
self.f.restoreState()
def center_print(self,lines,font,x,y,w,h):
l = len(lines)
size = font.get_size()
@ -258,19 +333,11 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
self.f.saveState()
self.f.setFillColor(make_color(font.get_color()))
if font.get_type_face() == TextDoc.FONT_SANS_SERIF:
if font.get_bold():
self.f.setFont("Helvetica-Bold",font.get_size())
else:
self.f.setFont("Helvetica",font.get_size())
else:
if font.get_bold():
self.f.setFont("Times-Bold",font.get_size())
else:
self.f.setFont("Times-Roman",font.get_size())
self.f.setStrokeColor(make_color(font.get_color()))
self.pdf_set_font(font)
for text in lines:
self.f.drawCentredString(start_x,start_y,text)
self.f.drawCentredString(start_x,start_y,text.encode('iso-8859-1'))
start_y = start_y + size*1.2
start_y = start_y + size*1.2
@ -278,23 +345,13 @@ class PdfDrawDoc(DrawDoc.DrawDoc):
def left_print(self,text,font,x,y):
size = font.get_size()
start_y = y
start_x = x
self.f.saveState()
self.f.setFillColor(make_color(font.get_color()))
if font.get_type_face() == TextDoc.FONT_SANS_SERIF:
if font.get_bold():
self.f.setFont("Helvetica-Bold",font.get_size())
else:
self.f.setFont("Helvetica",font.get_size())
else:
if font.get_bold():
self.f.setFont("Times-Bold",font.get_size())
else:
self.f.setFont("Times-Roman",font.get_size())
self.f.setStrokeColor(make_color(font.get_color()))
self.pdf_set_font(font)
self.f.drawString(start_x,start_y,text)
self.f.drawString(start_x,start_y,text.encode('iso-8859-1'))
self.f.restoreState()