reportlab/graphics/renderPM.py
author rgbecker
Wed, 15 Mar 2006 16:47:27 +0000
changeset 2574 d81949596070
parent 2553 a880f43d10bd
child 2575 0cba68b93555
permissions -rw-r--r--
reportlab: changes to accomodate barcodes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2214
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
     3
#history www.reportlab.co.uk/rl-cgi/viewcvs.cgi/rlextra/graphics/Csrc/renderPM/renderP.py
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2214
diff changeset
     4
__version__=''' $Id$ '''
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
     5
"""Usage:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
     6
    from reportlab.graphics import renderPM
1845
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
     7
    renderPM.drawToFile(drawing,filename,fmt='GIF',configPIL={....})
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
     8
Other functions let you create a PM drawing as string or into a PM buffer.
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
     9
Execute the script to see some test drawings."""
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    10
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    11
from reportlab.graphics.shapes import *
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2405
diff changeset
    12
from reportlab.graphics.renderbase import StateTracker, getStateDelta, renderScaledDrawing
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    13
from reportlab.pdfbase.pdfmetrics import getFont
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    14
from math import sin, cos, pi, ceil
2357
4c3231049766 Add reader callback arg to _renderPM.makeT1Font
rgbecker
parents: 2332
diff changeset
    15
from reportlab.lib.utils import getStringIO, open_and_read
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
    16
from reportlab import rl_config
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    17
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    18
class RenderPMError(Exception):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    19
    pass
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    20
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    21
import string, os, sys
903
d1970ac2e18f Readded import checks and improved messages
rgbecker
parents: 902
diff changeset
    22
d1970ac2e18f Readded import checks and improved messages
rgbecker
parents: 902
diff changeset
    23
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    24
    import _renderPM
903
d1970ac2e18f Readded import checks and improved messages
rgbecker
parents: 902
diff changeset
    25
except ImportError, errMsg:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    26
    raise ImportError, "No module named _renderPM\n" + \
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
    27
        (str(errMsg)!='No module named _renderPM' and "it may be the wrong version or badly installed!" or
2214
975720ae5f47 URL change from Aleksander Piotrowski
rgbecker
parents: 2206
diff changeset
    28
                                    "see http://www.reportlab.org/rl_addons.html")
903
d1970ac2e18f Readded import checks and improved messages
rgbecker
parents: 902
diff changeset
    29
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    30
from types import TupleType, ListType
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    31
_SeqTypes = (TupleType,ListType)
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    32
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    33
def _getImage():
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    34
    try:
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    35
        from PIL import Image
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    36
    except ImportError:
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    37
        import Image
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    38
    return Image
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
    39
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    40
def Color2Hex(c):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    41
    #assert isinstance(colorobj, colors.Color) #these checks don't work well RGB
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    42
    if c: return ((0xFF&int(255*c.red)) << 16) | ((0xFF&int(255*c.green)) << 8) | (0xFF&int(255*c.blue))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    43
    return c
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    44
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    45
# the main entry point for users...
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    46
def draw(drawing, canvas, x, y, showBoundary=rl_config._unset_):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    47
    """As it says"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    48
    R = _PMRenderer()
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
    49
    R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
    50
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    51
from reportlab.graphics.renderbase import Renderer
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    52
class _PMRenderer(Renderer):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    53
    """This draws onto a pix map image. It needs to be a class
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    54
    rather than a function, as some image-specific state tracking is
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    55
    needed outside of the state info in the SVG model."""
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    56
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    57
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    58
        self._tracker = StateTracker()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    59
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    60
    def pop(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    61
        self._tracker.pop()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    62
        self.applyState()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    63
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    64
    def push(self,node):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    65
        deltas = getStateDelta(node)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    66
        self._tracker.push(deltas)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    67
        self.applyState()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    68
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    69
    def applyState(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    70
        s = self._tracker.getState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    71
        self._canvas.ctm = s['ctm']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    72
        self._canvas.strokeWidth = s['strokeWidth']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    73
        self._canvas.strokeColor = Color2Hex(s['strokeColor'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    74
        self._canvas.lineCap = s['strokeLineCap']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    75
        self._canvas.lineJoin = s['strokeLineJoin']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    76
        da = s['strokeDashArray']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    77
        da = da and (0,da) or None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    78
        self._canvas.dashArray = da
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    79
        self._canvas.fillColor = Color2Hex(s['fillColor'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    80
        self._canvas.setFont(s['fontName'], s['fontSize'])
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    81
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    82
    def initState(self,x,y):
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    83
        deltas = STATE_DEFAULTS.copy()
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    84
        deltas['transform'] = self._canvas._baseCTM[0:4]+(x,y)
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    85
        self._tracker.push(deltas)
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
    86
        self.applyState()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    87
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    88
    def drawNode(self, node):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    89
        """This is the recursive method called for each node
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    90
        in the tree"""
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    91
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    92
        #apply state changes
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    93
        self.push(node)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    94
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    95
        #draw the object, or recurse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    96
        self.drawNodeDispatcher(node)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
    97
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    98
        # restore the state
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    99
        self.pop()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   100
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   101
    def drawRect(self, rect):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   102
        c = self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   103
        if rect.rx == rect.ry == 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   104
            #plain old rectangle, draw clockwise (x-axis to y-axis) direction
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   105
            c.rect(rect.x,rect.y, rect.width, rect.height)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   106
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   107
            c.roundRect(rect.x,rect.y, rect.width, rect.height, rect.rx, rect.ry)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   108
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   109
    def drawLine(self, line):
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   110
        self._canvas.line(line.x1,line.y1,line.x2,line.y2)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   111
1680
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   112
    def drawImage(self, image):
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   113
        if image.path and os.path.exists(image.path):
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   114
            if type(image.path) is type(''):
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   115
                im = _getImage().open(image.path).convert('RGB')
1680
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   116
            else:
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   117
                im = image.path.convert('RGB')
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   118
            srcW, srcH = im.size
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   119
            dstW, dstH = image.width, image.height
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   120
            if dstW is None: dstW = srcW
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   121
            if dstH is None: dstH = srcH
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   122
            self._canvas._aapixbuf(
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   123
                    image.x, image.y, dstW, dstH,
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   124
                    im.tostring(), srcW, srcH, 3,
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   125
                    )
261fd842e14d Changes for Dinu's image shape
rgbecker
parents: 1677
diff changeset
   126
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   127
    def drawCircle(self, circle):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   128
        c = self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   129
        c.circle(circle.cx,circle.cy, circle.r)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   130
        c.fillstrokepath()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   131
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   132
    def drawPolyLine(self, polyline, _doClose=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   133
        P = polyline.points
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   134
        assert len(P) >= 2, 'Polyline must have 1 or more points'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   135
        c = self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   136
        c.pathBegin()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   137
        c.moveTo(P[0], P[1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   138
        for i in range(2, len(P), 2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   139
            c.lineTo(P[i], P[i+1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   140
        if _doClose:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   141
            c.pathClose()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   142
            c.pathFill()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   143
        c.pathStroke()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   144
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   145
    def drawEllipse(self, ellipse):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   146
        c=self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   147
        c.ellipse(ellipse.cx, ellipse.cy, ellipse.rx,ellipse.ry)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   148
        c.fillstrokepath()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   149
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   150
    def drawPolygon(self, polygon):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   151
        self.drawPolyLine(polygon,_doClose=1)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   152
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   153
    def drawString(self, stringObj):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   154
        fill = self._canvas.fillColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   155
        if fill is not None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   156
            S = self._tracker.getState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   157
            text_anchor, x, y, text = S['textAnchor'], stringObj.x,stringObj.y,stringObj.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   158
            if not text_anchor in ['start','inherited']:
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
   159
                font, font_size = S['fontName'], S['fontSize']
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   160
                textLen = stringWidth(text, font,font_size)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   161
                if text_anchor=='end':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   162
                    x = x-textLen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   163
                elif text_anchor=='middle':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   164
                    x = x - textLen/2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   165
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   166
                    raise ValueError, 'bad value for textAnchor '+str(text_anchor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   167
            self._canvas.drawString(x,y,text)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   168
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   169
    def drawPath(self, path):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   170
        c = self._canvas
1697
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   171
        if path is EmptyClipPath:
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   172
            del c._clipPaths[-1]
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   173
            if c._clipPaths:
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   174
                P = c._clipPaths[-1]
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   175
                icp = P.isClipPath
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   176
                P.isClipPath = 1
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   177
                self.drawPath(P)
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   178
                P.isClipPath = icp
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   179
            else:
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   180
                c.clipPathClear()
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   181
            return
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   182
        c.pathBegin()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   183
        drawFuncs = (c.moveTo, c.lineTo, c.curveTo, c.pathClose)
1697
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   184
        from reportlab.graphics.shapes import _renderPath
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   185
        isClosed = _renderPath(path, drawFuncs)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   186
        if path.isClipPath:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   187
            c.clipPathSet()
1697
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   188
            c._clipPaths.append(path)
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   189
        else:
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   190
            if isClosed: c.pathFill()
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   191
            c.pathStroke()
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 1038
diff changeset
   192
1327
d83a6065cf94 split functionality into _setFont
rgbecker
parents: 1312
diff changeset
   193
def _setFont(gs,fontName,fontSize):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   194
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   195
        gs.setFont(fontName,fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   196
    except _renderPM.Error, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   197
        if errMsg.args[0]!="Can't find font!": raise
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   198
        #here's where we try to add a font to the canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   199
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   200
            f = getFont(fontName)
2386
320b823de224 renderPM.py: backward compatibility fix for _renderPM version <=0.98
rgbecker
parents: 2360
diff changeset
   201
            if _renderPM._version<='0.98':  #added reader arg in 0.99
320b823de224 renderPM.py: backward compatibility fix for _renderPM version <=0.98
rgbecker
parents: 2360
diff changeset
   202
                _renderPM.makeT1Font(fontName,f.face.findT1File(),f.encoding.vector)
320b823de224 renderPM.py: backward compatibility fix for _renderPM version <=0.98
rgbecker
parents: 2360
diff changeset
   203
            else:
320b823de224 renderPM.py: backward compatibility fix for _renderPM version <=0.98
rgbecker
parents: 2360
diff changeset
   204
                _renderPM.makeT1Font(fontName,f.face.findT1File(),f.encoding.vector,open_and_read)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   205
        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   206
            s1, s2 = map(str,sys.exc_info()[:2])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   207
            raise RenderPMError, "Can't setFont(%s) missing the T1 files?\nOriginally %s: %s" % (fontName,s1,s2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   208
        gs.setFont(fontName,fontSize)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   209
1899
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   210
def _convert2pilp(im):
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   211
    Image = _getImage()
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   212
    return im.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   213
2008
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   214
def _saveAsPICT(im,fn,fmt,transparent=None):
1899
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   215
    im = _convert2pilp(im)
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   216
    cols, rows = im.size
2016
a816a3167340 Comment problematic PICT code
rgbecker
parents: 2008
diff changeset
   217
    #s = _renderPM.pil2pict(cols,rows,im.tostring(),im.im.getpalette(),transparent is not None and Color2Hex(transparent) or -1)
a816a3167340 Comment problematic PICT code
rgbecker
parents: 2008
diff changeset
   218
    s = _renderPM.pil2pict(cols,rows,im.tostring(),im.im.getpalette())
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1901
diff changeset
   219
    if not hasattr(fn,'write'):
142332c87b5c Added markfilename call
rgbecker
parents: 1901
diff changeset
   220
        open(os.path.splitext(fn)[0]+'.'+string.lower(fmt),'wb').write(s)
2008
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   221
        if os.name=='mac':
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   222
            from reportlab.lib.utils import markfilename
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   223
            markfilename(fn,ext='PICT')
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1901
diff changeset
   224
    else:
142332c87b5c Added markfilename call
rgbecker
parents: 1901
diff changeset
   225
        fn.write(s)
1899
7a3ec5f97c9f Added support for pict
rgbecker
parents: 1895
diff changeset
   226
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   227
BEZIER_ARC_MAGIC = 0.5522847498     #constant for drawing circular arcs w/ Beziers
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   228
class PMCanvas:
1845
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   229
    def __init__(self,w,h,dpi=72,bg=0xffffff,configPIL=None):
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   230
        '''configPIL dict is passed to image save method'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   231
        scale = dpi/72.0
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   232
        w = int(w*scale+0.5)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   233
        h = int(h*scale+0.5)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   234
        self.__dict__['_gs'] = _renderPM.gstate(w,h,bg=bg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   235
        self.__dict__['_bg'] = bg
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   236
        self.__dict__['_baseCTM'] = (scale,0,0,scale,0,0)
1697
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   237
        self.__dict__['_clipPaths'] = []
1845
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   238
        self.__dict__['configPIL'] = configPIL
1993
26db6dee97fe Fix up resolution for TIFF
rgbecker
parents: 1924
diff changeset
   239
        self.__dict__['_dpi'] = dpi
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   240
        self.ctm = self._baseCTM
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   241
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   242
    def _drawTimeResize(self,w,h,bg=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   243
        if bg is None: bg = self._bg
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   244
        self._drawing.width, self._drawing.height = w, h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   245
        A = {'ctm':None, 'strokeWidth':None, 'strokeColor':None, 'lineCap':None, 'lineJoin':None, 'dashArray':None, 'fillColor':None}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   246
        gs = self._gs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   247
        fN,fS = gs.fontName, gs.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   248
        for k in A.keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   249
            A[k] = getattr(gs,k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   250
        del gs, self._gs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   251
        gs = self.__dict__['_gs'] = _renderPM.gstate(w,h,bg=bg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   252
        for k in A.keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   253
            setattr(self,k,A[k])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   254
        gs.setFont(fN,fS)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1581
diff changeset
   255
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   256
    def toPIL(self):
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   257
        im = _getImage().new('RGB', size=(self._gs.width, self._gs.height))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   258
        im.fromstring(self._gs.pixBuf)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   259
        return im
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   260
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   261
    def saveToFile(self,fn,fmt=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   262
        im = self.toPIL()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   263
        if fmt is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   264
            if type(fn) is not StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   265
                raise ValueError, "Invalid type '%s' for fn when fmt is None" % type(fn)
2401
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   266
            fmt = os.path.splitext(fn)[1]
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   267
            if fmt.startswith('.'): fmt = fmt[1:]
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   268
        configPIL = self.configPIL or {}
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   269
        fmt = string.upper(fmt)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   270
        if fmt in ['GIF']:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   271
            im = _convert2pilp(im)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   272
        elif fmt in ['PCT','PICT']:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   273
            return _saveAsPICT(im,fn,fmt,transparent=configPIL.get('transparent',None))
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   274
        elif fmt in ['PNG','TIFF','BMP', 'PPM', 'TIF']:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   275
            if fmt=='TIF': fmt = 'TIFF'
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   276
            if fmt=='PNG':
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   277
                try:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   278
                    from PIL import PngImagePlugin
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   279
                except ImportError:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   280
                    import PngImagePlugin
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   281
            elif fmt=='BMP':
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   282
                try:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   283
                    from PIL import BmpImagePlugin
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   284
                except ImportError:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   285
                    import BmpImagePlugin
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   286
        elif fmt in ('JPG','JPEG'):
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   287
            fmt = 'JPEG'
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   288
        else:
2401
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   289
            raise RenderPMError,"Unknown image kind %s" % fmt
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   290
        if fmt=='TIFF':
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   291
            tc = configPIL.get('transparent',None)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   292
            if tc:
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   293
                from PIL import ImageChops, Image
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   294
                T = 768*[0]
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   295
                for o, c in zip((0,256,512), tc.bitmap_rgb()):
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   296
                    T[o+c] = 255
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   297
                #if type(fn) is type(''): ImageChops.invert(im.point(T).convert('L').point(255*[0]+[255])).save(fn+'_mask.gif','GIF')
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   298
                im = Image.merge('RGBA', im.split()+(ImageChops.invert(im.point(T).convert('L').point(255*[0]+[255])),))
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   299
                #if type(fn) is type(''): im.save(fn+'_masked.gif','GIF')
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   300
            for a,d in ('resolution',self._dpi),('resolution unit','inch'):
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   301
                configPIL[a] = configPIL.get(a,d)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   302
        apply(im.save,(fn,fmt),configPIL)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   303
        if not hasattr(fn,'write') and os.name=='mac':
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   304
            from reportlab.lib.utils import markfilename
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   305
            markfilename(fn,ext=fmt)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   306
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   307
    def saveToString(self,fmt='GIF'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   308
        s = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   309
        self.saveToFile(s,fmt=fmt)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   310
        return s.getvalue()
939
cebd2d1df445 Improvements to testshapse.py
rgbecker
parents: 903
diff changeset
   311
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   312
    def _saveToBMP(self,f):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   313
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   314
        Niki Spahiev, <niki@vintech.bg>, asserts that this is a respectable way to get BMP without PIL
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   315
        f is a file like object to which the BMP is written
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   316
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   317
        import struct
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   318
        gs = self._gs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   319
        pix, width, height = gs.pixBuf, gs.width, gs.height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   320
        f.write(struct.pack('=2sLLLLLLhh24x','BM',len(pix)+54,0,54,40,width,height,1,24))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   321
        rowb = width * 3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   322
        for o in range(len(pix),0,-rowb):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   323
            f.write(pix[o-rowb:o])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   324
        f.write( '\0' * 14 )
1418
d76a36ebfa52 Added useful method from Niki Spahiev
rgbecker
parents: 1327
diff changeset
   325
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   326
    def setFont(self,fontName,fontSize,leading=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   327
        _setFont(self._gs,fontName,fontSize)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   328
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   329
    def __setattr__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   330
        setattr(self._gs,name,value)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   331
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   332
    def __getattr__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   333
        return getattr(self._gs,name)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   334
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   335
    def fillstrokepath(self,stroke=1,fill=1):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   336
        if fill: self.pathFill()
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   337
        if stroke: self.pathStroke()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   338
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   339
    def _bezierArcSegmentCCW(self, cx,cy, rx,ry, theta0, theta1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   340
        """compute the control points for a bezier arc with theta1-theta0 <= 90.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   341
        Points are computed for an arc with angle theta increasing in the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   342
        counter-clockwise (CCW) direction.  returns a tuple with starting point
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   343
        and 3 control points of a cubic bezier curve for the curvto opertator"""
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   344
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   345
        # Requires theta1 - theta0 <= 90 for a good approximation
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   346
        assert abs(theta1 - theta0) <= 90
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   347
        cos0 = cos(pi*theta0/180.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   348
        sin0 = sin(pi*theta0/180.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   349
        x0 = cx + rx*cos0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   350
        y0 = cy + ry*sin0
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   351
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   352
        cos1 = cos(pi*theta1/180.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   353
        sin1 = sin(pi*theta1/180.0)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   354
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   355
        x3 = cx + rx*cos1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   356
        y3 = cy + ry*sin1
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   357
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   358
        dx1 = -rx * sin0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   359
        dy1 = ry * cos0
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   360
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   361
        #from pdfgeom
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   362
        halfAng = pi*(theta1-theta0)/(2.0 * 180.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   363
        k = abs(4.0 / 3.0 * (1.0 - cos(halfAng) ) /(sin(halfAng)) )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   364
        x1 = x0 + dx1 * k
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   365
        y1 = y0 + dy1 * k
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   366
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   367
        dx2 = -rx * sin1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   368
        dy2 = ry * cos1
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   369
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   370
        x2 = x3 - dx2 * k
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   371
        y2 = y3 - dy2 * k
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   372
        return ((x0,y0), ((x1,y1), (x2,y2), (x3,y3)) )
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   373
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   374
    def bezierArcCCW(self, cx,cy, rx,ry, theta0, theta1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   375
        """return a set of control points for Bezier approximation to an arc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   376
        with angle increasing counter clockwise. No requirement on |theta1-theta0| <= 90
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   377
        However, it must be true that theta1-theta0 > 0."""
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   378
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   379
        # I believe this is also clockwise
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   380
        # pretty much just like Robert Kern's pdfgeom.BezierArc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   381
        angularExtent = theta1 - theta0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   382
        # break down the arc into fragments of <=90 degrees
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   383
        if abs(angularExtent) <= 90.0:  # we just need one fragment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   384
            angleList = [(theta0,theta1)]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   385
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   386
            Nfrag = int( ceil( abs(angularExtent)/90.) )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   387
            fragAngle = float(angularExtent)/ Nfrag  # this could be negative
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   388
            angleList = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   389
            for ii in range(Nfrag):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   390
                a = theta0 + ii * fragAngle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   391
                b = a + fragAngle # hmm.. is I wonder if this is precise enought
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   392
                angleList.append((a,b))
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   393
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   394
        ctrlpts = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   395
        for (a,b) in angleList:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   396
            if not ctrlpts: # first time
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   397
                [(x0,y0), pts] = self._bezierArcSegmentCCW(cx,cy, rx,ry, a,b)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   398
                ctrlpts.append(pts)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   399
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   400
                [(tmpx,tmpy), pts] = self._bezierArcSegmentCCW(cx,cy, rx,ry, a,b)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   401
                ctrlpts.append(pts)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   402
        return ((x0,y0), ctrlpts)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   403
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   404
    def addEllipsoidalArc(self, cx,cy, rx, ry, ang1, ang2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   405
        """adds an ellisesoidal arc segment to a path, with an ellipse centered
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   406
        on cx,cy and with radii (major & minor axes) rx and ry.  The arc is
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   407
        drawn in the CCW direction.  Requires: (ang2-ang1) > 0"""
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   408
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   409
        ((x0,y0), ctrlpts) = self.bezierArcCCW(cx,cy, rx,ry,ang1,ang2)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   410
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   411
        self.lineTo(x0,y0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   412
        for ((x1,y1), (x2,y2),(x3,y3)) in ctrlpts:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   413
            self.curveTo(x1,y1,x2,y2,x3,y3)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   414
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   415
    def drawCentredString(self, x, y, text, text_anchor='middle'):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   416
        if self.fillColor is not None:
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2402
diff changeset
   417
            textLen = stringWidth(text, self.fontName,self.fontSize)
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   418
            if text_anchor=='end':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   419
                x -= textLen
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   420
            elif text_anchor=='middle':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   421
                x -= textLen/2.
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   422
            self.drawString(x,y,text)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   423
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   424
    def drawRightString(self, text, x, y):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   425
        self.drawCentredString(text,x,y,text_anchor='end')
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   426
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   427
    def line(self,x1,y1,x2,y2):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   428
        if self.strokeColor is not None:
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   429
            self.pathBegin()
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   430
            self.moveTo(x1,y1)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   431
            self.lineTo(x2,y2)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   432
            self.pathStroke()
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   433
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   434
    def rect(self,x,y,width,height,stroke=1,fill=1):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   435
        self.pathBegin()
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   436
        self.moveTo(x, y)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   437
        self.lineTo(x+width, y)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   438
        self.lineTo(x+width, y + height)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   439
        self.lineTo(x, y + height)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   440
        self.pathClose()
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   441
        self.fillstrokepath(stroke=stroke,fill=fill)
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   442
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   443
    def roundRect(self, x, y, width, height, rx,ry):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   444
        """rect(self, x, y, width, height, rx,ry):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   445
        Draw a rectangle if rx or rx and ry are specified the corners are
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   446
        rounded with ellipsoidal arcs determined by rx and ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   447
        (drawn in the counter-clockwise direction)"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   448
        if rx==0: rx = ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   449
        if ry==0: ry = rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   450
        x2 = x + width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   451
        y2 = y + height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   452
        self.pathBegin()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   453
        self.moveTo(x+rx,y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   454
        self.addEllipsoidalArc(x2-rx, y+ry, rx, ry, 270, 360 )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   455
        self.addEllipsoidalArc(x2-rx, y2-ry, rx, ry, 0, 90)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   456
        self.addEllipsoidalArc(x+rx, y2-ry, rx, ry, 90, 180)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   457
        self.addEllipsoidalArc(x+rx, y+ry, rx, ry, 180,  270)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   458
        self.pathClose()
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   459
        self.fillstrokepath()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   460
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   461
    def circle(self, cx, cy, r):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   462
        "add closed path circle with center cx,cy and axes r: counter-clockwise orientation"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   463
        self.ellipse(cx,cy,r,r)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   464
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   465
    def ellipse(self, cx,cy,rx,ry):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   466
        """add closed path ellipse with center cx,cy and axes rx,ry: counter-clockwise orientation
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   467
        (remember y-axis increases downward) """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   468
        self.pathBegin()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   469
        # first segment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   470
        x0 = cx + rx   # (x0,y0) start pt
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   471
        y0 = cy
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
   472
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   473
        x3 = cx        # (x3,y3) end pt of arc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   474
        y3 = cy-ry
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   475
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   476
        x1 = cx+rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   477
        y1 = cy-ry*BEZIER_ARC_MAGIC
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   478
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   479
        x2 = x3 + rx*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   480
        y2 = y3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   481
        self.moveTo(x0, y0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   482
        self.curveTo(x1,y1,x2,y2,x3,y3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   483
        # next segment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   484
        x0 = x3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   485
        y0 = y3
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
   486
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   487
        x3 = cx-rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   488
        y3 = cy
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   489
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   490
        x1 = cx-rx*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   491
        y1 = cy-ry
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   492
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   493
        x2 = x3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   494
        y2 = cy- ry*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   495
        self.curveTo(x1,y1,x2,y2,x3,y3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   496
        # next segment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   497
        x0 = x3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   498
        y0 = y3
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
   499
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   500
        x3 = cx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   501
        y3 = cy+ry
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   502
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   503
        x1 = cx-rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   504
        y1 = cy+ry*BEZIER_ARC_MAGIC
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   505
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   506
        x2 = cx -rx*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   507
        y2 = cy+ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   508
        self.curveTo(x1,y1,x2,y2,x3,y3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   509
        #last segment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   510
        x0 = x3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   511
        y0 = y3
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1680
diff changeset
   512
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   513
        x3 = cx+rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   514
        y3 = cy
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   515
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   516
        x1 = cx+rx*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   517
        y1 = cy+ry
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   518
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   519
        x2 = cx+rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   520
        y2 = cy+ry*BEZIER_ARC_MAGIC
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   521
        self.curveTo(x1,y1,x2,y2,x3,y3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   522
        self.pathClose()
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   523
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   524
    def saveState(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   525
        '''do nothing for compatibility'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   526
        pass
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   527
2401
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   528
    def setFillColor(self,aColor):
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   529
        self.fillColor = Color2Hex(aColor)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   530
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   531
    def setStrokeColor(self,aColor):
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   532
        self.strokeColor = Color2Hex(aColor)
cd68d7a84d05 CanvasAdapter related changes
rgbecker
parents: 2386
diff changeset
   533
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   534
    restoreState = saveState
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   535
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   536
    # compatibility routines
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   537
    def setLineCap(self,cap):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   538
        self.lineCap = cap
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   539
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   540
    def setLineWidth(self,width):
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   541
        self.strokeWidth = width
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   542
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   543
def drawToPMCanvas(d, dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2405
diff changeset
   544
    d = renderScaledDrawing(d)
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2401
diff changeset
   545
    c = PMCanvas(d.width, d.height, dpi=dpi, bg=bg, configPIL=configPIL)
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   546
    draw(d, c, 0, 0, showBoundary=showBoundary)
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   547
    return c
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   548
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   549
def drawToPIL(d, dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   550
    return drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL, showBoundary=showBoundary).toPIL()
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   551
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   552
def drawToPILP(d, dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   553
    Image = _getImage()
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   554
    im = drawToPIL(d, dpi=dpi, bg=bg, configPIL=configPIL, showBoundary=showBoundary)
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   555
    return im.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   556
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   557
def drawToFile(d,fn,fmt='GIF', dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
1845
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   558
    '''create a pixmap and draw drawing, d to it then save as a file
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   559
    configPIL dict is passed to image save method'''
1895
fcd562e2a8ed Added some entry points for PIL workers
rgbecker
parents: 1864
diff changeset
   560
    c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL, showBoundary=showBoundary)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   561
    c.saveToFile(fn,fmt)
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   562
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2357
diff changeset
   563
def drawToString(d,fmt='GIF', dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   564
    s = getStringIO()
1845
d8f0814302d8 Added argument to get at PIL save
rgbecker
parents: 1697
diff changeset
   565
    drawToFile(d,s,fmt=fmt, dpi=dpi, bg=bg, configPIL=configPIL)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   566
    return s.getvalue()
944
3c36aba69e92 Added drawToString
rgbecker
parents: 939
diff changeset
   567
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   568
save = drawToFile
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   569
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   570
def test():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   571
    def ext(x):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   572
        if x=='tiff': x='tif'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   573
        return x
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   574
    #grab all drawings from the test module and write out.
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   575
    #make a page of links in HTML to assist viewing.
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   576
    import os
2124
09a02e040064 Attempt ttfonts in testing
rgbecker
parents: 2052
diff changeset
   577
    from reportlab.graphics import testshapes
09a02e040064 Attempt ttfonts in testing
rgbecker
parents: 2052
diff changeset
   578
    getAllTestDrawings = testshapes.getAllTestDrawings
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   579
    drawings = []
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   580
    if not os.path.isdir('pmout'):
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   581
        os.mkdir('pmout')
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   582
    htmlTop = """<html><head><title>renderPM output results</title></head>
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   583
    <body>
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   584
    <h1>renderPM results of output</h1>
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   585
    """
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   586
    htmlBottom = """</body>
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   587
    </html>
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   588
    """
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   589
    html = [htmlTop]
2126
cb38a911f83c Added better test example
rgbecker
parents: 2124
diff changeset
   590
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   591
    i = 0
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   592
    #print in a loop, with their doc strings
2184
928f35c56a19 Attempt to control ttf usage
rgbecker
parents: 2171
diff changeset
   593
    for (drawing, docstring, name) in getAllTestDrawings(doTTF=hasattr(_renderPM,'ft_get_face')):
2000
7556b5ee5c80 Allow for tiff transparency
rgbecker
parents: 1993
diff changeset
   594
        fnRoot = 'renderPM%d' % i
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   595
        if 1 or i==10:
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   596
            w = int(drawing.width)
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   597
            h = int(drawing.height)
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   598
            html.append('<hr><h2>Drawing %s %d</h2>\n<pre>%s</pre>' % (name, i, docstring))
899
4d9f637a37d4 Moved from rlextra/graphics/Csrc/renderPM
rgbecker
parents:
diff changeset
   599
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   600
            for k in ['gif','tiff', 'png', 'jpg', 'pct']:
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   601
                if k in ['gif','png','jpg','pct']:
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   602
                    html.append('<p>%s format</p>\n' % string.upper(k))
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   603
                try:
2000
7556b5ee5c80 Allow for tiff transparency
rgbecker
parents: 1993
diff changeset
   604
                    filename = '%s.%s' % (fnRoot, ext(k))
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   605
                    fullpath = os.path.join('pmout', filename)
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   606
                    if os.path.isfile(fullpath):
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   607
                        os.remove(fullpath)
2008
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   608
                    if k=='pct':
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   609
                        from reportlab.lib.colors import white
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   610
                        drawToFile(drawing,fullpath,fmt=k,configPIL={'transparent':white})
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   611
                    else:
f643cd8fee79 Test PICT transparency & move to better markfilename
rgbecker
parents: 2000
diff changeset
   612
                        drawToFile(drawing,fullpath,fmt=k)
2000
7556b5ee5c80 Allow for tiff transparency
rgbecker
parents: 1993
diff changeset
   613
                    if k in ['gif','png','jpg']:
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   614
                        html.append('<img src="%s" border="1"><br>\n' % filename)
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   615
                    print 'wrote',fullpath
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   616
                except AttributeError:
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   617
                    print 'Problem drawing %s file'%k
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   618
                    raise
2206
10d9221d28dd Allow for no eps preview in tests
rgbecker
parents: 2184
diff changeset
   619
        if os.environ.get('RL_NOEPSPREVIEW','0')=='1': drawing.__dict__['preview'] = 0
2000
7556b5ee5c80 Allow for tiff transparency
rgbecker
parents: 1993
diff changeset
   620
        drawing.save(formats=['eps','pdf'],outDir='pmout',fnRoot=fnRoot)
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   621
        i = i + 1
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   622
        #if i==10: break
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   623
    html.append(htmlBottom)
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   624
    htmlFileName = os.path.join('pmout', 'index.html')
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   625
    open(htmlFileName, 'w').writelines(html)
2052
96b3fec87ba0 Now uses markfilename from lib.utils for setting the filetype for the
johnprecedo
parents: 2016
diff changeset
   626
    if sys.platform=='mac':
96b3fec87ba0 Now uses markfilename from lib.utils for setting the filetype for the
johnprecedo
parents: 2016
diff changeset
   627
        from reportlab.lib.utils import markfilename
96b3fec87ba0 Now uses markfilename from lib.utils for setting the filetype for the
johnprecedo
parents: 2016
diff changeset
   628
        markfilename(htmlFileName,ext='HTML')
1924
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   629
    print 'wrote %s' % htmlFileName
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   630
f73e361db889 Fix up for import of test
rgbecker
parents: 1921
diff changeset
   631
if __name__=='__main__':
1697
f98c5d064e6e Added EmptyClipPath and handling
rgbecker
parents: 1683
diff changeset
   632
    test()