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/04/04 14:14:24 UTC

svn commit: r1888345 - in /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff: CFFCharset.java CFFCharsetCID.java CFFCharsetType1.java CFFExpertCharset.java CFFExpertSubsetCharset.java CFFISOAdobeCharset.java CFFParser.java EmbeddedCharset.java

Author: lehmi
Date: Sun Apr  4 14:14:24 2021
New Revision: 1888345

URL: http://svn.apache.org/viewvc?rev=1888345&view=rev
Log:
PDFBOX-5143: split CFFCharset, as suggested by valerybokov, closes #112

Added:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java   (with props)
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java   (with props)
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java   (with props)
Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertCharset.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertSubsetCharset.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFISOAdobeCharset.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java?rev=1888345&r1=1888344&r2=1888345&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java Sun Apr  4 14:14:24 2021
@@ -28,34 +28,12 @@ import java.util.Map;
  */
 public abstract class CFFCharset
 {
-    private final boolean isCIDFont;
-    private final Map<Integer, Integer> sidOrCidToGid = new HashMap<>(250);
-    private final Map<Integer, Integer> gidToSid = new HashMap<>(250);
-    private final Map<String, Integer> nameToSid = new HashMap<>(250);
-
-    // inverse
-    private final Map<Integer, Integer> gidToCid = new HashMap<>();
-    private final Map<Integer, String> gidToName = new HashMap<>(250);
-
-    /**
-     * Package-private constructor for use by subclasses.
-     *
-     * @param isCIDFont true if the parent font is a CIDFont
-     */
-    CFFCharset(boolean isCIDFont)
-    {
-        this.isCIDFont = isCIDFont;
-    }
-
     /**
      * Indicates if the charset belongs to a CID font.
      * 
      * @return true for CID fonts
      */
-    public boolean isCIDFont()
-    {
-        return isCIDFont;
-    }
+    public abstract boolean isCIDFont();
     
     /**
      * Adds a new GID/SID/name combination to the charset.
@@ -63,17 +41,7 @@ public abstract class CFFCharset
      * @param gid GID
      * @param sid SID
      */
-    public void addSID(int gid, int sid, String name)
-    {
-        if (isCIDFont)
-        {
-            throw new IllegalStateException("Not a Type 1-equivalent font");
-        }
-        sidOrCidToGid.put(sid, gid);
-        gidToSid.put(gid, sid);
-        nameToSid.put(name, sid);
-        gidToName.put(gid, name);
-    }
+    public abstract void addSID(int gid, int sid, String name);
 
     /**
      * Adds a new GID/CID combination to the charset.
@@ -81,15 +49,7 @@ public abstract class CFFCharset
      * @param gid GID
      * @param cid CID
      */
-    public void addCID(int gid, int cid)
-    {
-        if (!isCIDFont)
-        {
-            throw new IllegalStateException("Not a CIDFont");
-        }
-        sidOrCidToGid.put(cid, gid);
-        gidToCid.put(gid, cid);
-    }
+    public abstract void addCID(int gid, int cid);
 
     /**
      * Returns the SID for a given GID. SIDs are internal to the font and are not public.
@@ -97,19 +57,7 @@ public abstract class CFFCharset
      * @param sid SID
      * @return GID
      */
-    int getSIDForGID(int sid)
-    {
-        if (isCIDFont)
-        {
-            throw new IllegalStateException("Not a Type 1-equivalent font");
-        }
-        Integer gid = gidToSid.get(sid);
-        if (gid == null)
-        {
-            return 0;
-        }
-        return gid;
-    }
+    abstract int getSIDForGID(int sid);
 
     /**
      * Returns the GID for the given SID. SIDs are internal to the font and are not public.
@@ -117,19 +65,7 @@ public abstract class CFFCharset
      * @param sid SID
      * @return GID
      */
-    int getGIDForSID(int sid)
-    {
-        if (isCIDFont)
-        {
-            throw new IllegalStateException("Not a Type 1-equivalent font");
-        }
-        Integer gid = sidOrCidToGid.get(sid);
-        if (gid == null)
-        {
-            return 0;
-        }
-        return gid;
-    }
+    abstract int getGIDForSID(int sid);
 
     /**
      * Returns the GID for a given CID. Returns 0 if the CID is missing.
@@ -137,19 +73,7 @@ public abstract class CFFCharset
      * @param cid CID
      * @return GID
      */
-    public int getGIDForCID(int cid)
-    {
-        if (!isCIDFont)
-        {
-            throw new IllegalStateException("Not a CIDFont");
-        }
-        Integer gid = sidOrCidToGid.get(cid);
-        if (gid == null)
-        {
-            return 0;
-        }
-        return gid;
-    }
+    public abstract int getGIDForCID(int cid);
 
     /**
      * Returns the SID for a given PostScript name, you would think this is not needed,
@@ -158,19 +82,7 @@ public abstract class CFFCharset
      * @param name PostScript glyph name
      * @return SID
      */
-    int getSID(String name)
-    {
-        if (isCIDFont)
-        {
-            throw new IllegalStateException("Not a Type 1-equivalent font");
-        }
-        Integer sid = nameToSid.get(name);
-        if (sid == null)
-        {
-            return 0;
-        }
-        return sid;
-    }
+    abstract int getSID(String name);
 
     /**
      * Returns the PostScript glyph name for the given GID.
@@ -178,14 +90,7 @@ public abstract class CFFCharset
      * @param gid GID
      * @return PostScript glyph name
      */
-    public String getNameForGID(int gid)
-    {
-        if (isCIDFont)
-        {
-            throw new IllegalStateException("Not a Type 1-equivalent font");
-        }
-        return gidToName.get(gid);
-    }
+    public abstract String getNameForGID(int gid);
 
     /**
      * Returns the CID for the given GID.
@@ -193,18 +98,5 @@ public abstract class CFFCharset
      * @param gid GID
      * @return CID
      */
-    public int getCIDForGID(int gid)
-    {
-        if (!isCIDFont)
-        {
-            throw new IllegalStateException("Not a CIDFont");
-        }
-
-        Integer cid = gidToCid.get(gid);
-        if (cid != null)
-        {
-            return cid;
-        }
-        return 0;
-    }
+    public abstract int getCIDForGID(int gid);
 }

Added: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java?rev=1888345&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java (added)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java Sun Apr  4 14:14:24 2021
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.fontbox.cff;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A CFF charset. A charset is an array of CIDs for all glyphs in the font.
+ *
+ * @author Valery Bokov
+ */
+class CFFCharsetCID extends CFFCharset
+{
+    private final Map<Integer, Integer> sidOrCidToGid = new HashMap<>(250);
+
+    // inverse
+    private final Map<Integer, Integer> gidToCid = new HashMap<>();
+
+    @Override
+    public boolean isCIDFont()
+    {
+        return true;
+    }
+    
+    @Override
+    public void addSID(int gid, int sid, String name)
+    {
+        throw new IllegalStateException("Not a Type 1-equivalent font");
+    }
+
+    @Override
+    public void addCID(int gid, int cid)
+    {
+        sidOrCidToGid.put(cid, gid);
+        gidToCid.put(gid, cid);
+    }
+
+    @Override
+    int getSIDForGID(int sid)
+    {
+        throw new IllegalStateException("Not a Type 1-equivalent font");
+    }
+
+    @Override
+    int getGIDForSID(int sid)
+    {
+        throw new IllegalStateException("Not a Type 1-equivalent font");
+    }
+
+    @Override
+    public int getGIDForCID(int cid)
+    {
+        Integer gid = sidOrCidToGid.get(cid);
+        if (gid == null)
+        {
+            return 0;
+        }
+        return gid;
+    }
+
+    @Override
+    int getSID(String name)
+    {
+        throw new IllegalStateException("Not a Type 1-equivalent font");
+    }
+
+    @Override
+    public String getNameForGID(int gid)
+    {
+        throw new IllegalStateException("Not a Type 1-equivalent font");
+    }
+
+    @Override
+    public int getCIDForGID(int gid)
+    {
+        Integer cid = gidToCid.get(gid);
+        if (cid != null)
+        {
+            return cid;
+        }
+        return 0;
+    }
+}

Propchange: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java?rev=1888345&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java (added)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java Sun Apr  4 14:14:24 2021
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.fontbox.cff;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A CFF charset. A charset is an array of CIDs for all glyphs in the font.
+ *
+ * @author Valery Bokov
+ */
+class CFFCharsetType1 extends CFFCharset
+{
+    private final Map<Integer, Integer> sidOrCidToGid = new HashMap<>(250);
+    private final Map<Integer, Integer> gidToSid = new HashMap<>(250);
+    private final Map<String, Integer> nameToSid = new HashMap<>(250);
+
+    // inverse
+    private final Map<Integer, String> gidToName = new HashMap<>(250);
+
+    @Override
+    public boolean isCIDFont()
+    {
+        return false;
+    }
+    
+    @Override
+    public void addSID(int gid, int sid, String name)
+    {
+        sidOrCidToGid.put(sid, gid);
+        gidToSid.put(gid, sid);
+        nameToSid.put(name, sid);
+        gidToName.put(gid, name);
+    }
+
+    @Override
+    public void addCID(int gid, int cid)
+    {
+        throw new IllegalStateException("Not a CIDFont");
+    }
+
+    @Override
+    int getSIDForGID(int sid)
+    {
+        Integer gid = gidToSid.get(sid);
+        if (gid == null)
+        {
+            return 0;
+        }
+        return gid;
+    }
+
+    @Override
+    int getGIDForSID(int sid)
+    {
+        Integer gid = sidOrCidToGid.get(sid);
+        if (gid == null)
+        {
+            return 0;
+        }
+        return gid;
+    }
+
+    @Override
+    public int getGIDForCID(int cid)
+    {
+        throw new IllegalStateException("Not a CIDFont");
+    }
+
+    @Override
+    int getSID(String name)
+    {
+        Integer sid = nameToSid.get(name);
+        if (sid == null)
+        {
+            return 0;
+        }
+        return sid;
+    }
+
+    @Override
+    public String getNameForGID(int gid)
+    {
+        return gidToName.get(gid);
+    }
+
+    @Override
+    public int getCIDForGID(int gid)
+    {
+        throw new IllegalStateException("Not a CIDFont");
+    }
+}

Propchange: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetType1.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertCharset.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertCharset.java?rev=1888345&r1=1888344&r2=1888345&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertCharset.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertCharset.java Sun Apr  4 14:14:24 2021
@@ -21,7 +21,7 @@ package org.apache.fontbox.cff;
  * 
  * @author Villu Ruusmann
  */
-public final class CFFExpertCharset extends CFFCharset
+public final class CFFExpertCharset extends CFFCharsetType1
 {
     private static final int CHAR_CODE = 0;
     private static final int CHAR_NAME = 1;
@@ -200,7 +200,7 @@ public final class CFFExpertCharset exte
 
     private CFFExpertCharset()
     {
-        super(false);
+        // empty
     }
 
     /**

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertSubsetCharset.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertSubsetCharset.java?rev=1888345&r1=1888344&r2=1888345&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertSubsetCharset.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFExpertSubsetCharset.java Sun Apr  4 14:14:24 2021
@@ -22,7 +22,7 @@ package org.apache.fontbox.cff;
  * 
  * @author Villu Ruusmann
  */
-public final class CFFExpertSubsetCharset extends CFFCharset
+public final class CFFExpertSubsetCharset extends CFFCharsetType1
 {
 
     private static final int CHAR_CODE = 0;
@@ -123,7 +123,7 @@ public final class CFFExpertSubsetCharse
     
     private CFFExpertSubsetCharset()
     {
-        super(false);
+        // empty
     }
 
     /**

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFISOAdobeCharset.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFISOAdobeCharset.java?rev=1888345&r1=1888344&r2=1888345&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFISOAdobeCharset.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFISOAdobeCharset.java Sun Apr  4 14:14:24 2021
@@ -22,7 +22,7 @@ package org.apache.fontbox.cff;
  * 
  * @author Villu Ruusmann
  */
-public final class CFFISOAdobeCharset extends CFFCharset
+public final class CFFISOAdobeCharset extends CFFCharsetType1
 {
     private static final int CHAR_CODE = 0;
     private static final int CHAR_NAME = 1;
@@ -264,7 +264,7 @@ public final class CFFISOAdobeCharset ex
     
     private CFFISOAdobeCharset()
     {
-        super(false);
+        // empty
     }
 
     /**

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=1888345&r1=1888344&r2=1888345&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 Sun Apr  4 14:14:24 2021
@@ -1358,17 +1358,6 @@ public class CFFParser
     }
 
     /**
-     * Inner class representing an embedded CFF charset.
-     */
-    abstract static class EmbeddedCharset extends CFFCharset
-    {
-        protected EmbeddedCharset(boolean isCIDFont)
-        {
-            super(isCIDFont);
-        }
-    }
-
-    /**
      * An empty charset in a malformed CID font.
      */
     private static class EmptyCharset extends EmbeddedCharset

Added: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java?rev=1888345&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java (added)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java Sun Apr  4 14:14:24 2021
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.fontbox.cff;
+
+/**
+ * Class representing an embedded CFF charset.
+ *
+ */
+class EmbeddedCharset extends CFFCharset
+{
+    private final CFFCharset charset;
+
+    EmbeddedCharset(boolean isCIDFont)
+    {
+        charset = isCIDFont ? new CFFCharsetCID() : new CFFCharsetType1();
+    }
+
+    @Override
+    public int getCIDForGID(int gid)
+    {
+        return charset.getCIDForGID(gid);
+    }
+
+    @Override
+    public boolean isCIDFont()
+    {
+        return charset.isCIDFont();
+    }
+
+    @Override
+    public void addSID(int gid, int sid, String name)
+    {
+        charset.addSID(gid, sid, name);
+    }
+
+    @Override
+    public void addCID(int gid, int cid)
+    {
+        charset.addCID(gid, cid);
+    }
+
+    @Override
+    int getSIDForGID(int sid)
+    {
+        return charset.getSIDForGID(sid);
+    }
+
+    @Override
+    int getGIDForSID(int sid)
+    {
+        return charset.getGIDForSID(sid);
+    }
+
+    @Override
+    public int getGIDForCID(int cid)
+    {
+        return charset.getGIDForCID(cid);
+    }
+
+    @Override
+    int getSID(String name)
+    {
+        return charset.getSID(name);
+    }
+
+    @Override
+    public String getNameForGID(int gid)
+    {
+        return charset.getNameForGID(gid);
+    }
+}

Propchange: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/EmbeddedCharset.java
------------------------------------------------------------------------------
    svn:eol-style = native