xmlgraphics-fop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From me...@apache.org
Subject svn commit: r1388078 - in /xmlgraphics/fop-pdf-images/trunk: ./ src/java/org/apache/fop/render/pdf/pdfbox/
Date Thu, 20 Sep 2012 15:35:24 GMT
Author: mehdi
Date: Thu Sep 20 15:35:24 2012
New Revision: 1388078

URL: http://svn.apache.org/viewvc?rev=1388078&view=rev
Log:
Bugzilla#53909: Updated with caching and other bugfixes

Added:
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java
  (with props)
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java
  (with props)
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml
  (with props)
Modified:
    xmlgraphics/fop-pdf-images/trunk/build.xml
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxImageHandler.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java

Modified: xmlgraphics/fop-pdf-images/trunk/build.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/build.xml?rev=1388078&r1=1388077&r2=1388078&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/build.xml (original)
+++ xmlgraphics/fop-pdf-images/trunk/build.xml Thu Sep 20 15:35:24 2012
@@ -51,6 +51,13 @@
     </fileset>
   </path>
 
+  <path id="libs-build-tools-classpath">
+    <path refid="libs-build-classpath"/>
+    <fileset dir="${lib.dir}/build">
+      <include name="*.jar"/>
+    </fileset>
+  </path>
+
   <fileset dir="${basedir}" id="dist.bin">
     <include name="LICENSE"/>
     <include name="NOTICE"/>
@@ -109,6 +116,23 @@
     </copy>
   </target>
 
+  <target name="resourcegen" depends="compile-java">
+    <taskdef name="generateEventResources" 
+      classname="org.apache.fop.tools.EventProducerCollectorTask">
+      <classpath refid="libs-build-tools-classpath"/>
+    </taskdef>
+    <generateEventResources destdir="${build.classes.dir}">
+      <fileset dir="${src.java.dir}">
+        <include name="org/apache/fop/render/pdf/pdfbox/*.java"/>
+      </fileset>
+    </generateEventResources>
+    <copy todir="${build.classes.dir}">
+      <fileset dir="${src.java.dir}">
+        <include name="org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml"/>
+      </fileset>
+    </copy>
+  </target>
+
   <!-- =================================================================== -->
   <!-- Creates JAR files                                                   -->
   <!-- =================================================================== -->
@@ -119,7 +143,7 @@
     </uptodate>
   </target>
 
-  <target name="jar-main" depends="compile-java,uptodate-jar-main" description="Generates
the main jar file" unless="jar.main.uptodate">
+  <target name="jar-main" depends="resourcegen,compile-java,uptodate-jar-main" description="Generates
the main jar file" unless="jar.main.uptodate">
     <jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.classes.dir}">
       <manifest>
         <attribute name="Implementation-Title" value="${Name}"/>

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java?rev=1388078&r1=1388077&r2=1388078&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
(original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
Thu Sep 20 15:35:24 2012
@@ -21,18 +21,22 @@ package org.apache.fop.render.pdf.pdfbox
 
 import java.io.IOException;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.WeakHashMap;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 
 import org.apache.xmlgraphics.image.loader.util.ImageUtil;
 
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.events.EventBroadcaster;
 import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFFormXObject;
 import org.apache.fop.pdf.PDFPage;
+import org.apache.fop.pdf.Version;
+import org.apache.fop.render.pdf.pdfbox.Cache.ValueMaker;
 
 /**
  * Abstract base class for implementation of FOP's image handler interfaces (old and new)
@@ -41,96 +45,100 @@ import org.apache.fop.pdf.PDFPage;
  */
 public abstract class AbstractPDFBoxHandler {
 
-    /** logging instance */
-    protected static Log log = LogFactory.getLog(AbstractPDFBoxHandler.class);
+    private static final Cache.Type CACHE_TYPE = Cache.Type.valueOf(
+            System.getProperty("fop.pdfbox.doc-cache", Cache.Type.WEAK.name()).toUpperCase());
 
-    private static Map objectCaches = Collections.synchronizedMap(new java.util.WeakHashMap());
+    private static Cache<String, Map<Object, Object>> createDocumentCache() {
+        return Cache.createCache(CACHE_TYPE);
+    }
+
+    private static final ValueMaker<Map<Object, Object>> MAP_MAKER
+            = new ValueMaker<Map<Object, Object>>() {
+        public Map<Object, Object> make() throws Exception {
+            return new HashMap<Object, Object>();
+        }
+    };
+
+    private static Map<Object, Cache<String, Map<Object, Object>>> objectCacheMap
+            = Collections.synchronizedMap(new WeakHashMap<Object, Cache<String, Map<Object,
Object>>>());
 
     protected PDFFormXObject createFormForPDF(ImagePDF image,
-            PDFPage targetPage) throws IOException {
-        final int selectedPage = ImageUtil.needPageIndexFromURI(image.getInfo().getOriginalURI());
+            PDFPage targetPage, FOUserAgent userAgent) throws IOException {
+
+        EventBroadcaster eventBroadcaster = userAgent.getEventBroadcaster();
+        String originalImageUri = image.getInfo().getOriginalURI();
+        final int selectedPage = ImageUtil.needPageIndexFromURI(originalImageUri);
 
         PDDocument pddoc = image.getPDDocument();
         float pdfVersion = pddoc.getDocument().getVersion();
-        if (pdfVersion > 1.4f) {
-            log.warn("The version of the loaded PDF is " + pdfVersion
-                    + ". PDF Versions beyond 1.4 might not create correct results.");
+        Version inputDocVersion = Version.getValueOf(String.valueOf(pdfVersion));
+        PDFDocument pdfDoc = targetPage.getDocument();
+
+        if (pdfDoc.getPDFVersion().compareTo(inputDocVersion) < 0) {
+            try {
+                pdfDoc.setPDFVersion(inputDocVersion);
+            } catch (IllegalStateException e) {
+                getEventProducer(eventBroadcaster).pdfVersionMismatch(this,
+                         pdfDoc.getPDFVersionString(), String.valueOf(pdfVersion));
+            }
         }
 
         //Encryption test
-        if (pddoc.isEncrypted() && pddoc.getCurrentAccessPermission().isOwnerPermission())
{
-            log.error("PDF to be embedded must not be encrypted!"
-                    + " Alternative: provide authentication via interceptor.");
+        if (pddoc.isEncrypted()) {
+            getEventProducer(eventBroadcaster).encryptedPdf(this);
             return null;
         }
 
-        PDFDocument pdfDoc = targetPage.getDocument();
+
         //Warn about potential problems with PDF/A and PDF/X
         if (pdfDoc.getProfile().isPDFAActive()) {
-            log.warn("PDF/A mode is active."
-                    + " Embedding a PDF file may result in a non-compliant file!");
+            getEventProducer(eventBroadcaster).pdfAActive(this);
         }
         if (pdfDoc.getProfile().isPDFXActive()) {
-            log.warn("PDF/X mode is active."
-                    + " Embedding a PDF file may result in a non-compliant file!");
+            getEventProducer(eventBroadcaster).pdfXActive(this);
         }
 
-        PDPage page = (PDPage)pddoc.getDocumentCatalog().getAllPages().get(selectedPage);
+        Map<Object, Object> objectCache = getObjectCache(originalImageUri, userAgent);
 
-        //Only has an effect if PDDocuments are reused for multiple page which is currently
not
-        //the case. Code remains in place in case this can be improved in the future.
-        MapKey key = new MapKey(pddoc, pdfDoc);
-        Map objectCache = (Map)objectCaches.get(key);
-        if (objectCache == null) {
-            //Object cache itself doesn't need to be cached as FOP is not multi-threaded
-            objectCache = new java.util.HashMap();
-            objectCaches.put(key, objectCache);
-        }
+        PDPage page = (PDPage) pddoc.getDocumentCatalog().getAllPages().get(selectedPage);
 
         PDFBoxAdapter adapter = new PDFBoxAdapter(targetPage, objectCache);
-        PDFFormXObject form = adapter.createFormFromPDFBoxPage(
-                pddoc, page, image.getInfo().getOriginalURI());
+        PDFFormXObject form = adapter.createFormFromPDFBoxPage(pddoc, page, originalImageUri,
+                eventBroadcaster);
         return form;
     }
 
-    private static final class MapKey {
-
-        private PDDocument sourceDocument;
-        private PDFDocument targetDocument;
-
-        public MapKey(PDDocument source, PDFDocument target) {
-            this.sourceDocument = source;
-            this.targetDocument = target;
-        }
-
-        /** {@inheritDoc} */
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + sourceDocument.hashCode();
-            result = prime * result + targetDocument.hashCode();
-            return result;
-        }
-
-        /** {@inheritDoc} */
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            MapKey other = (MapKey)obj;
-            return sourceDocument == other.sourceDocument
-                    && targetDocument == other.targetDocument;
+    private Map<Object, Object> getObjectCache(String originalImageUri,
+            Object documentScopedReference) {
+        String fileUri = getImagePath(originalImageUri);
+        try {
+            return getDocumentCache(documentScopedReference)
+                        .getValue(fileUri, MAP_MAKER);
+        } catch (Exception e) {
+            // We cannot recover from this
+            throw new RuntimeException(e);
         }
+    }
 
+    private Cache<String, Map<Object, Object>> getDocumentCache(Object documentScopedReference)
{
+        Cache<String, Map<Object, Object>> documentCache = objectCacheMap.get(documentScopedReference);
+        if (documentCache == null) {
+            documentCache = createDocumentCache();
+            objectCacheMap.put(documentScopedReference, documentCache);
+        }
+        return documentCache;
+    }
 
+    private String getImagePath(String originalImageUri) {
+        int hashIndex = originalImageUri.indexOf('#');
+        if (hashIndex > 0) {
+            return originalImageUri.substring(0, hashIndex);
+        } else {
+            return originalImageUri;
+        }
     }
 
+    private PDFBoxEventProducer getEventProducer(EventBroadcaster eventBroadcaster) {
+        return PDFBoxEventProducer.Provider.get(eventBroadcaster);
+    }
 }

Added: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java?rev=1388078&view=auto
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java
(added)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java
Thu Sep 20 15:35:24 2012
@@ -0,0 +1,93 @@
+package org.apache.fop.render.pdf.pdfbox;
+
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+abstract class Cache<K,V> {
+
+    public enum Type {
+        WEAK, SOFT, STRONG;
+    }
+
+    public abstract V getValue(K key, ValueMaker<V> valueMaker) throws Exception;
+
+    public static <K,V> Cache<K,V> createCache(Type cacheType) {
+        switch (cacheType) {
+            case WEAK: return new WeakDocumentCache<K,V>();
+            case SOFT: return new SoftDocumentCache<K,V>();
+            case STRONG: return new StrongDocumentCache<K,V>();
+            default: return createDefaultCache();
+        }
+    }
+
+    private static <K,V> Cache<K,V> createDefaultCache() {
+        return new WeakDocumentCache<K,V>();
+    }
+
+    private static class StrongDocumentCache<K,V> extends Cache<K,V> {
+
+        private final Map<K, V> cache = new HashMap<K, V>();
+
+        @Override
+        public V getValue(K key, ValueMaker<V> valueMaker) throws Exception {
+            V value = cache.get(key);
+            if (value == null) {
+                value = valueMaker.make();
+                cache.put(key, value);
+            }
+            return value;
+        }
+    }
+
+    private static class SoftDocumentCache<K,V> extends Cache<K,V> {
+
+        private final Map<K, SoftReference<Object>> softKeys
+                = new HashMap<K, SoftReference<Object>>();
+
+        private final Map<Object, V> cache = new WeakHashMap<Object, V>();
+
+        private Object currentKey;
+
+        @Override
+        public V getValue(K key, ValueMaker<V> valueMaker) throws Exception {
+            SoftReference<Object> reference = softKeys.get(key);
+            Object softKey;
+            if (reference == null || reference.get() == null) {
+                softKey = new Object();
+                reference = new SoftReference<Object>(softKey);
+                softKeys.put(key, reference);
+            } else {
+                softKey = reference.get();
+            }
+            currentKey = softKey;
+            V value = cache.get(softKey);
+            if (value == null) {
+                value = valueMaker.make();
+                cache.put(softKey, value);
+            }
+            return value;
+        }
+    }
+
+    private static class WeakDocumentCache<K,V> extends Cache<K,V> {
+
+        private V currentValue;
+
+        private K currentKey;
+
+        @Override
+        public V getValue(K key, ValueMaker<V> valueMaker) throws Exception {
+            if (!key.equals(currentKey)) {
+                currentKey = key;
+                currentValue = valueMaker.make();
+            }
+            return currentValue;
+        }
+    }
+
+    public static interface ValueMaker<V> {
+        V make() throws Exception;
+    }
+}

Propchange: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/Cache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java?rev=1388078&r1=1388077&r2=1388078&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
(original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
Thu Sep 20 15:35:24 2012
@@ -27,6 +27,7 @@ import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -58,6 +59,7 @@ import org.apache.pdfbox.pdmodel.common.
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
 
+import org.apache.fop.events.EventBroadcaster;
 import org.apache.fop.pdf.PDFArray;
 import org.apache.fop.pdf.PDFDictionary;
 import org.apache.fop.pdf.PDFDocument;
@@ -172,8 +174,6 @@ class PDFBoxAdapter {
         } else if (base instanceof COSStream) {
             COSStream originalStream = (COSStream)base;
 
-            PDFStream stream = new PDFStream();
-
             InputStream in;
             Set filter;
             if (pdfDoc.isEncryptionActive()) {
@@ -184,17 +184,18 @@ class PDFBoxAdapter {
                 in = originalStream.getFilteredStream();
                 filter = Collections.EMPTY_SET;
             }
+            PDFStream stream = new PDFStream();
             OutputStream out = stream.getBufferOutputStream();
             IOUtils.copyLarge(in, out);
             transferDict(originalStream, stream, filter);
             return cacheClonedObject(keyBase, stream);
         } else if (base instanceof COSDictionary) {
             COSDictionary dic = (COSDictionary)base;
+            List keys = dic.keyList();
             PDFDictionary newDict = new PDFDictionary();
             cacheClonedObject(keyBase, newDict);
-            Iterator iter = dic.keySet().iterator();
-            while (iter.hasNext()) {
-                COSName key = (COSName)iter.next();
+            for (int i = 0; i < keys.size(); i++) {
+                COSName key = (COSName)keys.get(i);
                 if (!exclude.contains(key)) {
                     (newDict).put(key.getName(), cloneForNewDocument(dic.getItem(key)));
                 }
@@ -248,18 +249,16 @@ class PDFBoxAdapter {
 
     private Object cacheClonedObject(Object base, Object cloned) {
         Object key = getBaseKey(base);
-        if (base instanceof COSObject) {
-            PDFObject pdfobj = (PDFObject)cloned;
-            if (!pdfobj.hasObjectNumber()) {
-                pdfDoc.registerObject(pdfobj);
-                if (log.isTraceEnabled()) {
-                    log.trace(key + ": " + pdfobj.getClass().getName() + " registered as
"
+        if (key == null) {
+            return cloned;
+        }
+        PDFObject pdfobj = (PDFObject) cloned;
+        if (!pdfobj.hasObjectNumber()) {
+            pdfDoc.registerObject(pdfobj);
+            if (log.isTraceEnabled()) {
+                log.trace(key + ": " + pdfobj.getClass().getName() + " registered as "
                             + pdfobj.getObjectNumber() + " " + pdfobj.getGeneration());
-                }
             }
-        } else if (log.isTraceEnabled()) {
-            //TODO PDFDictionary.toString() may cause stack overflows
-            //log.trace(key + ": cached, direct object");
         }
         clonedVersion.put(key, cloned);
         return cloned;
@@ -268,10 +267,9 @@ class PDFBoxAdapter {
     private Object getBaseKey(Object base) {
         if (base instanceof COSObject) {
             COSObject obj = (COSObject)base;
-            return obj.getObjectNumber().intValue()
-                    + " " + obj.getGenerationNumber().intValue();
+            return obj.getObjectNumber().intValue() + " " + obj.getGenerationNumber().intValue();
         } else {
-            return base;
+            return null;
         }
     }
 
@@ -282,9 +280,9 @@ class PDFBoxAdapter {
 
     private void transferDict(COSDictionary orgDict, PDFStream targetDict,
             Set filter, boolean inclusive) throws IOException {
-        Iterator iter = orgDict.keySet().iterator();
-        while (iter.hasNext()) {
-            COSName key = (COSName)iter.next();
+        List keys = orgDict.keyList();
+        for (int i = 0, ci = keys.size(); i < ci; i++) {
+            COSName key = (COSName)keys.get(i);
             if (inclusive && !filter.contains(key.getName())) {
                 continue;
             } else if (!inclusive && filter.contains(key.getName())) {
@@ -303,9 +301,9 @@ class PDFBoxAdapter {
      * @return the Form XObject
      * @throws IOException if an I/O error occurs
      */
-    public PDFFormXObject createFormFromPDFBoxPage(PDDocument sourceDoc, PDPage page, String
key)
-            throws IOException {
-        handleAcroForm(sourceDoc, page);
+    public PDFFormXObject createFormFromPDFBoxPage(PDDocument sourceDoc, PDPage page, String
key,
+            EventBroadcaster eventBroadcaster) throws IOException {
+        handleAcroForm(sourceDoc, page, eventBroadcaster);
 
         PDResources sourcePageResources = page.findResources();
         PDFDictionary pageResources = null;
@@ -321,6 +319,8 @@ class PDFBoxAdapter {
             originalPageContents = (COSStream)pdStream.getCOSObject();
         }
 
+        bindOptionalContent(sourceDoc);
+
         PDFStream pageStream;
         Set filter;
         if (originalPageContents instanceof COSStreamArray) {
@@ -338,14 +338,14 @@ class PDFBoxAdapter {
             pageStream = new PDFStream();
         }
 
-        PDFFormXObject form;
-
-        form = pdfDoc.addFormXObject(null, pageStream,
+        PDFFormXObject form = pdfDoc.addFormXObject(null, pageStream,
                 (pageResources != null ? pageResources.makeReference() : null), key);
+
         if (originalPageContents != null) {
             transferDict(originalPageContents, pageStream, filter);
         }
         transferDict(page.getCOSDictionary(), pageStream, page2form, true);
+
         AffineTransform at = form.getMatrix();
         PDRectangle mediaBox = page.findMediaBox();
         PDRectangle cropBox = page.findCropBox();
@@ -356,6 +356,8 @@ class PDFBoxAdapter {
 
         //Transform to FOP's user space
         at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
+        at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(),
+                mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
         switch (rotation) {
         case 90:
             at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
@@ -373,8 +375,6 @@ class PDFBoxAdapter {
         default:
             //no additional transformations necessary
         }
-        //Compensate for Crop Boxes not starting at 0,0
-        at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
         form.setMatrix(at);
 
         form.setBBox(new Rectangle2D.Float(
@@ -396,7 +396,17 @@ class PDFBoxAdapter {
         return widgets;
     }
 
-    private void handleAcroForm(PDDocument sourceDoc, PDPage page) throws IOException {
+    private void bindOptionalContent(PDDocument sourceDoc) throws IOException {
+        /*
+         * PDOptionalContentProperties ocProperties =
+         * sourceDoc.getDocumentCatalog().getOCProperties(); PDFDictionary ocDictionary =
+         * (PDFDictionary) cloneForNewDocument(ocProperties); if (ocDictionary != null) {
+         * this.pdfDoc.getRoot().put(COSName.OCPROPERTIES.getName(), ocDictionary); }
+         */
+    }
+
+    private void handleAcroForm(PDDocument sourceDoc, PDPage page,
+            EventBroadcaster eventBroadcaster) throws IOException {
         PDDocumentCatalog srcCatalog = sourceDoc.getDocumentCatalog();
         PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
         List pageWidgets = getWidgets(page);
@@ -429,29 +439,25 @@ class PDFBoxAdapter {
         cacheClonedObject(cosPage, this.targetPage);
 
         COSArray annots = (COSArray)page.getCOSDictionary().getDictionaryObject(COSName.ANNOTS);
-        Set fields = Collections.EMPTY_SET;
+        Set fields = Collections.emptySet();
         if (annots != null) {
-            fields = new java.util.HashSet();
+            fields = new HashSet();
             Iterator iter = annots.iterator();
             while (iter.hasNext()) {
-                COSBase annot = (COSBase)iter.next();
-                PDFObject clonedAnnot;
-                //Exclude the parent of the annotation from being duplicated
-                COSObject cosAnnot = (COSObject)annot.getCOSObject();
-                COSDictionary field = (COSDictionary)(cosAnnot).getObject();
-                if (!"Widget".equals(field.getNameAsString(COSName.SUBTYPE))) {
-                    continue; //not a widget
+                COSObject annot = (COSObject) iter.next();
+                COSObject fieldObject = annot;
+                COSDictionary field = (COSDictionary) fieldObject.getObject();
+                if ("Widget".equals(field.getNameAsString(COSName.SUBTYPE))) {
+                    COSObject parent;
+                    while ((parent = (COSObject) field.getItem(COSName.PARENT)) != null)
{
+                        fieldObject = parent;
+                        field = (COSDictionary) fieldObject.getObject();
+                    }
+                    fields.add(fieldObject);
+                    Collection exclude = Collections.singletonList(COSName.P);
+                    PDFObject clonedAnnot = (PDFObject) cloneForNewDocument(annot, annot,
exclude);
+                    targetPage.addAnnotation(clonedAnnot);
                 }
-                COSDictionary parent;
-                while ((parent = (COSDictionary)field.getDictionaryObject(COSName.PARENT))
-                        != null) {
-                    field = parent;
-                }
-                fields.add(field);
-                Collection exclude = Collections.singletonList(COSName.P);
-                clonedAnnot = (PDFObject)cloneForNewDocument(
-                        (cosAnnot).getObject(), cosAnnot, exclude);
-                targetPage.addAnnotation(clonedAnnot);
             }
         }
 
@@ -475,21 +481,16 @@ class PDFBoxAdapter {
                 destAcroForm = new PDFDictionary(pdfDoc.getRoot());
             }
             pdfDoc.registerObject(destAcroForm);
-            catalog.put(COSName.ACRO_FORM.getName(), destAcroForm);
+            catalog.put(COSName.ACRO_FORM.getName(), destAcroForm );
         }
-
-        //Merge fields
-        PDFArray clonedFields = (PDFArray)destAcroForm.get(COSName.FIELDS.getName());
+        PDFArray clonedFields = (PDFArray) destAcroForm.get(COSName.FIELDS.getName());
         if (clonedFields == null) {
-            //No cloned AcroForm, yet
             clonedFields = new PDFArray();
             destAcroForm.put(COSName.FIELDS.getName(), clonedFields);
         }
         for (Iterator iter = fields.iterator(); iter.hasNext();) {
-            //Add collected fields to existing cloned AcroForm
-            COSDictionary field = (COSDictionary)iter.next();
-            PDFDictionary clone = (PDFDictionary)cloneForNewDocument(field);
-            pdfDoc.registerObject(clone);
+            COSObject field = (COSObject) iter.next();
+            PDFDictionary clone = (PDFDictionary) cloneForNewDocument(field);
             clonedFields.add(clone);
         }
     }

Added: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java?rev=1388078&view=auto
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java
(added)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java
Thu Sep 20 15:35:24 2012
@@ -0,0 +1,59 @@
+package org.apache.fop.render.pdf.pdfbox;
+
+import org.apache.fop.events.EventBroadcaster;
+import org.apache.fop.events.EventProducer;
+
+interface PDFBoxEventProducer extends EventProducer {
+
+    /** Provider class for the event producer. */
+    final class Provider {
+
+        private Provider() { }
+
+        /**
+         * Returns an event producer.
+         *
+         * @param broadcaster the event broadcaster to use
+         * @return the event producer
+         */
+        public static PDFBoxEventProducer get(EventBroadcaster broadcaster) {
+            return (PDFBoxEventProducer) broadcaster.getEventProducerFor(
+                    PDFBoxEventProducer.class);
+        }
+    }
+
+    /**
+     * The PDF version of the document being created is less than that of the PDF being inserted.
+     *
+     * @param source the event source
+     * @param outDocVersion PDF version of the document being created
+     * @param inputDocVersion PDF version of the included document
+     * @event.severity WARN
+     */
+    void pdfVersionMismatch(Object source, String outDocVersion, String inputDocVersion);
+
+
+    /**
+     * The document to be included is encrypted.
+     *
+     * @param source the event source
+     * @event.severity ERROR
+     */
+    void encryptedPdf(Object source);
+
+    /**
+     * PDF/A mode is active.
+     *
+     * @param source
+     * @event.severity WARN
+     */
+    void pdfAActive(Object source);
+
+    /**
+     * PDF/X mode is active.
+     *
+     * @param source
+     * @event.severity WARN
+     */
+    void pdfXActive(Object source);
+}

Propchange: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml?rev=1388078&view=auto
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml
(added)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml
Thu Sep 20 15:35:24 2012
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<catalogue xml:lang="en">
+  <message key="pdfVersionMismatch">The version of the loaded PDF is {inputDocVersion}.
This doesn't match the document version {outDocVersion} and could lead to errors.</message>
+  <message key="encryptedPdf">PDF to be embedded must not be encrypted!</message>
+  <message key="pdfAActive">PDF/A mode is active. Embedding a PDF file may result in
a non-compliant file!</message>
+  <message key="pdfXActive">PDF/X mode is active. Embedding a PDF file may result in
a non-compliant file!</message>
+</catalogue>

Propchange: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxEventProducer.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxImageHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxImageHandler.java?rev=1388078&r1=1388077&r2=1388078&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxImageHandler.java
(original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxImageHandler.java
Thu Sep 20 15:35:24 2012
@@ -54,7 +54,8 @@ public class PDFBoxImageHandler extends 
         PDFContentGenerator generator = pdfContext.getGenerator();
         ImagePDF pdfImage = (ImagePDF)image;
 
-        PDFFormXObject form = createFormForPDF(pdfImage, pdfContext.getPage());
+        PDFFormXObject form = createFormForPDF(pdfImage, pdfContext.getPage(),
+                pdfContext.getUserAgent());
         if (form == null) {
             return;
         }
@@ -79,7 +80,7 @@ public class PDFBoxImageHandler extends 
     }
 
     /** {@inheritDoc} */
-    public Class<?> getSupportedImageClass() {
+    public Class getSupportedImageClass() {
         return ImagePDF.class;
     }
 

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java?rev=1388078&r1=1388077&r2=1388078&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java
(original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java
Thu Sep 20 15:35:24 2012
@@ -35,11 +35,9 @@ public class PDFUtil {
     public static int getNormalizedRotation(PDPage page) {
         //Handle the /Rotation entry on the page dict
         int rotation = page.findRotation();
-        while (rotation >= 360) {
-            rotation -= 360;
-        }
+        rotation %= 360;
         if (rotation < 0) {
-            rotation = 0;
+            rotation += 360;
         }
         switch (rotation) {
         case 90:



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Mime
View raw message