Skip to content

Commit ea5d9b5

Browse files
author
Joao Goncalves
committed
FOP-3245 Add structure tree merging to external document
1 parent 2971201 commit ea5d9b5

File tree

9 files changed

+136
-0
lines changed

9 files changed

+136
-0
lines changed

fop-core/src/main/java/org/apache/fop/accessibility/fo/StructureTreeEventTrigger.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.fop.fo.FOText;
3636
import org.apache.fop.fo.FObj;
3737
import org.apache.fop.fo.extensions.ExtensionElementMapping;
38+
import org.apache.fop.fo.extensions.ExternalDocument;
3839
import org.apache.fop.fo.extensions.InternalElementMapping;
3940
import org.apache.fop.fo.flow.AbstractRetrieveMarker;
4041
import org.apache.fop.fo.flow.BasicLink;
@@ -407,6 +408,12 @@ public void image(ExternalGraphic eg) {
407408
endElement(eg);
408409
}
409410

411+
@Override
412+
public void startExternalDocument(ExternalDocument externalDocument) {
413+
startElementWithIDAndAltText(externalDocument, null);
414+
endElement(externalDocument);
415+
}
416+
410417
@Override
411418
public void startInstreamForeignObject(InstreamForeignObject ifo) {
412419
startElementWithIDAndAltText(ifo, ifo.getAltText());

fop-core/src/main/java/org/apache/fop/fo/extensions/ExternalDocument.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.xml.sax.Locator;
2323

24+
import org.apache.fop.accessibility.StructureTreeElement;
2425
import org.apache.fop.apps.FOPException;
2526
import org.apache.fop.datatypes.Length;
2627
import org.apache.fop.fo.FONode;
@@ -47,6 +48,7 @@ public class ExternalDocument extends AbstractPageSequence implements GraphicsPr
4748
private String src;
4849
private int textAlign;
4950
private Length width;
51+
private StructureTreeElement structureTreeElement;
5052
// Unused but valid items, commented out for performance:
5153
// private CommonAccessibility commonAccessibility;
5254
// private CommonAural commonAural;
@@ -194,5 +196,15 @@ public String getLocalName() {
194196
return "external-document";
195197
}
196198

199+
@Override
200+
public StructureTreeElement getStructureTreeElement() {
201+
return this.structureTreeElement;
202+
}
203+
204+
@Override
205+
public void setStructureTreeElement(StructureTreeElement element) {
206+
this.structureTreeElement = element;
207+
}
208+
197209
}
198210

fop-core/src/main/java/org/apache/fop/layoutmgr/ExternalDocumentLayoutManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ private void fillPage(String uri) {
197197
lineArea.addInlineArea(vp);
198198
lineArea.updateExtentsFromChildren();
199199
blockArea.addLineArea(lineArea);
200+
201+
TraitSetter.addStructureTreeElement(vp, fobj.getStructureTreeElement());
202+
200203
curPage.getPageViewport().getCurrentFlow().addBlock(blockArea);
201204
curPage.getPageViewport().getCurrentSpan().notifyFlowsFinished();
202205
}

fop-core/src/main/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class PDFStructureTreeBuilder implements StructureTreeEventHandler {
7171
addBuilder("character", new LanguageHolderBuilder(StandardStructureTypes.InlineLevelStructure.SPAN));
7272
addBuilder("external-graphic", new ImageBuilder());
7373
addBuilder("instream-foreign-object", new ImageBuilder());
74+
addBuilder("external-document", new ImageBuilder());
7475
addBuilder("inline", new InlineHolderBuilder());
7576
addBuilder("inline-container", StandardStructureTypes.Grouping.DIV);
7677
addBuilder("page-number", StandardStructureTypes.InlineLevelStructure.QUOTE);

fop-core/src/test/java/org/apache/fop/accessibility/fo/FO2StructureTreeConverterTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public void testCompleteDocument() throws Exception {
9393
testConverter("/org/apache/fop/fo/complete_document.fo");
9494
}
9595

96+
@Test
97+
public void testPDFUAMergeWithExternalDocument() throws Exception {
98+
testConverter("/org/apache/fop/fo/pdfua_with_external_document.fo");
99+
}
100+
96101
@Test
97102
public void testAbbreviationProperty() throws Exception {
98103
testConverter("abb.fo");
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* $Id$ */
19+
20+
package org.apache.fop.accessibility.fo;
21+
22+
import org.junit.Test;
23+
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.times;
27+
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.when;
29+
30+
import org.apache.fop.accessibility.StructureTreeEventHandler;
31+
import org.apache.fop.fo.Constants;
32+
import org.apache.fop.fo.FONode;
33+
import org.apache.fop.fo.PropertyList;
34+
import org.apache.fop.fo.expr.PropertyException;
35+
import org.apache.fop.fo.extensions.ExternalDocument;
36+
import org.apache.fop.fo.properties.CommonAccessibility;
37+
import org.apache.fop.fo.properties.StringProperty;
38+
39+
public class StructureTreeEventTriggerTestCase {
40+
41+
@Test
42+
public void testStructureTreeEventTrigger() throws PropertyException {
43+
PropertyList propertyList = mock(PropertyList.class);
44+
when(propertyList.get(Constants.PR_SOURCE_DOCUMENT)).thenReturn(StringProperty.getInstance(""));
45+
when(propertyList.get(Constants.PR_ROLE)).thenReturn(StringProperty.getInstance(""));
46+
CommonAccessibility mockCommonAccessibility = CommonAccessibility.getInstance(propertyList);
47+
48+
StructureTreeEventHandler mockHandler = mock(StructureTreeEventHandler.class);
49+
StructureTreeEventTrigger trigger = new StructureTreeEventTrigger(mockHandler);
50+
FONode mockParent = mock(FONode.class);
51+
ExternalDocument mockDoc = mock(ExternalDocument.class);
52+
when(mockDoc.getCommonAccessibility()).thenReturn(mockCommonAccessibility);
53+
when(mockDoc.getParent()).thenReturn(mockParent);
54+
55+
trigger.startExternalDocument(mockDoc);
56+
57+
verify(mockDoc, times(1)).setStructureTreeElement(any());
58+
verify(mockHandler, times(1)).startImageNode(any(), any(), any());
59+
60+
}
61+
}

fop-core/src/test/java/org/apache/fop/render/pdf/PDFStructureTreeBuilderTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

28+
import org.xml.sax.helpers.AttributesImpl;
29+
2830
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
2932
import static org.junit.Assert.assertNull;
3033

3134
import org.apache.fop.pdf.PDFDocument;
@@ -67,4 +70,16 @@ public void testMemoryLeak() throws IOException {
6770
assertNull(structElem.getParentStructElem());
6871
assertNull(structElem.getKids());
6972
}
73+
74+
@Test
75+
public void testExternalDocumentBuilder() {
76+
pdfFactory.getDocument().makeStructTreeRoot(null);
77+
78+
PDFStructElem elem = PDFStructureTreeBuilder.createStructureElement("external-document", new PDFStructElem(),
79+
new AttributesImpl(), pdfFactory, null);
80+
81+
assertNotNull("Must create a PDFStructElem", elem);
82+
assertEquals("Elem must be of type figure", StandardStructureTypes.Illustration.FIGURE,
83+
elem.getStructureType());
84+
}
7085
}

fop-core/src/test/resources/org/apache/fop/accessibility/fo/fo2StructureTree.xsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
</structure-tree>
4545
</xsl:template>
4646

47+
<!-- fo:page-sequence maps to structure-tree -->
48+
<xsl:template match="fox:external-document">
49+
<fo:external-document xmlns:fo="http://www.w3.org/1999/XSL/Format" fox:alt-text="">
50+
<xsl:apply-templates/>
51+
</fo:external-document>
52+
</xsl:template>
4753

4854
<!-- Declarations and Pagination and Layout Formatting Objects -->
4955
<xsl:template match="fo:static-content|fo:flow">
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Arial"
3+
xmlns:pdf="http://xmlgraphics.apache.org/fop/extensions/pdf" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">
4+
<fo:layout-master-set>
5+
<fo:simple-page-master master-name="simple" page-height="27.9cm" page-width="21.6cm">
6+
<fo:region-body />
7+
</fo:simple-page-master>
8+
</fo:layout-master-set>
9+
<fo:declarations>
10+
<x:xmpmeta xmlns:x="adobe:ns:meta/">
11+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
12+
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
13+
<dc:title>Document title</dc:title>
14+
<dc:creator>Document author</dc:creator>
15+
<dc:description>Document subject</dc:description>
16+
</rdf:Description>
17+
</rdf:RDF>
18+
</x:xmpmeta>
19+
</fo:declarations>
20+
<fo:page-sequence format="1" id="th_default_sequence1" master-reference="simple">
21+
<fo:flow flow-name="xsl-region-body">
22+
<fo:block>page 1</fo:block>
23+
</fo:flow>
24+
</fo:page-sequence>
25+
<fox:external-document src="/test/pdf/1.5/test.pdf"/>
26+
</fo:root>

0 commit comments

Comments
 (0)