Author: jeremias
Date: Fri Apr 17 07:56:17 2009
New Revision: 765890

URL: http://svn.apache.org/viewvc?rev=765890&view=rev
Log:
Removed FO to role mapping from the PDF library and moved it next to the 
renderer.

Added:
    
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
   (with props)
Modified:
    
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/pdf/PDFStructElem.java
    
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java

Modified: 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/pdf/PDFStructElem.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/pdf/PDFStructElem.java?rev=765890&r1=765889&r2=765890&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/pdf/PDFStructElem.java
 (original)
+++ 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/pdf/PDFStructElem.java
 Fri Apr 17 07:56:17 2009
@@ -25,81 +25,21 @@
 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 @@
     }
 
     /**
-     * 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 @@
      * @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 @@
     }
 
     /**
-     * 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 @@
         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");
+    }
 }

Added: 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java?rev=765890&view=auto
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
 (added)
+++ 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
 Fri Apr 17 07:56:17 2009
@@ -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;
+            }
+        }
+
+    }
+
+}

Propchange: 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java?rev=765890&r1=765889&r2=765890&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
 (original)
+++ 
xmlgraphics/fop/branches/Temp_Accessibility/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
 Fri Apr 17 07:56:17 2009
@@ -220,7 +220,8 @@
                 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 @@
                             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 @@
                     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 @@
             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) {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to