usp10: Add Phags-pa Script.
[wine] / dlls / usp10 / shape.c
1 /*
2  * Implementation of Shaping for the Uniscribe Script Processor (usp10.dll)
3  *
4  * Copyright 2010 CodeWeavers, Aric Stewart
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "usp10.h"
29 #include "winternl.h"
30
31 #include "usp10_internal.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
36
37 #define FIRST_ARABIC_CHAR 0x0600
38 #define LAST_ARABIC_CHAR  0x06ff
39
40 typedef VOID (*ContextualShapingProc)(HDC, ScriptCache*, SCRIPT_ANALYSIS*,
41                                       WCHAR*, INT, WORD*, INT*, INT, WORD*);
42
43 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
44 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
45 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
46
47 extern const unsigned short wine_shaping_table[];
48 extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
49
50 enum joining_types {
51     jtU,
52     jtT,
53     jtR,
54     jtL,
55     jtD,
56     jtC
57 };
58
59 enum joined_forms {
60     Xn=0,
61     Xr,
62     Xl,
63     Xm,
64     /* Syriac Alaph */
65     Afj,
66     Afn,
67     Afx
68 };
69
70 #ifdef WORDS_BIGENDIAN
71 #define GET_BE_WORD(x) (x)
72 #else
73 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
74 #endif
75
76 /* These are all structures needed for the GSUB table */
77 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
78           ( ( (ULONG)_x4 << 24 ) |     \
79             ( (ULONG)_x3 << 16 ) |     \
80             ( (ULONG)_x2 <<  8 ) |     \
81               (ULONG)_x1         )
82
83 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
84 #define GSUB_E_NOFEATURE -2
85 #define GSUB_E_NOGLYPH -1
86
87 typedef struct {
88     DWORD version;
89     WORD ScriptList;
90     WORD FeatureList;
91     WORD LookupList;
92 } GSUB_Header;
93
94 typedef struct {
95     CHAR ScriptTag[4];
96     WORD Script;
97 } GSUB_ScriptRecord;
98
99 typedef struct {
100     WORD ScriptCount;
101     GSUB_ScriptRecord ScriptRecord[1];
102 } GSUB_ScriptList;
103
104 typedef struct {
105     CHAR LangSysTag[4];
106     WORD LangSys;
107 } GSUB_LangSysRecord;
108
109 typedef struct {
110     WORD DefaultLangSys;
111     WORD LangSysCount;
112     GSUB_LangSysRecord LangSysRecord[1];
113 } GSUB_Script;
114
115 typedef struct {
116     WORD LookupOrder; /* Reserved */
117     WORD ReqFeatureIndex;
118     WORD FeatureCount;
119     WORD FeatureIndex[1];
120 } GSUB_LangSys;
121
122 typedef struct {
123     CHAR FeatureTag[4];
124     WORD Feature;
125 } GSUB_FeatureRecord;
126
127 typedef struct {
128     WORD FeatureCount;
129     GSUB_FeatureRecord FeatureRecord[1];
130 } GSUB_FeatureList;
131
132 typedef struct {
133     WORD FeatureParams; /* Reserved */
134     WORD LookupCount;
135     WORD LookupListIndex[1];
136 } GSUB_Feature;
137
138 typedef struct {
139     WORD LookupCount;
140     WORD Lookup[1];
141 } GSUB_LookupList;
142
143 typedef struct {
144     WORD LookupType;
145     WORD LookupFlag;
146     WORD SubTableCount;
147     WORD SubTable[1];
148 } GSUB_LookupTable;
149
150 typedef struct {
151     WORD CoverageFormat;
152     WORD GlyphCount;
153     WORD GlyphArray[1];
154 } GSUB_CoverageFormat1;
155
156 typedef struct {
157     WORD Start;
158     WORD End;
159     WORD StartCoverageIndex;
160 } GSUB_RangeRecord;
161
162 typedef struct {
163     WORD CoverageFormat;
164     WORD RangeCount;
165     GSUB_RangeRecord RangeRecord[1];
166 } GSUB_CoverageFormat2;
167
168 typedef struct {
169     WORD SubstFormat; /* = 1 */
170     WORD Coverage;
171     WORD DeltaGlyphID;
172 } GSUB_SingleSubstFormat1;
173
174 typedef struct {
175     WORD SubstFormat; /* = 2 */
176     WORD Coverage;
177     WORD GlyphCount;
178     WORD Substitute[1];
179 }GSUB_SingleSubstFormat2;
180
181 typedef struct {
182     WORD SubstFormat; /* = 1 */
183     WORD Coverage;
184     WORD LigSetCount;
185     WORD LigatureSet[1];
186 }GSUB_LigatureSubstFormat1;
187
188 typedef struct {
189     WORD LigatureCount;
190     WORD Ligature[1];
191 }GSUB_LigatureSet;
192
193 typedef struct{
194     WORD LigGlyph;
195     WORD CompCount;
196     WORD Component[1];
197 }GSUB_Ligature;
198
199 typedef struct{
200     WORD SequenceIndex;
201     WORD LookupListIndex;
202
203 }GSUB_SubstLookupRecord;
204
205 typedef struct{
206     WORD SubstFormat; /* = 1 */
207     WORD Coverage;
208     WORD ChainSubRuleSetCount;
209     WORD ChainSubRuleSet[1];
210 }GSUB_ChainContextSubstFormat1;
211
212 typedef struct {
213     WORD SubstFormat; /* = 3 */
214     WORD BacktrackGlyphCount;
215     WORD Coverage[1];
216 }GSUB_ChainContextSubstFormat3_1;
217
218 typedef struct{
219     WORD InputGlyphCount;
220     WORD Coverage[1];
221 }GSUB_ChainContextSubstFormat3_2;
222
223 typedef struct{
224     WORD LookaheadGlyphCount;
225     WORD Coverage[1];
226 }GSUB_ChainContextSubstFormat3_3;
227
228 typedef struct{
229     WORD SubstCount;
230     GSUB_SubstLookupRecord SubstLookupRecord[1];
231 }GSUB_ChainContextSubstFormat3_4;
232
233 typedef struct {
234     WORD SubstFormat; /* = 1 */
235     WORD Coverage;
236     WORD AlternateSetCount;
237     WORD AlternateSet[1];
238 } GSUB_AlternateSubstFormat1;
239
240 typedef struct{
241     WORD GlyphCount;
242     WORD Alternate[1];
243 } GSUB_AlternateSet;
244
245 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
246
247 /* the orders of joined_forms and contextual_features need to line up */
248 static const char* contextual_features[] =
249 {
250     "isol",
251     "fina",
252     "init",
253     "medi",
254     /* Syriac Alaph */
255     "med2",
256     "fin2",
257     "fin3"
258 };
259
260 static OPENTYPE_FEATURE_RECORD standard_features[] =
261 {
262     { 0x6167696c /*liga*/, 1},
263     { 0x67696c63 /*clig*/, 1},
264 };
265
266 static OPENTYPE_FEATURE_RECORD arabic_features[] =
267 {
268     { 0x67696c72 /*rlig*/, 1},
269     { 0x746c6163 /*calt*/, 1},
270     { 0x6167696c /*liga*/, 1},
271     { 0x67696c64 /*dlig*/, 1},
272     { 0x68777363 /*cswh*/, 1},
273     { 0x7465736d /*mset*/, 1},
274 };
275
276 static OPENTYPE_FEATURE_RECORD hebrew_features[] =
277 {
278     { 0x67696c64 /*dlig*/, 1},
279 };
280
281 static OPENTYPE_FEATURE_RECORD syriac_features[] =
282 {
283     { 0x67696c72 /*rlig*/, 1},
284     { 0x746c6163 /*calt*/, 1},
285     { 0x6167696c /*liga*/, 1},
286     { 0x67696c64 /*dlig*/, 1},
287 };
288
289 static OPENTYPE_FEATURE_RECORD sinhala_features[] =
290 {
291     /* Base forms */
292     { 0x6e686b61 /*akhn*/, 1},
293     { 0x66687072 /*rphf*/, 1},
294     { 0x75746176 /*vatu*/, 1},
295     { 0x66747370 /*pstf*/, 1},
296     /* Presentation forms */
297     { 0x73776c62 /*blws*/, 1},
298     { 0x73766261 /*abvs*/, 1},
299     { 0x73747370 /*psts*/, 1},
300 };
301
302 static OPENTYPE_FEATURE_RECORD tibetan_features[] =
303 {
304     { 0x73766261 /*abvs*/, 1},
305     { 0x73776c62 /*blws*/, 1},
306 };
307
308 typedef struct ScriptShapeDataTag {
309     TEXTRANGE_PROPERTIES  defaultTextRange;
310     CHAR                  otTag[5];
311     ContextualShapingProc contextProc;
312 } ScriptShapeData;
313
314 /* in order of scripts */
315 static const ScriptShapeData ShapingData[] =
316 {
317     {{ standard_features, 2}, "", NULL},
318     {{ standard_features, 2}, "latn", NULL},
319     {{ standard_features, 2}, "latn", NULL},
320     {{ standard_features, 2}, "latn", NULL},
321     {{ standard_features, 2}, "" , NULL},
322     {{ standard_features, 2}, "latn", NULL},
323     {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
324     {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
325     {{ hebrew_features, 1}, "hebr", NULL},
326     {{ syriac_features, 4}, "syrc", ContextualShape_Syriac},
327     {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
328     {{ NULL, 0}, "thaa", NULL},
329     {{ standard_features, 2}, "grek", NULL},
330     {{ standard_features, 2}, "cyrl", NULL},
331     {{ standard_features, 2}, "armn", NULL},
332     {{ standard_features, 2}, "geor", NULL},
333     {{ sinhala_features, 7}, "sinh", NULL},
334     {{ tibetan_features, 2}, "tibt", NULL},
335     {{ tibetan_features, 2}, "tibt", NULL},
336     {{ tibetan_features, 2}, "phag", ContextualShape_Phags_pa},
337 };
338
339 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
340 {
341     const GSUB_CoverageFormat1* cf1;
342
343     cf1 = table;
344
345     if (GET_BE_WORD(cf1->CoverageFormat) == 1)
346     {
347         int count = GET_BE_WORD(cf1->GlyphCount);
348         int i;
349         TRACE("Coverage Format 1, %i glyphs\n",count);
350         for (i = 0; i < count; i++)
351             if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
352                 return i;
353         return -1;
354     }
355     else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
356     {
357         const GSUB_CoverageFormat2* cf2;
358         int i;
359         int count;
360         cf2 = (const GSUB_CoverageFormat2*)cf1;
361
362         count = GET_BE_WORD(cf2->RangeCount);
363         TRACE("Coverage Format 2, %i ranges\n",count);
364         for (i = 0; i < count; i++)
365         {
366             if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
367                 return -1;
368             if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
369                 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
370             {
371                 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
372                     glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
373             }
374         }
375         return -1;
376     }
377     else
378         ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
379
380     return -1;
381 }
382
383 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
384 {
385     const GSUB_ScriptList *script;
386     const GSUB_Script *deflt = NULL;
387     int i;
388     script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
389
390     TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
391     for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
392     {
393         const GSUB_Script *scr;
394         int offset;
395
396         offset = GET_BE_WORD(script->ScriptRecord[i].Script);
397         scr = (const GSUB_Script*)((const BYTE*)script + offset);
398
399         if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
400             return scr;
401         if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
402             deflt = scr;
403     }
404     return deflt;
405 }
406
407 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
408 {
409     int i;
410     int offset;
411     const GSUB_LangSys *Lang;
412
413     TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
414
415     for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
416     {
417         offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
418         Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
419
420         if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
421             return Lang;
422     }
423     offset = GET_BE_WORD(script->DefaultLangSys);
424     if (offset)
425     {
426         Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
427         return Lang;
428     }
429     return NULL;
430 }
431
432 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
433 {
434     int i;
435     const GSUB_FeatureList *feature;
436     feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
437
438     TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
439     for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
440     {
441         int index = GET_BE_WORD(lang->FeatureIndex[i]);
442         if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
443         {
444             const GSUB_Feature *feat;
445             feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
446             return feat;
447         }
448     }
449     return NULL;
450 }
451
452 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
453 {
454     int j;
455     TRACE("Single Substitution Subtable\n");
456
457     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
458     {
459         int offset;
460         const GSUB_SingleSubstFormat1 *ssf1;
461         offset = GET_BE_WORD(look->SubTable[j]);
462         ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
463         if (GET_BE_WORD(ssf1->SubstFormat) == 1)
464         {
465             int offset = GET_BE_WORD(ssf1->Coverage);
466             TRACE("  subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
467             if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
468             {
469                 TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
470                 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
471                 TRACE(" 0x%x\n",glyphs[glyph_index]);
472                 return glyph_index + 1;
473             }
474         }
475         else
476         {
477             const GSUB_SingleSubstFormat2 *ssf2;
478             INT index;
479             INT offset;
480
481             ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
482             offset = GET_BE_WORD(ssf1->Coverage);
483             TRACE("  subtype 2,  glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
484             index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
485             TRACE("  Coverage index %i\n",index);
486             if (index != -1)
487             {
488                 TRACE("    Glyph is 0x%x ->",glyphs[glyph_index]);
489                 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
490                 TRACE("0x%x\n",glyphs[glyph_index]);
491                 return glyph_index + 1;
492             }
493         }
494     }
495     return GSUB_E_NOGLYPH;
496 }
497
498 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
499 {
500     int j;
501     TRACE("Alternate Substitution Subtable\n");
502
503     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
504     {
505         int offset;
506         const GSUB_AlternateSubstFormat1 *asf1;
507         INT index;
508
509         offset = GET_BE_WORD(look->SubTable[j]);
510         asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
511         offset = GET_BE_WORD(asf1->Coverage);
512
513         index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
514         if (index != -1)
515         {
516             const GSUB_AlternateSet *as;
517             offset =  GET_BE_WORD(asf1->AlternateSet[index]);
518             as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
519             FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
520
521             TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
522             glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
523             TRACE(" 0x%x\n",glyphs[glyph_index]);
524             return glyph_index + 1;
525         }
526     }
527     return GSUB_E_NOGLYPH;
528 }
529
530 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
531 {
532     int j;
533
534     TRACE("Ligature Substitution Subtable\n");
535     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
536     {
537         const GSUB_LigatureSubstFormat1 *lsf1;
538         int offset,index;
539
540         offset = GET_BE_WORD(look->SubTable[j]);
541         lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
542         offset = GET_BE_WORD(lsf1->Coverage);
543         index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
544         TRACE("  Coverage index %i\n",index);
545         if (index != -1)
546         {
547             const GSUB_LigatureSet *ls;
548             int k, count;
549
550             offset = GET_BE_WORD(lsf1->LigatureSet[index]);
551             ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
552             count = GET_BE_WORD(ls->LigatureCount);
553             TRACE("  LigatureSet has %i members\n",count);
554             for (k = 0; k < count; k++)
555             {
556                 const GSUB_Ligature *lig;
557                 int CompCount,l,CompIndex;
558
559                 offset = GET_BE_WORD(ls->Ligature[k]);
560                 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
561                 CompCount = GET_BE_WORD(lig->CompCount) - 1;
562                 CompIndex = glyph_index+write_dir;
563                 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
564                 {
565                     int CompGlyph;
566                     CompGlyph = GET_BE_WORD(lig->Component[l]);
567                     if (CompGlyph != glyphs[CompIndex])
568                         break;
569                     CompIndex += write_dir;
570                 }
571                 if (l == CompCount)
572                 {
573                     int replaceIdx = glyph_index;
574                     if (write_dir < 0)
575                         replaceIdx = glyph_index - CompCount;
576
577                     TRACE("    Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
578                     glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
579                     TRACE("0x%x\n",glyphs[replaceIdx]);
580                     if (CompCount > 0)
581                     {
582                         int j;
583                         for (j = replaceIdx + 1; j < *glyph_count; j++)
584                             glyphs[j] =glyphs[j+CompCount];
585                         *glyph_count = *glyph_count - CompCount;
586                     }
587                     return replaceIdx + 1;
588                 }
589             }
590         }
591     }
592     return GSUB_E_NOGLYPH;
593 }
594
595 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
596 {
597     int j;
598     BOOL done = FALSE;
599
600     TRACE("Chaining Contextual Substitution Subtable\n");
601     for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
602     {
603         const GSUB_ChainContextSubstFormat1 *ccsf1;
604         int offset;
605         int dirLookahead = write_dir;
606         int dirBacktrack = -1 * write_dir;
607
608         offset = GET_BE_WORD(look->SubTable[j]);
609         ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
610         if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
611         {
612             FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
613             return -1;
614         }
615         else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
616         {
617             FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
618             return -1;
619         }
620         else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
621         {
622             int k;
623             int indexGlyphs;
624             const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
625             const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
626             const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
627             const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
628             int newIndex = glyph_index;
629
630             ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
631
632             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
633
634             for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
635             {
636                 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
637                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
638                     break;
639             }
640             if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
641                 return -1;
642             TRACE("Matched Backtrack\n");
643
644             ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
645
646             indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
647             for (k = 0; k < indexGlyphs; k++)
648             {
649                 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
650                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
651                     break;
652             }
653             if (k != indexGlyphs)
654                 return -1;
655             TRACE("Matched IndexGlyphs\n");
656
657             ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
658
659             for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
660             {
661                 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
662                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
663                     break;
664             }
665             if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
666                 return -1;
667             TRACE("Matched LookAhead\n");
668
669             ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
670
671             for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
672             {
673                 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
674                 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
675
676                 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
677                 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
678                 if (newIndex == -1)
679                 {
680                     ERR("Chain failed to generate a glyph\n");
681                     return -1;
682                 }
683             }
684             return newIndex + 1;
685         }
686     }
687     return -1;
688 }
689
690 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
691 {
692     int offset;
693     const GSUB_LookupTable *look;
694
695     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
696     look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
697     TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
698     switch(GET_BE_WORD(look->LookupType))
699     {
700         case 1:
701             return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
702         case 3:
703             return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
704         case 4:
705             return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
706         case 6:
707             return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
708         default:
709             FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
710     }
711     return GSUB_E_NOGLYPH;
712 }
713
714 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
715 {
716     int i;
717     int out_index = GSUB_E_NOGLYPH;
718     const GSUB_LookupList *lookup;
719
720     lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
721
722     TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
723     for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
724     {
725         out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
726         if (out_index != GSUB_E_NOGLYPH)
727             break;
728     }
729     if (out_index == GSUB_E_NOGLYPH)
730         TRACE("lookups found no glyphs\n");
731     return out_index;
732 }
733
734 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
735 {
736     UINT charset;
737
738     if (ShapingData[psa->eScript].otTag[0] != 0)
739         return ShapingData[psa->eScript].otTag;
740
741     /*
742      * fall back to the font charset
743      */
744     charset = GetTextCharsetInfo(hdc, NULL, 0x0);
745     switch (charset)
746     {
747         case ANSI_CHARSET: return "latn";
748         case BALTIC_CHARSET: return "latn"; /* ?? */
749         case CHINESEBIG5_CHARSET: return "hani";
750         case EASTEUROPE_CHARSET: return "latn"; /* ?? */
751         case GB2312_CHARSET: return "hani";
752         case GREEK_CHARSET: return "grek";
753         case HANGUL_CHARSET: return "hang";
754         case RUSSIAN_CHARSET: return "cyrl";
755         case SHIFTJIS_CHARSET: return "kana";
756         case TURKISH_CHARSET: return "latn"; /* ?? */
757         case VIETNAMESE_CHARSET: return "latn";
758         case JOHAB_CHARSET: return "latn"; /* ?? */
759         case ARABIC_CHARSET: return "arab";
760         case HEBREW_CHARSET: return "hebr";
761         case THAI_CHARSET: return "thai";
762         default: return "latn";
763     }
764 }
765
766 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
767 {
768     const GSUB_Feature *feature;
769     int i;
770
771     for (i = 0; i <  psc->feature_count; i++)
772         if (strncmp(psc->features[i].tag,feat,4)==0)
773             return psc->features[i].feature;
774
775     feature = NULL;
776
777     if (psc->GSUB_Table)
778     {
779         const GSUB_Script *script;
780         const GSUB_LangSys *language;
781
782         script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa));
783         if (script)
784         {
785             language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
786             if (language)
787                 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
788         }
789
790         /* try in the default (latin) table */
791         if (!feature)
792         {
793             script = GSUB_get_script_table(psc->GSUB_Table, "latn");
794             if (script)
795             {
796                 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
797                 if (language)
798                     feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
799             }
800         }
801     }
802
803     TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
804
805     psc->feature_count++;
806
807     if (psc->features)
808         psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
809     else
810         psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
811
812     lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
813     psc->features[psc->feature_count - 1].feature = feature;
814     return feature;
815 }
816
817 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)
818 {
819     const GSUB_Feature *feature;
820
821     feature = load_GSUB_feature(hdc, psa, psc, feat);
822     if (!feature)
823         return GSUB_E_NOFEATURE;
824
825     TRACE("applying feature %s\n",feat);
826     return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
827 }
828
829 static VOID *load_gsub_table(HDC hdc)
830 {
831     VOID* GSUB_Table = NULL;
832     int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
833     if (length != GDI_ERROR)
834     {
835         GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
836         GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
837         TRACE("Loaded GSUB table of %i bytes\n",length);
838     }
839     return GSUB_Table;
840 }
841
842 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
843 {
844     if (changeCount == 0)
845         return;
846     else
847     {
848         int i;
849         int target_glyph = nextIndex - 1;
850         int target_index = -1;
851         int replacing_glyph = -1;
852         int changed = 0;
853
854         if (write_dir > 0)
855             for (i = 0; i < chars; i++)
856             {
857                 if (pwLogClust[i] == target_glyph)
858                 {
859                     target_index = i;
860                     break;
861                 }
862             }
863         else
864             for (i = chars - 1; i >= 0; i--)
865             {
866                 if (pwLogClust[i] == target_glyph)
867                 {
868                     target_index = i;
869                     break;
870                 }
871             }
872         if (target_index == -1)
873         {
874             ERR("Unable to find target glyph\n");
875             return;
876         }
877
878         if (changeCount < 0)
879         {
880             /* merge glyphs */
881             for(i = target_index; i < chars && i >= 0; i+=write_dir)
882             {
883                 if (pwLogClust[i] == target_glyph)
884                     continue;
885                 if(pwLogClust[i] == replacing_glyph)
886                     pwLogClust[i] = target_glyph;
887                 else
888                 {
889                     changed--;
890                     if (changed >= changeCount)
891                     {
892                         replacing_glyph = pwLogClust[i];
893                         pwLogClust[i] = target_glyph;
894                     }
895                     else
896                         break;
897                 }
898             }
899         }
900
901         /* renumber trailing indexes*/
902         for(i = target_index; i < chars && i >= 0; i+=write_dir)
903         {
904             if (pwLogClust[i] != target_glyph)
905                 pwLogClust[i] += changeCount;
906         }
907     }
908 }
909
910 static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, WORD *pwOutGlyphs, int write_dir, INT* pcGlyphs, INT cChars, const char* feat, WORD *pwLogClust )
911 {
912     int i;
913
914     if (psc->GSUB_Table)
915     {
916         const GSUB_Feature *feature;
917
918         feature = load_GSUB_feature(hdc, psa, psc, feat);
919         if (!feature)
920             return GSUB_E_NOFEATURE;
921
922         i = 0;
923         TRACE("applying feature %s\n",debugstr_an(feat,4));
924         while(i < *pcGlyphs)
925         {
926                 INT nextIndex;
927                 INT prevCount = *pcGlyphs;
928                 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
929                 if (nextIndex > GSUB_E_NOGLYPH)
930                 {
931                     UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
932                     i = nextIndex;
933                 }
934                 else
935                     i++;
936         }
937         return *pcGlyphs;
938     }
939     return GSUB_E_NOFEATURE;
940 }
941
942 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
943 {
944     if (i + delta < 0)
945         return 0;
946     if ( i+ delta >= cchLen)
947         return 0;
948
949     i += delta;
950
951     return chars[i];
952 }
953
954 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
955 {
956     if (i + delta < 0)
957     {
958         if (psa->fLinkBefore)
959             return jtR;
960         else
961             return jtU;
962     }
963     if ( i+ delta >= cchLen)
964     {
965         if (psa->fLinkAfter)
966             return jtL;
967         else
968             return jtU;
969     }
970
971     i += delta;
972
973     if (context_type[i] == jtT)
974         return neighbour_joining_type(i,delta,context_type,cchLen,psa);
975     else
976         return context_type[i];
977 }
978
979 static inline BOOL right_join_causing(CHAR joining_type)
980 {
981     return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
982 }
983
984 static inline BOOL left_join_causing(CHAR joining_type)
985 {
986     return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
987 }
988
989 static inline BOOL word_break_causing(WCHAR chr)
990 {
991     /* we are working within a string of characters already guareented to
992        be within one script, Syriac, so we do not worry about any characers
993        other than the space character outside of that range */
994     return (chr == 0 || chr == 0x20 );
995 }
996
997 /*
998  * ContextualShape_Arabic
999  */
1000 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1001 {
1002     CHAR *context_type;
1003     INT *context_shape;
1004     INT dirR, dirL;
1005     int i;
1006
1007     if (*pcGlyphs != cChars)
1008     {
1009         ERR("Number of Glyphs and Chars need to match at the beginning\n");
1010         return;
1011     }
1012
1013     if (!psa->fLogicalOrder && psa->fRTL)
1014     {
1015         dirR = 1;
1016         dirL = -1;
1017     }
1018     else
1019     {
1020         dirR = -1;
1021         dirL = 1;
1022     }
1023
1024     if (!psc->GSUB_Table)
1025         psc->GSUB_Table = load_gsub_table(hdc);
1026
1027     context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1028     context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1029
1030     for (i = 0; i < cChars; i++)
1031         context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1032
1033     for (i = 0; i < cChars; i++)
1034     {
1035         if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1036             context_shape[i] = Xr;
1037         else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1038             context_shape[i] = Xl;
1039         else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)) && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1040             context_shape[i] = Xm;
1041         else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1042             context_shape[i] = Xr;
1043         else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1044             context_shape[i] = Xl;
1045         else
1046             context_shape[i] = Xn;
1047     }
1048
1049     /* Contextual Shaping */
1050     i = 0;
1051     while(i < *pcGlyphs)
1052     {
1053         BOOL shaped = FALSE;
1054
1055         if (psc->GSUB_Table)
1056         {
1057             INT nextIndex;
1058             INT prevCount = *pcGlyphs;
1059             nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1060             if (nextIndex > GSUB_E_NOGLYPH)
1061             {
1062                 i = nextIndex;
1063                 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1064             }
1065             shaped = (nextIndex > GSUB_E_NOGLYPH);
1066         }
1067
1068         if (!shaped)
1069         {
1070             WORD newGlyph = pwOutGlyphs[i];
1071             if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1072             {
1073                 /* fall back to presentation form B */
1074                 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1075                 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1076                     pwOutGlyphs[i] = newGlyph;
1077             }
1078             i++;
1079         }
1080     }
1081
1082     HeapFree(GetProcessHeap(),0,context_shape);
1083     HeapFree(GetProcessHeap(),0,context_type);
1084 }
1085
1086 /*
1087  * ContextualShape_Syriac
1088  */
1089
1090 #define ALAPH 0x710
1091 #define DALATH 0x715
1092 #define RISH 0x72A
1093
1094 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1095 {
1096     CHAR *context_type;
1097     INT *context_shape;
1098     INT dirR, dirL;
1099     int i;
1100
1101     if (*pcGlyphs != cChars)
1102     {
1103         ERR("Number of Glyphs and Chars need to match at the beginning\n");
1104         return;
1105     }
1106
1107     if (!psa->fLogicalOrder && psa->fRTL)
1108     {
1109         dirR = 1;
1110         dirL = -1;
1111     }
1112     else
1113     {
1114         dirR = -1;
1115         dirL = 1;
1116     }
1117
1118     if (!psc->GSUB_Table)
1119         psc->GSUB_Table = load_gsub_table(hdc);
1120
1121     if (!psc->GSUB_Table)
1122         return;
1123
1124     context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1125     context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1126
1127     for (i = 0; i < cChars; i++)
1128         context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1129
1130     for (i = 0; i < cChars; i++)
1131     {
1132         if (pwcChars[i] == ALAPH)
1133         {
1134             WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1135
1136             if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1137             context_shape[i] = Afj;
1138             else if ( rchar != DALATH && rchar != RISH &&
1139 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1140 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1141             context_shape[i] = Afn;
1142             else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1143             context_shape[i] = Afx;
1144         }
1145         else if (context_type[i] == jtR &&
1146 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1147             context_shape[i] = Xr;
1148         else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1149             context_shape[i] = Xl;
1150         else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)) && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1151             context_shape[i] = Xm;
1152         else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1153             context_shape[i] = Xr;
1154         else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1155             context_shape[i] = Xl;
1156         else
1157             context_shape[i] = Xn;
1158     }
1159
1160     /* Contextual Shaping */
1161     i = 0;
1162     while(i < *pcGlyphs)
1163     {
1164         INT nextIndex;
1165         INT prevCount = *pcGlyphs;
1166         nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1167         if (nextIndex > GSUB_E_NOGLYPH)
1168         {
1169             UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1170             i = nextIndex;
1171         }
1172     }
1173
1174     HeapFree(GetProcessHeap(),0,context_shape);
1175     HeapFree(GetProcessHeap(),0,context_type);
1176 }
1177
1178 /*
1179  * ContextualShape_Phags_pa
1180  */
1181
1182 #define phags_pa_CANDRABINDU  0xA873
1183 #define phags_pa_START 0xA840
1184 #define phags_pa_END  0xA87F
1185
1186 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1187 {
1188     INT *context_shape;
1189     INT dirR, dirL;
1190     int i;
1191
1192     if (*pcGlyphs != cChars)
1193     {
1194         ERR("Number of Glyphs and Chars need to match at the beginning\n");
1195         return;
1196     }
1197
1198     if (!psa->fLogicalOrder && psa->fRTL)
1199     {
1200         dirR = 1;
1201         dirL = -1;
1202     }
1203     else
1204     {
1205         dirR = -1;
1206         dirL = 1;
1207     }
1208
1209     if (!psc->GSUB_Table)
1210         psc->GSUB_Table = load_gsub_table(hdc);
1211
1212     if (!psc->GSUB_Table)
1213         return;
1214
1215     context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1216
1217     for (i = 0; i < cChars; i++)
1218     {
1219         if (pwcChars[i] >= phags_pa_START && pwcChars[i] <=  phags_pa_END)
1220         {
1221             WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1222             WCHAR lchar = neighbour_char(i,dirL,pwcChars,cChars);
1223             BOOL jrchar = (rchar != phags_pa_CANDRABINDU && rchar >= phags_pa_START && rchar <=  phags_pa_END);
1224             BOOL jlchar = (lchar != phags_pa_CANDRABINDU && lchar >= phags_pa_START && lchar <=  phags_pa_END);
1225
1226             if (jrchar && jlchar)
1227                 context_shape[i] = Xm;
1228             else if (jrchar)
1229                 context_shape[i] = Xr;
1230             else if (jlchar)
1231                 context_shape[i] = Xl;
1232             else
1233                 context_shape[i] = Xn;
1234         }
1235         else
1236             context_shape[i] = -1;
1237     }
1238
1239     /* Contextual Shaping */
1240     i = 0;
1241     while(i < *pcGlyphs)
1242     {
1243         if (context_shape[i] >= 0)
1244         {
1245             INT nextIndex;
1246             INT prevCount = *pcGlyphs;
1247             nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1248             if (nextIndex > GSUB_E_NOGLYPH)
1249             {
1250                 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1251                 i = nextIndex;
1252             }
1253             else
1254                 i++;
1255         }
1256         else
1257             i++;
1258     }
1259
1260     HeapFree(GetProcessHeap(),0,context_shape);
1261 }
1262
1263 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1264 {
1265     if (ShapingData[psa->eScript].contextProc)
1266         ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1267 }
1268
1269 void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1270 {
1271     int i;
1272     INT dirL;
1273
1274     if (!rpRangeProperties)
1275         return;
1276
1277     if (!psc->GSUB_Table)
1278         psc->GSUB_Table = load_gsub_table(hdc);
1279
1280     if (!psc->GSUB_Table)
1281         return;
1282
1283     if (!psa->fLogicalOrder && psa->fRTL)
1284         dirL = -1;
1285     else
1286         dirL = 1;
1287
1288     for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1289     {
1290         if (rpRangeProperties->potfRecords[i].lParameter > 0)
1291         apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1292     }
1293 }
1294
1295 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1296 {
1297 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1298 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1299
1300     SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);
1301 }