diff options
author | Jeremias Maerki <jeremias@apache.org> | 2009-04-17 07:56:17 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2009-04-17 07:56:17 +0000 |
commit | 7882e4711f9bb1b32e3c4f17eaad41a3e4864957 (patch) | |
tree | e62704c9e029dd7ecdf040f92a73715b48126bee /src/java/org/apache | |
parent | 2a16dbaa207c3e36a43dca0dceaaa1bea51eb87d (diff) | |
download | xmlgraphics-fop-7882e4711f9bb1b32e3c4f17eaad41a3e4864957.tar.gz xmlgraphics-fop-7882e4711f9bb1b32e3c4f17eaad41a3e4864957.zip |
Removed FO to role mapping from the PDF library and moved it next to the renderer.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_Accessibility@765890 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
-rw-r--r-- | src/java/org/apache/fop/pdf/PDFStructElem.java | 105 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java | 133 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java | 14 |
3 files changed, 170 insertions, 82 deletions
diff --git a/src/java/org/apache/fop/pdf/PDFStructElem.java b/src/java/org/apache/fop/pdf/PDFStructElem.java index 408b1f2c7..e36f5fd42 100644 --- a/src/java/org/apache/fop/pdf/PDFStructElem.java +++ b/src/java/org/apache/fop/pdf/PDFStructElem.java @@ -25,81 +25,21 @@ package org.apache.fop.pdf; public class PDFStructElem extends PDFDictionary { private PDFObject parentObject = null; - private String source = ""; private boolean level1 = false; /** * Create the /StructTreeRoot dictionary - * @param fo passed in fo object * @param parent Parent of this PDFStructElem + * @param structureType the structure type for the element */ - public PDFStructElem(String fo, PDFObject parent) { + public PDFStructElem(PDFObject parent, PDFName structureType) { super(); if (parent instanceof PDFStructElem) { parentObject = (PDFStructElem) parent; } put("Type", new PDFName("StructElem")); - source = fo; - //TODO Move this into the render/pdf package. The PDF library shall not contain FO knowledge - if ("block".equals(fo)) { - put("S", new PDFName("P")); - } else if ("inline".equals(fo) || "wrapper".equals(fo) || "character".equals(fo)) { - put("S", new PDFName("Span")); - } else if ("table-cell".equals(fo)) { - PDFStructElem grandParent = (PDFStructElem) - ((PDFStructElem)parent).getParentStructElem(); - String s = grandParent.getSource(); - if ("table-header".equals(s)) { - put("S", new PDFName("TH")); - } else { - put("S", new PDFName("TD")); - } - } else if ("table-row".equals(fo)) { - put("S", new PDFName("TR")); - } else if ("root".equals(fo)) { - put("S", new PDFName("Document")); - } else if ("page-sequence".equals(fo)) { - put("S", new PDFName("Part")); - } else if ("flow".equals(fo) || "static-content".equals(fo)) { - put("S", new PDFName("Sect")); - } else if ("page-number".equals(fo) || "page-number-citation".equals(fo) - || "page-number-citation-last".equals(fo)) { - put("S", new PDFName("Quote")); - } else if ("external-graphic".equals(fo) || "instream-foreign-object".equals(fo)) { - put("S", new PDFName("Figure")); - } else if ("table".equals(fo)) { - put("S", new PDFName("Table")); - } else if ("table-body".equals(fo)) { - put("S", new PDFName("TBody")); - } else if ("table-header".equals(fo)) { - put("S", new PDFName("THead")); - } else if ("table-footer".equals(fo)) { - put("S", new PDFName("TFoot")); - } else if ("list-block".equals(fo)) { - put("S", new PDFName("L")); - } else if ("list-item".equals(fo)) { - put("S", new PDFName("LI")); - } else if ("list-item-label".equals(fo)) { - put("S", new PDFName("Lbl")); - } else if ("list-item-body".equals(fo)) { - put("S", new PDFName("LBody")); - } else if ("block-container".equals(fo)) { - put("S", new PDFName("Div")); - } else if ("basic-link".equals(fo)) { - put("S", new PDFName("Link")); - } else if ("footnote".equals(fo)) { - put("S", new PDFName("Note")); - } else if ("footnote-body".equals(fo)) { - put("S", new PDFName("Sect")); - } else if ("marker".equals(fo)) { - put("S", new PDFName("Private")); - } else { - log.error("Accessibility: PDFStructElem constructor is missing: " + fo); - } + setStructureType(structureType); setParent(parent); - if (!"external-graphic".equals(fo) && !"instream-foreign-object".equals(fo)) { - put("K", new PDFArray()); - } } /** @@ -138,14 +78,6 @@ public class PDFStructElem extends PDFDictionary { } /** - * Get the source of this StructElem - * @return the source - */ - public String getSource() { - return source; - } - - /** * The kids of this StructElem * @return the kids */ @@ -158,18 +90,23 @@ public class PDFStructElem extends PDFDictionary { * @param kid to be added */ public void addKid(PDFObject kid) { - getKids().add(kid); + PDFArray kids = getKids(); + if (kids == null) { + kids = new PDFArray(); + put("K", kids); + } + kids.add(kid); } /** - * Add a kid, but only if it does not already exist + * Add a kid, but only if it does not already exist. * @param kid to be added * @return true if kid did not already exist */ public boolean addUniqueKid(PDFObject kid) { PDFArray mArray = getKids(); - if (!mArray.contains(kid)) { - getKids().add(kid); + if (mArray == null || !mArray.contains(kid)) { + addKid(kid); return true; } else { return false; @@ -177,8 +114,7 @@ public class PDFStructElem extends PDFDictionary { } /** - * Add kid referenced through mcid integer - * used fo:external-graphic + * Add kid referenced through mcid integer. Used for images. * @param mcid of this kid */ public void addMCIDKid(int mcid) { @@ -193,4 +129,19 @@ public class PDFStructElem extends PDFDictionary { put("Pg", (PDFObject) pageObject); } + /** + * Sets the structure type (the "S" entry). + * @param type the structure type + */ + public void setStructureType(PDFName type) { + put("S", type); + } + + /** + * Returns the structure type of this structure element. + * @return the structure type + */ + public PDFName getStructureType() { + return (PDFName)get("S"); + } } diff --git a/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java b/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java new file mode 100644 index 000000000..f1de1a65b --- /dev/null +++ b/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.pdf; + +import java.util.Map; + +import org.apache.fop.pdf.PDFName; +import org.apache.fop.pdf.PDFObject; +import org.apache.fop.pdf.PDFStructElem; + +/** + * This class provides the standard mappings from Formatting Objects to PDF structure types. + */ +public class FOToPDFRoleMap { + + private static final Map STANDARD_MAPPINGS = new java.util.HashMap(); + + private static final PDFName TFOOT = new PDFName("TFoot"); + private static final PDFName THEAD = new PDFName("THead"); + + static { + addSimpleMapping("block", new PDFName("P")); + addSimpleMapping("block-container", new PDFName("Div")); + + PDFName st = new PDFName("Span"); + addSimpleMapping("inline", st); + addSimpleMapping("wrapper", st); + addSimpleMapping("character", st); + + addSimpleMapping("root", new PDFName("Document")); + addSimpleMapping("page-sequence", new PDFName("Part")); + addSimpleMapping("flow", new PDFName("Sect")); + addSimpleMapping("static-content", new PDFName("Sect")); + + st = new PDFName("Quote"); + addSimpleMapping("page-number", st); + addSimpleMapping("page-number-citation", st); + addSimpleMapping("page-number-citation-last", st); + + st = new PDFName("Figure"); + addSimpleMapping("external-graphic", st); + addSimpleMapping("instream-foreign-object", st); + + addSimpleMapping("table", new PDFName("Table")); + addSimpleMapping("table-body", new PDFName("TBody")); + addSimpleMapping("table-header", THEAD); + addSimpleMapping("table-footer", TFOOT); + addSimpleMapping("table-row", new PDFName("TR")); + STANDARD_MAPPINGS.put("table-cell", new TableCellMapper()); + + addSimpleMapping("list-block", new PDFName("L")); + addSimpleMapping("list-item", new PDFName("LI")); + addSimpleMapping("list-item-label", new PDFName("Lbl")); + addSimpleMapping("list-item-body", new PDFName("LBody")); + + addSimpleMapping("basic-link", new PDFName("Link")); + addSimpleMapping("footnote", new PDFName("Note")); + addSimpleMapping("footnote-body", new PDFName("Sect")); + addSimpleMapping("marker", new PDFName("Private")); + } + + private static void addSimpleMapping(String fo, PDFName structureType) { + STANDARD_MAPPINGS.put(fo, new SimpleMapper(structureType)); + } + + /** + * Maps a Formatting Object to a PDFName representing the associated structure type. + * @param fo the formatting object's local name + * @param parent the parent of the structure element to be mapped + * @return the structure type or null if no match could be found + */ + public static PDFName mapFormattingObject(String fo, PDFObject parent) { + Mapper mapper = (Mapper)STANDARD_MAPPINGS.get(fo); + if (mapper != null) { + return mapper.getStructureType(parent); + } + return null; + } + + private interface Mapper { + PDFName getStructureType(PDFObject parent); + } + + private static class SimpleMapper implements Mapper { + + private PDFName structureType; + + public SimpleMapper(PDFName structureType) { + this.structureType = structureType; + } + + public PDFName getStructureType(PDFObject parent) { + return structureType; + } + + } + + private static class TableCellMapper implements Mapper { + + private static final PDFName TD = new PDFName("TD"); + private static final PDFName TH = new PDFName("TH"); + + public PDFName getStructureType(PDFObject parent) { + PDFStructElem grandParent = (PDFStructElem) + ((PDFStructElem)parent).getParentStructElem(); + //TODO What to do with cells from table-footer? Currently they are mapped on TD. + if (THEAD.equals(grandParent.getStructureType())) { + return TH; + } else { + return TD; + } + } + + } + +} diff --git a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java index fea606f97..7f5a9be5f 100644 --- a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java +++ b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java @@ -220,7 +220,8 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { log.info("Accessibility is enabled"); PDFStructTreeRoot structTreeRoot = this.pdfDoc.getFactory().makeStructTreeRoot(); this.pdfDoc.getRoot().setStructTreeRoot(structTreeRoot); - PDFStructElem structElemDocument = new PDFStructElem("root", structTreeRoot); + PDFStructElem structElemDocument = new PDFStructElem(structTreeRoot, + FOToPDFRoleMap.mapFormattingObject("root", structTreeRoot)); this.pdfDoc.assignObjectNumber(structElemDocument); this.pdfDoc.addTrailerObject(structElemDocument); structTreeRoot.addKid(structElemDocument); @@ -306,7 +307,8 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { new ByteArrayInputStream(this.getUserAgent().getReducedFOTree())); } PDFStructElem parent = (PDFStructElem)getStructTreeRoot().getFirstChild(); - PDFStructElem structElemPart = new PDFStructElem("page-sequence", parent); + PDFStructElem structElemPart = new PDFStructElem(parent, + FOToPDFRoleMap.mapFormattingObject("page-sequence", parent)); this.pdfDoc.assignObjectNumber(structElemPart); this.pdfDoc.addTrailerObject(structElemPart); parent.addKid(structElemPart); @@ -325,8 +327,9 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { Node node = nodes.item(i); if (node.getNodeName().equals("fo:flow") || node.getNodeName().equals("fo:static-content")) { - PDFStructElem structElemSect = new PDFStructElem( - node.getLocalName(), structElemPart); + PDFStructElem structElemSect = new PDFStructElem(structElemPart, + FOToPDFRoleMap.mapFormattingObject(node.getLocalName(), + structElemPart)); this.pdfDoc.assignObjectNumber(structElemSect); this.pdfDoc.addTrailerObject(structElemSect); structElemPart.addKid(structElemSect); @@ -590,7 +593,8 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { ptr = ""; } String s = me.getLocalName(); - PDFStructElem structElem = new PDFStructElem(s, parent); + PDFStructElem structElem = new PDFStructElem(parent, + FOToPDFRoleMap.mapFormattingObject(s, parent)); this.pdfDoc.assignObjectNumber(structElem); this.pdfDoc.addTrailerObject(structElem); if (depth == 1) { |