Skip to content

Commit d5ea4d1

Browse files
Joao Goncalvesjoao12021996
authored andcommitted
FOP-3282 Duplicate spanned table header when static region per page conf is true
1 parent 1d1c280 commit d5ea4d1

File tree

2 files changed

+109
-5
lines changed

2 files changed

+109
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ public PDFStructElem build(StructureHierarchyMember parent, Attributes attribute
355355

356356
private PDFStructElem rootStructureElement;
357357

358+
private boolean staticContent;
359+
358360
void setPdfFactory(PDFFactory pdfFactory) {
359361
this.pdfFactory = pdfFactory;
360362
}
@@ -401,6 +403,9 @@ public void endPageSequence() {
401403
}
402404

403405
public StructureTreeElement startNode(String name, Attributes attributes, StructureTreeElement parent) {
406+
if ("static-content".equals(name)) {
407+
staticContent = true;
408+
}
404409
if (!isPDFA1Safe(name)) {
405410
return null;
406411
}
@@ -411,7 +416,7 @@ public StructureTreeElement startNode(String name, Attributes attributes, Struct
411416
parentElem = parent;
412417
}
413418
StructureTreeElement structElem;
414-
if (pdfFactory.getDocument().isStaticRegionsPerPageForAccessibility()) {
419+
if (staticContent && pdfFactory.getDocument().isStaticRegionsPerPageForAccessibility()) {
415420
structElem = new Factory(name, parentElem, attributes);
416421
} else {
417422
structElem = createStructureElement(
@@ -459,6 +464,10 @@ public PDFStructElem createStructureElement(int pageNumber) {
459464
}
460465

461466
public void endNode(String name) {
467+
if ("static-content".equals(name)) {
468+
staticContent = false;
469+
}
470+
462471
if (isPDFA1Safe(name)) {
463472
ancestors.removeFirst();
464473
}

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.File;
2424
import java.util.Collection;
25+
import java.util.List;
2526

2627
import javax.xml.transform.Result;
2728
import javax.xml.transform.Source;
@@ -30,13 +31,16 @@
3031
import javax.xml.transform.sax.SAXResult;
3132
import javax.xml.transform.stream.StreamSource;
3233

33-
import org.junit.Assert;
3434
import org.junit.Test;
35+
import org.xml.sax.helpers.AttributesImpl;
36+
import static org.junit.Assert.assertEquals;
3537

3638
import org.apache.fop.apps.FOUserAgent;
3739
import org.apache.fop.apps.Fop;
3840
import org.apache.fop.apps.FopFactory;
3941
import org.apache.fop.pdf.PDFLinearizationTestCase;
42+
import org.apache.fop.pdf.PDFStructElem;
43+
import org.apache.fop.pdf.StandardStructureTypes;
4044

4145
public class PDFStructureTreeTestCase {
4246
@Test
@@ -65,16 +69,107 @@ public void testStaticRegionPerPage() throws Exception {
6569
for (StringBuilder sb : objs) {
6670
String obj = sb.toString();
6771
if (obj.contains("/Type /MCR")) {
68-
Assert.assertEquals(obj, obj.split("/Pg ").length, 2);
72+
assertEquals(obj, obj.split("/Pg ").length, 2);
6973
count++;
7074
}
7175
}
72-
Assert.assertEquals(count, 4);
76+
assertEquals(count, 4);
7377
}
7478

75-
private ByteArrayOutputStream foToOutput(String fo) throws Exception {
79+
@Test
80+
public void testTableHeaderDuplicatedIfStaticRegionsPerPageTrue() throws Exception {
81+
List<PDFStructElem> elems = getPDFStructElems("<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" "
82+
+ "font-family=\"arial\" font-size=\"16pt\" xml:lang=\"en\">\n"
83+
+ " <fo:layout-master-set>\n"
84+
+ " <fo:simple-page-master master-name=\"A5-Page\" page-width=\"148mm\" page-height=\"210mm\">\n"
85+
+ " <fo:region-body background-color=\"#efefef\" margin=\"10mm\"/>\n"
86+
+ " </fo:simple-page-master>\n"
87+
+ " <fo:page-sequence-master master-name=\"A5\">\n"
88+
+ " <fo:repeatable-page-master-alternatives>\n"
89+
+ " <fo:conditional-page-master-reference master-reference=\"A5-Page\"/>\n"
90+
+ " </fo:repeatable-page-master-alternatives>\n"
91+
+ " </fo:page-sequence-master>\n"
92+
+ " </fo:layout-master-set>\n"
93+
+ " <fo:page-sequence master-reference=\"A5\">\n"
94+
+ " <fo:flow flow-name=\"xsl-region-body\">\n"
95+
+ " <fo:block>\n"
96+
+ " <fo:block padding-bottom=\"160mm\">Table overflow</fo:block>\n"
97+
+ " <fo:table table-layout=\"fixed\" width=\"100%\">\n"
98+
+ " <fo:table-header>\n"
99+
+ " <fo:table-row>\n"
100+
+ " <fo:table-cell number-columns-spanned=\"2\">\n"
101+
+ " <fo:block font-weight=\"bold\" font-style=\"italic\" text-align=\"left\" "
102+
+ "text-align-last=\"center\"> Table Title </fo:block>\n"
103+
+ " </fo:table-cell>\n"
104+
+ " </fo:table-row>\n"
105+
+ " </fo:table-header>\n"
106+
+ " <fo:table-body>\n"
107+
+ " <fo:table-row>\n"
108+
+ " <fo:table-cell>\n"
109+
+ " <fo:block>Row 1 Column A</fo:block>\n"
110+
+ " </fo:table-cell>\n"
111+
+ " <fo:table-cell>\n"
112+
+ " <fo:block>Row 1 Column B</fo:block>\n"
113+
+ " </fo:table-cell>\n"
114+
+ " </fo:table-row>\n"
115+
+ " <fo:table-row>\n"
116+
+ " <fo:table-cell>\n"
117+
+ " <fo:block>Row 2 Column A</fo:block>\n"
118+
+ " </fo:table-cell>\n"
119+
+ " <fo:table-cell>\n"
120+
+ " <fo:block>Row 2 Column B</fo:block>\n"
121+
+ " </fo:table-cell>\n"
122+
+ " </fo:table-row>\n"
123+
+ " <fo:table-row>\n"
124+
+ " <fo:table-cell>\n"
125+
+ " <fo:block>Row 2 Column A</fo:block>\n"
126+
+ " </fo:table-cell>\n"
127+
+ " <fo:table-cell>\n"
128+
+ " <fo:block>Row 2 Column B</fo:block>\n"
129+
+ " </fo:table-cell>\n"
130+
+ " </fo:table-row>\n"
131+
+ " <fo:table-row>\n"
132+
+ " <fo:table-cell>\n"
133+
+ " <fo:block>Row 2 Column A</fo:block>\n"
134+
+ " </fo:table-cell>\n"
135+
+ " <fo:table-cell>\n"
136+
+ " <fo:block>Row 2 Column B</fo:block>\n"
137+
+ " </fo:table-cell>\n"
138+
+ " </fo:table-row>\n"
139+
+ " </fo:table-body>\n"
140+
+ " </fo:table>\n"
141+
+ " </fo:block>\n"
142+
+ " </fo:flow>\n"
143+
+ " </fo:page-sequence>\n"
144+
+ "</fo:root>");
145+
146+
int count = 0;
147+
for (PDFStructElem elem : elems) {
148+
if (elem.getStructureType().equals(StandardStructureTypes.Table.THEAD)) {
149+
count++;
150+
}
151+
}
152+
153+
assertEquals("The static region per page conf must apply to static regions only", 1, count);
154+
}
155+
156+
private List<PDFStructElem> getPDFStructElems(String fo) throws Exception {
76157
FopFactory fopFactory = getFopFactory();
77158
FOUserAgent userAgent = fopFactory.newFOUserAgent();
159+
foToOutput(fo, fopFactory, userAgent);
160+
161+
PDFStructElem block = (PDFStructElem) userAgent
162+
.getStructureTreeEventHandler().startNode("block", new AttributesImpl(), null);
163+
164+
return block.getDocument().getStructureTreeElements();
165+
}
166+
167+
private ByteArrayOutputStream foToOutput(String fo) throws Exception {
168+
FopFactory fopFactory = getFopFactory();
169+
return foToOutput(fo, fopFactory, fopFactory.newFOUserAgent());
170+
}
171+
172+
private ByteArrayOutputStream foToOutput(String fo, FopFactory fopFactory, FOUserAgent userAgent) throws Exception {
78173
ByteArrayOutputStream bos = new ByteArrayOutputStream();
79174
Fop fop = fopFactory.newFop("application/pdf", userAgent, bos);
80175
Transformer transformer = TransformerFactory.newInstance().newTransformer();

0 commit comments

Comments
 (0)