usp10: Implement GPOS Coverage-based Chaining Context Glyph Positioning.
[wine] / dlls / usp10 / opentype.c
1 /*
2  * Opentype font interfaces for the Uniscribe Script Processor (usp10.dll)
3  *
4  * Copyright 2012 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 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "usp10.h"
30 #include "winternl.h"
31
32 #include "usp10_internal.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
37
38 #ifdef WORDS_BIGENDIAN
39 #define GET_BE_WORD(x) (x)
40 #define GET_BE_DWORD(x) (x)
41 #else
42 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
43 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
44 #endif
45
46 /* These are all structures needed for the cmap format 12 table */
47 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
48
49 typedef struct {
50     WORD platformID;
51     WORD encodingID;
52     DWORD offset;
53 } CMAP_EncodingRecord;
54
55 typedef struct {
56     WORD version;
57     WORD numTables;
58     CMAP_EncodingRecord tables[1];
59 } CMAP_Header;
60
61 typedef struct {
62     DWORD startCharCode;
63     DWORD endCharCode;
64     DWORD startGlyphID;
65 } CMAP_SegmentedCoverage_group;
66
67 typedef struct {
68     WORD format;
69     WORD reserved;
70     DWORD length;
71     DWORD language;
72     DWORD nGroups;
73     CMAP_SegmentedCoverage_group groups[1];
74 } CMAP_SegmentedCoverage;
75
76 /* These are all structures needed for the GDEF table */
77 #define GDEF_TAG MS_MAKE_TAG('G', 'D', 'E', 'F')
78
79 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
80
81 typedef struct {
82     DWORD Version;
83     WORD GlyphClassDef;
84     WORD AttachList;
85     WORD LigCaretList;
86     WORD MarkAttachClassDef;
87 } GDEF_Header;
88
89 typedef struct {
90     WORD ClassFormat;
91     WORD StartGlyph;
92     WORD GlyphCount;
93     WORD ClassValueArray[1];
94 } GDEF_ClassDefFormat1;
95
96 typedef struct {
97     WORD Start;
98     WORD End;
99     WORD Class;
100 } GDEF_ClassRangeRecord;
101
102 typedef struct {
103     WORD ClassFormat;
104     WORD ClassRangeCount;
105     GDEF_ClassRangeRecord ClassRangeRecord[1];
106 } GDEF_ClassDefFormat2;
107
108 /* These are all structures needed for the GSUB table */
109
110 typedef struct {
111     DWORD version;
112     WORD ScriptList;
113     WORD FeatureList;
114     WORD LookupList;
115 } GSUB_Header;
116
117 typedef struct {
118     CHAR ScriptTag[4];
119     WORD Script;
120 } OT_ScriptRecord;
121
122 typedef struct {
123     WORD ScriptCount;
124     OT_ScriptRecord ScriptRecord[1];
125 } OT_ScriptList;
126
127 typedef struct {
128     CHAR LangSysTag[4];
129     WORD LangSys;
130 } OT_LangSysRecord;
131
132 typedef struct {
133     WORD DefaultLangSys;
134     WORD LangSysCount;
135     OT_LangSysRecord LangSysRecord[1];
136 } OT_Script;
137
138 typedef struct {
139     WORD LookupOrder; /* Reserved */
140     WORD ReqFeatureIndex;
141     WORD FeatureCount;
142     WORD FeatureIndex[1];
143 } OT_LangSys;
144
145 typedef struct {
146     CHAR FeatureTag[4];
147     WORD Feature;
148 } OT_FeatureRecord;
149
150 typedef struct {
151     WORD FeatureCount;
152     OT_FeatureRecord FeatureRecord[1];
153 } OT_FeatureList;
154
155 typedef struct {
156     WORD FeatureParams; /* Reserved */
157     WORD LookupCount;
158     WORD LookupListIndex[1];
159 } OT_Feature;
160
161 typedef struct {
162     WORD LookupCount;
163     WORD Lookup[1];
164 } OT_LookupList;
165
166 typedef struct {
167     WORD LookupType;
168     WORD LookupFlag;
169     WORD SubTableCount;
170     WORD SubTable[1];
171 } OT_LookupTable;
172
173 typedef struct {
174     WORD CoverageFormat;
175     WORD GlyphCount;
176     WORD GlyphArray[1];
177 } OT_CoverageFormat1;
178
179 typedef struct {
180     WORD Start;
181     WORD End;
182     WORD StartCoverageIndex;
183 } OT_RangeRecord;
184
185 typedef struct {
186     WORD CoverageFormat;
187     WORD RangeCount;
188     OT_RangeRecord RangeRecord[1];
189 } OT_CoverageFormat2;
190
191 typedef struct {
192     WORD SubstFormat; /* = 1 */
193     WORD Coverage;
194     WORD DeltaGlyphID;
195 } GSUB_SingleSubstFormat1;
196
197 typedef struct {
198     WORD SubstFormat; /* = 2 */
199     WORD Coverage;
200     WORD GlyphCount;
201     WORD Substitute[1];
202 }GSUB_SingleSubstFormat2;
203
204 typedef struct {
205     WORD SubstFormat; /* = 1 */
206     WORD Coverage;
207     WORD SequenceCount;
208     WORD Sequence[1];
209 }GSUB_MultipleSubstFormat1;
210
211 typedef struct {
212     WORD GlyphCount;
213     WORD Substitute[1];
214 }GSUB_Sequence;
215
216 typedef struct {
217     WORD SubstFormat; /* = 1 */
218     WORD Coverage;
219     WORD LigSetCount;
220     WORD LigatureSet[1];
221 }GSUB_LigatureSubstFormat1;
222
223 typedef struct {
224     WORD LigatureCount;
225     WORD Ligature[1];
226 }GSUB_LigatureSet;
227
228 typedef struct{
229     WORD LigGlyph;
230     WORD CompCount;
231     WORD Component[1];
232 }GSUB_Ligature;
233
234 typedef struct{
235     WORD SequenceIndex;
236     WORD LookupListIndex;
237
238 }GSUB_SubstLookupRecord;
239
240 typedef struct{
241     WORD SubstFormat; /* = 1 */
242     WORD Coverage;
243     WORD ChainSubRuleSetCount;
244     WORD ChainSubRuleSet[1];
245 }GSUB_ChainContextSubstFormat1;
246
247 typedef struct {
248     WORD SubstFormat; /* = 3 */
249     WORD BacktrackGlyphCount;
250     WORD Coverage[1];
251 }GSUB_ChainContextSubstFormat3_1;
252
253 typedef struct{
254     WORD InputGlyphCount;
255     WORD Coverage[1];
256 }GSUB_ChainContextSubstFormat3_2;
257
258 typedef struct{
259     WORD LookaheadGlyphCount;
260     WORD Coverage[1];
261 }GSUB_ChainContextSubstFormat3_3;
262
263 typedef struct{
264     WORD SubstCount;
265     GSUB_SubstLookupRecord SubstLookupRecord[1];
266 }GSUB_ChainContextSubstFormat3_4;
267
268 typedef struct {
269     WORD SubstFormat; /* = 1 */
270     WORD Coverage;
271     WORD AlternateSetCount;
272     WORD AlternateSet[1];
273 } GSUB_AlternateSubstFormat1;
274
275 typedef struct{
276     WORD GlyphCount;
277     WORD Alternate[1];
278 } GSUB_AlternateSet;
279
280 /* These are all structures needed for the GPOS table */
281
282 typedef struct {
283     DWORD version;
284     WORD ScriptList;
285     WORD FeatureList;
286     WORD LookupList;
287 } GPOS_Header;
288
289 typedef struct {
290     WORD StartSize;
291     WORD EndSize;
292     WORD DeltaFormat;
293     WORD DeltaValue[1];
294 } OT_DeviceTable;
295
296 typedef struct {
297     WORD AnchorFormat;
298     WORD XCoordinate;
299     WORD YCoordinate;
300 } GPOS_AnchorFormat1;
301
302 typedef struct {
303     WORD AnchorFormat;
304     WORD XCoordinate;
305     WORD YCoordinate;
306     WORD AnchorPoint;
307 } GPOS_AnchorFormat2;
308
309 typedef struct {
310     WORD AnchorFormat;
311     WORD XCoordinate;
312     WORD YCoordinate;
313     WORD XDeviceTable;
314     WORD YDeviceTable;
315 } GPOS_AnchorFormat3;
316
317 typedef struct {
318     WORD XPlacement;
319     WORD YPlacement;
320     WORD XAdvance;
321     WORD YAdvance;
322     WORD XPlaDevice;
323     WORD YPlaDevice;
324     WORD XAdvDevice;
325     WORD YAdvDevice;
326 } GPOS_ValueRecord;
327
328 typedef struct {
329     WORD PosFormat;
330     WORD Coverage;
331     WORD ValueFormat;
332     WORD Value[1];
333 } GPOS_SinglePosFormat1;
334
335 typedef struct {
336     WORD PosFormat;
337     WORD Coverage;
338     WORD ValueFormat;
339     WORD ValueCount;
340     WORD Value[1];
341 } GPOS_SinglePosFormat2;
342
343 typedef struct {
344     WORD PosFormat;
345     WORD Coverage;
346     WORD ValueFormat1;
347     WORD ValueFormat2;
348     WORD PairSetCount;
349     WORD PairSetOffset[1];
350 } GPOS_PairPosFormat1;
351
352 typedef struct {
353     WORD SecondGlyph;
354     WORD Value1[1];
355     WORD Value2[1];
356 } GPOS_PairValueRecord;
357
358 typedef struct {
359     WORD PairValueCount;
360     GPOS_PairValueRecord PairValueRecord[1];
361 } GPOS_PairSet;
362
363 typedef struct {
364     WORD PosFormat;
365     WORD MarkCoverage;
366     WORD BaseCoverage;
367     WORD ClassCount;
368     WORD MarkArray;
369     WORD BaseArray;
370 } GPOS_MarkBasePosFormat1;
371
372 typedef struct {
373     WORD BaseAnchor[1];
374 } GPOS_BaseRecord;
375
376 typedef struct {
377     WORD BaseCount;
378     GPOS_BaseRecord BaseRecord[1];
379 } GPOS_BaseArray;
380
381 typedef struct {
382     WORD Class;
383     WORD MarkAnchor;
384 } GPOS_MarkRecord;
385
386 typedef struct {
387     WORD MarkCount;
388     GPOS_MarkRecord MarkRecord[1];
389 } GPOS_MarkArray;
390
391 typedef struct {
392     WORD SequenceIndex;
393     WORD LookupListIndex;
394 } GPOS_PosLookupRecord;
395
396 typedef struct {
397     WORD PosFormat;
398     WORD BacktrackGlyphCount;
399     WORD Coverage[1];
400 } GPOS_ChainContextPosFormat3_1;
401
402 typedef struct {
403     WORD InputGlyphCount;
404     WORD Coverage[1];
405 } GPOS_ChainContextPosFormat3_2;
406
407 typedef struct {
408     WORD LookaheadGlyphCount;
409     WORD Coverage[1];
410 } GPOS_ChainContextPosFormat3_3;
411
412 typedef struct {
413     WORD PosCount;
414     GPOS_PosLookupRecord PosLookupRecord[1];
415 } GPOS_ChainContextPosFormat3_4;
416
417 /**********
418  * CMAP
419  **********/
420
421 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
422 {
423     CMAP_Header *CMAP_Table = NULL;
424     int length;
425     int i;
426
427     if (!psc->CMAP_Table)
428     {
429         length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
430         if (length != GDI_ERROR)
431         {
432             psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
433             GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
434             TRACE("Loaded cmap table of %i bytes\n",length);
435         }
436         else
437             return NULL;
438     }
439
440     CMAP_Table = psc->CMAP_Table;
441
442     for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
443     {
444         if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
445              (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
446         {
447             CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
448             if (GET_BE_WORD(format->format) == 12)
449                 return format;
450         }
451     }
452     return NULL;
453 }
454
455 static int compare_group(const void *a, const void* b)
456 {
457     const DWORD *chr = a;
458     const CMAP_SegmentedCoverage_group *group = b;
459
460     if (*chr < GET_BE_DWORD(group->startCharCode))
461         return -1;
462     if (*chr > GET_BE_DWORD(group->endCharCode))
463         return 1;
464     return 0;
465 }
466
467 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
468 {
469     /* BMP: use gdi32 for ease */
470     if (utf32c < 0x10000)
471     {
472         WCHAR ch = utf32c;
473         return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
474     }
475
476     if (!psc->CMAP_format12_Table)
477         psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
478
479     if (flags & GGI_MARK_NONEXISTING_GLYPHS)
480         *pgi = 0xffff;
481     else
482         *pgi = 0;
483
484     if (psc->CMAP_format12_Table)
485     {
486         CMAP_SegmentedCoverage *format = NULL;
487         CMAP_SegmentedCoverage_group *group = NULL;
488
489         format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
490
491         group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
492                         sizeof(CMAP_SegmentedCoverage_group), compare_group);
493
494         if (group)
495         {
496             DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
497             *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
498             return 0;
499         }
500     }
501     return 0;
502 }
503
504 /**********
505  * GDEF
506  **********/
507
508 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
509 {
510     int offset;
511     WORD class = 0;
512     const GDEF_ClassDefFormat1 *cf1;
513
514     if (!header)
515         return 0;
516
517     offset = GET_BE_WORD(header->GlyphClassDef);
518     if (!offset)
519         return 0;
520
521     cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
522     if (GET_BE_WORD(cf1->ClassFormat) == 1)
523     {
524         if (glyph >= GET_BE_WORD(cf1->StartGlyph))
525         {
526             int index = glyph - GET_BE_WORD(cf1->StartGlyph);
527             if (index < GET_BE_WORD(cf1->GlyphCount))
528                 class = GET_BE_WORD(cf1->ClassValueArray[index]);
529         }
530     }
531     else if (GET_BE_WORD(cf1->ClassFormat) == 2)
532     {
533         const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
534         int i, top;
535         top = GET_BE_WORD(cf2->ClassRangeCount);
536         for (i = 0; i < top; i++)
537         {
538             if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
539                 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
540             {
541                 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
542                 break;
543             }
544         }
545     }
546     else
547         ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
548
549     return class;
550 }
551
552 static VOID *load_gdef_table(HDC hdc)
553 {
554     VOID* GDEF_Table = NULL;
555     int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
556     if (length != GDI_ERROR)
557     {
558         GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
559         GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
560         TRACE("Loaded GDEF table of %i bytes\n",length);
561     }
562     return GDEF_Table;
563 }
564
565 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
566 {
567     int i;
568
569     if (!psc->GDEF_Table)
570         psc->GDEF_Table = load_gdef_table(hdc);
571
572     for (i = 0; i < cGlyphs; i++)
573     {
574         WORD class;
575         int char_count = 0;
576         int k;
577
578         k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
579         if (k >= 0)
580         {
581             for (; k < cChars && pwLogClust[k] == i; k++)
582                 char_count++;
583         }
584
585         class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
586
587         switch (class)
588         {
589             case 0:
590             case BaseGlyph:
591                 pGlyphProp[i].sva.fClusterStart = 1;
592                 pGlyphProp[i].sva.fDiacritic = 0;
593                 pGlyphProp[i].sva.fZeroWidth = 0;
594                 break;
595             case LigatureGlyph:
596                 pGlyphProp[i].sva.fClusterStart = 1;
597                 pGlyphProp[i].sva.fDiacritic = 0;
598                 pGlyphProp[i].sva.fZeroWidth = 0;
599                 break;
600             case MarkGlyph:
601                 pGlyphProp[i].sva.fClusterStart = 0;
602                 pGlyphProp[i].sva.fDiacritic = 1;
603                 pGlyphProp[i].sva.fZeroWidth = 1;
604                 break;
605             case ComponentGlyph:
606                 pGlyphProp[i].sva.fClusterStart = 0;
607                 pGlyphProp[i].sva.fDiacritic = 0;
608                 pGlyphProp[i].sva.fZeroWidth = 0;
609                 break;
610             default:
611                 ERR("Unknown glyph class %i\n",class);
612                 pGlyphProp[i].sva.fClusterStart = 1;
613                 pGlyphProp[i].sva.fDiacritic = 0;
614                 pGlyphProp[i].sva.fZeroWidth = 0;
615         }
616
617         if (char_count == 0)
618             pGlyphProp[i].sva.fClusterStart = 0;
619     }
620 }
621
622 /**********
623  * GSUB
624  **********/
625 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
626
627 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
628 {
629     const OT_CoverageFormat1* cf1;
630
631     cf1 = table;
632
633     if (GET_BE_WORD(cf1->CoverageFormat) == 1)
634     {
635         int count = GET_BE_WORD(cf1->GlyphCount);
636         int i;
637         TRACE("Coverage Format 1, %i glyphs\n",count);
638         for (i = 0; i < count; i++)
639             if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
640                 return i;
641         return -1;
642     }
643     else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
644     {
645         const OT_CoverageFormat2* cf2;
646         int i;
647         int count;
648         cf2 = (const OT_CoverageFormat2*)cf1;
649
650         count = GET_BE_WORD(cf2->RangeCount);
651         TRACE("Coverage Format 2, %i ranges\n",count);
652         for (i = 0; i < count; i++)
653         {
654             if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
655                 return -1;
656             if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
657                 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
658             {
659                 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
660                     glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
661             }
662         }
663         return -1;
664     }
665     else
666         ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
667
668     return -1;
669 }
670
671 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
672 {
673     int j;
674     TRACE("Single Substitution Subtable\n");
675
676     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
677     {
678         int offset;
679         const GSUB_SingleSubstFormat1 *ssf1;
680         offset = GET_BE_WORD(look->SubTable[j]);
681         ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
682         if (GET_BE_WORD(ssf1->SubstFormat) == 1)
683         {
684             int offset = GET_BE_WORD(ssf1->Coverage);
685             TRACE("  subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
686             if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
687             {
688                 TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
689                 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
690                 TRACE(" 0x%x\n",glyphs[glyph_index]);
691                 return glyph_index + write_dir;
692             }
693         }
694         else
695         {
696             const GSUB_SingleSubstFormat2 *ssf2;
697             INT index;
698             INT offset;
699
700             ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
701             offset = GET_BE_WORD(ssf1->Coverage);
702             TRACE("  subtype 2,  glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
703             index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
704             TRACE("  Coverage index %i\n",index);
705             if (index != -1)
706             {
707                 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
708                     return GSUB_E_NOGLYPH;
709
710                 TRACE("    Glyph is 0x%x ->",glyphs[glyph_index]);
711                 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
712                 TRACE("0x%x\n",glyphs[glyph_index]);
713                 return glyph_index + write_dir;
714             }
715         }
716     }
717     return GSUB_E_NOGLYPH;
718 }
719
720 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
721 {
722     int j;
723     TRACE("Multiple Substitution Subtable\n");
724
725     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
726     {
727         int offset, index;
728         const GSUB_MultipleSubstFormat1 *msf1;
729         offset = GET_BE_WORD(look->SubTable[j]);
730         msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
731
732         offset = GET_BE_WORD(msf1->Coverage);
733         index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
734         if (index != -1)
735         {
736             const GSUB_Sequence *seq;
737             int sub_count;
738             int j;
739             offset = GET_BE_WORD(msf1->Sequence[index]);
740             seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
741             sub_count = GET_BE_WORD(seq->GlyphCount);
742             TRACE("  Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
743
744             for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
745                     glyphs[j] =glyphs[j-(sub_count-1)];
746
747             for (j = 0; j < sub_count; j++)
748                     if (write_dir < 0)
749                         glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
750                     else
751                         glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
752
753             *glyph_count = *glyph_count + (sub_count - 1);
754
755             if (TRACE_ON(uniscribe))
756             {
757                 for (j = 0; j < sub_count; j++)
758                     TRACE(" 0x%x",glyphs[glyph_index+j]);
759                 TRACE("\n");
760             }
761
762             return glyph_index + (sub_count * write_dir);
763         }
764     }
765     return GSUB_E_NOGLYPH;
766 }
767
768 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
769 {
770     int j;
771     TRACE("Alternate Substitution Subtable\n");
772
773     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
774     {
775         int offset;
776         const GSUB_AlternateSubstFormat1 *asf1;
777         INT index;
778
779         offset = GET_BE_WORD(look->SubTable[j]);
780         asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
781         offset = GET_BE_WORD(asf1->Coverage);
782
783         index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
784         if (index != -1)
785         {
786             const GSUB_AlternateSet *as;
787             offset =  GET_BE_WORD(asf1->AlternateSet[index]);
788             as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
789             FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
790             if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
791                 return GSUB_E_NOGLYPH;
792
793             TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
794             glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
795             TRACE(" 0x%x\n",glyphs[glyph_index]);
796             return glyph_index + write_dir;
797         }
798     }
799     return GSUB_E_NOGLYPH;
800 }
801
802 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
803 {
804     int j;
805
806     TRACE("Ligature Substitution Subtable\n");
807     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
808     {
809         const GSUB_LigatureSubstFormat1 *lsf1;
810         int offset,index;
811
812         offset = GET_BE_WORD(look->SubTable[j]);
813         lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
814         offset = GET_BE_WORD(lsf1->Coverage);
815         index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
816         TRACE("  Coverage index %i\n",index);
817         if (index != -1)
818         {
819             const GSUB_LigatureSet *ls;
820             int k, count;
821
822             offset = GET_BE_WORD(lsf1->LigatureSet[index]);
823             ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
824             count = GET_BE_WORD(ls->LigatureCount);
825             TRACE("  LigatureSet has %i members\n",count);
826             for (k = 0; k < count; k++)
827             {
828                 const GSUB_Ligature *lig;
829                 int CompCount,l,CompIndex;
830
831                 offset = GET_BE_WORD(ls->Ligature[k]);
832                 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
833                 CompCount = GET_BE_WORD(lig->CompCount) - 1;
834                 CompIndex = glyph_index+write_dir;
835                 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
836                 {
837                     int CompGlyph;
838                     CompGlyph = GET_BE_WORD(lig->Component[l]);
839                     if (CompGlyph != glyphs[CompIndex])
840                         break;
841                     CompIndex += write_dir;
842                 }
843                 if (l == CompCount)
844                 {
845                     int replaceIdx = glyph_index;
846                     if (write_dir < 0)
847                         replaceIdx = glyph_index - CompCount;
848
849                     TRACE("    Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
850                     glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
851                     TRACE("0x%x\n",glyphs[replaceIdx]);
852                     if (CompCount > 0)
853                     {
854                         int j;
855                         for (j = replaceIdx + 1; j < *glyph_count; j++)
856                             glyphs[j] =glyphs[j+CompCount];
857                         *glyph_count = *glyph_count - CompCount;
858                     }
859                     return replaceIdx + write_dir;
860                 }
861             }
862         }
863     }
864     return GSUB_E_NOGLYPH;
865 }
866
867 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
868 {
869     int j;
870     BOOL done = FALSE;
871
872     TRACE("Chaining Contextual Substitution Subtable\n");
873     for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
874     {
875         const GSUB_ChainContextSubstFormat1 *ccsf1;
876         int offset;
877         int dirLookahead = write_dir;
878         int dirBacktrack = -1 * write_dir;
879
880         offset = GET_BE_WORD(look->SubTable[j]);
881         ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
882         if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
883         {
884             FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
885             continue;
886         }
887         else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
888         {
889             FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
890             continue;
891         }
892         else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
893         {
894             int k;
895             int indexGlyphs;
896             const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
897             const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
898             const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
899             const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
900             int newIndex = glyph_index;
901
902             ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
903
904             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
905
906             for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
907             {
908                 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
909                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
910                     break;
911             }
912             if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
913                 continue;
914             TRACE("Matched Backtrack\n");
915
916             ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
917
918             indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
919             for (k = 0; k < indexGlyphs; k++)
920             {
921                 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
922                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
923                     break;
924             }
925             if (k != indexGlyphs)
926                 continue;
927             TRACE("Matched IndexGlyphs\n");
928
929             ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
930
931             for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
932             {
933                 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
934                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
935                     break;
936             }
937             if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
938                 continue;
939             TRACE("Matched LookAhead\n");
940
941             ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
942
943             if (GET_BE_WORD(ccsf3_4->SubstCount))
944             {
945                 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
946                 {
947                     int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
948                     int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
949
950                     TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
951                     newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
952                     if (newIndex == -1)
953                     {
954                         ERR("Chain failed to generate a glyph\n");
955                         continue;
956                     }
957                 }
958                 return newIndex;
959             }
960             else return GSUB_E_NOGLYPH;
961         }
962     }
963     return -1;
964 }
965
966 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
967 {
968     int offset;
969     const OT_LookupTable *look;
970
971     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
972     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
973     TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
974     switch(GET_BE_WORD(look->LookupType))
975     {
976         case 1:
977             return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
978         case 2:
979             return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
980         case 3:
981             return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
982         case 4:
983             return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
984         case 6:
985             return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
986         default:
987             FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
988     }
989     return GSUB_E_NOGLYPH;
990 }
991
992 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
993 {
994     const GSUB_Header *header = (const GSUB_Header *)table;
995     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
996
997     return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
998 }
999
1000 /**********
1001  * GPOS
1002  **********/
1003 static INT GPOS_apply_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset);
1004
1005 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1006 {
1007     static const WORD mask[3] = {3,0xf,0xff};
1008     if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem  <= GET_BE_WORD(DeviceTable->EndSize))
1009     {
1010         int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1011         int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1012         int value;
1013         TRACE("device table, format %i, index %i\n",format, index);
1014         index = index << format;
1015         value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1016         TRACE("offset %i, value %i\n",index, value);
1017         if (value > mask[format-1]/2)
1018             value = -1 * ((mask[format-1]+1) - value);
1019         return value;
1020     }
1021     return 0;
1022 }
1023
1024 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1025 {
1026     const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1027
1028     switch (GET_BE_WORD(anchor1->AnchorFormat))
1029     {
1030         case 1:
1031         {
1032             TRACE("Anchor Format 1\n");
1033             pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1034             pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1035             break;
1036         }
1037         case 2:
1038         {
1039             const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1040             TRACE("Anchor Format 2\n");
1041             pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1042             pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1043             break;
1044         }
1045         case 3:
1046         {
1047             int offset;
1048             const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1049             TRACE("Anchor Format 3\n");
1050             pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1051             pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1052             offset = GET_BE_WORD(anchor3->XDeviceTable);
1053             TRACE("ppem %i\n",ppem);
1054             if (offset)
1055             {
1056                 const OT_DeviceTable* DeviceTableX = NULL;
1057                 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1058                 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1059             }
1060             offset = GET_BE_WORD(anchor3->YDeviceTable);
1061             if (offset)
1062             {
1063                 const OT_DeviceTable* DeviceTableY = NULL;
1064                 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1065                 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1066             }
1067             break;
1068         }
1069         default:
1070             ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1071             pt->x = 0;
1072             pt->y = 0;
1073     }
1074 }
1075
1076 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1077 {
1078     int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1079
1080     TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1081     *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1082     *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1083     if (lplogfont->lfWidth)
1084         FIXME("Font with lfWidth set no handled properly\n");
1085 }
1086
1087 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1088 {
1089     INT offset = 0;
1090     if (ValueFormat & 0x0001) record->XPlacement = GET_BE_WORD(data[offset++]);
1091     if (ValueFormat & 0x0002) record->YPlacement = GET_BE_WORD(data[offset++]);
1092     if (ValueFormat & 0x0004) record->XAdvance = GET_BE_WORD(data[offset++]);
1093     if (ValueFormat & 0x0008) record->YAdvance = GET_BE_WORD(data[offset++]);
1094     if (ValueFormat & 0x0010) record->XPlaDevice = GET_BE_WORD(data[offset++]);
1095     if (ValueFormat & 0x0020) record->YPlaDevice = GET_BE_WORD(data[offset++]);
1096     if (ValueFormat & 0x0040) record->XAdvDevice = GET_BE_WORD(data[offset++]);
1097     if (ValueFormat & 0x0080) record->YAdvDevice = GET_BE_WORD(data[offset++]);
1098     return offset;
1099 }
1100
1101 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord,  WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1102 {
1103     if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1104     if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1105     if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1106     if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1107     if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1108     if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1109     if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1110     if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1111     if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1112 }
1113
1114 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1115 {
1116     int j;
1117
1118     TRACE("Single Adjustment Positioning Subtable\n");
1119
1120     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1121     {
1122         const GPOS_SinglePosFormat1 *spf1;
1123         WORD offset = GET_BE_WORD(look->SubTable[j]);
1124         spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1125         if (GET_BE_WORD(spf1->PosFormat) == 1)
1126         {
1127             offset = GET_BE_WORD(spf1->Coverage);
1128             if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1129             {
1130                 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1131                 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1132                 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1133                 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
1134                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1135             }
1136         }
1137         else if (GET_BE_WORD(spf1->PosFormat) == 2)
1138         {
1139             int index;
1140             const GPOS_SinglePosFormat2 *spf2;
1141             spf2 = (const GPOS_SinglePosFormat2*)spf1;
1142             offset = GET_BE_WORD(spf2->Coverage);
1143             index  = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1144             if (index != -1)
1145             {
1146                 int size;
1147                 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1148                 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1149                 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1150                 if (index > 0)
1151                 {
1152                     offset = size * index;
1153                     GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1154                 }
1155                 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
1156                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1157             }
1158         }
1159         else
1160             FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1161     }
1162 }
1163
1164 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1165 {
1166     int j;
1167
1168     TRACE("Pair Adjustment Positioning Subtable\n");
1169
1170     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1171     {
1172         const GPOS_PairPosFormat1 *ppf1;
1173         WORD offset = GET_BE_WORD(look->SubTable[j]);
1174         ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1175         if (GET_BE_WORD(ppf1->PosFormat) == 1)
1176         {
1177             offset = GET_BE_WORD(ppf1->Coverage);
1178             if (GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]) != -1)
1179             {
1180                 int i;
1181                 int count = GET_BE_WORD(ppf1->PairSetCount);
1182                 for (i = 0; i < count; i++)
1183                 {
1184                     int k;
1185                     int pair_count;
1186                     const GPOS_PairSet *ps;
1187                     offset = GET_BE_WORD(ppf1->PairSetOffset[i]);
1188                     ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1189                     pair_count = GET_BE_WORD(ps->PairValueCount);
1190                     for (k = 0; k < pair_count; k++)
1191                     {
1192                         if (glyphs[glyph_index+write_dir] == GET_BE_WORD(ps->PairValueRecord[k].SecondGlyph))
1193                         {
1194                             GPOS_ValueRecord ValueRecord1 = {0,0,0,0,0,0,0,0};
1195                             GPOS_ValueRecord ValueRecord2 = {0,0,0,0,0,0,0,0};
1196                             WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1197                             WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1198
1199                             TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1200
1201                             offset = GPOS_get_value_record(ValueFormat1, ps->PairValueRecord[k].Value1, &ValueRecord1);
1202                             GPOS_get_value_record(ValueFormat2, (WORD*)((const BYTE*)(ps->PairValueRecord[k].Value2)+offset), &ValueRecord2);
1203                             if (ValueFormat1)
1204                             {
1205                                 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord1,  ValueFormat1, ppem, &ptAdjust[0], &ptAdvance[0]);
1206                                 TRACE("Glyph 1 resulting cumulative offset is %i,%i design units\n",ptAdjust[0].x,ptAdjust[0].y);
1207                             }
1208                             if (ValueFormat2)
1209                             {
1210                                 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord2,  ValueFormat2, ppem, &ptAdjust[1], &ptAdvance[1]);
1211                                 TRACE("Glyph 2 resulting cumulative offset is %i,%i design units\n",ptAdjust[1].x,ptAdjust[1].y);
1212                                 return glyph_index+2;
1213                             }
1214                         }
1215                     }
1216                 }
1217             }
1218         }
1219         else
1220             FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1221     }
1222     return glyph_index+1;
1223 }
1224
1225 static VOID GPOS_apply_MarkToBase(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT pt)
1226 {
1227     int j;
1228
1229     TRACE("MarkToBase Attachment Positioning Subtable\n");
1230
1231     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1232     {
1233         int offset;
1234         const GPOS_MarkBasePosFormat1 *mbpf1;
1235         offset = GET_BE_WORD(look->SubTable[j]);
1236         mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1237         if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1238         {
1239             int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1240             int mark_index;
1241             mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1242             if (mark_index != -1)
1243             {
1244                 int base_index;
1245                 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1246                 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1247                 if (base_index != -1)
1248                 {
1249                     const GPOS_MarkArray *ma;
1250                     const GPOS_MarkRecord *mr;
1251                     const GPOS_BaseArray *ba;
1252                     const GPOS_BaseRecord *br;
1253                     int mark_class;
1254                     int class_count = GET_BE_WORD(mbpf1->ClassCount);
1255                     int baserecord_size;
1256                     POINT base_pt;
1257                     POINT mark_pt;
1258                     TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1259                     offset = GET_BE_WORD(mbpf1->MarkArray);
1260                     ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1261                     if (mark_index > GET_BE_WORD(ma->MarkCount))
1262                     {
1263                         ERR("Mark index exeeded mark count\n");
1264                         return;
1265                     }
1266                     mr = &ma->MarkRecord[mark_index];
1267                     mark_class = GET_BE_WORD(mr->Class);
1268                     TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1269                     offset = GET_BE_WORD(mbpf1->BaseArray);
1270                     ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1271                     baserecord_size = class_count * sizeof(WORD);
1272                     br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1273                     offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1274                     GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1275                     offset = GET_BE_WORD(mr->MarkAnchor);
1276                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1277                     TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1278                     TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1279                     pt->x += base_pt.x - mark_pt.x;
1280                     pt->y += base_pt.y - mark_pt.y;
1281                     TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1282                 }
1283             }
1284         }
1285         else
1286             FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1287     }
1288 }
1289
1290 static INT GPOS_apply_ChainContextPos(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, GOFFSET *pGoffset)
1291 {
1292     int j;
1293
1294     TRACE("Chaining Contextual Positioning Subtable\n");
1295
1296     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1297     {
1298         int offset;
1299         const GPOS_ChainContextPosFormat3_1 *ccpf3;
1300         int dirLookahead = write_dir;
1301         int dirBacktrack = -1 * write_dir;
1302
1303         offset = GET_BE_WORD(look->SubTable[j]);
1304         ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1305
1306         if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1307         {
1308             FIXME("  TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1309             continue;
1310         }
1311         else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1312         {
1313             FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1314             continue;
1315         }
1316         else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1317         {
1318             int k;
1319             int indexGlyphs;
1320             const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1321             const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1322             const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1323
1324             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1325
1326             for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1327             {
1328                 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1329                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1330                     break;
1331             }
1332             if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1333                 continue;
1334             TRACE("Matched Backtrack\n");
1335
1336             ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)(((LPBYTE)ccpf3)+sizeof(GPOS_ChainContextPosFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccpf3->BacktrackGlyphCount)-1)));
1337
1338             indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1339             for (k = 0; k < indexGlyphs; k++)
1340             {
1341                 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1342                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1343                     break;
1344             }
1345             if (k != indexGlyphs)
1346                 continue;
1347             TRACE("Matched IndexGlyphs\n");
1348
1349             ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)(((LPBYTE)ccpf3_2)+sizeof(GPOS_ChainContextPosFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_2->InputGlyphCount)-1)));
1350
1351             for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1352             {
1353                 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1354                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1355                     break;
1356             }
1357             if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1358                 continue;
1359             TRACE("Matched LookAhead\n");
1360
1361             ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)(((LPBYTE)ccpf3_3)+sizeof(GPOS_ChainContextPosFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)-1)));
1362
1363             if (GET_BE_WORD(ccpf3_4->PosCount))
1364             {
1365                 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1366                 {
1367                     int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1368                     int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1369
1370                     TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1371                     GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count, pGoffset);
1372                 }
1373                 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1374             }
1375             else return glyph_index + 1;
1376         }
1377         else
1378             FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1379     }
1380     return glyph_index + 1;
1381 }
1382
1383 static INT GPOS_apply_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset)
1384 {
1385     int offset;
1386     const OT_LookupTable *look;
1387     int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1388
1389     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1390     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1391     TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1392     switch(GET_BE_WORD(look->LookupType))
1393     {
1394         case 1:
1395         {
1396             double devX, devY;
1397             POINT adjust = {0,0};
1398             POINT advance = {0,0};
1399             GPOS_apply_SingleAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &adjust, &advance);
1400             if (adjust.x || adjust.y)
1401             {
1402                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1403                 pGoffset[glyph_index].du += (int)(devX+0.5);
1404                 pGoffset[glyph_index].dv += (int)(devY+0.5);
1405             }
1406             if (advance.x || advance.y)
1407             {
1408                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1409                 piAdvance[glyph_index] += (int)(devX+0.5);
1410                 if (advance.y)
1411                     FIXME("Unhandled adjustment to Y advancement\n");
1412             }
1413         }
1414         case 2:
1415         {
1416             POINT advance[2]= {{0,0},{0,0}};
1417             POINT adjust[2]= {{0,0},{0,0}};
1418             double devX, devY;
1419             int index;
1420             index = GPOS_apply_PairAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, adjust, advance);
1421             if (adjust[0].x || adjust[0].y)
1422             {
1423                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1424                 pGoffset[glyph_index].du += (int)(devX+0.5);
1425                 pGoffset[glyph_index].dv += (int)(devY+0.5);
1426             }
1427             if (advance[0].x || advance[0].y)
1428             {
1429                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1430                 piAdvance[glyph_index] += (int)(devX+0.5);
1431             }
1432             if (adjust[1].x || adjust[1].y)
1433             {
1434                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1435                 pGoffset[glyph_index + write_dir].du += (int)(devX+0.5);
1436                 pGoffset[glyph_index + write_dir].dv += (int)(devY+0.5);
1437             }
1438             if (advance[1].x || advance[1].y)
1439             {
1440                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1441                 piAdvance[glyph_index + write_dir] += (int)(devX+0.5);
1442             }
1443             return index;
1444         }
1445         case 4:
1446         {
1447             double devX, devY;
1448             POINT desU = {0,0};
1449             GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1450             if (desU.x || desU.y)
1451             {
1452                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1453                 pGoffset[glyph_index].du += ((int)(devX+0.5) - piAdvance[glyph_index-1]);
1454                 pGoffset[glyph_index].dv += (int)(devY+0.5);
1455             }
1456             break;
1457         }
1458         case 8:
1459         {
1460             return GPOS_apply_ChainContextPos(lpotm, lplogfont, piAdvance, lookup, look, glyphs, glyph_index, write_dir, glyph_count, ppem, pGoffset);
1461         }
1462         default:
1463             FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1464     }
1465     return glyph_index+1;
1466 }
1467
1468 INT OpenType_apply_GPOS_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, LPCVOID table, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset)
1469 {
1470     const GPOS_Header *header = (const GPOS_Header *)table;
1471     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1472
1473     return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1474 }
1475
1476 static void GSUB_initialize_script_cache(ScriptCache *psc)
1477 {
1478     int i;
1479
1480     if (psc->GSUB_Table)
1481     {
1482         const OT_ScriptList *script;
1483         const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1484         script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1485         psc->script_count = GET_BE_WORD(script->ScriptCount);
1486         TRACE("initializing %i scripts in this font\n",psc->script_count);
1487         if (psc->script_count)
1488         {
1489             psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1490             for (i = 0; i < psc->script_count; i++)
1491             {
1492                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1493                 psc->scripts[i].tag = MS_MAKE_TAG(script->ScriptRecord[i].ScriptTag[0], script->ScriptRecord[i].ScriptTag[1], script->ScriptRecord[i].ScriptTag[2], script->ScriptRecord[i].ScriptTag[3]);
1494                 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1495             }
1496         }
1497     }
1498 }
1499
1500 static void GPOS_expand_script_cache(ScriptCache *psc)
1501 {
1502     int i, count;
1503     const OT_ScriptList *script;
1504     const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1505
1506     if (!header)
1507         return;
1508
1509     script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1510     count = GET_BE_WORD(script->ScriptCount);
1511
1512     if (!psc->script_count)
1513     {
1514         psc->script_count = count;
1515         TRACE("initializing %i scripts in this font\n",psc->script_count);
1516         if (psc->script_count)
1517         {
1518             psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1519             for (i = 0; i < psc->script_count; i++)
1520             {
1521                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1522                 psc->scripts[i].tag = MS_MAKE_TAG(script->ScriptRecord[i].ScriptTag[0], script->ScriptRecord[i].ScriptTag[1], script->ScriptRecord[i].ScriptTag[2], script->ScriptRecord[i].ScriptTag[3]);
1523                 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1524             }
1525         }
1526     }
1527     else
1528     {
1529         for (i = 0; i < count; i++)
1530         {
1531             int j;
1532             int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1533             OPENTYPE_TAG tag = MS_MAKE_TAG(script->ScriptRecord[i].ScriptTag[0], script->ScriptRecord[i].ScriptTag[1], script->ScriptRecord[i].ScriptTag[2], script->ScriptRecord[i].ScriptTag[3]);
1534             for (j = 0; j < psc->script_count; j++)
1535             {
1536                 if (psc->scripts[j].tag == tag)
1537                 {
1538                     psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1539                     break;
1540                 }
1541             }
1542             if (j == psc->script_count)
1543             {
1544                 psc->script_count++;
1545                 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1546                 psc->scripts[j].tag = tag;
1547                 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1548             }
1549         }
1550     }
1551 }
1552
1553 static void _initialize_script_cache(ScriptCache *psc)
1554 {
1555     if (!psc->script_count)
1556     {
1557         GSUB_initialize_script_cache(psc);
1558         GPOS_expand_script_cache(psc);
1559     }
1560 }
1561
1562 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1563 {
1564     int i;
1565     HRESULT rc = S_OK;
1566
1567     _initialize_script_cache(psc);
1568
1569     *pcTags = psc->script_count;
1570
1571     if (!searchingFor && cMaxTags < *pcTags)
1572         rc = E_OUTOFMEMORY;
1573     else if (searchingFor)
1574         rc = USP_E_SCRIPT_NOT_IN_FONT;
1575
1576     for (i = 0; i < psc->script_count; i++)
1577     {
1578         if (i < cMaxTags)
1579             pScriptTags[i] = psc->scripts[i].tag;
1580
1581         if (searchingFor)
1582         {
1583             if (searchingFor == psc->scripts[i].tag)
1584             {
1585                 pScriptTags[0] = psc->scripts[i].tag;
1586                 *pcTags = 1;
1587                 rc = S_OK;
1588                 break;
1589             }
1590         }
1591     }
1592     return rc;
1593 }
1594
1595 static void GSUB_initialize_language_cache(LoadedScript *script)
1596 {
1597     int i;
1598
1599     if (script->gsub_table)
1600     {
1601         DWORD offset;
1602         const OT_Script* table = script->gsub_table;
1603         script->language_count = GET_BE_WORD(table->LangSysCount);
1604         offset = GET_BE_WORD(table->DefaultLangSys);
1605         if (offset)
1606         {
1607             script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1608             script->default_language.gsub_table = (const BYTE*)table + offset;
1609         }
1610
1611         if (script->language_count)
1612         {
1613             TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1614
1615             script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1616
1617             for (i = 0; i < script->language_count; i++)
1618             {
1619                 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1620                 script->languages[i].tag = MS_MAKE_TAG(table->LangSysRecord[i].LangSysTag[0], table->LangSysRecord[i].LangSysTag[1], table->LangSysRecord[i].LangSysTag[2], table->LangSysRecord[i].LangSysTag[3]);
1621                 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1622             }
1623         }
1624     }
1625 }
1626
1627 static void GPOS_expand_language_cache(LoadedScript *script)
1628 {
1629     int count;
1630     const OT_Script* table = script->gpos_table;
1631     DWORD offset;
1632
1633     if (!table)
1634         return;
1635
1636     offset = GET_BE_WORD(table->DefaultLangSys);
1637     if (offset)
1638         script->default_language.gpos_table = (const BYTE*)table + offset;
1639
1640     count = GET_BE_WORD(table->LangSysCount);
1641
1642     TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1643     if (!script->language_count)
1644     {
1645         int i;
1646         script->language_count = count;
1647
1648         script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1649
1650         for (i = 0; i < script->language_count; i++)
1651         {
1652             int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1653             script->languages[i].tag = MS_MAKE_TAG(table->LangSysRecord[i].LangSysTag[0], table->LangSysRecord[i].LangSysTag[1], table->LangSysRecord[i].LangSysTag[2], table->LangSysRecord[i].LangSysTag[3]);
1654             script->languages[i].gpos_table = ((const BYTE*)table + offset);
1655         }
1656     }
1657     else if (count)
1658     {
1659         int i,j;
1660         for (i = 0; i < count; i++)
1661         {
1662             int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1663             OPENTYPE_TAG tag = MS_MAKE_TAG(table->LangSysRecord[i].LangSysTag[0], table->LangSysRecord[i].LangSysTag[1], table->LangSysRecord[i].LangSysTag[2], table->LangSysRecord[i].LangSysTag[3]);
1664
1665             for (j = 0; j < script->language_count; j++)
1666             {
1667                 if (script->languages[j].tag == tag)
1668                 {
1669                     script->languages[j].gpos_table = ((const BYTE*)table + offset);
1670                     break;
1671                 }
1672             }
1673             if (j == script->language_count)
1674             {
1675                 script->language_count++;
1676                 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1677                 script->languages[j].tag = tag;
1678                 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1679             }
1680         }
1681     }
1682 }
1683
1684 static void _initialize_language_cache(LoadedScript *script)
1685 {
1686     if (!script->language_count)
1687     {
1688         GSUB_initialize_language_cache(script);
1689         GPOS_expand_language_cache(script);
1690     }
1691 }
1692
1693 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1694 {
1695     int i;
1696     HRESULT rc = S_OK;
1697     LoadedScript *script = NULL;
1698
1699     _initialize_script_cache(psc);
1700
1701     for (i = 0; i < psc->script_count; i++)
1702     {
1703          if (psc->scripts[i].tag == script_tag)
1704          {
1705             script = &psc->scripts[i];
1706             break;
1707          }
1708     }
1709
1710     if (!script)
1711         return E_INVALIDARG;
1712
1713     _initialize_language_cache(script);
1714
1715     if (!searchingFor && cMaxTags < script->language_count)
1716         rc = E_OUTOFMEMORY;
1717     else if (searchingFor)
1718         rc = E_INVALIDARG;
1719
1720     *pcTags = script->language_count;
1721
1722     for (i = 0; i < script->language_count; i++)
1723     {
1724         if (i < cMaxTags)
1725             pLanguageTags[i] = script->languages[i].tag;
1726
1727         if (searchingFor)
1728         {
1729             if (searchingFor == script->languages[i].tag)
1730             {
1731                 pLanguageTags[0] = script->languages[i].tag;
1732                 *pcTags = 1;
1733                 rc = S_OK;
1734                 break;
1735             }
1736         }
1737     }
1738
1739     if (script->default_language.gsub_table)
1740     {
1741         if (i < cMaxTags)
1742             pLanguageTags[i] = script->default_language.tag;
1743
1744         if (searchingFor  && FAILED(rc))
1745         {
1746             pLanguageTags[0] = script->default_language.tag;
1747         }
1748         i++;
1749         *pcTags = (*pcTags) + 1;
1750     }
1751
1752     return rc;
1753 }
1754
1755
1756 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1757 {
1758     int i;
1759
1760     if (language->gsub_table)
1761     {
1762         const OT_LangSys *lang = language->gsub_table;
1763         const GSUB_Header *header = (const GSUB_Header *)table;
1764         const OT_FeatureList *feature_list;
1765
1766         language->feature_count = GET_BE_WORD(lang->FeatureCount);
1767         TRACE("%i features\n",language->feature_count);
1768
1769         if (language->feature_count)
1770         {
1771             language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1772
1773             feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1774
1775             for (i = 0; i < language->feature_count; i++)
1776             {
1777                 const OT_Feature *feature;
1778                 int j;
1779                 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1780
1781                 language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
1782                 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1783                 feature = (const OT_Feature*)language->features[i].feature;
1784                 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1785                 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1786                 for (j = 0; j < language->features[i].lookup_count; j++)
1787                     language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1788             }
1789         }
1790     }
1791 }
1792
1793 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1794 {
1795     int i, count;
1796     const OT_LangSys *lang = language->gpos_table;
1797     const GPOS_Header *header = (const GPOS_Header *)table;
1798     const OT_FeatureList *feature_list;
1799
1800     if (!lang)
1801         return;
1802
1803     count = GET_BE_WORD(lang->FeatureCount);
1804     feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1805
1806     TRACE("%i features\n",count);
1807     if (!language->feature_count)
1808     {
1809         language->feature_count = count;
1810
1811         if (language->feature_count)
1812         {
1813             language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1814
1815             for (i = 0; i < language->feature_count; i++)
1816             {
1817                 const OT_Feature *feature;
1818                 int j;
1819                 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1820
1821                 language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
1822                 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1823                 feature = (const OT_Feature*)language->features[i].feature;
1824                 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1825                 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1826                 for (j = 0; j < language->features[i].lookup_count; j++)
1827                     language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1828             }
1829         }
1830     }
1831     else if (count)
1832     {
1833         language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1834
1835         for (i = 0; i < count; i++)
1836         {
1837             const OT_Feature *feature;
1838             int j;
1839             int index = GET_BE_WORD(lang->FeatureIndex[i]);
1840             int idx = language->feature_count + i;
1841
1842             language->features[idx].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
1843             language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1844             feature = (const OT_Feature*)language->features[idx].feature;
1845             language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1846             language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1847             for (j = 0; j < language->features[idx].lookup_count; j++)
1848                 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1849         }
1850         language->feature_count += count;
1851     }
1852 }
1853
1854 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1855 {
1856     if (!language->feature_count)
1857     {
1858         GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1859         GPOS_expand_feature_cache(psc->GPOS_Table, language);
1860     }
1861 }
1862
1863 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
1864 {
1865     int i;
1866     HRESULT rc = S_OK;
1867     LoadedScript *script = NULL;
1868     LoadedLanguage *language = NULL;
1869
1870     _initialize_script_cache(psc);
1871
1872     for (i = 0; i < psc->script_count; i++)
1873     {
1874         if (psc->scripts[i].tag == script_tag)
1875         {
1876             script = &psc->scripts[i];
1877             break;
1878         }
1879     }
1880
1881     if (!script)
1882     {
1883         *pcTags = 0;
1884         if (!filtered)
1885             return S_OK;
1886         else
1887             return E_INVALIDARG;
1888     }
1889
1890     _initialize_language_cache(script);
1891
1892     if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1893         language = &script->default_language;
1894     else
1895     {
1896         for (i = 0; i < script->language_count; i++)
1897         {
1898             if (script->languages[i].tag == language_tag)
1899             {
1900                 language = &script->languages[i];
1901                 break;
1902             }
1903         }
1904     }
1905
1906     if (!language)
1907     {
1908         *pcTags = 0;
1909         return S_OK;
1910     }
1911
1912     _initialize_feature_cache(psc, language);
1913
1914     *pcTags = language->feature_count;
1915
1916     if (!searchingFor && cMaxTags < *pcTags)
1917         rc = E_OUTOFMEMORY;
1918     else if (searchingFor)
1919         rc = E_INVALIDARG;
1920
1921     for (i = 0; i < language->feature_count; i++)
1922     {
1923         if (i < cMaxTags)
1924             pFeatureTags[i] = language->features[i].tag;
1925
1926         if (searchingFor)
1927         {
1928             if (searchingFor == language->features[i].tag)
1929             {
1930                 pFeatureTags[0] = language->features[i].tag;
1931                 *pcTags = 1;
1932                 if (feature)
1933                     *feature = &language->features[i];
1934                 rc = S_OK;
1935                 break;
1936             }
1937         }
1938     }
1939     return rc;
1940 }