From c756924a9bcfdb8ab7f01ec60e2c4e742966d60b Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Wed, 23 Oct 2024 10:37:39 +0100 Subject: [PATCH] FOP-3216: Add object number to struct elements for linearization --- .../java/org/apache/fop/pdf/PDFDocument.java | 3 ++ .../fop/pdf/PDFLinearizationTestCase.java | 46 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java index 12f14354e..acc41032f 100644 --- a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java +++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java @@ -395,6 +395,9 @@ public class PDFDocument { */ public void registerStructureElement(PDFStructElem structElem) { structureTreeElements.add(structElem); + if (linearizationEnabled) { + structElem.setObjectNumber(this); + } } /** diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java index a41cf30e9..796b44d74 100644 --- a/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFLinearizationTestCase.java @@ -31,13 +31,21 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; import org.junit.Assert; import org.junit.Test; 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.fonts.FontInfo; import org.apache.fop.render.intermediate.IFContext; import org.apache.fop.render.pdf.PDFContentGenerator; @@ -66,7 +74,37 @@ public class PDFLinearizationTestCase { } gen.flushPDFDoc(); byte[] data = out.toByteArray(); - checkPDF(data); + checkPDF(data, 5, 6); + } + + @Test + public void testAccessibility() throws Exception { + String fopxconf = "" + + "true" + + "" + + "true" + + ""; + String fo = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " aaa\n" + + " \n" + + " \n" + + "\n"; + FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI(), + new ByteArrayInputStream(fopxconf.getBytes())); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, fopFactory.newFOUserAgent(), out); + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes())); + Result res = new SAXResult(fop.getDefaultHandler()); + transformer.transform(src, res); + checkPDF(out.toByteArray(), 6, 18); } @Test @@ -94,7 +132,7 @@ public class PDFLinearizationTestCase { Assert.assertTrue(out.toString().contains("/Subtype /Image")); } - private void checkPDF(byte[] data) throws IOException { + private void checkPDF(byte[] data, int pagesObjNumber, int trailerCount) throws IOException { checkHintTable(data); InputStream is = new ByteArrayInputStream(data); Map objs = readObjs(is); @@ -120,13 +158,13 @@ public class PDFLinearizationTestCase { Assert.assertTrue(firstObj.endsWith("startxref0%%EOF")); int pageObjNumber = getValue("/O", firstObj); Assert.assertTrue(objs.get(pageObjNumber + " 0 obj").toString().contains("/Type /Page")); - Assert.assertTrue(objs.get("5 0 obj").toString().contains("/Type /Pages")); + Assert.assertTrue(objs.get(pagesObjNumber + " 0 obj").toString().contains("/Type /Pages")); int total = 0; for (int i : objects) { total += i; } - Assert.assertEquals(total, objs.size() - 6); + Assert.assertEquals(total, objs.size() - trailerCount); } private void checkFirstObj(byte[] data) throws IOException { -- 2.39.5