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 2022/08/28 07:58:56 UTC

svn commit: r1903719 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java

Author: tilman
Date: Sun Aug 28 07:58:56 2022
New Revision: 1903719

URL: http://svn.apache.org/viewvc?rev=1903719&view=rev
Log:
PDFBOX-4892: optimize with TreeMap

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

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java?rev=1903719&r1=1903718&r2=1903719&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java Sun Aug 28 07:58:56 2022
@@ -21,12 +21,10 @@ import java.io.ByteArrayInputStream;
 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 java.util.Set;
-import java.util.TreeSet;
+import java.util.TreeMap;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -105,7 +103,7 @@ final class PDCIDFontType2Embedder exten
             throws IOException
     {
         // build CID2GIDMap, because the content stream has been written with the old GIDs
-        Map<Integer, Integer> cidToGid = new HashMap<Integer, Integer>(gidToCid.size());
+        TreeMap<Integer, Integer> cidToGid = new TreeMap<Integer, Integer>();
         for (Map.Entry<Integer, Integer> entry : gidToCid.entrySet())
         {
             int newGID = entry.getKey();
@@ -236,9 +234,9 @@ final class PDCIDFontType2Embedder exten
         cidFont.setName(COSName.BASE_FONT, newName);
     }
 
-    private void buildCIDToGIDMap(Map<Integer, Integer> cidToGid) throws IOException
+    private void buildCIDToGIDMap(TreeMap<Integer, Integer> cidToGid) throws IOException
     {
-        int cidMax = Collections.max(cidToGid.keySet());
+        int cidMax = cidToGid.lastKey();
         byte buffer[] = new byte[cidMax * 2 + 2];
         int bi = 0;
         for (int i = 0; i <= cidMax; i++)
@@ -263,9 +261,9 @@ final class PDCIDFontType2Embedder exten
      * Builds the CIDSet entry, required by PDF/A. This lists all CIDs in the font, including those
      * that don't have a GID.
      */
-    private void buildCIDSet(Map<Integer, Integer> cidToGid) throws IOException
+    private void buildCIDSet(TreeMap<Integer, Integer> cidToGid) throws IOException
     {
-        int cidMax = Collections.max(cidToGid.keySet());
+        int cidMax = cidToGid.lastKey();
         byte[] bytes = new byte[cidMax / 8 + 1];
         for (int cid = 0; cid <= cidMax; cid++)
         {
@@ -282,7 +280,7 @@ final class PDCIDFontType2Embedder exten
     /**
      * Builds widths with a custom CIDToGIDMap (for embedding font subset).
      */
-    private void buildWidths(Map<Integer, Integer> cidToGid) throws IOException
+    private void buildWidths(TreeMap<Integer, Integer> cidToGid) throws IOException
     {
         float scaling = 1000f / ttf.getHeader().getUnitsPerEm();
 
@@ -290,7 +288,7 @@ final class PDCIDFontType2Embedder exten
         COSArray ws = new COSArray();
         int prev = Integer.MIN_VALUE;
         // Use a sorted list to get an optimal width array  
-        Set<Integer> keys = new TreeSet<Integer>(cidToGid.keySet());
+        Set<Integer> keys = cidToGid.keySet();
         HorizontalMetricsTable horizontalMetricsTable = ttf.getHorizontalMetrics();
         for (int cid : keys)
         {
@@ -340,7 +338,7 @@ final class PDCIDFontType2Embedder exten
     /**
      * Builds vertical metrics with a custom CIDToGIDMap (for embedding font subset).
      */
-    private void buildVerticalMetrics(Map<Integer, Integer> cidToGid) throws IOException
+    private void buildVerticalMetrics(TreeMap<Integer, Integer> cidToGid) throws IOException
     {
         // The "vhea" and "vmtx" tables that specify vertical metrics shall never be used by a conforming
         // reader. The only way to specify vertical metrics in PDF shall be by means of the DW2 and W2
@@ -365,7 +363,7 @@ final class PDCIDFontType2Embedder exten
         COSArray w2 = new COSArray();
         int prev = Integer.MIN_VALUE;
         // Use a sorted list to get an optimal width array
-        Set<Integer> keys = new TreeSet<Integer>(cidToGid.keySet());
+        Set<Integer> keys = cidToGid.keySet();
         for (int cid : keys)
         {
             // Unlike buildWidths, we look up with cid (not gid) here because this is