Skip to content

Commit

Permalink
FOP-3216: Add object number to struct elements for linearization
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Oct 23, 2024
1 parent 6aea002 commit c756924
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ public PDFStructTreeRoot makeStructTreeRoot(PDFParentTree parentTree) {
*/
public void registerStructureElement(PDFStructElem structElem) {
structureTreeElements.add(structElem);
if (linearizationEnabled) {
structElem.setObjectNumber(this);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@
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;
Expand Down Expand Up @@ -66,7 +74,37 @@ public void testPDF() throws IOException {
}
gen.flushPDFDoc();
byte[] data = out.toByteArray();
checkPDF(data);
checkPDF(data, 5, 6);
}

@Test
public void testAccessibility() throws Exception {
String fopxconf = "<fop version=\"1.0\">"
+ "<accessibility>true</accessibility>"
+ "<renderers><renderer mime=\"application/pdf\">"
+ "<linearization>true</linearization>"
+ "</renderer></renderers></fop>";
String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n"
+ " <fo:layout-master-set>\n"
+ " <fo:simple-page-master master-name=\"simple\">\n"
+ " <fo:region-body/>\n"
+ " </fo:simple-page-master>\n"
+ " </fo:layout-master-set>\n"
+ " <fo:page-sequence master-reference=\"simple\">\n"
+ " <fo:flow flow-name=\"xsl-region-body\">\n"
+ " <fo:block>aaa<fo:block break-before=\"page\"/></fo:block>\n"
+ " </fo:flow>\n"
+ " </fo:page-sequence>\n"
+ "</fo:root>\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
Expand Down Expand Up @@ -94,7 +132,7 @@ public void testImage() throws Exception {
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<String, StringBuilder> objs = readObjs(is);
Expand All @@ -120,13 +158,13 @@ private void checkPDF(byte[] data) throws IOException {
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 {
Expand Down

0 comments on commit c756924

Please sign in to comment.