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 2021/09/05 14:38:41 UTC

svn commit: r1892938 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java

Author: lehmi
Date: Sun Sep  5 14:38:41 2021
New Revision: 1892938

URL: http://svn.apache.org/viewvc?rev=1892938&view=rev
Log:
PDFBOX-5143: DRY, reuse ArrayList instead of recreating it, simplify peekNumbers 

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java?rev=1892938&r1=1892937&r2=1892938&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java Sun Sep  5 14:38:41 2021
@@ -34,9 +34,9 @@ public class Type2CharStringParser
     private static final int CALLSUBR = 10;
     private static final int CALLGSUBR = 29;
 
-    private int hstemCount = 0;
-    private int vstemCount = 0;
-    private List<Object> sequence = null;
+    private int hstemCount;
+    private int vstemCount;
+    private final List<Object> sequence = new ArrayList<>();
     @SuppressWarnings("unused")
     private final String fontName;
     @SuppressWarnings("unused")
@@ -77,17 +77,16 @@ public class Type2CharStringParser
      */
     public List<Object> parse(byte[] bytes, byte[][] globalSubrIndex, byte[][] localSubrIndex) throws IOException
     {
-        return parse(bytes, globalSubrIndex, localSubrIndex, true);
+        // reset values if the parser is used multiple times
+        hstemCount = 0;
+        vstemCount = 0;
+        sequence.clear();
+        return parseSequence(bytes, globalSubrIndex, localSubrIndex);
     }
-    
-    private List<Object> parse(byte[] bytes, byte[][] globalSubrIndex, byte[][] localSubrIndex, boolean init) throws IOException
+
+    private List<Object> parseSequence(byte[] bytes, byte[][] globalSubrIndex,
+            byte[][] localSubrIndex) throws IOException
     {
-        if (init) 
-        {
-            hstemCount = 0;
-            vstemCount = 0;
-            sequence = new ArrayList<>();
-        }
         DataInput input = new DataInput(bytes);
         boolean localSubroutineIndexProvided = localSubrIndex != null && localSubrIndex.length > 0;
         boolean globalSubroutineIndexProvided = globalSubrIndex != null && globalSubrIndex.length > 0;
@@ -122,33 +121,18 @@ public class Type2CharStringParser
     private void processCallSubr(byte[][] globalSubrIndex, byte[][] localSubrIndex)
             throws IOException
     {
-        Integer operand = (Integer) sequence.remove(sequence.size() - 1);
-        // get subrbias
-        int bias = 0;
-        int nSubrs = localSubrIndex.length;
-
-        if (nSubrs < 1240)
-        {
-            bias = 107;
-        }
-        else if (nSubrs < 33900)
-        {
-            bias = 1131;
-        }
-        else
-        {
-            bias = 32768;
-        }
-        int subrNumber = bias + operand;
+        int subrNumber = calculateSubrNumber((Integer) sequence.remove(sequence.size() - 1),
+                localSubrIndex.length);
         if (subrNumber < localSubrIndex.length)
         {
             byte[] subrBytes = localSubrIndex[subrNumber];
-            parse(subrBytes, globalSubrIndex, localSubrIndex, false);
+            parse(subrBytes, globalSubrIndex, localSubrIndex);
             Object lastItem = sequence.get(sequence.size() - 1);
             if (lastItem instanceof CharStringCommand
                     && Type2KeyWord.RET == ((CharStringCommand) lastItem).getType2KeyWord())
             {
-                sequence.remove(sequence.size() - 1); // remove "return" command
+                // remove "return" command
+                sequence.remove(sequence.size() - 1);
             }
         }
     }
@@ -156,48 +140,45 @@ public class Type2CharStringParser
     private void processCallGSubr(byte[][] globalSubrIndex, byte[][] localSubrIndex)
             throws IOException
     {
-        Integer operand = (Integer) sequence.remove(sequence.size() - 1);
-        // get subrbias
-        int bias;
-        int nSubrs = globalSubrIndex.length;
-
-        if (nSubrs < 1240)
-        {
-            bias = 107;
-        }
-        else if (nSubrs < 33900)
-        {
-            bias = 1131;
-        }
-        else
-        {
-            bias = 32768;
-        }
-
-        int subrNumber = bias + operand;
+        int subrNumber = calculateSubrNumber((Integer) sequence.remove(sequence.size() - 1),
+                globalSubrIndex.length);
         if (subrNumber < globalSubrIndex.length)
         {
             byte[] subrBytes = globalSubrIndex[subrNumber];
-            parse(subrBytes, globalSubrIndex, localSubrIndex, false);
+            parse(subrBytes, globalSubrIndex, localSubrIndex);
             Object lastItem = sequence.get(sequence.size() - 1);
             if (lastItem instanceof CharStringCommand
                     && Type2KeyWord.RET == ((CharStringCommand) lastItem).getType2KeyWord())
             {
-                sequence.remove(sequence.size() - 1); // remove "return" command
+                // remove "return" command
+                sequence.remove(sequence.size() - 1);
             }
         }
     }
 
+    private int calculateSubrNumber(int operand, int subrIndexlength)
+    {
+        if (subrIndexlength < 1240)
+        {
+            return 107 + operand;
+        }
+        if (subrIndexlength < 33900)
+        {
+            return 1131 + operand;
+        }
+        return 32768 + operand;
+    }
+
     private CharStringCommand readCommand(int b0, DataInput input) throws IOException
     {
 
         if (b0 == 1 || b0 == 18)
         {
-            hstemCount += peekNumbers().size() / 2;
+            hstemCount += countNumbers() / 2;
         } 
         else if (b0 == 3 || b0 == 19 || b0 == 20 || b0 == 23)
         {
-            vstemCount += peekNumbers().size() / 2;
+            vstemCount += countNumbers() / 2;
         } // End if
 
         if (b0 == 12)
@@ -268,19 +249,17 @@ public class Type2CharStringParser
         return length;
     }
 
-    private List<Number> peekNumbers()
+    private int countNumbers()
     {
-        List<Number> numbers = new ArrayList<>();
+        int count = 0;
         for (int i = sequence.size() - 1; i > -1; i--)
         {
-            Object object = sequence.get(i);
-
-            if (!(object instanceof Number))
+            if (!(sequence.get(i) instanceof Number))
             {
-                return numbers;
+                return count;
             }
-            numbers.add(0, (Number) object);
+            count++;
         }
-        return numbers;
+        return count;
     }
 }