[USP10] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / usp10 / shape.c
index 5ba6e6f..ca084e7 100644 (file)
@@ -32,6 +32,7 @@
 #include "usp10_internal.h"
 
 #include "wine/debug.h"
+#include "wine/heap.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
 
@@ -41,9 +42,14 @@ WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
 typedef VOID (*ContextualShapingProc)(HDC, ScriptCache*, SCRIPT_ANALYSIS*,
                                       WCHAR*, INT, WORD*, INT*, INT, WORD*);
 
+static void ContextualShape_Control(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Hebrew(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Thaana(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Thai(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Lao(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Sinhala(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Devanagari(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Bengali(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
@@ -59,8 +65,11 @@ static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
 
 typedef VOID (*ShapeCharGlyphPropProc)( HDC , ScriptCache*, SCRIPT_ANALYSIS*, const WCHAR*, const INT, const WORD*, const INT, WORD*, SCRIPT_CHARPROP*, SCRIPT_GLYPHPROP*);
 
-static void ShapeCharGlyphProp_Default( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS* psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD* pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP* pGlyphProp);
+static void ShapeCharGlyphProp_Default( ScriptCache* psc, SCRIPT_ANALYSIS* psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD* pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP* pGlyphProp);
+static void ShapeCharGlyphProp_Control( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
+static void ShapeCharGlyphProp_Latin( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
+static void ShapeCharGlyphProp_Hebrew( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 static void ShapeCharGlyphProp_Thai( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 static void ShapeCharGlyphProp_None( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 static void ShapeCharGlyphProp_Tibet( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
@@ -76,9 +85,9 @@ static void ShapeCharGlyphProp_Kannada( HDC hdc, ScriptCache *psc, SCRIPT_ANALYS
 static void ShapeCharGlyphProp_Malayalam( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 static void ShapeCharGlyphProp_Khmer( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp );
 
-extern const unsigned short indic_syllabic_table[];
-extern const unsigned short wine_shaping_table[];
-extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
+extern const unsigned short indic_syllabic_table[] DECLSPEC_HIDDEN;
+extern const unsigned short wine_shaping_table[] DECLSPEC_HIDDEN;
+extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4] DECLSPEC_HIDDEN;
 
 enum joining_types {
     jtU,
@@ -112,10 +121,13 @@ typedef struct tagConsonantComponents
     WCHAR output;
 } ConsonantComponents;
 
-typedef void (*second_reorder_function)(LPWSTR pwChar, IndicSyllable *syllable,WORD* pwGlyphs, IndicSyllable* glyph_index, lexical_function lex);
+typedef void (*second_reorder_function)(const WCHAR *chars, const IndicSyllable *syllable,
+        WORD *glyphs, const IndicSyllable *glyph_index, lexical_function lex);
+
+typedef int (*combining_lexical_function)(WCHAR c);
 
 /* the orders of joined_forms and contextual_features need to line up */
-static const char* contextual_features[] =
+static const char *const contextual_features[] =
 {
     "isol",
     "fina",
@@ -135,10 +147,19 @@ static OPENTYPE_FEATURE_RECORD standard_features[] =
 
 static OPENTYPE_FEATURE_RECORD latin_features[] =
 {
+    { MS_MAKE_TAG('l','o','c','l'), 1},
+    { MS_MAKE_TAG('c','c','m','p'), 1},
     { MS_MAKE_TAG('l','i','g','a'), 1},
     { MS_MAKE_TAG('c','l','i','g'), 1},
 };
 
+static OPENTYPE_FEATURE_RECORD latin_gpos_features[] =
+{
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('m','a','r','k'), 1},
+    { MS_MAKE_TAG('m','k','m','k'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD arabic_features[] =
 {
     { MS_MAKE_TAG('r','l','i','g'), 1},
@@ -149,7 +170,7 @@ static OPENTYPE_FEATURE_RECORD arabic_features[] =
     { MS_MAKE_TAG('m','s','e','t'), 1},
 };
 
-static const char* required_arabic_features[] =
+static const char *const required_arabic_features[] =
 {
     "fina",
     "init",
@@ -158,11 +179,26 @@ static const char* required_arabic_features[] =
     NULL
 };
 
+static OPENTYPE_FEATURE_RECORD arabic_gpos_features[] =
+{
+    { MS_MAKE_TAG('c','u','r','s'), 1},
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('m','a','r','k'), 1},
+    { MS_MAKE_TAG('m','k','m','k'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD hebrew_features[] =
 {
+    { MS_MAKE_TAG('c','c','m','p'), 1},
     { MS_MAKE_TAG('d','l','i','g'), 0},
 };
 
+static OPENTYPE_FEATURE_RECORD hebrew_gpos_features[] =
+{
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('m','a','r','k'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD syriac_features[] =
 {
     { MS_MAKE_TAG('r','l','i','g'), 1},
@@ -171,7 +207,7 @@ static OPENTYPE_FEATURE_RECORD syriac_features[] =
     { MS_MAKE_TAG('d','l','i','g'), 1},
 };
 
-static const char* required_syriac_features[] =
+static const char *const required_syriac_features[] =
 {
     "fina",
     "fin2",
@@ -183,6 +219,13 @@ static const char* required_syriac_features[] =
     NULL
 };
 
+static OPENTYPE_FEATURE_RECORD syriac_gpos_features[] =
+{
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('m','a','r','k'), 1},
+    { MS_MAKE_TAG('m','k','m','k'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD sinhala_features[] =
 {
     /* Presentation forms */
@@ -197,6 +240,12 @@ static OPENTYPE_FEATURE_RECORD tibetan_features[] =
     { MS_MAKE_TAG('b','l','w','s'), 1},
 };
 
+static OPENTYPE_FEATURE_RECORD tibetan_gpos_features[] =
+{
+    { MS_MAKE_TAG('a','b','v','m'), 1},
+    { MS_MAKE_TAG('b','l','w','m'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD phags_features[] =
 {
     { MS_MAKE_TAG('a','b','v','s'), 1},
@@ -209,13 +258,20 @@ static OPENTYPE_FEATURE_RECORD thai_features[] =
     { MS_MAKE_TAG('c','c','m','p'), 1},
 };
 
-static const char* required_lao_features[] =
+static OPENTYPE_FEATURE_RECORD thai_gpos_features[] =
+{
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('m','a','r','k'), 1},
+    { MS_MAKE_TAG('m','k','m','k'), 1},
+};
+
+static const char *const required_lao_features[] =
 {
     "ccmp",
     NULL
 };
 
-static const char* required_devanagari_features[] =
+static const char *const required_devanagari_features[] =
 {
     "nukt",
     "akhn",
@@ -241,13 +297,21 @@ static OPENTYPE_FEATURE_RECORD devanagari_features[] =
     { MS_MAKE_TAG('c','a','l','t'), 1},
 };
 
+static OPENTYPE_FEATURE_RECORD devanagari_gpos_features[] =
+{
+    { MS_MAKE_TAG('k','e','r','n'), 1},
+    { MS_MAKE_TAG('d','i','s','t'), 1},
+    { MS_MAKE_TAG('a','b','v','m'), 1},
+    { MS_MAKE_TAG('b','l','w','m'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD myanmar_features[] =
 {
     { MS_MAKE_TAG('l','i','g','a'), 1},
     { MS_MAKE_TAG('c','l','i','g'), 1},
 };
 
-static const char* required_bengali_features[] =
+static const char *const required_bengali_features[] =
 {
     "nukt",
     "akhn",
@@ -264,7 +328,7 @@ static const char* required_bengali_features[] =
     NULL
 };
 
-static const char* required_gurmukhi_features[] =
+static const char *const required_gurmukhi_features[] =
 {
     "nukt",
     "akhn",
@@ -283,7 +347,7 @@ static const char* required_gurmukhi_features[] =
     NULL
 };
 
-static const char* required_oriya_features[] =
+static const char *const required_oriya_features[] =
 {
     "nukt",
     "akhn",
@@ -300,7 +364,7 @@ static const char* required_oriya_features[] =
     NULL
 };
 
-static const char* required_tamil_features[] =
+static const char *const required_tamil_features[] =
 {
     "nukt",
     "akhn",
@@ -316,7 +380,7 @@ static const char* required_tamil_features[] =
     NULL
 };
 
-static const char* required_telugu_features[] =
+static const char *const required_telugu_features[] =
 {
     "nukt",
     "akhn",
@@ -343,7 +407,7 @@ static OPENTYPE_FEATURE_RECORD khmer_features[] =
     { MS_MAKE_TAG('c','l','i','g'), 1},
 };
 
-static const char* required_khmer_features[] =
+static const char *const required_khmer_features[] =
 {
     "pref",
     "blwf",
@@ -357,6 +421,14 @@ static const char* required_khmer_features[] =
     NULL
 };
 
+static OPENTYPE_FEATURE_RECORD khmer_gpos_features[] =
+{
+    { MS_MAKE_TAG('d','i','s','t'), 1},
+    { MS_MAKE_TAG('b','l','w','m'), 1},
+    { MS_MAKE_TAG('a','b','v','m'), 1},
+    { MS_MAKE_TAG('m','k','m','k'), 1},
+};
+
 static OPENTYPE_FEATURE_RECORD ethiopic_features[] =
 {
     { MS_MAKE_TAG('c','c','m','p'), 1},
@@ -375,7 +447,8 @@ static OPENTYPE_FEATURE_RECORD mongolian_features[] =
 
 typedef struct ScriptShapeDataTag {
     TEXTRANGE_PROPERTIES   defaultTextRange;
-    const char**           requiredFeatures;
+    TEXTRANGE_PROPERTIES   defaultGPOSTextRange;
+    const char *const *requiredFeatures;
     OPENTYPE_TAG           newOtTag;
     ContextualShapingProc  contextProc;
     ShapeCharGlyphPropProc charGlyphPropProc;
@@ -384,93 +457,94 @@ typedef struct ScriptShapeDataTag {
 /* in order of scripts */
 static const ScriptShapeData ShapingData[] =
 {
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ arabic_features, 6}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ arabic_features, 6}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ hebrew_features, 1}, NULL, 0, NULL, NULL},
-    {{ syriac_features, 4}, required_syriac_features, 0, ContextualShape_Syriac, ShapeCharGlyphProp_None},
-    {{ arabic_features, 6}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ NULL, 0}, NULL, 0, NULL, ShapeCharGlyphProp_None},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ sinhala_features, 3}, NULL, 0, ContextualShape_Sinhala, ShapeCharGlyphProp_Sinhala},
-    {{ tibetan_features, 2}, NULL, 0, NULL, ShapeCharGlyphProp_Tibet},
-    {{ tibetan_features, 2}, NULL, 0, NULL, ShapeCharGlyphProp_Tibet},
-    {{ phags_features, 3}, NULL, 0, ContextualShape_Phags_pa, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, NULL, 0, NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, NULL, 0, NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, required_lao_features, 0, NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, required_lao_features, 0, NULL, ShapeCharGlyphProp_Thai},
-    {{ devanagari_features, 6}, required_devanagari_features, MS_MAKE_TAG('d','e','v','2'), ContextualShape_Devanagari, ShapeCharGlyphProp_Devanagari},
-    {{ devanagari_features, 6}, required_devanagari_features, MS_MAKE_TAG('d','e','v','2'), ContextualShape_Devanagari, ShapeCharGlyphProp_Devanagari},
-    {{ devanagari_features, 6}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_gurmukhi_features, MS_MAKE_TAG('g','u','r','2'), ContextualShape_Gurmukhi, ShapeCharGlyphProp_Gurmukhi},
-    {{ devanagari_features, 6}, required_gurmukhi_features, MS_MAKE_TAG('g','u','r','2'), ContextualShape_Gurmukhi, ShapeCharGlyphProp_Gurmukhi},
-    {{ devanagari_features, 6}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_oriya_features, MS_MAKE_TAG('o','r','y','2'), ContextualShape_Oriya, ShapeCharGlyphProp_Oriya},
-    {{ devanagari_features, 6}, required_oriya_features, MS_MAKE_TAG('o','r','y','2'), ContextualShape_Oriya, ShapeCharGlyphProp_Oriya},
-    {{ devanagari_features, 6}, required_tamil_features, MS_MAKE_TAG('t','a','m','2'), ContextualShape_Tamil, ShapeCharGlyphProp_Tamil},
-    {{ devanagari_features, 6}, required_tamil_features, MS_MAKE_TAG('t','a','m','2'), ContextualShape_Tamil, ShapeCharGlyphProp_Tamil},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('t','e','l','2'), ContextualShape_Telugu, ShapeCharGlyphProp_Telugu},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('t','e','l','2'), ContextualShape_Telugu, ShapeCharGlyphProp_Telugu},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('k','n','d','2'), ContextualShape_Kannada, ShapeCharGlyphProp_Kannada},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('k','n','d','2'), ContextualShape_Kannada, ShapeCharGlyphProp_Kannada},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('m','l','m','2'), ContextualShape_Malayalam, ShapeCharGlyphProp_Malayalam},
-    {{ devanagari_features, 6}, required_telugu_features, MS_MAKE_TAG('m','l','m','2'), ContextualShape_Malayalam, ShapeCharGlyphProp_Malayalam},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ myanmar_features, 2}, NULL, 0, NULL, NULL},
-    {{ myanmar_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ standard_features, 2}, NULL, 0, NULL, NULL},
-    {{ khmer_features, 5}, required_khmer_features, 0, ContextualShape_Khmer, ShapeCharGlyphProp_Khmer},
-    {{ khmer_features, 5}, required_khmer_features, 0, ContextualShape_Khmer, ShapeCharGlyphProp_Khmer},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ ethiopic_features, 4}, NULL, 0, NULL, NULL},
-    {{ ethiopic_features, 4}, NULL, 0, NULL, NULL},
-    {{ mongolian_features, 4}, NULL, 0, ContextualShape_Mongolian, NULL},
-    {{ mongolian_features, 4}, NULL, 0, ContextualShape_Mongolian, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ NULL, 0}, NULL, 0, NULL, NULL},
-    {{ hebrew_features, 1}, NULL, 0, NULL, NULL},
-    {{ latin_features, 2}, NULL, 0, NULL, NULL},
-    {{ thai_features, 1}, NULL, 0, NULL, ShapeCharGlyphProp_Thai},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, ContextualShape_Control, ShapeCharGlyphProp_Control},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ arabic_features, 6}, {arabic_gpos_features, 4}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
+    {{ arabic_features, 6}, {arabic_gpos_features, 4}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
+    {{ hebrew_features, 2}, {hebrew_gpos_features, 2}, NULL, 0, ContextualShape_Hebrew, ShapeCharGlyphProp_Hebrew},
+    {{ syriac_features, 4}, {syriac_gpos_features, 3}, required_syriac_features, 0, ContextualShape_Syriac, ShapeCharGlyphProp_None},
+    {{ arabic_features, 6}, {arabic_gpos_features, 4}, required_arabic_features, 0, ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, ContextualShape_Thaana, ShapeCharGlyphProp_None},
+    {{ standard_features, 2}, {latin_gpos_features, 3}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {latin_gpos_features, 3}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {latin_gpos_features, 3}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {latin_gpos_features, 3}, NULL, 0, NULL, NULL},
+    {{ sinhala_features, 3}, {NULL, 0}, NULL, 0, ContextualShape_Sinhala, ShapeCharGlyphProp_Sinhala},
+    {{ tibetan_features, 2}, {tibetan_gpos_features, 2}, NULL, 0, NULL, ShapeCharGlyphProp_Tibet},
+    {{ tibetan_features, 2}, {tibetan_gpos_features, 2}, NULL, 0, NULL, ShapeCharGlyphProp_Tibet},
+    {{ phags_features, 3}, {NULL, 0}, NULL, 0, ContextualShape_Phags_pa, ShapeCharGlyphProp_Thai},
+    {{ thai_features, 1}, {thai_gpos_features, 3}, NULL, 0, ContextualShape_Thai, ShapeCharGlyphProp_Thai},
+    {{ thai_features, 1}, {thai_gpos_features, 3}, NULL, 0, ContextualShape_Thai, NULL},
+    {{ thai_features, 1}, {thai_gpos_features, 3}, required_lao_features, 0, ContextualShape_Lao, ShapeCharGlyphProp_Thai},
+    {{ thai_features, 1}, {thai_gpos_features, 3}, required_lao_features, 0, ContextualShape_Lao, ShapeCharGlyphProp_Thai},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_devanagari_features, MS_MAKE_TAG('d','e','v','2'), ContextualShape_Devanagari, ShapeCharGlyphProp_Devanagari},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_devanagari_features, MS_MAKE_TAG('d','e','v','2'), ContextualShape_Devanagari, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_bengali_features, MS_MAKE_TAG('b','n','g','2'), ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_gurmukhi_features, MS_MAKE_TAG('g','u','r','2'), ContextualShape_Gurmukhi, ShapeCharGlyphProp_Gurmukhi},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_gurmukhi_features, MS_MAKE_TAG('g','u','r','2'), ContextualShape_Gurmukhi, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_devanagari_features, MS_MAKE_TAG('g','j','r','2'), ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_oriya_features, MS_MAKE_TAG('o','r','y','2'), ContextualShape_Oriya, ShapeCharGlyphProp_Oriya},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_oriya_features, MS_MAKE_TAG('o','r','y','2'), ContextualShape_Oriya, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_tamil_features, MS_MAKE_TAG('t','a','m','2'), ContextualShape_Tamil, ShapeCharGlyphProp_Tamil},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_tamil_features, MS_MAKE_TAG('t','a','m','2'), ContextualShape_Tamil, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('t','e','l','2'), ContextualShape_Telugu, ShapeCharGlyphProp_Telugu},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('t','e','l','2'), ContextualShape_Telugu, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('k','n','d','2'), ContextualShape_Kannada, ShapeCharGlyphProp_Kannada},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('k','n','d','2'), ContextualShape_Kannada, NULL},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('m','l','m','2'), ContextualShape_Malayalam, ShapeCharGlyphProp_Malayalam},
+    {{ devanagari_features, 6}, {devanagari_gpos_features, 4}, required_telugu_features, MS_MAKE_TAG('m','l','m','2'), ContextualShape_Malayalam, NULL},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ myanmar_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ myanmar_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ standard_features, 2}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ khmer_features, 5}, {khmer_gpos_features, 4}, required_khmer_features, 0, ContextualShape_Khmer, ShapeCharGlyphProp_Khmer},
+    {{ khmer_features, 5}, {khmer_gpos_features, 4}, required_khmer_features, 0, ContextualShape_Khmer, ShapeCharGlyphProp_Khmer},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ ethiopic_features, 4}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ ethiopic_features, 4}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ mongolian_features, 4}, {NULL, 0}, NULL, 0, ContextualShape_Mongolian, NULL},
+    {{ mongolian_features, 4}, {NULL, 0}, NULL, 0, ContextualShape_Mongolian, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ NULL, 0}, {NULL, 0}, NULL, 0, NULL, NULL},
+    {{ hebrew_features, 2}, {hebrew_gpos_features, 2}, NULL, 0, ContextualShape_Hebrew, NULL},
+    {{ latin_features, 4}, {latin_gpos_features, 3}, NULL, 0, NULL, ShapeCharGlyphProp_Latin},
+    {{ thai_features, 1}, {thai_gpos_features, 3}, NULL, 0, ContextualShape_Thai, ShapeCharGlyphProp_Thai},
 };
 
 extern scriptData scriptInformation[];
 
-static INT GSUB_apply_feature_all_lookups(LPCVOID header, LoadedFeature *feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+static int GSUB_apply_feature_all_lookups(const void *header, LoadedFeature *feature,
+        WORD *glyphs, unsigned int glyph_index, int write_dir, int *glyph_count)
 {
     int i;
     int out_index = GSUB_E_NOGLYPH;
@@ -494,19 +568,21 @@ static INT GSUB_apply_feature_all_lookups(LPCVOID header, LoadedFeature *feature
     return out_index;
 }
 
-static OPENTYPE_TAG get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, BOOL tryNew)
+static OPENTYPE_TAG get_opentype_script(HDC hdc, const SCRIPT_ANALYSIS *psa,
+        const ScriptCache *script_cache, BOOL try_new)
 {
     UINT charset;
 
-    if (psc->userScript != 0)
+    if (script_cache->userScript)
     {
-        if (tryNew && ShapingData[psa->eScript].newOtTag != 0 && psc->userScript == scriptInformation[psa->eScript].scriptTag)
+        if (try_new && ShapingData[psa->eScript].newOtTag
+                && script_cache->userScript == scriptInformation[psa->eScript].scriptTag)
             return ShapingData[psa->eScript].newOtTag;
-        else
-            return psc->userScript;
+
+        return script_cache->userScript;
     }
 
-    if (tryNew && ShapingData[psa->eScript].newOtTag != 0)
+    if (try_new && ShapingData[psa->eScript].newOtTag)
         return ShapingData[psa->eScript].newOtTag;
 
     if (scriptInformation[psa->eScript].scriptTag)
@@ -537,16 +613,16 @@ static OPENTYPE_TAG get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCac
     }
 }
 
-static LoadedFeature* load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
+static LoadedFeature* load_OT_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, char tableType, const char* feat)
 {
     LoadedFeature *feature = NULL;
 
-    if (psc->GSUB_Table)
+    if (psc->GSUB_Table || psc->GPOS_Table)
     {
         int attempt = 2;
         OPENTYPE_TAG tags;
         OPENTYPE_TAG language;
-        OPENTYPE_TAG script;
+        OPENTYPE_TAG script = 0x00000000;
         int cTags;
 
         do
@@ -558,13 +634,17 @@ static LoadedFeature* load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCac
                 language = MS_MAKE_TAG('d','f','l','t');
             attempt--;
 
-            OpenType_GSUB_GetFontFeatureTags(psc, script, language, FALSE, MS_MAKE_TAG(feat[0],feat[1],feat[2],feat[3]), 1, &tags, &cTags, &feature);
+            OpenType_GetFontFeatureTags(psc, script, language, FALSE, MS_MAKE_TAG(feat[0],feat[1],feat[2],feat[3]), tableType, 1, &tags, &cTags, &feature);
 
         } while(attempt && !feature);
 
         /* try in the default (latin) table */
         if (!feature)
-            OpenType_GSUB_GetFontFeatureTags(psc, MS_MAKE_TAG('l','a','t','n'), MS_MAKE_TAG('d','f','l','t'), FALSE, MS_MAKE_TAG(feat[0],feat[1],feat[2],feat[3]), 1, &tags, &cTags, &feature);
+        {
+            if (!script)
+                script = MS_MAKE_TAG('l','a','t','n');
+            OpenType_GetFontFeatureTags(psc, script, MS_MAKE_TAG('d','f','l','t'), FALSE, MS_MAKE_TAG(feat[0],feat[1],feat[2],feat[3]), tableType, 1, &tags, &cTags, &feature);
+        }
     }
 
     TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
@@ -575,7 +655,7 @@ static INT apply_GSUB_feature_to_glyph(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCach
 {
     LoadedFeature *feature;
 
-    feature = load_GSUB_feature(hdc, psa, psc, feat);
+    feature = load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, feat);
     if (!feature)
         return GSUB_E_NOFEATURE;
 
@@ -589,20 +669,56 @@ static VOID *load_gsub_table(HDC hdc)
     int length = GetFontData(hdc, MS_MAKE_TAG('G', 'S', 'U', 'B'), 0, NULL, 0);
     if (length != GDI_ERROR)
     {
-        GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
+        GSUB_Table = heap_alloc(length);
         GetFontData(hdc, MS_MAKE_TAG('G', 'S', 'U', 'B'), 0, GSUB_Table, length);
         TRACE("Loaded GSUB table of %i bytes\n",length);
     }
     return GSUB_Table;
 }
 
+static VOID *load_gpos_table(HDC hdc)
+{
+    VOID* GPOS_Table = NULL;
+    int length = GetFontData(hdc, MS_MAKE_TAG('G', 'P', 'O', 'S'), 0, NULL, 0);
+    if (length != GDI_ERROR)
+    {
+        GPOS_Table = heap_alloc(length);
+        GetFontData(hdc, MS_MAKE_TAG('G', 'P', 'O', 'S'), 0, GPOS_Table, length);
+        TRACE("Loaded GPOS table of %i bytes\n",length);
+    }
+    return GPOS_Table;
+}
+
+static VOID *load_gdef_table(HDC hdc)
+{
+    VOID* GDEF_Table = NULL;
+    int length = GetFontData(hdc, MS_MAKE_TAG('G', 'D', 'E', 'F'), 0, NULL, 0);
+    if (length != GDI_ERROR)
+    {
+        GDEF_Table = heap_alloc(length);
+        GetFontData(hdc, MS_MAKE_TAG('G', 'D', 'E', 'F'), 0, GDEF_Table, length);
+        TRACE("Loaded GDEF table of %i bytes\n",length);
+    }
+    return GDEF_Table;
+}
+
+static VOID load_ot_tables(HDC hdc, ScriptCache *psc)
+{
+    if (!psc->GSUB_Table)
+        psc->GSUB_Table = load_gsub_table(hdc);
+    if (!psc->GPOS_Table)
+        psc->GPOS_Table = load_gpos_table(hdc);
+    if (!psc->GDEF_Table)
+        psc->GDEF_Table = load_gdef_table(hdc);
+}
+
 INT SHAPE_does_GSUB_feature_apply_to_chars(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, const WCHAR *chars, INT write_dir, INT count, const char* feature)
 {
     WORD *glyphs;
     INT glyph_count = count;
     INT rc;
 
-    glyphs = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*(count*2));
+    glyphs = heap_alloc(2 * count * sizeof(*glyphs));
     GetGlyphIndicesW(hdc, chars, count, glyphs, 0);
     rc = apply_GSUB_feature_to_glyph(hdc, psa, psc, glyphs, 0, write_dir, &glyph_count, feature);
     if (rc > GSUB_E_NOGLYPH)
@@ -610,7 +726,7 @@ INT SHAPE_does_GSUB_feature_apply_to_chars(HDC hdc, SCRIPT_ANALYSIS *psa, Script
     else
         rc = 0;
 
-    HeapFree(GetProcessHeap(),0,glyphs);
+    heap_free(glyphs);
     return rc;
 }
 
@@ -628,9 +744,9 @@ static void UpdateClustersFromGlyphProp(const int cGlyphs, const int cChars, WOR
                 if (pwLogClust[j] == i)
                 {
                     int k = j;
-                    while (!pGlyphProp[pwLogClust[k]].sva.fClusterStart && k >= 0 && k <cChars)
+                    while (k >= 0 && k <cChars && !pGlyphProp[pwLogClust[k]].sva.fClusterStart)
                         k-=1;
-                    if (pGlyphProp[pwLogClust[k]].sva.fClusterStart)
+                    if (k >= 0 && k <cChars && pGlyphProp[pwLogClust[k]].sva.fClusterStart)
                         pwLogClust[j] = pwLogClust[k];
                 }
             }
@@ -644,13 +760,12 @@ static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int ch
         return;
     else
     {
+        int cluster_dir = pwLogClust[0] < pwLogClust[chars-1] ? 1 : -1;
         int i;
         int target_glyph = nextIndex - write_dir;
-        int seeking_glyph;
         int target_index = -1;
         int replacing_glyph = -1;
         int changed = 0;
-        int top_logclust = 0;
 
         if (changeCount > 0)
         {
@@ -660,35 +775,7 @@ static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int ch
                 target_glyph = nextIndex + (changeCount + 1);
         }
 
-        seeking_glyph = target_glyph;
-        for (i = 0; i < chars; i++)
-            if (pwLogClust[i] > top_logclust)
-                top_logclust = pwLogClust[i];
-
-        do {
-            if (write_dir > 0)
-                for (i = 0; i < chars; i++)
-                {
-                    if (pwLogClust[i] == seeking_glyph)
-                    {
-                        target_index = i;
-                        break;
-                    }
-                }
-            else
-                for (i = chars - 1; i >= 0; i--)
-                {
-                    if (pwLogClust[i] == seeking_glyph)
-                    {
-                        target_index = i;
-                        break;
-                    }
-                }
-            if (target_index == -1)
-                seeking_glyph ++;
-        }
-        while (target_index == -1 && seeking_glyph <= top_logclust);
-
+        target_index = USP10_FindGlyphInLogClust(pwLogClust, chars, target_glyph);
         if (target_index == -1)
         {
             ERR("Unable to find target glyph\n");
@@ -698,7 +785,7 @@ static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int ch
         if (changeCount < 0)
         {
             /* merge glyphs */
-            for(i = target_index; i < chars && i >= 0; i+=write_dir)
+            for (i = target_index; i < chars && i >= 0; i += cluster_dir)
             {
                 if (pwLogClust[i] == target_glyph)
                     continue;
@@ -717,8 +804,8 @@ static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int ch
                 }
             }
 
-            /* renumber trailing indexes*/
-            for(i = target_index; i < chars && i >= 0; i+=write_dir)
+            /* renumber trailing indexes */
+            for (i = target_index; i < chars && i >= 0; i += cluster_dir)
             {
                 if (pwLogClust[i] != target_glyph)
                     pwLogClust[i] += changeCount;
@@ -726,8 +813,8 @@ static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int ch
         }
         else
         {
-            for(i = target_index; i < chars && i >= 0; i+=write_dir)
-                    pwLogClust[i] += changeCount;
+            for (i = target_index; i < chars && i >= 0; i += cluster_dir)
+                pwLogClust[i] += changeCount;
         }
     }
 }
@@ -739,7 +826,7 @@ static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, W
         LoadedFeature *feature;
         int lookup_index;
 
-        feature = load_GSUB_feature(hdc, psa, psc, feat);
+        feature = load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, feat);
         if (!feature)
             return GSUB_E_NOFEATURE;
 
@@ -773,17 +860,94 @@ static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, W
     return GSUB_E_NOFEATURE;
 }
 
+static void GPOS_apply_feature(const ScriptCache *psc, const OUTLINETEXTMETRICW *otm,
+        const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance,
+        const LoadedFeature *feature, const WORD *glyphs, int glyph_count, GOFFSET *goffset)
+{
+    int dir = analysis->fLogicalOrder && analysis->fRTL ? -1 : 1;
+    unsigned int start_idx, i, j;
+
+    TRACE("%i lookups\n", feature->lookup_count);
+
+    start_idx = dir < 0 ? glyph_count - 1 : 0;
+    for (i = 0; i < feature->lookup_count; i++)
+    {
+        for (j = 0; j < glyph_count; )
+            j += OpenType_apply_GPOS_lookup(psc, otm, logfont, analysis, advance,
+                    feature->lookups[i], glyphs, start_idx + dir * j, glyph_count, goffset);
+    }
+}
+
 static inline BOOL get_GSUB_Indic2(SCRIPT_ANALYSIS *psa, ScriptCache *psc)
 {
     OPENTYPE_TAG tag;
     HRESULT hr;
     int count = 0;
 
-    hr = OpenType_GSUB_GetFontScriptTags(psc, ShapingData[psa->eScript].newOtTag, 1, &tag, &count, NULL);
+    hr = OpenType_GetFontScriptTags(psc, ShapingData[psa->eScript].newOtTag, 1, &tag, &count);
 
     return(SUCCEEDED(hr));
 }
 
+static void insert_glyph(WORD *pwGlyphs, INT *pcGlyphs, INT cChars, INT write_dir, WORD glyph, INT index, WORD *pwLogClust)
+{
+    int i;
+    for (i = *pcGlyphs; i>=index; i--)
+        pwGlyphs[i+1] = pwGlyphs[i];
+    pwGlyphs[index] = glyph;
+    *pcGlyphs = *pcGlyphs+1;
+    if (write_dir < 0)
+        UpdateClusters(index-3, 1, write_dir, cChars, pwLogClust);
+    else
+        UpdateClusters(index, 1, write_dir, cChars, pwLogClust);
+}
+
+static void mark_invalid_combinations(HDC hdc, const WCHAR* pwcChars, INT cChars, WORD *pwGlyphs, INT *pcGlyphs, INT write_dir, WORD *pwLogClust, combining_lexical_function lex)
+{
+    CHAR *context_type;
+    int i,g;
+    WCHAR invalid = 0x25cc;
+    WORD invalid_glyph;
+
+    context_type = heap_alloc(cChars);
+
+    /* Mark invalid combinations */
+    for (i = 0; i < cChars; i++)
+       context_type[i] = lex(pwcChars[i]);
+
+    GetGlyphIndicesW(hdc, &invalid, 1, &invalid_glyph, 0);
+    for (i = 1, g=1; i < cChars - 1; i++, g++)
+    {
+        if (context_type[i] != 0 && context_type[i+write_dir]==context_type[i])
+        {
+            insert_glyph(pwGlyphs, pcGlyphs, cChars, write_dir, invalid_glyph, g, pwLogClust);
+            g++;
+        }
+    }
+
+    heap_free(context_type);
+}
+
+static void ContextualShape_Control(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    int i;
+    for (i=0; i < cChars; i++)
+    {
+        switch (pwcChars[i])
+        {
+            case 0x000A:
+            case 0x000D:
+                pwOutGlyphs[i] = psc->sfp.wgBlank;
+                break;
+            default:
+                if (pwcChars[i] < 0x1C)
+                    pwOutGlyphs[i] = psc->sfp.wgDefault;
+                else
+                    pwOutGlyphs[i] = psc->sfp.wgBlank;
+        }
+    }
+}
+
 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
 {
     if (i + delta < 0)
@@ -839,6 +1003,51 @@ static inline BOOL word_break_causing(WCHAR chr)
     return (chr == 0 || chr == 0x20 );
 }
 
+static int combining_lexical_Arabic(WCHAR c)
+{
+    enum {Arab_Norm = 0, Arab_DIAC1, Arab_DIAC2, Arab_DIAC3, Arab_DIAC4, Arab_DIAC5, Arab_DIAC6, Arab_DIAC7, Arab_DIAC8};
+
+   switch(c)
+    {
+        case 0x064B:
+        case 0x064C:
+        case 0x064E:
+        case 0x064F:
+        case 0x0652:
+        case 0x0657:
+        case 0x0658:
+        case 0x06E1: return Arab_DIAC1;
+        case 0x064D:
+        case 0x0650:
+        case 0x0656: return Arab_DIAC2;
+        case 0x0651: return Arab_DIAC3;
+        case 0x0610:
+        case 0x0611:
+        case 0x0612:
+        case 0x0613:
+        case 0x0614:
+        case 0x0659:
+        case 0x06D6:
+        case 0x06DC:
+        case 0x06DF:
+        case 0x06E0:
+        case 0x06E2:
+        case 0x06E4:
+        case 0x06E7:
+        case 0x06E8:
+        case 0x06EB:
+        case 0x06EC: return Arab_DIAC4;
+        case 0x06E3:
+        case 0x06EA:
+        case 0x06ED: return Arab_DIAC5;
+        case 0x0670: return Arab_DIAC6;
+        case 0x0653: return Arab_DIAC7;
+        case 0x0655:
+        case 0x0654: return Arab_DIAC8;
+        default: return Arab_Norm;
+    }
+}
+
 /*
  * ContextualShape_Arabic
  */
@@ -848,6 +1057,8 @@ static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
     INT *context_shape;
     INT dirR, dirL;
     int i;
+    int char_index;
+    int glyph_index;
 
     if (*pcGlyphs != cChars)
     {
@@ -855,22 +1066,21 @@ static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
         return;
     }
 
-    if (!psa->fLogicalOrder && psa->fRTL)
+    if (psa->fLogicalOrder && psa->fRTL)
     {
-        dirR = 1;
-        dirL = -1;
+        dirR = -1;
+        dirL = 1;
     }
     else
     {
-        dirR = -1;
-        dirL = 1;
+        dirR = 1;
+        dirL = -1;
     }
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
-    context_type = HeapAlloc(GetProcessHeap(),0,cChars);
-    context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
+    context_type = heap_alloc(cChars);
+    context_shape = heap_alloc(cChars * sizeof(*context_shape));
 
     for (i = 0; i < cChars; i++)
         context_type[i] = get_table_entry( wine_shaping_table, pwcChars[i] );
@@ -892,49 +1102,205 @@ static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
     }
 
     /* Contextual Shaping */
-    i = 0;
-    while(i < *pcGlyphs)
+    if (dirL > 0)
+        char_index = glyph_index = 0;
+    else
+        char_index = glyph_index = cChars-1;
+
+    while(char_index < cChars && char_index >= 0)
     {
         BOOL shaped = FALSE;
 
         if (psc->GSUB_Table)
         {
-            INT nextIndex;
+            INT nextIndex, offset = 0;
             INT prevCount = *pcGlyphs;
-            nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
+
+            /* Apply CCMP first */
+            apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, "ccmp");
+
+            if (prevCount != *pcGlyphs)
+            {
+                offset = *pcGlyphs - prevCount;
+                if (dirL < 0)
+                    glyph_index -= offset * dirL;
+            }
+
+            /* Apply the contextual feature */
+            nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, contextual_features[context_shape[char_index]]);
+
             if (nextIndex > GSUB_E_NOGLYPH)
             {
-                i = nextIndex;
-                UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
+                UpdateClusters(glyph_index, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
+                char_index += dirL;
+                if (!offset)
+                    glyph_index = nextIndex;
+                else
+                {
+                    offset = *pcGlyphs - prevCount;
+                    glyph_index += dirL * (offset + 1);
+                }
+                shaped = TRUE;
+            }
+            else if (nextIndex == GSUB_E_NOGLYPH)
+            {
+                char_index += dirL;
+                glyph_index += dirL;
+                shaped = TRUE;
             }
-            shaped = (nextIndex > GSUB_E_NOGLYPH);
         }
 
         if (!shaped)
         {
-            if (context_shape[i] == Xn)
+            if (context_shape[char_index] == Xn)
             {
-                WORD newGlyph = pwOutGlyphs[i];
-                if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
+                WORD newGlyph = pwOutGlyphs[glyph_index];
+                if (pwcChars[char_index] >= FIRST_ARABIC_CHAR && pwcChars[char_index] <= LAST_ARABIC_CHAR)
                 {
                     /* fall back to presentation form B */
-                    WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
-                    if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
-                        pwOutGlyphs[i] = newGlyph;
+                    WCHAR context_char = wine_shaping_forms[pwcChars[char_index] - FIRST_ARABIC_CHAR][context_shape[char_index]];
+                    if (context_char != pwcChars[char_index] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
+                        pwOutGlyphs[glyph_index] = newGlyph;
                 }
             }
-            i++;
+            char_index += dirL;
+            glyph_index += dirL;
         }
     }
 
-    HeapFree(GetProcessHeap(),0,context_shape);
-    HeapFree(GetProcessHeap(),0,context_type);
+    heap_free(context_shape);
+    heap_free(context_type);
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Arabic);
+}
+
+static int combining_lexical_Hebrew(WCHAR c)
+{
+    enum {Hebr_Norm=0, Hebr_DIAC, Hebr_CANT1, Hebr_CANT2, Hebr_CANT3, Hebr_CANT4, Hebr_CANT5, Hebr_CANT6, Hebr_CANT7, Hebr_CANT8, Hebr_CANT9, Hebr_CANT10, Hebr_DAGESH, Hebr_DOTABV, Hebr_HOLAM, Hebr_METEG, Hebr_PATAH, Hebr_QAMATS, Hebr_RAFE, Hebr_SHINSIN};
+
+   switch(c)
+    {
+        case 0x05B0:
+        case 0x05B1:
+        case 0x05B2:
+        case 0x05B3:
+        case 0x05B4:
+        case 0x05B5:
+        case 0x05B6:
+        case 0x05BB: return Hebr_DIAC;
+        case 0x0599:
+        case 0x05A1:
+        case 0x05A9:
+        case 0x05AE: return Hebr_CANT1;
+        case 0x0597:
+        case 0x05A8:
+        case 0x05AC: return Hebr_CANT2;
+        case 0x0592:
+        case 0x0593:
+        case 0x0594:
+        case 0x0595:
+        case 0x05A7:
+        case 0x05AB: return Hebr_CANT3;
+        case 0x0598:
+        case 0x059C:
+        case 0x059E:
+        case 0x059F: return Hebr_CANT4;
+        case 0x059D:
+        case 0x05A0: return Hebr_CANT5;
+        case 0x059B:
+        case 0x05A5: return Hebr_CANT6;
+        case 0x0591:
+        case 0x05A3:
+        case 0x05A6: return Hebr_CANT7;
+        case 0x0596:
+        case 0x05A4:
+        case 0x05AA: return Hebr_CANT8;
+        case 0x059A:
+        case 0x05AD: return Hebr_CANT9;
+        case 0x05AF: return Hebr_CANT10;
+        case 0x05BC: return Hebr_DAGESH;
+        case 0x05C4: return Hebr_DOTABV;
+        case 0x05B9: return Hebr_HOLAM;
+        case 0x05BD: return Hebr_METEG;
+        case 0x05B7: return Hebr_PATAH;
+        case 0x05B8: return Hebr_QAMATS;
+        case 0x05BF: return Hebr_RAFE;
+        case 0x05C1:
+        case 0x05C2: return Hebr_SHINSIN;
+        default: return Hebr_Norm;
+    }
+}
+
+static void ContextualShape_Hebrew(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    INT dirL;
+
+    if (*pcGlyphs != cChars)
+    {
+        ERR("Number of Glyphs and Chars need to match at the beginning\n");
+        return;
+    }
+
+    if (!psa->fLogicalOrder && psa->fRTL)
+        dirL = -1;
+    else
+        dirL = 1;
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Hebrew);
 }
 
 /*
  * ContextualShape_Syriac
  */
 
+static int combining_lexical_Syriac(WCHAR c)
+{
+    enum {Syriac_Norm=0, Syriac_DIAC1, Syriac_DIAC2, Syriac_DIAC3, Syriac_DIAC4, Syriac_DIAC5, Syriac_DIAC6, Syriac_DIAC7, Syriac_DIAC8, Syriac_DIAC9, Syriac_DIAC10, Syriac_DIAC11, Syriac_DIAC12, Syriac_DIAC13, Syriac_DIAC14, Syriac_DIAC15, Syriac_DIAC16, Syriac_DIAC17};
+
+   switch(c)
+    {
+        case 0x730:
+        case 0x733:
+        case 0x736:
+        case 0x73A:
+        case 0x73D: return Syriac_DIAC1;
+        case 0x731:
+        case 0x734:
+        case 0x737:
+        case 0x73B:
+        case 0x73E: return Syriac_DIAC2;
+        case 0x740:
+        case 0x749:
+        case 0x74A: return Syriac_DIAC3;
+        case 0x732:
+        case 0x735:
+        case 0x73F: return Syriac_DIAC4;
+        case 0x738:
+        case 0x739:
+        case 0x73C: return Syriac_DIAC5;
+        case 0x741:
+        case 0x30A: return Syriac_DIAC6;
+        case 0x742:
+        case 0x325: return Syriac_DIAC7;
+        case 0x747:
+        case 0x303: return Syriac_DIAC8;
+        case 0x748:
+        case 0x32D:
+        case 0x32E:
+        case 0x330:
+        case 0x331: return Syriac_DIAC9;
+        case 0x308: return Syriac_DIAC10;
+        case 0x304: return Syriac_DIAC11;
+        case 0x307: return Syriac_DIAC12;
+        case 0x323: return Syriac_DIAC13;
+        case 0x743: return Syriac_DIAC14;
+        case 0x744: return Syriac_DIAC15;
+        case 0x745: return Syriac_DIAC16;
+        case 0x746: return Syriac_DIAC17;
+        default: return Syriac_Norm;
+    }
+}
+
 #define ALAPH 0x710
 #define DALATH 0x715
 #define RISH 0x72A
@@ -945,6 +1311,8 @@ static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
     INT *context_shape;
     INT dirR, dirL;
     int i;
+    int char_index;
+    int glyph_index;
 
     if (*pcGlyphs != cChars)
     {
@@ -963,14 +1331,13 @@ static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
         dirL = 1;
     }
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (!psc->GSUB_Table)
         return;
 
-    context_type = HeapAlloc(GetProcessHeap(),0,cChars);
-    context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
+    context_type = heap_alloc(cChars);
+    context_shape = heap_alloc(cChars * sizeof(*context_shape));
 
     for (i = 0; i < cChars; i++)
         context_type[i] = get_table_entry( wine_shaping_table, pwcChars[i] );
@@ -1008,23 +1375,89 @@ right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
     }
 
     /* Contextual Shaping */
-    i = 0;
-    while(i < *pcGlyphs)
+    if (dirL > 0)
+        char_index = glyph_index = 0;
+    else
+        char_index = glyph_index = cChars-1;
+
+    while(char_index < cChars && char_index >= 0)
     {
-        INT nextIndex;
+        INT nextIndex, offset = 0;
         INT prevCount = *pcGlyphs;
-        nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
+
+        /* Apply CCMP first */
+        apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, "ccmp");
+
+        if (prevCount != *pcGlyphs)
+        {
+            offset = *pcGlyphs - prevCount;
+            if (dirL < 0)
+                glyph_index -= offset * dirL;
+        }
+
+        /* Apply the contextual feature */
+        nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, contextual_features[context_shape[char_index]]);
         if (nextIndex > GSUB_E_NOGLYPH)
         {
             UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
-            i = nextIndex;
+            char_index += dirL;
+            if (!offset)
+                glyph_index = nextIndex;
+            else
+            {
+                offset = *pcGlyphs - prevCount;
+                glyph_index += dirL * (offset + 1);
+            }
         }
         else
-            i++;
+        {
+            char_index += dirL;
+            glyph_index += dirL;
+        }
+    }
+
+    heap_free(context_shape);
+    heap_free(context_type);
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Syriac);
+}
+
+static int combining_lexical_Thaana(WCHAR c)
+{
+    enum {Thaana_Norm=0, Thaana_FILI};
+
+   switch(c)
+    {
+        case 0x7A6:
+        case 0x7A7:
+        case 0x7A8:
+        case 0x7A9:
+        case 0x7AA:
+        case 0x7AB:
+        case 0x7AC:
+        case 0x7AD:
+        case 0x7AE:
+        case 0x7AF: return Thaana_FILI;
+        default: return Thaana_Norm;
     }
+}
+
+static void ContextualShape_Thaana(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    INT dirL;
+
+    if (*pcGlyphs != cChars)
+    {
+        ERR("Number of Glyphs and Chars need to match at the beginning\n");
+        return;
+    }
+
+    if (!psa->fLogicalOrder && psa->fRTL)
+        dirL = -1;
+    else
+        dirL = 1;
 
-    HeapFree(GetProcessHeap(),0,context_shape);
-    HeapFree(GetProcessHeap(),0,context_type);
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Thaana);
 }
 
 /*
@@ -1040,6 +1473,8 @@ static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     INT *context_shape;
     INT dirR, dirL;
     int i;
+    int char_index;
+    int glyph_index;
 
     if (*pcGlyphs != cChars)
     {
@@ -1058,13 +1493,12 @@ static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
         dirL = 1;
     }
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (!psc->GSUB_Table)
         return;
 
-    context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
+    context_shape = heap_alloc(cChars * sizeof(*context_shape));
 
     for (i = 0; i < cChars; i++)
     {
@@ -1089,27 +1523,128 @@ static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     }
 
     /* Contextual Shaping */
-    i = 0;
-    while(i < *pcGlyphs)
+    if (dirL > 0)
+        char_index = glyph_index = 0;
+    else
+        char_index = glyph_index = cChars-1;
+
+    while(char_index < cChars && char_index >= 0)
     {
-        if (context_shape[i] >= 0)
+        if (context_shape[char_index] >= 0)
         {
             INT nextIndex;
             INT prevCount = *pcGlyphs;
-            nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
+            nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, contextual_features[context_shape[char_index]]);
+
             if (nextIndex > GSUB_E_NOGLYPH)
             {
                 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
-                i = nextIndex;
+                glyph_index = nextIndex;
+                char_index += dirL;
             }
             else
-                i++;
+            {
+                char_index += dirL;
+                glyph_index += dirL;
+            }
         }
         else
-            i++;
+        {
+            char_index += dirL;
+            glyph_index += dirL;
+        }
     }
 
-    HeapFree(GetProcessHeap(),0,context_shape);
+    heap_free(context_shape);
+}
+
+static int combining_lexical_Thai(WCHAR c)
+{
+    enum {Thai_Norm=0, Thai_ABOVE1, Thai_ABOVE2, Thai_ABOVE3, Thai_ABOVE4, Thai_BELOW1, Thai_BELOW2, Thai_AM};
+
+   switch(c)
+    {
+        case 0xE31:
+        case 0xE34:
+        case 0xE35:
+        case 0xE36:
+        case 0xE37: return Thai_ABOVE1;
+        case 0xE47:
+        case 0xE4D: return Thai_ABOVE2;
+        case 0xE48:
+        case 0xE49:
+        case 0xE4A:
+        case 0xE4B: return Thai_ABOVE3;
+        case 0xE4C:
+        case 0xE4E: return Thai_ABOVE4;
+        case 0xE38:
+        case 0xE39: return Thai_BELOW1;
+        case 0xE3A: return Thai_BELOW2;
+        case 0xE33: return Thai_AM;
+        default: return Thai_Norm;
+    }
+}
+
+static void ContextualShape_Thai(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    INT dirL;
+
+    if (*pcGlyphs != cChars)
+    {
+        ERR("Number of Glyphs and Chars need to match at the beginning\n");
+        return;
+    }
+
+    if (!psa->fLogicalOrder && psa->fRTL)
+        dirL = -1;
+    else
+        dirL = 1;
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Thai);
+}
+
+static int combining_lexical_Lao(WCHAR c)
+{
+    enum {Lao_Norm=0, Lao_ABOVE1, Lao_ABOVE2, Lao_BELOW1, Lao_BELOW2, Lao_AM};
+
+   switch(c)
+    {
+        case 0xEB1:
+        case 0xEB4:
+        case 0xEB5:
+        case 0xEB6:
+        case 0xEB7:
+        case 0xEBB:
+        case 0xECD: return Lao_ABOVE1;
+        case 0xEC8:
+        case 0xEC9:
+        case 0xECA:
+        case 0xECB:
+        case 0xECC: return Lao_ABOVE2;
+        case 0xEBC: return Lao_BELOW1;
+        case 0xEB8:
+        case 0xEB9: return Lao_BELOW2;
+        case 0xEB3: return Lao_AM;
+        default: return Lao_Norm;
+    }
+}
+
+static void ContextualShape_Lao(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    INT dirL;
+
+    if (*pcGlyphs != cChars)
+    {
+        ERR("Number of Glyphs and Chars need to match at the beginning\n");
+        return;
+    }
+
+    if (!psa->fLogicalOrder && psa->fRTL)
+        dirL = -1;
+    else
+        dirL = 1;
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Lao);
 }
 
 static void ReplaceInsertChars(HDC hdc, INT cWalk, INT* pcChars, WCHAR *pwOutChars, const WCHAR *replacements)
@@ -1121,7 +1656,7 @@ static void ReplaceInsertChars(HDC hdc, INT cWalk, INT* pcChars, WCHAR *pwOutCha
     cWalk=cWalk+1;
 
     /* Insert */
-    for (i = 1; replacements[i] != 0x0000 && i < 3; i++)
+    for (i = 1; i < 3 && replacements[i] != 0x0000; i++)
     {
         int j;
         for (j = *pcChars; j > cWalk; j--)
@@ -1189,7 +1724,7 @@ static void ComposeConsonants(HDC hdc, WCHAR *pwOutChars, INT *pcChars, const Co
     }
 }
 
-static void Reorder_Ra_follows_base(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Ra_follows_base(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     if (s->ralf >= 0)
     {
@@ -1208,7 +1743,7 @@ static void Reorder_Ra_follows_base(LPWSTR pwChar, IndicSyllable *s, lexical_fun
     }
 }
 
-static void Reorder_Ra_follows_matra(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Ra_follows_matra(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     if (s->ralf >= 0)
     {
@@ -1233,7 +1768,7 @@ static void Reorder_Ra_follows_matra(LPWSTR pwChar, IndicSyllable *s, lexical_fu
     }
 }
 
-static void Reorder_Ra_follows_syllable(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Ra_follows_syllable(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     if (s->ralf >= 0)
     {
@@ -1254,7 +1789,7 @@ static void Reorder_Ra_follows_syllable(LPWSTR pwChar, IndicSyllable *s, lexical
     }
 }
 
-static void Reorder_Matra_precede_base(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Matra_precede_base(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     int i;
 
@@ -1281,7 +1816,7 @@ static void Reorder_Matra_precede_base(LPWSTR pwChar, IndicSyllable *s, lexical_
     }
 }
 
-static void Reorder_Matra_precede_syllable(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Matra_precede_syllable(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     int i;
 
@@ -1308,14 +1843,16 @@ static void Reorder_Matra_precede_syllable(LPWSTR pwChar, IndicSyllable *s, lexi
     }
 }
 
-static void SecondReorder_Blwf_follows_matra(LPWSTR pwChar, IndicSyllable *s, WORD *glyphs, IndicSyllable *g, lexical_function lexical)
+static void SecondReorder_Blwf_follows_matra(const WCHAR *chars, const IndicSyllable *s,
+        WORD *glyphs, const IndicSyllable *g, lexical_function lexical)
 {
     if (s->blwf >= 0 && g->blwf > g->base)
     {
         int j,loc;
         int g_offset;
         for (loc = s->end; loc > s->blwf; loc--)
-            if (lexical(pwChar[loc]) == lex_Matra_below || lexical(pwChar[loc]) == lex_Matra_above || lexical(pwChar[loc]) == lex_Matra_post)
+            if (lexical(chars[loc]) == lex_Matra_below || lexical(chars[loc]) == lex_Matra_above
+                    || lexical(chars[loc]) == lex_Matra_post)
                 break;
 
         g_offset = (loc - s->blwf) - 1;
@@ -1332,14 +1869,15 @@ static void SecondReorder_Blwf_follows_matra(LPWSTR pwChar, IndicSyllable *s, WO
     }
 }
 
-static void SecondReorder_Matra_precede_base(LPWSTR pwChar, IndicSyllable *s, WORD *glyphs, IndicSyllable *g, lexical_function lexical)
+static void SecondReorder_Matra_precede_base(const WCHAR *chars, const IndicSyllable *s,
+        WORD *glyphs, const IndicSyllable *g, lexical_function lexical)
 {
     int i;
 
     /* reorder previously moved Matras to correct position*/
     for (i = s->start; i < s->base; i++)
     {
-        if (lexical(pwChar[i]) == lex_Matra_pre)
+        if (lexical(chars[i]) == lex_Matra_pre)
         {
             int j;
             int g_start = g->start + i - s->start;
@@ -1355,7 +1893,8 @@ static void SecondReorder_Matra_precede_base(LPWSTR pwChar, IndicSyllable *s, WO
     }
 }
 
-static void SecondReorder_Pref_precede_base(LPWSTR pwChar, IndicSyllable *s, WORD *glyphs, IndicSyllable *g, lexical_function lexical)
+static void SecondReorder_Pref_precede_base(const IndicSyllable *s,
+        WORD *glyphs, const IndicSyllable *g, lexical_function lexical)
 {
     if (s->pref >= 0 && g->pref > g->base)
     {
@@ -1368,7 +1907,7 @@ static void SecondReorder_Pref_precede_base(LPWSTR pwChar, IndicSyllable *s, WOR
     }
 }
 
-static void Reorder_Like_Sinhala(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Like_Sinhala(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     if (s->start == s->base && s->base == s->end)  return;
@@ -1378,7 +1917,7 @@ static void Reorder_Like_Sinhala(LPWSTR pwChar, IndicSyllable *s, lexical_functi
     Reorder_Matra_precede_base(pwChar, s, lexical);
 }
 
-static void Reorder_Like_Devanagari(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Like_Devanagari(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     if (s->start == s->base && s->base == s->end)  return;
@@ -1388,7 +1927,7 @@ static void Reorder_Like_Devanagari(LPWSTR pwChar, IndicSyllable *s, lexical_fun
     Reorder_Matra_precede_syllable(pwChar, s, lexical);
 }
 
-static void Reorder_Like_Bengali(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Like_Bengali(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     if (s->start == s->base && s->base == s->end)  return;
@@ -1398,7 +1937,7 @@ static void Reorder_Like_Bengali(LPWSTR pwChar, IndicSyllable *s, lexical_functi
     Reorder_Matra_precede_syllable(pwChar, s, lexical);
 }
 
-static void Reorder_Like_Kannada(LPWSTR pwChar, IndicSyllable *s, lexical_function lexical)
+static void Reorder_Like_Kannada(WCHAR *pwChar, IndicSyllable *s, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     if (s->start == s->base && s->base == s->end)  return;
@@ -1408,25 +1947,27 @@ static void Reorder_Like_Kannada(LPWSTR pwChar, IndicSyllable *s, lexical_functi
     Reorder_Matra_precede_syllable(pwChar, s, lexical);
 }
 
-static void SecondReorder_Like_Telugu(LPWSTR pwChar, IndicSyllable *s, WORD* pwGlyphs, IndicSyllable *g, lexical_function lexical)
+static void SecondReorder_Like_Telugu(const WCHAR *chars, const IndicSyllable *s,
+        WORD *glyphs, const IndicSyllable *g, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     TRACE("Glyphs (%i..%i..%i)\n",g->start,g->base,g->end);
     if (s->start == s->base && s->base == s->end)  return;
-    if (lexical(pwChar[s->base]) == lex_Vowel) return;
+    if (lexical(chars[s->base]) == lex_Vowel) return;
 
-    SecondReorder_Blwf_follows_matra(pwChar, s, pwGlyphs, g, lexical);
+    SecondReorder_Blwf_follows_matra(chars, s, glyphs, g, lexical);
 }
 
-static void SecondReorder_Like_Tamil(LPWSTR pwChar, IndicSyllable *s, WORD* pwGlyphs, IndicSyllable *g, lexical_function lexical)
+static void SecondReorder_Like_Tamil(const WCHAR *chars, const IndicSyllable *s,
+        WORD *glyphs, const IndicSyllable *g, lexical_function lexical)
 {
     TRACE("Syllable (%i..%i..%i)\n",s->start,s->base,s->end);
     TRACE("Glyphs (%i..%i..%i)\n",g->start,g->base,g->end);
     if (s->start == s->base && s->base == s->end)  return;
-    if (lexical(pwChar[s->base]) == lex_Vowel) return;
+    if (lexical(chars[s->base]) == lex_Vowel) return;
 
-    SecondReorder_Matra_precede_base(pwChar, s, pwGlyphs, g, lexical);
-    SecondReorder_Pref_precede_base(pwChar, s, pwGlyphs, g, lexical);
+    SecondReorder_Matra_precede_base(chars, s, glyphs, g, lexical);
+    SecondReorder_Pref_precede_base(s, glyphs, g, lexical);
 }
 
 
@@ -1586,17 +2127,17 @@ static void ShapeIndicSyllables(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa,
 {
     int c;
     int overall_shift = 0;
-    LoadedFeature *locl = (modern)?load_GSUB_feature(hdc, psa, psc, "locl"):NULL;
-    LoadedFeature *nukt = load_GSUB_feature(hdc, psa, psc, "nukt");
-    LoadedFeature *akhn = load_GSUB_feature(hdc, psa, psc, "akhn");
-    LoadedFeature *rkrf = (modern)?load_GSUB_feature(hdc, psa, psc, "rkrf"):NULL;
-    LoadedFeature *pstf = load_GSUB_feature(hdc, psa, psc, "pstf");
-    LoadedFeature *vatu = (!rkrf)?load_GSUB_feature(hdc, psa, psc, "vatu"):NULL;
-    LoadedFeature *cjct = (modern)?load_GSUB_feature(hdc, psa, psc, "cjct"):NULL;
-    BOOL rphf = (load_GSUB_feature(hdc, psa, psc, "rphf") != NULL);
-    BOOL pref = (load_GSUB_feature(hdc, psa, psc, "pref") != NULL);
-    BOOL blwf = (load_GSUB_feature(hdc, psa, psc, "blwf") != NULL);
-    BOOL half = (load_GSUB_feature(hdc, psa, psc, "half") != NULL);
+    LoadedFeature *locl = (modern)?load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "locl"):NULL;
+    LoadedFeature *nukt = load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "nukt");
+    LoadedFeature *akhn = load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "akhn");
+    LoadedFeature *rkrf = (modern)?load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "rkrf"):NULL;
+    LoadedFeature *pstf = load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "pstf");
+    LoadedFeature *vatu = (!rkrf)?load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "vatu"):NULL;
+    LoadedFeature *cjct = (modern)?load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "cjct"):NULL;
+    BOOL rphf = (load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "rphf") != NULL);
+    BOOL pref = (load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "pref") != NULL);
+    BOOL blwf = (load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "blwf") != NULL);
+    BOOL half = (load_OT_feature(hdc, psa, psc, FEATURE_GSUB_TABLE, "half") != NULL);
     IndicSyllable glyph_indexs;
 
     for (c = 0; c < syllable_count; c++)
@@ -1680,7 +2221,7 @@ static inline int unicode_lex(WCHAR c)
     switch( type )
     {
         case 0x0d07: /* Unknown */
-        case 0x0e07: /* Unknwon */
+        case 0x0e07: /* Unknown */
         default: return lex_Generic;
         case 0x0001:
         case 0x0002:
@@ -1712,7 +2253,8 @@ static inline int unicode_lex(WCHAR c)
         case 0x0407: return lex_Composed_Vowel;
         case 0x0507: return lex_Matra_above;
         case 0x0607: return lex_Matra_below;
-        case 0x000c: return lex_Ra;
+        case 0x000c:
+        case 0x0015: return lex_Ra;
     };
 }
 
@@ -1750,7 +2292,7 @@ static void ContextualShape_Sinhala(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * (cChars * 3));
+    input = heap_alloc(3 * cChars * sizeof(*input));
 
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
@@ -1776,8 +2318,8 @@ static void ContextualShape_Sinhala(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
     *pcGlyphs = cCount;
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, sinhala_lex, NULL, TRUE);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int devanagari_lex(WCHAR c)
@@ -1817,7 +2359,7 @@ static void ContextualShape_Devanagari(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
+    input = heap_alloc(cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Compose Consonant and Nukta */
@@ -1833,8 +2375,8 @@ static void ContextualShape_Devanagari(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, devanagari_lex, NULL, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int bengali_lex(WCHAR c)
@@ -1873,10 +2415,10 @@ static void ContextualShape_Bengali(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars * 2) * sizeof(WCHAR));
+    input = heap_alloc(2 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
-    /* Step 1: Decompose Vowels and Compose Consonents */
+    /* Step 1: Decompose Vowels and Compose Consonants */
     DecomposeVowels(hdc, input,  &cCount, Bengali_vowels, pwLogClust, cChars);
     ComposeConsonants(hdc, input, &cCount, Bengali_consonants, pwLogClust);
     TRACE("New composed string %s (%i)\n",debugstr_wn(input,cCount),cCount);
@@ -1903,8 +2445,8 @@ static void ContextualShape_Bengali(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
     /* Step 4: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, bengali_lex, NULL, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int gurmukhi_lex(WCHAR c)
@@ -1937,10 +2479,10 @@ static void ContextualShape_Gurmukhi(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
+    input = heap_alloc(cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
-    /* Step 1: Compose Consonents */
+    /* Step 1: Compose Consonants */
     ComposeConsonants(hdc, input, &cCount, Gurmukhi_consonants, pwLogClust);
     TRACE("New composed string %s (%i)\n",debugstr_wn(input,cCount),cCount);
 
@@ -1953,8 +2495,8 @@ static void ContextualShape_Gurmukhi(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, gurmukhi_lex, NULL, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int gujarati_lex(WCHAR c)
@@ -1981,7 +2523,7 @@ static void ContextualShape_Gujarati(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
+    input = heap_alloc(cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Reorder within Syllables */
@@ -1993,8 +2535,8 @@ static void ContextualShape_Gujarati(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     /* Step 2: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, gujarati_lex, NULL, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int oriya_lex(WCHAR c)
@@ -2032,10 +2574,10 @@ static void ContextualShape_Oriya(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
+    input = heap_alloc(2 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
-    /* Step 1: Decompose Vowels and Compose Consonents */
+    /* Step 1: Decompose Vowels and Compose Consonants */
     DecomposeVowels(hdc, input,  &cCount, Oriya_vowels, pwLogClust, cChars);
     ComposeConsonants(hdc, input, &cCount, Oriya_consonants, pwLogClust);
     TRACE("New composed string %s (%i)\n",debugstr_wn(input,cCount),cCount);
@@ -2049,8 +2591,8 @@ static void ContextualShape_Oriya(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, oriya_lex, NULL, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int tamil_lex(WCHAR c)
@@ -2082,10 +2624,10 @@ static void ContextualShape_Tamil(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
+    input = heap_alloc(2 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
-    /* Step 1: Decompose Vowels and Compose Consonents */
+    /* Step 1: Decompose Vowels and Compose Consonants */
     DecomposeVowels(hdc, input,  &cCount, Tamil_vowels, pwLogClust, cChars);
     ComposeConsonants(hdc, input, &cCount, Tamil_consonants, pwLogClust);
     TRACE("New composed string %s (%i)\n",debugstr_wn(input,cCount),cCount);
@@ -2099,8 +2641,8 @@ static void ContextualShape_Tamil(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, tamil_lex, SecondReorder_Like_Tamil, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int telugu_lex(WCHAR c)
@@ -2132,7 +2674,7 @@ static void ContextualShape_Telugu(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
+    input = heap_alloc(2 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Decompose Vowels */
@@ -2148,8 +2690,8 @@ static void ContextualShape_Telugu(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, telugu_lex, SecondReorder_Like_Telugu, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int kannada_lex(WCHAR c)
@@ -2184,7 +2726,7 @@ static void ContextualShape_Kannada(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars*3) * sizeof(WCHAR));
+    input = heap_alloc(3 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Decompose Vowels */
@@ -2200,8 +2742,8 @@ static void ContextualShape_Kannada(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, kannada_lex, SecondReorder_Like_Telugu, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int malayalam_lex(WCHAR c)
@@ -2229,7 +2771,7 @@ static void ContextualShape_Malayalam(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
+    input = heap_alloc(2 * cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Decompose Vowels */
@@ -2245,8 +2787,8 @@ static void ContextualShape_Malayalam(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     /* Step 3: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, malayalam_lex, SecondReorder_Like_Tamil, modern);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static int khmer_lex(WCHAR c)
@@ -2267,7 +2809,7 @@ static void ContextualShape_Khmer(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
         return;
     }
 
-    input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
+    input = heap_alloc(cChars * sizeof(*input));
     memcpy(input, pwcChars, cChars * sizeof(WCHAR));
 
     /* Step 1: Reorder within Syllables */
@@ -2279,8 +2821,8 @@ static void ContextualShape_Khmer(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
     /* Step 2: Base Form application to syllables */
     ShapeIndicSyllables(hdc, psc, psa, input, cChars, syllables, syllable_count, pwOutGlyphs, pcGlyphs, pwLogClust, khmer_lex, NULL, FALSE);
 
-    HeapFree(GetProcessHeap(),0,input);
-    HeapFree(GetProcessHeap(),0,syllables);
+    heap_free(input);
+    heap_free(syllables);
 }
 
 static inline BOOL mongolian_wordbreak(WCHAR chr)
@@ -2293,6 +2835,8 @@ static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     INT *context_shape;
     INT dirL;
     int i;
+    int char_index;
+    int glyph_index;
 
     if (*pcGlyphs != cChars)
     {
@@ -2305,13 +2849,10 @@ static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     else
         dirL = 1;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
-
     if (!psc->GSUB_Table)
         return;
 
-    context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
+    context_shape = heap_alloc(cChars * sizeof(*context_shape));
 
     for (i = 0; i < cChars; i++)
     {
@@ -2332,25 +2873,34 @@ static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     }
 
     /* Contextual Shaping */
-    i = 0;
-    while(i < *pcGlyphs)
+    if (dirL > 0)
+        char_index = glyph_index = 0;
+    else
+        char_index = glyph_index = cChars-1;
+
+    while(char_index < cChars && char_index >= 0)
     {
         INT nextIndex;
         INT prevCount = *pcGlyphs;
-        nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
+        nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, glyph_index, dirL, pcGlyphs, contextual_features[context_shape[char_index]]);
+
         if (nextIndex > GSUB_E_NOGLYPH)
         {
             UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
-            i = nextIndex;
+            glyph_index = nextIndex;
+            char_index += dirL;
         }
         else
-            i++;
+        {
+            char_index += dirL;
+            glyph_index += dirL;
+        }
     }
 
-    HeapFree(GetProcessHeap(),0,context_shape);
+    heap_free(context_shape);
 }
 
-static void ShapeCharGlyphProp_Default( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS* psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD* pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP* pGlyphProp)
+static void ShapeCharGlyphProp_Default( ScriptCache* psc, SCRIPT_ANALYSIS* psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD* pwLogClust, SCRIPT_CHARPROP* pCharProp, SCRIPT_GLYPHPROP* pGlyphProp)
 {
     int i,k;
 
@@ -2378,10 +2928,37 @@ static void ShapeCharGlyphProp_Default( HDC hdc, ScriptCache* psc, SCRIPT_ANALYS
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
     }
 
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
 }
 
+static void ShapeCharGlyphProp_Latin( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
+{
+    int i;
+
+    ShapeCharGlyphProp_Default( psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp);
+
+    for (i = 0; i < cGlyphs; i++)
+        if (pGlyphProp[i].sva.fZeroWidth)
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
+}
+
+static void ShapeCharGlyphProp_Control( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
+{
+    int i;
+    for (i = 0; i < cGlyphs; i++)
+    {
+        pGlyphProp[i].sva.fClusterStart = 1;
+        pGlyphProp[i].sva.fDiacritic = 0;
+        pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_BLANK;
+
+        if (pwGlyphs[i] == psc->sfp.wgDefault)
+            pGlyphProp[i].sva.fZeroWidth = 0;
+        else
+            pGlyphProp[i].sva.fZeroWidth = 1;
+    }
+}
+
 static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
 {
     int i,k;
@@ -2389,23 +2966,23 @@ static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
     INT dirR, dirL;
     BYTE *spaces;
 
-    spaces = HeapAlloc(GetProcessHeap(),0,cGlyphs);
+    spaces = heap_alloc(cGlyphs);
     memset(spaces,0,cGlyphs);
 
-    if (!psa->fLogicalOrder && psa->fRTL)
-    {
-        initGlyph = cGlyphs-1;
-        finaGlyph = 0;
-        dirR = 1;
-        dirL = -1;
-    }
-    else
+    if (psa->fLogicalOrder && psa->fRTL)
     {
         initGlyph = 0;
         finaGlyph = cGlyphs-1;
         dirR = -1;
         dirL = 1;
     }
+    else
+    {
+        initGlyph = cGlyphs-1;
+        finaGlyph = 0;
+        dirR = 1;
+        dirL = -1;
+    }
 
     for (i = 0; i < cGlyphs; i++)
     {
@@ -2487,20 +3064,46 @@ static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
 
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
-    HeapFree(GetProcessHeap(),0,spaces);
+    heap_free(spaces);
 }
 
-static void ShapeCharGlyphProp_Thai( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
+static void ShapeCharGlyphProp_Hebrew( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
 {
     int i,k;
+
+    for (i = 0; i < cGlyphs; i++)
+    {
+        int char_index[20];
+        int char_count = 0;
+
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
+        {
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
+        }
+
+        if (char_count == 0)
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
+        else
+        {
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
+            if (char_count ==1 && pwcChars[char_index[0]] == 0x0020)  /* space */
+                pCharProp[char_index[0]].fCanGlyphAlone = 1;
+        }
+    }
+
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
+}
+
+static void ShapeCharGlyphProp_Thai( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp )
+{
+    int i;
     int finaGlyph;
     INT dirL;
-    BYTE *spaces;
-
-    spaces = HeapAlloc(GetProcessHeap(),0,cGlyphs);
-    memset(spaces,0,cGlyphs);
 
     if (!psa->fLogicalOrder && psa->fRTL)
     {
@@ -2513,20 +3116,11 @@ static void ShapeCharGlyphProp_Thai( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
         dirL = 1;
     }
 
-    for (i = 0; i < cGlyphs; i++)
-    {
-        for (k = 0; k < cChars; k++)
-            if (pwLogClust[k] == i)
-            {
-                if (pwcChars[k] == 0x0020)
-                    spaces[i] = 1;
-            }
-    }
-
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
 
     for (i = 0; i < cGlyphs; i++)
     {
+        int k;
         int char_index[20];
         int char_count = 0;
 
@@ -2537,25 +3131,22 @@ static void ShapeCharGlyphProp_Thai( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
                 char_index[char_count++] = k;
         }
 
+        if (i == finaGlyph)
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
+        else
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
+
         if (char_count == 0)
             continue;
 
         if (char_count ==1 && pwcChars[char_index[0]] == 0x0020)  /* space */
-        {
-            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
             pCharProp[char_index[0]].fCanGlyphAlone = 1;
-        }
-        else if (i == finaGlyph)
-            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
-        else
-            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
 
         /* handle Thai SARA AM (U+0E33) differently than GDEF */
         if (char_count == 1 && pwcChars[char_index[0]] == 0x0e33)
             pGlyphProp[i].sva.fClusterStart = 0;
     }
 
-    HeapFree(GetProcessHeap(),0,spaces);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
 
     /* Do not allow justification between marks and their base */
@@ -2593,7 +3184,7 @@ static void ShapeCharGlyphProp_None( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS*
         else
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
 }
 
@@ -2624,7 +3215,7 @@ static void ShapeCharGlyphProp_Tibet( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS
         else
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
 
     /* Tibeten script does not set sva.fDiacritic or sva.fZeroWidth */
@@ -2642,7 +3233,7 @@ static void ShapeCharGlyphProp_BaseIndic( HDC hdc, ScriptCache *psc, SCRIPT_ANAL
 {
     int i,k;
 
-    OpenType_GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     for (i = 0; i < cGlyphs; i++)
     {
         int char_index[20];
@@ -2663,7 +3254,10 @@ static void ShapeCharGlyphProp_BaseIndic( HDC hdc, ScriptCache *psc, SCRIPT_ANAL
         }
 
         if (char_count == 0)
+        {
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
             continue;
+        }
 
         if (char_count ==1 && pwcChars[char_index[0]] == 0x0020)  /* space */
         {
@@ -2720,7 +3314,7 @@ static void ShapeCharGlyphProp_BaseIndic( HDC hdc, ScriptCache *psc, SCRIPT_ANAL
             }
         }
 
-        HeapFree(GetProcessHeap(), 0, syllables);
+        heap_free(syllables);
     }
 
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
@@ -2783,16 +3377,17 @@ static void ShapeCharGlyphProp_Khmer( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
 
 void SHAPE_CharGlyphProp(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WCHAR* pwcChars, const INT cChars, const WORD* pwGlyphs, const INT cGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProp, SCRIPT_GLYPHPROP *pGlyphProp)
 {
+    load_ot_tables(hdc, psc);
+
     if (ShapingData[psa->eScript].charGlyphPropProc)
         ShapingData[psa->eScript].charGlyphPropProc(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp);
     else
-        ShapeCharGlyphProp_Default(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp);
+        ShapeCharGlyphProp_Default(psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp);
 }
 
 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
 {
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (ShapingData[psa->eScript].contextProc)
         ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
@@ -2806,13 +3401,12 @@ static void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYS
     if (!rpRangeProperties)
         return;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (!psc->GSUB_Table)
         return;
 
-    if (!psa->fLogicalOrder && psa->fRTL)
+    if (scriptInformation[psa->eScript].a.fRTL && (!psa->fLogicalOrder || !psa->fRTL))
         dirL = -1;
     else
         dirL = 1;
@@ -2832,6 +3426,31 @@ rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
     SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);
 }
 
+void SHAPE_ApplyOpenTypePositions(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, const WORD* pwGlyphs, INT cGlyphs, int *piAdvance, GOFFSET *pGoffset )
+{
+    const TEXTRANGE_PROPERTIES *rpRangeProperties = &ShapingData[psa->eScript].defaultGPOSTextRange;
+    int i;
+
+    load_ot_tables(hdc, psc);
+
+    if (!psc->GPOS_Table || !psc->otm)
+        return;
+
+    for (i = 0; i < rpRangeProperties->cotfRecords; i++)
+    {
+        if (rpRangeProperties->potfRecords[i].lParameter > 0)
+        {
+            LoadedFeature *feature;
+
+            feature = load_OT_feature(hdc, psa, psc, FEATURE_GPOS_TABLE, (const char*)&rpRangeProperties->potfRecords[i].tagFeature);
+            if (!feature)
+                continue;
+
+            GPOS_apply_feature(psc, psc->otm, &psc->lf, psa, piAdvance, feature, pwGlyphs, cGlyphs, pGoffset);
+        }
+    }
+}
+
 HRESULT SHAPE_CheckFontForRequiredFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa)
 {
     LoadedFeature *feature;
@@ -2840,14 +3459,13 @@ HRESULT SHAPE_CheckFontForRequiredFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANA
     if (!ShapingData[psa->eScript].requiredFeatures)
         return S_OK;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     /* we need to have at least one of the required features */
     i = 0;
     while (ShapingData[psa->eScript].requiredFeatures[i])
     {
-        feature = load_GSUB_feature(hdc, psa, psc, ShapingData[psa->eScript].requiredFeatures[i]);
+        feature = load_OT_feature(hdc, psa, psc, FEATURE_ALL_TABLES, ShapingData[psa->eScript].requiredFeatures[i]);
         if (feature)
             return S_OK;
         i++;
@@ -2863,13 +3481,12 @@ HRESULT SHAPE_GetFontScriptTags( HDC hdc, ScriptCache *psc,
     HRESULT hr;
     OPENTYPE_TAG searching = 0x00000000;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (psa && scriptInformation[psa->eScript].scriptTag)
         searching = scriptInformation[psa->eScript].scriptTag;
 
-    hr = OpenType_GSUB_GetFontScriptTags(psc, searching, cMaxTags, pScriptTags, pcTags, NULL);
+    hr = OpenType_GetFontScriptTags(psc, searching, cMaxTags, pScriptTags, pcTags);
     if (FAILED(hr))
         *pcTags = 0;
     return hr;
@@ -2884,17 +3501,16 @@ HRESULT SHAPE_GetFontLanguageTags( HDC hdc, ScriptCache *psc,
     OPENTYPE_TAG searching = 0x00000000;
     BOOL fellback = FALSE;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (psa && psc->userLang != 0)
         searching = psc->userLang;
 
-    hr = OpenType_GSUB_GetFontLanguageTags(psc, tagScript, searching, cMaxTags, pLangSysTags, pcTags, NULL);
+    hr = OpenType_GetFontLanguageTags(psc, tagScript, searching, cMaxTags, pLangSysTags, pcTags);
     if (FAILED(hr))
     {
         fellback = TRUE;
-        hr = OpenType_GSUB_GetFontLanguageTags(psc, MS_MAKE_TAG('l','a','t','n'), searching, cMaxTags, pLangSysTags, pcTags, NULL);
+        hr = OpenType_GetFontLanguageTags(psc, MS_MAKE_TAG('l','a','t','n'), searching, cMaxTags, pLangSysTags, pcTags);
     }
 
     if (FAILED(hr) || fellback)
@@ -2912,8 +3528,7 @@ HRESULT SHAPE_GetFontFeatureTags( HDC hdc, ScriptCache *psc,
     HRESULT hr;
     BOOL filter = FALSE;
 
-    if (!psc->GSUB_Table)
-        psc->GSUB_Table = load_gsub_table(hdc);
+    load_ot_tables(hdc, psc);
 
     if (psa && scriptInformation[psa->eScript].scriptTag)
     {
@@ -2921,7 +3536,7 @@ HRESULT SHAPE_GetFontFeatureTags( HDC hdc, ScriptCache *psc,
         filter = TRUE;
     }
 
-    hr = OpenType_GSUB_GetFontFeatureTags(psc, tagScript, tagLangSys, filter, 0x00000000, cMaxTags, pFeatureTags, pcTags, NULL);
+    hr = OpenType_GetFontFeatureTags(psc, tagScript, tagLangSys, filter, 0x00000000, FEATURE_ALL_TABLES, cMaxTags, pFeatureTags, pcTags, NULL);
 
     if (FAILED(hr))
         *pcTags = 0;