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 2014/12/21 16:06:57 UTC

svn commit: r1647151 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java

Author: tilman
Date: Sun Dec 21 15:06:57 2014
New Revision: 1647151

URL: http://svn.apache.org/r1647151
Log:
PDFBOX-2576: split up long method into smaller ones

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java?rev=1647151&r1=1647150&r2=1647151&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java Sun Dec 21 15:06:57 2014
@@ -37,7 +37,7 @@ public class CMapParser
     private static final String MARK_END_OF_DICTIONARY = ">>";
     private static final String MARK_END_OF_ARRAY = "]";
 
-    private byte[] tokenParserByteBuffer = new byte[512];
+    private final byte[] tokenParserByteBuffer = new byte[512];
 
     /**
      * Creates a new instance of CMapParser.
@@ -105,7 +105,7 @@ public class CMapParser
         PushbackInputStream cmapStream = new PushbackInputStream(input);
         CMap result = new CMap();
         Object previousToken = null;
-        Object token = null;
+        Object token;
         while ((token = parseNextToken(cmapStream)) != null)
         {
             if (token instanceof Operator)
@@ -113,10 +113,7 @@ public class CMapParser
                 Operator op = (Operator) token;
                 if (op.op.equals("usecmap"))
                 {
-                    LiteralName useCmapName = (LiteralName) previousToken;
-                    InputStream useStream = getExternalCMap(useCmapName.name);
-                    CMap useCMap = parse(useStream);
-                    result.useCmap(useCMap);
+                    parseUsecmap(previousToken, result);
                 }
                 else if (op.op.equals("endcmap"))
                 {
@@ -125,251 +122,289 @@ public class CMapParser
                 }
                 else if (op.op.equals("begincodespacerange"))
                 {
-                    Number cosCount = (Number) previousToken;
-                    for (int j = 0; j < cosCount.intValue(); j++)
-                    {
-                        Object nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof Operator)
-                        {
-                            if (!((Operator) nextToken).op.equals("endcodespacerange"))
-                            {
-                                throw new IOException("Error : ~codespacerange contains an unexpected operator : "
-                                        + ((Operator) nextToken).op);
-                            }
-                            break;
-                        }
-                        byte[] startRange = (byte[]) nextToken;
-                        byte[] endRange = (byte[]) parseNextToken(cmapStream);
-                        CodespaceRange range = new CodespaceRange();
-                        range.setStart(startRange);
-                        range.setEnd(endRange);
-                        result.addCodespaceRange(range);
-                    }
+                    parseBegincodespacerange(previousToken, cmapStream, result);
                 }
                 else if (op.op.equals("beginbfchar"))
                 {
-                    Number cosCount = (Number) previousToken;
-                    for (int j = 0; j < cosCount.intValue(); j++)
-                    {
-                        Object nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof Operator)
-                        {
-                            if (!((Operator) nextToken).op.equals("endbfchar"))
-                            {
-                                throw new IOException("Error : ~bfchar contains an unexpected operator : "
-                                        + ((Operator) nextToken).op);
-                            }
-                            break;
-                        }
-                        byte[] inputCode = (byte[]) nextToken;
-                        nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof byte[])
-                        {
-                            byte[] bytes = (byte[]) nextToken;
-                            String value = createStringFromBytes(bytes);
-                            result.addCharMapping(inputCode, value);
-                        }
-                        else if (nextToken instanceof LiteralName)
-                        {
-                            result.addCharMapping(inputCode, ((LiteralName) nextToken).name);
-                        }
-                        else
-                        {
-                            throw new IOException("Error parsing CMap beginbfchar, expected{COSString "
-                                    + "or COSName} and not " + nextToken);
-                        }
-                    }
+                    parseBeginbfchar(previousToken, cmapStream, result);
                 }
                 else if (op.op.equals("beginbfrange"))
                 {
-                    Number cosCount = (Number) previousToken;
-
-                    for (int j = 0; j < cosCount.intValue(); j++)
-                    {
-                        Object nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof Operator)
-                        {
-                            if (!((Operator) nextToken).op.equals("endbfrange"))
-                            {
-                                throw new IOException("Error : ~bfrange contains an unexpected operator : "
-                                        + ((Operator) nextToken).op);
-                            }
-                            break;
-                        }
-                        byte[] startCode = (byte[]) nextToken;
-                        byte[] endCode = (byte[]) parseNextToken(cmapStream);
-                        nextToken = parseNextToken(cmapStream);
-                        List<byte[]> array = null;
-                        byte[] tokenBytes = null;
-                        if (nextToken instanceof List<?>)
-                        {
-                            array = (List<byte[]>) nextToken;
-                            tokenBytes = array.get(0);
-                        }
-                        else
-                        {
-                            tokenBytes = (byte[]) nextToken;
-                        }
-                        boolean done = false;
-                        // don't add 1:1 mappings to reduce the memory footprint
-                        /*if (Arrays.equals(startCode, tokenBytes))
-                        {
-                            done = true;
-                        }*/
-                        String value = null;
-
-                        int arrayIndex = 0;
-                        while (!done)
-                        {
-                            if (compare(startCode, endCode) >= 0)
-                            {
-                                done = true;
-                            }
-                            value = createStringFromBytes(tokenBytes);
-                            result.addCharMapping(startCode, value);
-                            increment(startCode);
-
-                            if (array == null)
-                            {
-                                increment(tokenBytes);
-                            }
-                            else
-                            {
-                                arrayIndex++;
-                                if (arrayIndex < array.size())
-                                {
-                                    tokenBytes = (byte[]) array.get(arrayIndex);
-                                }
-                            }
-                        }
-                    }
+                    parseBeginbfrange(previousToken, cmapStream, result);
                 }
                 else if (op.op.equals("begincidchar"))
                 {
-                    Number cosCount = (Number) previousToken;
-                    for (int j = 0; j < cosCount.intValue(); j++)
-                    {
-                        Object nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof Operator)
-                        {
-                            if (!((Operator) nextToken).op.equals("endcidchar"))
-                            {
-                                throw new IOException("Error : ~cidchar contains an unexpected operator : "
-                                        + ((Operator) nextToken).op);
-                            }
-                            break;
-                        }
-                        byte[] inputCode = (byte[]) nextToken;
-                        int mappedCode = (Integer) parseNextToken(cmapStream);
-                        int mappedCID = createIntFromBytes(inputCode);
-                        result.addCIDMapping(mappedCode, mappedCID);
-                    }
+                    parseBegincidchar(previousToken, cmapStream, result);
                 }
                 else if (op.op.equals("begincidrange"))
                 {
-                    int numberOfLines = (Integer) previousToken;
-                    for (int n = 0; n < numberOfLines; n++)
-                    {
-                        Object nextToken = parseNextToken(cmapStream);
-                        if (nextToken instanceof Operator)
-                        {
-                            if (!((Operator) nextToken).op.equals("endcidrange"))
-                            {
-                                throw new IOException("Error : ~cidrange contains an unexpected operator : "
-                                        + ((Operator) nextToken).op);
-                            }
-                            break;
-                        }
-                        byte[] startCode = (byte[]) nextToken;
-                        int start = createIntFromBytes(startCode);
-                        byte[] endCode = (byte[]) parseNextToken(cmapStream);
-                        int end = createIntFromBytes(endCode);
-                        int mappedCode = (Integer) parseNextToken(cmapStream);
-                        if (startCode.length <= 2 && endCode.length <= 2)
-                        {
-                            result.addCIDRange((char) start, (char) end, mappedCode);
-                        }
-                        else
-                        {
-                            // TODO Is this even possible?
-                            int endOfMappings = mappedCode + end - start;
-                            while (mappedCode <= endOfMappings)
-                            {
-                                int mappedCID = createIntFromBytes(startCode);
-                                result.addCIDMapping(mappedCode++, mappedCID);
-                                increment(startCode);
-                            }
-                        }
-                    }
+                    parseBegincidrange(previousToken, cmapStream, result);
                 }
             }
             else if (token instanceof LiteralName)
             {
-                LiteralName literal = (LiteralName) token;
-                if ("WMode".equals(literal.name))
+                parseLiteralName(token, cmapStream, result);
+            }
+            previousToken = token;
+        }
+        return result;
+    }
+
+    private void parseUsecmap(Object previousToken, CMap result) throws IOException
+    {
+        LiteralName useCmapName = (LiteralName) previousToken;
+        InputStream useStream = getExternalCMap(useCmapName.name);
+        CMap useCMap = parse(useStream);
+        result.useCmap(useCMap);
+    }
+
+    private void parseLiteralName(Object token, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        LiteralName literal = (LiteralName) token;
+        if ("WMode".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof Integer)
+            {
+                result.setWMode((Integer) next);
+            }
+        }
+        else if ("CMapName".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof LiteralName)
+            {
+                result.setName(((LiteralName) next).name);
+            }
+        }
+        else if ("CMapVersion".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof Number)
+            {
+                result.setVersion(((Number) next).toString());
+            }
+            else if (next instanceof String)
+            {
+                result.setVersion((String) next);
+            }
+        }
+        else if ("CMapType".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof Integer)
+            {
+                result.setType((Integer) next);
+            }
+        }
+        else if ("Registry".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof String)
+            {
+                result.setRegistry((String) next);
+            }
+        }
+        else if ("Ordering".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof String)
+            {
+                result.setOrdering((String) next);
+            }
+        }
+        else if ("Supplement".equals(literal.name))
+        {
+            Object next = parseNextToken(cmapStream);
+            if (next instanceof Integer)
+            {
+                result.setSupplement((Integer) next);
+            }
+        }
+    }
+
+    private void parseBegincodespacerange(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        Number cosCount = (Number) previousToken;
+        for (int j = 0; j < cosCount.intValue(); j++)
+        {
+            Object nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof Operator)
+            {
+                if (!((Operator) nextToken).op.equals("endcodespacerange"))
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof Integer)
-                    {
-                        result.setWMode((Integer) next);
-                    }
+                    throw new IOException("Error : ~codespacerange contains an unexpected operator : "
+                            + ((Operator) nextToken).op);
+                }
+                break;
+            }
+            byte[] startRange = (byte[]) nextToken;
+            byte[] endRange = (byte[]) parseNextToken(cmapStream);
+            CodespaceRange range = new CodespaceRange();
+            range.setStart(startRange);
+            range.setEnd(endRange);
+            result.addCodespaceRange(range);
+        }
+    }
+
+    private void parseBeginbfchar(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        Number cosCount = (Number) previousToken;
+        for (int j = 0; j < cosCount.intValue(); j++)
+        {
+            Object nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof Operator)
+            {
+                if (!((Operator) nextToken).op.equals("endbfchar"))
+                {
+                    throw new IOException("Error : ~bfchar contains an unexpected operator : "
+                            + ((Operator) nextToken).op);
                 }
-                else if ("CMapName".equals(literal.name))
+                break;
+            }
+            byte[] inputCode = (byte[]) nextToken;
+            nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof byte[])
+            {
+                byte[] bytes = (byte[]) nextToken;
+                String value = createStringFromBytes(bytes);
+                result.addCharMapping(inputCode, value);
+            }
+            else if (nextToken instanceof LiteralName)
+            {
+                result.addCharMapping(inputCode, ((LiteralName) nextToken).name);
+            }
+            else
+            {
+                throw new IOException("Error parsing CMap beginbfchar, expected{COSString "
+                        + "or COSName} and not " + nextToken);
+            }
+        }
+    }
+
+    private void parseBegincidrange(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        int numberOfLines = (Integer) previousToken;
+        for (int n = 0; n < numberOfLines; n++)
+        {
+            Object nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof Operator)
+            {
+                if (!((Operator) nextToken).op.equals("endcidrange"))
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof LiteralName)
-                    {
-                        result.setName(((LiteralName) next).name);
-                    }
+                    throw new IOException("Error : ~cidrange contains an unexpected operator : "
+                            + ((Operator) nextToken).op);
                 }
-                else if ("CMapVersion".equals(literal.name))
+                break;
+            }
+            byte[] startCode = (byte[]) nextToken;
+            int start = createIntFromBytes(startCode);
+            byte[] endCode = (byte[]) parseNextToken(cmapStream);
+            int end = createIntFromBytes(endCode);
+            int mappedCode = (Integer) parseNextToken(cmapStream);
+            if (startCode.length <= 2 && endCode.length <= 2)
+            {
+                result.addCIDRange((char) start, (char) end, mappedCode);
+            }
+            else
+            {
+                // TODO Is this even possible?
+                int endOfMappings = mappedCode + end - start;
+                while (mappedCode <= endOfMappings)
+                {
+                    int mappedCID = createIntFromBytes(startCode);
+                    result.addCIDMapping(mappedCode++, mappedCID);
+                    increment(startCode);
+                }
+            }
+        }
+    }
+
+    private void parseBegincidchar(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        Number cosCount = (Number) previousToken;
+        for (int j = 0; j < cosCount.intValue(); j++)
+        {
+            Object nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof Operator)
+            {
+                if (!((Operator) nextToken).op.equals("endcidchar"))
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof Number)
-                    {
-                        result.setVersion(((Number) next).toString());
-                    }
-                    else if (next instanceof String)
-                    {
-                        result.setVersion((String) next);
-                    }
+                    throw new IOException("Error : ~cidchar contains an unexpected operator : "
+                            + ((Operator) nextToken).op);
                 }
-                else if ("CMapType".equals(literal.name))
+                break;
+            }
+            byte[] inputCode = (byte[]) nextToken;
+            int mappedCode = (Integer) parseNextToken(cmapStream);
+            int mappedCID = createIntFromBytes(inputCode);
+            result.addCIDMapping(mappedCode, mappedCID);
+        }
+    }
+
+    private void parseBeginbfrange(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
+    {
+        Number cosCount = (Number) previousToken;
+
+        for (int j = 0; j < cosCount.intValue(); j++)
+        {
+            Object nextToken = parseNextToken(cmapStream);
+            if (nextToken instanceof Operator)
+            {
+                if (!((Operator) nextToken).op.equals("endbfrange"))
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof Integer)
-                    {
-                        result.setType((Integer) next);
-                    }
+                    throw new IOException("Error : ~bfrange contains an unexpected operator : "
+                            + ((Operator) nextToken).op);
                 }
-                else if ("Registry".equals(literal.name))
+                break;
+            }
+            byte[] startCode = (byte[]) nextToken;
+            byte[] endCode = (byte[]) parseNextToken(cmapStream);
+            nextToken = parseNextToken(cmapStream);
+            List<byte[]> array = null;
+            byte[] tokenBytes;
+            if (nextToken instanceof List<?>)
+            {
+                array = (List<byte[]>) nextToken;
+                tokenBytes = array.get(0);
+            }
+            else
+            {
+                tokenBytes = (byte[]) nextToken;
+            }
+            boolean done = false;
+            // don't add 1:1 mappings to reduce the memory footprint
+            /*if (Arrays.equals(startCode, tokenBytes))
+             {
+             done = true;
+             }*/
+            String value = null;
+
+            int arrayIndex = 0;
+            while (!done)
+            {
+                if (compare(startCode, endCode) >= 0)
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof String)
-                    {
-                        result.setRegistry((String) next);
-                    }
+                    done = true;
                 }
-                else if ("Ordering".equals(literal.name))
+                value = createStringFromBytes(tokenBytes);
+                result.addCharMapping(startCode, value);
+                increment(startCode);
+
+                if (array == null)
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof String)
-                    {
-                        result.setOrdering((String) next);
-                    }
+                    increment(tokenBytes);
                 }
-                else if ("Supplement".equals(literal.name))
+                else
                 {
-                    Object next = parseNextToken(cmapStream);
-                    if (next instanceof Integer)
+                    arrayIndex++;
+                    if (arrayIndex < array.size())
                     {
-                        result.setSupplement((Integer) next);
+                        tokenBytes = (byte[]) array.get(arrayIndex);
                     }
                 }
             }
-            previousToken = token;
         }
-        return result;
     }
 
     /**