You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/08/06 13:11:34 UTC

svn commit: r1755395 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java

Author: tilman
Date: Sat Aug  6 13:11:33 2016
New Revision: 1755395

URL: http://svn.apache.org/viewvc?rev=1755395&view=rev
Log:
PDFBOX-3442: cache direct fonts

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java?rev=1755395&r1=1755394&r2=1755395&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java Sat Aug  6 13:11:33 2016
@@ -17,7 +17,10 @@
 package org.apache.pdfbox.pdmodel;
 
 import java.io.IOException;
+import java.lang.ref.SoftReference;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
@@ -48,6 +51,11 @@ public final class PDResources implement
 {
     private final COSDictionary resources;
     private final ResourceCache cache;
+    
+    // PDFBOX-3442 cache fonts that are not indirect objects, as these aren't cached in ResourceCache
+    // and this would result in huge memory footprint in text extraction
+    private final Map <COSName,SoftReference<PDFont>> directFontCache = 
+            new HashMap<COSName, SoftReference<PDFont>>();
 
     /**
      * Constructor for embedding.
@@ -102,7 +110,7 @@ public final class PDResources implement
      * Returns the font resource with the given name, or null if none exists.
      *
      * @param name Name of the font resource.
-     * @throws java.io.IOException if something went wrong.
+     * @throws IOException if something went wrong.
      */
     public PDFont getFont(COSName name) throws IOException
     {
@@ -115,6 +123,18 @@ public final class PDResources implement
                 return cached;
             }
         }
+        else if (indirect == null)
+        {
+            SoftReference<PDFont> ref = directFontCache.get(name);
+            if (ref != null)
+            {
+                PDFont cached = ref.get();
+                if (cached != null)
+                {
+                    return cached;
+                }
+            }
+        }
 
         PDFont font = null;
         COSDictionary dict = (COSDictionary)get(COSName.FONT, name);
@@ -123,10 +143,14 @@ public final class PDResources implement
             font = PDFontFactory.createFont(dict);
         }
         
-        if (cache != null)
+        if (cache != null && indirect != null)
         {
             cache.put(indirect, font);
         }
+        else if (indirect == null)
+        {
+            directFontCache.put(name, new SoftReference<PDFont>(font));
+        }
         return font;
     }
 
@@ -231,7 +255,7 @@ public final class PDResources implement
      * Returns the shading resource with the given name, or null if none exists.
      *
      * @param name Name of the shading resource.
-     * @throws java.io.IOException if something went wrong.
+     * @throws IOException if something went wrong.
      */
     public PDShading getShading(COSName name) throws IOException
     {
@@ -264,7 +288,7 @@ public final class PDResources implement
      * Returns the pattern resource with the given name, or null if none exists.
      * 
      * @param name Name of the pattern resource.
-     * @throws java.io.IOException if something went wrong.
+     * @throws IOException if something went wrong.
      */
     public PDAbstractPattern getPattern(COSName name) throws IOException
     {
@@ -355,7 +379,7 @@ public final class PDResources implement
      * Returns the XObject resource with the given name, or null if none exists.
      * 
      * @param name Name of the XObject resource.
-     * @throws java.io.IOException if something went wrong.
+     * @throws IOException if something went wrong.
      */
     public PDXObject getXObject(COSName name) throws IOException
     {