reportlab/pdfgen/canvas.py
author andy_robinson
Tue, 23 May 2000 14:06:45 +0000
changeset 240 022c939a6d55
parent 232 150d4caf48c7
child 247 5d531acd7059
permissions -rwxr-xr-x
Preformatted objects now know how to split themselves.
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 $
240
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
    34
#	Revision 1.37  2000/05/23 14:06:45  andy_robinson
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
    35
#	Preformatted objects now know how to split themselves.
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
    36
#
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
    37
#	Revision 1.36  2000/05/18 17:11:12  aaron_watters
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
    38
#	removed 0's on stable linkage and outline operations.
240
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
    39
#	
228
d3aaff565043 Resynchronization
andy_robinson
parents: 181
diff changeset
    40
#	Revision 1.35  2000/05/18 09:05:08  andy_robinson
d3aaff565043 Resynchronization
andy_robinson
parents: 181
diff changeset
    41
#	Resynchronization
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
    42
#	
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
    43
#	Revision 1.34  2000/04/28 17:33:44  andy_robinson
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
    44
#	Added font encoding support and changed default encoding to WinAnsi
228
d3aaff565043 Resynchronization
andy_robinson
parents: 181
diff changeset
    45
#	
179
6098115c11aa Use str(filename) not '<Unknown>'
rgbecker
parents: 177
diff changeset
    46
#	Revision 1.33  2000/04/28 14:18:16  rgbecker
6098115c11aa Use str(filename) not '<Unknown>'
rgbecker
parents: 177
diff changeset
    47
#	Use str(filename) not '<Unknown>'
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
    48
#	
177
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
    49
#	Revision 1.32  2000/04/28 13:37:40  rgbecker
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
    50
#	Fix verbose filename print when it's actually a file type object
179
6098115c11aa Use str(filename) not '<Unknown>'
rgbecker
parents: 177
diff changeset
    51
#	
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
    52
#	Revision 1.31  2000/04/28 09:10:20  rgbecker
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
    53
#	Changed zlib error handling
177
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
    54
#	
166
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
    55
#	Revision 1.30  2000/04/25 20:20:13  aaron_watters
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
    56
#	Added support for closed outline entries
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
    57
#	
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
    58
#	Revision 1.29  2000/04/18 19:52:35  aaron_watters
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
    59
#	eliminated inForm/inPage apis in favor of only beginForm..endForm
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
    60
#	(page mode is automatically inferred when page-only operations are
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
    61
#	attempted...)
166
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
    62
#	
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
    63
#	Revision 1.28  2000/04/15 14:58:32  aaron_watters
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
    64
#	Completed addOutlineEntry0 api
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
    65
#	
127
44912033ce1b Removed illegal append statement usage in canvas.grid()
andy_robinson
parents: 117
diff changeset
    66
#	Revision 1.27  2000/04/14 11:28:32  andy_robinson
44912033ce1b Removed illegal append statement usage in canvas.grid()
andy_robinson
parents: 117
diff changeset
    67
#	Removed illegal append statement usage in canvas.grid()
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
    68
#	
117
fd3682228853 XML Tagged Paragraph parser changes
rgbecker
parents: 100
diff changeset
    69
#	Revision 1.26  2000/04/12 16:26:51  rgbecker
fd3682228853 XML Tagged Paragraph parser changes
rgbecker
parents: 100
diff changeset
    70
#	XML Tagged Paragraph parser changes
127
44912033ce1b Removed illegal append statement usage in canvas.grid()
andy_robinson
parents: 117
diff changeset
    71
#	
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
    72
#	Revision 1.25  2000/04/10 09:21:21  andy_robinson
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
    73
#	Color methods in textobject and canvas now synchronised.
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
    74
#	Added 'verbosity' keyword to allow hiding of 'save myfile.pdf' messages.
117
fd3682228853 XML Tagged Paragraph parser changes
rgbecker
parents: 100
diff changeset
    75
#	
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
    76
#	Revision 1.24  2000/04/06 09:52:02  andy_robinson
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
    77
#	Removed some old comments; tweaks to experimental Outline methods.
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
    78
#	
70
337c859f4de5 Fixes to setFill/StrokeColor
rgbecker
parents: 69
diff changeset
    79
#	Revision 1.23  2000/04/05 16:26:36  rgbecker
337c859f4de5 Fixes to setFill/StrokeColor
rgbecker
parents: 69
diff changeset
    80
#	Fixes to setFill/StrokeColor
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
    81
#	
69
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
    82
#	Revision 1.22  2000/04/05 16:21:02  rgbecker
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
    83
#	Added _SeqTypes for efficiency
70
337c859f4de5 Fixes to setFill/StrokeColor
rgbecker
parents: 69
diff changeset
    84
#	
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
    85
#	Revision 1.21  2000/04/05 16:15:11  rgbecker
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
    86
#	Made setFill/StrokeColor polymorphic
69
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
    87
#	
64
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
    88
#	Revision 1.20  2000/04/03 09:36:15  andy_robinson
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
    89
#	Using trailing zero convention for new form and link API
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
    90
#	
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
    91
#	Revision 1.19  2000/04/02 02:53:49  aaron_watters
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
    92
#	added support for outline trees
64
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
    93
#	
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
    94
#	Revision 1.18  2000/03/26 20:45:01  aaron_watters
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
    95
#	added beginForm..endForm and fixed some naming convention issues.
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
    96
#	
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
    97
#	Revision 1.17  2000/03/24 21:02:21  aaron_watters
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
    98
#	added support for destinations, forms, linkages
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
    99
#	
45
bfe68ff64924 fixed typo in setDash
andy_robinson
parents: 43
diff changeset
   100
#	Revision 1.15  2000/03/10 21:46:04  andy_robinson
bfe68ff64924 fixed typo in setDash
andy_robinson
parents: 43
diff changeset
   101
#	fixed typo in setDash
48
14c9e005289f Added CMYK color methods
andy_robinson
parents: 45
diff changeset
   102
#	
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   103
#	Revision 1.14  2000/03/08 13:40:03  andy_robinson
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   104
#	Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   105
#	which accepts color objects directly.
45
bfe68ff64924 fixed typo in setDash
andy_robinson
parents: 43
diff changeset
   106
#	
36
0ca883f42d46 Typo self._currentPageHasImages = 1
rgbecker
parents: 33
diff changeset
   107
#	Revision 1.13  2000/03/06 20:06:36  rgbecker
0ca883f42d46 Typo self._currentPageHasImages = 1
rgbecker
parents: 33
diff changeset
   108
#	Typo self._currentPageHasImages = 1
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   109
#	
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   110
#	Revision 1.12  2000/03/02 12:58:58  rgbecker
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   111
#	Remove over officious import checks Imag/zlib
36
0ca883f42d46 Typo self._currentPageHasImages = 1
rgbecker
parents: 33
diff changeset
   112
#	
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   113
#	Revision 1.11  2000/03/02 10:28:54  rgbecker
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   114
#	[].extend illegal in 1.5.1
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   115
#	
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   116
#	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
   117
#	Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
   118
#	
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
   119
#	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
   120
#	
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
   121
#	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
   122
#	
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
   123
#	Revision 1.8  2000/02/20 14:43:27  rgbecker
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
   124
#	_currentPageHasImages = 0 in init
26
ee96702d3008 Removed some constants which are no longer used.
andy_robinson
parents: 21
diff changeset
   125
#	
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   126
#	Revision 1.7  2000/02/20 11:08:56  rgbecker
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   127
#	Canvas.setPageSize fix
21
307573672f31 _currentPageHasImages = 0 in init
rgbecker
parents: 20
diff changeset
   128
#	
18
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
   129
#	Revision 1.6  2000/02/17 15:26:28  rgbecker
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
   130
#	Change page compression default
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   131
#	
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   132
#	Revision 1.5  2000/02/17 02:08:04  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   133
#	Docstring & other fixes
18
4c07a7e8649e Change page compression default
rgbecker
parents: 16
diff changeset
   134
#	
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   135
#	Revision 1.4  2000/02/16 09:42:50  rgbecker
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   136
#	Conversion to reportlab package
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   137
#	
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
   138
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
   139
#	License text fixes
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   140
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   141
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   142
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
   143
#	
240
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
   144
__version__=''' $Id: canvas.py,v 1.37 2000/05/23 14:06:45 andy_robinson Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   145
__doc__=""" 
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   146
PDFgen is a library to generate PDF files containing text and graphics.  It is the 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   147
foundation for a complete reporting solution in Python.  It is also the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   148
foundation for piddlePDF, the PDF back end for PIDDLE.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   149
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   150
Documentation is a little slim right now; run then look at testpdfgen.py
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   151
to get a clue.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   152
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   153
Progress Reports:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   154
8.83, 2000-01-13, gmcm:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   155
    Packagizing:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   156
        renamed from pdfgen.py to canvas.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   157
        broke out PDFTextObject to textobject.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   158
        broke out PDFPathObject to pathobject.py
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   159
        placed all three in a package directory named pdfgen
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   160
0.82, 1999-10-27, AR:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   161
        Fixed some bugs on printing to Postscript.  Added 'Text Object'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   162
        analogous to Path Object to control entry and exit from text mode.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   163
        Much simpler clipping API.  All verified to export Postscript and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   164
        redistill.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   165
        One limitation still - clipping to text paths is fine in Acrobat
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   166
        but not in Postscript (any level)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   167
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   168
0.81,1999-10-13, AR:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   169
        Adding RoundRect; changed all format strings to use %0.2f instead of %s,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   170
        so we don't get exponentials in the output.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
0.8,1999-10-07, AR:  all changed!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   172
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
##  0.81    1999-10-13:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   174
##                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
##
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   176
##
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   177
import os
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   178
import sys
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   179
import string
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   180
import time
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   181
import tempfile
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   182
import cStringIO
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   183
from types import *
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   184
from math import sin, cos, tan, pi, ceil
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   185
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   186
from reportlab.pdfbase import pdfutils
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   187
from reportlab.pdfbase import pdfdoc
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
   188
from reportlab.pdfbase import pdfmetrics
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   189
from reportlab.pdfgen  import pdfgeom, pathobject, textobject
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   190
from reportlab.lib.colors import ColorType
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   191
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   192
try:
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   193
	import zlib
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   194
except ImportError:
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   195
	zlib = None
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   196
69
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
   197
_SeqTypes=(TupleType,ListType)
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
   198
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   199
# Robert Kern
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   200
# Constants for closing paths.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   201
# May be useful if one changes 'arc' and 'rect' to take a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
# default argument that tells how to close the path.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   203
# That way we can draw filled shapes.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   204
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   205
FILL_EVEN_ODD = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   206
FILL_NON_ZERO = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   207
    #this is used by path-closing routines.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   208
    #map stroke, fill, fillmode -> operator
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   209
    # fillmode: 1 = non-Zero (obviously), 0 = evenOdd
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   210
PATH_OPS = {(0, 0, FILL_EVEN_ODD) : 'n',  #no op
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   211
            (0, 0, FILL_NON_ZERO) : 'n',  #no op
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   212
            (1, 0, FILL_EVEN_ODD) : 'S',  #stroke only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   213
            (1, 0, FILL_NON_ZERO) : 'S',  #stroke only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   214
            (0, 1, FILL_EVEN_ODD) : 'f*',  #Fill only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   215
            (0, 1, FILL_NON_ZERO) : 'f',  #Fill only
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   216
            (1, 1, FILL_EVEN_ODD) : 'B*',  #Stroke and Fill
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   217
            (1, 1, FILL_NON_ZERO) : 'B',  #Stroke and Fill
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   218
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   219
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   220
class Canvas:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   221
    """This is a low-level interface to the PDF file format.  The plan is to
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   222
    expose the whole pdfgen API through this.  Its drawing functions should have a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   223
    one-to-one correspondence with PDF functionality.  Unlike PIDDLE, it thinks
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   224
    in terms of RGB values, Postscript font names, paths, and a 'current graphics
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   225
    state'.  Just started development at 5/9/99, not in use yet.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   226
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   227
    """
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   228
    def __init__(self,filename,
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   229
                 pagesize=(595.27,841.89),
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   230
                 bottomup = 1,
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   231
                 pageCompression=0,
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
   232
				 encoding=pdfdoc.DEFAULT_ENCODING,
240
022c939a6d55 Preformatted objects now know how to split themselves.
andy_robinson
parents: 232
diff changeset
   233
                 verbosity=0):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   234
        """Most of the attributes are private - we will use set/get methods
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   235
        as the preferred interface.  Default page size is A4."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   236
        self._filename = filename
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
   237
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
   238
        self._doc = pdfdoc.PDFDocument(encoding)
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   239
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   240
        #this only controls whether it prints 'saved ...' - 0 disables
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   241
        self._verbosity = verbosity
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
   242
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 179
diff changeset
   243
		
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   244
        self._pagesize = pagesize
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   245
        #self._currentPageHasImages = 0
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
        self._pageTransitionString = ''
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   247
        self._destinations = {} # dictionary of destinations for cross indexing.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   248
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
   249
        self.setPageCompression(pageCompression)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   250
        self._pageNumber = 1   # keep a count
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   251
        #self._code = []    #where the current page's marking operators accumulate
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   252
        self._restartAccumulators()  # restart all accumulation state (generalized, arw)
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   253
        self._annotationCount = 0
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   254
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   255
        self._outlines = [] # list for a name tree
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   256
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   257
        #PostScript has the origin at bottom left. It is easy to achieve a top-
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   258
        #down coord system by translating to the top of the page and setting y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   259
        #scale to -1, but then text is inverted.  So self.bottomup is used
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   260
        #to also set the text matrix accordingly.  You can now choose your
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   261
        #drawing coordinates.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
        self.bottomup = bottomup
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   263
        self._make_preamble()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
        #initial graphics state
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
        self._x = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   267
        self._y = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   268
        self._fontname = 'Times-Roman'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   269
        self._fontsize = 12
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   270
        self._textMode = 0  #track if between BT/ET
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   271
        self._leading = 14.4
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
        self._currentMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
        self._fillMode = 0   #even-odd
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
        #text state        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
        self._charSpace = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
        self._wordSpace = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
        self._horizScale = 100
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
        self._textRenderMode = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   280
        self._rise = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   281
        self._textLineMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   282
        self._textMatrix = (1., 0., 0., 1., 0., 0.)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   283
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   284
        # line drawing        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   285
        self._lineCap = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   286
        self._lineJoin = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   287
        self._lineDash = None  #not done
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   288
        self._lineWidth = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   289
        self._mitreLimit = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   290
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   291
        self._fillColorRGB = (0,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   292
        self._strokeColorRGB = (0,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   293
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   294
    def _make_preamble(self):
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   295
        if self.bottomup:
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   296
            #set initial font
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   297
            #self._preamble = 'BT /F9 12 Tf 14.4 TL ET'
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   298
            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
   299
        else:
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   300
            #switch coordinates, flip text and set font
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   301
            #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
   302
            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
   303
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
    def _escape(self, s):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   305
        """PDF escapes are like Python ones, but brackets need slashes before them too.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   306
        Use Python's repr function and chop off the quotes first"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   307
        s = repr(s)[1:-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   308
        s = string.replace(s, '(','\(')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   309
        s = string.replace(s, ')','\)')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   310
        return s
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
    #info functions - non-standard
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
        self._doc.setAuthor(author)
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   315
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   316
    def addOutlineEntry(self, title, key, level=0, closed=None):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   317
        """Adds a new entry to the outline at given level.  If LEVEL not specified,
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   318
        entry goes at the top level.  If level specified, it must be
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   319
        no more than 1 greater than the outline level in the last call.
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   320
        
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   321
        The key must be the (unique) name of a bookmark.
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   322
        the title is the (non-unique) name to be displayed for the entry.
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   323
        
166
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
   324
        If closed is set then the entry should show no subsections by default
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   325
        when displayed.
166
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
   326
        
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   327
        Example
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   328
           c.addOutlineEntry("first section", "section1")
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   329
           c.addOutlineEntry("introduction", "s1s1", 1, closed=1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   330
           c.addOutlineEntry("body", "s1s2", 1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   331
           c.addOutlineEntry("detail1", "s1s2s1", 2)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   332
           c.addOutlineEntry("detail2", "s1s2s2", 2)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   333
           c.addOutlineEntry("conclusion", "s1s3", 1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   334
           c.addOutlineEntry("further reading", "s1s3s1", 2)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   335
           c.addOutlineEntry("second section", "section1")
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   336
           c.addOutlineEntry("introduction", "s2s1", 1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   337
           c.addOutlineEntry("body", "s2s2", 1, closed=1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   338
           c.addOutlineEntry("detail1", "s2s2s1", 2)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   339
           c.addOutlineEntry("detail2", "s2s2s2", 2)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   340
           c.addOutlineEntry("conclusion", "s2s3", 1)
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   341
           c.addOutlineEntry("further reading", "s2s3s1", 2)
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   342
           
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   343
        note that you can jump from level 5 to level 3 but not
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   344
        from 3 to 5: instead you need to provide all intervening
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   345
        levels going down (4 in this case).  Note that titles can
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   346
        collide but keys cannot.
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   347
        
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   348
        """
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   349
        #to be completed
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   350
        #self._outlines.append(title)
166
5cead5d5a4a7 Added support for closed outline entries
aaron_watters
parents: 139
diff changeset
   351
        self._doc.outline.addOutlineEntry(key, level, title, closed=closed)
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   352
        
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   353
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   354
    def setOutlineNames0(self, *nametree):   # keep this for now (?)
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   355
        """nametree should can be a recursive tree like so
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   356
           c.setOutlineNames(
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   357
             "chapter1dest",
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   358
             ("chapter2dest",
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   359
              ["chapter2section1dest",
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   360
               "chapter2section2dest",
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   361
               "chapter2conclusiondest"]
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   362
             ), # end of chapter2 description
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   363
             "chapter3dest",
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   364
             ("chapter4dest", ["c4s1", "c4s2"])
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   365
             )
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   366
          each of the string names inside must be bound to a bookmark
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   367
          before the document is generated.
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   368
        """
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   369
        #print nametree
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   370
        apply(self._doc.outline.setNames, (self,)+nametree)
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   371
        
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   372
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   373
        self._doc.setTitle(title)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   374
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   375
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   376
        self._doc.setSubject(subject)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   377
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   378
    def pageHasData(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   379
        "Info function - app can call it after showPage to see if it needs a save"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   380
        return len(self._code) == 0
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   381
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   382
    def showOutline(self):
64
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
   383
        "Specify that Acrobat Reader should start with the outline tree visible"
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 54
diff changeset
   384
        self._doc._catalog.showOutline()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   385
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   386
    def showPage(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   387
        """This is where the fun happens"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   388
        page = pdfdoc.PDFPage()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   389
        page.pagewidth = self._pagesize[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   390
        page.pageheight = self._pagesize[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   391
        page.hasImages = self._currentPageHasImages
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   392
        page.pageTransitionString = self._pageTransitionString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   393
        page.setCompression(self._pageCompression)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   394
        #print stream
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   395
        page.setStream([self._preamble] + self._code)
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   396
        self._setXObjects(page)
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   397
        self._setAnnotations(page)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   398
        self._doc.addPage(page)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   399
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   400
        #now get ready for the next one
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   401
        self._pageNumber = self._pageNumber + 1
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   402
        self._restartAccumulators()
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   403
        
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   404
    def _setAnnotations(self,page):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   405
        page.Annots = self._annotationrefs
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   406
        
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   407
    def _setXObjects(self, thing):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   408
        """for pages and forms, define the XObject dictionary for resources, if needed"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   409
        forms = self._formsinuse
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   410
        if forms:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   411
            xobjectsdict = self._doc.xobjDict(forms)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   412
            thing.XObjects = xobjectsdict
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   413
        else:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   414
            thing.XObjects = None
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   415
            
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   416
    def _bookmarkReference(self, name):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   417
        """get a reference to a (possibly undefined, possibly unbound) bookmark"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   418
        d = self._destinations
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   419
        try:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   420
            return d[name]
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   421
        except:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   422
            result = d[name] = pdfdoc.Destination(name) # newly defined, unbound
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   423
        return result
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   424
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   425
    def bookmarkPage(self, name):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   426
        """bind a bookmark (destination) to the current page"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   427
        # XXXX there are a lot of other ways a bookmark destination can be bound: should be implemented.
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   428
        # XXXX the other ways require tracking of the graphics state....
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   429
        dest = self._bookmarkReference(name)
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   430
        self._doc.inPage() # try to enable page-only features
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   431
        pageref = self._doc.thisPageRef()
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   432
        dest.fit()
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   433
        dest.setPageRef(pageref)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   434
        return dest
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   435
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   436
    def bookmarkHorizontalAbsolute(self, name, yhorizontal):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   437
        """bind a bookmark (destination) to the current page at a horizontal position"""
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   438
        dest = self._bookmarkReference(name)
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   439
        self._doc.inPage() # try to enable page-only features
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   440
        pageref = self._doc.thisPageRef()
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   441
        dest.fith(yhorizontal)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   442
        dest.setPageRef(pageref)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   443
        return dest
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   444
        
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   445
    #def _inPage0(self):  disallowed!
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   446
    #    """declare a page, enable page features"""
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   447
    #    self._doc.inPage()
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   448
        
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   449
    #def _inForm0(self):
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   450
    #    "deprecated in favore of beginForm...endForm"
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   451
    #    self._doc.inForm()
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   452
            
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   453
    def doForm(self, name):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   454
        """use a form XObj in current operation stream"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   455
        internalname = self._doc.hasForm(name)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   456
        if not internalname:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   457
            raise ValueError, "form is not defined %s" % name
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   458
        self._code.append("/%s Do" % internalname)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   459
        self._formsinuse.append(name)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   460
        
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   461
    def _restartAccumulators(self):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   462
        self._code = []    # ready for more...
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   463
        self._currentPageHasImages = 1 # for safety...
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   464
        self._formsinuse = []
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   465
        self._annotationrefs = []
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   466
        self._formData = None
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   467
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   468
    def beginForm(self, name, lowerx=0, lowery=0, upperx=None, uppery=None):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   469
        "declare the current graphics stream to be a named form"
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   470
        self._formData = (name, lowerx, lowery, upperx, uppery)
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   471
        self._doc.inForm()
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   472
        #self._inForm0()
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   473
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   474
    def endForm(self):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   475
        """emit the current collection of graphics operations as a Form
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   476
           as declared previously in beginForm"""
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   477
        (name, lowerx, lowery, upperx, uppery) = self._formData
139
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   478
        #self.makeForm0(name, lowerx, lowery, upperx, uppery)
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   479
        # fall through!  makeForm0 disallowed
6369f06cf280 eliminated inForm/inPage apis in favor of only beginForm..endForm
aaron_watters
parents: 136
diff changeset
   480
        #def makeForm0(self, name, lowerx=0, lowery=0, upperx=None, uppery=None):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   481
        """Like showpage, but make a form using accumulated operations instead"""
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   482
        # deprecated in favor or beginForm(...)... endForm()
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   483
        (w,h) = self._pagesize
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   484
        if upperx is None: upperx=w
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   485
        if uppery is None: uppery=h
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   486
        form = pdfdoc.PDFFormXObject(lowerx=lowerx, lowery=lowery, upperx=upperx, uppery=uppery)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   487
        form.compression = self._pageCompression
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   488
        form.setStreamList([self._preamble] + self._code) # ??? minus preamble (seems to be needed!)
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   489
        self._setXObjects(form)
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   490
        self._setAnnotations(form)
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   491
        self._doc.addForm(name, form)
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   492
        self._restartAccumulators()
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   493
        
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   494
    #def forceCodeInsert0(self, code):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   495
    #    """I know a whole lot about PDF and I want to add a bunch of code I know will work..."""
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   496
    #    self._code.append(code)
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   497
        
64
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
   498
    def textAnnotation0(self, contents, Rect=None, addtopage=1, name=None, **kw):
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   499
        """Experimental.
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   500
        """
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   501
        if not Rect:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   502
            (w,h) = self._pagesize# default to whole page (?)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   503
            Rect = (0,0,w,h)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   504
        annotation = apply(pdfdoc.TextAnnotation, (Rect, contents), kw)
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   505
        self._addAnnotation(annotation, name, addtopage)
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   506
        
64
568049c4a12f Using trailing zero convention for new form and link API
andy_robinson
parents: 60
diff changeset
   507
    def inkAnnotation0(self, contents, InkList=None, Rect=None, addtopage=1, name=None, **kw):
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   508
        "Experimental"
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   509
        (w,h) = self._pagesize
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   510
        if not Rect:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   511
            Rect = (0,0,w,h)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   512
        if not InkList:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   513
            InkList = ( (100,100,100,h-100,w-100,h-100,w-100,100), )
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   514
        annotation = apply(pdfdoc.InkAnnotation, (Rect, contents, InkList), kw)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   515
        self.addAnnotation(annotation, name, addtopage)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   516
    
232
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   517
    def linkAbsolute(self, contents, destinationname, Rect=None, addtopage=1, name=None, **kw):
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   518
        """rectangular link annotation positioned wrt the default user space
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   519
           Rect identifies (lowerx, lowery, upperx, uppery) for lower left
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   520
           and upperright points of the rectangle.  Translations and other transforms
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   521
           are IGNORED (absolute position wrt the default user space).
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   522
           destinationname should be the name of a bookmark (which may be defined later
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   523
           but must be defined before the document is generated).
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   524
           You may want to use the keyword argument Border='[0 0 0]' to
150d4caf48c7 removed 0's on stable linkage and outline operations.
aaron_watters
parents: 228
diff changeset
   525
           suppress the visible rectangle around the link."""
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   526
        destination = self._bookmarkReference(destinationname) # permitted to be undefined... must bind later...
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   527
        (w,h) = self._pagesize
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   528
        if not Rect:
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   529
            Rect = (0,0,w,h)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   530
        kw["Rect"] = Rect
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   531
        kw["Contents"] = contents
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   532
        kw["Destination"] = destination
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   533
        annotation = apply(pdfdoc.LinkAnnotation, (), kw)
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   534
        self._addAnnotation(annotation, name, addtopage)
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   535
    
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   536
    def _addAnnotation(self, annotation, name=None, addtopage=1):
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   537
        count = self._annotationCount = self._annotationCount+1
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   538
        if not name: name="NUMBER"+repr(count)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   539
        self._doc.addAnnotation(name, annotation)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   540
        if addtopage:
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   541
            self._annotatePage(name)
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   542
            
54
9f1cadbf9728 added beginForm..endForm and fixed some naming convention issues.
aaron_watters
parents: 52
diff changeset
   543
    def _annotatePage(self, name):
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   544
        ref = self._doc.refAnnotation(name)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   545
        self._annotationrefs.append(ref)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   546
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   547
    def getPageNumber(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   548
        return self._pageNumber
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   549
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   550
    def save(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   551
        """Saves the file.  If holding data, do
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   552
        a showPage() to save them having to."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   553
        if len(self._code):  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   554
            self.showPage()
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 70
diff changeset
   555
136
9fa42a32d2f7 Completed addOutlineEntry0 api
aaron_watters
parents: 127
diff changeset
   556
        self._doc.SaveToFile(self._filename, self)
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 77
diff changeset
   557
        if self._verbosity > 0:
177
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   558
            if type(self._filename)==StringType:
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   559
                name = self._filename
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   560
            else:
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   561
                if hasattr(self._filename,'name'):
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   562
                    name = self._filename.name
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   563
                else:
179
6098115c11aa Use str(filename) not '<Unknown>'
rgbecker
parents: 177
diff changeset
   564
                    name = str(filename)
177
13d1fe68a5ce Fix verbose filename print when it's actually a file type object
rgbecker
parents: 176
diff changeset
   565
            print 'saved', name
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   566
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   567
    def setPageSize(self, size):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   568
        """accepts a 2-tuple in points for paper size for this
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   569
        and subsequent pages"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   570
        self._pagesize = size
20
7add98108a3c Canvas.setPageSize fix
rgbecker
parents: 18
diff changeset
   571
        self._make_preamble()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   572
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   573
    def addLiteral(self, s, escaped=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   574
        if escaped==0:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   575
            s = self._escape(s)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   576
        self._code.append(s)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   577
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   578
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   579
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   580
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   581
        #      coordinate transformations
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   582
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   583
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   584
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   585
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   586
    def transform(self, a,b,c,d,e,f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   587
        """How can Python track this?"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   588
        a0,b0,c0,d0,e0,f0 = self._currentMatrix
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   589
        self._currentMatrix = (a0*a+c0*b,    b0*a+d0*b,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   590
                               a0*c+c0*d,    b0*c+d0*d,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   591
                               a0*e+c0*f+e0, b0*e+d0*f+f0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   592
        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
   593
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   594
    def translate(self, dx, dy):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   595
        self.transform(1,0,0,1,dx,dy)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   596
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   597
    def scale(self, x, y):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   598
        self.transform(x,0,0,y,0,0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   599
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   600
    def rotate(self, theta):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   601
        """Canvas.rotate(theta)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   602
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   603
        theta is in degrees."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   604
        c = cos(theta * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   605
        s = sin(theta * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   606
        self.transform(c, s, -s, c, 0, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   607
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   608
    def skew(self, alpha, beta):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   609
        tanAlpha = tan(alpha * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   610
        tanBeta  = tan(beta  * pi / 180)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   611
        self.transform(1, tanAlpha, tanBeta, 1, 0, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   612
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   613
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   614
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   615
        #      graphics state management
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   616
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   617
        ######################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   618
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   619
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   620
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   621
    def saveState(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   622
        """These need expanding to save/restore Python's state tracking too"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   623
        self._code.append('q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   624
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   625
    def restoreState(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   626
        """These need expanding to save/restore Python's state tracking too"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   627
        self._code.append('Q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   628
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   629
        ###############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   630
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   631
        #   Drawing methods.  These draw things directly without
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   632
        #   fiddling around with Path objects.  We can add any geometry
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   633
        #   methods we wish as long as their meaning is precise and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   634
        #   they are of general use.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   635
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   636
        #   In general there are two patterns.  Closed shapes
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   637
        #   have the pattern shape(self, args, stroke=1, fill=0);
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   638
        #   by default they draw an outline only. Line segments come
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   639
        #   in three flavours: line, bezier, arc (which is a segment
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   640
        #   of an elliptical arc, approximated by up to four bezier
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   641
        #   curves, one for each quadrant.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   642
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   643
        #   In the case of lines, we provide a 'plural' to unroll
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   644
        #   the inner loop; it is useful for drawing big grids
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   645
        ################################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   646
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   647
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   648
        #--------first the line drawing methods-----------------------
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   649
    def line(self, x1,y1, x2,y2):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   650
        "As it says"       
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   651
        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
   652
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   653
    def lines(self, linelist):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   654
        """As line(), but slightly more efficient for lots of them -
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   655
        one stroke operation and one less function call"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   656
        self._code.append('n')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   657
        for (x1,y1,x2,y2) in linelist:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   658
            self._code.append('%0.2f %0.2f m %0.2f %0.2f l' % (x1, y1, x2, y2))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   659
        self._code.append('S')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   660
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   661
    def grid(self, xlist, ylist):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   662
        """Lays out a grid in current line style.  Suuply list of
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   663
        x an y positions."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   664
        assert len(xlist) > 1, "x coordinate list must have 2+ items"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   665
        assert len(ylist) > 1, "y coordinate list must have 2+ items"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   666
        lines = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   667
        y0, y1 = ylist[0], ylist[-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   668
        x0, x1 = xlist[0], xlist[-1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   669
        for x in xlist:
127
44912033ce1b Removed illegal append statement usage in canvas.grid()
andy_robinson
parents: 117
diff changeset
   670
            lines.append((x,y0,x,y1))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   671
        for y in ylist:
127
44912033ce1b Removed illegal append statement usage in canvas.grid()
andy_robinson
parents: 117
diff changeset
   672
            lines.append((x0,y,x1,y))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   673
        self.lines(lines)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   674
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   675
    def bezier(self, x1, y1, x2, y2, x3, y3, x4, y4):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   676
        "Bezier curve with the four given control points"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   677
        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
   678
                          (x1, y1, x2, y2, x3, y3, x4, y4)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   679
                          )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   680
    def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   681
        """Contributed to piddlePDF by Robert Kern, 28/7/99.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   682
        Trimmed down by AR to remove color stuff for pdfgen.canvas and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   683
        revert to positive coordinates.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   684
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   685
        Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   686
        starting at startAng degrees and covering extent degrees.   Angles
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   687
        start with 0 to the right (+x) and increase counter-clockwise.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   688
        These should have x1<x2 and y1<y2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   689
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   690
        The algorithm is an elliptical generalization of the formulae in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   691
        Jim Fitzsimmon's TeX tutorial <URL: http://www.tinaja.com/bezarc1.pdf>."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   692
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   693
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   694
        #move to first point
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   695
        self._code.append('n %0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   696
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   697
            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
   698
        # stroke
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   699
        self._code.append('S')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   700
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   701
        #--------now the shape drawing methods-----------------------
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   702
    def rect(self, x, y, width, height, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   703
        "draws a rectangle"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   704
        self._code.append('n %0.2f %0.2f %0.2f %0.2f re ' % (x, y, width, height)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   705
                          + PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   706
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   707
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   708
    def ellipse(self, x1, y1, x2, y2, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   709
        """Uses bezierArc, which conveniently handles 360 degrees -
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   710
        nice touch Robert"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   711
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, 0, 360)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   712
        #move to first point
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   713
        self._code.append('n %0.2f %0.2f m' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   714
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   715
            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
   716
        #finish
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   717
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   718
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   719
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   720
    def wedge(self, x1,y1, x2,y2, startAng, extent, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   721
        """Like arc, but connects to the centre of the ellipse.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   722
        Most useful for pie charts and PacMan!"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   723
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   724
        x_cen  = (x1+x2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   725
        y_cen  = (y1+y2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   726
        pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   727
  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   728
        self._code.append('n %0.2f %0.2f m' % (x_cen, y_cen))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   729
        # Move the pen to the center of the rectangle
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   730
        self._code.append('%0.2f %0.2f l' % pointList[0][:2])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   731
        for curve in pointList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   732
            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
   733
        # finish the wedge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   734
        self._code.append('%0.2f %0.2f l ' % (x_cen, y_cen))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   735
        # final operator
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   736
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   737
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   738
    def circle(self, x_cen, y_cen, r, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   739
        """special case of ellipse"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   740
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   741
        x1 = x_cen - r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   742
        x2 = x_cen + r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   743
        y1 = y_cen - r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   744
        y2 = y_cen + r
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   745
        self.ellipse(x1, y1, x2, y2, stroke, fill)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   746
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   747
    def roundRect(self, x, y, width, height, radius, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   748
        """Draws a rectangle with rounded corners.  The corners are
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   749
        approximately quadrants of a circle, with the given radius."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   750
        #use a precomputed set of factors for the bezier approximation
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   751
        #to a circle. There are six relevant points on the x axis and y axis.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   752
        #sketch them and it should all make sense!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   753
        t = 0.4472 * radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   754
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   755
        x0 = x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   756
        x1 = x0 + t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   757
        x2 = x0 + radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   758
        x3 = x0 + width - radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   759
        x4 = x0 + width - t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   760
        x5 = x0 + width
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   761
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   762
        y0 = y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   763
        y1 = y0 + t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   764
        y2 = y0 + radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   765
        y3 = y0 + height - radius
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   766
        y4 = y0 + height - t
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   767
        y5 = y0 + height
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   768
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   769
        self._code.append('n %0.2f %0.2f m' % (x2, y0))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   770
        self._code.append('%0.2f %0.2f l' % (x3, y0))  # bottom row
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   771
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   772
                         % (x4, y0, x5, y1, x5, y2)) # bottom right
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   773
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   774
        self._code.append('%0.2f %0.2f l' % (x5, y3))  # right edge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   775
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   776
                         % (x5, y4, x4, y5, x3, y5)) # top right
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   777
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   778
        self._code.append('%0.2f %0.2f l' % (x2, y5))  # top row
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   779
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   780
                         % (x1, y5, x0, y4, x0, y3)) # top left
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   781
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   782
        self._code.append('%0.2f %0.2f l' % (x0, y2))  # left edge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   783
        self._code.append('%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   784
                         % (x0, y1, x1, y0, x2, y0)) # bottom left
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   785
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   786
        self._code.append('h')  #close off, although it should be where it started anyway
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   787
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   788
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   789
        self._code.append(PATH_OPS[stroke, fill, self._fillMode])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   790
        ##################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   791
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   792
        #  Text methods
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   793
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   794
        # As with graphics, a separate object ensures that
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   795
        # everything is bracketed between  text operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   796
        # The methods below are a high-level convenience.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   797
        # use PDFTextObject for multi-line text.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   798
        ##################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   799
52
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   800
 
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   801
    def setFillColorCMYK(self, c, m, y, k):
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   802
         """Takes 4 arguments between 0.0 and 1.0"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   803
         self._fillColorCMYK = (c, m, y, k)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   804
         self._code.append('%0.2f %0.2f %0.2f %0.2f k' % (c, m, y, k))
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   805
         
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   806
    def setStrokeColorCMYK(self, c, m, y, k):
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   807
         """Takes 4 arguments between 0.0 and 1.0"""
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   808
         self._strokeColorCMYK = (c, m, y, k)
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   809
         self._code.append('%0.2f %0.2f %0.2f %0.2f K' % (c, m, y, k))
3bbe0067a2dc added support for destinations, forms, linkages
aaron_watters
parents: 48
diff changeset
   810
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   811
    def drawString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   812
        """Draws a string in the current text styles."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   813
        #we could inline this for speed if needed
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   814
        t = self.beginText(x, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   815
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   816
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   817
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   818
    def drawRightString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   819
        """Draws a string right-aligned with the y coordinate"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   820
        width = self.stringWidth(text, self._fontname, self._fontsize)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   821
        t = self.beginText(x - width, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   822
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   823
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   824
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   825
    def drawCentredString(self, x, y, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   826
        """Draws a string right-aligned with the y coordinate.  I
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   827
        am British so the spelling is correct, OK?"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   828
        width = self.stringWidth(text, self._fontname, self._fontsize)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   829
        t = self.beginText(x - 0.5*width, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   830
        t.textLine(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   831
        self.drawText(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   832
  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   833
    def getAvailableFonts(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   834
        """Returns the list of PostScript font names available.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   835
        Standard set now, but may grow in future with font embedding."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   836
        fontnames = self._doc.getAvailableFonts()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   837
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   838
        return fontnames
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   839
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   840
    def setFont(self, psfontname, size, leading = None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   841
        """Sets the font.  If leading not specified, defaults to 1.2 x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   842
        font size. Raises a readable exception if an illegal font
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   843
        is supplied.  Font names are case-sensitive! Keeps track
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   844
        of font anme and size for metrics."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   845
        self._fontname = psfontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   846
        self._fontsize = size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   847
        pdffontname = self._doc.getInternalFontName(psfontname)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   848
        if leading is None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   849
            leading = size * 1.2
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   850
        self._leading = leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   851
        self._code.append('BT %s %0.1f Tf %0.1f TL ET' % (pdffontname, size, leading))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   852
117
fd3682228853 XML Tagged Paragraph parser changes
rgbecker
parents: 100
diff changeset
   853
    def stringWidth(self, text, fontName, fontSize):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   854
        "gets width of a string in the given font and size"
117
fd3682228853 XML Tagged Paragraph parser changes
rgbecker
parents: 100
diff changeset
   855
        return pdfmetrics.stringWidth(text, fontName, fontSize)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   856
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   857
    # basic graphics modes
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   858
    def setLineWidth(self, width):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   859
        self._lineWidth = width
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   860
        self._code.append('%0.2f w' % width)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   861
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   862
    def setLineCap(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   863
        """0=butt,1=round,2=square"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   864
        assert mode in (0,1,2), "Line caps allowed: 0=butt,1=round,2=square"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   865
        self._lineCap = mode
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   866
        self._code.append('%d J' % mode)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   867
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   868
    def setLineJoin(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   869
        """0=mitre, 1=round, 2=bevel"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   870
        assert mode in (0,1,2), "Line Joins allowed: 0=mitre, 1=round, 2=bevel"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   871
        self._lineJoin = mode
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   872
        self._code.append('%d j' % mode)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   873
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   874
    def setMiterLimit(self, limit):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   875
        self._miterLimit = limit
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   876
        self._code.append('%0.2f M' % limit)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   877
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   878
    def setDash(self, array=[], phase=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   879
        """Two notations.  pass two numbers, or an array and phase"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   880
        if type(array) == IntType or type(array) == FloatType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   881
            self._code.append('[%s %s] 0 d' % (array, phase))
45
bfe68ff64924 fixed typo in setDash
andy_robinson
parents: 43
diff changeset
   882
        elif type(array) == ListType or type(array) == TupleType:
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   883
            assert phase <= len(array), "setDash phase must be l.t.e. length of array"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   884
            textarray = string.join(map(str, array))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   885
            self._code.append('[%s] %s d' % (textarray, phase))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   886
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   887
    def setFillColorRGB(self, r, g, b):
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   888
        """Takes 3 arguments between 0.0 and 1.0"""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   889
        self._fillColorRGB = (r, g, b)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   890
        self._code.append('%0.2f %0.2f %0.2f rg' % (r,g,b))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   891
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   892
    def setStrokeColorRGB(self, r, g, b):
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   893
        """Takes 3 arguments between 0.0 and 1.0"""
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   894
        self._strokeColorRGB = (r, g, b)
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   895
        self._code.append('%0.2f %0.2f %0.2f RG' % (r,g,b))
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   896
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   897
    def setFillColor(self, aColor):
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   898
        """Takes a color object, allowing colors to be referred to by name"""
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   899
        if type(aColor) == ColorType:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   900
            rgb = (aColor.red, aColor.green, aColor.blue)
70
337c859f4de5 Fixes to setFill/StrokeColor
rgbecker
parents: 69
diff changeset
   901
            self._fillColorRGB = rgb
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   902
            self._code.append('%0.2f %0.2f %0.2f rg' % rgb )
69
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
   903
        elif type(aColor) in _SeqTypes:
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   904
            l = len(aColor)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   905
            if l==3:
70
337c859f4de5 Fixes to setFill/StrokeColor
rgbecker
parents: 69
diff changeset
   906
                self._fillColorRGB = aColor
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   907
                self._code.append('%0.2f %0.2f %0.2f rg' % aColor )
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   908
            elif l==4:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   909
                self.setFillColorCMYK(self, aColor[0], aColor[1], aColor[2], aColor[3])
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   910
            else:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   911
                raise 'Unknown color', str(aColor)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   912
        else:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   913
            raise 'Unknown color', str(aColor)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   914
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   915
  
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   916
    def setStrokeColor(self, aColor):
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 36
diff changeset
   917
        """Takes a color object, allowing colors to be referred to by name"""
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   918
        if type(aColor) == ColorType:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   919
            rgb = (aColor.red, aColor.green, aColor.blue)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   920
            self._strokeColorRGB = rgb
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   921
            self._code.append('%0.2f %0.2f %0.2f RG' % rgb )
69
8cf6b4b369d5 Added _SeqTypes for efficiency
rgbecker
parents: 68
diff changeset
   922
        elif type(aColor) in _SeqTypes:
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   923
            l = len(aColor)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   924
            if l==3:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   925
                self._strokeColorRGB = aColor
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   926
                self._code.append('%0.2f %0.2f %0.2f RG' % aColor )
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   927
            elif l==4:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   928
                self.setStrokeColorCMYK(self, aColor[0], aColor[1], aColor[2], aColor[3])
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   929
            else:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   930
                raise 'Unknown color', str(aColor)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   931
        else:
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
   932
            raise 'Unknown color', str(aColor)
27
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   933
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   934
    def setFillGray(self, gray):
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   935
        """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
   936
        self._fillColorRGB = (gray, gray, gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   937
        self._code.append('%0.2f g' % gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   938
        
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   939
    def setStrokeGray(self, gray):
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   940
        """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
   941
        self._strokeColorRGB = (gray, gray, gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   942
        self._code.append('%0.2f G' % gray)
684657a34422 Added methods setFillGray(g), setStrokeGray(g) where 0 <= g <= 1
andy_robinson
parents: 26
diff changeset
   943
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   944
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   945
    # path stuff - the separate path object builds it    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   946
    def beginPath(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   947
        """Returns a fresh path object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   948
        return pathobject.PDFPathObject()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   949
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   950
    def drawPath(self, aPath, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   951
        "Draw in the mode indicated"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   952
        op = PATH_OPS[stroke, fill, self._fillMode]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   953
        self._code.append(aPath.getCode() + ' ' + op)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   954
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   955
    def clipPath(self, aPath, stroke=1, fill=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   956
        "clip as well as drawing"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   957
        op = PATH_OPS[stroke, fill, self._fillMode]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   958
        self._code.append(aPath.getCode() + ' W ' + op)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   959
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   960
    def beginText(self, x=0, y=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   961
        """Returns a fresh text object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   962
        return textobject.PDFTextObject(self, x, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   963
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   964
    def drawText(self, aTextObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   965
        """Draws a text object"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   966
        self._code.append(aTextObject.getCode())
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   967
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   968
        ######################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   969
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   970
        #   Image routines
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   971
        #
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   972
        ######################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   973
    def drawInlineImage(self, image, x,y, width=None,height=None):
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   974
        """Draw an Image into the specified rectangle.  If width and
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   975
        height are omitted, they are calculated from the image size.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   976
        Also allow file names as well as images.  This allows a
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   977
        caching mechanism"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   978
            
36
0ca883f42d46 Typo self._currentPageHasImages = 1
rgbecker
parents: 33
diff changeset
   979
        self._currentPageHasImages = 1
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   980
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   981
        if type(image) == StringType:
228
d3aaff565043 Resynchronization
andy_robinson
parents: 181
diff changeset
   982
            if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   983
                #directly process JPEG files
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
   984
                #open file, needs some error handling!!
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   985
                imageFile = open(image, 'rb')
228
d3aaff565043 Resynchronization
andy_robinson
parents: 181
diff changeset
   986
                info = pdfutils.readJPEGInfo(imageFile)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   987
                imgwidth, imgheight = info[0], info[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   988
                if info[2] == 1:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   989
                    colorSpace = 'DeviceGray'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   990
                elif info[2] == 3:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   991
                    colorSpace = 'DeviceRGB'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   992
                else: #maybe should generate an error, is this right for CMYK?
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   993
                    colorSpace = 'DeviceCMYK'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   994
                imageFile.seek(0)		#reset file pointer
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   995
                imagedata = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   996
                imagedata.append('BI')   # begin image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   997
                # this describes what is in the image itself
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   998
                imagedata.append('/Width %0.2f /Height %0.2f' %(info[0], info[1]))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   999
                imagedata.append('/BitsPerComponent 8')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1000
                imagedata.append('/ColorSpace /%s' % colorSpace)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1001
                imagedata.append('/Filter [ /ASCII85Decode /DCTDecode]')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1002
                imagedata.append('ID')   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1003
                #write in blocks of (??) 60 characters per line to a list
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1004
                compressed = imageFile.read()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1005
                encoded = pdfutils._AsciiBase85Encode(compressed)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1006
                outstream = cStringIO.StringIO(encoded)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1007
                dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1008
                while dataline <> "":
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1009
                    imagedata.append(dataline)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1010
                    dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1011
                imagedata.append('EI')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1012
            else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1013
                if not pdfutils.cachedImageExists(image):
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1014
                    if not zlib:
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1015
                        print 'zlib not available'
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1016
                        return
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1017
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1018
                    try:
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1019
                        import Image
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1020
                    except ImportError:
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1021
                        print 'Python Imaging Library not available'
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1022
                        return
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1023
                    pdfutils.cacheImageFile(image)
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1024
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1025
                #now we have one cached, slurp it in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1026
                cachedname = os.path.splitext(image)[0] + '.a85'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1027
                imagedata = open(cachedname,'rb').readlines()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1028
                #trim off newlines...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1029
                imagedata = map(string.strip, imagedata)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1030
                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1031
                #parse line two for width, height
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1032
                words = string.split(imagedata[1])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1033
                imgwidth = string.atoi(words[1])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1034
                imgheight = string.atoi(words[3])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1035
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1036
            #PIL Image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1037
            #work out all dimensions
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1038
            if not zlib:
33
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1039
                print 'zlib not available'
bef84d99fbd8 Remove over officious import checks Imag/zlib
rgbecker
parents: 28
diff changeset
  1040
                return
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1041
            myimage = image.convert('RGB')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1042
            imgwidth, imgheight = myimage.size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1043
            imagedata = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1044
            imagedata.append('BI')   # begin image
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1045
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1046
            # this describes what is in the image itself
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1047
            imagedata.append('/W %0.2f /H %0.2f /BPC 8 /CS /RGB /F [/A85 /Fl]' % (imgwidth, imgheight))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1048
            imagedata.append('ID')   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1049
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1050
            #use a flate filter and Ascii Base 85 to compress
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1051
            raw = myimage.tostring()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1052
            assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1053
            compressed = zlib.compress(raw)   #this bit is very fast...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1054
            encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1055
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1056
            #write in blocks of (??) 60 characters per line to a list
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1057
            outstream = cStringIO.StringIO(encoded)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1058
            dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1059
            while dataline <> "":
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1060
                imagedata.append(dataline)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1061
                dataline = outstream.read(60)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1062
            imagedata.append('EI')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1063
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1064
        #now build the PDF for the image.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1065
        if not width:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1066
            width = imgwidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1067
        if not height:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1068
            height = imgheight
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1069
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1070
        # this says where and how big to draw it
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1071
        #self._code.append('ET')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1072
        #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
  1073
        if self.bottomup:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1074
            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
  1075
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1076
            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
  1077
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
  1078
        # self._code.extend(imagedata) if >=python-1.5.2
28
b62ec46b2759 [].extend illegal in 1.5.1
rgbecker
parents: 27
diff changeset
  1079
        for line in imagedata:
68
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
  1080
            self._code.append(line)
465aa7f56976 Made setFill/StrokeColor polymorphic
rgbecker
parents: 64
diff changeset
  1081
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1082
        self._code.append('Q')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1083
        #self._code.append('BT')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1084
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1085
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1086
    def setPageCompression(self, onoff=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1087
        """Possible values 1 or 0 (1 for 'on' is the default).
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1088
        If on, the page data will be compressed, leading to much
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1089
        smaller files, but takes a little longer to create the files.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1090
        This applies to all subsequent pages, or until setPageCompression()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1091
        is next called."""
176
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1092
        if onoff and not zlib:
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1093
            print 'zlib not available'
737dd0cc1d16 Changed zlib error handling
rgbecker
parents: 166
diff changeset
  1094
            return
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1095
        self._pageCompression = onoff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1096
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1097
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1098
    def setPageTransition(self, effectname=None, duration=1, 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1099
                        direction=0,dimension='H',motion='I'):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1100
        """PDF allows page transition effects for use when giving
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1101
        presentations.  There are six possible effects.  You can
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1102
        just guive the effect name, or supply more advanced options
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1103
        to refine the way it works.  There are three types of extra
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1104
        argument permitted, and here are the allowed values:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1105
            direction_arg = [0,90,180,270]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1106
            dimension_arg = ['H', 'V']
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1107
            motion_arg = ['I','O'] (start at inside or outside)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1108
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1109
        This table says which ones take which arguments:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1110
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1111
        PageTransitionEffects = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1112
            'Split': [direction_arg, motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1113
            'Blinds': [dimension_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1114
            'Box': [motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1115
            'Wipe' : [direction_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1116
            'Dissolve' : [],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1117
            'Glitter':[direction_arg]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1118
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1119
        Have fun!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1120
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1121
        if not effectname:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1122
            self._pageTransitionString = ''
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1123
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1124
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1125
        #first check each optional argument has an allowed value
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1126
        if direction in [0,90,180,270]:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1127
            direction_arg = '/Di /%d' % direction
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1128
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1129
            raise 'PDFError', ' directions allowed are 0,90,180,270'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1130
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1131
        if dimension in ['H', 'V']:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1132
            dimension_arg = '/Dm /%s' % dimension
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1133
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1134
            raise'PDFError','dimension values allowed are H and V'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1135
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1136
        if motion in ['I','O']:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1137
            motion_arg = '/M /%s' % motion
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1138
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1139
            raise'PDFError','motion values allowed are I and O'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1140
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1141
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1142
        # this says which effects require which argument types from above
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1143
        PageTransitionEffects = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1144
            'Split': [direction_arg, motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1145
            'Blinds': [dimension_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1146
            'Box': [motion_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1147
            'Wipe' : [direction_arg],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1148
            'Dissolve' : [],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1149
            'Glitter':[direction_arg]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1150
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1151
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1152
        try:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1153
            args = PageTransitionEffects[effectname]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1154
        except KeyError:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1155
            raise 'PDFError', 'Unknown Effect Name "%s"' % effectname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1156
            self._pageTransitionString = ''
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1157
            return
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1158
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1159
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1160
        self._pageTransitionString = (('/Trans <</D %d /S /%s ' % (duration, effectname)) + 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1161
            string.join(args, ' ') + ' >>')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1162
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1163
if __name__ == '__main__':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1164
    print 'For test scripts, run testpdfgen.py'