[USP10]
[reactos.git] / reactos / dll / win32 / usp10 / shape.c
index 8d3a355..e7d1558 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
  */
-#include <stdarg.h>
-
-#include "windef.h"
-#include "winbase.h"
-#include "wingdi.h"
-#include "winuser.h"
-#include "winnls.h"
-#include "usp10.h"
-#include "winternl.h"
 
 #include "usp10_internal.h"
 
-#include "wine/debug.h"
-
 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
 
 #define FIRST_ARABIC_CHAR 0x0600
@@ -40,9 +29,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);
@@ -53,11 +47,16 @@ static void ContextualShape_Tamil(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
 static void ContextualShape_Telugu(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Kannada(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 static void ContextualShape_Malayalam(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Khmer(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
+static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
 
 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 );
@@ -71,6 +70,7 @@ static void ShapeCharGlyphProp_Tamil( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
 static void ShapeCharGlyphProp_Telugu( 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_Kannada( 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_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[];
@@ -96,221 +96,6 @@ enum joined_forms {
     Afx
 };
 
-#ifdef WORDS_BIGENDIAN
-#define GET_BE_WORD(x) (x)
-#else
-#define GET_BE_WORD(x) RtlUshortByteSwap(x)
-#endif
-
-/* These are all structures needed for the GSUB table */
-#define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
-#define GSUB_E_NOFEATURE -2
-#define GSUB_E_NOGLYPH -1
-
-typedef struct {
-    DWORD version;
-    WORD ScriptList;
-    WORD FeatureList;
-    WORD LookupList;
-} GSUB_Header;
-
-typedef struct {
-    CHAR ScriptTag[4];
-    WORD Script;
-} GSUB_ScriptRecord;
-
-typedef struct {
-    WORD ScriptCount;
-    GSUB_ScriptRecord ScriptRecord[1];
-} GSUB_ScriptList;
-
-typedef struct {
-    CHAR LangSysTag[4];
-    WORD LangSys;
-} GSUB_LangSysRecord;
-
-typedef struct {
-    WORD DefaultLangSys;
-    WORD LangSysCount;
-    GSUB_LangSysRecord LangSysRecord[1];
-} GSUB_Script;
-
-typedef struct {
-    WORD LookupOrder; /* Reserved */
-    WORD ReqFeatureIndex;
-    WORD FeatureCount;
-    WORD FeatureIndex[1];
-} GSUB_LangSys;
-
-typedef struct {
-    CHAR FeatureTag[4];
-    WORD Feature;
-} GSUB_FeatureRecord;
-
-typedef struct {
-    WORD FeatureCount;
-    GSUB_FeatureRecord FeatureRecord[1];
-} GSUB_FeatureList;
-
-typedef struct {
-    WORD FeatureParams; /* Reserved */
-    WORD LookupCount;
-    WORD LookupListIndex[1];
-} GSUB_Feature;
-
-typedef struct {
-    WORD LookupCount;
-    WORD Lookup[1];
-} GSUB_LookupList;
-
-typedef struct {
-    WORD LookupType;
-    WORD LookupFlag;
-    WORD SubTableCount;
-    WORD SubTable[1];
-} GSUB_LookupTable;
-
-typedef struct {
-    WORD CoverageFormat;
-    WORD GlyphCount;
-    WORD GlyphArray[1];
-} GSUB_CoverageFormat1;
-
-typedef struct {
-    WORD Start;
-    WORD End;
-    WORD StartCoverageIndex;
-} GSUB_RangeRecord;
-
-typedef struct {
-    WORD CoverageFormat;
-    WORD RangeCount;
-    GSUB_RangeRecord RangeRecord[1];
-} GSUB_CoverageFormat2;
-
-typedef struct {
-    WORD SubstFormat; /* = 1 */
-    WORD Coverage;
-    WORD DeltaGlyphID;
-} GSUB_SingleSubstFormat1;
-
-typedef struct {
-    WORD SubstFormat; /* = 2 */
-    WORD Coverage;
-    WORD GlyphCount;
-    WORD Substitute[1];
-}GSUB_SingleSubstFormat2;
-
-typedef struct {
-    WORD SubstFormat; /* = 1 */
-    WORD Coverage;
-    WORD SequenceCount;
-    WORD Sequence[1];
-}GSUB_MultipleSubstFormat1;
-
-typedef struct {
-    WORD GlyphCount;
-    WORD Substitute[1];
-}GSUB_Sequence;
-
-typedef struct {
-    WORD SubstFormat; /* = 1 */
-    WORD Coverage;
-    WORD LigSetCount;
-    WORD LigatureSet[1];
-}GSUB_LigatureSubstFormat1;
-
-typedef struct {
-    WORD LigatureCount;
-    WORD Ligature[1];
-}GSUB_LigatureSet;
-
-typedef struct{
-    WORD LigGlyph;
-    WORD CompCount;
-    WORD Component[1];
-}GSUB_Ligature;
-
-typedef struct{
-    WORD SequenceIndex;
-    WORD LookupListIndex;
-
-}GSUB_SubstLookupRecord;
-
-typedef struct{
-    WORD SubstFormat; /* = 1 */
-    WORD Coverage;
-    WORD ChainSubRuleSetCount;
-    WORD ChainSubRuleSet[1];
-}GSUB_ChainContextSubstFormat1;
-
-typedef struct {
-    WORD SubstFormat; /* = 3 */
-    WORD BacktrackGlyphCount;
-    WORD Coverage[1];
-}GSUB_ChainContextSubstFormat3_1;
-
-typedef struct{
-    WORD InputGlyphCount;
-    WORD Coverage[1];
-}GSUB_ChainContextSubstFormat3_2;
-
-typedef struct{
-    WORD LookaheadGlyphCount;
-    WORD Coverage[1];
-}GSUB_ChainContextSubstFormat3_3;
-
-typedef struct{
-    WORD SubstCount;
-    GSUB_SubstLookupRecord SubstLookupRecord[1];
-}GSUB_ChainContextSubstFormat3_4;
-
-typedef struct {
-    WORD SubstFormat; /* = 1 */
-    WORD Coverage;
-    WORD AlternateSetCount;
-    WORD AlternateSet[1];
-} GSUB_AlternateSubstFormat1;
-
-typedef struct{
-    WORD GlyphCount;
-    WORD Alternate[1];
-} GSUB_AlternateSet;
-
-/* These are all structures needed for the GDEF table */
-#define GDEF_TAG MS_MAKE_TAG('G', 'D', 'E', 'F')
-
-enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
-
-typedef struct {
-    DWORD Version;
-    WORD GlyphClassDef;
-    WORD AttachList;
-    WORD LigCaretList;
-    WORD MarkAttachClassDef;
-} GDEF_Header;
-
-typedef struct {
-    WORD ClassFormat;
-    WORD StartGlyph;
-    WORD GlyphCount;
-    WORD ClassValueArray[1];
-} GDEF_ClassDefFormat1;
-
-typedef struct {
-    WORD Start;
-    WORD End;
-    WORD Class;
-} GDEF_ClassRangeRecord;
-
-typedef struct {
-    WORD ClassFormat;
-    WORD ClassRangeCount;
-    GDEF_ClassRangeRecord ClassRangeRecord[1];
-} GDEF_ClassDefFormat2;
-
-static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
-
 typedef struct tagVowelComponents
 {
     WCHAR base;
@@ -325,6 +110,8 @@ typedef struct tagConsonantComponents
 
 typedef void (*second_reorder_function)(LPWSTR pwChar, IndicSyllable *syllable,WORD* pwGlyphs, 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[] =
 {
@@ -346,10 +133,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},
@@ -369,11 +165,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},
@@ -394,6 +205,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 */
@@ -408,6 +226,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},
@@ -420,6 +244,13 @@ static OPENTYPE_FEATURE_RECORD thai_features[] =
     { MS_MAKE_TAG('c','c','m','p'), 1},
 };
 
+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* required_lao_features[] =
 {
     "ccmp",
@@ -452,6 +283,20 @@ 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[] =
 {
     "nukt",
@@ -539,513 +384,160 @@ static const char* required_telugu_features[] =
     NULL
 };
 
-typedef struct ScriptShapeDataTag {
-    TEXTRANGE_PROPERTIES   defaultTextRange;
-    const char**           requiredFeatures;
-    CHAR                   otTag[5];
-    CHAR                   newOtTag[5];
-    ContextualShapingProc  contextProc;
-    ShapeCharGlyphPropProc charGlyphPropProc;
-} ScriptShapeData;
-
-/* in order of scripts */
-static const ScriptShapeData ShapingData[] =
+static OPENTYPE_FEATURE_RECORD khmer_features[] =
 {
-    {{ standard_features, 2}, NULL, "", "", NULL, NULL},
-    {{ latin_features, 2}, NULL, "latn", "", NULL, NULL},
-    {{ latin_features, 2}, NULL, "latn", "", NULL, NULL},
-    {{ latin_features, 2}, NULL, "latn", "", NULL, NULL},
-    {{ standard_features, 2}, NULL, "" , "", NULL, NULL},
-    {{ latin_features, 2}, NULL, "latn", "", NULL, NULL},
-    {{ arabic_features, 6}, required_arabic_features, "arab", "", ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ arabic_features, 6}, required_arabic_features, "arab", "", ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ hebrew_features, 1}, NULL, "hebr", "", NULL, NULL},
-    {{ syriac_features, 4}, required_syriac_features, "syrc", "", ContextualShape_Syriac, ShapeCharGlyphProp_None},
-    {{ arabic_features, 6}, required_arabic_features, "arab", "", ContextualShape_Arabic, ShapeCharGlyphProp_Arabic},
-    {{ NULL, 0}, NULL, "thaa", "", NULL, ShapeCharGlyphProp_None},
-    {{ standard_features, 2}, NULL, "grek", "", NULL, NULL},
-    {{ standard_features, 2}, NULL, "cyrl", "", NULL, NULL},
-    {{ standard_features, 2}, NULL, "armn", "", NULL, NULL},
-    {{ standard_features, 2}, NULL, "geor", "", NULL, NULL},
-    {{ sinhala_features, 3}, NULL, "sinh", "", ContextualShape_Sinhala, ShapeCharGlyphProp_Sinhala},
-    {{ tibetan_features, 2}, NULL, "tibt", "", NULL, ShapeCharGlyphProp_Tibet},
-    {{ tibetan_features, 2}, NULL, "tibt", "", NULL, ShapeCharGlyphProp_Tibet},
-    {{ phags_features, 3}, NULL, "phag", "", ContextualShape_Phags_pa, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, NULL, "thai", "", NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, NULL, "thai", "", NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, required_lao_features, "lao", "", NULL, ShapeCharGlyphProp_Thai},
-    {{ thai_features, 1}, required_lao_features, "lao", "", NULL, ShapeCharGlyphProp_Thai},
-    {{ devanagari_features, 6}, required_devanagari_features, "deva", "dev2", ContextualShape_Devanagari, ShapeCharGlyphProp_Devanagari},
-    {{ devanagari_features, 6}, required_devanagari_features, "deva", "dev2", ContextualShape_Devanagari, ShapeCharGlyphProp_Devanagari},
-    {{ devanagari_features, 6}, required_bengali_features, "beng", "bng2", ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_bengali_features, "beng", "bng2", ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_bengali_features, "beng", "bng2", ContextualShape_Bengali, ShapeCharGlyphProp_Bengali},
-    {{ devanagari_features, 6}, required_gurmukhi_features, "guru", "gur2", ContextualShape_Gurmukhi, ShapeCharGlyphProp_Gurmukhi},
-    {{ devanagari_features, 6}, required_gurmukhi_features, "guru", "gur2", ContextualShape_Gurmukhi, ShapeCharGlyphProp_Gurmukhi},
-    {{ devanagari_features, 6}, required_devanagari_features, "gujr", "gjr2", ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_devanagari_features, "gujr", "gjr2", ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_devanagari_features, "gujr", "gjr2", ContextualShape_Gujarati, ShapeCharGlyphProp_Gujarati},
-    {{ devanagari_features, 6}, required_oriya_features, "orya", "ory2", ContextualShape_Oriya, ShapeCharGlyphProp_Oriya},
-    {{ devanagari_features, 6}, required_oriya_features, "orya", "ory2", ContextualShape_Oriya, ShapeCharGlyphProp_Oriya},
-    {{ devanagari_features, 6}, required_tamil_features, "taml", "tam2", ContextualShape_Tamil, ShapeCharGlyphProp_Tamil},
-    {{ devanagari_features, 6}, required_tamil_features, "taml", "tam2", ContextualShape_Tamil, ShapeCharGlyphProp_Tamil},
-    {{ devanagari_features, 6}, required_telugu_features, "telu", "tel2", ContextualShape_Telugu, ShapeCharGlyphProp_Telugu},
-    {{ devanagari_features, 6}, required_telugu_features, "telu", "tel2", ContextualShape_Telugu, ShapeCharGlyphProp_Telugu},
-    {{ devanagari_features, 6}, required_telugu_features, "knda", "knd2", ContextualShape_Kannada, ShapeCharGlyphProp_Kannada},
-    {{ devanagari_features, 6}, required_telugu_features, "knda", "knd2", ContextualShape_Kannada, ShapeCharGlyphProp_Kannada},
-    {{ devanagari_features, 6}, required_telugu_features, "mlym", "mlm2", ContextualShape_Malayalam, ShapeCharGlyphProp_Malayalam},
-    {{ devanagari_features, 6}, required_telugu_features, "mlym", "mlm2", ContextualShape_Malayalam, ShapeCharGlyphProp_Malayalam},
-    {{ standard_features, 2}, NULL, "" , "", NULL, NULL},
-    {{ latin_features, 2}, NULL, "latn" , "", NULL, NULL},
-    {{ standard_features, 2}, NULL, "" , "", NULL, NULL},
+    { MS_MAKE_TAG('p','r','e','s'), 1},
+    { MS_MAKE_TAG('b','l','w','s'), 1},
+    { MS_MAKE_TAG('a','b','v','s'), 1},
+    { MS_MAKE_TAG('p','s','t','s'), 1},
+    { MS_MAKE_TAG('c','l','i','g'), 1},
 };
 
-static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
+static const char* required_khmer_features[] =
 {
-    const GSUB_CoverageFormat1* cf1;
-
-    cf1 = table;
-
-    if (GET_BE_WORD(cf1->CoverageFormat) == 1)
-    {
-        int count = GET_BE_WORD(cf1->GlyphCount);
-        int i;
-        TRACE("Coverage Format 1, %i glyphs\n",count);
-        for (i = 0; i < count; i++)
-            if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
-                return i;
-        return -1;
-    }
-    else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
-    {
-        const GSUB_CoverageFormat2* cf2;
-        int i;
-        int count;
-        cf2 = (const GSUB_CoverageFormat2*)cf1;
-
-        count = GET_BE_WORD(cf2->RangeCount);
-        TRACE("Coverage Format 2, %i ranges\n",count);
-        for (i = 0; i < count; i++)
-        {
-            if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
-                return -1;
-            if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
-                (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
-            {
-                return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
-                    glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
-            }
-        }
-        return -1;
-    }
-    else
-        ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
-
-    return -1;
-}
-
-static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
-{
-    const GSUB_ScriptList *script;
-    const GSUB_Script *deflt = NULL;
-    int i;
-    script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
-
-    TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
-    for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
-    {
-        const GSUB_Script *scr;
-        int offset;
-
-        offset = GET_BE_WORD(script->ScriptRecord[i].Script);
-        scr = (const GSUB_Script*)((const BYTE*)script + offset);
-
-        if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
-            return scr;
-        if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
-            deflt = scr;
-    }
-    return deflt;
-}
-
-static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
-{
-    int i;
-    int offset;
-    const GSUB_LangSys *Lang;
-
-    TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
-
-    for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
-    {
-        offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
-        Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
-
-        if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
-            return Lang;
-    }
-    offset = GET_BE_WORD(script->DefaultLangSys);
-    if (offset)
-    {
-        Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
-        return Lang;
-    }
-    return NULL;
-}
-
-static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
-{
-    int i;
-    const GSUB_FeatureList *feature;
-    feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
-
-    TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
-    for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
-    {
-        int index = GET_BE_WORD(lang->FeatureIndex[i]);
-        if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
-        {
-            const GSUB_Feature *feat;
-            feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
-            return feat;
-        }
-    }
-    return NULL;
-}
-
-static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
-{
-    int j;
-    TRACE("Single Substitution Subtable\n");
-
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
-    {
-        int offset;
-        const GSUB_SingleSubstFormat1 *ssf1;
-        offset = GET_BE_WORD(look->SubTable[j]);
-        ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
-        if (GET_BE_WORD(ssf1->SubstFormat) == 1)
-        {
-            int offset = GET_BE_WORD(ssf1->Coverage);
-            TRACE("  subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
-            if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
-            {
-                TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
-                glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
-                TRACE(" 0x%x\n",glyphs[glyph_index]);
-                return glyph_index + write_dir;
-            }
-        }
-        else
-        {
-            const GSUB_SingleSubstFormat2 *ssf2;
-            INT index;
-            INT offset;
-
-            ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
-            offset = GET_BE_WORD(ssf1->Coverage);
-            TRACE("  subtype 2,  glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
-            index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
-            TRACE("  Coverage index %i\n",index);
-            if (index != -1)
-            {
-                if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
-                    return GSUB_E_NOGLYPH;
-
-                TRACE("    Glyph is 0x%x ->",glyphs[glyph_index]);
-                glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
-                TRACE("0x%x\n",glyphs[glyph_index]);
-                return glyph_index + write_dir;
-            }
-        }
-    }
-    return GSUB_E_NOGLYPH;
-}
-
-static INT GSUB_apply_MultipleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
-{
-    int j;
-    TRACE("Multiple Substitution Subtable\n");
-
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
-    {
-        int offset, index;
-        const GSUB_MultipleSubstFormat1 *msf1;
-        offset = GET_BE_WORD(look->SubTable[j]);
-        msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
-
-        offset = GET_BE_WORD(msf1->Coverage);
-        index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
-        if (index != -1)
-        {
-            const GSUB_Sequence *seq;
-            int sub_count;
-            int j;
-            offset = GET_BE_WORD(msf1->Sequence[index]);
-            seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
-            sub_count = GET_BE_WORD(seq->GlyphCount);
-            TRACE("  Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
-
-            for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
-                    glyphs[j] =glyphs[j-(sub_count-1)];
-
-            for (j = 0; j < sub_count; j++)
-                    if (write_dir < 0)
-                        glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
-                    else
-                        glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
-
-            *glyph_count = *glyph_count + (sub_count - 1);
-
-            if (TRACE_ON(uniscribe))
-            {
-                for (j = 0; j < sub_count; j++)
-                    TRACE(" 0x%x",glyphs[glyph_index+j]);
-                TRACE("\n");
-            }
-
-            return glyph_index + (sub_count * write_dir);
-        }
-    }
-    return GSUB_E_NOGLYPH;
-}
+    "pref",
+    "blwf",
+    "abvf",
+    "pstf",
+    "pres",
+    "blws",
+    "abvs",
+    "psts",
+    "clig",
+    NULL
+};
 
-static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+static OPENTYPE_FEATURE_RECORD khmer_gpos_features[] =
 {
-    int j;
-    TRACE("Alternate Substitution Subtable\n");
-
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
-    {
-        int offset;
-        const GSUB_AlternateSubstFormat1 *asf1;
-        INT index;
-
-        offset = GET_BE_WORD(look->SubTable[j]);
-        asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
-        offset = GET_BE_WORD(asf1->Coverage);
-
-        index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
-        if (index != -1)
-        {
-            const GSUB_AlternateSet *as;
-            offset =  GET_BE_WORD(asf1->AlternateSet[index]);
-            as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
-            FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
-            if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
-                return GSUB_E_NOGLYPH;
-
-            TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
-            glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
-            TRACE(" 0x%x\n",glyphs[glyph_index]);
-            return glyph_index + write_dir;
-        }
-    }
-    return GSUB_E_NOGLYPH;
-}
+    { 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 INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+static OPENTYPE_FEATURE_RECORD ethiopic_features[] =
 {
-    int j;
-
-    TRACE("Ligature Substitution Subtable\n");
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
-    {
-        const GSUB_LigatureSubstFormat1 *lsf1;
-        int offset,index;
-
-        offset = GET_BE_WORD(look->SubTable[j]);
-        lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
-        offset = GET_BE_WORD(lsf1->Coverage);
-        index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
-        TRACE("  Coverage index %i\n",index);
-        if (index != -1)
-        {
-            const GSUB_LigatureSet *ls;
-            int k, count;
-
-            offset = GET_BE_WORD(lsf1->LigatureSet[index]);
-            ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
-            count = GET_BE_WORD(ls->LigatureCount);
-            TRACE("  LigatureSet has %i members\n",count);
-            for (k = 0; k < count; k++)
-            {
-                const GSUB_Ligature *lig;
-                int CompCount,l,CompIndex;
-
-                offset = GET_BE_WORD(ls->Ligature[k]);
-                lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
-                CompCount = GET_BE_WORD(lig->CompCount) - 1;
-                CompIndex = glyph_index+write_dir;
-                for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
-                {
-                    int CompGlyph;
-                    CompGlyph = GET_BE_WORD(lig->Component[l]);
-                    if (CompGlyph != glyphs[CompIndex])
-                        break;
-                    CompIndex += write_dir;
-                }
-                if (l == CompCount)
-                {
-                    int replaceIdx = glyph_index;
-                    if (write_dir < 0)
-                        replaceIdx = glyph_index - CompCount;
-
-                    TRACE("    Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
-                    glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
-                    TRACE("0x%x\n",glyphs[replaceIdx]);
-                    if (CompCount > 0)
-                    {
-                        int j;
-                        for (j = replaceIdx + 1; j < *glyph_count; j++)
-                            glyphs[j] =glyphs[j+CompCount];
-                        *glyph_count = *glyph_count - CompCount;
-                    }
-                    return replaceIdx + write_dir;
-                }
-            }
-        }
-    }
-    return GSUB_E_NOGLYPH;
-}
+    { MS_MAKE_TAG('c','c','m','p'), 1},
+    { MS_MAKE_TAG('l','o','c','l'), 1},
+    { MS_MAKE_TAG('c','a','l','t'), 1},
+    { MS_MAKE_TAG('l','i','g','a'), 1},
+};
 
-static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+static OPENTYPE_FEATURE_RECORD mongolian_features[] =
 {
-    int j;
-    BOOL done = FALSE;
-
-    TRACE("Chaining Contextual Substitution Subtable\n");
-    for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
-    {
-        const GSUB_ChainContextSubstFormat1 *ccsf1;
-        int offset;
-        int dirLookahead = write_dir;
-        int dirBacktrack = -1 * write_dir;
-
-        offset = GET_BE_WORD(look->SubTable[j]);
-        ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
-        if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
-        {
-            FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
-            continue;
-        }
-        else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
-        {
-            FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
-            continue;
-        }
-        else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
-        {
-            int k;
-            int indexGlyphs;
-            const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
-            const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
-            const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
-            const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
-            int newIndex = glyph_index;
-
-            ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
-
-            TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
-
-            for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
-            {
-                offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
-                    break;
-            }
-            if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
-                continue;
-            TRACE("Matched Backtrack\n");
-
-            ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
-
-            indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
-            for (k = 0; k < indexGlyphs; k++)
-            {
-                offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
-                    break;
-            }
-            if (k != indexGlyphs)
-                continue;
-            TRACE("Matched IndexGlyphs\n");
-
-            ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
-
-            for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
-            {
-                offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
-                if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
-                    break;
-            }
-            if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
-                continue;
-            TRACE("Matched LookAhead\n");
-
-            ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
-
-            if (GET_BE_WORD(ccsf3_4->SubstCount))
-            {
-                for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
-                {
-                    int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
-                    int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
+    { MS_MAKE_TAG('c','c','m','p'), 1},
+    { MS_MAKE_TAG('l','o','c','l'), 1},
+    { MS_MAKE_TAG('c','a','l','t'), 1},
+    { MS_MAKE_TAG('r','l','i','g'), 1},
+};
 
-                    TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
-                    newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
-                    if (newIndex == -1)
-                    {
-                        ERR("Chain failed to generate a glyph\n");
-                        continue;
-                    }
-                }
-                return newIndex;
-            }
-            else return GSUB_E_NOGLYPH;
-        }
-    }
-    return -1;
-}
+typedef struct ScriptShapeDataTag {
+    TEXTRANGE_PROPERTIES   defaultTextRange;
+    TEXTRANGE_PROPERTIES   defaultGPOSTextRange;
+    const char**           requiredFeatures;
+    OPENTYPE_TAG           newOtTag;
+    ContextualShapingProc  contextProc;
+    ShapeCharGlyphPropProc charGlyphPropProc;
+} ScriptShapeData;
 
-static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+/* in order of scripts */
+static const ScriptShapeData ShapingData[] =
 {
-    int offset;
-    const GSUB_LookupTable *look;
+    {{ 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},
+};
 
-    offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
-    look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
-    TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
-    switch(GET_BE_WORD(look->LookupType))
-    {
-        case 1:
-            return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 2:
-            return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 3:
-            return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 4:
-            return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
-        case 6:
-            return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
-        default:
-            FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
-    }
-    return GSUB_E_NOGLYPH;
-}
+extern scriptData scriptInformation[];
 
-static INT GSUB_apply_feature_all_lookups(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
+static INT GSUB_apply_feature_all_lookups(LPCVOID header, LoadedFeature *feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
 {
     int i;
     int out_index = GSUB_E_NOGLYPH;
-    const GSUB_LookupList *lookup;
 
-    lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
-
-    TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
-    for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
+    TRACE("%i lookups\n", feature->lookup_count);
+    for (i = 0; i < feature->lookup_count; i++)
     {
-        out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
+        out_index = OpenType_apply_GSUB_lookup(header, feature->lookups[i], glyphs, glyph_index, write_dir, glyph_count);
         if (out_index != GSUB_E_NOGLYPH)
             break;
     }
@@ -1061,23 +553,23 @@ static INT GSUB_apply_feature_all_lookups(const GSUB_Header * header, const GSUB
     return out_index;
 }
 
-static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, BOOL tryNew)
+static OPENTYPE_TAG get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, BOOL tryNew)
 {
     UINT charset;
 
     if (psc->userScript != 0)
     {
-        if (tryNew && ShapingData[psa->eScript].newOtTag[0] != 0 && strncmp((char*)&psc->userScript,ShapingData[psa->eScript].otTag,4)==0)
+        if (tryNew && ShapingData[psa->eScript].newOtTag != 0 && psc->userScript == scriptInformation[psa->eScript].scriptTag)
             return ShapingData[psa->eScript].newOtTag;
         else
-            return (char*)&psc->userScript;
+            return psc->userScript;
     }
 
-    if (tryNew && ShapingData[psa->eScript].newOtTag[0] != 0)
+    if (tryNew && ShapingData[psa->eScript].newOtTag != 0)
         return ShapingData[psa->eScript].newOtTag;
 
-    if (ShapingData[psa->eScript].otTag[0] != 0)
-        return ShapingData[psa->eScript].otTag;
+    if (scriptInformation[psa->eScript].scriptTag)
+        return scriptInformation[psa->eScript].scriptTag;
 
     /*
      * fall back to the font charset
@@ -1085,95 +577,64 @@ static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCach
     charset = GetTextCharsetInfo(hdc, NULL, 0x0);
     switch (charset)
     {
-        case ANSI_CHARSET: return "latn";
-        case BALTIC_CHARSET: return "latn"; /* ?? */
-        case CHINESEBIG5_CHARSET: return "hani";
-        case EASTEUROPE_CHARSET: return "latn"; /* ?? */
-        case GB2312_CHARSET: return "hani";
-        case GREEK_CHARSET: return "grek";
-        case HANGUL_CHARSET: return "hang";
-        case RUSSIAN_CHARSET: return "cyrl";
-        case SHIFTJIS_CHARSET: return "kana";
-        case TURKISH_CHARSET: return "latn"; /* ?? */
-        case VIETNAMESE_CHARSET: return "latn";
-        case JOHAB_CHARSET: return "latn"; /* ?? */
-        case ARABIC_CHARSET: return "arab";
-        case HEBREW_CHARSET: return "hebr";
-        case THAI_CHARSET: return "thai";
-        default: return "latn";
-    }
-}
-
-static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
-{
-    const GSUB_Feature *feature;
-    const char* script;
-    int i;
-
-    script = get_opentype_script(hdc,psa,psc,FALSE);
-
-    for (i = 0; i <  psc->feature_count; i++)
-    {
-        if (strncmp(psc->features[i].tag,feat,4)==0 && strncmp(psc->features[i].script,script,4)==0)
-            return psc->features[i].feature;
+        case ANSI_CHARSET:
+        case BALTIC_CHARSET: return MS_MAKE_TAG('l','a','t','n');
+        case CHINESEBIG5_CHARSET: return MS_MAKE_TAG('h','a','n','i');
+        case EASTEUROPE_CHARSET: return MS_MAKE_TAG('l','a','t','n'); /* ?? */
+        case GB2312_CHARSET: return MS_MAKE_TAG('h','a','n','i');
+        case GREEK_CHARSET: return MS_MAKE_TAG('g','r','e','k');
+        case HANGUL_CHARSET: return MS_MAKE_TAG('h','a','n','g');
+        case RUSSIAN_CHARSET: return MS_MAKE_TAG('c','y','r','l');
+        case SHIFTJIS_CHARSET: return MS_MAKE_TAG('k','a','n','a');
+        case TURKISH_CHARSET: return MS_MAKE_TAG('l','a','t','n'); /* ?? */
+        case VIETNAMESE_CHARSET: return MS_MAKE_TAG('l','a','t','n');
+        case JOHAB_CHARSET: return MS_MAKE_TAG('l','a','t','n'); /* ?? */
+        case ARABIC_CHARSET: return MS_MAKE_TAG('a','r','a','b');
+        case HEBREW_CHARSET: return MS_MAKE_TAG('h','e','b','r');
+        case THAI_CHARSET: return MS_MAKE_TAG('t','h','a','i');
+        default: return MS_MAKE_TAG('l','a','t','n');
     }
+}
 
-    feature = NULL;
+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)
     {
-        const GSUB_Script *script;
-        const GSUB_LangSys *language;
         int attempt = 2;
+        OPENTYPE_TAG tags;
+        OPENTYPE_TAG language;
+        OPENTYPE_TAG script = 0x00000000;
+        int cTags;
 
         do
         {
-            script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa,psc,(attempt==2)));
+            script = get_opentype_script(hdc,psa,psc,(attempt==2));
+            if (psc->userLang != 0)
+                language = psc->userLang;
+            else
+                language = MS_MAKE_TAG('d','f','l','t');
             attempt--;
-            if (script)
-            {
-                if (psc->userLang != 0)
-                    language = GSUB_get_lang_table(script,(char*)&psc->userLang);
-                else
-                    language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
-                if (language)
-                    feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
-            }
+
+            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)
-        {
-            script = GSUB_get_script_table(psc->GSUB_Table, "latn");
-            if (script)
-            {
-                language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
-                if (language)
-                    feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
-            }
-        }
+        if (!feature && !script)
+            OpenType_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]), tableType, 1, &tags, &cTags, &feature);
     }
 
     TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
-
-    psc->feature_count++;
-
-    if (psc->features)
-        psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
-    else
-        psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
-
-    lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
-    lstrcpynA(psc->features[psc->feature_count - 1].script, script, 5);
-    psc->features[psc->feature_count - 1].feature = feature;
     return feature;
 }
 
 static INT apply_GSUB_feature_to_glyph(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, WORD *glyphs, INT index, INT write_dir, INT* glyph_count, const char* feat)
 {
-    const GSUB_Feature *feature;
+    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;
 
@@ -1184,143 +645,68 @@ static INT apply_GSUB_feature_to_glyph(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCach
 static VOID *load_gsub_table(HDC hdc)
 {
     VOID* GSUB_Table = NULL;
-    int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
+    int length = GetFontData(hdc, MS_MAKE_TAG('G', 'S', 'U', 'B'), 0, NULL, 0);
     if (length != GDI_ERROR)
     {
         GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
-        GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, 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;
 }
 
-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));
-    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)
-        rc = count - glyph_count;
-    else
-        rc = 0;
-
-    HeapFree(GetProcessHeap(),0,glyphs);
-    return rc;
-}
-
-static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
+static VOID *load_gpos_table(HDC hdc)
 {
-    int offset;
-    WORD class = 0;
-    const GDEF_ClassDefFormat1 *cf1;
-
-    if (!header)
-        return 0;
-
-    offset = GET_BE_WORD(header->GlyphClassDef);
-    if (!offset)
-        return 0;
-
-    cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
-    if (GET_BE_WORD(cf1->ClassFormat) == 1)
+    VOID* GPOS_Table = NULL;
+    int length = GetFontData(hdc, MS_MAKE_TAG('G', 'P', 'O', 'S'), 0, NULL, 0);
+    if (length != GDI_ERROR)
     {
-        if (glyph >= GET_BE_WORD(cf1->StartGlyph))
-        {
-            int index = glyph - GET_BE_WORD(cf1->StartGlyph);
-            if (index < GET_BE_WORD(cf1->GlyphCount))
-                class = GET_BE_WORD(cf1->ClassValueArray[index]);
-        }
+        GPOS_Table = HeapAlloc(GetProcessHeap(),0,length);
+        GetFontData(hdc, MS_MAKE_TAG('G', 'P', 'O', 'S'), 0, GPOS_Table, length);
+        TRACE("Loaded GPOS table of %i bytes\n",length);
     }
-    else if (GET_BE_WORD(cf1->ClassFormat) == 2)
-    {
-        const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
-        int i, top;
-        top = GET_BE_WORD(cf2->ClassRangeCount);
-        for (i = 0; i < top; i++)
-        {
-            if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
-                glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
-            {
-                class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
-                break;
-            }
-        }
-    }
-    else
-        ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
-
-    return class;
+    return GPOS_Table;
 }
 
 static VOID *load_gdef_table(HDC hdc)
 {
     VOID* GDEF_Table = NULL;
-    int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
+    int length = GetFontData(hdc, MS_MAKE_TAG('G', 'D', 'E', 'F'), 0, NULL, 0);
     if (length != GDI_ERROR)
     {
         GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
-        GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, 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 GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
+static VOID load_ot_tables(HDC hdc, ScriptCache *psc)
 {
-    int i;
-
+    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);
+}
 
-    for (i = 0; i < cGlyphs; i++)
-    {
-        WORD class;
-        int char_count = 0;
-        int k;
-
-        for (k = 0; k < cChars; k++)
-            if (pwLogClust[k] == i)
-                char_count++;
-
-        class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
+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;
 
-        switch (class)
-        {
-            case 0:
-            case BaseGlyph:
-                pGlyphProp[i].sva.fClusterStart = 1;
-                pGlyphProp[i].sva.fDiacritic = 0;
-                pGlyphProp[i].sva.fZeroWidth = 0;
-                break;
-            case LigatureGlyph:
-                pGlyphProp[i].sva.fClusterStart = 1;
-                pGlyphProp[i].sva.fDiacritic = 0;
-                pGlyphProp[i].sva.fZeroWidth = 0;
-                break;
-            case MarkGlyph:
-                pGlyphProp[i].sva.fClusterStart = 0;
-                pGlyphProp[i].sva.fDiacritic = 1;
-                pGlyphProp[i].sva.fZeroWidth = 1;
-                break;
-            case ComponentGlyph:
-                pGlyphProp[i].sva.fClusterStart = 0;
-                pGlyphProp[i].sva.fDiacritic = 0;
-                pGlyphProp[i].sva.fZeroWidth = 0;
-                break;
-            default:
-                ERR("Unknown glyph class %i\n",class);
-                pGlyphProp[i].sva.fClusterStart = 1;
-                pGlyphProp[i].sva.fDiacritic = 0;
-                pGlyphProp[i].sva.fZeroWidth = 0;
-        }
+    glyphs = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*(count*2));
+    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)
+        rc = count - glyph_count;
+    else
+        rc = 0;
 
-        if (char_count == 0)
-            pGlyphProp[i].sva.fClusterStart = 0;
-    }
+    HeapFree(GetProcessHeap(),0,glyphs);
+    return rc;
 }
 
 static void UpdateClustersFromGlyphProp(const int cGlyphs, const int cChars, WORD* pwLogClust, SCRIPT_GLYPHPROP *pGlyphProp)
@@ -1337,9 +723,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];
                 }
             }
@@ -1445,20 +831,15 @@ static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, W
 {
     if (psc->GSUB_Table)
     {
-        const GSUB_Feature *feature;
-        const GSUB_LookupList *lookup;
-        const GSUB_Header *header = psc->GSUB_Table;
-        int lookup_index, lookup_count;
+        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;
 
-        TRACE("applying feature %s\n",debugstr_an(feat,4));
-        lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
-        lookup_count = GET_BE_WORD(feature->LookupCount);
-        TRACE("%i lookups\n", lookup_count);
-        for (lookup_index = 0; lookup_index < lookup_count; lookup_index++)
+        TRACE("applying feature %s: %i lookups\n",debugstr_an(feat,4),feature->lookup_count);
+        for (lookup_index = 0; lookup_index < feature->lookup_count; lookup_index++)
         {
             int i;
 
@@ -1466,13 +847,13 @@ static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, W
                 i = 0;
             else
                 i = *pcGlyphs-1;
-            TRACE("applying lookup (%i/%i)\n",lookup_index,lookup_count);
+            TRACE("applying lookup (%i/%i)\n",lookup_index,feature->lookup_count);
             while(i < *pcGlyphs && i >= 0)
             {
                 INT nextIndex;
                 INT prevCount = *pcGlyphs;
 
-                nextIndex = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[lookup_index]), pwOutGlyphs, i, write_dir, pcGlyphs);
+                nextIndex = OpenType_apply_GSUB_lookup(psc->GSUB_Table, feature->lookups[lookup_index], pwOutGlyphs, i, write_dir, pcGlyphs);
                 if (*pcGlyphs != prevCount)
                 {
                     UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
@@ -1487,9 +868,84 @@ static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, W
     return GSUB_E_NOFEATURE;
 }
 
+static VOID GPOS_apply_feature(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, LoadedFeature *feature, const WORD *glyphs, INT glyph_count, GOFFSET *pGoffset)
+{
+    int i;
+
+    TRACE("%i lookups\n", feature->lookup_count);
+    for (i = 0; i < feature->lookup_count; i++)
+    {
+        int j;
+        for (j = 0; j < glyph_count; )
+            j = OpenType_apply_GPOS_lookup(psc, lpotm, lplogfont, analysis, piAdvance, feature->lookups[i], glyphs, j, glyph_count, pGoffset);
+    }
+}
+
 static inline BOOL get_GSUB_Indic2(SCRIPT_ANALYSIS *psa, ScriptCache *psc)
 {
-    return(GSUB_get_script_table(psc->GSUB_Table,ShapingData[psa->eScript].newOtTag)!=NULL);
+    OPENTYPE_TAG tag;
+    HRESULT hr;
+    int count = 0;
+
+    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 = HeapAlloc(GetProcessHeap(),0,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++;
+        }
+    }
+
+    HeapFree(GetProcessHeap(),0,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 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)
@@ -1547,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
  */
@@ -1574,8 +1075,7 @@ static void ContextualShape_Arabic(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);
 
     context_type = HeapAlloc(GetProcessHeap(),0,cChars);
     context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
@@ -1637,12 +1137,137 @@ static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *p
 
     HeapFree(GetProcessHeap(),0,context_shape);
     HeapFree(GetProcessHeap(),0,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
@@ -1671,8 +1296,7 @@ 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;
@@ -1733,6 +1357,46 @@ right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
 
     HeapFree(GetProcessHeap(),0,context_shape);
     HeapFree(GetProcessHeap(),0,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;
+
+    mark_invalid_combinations(hdc, pwcChars, cChars, pwOutGlyphs, pcGlyphs, dirL, pwLogClust, combining_lexical_Thaana);
 }
 
 /*
@@ -1766,8 +1430,7 @@ 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;
@@ -1817,7 +1480,96 @@ static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
             i++;
     }
 
-    HeapFree(GetProcessHeap(),0,context_shape);
+    HeapFree(GetProcessHeap(),0,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)
@@ -2157,7 +1909,7 @@ static inline void shift_syllable_glyph_indexs(IndicSyllable *glyph_index, INT i
         glyph_index->pref+= shift;
 }
 
-static void Apply_Indic_BasicForm(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwChars, INT cChars, IndicSyllable *syllable, WORD *pwOutGlyphs, INT* pcGlyphs, WORD *pwLogClust, lexical_function lexical, IndicSyllable *glyph_index, const GSUB_Feature *feature )
+static void Apply_Indic_BasicForm(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwChars, INT cChars, IndicSyllable *syllable, WORD *pwOutGlyphs, INT* pcGlyphs, WORD *pwLogClust, lexical_function lexical, IndicSyllable *glyph_index, LoadedFeature *feature )
 {
     int index = glyph_index->start;
 
@@ -2294,17 +2046,17 @@ static void ShapeIndicSyllables(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa,
 {
     int c;
     int overall_shift = 0;
-    const GSUB_Feature *locl = (modern)?load_GSUB_feature(hdc, psa, psc, "locl"):NULL;
-    const GSUB_Feature *nukt = load_GSUB_feature(hdc, psa, psc, "nukt");
-    const GSUB_Feature *akhn = load_GSUB_feature(hdc, psa, psc, "akhn");
-    const GSUB_Feature *rkrf = (modern)?load_GSUB_feature(hdc, psa, psc, "rkrf"):NULL;
-    const GSUB_Feature *pstf = load_GSUB_feature(hdc, psa, psc, "pstf");
-    const GSUB_Feature *vatu = (!rkrf)?load_GSUB_feature(hdc, psa, psc, "vatu"):NULL;
-    const GSUB_Feature *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++)
@@ -2584,7 +2336,7 @@ static void ContextualShape_Bengali(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *
     input = HeapAlloc(GetProcessHeap(), 0, (cChars * 2) * sizeof(WCHAR));
     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);
@@ -2625,7 +2377,6 @@ static int gurmukhi_lex(WCHAR c)
 
 static const ConsonantComponents Gurmukhi_consonants[] = {
             {{0x0A32,0x0A3C,0x0000}, 0x0A33},
-            {{0x0A38,0x0A3C,0x0000}, 0x0A36},
             {{0x0A16,0x0A3C,0x0000}, 0x0A59},
             {{0x0A17,0x0A3C,0x0000}, 0x0A5A},
             {{0x0A1C,0x0A3C,0x0000}, 0x0A5B},
@@ -2649,7 +2400,7 @@ static void ContextualShape_Gurmukhi(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
     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);
 
@@ -2744,7 +2495,7 @@ static void ContextualShape_Oriya(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
     input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
     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);
@@ -2794,7 +2545,7 @@ static void ContextualShape_Tamil(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *ps
     input = HeapAlloc(GetProcessHeap(), 0, (cChars*2) * sizeof(WCHAR));
     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);
@@ -2958,7 +2709,105 @@ static void ContextualShape_Malayalam(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS
     HeapFree(GetProcessHeap(),0,syllables);
 }
 
-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 int khmer_lex(WCHAR c)
+{
+    return unicode_lex(c);
+}
+
+static void ContextualShape_Khmer(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    int cCount = cChars;
+    WCHAR *input;
+    IndicSyllable *syllables = NULL;
+    int syllable_count = 0;
+
+    if (*pcGlyphs != cChars)
+    {
+        ERR("Number of Glyphs and Chars need to match at the beginning\n");
+        return;
+    }
+
+    input = HeapAlloc(GetProcessHeap(), 0, cChars * sizeof(WCHAR));
+    memcpy(input, pwcChars, cChars * sizeof(WCHAR));
+
+    /* Step 1: Reorder within Syllables */
+    Indic_ReorderCharacters( hdc, psa, psc, input, cCount, &syllables, &syllable_count, khmer_lex, Reorder_Like_Devanagari, FALSE);
+    TRACE("reordered string %s\n",debugstr_wn(input,cCount));
+    GetGlyphIndicesW(hdc, input, cCount, pwOutGlyphs, 0);
+    *pcGlyphs = cCount;
+
+    /* 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);
+}
+
+static inline BOOL mongolian_wordbreak(WCHAR chr)
+{
+    return ((chr == 0x0020) || (chr == 0x200C) || (chr == 0x202F) || (chr == 0x180E) || (chr == 0x1800) || (chr == 0x1802) || (chr == 0x1803) || (chr == 0x1805) || (chr == 0x1808) || (chr == 0x1809) || (chr == 0x1807));
+}
+
+static void ContextualShape_Mongolian(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
+{
+    INT *context_shape;
+    INT dirL;
+    int i;
+
+    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;
+
+    if (!psc->GSUB_Table)
+        return;
+
+    context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
+
+    for (i = 0; i < cChars; i++)
+    {
+        if (i == 0 || mongolian_wordbreak(pwcChars[i-1]))
+        {
+            if ((i == cChars-1) || mongolian_wordbreak(pwcChars[i+1]))
+                context_shape[i] = Xn;
+            else
+                context_shape[i] = Xl;
+        }
+        else
+        {
+            if ((i == cChars-1) || mongolian_wordbreak(pwcChars[i+1]))
+                context_shape[i] = Xr;
+            else
+                context_shape[i] = Xm;
+        }
+    }
+
+    /* Contextual Shaping */
+    i = 0;
+    while(i < *pcGlyphs)
+    {
+        INT nextIndex;
+        INT prevCount = *pcGlyphs;
+        nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
+        if (nextIndex > GSUB_E_NOGLYPH)
+        {
+            UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
+            i = nextIndex;
+        }
+        else
+            i++;
+    }
+
+    HeapFree(GetProcessHeap(),0,context_shape);
+}
+
+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;
 
@@ -2967,13 +2816,11 @@ static void ShapeCharGlyphProp_Default( HDC hdc, ScriptCache* psc, SCRIPT_ANALYS
         int char_index[20];
         int char_count = 0;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
         }
 
         if (char_count == 0)
@@ -2988,10 +2835,37 @@ static void ShapeCharGlyphProp_Default( HDC hdc, ScriptCache* psc, SCRIPT_ANALYS
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_CHARACTER;
     }
 
-    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;
@@ -3033,13 +2907,11 @@ static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
         int char_count = 0;
         BOOL isInit, isFinal;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
         }
 
         isInit = (i == initGlyph || (i+dirR > 0 && i+dirR < cGlyphs && spaces[i+dirR]));
@@ -3099,20 +2971,46 @@ static void ShapeCharGlyphProp_Arabic( HDC hdc, ScriptCache *psc, SCRIPT_ANALYSI
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
 
-    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);
 }
 
-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)
     {
@@ -3125,51 +3023,37 @@ 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;
-            }
-    }
-
-    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;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                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 */
@@ -3189,13 +3073,11 @@ static void ShapeCharGlyphProp_None( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS*
         int char_index[20];
         int char_count = 0;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
         }
 
         if (char_count == 0)
@@ -3209,7 +3091,7 @@ static void ShapeCharGlyphProp_None( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS*
         else
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
-    GDEF_UpdateGlyphProps(hdc, psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
+    OpenType_GDEF_UpdateGlyphProps(psc, pwGlyphs, cGlyphs, pwLogClust, cChars, pGlyphProp);
     UpdateClustersFromGlyphProp(cGlyphs, cChars, pwLogClust, pGlyphProp);
 }
 
@@ -3222,13 +3104,11 @@ static void ShapeCharGlyphProp_Tibet( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS
         int char_index[20];
         int char_count = 0;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
         }
 
         if (char_count == 0)
@@ -3242,7 +3122,7 @@ static void ShapeCharGlyphProp_Tibet( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS
         else
             pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
     }
-    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 */
@@ -3256,31 +3136,35 @@ static void ShapeCharGlyphProp_Tibet( HDC hdc, ScriptCache* psc, SCRIPT_ANALYSIS
     }
 }
 
-static void ShapeCharGlyphProp_BaseIndic( 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, lexical_function lexical, BOOL use_syllables)
+static void ShapeCharGlyphProp_BaseIndic( 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, lexical_function lexical, BOOL use_syllables, BOOL override_gsub)
 {
     int i,k;
 
-    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];
         int char_count = 0;
 
-        for (k = 0; k < cChars; k++)
+        k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
+        if (k>=0)
         {
-            if (pwLogClust[k] == i)
-            {
-                char_index[char_count] = k;
-                char_count++;
-            }
+            for (; k < cChars && pwLogClust[k] == i; k++)
+                char_index[char_count++] = k;
         }
 
-        /* Indic scripts do not set fDiacritic or fZeroWidth */
-        pGlyphProp[i].sva.fDiacritic = FALSE;
-        pGlyphProp[i].sva.fZeroWidth = FALSE;
+        if (override_gsub)
+        {
+            /* Most indic scripts do not set fDiacritic or fZeroWidth */
+            pGlyphProp[i].sva.fDiacritic = FALSE;
+            pGlyphProp[i].sva.fZeroWidth = FALSE;
+        }
 
         if (char_count == 0)
+        {
+            pGlyphProp[i].sva.uJustification = SCRIPT_JUSTIFY_NONE;
             continue;
+        }
 
         if (char_count ==1 && pwcChars[char_index[0]] == 0x0020)  /* space */
         {
@@ -3303,7 +3187,11 @@ static void ShapeCharGlyphProp_BaseIndic( HDC hdc, ScriptCache *psc, SCRIPT_ANAL
                     break;
                 case lex_ZWJ:
                 case lex_ZWNJ:
-                    k = char_count;
+                    /* check for dangling joiners */
+                    if (pwcChars[char_index[k]-1] == 0x0020 || pwcChars[char_index[k]+1] == 0x0020)
+                        pGlyphProp[i].sva.fClusterStart = 1;
+                    else
+                        k = char_count;
                     break;
                 default:
                     pGlyphProp[i].sva.fClusterStart = 1;
@@ -3341,66 +3229,72 @@ static void ShapeCharGlyphProp_BaseIndic( HDC hdc, ScriptCache *psc, SCRIPT_ANAL
 
 static void ShapeCharGlyphProp_Sinhala( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, sinhala_lex, FALSE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, sinhala_lex, FALSE, FALSE);
 }
 
 static void ShapeCharGlyphProp_Devanagari( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, devanagari_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, devanagari_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Bengali( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, bengali_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, bengali_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Gurmukhi( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, gurmukhi_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, gurmukhi_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Gujarati( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, gujarati_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, gujarati_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Oriya( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, oriya_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, oriya_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Tamil( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, tamil_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, tamil_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Telugu( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, telugu_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, telugu_lex, TRUE, TRUE);
 }
 
 static void ShapeCharGlyphProp_Kannada( 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, kannada_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, kannada_lex, TRUE, TRUE);
 }
 
 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 )
 {
-    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, malayalam_lex, TRUE);
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, malayalam_lex, TRUE, TRUE);
+}
+
+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 )
+{
+    ShapeCharGlyphProp_BaseIndic(hdc, psc, psa, pwcChars, cChars, pwGlyphs, cGlyphs, pwLogClust, pCharProp, pGlyphProp, khmer_lex, TRUE, TRUE);
 }
 
 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);
@@ -3414,8 +3308,7 @@ 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;
@@ -3440,22 +3333,51 @@ 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;
+    int i;
+
+    rpRangeProperties = &ShapingData[psa->eScript].defaultGPOSTextRange;
+
+    if (!rpRangeProperties)
+        return;
+
+    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)
 {
-    const GSUB_Feature *feature;
+    LoadedFeature *feature;
     int i;
 
     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++;
@@ -3463,3 +3385,72 @@ HRESULT SHAPE_CheckFontForRequiredFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANA
 
     return USP_E_SCRIPT_NOT_IN_FONT;
 }
+
+HRESULT SHAPE_GetFontScriptTags( HDC hdc, ScriptCache *psc,
+                                 SCRIPT_ANALYSIS *psa, int cMaxTags,
+                                 OPENTYPE_TAG *pScriptTags, int *pcTags)
+{
+    HRESULT hr;
+    OPENTYPE_TAG searching = 0x00000000;
+
+    load_ot_tables(hdc, psc);
+
+    if (psa && scriptInformation[psa->eScript].scriptTag)
+        searching = scriptInformation[psa->eScript].scriptTag;
+
+    hr = OpenType_GetFontScriptTags(psc, searching, cMaxTags, pScriptTags, pcTags);
+    if (FAILED(hr))
+        *pcTags = 0;
+    return hr;
+}
+
+HRESULT SHAPE_GetFontLanguageTags( HDC hdc, ScriptCache *psc,
+                                   SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
+                                   int cMaxTags, OPENTYPE_TAG *pLangSysTags,
+                                   int *pcTags)
+{
+    HRESULT hr;
+    OPENTYPE_TAG searching = 0x00000000;
+    BOOL fellback = FALSE;
+
+    load_ot_tables(hdc, psc);
+
+    if (psa && psc->userLang != 0)
+        searching = psc->userLang;
+
+    hr = OpenType_GetFontLanguageTags(psc, tagScript, searching, cMaxTags, pLangSysTags, pcTags);
+    if (FAILED(hr))
+    {
+        fellback = TRUE;
+        hr = OpenType_GetFontLanguageTags(psc, MS_MAKE_TAG('l','a','t','n'), searching, cMaxTags, pLangSysTags, pcTags);
+    }
+
+    if (FAILED(hr) || fellback)
+        *pcTags = 0;
+    if (SUCCEEDED(hr) && fellback && psa)
+        hr = E_INVALIDARG;
+    return hr;
+}
+
+HRESULT SHAPE_GetFontFeatureTags( HDC hdc, ScriptCache *psc,
+                                  SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
+                                  OPENTYPE_TAG tagLangSys, int cMaxTags,
+                                  OPENTYPE_TAG *pFeatureTags, int *pcTags)
+{
+    HRESULT hr;
+    BOOL filter = FALSE;
+
+    load_ot_tables(hdc, psc);
+
+    if (psa && scriptInformation[psa->eScript].scriptTag)
+    {
+        FIXME("Filtering not implemented\n");
+        filter = TRUE;
+    }
+
+    hr = OpenType_GetFontFeatureTags(psc, tagScript, tagLangSys, filter, 0x00000000, FEATURE_ALL_TABLES, cMaxTags, pFeatureTags, pcTags, NULL);
+
+    if (FAILED(hr))
+        *pcTags = 0;
+    return hr;
+}