You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ss...@apache.org on 2023/03/14 12:26:21 UTC
[xmlgraphics-fop-pdf-images] branch main updated: FOP-3123: Modified stream should be used in the cache key
This is an automated email from the ASF dual-hosted git repository.
ssteiner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/xmlgraphics-fop-pdf-images.git
The following commit(s) were added to refs/heads/main by this push:
new 13dc9e5 FOP-3123: Modified stream should be used in the cache key
13dc9e5 is described below
commit 13dc9e576730de702c27a7d9fb61d11fbcb0c818
Author: Simon Steiner <ss...@smartcommunications.com>
AuthorDate: Thu Mar 9 16:25:03 2023 +0000
FOP-3123: Modified stream should be used in the cache key
---
.../apache/fop/render/pdf/pdfbox/PDFCloner.java | 6 ++-
.../fop/render/pdf/pdfbox/PDFClonerTestCase.java | 55 ++++++++++++++++++++++
2 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/src/java/org/apache/fop/render/pdf/pdfbox/PDFCloner.java b/src/java/org/apache/fop/render/pdf/pdfbox/PDFCloner.java
index 37d9bc5..f184384 100644
--- a/src/java/org/apache/fop/render/pdf/pdfbox/PDFCloner.java
+++ b/src/java/org/apache/fop/render/pdf/pdfbox/PDFCloner.java
@@ -198,8 +198,12 @@ public class PDFCloner {
String newStream = writer.writeText(new PDStream(originalStream));
if (writer.keyUsed) {
filter = adapter.FILTER_FILTER;
- out.write(newStream.getBytes(PDFDocument.ENCODING));
+ byte[] bytes = newStream.getBytes(PDFDocument.ENCODING);
+ out.write(bytes);
out.close();
+ try (OutputStream originalStreamOS = originalStream.createUnfilteredStream()) {
+ originalStreamOS.write(bytes);
+ }
in = null;
}
} catch (IOException e) {
diff --git a/test/java/org/apache/fop/render/pdf/pdfbox/PDFClonerTestCase.java b/test/java/org/apache/fop/render/pdf/pdfbox/PDFClonerTestCase.java
index 4171be2..eb3ed40 100644
--- a/test/java/org/apache/fop/render/pdf/pdfbox/PDFClonerTestCase.java
+++ b/test/java/org/apache/fop/render/pdf/pdfbox/PDFClonerTestCase.java
@@ -19,8 +19,13 @@
package org.apache.fop.render.pdf.pdfbox;
import java.awt.geom.Rectangle2D;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
@@ -35,6 +40,7 @@ import org.apache.pdfbox.cos.COSString;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFResources;
+import org.apache.fop.pdf.PDFStream;
public class PDFClonerTestCase {
@Test
@@ -64,4 +70,53 @@ public class PDFClonerTestCase {
String cloned = (String) new PDFCloner(adapter).cloneForNewDocument(string);
Assert.assertArrayEquals(cloned.getBytes(PDFDocument.ENCODING), string.getBytes());
}
+
+ @Test
+ public void testStream() throws IOException {
+ PDFDocument doc = new PDFDocument("");
+ Rectangle2D rectangle = new Rectangle2D.Double();
+ PDFPage page = new PDFPage(new PDFResources(doc), 0, rectangle, rectangle, rectangle, rectangle);
+ page.setDocument(doc);
+ PDFBoxAdapter adapter = new PDFBoxAdapter(page, new HashMap<>(), null, new HashMap<>());
+ COSDictionary res = new COSDictionary();
+ COSDictionary child = new COSDictionary();
+ child.setBoolean("a", true);
+ res.setItem("a", child);
+ adapter.uniqueName = new UniqueName("a", res, false);
+ PDFStream cloneda = (PDFStream) new PDFCloner(adapter).cloneForNewDocument(getStream());
+ adapter.uniqueName = new UniqueName("b", res, false);
+ PDFStream clonedb = (PDFStream) new PDFCloner(adapter).cloneForNewDocument(getStream());
+ Assert.assertNotSame(cloneda, clonedb);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ cloneda.setDocument(doc);
+ clonedb.setDocument(doc);
+ setFilterMap(doc);
+ cloneda.output(bos);
+ clonedb.output(bos);
+ Assert.assertEquals(bos.toString(PDFDocument.ENCODING), "<< /Length 1 0 R /Subtype /Form >>\n"
+ + "stream\n"
+ + "/a97 tf\n\n"
+ + "endstream"
+ + "<< /Length 2 0 R /Subtype /Form >>\n"
+ + "stream\n"
+ + "/a98 tf\n\n"
+ + "endstream");
+ }
+
+ private void setFilterMap(PDFDocument doc) {
+ Map<String, List<String>> filterMap = new HashMap<>();
+ List<String> filterList = new ArrayList<>();
+ filterList.add("null");
+ filterMap.put("default", filterList);
+ doc.setFilterMap(filterMap);
+ }
+
+ private COSStream getStream() throws IOException {
+ COSStream stream = new COSStream();
+ stream.setItem(COSName.SUBTYPE, COSName.FORM);
+ try (OutputStream os = stream.createUnfilteredStream()) {
+ os.write("/a tf".getBytes(PDFDocument.ENCODING));
+ }
+ return stream;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org