[USP10] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / usp10 / opentype.c
index 2881f5d..2d708a8 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
  */
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "wingdi.h"
+#include "winuser.h"
+#include "winnls.h"
+#include "usp10.h"
+#include "winternl.h"
 
 #include "usp10_internal.h"
 
-#include <winternl.h>
+#include "wine/debug.h"
+#include "wine/heap.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
 
@@ -38,6 +49,31 @@ WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
 /* These are all structures needed for the cmap format 12 table */
 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
 
+enum gpos_lookup_type
+{
+    GPOS_LOOKUP_ADJUST_SINGLE = 0x1,
+    GPOS_LOOKUP_ADJUST_PAIR = 0x2,
+    GPOS_LOOKUP_ATTACH_CURSIVE = 0x3,
+    GPOS_LOOKUP_ATTACH_MARK_TO_BASE = 0x4,
+    GPOS_LOOKUP_ATTACH_MARK_TO_LIGATURE = 0x5,
+    GPOS_LOOKUP_ATTACH_MARK_TO_MARK = 0x6,
+    GPOS_LOOKUP_POSITION_CONTEXT = 0x7,
+    GPOS_LOOKUP_POSITION_CONTEXT_CHAINED = 0x8,
+    GPOS_LOOKUP_POSITION_EXTENSION = 0x9,
+};
+
+enum gsub_lookup_type
+{
+    GSUB_LOOKUP_SINGLE = 0x1,
+    GSUB_LOOKUP_MULTIPLE = 0x2,
+    GSUB_LOOKUP_ALTERNATE = 0x3,
+    GSUB_LOOKUP_LIGATURE = 0x4,
+    GSUB_LOOKUP_CONTEXT = 0x5,
+    GSUB_LOOKUP_CONTEXT_CHAINED = 0x6,
+    GSUB_LOOKUP_EXTENSION = 0x7,
+    GSUB_LOOKUP_CONTEXT_CHAINED_REVERSE = 0x8,
+};
+
 typedef struct {
     WORD platformID;
     WORD encodingID;
@@ -227,6 +263,51 @@ typedef struct{
 
 }GSUB_SubstLookupRecord;
 
+typedef struct{
+    WORD SubstFormat;
+    WORD Coverage;
+    WORD SubRuleSetCount;
+    WORD SubRuleSet[1];
+}GSUB_ContextSubstFormat1;
+
+typedef struct{
+    WORD SubRuleCount;
+    WORD SubRule[1];
+}GSUB_SubRuleSet;
+
+typedef struct {
+    WORD GlyphCount;
+    WORD SubstCount;
+    WORD Input[1];
+}GSUB_SubRule_1;
+
+typedef struct {
+    GSUB_SubstLookupRecord SubstLookupRecord[1];
+}GSUB_SubRule_2;
+
+typedef struct {
+    WORD SubstFormat;
+    WORD Coverage;
+    WORD ClassDef;
+    WORD SubClassSetCnt;
+    WORD SubClassSet[1];
+}GSUB_ContextSubstFormat2;
+
+typedef struct {
+    WORD SubClassRuleCnt;
+    WORD SubClassRule[1];
+}GSUB_SubClassSet;
+
+typedef struct {
+    WORD GlyphCount;
+    WORD SubstCount;
+    WORD Class[1];
+}GSUB_SubClassRule_1;
+
+typedef struct {
+    GSUB_SubstLookupRecord SubstLookupRecord[1];
+}GSUB_SubClassRule_2;
+
 typedef struct{
     WORD SubstFormat; /* = 1 */
     WORD Coverage;
@@ -234,6 +315,41 @@ typedef struct{
     WORD ChainSubRuleSet[1];
 }GSUB_ChainContextSubstFormat1;
 
+typedef struct {
+    WORD SubstFormat; /* = 2 */
+    WORD Coverage;
+    WORD BacktrackClassDef;
+    WORD InputClassDef;
+    WORD LookaheadClassDef;
+    WORD ChainSubClassSetCnt;
+    WORD ChainSubClassSet[1];
+}GSUB_ChainContextSubstFormat2;
+
+typedef struct {
+    WORD ChainSubClassRuleCnt;
+    WORD ChainSubClassRule[1];
+}GSUB_ChainSubClassSet;
+
+typedef struct {
+    WORD BacktrackGlyphCount;
+    WORD Backtrack[1];
+}GSUB_ChainSubClassRule_1;
+
+typedef struct {
+    WORD InputGlyphCount;
+    WORD Input[1];
+}GSUB_ChainSubClassRule_2;
+
+typedef struct {
+    WORD LookaheadGlyphCount;
+    WORD LookAhead[1];
+}GSUB_ChainSubClassRule_3;
+
+typedef struct {
+    WORD SubstCount;
+    GSUB_SubstLookupRecord SubstLookupRecord[1];
+}GSUB_ChainSubClassRule_4;
+
 typedef struct {
     WORD SubstFormat; /* = 3 */
     WORD BacktrackGlyphCount;
@@ -267,6 +383,12 @@ typedef struct{
     WORD Alternate[1];
 } GSUB_AlternateSet;
 
+typedef struct {
+    WORD SubstFormat;
+    WORD ExtensionLookupType;
+    DWORD ExtensionOffset;
+} GSUB_ExtensionPosFormat1;
+
 /* These are all structures needed for the GPOS table */
 
 typedef struct {
@@ -448,6 +570,29 @@ typedef struct {
     WORD LookupListIndex;
 } GPOS_PosLookupRecord;
 
+typedef struct {
+    WORD PosFormat;
+    WORD Coverage;
+    WORD ClassDef;
+    WORD PosClassSetCnt;
+    WORD PosClassSet[1];
+} GPOS_ContextPosFormat2;
+
+typedef struct {
+    WORD PosClassRuleCnt;
+    WORD PosClassRule[1];
+} GPOS_PosClassSet;
+
+typedef struct {
+    WORD GlyphCount;
+    WORD PosCount;
+    WORD Class[1];
+} GPOS_PosClassRule_1;
+
+typedef struct {
+    GPOS_PosLookupRecord PosLookupRecord[1];
+} GPOS_PosClassRule_2;
+
 typedef struct {
     WORD PosFormat;
     WORD BacktrackGlyphCount;
@@ -490,7 +635,7 @@ static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
         length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
         if (length != GDI_ERROR)
         {
-            psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
+            psc->CMAP_Table = heap_alloc(length);
             GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
             TRACE("Loaded cmap table of %i bytes\n",length);
         }
@@ -525,22 +670,22 @@ static int compare_group(const void *a, const void* b)
     return 0;
 }
 
-DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
+DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, WORD *glyph_index, DWORD flags)
 {
     /* BMP: use gdi32 for ease */
     if (utf32c < 0x10000)
     {
         WCHAR ch = utf32c;
-        return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
+        return GetGlyphIndicesW(hdc, &ch, 1, glyph_index, flags);
     }
 
     if (!psc->CMAP_format12_Table)
         psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
 
     if (flags & GGI_MARK_NONEXISTING_GLYPHS)
-        *pgi = 0xffff;
+        *glyph_index = 0xffffu;
     else
-        *pgi = 0;
+        *glyph_index = 0u;
 
     if (psc->CMAP_format12_Table)
     {
@@ -555,7 +700,7 @@ DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWOR
         if (group)
         {
             DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
-            *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
+            *glyph_index = GET_BE_DWORD(group->startGlyphID) + offset;
             return 0;
         }
     }
@@ -671,7 +816,7 @@ void OpenType_GDEF_UpdateGlyphProps(ScriptCache *psc, const WORD *pwGlyphs, cons
  **********/
 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
 
-static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
+static int GSUB_is_glyph_covered(const void *table, unsigned int glyph)
 {
     const OT_CoverageFormat1* cf1;
 
@@ -715,6 +860,25 @@ static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
     return -1;
 }
 
+static const BYTE *GSUB_get_subtable(const OT_LookupTable *look, int index)
+{
+    int offset = GET_BE_WORD(look->SubTable[index]);
+
+    if (GET_BE_WORD(look->LookupType) == GSUB_LOOKUP_EXTENSION)
+    {
+        const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)look + offset);
+        if (GET_BE_WORD(ext->SubstFormat) == 1)
+        {
+            offset += GET_BE_DWORD(ext->ExtensionOffset);
+        }
+        else
+        {
+            FIXME("Unhandled Extension Substitution Format %i\n",GET_BE_WORD(ext->SubstFormat));
+        }
+    }
+    return (const BYTE *)look + offset;
+}
+
 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
 {
     int j;
@@ -722,10 +886,7 @@ static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT
 
     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
     {
-        int offset;
-        const GSUB_SingleSubstFormat1 *ssf1;
-        offset = GET_BE_WORD(look->SubTable[j]);
-        ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
+        const GSUB_SingleSubstFormat1 *ssf1 = (const GSUB_SingleSubstFormat1*)GSUB_get_subtable(look, j);
         if (GET_BE_WORD(ssf1->SubstFormat) == 1)
         {
             int offset = GET_BE_WORD(ssf1->Coverage);
@@ -773,8 +934,7 @@ static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, IN
     {
         int offset, index;
         const GSUB_MultipleSubstFormat1 *msf1;
-        offset = GET_BE_WORD(look->SubTable[j]);
-        msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
+        msf1 = (const GSUB_MultipleSubstFormat1*)GSUB_get_subtable(look, j);
 
         offset = GET_BE_WORD(msf1->Coverage);
         index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
@@ -806,7 +966,10 @@ static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, IN
                 TRACE("\n");
             }
 
-            return glyph_index + (sub_count * write_dir);
+            if (write_dir > 0)
+                return glyph_index + sub_count;
+            else
+                return glyph_index - 1;
         }
     }
     return GSUB_E_NOGLYPH;
@@ -823,8 +986,7 @@ static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, I
         const GSUB_AlternateSubstFormat1 *asf1;
         INT index;
 
-        offset = GET_BE_WORD(look->SubTable[j]);
-        asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
+        asf1 = (const GSUB_AlternateSubstFormat1*)GSUB_get_subtable(look, j);
         offset = GET_BE_WORD(asf1->Coverage);
 
         index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
@@ -856,8 +1018,7 @@ static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, IN
         const GSUB_LigatureSubstFormat1 *lsf1;
         int offset,index;
 
-        offset = GET_BE_WORD(look->SubTable[j]);
-        lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
+        lsf1 = (const GSUB_LigatureSubstFormat1*)GSUB_get_subtable(look, j);
         offset = GET_BE_WORD(lsf1->Coverage);
         index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
         TRACE("  Coverage index %i\n",index);
@@ -898,9 +1059,8 @@ static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, IN
                     TRACE("0x%x\n",glyphs[replaceIdx]);
                     if (CompCount > 0)
                     {
-                        int j;
-                        for (j = replaceIdx + 1; j < *glyph_count; j++)
-                            glyphs[j] =glyphs[j+CompCount];
+                        unsigned int j = replaceIdx + 1;
+                        memmove(&glyphs[j], &glyphs[j + CompCount], (*glyph_count - j) * sizeof(*glyphs));
                         *glyph_count = *glyph_count - CompCount;
                     }
                     return replaceIdx + write_dir;
@@ -911,137 +1071,474 @@ static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, IN
     return GSUB_E_NOGLYPH;
 }
 
+static INT GSUB_apply_ContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+{
+    int j;
+    TRACE("Context Substitution Subtable\n");
+    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
+    {
+        const GSUB_ContextSubstFormat1 *csf1;
+
+        csf1 = (const GSUB_ContextSubstFormat1*)GSUB_get_subtable(look, j);
+        if (GET_BE_WORD(csf1->SubstFormat) == 1)
+        {
+            int offset, index;
+            TRACE("Context Substitution Subtable: Class 1\n");
+            offset = GET_BE_WORD(csf1->Coverage);
+            index = GSUB_is_glyph_covered((const BYTE*)csf1+offset, glyphs[glyph_index]);
+            TRACE("  Coverage index %i\n",index);
+            if (index != -1)
+            {
+                int k, count;
+                const GSUB_SubRuleSet *srs;
+                offset = GET_BE_WORD(csf1->SubRuleSet[index]);
+                srs = (const GSUB_SubRuleSet*)((const BYTE*)csf1+offset);
+                count = GET_BE_WORD(srs->SubRuleCount);
+                TRACE("  SubRuleSet has %i members\n",count);
+                for (k = 0; k < count; k++)
+                {
+                    const GSUB_SubRule_1 *sr;
+                    const GSUB_SubRule_2 *sr_2;
+                    unsigned int g;
+                    int g_count, l;
+                    int newIndex = glyph_index;
+
+                    offset = GET_BE_WORD(srs->SubRule[k]);
+                    sr = (const GSUB_SubRule_1*)((const BYTE*)srs+offset);
+                    g_count = GET_BE_WORD(sr->GlyphCount);
+                    TRACE("   SubRule has %i glyphs\n",g_count);
+
+                    g = glyph_index + write_dir * (g_count - 1);
+                    if (g >= *glyph_count)
+                        continue;
+
+                    for (l = 0; l < g_count-1; l++)
+                        if (glyphs[glyph_index + (write_dir * (l+1))] != GET_BE_WORD(sr->Input[l])) break;
+
+                    if (l < g_count-1)
+                    {
+                        TRACE("   Rule does not match\n");
+                        continue;
+                    }
+
+                    TRACE("   Rule matches\n");
+                    sr_2 = (const GSUB_SubRule_2 *)&sr->Input[g_count - 1];
+
+                    for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
+                    {
+                        unsigned int lookup_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
+                        unsigned int sequence_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex);
+
+                        g = glyph_index + write_dir * sequence_index;
+                        if (g >= *glyph_count)
+                        {
+                            WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
+                                    sequence_index, glyph_index, write_dir);
+                            continue;
+                        }
+
+                        TRACE("   SUBST: %u -> %u %u.\n", l, sequence_index, lookup_index);
+                        newIndex = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
+                        if (newIndex == GSUB_E_NOGLYPH)
+                        {
+                            ERR("   Chain failed to generate a glyph\n");
+                            continue;
+                        }
+                    }
+                    return newIndex;
+                }
+            }
+        }
+        else if (GET_BE_WORD(csf1->SubstFormat) == 2)
+        {
+            const GSUB_ContextSubstFormat2 *csf2;
+            const void *glyph_class_table;
+            int offset, index;
+
+            csf2 = (const GSUB_ContextSubstFormat2*)csf1;
+            TRACE("Context Substitution Subtable: Class 2\n");
+            offset = GET_BE_WORD(csf2->Coverage);
+            index = GSUB_is_glyph_covered((const BYTE*)csf2+offset, glyphs[glyph_index]);
+            TRACE("  Coverage index %i\n",index);
+            if (index != -1)
+            {
+                int k, count, class;
+                const GSUB_SubClassSet *scs;
+
+                offset = GET_BE_WORD(csf2->ClassDef);
+                glyph_class_table = (const BYTE *)csf2 + offset;
+
+                class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
+
+                offset = GET_BE_WORD(csf2->SubClassSet[class]);
+                if (offset == 0)
+                {
+                    TRACE("  No class rule table for class %i\n",class);
+                    continue;
+                }
+                scs = (const GSUB_SubClassSet*)((const BYTE*)csf2+offset);
+                count = GET_BE_WORD(scs->SubClassRuleCnt);
+                TRACE("  SubClassSet has %i members\n",count);
+                for (k = 0; k < count; k++)
+                {
+                    const GSUB_SubClassRule_1 *sr;
+                    const GSUB_SubClassRule_2 *sr_2;
+                    unsigned int g;
+                    int g_count, l;
+                    int newIndex = glyph_index;
+
+                    offset = GET_BE_WORD(scs->SubClassRule[k]);
+                    sr = (const GSUB_SubClassRule_1*)((const BYTE*)scs+offset);
+                    g_count = GET_BE_WORD(sr->GlyphCount);
+                    TRACE("   SubClassRule has %i glyphs classes\n",g_count);
+
+                    g = glyph_index + write_dir * (g_count - 1);
+                    if (g >= *glyph_count)
+                        continue;
+
+                    for (l = 0; l < g_count-1; l++)
+                    {
+                        int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
+                        if (g_class != GET_BE_WORD(sr->Class[l])) break;
+                    }
+
+                    if (l < g_count-1)
+                    {
+                        TRACE("   Rule does not match\n");
+                        continue;
+                    }
+
+                    TRACE("   Rule matches\n");
+                    sr_2 = (const GSUB_SubClassRule_2 *)&sr->Class[g_count - 1];
+
+                    for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
+                    {
+                        unsigned int lookup_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
+                        unsigned int sequence_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex);
+
+                        g = glyph_index + write_dir * sequence_index;
+                        if (g >= *glyph_count)
+                        {
+                            WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
+                                    sequence_index, glyph_index, write_dir);
+                            continue;
+                        }
+
+                        TRACE("   SUBST: %u -> %u %u.\n", l, sequence_index, lookup_index);
+                        newIndex = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
+                        if (newIndex == GSUB_E_NOGLYPH)
+                        {
+                            ERR("   Chain failed to generate a glyph\n");
+                            continue;
+                        }
+                    }
+                    return newIndex;
+                }
+            }
+        }
+        else
+            FIXME("Unhandled Context Substitution Format %i\n", GET_BE_WORD(csf1->SubstFormat));
+    }
+    return GSUB_E_NOGLYPH;
+}
+
 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
 {
     int j;
-    BOOL done = FALSE;
 
     TRACE("Chaining Contextual Substitution Subtable\n");
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
+    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
     {
         const GSUB_ChainContextSubstFormat1 *ccsf1;
         int offset;
         int dirLookahead = write_dir;
         int dirBacktrack = -1 * write_dir;
 
-        offset = GET_BE_WORD(look->SubTable[j]);
-        ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
+        ccsf1 = (const GSUB_ChainContextSubstFormat1*)GSUB_get_subtable(look, j);
         if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
         {
-            FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
+            static int once;
+            if (!once++)
+                FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
             continue;
         }
         else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
         {
-#ifndef __REACTOS__
-            FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
-#endif
-            continue;
+            WORD offset, count;
+            const void *backtrack_class_table;
+            const void *input_class_table;
+            const void *lookahead_class_table;
+            int i;
+            WORD class;
+
+            const GSUB_ChainContextSubstFormat2 *ccsf2 = (const GSUB_ChainContextSubstFormat2*)ccsf1;
+            const GSUB_ChainSubClassSet *csc;
+
+            TRACE("  subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
+
+            offset = GET_BE_WORD(ccsf2->Coverage);
+
+            if (GSUB_is_glyph_covered((const BYTE*)ccsf2+offset, glyphs[glyph_index]) == -1)
+            {
+                TRACE("Glyph not covered\n");
+                continue;
+            }
+            offset = GET_BE_WORD(ccsf2->BacktrackClassDef);
+            backtrack_class_table = (const BYTE*)ccsf2+offset;
+            offset = GET_BE_WORD(ccsf2->InputClassDef);
+            input_class_table = (const BYTE*)ccsf2+offset;
+            offset = GET_BE_WORD(ccsf2->LookaheadClassDef);
+            lookahead_class_table = (const BYTE*)ccsf2+offset;
+            count =  GET_BE_WORD(ccsf2->ChainSubClassSetCnt);
+
+            class = OT_get_glyph_class(input_class_table, glyphs[glyph_index]);
+            offset = GET_BE_WORD(ccsf2->ChainSubClassSet[class]);
+
+            if (offset == 0)
+            {
+                TRACE("No rules for class\n");
+                continue;
+            }
+
+            csc = (const GSUB_ChainSubClassSet*)((BYTE*)ccsf2+offset);
+            count = GET_BE_WORD(csc->ChainSubClassRuleCnt);
+
+            TRACE("%i rules to check\n",count);
+
+            for (i = 0; i < count; i++)
+            {
+                WORD backtrack_count, input_count, lookahead_count, substitute_count;
+                int k;
+                const GSUB_ChainSubClassRule_1 *backtrack;
+                const GSUB_ChainSubClassRule_2 *input;
+                const GSUB_ChainSubClassRule_3 *lookahead;
+                const GSUB_ChainSubClassRule_4 *substitute;
+                int new_index = GSUB_E_NOGLYPH;
+
+                offset = GET_BE_WORD(csc->ChainSubClassRule[i]);
+                backtrack = (const GSUB_ChainSubClassRule_1 *)((BYTE *)csc + offset);
+                backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
+                k = glyph_index + dirBacktrack * backtrack_count;
+                if (k < 0 || k >= *glyph_count)
+                    continue;
+
+                input = (const GSUB_ChainSubClassRule_2 *)&backtrack->Backtrack[backtrack_count];
+                input_count = GET_BE_WORD(input->InputGlyphCount) - 1;
+                k = glyph_index + write_dir * input_count;
+                if (k < 0 || k >= *glyph_count)
+                    continue;
+
+                lookahead = (const GSUB_ChainSubClassRule_3 *)&input->Input[input_count];
+                lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
+                k = glyph_index + dirLookahead * (input_count + lookahead_count);
+                if (k < 0 || k >= *glyph_count)
+                    continue;
+
+                substitute = (const GSUB_ChainSubClassRule_4 *)&lookahead->LookAhead[lookahead_count];
+
+                for (k = 0; k < backtrack_count; ++k)
+                {
+                    WORD target_class = GET_BE_WORD(backtrack->Backtrack[k]);
+                    WORD glyph_class = OT_get_glyph_class(backtrack_class_table, glyphs[glyph_index + (dirBacktrack * (k+1))]);
+                    if (target_class != glyph_class)
+                        break;
+                }
+                if (k != backtrack_count)
+                    continue;
+                TRACE("Matched Backtrack\n");
+
+                for (k = 0; k < input_count; ++k)
+                {
+                    WORD target_class = GET_BE_WORD(input->Input[k]);
+                    WORD glyph_class = OT_get_glyph_class(input_class_table, glyphs[glyph_index + (write_dir * (k+1))]);
+                    if (target_class != glyph_class)
+                        break;
+                }
+                if (k != input_count)
+                    continue;
+                TRACE("Matched IndexGlyphs\n");
+
+                for (k = 0; k < lookahead_count; ++k)
+                {
+                    WORD target_class = GET_BE_WORD(lookahead->LookAhead[k]);
+                    WORD glyph_class = OT_get_glyph_class(lookahead_class_table,
+                            glyphs[glyph_index + (dirLookahead * (input_count + k + 1))]);
+                    if (target_class != glyph_class)
+                        break;
+                }
+                if (k != lookahead_count)
+                    continue;
+                TRACE("Matched LookAhead\n");
+
+                substitute_count = GET_BE_WORD(substitute->SubstCount);
+                for (k = 0; k < substitute_count; ++k)
+                {
+                    unsigned int lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
+                    unsigned int sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex);
+                    unsigned int g = glyph_index + write_dir * sequence_index;
+
+                    if (g >= *glyph_count)
+                    {
+                        WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
+                                sequence_index, glyph_index, write_dir);
+                        continue;
+                    }
+
+                    TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
+                    new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
+                    if (new_index == GSUB_E_NOGLYPH)
+                        ERR("Chain failed to generate a glyph.\n");
+                }
+                return new_index;
+            }
         }
         else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
         {
+            WORD backtrack_count, input_count, lookahead_count, substitution_count;
             int k;
-            int indexGlyphs;
-            const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
-            const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
-            const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
-            const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
-            int newIndex = glyph_index;
-
-            ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
+            const GSUB_ChainContextSubstFormat3_1 *backtrack;
+            const GSUB_ChainContextSubstFormat3_2 *input;
+            const GSUB_ChainContextSubstFormat3_3 *lookahead;
+            const GSUB_ChainContextSubstFormat3_4 *substitute;
+            int new_index = GSUB_E_NOGLYPH;
 
             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
 
-            for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
+            backtrack = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
+            backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
+            k = glyph_index + dirBacktrack * backtrack_count;
+            if (k < 0 || k >= *glyph_count)
+                continue;
+
+            input = (const GSUB_ChainContextSubstFormat3_2 *)&backtrack->Coverage[backtrack_count];
+            input_count = GET_BE_WORD(input->InputGlyphCount);
+            k = glyph_index + write_dir * (input_count - 1);
+            if (k < 0 || k >= *glyph_count)
+                continue;
+
+            lookahead = (const GSUB_ChainContextSubstFormat3_3 *)&input->Coverage[input_count];
+            lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
+            k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
+            if (k < 0 || k >= *glyph_count)
+                continue;
+
+            substitute = (const GSUB_ChainContextSubstFormat3_4 *)&lookahead->Coverage[lookahead_count];
+
+            for (k = 0; k < backtrack_count; ++k)
             {
-                offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
+                offset = GET_BE_WORD(backtrack->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
+                        glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
                     break;
             }
-            if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
+            if (k != backtrack_count)
                 continue;
             TRACE("Matched Backtrack\n");
 
-            ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
-                    FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
-
-            indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
-            for (k = 0; k < indexGlyphs; k++)
+            for (k = 0; k < input_count; ++k)
             {
-                offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
+                offset = GET_BE_WORD(input->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
+                        glyphs[glyph_index + (write_dir * k)]) == -1)
                     break;
             }
-            if (k != indexGlyphs)
+            if (k != input_count)
                 continue;
             TRACE("Matched IndexGlyphs\n");
 
-            ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
-                    FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
-
-            for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
+            for (k = 0; k < lookahead_count; ++k)
             {
-                offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
+                offset = GET_BE_WORD(lookahead->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
+                        glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
                     break;
             }
-            if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
+            if (k != lookahead_count)
                 continue;
             TRACE("Matched LookAhead\n");
 
-            ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
-                    FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
-
-            if (GET_BE_WORD(ccsf3_4->SubstCount))
+            substitution_count = GET_BE_WORD(substitute->SubstCount);
+            for (k = 0; k < substitution_count; ++k)
             {
-                for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
-                {
-                    int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
-                    int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
+                unsigned int lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
+                unsigned int sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex);
+                unsigned int g = glyph_index + write_dir * sequence_index;
 
-                    TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
-                    newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
-                    if (newIndex == -1)
-                    {
-                        ERR("Chain failed to generate a glyph\n");
-                        continue;
-                    }
+                if (g >= *glyph_count)
+                {
+                    WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
+                            sequence_index, glyph_index, write_dir);
+                    continue;
                 }
-                return newIndex;
+
+                TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
+                new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
+                if (new_index == GSUB_E_NOGLYPH)
+                    ERR("Chain failed to generate a glyph.\n");
             }
-            else return GSUB_E_NOGLYPH;
+            return new_index;
         }
     }
-    return -1;
+    return GSUB_E_NOGLYPH;
 }
 
 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
 {
     int offset;
+    enum gsub_lookup_type type;
     const OT_LookupTable *look;
 
     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
-    TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
-    switch(GET_BE_WORD(look->LookupType))
+    type = GET_BE_WORD(look->LookupType);
+    TRACE("type %#x, flag %#x, subtables %u.\n", type,
+            GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
+
+    if (type == GSUB_LOOKUP_EXTENSION)
     {
-        case 1:
+        if (GET_BE_WORD(look->SubTableCount))
+        {
+            const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
+            if (GET_BE_WORD(ext->SubstFormat) == 1)
+            {
+                type = GET_BE_WORD(ext->ExtensionLookupType);
+                TRACE("extension type %i\n",type);
+            }
+            else
+            {
+                FIXME("Unhandled Extension Substitution Format %i\n",GET_BE_WORD(ext->SubstFormat));
+            }
+        }
+        else
+        {
+            WARN("lookup type is Extension Substitution but no extension subtable exists\n");
+        }
+    }
+    switch(type)
+    {
+        case GSUB_LOOKUP_SINGLE:
             return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 2:
+        case GSUB_LOOKUP_MULTIPLE:
             return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 3:
+        case GSUB_LOOKUP_ALTERNATE:
             return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 4:
+        case GSUB_LOOKUP_LIGATURE:
             return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 6:
+        case GSUB_LOOKUP_CONTEXT:
+            return GSUB_apply_ContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
+        case GSUB_LOOKUP_CONTEXT_CHAINED:
             return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
+        case GSUB_LOOKUP_EXTENSION:
+            FIXME("Extension Substitution types not valid here\n");
+            break;
         default:
-            FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
+            FIXME("Unhandled GSUB lookup type %#x.\n", type);
     }
     return GSUB_E_NOGLYPH;
 }
 
-INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+int OpenType_apply_GSUB_lookup(const void *table, unsigned int lookup_index, WORD *glyphs,
+        unsigned int glyph_index, int write_dir, int *glyph_count)
 {
     const GSUB_Header *header = (const GSUB_Header *)table;
     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
@@ -1052,18 +1549,28 @@ INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, IN
 /**********
  * GPOS
  **********/
-static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
-                             const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset);
+static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
+        const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
+        unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
+        GOFFSET *goffset);
 
 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
 {
     static const WORD mask[3] = {3,0xf,0xff};
     if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem  <= GET_BE_WORD(DeviceTable->EndSize))
     {
-        int format = GET_BE_WORD(DeviceTable->DeltaFormat);
+        WORD format = GET_BE_WORD(DeviceTable->DeltaFormat);
         int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
         int value;
-        TRACE("device table, format %i, index %i\n",format, index);
+
+        TRACE("device table, format %#x, index %i\n", format, index);
+
+        if (format < 1 || format > 3)
+        {
+            WARN("invalid delta format %#x\n", format);
+            return 0;
+        }
+
         index = index << format;
         value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
         TRACE("offset %i, value %i\n",index, value);
@@ -1074,7 +1581,7 @@ static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD p
     return 0;
 }
 
-static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
+static void GPOS_get_anchor_values(const void *table, POINT *pt, WORD ppem)
 {
     const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
 
@@ -1126,15 +1633,16 @@ static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
     }
 }
 
-static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
+static void GPOS_convert_design_units_to_device(const OUTLINETEXTMETRICW *otm, const LOGFONTW *logfont,
+        int desX, int desY, double *devX, double *devY)
 {
-    int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
+    int emHeight = otm->otmTextMetrics.tmAscent + otm->otmTextMetrics.tmDescent - otm->otmTextMetrics.tmInternalLeading;
 
-    TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
-    *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
-    *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
-    if (lplogfont->lfWidth)
-        FIXME("Font with lfWidth set no handled properly\n");
+    TRACE("emHeight %i lfWidth %i\n",emHeight, logfont->lfWidth);
+    *devX = (desX * emHeight) / (double)otm->otmEMSquare;
+    *devY = (desY * emHeight) / (double)otm->otmEMSquare;
+    if (logfont->lfWidth)
+        FIXME("Font with lfWidth set not handled properly.\n");
 }
 
 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
@@ -1151,7 +1659,8 @@ static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_Value
     return offset;
 }
 
-static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord,  WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
+static void GPOS_get_value_record_offsets(const BYTE *head, GPOS_ValueRecord *ValueRecord,
+        WORD ValueFormat, unsigned int ppem, POINT *ptPlacement, POINT *ptAdvance)
 {
     if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
     if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
@@ -1168,7 +1677,7 @@ static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
 {
     int offset = GET_BE_WORD(look->SubTable[index]);
 
-    if (GET_BE_WORD(look->LookupType) == 9)
+    if (GET_BE_WORD(look->LookupType) == GPOS_LOOKUP_POSITION_EXTENSION)
     {
         const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + offset);
         if (GET_BE_WORD(ext->PosFormat) == 1)
@@ -1183,8 +1692,9 @@ static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
     return (const BYTE *)look + offset;
 }
 
-static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
-                                        INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
+static void GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
+        POINT *adjust, POINT *advance)
 {
     int j;
 
@@ -1202,7 +1712,7 @@ static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT
                 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
                 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
                 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
-                GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
+                GPOS_get_value_record_offsets((const BYTE *)spf1, &ValueRecord, ValueFormat, ppem, adjust, advance);
                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
             }
         }
@@ -1224,7 +1734,7 @@ static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT
                     offset = size * index;
                     GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
                 }
-                GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
+                GPOS_get_value_record_offsets((const BYTE *)spf2, &ValueRecord, ValueFormat, ppem, adjust, advance);
                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
             }
         }
@@ -1246,24 +1756,26 @@ static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt
     if (val_fmt1)
     {
         GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
-        TRACE( "Glyph 1 resulting cumulative offset is %i,%i design units\n", adjust[0].x, adjust[0].y );
-        TRACE( "Glyph 1 resulting cumulative advance is %i,%i design units\n", advance[0].x, advance[0].y );
+        TRACE( "Glyph 1 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[0]) );
+        TRACE( "Glyph 1 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[0]) );
     }
     if (val_fmt2)
     {
         GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
-        TRACE( "Glyph 2 resulting cumulative offset is %i,%i design units\n", adjust[1].x, adjust[1].y );
-        TRACE( "Glyph 2 resulting cumulative advance is %i,%i design units\n", advance[1].x, advance[1].y );
+        TRACE( "Glyph 2 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[1]) );
+        TRACE( "Glyph 2 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[1]) );
     }
 }
 
-static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
-                                     INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
+static int GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
+        POINT *adjust, POINT *advance)
 {
     int j;
     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
 
-    if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return glyph_index + 1;
+    if (glyph_index + write_dir >= glyph_count)
+        return 1;
 
     TRACE("Pair Adjustment Positioning Subtable\n");
 
@@ -1297,9 +1809,10 @@ static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_AN
                     {
                         int next = 1;
                         TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
-                        apply_pair_value( ppf1, ValueFormat1, ValueFormat2, pair_val_rec->Value1, ppem, ptAdjust, ptAdvance );
+                        apply_pair_value(ppf1, ValueFormat1, ValueFormat2,
+                                pair_val_rec->Value1, ppem, adjust, advance);
                         if (ValueFormat2) next++;
-                        return glyph_index + next;
+                        return next;
                     }
                     pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
                 }
@@ -1330,25 +1843,26 @@ static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_AN
 
                     TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
 
-                    apply_pair_value( ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, ptAdjust, ptAdvance );
+                    apply_pair_value(ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, adjust, advance);
                     if (ValueFormat2) next++;
-                    return glyph_index + next;
+                    return next;
                 }
             }
         }
         else
             FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
     }
-    return glyph_index+1;
+    return 1;
 }
 
-static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
-                                     INT glyph_count, INT ppem, LPPOINT pt)
+static void GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
 {
     int j;
     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
 
-    if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return;
+    if (glyph_index + write_dir >= glyph_count)
+        return;
 
     TRACE("Cursive Attachment Positioning Subtable\n");
 
@@ -1370,7 +1884,7 @@ static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIP
                     GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
                     offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
                     GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
-                    TRACE("Found linkage %x[%i,%i] %x[%i,%i]\n",glyphs[glyph_index], exit_pt.x,exit_pt.y, glyphs[glyph_index+write_dir], entry_pt.x, entry_pt.y);
+                    TRACE("Found linkage %x[%s] %x[%s]\n",glyphs[glyph_index], wine_dbgstr_point(&exit_pt), glyphs[glyph_index+write_dir], wine_dbgstr_point(&entry_pt));
                     pt->x = entry_pt.x - exit_pt.x;
                     pt->y = entry_pt.y - exit_pt.y;
                     return;
@@ -1383,19 +1897,21 @@ static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIP
     return;
 }
 
-static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index, INT glyph_count, INT ppem, LPPOINT pt)
+static int GPOS_apply_MarkToBase(const ScriptCache *script_cache, const OT_LookupTable *look,
+        const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, unsigned int glyph_index,
+        unsigned int glyph_count, unsigned int ppem, POINT *pt)
 {
     int j;
     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
-    void *glyph_class_table = NULL;
+    const void *glyph_class_table = NULL;
     int rc = -1;
 
-    if (psc->GDEF_Table)
+    if (script_cache->GDEF_Table)
     {
-        const GDEF_Header *header = psc->GDEF_Table;
+        const GDEF_Header *header = script_cache->GDEF_Table;
         WORD offset = GET_BE_WORD( header->GlyphClassDef );
         if (offset)
-            glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
+            glyph_class_table = (const BYTE *)script_cache->GDEF_Table + offset;
     }
 
     TRACE("MarkToBase Attachment Positioning Subtable\n");
@@ -1437,7 +1953,7 @@ static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, c
                     ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
                     if (mark_index > GET_BE_WORD(ma->MarkCount))
                     {
-                        ERR("Mark index exeeded mark count\n");
+                        ERR("Mark index exceeded mark count\n");
                         return -1;
                     }
                     mr = &ma->MarkRecord[mark_index];
@@ -1451,11 +1967,11 @@ static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, c
                     GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
                     offset = GET_BE_WORD(mr->MarkAnchor);
                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
-                    TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
-                    TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
+                    TRACE("Offset on base is %s design units\n",wine_dbgstr_point(&base_pt));
+                    TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
                     pt->x += base_pt.x - mark_pt.x;
                     pt->y += base_pt.y - mark_pt.y;
-                    TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
+                    TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
                     rc = base_glyph;
                 }
             }
@@ -1466,8 +1982,8 @@ static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, c
     return rc;
 }
 
-static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
-                                      INT glyph_count, INT ppem, LPPOINT pt)
+static void GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
 {
     int j;
     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
@@ -1507,7 +2023,7 @@ static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_A
                     ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
                     if (mark_index > GET_BE_WORD(ma->MarkCount))
                     {
-                        ERR("Mark index exeeded mark count\n");
+                        ERR("Mark index exceeded mark count\n");
                         return;
                     }
                     mr = &ma->MarkRecord[mark_index];
@@ -1517,7 +2033,7 @@ static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_A
                     la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
                     if (ligature_index > GET_BE_WORD(la->LigatureCount))
                     {
-                        ERR("Ligature index exeeded ligature count\n");
+                        ERR("Ligature index exceeded ligature count\n");
                         return;
                     }
                     offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
@@ -1536,18 +2052,18 @@ static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_A
                     }
                     if (!offset)
                     {
-                        ERR("Failed to find avalible ligature connection point\n");
+                        ERR("Failed to find available ligature connection point\n");
                         return;
                     }
 
                     GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
                     offset = GET_BE_WORD(mr->MarkAnchor);
                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
-                    TRACE("Offset on ligature is %i,%i design units\n",ligature_pt.x,ligature_pt.y);
-                    TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
+                    TRACE("Offset on ligature is %s design units\n",wine_dbgstr_point(&ligature_pt));
+                    TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
                     pt->x += ligature_pt.x - mark_pt.x;
                     pt->y += ligature_pt.y - mark_pt.y;
-                    TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
+                    TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
                 }
             }
         }
@@ -1556,8 +2072,8 @@ static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_A
     }
 }
 
-static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
-                                  INT glyph_count, INT ppem, LPPOINT pt)
+static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
 {
     int j;
     BOOL rc = FALSE;
@@ -1608,11 +2124,11 @@ static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALY
                     GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
                     offset = GET_BE_WORD(mr->MarkAnchor);
                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
-                    TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
-                    TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
+                    TRACE("Offset on mark2 is %s design units\n",wine_dbgstr_point(&mark2_pt));
+                    TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
                     pt->x += mark2_pt.x - mark_pt.x;
                     pt->y += mark2_pt.y - mark_pt.y;
-                    TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
+                    TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
                     rc = TRUE;
                 }
             }
@@ -1623,9 +2139,129 @@ static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALY
     return rc;
 }
 
-static INT GPOS_apply_ChainContextPos(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
-                                      const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index,
-                                      INT glyph_count, INT ppem, GOFFSET *pGoffset)
+static unsigned int GPOS_apply_ContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
+        const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
+        const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
+        GOFFSET *goffset)
+{
+    int j;
+    int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
+
+    TRACE("Contextual Positioning Subtable\n");
+
+    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
+    {
+        const GPOS_ContextPosFormat2 *cpf2 = (GPOS_ContextPosFormat2*)GPOS_get_subtable(look, j);
+
+        if (GET_BE_WORD(cpf2->PosFormat) == 1)
+        {
+            static int once;
+            if (!once++)
+                FIXME("  TODO: subtype 1\n");
+            continue;
+        }
+        else if (GET_BE_WORD(cpf2->PosFormat) == 2)
+        {
+            WORD offset = GET_BE_WORD(cpf2->Coverage);
+            int index;
+
+            TRACE("Contextual Positioning Subtable: Format 2\n");
+
+            index = GSUB_is_glyph_covered((const BYTE*)cpf2+offset, glyphs[glyph_index]);
+            TRACE("Coverage index %i\n",index);
+            if (index != -1)
+            {
+                int k, count, class;
+                const GPOS_PosClassSet *pcs;
+                const void *glyph_class_table = NULL;
+
+                offset = GET_BE_WORD(cpf2->ClassDef);
+                glyph_class_table = (const BYTE *)cpf2 + offset;
+
+                class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
+
+                offset = GET_BE_WORD(cpf2->PosClassSet[class]);
+                if (offset == 0)
+                {
+                    TRACE("No class rule table for class %i\n",class);
+                    continue;
+                }
+                pcs = (const GPOS_PosClassSet*)((const BYTE*)cpf2+offset);
+                count = GET_BE_WORD(pcs->PosClassRuleCnt);
+                TRACE("PosClassSet has %i members\n",count);
+                for (k = 0; k < count; k++)
+                {
+                    const GPOS_PosClassRule_1 *pr;
+                    const GPOS_PosClassRule_2 *pr_2;
+                    unsigned int g;
+                    int g_count, l;
+
+                    offset = GET_BE_WORD(pcs->PosClassRule[k]);
+                    pr = (const GPOS_PosClassRule_1*)((const BYTE*)pcs+offset);
+                    g_count = GET_BE_WORD(pr->GlyphCount);
+                    TRACE("PosClassRule has %i glyphs classes\n",g_count);
+
+                    g = glyph_index + write_dir * (g_count - 1);
+                    if (g >= glyph_count)
+                        continue;
+
+                    for (l = 0; l < g_count-1; l++)
+                    {
+                        int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
+                        if (g_class != GET_BE_WORD(pr->Class[l])) break;
+                    }
+
+                    if (l < g_count-1)
+                    {
+                        TRACE("Rule does not match\n");
+                        continue;
+                    }
+
+                    TRACE("Rule matches\n");
+                    pr_2 = (const GPOS_PosClassRule_2 *)&pr->Class[g_count - 1];
+
+                    for (l = 0; l < GET_BE_WORD(pr->PosCount); l++)
+                    {
+                        unsigned int lookup_index = GET_BE_WORD(pr_2->PosLookupRecord[l].LookupListIndex);
+                        unsigned int sequence_index = GET_BE_WORD(pr_2->PosLookupRecord[l].SequenceIndex);
+
+                        g = glyph_index + write_dir * sequence_index;
+                        if (g >= glyph_count)
+                        {
+                            WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
+                                    sequence_index, glyph_index, write_dir);
+                            continue;
+                        }
+
+                        TRACE("Position: %u -> %u %u.\n", l, sequence_index, lookup_index);
+
+                        GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance,
+                                lookup, lookup_index, glyphs, g, glyph_count, goffset);
+                    }
+                    return 1;
+                }
+            }
+
+            TRACE("Not covered\n");
+            continue;
+        }
+        else if (GET_BE_WORD(cpf2->PosFormat) == 3)
+        {
+            static int once;
+            if (!once++)
+                FIXME("  TODO: subtype 3\n");
+            continue;
+        }
+        else
+            FIXME("Unhandled Contextual Positioning Format %i\n",GET_BE_WORD(cpf2->PosFormat));
+    }
+    return 1;
+}
+
+static unsigned int GPOS_apply_ChainContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
+        const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
+        const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
+        GOFFSET *goffset)
 {
     int j;
     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
@@ -1635,102 +2271,131 @@ static INT GPOS_apply_ChainContextPos(ScriptCache *psc, LPOUTLINETEXTMETRICW lpo
     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
     {
         int offset;
-        const GPOS_ChainContextPosFormat3_1 *ccpf3 = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
+        const GPOS_ChainContextPosFormat3_1 *backtrack = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
         int dirLookahead = write_dir;
         int dirBacktrack = -1 * write_dir;
 
-        if (GET_BE_WORD(ccpf3->PosFormat) == 1)
+        if (GET_BE_WORD(backtrack->PosFormat) == 1)
         {
-            FIXME("  TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
+            static int once;
+            if (!once++)
+                FIXME("  TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
             continue;
         }
-        else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
+        else if (GET_BE_WORD(backtrack->PosFormat) == 2)
         {
-            FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
+            static int once;
+            if (!once++)
+                FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
             continue;
         }
-        else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
+        else if (GET_BE_WORD(backtrack->PosFormat) == 3)
         {
+            WORD backtrack_count, input_count, lookahead_count, positioning_count;
             int k;
-            int indexGlyphs;
-            const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
-            const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
-            const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
+            const GPOS_ChainContextPosFormat3_2 *input;
+            const GPOS_ChainContextPosFormat3_3 *lookahead;
+            const GPOS_ChainContextPosFormat3_4 *positioning;
 
             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
 
-            for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
+            backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
+            k = glyph_index + dirBacktrack * backtrack_count;
+            if (k < 0 || k >= glyph_count)
+                continue;
+
+            input = (const GPOS_ChainContextPosFormat3_2 *)&backtrack->Coverage[backtrack_count];
+            input_count = GET_BE_WORD(input->InputGlyphCount);
+            k = glyph_index + write_dir * (input_count - 1);
+            if (k < 0 || k >= glyph_count)
+                continue;
+
+            lookahead = (const GPOS_ChainContextPosFormat3_3 *)&input->Coverage[input_count];
+            lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
+            k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
+            if (k < 0 || k >= glyph_count)
+                continue;
+
+            positioning = (const GPOS_ChainContextPosFormat3_4 *)&lookahead->Coverage[lookahead_count];
+
+            for (k = 0; k < backtrack_count; ++k)
             {
-                offset = GET_BE_WORD(ccpf3->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
+                offset = GET_BE_WORD(backtrack->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
+                        glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
                     break;
             }
-            if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
+            if (k != backtrack_count)
                 continue;
             TRACE("Matched Backtrack\n");
 
-            ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
-                    FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
-
-            indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
-            for (k = 0; k < indexGlyphs; k++)
+            for (k = 0; k < input_count; ++k)
             {
-                offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
+                offset = GET_BE_WORD(input->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
+                        glyphs[glyph_index + (write_dir * k)]) == -1)
                     break;
             }
-            if (k != indexGlyphs)
+            if (k != input_count)
                 continue;
             TRACE("Matched IndexGlyphs\n");
 
-            ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
-                    FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
-
-            for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
+            for (k = 0; k < lookahead_count; ++k)
             {
-                offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
+                offset = GET_BE_WORD(lookahead->Coverage[k]);
+                if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
+                        glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
                     break;
             }
-            if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
+            if (k != lookahead_count)
                 continue;
             TRACE("Matched LookAhead\n");
 
-            ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
-                    FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
+            if (!(positioning_count = GET_BE_WORD(positioning->PosCount)))
+                return 1;
 
-            if (GET_BE_WORD(ccpf3_4->PosCount))
+            for (k = 0; k < positioning_count; ++k)
             {
-                for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
-                {
-                    int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
-                    int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
+                unsigned int lookup_index = GET_BE_WORD(positioning->PosLookupRecord[k].LookupListIndex);
+                unsigned int sequence_index = GET_BE_WORD(positioning->PosLookupRecord[k].SequenceIndex);
+                unsigned int g = glyph_index + write_dir * sequence_index;
 
-                    TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
-                    GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, glyph_count, pGoffset);
+                if (g >= glyph_count)
+                {
+                    WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
+                            sequence_index, glyph_index, write_dir);
+                    continue;
                 }
-                return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
+
+                TRACE("Position: %u -> %u %u.\n", k, sequence_index, lookup_index);
+                GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup, lookup_index,
+                        glyphs, g, glyph_count, goffset);
             }
-            else return glyph_index + 1;
+            return input_count + lookahead_count;
         }
         else
-            FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
+            FIXME("Unhandled Chaining Contextual Positioning Format %#x.\n", GET_BE_WORD(backtrack->PosFormat));
     }
-    return glyph_index + 1;
+    return 1;
 }
 
-static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
+static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *lpotm,
+        const LOGFONTW *lplogfont, const SCRIPT_ANALYSIS *analysis, int *piAdvance, const OT_LookupList *lookup,
+        unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
+        GOFFSET *pGoffset)
 {
     int offset;
     const OT_LookupTable *look;
     int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
-    WORD type;
+    enum gpos_lookup_type type;
 
     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
     type = GET_BE_WORD(look->LookupType);
-    TRACE("type %i, flag %x, subtables %i\n",type,GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
-    if (type == 9)
+    TRACE("type %#x, flag %#x, subtables %u.\n", type,
+            GET_BE_WORD(look->LookupFlag), GET_BE_WORD(look->SubTableCount));
+
+    if (type == GPOS_LOOKUP_POSITION_EXTENSION)
     {
         if (GET_BE_WORD(look->SubTableCount))
         {
@@ -1752,7 +2417,7 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
     }
     switch (type)
     {
-        case 1:
+        case GPOS_LOOKUP_ADJUST_SINGLE:
         {
             double devX, devY;
             POINT adjust = {0,0};
@@ -1773,16 +2438,18 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
             }
             break;
         }
-        case 2:
+
+        case GPOS_LOOKUP_ADJUST_PAIR:
         {
             POINT advance[2]= {{0,0},{0,0}};
             POINT adjust[2]= {{0,0},{0,0}};
             double devX, devY;
-            int index;
+            int index_offset;
             int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
             int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
 
-            index = GPOS_apply_PairAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, adjust, advance);
+            index_offset = GPOS_apply_PairAdjustment(look, analysis, glyphs,
+                    glyph_index, glyph_count, ppem, adjust, advance);
             if (adjust[0].x || adjust[0].y)
             {
                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
@@ -1805,9 +2472,10 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
                 piAdvance[glyph_index + write_dir] += round(devX);
             }
-            return index;
+            return index_offset;
         }
-        case 3:
+
+        case GPOS_LOOKUP_ATTACH_CURSIVE:
         {
             POINT desU = {0,0};
             double devX, devY;
@@ -1822,11 +2490,13 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
             }
             break;
         }
-        case 4:
+
+        case GPOS_LOOKUP_ATTACH_MARK_TO_BASE:
         {
             double devX, devY;
             POINT desU = {0,0};
-            int base_index = GPOS_apply_MarkToBase(psc, look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
+            int base_index = GPOS_apply_MarkToBase(script_cache, look, analysis,
+                    glyphs, glyph_index, glyph_count, ppem, &desU);
             if (base_index != -1)
             {
                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
@@ -1840,7 +2510,8 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
             }
             break;
         }
-        case 5:
+
+        case GPOS_LOOKUP_ATTACH_MARK_TO_LIGATURE:
         {
             double devX, devY;
             POINT desU = {0,0};
@@ -1853,7 +2524,8 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
             }
             break;
         }
-        case 6:
+
+        case GPOS_LOOKUP_ATTACH_MARK_TO_MARK:
         {
             double devX, devY;
             POINT desU = {0,0};
@@ -1867,22 +2539,43 @@ static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOG
             }
             break;
         }
-        case 8:
-        {
-            return GPOS_apply_ChainContextPos(psc, lpotm, lplogfont, analysis, piAdvance, lookup, look, glyphs, glyph_index, glyph_count, ppem, pGoffset);
-        }
+
+        case GPOS_LOOKUP_POSITION_CONTEXT:
+            return GPOS_apply_ContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
+                    lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
+
+        case GPOS_LOOKUP_POSITION_CONTEXT_CHAINED:
+            return GPOS_apply_ChainContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
+                    lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
+
         default:
-            FIXME("We do not handle SubType %i\n",type);
+            FIXME("Unhandled GPOS lookup type %#x.\n", type);
     }
-    return glyph_index+1;
+    return 1;
 }
 
-INT OpenType_apply_GPOS_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
+unsigned int OpenType_apply_GPOS_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
+        const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, unsigned int lookup_index,
+        const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, GOFFSET *goffset)
 {
-    const GPOS_Header *header = (const GPOS_Header *)psc->GPOS_Table;
+    const GPOS_Header *header = (const GPOS_Header *)script_cache->GPOS_Table;
     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
 
-    return GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookup_index, glyphs, glyph_index, glyph_count, pGoffset);
+    return GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup,
+            lookup_index, glyphs, glyph_index, glyph_count, goffset);
+}
+
+static LoadedScript *usp10_script_cache_get_script(ScriptCache *script_cache, OPENTYPE_TAG tag)
+{
+    size_t i;
+
+    for (i = 0; i < script_cache->script_count; ++i)
+    {
+        if (script_cache->scripts[i].tag == tag)
+            return &script_cache->scripts[i];
+    }
+
+    return NULL;
 }
 
 static void GSUB_initialize_script_cache(ScriptCache *psc)
@@ -1895,10 +2588,10 @@ static void GSUB_initialize_script_cache(ScriptCache *psc)
         const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
         script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
         psc->script_count = GET_BE_WORD(script->ScriptCount);
-        TRACE("initializing %i scripts in this font\n",psc->script_count);
+        TRACE("initializing %li scripts in this font\n",psc->script_count);
         if (psc->script_count)
         {
-            psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
+            psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
             for (i = 0; i < psc->script_count; i++)
             {
                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
@@ -1914,6 +2607,7 @@ static void GPOS_expand_script_cache(ScriptCache *psc)
     int i, count;
     const OT_ScriptList *script;
     const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
+    LoadedScript *loaded_script;
 
     if (!header)
         return;
@@ -1927,10 +2621,10 @@ static void GPOS_expand_script_cache(ScriptCache *psc)
     if (!psc->script_count)
     {
         psc->script_count = count;
-        TRACE("initializing %i scripts in this font\n",psc->script_count);
+        TRACE("initializing %li scripts in this font\n",psc->script_count);
         if (psc->script_count)
         {
-            psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
+            psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
             for (i = 0; i < psc->script_count; i++)
             {
                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
@@ -1943,24 +2637,23 @@ static void GPOS_expand_script_cache(ScriptCache *psc)
     {
         for (i = 0; i < count; i++)
         {
-            int j;
             int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
             OPENTYPE_TAG tag = MS_MAKE_TAG(script->ScriptRecord[i].ScriptTag[0], script->ScriptRecord[i].ScriptTag[1], script->ScriptRecord[i].ScriptTag[2], script->ScriptRecord[i].ScriptTag[3]);
-            for (j = 0; j < psc->script_count; j++)
+
+            if (!(loaded_script = usp10_script_cache_get_script(psc, tag)))
             {
-                if (psc->scripts[j].tag == tag)
+                if (!usp10_array_reserve((void **)&psc->scripts, &psc->scripts_size,
+                        psc->script_count + 1, sizeof(*psc->scripts)))
                 {
-                    psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
-                    break;
+                    ERR("Failed grow scripts array.\n");
+                    return;
                 }
+
+                loaded_script = &psc->scripts[psc->script_count];
+                ++psc->script_count;
+                loaded_script->tag = tag;
             }
-            if (j == psc->script_count)
-            {
-                psc->script_count++;
-                psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
-                psc->scripts[j].tag = tag;
-                psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
-            }
+            loaded_script->gpos_table = (const BYTE *)script + offset;
         }
     }
 }
@@ -1978,36 +2671,47 @@ static void _initialize_script_cache(ScriptCache *psc)
 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
 {
     int i;
+    const LoadedScript *script;
     HRESULT rc = S_OK;
 
     _initialize_script_cache(psc);
 
     *pcTags = psc->script_count;
 
-    if (!searchingFor && cMaxTags < *pcTags)
+    if (searchingFor)
+    {
+        if (!(script = usp10_script_cache_get_script(psc, searchingFor)))
+            return USP_E_SCRIPT_NOT_IN_FONT;
+
+        *pScriptTags = script->tag;
+        *pcTags = 1;
+        return S_OK;
+    }
+
+    if (cMaxTags < *pcTags)
         rc = E_OUTOFMEMORY;
-    else if (searchingFor)
-        rc = USP_E_SCRIPT_NOT_IN_FONT;
 
-    for (i = 0; i < psc->script_count; i++)
+    cMaxTags = min(cMaxTags, psc->script_count);
+    for (i = 0; i < cMaxTags; ++i)
     {
-        if (i < cMaxTags)
-            pScriptTags[i] = psc->scripts[i].tag;
-
-        if (searchingFor)
-        {
-            if (searchingFor == psc->scripts[i].tag)
-            {
-                pScriptTags[0] = psc->scripts[i].tag;
-                *pcTags = 1;
-                rc = S_OK;
-                break;
-            }
-        }
+        pScriptTags[i] = psc->scripts[i].tag;
     }
     return rc;
 }
 
+static LoadedLanguage *usp10_script_get_language(LoadedScript *script, OPENTYPE_TAG tag)
+{
+    size_t i;
+
+    for (i = 0; i < script->language_count; ++i)
+    {
+        if (script->languages[i].tag == tag)
+            return &script->languages[i];
+    }
+
+    return NULL;
+}
+
 static void GSUB_initialize_language_cache(LoadedScript *script)
 {
     int i;
@@ -2026,9 +2730,9 @@ static void GSUB_initialize_language_cache(LoadedScript *script)
 
         if (script->language_count)
         {
-            TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
+            TRACE("Deflang %p, LangCount %li\n",script->default_language.gsub_table, script->language_count);
 
-            script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
+            script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
 
             for (i = 0; i < script->language_count; i++)
             {
@@ -2044,6 +2748,7 @@ static void GPOS_expand_language_cache(LoadedScript *script)
 {
     int count;
     const OT_Script* table = script->gpos_table;
+    LoadedLanguage *language;
     DWORD offset;
 
     if (!table)
@@ -2065,7 +2770,7 @@ static void GPOS_expand_language_cache(LoadedScript *script)
         int i;
         script->language_count = count;
 
-        script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
+        script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
 
         for (i = 0; i < script->language_count; i++)
         {
@@ -2076,27 +2781,26 @@ static void GPOS_expand_language_cache(LoadedScript *script)
     }
     else if (count)
     {
-        int i,j;
+        int i;
         for (i = 0; i < count; i++)
         {
             int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
             OPENTYPE_TAG tag = MS_MAKE_TAG(table->LangSysRecord[i].LangSysTag[0], table->LangSysRecord[i].LangSysTag[1], table->LangSysRecord[i].LangSysTag[2], table->LangSysRecord[i].LangSysTag[3]);
 
-            for (j = 0; j < script->language_count; j++)
+            if (!(language = usp10_script_get_language(script, tag)))
             {
-                if (script->languages[j].tag == tag)
+                if (!usp10_array_reserve((void **)&script->languages, &script->languages_size,
+                        script->language_count + 1, sizeof(*script->languages)))
                 {
-                    script->languages[j].gpos_table = ((const BYTE*)table + offset);
-                    break;
+                    ERR("Failed grow languages array.\n");
+                    return;
                 }
+
+                language = &script->languages[script->language_count];
+                ++script->language_count;
+                language->tag = tag;
             }
-            if (j == script->language_count)
-            {
-                script->language_count++;
-                script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
-                script->languages[j].tag = tag;
-                script->languages[j].gpos_table = ((const BYTE*)table + offset);
-            }
+            language->gpos_table = (const BYTE *)table + offset;
         }
     }
 }
@@ -2118,17 +2822,7 @@ HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag,
     LoadedScript *script = NULL;
 
     _initialize_script_cache(psc);
-
-    for (i = 0; i < psc->script_count; i++)
-    {
-         if (psc->scripts[i].tag == script_tag)
-         {
-            script = &psc->scripts[i];
-            break;
-         }
-    }
-
-    if (!script)
+    if (!(script = usp10_script_cache_get_script(psc, script_tag)))
         return E_INVALIDARG;
 
     _initialize_language_cache(script);
@@ -2173,141 +2867,79 @@ HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag,
     return rc;
 }
 
-
-static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
+static void usp10_language_add_feature_list(LoadedLanguage *language, char table_type,
+        const OT_LangSys *lang, const OT_FeatureList *feature_list)
 {
-    int i;
-
-    if (language->gsub_table)
-    {
-        const OT_LangSys *lang = language->gsub_table;
-        const GSUB_Header *header = (const GSUB_Header *)table;
-        const OT_FeatureList *feature_list;
+    unsigned int count = GET_BE_WORD(lang->FeatureCount);
+    unsigned int i, j;
 
-        language->feature_count = GET_BE_WORD(lang->FeatureCount);
-        TRACE("%i features\n",language->feature_count);
+    TRACE("table_type %#x, %u features.\n", table_type, count);
 
-        if (language->feature_count)
-        {
-            language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
+    if (!count)
+        return;
 
-            feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
+    if (!language->feature_count)
+        language->features = heap_alloc(count * sizeof(*language->features));
+    else
+        language->features = HeapReAlloc(GetProcessHeap(), 0, language->features,
+                (language->feature_count + count) * sizeof(*language->features));
 
-            for (i = 0; i < language->feature_count; i++)
-            {
-                const OT_Feature *feature;
-                int j;
-                int index = GET_BE_WORD(lang->FeatureIndex[i]);
-
-                language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
-                language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
-                feature = (const OT_Feature*)language->features[i].feature;
-                language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
-                language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
-                for (j = 0; j < language->features[i].lookup_count; j++)
-                    language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
-                language->features[i].tableType = FEATURE_GSUB_TABLE;
-            }
-        }
+    for (i = 0; i < count; ++i)
+    {
+        const OT_FeatureRecord *record;
+        LoadedFeature *loaded_feature;
+        const OT_Feature *feature;
+
+        record = &feature_list->FeatureRecord[GET_BE_WORD(lang->FeatureIndex[i])];
+        feature = (const OT_Feature *)((const BYTE *)feature_list + GET_BE_WORD(record->Feature));
+
+        loaded_feature = &language->features[language->feature_count + i];
+        loaded_feature->tag = MS_MAKE_TAG(record->FeatureTag[0], record->FeatureTag[1],
+                record->FeatureTag[2], record->FeatureTag[3]);
+        loaded_feature->tableType = table_type;
+        loaded_feature->feature = feature;
+        loaded_feature->lookup_count = GET_BE_WORD(feature->LookupCount);
+        loaded_feature->lookups = heap_alloc(loaded_feature->lookup_count * sizeof(*loaded_feature->lookups));
+        for (j = 0; j < loaded_feature->lookup_count; ++j)
+            loaded_feature->lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
     }
+    language->feature_count += count;
 }
 
-static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
+static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
 {
-    int i, count;
-    const OT_LangSys *lang = language->gpos_table;
-    const GPOS_Header *header = (const GPOS_Header *)table;
+    const GSUB_Header *gsub_header = psc->GSUB_Table;
+    const GPOS_Header *gpos_header = psc->GPOS_Table;
     const OT_FeatureList *feature_list;
+    const OT_LangSys *lang;
 
-    if (!lang)
-        return;
-
-    count = GET_BE_WORD(lang->FeatureCount);
-    feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
-
-    TRACE("%i features\n",count);
-
-    if (!count)
+    if (language->features_initialized)
         return;
 
-    if (!language->feature_count)
+    if ((lang = language->gsub_table))
     {
-        language->feature_count = count;
-
-        if (language->feature_count)
-        {
-            language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
-
-            for (i = 0; i < language->feature_count; i++)
-            {
-                const OT_Feature *feature;
-                int j;
-                int index = GET_BE_WORD(lang->FeatureIndex[i]);
-
-                language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
-                language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
-                feature = (const OT_Feature*)language->features[i].feature;
-                language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
-                language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
-                for (j = 0; j < language->features[i].lookup_count; j++)
-                    language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
-                language->features[i].tableType = FEATURE_GPOS_TABLE;
-            }
-        }
+        feature_list = (const OT_FeatureList *)((const BYTE *)gsub_header + GET_BE_WORD(gsub_header->FeatureList));
+        usp10_language_add_feature_list(language, FEATURE_GSUB_TABLE, lang, feature_list);
     }
-    else
-    {
-        language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
 
-        for (i = 0; i < count; i++)
-        {
-            const OT_Feature *feature;
-            int j;
-            int index = GET_BE_WORD(lang->FeatureIndex[i]);
-            int idx = language->feature_count + i;
-
-            language->features[idx].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
-            language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
-            feature = (const OT_Feature*)language->features[idx].feature;
-            language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
-            language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
-            for (j = 0; j < language->features[idx].lookup_count; j++)
-                language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
-            language->features[idx].tableType = FEATURE_GPOS_TABLE;
-        }
-        language->feature_count += count;
-    }
-}
-
-static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
-{
-    if (!language->features_initialized)
+    if ((lang = language->gpos_table))
     {
-        GSUB_initialize_feature_cache(psc->GSUB_Table, language);
-        GPOS_expand_feature_cache(psc->GPOS_Table, language);
-        language->features_initialized = TRUE;
+        feature_list = (const OT_FeatureList *)((const BYTE *)gpos_header + GET_BE_WORD(gpos_header->FeatureList));
+        usp10_language_add_feature_list(language, FEATURE_GPOS_TABLE, lang, feature_list);
     }
+
+    language->features_initialized = TRUE;
 }
 
 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, char tableType, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
 {
     int i;
+    LoadedScript *script;
     HRESULT rc = S_OK;
-    LoadedScript *script = NULL;
     LoadedLanguage *language = NULL;
 
     _initialize_script_cache(psc);
-
-    for (i = 0; i < psc->script_count; i++)
-    {
-        if (psc->scripts[i].tag == script_tag)
-        {
-            script = &psc->scripts[i];
-            break;
-        }
-    }
-
-    if (!script)
+    if (!(script = usp10_script_cache_get_script(psc, script_tag)))
     {
         *pcTags = 0;
         if (!filtered)
@@ -2321,16 +2953,7 @@ HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, O
     if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
         language = &script->default_language;
     else
-    {
-        for (i = 0; i < script->language_count; i++)
-        {
-            if (script->languages[i].tag == language_tag)
-            {
-                language = &script->languages[i];
-                break;
-            }
-        }
-    }
+        language = usp10_script_get_language(script, language_tag);
 
     if (!language)
     {