src/reportlab/graphics/renderPS.py
author robin
Thu, 24 Oct 2019 16:07:15 +0100
changeset 4551 d357e2acc856
parent 4528 e09377955af8
permissions -rw-r--r--
improve usage of eval/exec; version --> 3.5.32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4325
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
817
8c3a399effda License text changes
rgbecker
parents: 760
diff changeset
     2
#see license.txt for license details
4528
e09377955af8 try to eliminate changed bitbucket.org references
robin
parents: 4447
diff changeset
     3
#history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/graphics/renderPS.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4132
diff changeset
     4
__version__='3.3.0'
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2994
diff changeset
     5
__doc__="""Render drawing objects in Postscript"""
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2994
diff changeset
     6
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
     7
from reportlab.pdfbase.pdfmetrics import getFont, stringWidth, unicode2T1 # for font info
3981
16e850e1f4e5 renderPS.py: fix so it works again
robin
parents: 3976
diff changeset
     8
from reportlab.lib.utils import getBytesIO, getStringIO, asBytes, char2int, rawBytes, asNative, isUnicode
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3765
diff changeset
     9
from reportlab.lib.rl_accel import fp_str
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    10
from reportlab.lib.colors import black
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
    11
from reportlab.graphics.renderbase import Renderer, StateTracker, getStateDelta, renderScaledDrawing
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    12
from reportlab.graphics.shapes import STATE_DEFAULTS
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    13
import math
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    14
from operator import getitem
4367
9960d82643bf remove ascii, cmp & xrange builtins abuse; version-->3.4.15
robin <robin@reportlab.com>
parents: 4330
diff changeset
    15
from reportlab import rl_config, xrange, ascii
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
    16
from reportlab.pdfgen.canvas import FILL_EVEN_ODD, FILL_NON_ZERO
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    17
_ESCAPEDICT={}
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    18
for c in xrange(256):
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    19
    if c<32 or c>=127:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    20
        _ESCAPEDICT[c]= '\\%03o' % c
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    21
    elif c in (ord('\\'),ord('('),ord(')')):
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    22
        _ESCAPEDICT[c] = '\\'+chr(c)
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    23
    else:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    24
        _ESCAPEDICT[c] = chr(c)
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    25
del c
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    26
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    27
def _escape_and_limit(s):
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    28
    s = asBytes(s)
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    29
    R = []
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    30
    aR = R.append
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    31
    n = 0
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    32
    for c in s:
3981
16e850e1f4e5 renderPS.py: fix so it works again
robin
parents: 3976
diff changeset
    33
        c = _ESCAPEDICT[char2int(c)]
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    34
        aR(c)
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    35
        n += len(c)
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    36
        if n>=200:
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    37
            n = 0
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    38
            aR('\\\n')
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
    39
    return ''.join(R)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    40
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    41
# we need to create encoding vectors for each font we use, or they will
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    42
 # come out in Adobe's old StandardEncoding, which NOBODY uses.
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    43
PS_WinAnsiEncoding="""
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    44
/RE { %def
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    45
  findfont begin
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    46
  currentdict dup length dict begin
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    47
 { %forall
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    48
   1 index /FID ne { def } { pop pop } ifelse
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    49
 } forall
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    50
 /FontName exch def dup length 0 ne { %if
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    51
   /Encoding Encoding 256 array copy def
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    52
   0 exch { %forall
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    53
     dup type /nametype eq { %ifelse
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    54
       Encoding 2 index 2 index put
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    55
       pop 1 add
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    56
     }{ %else
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    57
       exch pop
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    58
     } ifelse
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    59
   } forall
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    60
 } if pop
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    61
  currentdict dup end end
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    62
  /FontName get exch definefont pop
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    63
} bind def
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    64
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    65
/WinAnsiEncoding [
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    66
  39/quotesingle 96/grave 128/euro 130/quotesinglbase/florin/quotedblbase
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    67
  /ellipsis/dagger/daggerdbl/circumflex/perthousand
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    68
  /Scaron/guilsinglleft/OE 145/quoteleft/quoteright
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    69
  /quotedblleft/quotedblright/bullet/endash/emdash
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    70
  /tilde/trademark/scaron/guilsinglright/oe/dotlessi
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    71
  159/Ydieresis 164/currency 166/brokenbar 168/dieresis/copyright
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    72
  /ordfeminine 172/logicalnot 174/registered/macron/ring
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    73
  177/plusminus/twosuperior/threesuperior/acute/mu
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    74
  183/periodcentered/cedilla/onesuperior/ordmasculine
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    75
  188/onequarter/onehalf/threequarters 192/Agrave/Aacute
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    76
  /Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    77
  /Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    78
  /Icircumflex/Idieresis/Eth/Ntilde/Ograve/Oacute
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    79
  /Ocircumflex/Otilde/Odieresis/multiply/Oslash
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    80
  /Ugrave/Uacute/Ucircumflex/Udieresis/Yacute/Thorn
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    81
  /germandbls/agrave/aacute/acircumflex/atilde/adieresis
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    82
  /aring/ae/ccedilla/egrave/eacute/ecircumflex
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    83
  /edieresis/igrave/iacute/icircumflex/idieresis
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    84
  /eth/ntilde/ograve/oacute/ocircumflex/otilde
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    85
  /odieresis/divide/oslash/ugrave/uacute/ucircumflex
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    86
  /udieresis/yacute/thorn/ydieresis
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
    87
] def
2796
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
    88
"""
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
    89
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
    90
class PSCanvas:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    91
    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
    92
        self.width, self.height = size
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    93
        xtraState = []
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    94
        self._xtraState_push = xtraState.append
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    95
        self._xtraState_pop = xtraState.pop
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
    96
        self.comments = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    97
        self.code = []
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
    98
        self.code_append = self.code.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
    99
        self._sep = '\n'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   100
        self._strokeColor = self._fillColor = self._lineWidth = \
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   101
            self._font = self._fontSize = self._lineCap = \
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   102
            self._lineJoin = self._color = None
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   103
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   104
        self._fontsUsed =   [] # track them as we go
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   105
        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
   106
        self.setStrokeColor(STATE_DEFAULTS['strokeColor'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   107
        self.setLineCap(2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   108
        self.setLineJoin(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   109
        self.setLineWidth(1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   110
        self.PostScriptLevel=PostScriptLevel
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   111
        self._fillMode = FILL_EVEN_ODD
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   112
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   113
    def comment(self,msg):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   114
        if self.comments: self.code_append('%'+msg)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   115
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   116
    def drawImage(self, image, x1,y1, width=None,height=None): # Postscript Level2 version
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   117
        # select between postscript level 1 or level 2
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   118
        if self.PostScriptLevel==1:
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   119
            self._drawImageLevel1(image, x1,y1, width, height)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   120
        elif self.PostScriptLevel==2:
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   121
            self._drawImageLevel2(image, x1, y1, width, height)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   122
        else :
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   123
            raise ValueError('Unsupported Postscript Level %s' % self.PostScriptLevel)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   124
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   125
    def clear(self):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   126
        self.code_append('showpage') # ugh, this makes no sense oh well.
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   127
2796
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   128
    def _t1_re_encode(self):
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   129
        if not self._fontsUsed: return
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   130
        # for each font used, reencode the vectors
2797
c04258bb5882 renderPS.py: attempt to fix the winansiencoding stuff
rgbecker
parents: 2796
diff changeset
   131
        C = []
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   132
        for fontName in self._fontsUsed:
2797
c04258bb5882 renderPS.py: attempt to fix the winansiencoding stuff
rgbecker
parents: 2796
diff changeset
   133
            fontObj = getFont(fontName)
c04258bb5882 renderPS.py: attempt to fix the winansiencoding stuff
rgbecker
parents: 2796
diff changeset
   134
            if not fontObj._dynamicFont and fontObj.encName=='WinAnsiEncoding':
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   135
                C.append('WinAnsiEncoding /%s /%s RE' % (fontName, fontName))
2797
c04258bb5882 renderPS.py: attempt to fix the winansiencoding stuff
rgbecker
parents: 2796
diff changeset
   136
        if C:
c04258bb5882 renderPS.py: attempt to fix the winansiencoding stuff
rgbecker
parents: 2796
diff changeset
   137
            C.insert(0,PS_WinAnsiEncoding)
3765
a7e7cd6e0003 renderPM.py now works to produce a pmout at least
robin
parents: 3723
diff changeset
   138
            self.code.insert(1, self._sep.join(C))
2796
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   139
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   140
    def save(self,f=None):
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   141
        if not hasattr(f,'write'):
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   142
            _f = open(f,'wb')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   143
        else:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   144
            _f = f
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   145
        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
   146
        self.code.insert(0,'''\
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   147
%%!PS-Adobe-3.0 EPSF-3.0
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   148
%%%%BoundingBox: 0 0 %d %d
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   149
%%%% Initialization:
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   150
/m {moveto} bind def
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   151
/l {lineto} bind def
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   152
/c {curveto} bind def
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   153
''' % (self.width,self.height))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   154
2796
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   155
        self._t1_re_encode()
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   156
        _f.write(rawBytes(self._sep.join(self.code)))
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   157
        if _f is not f:
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   158
            _f.close()
1904
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   159
            from reportlab.lib.utils import markfilename
142332c87b5c Added markfilename call
rgbecker
parents: 1810
diff changeset
   160
            markfilename(f,creatorcode='XPR3',filetype='EPSF')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   161
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   162
    def saveState(self):
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
   163
        self._xtraState_push((self._fontCodeLoc,))
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   164
        self.code_append('gsave')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   165
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   166
    def restoreState(self):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   167
        self.code_append('grestore')
2493
9ff225d52629 renderPS.py: attempt to fix setFont error
rgbecker
parents: 2440
diff changeset
   168
        self._fontCodeLoc, = self._xtraState_pop()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   169
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   170
    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
   171
        """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
   172
        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
   173
        font = font or self._font
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   174
        fontSize = fontSize or self._fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   175
        return stringWidth(s, font, fontSize)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   176
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   177
    def setLineCap(self,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   178
        if self._lineCap!=v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   179
            self._lineCap = v
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   180
            self.code_append('%d setlinecap'%v)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   181
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   182
    def setLineJoin(self,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   183
        if self._lineJoin!=v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   184
            self._lineJoin = v
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   185
            self.code_append('%d setlinejoin'%v)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   186
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   187
    def setDash(self, array=[], phase=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   188
        """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
   189
        # copied and modified from reportlab.canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   190
        psoperation = "setdash"
3502
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   191
        if isinstance(array,(float,int)):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   192
            self.code_append('[%s %s] 0 %s' % (array, phase, psoperation))
3502
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   193
        elif isinstance(array,(tuple,list)):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   194
            assert phase >= 0, "phase is a length in user space"
3765
a7e7cd6e0003 renderPM.py now works to produce a pmout at least
robin
parents: 3723
diff changeset
   195
            textarray = ' '.join(map(str, array))
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   196
            self.code_append('[%s] %s %s' % (textarray, phase, psoperation))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   197
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   198
    def setStrokeColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   199
        self._strokeColor = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   200
        self.setColor(color)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   201
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   202
    def setColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   203
        if self._color!=color:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   204
            self._color = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   205
            if color:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   206
                if hasattr(color, "cyan"):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   207
                    self.code_append('%s setcmykcolor' % fp_str(color.cyan, color.magenta, color.yellow, color.black))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   208
                else:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   209
                    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
   210
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   211
    def setFillColor(self, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   212
        self._fillColor = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   213
        self.setColor(color)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   214
4325
fc03e6396da5 add support for fillMode in drawing stack; version-->3.3.34
robin <robin@reportlab.com>
parents: 4306
diff changeset
   215
    def setFillMode(self, v):
fc03e6396da5 add support for fillMode in drawing stack; version-->3.3.34
robin <robin@reportlab.com>
parents: 4306
diff changeset
   216
        self._fillMode = v
fc03e6396da5 add support for fillMode in drawing stack; version-->3.3.34
robin <robin@reportlab.com>
parents: 4306
diff changeset
   217
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   218
    def setLineWidth(self, width):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   219
        if width != self._lineWidth:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   220
            self._lineWidth = width
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   221
            self.code_append('%s setlinewidth' % width)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   222
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   223
    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
   224
        if self._font!=font or self._fontSize!=fontSize:
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   225
            self._fontCodeLoc = len(self.code)
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   226
            self._font = font
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   227
            self._fontSize = fontSize
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   228
            self.code_append('')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   229
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   230
    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
   231
        if self._strokeColor != None:
2440
64847841717e renderPS.py: fix buglet in color setting
rgbecker
parents: 2405
diff changeset
   232
            self.setColor(self._strokeColor)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   233
            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
   234
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   235
    def _escape(self, s):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   236
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   237
        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
   238
        escaped with backslashes.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   239
        '''
2783
1d51b498a0eb graphics\renderPS.py: improve escape method
rgbecker
parents: 2722
diff changeset
   240
        try:
2786
72c772f507a9 renderPS.py: fix length problems in escaped strings
rgbecker
parents: 2783
diff changeset
   241
            return _escape_and_limit(s)
2783
1d51b498a0eb graphics\renderPS.py: improve escape method
rgbecker
parents: 2722
diff changeset
   242
        except:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   243
            raise ValueError("cannot escape %s" % ascii(s))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   244
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   245
    def _issueT1String(self,fontObj,x,y,s):
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   246
        fc = fontObj
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   247
        code_append = self.code_append
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   248
        fontSize = self._fontSize
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   249
        fontsUsed = self._fontsUsed
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   250
        escape = self._escape
3981
16e850e1f4e5 renderPS.py: fix so it works again
robin
parents: 3976
diff changeset
   251
        if not isUnicode(s):
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   252
            try:
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   253
                s = s.decode('utf8')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   254
            except UnicodeDecodeError as e:
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   255
                i,j = e.args[2:4]
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   256
                raise UnicodeDecodeError(*(e.args[:4]+('%s\n%s-->%s<--%s' % (e.args[4],s[i-10:i],s[i:j],s[j:j+10]),)))
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   257
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   258
        for f, t in unicode2T1(s,[fontObj]+fontObj.substitutionFonts):
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   259
            if f!=fc:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   260
                psName = asNative(f.face.name)
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   261
                code_append('(%s) findfont %s scalefont setfont' % (psName,fp_str(fontSize)))
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   262
                if psName not in fontsUsed:
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   263
                    fontsUsed.append(psName)
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   264
                fc = f
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   265
            code_append('%s m (%s) show ' % (fp_str(x,y),escape(t)))
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   266
            x += f.stringWidth(t.decode(f.encName),fontSize)
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   267
        if fontObj!=fc:
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   268
            self._font = None
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   269
            self.setFont(fontObj.face.name,fontSize)
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   270
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   271
    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
   272
        if self._fillColor != None:
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   273
            fontObj = getFont(self._font)
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   274
            if not self.code[self._fontCodeLoc]:
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   275
                psName = asNative(fontObj.face.name)
2208
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   276
                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
   277
                if psName not in self._fontsUsed:
c068b78ea4ba Attempt to get delayed font setting and proper psNames
rgbecker
parents: 1952
diff changeset
   278
                    self._fontsUsed.append(psName)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   279
            self.setColor(self._fillColor)
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   280
            if angle!=0:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   281
                self.code_append('gsave %s translate %s rotate' % (fp_str(x,y),fp_str(angle)))
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   282
                x = y = 0
2796
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   283
            if fontObj._dynamicFont:
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   284
                s = self._escape(s)
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   285
                self.code_append('%s m (%s) show ' % (fp_str(x,y),s))
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   286
            else:
8982611d412c renderPS.py:re-add the WinAnsiEncoding stuff(my brain hurts)
rgbecker
parents: 2795
diff changeset
   287
                self._issueT1String(fontObj,x,y,s)
2795
58530293e457 renderPS.py: remove winansi encoding stuff (we use standard now for T1)
rgbecker
parents: 2786
diff changeset
   288
            if angle!=0:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   289
                self.code_append('grestore')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   290
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   291
    def drawCentredString(self, x, y, text, text_anchor='middle'):
2440
64847841717e renderPS.py: fix buglet in color setting
rgbecker
parents: 2405
diff changeset
   292
        if self._fillColor is not None:
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   293
            textLen = stringWidth(text, self._font,self._fontSize)
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   294
            if text_anchor=='end':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2570
diff changeset
   295
                x -= textLen
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   296
            elif text_anchor=='middle':
2574
d81949596070 reportlab: changes to accomodate barcodes
rgbecker
parents: 2570
diff changeset
   297
                x -= textLen/2.
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3032
diff changeset
   298
            elif text_anchor=='numeric':
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3032
diff changeset
   299
                x -= numericXShift(text_anchor,text,textLen,self._font,self._fontSize)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   300
            self.drawString(x,y,text)
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   301
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   302
    def drawRightString(self, text, x, y):
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   303
        self.drawCentredString(text,x,y,text_anchor='end')
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   304
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   305
    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
   306
        codeline = '%s m %s curveto'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   307
        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
   308
        if self._fillColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   309
            self.setColor(self._fillColor)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   310
            self.code_append((codeline % data) + ' eofill')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   311
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   312
            self.setColor(self._strokeColor)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   313
            self.code_append((codeline % data)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   314
                            + ((closed and ' closepath') or '')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   315
                            + ' stroke')
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   316
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   317
    ########################################################################################
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   318
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   319
    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
   320
        "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
   321
        # Path is drawn in counter-clockwise direction"
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   322
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   323
        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
   324
        y1, y2 = min(y1,y2), max(y1, y2)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   325
        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
   326
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   327
    def roundRect(self, x1,y1, x2,y2, rx=8, ry=8):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   328
        """Draw a rounded rectangle between x1,y1, and x2,y2,
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   329
        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
   330
        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
   331
        # Path is drawn in counter-clockwise direction
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   332
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   333
        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
   334
        y1, y2 = min(y1,y2), max(y1, y2)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   335
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   336
        # 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
   337
        # 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
   338
        # 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
   339
        # arguments are (cx, cy, rx, ry, startAngle, endAngle)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   340
        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
   341
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   342
        # 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
   343
        # go with newpath for precision, does this violate any assumptions in code???
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   344
        rr = ['newpath'] # Round Rect code path
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   345
        a = rr.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   346
        # upper left corner ellipse is first
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   347
        a(ellipsePath % (x1+rx, y1+ry, rx, -ry, 90, 180))
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   348
        a(ellipsePath % (x1+rx, y2-ry, rx, -ry, 180, 270))
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   349
        a(ellipsePath % (x2-rx, y2-ry, rx, -ry, 270, 360))
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   350
        a(ellipsePath % (x2-rx, y1+ry, rx, -ry, 0,  90) )
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   351
        a('closepath')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   352
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   353
        self._fillAndStroke(rr)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   354
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   355
    def ellipse(self, x1,y1, x2,y2):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   356
        """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
   357
        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
   358
        #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
   359
        self.drawArc(x1,y1, x2,y2)
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   360
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   361
    def circle(self, xc, yc, r):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   362
        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
   363
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   364
    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
   365
        """Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   366
        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
   367
        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
   368
        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
   369
        #calculate centre of ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   370
        #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
   371
        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
   372
        rx, ry = (x2-x1)/2.0, (y2-y1)/2.0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   373
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   374
        codeline = self._genArcCode(x1, y1, x2, y2, startAng, extent)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   375
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   376
        startAngleRadians = math.pi*startAng/180.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   377
        extentRadians = math.pi*extent/180.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   378
        endAngleRadians = startAngleRadians + extentRadians
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   379
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   380
        codelineAppended = 0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   381
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   382
        # fill portion
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   383
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   384
        if self._fillColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   385
            self.setColor(self._fillColor)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   386
            self.code_append(codeline)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   387
            codelineAppended = 1
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   388
            if self._strokeColor!=None: self.code_append('gsave')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   389
            self.lineTo(cx,cy)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   390
            self.code_append('eofill')
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   391
            if self._strokeColor!=None: self.code_append('grestore')
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
        # stroke portion
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   394
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   395
            # 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
   396
            self.setColor(self._strokeColor)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   397
            (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
   398
            if not codelineAppended:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   399
                self.code_append(codeline)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   400
            if fromcenter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   401
                # move to center
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   402
                self.lineTo(cx,cy)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   403
                self.lineTo(startx, starty)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   404
                self.code_append('closepath')
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   405
            self.code_append('stroke')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   406
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   407
    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
   408
        "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
   409
        #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
   410
        xScale = abs((x2-x1)/2.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   411
        yScale = abs((y2-y1)/2.0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   412
        #calculate centre of ellipse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   413
        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
   414
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   415
        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
   416
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   417
        if extent >= 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   418
            arc='arc'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   419
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   420
            arc='arcn'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   421
        data = (x,y, xScale, yScale, startAng, startAng+extent, arc)
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
        return codeline % data
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   424
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   425
    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
   426
        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
   427
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   428
        start = p[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   429
        p = p[1:]
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   430
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   431
        poly = []
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   432
        a = poly.append
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   433
        a("%s m" % fp_str(start))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   434
        for point in p:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   435
            a("%s l" % fp_str(point))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   436
        if closed:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   437
            a("closepath")
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   438
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   439
        self._fillAndStroke(poly,stroke=stroke,fill=fill)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   440
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   441
    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
   442
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   443
            self._setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   444
            codeline = '%s m %s l stroke'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   445
            for line in lineList:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   446
                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
   447
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   448
    def moveTo(self,x,y):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   449
        self.code_append('%s m' % fp_str(x, y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   450
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   451
    def lineTo(self,x,y):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   452
        self.code_append('%s l' % fp_str(x, y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   453
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   454
    def curveTo(self,x1,y1,x2,y2,x3,y3):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   455
        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
   456
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   457
    def closePath(self):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   458
        self.code_append('closepath')
1071
eb266be600ed Just van Rossum's path patch
rgbecker
parents: 977
diff changeset
   459
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   460
    def polyLine(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   461
        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
   462
        if self._strokeColor != None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   463
            self.setColor(self._strokeColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   464
            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
   465
            for t in p[1:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   466
                self.lineTo(t[0], t[1])
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   467
            self.code_append('stroke')
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   468
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   469
    def drawFigure(self, partList, closed=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   470
        figureCode = []
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   471
        a = figureCode.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   472
        first = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   473
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   474
        for part in partList:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   475
            op = part[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   476
            args = list(part[1:])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   477
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   478
            if op == figureLine:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   479
                if first:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   480
                    first = 0
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   481
                    a("%s m" % fp_str(args[:2]))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   482
                else:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   483
                    a("%s l" % fp_str(args[:2]))
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   484
                a("%s l" % fp_str(args[2:]))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   485
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   486
            elif op == figureArc:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   487
                first = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   488
                x1,y1,x2,y2,startAngle,extent = args[:6]
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   489
                a(self._genArcCode(x1,y1,x2,y2,startAngle,extent))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   490
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   491
            elif op == figureCurve:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   492
                if first:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   493
                    first = 0
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   494
                    a("%s m" % fp_str(args[:2]))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   495
                else:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   496
                    a("%s l" % fp_str(args[:2]))
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   497
                a("%s curveto" % fp_str(args[2:]))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   498
            else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   499
                raise TypeError("unknown figure operator: "+op)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   500
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   501
        if closed:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   502
            a("closepath")
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   503
        self._fillAndStroke(figureCode)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   504
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   505
    def _fillAndStroke(self,code,clip=0,fill=1,stroke=1,fillMode=None):
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   506
        fill = self._fillColor and fill
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   507
        stroke = self._strokeColor and stroke
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   508
        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
   509
            self.code.extend(code)
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   510
            if fill:
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   511
                if fillMode is None:
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   512
                    fillMode = self._fillMode
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   513
                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
   514
                self.setColor(self._fillColor)
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   515
                self.code_append("eofill" if fillMode==FILL_EVEN_ODD else "fill")
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   516
                if stroke or clip: self.code_append("grestore")
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   517
            if stroke:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   518
                if clip: self.code_append("gsave")
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   519
                self.setColor(self._strokeColor)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   520
                self.code_append("stroke")
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   521
                if clip: self.code_append("grestore")
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   522
            if clip:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   523
                self.code_append("clip")
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   524
                self.code_append("newpath")
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   525
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   526
    def translate(self,x,y):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   527
        self.code_append('%s translate' % fp_str(x,y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   528
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   529
    def scale(self,x,y):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   530
        self.code_append('%s scale' % fp_str(x,y))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   531
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   532
    def transform(self,a,b,c,d,e,f):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   533
        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
   534
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   535
    def _drawTimeResize(self,w,h):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   536
        '''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
   537
        self.width, self.height = w, h
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   538
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   539
    def _drawImageLevel1(self, image, x1, y1, width=None, height=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   540
        # 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
   541
        # 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
   542
        component_depth = 8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   543
        myimage = image.convert('RGB')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   544
        imgwidth, imgheight = myimage.size
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   545
        if not width:
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   546
            width = imgwidth
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   547
        if not height:
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   548
            height = imgheight
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   549
        #print 'Image size (%d, %d); Draw size (%d, %d)' % (imgwidth, imgheight, width, height)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   550
        # 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
   551
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   552
        # "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
   553
        # 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
   554
        # 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
   555
        # 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
   556
        #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   557
        # 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
   558
        #
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   559
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   560
        # 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
   561
        # 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
   562
        # 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
   563
        # (for a left to right, top to bottom image )
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   564
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   565
        # 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
   566
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   567
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   568
            'gsave',
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   569
            '%s %s translate' % (x1,y1), # need to start are lower left of image
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   570
            '%s %s scale' % (width,height),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   571
            '/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
   572
            ])
1665
c2ce87221cf7 Changes to allow flaky _drawTimeResize
rgbecker
parents: 1636
diff changeset
   573
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   574
        # 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
   575
        # 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
   576
        # 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
   577
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   578
            '%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
   579
            '[%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
   580
            '{ currentfile scanline readhexstring pop } false 3',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   581
            'colorimage '
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   582
            ])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   583
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   584
        # 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
   585
        # 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
   586
        # piddlePDF again
4103
c5c1e06aa8fc attempt to pacify pillow tostring warnings reported by Kale Franz @ bitbucket.org
robin
parents: 3981
diff changeset
   587
        rawimage = (myimage.tobytes if hasattr(myimage,'tobytes') else myimage.tostring)()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   588
        hex_encoded = self._AsciiHexEncode(rawimage)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   589
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   590
        # write in blocks of 78 chars per line
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   591
        outstream = getStringIO(hex_encoded)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   592
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   593
        dataline = outstream.read(78)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   594
        while dataline != "":
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   595
            self.code_append(dataline)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   596
            dataline= outstream.read(78)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   597
        self.code_append('% end of image data') # for clarity
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   598
        self.code_append('grestore') # return coordinates to normal
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   599
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   600
    # end of drawImage
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   601
    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
   602
        "Helper function used by images"
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   603
        output = getStringIO()
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   604
        for char in asBytes(input):
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   605
            output.write('%02x' % char2int(char))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   606
        return output.getvalue()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   607
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   608
    def _drawImageLevel2(self, image, x1,y1, width=None,height=None): # Postscript Level2 version
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   609
        '''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
   610
        ### 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
   611
        if image.mode=='L' :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   612
            imBitsPerComponent = 8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   613
            imNumComponents = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   614
            myimage = image
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   615
        elif image.mode == '1':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   616
            myimage = image.convert('L')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   617
            imNumComponents = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   618
            myimage = image
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   619
        else :
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   620
            myimage = image.convert('RGB')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   621
            imNumComponents = 3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   622
            imBitsPerComponent = 8
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   623
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   624
        imwidth, imheight = myimage.size
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   625
        if not width:
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   626
            width = imwidth
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   627
        if not height:
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   628
            height = imheight
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   629
        self.code.extend([
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   630
            'gsave',
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   631
            '%s %s translate' % (x1,y1), # need to start are lower left of image
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   632
            '%s %s scale' % (width,height)])
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   633
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   634
        if imNumComponents == 3 :
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   635
            self.code_append('/DeviceRGB setcolorspace')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   636
        elif imNumComponents == 1 :
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   637
            self.code_append('/DeviceGray setcolorspace')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   638
        # create the image dictionary
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   639
        self.code_append("""
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   640
<<
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   641
/ImageType 1
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   642
/Width %d /Height %d  %% dimensions of source image
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   643
/BitsPerComponent %d""" % (imwidth, imheight, imBitsPerComponent) )
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   644
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   645
        if imNumComponents == 1:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   646
            self.code_append('/Decode [0 1]')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   647
        if imNumComponents == 3:
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   648
            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
   649
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   650
        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
   651
                            '/DataSource currentfile /ASCIIHexDecode filter',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   652
                            '>> % End image dictionary',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   653
                            'image'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   654
        # after image operator just need to dump image dat to file as hexstring
4103
c5c1e06aa8fc attempt to pacify pillow tostring warnings reported by Kale Franz @ bitbucket.org
robin
parents: 3981
diff changeset
   655
        rawimage = (myimage.tobytes if hasattr(myimage,'tobytes') else myimage.tostring)()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   656
        hex_encoded = self._AsciiHexEncode(rawimage)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   657
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   658
        # write in blocks of 78 chars per line
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   659
        outstream = getStringIO(hex_encoded)
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
        dataline = outstream.read(78)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   662
        while dataline != "":
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   663
            self.code_append(dataline)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   664
            dataline= outstream.read(78)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   665
        self.code_append('> % end of image data') # > is EOD for hex encoded filterfor clarity
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   666
        self.code_append('grestore') # return coordinates to normal
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   667
604
c61e69d2721e Fixes for QIR barchart presentation
andy_robinson
parents: 587
diff changeset
   668
# renderpdf - draws them onto a canvas
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   669
"""Usage:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   670
    from reportlab.graphics import renderPS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   671
    renderPS.draw(drawing, canvas, x, y)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   672
Execute the script to see some test drawings."""
3975
4a3599863c11 eliminate from . imports in favour of absolutes to allow running modules
robin
parents: 3781
diff changeset
   673
from reportlab.graphics.shapes import *
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   674
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   675
# hack so we only get warnings once each
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   676
#warnOnce = WarnOnce()
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   677
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   678
# the main entry point for users...
1259
49bfe92e612e Used showBoundary every wherei, moved to Drawings as Groups
rgbecker
parents: 1230
diff changeset
   679
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
   680
    """As it says"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   681
    R = _PSRenderer()
2553
a880f43d10bd reprotlab/graphics: fix so renderScale is used properly
rgbecker
parents: 2544
diff changeset
   682
    R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   683
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   684
def _pointsFromList(L):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   685
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   686
    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
   687
    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
   688
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   689
    P=[]
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   690
    a = P.append
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   691
    for i in range(0,len(L),2):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2574
diff changeset
   692
        a((L[i],L[i+1]))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   693
    return P
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   694
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   695
class _PSRenderer(Renderer):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   696
    """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
   697
    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
   698
    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
   699
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   700
    def drawNode(self, node):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   701
        """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
   702
        in the tree"""
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   703
        self._canvas.comment('begin node %r'%node)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   704
        color = self._canvas._color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   705
        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
   706
            self._canvas.saveState()
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   707
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   708
        #apply state changes
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   709
        deltas = getStateDelta(node)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   710
        self._tracker.push(deltas)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   711
        self.applyStateChanges(deltas, {})
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
        #draw the object, or recurse
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   714
        self.drawNodeDispatcher(node)
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
        rDeltas = self._tracker.pop()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   717
        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
   718
            self._canvas.restoreState()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   719
        self._canvas.comment('end node %r'%node)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   720
        self._canvas._color = color
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   721
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   722
        #restore things we might have lost (without actually doing anything).
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   723
        for k, v in rDeltas.items():
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3219
diff changeset
   724
            if k in self._restores:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   725
                setattr(self._canvas,self._restores[k],v)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   726
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   727
##  _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
   728
##              '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
   729
##              'font_size':'_fontSize'}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   730
    _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
   731
                'strokeLineJoin':'_lineJoin','fillColor':'_fillColor','fontName':'_font',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   732
                'fontSize':'_fontSize'}
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 drawRect(self, rect):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   735
        if rect.rx == rect.ry == 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   736
            #plain old rectangle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   737
            self._canvas.rect(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   738
                    rect.x, rect.y,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   739
                    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
   740
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   741
            #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
   742
            #pdfgen roundRect function.  TODO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   743
            self._canvas.roundRect(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   744
                    rect.x, rect.y,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   745
                    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
   746
                    )
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   747
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   748
    def drawLine(self, line):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   749
        if self._canvas._strokeColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   750
            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
   751
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   752
    def drawCircle(self, circle):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   753
        self._canvas.circle( circle.cx, circle.cy, circle.r)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   754
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   755
    def drawWedge(self, wedge):
1952
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   756
        yradius, radius1, yradius1 = wedge._xtraRadii()
4132
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   757
        if (radius1==0 or radius1 is None) and (yradius1==0 or yradius1 is None) and not wedge.annular:
1952
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   758
            startangledegrees = wedge.startangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   759
            endangledegrees = wedge.endangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   760
            centerx= wedge.centerx
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   761
            centery = wedge.centery
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   762
            radius = wedge.radius
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   763
            extent = endangledegrees - startangledegrees
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   764
            self._canvas.drawArc(centerx-radius, centery-yradius, centerx+radius, centery+yradius,
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   765
                startangledegrees, extent, fromcenter=1)
f32a8bf74bb0 Attempt at adding in a generalised wedge
rgbecker
parents: 1904
diff changeset
   766
        else:
4132
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   767
            P = wedge.asPolygon()
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   768
            if isinstance(P,Path):
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   769
                self.drawPath(P)
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   770
            else:
28379377462b fix doughnut chart special case and how it is drawn; bump to 3.1.31
robin
parents: 4103
diff changeset
   771
                self.drawPolygon(P)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   772
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   773
    def drawPolyLine(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   774
        if self._canvas._strokeColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   775
            self._canvas.polyLine(_pointsFromList(p.points))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   776
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   777
    def drawEllipse(self, ellipse):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   778
        #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
   779
        x1 = ellipse.cx - ellipse.rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   780
        x2 = ellipse.cx + ellipse.rx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   781
        y1 = ellipse.cy - ellipse.ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   782
        y2 = ellipse.cy + ellipse.ry
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   783
        self._canvas.ellipse(x1,y1,x2,y2)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   784
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   785
    def drawPolygon(self, p):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   786
        self._canvas.polygon(_pointsFromList(p.points), closed=1)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   787
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   788
    def drawString(self, stringObj):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   789
        if self._canvas._fillColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   790
            S = self._tracker.getState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   791
            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
   792
            if not text_anchor in ['start','inherited']:
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1679
diff changeset
   793
                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
   794
                textLen = stringWidth(text, font,fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   795
                if text_anchor=='end':
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3032
diff changeset
   796
                    x -= textLen
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   797
                elif text_anchor=='middle':
3219
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3032
diff changeset
   798
                    x -= textLen/2
b28d6eef8227 reportlab: add support for String anchor 'numeric'
rgbecker
parents: 3032
diff changeset
   799
                elif text_anchor=='numeric':
3361
b7da4a9298d4 renderPS.py: fix encoding bug
rgbecker
parents: 3326
diff changeset
   800
                    x -= numericXShift(text_anchor,text,textLen,font,fontSize,encoding='winansi')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   801
                else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   802
                    raise ValueError('bad value for text_anchor '+str(text_anchor))
2405
26e2154ef4ba reportlab graphics: changes for canvasadapter
rgbecker
parents: 2360
diff changeset
   803
            self._canvas.drawString(x,y,text)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   804
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   805
    def drawPath(self, path, fillMode=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   806
        from reportlab.graphics.shapes import _renderPath
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   807
        c = self._canvas
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   808
        drawFuncs = (c.moveTo, c.lineTo, c.curveTo, c.closePath)
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   809
        autoclose = getattr(path,'autoclose','')
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   810
        def rP(**kwds):
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   811
            return _renderPath(path, drawFuncs, **kwds)
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   812
        if fillMode is None:
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   813
            fillMode = getattr(path,'fillMode',c._fillMode)
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   814
        fill = c._fillColor is not None
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   815
        stroke = c._strokeColor is not None
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   816
        clip = path.isClipPath
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   817
        fas = lambda **kwds: c._fillAndStroke([], fillMode=fillMode, **kwds)
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   818
        pathFill = lambda : c._fillAndStroke([], stroke=0, fillMode=fillMode)
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   819
        pathStroke = lambda : c._fillAndStroke([], fill=0)
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   820
        if autoclose=='svg':
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   821
            rP()
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   822
            fas(stroke=stroke,fill=fill,clip=clip)
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   823
        elif autoclose=='pdf':
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   824
            if fill:
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   825
                rP(forceClose=True)
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   826
                fas(stroke=stroke,fill=fill,clip=clip)
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   827
            elif stroke or clip:
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   828
                rP()
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   829
                fas(stroke=stroke,fill=0,clip=clip)
4305
41faca6e9c0b add support for different fill policies in renderXX drawPath; version-->3.3.28
robin
parents: 4283
diff changeset
   830
        else:
4306
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   831
            if fill and rP(countOnly=True):
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   832
                rP()
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   833
            elif stroke or clip:
8ffb2ffc283b support for Path autoclose & fillMode; version --> 3.3.29
robin
parents: 4305
diff changeset
   834
                fas(stroke=stroke,fill=0,clip=clip)
580
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
    def applyStateChanges(self, delta, newState):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   837
        """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
   838
        needed to set those properties"""
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   839
        for key, value in delta.items():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   840
            if key == 'transform':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   841
                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
   842
                                 value[3], value[4], value[5])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   843
            elif key == 'strokeColor':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   844
                #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
   845
                #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
   846
                #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
   847
                self._canvas.setStrokeColor(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   848
            elif key == 'strokeWidth':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   849
                self._canvas.setLineWidth(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   850
            elif key == 'strokeLineCap':  #0,1,2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   851
                self._canvas.setLineCap(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   852
            elif key == 'strokeLineJoin':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   853
                self._canvas.setLineJoin(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   854
            elif key == 'strokeDashArray':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   855
                if value:
3502
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   856
                    if isinstance(value,(list,tuple)) and len(value)==2 and isinstance(value[1],(tuple,list)):
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   857
                        phase = value[0]
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   858
                        value = value[1]
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   859
                    else:
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   860
                        phase = 0
b2c9927314fc color fixes
rgbecker
parents: 3361
diff changeset
   861
                    self._canvas.setDash(value,phase)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   862
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   863
                    self._canvas.setDash()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   864
##          elif key == 'stroke_opacity':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   865
##              warnOnce('Stroke Opacity not supported yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   866
            elif key == 'fillColor':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   867
                #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
   868
                #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
   869
                #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
   870
                self._canvas.setFillColor(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   871
##          elif key == 'fill_rule':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   872
##              warnOnce('Fill rules not done yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   873
##          elif key == 'fill_opacity':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   874
##              warnOnce('Fill opacity not done yet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   875
            elif key in ['fontSize', 'fontName']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   876
                # both need setting together in PDF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   877
                # 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
   878
                # 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
   879
                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
   880
                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
   881
                self._canvas.setFont(fontname, fontsize)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   882
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   883
    def drawImage(self, image):
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   884
        from reportlab.lib.utils import ImageReader
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   885
        im = ImageReader(image.path)
4447
636c17021410 fix renderPS drawImage chain; version --> 3.5.3
robin <robin@reportlab.com>
parents: 4370
diff changeset
   886
        self._canvas.drawImage(im._image,image.x,image.y,image.width,image.height)
2570
7b93b3b42e51 reportlab/graphics cosmetics and adding drawImage to renderPS
rgbecker
parents: 2553
diff changeset
   887
2722
f2e2b2901e8c reportlab/graphics: fix rlextra dependency
rgbecker
parents: 2678
diff changeset
   888
def drawToFile(d,fn, showBoundary=rl_config.showBoundary,**kwd):
2544
a6b9aa99b3c3 graphics: added Drawing.renderScale hack for renderer terminal drawing scales
rgbecker
parents: 2493
diff changeset
   889
    d = renderScaledDrawing(d)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   890
    c = PSCanvas((d.width,d.height))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   891
    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
   892
    c.save(fn)
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   893
1810
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   894
def drawToString(d, showBoundary=rl_config.showBoundary):
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   895
    "Returns a PS as a string in memory, without touching the disk"
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   896
    s = getBytesIO()
1810
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   897
    drawToFile(d, s, showBoundary=showBoundary)
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   898
    return s.getvalue()
c854388d98fe Added Dirk Datzert's drawToString func
rgbecker
parents: 1683
diff changeset
   899
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   900
#########################################################
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   901
#
4283
50081cf927c3 fix problems with svg drawToString contrib by Eric Gillet & Johann Du Toit
robin
parents: 4274
diff changeset
   902
#   test code.  First, define a bunch of drawings.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   903
#   Routine to draw them comes at the end.
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   904
#
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   905
#########################################################
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   906
def test(outDir='epsout',shout=False):
3975
4a3599863c11 eliminate from . imports in favour of absolutes to allow running modules
robin
parents: 3781
diff changeset
   907
    from reportlab.graphics import testshapes
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   908
    from reportlab.rl_config import verbose
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   909
    OLDFONTS = testshapes._FONTS[:]
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   910
    testshapes._FONTS[:] = ['Times-Roman','Times-Bold','Times-Italic', 'Times-BoldItalic','Courier']
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   911
    try:
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   912
        import os
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   913
        # save all drawings and their doc strings from the test file
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   914
        if not os.path.isdir(outDir):
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   915
            os.mkdir(outDir)
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   916
        #grab all drawings from the test module
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   917
        drawings = []
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   918
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   919
        for funcname in dir(testshapes):
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   920
            if funcname[0:10] == 'getDrawing':
4551
d357e2acc856 improve usage of eval/exec; version --> 3.5.32
robin
parents: 4528
diff changeset
   921
                func = getattr(testshapes,funcname)
d357e2acc856 improve usage of eval/exec; version --> 3.5.32
robin
parents: 4528
diff changeset
   922
                drawing = func()
d357e2acc856 improve usage of eval/exec; version --> 3.5.32
robin
parents: 4528
diff changeset
   923
                docstring = getattr(func,'__doc__','')
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   924
                drawings.append((drawing, docstring))
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   925
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   926
        i = 0
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   927
        for (d, docstring) in drawings:
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   928
            filename = outDir + os.sep + 'renderPS_%d.eps'%i
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   929
            drawToFile(d,filename)
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   930
            if shout or verbose>2: print('renderPS test saved %s' % ascii(filename))
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   931
            i += 1
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   932
    finally:
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   933
        testshapes._FONTS[:] = OLDFONTS
580
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   934
62e61180ae41 Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
diff changeset
   935
if __name__=='__main__':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   936
    import sys
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   937
    if len(sys.argv)>1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   938
        outdir = sys.argv[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   939
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1665
diff changeset
   940
        outdir = 'epsout'
3976
97f8403c5e12 renderPS.py: fixes to get a working state and make tests happier
robin
parents: 3975
diff changeset
   941
    test(outdir,shout=True)