pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1753721 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
Date Thu, 21 Jul 2016 18:13:55 GMT
Author: tilman
Date: Thu Jul 21 18:13:55 2016
New Revision: 1753721

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

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

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java?rev=1753721&r1=1753720&r2=1753721&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java Thu Jul 21
18:13:55 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



Mime
View raw message