reportlab/pdfgen/pathobject.py
author rgbecker
Wed, 25 Oct 2000 08:57:46 +0000
changeset 494 54257447cfe9
parent 365 2a6ac05b39e8
child 562 6c9408ec3302
permissions -rwxr-xr-x
Changed to indirect copyright
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 365
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 365
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 365
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfgen/pathobject.py?cvsroot=reportlab
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 365
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/pdfgen/pathobject.py,v 1.7 2000/10/25 08:57:45 rgbecker Exp $
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 365
diff changeset
     5
__version__=''' $Id: pathobject.py,v 1.7 2000/10/25 08:57:45 rgbecker Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
     6
__doc__=""" 
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
PDFPathObject is an efficient way to draw paths on a Canvas. Do not
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     8
instantiate directly, obtain one from the Canvas instead.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    10
Progress Reports:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
8.83, 2000-01-13, gmcm:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    12
    created from pdfgen.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    13
"""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    14
import string
261
c924bbbde827 Full qualified a local import in pdfgen package
andy_robinson
parents: 16
diff changeset
    15
import reportlab.pdfgen.pdfgeom
365
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    16
from reportlab.pdfgen import pdfgeom
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    17
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    18
class PDFPathObject:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    19
    """Represents a graphic path.  There are certain 'modes' to PDF
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    20
    drawing, and making a separate object to expose Path operations
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    21
    ensures they are completed with no run-time overhead.  Ask
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    22
    the Canvas for a PDFPath with getNewPathObject(); moveto/lineto/
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    23
    curveto wherever you want; add whole shapes; and then add it back
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    24
    into the canvas with one of the relevant operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    25
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    26
    Path objects are probably not long, so we pack onto one line"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    27
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    28
        self._code = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    29
        self._code.append('n')   #newpath
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    30
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    31
    def getCode(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    32
        "pack onto one line; used internally"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    33
        return string.join(self._code, ' ')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    34
    def moveTo(self, x, y):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    35
        self._code.append('%0.2f %0.2f m' % (x,y))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    36
    def lineTo(self, x, y):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    37
        self._code.append('%0.2f %0.2f l' % (x,y))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    38
    def curveTo(self, x1, y1, x2, y2, x3, y3):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    39
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % (x1, y1, x2, y2, x3, y3))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    40
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    41
    def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    42
        """Contributed to piddlePDF by Robert Kern, 28/7/99.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
        Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    44
        starting at startAng degrees and covering extent degrees.   Angles
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    45
        start with 0 to the right (+x) and increase counter-clockwise.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    46
        These should have x1<x2 and y1<y2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    47
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    48
        The algorithm is an elliptical generalization of the formulae in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    49
        Jim Fitzsimmon's TeX tutorial <URL: http://www.tinaja.com/bezarc1.pdf>."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    50
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    51
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    52
        #move to first point
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    53
        self._code.append('%0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
    def arcTo(self, x1,y1, x2,y2, startAng=0, extent=90):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
        """Like arc, but draws a line from the current point to
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
        the start if the start is not the current point."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
        self._code.append('%0.2f %0.2f l' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
    def rect(self, x, y, width, height):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
        """Adds a rectangle to the path"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
        self._code.append('%0.2f %0.2f %0.2f %0.2f re' % (x, y, width, height))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
    def ellipse(self, x, y, width, height):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    71
        """adds an ellipse to the path"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    72
        pointList = pdfgeom.bezierArc(x, y, x + width,y + height, 0, 360)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    73
        self._code.append('%0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    74
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    75
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    76
       
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    77
    def circle(self, x_cen, y_cen, r):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    78
        """adds a circle to the path"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    79
        x1 = x_cen - r
365
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    80
        #x2 = x_cen + r
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    81
        y1 = y_cen - r
365
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    82
        #y2 = y_cen + r
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    83
        width = height = 2*r
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    84
        #self.ellipse(x_cen - r, y_cen - r, x_cen + r, y_cen + r)
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    85
        self.ellipse(x1, y1, width, height)
2a6ac05b39e8 fixed nameerror and path.circle bug
aaron_watters
parents: 261
diff changeset
    86
        
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    87
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    88
    def close(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    89
        "draws a line back to where it started"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    90
        self._code.append('h')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    91