msvcrt: Don't define type_info structures as const so we can set demangled class...
[wine.git] / dlls / usp10 / opentype.c
blob0351f4f4af80e1674e2d9917cb92928118f59ceb
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 #define round(x) (((x) < 0) ? (int)((x) - 0.5) : (int)((x) + 0.5))
48 /* These are all structures needed for the cmap format 12 table */
49 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
51 enum gpos_lookup_type
53 GPOS_LOOKUP_ADJUST_SINGLE = 0x1,
54 GPOS_LOOKUP_ADJUST_PAIR = 0x2,
55 GPOS_LOOKUP_ATTACH_CURSIVE = 0x3,
56 GPOS_LOOKUP_ATTACH_MARK_TO_BASE = 0x4,
57 GPOS_LOOKUP_ATTACH_MARK_TO_LIGATURE = 0x5,
58 GPOS_LOOKUP_ATTACH_MARK_TO_MARK = 0x6,
59 GPOS_LOOKUP_POSITION_CONTEXT = 0x7,
60 GPOS_LOOKUP_POSITION_CONTEXT_CHAINED = 0x8,
61 GPOS_LOOKUP_POSITION_EXTENSION = 0x9,
64 enum gsub_lookup_type
66 GSUB_LOOKUP_SINGLE = 0x1,
67 GSUB_LOOKUP_MULTIPLE = 0x2,
68 GSUB_LOOKUP_ALTERNATE = 0x3,
69 GSUB_LOOKUP_LIGATURE = 0x4,
70 GSUB_LOOKUP_CONTEXT = 0x5,
71 GSUB_LOOKUP_CONTEXT_CHAINED = 0x6,
72 GSUB_LOOKUP_EXTENSION = 0x7,
73 GSUB_LOOKUP_CONTEXT_CHAINED_REVERSE = 0x8,
76 typedef struct {
77 WORD platformID;
78 WORD encodingID;
79 DWORD offset;
80 } CMAP_EncodingRecord;
82 typedef struct {
83 WORD version;
84 WORD numTables;
85 CMAP_EncodingRecord tables[1];
86 } CMAP_Header;
88 typedef struct {
89 DWORD startCharCode;
90 DWORD endCharCode;
91 DWORD startGlyphID;
92 } CMAP_SegmentedCoverage_group;
94 typedef struct {
95 WORD format;
96 WORD reserved;
97 DWORD length;
98 DWORD language;
99 DWORD nGroups;
100 CMAP_SegmentedCoverage_group groups[1];
101 } CMAP_SegmentedCoverage;
103 /* These are all structures needed for the GDEF table */
104 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
106 typedef struct {
107 DWORD Version;
108 WORD GlyphClassDef;
109 WORD AttachList;
110 WORD LigCaretList;
111 WORD MarkAttachClassDef;
112 } GDEF_Header;
114 typedef struct {
115 WORD ClassFormat;
116 WORD StartGlyph;
117 WORD GlyphCount;
118 WORD ClassValueArray[1];
119 } OT_ClassDefFormat1;
121 typedef struct {
122 WORD Start;
123 WORD End;
124 WORD Class;
125 } OT_ClassRangeRecord;
127 typedef struct {
128 WORD ClassFormat;
129 WORD ClassRangeCount;
130 OT_ClassRangeRecord ClassRangeRecord[1];
131 } OT_ClassDefFormat2;
133 /* These are all structures needed for the GSUB table */
135 typedef struct {
136 DWORD version;
137 WORD ScriptList;
138 WORD FeatureList;
139 WORD LookupList;
140 } GSUB_Header;
142 typedef struct {
143 CHAR ScriptTag[4];
144 WORD Script;
145 } OT_ScriptRecord;
147 typedef struct {
148 WORD ScriptCount;
149 OT_ScriptRecord ScriptRecord[1];
150 } OT_ScriptList;
152 typedef struct {
153 CHAR LangSysTag[4];
154 WORD LangSys;
155 } OT_LangSysRecord;
157 typedef struct {
158 WORD DefaultLangSys;
159 WORD LangSysCount;
160 OT_LangSysRecord LangSysRecord[1];
161 } OT_Script;
163 typedef struct {
164 WORD LookupOrder; /* Reserved */
165 WORD ReqFeatureIndex;
166 WORD FeatureCount;
167 WORD FeatureIndex[1];
168 } OT_LangSys;
170 typedef struct {
171 CHAR FeatureTag[4];
172 WORD Feature;
173 } OT_FeatureRecord;
175 typedef struct {
176 WORD FeatureCount;
177 OT_FeatureRecord FeatureRecord[1];
178 } OT_FeatureList;
180 typedef struct {
181 WORD FeatureParams; /* Reserved */
182 WORD LookupCount;
183 WORD LookupListIndex[1];
184 } OT_Feature;
186 typedef struct {
187 WORD LookupCount;
188 WORD Lookup[1];
189 } OT_LookupList;
191 typedef struct {
192 WORD LookupType;
193 WORD LookupFlag;
194 WORD SubTableCount;
195 WORD SubTable[1];
196 } OT_LookupTable;
198 typedef struct {
199 WORD CoverageFormat;
200 WORD GlyphCount;
201 WORD GlyphArray[1];
202 } OT_CoverageFormat1;
204 typedef struct {
205 WORD Start;
206 WORD End;
207 WORD StartCoverageIndex;
208 } OT_RangeRecord;
210 typedef struct {
211 WORD CoverageFormat;
212 WORD RangeCount;
213 OT_RangeRecord RangeRecord[1];
214 } OT_CoverageFormat2;
216 typedef struct {
217 WORD SubstFormat; /* = 1 */
218 WORD Coverage;
219 WORD DeltaGlyphID;
220 } GSUB_SingleSubstFormat1;
222 typedef struct {
223 WORD SubstFormat; /* = 2 */
224 WORD Coverage;
225 WORD GlyphCount;
226 WORD Substitute[1];
227 }GSUB_SingleSubstFormat2;
229 typedef struct {
230 WORD SubstFormat; /* = 1 */
231 WORD Coverage;
232 WORD SequenceCount;
233 WORD Sequence[1];
234 }GSUB_MultipleSubstFormat1;
236 typedef struct {
237 WORD GlyphCount;
238 WORD Substitute[1];
239 }GSUB_Sequence;
241 typedef struct {
242 WORD SubstFormat; /* = 1 */
243 WORD Coverage;
244 WORD LigSetCount;
245 WORD LigatureSet[1];
246 }GSUB_LigatureSubstFormat1;
248 typedef struct {
249 WORD LigatureCount;
250 WORD Ligature[1];
251 }GSUB_LigatureSet;
253 typedef struct{
254 WORD LigGlyph;
255 WORD CompCount;
256 WORD Component[1];
257 }GSUB_Ligature;
259 typedef struct{
260 WORD SequenceIndex;
261 WORD LookupListIndex;
263 }GSUB_SubstLookupRecord;
265 typedef struct{
266 WORD SubstFormat;
267 WORD Coverage;
268 WORD SubRuleSetCount;
269 WORD SubRuleSet[1];
270 }GSUB_ContextSubstFormat1;
272 typedef struct{
273 WORD SubRuleCount;
274 WORD SubRule[1];
275 }GSUB_SubRuleSet;
277 typedef struct {
278 WORD GlyphCount;
279 WORD SubstCount;
280 WORD Input[1];
281 }GSUB_SubRule_1;
283 typedef struct {
284 GSUB_SubstLookupRecord SubstLookupRecord[1];
285 }GSUB_SubRule_2;
287 typedef struct {
288 WORD SubstFormat;
289 WORD Coverage;
290 WORD ClassDef;
291 WORD SubClassSetCnt;
292 WORD SubClassSet[1];
293 }GSUB_ContextSubstFormat2;
295 typedef struct {
296 WORD SubClassRuleCnt;
297 WORD SubClassRule[1];
298 }GSUB_SubClassSet;
300 typedef struct {
301 WORD GlyphCount;
302 WORD SubstCount;
303 WORD Class[1];
304 }GSUB_SubClassRule_1;
306 typedef struct {
307 GSUB_SubstLookupRecord SubstLookupRecord[1];
308 }GSUB_SubClassRule_2;
310 typedef struct{
311 WORD SubstFormat; /* = 1 */
312 WORD Coverage;
313 WORD ChainSubRuleSetCount;
314 WORD ChainSubRuleSet[1];
315 }GSUB_ChainContextSubstFormat1;
317 typedef struct {
318 WORD SubstFormat; /* = 2 */
319 WORD Coverage;
320 WORD BacktrackClassDef;
321 WORD InputClassDef;
322 WORD LookaheadClassDef;
323 WORD ChainSubClassSetCnt;
324 WORD ChainSubClassSet[1];
325 }GSUB_ChainContextSubstFormat2;
327 typedef struct {
328 WORD ChainSubClassRuleCnt;
329 WORD ChainSubClassRule[1];
330 }GSUB_ChainSubClassSet;
332 typedef struct {
333 WORD BacktrackGlyphCount;
334 WORD Backtrack[1];
335 }GSUB_ChainSubClassRule_1;
337 typedef struct {
338 WORD InputGlyphCount;
339 WORD Input[1];
340 }GSUB_ChainSubClassRule_2;
342 typedef struct {
343 WORD LookaheadGlyphCount;
344 WORD LookAhead[1];
345 }GSUB_ChainSubClassRule_3;
347 typedef struct {
348 WORD SubstCount;
349 GSUB_SubstLookupRecord SubstLookupRecord[1];
350 }GSUB_ChainSubClassRule_4;
352 typedef struct {
353 WORD SubstFormat; /* = 3 */
354 WORD BacktrackGlyphCount;
355 WORD Coverage[1];
356 }GSUB_ChainContextSubstFormat3_1;
358 typedef struct{
359 WORD InputGlyphCount;
360 WORD Coverage[1];
361 }GSUB_ChainContextSubstFormat3_2;
363 typedef struct{
364 WORD LookaheadGlyphCount;
365 WORD Coverage[1];
366 }GSUB_ChainContextSubstFormat3_3;
368 typedef struct{
369 WORD SubstCount;
370 GSUB_SubstLookupRecord SubstLookupRecord[1];
371 }GSUB_ChainContextSubstFormat3_4;
373 typedef struct {
374 WORD SubstFormat; /* = 1 */
375 WORD Coverage;
376 WORD AlternateSetCount;
377 WORD AlternateSet[1];
378 } GSUB_AlternateSubstFormat1;
380 typedef struct{
381 WORD GlyphCount;
382 WORD Alternate[1];
383 } GSUB_AlternateSet;
385 typedef struct {
386 WORD SubstFormat;
387 WORD ExtensionLookupType;
388 DWORD ExtensionOffset;
389 } GSUB_ExtensionPosFormat1;
391 /* These are all structures needed for the GPOS table */
393 typedef struct {
394 DWORD version;
395 WORD ScriptList;
396 WORD FeatureList;
397 WORD LookupList;
398 } GPOS_Header;
400 typedef struct {
401 WORD StartSize;
402 WORD EndSize;
403 WORD DeltaFormat;
404 WORD DeltaValue[1];
405 } OT_DeviceTable;
407 typedef struct {
408 WORD AnchorFormat;
409 WORD XCoordinate;
410 WORD YCoordinate;
411 } GPOS_AnchorFormat1;
413 typedef struct {
414 WORD AnchorFormat;
415 WORD XCoordinate;
416 WORD YCoordinate;
417 WORD AnchorPoint;
418 } GPOS_AnchorFormat2;
420 typedef struct {
421 WORD AnchorFormat;
422 WORD XCoordinate;
423 WORD YCoordinate;
424 WORD XDeviceTable;
425 WORD YDeviceTable;
426 } GPOS_AnchorFormat3;
428 typedef struct {
429 WORD XPlacement;
430 WORD YPlacement;
431 WORD XAdvance;
432 WORD YAdvance;
433 WORD XPlaDevice;
434 WORD YPlaDevice;
435 WORD XAdvDevice;
436 WORD YAdvDevice;
437 } GPOS_ValueRecord;
439 typedef struct {
440 WORD PosFormat;
441 WORD Coverage;
442 WORD ValueFormat;
443 WORD Value[1];
444 } GPOS_SinglePosFormat1;
446 typedef struct {
447 WORD PosFormat;
448 WORD Coverage;
449 WORD ValueFormat;
450 WORD ValueCount;
451 WORD Value[1];
452 } GPOS_SinglePosFormat2;
454 typedef struct {
455 WORD PosFormat;
456 WORD Coverage;
457 WORD ValueFormat1;
458 WORD ValueFormat2;
459 WORD PairSetCount;
460 WORD PairSetOffset[1];
461 } GPOS_PairPosFormat1;
463 typedef struct {
464 WORD PosFormat;
465 WORD Coverage;
466 WORD ValueFormat1;
467 WORD ValueFormat2;
468 WORD ClassDef1;
469 WORD ClassDef2;
470 WORD Class1Count;
471 WORD Class2Count;
472 WORD Class1Record[1];
473 } GPOS_PairPosFormat2;
475 typedef struct {
476 WORD SecondGlyph;
477 WORD Value1[1];
478 WORD Value2[1];
479 } GPOS_PairValueRecord;
481 typedef struct {
482 WORD PairValueCount;
483 GPOS_PairValueRecord PairValueRecord[1];
484 } GPOS_PairSet;
486 typedef struct {
487 WORD EntryAnchor;
488 WORD ExitAnchor;
489 } GPOS_EntryExitRecord;
491 typedef struct {
492 WORD PosFormat;
493 WORD Coverage;
494 WORD EntryExitCount;
495 GPOS_EntryExitRecord EntryExitRecord[1];
496 } GPOS_CursivePosFormat1;
498 typedef struct {
499 WORD PosFormat;
500 WORD MarkCoverage;
501 WORD BaseCoverage;
502 WORD ClassCount;
503 WORD MarkArray;
504 WORD BaseArray;
505 } GPOS_MarkBasePosFormat1;
507 typedef struct {
508 WORD BaseAnchor[1];
509 } GPOS_BaseRecord;
511 typedef struct {
512 WORD BaseCount;
513 GPOS_BaseRecord BaseRecord[1];
514 } GPOS_BaseArray;
516 typedef struct {
517 WORD Class;
518 WORD MarkAnchor;
519 } GPOS_MarkRecord;
521 typedef struct {
522 WORD MarkCount;
523 GPOS_MarkRecord MarkRecord[1];
524 } GPOS_MarkArray;
526 typedef struct {
527 WORD PosFormat;
528 WORD MarkCoverage;
529 WORD LigatureCoverage;
530 WORD ClassCount;
531 WORD MarkArray;
532 WORD LigatureArray;
533 } GPOS_MarkLigPosFormat1;
535 typedef struct {
536 WORD LigatureCount;
537 WORD LigatureAttach[1];
538 } GPOS_LigatureArray;
540 typedef struct {
541 WORD LigatureAnchor[1];
542 } GPOS_ComponentRecord;
544 typedef struct {
545 WORD ComponentCount;
546 GPOS_ComponentRecord ComponentRecord[1];
547 } GPOS_LigatureAttach;
549 typedef struct {
550 WORD PosFormat;
551 WORD Mark1Coverage;
552 WORD Mark2Coverage;
553 WORD ClassCount;
554 WORD Mark1Array;
555 WORD Mark2Array;
556 } GPOS_MarkMarkPosFormat1;
558 typedef struct {
559 WORD Mark2Anchor[1];
560 } GPOS_Mark2Record;
562 typedef struct {
563 WORD Mark2Count;
564 GPOS_Mark2Record Mark2Record[1];
565 } GPOS_Mark2Array;
567 typedef struct {
568 WORD SequenceIndex;
569 WORD LookupListIndex;
570 } GPOS_PosLookupRecord;
572 typedef struct {
573 WORD PosFormat;
574 WORD Coverage;
575 WORD ClassDef;
576 WORD PosClassSetCnt;
577 WORD PosClassSet[1];
578 } GPOS_ContextPosFormat2;
580 typedef struct {
581 WORD PosClassRuleCnt;
582 WORD PosClassRule[1];
583 } GPOS_PosClassSet;
585 typedef struct {
586 WORD GlyphCount;
587 WORD PosCount;
588 WORD Class[1];
589 } GPOS_PosClassRule_1;
591 typedef struct {
592 GPOS_PosLookupRecord PosLookupRecord[1];
593 } GPOS_PosClassRule_2;
595 typedef struct {
596 WORD PosFormat;
597 WORD BacktrackGlyphCount;
598 WORD Coverage[1];
599 } GPOS_ChainContextPosFormat3_1;
601 typedef struct {
602 WORD InputGlyphCount;
603 WORD Coverage[1];
604 } GPOS_ChainContextPosFormat3_2;
606 typedef struct {
607 WORD LookaheadGlyphCount;
608 WORD Coverage[1];
609 } GPOS_ChainContextPosFormat3_3;
611 typedef struct {
612 WORD PosCount;
613 GPOS_PosLookupRecord PosLookupRecord[1];
614 } GPOS_ChainContextPosFormat3_4;
616 typedef struct {
617 WORD PosFormat;
618 WORD ExtensionLookupType;
619 DWORD ExtensionOffset;
620 } GPOS_ExtensionPosFormat1;
622 /**********
623 * CMAP
624 **********/
626 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
628 CMAP_Header *CMAP_Table = NULL;
629 int length;
630 int i;
632 if (!psc->CMAP_Table)
634 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
635 if (length != GDI_ERROR)
637 psc->CMAP_Table = heap_alloc(length);
638 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
639 TRACE("Loaded cmap table of %i bytes\n",length);
641 else
642 return NULL;
645 CMAP_Table = psc->CMAP_Table;
647 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
649 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
650 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
652 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
653 if (GET_BE_WORD(format->format) == 12)
654 return format;
657 return NULL;
660 static int compare_group(const void *a, const void* b)
662 const DWORD *chr = a;
663 const CMAP_SegmentedCoverage_group *group = b;
665 if (*chr < GET_BE_DWORD(group->startCharCode))
666 return -1;
667 if (*chr > GET_BE_DWORD(group->endCharCode))
668 return 1;
669 return 0;
672 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, WORD *glyph_index, DWORD flags)
674 /* BMP: use gdi32 for ease */
675 if (utf32c < 0x10000)
677 WCHAR ch = utf32c;
678 return GetGlyphIndicesW(hdc, &ch, 1, glyph_index, flags);
681 if (!psc->CMAP_format12_Table)
682 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
684 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
685 *glyph_index = 0xffffu;
686 else
687 *glyph_index = 0u;
689 if (psc->CMAP_format12_Table)
691 CMAP_SegmentedCoverage *format = NULL;
692 CMAP_SegmentedCoverage_group *group = NULL;
694 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
696 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
697 sizeof(CMAP_SegmentedCoverage_group), compare_group);
699 if (group)
701 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
702 *glyph_index = GET_BE_DWORD(group->startGlyphID) + offset;
703 return 0;
706 return 0;
709 /**********
710 * GDEF
711 **********/
713 static WORD OT_get_glyph_class(const void *table, WORD glyph)
715 WORD class = 0;
716 const OT_ClassDefFormat1 *cf1 = table;
718 if (!table) return 0;
720 if (GET_BE_WORD(cf1->ClassFormat) == 1)
722 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
724 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
725 if (index < GET_BE_WORD(cf1->GlyphCount))
726 class = GET_BE_WORD(cf1->ClassValueArray[index]);
729 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
731 const OT_ClassDefFormat2 *cf2 = table;
732 int i, top;
733 top = GET_BE_WORD(cf2->ClassRangeCount);
734 for (i = 0; i < top; i++)
736 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
737 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
739 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
740 break;
744 else
745 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
747 return class;
750 void OpenType_GDEF_UpdateGlyphProps(ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
752 int i;
753 void *glyph_class_table = NULL;
755 if (psc->GDEF_Table)
757 const GDEF_Header *header = psc->GDEF_Table;
758 WORD offset = GET_BE_WORD( header->GlyphClassDef );
759 if (offset)
760 glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
763 for (i = 0; i < cGlyphs; i++)
765 WORD class;
766 int char_count = 0;
767 int k;
769 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
770 if (k >= 0)
772 for (; k < cChars && pwLogClust[k] == i; k++)
773 char_count++;
776 class = OT_get_glyph_class( glyph_class_table, pwGlyphs[i] );
778 switch (class)
780 case 0:
781 case BaseGlyph:
782 pGlyphProp[i].sva.fClusterStart = 1;
783 pGlyphProp[i].sva.fDiacritic = 0;
784 pGlyphProp[i].sva.fZeroWidth = 0;
785 break;
786 case LigatureGlyph:
787 pGlyphProp[i].sva.fClusterStart = 1;
788 pGlyphProp[i].sva.fDiacritic = 0;
789 pGlyphProp[i].sva.fZeroWidth = 0;
790 break;
791 case MarkGlyph:
792 pGlyphProp[i].sva.fClusterStart = 0;
793 pGlyphProp[i].sva.fDiacritic = 1;
794 pGlyphProp[i].sva.fZeroWidth = 1;
795 break;
796 case ComponentGlyph:
797 pGlyphProp[i].sva.fClusterStart = 0;
798 pGlyphProp[i].sva.fDiacritic = 0;
799 pGlyphProp[i].sva.fZeroWidth = 0;
800 break;
801 default:
802 ERR("Unknown glyph class %i\n",class);
803 pGlyphProp[i].sva.fClusterStart = 1;
804 pGlyphProp[i].sva.fDiacritic = 0;
805 pGlyphProp[i].sva.fZeroWidth = 0;
808 if (char_count == 0)
809 pGlyphProp[i].sva.fClusterStart = 0;
813 /**********
814 * GSUB
815 **********/
816 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
818 static int GSUB_is_glyph_covered(const void *table, unsigned int glyph)
820 const OT_CoverageFormat1* cf1;
822 cf1 = table;
824 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
826 int count = GET_BE_WORD(cf1->GlyphCount);
827 int i;
828 TRACE("Coverage Format 1, %i glyphs\n",count);
829 for (i = 0; i < count; i++)
830 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
831 return i;
832 return -1;
834 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
836 const OT_CoverageFormat2* cf2;
837 int i;
838 int count;
839 cf2 = (const OT_CoverageFormat2*)cf1;
841 count = GET_BE_WORD(cf2->RangeCount);
842 TRACE("Coverage Format 2, %i ranges\n",count);
843 for (i = 0; i < count; i++)
845 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
846 return -1;
847 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
848 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
850 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
851 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
854 return -1;
856 else
857 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
859 return -1;
862 static const BYTE *GSUB_get_subtable(const OT_LookupTable *look, int index)
864 int offset = GET_BE_WORD(look->SubTable[index]);
866 if (GET_BE_WORD(look->LookupType) == GSUB_LOOKUP_EXTENSION)
868 const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)look + offset);
869 if (GET_BE_WORD(ext->SubstFormat) == 1)
871 offset += GET_BE_DWORD(ext->ExtensionOffset);
873 else
875 FIXME("Unhandled Extension Substitution Format %i\n",GET_BE_WORD(ext->SubstFormat));
878 return (const BYTE *)look + offset;
881 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
883 int j;
884 TRACE("Single Substitution Subtable\n");
886 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
888 const GSUB_SingleSubstFormat1 *ssf1 = (const GSUB_SingleSubstFormat1*)GSUB_get_subtable(look, j);
889 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
891 int offset = GET_BE_WORD(ssf1->Coverage);
892 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
893 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
895 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
896 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
897 TRACE(" 0x%x\n",glyphs[glyph_index]);
898 return glyph_index + write_dir;
901 else
903 const GSUB_SingleSubstFormat2 *ssf2;
904 INT index;
905 INT offset;
907 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
908 offset = GET_BE_WORD(ssf1->Coverage);
909 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
910 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
911 TRACE(" Coverage index %i\n",index);
912 if (index != -1)
914 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
915 return GSUB_E_NOGLYPH;
917 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
918 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
919 TRACE("0x%x\n",glyphs[glyph_index]);
920 return glyph_index + write_dir;
924 return GSUB_E_NOGLYPH;
927 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
929 int j;
930 TRACE("Multiple Substitution Subtable\n");
932 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
934 int offset, index;
935 const GSUB_MultipleSubstFormat1 *msf1;
936 msf1 = (const GSUB_MultipleSubstFormat1*)GSUB_get_subtable(look, j);
938 offset = GET_BE_WORD(msf1->Coverage);
939 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
940 if (index != -1)
942 const GSUB_Sequence *seq;
943 int sub_count;
944 int j;
945 offset = GET_BE_WORD(msf1->Sequence[index]);
946 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
947 sub_count = GET_BE_WORD(seq->GlyphCount);
948 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
950 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
951 glyphs[j] =glyphs[j-(sub_count-1)];
953 for (j = 0; j < sub_count; j++)
954 if (write_dir < 0)
955 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
956 else
957 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
959 *glyph_count = *glyph_count + (sub_count - 1);
961 if (TRACE_ON(uniscribe))
963 for (j = 0; j < sub_count; j++)
964 TRACE(" 0x%x",glyphs[glyph_index+j]);
965 TRACE("\n");
968 if (write_dir > 0)
969 return glyph_index + sub_count;
970 else
971 return glyph_index - 1;
974 return GSUB_E_NOGLYPH;
977 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
979 int j;
980 TRACE("Alternate Substitution Subtable\n");
982 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
984 int offset;
985 const GSUB_AlternateSubstFormat1 *asf1;
986 INT index;
988 asf1 = (const GSUB_AlternateSubstFormat1*)GSUB_get_subtable(look, j);
989 offset = GET_BE_WORD(asf1->Coverage);
991 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
992 if (index != -1)
994 const GSUB_AlternateSet *as;
995 offset = GET_BE_WORD(asf1->AlternateSet[index]);
996 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
997 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
998 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
999 return GSUB_E_NOGLYPH;
1001 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
1002 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
1003 TRACE(" 0x%x\n",glyphs[glyph_index]);
1004 return glyph_index + write_dir;
1007 return GSUB_E_NOGLYPH;
1010 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1012 int j;
1014 TRACE("Ligature Substitution Subtable\n");
1015 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1017 const GSUB_LigatureSubstFormat1 *lsf1;
1018 int offset,index;
1020 lsf1 = (const GSUB_LigatureSubstFormat1*)GSUB_get_subtable(look, j);
1021 offset = GET_BE_WORD(lsf1->Coverage);
1022 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
1023 TRACE(" Coverage index %i\n",index);
1024 if (index != -1)
1026 const GSUB_LigatureSet *ls;
1027 int k, count;
1029 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
1030 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
1031 count = GET_BE_WORD(ls->LigatureCount);
1032 TRACE(" LigatureSet has %i members\n",count);
1033 for (k = 0; k < count; k++)
1035 const GSUB_Ligature *lig;
1036 int CompCount,l,CompIndex;
1038 offset = GET_BE_WORD(ls->Ligature[k]);
1039 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
1040 CompCount = GET_BE_WORD(lig->CompCount) - 1;
1041 CompIndex = glyph_index+write_dir;
1042 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
1044 int CompGlyph;
1045 CompGlyph = GET_BE_WORD(lig->Component[l]);
1046 if (CompGlyph != glyphs[CompIndex])
1047 break;
1048 CompIndex += write_dir;
1050 if (l == CompCount)
1052 int replaceIdx = glyph_index;
1053 if (write_dir < 0)
1054 replaceIdx = glyph_index - CompCount;
1056 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
1057 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
1058 TRACE("0x%x\n",glyphs[replaceIdx]);
1059 if (CompCount > 0)
1061 unsigned int j = replaceIdx + 1;
1062 memmove(&glyphs[j], &glyphs[j + CompCount], (*glyph_count - j) * sizeof(*glyphs));
1063 *glyph_count = *glyph_count - CompCount;
1065 return replaceIdx + write_dir;
1070 return GSUB_E_NOGLYPH;
1073 static INT GSUB_apply_ContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1075 int j;
1076 TRACE("Context Substitution Subtable\n");
1077 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1079 const GSUB_ContextSubstFormat1 *csf1;
1081 csf1 = (const GSUB_ContextSubstFormat1*)GSUB_get_subtable(look, j);
1082 if (GET_BE_WORD(csf1->SubstFormat) == 1)
1084 int offset, index;
1085 TRACE("Context Substitution Subtable: Class 1\n");
1086 offset = GET_BE_WORD(csf1->Coverage);
1087 index = GSUB_is_glyph_covered((const BYTE*)csf1+offset, glyphs[glyph_index]);
1088 TRACE(" Coverage index %i\n",index);
1089 if (index != -1)
1091 int k, count;
1092 const GSUB_SubRuleSet *srs;
1093 offset = GET_BE_WORD(csf1->SubRuleSet[index]);
1094 srs = (const GSUB_SubRuleSet*)((const BYTE*)csf1+offset);
1095 count = GET_BE_WORD(srs->SubRuleCount);
1096 TRACE(" SubRuleSet has %i members\n",count);
1097 for (k = 0; k < count; k++)
1099 const GSUB_SubRule_1 *sr;
1100 const GSUB_SubRule_2 *sr_2;
1101 unsigned int g;
1102 int g_count, l;
1103 int newIndex = glyph_index;
1105 offset = GET_BE_WORD(srs->SubRule[k]);
1106 sr = (const GSUB_SubRule_1*)((const BYTE*)srs+offset);
1107 g_count = GET_BE_WORD(sr->GlyphCount);
1108 TRACE(" SubRule has %i glyphs\n",g_count);
1110 g = glyph_index + write_dir * (g_count - 1);
1111 if (g >= *glyph_count)
1112 continue;
1114 for (l = 0; l < g_count-1; l++)
1115 if (glyphs[glyph_index + (write_dir * (l+1))] != GET_BE_WORD(sr->Input[l])) break;
1117 if (l < g_count-1)
1119 TRACE(" Rule does not match\n");
1120 continue;
1123 TRACE(" Rule matches\n");
1124 sr_2 = (const GSUB_SubRule_2 *)&sr->Input[g_count - 1];
1126 for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
1128 unsigned int lookup_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
1129 unsigned int sequence_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex);
1131 g = glyph_index + write_dir * sequence_index;
1132 if (g >= *glyph_count)
1134 WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
1135 sequence_index, glyph_index, write_dir);
1136 continue;
1139 TRACE(" SUBST: %u -> %u %u.\n", l, sequence_index, lookup_index);
1140 newIndex = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
1141 if (newIndex == GSUB_E_NOGLYPH)
1143 ERR(" Chain failed to generate a glyph\n");
1144 continue;
1147 return newIndex;
1151 else if (GET_BE_WORD(csf1->SubstFormat) == 2)
1153 const GSUB_ContextSubstFormat2 *csf2;
1154 const void *glyph_class_table;
1155 int offset, index;
1157 csf2 = (const GSUB_ContextSubstFormat2*)csf1;
1158 TRACE("Context Substitution Subtable: Class 2\n");
1159 offset = GET_BE_WORD(csf2->Coverage);
1160 index = GSUB_is_glyph_covered((const BYTE*)csf2+offset, glyphs[glyph_index]);
1161 TRACE(" Coverage index %i\n",index);
1162 if (index != -1)
1164 int k, count, class;
1165 const GSUB_SubClassSet *scs;
1167 offset = GET_BE_WORD(csf2->ClassDef);
1168 glyph_class_table = (const BYTE *)csf2 + offset;
1170 class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
1172 offset = GET_BE_WORD(csf2->SubClassSet[class]);
1173 if (offset == 0)
1175 TRACE(" No class rule table for class %i\n",class);
1176 continue;
1178 scs = (const GSUB_SubClassSet*)((const BYTE*)csf2+offset);
1179 count = GET_BE_WORD(scs->SubClassRuleCnt);
1180 TRACE(" SubClassSet has %i members\n",count);
1181 for (k = 0; k < count; k++)
1183 const GSUB_SubClassRule_1 *sr;
1184 const GSUB_SubClassRule_2 *sr_2;
1185 unsigned int g;
1186 int g_count, l;
1187 int newIndex = glyph_index;
1189 offset = GET_BE_WORD(scs->SubClassRule[k]);
1190 sr = (const GSUB_SubClassRule_1*)((const BYTE*)scs+offset);
1191 g_count = GET_BE_WORD(sr->GlyphCount);
1192 TRACE(" SubClassRule has %i glyphs classes\n",g_count);
1194 g = glyph_index + write_dir * (g_count - 1);
1195 if (g >= *glyph_count)
1196 continue;
1198 for (l = 0; l < g_count-1; l++)
1200 int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
1201 if (g_class != GET_BE_WORD(sr->Class[l])) break;
1204 if (l < g_count-1)
1206 TRACE(" Rule does not match\n");
1207 continue;
1210 TRACE(" Rule matches\n");
1211 sr_2 = (const GSUB_SubClassRule_2 *)&sr->Class[g_count - 1];
1213 for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
1215 unsigned int lookup_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
1216 unsigned int sequence_index = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex);
1218 g = glyph_index + write_dir * sequence_index;
1219 if (g >= *glyph_count)
1221 WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
1222 sequence_index, glyph_index, write_dir);
1223 continue;
1226 TRACE(" SUBST: %u -> %u %u.\n", l, sequence_index, lookup_index);
1227 newIndex = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
1228 if (newIndex == GSUB_E_NOGLYPH)
1230 ERR(" Chain failed to generate a glyph\n");
1231 continue;
1234 return newIndex;
1238 else
1239 FIXME("Unhandled Context Substitution Format %i\n", GET_BE_WORD(csf1->SubstFormat));
1241 return GSUB_E_NOGLYPH;
1244 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1246 int j;
1248 TRACE("Chaining Contextual Substitution Subtable\n");
1249 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1251 const GSUB_ChainContextSubstFormat1 *ccsf1;
1252 int offset;
1253 int dirLookahead = write_dir;
1254 int dirBacktrack = -1 * write_dir;
1256 ccsf1 = (const GSUB_ChainContextSubstFormat1*)GSUB_get_subtable(look, j);
1257 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
1259 static int once;
1260 if (!once++)
1261 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
1262 continue;
1264 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
1266 WORD offset, count;
1267 const void *backtrack_class_table;
1268 const void *input_class_table;
1269 const void *lookahead_class_table;
1270 int i;
1271 WORD class;
1273 const GSUB_ChainContextSubstFormat2 *ccsf2 = (const GSUB_ChainContextSubstFormat2*)ccsf1;
1274 const GSUB_ChainSubClassSet *csc;
1276 TRACE(" subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
1278 offset = GET_BE_WORD(ccsf2->Coverage);
1280 if (GSUB_is_glyph_covered((const BYTE*)ccsf2+offset, glyphs[glyph_index]) == -1)
1282 TRACE("Glyph not covered\n");
1283 continue;
1285 offset = GET_BE_WORD(ccsf2->BacktrackClassDef);
1286 backtrack_class_table = (const BYTE*)ccsf2+offset;
1287 offset = GET_BE_WORD(ccsf2->InputClassDef);
1288 input_class_table = (const BYTE*)ccsf2+offset;
1289 offset = GET_BE_WORD(ccsf2->LookaheadClassDef);
1290 lookahead_class_table = (const BYTE*)ccsf2+offset;
1291 count = GET_BE_WORD(ccsf2->ChainSubClassSetCnt);
1293 class = OT_get_glyph_class(input_class_table, glyphs[glyph_index]);
1294 offset = GET_BE_WORD(ccsf2->ChainSubClassSet[class]);
1296 if (offset == 0)
1298 TRACE("No rules for class\n");
1299 continue;
1302 csc = (const GSUB_ChainSubClassSet*)((BYTE*)ccsf2+offset);
1303 count = GET_BE_WORD(csc->ChainSubClassRuleCnt);
1305 TRACE("%i rules to check\n",count);
1307 for (i = 0; i < count; i++)
1309 WORD backtrack_count, input_count, lookahead_count, substitute_count;
1310 int k;
1311 const GSUB_ChainSubClassRule_1 *backtrack;
1312 const GSUB_ChainSubClassRule_2 *input;
1313 const GSUB_ChainSubClassRule_3 *lookahead;
1314 const GSUB_ChainSubClassRule_4 *substitute;
1315 int new_index = GSUB_E_NOGLYPH;
1317 offset = GET_BE_WORD(csc->ChainSubClassRule[i]);
1318 backtrack = (const GSUB_ChainSubClassRule_1 *)((BYTE *)csc + offset);
1319 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
1320 k = glyph_index + dirBacktrack * backtrack_count;
1321 if (k < 0 || k >= *glyph_count)
1322 continue;
1324 input = (const GSUB_ChainSubClassRule_2 *)&backtrack->Backtrack[backtrack_count];
1325 input_count = GET_BE_WORD(input->InputGlyphCount) - 1;
1326 k = glyph_index + write_dir * input_count;
1327 if (k < 0 || k >= *glyph_count)
1328 continue;
1330 lookahead = (const GSUB_ChainSubClassRule_3 *)&input->Input[input_count];
1331 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
1332 k = glyph_index + dirLookahead * (input_count + lookahead_count);
1333 if (k < 0 || k >= *glyph_count)
1334 continue;
1336 substitute = (const GSUB_ChainSubClassRule_4 *)&lookahead->LookAhead[lookahead_count];
1338 for (k = 0; k < backtrack_count; ++k)
1340 WORD target_class = GET_BE_WORD(backtrack->Backtrack[k]);
1341 WORD glyph_class = OT_get_glyph_class(backtrack_class_table, glyphs[glyph_index + (dirBacktrack * (k+1))]);
1342 if (target_class != glyph_class)
1343 break;
1345 if (k != backtrack_count)
1346 continue;
1347 TRACE("Matched Backtrack\n");
1349 for (k = 0; k < input_count; ++k)
1351 WORD target_class = GET_BE_WORD(input->Input[k]);
1352 WORD glyph_class = OT_get_glyph_class(input_class_table, glyphs[glyph_index + (write_dir * (k+1))]);
1353 if (target_class != glyph_class)
1354 break;
1356 if (k != input_count)
1357 continue;
1358 TRACE("Matched IndexGlyphs\n");
1360 for (k = 0; k < lookahead_count; ++k)
1362 WORD target_class = GET_BE_WORD(lookahead->LookAhead[k]);
1363 WORD glyph_class = OT_get_glyph_class(lookahead_class_table,
1364 glyphs[glyph_index + (dirLookahead * (input_count + k + 1))]);
1365 if (target_class != glyph_class)
1366 break;
1368 if (k != lookahead_count)
1369 continue;
1370 TRACE("Matched LookAhead\n");
1372 substitute_count = GET_BE_WORD(substitute->SubstCount);
1373 for (k = 0; k < substitute_count; ++k)
1375 unsigned int lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
1376 unsigned int sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex);
1377 unsigned int g = glyph_index + write_dir * sequence_index;
1379 if (g >= *glyph_count)
1381 WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
1382 sequence_index, glyph_index, write_dir);
1383 continue;
1386 TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
1387 new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
1388 if (new_index == GSUB_E_NOGLYPH)
1389 ERR("Chain failed to generate a glyph.\n");
1391 return new_index;
1394 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
1396 WORD backtrack_count, input_count, lookahead_count, substitution_count;
1397 int k;
1398 const GSUB_ChainContextSubstFormat3_1 *backtrack;
1399 const GSUB_ChainContextSubstFormat3_2 *input;
1400 const GSUB_ChainContextSubstFormat3_3 *lookahead;
1401 const GSUB_ChainContextSubstFormat3_4 *substitute;
1402 int new_index = GSUB_E_NOGLYPH;
1404 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
1406 backtrack = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
1407 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
1408 k = glyph_index + dirBacktrack * backtrack_count;
1409 if (k < 0 || k >= *glyph_count)
1410 continue;
1412 input = (const GSUB_ChainContextSubstFormat3_2 *)&backtrack->Coverage[backtrack_count];
1413 input_count = GET_BE_WORD(input->InputGlyphCount);
1414 k = glyph_index + write_dir * (input_count - 1);
1415 if (k < 0 || k >= *glyph_count)
1416 continue;
1418 lookahead = (const GSUB_ChainContextSubstFormat3_3 *)&input->Coverage[input_count];
1419 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
1420 k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
1421 if (k < 0 || k >= *glyph_count)
1422 continue;
1424 substitute = (const GSUB_ChainContextSubstFormat3_4 *)&lookahead->Coverage[lookahead_count];
1426 for (k = 0; k < backtrack_count; ++k)
1428 offset = GET_BE_WORD(backtrack->Coverage[k]);
1429 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1430 glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
1431 break;
1433 if (k != backtrack_count)
1434 continue;
1435 TRACE("Matched Backtrack\n");
1437 for (k = 0; k < input_count; ++k)
1439 offset = GET_BE_WORD(input->Coverage[k]);
1440 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1441 glyphs[glyph_index + (write_dir * k)]) == -1)
1442 break;
1444 if (k != input_count)
1445 continue;
1446 TRACE("Matched IndexGlyphs\n");
1448 for (k = 0; k < lookahead_count; ++k)
1450 offset = GET_BE_WORD(lookahead->Coverage[k]);
1451 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1452 glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
1453 break;
1455 if (k != lookahead_count)
1456 continue;
1457 TRACE("Matched LookAhead\n");
1459 substitution_count = GET_BE_WORD(substitute->SubstCount);
1460 for (k = 0; k < substitution_count; ++k)
1462 unsigned int lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
1463 unsigned int sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex);
1464 unsigned int g = glyph_index + write_dir * sequence_index;
1466 if (g >= *glyph_count)
1468 WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
1469 sequence_index, glyph_index, write_dir);
1470 continue;
1473 TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
1474 new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs, g, write_dir, glyph_count);
1475 if (new_index == GSUB_E_NOGLYPH)
1476 ERR("Chain failed to generate a glyph.\n");
1478 return new_index;
1481 return GSUB_E_NOGLYPH;
1484 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1486 int offset;
1487 enum gsub_lookup_type type;
1488 const OT_LookupTable *look;
1490 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1491 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1492 type = GET_BE_WORD(look->LookupType);
1493 TRACE("type %#x, flag %#x, subtables %u.\n", type,
1494 GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1496 if (type == GSUB_LOOKUP_EXTENSION)
1498 if (GET_BE_WORD(look->SubTableCount))
1500 const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
1501 if (GET_BE_WORD(ext->SubstFormat) == 1)
1503 type = GET_BE_WORD(ext->ExtensionLookupType);
1504 TRACE("extension type %i\n",type);
1506 else
1508 FIXME("Unhandled Extension Substitution Format %i\n",GET_BE_WORD(ext->SubstFormat));
1511 else
1513 WARN("lookup type is Extension Substitution but no extension subtable exists\n");
1516 switch(type)
1518 case GSUB_LOOKUP_SINGLE:
1519 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1520 case GSUB_LOOKUP_MULTIPLE:
1521 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1522 case GSUB_LOOKUP_ALTERNATE:
1523 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1524 case GSUB_LOOKUP_LIGATURE:
1525 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1526 case GSUB_LOOKUP_CONTEXT:
1527 return GSUB_apply_ContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1528 case GSUB_LOOKUP_CONTEXT_CHAINED:
1529 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1530 case GSUB_LOOKUP_EXTENSION:
1531 FIXME("Extension Substitution types not valid here\n");
1532 break;
1533 default:
1534 FIXME("Unhandled GSUB lookup type %#x.\n", type);
1536 return GSUB_E_NOGLYPH;
1539 int OpenType_apply_GSUB_lookup(const void *table, unsigned int lookup_index, WORD *glyphs,
1540 unsigned int glyph_index, int write_dir, int *glyph_count)
1542 const GSUB_Header *header = (const GSUB_Header *)table;
1543 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1545 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1548 /**********
1549 * GPOS
1550 **********/
1551 static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
1552 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
1553 unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
1554 GOFFSET *goffset);
1556 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1558 static const WORD mask[3] = {3,0xf,0xff};
1559 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1561 WORD format = GET_BE_WORD(DeviceTable->DeltaFormat);
1562 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1563 int value;
1565 TRACE("device table, format %#x, index %i\n", format, index);
1567 if (format < 1 || format > 3)
1569 WARN("invalid delta format %#x\n", format);
1570 return 0;
1573 index = index << format;
1574 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1575 TRACE("offset %i, value %i\n",index, value);
1576 if (value > mask[format-1]/2)
1577 value = -1 * ((mask[format-1]+1) - value);
1578 return value;
1580 return 0;
1583 static void GPOS_get_anchor_values(const void *table, POINT *pt, WORD ppem)
1585 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1587 switch (GET_BE_WORD(anchor1->AnchorFormat))
1589 case 1:
1591 TRACE("Anchor Format 1\n");
1592 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1593 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1594 break;
1596 case 2:
1598 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1599 TRACE("Anchor Format 2\n");
1600 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1601 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1602 break;
1604 case 3:
1606 int offset;
1607 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1608 TRACE("Anchor Format 3\n");
1609 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1610 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1611 offset = GET_BE_WORD(anchor3->XDeviceTable);
1612 TRACE("ppem %i\n",ppem);
1613 if (offset)
1615 const OT_DeviceTable* DeviceTableX = NULL;
1616 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1617 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1619 offset = GET_BE_WORD(anchor3->YDeviceTable);
1620 if (offset)
1622 const OT_DeviceTable* DeviceTableY = NULL;
1623 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1624 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1626 break;
1628 default:
1629 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1630 pt->x = 0;
1631 pt->y = 0;
1635 static void GPOS_convert_design_units_to_device(const OUTLINETEXTMETRICW *otm, const LOGFONTW *logfont,
1636 int desX, int desY, double *devX, double *devY)
1638 int emHeight = otm->otmTextMetrics.tmAscent + otm->otmTextMetrics.tmDescent - otm->otmTextMetrics.tmInternalLeading;
1640 TRACE("emHeight %i lfWidth %i\n",emHeight, logfont->lfWidth);
1641 *devX = (desX * emHeight) / (double)otm->otmEMSquare;
1642 *devY = (desY * emHeight) / (double)otm->otmEMSquare;
1643 if (logfont->lfWidth)
1644 FIXME("Font with lfWidth set not handled properly.\n");
1647 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1649 INT offset = 0;
1650 if (ValueFormat & 0x0001) { if (data) record->XPlacement = GET_BE_WORD(data[offset]); offset++; }
1651 if (ValueFormat & 0x0002) { if (data) record->YPlacement = GET_BE_WORD(data[offset]); offset++; }
1652 if (ValueFormat & 0x0004) { if (data) record->XAdvance = GET_BE_WORD(data[offset]); offset++; }
1653 if (ValueFormat & 0x0008) { if (data) record->YAdvance = GET_BE_WORD(data[offset]); offset++; }
1654 if (ValueFormat & 0x0010) { if (data) record->XPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1655 if (ValueFormat & 0x0020) { if (data) record->YPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1656 if (ValueFormat & 0x0040) { if (data) record->XAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1657 if (ValueFormat & 0x0080) { if (data) record->YAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1658 return offset;
1661 static void GPOS_get_value_record_offsets(const BYTE *head, GPOS_ValueRecord *ValueRecord,
1662 WORD ValueFormat, unsigned int ppem, POINT *ptPlacement, POINT *ptAdvance)
1664 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1665 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1666 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1667 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1668 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1669 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1670 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1671 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1672 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1675 static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
1677 int offset = GET_BE_WORD(look->SubTable[index]);
1679 if (GET_BE_WORD(look->LookupType) == GPOS_LOOKUP_POSITION_EXTENSION)
1681 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + offset);
1682 if (GET_BE_WORD(ext->PosFormat) == 1)
1684 offset += GET_BE_DWORD(ext->ExtensionOffset);
1686 else
1688 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1691 return (const BYTE *)look + offset;
1694 static void GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1695 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
1696 POINT *adjust, POINT *advance)
1698 int j;
1700 TRACE("Single Adjustment Positioning Subtable\n");
1702 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1704 const GPOS_SinglePosFormat1 *spf1 = (const GPOS_SinglePosFormat1*)GPOS_get_subtable(look, j);
1705 WORD offset;
1706 if (GET_BE_WORD(spf1->PosFormat) == 1)
1708 offset = GET_BE_WORD(spf1->Coverage);
1709 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1711 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1712 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1713 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1714 GPOS_get_value_record_offsets((const BYTE *)spf1, &ValueRecord, ValueFormat, ppem, adjust, advance);
1715 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1718 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1720 int index;
1721 const GPOS_SinglePosFormat2 *spf2;
1722 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1723 offset = GET_BE_WORD(spf2->Coverage);
1724 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1725 if (index != -1)
1727 int size;
1728 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1729 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1730 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1731 if (index > 0)
1733 offset = size * index;
1734 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1736 GPOS_get_value_record_offsets((const BYTE *)spf2, &ValueRecord, ValueFormat, ppem, adjust, advance);
1737 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1740 else
1741 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1745 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1746 INT ppem, POINT *adjust, POINT *advance )
1748 GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1749 GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1750 INT size;
1752 size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1753 GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1755 if (val_fmt1)
1757 GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1758 TRACE( "Glyph 1 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[0]) );
1759 TRACE( "Glyph 1 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[0]) );
1761 if (val_fmt2)
1763 GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1764 TRACE( "Glyph 2 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[1]) );
1765 TRACE( "Glyph 2 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[1]) );
1769 static int GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1770 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
1771 POINT *adjust, POINT *advance)
1773 int j;
1774 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1776 if (glyph_index + write_dir >= glyph_count)
1777 return 1;
1779 TRACE("Pair Adjustment Positioning Subtable\n");
1781 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1783 const GPOS_PairPosFormat1 *ppf1 = (const GPOS_PairPosFormat1*)GPOS_get_subtable(look, j);
1784 WORD offset;
1785 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1787 int index;
1788 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1789 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1790 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1791 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1792 offset = GET_BE_WORD(ppf1->Coverage);
1793 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1794 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1796 int k;
1797 int pair_count;
1798 const GPOS_PairSet *ps;
1799 const GPOS_PairValueRecord *pair_val_rec;
1800 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1801 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1802 pair_count = GET_BE_WORD(ps->PairValueCount);
1803 pair_val_rec = ps->PairValueRecord;
1804 for (k = 0; k < pair_count; k++)
1806 WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1807 if (glyphs[glyph_index+write_dir] == second_glyph)
1809 int next = 1;
1810 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1811 apply_pair_value(ppf1, ValueFormat1, ValueFormat2,
1812 pair_val_rec->Value1, ppem, adjust, advance);
1813 if (ValueFormat2) next++;
1814 return next;
1816 pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1820 else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1822 const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)ppf1;
1823 int index;
1824 WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1825 WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1826 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1827 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1828 WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1829 WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1831 offset = GET_BE_WORD( ppf2->Coverage );
1832 index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1833 if (index != -1)
1835 WORD class1, class2;
1836 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1837 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1838 if (class1 < class1_count && class2 < class2_count)
1840 const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1841 int next = 1;
1843 TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1845 apply_pair_value(ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, adjust, advance);
1846 if (ValueFormat2) next++;
1847 return next;
1851 else
1852 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1854 return 1;
1857 static void GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1858 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
1860 int j;
1861 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1863 if (glyph_index + write_dir >= glyph_count)
1864 return;
1866 TRACE("Cursive Attachment Positioning Subtable\n");
1868 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1870 const GPOS_CursivePosFormat1 *cpf1 = (const GPOS_CursivePosFormat1 *)GPOS_get_subtable(look, j);
1871 if (GET_BE_WORD(cpf1->PosFormat) == 1)
1873 int index_exit, index_entry;
1874 WORD offset = GET_BE_WORD( cpf1->Coverage );
1875 index_exit = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index]);
1876 if (index_exit != -1 && cpf1->EntryExitRecord[index_exit].ExitAnchor!= 0)
1878 index_entry = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index+write_dir]);
1879 if (index_entry != -1 && cpf1->EntryExitRecord[index_entry].EntryAnchor != 0)
1881 POINT exit_pt, entry_pt;
1882 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_exit].ExitAnchor);
1883 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
1884 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
1885 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
1886 TRACE("Found linkage %x[%s] %x[%s]\n",glyphs[glyph_index], wine_dbgstr_point(&exit_pt), glyphs[glyph_index+write_dir], wine_dbgstr_point(&entry_pt));
1887 pt->x = entry_pt.x - exit_pt.x;
1888 pt->y = entry_pt.y - exit_pt.y;
1889 return;
1893 else
1894 FIXME("Cursive Attachment Positioning: Format %i Unhandled\n",GET_BE_WORD(cpf1->PosFormat));
1896 return;
1899 static int GPOS_apply_MarkToBase(const ScriptCache *script_cache, const OT_LookupTable *look,
1900 const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, unsigned int glyph_index,
1901 unsigned int glyph_count, unsigned int ppem, POINT *pt)
1903 int j;
1904 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1905 const void *glyph_class_table = NULL;
1906 int rc = -1;
1908 if (script_cache->GDEF_Table)
1910 const GDEF_Header *header = script_cache->GDEF_Table;
1911 WORD offset = GET_BE_WORD( header->GlyphClassDef );
1912 if (offset)
1913 glyph_class_table = (const BYTE *)script_cache->GDEF_Table + offset;
1916 TRACE("MarkToBase Attachment Positioning Subtable\n");
1918 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1920 const GPOS_MarkBasePosFormat1 *mbpf1 = (const GPOS_MarkBasePosFormat1 *)GPOS_get_subtable(look, j);
1921 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1923 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1924 int mark_index;
1925 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1926 if (mark_index != -1)
1928 int base_index;
1929 int base_glyph = glyph_index - write_dir;
1931 if (glyph_class_table)
1933 while (OT_get_glyph_class(glyph_class_table, glyphs[base_glyph]) == MarkGlyph && base_glyph > 0 && base_glyph < glyph_count)
1934 base_glyph -= write_dir;
1937 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1938 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[base_glyph]);
1939 if (base_index != -1)
1941 const GPOS_MarkArray *ma;
1942 const GPOS_MarkRecord *mr;
1943 const GPOS_BaseArray *ba;
1944 const GPOS_BaseRecord *br;
1945 int mark_class;
1946 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1947 int baserecord_size;
1948 POINT base_pt;
1949 POINT mark_pt;
1950 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[base_glyph], base_index);
1951 offset = GET_BE_WORD(mbpf1->MarkArray);
1952 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1953 if (mark_index > GET_BE_WORD(ma->MarkCount))
1955 ERR("Mark index exceeded mark count\n");
1956 return -1;
1958 mr = &ma->MarkRecord[mark_index];
1959 mark_class = GET_BE_WORD(mr->Class);
1960 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1961 offset = GET_BE_WORD(mbpf1->BaseArray);
1962 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1963 baserecord_size = class_count * sizeof(WORD);
1964 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1965 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1966 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1967 offset = GET_BE_WORD(mr->MarkAnchor);
1968 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1969 TRACE("Offset on base is %s design units\n",wine_dbgstr_point(&base_pt));
1970 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
1971 pt->x += base_pt.x - mark_pt.x;
1972 pt->y += base_pt.y - mark_pt.y;
1973 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
1974 rc = base_glyph;
1978 else
1979 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1981 return rc;
1984 static void GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1985 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
1987 int j;
1988 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1990 TRACE("MarkToLigature Attachment Positioning Subtable\n");
1992 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1994 const GPOS_MarkLigPosFormat1 *mlpf1 = (const GPOS_MarkLigPosFormat1 *)GPOS_get_subtable(look, j);
1995 if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1997 int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1998 int mark_index;
1999 mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
2000 if (mark_index != -1)
2002 int ligature_index;
2003 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
2004 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
2005 if (ligature_index != -1)
2007 const GPOS_MarkArray *ma;
2008 const GPOS_MarkRecord *mr;
2010 const GPOS_LigatureArray *la;
2011 const GPOS_LigatureAttach *lt;
2012 int mark_class;
2013 int class_count = GET_BE_WORD(mlpf1->ClassCount);
2014 int component_count;
2015 int component_size;
2016 int i;
2017 POINT ligature_pt;
2018 POINT mark_pt;
2020 TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
2021 offset = GET_BE_WORD(mlpf1->MarkArray);
2022 ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
2023 if (mark_index > GET_BE_WORD(ma->MarkCount))
2025 ERR("Mark index exceeded mark count\n");
2026 return;
2028 mr = &ma->MarkRecord[mark_index];
2029 mark_class = GET_BE_WORD(mr->Class);
2030 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
2031 offset = GET_BE_WORD(mlpf1->LigatureArray);
2032 la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
2033 if (ligature_index > GET_BE_WORD(la->LigatureCount))
2035 ERR("Ligature index exceeded ligature count\n");
2036 return;
2038 offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
2039 lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
2041 component_count = GET_BE_WORD(lt->ComponentCount);
2042 component_size = class_count * sizeof(WORD);
2043 offset = 0;
2044 for (i = 0; i < component_count && !offset; i++)
2046 int k;
2047 const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
2048 for (k = 0; k < class_count && !offset; k++)
2049 offset = GET_BE_WORD(cr->LigatureAnchor[k]);
2050 cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
2052 if (!offset)
2054 ERR("Failed to find avalible ligature connection point\n");
2055 return;
2058 GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
2059 offset = GET_BE_WORD(mr->MarkAnchor);
2060 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
2061 TRACE("Offset on ligature is %s design units\n",wine_dbgstr_point(&ligature_pt));
2062 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
2063 pt->x += ligature_pt.x - mark_pt.x;
2064 pt->y += ligature_pt.y - mark_pt.y;
2065 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
2069 else
2070 FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
2074 static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
2075 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
2077 int j;
2078 BOOL rc = FALSE;
2079 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2081 TRACE("MarkToMark Attachment Positioning Subtable\n");
2083 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2085 const GPOS_MarkMarkPosFormat1 *mmpf1 = (const GPOS_MarkMarkPosFormat1 *)GPOS_get_subtable(look, j);
2086 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
2088 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
2089 int mark_index;
2090 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
2091 if (mark_index != -1)
2093 int mark2_index;
2094 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
2095 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
2096 if (mark2_index != -1)
2098 const GPOS_MarkArray *ma;
2099 const GPOS_MarkRecord *mr;
2100 const GPOS_Mark2Array *m2a;
2101 const GPOS_Mark2Record *m2r;
2102 int mark_class;
2103 int class_count = GET_BE_WORD(mmpf1->ClassCount);
2104 int mark2record_size;
2105 POINT mark2_pt;
2106 POINT mark_pt;
2107 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
2108 offset = GET_BE_WORD(mmpf1->Mark1Array);
2109 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
2110 if (mark_index > GET_BE_WORD(ma->MarkCount))
2112 ERR("Mark index exceeded mark count\n");
2113 return FALSE;
2115 mr = &ma->MarkRecord[mark_index];
2116 mark_class = GET_BE_WORD(mr->Class);
2117 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
2118 offset = GET_BE_WORD(mmpf1->Mark2Array);
2119 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
2120 mark2record_size = class_count * sizeof(WORD);
2121 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
2122 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
2123 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
2124 offset = GET_BE_WORD(mr->MarkAnchor);
2125 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
2126 TRACE("Offset on mark2 is %s design units\n",wine_dbgstr_point(&mark2_pt));
2127 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
2128 pt->x += mark2_pt.x - mark_pt.x;
2129 pt->y += mark2_pt.y - mark_pt.y;
2130 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
2131 rc = TRUE;
2135 else
2136 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
2138 return rc;
2141 static unsigned int GPOS_apply_ContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2142 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
2143 const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2144 GOFFSET *goffset)
2146 int j;
2147 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2149 TRACE("Contextual Positioning Subtable\n");
2151 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2153 const GPOS_ContextPosFormat2 *cpf2 = (GPOS_ContextPosFormat2*)GPOS_get_subtable(look, j);
2155 if (GET_BE_WORD(cpf2->PosFormat) == 1)
2157 static int once;
2158 if (!once++)
2159 FIXME(" TODO: subtype 1\n");
2160 continue;
2162 else if (GET_BE_WORD(cpf2->PosFormat) == 2)
2164 WORD offset = GET_BE_WORD(cpf2->Coverage);
2165 int index;
2167 TRACE("Contextual Positioning Subtable: Format 2\n");
2169 index = GSUB_is_glyph_covered((const BYTE*)cpf2+offset, glyphs[glyph_index]);
2170 TRACE("Coverage index %i\n",index);
2171 if (index != -1)
2173 int k, count, class;
2174 const GPOS_PosClassSet *pcs;
2175 const void *glyph_class_table = NULL;
2177 offset = GET_BE_WORD(cpf2->ClassDef);
2178 glyph_class_table = (const BYTE *)cpf2 + offset;
2180 class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
2182 offset = GET_BE_WORD(cpf2->PosClassSet[class]);
2183 if (offset == 0)
2185 TRACE("No class rule table for class %i\n",class);
2186 continue;
2188 pcs = (const GPOS_PosClassSet*)((const BYTE*)cpf2+offset);
2189 count = GET_BE_WORD(pcs->PosClassRuleCnt);
2190 TRACE("PosClassSet has %i members\n",count);
2191 for (k = 0; k < count; k++)
2193 const GPOS_PosClassRule_1 *pr;
2194 const GPOS_PosClassRule_2 *pr_2;
2195 unsigned int g;
2196 int g_count, l;
2198 offset = GET_BE_WORD(pcs->PosClassRule[k]);
2199 pr = (const GPOS_PosClassRule_1*)((const BYTE*)pcs+offset);
2200 g_count = GET_BE_WORD(pr->GlyphCount);
2201 TRACE("PosClassRule has %i glyphs classes\n",g_count);
2203 g = glyph_index + write_dir * (g_count - 1);
2204 if (g >= glyph_count)
2205 continue;
2207 for (l = 0; l < g_count-1; l++)
2209 int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
2210 if (g_class != GET_BE_WORD(pr->Class[l])) break;
2213 if (l < g_count-1)
2215 TRACE("Rule does not match\n");
2216 continue;
2219 TRACE("Rule matches\n");
2220 pr_2 = (const GPOS_PosClassRule_2 *)&pr->Class[g_count - 1];
2222 for (l = 0; l < GET_BE_WORD(pr->PosCount); l++)
2224 unsigned int lookup_index = GET_BE_WORD(pr_2->PosLookupRecord[l].LookupListIndex);
2225 unsigned int sequence_index = GET_BE_WORD(pr_2->PosLookupRecord[l].SequenceIndex);
2227 g = glyph_index + write_dir * sequence_index;
2228 if (g >= glyph_count)
2230 WARN("Invalid sequence index %u (glyph index %u, write dir %d).\n",
2231 sequence_index, glyph_index, write_dir);
2232 continue;
2235 TRACE("Position: %u -> %u %u.\n", l, sequence_index, lookup_index);
2237 GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance,
2238 lookup, lookup_index, glyphs, g, glyph_count, goffset);
2240 return 1;
2244 TRACE("Not covered\n");
2245 continue;
2247 else if (GET_BE_WORD(cpf2->PosFormat) == 3)
2249 static int once;
2250 if (!once++)
2251 FIXME(" TODO: subtype 3\n");
2252 continue;
2254 else
2255 FIXME("Unhandled Contextual Positioning Format %i\n",GET_BE_WORD(cpf2->PosFormat));
2257 return 1;
2260 static unsigned int GPOS_apply_ChainContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2261 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
2262 const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2263 GOFFSET *goffset)
2265 int j;
2266 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2268 TRACE("Chaining Contextual Positioning Subtable\n");
2270 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2272 int offset;
2273 const GPOS_ChainContextPosFormat3_1 *backtrack = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
2274 int dirLookahead = write_dir;
2275 int dirBacktrack = -1 * write_dir;
2277 if (GET_BE_WORD(backtrack->PosFormat) == 1)
2279 static int once;
2280 if (!once++)
2281 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
2282 continue;
2284 else if (GET_BE_WORD(backtrack->PosFormat) == 2)
2286 static int once;
2287 if (!once++)
2288 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
2289 continue;
2291 else if (GET_BE_WORD(backtrack->PosFormat) == 3)
2293 WORD backtrack_count, input_count, lookahead_count, positioning_count;
2294 int k;
2295 const GPOS_ChainContextPosFormat3_2 *input;
2296 const GPOS_ChainContextPosFormat3_3 *lookahead;
2297 const GPOS_ChainContextPosFormat3_4 *positioning;
2299 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
2301 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
2302 k = glyph_index + dirBacktrack * backtrack_count;
2303 if (k < 0 || k >= glyph_count)
2304 continue;
2306 input = (const GPOS_ChainContextPosFormat3_2 *)&backtrack->Coverage[backtrack_count];
2307 input_count = GET_BE_WORD(input->InputGlyphCount);
2308 k = glyph_index + write_dir * (input_count - 1);
2309 if (k < 0 || k >= glyph_count)
2310 continue;
2312 lookahead = (const GPOS_ChainContextPosFormat3_3 *)&input->Coverage[input_count];
2313 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
2314 k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
2315 if (k < 0 || k >= glyph_count)
2316 continue;
2318 positioning = (const GPOS_ChainContextPosFormat3_4 *)&lookahead->Coverage[lookahead_count];
2320 for (k = 0; k < backtrack_count; ++k)
2322 offset = GET_BE_WORD(backtrack->Coverage[k]);
2323 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2324 glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
2325 break;
2327 if (k != backtrack_count)
2328 continue;
2329 TRACE("Matched Backtrack\n");
2331 for (k = 0; k < input_count; ++k)
2333 offset = GET_BE_WORD(input->Coverage[k]);
2334 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2335 glyphs[glyph_index + (write_dir * k)]) == -1)
2336 break;
2338 if (k != input_count)
2339 continue;
2340 TRACE("Matched IndexGlyphs\n");
2342 for (k = 0; k < lookahead_count; ++k)
2344 offset = GET_BE_WORD(lookahead->Coverage[k]);
2345 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2346 glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
2347 break;
2349 if (k != lookahead_count)
2350 continue;
2351 TRACE("Matched LookAhead\n");
2353 if (!(positioning_count = GET_BE_WORD(positioning->PosCount)))
2354 return 1;
2356 for (k = 0; k < positioning_count; ++k)
2358 unsigned int lookup_index = GET_BE_WORD(positioning->PosLookupRecord[k].LookupListIndex);
2359 unsigned int sequence_index = GET_BE_WORD(positioning->PosLookupRecord[k].SequenceIndex);
2360 unsigned int g = glyph_index + write_dir * sequence_index;
2362 if (g >= glyph_count)
2364 WARN("Skipping invalid sequence index %u (glyph index %u, write dir %d).\n",
2365 sequence_index, glyph_index, write_dir);
2366 continue;
2369 TRACE("Position: %u -> %u %u.\n", k, sequence_index, lookup_index);
2370 GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup, lookup_index,
2371 glyphs, g, glyph_count, goffset);
2373 return input_count + lookahead_count;
2375 else
2376 FIXME("Unhandled Chaining Contextual Positioning Format %#x.\n", GET_BE_WORD(backtrack->PosFormat));
2378 return 1;
2381 static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *lpotm,
2382 const LOGFONTW *lplogfont, const SCRIPT_ANALYSIS *analysis, int *piAdvance, const OT_LookupList *lookup,
2383 unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2384 GOFFSET *pGoffset)
2386 int offset;
2387 const OT_LookupTable *look;
2388 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
2389 enum gpos_lookup_type type;
2391 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
2392 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
2393 type = GET_BE_WORD(look->LookupType);
2394 TRACE("type %#x, flag %#x, subtables %u.\n", type,
2395 GET_BE_WORD(look->LookupFlag), GET_BE_WORD(look->SubTableCount));
2397 if (type == GPOS_LOOKUP_POSITION_EXTENSION)
2399 if (GET_BE_WORD(look->SubTableCount))
2401 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
2402 if (GET_BE_WORD(ext->PosFormat) == 1)
2404 type = GET_BE_WORD(ext->ExtensionLookupType);
2405 TRACE("extension type %i\n",type);
2407 else
2409 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
2412 else
2414 WARN("lookup type is Extension Positioning but no extension subtable exists\n");
2417 switch (type)
2419 case GPOS_LOOKUP_ADJUST_SINGLE:
2421 double devX, devY;
2422 POINT adjust = {0,0};
2423 POINT advance = {0,0};
2424 GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
2425 if (adjust.x || adjust.y)
2427 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
2428 pGoffset[glyph_index].du += round(devX);
2429 pGoffset[glyph_index].dv += round(devY);
2431 if (advance.x || advance.y)
2433 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
2434 piAdvance[glyph_index] += round(devX);
2435 if (advance.y)
2436 FIXME("Unhandled adjustment to Y advancement\n");
2438 break;
2441 case GPOS_LOOKUP_ADJUST_PAIR:
2443 POINT advance[2]= {{0,0},{0,0}};
2444 POINT adjust[2]= {{0,0},{0,0}};
2445 double devX, devY;
2446 int index_offset;
2447 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2448 int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
2450 index_offset = GPOS_apply_PairAdjustment(look, analysis, glyphs,
2451 glyph_index, glyph_count, ppem, adjust, advance);
2452 if (adjust[0].x || adjust[0].y)
2454 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
2455 pGoffset[glyph_index].du += round(devX) * offset_sign;
2456 pGoffset[glyph_index].dv += round(devY);
2458 if (advance[0].x || advance[0].y)
2460 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
2461 piAdvance[glyph_index] += round(devX);
2463 if (adjust[1].x || adjust[1].y)
2465 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
2466 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
2467 pGoffset[glyph_index + write_dir].dv += round(devY);
2469 if (advance[1].x || advance[1].y)
2471 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
2472 piAdvance[glyph_index + write_dir] += round(devX);
2474 return index_offset;
2477 case GPOS_LOOKUP_ATTACH_CURSIVE:
2479 POINT desU = {0,0};
2480 double devX, devY;
2481 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2483 GPOS_apply_CursiveAttachment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
2484 if (desU.x || desU.y)
2486 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2487 /* Windows does not appear to apply X offsets here */
2488 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index+write_dir].dv;
2490 break;
2493 case GPOS_LOOKUP_ATTACH_MARK_TO_BASE:
2495 double devX, devY;
2496 POINT desU = {0,0};
2497 int base_index = GPOS_apply_MarkToBase(script_cache, look, analysis,
2498 glyphs, glyph_index, glyph_count, ppem, &desU);
2499 if (base_index != -1)
2501 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2502 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[base_index];
2503 else
2505 if (analysis->fLogicalOrder) devX *= -1;
2506 pGoffset[glyph_index].du = round(devX);
2508 pGoffset[glyph_index].dv = round(devY);
2510 break;
2513 case GPOS_LOOKUP_ATTACH_MARK_TO_LIGATURE:
2515 double devX, devY;
2516 POINT desU = {0,0};
2517 GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
2518 if (desU.x || desU.y)
2520 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2521 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
2522 pGoffset[glyph_index].dv = round(devY);
2524 break;
2527 case GPOS_LOOKUP_ATTACH_MARK_TO_MARK:
2529 double devX, devY;
2530 POINT desU = {0,0};
2531 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2532 if (GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU))
2534 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2535 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
2536 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
2537 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
2539 break;
2542 case GPOS_LOOKUP_POSITION_CONTEXT:
2543 return GPOS_apply_ContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
2544 lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
2546 case GPOS_LOOKUP_POSITION_CONTEXT_CHAINED:
2547 return GPOS_apply_ChainContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
2548 lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
2550 default:
2551 FIXME("Unhandled GPOS lookup type %#x.\n", type);
2553 return 1;
2556 unsigned int OpenType_apply_GPOS_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2557 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, unsigned int lookup_index,
2558 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, GOFFSET *goffset)
2560 const GPOS_Header *header = (const GPOS_Header *)script_cache->GPOS_Table;
2561 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
2563 return GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup,
2564 lookup_index, glyphs, glyph_index, glyph_count, goffset);
2567 static LoadedScript *usp10_script_cache_get_script(ScriptCache *script_cache, OPENTYPE_TAG tag)
2569 size_t i;
2571 for (i = 0; i < script_cache->script_count; ++i)
2573 if (script_cache->scripts[i].tag == tag)
2574 return &script_cache->scripts[i];
2577 return NULL;
2580 static void GSUB_initialize_script_cache(ScriptCache *psc)
2582 int i;
2584 if (psc->GSUB_Table)
2586 const OT_ScriptList *script;
2587 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
2588 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
2589 psc->script_count = GET_BE_WORD(script->ScriptCount);
2590 TRACE("initializing %li scripts in this font\n",psc->script_count);
2591 if (psc->script_count)
2593 psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
2594 for (i = 0; i < psc->script_count; i++)
2596 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2597 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]);
2598 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
2604 static void GPOS_expand_script_cache(ScriptCache *psc)
2606 int i, count;
2607 const OT_ScriptList *script;
2608 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
2609 LoadedScript *loaded_script;
2611 if (!header)
2612 return;
2614 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
2615 count = GET_BE_WORD(script->ScriptCount);
2617 if (!count)
2618 return;
2620 if (!psc->script_count)
2622 psc->script_count = count;
2623 TRACE("initializing %li scripts in this font\n",psc->script_count);
2624 if (psc->script_count)
2626 psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
2627 for (i = 0; i < psc->script_count; i++)
2629 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2630 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]);
2631 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
2635 else
2637 for (i = 0; i < count; i++)
2639 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2640 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]);
2642 if (!(loaded_script = usp10_script_cache_get_script(psc, tag)))
2644 if (!usp10_array_reserve((void **)&psc->scripts, &psc->scripts_size,
2645 psc->script_count + 1, sizeof(*psc->scripts)))
2647 ERR("Failed grow scripts array.\n");
2648 return;
2651 loaded_script = &psc->scripts[psc->script_count];
2652 ++psc->script_count;
2653 loaded_script->tag = tag;
2655 loaded_script->gpos_table = (const BYTE *)script + offset;
2660 static void _initialize_script_cache(ScriptCache *psc)
2662 if (!psc->scripts_initialized)
2664 GSUB_initialize_script_cache(psc);
2665 GPOS_expand_script_cache(psc);
2666 psc->scripts_initialized = TRUE;
2670 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
2672 int i;
2673 const LoadedScript *script;
2674 HRESULT rc = S_OK;
2676 _initialize_script_cache(psc);
2678 *pcTags = psc->script_count;
2680 if (searchingFor)
2682 if (!(script = usp10_script_cache_get_script(psc, searchingFor)))
2683 return USP_E_SCRIPT_NOT_IN_FONT;
2685 *pScriptTags = script->tag;
2686 *pcTags = 1;
2687 return S_OK;
2690 if (cMaxTags < *pcTags)
2691 rc = E_OUTOFMEMORY;
2693 cMaxTags = min(cMaxTags, psc->script_count);
2694 for (i = 0; i < cMaxTags; ++i)
2696 pScriptTags[i] = psc->scripts[i].tag;
2698 return rc;
2701 static LoadedLanguage *usp10_script_get_language(LoadedScript *script, OPENTYPE_TAG tag)
2703 size_t i;
2705 for (i = 0; i < script->language_count; ++i)
2707 if (script->languages[i].tag == tag)
2708 return &script->languages[i];
2711 return NULL;
2714 static void GSUB_initialize_language_cache(LoadedScript *script)
2716 int i;
2718 if (script->gsub_table)
2720 DWORD offset;
2721 const OT_Script* table = script->gsub_table;
2722 script->language_count = GET_BE_WORD(table->LangSysCount);
2723 offset = GET_BE_WORD(table->DefaultLangSys);
2724 if (offset)
2726 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
2727 script->default_language.gsub_table = (const BYTE*)table + offset;
2730 if (script->language_count)
2732 TRACE("Deflang %p, LangCount %li\n",script->default_language.gsub_table, script->language_count);
2734 script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
2736 for (i = 0; i < script->language_count; i++)
2738 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2739 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]);
2740 script->languages[i].gsub_table = ((const BYTE*)table + offset);
2746 static void GPOS_expand_language_cache(LoadedScript *script)
2748 int count;
2749 const OT_Script* table = script->gpos_table;
2750 LoadedLanguage *language;
2751 DWORD offset;
2753 if (!table)
2754 return;
2756 offset = GET_BE_WORD(table->DefaultLangSys);
2757 if (offset)
2758 script->default_language.gpos_table = (const BYTE*)table + offset;
2760 count = GET_BE_WORD(table->LangSysCount);
2762 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
2764 if (!count)
2765 return;
2767 if (!script->language_count)
2769 int i;
2770 script->language_count = count;
2772 script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
2774 for (i = 0; i < script->language_count; i++)
2776 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2777 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]);
2778 script->languages[i].gpos_table = ((const BYTE*)table + offset);
2781 else if (count)
2783 int i;
2784 for (i = 0; i < count; i++)
2786 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2787 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]);
2789 if (!(language = usp10_script_get_language(script, tag)))
2791 if (!usp10_array_reserve((void **)&script->languages, &script->languages_size,
2792 script->language_count + 1, sizeof(*script->languages)))
2794 ERR("Failed grow languages array.\n");
2795 return;
2798 language = &script->languages[script->language_count];
2799 ++script->language_count;
2800 language->tag = tag;
2802 language->gpos_table = (const BYTE *)table + offset;
2807 static void _initialize_language_cache(LoadedScript *script)
2809 if (!script->languages_initialized)
2811 GSUB_initialize_language_cache(script);
2812 GPOS_expand_language_cache(script);
2813 script->languages_initialized = TRUE;
2817 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2819 int i;
2820 HRESULT rc = S_OK;
2821 LoadedScript *script = NULL;
2823 _initialize_script_cache(psc);
2824 if (!(script = usp10_script_cache_get_script(psc, script_tag)))
2825 return E_INVALIDARG;
2827 _initialize_language_cache(script);
2829 if (!searchingFor && cMaxTags < script->language_count)
2830 rc = E_OUTOFMEMORY;
2831 else if (searchingFor)
2832 rc = E_INVALIDARG;
2834 *pcTags = script->language_count;
2836 for (i = 0; i < script->language_count; i++)
2838 if (i < cMaxTags)
2839 pLanguageTags[i] = script->languages[i].tag;
2841 if (searchingFor)
2843 if (searchingFor == script->languages[i].tag)
2845 pLanguageTags[0] = script->languages[i].tag;
2846 *pcTags = 1;
2847 rc = S_OK;
2848 break;
2853 if (script->default_language.gsub_table)
2855 if (i < cMaxTags)
2856 pLanguageTags[i] = script->default_language.tag;
2858 if (searchingFor && FAILED(rc))
2860 pLanguageTags[0] = script->default_language.tag;
2862 i++;
2863 *pcTags = (*pcTags) + 1;
2866 return rc;
2869 static void usp10_language_add_feature_list(LoadedLanguage *language, char table_type,
2870 const OT_LangSys *lang, const OT_FeatureList *feature_list)
2872 unsigned int count = GET_BE_WORD(lang->FeatureCount);
2873 unsigned int i, j;
2875 TRACE("table_type %#x, %u features.\n", table_type, count);
2877 if (!count)
2878 return;
2880 if (!language->feature_count)
2881 language->features = heap_alloc(count * sizeof(*language->features));
2882 else
2883 language->features = HeapReAlloc(GetProcessHeap(), 0, language->features,
2884 (language->feature_count + count) * sizeof(*language->features));
2886 for (i = 0; i < count; ++i)
2888 const OT_FeatureRecord *record;
2889 LoadedFeature *loaded_feature;
2890 const OT_Feature *feature;
2892 record = &feature_list->FeatureRecord[GET_BE_WORD(lang->FeatureIndex[i])];
2893 feature = (const OT_Feature *)((const BYTE *)feature_list + GET_BE_WORD(record->Feature));
2895 loaded_feature = &language->features[language->feature_count + i];
2896 loaded_feature->tag = MS_MAKE_TAG(record->FeatureTag[0], record->FeatureTag[1],
2897 record->FeatureTag[2], record->FeatureTag[3]);
2898 loaded_feature->tableType = table_type;
2899 loaded_feature->feature = feature;
2900 loaded_feature->lookup_count = GET_BE_WORD(feature->LookupCount);
2901 loaded_feature->lookups = heap_alloc(loaded_feature->lookup_count * sizeof(*loaded_feature->lookups));
2902 for (j = 0; j < loaded_feature->lookup_count; ++j)
2903 loaded_feature->lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2905 language->feature_count += count;
2908 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2910 const GSUB_Header *gsub_header = psc->GSUB_Table;
2911 const GPOS_Header *gpos_header = psc->GPOS_Table;
2912 const OT_FeatureList *feature_list;
2913 const OT_LangSys *lang;
2915 if (language->features_initialized)
2916 return;
2918 if ((lang = language->gsub_table))
2920 feature_list = (const OT_FeatureList *)((const BYTE *)gsub_header + GET_BE_WORD(gsub_header->FeatureList));
2921 usp10_language_add_feature_list(language, FEATURE_GSUB_TABLE, lang, feature_list);
2924 if ((lang = language->gpos_table))
2926 feature_list = (const OT_FeatureList *)((const BYTE *)gpos_header + GET_BE_WORD(gpos_header->FeatureList));
2927 usp10_language_add_feature_list(language, FEATURE_GPOS_TABLE, lang, feature_list);
2930 language->features_initialized = TRUE;
2933 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, char tableType, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
2935 int i;
2936 LoadedScript *script;
2937 HRESULT rc = S_OK;
2938 LoadedLanguage *language = NULL;
2940 _initialize_script_cache(psc);
2941 if (!(script = usp10_script_cache_get_script(psc, script_tag)))
2943 *pcTags = 0;
2944 if (!filtered)
2945 return S_OK;
2946 else
2947 return E_INVALIDARG;
2950 _initialize_language_cache(script);
2952 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2953 language = &script->default_language;
2954 else
2955 language = usp10_script_get_language(script, language_tag);
2957 if (!language)
2959 *pcTags = 0;
2960 return S_OK;
2963 _initialize_feature_cache(psc, language);
2965 if (tableType)
2967 *pcTags = 0;
2968 for (i = 0; i < language->feature_count; i++)
2969 if (language->features[i].tableType == tableType)
2970 *pcTags = (*pcTags)+1;
2972 else
2973 *pcTags = language->feature_count;
2975 if (!searchingFor && cMaxTags < *pcTags)
2976 rc = E_OUTOFMEMORY;
2977 else if (searchingFor)
2978 rc = E_INVALIDARG;
2980 for (i = 0; i < language->feature_count; i++)
2982 if (i < cMaxTags)
2984 if (!tableType || language->features[i].tableType == tableType)
2985 pFeatureTags[i] = language->features[i].tag;
2988 if (searchingFor)
2990 if ((searchingFor == language->features[i].tag) &&
2991 (!tableType || language->features[i].tableType == tableType))
2993 pFeatureTags[0] = language->features[i].tag;
2994 *pcTags = 1;
2995 if (feature)
2996 *feature = &language->features[i];
2997 rc = S_OK;
2998 break;
3002 return rc;