src/reportlab/graphics/renderSVG.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3765 a7e7cd6e0003
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 3028
diff changeset
     1
__doc__="""An experimental SVG renderer for the ReportLab graphics framework.
1607
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
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
    10
import math, types, sys, os, codecs
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    11
from operator import getitem
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    12
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    13
from reportlab.pdfbase.pdfmetrics import stringWidth # for font info
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    14
from reportlab.lib.utils import fp_str
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    15
from reportlab.lib.colors import black
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2360
diff changeset
    16
from reportlab.graphics.renderbase import StateTracker, getStateDelta, Renderer, renderScaledDrawing
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    17
from reportlab.graphics.shapes import STATE_DEFAULTS, Path, UserNode
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    18
from reportlab.graphics.shapes import * # (only for test0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    19
from reportlab import rl_config
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    20
from reportlab.lib.utils import getBytesIO, RLString
1607
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
### some constants ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    25
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    26
sin = math.sin
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    27
cos = math.cos
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    28
pi = math.pi
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    29
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
    30
AREA_STYLES = 'stroke-width stroke-linecap stroke stroke-opacity fill fill-opacity stroke-dasharray id'.split()
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
    31
LINE_STYLES = 'stroke-width stroke-linecap stroke stroke-opacity stroke-dasharray id'.split()
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
    32
TEXT_STYLES = 'font-family font-weight font-style font-variant font-size id'.split()
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    33
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    34
### top-level user function ###
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
    35
def drawToString(d, showBoundary=rl_config.showBoundary,**kwds):
1743
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    36
    "Returns a SVG as a string in memory, without touching the disk"
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    37
    s = getBytesIO()
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
    38
    drawToFile(d, s, showBoundary=showBoundary,**kwds)
1743
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    39
    return s.getvalue()
4815d932fb3a Dirk Datzert's drawToString function
andy_robinson
parents: 1683
diff changeset
    40
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
    41
def drawToFile(d, fn, showBoundary=rl_config.showBoundary,**kwds):
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2360
diff changeset
    42
    d = renderScaledDrawing(d)
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
    43
    c = SVGCanvas((d.width, d.height),**kwds)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    44
    draw(d, c, 0, 0, showBoundary=showBoundary)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    45
    c.save(fn)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    46
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    47
def draw(drawing, canvas, x=0, y=0, showBoundary=rl_config.showBoundary):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    48
    """As it says."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    49
    r = _SVGRenderer()
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
    50
    r.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    51
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    52
### helper functions ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    53
def _pointsFromList(L):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    54
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    55
    given a list of coordinates [x0, y0, x1, y1....]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    56
    produce a list of points [(x0,y0), (y1,y0),....]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    57
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    58
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    59
    P=[]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    60
    for i in range(0,len(L), 2):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    61
        P.append((L[i], L[i+1]))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    62
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    63
    return P
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
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    68
    Creates a new DOM node with tag name 'newTag' for document 'doc'
1607
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
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    73
    E.g.
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    74
1607
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)
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    86
    for newAttr, attr in attrDict.items():
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
    87
        sattr =  str(attr)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
    88
        if not node:
1607
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
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
    96
class EncodedWriter(list):
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
    97
    '''
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
    98
    EncodedWriter(encoding) assumes .write will be called with
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
    99
    either unicode or utf8 encoded strings.  it will accumulate
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   100
    strings encoded as the specified encoding.
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   101
    '''
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   102
    BOMS =  {
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   103
        'utf-32':codecs.BOM_UTF32,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   104
        'utf-32-be':codecs.BOM_UTF32_BE,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   105
        'utf-32-le':codecs.BOM_UTF32_LE,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   106
        'utf-16':codecs.BOM_UTF16,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   107
        'utf-16-be':codecs.BOM_UTF16_BE,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   108
        'utf-16-le':codecs.BOM_UTF16_LE,
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   109
        }
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   110
    def __init__(self,encoding,bom=False):
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   111
        list.__init__(self)
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   112
        self.encoding = encoding = codecs.lookup(encoding).name
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   113
        if bom and '16' in encoding or '32' in encoding:
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   114
            self.write(self.BOMS[encoding])
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   115
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   116
    def write(self,s):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   117
        if isinstance(s,str):
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   118
            s = s.encode(self.encoding)
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   119
        elif isinstance(s,str):
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   120
            try:
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   121
                 u = s.decode('utf-8')
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   122
            except:
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   123
                et, ev, tb = sys.exc_info()
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   124
                ev = str(ev)
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   125
                del et, tb
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   126
                raise ValueError("String %r not encoded as 'utf-8'\nerror=%s" % (s,ev))
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   127
            if self.encoding!='utf-8':
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   128
                s = u.decode(self.encoding)
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   129
        else:
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   130
            raise ValueError("EncodedWriter.write(%r) argument should be 'utf-8' string or unicode" % s)
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   131
        self.append(s)
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   132
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   133
    def getvalue(self):
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   134
        r = ''.join(self)
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   135
        del self[:]
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   136
        return r
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   137
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   138
### classes ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   139
class SVGCanvas:
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   140
    def __init__(self, size=(300,300), encoding='utf-8', verbose=0, bom=False, **kwds):
3639
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   141
        '''
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   142
        verbose = 0 >0 means do verbose stuff
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   143
        useClip = False True means don't use a clipPath definition put the global clip into the clip property
3639
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   144
                        to get around an issue with safari
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   145
        extraXmlDecl = ''   use to add extra xml declarations
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   146
        scaleGroupId = ''   id of an extra group to add around the drawing to allow easy scaling
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   147
        svgAttrs = {}       dictionary of attributes to be applied to the svg tag itself
3639
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   148
        '''
3607
97d0b7cfa37f renderSVG.py: allow for encoding (default utf8) and verbose=0; Guillaume's changes
rgbecker
parents: 3606
diff changeset
   149
        self.verbose = verbose
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   150
        self.encoding = codecs.lookup(encoding).name
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   151
        self.bom = bom
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   152
        useClip = kwds.pop('useClip',False)
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   153
        self.fontHacks = kwds.pop('fontHacks',{})
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   154
        self.extraXmlDecl = kwds.pop('extraXmlDecl','')
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   155
        scaleGroupId = kwds.pop('scaleGroupId','')
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   156
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   157
        self.width, self.height = self.size = size
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   158
        # self.height = size[1]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   159
        self.code = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   160
        self.style = {}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   161
        self.path = ''
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   162
        self._strokeColor = self._fillColor = self._lineWidth = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   163
            self._font = self._fontSize = self._lineCap = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   164
            self._lineJoin = self._color = None
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   165
1642
97651958fbe5 Load minidom specifically (pleasing Jython).
dinu_gherman
parents: 1617
diff changeset
   166
        implementation = getDOMImplementation('minidom')
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   167
        #Based on official example here http://www.w3.org/TR/SVG10/linking.html want:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   168
        #<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" 
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   169
        #  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   170
        #Thus,
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   171
        #doctype = implementation.createDocumentType("svg",
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   172
        #          "-//W3C//DTD SVG 20010904//EN",
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   173
        #          "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd")
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   174
        #
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   175
        #However, putting that example through http://validator.w3.org/ recommends:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   176
        #<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" 
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   177
        #  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   178
        #So we'll use that for our SVG 1.0 output.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   179
        doctype = implementation.createDocumentType("svg",
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   180
                  "-//W3C//DTD SVG 1.0//EN",
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   181
                  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd")
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   182
        self.doc = implementation.createDocument(None,"svg",doctype)
1617
c449e678a126 Removed dependency on PyXML. Now should use only std. lib. XML modules.
dinu_gherman
parents: 1614
diff changeset
   183
        self.svg = self.doc.documentElement
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   184
        svgAttrs = dict(
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   185
                    width = str(size[0]),
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   186
                    height=str(self.height),
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   187
                    preserveAspectRatio="xMinYMin meet",
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   188
                    viewBox="0 0 %d %d" % (self.width, self.height),
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   189
                    #baseProfile = "full",  #disliked in V 1.0
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   190
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   191
                    #these suggested by Tim Roberts, as updated by peter@maubp.freeserve.co.uk 
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   192
                    xmlns="http://www.w3.org/2000/svg",
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   193
                    version="1.0",
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   194
                    )
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   195
        svgAttrs["xmlns:xlink"] = "http://www.w3.org/1999/xlink"
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   196
        svgAttrs.update(kwds.pop('svgAttrs',{}))
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   197
        for k,v in svgAttrs.items():
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   198
            self.svg.setAttribute(k,v)
3605
bb3c9e12a4db added viewBox attribute to SVG drawings. This means they can be resized by setting the width and eheight in the embed tag.
andy
parents: 3592
diff changeset
   199
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   200
        title = self.doc.createElement('title')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   201
        text = self.doc.createTextNode('...')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   202
        title.appendChild(text)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   203
        self.svg.appendChild(title)
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   204
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   205
        desc = self.doc.createElement('desc')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   206
        text = self.doc.createTextNode('...')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   207
        desc.appendChild(text)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   208
        self.svg.appendChild(desc)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   209
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   210
        self.setFont(STATE_DEFAULTS['fontName'], STATE_DEFAULTS['fontSize'])
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   211
        self.setStrokeColor(STATE_DEFAULTS['strokeColor'])
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   212
        self.setLineCap(2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   213
        self.setLineJoin(0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   214
        self.setLineWidth(1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   215
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   216
        if not useClip:
3639
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   217
            # Add a rectangular clipping path identical to view area.
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   218
            clipPath = transformNode(self.doc, "clipPath", id="clip")
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   219
            clipRect = transformNode(self.doc, "rect", x=0, y=0,
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   220
                width=self.width, height=self.height)
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   221
            clipPath.appendChild(clipRect)
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   222
            self.svg.appendChild(clipPath)
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   223
            gtkw = dict(style="clip-path: url(#clip)")
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   224
        else:
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   225
            gtkw = dict(clip="0 0 %d %d" % (self.width,self.height))
1661
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   226
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   227
        self.groupTree = transformNode(self.doc, "g",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   228
            id="group",
1661
94d09f7b4c9a Added outer rectangular clipping path.
dinu_gherman
parents: 1648
diff changeset
   229
            transform="scale(1,-1) translate(0,-%d)" % self.height,
3639
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   230
            **gtkw
22763fe00fab renderSVG.py: allow way to use clip property rather than <clipPath>
rptlab
parents: 3635
diff changeset
   231
            )
3644
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   232
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   233
        if scaleGroupId:
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   234
            self.scaleTree = transformNode(self.doc, "g", id=scaleGroupId, transform="scale(1,1)")
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   235
            self.scaleTree.appendChild(self.groupTree)
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   236
            self.svg.appendChild(self.scaleTree)
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   237
        else:
8072bd965473 renderSVG.py: more features to support web usage
rptlab
parents: 3641
diff changeset
   238
            self.svg.appendChild(self.groupTree)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   239
        self.currGroup = self.groupTree
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   240
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   241
    def save(self, fn=None):
3621
e9525ac2459b renderSVG.py: more support for encoded output
rgbecker
parents: 3620
diff changeset
   242
        writer = EncodedWriter(self.encoding,bom=self.bom)
3620
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   243
        self.doc.writexml(writer,addindent="\t",newl="\n",encoding=self.encoding)
7b2b21af8759 renderSVG.py: use a properly checking EndodedWriter class
rgbecker
parents: 3608
diff changeset
   244
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   245
        if type(fn) in str:
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   246
            f = open(fn, 'w')
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   247
        else:
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   248
            f = fn
3641
2d1e356abce1 renderSVG.py: add canvas extraXmlDecl argument
rptlab
parents: 3640
diff changeset
   249
        svg = writer.getvalue()
2d1e356abce1 renderSVG.py: add canvas extraXmlDecl argument
rptlab
parents: 3640
diff changeset
   250
        exd = self.extraXmlDecl
2d1e356abce1 renderSVG.py: add canvas extraXmlDecl argument
rptlab
parents: 3640
diff changeset
   251
        if exd:
2d1e356abce1 renderSVG.py: add canvas extraXmlDecl argument
rptlab
parents: 3640
diff changeset
   252
            svg = svg.replace('?>','?>'+exd)
2d1e356abce1 renderSVG.py: add canvas extraXmlDecl argument
rptlab
parents: 3640
diff changeset
   253
        f.write(svg)
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   254
        if f is not fn:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   255
            f.close()
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   256
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   257
    ### helpers ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   258
    def NOTUSED_stringWidth(self, s, font=None, fontSize=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   259
        """Return the logical width of the string if it were drawn
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   260
        in the current font (defaults to self.font).
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   261
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   262
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   263
        font = font or self._font
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   264
        fontSize = fontSize or self._fontSize
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   265
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   266
        return stringWidth(s, font, fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   267
3631
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   268
    def _formatStyle(self, include=[], exclude='',**kwds):
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   269
        style = self.style.copy()
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   270
        style.update(kwds)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   271
        keys = list(style.keys())
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   272
        if include:
3631
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   273
            keys = [k for k in keys if k in include]
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   274
        if exclude:
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   275
            exclude = exclude.split()
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   276
            items = [k+': '+str(style[k]) for k in keys if k not in exclude]
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   277
        else:
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   278
            items = [k+': '+str(style[k]) for k in keys]
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   279
        return '; '.join(items) + ';'
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   280
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   281
    def _escape(self, s):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   282
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   283
        return a copy of string s with special characters in postscript strings
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   284
        escaped with backslashes.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   285
        Have not handled characters that are converted normally in python strings
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   286
        i.e. \\n -> newline
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   287
        """
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   288
        return s.replace(chr(0x5C), r'\\' ).replace('(', '\(' ).replace(')', '\)')
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   289
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   290
    def _genArcCode(self, x1, y1, x2, y2, startAng, extent):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   291
        """Calculate the path for an arc inscribed in rectangle defined
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   292
        by (x1,y1),(x2,y2)."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   293
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   294
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   295
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   296
        #calculate semi-minor and semi-major axes of ellipse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   297
        xScale = abs((x2-x1)/2.0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   298
        yScale = abs((y2-y1)/2.0)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   299
        #calculate centre of ellipse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   300
        x, y = (x1+x2)/2.0, (y1+y2)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   301
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   302
        codeline = 'matrix currentmatrix %s %s translate %s %s scale 0 0 1 %s %s %s setmatrix'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   303
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   304
        if extent >= 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   305
            arc='arc'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   306
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   307
            arc='arcn'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   308
        data = (x,y, xScale, yScale, startAng, startAng+extent, arc)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   309
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   310
        return codeline % data
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   311
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   312
    def _fillAndStroke(self, code, clip=0, link_info=None,styles=AREA_STYLES):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   313
        path = transformNode(self.doc, "path",
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   314
            d=self.path, style=self._formatStyle(styles))
3128
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   315
        if link_info :
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   316
            path = self._add_link(path, link_info)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   317
        self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   318
        self.path = ''
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   319
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   320
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   321
    ### styles ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   322
    def setLineCap(self, v):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   323
        vals = {0:'butt', 1:'round', 2:'square'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   324
        if self._lineCap != v:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   325
            self._lineCap = v
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   326
            self.style['stroke-linecap'] = vals[v]
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   327
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   328
    def setLineJoin(self, v):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   329
        vals = {0:'miter', 1:'round', 2:'bevel'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   330
        if self._lineJoin != v:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   331
            self._lineJoin = v
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   332
            self.style['stroke-linecap'] = vals[v]
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   333
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   334
    def setDash(self, array=[], phase=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   335
        """Two notations. Pass two numbers, or an array and phase."""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   336
3502
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   337
        if isinstance(array,(float,int)):
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   338
            self.style['stroke-dasharray'] = ', '.join(map(str, ([array, phase])))
3502
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   339
        elif isinstance(array,(tuple,list)) and len(array) > 0:
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   340
            assert phase >= 0, "phase is a length in user space"
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   341
            self.style['stroke-dasharray'] = ', '.join(map(str, (array+[phase])))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   342
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   343
    def setStrokeColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   344
        self._strokeColor = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   345
        self.setColor(color)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   346
        if color == None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   347
            self.style['stroke'] = 'none'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   348
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   349
            r, g, b = color.red, color.green, color.blue
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   350
            self.style['stroke'] = 'rgb(%d%%,%d%%,%d%%)' % (r*100, g*100, b*100)
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   351
            alpha = color.normalizedAlpha
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   352
            if alpha!=1:
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   353
                self.style['stroke-opacity'] = '%s' % alpha
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   354
            elif 'stroke-opacity' in self.style:
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   355
                del self.style['stroke-opacity']
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   356
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   357
    def setColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   358
        if self._color != color:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   359
            self._color = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   360
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   361
    def setFillColor(self, color):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   362
        self._fillColor = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   363
        self.setColor(color)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   364
        if color == None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   365
            self.style['fill'] = 'none'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   366
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   367
            r, g, b = color.red, color.green, color.blue
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   368
            self.style['fill'] = 'rgb(%d%%,%d%%,%d%%)' % (r*100, g*100, b*100)
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   369
            alpha = color.normalizedAlpha
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   370
            if alpha!=1:
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   371
                self.style['fill-opacity'] = '%s' % alpha
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   372
            elif 'fill-opacity' in self.style:
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   373
                del self.style['fill-opacity']
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   374
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   375
    def setLineWidth(self, width):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   376
        if width != self._lineWidth:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   377
            self._lineWidth = width
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   378
            self.style['stroke-width'] = width
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   379
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   380
    def setFont(self, font, fontSize):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   381
        if self._font != font or self._fontSize != fontSize:
3631
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   382
            self._font = font
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   383
            self._fontSize = fontSize
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   384
            style = self.style
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   385
            for k in TEXT_STYLES:
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   386
                if k in style:
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   387
                    del style[k]
3640
fe11db4e7bed renderSVG.py: add fontHacks to SVGCanvas
rptlab
parents: 3639
diff changeset
   388
            svgAttrs = self.fontHacks[font] if font in self.fontHacks else {}
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   389
            if isinstance(font,RLString):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   390
                svgAttrs.update(iter(font.svgAttrs.items()))
3640
fe11db4e7bed renderSVG.py: add fontHacks to SVGCanvas
rptlab
parents: 3639
diff changeset
   391
            if svgAttrs:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   392
                for k,v in svgAttrs.items():
3640
fe11db4e7bed renderSVG.py: add fontHacks to SVGCanvas
rptlab
parents: 3639
diff changeset
   393
                    a = 'font-'+k
fe11db4e7bed renderSVG.py: add fontHacks to SVGCanvas
rptlab
parents: 3639
diff changeset
   394
                    if a in TEXT_STYLES:
fe11db4e7bed renderSVG.py: add fontHacks to SVGCanvas
rptlab
parents: 3639
diff changeset
   395
                        style[a] = v
3631
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   396
            if 'font-family' not in style:
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   397
                style['font-family'] = font
faec46a34c20 renderSVG add support for font attributes
rgbecker
parents: 3621
diff changeset
   398
            style['font-size'] = '%spx' % fontSize
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   399
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   400
    def _add_link(self, dom_object, link_info) :
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   401
        assert isinstance(link_info, dict)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   402
        link = transformNode(self.doc, "a", **link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   403
        link.appendChild(dom_object)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   404
        return link
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   405
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   406
    ### shapes ###
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   407
    def rect(self, x1,y1, x2,y2, rx=8, ry=8, link_info=None, **_svgAttrs):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   408
        "Draw a rectangle between x1,y1 and x2,y2."
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   409
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   410
        if self.verbose: print("+++ SVGCanvas.rect")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   411
3358
1c3e05c2cf09 renderSVG.py: attempt to fix negative dimensions for rect
rgbecker
parents: 3326
diff changeset
   412
        x = min(x1,x2)
1c3e05c2cf09 renderSVG.py: attempt to fix negative dimensions for rect
rgbecker
parents: 3326
diff changeset
   413
        y = min(y1,y2)
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   414
        kwds = {}
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   415
        rect = transformNode(self.doc, "rect",
3358
1c3e05c2cf09 renderSVG.py: attempt to fix negative dimensions for rect
rgbecker
parents: 3326
diff changeset
   416
            x=x, y=y, width=max(x1,x2)-x, height=max(y1,y2)-y,
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   417
            style=self._formatStyle(AREA_STYLES),**_svgAttrs)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   418
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   419
        if link_info :
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   420
            rect = self._add_link(rect, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   421
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   422
        self.currGroup.appendChild(rect)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   423
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   424
    def roundRect(self, x1,y1, x2,y2, rx=8, ry=8, link_info=None, **_svgAttrs):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   425
        """Draw a rounded rectangle between x1,y1 and x2,y2.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   426
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   427
        Corners inset as ellipses with x-radius rx and y-radius ry.
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   428
        These should have x1<x2, y1<y2, rx>0, and ry>0.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   429
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   430
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   431
        kwds = {}
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   432
        rect = transformNode(self.doc, "rect",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   433
            x=x1, y=y1, width=x2-x1, height=y2-y1, rx=rx, ry=ry,
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   434
            style=self._formatStyle(AREA_STYLES), **_svgAttrs)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   435
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   436
        if link_info:
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   437
            rect = self._add_link(rect, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   438
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   439
        self.currGroup.appendChild(rect)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   440
3635
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3632
diff changeset
   441
    def drawString(self, s, x, y, angle=0, link_info=None,**_svgAttrs):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   442
        if self.verbose: print("+++ SVGCanvas.drawString")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   443
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   444
        if self._fillColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   445
            self.setColor(self._fillColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   446
            s = self._escape(s)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   447
            st = self._formatStyle(TEXT_STYLES)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   448
            if angle != 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   449
               st = st + " rotate(%f %f %f);" % (angle, x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   450
            st = st + " fill: %s;" % self.style['fill']
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   451
            text = transformNode(self.doc, "text",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   452
                x=x, y=y, style=st,
3635
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3632
diff changeset
   453
                transform="translate(0,%d) scale(1,-1)" % (2*y),
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3632
diff changeset
   454
                **_svgAttrs
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3632
diff changeset
   455
                )
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   456
            content = self.doc.createTextNode(s)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   457
            text.appendChild(content)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   458
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   459
            if link_info:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   460
                text = self._add_link(text, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   461
    
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   462
            self.currGroup.appendChild(text)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   463
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   464
    def drawCentredString(self, s, x, y, angle=0, text_anchor='middle', link_info=None):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   465
        if self.verbose: print("+++ SVGCanvas.drawCentredString")
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   466
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   467
        if self._fillColor != None:
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   468
            if not text_anchor in ['start', 'inherited']:
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   469
                textLen = stringWidth(s,self._font,self._fontSize)
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   470
                if text_anchor=='end':
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   471
                    x -= textLen
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   472
                elif text_anchor=='middle':
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   473
                    x -= textLen/2.
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   474
                elif text_anchor=='numeric':
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   475
                    x -= numericXShift(text_anchor,s,textLen,self._font,self._fontSize)
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   476
                else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   477
                    raise ValueError('bad value for text_anchor ' + str(text_anchor))
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   478
        self.drawString(x,y,text,angle=angle, link_info=link_info)
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2553
diff changeset
   479
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   480
    def drawRightString(self, text, x, y, angle=0, link_info=None):
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   481
        self.drawCentredString(text,x,y,angle=angle,text_anchor='end', link_info=link_info)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   482
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   483
    def comment(self, data):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   484
        "Add a comment."
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   485
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   486
        comment = self.doc.createComment(data)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   487
        # self.currGroup.appendChild(comment)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   488
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   489
    def drawImage(self, image, x1, y1, x2=None, y2=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   490
        pass
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   491
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   492
    def line(self, x1, y1, x2, y2):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   493
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   494
            if 0: # something is wrong with line in my SVG viewer...
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   495
                line = transformNode(self.doc, "line",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   496
                    x=x1, y=y1, x2=x2, y2=y2,
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   497
                    style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   498
                self.currGroup.appendChild(line)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   499
            path = transformNode(self.doc, "path",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   500
                d="M %f,%f L %f,%f Z" % (x1,y1,x2,y2),
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   501
                style=self._formatStyle(LINE_STYLES))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   502
            self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   503
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   504
    def ellipse(self, x1, y1, x2, y2, link_info=None):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   505
        """Draw an orthogonal ellipse inscribed within the rectangle x1,y1,x2,y2.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   506
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   507
        These should have x1<x2 and y1<y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   508
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   509
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   510
        ellipse = transformNode(self.doc, "ellipse",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   511
            cx=(x1+x2)/2.0, cy=(y1+y2)/2.0, rx=(x2-x1)/2.0, ry=(y2-y1)/2.0,
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   512
            style=self._formatStyle(AREA_STYLES))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   513
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   514
        if link_info:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   515
            ellipse = self._add_link(ellipse, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   516
            
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   517
        self.currGroup.appendChild(ellipse)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   518
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   519
    def circle(self, xc, yc, r, link_info=None):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   520
        circle = transformNode(self.doc, "circle",
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   521
            cx=xc, cy=yc, r=r,
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   522
            style=self._formatStyle(AREA_STYLES))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   523
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   524
        if link_info:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   525
            circle = self._add_link(circle, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   526
        
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   527
        self.currGroup.appendChild(circle)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   528
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   529
    def drawCurve(self, x1, y1, x2, y2, x3, y3, x4, y4, closed=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   530
        pass
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   531
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   532
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   533
        codeline = '%s m %s curveto'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   534
        data = (fp_str(x1, y1), fp_str(x2, y2, x3, y3, x4, y4))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   535
        if self._fillColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   536
            self.setColor(self._fillColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   537
            self.code.append((codeline % data) + ' eofill')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   538
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   539
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   540
            self.code.append((codeline % data)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   541
                            + ((closed and ' closepath') or '')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   542
                            + ' stroke')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   543
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   544
    def drawArc(self, x1,y1, x2,y2, startAng=0, extent=360, fromcenter=0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   545
        """Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   546
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   547
        Starting at startAng degrees and covering extent degrees. Angles
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   548
        start with 0 to the right (+x) and increase counter-clockwise.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   549
        These should have x1<x2 and y1<y2.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   550
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   551
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   552
        cx, cy = (x1+x2)/2.0, (y1+y2)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   553
        rx, ry = (x2-x1)/2.0, (y2-y1)/2.0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   554
        mx = rx * cos(startAng*pi/180) + cx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   555
        my = ry * sin(startAng*pi/180) + cy
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   556
        ax = rx * cos((startAng+extent)*pi/180) + cx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   557
        ay = ry * sin((startAng+extent)*pi/180) + cy
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   558
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   559
        str = ''
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   560
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   561
            str = str + "M %f, %f L %f, %f " % (cx, cy, ax, ay)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   562
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   563
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   564
            str = str + "A %f, %f %d %d %d %f, %f " % \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   565
              (rx, ry, 0, extent>=180, 0, mx, my)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   566
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   567
            str = str + "M %f, %f A %f, %f %d %d %d %f, %f Z " % \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   568
              (mx, my, rx, ry, 0, extent>=180, 0, mx, my)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   569
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   570
        if fromcenter:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   571
            str = str + "L %f, %f Z " % (cx, cy)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   572
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   573
        path = transformNode(self.doc, "path",
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   574
            d=str, style=self._formatStyle())
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   575
        self.currGroup.appendChild(path)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   576
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   577
    def polygon(self, points, closed=0, link_info=None):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   578
        assert len(points) >= 2, 'Polygon must have 2 or more points'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   579
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   580
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   581
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   582
            pairs = []
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   583
            for i in range(len(points)):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   584
                pairs.append("%f %f" % (points[i]))
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   585
            pts = ', '.join(pairs)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   586
            polyline = transformNode(self.doc, "polygon",
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   587
                points=pts, style=self._formatStyle(AREA_STYLES))
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   588
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   589
            if link_info:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   590
                polyline = self._add_link(polyline, link_info)
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   591
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   592
            self.currGroup.appendChild(polyline)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   593
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   594
        # self._fillAndStroke(polyCode)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   595
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   596
    def lines(self, lineList, color=None, width=None):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   597
        # print "### lineList", lineList
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   598
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   599
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   600
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   601
            self._setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   602
            codeline = '%s m %s l stroke'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   603
            for line in lineList:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   604
                self.code.append(codeline % (fp_str(line[0]), fp_str(line[1])))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   605
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   606
    def polyLine(self, points):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   607
        assert len(points) >= 1, 'Polyline must have 1 or more points'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   608
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   609
        if self._strokeColor != None:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   610
            self.setColor(self._strokeColor)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   611
            pairs = []
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   612
            for i in range(len(points)):
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   613
                pairs.append("%f %f" % (points[i]))
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   614
            pts = ', '.join(pairs)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   615
            polyline = transformNode(self.doc, "polyline",
3370
0783797d9871 graphics.py: adjust renderPM test and fix renderSVG PolyLine
rgbecker
parents: 3358
diff changeset
   616
                points=pts, style=self._formatStyle(AREA_STYLES,fill=None))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   617
            self.currGroup.appendChild(polyline)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   618
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   619
    ### groups ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   620
    def startGroup(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   621
        if self.verbose: print("+++ begin SVGCanvas.startGroup")
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   622
        currGroup, group = self.currGroup, transformNode(self.doc, "g", transform="")
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   623
        currGroup.appendChild(group)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   624
        self.currGroup = group
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   625
        if self.verbose: print("+++ end SVGCanvas.startGroup")
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   626
        return currGroup
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   627
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   628
    def endGroup(self,currGroup):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   629
        if self.verbose: print("+++ begin SVGCanvas.endGroup")
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   630
        self.currGroup = currGroup
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   631
        if self.verbose: print("+++ end SVGCanvas.endGroup")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   632
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   633
    def transform(self, a, b, c, d, e, f):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   634
        if self.verbose: print("!!! begin SVGCanvas.transform", a, b, c, d, e, f)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   635
        tr = self.currGroup.getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   636
        t = 'matrix(%f, %f, %f, %f, %f, %f)' % (a,b,c,d,e,f)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   637
        if (a, b, c, d, e, f) != (1, 0, 0, 1, 0, 0):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   638
            self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   639
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   640
    def translate(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   641
        # probably never used
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   642
        print("!!! begin SVGCanvas.translate")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   643
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   644
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   645
        tr = self.currGroup.getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   646
        t = 'translate(%f, %f)' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   647
        self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   648
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   649
    def scale(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   650
        # probably never used
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   651
        print("!!! begin SVGCanvas.scale")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   652
        return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   653
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   654
        tr = self.groups[-1].getAttribute("transform")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   655
        t = 'scale(%f, %f)' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   656
        self.currGroup.setAttribute("transform", "%s %s" % (tr, t))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   657
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   658
    ### paths ###
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   659
    def moveTo(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   660
        self.path = self.path + 'M %f %f ' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   661
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   662
    def lineTo(self, x, y):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   663
        self.path = self.path + 'L %f %f ' % (x, y)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   664
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   665
    def curveTo(self, x1, y1, x2, y2, x3, y3):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   666
        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
   667
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   668
    def closePath(self):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   669
        self.path = self.path + 'Z '
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   670
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   671
    def saveState(self):
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   672
        pass
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   673
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   674
    def restoreState(self):
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   675
        pass
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   676
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   677
class _SVGRenderer(Renderer):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   678
    """This draws onto an SVG document.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   679
    """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   680
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   681
    def __init__(self):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   682
        self._tracker = StateTracker()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   683
        self.verbose = 0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   684
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   685
    def drawNode(self, node):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   686
        """This is the recursive method called for each node in the tree.
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   687
        """
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   688
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   689
        if self.verbose: print("### begin _SVGRenderer.drawNode(%r)" % node)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   690
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   691
        self._canvas.comment('begin node %r'%node)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   692
        color = self._canvas._color
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   693
        style = self._canvas.style.copy()
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   694
        if not (isinstance(node, Path) and node.isClipPath):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   695
            pass # self._canvas.saveState()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   696
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   697
        #apply state changes
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   698
        deltas = getStateDelta(node)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   699
        self._tracker.push(deltas)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   700
        self.applyStateChanges(deltas, {})
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   701
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   702
        #draw the object, or recurse
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   703
        self.drawNodeDispatcher(node)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   704
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   705
        rDeltas = self._tracker.pop()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   706
        if not (isinstance(node, Path) and node.isClipPath):
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   707
            pass #self._canvas.restoreState()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   708
        self._canvas.comment('end node %r'%node)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   709
        self._canvas._color = color
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   710
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   711
        #restore things we might have lost (without actually doing anything).
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   712
        for k, v in rDeltas.items():
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   713
            if k in self._restores:
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   714
                setattr(self._canvas,self._restores[k],v)
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   715
        self._canvas.style = style
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   716
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   717
        if self.verbose: print("### end _SVGRenderer.drawNode(%r)" % node)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   718
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   719
    _restores = {'strokeColor':'_strokeColor','strokeWidth': '_lineWidth','strokeLineCap':'_lineCap',
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   720
                'strokeLineJoin':'_lineJoin','fillColor':'_fillColor','fontName':'_font',
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   721
                'fontSize':'_fontSize'}
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   722
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   723
    def _get_link_info_dict(self, obj):
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   724
        #We do not want None or False as the link, even if it is the
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   725
        #attribute's value - use the empty string instead.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   726
        url = getattr(obj, "hrefURL", "") or ""
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   727
        title = getattr(obj, "hrefTitle", "") or ""
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   728
        if url :
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   729
            #Is it valid to have a link with no href?  The XML requires
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   730
            #the xlink:href to be present, but you might just want a
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   731
            #tool tip shown (via the xlink:title attribute).  Note that
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   732
            #giving an href of "" is equivalent to "the current page"
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   733
            #(a relative link saying go nowhere).
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   734
            return {"xlink:href":url, "xlink:title":title, "target":"_top"}
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   735
            #Currently of all the mainstream browsers I have tested, only Safari/webkit
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   736
            #will show  SVG images embedded in HTML using a simple <img src="..." /> tag.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   737
            #However, the links don't work (Safari 3.2.1 on the Mac).
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   738
            #
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   739
            #Therefore I use the following, which also works for Firefox, Opera, and
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   740
            #IE 6.0 with Adobe SVG Viewer 6 beta:
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   741
            #<object data="..." type="image/svg+xml" width="430" height="150" class="img">
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   742
            #
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   743
            #Once displayed, Firefox and Safari treat the SVG like a frame, and
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   744
            #by default clicking on links acts "in frame" and replaces the image.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   745
            #Opera does what I expect, and replaces the whole page with the link.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   746
            #
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   747
            #Therefore I use target="_top" to force the links to replace the whole page.
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   748
            #This now works as expected on Safari 3.2.1, Firefox 3.0.6, Opera 9.20. 
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   749
            #Perhaps the target attribute should be an option, perhaps defaulting to
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   750
            #"_top" as used here?
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   751
        else :
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   752
            return None
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   753
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   754
    def drawGroup(self, group):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   755
        if self.verbose: print("### begin _SVGRenderer.drawGroup")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   756
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   757
        currGroup = self._canvas.startGroup()
3056
3ce50e421931 reportlab.graphics.renderSVG: Fixed bug 704.
jonas
parents: 3032
diff changeset
   758
        a, b, c, d, e, f = self._tracker.getState()['transform']
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   759
        for childNode in group.getContents():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   760
            if isinstance(childNode, UserNode):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   761
                node2 = childNode.provideNode()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   762
            else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   763
                node2 = childNode
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   764
            self.drawNode(node2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   765
        self._canvas.transform(a, b, c, d, e, f)
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   766
        self._canvas.endGroup(currGroup)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   767
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   768
        if self.verbose: print("### end _SVGRenderer.drawGroup")
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   769
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   770
    def drawRect(self, rect):
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   771
        link_info = self._get_link_info_dict(rect)
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   772
        svgAttrs = getattr(rect,'_svgAttrs',{})
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   773
        if rect.rx == rect.ry == 0:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   774
            #plain old rectangle
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   775
            self._canvas.rect(
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   776
                    rect.x, rect.y,
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   777
                    rect.x+rect.width, rect.y+rect.height, link_info=link_info, **svgAttrs)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   778
        else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   779
            #cheat and assume ry = rx; better to generalize
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   780
            #pdfgen roundRect function.  TODO
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   781
            self._canvas.roundRect(
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   782
                    rect.x, rect.y,
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   783
                    rect.x+rect.width, rect.y+rect.height,
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   784
                    rect.rx, rect.ry,
3632
401542b8679b support for passing attributes into the svg renderer
rgbecker
parents: 3631
diff changeset
   785
                    link_info=link_info, **svgAttrs)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   786
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   787
    def drawString(self, stringObj):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   788
        if self._canvas._fillColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   789
            S = self._tracker.getState()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   790
            text_anchor, x, y, text = S['textAnchor'], stringObj.x, stringObj.y, stringObj.text
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   791
            if not text_anchor in ('start', 'inherited'):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   792
                font, fontSize = S['fontName'], S['fontSize']
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   793
                textLen = stringWidth(text, font,fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   794
                if text_anchor=='end':
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   795
                    x -= textLen
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   796
                elif text_anchor=='middle':
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   797
                    x -= textLen/2
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   798
                elif text_anchor=='numeric':
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3128
diff changeset
   799
                    x -= numericXShift(text_anchor,text,textLen,font,fontSize)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   800
                else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3644
diff changeset
   801
                    raise ValueError('bad value for text_anchor ' + str(text_anchor))
3635
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3632
diff changeset
   802
            self._canvas.drawString(text,x,y,link_info=self._get_link_info_dict(stringObj),**getattr(stringObj,'_svgAttrs',{}))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   803
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   804
    def drawLine(self, line):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   805
        if self._canvas._strokeColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   806
            self._canvas.line(line.x1, line.y1, line.x2, line.y2)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   807
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   808
    def drawCircle(self, circle):
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   809
        self._canvas.circle( circle.cx, circle.cy, circle.r, link_info=self._get_link_info_dict(circle))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   810
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   811
    def drawWedge(self, wedge):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   812
        centerx, centery, radius, startangledegrees, endangledegrees = \
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   813
         wedge.centerx, wedge.centery, wedge.radius, wedge.startangledegrees, wedge.endangledegrees
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   814
        yradius = wedge.yradius or wedge.radius
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   815
        (x1, y1) = (centerx-radius, centery-yradius)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   816
        (x2, y2) = (centerx+radius, centery+yradius)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   817
        extent = endangledegrees - startangledegrees
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   818
        self._canvas.drawArc(x1, y1, x2, y2, startangledegrees, extent, fromcenter=1)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   819
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   820
    def drawPolyLine(self, p):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   821
        if self._canvas._strokeColor:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   822
            self._canvas.polyLine(_pointsFromList(p.points))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   823
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   824
    def drawEllipse(self, ellipse):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   825
        #need to convert to pdfgen's bounding box representation
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   826
        x1 = ellipse.cx - ellipse.rx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   827
        x2 = ellipse.cx + ellipse.rx
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   828
        y1 = ellipse.cy - ellipse.ry
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   829
        y2 = ellipse.cy + ellipse.ry
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   830
        self._canvas.ellipse(x1,y1,x2,y2, link_info=self._get_link_info_dict(ellipse))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   831
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   832
    def drawPolygon(self, p):
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   833
        self._canvas.polygon(_pointsFromList(p.points), closed=1, link_info=self._get_link_info_dict(p))
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   834
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   835
    def drawPath(self, path):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   836
        # print "### drawPath", path.points
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   837
        from reportlab.graphics.shapes import _renderPath
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   838
        c = self._canvas
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   839
        drawFuncs = (c.moveTo, c.lineTo, c.curveTo, c.closePath)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   840
        isClosed = _renderPath(path, drawFuncs)
3128
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   841
        if isClosed:
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   842
            #Only try and add links to closed paths...
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   843
            link_info = self._get_link_info_dict(path)
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   844
        else :
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   845
            c._fillColor = None
3128
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   846
            link_info = None
28e7abf47067 reportlab: renderSVG.py add in PJACock's (<peter@maubp.freeserve.co.uk>) path linkability patch
rgbecker
parents: 3115
diff changeset
   847
        c._fillAndStroke([], clip=path.isClipPath, link_info=link_info)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   848
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   849
    def applyStateChanges(self, delta, newState):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   850
        """This takes a set of states, and outputs the operators
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   851
        needed to set those properties"""
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   852
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   853
        for key, value in delta.items():
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   854
            if key == 'transform':
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1670
diff changeset
   855
                pass
1648
64ffdd1963a7 Apparent fix for transform problem
rgbecker
parents: 1644
diff changeset
   856
                #self._canvas.transform(value[0], value[1], value[2], value[3], value[4], value[5])
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   857
            elif key == 'strokeColor':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   858
                self._canvas.setStrokeColor(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   859
            elif key == 'strokeWidth':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   860
                self._canvas.setLineWidth(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   861
            elif key == 'strokeLineCap':  #0,1,2
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   862
                self._canvas.setLineCap(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   863
            elif key == 'strokeLineJoin':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   864
                self._canvas.setLineJoin(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   865
            elif key == 'strokeDashArray':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   866
                if value:
3502
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   867
                    if isinstance(value,(list,tuple)) and len(value)==2 and isinstance(value[1],(tuple,list)):
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   868
                        phase = value[0]
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   869
                        value = value[1]
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   870
                    else:
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   871
                        phase = 0
b2c9927314fc color fixes
rgbecker
parents: 3370
diff changeset
   872
                    self._canvas.setDash(value,phase)
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   873
                else:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   874
                    self._canvas.setDash()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   875
            elif key == 'fillColor':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   876
                self._canvas.setFillColor(value)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   877
            elif key in ['fontSize', 'fontName']:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   878
                fontname = delta.get('fontName', self._canvas._font)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   879
                fontsize = delta.get('fontSize', self._canvas._fontSize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   880
                self._canvas.setFont(fontname, fontsize)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   881
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   882
def test0(outdir='svgout'):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   883
    # print all drawings and their doc strings from the test
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   884
    # file
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   885
    if not os.path.isdir(outdir):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   886
        os.mkdir(outdir)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   887
    #grab all drawings from the test module
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   888
    from reportlab.graphics import testshapes
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   889
    drawings = []
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   890
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   891
    for funcname in dir(testshapes):
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   892
        #if funcname[0:11] == 'getDrawing2':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   893
        #    print 'hacked to only show drawing 2'
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   894
        if funcname[0:10] == 'getDrawing':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   895
            drawing = eval('testshapes.' + funcname + '()')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   896
            docstring = eval('testshapes.' + funcname + '.__doc__')
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   897
            drawings.append((drawing, docstring))
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   898
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   899
    # return
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   900
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   901
    i = 0
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   902
    for (d, docstring) in drawings:
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   903
        filename = outdir + os.sep + 'renderSVG_%d.svg' % i
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   904
        drawToFile(d, filename)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   905
        # print 'saved', filename
3114
3e5d70c382c3 renderSVG.py: add in PJACock's pacth and fix a couple of bugs
rgbecker
parents: 3096
diff changeset
   906
        i += 1
1607
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   907
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   908
def test1():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   909
    from reportlab.graphics.testshapes import getDrawing01
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   910
    d = getDrawing01()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   911
    drawToFile(d, "svgout/test.svg")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   912
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   913
def test2():
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   914
    from reportlab.lib.corp import RL_CorpLogo
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   915
    from reportlab.graphics.shapes import Drawing
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   916
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   917
    rl = RL_CorpLogo()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   918
    d = Drawing(rl.width,rl.height)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   919
    d.add(rl)
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   920
    drawToFile(d, "svgout/corplogo.svg")
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   921
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   922
if __name__=='__main__':
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   923
    test0()
ab51672c5abf Initial checkin.
dinu_gherman
parents:
diff changeset
   924
    test1()
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 1744
diff changeset
   925
    test2()