imm32: Retrieve the graphics driver module from gdi32.
[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 #define round(x) (((x) < 0) ? (int)((x) - 0.5) : (int)((x) + 0.5))
47
48 /* These are all structures needed for the cmap format 12 table */
49 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
50
51 typedef struct {
52     WORD platformID;
53     WORD encodingID;
54     DWORD offset;
55 } CMAP_EncodingRecord;
56
57 typedef struct {
58     WORD version;
59     WORD numTables;
60     CMAP_EncodingRecord tables[1];
61 } CMAP_Header;
62
63 typedef struct {
64     DWORD startCharCode;
65     DWORD endCharCode;
66     DWORD startGlyphID;
67 } CMAP_SegmentedCoverage_group;
68
69 typedef struct {
70     WORD format;
71     WORD reserved;
72     DWORD length;
73     DWORD language;
74     DWORD nGroups;
75     CMAP_SegmentedCoverage_group groups[1];
76 } CMAP_SegmentedCoverage;
77
78 /* These are all structures needed for the GDEF table */
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 } OT_ClassDefFormat1;
95
96 typedef struct {
97     WORD Start;
98     WORD End;
99     WORD Class;
100 } OT_ClassRangeRecord;
101
102 typedef struct {
103     WORD ClassFormat;
104     WORD ClassRangeCount;
105     OT_ClassRangeRecord ClassRangeRecord[1];
106 } OT_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 PosFormat;
354     WORD Coverage;
355     WORD ValueFormat1;
356     WORD ValueFormat2;
357     WORD ClassDef1;
358     WORD ClassDef2;
359     WORD Class1Count;
360     WORD Class2Count;
361     WORD Class1Record[1];
362 } GPOS_PairPosFormat2;
363
364 typedef struct {
365     WORD SecondGlyph;
366     WORD Value1[1];
367     WORD Value2[1];
368 } GPOS_PairValueRecord;
369
370 typedef struct {
371     WORD PairValueCount;
372     GPOS_PairValueRecord PairValueRecord[1];
373 } GPOS_PairSet;
374
375 typedef struct {
376     WORD EntryAnchor;
377     WORD ExitAnchor;
378 } GPOS_EntryExitRecord;
379
380 typedef struct {
381     WORD PosFormat;
382     WORD Coverage;
383     WORD EntryExitCount;
384     GPOS_EntryExitRecord EntryExitRecord[1];
385 } GPOS_CursivePosFormat1;
386
387 typedef struct {
388     WORD PosFormat;
389     WORD MarkCoverage;
390     WORD BaseCoverage;
391     WORD ClassCount;
392     WORD MarkArray;
393     WORD BaseArray;
394 } GPOS_MarkBasePosFormat1;
395
396 typedef struct {
397     WORD BaseAnchor[1];
398 } GPOS_BaseRecord;
399
400 typedef struct {
401     WORD BaseCount;
402     GPOS_BaseRecord BaseRecord[1];
403 } GPOS_BaseArray;
404
405 typedef struct {
406     WORD Class;
407     WORD MarkAnchor;
408 } GPOS_MarkRecord;
409
410 typedef struct {
411     WORD MarkCount;
412     GPOS_MarkRecord MarkRecord[1];
413 } GPOS_MarkArray;
414
415 typedef struct {
416     WORD PosFormat;
417     WORD MarkCoverage;
418     WORD LigatureCoverage;
419     WORD ClassCount;
420     WORD MarkArray;
421     WORD LigatureArray;
422 } GPOS_MarkLigPosFormat1;
423
424 typedef struct {
425     WORD LigatureCount;
426     WORD LigatureAttach[1];
427 } GPOS_LigatureArray;
428
429 typedef struct {
430     WORD LigatureAnchor[1];
431 } GPOS_ComponentRecord;
432
433 typedef struct {
434     WORD ComponentCount;
435     GPOS_ComponentRecord ComponentRecord[1];
436 } GPOS_LigatureAttach;
437
438 typedef struct {
439     WORD PosFormat;
440     WORD Mark1Coverage;
441     WORD Mark2Coverage;
442     WORD ClassCount;
443     WORD Mark1Array;
444     WORD Mark2Array;
445 } GPOS_MarkMarkPosFormat1;
446
447 typedef struct {
448     WORD Mark2Anchor[1];
449 } GPOS_Mark2Record;
450
451 typedef struct {
452     WORD Mark2Count;
453     GPOS_Mark2Record Mark2Record[1];
454 } GPOS_Mark2Array;
455
456 typedef struct {
457     WORD SequenceIndex;
458     WORD LookupListIndex;
459 } GPOS_PosLookupRecord;
460
461 typedef struct {
462     WORD PosFormat;
463     WORD BacktrackGlyphCount;
464     WORD Coverage[1];
465 } GPOS_ChainContextPosFormat3_1;
466
467 typedef struct {
468     WORD InputGlyphCount;
469     WORD Coverage[1];
470 } GPOS_ChainContextPosFormat3_2;
471
472 typedef struct {
473     WORD LookaheadGlyphCount;
474     WORD Coverage[1];
475 } GPOS_ChainContextPosFormat3_3;
476
477 typedef struct {
478     WORD PosCount;
479     GPOS_PosLookupRecord PosLookupRecord[1];
480 } GPOS_ChainContextPosFormat3_4;
481
482 /**********
483  * CMAP
484  **********/
485
486 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
487 {
488     CMAP_Header *CMAP_Table = NULL;
489     int length;
490     int i;
491
492     if (!psc->CMAP_Table)
493     {
494         length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
495         if (length != GDI_ERROR)
496         {
497             psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
498             GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
499             TRACE("Loaded cmap table of %i bytes\n",length);
500         }
501         else
502             return NULL;
503     }
504
505     CMAP_Table = psc->CMAP_Table;
506
507     for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
508     {
509         if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
510              (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
511         {
512             CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
513             if (GET_BE_WORD(format->format) == 12)
514                 return format;
515         }
516     }
517     return NULL;
518 }
519
520 static int compare_group(const void *a, const void* b)
521 {
522     const DWORD *chr = a;
523     const CMAP_SegmentedCoverage_group *group = b;
524
525     if (*chr < GET_BE_DWORD(group->startCharCode))
526         return -1;
527     if (*chr > GET_BE_DWORD(group->endCharCode))
528         return 1;
529     return 0;
530 }
531
532 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
533 {
534     /* BMP: use gdi32 for ease */
535     if (utf32c < 0x10000)
536     {
537         WCHAR ch = utf32c;
538         return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
539     }
540
541     if (!psc->CMAP_format12_Table)
542         psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
543
544     if (flags & GGI_MARK_NONEXISTING_GLYPHS)
545         *pgi = 0xffff;
546     else
547         *pgi = 0;
548
549     if (psc->CMAP_format12_Table)
550     {
551         CMAP_SegmentedCoverage *format = NULL;
552         CMAP_SegmentedCoverage_group *group = NULL;
553
554         format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
555
556         group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
557                         sizeof(CMAP_SegmentedCoverage_group), compare_group);
558
559         if (group)
560         {
561             DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
562             *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
563             return 0;
564         }
565     }
566     return 0;
567 }
568
569 /**********
570  * GDEF
571  **********/
572
573 static WORD OT_get_glyph_class(const void *table, WORD glyph)
574 {
575     WORD class = 0;
576     const OT_ClassDefFormat1 *cf1 = table;
577
578     if (!table) return 0;
579
580     if (GET_BE_WORD(cf1->ClassFormat) == 1)
581     {
582         if (glyph >= GET_BE_WORD(cf1->StartGlyph))
583         {
584             int index = glyph - GET_BE_WORD(cf1->StartGlyph);
585             if (index < GET_BE_WORD(cf1->GlyphCount))
586                 class = GET_BE_WORD(cf1->ClassValueArray[index]);
587         }
588     }
589     else if (GET_BE_WORD(cf1->ClassFormat) == 2)
590     {
591         const OT_ClassDefFormat2 *cf2 = table;
592         int i, top;
593         top = GET_BE_WORD(cf2->ClassRangeCount);
594         for (i = 0; i < top; i++)
595         {
596             if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
597                 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
598             {
599                 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
600                 break;
601             }
602         }
603     }
604     else
605         ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
606
607     return class;
608 }
609
610 void OpenType_GDEF_UpdateGlyphProps(ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
611 {
612     int i;
613     void *glyph_class_table = NULL;
614
615     if (psc->GDEF_Table)
616     {
617         const GDEF_Header *header = psc->GDEF_Table;
618         WORD offset = GET_BE_WORD( header->GlyphClassDef );
619         if (offset)
620             glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
621     }
622
623     for (i = 0; i < cGlyphs; i++)
624     {
625         WORD class;
626         int char_count = 0;
627         int k;
628
629         k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
630         if (k >= 0)
631         {
632             for (; k < cChars && pwLogClust[k] == i; k++)
633                 char_count++;
634         }
635
636         class = OT_get_glyph_class( glyph_class_table, pwGlyphs[i] );
637
638         switch (class)
639         {
640             case 0:
641             case BaseGlyph:
642                 pGlyphProp[i].sva.fClusterStart = 1;
643                 pGlyphProp[i].sva.fDiacritic = 0;
644                 pGlyphProp[i].sva.fZeroWidth = 0;
645                 break;
646             case LigatureGlyph:
647                 pGlyphProp[i].sva.fClusterStart = 1;
648                 pGlyphProp[i].sva.fDiacritic = 0;
649                 pGlyphProp[i].sva.fZeroWidth = 0;
650                 break;
651             case MarkGlyph:
652                 pGlyphProp[i].sva.fClusterStart = 0;
653                 pGlyphProp[i].sva.fDiacritic = 1;
654                 pGlyphProp[i].sva.fZeroWidth = 1;
655                 break;
656             case ComponentGlyph:
657                 pGlyphProp[i].sva.fClusterStart = 0;
658                 pGlyphProp[i].sva.fDiacritic = 0;
659                 pGlyphProp[i].sva.fZeroWidth = 0;
660                 break;
661             default:
662                 ERR("Unknown glyph class %i\n",class);
663                 pGlyphProp[i].sva.fClusterStart = 1;
664                 pGlyphProp[i].sva.fDiacritic = 0;
665                 pGlyphProp[i].sva.fZeroWidth = 0;
666         }
667
668         if (char_count == 0)
669             pGlyphProp[i].sva.fClusterStart = 0;
670     }
671 }
672
673 /**********
674  * GSUB
675  **********/
676 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
677
678 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
679 {
680     const OT_CoverageFormat1* cf1;
681
682     cf1 = table;
683
684     if (GET_BE_WORD(cf1->CoverageFormat) == 1)
685     {
686         int count = GET_BE_WORD(cf1->GlyphCount);
687         int i;
688         TRACE("Coverage Format 1, %i glyphs\n",count);
689         for (i = 0; i < count; i++)
690             if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
691                 return i;
692         return -1;
693     }
694     else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
695     {
696         const OT_CoverageFormat2* cf2;
697         int i;
698         int count;
699         cf2 = (const OT_CoverageFormat2*)cf1;
700
701         count = GET_BE_WORD(cf2->RangeCount);
702         TRACE("Coverage Format 2, %i ranges\n",count);
703         for (i = 0; i < count; i++)
704         {
705             if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
706                 return -1;
707             if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
708                 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
709             {
710                 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
711                     glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
712             }
713         }
714         return -1;
715     }
716     else
717         ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
718
719     return -1;
720 }
721
722 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
723 {
724     int j;
725     TRACE("Single Substitution Subtable\n");
726
727     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
728     {
729         int offset;
730         const GSUB_SingleSubstFormat1 *ssf1;
731         offset = GET_BE_WORD(look->SubTable[j]);
732         ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
733         if (GET_BE_WORD(ssf1->SubstFormat) == 1)
734         {
735             int offset = GET_BE_WORD(ssf1->Coverage);
736             TRACE("  subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
737             if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
738             {
739                 TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
740                 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
741                 TRACE(" 0x%x\n",glyphs[glyph_index]);
742                 return glyph_index + write_dir;
743             }
744         }
745         else
746         {
747             const GSUB_SingleSubstFormat2 *ssf2;
748             INT index;
749             INT offset;
750
751             ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
752             offset = GET_BE_WORD(ssf1->Coverage);
753             TRACE("  subtype 2,  glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
754             index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
755             TRACE("  Coverage index %i\n",index);
756             if (index != -1)
757             {
758                 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
759                     return GSUB_E_NOGLYPH;
760
761                 TRACE("    Glyph is 0x%x ->",glyphs[glyph_index]);
762                 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
763                 TRACE("0x%x\n",glyphs[glyph_index]);
764                 return glyph_index + write_dir;
765             }
766         }
767     }
768     return GSUB_E_NOGLYPH;
769 }
770
771 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
772 {
773     int j;
774     TRACE("Multiple Substitution Subtable\n");
775
776     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
777     {
778         int offset, index;
779         const GSUB_MultipleSubstFormat1 *msf1;
780         offset = GET_BE_WORD(look->SubTable[j]);
781         msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
782
783         offset = GET_BE_WORD(msf1->Coverage);
784         index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
785         if (index != -1)
786         {
787             const GSUB_Sequence *seq;
788             int sub_count;
789             int j;
790             offset = GET_BE_WORD(msf1->Sequence[index]);
791             seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
792             sub_count = GET_BE_WORD(seq->GlyphCount);
793             TRACE("  Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
794
795             for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
796                     glyphs[j] =glyphs[j-(sub_count-1)];
797
798             for (j = 0; j < sub_count; j++)
799                     if (write_dir < 0)
800                         glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
801                     else
802                         glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
803
804             *glyph_count = *glyph_count + (sub_count - 1);
805
806             if (TRACE_ON(uniscribe))
807             {
808                 for (j = 0; j < sub_count; j++)
809                     TRACE(" 0x%x",glyphs[glyph_index+j]);
810                 TRACE("\n");
811             }
812
813             return glyph_index + (sub_count * write_dir);
814         }
815     }
816     return GSUB_E_NOGLYPH;
817 }
818
819 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
820 {
821     int j;
822     TRACE("Alternate Substitution Subtable\n");
823
824     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
825     {
826         int offset;
827         const GSUB_AlternateSubstFormat1 *asf1;
828         INT index;
829
830         offset = GET_BE_WORD(look->SubTable[j]);
831         asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
832         offset = GET_BE_WORD(asf1->Coverage);
833
834         index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
835         if (index != -1)
836         {
837             const GSUB_AlternateSet *as;
838             offset =  GET_BE_WORD(asf1->AlternateSet[index]);
839             as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
840             FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
841             if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
842                 return GSUB_E_NOGLYPH;
843
844             TRACE("  Glyph 0x%x ->",glyphs[glyph_index]);
845             glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
846             TRACE(" 0x%x\n",glyphs[glyph_index]);
847             return glyph_index + write_dir;
848         }
849     }
850     return GSUB_E_NOGLYPH;
851 }
852
853 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
854 {
855     int j;
856
857     TRACE("Ligature Substitution Subtable\n");
858     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
859     {
860         const GSUB_LigatureSubstFormat1 *lsf1;
861         int offset,index;
862
863         offset = GET_BE_WORD(look->SubTable[j]);
864         lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
865         offset = GET_BE_WORD(lsf1->Coverage);
866         index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
867         TRACE("  Coverage index %i\n",index);
868         if (index != -1)
869         {
870             const GSUB_LigatureSet *ls;
871             int k, count;
872
873             offset = GET_BE_WORD(lsf1->LigatureSet[index]);
874             ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
875             count = GET_BE_WORD(ls->LigatureCount);
876             TRACE("  LigatureSet has %i members\n",count);
877             for (k = 0; k < count; k++)
878             {
879                 const GSUB_Ligature *lig;
880                 int CompCount,l,CompIndex;
881
882                 offset = GET_BE_WORD(ls->Ligature[k]);
883                 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
884                 CompCount = GET_BE_WORD(lig->CompCount) - 1;
885                 CompIndex = glyph_index+write_dir;
886                 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
887                 {
888                     int CompGlyph;
889                     CompGlyph = GET_BE_WORD(lig->Component[l]);
890                     if (CompGlyph != glyphs[CompIndex])
891                         break;
892                     CompIndex += write_dir;
893                 }
894                 if (l == CompCount)
895                 {
896                     int replaceIdx = glyph_index;
897                     if (write_dir < 0)
898                         replaceIdx = glyph_index - CompCount;
899
900                     TRACE("    Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
901                     glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
902                     TRACE("0x%x\n",glyphs[replaceIdx]);
903                     if (CompCount > 0)
904                     {
905                         int j;
906                         for (j = replaceIdx + 1; j < *glyph_count; j++)
907                             glyphs[j] =glyphs[j+CompCount];
908                         *glyph_count = *glyph_count - CompCount;
909                     }
910                     return replaceIdx + write_dir;
911                 }
912             }
913         }
914     }
915     return GSUB_E_NOGLYPH;
916 }
917
918 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
919 {
920     int j;
921     BOOL done = FALSE;
922
923     TRACE("Chaining Contextual Substitution Subtable\n");
924     for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
925     {
926         const GSUB_ChainContextSubstFormat1 *ccsf1;
927         int offset;
928         int dirLookahead = write_dir;
929         int dirBacktrack = -1 * write_dir;
930
931         offset = GET_BE_WORD(look->SubTable[j]);
932         ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
933         if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
934         {
935             FIXME("  TODO: subtype 1 (Simple context glyph substitution)\n");
936             continue;
937         }
938         else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
939         {
940             FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
941             continue;
942         }
943         else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
944         {
945             int k;
946             int indexGlyphs;
947             const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
948             const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
949             const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
950             const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
951             int newIndex = glyph_index;
952
953             ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
954
955             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
956
957             for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
958             {
959                 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
960                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
961                     break;
962             }
963             if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
964                 continue;
965             TRACE("Matched Backtrack\n");
966
967             ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
968                     FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
969
970             indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
971             for (k = 0; k < indexGlyphs; k++)
972             {
973                 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
974                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
975                     break;
976             }
977             if (k != indexGlyphs)
978                 continue;
979             TRACE("Matched IndexGlyphs\n");
980
981             ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
982                     FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
983
984             for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
985             {
986                 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
987                 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
988                     break;
989             }
990             if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
991                 continue;
992             TRACE("Matched LookAhead\n");
993
994             ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
995                     FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
996
997             if (GET_BE_WORD(ccsf3_4->SubstCount))
998             {
999                 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
1000                 {
1001                     int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
1002                     int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
1003
1004                     TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1005                     newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
1006                     if (newIndex == -1)
1007                     {
1008                         ERR("Chain failed to generate a glyph\n");
1009                         continue;
1010                     }
1011                 }
1012                 return newIndex;
1013             }
1014             else return GSUB_E_NOGLYPH;
1015         }
1016     }
1017     return -1;
1018 }
1019
1020 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1021 {
1022     int offset;
1023     const OT_LookupTable *look;
1024
1025     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1026     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1027     TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1028     switch(GET_BE_WORD(look->LookupType))
1029     {
1030         case 1:
1031             return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1032         case 2:
1033             return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1034         case 3:
1035             return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1036         case 4:
1037             return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1038         case 6:
1039             return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1040         default:
1041             FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1042     }
1043     return GSUB_E_NOGLYPH;
1044 }
1045
1046 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1047 {
1048     const GSUB_Header *header = (const GSUB_Header *)table;
1049     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1050
1051     return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1052 }
1053
1054 /**********
1055  * GPOS
1056  **********/
1057 static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1058                              const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset);
1059
1060 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1061 {
1062     static const WORD mask[3] = {3,0xf,0xff};
1063     if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem  <= GET_BE_WORD(DeviceTable->EndSize))
1064     {
1065         int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1066         int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1067         int value;
1068         TRACE("device table, format %i, index %i\n",format, index);
1069         index = index << format;
1070         value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1071         TRACE("offset %i, value %i\n",index, value);
1072         if (value > mask[format-1]/2)
1073             value = -1 * ((mask[format-1]+1) - value);
1074         return value;
1075     }
1076     return 0;
1077 }
1078
1079 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1080 {
1081     const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1082
1083     switch (GET_BE_WORD(anchor1->AnchorFormat))
1084     {
1085         case 1:
1086         {
1087             TRACE("Anchor Format 1\n");
1088             pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1089             pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1090             break;
1091         }
1092         case 2:
1093         {
1094             const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1095             TRACE("Anchor Format 2\n");
1096             pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1097             pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1098             break;
1099         }
1100         case 3:
1101         {
1102             int offset;
1103             const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1104             TRACE("Anchor Format 3\n");
1105             pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1106             pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1107             offset = GET_BE_WORD(anchor3->XDeviceTable);
1108             TRACE("ppem %i\n",ppem);
1109             if (offset)
1110             {
1111                 const OT_DeviceTable* DeviceTableX = NULL;
1112                 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1113                 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1114             }
1115             offset = GET_BE_WORD(anchor3->YDeviceTable);
1116             if (offset)
1117             {
1118                 const OT_DeviceTable* DeviceTableY = NULL;
1119                 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1120                 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1121             }
1122             break;
1123         }
1124         default:
1125             ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1126             pt->x = 0;
1127             pt->y = 0;
1128     }
1129 }
1130
1131 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1132 {
1133     int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1134
1135     TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1136     *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1137     *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1138     if (lplogfont->lfWidth)
1139         FIXME("Font with lfWidth set no handled properly\n");
1140 }
1141
1142 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1143 {
1144     INT offset = 0;
1145     if (ValueFormat & 0x0001) { if (data) record->XPlacement = GET_BE_WORD(data[offset]); offset++; }
1146     if (ValueFormat & 0x0002) { if (data) record->YPlacement = GET_BE_WORD(data[offset]); offset++; }
1147     if (ValueFormat & 0x0004) { if (data) record->XAdvance   = GET_BE_WORD(data[offset]); offset++; }
1148     if (ValueFormat & 0x0008) { if (data) record->YAdvance   = GET_BE_WORD(data[offset]); offset++; }
1149     if (ValueFormat & 0x0010) { if (data) record->XPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1150     if (ValueFormat & 0x0020) { if (data) record->YPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1151     if (ValueFormat & 0x0040) { if (data) record->XAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1152     if (ValueFormat & 0x0080) { if (data) record->YAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1153     return offset;
1154 }
1155
1156 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord,  WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1157 {
1158     if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1159     if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1160     if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1161     if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1162     if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1163     if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1164     if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1165     if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1166     if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1167 }
1168
1169 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1170                                         INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1171 {
1172     int j;
1173
1174     TRACE("Single Adjustment Positioning Subtable\n");
1175
1176     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1177     {
1178         const GPOS_SinglePosFormat1 *spf1;
1179         WORD offset = GET_BE_WORD(look->SubTable[j]);
1180         spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1181         if (GET_BE_WORD(spf1->PosFormat) == 1)
1182         {
1183             offset = GET_BE_WORD(spf1->Coverage);
1184             if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1185             {
1186                 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1187                 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1188                 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1189                 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
1190                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1191             }
1192         }
1193         else if (GET_BE_WORD(spf1->PosFormat) == 2)
1194         {
1195             int index;
1196             const GPOS_SinglePosFormat2 *spf2;
1197             spf2 = (const GPOS_SinglePosFormat2*)spf1;
1198             offset = GET_BE_WORD(spf2->Coverage);
1199             index  = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1200             if (index != -1)
1201             {
1202                 int size;
1203                 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1204                 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1205                 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1206                 if (index > 0)
1207                 {
1208                     offset = size * index;
1209                     GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1210                 }
1211                 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord,  ValueFormat, ppem, ptAdjust, ptAdvance);
1212                 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1213             }
1214         }
1215         else
1216             FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1217     }
1218 }
1219
1220 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1221                               INT ppem, POINT *adjust, POINT *advance )
1222 {
1223     GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1224     GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1225     INT size;
1226
1227     size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1228     GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1229
1230     if (val_fmt1)
1231     {
1232         GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1233         TRACE( "Glyph 1 resulting cumulative offset is %i,%i design units\n", adjust[0].x, adjust[0].y );
1234         TRACE( "Glyph 1 resulting cumulative advance is %i,%i design units\n", advance[0].x, advance[0].y );
1235     }
1236     if (val_fmt2)
1237     {
1238         GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1239         TRACE( "Glyph 2 resulting cumulative offset is %i,%i design units\n", adjust[1].x, adjust[1].y );
1240         TRACE( "Glyph 2 resulting cumulative advance is %i,%i design units\n", advance[1].x, advance[1].y );
1241     }
1242 }
1243
1244 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1245                                      INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1246 {
1247     int j;
1248     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1249
1250     if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return glyph_index + 1;
1251
1252     TRACE("Pair Adjustment Positioning Subtable\n");
1253
1254     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1255     {
1256         const GPOS_PairPosFormat1 *ppf1;
1257         WORD offset = GET_BE_WORD(look->SubTable[j]);
1258         ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1259         if (GET_BE_WORD(ppf1->PosFormat) == 1)
1260         {
1261             int index;
1262             WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1263             WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1264             INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1265             INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1266             offset = GET_BE_WORD(ppf1->Coverage);
1267             index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1268             if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1269             {
1270                 int k;
1271                 int pair_count;
1272                 const GPOS_PairSet *ps;
1273                 const GPOS_PairValueRecord *pair_val_rec;
1274                 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1275                 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1276                 pair_count = GET_BE_WORD(ps->PairValueCount);
1277                 pair_val_rec = ps->PairValueRecord;
1278                 for (k = 0; k < pair_count; k++)
1279                 {
1280                     WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1281                     if (glyphs[glyph_index+write_dir] == second_glyph)
1282                     {
1283                         int next = 1;
1284                         TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1285                         apply_pair_value( ppf1, ValueFormat1, ValueFormat2, pair_val_rec->Value1, ppem, ptAdjust, ptAdvance );
1286                         if (ValueFormat2) next++;
1287                         return glyph_index + next;
1288                     }
1289                     pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1290                 }
1291             }
1292         }
1293         else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1294         {
1295             const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)((const BYTE*)look + offset);
1296             int index;
1297             WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1298             WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1299             INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1300             INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1301             WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1302             WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1303
1304             offset = GET_BE_WORD( ppf2->Coverage );
1305             index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1306             if (index != -1)
1307             {
1308                 WORD class1, class2;
1309                 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1310                 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1311                 if (class1 < class1_count && class2 < class2_count)
1312                 {
1313                     const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1314                     int next = 1;
1315
1316                     TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1317
1318                     apply_pair_value( ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, ptAdjust, ptAdvance );
1319                     if (ValueFormat2) next++;
1320                     return glyph_index + next;
1321                 }
1322             }
1323         }
1324         else
1325             FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1326     }
1327     return glyph_index+1;
1328 }
1329
1330 static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1331                                      INT glyph_count, INT ppem, LPPOINT pt)
1332 {
1333     int j;
1334     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1335
1336     if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return;
1337
1338     TRACE("Cursive Attachment Positioning Subtable\n");
1339
1340     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1341     {
1342         const GPOS_CursivePosFormat1 *cpf1;
1343         WORD offset = GET_BE_WORD(look->SubTable[j]);
1344         cpf1 = (const GPOS_CursivePosFormat1*)((const BYTE*)look+offset);
1345         if (GET_BE_WORD(cpf1->PosFormat) == 1)
1346         {
1347             int index_exit, index_entry;
1348             offset = GET_BE_WORD( cpf1->Coverage );
1349             index_exit = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index]);
1350             if (index_exit != -1 && cpf1->EntryExitRecord[index_exit].ExitAnchor!= 0)
1351             {
1352                 index_entry = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index+write_dir]);
1353                 if (index_entry != -1 && cpf1->EntryExitRecord[index_entry].EntryAnchor != 0)
1354                 {
1355                     POINT exit_pt, entry_pt;
1356                     offset = GET_BE_WORD(cpf1->EntryExitRecord[index_exit].ExitAnchor);
1357                     GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
1358                     offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
1359                     GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
1360                     TRACE("Found linkage %x[%i,%i] %x[%i,%i]\n",glyphs[glyph_index], exit_pt.x,exit_pt.y, glyphs[glyph_index+write_dir], entry_pt.x, entry_pt.y);
1361                     pt->x = entry_pt.x - exit_pt.x;
1362                     pt->y = entry_pt.y - exit_pt.y;
1363                     return;
1364                 }
1365             }
1366         }
1367         else
1368             FIXME("Cursive Attachment Positioning: Format %i Unhandled\n",GET_BE_WORD(cpf1->PosFormat));
1369     }
1370     return;
1371 }
1372
1373 static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index, INT glyph_count, INT ppem, LPPOINT pt)
1374 {
1375     int j;
1376     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1377     void *glyph_class_table = NULL;
1378     int rc = -1;
1379
1380     if (psc->GDEF_Table)
1381     {
1382         const GDEF_Header *header = psc->GDEF_Table;
1383         WORD offset = GET_BE_WORD( header->GlyphClassDef );
1384         if (offset)
1385             glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
1386     }
1387
1388     TRACE("MarkToBase Attachment Positioning Subtable\n");
1389
1390     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1391     {
1392         int offset;
1393         const GPOS_MarkBasePosFormat1 *mbpf1;
1394         offset = GET_BE_WORD(look->SubTable[j]);
1395         mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1396         if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1397         {
1398             int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1399             int mark_index;
1400             mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1401             if (mark_index != -1)
1402             {
1403                 int base_index;
1404                 int base_glyph = glyph_index - write_dir;
1405
1406                 if (glyph_class_table)
1407                 {
1408                     while (OT_get_glyph_class(glyph_class_table, glyphs[base_glyph]) == MarkGlyph && base_glyph > 0 && base_glyph < glyph_count)
1409                         base_glyph -= write_dir;
1410                 }
1411
1412                 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1413                 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[base_glyph]);
1414                 if (base_index != -1)
1415                 {
1416                     const GPOS_MarkArray *ma;
1417                     const GPOS_MarkRecord *mr;
1418                     const GPOS_BaseArray *ba;
1419                     const GPOS_BaseRecord *br;
1420                     int mark_class;
1421                     int class_count = GET_BE_WORD(mbpf1->ClassCount);
1422                     int baserecord_size;
1423                     POINT base_pt;
1424                     POINT mark_pt;
1425                     TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[base_glyph], base_index);
1426                     offset = GET_BE_WORD(mbpf1->MarkArray);
1427                     ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1428                     if (mark_index > GET_BE_WORD(ma->MarkCount))
1429                     {
1430                         ERR("Mark index exeeded mark count\n");
1431                         return -1;
1432                     }
1433                     mr = &ma->MarkRecord[mark_index];
1434                     mark_class = GET_BE_WORD(mr->Class);
1435                     TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1436                     offset = GET_BE_WORD(mbpf1->BaseArray);
1437                     ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1438                     baserecord_size = class_count * sizeof(WORD);
1439                     br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1440                     offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1441                     GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1442                     offset = GET_BE_WORD(mr->MarkAnchor);
1443                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1444                     TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1445                     TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1446                     pt->x += base_pt.x - mark_pt.x;
1447                     pt->y += base_pt.y - mark_pt.y;
1448                     TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1449                     rc = base_glyph;
1450                 }
1451             }
1452         }
1453         else
1454             FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1455     }
1456     return rc;
1457 }
1458
1459 static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1460                                       INT glyph_count, INT ppem, LPPOINT pt)
1461 {
1462     int j;
1463     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1464
1465     TRACE("MarkToLigature Attachment Positioning Subtable\n");
1466
1467     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1468     {
1469         int offset;
1470         const GPOS_MarkLigPosFormat1 *mlpf1;
1471         offset = GET_BE_WORD(look->SubTable[j]);
1472         mlpf1 = (const GPOS_MarkLigPosFormat1*)((const BYTE*)look+offset);
1473         if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1474         {
1475             int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1476             int mark_index;
1477             mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
1478             if (mark_index != -1)
1479             {
1480                 int ligature_index;
1481                 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
1482                 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
1483                 if (ligature_index != -1)
1484                 {
1485                     const GPOS_MarkArray *ma;
1486                     const GPOS_MarkRecord *mr;
1487
1488                     const GPOS_LigatureArray *la;
1489                     const GPOS_LigatureAttach *lt;
1490                     int mark_class;
1491                     int class_count = GET_BE_WORD(mlpf1->ClassCount);
1492                     int component_count;
1493                     int component_size;
1494                     int i;
1495                     POINT ligature_pt;
1496                     POINT mark_pt;
1497
1498                     TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
1499                     offset = GET_BE_WORD(mlpf1->MarkArray);
1500                     ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
1501                     if (mark_index > GET_BE_WORD(ma->MarkCount))
1502                     {
1503                         ERR("Mark index exeeded mark count\n");
1504                         return;
1505                     }
1506                     mr = &ma->MarkRecord[mark_index];
1507                     mark_class = GET_BE_WORD(mr->Class);
1508                     TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1509                     offset = GET_BE_WORD(mlpf1->LigatureArray);
1510                     la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
1511                     if (ligature_index > GET_BE_WORD(la->LigatureCount))
1512                     {
1513                         ERR("Ligature index exeeded ligature count\n");
1514                         return;
1515                     }
1516                     offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
1517                     lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
1518
1519                     component_count = GET_BE_WORD(lt->ComponentCount);
1520                     component_size = class_count * sizeof(WORD);
1521                     offset = 0;
1522                     for (i = 0; i < component_count && !offset; i++)
1523                     {
1524                         int k;
1525                         const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
1526                         for (k = 0; k < class_count && !offset; k++)
1527                             offset = GET_BE_WORD(cr->LigatureAnchor[k]);
1528                         cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
1529                     }
1530                     if (!offset)
1531                     {
1532                         ERR("Failed to find avalible ligature connection point\n");
1533                         return;
1534                     }
1535
1536                     GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
1537                     offset = GET_BE_WORD(mr->MarkAnchor);
1538                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1539                     TRACE("Offset on ligature is %i,%i design units\n",ligature_pt.x,ligature_pt.y);
1540                     TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1541                     pt->x += ligature_pt.x - mark_pt.x;
1542                     pt->y += ligature_pt.y - mark_pt.y;
1543                     TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1544                 }
1545             }
1546         }
1547         else
1548             FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
1549     }
1550 }
1551
1552 static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1553                                   INT glyph_count, INT ppem, LPPOINT pt)
1554 {
1555     int j;
1556     BOOL rc = FALSE;
1557     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1558
1559     TRACE("MarkToMark Attachment Positioning Subtable\n");
1560
1561     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1562     {
1563         int offset;
1564         const GPOS_MarkMarkPosFormat1 *mmpf1;
1565         offset = GET_BE_WORD(look->SubTable[j]);
1566         mmpf1 = (const GPOS_MarkMarkPosFormat1*)((const BYTE*)look+offset);
1567         if (GET_BE_WORD(mmpf1->PosFormat) == 1)
1568         {
1569             int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
1570             int mark_index;
1571             mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
1572             if (mark_index != -1)
1573             {
1574                 int mark2_index;
1575                 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
1576                 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
1577                 if (mark2_index != -1)
1578                 {
1579                     const GPOS_MarkArray *ma;
1580                     const GPOS_MarkRecord *mr;
1581                     const GPOS_Mark2Array *m2a;
1582                     const GPOS_Mark2Record *m2r;
1583                     int mark_class;
1584                     int class_count = GET_BE_WORD(mmpf1->ClassCount);
1585                     int mark2record_size;
1586                     POINT mark2_pt;
1587                     POINT mark_pt;
1588                     TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
1589                     offset = GET_BE_WORD(mmpf1->Mark1Array);
1590                     ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
1591                     if (mark_index > GET_BE_WORD(ma->MarkCount))
1592                     {
1593                         ERR("Mark index exeeded mark count\n");
1594                         return FALSE;
1595                     }
1596                     mr = &ma->MarkRecord[mark_index];
1597                     mark_class = GET_BE_WORD(mr->Class);
1598                     TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1599                     offset = GET_BE_WORD(mmpf1->Mark2Array);
1600                     m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
1601                     mark2record_size = class_count * sizeof(WORD);
1602                     m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
1603                     offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
1604                     GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
1605                     offset = GET_BE_WORD(mr->MarkAnchor);
1606                     GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1607                     TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
1608                     TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1609                     pt->x += mark2_pt.x - mark_pt.x;
1610                     pt->y += mark2_pt.y - mark_pt.y;
1611                     TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1612                     rc = TRUE;
1613                 }
1614             }
1615         }
1616         else
1617             FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
1618     }
1619     return rc;
1620 }
1621
1622 static INT GPOS_apply_ChainContextPos(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1623                                       const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index,
1624                                       INT glyph_count, INT ppem, GOFFSET *pGoffset)
1625 {
1626     int j;
1627     int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1628
1629     TRACE("Chaining Contextual Positioning Subtable\n");
1630
1631     for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1632     {
1633         int offset;
1634         const GPOS_ChainContextPosFormat3_1 *ccpf3;
1635         int dirLookahead = write_dir;
1636         int dirBacktrack = -1 * write_dir;
1637
1638         offset = GET_BE_WORD(look->SubTable[j]);
1639         ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1640
1641         if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1642         {
1643             FIXME("  TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1644             continue;
1645         }
1646         else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1647         {
1648             FIXME("  TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1649             continue;
1650         }
1651         else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1652         {
1653             int k;
1654             int indexGlyphs;
1655             const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1656             const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1657             const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1658
1659             TRACE("  subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1660
1661             for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1662             {
1663                 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1664                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1665                     break;
1666             }
1667             if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1668                 continue;
1669             TRACE("Matched Backtrack\n");
1670
1671             ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
1672                     FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
1673
1674             indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1675             for (k = 0; k < indexGlyphs; k++)
1676             {
1677                 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1678                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1679                     break;
1680             }
1681             if (k != indexGlyphs)
1682                 continue;
1683             TRACE("Matched IndexGlyphs\n");
1684
1685             ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
1686                     FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
1687
1688             for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1689             {
1690                 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1691                 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1692                     break;
1693             }
1694             if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1695                 continue;
1696             TRACE("Matched LookAhead\n");
1697
1698             ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
1699                     FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
1700
1701             if (GET_BE_WORD(ccpf3_4->PosCount))
1702             {
1703                 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1704                 {
1705                     int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1706                     int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1707
1708                     TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1709                     GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, glyph_count, pGoffset);
1710                 }
1711                 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1712             }
1713             else return glyph_index + 1;
1714         }
1715         else
1716             FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1717     }
1718     return glyph_index + 1;
1719 }
1720
1721 static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1722 {
1723     int offset;
1724     const OT_LookupTable *look;
1725     int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1726
1727     offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1728     look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1729     TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1730     switch(GET_BE_WORD(look->LookupType))
1731     {
1732         case 1:
1733         {
1734             double devX, devY;
1735             POINT adjust = {0,0};
1736             POINT advance = {0,0};
1737             GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
1738             if (adjust.x || adjust.y)
1739             {
1740                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1741                 pGoffset[glyph_index].du += round(devX);
1742                 pGoffset[glyph_index].dv += round(devY);
1743             }
1744             if (advance.x || advance.y)
1745             {
1746                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1747                 piAdvance[glyph_index] += round(devX);
1748                 if (advance.y)
1749                     FIXME("Unhandled adjustment to Y advancement\n");
1750             }
1751             break;
1752         }
1753         case 2:
1754         {
1755             POINT advance[2]= {{0,0},{0,0}};
1756             POINT adjust[2]= {{0,0},{0,0}};
1757             double devX, devY;
1758             int index;
1759             int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1760             int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
1761
1762             index = GPOS_apply_PairAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, adjust, advance);
1763             if (adjust[0].x || adjust[0].y)
1764             {
1765                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1766                 pGoffset[glyph_index].du += round(devX) * offset_sign;
1767                 pGoffset[glyph_index].dv += round(devY);
1768             }
1769             if (advance[0].x || advance[0].y)
1770             {
1771                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1772                 piAdvance[glyph_index] += round(devX);
1773             }
1774             if (adjust[1].x || adjust[1].y)
1775             {
1776                 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1777                 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
1778                 pGoffset[glyph_index + write_dir].dv += round(devY);
1779             }
1780             if (advance[1].x || advance[1].y)
1781             {
1782                 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1783                 piAdvance[glyph_index + write_dir] += round(devX);
1784             }
1785             return index;
1786         }
1787         case 3:
1788         {
1789             POINT desU = {0,0};
1790             double devX, devY;
1791             int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1792
1793             GPOS_apply_CursiveAttachment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1794             if (desU.x || desU.y)
1795             {
1796                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1797                 /* Windows does not appear to apply X offsets here */
1798                 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index+write_dir].dv;
1799             }
1800             break;
1801         }
1802         case 4:
1803         {
1804             double devX, devY;
1805             POINT desU = {0,0};
1806             int base_index = GPOS_apply_MarkToBase(psc, look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1807             if (base_index != -1)
1808             {
1809                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1810                 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[base_index];
1811                 else
1812                 {
1813                     if (analysis->fLogicalOrder) devX *= -1;
1814                     pGoffset[glyph_index].du = round(devX);
1815                 }
1816                 pGoffset[glyph_index].dv = round(devY);
1817             }
1818             break;
1819         }
1820         case 5:
1821         {
1822             double devX, devY;
1823             POINT desU = {0,0};
1824             GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1825             if (desU.x || desU.y)
1826             {
1827                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1828                 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
1829                 pGoffset[glyph_index].dv = round(devY);
1830             }
1831             break;
1832         }
1833         case 6:
1834         {
1835             double devX, devY;
1836             POINT desU = {0,0};
1837             int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1838             if (GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU))
1839             {
1840                 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1841                 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
1842                 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
1843                 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
1844             }
1845             break;
1846         }
1847         case 8:
1848         {
1849             return GPOS_apply_ChainContextPos(psc, lpotm, lplogfont, analysis, piAdvance, lookup, look, glyphs, glyph_index, glyph_count, ppem, pGoffset);
1850         }
1851         default:
1852             FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1853     }
1854     return glyph_index+1;
1855 }
1856
1857 INT OpenType_apply_GPOS_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1858 {
1859     const GPOS_Header *header = (const GPOS_Header *)psc->GPOS_Table;
1860     const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1861
1862     return GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookup_index, glyphs, glyph_index, glyph_count, pGoffset);
1863 }
1864
1865 static void GSUB_initialize_script_cache(ScriptCache *psc)
1866 {
1867     int i;
1868
1869     if (psc->GSUB_Table)
1870     {
1871         const OT_ScriptList *script;
1872         const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1873         script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1874         psc->script_count = GET_BE_WORD(script->ScriptCount);
1875         TRACE("initializing %i scripts in this font\n",psc->script_count);
1876         if (psc->script_count)
1877         {
1878             psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1879             for (i = 0; i < psc->script_count; i++)
1880             {
1881                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1882                 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]);
1883                 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1884             }
1885         }
1886     }
1887 }
1888
1889 static void GPOS_expand_script_cache(ScriptCache *psc)
1890 {
1891     int i, count;
1892     const OT_ScriptList *script;
1893     const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1894
1895     if (!header)
1896         return;
1897
1898     script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1899     count = GET_BE_WORD(script->ScriptCount);
1900
1901     if (!count)
1902         return;
1903
1904     if (!psc->script_count)
1905     {
1906         psc->script_count = count;
1907         TRACE("initializing %i scripts in this font\n",psc->script_count);
1908         if (psc->script_count)
1909         {
1910             psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1911             for (i = 0; i < psc->script_count; i++)
1912             {
1913                 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1914                 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]);
1915                 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1916             }
1917         }
1918     }
1919     else
1920     {
1921         for (i = 0; i < count; i++)
1922         {
1923             int j;
1924             int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1925             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]);
1926             for (j = 0; j < psc->script_count; j++)
1927             {
1928                 if (psc->scripts[j].tag == tag)
1929                 {
1930                     psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1931                     break;
1932                 }
1933             }
1934             if (j == psc->script_count)
1935             {
1936                 psc->script_count++;
1937                 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1938                 psc->scripts[j].tag = tag;
1939                 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1940             }
1941         }
1942     }
1943 }
1944
1945 static void _initialize_script_cache(ScriptCache *psc)
1946 {
1947     if (!psc->scripts_initialized)
1948     {
1949         GSUB_initialize_script_cache(psc);
1950         GPOS_expand_script_cache(psc);
1951         psc->scripts_initialized = TRUE;
1952     }
1953 }
1954
1955 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1956 {
1957     int i;
1958     HRESULT rc = S_OK;
1959
1960     _initialize_script_cache(psc);
1961
1962     *pcTags = psc->script_count;
1963
1964     if (!searchingFor && cMaxTags < *pcTags)
1965         rc = E_OUTOFMEMORY;
1966     else if (searchingFor)
1967         rc = USP_E_SCRIPT_NOT_IN_FONT;
1968
1969     for (i = 0; i < psc->script_count; i++)
1970     {
1971         if (i < cMaxTags)
1972             pScriptTags[i] = psc->scripts[i].tag;
1973
1974         if (searchingFor)
1975         {
1976             if (searchingFor == psc->scripts[i].tag)
1977             {
1978                 pScriptTags[0] = psc->scripts[i].tag;
1979                 *pcTags = 1;
1980                 rc = S_OK;
1981                 break;
1982             }
1983         }
1984     }
1985     return rc;
1986 }
1987
1988 static void GSUB_initialize_language_cache(LoadedScript *script)
1989 {
1990     int i;
1991
1992     if (script->gsub_table)
1993     {
1994         DWORD offset;
1995         const OT_Script* table = script->gsub_table;
1996         script->language_count = GET_BE_WORD(table->LangSysCount);
1997         offset = GET_BE_WORD(table->DefaultLangSys);
1998         if (offset)
1999         {
2000             script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
2001             script->default_language.gsub_table = (const BYTE*)table + offset;
2002         }
2003
2004         if (script->language_count)
2005         {
2006             TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
2007
2008             script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2009
2010             for (i = 0; i < script->language_count; i++)
2011             {
2012                 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2013                 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]);
2014                 script->languages[i].gsub_table = ((const BYTE*)table + offset);
2015             }
2016         }
2017     }
2018 }
2019
2020 static void GPOS_expand_language_cache(LoadedScript *script)
2021 {
2022     int count;
2023     const OT_Script* table = script->gpos_table;
2024     DWORD offset;
2025
2026     if (!table)
2027         return;
2028
2029     offset = GET_BE_WORD(table->DefaultLangSys);
2030     if (offset)
2031         script->default_language.gpos_table = (const BYTE*)table + offset;
2032
2033     count = GET_BE_WORD(table->LangSysCount);
2034
2035     TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
2036
2037     if (!count)
2038         return;
2039
2040     if (!script->language_count)
2041     {
2042         int i;
2043         script->language_count = count;
2044
2045         script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2046
2047         for (i = 0; i < script->language_count; i++)
2048         {
2049             int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2050             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]);
2051             script->languages[i].gpos_table = ((const BYTE*)table + offset);
2052         }
2053     }
2054     else if (count)
2055     {
2056         int i,j;
2057         for (i = 0; i < count; i++)
2058         {
2059             int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2060             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]);
2061
2062             for (j = 0; j < script->language_count; j++)
2063             {
2064                 if (script->languages[j].tag == tag)
2065                 {
2066                     script->languages[j].gpos_table = ((const BYTE*)table + offset);
2067                     break;
2068                 }
2069             }
2070             if (j == script->language_count)
2071             {
2072                 script->language_count++;
2073                 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
2074                 script->languages[j].tag = tag;
2075                 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2076             }
2077         }
2078     }
2079 }
2080
2081 static void _initialize_language_cache(LoadedScript *script)
2082 {
2083     if (!script->languages_initialized)
2084     {
2085         GSUB_initialize_language_cache(script);
2086         GPOS_expand_language_cache(script);
2087         script->languages_initialized = TRUE;
2088     }
2089 }
2090
2091 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2092 {
2093     int i;
2094     HRESULT rc = S_OK;
2095     LoadedScript *script = NULL;
2096
2097     _initialize_script_cache(psc);
2098
2099     for (i = 0; i < psc->script_count; i++)
2100     {
2101          if (psc->scripts[i].tag == script_tag)
2102          {
2103             script = &psc->scripts[i];
2104             break;
2105          }
2106     }
2107
2108     if (!script)
2109         return E_INVALIDARG;
2110
2111     _initialize_language_cache(script);
2112
2113     if (!searchingFor && cMaxTags < script->language_count)
2114         rc = E_OUTOFMEMORY;
2115     else if (searchingFor)
2116         rc = E_INVALIDARG;
2117
2118     *pcTags = script->language_count;
2119
2120     for (i = 0; i < script->language_count; i++)
2121     {
2122         if (i < cMaxTags)
2123             pLanguageTags[i] = script->languages[i].tag;
2124
2125         if (searchingFor)
2126         {
2127             if (searchingFor == script->languages[i].tag)
2128             {
2129                 pLanguageTags[0] = script->languages[i].tag;
2130                 *pcTags = 1;
2131                 rc = S_OK;
2132                 break;
2133             }
2134         }
2135     }
2136
2137     if (script->default_language.gsub_table)
2138     {
2139         if (i < cMaxTags)
2140             pLanguageTags[i] = script->default_language.tag;
2141
2142         if (searchingFor  && FAILED(rc))
2143         {
2144             pLanguageTags[0] = script->default_language.tag;
2145         }
2146         i++;
2147         *pcTags = (*pcTags) + 1;
2148     }
2149
2150     return rc;
2151 }
2152
2153
2154 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
2155 {
2156     int i;
2157
2158     if (language->gsub_table)
2159     {
2160         const OT_LangSys *lang = language->gsub_table;
2161         const GSUB_Header *header = (const GSUB_Header *)table;
2162         const OT_FeatureList *feature_list;
2163
2164         language->feature_count = GET_BE_WORD(lang->FeatureCount);
2165         TRACE("%i features\n",language->feature_count);
2166
2167         if (language->feature_count)
2168         {
2169             language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2170
2171             feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2172
2173             for (i = 0; i < language->feature_count; i++)
2174             {
2175                 const OT_Feature *feature;
2176                 int j;
2177                 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2178
2179                 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]);
2180                 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2181                 feature = (const OT_Feature*)language->features[i].feature;
2182                 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2183                 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2184                 for (j = 0; j < language->features[i].lookup_count; j++)
2185                     language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2186                 language->features[i].tableType = FEATURE_GSUB_TABLE;
2187             }
2188         }
2189     }
2190 }
2191
2192 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
2193 {
2194     int i, count;
2195     const OT_LangSys *lang = language->gpos_table;
2196     const GPOS_Header *header = (const GPOS_Header *)table;
2197     const OT_FeatureList *feature_list;
2198
2199     if (!lang)
2200         return;
2201
2202     count = GET_BE_WORD(lang->FeatureCount);
2203     feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2204
2205     TRACE("%i features\n",count);
2206
2207     if (!count)
2208         return;
2209
2210     if (!language->feature_count)
2211     {
2212         language->feature_count = count;
2213
2214         if (language->feature_count)
2215         {
2216             language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2217
2218             for (i = 0; i < language->feature_count; i++)
2219             {
2220                 const OT_Feature *feature;
2221                 int j;
2222                 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2223
2224                 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]);
2225                 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2226                 feature = (const OT_Feature*)language->features[i].feature;
2227                 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2228                 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2229                 for (j = 0; j < language->features[i].lookup_count; j++)
2230                     language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2231                 language->features[i].tableType = FEATURE_GPOS_TABLE;
2232             }
2233         }
2234     }
2235     else
2236     {
2237         language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
2238
2239         for (i = 0; i < count; i++)
2240         {
2241             const OT_Feature *feature;
2242             int j;
2243             int index = GET_BE_WORD(lang->FeatureIndex[i]);
2244             int idx = language->feature_count + i;
2245
2246             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]);
2247             language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2248             feature = (const OT_Feature*)language->features[idx].feature;
2249             language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
2250             language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
2251             for (j = 0; j < language->features[idx].lookup_count; j++)
2252                 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2253             language->features[idx].tableType = FEATURE_GPOS_TABLE;
2254         }
2255         language->feature_count += count;
2256     }
2257 }
2258
2259 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2260 {
2261     if (!language->features_initialized)
2262     {
2263         GSUB_initialize_feature_cache(psc->GSUB_Table, language);
2264         GPOS_expand_feature_cache(psc->GPOS_Table, language);
2265         language->features_initialized = TRUE;
2266     }
2267 }
2268
2269 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, char tableType, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
2270 {
2271     int i;
2272     HRESULT rc = S_OK;
2273     LoadedScript *script = NULL;
2274     LoadedLanguage *language = NULL;
2275
2276     _initialize_script_cache(psc);
2277
2278     for (i = 0; i < psc->script_count; i++)
2279     {
2280         if (psc->scripts[i].tag == script_tag)
2281         {
2282             script = &psc->scripts[i];
2283             break;
2284         }
2285     }
2286
2287     if (!script)
2288     {
2289         *pcTags = 0;
2290         if (!filtered)
2291             return S_OK;
2292         else
2293             return E_INVALIDARG;
2294     }
2295
2296     _initialize_language_cache(script);
2297
2298     if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2299         language = &script->default_language;
2300     else
2301     {
2302         for (i = 0; i < script->language_count; i++)
2303         {
2304             if (script->languages[i].tag == language_tag)
2305             {
2306                 language = &script->languages[i];
2307                 break;
2308             }
2309         }
2310     }
2311
2312     if (!language)
2313     {
2314         *pcTags = 0;
2315         return S_OK;
2316     }
2317
2318     _initialize_feature_cache(psc, language);
2319
2320     if (tableType)
2321     {
2322         *pcTags = 0;
2323         for (i = 0; i < language->feature_count; i++)
2324             if (language->features[i].tableType == tableType)
2325                 *pcTags = (*pcTags)+1;
2326     }
2327     else
2328         *pcTags = language->feature_count;
2329
2330     if (!searchingFor && cMaxTags < *pcTags)
2331         rc = E_OUTOFMEMORY;
2332     else if (searchingFor)
2333         rc = E_INVALIDARG;
2334
2335     for (i = 0; i < language->feature_count; i++)
2336     {
2337         if (i < cMaxTags)
2338         {
2339             if (!tableType || language->features[i].tableType == tableType)
2340                 pFeatureTags[i] = language->features[i].tag;
2341         }
2342
2343         if (searchingFor)
2344         {
2345             if ((searchingFor == language->features[i].tag) &&
2346                 (!tableType || language->features[i].tableType == tableType))
2347             {
2348                 pFeatureTags[0] = language->features[i].tag;
2349                 *pcTags = 1;
2350                 if (feature)
2351                     *feature = &language->features[i];
2352                 rc = S_OK;
2353                 break;
2354             }
2355         }
2356     }
2357     return rc;
2358 }