From f238bd0aa5085b8fbd10a73a156893fd7e13ce55 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Thu, 11 Jun 2015 15:46:20 +0000 Subject: [PATCH] FOP-2436: Merging of Tagged (Accessible) PDF git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1684928 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/pdf/PDFDictionary.java | 22 ++++++++++ .../org/apache/fop/pdf/PDFStructElem.java | 26 +++++++++++ .../pdf/PDFLogicalStructureHandler.java | 10 +++-- .../org/apache/fop/render/pdf/PDFPainter.java | 43 +++++++++++++++++-- .../fop/render/pdf/PDFRenderingContext.java | 19 ++++++++ 5 files changed, 113 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/fop/pdf/PDFDictionary.java b/src/java/org/apache/fop/pdf/PDFDictionary.java index d91e87d92..4ad1c2ce5 100644 --- a/src/java/org/apache/fop/pdf/PDFDictionary.java +++ b/src/java/org/apache/fop/pdf/PDFDictionary.java @@ -161,4 +161,26 @@ public class PDFDictionary extends PDFObject { } } } + + public Set 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); + } } diff --git a/src/java/org/apache/fop/pdf/PDFStructElem.java b/src/java/org/apache/fop/pdf/PDFStructElem.java index badf53060..f9a182ceb 100644 --- a/src/java/org/apache/fop/pdf/PDFStructElem.java +++ b/src/java/org/apache/fop/pdf/PDFStructElem.java @@ -46,6 +46,12 @@ public class PDFStructElem extends StructureHierarchyMember private List 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 getKids() { + return this.kids; + } + /** * Class representing a placeholder for a PDF Structure Element. */ diff --git a/src/java/org/apache/fop/render/pdf/PDFLogicalStructureHandler.java b/src/java/org/apache/fop/render/pdf/PDFLogicalStructureHandler.java index e82a63d0d..abaffad60 100644 --- a/src/java/org/apache/fop/render/pdf/PDFLogicalStructureHandler.java +++ b/src/java/org/apache/fop/render/pdf/PDFLogicalStructureHandler.java @@ -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++; } diff --git a/src/java/org/apache/fop/render/pdf/PDFPainter.java b/src/java/org/apache/fop/render/pdf/PDFPainter.java index c43e6347a..998dc3ade 100644 --- a/src/java/org/apache/fop/render/pdf/PDFPainter.java +++ b/src/java/org/apache/fop/render/pdf/PDFPainter.java @@ -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 { 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 { } } + @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 { getUserAgent(), generator, getDocumentHandler().getCurrentPage(), getFontInfo()); pdfContext.setMarkedContentInfo(imageMCI); pdfContext.setPageNumbers(getDocumentHandler().getPageNumbers()); + pdfContext.setPdfLogicalStructureHandler(logicalStructureHandler); + pdfContext.setCurrentSessionStructElem((PDFStructElem) getContext().getStructureTreeElement()); return pdfContext; } diff --git a/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java b/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java index 429d9b936..b99bcdbfe 100644 --- a/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java +++ b/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java @@ -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 pageNumbers; + private PDFLogicalStructureHandler pdfLogicalStructureHandler; + private PDFStructElem currentSessionStructElem; /** * Main constructor. @@ -100,4 +103,20 @@ public class PDFRenderingContext extends AbstractRenderingContext { public void setPageNumbers(Map 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; + } } -- 2.39.5