src/reportlab/graphics/renderPS.py
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3765 a7e7cd6e0003
equal deleted inserted replaced
3722:29c11b905751 3723:99aa837b6703
     4 __version__=''' $Id$ '''
     4 __version__=''' $Id$ '''
     5 __doc__="""Render drawing objects in Postscript"""
     5 __doc__="""Render drawing objects in Postscript"""
     6 
     6 
     7 import string, types
     7 import string, types
     8 from reportlab.pdfbase.pdfmetrics import getFont, stringWidth, unicode2T1 # for font info
     8 from reportlab.pdfbase.pdfmetrics import getFont, stringWidth, unicode2T1 # for font info
     9 from reportlab.lib.utils import fp_str, getStringIO
     9 from reportlab.lib.utils import fp_str, getBytesIO
    10 from reportlab.lib.colors import black
    10 from reportlab.lib.colors import black
    11 from reportlab.graphics.renderbase import Renderer, StateTracker, getStateDelta, renderScaledDrawing
    11 from reportlab.graphics.renderbase import Renderer, StateTracker, getStateDelta, renderScaledDrawing
    12 from reportlab.graphics.shapes import STATE_DEFAULTS
    12 from reportlab.graphics.shapes import STATE_DEFAULTS
    13 import math
    13 import math
    14 from types import StringType
    14 from types import StringType
   587         # piddlePDF again
   587         # piddlePDF again
   588         rawimage = myimage.tostring()
   588         rawimage = myimage.tostring()
   589         hex_encoded = self._AsciiHexEncode(rawimage)
   589         hex_encoded = self._AsciiHexEncode(rawimage)
   590 
   590 
   591         # write in blocks of 78 chars per line
   591         # write in blocks of 78 chars per line
   592         outstream = getStringIO(hex_encoded)
   592         outstream = getBytesIO(hex_encoded)
   593 
   593 
   594         dataline = outstream.read(78)
   594         dataline = outstream.read(78)
   595         while dataline != "":
   595         while dataline != "":
   596             self.code_append(dataline)
   596             self.code_append(dataline)
   597             dataline= outstream.read(78)
   597             dataline= outstream.read(78)
   599         self.code_append('grestore') # return coordinates to normal
   599         self.code_append('grestore') # return coordinates to normal
   600 
   600 
   601     # end of drawImage
   601     # end of drawImage
   602     def _AsciiHexEncode(self, input):  # also based on piddlePDF
   602     def _AsciiHexEncode(self, input):  # also based on piddlePDF
   603         "Helper function used by images"
   603         "Helper function used by images"
   604         output = getStringIO()
   604         output = getBytesIO()
   605         for char in input:
   605         for char in input:
   606             output.write('%02x' % ord(char))
   606             output.write('%02x' % ord(char))
   607         return output.getvalue()
   607         return output.getvalue()
   608 
   608 
   609     def _drawImageLevel2(self, image, x1,y1, x2=None,y2=None): # Postscript Level2 version
   609     def _drawImageLevel2(self, image, x1,y1, x2=None,y2=None): # Postscript Level2 version
   657         # after image operator just need to dump image dat to file as hexstring
   657         # after image operator just need to dump image dat to file as hexstring
   658         rawimage = myimage.tostring()
   658         rawimage = myimage.tostring()
   659         hex_encoded = self._AsciiHexEncode(rawimage)
   659         hex_encoded = self._AsciiHexEncode(rawimage)
   660 
   660 
   661         # write in blocks of 78 chars per line
   661         # write in blocks of 78 chars per line
   662         outstream = getStringIO(hex_encoded)
   662         outstream = getBytesIO(hex_encoded)
   663 
   663 
   664         dataline = outstream.read(78)
   664         dataline = outstream.read(78)
   665         while dataline != "":
   665         while dataline != "":
   666             self.code_append(dataline)
   666             self.code_append(dataline)
   667             dataline= outstream.read(78)
   667             dataline= outstream.read(78)
   724             self._canvas.restoreState()
   724             self._canvas.restoreState()
   725         self._canvas.comment('end node %r'%node)
   725         self._canvas.comment('end node %r'%node)
   726         self._canvas._color = color
   726         self._canvas._color = color
   727 
   727 
   728         #restore things we might have lost (without actually doing anything).
   728         #restore things we might have lost (without actually doing anything).
   729         for k, v in list(rDeltas.items()):
   729         for k, v in rDeltas.items():
   730             if k in self._restores:
   730             if k in self._restores:
   731                 setattr(self._canvas,self._restores[k],v)
   731                 setattr(self._canvas,self._restores[k],v)
   732 
   732 
   733 ##  _restores = {'stroke':'_stroke','stroke_width': '_lineWidth','stroke_linecap':'_lineCap',
   733 ##  _restores = {'stroke':'_stroke','stroke_width': '_lineWidth','stroke_linecap':'_lineCap',
   734 ##              'stroke_linejoin':'_lineJoin','fill':'_fill','font_family':'_font',
   734 ##              'stroke_linejoin':'_lineJoin','fill':'_fill','font_family':'_font',
   814         c._fillAndStroke([], clip=path.isClipPath)
   814         c._fillAndStroke([], clip=path.isClipPath)
   815 
   815 
   816     def applyStateChanges(self, delta, newState):
   816     def applyStateChanges(self, delta, newState):
   817         """This takes a set of states, and outputs the operators
   817         """This takes a set of states, and outputs the operators
   818         needed to set those properties"""
   818         needed to set those properties"""
   819         for key, value in list(delta.items()):
   819         for key, value in delta.items():
   820             if key == 'transform':
   820             if key == 'transform':
   821                 self._canvas.transform(value[0], value[1], value[2],
   821                 self._canvas.transform(value[0], value[1], value[2],
   822                                  value[3], value[4], value[5])
   822                                  value[3], value[4], value[5])
   823             elif key == 'strokeColor':
   823             elif key == 'strokeColor':
   824                 #this has different semantics in PDF to SVG;
   824                 #this has different semantics in PDF to SVG;
   877     draw(d, c, 0, 0, showBoundary=showBoundary)
   877     draw(d, c, 0, 0, showBoundary=showBoundary)
   878     c.save(fn)
   878     c.save(fn)
   879 
   879 
   880 def drawToString(d, showBoundary=rl_config.showBoundary):
   880 def drawToString(d, showBoundary=rl_config.showBoundary):
   881     "Returns a PS as a string in memory, without touching the disk"
   881     "Returns a PS as a string in memory, without touching the disk"
   882     s = getStringIO()
   882     s = getBytesIO()
   883     drawToFile(d, s, showBoundary=showBoundary)
   883     drawToFile(d, s, showBoundary=showBoundary)
   884     return s.getvalue()
   884     return s.getvalue()
   885 
   885 
   886 #########################################################
   886 #########################################################
   887 #
   887 #