reportlab/pdfbase/pdfdoc.py
author aaron_watters
Wed, 18 Oct 2000 05:03:21 +0000
changeset 481 136669babedf
parent 479 e37604e2fcf9
child 482 15d1ca6de00f
permissions -rwxr-xr-x
complete revision of pdfdoc. Not finished (compression missing, testing needed) I got Robin's last change in at the last moment :)
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: pdfdoc.py,v $
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    34
#	Revision 1.26  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
    35
#	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
    36
#	I got Robin's last change in at the last moment :)
479
e37604e2fcf9 Added showFullScreen0
andy_robinson
parents: 458
diff changeset
    37
#
458
5208a7e86d29 Paul Eddington's unix tell() returns a LongIntType bugfix
rgbecker
parents: 440
diff changeset
    38
#	Revision 1.24  2000/09/08 10:04:08  rgbecker
5208a7e86d29 Paul Eddington's unix tell() returns a LongIntType bugfix
rgbecker
parents: 440
diff changeset
    39
#	Paul Eddington's unix tell() returns a LongIntType bugfix
479
e37604e2fcf9 Added showFullScreen0
andy_robinson
parents: 458
diff changeset
    40
#	
440
dc4af79eda1c change to PDFLiteral to support "lazy string conversions" (to support lazy crosslinks)
aaron_watters
parents: 428
diff changeset
    41
#	Revision 1.23  2000/08/24 02:26:04  aaron_watters
dc4af79eda1c change to PDFLiteral to support "lazy string conversions" (to support lazy crosslinks)
aaron_watters
parents: 428
diff changeset
    42
#	change to PDFLiteral to support "lazy string conversions" (to support lazy crosslinks)
458
5208a7e86d29 Paul Eddington's unix tell() returns a LongIntType bugfix
rgbecker
parents: 440
diff changeset
    43
#	
428
6d26174d043e Andy's Symbol/Zapf font fix
rgbecker
parents: 305
diff changeset
    44
#	Revision 1.22  2000/08/09 10:57:52  rgbecker
6d26174d043e Andy's Symbol/Zapf font fix
rgbecker
parents: 305
diff changeset
    45
#	Andy's Symbol/Zapf font fix
440
dc4af79eda1c change to PDFLiteral to support "lazy string conversions" (to support lazy crosslinks)
aaron_watters
parents: 428
diff changeset
    46
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 303
diff changeset
    47
#	Revision 1.21  2000/06/26 15:58:22  rgbecker
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 303
diff changeset
    48
#	Simple fix to widths problem
428
6d26174d043e Andy's Symbol/Zapf font fix
rgbecker
parents: 305
diff changeset
    49
#	
303
b989a9f0de7f /Producer (ReportLab http://www.reportlab.com) in document
aaron_watters
parents: 252
diff changeset
    50
#	Revision 1.20  2000/06/23 17:51:22  aaron_watters
b989a9f0de7f /Producer (ReportLab http://www.reportlab.com) in document
aaron_watters
parents: 252
diff changeset
    51
#	/Producer (ReportLab http://www.reportlab.com) in document
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 303
diff changeset
    52
#	
252
04d4a765d934 SaveToFile: only close the file if we opened it.
rgbecker
parents: 181
diff changeset
    53
#	Revision 1.19  2000/06/01 09:44:26  rgbecker
04d4a765d934 SaveToFile: only close the file if we opened it.
rgbecker
parents: 181
diff changeset
    54
#	SaveToFile: only close the file if we opened it.
04d4a765d934 SaveToFile: only close the file if we opened it.
rgbecker
parents: 181
diff changeset
    55
#	Aggregated from types imports to module level.
303
b989a9f0de7f /Producer (ReportLab http://www.reportlab.com) in document
aaron_watters
parents: 252
diff changeset
    56
#	
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
    57
#	Revision 1.18  2000/04/28 17:33:44  andy_robinson
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
    58
#	Added font encoding support and changed default encoding to WinAnsi
252
04d4a765d934 SaveToFile: only close the file if we opened it.
rgbecker
parents: 181
diff changeset
    59
#	
175
be80e638537b Fix typo in SaveToFile
rgbecker
parents: 174
diff changeset
    60
#	Revision 1.17  2000/04/28 09:08:42  rgbecker
be80e638537b Fix typo in SaveToFile
rgbecker
parents: 174
diff changeset
    61
#	Fix typo in SaveToFile
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
    62
#	
174
33c5cdce9777 Dinu's SaveFile patch
rgbecker
parents: 165
diff changeset
    63
#	Revision 1.16  2000/04/27 18:11:56  rgbecker
33c5cdce9777 Dinu's SaveFile patch
rgbecker
parents: 165
diff changeset
    64
#	Dinu's SaveFile patch
175
be80e638537b Fix typo in SaveToFile
rgbecker
parents: 174
diff changeset
    65
#	
165
30eb8a62ee95 added support for closed outline entries
aaron_watters
parents: 138
diff changeset
    66
#	Revision 1.15  2000/04/25 20:19:07  aaron_watters
30eb8a62ee95 added support for closed outline entries
aaron_watters
parents: 138
diff changeset
    67
#	added support for closed outline entries
174
33c5cdce9777 Dinu's SaveFile patch
rgbecker
parents: 165
diff changeset
    68
#	
138
663357be1296 Minor support for inPage/inForm api elimination in canvas
aaron_watters
parents: 137
diff changeset
    69
#	Revision 1.14  2000/04/18 19:50:30  aaron_watters
663357be1296 Minor support for inPage/inForm api elimination in canvas
aaron_watters
parents: 137
diff changeset
    70
#	Minor support for inPage/inForm api elimination in canvas
165
30eb8a62ee95 added support for closed outline entries
aaron_watters
parents: 138
diff changeset
    71
#	
137
44f0be42ef67 added support for addOutlineEntry0 api
aaron_watters
parents: 77
diff changeset
    72
#	Revision 1.13  2000/04/15 15:00:09  aaron_watters
44f0be42ef67 added support for addOutlineEntry0 api
aaron_watters
parents: 77
diff changeset
    73
#	added support for addOutlineEntry0 api
138
663357be1296 Minor support for inPage/inForm api elimination in canvas
aaron_watters
parents: 137
diff changeset
    74
#	
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 60
diff changeset
    75
#	Revision 1.12  2000/04/06 09:52:02  andy_robinson
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 60
diff changeset
    76
#	Removed some old comments; tweaks to experimental Outline methods.
137
44f0be42ef67 added support for addOutlineEntry0 api
aaron_watters
parents: 77
diff changeset
    77
#	
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 53
diff changeset
    78
#	Revision 1.11  2000/04/02 02:52:39  aaron_watters
5b97da0315d3 added support for outline trees
aaron_watters
parents: 53
diff changeset
    79
#	added support for outline trees
77
a5d452cb7a5d Removed some old comments; tweaks to experimental Outline methods.
andy_robinson
parents: 60
diff changeset
    80
#	
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
    81
#	Revision 1.10  2000/03/24 21:03:51  aaron_watters
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
    82
#	Added forms, destinations, linkages and other features
60
5b97da0315d3 added support for outline trees
aaron_watters
parents: 53
diff changeset
    83
#	
25
19e8698c4a76 Memory leak fixes
rgbecker
parents: 17
diff changeset
    84
#	Revision 1.7  2000/02/23 15:09:23  rgbecker
19e8698c4a76 Memory leak fixes
rgbecker
parents: 17
diff changeset
    85
#	Memory leak fixes
46
d8054ad2fc24 fixed off by one error in obj ref accounting, made font dict external object
rgbecker
parents: 25
diff changeset
    86
#	
17
9d443a3be1f9 added _HAVE_ZLIB to stop compression being set without zlib
rgbecker
parents: 16
diff changeset
    87
#	Revision 1.6  2000/02/17 12:36:25  rgbecker
9d443a3be1f9 added _HAVE_ZLIB to stop compression being set without zlib
rgbecker
parents: 16
diff changeset
    88
#	added _HAVE_ZLIB to stop compression being set without zlib
25
19e8698c4a76 Memory leak fixes
rgbecker
parents: 17
diff changeset
    89
#	
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    90
#	Revision 1.5  2000/02/17 02:07:23  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    91
#	Docstring & other fixes
17
9d443a3be1f9 added _HAVE_ZLIB to stop compression being set without zlib
rgbecker
parents: 16
diff changeset
    92
#	
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    93
#	Revision 1.4  2000/02/16 09:42:50  rgbecker
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    94
#	Conversion to reportlab package
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
    95
#	
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    96
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    97
#	License text fixes
10
0cb88428a663 Conversion to reportlab package
rgbecker
parents: 7
diff changeset
    98
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    99
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   100
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
   101
#	
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   102
__version__=''' $Id: pdfdoc.py,v 1.26 2000/10/18 05:03:21 aaron_watters Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 10
diff changeset
   103
__doc__=""" 
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   104
PDFgen is a library to generate PDF files containing text and graphics.  It is the 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   105
foundation for a complete reporting solution in Python.  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   106
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   107
The module pdfdoc.py handles the 'outer structure' of PDF documents, ensuring that
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   108
all objects are properly cross-referenced and indexed to the nearest byte.  The 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   109
'inner structure' - the page descriptions - are presumed to be generated before 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   110
each page is saved.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   111
pdfgen.py calls this and provides a 'canvas' object to handle page marking operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   112
piddlePDF calls pdfgen and offers a high-level interface.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   114
2000-10-13 gmcm Packagize
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   115
"""
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   116
"""extremely anally  retentive structured version of pdfdoc"""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   117
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
   118
DEFAULT_ENCODING = 'WinAnsiEncoding' #hack here for a system wide change
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
   119
ALLOWED_ENCODINGS = ('WinAnsiEncoding', 'MacRomanEncoding')
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   120
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   121
PDFError = 'PDFError'
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   122
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   123
StandardEnglishFonts = [
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   124
    'Courier', 'Courier-Bold', 'Courier-Oblique', 'Courier-BoldOblique',  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   125
    'Helvetica', 'Helvetica-Bold', 'Helvetica-Oblique', 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   126
    'Helvetica-BoldOblique',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   127
    'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   128
    'Symbol','ZapfDingbats']
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   129
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   130
# set this flag to get more vertical whitespace (and larger files)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   131
LongFormat = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   132
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   133
# XXXX stream filters need to be added
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   134
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   135
# __InternalName__ is a special attribute that can only be set by the Document arbitrator
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   136
__InternalName__ = "__InternalName__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   137
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   138
# __RefOnly__ marks reference only elements that must be formatted on top level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   139
__RefOnly__ = "__RefOnly__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   140
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   141
# __Comment__ provides a (one line) comment to inline with an object ref, if present
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   142
#   if it is more than one line then percentize it...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   143
__Comment__ = "__Comment__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   144
DoComments = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   145
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   146
# name for standard font dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   147
BasicFonts = "BasicFonts"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   148
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   149
# name for the pages object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   150
Pages = "Pages"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   151
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   152
### generic utilities
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   153
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   154
import string, types
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   155
from reportlab.pdfbase import pdfutils
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   156
from reportlab.pdfbase.pdfutils import LINEEND   # this constant needed in both
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   157
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   158
# for % substitutions
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   159
LINEENDDICT = {"LINEEND": LINEEND, "PERCENT": "%"}
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   160
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   161
def markfilename(filename):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   162
	# with the Mac, we need to tag the file in a special
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   163
	#way so the system knows it is a PDF file.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   164
	#This supplied by Joe Strout
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   165
	import os
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   166
	if os.name == 'mac':
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   167
		import macfs
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   168
		try: 
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   169
			macfs.FSSpec(filename).SetCreatorType('CARO','PDF ')
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   170
		except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   171
			pass
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   172
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   173
def format(element, document, toplevel=0):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   174
    """Indirection step for formatting.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   175
       Ensures that document parameters alter behaviour
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   176
       of formatting for all elements.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   177
    """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   178
    from types import InstanceType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   179
    if type(element) is InstanceType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   180
        if not toplevel and hasattr(element, __RefOnly__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   181
            # the object cannot be a component at non top level.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   182
            # make a reference to it and return it's format
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   183
            R = document.Reference(element)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   184
            return R.format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   185
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   186
            try:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   187
                fmt = element.format
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   188
            except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   189
                raise AttributeError, "%s has no format operation" % element
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   190
            f = fmt(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   191
            if DoComments and hasattr(element, __Comment__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   192
                f = "%s%s%s%s" % ("% ", element.__Comment__, LINEEND, f)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   193
            return f
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   194
    else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   195
        return str(element)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   196
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   197
def indent(s, IND=LINEEND+" "):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   198
    return string.replace(s, LINEEND, IND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   199
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   200
### the global document structure manager
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   201
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
class PDFDocument:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   203
    objectcounter = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   204
    inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   205
    # set this to define filters 
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   206
    defaultStreamFilters = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   207
    pageCounter = 1
181
62d7a443cbc4 Added font encoding support and changed default encoding to WinAnsi
andy_robinson
parents: 175
diff changeset
   208
    def __init__(self, encoding=DEFAULT_ENCODING):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   209
        self.encoding = encoding
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   210
        # mapping of internal identifier ("Page001") to PDF objectnumber and generation number (34, 0)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   211
        self.idToObjectNumberAndVersion = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   212
        # mapping of internal identifier ("Page001") to PDF object (PDFPage instance)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   213
        self.idToObject = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   214
        # internal id to file location
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   215
        self.idToOffset = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   216
        # number to id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   217
        self.numberToId = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   218
        cat = self.Catalog = self._catalog = PDFCatalog()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   219
        pages = self.Pages = PDFPages()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   220
        cat.Pages = pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   221
        outlines = self.Outlines = self.outline = PDFOutlines()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   222
        cat.Outlines = outlines
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   223
        self.info = self.Info = PDFInfo()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   224
        self.Reference(self.Catalog)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   225
        self.Reference(self.Info)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   226
        # make std fonts (this could be made optional
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   227
        self.fontMapping = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   228
        MakeStandardEnglishFontObjects(self, encoding)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   229
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   230
    def SaveToFile(self, filename, canvas):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   231
        from types import StringType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   232
        # prepare outline
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   233
        outline = self.outline
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   234
        outline.prepare(self, canvas)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   235
        if type(filename) is StringType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   236
            myfile = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   237
            f = open(filename, "wb")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   238
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   239
            myfile = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   240
            f = filename # IT BETTER BE A FILE-LIKE OBJECT!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   241
        txt = self.format()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   242
        f.write(txt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   243
        if myfile:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   244
            f.close()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   245
            markfilename(filename) # do platform specific file junk
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
        
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   247
    def inPage(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   248
        """specify the current object as a page (enables reference binding and other page features)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   249
        if self.inObject is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   250
            if self.inObject=="page": return
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   251
            raise ValueError, "can't go in page already in object %s" % self.inObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   252
        self.inObject = "page"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   253
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   254
    def inForm(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   255
        """specify that we are in a form xobject (disable page features, etc)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   256
        if self.inObject not in ["form", None]:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   257
            raise ValueError, "can't go in form already in object %s" % self.inObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   258
        self.inObject = "form"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   259
        # don't need to do anything else, I think...        
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   260
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   261
    def getInternalFontName(self, psfontname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   262
        fm = self.fontMapping
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   263
        if fm.has_key(psfontname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   264
            return fm[psfontname]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   265
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   266
            raise PDFError, "Font %s not available in document" % repr(psfontname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   267
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   268
    def thisPageName(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   269
        return "Page"+repr(self.pageCounter)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   270
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   271
    def thisPageRef(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   272
        return PDFObjectReference(self.thisPageName())
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   273
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   274
    def addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   275
        name = self.thisPageName()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   276
        self.Reference(page, name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   277
        self.Pages.addPage(page)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   278
        self.pageCounter = self.pageCounter+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   279
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   280
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   281
    def formName(self, externalname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   282
        return "FormXob.%s" % externalname
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   283
    
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   284
    def addForm(self, name, form):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   285
        """add a Form XObject."""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   286
        # XXX should check that name is a legal PDF name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   287
        if self.inObject != "form":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   288
            self.inForm()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   289
        self.Reference(form, self.formName(name))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   290
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   291
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   292
    def annotationName(self, externalname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   293
        return "Annot.%s"%externalname
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   294
    
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   295
    def addAnnotation(self, name, annotation):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   296
        self.Reference(annotation, self.annotationName(name))
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
   297
        
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   298
    def refAnnotation(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   299
        internalname = self.annotationName(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   300
        return PDFObjectReference(internalname)
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
   301
        
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   302
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   303
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
        self.info.title = title
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   305
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   306
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   307
        "embedded in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   308
        self.info.author = author
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   309
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   310
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
        self.info.subject = subject
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
    def getAvailableFonts(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   315
        fontnames = self.fontMapping.keys()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   316
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   317
        return fontnames
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   318
    
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   319
    def format(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   320
        # register the Catalog/INfo and then format the objects one by one until exhausted
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   321
        # (possible infinite loop if there is a bug that continually makes new objects/refs...)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   322
        cat = self.Catalog
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   323
        info = self.Info
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   324
        self.Reference(self.Catalog)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   325
        self.Reference(self.Info)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   326
        # make std fonts (this could be made optional
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   327
        counter = 0 # start at first object (object 1 after preincrement)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   328
        ids = [] # the collection of object ids in object number order
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   329
        numbertoid = self.numberToId
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   330
        idToNV = self.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   331
        idToOb = self.idToObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   332
        idToOf = self.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   333
        ### note that new entries may be "appended" DURING FORMATTING
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   334
        done = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   335
        File = PDFFile() # output collector
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   336
        while done is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   337
            counter = counter+1 # do next object...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   338
            if numbertoid.has_key(counter):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   339
                id = numbertoid[counter]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   340
                #printidToOb
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   341
                obj = idToOb[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   342
                IO = PDFIndirectObject(id, obj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   343
                IOf = IO.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   344
                # add a comment to the PDF output
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   345
                if DoComments:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   346
                    File.add("%% %s %s %s" % (repr(id), repr(repr(obj)[:50]), LINEEND))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   347
                offset = File.add(IOf)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   348
                idToOf[id] = offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   349
                ids.append(id)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   350
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   351
                done = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   352
        # sanity checks (must happen AFTER formatting)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   353
        lno = len(numbertoid)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   354
        if counter-1!=lno:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   355
            raise ValueError, "counter %s doesn't match number to id dictionary %s" %(counter, lno)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   356
        # now add the xref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   357
        xref = PDFCrossReferenceTable()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   358
        xref.addsection(0, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   359
        xreff = xref.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   360
        xrefoffset = File.add(xreff)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   361
        # now add the trailer
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   362
        trailer = PDFTrailer(
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   363
            startxref = xrefoffset,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   364
            Size = lno,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   365
            Root = self.Reference(cat),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   366
            Info = self.Reference(info)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   367
            )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   368
        trailerf = trailer.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   369
        File.add(trailerf)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   370
        # return string format for pdf file
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   371
        return File.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   372
    
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   373
    def hasForm(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   374
        """test for existence of named form"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   375
        internalname = self.formName(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   376
        try:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   377
            test = self.idToObject[internalname]          
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   378
        except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   379
            return 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   380
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   381
            return internalname
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   382
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   383
    def xobjDict(self, formnames):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   384
        """construct an xobject dict (for inclusion in a resource dict, usually)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   385
           from a list of form names (images not yet supported)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   386
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   387
        for name in formnames:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   388
            internalname = self.formName(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   389
            reference = PDFObjectReference(internalname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   390
            D[internalname] = reference
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   391
        #print "xobjDict D", D
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   392
        return PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   393
        
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   394
    def Reference(self, object, name=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   395
        ### note references may "grow" during the final formatting pass: don't use d.keys()!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   396
        # don't make references to other references, or non instances
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   397
        from types import InstanceType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   398
        #print"object type is ", type(object)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   399
        tob = type(object)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   400
        if (tob is not InstanceType) or (tob is InstanceType and object.__class__ is PDFObjectReference):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   401
            return object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   402
        idToObject = self.idToObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   403
        if hasattr(object, __InternalName__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   404
            # already registered
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   405
            intname = object.__InternalName__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   406
            if name is not None and name!=intname:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   407
                raise ValueError, "attempt to reregister object %s with new name %s" % (
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   408
                    repr(intname), repr(name))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   409
            if not idToObject.has_key(intname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   410
                raise ValueError, "object named but not registered"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   411
            return PDFObjectReference(intname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   412
        # otherwise register the new object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   413
        objectcounter = self.objectcounter = self.objectcounter+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   414
        if name is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   415
            name = "R"+repr(objectcounter)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   416
        if idToObject.has_key(name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   417
            raise ValueError, "redefining named object: "+repr(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   418
        object.__InternalName__ = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   419
        self.idToObjectNumberAndVersion[name] = (objectcounter, 0)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   420
        self.numberToId[objectcounter] = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   421
        idToObject[name] = object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   422
        return PDFObjectReference(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   423
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   424
### chapter 4 Objects
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   425
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   426
PDFtrue = "true"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   427
PDFfalse = "false"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   428
PDFnull = "null"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   429
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   430
def PDFnumber(n):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   431
    return n
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   432
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   433
def PDFString(str):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   434
    # might need to change this to class for encryption
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   435
    return "(%s)" % pdfutils._escape(str)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   436
    
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   437
def PDFName(data):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   438
    # first convert the name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   439
    ldata = list(data)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   440
    index = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   441
    for thischar in data:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   442
        if 0x21<=ord(thischar)<=0x7e and thischar not in "%()<>{}[]#":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   443
            pass # no problemo
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   444
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   445
            hexord = hex(ord(thischar))[2:] # forget the 0x thing...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   446
            ldata[index] = "#"+hexord
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   447
        index = index+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   448
    data = string.join(ldata, "")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   449
    return "/%s" % data
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   450
    
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   451
class PDFDictionary:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   452
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   453
    multiline = LongFormat
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   454
    def __init__(self, dict=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   455
        """dict should be namestring to value eg "a": 122 NOT pdfname to value NOT "/a":122"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   456
        if dict is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   457
            self.dict = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   458
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   459
            self.dict = dict.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   460
    def __setitem__(self, name, value):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   461
        self.dict[name] = value
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   462
    def Reference(name, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   463
        ob = self.dict[name]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   464
        self.dict[name] = document.Reference(ob)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   465
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   466
        dict = self.dict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   467
        keys = dict.keys()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   468
        keys.sort()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   469
        L = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   470
        a = L.append
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   471
        for k in keys:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   472
            v = dict[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   473
            fv = format(v, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   474
            fk = format(PDFName(k), document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   475
            a(fk)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   476
            a(" "+fv)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   477
        #L = map(str, L)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   478
        if self.multiline:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   479
            Lj = string.join(L, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   480
            Lj = indent(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   481
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   482
            Lj = L
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   483
            # break up every 6 elements anyway
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   484
            for i in range(6, len(Lj), 6):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   485
                Lj.insert(i,LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   486
            Lj = string.join(L, " ")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   487
        return "<< %s >>" % Lj
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   488
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   489
STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   490
             "stream" # stream keyword
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   491
             "%(LINEEND)s" # a line end (could be just a \n)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   492
             "%(content)s" # the content, with no lineend
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   493
             "endstream%(LINEEND)s" # the endstream keyword
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   494
             )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   495
class PDFStream:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   496
    '''set dictionary elements explicitly stream.dictionary[name]=value'''
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   497
    ### compression stuff not implemented yet
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   498
    __RefOnly__ = 1 # must be at top level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   499
    def __init__(self, dictionary=None, content=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   500
        if dictionary is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   501
            dictionary = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   502
        self.dictionary = dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   503
        self.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   504
        self.filters = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   505
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   506
        dictionary = self.dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   507
        content = self.content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   508
        filters = self.filters
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   509
        if self.content is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   510
            raise ValueError, "stream content not set"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   511
        if filters is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   512
            filters = document.defaultStreamFilters
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   513
        if filters is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   514
            raise "oops", "filters for streams not yet implemented"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   515
        fc = format(content, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   516
        #print "type(content)", type(content)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   517
        if fc!=content: burp
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   518
        # set dictionary length parameter
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   519
        dictionary["Length"] = len(content)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   520
        fd = format(dictionary, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   521
        sdict = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   522
        sdict["dictionary"] = fd
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   523
        sdict["content"] = fc
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   524
        return STREAMFMT % sdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   525
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   526
def teststream(content=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   527
    #content = "" # test
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   528
    if content is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   529
        content = teststreamcontent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   530
    content = string.strip(content)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   531
    content = string.replace(content, "\n", LINEEND) + LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   532
    S = PDFStream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   533
    S.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   534
    # nothing else needed...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   535
    S.__Comment__ = "test stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   536
    return S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   537
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   538
teststreamcontent = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   539
1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   540
1.00 0.00 1.00 rg
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   541
n 72.00 72.00 432.00 648.00 re B*
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   542
"""       
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   543
class PDFArray:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   544
    multiline = LongFormat
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   545
    def __init__(self, sequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   546
        self.sequence = list(sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   547
    def References(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   548
        """make all objects in sequence references"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   549
        self.sequence = map(document.Reference, self.sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   550
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   551
        #ssequence = map(str, self.sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   552
        sequence = self.sequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   553
        fsequence = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   554
        for elt in sequence:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   555
            felt = format(elt, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   556
            fsequence.append(felt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   557
        if self.multiline:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   558
            Lj = string.join(fsequence, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   559
            Lj = indent(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   560
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   561
            # break up every 10 elements anyway
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   562
            Lj = fsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   563
            breakline = LINEEND+" "
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   564
            for i in range(10, len(Lj), 10):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   565
                Lj.insert(i,breakline)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   566
            Lj = string.join(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   567
        return "[ %s ]" % Lj
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   568
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   569
INDIRECTOBFMT = ("%(n)s %(v)s obj%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   570
                 "%(content)s" "%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   571
                 "endobj" "%(LINEEND)s")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   572
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   573
class PDFIndirectObject:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   574
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   575
    def __init__(self, name, content):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   576
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   577
        self.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   578
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   579
        name = self.name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   580
        (n, v) = document.idToObjectNumberAndVersion[name]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   581
        content = self.content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   582
        fcontent = format(content, document, toplevel=1) # yes this is at top level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   583
        sdict = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   584
        sdict["n"] = n
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   585
        sdict["v"] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   586
        sdict["content"] = fcontent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   587
        return INDIRECTOBFMT % sdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   588
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   589
class PDFObjectReference:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   590
    def __init__(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   591
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   592
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   593
        name = self.name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   594
        (n, v) = document.idToObjectNumberAndVersion[name]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   595
        return "%s %s R" % (n,v)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   596
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   597
### chapter 5
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   598
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   599
PDFHeader = ("%PDF-1.3"+LINEEND+"%í춾  "+LINEEND)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   600
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   601
class PDFFile:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   602
    ### just accumulates strings: keeps track of current offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   603
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   604
        self.strings = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   605
        self.offset = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   606
        self.add(PDFHeader)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   607
    def add(self, s):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   608
        """should be constructed as late as possible, return position where placed"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   609
        result = self.offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   610
        self.offset = result+len(s)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   611
        self.strings.append(s)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   612
        return result
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   613
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   614
        return string.join(self.strings, "")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   615
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   616
XREFFMT = '%0.10d %0.5d n'    
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   617
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   618
class PDFCrossReferenceSubsection:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   619
    def __init__(self, firstentrynumber, idsequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   620
        self.firstentrynumber = firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   621
        self.idsequence = idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   622
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   623
        """id sequence should represent contiguous object nums else error. free numbers not supported (yet)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   624
        firstentrynumber = self.firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   625
        idsequence = self.idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   626
        entries = list(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   627
        nentries = len(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   628
        # special case: object number 0 is always free
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   629
        taken = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   630
        if firstentrynumber==0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   631
            taken[0] = "standard free entry"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   632
            nentries = nentries+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   633
            entries.insert(0, "0000000000 65535 f")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   634
        idToNV = document.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   635
        idToOffset = document.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   636
        lastentrynumber = firstentrynumber+nentries-1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   637
        for id in idsequence:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   638
            (num, version) = idToNV[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   639
            if taken.has_key(num):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   640
                raise ValueError, "object number collision %s %s %s" % (num, repr(id), repr(taken[id]))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   641
            if num>lastentrynumber or num<firstentrynumber:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   642
                raise ValueError, "object number %s not in range %s..%s" % (num, firstentrynumber, lastentrynumber)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   643
            # compute position in list
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   644
            rnum = num-firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   645
            taken[num] = id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   646
            offset = idToOffset[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   647
            entries[num] = XREFFMT % (offset, version)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   648
        # now add the initial line
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   649
        firstline = "%s %s" % (firstentrynumber, nentries)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   650
        entries.insert(0, firstline)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   651
        # make sure it ends with a LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   652
        entries.append("")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   653
        if LINEEND=="\n" or LINEEND=="\r":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   654
            reflineend = " "+LINEEND # as per spec
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   655
        elif LINEEND=="\r\n":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   656
            reflineend = LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   657
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   658
            raise ValueError, "bad end of line! %s" % repr(LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   659
        return string.join(entries, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   660
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   661
class PDFCrossReferenceTable:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   662
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   663
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   664
        self.sections = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   665
    def addsection(self, firstentry, ids):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   666
        section = PDFCrossReferenceSubsection(firstentry, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   667
        self.sections.append(section)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   668
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   669
        sections = self.sections
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   670
        if not sections:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   671
            raise ValueError, "no crossref sections"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   672
        L = ["xref"+LINEEND]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   673
        for s in self.sections:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   674
            fs = format(s, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   675
            L.append(fs)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   676
        return string.join(L, "")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   677
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   678
TRAILERFMT = ("trailer%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   679
              "%(dict)s%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   680
              "startxref%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   681
              "%(startxref)s%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   682
              "%(PERCENT)s%(PERCENT)sEOF")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   683
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   684
class PDFTrailer:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   685
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   686
    def __init__(self, startxref, Size=None, Prev=None, Root=None, Info=None, ID=None, Encrypt=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   687
        self.startxref = startxref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   688
        if Size is None or Root is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   689
            raise ValueError, "Size and Root keys required"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   690
        dict = self.dict = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   691
        for (n,v) in [("Size", Size), ("Prev", Prev), ("Root", Root),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   692
                      ("Info", Info), ("Id", ID), ("Encrypt", Encrypt)]:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   693
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   694
                dict[n] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   695
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   696
        fdict = format(self.dict, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   697
        D = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   698
        D["dict"] = fdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   699
        D["startxref"] = self.startxref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   700
        return TRAILERFMT % D
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   701
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   702
#### XXXX skipping incremental update,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   703
#### encryption
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   704
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   705
#### chapter 6, doc structure
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   706
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   707
class PDFCatalog:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   708
    __Comment__ = "Document Root"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   709
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   710
    # to override, set as attributes
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   711
    __Defaults__ = {"Type": PDFName("Catalog"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   712
                "PageMode": PDFName("UseNone"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   713
                }
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   714
    __NoDefault__ = string.split("""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   715
        Dests Outlines Pages Threads AcroForm Names OpenActions PageMode URI
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   716
        ViewerPreferences PageLabels PageLayout JavaScript StructTreeRoot SpiderInfo"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   717
                                 )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   718
    __Refs__ = __NoDefault__ # make these all into references, if present
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   719
    
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   720
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   721
        self.check_format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   722
        defaults = self.__Defaults__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   723
        Refs = self.__Refs__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   724
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   725
        for k in defaults.keys():
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   726
            default = defaults[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   727
            v = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   728
            if hasattr(self, k) and getattr(self,k) is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   729
                v = getattr(self, k)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   730
            elif default is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   731
                v = default
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   732
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   733
                D[k] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   734
        for k in self.__NoDefault__:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   735
            if hasattr(self, k):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   736
                v = getattr(self,k)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   737
                if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   738
                    D[k] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   739
        # force objects to be references where required
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   740
        for k in Refs:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   741
            if D.has_key(k):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   742
                #print"k is", k, "value", D[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   743
                D[k] = document.Reference(D[k])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   744
        dict = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   745
        return format(dict, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   746
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   747
    def showOutline(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   748
        self.PageMode = PDFName("UseOutlines")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   749
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   750
    def showFullScreen(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   751
        self.PageMode = PDFName("FullScreen")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   752
        
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   753
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   754
        """for use in subclasses"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   755
        pass
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   756
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   757
# not yet implementing
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   758
#  ViewerPreferences, PageLabelDictionaries,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   759
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   760
class PDFPages(PDFCatalog):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   761
    """PAGES TREE WITH ONE INTERNAL NODE, FOR "BALANCING" CHANGE IMPLEMENATION"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   762
    __Comment__ = "page tree"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   763
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   764
    # note: could implement page attribute inheritance...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   765
    __Defaults__ = {"Type": PDFName("Pages"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   766
                    }
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   767
    __NoDefault__ = string.split("Kids Count Parent")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   768
    __Refs__ = ["Parent"]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   769
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   770
        self.pages = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   771
    def __getitem__(self, item):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   772
        return self.pages[item]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   773
    def addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   774
        self.pages.append(page)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   775
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   776
        # convert all pages to page references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   777
        pages = self.pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   778
        kids = PDFArray(self.pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   779
        # make sure all pages are references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   780
        kids.References(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   781
        self.Kids = kids
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   782
        self.Count = len(pages)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   783
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   784
class PDFPage(PDFCatalog):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   785
    __Comment__ = "Page dictionary"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   786
    # all PDF attributes can be set explicitly
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   787
    # if this flag is set, the "usual" behavior will be suppressed
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   788
    Override_default_compilation = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   789
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   790
    __Defaults__ = {"Type": PDFName("Page"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   791
                   # "Parent": PDFObjectReference(Pages),  # no! use document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   792
                    }
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   793
    __NoDefault__ = string.split(""" Parent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   794
        MediaBox Resources Contents CropBox Rotate Thumb Annots B Dur Hid Trans AA
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   795
        PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   796
    """)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   797
    __Refs__ = string.split("""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   798
        Contents Parent ID
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   799
    """)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   800
    pagewidth = 595
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   801
    pageheight = 842
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   802
    stream = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   803
    hasImages = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   804
    compression = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   805
    XObjects = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   806
    # transitionstring?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   807
    # xobjects?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   808
    # annotations
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   809
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   810
        # set all nodefaults to None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   811
        for name in self.__NoDefault__:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   812
            setattr(self, name, None)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   813
    def setCompression(self, onoff):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   814
        self.compression = onoff
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   815
    def setStream(self, code):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   816
        if self.Override_default_compilation:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   817
            raise ValueError, "overridden! must set stream explicitly"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   818
        from types import ListType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   819
        if type(code) is ListType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   820
            code = string.join(code, LINEEND)+LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   821
        self.stream = code
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   822
        
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   823
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   824
        # set up parameters unless usual behaviour is suppressed
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   825
        if self.Override_default_compilation:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   826
            return   
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   827
        self.MediaBox = self.MediaBox or PDFArray([0, 0, self.pagewidth, self.pageheight])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   828
        if not self.Annots:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   829
            self.Annots = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   830
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   831
            #print self.Annots
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   832
            #raise ValueError, "annotations not reimplemented yet"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   833
            if type(self.Annots) is not types.InstanceType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   834
                self.Annots = PDFArray(self.Annots)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   835
        if not self.Contents:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   836
            stream = self.stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   837
            if not stream:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   838
                self.Contents = teststream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   839
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   840
                S = PDFStream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   841
                S.content = stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   842
                # need to add filter stuff (?)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   843
                S.__Comment__ = "page stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   844
                self.Contents = S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   845
        if not self.Resources:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   846
            resources = PDFResourceDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   847
            # fonts!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   848
            resources.basicFonts()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   849
            if self.hasImages:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   850
                resources.allProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   851
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   852
                resources.basicProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   853
            if self.XObjects:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   854
                #print "XObjects", self.XObjects.dict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   855
                resources.XObject = self.XObjects
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   856
            self.Resources = resources
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   857
        if not self.Parent:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   858
            pages = document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   859
            self.Parent = document.Reference(pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   860
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   861
def testpage(document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   862
    P = PDFPage()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   863
    P.Contents = teststream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   864
    pages = document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   865
    P.Parent = document.Reference(pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   866
    P.MediaBox = PDFArray([0, 0, 595, 841])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   867
    resources = PDFResourceDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   868
    resources.allProcs() # enable all procsets
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   869
    resources.basicFonts()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   870
    P.Resources = resources
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   871
    pages.addPage(P)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   872
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   873
#### DUMMY OUTLINES IMPLEMENTATION FOR NOW
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   874
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   875
'''    
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   876
DUMMYOUTLINE = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   877
<<
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   878
  /Count
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   879
      0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   880
  /Type
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   881
      /Outlines
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   882
>>"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   883
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   884
class PDFOutlines:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   885
    __Comment__ = "TEST OUTLINE!"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   886
    text = string.replace(DUMMYOUTLINE, "\n", LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   887
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   888
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   889
        return self.text
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   890
        '''
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   891
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   892
class OutlineEntryObject:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   893
	"an entry in an outline"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   894
	Title = Dest = Parent = Prev = Next = First = Last = Count = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   895
	def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   896
		D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   897
		D["Title"] = PDFString(self.Title)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   898
		D["Parent"] = self.Parent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   899
		D["Dest"] = self.Dest
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   900
		for n in ("Prev", "Next", "First", "Last", "Count"):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   901
			v = getattr(self, n)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   902
			if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   903
				D[n] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   904
		PD = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   905
		return PD.format(document)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   906
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   907
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   908
class PDFOutlines:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   909
	"""takes a recursive list of outline destinations
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   910
	   like
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   911
		   out = PDFOutline1()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   912
		   out.setNames(canvas, # requires canvas for name resolution
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   913
			 "chapter1dest",
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   914
			 ("chapter2dest",
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   915
			  ["chapter2section1dest",
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   916
			   "chapter2section2dest",
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   917
			   "chapter2conclusiondest"]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   918
			 ), # end of chapter2 description
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   919
			 "chapter3dest",
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   920
			 ("chapter4dest", ["c4s1", "c4s2"])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   921
			 )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   922
	   Higher layers may build this structure incrementally. KISS at base level.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   923
	"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   924
	# first attempt, many possible features missing.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   925
	#no init for now
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   926
	mydestinations = ready = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   927
	counter = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   928
	currentlevel = -1 # ie, no levels yet
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   929
	
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   930
	def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   931
		self.destinationnamestotitles = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   932
		self.destinationstotitles = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   933
		self.levelstack = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   934
		self.buildtree = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   935
		self.closedict = {} # dictionary of "closed" destinations in the outline
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   936
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   937
	def addOutlineEntry(self, destinationname, level=0, title=None, closed=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   938
		"""destinationname of None means "close the tree" """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   939
		from types import IntType, TupleType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   940
		if destinationname is None and level!=0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   941
			raise ValueError, "close tree must have level of 0"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   942
		if type(level) is not IntType: raise ValueError, "level must be integer, got %s" % type(level)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   943
		if level<0: raise ValueError, "negative levels not allowed"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   944
		if title is None: title = destinationname
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   945
		currentlevel = self.currentlevel
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   946
		stack = self.levelstack
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   947
		tree = self.buildtree
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   948
		# adjust currentlevel and stack to match level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   949
		if level>currentlevel:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   950
			if level>currentlevel+1:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   951
				raise ValueError, "can't jump from outline level %s to level %s, need intermediates" %(currentlevel, level)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   952
			level = currentlevel = currentlevel+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   953
			stack.append([])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   954
		while level<currentlevel:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   955
			# pop off levels to match
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   956
			current = stack[-1]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   957
			del stack[-1]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   958
			previous = stack[-1]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   959
			lastinprevious = previous[-1]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   960
			if type(lastinprevious) is TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   961
				(name, sectionlist) = lastinprevious
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   962
				raise ValueError, "cannot reset existing sections: " + repr(lastinprevious)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   963
			else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   964
				name = lastinprevious
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   965
				sectionlist = current
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   966
				previous[-1] = (name, sectionlist)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   967
			#sectionlist.append(current)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   968
			currentlevel = currentlevel-1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   969
		if destinationname is None: return
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   970
		stack[-1].append(destinationname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   971
		self.destinationnamestotitles[destinationname] = title
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   972
		if closed: self.closedict[destinationname] = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   973
		self.currentlevel = level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   974
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   975
	def setDestinations(self, destinationtree):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   976
		self.mydestinations = destinationtree
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   977
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   978
	def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   979
		D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   980
		D["Type"] = PDFName("Outlines")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   981
		c = self.count
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   982
		D["Count"] = c
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   983
		if c!=0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   984
		    D["First"] = self.first
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   985
		    D["Last"] = self.last
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   986
		PD = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   987
		return PD.format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   988
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   989
	def setNames(self, canvas, *nametree):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   990
		desttree = self.translateNames(canvas, nametree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   991
		self.setDestinations(desttree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   992
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   993
	def setNameList(self, canvas, nametree):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   994
		"Explicit list so I don't need to do apply(...) in the caller"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   995
		desttree = self.translateNames(canvas, nametree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   996
		self.setDestinations(desttree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   997
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   998
	def translateNames(self, canvas, object):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   999
		"recursively translate tree of names into tree of destinations"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1000
		from types import StringType, ListType, TupleType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1001
		Ot = type(object)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1002
		destinationnamestotitles = self.destinationnamestotitles
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1003
		destinationstotitles = self.destinationstotitles
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1004
		closedict = self.closedict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1005
		if Ot is StringType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1006
			destination = canvas._bookmarkReference(object)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1007
			title = object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1008
			if destinationnamestotitles.has_key(object):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1009
				title = destinationnamestotitles[object]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1010
			else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1011
				destinationnamestotitles[title] = title
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1012
			destinationstotitles[destination] = title
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1013
			if closedict.has_key(object):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1014
				closedict[destination] = 1 # mark destination closed
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1015
			return {object: canvas._bookmarkReference(object)} # name-->ref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1016
		if Ot is ListType or Ot is TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1017
			L = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1018
			for o in object:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1019
				L.append(self.translateNames(canvas, o))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1020
			if Ot is TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1021
				return tuple(L)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1022
			return L
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1023
		raise "in outline, destination name must be string: got a %s" % Ot
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1024
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1025
	def prepare(self, document, canvas):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1026
		"""prepare all data structures required for save operation (create related objects)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1027
		if self.mydestinations is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1028
			if self.levelstack:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1029
				self.addOutlineEntry(None) # close the tree
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1030
				destnames = self.levelstack[0]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1031
				#from pprint import pprint; pprint(destnames); stop
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1032
				self.mydestinations = self.translateNames(canvas, destnames)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1033
			else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1034
				self.first = self.last = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1035
				self.count = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1036
				self.ready = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1037
				return
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1038
		#self.first = document.objectReference("Outline.First")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1039
		#self.last = document.objectReference("Outline.Last")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1040
		# XXXX this needs to be generalized for closed entries!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1041
		self.count = count(self.mydestinations, self.closedict)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1042
		(self.first, self.last) = self.maketree(document, self.mydestinations, toplevel=1)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1043
		self.ready = 1
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1044
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1045
	def maketree(self, document, destinationtree, Parent=None, toplevel=0):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1046
		from types import ListType, TupleType, DictType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1047
		tdestinationtree = type(destinationtree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1048
		if toplevel:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1049
			levelname = "Outline"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1050
			Parent = document.Reference(document.Outlines)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1051
		else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1052
			self.count = self.count+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1053
			levelname = "Outline.%s" % self.count
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1054
			if Parent is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1055
				raise ValueError, "non-top level outline elt parent must be specified"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1056
		if tdestinationtree is not ListType and tdestinationtree is not TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1057
			raise ValueError, "destinationtree must be list or tuple, got %s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1058
		nelts = len(destinationtree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1059
		lastindex = nelts-1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1060
		lastelt = firstref = lastref = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1061
		destinationnamestotitles = self.destinationnamestotitles
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1062
		closedict = self.closedict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1063
		for index in range(nelts):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1064
			eltobj = OutlineEntryObject()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1065
			eltobj.Parent = Parent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1066
			eltname = "%s.%s" % (levelname, index)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1067
			eltref = document.Reference(eltobj, eltname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1068
			#document.add(eltname, eltobj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1069
			if lastelt is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1070
				lastelt.Next = eltref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1071
				eltobj.Prev = lastref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1072
			if firstref is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1073
				firstref = eltref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1074
			lastref = eltref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1075
			lastelt = eltobj # advance eltobj
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1076
			lastref = eltref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1077
			elt = destinationtree[index]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1078
			te = type(elt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1079
			if te is DictType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1080
				# simple leaf {name: dest}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1081
				leafdict = elt
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1082
			elif te is TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1083
				# leaf with subsections: ({name: ref}, subsections) XXXX should clean up (see count(...))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1084
				try:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1085
					(leafdict, subsections) = elt
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1086
				except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1087
					raise ValueError, "destination tree elt tuple should have two elts, got %s" % len(elt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1088
				eltobj.Count = count(subsections, closedict)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1089
				(eltobj.First, eltobj.Last) = self.maketree(document, subsections, eltref)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1090
			else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1091
				raise ValueError, "destination tree elt should be dict or tuple, got %s" % te
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1092
			try:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1093
				[(Title, Dest)] = leafdict.items()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1094
			except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1095
				raise ValueError, "bad outline leaf dictionary, should have one entry "+str(elt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1096
			eltobj.Title = destinationnamestotitles[Title]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1097
			eltobj.Dest = Dest
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1098
			if te is TupleType and closedict.has_key(Dest):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1099
				# closed subsection, count should be negative
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1100
				eltobj.Count = -eltobj.Count
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1101
		return (firstref, lastref)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1102
		
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1103
def count(tree, closedict=None): 
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1104
	"""utility for outline: recursively count leaves in a tuple/list tree"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1105
	from operator import add
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1106
	from types import TupleType, ListType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1107
	tt = type(tree)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1108
	if tt is TupleType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1109
		# leaf with subsections XXXX should clean up this structural usage
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1110
		(leafdict, subsections) = tree
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1111
		[(Title, Dest)] = leafdict.items()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1112
		if closedict and closedict.has_key(Dest):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1113
			return 1 # closed tree element
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1114
	if tt is TupleType or tt is ListType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1115
		#return reduce(add, map(count, tree))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1116
		counts = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1117
		for e in tree:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1118
			counts.append(count(e, closedict))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1119
		return reduce(add, counts)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1120
	return 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1121
	
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1122
	
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1123
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1124
#### dummy info
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1125
DUMMYINFO = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1126
<</Title (testing)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1127
/Author (arw)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1128
/CreationDate (D:20001012220652)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1129
/Producer (ReportLab http://www.reportlab.com)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1130
/Subject (this file generated by an alpha test module)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1131
>>
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1132
"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1133
class PDFInfo0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1134
    __Comment__ = "TEST INFO STRUCTURE"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1135
    text = string.replace(DUMMYINFO, "\n", LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1136
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1137
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1138
        return self.text
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1139
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1140
class PDFInfo:
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1141
    """PDF documents can have basic information embedded, viewable from
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1142
    File | Document Info in Acrobat Reader.  If this is wrong, you get
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1143
    Postscript errors while printing, even though it does not print."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1144
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1145
        self.title = "untitled"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1146
        self.author = "anonymous"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1147
        self.subject = "unspecified"
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1148
        #now = time.localtime(time.time())
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1149
        #self.datestr = '%04d%02d%02d%02d%02d%02d' % tuple(now[0:6])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1150
        
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1151
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1152
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1153
        D["Title"] = PDFString(self.title)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1154
        D["Author"] = PDFString(self.author)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1155
        D["CreationDate"] = PDFDate()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1156
        D["Producer"] = PDFString("ReporLab http://www.reportlab.com")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1157
        D["Subject"] = PDFString(self.subject)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1158
        PD = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1159
        return PD.format(document)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1160
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1161
# skipping thumbnails, etc
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1162
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1163
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1164
class Annotation:
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1165
    """superclass for all annotations."""
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1166
    defaults = [("Type", PDFName("Annot"),)]
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1167
    required = ("Type", "Rect", "Contents", "Subtype")
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1168
    permitted = required+(
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1169
      "Border", "C", "T", "M", "F", "H", "BS", "AA", "AS", "Popup", "P")
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1170
    def cvtdict(self, d):
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1171
        """transform dict args from python form to pdf string rep as needed"""
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1172
        Rect = d["Rect"]
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1173
        if type(Rect) is not types.StringType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1174
            d["Rect"] = PDFArray(Rect)
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1175
        d["Contents"] = PDFString(d["Contents"])
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1176
        return d
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1177
    def AnnotationDict(self, **kw):
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1178
        d = {}
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1179
        for (name,val) in self.defaults:
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1180
            d[name] = val
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1181
        d.update(kw)
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1182
        for name in self.required:
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1183
            if not d.has_key(name):
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1184
                raise ValueError, "keyword argument %s missing" % name
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1185
        d = self.cvtdict(d)
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1186
        permitted = self.permitted
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1187
        for name in d.keys():
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1188
            if name not in permitted:
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1189
                raise ValueError, "bad annotation dictionary name %s" % name
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1190
        return PDFDictionary(d)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1191
    def Dict(self):
53
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1192
        raise ValueError, "DictString undefined for virtual superclass Annotation, must overload"
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1193
        # but usually
e624c027a5d6 Added forms, destinations, linkages and other features
aaron_watters
parents: 47
diff changeset
  1194
        #return self.AnnotationDict(self, Rect=(a,b,c,d)) or whatever
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset