You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by ss...@apache.org on 2015/02/06 14:19:09 UTC

svn commit: r1657799 [1/2] - in /xmlgraphics/fop/branches/Temp_PDFLinearization: src/java/org/apache/fop/pdf/ src/java/org/apache/fop/pdf/xref/ src/java/org/apache/fop/render/pdf/ src/java/org/apache/fop/svg/ test/java/org/apache/fop/pdf/ test/java/org...

Author: ssteiner
Date: Fri Feb  6 13:19:08 2015
New Revision: 1657799

URL: http://svn.apache.org/r1657799
Log:
FOP-2445: PDF Linearization

Added:
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java   (with props)
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java   (with props)
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java   (with props)
Modified:
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/AbstractPDFStream.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/BitmapImage.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/CompressedObject.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFAnnotList.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFArray.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFCIDFont.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDictionary.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDocument.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionJCE.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionManager.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFFactory.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGState.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGoTo.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFImageXObject.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFInfo.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLink.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObject.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFOutline.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFPages.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFReference.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFResources.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFRoot.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFStream.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CompressedObjectReference.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceStream.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceTable.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/TrailerDictionary.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFImageHandlerGraphics2D.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFImageHandlerSVG.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFRendererConfig.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFRendererOption.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/svg/PDFGraphics2D.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/AbstractPDFStreamTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/ObjectStreamManagerTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/ObjectStreamTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/PDFEncryptionJCETestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/PDFObjectTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/xref/CompressedObjectReferenceTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/xref/CrossReferenceObjectTest.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/xref/CrossReferenceStreamTestCase.java
    xmlgraphics/fop/branches/Temp_PDFLinearization/test/java/org/apache/fop/pdf/xref/CrossReferenceTableTestCase.java

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/AbstractPDFStream.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/AbstractPDFStream.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/AbstractPDFStream.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/AbstractPDFStream.java Fri Feb  6 13:19:08 2015
@@ -21,6 +21,7 @@ package org.apache.fop.pdf;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Set;
 
 import org.apache.commons.io.output.CountingOutputStream;
 
@@ -36,7 +37,9 @@ public abstract class AbstractPDFStream
     /** The filters that should be applied */
     private PDFFilterList filters;
 
-    private final boolean encodeOnTheFly;
+    private boolean encodeOnTheFly;
+
+    private PDFNumber refLength = new PDFNumber();
 
     protected AbstractPDFStream() {
         this(true);
@@ -220,11 +223,11 @@ public abstract class AbstractPDFStream
         StringBuilder textBuffer = new StringBuilder(64);
 
         StreamCache encodedStream = null;
-        PDFNumber refLength = null;
         final Object lengthEntry;
         if (encodeOnTheFly) {
-            refLength = new PDFNumber();
-            getDocumentSafely().registerObject(refLength);
+            if (!refLength.hasObjectNumber()) {
+                registerChildren();
+            }
             lengthEntry = refLength;
         } else {
             encodedStream = encodeStream();
@@ -281,4 +284,18 @@ public abstract class AbstractPDFStream
     protected boolean multipleFiltersAllowed() {
         return true;
     }
+
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        dictionary.getChildren(children);
+        if (encodeOnTheFly) {
+            children.add(refLength);
+        }
+    }
+
+    public void registerChildren() {
+        if (encodeOnTheFly) {
+            getDocument().registerObject(refLength);
+        }
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/BitmapImage.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/BitmapImage.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/BitmapImage.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/BitmapImage.java Fri Feb  6 13:19:08 2015
@@ -51,7 +51,7 @@ public class BitmapImage implements PDFI
      * @param mask the transparency mask reference if any
      */
     public BitmapImage(String k, int width, int height, byte[] data,
-                  String mask) {
+                  PDFReference mask) {
         this.key = k;
         this.height = height;
         this.width = width;
@@ -59,7 +59,7 @@ public class BitmapImage implements PDFI
         this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
         this.bitmaps = data;
         if (mask != null) {
-            maskRef = new PDFReference(mask);
+            maskRef = mask;
         }
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/CompressedObject.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/CompressedObject.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/CompressedObject.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/CompressedObject.java Fri Feb  6 13:19:08 2015
@@ -35,7 +35,7 @@ interface CompressedObject {
      *
      * @return the object number.
      */
-    int getObjectNumber();
+    PDFObjectNumber getObjectNumber();
 
     /**
      * Outputs this object's content into the given stream.

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFAnnotList.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFAnnotList.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFAnnotList.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFAnnotList.java Fri Feb  6 13:19:08 2015
@@ -21,6 +21,7 @@ package org.apache.fop.pdf;
 
 // Java
 import java.util.List;
+import java.util.Set;
 
 /**
  * class representing an object which is a list of annotations.
@@ -73,4 +74,9 @@ public class PDFAnnotList extends PDFObj
      * 19 0 R
      * ]
      */
+
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        PDFDictionary.getChildren(links, children);
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFArray.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFArray.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFArray.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFArray.java Fri Feb  6 13:19:08 2015
@@ -21,7 +21,9 @@ package org.apache.fop.pdf;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.commons.io.output.CountingOutputStream;
 
@@ -213,4 +215,14 @@ public class PDFArray extends PDFObject
         return cout.getCount();
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        List<Object> contents = new ArrayList<Object>();
+        for (Object c : values) {
+            if (!(c instanceof PDFReference)) {
+                contents.add(c);
+            }
+        }
+        PDFDictionary.getChildren(contents, children);
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFCIDFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFCIDFont.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFCIDFont.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFCIDFont.java Fri Feb  6 13:19:08 2015
@@ -21,6 +21,7 @@ package org.apache.fop.pdf;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.util.Set;
 
 import org.apache.fop.fonts.CIDFontType;
 
@@ -301,5 +302,20 @@ public class PDFCIDFont extends PDFObjec
         return bout.toByteArray();
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        super.getChildren(children);
+        if (cidMap != null) {
+            children.add(cidMap);
+            cidMap.getChildren(children);
+        }
+        children.add(descriptor);
+        descriptor.getChildren(children);
+        if (cmap != null) {
+            children.add(cmap);
+            cmap.getChildren(children);
+        }
+    }
+
 }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDictionary.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDictionary.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDictionary.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDictionary.java Fri Feb  6 13:19:08 2015
@@ -21,8 +21,12 @@ package org.apache.fop.pdf;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.io.output.CountingOutputStream;
 
@@ -30,7 +34,7 @@ import org.apache.commons.io.output.Coun
  * Class representing a PDF dictionary object
  */
 public class PDFDictionary extends PDFObject {
-
+    private boolean visited;
     /**
      * the entry map
      */
@@ -134,4 +138,28 @@ public class PDFDictionary extends PDFOb
         textBuffer.append(">>");
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        if (!visited) {
+            visited = true;
+            Map<String, Object> childrenMap = new HashMap<String, Object>(entries);
+            childrenMap.remove("Parent");
+            getChildren(childrenMap.values(), children);
+            visited = false;
+        }
+    }
+
+    public static void getChildren(Collection<Object> values, Set<PDFObject> children) {
+        for (Object x : values) {
+            if (x instanceof PDFReference) {
+                x = ((PDFReference) x).getObject();
+            }
+            if (x instanceof PDFObject) {
+                if (((PDFObject) x).hasObjectNumber()) {
+                    children.add((PDFObject) x);
+                }
+                ((PDFObject) x).getChildren(children);
+            }
+        }
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDocument.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDocument.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDocument.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFDocument.java Fri Feb  6 13:19:08 2015
@@ -78,18 +78,18 @@ public class PDFDocument {
     private Log log = LogFactory.getLog("org.apache.fop.pdf");
 
     /** the current character position */
-    private long position;
+    protected long position;
 
     /** the character position of each object */
-    private List<Long> indirectObjectOffsets = new ArrayList<Long>();
+    protected List<Long> indirectObjectOffsets = new ArrayList<Long>();
 
-    private Collection<PDFStructElem> structureTreeElements;
+    protected List<PDFStructElem> structureTreeElements;
 
     /** List of objects to write in the trailer */
-    private List<PDFObject> trailerObjects = new ArrayList<PDFObject>();
+    protected List<PDFObject> trailerObjects = new ArrayList<PDFObject>();
 
     /** the objects themselves */
-    private List<PDFObject> objects = new LinkedList<PDFObject>();
+    protected List<PDFObject> objects = new LinkedList<PDFObject>();
 
     /** Controls the PDF version of this document */
     private VersionController versionController;
@@ -128,6 +128,8 @@ public class PDFDocument {
     /** the counter for XObject numbering */
     private int xObjectCount;
 
+    protected int gStateObjectCount;
+
     /* TODO: Should be modified (works only for image subtype) */
     private Map<String, PDFXObject> xObjectsMap = new HashMap<String, PDFXObject>();
 
@@ -155,6 +157,8 @@ public class PDFDocument {
 
     private List<PDFLaunch> launches = new ArrayList<PDFLaunch>();
 
+    protected List<PDFPage> pageObjs = new ArrayList<PDFPage>();
+
     private List<PDFLayer> layers;
 
     private List<PDFNavigator> navigators;
@@ -169,6 +173,10 @@ public class PDFDocument {
 
     private boolean mergeFontsEnabled;
 
+    private boolean linearizationEnabled;
+
+    protected boolean outputStarted;
+
     /**
      * Creates an empty PDF document.
      *
@@ -348,6 +356,14 @@ public class PDFDocument {
     }
 
     /**
+     * Get the Structural Tree Collection for this document
+     * @return
+     */
+    public List<PDFStructElem> getStructureTreeElements() {
+        return structureTreeElements;
+    }
+
+    /**
      * Creates and returns a StructTreeRoot object.
      *
      * @param parentTree the value of the ParenTree entry
@@ -398,6 +414,9 @@ public class PDFDocument {
     public PDFObject registerObject(PDFObject obj) {
         assignObjectNumber(obj);
         addObject(obj);
+        if (obj instanceof AbstractPDFStream) {
+            ((AbstractPDFStream) obj).registerChildren();
+        }
         return obj;
     }
 
@@ -421,6 +440,9 @@ public class PDFDocument {
      * @param obj {@link PDFObject} to assign a number to
      */
     public void assignObjectNumber(PDFObject obj) {
+        if (outputStarted && isLinearizationEnabled()) {
+            throw new IllegalStateException("Can't assign number after start of output");
+        }
         if (obj == null) {
             throw new NullPointerException("obj must not be null");
         }
@@ -436,7 +458,7 @@ public class PDFDocument {
                     + "PDFObject already has a parent PDFDocument");
         }
 
-        obj.setObjectNumber(++this.objectcount);
+        obj.setObjectNumber(this);
 
         if (currentParent == null) {
             obj.setDocument(this);
@@ -485,6 +507,7 @@ public class PDFDocument {
         }
         if (obj instanceof PDFPage) {
             this.pages.notifyKidRegistered((PDFPage)obj);
+            pageObjs.add((PDFPage) obj);
         }
         if (obj instanceof PDFLaunch) {
             this.launches.add((PDFLaunch) obj);
@@ -551,7 +574,7 @@ public class PDFDocument {
     public void setEncryption(PDFEncryptionParams params) {
         getProfile().verifyEncryptionAllowed();
         fileIDGenerator = FileIDGenerator.getRandomFileIDGenerator();
-        this.encryption = PDFEncryptionManager.newInstance(++this.objectcount, params, this);
+        this.encryption = PDFEncryptionManager.newInstance(params, this);
         if (this.encryption != null) {
             PDFObject pdfObject = (PDFObject)this.encryption;
             addTrailerObject(pdfObject);
@@ -965,6 +988,7 @@ public class PDFDocument {
      * @throws IOException if there is an exception writing to the output stream
      */
     public void output(OutputStream stream) throws IOException {
+        outputStarted = true;
         //Write out objects until the list is empty. This approach (used with a
         //LinkedList) allows for output() methods to create and register objects
         //on the fly even during serialization.
@@ -974,9 +998,29 @@ public class PDFDocument {
         }
     }
 
-    private void streamIndirectObject(PDFObject o, OutputStream stream) throws IOException {
+    protected void writeTrailer(OutputStream stream, int first, int last, int size, long mainOffset, long startxref)
+            throws IOException {
+        TrailerOutputHelper trailerOutputHelper = mayCompressStructureTreeElements()
+                ? new CompressedTrailerOutputHelper()
+                : new UncompressedTrailerOutputHelper();
+        if (structureTreeElements != null) {
+            trailerOutputHelper.outputStructureTreeElements(stream);
+        }
+        TrailerDictionary trailerDictionary = createTrailerDictionary(mainOffset != 0);
+        if (mainOffset != 0) {
+            trailerDictionary.getDictionary().put("Prev", mainOffset);
+        }
+        trailerOutputHelper.outputCrossReferenceObject(stream, trailerDictionary, first, last, size);
+        String trailer = "\nstartxref\n" + startxref + "\n%%EOF\n";
+        stream.write(encode(trailer));
+    }
+
+    protected int streamIndirectObject(PDFObject o, OutputStream stream) throws IOException {
+        outputStarted = true;
         recordObjectOffset(o);
-        this.position += outputIndirectObject(o, stream);
+        int len = outputIndirectObject(o, stream);
+        this.position += len;
+        return len;
     }
 
     private void streamIndirectObjects(Collection<? extends PDFObject> objects, OutputStream stream)
@@ -987,7 +1031,7 @@ public class PDFDocument {
     }
 
     private void recordObjectOffset(PDFObject object) {
-        int index = object.getObjectNumber() - 1;
+        int index = object.getObjectNumber().getNumber() - 1;
         while (indirectObjectOffsets.size() <= index) {
             indirectObjectOffsets.add(null);
         }
@@ -1076,23 +1120,26 @@ public class PDFDocument {
             trailerOutputHelper.outputStructureTreeElements(stream);
         }
         streamIndirectObjects(trailerObjects, stream);
-        TrailerDictionary trailerDictionary = createTrailerDictionary();
-        long startxref = trailerOutputHelper.outputCrossReferenceObject(stream, trailerDictionary);
+        TrailerDictionary trailerDictionary = createTrailerDictionary(true);
+        long startxref = trailerOutputHelper.outputCrossReferenceObject(stream, trailerDictionary, 0,
+                indirectObjectOffsets.size(), indirectObjectOffsets.size());
         String trailer = "\nstartxref\n" + startxref + "\n%%EOF\n";
         stream.write(encode(trailer));
     }
 
     private boolean mayCompressStructureTreeElements() {
         return accessibilityEnabled
-                && versionController.getPDFVersion().compareTo(Version.V1_5) >= 0;
+                && versionController.getPDFVersion().compareTo(Version.V1_5) >= 0
+                && !isLinearizationEnabled();
     }
 
-    private TrailerDictionary createTrailerDictionary() {
+    private TrailerDictionary createTrailerDictionary(boolean addRoot) {
         FileIDGenerator gen = getFileIDGenerator();
-        TrailerDictionary trailerDictionary = new TrailerDictionary(this)
-                .setRoot(root)
-                .setInfo(info)
-                .setFileID(gen.getOriginalFileID(), gen.getUpdatedFileID());
+        TrailerDictionary trailerDictionary = new TrailerDictionary(this);
+        if (addRoot) {
+            trailerDictionary.setRoot(root).setInfo(info);
+        }
+        trailerDictionary.setFileID(gen.getOriginalFileID(), gen.getUpdatedFileID());
         if (isEncryptionActive()) {
             trailerDictionary.setEncryption(encryption);
         }
@@ -1117,7 +1164,8 @@ public class PDFDocument {
         /**
          * @return the offset of the cross-reference object (the value of startxref)
          */
-        long outputCrossReferenceObject(OutputStream stream, TrailerDictionary trailerDictionary)
+        long outputCrossReferenceObject(OutputStream stream, TrailerDictionary trailerDictionary,
+                                        int first, int last, int size)
                 throws IOException;
     }
 
@@ -1129,9 +1177,9 @@ public class PDFDocument {
         }
 
         public long outputCrossReferenceObject(OutputStream stream,
-                TrailerDictionary trailerDictionary) throws IOException {
+                TrailerDictionary trailerDictionary, int first, int last, int size) throws IOException {
             new CrossReferenceTable(trailerDictionary, position,
-                    indirectObjectOffsets).output(stream);
+                    indirectObjectOffsets, first, last, size).output(stream);
             return position;
         }
     }
@@ -1150,7 +1198,7 @@ public class PDFDocument {
         }
 
         public long outputCrossReferenceObject(OutputStream stream,
-                TrailerDictionary trailerDictionary) throws IOException {
+                TrailerDictionary trailerDictionary, int first, int last, int size) throws IOException {
             // Outputting the object streams should not have created new indirect objects
             assert objects.isEmpty();
             new CrossReferenceStream(PDFDocument.this, ++objectcount, trailerDictionary, position,
@@ -1175,4 +1223,13 @@ public class PDFDocument {
         }
         return fileIDGenerator;
     }
+
+    public boolean isLinearizationEnabled() {
+        return linearizationEnabled;
+    }
+
+    public void setLinearizationEnabled(boolean b) {
+        linearizationEnabled = b;
+    }
+
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionJCE.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionJCE.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionJCE.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionJCE.java Fri Feb  6 13:19:08 2015
@@ -621,11 +621,11 @@ public final class PDFEncryptionJCE exte
 
     private class EncryptionFilter extends PDFFilter {
 
-        private int streamNumber;
+        private PDFObjectNumber streamNumber;
 
         private int streamGeneration;
 
-        EncryptionFilter(int streamNumber, int streamGeneration) {
+        EncryptionFilter(PDFObjectNumber streamNumber, int streamGeneration) {
             this.streamNumber  = streamNumber;
             this.streamGeneration = streamGeneration;
         }
@@ -658,7 +658,7 @@ public final class PDFEncryptionJCE exte
                 out.flush();
                 return new CipherOutputStream(out, cipher);
             } else {
-                byte[] key = createEncryptionKey(streamNumber, streamGeneration);
+                byte[] key = createEncryptionKey(streamNumber.getNumber(), streamGeneration);
                 Cipher cipher = initCipher(key);
                 return new CipherOutputStream(out, cipher);
             }
@@ -666,7 +666,7 @@ public final class PDFEncryptionJCE exte
 
     }
 
-    private PDFEncryptionJCE(int objectNumber, PDFEncryptionParams params, PDFDocument pdf) {
+    private PDFEncryptionJCE(PDFObjectNumber objectNumber, PDFEncryptionParams params, PDFDocument pdf) {
         setObjectNumber(objectNumber);
         try {
             if (params.getEncryptionLengthInBits() == 256) {
@@ -692,7 +692,7 @@ public final class PDFEncryptionJCE exte
      * @return the newly created encryption object
      */
     public static PDFEncryption make(
-            int objectNumber, PDFEncryptionParams params, PDFDocument pdf) {
+            PDFObjectNumber objectNumber, PDFEncryptionParams params, PDFDocument pdf) {
         return new PDFEncryptionJCE(objectNumber, params, pdf);
     }
 
@@ -714,7 +714,7 @@ public final class PDFEncryptionJCE exte
             System.arraycopy(encryptedData, 0, storedData, 16, encryptedData.length);
             return storedData;
         } else {
-            byte[] key = createEncryptionKey(o.getObjectNumber(), o.getGeneration());
+            byte[] key = createEncryptionKey(o.getObjectNumber().getNumber(), o.getGeneration());
             return encryptWithKey(key, data);
         }
     }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionManager.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionManager.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionManager.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFEncryptionManager.java Fri Feb  6 13:19:08 2015
@@ -107,20 +107,21 @@ public final class PDFEncryptionManager
 
     /**
      * Creates a new PDFEncryption instance if PDF encryption is available.
-     * @param objnum PDF object number
      * @param params PDF encryption parameters
      * @param pdf the PDF document to encrypt
      * @return PDFEncryption the newly created instance, null if PDF encryption
      * is unavailable.
      */
-    public static PDFEncryption newInstance(int objnum, PDFEncryptionParams params,
+    public static PDFEncryption newInstance(PDFEncryptionParams params,
             PDFDocument pdf) {
         try {
+            PDFObjectNumber pdfObjectNumber = new PDFObjectNumber();
+            pdfObjectNumber.setDocument(pdf);
             Class<?> clazz = Class.forName("org.apache.fop.pdf.PDFEncryptionJCE");
             Method makeMethod = clazz.getMethod("make",
-                        new Class[] {int.class, PDFEncryptionParams.class, PDFDocument.class});
+                        new Class[] {PDFObjectNumber.class, PDFEncryptionParams.class, PDFDocument.class});
             Object obj = makeMethod.invoke(null,
-                        new Object[] {new Integer(objnum), params, pdf});
+                        new Object[] {pdfObjectNumber, params, pdf});
             return (PDFEncryption)obj;
         } catch (ClassNotFoundException e) {
             if (checkAvailableAlgorithms()) {

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFFactory.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFFactory.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFFactory.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFFactory.java Fri Feb  6 13:19:08 2015
@@ -105,7 +105,7 @@ public class PDFFactory {
      */
     public PDFRoot makeRoot(PDFPages pages) {
         //Make a /Pages object. This object is written in the trailer.
-        PDFRoot pdfRoot = new PDFRoot(++this.document.objectcount, pages);
+        PDFRoot pdfRoot = new PDFRoot(document, pages);
         pdfRoot.setDocument(getDocument());
         getDocument().addTrailerObject(pdfRoot);
         return pdfRoot;
@@ -117,7 +117,7 @@ public class PDFFactory {
      * @return a new PDF Pages object for adding pages to
      */
     public PDFPages makePages() {
-        PDFPages pdfPages = new PDFPages(++(this.document.objectcount));
+        PDFPages pdfPages = new PDFPages(getDocument());
         pdfPages.setDocument(getDocument());
         getDocument().addTrailerObject(pdfPages);
         return pdfPages;
@@ -129,7 +129,7 @@ public class PDFFactory {
      * @return a new PDF resources object
      */
     public PDFResources makeResources() {
-        PDFResources pdfResources = new PDFResources(++this.document.objectcount);
+        PDFResources pdfResources = new PDFResources(getDocument());
         pdfResources.setDocument(getDocument());
         getDocument().addTrailerObject(pdfResources);
         return pdfResources;
@@ -197,7 +197,14 @@ public class PDFFactory {
          * create a PDFPage with the next object number, the given
          * resources, contents and dimensions
          */
-        PDFPage page = new PDFPage(resources, pageIndex, mediaBox, cropBox, bleedBox, trimBox);
+        PDFResources res = getDocument().getFactory().makeResources();
+        if (getDocument().isLinearizationEnabled()) {
+            getDocument().trailerObjects.remove(resources);
+        }
+        res.setParentResources(resources);
+
+        PDFPage page = new PDFPage(res, pageIndex, mediaBox, cropBox, bleedBox, trimBox);
+        res.setParent(page);
 
         getDocument().assignObjectNumber(page);
         getDocument().getPages().addPage(page);
@@ -763,7 +770,7 @@ public class PDFFactory {
      * @return the new PDF outline object
      */
     public PDFOutline makeOutline(PDFOutline parent, String label,
-                                  String actionRef, boolean showSubItems) {
+                                  PDFReference actionRef, boolean showSubItems) {
         PDFOutline pdfOutline = new PDFOutline(label, actionRef, showSubItems);
         if (parent != null) {
             parent.addOutline(pdfOutline);
@@ -785,7 +792,7 @@ public class PDFFactory {
                                   PDFAction pdfAction, boolean showSubItems) {
         return pdfAction == null
                  ? null
-                 : makeOutline(parent, label, pdfAction.getAction(), showSubItems);
+                 : makeOutline(parent, label, new PDFReference(pdfAction.getAction()), showSubItems);
     }
 
     // This one is obsolete now, at least it isn't called from anywhere inside FOP
@@ -804,7 +811,7 @@ public class PDFFactory {
                                   boolean showSubItems) {
 
         String goToRef = getGoToReference(destination, yoffset);
-        return makeOutline(parent, label, goToRef, showSubItems);
+        return makeOutline(parent, label, new PDFReference(goToRef), showSubItems);
     }
 
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGState.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGState.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGState.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGState.java Fri Feb  6 13:19:08 2015
@@ -105,13 +105,17 @@ public class PDFGState extends PDFObject
     }
 
     private Map values = new java.util.HashMap();
+    private int objNum;
 
     /**
      * Returns the name of this object
      * @return the name
      */
     public String getName() {
-        return "GS" + getObjectNumber();
+        if (objNum == 0) {
+            objNum = ++getDocument().gStateObjectCount;
+        }
+        return "GS" + objNum;
     }
 
     /**

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGoTo.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGoTo.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGoTo.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFGoTo.java Fri Feb  6 13:19:08 2015
@@ -31,7 +31,7 @@ public class PDFGoTo extends PDFAction {
     /**
      * the pageReference
      */
-    private String pageReference;
+    private PDFReference pageReference;
     private String destination;
     private float xPosition;
     private float yPosition;
@@ -43,7 +43,9 @@ public class PDFGoTo extends PDFAction {
      */
     public PDFGoTo(String pageReference) {
         super();
-        setPageReference(pageReference);
+        if (pageReference != null) {
+            setPageReference(new PDFReference(pageReference));
+        }
     }
 
     /**
@@ -63,7 +65,7 @@ public class PDFGoTo extends PDFAction {
      *
      * @param pageReference the new page reference to use
      */
-    public void setPageReference(String pageReference) {
+    public void setPageReference(PDFReference pageReference) {
         this.pageReference = pageReference;
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java Fri Feb  6 13:19:08 2015
@@ -23,6 +23,7 @@ import java.awt.color.ColorSpace;
 import java.awt.color.ICC_Profile;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Set;
 
 import org.apache.commons.io.IOUtils;
 
@@ -155,4 +156,10 @@ public class PDFICCBasedColorSpace exten
         return sRGBProfile;
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        super.getChildren(children);
+        children.add(iccStream);
+        iccStream.getChildren(children);
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFImageXObject.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFImageXObject.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFImageXObject.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFImageXObject.java Fri Feb  6 13:19:08 2015
@@ -22,6 +22,7 @@ package org.apache.fop.pdf;
 // Java
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Set;
 
 /* modified by JKT to integrate with 0.12.0 */
 /* modified by Eric SCHAEFFER to integrate with 0.13.0 */
@@ -171,4 +172,14 @@ public class PDFImageXObject extends PDF
         return pdfimage.multipleFiltersAllowed();
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        super.getChildren(children);
+        PDFICCStream pdfICCStream = pdfimage.getICCStream();
+        if (pdfICCStream != null) {
+            children.add(pdfICCStream);
+            pdfICCStream.getChildren(children);
+        }
+    }
+
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFInfo.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFInfo.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFInfo.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFInfo.java Fri Feb  6 13:19:08 2015
@@ -22,7 +22,7 @@ package org.apache.fop.pdf;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.Date;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.TimeZone;
 
@@ -317,7 +317,7 @@ public class PDFInfo extends PDFObject {
             throw new IllegalArgumentException(key + " is a reserved keyword");
         }
         if (customProperties == null) {
-            customProperties = new HashMap<PDFName, String>();
+            customProperties = new LinkedHashMap<PDFName, String>();
         }
         customProperties.put(new PDFName(key), value);
     }

Added: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java?rev=1657799&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java (added)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java Fri Feb  6 13:19:08 2015
@@ -0,0 +1,422 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+package org.apache.fop.pdf;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.io.output.CountingOutputStream;
+
+public class PDFLinearization {
+    private PDFDocument doc;
+    private Map<PDFPage, Set<PDFObject>> pageObjsMap = new HashMap<PDFPage, Set<PDFObject>>();
+    private PDFDictionary linearDict;
+    private HintTable hintTable;
+
+    public PDFLinearization(PDFDocument doc) {
+        this.doc = doc;
+    }
+
+    static class HintTable extends PDFStream {
+        private List<PDFPage> pages;
+        int pageStartPos;
+        List<Integer> sharedLengths = new ArrayList<Integer>();
+        List<Integer> pageLengths = new ArrayList<Integer>();
+        List<Integer> contentStreamLengths = new ArrayList<Integer>();
+        List<Integer> objCount = new ArrayList<Integer>();
+        Map<String, int[]> hintGroups = new HashMap<String, int[]>();
+
+        public HintTable(PDFDocument doc) {
+            super(false);
+            doc.assignObjectNumber(this);
+            doc.addObject(this);
+            pages = doc.pageObjs;
+            for (int i = 0; i < pages.size(); i++) {
+                pageLengths.add(0);
+                contentStreamLengths.add(0);
+                objCount.add(0);
+            }
+            hintGroups.put("/C", new int[4]);
+            hintGroups.put("/L", new int[4]);
+            hintGroups.put("/I", new int[4]);
+            hintGroups.put("/E", new int[4]);
+            hintGroups.put("/O", new int[4]);
+            hintGroups.put("/V", new int[4]);
+        }
+
+        @Override
+        public PDFFilterList getFilterList() {
+            return new PDFFilterList(getDocument().isEncryptionActive());
+        }
+
+        @Override
+        protected void outputRawStreamData(OutputStream os) throws IOException {
+            CountingOutputStream bos = new CountingOutputStream(os);
+
+            //start header
+            writeULong(1, bos); //1
+            writeULong(pageStartPos, bos); //2
+            writeCard16(32, bos); //3
+            writeULong(0, bos); //4
+            writeCard16(32, bos); //5
+            writeULong(0, bos); //6
+            writeCard16(0, bos); //7
+            writeULong(0, bos); //8
+            writeCard16(32, bos); //9
+            writeCard16(0, bos); //10
+            writeCard16(0, bos); //11
+            writeCard16(0, bos); //12
+            writeCard16(4, bos); //13
+            //end header
+
+            for (PDFPage page : pages) {
+                writeULong(objCount.get(page.pageIndex) - 1, bos);
+            }
+            for (PDFPage page : pages) {
+                writeULong(pageLengths.get(page.pageIndex), bos);
+            }
+            for (PDFPage page : pages) {
+                writeULong(contentStreamLengths.get(page.pageIndex), bos);
+            }
+
+            writeSharedTable(bos);
+
+            for (Map.Entry<String, int[]> group : hintGroups.entrySet()) {
+                put(group.getKey(), bos.getCount());
+                for (int i : group.getValue()) {
+                    writeULong(i, bos);
+                }
+                if (group.getKey().equals("/C")) {
+                    writeULong(0, bos);
+                    writeCard16(0, bos);
+                }
+            }
+        }
+
+        private void writeSharedTable(CountingOutputStream bos) throws IOException {
+            put("/S", bos.getCount());
+
+            //Shared object hint table, header section
+            writeULong(0, bos); //1
+            writeULong(0, bos); //2
+            writeULong(sharedLengths.size(), bos); //3
+            writeULong(sharedLengths.size(), bos); //4
+            writeCard16(0, bos); //5
+            writeULong(0, bos); //6
+            writeCard16(32, bos); //7
+
+            for (int i : sharedLengths) {
+                writeULong(i, bos);
+            }
+            writeULong(0, bos);
+        }
+
+        private void writeCard16(int s, OutputStream bos) throws IOException {
+            byte b1 = (byte)((s >> 8) & 0xff);
+            byte b2 = (byte)(s & 0xff);
+            bos.write(b1);
+            bos.write(b2);
+        }
+
+        private void writeULong(int s, OutputStream bos) throws IOException {
+            byte b1 = (byte)((s >> 24) & 0xff);
+            byte b2 = (byte)((s >> 16) & 0xff);
+            byte b3 = (byte)((s >> 8) & 0xff);
+            byte b4 = (byte)(s & 0xff);
+            bos.write(b1);
+            bos.write(b2);
+            bos.write(b3);
+            bos.write(b4);
+        }
+    }
+
+    static class LinearPDFDictionary extends PDFDictionary {
+        private int lastsize = -1;
+
+        public LinearPDFDictionary(PDFDocument doc) {
+            put("Linearized", 1);
+            put("/L", 0);
+            PDFArray larray = new PDFArray();
+            larray.add(0);
+            larray.add(0);
+            put("/H", larray);
+            doc.assignObjectNumber(this);
+            getObjectNumber().getNumber();
+            put("/O", getObjectNumber().getNumber() + 3);
+            put("/E", 0);
+            put("/N", doc.pageObjs.size());
+            put("/T", 0);
+        }
+
+        public int output(OutputStream stream) throws IOException {
+            int size = super.output(stream);
+            int padding = lastsize - size + 32;
+            if (lastsize == -1) {
+                padding = 32;
+                lastsize = size;
+            }
+            writePadding(padding, stream);
+            return size + padding;
+        }
+    }
+
+
+    private Set<PDFObject> assignNumbers() throws IOException {
+        Set<PDFObject> page1Children = getPage1Children();
+        if (!doc.pageObjs.isEmpty()) {
+            for (int i = 1; i < doc.pageObjs.size(); i++) {
+                PDFPage page = doc.pageObjs.get(i);
+                Set<PDFObject> children = pageObjsMap.get(page);
+                for (PDFObject c : children) {
+                    if (!page1Children.contains(c) && c.hasObjectNumber()) {
+                        c.getObjectNumber().getNumber();
+                    }
+                }
+            }
+            for (PDFObject o : doc.objects) {
+                if (o instanceof PDFDests || o instanceof PDFOutline) {
+                    for (PDFObject c : getChildren(o)) {
+                        c.getObjectNumber().getNumber();
+                    }
+                }
+                if (o instanceof PDFInfo || o instanceof PDFPageLabels) {
+                    o.getObjectNumber().getNumber();
+                }
+            }
+            for (PDFObject o : doc.objects) {
+                if (!page1Children.contains(o)) {
+                    o.getObjectNumber().getNumber();
+                }
+            }
+        }
+        linearDict = new LinearPDFDictionary(doc);
+        for (PDFObject o : page1Children) {
+            o.getObjectNumber().getNumber();
+        }
+        sort(doc.objects);
+        return page1Children;
+    }
+
+    private void sort(List<PDFObject> objects) {
+        Collections.sort(objects, new Comparator<PDFObject>() {
+            public int compare(PDFObject o1, PDFObject o2) {
+                return ((Integer) o1.getObjectNumber().getNumber()).compareTo(o2.getObjectNumber().getNumber());
+            }
+        });
+    }
+
+    private Set<PDFObject> getChildren(PDFObject o) {
+        Set<PDFObject> children = new LinkedHashSet<PDFObject>();
+        children.add(o);
+        o.getChildren(children);
+        return children;
+    }
+
+    public void outputPages(OutputStream stream) throws IOException {
+        Collections.sort(doc.pageObjs, new Comparator<PDFPage>() {
+            public int compare(PDFPage o1, PDFPage o2) {
+                return ((Integer) o1.pageIndex).compareTo(o2.pageIndex);
+            }
+        });
+        doc.objects.addAll(doc.trailerObjects);
+        doc.trailerObjects = null;
+        if (doc.getStructureTreeElements() != null) {
+            doc.objects.addAll(doc.getStructureTreeElements());
+            doc.structureTreeElements = null;
+        }
+        for (int i = 0; i < doc.objects.size() * 2; i++) {
+            doc.indirectObjectOffsets.add(0L);
+        }
+        Set<PDFObject> page1Children = assignNumbers();
+        doc.streamIndirectObject(linearDict, new ByteArrayOutputStream());
+        for (PDFObject o : page1Children) {
+            doc.objects.remove(o);
+        }
+        int sizeOfRest = doc.objects.size();
+
+        ByteArrayOutputStream fakeHeaderTrailerStream = new ByteArrayOutputStream();
+        long topTrailer = doc.position;
+        doc.writeTrailer(fakeHeaderTrailerStream, sizeOfRest, page1Children.size() + 1,
+                page1Children.size() + sizeOfRest + 1, Long.MAX_VALUE, 0);
+        doc.position += fakeHeaderTrailerStream.size();
+
+        ByteArrayOutputStream pageStream = new ByteArrayOutputStream();
+        writeObjects(page1Children, pageStream, sizeOfRest + 1);
+        long trailerOffset = doc.position;
+        ByteArrayOutputStream footerTrailerStream = new ByteArrayOutputStream();
+        doc.writeTrailer(footerTrailerStream, 0, sizeOfRest, sizeOfRest, 0, topTrailer);
+        doc.position += footerTrailerStream.size();
+
+        linearDict.put("/L", doc.position);
+
+        PDFDocument.outputIndirectObject(linearDict, stream);
+        CountingOutputStream realTrailer = new CountingOutputStream(stream);
+        doc.writeTrailer(realTrailer, sizeOfRest, page1Children.size() + 1,
+                page1Children.size() + sizeOfRest + 1, trailerOffset, 0);
+        writePadding(fakeHeaderTrailerStream.size() - realTrailer.getCount(), stream);
+        for (PDFObject o : page1Children) {
+            PDFDocument.outputIndirectObject(o, stream);
+            if (o instanceof HintTable) {
+                break;
+            }
+        }
+        stream.write(pageStream.toByteArray());
+        stream.write(footerTrailerStream.toByteArray());
+    }
+
+    private Set<PDFObject> getPage1Children() throws IOException {
+        Set<PDFObject> page1Children = new LinkedHashSet<PDFObject>();
+        if (!doc.pageObjs.isEmpty()) {
+            PDFPage page1 = doc.pageObjs.get(0);
+            page1Children.add(doc.getRoot());
+            hintTable = new HintTable(doc);
+            page1Children.add(hintTable);
+            page1Children.add(page1);
+            page1.getChildren(page1Children);
+            doc.objects.remove(doc.getPages());
+            doc.objects.add(0, doc.getPages());
+            pageObjsMap.put(page1, page1Children);
+
+            for (int i = 1; i < doc.pageObjs.size(); i++) {
+                PDFPage page = doc.pageObjs.get(i);
+                pageObjsMap.put(page, getChildren(page));
+            }
+        }
+        return page1Children;
+    }
+
+    private static void writePadding(int padding, OutputStream stream) throws IOException {
+        for (int i = 0; i < padding; i++) {
+            stream.write(" ".getBytes("UTF-8"));
+        }
+    }
+
+    private void writeObjects(Set<PDFObject> children1, OutputStream pageStream, int sizeOfRest) throws IOException {
+        writePage1(children1, pageStream);
+        linearDict.put("/E", doc.position);
+        for (PDFPage page : doc.pageObjs) {
+            if (page.pageIndex != 0) {
+                writePage(page, pageStream);
+            }
+        }
+        while (!doc.objects.isEmpty()) {
+            PDFObject o = doc.objects.remove(0);
+            if (o instanceof PDFOutline) {
+                writeObjectGroup("/O", getChildren(o), pageStream);
+            } else if (o instanceof PDFDests) {
+                writeObjectGroup("/E", getChildren(o), pageStream);
+            } else if (o instanceof PDFInfo) {
+                writeObjectGroup("/I", getChildren(o), pageStream);
+            } else if (o instanceof PDFPageLabels) {
+                writeObjectGroup("/L", getChildren(o), pageStream);
+            } else if (o instanceof PDFStructTreeRoot) {
+                writeObjectGroup("/C", getChildren(o), pageStream);
+            } else {
+                doc.streamIndirectObject(o, pageStream);
+            }
+        }
+        linearDict.put("/T", doc.position + 8 + String.valueOf(sizeOfRest).length());
+    }
+
+    private void writeObjectGroup(String name, Set<PDFObject> objects, OutputStream pageStream)
+            throws IOException {
+        List<PDFObject> children = new ArrayList<PDFObject>(objects);
+        sort(children);
+
+        int[] values = hintTable.hintGroups.get(name);
+        values[0] = children.iterator().next().getObjectNumber().getNumber();
+        values[1] = (int) doc.position;
+        values[2] = children.size();
+        for (PDFObject o : children) {
+            values[3] += doc.streamIndirectObject(o, pageStream);
+            doc.objects.remove(o);
+        }
+    }
+
+    private void writePage1(Set<PDFObject> children1, OutputStream pageStream) throws IOException {
+        hintTable.pageStartPos = (int) doc.position;
+        OutputStream stream = new ByteArrayOutputStream();
+
+        Set<PDFObject> sharedChildren = getSharedObjects();
+
+        int page1Len = 0;
+        int objCount = 0;
+        int sharedCount = 0;
+        for (PDFObject o : children1) {
+            if (o instanceof HintTable) {
+                PDFArray a = (PDFArray) linearDict.get("/H");
+                a.set(0, doc.position);
+                doc.streamIndirectObject(o, stream);
+                a.set(1, doc.position - (Double)a.get(0));
+                stream = pageStream;
+            } else {
+                int len = doc.streamIndirectObject(o, stream);
+                if (o instanceof PDFStream && hintTable.contentStreamLengths.get(0) == 0) {
+                    hintTable.contentStreamLengths.set(0, len);
+                }
+                if (!(o instanceof PDFRoot)) {
+                    page1Len += len;
+                    objCount++;
+                }
+                if (sharedChildren.contains(o)) {
+                    hintTable.sharedLengths.set(sharedCount, len);
+                    sharedCount++;
+                }
+            }
+        }
+        hintTable.pageLengths.set(0, page1Len);
+        hintTable.objCount.set(0, objCount);
+    }
+
+    private Set<PDFObject> getSharedObjects() {
+        Set<PDFObject> pageSharedChildren = getChildren(doc.pageObjs.get(0));
+        for (int i = 0; i < pageSharedChildren.size(); i++) {
+            hintTable.sharedLengths.add(0);
+        }
+        return pageSharedChildren;
+    }
+
+    private void writePage(PDFPage page, OutputStream pageStream) throws IOException {
+        Set<PDFObject> children = pageObjsMap.get(page);
+        int pageLen = 0;
+        int objCount = 0;
+        for (PDFObject c : children) {
+            if (doc.objects.contains(c)) {
+                int len = doc.streamIndirectObject(c, pageStream);
+                if (c instanceof PDFStream) {
+                    hintTable.contentStreamLengths.set(page.pageIndex, len);
+                }
+                pageLen += len;
+                doc.objects.remove(c);
+                objCount++;
+            }
+        }
+        hintTable.pageLengths.set(page.pageIndex, pageLen);
+        hintTable.objCount.set(page.pageIndex, objCount);
+    }
+}

Propchange: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLinearization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLink.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLink.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLink.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFLink.java Fri Feb  6 13:19:08 2015
@@ -21,6 +21,7 @@ package org.apache.fop.pdf;
 
 // Java
 import java.awt.geom.Rectangle2D;
+import java.util.Set;
 
 /**
  * class representing an /Annot object of /Subtype /Link
@@ -142,5 +143,14 @@ public class PDFLink extends PDFObject {
 
         return true;
     }
+
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        super.getChildren(children);
+        if (action.hasObjectNumber()) {
+            children.add(action);
+        }
+        action.getChildren(children);
+    }
 }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObject.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObject.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObject.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObject.java Fri Feb  6 13:19:08 2015
@@ -22,6 +22,7 @@ package org.apache.fop.pdf;
 // Java
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -41,7 +42,8 @@ public abstract class PDFObject implemen
     /**
      * the object's number
      */
-    private int objnum;
+    private boolean hasObjNum;
+    private PDFObjectNumber objNum = new PDFObjectNumber();
 
     /**
      * the object's generation (0 in new documents)
@@ -60,11 +62,11 @@ public abstract class PDFObject implemen
      * Returns the object's number.
      * @return the PDF Object number
      */
-    public int getObjectNumber() {
-        if (this.objnum == 0) {
+    public PDFObjectNumber getObjectNumber() {
+        if (!hasObjNum) {
             throw new IllegalStateException("Object has no number assigned: " + this.toString());
         }
-        return this.objnum;
+        return objNum;
     }
 
     /**
@@ -88,23 +90,33 @@ public abstract class PDFObject implemen
      * @return True if it has an object number
      */
     public boolean hasObjectNumber() {
-        return this.objnum > 0;
+        return hasObjNum;
     }
 
     /**
      * Sets the object number
-     * @param objnum the object number
      */
-    public void setObjectNumber(int objnum) {
-        this.objnum = objnum;
+    public void setObjectNumber(PDFDocument document) {
+        objNum.setDocument(document);
+        hasObjNum = true;
         PDFDocument doc = getDocument();
         setParent(null);
         setDocument(doc); //Restore reference to PDFDocument after setting parent to null
         if (log.isTraceEnabled()) {
-            log.trace("Assigning " + this + " object number " + objnum);
+            log.trace("Assigning " + this + " object number " + objNum);
         }
     }
 
+    public void setObjectNumber(PDFObjectNumber objectNumber) {
+        objNum = objectNumber;
+        hasObjNum = true;
+    }
+
+    public void setObjectNumber(int objectNumber) {
+        objNum = new PDFObjectNumber(objectNumber);
+        hasObjNum = true;
+    }
+
     /**
      * Returns this object's generation.
      * @return the PDF Object generation
@@ -184,8 +196,7 @@ public abstract class PDFObject implemen
             throw new IllegalArgumentException(
                     "Cannot reference this object. It doesn't have an object number");
         }
-        String ref = getObjectNumber() + " " + getGeneration() + " R";
-        return ref;
+        return makeReference().toString();
     }
 
     /**
@@ -344,4 +355,7 @@ public abstract class PDFObject implemen
     protected boolean contentEquals(PDFObject o) {
         return this.equals(o);
     }
+
+    public void getChildren(Set<PDFObject> children) {
+    }
 }

Added: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java?rev=1657799&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java (added)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java Fri Feb  6 13:19:08 2015
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+package org.apache.fop.pdf;
+
+public class PDFObjectNumber {
+    private int num;
+    private PDFDocument doc;
+
+    public PDFObjectNumber() {
+    }
+    public PDFObjectNumber(int num) {
+        this.num = num;
+    }
+
+    public void setDocument(PDFDocument doc) {
+        this.doc = doc;
+    }
+
+    public int getNumber() {
+        if (num == 0 && doc != null) {
+//            assert doc.outputStarted;
+            num = ++doc.objectcount;
+        }
+        return num;
+    }
+
+    public String toString() {
+        return String.valueOf(getNumber());
+    }
+}

Propchange: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFObjectNumber.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFOutline.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFOutline.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFOutline.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFOutline.java Fri Feb  6 13:19:08 2015
@@ -22,6 +22,7 @@ package org.apache.fop.pdf;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.List;
+import java.util.Set;
 
 /**
  * <p>This represents a single Outline object in a PDF, including the root Outlines
@@ -58,7 +59,7 @@ public class PDFOutline extends PDFObjec
      */
     private String title;
 
-    private String actionRef;
+    private PDFReference actionRef;
 
     /**
      * Create a PDF outline with the title and action.
@@ -67,7 +68,7 @@ public class PDFOutline extends PDFObjec
      * @param action the action for this outline
      * @param openItem indicator of whether child items are visible or not
      */
-    public PDFOutline(String title, String action, boolean openItem) {
+    public PDFOutline(String title, PDFReference action, boolean openItem) {
         super();
         subentries = new java.util.ArrayList();
         count = 0;
@@ -170,4 +171,19 @@ public class PDFOutline extends PDFObjec
         return bout.toByteArray();
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        if (parent != null) {
+            children.add(parent);
+        }
+        if (first != null && last != null) {
+            children.add(first);
+            children.add(last);
+            first.getChildren(children);
+            last.getChildren(children);
+        }
+        if (actionRef != null) {
+            children.add(actionRef.getObject());
+        }
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFPages.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFPages.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFPages.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFPages.java Fri Feb  6 13:19:08 2015
@@ -50,11 +50,9 @@ public class PDFPages extends PDFObject
      * It must also be allocated an object ID (so that the kids
      * can refer to the parent) so that the XRef table needs to
      * be updated before this object is written.
-     *
-     * @param objnum the object's number
      */
-    public PDFPages(int objnum) {
-        setObjectNumber(objnum);
+    public PDFPages(PDFDocument document) {
+        setObjectNumber(document);
     }
 
     /**
@@ -81,9 +79,9 @@ public class PDFPages extends PDFObject
                 throw new IllegalStateException("A page already exists at index "
                         + idx + " (zero-based).");
             }
-            this.kids.set(idx, page.referencePDF());
+            this.kids.set(idx, page.makeReference());
         } else {
-            this.kids.add(page.referencePDF());
+            this.kids.add(page.makeReference());
         }
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFReference.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFReference.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFReference.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFReference.java Fri Feb  6 13:19:08 2015
@@ -31,7 +31,7 @@ import java.lang.ref.SoftReference;
  */
 public class PDFReference implements PDFWritable {
 
-    private int objectNumber;
+    private PDFObjectNumber objectNumber;
     private int generation;
 
     private Reference<PDFObject> objReference;
@@ -56,7 +56,7 @@ public class PDFReference implements PDF
         }
         String[] parts = ref.split(" ");
         assert parts.length == 3;
-        this.objectNumber = Integer.parseInt(parts[0]);
+        this.objectNumber = new PDFObjectNumber(Integer.parseInt(parts[0]));
         this.generation = Integer.parseInt(parts[1]);
         assert "R".equals(parts[2]);
     }
@@ -81,7 +81,7 @@ public class PDFReference implements PDF
      * Returns the object number.
      * @return the object number
      */
-    public int getObjectNumber() {
+    public PDFObjectNumber getObjectNumber() {
         return this.objectNumber;
     }
 
@@ -103,7 +103,7 @@ public class PDFReference implements PDF
 
     /** {@inheritDoc} */
     public void outputInline(OutputStream out, StringBuilder textBuffer) {
-        textBuffer.append(getObjectNumber()).append(' ').append(getGeneration()).append(" R");
+        textBuffer.append(getObjectNumber().getNumber()).append(' ').append(getGeneration()).append(" R");
     }
 
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFResources.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFResources.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFResources.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFResources.java Fri Feb  6 13:19:08 2015
@@ -52,7 +52,7 @@ public class PDFResources extends PDFDic
      */
     protected Set<PDFXObject> xObjects = new LinkedHashSet<PDFXObject>();
     /** Map of color spaces (key: color space name) */
-    protected Map<PDFName, PDFColorSpace> colorSpaces = new LinkedHashMap<PDFName, PDFColorSpace>();
+    protected Map<LazyName, PDFColorSpace> colorSpaces = new LinkedHashMap<LazyName, PDFColorSpace>();
 
     /** Map of ICC color spaces (key: ICC profile description) */
     protected Map<String, PDFICCBasedColorSpace> iccColorSpaces = new LinkedHashMap<String, PDFICCBasedColorSpace>();
@@ -68,13 +68,11 @@ public class PDFResources extends PDFDic
 
     /**
      * create a /Resources object.
-     *
-     * @param objnum the object's number
      */
-    public PDFResources(int objnum) {
+    public PDFResources(PDFDocument doc) {
         /* generic creation of object */
         super();
-        setObjectNumber(objnum);
+        setObjectNumber(doc);
     }
 
     public void addContext(PDFResourceContext c) {
@@ -156,7 +154,7 @@ public class PDFResources extends PDFDic
      * @param colorSpace the color space
      */
     public void addColorSpace(PDFColorSpace colorSpace) {
-        this.colorSpaces.put(new PDFName(colorSpace.getName()), colorSpace);
+        this.colorSpaces.put(new LazyName(colorSpace), colorSpace);
         if (colorSpace instanceof PDFICCBasedColorSpace) {
             PDFICCBasedColorSpace icc = (PDFICCBasedColorSpace)colorSpace;
             String desc = ColorProfileUtil.getICCProfileDescription(
@@ -165,6 +163,16 @@ public class PDFResources extends PDFDic
         }
     }
 
+    static class LazyName {
+        private PDFColorSpace colorSpace;
+        public LazyName(PDFColorSpace colorSpace) {
+            this.colorSpace = colorSpace;
+        }
+        public PDFName getName() {
+            return new PDFName(colorSpace.getName());
+        }
+    }
+
     /**
      * Returns a ICCBased color space by profile name.
      * @param desc the name of the color space
@@ -181,8 +189,12 @@ public class PDFResources extends PDFDic
      * @return the requested color space or null if it wasn't found
      */
     public PDFColorSpace getColorSpace(PDFName name) {
-        PDFColorSpace cs = this.colorSpaces.get(name);
-        return cs;
+        for (Map.Entry<LazyName, PDFColorSpace> x : colorSpaces.entrySet()) {
+            if (x.getKey().getName().equals(name)) {
+                return x.getValue();
+            }
+        }
+        return null;
     }
 
     /**
@@ -303,4 +315,43 @@ public class PDFResources extends PDFDic
         }
     }
 
+    @Override
+    public void getChildren(Set<PDFObject> children) {
+        getChildren(children, false);
+    }
+
+    private void getChildren(Set<PDFObject> children, boolean isParent) {
+        super.getChildren(children);
+        for (PDFDictionary f : fonts.values()) {
+            children.add(f);
+            f.getChildren(children);
+        }
+        for (PDFResourceContext c : contexts) {
+            for (PDFXObject x : c.getXObjects()) {
+                children.add(x);
+                x.getChildren(children);
+            }
+            for (PDFPattern x : c.getPatterns()) {
+                children.add(x);
+                x.getChildren(children);
+            }
+            for (PDFShading x : c.getShadings()) {
+                children.add(x);
+                x.getChildren(children);
+            }
+            for (PDFGState x : c.getGStates()) {
+                children.add(x);
+                x.getChildren(children);
+            }
+        }
+        if (!isParent) {
+            for (PDFColorSpace x : colorSpaces.values()) {
+                children.add((PDFObject)x);
+                ((PDFObject)x).getChildren(children);
+            }
+        }
+        if (parent != null) {
+            parent.getChildren(children, true);
+        }
+    }
 }

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFRoot.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFRoot.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFRoot.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFRoot.java Fri Feb  6 13:19:08 2015
@@ -50,6 +50,9 @@ public class PDFRoot extends PDFDictiona
      */
     public static final int PAGEMODE_FULLSCREEN = 3;
 
+    private final PDFDocument document;
+
+
     private static final PDFName[] PAGEMODE_NAMES = new PDFName[] {
         new PDFName("UseNone"),
         new PDFName("UseOutlines"),
@@ -67,9 +70,9 @@ public class PDFRoot extends PDFDictiona
      * @param objnum the object's number
      * @param pages the PDFPages object
      */
-    public PDFRoot(int objnum, PDFPages pages) {
-        super();
-        setObjectNumber(objnum);
+    public PDFRoot(PDFDocument document, PDFPages pages) {
+        this.document = document;
+        setObjectNumber(document);
         put("Type", new PDFName("Catalog"));
         setRootPages(pages);
         setLanguage("x-unknown");

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFStream.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFStream.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFStream.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/PDFStream.java Fri Feb  6 13:19:08 2015
@@ -187,7 +187,7 @@ public class PDFStream extends AbstractP
         final int len = super.output(stream);
 
         //Now that the data has been written, it can be discarded.
-        this.data = null;
+//        this.data = null;
         return len;
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CompressedObjectReference.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CompressedObjectReference.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CompressedObjectReference.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CompressedObjectReference.java Fri Feb  6 13:19:08 2015
@@ -22,15 +22,17 @@ package org.apache.fop.pdf.xref;
 import java.io.DataOutputStream;
 import java.io.IOException;
 
+import org.apache.fop.pdf.PDFObjectNumber;
+
 /**
  * A reference to an indirect object stored in an object stream. Contains the relevant
  * information to add to a cross-reference stream.
  */
 public class CompressedObjectReference implements ObjectReference {
 
-    private final int objectNumber;
+    private final PDFObjectNumber objectNumber;
 
-    private final int objectStreamNumber;
+    private final PDFObjectNumber objectStreamNumber;
 
     private final int index;
 
@@ -42,7 +44,7 @@ public class CompressedObjectReference i
      * object is to be found
      * @param index the index of the compressed object in the object stream
      */
-    public CompressedObjectReference(int objectNumber, int objectStreamNumber, int index) {
+    public CompressedObjectReference(PDFObjectNumber objectNumber, PDFObjectNumber objectStreamNumber, int index) {
         this.objectNumber = objectNumber;
         this.objectStreamNumber = objectStreamNumber;
         this.index = index;
@@ -50,16 +52,16 @@ public class CompressedObjectReference i
 
     public void output(DataOutputStream out) throws IOException {
         out.write(2);
-        out.writeLong(objectStreamNumber);
+        out.writeLong(objectStreamNumber.getNumber());
         out.write(0);
         out.write(index);
     }
 
-    public int getObjectNumber() {
+    public PDFObjectNumber getObjectNumber() {
         return objectNumber;
     }
 
-    public int getObjectStreamNumber() {
+    public PDFObjectNumber getObjectStreamNumber() {
         return objectStreamNumber;
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceStream.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceStream.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceStream.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceStream.java Fri Feb  6 13:19:08 2015
@@ -31,6 +31,7 @@ import org.apache.fop.pdf.PDFDictionary;
 import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFFilterList;
 import org.apache.fop.pdf.PDFName;
+import org.apache.fop.pdf.PDFObjectNumber;
 import org.apache.fop.pdf.PDFStream;
 
 /**
@@ -60,7 +61,10 @@ public class CrossReferenceStream extend
             objectReferences.add(offset == null ? null : new UncompressedObjectReference(offset));
         }
         for (CompressedObjectReference ref : compressedObjectReferences) {
-            this.objectReferences.set(ref.getObjectNumber() - 1, ref);
+            while (ref.getObjectNumber().getNumber() > objectReferences.size()) {
+                objectReferences.add(null);
+            }
+            this.objectReferences.set(ref.getObjectNumber().getNumber() - 1, ref);
         }
     }
 
@@ -77,7 +81,7 @@ public class CrossReferenceStream extend
             }
 
         };
-        helperStream.setObjectNumber(objectNumber);
+        helperStream.setObjectNumber(new PDFObjectNumber(objectNumber));
         helperStream.setDocument(document);
         ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
         DataOutputStream data = new DataOutputStream(byteArray);

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceTable.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceTable.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceTable.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/CrossReferenceTable.java Fri Feb  6 13:19:08 2015
@@ -35,10 +35,17 @@ public class CrossReferenceTable extends
 
     private final StringBuilder pdf = new StringBuilder(256);
 
+    private int last;
+    private int first;
+    private int size;
+
     public CrossReferenceTable(TrailerDictionary trailerDictionary, long startxref,
-            List<Long> location) {
+            List<Long> location, int first, int last, int size) {
         super(trailerDictionary, startxref);
         this.objectReferences = location;
+        this.first = first;
+        this.last = last;
+        this.size = size;
     }
 
     public void output(OutputStream stream) throws IOException {
@@ -47,10 +54,17 @@ public class CrossReferenceTable extends
     }
 
     private void outputXref() throws IOException {
-        pdf.append("xref\n0 ");
-        pdf.append(objectReferences.size() + 1);
-        pdf.append("\n0000000000 65535 f \n");
-        for (Long objectReference : objectReferences) {
+        if (first == 0) {
+            pdf.append("xref\n0 ");
+            pdf.append(last + 1);
+            pdf.append("\n0000000000 65535 f \n");
+        } else {
+            pdf.append("xref\n" + (first + 1) + " ");
+            pdf.append(last + "\n");
+        }
+        for (int i = first; i < first + last; i++) {
+            Long objectReference = objectReferences.get(i);
+            assert objectReference != null;
             final String padding = "0000000000";
             String s = String.valueOf(objectReference);
             if (s.length() > 10) {
@@ -66,7 +80,7 @@ public class CrossReferenceTable extends
         pdf.append("trailer\n");
         stream.write(PDFDocument.encode(pdf.toString()));
         PDFDictionary dictionary = trailerDictionary.getDictionary();
-        dictionary.put("/Size", objectReferences.size() + 1);
+        dictionary.put("/Size", size + 1);
         dictionary.output(stream);
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/TrailerDictionary.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/TrailerDictionary.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/TrailerDictionary.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/pdf/xref/TrailerDictionary.java Fri Feb  6 13:19:08 2015
@@ -87,7 +87,7 @@ public class TrailerDictionary {
         return this;
     }
 
-    PDFDictionary getDictionary() {
+    public PDFDictionary getDictionary() {
         return dictionary;
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java Fri Feb  6 13:19:08 2015
@@ -28,6 +28,7 @@ import java.io.OutputStream;
 import org.apache.fop.pdf.PDFColorHandler;
 import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFFilterList;
+import org.apache.fop.pdf.PDFLinearization;
 import org.apache.fop.pdf.PDFNumber;
 import org.apache.fop.pdf.PDFPaintingState;
 import org.apache.fop.pdf.PDFReference;
@@ -148,6 +149,9 @@ public class PDFContentGenerator {
      * @throws IOException if an error occurs while flushing the PDF objects
      */
     public void flushPDFDoc() throws IOException {
+        if (document.isLinearizationEnabled()) {
+            new PDFLinearization(document).outputPages(outputStream);
+        }
         this.document.output(this.outputStream);
     }
 

Modified: xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java?rev=1657799&r1=1657798&r2=1657799&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java (original)
+++ xmlgraphics/fop/branches/Temp_PDFLinearization/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java Fri Feb  6 13:19:08 2015
@@ -41,6 +41,7 @@ import org.apache.fop.pdf.PDFAnnotList;
 import org.apache.fop.pdf.PDFArray;
 import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFPage;
+import org.apache.fop.pdf.PDFReference;
 import org.apache.fop.pdf.PDFResources;
 import org.apache.fop.render.extensions.prepress.PageBoundaries;
 import org.apache.fop.render.extensions.prepress.PageScale;
@@ -180,9 +181,13 @@ public class PDFDocumentHandler extends
 
     /** {@inheritDoc} */
     public void endDocument() throws IFException {
+        pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
         try {
-            pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
-            pdfDoc.outputTrailer(this.outputStream);
+            if (pdfDoc.isLinearizationEnabled()) {
+                generator.flushPDFDoc();
+            } else {
+                pdfDoc.outputTrailer(this.outputStream);
+            }
             this.pdfDoc = null;
 
             pdfResources = null;
@@ -286,8 +291,11 @@ public class PDFDocumentHandler extends
                 this.pdfDoc.addObject(annots);
             }
             this.pdfDoc.addObject(currentPage);
-            this.generator.flushPDFDoc();
-            this.generator = null;
+
+            if (!pdfDoc.isLinearizationEnabled()) {
+                this.generator.flushPDFDoc();
+                this.generator = null;
+            }
         } catch (IOException ioe) {
             throw new IFException("I/O error in endPage()", ioe);
         }
@@ -329,17 +337,15 @@ public class PDFDocumentHandler extends
 
     static final class PageReference {
 
-        private final String pageRef;
+        private final PDFReference pageRef;
         private final Dimension pageDimension;
 
         private PageReference(PDFPage page, Dimension dim) {
-            // Avoid keeping references to PDFPage as memory usage is
-            // considerably increased when handling thousands of pages.
-            this.pageRef = page.makeReference().toString();
+            this.pageRef = page.makeReference();
             this.pageDimension = new Dimension(dim);
         }
 
-        public String getPageRef() {
+        public PDFReference getPageRef() {
             return this.pageRef;
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org