]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-2647: Deduplicate PDF content stream
authorSimon Steiner <ssteiner@apache.org>
Thu, 15 Sep 2016 14:37:18 +0000 (14:37 +0000)
committerSimon Steiner <ssteiner@apache.org>
Thu, 15 Sep 2016 14:37:18 +0000 (14:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1760942 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java
fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java
fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java [new file with mode: 0644]

index fc08e17608cedc1f680e0f7737d9751bcecbae1a..156bf0354e1d54ec463bf94e045bcedc4299da4f 100644 (file)
@@ -118,9 +118,9 @@ public class PDFPage extends PDFResourceContext {
      *
      * @param contents the contents of the page
      */
-    public void setContents(PDFStream contents) {
+    public void setContents(PDFReference contents) {
         if (contents != null) {
-            put("Contents", new PDFReference(contents));
+            put("Contents", contents);
         }
     }
 
index d6dd98cf3e688c8bed1087625c08484cbe67e457..c5a8ad0835d2fae701f607ccc7379e8537f51dee 100644 (file)
 
 package org.apache.fop.pdf;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.util.Arrays;
 
 /**
  * Class representing a PDF stream.
@@ -191,4 +193,9 @@ public class PDFStream extends AbstractPDFStream {
         return len;
     }
 
+    public int streamHashCode() throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        outputRawStreamData(bos);
+        return Arrays.hashCode(bos.toByteArray());
+    }
 }
index b4e707defc980f4e665dc297b1d35121741de483..34a12cb149de39b1235402ff040f992cf27220af 100644 (file)
@@ -43,6 +43,7 @@ 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.pdf.PDFStream;
 import org.apache.fop.render.extensions.prepress.PageBoundaries;
 import org.apache.fop.render.extensions.prepress.PageScale;
 import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
@@ -97,6 +98,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
             = new PDFDocumentNavigationHandler(this);
 
     private Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
+    private Map<Integer, PDFReference> contents = new HashMap<Integer, PDFReference>();
 
     /**
      * Default constructor.
@@ -287,8 +289,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
         }
         try {
             this.documentNavigationHandler.commit();
-            this.pdfDoc.registerObject(generator.getStream());
-            currentPage.setContents(generator.getStream());
+            setUpContents();
             PDFAnnotList annots = currentPage.getAnnotations();
             if (annots != null) {
                 this.pdfDoc.addObject(annots);
@@ -304,6 +305,17 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
         }
     }
 
+    private void setUpContents() throws IOException {
+        PDFStream stream = generator.getStream();
+        int hash = stream.streamHashCode();
+        if (!contents.containsKey(hash)) {
+            pdfDoc.registerObject(stream);
+            PDFReference ref = new PDFReference(stream);
+            contents.put(hash, ref);
+        }
+        currentPage.setContents(contents.get(hash));
+    }
+
     /** {@inheritDoc} */
     public void handleExtensionObject(Object extension) throws IFException {
         if (extension instanceof XMPMetadata) {
index 19862480b43793da5465f3a770b90861b8e3bc3a..6bc19db44de6abcb6788c13fa631e8a3e24ef405 100644 (file)
@@ -42,6 +42,7 @@ import org.apache.fop.pdf.PDFFilterList;
 import org.apache.fop.pdf.PDFNumber;
 import org.apache.fop.pdf.PDFPage;
 import org.apache.fop.pdf.PDFPaintingState;
+import org.apache.fop.pdf.PDFReference;
 import org.apache.fop.pdf.PDFResources;
 import org.apache.fop.pdf.PDFStream;
 
@@ -279,7 +280,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D {
                 PDFFilterList.CONTENT_FILTER, false);
         pdfStream.add(getString());
         this.pdfDoc.registerObject(pdfStream);
-        pdfContext.getCurrentPage().setContents(pdfStream);
+        pdfContext.getCurrentPage().setContents(new PDFReference(pdfStream));
         PDFAnnotList annots = pdfContext.getCurrentPage().getAnnotations();
         if (annots != null) {
             this.pdfDoc.addObject(annots);
index 3df497e0f52ed2232b88c8a208f049f1148bfaf5..a41cf30e95db2ccfd35fff8ef8dbcc7d6585e00a 100644 (file)
@@ -62,7 +62,7 @@ public class PDFLinearizationTestCase {
             PDFPage page = new PDFPage(resources, i, f, f, f, f);
             doc.registerObject(page);
             doc.registerObject(gen.getStream());
-            page.setContents(gen.getStream());
+            page.setContents(new PDFReference(gen.getStream()));
         }
         gen.flushPDFDoc();
         byte[] data = out.toByteArray();
diff --git a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java
new file mode 100644 (file)
index 0000000..300e1f3
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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.render.pdf;
+
+import java.awt.Dimension;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.transform.stream.StreamResult;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.render.intermediate.IFContext;
+import org.apache.fop.render.intermediate.IFException;
+
+public class PDFDocumentHandlerTestCase {
+    @Test
+    public void testPageContentsDeduplicated() throws IFException {
+        FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
+        PDFDocumentHandler d = new PDFDocumentHandler(new IFContext(userAgent));
+        d.setFontInfo(new FontInfo());
+        OutputStream writer = new ByteArrayOutputStream();
+        StreamResult result = new StreamResult(writer);
+        d.setResult(result);
+        d.startDocument();
+
+        addPage(d, "a", 0);
+        addPage(d, "b", 1);
+        addPage(d, "a", 2);
+        d.endDocument();
+
+        List<String> contents = new ArrayList<String>();
+        for (String line : writer.toString().split("\n")) {
+            if (line.trim().startsWith("/Contents")) {
+                contents.add(line);
+            }
+        }
+        Assert.assertEquals(contents.size(), 3);
+        Assert.assertEquals(contents.get(0), contents.get(2));
+        Assert.assertFalse(contents.get(0).equals(contents.get(1)));
+    }
+
+    private void addPage(PDFDocumentHandler d, String command, int i) throws IFException {
+        d.startPage(i, "", "", new Dimension());
+        d.getGenerator().add(command);
+        d.endPage();
+    }
+}