winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / usp10 / opentype.c
blobe7f2dfdd9380da6b4d0fc72922c7c9ccd97f920e
1 /*
2 * Opentype font interfaces for the Uniscribe Script Processor (usp10.dll)
4 * Copyright 2012 CodeWeavers, Aric Stewart
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.
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.
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
21 #include <stdarg.h>
22 #include <stdlib.h>
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"
32 #include "usp10_internal.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
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
46 #define round(x) (((x) < 0) ? (int)((x) - 0.5) : (int)((x) + 0.5))
48 /* These are all structures needed for the cmap format 12 table */
49 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
51 typedef struct {
52 WORD platformID;
53 WORD encodingID;
54 DWORD offset;
55 } CMAP_EncodingRecord;
57 typedef struct {
58 WORD version;
59 WORD numTables;
60 CMAP_EncodingRecord tables[1];
61 } CMAP_Header;
63 typedef struct {
64 DWORD startCharCode;
65 DWORD endCharCode;
66 DWORD startGlyphID;
67 } CMAP_SegmentedCoverage_group;
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;
78 /* These are all structures needed for the GDEF table */
79 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
81 typedef struct {
82 DWORD Version;
83 WORD GlyphClassDef;
84 WORD AttachList;
85 WORD LigCaretList;
86 WORD MarkAttachClassDef;
87 } GDEF_Header;
89 typedef struct {
90 WORD ClassFormat;
91 WORD StartGlyph;
92 WORD GlyphCount;
93 WORD ClassValueArray[1];
94 } OT_ClassDefFormat1;
96 typedef struct {
97 WORD Start;
98 WORD End;
99 WORD Class;
100 } OT_ClassRangeRecord;
102 typedef struct {
103 WORD ClassFormat;
104 WORD ClassRangeCount;
105 OT_ClassRangeRecord ClassRangeRecord[1];
106 } OT_ClassDefFormat2;
108 /* These are all structures needed for the GSUB table */
110 typedef struct {
111 DWORD version;
112 WORD ScriptList;
113 WORD FeatureList;
114 WORD LookupList;
115 } GSUB_Header;
117 typedef struct {
118 CHAR ScriptTag[4];
119 WORD Script;
120 } OT_ScriptRecord;
122 typedef struct {
123 WORD ScriptCount;
124 OT_ScriptRecord ScriptRecord[1];
125 } OT_ScriptList;
127 typedef struct {
128 CHAR LangSysTag[4];
129 WORD LangSys;
130 } OT_LangSysRecord;
132 typedef struct {
133 WORD DefaultLangSys;
134 WORD LangSysCount;
135 OT_LangSysRecord LangSysRecord[1];
136 } OT_Script;
138 typedef struct {
139 WORD LookupOrder; /* Reserved */
140 WORD ReqFeatureIndex;
141 WORD FeatureCount;
142 WORD FeatureIndex[1];
143 } OT_LangSys;
145 typedef struct {
146 CHAR FeatureTag[4];
147 WORD Feature;
148 } OT_FeatureRecord;
150 typedef struct {
151 WORD FeatureCount;
152 OT_FeatureRecord FeatureRecord[1];
153 } OT_FeatureList;
155 typedef struct {
156 WORD FeatureParams; /* Reserved */
157 WORD LookupCount;
158 WORD LookupListIndex[1];
159 } OT_Feature;
161 typedef struct {
162 WORD LookupCount;
163 WORD Lookup[1];
164 } OT_LookupList;
166 typedef struct {
167 WORD LookupType;
168 WORD LookupFlag;
169 WORD SubTableCount;
170 WORD SubTable[1];
171 } OT_LookupTable;
173 typedef struct {
174 WORD CoverageFormat;
175 WORD GlyphCount;
176 WORD GlyphArray[1];
177 } OT_CoverageFormat1;
179 typedef struct {
180 WORD Start;
181 WORD End;
182 WORD StartCoverageIndex;
183 } OT_RangeRecord;
185 typedef struct {
186 WORD CoverageFormat;
187 WORD RangeCount;
188 OT_RangeRecord RangeRecord[1];
189 } OT_CoverageFormat2;
191 typedef struct {
192 WORD SubstFormat; /* = 1 */
193 WORD Coverage;
194 WORD DeltaGlyphID;
195 } GSUB_SingleSubstFormat1;
197 typedef struct {
198 WORD SubstFormat; /* = 2 */
199 WORD Coverage;
200 WORD GlyphCount;
201 WORD Substitute[1];
202 }GSUB_SingleSubstFormat2;
204 typedef struct {
205 WORD SubstFormat; /* = 1 */
206 WORD Coverage;
207 WORD SequenceCount;
208 WORD Sequence[1];
209 }GSUB_MultipleSubstFormat1;
211 typedef struct {
212 WORD GlyphCount;
213 WORD Substitute[1];
214 }GSUB_Sequence;
216 typedef struct {
217 WORD SubstFormat; /* = 1 */
218 WORD Coverage;
219 WORD LigSetCount;
220 WORD LigatureSet[1];
221 }GSUB_LigatureSubstFormat1;
223 typedef struct {
224 WORD LigatureCount;
225 WORD Ligature[1];
226 }GSUB_LigatureSet;
228 typedef struct{
229 WORD LigGlyph;
230 WORD CompCount;
231 WORD Component[1];
232 }GSUB_Ligature;
234 typedef struct{
235 WORD SequenceIndex;
236 WORD LookupListIndex;
238 }GSUB_SubstLookupRecord;
240 typedef struct{
241 WORD SubstFormat; /* = 1 */
242 WORD Coverage;
243 WORD ChainSubRuleSetCount;
244 WORD ChainSubRuleSet[1];
245 }GSUB_ChainContextSubstFormat1;
247 typedef struct {
248 WORD SubstFormat; /* = 3 */
249 WORD BacktrackGlyphCount;
250 WORD Coverage[1];
251 }GSUB_ChainContextSubstFormat3_1;
253 typedef struct{
254 WORD InputGlyphCount;
255 WORD Coverage[1];
256 }GSUB_ChainContextSubstFormat3_2;
258 typedef struct{
259 WORD LookaheadGlyphCount;
260 WORD Coverage[1];
261 }GSUB_ChainContextSubstFormat3_3;
263 typedef struct{
264 WORD SubstCount;
265 GSUB_SubstLookupRecord SubstLookupRecord[1];
266 }GSUB_ChainContextSubstFormat3_4;
268 typedef struct {
269 WORD SubstFormat; /* = 1 */
270 WORD Coverage;
271 WORD AlternateSetCount;
272 WORD AlternateSet[1];
273 } GSUB_AlternateSubstFormat1;
275 typedef struct{
276 WORD GlyphCount;
277 WORD Alternate[1];
278 } GSUB_AlternateSet;
280 /* These are all structures needed for the GPOS table */
282 typedef struct {
283 DWORD version;
284 WORD ScriptList;
285 WORD FeatureList;
286 WORD LookupList;
287 } GPOS_Header;
289 typedef struct {
290 WORD StartSize;
291 WORD EndSize;
292 WORD DeltaFormat;
293 WORD DeltaValue[1];
294 } OT_DeviceTable;
296 typedef struct {
297 WORD AnchorFormat;
298 WORD XCoordinate;
299 WORD YCoordinate;
300 } GPOS_AnchorFormat1;
302 typedef struct {
303 WORD AnchorFormat;
304 WORD XCoordinate;
305 WORD YCoordinate;
306 WORD AnchorPoint;
307 } GPOS_AnchorFormat2;
309 typedef struct {
310 WORD AnchorFormat;
311 WORD XCoordinate;
312 WORD YCoordinate;
313 WORD XDeviceTable;
314 WORD YDeviceTable;
315 } GPOS_AnchorFormat3;
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;
328 typedef struct {
329 WORD PosFormat;
330 WORD Coverage;
331 WORD ValueFormat;
332 WORD Value[1];
333 } GPOS_SinglePosFormat1;
335 typedef struct {
336 WORD PosFormat;
337 WORD Coverage;
338 WORD ValueFormat;
339 WORD ValueCount;
340 WORD Value[1];
341 } GPOS_SinglePosFormat2;
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;
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;
364 typedef struct {
365 WORD SecondGlyph;
366 WORD Value1[1];
367 WORD Value2[1];
368 } GPOS_PairValueRecord;
370 typedef struct {
371 WORD PairValueCount;
372 GPOS_PairValueRecord PairValueRecord[1];
373 } GPOS_PairSet;
375 typedef struct {
376 WORD EntryAnchor;
377 WORD ExitAnchor;
378 } GPOS_EntryExitRecord;
380 typedef struct {
381 WORD PosFormat;
382 WORD Coverage;
383 WORD EntryExitCount;
384 GPOS_EntryExitRecord EntryExitRecord[1];
385 } GPOS_CursivePosFormat1;
387 typedef struct {
388 WORD PosFormat;
389 WORD MarkCoverage;
390 WORD BaseCoverage;
391 WORD ClassCount;
392 WORD MarkArray;
393 WORD BaseArray;
394 } GPOS_MarkBasePosFormat1;
396 typedef struct {
397 WORD BaseAnchor[1];
398 } GPOS_BaseRecord;
400 typedef struct {
401 WORD BaseCount;
402 GPOS_BaseRecord BaseRecord[1];
403 } GPOS_BaseArray;
405 typedef struct {
406 WORD Class;
407 WORD MarkAnchor;
408 } GPOS_MarkRecord;
410 typedef struct {
411 WORD MarkCount;
412 GPOS_MarkRecord MarkRecord[1];
413 } GPOS_MarkArray;
415 typedef struct {
416 WORD PosFormat;
417 WORD MarkCoverage;
418 WORD LigatureCoverage;
419 WORD ClassCount;
420 WORD MarkArray;
421 WORD LigatureArray;
422 } GPOS_MarkLigPosFormat1;
424 typedef struct {
425 WORD LigatureCount;
426 WORD LigatureAttach[1];
427 } GPOS_LigatureArray;
429 typedef struct {
430 WORD LigatureAnchor[1];
431 } GPOS_ComponentRecord;
433 typedef struct {
434 WORD ComponentCount;
435 GPOS_ComponentRecord ComponentRecord[1];
436 } GPOS_LigatureAttach;
438 typedef struct {
439 WORD PosFormat;
440 WORD Mark1Coverage;
441 WORD Mark2Coverage;
442 WORD ClassCount;
443 WORD Mark1Array;
444 WORD Mark2Array;
445 } GPOS_MarkMarkPosFormat1;
447 typedef struct {
448 WORD Mark2Anchor[1];
449 } GPOS_Mark2Record;
451 typedef struct {
452 WORD Mark2Count;
453 GPOS_Mark2Record Mark2Record[1];
454 } GPOS_Mark2Array;
456 typedef struct {
457 WORD SequenceIndex;
458 WORD LookupListIndex;
459 } GPOS_PosLookupRecord;
461 typedef struct {
462 WORD PosFormat;
463 WORD BacktrackGlyphCount;
464 WORD Coverage[1];
465 } GPOS_ChainContextPosFormat3_1;
467 typedef struct {
468 WORD InputGlyphCount;
469 WORD Coverage[1];
470 } GPOS_ChainContextPosFormat3_2;
472 typedef struct {
473 WORD LookaheadGlyphCount;
474 WORD Coverage[1];
475 } GPOS_ChainContextPosFormat3_3;
477 typedef struct {
478 WORD PosCount;
479 GPOS_PosLookupRecord PosLookupRecord[1];
480 } GPOS_ChainContextPosFormat3_4;
482 typedef struct {
483 WORD PosFormat;
484 WORD ExtensionLookupType;
485 DWORD ExtensionOffset;
486 } GPOS_ExtensionPosFormat1;
488 /**********
489 * CMAP
490 **********/
492 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
494 CMAP_Header *CMAP_Table = NULL;
495 int length;
496 int i;
498 if (!psc->CMAP_Table)
500 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
501 if (length != GDI_ERROR)
503 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
504 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
505 TRACE("Loaded cmap table of %i bytes\n",length);
507 else
508 return NULL;
511 CMAP_Table = psc->CMAP_Table;
513 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
515 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
516 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
518 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
519 if (GET_BE_WORD(format->format) == 12)
520 return format;
523 return NULL;
526 static int compare_group(const void *a, const void* b)
528 const DWORD *chr = a;
529 const CMAP_SegmentedCoverage_group *group = b;
531 if (*chr < GET_BE_DWORD(group->startCharCode))
532 return -1;
533 if (*chr > GET_BE_DWORD(group->endCharCode))
534 return 1;
535 return 0;
538 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
540 /* BMP: use gdi32 for ease */
541 if (utf32c < 0x10000)
543 WCHAR ch = utf32c;
544 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
547 if (!psc->CMAP_format12_Table)
548 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
550 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
551 *pgi = 0xffff;
552 else
553 *pgi = 0;
555 if (psc->CMAP_format12_Table)
557 CMAP_SegmentedCoverage *format = NULL;
558 CMAP_SegmentedCoverage_group *group = NULL;
560 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
562 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
563 sizeof(CMAP_SegmentedCoverage_group), compare_group);
565 if (group)
567 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
568 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
569 return 0;
572 return 0;
575 /**********
576 * GDEF
577 **********/
579 static WORD OT_get_glyph_class(const void *table, WORD glyph)
581 WORD class = 0;
582 const OT_ClassDefFormat1 *cf1 = table;
584 if (!table) return 0;
586 if (GET_BE_WORD(cf1->ClassFormat) == 1)
588 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
590 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
591 if (index < GET_BE_WORD(cf1->GlyphCount))
592 class = GET_BE_WORD(cf1->ClassValueArray[index]);
595 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
597 const OT_ClassDefFormat2 *cf2 = table;
598 int i, top;
599 top = GET_BE_WORD(cf2->ClassRangeCount);
600 for (i = 0; i < top; i++)
602 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
603 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
605 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
606 break;
610 else
611 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
613 return class;
616 void OpenType_GDEF_UpdateGlyphProps(ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
618 int i;
619 void *glyph_class_table = NULL;
621 if (psc->GDEF_Table)
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;
629 for (i = 0; i < cGlyphs; i++)
631 WORD class;
632 int char_count = 0;
633 int k;
635 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
636 if (k >= 0)
638 for (; k < cChars && pwLogClust[k] == i; k++)
639 char_count++;
642 class = OT_get_glyph_class( glyph_class_table, pwGlyphs[i] );
644 switch (class)
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;
674 if (char_count == 0)
675 pGlyphProp[i].sva.fClusterStart = 0;
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);
684 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
686 const OT_CoverageFormat1* cf1;
688 cf1 = table;
690 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
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;
700 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
702 const OT_CoverageFormat2* cf2;
703 int i;
704 int count;
705 cf2 = (const OT_CoverageFormat2*)cf1;
707 count = GET_BE_WORD(cf2->RangeCount);
708 TRACE("Coverage Format 2, %i ranges\n",count);
709 for (i = 0; i < count; i++)
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)))
716 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
717 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
720 return -1;
722 else
723 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
725 return -1;
728 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
730 int j;
731 TRACE("Single Substitution Subtable\n");
733 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
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)
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)
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;
751 else
753 const GSUB_SingleSubstFormat2 *ssf2;
754 INT index;
755 INT offset;
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)
764 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
765 return GSUB_E_NOGLYPH;
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;
774 return GSUB_E_NOGLYPH;
777 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
779 int j;
780 TRACE("Multiple Substitution Subtable\n");
782 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
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);
789 offset = GET_BE_WORD(msf1->Coverage);
790 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
791 if (index != -1)
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));
801 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
802 glyphs[j] =glyphs[j-(sub_count-1)];
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]);
810 *glyph_count = *glyph_count + (sub_count - 1);
812 if (TRACE_ON(uniscribe))
814 for (j = 0; j < sub_count; j++)
815 TRACE(" 0x%x",glyphs[glyph_index+j]);
816 TRACE("\n");
819 return glyph_index + (sub_count * write_dir);
822 return GSUB_E_NOGLYPH;
825 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
827 int j;
828 TRACE("Alternate Substitution Subtable\n");
830 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
832 int offset;
833 const GSUB_AlternateSubstFormat1 *asf1;
834 INT index;
836 offset = GET_BE_WORD(look->SubTable[j]);
837 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
838 offset = GET_BE_WORD(asf1->Coverage);
840 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
841 if (index != -1)
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;
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;
856 return GSUB_E_NOGLYPH;
859 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
861 int j;
863 TRACE("Ligature Substitution Subtable\n");
864 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
866 const GSUB_LigatureSubstFormat1 *lsf1;
867 int offset,index;
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)
876 const GSUB_LigatureSet *ls;
877 int k, count;
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++)
885 const GSUB_Ligature *lig;
886 int CompCount,l,CompIndex;
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++)
894 int CompGlyph;
895 CompGlyph = GET_BE_WORD(lig->Component[l]);
896 if (CompGlyph != glyphs[CompIndex])
897 break;
898 CompIndex += write_dir;
900 if (l == CompCount)
902 int replaceIdx = glyph_index;
903 if (write_dir < 0)
904 replaceIdx = glyph_index - CompCount;
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)
911 int j;
912 for (j = replaceIdx + 1; j < *glyph_count; j++)
913 glyphs[j] =glyphs[j+CompCount];
914 *glyph_count = *glyph_count - CompCount;
916 return replaceIdx + write_dir;
921 return GSUB_E_NOGLYPH;
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)
926 int j;
928 TRACE("Chaining Contextual Substitution Subtable\n");
929 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
931 const GSUB_ChainContextSubstFormat1 *ccsf1;
932 int offset;
933 int dirLookahead = write_dir;
934 int dirBacktrack = -1 * write_dir;
936 offset = GET_BE_WORD(look->SubTable[j]);
937 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
938 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
940 static int once;
941 if (!once++)
942 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
943 continue;
945 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
947 static int once;
948 if (!once++)
949 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
950 continue;
952 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
954 int k;
955 int indexGlyphs;
956 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
957 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
958 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
959 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
960 int newIndex = glyph_index;
962 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
964 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
966 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
968 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
969 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
970 break;
972 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
973 continue;
974 TRACE("Matched Backtrack\n");
976 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
977 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
979 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
980 for (k = 0; k < indexGlyphs; k++)
982 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
983 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
984 break;
986 if (k != indexGlyphs)
987 continue;
988 TRACE("Matched IndexGlyphs\n");
990 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
991 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
993 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
995 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
996 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
997 break;
999 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
1000 continue;
1001 TRACE("Matched LookAhead\n");
1003 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
1004 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
1006 if (GET_BE_WORD(ccsf3_4->SubstCount))
1008 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
1010 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
1011 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
1013 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1014 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
1015 if (newIndex == -1)
1017 ERR("Chain failed to generate a glyph\n");
1018 continue;
1021 return newIndex;
1023 else return GSUB_E_NOGLYPH;
1026 return -1;
1029 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1031 int offset;
1032 const OT_LookupTable *look;
1034 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1035 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1036 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1037 switch(GET_BE_WORD(look->LookupType))
1039 case 1:
1040 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1041 case 2:
1042 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1043 case 3:
1044 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1045 case 4:
1046 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1047 case 6:
1048 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1049 default:
1050 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1052 return GSUB_E_NOGLYPH;
1055 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1057 const GSUB_Header *header = (const GSUB_Header *)table;
1058 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1060 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1063 /**********
1064 * GPOS
1065 **********/
1066 static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1067 const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset);
1069 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1071 static const WORD mask[3] = {3,0xf,0xff};
1072 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1074 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1075 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1076 int value;
1077 TRACE("device table, format %i, index %i\n",format, index);
1078 index = index << format;
1079 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1080 TRACE("offset %i, value %i\n",index, value);
1081 if (value > mask[format-1]/2)
1082 value = -1 * ((mask[format-1]+1) - value);
1083 return value;
1085 return 0;
1088 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1090 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1092 switch (GET_BE_WORD(anchor1->AnchorFormat))
1094 case 1:
1096 TRACE("Anchor Format 1\n");
1097 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1098 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1099 break;
1101 case 2:
1103 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1104 TRACE("Anchor Format 2\n");
1105 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1106 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1107 break;
1109 case 3:
1111 int offset;
1112 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1113 TRACE("Anchor Format 3\n");
1114 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1115 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1116 offset = GET_BE_WORD(anchor3->XDeviceTable);
1117 TRACE("ppem %i\n",ppem);
1118 if (offset)
1120 const OT_DeviceTable* DeviceTableX = NULL;
1121 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1122 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1124 offset = GET_BE_WORD(anchor3->YDeviceTable);
1125 if (offset)
1127 const OT_DeviceTable* DeviceTableY = NULL;
1128 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1129 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1131 break;
1133 default:
1134 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1135 pt->x = 0;
1136 pt->y = 0;
1140 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1142 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1144 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1145 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1146 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1147 if (lplogfont->lfWidth)
1148 FIXME("Font with lfWidth set no handled properly\n");
1151 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1153 INT offset = 0;
1154 if (ValueFormat & 0x0001) { if (data) record->XPlacement = GET_BE_WORD(data[offset]); offset++; }
1155 if (ValueFormat & 0x0002) { if (data) record->YPlacement = GET_BE_WORD(data[offset]); offset++; }
1156 if (ValueFormat & 0x0004) { if (data) record->XAdvance = GET_BE_WORD(data[offset]); offset++; }
1157 if (ValueFormat & 0x0008) { if (data) record->YAdvance = GET_BE_WORD(data[offset]); offset++; }
1158 if (ValueFormat & 0x0010) { if (data) record->XPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1159 if (ValueFormat & 0x0020) { if (data) record->YPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1160 if (ValueFormat & 0x0040) { if (data) record->XAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1161 if (ValueFormat & 0x0080) { if (data) record->YAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1162 return offset;
1165 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1167 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1168 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1169 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1170 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1171 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1172 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1173 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1174 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1175 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1178 static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
1180 int offset = GET_BE_WORD(look->SubTable[index]);
1182 if (GET_BE_WORD(look->LookupType) == 9)
1184 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + offset);
1185 if (GET_BE_WORD(ext->PosFormat) == 1)
1187 offset += GET_BE_DWORD(ext->ExtensionOffset);
1189 else
1191 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1194 return (const BYTE *)look + offset;
1197 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1198 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1200 int j;
1202 TRACE("Single Adjustment Positioning Subtable\n");
1204 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1206 const GPOS_SinglePosFormat1 *spf1 = (const GPOS_SinglePosFormat1*)GPOS_get_subtable(look, j);
1207 WORD offset;
1208 if (GET_BE_WORD(spf1->PosFormat) == 1)
1210 offset = GET_BE_WORD(spf1->Coverage);
1211 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1213 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1214 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1215 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1216 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1217 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1220 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1222 int index;
1223 const GPOS_SinglePosFormat2 *spf2;
1224 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1225 offset = GET_BE_WORD(spf2->Coverage);
1226 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1227 if (index != -1)
1229 int size;
1230 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1231 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1232 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1233 if (index > 0)
1235 offset = size * index;
1236 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1238 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1239 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1242 else
1243 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1247 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1248 INT ppem, POINT *adjust, POINT *advance )
1250 GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1251 GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1252 INT size;
1254 size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1255 GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1257 if (val_fmt1)
1259 GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1260 TRACE( "Glyph 1 resulting cumulative offset is %i,%i design units\n", adjust[0].x, adjust[0].y );
1261 TRACE( "Glyph 1 resulting cumulative advance is %i,%i design units\n", advance[0].x, advance[0].y );
1263 if (val_fmt2)
1265 GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1266 TRACE( "Glyph 2 resulting cumulative offset is %i,%i design units\n", adjust[1].x, adjust[1].y );
1267 TRACE( "Glyph 2 resulting cumulative advance is %i,%i design units\n", advance[1].x, advance[1].y );
1271 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1272 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1274 int j;
1275 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1277 if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return glyph_index + 1;
1279 TRACE("Pair Adjustment Positioning Subtable\n");
1281 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1283 const GPOS_PairPosFormat1 *ppf1 = (const GPOS_PairPosFormat1*)GPOS_get_subtable(look, j);
1284 WORD offset;
1285 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1287 int index;
1288 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1289 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1290 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1291 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1292 offset = GET_BE_WORD(ppf1->Coverage);
1293 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1294 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1296 int k;
1297 int pair_count;
1298 const GPOS_PairSet *ps;
1299 const GPOS_PairValueRecord *pair_val_rec;
1300 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1301 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1302 pair_count = GET_BE_WORD(ps->PairValueCount);
1303 pair_val_rec = ps->PairValueRecord;
1304 for (k = 0; k < pair_count; k++)
1306 WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1307 if (glyphs[glyph_index+write_dir] == second_glyph)
1309 int next = 1;
1310 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1311 apply_pair_value( ppf1, ValueFormat1, ValueFormat2, pair_val_rec->Value1, ppem, ptAdjust, ptAdvance );
1312 if (ValueFormat2) next++;
1313 return glyph_index + next;
1315 pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1319 else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1321 const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)ppf1;
1322 int index;
1323 WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1324 WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1325 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1326 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1327 WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1328 WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1330 offset = GET_BE_WORD( ppf2->Coverage );
1331 index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1332 if (index != -1)
1334 WORD class1, class2;
1335 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1336 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1337 if (class1 < class1_count && class2 < class2_count)
1339 const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1340 int next = 1;
1342 TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1344 apply_pair_value( ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, ptAdjust, ptAdvance );
1345 if (ValueFormat2) next++;
1346 return glyph_index + next;
1350 else
1351 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1353 return glyph_index+1;
1356 static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1357 INT glyph_count, INT ppem, LPPOINT pt)
1359 int j;
1360 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1362 if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return;
1364 TRACE("Cursive Attachment Positioning Subtable\n");
1366 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1368 const GPOS_CursivePosFormat1 *cpf1 = (const GPOS_CursivePosFormat1 *)GPOS_get_subtable(look, j);
1369 if (GET_BE_WORD(cpf1->PosFormat) == 1)
1371 int index_exit, index_entry;
1372 WORD offset = GET_BE_WORD( cpf1->Coverage );
1373 index_exit = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index]);
1374 if (index_exit != -1 && cpf1->EntryExitRecord[index_exit].ExitAnchor!= 0)
1376 index_entry = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index+write_dir]);
1377 if (index_entry != -1 && cpf1->EntryExitRecord[index_entry].EntryAnchor != 0)
1379 POINT exit_pt, entry_pt;
1380 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_exit].ExitAnchor);
1381 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
1382 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
1383 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
1384 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);
1385 pt->x = entry_pt.x - exit_pt.x;
1386 pt->y = entry_pt.y - exit_pt.y;
1387 return;
1391 else
1392 FIXME("Cursive Attachment Positioning: Format %i Unhandled\n",GET_BE_WORD(cpf1->PosFormat));
1394 return;
1397 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)
1399 int j;
1400 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1401 void *glyph_class_table = NULL;
1402 int rc = -1;
1404 if (psc->GDEF_Table)
1406 const GDEF_Header *header = psc->GDEF_Table;
1407 WORD offset = GET_BE_WORD( header->GlyphClassDef );
1408 if (offset)
1409 glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
1412 TRACE("MarkToBase Attachment Positioning Subtable\n");
1414 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1416 const GPOS_MarkBasePosFormat1 *mbpf1 = (const GPOS_MarkBasePosFormat1 *)GPOS_get_subtable(look, j);
1417 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1419 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1420 int mark_index;
1421 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1422 if (mark_index != -1)
1424 int base_index;
1425 int base_glyph = glyph_index - write_dir;
1427 if (glyph_class_table)
1429 while (OT_get_glyph_class(glyph_class_table, glyphs[base_glyph]) == MarkGlyph && base_glyph > 0 && base_glyph < glyph_count)
1430 base_glyph -= write_dir;
1433 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1434 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[base_glyph]);
1435 if (base_index != -1)
1437 const GPOS_MarkArray *ma;
1438 const GPOS_MarkRecord *mr;
1439 const GPOS_BaseArray *ba;
1440 const GPOS_BaseRecord *br;
1441 int mark_class;
1442 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1443 int baserecord_size;
1444 POINT base_pt;
1445 POINT mark_pt;
1446 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[base_glyph], base_index);
1447 offset = GET_BE_WORD(mbpf1->MarkArray);
1448 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1449 if (mark_index > GET_BE_WORD(ma->MarkCount))
1451 ERR("Mark index exeeded mark count\n");
1452 return -1;
1454 mr = &ma->MarkRecord[mark_index];
1455 mark_class = GET_BE_WORD(mr->Class);
1456 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1457 offset = GET_BE_WORD(mbpf1->BaseArray);
1458 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1459 baserecord_size = class_count * sizeof(WORD);
1460 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1461 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1462 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1463 offset = GET_BE_WORD(mr->MarkAnchor);
1464 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1465 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1466 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1467 pt->x += base_pt.x - mark_pt.x;
1468 pt->y += base_pt.y - mark_pt.y;
1469 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1470 rc = base_glyph;
1474 else
1475 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1477 return rc;
1480 static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1481 INT glyph_count, INT ppem, LPPOINT pt)
1483 int j;
1484 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1486 TRACE("MarkToLigature Attachment Positioning Subtable\n");
1488 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1490 const GPOS_MarkLigPosFormat1 *mlpf1 = (const GPOS_MarkLigPosFormat1 *)GPOS_get_subtable(look, j);
1491 if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1493 int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1494 int mark_index;
1495 mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
1496 if (mark_index != -1)
1498 int ligature_index;
1499 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
1500 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
1501 if (ligature_index != -1)
1503 const GPOS_MarkArray *ma;
1504 const GPOS_MarkRecord *mr;
1506 const GPOS_LigatureArray *la;
1507 const GPOS_LigatureAttach *lt;
1508 int mark_class;
1509 int class_count = GET_BE_WORD(mlpf1->ClassCount);
1510 int component_count;
1511 int component_size;
1512 int i;
1513 POINT ligature_pt;
1514 POINT mark_pt;
1516 TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
1517 offset = GET_BE_WORD(mlpf1->MarkArray);
1518 ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
1519 if (mark_index > GET_BE_WORD(ma->MarkCount))
1521 ERR("Mark index exeeded mark count\n");
1522 return;
1524 mr = &ma->MarkRecord[mark_index];
1525 mark_class = GET_BE_WORD(mr->Class);
1526 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1527 offset = GET_BE_WORD(mlpf1->LigatureArray);
1528 la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
1529 if (ligature_index > GET_BE_WORD(la->LigatureCount))
1531 ERR("Ligature index exeeded ligature count\n");
1532 return;
1534 offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
1535 lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
1537 component_count = GET_BE_WORD(lt->ComponentCount);
1538 component_size = class_count * sizeof(WORD);
1539 offset = 0;
1540 for (i = 0; i < component_count && !offset; i++)
1542 int k;
1543 const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
1544 for (k = 0; k < class_count && !offset; k++)
1545 offset = GET_BE_WORD(cr->LigatureAnchor[k]);
1546 cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
1548 if (!offset)
1550 ERR("Failed to find avalible ligature connection point\n");
1551 return;
1554 GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
1555 offset = GET_BE_WORD(mr->MarkAnchor);
1556 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1557 TRACE("Offset on ligature is %i,%i design units\n",ligature_pt.x,ligature_pt.y);
1558 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1559 pt->x += ligature_pt.x - mark_pt.x;
1560 pt->y += ligature_pt.y - mark_pt.y;
1561 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1565 else
1566 FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
1570 static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1571 INT glyph_count, INT ppem, LPPOINT pt)
1573 int j;
1574 BOOL rc = FALSE;
1575 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1577 TRACE("MarkToMark Attachment Positioning Subtable\n");
1579 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1581 const GPOS_MarkMarkPosFormat1 *mmpf1 = (const GPOS_MarkMarkPosFormat1 *)GPOS_get_subtable(look, j);
1582 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
1584 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
1585 int mark_index;
1586 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
1587 if (mark_index != -1)
1589 int mark2_index;
1590 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
1591 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
1592 if (mark2_index != -1)
1594 const GPOS_MarkArray *ma;
1595 const GPOS_MarkRecord *mr;
1596 const GPOS_Mark2Array *m2a;
1597 const GPOS_Mark2Record *m2r;
1598 int mark_class;
1599 int class_count = GET_BE_WORD(mmpf1->ClassCount);
1600 int mark2record_size;
1601 POINT mark2_pt;
1602 POINT mark_pt;
1603 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
1604 offset = GET_BE_WORD(mmpf1->Mark1Array);
1605 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
1606 if (mark_index > GET_BE_WORD(ma->MarkCount))
1608 ERR("Mark index exceeded mark count\n");
1609 return FALSE;
1611 mr = &ma->MarkRecord[mark_index];
1612 mark_class = GET_BE_WORD(mr->Class);
1613 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1614 offset = GET_BE_WORD(mmpf1->Mark2Array);
1615 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
1616 mark2record_size = class_count * sizeof(WORD);
1617 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
1618 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
1619 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
1620 offset = GET_BE_WORD(mr->MarkAnchor);
1621 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1622 TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
1623 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1624 pt->x += mark2_pt.x - mark_pt.x;
1625 pt->y += mark2_pt.y - mark_pt.y;
1626 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1627 rc = TRUE;
1631 else
1632 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
1634 return rc;
1637 static INT GPOS_apply_ChainContextPos(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1638 const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index,
1639 INT glyph_count, INT ppem, GOFFSET *pGoffset)
1641 int j;
1642 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1644 TRACE("Chaining Contextual Positioning Subtable\n");
1646 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1648 int offset;
1649 const GPOS_ChainContextPosFormat3_1 *ccpf3 = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
1650 int dirLookahead = write_dir;
1651 int dirBacktrack = -1 * write_dir;
1653 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1655 static int once;
1656 if (!once++)
1657 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1658 continue;
1660 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1662 static int once;
1663 if (!once++)
1664 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1665 continue;
1667 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1669 int k;
1670 int indexGlyphs;
1671 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1672 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1673 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1675 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1677 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1679 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1680 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1681 break;
1683 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1684 continue;
1685 TRACE("Matched Backtrack\n");
1687 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
1688 FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
1690 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1691 for (k = 0; k < indexGlyphs; k++)
1693 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1694 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1695 break;
1697 if (k != indexGlyphs)
1698 continue;
1699 TRACE("Matched IndexGlyphs\n");
1701 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
1702 FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
1704 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1706 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1707 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1708 break;
1710 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1711 continue;
1712 TRACE("Matched LookAhead\n");
1714 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
1715 FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
1717 if (GET_BE_WORD(ccpf3_4->PosCount))
1719 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1721 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1722 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1724 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1725 GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, glyph_count, pGoffset);
1727 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1729 else return glyph_index + 1;
1731 else
1732 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1734 return glyph_index + 1;
1737 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)
1739 int offset;
1740 const OT_LookupTable *look;
1741 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1742 WORD type;
1744 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1745 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1746 type = GET_BE_WORD(look->LookupType);
1747 TRACE("type %i, flag %x, subtables %i\n",type,GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1748 if (type == 9)
1750 if (GET_BE_WORD(look->SubTableCount))
1752 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
1753 if (GET_BE_WORD(ext->PosFormat) == 1)
1755 type = GET_BE_WORD(ext->ExtensionLookupType);
1756 TRACE("extension type %i\n",type);
1758 else
1760 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1763 else
1765 WARN("lookup type is Extension Positioning but no extension subtable exists\n");
1768 switch (type)
1770 case 1:
1772 double devX, devY;
1773 POINT adjust = {0,0};
1774 POINT advance = {0,0};
1775 GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
1776 if (adjust.x || adjust.y)
1778 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1779 pGoffset[glyph_index].du += round(devX);
1780 pGoffset[glyph_index].dv += round(devY);
1782 if (advance.x || advance.y)
1784 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1785 piAdvance[glyph_index] += round(devX);
1786 if (advance.y)
1787 FIXME("Unhandled adjustment to Y advancement\n");
1789 break;
1791 case 2:
1793 POINT advance[2]= {{0,0},{0,0}};
1794 POINT adjust[2]= {{0,0},{0,0}};
1795 double devX, devY;
1796 int index;
1797 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1798 int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
1800 index = GPOS_apply_PairAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, adjust, advance);
1801 if (adjust[0].x || adjust[0].y)
1803 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1804 pGoffset[glyph_index].du += round(devX) * offset_sign;
1805 pGoffset[glyph_index].dv += round(devY);
1807 if (advance[0].x || advance[0].y)
1809 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1810 piAdvance[glyph_index] += round(devX);
1812 if (adjust[1].x || adjust[1].y)
1814 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1815 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
1816 pGoffset[glyph_index + write_dir].dv += round(devY);
1818 if (advance[1].x || advance[1].y)
1820 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1821 piAdvance[glyph_index + write_dir] += round(devX);
1823 return index;
1825 case 3:
1827 POINT desU = {0,0};
1828 double devX, devY;
1829 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1831 GPOS_apply_CursiveAttachment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1832 if (desU.x || desU.y)
1834 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1835 /* Windows does not appear to apply X offsets here */
1836 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index+write_dir].dv;
1838 break;
1840 case 4:
1842 double devX, devY;
1843 POINT desU = {0,0};
1844 int base_index = GPOS_apply_MarkToBase(psc, look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1845 if (base_index != -1)
1847 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1848 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[base_index];
1849 else
1851 if (analysis->fLogicalOrder) devX *= -1;
1852 pGoffset[glyph_index].du = round(devX);
1854 pGoffset[glyph_index].dv = round(devY);
1856 break;
1858 case 5:
1860 double devX, devY;
1861 POINT desU = {0,0};
1862 GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1863 if (desU.x || desU.y)
1865 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1866 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
1867 pGoffset[glyph_index].dv = round(devY);
1869 break;
1871 case 6:
1873 double devX, devY;
1874 POINT desU = {0,0};
1875 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1876 if (GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU))
1878 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1879 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
1880 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
1881 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
1883 break;
1885 case 8:
1887 return GPOS_apply_ChainContextPos(psc, lpotm, lplogfont, analysis, piAdvance, lookup, look, glyphs, glyph_index, glyph_count, ppem, pGoffset);
1889 default:
1890 FIXME("We do not handle SubType %i\n",type);
1892 return glyph_index+1;
1895 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)
1897 const GPOS_Header *header = (const GPOS_Header *)psc->GPOS_Table;
1898 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1900 return GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookup_index, glyphs, glyph_index, glyph_count, pGoffset);
1903 static void GSUB_initialize_script_cache(ScriptCache *psc)
1905 int i;
1907 if (psc->GSUB_Table)
1909 const OT_ScriptList *script;
1910 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1911 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1912 psc->script_count = GET_BE_WORD(script->ScriptCount);
1913 TRACE("initializing %i scripts in this font\n",psc->script_count);
1914 if (psc->script_count)
1916 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1917 for (i = 0; i < psc->script_count; i++)
1919 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1920 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]);
1921 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1927 static void GPOS_expand_script_cache(ScriptCache *psc)
1929 int i, count;
1930 const OT_ScriptList *script;
1931 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1933 if (!header)
1934 return;
1936 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1937 count = GET_BE_WORD(script->ScriptCount);
1939 if (!count)
1940 return;
1942 if (!psc->script_count)
1944 psc->script_count = count;
1945 TRACE("initializing %i scripts in this font\n",psc->script_count);
1946 if (psc->script_count)
1948 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1949 for (i = 0; i < psc->script_count; i++)
1951 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1952 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]);
1953 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1957 else
1959 for (i = 0; i < count; i++)
1961 int j;
1962 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1963 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]);
1964 for (j = 0; j < psc->script_count; j++)
1966 if (psc->scripts[j].tag == tag)
1968 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1969 break;
1972 if (j == psc->script_count)
1974 psc->script_count++;
1975 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1976 psc->scripts[j].tag = tag;
1977 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1983 static void _initialize_script_cache(ScriptCache *psc)
1985 if (!psc->scripts_initialized)
1987 GSUB_initialize_script_cache(psc);
1988 GPOS_expand_script_cache(psc);
1989 psc->scripts_initialized = TRUE;
1993 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1995 int i;
1996 HRESULT rc = S_OK;
1998 _initialize_script_cache(psc);
2000 *pcTags = psc->script_count;
2002 if (!searchingFor && cMaxTags < *pcTags)
2003 rc = E_OUTOFMEMORY;
2004 else if (searchingFor)
2005 rc = USP_E_SCRIPT_NOT_IN_FONT;
2007 for (i = 0; i < psc->script_count; i++)
2009 if (i < cMaxTags)
2010 pScriptTags[i] = psc->scripts[i].tag;
2012 if (searchingFor)
2014 if (searchingFor == psc->scripts[i].tag)
2016 pScriptTags[0] = psc->scripts[i].tag;
2017 *pcTags = 1;
2018 rc = S_OK;
2019 break;
2023 return rc;
2026 static void GSUB_initialize_language_cache(LoadedScript *script)
2028 int i;
2030 if (script->gsub_table)
2032 DWORD offset;
2033 const OT_Script* table = script->gsub_table;
2034 script->language_count = GET_BE_WORD(table->LangSysCount);
2035 offset = GET_BE_WORD(table->DefaultLangSys);
2036 if (offset)
2038 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
2039 script->default_language.gsub_table = (const BYTE*)table + offset;
2042 if (script->language_count)
2044 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
2046 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2048 for (i = 0; i < script->language_count; i++)
2050 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2051 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]);
2052 script->languages[i].gsub_table = ((const BYTE*)table + offset);
2058 static void GPOS_expand_language_cache(LoadedScript *script)
2060 int count;
2061 const OT_Script* table = script->gpos_table;
2062 DWORD offset;
2064 if (!table)
2065 return;
2067 offset = GET_BE_WORD(table->DefaultLangSys);
2068 if (offset)
2069 script->default_language.gpos_table = (const BYTE*)table + offset;
2071 count = GET_BE_WORD(table->LangSysCount);
2073 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
2075 if (!count)
2076 return;
2078 if (!script->language_count)
2080 int i;
2081 script->language_count = count;
2083 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2085 for (i = 0; i < script->language_count; i++)
2087 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2088 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]);
2089 script->languages[i].gpos_table = ((const BYTE*)table + offset);
2092 else if (count)
2094 int i,j;
2095 for (i = 0; i < count; i++)
2097 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2098 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]);
2100 for (j = 0; j < script->language_count; j++)
2102 if (script->languages[j].tag == tag)
2104 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2105 break;
2108 if (j == script->language_count)
2110 script->language_count++;
2111 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
2112 script->languages[j].tag = tag;
2113 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2119 static void _initialize_language_cache(LoadedScript *script)
2121 if (!script->languages_initialized)
2123 GSUB_initialize_language_cache(script);
2124 GPOS_expand_language_cache(script);
2125 script->languages_initialized = TRUE;
2129 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2131 int i;
2132 HRESULT rc = S_OK;
2133 LoadedScript *script = NULL;
2135 _initialize_script_cache(psc);
2137 for (i = 0; i < psc->script_count; i++)
2139 if (psc->scripts[i].tag == script_tag)
2141 script = &psc->scripts[i];
2142 break;
2146 if (!script)
2147 return E_INVALIDARG;
2149 _initialize_language_cache(script);
2151 if (!searchingFor && cMaxTags < script->language_count)
2152 rc = E_OUTOFMEMORY;
2153 else if (searchingFor)
2154 rc = E_INVALIDARG;
2156 *pcTags = script->language_count;
2158 for (i = 0; i < script->language_count; i++)
2160 if (i < cMaxTags)
2161 pLanguageTags[i] = script->languages[i].tag;
2163 if (searchingFor)
2165 if (searchingFor == script->languages[i].tag)
2167 pLanguageTags[0] = script->languages[i].tag;
2168 *pcTags = 1;
2169 rc = S_OK;
2170 break;
2175 if (script->default_language.gsub_table)
2177 if (i < cMaxTags)
2178 pLanguageTags[i] = script->default_language.tag;
2180 if (searchingFor && FAILED(rc))
2182 pLanguageTags[0] = script->default_language.tag;
2184 i++;
2185 *pcTags = (*pcTags) + 1;
2188 return rc;
2192 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
2194 int i;
2196 if (language->gsub_table)
2198 const OT_LangSys *lang = language->gsub_table;
2199 const GSUB_Header *header = (const GSUB_Header *)table;
2200 const OT_FeatureList *feature_list;
2202 language->feature_count = GET_BE_WORD(lang->FeatureCount);
2203 TRACE("%i features\n",language->feature_count);
2205 if (language->feature_count)
2207 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2209 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2211 for (i = 0; i < language->feature_count; i++)
2213 const OT_Feature *feature;
2214 int j;
2215 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2217 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]);
2218 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2219 feature = (const OT_Feature*)language->features[i].feature;
2220 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2221 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2222 for (j = 0; j < language->features[i].lookup_count; j++)
2223 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2224 language->features[i].tableType = FEATURE_GSUB_TABLE;
2230 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
2232 int i, count;
2233 const OT_LangSys *lang = language->gpos_table;
2234 const GPOS_Header *header = (const GPOS_Header *)table;
2235 const OT_FeatureList *feature_list;
2237 if (!lang)
2238 return;
2240 count = GET_BE_WORD(lang->FeatureCount);
2241 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2243 TRACE("%i features\n",count);
2245 if (!count)
2246 return;
2248 if (!language->feature_count)
2250 language->feature_count = count;
2252 if (language->feature_count)
2254 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2256 for (i = 0; i < language->feature_count; i++)
2258 const OT_Feature *feature;
2259 int j;
2260 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2262 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]);
2263 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2264 feature = (const OT_Feature*)language->features[i].feature;
2265 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2266 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2267 for (j = 0; j < language->features[i].lookup_count; j++)
2268 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2269 language->features[i].tableType = FEATURE_GPOS_TABLE;
2273 else
2275 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
2277 for (i = 0; i < count; i++)
2279 const OT_Feature *feature;
2280 int j;
2281 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2282 int idx = language->feature_count + i;
2284 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]);
2285 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2286 feature = (const OT_Feature*)language->features[idx].feature;
2287 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
2288 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
2289 for (j = 0; j < language->features[idx].lookup_count; j++)
2290 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2291 language->features[idx].tableType = FEATURE_GPOS_TABLE;
2293 language->feature_count += count;
2297 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2299 if (!language->features_initialized)
2301 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
2302 GPOS_expand_feature_cache(psc->GPOS_Table, language);
2303 language->features_initialized = TRUE;
2307 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)
2309 int i;
2310 HRESULT rc = S_OK;
2311 LoadedScript *script = NULL;
2312 LoadedLanguage *language = NULL;
2314 _initialize_script_cache(psc);
2316 for (i = 0; i < psc->script_count; i++)
2318 if (psc->scripts[i].tag == script_tag)
2320 script = &psc->scripts[i];
2321 break;
2325 if (!script)
2327 *pcTags = 0;
2328 if (!filtered)
2329 return S_OK;
2330 else
2331 return E_INVALIDARG;
2334 _initialize_language_cache(script);
2336 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2337 language = &script->default_language;
2338 else
2340 for (i = 0; i < script->language_count; i++)
2342 if (script->languages[i].tag == language_tag)
2344 language = &script->languages[i];
2345 break;
2350 if (!language)
2352 *pcTags = 0;
2353 return S_OK;
2356 _initialize_feature_cache(psc, language);
2358 if (tableType)
2360 *pcTags = 0;
2361 for (i = 0; i < language->feature_count; i++)
2362 if (language->features[i].tableType == tableType)
2363 *pcTags = (*pcTags)+1;
2365 else
2366 *pcTags = language->feature_count;
2368 if (!searchingFor && cMaxTags < *pcTags)
2369 rc = E_OUTOFMEMORY;
2370 else if (searchingFor)
2371 rc = E_INVALIDARG;
2373 for (i = 0; i < language->feature_count; i++)
2375 if (i < cMaxTags)
2377 if (!tableType || language->features[i].tableType == tableType)
2378 pFeatureTags[i] = language->features[i].tag;
2381 if (searchingFor)
2383 if ((searchingFor == language->features[i].tag) &&
2384 (!tableType || language->features[i].tableType == tableType))
2386 pFeatureTags[0] = language->features[i].tag;
2387 *pcTags = 1;
2388 if (feature)
2389 *feature = &language->features[i];
2390 rc = S_OK;
2391 break;
2395 return rc;