# $Id$ # Copyright (C) 2005 Greg Landrum and Rational Discovery LLC # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """pidReportLab Bits have been shamelessly cobbled from piddlePDF.py and/or piddlePS.py Greg Landrum (greg.landrum@gmail.com) 3/28/2005 """ """ Functionality implemented: -drawLine -drawPolygon -drawEllipse -drawArc -drawCurve -drawString (rotated text is, mostly, fine... see below) -drawImage """ from sping.pid import * from sping.PDF import pidPDF,pdfmetrics from reportlab.lib import colors from reportlab.graphics import shapes import string, os, types from math import * def colorToRL(color): if color != transparent: return colors.Color(color.red,color.green,color.blue) else: return None class RLCanvas( Canvas ): def __init__(self, size=(300,300), name='RLCanvas'): self.size = size self._initOutput() Canvas.__init__(self, size, name) self.drawing = shapes.Drawing(size[0],size[1]) def _initOutput(self): pass # public functions def clear(self): self._initOutput() def flush(self): # self.save('svg') pass # to fit new definition of flush() -cwl def save(self, file=None, format=None): """Hand this either a file= or file = . """ if not file: file = self.name from reportlab.graphics import renderPDF renderPDF.drawToFile(self.drawing,file,self.name) def fixY(self,y): return self.size[1]-y # taken from pidPDF.py def _findPostScriptFontName(self, font): """Attempts to return proper font name.""" #maps a piddle font to a postscript one. #step 1 - no face ends up serif, others are lowercased if not font.face: face = 'serif' else: face = font.face.lower() while pidPDF.font_face_map.has_key(face): face = pidPDF.font_face_map[face] #step 2, - resolve bold/italic to get the right PS font name psname = pidPDF.ps_font_map[(face, font.bold, font.italic)] return psname #------------- drawing methods -------------- def drawLine(self, x1,y1, x2,y2, color=None, width=None, dash=None,**kwargs): "Draw a straight line between x1,y1 and x2,y2." # set color... if color: if color == transparent: return elif self.defaultLineColor == transparent: return else: color = self.defaultLineColor color = colorToRL(color) if width: w = width else: w = self.defaultLineWidth self.drawing.add(shapes.Line(x1,self.fixY(y1),x2,self.fixY(y2),strokeColor=color,strokeWidth=w, strokeDashArray=dash)) return def drawCurve(self, x1,y1, x2,y2, x3,y3, x4,y4, closed=0, **kwargs): "Draw a Bezier curve with control points x1,y1 to x4,y4." pts = self.curvePoints(x1, y1, x2, y2, x3, y3, x4, y4) if not closed: pointlist = [ (pts[x][0],pts[x][1],pts[x+1][0],pts[x+1][1]) for x in range(len(pts)-1)] self.drawLines(pointlist,**kwargs) else: self.drawPolygon(pointlist,closed=1,**kwargs) def drawArc(self, x1,y1, x2,y2, startAng=0, extent=360, edgeColor=None, edgeWidth=None, fillColor=None, dash=None,**kwargs): """Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2, starting at startAng degrees and covering extent degrees. Angles start with 0 to the right (+x) and increase counter-clockwise. These should have x1