]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-2436: Merging of Tagged (Accessible) PDF
authorSimon Steiner <ssteiner@apache.org>
Thu, 11 Jun 2015 15:46:20 +0000 (15:46 +0000)
committerSimon Steiner <ssteiner@apache.org>
Thu, 11 Jun 2015 15:46:20 +0000 (15:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1684928 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/pdf/PDFDictionary.java
src/java/org/apache/fop/pdf/PDFStructElem.java
src/java/org/apache/fop/render/pdf/PDFLogicalStructureHandler.java
src/java/org/apache/fop/render/pdf/PDFPainter.java
src/java/org/apache/fop/render/pdf/PDFRenderingContext.java

index d91e87d92fd80f7879c6a4d3d40b4bcba7d4fa75..4ad1c2ce516f7bb1086ce03ef7705efe0ce957e6 100644 (file)
@@ -161,4 +161,26 @@ public class PDFDictionary extends PDFObject {
             }
         }
     }
+
+    public Set<String> keySet() {
+        return entries.keySet();
+    }
+
+    /**
+     * @see java.util.Map#containsKey(Object)
+     *
+     * @param name The key to find in the map.
+     * @return true if the map contains this key.
+     */
+    public boolean containsKey(String name) {
+        return this.entries.containsKey(name);
+    }
+
+    /**
+     * Removes the mapping for the specified key
+     * @param name  key whose mapping is to be removed
+     */
+    public void remove(String name) {
+        entries.remove(name);
+    }
 }
index badf53060c8b7a7c55ef1b296c5041880a697645..f9a182ceb93d744a9d946e19abd95942b4224a05 100644 (file)
@@ -46,6 +46,12 @@ public class PDFStructElem extends StructureHierarchyMember
 
     private List<PDFDictionary> attributes;
 
+    /**
+     * Creates PDFStructElem with no entries.
+     */
+    public PDFStructElem() {
+    }
+
     /**
      * Creates a new structure element.
      *
@@ -170,6 +176,22 @@ public class PDFStructElem extends StructureHierarchyMember
         }
     }
 
+    public void addKidInSpecificOrder(int position, PDFStructElem kid) {
+        if (kids == null) {
+            addKid(kid);
+        } else {
+            if ((kids.size() - 1) < position) {
+                kids.add(kid);
+            } else if (kids.get(position) == null) {
+                kids.set(position, kid);
+            } else {
+                if (!kids.contains(kid)) {
+                    kids.add(position, kid);
+                }
+            }
+        }
+    }
+
     /**
      * Attaches all valid kids to the kids array.
      *
@@ -217,6 +239,10 @@ public class PDFStructElem extends StructureHierarchyMember
         attributes.add(attribute);
     }
 
+    public List<PDFObject> getKids() {
+        return this.kids;
+    }
+
     /**
      * Class representing a placeholder for a PDF Structure Element.
      */
index e82a63d0d331f7ec57df8e436db04cf0be3bdbea..abaffad60e964feda91014dde25a5e1f41f61181 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.fop.pdf.PDFStructElem;
 /**
  * Handles the creation of the logical structure in the PDF document.
  */
-class PDFLogicalStructureHandler {
+public class PDFLogicalStructureHandler {
 
     private static final PDFName MCR = new PDFName("MCR");
 
@@ -92,11 +92,15 @@ class PDFLogicalStructureHandler {
         this.pdfDoc = pdfDoc;
     }
 
-    PDFParentTree getParentTree() {
+    public PDFArray getPageParentTree() {
+        return this.pageParentTreeArray;
+    }
+
+    public PDFParentTree getParentTree() {
         return parentTree;
     }
 
-    private int getNextParentTreeKey() {
+    public int getNextParentTreeKey() {
         return parentTreeKey++;
     }
 
index c43e6347a3be47bb8b4b75ae79f709d073281748..998dc3adebdcf42f0e90222fac19f16b99eff86a 100644 (file)
@@ -25,6 +25,7 @@ import java.awt.Paint;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.HashSet;
 import java.util.Locale;
@@ -32,6 +33,12 @@ import java.util.Set;
 
 import org.w3c.dom.Document;
 
+import org.apache.xmlgraphics.image.loader.ImageException;
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+
+import org.apache.fop.ResourceEventProducer;
 import org.apache.fop.fonts.Font;
 import org.apache.fop.fonts.FontTriplet;
 import org.apache.fop.fonts.LazyFont;
@@ -166,10 +173,6 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
                 placeImage(rect, xobject);
             }
         } else {
-            if (accessEnabled) {
-                PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
-                prepareImageMCID(structElem);
-            }
             drawImageUsingURI(uri, rect);
             if (!getDocumentHandler().getPDFDocument().isLinearizationEnabled()) {
                 flushPDFDoc();
@@ -177,6 +180,36 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
         }
     }
 
+    @Override
+    protected void drawImageUsingURI(String uri, Rectangle rect) {
+        ImageManager manager = getUserAgent().getImageManager();
+        ImageInfo info = null;
+        try {
+            ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
+            info = manager.getImageInfo(uri, sessionContext);
+            if (accessEnabled) {
+                PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
+                String mimeType = info.getMimeType();
+                if (!mimeType.equalsIgnoreCase("application/pdf")) {
+                    prepareImageMCID(structElem);
+                }
+            }
+            drawImageUsingImageHandler(info, rect);
+        } catch (ImageException ie) {
+            ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+                    getUserAgent().getEventBroadcaster());
+            eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
+        } catch (FileNotFoundException fe) {
+            ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+                    getUserAgent().getEventBroadcaster());
+            eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
+        } catch (IOException ioe) {
+            ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+                    getUserAgent().getEventBroadcaster());
+            eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
+        }
+    }
+
     private void prepareImageMCID(PDFStructElem structElem) {
         imageMCI = logicalStructureHandler.addImageContentItem(structElem);
         if (structElem != null) {
@@ -191,6 +224,8 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
                 getUserAgent(), generator, getDocumentHandler().getCurrentPage(), getFontInfo());
         pdfContext.setMarkedContentInfo(imageMCI);
         pdfContext.setPageNumbers(getDocumentHandler().getPageNumbers());
+        pdfContext.setPdfLogicalStructureHandler(logicalStructureHandler);
+        pdfContext.setCurrentSessionStructElem((PDFStructElem) getContext().getStructureTreeElement());
         return pdfContext;
     }
 
index 429d9b936a2c9d1dc90e26df9eca8bc7cc0d3323..b99bcdbfed85e98d77086e4f88e7e5f5ffa71d03 100644 (file)
@@ -27,6 +27,7 @@ import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.fonts.FontInfo;
 import org.apache.fop.pdf.PDFArray;
 import org.apache.fop.pdf.PDFPage;
+import org.apache.fop.pdf.PDFStructElem;
 import org.apache.fop.render.AbstractRenderingContext;
 import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
 
@@ -40,6 +41,8 @@ public class PDFRenderingContext extends AbstractRenderingContext {
     private PDFPage page;
     private MarkedContentInfo mci;
     private Map<Integer, PDFArray> pageNumbers;
+    private PDFLogicalStructureHandler pdfLogicalStructureHandler;
+    private PDFStructElem currentSessionStructElem;
 
     /**
      * Main constructor.
@@ -100,4 +103,20 @@ public class PDFRenderingContext extends AbstractRenderingContext {
     public void setPageNumbers(Map<Integer, PDFArray> pageNumbers) {
         this.pageNumbers = pageNumbers;
     }
+
+    public PDFLogicalStructureHandler getPdfLogicalStructureHandler() {
+        return pdfLogicalStructureHandler;
+    }
+
+    public void setPdfLogicalStructureHandler(PDFLogicalStructureHandler pdfLogicalStructureHandler) {
+        this.pdfLogicalStructureHandler = pdfLogicalStructureHandler;
+    }
+
+    public PDFStructElem getCurrentSessionStructElem() {
+        return currentSessionStructElem;
+    }
+
+    public void setCurrentSessionStructElem(PDFStructElem currentSessionStructElem) {
+        this.currentSessionStructElem = currentSessionStructElem;
+    }
 }