reportlab/graphics/renderPS.py
author rgbecker
Wed, 15 Mar 2006 16:47:27 +0000
changeset 2574 d81949596070
parent 2570 7b93b3b42e51
child 2678 38d18a697cd0
permissions -rw-r--r--
reportlab: changes to accomodate barcodes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2208
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
817
8c3a399effda License text changes
rgbecker
parents: 760
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2208
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/renderPS.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2208
diff changeset
     4
__version__=''' $Id$ '''
1582
47e215fe69c9 Use general StringIO instance interface
rgbecker
parents: 1454
diff changeset
     5
import string, types
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
     6
from reportlab.pdfbase.pdfmetrics import getFont, stringWidth # for font info
1582
47e215fe69c9 Use general StringIO instance interface
rgbecker
parents: 1454
diff changeset
     7
from reportlab.lib.utils import fp_str, getStringIO
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
     8
from reportlab.lib.colors import black
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
     9
from reportlab.graphics.renderbase import Renderer, StateTracker, getStateDelta, renderScaledDrawing
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    10
from reportlab.graphics.shapes import STATE_DEFAULTS
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    11
import math
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    12
from types import StringType
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    13
from operator import getitem
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
    14
from reportlab import rl_config
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    15
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    16
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    17
# we need to create encoding vectors for each font we use, or they will
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    18
# come out in Adobe's old StandardEncoding, which NOBODY uses.
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    19
PS_WinAnsiEncoding="""
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    20
/RE { %def
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    21
  findfont begin
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    22
  currentdict dup length dict begin
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    23
    { %forall
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    24
      1 index /FID ne { def } { pop pop } ifelse
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    25
    } forall
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    26
    /FontName exch def dup length 0 ne { %if
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    27
      /Encoding Encoding 256 array copy def
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    28
      0 exch { %forall
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    29
        dup type /nametype eq { %ifelse
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    30
          Encoding 2 index 2 index put
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    31
          pop 1 add
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    32
        }{ %else
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    33
          exch pop
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    34
        } ifelse
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    35
      } forall
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    36
    } if pop
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    37
  currentdict dup end end
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    38
  /FontName get exch definefont pop
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    39
} bind def
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    40
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
    41
/WinAnsiEncoding [
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    42
  39/quotesingle 96/grave 128/euro 130/quotesinglbase/florin/quotedblbase
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    43
  /ellipsis/dagger/daggerdbl/circumflex/perthousand
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    44
  /Scaron/guilsinglleft/OE 145/quoteleft/quoteright
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    45
  /quotedblleft/quotedblright/bullet/endash/emdash
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    46
  /tilde/trademark/scaron/guilsinglright/oe/dotlessi
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    47
  159/Ydieresis 164/currency 166/brokenbar 168/dieresis/copyright
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    48
  /ordfeminine 172/logicalnot 174/registered/macron/ring
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    49
  177/plusminus/twosuperior/threesuperior/acute/mu
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    50
  183/periodcentered/cedilla/onesuperior/ordmasculine
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    51
  188/onequarter/onehalf/threequarters 192/Agrave/Aacute
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    52
  /Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    53
  /Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    54
  /Icircumflex/Idieresis/Eth/Ntilde/Ograve/Oacute
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    55
  /Ocircumflex/Otilde/Odieresis/multiply/Oslash
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    56
  /Ugrave/Uacute/Ucircumflex/Udieresis/Yacute/Thorn
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    57
  /germandbls/agrave/aacute/acircumflex/atilde/adieresis
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    58
  /aring/ae/ccedilla/egrave/eacute/ecircumflex
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    59
  /edieresis/igrave/iacute/icircumflex/idieresis
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    60
  /eth/ntilde/ograve/oacute/ocircumflex/otilde
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    61
  /odieresis/divide/oslash/ugrave/uacute/ucircumflex
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    62
  /udieresis/yacute/thorn/ydieresis
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    63
] def
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    64
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    65
"""
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    66
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    67
class PSCanvas:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    68
    def __init__(self,size=(300,300), PostScriptLevel=2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    69
        self.width, self.height = size
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    70
        xtraState = []
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    71
        self._xtraState_push = xtraState.append
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    72
        self._xtraState_pop = xtraState.pop
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    73
        self.comments = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    74
        self.code = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    75
        self._sep = '\n'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    76
        self._strokeColor = self._fillColor = self._lineWidth = \
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    77
            self._font = self._fontSize = self._lineCap = \
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    78
            self._lineJoin = self._color = None
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
    79
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
    80
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
    81
        self._fontsUsed =   [] # track them as we go
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
    82
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    83
        self.setFont(STATE_DEFAULTS['fontName'],STATE_DEFAULTS['fontSize'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    84
        self.setStrokeColor(STATE_DEFAULTS['strokeColor'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    85
        self.setLineCap(2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    86
        self.setLineJoin(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    87
        self.setLineWidth(1)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
    88
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    89
        self.PostScriptLevel=PostScriptLevel
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
    90
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    91
    def comment(self,msg):
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    92
        if self.comments: self.code.append('%'+msg)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
    93
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    94
    def drawImage(self, image, x1,y1, x2=None,y2=None): # Postscript Level2 version
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    95
        # select between postscript level 1 or level 2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    96
        if PostScriptLevel==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    97
            self._drawImageLevel1(image, x1,y1, x2=None,y2=None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    98
        elif PostScriptLevel == 2 :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    99
            self._drawImageLevel2(image, x1,y1, x2=None,y2=None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   100
        else :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   101
            raise 'PostScriptLevelException'
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   102
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   103
    def clear(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   104
        self.code.append('showpage') # ugh, this makes no sense oh well.
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   105
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   106
    def save(self,f=None):
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   107
        if not hasattr(f,'write'):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   108
            file = open(f,'wb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   109
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   110
            file = f
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   111
        if self.code[-1]!='showpage': self.clear()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   112
        self.code.insert(0,'''\
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   113
%%!PS-Adobe-3.0 EPSF-3.0
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   114
%%%%BoundingBox: 0 0 %d %d
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   115
%%%% Initialization:
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   116
/m {moveto} bind def
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   117
/l {lineto} bind def
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   118
/c {curveto} bind def
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   119
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   120
%s
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   121
''' % (self.width,self.height, PS_WinAnsiEncoding))
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   122
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   123
        # for each font used, reencode the vectors
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   124
        fontReencode = []
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   125
        for fontName in self._fontsUsed:
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   126
            fontReencode.append('WinAnsiEncoding /%s /%s RE' % (fontName, fontName))
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
   127
        self.code.insert(1, string.join(fontReencode, self._sep))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   128
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   129
        file.write(string.join(self.code,self._sep))
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   130
        if file is not f:
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   131
            file.close()
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   132
            from reportlab.lib.utils import markfilename
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   133
            markfilename(f,creatorcode='XPR3',filetype='EPSF')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   134
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   135
    def saveState(self):
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
   136
        self._xtraState_push((self._fontCodeLoc,))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   137
        self.code.append('gsave')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   138
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   139
    def restoreState(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   140
        self.code.append('grestore')
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
   141
        self._fontCodeLoc, = self._xtraState_pop()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   142
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   143
    def stringWidth(self, s, font=None, fontSize=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   144
        """Return the logical width of the string if it were drawn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   145
        in the current font (defaults to self.font)."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   146
        font = font or self._font
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   147
        fontSize = fontSize or self._fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   148
        return stringWidth(s, font, fontSize)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   149
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   150
    def setLineCap(self,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   151
        if self._lineCap!=v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   152
            self._lineCap = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   153
            self.code.append('%d setlinecap'%v)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   154
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   155
    def setLineJoin(self,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   156
        if self._lineJoin!=v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   157
            self._lineJoin = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   158
            self.code.append('%d setlinejoin'%v)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   159
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   160
    def setDash(self, array=[], phase=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   161
        """Two notations.  pass two numbers, or an array and phase"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   162
        # copied and modified from reportlab.canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   163
        psoperation = "setdash"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   164
        if type(array) == types.IntType or type(array) == types.FloatType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   165
            self._code.append('[%s %s] 0 %s' % (array, phase, psoperation))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   166
        elif type(array) == types.ListType or type(array) == types.TupleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   167
            assert phase >= 0, "phase is a length in user space"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   168
            textarray = string.join(map(str, array))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   169
            self.code.append('[%s] %s %s' % (textarray, phase, psoperation))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   170
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   171
    def setStrokeColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   172
        self._strokeColor = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   173
        self.setColor(color)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   174
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   175
    def setColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   176
        if self._color!=color:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   177
            self._color = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   178
            if color:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   179
                if hasattr(color, "cyan"):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   180
                    self.code.append('%s setcmykcolor' % fp_str(color.cyan, color.magenta, color.yellow, color.black))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   181
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   182
                    self.code.append('%s setrgbcolor' % fp_str(color.red, color.green, color.blue))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   183
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   184
    def setFillColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   185
        self._fillColor = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   186
        self.setColor(color)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   187
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   188
    def setLineWidth(self, width):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   189
        if width != self._lineWidth:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   190
            self._lineWidth = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   191
            self.code.append('%s setlinewidth' % width)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   192
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   193
    def setFont(self,font,fontSize,leading=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   194
        if self._font!=font or self._fontSize!=fontSize:
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   195
            self._fontCodeLoc = len(self.code)
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   196
            self._font = font
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   197
            self._fontSize = fontSize
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   198
            self.code.append('')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   199
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   200
    def line(self, x1, y1, x2, y2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   201
        if self._strokeColor != None:
2440
64847841717e renderPS.py: fix buglet in color setting
rgbecker
parents: 2405
diff changeset
   202
            self.setColor(self._strokeColor)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   203
            self.code.append('%s m %s l stroke' % (fp_str(x1, y1), fp_str(x2, y2)))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   204
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   205
    def _escape(self, s):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   206
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   207
        return a copy of string s with special characters in postscript strings
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   208
        escaped with backslashes.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   209
        Have not handled characters that are converted normally in python strings
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   210
        i.e. \n -> newline
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   211
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   212
        str = string.replace(s, chr(0x5C), r'\\' )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   213
        str = string.replace(str, '(', '\(' )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   214
        str = string.replace(str, ')', '\)')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   215
        return str
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   216
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   217
    def drawString(self, x, y, s, angle=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   218
        if self._fillColor != None:
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   219
            if not self.code[self._fontCodeLoc]:
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   220
                psName = getFont(self._font).face.name
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   221
                self.code[self._fontCodeLoc]='(%s) findfont %s scalefont setfont' % (psName,fp_str(self._fontSize))
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   222
                if psName not in self._fontsUsed:
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   223
                    self._fontsUsed.append(psName)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   224
            self.setColor(self._fillColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   225
            s = self._escape(s)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   226
## before inverting...
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   227
##            if angle == 0 :   # do special case of angle = 0 first. Avoids a bunch of gsave/grestore ops
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   228
##                self.code.append('%s m 1 -1 scale (%s) show 1 -1 scale' % (fp_str(x,y),s))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   229
##            else : # general case, rotated text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   230
##                self.code.append('gsave %s %s translate %s rotate' % (x,y,angle))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   231
##                self.code.append('0 0 m 1 -1 scale (%s) show' % s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   232
##                self.code.append('grestore')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   233
            if angle == 0 :   # do special case of angle = 0 first. Avoids a bunch of gsave/grestore ops
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   234
                self.code.append('%s m (%s) show ' % (fp_str(x,y),s))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   235
            else : # general case, rotated text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   236
                self.code.append('gsave %s %s translate %s rotate' % (x,y,angle))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   237
                self.code.append('0 0 m (%s) show' % s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   238
                self.code.append('grestore')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   239
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   240
    def drawCentredString(self, x, y, text, text_anchor='middle'):
2440
64847841717e renderPS.py: fix buglet in color setting
rgbecker
parents: 2405
diff changeset
   241
        if self._fillColor is not None:
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   242
            textLen = stringWidth(text, self._font,self._fontSize)
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   243
            if text_anchor=='end':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2570
diff changeset
   244
                x -= textLen
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   245
            elif text_anchor=='middle':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2570
diff changeset
   246
                x -= textLen/2.
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   247
            self.drawString(x,y,text)
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   248
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   249
    def drawRightString(self, text, x, y):
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   250
        self.drawCentredString(text,x,y,text_anchor='end')
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   251
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   252
    def drawCurve(self, x1, y1, x2, y2, x3, y3, x4, y4, closed=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   253
        codeline = '%s m %s curveto'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   254
        data = (fp_str(x1, y1), fp_str(x2, y2, x3, y3, x4, y4))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   255
        if self._fillColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   256
            self.setColor(self._fillColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   257
            self.code.append((codeline % data) + ' eofill')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   258
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   259
            self.setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   260
            self.code.append((codeline % data)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   261
                            + ((closed and ' closepath') or '')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   262
                            + ' stroke')
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   263
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   264
    ########################################################################################
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   265
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   266
    def rect(self, x1,y1, x2,y2, stroke=1, fill=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   267
        "Draw a rectangle between x1,y1, and x2,y2"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   268
        # Path is drawn in counter-clockwise direction"
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   269
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   270
        x1, x2 = min(x1,x2), max(x1, x2) # from piddle.py
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   271
        y1, y2 = min(y1,y2), max(y1, y2)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   272
        self.polygon(((x1,y1),(x2,y1),(x2,y2),(x1,y2)), closed=1, stroke=stroke, fill = fill)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   273
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   274
    def roundRect(self, x1,y1, x2,y2, rx=8, ry=8):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   275
        """Draw a rounded rectangle between x1,y1, and x2,y2,
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   276
        with corners inset as ellipses with x radius rx and y radius ry.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   277
        These should have x1<x2, y1<y2, rx>0, and ry>0."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   278
        # Path is drawn in counter-clockwise direction
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   279
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   280
        x1, x2 = min(x1,x2), max(x1, x2) # from piddle.py
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   281
        y1, y2 = min(y1,y2), max(y1, y2)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   282
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   283
        # Note: arcto command draws a line from current point to beginning of arc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   284
        # save current matrix, translate to center of ellipse, scale by rx ry, and draw
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   285
        # a circle of unit radius in counterclockwise dir, return to original matrix
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   286
        # arguments are (cx, cy, rx, ry, startAngle, endAngle)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   287
        ellipsePath = 'matrix currentmatrix %s %s translate %s %s scale 0 0 1 %s %s arc setmatrix'
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   288
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   289
        # choice between newpath and moveTo beginning of arc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   290
        # go with newpath for precision, does this violate any assumptions in code???
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   291
        rrCode = ['newpath'] # Round Rect code path
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   292
        # upper left corner ellipse is first
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   293
        rrCode.append(ellipsePath % (x1+rx, y1+ry, rx, -ry, 90, 180))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   294
        rrCode.append(ellipsePath % (x1+rx, y2-ry, rx, -ry, 180, 270))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   295
        rrCode.append(ellipsePath % (x2-rx, y2-ry, rx, -ry, 270, 360))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   296
        rrCode.append(ellipsePath % (x2-rx, y1+ry, rx, -ry, 0,  90) )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   297
        rrCode.append('closepath')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   298
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   299
        self._fillAndStroke(rrCode)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   300
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   301
    def ellipse(self, x1,y1, x2,y2):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   302
        """Draw an orthogonal ellipse inscribed within the rectangle x1,y1,x2,y2.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   303
        These should have x1<x2 and y1<y2."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   304
        #Just invoke drawArc to actually draw the ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   305
        self.drawArc(x1,y1, x2,y2)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   306
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   307
    def circle(self, xc, yc, r):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   308
        self.ellipse(xc-r,yc-r, xc+r,yc+r)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   309
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   310
    def drawArc(self, x1,y1, x2,y2, startAng=0, extent=360, fromcenter=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   311
        """Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   312
        starting at startAng degrees and covering extent degrees.   Angles
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   313
        start with 0 to the right (+x) and increase counter-clockwise.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   314
        These should have x1<x2 and y1<y2."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   315
        #calculate centre of ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   316
        #print "x1,y1,x2,y2,startAng,extent,fromcenter", x1,y1,x2,y2,startAng,extent,fromcenter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   317
        cx, cy = (x1+x2)/2.0, (y1+y2)/2.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   318
        rx, ry = (x2-x1)/2.0, (y2-y1)/2.0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   319
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   320
        codeline = self._genArcCode(x1, y1, x2, y2, startAng, extent)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   321
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   322
        startAngleRadians = math.pi*startAng/180.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   323
        extentRadians = math.pi*extent/180.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   324
        endAngleRadians = startAngleRadians + extentRadians
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   325
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   326
        codelineAppended = 0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   327
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   328
        # fill portion
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   329
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   330
        if self._fillColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   331
            self.setColor(self._fillColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   332
            self.code.append(codeline)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   333
            codelineAppended = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   334
            if self._strokeColor!=None: self.code.append('gsave')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   335
            self.lineTo(cx,cy)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   336
            self.code.append('eofill')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   337
            if self._strokeColor!=None: self.code.append('grestore')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   338
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   339
        # stroke portion
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   340
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   341
            # this is a bit hacked up.  There is certainly a better way...
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   342
            self.setColor(self._strokeColor)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   343
            (startx, starty) = (cx+rx*math.cos(startAngleRadians), cy+ry*math.sin(startAngleRadians))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   344
            if not codelineAppended:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   345
                self.code.append(codeline)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   346
            if fromcenter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   347
                # move to center
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   348
                self.lineTo(cx,cy)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   349
                self.lineTo(startx, starty)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   350
                self.code.append('closepath')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   351
            self.code.append('stroke')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   352
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   353
    def _genArcCode(self, x1, y1, x2, y2, startAng, extent):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   354
        "Calculate the path for an arc inscribed in rectangle defined by (x1,y1),(x2,y2)"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   355
        #calculate semi-minor and semi-major axes of ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   356
        xScale = abs((x2-x1)/2.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   357
        yScale = abs((y2-y1)/2.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   358
        #calculate centre of ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   359
        x, y = (x1+x2)/2.0, (y1+y2)/2.0
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   360
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   361
        codeline = 'matrix currentmatrix %s %s translate %s %s scale 0 0 1 %s %s %s setmatrix'
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   362
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   363
        if extent >= 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   364
            arc='arc'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   365
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   366
            arc='arcn'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   367
        data = (x,y, xScale, yScale, startAng, startAng+extent, arc)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   368
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   369
        return codeline % data
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   370
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   371
    def polygon(self, p, closed=0, stroke=1, fill=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   372
        assert len(p) >= 2, 'Polygon must have 2 or more points'
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   373
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   374
        start = p[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   375
        p = p[1:]
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   376
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   377
        polyCode = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   378
        polyCode.append("%s m" % fp_str(start))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   379
        for point in p:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   380
            polyCode.append("%s l" % fp_str(point))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   381
        if closed:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   382
            polyCode.append("closepath")
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   383
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   384
        self._fillAndStroke(polyCode,stroke=stroke,fill=fill)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   385
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   386
    def lines(self, lineList, color=None, width=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   387
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   388
            self._setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   389
            codeline = '%s m %s l stroke'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   390
            for line in lineList:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   391
                self.code.append(codeline % (fp_str(line[0]),fp_str(line[1])))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   392
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   393
    def moveTo(self,x,y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   394
        self.code.append('%s m' % fp_str(x, y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   395
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   396
    def lineTo(self,x,y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   397
        self.code.append('%s l' % fp_str(x, y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   398
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   399
    def curveTo(self,x1,y1,x2,y2,x3,y3):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   400
        self.code.append('%s c' % fp_str(x1,y1,x2,y2,x3,y3))
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   401
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   402
    def closePath(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   403
        self.code.append('closepath')
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   404
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   405
    def polyLine(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   406
        assert len(p) >= 1, 'Polyline must have 1 or more points'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   407
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   408
            self.setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   409
            self.moveTo(p[0][0], p[0][1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   410
            for t in p[1:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   411
                self.lineTo(t[0], t[1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   412
            self.code.append('stroke')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   413
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   414
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   415
    def drawFigure(self, partList, closed=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   416
        figureCode = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   417
        first = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   418
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   419
        for part in partList:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   420
            op = part[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   421
            args = list(part[1:])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   422
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   423
            if op == figureLine:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   424
                if first:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   425
                    first = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   426
                    figureCode.append("%s m" % fp_str(args[:2]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   427
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   428
                    figureCode.append("%s l" % fp_str(args[:2]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   429
                figureCode.append("%s l" % fp_str(args[2:]))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   430
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   431
            elif op == figureArc:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   432
                first = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   433
                x1,y1,x2,y2,startAngle,extent = args[:6]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   434
                figureCode.append(self._genArcCode(x1,y1,x2,y2,startAngle,extent))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   435
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   436
            elif op == figureCurve:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   437
                if first:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   438
                    first = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   439
                    figureCode.append("%s m" % fp_str(args[:2]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   440
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   441
                    figureCode.append("%s l" % fp_str(args[:2]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   442
                figureCode.append("%s curveto" % fp_str(args[2:]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   443
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   444
                raise TypeError, "unknown figure operator: "+op
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   445
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   446
        if closed:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   447
            figureCode.append("closepath")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   448
        self._fillAndStroke(figureCode)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   449
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   450
    def _fillAndStroke(self,code,clip=0,fill=1,stroke=1):
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   451
        fill = self._fillColor and fill
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   452
        stroke = self._strokeColor and stroke
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   453
        if fill or stroke or clip:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   454
            self.code.extend(code)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   455
            if fill:
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   456
                if stroke or clip: self.code.append("gsave")
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   457
                self.setColor(self._fillColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   458
                self.code.append("eofill")
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   459
                if stroke or clip: self.code.append("grestore")
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   460
            if stroke:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   461
                if clip: self.code.append("gsave")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   462
                self.setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   463
                self.code.append("stroke")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   464
                if clip: self.code.append("grestore")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   465
            if clip:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   466
                self.code.append("clip")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   467
                self.code.append("newpath")
1230
def1b1529c88 *** empty log message ***
kern
parents: 1082
diff changeset
   468
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   469
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   470
    def translate(self,x,y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   471
        self.code.append('%s translate' % fp_str(x,y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   472
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   473
    def scale(self,x,y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   474
        self.code.append('%s scale' % fp_str(x,y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   475
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   476
    def transform(self,a,b,c,d,e,f):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   477
        self.code.append('[%s] concat' % fp_str(a,b,c,d,e,f))
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   478
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   479
    def _drawTimeResize(self,w,h):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   480
        '''if this is used we're probably in the wrong world'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   481
        self.width, self.height = w, h
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   482
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   483
    ############################################################################################
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   484
    # drawImage(self. image, x1, y1, x2=None, y2=None) is now defined by either _drawImageLevel1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   485
    #    ._drawImageLevel2, the choice is made in .__init__ depending on option
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   486
    def _drawImageLevel1(self, image, x1, y1, x2=None,y2=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   487
        # Postscript Level1 version available for fallback mode when Level2 doesn't work
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   488
        """drawImage(self,image,x1,y1,x2=None,y2=None) : If x2 and y2 are ommitted, they are
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   489
        calculated from image size. (x1,y1) is upper left of image, (x2,y2) is lower right of
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   490
        image in piddle coordinates."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   491
        # For now let's start with 24 bit RGB images (following piddlePDF again)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   492
        component_depth = 8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   493
        myimage = image.convert('RGB')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   494
        imgwidth, imgheight = myimage.size
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   495
        if not x2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   496
            x2 = imgwidth + x1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   497
        if not y2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   498
            y2 = y1 + imgheight
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   499
        drawwidth = x2 - x1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   500
        drawheight = y2 - y1
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   501
        #print 'Image size (%d, %d); Draw size (%d, %d)' % (imgwidth, imgheight, drawwidth, drawheight)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   502
        # now I need to tell postscript how big image is
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   503
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   504
        # "image operators assume that they receive sample data from
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   505
        # their data source in x-axis major index order.  The coordinate
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   506
        # of the lower-left corner of the first sample is (0,0), of the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   507
        # second (1,0) and so on" -PS2 ref manual p. 215
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   508
        #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   509
        # The ImageMatrix maps unit squre of user space to boundary of the source image
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   510
        #
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   511
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   512
        # The CurrentTransformationMatrix (CTM) maps the unit square of
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   513
        # user space to the rect...on the page that is to receive the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   514
        # image. A common ImageMatrix is [width 0 0 -height 0 height]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   515
        # (for a left to right, top to bottom image )
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   516
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   517
        # first let's map the user coordinates start at offset x1,y1 on page
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   518
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   519
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   520
            'gsave',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   521
            '%s %s translate' % (x1,-y1 - drawheight), # need to start are lower left of image
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   522
            '%s %s scale' % (drawwidth,drawheight),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   523
            '/scanline %d 3 mul string def' % imgwidth  # scanline by multiples of image width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   524
            ])
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   525
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   526
        # now push the dimensions and depth info onto the stack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   527
        # and push the ImageMatrix to map the source to the target rectangle (see above)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   528
        # finally specify source (PS2 pp. 225 ) and by exmample
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   529
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   530
            '%s %s %s' % (imgwidth, imgheight, component_depth),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   531
            '[%s %s %s %s %s %s]' % (imgwidth, 0, 0, -imgheight, 0, imgheight),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   532
            '{ currentfile scanline readhexstring pop } false 3',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   533
            'colorimage '
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   534
            ])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   535
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   536
        # data source output--now we just need to deliver a hex encode
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   537
        # series of lines of the right overall size can follow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   538
        # piddlePDF again
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   539
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   540
        rawimage = myimage.tostring()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   541
        assert(len(rawimage) == imgwidth*imgheight, 'Wrong amount of data for image')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   542
        #compressed = zlib.compress(rawimage) # no zlib at moment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   543
        hex_encoded = self._AsciiHexEncode(rawimage)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   544
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   545
        # write in blocks of 78 chars per line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   546
        outstream = getStringIO(hex_encoded)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   547
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   548
        dataline = outstream.read(78)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   549
        while dataline <> "":
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   550
            self.code.append(dataline)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   551
            dataline= outstream.read(78)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   552
        self.code.append('% end of image data') # for clarity
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   553
        self.code.append('grestore') # return coordinates to normal
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   554
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   555
    # end of drawImage
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   556
    def _AsciiHexEncode(self, input):  # also based on piddlePDF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   557
        "Helper function used by images"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   558
        output = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   559
        for char in input:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   560
            output.write('%02x' % ord(char))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   561
        return output.getvalue()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   562
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   563
    def _drawImageLevel2(self, image, x1,y1, x2=None,y2=None): # Postscript Level2 version
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   564
        '''At present we're handling only PIL'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   565
        ### what sort of image are we to draw
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   566
        if image.mode=='L' :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   567
            imBitsPerComponent = 8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   568
            imNumComponents = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   569
            myimage = image
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   570
        elif image.mode == '1':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   571
            myimage = image.convert('L')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   572
            imNumComponents = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   573
            myimage = image
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   574
        else :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   575
            myimage = image.convert('RGB')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   576
            imNumComponents = 3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   577
            imBitsPerComponent = 8
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   578
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   579
        imwidth, imheight = myimage.size
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   580
        if not x2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   581
            x2 = imwidth + x1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   582
        if not y2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   583
            y2 = y1 + imheight
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   584
        drawwidth = x2 - x1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   585
        drawheight = y2 - y1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   586
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   587
            'gsave',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   588
            '%s %s translate' % (x1,-y1 - drawheight), # need to start are lower left of image
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   589
            '%s %s scale' % (drawwidth,drawheight)])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   590
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   591
        if imNumComponents == 3 :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   592
            self.code.append('/DeviceRGB setcolorspace')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   593
        elif imNumComponents == 1 :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   594
            self.code.append('/DeviceGray setcolorspace')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   595
        # create the image dictionary
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   596
        self.code.append("""
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   597
<<
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   598
/ImageType 1
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   599
/Width %d /Height %d  %% dimensions of source image
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   600
/BitsPerComponent %d""" % (imwidth, imheight, imBitsPerComponent) )
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   601
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   602
        if imNumComponents == 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   603
            self.code.append('/Decode [0 1]')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   604
        if imNumComponents == 3:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   605
            self.code.append('/Decode [0 1 0 1 0 1]  %% decode color values normally')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   606
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   607
        self.code.extend([  '/ImageMatrix [%s 0 0 %s 0 %s]' % (imwidth, -imheight, imheight),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   608
                            '/DataSource currentfile /ASCIIHexDecode filter',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   609
                            '>> % End image dictionary',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   610
                            'image'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   611
        # after image operator just need to dump image dat to file as hexstring
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   612
        rawimage = myimage.tostring()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   613
        assert(len(rawimage) == imwidth*imheight, 'Wrong amount of data for image')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   614
        #compressed = zlib.compress(rawimage) # no zlib at moment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   615
        hex_encoded = self._AsciiHexEncode(rawimage)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   616
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   617
        # write in blocks of 78 chars per line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   618
        outstream = getStringIO(hex_encoded)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   619
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   620
        dataline = outstream.read(78)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   621
        while dataline <> "":
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   622
            self.code.append(dataline)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   623
            dataline= outstream.read(78)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   624
        self.code.append('> % end of image data') # > is EOD for hex encoded filterfor clarity
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   625
        self.code.append('grestore') # return coordinates to normal
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   626
604
c61e69d2721e Fixes for QIR barchart presentation
andy_robinson
parents: 587
diff changeset
   627
# renderpdf - draws them onto a canvas
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   628
"""Usage:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   629
    from reportlab.graphics import renderPS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   630
    renderPS.draw(drawing, canvas, x, y)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   631
Execute the script to see some test drawings."""
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   632
from shapes import *
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   633
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   634
# hack so we only get warnings once each
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   635
#warnOnce = WarnOnce()
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   636
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   637
# the main entry point for users...
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   638
def draw(drawing, canvas, x=0, y=0, showBoundary=rl_config.showBoundary):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   639
    """As it says"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   640
    R = _PSRenderer()
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
   641
    R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   642
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   643
def _pointsFromList(L):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   644
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   645
    given a list of coordinates [x0, y0, x1, y1....]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   646
    produce a list of points [(x0,y0), (y1,y0),....]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   647
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   648
    P=[]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   649
    for i in range(0,len(L),2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   650
        P.append((L[i],L[i+1]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   651
    return P
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   652
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   653
class _PSRenderer(Renderer):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   654
    """This draws onto a EPS document.  It needs to be a class
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   655
    rather than a function, as some EPS-specific state tracking is
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   656
    needed outside of the state info in the SVG model."""
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   657
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   658
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   659
        self._tracker = StateTracker()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   660
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   661
    def drawNode(self, node):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   662
        """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
   663
        in the tree"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   664
        self._canvas.comment('begin node %s'%`node`)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   665
        color = self._canvas._color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   666
        if not (isinstance(node, Path) and node.isClipPath):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   667
            self._canvas.saveState()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   668
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   669
        #apply state changes
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   670
        deltas = getStateDelta(node)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   671
        self._tracker.push(deltas)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   672
        self.applyStateChanges(deltas, {})
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   673
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   674
        #draw the object, or recurse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   675
        self.drawNodeDispatcher(node)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   676
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   677
        rDeltas = self._tracker.pop()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   678
        if not (isinstance(node, Path) and node.isClipPath):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   679
            self._canvas.restoreState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   680
        self._canvas.comment('end node %s'%`node`)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   681
        self._canvas._color = color
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   682
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   683
        #restore things we might have lost (without actually doing anything).
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   684
        for k, v in rDeltas.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   685
            if self._restores.has_key(k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   686
                setattr(self._canvas,self._restores[k],v)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   687
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   688
##  _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
   689
##              '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
   690
##              'font_size':'_fontSize'}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   691
    _restores = {'strokeColor':'_strokeColor','strokeWidth': '_lineWidth','strokeLineCap':'_lineCap',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   692
                'strokeLineJoin':'_lineJoin','fillColor':'_fillColor','fontName':'_font',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   693
                'fontSize':'_fontSize'}
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   694
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   695
    def drawRect(self, rect):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   696
        if rect.rx == rect.ry == 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   697
            #plain old rectangle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   698
            self._canvas.rect(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   699
                    rect.x, rect.y,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   700
                    rect.x+rect.width, rect.y+rect.height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   701
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   702
            #cheat and assume ry = rx; better to generalize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   703
            #pdfgen roundRect function.  TODO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   704
            self._canvas.roundRect(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   705
                    rect.x, rect.y,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   706
                    rect.x+rect.width, rect.y+rect.height, rect.rx, rect.ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   707
                    )
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   708
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   709
    def drawLine(self, line):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   710
        if self._canvas._strokeColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   711
            self._canvas.line(line.x1, line.y1, line.x2, line.y2)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   712
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   713
    def drawCircle(self, circle):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   714
        self._canvas.circle( circle.cx, circle.cy, circle.r)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   715
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   716
    def drawWedge(self, wedge):
1952
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   717
        yradius, radius1, yradius1 = wedge._xtraRadii()
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   718
        if (radius1==0 or radius1 is None) and (yradius1==0 or yradius1 is None):
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   719
            startangledegrees = wedge.startangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   720
            endangledegrees = wedge.endangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   721
            centerx= wedge.centerx
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   722
            centery = wedge.centery
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   723
            radius = wedge.radius
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   724
            extent = endangledegrees - startangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   725
            self._canvas.drawArc(centerx-radius, centery-yradius, centerx+radius, centery+yradius,
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   726
                startangledegrees, extent, fromcenter=1)
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   727
        else:
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   728
            self.drawPolygon(wedge.asPolygon())
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   729
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   730
    def drawPolyLine(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   731
        if self._canvas._strokeColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   732
            self._canvas.polyLine(_pointsFromList(p.points))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   733
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   734
    def drawEllipse(self, ellipse):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   735
        #need to convert to pdfgen's bounding box representation
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   736
        x1 = ellipse.cx - ellipse.rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   737
        x2 = ellipse.cx + ellipse.rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   738
        y1 = ellipse.cy - ellipse.ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   739
        y2 = ellipse.cy + ellipse.ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   740
        self._canvas.ellipse(x1,y1,x2,y2)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   741
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   742
    def drawPolygon(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   743
        self._canvas.polygon(_pointsFromList(p.points), closed=1)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   744
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   745
    def drawString(self, stringObj):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   746
        if self._canvas._fillColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   747
            S = self._tracker.getState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   748
            text_anchor, x, y, text = S['textAnchor'], stringObj.x,stringObj.y,stringObj.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   749
            if not text_anchor in ['start','inherited']:
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   750
                font, fontSize = S['fontName'], S['fontSize']
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   751
                textLen = stringWidth(text, font,fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   752
                if text_anchor=='end':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   753
                    x = x-textLen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   754
                elif text_anchor=='middle':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   755
                    x = x - textLen/2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   756
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   757
                    raise ValueError, 'bad value for text_anchor '+str(text_anchor)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   758
            self._canvas.drawString(x,y,text)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   759
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   760
    def drawPath(self, path):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   761
        from reportlab.graphics.shapes import _renderPath
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   762
        c = self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   763
        drawFuncs = (c.moveTo, c.lineTo, c.curveTo, c.closePath)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   764
        isClosed = _renderPath(path, drawFuncs)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   765
        if not isClosed:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   766
            c._fillColor = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   767
        c._fillAndStroke([], clip=path.isClipPath)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   768
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   769
    def applyStateChanges(self, delta, newState):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   770
        """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
   771
        needed to set those properties"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   772
        for key, value in delta.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   773
            if key == 'transform':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   774
                self._canvas.transform(value[0], value[1], value[2],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   775
                                 value[3], value[4], value[5])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   776
            elif key == 'strokeColor':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   777
                #this has different semantics in PDF to SVG;
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   778
                #we always have a color, and either do or do
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   779
                #not apply it; in SVG one can have a 'None' color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   780
                self._canvas.setStrokeColor(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   781
            elif key == 'strokeWidth':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   782
                self._canvas.setLineWidth(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   783
            elif key == 'strokeLineCap':  #0,1,2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   784
                self._canvas.setLineCap(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   785
            elif key == 'strokeLineJoin':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   786
                self._canvas.setLineJoin(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   787
            elif key == 'strokeDashArray':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   788
                if value:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   789
                    self._canvas.setDash(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   790
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   791
                    self._canvas.setDash()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   792
##          elif key == 'stroke_opacity':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   793
##              warnOnce('Stroke Opacity not supported yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   794
            elif key == 'fillColor':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   795
                #this has different semantics in PDF to SVG;
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   796
                #we always have a color, and either do or do
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   797
                #not apply it; in SVG one can have a 'None' color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   798
                self._canvas.setFillColor(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   799
##          elif key == 'fill_rule':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   800
##              warnOnce('Fill rules not done yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   801
##          elif key == 'fill_opacity':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   802
##              warnOnce('Fill opacity not done yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   803
            elif key in ['fontSize', 'fontName']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   804
                # both need setting together in PDF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   805
                # one or both might be in the deltas,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   806
                # so need to get whichever is missing
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   807
                fontname = delta.get('fontName', self._canvas._font)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   808
                fontsize = delta.get('fontSize', self._canvas._fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   809
                self._canvas.setFont(fontname, fontsize)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   810
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   811
    def drawImage(self, image):
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   812
        from reportlab.lib.utils import ImageReader
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   813
        im = ImageReader(image.path)
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   814
        x0 = image.x
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   815
        y0 = image.y
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   816
        x1 = image.width
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   817
        if x1 is not None: x1 += x0
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   818
        y1 = image.height
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   819
        if y1 is not None: y1 += y0
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   820
        self._canvas.drawImage(im._image,x0,y0,x1,y1)
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   821
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   822
def drawToFile(d,fn, showBoundary=rl_config.showBoundary):
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2493
diff changeset
   823
    d = renderScaledDrawing(d)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   824
    c = PSCanvas((d.width,d.height))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   825
    draw(d, c, 0, 0, showBoundary=showBoundary)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   826
    c.save(fn)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   827
1810
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   828
def drawToString(d, showBoundary=rl_config.showBoundary):
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   829
    "Returns a PS as a string in memory, without touching the disk"
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   830
    s = getStringIO()
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   831
    drawToFile(d, s, showBoundary=showBoundary)
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   832
    return s.getvalue()
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   833
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   834
#########################################################
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   835
#
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   836
#   test code.  First, defin a bunch of drawings.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   837
#   Routine to draw them comes at the end.
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   838
#
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   839
#########################################################
587
5ae563fe5235 Fixed group transform bug and added tests
andy_robinson
parents: 580
diff changeset
   840
def test(outdir='epsout'):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   841
    import os
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   842
    # print all drawings and their doc strings from the test
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   843
    # file
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   844
    if not os.path.isdir(outdir):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   845
        os.mkdir(outdir)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   846
    #grab all drawings from the test module
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   847
    import testshapes
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   848
    drawings = []
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   849
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   850
    for funcname in dir(testshapes):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   851
        #if funcname[0:11] == 'getDrawing2':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   852
        #    print 'hacked to only show drawing 2'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   853
        if funcname[0:10] == 'getDrawing':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   854
            drawing = eval('testshapes.' + funcname + '()')  #execute it
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   855
            docstring = eval('testshapes.' + funcname + '.__doc__')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   856
            drawings.append((drawing, docstring))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   857
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   858
    i = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   859
    for (d, docstring) in drawings:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   860
        filename = outdir + os.sep + 'renderPS_%d.eps'%i
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   861
        drawToFile(d,filename)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   862
        print 'saved', filename
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   863
        i = i + 1
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   864
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   865
if __name__=='__main__':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   866
    import sys
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   867
    if len(sys.argv)>1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   868
        outdir = sys.argv[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   869
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   870
        outdir = 'epsout'
1810
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   871
    test(outdir)