usp10: Avoid special handling if the substitution count is 0 in GSUB_apply_ChainConte...
[wine.git] / dlls / usp10 / opentype.c
bloba3346ef43f045475829509c272daa8553fbeb76f
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 int g_count, l;
1102 int newIndex = glyph_index;
1104 offset = GET_BE_WORD(srs->SubRule[k]);
1105 sr = (const GSUB_SubRule_1*)((const BYTE*)srs+offset);
1106 g_count = GET_BE_WORD(sr->GlyphCount);
1107 TRACE(" SubRule has %i glyphs\n",g_count);
1108 for (l = 0; l < g_count-1; l++)
1109 if (glyphs[glyph_index + (write_dir * (l+1))] != GET_BE_WORD(sr->Input[l])) break;
1111 if (l < g_count-1)
1113 TRACE(" Rule does not match\n");
1114 continue;
1117 TRACE(" Rule matches\n");
1118 sr_2 = (const GSUB_SubRule_2*)((const BYTE*)sr+
1119 FIELD_OFFSET(GSUB_SubRule_1, Input[g_count-1]));
1121 for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
1123 int lookupIndex = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
1124 int SequenceIndex = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex) * write_dir;
1126 TRACE(" SUBST: %i -> %i %i\n",l, SequenceIndex, lookupIndex);
1127 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
1128 if (newIndex == GSUB_E_NOGLYPH)
1130 ERR(" Chain failed to generate a glyph\n");
1131 continue;
1134 return newIndex;
1138 else if (GET_BE_WORD(csf1->SubstFormat) == 2)
1140 const GSUB_ContextSubstFormat2 *csf2;
1141 const void *glyph_class_table;
1142 int offset, index;
1144 csf2 = (const GSUB_ContextSubstFormat2*)csf1;
1145 TRACE("Context Substitution Subtable: Class 2\n");
1146 offset = GET_BE_WORD(csf2->Coverage);
1147 index = GSUB_is_glyph_covered((const BYTE*)csf2+offset, glyphs[glyph_index]);
1148 TRACE(" Coverage index %i\n",index);
1149 if (index != -1)
1151 int k, count, class;
1152 const GSUB_SubClassSet *scs;
1154 offset = GET_BE_WORD(csf2->ClassDef);
1155 glyph_class_table = (const BYTE *)csf2 + offset;
1157 class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
1159 offset = GET_BE_WORD(csf2->SubClassSet[class]);
1160 if (offset == 0)
1162 TRACE(" No class rule table for class %i\n",class);
1163 continue;
1165 scs = (const GSUB_SubClassSet*)((const BYTE*)csf2+offset);
1166 count = GET_BE_WORD(scs->SubClassRuleCnt);
1167 TRACE(" SubClassSet has %i members\n",count);
1168 for (k = 0; k < count; k++)
1170 const GSUB_SubClassRule_1 *sr;
1171 const GSUB_SubClassRule_2 *sr_2;
1172 int g_count, l;
1173 int newIndex = glyph_index;
1175 offset = GET_BE_WORD(scs->SubClassRule[k]);
1176 sr = (const GSUB_SubClassRule_1*)((const BYTE*)scs+offset);
1177 g_count = GET_BE_WORD(sr->GlyphCount);
1178 TRACE(" SubClassRule has %i glyphs classes\n",g_count);
1179 for (l = 0; l < g_count-1; l++)
1181 int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
1182 if (g_class != GET_BE_WORD(sr->Class[l])) break;
1185 if (l < g_count-1)
1187 TRACE(" Rule does not match\n");
1188 continue;
1191 TRACE(" Rule matches\n");
1192 sr_2 = (const GSUB_SubClassRule_2*)((const BYTE*)sr+
1193 FIELD_OFFSET(GSUB_SubClassRule_1, Class[g_count-1]));
1195 for (l = 0; l < GET_BE_WORD(sr->SubstCount); l++)
1197 int lookupIndex = GET_BE_WORD(sr_2->SubstLookupRecord[l].LookupListIndex);
1198 int SequenceIndex = GET_BE_WORD(sr_2->SubstLookupRecord[l].SequenceIndex) * write_dir;
1200 TRACE(" SUBST: %i -> %i %i\n",l, SequenceIndex, lookupIndex);
1201 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
1202 if (newIndex == GSUB_E_NOGLYPH)
1204 ERR(" Chain failed to generate a glyph\n");
1205 continue;
1208 return newIndex;
1212 else
1213 FIXME("Unhandled Context Substitution Format %i\n", GET_BE_WORD(csf1->SubstFormat));
1215 return GSUB_E_NOGLYPH;
1218 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1220 int j;
1222 TRACE("Chaining Contextual Substitution Subtable\n");
1223 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1225 const GSUB_ChainContextSubstFormat1 *ccsf1;
1226 int offset;
1227 int dirLookahead = write_dir;
1228 int dirBacktrack = -1 * write_dir;
1230 ccsf1 = (const GSUB_ChainContextSubstFormat1*)GSUB_get_subtable(look, j);
1231 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
1233 static int once;
1234 if (!once++)
1235 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
1236 continue;
1238 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
1240 WORD offset, count;
1241 const void *backtrack_class_table;
1242 const void *input_class_table;
1243 const void *lookahead_class_table;
1244 int i;
1245 WORD class;
1247 const GSUB_ChainContextSubstFormat2 *ccsf2 = (const GSUB_ChainContextSubstFormat2*)ccsf1;
1248 const GSUB_ChainSubClassSet *csc;
1250 TRACE(" subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
1252 offset = GET_BE_WORD(ccsf2->Coverage);
1254 if (GSUB_is_glyph_covered((const BYTE*)ccsf2+offset, glyphs[glyph_index]) == -1)
1256 TRACE("Glyph not covered\n");
1257 continue;
1259 offset = GET_BE_WORD(ccsf2->BacktrackClassDef);
1260 backtrack_class_table = (const BYTE*)ccsf2+offset;
1261 offset = GET_BE_WORD(ccsf2->InputClassDef);
1262 input_class_table = (const BYTE*)ccsf2+offset;
1263 offset = GET_BE_WORD(ccsf2->LookaheadClassDef);
1264 lookahead_class_table = (const BYTE*)ccsf2+offset;
1265 count = GET_BE_WORD(ccsf2->ChainSubClassSetCnt);
1267 class = OT_get_glyph_class(input_class_table, glyphs[glyph_index]);
1268 offset = GET_BE_WORD(ccsf2->ChainSubClassSet[class]);
1270 if (offset == 0)
1272 TRACE("No rules for class\n");
1273 continue;
1276 csc = (const GSUB_ChainSubClassSet*)((BYTE*)ccsf2+offset);
1277 count = GET_BE_WORD(csc->ChainSubClassRuleCnt);
1279 TRACE("%i rules to check\n",count);
1281 for (i = 0; i < count; i++)
1283 WORD backtrack_count, input_count, lookahead_count, substitute_count;
1284 int k;
1285 const GSUB_ChainSubClassRule_1 *backtrack;
1286 const GSUB_ChainSubClassRule_2 *input;
1287 const GSUB_ChainSubClassRule_3 *lookahead;
1288 const GSUB_ChainSubClassRule_4 *substitute;
1289 int new_index = GSUB_E_NOGLYPH;
1291 offset = GET_BE_WORD(csc->ChainSubClassRule[i]);
1292 backtrack = (const GSUB_ChainSubClassRule_1 *)((BYTE *)csc + offset);
1293 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
1294 k = glyph_index + dirBacktrack * backtrack_count;
1295 if (k < 0 || k >= *glyph_count)
1296 continue;
1298 input = (const GSUB_ChainSubClassRule_2 *)&backtrack->Backtrack[backtrack_count];
1299 input_count = GET_BE_WORD(input->InputGlyphCount) - 1;
1300 k = glyph_index + write_dir * input_count;
1301 if (k < 0 || k >= *glyph_count)
1302 continue;
1304 lookahead = (const GSUB_ChainSubClassRule_3 *)&input->Input[input_count];
1305 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
1306 k = glyph_index + dirLookahead * (input_count + lookahead_count);
1307 if (k < 0 || k >= *glyph_count)
1308 continue;
1310 substitute = (const GSUB_ChainSubClassRule_4 *)&lookahead->LookAhead[lookahead_count];
1312 for (k = 0; k < backtrack_count; ++k)
1314 WORD target_class = GET_BE_WORD(backtrack->Backtrack[k]);
1315 WORD glyph_class = OT_get_glyph_class(backtrack_class_table, glyphs[glyph_index + (dirBacktrack * (k+1))]);
1316 if (target_class != glyph_class)
1317 break;
1319 if (k != backtrack_count)
1320 continue;
1321 TRACE("Matched Backtrack\n");
1323 for (k = 0; k < input_count; ++k)
1325 WORD target_class = GET_BE_WORD(input->Input[k]);
1326 WORD glyph_class = OT_get_glyph_class(input_class_table, glyphs[glyph_index + (write_dir * (k+1))]);
1327 if (target_class != glyph_class)
1328 break;
1330 if (k != input_count)
1331 continue;
1332 TRACE("Matched IndexGlyphs\n");
1334 for (k = 0; k < lookahead_count; ++k)
1336 WORD target_class = GET_BE_WORD(lookahead->LookAhead[k]);
1337 WORD glyph_class = OT_get_glyph_class(lookahead_class_table,
1338 glyphs[glyph_index + (dirLookahead * (input_count + k + 1))]);
1339 if (target_class != glyph_class)
1340 break;
1342 if (k != lookahead_count)
1343 continue;
1344 TRACE("Matched LookAhead\n");
1346 substitute_count = GET_BE_WORD(substitute->SubstCount);
1347 for (k = 0; k < substitute_count; ++k)
1349 WORD lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
1350 WORD sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex) * write_dir;
1352 TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
1353 new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs,
1354 glyph_index + sequence_index, write_dir, glyph_count);
1355 if (new_index == GSUB_E_NOGLYPH)
1356 ERR("Chain failed to generate a glyph.\n");
1358 return new_index;
1361 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
1363 WORD backtrack_count, input_count, lookahead_count, substitution_count;
1364 int k;
1365 const GSUB_ChainContextSubstFormat3_1 *backtrack;
1366 const GSUB_ChainContextSubstFormat3_2 *input;
1367 const GSUB_ChainContextSubstFormat3_3 *lookahead;
1368 const GSUB_ChainContextSubstFormat3_4 *substitute;
1369 int new_index = GSUB_E_NOGLYPH;
1371 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
1373 backtrack = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
1374 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
1375 k = glyph_index + dirBacktrack * backtrack_count;
1376 if (k < 0 || k >= *glyph_count)
1377 continue;
1379 input = (const GSUB_ChainContextSubstFormat3_2 *)&backtrack->Coverage[backtrack_count];
1380 input_count = GET_BE_WORD(input->InputGlyphCount);
1381 k = glyph_index + write_dir * (input_count - 1);
1382 if (k < 0 || k >= *glyph_count)
1383 continue;
1385 lookahead = (const GSUB_ChainContextSubstFormat3_3 *)&input->Coverage[input_count];
1386 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
1387 k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
1388 if (k < 0 || k >= *glyph_count)
1389 continue;
1391 substitute = (const GSUB_ChainContextSubstFormat3_4 *)&lookahead->Coverage[lookahead_count];
1393 for (k = 0; k < backtrack_count; ++k)
1395 offset = GET_BE_WORD(backtrack->Coverage[k]);
1396 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1397 glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
1398 break;
1400 if (k != backtrack_count)
1401 continue;
1402 TRACE("Matched Backtrack\n");
1404 for (k = 0; k < input_count; ++k)
1406 offset = GET_BE_WORD(input->Coverage[k]);
1407 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1408 glyphs[glyph_index + (write_dir * k)]) == -1)
1409 break;
1411 if (k != input_count)
1412 continue;
1413 TRACE("Matched IndexGlyphs\n");
1415 for (k = 0; k < lookahead_count; ++k)
1417 offset = GET_BE_WORD(lookahead->Coverage[k]);
1418 if (GSUB_is_glyph_covered((const BYTE *)ccsf1 + offset,
1419 glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
1420 break;
1422 if (k != lookahead_count)
1423 continue;
1424 TRACE("Matched LookAhead\n");
1426 substitution_count = GET_BE_WORD(substitute->SubstCount);
1427 for (k = 0; k < substitution_count; ++k)
1429 WORD lookup_index = GET_BE_WORD(substitute->SubstLookupRecord[k].LookupListIndex);
1430 WORD sequence_index = GET_BE_WORD(substitute->SubstLookupRecord[k].SequenceIndex) * write_dir;
1432 TRACE("SUBST: %u -> %u %u.\n", k, sequence_index, lookup_index);
1433 new_index = GSUB_apply_lookup(lookup, lookup_index, glyphs,
1434 glyph_index + sequence_index, write_dir, glyph_count);
1435 if (new_index == GSUB_E_NOGLYPH)
1436 ERR("Chain failed to generate a glyph.\n");
1438 return new_index;
1441 return GSUB_E_NOGLYPH;
1444 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1446 int offset;
1447 enum gsub_lookup_type type;
1448 const OT_LookupTable *look;
1450 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1451 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1452 type = GET_BE_WORD(look->LookupType);
1453 TRACE("type %#x, flag %#x, subtables %u.\n", type,
1454 GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1456 if (type == GSUB_LOOKUP_EXTENSION)
1458 if (GET_BE_WORD(look->SubTableCount))
1460 const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
1461 if (GET_BE_WORD(ext->SubstFormat) == 1)
1463 type = GET_BE_WORD(ext->ExtensionLookupType);
1464 TRACE("extension type %i\n",type);
1466 else
1468 FIXME("Unhandled Extension Substitution Format %i\n",GET_BE_WORD(ext->SubstFormat));
1471 else
1473 WARN("lookup type is Extension Substitution but no extension subtable exists\n");
1476 switch(type)
1478 case GSUB_LOOKUP_SINGLE:
1479 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1480 case GSUB_LOOKUP_MULTIPLE:
1481 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1482 case GSUB_LOOKUP_ALTERNATE:
1483 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1484 case GSUB_LOOKUP_LIGATURE:
1485 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1486 case GSUB_LOOKUP_CONTEXT:
1487 return GSUB_apply_ContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1488 case GSUB_LOOKUP_CONTEXT_CHAINED:
1489 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1490 case GSUB_LOOKUP_EXTENSION:
1491 FIXME("Extension Substitution types not valid here\n");
1492 break;
1493 default:
1494 FIXME("Unhandled GSUB lookup type %#x.\n", type);
1496 return GSUB_E_NOGLYPH;
1499 int OpenType_apply_GSUB_lookup(const void *table, unsigned int lookup_index, WORD *glyphs,
1500 unsigned int glyph_index, int write_dir, int *glyph_count)
1502 const GSUB_Header *header = (const GSUB_Header *)table;
1503 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1505 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1508 /**********
1509 * GPOS
1510 **********/
1511 static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
1512 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
1513 unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
1514 GOFFSET *goffset);
1516 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1518 static const WORD mask[3] = {3,0xf,0xff};
1519 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1521 WORD format = GET_BE_WORD(DeviceTable->DeltaFormat);
1522 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1523 int value;
1525 TRACE("device table, format %#x, index %i\n", format, index);
1527 if (format < 1 || format > 3)
1529 WARN("invalid delta format %#x\n", format);
1530 return 0;
1533 index = index << format;
1534 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1535 TRACE("offset %i, value %i\n",index, value);
1536 if (value > mask[format-1]/2)
1537 value = -1 * ((mask[format-1]+1) - value);
1538 return value;
1540 return 0;
1543 static void GPOS_get_anchor_values(const void *table, POINT *pt, WORD ppem)
1545 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1547 switch (GET_BE_WORD(anchor1->AnchorFormat))
1549 case 1:
1551 TRACE("Anchor Format 1\n");
1552 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1553 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1554 break;
1556 case 2:
1558 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1559 TRACE("Anchor Format 2\n");
1560 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1561 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1562 break;
1564 case 3:
1566 int offset;
1567 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1568 TRACE("Anchor Format 3\n");
1569 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1570 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1571 offset = GET_BE_WORD(anchor3->XDeviceTable);
1572 TRACE("ppem %i\n",ppem);
1573 if (offset)
1575 const OT_DeviceTable* DeviceTableX = NULL;
1576 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1577 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1579 offset = GET_BE_WORD(anchor3->YDeviceTable);
1580 if (offset)
1582 const OT_DeviceTable* DeviceTableY = NULL;
1583 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1584 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1586 break;
1588 default:
1589 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1590 pt->x = 0;
1591 pt->y = 0;
1595 static void GPOS_convert_design_units_to_device(const OUTLINETEXTMETRICW *otm, const LOGFONTW *logfont,
1596 int desX, int desY, double *devX, double *devY)
1598 int emHeight = otm->otmTextMetrics.tmAscent + otm->otmTextMetrics.tmDescent - otm->otmTextMetrics.tmInternalLeading;
1600 TRACE("emHeight %i lfWidth %i\n",emHeight, logfont->lfWidth);
1601 *devX = (desX * emHeight) / (double)otm->otmEMSquare;
1602 *devY = (desY * emHeight) / (double)otm->otmEMSquare;
1603 if (logfont->lfWidth)
1604 FIXME("Font with lfWidth set not handled properly.\n");
1607 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1609 INT offset = 0;
1610 if (ValueFormat & 0x0001) { if (data) record->XPlacement = GET_BE_WORD(data[offset]); offset++; }
1611 if (ValueFormat & 0x0002) { if (data) record->YPlacement = GET_BE_WORD(data[offset]); offset++; }
1612 if (ValueFormat & 0x0004) { if (data) record->XAdvance = GET_BE_WORD(data[offset]); offset++; }
1613 if (ValueFormat & 0x0008) { if (data) record->YAdvance = GET_BE_WORD(data[offset]); offset++; }
1614 if (ValueFormat & 0x0010) { if (data) record->XPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1615 if (ValueFormat & 0x0020) { if (data) record->YPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1616 if (ValueFormat & 0x0040) { if (data) record->XAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1617 if (ValueFormat & 0x0080) { if (data) record->YAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1618 return offset;
1621 static void GPOS_get_value_record_offsets(const BYTE *head, GPOS_ValueRecord *ValueRecord,
1622 WORD ValueFormat, unsigned int ppem, POINT *ptPlacement, POINT *ptAdvance)
1624 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1625 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1626 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1627 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1628 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1629 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1630 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1631 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1632 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1635 static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
1637 int offset = GET_BE_WORD(look->SubTable[index]);
1639 if (GET_BE_WORD(look->LookupType) == GPOS_LOOKUP_POSITION_EXTENSION)
1641 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + offset);
1642 if (GET_BE_WORD(ext->PosFormat) == 1)
1644 offset += GET_BE_DWORD(ext->ExtensionOffset);
1646 else
1648 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1651 return (const BYTE *)look + offset;
1654 static void GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1655 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
1656 POINT *adjust, POINT *advance)
1658 int j;
1660 TRACE("Single Adjustment Positioning Subtable\n");
1662 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1664 const GPOS_SinglePosFormat1 *spf1 = (const GPOS_SinglePosFormat1*)GPOS_get_subtable(look, j);
1665 WORD offset;
1666 if (GET_BE_WORD(spf1->PosFormat) == 1)
1668 offset = GET_BE_WORD(spf1->Coverage);
1669 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1671 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1672 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1673 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1674 GPOS_get_value_record_offsets((const BYTE *)spf1, &ValueRecord, ValueFormat, ppem, adjust, advance);
1675 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1678 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1680 int index;
1681 const GPOS_SinglePosFormat2 *spf2;
1682 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1683 offset = GET_BE_WORD(spf2->Coverage);
1684 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1685 if (index != -1)
1687 int size;
1688 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1689 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1690 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1691 if (index > 0)
1693 offset = size * index;
1694 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1696 GPOS_get_value_record_offsets((const BYTE *)spf2, &ValueRecord, ValueFormat, ppem, adjust, advance);
1697 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1700 else
1701 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1705 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1706 INT ppem, POINT *adjust, POINT *advance )
1708 GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1709 GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1710 INT size;
1712 size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1713 GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1715 if (val_fmt1)
1717 GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1718 TRACE( "Glyph 1 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[0]) );
1719 TRACE( "Glyph 1 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[0]) );
1721 if (val_fmt2)
1723 GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1724 TRACE( "Glyph 2 resulting cumulative offset is %s design units\n", wine_dbgstr_point(&adjust[1]) );
1725 TRACE( "Glyph 2 resulting cumulative advance is %s design units\n", wine_dbgstr_point(&advance[1]) );
1729 static int GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1730 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem,
1731 POINT *adjust, POINT *advance)
1733 int j;
1734 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1736 if (glyph_index + write_dir >= glyph_count)
1737 return 1;
1739 TRACE("Pair Adjustment Positioning Subtable\n");
1741 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1743 const GPOS_PairPosFormat1 *ppf1 = (const GPOS_PairPosFormat1*)GPOS_get_subtable(look, j);
1744 WORD offset;
1745 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1747 int index;
1748 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1749 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1750 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1751 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1752 offset = GET_BE_WORD(ppf1->Coverage);
1753 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1754 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1756 int k;
1757 int pair_count;
1758 const GPOS_PairSet *ps;
1759 const GPOS_PairValueRecord *pair_val_rec;
1760 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1761 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1762 pair_count = GET_BE_WORD(ps->PairValueCount);
1763 pair_val_rec = ps->PairValueRecord;
1764 for (k = 0; k < pair_count; k++)
1766 WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1767 if (glyphs[glyph_index+write_dir] == second_glyph)
1769 int next = 1;
1770 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1771 apply_pair_value(ppf1, ValueFormat1, ValueFormat2,
1772 pair_val_rec->Value1, ppem, adjust, advance);
1773 if (ValueFormat2) next++;
1774 return next;
1776 pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1780 else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1782 const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)ppf1;
1783 int index;
1784 WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1785 WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1786 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1787 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1788 WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1789 WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1791 offset = GET_BE_WORD( ppf2->Coverage );
1792 index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1793 if (index != -1)
1795 WORD class1, class2;
1796 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1797 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1798 if (class1 < class1_count && class2 < class2_count)
1800 const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1801 int next = 1;
1803 TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1805 apply_pair_value(ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, adjust, advance);
1806 if (ValueFormat2) next++;
1807 return next;
1811 else
1812 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1814 return 1;
1817 static void GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1818 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
1820 int j;
1821 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1823 if (glyph_index + write_dir >= glyph_count)
1824 return;
1826 TRACE("Cursive Attachment Positioning Subtable\n");
1828 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1830 const GPOS_CursivePosFormat1 *cpf1 = (const GPOS_CursivePosFormat1 *)GPOS_get_subtable(look, j);
1831 if (GET_BE_WORD(cpf1->PosFormat) == 1)
1833 int index_exit, index_entry;
1834 WORD offset = GET_BE_WORD( cpf1->Coverage );
1835 index_exit = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index]);
1836 if (index_exit != -1 && cpf1->EntryExitRecord[index_exit].ExitAnchor!= 0)
1838 index_entry = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index+write_dir]);
1839 if (index_entry != -1 && cpf1->EntryExitRecord[index_entry].EntryAnchor != 0)
1841 POINT exit_pt, entry_pt;
1842 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_exit].ExitAnchor);
1843 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
1844 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
1845 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
1846 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));
1847 pt->x = entry_pt.x - exit_pt.x;
1848 pt->y = entry_pt.y - exit_pt.y;
1849 return;
1853 else
1854 FIXME("Cursive Attachment Positioning: Format %i Unhandled\n",GET_BE_WORD(cpf1->PosFormat));
1856 return;
1859 static int GPOS_apply_MarkToBase(const ScriptCache *script_cache, const OT_LookupTable *look,
1860 const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, unsigned int glyph_index,
1861 unsigned int glyph_count, unsigned int ppem, POINT *pt)
1863 int j;
1864 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1865 const void *glyph_class_table = NULL;
1866 int rc = -1;
1868 if (script_cache->GDEF_Table)
1870 const GDEF_Header *header = script_cache->GDEF_Table;
1871 WORD offset = GET_BE_WORD( header->GlyphClassDef );
1872 if (offset)
1873 glyph_class_table = (const BYTE *)script_cache->GDEF_Table + offset;
1876 TRACE("MarkToBase Attachment Positioning Subtable\n");
1878 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1880 const GPOS_MarkBasePosFormat1 *mbpf1 = (const GPOS_MarkBasePosFormat1 *)GPOS_get_subtable(look, j);
1881 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1883 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1884 int mark_index;
1885 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1886 if (mark_index != -1)
1888 int base_index;
1889 int base_glyph = glyph_index - write_dir;
1891 if (glyph_class_table)
1893 while (OT_get_glyph_class(glyph_class_table, glyphs[base_glyph]) == MarkGlyph && base_glyph > 0 && base_glyph < glyph_count)
1894 base_glyph -= write_dir;
1897 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1898 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[base_glyph]);
1899 if (base_index != -1)
1901 const GPOS_MarkArray *ma;
1902 const GPOS_MarkRecord *mr;
1903 const GPOS_BaseArray *ba;
1904 const GPOS_BaseRecord *br;
1905 int mark_class;
1906 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1907 int baserecord_size;
1908 POINT base_pt;
1909 POINT mark_pt;
1910 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[base_glyph], base_index);
1911 offset = GET_BE_WORD(mbpf1->MarkArray);
1912 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1913 if (mark_index > GET_BE_WORD(ma->MarkCount))
1915 ERR("Mark index exceeded mark count\n");
1916 return -1;
1918 mr = &ma->MarkRecord[mark_index];
1919 mark_class = GET_BE_WORD(mr->Class);
1920 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1921 offset = GET_BE_WORD(mbpf1->BaseArray);
1922 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1923 baserecord_size = class_count * sizeof(WORD);
1924 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1925 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1926 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1927 offset = GET_BE_WORD(mr->MarkAnchor);
1928 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1929 TRACE("Offset on base is %s design units\n",wine_dbgstr_point(&base_pt));
1930 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
1931 pt->x += base_pt.x - mark_pt.x;
1932 pt->y += base_pt.y - mark_pt.y;
1933 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
1934 rc = base_glyph;
1938 else
1939 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1941 return rc;
1944 static void GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
1945 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
1947 int j;
1948 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1950 TRACE("MarkToLigature Attachment Positioning Subtable\n");
1952 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1954 const GPOS_MarkLigPosFormat1 *mlpf1 = (const GPOS_MarkLigPosFormat1 *)GPOS_get_subtable(look, j);
1955 if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1957 int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1958 int mark_index;
1959 mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
1960 if (mark_index != -1)
1962 int ligature_index;
1963 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
1964 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
1965 if (ligature_index != -1)
1967 const GPOS_MarkArray *ma;
1968 const GPOS_MarkRecord *mr;
1970 const GPOS_LigatureArray *la;
1971 const GPOS_LigatureAttach *lt;
1972 int mark_class;
1973 int class_count = GET_BE_WORD(mlpf1->ClassCount);
1974 int component_count;
1975 int component_size;
1976 int i;
1977 POINT ligature_pt;
1978 POINT mark_pt;
1980 TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
1981 offset = GET_BE_WORD(mlpf1->MarkArray);
1982 ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
1983 if (mark_index > GET_BE_WORD(ma->MarkCount))
1985 ERR("Mark index exceeded mark count\n");
1986 return;
1988 mr = &ma->MarkRecord[mark_index];
1989 mark_class = GET_BE_WORD(mr->Class);
1990 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1991 offset = GET_BE_WORD(mlpf1->LigatureArray);
1992 la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
1993 if (ligature_index > GET_BE_WORD(la->LigatureCount))
1995 ERR("Ligature index exceeded ligature count\n");
1996 return;
1998 offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
1999 lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
2001 component_count = GET_BE_WORD(lt->ComponentCount);
2002 component_size = class_count * sizeof(WORD);
2003 offset = 0;
2004 for (i = 0; i < component_count && !offset; i++)
2006 int k;
2007 const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
2008 for (k = 0; k < class_count && !offset; k++)
2009 offset = GET_BE_WORD(cr->LigatureAnchor[k]);
2010 cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
2012 if (!offset)
2014 ERR("Failed to find avalible ligature connection point\n");
2015 return;
2018 GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
2019 offset = GET_BE_WORD(mr->MarkAnchor);
2020 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
2021 TRACE("Offset on ligature is %s design units\n",wine_dbgstr_point(&ligature_pt));
2022 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
2023 pt->x += ligature_pt.x - mark_pt.x;
2024 pt->y += ligature_pt.y - mark_pt.y;
2025 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
2029 else
2030 FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
2034 static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis,
2035 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, unsigned int ppem, POINT *pt)
2037 int j;
2038 BOOL rc = FALSE;
2039 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2041 TRACE("MarkToMark Attachment Positioning Subtable\n");
2043 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2045 const GPOS_MarkMarkPosFormat1 *mmpf1 = (const GPOS_MarkMarkPosFormat1 *)GPOS_get_subtable(look, j);
2046 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
2048 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
2049 int mark_index;
2050 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
2051 if (mark_index != -1)
2053 int mark2_index;
2054 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
2055 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
2056 if (mark2_index != -1)
2058 const GPOS_MarkArray *ma;
2059 const GPOS_MarkRecord *mr;
2060 const GPOS_Mark2Array *m2a;
2061 const GPOS_Mark2Record *m2r;
2062 int mark_class;
2063 int class_count = GET_BE_WORD(mmpf1->ClassCount);
2064 int mark2record_size;
2065 POINT mark2_pt;
2066 POINT mark_pt;
2067 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
2068 offset = GET_BE_WORD(mmpf1->Mark1Array);
2069 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
2070 if (mark_index > GET_BE_WORD(ma->MarkCount))
2072 ERR("Mark index exceeded mark count\n");
2073 return FALSE;
2075 mr = &ma->MarkRecord[mark_index];
2076 mark_class = GET_BE_WORD(mr->Class);
2077 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
2078 offset = GET_BE_WORD(mmpf1->Mark2Array);
2079 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
2080 mark2record_size = class_count * sizeof(WORD);
2081 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
2082 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
2083 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
2084 offset = GET_BE_WORD(mr->MarkAnchor);
2085 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
2086 TRACE("Offset on mark2 is %s design units\n",wine_dbgstr_point(&mark2_pt));
2087 TRACE("Offset on mark is %s design units\n",wine_dbgstr_point(&mark_pt));
2088 pt->x += mark2_pt.x - mark_pt.x;
2089 pt->y += mark2_pt.y - mark_pt.y;
2090 TRACE("Resulting cumulative offset is %s design units\n",wine_dbgstr_point(pt));
2091 rc = TRUE;
2095 else
2096 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
2098 return rc;
2101 static unsigned int GPOS_apply_ContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2102 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
2103 const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2104 GOFFSET *goffset)
2106 int j;
2107 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2109 TRACE("Contextual Positioning Subtable\n");
2111 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2113 const GPOS_ContextPosFormat2 *cpf2 = (GPOS_ContextPosFormat2*)GPOS_get_subtable(look, j);
2115 if (GET_BE_WORD(cpf2->PosFormat) == 1)
2117 static int once;
2118 if (!once++)
2119 FIXME(" TODO: subtype 1\n");
2120 continue;
2122 else if (GET_BE_WORD(cpf2->PosFormat) == 2)
2124 WORD offset = GET_BE_WORD(cpf2->Coverage);
2125 int index;
2127 TRACE("Contextual Positioning Subtable: Format 2\n");
2129 index = GSUB_is_glyph_covered((const BYTE*)cpf2+offset, glyphs[glyph_index]);
2130 TRACE("Coverage index %i\n",index);
2131 if (index != -1)
2133 int k, count, class;
2134 const GPOS_PosClassSet *pcs;
2135 const void *glyph_class_table = NULL;
2137 offset = GET_BE_WORD(cpf2->ClassDef);
2138 glyph_class_table = (const BYTE *)cpf2 + offset;
2140 class = OT_get_glyph_class(glyph_class_table,glyphs[glyph_index]);
2142 offset = GET_BE_WORD(cpf2->PosClassSet[class]);
2143 if (offset == 0)
2145 TRACE("No class rule table for class %i\n",class);
2146 continue;
2148 pcs = (const GPOS_PosClassSet*)((const BYTE*)cpf2+offset);
2149 count = GET_BE_WORD(pcs->PosClassRuleCnt);
2150 TRACE("PosClassSet has %i members\n",count);
2151 for (k = 0; k < count; k++)
2153 const GPOS_PosClassRule_1 *pr;
2154 const GPOS_PosClassRule_2 *pr_2;
2155 int g_count, l;
2157 offset = GET_BE_WORD(pcs->PosClassRule[k]);
2158 pr = (const GPOS_PosClassRule_1*)((const BYTE*)pcs+offset);
2159 g_count = GET_BE_WORD(pr->GlyphCount);
2160 TRACE("PosClassRule has %i glyphs classes\n",g_count);
2161 for (l = 0; l < g_count-1; l++)
2163 int g_class = OT_get_glyph_class(glyph_class_table, glyphs[glyph_index + (write_dir * (l+1))]);
2164 if (g_class != GET_BE_WORD(pr->Class[l])) break;
2167 if (l < g_count-1)
2169 TRACE("Rule does not match\n");
2170 continue;
2173 TRACE("Rule matches\n");
2174 pr_2 = (const GPOS_PosClassRule_2*)((const BYTE*)pr+
2175 FIELD_OFFSET(GPOS_PosClassRule_1, Class[g_count-1]));
2177 for (l = 0; l < GET_BE_WORD(pr->PosCount); l++)
2179 int lookupIndex = GET_BE_WORD(pr_2->PosLookupRecord[l].LookupListIndex);
2180 int SequenceIndex = GET_BE_WORD(pr_2->PosLookupRecord[l].SequenceIndex) * write_dir;
2182 TRACE("Position: %i -> %i %i\n",l, SequenceIndex, lookupIndex);
2183 GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup, lookupIndex,
2184 glyphs, glyph_index + SequenceIndex, glyph_count, goffset);
2186 return 1;
2190 TRACE("Not covered\n");
2191 continue;
2193 else if (GET_BE_WORD(cpf2->PosFormat) == 3)
2195 static int once;
2196 if (!once++)
2197 FIXME(" TODO: subtype 3\n");
2198 continue;
2200 else
2201 FIXME("Unhandled Contextual Positioning Format %i\n",GET_BE_WORD(cpf2->PosFormat));
2203 return 1;
2206 static unsigned int GPOS_apply_ChainContextPos(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2207 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, const OT_LookupList *lookup,
2208 const OT_LookupTable *look, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2209 GOFFSET *goffset)
2211 int j;
2212 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2214 TRACE("Chaining Contextual Positioning Subtable\n");
2216 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2218 int offset;
2219 const GPOS_ChainContextPosFormat3_1 *backtrack = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
2220 int dirLookahead = write_dir;
2221 int dirBacktrack = -1 * write_dir;
2223 if (GET_BE_WORD(backtrack->PosFormat) == 1)
2225 static int once;
2226 if (!once++)
2227 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
2228 continue;
2230 else if (GET_BE_WORD(backtrack->PosFormat) == 2)
2232 static int once;
2233 if (!once++)
2234 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
2235 continue;
2237 else if (GET_BE_WORD(backtrack->PosFormat) == 3)
2239 WORD backtrack_count, input_count, lookahead_count, positioning_count;
2240 int k;
2241 const GPOS_ChainContextPosFormat3_2 *input;
2242 const GPOS_ChainContextPosFormat3_3 *lookahead;
2243 const GPOS_ChainContextPosFormat3_4 *positioning;
2245 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
2247 backtrack_count = GET_BE_WORD(backtrack->BacktrackGlyphCount);
2248 k = glyph_index + dirBacktrack * backtrack_count;
2249 if (k < 0 || k >= glyph_count)
2250 continue;
2252 input = (const GPOS_ChainContextPosFormat3_2 *)&backtrack->Coverage[backtrack_count];
2253 input_count = GET_BE_WORD(input->InputGlyphCount);
2254 k = glyph_index + write_dir * (input_count - 1);
2255 if (k < 0 || k >= glyph_count)
2256 continue;
2258 lookahead = (const GPOS_ChainContextPosFormat3_3 *)&input->Coverage[input_count];
2259 lookahead_count = GET_BE_WORD(lookahead->LookaheadGlyphCount);
2260 k = glyph_index + dirLookahead * (input_count + lookahead_count - 1);
2261 if (k < 0 || k >= glyph_count)
2262 continue;
2264 positioning = (const GPOS_ChainContextPosFormat3_4 *)&lookahead->Coverage[lookahead_count];
2266 for (k = 0; k < backtrack_count; ++k)
2268 offset = GET_BE_WORD(backtrack->Coverage[k]);
2269 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2270 glyphs[glyph_index + (dirBacktrack * (k + 1))]) == -1)
2271 break;
2273 if (k != backtrack_count)
2274 continue;
2275 TRACE("Matched Backtrack\n");
2277 for (k = 0; k < input_count; ++k)
2279 offset = GET_BE_WORD(input->Coverage[k]);
2280 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2281 glyphs[glyph_index + (write_dir * k)]) == -1)
2282 break;
2284 if (k != input_count)
2285 continue;
2286 TRACE("Matched IndexGlyphs\n");
2288 for (k = 0; k < lookahead_count; ++k)
2290 offset = GET_BE_WORD(lookahead->Coverage[k]);
2291 if (GSUB_is_glyph_covered((const BYTE *)backtrack + offset,
2292 glyphs[glyph_index + (dirLookahead * (input_count + k))]) == -1)
2293 break;
2295 if (k != lookahead_count)
2296 continue;
2297 TRACE("Matched LookAhead\n");
2299 if (!(positioning_count = GET_BE_WORD(positioning->PosCount)))
2300 return 1;
2302 for (k = 0; k < positioning_count; ++k)
2304 WORD lookup_index = GET_BE_WORD(positioning->PosLookupRecord[k].LookupListIndex);
2305 WORD sequence_index = GET_BE_WORD(positioning->PosLookupRecord[k].SequenceIndex) * write_dir;
2307 TRACE("Position: %u -> %u %u.\n", k, sequence_index, lookup_index);
2308 GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup, lookup_index,
2309 glyphs, glyph_index + sequence_index, glyph_count, goffset);
2311 return input_count + lookahead_count;
2313 else
2314 FIXME("Unhandled Chaining Contextual Positioning Format %#x.\n", GET_BE_WORD(backtrack->PosFormat));
2316 return 1;
2319 static unsigned int GPOS_apply_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *lpotm,
2320 const LOGFONTW *lplogfont, const SCRIPT_ANALYSIS *analysis, int *piAdvance, const OT_LookupList *lookup,
2321 unsigned int lookup_index, const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count,
2322 GOFFSET *pGoffset)
2324 int offset;
2325 const OT_LookupTable *look;
2326 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
2327 enum gpos_lookup_type type;
2329 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
2330 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
2331 type = GET_BE_WORD(look->LookupType);
2332 TRACE("type %#x, flag %#x, subtables %u.\n", type,
2333 GET_BE_WORD(look->LookupFlag), GET_BE_WORD(look->SubTableCount));
2335 if (type == GPOS_LOOKUP_POSITION_EXTENSION)
2337 if (GET_BE_WORD(look->SubTableCount))
2339 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
2340 if (GET_BE_WORD(ext->PosFormat) == 1)
2342 type = GET_BE_WORD(ext->ExtensionLookupType);
2343 TRACE("extension type %i\n",type);
2345 else
2347 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
2350 else
2352 WARN("lookup type is Extension Positioning but no extension subtable exists\n");
2355 switch (type)
2357 case GPOS_LOOKUP_ADJUST_SINGLE:
2359 double devX, devY;
2360 POINT adjust = {0,0};
2361 POINT advance = {0,0};
2362 GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
2363 if (adjust.x || adjust.y)
2365 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
2366 pGoffset[glyph_index].du += round(devX);
2367 pGoffset[glyph_index].dv += round(devY);
2369 if (advance.x || advance.y)
2371 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
2372 piAdvance[glyph_index] += round(devX);
2373 if (advance.y)
2374 FIXME("Unhandled adjustment to Y advancement\n");
2376 break;
2379 case GPOS_LOOKUP_ADJUST_PAIR:
2381 POINT advance[2]= {{0,0},{0,0}};
2382 POINT adjust[2]= {{0,0},{0,0}};
2383 double devX, devY;
2384 int index_offset;
2385 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2386 int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
2388 index_offset = GPOS_apply_PairAdjustment(look, analysis, glyphs,
2389 glyph_index, glyph_count, ppem, adjust, advance);
2390 if (adjust[0].x || adjust[0].y)
2392 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
2393 pGoffset[glyph_index].du += round(devX) * offset_sign;
2394 pGoffset[glyph_index].dv += round(devY);
2396 if (advance[0].x || advance[0].y)
2398 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
2399 piAdvance[glyph_index] += round(devX);
2401 if (adjust[1].x || adjust[1].y)
2403 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
2404 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
2405 pGoffset[glyph_index + write_dir].dv += round(devY);
2407 if (advance[1].x || advance[1].y)
2409 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
2410 piAdvance[glyph_index + write_dir] += round(devX);
2412 return index_offset;
2415 case GPOS_LOOKUP_ATTACH_CURSIVE:
2417 POINT desU = {0,0};
2418 double devX, devY;
2419 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2421 GPOS_apply_CursiveAttachment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
2422 if (desU.x || desU.y)
2424 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2425 /* Windows does not appear to apply X offsets here */
2426 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index+write_dir].dv;
2428 break;
2431 case GPOS_LOOKUP_ATTACH_MARK_TO_BASE:
2433 double devX, devY;
2434 POINT desU = {0,0};
2435 int base_index = GPOS_apply_MarkToBase(script_cache, look, analysis,
2436 glyphs, glyph_index, glyph_count, ppem, &desU);
2437 if (base_index != -1)
2439 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2440 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[base_index];
2441 else
2443 if (analysis->fLogicalOrder) devX *= -1;
2444 pGoffset[glyph_index].du = round(devX);
2446 pGoffset[glyph_index].dv = round(devY);
2448 break;
2451 case GPOS_LOOKUP_ATTACH_MARK_TO_LIGATURE:
2453 double devX, devY;
2454 POINT desU = {0,0};
2455 GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
2456 if (desU.x || desU.y)
2458 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2459 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
2460 pGoffset[glyph_index].dv = round(devY);
2462 break;
2465 case GPOS_LOOKUP_ATTACH_MARK_TO_MARK:
2467 double devX, devY;
2468 POINT desU = {0,0};
2469 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
2470 if (GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU))
2472 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
2473 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
2474 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
2475 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
2477 break;
2480 case GPOS_LOOKUP_POSITION_CONTEXT:
2481 return GPOS_apply_ContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
2482 lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
2484 case GPOS_LOOKUP_POSITION_CONTEXT_CHAINED:
2485 return GPOS_apply_ChainContextPos(script_cache, lpotm, lplogfont, analysis, piAdvance,
2486 lookup, look, glyphs, glyph_index, glyph_count, pGoffset);
2488 default:
2489 FIXME("Unhandled GPOS lookup type %#x.\n", type);
2491 return 1;
2494 unsigned int OpenType_apply_GPOS_lookup(const ScriptCache *script_cache, const OUTLINETEXTMETRICW *otm,
2495 const LOGFONTW *logfont, const SCRIPT_ANALYSIS *analysis, int *advance, unsigned int lookup_index,
2496 const WORD *glyphs, unsigned int glyph_index, unsigned int glyph_count, GOFFSET *goffset)
2498 const GPOS_Header *header = (const GPOS_Header *)script_cache->GPOS_Table;
2499 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
2501 return GPOS_apply_lookup(script_cache, otm, logfont, analysis, advance, lookup,
2502 lookup_index, glyphs, glyph_index, glyph_count, goffset);
2505 static LoadedScript *usp10_script_cache_get_script(ScriptCache *script_cache, OPENTYPE_TAG tag)
2507 size_t i;
2509 for (i = 0; i < script_cache->script_count; ++i)
2511 if (script_cache->scripts[i].tag == tag)
2512 return &script_cache->scripts[i];
2515 return NULL;
2518 static void GSUB_initialize_script_cache(ScriptCache *psc)
2520 int i;
2522 if (psc->GSUB_Table)
2524 const OT_ScriptList *script;
2525 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
2526 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
2527 psc->script_count = GET_BE_WORD(script->ScriptCount);
2528 TRACE("initializing %li scripts in this font\n",psc->script_count);
2529 if (psc->script_count)
2531 psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
2532 for (i = 0; i < psc->script_count; i++)
2534 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2535 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]);
2536 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
2542 static void GPOS_expand_script_cache(ScriptCache *psc)
2544 int i, count;
2545 const OT_ScriptList *script;
2546 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
2547 LoadedScript *loaded_script;
2549 if (!header)
2550 return;
2552 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
2553 count = GET_BE_WORD(script->ScriptCount);
2555 if (!count)
2556 return;
2558 if (!psc->script_count)
2560 psc->script_count = count;
2561 TRACE("initializing %li scripts in this font\n",psc->script_count);
2562 if (psc->script_count)
2564 psc->scripts = heap_alloc_zero(psc->script_count * sizeof(*psc->scripts));
2565 for (i = 0; i < psc->script_count; i++)
2567 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2568 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]);
2569 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
2573 else
2575 for (i = 0; i < count; i++)
2577 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
2578 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]);
2580 if (!(loaded_script = usp10_script_cache_get_script(psc, tag)))
2582 if (!usp10_array_reserve((void **)&psc->scripts, &psc->scripts_size,
2583 psc->script_count + 1, sizeof(*psc->scripts)))
2585 ERR("Failed grow scripts array.\n");
2586 return;
2589 loaded_script = &psc->scripts[psc->script_count];
2590 ++psc->script_count;
2591 loaded_script->tag = tag;
2593 loaded_script->gpos_table = (const BYTE *)script + offset;
2598 static void _initialize_script_cache(ScriptCache *psc)
2600 if (!psc->scripts_initialized)
2602 GSUB_initialize_script_cache(psc);
2603 GPOS_expand_script_cache(psc);
2604 psc->scripts_initialized = TRUE;
2608 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
2610 int i;
2611 const LoadedScript *script;
2612 HRESULT rc = S_OK;
2614 _initialize_script_cache(psc);
2616 *pcTags = psc->script_count;
2618 if (searchingFor)
2620 if (!(script = usp10_script_cache_get_script(psc, searchingFor)))
2621 return USP_E_SCRIPT_NOT_IN_FONT;
2623 *pScriptTags = script->tag;
2624 *pcTags = 1;
2625 return S_OK;
2628 if (cMaxTags < *pcTags)
2629 rc = E_OUTOFMEMORY;
2631 cMaxTags = min(cMaxTags, psc->script_count);
2632 for (i = 0; i < cMaxTags; ++i)
2634 pScriptTags[i] = psc->scripts[i].tag;
2636 return rc;
2639 static LoadedLanguage *usp10_script_get_language(LoadedScript *script, OPENTYPE_TAG tag)
2641 size_t i;
2643 for (i = 0; i < script->language_count; ++i)
2645 if (script->languages[i].tag == tag)
2646 return &script->languages[i];
2649 return NULL;
2652 static void GSUB_initialize_language_cache(LoadedScript *script)
2654 int i;
2656 if (script->gsub_table)
2658 DWORD offset;
2659 const OT_Script* table = script->gsub_table;
2660 script->language_count = GET_BE_WORD(table->LangSysCount);
2661 offset = GET_BE_WORD(table->DefaultLangSys);
2662 if (offset)
2664 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
2665 script->default_language.gsub_table = (const BYTE*)table + offset;
2668 if (script->language_count)
2670 TRACE("Deflang %p, LangCount %li\n",script->default_language.gsub_table, script->language_count);
2672 script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
2674 for (i = 0; i < script->language_count; i++)
2676 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2677 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]);
2678 script->languages[i].gsub_table = ((const BYTE*)table + offset);
2684 static void GPOS_expand_language_cache(LoadedScript *script)
2686 int count;
2687 const OT_Script* table = script->gpos_table;
2688 LoadedLanguage *language;
2689 DWORD offset;
2691 if (!table)
2692 return;
2694 offset = GET_BE_WORD(table->DefaultLangSys);
2695 if (offset)
2696 script->default_language.gpos_table = (const BYTE*)table + offset;
2698 count = GET_BE_WORD(table->LangSysCount);
2700 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
2702 if (!count)
2703 return;
2705 if (!script->language_count)
2707 int i;
2708 script->language_count = count;
2710 script->languages = heap_alloc_zero(script->language_count * sizeof(*script->languages));
2712 for (i = 0; i < script->language_count; i++)
2714 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2715 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]);
2716 script->languages[i].gpos_table = ((const BYTE*)table + offset);
2719 else if (count)
2721 int i;
2722 for (i = 0; i < count; i++)
2724 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2725 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]);
2727 if (!(language = usp10_script_get_language(script, tag)))
2729 if (!usp10_array_reserve((void **)&script->languages, &script->languages_size,
2730 script->language_count + 1, sizeof(*script->languages)))
2732 ERR("Failed grow languages array.\n");
2733 return;
2736 language = &script->languages[script->language_count];
2737 ++script->language_count;
2738 language->tag = tag;
2740 language->gpos_table = (const BYTE *)table + offset;
2745 static void _initialize_language_cache(LoadedScript *script)
2747 if (!script->languages_initialized)
2749 GSUB_initialize_language_cache(script);
2750 GPOS_expand_language_cache(script);
2751 script->languages_initialized = TRUE;
2755 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2757 int i;
2758 HRESULT rc = S_OK;
2759 LoadedScript *script = NULL;
2761 _initialize_script_cache(psc);
2762 if (!(script = usp10_script_cache_get_script(psc, script_tag)))
2763 return E_INVALIDARG;
2765 _initialize_language_cache(script);
2767 if (!searchingFor && cMaxTags < script->language_count)
2768 rc = E_OUTOFMEMORY;
2769 else if (searchingFor)
2770 rc = E_INVALIDARG;
2772 *pcTags = script->language_count;
2774 for (i = 0; i < script->language_count; i++)
2776 if (i < cMaxTags)
2777 pLanguageTags[i] = script->languages[i].tag;
2779 if (searchingFor)
2781 if (searchingFor == script->languages[i].tag)
2783 pLanguageTags[0] = script->languages[i].tag;
2784 *pcTags = 1;
2785 rc = S_OK;
2786 break;
2791 if (script->default_language.gsub_table)
2793 if (i < cMaxTags)
2794 pLanguageTags[i] = script->default_language.tag;
2796 if (searchingFor && FAILED(rc))
2798 pLanguageTags[0] = script->default_language.tag;
2800 i++;
2801 *pcTags = (*pcTags) + 1;
2804 return rc;
2807 static void usp10_language_add_feature_list(LoadedLanguage *language, char table_type,
2808 const OT_LangSys *lang, const OT_FeatureList *feature_list)
2810 unsigned int count = GET_BE_WORD(lang->FeatureCount);
2811 unsigned int i, j;
2813 TRACE("table_type %#x, %u features.\n", table_type, count);
2815 if (!count)
2816 return;
2818 if (!language->feature_count)
2819 language->features = heap_alloc(count * sizeof(*language->features));
2820 else
2821 language->features = HeapReAlloc(GetProcessHeap(), 0, language->features,
2822 (language->feature_count + count) * sizeof(*language->features));
2824 for (i = 0; i < count; ++i)
2826 const OT_FeatureRecord *record;
2827 LoadedFeature *loaded_feature;
2828 const OT_Feature *feature;
2830 record = &feature_list->FeatureRecord[GET_BE_WORD(lang->FeatureIndex[i])];
2831 feature = (const OT_Feature *)((const BYTE *)feature_list + GET_BE_WORD(record->Feature));
2833 loaded_feature = &language->features[language->feature_count + i];
2834 loaded_feature->tag = MS_MAKE_TAG(record->FeatureTag[0], record->FeatureTag[1],
2835 record->FeatureTag[2], record->FeatureTag[3]);
2836 loaded_feature->tableType = table_type;
2837 loaded_feature->feature = feature;
2838 loaded_feature->lookup_count = GET_BE_WORD(feature->LookupCount);
2839 loaded_feature->lookups = heap_alloc(loaded_feature->lookup_count * sizeof(*loaded_feature->lookups));
2840 for (j = 0; j < loaded_feature->lookup_count; ++j)
2841 loaded_feature->lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2843 language->feature_count += count;
2846 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2848 const GSUB_Header *gsub_header = psc->GSUB_Table;
2849 const GPOS_Header *gpos_header = psc->GPOS_Table;
2850 const OT_FeatureList *feature_list;
2851 const OT_LangSys *lang;
2853 if (language->features_initialized)
2854 return;
2856 if ((lang = language->gsub_table))
2858 feature_list = (const OT_FeatureList *)((const BYTE *)gsub_header + GET_BE_WORD(gsub_header->FeatureList));
2859 usp10_language_add_feature_list(language, FEATURE_GSUB_TABLE, lang, feature_list);
2862 if ((lang = language->gpos_table))
2864 feature_list = (const OT_FeatureList *)((const BYTE *)gpos_header + GET_BE_WORD(gpos_header->FeatureList));
2865 usp10_language_add_feature_list(language, FEATURE_GPOS_TABLE, lang, feature_list);
2868 language->features_initialized = TRUE;
2871 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)
2873 int i;
2874 LoadedScript *script;
2875 HRESULT rc = S_OK;
2876 LoadedLanguage *language = NULL;
2878 _initialize_script_cache(psc);
2879 if (!(script = usp10_script_cache_get_script(psc, script_tag)))
2881 *pcTags = 0;
2882 if (!filtered)
2883 return S_OK;
2884 else
2885 return E_INVALIDARG;
2888 _initialize_language_cache(script);
2890 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2891 language = &script->default_language;
2892 else
2893 language = usp10_script_get_language(script, language_tag);
2895 if (!language)
2897 *pcTags = 0;
2898 return S_OK;
2901 _initialize_feature_cache(psc, language);
2903 if (tableType)
2905 *pcTags = 0;
2906 for (i = 0; i < language->feature_count; i++)
2907 if (language->features[i].tableType == tableType)
2908 *pcTags = (*pcTags)+1;
2910 else
2911 *pcTags = language->feature_count;
2913 if (!searchingFor && cMaxTags < *pcTags)
2914 rc = E_OUTOFMEMORY;
2915 else if (searchingFor)
2916 rc = E_INVALIDARG;
2918 for (i = 0; i < language->feature_count; i++)
2920 if (i < cMaxTags)
2922 if (!tableType || language->features[i].tableType == tableType)
2923 pFeatureTags[i] = language->features[i].tag;
2926 if (searchingFor)
2928 if ((searchingFor == language->features[i].tag) &&
2929 (!tableType || language->features[i].tableType == tableType))
2931 pFeatureTags[0] = language->features[i].tag;
2932 *pcTags = 1;
2933 if (feature)
2934 *feature = &language->features[i];
2935 rc = S_OK;
2936 break;
2940 return rc;