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 2018/02/15 20:08:19 UTC

svn commit: r1824354 - in /pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf: SubstitutingCmapLookup.java TrueTypeFont.java

Author: tilman
Date: Thu Feb 15 20:08:19 2018
New Revision: 1824354

URL: http://svn.apache.org/viewvc?rev=1824354&view=rev
Log:
PDFBOX-4106: implement GSUB substitution, by Aaron Madlon-Kay

Added:
    pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java   (with props)
Modified:
    pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java

Added: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java?rev=1824354&view=auto
==============================================================================
--- pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java (added)
+++ pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java Thu Feb 15 20:08:19 2018
@@ -0,0 +1,53 @@
+/*
+ * 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.ttf;
+
+import java.util.List;
+
+/**
+ * A cmap lookup that performs substitution via the 'GSUB' table.
+ *
+ * @author Aaron Madlon-Kay
+ */
+public class SubstitutingCmapLookup implements CmapLookup
+{
+    private final CmapSubtable cmap;
+    private final GlyphSubstitutionTable gsub;
+    private final List<String> enabledFeatures;
+
+    public SubstitutingCmapLookup(CmapSubtable cmap, GlyphSubstitutionTable gsub,
+            List<String> enabledFeatures)
+    {
+        this.cmap = cmap;
+        this.gsub = gsub;
+        this.enabledFeatures = enabledFeatures;
+    }
+
+    @Override
+    public int getGlyphId(int characterCode)
+    {
+        int gid = cmap.getGlyphId(characterCode);
+        String[] scriptTags = OpenTypeScript.getScriptTags(characterCode);
+        return gsub.getSubstitution(gid, scriptTags, enabledFeatures);
+    }
+
+    @Override
+    public List<Integer> getCharCodes(int gid)
+    {
+        return cmap.getCharCodes(gsub.getUnsubstitution(gid));
+    }
+}

Propchange: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/SubstitutingCmapLookup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java?rev=1824354&r1=1824353&r2=1824354&view=diff
==============================================================================
--- pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java (original)
+++ pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java Thu Feb 15 20:08:19 2018
@@ -20,8 +20,10 @@ import java.awt.geom.GeneralPath;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -41,7 +43,8 @@ public class TrueTypeFont implements Fon
     protected Map<String,TTFTable> tables = new HashMap<String,TTFTable>();
     private final TTFDataStream data;
     private Map<String, Integer> postScriptNames;
-    
+    private final List<String> enabledGsubFeatures = new ArrayList<String>();
+
     /**
      * Constructor.  Clients should use the TTFParser to create a new TrueTypeFont object.
      * 
@@ -523,6 +526,15 @@ public class TrueTypeFont implements Fon
     public CmapLookup getUnicodeCmapLookup(boolean isStrict) throws IOException
     {
         CmapSubtable cmap = getUnicodeCmapImpl(isStrict);
+        if (!enabledGsubFeatures.isEmpty())
+        {
+            GlyphSubstitutionTable table = getGsub();
+            if (table != null)
+            {
+                return new SubstitutingCmapLookup(cmap, (GlyphSubstitutionTable) table,
+                        Collections.unmodifiableList(enabledGsubFeatures));
+            }
+        }
         return cmap;
     }
 
@@ -691,6 +703,27 @@ public class TrueTypeFont implements Fon
         return Arrays.<Number>asList(0.001f * scale, 0, 0, 0.001f * scale, 0, 0);
     }
 
+    /**
+     * Enable a particular glyph substitution feature. This feature might not be supported by the
+     * font, or might not be implemented in PDFBox yet.
+     *
+     * @param featureTag The GSUB feature to enable
+     */
+    public void enableGsubFeature(String featureTag)
+    {
+        enabledGsubFeatures.add(featureTag);
+    }
+
+    /**
+     * Disable a particular glyph substitution feature.
+     *
+     * @param featureTag The GSUB feature to disable
+     */
+    public void disableGsubFeature(String featureTag)
+    {
+        enabledGsubFeatures.remove(featureTag);
+    }
+
     @Override
     public String toString()
     {