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];
102 } OT_ClassRangeRecord;
106 WORD ClassRangeCount;
107 OT_ClassRangeRecord ClassRangeRecord[1];
108 } OT_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;
363 WORD Class1Record[1];
364 } GPOS_PairPosFormat2;
370 } GPOS_PairValueRecord;
374 GPOS_PairValueRecord PairValueRecord[1];
384 } GPOS_MarkBasePosFormat1;
392 GPOS_BaseRecord BaseRecord[1];
402 GPOS_MarkRecord MarkRecord[1];
408 WORD LigatureCoverage;
412 } GPOS_MarkLigPosFormat1;
416 WORD LigatureAttach[1];
417 } GPOS_LigatureArray;
420 WORD LigatureAnchor[1];
421 } GPOS_ComponentRecord;
425 GPOS_ComponentRecord ComponentRecord[1];
426 } GPOS_LigatureAttach;
435 } GPOS_MarkMarkPosFormat1;
443 GPOS_Mark2Record Mark2Record[1];
448 WORD LookupListIndex;
449 } GPOS_PosLookupRecord;
453 WORD BacktrackGlyphCount;
455 } GPOS_ChainContextPosFormat3_1;
458 WORD InputGlyphCount;
460 } GPOS_ChainContextPosFormat3_2;
463 WORD LookaheadGlyphCount;
465 } GPOS_ChainContextPosFormat3_3;
469 GPOS_PosLookupRecord PosLookupRecord[1];
470 } GPOS_ChainContextPosFormat3_4;
476 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
478 CMAP_Header *CMAP_Table = NULL;
482 if (!psc->CMAP_Table)
484 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
485 if (length != GDI_ERROR)
487 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
488 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
489 TRACE("Loaded cmap table of %i bytes\n",length);
495 CMAP_Table = psc->CMAP_Table;
497 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
499 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
500 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
502 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
503 if (GET_BE_WORD(format->format) == 12)
510 static int compare_group(const void *a, const void* b)
512 const DWORD *chr = a;
513 const CMAP_SegmentedCoverage_group *group = b;
515 if (*chr < GET_BE_DWORD(group->startCharCode))
517 if (*chr > GET_BE_DWORD(group->endCharCode))
522 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
524 /* BMP: use gdi32 for ease */
525 if (utf32c < 0x10000)
528 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
531 if (!psc->CMAP_format12_Table)
532 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
534 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
539 if (psc->CMAP_format12_Table)
541 CMAP_SegmentedCoverage *format = NULL;
542 CMAP_SegmentedCoverage_group *group = NULL;
544 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
546 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
547 sizeof(CMAP_SegmentedCoverage_group), compare_group);
551 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
552 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
563 static WORD OT_get_glyph_class(const void *table, WORD glyph)
566 const OT_ClassDefFormat1 *cf1 = table;
568 if (!table) return 0;
570 if (GET_BE_WORD(cf1->ClassFormat) == 1)
572 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
574 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
575 if (index < GET_BE_WORD(cf1->GlyphCount))
576 class = GET_BE_WORD(cf1->ClassValueArray[index]);
579 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
581 const OT_ClassDefFormat2 *cf2 = table;
583 top = GET_BE_WORD(cf2->ClassRangeCount);
584 for (i = 0; i < top; i++)
586 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
587 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
589 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
595 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
600 static VOID *load_gdef_table(HDC hdc)
602 VOID* GDEF_Table = NULL;
603 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
604 if (length != GDI_ERROR)
606 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
607 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
608 TRACE("Loaded GDEF table of %i bytes\n",length);
613 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
616 void *glyph_class_table = NULL;
618 if (!psc->GDEF_Table)
619 psc->GDEF_Table = load_gdef_table(hdc);
623 const GDEF_Header *header = psc->GDEF_Table;
624 WORD offset = GET_BE_WORD( header->GlyphClassDef );
626 glyph_class_table = (BYTE *)psc->GDEF_Table + offset;
629 for (i = 0; i < cGlyphs; i++)
635 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
638 for (; k < cChars && pwLogClust[k] == i; k++)
642 class = OT_get_glyph_class( glyph_class_table, pwGlyphs[i] );
648 pGlyphProp[i].sva.fClusterStart = 1;
649 pGlyphProp[i].sva.fDiacritic = 0;
650 pGlyphProp[i].sva.fZeroWidth = 0;
653 pGlyphProp[i].sva.fClusterStart = 1;
654 pGlyphProp[i].sva.fDiacritic = 0;
655 pGlyphProp[i].sva.fZeroWidth = 0;
658 pGlyphProp[i].sva.fClusterStart = 0;
659 pGlyphProp[i].sva.fDiacritic = 1;
660 pGlyphProp[i].sva.fZeroWidth = 1;
663 pGlyphProp[i].sva.fClusterStart = 0;
664 pGlyphProp[i].sva.fDiacritic = 0;
665 pGlyphProp[i].sva.fZeroWidth = 0;
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;
675 pGlyphProp[i].sva.fClusterStart = 0;
682 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
684 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
686 const OT_CoverageFormat1* cf1;
690 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
692 int count = GET_BE_WORD(cf1->GlyphCount);
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]))
700 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
702 const OT_CoverageFormat2* cf2;
705 cf2 = (const OT_CoverageFormat2*)cf1;
707 count = GET_BE_WORD(cf2->RangeCount);
708 TRACE("Coverage Format 2, %i ranges\n",count);
709 for (i = 0; i < count; i++)
711 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
713 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
714 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
716 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
717 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
723 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
728 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
731 TRACE("Single Substitution Subtable\n");
733 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
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)
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)
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;
753 const GSUB_SingleSubstFormat2 *ssf2;
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);
764 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
765 return GSUB_E_NOGLYPH;
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;
774 return GSUB_E_NOGLYPH;
777 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
780 TRACE("Multiple Substitution Subtable\n");
782 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
785 const GSUB_MultipleSubstFormat1 *msf1;
786 offset = GET_BE_WORD(look->SubTable[j]);
787 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
789 offset = GET_BE_WORD(msf1->Coverage);
790 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
793 const GSUB_Sequence *seq;
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));
801 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
802 glyphs[j] =glyphs[j-(sub_count-1)];
804 for (j = 0; j < sub_count; j++)
806 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
808 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
810 *glyph_count = *glyph_count + (sub_count - 1);
812 if (TRACE_ON(uniscribe))
814 for (j = 0; j < sub_count; j++)
815 TRACE(" 0x%x",glyphs[glyph_index+j]);
819 return glyph_index + (sub_count * write_dir);
822 return GSUB_E_NOGLYPH;
825 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
828 TRACE("Alternate Substitution Subtable\n");
830 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
833 const GSUB_AlternateSubstFormat1 *asf1;
836 offset = GET_BE_WORD(look->SubTable[j]);
837 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
838 offset = GET_BE_WORD(asf1->Coverage);
840 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
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;
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;
856 return GSUB_E_NOGLYPH;
859 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
863 TRACE("Ligature Substitution Subtable\n");
864 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
866 const GSUB_LigatureSubstFormat1 *lsf1;
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);
876 const GSUB_LigatureSet *ls;
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++)
885 const GSUB_Ligature *lig;
886 int CompCount,l,CompIndex;
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++)
895 CompGlyph = GET_BE_WORD(lig->Component[l]);
896 if (CompGlyph != glyphs[CompIndex])
898 CompIndex += write_dir;
902 int replaceIdx = glyph_index;
904 replaceIdx = glyph_index - CompCount;
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]);
912 for (j = replaceIdx + 1; j < *glyph_count; j++)
913 glyphs[j] =glyphs[j+CompCount];
914 *glyph_count = *glyph_count - CompCount;
916 return replaceIdx + write_dir;
921 return GSUB_E_NOGLYPH;
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)
929 TRACE("Chaining Contextual Substitution Subtable\n");
930 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
932 const GSUB_ChainContextSubstFormat1 *ccsf1;
934 int dirLookahead = write_dir;
935 int dirBacktrack = -1 * write_dir;
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)
941 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
944 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
946 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
949 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
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;
959 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
961 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
963 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
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)
969 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
971 TRACE("Matched Backtrack\n");
973 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)((BYTE *)ccsf1 +
974 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_1, Coverage[GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)]));
976 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
977 for (k = 0; k < indexGlyphs; k++)
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)
983 if (k != indexGlyphs)
985 TRACE("Matched IndexGlyphs\n");
987 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)((BYTE *)ccsf3_2 +
988 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_2, Coverage[GET_BE_WORD(ccsf3_2->InputGlyphCount)]));
990 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
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)
996 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
998 TRACE("Matched LookAhead\n");
1000 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)((BYTE *)ccsf3_3 +
1001 FIELD_OFFSET(GSUB_ChainContextSubstFormat3_3, Coverage[GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)]));
1003 if (GET_BE_WORD(ccsf3_4->SubstCount))
1005 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
1007 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
1008 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
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);
1014 ERR("Chain failed to generate a glyph\n");
1020 else return GSUB_E_NOGLYPH;
1026 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
1029 const OT_LookupTable *look;
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))
1037 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1039 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1041 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1043 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
1045 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
1047 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1049 return GSUB_E_NOGLYPH;
1052 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
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));
1057 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1063 static INT GPOS_apply_lookup(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);
1066 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1068 static const WORD mask[3] = {3,0xf,0xff};
1069 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1071 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1072 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
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);
1085 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1087 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1089 switch (GET_BE_WORD(anchor1->AnchorFormat))
1093 TRACE("Anchor Format 1\n");
1094 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1095 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
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);
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);
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);
1121 offset = GET_BE_WORD(anchor3->YDeviceTable);
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);
1131 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1137 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1139 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
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");
1148 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
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++; }
1162 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
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);
1175 static VOID GPOS_apply_SingleAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1176 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1180 TRACE("Single Adjustment Positioning Subtable\n");
1182 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1184 const GPOS_SinglePosFormat1 *spf1;
1185 WORD offset = GET_BE_WORD(look->SubTable[j]);
1186 spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1187 if (GET_BE_WORD(spf1->PosFormat) == 1)
1189 offset = GET_BE_WORD(spf1->Coverage);
1190 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1192 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1193 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1194 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1195 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1196 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1199 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1202 const GPOS_SinglePosFormat2 *spf2;
1203 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1204 offset = GET_BE_WORD(spf2->Coverage);
1205 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1209 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1210 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1211 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1214 offset = size * index;
1215 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1217 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1218 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1222 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1226 static void apply_pair_value( const void *pos_table, WORD val_fmt1, WORD val_fmt2, const WORD *pair,
1227 INT ppem, POINT *adjust, POINT *advance )
1229 GPOS_ValueRecord val_rec1 = {0,0,0,0,0,0,0,0};
1230 GPOS_ValueRecord val_rec2 = {0,0,0,0,0,0,0,0};
1233 size = GPOS_get_value_record( val_fmt1, pair, &val_rec1 );
1234 GPOS_get_value_record( val_fmt2, pair + size, &val_rec2 );
1238 GPOS_get_value_record_offsets( pos_table, &val_rec1, val_fmt1, ppem, adjust, advance );
1239 TRACE( "Glyph 1 resulting cumulative offset is %i,%i design units\n", adjust[0].x, adjust[0].y );
1240 TRACE( "Glyph 1 resulting cumulative advance is %i,%i design units\n", advance[0].x, advance[0].y );
1244 GPOS_get_value_record_offsets( pos_table, &val_rec2, val_fmt2, ppem, adjust + 1, advance + 1 );
1245 TRACE( "Glyph 2 resulting cumulative offset is %i,%i design units\n", adjust[1].x, adjust[1].y );
1246 TRACE( "Glyph 2 resulting cumulative advance is %i,%i design units\n", advance[1].x, advance[1].y );
1250 static INT GPOS_apply_PairAdjustment(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1251 INT glyph_count, INT ppem, LPPOINT ptAdjust, LPPOINT ptAdvance)
1254 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1256 if (glyph_index + write_dir < 0 || glyph_index + write_dir >= glyph_count) return glyph_index + 1;
1258 TRACE("Pair Adjustment Positioning Subtable\n");
1260 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1262 const GPOS_PairPosFormat1 *ppf1;
1263 WORD offset = GET_BE_WORD(look->SubTable[j]);
1264 ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1265 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1268 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1269 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1270 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1271 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1272 offset = GET_BE_WORD(ppf1->Coverage);
1273 index = GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]);
1274 if (index != -1 && index < GET_BE_WORD(ppf1->PairSetCount))
1278 const GPOS_PairSet *ps;
1279 const GPOS_PairValueRecord *pair_val_rec;
1280 offset = GET_BE_WORD(ppf1->PairSetOffset[index]);
1281 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1282 pair_count = GET_BE_WORD(ps->PairValueCount);
1283 pair_val_rec = ps->PairValueRecord;
1284 for (k = 0; k < pair_count; k++)
1286 WORD second_glyph = GET_BE_WORD(pair_val_rec->SecondGlyph);
1287 if (glyphs[glyph_index+write_dir] == second_glyph)
1290 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1291 apply_pair_value( ppf1, ValueFormat1, ValueFormat2, pair_val_rec->Value1, ppem, ptAdjust, ptAdvance );
1292 if (ValueFormat2) next++;
1293 return glyph_index + next;
1295 pair_val_rec = (const GPOS_PairValueRecord *)(pair_val_rec->Value1 + val_fmt1_size + val_fmt2_size);
1299 else if (GET_BE_WORD(ppf1->PosFormat) == 2)
1301 const GPOS_PairPosFormat2 *ppf2 = (const GPOS_PairPosFormat2*)((const BYTE*)look + offset);
1303 WORD ValueFormat1 = GET_BE_WORD( ppf2->ValueFormat1 );
1304 WORD ValueFormat2 = GET_BE_WORD( ppf2->ValueFormat2 );
1305 INT val_fmt1_size = GPOS_get_value_record( ValueFormat1, NULL, NULL );
1306 INT val_fmt2_size = GPOS_get_value_record( ValueFormat2, NULL, NULL );
1307 WORD class1_count = GET_BE_WORD( ppf2->Class1Count );
1308 WORD class2_count = GET_BE_WORD( ppf2->Class2Count );
1310 offset = GET_BE_WORD( ppf2->Coverage );
1311 index = GSUB_is_glyph_covered( (const BYTE*)ppf2 + offset, glyphs[glyph_index] );
1314 WORD class1, class2;
1315 class1 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef1), glyphs[glyph_index] );
1316 class2 = OT_get_glyph_class( (const BYTE *)ppf2 + GET_BE_WORD(ppf2->ClassDef2), glyphs[glyph_index + write_dir] );
1317 if (class1 < class1_count && class2 < class2_count)
1319 const WORD *pair_val = ppf2->Class1Record + (class1 * class2_count + class2) * (val_fmt1_size + val_fmt2_size);
1322 TRACE( "Format 2: Found Pair %x,%x\n", glyphs[glyph_index], glyphs[glyph_index + write_dir] );
1324 apply_pair_value( ppf2, ValueFormat1, ValueFormat2, pair_val, ppem, ptAdjust, ptAdvance );
1325 if (ValueFormat2) next++;
1326 return glyph_index + next;
1331 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1333 return glyph_index+1;
1336 static VOID GPOS_apply_MarkToBase(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1337 INT glyph_count, INT ppem, LPPOINT pt)
1340 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1342 TRACE("MarkToBase Attachment Positioning Subtable\n");
1344 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1347 const GPOS_MarkBasePosFormat1 *mbpf1;
1348 offset = GET_BE_WORD(look->SubTable[j]);
1349 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1350 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1352 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1354 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1355 if (mark_index != -1)
1358 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1359 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1360 if (base_index != -1)
1362 const GPOS_MarkArray *ma;
1363 const GPOS_MarkRecord *mr;
1364 const GPOS_BaseArray *ba;
1365 const GPOS_BaseRecord *br;
1367 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1368 int baserecord_size;
1371 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1372 offset = GET_BE_WORD(mbpf1->MarkArray);
1373 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1374 if (mark_index > GET_BE_WORD(ma->MarkCount))
1376 ERR("Mark index exeeded mark count\n");
1379 mr = &ma->MarkRecord[mark_index];
1380 mark_class = GET_BE_WORD(mr->Class);
1381 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1382 offset = GET_BE_WORD(mbpf1->BaseArray);
1383 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1384 baserecord_size = class_count * sizeof(WORD);
1385 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1386 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1387 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1388 offset = GET_BE_WORD(mr->MarkAnchor);
1389 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1390 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1391 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1392 pt->x += base_pt.x - mark_pt.x;
1393 pt->y += base_pt.y - mark_pt.y;
1394 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1399 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1403 static VOID GPOS_apply_MarkToLigature(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1404 INT glyph_count, INT ppem, LPPOINT pt)
1407 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1409 TRACE("MarkToLigature Attachment Positioning Subtable\n");
1411 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1414 const GPOS_MarkLigPosFormat1 *mlpf1;
1415 offset = GET_BE_WORD(look->SubTable[j]);
1416 mlpf1 = (const GPOS_MarkLigPosFormat1*)((const BYTE*)look+offset);
1417 if (GET_BE_WORD(mlpf1->PosFormat) == 1)
1419 int offset = GET_BE_WORD(mlpf1->MarkCoverage);
1421 mark_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index]);
1422 if (mark_index != -1)
1425 offset = GET_BE_WORD(mlpf1->LigatureCoverage);
1426 ligature_index = GSUB_is_glyph_covered((const BYTE*)mlpf1+offset, glyphs[glyph_index - write_dir]);
1427 if (ligature_index != -1)
1429 const GPOS_MarkArray *ma;
1430 const GPOS_MarkRecord *mr;
1432 const GPOS_LigatureArray *la;
1433 const GPOS_LigatureAttach *lt;
1435 int class_count = GET_BE_WORD(mlpf1->ClassCount);
1436 int component_count;
1442 TRACE("Mark %x(%i) and ligature %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], ligature_index);
1443 offset = GET_BE_WORD(mlpf1->MarkArray);
1444 ma = (const GPOS_MarkArray*)((const BYTE*)mlpf1 + offset);
1445 if (mark_index > GET_BE_WORD(ma->MarkCount))
1447 ERR("Mark index exeeded mark count\n");
1450 mr = &ma->MarkRecord[mark_index];
1451 mark_class = GET_BE_WORD(mr->Class);
1452 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1453 offset = GET_BE_WORD(mlpf1->LigatureArray);
1454 la = (const GPOS_LigatureArray*)((const BYTE*)mlpf1 + offset);
1455 if (ligature_index > GET_BE_WORD(la->LigatureCount))
1457 ERR("Ligature index exeeded ligature count\n");
1460 offset = GET_BE_WORD(la->LigatureAttach[ligature_index]);
1461 lt = (const GPOS_LigatureAttach*)((const BYTE*)la + offset);
1463 component_count = GET_BE_WORD(lt->ComponentCount);
1464 component_size = class_count * sizeof(WORD);
1466 for (i = 0; i < component_count && !offset; i++)
1469 const GPOS_ComponentRecord *cr = (const GPOS_ComponentRecord*)((const BYTE*)lt->ComponentRecord + (component_size * i));
1470 for (k = 0; k < class_count && !offset; k++)
1471 offset = GET_BE_WORD(cr->LigatureAnchor[k]);
1472 cr = (const GPOS_ComponentRecord*)((const BYTE*)cr + component_size);
1476 ERR("Failed to find avalible ligature connection point\n");
1480 GPOS_get_anchor_values((const BYTE*)lt + offset, &ligature_pt, ppem);
1481 offset = GET_BE_WORD(mr->MarkAnchor);
1482 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1483 TRACE("Offset on ligature is %i,%i design units\n",ligature_pt.x,ligature_pt.y);
1484 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1485 pt->x += ligature_pt.x - mark_pt.x;
1486 pt->y += ligature_pt.y - mark_pt.y;
1487 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1492 FIXME("Unhandled Mark To Ligature Format %i\n",GET_BE_WORD(mlpf1->PosFormat));
1496 static VOID GPOS_apply_MarkToMark(const OT_LookupTable *look, const SCRIPT_ANALYSIS *analysis, const WORD *glyphs, INT glyph_index,
1497 INT glyph_count, INT ppem, LPPOINT pt)
1500 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1502 TRACE("MarkToMark Attachment Positioning Subtable\n");
1504 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1507 const GPOS_MarkMarkPosFormat1 *mmpf1;
1508 offset = GET_BE_WORD(look->SubTable[j]);
1509 mmpf1 = (const GPOS_MarkMarkPosFormat1*)((const BYTE*)look+offset);
1510 if (GET_BE_WORD(mmpf1->PosFormat) == 1)
1512 int offset = GET_BE_WORD(mmpf1->Mark1Coverage);
1514 mark_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index]);
1515 if (mark_index != -1)
1518 offset = GET_BE_WORD(mmpf1->Mark2Coverage);
1519 mark2_index = GSUB_is_glyph_covered((const BYTE*)mmpf1+offset, glyphs[glyph_index - write_dir]);
1520 if (mark2_index != -1)
1522 const GPOS_MarkArray *ma;
1523 const GPOS_MarkRecord *mr;
1524 const GPOS_Mark2Array *m2a;
1525 const GPOS_Mark2Record *m2r;
1527 int class_count = GET_BE_WORD(mmpf1->ClassCount);
1528 int mark2record_size;
1531 TRACE("Mark %x(%i) and Mark2 %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], mark2_index);
1532 offset = GET_BE_WORD(mmpf1->Mark1Array);
1533 ma = (const GPOS_MarkArray*)((const BYTE*)mmpf1 + offset);
1534 if (mark_index > GET_BE_WORD(ma->MarkCount))
1536 ERR("Mark index exeeded mark count\n");
1539 mr = &ma->MarkRecord[mark_index];
1540 mark_class = GET_BE_WORD(mr->Class);
1541 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1542 offset = GET_BE_WORD(mmpf1->Mark2Array);
1543 m2a = (const GPOS_Mark2Array*)((const BYTE*)mmpf1 + offset);
1544 mark2record_size = class_count * sizeof(WORD);
1545 m2r = (const GPOS_Mark2Record*)((const BYTE*)m2a + sizeof(WORD) + (mark2record_size * mark2_index));
1546 offset = GET_BE_WORD(m2r->Mark2Anchor[mark_class]);
1547 GPOS_get_anchor_values((const BYTE*)m2a + offset, &mark2_pt, ppem);
1548 offset = GET_BE_WORD(mr->MarkAnchor);
1549 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1550 TRACE("Offset on mark2 is %i,%i design units\n",mark2_pt.x,mark2_pt.y);
1551 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1552 pt->x += mark2_pt.x - mark_pt.x;
1553 pt->y += mark2_pt.y - mark_pt.y;
1554 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1559 FIXME("Unhandled Mark To Mark Format %i\n",GET_BE_WORD(mmpf1->PosFormat));
1563 static INT GPOS_apply_ChainContextPos(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1564 const OT_LookupList *lookup, const OT_LookupTable *look, const WORD *glyphs, INT glyph_index,
1565 INT glyph_count, INT ppem, GOFFSET *pGoffset)
1568 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1570 TRACE("Chaining Contextual Positioning Subtable\n");
1572 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1575 const GPOS_ChainContextPosFormat3_1 *ccpf3;
1576 int dirLookahead = write_dir;
1577 int dirBacktrack = -1 * write_dir;
1579 offset = GET_BE_WORD(look->SubTable[j]);
1580 ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1582 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1584 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1587 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1589 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1592 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1596 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1597 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1598 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1600 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1602 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1604 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1605 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1608 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1610 TRACE("Matched Backtrack\n");
1612 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)((BYTE *)ccpf3 +
1613 FIELD_OFFSET(GPOS_ChainContextPosFormat3_1, Coverage[GET_BE_WORD(ccpf3->BacktrackGlyphCount)]));
1615 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1616 for (k = 0; k < indexGlyphs; k++)
1618 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1619 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1622 if (k != indexGlyphs)
1624 TRACE("Matched IndexGlyphs\n");
1626 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)((BYTE *)ccpf3_2 +
1627 FIELD_OFFSET(GPOS_ChainContextPosFormat3_2, Coverage[GET_BE_WORD(ccpf3_2->InputGlyphCount)]));
1629 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1631 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1632 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1635 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1637 TRACE("Matched LookAhead\n");
1639 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)((BYTE *)ccpf3_3 +
1640 FIELD_OFFSET(GPOS_ChainContextPosFormat3_3, Coverage[GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)]));
1642 if (GET_BE_WORD(ccpf3_4->PosCount))
1644 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1646 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1647 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1649 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1650 GPOS_apply_lookup(lpotm, lplogfont, analysis, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, glyph_count, pGoffset);
1652 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1654 else return glyph_index + 1;
1657 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1659 return glyph_index + 1;
1662 static INT GPOS_apply_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance,
1663 const OT_LookupList* lookup, INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1666 const OT_LookupTable *look;
1667 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1669 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1670 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1671 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1672 switch(GET_BE_WORD(look->LookupType))
1677 POINT adjust = {0,0};
1678 POINT advance = {0,0};
1679 GPOS_apply_SingleAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, &adjust, &advance);
1680 if (adjust.x || adjust.y)
1682 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1683 pGoffset[glyph_index].du += round(devX);
1684 pGoffset[glyph_index].dv += round(devY);
1686 if (advance.x || advance.y)
1688 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1689 piAdvance[glyph_index] += round(devX);
1691 FIXME("Unhandled adjustment to Y advancement\n");
1697 POINT advance[2]= {{0,0},{0,0}};
1698 POINT adjust[2]= {{0,0},{0,0}};
1701 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1702 int offset_sign = (analysis->fRTL && analysis->fLogicalOrder) ? -1 : 1;
1704 index = GPOS_apply_PairAdjustment(look, analysis, glyphs, glyph_index, glyph_count, ppem, adjust, advance);
1705 if (adjust[0].x || adjust[0].y)
1707 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1708 pGoffset[glyph_index].du += round(devX) * offset_sign;
1709 pGoffset[glyph_index].dv += round(devY);
1711 if (advance[0].x || advance[0].y)
1713 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1714 piAdvance[glyph_index] += round(devX);
1716 if (adjust[1].x || adjust[1].y)
1718 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1719 pGoffset[glyph_index + write_dir].du += round(devX) * offset_sign;
1720 pGoffset[glyph_index + write_dir].dv += round(devY);
1722 if (advance[1].x || advance[1].y)
1724 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1725 piAdvance[glyph_index + write_dir] += round(devX);
1733 GPOS_apply_MarkToBase(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1734 if (desU.x || desU.y)
1736 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1737 if (!analysis->fRTL) pGoffset[glyph_index].du = round(devX) - piAdvance[glyph_index-1];
1740 if (analysis->fLogicalOrder) devX *= -1;
1741 pGoffset[glyph_index].du = round(devX);
1743 pGoffset[glyph_index].dv = round(devY);
1751 GPOS_apply_MarkToLigature(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1752 if (desU.x || desU.y)
1754 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1755 pGoffset[glyph_index].du = (round(devX) - piAdvance[glyph_index-1]);
1756 pGoffset[glyph_index].dv = round(devY);
1764 int write_dir = (analysis->fRTL && !analysis->fLogicalOrder) ? -1 : 1;
1765 GPOS_apply_MarkToMark(look, analysis, glyphs, glyph_index, glyph_count, ppem, &desU);
1766 if (desU.x || desU.y)
1768 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1769 if (analysis->fRTL && analysis->fLogicalOrder) devX *= -1;
1770 pGoffset[glyph_index].du = round(devX) + pGoffset[glyph_index - write_dir].du;
1771 pGoffset[glyph_index].dv = round(devY) + pGoffset[glyph_index - write_dir].dv;
1777 return GPOS_apply_ChainContextPos(lpotm, lplogfont, analysis, piAdvance, lookup, look, glyphs, glyph_index, glyph_count, ppem, pGoffset);
1780 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1782 return glyph_index+1;
1785 INT OpenType_apply_GPOS_lookup(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, const SCRIPT_ANALYSIS *analysis, INT* piAdvance, LPCVOID table,
1786 INT lookup_index, const WORD *glyphs, INT glyph_index, INT glyph_count, GOFFSET *pGoffset)
1788 const GPOS_Header *header = (const GPOS_Header *)table;
1789 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1791 return GPOS_apply_lookup(lpotm, lplogfont, analysis, piAdvance, lookup, lookup_index, glyphs, glyph_index, glyph_count, pGoffset);
1794 static void GSUB_initialize_script_cache(ScriptCache *psc)
1798 if (psc->GSUB_Table)
1800 const OT_ScriptList *script;
1801 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1802 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1803 psc->script_count = GET_BE_WORD(script->ScriptCount);
1804 TRACE("initializing %i scripts in this font\n",psc->script_count);
1805 if (psc->script_count)
1807 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1808 for (i = 0; i < psc->script_count; i++)
1810 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1811 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]);
1812 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1818 static void GPOS_expand_script_cache(ScriptCache *psc)
1821 const OT_ScriptList *script;
1822 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1827 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1828 count = GET_BE_WORD(script->ScriptCount);
1830 if (!psc->script_count)
1832 psc->script_count = count;
1833 TRACE("initializing %i scripts in this font\n",psc->script_count);
1834 if (psc->script_count)
1836 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1837 for (i = 0; i < psc->script_count; i++)
1839 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1840 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]);
1841 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1847 for (i = 0; i < count; i++)
1850 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1851 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]);
1852 for (j = 0; j < psc->script_count; j++)
1854 if (psc->scripts[j].tag == tag)
1856 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1860 if (j == psc->script_count)
1862 psc->script_count++;
1863 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1864 psc->scripts[j].tag = tag;
1865 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1871 static void _initialize_script_cache(ScriptCache *psc)
1873 if (!psc->script_count)
1875 GSUB_initialize_script_cache(psc);
1876 GPOS_expand_script_cache(psc);
1880 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1885 _initialize_script_cache(psc);
1887 *pcTags = psc->script_count;
1889 if (!searchingFor && cMaxTags < *pcTags)
1891 else if (searchingFor)
1892 rc = USP_E_SCRIPT_NOT_IN_FONT;
1894 for (i = 0; i < psc->script_count; i++)
1897 pScriptTags[i] = psc->scripts[i].tag;
1901 if (searchingFor == psc->scripts[i].tag)
1903 pScriptTags[0] = psc->scripts[i].tag;
1913 static void GSUB_initialize_language_cache(LoadedScript *script)
1917 if (script->gsub_table)
1920 const OT_Script* table = script->gsub_table;
1921 script->language_count = GET_BE_WORD(table->LangSysCount);
1922 offset = GET_BE_WORD(table->DefaultLangSys);
1925 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1926 script->default_language.gsub_table = (const BYTE*)table + offset;
1929 if (script->language_count)
1931 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1933 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1935 for (i = 0; i < script->language_count; i++)
1937 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1938 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]);
1939 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1945 static void GPOS_expand_language_cache(LoadedScript *script)
1948 const OT_Script* table = script->gpos_table;
1954 offset = GET_BE_WORD(table->DefaultLangSys);
1956 script->default_language.gpos_table = (const BYTE*)table + offset;
1958 count = GET_BE_WORD(table->LangSysCount);
1960 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1961 if (!script->language_count)
1964 script->language_count = count;
1966 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1968 for (i = 0; i < script->language_count; i++)
1970 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1971 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]);
1972 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1978 for (i = 0; i < count; i++)
1980 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1981 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]);
1983 for (j = 0; j < script->language_count; j++)
1985 if (script->languages[j].tag == tag)
1987 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1991 if (j == script->language_count)
1993 script->language_count++;
1994 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1995 script->languages[j].tag = tag;
1996 script->languages[j].gpos_table = ((const BYTE*)table + offset);
2002 static void _initialize_language_cache(LoadedScript *script)
2004 if (!script->language_count)
2006 GSUB_initialize_language_cache(script);
2007 GPOS_expand_language_cache(script);
2011 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
2015 LoadedScript *script = NULL;
2017 _initialize_script_cache(psc);
2019 for (i = 0; i < psc->script_count; i++)
2021 if (psc->scripts[i].tag == script_tag)
2023 script = &psc->scripts[i];
2029 return E_INVALIDARG;
2031 _initialize_language_cache(script);
2033 if (!searchingFor && cMaxTags < script->language_count)
2035 else if (searchingFor)
2038 *pcTags = script->language_count;
2040 for (i = 0; i < script->language_count; i++)
2043 pLanguageTags[i] = script->languages[i].tag;
2047 if (searchingFor == script->languages[i].tag)
2049 pLanguageTags[0] = script->languages[i].tag;
2057 if (script->default_language.gsub_table)
2060 pLanguageTags[i] = script->default_language.tag;
2062 if (searchingFor && FAILED(rc))
2064 pLanguageTags[0] = script->default_language.tag;
2067 *pcTags = (*pcTags) + 1;
2074 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
2078 if (language->gsub_table)
2080 const OT_LangSys *lang = language->gsub_table;
2081 const GSUB_Header *header = (const GSUB_Header *)table;
2082 const OT_FeatureList *feature_list;
2084 language->feature_count = GET_BE_WORD(lang->FeatureCount);
2085 TRACE("%i features\n",language->feature_count);
2087 if (language->feature_count)
2089 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2091 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2093 for (i = 0; i < language->feature_count; i++)
2095 const OT_Feature *feature;
2097 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2099 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]);
2100 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2101 feature = (const OT_Feature*)language->features[i].feature;
2102 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2103 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2104 for (j = 0; j < language->features[i].lookup_count; j++)
2105 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2106 language->features[i].tableType = FEATURE_GSUB_TABLE;
2112 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
2115 const OT_LangSys *lang = language->gpos_table;
2116 const GPOS_Header *header = (const GPOS_Header *)table;
2117 const OT_FeatureList *feature_list;
2122 count = GET_BE_WORD(lang->FeatureCount);
2123 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
2125 TRACE("%i features\n",count);
2126 if (!language->feature_count)
2128 language->feature_count = count;
2130 if (language->feature_count)
2132 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
2134 for (i = 0; i < language->feature_count; i++)
2136 const OT_Feature *feature;
2138 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2140 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]);
2141 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2142 feature = (const OT_Feature*)language->features[i].feature;
2143 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
2144 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
2145 for (j = 0; j < language->features[i].lookup_count; j++)
2146 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2147 language->features[i].tableType = FEATURE_GPOS_TABLE;
2153 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
2155 for (i = 0; i < count; i++)
2157 const OT_Feature *feature;
2159 int index = GET_BE_WORD(lang->FeatureIndex[i]);
2160 int idx = language->feature_count + i;
2162 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]);
2163 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
2164 feature = (const OT_Feature*)language->features[idx].feature;
2165 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
2166 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
2167 for (j = 0; j < language->features[idx].lookup_count; j++)
2168 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
2169 language->features[idx].tableType = FEATURE_GPOS_TABLE;
2171 language->feature_count += count;
2175 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
2177 if (!language->feature_count)
2179 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
2180 GPOS_expand_feature_cache(psc->GPOS_Table, language);
2184 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)
2188 LoadedScript *script = NULL;
2189 LoadedLanguage *language = NULL;
2191 _initialize_script_cache(psc);
2193 for (i = 0; i < psc->script_count; i++)
2195 if (psc->scripts[i].tag == script_tag)
2197 script = &psc->scripts[i];
2208 return E_INVALIDARG;
2211 _initialize_language_cache(script);
2213 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
2214 language = &script->default_language;
2217 for (i = 0; i < script->language_count; i++)
2219 if (script->languages[i].tag == language_tag)
2221 language = &script->languages[i];
2233 _initialize_feature_cache(psc, language);
2238 for (i = 0; i < language->feature_count; i++)
2239 if (language->features[i].tableType == tableType)
2240 *pcTags = (*pcTags)+1;
2243 *pcTags = language->feature_count;
2245 if (!searchingFor && cMaxTags < *pcTags)
2247 else if (searchingFor)
2250 for (i = 0; i < language->feature_count; i++)
2254 if (!tableType || language->features[i].tableType == tableType)
2255 pFeatureTags[i] = language->features[i].tag;
2260 if ((searchingFor == language->features[i].tag) &&
2261 (!tableType || language->features[i].tableType == tableType))
2263 pFeatureTags[0] = language->features[i].tag;
2266 *feature = &language->features[i];