complete revision of pdfdoc. Not finished (compression missing, testing needed)
authoraaron_watters
Wed, 18 Oct 2000 05:03:21 +0000
changeset 481 136669babedf
parent 480 bf0f7c5c4d13
child 482 15d1ca6de00f
complete revision of pdfdoc. Not finished (compression missing, testing needed) I got Robin's last change in at the last moment :)
reportlab/pdfbase/pdfdoc.py
reportlab/pdfgen/canvas.py
--- a/reportlab/pdfbase/pdfdoc.py	Sun Oct 15 22:02:57 2000 +0000
+++ b/reportlab/pdfbase/pdfdoc.py	Wed Oct 18 05:03:21 2000 +0000
@@ -31,8 +31,9 @@
 #
 ###############################################################################
 #	$Log: pdfdoc.py,v $
-#	Revision 1.25  2000/10/15 21:57:12  andy_robinson
-#	Added showFullScreen0
+#	Revision 1.26  2000/10/18 05:03:21  aaron_watters
+#	complete revision of pdfdoc.  Not finished (compression missing, testing needed)
+#	I got Robin's last change in at the last moment :)
 #
 #	Revision 1.24  2000/09/08 10:04:08  rgbecker
 #	Paul Eddington's unix tell() returns a LongIntType bugfix
@@ -98,7 +99,7 @@
 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
 #	Added license, __version__ and Logi comment
 #	
-__version__=''' $Id: pdfdoc.py,v 1.25 2000/10/15 21:57:12 andy_robinson Exp $ '''
+__version__=''' $Id: pdfdoc.py,v 1.26 2000/10/18 05:03:21 aaron_watters Exp $ '''
 __doc__=""" 
 PDFgen is a library to generate PDF files containing text and graphics.  It is the 
 foundation for a complete reporting solution in Python.  
@@ -112,38 +113,12 @@
 
 2000-10-13 gmcm Packagize
 """
-
-import os
-import sys
-import string
-import time
-import tempfile
-import cStringIO
-from types import IntType, TupleType, StringType, ListType, DictType
-from math import sin, cos, pi, ceil
-
-try:
-    import zlib
-    _HAVE_ZLIB = 1
-except:
-    print "zlib not available, page compression not available"
-    _HAVE_ZLIB = 0
-
-
-from reportlab.pdfgen.pdfgeom import bezierArc
-
-from reportlab.pdfbase import pdfutils
-from reportlab.pdfbase.pdfutils import LINEEND   # this constant needed in both
-from reportlab.pdfbase import pdfmetrics
-##############################################################
-#
-#            Constants and declarations
-#
-##############################################################
+"""extremely anally  retentive structured version of pdfdoc"""
 
 DEFAULT_ENCODING = 'WinAnsiEncoding' #hack here for a system wide change
 ALLOWED_ENCODINGS = ('WinAnsiEncoding', 'MacRomanEncoding')
 
+PDFError = 'PDFError'
 
 StandardEnglishFonts = [
     'Courier', 'Courier-Bold', 'Courier-Oblique', 'Courier-BoldOblique',  
@@ -152,125 +127,178 @@
     'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic',
     'Symbol','ZapfDingbats']
 
-PDFError = 'PDFError'
-AFMDIR = '.'
+# set this flag to get more vertical whitespace (and larger files)
+LongFormat = 1
+
+# XXXX stream filters need to be added
+
+# __InternalName__ is a special attribute that can only be set by the Document arbitrator
+__InternalName__ = "__InternalName__"
+
+# __RefOnly__ marks reference only elements that must be formatted on top level
+__RefOnly__ = "__RefOnly__"
+
+# __Comment__ provides a (one line) comment to inline with an object ref, if present
+#   if it is more than one line then percentize it...
+__Comment__ = "__Comment__"
+DoComments = 1
+
+# name for standard font dictionary
+BasicFonts = "BasicFonts"
+
+# name for the pages object
+Pages = "Pages"
+
+### generic utilities
+
+import string, types
+from reportlab.pdfbase import pdfutils
+from reportlab.pdfbase.pdfutils import LINEEND   # this constant needed in both
+
+# for % substitutions
+LINEENDDICT = {"LINEEND": LINEEND, "PERCENT": "%"}
 
-A4 = (595.27,841.89)   #default page size
+def markfilename(filename):
+	# with the Mac, we need to tag the file in a special
+	#way so the system knows it is a PDF file.
+	#This supplied by Joe Strout
+	import os
+	if os.name == 'mac':
+		import macfs
+		try: 
+			macfs.FSSpec(filename).SetCreatorType('CARO','PDF ')
+		except:
+			pass
+
+def format(element, document, toplevel=0):
+    """Indirection step for formatting.
+       Ensures that document parameters alter behaviour
+       of formatting for all elements.
+    """
+    from types import InstanceType
+    if type(element) is InstanceType:
+        if not toplevel and hasattr(element, __RefOnly__):
+            # the object cannot be a component at non top level.
+            # make a reference to it and return it's format
+            R = document.Reference(element)
+            return R.format(document)
+        else:
+            try:
+                fmt = element.format
+            except:
+                raise AttributeError, "%s has no format operation" % element
+            f = fmt(document)
+            if DoComments and hasattr(element, __Comment__):
+                f = "%s%s%s%s" % ("% ", element.__Comment__, LINEEND, f)
+            return f
+    else:
+        return str(element)
+
+def indent(s, IND=LINEEND+" "):
+    return string.replace(s, LINEEND, IND)
+
+### the global document structure manager
 
 class PDFDocument:
-    """Responsible for linking and writing out the whole document.
-    Builds up a list of objects using add(key, object).  Each of these
-    must inherit from PDFObject and be able to write itself into the file.
-    For cross-linking, it provides getPosition(key) which tells you where
-    another object is, or raises a KeyError if not found.  The rule is that
-    objects should only refer ones previously written to file.
-    """
+    objectcounter = 0
+    inObject = None
+    # set this to define filters 
+    defaultStreamFilters = None
+    pageCounter = 1
     def __init__(self, encoding=DEFAULT_ENCODING):
-        self.objects = []
-        self.objectPositions = {}
-        # named object references (may be unbound)
-        self.objectReferences = {}
-        self.inObject = None # mark for whether in page or form or possibly others...
-        self.thispageposition = None # record if in page
-        self.thisformposition = None # record if in form
-
-        #check encoding legal - they can omit the suffix 'Encoding
-        if encoding[-8:] <> 'Encoding':
-            encoding = encoding + 'Encoding'
-        assert encoding in ALLOWED_ENCODINGS, \
-               "Unknown encoding %s. Allowed values are %s" % (
-                encoding, ALLOWED_ENCODINGS)
-                        
-        self.fonts = MakeType1Fonts(encoding)
-        self.encoding = encoding	#hack to record the font encoding only used by canvas.stringWidth
+        self.encoding = encoding
+        # mapping of internal identifier ("Page001") to PDF objectnumber and generation number (34, 0)
+        self.idToObjectNumberAndVersion = {}
+        # mapping of internal identifier ("Page001") to PDF object (PDFPage instance)
+        self.idToObject = {}
+        # internal id to file location
+        self.idToOffset = {}
+        # number to id
+        self.numberToId = {}
+        cat = self.Catalog = self._catalog = PDFCatalog()
+        pages = self.Pages = PDFPages()
+        cat.Pages = pages
+        outlines = self.Outlines = self.outline = PDFOutlines()
+        cat.Outlines = outlines
+        self.info = self.Info = PDFInfo()
+        self.Reference(self.Catalog)
+        self.Reference(self.Info)
+        # make std fonts (this could be made optional
+        self.fontMapping = {}
+        MakeStandardEnglishFontObjects(self, encoding)
 
-        #mapping of Postscriptfont names to internal ones;
-        #needs to be dynamically built once we start adding
-        #fonts in.
-        self.fontMapping = {}
-        for i in range(len(StandardEnglishFonts)):
-            psname = StandardEnglishFonts[i]
-            pdfname = '/F%d' % (i+1)
-            self.fontMapping[psname] = pdfname
-        
-            
-        self.pages = []
-        self.pagepositions = []
+    def SaveToFile(self, filename, canvas):
+        from types import StringType
+        # prepare outline
+        outline = self.outline
+        outline.prepare(self, canvas)
+        if type(filename) is StringType:
+            myfile = 1
+            f = open(filename, "wb")
+        else:
+            myfile = 0
+            f = filename # IT BETTER BE A FILE-LIKE OBJECT!
+        txt = self.format()
+        f.write(txt)
+        if myfile:
+            f.close()
+            markfilename(filename) # do platform specific file junk
         
-        # position 1
-        cat = PDFCatalog()
-        self._catalog = cat
-        cat.RefPages = 3
-        cat.RefOutlines = 2
-        self.add('Catalog', cat)
-    
-        # position 2 - outlines
-        outl = PDFOutline()
-        self.outline = outl
-        self.add('Outline', outl)
-    
-        # position 3 - pages collection
-        self.PageCol = PDFPageCollection()
-        self.add('PagesTreeRoot',self.PageCol)
+    def inPage(self):
+        """specify the current object as a page (enables reference binding and other page features)"""
+        if self.inObject is not None:
+            if self.inObject=="page": return
+            raise ValueError, "can't go in page already in object %s" % self.inObject
+        self.inObject = "page"
+
+    def inForm(self):
+        """specify that we are in a form xobject (disable page features, etc)"""
+        if self.inObject not in ["form", None]:
+            raise ValueError, "can't go in form already in object %s" % self.inObject
+        self.inObject = "form"
+        # don't need to do anything else, I think...        
+
+    def getInternalFontName(self, psfontname):
+        fm = self.fontMapping
+        if fm.has_key(psfontname):
+            return fm[psfontname]
+        else:
+            raise PDFError, "Font %s not available in document" % repr(psfontname)
+
+    def thisPageName(self):
+        return "Page"+repr(self.pageCounter)
+
+    def thisPageRef(self):
+        return PDFObjectReference(self.thisPageName())
+
+    def addPage(self, page):
+        name = self.thisPageName()
+        self.Reference(page, name)
+        self.Pages.addPage(page)
+        self.pageCounter = self.pageCounter+1
+        self.inObject = None
+
+    def formName(self, externalname):
+        return "FormXob.%s" % externalname
     
-        # positions 4-16 - fonts
-        fontstartpos = len(self.objects) + 1
-        for font in self.fonts:
-            self.add('Font.'+font.keyname, font)
-        # make font dict as a shared indirect object reference
-        #self.fontdict = MakeFontDictionary(fontstartpos, len(self.fonts))
-        fontdicttext = MakeFontDictionary(fontstartpos, len(self.fonts))
-        fontdictob = PDFLiteral(fontdicttext)
-        self.add("FontDictionary", fontdictob)
-        self.fontdict = self.objectReference("FontDictionary")
-        
-        # position 17 - Info
-        self.info = PDFInfo()  #hang onto it!
-        self.add('Info', self.info)
-        self.infopos = len(self.objects)  #1-based, this gives its position
+    def addForm(self, name, form):
+        """add a Form XObject."""
+        # XXX should check that name is a legal PDF name
+        if self.inObject != "form":
+            self.inForm()
+        self.Reference(form, self.formName(name))
+        self.inObject = None
+
+    def annotationName(self, externalname):
+        return "Annot.%s"%externalname
     
-    
-    def add(self, key, obj):
-        #self.objectPositions[key] = len(self.objects)  # its position
-        self.objectPositions[key] = p = len(self.objects)+1 # its position
-        self.setObjectReference(key, p)
-        self.objects.append(obj)
-        #obj.doc = self
-        #return len(self.objects) - 1  # give its position
-        return self.getPosition(key)
+    def addAnnotation(self, name, annotation):
+        self.Reference(annotation, self.annotationName(name))
         
-    def replace(self, key, obj):
-        """replace an object (but you better know what you are doing)"""
-        position = self.getPosition(key)
-        #self.setObjectReference(key, position)
-        self.objects[position-1] = obj
-        return position
+    def refAnnotation(self, name):
+        internalname = self.annotationName(name)
+        return PDFObjectReference(internalname)
         
-    def reserve(self, key):
-        """reserve an object position later to be replaced (no replace == error)"""
-        return self.add(key, key)
-
-    def getPosition(self, key):
-        """Tell you where the given object is in the file - used for
-        cross-linking; an object can call self.doc.getPosition("Page001")
-        to find out where the object keyed under "Page001" is stored."""
-        return self.objectPositions[key]
-        
-    def setObjectReference(self, key, position):
-        ref = self.objectReference(key)
-        ref.bind("%s 0 R" % position)
-        
-    def objectReference(self, key):
-        "get a (lazy) object ref"
-        #return "%s 0 R" % self.getPosition(key)
-        refs = self.objectReferences
-        try:
-            test = refs[key]
-        except:
-            refs[key] = test = BindableStr(key)
-        return test
-        
-    
     def setTitle(self, title):
         "embeds in PDF file"
         self.info.title = title
@@ -282,296 +310,834 @@
     def setSubject(self, subject):
         "embeds in PDF file"
         self.info.subject = subject
-            
-    
-    def printXref(self):
-        self.startxref = sys.stdout.tell()
-        print 'xref'
-        print 0,len(self.objects) + 1
-        print '0000000000 65535 f'
-        for pos in self.xref:
-            print '%0.10d 00000 n' % pos
-
-    def writeXref(self, f):
-        self.startxref = f.tell()
-        f.write('xref' + LINEEND)
-        f.write('0 %d' % (len(self.objects) + 1) + LINEEND)
-        f.write('0000000000 65535 f' + LINEEND)
-        for pos in self.xref:
-            f.write('%0.10d 00000 n' % pos + LINEEND)
-    
-    def printTrailer(self):
-        print 'trailer'
-        print '<< /Size %d /Root %d 0 R /Info %d 0 R>>' % (len(self.objects) + 1, 1, self.infopos)
-        print 'startxref'
-        print self.startxref
-
-    def writeTrailer(self, f):
-        f.write('trailer' + LINEEND)
-        f.write('<< /Size %d /Root %d 0 R /Info %d 0 R>>' % (len(self.objects) + 1, 1, self.infopos)  + LINEEND)
-        f.write('startxref' + LINEEND)
-        f.write(('%d' % self.startxref) + LINEEND)
-
-    def SaveToFile(self, filename,canvas):
-        """Open a file, and ask each object in turn to write itself to
-        the file.  Keep track of the file position at each point for
-        use in the index at the end"""
-
-        if type(filename)==StringType:
-            f = open(filename, 'wb')
-        else:
-            f = filename #assume it's a file type object
-
-        i = 1
-        self.xref = []
-        f.write("%PDF-1.2" + LINEEND)  # for CID support
-        f.write("%\355\354\266\276" + LINEEND)
-        # do preprocessing as needed
-        # prepare outline
-        outline = self.outline
-        outline.prepare(self, canvas)
-        for obj in self.objects:
-            pos = f.tell()
-            self.xref.append(pos)
-            f.write(str(i) + ' 0 obj' + LINEEND)
-            obj.save(f)
-            f.write('endobj' + LINEEND)
-            i = i + 1
-        self.writeXref(f)
-        self.writeTrailer(f)
-        f.write('%%EOF')  # no lineend needed on this one!
-
-        if f is not filename: f.close()
-        # with the Mac, we need to tag the file in a special
-        #way so the system knows it is a PDF file.
-        #This supplied by Joe Strout
-        if os.name == 'mac':
-            import macfs
-            try: 
-                macfs.FSSpec(filename).SetCreatorType('CARO','PDF ')
-            except:
-                pass
-
-
-    def printPDF(self):
-        "prints it to standard output.  Logs positions for doing trailer"
-        print "%PDF-1.0"
-        print "%\355\354\266\276"
-        i = 1
-        self.xref = []
-        for obj in self.objects:
-            pos = sys.stdout.tell()
-            self.xref.append(pos)
-            print i, '0 obj'
-            obj.printPDF()
-            print 'endobj'
-            i = i + 1
-        self.printXref()
-        self.printTrailer()
-        print "%%EOF",
-        
-    def inPage(self):
-        """specify the current object as a page (enables reference binding and other page features)"""
-        if self.inObject is not None:
-            if self.inObject=="page": return
-            raise ValueError, "can't go in page already in object %s" % self.inObject
-        self.inObject = "page"
-        pagenum = len(self.PageCol.PageList)+1
-        pagename = "Page%06d" % pagenum
-        streamname = "PageStream%06d" % pagenum
-        pageposition = self.reserve(pagename)
-        streamposition = self.reserve(streamname)
-        self.pageposition = (pagenum, pagename, streamname, pageposition, streamposition)
-        
-    def thisPageRef(self):
-        if self.inObject!="page":
-            raise ValueError, "can't get thisPageRef -- not declared inPage"
-        (pagenum, pagename, streamname, pageposition, streamposition) = self.pageposition
-        return self.objectReference(pagename)
-        
-    def inForm(self):
-        """specify that we are in a form xobject (disable page features, etc)"""
-        if self.inObject not in ["form", None]:
-            raise ValueError, "can't go in form already in object %s" % self.inObject
-        self.inObject = "form"
-        # don't need to do anything else, I think...
-
-    def addPage(self, page):
-        """adds page and stream at end.  Maintains pages list"""
-        #page.buildstream()
-        if self.inObject != "page":
-            self.inPage()
-        (pagenum, pagename, streamname, pageposition, streamposition) = self.pageposition
-        pos = len(self.objects) # work out where added
-        
-        parentpos = page.ParentPos = self.getPosition("PagesTreeRoot")   #pages collection
-        page.info = {
-            'parentpos': parentpos,
-            'fontdict':self.fontdict,
-            'contentspos':streamposition,
-            }
-        
-        self.PageCol.PageList.append(pageposition)  
-        #pagenum = len(self.PageCol.PageList)
-        #self.add('Page%06d'% pagenum, page)
-        self.replace(pagename, page)
-        #self.objects.append(page)
-        #self.add('PageStream%06d'% pagenum, page.stream)
-        self.replace(streamname, page.stream)
-        #self.objects.append(page.stream)
-        # clear inObject
-        self.inObject = None
-        
-    def addForm(self, name, form):
-        """add a Form XObject."""
-        # XXX should check that name is a legal PDF name
-        if self.inObject != "form":
-            self.inForm()
-        form.info = {"fontdict": self.fontdict}
-        self.add("FormXob.%s" % name, form)
-        self.inObject = None
-        
-    def hasForm(self, name):
-        """test for existence of named form"""
-        internalname = "FormXob.%s" % name
-        try:
-            test = self.objectReference(internalname)            
-        except:
-            return 0
-        else:
-            return internalname
-        
-    def xobjDict(self, formnames):
-        """construct an xobject dict (for inclusion in a resource dict, usually)
-           from a list of form names (images not yet supported)"""
-        L = []
-        a = L.append
-        a("        <<")
-        for name in formnames:
-            internalname = "FormXob.%s" % name
-            reference = self.objectReference(internalname)
-            a("           /%s %s" % (internalname, reference))
-        a(">>")
-        a("")
-        return string.join(L, LINEEND)
-        
-    def addAnnotation(self, name, annotation):
-        self.add("Annot.%s"%name, annotation)
-        
-    def refAnnotation(self, name):
-        internalname = "Annot.%s" % name
-        return self.objectReference(internalname)
-
-    def hasFont(self, psfontname):
-        return self.fontMapping.has_key(psfontname)
-
-    def getInternalFontName(self, psfontname):
-        try:
-            return self.fontMapping[psfontname]
-        except:
-            raise PDFError, "Font %s not available in document" % psfontname
 
     def getAvailableFonts(self):
         fontnames = self.fontMapping.keys()
         fontnames.sort()
         return fontnames
     
-##############################################################
-#
-#            Utilities
-#
-##############################################################
+    def format(self):
+        # register the Catalog/INfo and then format the objects one by one until exhausted
+        # (possible infinite loop if there is a bug that continually makes new objects/refs...)
+        cat = self.Catalog
+        info = self.Info
+        self.Reference(self.Catalog)
+        self.Reference(self.Info)
+        # make std fonts (this could be made optional
+        counter = 0 # start at first object (object 1 after preincrement)
+        ids = [] # the collection of object ids in object number order
+        numbertoid = self.numberToId
+        idToNV = self.idToObjectNumberAndVersion
+        idToOb = self.idToObject
+        idToOf = self.idToOffset
+        ### note that new entries may be "appended" DURING FORMATTING
+        done = None
+        File = PDFFile() # output collector
+        while done is None:
+            counter = counter+1 # do next object...
+            if numbertoid.has_key(counter):
+                id = numbertoid[counter]
+                #printidToOb
+                obj = idToOb[id]
+                IO = PDFIndirectObject(id, obj)
+                IOf = IO.format(self)
+                # add a comment to the PDF output
+                if DoComments:
+                    File.add("%% %s %s %s" % (repr(id), repr(repr(obj)[:50]), LINEEND))
+                offset = File.add(IOf)
+                idToOf[id] = offset
+                ids.append(id)
+            else:
+                done = 1
+        # sanity checks (must happen AFTER formatting)
+        lno = len(numbertoid)
+        if counter-1!=lno:
+            raise ValueError, "counter %s doesn't match number to id dictionary %s" %(counter, lno)
+        # now add the xref
+        xref = PDFCrossReferenceTable()
+        xref.addsection(0, ids)
+        xreff = xref.format(self)
+        xrefoffset = File.add(xreff)
+        # now add the trailer
+        trailer = PDFTrailer(
+            startxref = xrefoffset,
+            Size = lno,
+            Root = self.Reference(cat),
+            Info = self.Reference(info)
+            )
+        trailerf = trailer.format(self)
+        File.add(trailerf)
+        # return string format for pdf file
+        return File.format(self)
+    
+    def hasForm(self, name):
+        """test for existence of named form"""
+        internalname = self.formName(name)
+        try:
+            test = self.idToObject[internalname]          
+        except:
+            return 0
+        else:
+            return internalname
 
-class OutputGrabber:
-    """At times we need to put something in the place of standard
-    output.  This grabs stdout, keeps the data, and releases stdout
-    when done.
+    def xobjDict(self, formnames):
+        """construct an xobject dict (for inclusion in a resource dict, usually)
+           from a list of form names (images not yet supported)"""
+        D = {}
+        for name in formnames:
+            internalname = self.formName(name)
+            reference = PDFObjectReference(internalname)
+            D[internalname] = reference
+        #print "xobjDict D", D
+        return PDFDictionary(D)
+        
+    def Reference(self, object, name=None):
+        ### note references may "grow" during the final formatting pass: don't use d.keys()!
+        # don't make references to other references, or non instances
+        from types import InstanceType
+        #print"object type is ", type(object)
+        tob = type(object)
+        if (tob is not InstanceType) or (tob is InstanceType and object.__class__ is PDFObjectReference):
+            return object
+        idToObject = self.idToObject
+        if hasattr(object, __InternalName__):
+            # already registered
+            intname = object.__InternalName__
+            if name is not None and name!=intname:
+                raise ValueError, "attempt to reregister object %s with new name %s" % (
+                    repr(intname), repr(name))
+            if not idToObject.has_key(intname):
+                raise ValueError, "object named but not registered"
+            return PDFObjectReference(intname)
+        # otherwise register the new object
+        objectcounter = self.objectcounter = self.objectcounter+1
+        if name is None:
+            name = "R"+repr(objectcounter)
+        if idToObject.has_key(name):
+            raise ValueError, "redefining named object: "+repr(name)
+        object.__InternalName__ = name
+        self.idToObjectNumberAndVersion[name] = (objectcounter, 0)
+        self.numberToId[objectcounter] = name
+        idToObject[name] = object
+        return PDFObjectReference(name)
+
+### chapter 4 Objects
+
+PDFtrue = "true"
+PDFfalse = "false"
+PDFnull = "null"
+
+def PDFnumber(n):
+    return n
+
+def PDFString(str):
+    # might need to change this to class for encryption
+    return "(%s)" % pdfutils._escape(str)
+    
+def PDFName(data):
+    # first convert the name
+    ldata = list(data)
+    index = 0
+    for thischar in data:
+        if 0x21<=ord(thischar)<=0x7e and thischar not in "%()<>{}[]#":
+            pass # no problemo
+        else:
+            hexord = hex(ord(thischar))[2:] # forget the 0x thing...
+            ldata[index] = "#"+hexord
+        index = index+1
+    data = string.join(ldata, "")
+    return "/%s" % data
     
-    NOT working well enough!"""
-    def __init__(self):
-        self.oldoutput = sys.stdout
-        sys.stdout = self
-        self.closed = 0
-        self.data = []
-    def write(self, x):
-        if not self.closed:
-            self.data.append(x)
-    
-    def getData(self):
-        return string.join(self.data)
+class PDFDictionary:
+
+    multiline = LongFormat
+    def __init__(self, dict=None):
+        """dict should be namestring to value eg "a": 122 NOT pdfname to value NOT "/a":122"""
+        if dict is None:
+            self.dict = {}
+        else:
+            self.dict = dict.copy()
+    def __setitem__(self, name, value):
+        self.dict[name] = value
+    def Reference(name, document):
+        ob = self.dict[name]
+        self.dict[name] = document.Reference(ob)
+    def format(self, document):
+        dict = self.dict
+        keys = dict.keys()
+        keys.sort()
+        L = []
+        a = L.append
+        for k in keys:
+            v = dict[k]
+            fv = format(v, document)
+            fk = format(PDFName(k), document)
+            a(fk)
+            a(" "+fv)
+        #L = map(str, L)
+        if self.multiline:
+            Lj = string.join(L, LINEEND)
+            Lj = indent(Lj)
+        else:
+            Lj = L
+            # break up every 6 elements anyway
+            for i in range(6, len(Lj), 6):
+                Lj.insert(i,LINEEND)
+            Lj = string.join(L, " ")
+        return "<< %s >>" % Lj
+
+STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary
+             "stream" # stream keyword
+             "%(LINEEND)s" # a line end (could be just a \n)
+             "%(content)s" # the content, with no lineend
+             "endstream%(LINEEND)s" # the endstream keyword
+             )
+class PDFStream:
+    '''set dictionary elements explicitly stream.dictionary[name]=value'''
+    ### compression stuff not implemented yet
+    __RefOnly__ = 1 # must be at top level
+    def __init__(self, dictionary=None, content=None):
+        if dictionary is None:
+            dictionary = PDFDictionary()
+        self.dictionary = dictionary
+        self.content = content
+        self.filters = None
+    def format(self, document):
+        dictionary = self.dictionary
+        content = self.content
+        filters = self.filters
+        if self.content is None:
+            raise ValueError, "stream content not set"
+        if filters is None:
+            filters = document.defaultStreamFilters
+        if filters is not None:
+            raise "oops", "filters for streams not yet implemented"
+        fc = format(content, document)
+        #print "type(content)", type(content)
+        if fc!=content: burp
+        # set dictionary length parameter
+        dictionary["Length"] = len(content)
+        fd = format(dictionary, document)
+        sdict = LINEENDDICT.copy()
+        sdict["dictionary"] = fd
+        sdict["content"] = fc
+        return STREAMFMT % sdict
+
+def teststream(content=None):
+    #content = "" # test
+    if content is None:
+        content = teststreamcontent
+    content = string.strip(content)
+    content = string.replace(content, "\n", LINEEND) + LINEEND
+    S = PDFStream()
+    S.content = content
+    # nothing else needed...
+    S.__Comment__ = "test stream"
+    return S
+
+teststreamcontent = """
+1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET
+1.00 0.00 1.00 rg
+n 72.00 72.00 432.00 648.00 re B*
+"""       
+class PDFArray:
+    multiline = LongFormat
+    def __init__(self, sequence):
+        self.sequence = list(sequence)
+    def References(self, document):
+        """make all objects in sequence references"""
+        self.sequence = map(document.Reference, self.sequence)
+    def format(self, document):
+        #ssequence = map(str, self.sequence)
+        sequence = self.sequence
+        fsequence = []
+        for elt in sequence:
+            felt = format(elt, document)
+            fsequence.append(felt)
+        if self.multiline:
+            Lj = string.join(fsequence, LINEEND)
+            Lj = indent(Lj)
+        else:
+            # break up every 10 elements anyway
+            Lj = fsequence
+            breakline = LINEEND+" "
+            for i in range(10, len(Lj), 10):
+                Lj.insert(i,breakline)
+            Lj = string.join(Lj)
+        return "[ %s ]" % Lj
+
+INDIRECTOBFMT = ("%(n)s %(v)s obj%(LINEEND)s"
+                 "%(content)s" "%(LINEEND)s"
+                 "endobj" "%(LINEEND)s")
+
+class PDFIndirectObject:
+    __RefOnly__ = 1
+    def __init__(self, name, content):
+        self.name = name
+        self.content = content
+    def format(self, document):
+        name = self.name
+        (n, v) = document.idToObjectNumberAndVersion[name]
+        content = self.content
+        fcontent = format(content, document, toplevel=1) # yes this is at top level
+        sdict = LINEENDDICT.copy()
+        sdict["n"] = n
+        sdict["v"] = v
+        sdict["content"] = fcontent
+        return INDIRECTOBFMT % sdict
+
+class PDFObjectReference:
+    def __init__(self, name):
+        self.name = name
+    def format(self, document):
+        name = self.name
+        (n, v) = document.idToObjectNumberAndVersion[name]
+        return "%s %s R" % (n,v)
+
+### chapter 5
+
+PDFHeader = ("%PDF-1.3"+LINEEND+"%í춾  "+LINEEND)
 
-    def close(self):
-        sys.stdout = self.oldoutput
-        self.closed = 1
-        
-    def __del__(self):
-        if not self.closed:
-            self.close()
+class PDFFile:
+    ### just accumulates strings: keeps track of current offset
+    def __init__(self):
+        self.strings = []
+        self.offset = 0
+        self.add(PDFHeader)
+    def add(self, s):
+        """should be constructed as late as possible, return position where placed"""
+        result = self.offset
+        self.offset = result+len(s)
+        self.strings.append(s)
+        return result
+    def format(self, document):
+        return string.join(self.strings, "")
+
+XREFFMT = '%0.10d %0.5d n'    
+
+class PDFCrossReferenceSubsection:
+    def __init__(self, firstentrynumber, idsequence):
+        self.firstentrynumber = firstentrynumber
+        self.idsequence = idsequence
+    def format(self, document):
+        """id sequence should represent contiguous object nums else error. free numbers not supported (yet)"""
+        firstentrynumber = self.firstentrynumber
+        idsequence = self.idsequence
+        entries = list(idsequence)
+        nentries = len(idsequence)
+        # special case: object number 0 is always free
+        taken = {}
+        if firstentrynumber==0:
+            taken[0] = "standard free entry"
+            nentries = nentries+1
+            entries.insert(0, "0000000000 65535 f")
+        idToNV = document.idToObjectNumberAndVersion
+        idToOffset = document.idToOffset
+        lastentrynumber = firstentrynumber+nentries-1
+        for id in idsequence:
+            (num, version) = idToNV[id]
+            if taken.has_key(num):
+                raise ValueError, "object number collision %s %s %s" % (num, repr(id), repr(taken[id]))
+            if num>lastentrynumber or num<firstentrynumber:
+                raise ValueError, "object number %s not in range %s..%s" % (num, firstentrynumber, lastentrynumber)
+            # compute position in list
+            rnum = num-firstentrynumber
+            taken[num] = id
+            offset = idToOffset[id]
+            entries[num] = XREFFMT % (offset, version)
+        # now add the initial line
+        firstline = "%s %s" % (firstentrynumber, nentries)
+        entries.insert(0, firstline)
+        # make sure it ends with a LINEEND
+        entries.append("")
+        if LINEEND=="\n" or LINEEND=="\r":
+            reflineend = " "+LINEEND # as per spec
+        elif LINEEND=="\r\n":
+            reflineend = LINEEND
+        else:
+            raise ValueError, "bad end of line! %s" % repr(LINEEND)
+        return string.join(entries, LINEEND)
+
+class PDFCrossReferenceTable:
+
+    def __init__(self):
+        self.sections = []
+    def addsection(self, firstentry, ids):
+        section = PDFCrossReferenceSubsection(firstentry, ids)
+        self.sections.append(section)
+    def format(self, document):
+        sections = self.sections
+        if not sections:
+            raise ValueError, "no crossref sections"
+        L = ["xref"+LINEEND]
+        for s in self.sections:
+            fs = format(s, document)
+            L.append(fs)
+        return string.join(L, "")
+
+TRAILERFMT = ("trailer%(LINEEND)s"
+              "%(dict)s%(LINEEND)s"
+              "startxref%(LINEEND)s"
+              "%(startxref)s%(LINEEND)s"
+              "%(PERCENT)s%(PERCENT)sEOF")
+
+class PDFTrailer:
+
+    def __init__(self, startxref, Size=None, Prev=None, Root=None, Info=None, ID=None, Encrypt=None):
+        self.startxref = startxref
+        if Size is None or Root is None:
+            raise ValueError, "Size and Root keys required"
+        dict = self.dict = PDFDictionary()
+        for (n,v) in [("Size", Size), ("Prev", Prev), ("Root", Root),
+                      ("Info", Info), ("Id", ID), ("Encrypt", Encrypt)]:
+            if v is not None:
+                dict[n] = v
+    def format(self, document):
+        fdict = format(self.dict, document)
+        D = LINEENDDICT.copy()
+        D["dict"] = fdict
+        D["startxref"] = self.startxref
+        return TRAILERFMT % D
+
+#### XXXX skipping incremental update,
+#### encryption
+
+#### chapter 6, doc structure
+
+class PDFCatalog:
+    __Comment__ = "Document Root"
+    __RefOnly__ = 1
+    # to override, set as attributes
+    __Defaults__ = {"Type": PDFName("Catalog"),
+                "PageMode": PDFName("UseNone"),
+                }
+    __NoDefault__ = string.split("""
+        Dests Outlines Pages Threads AcroForm Names OpenActions PageMode URI
+        ViewerPreferences PageLabels PageLayout JavaScript StructTreeRoot SpiderInfo"""
+                                 )
+    __Refs__ = __NoDefault__ # make these all into references, if present
     
-                
-def testOutputGrabber():
-    gr = OutputGrabber()
-    for i in range(10):
-        print 'line',i
-    data = gr.getData()
-    gr.close()
-    print 'Data...',data
-    
+    def format(self, document):
+        self.check_format(document)
+        defaults = self.__Defaults__
+        Refs = self.__Refs__
+        D = {}
+        for k in defaults.keys():
+            default = defaults[k]
+            v = None
+            if hasattr(self, k) and getattr(self,k) is not None:
+                v = getattr(self, k)
+            elif default is not None:
+                v = default
+            if v is not None:
+                D[k] = v
+        for k in self.__NoDefault__:
+            if hasattr(self, k):
+                v = getattr(self,k)
+                if v is not None:
+                    D[k] = v
+        # force objects to be references where required
+        for k in Refs:
+            if D.has_key(k):
+                #print"k is", k, "value", D[k]
+                D[k] = document.Reference(D[k])
+        dict = PDFDictionary(D)
+        return format(dict, document)
+
+    def showOutline(self):
+        self.PageMode = PDFName("UseOutlines")
+
+    def showFullScreen(self):
+        self.PageMode = PDFName("FullScreen")
+        
+    def check_format(self, document):
+        """for use in subclasses"""
+        pass
+
+# not yet implementing
+#  ViewerPreferences, PageLabelDictionaries,
+
+class PDFPages(PDFCatalog):
+    """PAGES TREE WITH ONE INTERNAL NODE, FOR "BALANCING" CHANGE IMPLEMENATION"""
+    __Comment__ = "page tree"
+    __RefOnly__ = 1
+    # note: could implement page attribute inheritance...
+    __Defaults__ = {"Type": PDFName("Pages"),
+                    }
+    __NoDefault__ = string.split("Kids Count Parent")
+    __Refs__ = ["Parent"]
+    def __init__(self):
+        self.pages = []
+    def __getitem__(self, item):
+        return self.pages[item]
+    def addPage(self, page):
+        self.pages.append(page)
+    def check_format(self, document):
+        # convert all pages to page references
+        pages = self.pages
+        kids = PDFArray(self.pages)
+        # make sure all pages are references
+        kids.References(document)
+        self.Kids = kids
+        self.Count = len(pages)
 
-##############################################################
-#
-#            PDF Object Hierarchy
-#
-##############################################################
+class PDFPage(PDFCatalog):
+    __Comment__ = "Page dictionary"
+    # all PDF attributes can be set explicitly
+    # if this flag is set, the "usual" behavior will be suppressed
+    Override_default_compilation = 0
+    __RefOnly__ = 1
+    __Defaults__ = {"Type": PDFName("Page"),
+                   # "Parent": PDFObjectReference(Pages),  # no! use document.Pages
+                    }
+    __NoDefault__ = string.split(""" Parent
+        MediaBox Resources Contents CropBox Rotate Thumb Annots B Dur Hid Trans AA
+        PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ
+    """)
+    __Refs__ = string.split("""
+        Contents Parent ID
+    """)
+    pagewidth = 595
+    pageheight = 842
+    stream = None
+    hasImages = 0
+    compression = 0
+    XObjects = None
+    # transitionstring?
+    # xobjects?
+    # annotations
+    def __init__(self):
+        # set all nodefaults to None
+        for name in self.__NoDefault__:
+            setattr(self, name, None)
+    def setCompression(self, onoff):
+        self.compression = onoff
+    def setStream(self, code):
+        if self.Override_default_compilation:
+            raise ValueError, "overridden! must set stream explicitly"
+        from types import ListType
+        if type(code) is ListType:
+            code = string.join(code, LINEEND)+LINEEND
+        self.stream = code
+        
+    def check_format(self, document):
+        # set up parameters unless usual behaviour is suppressed
+        if self.Override_default_compilation:
+            return   
+        self.MediaBox = self.MediaBox or PDFArray([0, 0, self.pagewidth, self.pageheight])
+        if not self.Annots:
+            self.Annots = None
+        else:
+            #print self.Annots
+            #raise ValueError, "annotations not reimplemented yet"
+            if type(self.Annots) is not types.InstanceType:
+                self.Annots = PDFArray(self.Annots)
+        if not self.Contents:
+            stream = self.stream
+            if not stream:
+                self.Contents = teststream()
+            else:
+                S = PDFStream()
+                S.content = stream
+                # need to add filter stuff (?)
+                S.__Comment__ = "page stream"
+                self.Contents = S
+        if not self.Resources:
+            resources = PDFResourceDictionary()
+            # fonts!
+            resources.basicFonts()
+            if self.hasImages:
+                resources.allProcs()
+            else:
+                resources.basicProcs()
+            if self.XObjects:
+                #print "XObjects", self.XObjects.dict
+                resources.XObject = self.XObjects
+            self.Resources = resources
+        if not self.Parent:
+            pages = document.Pages
+            self.Parent = document.Reference(pages)
+
+def testpage(document):
+    P = PDFPage()
+    P.Contents = teststream()
+    pages = document.Pages
+    P.Parent = document.Reference(pages)
+    P.MediaBox = PDFArray([0, 0, 595, 841])
+    resources = PDFResourceDictionary()
+    resources.allProcs() # enable all procsets
+    resources.basicFonts()
+    P.Resources = resources
+    pages.addPage(P)
+
+#### DUMMY OUTLINES IMPLEMENTATION FOR NOW
+
+'''    
+DUMMYOUTLINE = """
+<<
+  /Count
+      0
+  /Type
+      /Outlines
+>>"""
+
+class PDFOutlines:
+    __Comment__ = "TEST OUTLINE!"
+    text = string.replace(DUMMYOUTLINE, "\n", LINEEND)
+    __RefOnly__ = 1
+    def format(self, document):
+        return self.text
+        '''
+
+class OutlineEntryObject:
+	"an entry in an outline"
+	Title = Dest = Parent = Prev = Next = First = Last = Count = None
+	def format(self, document):
+		D = {}
+		D["Title"] = PDFString(self.Title)
+		D["Parent"] = self.Parent
+		D["Dest"] = self.Dest
+		for n in ("Prev", "Next", "First", "Last", "Count"):
+			v = getattr(self, n)
+			if v is not None:
+				D[n] = v
+		PD = PDFDictionary(D)
+		return PD.format(document)
 
 
+class PDFOutlines:
+	"""takes a recursive list of outline destinations
+	   like
+		   out = PDFOutline1()
+		   out.setNames(canvas, # requires canvas for name resolution
+			 "chapter1dest",
+			 ("chapter2dest",
+			  ["chapter2section1dest",
+			   "chapter2section2dest",
+			   "chapter2conclusiondest"]
+			 ), # end of chapter2 description
+			 "chapter3dest",
+			 ("chapter4dest", ["c4s1", "c4s2"])
+			 )
+	   Higher layers may build this structure incrementally. KISS at base level.
+	"""
+	# first attempt, many possible features missing.
+	#no init for now
+	mydestinations = ready = None
+	counter = 0
+	currentlevel = -1 # ie, no levels yet
+	
+	def __init__(self):
+		self.destinationnamestotitles = {}
+		self.destinationstotitles = {}
+		self.levelstack = []
+		self.buildtree = []
+		self.closedict = {} # dictionary of "closed" destinations in the outline
 
-class PDFObject:
-    """Base class for all PDF objects.  In PDF, precise measurement
-    of file offsets is essential, so the usual trick of just printing
-    and redirecting output has proved to give different behaviour on
-    Mac and Windows.  While it might be soluble, I'm taking charge
-    of line ends at the binary level and explicitly writing to a file.
-    The LINEEND constant lets me try CR, LF and CRLF easily to help
-    pin down the problem."""
-    def save(self, file):
-        "Save its content to an open file"
-        file.write('% base PDF object' + LINEEND)
-    def printPDF(self):
-        self.save(sys.stdout)
-    
-
-class PDFLiteral(PDFObject):
-    " a ready-made one you wish to quote"
-    def __init__(self, text):
-        self.text = text
-    def save(self, file):
-        file.write(str(self.text) + LINEEND)
-
+	def addOutlineEntry(self, destinationname, level=0, title=None, closed=None):
+		"""destinationname of None means "close the tree" """
+		from types import IntType, TupleType
+		if destinationname is None and level!=0:
+			raise ValueError, "close tree must have level of 0"
+		if type(level) is not IntType: raise ValueError, "level must be integer, got %s" % type(level)
+		if level<0: raise ValueError, "negative levels not allowed"
+		if title is None: title = destinationname
+		currentlevel = self.currentlevel
+		stack = self.levelstack
+		tree = self.buildtree
+		# adjust currentlevel and stack to match level
+		if level>currentlevel:
+			if level>currentlevel+1:
+				raise ValueError, "can't jump from outline level %s to level %s, need intermediates" %(currentlevel, level)
+			level = currentlevel = currentlevel+1
+			stack.append([])
+		while level<currentlevel:
+			# pop off levels to match
+			current = stack[-1]
+			del stack[-1]
+			previous = stack[-1]
+			lastinprevious = previous[-1]
+			if type(lastinprevious) is TupleType:
+				(name, sectionlist) = lastinprevious
+				raise ValueError, "cannot reset existing sections: " + repr(lastinprevious)
+			else:
+				name = lastinprevious
+				sectionlist = current
+				previous[-1] = (name, sectionlist)
+			#sectionlist.append(current)
+			currentlevel = currentlevel-1
+		if destinationname is None: return
+		stack[-1].append(destinationname)
+		self.destinationnamestotitles[destinationname] = title
+		if closed: self.closedict[destinationname] = 1
+		self.currentlevel = level
+		
+	def setDestinations(self, destinationtree):
+		self.mydestinations = destinationtree
+		
+	def format(self, document):
+		D = {}
+		D["Type"] = PDFName("Outlines")
+		c = self.count
+		D["Count"] = c
+		if c!=0:
+		    D["First"] = self.first
+		    D["Last"] = self.last
+		PD = PDFDictionary(D)
+		return PD.format(document)
+		
+	def setNames(self, canvas, *nametree):
+		desttree = self.translateNames(canvas, nametree)
+		self.setDestinations(desttree)
+		
+	def setNameList(self, canvas, nametree):
+		"Explicit list so I don't need to do apply(...) in the caller"
+		desttree = self.translateNames(canvas, nametree)
+		self.setDestinations(desttree)
+		
+	def translateNames(self, canvas, object):
+		"recursively translate tree of names into tree of destinations"
+		from types import StringType, ListType, TupleType
+		Ot = type(object)
+		destinationnamestotitles = self.destinationnamestotitles
+		destinationstotitles = self.destinationstotitles
+		closedict = self.closedict
+		if Ot is StringType:
+			destination = canvas._bookmarkReference(object)
+			title = object
+			if destinationnamestotitles.has_key(object):
+				title = destinationnamestotitles[object]
+			else:
+				destinationnamestotitles[title] = title
+			destinationstotitles[destination] = title
+			if closedict.has_key(object):
+				closedict[destination] = 1 # mark destination closed
+			return {object: canvas._bookmarkReference(object)} # name-->ref
+		if Ot is ListType or Ot is TupleType:
+			L = []
+			for o in object:
+				L.append(self.translateNames(canvas, o))
+			if Ot is TupleType:
+				return tuple(L)
+			return L
+		raise "in outline, destination name must be string: got a %s" % Ot
 
+	def prepare(self, document, canvas):
+		"""prepare all data structures required for save operation (create related objects)"""
+		if self.mydestinations is None:
+			if self.levelstack:
+				self.addOutlineEntry(None) # close the tree
+				destnames = self.levelstack[0]
+				#from pprint import pprint; pprint(destnames); stop
+				self.mydestinations = self.translateNames(canvas, destnames)
+			else:
+				self.first = self.last = None
+				self.count = 0
+				self.ready = 1
+				return
+		#self.first = document.objectReference("Outline.First")
+		#self.last = document.objectReference("Outline.Last")
+		# XXXX this needs to be generalized for closed entries!
+		self.count = count(self.mydestinations, self.closedict)
+		(self.first, self.last) = self.maketree(document, self.mydestinations, toplevel=1)
+		self.ready = 1
 
-class PDFCatalog(PDFObject):
-    "requires RefPages and RefOutlines set"
-    PageMode = "/UseNone"
-    def __init__(self):
-        self.template = string.join([
-                        '<<',
-                        '/Type /Catalog',
-                        '/Pages %d 0 R',
-                        '/Outlines %d 0 R',
-                        '/PageMode %s',
-                        '>>'
-                        ],LINEEND
-                        )
-    def showOutline(self):
-        self.PageMode = "/UseOutlines"
-    def showFullScreen(self):
-        self.PageMode = "/FullScreen"
-    def save(self, file):
-        file.write(self.template % (self.RefPages, self.RefOutlines, self.PageMode) + LINEEND)
+	def maketree(self, document, destinationtree, Parent=None, toplevel=0):
+		from types import ListType, TupleType, DictType
+		tdestinationtree = type(destinationtree)
+		if toplevel:
+			levelname = "Outline"
+			Parent = document.Reference(document.Outlines)
+		else:
+			self.count = self.count+1
+			levelname = "Outline.%s" % self.count
+			if Parent is None:
+				raise ValueError, "non-top level outline elt parent must be specified"
+		if tdestinationtree is not ListType and tdestinationtree is not TupleType:
+			raise ValueError, "destinationtree must be list or tuple, got %s"
+		nelts = len(destinationtree)
+		lastindex = nelts-1
+		lastelt = firstref = lastref = None
+		destinationnamestotitles = self.destinationnamestotitles
+		closedict = self.closedict
+		for index in range(nelts):
+			eltobj = OutlineEntryObject()
+			eltobj.Parent = Parent
+			eltname = "%s.%s" % (levelname, index)
+			eltref = document.Reference(eltobj, eltname)
+			#document.add(eltname, eltobj)
+			if lastelt is not None:
+				lastelt.Next = eltref
+				eltobj.Prev = lastref
+			if firstref is None:
+				firstref = eltref
+			lastref = eltref
+			lastelt = eltobj # advance eltobj
+			lastref = eltref
+			elt = destinationtree[index]
+			te = type(elt)
+			if te is DictType:
+				# simple leaf {name: dest}
+				leafdict = elt
+			elif te is TupleType:
+				# leaf with subsections: ({name: ref}, subsections) XXXX should clean up (see count(...))
+				try:
+					(leafdict, subsections) = elt
+				except:
+					raise ValueError, "destination tree elt tuple should have two elts, got %s" % len(elt)
+				eltobj.Count = count(subsections, closedict)
+				(eltobj.First, eltobj.Last) = self.maketree(document, subsections, eltref)
+			else:
+				raise ValueError, "destination tree elt should be dict or tuple, got %s" % te
+			try:
+				[(Title, Dest)] = leafdict.items()
+			except:
+				raise ValueError, "bad outline leaf dictionary, should have one entry "+str(elt)
+			eltobj.Title = destinationnamestotitles[Title]
+			eltobj.Dest = Dest
+			if te is TupleType and closedict.has_key(Dest):
+				# closed subsection, count should be negative
+				eltobj.Count = -eltobj.Count
+		return (firstref, lastref)
+		
+def count(tree, closedict=None): 
+	"""utility for outline: recursively count leaves in a tuple/list tree"""
+	from operator import add
+	from types import TupleType, ListType
+	tt = type(tree)
+	if tt is TupleType:
+		# leaf with subsections XXXX should clean up this structural usage
+		(leafdict, subsections) = tree
+		[(Title, Dest)] = leafdict.items()
+		if closedict and closedict.has_key(Dest):
+			return 1 # closed tree element
+	if tt is TupleType or tt is ListType:
+		#return reduce(add, map(count, tree))
+		counts = []
+		for e in tree:
+			counts.append(count(e, closedict))
+		return reduce(add, counts)
+	return 1
+	
+	
 
+#### dummy info
+DUMMYINFO = """
+<</Title (testing)
+/Author (arw)
+/CreationDate (D:20001012220652)
+/Producer (ReportLab http://www.reportlab.com)
+/Subject (this file generated by an alpha test module)
+>>
+"""
+class PDFInfo0:
+    __Comment__ = "TEST INFO STRUCTURE"
+    text = string.replace(DUMMYINFO, "\n", LINEEND)
+    __RefOnly__ = 1
+    def format(self, document):
+        return self.text
 
-class PDFInfo(PDFObject):
+class PDFInfo:
     """PDF documents can have basic information embedded, viewable from
     File | Document Info in Acrobat Reader.  If this is wrong, you get
     Postscript errors while printing, even though it does not print."""
@@ -579,465 +1145,33 @@
         self.title = "untitled"
         self.author = "anonymous"
         self.subject = "unspecified"
-
-        now = time.localtime(time.time())
-        self.datestr = '%04d%02d%02d%02d%02d%02d' % tuple(now[0:6])
+        #now = time.localtime(time.time())
+        #self.datestr = '%04d%02d%02d%02d%02d%02d' % tuple(now[0:6])
+        
+    def format(self, document):
+        D = {}
+        D["Title"] = PDFString(self.title)
+        D["Author"] = PDFString(self.author)
+        D["CreationDate"] = PDFDate()
+        D["Producer"] = PDFString("ReporLab http://www.reportlab.com")
+        D["Subject"] = PDFString(self.subject)
+        PD = PDFDictionary(D)
+        return PD.format(document)
 
-    def save(self, file):
-        file.write(string.join([
-                "<</Title (%s)",
-                "/Author (%s)",
-                "/CreationDate (D:%s)",
-                "/Producer (ReportLab http://www.reportlab.com)",
-                "/Subject (%s)",
-                ">>"
-                ], LINEEND
-            ) % ( 
-    pdfutils._escape(self.title), 
-    pdfutils._escape(self.author), 
-    self.datestr, 
-    pdfutils._escape(self.subject)
-    ) + LINEEND)
-    
+# skipping thumbnails, etc
 
 
-class PDFOutline0(PDFObject):
-    "null outline, does nothing yet"
-    def __init__(self):
-        self.template = string.join([
-                '<<',
-                '/Type /Outlines',
-                '/Count 0',
-                '>>'],
-                LINEEND)
-    def save(self, file):
-        file.write(self.template + LINEEND)
-        
-class PDFOutline(PDFObject):
-    """takes a recursive list of outline destinations
-       like
-           out = PDFOutline1()
-           out.setNames(canvas, # requires canvas for name resolution
-             "chapter1dest",
-             ("chapter2dest",
-              ["chapter2section1dest",
-               "chapter2section2dest",
-               "chapter2conclusiondest"]
-             ), # end of chapter2 description
-             "chapter3dest",
-             ("chapter4dest", ["c4s1", "c4s2"])
-             )
-       Higher layers may build this structure incrementally. KISS at base level.
-    """
-    # first attempt, many possible features missing.
-    #no init for now
-    mydestinations = ready = None
-    counter = 0
-    currentlevel = -1 # ie, no levels yet
-    def __init__(self):
-        self.destinationnamestotitles = {}
-        self.destinationstotitles = {}
-        self.levelstack = []
-        self.buildtree = []
-        self.closedict = {} # dictionary of "closed" destinations in the outline
-
-    def addOutlineEntry(self, destinationname, level=0, title=None, closed=None):
-        """destinationname of None means "close the tree" """
-        if destinationname is None and level!=0:
-            raise ValueError, "close tree must have level of 0"
-        if type(level) is not IntType: raise ValueError, "level must be integer, got %s" % type(level)
-        if level<0: raise ValueError, "negative levels not allowed"
-        if title is None: title = destinationname
-        currentlevel = self.currentlevel
-        stack = self.levelstack
-        tree = self.buildtree
-        # adjust currentlevel and stack to match level
-        if level>currentlevel:
-            if level>currentlevel+1:
-                raise ValueError, "can't jump from outline level %s to level %s, need intermediates" %(currentlevel, level)
-            level = currentlevel = currentlevel+1
-            stack.append([])
-        while level<currentlevel:
-            # pop off levels to match
-            current = stack[-1]
-            del stack[-1]
-            previous = stack[-1]
-            lastinprevious = previous[-1]
-            if type(lastinprevious) is TupleType:
-                (name, sectionlist) = lastinprevious
-                raise ValueError, "cannot reset existing sections: " + repr(lastinprevious)
-            else:
-                name = lastinprevious
-                sectionlist = current
-                previous[-1] = (name, sectionlist)
-            #sectionlist.append(current)
-            currentlevel = currentlevel-1
-        if destinationname is None: return
-        stack[-1].append(destinationname)
-        self.destinationnamestotitles[destinationname] = title
-        if closed: self.closedict[destinationname] = 1
-        self.currentlevel = level
-    def setDestinations(self, destinationtree):
-        self.mydestinations = destinationtree
-    def save(self, file):
-        c = self.count
-        if c==0:
-           file.write(BasicPDFDictString(Type="/Outlines", Count=c))
-           return
-        first = self.first
-        last = self.last
-        file.write(BasicPDFDictString(Type="/Outlines", Count=c, First=first, Last=last))
-    def setNames(self, canvas, *nametree):
-        desttree = self.translateNames(canvas, nametree)
-        self.setDestinations(desttree)
-        
-    def setNameList(self, canvas, nametree):
-        "Explicit list so I don't need to do apply(...) in the caller"
-        desttree = self.translateNames(canvas, nametree)
-        self.setDestinations(desttree)
-        
-    def translateNames(self, canvas, object):
-        "recursively translate tree of names into tree of destinations"
-        Ot = type(object)
-        destinationnamestotitles = self.destinationnamestotitles
-        destinationstotitles = self.destinationstotitles
-        closedict = self.closedict
-        if Ot is StringType:
-            destination = canvas._bookmarkReference(object)
-            title = object
-            if destinationnamestotitles.has_key(object):
-                title = destinationnamestotitles[object]
-            else:
-                destinationnamestotitles[title] = title
-            destinationstotitles[destination] = title
-            if closedict.has_key(object):
-                closedict[destination] = 1 # mark destination closed
-            return {object: canvas._bookmarkReference(object)} # name-->ref
-        if Ot is ListType or Ot is TupleType:
-            L = []
-            for o in object:
-                L.append(self.translateNames(canvas, o))
-            if Ot is TupleType:
-                return tuple(L)
-            return L
-        raise "in outline, destination name must be string: got a %s" % Ot
-    def prepare(self, document, canvas):
-        """prepare all data structures required for save operation (create related objects)"""
-        if self.mydestinations is None:
-            if self.levelstack:
-                self.addOutlineEntry(None) # close the tree
-                destnames = self.levelstack[0]
-                #from pprint import pprint; pprint(destnames); stop
-                self.mydestinations = self.translateNames(canvas, destnames)
-            else:
-                self.first = self.last = None
-                self.count = 0
-                self.ready = 1
-                return
-        #self.first = document.objectReference("Outline.First")
-        #self.last = document.objectReference("Outline.Last")
-        # XXXX this needs to be generalized for closed entries!
-        self.count = count(self.mydestinations, self.closedict)
-        (self.first, self.last) = self.maketree(document, self.mydestinations, toplevel=1)
-        self.ready = 1
-    def maketree(self, document, destinationtree, Parent=None, toplevel=0):
-        if toplevel:
-            levelname = "Outline"
-            Parent = document.objectReference("Outline")
-        else:
-            self.count = self.count+1
-            levelname = "Outline.%s" % self.count
-            if Parent is None:
-                raise ValueError, "non-top level outline elt parent must be specified"
-        if type(destinationtree) is not ListType and type(destinationtree) is not TupleType:
-            raise ValueError, "destinationtree must be list or tuple, got %s"
-        nelts = len(destinationtree)
-        lastindex = nelts-1
-        lastelt = firstref = lastref = None
-        destinationnamestotitles = self.destinationnamestotitles
-        closedict = self.closedict
-        for index in range(nelts):
-            eltobj = OutlineEntryObject()
-            eltobj.Parent = Parent
-            eltname = "%s.%s" % (levelname, index)
-            eltref = document.objectReference(eltname)
-            document.add(eltname, eltobj)
-            if lastelt is not None:
-                lastelt.Next = eltref
-                eltobj.Prev = lastref
-            if firstref is None:
-                firstref = eltref
-            lastref = eltref
-            lastelt = eltobj # advance eltobj
-            lastref = eltref
-            elt = destinationtree[index]
-            te = type(elt)
-            if te is DictType:
-                # simple leaf {name: dest}
-                leafdict = elt
-            elif te is TupleType:
-                # leaf with subsections: ({name: ref}, subsections) XXXX should clean up (see count(...))
-                try:
-                    (leafdict, subsections) = elt
-                except:
-                    raise ValueError, "destination tree elt tuple should have two elts, got %s" % len(elt)
-                eltobj.Count = count(subsections, closedict)
-                (eltobj.First, eltobj.Last) = self.maketree(document, subsections, eltref)
-            else:
-                raise ValueError, "destination tree elt should be dict or tuple, got %s" % te
-            try:
-                [(Title, Dest)] = leafdict.items()
-            except:
-                raise ValueError, "bad outline leaf dictionary, should have one entry "+str(elt)
-            eltobj.Title = destinationnamestotitles[Title]
-            eltobj.Dest = Dest
-            if te is TupleType and closedict.has_key(Dest):
-                # closed subsection, count should be negative
-                eltobj.Count = -eltobj.Count
-        return (firstref, lastref)
-        
-def count(tree, closedict=None): 
-    """utility for outline: recursively count leaves in a tuple/list tree"""
-    from operator import add
-    tt = type(tree)
-    if tt is TupleType:
-        # leaf with subsections XXXX should clean up this structural usage
-        (leafdict, subsections) = tree
-        [(Title, Dest)] = leafdict.items()
-        if closedict and closedict.has_key(Dest):
-            return 1 # closed tree element
-    if tt is TupleType or tt is ListType:
-        #return reduce(add, map(count, tree))
-        counts = []
-        for e in tree:
-            counts.append(count(e, closedict))
-        return reduce(add, counts)
-    return 1
-    
-class OutlineEntryObject(PDFObject):
-    "an entry in an outline"
-    Title = Dest = Parent = Prev = Next = First = Last = Count = None
-    def save(self, file):
-        D = {}
-        D["Title"] = PDFString(self.Title)
-        D["Parent"] = self.Parent
-        D["Dest"] = self.Dest
-        for n in ("Prev", "Next", "First", "Last", "Count"):
-            v = getattr(self, n)
-            if v is not None:
-                D[n] = v
-        file.write(apply(BasicPDFDictString, (), D))
-    
-
-class PDFPageCollection(PDFObject):
-    "presumes PageList attribute set (list of integers)"
-    def __init__(self):
-        self.PageList = []
-
-    def save(self, file):
-        lines = [ '<<',
-                '/Type /Pages',
-                '/Count %d' % len(self.PageList),
-                '/Kids ['
-                ]
-        for page in self.PageList:
-            lines.append(str(page) + ' 0 R ')
-        lines.append(']')
-        lines.append('>>')
-        text = string.join(lines, LINEEND)
-        file.write(text + LINEEND)
-
-
-class ResourceDictUserMixin:
-    """common functionality for PDFObjects that have resource dictionaries"""
-    #override
-    info = None
-
-    def resourceDict(self, **kw):
-        info = {}
-        if self.info:
-            info.update(self.info)
-        info.update(kw)
-        #info = self.info
-        L = []
-        a = L.append
-        a("<<")
-        a("    /Font %(fontdict)s" % info)
-        a("    /ProcSet %(procsettext)s" % info)
-        #if self.XObjects:
-        #    a("    /XObject "+self.XObjects)
-        if info.has_key("XObjects") and info["XObjects"]:
-             a("    /XObject "+info["XObjects"])
-        a(">>")
-        #a("")
-        return string.join(L, LINEEND)
-        
-class PDFPage(PDFObject, ResourceDictUserMixin):
-    """The Bastard.  Needs list of Resources etc. Use a standard one for now.
-    It manages a PDFStream object which must be added to the document's list
-    of objects as well."""
-    def __init__(self):
-        self.drawables = []
-        self.pagewidth = 595  #these are overridden by piddlePDF
-        self.pageheight = 842
-        self.stream = PDFStream()
-        self.hasImages = 0
-        # when set this should be a python string containing a PDF XObject dictionary
-        self.XObjects = ""
-        # when full, should contain a list of pdfgen annotation names
-        #self.AnnotationNames = [] (not used)
-        # when set, should contain a list of object references equivalent to names above
-        self.Annots = []
-        self.pageTransitionString = ''  # presentation effects
-        # editors on different systems may put different things in the line end
-        # without me noticing.  No triple-quoted strings allowed!
-        self.template = string.join([
-                '<<',
-                '/Type /Page',
-                '/Parent %(parentpos)d 0 R',
-                '/Resources',
-                #'   <<',
-                #'   /Font %(fontdict)s',
-                #'   /ProcSet %(procsettext)s',
-                #'   >>',
-                "%(resourcedict)s",
-                '/MediaBox [0 0 %(pagewidth)d %(pageheight)d]',  #A4 by default
-                '/Contents %(contentspos)d 0 R',
-                '%(transitionString)s',
-                '%(Annots)s',
-                '>>'],
-            LINEEND)
-            
-    #def addXObjectDictString(XObjectDictString): set directly (also for form)
-    #    self.XObjects = XObjectDictString
-        
-    def setCompression(self, onoff=0):
-        "Turns page compression on or off"
-        assert onoff in [0,1], "Page compression options are 1=on, 2=off"
-        self.stream.compression = onoff 
-        
-    def save(self, file):
-        info = self.info
-        info['pagewidth'] = self.pagewidth
-        info['pageheight'] = self.pageheight
-        # check for image support
-        if self.hasImages:
-            info['procsettext'] = '[/PDF /Text /ImageC]'
-        else:
-            info['procsettext'] = '[/PDF /Text]'
-        info['transitionString'] = self.pageTransitionString
-        info['resourcedict'] = self.resourceDict(XObjects=self.XObjects)
-        Annots = self.Annots
-        if Annots:
-            info['Annots'] = "/Annots %s" % apply(BasicPDFArrayString,Annots)
-        else:
-            info['Annots'] = ""
-
-        #print self.template
-        #print info
-        file.write(self.template % info + LINEEND)
-
-    def clear(self):
-        self.drawables = []
-    
-    def setStream(self, data):
-        if type(data) is ListType:
-            data = string.join(data, LINEEND)
-        self.stream.setStream(data)
-
-TestStream = "BT /F6 24 Tf 80 672 Td 24 TL (   ) Tj T* ET"
-
-
-class PDFStream(PDFObject):
-    "Used for the contents of a page"
-    def __init__(self):
-        self.data = None
-        self.compression = 0
-
-    def setStream(self, data):
-        self.data = data
-        
-    def streamDict(self, **kw):
-        if self.compression:
-            return '<< /Length %(length)d /Filter [/ASCII85Decode /FlateDecode]>>' % kw + LINEEND
-        else:
-            return '<< /Length %(length)d >>' % kw + LINEEND
-
-    def save(self, file):
-        #avoid crashes if they wrote nothing in the page
-        if self.data == None:
-             self.data = TestStream
-
-        if self.compression == 1:
-            comp = zlib.compress(self.data)   #this bit is very fast...
-            base85 = pdfutils._AsciiBase85Encode(comp) #...sadly this isn't
-            wrapped = pdfutils._wrap(base85)
-            data_to_write = wrapped
-        else:
-            data_to_write = self.data
-        # the PDF length key should contain the length including
-        # any extra LF pairs added by Print on DOS.
-        
-        #lines = len(string.split(self.data,'\n'))
-        #length = len(self.data) + lines   # one extra LF each
-        length = len(data_to_write) + len(LINEEND)    #AR 19980202
-        #arw: mar16 2000: make more general for subclassing
-        #if self.compression:
-        #    file.write('<< /Length %d /Filter [/ASCII85Decode /FlateDecode]>>' % length + LINEEND)
-        #else:
-        #    file.write('<< /Length %d >>' % length + LINEEND)
-        file.write(self.streamDict(length=length))
-        file.write('stream' + LINEEND)
-        file.write(data_to_write + LINEEND)
-        file.write('endstream' + LINEEND)
-        
-PDFFormDictTemplate = string.join([ # from pdf spec mar 11 1999 p 257 (arw)
-  "<< /Type /XObject /Subtype /Form /FormType 1",
-  "/BBox [%(lowerx)d %(lowery)d %(upperx)d %(uppery)d]",
-  "/Matrix [1 0 0 1 0 0]", # constant matrix for now
-  "/Length %(length)d",
-  "%(filter)s",
-  "/Resources",
-  "%(resourcedict)s",
-  "%(Annots)s",
-  ">>",
-  ""], LINEEND) 
-        
-class PDFFormXObject(PDFStream, ResourceDictUserMixin):
-    # like page requires .info set by some higher level (doc)
-    # XXXX any resource used in a form must be propagated up to the page that (recursively) uses
-    #   the form!! (not implemented yet).
-    XObjects = Annots = None
-    compression = 0
-    def __init__(self, lowerx, lowery, upperx, uppery):
-        self.lowerx = lowerx; self.lowery=lowery; self.upperx=upperx; self.uppery=uppery
-    def streamDict(self, **kw):
-        D = {"lowerx":self.lowerx, "lowery": self.lowery, "upperx":self.upperx, "uppery":self.uppery, "filter":""}
-        if self.compression: D["filter"] = "/Filter [/ASCII85Decode /FlateDecode]"
-        D["resourcedict"] = self.resourceDict(procsettext="[/PDF /Text /ImageC]", XObjects=self.XObjects)
-        Annots = self.Annots
-        if Annots:
-            D['Annots'] = "/Annots %s" % apply(BasicPDFArrayString,Annots)
-        else:
-            D['Annots'] = ""
-        D.update(kw)
-        return PDFFormDictTemplate % D
-    def setStreamList(self, data):
-        if type(data) is ListType:
-            data = string.join(data, LINEEND)
-        self.setStream(data)
-        
-class Annotation(PDFObject):
+class Annotation:
     """superclass for all annotations."""
-    defaults = (("Type", "/Annot"),)
+    defaults = [("Type", PDFName("Annot"),)]
     required = ("Type", "Rect", "Contents", "Subtype")
     permitted = required+(
       "Border", "C", "T", "M", "F", "H", "BS", "AA", "AS", "Popup", "P")
     def cvtdict(self, d):
         """transform dict args from python form to pdf string rep as needed"""
         Rect = d["Rect"]
-        if type(Rect) is not StringType:
-            d["Rect"] = apply(BasicPDFArrayString, tuple(Rect))
+        if type(Rect) is not types.StringType:
+            d["Rect"] = PDFArray(Rect)
         d["Contents"] = PDFString(d["Contents"])
         return d
     def AnnotationDict(self, **kw):
@@ -1053,13 +1187,14 @@
         for name in d.keys():
             if name not in permitted:
                 raise ValueError, "bad annotation dictionary name %s" % name
-        return apply(BasicPDFDictString, (), d)
-    def DictString(self):
+        return PDFDictionary(d)
+    def Dict(self):
         raise ValueError, "DictString undefined for virtual superclass Annotation, must overload"
         # but usually
         #return self.AnnotationDict(self, Rect=(a,b,c,d)) or whatever
-    def save(self, file):
-        file.write(self.DictString())
+    def format(self, document):
+        D = self.Dict()
+        return D.format(document)
 
 class TextAnnotation(Annotation):
     permitted = Annotation.permitted + (
@@ -1068,7 +1203,7 @@
         self.Rect = Rect
         self.Contents = Contents
         self.otherkw = kw
-    def DictString(self):
+    def Dict(self):
         d = {}
         d.update(self.otherkw)
         d["Rect"] = self.Rect
@@ -1093,7 +1228,7 @@
              /Dest [23 0 R /Fit] >>
              """
              
-    def DictString(self):
+    def Dict(self):
         d = {}
         d.update(self.otherkw)
         d["Border"] = self.Border
@@ -1103,186 +1238,322 @@
         d["Dest"] = self.Destination
         return apply(self.AnnotationDict, (), d)
         
+
+# skipping names tree
+
+# skipping actions
+
+# skipping names trees
+
+# skipping to chapter 7
+
+class PDFRectangle:
+    def __init__(self, llx, lly, urx, ury):
+        self.llx, self.lly, self.ulx, self.ury = llx, lly, urx, ury
+    def format(self, document):
+        A = PDFArray([self.llx, self.lly, self.ulx, self.ury])
+        return format(A, document)
+
+DATEFMT = '%04d%02d%02d%02d%02d%02d'
+import time
+(nowyyyy, nowmm, nowdd, nowhh, nowm, nows) = tuple(time.localtime(time.time())[:6])
+
+class PDFDate:
+    # gmt offset not yet suppported
+    def __init__(self, yyyy=nowyyyy, mm=nowmm, dd=nowdd, hh=nowhh, m=nowm, s=nows):
+        self.yyyy=yyyy; self.mm=mm; self.dd=dd; self.hh=hh; self.m=m; self.s=s
+    def format(self, doc):
+        S = PDFString(DATEFMT % (self.yyyy, self.mm, self.dd, self.hh, self.m, self.s))
+        return format(S, doc)
+
+  
 class Destination:
-    """not a pdfobject!  This is a placeholder that can convert itself
-       to a string only after it has been defined by the methods
+    """not a pdfobject!  This is a placeholder that can delegates
+       to a pdf object only after it has been defined by the methods
        below.  EG a Destination can refer to Appendix A before it has been
        defined, but only if Appendix A is explicitly noted as a destination
        and resolved before the document is generated...
        For example the following sequence causes resolution before doc generation.
           d = Destination()
           d.fit() # or other format defining method call
-          d.setPageRef("20 0 R")
+          d.setPage(p)
        (at present setPageRef is called on generation of the page).
     """
-    representation = format = pageref = None
+    representation = format = page = None
     def __init__(self,name):
         self.name = name
-    def __str__(self):
-        f = self.format
+    def format(self, document):
+        f = self.fmt
         if f is None: raise ValueError, "format not resolved %s" % self.name
-        p = self.pageref
+        p = self.page
         if p is None: raise ValueError, "Page reference unbound %s" % self.name
-        return f % p
+        f.page = p
+        return f.format(document)
     def xyz(self, left, top, zoom):  # see pdfspec mar 11 99 pp184+
-        self.format = "[ %%s /XYZ %s %s %s ]" % (left, top, zoom)
+        self.fmt = PDFDestinationXYZ(None, left, top, zoom)
     def fit(self):
-        self.format = "[ %s /Fit ]"
+        self.fmt = PDFDestinationFit(None)
     def fitb(self):
-        self.format = "[ %s /FitB ]"
+        self.fmt = PDFDestinationFitB(None)
     def fith(self, top):
-        self.format = "[ %%s /FitH %s ]" % top
+        self.fmt = PDFDestinationFitH(None,top)
     def fitv(self, left):
-        self.format = "[ %%s /FitV %s ]" % left
+        self.fmt = PDFDestinationFitV(None, left)
     def fitbh(self, top):
-        self.format = "[ %%s /FitBH %s ]" % top
+        self.fmt = PDFDestinationFitBH(None, top)
     def fitbv(self, left):
-        self.format = "[ %%s /FitBV %s ]" % left
-    def setPageRef(self, pageref):
-        self.pageref = pageref
-        
-class BindableStr:
-    """trick to make an object whose str() operation may be defined later."""
-    strValue = None
-    def __init__(self, name):
-        self.name = name
-    def __str__(self):
-        v = self.value
-        if v is None: raise ValueError, "str value unbound for name %s" % self.name
-        return v
-    def bind(self, v):
-        self.value = v
+        self.fmt = PDFDestinationFitBV(None, left)
+    def fitr(self, left, bottom, right, top):
+        self.fmt = PDFDestinationFitR(None, left, bottom, right, top)
+    def setPage(self, page):
+        self.page = page
+        #self.fmt.page = page # may not yet be defined!
+            
+class PDFDestinationXYZ:
+    typename = "XYZ"
+    def __init__(self, page, left, top, zoom):
+        self.page = page; self.top=top; self.zoom=zoom
+    def format(self, document):
+        pageref = document.Reference(self.page)
+        A = PDFArray( [ pageref, PDFName(self.typename), self.left, self.top, self.zoom ] )
+        return format(A, document)
+    
+class PDFDestinationFit:
+    typename = "Fit"
+    def __init__(self, page):
+        self.page = page
+    def format(self, document):
+        pageref = document.Reference(self.page)
+        A = PDFArray( [ pageref, PDFName(self.typename) ] )
+        return format(A, document)
+
+class PDFDestinationFitB(PDFDestinationFit):
+    typename = "FitB"
+    
+class PDFDestinationFitH:
+    typename = "FitH"
+    def __init__(self, page, top):
+        self.page = page; self.top=top
+    def format(self, document):
+        pageref = document.Reference(self.page)
+        A = PDFArray( [ pageref, PDFName(self.typename), self.top ] )
+        return format(A, document)
+
+class PDFDestinationFitBH(PDFDestinationFitH):
+    typename = "FitBH"
+    
+class PDFDestinationFitV:
+    typename = "FitV"
+    def __init__(self, page, left):
+        self.page = page; self.left=left
+    def format(self, document):
+        pageref = document.Reference(self.page)
+        A = PDFArray( [ pageref, PDFName(self.typename), self.left ] )
+        return format(A, document)
+
+class PDFDestinationBV(PDFDestinationFitV):
+    typename = "FitBV"
+
+class PDFDestinationFitR:
+    typename = "FitR"
+    def __init__(self, page, left, bottom, right, top):
+        self.page = page; self.left=left; self.bottom=bottom; self.right=right; self.top=top
+    def format(self, document):
+        pageref = document.Reference(self.page)
+        A = PDFArray( [ pageref, PDFName(self.typename), self.left, self.bottom, self.right, self.top] )
+        return format(A, document)
+
+# named destinations need nothing
+
+# skipping filespecs
+
+class PDFResourceDictionary:
+    """each element *could* be reset to a reference if desired"""
+    def __init__(self):
+        self.ColorSpace = {}
+        self.XObject = {}
+        self.ExtGState = {}
+        self.Font = {}
+        self.Pattern = {}
+        self.ProcSet = []
+        self.Properties = {}
+        self.Shading = {}
+        # ?by default define the basicprocs
+        self.basicProcs()
+    stdprocs = map(PDFName, string.split("PDF Text ImageB ImageC ImageI"))
+    dict_attributes = ("ColorSpace", "XObject", "ExtGState", "Font", "Pattern", "Properties", "Shading")
+    def allProcs(self):
+        # define all standard procsets
+        self.ProcSet = self.stdprocs
+    def basicProcs(self):
+        self.ProcSet = self.stdprocs[:2] # just PDF and Text
+    def basicFonts(self):
+        self.Font = PDFObjectReference(BasicFonts)
+    def format(self, document):
+        D = {}
+        from types import ListType, DictType
+        for dname in self.dict_attributes:
+            v = getattr(self, dname)
+            if type(v) is DictType:
+                if v:
+                    dv = PDFDictionary(v)
+                    D[dname] = dv
+            else:
+                D[dname] = v
+        v = self.ProcSet
+        dname = "ProcSet"
+        if type(v) is ListType:
+            if v:
+                dv = PDFArray(v)
+                D[dname] = dv
+        else:
+            D[dname] = v
+        DD = PDFDictionary(D)
+        return format(DD, document)
 
-# this one doesn't seem to work (but I'm not sure :( ).
-class InkAnnotation(Annotation):
-    permitted = Annotation.permitted + (
-        "InkList", "BS", "AP")
-    def __init__(self, Rect, Contents, InkList, **kw):
-        # inklist should be a list of tuples representing a path (in default user space)
-        self.Rect = Rect
-        self.Contents = Contents
-        self.InkList = InkList
-        self.otherkw = kw
-    def DictString(self):
-        InkList = self.InkList
-        # convert the inklist seq of seq into pdf string rep
-        #InkList = map(BasicPDFArrayString, InkList)
-        L = []
-        for e in InkList:
-            L.append(apply(BasicPDFArrayString, e))
-        InkList = apply(BasicPDFArrayString, tuple(L))
-        d = {}
-        d["InkList"] = InkList
-        d["Rect"] = self.Rect
-        d["Contents"] = self.Contents
-        d.update(self.otherkw)
-        d["Subtype"] = "Ink"
-        return apply(self.AnnotationDict, (), d)
-###### more helpers ###### (maybe these should be in utils or in a separate file?)
-def BasicPDFDictString(**kw):
-    """from a set of keyword arguments (with string values)
-       make a PDF dictionary."""
-    L = []
-    a = L.append
-    a("<<")
-    for name in kw.keys():
-        val = kw[name]
-        # XXXX should check name and val for valid data!!!
-        a("  /%s" % name)
-        a("      %s" % val)
-    a(">>")
-    a("")
-    return string.join(L, LINEEND)
-   
-def BasicPDFArrayString(*args):
-    """from a list of positional arguments (with string or int values)
-       make a PDF array"""
-    # XXXX should check elts for valid data
-    L = ["["]
-    a = L.append
-    for arg in args:
-        a(" %s" % arg)
-    a("]")
-    return string.join(L, "")
-    
-def PDFString(str):
-    return "(%s)" % pdfutils._escape(str)
-    
-##### end helpers #####
+class PDFType1Font:
+    """no init: set attributes explicitly"""
+    __RefOnly__ = 1
+    # note! /Name appears to be an undocumented attribute....
+    name_attributes = string.split("Type Subtype BaseFont ToUnicode Name")
+    Type = "Font"
+    Subtype = "Type1"
+    # these attributes are assumed to already be of the right type
+    local_attributes = string.split("FirstChar LastChar Widths Encoding FontDescriptor")
+    def format(self, document):
+        D = {}
+        for name in self.name_attributes:
+            if hasattr(self, name):
+                value = getattr(self, name)
+                D[name] = PDFName(value)
+        for name in self.local_attributes:
+            if hasattr(self, name):
+                value = getattr(self, name)
+                D[name] = value
+        #print D
+        PD = PDFDictionary(D)
+        return PD.format(document)
 
+def MakeStandardEnglishFontObjects(document, encoding=DEFAULT_ENCODING):
+    # make the standard fonts and the standard font dictionary
+    if encoding not in ALLOWED_ENCODINGS:
+        raise ValueError, "bad encoding %s" % repr(encoding)
+    D = {}
+    count = 1
+    fontmapping = document.fontMapping
+    for name in StandardEnglishFonts:
+        F = PDFType1Font()
+        F.BaseFont = name
+        F.Encoding = PDFName(DEFAULT_ENCODING)
+        F.__Comment__ = "Standard English Font %s" % repr(name)
+        fname = "F"+repr(count)
+        F.Name = fname
+        R = document.Reference(F, fname)
+        D[fname] = R
+        fontmapping[name] = "/"+fname # record the external to internal name map (NOT REALLY A PDFNAME: PAGE DESC)
+        count = count+1
+    DD = PDFDictionary(D)
+    DD.__Comment__ = "The standard fonts dictionary"
+    DDR = document.Reference(DD, BasicFonts)
+    return DDR
+
+class PDFTrueTypeFont(PDFType1Font):
+    Subtype = "TrueType"
+    #local_attributes = string.split("FirstChar LastChar Widths Encoding FontDescriptor") #same
+
+class PDFMMType1Font(PDFType1Font):
+    Subtype = "MMType1"
+
+class PDFType3Font(PDFType1Font):
+    Subtype = "Type3"
+    local_attributes = string.split(
+        "FirstChar LastChar Widths CharProcs FontBBox FontMatrix Resources Encoding")
+
+class PDFType0Font(PDFType1Font):
+    Subtype = "Type0"
+    local_attributes = string.split(
+        "DescendantFonts Encoding")
+
+class PDFCIDFontType0(PDFType1Font):
+    Subtype = "CIDFontType0"
+    local_attributes = string.split(
+        "CIDSystemInfo FontDescriptor DW W DW2 W2 Registry Ordering Supplement")
+
+class PDFCIDFontType0(PDFType1Font):
+    Subtype = "CIDFontType2"
+    local_attributes = string.split(
+        "BaseFont CIDToGIDMap CIDSystemInfo FontDescriptor DW W DW2 W2")
 
-class PDFImage(PDFObject):
-    # sample one while developing.  Currently, images go in a literals
-    def save(self, file):
-        file.write(string.join([
-                '<<',
-                '/Type /XObject',
-                '/Subtype /Image',
-                '/Name /Im0',
-                '/Width 24',
-                '/Height 23',
-                '/BitsPerComponent 1',
-                '/ColorSpace /DeviceGray',
-                '/Filter /ASCIIHexDecode',
-                '/Length 174',
-                '>>',
-                'stream',
-                '003B00 002700 002480 0E4940 114920 14B220 3CB650',
-                '75FE88 17FF8C 175F14 1C07E2 3803C4 703182 F8EDFC',
-                'B2BBC2 BB6F84 31BFC2 18EA3C 0E3E00 07FC00 03F800',
-                '1E1800 1FF800>',
-                'endstream',
-                'endobj'
-                ], LINEEND) + LINEEND)
+class PDFEncoding(PDFType1Font):
+    Type = "Encoding"
+    name_attributes = string.split("Type BaseEncoding")
+    # these attributes are assumed to already be of the right type
+    local_attributes = ["Differences"]
+
+# skipping CMaps
 
-class PDFType1Font(PDFObject):
-    def __init__(self, key, font, encoding):
-        self.fontname = font
-        self.keyname = key
-        self.encoding = encoding
-        self.template = string.join([
-                    '<<',
-                    '/Type /Font',
-                    '/Subtype /Type1',
-                    '/Name /%s',
-                    '/BaseFont /%s',
-                    '/Encoding /%s',
-                    '>>'],
-                    LINEEND)
-    def save(self, file):
-        file.write(self.template %
-                   (self.keyname, self.fontname, self.encoding)
-                   + LINEEND)
+class PDFFormXObject:
+	# like page requires .info set by some higher level (doc)
+	# XXXX any resource used in a form must be propagated up to the page that (recursively) uses
+	#   the form!! (not implemented yet).
+	XObjects = Annots = BBox = Matrix = Contents = stream = Resources = None
+	hasImages = 1 # probably should change
+	compression = 0
+	def __init__(self, lowerx, lowery, upperx, uppery):
+		#not done
+		self.lowerx = lowerx; self.lowery=lowery; self.upperx=upperx; self.uppery=uppery
+		
+	def setStreamList(self, data):
+		if type(data) is types.ListType:
+			data = string.join(data, LINEEND)
+		self.stream = data
+		
+	def format(self, document):
+		self.BBox = self.BBox or PDFArray([self.lowerx, self.lowery, self.upperx, self.uppery])
+		self.Matrix = self.Matrix or PDFArray([1, 0, 0, 1, 0, 0])
+		if not self.Annots:
+			self.Annots = None
+		else:
+			raise ValueError, "annotations not reimplemented yet"
+		if not self.Contents:
+			stream = self.stream
+			if not stream:
+				self.Contents = teststream()
+			else:
+				S = PDFStream()
+				S.content = stream
+				# need to add filter stuff (?)
+				S.__Comment__ = "xobject form stream"
+				self.Contents = S
+		if not self.Resources:
+			resources = PDFResourceDictionary()
+			# fonts!
+			resources.basicFonts()
+			if self.hasImages:
+				resources.allProcs()
+			else:
+				resources.basicProcs()
+		sdict = self.Contents.dictionary
+		sdict["Type"] = PDFName("XObject")
+		sdict["Subtype"] = PDFName("Form")
+		sdict["FormType"] = 1
+		sdict["BBox"] = self.BBox
+		sdict["Matrix"] = self.Matrix
+		sdict["Resources"] = resources
+		return self.Contents.format(document)
 
-##############################################################
-#
-#            some helpers
-#
-##############################################################
-def MakeType1Fonts(encoding):
-    "returns a list of all the standard font objects"
-    fonts = []
-    pos = 1
-    for fontname in StandardEnglishFonts:
-        #These only work properly in StandardEncoding
-        if fontname in ['Symbol', 'ZapfDingbats']:
-            encUsed = 'StandardEncoding'	# Andy's Fix
-        else:
-            encUsed = encoding
-        font = PDFType1Font('F'+str(pos), fontname, encUsed)
-        fonts.append(font)
-        pos = pos + 1
-    return fonts
+if __name__=="__main__":
+    # first test
+    print "line end is", repr(LINEEND)
+    print "PDFName", PDFName("test")
+    D = PDFDocument()
+    print "PDFDict", PDFDictionary({"this":1}).format(D)
+    testpage(D)
+    txt = D.format()
+    fn = "test.pdf"
+    f = open(fn, "wb")
+    f.write(txt)
+    print "wrote", fn
 
-def MakeFontDictionary(startpos, count):
-    "returns a font dictionary assuming they are all in the file from startpos"    
-    dict = "  <<" + LINEEND
-    pos = startpos
-    for i in range(count):
-        dict = dict + '\t\t/F%d %d 0 R ' % (i + 1, startpos + i) + LINEEND
-    dict = dict + "\t\t>>" + LINEEND
-    return dict
-        
-if __name__ == '__main__':
-	#these tests are for memory leaks only
-	doc = PDFDocument()
+    
+            
\ No newline at end of file
--- a/reportlab/pdfgen/canvas.py	Sun Oct 15 22:02:57 2000 +0000
+++ b/reportlab/pdfgen/canvas.py	Wed Oct 18 05:03:21 2000 +0000
@@ -31,9 +31,13 @@
 #
 ###############################################################################
 #	$Log: canvas.py,v $
+#	Revision 1.53  2000/10/18 05:03:21  aaron_watters
+#	complete revision of pdfdoc.  Not finished (compression missing, testing needed)
+#	I got Robin's last change in at the last moment :)
+#
 #	Revision 1.52  2000/10/15 21:57:13  andy_robinson
 #	Added showFullScreen0
-#
+#	
 #	Revision 1.51  2000/09/04 08:06:15  rgbecker
 #	Fix spurious comment reference to layout
 #	
@@ -186,7 +190,7 @@
 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
 #	Added license, __version__ and Logi comment
 #	
-__version__=''' $Id: canvas.py,v 1.52 2000/10/15 21:57:13 andy_robinson Exp $ '''
+__version__=''' $Id: canvas.py,v 1.53 2000/10/18 05:03:21 aaron_watters Exp $ '''
 __doc__=""" 
 PDFgen is a library to generate PDF files containing text and graphics.  It is the 
 foundation for a complete reporting solution in Python.  It is also the
@@ -545,7 +549,7 @@
         self._doc.inPage() # try to enable page-only features
         pageref = self._doc.thisPageRef()
         dest.fit()
-        dest.setPageRef(pageref)
+        dest.setPage(pageref) # formatter won't make a ref to a ref
         return dest
         
     def bookmarkHorizontalAbsolute(self, key, yhorizontal):
@@ -560,7 +564,7 @@
         self._doc.inPage() # try to enable page-only features
         pageref = self._doc.thisPageRef()
         dest.fith(yhorizontal)
-        dest.setPageRef(pageref)
+        dest.setPage(pageref)
         return dest
         
     #def _inPage0(self):  disallowed!