pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1721423 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java
Date Tue, 22 Dec 2015 16:24:53 GMT
Author: lehmi
Date: Tue Dec 22 16:24:52 2015
New Revision: 1721423

URL: http://svn.apache.org/viewvc?rev=1721423&view=rev
Log:
PDFBOX-3131: use a range mapping instead of storing a mapping for each single value of a format
1 charset of a CID font

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

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java?rev=1721423&r1=1721422&r2=1721423&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java Tue Dec 22 16:24:52
2015
@@ -1029,6 +1029,7 @@ public class CFFParser
         if (isCIDFont)
         {
             charset.addCID(0, 0);
+            charset.rangesCID2GID = new ArrayList<RangeMapping>();
         }
         else
         {
@@ -1039,18 +1040,18 @@ public class CFFParser
         {
             int rangeFirst = dataInput.readSID();
             int rangeLeft = dataInput.readCard8();
-            for (int j = 0; j < 1 + rangeLeft; j++)
+            if (!isCIDFont)
             {
-                int sid = rangeFirst + j;
-                if (isCIDFont)
-                {
-                    charset.addCID(gid + j, sid);
-                }
-                else
+                for (int j = 0; j < 1 + rangeLeft; j++)
                 {
+                    int sid = rangeFirst + j;
                     charset.addSID(gid + j, sid, readString(sid));
                 }
             }
+            else
+            {
+                charset.rangesCID2GID.add(new RangeMapping(gid, rangeFirst, rangeLeft));
+            }
             gid += rangeLeft;
         }
         return charset;
@@ -1064,7 +1065,7 @@ public class CFFParser
         if (isCIDFont)
         {
             charset.addCID(0, 0);
-            charset.rangesCID2GID = new ArrayList<Format2Charset.Range>();
+            charset.rangesCID2GID = new ArrayList<RangeMapping>();
         }
         else
         {
@@ -1085,7 +1086,7 @@ public class CFFParser
             }
             else
             {
-                charset.rangesCID2GID.add(new Format2Charset.Range(gid, first, nLeft));
+                charset.rangesCID2GID.add(new RangeMapping(gid, first, nLeft));
             }
             gid += nLeft;
         }
@@ -1342,6 +1343,7 @@ public class CFFParser
     private static class Format1Charset extends EmbeddedCharset
     {
         private int format;
+        private List<RangeMapping> rangesCID2GID;
 
         protected Format1Charset(boolean isCIDFont)
         {
@@ -1349,6 +1351,32 @@ public class CFFParser
         }
 
         @Override
+        public int getCIDForGID(int gid)
+        {
+            for (RangeMapping mapping : rangesCID2GID)
+            {
+                if (mapping.isInRange(gid))
+                {
+                    return mapping.mapValue(gid);
+                }
+            }
+            return super.getCIDForGID(gid);
+        }
+        
+        @Override
+        public int getGIDForCID(int cid)
+        {
+            for (RangeMapping mapping : rangesCID2GID)
+            {
+                if (mapping.isInReverseRange(cid))
+                {
+                    return mapping.mapReverseValue(cid);
+                }
+            }
+            return super.getGIDForCID(cid);
+        }
+        
+        @Override
         public String toString()
         {
             return getClass().getName() + "[format=" + format + "]";
@@ -1361,7 +1389,7 @@ public class CFFParser
     private static class Format2Charset extends EmbeddedCharset
     {
         private int format;
-        private List<Range> rangesCID2GID;
+        private List<RangeMapping> rangesCID2GID;
         
         protected Format2Charset(boolean isCIDFont)
         {
@@ -1371,11 +1399,11 @@ public class CFFParser
         @Override
         public int getCIDForGID(int gid)
         {
-            for (Range range : rangesCID2GID)
+            for (RangeMapping mapping : rangesCID2GID)
             {
-                if (range.isInRange(gid))
+                if (mapping.isInRange(gid))
                 {
-                    return range.mapValue(gid);
+                    return mapping.mapValue(gid);
                 }
             }
             return super.getCIDForGID(gid);
@@ -1384,11 +1412,11 @@ public class CFFParser
         @Override
         public int getGIDForCID(int cid)
         {
-            for (Range range : rangesCID2GID)
+            for (RangeMapping mapping : rangesCID2GID)
             {
-                if (range.isInReverseRange(cid))
+                if (mapping.isInReverseRange(cid))
                 {
-                    return range.mapReverseValue(cid);
+                    return mapping.mapReverseValue(cid);
                 }
             }
             return super.getGIDForCID(cid);
@@ -1400,63 +1428,64 @@ public class CFFParser
             return getClass().getName() + "[format=" + format + "]";
         }
 
-        /**
-         * Inner class representing a range of a charset. 
-         */
-        private static class Range
+    }
+
+    /**
+     * Inner class representing a rang mapping for a CID charset. 
+     */
+    private static final class RangeMapping
+    {
+        private final int startValue;
+        private final int endValue;
+        private final int startMappedValue;
+        private final int endMappedValue;
+
+        private RangeMapping(int startGID, int first, int nLeft)
+        {
+            this.startValue = startGID;
+            endValue = startValue + nLeft;
+            this.startMappedValue = first;
+            endMappedValue = startMappedValue + nLeft;
+        }
+        
+        boolean isInRange(int value)
         {
-            private final int startValue;
-            private final int endValue;
-            private final int startMappedValue;
-            private final int endMappedValue;
-
-            private Range(int startGID, int first, int nLeft)
-            {
-                this.startValue = startGID;
-                endValue = startValue + nLeft;
-                this.startMappedValue = first;
-                endMappedValue = startMappedValue + nLeft;
-            }
-            
-            boolean isInRange(int value)
+            return value >= startValue && value <= endValue;
+        }
+        
+        boolean isInReverseRange(int value)
+        {
+            return value >= startMappedValue && value <= endMappedValue;
+        }
+
+        int mapValue(int value)
+        {
+            if (isInRange(value))
             {
-                return value >= startValue && value <= endValue;
+                return startMappedValue + (value - startValue);
             }
-            
-            boolean isInReverseRange(int value)
+            else
             {
-                return value >= startMappedValue && value <= endMappedValue;
+                return 0;
             }
+        }
 
-            int mapValue(int value)
+        int mapReverseValue(int value)
+        {
+            if (isInReverseRange(value))
             {
-                if (isInRange(value))
-                {
-                    return startMappedValue + (value - startValue);
-                }
-                else
-                {
-                    return 0;
-                }
+                return startValue + (value - startMappedValue);
             }
-
-            int mapReverseValue(int value)
+            else
             {
-                if (isInReverseRange(value))
-                {
-                    return startValue + (value - startMappedValue);
-                }
-                else
-                {
-                    return 0;
-                }
+                return 0;
             }
+        }
 
-            @Override
-            public String toString()
-            {
-                return getClass().getName() + "[startGID=" + startValue + ", endGID=" + endValue
+  ", first=" + startMappedValue +"]";
-            }
+        @Override
+        public String toString()
+        {
+            return getClass().getName() + "[start value=" + startValue + ", end value=" +
endValue +  ", start mapped-value=" + startMappedValue +  ", end mapped-value=" + endMappedValue
+"]";
         }
     }
 



Mime
View raw message