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/07/21 18:13:51 UTC

svn commit: r1753720 - /pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java

Author: tilman
Date: Thu Jul 21 18:13:51 2016
New Revision: 1753720

URL: http://svn.apache.org/viewvc?rev=1753720&view=rev
Log:
PDFBOX-3432: optimize by avoiding calling Collections.max() + DRY refactoring, by Michael Doswald

Modified:
    pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java

Modified: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java?rev=1753720&r1=1753719&r2=1753720&view=diff
==============================================================================
--- pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java (original)
+++ pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java Thu Jul 21 18:13:51 2016
@@ -18,7 +18,6 @@ package org.apache.fontbox.ttf;
 
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -351,17 +350,14 @@ public class CmapSubtable
         Map<Integer, Integer> tmpGlyphToChar = new HashMap<Integer, Integer>(numGlyphs);
         characterCodeToGlyphId = new HashMap<Integer, Integer>(numGlyphs);
         int[] glyphIdArray = data.readUnsignedShortArray(entryCount);
+        int maxGlyphId = 0;
         for (int i = 0; i < entryCount; i++)
         {
+            maxGlyphId = Math.max(maxGlyphId, glyphIdArray[i]);
             tmpGlyphToChar.put(glyphIdArray[i], firstCode + i);
             characterCodeToGlyphId.put(firstCode + i, glyphIdArray[i]);
         }
-        glyphIdToCharacterCode = newGlyphIdToCharacterCode(Collections.max(tmpGlyphToChar.keySet()) + 1);
-        for (Entry<Integer, Integer> entry : tmpGlyphToChar.entrySet())
-        {
-            // link the glyphId with the right character code
-            glyphIdToCharacterCode[entry.getKey()] = entry.getValue();
-        }
+        buildGlyphIdToCharacterCodeLookup(tmpGlyphToChar, maxGlyphId);
     }
 
     /**
@@ -386,6 +382,7 @@ public class CmapSubtable
 
         Map<Integer, Integer> tmpGlyphToChar = new HashMap<Integer, Integer>(numGlyphs);
         characterCodeToGlyphId = new HashMap<Integer, Integer>(numGlyphs);
+        int maxGlyphId = 0;
 
         long currentPosition = data.getCurrentPosition();
 
@@ -402,6 +399,7 @@ public class CmapSubtable
                     if (rangeOffset == 0)
                     {
                         int glyphid = (j + delta) & 0xFFFF;
+                        maxGlyphId = Math.max(glyphid, maxGlyphId);
                         tmpGlyphToChar.put(glyphid, j);
                         characterCodeToGlyphId.put(j, glyphid);
                     }
@@ -417,6 +415,7 @@ public class CmapSubtable
                             glyphIndex = (glyphIndex + delta) & 0xFFFF;
                             if (!tmpGlyphToChar.containsKey(glyphIndex))
                             {
+                                maxGlyphId = Math.max(glyphIndex, maxGlyphId);
                                 tmpGlyphToChar.put(glyphIndex, j);
                                 characterCodeToGlyphId.put(j, glyphIndex);
                             }
@@ -435,7 +434,12 @@ public class CmapSubtable
             LOG.warn("cmap format 4 subtable is empty");
             return;
         }
-        glyphIdToCharacterCode = newGlyphIdToCharacterCode(Collections.max(tmpGlyphToChar.keySet()) + 1);
+        buildGlyphIdToCharacterCodeLookup(tmpGlyphToChar, maxGlyphId);
+    }
+
+    private void buildGlyphIdToCharacterCodeLookup(Map<Integer, Integer> tmpGlyphToChar, int maxGlyphId)
+    {
+        glyphIdToCharacterCode = newGlyphIdToCharacterCode(maxGlyphId + 1);
         for (Entry<Integer, Integer> entry : tmpGlyphToChar.entrySet())
         {
             // link the glyphId with the right character code