reportlab/graphics/renderSVG.py
author rgbecker
Wed, 15 Mar 2006 16:47:27 +0000
changeset 2574 d81949596070
parent 2553 a880f43d10bd
permissions -rw-r--r--
reportlab: changes to accomodate barcodes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
     1
"""An experimental SVG renderer for the ReportLab graphics framework.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
     2
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
     3
This will create SVG code from the ReportLab Graphics API (RLG).
1670
d2b387acfda8 Added comment on svglib.
dinu_gherman
parents: 1665
diff changeset
     4
To read existing SVG code and convert it into ReportLab graphics
d2b387acfda8 Added comment on svglib.
dinu_gherman
parents: 1665
diff changeset
     5
objects download the svglib module here:
d2b387acfda8 Added comment on svglib.
dinu_gherman
parents: 1665
diff changeset
     6
d2b387acfda8 Added comment on svglib.
dinu_gherman
parents: 1665
diff changeset
     7
  http://python.net/~gherman/#svglib
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
     8
"""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
     9
1744
be3f93b1d001 Whoops, wrong stringIO
andy_robinson
parents: 1743
diff changeset
    10
import math, string, types, sys, os
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    11
from types import StringType
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    12
from operator import getitem
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    13
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    14
from reportlab.pdfbase.pdfmetrics import stringWidth # for font info
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    15
from reportlab.lib.utils import fp_str
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    16
from reportlab.lib.colors import black
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2360
diff changeset
    17
from reportlab.graphics.renderbase import StateTracker, getStateDelta, Renderer, renderScaledDrawing
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    18
from reportlab.graphics.shapes import STATE_DEFAULTS, Path, UserNode
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    19
from reportlab.graphics.shapes import * # (only for test0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    20
from reportlab import rl_config
1744
be3f93b1d001 Whoops, wrong stringIO
andy_robinson
parents: 1743
diff changeset
    21
from reportlab.lib.utils import getStringIO
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    22
1617
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
    23
from xml.dom import getDOMImplementation
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    24
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    25
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    26
### some constants ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    27
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    28
sin = math.sin
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    29
cos = math.cos
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    30
pi = math.pi
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    31
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    32
LINE_STYLES = 'stroke-width stroke-linecap stroke fill stroke-dasharray'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    33
TEXT_STYLES = 'font-family font-size'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    34
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    35
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    36
### top-level user function ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    37
1743
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    38
def drawToString(d, showBoundary=rl_config.showBoundary):
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    39
    "Returns a SVG as a string in memory, without touching the disk"
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    40
    s = getStringIO()
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    41
    drawToFile(d, s, showBoundary=showBoundary)
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    42
    return s.getvalue()
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    43
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    44
def drawToFile(d, fn, showBoundary=rl_config.showBoundary):
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2360
diff changeset
    45
    d = renderScaledDrawing(d)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    46
    c = SVGCanvas((d.width, d.height))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    47
    draw(d, c, 0, 0, showBoundary=showBoundary)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    48
    c.save(fn)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    49
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    50
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    51
def draw(drawing, canvas, x=0, y=0, showBoundary=rl_config.showBoundary):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    52
    """As it says."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    53
    r = _SVGRenderer()
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
    54
    r.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    55
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    56
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    57
### helper functions ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    58
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    59
def _pointsFromList(L):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    60
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    61
    given a list of coordinates [x0, y0, x1, y1....]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    62
    produce a list of points [(x0,y0), (y1,y0),....]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    63
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    64
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    65
    P=[]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    66
    for i in range(0,len(L), 2):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    67
        P.append((L[i], L[i+1]))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    68
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    69
    return P
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    70
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    71
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    72
def transformNode(doc, newTag, node=None, **attrDict):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    73
    """Transform a DOM node into new node and copy selected attributes.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    74
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    75
    Creates a new DOM node with tag name 'newTag' for document 'doc'
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    76
    and copies selected attributes from an existing 'node' as provided
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    77
    in 'attrDict'. The source 'node' can be None. Attribute values will
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    78
    be converted to strings.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    79
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    80
    E.g.
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    81
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    82
        n = transformNode(doc, "node1", x="0", y="1")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    83
        -> DOM node for <node1 x="0" y="1"/>
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    84
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    85
        n = transformNode(doc, "node1", x=0, y=1+1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    86
        -> DOM node for <node1 x="0" y="2"/>
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    87
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    88
        n = transformNode(doc, "node1", node0, x="x0", y="x0", zoo=bar())
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    89
        -> DOM node for <node1 x="[node0.x0]" y="[node0.y0]" zoo="[bar()]"/>
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    90
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    91
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    92
    newNode = doc.createElement(newTag)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    93
    for newAttr, attr in attrDict.items():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    94
        sattr =  str(attr)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    95
        if not node:
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    96
            newNode.setAttribute(newAttr, sattr)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    97
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    98
            attrVal = node.getAttribute(sattr)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    99
            newNode.setAttribute(newAttr, attrVal or sattr)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   100
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   101
    return newNode
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   102
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   103
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   104
### classes ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   105
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   106
class SVGCanvas:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   107
    def __init__(self, size=(300,300)):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   108
        self.verbose = 0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   109
        self.width, self.height = self.size = size
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   110
        # self.height = size[1]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   111
        self.code = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   112
        self.style = {}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   113
        self.path = ''
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   114
        self._strokeColor = self._fillColor = self._lineWidth = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   115
            self._font = self._fontSize = self._lineCap = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   116
            self._lineJoin = self._color = None
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   117
1642
97651958fbe5 Load minidom specifically (pleasing Jython).
dinu_gherman
parents: 1617
diff changeset
   118
        implementation = getDOMImplementation('minidom')
1617
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   119
        self.doc = implementation.createDocument(None, "svg", None)
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   120
        self.svg = self.doc.documentElement
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   121
        self.svg.setAttribute("width", str(size[0]))
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   122
        self.svg.setAttribute("height", str(self.height))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   123
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   124
        title = self.doc.createElement('title')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   125
        text = self.doc.createTextNode('...')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   126
        title.appendChild(text)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   127
        self.svg.appendChild(title)
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   128
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   129
        desc = self.doc.createElement('desc')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   130
        text = self.doc.createTextNode('...')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   131
        desc.appendChild(text)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   132
        self.svg.appendChild(desc)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   133
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   134
        self.setFont(STATE_DEFAULTS['fontName'], STATE_DEFAULTS['fontSize'])
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   135
        self.setStrokeColor(STATE_DEFAULTS['strokeColor'])
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   136
        self.setLineCap(2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   137
        self.setLineJoin(0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   138
        self.setLineWidth(1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   139
1661
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   140
        # Add a rectangular clipping path identical to view area.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   141
        clipPath = transformNode(self.doc, "clipPath", id="clip")
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   142
        clipRect = transformNode(self.doc, "rect", x=0, y=0,
1661
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   143
            width=self.width, height=self.height)
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   144
        clipPath.appendChild(clipRect)
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   145
        self.svg.appendChild(clipPath)
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   146
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   147
        self.groupTree = transformNode(self.doc, "g",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   148
            id="group",
1661
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   149
            transform="scale(1,-1) translate(0,-%d)" % self.height,
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   150
            style="clip-path: url(#clip)")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   151
        self.svg.appendChild(self.groupTree)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   152
        self.currGroup = self.groupTree
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   153
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   154
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   155
    def save(self, f=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   156
        if type(f) is StringType:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   157
            file = open(f, 'w')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   158
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   159
            file = f
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   160
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   161
        file.write("""\
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   162
<?xml version="1.0" encoding="iso-8859-1"?>
1644
05172368439f Added newline before svg document element.
dinu_gherman
parents: 1642
diff changeset
   163
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd" >\n""")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   164
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   165
        # use = self.doc.createElement('use')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   166
        # use.setAttribute("xlink:href", "#group")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   167
        # use.setAttribute("transform", "scale(1, -1)")
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   168
        # self.svg.appendChild(use)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   169
1617
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   170
        result = self.svg.toprettyxml(indent="    ")
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   171
        file.write(result)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   172
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   173
        if file is not f:
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   174
            file.close()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   175
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   176
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   177
    ### helpers ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   178
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   179
    def NOTUSED_stringWidth(self, s, font=None, fontSize=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   180
        """Return the logical width of the string if it were drawn
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   181
        in the current font (defaults to self.font).
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   182
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   183
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   184
        font = font or self._font
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   185
        fontSize = fontSize or self._fontSize
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   186
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   187
        return stringWidth(s, font, fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   188
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   189
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   190
    def _formatStyle(self, include=''):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   191
        str = ''
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   192
        include = string.split(include)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   193
        keys = self.style.keys()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   194
        if include:
1611
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   195
            #2.1-safe version of the line below follows:
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   196
            #keys = filter(lambda k: k in include, keys)
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   197
            tmp = []
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   198
            for word in keys:
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   199
                if word in include:
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   200
                    tmp.append(word)
f3b184741e4c 2.1 compatible
andy_robinson
parents: 1607
diff changeset
   201
            keys = tmp
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   202
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   203
        items = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   204
        for k in keys:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   205
            items.append((k, self.style[k]))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   206
        items = map(lambda i: "%s: %s"%(i[0], i[1]), items)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   207
        str = string.join(items, '; ') + ';'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   208
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   209
        return str
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   210
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   211
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   212
    def _escape(self, s):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   213
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   214
        return a copy of string s with special characters in postscript strings
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   215
        escaped with backslashes.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   216
        Have not handled characters that are converted normally in python strings
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   217
        i.e. \n -> newline
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   218
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   219
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   220
        str = string.replace(s, chr(0x5C), r'\\' )
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   221
        str = string.replace(str, '(', '\(' )
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   222
        str = string.replace(str, ')', '\)')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   223
        return str
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   224
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   225
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   226
    def _genArcCode(self, x1, y1, x2, y2, startAng, extent):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   227
        """Calculate the path for an arc inscribed in rectangle defined
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   228
        by (x1,y1),(x2,y2)."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   229
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   230
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   231
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   232
        #calculate semi-minor and semi-major axes of ellipse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   233
        xScale = abs((x2-x1)/2.0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   234
        yScale = abs((y2-y1)/2.0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   235
        #calculate centre of ellipse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   236
        x, y = (x1+x2)/2.0, (y1+y2)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   237
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   238
        codeline = 'matrix currentmatrix %s %s translate %s %s scale 0 0 1 %s %s %s setmatrix'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   239
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   240
        if extent >= 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   241
            arc='arc'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   242
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   243
            arc='arcn'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   244
        data = (x,y, xScale, yScale, startAng, startAng+extent, arc)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   245
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   246
        return codeline % data
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   247
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   248
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   249
    def _fillAndStroke(self, code, clip=0):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   250
        path = transformNode(self.doc, "path",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   251
            d=self.path, style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   252
        self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   253
        self.path = ''
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   254
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   255
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   256
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   257
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   258
        if self._fillColor or self._strokeColor or clip:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   259
            self.code.extend(code)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   260
            if self._fillColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   261
                if self._strokeColor or clip:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   262
                    self.code.append("gsave")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   263
                self.setColor(self._fillColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   264
                self.code.append("eofill")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   265
                if self._strokeColor or clip:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   266
                    self.code.append("grestore")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   267
            if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   268
                if clip: self.code.append("gsave")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   269
                self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   270
                self.code.append("stroke")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   271
                if clip: self.code.append("grestore")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   272
            if clip:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   273
                self.code.append("clip")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   274
                self.code.append("newpath")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   275
        """
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   276
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   277
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   278
    ### styles ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   279
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   280
    def setLineCap(self, v):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   281
        vals = {0:'butt', 1:'round', 2:'square'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   282
        if self._lineCap != v:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   283
            self._lineCap = v
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   284
            self.style['stroke-linecap'] = vals[v]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   285
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   286
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   287
    def setLineJoin(self, v):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   288
        vals = {0:'miter', 1:'round', 2:'bevel'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   289
        if self._lineJoin != v:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   290
            self._lineJoin = v
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   291
            self.style['stroke-linecap'] = vals[v]
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   292
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   293
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   294
    def setDash(self, array=[], phase=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   295
        """Two notations. Pass two numbers, or an array and phase."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   296
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   297
        join = string.join
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   298
        if type(array) in (types.IntType, types.FloatType):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   299
            self.style['stroke-dasharray'] = join(map(str, ([array, phase])), ', ')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   300
        elif type(array) in (types.ListType, types.TupleType) and len(array) > 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   301
            assert phase >= 0, "phase is a length in user space"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   302
            self.style['stroke-dasharray'] = join(map(str, (array+[phase])), ', ')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   303
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   304
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   305
    def setStrokeColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   306
        self._strokeColor = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   307
        self.setColor(color)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   308
        if color == None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   309
            self.style['stroke'] = 'none'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   310
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   311
            r, g, b = color.red, color.green, color.blue
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   312
            self.style['stroke'] = 'rgb(%d%%,%d%%,%d%%)' % (r*100, g*100, b*100)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   313
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   314
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   315
    def setColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   316
        if self._color != color:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   317
            self._color = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   318
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   319
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   320
    def setFillColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   321
        self._fillColor = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   322
        self.setColor(color)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   323
        if color == None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   324
            self.style['fill'] = 'none'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   325
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   326
            r, g, b = color.red, color.green, color.blue
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   327
            self.style['fill'] = 'rgb(%d%%,%d%%,%d%%)' % (r*100, g*100, b*100)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   328
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   329
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   330
    def setLineWidth(self, width):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   331
        if width != self._lineWidth:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   332
            self._lineWidth = width
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   333
            self.style['stroke-width'] = width
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   334
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   335
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   336
    def setFont(self, font, fontSize):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   337
        if self._font != font or self._fontSize != fontSize:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   338
            self._font, self._fontSize = (font, fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   339
            self.style['font-family'] = font
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   340
            self.style['font-size'] = fontSize
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   341
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   342
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   343
    ### shapes ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   344
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   345
    def rect(self, x1,y1, x2,y2, rx=8, ry=8):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   346
        "Draw a rectangle between x1,y1 and x2,y2."
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   347
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   348
        if self.verbose: print "+++ SVGCanvas.rect"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   349
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   350
        rect = transformNode(self.doc, "rect",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   351
            x=x1, y=y1, width=x2-x1, height=y2-y1,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   352
            style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   353
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   354
        self.currGroup.appendChild(rect)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   355
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   356
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   357
    def roundRect(self, x1,y1, x2,y2, rx=8, ry=8):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   358
        """Draw a rounded rectangle between x1,y1 and x2,y2.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   359
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   360
        Corners inset as ellipses with x-radius rx and y-radius ry.
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   361
        These should have x1<x2, y1<y2, rx>0, and ry>0.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   362
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   363
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   364
        rect = transformNode(self.doc, "rect",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   365
            x=x1, y=y1, width=x2-x1, height=y2-y1, rx=rx, ry=ry,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   366
            style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   367
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   368
        self.currGroup.appendChild(rect)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   369
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   370
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   371
    def drawString(self, s, x, y, angle=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   372
        if self.verbose: print "+++ SVGCanvas.drawString"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   373
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   374
        if self._fillColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   375
            self.setColor(self._fillColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   376
            s = self._escape(s)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   377
            st = self._formatStyle(TEXT_STYLES)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   378
            if angle != 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   379
               st = st + " rotate(%f %f %f);" % (angle, x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   380
            st = st + " fill: %s;" % self.style['fill']
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   381
            text = transformNode(self.doc, "text",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   382
                x=x, y=y, style=st,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   383
                transform="translate(0,%d) scale(1,-1)" % (2*y))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   384
            content = self.doc.createTextNode(s)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   385
            text.appendChild(content)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   386
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   387
            self.currGroup.appendChild(text)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   388
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   389
    def drawCentredString(self, s, x, y, angle=0,text_anchor='middle'):
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   390
        if self.verbose: print "+++ SVGCanvas.drawCentredString"
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   391
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   392
        if self._fillColor != None:
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   393
            if not text_anchor in ['start', 'inherited']:
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   394
                textLen = stringWidth(s,self._font,self._fontSize)
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   395
                if text_anchor=='end':
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   396
                    x -= textLen
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   397
                elif text_anchor=='middle':
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   398
                    x -= textLen/2.
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   399
                else:
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   400
                    raise ValueError, 'bad value for text_anchor ' + str(text_anchor)
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   401
        self.drawString(x,y,text,angle=angle)
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   402
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   403
    def drawRightString(self, text, x, y, angle=0):
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   404
        self.drawCentredString(text,x,y,angle=angle,text_anchor='end')
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   405
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   406
    def comment(self, data):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   407
        "Add a comment."
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   408
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   409
        comment = self.doc.createComment(data)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   410
        # self.currGroup.appendChild(comment)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   411
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   412
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   413
    def drawImage(self, image, x1, y1, x2=None, y2=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   414
        pass
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   415
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   416
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   417
    def line(self, x1, y1, x2, y2):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   418
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   419
            if 0: # something is wrong with line in my SVG viewer...
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   420
                line = transformNode(self.doc, "line",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   421
                    x=x1, y=y1, x2=x2, y2=y2,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   422
                    style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   423
                self.currGroup.appendChild(line)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   424
            path = transformNode(self.doc, "path",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   425
                d="M %f,%f L %f,%f Z" % (x1,y1,x2,y2),
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   426
                style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   427
            self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   428
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   429
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   430
    def ellipse(self, x1, y1, x2, y2):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   431
        """Draw an orthogonal ellipse inscribed within the rectangle x1,y1,x2,y2.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   432
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   433
        These should have x1<x2 and y1<y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   434
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   435
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   436
        ellipse = transformNode(self.doc, "ellipse",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   437
            cx=(x1+x2)/2.0, cy=(y1+y2)/2.0, rx=(x2-x1)/2.0, ry=(y2-y1)/2.0,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   438
            style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   439
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   440
        self.currGroup.appendChild(ellipse)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   441
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   442
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   443
    def circle(self, xc, yc, r):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   444
        circle = transformNode(self.doc, "circle",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   445
            cx=xc, cy=yc, r=r,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   446
            style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   447
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   448
        self.currGroup.appendChild(circle)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   449
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   450
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   451
    def drawCurve(self, x1, y1, x2, y2, x3, y3, x4, y4, closed=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   452
        pass
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   453
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   454
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   455
        codeline = '%s m %s curveto'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   456
        data = (fp_str(x1, y1), fp_str(x2, y2, x3, y3, x4, y4))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   457
        if self._fillColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   458
            self.setColor(self._fillColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   459
            self.code.append((codeline % data) + ' eofill')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   460
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   461
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   462
            self.code.append((codeline % data)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   463
                            + ((closed and ' closepath') or '')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   464
                            + ' stroke')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   465
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   466
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   467
    def drawArc(self, x1,y1, x2,y2, startAng=0, extent=360, fromcenter=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   468
        """Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   469
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   470
        Starting at startAng degrees and covering extent degrees. Angles
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   471
        start with 0 to the right (+x) and increase counter-clockwise.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   472
        These should have x1<x2 and y1<y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   473
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   474
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   475
        cx, cy = (x1+x2)/2.0, (y1+y2)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   476
        rx, ry = (x2-x1)/2.0, (y2-y1)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   477
        mx = rx * cos(startAng*pi/180) + cx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   478
        my = ry * sin(startAng*pi/180) + cy
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   479
        ax = rx * cos((startAng+extent)*pi/180) + cx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   480
        ay = ry * sin((startAng+extent)*pi/180) + cy
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   481
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   482
        str = ''
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   483
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   484
            str = str + "M %f, %f L %f, %f " % (cx, cy, ax, ay)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   485
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   486
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   487
            str = str + "A %f, %f %d %d %d %f, %f " % \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   488
              (rx, ry, 0, extent>=180, 0, mx, my)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   489
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   490
            str = str + "M %f, %f A %f, %f %d %d %d %f, %f Z " % \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   491
              (mx, my, rx, ry, 0, extent>=180, 0, mx, my)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   492
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   493
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   494
            str = str + "L %f, %f Z " % (cx, cy)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   495
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   496
        path = transformNode(self.doc, "path",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   497
            d=str, style=self._formatStyle())
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   498
        self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   499
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   500
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   501
    def polygon(self, points, closed=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   502
        assert len(points) >= 2, 'Polygon must have 2 or more points'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   503
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   504
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   505
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   506
            pairs = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   507
            for i in xrange(len(points)):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   508
                pairs.append("%f %f" % (points[i]))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   509
            pts = string.join(pairs, ', ')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   510
            polyline = transformNode(self.doc, "polygon",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   511
                points=pts, style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   512
            self.currGroup.appendChild(polyline)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   513
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   514
        # self._fillAndStroke(polyCode)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   515
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   516
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   517
    def lines(self, lineList, color=None, width=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   518
        # print "### lineList", lineList
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   519
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   520
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   521
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   522
            self._setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   523
            codeline = '%s m %s l stroke'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   524
            for line in lineList:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   525
                self.code.append(codeline % (fp_str(line[0]), fp_str(line[1])))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   526
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   527
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   528
    def polyLine(self, points):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   529
        assert len(points) >= 1, 'Polyline must have 1 or more points'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   530
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   531
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   532
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   533
            pairs = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   534
            for i in xrange(len(points)):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   535
                pairs.append("%f %f" % (points[i]))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   536
            pts = string.join(pairs, ', ')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   537
            polyline = transformNode(self.doc, "polyline",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   538
                points=pts, style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   539
            self.currGroup.appendChild(polyline)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   540
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   541
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   542
    ### groups ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   543
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   544
    def startGroup(self):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   545
        if self.verbose: print "+++ begin SVGCanvas.startGroup"
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   546
        currGroup, group = self.currGroup, transformNode(self.doc, "g", transform="")
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   547
        currGroup.appendChild(group)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   548
        self.currGroup = group
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   549
        if self.verbose: print "+++ end SVGCanvas.startGroup"
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   550
        return currGroup
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   551
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   552
    def endGroup(self,currGroup):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   553
        if self.verbose: print "+++ begin SVGCanvas.endGroup"
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   554
        self.currGroup = currGroup
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   555
        if self.verbose: print "+++ end SVGCanvas.endGroup"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   556
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   557
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   558
    def transform(self, a, b, c, d, e, f):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   559
        if self.verbose: print "!!! begin SVGCanvas.transform", a, b, c, d, e, f
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   560
        tr = self.currGroup.getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   561
        t = 'matrix(%f, %f, %f, %f, %f, %f)' % (a,b,c,d,e,f)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   562
        if (a, b, c, d, e, f) != (1, 0, 0, 1, 0, 0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   563
            self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   564
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   565
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   566
    def translate(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   567
        # probably never used
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   568
        print "!!! begin SVGCanvas.translate"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   569
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   570
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   571
        tr = self.currGroup.getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   572
        t = 'translate(%f, %f)' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   573
        self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   574
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   575
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   576
    def scale(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   577
        # probably never used
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   578
        print "!!! begin SVGCanvas.scale"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   579
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   580
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   581
        tr = self.groups[-1].getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   582
        t = 'scale(%f, %f)' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   583
        self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   584
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   585
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   586
    ### paths ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   587
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   588
    def moveTo(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   589
        self.path = self.path + 'M %f %f ' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   590
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   591
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   592
    def lineTo(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   593
        self.path = self.path + 'L %f %f ' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   594
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   595
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   596
    def curveTo(self, x1, y1, x2, y2, x3, y3):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   597
        self.path = self.path + 'C %f %f %f %f %f %f ' % (x1, y1, x2, y2, x3, y3)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   598
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   599
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   600
    def closePath(self):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   601
        self.path = self.path + 'Z '
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   602
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   603
    def saveState(self):
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   604
        pass
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   605
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   606
    def restoreState(self):
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   607
        pass
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   608
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   609
class _SVGRenderer(Renderer):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   610
    """This draws onto an SVG document.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   611
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   612
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   613
    def __init__(self):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   614
        self._tracker = StateTracker()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   615
        self.verbose = 0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   616
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   617
    def drawNode(self, node):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   618
        """This is the recursive method called for each node in the tree.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   619
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   620
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   621
        if self.verbose: print "### begin _SVGRenderer.drawNode"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   622
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   623
        self._canvas.comment('begin node %s'%`node`)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   624
        color = self._canvas._color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   625
        if not (isinstance(node, Path) and node.isClipPath):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   626
            pass # self._canvas.saveState()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   627
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   628
        #apply state changes
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   629
        deltas = getStateDelta(node)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   630
        self._tracker.push(deltas)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   631
        self.applyStateChanges(deltas, {})
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   632
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   633
        #draw the object, or recurse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   634
        self.drawNodeDispatcher(node)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   635
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   636
        rDeltas = self._tracker.pop()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   637
        if not (isinstance(node, Path) and node.isClipPath):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   638
            pass # self._canvas.restoreState()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   639
        self._canvas.comment('end node %s'%`node`)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   640
        self._canvas._color = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   641
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   642
        #restore things we might have lost (without actually doing anything).
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   643
        for k, v in rDeltas.items():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   644
            if self._restores.has_key(k):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   645
                setattr(self._canvas,self._restores[k],v)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   646
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   647
        if self.verbose: print "### end _SVGRenderer.drawNode"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   648
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   649
    _restores = {'strokeColor':'_strokeColor','strokeWidth': '_lineWidth','strokeLineCap':'_lineCap',
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   650
                'strokeLineJoin':'_lineJoin','fillColor':'_fillColor','fontName':'_font',
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   651
                'fontSize':'_fontSize'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   652
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   653
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   654
    def drawGroup(self, group):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   655
        if self.verbose: print "### begin _SVGRenderer.drawGroup"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   656
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   657
        currGroup = self._canvas.startGroup()
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   658
        a, b, c, d, e, f = self._tracker.getCTM()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   659
        for childNode in group.getContents():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   660
            if isinstance(childNode, UserNode):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   661
                node2 = childNode.provideNode()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   662
            else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   663
                node2 = childNode
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   664
            self.drawNode(node2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   665
        self._canvas.transform(a, b, c, d, e, f)
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   666
        self._canvas.endGroup(currGroup)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   667
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   668
        if self.verbose: print "### end _SVGRenderer.drawGroup"
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   669
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   670
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   671
    def drawRect(self, rect):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   672
        if rect.rx == rect.ry == 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   673
            #plain old rectangle
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   674
            self._canvas.rect(
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   675
                    rect.x, rect.y,
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   676
                    rect.x+rect.width, rect.y+rect.height)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   677
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   678
            #cheat and assume ry = rx; better to generalize
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   679
            #pdfgen roundRect function.  TODO
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   680
            self._canvas.roundRect(
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   681
                    rect.x, rect.y,
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   682
                    rect.x+rect.width, rect.y+rect.height,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   683
                    rect.rx, rect.ry
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   684
                    )
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   685
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   686
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   687
    def drawString(self, stringObj):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   688
        if self._canvas._fillColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   689
            S = self._tracker.getState()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   690
            text_anchor, x, y, text = S['textAnchor'], stringObj.x, stringObj.y, stringObj.text
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   691
            if not text_anchor in ['start', 'inherited']:
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   692
                font, fontSize = S['fontName'], S['fontSize']
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   693
                textLen = stringWidth(text, font,fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   694
                if text_anchor=='end':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   695
                    x = x-textLen
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   696
                elif text_anchor=='middle':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   697
                    x = x - textLen/2
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   698
                else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   699
                    raise ValueError, 'bad value for text_anchor ' + str(text_anchor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   700
            self._canvas.drawString(text,x,y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   701
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   702
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   703
    def drawLine(self, line):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   704
        if self._canvas._strokeColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   705
            self._canvas.line(line.x1, line.y1, line.x2, line.y2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   706
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   707
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   708
    def drawCircle(self, circle):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   709
        self._canvas.circle( circle.cx, circle.cy, circle.r)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   710
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   711
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   712
    def drawWedge(self, wedge):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   713
        centerx, centery, radius, startangledegrees, endangledegrees = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   714
         wedge.centerx, wedge.centery, wedge.radius, wedge.startangledegrees, wedge.endangledegrees
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   715
        yradius = wedge.yradius or wedge.radius
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   716
        (x1, y1) = (centerx-radius, centery-yradius)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   717
        (x2, y2) = (centerx+radius, centery+yradius)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   718
        extent = endangledegrees - startangledegrees
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   719
        self._canvas.drawArc(x1, y1, x2, y2, startangledegrees, extent, fromcenter=1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   720
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   721
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   722
    def drawPolyLine(self, p):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   723
        if self._canvas._strokeColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   724
            self._canvas.polyLine(_pointsFromList(p.points))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   725
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   726
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   727
    def drawEllipse(self, ellipse):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   728
        #need to convert to pdfgen's bounding box representation
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   729
        x1 = ellipse.cx - ellipse.rx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   730
        x2 = ellipse.cx + ellipse.rx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   731
        y1 = ellipse.cy - ellipse.ry
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   732
        y2 = ellipse.cy + ellipse.ry
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   733
        self._canvas.ellipse(x1,y1,x2,y2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   734
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   735
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   736
    def drawPolygon(self, p):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   737
        self._canvas.polygon(_pointsFromList(p.points), closed=1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   738
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   739
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   740
    def drawPath(self, path):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   741
        # print "### drawPath", path.points
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   742
        from reportlab.graphics.shapes import _renderPath
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   743
        c = self._canvas
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   744
        drawFuncs = (c.moveTo, c.lineTo, c.curveTo, c.closePath)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   745
        isClosed = _renderPath(path, drawFuncs)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   746
        if not isClosed:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   747
            c._fillColor = None
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   748
        c._fillAndStroke([], clip=path.isClipPath)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   749
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   750
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   751
    def applyStateChanges(self, delta, newState):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   752
        """This takes a set of states, and outputs the operators
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   753
        needed to set those properties"""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   754
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   755
        for key, value in delta.items():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   756
            if key == 'transform':
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   757
                pass
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   758
                #self._canvas.transform(value[0], value[1], value[2], value[3], value[4], value[5])
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   759
            elif key == 'strokeColor':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   760
                self._canvas.setStrokeColor(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   761
            elif key == 'strokeWidth':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   762
                self._canvas.setLineWidth(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   763
            elif key == 'strokeLineCap':  #0,1,2
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   764
                self._canvas.setLineCap(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   765
            elif key == 'strokeLineJoin':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   766
                self._canvas.setLineJoin(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   767
            elif key == 'strokeDashArray':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   768
                if value:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   769
                    self._canvas.setDash(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   770
                else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   771
                    self._canvas.setDash()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   772
            elif key == 'fillColor':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   773
                self._canvas.setFillColor(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   774
            elif key in ['fontSize', 'fontName']:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   775
                fontname = delta.get('fontName', self._canvas._font)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   776
                fontsize = delta.get('fontSize', self._canvas._fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   777
                self._canvas.setFont(fontname, fontsize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   778
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   779
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   780
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   781
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   782
def test0(outdir='svgout'):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   783
    # print all drawings and their doc strings from the test
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   784
    # file
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   785
    if not os.path.isdir(outdir):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   786
        os.mkdir(outdir)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   787
    #grab all drawings from the test module
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   788
    from reportlab.graphics import testshapes
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   789
    drawings = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   790
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   791
    for funcname in dir(testshapes):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   792
        #if funcname[0:11] == 'getDrawing2':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   793
        #    print 'hacked to only show drawing 2'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   794
        if funcname[0:10] == 'getDrawing':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   795
            drawing = eval('testshapes.' + funcname + '()')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   796
            docstring = eval('testshapes.' + funcname + '.__doc__')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   797
            drawings.append((drawing, docstring))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   798
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   799
    # return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   800
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   801
    i = 0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   802
    for (d, docstring) in drawings:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   803
        filename = outdir + os.sep + 'renderSVG_%d.svg' % i
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   804
        drawToFile(d, filename)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   805
        # print 'saved', filename
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   806
        i = i + 1
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   807
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   808
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   809
def test1():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   810
    from reportlab.graphics.testshapes import getDrawing01
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   811
    d = getDrawing01()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   812
    drawToFile(d, "svgout/test.svg")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   813
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   814
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   815
def test2():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   816
    from reportlab.lib.corp import RL_CorpLogo
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   817
    from reportlab.graphics.shapes import Drawing
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   818
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   819
    rl = RL_CorpLogo()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   820
    d = Drawing(rl.width,rl.height)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   821
    d.add(rl)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   822
    drawToFile(d, "svgout/corplogo.svg")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   823
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   824
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   825
if __name__=='__main__':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   826
    test0()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   827
    test1()
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   828
    test2()