Author: ssteiner
Date: Thu Sep 15 14:37:18 2016
New Revision: 1760942
URL: http://svn.apache.org/viewvc?rev=1760942&view=rev
Log:
FOP-2647: Deduplicate PDF content stream
Added:
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java (with props)
Modified:
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java
Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java?rev=1760942&r1=1760941&r2=1760942&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java (original)
+++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFPage.java Thu Sep 15 14:37:18 2016
@@ -118,9 +118,9 @@ public class PDFPage extends PDFResource
*
* @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);
}
}
Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java?rev=1760942&r1=1760941&r2=1760942&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java (original)
+++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/pdf/PDFStream.java Thu Sep 15 14:37:18 2016
@@ -19,10 +19,12 @@
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 AbstractP
return len;
}
+ public int streamHashCode() throws IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ outputRawStreamData(bos);
+ return Arrays.hashCode(bos.toByteArray());
+ }
}
Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java?rev=1760942&r1=1760941&r2=1760942&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java (original)
+++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java Thu Sep 15 14:37:18 2016
@@ -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
= 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
}
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
}
}
+ 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) {
Modified: xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java?rev=1760942&r1=1760941&r2=1760942&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java (original)
+++ xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java Thu Sep 15 14:37:18 2016
@@ -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 exten
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);
Modified: xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java?rev=1760942&r1=1760941&r2=1760942&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java (original)
+++ xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java Thu Sep 15 14:37:18 2016
@@ -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();
Added: xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java?rev=1760942&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java (added)
+++ xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java Thu Sep 15 14:37:18 2016
@@ -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();
+ }
+}
Propchange: xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFDocumentHandlerTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.