git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1760942 13f79535-47bb-0310-9956-ffa450edef68tags/fop-2_2
@@ -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); | |||
} | |||
} | |||
@@ -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 AbstractPDFStream { | |||
return len; | |||
} | |||
public int streamHashCode() throws IOException { | |||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |||
outputRawStreamData(bos); | |||
return Arrays.hashCode(bos.toByteArray()); | |||
} | |||
} |
@@ -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) { |
@@ -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); |
@@ -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(); |
@@ -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(); | |||
} | |||
} |