usp10: Implement GPOS MarkToBase.
[wine/multimedia.git] / dlls / usp10 / opentype.c
blob093dab7a1bba904fad57c3442e19d226a9e5e4c1
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 typedef struct {
318 WORD PosFormat;
319 WORD MarkCoverage;
320 WORD BaseCoverage;
321 WORD ClassCount;
322 WORD MarkArray;
323 WORD BaseArray;
324 } GPOS_MarkBasePosFormat1;
326 typedef struct {
327 WORD BaseAnchor[1];
328 } GPOS_BaseRecord;
330 typedef struct {
331 WORD BaseCount;
332 GPOS_BaseRecord BaseRecord[1];
333 } GPOS_BaseArray;
335 typedef struct {
336 WORD Class;
337 WORD MarkAnchor;
338 } GPOS_MarkRecord;
340 typedef struct {
341 WORD MarkCount;
342 GPOS_MarkRecord MarkRecord[1];
343 } GPOS_MarkArray;
345 /**********
346 * CMAP
347 **********/
349 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
351 CMAP_Header *CMAP_Table = NULL;
352 int length;
353 int i;
355 if (!psc->CMAP_Table)
357 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
358 if (length != GDI_ERROR)
360 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
361 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
362 TRACE("Loaded cmap table of %i bytes\n",length);
364 else
365 return NULL;
368 CMAP_Table = psc->CMAP_Table;
370 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
372 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
373 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
375 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
376 if (GET_BE_WORD(format->format) == 12)
377 return format;
380 return NULL;
383 static int compare_group(const void *a, const void* b)
385 const DWORD *chr = a;
386 const CMAP_SegmentedCoverage_group *group = b;
388 if (*chr < GET_BE_DWORD(group->startCharCode))
389 return -1;
390 if (*chr > GET_BE_DWORD(group->endCharCode))
391 return 1;
392 return 0;
395 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
397 /* BMP: use gdi32 for ease */
398 if (utf32c < 0x10000)
400 WCHAR ch = utf32c;
401 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
404 if (!psc->CMAP_format12_Table)
405 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
407 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
408 *pgi = 0xffff;
409 else
410 *pgi = 0;
412 if (psc->CMAP_format12_Table)
414 CMAP_SegmentedCoverage *format = NULL;
415 CMAP_SegmentedCoverage_group *group = NULL;
417 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
419 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
420 sizeof(CMAP_SegmentedCoverage_group), compare_group);
422 if (group)
424 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
425 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
426 return 0;
429 return 0;
432 /**********
433 * GDEF
434 **********/
436 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
438 int offset;
439 WORD class = 0;
440 const GDEF_ClassDefFormat1 *cf1;
442 if (!header)
443 return 0;
445 offset = GET_BE_WORD(header->GlyphClassDef);
446 if (!offset)
447 return 0;
449 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
450 if (GET_BE_WORD(cf1->ClassFormat) == 1)
452 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
454 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
455 if (index < GET_BE_WORD(cf1->GlyphCount))
456 class = GET_BE_WORD(cf1->ClassValueArray[index]);
459 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
461 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
462 int i, top;
463 top = GET_BE_WORD(cf2->ClassRangeCount);
464 for (i = 0; i < top; i++)
466 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
467 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
469 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
470 break;
474 else
475 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
477 return class;
480 static VOID *load_gdef_table(HDC hdc)
482 VOID* GDEF_Table = NULL;
483 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
484 if (length != GDI_ERROR)
486 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
487 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
488 TRACE("Loaded GDEF table of %i bytes\n",length);
490 return GDEF_Table;
493 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
495 int i;
497 if (!psc->GDEF_Table)
498 psc->GDEF_Table = load_gdef_table(hdc);
500 for (i = 0; i < cGlyphs; i++)
502 WORD class;
503 int char_count = 0;
504 int k;
506 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
507 if (k >= 0)
509 for (; k < cChars && pwLogClust[k] == i; k++)
510 char_count++;
513 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
515 switch (class)
517 case 0:
518 case BaseGlyph:
519 pGlyphProp[i].sva.fClusterStart = 1;
520 pGlyphProp[i].sva.fDiacritic = 0;
521 pGlyphProp[i].sva.fZeroWidth = 0;
522 break;
523 case LigatureGlyph:
524 pGlyphProp[i].sva.fClusterStart = 1;
525 pGlyphProp[i].sva.fDiacritic = 0;
526 pGlyphProp[i].sva.fZeroWidth = 0;
527 break;
528 case MarkGlyph:
529 pGlyphProp[i].sva.fClusterStart = 0;
530 pGlyphProp[i].sva.fDiacritic = 1;
531 pGlyphProp[i].sva.fZeroWidth = 1;
532 break;
533 case ComponentGlyph:
534 pGlyphProp[i].sva.fClusterStart = 0;
535 pGlyphProp[i].sva.fDiacritic = 0;
536 pGlyphProp[i].sva.fZeroWidth = 0;
537 break;
538 default:
539 ERR("Unknown glyph class %i\n",class);
540 pGlyphProp[i].sva.fClusterStart = 1;
541 pGlyphProp[i].sva.fDiacritic = 0;
542 pGlyphProp[i].sva.fZeroWidth = 0;
545 if (char_count == 0)
546 pGlyphProp[i].sva.fClusterStart = 0;
550 /**********
551 * GSUB
552 **********/
553 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
555 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
557 const OT_CoverageFormat1* cf1;
559 cf1 = table;
561 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
563 int count = GET_BE_WORD(cf1->GlyphCount);
564 int i;
565 TRACE("Coverage Format 1, %i glyphs\n",count);
566 for (i = 0; i < count; i++)
567 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
568 return i;
569 return -1;
571 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
573 const OT_CoverageFormat2* cf2;
574 int i;
575 int count;
576 cf2 = (const OT_CoverageFormat2*)cf1;
578 count = GET_BE_WORD(cf2->RangeCount);
579 TRACE("Coverage Format 2, %i ranges\n",count);
580 for (i = 0; i < count; i++)
582 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
583 return -1;
584 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
585 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
587 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
588 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
591 return -1;
593 else
594 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
596 return -1;
599 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
601 int j;
602 TRACE("Single Substitution Subtable\n");
604 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
606 int offset;
607 const GSUB_SingleSubstFormat1 *ssf1;
608 offset = GET_BE_WORD(look->SubTable[j]);
609 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
610 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
612 int offset = GET_BE_WORD(ssf1->Coverage);
613 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
614 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
616 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
617 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
618 TRACE(" 0x%x\n",glyphs[glyph_index]);
619 return glyph_index + write_dir;
622 else
624 const GSUB_SingleSubstFormat2 *ssf2;
625 INT index;
626 INT offset;
628 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
629 offset = GET_BE_WORD(ssf1->Coverage);
630 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
631 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
632 TRACE(" Coverage index %i\n",index);
633 if (index != -1)
635 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
636 return GSUB_E_NOGLYPH;
638 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
639 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
640 TRACE("0x%x\n",glyphs[glyph_index]);
641 return glyph_index + write_dir;
645 return GSUB_E_NOGLYPH;
648 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
650 int j;
651 TRACE("Multiple Substitution Subtable\n");
653 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
655 int offset, index;
656 const GSUB_MultipleSubstFormat1 *msf1;
657 offset = GET_BE_WORD(look->SubTable[j]);
658 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
660 offset = GET_BE_WORD(msf1->Coverage);
661 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
662 if (index != -1)
664 const GSUB_Sequence *seq;
665 int sub_count;
666 int j;
667 offset = GET_BE_WORD(msf1->Sequence[index]);
668 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
669 sub_count = GET_BE_WORD(seq->GlyphCount);
670 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
672 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
673 glyphs[j] =glyphs[j-(sub_count-1)];
675 for (j = 0; j < sub_count; j++)
676 if (write_dir < 0)
677 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
678 else
679 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
681 *glyph_count = *glyph_count + (sub_count - 1);
683 if (TRACE_ON(uniscribe))
685 for (j = 0; j < sub_count; j++)
686 TRACE(" 0x%x",glyphs[glyph_index+j]);
687 TRACE("\n");
690 return glyph_index + (sub_count * write_dir);
693 return GSUB_E_NOGLYPH;
696 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
698 int j;
699 TRACE("Alternate Substitution Subtable\n");
701 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
703 int offset;
704 const GSUB_AlternateSubstFormat1 *asf1;
705 INT index;
707 offset = GET_BE_WORD(look->SubTable[j]);
708 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
709 offset = GET_BE_WORD(asf1->Coverage);
711 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
712 if (index != -1)
714 const GSUB_AlternateSet *as;
715 offset = GET_BE_WORD(asf1->AlternateSet[index]);
716 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
717 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
718 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
719 return GSUB_E_NOGLYPH;
721 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
722 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
723 TRACE(" 0x%x\n",glyphs[glyph_index]);
724 return glyph_index + write_dir;
727 return GSUB_E_NOGLYPH;
730 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
732 int j;
734 TRACE("Ligature Substitution Subtable\n");
735 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
737 const GSUB_LigatureSubstFormat1 *lsf1;
738 int offset,index;
740 offset = GET_BE_WORD(look->SubTable[j]);
741 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
742 offset = GET_BE_WORD(lsf1->Coverage);
743 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
744 TRACE(" Coverage index %i\n",index);
745 if (index != -1)
747 const GSUB_LigatureSet *ls;
748 int k, count;
750 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
751 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
752 count = GET_BE_WORD(ls->LigatureCount);
753 TRACE(" LigatureSet has %i members\n",count);
754 for (k = 0; k < count; k++)
756 const GSUB_Ligature *lig;
757 int CompCount,l,CompIndex;
759 offset = GET_BE_WORD(ls->Ligature[k]);
760 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
761 CompCount = GET_BE_WORD(lig->CompCount) - 1;
762 CompIndex = glyph_index+write_dir;
763 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
765 int CompGlyph;
766 CompGlyph = GET_BE_WORD(lig->Component[l]);
767 if (CompGlyph != glyphs[CompIndex])
768 break;
769 CompIndex += write_dir;
771 if (l == CompCount)
773 int replaceIdx = glyph_index;
774 if (write_dir < 0)
775 replaceIdx = glyph_index - CompCount;
777 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
778 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
779 TRACE("0x%x\n",glyphs[replaceIdx]);
780 if (CompCount > 0)
782 int j;
783 for (j = replaceIdx + 1; j < *glyph_count; j++)
784 glyphs[j] =glyphs[j+CompCount];
785 *glyph_count = *glyph_count - CompCount;
787 return replaceIdx + write_dir;
792 return GSUB_E_NOGLYPH;
795 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
797 int j;
798 BOOL done = FALSE;
800 TRACE("Chaining Contextual Substitution Subtable\n");
801 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
803 const GSUB_ChainContextSubstFormat1 *ccsf1;
804 int offset;
805 int dirLookahead = write_dir;
806 int dirBacktrack = -1 * write_dir;
808 offset = GET_BE_WORD(look->SubTable[j]);
809 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
810 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
812 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
813 continue;
815 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
817 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
818 continue;
820 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
822 int k;
823 int indexGlyphs;
824 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
825 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
826 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
827 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
828 int newIndex = glyph_index;
830 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
832 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
834 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
836 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
837 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
838 break;
840 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
841 continue;
842 TRACE("Matched Backtrack\n");
844 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
846 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
847 for (k = 0; k < indexGlyphs; k++)
849 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
850 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
851 break;
853 if (k != indexGlyphs)
854 continue;
855 TRACE("Matched IndexGlyphs\n");
857 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
859 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
861 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
862 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
863 break;
865 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
866 continue;
867 TRACE("Matched LookAhead\n");
869 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
871 if (GET_BE_WORD(ccsf3_4->SubstCount))
873 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
875 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
876 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
878 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
879 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
880 if (newIndex == -1)
882 ERR("Chain failed to generate a glyph\n");
883 continue;
886 return newIndex;
888 else return GSUB_E_NOGLYPH;
891 return -1;
894 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
896 int offset;
897 const OT_LookupTable *look;
899 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
900 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
901 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
902 switch(GET_BE_WORD(look->LookupType))
904 case 1:
905 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
906 case 2:
907 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
908 case 3:
909 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
910 case 4:
911 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
912 case 6:
913 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
914 default:
915 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
917 return GSUB_E_NOGLYPH;
920 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
922 const GSUB_Header *header = (const GSUB_Header *)table;
923 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
925 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
928 /**********
929 * GPOS
930 **********/
931 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
933 static const WORD mask[3] = {3,0xf,0xff};
934 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
936 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
937 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
938 int value;
939 TRACE("device table, format %i, index %i\n",format, index);
940 index = index << format;
941 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
942 TRACE("offset %i, value %i\n",index, value);
943 if (value > mask[format-1]/2)
944 value = -1 * ((mask[format-1]+1) - value);
945 return value;
947 return 0;
950 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
952 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
954 switch (GET_BE_WORD(anchor1->AnchorFormat))
956 case 1:
958 TRACE("Anchor Format 1\n");
959 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
960 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
961 break;
963 case 2:
965 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
966 TRACE("Anchor Format 2\n");
967 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
968 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
969 break;
971 case 3:
973 int offset;
974 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
975 TRACE("Anchor Format 3\n");
976 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
977 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
978 offset = GET_BE_WORD(anchor3->XDeviceTable);
979 TRACE("ppem %i\n",ppem);
980 if (offset)
982 const OT_DeviceTable* DeviceTableX = NULL;
983 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
984 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
986 offset = GET_BE_WORD(anchor3->YDeviceTable);
987 if (offset)
989 const OT_DeviceTable* DeviceTableY = NULL;
990 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
991 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
993 break;
995 default:
996 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
997 pt->x = 0;
998 pt->y = 0;
1002 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1004 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1006 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1007 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1008 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1009 if (lplogfont->lfWidth)
1010 FIXME("Font with lfWidth set no handled properly\n");
1013 static VOID GPOS_apply_MarkToBase(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT pt)
1015 int j;
1017 TRACE("MarkToBase Attachment Positioning Subtable\n");
1019 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1021 int offset;
1022 const GPOS_MarkBasePosFormat1 *mbpf1;
1023 offset = GET_BE_WORD(look->SubTable[j]);
1024 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1025 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1027 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1028 int mark_index;
1029 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1030 if (mark_index != -1)
1032 int base_index;
1033 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1034 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1035 if (base_index != -1)
1037 const GPOS_MarkArray *ma;
1038 const GPOS_MarkRecord *mr;
1039 const GPOS_BaseArray *ba;
1040 const GPOS_BaseRecord *br;
1041 int mark_class;
1042 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1043 int baserecord_size;
1044 POINT base_pt;
1045 POINT mark_pt;
1046 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1047 offset = GET_BE_WORD(mbpf1->MarkArray);
1048 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1049 if (mark_index > GET_BE_WORD(ma->MarkCount))
1051 ERR("Mark index exeeded mark count\n");
1052 return;
1054 mr = &ma->MarkRecord[mark_index];
1055 mark_class = GET_BE_WORD(mr->Class);
1056 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1057 offset = GET_BE_WORD(mbpf1->BaseArray);
1058 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1059 baserecord_size = class_count * sizeof(WORD);
1060 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1061 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1062 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1063 offset = GET_BE_WORD(mr->MarkAnchor);
1064 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1065 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1066 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1067 pt->x += base_pt.x - mark_pt.x;
1068 pt->y += base_pt.y - mark_pt.y;
1069 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1073 else
1074 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1078 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)
1080 int offset;
1081 const OT_LookupTable *look;
1082 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1084 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1085 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1086 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1087 switch(GET_BE_WORD(look->LookupType))
1089 case 4:
1091 double devX, devY;
1092 POINT desU = {0,0};
1093 GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1094 if (desU.x || desU.y)
1096 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1097 pGoffset[glyph_index].du += ((int)(devX+0.5) - piAdvance[glyph_index-1]);
1098 pGoffset[glyph_index].dv += (int)(devY+0.5);
1100 break;
1102 default:
1103 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1105 return glyph_index+1;
1108 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)
1110 const GPOS_Header *header = (const GPOS_Header *)table;
1111 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1113 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1116 static void GSUB_initialize_script_cache(ScriptCache *psc)
1118 int i;
1120 if (psc->GSUB_Table)
1122 const OT_ScriptList *script;
1123 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1124 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1125 psc->script_count = GET_BE_WORD(script->ScriptCount);
1126 TRACE("initializing %i scripts in this font\n",psc->script_count);
1127 if (psc->script_count)
1129 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1130 for (i = 0; i < psc->script_count; i++)
1132 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1133 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]);
1134 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1140 static void GPOS_expand_script_cache(ScriptCache *psc)
1142 int i, count;
1143 const OT_ScriptList *script;
1144 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1146 if (!header)
1147 return;
1149 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1150 count = GET_BE_WORD(script->ScriptCount);
1152 if (!psc->script_count)
1154 psc->script_count = count;
1155 TRACE("initializing %i scripts in this font\n",psc->script_count);
1156 if (psc->script_count)
1158 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1159 for (i = 0; i < psc->script_count; i++)
1161 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1162 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]);
1163 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1167 else
1169 for (i = 0; i < count; i++)
1171 int j;
1172 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1173 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]);
1174 for (j = 0; j < psc->script_count; j++)
1176 if (psc->scripts[j].tag == tag)
1178 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1179 break;
1182 if (j == psc->script_count)
1184 psc->script_count++;
1185 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1186 psc->scripts[j].tag = tag;
1187 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1193 static void _initialize_script_cache(ScriptCache *psc)
1195 if (!psc->script_count)
1197 GSUB_initialize_script_cache(psc);
1198 GPOS_expand_script_cache(psc);
1202 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1204 int i;
1205 HRESULT rc = S_OK;
1207 _initialize_script_cache(psc);
1209 *pcTags = psc->script_count;
1211 if (!searchingFor && cMaxTags < *pcTags)
1212 rc = E_OUTOFMEMORY;
1213 else if (searchingFor)
1214 rc = USP_E_SCRIPT_NOT_IN_FONT;
1216 for (i = 0; i < psc->script_count; i++)
1218 if (i < cMaxTags)
1219 pScriptTags[i] = psc->scripts[i].tag;
1221 if (searchingFor)
1223 if (searchingFor == psc->scripts[i].tag)
1225 pScriptTags[0] = psc->scripts[i].tag;
1226 *pcTags = 1;
1227 rc = S_OK;
1228 break;
1232 return rc;
1235 static void GSUB_initialize_language_cache(LoadedScript *script)
1237 int i;
1239 if (script->gsub_table)
1241 DWORD offset;
1242 const OT_Script* table = script->gsub_table;
1243 script->language_count = GET_BE_WORD(table->LangSysCount);
1244 offset = GET_BE_WORD(table->DefaultLangSys);
1245 if (offset)
1247 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1248 script->default_language.gsub_table = (const BYTE*)table + offset;
1251 if (script->language_count)
1253 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1255 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1257 for (i = 0; i < script->language_count; i++)
1259 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1260 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]);
1261 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1267 static void GPOS_expand_language_cache(LoadedScript *script)
1269 int count;
1270 const OT_Script* table = script->gpos_table;
1271 DWORD offset;
1273 if (!table)
1274 return;
1276 offset = GET_BE_WORD(table->DefaultLangSys);
1277 if (offset)
1278 script->default_language.gpos_table = (const BYTE*)table + offset;
1280 count = GET_BE_WORD(table->LangSysCount);
1282 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1283 if (!script->language_count)
1285 int i;
1286 script->language_count = count;
1288 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1290 for (i = 0; i < script->language_count; i++)
1292 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1293 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]);
1294 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1297 else if (count)
1299 int i,j;
1300 for (i = 0; i < count; i++)
1302 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1303 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]);
1305 for (j = 0; j < script->language_count; j++)
1307 if (script->languages[j].tag == tag)
1309 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1310 break;
1313 if (j == script->language_count)
1315 script->language_count++;
1316 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1317 script->languages[j].tag = tag;
1318 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1324 static void _initialize_language_cache(LoadedScript *script)
1326 if (!script->language_count)
1328 GSUB_initialize_language_cache(script);
1329 GPOS_expand_language_cache(script);
1333 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1335 int i;
1336 HRESULT rc = S_OK;
1337 LoadedScript *script = NULL;
1339 _initialize_script_cache(psc);
1341 for (i = 0; i < psc->script_count; i++)
1343 if (psc->scripts[i].tag == script_tag)
1345 script = &psc->scripts[i];
1346 break;
1350 if (!script)
1351 return E_INVALIDARG;
1353 _initialize_language_cache(script);
1355 if (!searchingFor && cMaxTags < script->language_count)
1356 rc = E_OUTOFMEMORY;
1357 else if (searchingFor)
1358 rc = E_INVALIDARG;
1360 *pcTags = script->language_count;
1362 for (i = 0; i < script->language_count; i++)
1364 if (i < cMaxTags)
1365 pLanguageTags[i] = script->languages[i].tag;
1367 if (searchingFor)
1369 if (searchingFor == script->languages[i].tag)
1371 pLanguageTags[0] = script->languages[i].tag;
1372 *pcTags = 1;
1373 rc = S_OK;
1374 break;
1379 if (script->default_language.gsub_table)
1381 if (i < cMaxTags)
1382 pLanguageTags[i] = script->default_language.tag;
1384 if (searchingFor && FAILED(rc))
1386 pLanguageTags[0] = script->default_language.tag;
1388 i++;
1389 *pcTags = (*pcTags) + 1;
1392 return rc;
1396 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1398 int i;
1400 if (language->gsub_table)
1402 const OT_LangSys *lang = language->gsub_table;
1403 const GSUB_Header *header = (const GSUB_Header *)table;
1404 const OT_FeatureList *feature_list;
1406 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1407 TRACE("%i features\n",language->feature_count);
1409 if (language->feature_count)
1411 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1413 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1415 for (i = 0; i < language->feature_count; i++)
1417 const OT_Feature *feature;
1418 int j;
1419 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1421 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]);
1422 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1423 feature = (const OT_Feature*)language->features[i].feature;
1424 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1425 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1426 for (j = 0; j < language->features[i].lookup_count; j++)
1427 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1433 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1435 int i, count;
1436 const OT_LangSys *lang = language->gpos_table;
1437 const GPOS_Header *header = (const GPOS_Header *)table;
1438 const OT_FeatureList *feature_list;
1440 if (!lang)
1441 return;
1443 count = GET_BE_WORD(lang->FeatureCount);
1444 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1446 TRACE("%i features\n",count);
1447 if (!language->feature_count)
1449 language->feature_count = count;
1451 if (language->feature_count)
1453 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1455 for (i = 0; i < language->feature_count; i++)
1457 const OT_Feature *feature;
1458 int j;
1459 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1461 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]);
1462 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1463 feature = (const OT_Feature*)language->features[i].feature;
1464 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1465 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1466 for (j = 0; j < language->features[i].lookup_count; j++)
1467 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1471 else if (count)
1473 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1475 for (i = 0; i < count; i++)
1477 const OT_Feature *feature;
1478 int j;
1479 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1480 int idx = language->feature_count + i;
1482 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]);
1483 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1484 feature = (const OT_Feature*)language->features[idx].feature;
1485 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1486 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1487 for (j = 0; j < language->features[idx].lookup_count; j++)
1488 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1490 language->feature_count += count;
1494 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1496 if (!language->feature_count)
1498 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1499 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1503 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)
1505 int i;
1506 HRESULT rc = S_OK;
1507 LoadedScript *script = NULL;
1508 LoadedLanguage *language = NULL;
1510 _initialize_script_cache(psc);
1512 for (i = 0; i < psc->script_count; i++)
1514 if (psc->scripts[i].tag == script_tag)
1516 script = &psc->scripts[i];
1517 break;
1521 if (!script)
1523 *pcTags = 0;
1524 if (!filtered)
1525 return S_OK;
1526 else
1527 return E_INVALIDARG;
1530 _initialize_language_cache(script);
1532 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1533 language = &script->default_language;
1534 else
1536 for (i = 0; i < script->language_count; i++)
1538 if (script->languages[i].tag == language_tag)
1540 language = &script->languages[i];
1541 break;
1546 if (!language)
1548 *pcTags = 0;
1549 return S_OK;
1552 _initialize_feature_cache(psc, language);
1554 *pcTags = language->feature_count;
1556 if (!searchingFor && cMaxTags < *pcTags)
1557 rc = E_OUTOFMEMORY;
1558 else if (searchingFor)
1559 rc = E_INVALIDARG;
1561 for (i = 0; i < language->feature_count; i++)
1563 if (i < cMaxTags)
1564 pFeatureTags[i] = language->features[i].tag;
1566 if (searchingFor)
1568 if (searchingFor == language->features[i].tag)
1570 pFeatureTags[0] = language->features[i].tag;
1571 *pcTags = 1;
1572 if (feature)
1573 *feature = &language->features[i];
1574 rc = S_OK;
1575 break;
1579 return rc;