dsound: Get rid of the DSOUND_QueryInterface() helper.
[wine/multimedia.git] / dlls / usp10 / opentype.c
blobcc57f65b71fd37574a3dcdf7352df4f94130d4e7
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 Coverage;
331 WORD ValueFormat;
332 WORD Value[1];
333 } GPOS_SinglePosFormat1;
335 typedef struct {
336 WORD PosFormat;
337 WORD Coverage;
338 WORD ValueFormat;
339 WORD ValueCount;
340 WORD Value[1];
341 } GPOS_SinglePosFormat2;
343 typedef struct {
344 WORD PosFormat;
345 WORD Coverage;
346 WORD ValueFormat1;
347 WORD ValueFormat2;
348 WORD PairSetCount;
349 WORD PairSetOffset[1];
350 } GPOS_PairPosFormat1;
352 typedef struct {
353 WORD SecondGlyph;
354 WORD Value1[1];
355 WORD Value2[1];
356 } GPOS_PairValueRecord;
358 typedef struct {
359 WORD PairValueCount;
360 GPOS_PairValueRecord PairValueRecord[1];
361 } GPOS_PairSet;
363 typedef struct {
364 WORD PosFormat;
365 WORD MarkCoverage;
366 WORD BaseCoverage;
367 WORD ClassCount;
368 WORD MarkArray;
369 WORD BaseArray;
370 } GPOS_MarkBasePosFormat1;
372 typedef struct {
373 WORD BaseAnchor[1];
374 } GPOS_BaseRecord;
376 typedef struct {
377 WORD BaseCount;
378 GPOS_BaseRecord BaseRecord[1];
379 } GPOS_BaseArray;
381 typedef struct {
382 WORD Class;
383 WORD MarkAnchor;
384 } GPOS_MarkRecord;
386 typedef struct {
387 WORD MarkCount;
388 GPOS_MarkRecord MarkRecord[1];
389 } GPOS_MarkArray;
391 typedef struct {
392 WORD SequenceIndex;
393 WORD LookupListIndex;
394 } GPOS_PosLookupRecord;
396 typedef struct {
397 WORD PosFormat;
398 WORD BacktrackGlyphCount;
399 WORD Coverage[1];
400 } GPOS_ChainContextPosFormat3_1;
402 typedef struct {
403 WORD InputGlyphCount;
404 WORD Coverage[1];
405 } GPOS_ChainContextPosFormat3_2;
407 typedef struct {
408 WORD LookaheadGlyphCount;
409 WORD Coverage[1];
410 } GPOS_ChainContextPosFormat3_3;
412 typedef struct {
413 WORD PosCount;
414 GPOS_PosLookupRecord PosLookupRecord[1];
415 } GPOS_ChainContextPosFormat3_4;
417 /**********
418 * CMAP
419 **********/
421 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
423 CMAP_Header *CMAP_Table = NULL;
424 int length;
425 int i;
427 if (!psc->CMAP_Table)
429 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
430 if (length != GDI_ERROR)
432 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
433 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
434 TRACE("Loaded cmap table of %i bytes\n",length);
436 else
437 return NULL;
440 CMAP_Table = psc->CMAP_Table;
442 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
444 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
445 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
447 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
448 if (GET_BE_WORD(format->format) == 12)
449 return format;
452 return NULL;
455 static int compare_group(const void *a, const void* b)
457 const DWORD *chr = a;
458 const CMAP_SegmentedCoverage_group *group = b;
460 if (*chr < GET_BE_DWORD(group->startCharCode))
461 return -1;
462 if (*chr > GET_BE_DWORD(group->endCharCode))
463 return 1;
464 return 0;
467 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
469 /* BMP: use gdi32 for ease */
470 if (utf32c < 0x10000)
472 WCHAR ch = utf32c;
473 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
476 if (!psc->CMAP_format12_Table)
477 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
479 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
480 *pgi = 0xffff;
481 else
482 *pgi = 0;
484 if (psc->CMAP_format12_Table)
486 CMAP_SegmentedCoverage *format = NULL;
487 CMAP_SegmentedCoverage_group *group = NULL;
489 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
491 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
492 sizeof(CMAP_SegmentedCoverage_group), compare_group);
494 if (group)
496 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
497 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
498 return 0;
501 return 0;
504 /**********
505 * GDEF
506 **********/
508 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
510 int offset;
511 WORD class = 0;
512 const GDEF_ClassDefFormat1 *cf1;
514 if (!header)
515 return 0;
517 offset = GET_BE_WORD(header->GlyphClassDef);
518 if (!offset)
519 return 0;
521 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
522 if (GET_BE_WORD(cf1->ClassFormat) == 1)
524 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
526 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
527 if (index < GET_BE_WORD(cf1->GlyphCount))
528 class = GET_BE_WORD(cf1->ClassValueArray[index]);
531 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
533 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
534 int i, top;
535 top = GET_BE_WORD(cf2->ClassRangeCount);
536 for (i = 0; i < top; i++)
538 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
539 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
541 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
542 break;
546 else
547 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
549 return class;
552 static VOID *load_gdef_table(HDC hdc)
554 VOID* GDEF_Table = NULL;
555 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
556 if (length != GDI_ERROR)
558 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
559 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
560 TRACE("Loaded GDEF table of %i bytes\n",length);
562 return GDEF_Table;
565 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
567 int i;
569 if (!psc->GDEF_Table)
570 psc->GDEF_Table = load_gdef_table(hdc);
572 for (i = 0; i < cGlyphs; i++)
574 WORD class;
575 int char_count = 0;
576 int k;
578 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
579 if (k >= 0)
581 for (; k < cChars && pwLogClust[k] == i; k++)
582 char_count++;
585 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
587 switch (class)
589 case 0:
590 case BaseGlyph:
591 pGlyphProp[i].sva.fClusterStart = 1;
592 pGlyphProp[i].sva.fDiacritic = 0;
593 pGlyphProp[i].sva.fZeroWidth = 0;
594 break;
595 case LigatureGlyph:
596 pGlyphProp[i].sva.fClusterStart = 1;
597 pGlyphProp[i].sva.fDiacritic = 0;
598 pGlyphProp[i].sva.fZeroWidth = 0;
599 break;
600 case MarkGlyph:
601 pGlyphProp[i].sva.fClusterStart = 0;
602 pGlyphProp[i].sva.fDiacritic = 1;
603 pGlyphProp[i].sva.fZeroWidth = 1;
604 break;
605 case ComponentGlyph:
606 pGlyphProp[i].sva.fClusterStart = 0;
607 pGlyphProp[i].sva.fDiacritic = 0;
608 pGlyphProp[i].sva.fZeroWidth = 0;
609 break;
610 default:
611 ERR("Unknown glyph class %i\n",class);
612 pGlyphProp[i].sva.fClusterStart = 1;
613 pGlyphProp[i].sva.fDiacritic = 0;
614 pGlyphProp[i].sva.fZeroWidth = 0;
617 if (char_count == 0)
618 pGlyphProp[i].sva.fClusterStart = 0;
622 /**********
623 * GSUB
624 **********/
625 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
627 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
629 const OT_CoverageFormat1* cf1;
631 cf1 = table;
633 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
635 int count = GET_BE_WORD(cf1->GlyphCount);
636 int i;
637 TRACE("Coverage Format 1, %i glyphs\n",count);
638 for (i = 0; i < count; i++)
639 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
640 return i;
641 return -1;
643 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
645 const OT_CoverageFormat2* cf2;
646 int i;
647 int count;
648 cf2 = (const OT_CoverageFormat2*)cf1;
650 count = GET_BE_WORD(cf2->RangeCount);
651 TRACE("Coverage Format 2, %i ranges\n",count);
652 for (i = 0; i < count; i++)
654 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
655 return -1;
656 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
657 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
659 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
660 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
663 return -1;
665 else
666 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
668 return -1;
671 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
673 int j;
674 TRACE("Single Substitution Subtable\n");
676 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
678 int offset;
679 const GSUB_SingleSubstFormat1 *ssf1;
680 offset = GET_BE_WORD(look->SubTable[j]);
681 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
682 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
684 int offset = GET_BE_WORD(ssf1->Coverage);
685 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
686 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
688 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
689 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
690 TRACE(" 0x%x\n",glyphs[glyph_index]);
691 return glyph_index + write_dir;
694 else
696 const GSUB_SingleSubstFormat2 *ssf2;
697 INT index;
698 INT offset;
700 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
701 offset = GET_BE_WORD(ssf1->Coverage);
702 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
703 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
704 TRACE(" Coverage index %i\n",index);
705 if (index != -1)
707 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
708 return GSUB_E_NOGLYPH;
710 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
711 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
712 TRACE("0x%x\n",glyphs[glyph_index]);
713 return glyph_index + write_dir;
717 return GSUB_E_NOGLYPH;
720 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
722 int j;
723 TRACE("Multiple Substitution Subtable\n");
725 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
727 int offset, index;
728 const GSUB_MultipleSubstFormat1 *msf1;
729 offset = GET_BE_WORD(look->SubTable[j]);
730 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
732 offset = GET_BE_WORD(msf1->Coverage);
733 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
734 if (index != -1)
736 const GSUB_Sequence *seq;
737 int sub_count;
738 int j;
739 offset = GET_BE_WORD(msf1->Sequence[index]);
740 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
741 sub_count = GET_BE_WORD(seq->GlyphCount);
742 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
744 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
745 glyphs[j] =glyphs[j-(sub_count-1)];
747 for (j = 0; j < sub_count; j++)
748 if (write_dir < 0)
749 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
750 else
751 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
753 *glyph_count = *glyph_count + (sub_count - 1);
755 if (TRACE_ON(uniscribe))
757 for (j = 0; j < sub_count; j++)
758 TRACE(" 0x%x",glyphs[glyph_index+j]);
759 TRACE("\n");
762 return glyph_index + (sub_count * write_dir);
765 return GSUB_E_NOGLYPH;
768 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
770 int j;
771 TRACE("Alternate Substitution Subtable\n");
773 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
775 int offset;
776 const GSUB_AlternateSubstFormat1 *asf1;
777 INT index;
779 offset = GET_BE_WORD(look->SubTable[j]);
780 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
781 offset = GET_BE_WORD(asf1->Coverage);
783 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
784 if (index != -1)
786 const GSUB_AlternateSet *as;
787 offset = GET_BE_WORD(asf1->AlternateSet[index]);
788 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
789 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
790 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
791 return GSUB_E_NOGLYPH;
793 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
794 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
795 TRACE(" 0x%x\n",glyphs[glyph_index]);
796 return glyph_index + write_dir;
799 return GSUB_E_NOGLYPH;
802 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
804 int j;
806 TRACE("Ligature Substitution Subtable\n");
807 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
809 const GSUB_LigatureSubstFormat1 *lsf1;
810 int offset,index;
812 offset = GET_BE_WORD(look->SubTable[j]);
813 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
814 offset = GET_BE_WORD(lsf1->Coverage);
815 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
816 TRACE(" Coverage index %i\n",index);
817 if (index != -1)
819 const GSUB_LigatureSet *ls;
820 int k, count;
822 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
823 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
824 count = GET_BE_WORD(ls->LigatureCount);
825 TRACE(" LigatureSet has %i members\n",count);
826 for (k = 0; k < count; k++)
828 const GSUB_Ligature *lig;
829 int CompCount,l,CompIndex;
831 offset = GET_BE_WORD(ls->Ligature[k]);
832 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
833 CompCount = GET_BE_WORD(lig->CompCount) - 1;
834 CompIndex = glyph_index+write_dir;
835 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
837 int CompGlyph;
838 CompGlyph = GET_BE_WORD(lig->Component[l]);
839 if (CompGlyph != glyphs[CompIndex])
840 break;
841 CompIndex += write_dir;
843 if (l == CompCount)
845 int replaceIdx = glyph_index;
846 if (write_dir < 0)
847 replaceIdx = glyph_index - CompCount;
849 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
850 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
851 TRACE("0x%x\n",glyphs[replaceIdx]);
852 if (CompCount > 0)
854 int j;
855 for (j = replaceIdx + 1; j < *glyph_count; j++)
856 glyphs[j] =glyphs[j+CompCount];
857 *glyph_count = *glyph_count - CompCount;
859 return replaceIdx + write_dir;
864 return GSUB_E_NOGLYPH;
867 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
869 int j;
870 BOOL done = FALSE;
872 TRACE("Chaining Contextual Substitution Subtable\n");
873 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
875 const GSUB_ChainContextSubstFormat1 *ccsf1;
876 int offset;
877 int dirLookahead = write_dir;
878 int dirBacktrack = -1 * write_dir;
880 offset = GET_BE_WORD(look->SubTable[j]);
881 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
882 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
884 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
885 continue;
887 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
889 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
890 continue;
892 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
894 int k;
895 int indexGlyphs;
896 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
897 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
898 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
899 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
900 int newIndex = glyph_index;
902 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
904 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
906 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
908 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
909 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
910 break;
912 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
913 continue;
914 TRACE("Matched Backtrack\n");
916 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
918 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
919 for (k = 0; k < indexGlyphs; k++)
921 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
922 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
923 break;
925 if (k != indexGlyphs)
926 continue;
927 TRACE("Matched IndexGlyphs\n");
929 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
931 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
933 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
934 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
935 break;
937 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
938 continue;
939 TRACE("Matched LookAhead\n");
941 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
943 if (GET_BE_WORD(ccsf3_4->SubstCount))
945 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
947 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
948 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
950 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
951 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
952 if (newIndex == -1)
954 ERR("Chain failed to generate a glyph\n");
955 continue;
958 return newIndex;
960 else return GSUB_E_NOGLYPH;
963 return -1;
966 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
968 int offset;
969 const OT_LookupTable *look;
971 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
972 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
973 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
974 switch(GET_BE_WORD(look->LookupType))
976 case 1:
977 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
978 case 2:
979 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
980 case 3:
981 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
982 case 4:
983 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
984 case 6:
985 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
986 default:
987 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
989 return GSUB_E_NOGLYPH;
992 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
994 const GSUB_Header *header = (const GSUB_Header *)table;
995 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
997 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1000 /**********
1001 * GPOS
1002 **********/
1003 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);
1005 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1007 static const WORD mask[3] = {3,0xf,0xff};
1008 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1010 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1011 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1012 int value;
1013 TRACE("device table, format %i, index %i\n",format, index);
1014 index = index << format;
1015 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1016 TRACE("offset %i, value %i\n",index, value);
1017 if (value > mask[format-1]/2)
1018 value = -1 * ((mask[format-1]+1) - value);
1019 return value;
1021 return 0;
1024 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1026 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1028 switch (GET_BE_WORD(anchor1->AnchorFormat))
1030 case 1:
1032 TRACE("Anchor Format 1\n");
1033 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1034 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1035 break;
1037 case 2:
1039 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1040 TRACE("Anchor Format 2\n");
1041 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1042 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1043 break;
1045 case 3:
1047 int offset;
1048 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1049 TRACE("Anchor Format 3\n");
1050 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1051 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1052 offset = GET_BE_WORD(anchor3->XDeviceTable);
1053 TRACE("ppem %i\n",ppem);
1054 if (offset)
1056 const OT_DeviceTable* DeviceTableX = NULL;
1057 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1058 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1060 offset = GET_BE_WORD(anchor3->YDeviceTable);
1061 if (offset)
1063 const OT_DeviceTable* DeviceTableY = NULL;
1064 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1065 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1067 break;
1069 default:
1070 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1071 pt->x = 0;
1072 pt->y = 0;
1076 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1078 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1080 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1081 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1082 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1083 if (lplogfont->lfWidth)
1084 FIXME("Font with lfWidth set no handled properly\n");
1087 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1089 INT offset = 0;
1090 if (ValueFormat & 0x0001) record->XPlacement = GET_BE_WORD(data[offset++]);
1091 if (ValueFormat & 0x0002) record->YPlacement = GET_BE_WORD(data[offset++]);
1092 if (ValueFormat & 0x0004) record->XAdvance = GET_BE_WORD(data[offset++]);
1093 if (ValueFormat & 0x0008) record->YAdvance = GET_BE_WORD(data[offset++]);
1094 if (ValueFormat & 0x0010) record->XPlaDevice = GET_BE_WORD(data[offset++]);
1095 if (ValueFormat & 0x0020) record->YPlaDevice = GET_BE_WORD(data[offset++]);
1096 if (ValueFormat & 0x0040) record->XAdvDevice = GET_BE_WORD(data[offset++]);
1097 if (ValueFormat & 0x0080) record->YAdvDevice = GET_BE_WORD(data[offset++]);
1098 return offset;
1101 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1103 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1104 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1105 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1106 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1107 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1108 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1109 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1110 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1111 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1114 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1116 int j;
1118 TRACE("Single Adjustment Positioning Subtable\n");
1120 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1122 const GPOS_SinglePosFormat1 *spf1;
1123 WORD offset = GET_BE_WORD(look->SubTable[j]);
1124 spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1125 if (GET_BE_WORD(spf1->PosFormat) == 1)
1127 offset = GET_BE_WORD(spf1->Coverage);
1128 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1130 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1131 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1132 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1133 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1134 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1137 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1139 int index;
1140 const GPOS_SinglePosFormat2 *spf2;
1141 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1142 offset = GET_BE_WORD(spf2->Coverage);
1143 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1144 if (index != -1)
1146 int size;
1147 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1148 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1149 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1150 if (index > 0)
1152 offset = size * index;
1153 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1155 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1156 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1159 else
1160 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1164 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1166 int j;
1168 TRACE("Pair Adjustment Positioning Subtable\n");
1170 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1172 const GPOS_PairPosFormat1 *ppf1;
1173 WORD offset = GET_BE_WORD(look->SubTable[j]);
1174 ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1175 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1177 offset = GET_BE_WORD(ppf1->Coverage);
1178 if (GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]) != -1)
1180 int i;
1181 int count = GET_BE_WORD(ppf1->PairSetCount);
1182 for (i = 0; i < count; i++)
1184 int k;
1185 int pair_count;
1186 const GPOS_PairSet *ps;
1187 offset = GET_BE_WORD(ppf1->PairSetOffset[i]);
1188 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1189 pair_count = GET_BE_WORD(ps->PairValueCount);
1190 for (k = 0; k < pair_count; k++)
1192 if (glyphs[glyph_index+write_dir] == GET_BE_WORD(ps->PairValueRecord[k].SecondGlyph))
1194 GPOS_ValueRecord ValueRecord1 = {0,0,0,0,0,0,0,0};
1195 GPOS_ValueRecord ValueRecord2 = {0,0,0,0,0,0,0,0};
1196 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1197 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1199 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1201 offset = GPOS_get_value_record(ValueFormat1, ps->PairValueRecord[k].Value1, &ValueRecord1);
1202 GPOS_get_value_record(ValueFormat2, (WORD*)((const BYTE*)(ps->PairValueRecord[k].Value2)+offset), &ValueRecord2);
1203 if (ValueFormat1)
1205 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord1, ValueFormat1, ppem, &ptAdjust[0], &ptAdvance[0]);
1206 TRACE("Glyph 1 resulting cumulative offset is %i,%i design units\n",ptAdjust[0].x,ptAdjust[0].y);
1208 if (ValueFormat2)
1210 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord2, ValueFormat2, ppem, &ptAdjust[1], &ptAdvance[1]);
1211 TRACE("Glyph 2 resulting cumulative offset is %i,%i design units\n",ptAdjust[1].x,ptAdjust[1].y);
1212 return glyph_index+2;
1219 else
1220 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1222 return glyph_index+1;
1225 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)
1227 int j;
1229 TRACE("MarkToBase Attachment Positioning Subtable\n");
1231 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1233 int offset;
1234 const GPOS_MarkBasePosFormat1 *mbpf1;
1235 offset = GET_BE_WORD(look->SubTable[j]);
1236 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1237 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1239 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1240 int mark_index;
1241 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1242 if (mark_index != -1)
1244 int base_index;
1245 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1246 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1247 if (base_index != -1)
1249 const GPOS_MarkArray *ma;
1250 const GPOS_MarkRecord *mr;
1251 const GPOS_BaseArray *ba;
1252 const GPOS_BaseRecord *br;
1253 int mark_class;
1254 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1255 int baserecord_size;
1256 POINT base_pt;
1257 POINT mark_pt;
1258 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1259 offset = GET_BE_WORD(mbpf1->MarkArray);
1260 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1261 if (mark_index > GET_BE_WORD(ma->MarkCount))
1263 ERR("Mark index exeeded mark count\n");
1264 return;
1266 mr = &ma->MarkRecord[mark_index];
1267 mark_class = GET_BE_WORD(mr->Class);
1268 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1269 offset = GET_BE_WORD(mbpf1->BaseArray);
1270 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1271 baserecord_size = class_count * sizeof(WORD);
1272 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1273 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1274 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1275 offset = GET_BE_WORD(mr->MarkAnchor);
1276 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1277 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1278 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1279 pt->x += base_pt.x - mark_pt.x;
1280 pt->y += base_pt.y - mark_pt.y;
1281 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1285 else
1286 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1290 static INT GPOS_apply_ChainContextPos(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, GOFFSET *pGoffset)
1292 int j;
1294 TRACE("Chaining Contextual Positioning Subtable\n");
1296 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1298 int offset;
1299 const GPOS_ChainContextPosFormat3_1 *ccpf3;
1300 int dirLookahead = write_dir;
1301 int dirBacktrack = -1 * write_dir;
1303 offset = GET_BE_WORD(look->SubTable[j]);
1304 ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1306 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1308 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1309 continue;
1311 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1313 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1314 continue;
1316 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1318 int k;
1319 int indexGlyphs;
1320 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1321 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1322 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1324 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1326 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1328 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1329 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1330 break;
1332 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1333 continue;
1334 TRACE("Matched Backtrack\n");
1336 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)(((LPBYTE)ccpf3)+sizeof(GPOS_ChainContextPosFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccpf3->BacktrackGlyphCount)-1)));
1338 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1339 for (k = 0; k < indexGlyphs; k++)
1341 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1342 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1343 break;
1345 if (k != indexGlyphs)
1346 continue;
1347 TRACE("Matched IndexGlyphs\n");
1349 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)(((LPBYTE)ccpf3_2)+sizeof(GPOS_ChainContextPosFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_2->InputGlyphCount)-1)));
1351 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1353 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1354 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1355 break;
1357 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1358 continue;
1359 TRACE("Matched LookAhead\n");
1361 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)(((LPBYTE)ccpf3_3)+sizeof(GPOS_ChainContextPosFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)-1)));
1363 if (GET_BE_WORD(ccpf3_4->PosCount))
1365 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1367 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1368 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1370 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1371 GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count, pGoffset);
1373 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1375 else return glyph_index + 1;
1377 else
1378 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1380 return glyph_index + 1;
1383 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)
1385 int offset;
1386 const OT_LookupTable *look;
1387 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1389 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1390 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1391 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1392 switch(GET_BE_WORD(look->LookupType))
1394 case 1:
1396 double devX, devY;
1397 POINT adjust = {0,0};
1398 POINT advance = {0,0};
1399 GPOS_apply_SingleAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &adjust, &advance);
1400 if (adjust.x || adjust.y)
1402 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1403 pGoffset[glyph_index].du += (int)(devX+0.5);
1404 pGoffset[glyph_index].dv += (int)(devY+0.5);
1406 if (advance.x || advance.y)
1408 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1409 piAdvance[glyph_index] += (int)(devX+0.5);
1410 if (advance.y)
1411 FIXME("Unhandled adjustment to Y advancement\n");
1414 case 2:
1416 POINT advance[2]= {{0,0},{0,0}};
1417 POINT adjust[2]= {{0,0},{0,0}};
1418 double devX, devY;
1419 int index;
1420 index = GPOS_apply_PairAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, adjust, advance);
1421 if (adjust[0].x || adjust[0].y)
1423 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1424 pGoffset[glyph_index].du += (int)(devX+0.5);
1425 pGoffset[glyph_index].dv += (int)(devY+0.5);
1427 if (advance[0].x || advance[0].y)
1429 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1430 piAdvance[glyph_index] += (int)(devX+0.5);
1432 if (adjust[1].x || adjust[1].y)
1434 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1435 pGoffset[glyph_index + write_dir].du += (int)(devX+0.5);
1436 pGoffset[glyph_index + write_dir].dv += (int)(devY+0.5);
1438 if (advance[1].x || advance[1].y)
1440 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1441 piAdvance[glyph_index + write_dir] += (int)(devX+0.5);
1443 return index;
1445 case 4:
1447 double devX, devY;
1448 POINT desU = {0,0};
1449 GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1450 if (desU.x || desU.y)
1452 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1453 pGoffset[glyph_index].du += ((int)(devX+0.5) - piAdvance[glyph_index-1]);
1454 pGoffset[glyph_index].dv += (int)(devY+0.5);
1456 break;
1458 case 8:
1460 return GPOS_apply_ChainContextPos(lpotm, lplogfont, piAdvance, lookup, look, glyphs, glyph_index, write_dir, glyph_count, ppem, pGoffset);
1462 default:
1463 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1465 return glyph_index+1;
1468 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)
1470 const GPOS_Header *header = (const GPOS_Header *)table;
1471 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1473 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1476 static void GSUB_initialize_script_cache(ScriptCache *psc)
1478 int i;
1480 if (psc->GSUB_Table)
1482 const OT_ScriptList *script;
1483 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1484 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1485 psc->script_count = GET_BE_WORD(script->ScriptCount);
1486 TRACE("initializing %i scripts in this font\n",psc->script_count);
1487 if (psc->script_count)
1489 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1490 for (i = 0; i < psc->script_count; i++)
1492 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1493 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]);
1494 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1500 static void GPOS_expand_script_cache(ScriptCache *psc)
1502 int i, count;
1503 const OT_ScriptList *script;
1504 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1506 if (!header)
1507 return;
1509 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1510 count = GET_BE_WORD(script->ScriptCount);
1512 if (!psc->script_count)
1514 psc->script_count = count;
1515 TRACE("initializing %i scripts in this font\n",psc->script_count);
1516 if (psc->script_count)
1518 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1519 for (i = 0; i < psc->script_count; i++)
1521 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1522 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]);
1523 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1527 else
1529 for (i = 0; i < count; i++)
1531 int j;
1532 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1533 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]);
1534 for (j = 0; j < psc->script_count; j++)
1536 if (psc->scripts[j].tag == tag)
1538 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1539 break;
1542 if (j == psc->script_count)
1544 psc->script_count++;
1545 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1546 psc->scripts[j].tag = tag;
1547 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1553 static void _initialize_script_cache(ScriptCache *psc)
1555 if (!psc->script_count)
1557 GSUB_initialize_script_cache(psc);
1558 GPOS_expand_script_cache(psc);
1562 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1564 int i;
1565 HRESULT rc = S_OK;
1567 _initialize_script_cache(psc);
1569 *pcTags = psc->script_count;
1571 if (!searchingFor && cMaxTags < *pcTags)
1572 rc = E_OUTOFMEMORY;
1573 else if (searchingFor)
1574 rc = USP_E_SCRIPT_NOT_IN_FONT;
1576 for (i = 0; i < psc->script_count; i++)
1578 if (i < cMaxTags)
1579 pScriptTags[i] = psc->scripts[i].tag;
1581 if (searchingFor)
1583 if (searchingFor == psc->scripts[i].tag)
1585 pScriptTags[0] = psc->scripts[i].tag;
1586 *pcTags = 1;
1587 rc = S_OK;
1588 break;
1592 return rc;
1595 static void GSUB_initialize_language_cache(LoadedScript *script)
1597 int i;
1599 if (script->gsub_table)
1601 DWORD offset;
1602 const OT_Script* table = script->gsub_table;
1603 script->language_count = GET_BE_WORD(table->LangSysCount);
1604 offset = GET_BE_WORD(table->DefaultLangSys);
1605 if (offset)
1607 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1608 script->default_language.gsub_table = (const BYTE*)table + offset;
1611 if (script->language_count)
1613 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1615 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1617 for (i = 0; i < script->language_count; i++)
1619 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1620 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]);
1621 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1627 static void GPOS_expand_language_cache(LoadedScript *script)
1629 int count;
1630 const OT_Script* table = script->gpos_table;
1631 DWORD offset;
1633 if (!table)
1634 return;
1636 offset = GET_BE_WORD(table->DefaultLangSys);
1637 if (offset)
1638 script->default_language.gpos_table = (const BYTE*)table + offset;
1640 count = GET_BE_WORD(table->LangSysCount);
1642 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1643 if (!script->language_count)
1645 int i;
1646 script->language_count = count;
1648 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1650 for (i = 0; i < script->language_count; i++)
1652 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1653 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]);
1654 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1657 else if (count)
1659 int i,j;
1660 for (i = 0; i < count; i++)
1662 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1663 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]);
1665 for (j = 0; j < script->language_count; j++)
1667 if (script->languages[j].tag == tag)
1669 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1670 break;
1673 if (j == script->language_count)
1675 script->language_count++;
1676 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1677 script->languages[j].tag = tag;
1678 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1684 static void _initialize_language_cache(LoadedScript *script)
1686 if (!script->language_count)
1688 GSUB_initialize_language_cache(script);
1689 GPOS_expand_language_cache(script);
1693 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1695 int i;
1696 HRESULT rc = S_OK;
1697 LoadedScript *script = NULL;
1699 _initialize_script_cache(psc);
1701 for (i = 0; i < psc->script_count; i++)
1703 if (psc->scripts[i].tag == script_tag)
1705 script = &psc->scripts[i];
1706 break;
1710 if (!script)
1711 return E_INVALIDARG;
1713 _initialize_language_cache(script);
1715 if (!searchingFor && cMaxTags < script->language_count)
1716 rc = E_OUTOFMEMORY;
1717 else if (searchingFor)
1718 rc = E_INVALIDARG;
1720 *pcTags = script->language_count;
1722 for (i = 0; i < script->language_count; i++)
1724 if (i < cMaxTags)
1725 pLanguageTags[i] = script->languages[i].tag;
1727 if (searchingFor)
1729 if (searchingFor == script->languages[i].tag)
1731 pLanguageTags[0] = script->languages[i].tag;
1732 *pcTags = 1;
1733 rc = S_OK;
1734 break;
1739 if (script->default_language.gsub_table)
1741 if (i < cMaxTags)
1742 pLanguageTags[i] = script->default_language.tag;
1744 if (searchingFor && FAILED(rc))
1746 pLanguageTags[0] = script->default_language.tag;
1748 i++;
1749 *pcTags = (*pcTags) + 1;
1752 return rc;
1756 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1758 int i;
1760 if (language->gsub_table)
1762 const OT_LangSys *lang = language->gsub_table;
1763 const GSUB_Header *header = (const GSUB_Header *)table;
1764 const OT_FeatureList *feature_list;
1766 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1767 TRACE("%i features\n",language->feature_count);
1769 if (language->feature_count)
1771 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1773 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1775 for (i = 0; i < language->feature_count; i++)
1777 const OT_Feature *feature;
1778 int j;
1779 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1781 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]);
1782 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1783 feature = (const OT_Feature*)language->features[i].feature;
1784 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1785 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1786 for (j = 0; j < language->features[i].lookup_count; j++)
1787 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1793 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1795 int i, count;
1796 const OT_LangSys *lang = language->gpos_table;
1797 const GPOS_Header *header = (const GPOS_Header *)table;
1798 const OT_FeatureList *feature_list;
1800 if (!lang)
1801 return;
1803 count = GET_BE_WORD(lang->FeatureCount);
1804 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1806 TRACE("%i features\n",count);
1807 if (!language->feature_count)
1809 language->feature_count = count;
1811 if (language->feature_count)
1813 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1815 for (i = 0; i < language->feature_count; i++)
1817 const OT_Feature *feature;
1818 int j;
1819 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1821 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]);
1822 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1823 feature = (const OT_Feature*)language->features[i].feature;
1824 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1825 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1826 for (j = 0; j < language->features[i].lookup_count; j++)
1827 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1831 else if (count)
1833 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1835 for (i = 0; i < count; i++)
1837 const OT_Feature *feature;
1838 int j;
1839 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1840 int idx = language->feature_count + i;
1842 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]);
1843 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1844 feature = (const OT_Feature*)language->features[idx].feature;
1845 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1846 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1847 for (j = 0; j < language->features[idx].lookup_count; j++)
1848 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1850 language->feature_count += count;
1854 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1856 if (!language->feature_count)
1858 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1859 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1863 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)
1865 int i;
1866 HRESULT rc = S_OK;
1867 LoadedScript *script = NULL;
1868 LoadedLanguage *language = NULL;
1870 _initialize_script_cache(psc);
1872 for (i = 0; i < psc->script_count; i++)
1874 if (psc->scripts[i].tag == script_tag)
1876 script = &psc->scripts[i];
1877 break;
1881 if (!script)
1883 *pcTags = 0;
1884 if (!filtered)
1885 return S_OK;
1886 else
1887 return E_INVALIDARG;
1890 _initialize_language_cache(script);
1892 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1893 language = &script->default_language;
1894 else
1896 for (i = 0; i < script->language_count; i++)
1898 if (script->languages[i].tag == language_tag)
1900 language = &script->languages[i];
1901 break;
1906 if (!language)
1908 *pcTags = 0;
1909 return S_OK;
1912 _initialize_feature_cache(psc, language);
1914 *pcTags = language->feature_count;
1916 if (!searchingFor && cMaxTags < *pcTags)
1917 rc = E_OUTOFMEMORY;
1918 else if (searchingFor)
1919 rc = E_INVALIDARG;
1921 for (i = 0; i < language->feature_count; i++)
1923 if (i < cMaxTags)
1924 pFeatureTags[i] = language->features[i].tag;
1926 if (searchingFor)
1928 if (searchingFor == language->features[i].tag)
1930 pFeatureTags[0] = language->features[i].tag;
1931 *pcTags = 1;
1932 if (feature)
1933 *feature = &language->features[i];
1934 rc = S_OK;
1935 break;
1939 return rc;