reportlab/pdfbase/pdfdoc.py
author rgbecker
Thu, 01 Sep 2005 16:18:33 +0000
changeset 2517 c8f07edb561d
parent 2487 a51bf871b709
child 2524 7e6da7c7fe2c
permissions -rwxr-xr-x
pdfdoc: allow special escaping in Annotations and PDFString
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2274
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 484
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2274
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfbase/pdfdoc.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2274
diff changeset
     4
__version__=''' $Id$ '''
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     5
__doc__="""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     6
The module pdfdoc.py handles the 'outer structure' of PDF documents, ensuring that
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     7
all objects are properly cross-referenced and indexed to the nearest byte.  The
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     8
'inner structure' - the page descriptions - are presumed to be generated before
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
each page is saved.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    10
pdfgen.py calls this and provides a 'canvas' object to handle page marking operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    11
piddlePDF calls pdfgen and offers a high-level interface.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    12
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    13
The classes within this generally mirror structures in the PDF file
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    14
and are not part of any public interface.  Instead, canvas and font
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    15
classes are made available elsewhere for users to manipulate.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    16
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    17
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
    18
import string, types, binascii
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    19
from reportlab.pdfbase import pdfutils
2067
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    20
from reportlab.pdfbase.pdfutils import LINEEND # this constant needed in both
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    21
from reportlab import rl_config
2067
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    22
from reportlab.lib.utils import import_zlib, open_for_read, fp_str
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    23
1653
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    24
from sys import platform
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    25
try:
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    26
    from sys import version_info
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    27
except: # pre-2.0
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    28
    # may be inaccurate but will at least
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    29
    #work in anything which seeks to format
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    30
    # version_info into a string
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    31
    version_info = (1,5,2,'unknown',0)
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    32
1622
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    33
if platform[:4] == 'java' and version_info[:2] == (2, 1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1653
diff changeset
    34
    # workaround for list()-bug in Jython 2.1 (should be fixed in 2.2)
1622
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    35
    def list(sequence):
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    36
        def f(x):
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    37
            return x
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    38
        return map(f, sequence)
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    39
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    40
class PDFError(Exception):
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    41
    pass
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    42
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    44
# set this flag to get more vertical whitespace (and larger files)
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1317
diff changeset
    45
LongFormat = 1
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    46
##if LongFormat: (doesn't work)
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    47
##    pass
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    48
##else:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    49
##    LINEEND = "\n" # no wasteful carriage returns!
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    50
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    51
# __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
    52
__InternalName__ = "__InternalName__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    53
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    54
# __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
    55
__RefOnly__ = "__RefOnly__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    56
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    57
# __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
    58
#   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
    59
__Comment__ = "__Comment__"
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    60
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    61
# If DoComments is set then add helpful (space wasting) comment lines to PDF files
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    62
DoComments = 1
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    63
if not LongFormat:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    64
    DoComments = 0
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    65
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    66
# name for standard font dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    67
BasicFonts = "BasicFonts"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    68
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    69
# name for the pages object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    70
Pages = "Pages"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    71
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    72
### generic utilities
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    73
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    74
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    75
# for % substitutions
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    76
LINEENDDICT = {"LINEEND": LINEEND, "PERCENT": "%"}
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    77
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    78
def format(element, document, toplevel=0):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    79
    """Indirection step for formatting.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    80
       Ensures that document parameters alter behaviour
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    81
       of formatting for all elements.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    82
    """
2067
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    83
    from types import InstanceType, FloatType, IntType
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    84
    if type(element) is InstanceType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    85
        if not toplevel and hasattr(element, __RefOnly__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    86
            # 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
    87
            # 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
    88
            R = document.Reference(element)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    89
            return R.format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    90
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    91
            try:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    92
                fmt = element.format
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    93
            except:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    94
                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
    95
            f = fmt(document)
2273
82f026361b70 Fix bug introduced during transparency/ImageReader changes
rgbecker
parents: 2234
diff changeset
    96
            if not rl_config.invariant and DoComments and hasattr(element, __Comment__):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    97
                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
    98
            return f
2067
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    99
    elif type(element) in (FloatType, IntType):
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
   100
        #use a controlled number formatting routine
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
   101
        #instead of str, so Jython/Python etc do not differ
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
   102
        return fp_str(element)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   103
    else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   104
        return str(element)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   105
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   106
def indent(s, IND=LINEEND+" "):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   107
    return string.replace(s, LINEEND, IND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   108
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   109
def xObjectName(externalname):
1560
382bbd1f20ae revert to older internal name for backward compatibility
rgbecker
parents: 1554
diff changeset
   110
    return "FormXob.%s" % externalname
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   111
1605
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
   112
# backwards compatibility
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
   113
formName = xObjectName
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
   114
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   115
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   116
# no encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   117
class NoEncryption:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   118
    def encode(self, t):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   119
        "encode a string, stream, text"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   120
        return t
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   121
    def prepare(self, document):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   122
        # get ready to do encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   123
        pass
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   124
    def register(self, objnum, version):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   125
        # enter a new direct object
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   126
        pass
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   127
    def info(self):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   128
        # the representation of self in file if any (should be None or PDFDict)
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   129
        return None
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   130
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   131
class DummyDoc:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   132
    "used to bypass encryption when required"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   133
    encrypt = NoEncryption()
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   134
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   135
### the global document structure manager
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   136
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   137
class PDFDocument:
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   138
    _ID = None
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   139
    objectcounter = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   140
    inObject = None
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   141
    # set this to define filters
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   142
    defaultStreamFilters = None
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   143
    encrypt = NoEncryption() # default no encryption
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   144
    pageCounter = 1
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   145
    def __init__(self,
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   146
                 encoding=rl_config.defaultEncoding,
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   147
                 dummyoutline=0,
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   148
                 compression=rl_config.pageCompression,
2476
19fb0900fba1 pdfdoc.py: minor changes to simplify PDFInfoL/U in other apps
rgbecker
parents: 2378
diff changeset
   149
                 invariant=rl_config.invariant,
19fb0900fba1 pdfdoc.py: minor changes to simplify PDFInfoL/U in other apps
rgbecker
parents: 2378
diff changeset
   150
                 filename=None):
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   151
        #self.defaultStreamFilters = [PDFBase85Encode, PDFZCompress] # for testing!
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   152
        #self.defaultStreamFilters = [PDFZCompress] # for testing!
502
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   153
        assert encoding in ['MacRomanEncoding',
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   154
                            'WinAnsiEncoding',
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   155
                            'MacRoman',
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   156
                            'WinAnsi'], 'Unsupported encoding %s' % encoding
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   157
        if encoding[-8:] <> 'Encoding':
3a9547186146 Fixed font encoding lookup, metrics for JPython, created test directory
andy_robinson
parents: 500
diff changeset
   158
            encoding = encoding + 'Encoding'
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   159
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   160
        # allow None value to be passed in to mean 'give system defaults'
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   161
        if invariant is None:
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   162
            self.invariant = rl_config.invariant
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   163
        else:
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   164
            self.invariant = invariant
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   165
        self.setCompression(compression)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   166
        self.encoding = encoding
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   167
        # signature for creating PDF ID
1977
74967fd03ed4 Fix up invariance
rgbecker
parents: 1965
diff changeset
   168
        import md5
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   169
        sig = self.signature = md5.new()
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   170
        sig.update("a reportlab document")
1977
74967fd03ed4 Fix up invariance
rgbecker
parents: 1965
diff changeset
   171
        if not self.invariant:
1987
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   172
            cat = _getTimeStamp()
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   173
        else:
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   174
            cat = 946684800.0
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   175
        sig.update(repr(cat)) # initialize with timestamp digest
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   176
        # 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
   177
        self.idToObjectNumberAndVersion = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   178
        # 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
   179
        self.idToObject = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   180
        # internal id to file location
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   181
        self.idToOffset = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   182
        # number to id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   183
        self.numberToId = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   184
        cat = self.Catalog = self._catalog = PDFCatalog()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   185
        pages = self.Pages = PDFPages()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   186
        cat.Pages = pages
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   187
        if dummyoutline:
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   188
            outlines = PDFOutlines0()
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   189
        else:
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   190
            outlines = PDFOutlines()
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   191
        self.Outlines = self.outline = outlines
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   192
        cat.Outlines = outlines
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   193
        self.info = PDFInfo()
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   194
        self.info.invariant = self.invariant
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   195
        #self.Reference(self.Catalog)
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   196
        #self.Reference(self.Info)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   197
        self.fontMapping = {}
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   198
        #make an empty font dictionary
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   199
        DD = PDFDictionary({})
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   200
        DD.__Comment__ = "The standard fonts dictionary"
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   201
        DDR = self.Reference(DD, BasicFonts)
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   202
        self.delayedFonts = []
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   203
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   204
    def setCompression(self, onoff):
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   205
        # XXX: maybe this should also set self.defaultStreamFilters?
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   206
        self.compression = onoff
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   207
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   208
    def updateSignature(self, thing):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   209
        "add information to the signature"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   210
        if self._ID: return # but not if its used already!
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   211
        self.signature.update(str(thing))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   212
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   213
    def ID(self):
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   214
        "A unique fingerprint for the file (unless in invariant mode)"
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   215
        if self._ID:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   216
            return self._ID
1985
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   217
        digest = self.signature.digest()
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   218
        doc = DummyDoc()
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   219
        ID = PDFString(digest)
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   220
        IDs = ID.format(doc)
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   221
        self._ID = "%s %% ReportLab generated PDF document -- digest (http://www.reportlab.com) %s [%s %s] %s" % (
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   222
                LINEEND, LINEEND, IDs, IDs, LINEEND)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   223
        return self._ID
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   224
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   225
    def SaveToFile(self, filename, canvas):
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   226
        if callable(getattr(filename, "write",None)):
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   227
            myfile = 0
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   228
            f = filename
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   229
            filename = str(getattr(filename,'name',''))
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   230
        else :
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   231
            myfile = 1
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   232
            filename = str(filename)
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   233
            f = open(filename, "wb")
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   234
        f.write(self.GetPDFData(canvas))
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   235
        if myfile:
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   236
            f.close()
2010
83e136234c0f Added import of os
rgbecker
parents: 2009
diff changeset
   237
            import os
2009
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   238
            if os.name=='mac':
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   239
                from reportlab.lib.utils import markfilename
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   240
                markfilename(filename) # do platform specific file junk
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   241
        if getattr(canvas,'_verbosity',None): print 'saved', filename
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   242
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   243
    def GetPDFData(self, canvas):
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   244
        # realize delayed fonts
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   245
        for fnt in self.delayedFonts:
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   246
            fnt.addObjects(self)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   247
        # add info stuff to signature
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   248
        self.info.invariant = self.invariant
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   249
        self.info.digest(self.signature)
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   250
        ### later: maybe add more info to sig?
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   251
        # prepare outline
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   252
        self.Reference(self.Catalog)
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   253
        self.Reference(self.info)
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   254
        outline = self.outline
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   255
        outline.prepare(self, canvas)
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   256
        return self.format()
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   257
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   258
    def inPage(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   259
        """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
   260
        if self.inObject is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   261
            if self.inObject=="page": return
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   262
            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
   263
        self.inObject = "page"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   264
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   265
    def inForm(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   266
        """specify that we are in a form xobject (disable page features, etc)"""
742
df72775fc33d fixed minor bug in forms handling
aaron_watters
parents: 724
diff changeset
   267
        # don't need this check anymore since going in a form pushes old context at canvas level.
df72775fc33d fixed minor bug in forms handling
aaron_watters
parents: 724
diff changeset
   268
        #if self.inObject not in ["form", None]:
df72775fc33d fixed minor bug in forms handling
aaron_watters
parents: 724
diff changeset
   269
        #    raise ValueError, "can't go in form already in object %s" % self.inObject
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   270
        self.inObject = "form"
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   271
        # don't need to do anything else, I think...
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   272
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   273
    def getInternalFontName(self, psfontname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   274
        fm = self.fontMapping
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   275
        if fm.has_key(psfontname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   276
            return fm[psfontname]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   277
        else:
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   278
            try:
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   279
                # does pdfmetrics know about it? if so, add
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   280
                from reportlab.pdfbase import pdfmetrics
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   281
                fontObj = pdfmetrics.getFont(psfontname)
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   282
                if getattr(fontObj, '_dynamicFont', 0):
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   283
                    raise PDFError, "getInternalFontName(%s) called for a dynamic font" % repr(psfontname)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   284
                fontObj.addObjects(self)
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   285
                #self.addFont(fontObj)
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   286
                return fm[psfontname]
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   287
            except KeyError:
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   288
                raise PDFError, "Font %s not known!" % repr(psfontname)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   289
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   290
    def thisPageName(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   291
        return "Page"+repr(self.pageCounter)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   292
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   293
    def thisPageRef(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   294
        return PDFObjectReference(self.thisPageName())
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 addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   297
        name = self.thisPageName()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   298
        self.Reference(page, name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   299
        self.Pages.addPage(page)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   300
        self.pageCounter = self.pageCounter+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   301
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   302
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   303
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   304
    def addForm(self, name, form):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   305
        """add a Form XObject."""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   306
        # 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
   307
        if self.inObject != "form":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   308
            self.inForm()
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   309
        self.Reference(form, xObjectName(name))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   310
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   311
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   312
    def annotationName(self, externalname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   313
        return "Annot.%s"%externalname
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   314
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   315
    def addAnnotation(self, name, annotation):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   316
        self.Reference(annotation, self.annotationName(name))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   317
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   318
    def refAnnotation(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   319
        internalname = self.annotationName(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   320
        return PDFObjectReference(internalname)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   321
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   322
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   323
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   324
        self.info.title = title
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   325
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   326
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   327
        "embedded in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   328
        self.info.author = author
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   329
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   330
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   331
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   332
        self.info.subject = subject
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   333
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   334
    def getAvailableFonts(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   335
        fontnames = self.fontMapping.keys()
1151
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   336
        # the standard 14 are also always available! (even if not initialized yet)
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   337
        import _fontdata
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   338
        for name in _fontdata.standardFonts:
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   339
            if name not in fontnames:
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   340
                fontnames.append(name)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   341
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   342
        return fontnames
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   343
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   344
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   345
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   346
    def format(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   347
        # 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
   348
        # (possible infinite loop if there is a bug that continually makes new objects/refs...)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   349
        # Prepare encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   350
        self.encrypt.prepare(self)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   351
        cat = self.Catalog
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   352
        info = self.info
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   353
        self.Reference(self.Catalog)
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   354
        self.Reference(self.info)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   355
        # register the encryption dictionary if present
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   356
        encryptref = None
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   357
        encryptinfo = self.encrypt.info()
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   358
        if encryptinfo:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   359
            encryptref = self.Reference(encryptinfo)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   360
        # 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
   361
        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
   362
        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
   363
        numbertoid = self.numberToId
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   364
        idToNV = self.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   365
        idToOb = self.idToObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   366
        idToOf = self.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   367
        ### 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
   368
        done = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   369
        File = PDFFile() # output collector
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   370
        while done is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   371
            counter = counter+1 # do next object...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   372
            if numbertoid.has_key(counter):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   373
                id = numbertoid[counter]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   374
                #printidToOb
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   375
                obj = idToOb[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   376
                IO = PDFIndirectObject(id, obj)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   377
                # register object number and version
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   378
                #encrypt.register(id,
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   379
                IOf = IO.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   380
                # add a comment to the PDF output
2273
82f026361b70 Fix bug introduced during transparency/ImageReader changes
rgbecker
parents: 2234
diff changeset
   381
                if not rl_config.invariant and DoComments:
1840
9c20e252dcd2 Fixed how object comments are formatted - could not always __class___.__name__
andy_robinson
parents: 1838
diff changeset
   382
                    try:
9c20e252dcd2 Fixed how object comments are formatted - could not always __class___.__name__
andy_robinson
parents: 1838
diff changeset
   383
                        classname = obj.__class__.__name__
9c20e252dcd2 Fixed how object comments are formatted - could not always __class___.__name__
andy_robinson
parents: 1838
diff changeset
   384
                    except:
9c20e252dcd2 Fixed how object comments are formatted - could not always __class___.__name__
andy_robinson
parents: 1838
diff changeset
   385
                        classname = repr(obj)
9c20e252dcd2 Fixed how object comments are formatted - could not always __class___.__name__
andy_robinson
parents: 1838
diff changeset
   386
                    File.add("%% %s: class %s %s" % (repr(id), classname[:50], LINEEND))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   387
                offset = File.add(IOf)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   388
                idToOf[id] = offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   389
                ids.append(id)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   390
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   391
                done = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   392
        # sanity checks (must happen AFTER formatting)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   393
        lno = len(numbertoid)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   394
        if counter-1!=lno:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   395
            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
   396
        # now add the xref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   397
        xref = PDFCrossReferenceTable()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   398
        xref.addsection(0, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   399
        xreff = xref.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   400
        xrefoffset = File.add(xreff)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   401
        # now add the trailer
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   402
        trailer = PDFTrailer(
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   403
            startxref = xrefoffset,
1309
2b674954263d /Size in Trailer off by one.
aaron_watters
parents: 1179
diff changeset
   404
            Size = lno+1,
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   405
            Root = self.Reference(cat),
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   406
            Info = self.Reference(info),
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   407
            Encrypt = encryptref,
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   408
            ID = self.ID(),
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   409
            )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   410
        trailerf = trailer.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   411
        File.add(trailerf)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   412
        # return string format for pdf file
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   413
        return File.format(self)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   414
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   415
    def hasForm(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   416
        """test for existence of named form"""
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   417
        internalname = xObjectName(name)
1395
65c2c66a3a1b canvas.hasForm now gives honest answers.
andy_robinson
parents: 1363
diff changeset
   418
        return self.idToObject.has_key(internalname)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   419
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   420
    def getFormBBox(self, name):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   421
        "get the declared bounding box of the form as a list"
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   422
        internalname = xObjectName(name)
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   423
        if self.idToObject.has_key(internalname):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   424
            theform = self.idToObject[internalname]
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   425
            if isinstance(theform, PDFFormXObject):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   426
                # internally defined form
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   427
                return theform.BBoxList()
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   428
            elif isinstance(theform, PDFStream):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   429
                # externally defined form
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   430
                return list(theform.dictionary.dict["BBox"].sequence)
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   431
            else:
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   432
                raise ValueError, "I don't understand the form instance %s" % repr(name)
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   433
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   434
    def getXObjectName(self, name):
1395
65c2c66a3a1b canvas.hasForm now gives honest answers.
andy_robinson
parents: 1363
diff changeset
   435
        """Lets canvas find out what form is called internally.
65c2c66a3a1b canvas.hasForm now gives honest answers.
andy_robinson
parents: 1363
diff changeset
   436
        Never mind whether it is defined yet or not."""
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   437
        return xObjectName(name)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   438
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   439
    def xobjDict(self, formnames):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   440
        """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
   441
           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
   442
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   443
        for name in formnames:
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   444
            internalname = xObjectName(name)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   445
            reference = PDFObjectReference(internalname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   446
            D[internalname] = reference
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   447
        #print "xobjDict D", D
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   448
        return PDFDictionary(D)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   449
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   450
    def Reference(self, object, name=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   451
        ### note references may "grow" during the final formatting pass: don't use d.keys()!
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   452
        # don't make references to other references, or non instances, unless they are named!
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   453
        from types import InstanceType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   454
        #print"object type is ", type(object)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   455
        tob = type(object)
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   456
        idToObject = self.idToObject
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   457
        if name is None and (
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   458
            (tob is not InstanceType) or (tob is InstanceType and object.__class__ is PDFObjectReference)):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   459
            return object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   460
        if hasattr(object, __InternalName__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   461
            # already registered
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   462
            intname = object.__InternalName__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   463
            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
   464
                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
   465
                    repr(intname), repr(name))
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   466
            if not idToObject.has_key(intname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   467
                raise ValueError, "object named but not registered"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   468
            return PDFObjectReference(intname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   469
        # otherwise register the new object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   470
        objectcounter = self.objectcounter = self.objectcounter+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   471
        if name is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   472
            name = "R"+repr(objectcounter)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   473
        if idToObject.has_key(name):
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   474
            other = idToObject[name]
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   475
            if other!=object:
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   476
                raise ValueError, "redefining named object: "+repr(name)
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   477
            return PDFObjectReference(name)
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   478
        if tob is InstanceType:
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   479
            object.__InternalName__ = name
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   480
        #print "name", name, "counter", objectcounter
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   481
        self.idToObjectNumberAndVersion[name] = (objectcounter, 0)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   482
        self.numberToId[objectcounter] = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   483
        idToObject[name] = object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   484
        return PDFObjectReference(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   485
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   486
### chapter 4 Objects
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   487
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   488
PDFtrue = "true"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   489
PDFfalse = "false"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   490
PDFnull = "null"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   491
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   492
class PDFText:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   493
    def __init__(self, t):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   494
        self.t = t
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   495
    def format(self, document):
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   496
        result = binascii.hexlify(document.encrypt.encode(self.t))
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   497
        return "<%s>" % result
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   498
    def __str__(self):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   499
        dummydoc = DummyDoc()
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   500
        return self.format(dummydoc)
1014
ba7f9c1294ea added missing PDFText() function for consistency (needed by pagecatcher)
aaron_watters
parents: 873
diff changeset
   501
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   502
def PDFnumber(n):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   503
    return n
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   504
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   505
import re
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   506
_re_cleanparens=re.compile('[^()]')
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   507
del re
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   508
def _isbalanced(s):
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   509
    '''test whether a string is balanced in parens'''
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   510
    s = _re_cleanparens.sub('',s)
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   511
    n = 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   512
    for c in s:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   513
        if c=='(': n+=1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   514
        else:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   515
            n -= 1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   516
            if n<0: return 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   517
    return not n and 1 or 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   518
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   519
class PDFString:
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   520
    def __init__(self, str, escape=1):
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   521
        # might need to change this to class for encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   522
        self.s = str
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   523
        self.escape = escape
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   524
    def format(self, document):
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   525
        s = self.s
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   526
        escape = self.escape
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   527
        if not isinstance(document.encrypt,NoEncryption):
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   528
            s = document.encrypt.encode(s)
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   529
            escape = 1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   530
        if escape:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   531
            try:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   532
                es = "(%s)" % pdfutils._escape(s)
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   533
            except:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   534
                raise ValueError("cannot escape %s %s" % (s, repr(s)))
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   535
            if escape&2:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   536
                es = es.replace('\\012','\n')
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   537
            if escape&4 and _isbalanced(s): 
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   538
                es = es.replace('\\(','(').replace('\\)',')')
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   539
            return es
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   540
        else:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   541
            return '(%s)' % s
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   542
    def __str__(self):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   543
        return "(%s)" % pdfutils._escape(self.s)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   544
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   545
def PDFName(data):
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   546
    # might need to change this to class for encryption
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   547
    #  NOTE: RESULT MUST ALWAYS SUPPORT MEANINGFUL COMPARISONS (EQUALITY) AND HASH
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   548
    # first convert the name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   549
    ldata = list(data)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   550
    index = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   551
    for thischar in data:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   552
        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
   553
            pass # no problemo
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   554
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   555
            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
   556
            ldata[index] = "#"+hexord
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   557
        index = index+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   558
    data = string.join(ldata, "")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   559
    return "/%s" % data
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   560
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   561
class PDFDictionary:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   562
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   563
    multiline = LongFormat
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   564
    def __init__(self, dict=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   565
        """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
   566
        if dict is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   567
            self.dict = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   568
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   569
            self.dict = dict.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   570
    def __setitem__(self, name, value):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   571
        self.dict[name] = value
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   572
    def Reference(name, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   573
        ob = self.dict[name]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   574
        self.dict[name] = document.Reference(ob)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   575
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   576
        dict = self.dict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   577
        keys = dict.keys()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   578
        keys.sort()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   579
        L = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   580
        a = L.append
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   581
        for k in keys:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   582
            v = dict[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   583
            fv = format(v, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   584
            fk = format(PDFName(k), document)
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   585
            #a(fk)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   586
            #a(" "+fv)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   587
            a(fk + " " + fv)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   588
        #L = map(str, L)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   589
        if self.multiline:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   590
            Lj = string.join(L, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   591
            Lj = indent(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   592
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   593
            Lj = L
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   594
            # break up every 6 elements anyway
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   595
            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
   596
                Lj.insert(i,LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   597
            Lj = string.join(L, " ")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   598
        return "<< %s >>" % Lj
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   599
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   600
# stream filters are objects to support round trip and
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   601
# possibly in the future also support parameters
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   602
class PDFStreamFilterZCompress:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   603
    pdfname = "FlateDecode"
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   604
    def encode(self, text):
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   605
        from reportlab.lib.utils import import_zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   606
        zlib = import_zlib()
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   607
        if not zlib: raise ImportError, "cannot z-compress zlib unavailable"
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   608
        return zlib.compress(text)
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   609
    def decode(self, encoded):
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   610
        from reportlab.lib.utils import import_zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   611
        zlib = import_zlib()
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   612
        if not zlib: raise ImportError, "cannot z-decompress zlib unavailable"
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   613
        return zlib.decompress(encoded)
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   614
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   615
# need only one of these, unless we implement parameters later
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   616
PDFZCompress = PDFStreamFilterZCompress()
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   617
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   618
class PDFStreamFilterBase85Encode:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   619
    pdfname = "ASCII85Decode"
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   620
    def encode(self, text):
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   621
        from pdfutils import _AsciiBase85Encode, _wrap
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   622
        return _wrap(_AsciiBase85Encode(text))
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   623
    def decode(self, text):
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   624
        from pdfutils import _AsciiBase85Decode
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   625
        return _AsciiBase85Decode(text)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   626
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   627
# need only one of these too
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   628
PDFBase85Encode = PDFStreamFilterBase85Encode()
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   629
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   630
STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   631
             "stream" # stream keyword
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   632
             "%(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
   633
             "%(content)s" # the content, with no lineend
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   634
             "endstream%(LINEEND)s" # the endstream keyword
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   635
             )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   636
class PDFStream:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   637
    '''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
   638
    ### compression stuff not implemented yet
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   639
    __RefOnly__ = 1 # must be at top level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   640
    def __init__(self, dictionary=None, content=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   641
        if dictionary is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   642
            dictionary = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   643
        self.dictionary = dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   644
        self.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   645
        self.filters = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   646
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   647
        dictionary = self.dictionary
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   648
        # copy it for modification
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   649
        dictionary = PDFDictionary(dictionary.dict.copy())
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   650
        content = self.content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   651
        filters = self.filters
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   652
        if self.content is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   653
            raise ValueError, "stream content not set"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   654
        if filters is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   655
            filters = document.defaultStreamFilters
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   656
        # only apply filters if they haven't been applied elsewhere
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   657
        if filters is not None and not dictionary.dict.has_key("Filter"):
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   658
            # apply filters in reverse order listed
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   659
            rf = list(filters)
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   660
            rf.reverse()
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   661
            fnames = []
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   662
            for f in rf:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   663
                #print "*****************content:"; print repr(content[:200])
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   664
                #print "*****************filter", f.pdfname
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   665
                content = f.encode(content)
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   666
                fnames.insert(0, PDFName(f.pdfname))
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   667
            #print "*****************finally:"; print content[:200]
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   668
            #print "****** FILTERS", fnames
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   669
            #stop
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   670
            dictionary["Filter"] = PDFArray(fnames)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   671
        # "stream encoding is done after all filters have been applied"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   672
        content = document.encrypt.encode(content)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   673
        fc = format(content, document)
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   674
        #print "type(content)", type(content), len(content), type(self.dictionary)
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   675
        lc = len(content)
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   676
        #if fc!=content: burp
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   677
        # set dictionary length parameter
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   678
        dictionary["Length"] = lc
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   679
        fd = format(dictionary, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   680
        sdict = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   681
        sdict["dictionary"] = fd
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   682
        sdict["content"] = fc
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   683
        return STREAMFMT % sdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   684
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   685
def teststream(content=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   686
    #content = "" # test
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   687
    if content is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   688
        content = teststreamcontent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   689
    content = string.strip(content)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   690
    content = string.replace(content, "\n", LINEEND) + LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   691
    S = PDFStream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   692
    S.content = content
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   693
    S.filters = [PDFBase85Encode, PDFZCompress]
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   694
    # nothing else needed...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   695
    S.__Comment__ = "test stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   696
    return S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   697
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   698
teststreamcontent = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   699
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
   700
1.00 0.00 1.00 rg
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   701
n 72.00 72.00 432.00 648.00 re B*
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   702
"""
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   703
class PDFArray:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   704
    multiline = LongFormat
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   705
    def __init__(self, sequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   706
        self.sequence = list(sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   707
    def References(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   708
        """make all objects in sequence references"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   709
        self.sequence = map(document.Reference, self.sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   710
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   711
        #ssequence = map(str, self.sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   712
        sequence = self.sequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   713
        fsequence = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   714
        for elt in sequence:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   715
            felt = format(elt, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   716
            fsequence.append(felt)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   717
        if self.multiline:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   718
            Lj = string.join(fsequence, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   719
            Lj = indent(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   720
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   721
            # break up every 10 elements anyway
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   722
            Lj = fsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   723
            breakline = LINEEND+" "
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   724
            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
   725
                Lj.insert(i,breakline)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   726
            Lj = string.join(Lj)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   727
        return "[ %s ]" % Lj
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   728
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   729
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
   730
                 "%(content)s" "%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   731
                 "endobj" "%(LINEEND)s")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   732
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   733
class PDFIndirectObject:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   734
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   735
    def __init__(self, name, content):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   736
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   737
        self.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   738
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   739
        name = self.name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   740
        (n, v) = document.idToObjectNumberAndVersion[name]
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   741
        # set encryption parameters
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   742
        document.encrypt.register(n, v)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   743
        content = self.content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   744
        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
   745
        sdict = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   746
        sdict["n"] = n
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   747
        sdict["v"] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   748
        sdict["content"] = fcontent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   749
        return INDIRECTOBFMT % sdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   750
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   751
class PDFObjectReference:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   752
    def __init__(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   753
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   754
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   755
        name = self.name
1317
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   756
        try:
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   757
            (n, v) = document.idToObjectNumberAndVersion[name]
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   758
        except:
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   759
            raise KeyError, "forward reference to %s not resolved upon final formatting" % repr(name)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   760
        return "%s %s R" % (n,v)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   761
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   762
### chapter 5
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   763
# Following Ken Lunde's advice and the PDF spec, this includes
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   764
# some high-order bytes.  I chose the characters for Tokyo
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   765
# in Shift-JIS encoding, as these cannot be mistaken for
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   766
# any other encoding, and we'll be able to tell if something
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   767
# has run our PDF files through a dodgy Unicode conversion.
1063
378a0f553608 cosmetics
aaron_watters
parents: 1062
diff changeset
   768
PDFHeader = (
378a0f553608 cosmetics
aaron_watters
parents: 1062
diff changeset
   769
"%PDF-1.3"+LINEEND+
378a0f553608 cosmetics
aaron_watters
parents: 1062
diff changeset
   770
"%\223\214\213\236 ReportLab Generated PDF document http://www.reportlab.com"+LINEEND)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   771
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   772
class PDFFile:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   773
    ### 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
   774
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   775
        self.strings = []
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   776
        self.write = self.strings.append
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   777
        self.offset = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   778
        self.add(PDFHeader)
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   779
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   780
    def closeOrReset(self):
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   781
        pass
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   782
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   783
    def add(self, s):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   784
        """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
   785
        result = self.offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   786
        self.offset = result+len(s)
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   787
        self.write(s)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   788
        return result
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   789
    def format(self, document):
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   790
        strings = map(str, self.strings) # final conversion, in case of lazy objects
2359
d7991ae46607 pdfdoc: fix rare bug (exposed in tinyrml)
rgbecker
parents: 2332
diff changeset
   791
        return string.join(strings, "")
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   792
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   793
XREFFMT = '%0.10d %0.5d n'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   794
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   795
class PDFCrossReferenceSubsection:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   796
    def __init__(self, firstentrynumber, idsequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   797
        self.firstentrynumber = firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   798
        self.idsequence = idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   799
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   800
        """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
   801
        firstentrynumber = self.firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   802
        idsequence = self.idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   803
        entries = list(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   804
        nentries = len(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   805
        # 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
   806
        taken = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   807
        if firstentrynumber==0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   808
            taken[0] = "standard free entry"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   809
            nentries = nentries+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   810
            entries.insert(0, "0000000000 65535 f")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   811
        idToNV = document.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   812
        idToOffset = document.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   813
        lastentrynumber = firstentrynumber+nentries-1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   814
        for id in idsequence:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   815
            (num, version) = idToNV[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   816
            if taken.has_key(num):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   817
                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
   818
            if num>lastentrynumber or num<firstentrynumber:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   819
                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
   820
            # compute position in list
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   821
            rnum = num-firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   822
            taken[num] = id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   823
            offset = idToOffset[id]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   824
            entries[num] = XREFFMT % (offset, version)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   825
        # now add the initial line
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   826
        firstline = "%s %s" % (firstentrynumber, nentries)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   827
        entries.insert(0, firstline)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   828
        # make sure it ends with a LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   829
        entries.append("")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   830
        if LINEEND=="\n" or LINEEND=="\r":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   831
            reflineend = " "+LINEEND # as per spec
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   832
        elif LINEEND=="\r\n":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   833
            reflineend = LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   834
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   835
            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
   836
        return string.join(entries, LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   837
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   838
class PDFCrossReferenceTable:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   839
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   840
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   841
        self.sections = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   842
    def addsection(self, firstentry, ids):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   843
        section = PDFCrossReferenceSubsection(firstentry, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   844
        self.sections.append(section)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   845
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   846
        sections = self.sections
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   847
        if not sections:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   848
            raise ValueError, "no crossref sections"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   849
        L = ["xref"+LINEEND]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   850
        for s in self.sections:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   851
            fs = format(s, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   852
            L.append(fs)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   853
        return string.join(L, "")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   854
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   855
TRAILERFMT = ("trailer%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   856
              "%(dict)s%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   857
              "startxref%(LINEEND)s"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   858
              "%(startxref)s%(LINEEND)s"
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   859
              "%(PERCENT)s%(PERCENT)sEOF%(LINEEND)s")
481
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
class PDFTrailer:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   862
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   863
    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
   864
        self.startxref = startxref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   865
        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
   866
            raise ValueError, "Size and Root keys required"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   867
        dict = self.dict = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   868
        for (n,v) in [("Size", Size), ("Prev", Prev), ("Root", Root),
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   869
                      ("Info", Info), ("ID", ID), ("Encrypt", Encrypt)]:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   870
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   871
                dict[n] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   872
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   873
        fdict = format(self.dict, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   874
        D = LINEENDDICT.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   875
        D["dict"] = fdict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   876
        D["startxref"] = self.startxref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   877
        return TRAILERFMT % D
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   878
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   879
#### XXXX skipping incremental update,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   880
#### encryption
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   881
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   882
#### chapter 6, doc structure
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   883
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   884
class PDFCatalog:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   885
    __Comment__ = "Document Root"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   886
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   887
    # to override, set as attributes
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   888
    __Defaults__ = {"Type": PDFName("Catalog"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   889
                "PageMode": PDFName("UseNone"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   890
                }
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   891
    __NoDefault__ = string.split("""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   892
        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
   893
        ViewerPreferences PageLabels PageLayout JavaScript StructTreeRoot SpiderInfo"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   894
                                 )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   895
    __Refs__ = __NoDefault__ # make these all into references, if present
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   896
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   897
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   898
        self.check_format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   899
        defaults = self.__Defaults__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   900
        Refs = self.__Refs__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   901
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   902
        for k in defaults.keys():
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   903
            default = defaults[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   904
            v = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   905
            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
   906
                v = getattr(self, k)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   907
            elif default is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   908
                v = default
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   909
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   910
                D[k] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   911
        for k in self.__NoDefault__:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   912
            if hasattr(self, k):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   913
                v = getattr(self,k)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   914
                if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   915
                    D[k] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   916
        # force objects to be references where required
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   917
        for k in Refs:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   918
            if D.has_key(k):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   919
                #print"k is", k, "value", D[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   920
                D[k] = document.Reference(D[k])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   921
        dict = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   922
        return format(dict, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   923
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   924
    def showOutline(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   925
        self.PageMode = PDFName("UseOutlines")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   926
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   927
    def showFullScreen(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   928
        self.PageMode = PDFName("FullScreen")
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   929
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   930
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   931
        """for use in subclasses"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   932
        pass
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   933
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   934
# not yet implementing
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   935
#  ViewerPreferences, PageLabelDictionaries,
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
class PDFPages(PDFCatalog):
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2200
diff changeset
   938
    """PAGES TREE WITH ONE INTERNAL NODE, FOR "BALANCING" CHANGE IMPLEMENTATION"""
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   939
    __Comment__ = "page tree"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   940
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   941
    # note: could implement page attribute inheritance...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   942
    __Defaults__ = {"Type": PDFName("Pages"),
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
    __NoDefault__ = string.split("Kids Count Parent")
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   945
    __Refs__ = ["Parent"]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   946
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   947
        self.pages = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   948
    def __getitem__(self, item):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   949
        return self.pages[item]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   950
    def addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   951
        self.pages.append(page)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   952
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   953
        # convert all pages to page references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   954
        pages = self.pages
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2200
diff changeset
   955
        kids = PDFArray(pages)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   956
        # make sure all pages are references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   957
        kids.References(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   958
        self.Kids = kids
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   959
        self.Count = len(pages)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   960
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   961
class PDFPage(PDFCatalog):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   962
    __Comment__ = "Page dictionary"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   963
    # all PDF attributes can be set explicitly
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   964
    # 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
   965
    Override_default_compilation = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   966
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   967
    __Defaults__ = {"Type": PDFName("Page"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   968
                   # "Parent": PDFObjectReference(Pages),  # no! use document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   969
                    }
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   970
    __NoDefault__ = string.split(""" Parent
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   971
        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
   972
        PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ
500
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
   973
        Trans
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   974
    """)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   975
    __Refs__ = string.split("""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   976
        Contents Parent ID
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   977
    """)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   978
    pagewidth = 595
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   979
    pageheight = 842
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   980
    stream = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   981
    hasImages = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   982
    compression = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   983
    XObjects = None
500
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
   984
    Trans = None
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   985
    # transitionstring?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   986
    # xobjects?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   987
    # annotations
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   988
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   989
        # set all nodefaults to None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   990
        for name in self.__NoDefault__:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   991
            setattr(self, name, None)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   992
    def setCompression(self, onoff):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   993
        self.compression = onoff
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   994
    def setStream(self, code):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   995
        if self.Override_default_compilation:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   996
            raise ValueError, "overridden! must set stream explicitly"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   997
        from types import ListType
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   998
        if type(code) is ListType:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   999
            code = string.join(code, LINEEND)+LINEEND
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1000
        self.stream = code
500
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1001
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1002
    def setPageTransition(self, tranDict):
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1003
        self.Trans = PDFDictionary(tranDict)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1004
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1005
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1006
        # 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
  1007
        if self.Override_default_compilation:
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1008
            return
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1009
        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
  1010
        if not self.Annots:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1011
            self.Annots = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1012
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1013
            #print self.Annots
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1014
            #raise ValueError, "annotations not reimplemented yet"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1015
            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
  1016
                self.Annots = PDFArray(self.Annots)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1017
        if not self.Contents:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1018
            stream = self.stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1019
            if not stream:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1020
                self.Contents = teststream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1021
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1022
                S = PDFStream()
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
  1023
                if self.compression:
1679
e77be1edcd61 Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents: 1677
diff changeset
  1024
                    S.filters = [PDFBase85Encode, PDFZCompress]
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1025
                S.content = stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1026
                S.__Comment__ = "page stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1027
                self.Contents = S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1028
        if not self.Resources:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1029
            resources = PDFResourceDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1030
            # fonts!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1031
            resources.basicFonts()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1032
            if self.hasImages:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1033
                resources.allProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1034
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1035
                resources.basicProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1036
            if self.XObjects:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1037
                #print "XObjects", self.XObjects.dict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1038
                resources.XObject = self.XObjects
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1039
            self.Resources = resources
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1040
        if not self.Parent:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1041
            pages = document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1042
            self.Parent = document.Reference(pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1043
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1044
def testpage(document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1045
    P = PDFPage()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1046
    P.Contents = teststream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1047
    pages = document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1048
    P.Parent = document.Reference(pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1049
    P.MediaBox = PDFArray([0, 0, 595, 841])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1050
    resources = PDFResourceDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1051
    resources.allProcs() # enable all procsets
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1052
    resources.basicFonts()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1053
    P.Resources = resources
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1054
    pages.addPage(P)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1055
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
  1056
#### DUMMY OUTLINES IMPLEMENTATION FOR testing
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1057
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1058
DUMMYOUTLINE = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1059
<<
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1060
  /Count
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1061
      0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1062
  /Type
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1063
      /Outlines
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1064
>>"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1065
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
  1066
class PDFOutlines0:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1067
    __Comment__ = "TEST OUTLINE!"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1068
    text = string.replace(DUMMYOUTLINE, "\n", LINEEND)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1069
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1070
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1071
        return self.text
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1072
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1073
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1074
class OutlineEntryObject:
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1075
    "an entry in an outline"
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1076
    Title = Dest = Parent = Prev = Next = First = Last = Count = None
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1077
    def format(self, document):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1078
        D = {}
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1079
        D["Title"] = PDFString(self.Title)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1080
        D["Parent"] = self.Parent
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1081
        D["Dest"] = self.Dest
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1082
        for n in ("Prev", "Next", "First", "Last", "Count"):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1083
            v = getattr(self, n)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1084
            if v is not None:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1085
                D[n] = v
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1086
        PD = PDFDictionary(D)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1087
        return PD.format(document)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1088
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1089
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1090
class PDFOutlines:
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1091
    """takes a recursive list of outline destinations
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1092
       like
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1093
           out = PDFOutline1()
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1094
           out.setNames(canvas, # requires canvas for name resolution
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1095
             "chapter1dest",
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1096
             ("chapter2dest",
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1097
              ["chapter2section1dest",
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1098
               "chapter2section2dest",
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1099
               "chapter2conclusiondest"]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1100
             ), # end of chapter2 description
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1101
             "chapter3dest",
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1102
             ("chapter4dest", ["c4s1", "c4s2"])
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1103
             )
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1104
       Higher layers may build this structure incrementally. KISS at base level.
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1105
    """
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1106
    # first attempt, many possible features missing.
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1107
    #no init for now
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1108
    mydestinations = ready = None
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1109
    counter = 0
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1110
    currentlevel = -1 # ie, no levels yet
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1111
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1112
    def __init__(self):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1113
        self.destinationnamestotitles = {}
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1114
        self.destinationstotitles = {}
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1115
        self.levelstack = []
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1116
        self.buildtree = []
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1117
        self.closedict = {} # dictionary of "closed" destinations in the outline
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
  1118
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1119
    def addOutlineEntry(self, destinationname, level=0, title=None, closed=None):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1120
        """destinationname of None means "close the tree" """
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1121
        from types import IntType, TupleType
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1122
        if destinationname is None and level!=0:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1123
            raise ValueError, "close tree must have level of 0"
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1124
        if type(level) is not IntType: raise ValueError, "level must be integer, got %s" % type(level)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1125
        if level<0: raise ValueError, "negative levels not allowed"
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1126
        if title is None: title = destinationname
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1127
        currentlevel = self.currentlevel
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1128
        stack = self.levelstack
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1129
        tree = self.buildtree
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1130
        # adjust currentlevel and stack to match level
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1131
        if level>currentlevel:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1132
            if level>currentlevel+1:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1133
                raise ValueError, "can't jump from outline level %s to level %s, need intermediates" %(currentlevel, level)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1134
            level = currentlevel = currentlevel+1
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1135
            stack.append([])
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1136
        while level<currentlevel:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1137
            # pop off levels to match
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1138
            current = stack[-1]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1139
            del stack[-1]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1140
            previous = stack[-1]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1141
            lastinprevious = previous[-1]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1142
            if type(lastinprevious) is TupleType:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1143
                (name, sectionlist) = lastinprevious
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1144
                raise ValueError, "cannot reset existing sections: " + repr(lastinprevious)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1145
            else:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1146
                name = lastinprevious
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1147
                sectionlist = current
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1148
                previous[-1] = (name, sectionlist)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1149
            #sectionlist.append(current)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1150
            currentlevel = currentlevel-1
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1151
        if destinationname is None: return
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1152
        stack[-1].append(destinationname)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1153
        self.destinationnamestotitles[destinationname] = title
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1154
        if closed: self.closedict[destinationname] = 1
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1155
        self.currentlevel = level
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1156
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1157
    def setDestinations(self, destinationtree):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1158
        self.mydestinations = destinationtree
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1159
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1160
    def format(self, document):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1161
        D = {}
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1162
        D["Type"] = PDFName("Outlines")
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1163
        c = self.count
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1164
        D["Count"] = c
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1165
        if c!=0:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1166
            D["First"] = self.first
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1167
            D["Last"] = self.last
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1168
        PD = PDFDictionary(D)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1169
        return PD.format(document)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1170
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1171
    def setNames(self, canvas, *nametree):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1172
        desttree = self.translateNames(canvas, nametree)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1173
        self.setDestinations(desttree)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1174
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1175
    def setNameList(self, canvas, nametree):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1176
        "Explicit list so I don't need to do apply(...) in the caller"
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1177
        desttree = self.translateNames(canvas, nametree)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1178
        self.setDestinations(desttree)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1179
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1180
    def translateNames(self, canvas, object):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1181
        "recursively translate tree of names into tree of destinations"
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1182
        from types import StringType, ListType, TupleType
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1183
        Ot = type(object)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1184
        destinationnamestotitles = self.destinationnamestotitles
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1185
        destinationstotitles = self.destinationstotitles
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1186
        closedict = self.closedict
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1187
        if Ot is StringType:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1188
            destination = canvas._bookmarkReference(object)
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1189
            title = object
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1190
            if destinationnamestotitles.has_key(object):
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1191
                title = destinationnamestotitles[object]
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1192
            else:
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1193
                destinationnamestotitles[title] = title
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1194
            destinationstotitles[destination] = title
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
  1195
            if closedict.has_key(object):
934a4f24ea5d Added explicit font and encoding support
andy_robins&