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/06/20 16:04:58 UTC

svn commit: r1749352 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java

Author: tilman
Date: Mon Jun 20 16:04:58 2016
New Revision: 1749352

URL: http://svn.apache.org/viewvc?rev=1749352&view=rev
Log:
PDFBOX-3382: speed up getWidth()

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=1749352&r1=1749351&r2=1749352&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Mon Jun 20 16:04:58 2016
@@ -21,7 +21,9 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.fontbox.afm.FontMetrics;
@@ -52,12 +54,17 @@ public abstract class PDFont implements
 
     protected final COSDictionary dict;
     private final CMap toUnicodeCMap;
-    private final FontMetrics afmStandard14; // AFM for standard 14 fonts
+    
+    /**
+     * AFM for standard 14 fonts
+     */
+    private final FontMetrics afmStandard14;
 
     private PDFontDescriptor fontDescriptor;
     private List<Float> widths;
     private float avgFontWidth;
     private float fontWidthOfSpace = -1f;
+    private final Map<Integer, Float> codeToWidthMap = new HashMap<Integer, Float>();
 
     /**
      * Constructor for embedding.
@@ -215,6 +222,12 @@ public abstract class PDFont implements
     @Override
     public float getWidth(int code) throws IOException
     {
+        Float width = codeToWidthMap.get(code);
+        if (width != null)
+        {
+            return width;
+        }
+        
         // Acrobat overrides the widths in the font program on the conforming reader's system with
         // the widths specified in the font dictionary." (Adobe Supplement to the ISO 32000)
         //
@@ -230,25 +243,33 @@ public abstract class PDFont implements
             int idx = code - firstChar;
             if (siz > 0 && code >= firstChar && code <= lastChar && idx < siz)
             {
-                return getWidths().get(idx);
+                width = getWidths().get(idx);
+                codeToWidthMap.put(code, width);
+                return width;
             }
 
             PDFontDescriptor fd = getFontDescriptor();
             if (fd != null && fd.hasMissingWidth())
             {
                 // get entry from /MissingWidth entry
-                return fd.getMissingWidth();
+                width = fd.getMissingWidth();
+                codeToWidthMap.put(code, width);
+                return width;
             }
         }
 
         // standard 14 font widths are specified by an AFM
         if (isStandard14())
         {
-            return getStandard14Width(code);
+            width = getStandard14Width(code);
+            codeToWidthMap.put(code, width);
+            return width;
         }
         
         // if there's nothing to override with, then obviously we fall back to the font
-        return getWidthFromFont(code);
+        width = getWidthFromFont(code);
+        codeToWidthMap.put(code, width);
+        return width;
     }
 
     /**