Sync with trunk for console graphics palettes.
[reactos.git] / dll / win32 / usp10 / opentype.c
1 /*
2 * Opentype font interfaces for the Uniscribe Script Processor (usp10.dll)
3 *
4 * Copyright 2012 CodeWeavers, Aric Stewart
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21 #include <stdarg.h>
22 #include <stdlib.h>
23
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>
31
32 #include "usp10_internal.h"
33
34 #include <wine/debug.h>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
37
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
45
46 #define round(x) (((x) < 0) ? (int)((x) - 0.5) : (int)((x) + 0.5))
47
48 /* These are all structures needed for the cmap format 12 table */
49 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
50
51 typedef struct {
52 WORD platformID;
53 WORD encodingID;
54 DWORD offset;
55 } CMAP_EncodingRecord;
56
57 typedef struct {
58 WORD version;
59 WORD numTables;
60 CMAP_EncodingRecord tables[1];
61 } CMAP_Header;
62
63 typedef struct {
64 DWORD startCharCode;
65 DWORD endCharCode;
66 DWORD startGlyphID;
67 } CMAP_SegmentedCoverage_group;
68
69 typedef struct {
70 WORD format;
71 WORD reserved;
72 DWORD length;
73 DWORD language;
74 DWORD nGroups;
75 CMAP_SegmentedCoverage_group groups[1];
76 } CMAP_SegmentedCoverage;
77
78 /* These are all structures needed for the GDEF table */
79 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
80
81 typedef struct {
82 DWORD Version;
83 WORD GlyphClassDef;
84 WORD AttachList;
85 WORD LigCaretList;
86 WORD MarkAttachClassDef;
87 } GDEF_Header;
88
89 typedef struct {
90 WORD ClassFormat;
91 WORD StartGlyph;
92 WORD GlyphCount;
93 WORD ClassValueArray[1];
94 } OT_ClassDefFormat1;
95
96 typedef struct {
97 WORD Start;
98 WORD End;
99 WORD Class;
100 } OT_ClassRangeRecord;
101
102 typedef struct {
103 WORD ClassFormat;
104 WORD ClassRangeCount;
105 OT_ClassRangeRecord ClassRangeRecord[1];
106 } OT_ClassDefFormat2;
107
108 /* These are all structures needed for the GSUB table */
109
110 typedef struct {
111 DWORD version;
112 WORD ScriptList;
113 WORD FeatureList;
114 WORD LookupList;
115 } GSUB_Header;
116
117 typedef struct {
118 CHAR ScriptTag[4];
119 WORD Script;
120 } OT_ScriptRecord;
121
122 typedef struct {
123 WORD ScriptCount;
124 OT_ScriptRecord ScriptRecord[1];
125 } OT_ScriptList;
126
127 typedef struct {
128 CHAR LangSysTag[4];
129 WORD LangSys;
130 } OT_LangSysRecord;
131
132 typedef struct {
133 WORD DefaultLangSys;
134 WORD LangSysCount;
135 OT_LangSysRecord LangSysRecord[1];
136 } OT_Script;
137
138 typedef struct {
139 WORD LookupOrder; /* Reserved */
140 WORD ReqFeatureIndex;
141 WORD FeatureCount;
142 WORD FeatureIndex[1];
143 } OT_LangSys;
144
145 typedef struct {
146 CHAR FeatureTag[4];
147 WORD Feature;
148 } OT_FeatureRecord;
149
150 typedef struct {
151 WORD FeatureCount;
152 OT_FeatureRecord FeatureRecord[1];
153 } OT_FeatureList;
154
155 typedef struct {
156 WORD FeatureParams; /* Reserved */
157 WORD LookupCount;
158 WORD LookupListIndex[1];
159 } OT_Feature;
160
161 typedef struct {
162 WORD LookupCount;
163 WORD Lookup[1];
164 } OT_LookupList;
165
166 typedef struct {
167 WORD LookupType;
168 WORD LookupFlag;
169 WORD SubTableCount;
170 WORD SubTable[1];
171 } OT_LookupTable;
172
173 typedef struct {
174 WORD CoverageFormat;
175 WORD GlyphCount;
176 WORD GlyphArray[1];
177 } OT_CoverageFormat1;
178
179 typedef struct {
180 WORD Start;
181 WORD End;
182 WORD StartCoverageIndex;
183 } OT_RangeRecord;
184
185 typedef struct {
186 WORD CoverageFormat;
187 WORD RangeCount;
188 OT_RangeRecord RangeRecord[1];
189 } OT_CoverageFormat2;
190
191 typedef struct {
192 WORD SubstFormat; /* = 1 */
193 WORD Coverage;
194 WORD DeltaGlyphID;
195 } GSUB_SingleSubstFormat1;
196
197 typedef struct {
198 WORD SubstFormat; /* = 2 */
199 WORD Coverage;
200 WORD GlyphCount;
201 WORD Substitute[1];
202 }GSUB_SingleSubstFormat2;
203
204 typedef struct {
205 WORD SubstFormat; /* = 1 */
206 WORD Coverage;
207 WORD SequenceCount;
208 WORD Sequence[1];
209 }GSUB_MultipleSubstFormat1;
210
211 typedef struct {
212 WORD GlyphCount;
213 WORD Substitute[1];
214 }GSUB_Sequence;
215
216 typedef struct {
217 WORD SubstFormat; /* = 1 */
218 WORD Coverage;
219 WORD LigSetCount;
220 WORD LigatureSet[1];
221 }GSUB_LigatureSubstFormat1;
222
223 typedef struct {
224 WORD LigatureCount;
225 WORD Ligature[1];
226 }GSUB_LigatureSet;
227
228 typedef struct{
229 WORD LigGlyph;
230 WORD CompCount;
231 WORD Component[1];
232 }GSUB_Ligature;
233
234 typedef struct{
235 WORD SequenceIndex;
236 WORD LookupListIndex;
237
238 }GSUB_SubstLookupRecord;
239
240 typedef struct{
241 WORD SubstFormat; /* = 1 */
242 WORD Coverage;
243 WORD ChainSubRuleSetCount;
244 WORD ChainSubRuleSet[1];
245 }GSUB_ChainContextSubstFormat1;
246
247 typedef struct {
248 WORD SubstFormat; /* = 3 */
249 WORD BacktrackGlyphCount;
250 WORD Coverage[1];
251 }GSUB_ChainContextSubstFormat3_1;
252
253 typedef struct{
254 WORD InputGlyphCount;
255 WORD Coverage[1];
256 }GSUB_ChainContextSubstFormat3_2;
257
258 typedef struct{
259 WORD LookaheadGlyphCount;
260 WORD Coverage[1];
261 }GSUB_ChainContextSubstFormat3_3;
262
263 typedef struct{
264 WORD SubstCount;
265 GSUB_SubstLookupRecord SubstLookupRecord[1];
266 }GSUB_ChainContextSubstFormat3_4;
267
268 typedef struct {
269 WORD SubstFormat; /* = 1 */
270 WORD Coverage;
271 WORD AlternateSetCount;
272 WORD AlternateSet[1];
273 } GSUB_AlternateSubstFormat1;
274
275 typedef struct{
276 WORD GlyphCount;
277 WORD Alternate[1];
278 } GSUB_AlternateSet;
279
280 /* These are all structures needed for the GPOS table */
281
282 typedef struct {
283 DWORD version;
284 WORD ScriptList;
285 WORD FeatureList;
286 WORD LookupList;
287 } GPOS_Header;
288
289 typedef struct {
290 WORD StartSize;
291 WORD EndSize;
292 WORD DeltaFormat;
293 WORD DeltaValue[1];
294 } OT_DeviceTable;
295
296 typedef struct {
297 WORD AnchorFormat;
298 WORD XCoordinate;
299 WORD YCoordinate;
300 } GPOS_AnchorFormat1;
301
302 typedef struct {
303 WORD AnchorFormat;
304 WORD XCoordinate;
305 WORD YCoordinate;
306 WORD AnchorPoint;
307 } GPOS_AnchorFormat2;
308
309 typedef struct {
310 WORD AnchorFormat;
311 WORD XCoordinate;
312 WORD YCoordinate;
313 WORD XDeviceTable;
314 WORD YDeviceTable;
315 } GPOS_AnchorFormat3;
316
317 typedef struct {
318 WORD XPlacement;
319 WORD YPlacement;
320 WORD XAdvance;
321 WORD YAdvance;
322 WORD XPlaDevice;
323 WORD YPlaDevice;
324 WORD XAdvDevice;
325 WORD YAdvDevice;
326 } GPOS_ValueRecord;
327
328 typedef struct {
329 WORD PosFormat;
330 WORD Coverage;
331 WORD ValueFormat;
332 WORD Value[1];
333 } GPOS_SinglePosFormat1;
334
335 typedef struct {
336 WORD PosFormat;
337 WORD Coverage;
338 WORD ValueFormat;
339 WORD ValueCount;
340 WORD Value[1];
341 } GPOS_SinglePosFormat2;
342
343 typedef struct {
344 WORD PosFormat;
345 WORD Coverage;
346 WORD ValueFormat1;
347 WORD ValueFormat2;
348 WORD PairSetCount;
349 WORD PairSetOffset[1];
350 } GPOS_PairPosFormat1;
351
352 typedef struct {
353 WORD PosFormat;
354 WORD Coverage;
355 WORD ValueFormat1;
356 WORD ValueFormat2;
357 WORD ClassDef1;
358 WORD ClassDef2;
359 WORD Class1Count;
360 WORD Class2Count;
361 WORD Class1Record[1];
362 } GPOS_PairPosFormat2;
363
364 typedef struct {
365 WORD SecondGlyph;
366 WORD Value1[1];
367 WORD Value2[1];
368 } GPOS_PairValueRecord;
369
370 typedef struct {
371 WORD PairValueCount;
372 GPOS_PairValueRecord PairValueRecord[1];
373 } GPOS_PairSet;
374
375 typedef struct {
376 WORD EntryAnchor;
377 WORD ExitAnchor;
378 } GPOS_EntryExitRecord;
379
380 typedef struct {
381 WORD PosFormat;
382 WORD Coverage;
383 WORD EntryExitCount;
384 GPOS_EntryExitRecord EntryExitRecord[1];
385 } GPOS_CursivePosFormat1;
386
387 typedef struct {
388 WORD PosFormat;
389 WORD MarkCoverage;
390 WORD BaseCoverage;
391 WORD ClassCount;
392 WORD MarkArray;
393 WORD BaseArray;
394 } GPOS_MarkBasePosFormat1;
395
396 typedef struct {
397 WORD BaseAnchor[1];
398 } GPOS_BaseRecord;
399
400 typedef struct {
401 WORD BaseCount;
402 GPOS_BaseRecord BaseRecord[1];
403 } GPOS_BaseArray;
404
405 typedef struct {
406 WORD Class;
407 WORD MarkAnchor;
408 } GPOS_MarkRecord;
409
410 typedef struct {
411 WORD MarkCount;
412 GPOS_MarkRecord MarkRecord[1];
413 } GPOS_MarkArray;
414
415 typedef struct {
416 WORD PosFormat;
417 WORD MarkCoverage;
418 WORD LigatureCoverage;
419 WORD ClassCount;
420 WORD MarkArray;
421 WORD LigatureArray;
422 } GPOS_MarkLigPosFormat1;
423
424 typedef struct {
425 WORD LigatureCount;
426 WORD LigatureAttach[1];
427 } GPOS_LigatureArray;
428
429 typedef struct {
430 WORD LigatureAnchor[1];
431 } GPOS_ComponentRecord;
432
433 typedef struct {
434 WORD ComponentCount;
435 GPOS_ComponentRecord ComponentRecord[1];
436 } GPOS_LigatureAttach;
437
438 typedef struct {
439 WORD PosFormat;
440 WORD Mark1Coverage;
441 WORD Mark2Coverage;
442 WORD ClassCount;
443 WORD Mark1Array;
444 WORD Mark2Array;
445 } GPOS_MarkMarkPosFormat1;
446
447 typedef struct {
448 WORD Mark2Anchor[1];
449 } GPOS_Mark2Record;
450
451 typedef struct {
452 WORD Mark2Count;
453 GPOS_Mark2Record Mark2Record[1];
454 } GPOS_Mark2Array;
455
456 typedef struct {
457 WORD SequenceIndex;
458 WORD LookupListIndex;
459 } GPOS_PosLookupRecord;
460
461 typedef struct {
462 WORD PosFormat;
463 WORD BacktrackGlyphCount;
464 WORD Coverage[1];
465 } GPOS_ChainContextPosFormat3_1;
466
467 typedef struct {
468 WORD InputGlyphCount;
469 WORD Coverage[1];
470 } GPOS_ChainContextPosFormat3_2;
471
472 typedef struct {
473 WORD LookaheadGlyphCount;
474 WORD Coverage[1];
475 } GPOS_ChainContextPosFormat3_3;
476
477 typedef struct {
478 WORD PosCount;
479 GPOS_PosLookupRecord PosLookupRecord[1];
480 } GPOS_ChainContextPosFormat3_4;
481
482 typedef struct {
483 WORD PosFormat;
484 WORD ExtensionLookupType;
485 DWORD ExtensionOffset;
486 } GPOS_ExtensionPosFormat1;
487
488 /**********
489 * CMAP
490 **********/
491
492 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
493 {
494 CMAP_Header *CMAP_Table = NULL;
495 int length;
496 int i;
497
498 if (!psc->CMAP_Table)
499 {
500 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
501 if (length != GDI_ERROR)
502 {
503 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
504 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
505 TRACE("Loaded cmap table of %i bytes\n",length);
506 }
507 else
508 return NULL;
509 }
510
511 CMAP_Table = psc->CMAP_Table;
512
513 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
514 {
515 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
516 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
517 {
518 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
519 if (GET_BE_WORD(format->format) == 12)
520 return format;
521 }
522 }
523 return NULL;
524 }
525
526 static int compare_group(const void *a, const void* b)
527 {
528 const DWORD *chr = a;
529 const CMAP_SegmentedCoverage_group *group = b;
530
531 if (*chr < GET_BE_DWORD(group->startCharCode))
532 return -1;
533 if (*chr > GET_BE_DWORD(group->endCharCode))
534 return 1;
535 return 0;
536 }
537
538 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
539 {
540 /* BMP: use gdi32 for ease */
541 if (utf32c < 0x10000)
542 {
543 WCHAR ch = utf32c;
544 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
545 }
546
547 if (!psc->CMAP_format12_Table)
548 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
549
550 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
551 *pgi = 0xffff;
552 else
553 *pgi = 0;
554
555 if (psc->CMAP_format12_Table)
556 {
557 CMAP_SegmentedCoverage *format = NULL;
558 CMAP_SegmentedCoverage_group *group = NULL;
559
560 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
561
562 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
563 sizeof(CMAP_SegmentedCoverage_group), compare_group);
564
565 if (group)
566 {
567 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
568 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
569 return 0;
570 }
571 }
572 return 0;
573 }
574
575 /**********
576 * GDEF
577 **********/
578
579 static WORD OT_get_glyph_class(const void *table, WORD glyph)
580 {
581 WORD class = 0;
582 const OT_ClassDefFormat1 *cf1 = table;
583
584 if (!table) return 0;
585
586 if (GET_BE_WORD(cf1->ClassFormat) == 1)
587 {
588 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
589 {
590 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
591 if (index < GET_BE_WORD(cf1->GlyphCount))
592 class = GET_BE_WORD(cf1->ClassValueArray[index]);
593 }
594 }
595 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
596 {
597 const OT_ClassDefFormat2 *cf2 = table;
598 int i, top;
599 top = GET_BE_WORD(cf2->ClassRangeCount);
600 for (i = 0; i < top; i++)
601 {
602 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
603 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
604 {
605 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
606 break;
607 }
608 }
609 }
610 else
611 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
612
613 return class;
614 }
615
616 void OpenType_GDEF_UpdateGlyphProps(ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
617 {
618 int i;
619 void *glyph_class_table = NULL;
620
621 if (psc->GDEF_Table)
622 {
623 const GDEF_Header *header = psc->GDEF_Table;
624 WORD offset = GET_BE_WORD( header->GlyphClassDef );
625 if (offset)
626 glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
627 }
628
629 for (i = 0; i < cGlyphs; i++)
630 {
631 WORD class;
632 int char_count = 0;
633 int k;
634
635 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
636 if (k >= 0)
637 {
638 for (; k < cChars && pwLogClust[k] == i; k++)
639 char_count++;
640 }
641
642 class = OT_get_glyph_class( glyph_class_table, pwGlyphs[i] );
643
644 switch (class)
645 {
646 case 0:
647 case BaseGlyph:
648 pGlyphProp[i].sva.fClusterStart = 1;
649 pGlyphProp[i].sva.fDiacritic = 0;
650 pGlyphProp[i].sva.fZeroWidth = 0;
651 break;
652 case LigatureGlyph:
653 pGlyphProp[i].sva.fClusterStart = 1;
654 pGlyphProp[i].sva.fDiacritic = 0;
655 pGlyphProp[i].sva.fZeroWidth = 0;
656 break;
657 case MarkGlyph:
658 pGlyphProp[i].sva.fClusterStart = 0;
659 pGlyphProp[i].sva.fDiacritic = 1;
660 pGlyphProp[i].sva.fZeroWidth = 1;
661 break;
662 case ComponentGlyph:
663 pGlyphProp[i].sva.fClusterStart = 0;
664 pGlyphProp[i].sva.fDiacritic = 0;
665 pGlyphProp[i].sva.fZeroWidth = 0;
666 break;
667 default:
668 ERR("Unknown glyph class %i\n",class);
669 pGlyphProp[i].sva.fClusterStart = 1;
670 pGlyphProp[i].sva.fDiacritic = 0;
671 pGlyphProp[i].sva.fZeroWidth = 0;
672 }
673
674 if (char_count == 0)
675 pGlyphProp[i].sva.fClusterStart = 0;
676 }
677 }
678
679 /**********
680 * GSUB
681 **********/
682 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
683
684 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
685 {
686 const OT_CoverageFormat1* cf1;
687
688 cf1 = table;
689
690 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
691 {
692 int count = GET_BE_WORD(cf1->GlyphCount);
693 int i;
694 TRACE("Coverage Format 1, %i glyphs\n",count);
695 for (i = 0; i < count; i++)
696 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
697 return i;
698 return -1;
699 }
700 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
701 {
702 const OT_CoverageFormat2* cf2;
703 int i;
704 int count;
705 cf2 = (const OT_CoverageFormat2*)cf1;
706
707 count = GET_BE_WORD(cf2->RangeCount);
708 TRACE("Coverage Format 2, %i ranges\n",count);
709 for (i = 0; i < count; i++)
710 {
711 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
712 return -1;
713 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
714 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
715 {
716 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
717 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
718 }
719 }
720 return -1;
721 }
722 else
723 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
724
725 return -1;
726 }
727
728 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
729 {
730 int j;
731 TRACE("Single Substitution Subtable\n");
732
733 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
734 {
735 int offset;
736 const GSUB_SingleSubstFormat1 *ssf1;
737 offset = GET_BE_WORD(look->SubTable[j]);
738 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
739 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
740 {
741 int offset = GET_BE_WORD(ssf1->Coverage);
742 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
743 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
744 {
745 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
746 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
747 TRACE(" 0x%x\n",glyphs[glyph_index]);
748 return glyph_index + write_dir;
749 }
750 }
751 else
752 {
753 const GSUB_SingleSubstFormat2 *ssf2;
754 INT index;
755 INT offset;
756
757 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
758 offset = GET_BE_WORD(ssf1->Coverage);
759 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
760 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
761 TRACE(" Coverage index %i\n",index);
762 if (index != -1)
763 {
764 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
765 return GSUB_E_NOGLYPH;
766
767 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
768 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
769 TRACE("0x%x\n",glyphs[glyph_index]);
770 return glyph_index + write_dir;
771 }
772 }
773 }
774 return GSUB_E_NOGLYPH;
775 }
776
777 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
778 {
779 int j;
780 TRACE("Multiple Substitution Subtable\n");
781
782 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
783 {
784 int offset, index;
785 const GSUB_MultipleSubstFormat1 *msf1;
786 offset = GET_BE_WORD(look->SubTable[j]);
787 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
788
789 offset = GET_BE_WORD(msf1->Coverage);
790 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
791 if (index != -1)
792 {
793 const GSUB_Sequence *seq;
794 int sub_count;
795 int j;
796 offset = GET_BE_WORD(msf1->Sequence[index]);
797 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
798 sub_count = GET_BE_WORD(seq->GlyphCount);
799 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
800
801 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
802 glyphs[j] =glyphs[j-(sub_count-1)];
803
804 for (j = 0; j < sub_count; j++)
805 if (write_dir < 0)
806 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
807 else
808 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
809
810 *glyph_count = *glyph_count + (sub_count - 1);
811
812 if (TRACE_ON(uniscribe))
813 {
814 for (j = 0; j < sub_count; j++)
815 TRACE(" 0x%x",glyphs[glyph_index+j]);
816 TRACE("\n");
817 }
818
819 return glyph_index + (sub_count * write_dir);
820 }
821 }
822 return GSUB_E_NOGLYPH;
823 }
824
825 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
826 {
827 int j;
828 TRACE("Alternate Substitution Subtable\n");
829
830 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
831 {
832 int offset;
833 const GSUB_AlternateSubstFormat1 *asf1;
834 INT index;
835
836 offset = GET_BE_WORD(look->SubTable[j]);
837 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
838 offset = GET_BE_WORD(asf1->Coverage);
839
840 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
841 if (index != -1)
842 {
843 const GSUB_AlternateSet *as;
844 offset = GET_BE_WORD(asf1->AlternateSet[index]);
845 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
846 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
847 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
848 return GSUB_E_NOGLYPH;
849
850 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
851 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
852 TRACE(" 0x%x\n",glyphs[glyph_index]);
853 return glyph_index + write_dir;
854 }
855 }
856 return GSUB_E_NOGLYPH;
857 }
858
859 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
860 {
861 int j;
862
863 TRACE("Ligature Substitution Subtable\n");
864 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
865 {
866 const GSUB_LigatureSubstFormat1 *lsf1;
867 int offset,index;
868
869 offset = GET_BE_WORD(look->SubTable[j]);
870 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
871 offset = GET_BE_WORD(lsf1->Coverage);
872 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
873 TRACE(" Coverage index %i\n",index);
874 if (index != -1)
875 {
876 const GSUB_LigatureSet *ls;
877 int k, count;
878
879 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
880 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
881 count = GET_BE_WORD(ls->LigatureCount);
882 TRACE(" LigatureSet has %i members\n",count);
883 for (k = 0; k < count; k++)
884 {
885 const GSUB_Ligature *lig;
886 int CompCount,l,CompIndex;
887
888 offset = GET_BE_WORD(ls->Ligature[k]);
889 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
890 CompCount = GET_BE_WORD(lig->CompCount) - 1;
891 CompIndex = glyph_index+write_dir;
892 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
893 {
894 int CompGlyph;
895 CompGlyph = GET_BE_WORD(lig->Component[l]);
896 if (CompGlyph != glyphs[CompIndex])
897 break;
898 CompIndex += write_dir;
899 }
900 if (l == CompCount)
901 {
902 int replaceIdx = glyph_index;
903 if (write_dir < 0)
904 replaceIdx = glyph_index - CompCount;
905
906 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
907 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
908 TRACE("0x%x\n",glyphs[replaceIdx]);
909 if (CompCount > 0)
910 {
911 int j;
912 for (j = replaceIdx + 1; j < *glyph_count; j++)
913 glyphs[j] =glyphs[j+CompCount];
914 *glyph_count = *glyph_count - CompCount;
915 }
916 return replaceIdx + write_dir;
917 }
918 }
919 }
920 }
921 return GSUB_E_NOGLYPH;
922 }
923
924 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
925 {
926 int j;
927 BOOL done = FALSE;
928
929 TRACE("Chaining Contextual Substitution Subtable\n");
930 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
931 {
932 const GSUB_ChainContextSubstFormat1 *ccsf1;
933 int offset;
934 int dirLookahead = write_dir;
935 int dirBacktrack = -1 * write_dir;
936
937 offset = GET_BE_WORD(look->SubTable[j]);
938 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
939 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
940 {
941 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
942 continue;
943 }
944 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
945 {
946 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
947 continue;
948 }
949 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
950 {
951 int k;
952 int indexGlyphs;
953 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
954 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
955 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
956 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
957 int newIndex = glyph_index;
958
959 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
960
961 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
962
963 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
964 {
965 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
966 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
967 break;
968 }
969 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
970 continue;
971 TRACE("Matched Backtrack\n");
972
973 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
974 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
975
976 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
977 for (k = 0; k < indexGlyphs; k++)
978 {
979 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
980 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
981 break;
982 }
983 if (k != indexGlyphs)
984 continue;
985 TRACE("Matched IndexGlyphs\n");
986
987 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
988 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
989
990 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
991 {
992 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
993 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
994 break;
995 }
996 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
997 continue;
998 TRACE("Matched LookAhead\n");
999
1000 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
1001 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
1002
1003 if (GET_BE_WORD(ccsf3_4->SubstCount))
1004 {
1005 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
1006 {
1007 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
1008 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
1009
1010 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1011 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
1012 if (newIndex == -1)
1013 {
1014 ERR("Chain failed to generate a glyph\n");
1015 continue;
1016 }
1017 }
1018 return newIndex;
1019 }
1020 else return GSUB_E_NOGLYPH;
1021 }
1022 }
1023 return -1;
1024 }
1025
1026 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1027 {
1028 int offset;
1029 const OT_LookupTable *look;
1030
1031 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1032 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1033 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1034 switch(GET_BE_WORD(look->LookupType))
1035 {
1036 case 1:
1037 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1038 case 2:
1039 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1040 case 3:
1041 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1042 case 4:
1043 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1044 case 6:
1045 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1046 default:
1047 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1048 }
1049 return GSUB_E_NOGLYPH;
1050 }
1051
1052 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1053 {
1054 const GSUB_Header *header = (const GSUB_Header *)table;
1055 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1056
1057 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1058 }
1059
1060 /**********
1061 * GPOS
1062 **********/
1063 static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1064 const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset);
1065
1066 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1067 {
1068 static const WORD mask[3] = {3,0xf,0xff};
1069 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1070 {
1071 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1072 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1073 int value;
1074 TRACE("device table, format %i, index %i\n",format, index);
1075 index = index << format;
1076 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1077 TRACE("offset %i, value %i\n",index, value);
1078 if (value > mask[format-1]/2)
1079 value = -1 * ((mask[format-1]+1) - value);
1080 return value;
1081 }
1082 return 0;
1083 }
1084
1085 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1086 {
1087 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1088
1089 switch (GET_BE_WORD(anchor1->AnchorFormat))
1090 {
1091 case 1:
1092 {
1093 TRACE("Anchor Format 1\n");
1094 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1095 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1096 break;
1097 }
1098 case 2:
1099 {
1100 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1101 TRACE("Anchor Format 2\n");
1102 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1103 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1104 break;
1105 }
1106 case 3:
1107 {
1108 int offset;
1109 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1110 TRACE("Anchor Format 3\n");
1111 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1112 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1113 offset = GET_BE_WORD(anchor3->XDeviceTable);
1114 TRACE("ppem %i\n",ppem);
1115 if (offset)
1116 {
1117 const OT_DeviceTable* DeviceTableX = NULL;
1118 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1119 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1120 }
1121 offset = GET_BE_WORD(anchor3->YDeviceTable);
1122 if (offset)
1123 {
1124 const OT_DeviceTable* DeviceTableY = NULL;
1125 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1126 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1127 }
1128 break;
1129 }
1130 default:
1131 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1132 pt->x = 0;
1133 pt->y = 0;
1134 }
1135 }
1136
1137 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1138 {
1139 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1140
1141 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1142 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1143 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1144 if (lplogfont->lfWidth)
1145 FIXME("Font with lfWidth set no handled properly\n");
1146 }
1147
1148 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1149 {
1150 INT offset = 0;
1151 if (ValueFormat & 0x0001) { if (data) record->XPlacement = GET_BE_WORD(data[offset]); offset++; }
1152 if (ValueFormat & 0x0002) { if (data) record->YPlacement = GET_BE_WORD(data[offset]); offset++; }
1153 if (ValueFormat & 0x0004) { if (data) record->XAdvance = GET_BE_WORD(data[offset]); offset++; }
1154 if (ValueFormat & 0x0008) { if (data) record->YAdvance = GET_BE_WORD(data[offset]); offset++; }
1155 if (ValueFormat & 0x0010) { if (data) record->XPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1156 if (ValueFormat & 0x0020) { if (data) record->YPlaDevice = GET_BE_WORD(data[offset]); offset++; }
1157 if (ValueFormat & 0x0040) { if (data) record->XAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1158 if (ValueFormat & 0x0080) { if (data) record->YAdvDevice = GET_BE_WORD(data[offset]); offset++; }
1159 return offset;
1160 }
1161
1162 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1163 {
1164 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1165 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1166 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1167 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1168 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1169 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1170 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1171 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1172 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1173 }
1174
1175 static const BYTE *GPOS_get_subtable(const OT_LookupTable *look, int index)
1176 {
1177 int offset = GET_BE_WORD(look->SubTable[index]);
1178
1179 if (GET_BE_WORD(look->LookupType) == 9)
1180 {
1181 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + offset);
1182 if (GET_BE_WORD(ext->PosFormat) == 1)
1183 {
1184 offset += GET_BE_DWORD(ext->ExtensionOffset);
1185 }
1186 else
1187 {
1188 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1189 }
1190 }
1191 return (const BYTE *)look + offset;
1192 }
1193
1194 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1195 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1196 {
1197 int j;
1198
1199 TRACE("Single Adjustment Positioning Subtable\n");
1200
1201 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1202 {
1203 const GPOS_SinglePosFormat1 *spf1 = (const GPOS_SinglePosFormat1*)GPOS_get_subtable(look, j);
1204 WORD offset;
1205 if (GET_BE_WORD(spf1->PosFormat) == 1)
1206 {
1207 offset = GET_BE_WORD(spf1->Coverage);
1208 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1209 {
1210 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1211 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1212 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1213 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1214 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1215 }
1216 }
1217 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1218 {
1219 int index;
1220 const GPOS_SinglePosFormat2 *spf2;
1221 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1222 offset = GET_BE_WORD(spf2->Coverage);
1223 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1224 if (index != -1)
1225 {
1226 int size;
1227 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1228 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1229 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1230 if (index > 0)
1231 {
1232 offset = size * index;
1233 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1234 }
1235 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1236 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1237 }
1238 }
1239 else
1240 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1241 }
1242 }
1243
1244 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1245 INT ppem, POINT *adjust, POINT *advance )
1246 {
1247 GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1248 GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1249 INT size;
1250
1251 size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1252 GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1253
1254 if (val_fmt1)
1255 {
1256 GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1257 TRACE( "Glyph 1 resulting cumulative offset is %i,%i design units\n", adjust[0].x, adjust[0].y );
1258 TRACE( "Glyph 1 resulting cumulative advance is %i,%i design units\n", advance[0].x, advance[0].y );
1259 }
1260 if (val_fmt2)
1261 {
1262 GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1263 TRACE( "Glyph 2 resulting cumulative offset is %i,%i design units\n", adjust[1].x, adjust[1].y );
1264 TRACE( "Glyph 2 resulting cumulative advance is %i,%i design units\n", advance[1].x, advance[1].y );
1265 }
1266 }
1267
1268 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1269 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1270 {
1271 int j;
1272 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1273
1274 if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return glyph_index + 1;
1275
1276 TRACE("Pair Adjustment Positioning Subtable\n");
1277
1278 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1279 {
1280 const GPOS_PairPosFormat1 *ppf1 = (const GPOS_PairPosFormat1*)GPOS_get_subtable(look, j);
1281 WORD offset;
1282 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1283 {
1284 int index;
1285 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1286 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1287 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1288 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1289 offset = GET_BE_WORD(ppf1->Coverage);
1290 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1291 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1292 {
1293 int k;
1294 int pair_count;
1295 const GPOS_PairSet *ps;
1296 const GPOS_PairValueRecord *pair_val_rec;
1297 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1298 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1299 pair_count = GET_BE_WORD(ps->PairValueCount);
1300 pair_val_rec = ps->PairValueRecord;
1301 for (k = 0; k < pair_count; k++)
1302 {
1303 WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1304 if (glyphs[glyph_index+write_dir] == second_glyph)
1305 {
1306 int next = 1;
1307 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1308 apply_pair_value( ppf1, ValueFormat1, ValueFormat2, pair_val_rec->Value1, ppem, ptAdjust, ptAdvance );
1309 if (ValueFormat2) next++;
1310 return glyph_index + next;
1311 }
1312 pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1313 }
1314 }
1315 }
1316 else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1317 {
1318 const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)ppf1;
1319 int index;
1320 WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1321 WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1322 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1323 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1324 WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1325 WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1326
1327 offset = GET_BE_WORD( ppf2->Coverage );
1328 index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1329 if (index != -1)
1330 {
1331 WORD class1, class2;
1332 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1333 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1334 if (class1 < class1_count && class2 < class2_count)
1335 {
1336 const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1337 int next = 1;
1338
1339 TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1340
1341 apply_pair_value( ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, ptAdjust, ptAdvance );
1342 if (ValueFormat2) next++;
1343 return glyph_index + next;
1344 }
1345 }
1346 }
1347 else
1348 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1349 }
1350 return glyph_index+1;
1351 }
1352
1353 static VOID GPOS_apply_CursiveAttachment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1354 INT glyph_count, INT ppem, LPPOINT pt)
1355 {
1356 int j;
1357 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1358
1359 if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return;
1360
1361 TRACE("Cursive Attachment Positioning Subtable\n");
1362
1363 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1364 {
1365 const GPOS_CursivePosFormat1 *cpf1 = (const GPOS_CursivePosFormat1 *)GPOS_get_subtable(look, j);
1366 if (GET_BE_WORD(cpf1->PosFormat) == 1)
1367 {
1368 int index_exit, index_entry;
1369 WORD offset = GET_BE_WORD( cpf1->Coverage );
1370 index_exit = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index]);
1371 if (index_exit != -1 && cpf1->EntryExitRecord[index_exit].ExitAnchor!= 0)
1372 {
1373 index_entry = GSUB_is_glyph_covered((const BYTE*)cpf1+offset, glyphs[glyph_index+write_dir]);
1374 if (index_entry != -1 && cpf1->EntryExitRecord[index_entry].EntryAnchor != 0)
1375 {
1376 POINT exit_pt, entry_pt;
1377 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_exit].ExitAnchor);
1378 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &exit_pt, ppem);
1379 offset = GET_BE_WORD(cpf1->EntryExitRecord[index_entry].EntryAnchor);
1380 GPOS_get_anchor_values((const BYTE*)cpf1 + offset, &entry_pt, ppem);
1381 TRACE("Found linkage %x[%i,%i] %x[%i,%i]\n",glyphs[glyph_index], exit_pt.x,exit_pt.y, glyphs[glyph_index+write_dir], entry_pt.x, entry_pt.y);
1382 pt->x = entry_pt.x - exit_pt.x;
1383 pt->y = entry_pt.y - exit_pt.y;
1384 return;
1385 }
1386 }
1387 }
1388 else
1389 FIXME("Cursive Attachment Positioning: Format %i Unhandled\n",GET_BE_WORD(cpf1->PosFormat));
1390 }
1391 return;
1392 }
1393
1394 static int GPOS_apply_MarkToBase(ScriptCache *psc, const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index, INT glyph_count, INT ppem, LPPOINT pt)
1395 {
1396 int j;
1397 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1398 void *glyph_class_table = NULL;
1399 int rc = -1;
1400
1401 if (psc->GDEF_Table)
1402 {
1403 const GDEF_Header *header = psc->GDEF_Table;
1404 WORD offset = GET_BE_WORD( header->GlyphClassDef );
1405 if (offset)
1406 glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
1407 }
1408
1409 TRACE("MarkToBase Attachment Positioning Subtable\n");
1410
1411 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1412 {
1413 const GPOS_MarkBasePosFormat1 *mbpf1 = (const GPOS_MarkBasePosFormat1 *)GPOS_get_subtable(look, j);
1414 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1415 {
1416 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1417 int mark_index;
1418 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1419 if (mark_index != -1)
1420 {
1421 int base_index;
1422 int base_glyph = glyph_index - write_dir;
1423
1424 if (glyph_class_table)
1425 {
1426 while (OT_get_glyph_class(glyph_class_table, glyphs[base_glyph]) == MarkGlyph && base_glyph > 0 && base_glyph < glyph_count)
1427 base_glyph -= write_dir;
1428 }
1429
1430 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1431 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[base_glyph]);
1432 if (base_index != -1)
1433 {
1434 const GPOS_MarkArray *ma;
1435 const GPOS_MarkRecord *mr;
1436 const GPOS_BaseArray *ba;
1437 const GPOS_BaseRecord *br;
1438 int mark_class;
1439 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1440 int baserecord_size;
1441 POINT base_pt;
1442 POINT mark_pt;
1443 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[base_glyph], base_index);
1444 offset = GET_BE_WORD(mbpf1->MarkArray);
1445 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1446 if (mark_index > GET_BE_WORD(ma->MarkCount))
1447 {
1448 ERR("Mark index exeeded mark count\n");
1449 return -1;
1450 }
1451 mr = &ma->MarkRecord[mark_index];
1452 mark_class = GET_BE_WORD(mr->Class);
1453 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1454 offset = GET_BE_WORD(mbpf1->BaseArray);
1455 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1456 baserecord_size = class_count * sizeof(WORD);
1457 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1458 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1459 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1460 offset = GET_BE_WORD(mr->MarkAnchor);
1461 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1462 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1463 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1464 pt->x += base_pt.x - mark_pt.x;
1465 pt->y += base_pt.y - mark_pt.y;
1466 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1467 rc = base_glyph;
1468 }
1469 }
1470 }
1471 else
1472 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1473 }
1474 return rc;
1475 }
1476
1477 static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1478 INT glyph_count, INT ppem, LPPOINT pt)
1479 {
1480 int j;
1481 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1482
1483 TRACE("MarkToLigature Attachment Positioning Subtable\n");
1484
1485 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1486 {
1487 const GPOS_MarkLigPosFormat1 *mlpf1 = (const GPOS_MarkLigPosFormat1 *)GPOS_get_subtable(look, j);
1488 if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1489 {
1490 int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1491 int mark_index;
1492 mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
1493 if (mark_index != -1)
1494 {
1495 int ligature_index;
1496 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
1497 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
1498 if (ligature_index != -1)
1499 {
1500 const GPOS_MarkArray *ma;
1501 const GPOS_MarkRecord *mr;
1502
1503 const GPOS_LigatureArray *la;
1504 const GPOS_LigatureAttach *lt;
1505 int mark_class;
1506 int class_count = GET_BE_WORD(mlpf1->ClassCount);
1507 int component_count;
1508 int component_size;
1509 int i;
1510 POINT ligature_pt;
1511 POINT mark_pt;
1512
1513 TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
1514 offset = GET_BE_WORD(mlpf1->MarkArray);
1515 ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
1516 if (mark_index > GET_BE_WORD(ma->MarkCount))
1517 {
1518 ERR("Mark index exeeded mark count\n");
1519 return;
1520 }
1521 mr = &ma->MarkRecord[mark_index];
1522 mark_class = GET_BE_WORD(mr->Class);
1523 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1524 offset = GET_BE_WORD(mlpf1->LigatureArray);
1525 la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
1526 if (ligature_index > GET_BE_WORD(la->LigatureCount))
1527 {
1528 ERR("Ligature index exeeded ligature count\n");
1529 return;
1530 }
1531 offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
1532 lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
1533
1534 component_count = GET_BE_WORD(lt->ComponentCount);
1535 component_size = class_count * sizeof(WORD);
1536 offset = 0;
1537 for (i = 0; i < component_count && !offset; i++)
1538 {
1539 int k;
1540 const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
1541 for (k = 0; k < class_count && !offset; k++)
1542 offset = GET_BE_WORD(cr->LigatureAnchor[k]);
1543 cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
1544 }
1545 if (!offset)
1546 {
1547 ERR("Failed to find avalible ligature connection point\n");
1548 return;
1549 }
1550
1551 GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
1552 offset = GET_BE_WORD(mr->MarkAnchor);
1553 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1554 TRACE("Offset on ligature is %i,%i design units\n",ligature_pt.x,ligature_pt.y);
1555 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1556 pt->x += ligature_pt.x - mark_pt.x;
1557 pt->y += ligature_pt.y - mark_pt.y;
1558 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1559 }
1560 }
1561 }
1562 else
1563 FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
1564 }
1565 }
1566
1567 static BOOL GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1568 INT glyph_count, INT ppem, LPPOINT pt)
1569 {
1570 int j;
1571 BOOL rc = FALSE;
1572 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1573
1574 TRACE("MarkToMark Attachment Positioning Subtable\n");
1575
1576 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1577 {
1578 const GPOS_MarkMarkPosFormat1 *mmpf1 = (const GPOS_MarkMarkPosFormat1 *)GPOS_get_subtable(look, j);
1579 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
1580 {
1581 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
1582 int mark_index;
1583 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
1584 if (mark_index != -1)
1585 {
1586 int mark2_index;
1587 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
1588 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
1589 if (mark2_index != -1)
1590 {
1591 const GPOS_MarkArray *ma;
1592 const GPOS_MarkRecord *mr;
1593 const GPOS_Mark2Array *m2a;
1594 const GPOS_Mark2Record *m2r;
1595 int mark_class;
1596 int class_count = GET_BE_WORD(mmpf1->ClassCount);
1597 int mark2record_size;
1598 POINT mark2_pt;
1599 POINT mark_pt;
1600 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
1601 offset = GET_BE_WORD(mmpf1->Mark1Array);
1602 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
1603 if (mark_index > GET_BE_WORD(ma->MarkCount))
1604 {
1605 ERR("Mark index exceeded mark count\n");
1606 return FALSE;
1607 }
1608 mr = &ma->MarkRecord[mark_index];
1609 mark_class = GET_BE_WORD(mr->Class);
1610 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1611 offset = GET_BE_WORD(mmpf1->Mark2Array);
1612 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
1613 mark2record_size = class_count * sizeof(WORD);
1614 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
1615 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
1616 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
1617 offset = GET_BE_WORD(mr->MarkAnchor);
1618 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1619 TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
1620 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1621 pt->x += mark2_pt.x - mark_pt.x;
1622 pt->y += mark2_pt.y - mark_pt.y;
1623 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1624 rc = TRUE;
1625 }
1626 }
1627 }
1628 else
1629 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
1630 }
1631 return rc;
1632 }
1633
1634 static INT GPOS_apply_ChainContextPos(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1635 const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index,
1636 INT glyph_count, INT ppem, GOFFSET *pGoffset)
1637 {
1638 int j;
1639 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1640
1641 TRACE("Chaining Contextual Positioning Subtable\n");
1642
1643 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1644 {
1645 int offset;
1646 const GPOS_ChainContextPosFormat3_1 *ccpf3 = (GPOS_ChainContextPosFormat3_1 *)GPOS_get_subtable(look, j);
1647 int dirLookahead = write_dir;
1648 int dirBacktrack = -1 * write_dir;
1649
1650 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1651 {
1652 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1653 continue;
1654 }
1655 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1656 {
1657 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1658 continue;
1659 }
1660 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1661 {
1662 int k;
1663 int indexGlyphs;
1664 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1665 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1666 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1667
1668 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1669
1670 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1671 {
1672 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1673 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1674 break;
1675 }
1676 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1677 continue;
1678 TRACE("Matched Backtrack\n");
1679
1680 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
1681 FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
1682
1683 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1684 for (k = 0; k < indexGlyphs; k++)
1685 {
1686 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1687 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1688 break;
1689 }
1690 if (k != indexGlyphs)
1691 continue;
1692 TRACE("Matched IndexGlyphs\n");
1693
1694 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
1695 FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
1696
1697 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1698 {
1699 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1700 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1701 break;
1702 }
1703 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1704 continue;
1705 TRACE("Matched LookAhead\n");
1706
1707 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
1708 FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
1709
1710 if (GET_BE_WORD(ccpf3_4->PosCount))
1711 {
1712 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1713 {
1714 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1715 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1716
1717 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1718 GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, glyph_count, pGoffset);
1719 }
1720 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1721 }
1722 else return glyph_index + 1;
1723 }
1724 else
1725 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1726 }
1727 return glyph_index + 1;
1728 }
1729
1730 static INT GPOS_apply_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1731 {
1732 int offset;
1733 const OT_LookupTable *look;
1734 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1735 WORD type;
1736
1737 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1738 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1739 type = GET_BE_WORD(look->LookupType);
1740 TRACE("type %i, flag %x, subtables %i\n",type,GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1741 if (type == 9)
1742 {
1743 if (GET_BE_WORD(look->SubTableCount))
1744 {
1745 const GPOS_ExtensionPosFormat1 *ext = (const GPOS_ExtensionPosFormat1 *)((const BYTE *)look + GET_BE_WORD(look->SubTable[0]));
1746 if (GET_BE_WORD(ext->PosFormat) == 1)
1747 {
1748 type = GET_BE_WORD(ext->ExtensionLookupType);
1749 TRACE("extension type %i\n",type);
1750 }
1751 else
1752 {
1753 FIXME("Unhandled Extension Positioning Format %i\n",GET_BE_WORD(ext->PosFormat));
1754 }
1755 }
1756 else
1757 {
1758 WARN("lookup type is Extension Positioning but no extension subtable exists\n");
1759 }
1760 }
1761 switch (type)
1762 {
1763 case 1:
1764 {
1765 double devX, devY;
1766 POINT adjust = {0,0};
1767 POINT advance = {0,0};
1768 GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
1769 if (adjust.x || adjust.y)
1770 {
1771 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1772 pGoffset[glyph_index].du += round(devX);
1773 pGoffset[glyph_index].dv += round(devY);
1774 }
1775 if (advance.x || advance.y)
1776 {
1777 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1778 piAdvance[glyph_index] += round(devX);
1779 if (advance.y)
1780 FIXME("Unhandled adjustment to Y advancement\n");
1781 }
1782 break;
1783 }
1784 case 2:
1785 {
1786 POINT advance[2]= {{0,0},{0,0}};
1787 POINT adjust[2]= {{0,0},{0,0}};
1788 double devX, devY;
1789 int index;
1790 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1791 int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
1792
1793 index = GPOS_apply_PairAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, adjust, advance);
1794 if (adjust[0].x || adjust[0].y)
1795 {
1796 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1797 pGoffset[glyph_index].du += round(devX) * offset_sign;
1798 pGoffset[glyph_index].dv += round(devY);
1799 }
1800 if (advance[0].x || advance[0].y)
1801 {
1802 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1803 piAdvance[glyph_index] += round(devX);
1804 }
1805 if (adjust[1].x || adjust[1].y)
1806 {
1807 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1808 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
1809 pGoffset[glyph_index + write_dir].dv += round(devY);
1810 }
1811 if (advance[1].x || advance[1].y)
1812 {
1813 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1814 piAdvance[glyph_index + write_dir] += round(devX);
1815 }
1816 return index;
1817 }
1818 case 3:
1819 {
1820 POINT desU = {0,0};
1821 double devX, devY;
1822 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1823
1824 GPOS_apply_CursiveAttachment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1825 if (desU.x || desU.y)
1826 {
1827 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1828 /* Windows does not appear to apply X offsets here */
1829 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index+write_dir].dv;
1830 }
1831 break;
1832 }
1833 case 4:
1834 {
1835 double devX, devY;
1836 POINT desU = {0,0};
1837 int base_index = GPOS_apply_MarkToBase(psc, look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1838 if (base_index != -1)
1839 {
1840 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1841 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[base_index];
1842 else
1843 {
1844 if (analysis->fLogicalOrder) devX *= -1;
1845 pGoffset[glyph_index].du = round(devX);
1846 }
1847 pGoffset[glyph_index].dv = round(devY);
1848 }
1849 break;
1850 }
1851 case 5:
1852 {
1853 double devX, devY;
1854 POINT desU = {0,0};
1855 GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1856 if (desU.x || desU.y)
1857 {
1858 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1859 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
1860 pGoffset[glyph_index].dv = round(devY);
1861 }
1862 break;
1863 }
1864 case 6:
1865 {
1866 double devX, devY;
1867 POINT desU = {0,0};
1868 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1869 if (GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU))
1870 {
1871 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1872 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
1873 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
1874 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
1875 }
1876 break;
1877 }
1878 case 8:
1879 {
1880 return GPOS_apply_ChainContextPos(psc, lpotm, lplogfont, analysis, piAdvance, lookup, look, glyphs, glyph_index, glyph_count, ppem, pGoffset);
1881 }
1882 default:
1883 FIXME("We do not handle SubType %i\n",type);
1884 }
1885 return glyph_index+1;
1886 }
1887
1888 INT OpenType_apply_GPOS_lookup(ScriptCache *psc, LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1889 {
1890 const GPOS_Header *header = (const GPOS_Header *)psc->GPOS_Table;
1891 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1892
1893 return GPOS_apply_lookup(psc, lpotm, lplogfont, analysis, piAdvance, lookup, lookup_index, glyphs, glyph_index, glyph_count, pGoffset);
1894 }
1895
1896 static void GSUB_initialize_script_cache(ScriptCache *psc)
1897 {
1898 int i;
1899
1900 if (psc->GSUB_Table)
1901 {
1902 const OT_ScriptList *script;
1903 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1904 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1905 psc->script_count = GET_BE_WORD(script->ScriptCount);
1906 TRACE("initializing %i scripts in this font\n",psc->script_count);
1907 if (psc->script_count)
1908 {
1909 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1910 for (i = 0; i < psc->script_count; i++)
1911 {
1912 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1913 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]);
1914 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1915 }
1916 }
1917 }
1918 }
1919
1920 static void GPOS_expand_script_cache(ScriptCache *psc)
1921 {
1922 int i, count;
1923 const OT_ScriptList *script;
1924 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1925
1926 if (!header)
1927 return;
1928
1929 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1930 count = GET_BE_WORD(script->ScriptCount);
1931
1932 if (!count)
1933 return;
1934
1935 if (!psc->script_count)
1936 {
1937 psc->script_count = count;
1938 TRACE("initializing %i scripts in this font\n",psc->script_count);
1939 if (psc->script_count)
1940 {
1941 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1942 for (i = 0; i < psc->script_count; i++)
1943 {
1944 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1945 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]);
1946 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1947 }
1948 }
1949 }
1950 else
1951 {
1952 for (i = 0; i < count; i++)
1953 {
1954 int j;
1955 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1956 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]);
1957 for (j = 0; j < psc->script_count; j++)
1958 {
1959 if (psc->scripts[j].tag == tag)
1960 {
1961 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1962 break;
1963 }
1964 }
1965 if (j == psc->script_count)
1966 {
1967 psc->script_count++;
1968 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1969 psc->scripts[j].tag = tag;
1970 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1971 }
1972 }
1973 }
1974 }
1975
1976 static void _initialize_script_cache(ScriptCache *psc)
1977 {
1978 if (!psc->scripts_initialized)
1979 {
1980 GSUB_initialize_script_cache(psc);
1981 GPOS_expand_script_cache(psc);
1982 psc->scripts_initialized = TRUE;
1983 }
1984 }
1985
1986 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1987 {
1988 int i;
1989 HRESULT rc = S_OK;
1990
1991 _initialize_script_cache(psc);
1992
1993 *pcTags = psc->script_count;
1994
1995 if (!searchingFor && cMaxTags < *pcTags)
1996 rc = E_OUTOFMEMORY;
1997 else if (searchingFor)
1998 rc = USP_E_SCRIPT_NOT_IN_FONT;
1999
2000 for (i = 0; i < psc->script_count; i++)
2001 {
2002 if (i < cMaxTags)
2003 pScriptTags[i] = psc->scripts[i].tag;
2004
2005 if (searchingFor)
2006 {
2007 if (searchingFor == psc->scripts[i].tag)
2008 {
2009 pScriptTags[0] = psc->scripts[i].tag;
2010 *pcTags = 1;
2011 rc = S_OK;
2012 break;
2013 }
2014 }
2015 }
2016 return rc;
2017 }
2018
2019 static void GSUB_initialize_language_cache(LoadedScript *script)
2020 {
2021 int i;
2022
2023 if (script->gsub_table)
2024 {
2025 DWORD offset;
2026 const OT_Script* table = script->gsub_table;
2027 script->language_count = GET_BE_WORD(table->LangSysCount);
2028 offset = GET_BE_WORD(table->DefaultLangSys);
2029 if (offset)
2030 {
2031 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
2032 script->default_language.gsub_table = (const BYTE*)table + offset;
2033 }
2034
2035 if (script->language_count)
2036 {
2037 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
2038
2039 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2040
2041 for (i = 0; i < script->language_count; i++)
2042 {
2043 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2044 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]);
2045 script->languages[i].gsub_table = ((const BYTE*)table + offset);
2046 }
2047 }
2048 }
2049 }
2050
2051 static void GPOS_expand_language_cache(LoadedScript *script)
2052 {
2053 int count;
2054 const OT_Script* table = script->gpos_table;
2055 DWORD offset;
2056
2057 if (!table)
2058 return;
2059
2060 offset = GET_BE_WORD(table->DefaultLangSys);
2061 if (offset)
2062 script->default_language.gpos_table = (const BYTE*)table + offset;
2063
2064 count = GET_BE_WORD(table->LangSysCount);
2065
2066 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
2067
2068 if (!count)
2069 return;
2070
2071 if (!script->language_count)
2072 {
2073 int i;
2074 script->language_count = count;
2075
2076 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
2077
2078 for (i = 0; i < script->language_count; i++)
2079 {
2080 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2081 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]);
2082 script->languages[i].gpos_table = ((const BYTE*)table + offset);
2083 }
2084 }
2085 else if (count)
2086 {
2087 int i,j;
2088 for (i = 0; i < count; i++)
2089 {
2090 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
2091 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]);
2092
2093 for (j = 0; j < script->language_count; j++)
2094 {
2095 if (script->languages[j].tag == tag)
2096 {
2097 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2098 break;
2099 }
2100 }
2101 if (j == script->language_count)
2102 {
2103 script->language_count++;
2104 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
2105 script->languages[j].tag = tag;
2106 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2107 }
2108 }
2109 }
2110 }
2111
2112 static void _initialize_language_cache(LoadedScript *script)
2113 {
2114 if (!script->languages_initialized)
2115 {
2116 GSUB_initialize_language_cache(script);
2117 GPOS_expand_language_cache(script);
2118 script->languages_initialized = TRUE;
2119 }
2120 }
2121
2122 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2123 {
2124 int i;
2125 HRESULT rc = S_OK;
2126 LoadedScript *script = NULL;
2127
2128 _initialize_script_cache(psc);
2129
2130 for (i = 0; i < psc->script_count; i++)
2131 {
2132 if (psc->scripts[i].tag == script_tag)
2133 {
2134 script = &psc->scripts[i];
2135 break;
2136 }
2137 }
2138
2139 if (!script)
2140 return E_INVALIDARG;
2141
2142 _initialize_language_cache(script);
2143
2144 if (!searchingFor && cMaxTags < script->language_count)
2145 rc = E_OUTOFMEMORY;
2146 else if (searchingFor)
2147 rc = E_INVALIDARG;
2148
2149 *pcTags = script->language_count;
2150
2151 for (i = 0; i < script->language_count; i++)
2152 {
2153 if (i < cMaxTags)
2154 pLanguageTags[i] = script->languages[i].tag;
2155
2156 if (searchingFor)
2157 {
2158 if (searchingFor == script->languages[i].tag)
2159 {
2160 pLanguageTags[0] = script->languages[i].tag;
2161 *pcTags = 1;
2162 rc = S_OK;
2163 break;
2164 }
2165 }
2166 }
2167
2168 if (script->default_language.gsub_table)
2169 {
2170 if (i < cMaxTags)
2171 pLanguageTags[i] = script->default_language.tag;
2172
2173 if (searchingFor && FAILED(rc))
2174 {
2175 pLanguageTags[0] = script->default_language.tag;
2176 }
2177 i++;
2178 *pcTags = (*pcTags) + 1;
2179 }
2180
2181 return rc;
2182 }
2183
2184
2185 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
2186 {
2187 int i;
2188
2189 if (language->gsub_table)
2190 {
2191 const OT_LangSys *lang = language->gsub_table;
2192 const GSUB_Header *header = (const GSUB_Header *)table;
2193 const OT_FeatureList *feature_list;
2194
2195 language->feature_count = GET_BE_WORD(lang->FeatureCount);
2196 TRACE("%i features\n",language->feature_count);
2197
2198 if (language->feature_count)
2199 {
2200 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2201
2202 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2203
2204 for (i = 0; i < language->feature_count; i++)
2205 {
2206 const OT_Feature *feature;
2207 int j;
2208 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2209
2210 language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
2211 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2212 feature = (const OT_Feature*)language->features[i].feature;
2213 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2214 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2215 for (j = 0; j < language->features[i].lookup_count; j++)
2216 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2217 language->features[i].tableType = FEATURE_GSUB_TABLE;
2218 }
2219 }
2220 }
2221 }
2222
2223 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
2224 {
2225 int i, count;
2226 const OT_LangSys *lang = language->gpos_table;
2227 const GPOS_Header *header = (const GPOS_Header *)table;
2228 const OT_FeatureList *feature_list;
2229
2230 if (!lang)
2231 return;
2232
2233 count = GET_BE_WORD(lang->FeatureCount);
2234 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2235
2236 TRACE("%i features\n",count);
2237
2238 if (!count)
2239 return;
2240
2241 if (!language->feature_count)
2242 {
2243 language->feature_count = count;
2244
2245 if (language->feature_count)
2246 {
2247 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2248
2249 for (i = 0; i < language->feature_count; i++)
2250 {
2251 const OT_Feature *feature;
2252 int j;
2253 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2254
2255 language->features[i].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
2256 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2257 feature = (const OT_Feature*)language->features[i].feature;
2258 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2259 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2260 for (j = 0; j < language->features[i].lookup_count; j++)
2261 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2262 language->features[i].tableType = FEATURE_GPOS_TABLE;
2263 }
2264 }
2265 }
2266 else
2267 {
2268 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
2269
2270 for (i = 0; i < count; i++)
2271 {
2272 const OT_Feature *feature;
2273 int j;
2274 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2275 int idx = language->feature_count + i;
2276
2277 language->features[idx].tag = MS_MAKE_TAG(feature_list->FeatureRecord[index].FeatureTag[0], feature_list->FeatureRecord[index].FeatureTag[1], feature_list->FeatureRecord[index].FeatureTag[2], feature_list->FeatureRecord[index].FeatureTag[3]);
2278 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2279 feature = (const OT_Feature*)language->features[idx].feature;
2280 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
2281 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
2282 for (j = 0; j < language->features[idx].lookup_count; j++)
2283 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2284 language->features[idx].tableType = FEATURE_GPOS_TABLE;
2285 }
2286 language->feature_count += count;
2287 }
2288 }
2289
2290 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2291 {
2292 if (!language->features_initialized)
2293 {
2294 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
2295 GPOS_expand_feature_cache(psc->GPOS_Table, language);
2296 language->features_initialized = TRUE;
2297 }
2298 }
2299
2300 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)
2301 {
2302 int i;
2303 HRESULT rc = S_OK;
2304 LoadedScript *script = NULL;
2305 LoadedLanguage *language = NULL;
2306
2307 _initialize_script_cache(psc);
2308
2309 for (i = 0; i < psc->script_count; i++)
2310 {
2311 if (psc->scripts[i].tag == script_tag)
2312 {
2313 script = &psc->scripts[i];
2314 break;
2315 }
2316 }
2317
2318 if (!script)
2319 {
2320 *pcTags = 0;
2321 if (!filtered)
2322 return S_OK;
2323 else
2324 return E_INVALIDARG;
2325 }
2326
2327 _initialize_language_cache(script);
2328
2329 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2330 language = &script->default_language;
2331 else
2332 {
2333 for (i = 0; i < script->language_count; i++)
2334 {
2335 if (script->languages[i].tag == language_tag)
2336 {
2337 language = &script->languages[i];
2338 break;
2339 }
2340 }
2341 }
2342
2343 if (!language)
2344 {
2345 *pcTags = 0;
2346 return S_OK;
2347 }
2348
2349 _initialize_feature_cache(psc, language);
2350
2351 if (tableType)
2352 {
2353 *pcTags = 0;
2354 for (i = 0; i < language->feature_count; i++)
2355 if (language->features[i].tableType == tableType)
2356 *pcTags = (*pcTags)+1;
2357 }
2358 else
2359 *pcTags = language->feature_count;
2360
2361 if (!searchingFor && cMaxTags < *pcTags)
2362 rc = E_OUTOFMEMORY;
2363 else if (searchingFor)
2364 rc = E_INVALIDARG;
2365
2366 for (i = 0; i < language->feature_count; i++)
2367 {
2368 if (i < cMaxTags)
2369 {
2370 if (!tableType || language->features[i].tableType == tableType)
2371 pFeatureTags[i] = language->features[i].tag;
2372 }
2373
2374 if (searchingFor)
2375 {
2376 if ((searchingFor == language->features[i].tag) &&
2377 (!tableType || language->features[i].tableType == tableType))
2378 {
2379 pFeatureTags[0] = language->features[i].tag;
2380 *pcTags = 1;
2381 if (feature)
2382 *feature = &language->features[i];
2383 rc = S_OK;
2384 break;
2385 }
2386 }
2387 }
2388 return rc;
2389 }