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 2023/05/14 09:15:47 UTC

svn commit: r1909811 - in /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf: GlyphSubstitutionTable.java gsub/GlyphSubstitutionDataExtractor.java

Author: tilman
Date: Sun May 14 09:15:47 2023
New Revision: 1909811

URL: http://svn.apache.org/viewvc?rev=1909811&view=rev
Log:
PDFBOX-5603: add reading Multiple Substitution Format 1

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java?rev=1909811&r1=1909810&r2=1909811&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java Sun May 14 09:15:47 2023
@@ -47,8 +47,10 @@ import org.apache.fontbox.ttf.table.comm
 import org.apache.fontbox.ttf.table.gsub.LigatureSetTable;
 import org.apache.fontbox.ttf.table.gsub.LigatureTable;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeLigatureSubstitutionSubstFormat1;
+import org.apache.fontbox.ttf.table.gsub.LookupTypeMultipleSubstitutionFormat1;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeSingleSubstFormat1;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeSingleSubstFormat2;
+import org.apache.fontbox.ttf.table.gsub.SequenceTable;
 
 /**
  * A glyph substitution 'GSUB' table in a TrueType or OpenType font.
@@ -255,16 +257,25 @@ public class GlyphSubstitutionTable exte
                 // Single Substitution Subtable
                 // https://docs.microsoft.com/en-us/typography/opentype/spec/gsub#SS
                 return readSingleLookupSubTable(data, offset);
+            case 2:
+                // Multiple Substitution Subtable
+                // https://learn.microsoft.com/en-us/typography/opentype/spec/gsub#lookuptype-2-multiple-substitution-subtable
+                return readMultipleSubstitutionSubtable(data, offset);
             case 4:
                 // Ligature Substitution Subtable
                 // https://docs.microsoft.com/en-us/typography/opentype/spec/gsub#LS
                 return readLigatureSubstitutionSubtable(data, offset);
+
+                // when creating a new LookupSubTable derived type, don't forget to add a "switch"
+                // in readLookupTable() and add the type in GlyphSubstitutionDataExtractor.extractData()
+
             default:
                 // Other lookup types are not supported
                 LOG.debug("Type " + lookupType
                         + " GSUB lookup table is not supported and will be ignored");
                 return null;
                 //TODO next: implement type 6
+                // https://learn.microsoft.com/en-us/typography/opentype/spec/gsub#lookuptype-6-chained-contexts-substitution-subtable
                 // see e.g. readChainedContextualSubTable in Apache FOP
                 // https://github.com/apache/xmlgraphics-fop/blob/1323c2e3511eb23c7dd9b8fb74463af707fa972d/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/OTFAdvancedTypographicTableReader.java#L898
         }
@@ -295,6 +306,7 @@ public class GlyphSubstitutionTable exte
         switch (lookupType)
         {
         case 1:
+        case 2:
         case 4:
             for (int i = 0; i < subTableCount; i++)
             {
@@ -361,6 +373,49 @@ public class GlyphSubstitutionTable exte
         }
     }
 
+    private LookupSubTable readMultipleSubstitutionSubtable(TTFDataStream data, long offset)
+            throws IOException
+    {
+        data.seek(offset);
+        int substFormat = data.readUnsignedShort();
+
+        if (substFormat != 1)
+        {
+            throw new IOException(
+                    "The expected SubstFormat for LigatureSubstitutionTable is 1");
+        }
+
+        int coverage = data.readUnsignedShort();
+        int sequenceCount = data.readUnsignedShort();
+        int[] sequenceOffsets = new int[sequenceCount];
+        for (int i = 0; i < sequenceCount; i++)
+        {
+            sequenceOffsets[i] = data.readUnsignedShort();
+        }
+
+        CoverageTable coverageTable = readCoverageTable(data, offset + coverage);
+
+        if (sequenceCount != coverageTable.getSize())
+        {
+            throw new IOException(
+                    "According to the OpenTypeFont specifications, the coverage count should be equal to the no. of SequenceTables");
+        }
+
+        SequenceTable[] sequenceTables = new SequenceTable[sequenceCount];
+        for (int i = 0; i < sequenceCount; i++)
+        {
+            data.seek(offset + sequenceOffsets[i]);
+            int glyphCount = data.readUnsignedShort();
+            for (int j = 0; j < glyphCount; ++j)
+            {
+                int substituteGlyphIDs[] = data.readUnsignedShortArray(glyphCount);
+                sequenceTables[i] = new SequenceTable(glyphCount, substituteGlyphIDs);            
+            }
+        }
+
+        return new LookupTypeMultipleSubstitutionFormat1(substFormat, coverageTable, sequenceTables);
+    }
+
     private LookupSubTable readLigatureSubstitutionSubtable(TTFDataStream data, long offset)
             throws IOException
     {
@@ -412,7 +467,6 @@ public class GlyphSubstitutionTable exte
         data.seek(ligatureSetTableLocation);
 
         int ligatureCount = data.readUnsignedShort();
-        LOG.debug("ligatureCount=" + ligatureCount);
 
         int[] ligatureOffsets = new int[ligatureCount];
         LigatureTable[] ligatureTables = new LigatureTable[ligatureCount];
@@ -632,7 +686,7 @@ public class GlyphSubstitutionTable exte
             LookupTable lookupTable = lookupListTable.getLookups()[lookupListIndex];
             if (lookupTable.getLookupType() != 1)
             {
-                LOG.debug("Skipping GSUB feature '" + featureRecord.getFeatureTag()
+                LOG.warn("Skipping GSUB feature '" + featureRecord.getFeatureTag()
                         + "' because it requires unsupported lookup table type "
                         + lookupTable.getLookupType());
                 continue;

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java?rev=1909811&r1=1909810&r2=1909811&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java Sun May 14 09:15:47 2023
@@ -39,13 +39,16 @@ import org.apache.fontbox.ttf.table.comm
 import org.apache.fontbox.ttf.table.gsub.LigatureSetTable;
 import org.apache.fontbox.ttf.table.gsub.LigatureTable;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeLigatureSubstitutionSubstFormat1;
+import org.apache.fontbox.ttf.table.gsub.LookupTypeMultipleSubstitutionFormat1;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeSingleSubstFormat1;
 import org.apache.fontbox.ttf.table.gsub.LookupTypeSingleSubstFormat2;
+import org.apache.fontbox.ttf.table.gsub.SequenceTable;
 
 /**
- * This class has utility methods to extract meaningful data from the highly obfuscated GSUB Tables. This data is then
- * used to determine which combination of Glyphs or words have to be replaced.
- * 
+ * This class has utility methods to extract meaningful GsubData from the highly obfuscated GSUB
+ * Tables. This GsubData is then used to determine which combination of glyphs or words have to be
+ * replaced.
+ *
  * @author Palash Ray
  * 
  */
@@ -72,6 +75,10 @@ public class GlyphSubstitutionDataExtrac
      * {@link Language}'s searching for the first match with the scripts of the font. Instead, it unconditionally
      * creates {@link ScriptTableDetails} instance with language left {@linkplain Language#UNSPECIFIED unspecified}.
      * 
+     * @param scriptName
+     * @param scriptTable
+     * @param featureListTable
+     * @param lookupListTable
      * @return {@link GsubData} instance built especially for the given {@code scriptName}
      */
     public GsubData getGsubData(String scriptName, ScriptTable scriptTable,
@@ -176,6 +183,11 @@ public class GlyphSubstitutionDataExtrac
                 extractDataFromSingleSubstTableFormat2Table(glyphSubstitutionMap,
                         (LookupTypeSingleSubstFormat2) lookupSubTable);
             }
+            else if (lookupSubTable instanceof LookupTypeMultipleSubstitutionFormat1)
+            {
+                extractDataFromMultipleSubstitutionFormat1Table(glyphSubstitutionMap,
+                        (LookupTypeMultipleSubstitutionFormat1) lookupSubTable);
+            }
             else
             {
                 // usually null, due to being skipped in GlyphSubstitutionTable.readLookupTable()
@@ -222,6 +234,29 @@ public class GlyphSubstitutionDataExtrac
         }
     }
 
+    private void extractDataFromMultipleSubstitutionFormat1Table(
+            Map<List<Integer>, Integer> glyphSubstitutionMap,
+            LookupTypeMultipleSubstitutionFormat1 multipleSubstFormat1Subtable)
+    {
+        CoverageTable coverageTable = multipleSubstFormat1Subtable.getCoverageTable();
+
+        if (coverageTable.getSize() != multipleSubstFormat1Subtable.getSequenceTables().length)
+        {
+            throw new IllegalArgumentException(
+                    "The no. coverage table entries should be the same as the size of the sequencce tables");
+        }
+
+        for (int i = 0; i < coverageTable.getSize(); i++)
+        {
+            int coverageGlyphId = coverageTable.getGlyphId(i);
+            SequenceTable sequenceTable = multipleSubstFormat1Subtable.getSequenceTables()[i];
+            
+            //TODO implement storing this data
+            // (not possible at this time because the map value isn't a list)
+        }
+        
+    }
+
     private void extractDataFromLigatureSubstitutionSubstFormat1Table(
             Map<List<Integer>, Integer> glyphSubstitutionMap,
             LookupTypeLigatureSubstitutionSubstFormat1 ligatureSubstitutionTable)