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