src/reportlab/graphics/renderbase.py
branchpy33
changeset 3721 0c93dd8ff567
parent 3617 ae5744e97c42
child 3723 99aa837b6703
equal deleted inserted replaced
3720:7a059dde5bf5 3721:0c93dd8ff567
    35     """Used to compute when we need to change the graphics state.
    35     """Used to compute when we need to change the graphics state.
    36     For example, if we have two adjacent red shapes we don't need
    36     For example, if we have two adjacent red shapes we don't need
    37     to set the pen color to red in between. Returns the effect
    37     to set the pen color to red in between. Returns the effect
    38     the given shape would have on the graphics state"""
    38     the given shape would have on the graphics state"""
    39     delta = {}
    39     delta = {}
    40     for (prop, value) in shape.getProperties().items():
    40     for (prop, value) in list(shape.getProperties().items()):
    41         if prop in STATE_DEFAULTS:
    41         if prop in STATE_DEFAULTS:
    42             delta[prop] = value
    42             delta[prop] = value
    43     return delta
    43     return delta
    44 
    44 
    45 
    45 
    70         """Take a new state dictionary of changes and push it onto
    70         """Take a new state dictionary of changes and push it onto
    71         the stack.  After doing this, the combined state is accessible
    71         the stack.  After doing this, the combined state is accessible
    72         through getState()"""
    72         through getState()"""
    73 
    73 
    74         newstate = self._combined[-1].copy()
    74         newstate = self._combined[-1].copy()
    75         for (key, value) in delta.items():
    75         for (key, value) in list(delta.items()):
    76             if key == 'transform':  #do cumulative matrix
    76             if key == 'transform':  #do cumulative matrix
    77                 newstate['transform'] = delta['transform']
    77                 newstate['transform'] = delta['transform']
    78                 newstate['ctm'] = mmult(self._combined[-1]['ctm'], delta['transform'])
    78                 newstate['ctm'] = mmult(self._combined[-1]['ctm'], delta['transform'])
    79                 #print 'statetracker transform = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['transform'])
    79                 #print 'statetracker transform = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['transform'])
    80                 #print 'statetracker ctm = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['ctm'])
    80                 #print 'statetracker ctm = (%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f)' % tuple(newstate['ctm'])
    95         lastDelta = self._deltas[-1]
    95         lastDelta = self._deltas[-1]
    96         del  self._deltas[-1]
    96         del  self._deltas[-1]
    97         #need to diff this against the last one in the state
    97         #need to diff this against the last one in the state
    98         reverseDelta = {}
    98         reverseDelta = {}
    99         #print 'pop()...'
    99         #print 'pop()...'
   100         for key, curValue in lastDelta.items():
   100         for key, curValue in list(lastDelta.items()):
   101             #print '   key=%s, value=%s' % (key, curValue)
   101             #print '   key=%s, value=%s' % (key, curValue)
   102             prevValue = newState[key]
   102             prevValue = newState[key]
   103             if prevValue != curValue:
   103             if prevValue != curValue:
   104                 #print '    state popping "%s"="%s"' % (key, curValue)
   104                 #print '    state popping "%s"="%s"' % (key, curValue)
   105                 if key == 'transform':
   105                 if key == 'transform':
   123     def __setitem__(self,key,value):
   123     def __setitem__(self,key,value):
   124         "sets the complete graphics state value of key to value"
   124         "sets the complete graphics state value of key to value"
   125         self._combined[-1][key] = value
   125         self._combined[-1][key] = value
   126 
   126 
   127 def testStateTracker():
   127 def testStateTracker():
   128     print 'Testing state tracker'
   128     print('Testing state tracker')
   129     defaults = {'fillColor':None, 'strokeColor':None,'fontName':None, 'transform':[1,0,0,1,0,0]}
   129     defaults = {'fillColor':None, 'strokeColor':None,'fontName':None, 'transform':[1,0,0,1,0,0]}
   130     from reportlab.graphics.shapes import _baseGFontName
   130     from reportlab.graphics.shapes import _baseGFontName
   131     deltas = [
   131     deltas = [
   132         {'fillColor':'red'},
   132         {'fillColor':'red'},
   133         {'fillColor':'green', 'strokeColor':'blue','fontName':_baseGFontName},
   133         {'fillColor':'green', 'strokeColor':'blue','fontName':_baseGFontName},
   135         {'transform':[0.5,0,0,0.5,2,3]},
   135         {'transform':[0.5,0,0,0.5,2,3]},
   136         {'strokeColor':'red'}
   136         {'strokeColor':'red'}
   137         ]
   137         ]
   138 
   138 
   139     st = StateTracker(defaults)
   139     st = StateTracker(defaults)
   140     print 'initial:', st.getState()
   140     print('initial:', st.getState())
   141     print
   141     print()
   142     for delta in deltas:
   142     for delta in deltas:
   143         print 'pushing:', delta
   143         print('pushing:', delta)
   144         st.push(delta)
   144         st.push(delta)
   145         print 'state:  ',st.getState(),'\n'
   145         print('state:  ',st.getState(),'\n')
   146 
   146 
   147     for delta in deltas:
   147     for delta in deltas:
   148         print 'popping:',st.pop()
   148         print('popping:',st.pop())
   149         print 'state:  ',st.getState(),'\n'
   149         print('state:  ',st.getState(),'\n')
   150 
   150 
   151 
   151 
   152 def _expandUserNode(node,canvas):
   152 def _expandUserNode(node,canvas):
   153     if isinstance(node, UserNode):
   153     if isinstance(node, UserNode):
   154         try:
   154         try:
   179     def __init__(self):
   179     def __init__(self):
   180         self._tracker = StateTracker()
   180         self._tracker = StateTracker()
   181         self._nodeStack = []   #track nodes visited
   181         self._nodeStack = []   #track nodes visited
   182 
   182 
   183     def undefined(self, operation):
   183     def undefined(self, operation):
   184         raise ValueError, "%s operation not defined at superclass class=%s" %(operation, self.__class__)
   184         raise ValueError("%s operation not defined at superclass class=%s" %(operation, self.__class__))
   185 
   185 
   186     def draw(self, drawing, canvas, x=0, y=0, showBoundary=rl_config._unset_):
   186     def draw(self, drawing, canvas, x=0, y=0, showBoundary=rl_config._unset_):
   187         """This is the top level function, which draws the drawing at the given
   187         """This is the top level function, which draws the drawing at the given
   188         location. The recursive part is handled by drawNode."""
   188         location. The recursive part is handled by drawNode."""
   189         #stash references for ease of  communication
   189         #stash references for ease of  communication
   228         and replace them with their calculated values.
   228         and replace them with their calculated values.
   229         Generally things may look at the drawing or their
   229         Generally things may look at the drawing or their
   230         parent.
   230         parent.
   231         
   231         
   232         """
   232         """
   233         for (key, value) in node.__dict__.items():
   233         for (key, value) in list(node.__dict__.items()):
   234             if isinstance(value, DerivedValue):
   234             if isinstance(value, DerivedValue):
   235                 #just replace with default for key?
   235                 #just replace with default for key?
   236                 #print '    fillDerivedValues(%s)' % key
   236                 #print '    fillDerivedValues(%s)' % key
   237                 newValue = value.getValue(self, key)
   237                 newValue = value.getValue(self, key)
   238                 #print '   got value of %s' % newValue
   238                 #print '   got value of %s' % newValue
   279             elif isinstance(node, Group):
   279             elif isinstance(node, Group):
   280                 self.drawGroup(node)
   280                 self.drawGroup(node)
   281             elif isinstance(node, Wedge):
   281             elif isinstance(node, Wedge):
   282                 self.drawWedge(node)
   282                 self.drawWedge(node)
   283             else:
   283             else:
   284                 print 'DrawingError','Unexpected element %s in drawing!' % str(node)
   284                 print('DrawingError','Unexpected element %s in drawing!' % str(node))
   285         finally:
   285         finally:
   286             if not ocanvas: del node._canvas
   286             if not ocanvas: del node._canvas
   287 
   287 
   288     _restores = {'stroke':'_stroke','stroke_width': '_lineWidth','stroke_linecap':'_lineCap',
   288     _restores = {'stroke':'_stroke','stroke_width': '_lineWidth','stroke_linecap':'_lineCap',
   289                 'stroke_linejoin':'_lineJoin','fill':'_fill','font_family':'_font',
   289                 'stroke_linejoin':'_lineJoin','fill':'_fill','font_family':'_font',
   348         """This takes a set of states, and outputs the operators
   348         """This takes a set of states, and outputs the operators
   349         needed to set those properties"""
   349         needed to set those properties"""
   350         self.undefined("applyStateChanges")
   350         self.undefined("applyStateChanges")
   351 
   351 
   352 if __name__=='__main__':
   352 if __name__=='__main__':
   353     print "this file has no script interpretation"
   353     print("this file has no script interpretation")
   354     print __doc__
   354     print(__doc__)