You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2015/12/12 16:31:28 UTC

svn commit: r1719701 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java

Author: lehmi
Date: Sat Dec 12 15:31:28 2015
New Revision: 1719701

URL: http://svn.apache.org/viewvc?rev=1719701&view=rev
Log:
PDFBOX-3151: optimize the Hashmap access based on Philip Helgers proposal, DRY refactoring

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java?rev=1719701&r1=1719700&r2=1719701&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java Sat Dec 12 15:31:28 2015
@@ -17,6 +17,7 @@
 package org.apache.pdfbox.pdmodel.font;
 
 import java.io.IOException;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -44,6 +45,7 @@ public abstract class PDCIDFont implemen
 
     private Map<Integer, Float> widths;
     private float defaultWidth;
+    private float averageWidth;
 
     private final Map<Integer, Float> verticalDisplacementY = new HashMap<Integer, Float>(); // w1y
     private final Map<Integer, Vector> positionVectors = new HashMap<Integer, Vector>();     // v
@@ -239,17 +241,17 @@ public abstract class PDCIDFont implemen
      */
     private Vector getDefaultPositionVector(int cid)
     {
-        float w0;
-        if (widths.containsKey(cid))
-        {
-            w0 = widths.get(cid);
-        }
-        else
+        return new Vector(getWidthForCID(cid) / 2, dw2[0]);
+    }
+
+    private float getWidthForCID(int cid)
+    {
+        Float width = widths.get(cid);
+        if (width == null)
         {
-            w0 = getDefaultWidth();
+            width = getDefaultWidth();
         }
-
-        return new Vector(w0 / 2, dw2[0]);
+        return width;
     }
 
     @Override
@@ -257,14 +259,11 @@ public abstract class PDCIDFont implemen
     {
         int cid = codeToCID(code);
         Vector v = positionVectors.get(cid);
-        if (v != null)
-        {
-            return v;
-        }
-        else
+        if (v == null)
         {
-            return getDefaultPositionVector(cid);
+            v = getDefaultPositionVector(cid);
         }
+        return v;
     }
 
     /**
@@ -277,14 +276,11 @@ public abstract class PDCIDFont implemen
     {
         int cid = codeToCID(code);
         Float w1y = verticalDisplacementY.get(cid);
-        if (w1y != null)
+        if (w1y == null)
         {
-            return w1y;
-        }
-        else
-        {
-            return dw2[1];
+            w1y = dw2[1];
         }
+        return w1y;
     }
 
     @Override
@@ -296,16 +292,7 @@ public abstract class PDCIDFont implemen
         // these widths are supposed to be consistent with the actual widths given in the CIDFont
         // program, but PDFBOX-563 shows that when they are not, Acrobat overrides the embedded
         // font widths with the widths given in the font dictionary
-        
-        int cid = codeToCID(code);
-        if (widths.containsKey(cid))
-        {
-            return widths.get(cid);
-        }
-        else
-        {
-            return getDefaultWidth();
-        }
+        return getWidthForCID(codeToCID(code));
     }
 
     @Override
@@ -318,44 +305,26 @@ public abstract class PDCIDFont implemen
     // todo: this method is highly suspicious, the average glyph width is not usually a good metric
     public float getAverageFontWidth()
     {
-        float totalWidths = 0.0f;
-        float characterCount = 0.0f;
-        COSArray widths = (COSArray) dict.getDictionaryObject(COSName.W);
-
-        if (widths != null)
+        if (averageWidth == 0)
         {
-            for (int i = 0; i < widths.size(); i++)
+            float totalWidths = 0.0f;
+            float characterCount = 0.0f;
+            if (widths != null)
             {
-                COSNumber firstCode = (COSNumber) widths.getObject(i++);
-                COSBase next = widths.getObject(i);
-                if (next instanceof COSArray)
-                {
-                    COSArray array = (COSArray) next;
-                    for (int j = 0; j < array.size(); j++)
-                    {
-                        COSNumber width = (COSNumber) array.get(j);
-                        totalWidths += width.floatValue();
-                        characterCount += 1;
-                    }
-                }
-                else
+                characterCount = widths.size();
+                Collection<Float> widthsValues = widths.values();
+                for (Float width : widthsValues)
                 {
-                    i++;
-                    COSNumber rangeWidth = (COSNumber) widths.getObject(i);
-                    if (rangeWidth.floatValue() > 0)
-                    {
-                        totalWidths += rangeWidth.floatValue();
-                        characterCount += 1;
-                    }
+                    totalWidths += width;
                 }
             }
+            float averageWidth = totalWidths / characterCount;
+            if (averageWidth <= 0 || Float.isNaN(averageWidth))
+            {
+                averageWidth = getDefaultWidth();
+            }
         }
-        float average = totalWidths / characterCount;
-        if (average <= 0 || Float.isNaN(average))
-        {
-            average = getDefaultWidth();
-        }
-        return average;
+        return averageWidth;
     }
 
     /**
@@ -371,7 +340,6 @@ public abstract class PDCIDFont implemen
         {
             cidSystemInfo = new PDCIDSystemInfo(cidSystemInfoDict);
         }
-
         return cidSystemInfo;
     }