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