usp10: Handle GPOS ValueRecords.
[wine/multimedia.git] / dlls / usp10 / opentype.c
blob70d0da75cbda0ea9125e743b719572f9dcf4a86e
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 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 MarkCoverage;
331 WORD BaseCoverage;
332 WORD ClassCount;
333 WORD MarkArray;
334 WORD BaseArray;
335 } GPOS_MarkBasePosFormat1;
337 typedef struct {
338 WORD BaseAnchor[1];
339 } GPOS_BaseRecord;
341 typedef struct {
342 WORD BaseCount;
343 GPOS_BaseRecord BaseRecord[1];
344 } GPOS_BaseArray;
346 typedef struct {
347 WORD Class;
348 WORD MarkAnchor;
349 } GPOS_MarkRecord;
351 typedef struct {
352 WORD MarkCount;
353 GPOS_MarkRecord MarkRecord[1];
354 } GPOS_MarkArray;
356 /**********
357 * CMAP
358 **********/
360 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
362 CMAP_Header *CMAP_Table = NULL;
363 int length;
364 int i;
366 if (!psc->CMAP_Table)
368 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
369 if (length != GDI_ERROR)
371 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
372 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
373 TRACE("Loaded cmap table of %i bytes\n",length);
375 else
376 return NULL;
379 CMAP_Table = psc->CMAP_Table;
381 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
383 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
384 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
386 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
387 if (GET_BE_WORD(format->format) == 12)
388 return format;
391 return NULL;
394 static int compare_group(const void *a, const void* b)
396 const DWORD *chr = a;
397 const CMAP_SegmentedCoverage_group *group = b;
399 if (*chr < GET_BE_DWORD(group->startCharCode))
400 return -1;
401 if (*chr > GET_BE_DWORD(group->endCharCode))
402 return 1;
403 return 0;
406 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
408 /* BMP: use gdi32 for ease */
409 if (utf32c < 0x10000)
411 WCHAR ch = utf32c;
412 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
415 if (!psc->CMAP_format12_Table)
416 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
418 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
419 *pgi = 0xffff;
420 else
421 *pgi = 0;
423 if (psc->CMAP_format12_Table)
425 CMAP_SegmentedCoverage *format = NULL;
426 CMAP_SegmentedCoverage_group *group = NULL;
428 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
430 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
431 sizeof(CMAP_SegmentedCoverage_group), compare_group);
433 if (group)
435 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
436 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
437 return 0;
440 return 0;
443 /**********
444 * GDEF
445 **********/
447 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
449 int offset;
450 WORD class = 0;
451 const GDEF_ClassDefFormat1 *cf1;
453 if (!header)
454 return 0;
456 offset = GET_BE_WORD(header->GlyphClassDef);
457 if (!offset)
458 return 0;
460 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
461 if (GET_BE_WORD(cf1->ClassFormat) == 1)
463 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
465 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
466 if (index < GET_BE_WORD(cf1->GlyphCount))
467 class = GET_BE_WORD(cf1->ClassValueArray[index]);
470 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
472 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
473 int i, top;
474 top = GET_BE_WORD(cf2->ClassRangeCount);
475 for (i = 0; i < top; i++)
477 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
478 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
480 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
481 break;
485 else
486 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
488 return class;
491 static VOID *load_gdef_table(HDC hdc)
493 VOID* GDEF_Table = NULL;
494 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
495 if (length != GDI_ERROR)
497 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
498 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
499 TRACE("Loaded GDEF table of %i bytes\n",length);
501 return GDEF_Table;
504 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
506 int i;
508 if (!psc->GDEF_Table)
509 psc->GDEF_Table = load_gdef_table(hdc);
511 for (i = 0; i < cGlyphs; i++)
513 WORD class;
514 int char_count = 0;
515 int k;
517 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
518 if (k >= 0)
520 for (; k < cChars && pwLogClust[k] == i; k++)
521 char_count++;
524 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
526 switch (class)
528 case 0:
529 case BaseGlyph:
530 pGlyphProp[i].sva.fClusterStart = 1;
531 pGlyphProp[i].sva.fDiacritic = 0;
532 pGlyphProp[i].sva.fZeroWidth = 0;
533 break;
534 case LigatureGlyph:
535 pGlyphProp[i].sva.fClusterStart = 1;
536 pGlyphProp[i].sva.fDiacritic = 0;
537 pGlyphProp[i].sva.fZeroWidth = 0;
538 break;
539 case MarkGlyph:
540 pGlyphProp[i].sva.fClusterStart = 0;
541 pGlyphProp[i].sva.fDiacritic = 1;
542 pGlyphProp[i].sva.fZeroWidth = 1;
543 break;
544 case ComponentGlyph:
545 pGlyphProp[i].sva.fClusterStart = 0;
546 pGlyphProp[i].sva.fDiacritic = 0;
547 pGlyphProp[i].sva.fZeroWidth = 0;
548 break;
549 default:
550 ERR("Unknown glyph class %i\n",class);
551 pGlyphProp[i].sva.fClusterStart = 1;
552 pGlyphProp[i].sva.fDiacritic = 0;
553 pGlyphProp[i].sva.fZeroWidth = 0;
556 if (char_count == 0)
557 pGlyphProp[i].sva.fClusterStart = 0;
561 /**********
562 * GSUB
563 **********/
564 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
566 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
568 const OT_CoverageFormat1* cf1;
570 cf1 = table;
572 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
574 int count = GET_BE_WORD(cf1->GlyphCount);
575 int i;
576 TRACE("Coverage Format 1, %i glyphs\n",count);
577 for (i = 0; i < count; i++)
578 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
579 return i;
580 return -1;
582 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
584 const OT_CoverageFormat2* cf2;
585 int i;
586 int count;
587 cf2 = (const OT_CoverageFormat2*)cf1;
589 count = GET_BE_WORD(cf2->RangeCount);
590 TRACE("Coverage Format 2, %i ranges\n",count);
591 for (i = 0; i < count; i++)
593 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
594 return -1;
595 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
596 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
598 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
599 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
602 return -1;
604 else
605 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
607 return -1;
610 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
612 int j;
613 TRACE("Single Substitution Subtable\n");
615 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
617 int offset;
618 const GSUB_SingleSubstFormat1 *ssf1;
619 offset = GET_BE_WORD(look->SubTable[j]);
620 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
621 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
623 int offset = GET_BE_WORD(ssf1->Coverage);
624 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
625 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
627 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
628 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
629 TRACE(" 0x%x\n",glyphs[glyph_index]);
630 return glyph_index + write_dir;
633 else
635 const GSUB_SingleSubstFormat2 *ssf2;
636 INT index;
637 INT offset;
639 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
640 offset = GET_BE_WORD(ssf1->Coverage);
641 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
642 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
643 TRACE(" Coverage index %i\n",index);
644 if (index != -1)
646 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
647 return GSUB_E_NOGLYPH;
649 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
650 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
651 TRACE("0x%x\n",glyphs[glyph_index]);
652 return glyph_index + write_dir;
656 return GSUB_E_NOGLYPH;
659 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
661 int j;
662 TRACE("Multiple Substitution Subtable\n");
664 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
666 int offset, index;
667 const GSUB_MultipleSubstFormat1 *msf1;
668 offset = GET_BE_WORD(look->SubTable[j]);
669 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
671 offset = GET_BE_WORD(msf1->Coverage);
672 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
673 if (index != -1)
675 const GSUB_Sequence *seq;
676 int sub_count;
677 int j;
678 offset = GET_BE_WORD(msf1->Sequence[index]);
679 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
680 sub_count = GET_BE_WORD(seq->GlyphCount);
681 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
683 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
684 glyphs[j] =glyphs[j-(sub_count-1)];
686 for (j = 0; j < sub_count; j++)
687 if (write_dir < 0)
688 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
689 else
690 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
692 *glyph_count = *glyph_count + (sub_count - 1);
694 if (TRACE_ON(uniscribe))
696 for (j = 0; j < sub_count; j++)
697 TRACE(" 0x%x",glyphs[glyph_index+j]);
698 TRACE("\n");
701 return glyph_index + (sub_count * write_dir);
704 return GSUB_E_NOGLYPH;
707 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
709 int j;
710 TRACE("Alternate Substitution Subtable\n");
712 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
714 int offset;
715 const GSUB_AlternateSubstFormat1 *asf1;
716 INT index;
718 offset = GET_BE_WORD(look->SubTable[j]);
719 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
720 offset = GET_BE_WORD(asf1->Coverage);
722 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
723 if (index != -1)
725 const GSUB_AlternateSet *as;
726 offset = GET_BE_WORD(asf1->AlternateSet[index]);
727 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
728 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
729 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
730 return GSUB_E_NOGLYPH;
732 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
733 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
734 TRACE(" 0x%x\n",glyphs[glyph_index]);
735 return glyph_index + write_dir;
738 return GSUB_E_NOGLYPH;
741 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
743 int j;
745 TRACE("Ligature Substitution Subtable\n");
746 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
748 const GSUB_LigatureSubstFormat1 *lsf1;
749 int offset,index;
751 offset = GET_BE_WORD(look->SubTable[j]);
752 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
753 offset = GET_BE_WORD(lsf1->Coverage);
754 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
755 TRACE(" Coverage index %i\n",index);
756 if (index != -1)
758 const GSUB_LigatureSet *ls;
759 int k, count;
761 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
762 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
763 count = GET_BE_WORD(ls->LigatureCount);
764 TRACE(" LigatureSet has %i members\n",count);
765 for (k = 0; k < count; k++)
767 const GSUB_Ligature *lig;
768 int CompCount,l,CompIndex;
770 offset = GET_BE_WORD(ls->Ligature[k]);
771 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
772 CompCount = GET_BE_WORD(lig->CompCount) - 1;
773 CompIndex = glyph_index+write_dir;
774 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
776 int CompGlyph;
777 CompGlyph = GET_BE_WORD(lig->Component[l]);
778 if (CompGlyph != glyphs[CompIndex])
779 break;
780 CompIndex += write_dir;
782 if (l == CompCount)
784 int replaceIdx = glyph_index;
785 if (write_dir < 0)
786 replaceIdx = glyph_index - CompCount;
788 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
789 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
790 TRACE("0x%x\n",glyphs[replaceIdx]);
791 if (CompCount > 0)
793 int j;
794 for (j = replaceIdx + 1; j < *glyph_count; j++)
795 glyphs[j] =glyphs[j+CompCount];
796 *glyph_count = *glyph_count - CompCount;
798 return replaceIdx + write_dir;
803 return GSUB_E_NOGLYPH;
806 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
808 int j;
809 BOOL done = FALSE;
811 TRACE("Chaining Contextual Substitution Subtable\n");
812 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
814 const GSUB_ChainContextSubstFormat1 *ccsf1;
815 int offset;
816 int dirLookahead = write_dir;
817 int dirBacktrack = -1 * write_dir;
819 offset = GET_BE_WORD(look->SubTable[j]);
820 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
821 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
823 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
824 continue;
826 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
828 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
829 continue;
831 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
833 int k;
834 int indexGlyphs;
835 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
836 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
837 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
838 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
839 int newIndex = glyph_index;
841 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
843 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
845 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
847 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
848 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
849 break;
851 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
852 continue;
853 TRACE("Matched Backtrack\n");
855 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
857 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
858 for (k = 0; k < indexGlyphs; k++)
860 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
861 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
862 break;
864 if (k != indexGlyphs)
865 continue;
866 TRACE("Matched IndexGlyphs\n");
868 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
870 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
872 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
873 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
874 break;
876 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
877 continue;
878 TRACE("Matched LookAhead\n");
880 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
882 if (GET_BE_WORD(ccsf3_4->SubstCount))
884 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
886 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
887 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
889 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
890 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
891 if (newIndex == -1)
893 ERR("Chain failed to generate a glyph\n");
894 continue;
897 return newIndex;
899 else return GSUB_E_NOGLYPH;
902 return -1;
905 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
907 int offset;
908 const OT_LookupTable *look;
910 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
911 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
912 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
913 switch(GET_BE_WORD(look->LookupType))
915 case 1:
916 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
917 case 2:
918 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
919 case 3:
920 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
921 case 4:
922 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
923 case 6:
924 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
925 default:
926 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
928 return GSUB_E_NOGLYPH;
931 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
933 const GSUB_Header *header = (const GSUB_Header *)table;
934 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
936 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
939 /**********
940 * GPOS
941 **********/
942 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
944 static const WORD mask[3] = {3,0xf,0xff};
945 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
947 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
948 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
949 int value;
950 TRACE("device table, format %i, index %i\n",format, index);
951 index = index << format;
952 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
953 TRACE("offset %i, value %i\n",index, value);
954 if (value > mask[format-1]/2)
955 value = -1 * ((mask[format-1]+1) - value);
956 return value;
958 return 0;
961 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
963 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
965 switch (GET_BE_WORD(anchor1->AnchorFormat))
967 case 1:
969 TRACE("Anchor Format 1\n");
970 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
971 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
972 break;
974 case 2:
976 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
977 TRACE("Anchor Format 2\n");
978 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
979 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
980 break;
982 case 3:
984 int offset;
985 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
986 TRACE("Anchor Format 3\n");
987 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
988 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
989 offset = GET_BE_WORD(anchor3->XDeviceTable);
990 TRACE("ppem %i\n",ppem);
991 if (offset)
993 const OT_DeviceTable* DeviceTableX = NULL;
994 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
995 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
997 offset = GET_BE_WORD(anchor3->YDeviceTable);
998 if (offset)
1000 const OT_DeviceTable* DeviceTableY = NULL;
1001 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1002 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1004 break;
1006 default:
1007 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1008 pt->x = 0;
1009 pt->y = 0;
1013 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1015 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1017 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1018 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1019 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1020 if (lplogfont->lfWidth)
1021 FIXME("Font with lfWidth set no handled properly\n");
1024 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1026 INT offset = 0;
1027 if (ValueFormat & 0x0001) record->XPlacement = GET_BE_WORD(data[offset++]);
1028 if (ValueFormat & 0x0002) record->YPlacement = GET_BE_WORD(data[offset++]);
1029 if (ValueFormat & 0x0004) record->XAdvance = GET_BE_WORD(data[offset++]);
1030 if (ValueFormat & 0x0008) record->YAdvance = GET_BE_WORD(data[offset++]);
1031 if (ValueFormat & 0x0010) record->XPlaDevice = GET_BE_WORD(data[offset++]);
1032 if (ValueFormat & 0x0020) record->YPlaDevice = GET_BE_WORD(data[offset++]);
1033 if (ValueFormat & 0x0040) record->XAdvDevice = GET_BE_WORD(data[offset++]);
1034 if (ValueFormat & 0x0080) record->YAdvDevice = GET_BE_WORD(data[offset++]);
1035 return offset;
1038 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1040 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1041 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1042 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1043 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1044 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1045 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1046 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1047 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1048 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1051 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)
1053 int j;
1055 TRACE("MarkToBase Attachment Positioning Subtable\n");
1057 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1059 int offset;
1060 const GPOS_MarkBasePosFormat1 *mbpf1;
1061 offset = GET_BE_WORD(look->SubTable[j]);
1062 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1063 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1065 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1066 int mark_index;
1067 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1068 if (mark_index != -1)
1070 int base_index;
1071 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1072 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1073 if (base_index != -1)
1075 const GPOS_MarkArray *ma;
1076 const GPOS_MarkRecord *mr;
1077 const GPOS_BaseArray *ba;
1078 const GPOS_BaseRecord *br;
1079 int mark_class;
1080 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1081 int baserecord_size;
1082 POINT base_pt;
1083 POINT mark_pt;
1084 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1085 offset = GET_BE_WORD(mbpf1->MarkArray);
1086 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1087 if (mark_index > GET_BE_WORD(ma->MarkCount))
1089 ERR("Mark index exeeded mark count\n");
1090 return;
1092 mr = &ma->MarkRecord[mark_index];
1093 mark_class = GET_BE_WORD(mr->Class);
1094 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1095 offset = GET_BE_WORD(mbpf1->BaseArray);
1096 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1097 baserecord_size = class_count * sizeof(WORD);
1098 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1099 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1100 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1101 offset = GET_BE_WORD(mr->MarkAnchor);
1102 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1103 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1104 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1105 pt->x += base_pt.x - mark_pt.x;
1106 pt->y += base_pt.y - mark_pt.y;
1107 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1111 else
1112 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1116 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)
1118 int offset;
1119 const OT_LookupTable *look;
1120 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1122 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1123 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1124 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1125 switch(GET_BE_WORD(look->LookupType))
1127 case 4:
1129 double devX, devY;
1130 POINT desU = {0,0};
1131 GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1132 if (desU.x || desU.y)
1134 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1135 pGoffset[glyph_index].du += ((int)(devX+0.5) - piAdvance[glyph_index-1]);
1136 pGoffset[glyph_index].dv += (int)(devY+0.5);
1138 break;
1140 default:
1141 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1143 return glyph_index+1;
1146 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)
1148 const GPOS_Header *header = (const GPOS_Header *)table;
1149 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1151 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1154 static void GSUB_initialize_script_cache(ScriptCache *psc)
1156 int i;
1158 if (psc->GSUB_Table)
1160 const OT_ScriptList *script;
1161 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1162 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1163 psc->script_count = GET_BE_WORD(script->ScriptCount);
1164 TRACE("initializing %i scripts in this font\n",psc->script_count);
1165 if (psc->script_count)
1167 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1168 for (i = 0; i < psc->script_count; i++)
1170 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1171 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]);
1172 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1178 static void GPOS_expand_script_cache(ScriptCache *psc)
1180 int i, count;
1181 const OT_ScriptList *script;
1182 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1184 if (!header)
1185 return;
1187 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1188 count = GET_BE_WORD(script->ScriptCount);
1190 if (!psc->script_count)
1192 psc->script_count = count;
1193 TRACE("initializing %i scripts in this font\n",psc->script_count);
1194 if (psc->script_count)
1196 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1197 for (i = 0; i < psc->script_count; i++)
1199 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1200 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]);
1201 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1205 else
1207 for (i = 0; i < count; i++)
1209 int j;
1210 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1211 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]);
1212 for (j = 0; j < psc->script_count; j++)
1214 if (psc->scripts[j].tag == tag)
1216 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1217 break;
1220 if (j == psc->script_count)
1222 psc->script_count++;
1223 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1224 psc->scripts[j].tag = tag;
1225 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1231 static void _initialize_script_cache(ScriptCache *psc)
1233 if (!psc->script_count)
1235 GSUB_initialize_script_cache(psc);
1236 GPOS_expand_script_cache(psc);
1240 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1242 int i;
1243 HRESULT rc = S_OK;
1245 _initialize_script_cache(psc);
1247 *pcTags = psc->script_count;
1249 if (!searchingFor && cMaxTags < *pcTags)
1250 rc = E_OUTOFMEMORY;
1251 else if (searchingFor)
1252 rc = USP_E_SCRIPT_NOT_IN_FONT;
1254 for (i = 0; i < psc->script_count; i++)
1256 if (i < cMaxTags)
1257 pScriptTags[i] = psc->scripts[i].tag;
1259 if (searchingFor)
1261 if (searchingFor == psc->scripts[i].tag)
1263 pScriptTags[0] = psc->scripts[i].tag;
1264 *pcTags = 1;
1265 rc = S_OK;
1266 break;
1270 return rc;
1273 static void GSUB_initialize_language_cache(LoadedScript *script)
1275 int i;
1277 if (script->gsub_table)
1279 DWORD offset;
1280 const OT_Script* table = script->gsub_table;
1281 script->language_count = GET_BE_WORD(table->LangSysCount);
1282 offset = GET_BE_WORD(table->DefaultLangSys);
1283 if (offset)
1285 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1286 script->default_language.gsub_table = (const BYTE*)table + offset;
1289 if (script->language_count)
1291 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1293 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1295 for (i = 0; i < script->language_count; i++)
1297 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1298 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]);
1299 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1305 static void GPOS_expand_language_cache(LoadedScript *script)
1307 int count;
1308 const OT_Script* table = script->gpos_table;
1309 DWORD offset;
1311 if (!table)
1312 return;
1314 offset = GET_BE_WORD(table->DefaultLangSys);
1315 if (offset)
1316 script->default_language.gpos_table = (const BYTE*)table + offset;
1318 count = GET_BE_WORD(table->LangSysCount);
1320 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1321 if (!script->language_count)
1323 int i;
1324 script->language_count = count;
1326 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1328 for (i = 0; i < script->language_count; i++)
1330 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1331 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]);
1332 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1335 else if (count)
1337 int i,j;
1338 for (i = 0; i < count; i++)
1340 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1341 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]);
1343 for (j = 0; j < script->language_count; j++)
1345 if (script->languages[j].tag == tag)
1347 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1348 break;
1351 if (j == script->language_count)
1353 script->language_count++;
1354 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1355 script->languages[j].tag = tag;
1356 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1362 static void _initialize_language_cache(LoadedScript *script)
1364 if (!script->language_count)
1366 GSUB_initialize_language_cache(script);
1367 GPOS_expand_language_cache(script);
1371 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1373 int i;
1374 HRESULT rc = S_OK;
1375 LoadedScript *script = NULL;
1377 _initialize_script_cache(psc);
1379 for (i = 0; i < psc->script_count; i++)
1381 if (psc->scripts[i].tag == script_tag)
1383 script = &psc->scripts[i];
1384 break;
1388 if (!script)
1389 return E_INVALIDARG;
1391 _initialize_language_cache(script);
1393 if (!searchingFor && cMaxTags < script->language_count)
1394 rc = E_OUTOFMEMORY;
1395 else if (searchingFor)
1396 rc = E_INVALIDARG;
1398 *pcTags = script->language_count;
1400 for (i = 0; i < script->language_count; i++)
1402 if (i < cMaxTags)
1403 pLanguageTags[i] = script->languages[i].tag;
1405 if (searchingFor)
1407 if (searchingFor == script->languages[i].tag)
1409 pLanguageTags[0] = script->languages[i].tag;
1410 *pcTags = 1;
1411 rc = S_OK;
1412 break;
1417 if (script->default_language.gsub_table)
1419 if (i < cMaxTags)
1420 pLanguageTags[i] = script->default_language.tag;
1422 if (searchingFor && FAILED(rc))
1424 pLanguageTags[0] = script->default_language.tag;
1426 i++;
1427 *pcTags = (*pcTags) + 1;
1430 return rc;
1434 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1436 int i;
1438 if (language->gsub_table)
1440 const OT_LangSys *lang = language->gsub_table;
1441 const GSUB_Header *header = (const GSUB_Header *)table;
1442 const OT_FeatureList *feature_list;
1444 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1445 TRACE("%i features\n",language->feature_count);
1447 if (language->feature_count)
1449 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1451 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1453 for (i = 0; i < language->feature_count; i++)
1455 const OT_Feature *feature;
1456 int j;
1457 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1459 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]);
1460 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1461 feature = (const OT_Feature*)language->features[i].feature;
1462 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1463 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1464 for (j = 0; j < language->features[i].lookup_count; j++)
1465 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1471 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1473 int i, count;
1474 const OT_LangSys *lang = language->gpos_table;
1475 const GPOS_Header *header = (const GPOS_Header *)table;
1476 const OT_FeatureList *feature_list;
1478 if (!lang)
1479 return;
1481 count = GET_BE_WORD(lang->FeatureCount);
1482 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1484 TRACE("%i features\n",count);
1485 if (!language->feature_count)
1487 language->feature_count = count;
1489 if (language->feature_count)
1491 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1493 for (i = 0; i < language->feature_count; i++)
1495 const OT_Feature *feature;
1496 int j;
1497 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1499 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]);
1500 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1501 feature = (const OT_Feature*)language->features[i].feature;
1502 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1503 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1504 for (j = 0; j < language->features[i].lookup_count; j++)
1505 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1509 else if (count)
1511 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1513 for (i = 0; i < count; i++)
1515 const OT_Feature *feature;
1516 int j;
1517 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1518 int idx = language->feature_count + i;
1520 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]);
1521 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1522 feature = (const OT_Feature*)language->features[idx].feature;
1523 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1524 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1525 for (j = 0; j < language->features[idx].lookup_count; j++)
1526 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1528 language->feature_count += count;
1532 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1534 if (!language->feature_count)
1536 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1537 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1541 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)
1543 int i;
1544 HRESULT rc = S_OK;
1545 LoadedScript *script = NULL;
1546 LoadedLanguage *language = NULL;
1548 _initialize_script_cache(psc);
1550 for (i = 0; i < psc->script_count; i++)
1552 if (psc->scripts[i].tag == script_tag)
1554 script = &psc->scripts[i];
1555 break;
1559 if (!script)
1561 *pcTags = 0;
1562 if (!filtered)
1563 return S_OK;
1564 else
1565 return E_INVALIDARG;
1568 _initialize_language_cache(script);
1570 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1571 language = &script->default_language;
1572 else
1574 for (i = 0; i < script->language_count; i++)
1576 if (script->languages[i].tag == language_tag)
1578 language = &script->languages[i];
1579 break;
1584 if (!language)
1586 *pcTags = 0;
1587 return S_OK;
1590 _initialize_feature_cache(psc, language);
1592 *pcTags = language->feature_count;
1594 if (!searchingFor && cMaxTags < *pcTags)
1595 rc = E_OUTOFMEMORY;
1596 else if (searchingFor)
1597 rc = E_INVALIDARG;
1599 for (i = 0; i < language->feature_count; i++)
1601 if (i < cMaxTags)
1602 pFeatureTags[i] = language->features[i].tag;
1604 if (searchingFor)
1606 if (searchingFor == language->features[i].tag)
1608 pFeatureTags[0] = language->features[i].tag;
1609 *pcTags = 1;
1610 if (feature)
1611 *feature = &language->features[i];
1612 rc = S_OK;
1613 break;
1617 return rc;