author | rgbecker |
Sat, 07 Aug 2004 15:00:28 +0000 | |
changeset 2360 | 0fbaee224de1 |
parent 2332 | 2a7ab4405e18 |
child 2401 | cd68d7a84d05 |
permissions | -rw-r--r-- |
2332 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2004 |
817 | 2 |
#see license.txt for license details |
2332 | 3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/renderbase.py |
568 | 4 |
""" |
5 |
Superclass for renderers to factor out common functionality and default implementations. |
|
6 |
""" |
|
7 |
||
8 |
||
9 |
__version__=''' $Id $ ''' |
|
10 |
||
11 |
from reportlab.graphics.shapes import * |
|
2360
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
12 |
from reportlab import rl_config |
568 | 13 |
|
14 |
def inverse(A): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
15 |
"For A affine 2D represented as 6vec return 6vec version of A**(-1)" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
16 |
# I checked this RGB |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
17 |
det = float(A[0]*A[3] - A[2]*A[1]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
18 |
R = [A[3]/det, -A[1]/det, -A[2]/det, A[0]/det] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
19 |
return tuple(R+[-R[0]*A[4]-R[2]*A[5],-R[1]*A[4]-R[3]*A[5]]) |
568 | 20 |
|
21 |
def mmult(A, B): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
22 |
"A postmultiplied by B" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
23 |
# I checked this RGB |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
24 |
# [a0 a2 a4] [b0 b2 b4] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
25 |
# [a1 a3 a5] * [b1 b3 b5] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
26 |
# [ 1 ] [ 1 ] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
27 |
# |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
28 |
return (A[0]*B[0] + A[2]*B[1], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
29 |
A[1]*B[0] + A[3]*B[1], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
30 |
A[0]*B[2] + A[2]*B[3], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
31 |
A[1]*B[2] + A[3]*B[3], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
32 |
A[0]*B[4] + A[2]*B[5] + A[4], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
33 |
A[1]*B[4] + A[3]*B[5] + A[5]) |
568 | 34 |
|
35 |
||
36 |
def getStateDelta(shape): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
37 |
"""Used to compute when we need to change the graphics state. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
38 |
For example, if we have two adjacent red shapes we don't need |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
39 |
to set the pen color to red in between. Returns the effect |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
40 |
the given shape would have on the graphics state""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
41 |
delta = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
42 |
for (prop, value) in shape.getProperties().items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
43 |
if STATE_DEFAULTS.has_key(prop): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
44 |
delta[prop] = value |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
45 |
return delta |
568 | 46 |
|
47 |
||
48 |
class StateTracker: |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
49 |
"""Keeps a stack of transforms and state |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
50 |
properties. It can contain any properties you |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
51 |
want, but the keys 'transform' and 'ctm' have |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
52 |
special meanings. The getCTM() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
53 |
method returns the current transformation |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
54 |
matrix at any point, without needing to |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
55 |
invert matrixes when you pop.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
56 |
def __init__(self, defaults=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
57 |
# one stack to keep track of what changes... |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
58 |
self.__deltas = [] |
568 | 59 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
60 |
# and another to keep track of cumulative effects. Last one in |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
61 |
# list is the current graphics state. We put one in to simplify |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
62 |
# loops below. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
63 |
self.__combined = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
64 |
if defaults is None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
65 |
defaults = STATE_DEFAULTS.copy() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
66 |
#ensure that if we have a transform, we have a CTM |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
67 |
if defaults.has_key('transform'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
68 |
defaults['ctm'] = defaults['transform'] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
69 |
self.__combined.append(defaults) |
568 | 70 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
71 |
def push(self,delta): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
72 |
"""Take a new state dictionary of changes and push it onto |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
73 |
the stack. After doing this, the combined state is accessible |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
74 |
through getState()""" |
1665 | 75 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
76 |
newstate = self.__combined[-1].copy() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
77 |
for (key, value) in delta.items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
78 |
if key == 'transform': #do cumulative matrix |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
79 |
newstate['transform'] = delta['transform'] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
80 |
newstate['ctm'] = mmult(self.__combined[-1]['ctm'], delta['transform']) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
81 |
#print 'statetracker transform = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['transform']) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
82 |
#print 'statetracker ctm = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['ctm']) |
568 | 83 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
84 |
else: #just overwrite it |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
85 |
newstate[key] = value |
568 | 86 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
87 |
self.__combined.append(newstate) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
88 |
self.__deltas.append(delta) |
568 | 89 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
90 |
def pop(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
91 |
"""steps back one, and returns a state dictionary with the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
92 |
deltas to reverse out of wherever you are. Depending |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
93 |
on your back end, you may not need the return value, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
94 |
since you can get the complete state afterwards with getState()""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
95 |
del self.__combined[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
96 |
newState = self.__combined[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
97 |
lastDelta = self.__deltas[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
98 |
del self.__deltas[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
99 |
#need to diff this against the last one in the state |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
100 |
reverseDelta = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
101 |
#print 'pop()...' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
102 |
for key, curValue in lastDelta.items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
103 |
#print ' key=%s, value=%s' % (key, curValue) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
104 |
prevValue = newState[key] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
105 |
if prevValue <> curValue: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
106 |
#print ' state popping "%s"="%s"' % (key, curValue) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
107 |
if key == 'transform': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
108 |
reverseDelta[key] = inverse(lastDelta['transform']) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
109 |
else: #just return to previous state |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
110 |
reverseDelta[key] = prevValue |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
111 |
return reverseDelta |
568 | 112 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
113 |
def getState(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
114 |
"returns the complete graphics state at this point" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
115 |
return self.__combined[-1] |
568 | 116 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
117 |
def getCTM(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
118 |
"returns the current transformation matrix at this point""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
119 |
return self.__combined[-1]['ctm'] |
568 | 120 |
|
121 |
||
122 |
def testStateTracker(): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
123 |
print 'Testing state tracker' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
124 |
defaults = {'fillColor':None, 'strokeColor':None,'fontName':None, 'transform':[1,0,0,1,0,0]} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
125 |
deltas = [ |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
126 |
{'fillColor':'red'}, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
127 |
{'fillColor':'green', 'strokeColor':'blue','fontName':'Times-Roman'}, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
128 |
{'transform':[0.5,0,0,0.5,0,0]}, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
129 |
{'transform':[0.5,0,0,0.5,2,3]}, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
130 |
{'strokeColor':'red'} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
131 |
] |
1665 | 132 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
133 |
st = StateTracker(defaults) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
134 |
print 'initial:', st.getState() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
135 |
|
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
136 |
for delta in deltas: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
137 |
print 'pushing:', delta |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
138 |
st.push(delta) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
139 |
print 'state: ',st.getState(),'\n' |
568 | 140 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
141 |
for delta in deltas: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
142 |
print 'popping:',st.pop() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
143 |
print 'state: ',st.getState(),'\n' |
1665 | 144 |
|
568 | 145 |
|
1665 | 146 |
def _expandUserNode(node,canvas): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
147 |
if isinstance(node, UserNode): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
148 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
149 |
if hasattr(node,'_canvas'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
150 |
ocanvas = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
151 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
152 |
node._canvas = canvas |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
153 |
ocanvas = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
154 |
onode = node |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
155 |
node = node.provideNode() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
156 |
finally: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
157 |
if not ocanvas: del onode._canvas |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
158 |
return node |
568 | 159 |
|
160 |
class Renderer: |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
161 |
"""Virtual superclass for graphics renderers.""" |
1665 | 162 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
163 |
def __init__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
164 |
self._tracker = StateTracker() |
1665 | 165 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
166 |
def undefined(self, operation): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
167 |
raise ValueError, "%s operation not defined at superclass class=%s" %(operation, self.__class__) |
568 | 168 |
|
2360
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
169 |
def draw(self, drawing, canvas, x=0, y=0, showBoundary=rl_config._unset_): |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
170 |
"""This is the top level function, which draws the drawing at the given |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
171 |
location. The recursive part is handled by drawNode.""" |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
172 |
#stash references for ease of communication |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
173 |
if showBoundary is rl_config._unset_: showBoundary=rl_config.showBoundary |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
174 |
self._canvas = canvas |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
175 |
canvas.__dict__['_drawing'] = self._drawing = drawing |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
176 |
drawing._parent = None |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
177 |
try: |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
178 |
#bounding box |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
179 |
if showBoundary: canvas.rect(x, y, drawing.width, drawing.height) |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
180 |
canvas.saveState() |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
181 |
self.initState(x,y) |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
182 |
self.drawNode(drawing) |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
183 |
self.pop() |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
184 |
canvas.restoreState() |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
185 |
finally: |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
186 |
#remove any circular references |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
187 |
del self._canvas, self._drawing, canvas._drawing, drawing._parent |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
188 |
|
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
189 |
def initState(self,x,y): |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
190 |
deltas = STATE_DEFAULTS.copy() |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
191 |
deltas['transform'] = [1,0,0,1,x,y] |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
192 |
self._tracker.push(deltas) |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
193 |
self.applyStateChanges(deltas, {}) |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
194 |
|
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
195 |
def pop(self): |
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
196 |
self._tracker.pop() |
568 | 197 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
198 |
def drawNode(self, node): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
199 |
"""This is the recursive method called for each node |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
200 |
in the tree""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
201 |
# Undefined here, but with closer analysis probably can be handled in superclass |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
202 |
self.undefined("drawNode") |
1665 | 203 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
204 |
def drawNodeDispatcher(self, node): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
205 |
"""dispatch on the node's (super) class: shared code""" |
568 | 206 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
207 |
canvas = getattr(self,'_canvas',None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
208 |
# replace UserNode with its contents |
568 | 209 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
210 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
211 |
node = _expandUserNode(node,canvas) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
212 |
if hasattr(node,'_canvas'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
213 |
ocanvas = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
214 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
215 |
node._canvas = canvas |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
216 |
ocanvas = None |
568 | 217 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
218 |
#draw the object, or recurse |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
219 |
if isinstance(node, Line): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
220 |
self.drawLine(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
221 |
elif isinstance(node, Image): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
222 |
self.drawImage(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
223 |
elif isinstance(node, Rect): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
224 |
self.drawRect(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
225 |
elif isinstance(node, Circle): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
226 |
self.drawCircle(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
227 |
elif isinstance(node, Ellipse): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
228 |
self.drawEllipse(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
229 |
elif isinstance(node, PolyLine): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
230 |
self.drawPolyLine(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
231 |
elif isinstance(node, Polygon): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
232 |
self.drawPolygon(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
233 |
elif isinstance(node, Path): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
234 |
self.drawPath(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
235 |
elif isinstance(node, String): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
236 |
self.drawString(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
237 |
elif isinstance(node, Group): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
238 |
self.drawGroup(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
239 |
elif isinstance(node, Wedge): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
240 |
self.drawWedge(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
241 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
242 |
print 'DrawingError','Unexpected element %s in drawing!' % str(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
243 |
finally: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
244 |
if not ocanvas: del node._canvas |
1665 | 245 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
246 |
_restores = {'stroke':'_stroke','stroke_width': '_lineWidth','stroke_linecap':'_lineCap', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
247 |
'stroke_linejoin':'_lineJoin','fill':'_fill','font_family':'_font', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
248 |
'font_size':'_fontSize'} |
585
e0144950b3e2
Fixes to CTM to support bitmap renderer; extra string rotation
andy_robinson
parents:
580
diff
changeset
|
249 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
250 |
def drawGroup(self, group): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
251 |
# just do the contents. Some renderers might need to override this |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
252 |
# if they need a flipped transform |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
253 |
canvas = getattr(self,'_canvas',None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
254 |
for node in group.getContents(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
255 |
node = _expandUserNode(node,canvas) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
256 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
257 |
if hasattr(node,'_canvas'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
258 |
ocanvas = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
259 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
260 |
node._canvas = canvas |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
261 |
ocanvas = None |
2360
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
262 |
node._parent = group |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
263 |
self.drawNode(node) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
264 |
finally: |
2360
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
265 |
del node._parent |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
266 |
if not ocanvas: del node._canvas |
585
e0144950b3e2
Fixes to CTM to support bitmap renderer; extra string rotation
andy_robinson
parents:
580
diff
changeset
|
267 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
268 |
def drawWedge(self, wedge): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
269 |
# by default ask the wedge to make a polygon of itself and draw that! |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
270 |
#print "drawWedge" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
271 |
polygon = wedge.asPolygon() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
272 |
self.drawPolygon(polygon) |
1665 | 273 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
274 |
def drawPath(self, path): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
275 |
polygons = path.asPolygons() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
276 |
for polygon in polygons: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
277 |
self.drawPolygon(polygon) |
568 | 278 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
279 |
def drawRect(self, rect): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
280 |
# could be implemented in terms of polygon |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
281 |
self.undefined("drawRect") |
568 | 282 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
283 |
def drawLine(self, line): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
284 |
self.undefined("drawLine") |
568 | 285 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
286 |
def drawCircle(self, circle): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
287 |
self.undefined("drawCircle") |
1665 | 288 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
289 |
def drawPolyLine(self, p): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
290 |
self.undefined("drawPolyLine") |
568 | 291 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
292 |
def drawEllipse(self, ellipse): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
293 |
self.undefined("drawEllipse") |
568 | 294 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
295 |
def drawPolygon(self, p): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
296 |
self.undefined("drawPolygon") |
568 | 297 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
298 |
def drawString(self, stringObj): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
299 |
self.undefined("drawString") |
568 | 300 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
301 |
def applyStateChanges(self, delta, newState): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
302 |
"""This takes a set of states, and outputs the operators |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
303 |
needed to set those properties""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
304 |
self.undefined("applyStateChanges") |
568 | 305 |
|
306 |
if __name__=='__main__': |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1665
diff
changeset
|
307 |
print "this file has no script interpretation" |
2360
0fbaee224de1
rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents:
2332
diff
changeset
|
308 |
print __doc__ |