From 6c6183d35af7ed373ac3c9613ea4e35e77ae3afc Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Tue, 24 Jul 2012 16:39:04 +0000 Subject: Bugzilla #53596: When PDF accessibility is enabled, the structure tree must contain information about the number of columns or rows spanned by a table cell. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1365162 13f79535-47bb-0310-9956-ffa450edef68 --- .../fo/StructureTreeEventTrigger.java | 13 +++++---- src/java/org/apache/fop/pdf/PDFArray.java | 9 ++++++ src/java/org/apache/fop/pdf/PDFStructElem.java | 34 ++++++++++++++++++++++ .../fop/render/pdf/PDFStructureTreeBuilder.java | 12 ++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/java/org/apache/fop/accessibility/fo/StructureTreeEventTrigger.java b/src/java/org/apache/fop/accessibility/fo/StructureTreeEventTrigger.java index 51f7f2bed..7e3ed0591 100644 --- a/src/java/org/apache/fop/accessibility/fo/StructureTreeEventTrigger.java +++ b/src/java/org/apache/fop/accessibility/fo/StructureTreeEventTrigger.java @@ -221,14 +221,17 @@ class StructureTreeEventTrigger extends FOEventHandler { @Override public void startCell(TableCell tc) { AttributesImpl attributes = new AttributesImpl(); - int colSpan = tc.getNumberColumnsSpanned(); - if (colSpan > 1) { - addNoNamespaceAttribute(attributes, "number-columns-spanned", - Integer.toString(colSpan)); - } + addSpanAttribute(attributes, "number-columns-spanned", tc.getNumberColumnsSpanned()); + addSpanAttribute(attributes, "number-rows-spanned", tc.getNumberRowsSpanned()); startElement(tc, attributes); } + private void addSpanAttribute(AttributesImpl attributes, String attributeName, int span) { + if (span > 1) { + addNoNamespaceAttribute(attributes, attributeName, Integer.toString(span)); + } + } + @Override public void endCell(TableCell tc) { endElement(tc); diff --git a/src/java/org/apache/fop/pdf/PDFArray.java b/src/java/org/apache/fop/pdf/PDFArray.java index 131830328..78f5d080b 100644 --- a/src/java/org/apache/fop/pdf/PDFArray.java +++ b/src/java/org/apache/fop/pdf/PDFArray.java @@ -99,6 +99,15 @@ public class PDFArray extends PDFObject { this(null, elements); } + /** + * Creates an array object made of the given elements. + * + * @param elements the array content + */ + public PDFArray(List elements) { + this(null, elements); + } + /** * Create the array object * @param parent the array's parent if any diff --git a/src/java/org/apache/fop/pdf/PDFStructElem.java b/src/java/org/apache/fop/pdf/PDFStructElem.java index 8030e53db..2a756fe9b 100644 --- a/src/java/org/apache/fop/pdf/PDFStructElem.java +++ b/src/java/org/apache/fop/pdf/PDFStructElem.java @@ -33,6 +33,8 @@ import org.apache.fop.util.LanguageTags; */ public class PDFStructElem extends PDFDictionary implements StructureTreeElement, CompressedObject { + private static final PDFName TABLE = new PDFName("Table"); + private PDFStructElem parentElement; /** @@ -40,6 +42,8 @@ public class PDFStructElem extends PDFDictionary implements StructureTreeElement */ protected List kids; + private List attributes; + /** * Creates a new structure element. * @@ -143,9 +147,21 @@ public class PDFStructElem extends PDFDictionary implements StructureTreeElement @Override protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException { attachKids(); + attachAttributes(); super.writeDictionary(out, textBuffer); } + private void attachAttributes() { + if (attributes != null) { + if (attributes.size() == 1) { + put("A", attributes.get(0)); + } else { + PDFArray array = new PDFArray(attributes); + put("A", array); + } + } + } + /** * Attaches all valid kids to the kids array. * @@ -175,6 +191,24 @@ public class PDFStructElem extends PDFDictionary implements StructureTreeElement return kidsAttached; } + public void setTableAttributeColSpan(int colSpan) { + setTableAttributeRowColumnSpan("ColSpan", colSpan); + } + + public void setTableAttributeRowSpan(int rowSpan) { + setTableAttributeRowColumnSpan("RowSpan", rowSpan); + } + + private void setTableAttributeRowColumnSpan(String typeSpan, int span) { + PDFDictionary attribute = new PDFDictionary(); + attribute.put("O", TABLE); + attribute.put(typeSpan, span); + if (attributes == null) { + attributes = new ArrayList(2); + } + attributes.add(attribute); + } + /** * Class representing a placeholder for a PDF Structure Element. */ diff --git a/src/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java b/src/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java index 3839d47bc..0f8e74515 100644 --- a/src/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java +++ b/src/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java @@ -90,11 +90,23 @@ class PDFStructureTreeBuilder implements StructureTreeEventHandler { PDFStructElem parent = ancestors.getFirst(); String role = attributes.getValue("role"); PDFStructElem structElem = createStructureElement(name, parent, role); + setSpanAttributes(structElem, attributes); parent.addKid(structElem); ancestors.addFirst(structElem); return structElem; } + private void setSpanAttributes(PDFStructElem structElem, Attributes attributes) { + String columnSpan = attributes.getValue("number-columns-spanned"); + if (columnSpan != null) { + structElem.setTableAttributeColSpan(Integer.parseInt(columnSpan)); + } + String rowSpan = attributes.getValue("number-rows-spanned"); + if (rowSpan != null) { + structElem.setTableAttributeRowSpan(Integer.parseInt(rowSpan)); + } + } + public void endNode(String name) { removeFirstAncestor(); } -- cgit v1.2.3