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