usp10: Implement GPOS Anchor formats.
[wine/multimedia.git] / dlls / usp10 / opentype.c
blob8434789ade021107081e5018ab0a598e40c911de
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 /* These are all structures needed for the cmap format 12 table */
47 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
49 typedef struct {
50 WORD platformID;
51 WORD encodingID;
52 DWORD offset;
53 } CMAP_EncodingRecord;
55 typedef struct {
56 WORD version;
57 WORD numTables;
58 CMAP_EncodingRecord tables[1];
59 } CMAP_Header;
61 typedef struct {
62 DWORD startCharCode;
63 DWORD endCharCode;
64 DWORD startGlyphID;
65 } CMAP_SegmentedCoverage_group;
67 typedef struct {
68 WORD format;
69 WORD reserved;
70 DWORD length;
71 DWORD language;
72 DWORD nGroups;
73 CMAP_SegmentedCoverage_group groups[1];
74 } CMAP_SegmentedCoverage;
76 /* These are all structures needed for the GDEF table */
77 #define GDEF_TAG MS_MAKE_TAG('G', 'D', 'E', 'F')
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 } GDEF_ClassDefFormat1;
96 typedef struct {
97 WORD Start;
98 WORD End;
99 WORD Class;
100 } GDEF_ClassRangeRecord;
102 typedef struct {
103 WORD ClassFormat;
104 WORD ClassRangeCount;
105 GDEF_ClassRangeRecord ClassRangeRecord[1];
106 } GDEF_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 /**********
318 * CMAP
319 **********/
321 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
323 CMAP_Header *CMAP_Table = NULL;
324 int length;
325 int i;
327 if (!psc->CMAP_Table)
329 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
330 if (length != GDI_ERROR)
332 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
333 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
334 TRACE("Loaded cmap table of %i bytes\n",length);
336 else
337 return NULL;
340 CMAP_Table = psc->CMAP_Table;
342 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
344 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
345 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
347 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
348 if (GET_BE_WORD(format->format) == 12)
349 return format;
352 return NULL;
355 static int compare_group(const void *a, const void* b)
357 const DWORD *chr = a;
358 const CMAP_SegmentedCoverage_group *group = b;
360 if (*chr < GET_BE_DWORD(group->startCharCode))
361 return -1;
362 if (*chr > GET_BE_DWORD(group->endCharCode))
363 return 1;
364 return 0;
367 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
369 /* BMP: use gdi32 for ease */
370 if (utf32c < 0x10000)
372 WCHAR ch = utf32c;
373 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
376 if (!psc->CMAP_format12_Table)
377 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
379 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
380 *pgi = 0xffff;
381 else
382 *pgi = 0;
384 if (psc->CMAP_format12_Table)
386 CMAP_SegmentedCoverage *format = NULL;
387 CMAP_SegmentedCoverage_group *group = NULL;
389 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
391 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
392 sizeof(CMAP_SegmentedCoverage_group), compare_group);
394 if (group)
396 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
397 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
398 return 0;
401 return 0;
404 /**********
405 * GDEF
406 **********/
408 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
410 int offset;
411 WORD class = 0;
412 const GDEF_ClassDefFormat1 *cf1;
414 if (!header)
415 return 0;
417 offset = GET_BE_WORD(header->GlyphClassDef);
418 if (!offset)
419 return 0;
421 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
422 if (GET_BE_WORD(cf1->ClassFormat) == 1)
424 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
426 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
427 if (index < GET_BE_WORD(cf1->GlyphCount))
428 class = GET_BE_WORD(cf1->ClassValueArray[index]);
431 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
433 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
434 int i, top;
435 top = GET_BE_WORD(cf2->ClassRangeCount);
436 for (i = 0; i < top; i++)
438 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
439 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
441 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
442 break;
446 else
447 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
449 return class;
452 static VOID *load_gdef_table(HDC hdc)
454 VOID* GDEF_Table = NULL;
455 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
456 if (length != GDI_ERROR)
458 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
459 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
460 TRACE("Loaded GDEF table of %i bytes\n",length);
462 return GDEF_Table;
465 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
467 int i;
469 if (!psc->GDEF_Table)
470 psc->GDEF_Table = load_gdef_table(hdc);
472 for (i = 0; i < cGlyphs; i++)
474 WORD class;
475 int char_count = 0;
476 int k;
478 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
479 if (k >= 0)
481 for (; k < cChars && pwLogClust[k] == i; k++)
482 char_count++;
485 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
487 switch (class)
489 case 0:
490 case BaseGlyph:
491 pGlyphProp[i].sva.fClusterStart = 1;
492 pGlyphProp[i].sva.fDiacritic = 0;
493 pGlyphProp[i].sva.fZeroWidth = 0;
494 break;
495 case LigatureGlyph:
496 pGlyphProp[i].sva.fClusterStart = 1;
497 pGlyphProp[i].sva.fDiacritic = 0;
498 pGlyphProp[i].sva.fZeroWidth = 0;
499 break;
500 case MarkGlyph:
501 pGlyphProp[i].sva.fClusterStart = 0;
502 pGlyphProp[i].sva.fDiacritic = 1;
503 pGlyphProp[i].sva.fZeroWidth = 1;
504 break;
505 case ComponentGlyph:
506 pGlyphProp[i].sva.fClusterStart = 0;
507 pGlyphProp[i].sva.fDiacritic = 0;
508 pGlyphProp[i].sva.fZeroWidth = 0;
509 break;
510 default:
511 ERR("Unknown glyph class %i\n",class);
512 pGlyphProp[i].sva.fClusterStart = 1;
513 pGlyphProp[i].sva.fDiacritic = 0;
514 pGlyphProp[i].sva.fZeroWidth = 0;
517 if (char_count == 0)
518 pGlyphProp[i].sva.fClusterStart = 0;
522 /**********
523 * GSUB
524 **********/
525 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
527 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
529 const OT_CoverageFormat1* cf1;
531 cf1 = table;
533 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
535 int count = GET_BE_WORD(cf1->GlyphCount);
536 int i;
537 TRACE("Coverage Format 1, %i glyphs\n",count);
538 for (i = 0; i < count; i++)
539 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
540 return i;
541 return -1;
543 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
545 const OT_CoverageFormat2* cf2;
546 int i;
547 int count;
548 cf2 = (const OT_CoverageFormat2*)cf1;
550 count = GET_BE_WORD(cf2->RangeCount);
551 TRACE("Coverage Format 2, %i ranges\n",count);
552 for (i = 0; i < count; i++)
554 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
555 return -1;
556 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
557 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
559 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
560 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
563 return -1;
565 else
566 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
568 return -1;
571 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
573 int j;
574 TRACE("Single Substitution Subtable\n");
576 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
578 int offset;
579 const GSUB_SingleSubstFormat1 *ssf1;
580 offset = GET_BE_WORD(look->SubTable[j]);
581 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
582 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
584 int offset = GET_BE_WORD(ssf1->Coverage);
585 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
586 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
588 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
589 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
590 TRACE(" 0x%x\n",glyphs[glyph_index]);
591 return glyph_index + write_dir;
594 else
596 const GSUB_SingleSubstFormat2 *ssf2;
597 INT index;
598 INT offset;
600 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
601 offset = GET_BE_WORD(ssf1->Coverage);
602 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
603 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
604 TRACE(" Coverage index %i\n",index);
605 if (index != -1)
607 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
608 return GSUB_E_NOGLYPH;
610 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
611 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
612 TRACE("0x%x\n",glyphs[glyph_index]);
613 return glyph_index + write_dir;
617 return GSUB_E_NOGLYPH;
620 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
622 int j;
623 TRACE("Multiple Substitution Subtable\n");
625 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
627 int offset, index;
628 const GSUB_MultipleSubstFormat1 *msf1;
629 offset = GET_BE_WORD(look->SubTable[j]);
630 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
632 offset = GET_BE_WORD(msf1->Coverage);
633 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
634 if (index != -1)
636 const GSUB_Sequence *seq;
637 int sub_count;
638 int j;
639 offset = GET_BE_WORD(msf1->Sequence[index]);
640 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
641 sub_count = GET_BE_WORD(seq->GlyphCount);
642 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
644 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
645 glyphs[j] =glyphs[j-(sub_count-1)];
647 for (j = 0; j < sub_count; j++)
648 if (write_dir < 0)
649 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
650 else
651 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
653 *glyph_count = *glyph_count + (sub_count - 1);
655 if (TRACE_ON(uniscribe))
657 for (j = 0; j < sub_count; j++)
658 TRACE(" 0x%x",glyphs[glyph_index+j]);
659 TRACE("\n");
662 return glyph_index + (sub_count * write_dir);
665 return GSUB_E_NOGLYPH;
668 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
670 int j;
671 TRACE("Alternate Substitution Subtable\n");
673 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
675 int offset;
676 const GSUB_AlternateSubstFormat1 *asf1;
677 INT index;
679 offset = GET_BE_WORD(look->SubTable[j]);
680 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
681 offset = GET_BE_WORD(asf1->Coverage);
683 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
684 if (index != -1)
686 const GSUB_AlternateSet *as;
687 offset = GET_BE_WORD(asf1->AlternateSet[index]);
688 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
689 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
690 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
691 return GSUB_E_NOGLYPH;
693 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
694 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
695 TRACE(" 0x%x\n",glyphs[glyph_index]);
696 return glyph_index + write_dir;
699 return GSUB_E_NOGLYPH;
702 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
704 int j;
706 TRACE("Ligature Substitution Subtable\n");
707 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
709 const GSUB_LigatureSubstFormat1 *lsf1;
710 int offset,index;
712 offset = GET_BE_WORD(look->SubTable[j]);
713 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
714 offset = GET_BE_WORD(lsf1->Coverage);
715 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
716 TRACE(" Coverage index %i\n",index);
717 if (index != -1)
719 const GSUB_LigatureSet *ls;
720 int k, count;
722 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
723 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
724 count = GET_BE_WORD(ls->LigatureCount);
725 TRACE(" LigatureSet has %i members\n",count);
726 for (k = 0; k < count; k++)
728 const GSUB_Ligature *lig;
729 int CompCount,l,CompIndex;
731 offset = GET_BE_WORD(ls->Ligature[k]);
732 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
733 CompCount = GET_BE_WORD(lig->CompCount) - 1;
734 CompIndex = glyph_index+write_dir;
735 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
737 int CompGlyph;
738 CompGlyph = GET_BE_WORD(lig->Component[l]);
739 if (CompGlyph != glyphs[CompIndex])
740 break;
741 CompIndex += write_dir;
743 if (l == CompCount)
745 int replaceIdx = glyph_index;
746 if (write_dir < 0)
747 replaceIdx = glyph_index - CompCount;
749 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
750 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
751 TRACE("0x%x\n",glyphs[replaceIdx]);
752 if (CompCount > 0)
754 int j;
755 for (j = replaceIdx + 1; j < *glyph_count; j++)
756 glyphs[j] =glyphs[j+CompCount];
757 *glyph_count = *glyph_count - CompCount;
759 return replaceIdx + write_dir;
764 return GSUB_E_NOGLYPH;
767 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
769 int j;
770 BOOL done = FALSE;
772 TRACE("Chaining Contextual Substitution Subtable\n");
773 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
775 const GSUB_ChainContextSubstFormat1 *ccsf1;
776 int offset;
777 int dirLookahead = write_dir;
778 int dirBacktrack = -1 * write_dir;
780 offset = GET_BE_WORD(look->SubTable[j]);
781 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
782 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
784 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
785 continue;
787 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
789 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
790 continue;
792 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
794 int k;
795 int indexGlyphs;
796 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
797 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
798 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
799 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
800 int newIndex = glyph_index;
802 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
804 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
806 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
808 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
809 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
810 break;
812 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
813 continue;
814 TRACE("Matched Backtrack\n");
816 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
818 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
819 for (k = 0; k < indexGlyphs; k++)
821 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
822 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
823 break;
825 if (k != indexGlyphs)
826 continue;
827 TRACE("Matched IndexGlyphs\n");
829 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
831 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
833 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
834 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
835 break;
837 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
838 continue;
839 TRACE("Matched LookAhead\n");
841 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
843 if (GET_BE_WORD(ccsf3_4->SubstCount))
845 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
847 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
848 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
850 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
851 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
852 if (newIndex == -1)
854 ERR("Chain failed to generate a glyph\n");
855 continue;
858 return newIndex;
860 else return GSUB_E_NOGLYPH;
863 return -1;
866 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
868 int offset;
869 const OT_LookupTable *look;
871 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
872 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
873 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
874 switch(GET_BE_WORD(look->LookupType))
876 case 1:
877 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
878 case 2:
879 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
880 case 3:
881 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
882 case 4:
883 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
884 case 6:
885 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
886 default:
887 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
889 return GSUB_E_NOGLYPH;
892 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
894 const GSUB_Header *header = (const GSUB_Header *)table;
895 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
897 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
900 /**********
901 * GPOS
902 **********/
903 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
905 static const WORD mask[3] = {3,0xf,0xff};
906 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
908 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
909 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
910 int value;
911 TRACE("device table, format %i, index %i\n",format, index);
912 index = index << format;
913 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
914 TRACE("offset %i, value %i\n",index, value);
915 if (value > mask[format-1]/2)
916 value = -1 * ((mask[format-1]+1) - value);
917 return value;
919 return 0;
922 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
924 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
926 switch (GET_BE_WORD(anchor1->AnchorFormat))
928 case 1:
930 TRACE("Anchor Format 1\n");
931 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
932 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
933 break;
935 case 2:
937 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
938 TRACE("Anchor Format 2\n");
939 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
940 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
941 break;
943 case 3:
945 int offset;
946 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
947 TRACE("Anchor Format 3\n");
948 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
949 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
950 offset = GET_BE_WORD(anchor3->XDeviceTable);
951 TRACE("ppem %i\n",ppem);
952 if (offset)
954 const OT_DeviceTable* DeviceTableX = NULL;
955 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
956 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
958 offset = GET_BE_WORD(anchor3->YDeviceTable);
959 if (offset)
961 const OT_DeviceTable* DeviceTableY = NULL;
962 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
963 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
965 break;
967 default:
968 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
969 pt->x = 0;
970 pt->y = 0;
974 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)
976 int offset;
977 const OT_LookupTable *look;
979 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
980 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
981 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
982 switch(GET_BE_WORD(look->LookupType))
984 default:
985 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
987 return glyph_index+1;
990 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)
992 const GPOS_Header *header = (const GPOS_Header *)table;
993 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
995 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
998 static void GSUB_initialize_script_cache(ScriptCache *psc)
1000 int i;
1002 if (psc->GSUB_Table)
1004 const OT_ScriptList *script;
1005 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1006 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1007 psc->script_count = GET_BE_WORD(script->ScriptCount);
1008 TRACE("initializing %i scripts in this font\n",psc->script_count);
1009 if (psc->script_count)
1011 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1012 for (i = 0; i < psc->script_count; i++)
1014 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1015 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]);
1016 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1022 static void GPOS_expand_script_cache(ScriptCache *psc)
1024 int i, count;
1025 const OT_ScriptList *script;
1026 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1028 if (!header)
1029 return;
1031 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1032 count = GET_BE_WORD(script->ScriptCount);
1034 if (!psc->script_count)
1036 psc->script_count = count;
1037 TRACE("initializing %i scripts in this font\n",psc->script_count);
1038 if (psc->script_count)
1040 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1041 for (i = 0; i < psc->script_count; i++)
1043 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1044 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]);
1045 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1049 else
1051 for (i = 0; i < count; i++)
1053 int j;
1054 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1055 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]);
1056 for (j = 0; j < psc->script_count; j++)
1058 if (psc->scripts[j].tag == tag)
1060 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1061 break;
1064 if (j == psc->script_count)
1066 psc->script_count++;
1067 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1068 psc->scripts[j].tag = tag;
1069 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1075 static void _initialize_script_cache(ScriptCache *psc)
1077 if (!psc->script_count)
1079 GSUB_initialize_script_cache(psc);
1080 GPOS_expand_script_cache(psc);
1084 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1086 int i;
1087 HRESULT rc = S_OK;
1089 _initialize_script_cache(psc);
1091 *pcTags = psc->script_count;
1093 if (!searchingFor && cMaxTags < *pcTags)
1094 rc = E_OUTOFMEMORY;
1095 else if (searchingFor)
1096 rc = USP_E_SCRIPT_NOT_IN_FONT;
1098 for (i = 0; i < psc->script_count; i++)
1100 if (i < cMaxTags)
1101 pScriptTags[i] = psc->scripts[i].tag;
1103 if (searchingFor)
1105 if (searchingFor == psc->scripts[i].tag)
1107 pScriptTags[0] = psc->scripts[i].tag;
1108 *pcTags = 1;
1109 rc = S_OK;
1110 break;
1114 return rc;
1117 static void GSUB_initialize_language_cache(LoadedScript *script)
1119 int i;
1121 if (script->gsub_table)
1123 DWORD offset;
1124 const OT_Script* table = script->gsub_table;
1125 script->language_count = GET_BE_WORD(table->LangSysCount);
1126 offset = GET_BE_WORD(table->DefaultLangSys);
1127 if (offset)
1129 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1130 script->default_language.gsub_table = (const BYTE*)table + offset;
1133 if (script->language_count)
1135 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1137 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1139 for (i = 0; i < script->language_count; i++)
1141 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1142 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]);
1143 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1149 static void GPOS_expand_language_cache(LoadedScript *script)
1151 int count;
1152 const OT_Script* table = script->gpos_table;
1153 DWORD offset;
1155 if (!table)
1156 return;
1158 offset = GET_BE_WORD(table->DefaultLangSys);
1159 if (offset)
1160 script->default_language.gpos_table = (const BYTE*)table + offset;
1162 count = GET_BE_WORD(table->LangSysCount);
1164 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1165 if (!script->language_count)
1167 int i;
1168 script->language_count = count;
1170 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1172 for (i = 0; i < script->language_count; i++)
1174 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1175 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]);
1176 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1179 else if (count)
1181 int i,j;
1182 for (i = 0; i < count; i++)
1184 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1185 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]);
1187 for (j = 0; j < script->language_count; j++)
1189 if (script->languages[j].tag == tag)
1191 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1192 break;
1195 if (j == script->language_count)
1197 script->language_count++;
1198 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1199 script->languages[j].tag = tag;
1200 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1206 static void _initialize_language_cache(LoadedScript *script)
1208 if (!script->language_count)
1210 GSUB_initialize_language_cache(script);
1211 GPOS_expand_language_cache(script);
1215 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1217 int i;
1218 HRESULT rc = S_OK;
1219 LoadedScript *script = NULL;
1221 _initialize_script_cache(psc);
1223 for (i = 0; i < psc->script_count; i++)
1225 if (psc->scripts[i].tag == script_tag)
1227 script = &psc->scripts[i];
1228 break;
1232 if (!script)
1233 return E_INVALIDARG;
1235 _initialize_language_cache(script);
1237 if (!searchingFor && cMaxTags < script->language_count)
1238 rc = E_OUTOFMEMORY;
1239 else if (searchingFor)
1240 rc = E_INVALIDARG;
1242 *pcTags = script->language_count;
1244 for (i = 0; i < script->language_count; i++)
1246 if (i < cMaxTags)
1247 pLanguageTags[i] = script->languages[i].tag;
1249 if (searchingFor)
1251 if (searchingFor == script->languages[i].tag)
1253 pLanguageTags[0] = script->languages[i].tag;
1254 *pcTags = 1;
1255 rc = S_OK;
1256 break;
1261 if (script->default_language.gsub_table)
1263 if (i < cMaxTags)
1264 pLanguageTags[i] = script->default_language.tag;
1266 if (searchingFor && FAILED(rc))
1268 pLanguageTags[0] = script->default_language.tag;
1270 i++;
1271 *pcTags = (*pcTags) + 1;
1274 return rc;
1278 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1280 int i;
1282 if (language->gsub_table)
1284 const OT_LangSys *lang = language->gsub_table;
1285 const GSUB_Header *header = (const GSUB_Header *)table;
1286 const OT_FeatureList *feature_list;
1288 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1289 TRACE("%i features\n",language->feature_count);
1291 if (language->feature_count)
1293 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1295 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1297 for (i = 0; i < language->feature_count; i++)
1299 const OT_Feature *feature;
1300 int j;
1301 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1303 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]);
1304 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1305 feature = (const OT_Feature*)language->features[i].feature;
1306 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1307 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1308 for (j = 0; j < language->features[i].lookup_count; j++)
1309 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1315 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1317 int i, count;
1318 const OT_LangSys *lang = language->gpos_table;
1319 const GPOS_Header *header = (const GPOS_Header *)table;
1320 const OT_FeatureList *feature_list;
1322 if (!lang)
1323 return;
1325 count = GET_BE_WORD(lang->FeatureCount);
1326 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1328 TRACE("%i features\n",count);
1329 if (!language->feature_count)
1331 language->feature_count = count;
1333 if (language->feature_count)
1335 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1337 for (i = 0; i < language->feature_count; i++)
1339 const OT_Feature *feature;
1340 int j;
1341 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1343 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]);
1344 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1345 feature = (const OT_Feature*)language->features[i].feature;
1346 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1347 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1348 for (j = 0; j < language->features[i].lookup_count; j++)
1349 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1353 else if (count)
1355 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1357 for (i = 0; i < count; i++)
1359 const OT_Feature *feature;
1360 int j;
1361 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1362 int idx = language->feature_count + i;
1364 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]);
1365 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1366 feature = (const OT_Feature*)language->features[idx].feature;
1367 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1368 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1369 for (j = 0; j < language->features[idx].lookup_count; j++)
1370 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1372 language->feature_count += count;
1376 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1378 if (!language->feature_count)
1380 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1381 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1385 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
1387 int i;
1388 HRESULT rc = S_OK;
1389 LoadedScript *script = NULL;
1390 LoadedLanguage *language = NULL;
1392 _initialize_script_cache(psc);
1394 for (i = 0; i < psc->script_count; i++)
1396 if (psc->scripts[i].tag == script_tag)
1398 script = &psc->scripts[i];
1399 break;
1403 if (!script)
1405 *pcTags = 0;
1406 if (!filtered)
1407 return S_OK;
1408 else
1409 return E_INVALIDARG;
1412 _initialize_language_cache(script);
1414 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1415 language = &script->default_language;
1416 else
1418 for (i = 0; i < script->language_count; i++)
1420 if (script->languages[i].tag == language_tag)
1422 language = &script->languages[i];
1423 break;
1428 if (!language)
1430 *pcTags = 0;
1431 return S_OK;
1434 _initialize_feature_cache(psc, language);
1436 *pcTags = language->feature_count;
1438 if (!searchingFor && cMaxTags < *pcTags)
1439 rc = E_OUTOFMEMORY;
1440 else if (searchingFor)
1441 rc = E_INVALIDARG;
1443 for (i = 0; i < language->feature_count; i++)
1445 if (i < cMaxTags)
1446 pFeatureTags[i] = language->features[i].tag;
1448 if (searchingFor)
1450 if (searchingFor == language->features[i].tag)
1452 pFeatureTags[0] = language->features[i].tag;
1453 *pcTags = 1;
1454 if (feature)
1455 *feature = &language->features[i];
1456 rc = S_OK;
1457 break;
1461 return rc;