1
#Copyright ReportLab Europe Ltd. 2000-2004
2
#see license.txt for license details
3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfgen/pathobject.py
4
__version__=''' $Id$ '''
6
PDFPathObject is an efficient way to draw paths on a Canvas. Do not
7
instantiate directly, obtain one from the Canvas instead.
10
8.83, 2000-01-13, gmcm:
11
created from pdfgen.py
15
from reportlab.pdfgen import pdfgeom
16
from reportlab.lib.utils import fp_str
20
"""Represents a graphic path. There are certain 'modes' to PDF
21
drawing, and making a separate object to expose Path operations
22
ensures they are completed with no run-time overhead. Ask
23
the Canvas for a PDFPath with getNewPathObject(); moveto/lineto/
24
curveto wherever you want; add whole shapes; and then add it back
25
into the canvas with one of the relevant operators.
27
Path objects are probably not long, so we pack onto one line"""
31
self._code.append('n') #newpath
34
"pack onto one line; used internally"
35
return string.join(self._code, ' ')
37
def moveTo(self, x, y):
38
self._code.append('%s m' % fp_str(x,y))
40
def lineTo(self, x, y):
41
self._code.append('%s l' % fp_str(x,y))
43
def curveTo(self, x1, y1, x2, y2, x3, y3):
44
self._code.append('%s c' % fp_str(x1, y1, x2, y2, x3, y3))
46
def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
47
"""Contributed to piddlePDF by Robert Kern, 28/7/99.
48
Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
49
starting at startAng degrees and covering extent degrees. Angles
50
start with 0 to the right (+x) and increase counter-clockwise.
51
These should have x1<x2 and y1<y2.
53
The algorithm is an elliptical generalization of the formulae in
54
Jim Fitzsimmon's TeX tutorial <URL: http://www.tinaja.com/bezarc1.pdf>."""
56
pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
58
self._code.append('%s m' % fp_str(pointList[0][:2]))
59
for curve in pointList:
60
self._code.append('%s c' % fp_str(curve[2:]))
62
def arcTo(self, x1,y1, x2,y2, startAng=0, extent=90):
63
"""Like arc, but draws a line from the current point to
64
the start if the start is not the current point."""
65
pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
66
self._code.append('%s l' % fp_str(pointList[0][:2]))
67
for curve in pointList:
68
self._code.append('%s c' % fp_str(curve[2:]))
70
def rect(self, x, y, width, height):
71
"""Adds a rectangle to the path"""
72
self._code.append('%s re' % fp_str((x, y, width, height)))
74
def ellipse(self, x, y, width, height):
75
"""adds an ellipse to the path"""
76
pointList = pdfgeom.bezierArc(x, y, x + width,y + height, 0, 360)
77
self._code.append('%s m' % fp_str(pointList[0][:2]))
78
for curve in pointList:
79
self._code.append('%s c' % fp_str(curve[2:]))
81
def circle(self, x_cen, y_cen, r):
82
"""adds a circle to the path"""
88
#self.ellipse(x_cen - r, y_cen - r, x_cen + r, y_cen + r)
89
self.ellipse(x1, y1, width, height)
92
"draws a line back to where it started"
93
self._code.append('h')