You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by sp...@apache.org on 2010/09/30 11:47:17 UTC

svn commit: r1002978 [8/9] - in /xmlgraphics/fop/branches/Temp_ComplexScripts: ./ src/java/org/apache/fop/area/ src/java/org/apache/fop/area/inline/ src/java/org/apache/fop/fo/ src/java/org/apache/fop/fonts/ src/java/org/apache/fop/fonts/apps/ src/java...

Modified: xmlgraphics/fop/branches/Temp_ComplexScripts/src/java/org/apache/fop/fonts/truetype/TTFFile.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ComplexScripts/src/java/org/apache/fop/fonts/truetype/TTFFile.java?rev=1002978&r1=1002977&r2=1002978&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ComplexScripts/src/java/org/apache/fop/fonts/truetype/TTFFile.java (original)
+++ xmlgraphics/fop/branches/Temp_ComplexScripts/src/java/org/apache/fop/fonts/truetype/TTFFile.java Thu Sep 30 09:47:15 2010
@@ -20,6 +20,7 @@
 package org.apache.fop.fonts.truetype;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Iterator;
 import java.util.List;
@@ -33,7 +34,11 @@ import org.apache.xmlgraphics.fonts.Glyp
 
 
 import org.apache.fop.fonts.FontUtil;
+import org.apache.fop.fonts.GlyphClassTable;
 import org.apache.fop.fonts.GlyphCoverageTable;
+import org.apache.fop.fonts.GlyphDefinitionSubtable;
+import org.apache.fop.fonts.GlyphDefinitionTable;
+import org.apache.fop.fonts.GlyphMappingTable;
 import org.apache.fop.fonts.GlyphPositioningSubtable;
 import org.apache.fop.fonts.GlyphPositioningTable;
 import org.apache.fop.fonts.GlyphSubstitutionSubtable;
@@ -41,6 +46,8 @@ import org.apache.fop.fonts.GlyphSubstit
 import org.apache.fop.fonts.GlyphSubtable;
 import org.apache.fop.fonts.GlyphTable;
 
+import org.apache.fop.util.CharUtilities;
+
 // CSOFF: AvoidNestedBlocksCheck
 // CSOFF: NoWhitespaceAfterCheck
 // CSOFF: InnerAssignmentCheck
@@ -59,9 +66,6 @@ public class TTFFile {
     static final int MAX_CHAR_CODE = 255;
     static final int ENC_BUF_SIZE = 1024;
 
-    /** Set to true to get even more debug output than with level DEBUG */
-    public static final boolean TRACE_ENABLED = false;
-
     private String encoding = "WinAnsiEncoding";    // Default encoding
 
     private short firstChar = 0;
@@ -74,7 +78,7 @@ public class TTFFile {
     private Map kerningTab;                          // for CIDs
     private Map ansiKerningTab;                      // For winAnsiEncoding
     private List cmaps;
-    private List unicodeMapping;
+    private Set unicodeMappings;
 
     private int upem;                                // unitsPerEm from "head" table
     private int nhmtx;                               // Number of horizontal metrics
@@ -140,9 +144,10 @@ public class TTFFile {
     private Map/*<String,Object[3]>*/ seScripts;
     private Map/*<String,Object[2]>*/ seLanguages;
     private Map/*<String,List<String>>*/ seFeatures;
-    private List seCoverage;
+    private GlyphMappingTable seMapping;
     private List seEntries;
     private List seSubtables;
+    private GlyphDefinitionTable gdef;
     private GlyphSubstitutionTable gsub;
     private GlyphPositioningTable gpos;
 
@@ -154,7 +159,7 @@ public class TTFFile {
     /**
      * Key-value helper class
      */
-    class UnicodeMapping {
+    class UnicodeMapping implements Comparable {
 
         private int unicodeIndex;
         private int glyphIndex;
@@ -181,6 +186,46 @@ public class TTFFile {
         public int getUnicodeIndex() {
             return unicodeIndex;
         }
+
+
+        /** {@inheritDoc} */
+        public int hashCode() {
+            int hc = unicodeIndex;
+            hc = 19 * hc + ( hc ^ glyphIndex );
+            return hc;
+        }
+
+        /** {@inheritDoc} */
+        public boolean equals ( Object o ) {
+            if ( o instanceof UnicodeMapping ) {
+                UnicodeMapping m = (UnicodeMapping) o;
+                if ( unicodeIndex != m.unicodeIndex ) {
+                    return false;
+                } else if ( glyphIndex != m.glyphIndex ) {
+                    return false;
+                } else {
+                    return true;
+                }
+            } else {
+                return false;
+            }
+        }
+
+        /** {@inheritDoc} */
+        public int compareTo ( Object o ) {
+            if ( o instanceof UnicodeMapping ) {
+                UnicodeMapping m = (UnicodeMapping) o;
+                if ( unicodeIndex > m.unicodeIndex ) {
+                    return 1;
+                } else if ( unicodeIndex < m.unicodeIndex ) {
+                    return -1;
+                } else {
+                    return 0;
+                }
+            } else {
+                return -1;
+            }
+        }
     }
 
     /**
@@ -229,7 +274,7 @@ public class TTFFile {
      */
     private boolean readCMAP(FontFileReader in) throws IOException {
 
-        unicodeMapping = new java.util.ArrayList();
+        unicodeMappings = new java.util.TreeSet();
 
         seekTab(in, "cmap", 2);
         int numCMap = in.readTTFUShort();    // Number of cmap subtables
@@ -370,7 +415,7 @@ public class TTFFile {
                             glyphIdx = (in.readTTFUShort() + cmapDeltas[i])
                                        & 0xffff;
 
-                            unicodeMapping.add(new UnicodeMapping(glyphIdx, j));
+                            unicodeMappings.add(new UnicodeMapping(glyphIdx, j));
                             mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(j));
 
                             if (encodingID == 0 && j >= 0xF020 && j <= 0xF0FF) {
@@ -380,7 +425,7 @@ public class TTFFile {
                                 int mapped = j - 0xF000;
                                 if (!eightBitGlyphs.get(mapped)) {
                                     //Only map if Unicode code point hasn't been mapped before
-                                    unicodeMapping.add(new UnicodeMapping(glyphIdx, mapped));
+                                    unicodeMappings.add(new UnicodeMapping(glyphIdx, mapped));
                                     mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(mapped));
                                 }
                             }
@@ -421,7 +466,7 @@ public class TTFFile {
                                                    + mtxTab.length);
                             }
 
-                            unicodeMapping.add(new UnicodeMapping(glyphIdx, j));
+                            unicodeMappings.add(new UnicodeMapping(glyphIdx, j));
                             if (glyphIdx < mtxTab.length) {
                                 mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(j));
                             } else {
@@ -579,21 +624,83 @@ public class TTFFile {
             return false;
         }
         // Create cmaps for bfentries
+        augmentCMaps();
         createCMaps();
-        // print_max_min();
 
         readKerning(in);
+        readGDEF(in);
         readGSUB(in);
         readGPOS(in);
         guessVerticalMetricsFromGlyphBBox();
         return true;
     }
 
+    /**
+     * Augment the previously ingested CMAP data with new entries to ensure
+     * that every glyph index has a corresponding Unicode value. This is required
+     * by GSUB/GPOS processing which can emit glyph indices that are not in the
+     * normal CMAP (and, for which, on other platforms, the glyph indices are used
+     * directly for rendering purposes (rather than character codes). However, in
+     * the case of FOP IF representation, character codes are used, and, consequently
+     * every glyph needs some character value. Here, we assign them to the Unicode
+     * private use range, starting at 0xE000 up to 0xF8FF. If there are existing
+     * assignments in this range, we just skip over them. Note that it is possible
+     * to exhaust this range of 6400 code values in the case a font has an
+     * extraordinary number of unmapped glyphs. In that case, we do not make
+     * any further assignments, but print a warning message.
+     */
+    private void augmentCMaps() {
+        int numMapped = 0;
+        int numUnmapped = 0;
+        int nextPrivateUse = 0xE000;
+        int firstPrivate = 0;
+        int lastPrivate = 0;
+        int firstUnmapped = 0;
+        int lastUnmapped = 0;
+        for ( int i = 0, n = numberOfGlyphs; i < n; i++ ) {
+            Integer uc = glyphToUnicode ( i );
+            if ( uc == null ) {
+                while ( ( nextPrivateUse < 0xF900 ) && ( unicodeToGlyphMap.get(new Integer(nextPrivateUse)) != null ) ) {
+                    nextPrivateUse++;
+                }
+                if ( nextPrivateUse < 0xF900 ) {
+                    int pu = nextPrivateUse;
+                    unicodeMappings.add ( new UnicodeMapping ( i, pu ) );
+                    if ( firstPrivate == 0 ) {
+                        firstPrivate = pu;
+                    }
+                    lastPrivate = pu;
+                    numMapped++;
+                } else {
+                    if ( firstUnmapped == 0 ) {
+                        firstUnmapped = i;
+                    }
+                    lastUnmapped = i;
+                    numUnmapped++;
+                }
+            }
+        }
+        if ( numMapped > 0 ) {
+            if (log.isDebugEnabled()) {
+                log.debug ( "augment CMAP for "
+                            + numMapped
+                            + " glyphs, mapped to private use characters in the range ["
+                            + CharUtilities.format ( firstPrivate ) + ","
+                            + CharUtilities.format ( lastPrivate ) + "] (inclusive)" );
+            }
+        }
+        if ( numUnmapped > 0 ) {
+            log.warn ( "Exhausted private use area: unable to map "
+                       + numUnmapped + " glyphs in glyph index range ["
+                       + firstUnmapped + "," + lastUnmapped + "] (inclusive) of font '" + getFullName() + "'" );
+        }
+    }
+
     private void createCMaps() {
         cmaps = new java.util.ArrayList();
         TTFCmapEntry tce = new TTFCmapEntry();
 
-        Iterator e = unicodeMapping.listIterator();
+        Iterator e = unicodeMappings.iterator();
         UnicodeMapping um = (UnicodeMapping)e.next();
         UnicodeMapping lastMapping = um;
 
@@ -955,8 +1062,8 @@ public class TTFFile {
         int mtxSize = Math.max(numberOfGlyphs, nhmtx);
         mtxTab = new TTFMtxEntry[mtxSize];
 
-        if (TRACE_ENABLED) {
-            log.debug("*** Widths array: \n");
+        if (log.isTraceEnabled()) {
+            log.trace("*** Widths array: \n");
         }
         for (int i = 0; i < mtxSize; i++) {
             mtxTab[i] = new TTFMtxEntry();
@@ -965,11 +1072,9 @@ public class TTFFile {
             mtxTab[i].setWx(in.readTTFUShort());
             mtxTab[i].setLsb(in.readTTFUShort());
 
-            if (TRACE_ENABLED) {
-                if (log.isDebugEnabled()) {
-                    log.debug("   width[" + i + "] = "
-                        + convertTTFUnit2PDFUnit(mtxTab[i].getWx()) + ";");
-                }
+            if (log.isTraceEnabled()) {
+                log.trace("   width[" + i + "] = "
+                          + convertTTFUnit2PDFUnit(mtxTab[i].getWx()) + ";");
             }
         }
 
@@ -1517,6 +1622,7 @@ public class TTFFile {
         }
     }
 
+    /** helper method for formatting an integer array for output */
     private String toString ( int[] ia ) {
         StringBuffer sb = new StringBuffer();
         if ( ( ia == null ) || ( ia.length == 0 ) ) {
@@ -1727,6 +1833,14 @@ public class TTFFile {
     }
 
     /**
+     * Returns the GDEF table or null if none present.
+     * @return the GDEF table
+     */
+    public GlyphDefinitionTable getGDEF() {
+        return gdef;
+    }
+
+    /**
      * Returns the GSUB table or null if none present.
      * @return the GSUB table
      */
@@ -1742,13 +1856,64 @@ public class TTFFile {
         return gpos;
     }
 
+    static final class GDEFLookupType {
+        static final int GLYPH_CLASS                    = 1;
+        static final int ATTACHMENT_POINT               = 2;
+        static final int LIGATURE_CARET                 = 3;
+        static final int MARK_ATTACHMENT                = 4;
+        private GDEFLookupType() {
+        }
+        public static int getSubtableType ( int lt ) {
+            int st;
+            switch ( lt ) {
+            case GDEFLookupType.GLYPH_CLASS:
+                st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_GLYPH_CLASS;
+                break;
+            case GDEFLookupType.ATTACHMENT_POINT:
+                st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
+                break;
+            case GDEFLookupType.LIGATURE_CARET:
+                st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_LIGATURE_CARET;
+                break;
+            case GDEFLookupType.MARK_ATTACHMENT:
+                st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
+                break;
+            default:
+                st = -1;
+                break;
+            }
+            return st;
+        }
+        public static String toString(int type) {
+            String s;
+            switch ( type ) {
+            case GLYPH_CLASS:
+                s = "GlyphClass";
+                break;
+            case ATTACHMENT_POINT:
+                s = "AttachmentPoint";
+                break;
+            case LIGATURE_CARET:
+                s = "LigatureCaret";
+                break;
+            case MARK_ATTACHMENT:
+                s = "MarkAttachment";
+                break;
+            default:
+                s = "?";
+                break;
+            }
+            return s;
+        }
+    }
+
     static final class GSUBLookupType {
         static final int SINGLE                         = 1;
         static final int MULTIPLE                       = 2;
         static final int ALTERNATE                      = 3;
         static final int LIGATURE                       = 4;
-        static final int CONTEXT                        = 5;
-        static final int CHAINED_CONTEXT                = 6;
+        static final int CONTEXTUAL                     = 5;
+        static final int CHAINED_CONTEXTUAL             = 6;
         static final int EXTENSION                      = 7;
         static final int REVERSE_CHAINED_SINGLE         = 8;
         private GSUBLookupType() {
@@ -1768,17 +1933,17 @@ public class TTFFile {
             case GSUBLookupType.LIGATURE:
                 st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_LIGATURE;
                 break;
-            case GSUBLookupType.CONTEXT:
-                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CONTEXT;
+            case GSUBLookupType.CONTEXTUAL:
+                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CONTEXTUAL;
                 break;
-            case GSUBLookupType.CHAINED_CONTEXT:
-                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CHAINING_CONTEXT;
+            case GSUBLookupType.CHAINED_CONTEXTUAL:
+                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
                 break;
             case GSUBLookupType.EXTENSION:
                 st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION;
                 break;
             case GSUBLookupType.REVERSE_CHAINED_SINGLE:
-                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_REVERSE_CHAINING_CONTEXT_SINGLE;
+                st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
                 break;
             default:
                 st = -1;
@@ -1801,11 +1966,11 @@ public class TTFFile {
             case LIGATURE:
                 s = "Ligature";
                 break;
-            case CONTEXT:
-                s = "Context";
+            case CONTEXTUAL:
+                s = "Contextual";
                 break;
-            case CHAINED_CONTEXT:
-                s = "ChainedContext";
+            case CHAINED_CONTEXTUAL:
+                s = "ChainedContextual";
                 break;
             case EXTENSION:
                 s = "Extension";
@@ -1828,8 +1993,8 @@ public class TTFFile {
         static final int MARK_TO_BASE                   = 4;
         static final int MARK_TO_LIGATURE               = 5;
         static final int MARK_TO_MARK                   = 6;
-        static final int CONTEXT                        = 7;
-        static final int CHAINED_CONTEXT                = 8;
+        static final int CONTEXTUAL                     = 7;
+        static final int CHAINED_CONTEXTUAL             = 8;
         static final int EXTENSION                      = 9;
         private GPOSLookupType() {
         }
@@ -1854,11 +2019,11 @@ public class TTFFile {
             case MARK_TO_MARK:
                 s = "MarkToMark";
                 break;
-            case CONTEXT:
-                s = "Context";
+            case CONTEXTUAL:
+                s = "Contextual";
                 break;
-            case CHAINED_CONTEXT:
-                s = "ChainedContext";
+            case CHAINED_CONTEXTUAL:
+                s = "ChainedContextual";
                 break;
             case EXTENSION:
                 s = "Extension";
@@ -1930,7 +2095,8 @@ public class TTFFile {
         }
     }
 
-    private void readCoverageTableFormat1(FontFileReader in, String label, long tableOffset, int coverageFormat) throws IOException {
+    private GlyphCoverageTable readCoverageTableFormat1(FontFileReader in, String label, long tableOffset, int coverageFormat) throws IOException {
+        List entries = new java.util.ArrayList();
         in.seekSet(tableOffset);
         // skip over format (already known)
         in.skip ( 2 );
@@ -1940,80 +2106,118 @@ public class TTFFile {
         for ( int i = 0, n = ng; i < n; i++ ) {
             int g = in.readTTFUShort();
             ga[i] = g;
-            seCoverage.add ( Integer.valueOf(g) );
+            entries.add ( Integer.valueOf(g) );
         }
         // dump info if debugging
         if (log.isDebugEnabled()) {
             log.debug(label + " glyphs: " + toString(ga) );
         }
+        return GlyphCoverageTable.createCoverageTable ( entries );
     }
 
-    private void readCoverageTableFormat2(FontFileReader in, String label, long tableOffset, int coverageFormat) throws IOException {
+    private GlyphCoverageTable readCoverageTableFormat2(FontFileReader in, String label, long tableOffset, int coverageFormat) throws IOException {
+        List entries = new java.util.ArrayList();
         in.seekSet(tableOffset);
         // skip over format (already known)
         in.skip ( 2 );
         // read range record count
         int nr = in.readTTFUShort();
-        int[] rsa = new int[nr];
-        int[] rea = new int[nr];
-        int[] rxa = new int[nr];
         for ( int i = 0, n = nr; i < n; i++ ) {
             // read range start
             int s = in.readTTFUShort();
             // read range end
             int e = in.readTTFUShort();
-            // read range coverage index
-            int x = in.readTTFUShort();
+            // read range coverage (mapping) index
+            int m = in.readTTFUShort();
             // dump info if debugging
             if (log.isDebugEnabled()) {
-                log.debug(label + " range[" + i + "]: [" + s + "," + e + "]: " + x );
+                log.debug(label + " range[" + i + "]: [" + s + "," + e + "]: " + m );
             }
-            rsa[i] = s;
-            rea[i] = e;
-            rxa[i] = x;
-            seCoverage.add ( new GlyphCoverageTable.CoverageRange ( s, e, x ) );
+            entries.add ( new GlyphCoverageTable.MappingRange ( s, e, m ) );
         }
+        return GlyphCoverageTable.createCoverageTable ( entries );
     }
 
-    private void readCoverageTable(FontFileReader in, String label, long tableOffset) throws IOException {
+    private GlyphCoverageTable readCoverageTable(FontFileReader in, String label, long tableOffset) throws IOException {
+        GlyphCoverageTable gct;
         long cp = in.getCurrentPos();
         in.seekSet(tableOffset);
         // read coverage table format
         int cf = in.readTTFUShort();
         if ( cf == 1 ) {
-            readCoverageTableFormat1 ( in, label, tableOffset, cf );
+            gct = readCoverageTableFormat1 ( in, label, tableOffset, cf );
         } else if ( cf == 2 ) {
-            readCoverageTableFormat2 ( in, label, tableOffset, cf );
+            gct = readCoverageTableFormat2 ( in, label, tableOffset, cf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported coverage table format: " + cf );
         }
         in.seekSet ( cp );
+        return gct;
     }
 
-    /* not used yet
-    private void readClassDefTableFormat1(FontFileReader in, long tableOffset, int classFormat) throws IOException {
+    private GlyphClassTable readClassDefTableFormat1(FontFileReader in, String label, long tableOffset, int classFormat) throws IOException {
+        List entries = new java.util.ArrayList();
         in.seekSet(tableOffset);
         // skip over format (already known)
         in.skip ( 2 );
+        // read start glyph
+        int sg = in.readTTFUShort();
+        entries.add ( Integer.valueOf(sg) );
+        // read glyph count
+        int ng = in.readTTFUShort();
+        // read glyph classes
+        int[] ca = new int[ng];
+        for ( int i = 0, n = ng; i < n; i++ ) {
+            int gc = in.readTTFUShort();
+            ca[i] = gc;
+            entries.add ( Integer.valueOf(gc) );
+        }
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(label + " glyph classes: " + toString(ca) );
+        }
+        return GlyphClassTable.createClassTable ( entries );
     }
 
-    private void readClassDefTableFormat2(FontFileReader in, long tableOffset, int classFormat) throws IOException {
+    private GlyphClassTable readClassDefTableFormat2(FontFileReader in, String label, long tableOffset, int classFormat) throws IOException {
+        List entries = new java.util.ArrayList();
         in.seekSet(tableOffset);
         // skip over format (already known)
         in.skip ( 2 );
+        // read range record count
+        int nr = in.readTTFUShort();
+        for ( int i = 0, n = nr; i < n; i++ ) {
+            // read range start
+            int s = in.readTTFUShort();
+            // read range end
+            int e = in.readTTFUShort();
+            // read range glyph class (mapping) index
+            int m = in.readTTFUShort();
+            // dump info if debugging
+            if (log.isDebugEnabled()) {
+                log.debug(label + " range[" + i + "]: [" + s + "," + e + "]: " + m );
+            }
+            entries.add ( new GlyphClassTable.MappingRange ( s, e, m ) );
+        }
+        return GlyphClassTable.createClassTable ( entries );
     }
 
-    private void readClassDefTable(FontFileReader in, long tableOffset) throws IOException {
+    private GlyphClassTable readClassDefTable(FontFileReader in, String label, long tableOffset) throws IOException {
+        GlyphClassTable gct;
         long cp = in.getCurrentPos();
         in.seekSet(tableOffset);
         // read class table format
         int cf = in.readTTFUShort();
         if ( cf == 1 ) {
-            readClassDefTableFormat1 ( in, tableOffset, cf );
+            gct = readClassDefTableFormat1 ( in, label, tableOffset, cf );
         } else if ( cf == 2 ) {
-            readClassDefTableFormat2 ( in, tableOffset, cf );
+            gct = readClassDefTableFormat2 ( in, label, tableOffset, cf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported class definition table format: " + cf );
         }
         in.seekSet ( cp );
+        return gct;
     }
-    */
 
     private void readSingleSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
         String tableTag = "GSUB";
@@ -2026,12 +2230,12 @@ public class TTFFile {
         int dg = in.readTTFShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " single substitution format: " + subtableFormat + " (delta)" );
+            log.debug(tableTag + " single substitution subtable format: " + subtableFormat + " (delta)" );
             log.debug(tableTag + " single substitution coverage table offset: " + co );
             log.debug(tableTag + " single substitution delta: " + dg );
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " single substitution coverage", subtableOffset + co );
+        seMapping = readCoverageTable ( in, tableTag + " single substitution coverage", subtableOffset + co );
         seEntries.add ( Integer.valueOf ( dg ) );
     }
 
@@ -2046,12 +2250,12 @@ public class TTFFile {
         int ng = in.readTTFUShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " single substitution format: " + subtableFormat + " (mapped)" );
+            log.debug(tableTag + " single substitution subtable format: " + subtableFormat + " (mapped)" );
             log.debug(tableTag + " single substitution coverage table offset: " + co );
             log.debug(tableTag + " single substitution glyph count: " + ng );
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " single substitution coverage", subtableOffset + co );
+        seMapping = readCoverageTable ( in, tableTag + " single substitution coverage", subtableOffset + co );
         // read glyph substitutions
         int[] gsa = new int[ng];
         for ( int i = 0, n = ng; i < n; i++ ) {
@@ -2066,12 +2270,14 @@ public class TTFFile {
 
     private int readSingleSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
         if ( sf == 1 ) {
             readSingleSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
         } else if ( sf == 2 ) {
             readSingleSubTableFormat2 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported single substitution subtable format: " + sf );
         }
         return sf;
     }
@@ -2087,40 +2293,49 @@ public class TTFFile {
         int ns = in.readTTFUShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " multiple substitution format: " + subtableFormat + " (mapped)" );
+            log.debug(tableTag + " multiple substitution subtable format: " + subtableFormat + " (mapped)" );
             log.debug(tableTag + " multiple substitution coverage table offset: " + co );
             log.debug(tableTag + " multiple substitution sequence count: " + ns );
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " multiple substitution coverage", subtableOffset + co );
+        seMapping = readCoverageTable ( in, tableTag + " multiple substitution coverage", subtableOffset + co );
         // read sequence table offsets
         int[] soa = new int[ns];
         for ( int i = 0, n = ns; i < n; i++ ) {
             soa[i] = in.readTTFUShort();
         }
         // read sequence tables
+        int[][] gsa = new int [ ns ] [];
         for ( int i = 0, n = ns; i < n; i++ ) {
             int so = soa[i];
-            in.seekSet(subtableOffset + so);
-            // read glyph count
-            int ng = in.readTTFUShort();
-            int[] ga = new int[ng];
-            for ( int j = 0; j < ng; j++ ) {
-                int gs = in.readTTFUShort();
-                ga[j] = gs;
+            int[] ga;
+            if ( so > 0 ) {
+                in.seekSet(subtableOffset + so);
+                // read glyph count
+                int ng = in.readTTFUShort();
+                ga = new int[ng];
+                for ( int j = 0; j < ng; j++ ) {
+                    ga[j] = in.readTTFUShort();
+                }
+            } else {
+                ga = null;
             }
             if (log.isDebugEnabled()) {
                 log.debug(tableTag + " multiple substitution sequence[" + i + "]: " + toString ( ga ) );
             }
+            gsa [ i ] = ga;
         }
+        seEntries.add ( gsa );
     }
 
     private int readMultipleSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
         if ( sf == 1 ) {
             readMultipleSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported multiple substitution subtable format: " + sf );
         }
         return sf;
     }
@@ -2136,12 +2351,12 @@ public class TTFFile {
         int ns = in.readTTFUShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " alternate substitution format: " + subtableFormat + " (mapped)" );
+            log.debug(tableTag + " alternate substitution subtable format: " + subtableFormat + " (mapped)" );
             log.debug(tableTag + " alternate substitution coverage table offset: " + co );
             log.debug(tableTag + " alternate substitution alternate set count: " + ns );
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " alternate substitution coverage", subtableOffset + co );
+        seMapping = readCoverageTable ( in, tableTag + " alternate substitution coverage", subtableOffset + co );
         // read alternate set table offsets
         int[] soa = new int[ns];
         for ( int i = 0, n = ns; i < n; i++ ) {
@@ -2161,15 +2376,18 @@ public class TTFFile {
             if (log.isDebugEnabled()) {
                 log.debug(tableTag + " alternate substitution alternate set[" + i + "]: " + toString ( ga ) );
             }
+            seEntries.add ( ga );
         }
     }
 
     private int readAlternateSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
         if ( sf == 1 ) {
             readAlternateSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported alternate substitution subtable format: " + sf );
         }
         return sf;
     }
@@ -2185,12 +2403,12 @@ public class TTFFile {
         int ns = in.readTTFUShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " ligature substitution format: " + subtableFormat + " (mapped)" );
+            log.debug(tableTag + " ligature substitution subtable format: " + subtableFormat + " (mapped)" );
             log.debug(tableTag + " ligature substitution coverage table offset: " + co );
             log.debug(tableTag + " ligature substitution ligature set count: " + ns );
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " ligature substitution coverage", subtableOffset + co );
+        seMapping = readCoverageTable ( in, tableTag + " ligature substitution coverage", subtableOffset + co );
         // read ligature set table offsets
         int[] soa = new int[ns];
         for ( int i = 0, n = ns; i < n; i++ ) {
@@ -2230,322 +2448,766 @@ public class TTFFile {
 
     private int readLigatureSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
         if ( sf == 1 ) {
             readLigatureSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported ligature substitution subtable format: " + sf );
         }
         return sf;
     }
 
-    private int readContextSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
+    private GlyphTable.RuleLookup[] readRuleLookups(FontFileReader in, int numLookups, String header) throws IOException {
+        GlyphTable.RuleLookup[] la = new GlyphTable.RuleLookup [ numLookups ];
+        for ( int i = 0, n = numLookups; i < n; i++ ) {
+            int sequenceIndex = in.readTTFUShort();
+            int lookupIndex = in.readTTFUShort();
+            la [ i ] = new GlyphTable.RuleLookup ( sequenceIndex, lookupIndex );
+            // dump info if debugging and header is non-null
+            if ( log.isDebugEnabled() && ( header != null ) ) {
+                log.debug(header + "lookup[" + i + "]: " + la[i]);
+            }
+        }
+        return la;
+    }
+
+    private void readContextualSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GSUB";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read coverage offset
+        int co = in.readTTFUShort();
+        // read rule set count
+        int nrs = in.readTTFUShort();
+        // read rule set offsets
+        int[] rsoa = new int [ nrs ];
+        for ( int i = 0; i < nrs; i++ ) {
+            rsoa [ i ] = in.readTTFUShort();
+        }
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyphs)" );
+            log.debug(tableTag + " contextual substitution coverage table offset: " + co );
+            log.debug(tableTag + " contextual substitution rule set count: " + nrs );
+            for ( int i = 0; i < nrs; i++ ) {
+                log.debug(tableTag + " contextual substitution rule set offset[" + i + "]: " + rsoa[i] );
+            }
+        }
+        // read coverage table
+        GlyphCoverageTable ct;
+        if ( co > 0 ) {
+            ct = readCoverageTable ( in, tableTag + " contextual substitution coverage", subtableOffset + co );
+        } else {
+            ct = null;
+        }
+        // read rule sets
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
+        String header = null;
+        for ( int i = 0; i < nrs; i++ ) {
+            GlyphTable.RuleSet rs;
+            int rso = rsoa [ i ];
+            if ( rso > 0 ) {
+                // seek to rule set [ i ]
+                in.seekSet ( subtableOffset + rso );
+                // read rule count
+                int nr = in.readTTFUShort();
+                // read rule offsets
+                int[] roa = new int [ nr ];
+                GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
+                for ( int j = 0; j < nr; j++ ) {
+                    roa [ j ] = in.readTTFUShort();
+                }
+                // read glyph sequence rules
+                for ( int j = 0; j < nr; j++ ) {
+                    GlyphTable.GlyphSequenceRule r;
+                    int ro = roa [ j ];
+                    if ( ro > 0 ) {
+                        // seek to rule [ j ]
+                        in.seekSet ( subtableOffset + rso + ro );
+                        // read glyph count
+                        int ng = in.readTTFUShort();
+                        // read rule lookup count
+                        int nl = in.readTTFUShort();
+                        // read glyphs
+                        int[] glyphs = new int [ ng - 1 ];
+                        for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
+                            glyphs [ k ] = in.readTTFUShort();
+                        }
+                        // read rule lookups
+                        if (log.isDebugEnabled()) {
+                            header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
+                        }
+                        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+                        r = new GlyphTable.GlyphSequenceRule ( lookups, ng, glyphs );
+                    } else {
+                        r = null;
+                    }
+                    ra [ j ] = r;
+                }
+                rs = new GlyphTable.HomogeneousRuleSet ( ra );
+            } else {
+                rs = null;
+            }
+            rsa [ i ] = rs;
+        }
+        // store results
+        seMapping = ct;
+        seEntries.add ( rsa );
+    }
+
+    private void readContextualSubTableFormat2(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GSUB";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read coverage offset
+        int co = in.readTTFUShort();
+        // read class def table offset
+        int cdo = in.readTTFUShort();
+        // read class rule set count
+        int ngc = in.readTTFUShort();
+        // read class rule set offsets
+        int[] csoa = new int [ ngc ];
+        for ( int i = 0; i < ngc; i++ ) {
+            csoa [ i ] = in.readTTFUShort();
+        }
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyph classes)" );
+            log.debug(tableTag + " contextual substitution coverage table offset: " + co );
+            log.debug(tableTag + " contextual substitution class set count: " + ngc );
+            for ( int i = 0; i < ngc; i++ ) {
+                log.debug(tableTag + " contextual substitution class set offset[" + i + "]: " + csoa[i] );
+            }
+        }
+        // read coverage table
+        GlyphCoverageTable ct;
+        if ( co > 0 ) {
+            ct = readCoverageTable ( in, tableTag + " contextual substitution coverage", subtableOffset + co );
+        } else {
+            ct = null;
+        }
+        // read class definition table
+        GlyphClassTable cdt;
+        if ( cdo > 0 ) {
+            cdt = readClassDefTable ( in, tableTag + " contextual substitution class definition", subtableOffset + cdo );
+        } else {
+            cdt = null;
+        }
+        // read rule sets
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
+        String header = null;
+        for ( int i = 0; i < ngc; i++ ) {
+            int cso = csoa [ i ];
+            GlyphTable.RuleSet rs;
+            if ( cso > 0 ) {
+                // seek to rule set [ i ]
+                in.seekSet ( subtableOffset + cso );
+                // read rule count
+                int nr = in.readTTFUShort();
+                // read rule offsets
+                int[] roa = new int [ nr ];
+                GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
+                for ( int j = 0; j < nr; j++ ) {
+                    roa [ j ] = in.readTTFUShort();
+                }
+                // read glyph sequence rules
+                for ( int j = 0; j < nr; j++ ) {
+                    int ro = roa [ j ];
+                    GlyphTable.ClassSequenceRule r;
+                    if ( ro > 0 ) {
+                        // seek to rule [ j ]
+                        in.seekSet ( subtableOffset + cso + ro );
+                        // read glyph count
+                        int ng = in.readTTFUShort();
+                        // read rule lookup count
+                        int nl = in.readTTFUShort();
+                        // read classes
+                        int[] classes = new int [ ng - 1 ];
+                        for ( int k = 0, nk = classes.length; k < nk; k++ ) {
+                            classes [ k ] = in.readTTFUShort();
+                        }
+                        // read rule lookups
+                        if (log.isDebugEnabled()) {
+                            header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
+                        }
+                        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+                        r = new GlyphTable.ClassSequenceRule ( lookups, ng, classes );
+                    } else {
+                        assert ro > 0 : "unexpected null subclass rule offset";
+                        r = null;
+                    }
+                    ra [ j ] = r;
+                }
+                rs = new GlyphTable.HomogeneousRuleSet ( ra );
+            } else {
+                rs = null;
+            }
+            rsa [ i ] = rs;
+        }
+        // store results
+        seMapping = ct;
+        seEntries.add ( cdt );
+        seEntries.add ( Integer.valueOf ( ngc ) );
+        seEntries.add ( rsa );
+    }
+
+    private void readContextualSubTableFormat3(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GSUB";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read glyph (input sequence length) count
+        int ng = in.readTTFUShort();
+        // read substitution lookup count
+        int nl = in.readTTFUShort();
+        // read glyph coverage offsets, one per glyph input sequence length count
+        int[] gcoa = new int [ ng ];
+        for ( int i = 0; i < ng; i++ ) {
+            gcoa [ i ] = in.readTTFUShort();
+        }
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyph sets)" );
+            log.debug(tableTag + " contextual substitution glyph input sequence length count: " + ng );
+            log.debug(tableTag + " contextual substitution lookup count: " + nl );
+            for ( int i = 0; i < ng; i++ ) {
+                log.debug(tableTag + " contextual substitution coverage table offset[" + i + "]: " + gcoa[i] );
+            }
+        }
+        // read coverage tables
+        GlyphCoverageTable[] gca = new GlyphCoverageTable [ ng ];
+        for ( int i = 0; i < ng; i++ ) {
+            int gco = gcoa [ i ];
+            GlyphCoverageTable gct;
+            if ( gco > 0 ) {
+                gct = readCoverageTable ( in, tableTag + " contextual substitution coverage[" + i + "]", subtableOffset + gco );
+            } else {
+                gct = null;
+            }
+            gca [ i ] = gct;
+        }
+        // read rule lookups
+        String header = null;
+        if (log.isDebugEnabled()) {
+            header = tableTag + " contextual substitution lookups: ";
+        }
+        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+        // construct rule, rule set, and rule set array
+        GlyphTable.Rule r = new GlyphTable.CoverageSequenceRule ( lookups, ng, gca );
+        GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
+        // store results
+        assert ( gca != null ) && ( gca.length > 0 );
+        seMapping = gca[0];
+        seEntries.add ( rsa );
+    }
+
+    private int readContextualSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
-        // [TBD] - implement me
+        if ( sf == 1 ) {
+            readContextualSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else if ( sf == 2 ) {
+            readContextualSubTableFormat2 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else if ( sf == 3 ) {
+            readContextualSubTableFormat3 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported contextual substitution subtable format: " + sf );
+        }
         return sf;
     }
 
-    private void readChainedContextSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+    private void readChainedContextualSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
         String tableTag = "GSUB";
         in.seekSet(subtableOffset);
         // skip over format (already known)
         in.skip ( 2 );
         // read coverage offset
         int co = in.readTTFUShort();
-        // read subrule set count
-        int ns = in.readTTFUShort();
+        // read rule set count
+        int nrs = in.readTTFUShort();
+        // read rule set offsets
+        int[] rsoa = new int [ nrs ];
+        for ( int i = 0; i < nrs; i++ ) {
+            rsoa [ i ] = in.readTTFUShort();
+        }
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " chained context substitution format: " + subtableFormat + " (simple)" );
-            log.debug(tableTag + " chained context substitution coverage table offset: " + co );
-            log.debug(tableTag + " chained context substitution subrule set count: " + ns );
+            log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyphs)" );
+            log.debug(tableTag + " chained contextual substitution coverage table offset: " + co );
+            log.debug(tableTag + " chained contextual substitution rule set count: " + nrs );
+            for ( int i = 0; i < nrs; i++ ) {
+                log.debug(tableTag + " chained contextual substitution rule set offset[" + i + "]: " + rsoa[i] );
+            }
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " chained context substitution coverage", subtableOffset + co );
-        // read subrule set table offsets
-        int[] soa = new int[ns];
-        for ( int i = 0, n = ns; i < n; i++ ) {
-            soa[i] = in.readTTFUShort();
+        GlyphCoverageTable ct;
+        if ( co > 0 ) {
+            ct = readCoverageTable ( in, tableTag + " chained contextual substitution coverage", subtableOffset + co );
+        } else {
+            ct = null;
         }
-        // read subrule set tables
-        for ( int i = 0, n = ns; i < n; i++ ) {
-            int so = soa[i];
-            in.seekSet(subtableOffset + so);
-            // read subrule table count
-            int nst = in.readTTFUShort();
-            int[] stoa = new int[nst];
-            for ( int j = 0; j < nst; j++ ) {
-                stoa[j] = in.readTTFUShort();
-            }
-            for ( int j = 0; j < nst; j++ ) {
-                int sto = stoa[j];
-                in.seekSet(subtableOffset + so + sto);
-                // read backtrack glyph count
-                int nbg = in.readTTFUShort();
-                int[] bga = new int[nbg];
-                // read backtrack glyphs
-                for ( int k = 0; k < nbg; k++ ) {
-                    bga[k] = in.readTTFUShort();
-                }
-                // read input glyph count
-                int nig = in.readTTFUShort();
-                int[] iga = new int [ nig - 1 ];
-                // read input glyphs
-                for ( int k = 0; k < nig - 1; k++ ) {
-                    iga[k] = in.readTTFUShort();
-                }
-                // read lookahead glyph count
-                int nlg = in.readTTFUShort();
-                int[] lga = new int[nlg];
-                // read lookahead glyphs
-                for ( int k = 0; k < nlg; k++ ) {
-                    lga[k] = in.readTTFUShort();
-                }
-                // read substitution lookup record count
-                int nsl = in.readTTFUShort();
-                int[] sia = new int[nsl];
-                int[] lia = new int[nsl];
-                // read substitution lookup records
-                for ( int k = 0; k < nsl; k++ ) {
-                    // read sequence index
-                    sia[k] = in.readTTFUShort();
-                    // read lookup list index
-                    lia[k] = in.readTTFUShort();
-                }
-                if (log.isDebugEnabled()) {
-                    log.debug(tableTag + " chained context substitution subrule set[" + i + "]: backtrack [" + toString(bga) + "]" );
-                    log.debug(tableTag + " chained context substitution subrule set[" + i + "]: input     [" + toString(iga) + "]" );
-                    log.debug(tableTag + " chained context substitution subrule set[" + i + "]: lookahead [" + toString(lga) + "]" );
-                    log.debug(tableTag + " chained context substitution lookup count: " + nsl );
-                    for ( int k = 0; k < nsl; k++ ) {
-                        log.debug(tableTag + " chained context substitution lookup[" + i + "]: [" + sia[k] + "," + lia[k] + "]" );
+        // read rule sets
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
+        String header = null;
+        for ( int i = 0; i < nrs; i++ ) {
+            GlyphTable.RuleSet rs;
+            int rso = rsoa [ i ];
+            if ( rso > 0 ) {
+                // seek to rule set [ i ]
+                in.seekSet ( subtableOffset + rso );
+                // read rule count
+                int nr = in.readTTFUShort();
+                // read rule offsets
+                int[] roa = new int [ nr ];
+                GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
+                for ( int j = 0; j < nr; j++ ) {
+                    roa [ j ] = in.readTTFUShort();
+                }
+                // read glyph sequence rules
+                for ( int j = 0; j < nr; j++ ) {
+                    GlyphTable.ChainedGlyphSequenceRule r;
+                    int ro = roa [ j ];
+                    if ( ro > 0 ) {
+                        // seek to rule [ j ]
+                        in.seekSet ( subtableOffset + rso + ro );
+                        // read backtrack glyph count
+                        int nbg = in.readTTFUShort();
+                        // read backtrack glyphs
+                        int[] backtrackGlyphs = new int [ nbg ];
+                        for ( int k = 0, nk = backtrackGlyphs.length; k < nk; k++ ) {
+                            backtrackGlyphs [ k ] = in.readTTFUShort();
+                        }
+                        // read input glyph count
+                        int nig = in.readTTFUShort();
+                        // read glyphs
+                        int[] glyphs = new int [ nig - 1 ];
+                        for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
+                            glyphs [ k ] = in.readTTFUShort();
+                        }
+                        // read lookahead glyph count
+                        int nlg = in.readTTFUShort();
+                        // read lookahead glyphs
+                        int[] lookaheadGlyphs = new int [ nlg ];
+                        for ( int k = 0, nk = lookaheadGlyphs.length; k < nk; k++ ) {
+                            lookaheadGlyphs [ k ] = in.readTTFUShort();
+                        }
+                        // read rule lookup count
+                        int nl = in.readTTFUShort();
+                        // read rule lookups
+                        if (log.isDebugEnabled()) {
+                            header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
+                        }
+                        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+                        r = new GlyphTable.ChainedGlyphSequenceRule ( lookups, nig, glyphs, backtrackGlyphs, lookaheadGlyphs );
+                    } else {
+                        r = null;
                     }
+                    ra [ j ] = r;
                 }
+                rs = new GlyphTable.HomogeneousRuleSet ( ra );
+            } else {
+                rs = null;
             }
+            rsa [ i ] = rs;
         }
+        // store results
+        seMapping = ct;
+        seEntries.add ( rsa );
     }
 
-    private void readChainedContextSubTableFormat2(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+    private void readChainedContextualSubTableFormat2(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
         String tableTag = "GSUB";
         in.seekSet(subtableOffset);
         // skip over format (already known)
         in.skip ( 2 );
         // read coverage offset
         int co = in.readTTFUShort();
-        // read backtrack classdef table offset
-        int bo = in.readTTFUShort();
-        // read input classdef table offset
-        int io = in.readTTFUShort();
-        // read lookahead classdef table offset
-        int lo = in.readTTFUShort();
-        // read subclass set count
-        int ns = in.readTTFUShort();
+        // read backtrack class def table offset
+        int bcdo = in.readTTFUShort();
+        // read input class def table offset
+        int icdo = in.readTTFUShort();
+        // read lookahead class def table offset
+        int lcdo = in.readTTFUShort();
+        // read class set count
+        int ngc = in.readTTFUShort();
+        // read class set offsets
+        int[] csoa = new int [ ngc ];
+        for ( int i = 0; i < ngc; i++ ) {
+            csoa [ i ] = in.readTTFUShort();
+        }
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " chained context substitution format: " + subtableFormat + " (class based)" );
-            log.debug(tableTag + " chained context substitution coverage table offset: " + co );
-            log.debug(tableTag + " chained context substitution backtrack classdef table offset: " + bo );
-            log.debug(tableTag + " chained context substitution input classdef table offset: " + io );
-            log.debug(tableTag + " chained context substitution lookahead classdef table offset: " + lo );
-            log.debug(tableTag + " chained context substitution subclass set count: " + ns );
+            log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyph classes)" );
+            log.debug(tableTag + " chained contextual substitution coverage table offset: " + co );
+            log.debug(tableTag + " chained contextual substitution class set count: " + ngc );
+            for ( int i = 0; i < ngc; i++ ) {
+                log.debug(tableTag + " chained contextual substitution class set offset[" + i + "]: " + csoa[i] );
+            }
         }
         // read coverage table
-        readCoverageTable ( in, tableTag + " chained context substitution coverage", subtableOffset + co );
-        // read subclass set table offsets
-        int[] soa = new int[ns];
-        for ( int i = 0, n = ns; i < n; i++ ) {
-            soa[i] = in.readTTFUShort();
+        GlyphCoverageTable ct;
+        if ( co > 0 ) {
+            ct = readCoverageTable ( in, tableTag + " chained contextual substitution coverage", subtableOffset + co );
+        } else {
+            ct = null;
         }
-        // read subclass set tables
-        for ( int i = 0, n = ns; i < n; i++ ) {
-            int so = soa[i];
-            if ( so == 0 ) {
-                continue;
-            }
-            in.seekSet(subtableOffset + so);
-            // read subclass rule table count
-            int nst = in.readTTFUShort();
-            int[] stoa = new int[nst];
-            for ( int j = 0; j < nst; j++ ) {
-                stoa[j] = in.readTTFUShort();
-            }
-            for ( int j = 0; j < nst; j++ ) {
-                int sto = stoa[j];
-                in.seekSet(subtableOffset + so + sto);
-                // read backtrack class count
-                int nbc = in.readTTFUShort();
-                int[] bca = new int[nbc];
-                // read backtrack classes
-                for ( int k = 0; k < nbc; k++ ) {
-                    bca[k] = in.readTTFUShort();
-                }
-                // read input class count
-                int nic = in.readTTFUShort();
-                int[] ica = new int [ nic - 1 ];
-                // read inpput classes
-                for ( int k = 0; k < nic - 1; k++ ) {
-                    ica[k] = in.readTTFUShort();
-                }
-                // read lookahead class count
-                int nlc = in.readTTFUShort();
-                int[] lca = new int[nlc];
-                // read lookahead classes
-                for ( int k = 0; k < nlc; k++ ) {
-                    lca[k] = in.readTTFUShort();
-                }
-                // read substitution lookup record count
-                int nsl = in.readTTFUShort();
-                int[] sia = new int[nsl];
-                int[] lia = new int[nsl];
-                // read substitution lookup records
-                for ( int k = 0; k < nsl; k++ ) {
-                    // read sequence index
-                    sia[k] = in.readTTFUShort();
-                    // read lookup list index
-                    lia[k] = in.readTTFUShort();
-                }
-                if (log.isDebugEnabled()) {
-                    log.debug(tableTag + " chained context substitution subclass set[" + i + "]: backtrack [" + toString(bca) + "]" );
-                    log.debug(tableTag + " chained context substitution subclass set[" + i + "]: input     [" + toString(ica) + "]" );
-                    log.debug(tableTag + " chained context substitution subclass set[" + i + "]: lookahead [" + toString(lca) + "]" );
-                    log.debug(tableTag + " chained context substitution lookup count: " + nsl );
-                    for ( int k = 0; k < nsl; k++ ) {
-                        log.debug(tableTag + " chained context substitution lookup[" + i + "]: [" + sia[k] + "," + lia[k] + "]" );
+        // read backtrack class definition table
+        GlyphClassTable bcdt;
+        if ( bcdo > 0 ) {
+            bcdt = readClassDefTable ( in, tableTag + " contextual substitution backtrack class definition", subtableOffset + bcdo );
+        } else {
+            bcdt = null;
+        }
+        // read input class definition table
+        GlyphClassTable icdt;
+        if ( icdo > 0 ) {
+            icdt = readClassDefTable ( in, tableTag + " contextual substitution input class definition", subtableOffset + icdo );
+        } else {
+            icdt = null;
+        }
+        // read lookahead class definition table
+        GlyphClassTable lcdt;
+        if ( lcdo > 0 ) {
+            lcdt = readClassDefTable ( in, tableTag + " contextual substitution lookahead class definition", subtableOffset + lcdo );
+        } else {
+            lcdt = null;
+        }
+        // read rule sets
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
+        String header = null;
+        for ( int i = 0; i < ngc; i++ ) {
+            int cso = csoa [ i ];
+            GlyphTable.RuleSet rs;
+            if ( cso > 0 ) {
+                // seek to rule set [ i ]
+                in.seekSet ( subtableOffset + cso );
+                // read rule count
+                int nr = in.readTTFUShort();
+                // read rule offsets
+                int[] roa = new int [ nr ];
+                GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
+                for ( int j = 0; j < nr; j++ ) {
+                    roa [ j ] = in.readTTFUShort();
+                }
+                // read glyph sequence rules
+                for ( int j = 0; j < nr; j++ ) {
+                    int ro = roa [ j ];
+                    GlyphTable.ChainedClassSequenceRule r;
+                    if ( ro > 0 ) {
+                        // seek to rule [ j ]
+                        in.seekSet ( subtableOffset + cso + ro );
+                        // read backtrack glyph class count
+                        int nbc = in.readTTFUShort();
+                        // read backtrack glyph classes
+                        int[] backtrackClasses = new int [ nbc ];
+                        for ( int k = 0, nk = backtrackClasses.length; k < nk; k++ ) {
+                            backtrackClasses [ k ] = in.readTTFUShort();
+                        }
+                        // read input glyph class count
+                        int nic = in.readTTFUShort();
+                        // read input glyph classes
+                        int[] classes = new int [ nic - 1 ];
+                        for ( int k = 0, nk = classes.length; k < nk; k++ ) {
+                            classes [ k ] = in.readTTFUShort();
+                        }
+                        // read lookahead glyph class count
+                        int nlc = in.readTTFUShort();
+                        // read lookahead glyph classes
+                        int[] lookaheadClasses = new int [ nlc ];
+                        for ( int k = 0, nk = lookaheadClasses.length; k < nk; k++ ) {
+                            lookaheadClasses [ k ] = in.readTTFUShort();
+                        }
+                        // read rule lookup count
+                        int nl = in.readTTFUShort();
+                        // read rule lookups
+                        if (log.isDebugEnabled()) {
+                            header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
+                        }
+                        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+                        r = new GlyphTable.ChainedClassSequenceRule ( lookups, nic, classes, backtrackClasses, lookaheadClasses );
+                    } else {
+                        r = null;
                     }
+                    ra [ j ] = r;
                 }
+                rs = new GlyphTable.HomogeneousRuleSet ( ra );
+            } else {
+                rs = null;
             }
+            rsa [ i ] = rs;
         }
+        // store results
+        seMapping = ct;
+        seEntries.add ( icdt );
+        seEntries.add ( bcdt );
+        seEntries.add ( lcdt );
+        seEntries.add ( Integer.valueOf ( ngc ) );
+        seEntries.add ( rsa );
     }
 
-    private void readChainedContextSubTableFormat3(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+    private void readChainedContextualSubTableFormat3(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
         String tableTag = "GSUB";
         in.seekSet(subtableOffset);
         // skip over format (already known)
         in.skip ( 2 );
         // read backtrack glyph count
         int nbg = in.readTTFUShort();
-        // read backtrack glyph coverage table offsets
-        int[] boa = new int[nbg];
+        // read backtrack glyph coverage offsets
+        int[] bgcoa = new int [ nbg ];
         for ( int i = 0; i < nbg; i++ ) {
-            boa[i] = in.readTTFUShort();
+            bgcoa [ i ] = in.readTTFUShort();
         }
         // read input glyph count
         int nig = in.readTTFUShort();
-        // read input glyph coverage table offsets
-        int[] ioa = new int[nig];
+        // read backtrack glyph coverage offsets
+        int[] igcoa = new int [ nig ];
         for ( int i = 0; i < nig; i++ ) {
-            ioa[i] = in.readTTFUShort();
+            igcoa [ i ] = in.readTTFUShort();
         }
         // read lookahead glyph count
         int nlg = in.readTTFUShort();
-        // read lookahead glyph coverage table offsets
-        int[] loa = new int[nlg];
+        // read backtrack glyph coverage offsets
+        int[] lgcoa = new int [ nlg ];
         for ( int i = 0; i < nlg; i++ ) {
-            loa[i] = in.readTTFUShort();
-        }
-        // read substitution lookup record count
-        int nsl = in.readTTFUShort();
-        int[] sia = new int[nsl];
-        int[] lia = new int[nsl];
-        // read substitution lookup records
-        for ( int i = 0; i < nsl; i++ ) {
-            // read sequence index
-            sia[i] = in.readTTFUShort();
-            // read lookup list index
-            lia[i] = in.readTTFUShort();
+            lgcoa [ i ] = in.readTTFUShort();
         }
+        // read substitution lookup count
+        int nl = in.readTTFUShort();
         // dump info if debugging
         if (log.isDebugEnabled()) {
-            log.debug(tableTag + " chained context substitution format: " + subtableFormat + " (coverage based)" );
-            log.debug(tableTag + " chained context substitution backtrack coverage table offsets: " + toString(boa) );
-            log.debug(tableTag + " chained context substitution input coverage table offsets: " + toString(ioa) );
-            log.debug(tableTag + " chained context substitution lookahead coverage table offsets: " + toString(loa) );
-            log.debug(tableTag + " chained context substitution lookup count: " + nsl );
-            for ( int i = 0; i < nsl; i++ ) {
-                log.debug(tableTag + " chained context substitution lookup[" + i + "]: [" + sia[i] + "," + lia[i] + "]" );
+            log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyph sets)" );
+            log.debug(tableTag + " chained contextual substitution backtrack glyph count: " + nbg );
+            for ( int i = 0; i < nbg; i++ ) {
+                log.debug(tableTag + " chained contextual substitution backtrack coverage table offset[" + i + "]: " + bgcoa[i] );
+            }
+            log.debug(tableTag + " chained contextual substitution input glyph count: " + nig );
+            for ( int i = 0; i < nig; i++ ) {
+                log.debug(tableTag + " chained contextual substitution input coverage table offset[" + i + "]: " + igcoa[i] );
+            }
+            log.debug(tableTag + " chained contextual substitution lookahead glyph count: " + nlg );
+            for ( int i = 0; i < nlg; i++ ) {
+                log.debug(tableTag + " chained contextual substitution lookahead coverage table offset[" + i + "]: " + lgcoa[i] );
             }
+            log.debug(tableTag + " chained contextual substitution lookup count: " + nl );
         }
         // read backtrack coverage tables
-        for ( int i = 0; i < boa.length; i++ ) {
-            if (log.isDebugEnabled()) {
-                log.debug(tableTag + " chained context substitution backtrack coverage table[" + i + "]" );
+        GlyphCoverageTable[] bgca = new GlyphCoverageTable[nbg];
+        for ( int i = 0; i < nbg; i++ ) {
+            int bgco = bgcoa [ i ];
+            GlyphCoverageTable bgct;
+            if ( bgco > 0 ) {
+                bgct = readCoverageTable ( in, tableTag + " chained contextual substitution backtrack coverage[" + i + "]", subtableOffset + bgco );
+            } else {
+                bgct = null;
             }
-            readCoverageTable ( in, tableTag + " chained context substitution coverage", subtableOffset + boa [ i ] );
+            bgca[i] = bgct;
         }
         // read input coverage tables
-        for ( int i = 0; i < ioa.length; i++ ) {
-            if (log.isDebugEnabled()) {
-                log.debug(tableTag + " chained context substitution input coverage table[" + i + "]" );
+        GlyphCoverageTable[] igca = new GlyphCoverageTable[nig];
+        for ( int i = 0; i < nig; i++ ) {
+            int igco = igcoa [ i ];
+            GlyphCoverageTable igct;
+            if ( igco > 0 ) {
+                igct = readCoverageTable ( in, tableTag + " chained contextual substitution input coverage[" + i + "]", subtableOffset + igco );
+            } else {
+                igct = null;
             }
-            readCoverageTable ( in, tableTag + " chained context substitution coverage", subtableOffset + ioa [ i ] );
+            igca[i] = igct;
         }
         // read lookahead coverage tables
-        for ( int i = 0; i < loa.length; i++ ) {
-            if (log.isDebugEnabled()) {
-                log.debug(tableTag + " chained context substitution lookahead coverage table[" + i + "]" );
+        GlyphCoverageTable[] lgca = new GlyphCoverageTable[nlg];
+        for ( int i = 0; i < nlg; i++ ) {
+            int lgco = lgcoa [ i ];
+            GlyphCoverageTable lgct;
+            if ( lgco > 0 ) {
+                lgct = readCoverageTable ( in, tableTag + " chained contextual substitution lookahead coverage[" + i + "]", subtableOffset + lgco );
+            } else {
+                lgct = null;
             }
-            readCoverageTable ( in, tableTag + " chained context substitution coverage", subtableOffset + loa [ i ] );
+            lgca[i] = lgct;
+        }
+        // read rule lookups
+        String header = null;
+        if (log.isDebugEnabled()) {
+            header = tableTag + " chained contextual substitution lookups: ";
         }
+        GlyphTable.RuleLookup[] lookups = readRuleLookups ( in, nl, header );
+        // construct rule, rule set, and rule set array
+        GlyphTable.Rule r = new GlyphTable.ChainedCoverageSequenceRule ( lookups, nig, igca, bgca, lgca );
+        GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
+        GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
+        // store results
+        assert ( igca != null ) && ( igca.length > 0 );
+        seMapping = igca[0];
+        seEntries.add ( rsa );
     }
 
-    private int readChainedContextSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
+    private int readChainedContextualSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
         if ( sf == 1 ) {
-            readChainedContextSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+            readChainedContextualSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
         } else if ( sf == 2 ) {
-            readChainedContextSubTableFormat2 ( in, lookupType, lookupFlags, subtableOffset, sf );
+            readChainedContextualSubTableFormat2 ( in, lookupType, lookupFlags, subtableOffset, sf );
         } else if ( sf == 3 ) {
-            readChainedContextSubTableFormat3 ( in, lookupType, lookupFlags, subtableOffset, sf );
+            readChainedContextualSubTableFormat3 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported chained contextual substitution subtable format: " + sf );
         }
         return sf;
     }
 
-    private int readExtensionSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
+    private void readExtensionSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GSUB";
         in.seekSet(subtableOffset);
-        // read substitution format
-        int sf = in.readTTFUShort();
-        // [TBD] - implement me
-        return sf;
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read extension lookup type
+        int lt = in.readTTFUShort();
+        // read extension offset
+        long eo = in.readTTFULong();
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " extension substitution subtable format: " + subtableFormat );
+            log.debug(tableTag + " extension substitution lookup type: " + lt );
+            log.debug(tableTag + " extension substitution lookup table offset: " + eo );
+        }
+        // read referenced subtable from extended offset
+        readGSUBSubtable ( in, lt, lookupFlags, lookupSequence, subtableSequence, subtableOffset + eo );
     }
 
-    private int readReverseChainedSingleSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
+    private int readExtensionSubTable(FontFileReader in, int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read substitution subtable format
         int sf = in.readTTFUShort();
-        // [TBD] - implement me
+        if ( sf == 1 ) {
+            readExtensionSubTableFormat1 ( in, lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported extension substitution subtable format: " + sf );
+        }
         return sf;
     }
 
-    private void readGSUBSubtable(FontFileReader in, int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
-        initSESubState();
-        int subtableFormat = -1;
-        switch ( lookupType ) {
-        case GSUBLookupType.SINGLE:
-            subtableFormat = readSingleSubTable ( in, lookupType, lookupFlags, subtableOffset );
-            break;
-        case GSUBLookupType.MULTIPLE:
-            subtableFormat = readMultipleSubTable ( in, lookupType, lookupFlags, subtableOffset );
-            break;
-        case GSUBLookupType.ALTERNATE:
-            subtableFormat = readAlternateSubTable ( in, lookupType, lookupFlags, subtableOffset );
-            break;
+    private void readReverseChainedSingleSubTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GSUB";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read coverage offset
+        int co = in.readTTFUShort();
+        // read backtrack glyph count
+        int nbg = in.readTTFUShort();
+        // read backtrack glyph coverage offsets
+        int[] bgcoa = new int [ nbg ];
+        for ( int i = 0; i < nbg; i++ ) {
+            bgcoa [ i ] = in.readTTFUShort();
+        }
+        // read lookahead glyph count
+        int nlg = in.readTTFUShort();
+        // read backtrack glyph coverage offsets
+        int[] lgcoa = new int [ nlg ];
+        for ( int i = 0; i < nlg; i++ ) {
+            lgcoa [ i ] = in.readTTFUShort();
+        }
+        // read substitution (output) glyph count
+        int ng = in.readTTFUShort();
+        // read substitution (output) glyphs
+        int[] glyphs = new int [ ng ];
+        for ( int i = 0, n = ng; i < n; i++ ) {
+            glyphs [ i ] = in.readTTFUShort();
+        }
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " reverse chained contextual substitution format: " + subtableFormat );
+            log.debug(tableTag + " reverse chained contextual substitution coverage table offset: " + co );
+            log.debug(tableTag + " reverse chained contextual substitution backtrack glyph count: " + nbg );
+            for ( int i = 0; i < nbg; i++ ) {
+                log.debug(tableTag + " reverse chained contextual substitution backtrack coverage table offset[" + i + "]: " + bgcoa[i] );
+            }
+            log.debug(tableTag + " reverse chained contextual substitution lookahead glyph count: " + nlg );
+            for ( int i = 0; i < nlg; i++ ) {
+                log.debug(tableTag + " reverse chained contextual substitution lookahead coverage table offset[" + i + "]: " + lgcoa[i] );
+            }
+            log.debug(tableTag + " reverse chained contextual substitution glyphs: " + toString(glyphs) );
+        }
+        // read coverage table
+        GlyphCoverageTable ct = readCoverageTable ( in, tableTag + " reverse chained contextual substitution coverage", subtableOffset + co );
+        // read backtrack coverage tables
+        GlyphCoverageTable[] bgca = new GlyphCoverageTable[nbg];
+        for ( int i = 0; i < nbg; i++ ) {
+            int bgco = bgcoa[i];
+            GlyphCoverageTable bgct;
+            if ( bgco > 0 ) {
+                bgct = readCoverageTable ( in, tableTag + " reverse chained contextual substitution backtrack coverage[" + i + "]", subtableOffset + bgco );
+            } else {
+                bgct = null;
+            }
+            bgca[i] = bgct;
+        }
+        // read lookahead coverage tables
+        GlyphCoverageTable[] lgca = new GlyphCoverageTable[nlg];
+        for ( int i = 0; i < nlg; i++ ) {
+            int lgco = lgcoa[i];
+            GlyphCoverageTable lgct;
+            if ( lgco > 0 ) {
+                lgct = readCoverageTable ( in, tableTag + " reverse chained contextual substitution lookahead coverage[" + i + "]", subtableOffset + lgco );
+            } else {
+                lgct = null;
+            }
+            lgca[i] = lgct;
+        }
+        // store results
+        seMapping = ct;
+        seEntries.add ( bgca );
+        seEntries.add ( lgca );
+        seEntries.add ( glyphs );
+    }
+
+    private int readReverseChainedSingleSubTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
+        in.seekSet(subtableOffset);
+        // read substitution subtable format
+        int sf = in.readTTFUShort();
+        if ( sf == 1 ) {
+            readReverseChainedSingleSubTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported reverse chained single substitution subtable format: " + sf );
+        }
+        return sf;
+    }
+
+    private void readGSUBSubtable(FontFileReader in, int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
+        initSESubState();
+        int subtableFormat = -1;
+        switch ( lookupType ) {
+        case GSUBLookupType.SINGLE:
+            subtableFormat = readSingleSubTable ( in, lookupType, lookupFlags, subtableOffset );
+            break;
+        case GSUBLookupType.MULTIPLE:
+            subtableFormat = readMultipleSubTable ( in, lookupType, lookupFlags, subtableOffset );
+            break;
+        case GSUBLookupType.ALTERNATE:
+            subtableFormat = readAlternateSubTable ( in, lookupType, lookupFlags, subtableOffset );
+            break;
         case GSUBLookupType.LIGATURE:
             subtableFormat = readLigatureSubTable ( in, lookupType, lookupFlags, subtableOffset );
             break;
-        case GSUBLookupType.CONTEXT:
-            subtableFormat = readContextSubTable ( in, lookupType, lookupFlags, subtableOffset );
+        case GSUBLookupType.CONTEXTUAL:
+            subtableFormat = readContextualSubTable ( in, lookupType, lookupFlags, subtableOffset );
             break;
-        case GSUBLookupType.CHAINED_CONTEXT:
-            subtableFormat = readChainedContextSubTable ( in, lookupType, lookupFlags, subtableOffset );
+        case GSUBLookupType.CHAINED_CONTEXTUAL:
+            subtableFormat = readChainedContextualSubTable ( in, lookupType, lookupFlags, subtableOffset );
             break;
         case GSUBLookupType.REVERSE_CHAINED_SINGLE:
             subtableFormat = readReverseChainedSingleSubTable ( in, lookupType, lookupFlags, subtableOffset );
             break;
         case GSUBLookupType.EXTENSION:
-            subtableFormat = readExtensionSubTable ( in, lookupType, lookupFlags, subtableOffset );
+            subtableFormat = readExtensionSubTable ( in, lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset );
             break;
         default:
             break;
@@ -2554,75 +3216,1419 @@ public class TTFFile {
         resetSESubState();
     }
 
+    private GlyphPositioningTable.DeviceTable readPosDeviceTable(FontFileReader in, long subtableOffset, long deviceTableOffset) throws IOException {
+        long cp = in.getCurrentPos();
+        in.seekSet(subtableOffset + deviceTableOffset);
+        // read start size
+        int ss = in.readTTFUShort();
+        // read end size
+        int es = in.readTTFUShort();
+        // read delta format
+        int df = in.readTTFUShort();
+        // read deltas
+        int n = ( es - ss ) + 1;
+        int[] da = new int [ n ];
+        int s1, m1, dm, dd, s2;
+        if ( df == 1 ) {
+            s1 = 14; m1 = 0x3; dm = 1; dd = 4; s2 = 2;
+        } else if ( df == 2 ) {
+            s1 = 12; m1 = 0xF; dm = 7; dd = 16; s2 = 4;
+        } else if ( df == 3 ) {
+            s1 = 8; m1 = 0xFF; dm = 127; dd = 256; s2 = 8;
+        } else {
+            throw new UnsupportedOperationException ( "unsupported device table delta format: " + df );
+        }
+        for ( int i = 0; ( i < n ) && ( s2 > 0 );) {
+            int p = in.readTTFUShort();
+            for ( int j = 0, k = 16 / s2; j < k; j++ ) {
+                int d = ( p >> s1 ) & m1;
+                if ( d > dm ) {
+                    d -= dd;
+                }
+                if ( i < n ) {
+                    da [ i++ ] = d;
+                } else {
+                    break;
+                }
+                p <<= s2;
+            }
+        }
+        in.seekSet(cp);
+        return new GlyphPositioningTable.DeviceTable ( ss, es, da );
+    }
+
+    private GlyphPositioningTable.Value readPosValue(FontFileReader in, long subtableOffset, int valueFormat) throws IOException {
+        // XPlacement
+        int xp;
+        if ( ( valueFormat & GlyphPositioningTable.Value.X_PLACEMENT ) != 0 ) {
+            xp = convertTTFUnit2PDFUnit ( in.readTTFShort() );
+        } else {
+            xp = 0;
+        }
+        // YPlacement
+        int yp;
+        if ( ( valueFormat & GlyphPositioningTable.Value.Y_PLACEMENT ) != 0 ) {
+            yp = convertTTFUnit2PDFUnit ( in.readTTFShort() );
+        } else {
+            yp = 0;
+        }
+        // XAdvance
+        int xa;
+        if ( ( valueFormat & GlyphPositioningTable.Value.X_ADVANCE ) != 0 ) {
+            xa = convertTTFUnit2PDFUnit ( in.readTTFShort() );
+        } else {
+            xa = 0;
+        }
+        // YAdvance
+        int ya;
+        if ( ( valueFormat & GlyphPositioningTable.Value.Y_ADVANCE ) != 0 ) {
+            ya = convertTTFUnit2PDFUnit ( in.readTTFShort() );
+        } else {
+            ya = 0;
+        }
+        // XPlaDevice
+        GlyphPositioningTable.DeviceTable xpd;
+        if ( ( valueFormat & GlyphPositioningTable.Value.X_PLACEMENT_DEVICE ) != 0 ) {
+            int xpdo = in.readTTFUShort();
+            xpd = readPosDeviceTable ( in, subtableOffset, xpdo );
+        } else {
+            xpd = null;
+        }
+        // YPlaDevice
+        GlyphPositioningTable.DeviceTable ypd;
+        if ( ( valueFormat & GlyphPositioningTable.Value.Y_PLACEMENT_DEVICE ) != 0 ) {
+            int ypdo = in.readTTFUShort();
+            ypd = readPosDeviceTable ( in, subtableOffset, ypdo );
+        } else {
+            ypd = null;
+        }
+        // XAdvDevice
+        GlyphPositioningTable.DeviceTable xad;
+        if ( ( valueFormat & GlyphPositioningTable.Value.X_ADVANCE_DEVICE ) != 0 ) {
+            int xado = in.readTTFUShort();
+            xad = readPosDeviceTable ( in, subtableOffset, xado );
+        } else {
+            xad = null;
+        }
+        // YAdvDevice
+        GlyphPositioningTable.DeviceTable yad;
+        if ( ( valueFormat & GlyphPositioningTable.Value.Y_ADVANCE_DEVICE ) != 0 ) {
+            int yado = in.readTTFUShort();
+            yad = readPosDeviceTable ( in, subtableOffset, yado );
+        } else {
+            yad = null;
+        }
+        return new GlyphPositioningTable.Value ( xp, yp, xa, ya, xpd, ypd, xad, yad );
+    }
+
+    private void readSinglePosTableFormat1(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GPOS";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read coverage offset
+        int co = in.readTTFUShort();
+        // read value format
+        int vf = in.readTTFUShort();
+        // read value
+        GlyphPositioningTable.Value v = readPosValue ( in, subtableOffset, vf );
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " single positioning subtable format: " + subtableFormat + " (delta)" );
+            log.debug(tableTag + " single positioning coverage table offset: " + co );
+            log.debug(tableTag + " single positioning value: " + v );
+        }
+        // read coverage table
+        GlyphCoverageTable ct = readCoverageTable ( in, tableTag + " single positioning coverage", subtableOffset + co );
+        // store results
+        seMapping = ct;
+        seEntries.add ( v );
+    }
+
+    private void readSinglePosTableFormat2(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
+        String tableTag = "GPOS";
+        in.seekSet(subtableOffset);
+        // skip over format (already known)
+        in.skip ( 2 );
+        // read coverage offset
+        int co = in.readTTFUShort();
+        // read value format
+        int vf = in.readTTFUShort();
+        // read value count
+        int nv = in.readTTFUShort();
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " single positioning subtable format: " + subtableFormat + " (mapped)" );
+            log.debug(tableTag + " single positioning coverage table offset: " + co );
+            log.debug(tableTag + " single positioning value count: " + nv );
+        }
+        // read coverage table
+        GlyphCoverageTable ct = readCoverageTable ( in, tableTag + " single positioning coverage", subtableOffset + co );
+        // read positioning values
+        GlyphPositioningTable.Value[] pva = new GlyphPositioningTable.Value[nv];
+        for ( int i = 0, n = nv; i < n; i++ ) {
+            GlyphPositioningTable.Value pv = readPosValue ( in, subtableOffset, vf );
+            if (log.isDebugEnabled()) {
+                log.debug(tableTag + " single positioning value[" + i + "]: " + pv );
+            }
+            pva[i] = pv;
+        }
+        // store results
+        seMapping = ct;
+        seEntries.add ( pva );
+    }
+
     private int readSinglePosTable(FontFileReader in, int lookupType, int lookupFlags, long subtableOffset) throws IOException {
         in.seekSet(subtableOffset);
-        // read substitution format
+        // read positionining subtable format
         int sf = in.readTTFUShort();
-        // [TBD] - implement me
+        if ( sf == 1 ) {
+            readSinglePosTableFormat1 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else if ( sf == 2 ) {
+            readSinglePosTableFormat2 ( in, lookupType, lookupFlags, subtableOffset, sf );
+        } else {
+            throw new UnsupportedOperationException ( "unsupported single positioning subtable format: " + sf );
+        }
         return sf;
     }
 
+    private GlyphPositioningTable.PairValues readPosPairValues(FontFileReader in, long subtableOffset, boolean hasGlyph, int vf1, int vf2) throws IOException {
+        // read glyph (if present)
+        int glyph;
+        if ( hasGlyph ) {
+            glyph = in.readTTFUShort();
+        } else {
+            glyph = 0;
+        }
+        // read first value (if present)
+        GlyphPositioningTable.Value v1;
+        if ( vf1 != 0 ) {
+            v1 = readPosValue ( in, subtableOffset, vf1 );
+        } else {
+            v1 = null;
+        }
+        // read second value (if present)
+        GlyphPositioningTable.Value v2;
+        if ( vf2 != 0 ) {
+            v2 = readPosValue ( in, subtableOffset, vf2 );
+        } else {
+            v2 = null;
+        }
+        return new GlyphPositioningTable.PairValues ( glyph, v1, v2 );
+    }
+
+    private GlyphPositioningTable.PairValues[] readPosPairSetTable(FontFileReader in, long subtableOffset, int pairSetTableOffset, int vf1, int vf2) throws IOException {
+        String tableTag = "GPOS";
+        long cp = in.getCurrentPos();
+        in.seekSet(subtableOffset + pairSetTableOffset);
+        // read pair values count
+        int npv = in.readTTFUShort();
+        // dump info if debugging
+        if (log.isDebugEnabled()) {
+            log.debug(tableTag + " pair set table offset: " + pairSetTableOffset );
+            log.debug(tableTag + " pair set table values count: " + npv );
+        }
+        // read pair values
+        GlyphPositioningTable.PairValues[] pva = new GlyphPositioningTable.PairValues [ npv ];

[... 1653 lines stripped ...]


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org