usp10: Move GSUB functions to opentype.c.
[wine/multimedia.git] / dlls / usp10 / opentype.c
blobc30d5131ede333d73880d1e9f25300ed34abc1df
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 } GSUB_ScriptRecord;
122 typedef struct {
123 WORD ScriptCount;
124 GSUB_ScriptRecord ScriptRecord[1];
125 } GSUB_ScriptList;
127 typedef struct {
128 CHAR LangSysTag[4];
129 WORD LangSys;
130 } GSUB_LangSysRecord;
132 typedef struct {
133 WORD DefaultLangSys;
134 WORD LangSysCount;
135 GSUB_LangSysRecord LangSysRecord[1];
136 } GSUB_Script;
138 typedef struct {
139 WORD LookupOrder; /* Reserved */
140 WORD ReqFeatureIndex;
141 WORD FeatureCount;
142 WORD FeatureIndex[1];
143 } GSUB_LangSys;
145 typedef struct {
146 CHAR FeatureTag[4];
147 WORD Feature;
148 } GSUB_FeatureRecord;
150 typedef struct {
151 WORD FeatureCount;
152 GSUB_FeatureRecord FeatureRecord[1];
153 } GSUB_FeatureList;
155 typedef struct {
156 WORD FeatureParams; /* Reserved */
157 WORD LookupCount;
158 WORD LookupListIndex[1];
159 } GSUB_Feature;
161 typedef struct {
162 WORD LookupCount;
163 WORD Lookup[1];
164 } GSUB_LookupList;
166 typedef struct {
167 WORD LookupType;
168 WORD LookupFlag;
169 WORD SubTableCount;
170 WORD SubTable[1];
171 } GSUB_LookupTable;
173 typedef struct {
174 WORD CoverageFormat;
175 WORD GlyphCount;
176 WORD GlyphArray[1];
177 } GSUB_CoverageFormat1;
179 typedef struct {
180 WORD Start;
181 WORD End;
182 WORD StartCoverageIndex;
183 } GSUB_RangeRecord;
185 typedef struct {
186 WORD CoverageFormat;
187 WORD RangeCount;
188 GSUB_RangeRecord RangeRecord[1];
189 } GSUB_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 /**********
281 * CMAP
282 **********/
284 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
286 CMAP_Header *CMAP_Table = NULL;
287 int length;
288 int i;
290 if (!psc->CMAP_Table)
292 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
293 if (length != GDI_ERROR)
295 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
296 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
297 TRACE("Loaded cmap table of %i bytes\n",length);
299 else
300 return NULL;
303 CMAP_Table = psc->CMAP_Table;
305 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
307 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
308 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
310 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
311 if (GET_BE_WORD(format->format) == 12)
312 return format;
315 return NULL;
318 static int compare_group(const void *a, const void* b)
320 const DWORD *chr = a;
321 const CMAP_SegmentedCoverage_group *group = b;
323 if (*chr < GET_BE_DWORD(group->startCharCode))
324 return -1;
325 if (*chr > GET_BE_DWORD(group->endCharCode))
326 return 1;
327 return 0;
330 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
332 /* BMP: use gdi32 for ease */
333 if (utf32c < 0x10000)
335 WCHAR ch = utf32c;
336 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
339 if (!psc->CMAP_format12_Table)
340 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
342 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
343 *pgi = 0xffff;
344 else
345 *pgi = 0;
347 if (psc->CMAP_format12_Table)
349 CMAP_SegmentedCoverage *format = NULL;
350 CMAP_SegmentedCoverage_group *group = NULL;
352 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
354 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
355 sizeof(CMAP_SegmentedCoverage_group), compare_group);
357 if (group)
359 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
360 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
361 return 0;
364 return 0;
367 /**********
368 * GDEF
369 **********/
371 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
373 int offset;
374 WORD class = 0;
375 const GDEF_ClassDefFormat1 *cf1;
377 if (!header)
378 return 0;
380 offset = GET_BE_WORD(header->GlyphClassDef);
381 if (!offset)
382 return 0;
384 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
385 if (GET_BE_WORD(cf1->ClassFormat) == 1)
387 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
389 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
390 if (index < GET_BE_WORD(cf1->GlyphCount))
391 class = GET_BE_WORD(cf1->ClassValueArray[index]);
394 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
396 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
397 int i, top;
398 top = GET_BE_WORD(cf2->ClassRangeCount);
399 for (i = 0; i < top; i++)
401 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
402 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
404 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
405 break;
409 else
410 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
412 return class;
415 static VOID *load_gdef_table(HDC hdc)
417 VOID* GDEF_Table = NULL;
418 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
419 if (length != GDI_ERROR)
421 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
422 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
423 TRACE("Loaded GDEF table of %i bytes\n",length);
425 return GDEF_Table;
428 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
430 int i;
432 if (!psc->GDEF_Table)
433 psc->GDEF_Table = load_gdef_table(hdc);
435 for (i = 0; i < cGlyphs; i++)
437 WORD class;
438 int char_count = 0;
439 int k;
441 for (k = 0; k < cChars; k++)
442 if (pwLogClust[k] == i)
443 char_count++;
445 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
447 switch (class)
449 case 0:
450 case BaseGlyph:
451 pGlyphProp[i].sva.fClusterStart = 1;
452 pGlyphProp[i].sva.fDiacritic = 0;
453 pGlyphProp[i].sva.fZeroWidth = 0;
454 break;
455 case LigatureGlyph:
456 pGlyphProp[i].sva.fClusterStart = 1;
457 pGlyphProp[i].sva.fDiacritic = 0;
458 pGlyphProp[i].sva.fZeroWidth = 0;
459 break;
460 case MarkGlyph:
461 pGlyphProp[i].sva.fClusterStart = 0;
462 pGlyphProp[i].sva.fDiacritic = 1;
463 pGlyphProp[i].sva.fZeroWidth = 1;
464 break;
465 case ComponentGlyph:
466 pGlyphProp[i].sva.fClusterStart = 0;
467 pGlyphProp[i].sva.fDiacritic = 0;
468 pGlyphProp[i].sva.fZeroWidth = 0;
469 break;
470 default:
471 ERR("Unknown glyph class %i\n",class);
472 pGlyphProp[i].sva.fClusterStart = 1;
473 pGlyphProp[i].sva.fDiacritic = 0;
474 pGlyphProp[i].sva.fZeroWidth = 0;
477 if (char_count == 0)
478 pGlyphProp[i].sva.fClusterStart = 0;
482 /**********
483 * GSUB
484 **********/
485 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
487 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
489 const GSUB_CoverageFormat1* cf1;
491 cf1 = table;
493 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
495 int count = GET_BE_WORD(cf1->GlyphCount);
496 int i;
497 TRACE("Coverage Format 1, %i glyphs\n",count);
498 for (i = 0; i < count; i++)
499 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
500 return i;
501 return -1;
503 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
505 const GSUB_CoverageFormat2* cf2;
506 int i;
507 int count;
508 cf2 = (const GSUB_CoverageFormat2*)cf1;
510 count = GET_BE_WORD(cf2->RangeCount);
511 TRACE("Coverage Format 2, %i ranges\n",count);
512 for (i = 0; i < count; i++)
514 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
515 return -1;
516 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
517 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
519 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
520 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
523 return -1;
525 else
526 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
528 return -1;
531 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
533 int j;
534 TRACE("Single Substitution Subtable\n");
536 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
538 int offset;
539 const GSUB_SingleSubstFormat1 *ssf1;
540 offset = GET_BE_WORD(look->SubTable[j]);
541 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
542 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
544 int offset = GET_BE_WORD(ssf1->Coverage);
545 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
546 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
548 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
549 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
550 TRACE(" 0x%x\n",glyphs[glyph_index]);
551 return glyph_index + write_dir;
554 else
556 const GSUB_SingleSubstFormat2 *ssf2;
557 INT index;
558 INT offset;
560 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
561 offset = GET_BE_WORD(ssf1->Coverage);
562 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
563 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
564 TRACE(" Coverage index %i\n",index);
565 if (index != -1)
567 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
568 return GSUB_E_NOGLYPH;
570 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
571 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
572 TRACE("0x%x\n",glyphs[glyph_index]);
573 return glyph_index + write_dir;
577 return GSUB_E_NOGLYPH;
580 static INT GSUB_apply_MultipleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
582 int j;
583 TRACE("Multiple Substitution Subtable\n");
585 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
587 int offset, index;
588 const GSUB_MultipleSubstFormat1 *msf1;
589 offset = GET_BE_WORD(look->SubTable[j]);
590 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
592 offset = GET_BE_WORD(msf1->Coverage);
593 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
594 if (index != -1)
596 const GSUB_Sequence *seq;
597 int sub_count;
598 int j;
599 offset = GET_BE_WORD(msf1->Sequence[index]);
600 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
601 sub_count = GET_BE_WORD(seq->GlyphCount);
602 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
604 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
605 glyphs[j] =glyphs[j-(sub_count-1)];
607 for (j = 0; j < sub_count; j++)
608 if (write_dir < 0)
609 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
610 else
611 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
613 *glyph_count = *glyph_count + (sub_count - 1);
615 if (TRACE_ON(uniscribe))
617 for (j = 0; j < sub_count; j++)
618 TRACE(" 0x%x",glyphs[glyph_index+j]);
619 TRACE("\n");
622 return glyph_index + (sub_count * write_dir);
625 return GSUB_E_NOGLYPH;
628 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
630 int j;
631 TRACE("Alternate Substitution Subtable\n");
633 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
635 int offset;
636 const GSUB_AlternateSubstFormat1 *asf1;
637 INT index;
639 offset = GET_BE_WORD(look->SubTable[j]);
640 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
641 offset = GET_BE_WORD(asf1->Coverage);
643 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
644 if (index != -1)
646 const GSUB_AlternateSet *as;
647 offset = GET_BE_WORD(asf1->AlternateSet[index]);
648 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
649 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
650 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
651 return GSUB_E_NOGLYPH;
653 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
654 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
655 TRACE(" 0x%x\n",glyphs[glyph_index]);
656 return glyph_index + write_dir;
659 return GSUB_E_NOGLYPH;
662 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
664 int j;
666 TRACE("Ligature Substitution Subtable\n");
667 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
669 const GSUB_LigatureSubstFormat1 *lsf1;
670 int offset,index;
672 offset = GET_BE_WORD(look->SubTable[j]);
673 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
674 offset = GET_BE_WORD(lsf1->Coverage);
675 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
676 TRACE(" Coverage index %i\n",index);
677 if (index != -1)
679 const GSUB_LigatureSet *ls;
680 int k, count;
682 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
683 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
684 count = GET_BE_WORD(ls->LigatureCount);
685 TRACE(" LigatureSet has %i members\n",count);
686 for (k = 0; k < count; k++)
688 const GSUB_Ligature *lig;
689 int CompCount,l,CompIndex;
691 offset = GET_BE_WORD(ls->Ligature[k]);
692 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
693 CompCount = GET_BE_WORD(lig->CompCount) - 1;
694 CompIndex = glyph_index+write_dir;
695 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
697 int CompGlyph;
698 CompGlyph = GET_BE_WORD(lig->Component[l]);
699 if (CompGlyph != glyphs[CompIndex])
700 break;
701 CompIndex += write_dir;
703 if (l == CompCount)
705 int replaceIdx = glyph_index;
706 if (write_dir < 0)
707 replaceIdx = glyph_index - CompCount;
709 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
710 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
711 TRACE("0x%x\n",glyphs[replaceIdx]);
712 if (CompCount > 0)
714 int j;
715 for (j = replaceIdx + 1; j < *glyph_count; j++)
716 glyphs[j] =glyphs[j+CompCount];
717 *glyph_count = *glyph_count - CompCount;
719 return replaceIdx + write_dir;
724 return GSUB_E_NOGLYPH;
727 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
729 int j;
730 BOOL done = FALSE;
732 TRACE("Chaining Contextual Substitution Subtable\n");
733 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
735 const GSUB_ChainContextSubstFormat1 *ccsf1;
736 int offset;
737 int dirLookahead = write_dir;
738 int dirBacktrack = -1 * write_dir;
740 offset = GET_BE_WORD(look->SubTable[j]);
741 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
742 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
744 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
745 continue;
747 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
749 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
750 continue;
752 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
754 int k;
755 int indexGlyphs;
756 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
757 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
758 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
759 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
760 int newIndex = glyph_index;
762 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
764 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
766 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
768 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
769 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
770 break;
772 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
773 continue;
774 TRACE("Matched Backtrack\n");
776 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
778 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
779 for (k = 0; k < indexGlyphs; k++)
781 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
782 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
783 break;
785 if (k != indexGlyphs)
786 continue;
787 TRACE("Matched IndexGlyphs\n");
789 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
791 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
793 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
794 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
795 break;
797 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
798 continue;
799 TRACE("Matched LookAhead\n");
801 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
803 if (GET_BE_WORD(ccsf3_4->SubstCount))
805 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
807 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
808 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
810 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
811 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
812 if (newIndex == -1)
814 ERR("Chain failed to generate a glyph\n");
815 continue;
818 return newIndex;
820 else return GSUB_E_NOGLYPH;
823 return -1;
826 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
828 int offset;
829 const GSUB_LookupTable *look;
831 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
832 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
833 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
834 switch(GET_BE_WORD(look->LookupType))
836 case 1:
837 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
838 case 2:
839 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
840 case 3:
841 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
842 case 4:
843 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
844 case 6:
845 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
846 default:
847 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
849 return GSUB_E_NOGLYPH;
852 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
854 const GSUB_Header *header = (const GSUB_Header *)table;
855 const GSUB_LookupList *lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
857 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
860 static void GSUB_initialize_script_cache(ScriptCache *psc)
862 int i;
864 if (!psc->script_count)
866 const GSUB_ScriptList *script;
867 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
868 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
869 psc->script_count = GET_BE_WORD(script->ScriptCount);
870 TRACE("initializing %i scripts in this font\n",psc->script_count);
871 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
872 for (i = 0; i < psc->script_count; i++)
874 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
875 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]);
876 psc->scripts[i].table = ((const BYTE*)script + offset);
881 HRESULT OpenType_GSUB_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags, LPCVOID* script_table)
883 int i;
884 HRESULT rc = S_OK;
886 GSUB_initialize_script_cache(psc);
887 *pcTags = psc->script_count;
889 if (!searchingFor && cMaxTags < *pcTags)
890 rc = E_OUTOFMEMORY;
891 else if (searchingFor)
892 rc = USP_E_SCRIPT_NOT_IN_FONT;
894 for (i = 0; i < psc->script_count; i++)
896 if (i < cMaxTags)
897 pScriptTags[i] = psc->scripts[i].tag;
899 if (searchingFor)
901 if (searchingFor == psc->scripts[i].tag)
903 pScriptTags[0] = psc->scripts[i].tag;
904 *pcTags = 1;
905 if (script_table)
906 *script_table = psc->scripts[i].table;
907 rc = S_OK;
908 break;
912 return rc;
915 static void GSUB_initialize_language_cache(LoadedScript *script)
917 int i;
919 if (!script->language_count)
921 const GSUB_Script* table = script->table;
922 script->language_count = GET_BE_WORD(table->LangSysCount);
923 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
924 script->default_language.table = (const BYTE*)table + GET_BE_WORD(table->DefaultLangSys);
926 TRACE("Deflang %p, LangCount %i\n",script->default_language.table, script->language_count);
928 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
930 for (i = 0; i < script->language_count; i++)
932 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
933 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]);
934 script->languages[i].table = ((const BYTE*)table + offset);
939 HRESULT OpenType_GSUB_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags, LPCVOID* language_table)
941 int i;
942 HRESULT rc = S_OK;
943 LoadedScript *script = NULL;
945 GSUB_initialize_script_cache(psc);
947 for (i = 0; i < psc->script_count; i++)
949 if (psc->scripts[i].tag == script_tag)
951 script = &psc->scripts[i];
952 break;
956 if (!script)
957 return E_INVALIDARG;
959 GSUB_initialize_language_cache(script);
961 if (!searchingFor && cMaxTags < script->language_count)
962 rc = E_OUTOFMEMORY;
963 else if (searchingFor)
964 rc = E_INVALIDARG;
966 *pcTags = script->language_count;
968 for (i = 0; i < script->language_count; i++)
970 if (i < cMaxTags)
971 pLanguageTags[i] = script->languages[i].tag;
973 if (searchingFor)
975 if (searchingFor == script->languages[i].tag)
977 pLanguageTags[0] = script->languages[i].tag;
978 *pcTags = 1;
979 if (language_table)
980 *language_table = script->languages[i].table;
981 rc = S_OK;
982 break;
987 if (script->default_language.table)
989 if (i < cMaxTags)
990 pLanguageTags[i] = script->default_language.tag;
992 if (searchingFor && FAILED(rc))
994 pLanguageTags[0] = script->default_language.tag;
995 if (language_table)
996 *language_table = script->default_language.table;
998 i++;
999 *pcTags = (*pcTags) + 1;
1002 return rc;
1006 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1008 int i;
1010 if (!language->feature_count)
1012 const GSUB_LangSys *lang= language->table;
1013 const GSUB_Header *header = (const GSUB_Header *)table;
1014 const GSUB_FeatureList *feature_list;
1016 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1017 TRACE("%i features\n",language->feature_count);
1019 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1021 feature_list = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1023 for (i = 0; i < language->feature_count; i++)
1025 const GSUB_Feature *feature;
1026 int j;
1027 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1029 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]);
1030 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1031 feature = (const GSUB_Feature*)language->features[i].feature;
1032 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1033 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1034 for (j = 0; j < language->features[i].lookup_count; j++)
1035 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1040 HRESULT OpenType_GSUB_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)
1042 int i;
1043 HRESULT rc = S_OK;
1044 LoadedScript *script = NULL;
1045 LoadedLanguage *language = NULL;
1047 GSUB_initialize_script_cache(psc);
1049 for (i = 0; i < psc->script_count; i++)
1051 if (psc->scripts[i].tag == script_tag)
1053 script = &psc->scripts[i];
1054 break;
1058 if (!script)
1060 *pcTags = 0;
1061 if (!filtered)
1062 return S_OK;
1063 else
1064 return E_INVALIDARG;
1067 GSUB_initialize_language_cache(script);
1069 if (script->default_language.table && script->default_language.tag == language_tag)
1070 language = &script->default_language;
1071 else
1073 for (i = 0; i < script->language_count; i++)
1075 if (script->languages[i].tag == language_tag)
1077 language = &script->languages[i];
1078 break;
1083 if (!language)
1085 *pcTags = 0;
1086 return S_OK;
1089 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1091 *pcTags = language->feature_count;
1093 if (!searchingFor && cMaxTags < *pcTags)
1094 rc = E_OUTOFMEMORY;
1095 else if (searchingFor)
1096 rc = E_INVALIDARG;
1098 for (i = 0; i < language->feature_count; i++)
1100 if (i < cMaxTags)
1101 pFeatureTags[i] = language->features[i].tag;
1103 if (searchingFor)
1105 if (searchingFor == language->features[i].tag)
1107 pFeatureTags[0] = language->features[i].tag;
1108 *pcTags = 1;
1109 if (feature)
1110 *feature = &language->features[i];
1111 rc = S_OK;
1112 break;
1116 return rc;