From: Jeremias Maerki Date: Fri, 9 Sep 2016 14:54:05 +0000 (+0000) Subject: FOP-2646: Fixed NPE when an image is marked as an artifact (accessibility/PDF/UA... X-Git-Tag: fop-2_2~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=49ab3fd3edfb5fcdccb6794f573dbab6a457d8e9;p=xmlgraphics-fop.git FOP-2646: Fixed NPE when an image is marked as an artifact (accessibility/PDF/UA enabled). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1760033 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java index c7b2879f1..c08dd86c8 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java @@ -188,14 +188,16 @@ public class PDFPainter extends AbstractIFPainter { private void addStructTreeBBox(Rectangle rect) { if (accessEnabled && getDocumentHandler().getPDFDocument().getProfile().getPDFUAMode().isEnabled()) { PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement(); - PDFDictionary d = new PDFDictionary(); - int x = rect.x / 1000; - int y = rect.y / 1000; - int w = rect.width / 1000; - int h = rect.height / 1000; - d.put("BBox", new PDFArray(x, y, w, h)); - d.put("O", new PDFName("Layout")); - structElem.put("A", d); + if (structElem != null) { //structElem is null if the image is marked as an artifact + PDFDictionary d = new PDFDictionary(); + int x = rect.x / 1000; + int y = rect.y / 1000; + int w = rect.width / 1000; + int h = rect.height / 1000; + d.put("BBox", new PDFArray(x, y, w, h)); + d.put("O", new PDFName("Layout")); + structElem.put("A", d); + } } } diff --git a/fop-core/src/test/java/org/apache/fop/accessibility/fo/ArtifactImageBugTestCase.java b/fop-core/src/test/java/org/apache/fop/accessibility/fo/ArtifactImageBugTestCase.java new file mode 100644 index 000000000..97b37e05f --- /dev/null +++ b/fop-core/src/test/java/org/apache/fop/accessibility/fo/ArtifactImageBugTestCase.java @@ -0,0 +1,100 @@ +/* + * 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.accessibility.fo; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.MissingResourceException; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import org.junit.Test; + +import org.xml.sax.SAXException; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; +import org.apache.fop.render.intermediate.IFContext; +import org.apache.fop.render.intermediate.IFDocumentHandler; +import org.apache.fop.render.intermediate.IFSerializer; + +/** + * Test for reproducing a NullPointerException occurring with activated PDF/UA and when an image + * is marked as an artifact. + * @see FOP-2646 + */ +public class ArtifactImageBugTestCase { + + @Test + public void testMarkerStateTrackingBug() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + formatFO(needResource("artifact-image-npe.fo"), out, MimeConstants.MIME_PDF); + //checkPDF(out); + } + + private void formatFO(URL foFile, ByteArrayOutputStream out, String mimeFopIf) + throws IOException, SAXException, TransformerException { + FopFactory fopFactory = getFopFactory(); + FOUserAgent userAgent = fopFactory.newFOUserAgent(); + + if (mimeFopIf.equals(MimeConstants.MIME_FOP_IF)) { + IFSerializer serializer = new IFSerializer(new IFContext(userAgent)); + IFDocumentHandler targetHandler + = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF); + serializer.mimicDocumentHandler(targetHandler); + userAgent.setDocumentHandlerOverride(serializer); + } + + Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out); + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + Source src = new StreamSource(foFile.openStream()); + Result res = new SAXResult(fop.getDefaultHandler()); + transformer.transform(src, res); + } + + private FopFactory getFopFactory() throws IOException, SAXException { + return FopFactory.newInstance(new File(".").toURI(), + needResource("/org/apache/fop/pdf/PDFUA.xconf").openStream()); + } + + private URL needResource(String resourceName) { + return needResource(getClass(), resourceName); + } + + private URL needResource(Class contextClass, String resourceName) { + URL url = contextClass.getResource(resourceName); + if (url == null) { + throw new MissingResourceException("Resource not found: " + resourceName, + contextClass.getName(), resourceName); + } + return url; + } + +} diff --git a/fop-core/src/test/resources/org/apache/fop/accessibility/fo/artifact-image-npe.fo b/fop-core/src/test/resources/org/apache/fop/accessibility/fo/artifact-image-npe.fo new file mode 100644 index 000000000..d47bae097 --- /dev/null +++ b/fop-core/src/test/resources/org/apache/fop/accessibility/fo/artifact-image-npe.fo @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + PDF/UA test case provoking an NPE with artifact images. + The Apache Software Foundation + Demonstrates a NullPointerException a NullPointerException occurring with activated PDF/UA and when an image is marked as an artifact. See FOP-2646 + + + + + + + + + + + + + Chapter 1Chapter 1 + Some text and an image wrapped in an fo:wrapper that is marked as an artifact: + + Some text. + Chapter 2Chapter 2 + Some text. + + + + Chapter 3Chapter 3 + Some text. + + + + + + + Next Page Sequence. Does accessibility still work?Artifact + + + diff --git a/fop-core/src/test/resources/org/apache/fop/pdf/PDFUA.xconf b/fop-core/src/test/resources/org/apache/fop/pdf/PDFUA.xconf new file mode 100644 index 000000000..fc0e46892 --- /dev/null +++ b/fop-core/src/test/resources/org/apache/fop/pdf/PDFUA.xconf @@ -0,0 +1,20 @@ + + + + file:///C:/Dev/projects/SBS/workspace/fop-trunk/fop/ + + true + + + + + PDF/UA-1 + + + + + + + + +