reportlab/pdfgen/canvas.py
author rgbecker
Thu, 02 Mar 2000 10:28:54 +0000
changeset 28 b62ec46b2759
parent 27 684657a34422
child 33 bef84d99fbd8
permissions -rwxr-xr-x
[].extend illegal in 1.5.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     1
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     2
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     3
#	ReportLab Public License Version 1.0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     4
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     5
#   Except for the change of names the spirit and intention of this
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     6
#   license is the same as that of Python
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     9
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    10
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
# All Rights Reserved
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    12
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    16
# copyright notice and this permission notice appear in supporting
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    17
# documentation, and that the name of ReportLab not be used
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    19
# without specific, written prior permission. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    20
# 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    21
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    22
# Disclaimer
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    23
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    31
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    32
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    33
#	$Log: canvas.py,v $
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
    34
#	Revision 1.11  2000/03/02 10:28:54  rgbecker
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
    35
#	[].extend illegal in 1.5.1
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
    36
#
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
    37
#	Revision 1.10  2000/02/24 17:28:13  andy_robinson
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
    38
#	Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
    39
#	
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
    40
#	Revision 1.9  2000/02/24 09:12:55  andy_robinson
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
    41
#	
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
    42
#	Removed some constants which are no longer used.
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
    43
#	
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
    44
#	Revision 1.8  2000/02/20 14:43:27  rgbecker
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
    45
#	_currentPageHasImages = 0 in init
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
    46
#	
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
    47
#	Revision 1.7  2000/02/20 11:08:56  rgbecker
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
    48
#	Canvas.setPageSize fix
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
    49
#	
18
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
    50
#	Revision 1.6  2000/02/17 15:26:28  rgbecker
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
    51
#	Change page compression default
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
    52
#	
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    53
#	Revision 1.5  2000/02/17 02:08:04  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    54
#	Docstring & other fixes
18
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
    55
#	
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    56
#	Revision 1.4  2000/02/16 09:42:50  rgbecker
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    57
#	Conversion to reportlab package
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    58
#	
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    59
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    60
#	License text fixes
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    61
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    62
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    63
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    64
#	
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
    65
__version__=''' $Id: canvas.py,v 1.11 2000/03/02 10:28:54 rgbecker Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    66
__doc__=""" 
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
PDFgen is a library to generate PDF files containing text and graphics.  It is the 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
foundation for a complete reporting solution in Python.  It is also the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
foundation for piddlePDF, the PDF back end for PIDDLE.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    71
Documentation is a little slim right now; run then look at testpdfgen.py
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    72
to get a clue.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    73
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    74
Progress Reports:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    75
8.83, 2000-01-13, gmcm:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    76
    Packagizing:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    77
        renamed from pdfgen.py to canvas.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    78
        broke out PDFTextObject to textobject.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    79
        broke out PDFPathObject to pathobject.py
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    80
        placed all three in a package directory named pdfgen
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    81
0.82, 1999-10-27, AR:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    82
        Fixed some bugs on printing to Postscript.  Added 'Text Object'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    83
        analogous to Path Object to control entry and exit from text mode.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    84
        Much simpler clipping API.  All verified to export Postscript and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    85
        redistill.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    86
        One limitation still - clipping to text paths is fine in Acrobat
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    87
        but not in Postscript (any level)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    88
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    89
0.81,1999-10-13, AR:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    90
        Adding RoundRect; changed all format strings to use %0.2f instead of %s,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    91
        so we don't get exponentials in the output.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    92
0.8,1999-10-07, AR:  all changed!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    93
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    94
##  0.81    1999-10-13:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    95
##                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    96
##
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    97
##
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    98
import os
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    99
import sys
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   100
import string
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   101
import time
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   102
import tempfile
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   103
import cStringIO
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   104
from types import *
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   105
from math import sin, cos, tan, pi, ceil
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   106
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   107
from reportlab.pdfbase import pdfutils
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   108
from reportlab.pdfbase import pdfdoc
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   109
from reportlab.pdfbase import pdfmetrics
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   110
from reportlab.pdfgen  import pdfgeom, pathobject, textobject
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   111
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   112
# Robert Kern
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
# Constants for closing paths.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   114
# May be useful if one changes 'arc' and 'rect' to take a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   115
# default argument that tells how to close the path.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   116
# That way we can draw filled shapes.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   117
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   118
FILL_EVEN_ODD = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   119
FILL_NON_ZERO = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   120
    #this is used by path-closing routines.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   121
    #map stroke, fill, fillmode -> operator
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   122
    # fillmode: 1 = non-Zero (obviously), 0 = evenOdd
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   123
PATH_OPS = {(0, 0, FILL_EVEN_ODD) : 'n',  #no op
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   124
            (0, 0, FILL_NON_ZERO) : 'n',  #no op
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   125
            (1, 0, FILL_EVEN_ODD) : 'S',  #stroke only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   126
            (1, 0, FILL_NON_ZERO) : 'S',  #stroke only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   127
            (0, 1, FILL_EVEN_ODD) : 'f*',  #Fill only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   128
            (0, 1, FILL_NON_ZERO) : 'f',  #Fill only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   129
            (1, 1, FILL_EVEN_ODD) : 'B*',  #Stroke and Fill
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   130
            (1, 1, FILL_NON_ZERO) : 'B',  #Stroke and Fill
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   131
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   132
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   133
class Canvas:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   134
    """This is a low-level interface to the PDF file format.  The plan is to
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   135
    expose the whole pdfgen API through this.  Its drawing functions should have a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   136
    one-to-one correspondence with PDF functionality.  Unlike PIDDLE, it thinks
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   137
    in terms of RGB values, Postscript font names, paths, and a 'current graphics
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   138
    state'.  Just started development at 5/9/99, not in use yet.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   139
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   140
    """
18
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
   141
    def __init__(self,filename,pagesize=(595.27,841.89), bottomup = 1, pageCompression=0 ):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   142
        """Most of the attributes are private - we will use set/get methods
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   143
        as the preferred interface.  Default page size is A4."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   144
        self._filename = filename
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   145
        self._doc = pdfdoc.PDFDocument()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   146
        self._pagesize = pagesize
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
   147
        self._currentPageHasImages = 0
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   148
        self._pageTransitionString = ''
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   149
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
   150
        self._pageCompression = pageCompression  #off by default - turn on when we're happy!
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   151
        self._pageNumber = 1   # keep a count
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   152
        self._code = []    #where the current page's marking operators accumulate
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   153
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   154
        #PostScript has the origin at bottom left. It is easy to achieve a top-
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   155
        #down coord system by translating to the top of the page and setting y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   156
        #scale to -1, but then text is inverted.  So self.bottomup is used
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   157
        #to also set the text matrix accordingly.  You can now choose your
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   158
        #drawing coordinates.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   159
        self.bottomup = bottomup
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   160
        self._make_preamble()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   161
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   162
        #initial graphics state
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   163
        self._x = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   164
        self._y = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   165
        self._fontname = 'Times-Roman'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   166
        self._fontsize = 12
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   167
        self._textMode = 0  #track if between BT/ET
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   168
        self._leading = 14.4
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   169
        self._currentMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   170
        self._fillMode = 0   #even-odd
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   172
        #text state        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
        self._charSpace = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   174
        self._wordSpace = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
        self._horizScale = 100
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   176
        self._textRenderMode = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   177
        self._rise = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   178
        self._textLineMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   179
        self._textMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   180
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   181
        # line drawing        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   182
        self._lineCap = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   183
        self._lineJoin = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   184
        self._lineDash = None  #not done
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   185
        self._lineWidth = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   186
        self._mitreLimit = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   187
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   188
        self._fillColorRGB = (0,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   189
        self._strokeColorRGB = (0,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   190
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   191
    def _make_preamble(self):
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   192
        if self.bottomup:
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   193
            #set initial font
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   194
            #self._preamble = 'BT /F9 12 Tf 14.4 TL ET'
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   195
            self._preamble = '1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET'
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   196
        else:
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   197
            #switch coordinates, flip text and set font
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   198
            #self._preamble = '1 0 0 -1 0 %0.2f cm BT /F9 12 Tf 14.4 TL ET' % self._pagesize[1]
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   199
            self._preamble = '1 0 0 -1 0 %0.2f cm BT /F9 12 Tf 14.4 TL ET' % self._pagesize[1]
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   200
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   201
    def _escape(self, s):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
        """PDF escapes are like Python ones, but brackets need slashes before them too.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   203
        Use Python's repr function and chop off the quotes first"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   204
        s = repr(s)[1:-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   205
        s = string.replace(s, '(','\(')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   206
        s = string.replace(s, ')','\)')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   207
        return s
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   208
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   209
    #info functions - non-standard
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   210
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   211
        self._doc.setAuthor(author)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   212
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   213
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   214
        self._doc.setTitle(title)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   215
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   216
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   217
        self._doc.setSubject(subject)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   218
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   219
    def pageHasData(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   220
        "Info function - app can call it after showPage to see if it needs a save"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   221
        return len(self._code) == 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   222
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   223
    def showPage(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   224
        """This is where the fun happens"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   225
        page = pdfdoc.PDFPage()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   226
        page.pagewidth = self._pagesize[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   227
        page.pageheight = self._pagesize[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   228
        page.hasImages = self._currentPageHasImages
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   229
        page.pageTransitionString = self._pageTransitionString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   230
        page.setCompression(self._pageCompression)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   231
        #print stream
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   232
        page.setStream([self._preamble] + self._code)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   233
        self._doc.addPage(page)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   234
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   235
        #now get ready for the next one
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   236
        self._pageNumber = self._pageNumber + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   237
        self._code = []    # ready for more...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   238
        self._currentPageHasImages = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   239
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   240
    def getPageNumber(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   241
        return self._pageNumber
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   242
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   243
    def save(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   244
        """Saves the file.  If holding data, do
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   245
        a showPage() to save them having to."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
        if len(self._code):  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   247
            self.showPage()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   248
        self._doc.SaveToFile(self._filename)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   249
        print 'saved', self._filename
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   250
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   251
    def setPageSize(self, size):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   252
        """accepts a 2-tuple in points for paper size for this
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   253
        and subsequent pages"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   254
        self._pagesize = size
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   255
        self._make_preamble()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   256
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   257
    def addLiteral(self, s, escaped=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   258
        if escaped==0:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   259
            s = self._escape(s)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   260
        self._code.append(s)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   261
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   263
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
        #      coordinate transformations
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   267
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   268
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   269
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   270
    def transform(self, a,b,c,d,e,f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   271
        """How can Python track this?"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
        a0,b0,c0,d0,e0,f0 = self._currentMatrix
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
        self._currentMatrix = (a0*a+c0*b,    b0*a+d0*b,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
                               a0*c+c0*d,    b0*c+d0*d,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
                               a0*e+c0*f+e0, b0*e+d0*f+f0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f cm' % (a,b,c,d,e,f))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
    def translate(self, dx, dy):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
        self.transform(1,0,0,1,dx,dy)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   280
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   281
    def scale(self, x, y):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   282
        self.transform(x,0,0,y,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   283
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   284
    def rotate(self, theta):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   285
        """Canvas.rotate(theta)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   286
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   287
        theta is in degrees."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   288
        c = cos(theta * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   289
        s = sin(theta * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   290
        self.transform(c, s, -s, c, 0, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   291
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   292
    def skew(self, alpha, beta):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   293
        tanAlpha = tan(alpha * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   294
        tanBeta  = tan(beta  * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   295
        self.transform(1, tanAlpha, tanBeta, 1, 0, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   296
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   297
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   298
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   299
        #      graphics state management
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   300
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   301
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   302
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   303
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   305
    def saveState(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   306
        """These need expanding to save/restore Python's state tracking too"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   307
        self._code.append('q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   308
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   309
    def restoreState(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   310
        """These need expanding to save/restore Python's state tracking too"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
        self._code.append('Q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
        ###############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   315
        #   Drawing methods.  These draw things directly without
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   316
        #   fiddling around with Path objects.  We can add any geometry
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   317
        #   methods we wish as long as their meaning is precise and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   318
        #   they are of general use.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   319
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   320
        #   In general there are two patterns.  Closed shapes
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   321
        #   have the pattern shape(self, args, stroke=1, fill=0);
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   322
        #   by default they draw an outline only. Line segments come
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   323
        #   in three flavours: line, bezier, arc (which is a segment
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   324
        #   of an elliptical arc, approximated by up to four bezier
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   325
        #   curves, one for each quadrant.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   326
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   327
        #   In the case of lines, we provide a 'plural' to unroll
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   328
        #   the inner loop; it is useful for drawing big grids
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   329
        ################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   330
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   331
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   332
        #--------first the line drawing methods-----------------------
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   333
    def line(self, x1,y1, x2,y2):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   334
        "As it says"       
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   335
        self._code.append('n %0.2f %0.2f m %0.2f %0.2f l S' % (x1, y1, x2, y2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   336
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   337
    def lines(self, linelist):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   338
        """As line(), but slightly more efficient for lots of them -
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   339
        one stroke operation and one less function call"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   340
        self._code.append('n')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   341
        for (x1,y1,x2,y2) in linelist:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   342
            self._code.append('%0.2f %0.2f m %0.2f %0.2f l' % (x1, y1, x2, y2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   343
        self._code.append('S')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   344
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   345
    def grid(self, xlist, ylist):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   346
        """Lays out a grid in current line style.  Suuply list of
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   347
        x an y positions."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   348
        assert len(xlist) > 1, "x coordinate list must have 2+ items"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   349
        assert len(ylist) > 1, "y coordinate list must have 2+ items"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   350
        lines = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   351
        y0, y1 = ylist[0], ylist[-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   352
        x0, x1 = xlist[0], xlist[-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   353
        for x in xlist:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   354
            lines.append(x,y0,x,y1)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   355
        for y in ylist:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   356
            lines.append(x0,y,x1,y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   357
        self.lines(lines)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   358
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   359
    def bezier(self, x1, y1, x2, y2, x3, y3, x4, y4):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   360
        "Bezier curve with the four given control points"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   361
        self._code.append('n %0.2f %0.2f m %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c S' %
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   362
                          (x1, y1, x2, y2, x3, y3, x4, y4)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   363
                          )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   364
    def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   365
        """Contributed to piddlePDF by Robert Kern, 28/7/99.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   366
        Trimmed down by AR to remove color stuff for pdfgen.canvas and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   367
        revert to positive coordinates.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   368
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   369
        Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   370
        starting at startAng degrees and covering extent degrees.   Angles
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   371
        start with 0 to the right (+x) and increase counter-clockwise.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   372
        These should have x1<x2 and y1<y2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   373
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   374
        The algorithm is an elliptical generalization of the formulae in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   375
        Jim Fitzsimmon's TeX tutorial <URL: http://www.tinaja.com/bezarc1.pdf>."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   376
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   377
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   378
        #move to first point
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   379
        self._code.append('n %0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   380
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   381
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   382
        # stroke
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   383
        self._code.append('S')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   384
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   385
        #--------now the shape drawing methods-----------------------
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   386
    def rect(self, x, y, width, height, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   387
        "draws a rectangle"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   388
        self._code.append('n %0.2f %0.2f %0.2f %0.2f re ' % (x, y, width, height)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   389
                          + PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   390
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   391
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   392
    def ellipse(self, x1, y1, x2, y2, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   393
        """Uses bezierArc, which conveniently handles 360 degrees -
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   394
        nice touch Robert"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   395
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, 0, 360)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   396
        #move to first point
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   397
        self._code.append('n %0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   398
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   399
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   400
        #finish
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   401
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   402
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   403
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   404
    def wedge(self, x1,y1, x2,y2, startAng, extent, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   405
        """Like arc, but connects to the centre of the ellipse.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   406
        Most useful for pie charts and PacMan!"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   407
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   408
        x_cen  = (x1+x2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   409
        y_cen  = (y1+y2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   410
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   411
  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   412
        self._code.append('n %0.2f %0.2f m' % (x_cen, y_cen))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   413
        # Move the pen to the center of the rectangle
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   414
        self._code.append('%0.2f %0.2f l' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   415
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   416
            self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c' % curve[2:])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   417
        # finish the wedge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   418
        self._code.append('%0.2f %0.2f l ' % (x_cen, y_cen))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   419
        # final operator
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   420
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   421
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   422
    def circle(self, x_cen, y_cen, r, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   423
        """special case of ellipse"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   424
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   425
        x1 = x_cen - r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   426
        x2 = x_cen + r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   427
        y1 = y_cen - r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   428
        y2 = y_cen + r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   429
        self.ellipse(x1, y1, x2, y2, stroke, fill)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   430
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   431
    def roundRect(self, x, y, width, height, radius, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   432
        """Draws a rectangle with rounded corners.  The corners are
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   433
        approximately quadrants of a circle, with the given radius."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   434
        #use a precomputed set of factors for the bezier approximation
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   435
        #to a circle. There are six relevant points on the x axis and y axis.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   436
        #sketch them and it should all make sense!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   437
        t = 0.4472 * radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   438
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   439
        x0 = x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   440
        x1 = x0 + t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   441
        x2 = x0 + radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   442
        x3 = x0 + width - radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   443
        x4 = x0 + width - t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   444
        x5 = x0 + width
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   445
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   446
        y0 = y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   447
        y1 = y0 + t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   448
        y2 = y0 + radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   449
        y3 = y0 + height - radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   450
        y4 = y0 + height - t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   451
        y5 = y0 + height
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   452
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   453
        self._code.append('n %0.2f %0.2f m' % (x2, y0))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   454
        self._code.append('%0.2f %0.2f l' % (x3, y0))  # bottom row
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   455
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   456
                         % (x4, y0, x5, y1, x5, y2)) # bottom right
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   457
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   458
        self._code.append('%0.2f %0.2f l' % (x5, y3))  # right edge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   459
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   460
                         % (x5, y4, x4, y5, x3, y5)) # top right
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   461
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   462
        self._code.append('%0.2f %0.2f l' % (x2, y5))  # top row
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   463
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   464
                         % (x1, y5, x0, y4, x0, y3)) # top left
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   465
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   466
        self._code.append('%0.2f %0.2f l' % (x0, y2))  # left edge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   467
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   468
                         % (x0, y1, x1, y0, x2, y0)) # bottom left
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   469
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   470
        self._code.append('h')  #close off, although it should be where it started anyway
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   471
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   472
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   473
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   474
        ##################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   475
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   476
        #  Text methods
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   477
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   478
        # As with graphics, a separate object ensures that
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   479
        # everything is bracketed between  text operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   480
        # The methods below are a high-level convenience.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   481
        # use PDFTextObject for multi-line text.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   482
        ##################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   483
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   484
    def drawString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   485
        """Draws a string in the current text styles."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   486
        #we could inline this for speed if needed
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   487
        t = self.beginText(x, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   488
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   489
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   490
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   491
    def drawRightString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   492
        """Draws a string right-aligned with the y coordinate"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   493
        width = self.stringWidth(text, self._fontname, self._fontsize)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   494
        t = self.beginText(x - width, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   495
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   496
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   497
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   498
    def drawCentredString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   499
        """Draws a string right-aligned with the y coordinate.  I
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   500
        am British so the spelling is correct, OK?"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   501
        width = self.stringWidth(text, self._fontname, self._fontsize)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   502
        t = self.beginText(x - 0.5*width, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   503
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   504
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   505
  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   506
    def getAvailableFonts(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   507
        """Returns the list of PostScript font names available.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   508
        Standard set now, but may grow in future with font embedding."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   509
        fontnames = self._doc.getAvailableFonts()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   510
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   511
        return fontnames
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   512
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   513
    def setFont(self, psfontname, size, leading = None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   514
        """Sets the font.  If leading not specified, defaults to 1.2 x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   515
        font size. Raises a readable exception if an illegal font
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   516
        is supplied.  Font names are case-sensitive! Keeps track
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   517
        of font anme and size for metrics."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   518
        self._fontname = psfontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   519
        self._fontsize = size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   520
        pdffontname = self._doc.getInternalFontName(psfontname)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   521
        if leading is None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   522
            leading = size * 1.2
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   523
        self._leading = leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   524
        self._code.append('BT %s %0.1f Tf %0.1f TL ET' % (pdffontname, size, leading))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   525
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   526
    def stringWidth(self, text, fontname, fontsize):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   527
        "gets width of a string in the given font and size"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   528
        return pdfmetrics.stringwidth(text, fontname) * 0.001 * fontsize
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   529
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   530
    # basic graphics modes
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   531
    def setLineWidth(self, width):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   532
        self._lineWidth = width
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   533
        self._code.append('%0.2f w' % width)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   534
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   535
    def setLineCap(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   536
        """0=butt,1=round,2=square"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   537
        assert mode in (0,1,2), "Line caps allowed: 0=butt,1=round,2=square"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   538
        self._lineCap = mode
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   539
        self._code.append('%d J' % mode)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   540
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   541
    def setLineJoin(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   542
        """0=mitre, 1=round, 2=bevel"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   543
        assert mode in (0,1,2), "Line Joins allowed: 0=mitre, 1=round, 2=bevel"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   544
        self._lineJoin = mode
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   545
        self._code.append('%d j' % mode)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   546
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   547
    def setMiterLimit(self, limit):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   548
        self._miterLimit = limit
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   549
        self._code.append('%0.2f M' % limit)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   550
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   551
    def setDash(self, array=[], phase=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   552
        """Two notations.  pass two numbers, or an array and phase"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   553
        if type(array) == IntType or type(array) == FloatType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   554
            self._code.append('[%s %s] 0 d' % (array, phase))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   555
        elif type(array) == ListType or type(Array) == TupleType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   556
            assert phase <= len(array), "setDash phase must be l.t.e. length of array"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   557
            textarray = string.join(map(str, array))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   558
            self._code.append('[%s] %s d' % (textarray, phase))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   559
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   560
    def setFillColorRGB(self, r, g, b):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   561
        self._fillColorRGB = (r, g, b)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   562
        self._code.append('%0.2f %0.2f %0.2f rg' % (r,g,b))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   563
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   564
    def setStrokeColorRGB(self, r, g, b):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   565
        self._strokeColorRGB = (r, g, b)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   566
        self._code.append('%0.2f %0.2f %0.2f RG' % (r,g,b))
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   567
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   568
    def setFillGray(self, gray):
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   569
        """Sets the gray level; 0.0=black, 1.0=white"""
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   570
        self._fillColorRGB = (gray, gray, gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   571
        self._code.append('%0.2f g' % gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   572
        
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   573
    def setStrokeGray(self, gray):
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   574
        """Sets the gray level; 0.0=black, 1.0=white"""
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   575
        self._strokeColorRGB = (gray, gray, gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   576
        self._code.append('%0.2f G' % gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   577
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   578
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   579
    # path stuff - the separate path object builds it    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   580
    def beginPath(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   581
        """Returns a fresh path object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   582
        return pathobject.PDFPathObject()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   583
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   584
    def drawPath(self, aPath, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   585
        "Draw in the mode indicated"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   586
        op = PATH_OPS[stroke, fill, self._fillMode]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   587
        self._code.append(aPath.getCode() + ' ' + op)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   588
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   589
    def clipPath(self, aPath, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   590
        "clip as well as drawing"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   591
        op = PATH_OPS[stroke, fill, self._fillMode]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   592
        self._code.append(aPath.getCode() + ' W ' + op)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   593
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   594
    def beginText(self, x=0, y=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   595
        """Returns a fresh text object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   596
        return textobject.PDFTextObject(self, x, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   597
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   598
    def drawText(self, aTextObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   599
        """Draws a text object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   600
        self._code.append(aTextObject.getCode())
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   601
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   602
        ######################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   603
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   604
        #   Image routines
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   605
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   606
        ######################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   607
    def drawInlineImage(self, image, x,y, width=None,height=None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   608
        """Draw a PIL Image into the specified rectangle.  If width and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   609
        height are omitted, they are calculated from the image size.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   610
        Also allow file names as well as images.  This allows a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   611
        caching mechanism"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   612
        try: 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   613
            import Image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   614
        except ImportError:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   615
            print 'Python Imaging Library not available'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   616
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   617
        try:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   618
            import zlib
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   619
        except ImportError:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   620
            print 'zlib not available'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   621
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   622
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   623
        self.currentPageHasImages = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   624
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   625
        if type(image) == StringType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   626
            if os.path.splitext(image)[1] in ['.jpg', '.JPG']:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   627
            #directly process JPEG files
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   628
            #open file, needs some error handling!!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   629
                imageFile = open(image, 'rb')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   630
                info = self.readJPEGInfo(imageFile)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   631
                imgwidth, imgheight = info[0], info[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   632
                if info[2] == 1:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   633
                    colorSpace = 'DeviceGray'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   634
                elif info[2] == 3:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   635
                    colorSpace = 'DeviceRGB'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   636
                else: #maybe should generate an error, is this right for CMYK?
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   637
                    colorSpace = 'DeviceCMYK'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   638
                imageFile.seek(0)		#reset file pointer
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   639
                imagedata = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   640
                imagedata.append('BI')   # begin image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   641
                # this describes what is in the image itself
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   642
                imagedata.append('/Width %0.2f /Height %0.2f' %(info[0], info[1]))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   643
                imagedata.append('/BitsPerComponent 8')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   644
                imagedata.append('/ColorSpace /%s' % colorSpace)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   645
                imagedata.append('/Filter [ /ASCII85Decode /DCTDecode]')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   646
                imagedata.append('ID')   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   647
                #write in blocks of (??) 60 characters per line to a list
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   648
                compressed = imageFile.read()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   649
                encoded = pdfutils._AsciiBase85Encode(compressed)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   650
                outstream = cStringIO.StringIO(encoded)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   651
                dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   652
                while dataline <> "":
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   653
                    imagedata.append(dataline)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   654
                    dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   655
                imagedata.append('EI')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   656
            else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   657
                if not pdfutils.cachedImageExists(image):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   658
                    pdfutils.cacheImageFile(image)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   659
                #now we have one cached, slurp it in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   660
                cachedname = os.path.splitext(image)[0] + '.a85'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   661
                imagedata = open(cachedname,'rb').readlines()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   662
                #trim off newlines...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   663
                imagedata = map(string.strip, imagedata)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   664
                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   665
                #parse line two for width, height
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   666
                words = string.split(imagedata[1])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   667
                imgwidth = string.atoi(words[1])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   668
                imgheight = string.atoi(words[3])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   669
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   670
            #PIL Image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   671
            #work out all dimensions
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   672
            myimage = image.convert('RGB')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   673
            imgwidth, imgheight = myimage.size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   674
            imagedata = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   675
            imagedata.append('BI')   # begin image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   676
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   677
            # this describes what is in the image itself
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   678
            imagedata.append('/W %0.2f /H %0.2f /BPC 8 /CS /RGB /F [/A85 /Fl]' % (imgwidth, imgheight))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   679
            imagedata.append('ID')   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   680
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   681
            #use a flate filter and Ascii Base 85 to compress
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   682
            raw = myimage.tostring()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   683
            assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   684
            compressed = zlib.compress(raw)   #this bit is very fast...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   685
            encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   686
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   687
            #write in blocks of (??) 60 characters per line to a list
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   688
            outstream = cStringIO.StringIO(encoded)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   689
            dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   690
            while dataline <> "":
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   691
                imagedata.append(dataline)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   692
                dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   693
            imagedata.append('EI')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   694
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   695
        #now build the PDF for the image.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   696
        if not width:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   697
            width = imgwidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   698
        if not height:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   699
            height = imgheight
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   700
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   701
        # this says where and how big to draw it
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   702
        #self._code.append('ET')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   703
        #self._code.append('q %0.2f 0 0 %0.2f %0.2f %0.2f cm' % (width, height, x, y+height))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   704
        if self.bottomup:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   705
            self._code.append('q %0.2f 0 0 %0.2f %0.2f %0.2f cm' % (width, height, x, y))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   706
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   707
            self._code.append('q %0.2f 0 0 %0.2f %0.2f %0.2f cm' % (width, height, x, y+height))
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   708
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   709
		# self._code.extend(imagedata) if >=python-1.5.2
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   710
        for line in imagedata:
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   711
       	    self._code.append(line)
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   712
			
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   713
        self._code.append('Q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   714
        #self._code.append('BT')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   715
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   716
#########################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   717
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   718
#  JPEG processing code - contributed by Eric Johnson
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   719
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   720
#########################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   721
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   722
    # Read data from the JPEG file. We should probably be using PIL to
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   723
    # get this information for us -- but this way is more fun!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   724
    # Returns (width, height, color components) as a triple
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   725
    # This is based on Thomas Merz's code from GhostScript (viewjpeg.ps)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   726
    def readJPEGInfo(self, image):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   727
        "Read width, height and number of components from JPEG file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   728
    	import struct
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   729
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   730
	#Acceptable JPEG Markers:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   731
	#  SROF0=baseline, SOF1=extended sequential or SOF2=progressive
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   732
	validMarkers = [0xC0, 0xC1, 0xC2]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   733
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   734
	#JPEG markers without additional parameters
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   735
	noParamMarkers = \
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   736
	    [ 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0x01 ]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   737
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   738
	#Unsupported JPEG Markers
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   739
	unsupportedMarkers = \
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   740
	    [ 0xC3, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF ]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   741
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   742
	#read JPEG marker segments until we find SOFn marker or EOF
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   743
	done = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   744
	while not done:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   745
	    x = struct.unpack('B', image.read(1))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   746
	    if x[0] == 0xFF:			#found marker
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   747
	    	x = struct.unpack('B', image.read(1))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   748
		#print "Marker: ", '%0.2x' % x[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   749
		#check marker type is acceptable and process it
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   750
		if x[0] in validMarkers:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   751
		    image.seek(2, 1)		#skip segment length
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   752
		    x = struct.unpack('B', image.read(1)) #data precision
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   753
		    if x[0] != 8:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   754
			raise 'PDFError', ' JPEG must have 8 bits per component'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   755
		    y = struct.unpack('BB', image.read(2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   756
		    height = (y[0] << 8) + y[1] 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   757
		    y = struct.unpack('BB', image.read(2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   758
		    width =  (y[0] << 8) + y[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   759
		    y = struct.unpack('B', image.read(1))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   760
		    color =  y[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   761
		    return width, height, color
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   762
		    done = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   763
		elif x[0] in unsupportedMarkers:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   764
		    raise 'PDFError', ' Unsupported JPEG marker: %0.2x' % x[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   765
		elif x[0] not in noParamMarkers:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   766
		    #skip segments with parameters
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   767
		    #read length and skip the data
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   768
		    x = struct.unpack('BB', image.read(2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   769
		    image.seek( (x[0] << 8) + x[1] - 2, 1)	
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   770
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   771
    def setPageCompression(self, onoff=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   772
        """Possible values 1 or 0 (1 for 'on' is the default).
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   773
        If on, the page data will be compressed, leading to much
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   774
        smaller files, but takes a little longer to create the files.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   775
        This applies to all subsequent pages, or until setPageCompression()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   776
        is next called."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   777
        self._pageCompression = onoff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   778
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   779
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   780
    def setPageTransition(self, effectname=None, duration=1, 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   781
                        direction=0,dimension='H',motion='I'):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   782
        """PDF allows page transition effects for use when giving
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   783
        presentations.  There are six possible effects.  You can
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   784
        just guive the effect name, or supply more advanced options
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   785
        to refine the way it works.  There are three types of extra
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   786
        argument permitted, and here are the allowed values:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   787
            direction_arg = [0,90,180,270]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   788
            dimension_arg = ['H', 'V']
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   789
            motion_arg = ['I','O'] (start at inside or outside)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   790
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   791
        This table says which ones take which arguments:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   792
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   793
        PageTransitionEffects = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   794
            'Split': [direction_arg, motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   795
            'Blinds': [dimension_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   796
            'Box': [motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   797
            'Wipe' : [direction_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   798
            'Dissolve' : [],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   799
            'Glitter':[direction_arg]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   800
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   801
        Have fun!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   802
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   803
        if not effectname:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   804
            self._pageTransitionString = ''
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   805
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   806
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   807
        #first check each optional argument has an allowed value
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   808
        if direction in [0,90,180,270]:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   809
            direction_arg = '/Di /%d' % direction
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   810
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   811
            raise 'PDFError', ' directions allowed are 0,90,180,270'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   812
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   813
        if dimension in ['H', 'V']:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   814
            dimension_arg = '/Dm /%s' % dimension
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   815
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   816
            raise'PDFError','dimension values allowed are H and V'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   817
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   818
        if motion in ['I','O']:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   819
            motion_arg = '/M /%s' % motion
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   820
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   821
            raise'PDFError','motion values allowed are I and O'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   822
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   823
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   824
        # this says which effects require which argument types from above
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   825
        PageTransitionEffects = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   826
            'Split': [direction_arg, motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   827
            'Blinds': [dimension_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   828
            'Box': [motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   829
            'Wipe' : [direction_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   830
            'Dissolve' : [],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   831
            'Glitter':[direction_arg]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   832
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   833
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   834
        try:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   835
            args = PageTransitionEffects[effectname]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   836
        except KeyError:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   837
            raise 'PDFError', 'Unknown Effect Name "%s"' % effectname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   838
            self._pageTransitionString = ''
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   839
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   840
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   841
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   842
        self._pageTransitionString = (('/Trans <</D %d /S /%s ' % (duration, effectname)) + 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   843
            string.join(args, ' ') + ' >>')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   844
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   845
if __name__ == '__main__':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   846
    print 'For test scripts, run testpdfgen.py'