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