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
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)
42 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
43 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
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')
55 } CMAP_EncodingRecord;
60 CMAP_EncodingRecord tables[1];
67 } CMAP_SegmentedCoverage_group;
75 CMAP_SegmentedCoverage_group groups[1];
76 } CMAP_SegmentedCoverage;
78 /* These are all structures needed for the GDEF table */
79 #define GDEF_TAG MS_MAKE_TAG('G', 'D', 'E', 'F')
81 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
88 WORD MarkAttachClassDef;
95 WORD ClassValueArray[1];
96 } GDEF_ClassDefFormat1;
102 } GDEF_ClassRangeRecord;
106 WORD ClassRangeCount;
107 GDEF_ClassRangeRecord ClassRangeRecord[1];
108 } GDEF_ClassDefFormat2;
110 /* These are all structures needed for the GSUB table */
126 OT_ScriptRecord ScriptRecord[1];
137 OT_LangSysRecord LangSysRecord[1];
141 WORD LookupOrder; /* Reserved */
142 WORD ReqFeatureIndex;
144 WORD FeatureIndex[1];
154 OT_FeatureRecord FeatureRecord[1];
158 WORD FeatureParams; /* Reserved */
160 WORD LookupListIndex[1];
179 } OT_CoverageFormat1;
184 WORD StartCoverageIndex;
190 OT_RangeRecord RangeRecord[1];
191 } OT_CoverageFormat2;
194 WORD SubstFormat; /* = 1 */
197 } GSUB_SingleSubstFormat1;
200 WORD SubstFormat; /* = 2 */
204 }GSUB_SingleSubstFormat2;
207 WORD SubstFormat; /* = 1 */
211 }GSUB_MultipleSubstFormat1;
219 WORD SubstFormat; /* = 1 */
223 }GSUB_LigatureSubstFormat1;
238 WORD LookupListIndex;
240 }GSUB_SubstLookupRecord;
243 WORD SubstFormat; /* = 1 */
245 WORD ChainSubRuleSetCount;
246 WORD ChainSubRuleSet[1];
247 }GSUB_ChainContextSubstFormat1;
250 WORD SubstFormat; /* = 3 */
251 WORD BacktrackGlyphCount;
253 }GSUB_ChainContextSubstFormat3_1;
256 WORD InputGlyphCount;
258 }GSUB_ChainContextSubstFormat3_2;
261 WORD LookaheadGlyphCount;
263 }GSUB_ChainContextSubstFormat3_3;
267 GSUB_SubstLookupRecord SubstLookupRecord[1];
268 }GSUB_ChainContextSubstFormat3_4;
271 WORD SubstFormat; /* = 1 */
273 WORD AlternateSetCount;
274 WORD AlternateSet[1];
275 } GSUB_AlternateSubstFormat1;
282 /* These are all structures needed for the GPOS table */
302 } GPOS_AnchorFormat1;
309 } GPOS_AnchorFormat2;
317 } GPOS_AnchorFormat3;
335 } GPOS_SinglePosFormat1;
343 } GPOS_SinglePosFormat2;
351 WORD PairSetOffset[1];
352 } GPOS_PairPosFormat1;
358 } GPOS_PairValueRecord;
362 GPOS_PairValueRecord PairValueRecord[1];
372 } GPOS_MarkBasePosFormat1;
380 GPOS_BaseRecord BaseRecord[1];
390 GPOS_MarkRecord MarkRecord[1];
400 } GPOS_MarkMarkPosFormat1;
408 GPOS_Mark2Record Mark2Record[1];
413 WORD LookupListIndex;
414 } GPOS_PosLookupRecord;
418 WORD BacktrackGlyphCount;
420 } GPOS_ChainContextPosFormat3_1;
423 WORD InputGlyphCount;
425 } GPOS_ChainContextPosFormat3_2;
428 WORD LookaheadGlyphCount;
430 } GPOS_ChainContextPosFormat3_3;
434 GPOS_PosLookupRecord PosLookupRecord[1];
435 } GPOS_ChainContextPosFormat3_4;
441 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
443 CMAP_Header *CMAP_Table = NULL;
447 if (!psc->CMAP_Table)
449 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
450 if (length != GDI_ERROR)
452 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
453 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
454 TRACE("Loaded cmap table of %i bytes\n",length);
460 CMAP_Table = psc->CMAP_Table;
462 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
464 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
465 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
467 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
468 if (GET_BE_WORD(format->format) == 12)
475 static int compare_group(const void *a, const void* b)
477 const DWORD *chr = a;
478 const CMAP_SegmentedCoverage_group *group = b;
480 if (*chr < GET_BE_DWORD(group->startCharCode))
482 if (*chr > GET_BE_DWORD(group->endCharCode))
487 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
489 /* BMP: use gdi32 for ease */
490 if (utf32c < 0x10000)
493 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
496 if (!psc->CMAP_format12_Table)
497 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
499 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
504 if (psc->CMAP_format12_Table)
506 CMAP_SegmentedCoverage *format = NULL;
507 CMAP_SegmentedCoverage_group *group = NULL;
509 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
511 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
512 sizeof(CMAP_SegmentedCoverage_group), compare_group);
516 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
517 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
528 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
532 const GDEF_ClassDefFormat1 *cf1;
537 offset = GET_BE_WORD(header->GlyphClassDef);
541 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
542 if (GET_BE_WORD(cf1->ClassFormat) == 1)
544 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
546 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
547 if (index < GET_BE_WORD(cf1->GlyphCount))
548 class = GET_BE_WORD(cf1->ClassValueArray[index]);
551 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
553 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
555 top = GET_BE_WORD(cf2->ClassRangeCount);
556 for (i = 0; i < top; i++)
558 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
559 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
561 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
567 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
572 static VOID *load_gdef_table(HDC hdc)
574 VOID* GDEF_Table = NULL;
575 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
576 if (length != GDI_ERROR)
578 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
579 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
580 TRACE("Loaded GDEF table of %i bytes\n",length);
585 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
589 if (!psc->GDEF_Table)
590 psc->GDEF_Table = load_gdef_table(hdc);
592 for (i = 0; i < cGlyphs; i++)
598 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
601 for (; k < cChars && pwLogClust[k] == i; k++)
605 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
611 pGlyphProp[i].sva.fClusterStart = 1;
612 pGlyphProp[i].sva.fDiacritic = 0;
613 pGlyphProp[i].sva.fZeroWidth = 0;
616 pGlyphProp[i].sva.fClusterStart = 1;
617 pGlyphProp[i].sva.fDiacritic = 0;
618 pGlyphProp[i].sva.fZeroWidth = 0;
621 pGlyphProp[i].sva.fClusterStart = 0;
622 pGlyphProp[i].sva.fDiacritic = 1;
623 pGlyphProp[i].sva.fZeroWidth = 1;
626 pGlyphProp[i].sva.fClusterStart = 0;
627 pGlyphProp[i].sva.fDiacritic = 0;
628 pGlyphProp[i].sva.fZeroWidth = 0;
631 ERR("Unknown glyph class %i\n",class);
632 pGlyphProp[i].sva.fClusterStart = 1;
633 pGlyphProp[i].sva.fDiacritic = 0;
634 pGlyphProp[i].sva.fZeroWidth = 0;
638 pGlyphProp[i].sva.fClusterStart = 0;
645 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
647 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
649 const OT_CoverageFormat1* cf1;
653 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
655 int count = GET_BE_WORD(cf1->GlyphCount);
657 TRACE("Coverage Format 1, %i glyphs\n",count);
658 for (i = 0; i < count; i++)
659 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
663 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
665 const OT_CoverageFormat2* cf2;
668 cf2 = (const OT_CoverageFormat2*)cf1;
670 count = GET_BE_WORD(cf2->RangeCount);
671 TRACE("Coverage Format 2, %i ranges\n",count);
672 for (i = 0; i < count; i++)
674 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
676 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
677 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
679 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
680 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
686 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
691 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
694 TRACE("Single Substitution Subtable\n");
696 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
699 const GSUB_SingleSubstFormat1 *ssf1;
700 offset = GET_BE_WORD(look->SubTable[j]);
701 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
702 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
704 int offset = GET_BE_WORD(ssf1->Coverage);
705 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
706 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
708 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
709 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
710 TRACE(" 0x%x\n",glyphs[glyph_index]);
711 return glyph_index + write_dir;
716 const GSUB_SingleSubstFormat2 *ssf2;
720 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
721 offset = GET_BE_WORD(ssf1->Coverage);
722 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
723 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
724 TRACE(" Coverage index %i\n",index);
727 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
728 return GSUB_E_NOGLYPH;
730 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
731 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
732 TRACE("0x%x\n",glyphs[glyph_index]);
733 return glyph_index + write_dir;
737 return GSUB_E_NOGLYPH;
740 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
743 TRACE("Multiple Substitution Subtable\n");
745 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
748 const GSUB_MultipleSubstFormat1 *msf1;
749 offset = GET_BE_WORD(look->SubTable[j]);
750 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
752 offset = GET_BE_WORD(msf1->Coverage);
753 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
756 const GSUB_Sequence *seq;
759 offset = GET_BE_WORD(msf1->Sequence[index]);
760 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
761 sub_count = GET_BE_WORD(seq->GlyphCount);
762 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
764 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
765 glyphs[j] =glyphs[j-(sub_count-1)];
767 for (j = 0; j < sub_count; j++)
769 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
771 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
773 *glyph_count = *glyph_count + (sub_count - 1);
775 if (TRACE_ON(uniscribe))
777 for (j = 0; j < sub_count; j++)
778 TRACE(" 0x%x",glyphs[glyph_index+j]);
782 return glyph_index + (sub_count * write_dir);
785 return GSUB_E_NOGLYPH;
788 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
791 TRACE("Alternate Substitution Subtable\n");
793 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
796 const GSUB_AlternateSubstFormat1 *asf1;
799 offset = GET_BE_WORD(look->SubTable[j]);
800 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
801 offset = GET_BE_WORD(asf1->Coverage);
803 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
806 const GSUB_AlternateSet *as;
807 offset = GET_BE_WORD(asf1->AlternateSet[index]);
808 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
809 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
810 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
811 return GSUB_E_NOGLYPH;
813 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
814 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
815 TRACE(" 0x%x\n",glyphs[glyph_index]);
816 return glyph_index + write_dir;
819 return GSUB_E_NOGLYPH;
822 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
826 TRACE("Ligature Substitution Subtable\n");
827 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
829 const GSUB_LigatureSubstFormat1 *lsf1;
832 offset = GET_BE_WORD(look->SubTable[j]);
833 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
834 offset = GET_BE_WORD(lsf1->Coverage);
835 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
836 TRACE(" Coverage index %i\n",index);
839 const GSUB_LigatureSet *ls;
842 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
843 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
844 count = GET_BE_WORD(ls->LigatureCount);
845 TRACE(" LigatureSet has %i members\n",count);
846 for (k = 0; k < count; k++)
848 const GSUB_Ligature *lig;
849 int CompCount,l,CompIndex;
851 offset = GET_BE_WORD(ls->Ligature[k]);
852 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
853 CompCount = GET_BE_WORD(lig->CompCount) - 1;
854 CompIndex = glyph_index+write_dir;
855 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
858 CompGlyph = GET_BE_WORD(lig->Component[l]);
859 if (CompGlyph != glyphs[CompIndex])
861 CompIndex += write_dir;
865 int replaceIdx = glyph_index;
867 replaceIdx = glyph_index - CompCount;
869 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
870 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
871 TRACE("0x%x\n",glyphs[replaceIdx]);
875 for (j = replaceIdx + 1; j < *glyph_count; j++)
876 glyphs[j] =glyphs[j+CompCount];
877 *glyph_count = *glyph_count - CompCount;
879 return replaceIdx + write_dir;
884 return GSUB_E_NOGLYPH;
887 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
892 TRACE("Chaining Contextual Substitution Subtable\n");
893 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
895 const GSUB_ChainContextSubstFormat1 *ccsf1;
897 int dirLookahead = write_dir;
898 int dirBacktrack = -1 * write_dir;
900 offset = GET_BE_WORD(look->SubTable[j]);
901 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
902 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
904 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
907 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
909 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
912 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
916 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
917 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
918 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
919 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
920 int newIndex = glyph_index;
922 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
924 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
926 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
928 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
929 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
932 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
934 TRACE("Matched Backtrack\n");
936 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
937 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
939 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
940 for (k = 0; k < indexGlyphs; k++)
942 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
943 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
946 if (k != indexGlyphs)
948 TRACE("Matched IndexGlyphs\n");
950 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
951 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
953 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
955 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
956 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
959 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
961 TRACE("Matched LookAhead\n");
963 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
964 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
966 if (GET_BE_WORD(ccsf3_4->SubstCount))
968 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
970 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
971 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
973 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
974 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
977 ERR("Chain failed to generate a glyph\n");
983 else return GSUB_E_NOGLYPH;
989 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
992 const OT_LookupTable *look;
994 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
995 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
996 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
997 switch(GET_BE_WORD(look->LookupType))
1000 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1002 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1004 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1006 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1008 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1010 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1012 return GSUB_E_NOGLYPH;
1015 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1017 const GSUB_Header *header = (const GSUB_Header *)table;
1018 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1020 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1026 static INT GPOS_apply_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset);
1028 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1030 static const WORD mask[3] = {3,0xf,0xff};
1031 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1033 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1034 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1036 TRACE("device table, format %i, index %i\n",format, index);
1037 index = index << format;
1038 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1039 TRACE("offset %i, value %i\n",index, value);
1040 if (value > mask[format-1]/2)
1041 value = -1 * ((mask[format-1]+1) - value);
1047 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1049 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1051 switch (GET_BE_WORD(anchor1->AnchorFormat))
1055 TRACE("Anchor Format 1\n");
1056 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1057 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1062 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1063 TRACE("Anchor Format 2\n");
1064 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1065 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1071 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1072 TRACE("Anchor Format 3\n");
1073 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1074 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1075 offset = GET_BE_WORD(anchor3->XDeviceTable);
1076 TRACE("ppem %i\n",ppem);
1079 const OT_DeviceTable* DeviceTableX = NULL;
1080 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1081 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1083 offset = GET_BE_WORD(anchor3->YDeviceTable);
1086 const OT_DeviceTable* DeviceTableY = NULL;
1087 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1088 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1093 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1099 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1101 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1103 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1104 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1105 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1106 if (lplogfont->lfWidth)
1107 FIXME("Font with lfWidth set no handled properly\n");
1110 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1113 if (ValueFormat & 0x0001) record->XPlacement = GET_BE_WORD(data[offset++]);
1114 if (ValueFormat & 0x0002) record->YPlacement = GET_BE_WORD(data[offset++]);
1115 if (ValueFormat & 0x0004) record->XAdvance = GET_BE_WORD(data[offset++]);
1116 if (ValueFormat & 0x0008) record->YAdvance = GET_BE_WORD(data[offset++]);
1117 if (ValueFormat & 0x0010) record->XPlaDevice = GET_BE_WORD(data[offset++]);
1118 if (ValueFormat & 0x0020) record->YPlaDevice = GET_BE_WORD(data[offset++]);
1119 if (ValueFormat & 0x0040) record->XAdvDevice = GET_BE_WORD(data[offset++]);
1120 if (ValueFormat & 0x0080) record->YAdvDevice = GET_BE_WORD(data[offset++]);
1124 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1126 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1127 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1128 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1129 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1130 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1131 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1132 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1133 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1134 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1137 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1141 TRACE("Single Adjustment Positioning Subtable\n");
1143 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1145 const GPOS_SinglePosFormat1 *spf1;
1146 WORD offset = GET_BE_WORD(look->SubTable[j]);
1147 spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1148 if (GET_BE_WORD(spf1->PosFormat) == 1)
1150 offset = GET_BE_WORD(spf1->Coverage);
1151 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1153 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1154 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1155 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1156 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1157 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1160 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1163 const GPOS_SinglePosFormat2 *spf2;
1164 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1165 offset = GET_BE_WORD(spf2->Coverage);
1166 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1170 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1171 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1172 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1175 offset = size * index;
1176 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1178 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1179 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1183 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1187 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1191 TRACE("Pair Adjustment Positioning Subtable\n");
1193 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1195 const GPOS_PairPosFormat1 *ppf1;
1196 WORD offset = GET_BE_WORD(look->SubTable[j]);
1197 ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1198 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1201 offset = GET_BE_WORD(ppf1->Coverage);
1202 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1203 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1207 const GPOS_PairSet *ps;
1208 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1209 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1210 pair_count = GET_BE_WORD(ps->PairValueCount);
1211 for (k = 0; k < pair_count; k++)
1213 WORD second_glyph = GET_BE_WORD(ps->PairValueRecord[k].SecondGlyph);
1214 if (glyphs[glyph_index+write_dir] == second_glyph)
1217 GPOS_ValueRecord ValueRecord1 = {0,0,0,0,0,0,0,0};
1218 GPOS_ValueRecord ValueRecord2 = {0,0,0,0,0,0,0,0};
1219 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1220 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1222 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1224 offset = GPOS_get_value_record(ValueFormat1, ps->PairValueRecord[k].Value1, &ValueRecord1);
1225 GPOS_get_value_record(ValueFormat2, (WORD*)((const BYTE*)(ps->PairValueRecord[k].Value2)+offset), &ValueRecord2);
1228 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord1, ValueFormat1, ppem, &ptAdjust[0], &ptAdvance[0]);
1229 TRACE("Glyph 1 resulting cumulative offset is %i,%i design units\n",ptAdjust[0].x,ptAdjust[0].y);
1230 TRACE("Glyph 1 resulting cumulative advance is %i,%i design units\n",ptAdvance[0].x,ptAdvance[0].y);
1234 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord2, ValueFormat2, ppem, &ptAdjust[1], &ptAdvance[1]);
1235 TRACE("Glyph 2 resulting cumulative offset is %i,%i design units\n",ptAdjust[1].x,ptAdjust[1].y);
1236 TRACE("Glyph 2 resulting cumulative advance is %i,%i design units\n",ptAdvance[1].x,ptAdvance[1].y);
1240 return glyph_index + next;
1246 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1248 return glyph_index+1;
1251 static VOID GPOS_apply_MarkToBase(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT pt)
1255 TRACE("MarkToBase Attachment Positioning Subtable\n");
1257 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1260 const GPOS_MarkBasePosFormat1 *mbpf1;
1261 offset = GET_BE_WORD(look->SubTable[j]);
1262 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1263 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1265 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1267 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1268 if (mark_index != -1)
1271 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1272 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1273 if (base_index != -1)
1275 const GPOS_MarkArray *ma;
1276 const GPOS_MarkRecord *mr;
1277 const GPOS_BaseArray *ba;
1278 const GPOS_BaseRecord *br;
1280 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1281 int baserecord_size;
1284 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1285 offset = GET_BE_WORD(mbpf1->MarkArray);
1286 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1287 if (mark_index > GET_BE_WORD(ma->MarkCount))
1289 ERR("Mark index exeeded mark count\n");
1292 mr = &ma->MarkRecord[mark_index];
1293 mark_class = GET_BE_WORD(mr->Class);
1294 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1295 offset = GET_BE_WORD(mbpf1->BaseArray);
1296 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1297 baserecord_size = class_count * sizeof(WORD);
1298 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1299 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1300 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1301 offset = GET_BE_WORD(mr->MarkAnchor);
1302 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1303 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1304 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1305 pt->x += base_pt.x - mark_pt.x;
1306 pt->y += base_pt.y - mark_pt.y;
1307 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1312 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1316 static VOID GPOS_apply_MarkToMark(const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, LPPOINT pt)
1320 TRACE("MarkToMark Attachment Positioning Subtable\n");
1322 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1325 const GPOS_MarkMarkPosFormat1 *mmpf1;
1326 offset = GET_BE_WORD(look->SubTable[j]);
1327 mmpf1 = (const GPOS_MarkMarkPosFormat1*)((const BYTE*)look+offset);
1328 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
1330 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
1332 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
1333 if (mark_index != -1)
1336 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
1337 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
1338 if (mark2_index != -1)
1340 const GPOS_MarkArray *ma;
1341 const GPOS_MarkRecord *mr;
1342 const GPOS_Mark2Array *m2a;
1343 const GPOS_Mark2Record *m2r;
1345 int class_count = GET_BE_WORD(mmpf1->ClassCount);
1346 int mark2record_size;
1349 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
1350 offset = GET_BE_WORD(mmpf1->Mark1Array);
1351 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
1352 if (mark_index > GET_BE_WORD(ma->MarkCount))
1354 ERR("Mark index exeeded mark count\n");
1357 mr = &ma->MarkRecord[mark_index];
1358 mark_class = GET_BE_WORD(mr->Class);
1359 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1360 offset = GET_BE_WORD(mmpf1->Mark2Array);
1361 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
1362 mark2record_size = class_count * sizeof(WORD);
1363 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
1364 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
1365 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
1366 offset = GET_BE_WORD(mr->MarkAnchor);
1367 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1368 TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
1369 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1370 pt->x += mark2_pt.x - mark_pt.x;
1371 pt->y += mark2_pt.y - mark_pt.y;
1372 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1377 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
1381 static INT GPOS_apply_ChainContextPos(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, INT ppem, GOFFSET *pGoffset)
1385 TRACE("Chaining Contextual Positioning Subtable\n");
1387 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1390 const GPOS_ChainContextPosFormat3_1 *ccpf3;
1391 int dirLookahead = write_dir;
1392 int dirBacktrack = -1 * write_dir;
1394 offset = GET_BE_WORD(look->SubTable[j]);
1395 ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1397 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1399 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1402 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1404 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1407 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1411 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1412 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1413 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1415 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1417 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1419 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1420 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1423 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1425 TRACE("Matched Backtrack\n");
1427 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
1428 FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
1430 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1431 for (k = 0; k < indexGlyphs; k++)
1433 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1434 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1437 if (k != indexGlyphs)
1439 TRACE("Matched IndexGlyphs\n");
1441 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
1442 FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
1444 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1446 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1447 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1450 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1452 TRACE("Matched LookAhead\n");
1454 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
1455 FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
1457 if (GET_BE_WORD(ccpf3_4->PosCount))
1459 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1461 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1462 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1464 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1465 GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count, pGoffset);
1467 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1469 else return glyph_index + 1;
1472 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1474 return glyph_index + 1;
1477 static INT GPOS_apply_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset)
1480 const OT_LookupTable *look;
1481 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1483 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1484 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1485 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1486 switch(GET_BE_WORD(look->LookupType))
1491 POINT adjust = {0,0};
1492 POINT advance = {0,0};
1493 GPOS_apply_SingleAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &adjust, &advance);
1494 if (adjust.x || adjust.y)
1496 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1497 pGoffset[glyph_index].du += round(devX);
1498 pGoffset[glyph_index].dv += round(devY);
1500 if (advance.x || advance.y)
1502 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1503 piAdvance[glyph_index] += round(devX);
1505 FIXME("Unhandled adjustment to Y advancement\n");
1511 POINT advance[2]= {{0,0},{0,0}};
1512 POINT adjust[2]= {{0,0},{0,0}};
1515 index = GPOS_apply_PairAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, adjust, advance);
1516 if (adjust[0].x || adjust[0].y)
1518 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1519 pGoffset[glyph_index].du += round(devX);
1520 pGoffset[glyph_index].dv += round(devY);
1522 if (advance[0].x || advance[0].y)
1524 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1525 piAdvance[glyph_index] += round(devX);
1527 if (adjust[1].x || adjust[1].y)
1529 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1530 pGoffset[glyph_index + write_dir].du += round(devX);
1531 pGoffset[glyph_index + write_dir].dv += round(devY);
1533 if (advance[1].x || advance[1].y)
1535 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1536 piAdvance[glyph_index + write_dir] += round(devX);
1544 GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1545 if (desU.x || desU.y)
1547 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1548 pGoffset[glyph_index].du += (round(devX) - piAdvance[glyph_index-1]);
1549 pGoffset[glyph_index].dv += round(devY);
1557 GPOS_apply_MarkToMark(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1558 if (desU.x || desU.y)
1560 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1561 pGoffset[glyph_index].du += round(devX) + pGoffset[glyph_index-1].du;
1562 pGoffset[glyph_index].dv += round(devY) + pGoffset[glyph_index-1].dv;
1568 return GPOS_apply_ChainContextPos(lpotm, lplogfont, piAdvance, lookup, look, glyphs, glyph_index, write_dir, glyph_count, ppem, pGoffset);
1571 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1573 return glyph_index+1;
1576 INT OpenType_apply_GPOS_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, INT* piAdvance, LPCVOID table, INT lookup_index, const WORD *glyphs, INT glyph_index, INT write_dir, INT glyph_count, GOFFSET *pGoffset)
1578 const GPOS_Header *header = (const GPOS_Header *)table;
1579 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1581 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1584 static void GSUB_initialize_script_cache(ScriptCache *psc)
1588 if (psc->GSUB_Table)
1590 const OT_ScriptList *script;
1591 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1592 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1593 psc->script_count = GET_BE_WORD(script->ScriptCount);
1594 TRACE("initializing %i scripts in this font\n",psc->script_count);
1595 if (psc->script_count)
1597 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1598 for (i = 0; i < psc->script_count; i++)
1600 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1601 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]);
1602 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1608 static void GPOS_expand_script_cache(ScriptCache *psc)
1611 const OT_ScriptList *script;
1612 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1617 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1618 count = GET_BE_WORD(script->ScriptCount);
1620 if (!psc->script_count)
1622 psc->script_count = count;
1623 TRACE("initializing %i scripts in this font\n",psc->script_count);
1624 if (psc->script_count)
1626 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1627 for (i = 0; i < psc->script_count; i++)
1629 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1630 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]);
1631 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1637 for (i = 0; i < count; i++)
1640 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1641 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]);
1642 for (j = 0; j < psc->script_count; j++)
1644 if (psc->scripts[j].tag == tag)
1646 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1650 if (j == psc->script_count)
1652 psc->script_count++;
1653 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1654 psc->scripts[j].tag = tag;
1655 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1661 static void _initialize_script_cache(ScriptCache *psc)
1663 if (!psc->script_count)
1665 GSUB_initialize_script_cache(psc);
1666 GPOS_expand_script_cache(psc);
1670 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1675 _initialize_script_cache(psc);
1677 *pcTags = psc->script_count;
1679 if (!searchingFor && cMaxTags < *pcTags)
1681 else if (searchingFor)
1682 rc = USP_E_SCRIPT_NOT_IN_FONT;
1684 for (i = 0; i < psc->script_count; i++)
1687 pScriptTags[i] = psc->scripts[i].tag;
1691 if (searchingFor == psc->scripts[i].tag)
1693 pScriptTags[0] = psc->scripts[i].tag;
1703 static void GSUB_initialize_language_cache(LoadedScript *script)
1707 if (script->gsub_table)
1710 const OT_Script* table = script->gsub_table;
1711 script->language_count = GET_BE_WORD(table->LangSysCount);
1712 offset = GET_BE_WORD(table->DefaultLangSys);
1715 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1716 script->default_language.gsub_table = (const BYTE*)table + offset;
1719 if (script->language_count)
1721 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1723 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1725 for (i = 0; i < script->language_count; i++)
1727 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1728 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]);
1729 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1735 static void GPOS_expand_language_cache(LoadedScript *script)
1738 const OT_Script* table = script->gpos_table;
1744 offset = GET_BE_WORD(table->DefaultLangSys);
1746 script->default_language.gpos_table = (const BYTE*)table + offset;
1748 count = GET_BE_WORD(table->LangSysCount);
1750 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1751 if (!script->language_count)
1754 script->language_count = count;
1756 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1758 for (i = 0; i < script->language_count; i++)
1760 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1761 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]);
1762 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1768 for (i = 0; i < count; i++)
1770 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1771 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]);
1773 for (j = 0; j < script->language_count; j++)
1775 if (script->languages[j].tag == tag)
1777 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1781 if (j == script->language_count)
1783 script->language_count++;
1784 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1785 script->languages[j].tag = tag;
1786 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1792 static void _initialize_language_cache(LoadedScript *script)
1794 if (!script->language_count)
1796 GSUB_initialize_language_cache(script);
1797 GPOS_expand_language_cache(script);
1801 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1805 LoadedScript *script = NULL;
1807 _initialize_script_cache(psc);
1809 for (i = 0; i < psc->script_count; i++)
1811 if (psc->scripts[i].tag == script_tag)
1813 script = &psc->scripts[i];
1819 return E_INVALIDARG;
1821 _initialize_language_cache(script);
1823 if (!searchingFor && cMaxTags < script->language_count)
1825 else if (searchingFor)
1828 *pcTags = script->language_count;
1830 for (i = 0; i < script->language_count; i++)
1833 pLanguageTags[i] = script->languages[i].tag;
1837 if (searchingFor == script->languages[i].tag)
1839 pLanguageTags[0] = script->languages[i].tag;
1847 if (script->default_language.gsub_table)
1850 pLanguageTags[i] = script->default_language.tag;
1852 if (searchingFor && FAILED(rc))
1854 pLanguageTags[0] = script->default_language.tag;
1857 *pcTags = (*pcTags) + 1;
1864 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1868 if (language->gsub_table)
1870 const OT_LangSys *lang = language->gsub_table;
1871 const GSUB_Header *header = (const GSUB_Header *)table;
1872 const OT_FeatureList *feature_list;
1874 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1875 TRACE("%i features\n",language->feature_count);
1877 if (language->feature_count)
1879 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1881 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1883 for (i = 0; i < language->feature_count; i++)
1885 const OT_Feature *feature;
1887 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1889 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]);
1890 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1891 feature = (const OT_Feature*)language->features[i].feature;
1892 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1893 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1894 for (j = 0; j < language->features[i].lookup_count; j++)
1895 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1901 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1904 const OT_LangSys *lang = language->gpos_table;
1905 const GPOS_Header *header = (const GPOS_Header *)table;
1906 const OT_FeatureList *feature_list;
1911 count = GET_BE_WORD(lang->FeatureCount);
1912 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1914 TRACE("%i features\n",count);
1915 if (!language->feature_count)
1917 language->feature_count = count;
1919 if (language->feature_count)
1921 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1923 for (i = 0; i < language->feature_count; i++)
1925 const OT_Feature *feature;
1927 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1929 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]);
1930 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1931 feature = (const OT_Feature*)language->features[i].feature;
1932 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1933 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1934 for (j = 0; j < language->features[i].lookup_count; j++)
1935 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1941 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1943 for (i = 0; i < count; i++)
1945 const OT_Feature *feature;
1947 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1948 int idx = language->feature_count + i;
1950 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]);
1951 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1952 feature = (const OT_Feature*)language->features[idx].feature;
1953 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1954 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1955 for (j = 0; j < language->features[idx].lookup_count; j++)
1956 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1958 language->feature_count += count;
1962 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1964 if (!language->feature_count)
1966 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1967 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1971 HRESULT OpenType_GetFontFeatureTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG language_tag, BOOL filtered, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pFeatureTags, int *pcTags, LoadedFeature** feature)
1975 LoadedScript *script = NULL;
1976 LoadedLanguage *language = NULL;
1978 _initialize_script_cache(psc);
1980 for (i = 0; i < psc->script_count; i++)
1982 if (psc->scripts[i].tag == script_tag)
1984 script = &psc->scripts[i];
1995 return E_INVALIDARG;
1998 _initialize_language_cache(script);
2000 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2001 language = &script->default_language;
2004 for (i = 0; i < script->language_count; i++)
2006 if (script->languages[i].tag == language_tag)
2008 language = &script->languages[i];
2020 _initialize_feature_cache(psc, language);
2022 *pcTags = language->feature_count;
2024 if (!searchingFor && cMaxTags < *pcTags)
2026 else if (searchingFor)
2029 for (i = 0; i < language->feature_count; i++)
2032 pFeatureTags[i] = language->features[i].tag;
2036 if (searchingFor == language->features[i].tag)
2038 pFeatureTags[0] = language->features[i].tag;
2041 *feature = &language->features[i];