Skip to content

Commit

Permalink
FOP-3183: Disable pattern modification using xobjforms
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Jun 6, 2024
1 parent 8fd6e66 commit a74500f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
9 changes: 4 additions & 5 deletions src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Object createStreamFromPDFBoxPage(PDDocument sourceDoc, PDPage sourcePage
AffineTransform pageAdjust, FontInfo fontinfo, Rectangle destRect)
throws IOException {
COSDictionary sourcePageResources = getResources(sourcePage);
PatternUtil patternUtil = new PatternUtil(targetPage, destRect, sourcePage);
PatternUtil patternUtil = new PatternUtil(targetPage, destRect, sourcePage, pdfDoc.isFormXObjectEnabled());
uniqueName = new UniqueName(
key, sourcePageResources, patternUtil.getPatternNames(), pdfDoc.isFormXObjectEnabled(), destRect);
key = patternUtil.getKey(key);
Expand Down Expand Up @@ -208,10 +208,8 @@ public Object createStreamFromPDFBoxPage(PDDocument sourceDoc, PDPage sourcePage
pdStream = new PDStream(sourceDoc, new ByteArrayInputStream(newStream.getBytes(PDFDocument.ENCODING)));
}
mergeXObj(sourcePageResources, fontinfo, uniqueName);

List<COSName> exclude = Collections.singletonList(COSName.PATTERN);
PDFDictionary pageResources =
(PDFDictionary)cloneForNewDocument(sourcePageResources, sourcePageResources, exclude);
(PDFDictionary)cloneForNewDocument(sourcePageResources, sourcePageResources, patternUtil.getExclude());

updateMergeFontInfo(pageResources, fontinfo);
updateXObj(sourcePageResources, pageResources);
Expand All @@ -233,7 +231,8 @@ public Object createStreamFromPDFBoxPage(PDDocument sourceDoc, PDPage sourcePage
// IOUtils.copyLarge(in, out);
// filter = FILTER_FILTER;
// } else {
pageStream = (PDFStream)cloneForNewDocument(originalPageContents, originalPageContents, exclude);
pageStream = (PDFStream)cloneForNewDocument(originalPageContents, originalPageContents,
patternUtil.getExclude());
filter = Collections.EMPTY_SET;
// }
if (pageStream == null) {
Expand Down
42 changes: 27 additions & 15 deletions src/java/org/apache/fop/render/pdf/pdfbox/PatternUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.pdfbox.cos.COSName;
Expand All @@ -42,16 +43,18 @@ public class PatternUtil {
private Rectangle pos;
private PDPage sourcePage;

public PatternUtil(PDFPage targetPage, Rectangle pos, PDPage sourcePage) throws IOException {
this.targetPage = targetPage;
this.pos = pos;
this.sourcePage = sourcePage;
PDResources srcPgResources = sourcePage.getResources();
if (srcPgResources != null) {
for (COSName name : srcPgResources.getPatternNames()) {
patternNames.add(name);
public PatternUtil(PDFPage targetPage, Rectangle pos, PDPage sourcePage, boolean disabled) throws IOException {
if (!disabled) {
this.targetPage = targetPage;
this.pos = pos;
this.sourcePage = sourcePage;
PDResources srcPgResources = sourcePage.getResources();
if (srcPgResources != null) {
for (COSName name : srcPgResources.getPatternNames()) {
patternNames.add(name);
}
transformPatterns();
}
transformPatterns();
}
}

Expand Down Expand Up @@ -96,12 +99,14 @@ private AffineTransform getShadingAffineTransform() {
}

public void promotePatterns() {
PDFDictionary patternsDict = (PDFDictionary) targetPage.getPDFResources().get(COSName.PATTERN.getName());
if (patternsDict != null) {
for (String key : patternsDict.keySet()) {
PDFObject pattern = (PDFObject) patternsDict.get(key);
pattern.setObjectNumber(targetPage.getDocument());
targetPage.getDocument().addObject(pattern);
if (targetPage != null) {
PDFDictionary patternsDict = (PDFDictionary) targetPage.getPDFResources().get(COSName.PATTERN.getName());
if (patternsDict != null) {
for (String key : patternsDict.keySet()) {
PDFObject pattern = (PDFObject) patternsDict.get(key);
pattern.setObjectNumber(targetPage.getDocument());
targetPage.getDocument().addObject(pattern);
}
}
}
}
Expand All @@ -112,4 +117,11 @@ public String getKey(String key) {
}
return key + pos.getX() + pos.getY() + pos.getWidth() + pos.getHeight();
}

public List<COSName> getExclude() {
if (targetPage == null) {
return Collections.emptyList();
}
return Collections.singletonList(COSName.PATTERN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,25 @@ public void testPatternMatrix() throws Exception {
+ ">>"));
}

@Test
public void testPatternMatrixFormXObject() throws Exception {
PDDocument doc = load(SHADING);
PDFDocument pdfdoc = new PDFDocument("");
pdfdoc.setFormXObjectEnabled(true);
Rectangle destRect = new Rectangle(0, 1650, 274818, 174879);
loadPage(pdfdoc, doc, destRect);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
pdfdoc.output(bos);
String outStr = bos.toString("UTF-8").replaceAll("\\s\\s/", "/");
Assert.assertTrue(outStr.contains("/Pattern << /Pa1 12 0 R /Pa2 13 0 R >>"));
Assert.assertTrue(outStr.contains("<<\n"
+ "/Type /Pattern\n"
+ "/PatternType 2\n"
+ "/Shading 2 0 R\n"
+ "/Matrix [120 0 0 -120 162 705]\n"
+ ">>"));
}

@Test
public void testAscenderDoesntMatch() throws IOException {
FontInfo fi = new FontInfo();
Expand Down

0 comments on commit a74500f

Please sign in to comment.