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