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