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 /* These are all structures needed for the cmap format 12 table */
47 #define CMAP_TAG MS_MAKE_TAG('c', 'm', 'a', 'p')
53 } CMAP_EncodingRecord;
58 CMAP_EncodingRecord tables[1];
65 } CMAP_SegmentedCoverage_group;
73 CMAP_SegmentedCoverage_group groups[1];
74 } CMAP_SegmentedCoverage;
76 /* These are all structures needed for the GDEF table */
77 #define GDEF_TAG MS_MAKE_TAG('G', 'D', 'E', 'F')
79 enum {BaseGlyph=1, LigatureGlyph, MarkGlyph, ComponentGlyph};
86 WORD MarkAttachClassDef;
93 WORD ClassValueArray[1];
94 } GDEF_ClassDefFormat1;
100 } GDEF_ClassRangeRecord;
104 WORD ClassRangeCount;
105 GDEF_ClassRangeRecord ClassRangeRecord[1];
106 } GDEF_ClassDefFormat2;
108 /* These are all structures needed for the GSUB table */
124 OT_ScriptRecord ScriptRecord[1];
135 OT_LangSysRecord LangSysRecord[1];
139 WORD LookupOrder; /* Reserved */
140 WORD ReqFeatureIndex;
142 WORD FeatureIndex[1];
152 OT_FeatureRecord FeatureRecord[1];
156 WORD FeatureParams; /* Reserved */
158 WORD LookupListIndex[1];
177 } OT_CoverageFormat1;
182 WORD StartCoverageIndex;
188 OT_RangeRecord RangeRecord[1];
189 } OT_CoverageFormat2;
192 WORD SubstFormat; /* = 1 */
195 } GSUB_SingleSubstFormat1;
198 WORD SubstFormat; /* = 2 */
202 }GSUB_SingleSubstFormat2;
205 WORD SubstFormat; /* = 1 */
209 }GSUB_MultipleSubstFormat1;
217 WORD SubstFormat; /* = 1 */
221 }GSUB_LigatureSubstFormat1;
236 WORD LookupListIndex;
238 }GSUB_SubstLookupRecord;
241 WORD SubstFormat; /* = 1 */
243 WORD ChainSubRuleSetCount;
244 WORD ChainSubRuleSet[1];
245 }GSUB_ChainContextSubstFormat1;
248 WORD SubstFormat; /* = 3 */
249 WORD BacktrackGlyphCount;
251 }GSUB_ChainContextSubstFormat3_1;
254 WORD InputGlyphCount;
256 }GSUB_ChainContextSubstFormat3_2;
259 WORD LookaheadGlyphCount;
261 }GSUB_ChainContextSubstFormat3_3;
265 GSUB_SubstLookupRecord SubstLookupRecord[1];
266 }GSUB_ChainContextSubstFormat3_4;
269 WORD SubstFormat; /* = 1 */
271 WORD AlternateSetCount;
272 WORD AlternateSet[1];
273 } GSUB_AlternateSubstFormat1;
280 /* These are all structures needed for the GPOS table */
300 } GPOS_AnchorFormat1;
307 } GPOS_AnchorFormat2;
315 } GPOS_AnchorFormat3;
333 } GPOS_SinglePosFormat1;
341 } GPOS_SinglePosFormat2;
349 WORD PairSetOffset[1];
350 } GPOS_PairPosFormat1;
356 } GPOS_PairValueRecord;
360 GPOS_PairValueRecord PairValueRecord[1];
370 } GPOS_MarkBasePosFormat1;
378 GPOS_BaseRecord BaseRecord[1];
388 GPOS_MarkRecord MarkRecord[1];
393 WORD LookupListIndex;
394 } GPOS_PosLookupRecord;
398 WORD BacktrackGlyphCount;
400 } GPOS_ChainContextPosFormat3_1;
403 WORD InputGlyphCount;
405 } GPOS_ChainContextPosFormat3_2;
408 WORD LookaheadGlyphCount;
410 } GPOS_ChainContextPosFormat3_3;
414 GPOS_PosLookupRecord PosLookupRecord[1];
415 } GPOS_ChainContextPosFormat3_4;
421 static VOID *load_CMAP_format12_table(HDC hdc, ScriptCache *psc)
423 CMAP_Header *CMAP_Table = NULL;
427 if (!psc->CMAP_Table)
429 length = GetFontData(hdc, CMAP_TAG , 0, NULL, 0);
430 if (length != GDI_ERROR)
432 psc->CMAP_Table = HeapAlloc(GetProcessHeap(),0,length);
433 GetFontData(hdc, CMAP_TAG , 0, psc->CMAP_Table, length);
434 TRACE("Loaded cmap table of %i bytes\n",length);
440 CMAP_Table = psc->CMAP_Table;
442 for (i = 0; i < GET_BE_WORD(CMAP_Table->numTables); i++)
444 if ( (GET_BE_WORD(CMAP_Table->tables[i].platformID) == 3) &&
445 (GET_BE_WORD(CMAP_Table->tables[i].encodingID) == 10) )
447 CMAP_SegmentedCoverage *format = (CMAP_SegmentedCoverage*)(((BYTE*)CMAP_Table) + GET_BE_DWORD(CMAP_Table->tables[i].offset));
448 if (GET_BE_WORD(format->format) == 12)
455 static int compare_group(const void *a, const void* b)
457 const DWORD *chr = a;
458 const CMAP_SegmentedCoverage_group *group = b;
460 if (*chr < GET_BE_DWORD(group->startCharCode))
462 if (*chr > GET_BE_DWORD(group->endCharCode))
467 DWORD OpenType_CMAP_GetGlyphIndex(HDC hdc, ScriptCache *psc, DWORD utf32c, LPWORD pgi, DWORD flags)
469 /* BMP: use gdi32 for ease */
470 if (utf32c < 0x10000)
473 return GetGlyphIndicesW(hdc,&ch, 1, pgi, flags);
476 if (!psc->CMAP_format12_Table)
477 psc->CMAP_format12_Table = load_CMAP_format12_table(hdc, psc);
479 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
484 if (psc->CMAP_format12_Table)
486 CMAP_SegmentedCoverage *format = NULL;
487 CMAP_SegmentedCoverage_group *group = NULL;
489 format = (CMAP_SegmentedCoverage *)psc->CMAP_format12_Table;
491 group = bsearch(&utf32c, format->groups, GET_BE_DWORD(format->nGroups),
492 sizeof(CMAP_SegmentedCoverage_group), compare_group);
496 DWORD offset = utf32c - GET_BE_DWORD(group->startCharCode);
497 *pgi = GET_BE_DWORD(group->startGlyphID) + offset;
508 static WORD GDEF_get_glyph_class(const GDEF_Header *header, WORD glyph)
512 const GDEF_ClassDefFormat1 *cf1;
517 offset = GET_BE_WORD(header->GlyphClassDef);
521 cf1 = (GDEF_ClassDefFormat1*)(((BYTE*)header)+offset);
522 if (GET_BE_WORD(cf1->ClassFormat) == 1)
524 if (glyph >= GET_BE_WORD(cf1->StartGlyph))
526 int index = glyph - GET_BE_WORD(cf1->StartGlyph);
527 if (index < GET_BE_WORD(cf1->GlyphCount))
528 class = GET_BE_WORD(cf1->ClassValueArray[index]);
531 else if (GET_BE_WORD(cf1->ClassFormat) == 2)
533 const GDEF_ClassDefFormat2 *cf2 = (GDEF_ClassDefFormat2*)cf1;
535 top = GET_BE_WORD(cf2->ClassRangeCount);
536 for (i = 0; i < top; i++)
538 if (glyph >= GET_BE_WORD(cf2->ClassRangeRecord[i].Start) &&
539 glyph <= GET_BE_WORD(cf2->ClassRangeRecord[i].End))
541 class = GET_BE_WORD(cf2->ClassRangeRecord[i].Class);
547 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1->ClassFormat));
552 static VOID *load_gdef_table(HDC hdc)
554 VOID* GDEF_Table = NULL;
555 int length = GetFontData(hdc, GDEF_TAG , 0, NULL, 0);
556 if (length != GDI_ERROR)
558 GDEF_Table = HeapAlloc(GetProcessHeap(),0,length);
559 GetFontData(hdc, GDEF_TAG , 0, GDEF_Table, length);
560 TRACE("Loaded GDEF table of %i bytes\n",length);
565 void OpenType_GDEF_UpdateGlyphProps(HDC hdc, ScriptCache *psc, const WORD *pwGlyphs, const WORD cGlyphs, WORD* pwLogClust, const WORD cChars, SCRIPT_GLYPHPROP *pGlyphProp)
569 if (!psc->GDEF_Table)
570 psc->GDEF_Table = load_gdef_table(hdc);
572 for (i = 0; i < cGlyphs; i++)
578 k = USP10_FindGlyphInLogClust(pwLogClust, cChars, i);
581 for (; k < cChars && pwLogClust[k] == i; k++)
585 class = GDEF_get_glyph_class(psc->GDEF_Table, pwGlyphs[i]);
591 pGlyphProp[i].sva.fClusterStart = 1;
592 pGlyphProp[i].sva.fDiacritic = 0;
593 pGlyphProp[i].sva.fZeroWidth = 0;
596 pGlyphProp[i].sva.fClusterStart = 1;
597 pGlyphProp[i].sva.fDiacritic = 0;
598 pGlyphProp[i].sva.fZeroWidth = 0;
601 pGlyphProp[i].sva.fClusterStart = 0;
602 pGlyphProp[i].sva.fDiacritic = 1;
603 pGlyphProp[i].sva.fZeroWidth = 1;
606 pGlyphProp[i].sva.fClusterStart = 0;
607 pGlyphProp[i].sva.fDiacritic = 0;
608 pGlyphProp[i].sva.fZeroWidth = 0;
611 ERR("Unknown glyph class %i\n",class);
612 pGlyphProp[i].sva.fClusterStart = 1;
613 pGlyphProp[i].sva.fDiacritic = 0;
614 pGlyphProp[i].sva.fZeroWidth = 0;
618 pGlyphProp[i].sva.fClusterStart = 0;
625 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
627 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
629 const OT_CoverageFormat1* cf1;
633 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
635 int count = GET_BE_WORD(cf1->GlyphCount);
637 TRACE("Coverage Format 1, %i glyphs\n",count);
638 for (i = 0; i < count; i++)
639 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
643 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
645 const OT_CoverageFormat2* cf2;
648 cf2 = (const OT_CoverageFormat2*)cf1;
650 count = GET_BE_WORD(cf2->RangeCount);
651 TRACE("Coverage Format 2, %i ranges\n",count);
652 for (i = 0; i < count; i++)
654 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
656 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
657 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
659 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
660 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
666 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
671 static INT GSUB_apply_SingleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
674 TRACE("Single Substitution Subtable\n");
676 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
679 const GSUB_SingleSubstFormat1 *ssf1;
680 offset = GET_BE_WORD(look->SubTable[j]);
681 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
682 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
684 int offset = GET_BE_WORD(ssf1->Coverage);
685 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
686 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
688 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
689 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
690 TRACE(" 0x%x\n",glyphs[glyph_index]);
691 return glyph_index + write_dir;
696 const GSUB_SingleSubstFormat2 *ssf2;
700 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
701 offset = GET_BE_WORD(ssf1->Coverage);
702 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
703 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
704 TRACE(" Coverage index %i\n",index);
707 if (glyphs[glyph_index] == GET_BE_WORD(ssf2->Substitute[index]))
708 return GSUB_E_NOGLYPH;
710 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
711 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
712 TRACE("0x%x\n",glyphs[glyph_index]);
713 return glyph_index + write_dir;
717 return GSUB_E_NOGLYPH;
720 static INT GSUB_apply_MultipleSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
723 TRACE("Multiple Substitution Subtable\n");
725 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
728 const GSUB_MultipleSubstFormat1 *msf1;
729 offset = GET_BE_WORD(look->SubTable[j]);
730 msf1 = (const GSUB_MultipleSubstFormat1*)((const BYTE*)look+offset);
732 offset = GET_BE_WORD(msf1->Coverage);
733 index = GSUB_is_glyph_covered((const BYTE*)msf1+offset, glyphs[glyph_index]);
736 const GSUB_Sequence *seq;
739 offset = GET_BE_WORD(msf1->Sequence[index]);
740 seq = (const GSUB_Sequence*)((const BYTE*)msf1+offset);
741 sub_count = GET_BE_WORD(seq->GlyphCount);
742 TRACE(" Glyph 0x%x (+%i)->",glyphs[glyph_index],(sub_count-1));
744 for (j = (*glyph_count)+(sub_count-1); j > glyph_index; j--)
745 glyphs[j] =glyphs[j-(sub_count-1)];
747 for (j = 0; j < sub_count; j++)
749 glyphs[glyph_index + (sub_count-1) - j] = GET_BE_WORD(seq->Substitute[j]);
751 glyphs[glyph_index + j] = GET_BE_WORD(seq->Substitute[j]);
753 *glyph_count = *glyph_count + (sub_count - 1);
755 if (TRACE_ON(uniscribe))
757 for (j = 0; j < sub_count; j++)
758 TRACE(" 0x%x",glyphs[glyph_index+j]);
762 return glyph_index + (sub_count * write_dir);
765 return GSUB_E_NOGLYPH;
768 static INT GSUB_apply_AlternateSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
771 TRACE("Alternate Substitution Subtable\n");
773 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
776 const GSUB_AlternateSubstFormat1 *asf1;
779 offset = GET_BE_WORD(look->SubTable[j]);
780 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
781 offset = GET_BE_WORD(asf1->Coverage);
783 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
786 const GSUB_AlternateSet *as;
787 offset = GET_BE_WORD(asf1->AlternateSet[index]);
788 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
789 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
790 if (glyphs[glyph_index] == GET_BE_WORD(as->Alternate[0]))
791 return GSUB_E_NOGLYPH;
793 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
794 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
795 TRACE(" 0x%x\n",glyphs[glyph_index]);
796 return glyph_index + write_dir;
799 return GSUB_E_NOGLYPH;
802 static INT GSUB_apply_LigatureSubst(const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
806 TRACE("Ligature Substitution Subtable\n");
807 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
809 const GSUB_LigatureSubstFormat1 *lsf1;
812 offset = GET_BE_WORD(look->SubTable[j]);
813 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
814 offset = GET_BE_WORD(lsf1->Coverage);
815 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
816 TRACE(" Coverage index %i\n",index);
819 const GSUB_LigatureSet *ls;
822 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
823 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
824 count = GET_BE_WORD(ls->LigatureCount);
825 TRACE(" LigatureSet has %i members\n",count);
826 for (k = 0; k < count; k++)
828 const GSUB_Ligature *lig;
829 int CompCount,l,CompIndex;
831 offset = GET_BE_WORD(ls->Ligature[k]);
832 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
833 CompCount = GET_BE_WORD(lig->CompCount) - 1;
834 CompIndex = glyph_index+write_dir;
835 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
838 CompGlyph = GET_BE_WORD(lig->Component[l]);
839 if (CompGlyph != glyphs[CompIndex])
841 CompIndex += write_dir;
845 int replaceIdx = glyph_index;
847 replaceIdx = glyph_index - CompCount;
849 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
850 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
851 TRACE("0x%x\n",glyphs[replaceIdx]);
855 for (j = replaceIdx + 1; j < *glyph_count; j++)
856 glyphs[j] =glyphs[j+CompCount];
857 *glyph_count = *glyph_count - CompCount;
859 return replaceIdx + write_dir;
864 return GSUB_E_NOGLYPH;
867 static INT GSUB_apply_ChainContextSubst(const OT_LookupList* lookup, const OT_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
872 TRACE("Chaining Contextual Substitution Subtable\n");
873 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
875 const GSUB_ChainContextSubstFormat1 *ccsf1;
877 int dirLookahead = write_dir;
878 int dirBacktrack = -1 * write_dir;
880 offset = GET_BE_WORD(look->SubTable[j]);
881 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
882 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
884 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
887 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
889 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
892 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
896 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
897 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
898 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
899 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
900 int newIndex = glyph_index;
902 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
904 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
906 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
908 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
909 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
912 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
914 TRACE("Matched Backtrack\n");
916 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
918 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
919 for (k = 0; k < indexGlyphs; k++)
921 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
922 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
925 if (k != indexGlyphs)
927 TRACE("Matched IndexGlyphs\n");
929 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
931 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
933 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
934 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
937 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
939 TRACE("Matched LookAhead\n");
941 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
943 if (GET_BE_WORD(ccsf3_4->SubstCount))
945 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
947 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
948 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
950 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
951 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
954 ERR("Chain failed to generate a glyph\n");
960 else return GSUB_E_NOGLYPH;
966 static INT GSUB_apply_lookup(const OT_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
969 const OT_LookupTable *look;
971 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
972 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
973 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
974 switch(GET_BE_WORD(look->LookupType))
977 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
979 return GSUB_apply_MultipleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
981 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
983 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
985 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
987 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
989 return GSUB_E_NOGLYPH;
992 INT OpenType_apply_GSUB_lookup(LPCVOID table, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
994 const GSUB_Header *header = (const GSUB_Header *)table;
995 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
997 return GSUB_apply_lookup(lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count);
1003 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);
1005 static INT GPOS_get_device_table_value(const OT_DeviceTable *DeviceTable, WORD ppem)
1007 static const WORD mask[3] = {3,0xf,0xff};
1008 if (DeviceTable && ppem >= GET_BE_WORD(DeviceTable->StartSize) && ppem <= GET_BE_WORD(DeviceTable->EndSize))
1010 int format = GET_BE_WORD(DeviceTable->DeltaFormat);
1011 int index = ppem - GET_BE_WORD(DeviceTable->StartSize);
1013 TRACE("device table, format %i, index %i\n",format, index);
1014 index = index << format;
1015 value = (DeviceTable->DeltaValue[index/sizeof(WORD)] << (index%sizeof(WORD)))&mask[format-1];
1016 TRACE("offset %i, value %i\n",index, value);
1017 if (value > mask[format-1]/2)
1018 value = -1 * ((mask[format-1]+1) - value);
1024 static VOID GPOS_get_anchor_values(LPCVOID table, LPPOINT pt, WORD ppem)
1026 const GPOS_AnchorFormat1* anchor1 = (const GPOS_AnchorFormat1*)table;
1028 switch (GET_BE_WORD(anchor1->AnchorFormat))
1032 TRACE("Anchor Format 1\n");
1033 pt->x = (short)GET_BE_WORD(anchor1->XCoordinate);
1034 pt->y = (short)GET_BE_WORD(anchor1->YCoordinate);
1039 const GPOS_AnchorFormat2* anchor2 = (const GPOS_AnchorFormat2*)table;
1040 TRACE("Anchor Format 2\n");
1041 pt->x = (short)GET_BE_WORD(anchor2->XCoordinate);
1042 pt->y = (short)GET_BE_WORD(anchor2->YCoordinate);
1048 const GPOS_AnchorFormat3* anchor3 = (const GPOS_AnchorFormat3*)table;
1049 TRACE("Anchor Format 3\n");
1050 pt->x = (short)GET_BE_WORD(anchor3->XCoordinate);
1051 pt->y = (short)GET_BE_WORD(anchor3->YCoordinate);
1052 offset = GET_BE_WORD(anchor3->XDeviceTable);
1053 TRACE("ppem %i\n",ppem);
1056 const OT_DeviceTable* DeviceTableX = NULL;
1057 DeviceTableX = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1058 pt->x += GPOS_get_device_table_value(DeviceTableX, ppem);
1060 offset = GET_BE_WORD(anchor3->YDeviceTable);
1063 const OT_DeviceTable* DeviceTableY = NULL;
1064 DeviceTableY = (const OT_DeviceTable*)((const BYTE*)anchor3 + offset);
1065 pt->y += GPOS_get_device_table_value(DeviceTableY, ppem);
1070 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1->AnchorFormat));
1076 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm, LPLOGFONTW lplogfont, int desX, int desY, double *devX, double *devY)
1078 int emHeight = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1080 TRACE("emHeight %i lfWidth %i\n",emHeight, lplogfont->lfWidth);
1081 *devX = (desX * emHeight) / (double)lpotm->otmEMSquare;
1082 *devY = (desY * emHeight) / (double)lpotm->otmEMSquare;
1083 if (lplogfont->lfWidth)
1084 FIXME("Font with lfWidth set no handled properly\n");
1087 static INT GPOS_get_value_record(WORD ValueFormat, const WORD data[], GPOS_ValueRecord *record)
1090 if (ValueFormat & 0x0001) record->XPlacement = GET_BE_WORD(data[offset++]);
1091 if (ValueFormat & 0x0002) record->YPlacement = GET_BE_WORD(data[offset++]);
1092 if (ValueFormat & 0x0004) record->XAdvance = GET_BE_WORD(data[offset++]);
1093 if (ValueFormat & 0x0008) record->YAdvance = GET_BE_WORD(data[offset++]);
1094 if (ValueFormat & 0x0010) record->XPlaDevice = GET_BE_WORD(data[offset++]);
1095 if (ValueFormat & 0x0020) record->YPlaDevice = GET_BE_WORD(data[offset++]);
1096 if (ValueFormat & 0x0040) record->XAdvDevice = GET_BE_WORD(data[offset++]);
1097 if (ValueFormat & 0x0080) record->YAdvDevice = GET_BE_WORD(data[offset++]);
1101 static VOID GPOS_get_value_record_offsets(const BYTE* head, GPOS_ValueRecord *ValueRecord, WORD ValueFormat, INT ppem, LPPOINT ptPlacement, LPPOINT ptAdvance)
1103 if (ValueFormat & 0x0001) ptPlacement->x += (short)ValueRecord->XPlacement;
1104 if (ValueFormat & 0x0002) ptPlacement->y += (short)ValueRecord->YPlacement;
1105 if (ValueFormat & 0x0004) ptAdvance->x += (short)ValueRecord->XAdvance;
1106 if (ValueFormat & 0x0008) ptAdvance->y += (short)ValueRecord->YAdvance;
1107 if (ValueFormat & 0x0010) ptPlacement->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XPlaDevice), ppem);
1108 if (ValueFormat & 0x0020) ptPlacement->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YPlaDevice), ppem);
1109 if (ValueFormat & 0x0040) ptAdvance->x += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->XAdvDevice), ppem);
1110 if (ValueFormat & 0x0080) ptAdvance->y += GPOS_get_device_table_value((const OT_DeviceTable*)(head + ValueRecord->YAdvDevice), ppem);
1111 if (ValueFormat & 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat&0xFF00);
1114 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)
1118 TRACE("Single Adjustment Positioning Subtable\n");
1120 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1122 const GPOS_SinglePosFormat1 *spf1;
1123 WORD offset = GET_BE_WORD(look->SubTable[j]);
1124 spf1 = (const GPOS_SinglePosFormat1*)((const BYTE*)look+offset);
1125 if (GET_BE_WORD(spf1->PosFormat) == 1)
1127 offset = GET_BE_WORD(spf1->Coverage);
1128 if (GSUB_is_glyph_covered((const BYTE*)spf1+offset, glyphs[glyph_index]) != -1)
1130 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1131 WORD ValueFormat = GET_BE_WORD(spf1->ValueFormat);
1132 GPOS_get_value_record(ValueFormat, spf1->Value, &ValueRecord);
1133 GPOS_get_value_record_offsets((const BYTE*)spf1, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1134 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1137 else if (GET_BE_WORD(spf1->PosFormat) == 2)
1140 const GPOS_SinglePosFormat2 *spf2;
1141 spf2 = (const GPOS_SinglePosFormat2*)spf1;
1142 offset = GET_BE_WORD(spf2->Coverage);
1143 index = GSUB_is_glyph_covered((const BYTE*)spf2+offset, glyphs[glyph_index]);
1147 GPOS_ValueRecord ValueRecord = {0,0,0,0,0,0,0,0};
1148 WORD ValueFormat = GET_BE_WORD(spf2->ValueFormat);
1149 size = GPOS_get_value_record(ValueFormat, spf2->Value, &ValueRecord);
1152 offset = size * index;
1153 GPOS_get_value_record(ValueFormat, &spf2->Value[offset], &ValueRecord);
1155 GPOS_get_value_record_offsets((const BYTE*)spf2, &ValueRecord, ValueFormat, ppem, ptAdjust, ptAdvance);
1156 TRACE("Glyph Adjusted by %i,%i\n",ValueRecord.XPlacement,ValueRecord.YPlacement);
1160 FIXME("Single Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(spf1->PosFormat));
1164 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)
1168 TRACE("Pair Adjustment Positioning Subtable\n");
1170 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1172 const GPOS_PairPosFormat1 *ppf1;
1173 WORD offset = GET_BE_WORD(look->SubTable[j]);
1174 ppf1 = (const GPOS_PairPosFormat1*)((const BYTE*)look+offset);
1175 if (GET_BE_WORD(ppf1->PosFormat) == 1)
1177 offset = GET_BE_WORD(ppf1->Coverage);
1178 if (GSUB_is_glyph_covered((const BYTE*)ppf1+offset, glyphs[glyph_index]) != -1)
1181 int count = GET_BE_WORD(ppf1->PairSetCount);
1182 for (i = 0; i < count; i++)
1186 const GPOS_PairSet *ps;
1187 offset = GET_BE_WORD(ppf1->PairSetOffset[i]);
1188 ps = (const GPOS_PairSet*)((const BYTE*)ppf1+offset);
1189 pair_count = GET_BE_WORD(ps->PairValueCount);
1190 for (k = 0; k < pair_count; k++)
1192 if (glyphs[glyph_index+write_dir] == GET_BE_WORD(ps->PairValueRecord[k].SecondGlyph))
1194 GPOS_ValueRecord ValueRecord1 = {0,0,0,0,0,0,0,0};
1195 GPOS_ValueRecord ValueRecord2 = {0,0,0,0,0,0,0,0};
1196 WORD ValueFormat1 = GET_BE_WORD(ppf1->ValueFormat1);
1197 WORD ValueFormat2 = GET_BE_WORD(ppf1->ValueFormat2);
1199 TRACE("Format 1: Found Pair %x,%x\n",glyphs[glyph_index],glyphs[glyph_index+write_dir]);
1201 offset = GPOS_get_value_record(ValueFormat1, ps->PairValueRecord[k].Value1, &ValueRecord1);
1202 GPOS_get_value_record(ValueFormat2, (WORD*)((const BYTE*)(ps->PairValueRecord[k].Value2)+offset), &ValueRecord2);
1205 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord1, ValueFormat1, ppem, &ptAdjust[0], &ptAdvance[0]);
1206 TRACE("Glyph 1 resulting cumulative offset is %i,%i design units\n",ptAdjust[0].x,ptAdjust[0].y);
1210 GPOS_get_value_record_offsets((const BYTE*)ppf1, &ValueRecord2, ValueFormat2, ppem, &ptAdjust[1], &ptAdvance[1]);
1211 TRACE("Glyph 2 resulting cumulative offset is %i,%i design units\n",ptAdjust[1].x,ptAdjust[1].y);
1212 return glyph_index+2;
1220 FIXME("Pair Adjustment Positioning: Format %i Unhandled\n",GET_BE_WORD(ppf1->PosFormat));
1222 return glyph_index+1;
1225 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)
1229 TRACE("MarkToBase Attachment Positioning Subtable\n");
1231 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1234 const GPOS_MarkBasePosFormat1 *mbpf1;
1235 offset = GET_BE_WORD(look->SubTable[j]);
1236 mbpf1 = (const GPOS_MarkBasePosFormat1*)((const BYTE*)look+offset);
1237 if (GET_BE_WORD(mbpf1->PosFormat) == 1)
1239 int offset = GET_BE_WORD(mbpf1->MarkCoverage);
1241 mark_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index]);
1242 if (mark_index != -1)
1245 offset = GET_BE_WORD(mbpf1->BaseCoverage);
1246 base_index = GSUB_is_glyph_covered((const BYTE*)mbpf1+offset, glyphs[glyph_index - write_dir]);
1247 if (base_index != -1)
1249 const GPOS_MarkArray *ma;
1250 const GPOS_MarkRecord *mr;
1251 const GPOS_BaseArray *ba;
1252 const GPOS_BaseRecord *br;
1254 int class_count = GET_BE_WORD(mbpf1->ClassCount);
1255 int baserecord_size;
1258 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs[glyph_index], mark_index, glyphs[glyph_index - write_dir], base_index);
1259 offset = GET_BE_WORD(mbpf1->MarkArray);
1260 ma = (const GPOS_MarkArray*)((const BYTE*)mbpf1 + offset);
1261 if (mark_index > GET_BE_WORD(ma->MarkCount))
1263 ERR("Mark index exeeded mark count\n");
1266 mr = &ma->MarkRecord[mark_index];
1267 mark_class = GET_BE_WORD(mr->Class);
1268 TRACE("Mark Class %i total classes %i\n",mark_class,class_count);
1269 offset = GET_BE_WORD(mbpf1->BaseArray);
1270 ba = (const GPOS_BaseArray*)((const BYTE*)mbpf1 + offset);
1271 baserecord_size = class_count * sizeof(WORD);
1272 br = (const GPOS_BaseRecord*)((const BYTE*)ba + sizeof(WORD) + (baserecord_size * base_index));
1273 offset = GET_BE_WORD(br->BaseAnchor[mark_class]);
1274 GPOS_get_anchor_values((const BYTE*)ba + offset, &base_pt, ppem);
1275 offset = GET_BE_WORD(mr->MarkAnchor);
1276 GPOS_get_anchor_values((const BYTE*)ma + offset, &mark_pt, ppem);
1277 TRACE("Offset on base is %i,%i design units\n",base_pt.x,base_pt.y);
1278 TRACE("Offset on mark is %i,%i design units\n",mark_pt.x, mark_pt.y);
1279 pt->x += base_pt.x - mark_pt.x;
1280 pt->y += base_pt.y - mark_pt.y;
1281 TRACE("Resulting cumulative offset is %i,%i design units\n",pt->x,pt->y);
1286 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1->PosFormat));
1290 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)
1294 TRACE("Chaining Contextual Positioning Subtable\n");
1296 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
1299 const GPOS_ChainContextPosFormat3_1 *ccpf3;
1300 int dirLookahead = write_dir;
1301 int dirBacktrack = -1 * write_dir;
1303 offset = GET_BE_WORD(look->SubTable[j]);
1304 ccpf3 = (const GPOS_ChainContextPosFormat3_1*)((const BYTE*)look+offset);
1306 if (GET_BE_WORD(ccpf3->PosFormat) == 1)
1308 FIXME(" TODO: subtype 1 (Simple Chaining Context Glyph Positioning)\n");
1311 else if (GET_BE_WORD(ccpf3->PosFormat) == 2)
1313 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Positioning)\n");
1316 else if (GET_BE_WORD(ccpf3->PosFormat) == 3)
1320 const GPOS_ChainContextPosFormat3_2 *ccpf3_2;
1321 const GPOS_ChainContextPosFormat3_3 *ccpf3_3;
1322 const GPOS_ChainContextPosFormat3_4 *ccpf3_4;
1324 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Positioning)\n");
1326 for (k = 0; k < GET_BE_WORD(ccpf3->BacktrackGlyphCount); k++)
1328 offset = GET_BE_WORD(ccpf3->Coverage[k]);
1329 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
1332 if (k != GET_BE_WORD(ccpf3->BacktrackGlyphCount))
1334 TRACE("Matched Backtrack\n");
1336 ccpf3_2 = (const GPOS_ChainContextPosFormat3_2*)(((LPBYTE)ccpf3)+sizeof(GPOS_ChainContextPosFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccpf3->BacktrackGlyphCount)-1)));
1338 indexGlyphs = GET_BE_WORD(ccpf3_2->InputGlyphCount);
1339 for (k = 0; k < indexGlyphs; k++)
1341 offset = GET_BE_WORD(ccpf3_2->Coverage[k]);
1342 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
1345 if (k != indexGlyphs)
1347 TRACE("Matched IndexGlyphs\n");
1349 ccpf3_3 = (const GPOS_ChainContextPosFormat3_3*)(((LPBYTE)ccpf3_2)+sizeof(GPOS_ChainContextPosFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_2->InputGlyphCount)-1)));
1351 for (k = 0; k < GET_BE_WORD(ccpf3_3->LookaheadGlyphCount); k++)
1353 offset = GET_BE_WORD(ccpf3_3->Coverage[k]);
1354 if (GSUB_is_glyph_covered((const BYTE*)ccpf3+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
1357 if (k != GET_BE_WORD(ccpf3_3->LookaheadGlyphCount))
1359 TRACE("Matched LookAhead\n");
1361 ccpf3_4 = (const GPOS_ChainContextPosFormat3_4*)(((LPBYTE)ccpf3_3)+sizeof(GPOS_ChainContextPosFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccpf3_3->LookaheadGlyphCount)-1)));
1363 if (GET_BE_WORD(ccpf3_4->PosCount))
1365 for (k = 0; k < GET_BE_WORD(ccpf3_4->PosCount); k++)
1367 int lookupIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].LookupListIndex);
1368 int SequenceIndex = GET_BE_WORD(ccpf3_4->PosLookupRecord[k].SequenceIndex) * write_dir;
1370 TRACE("Position: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
1371 GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count, pGoffset);
1373 return glyph_index + indexGlyphs + GET_BE_WORD(ccpf3_3->LookaheadGlyphCount);
1375 else return glyph_index + 1;
1378 FIXME("Unhandled Chaining Contextual Positioning Format %i\n",GET_BE_WORD(ccpf3->PosFormat));
1380 return glyph_index + 1;
1383 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)
1386 const OT_LookupTable *look;
1387 int ppem = lpotm->otmTextMetrics.tmAscent + lpotm->otmTextMetrics.tmDescent - lpotm->otmTextMetrics.tmInternalLeading;
1389 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
1390 look = (const OT_LookupTable*)((const BYTE*)lookup + offset);
1391 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
1392 switch(GET_BE_WORD(look->LookupType))
1397 POINT adjust = {0,0};
1398 POINT advance = {0,0};
1399 GPOS_apply_SingleAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &adjust, &advance);
1400 if (adjust.x || adjust.y)
1402 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust.x, adjust.y, &devX, &devY);
1403 pGoffset[glyph_index].du += (int)(devX+0.5);
1404 pGoffset[glyph_index].dv += (int)(devY+0.5);
1406 if (advance.x || advance.y)
1408 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance.x, advance.y, &devX, &devY);
1409 piAdvance[glyph_index] += (int)(devX+0.5);
1411 FIXME("Unhandled adjustment to Y advancement\n");
1416 POINT advance[2]= {{0,0},{0,0}};
1417 POINT adjust[2]= {{0,0},{0,0}};
1420 index = GPOS_apply_PairAdjustment(look, glyphs, glyph_index, write_dir, glyph_count, ppem, adjust, advance);
1421 if (adjust[0].x || adjust[0].y)
1423 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[0].x, adjust[0].y, &devX, &devY);
1424 pGoffset[glyph_index].du += (int)(devX+0.5);
1425 pGoffset[glyph_index].dv += (int)(devY+0.5);
1427 if (advance[0].x || advance[0].y)
1429 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[0].x, advance[0].y, &devX, &devY);
1430 piAdvance[glyph_index] += (int)(devX+0.5);
1432 if (adjust[1].x || adjust[1].y)
1434 GPOS_convert_design_units_to_device(lpotm, lplogfont, adjust[1].x, adjust[1].y, &devX, &devY);
1435 pGoffset[glyph_index + write_dir].du += (int)(devX+0.5);
1436 pGoffset[glyph_index + write_dir].dv += (int)(devY+0.5);
1438 if (advance[1].x || advance[1].y)
1440 GPOS_convert_design_units_to_device(lpotm, lplogfont, advance[1].x, advance[1].y, &devX, &devY);
1441 piAdvance[glyph_index + write_dir] += (int)(devX+0.5);
1449 GPOS_apply_MarkToBase(look, glyphs, glyph_index, write_dir, glyph_count, ppem, &desU);
1450 if (desU.x || desU.y)
1452 GPOS_convert_design_units_to_device(lpotm, lplogfont, desU.x, desU.y, &devX, &devY);
1453 pGoffset[glyph_index].du += ((int)(devX+0.5) - piAdvance[glyph_index-1]);
1454 pGoffset[glyph_index].dv += (int)(devY+0.5);
1460 return GPOS_apply_ChainContextPos(lpotm, lplogfont, piAdvance, lookup, look, glyphs, glyph_index, write_dir, glyph_count, ppem, pGoffset);
1463 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
1465 return glyph_index+1;
1468 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)
1470 const GPOS_Header *header = (const GPOS_Header *)table;
1471 const OT_LookupList *lookup = (const OT_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
1473 return GPOS_apply_lookup(lpotm, lplogfont, piAdvance, lookup, lookup_index, glyphs, glyph_index, write_dir, glyph_count, pGoffset);
1476 static void GSUB_initialize_script_cache(ScriptCache *psc)
1480 if (psc->GSUB_Table)
1482 const OT_ScriptList *script;
1483 const GSUB_Header* header = (const GSUB_Header*)psc->GSUB_Table;
1484 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1485 psc->script_count = GET_BE_WORD(script->ScriptCount);
1486 TRACE("initializing %i scripts in this font\n",psc->script_count);
1487 if (psc->script_count)
1489 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1490 for (i = 0; i < psc->script_count; i++)
1492 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1493 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]);
1494 psc->scripts[i].gsub_table = ((const BYTE*)script + offset);
1500 static void GPOS_expand_script_cache(ScriptCache *psc)
1503 const OT_ScriptList *script;
1504 const GPOS_Header* header = (const GPOS_Header*)psc->GPOS_Table;
1509 script = (const OT_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
1510 count = GET_BE_WORD(script->ScriptCount);
1512 if (!psc->script_count)
1514 psc->script_count = count;
1515 TRACE("initializing %i scripts in this font\n",psc->script_count);
1516 if (psc->script_count)
1518 psc->scripts = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LoadedScript) * psc->script_count);
1519 for (i = 0; i < psc->script_count; i++)
1521 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1522 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]);
1523 psc->scripts[i].gpos_table = ((const BYTE*)script + offset);
1529 for (i = 0; i < count; i++)
1532 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1533 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]);
1534 for (j = 0; j < psc->script_count; j++)
1536 if (psc->scripts[j].tag == tag)
1538 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1542 if (j == psc->script_count)
1544 psc->script_count++;
1545 psc->scripts = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,psc->scripts, sizeof(LoadedScript) * psc->script_count);
1546 psc->scripts[j].tag = tag;
1547 psc->scripts[j].gpos_table = ((const BYTE*)script + offset);
1553 static void _initialize_script_cache(ScriptCache *psc)
1555 if (!psc->script_count)
1557 GSUB_initialize_script_cache(psc);
1558 GPOS_expand_script_cache(psc);
1562 HRESULT OpenType_GetFontScriptTags(ScriptCache *psc, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pScriptTags, int *pcTags)
1567 _initialize_script_cache(psc);
1569 *pcTags = psc->script_count;
1571 if (!searchingFor && cMaxTags < *pcTags)
1573 else if (searchingFor)
1574 rc = USP_E_SCRIPT_NOT_IN_FONT;
1576 for (i = 0; i < psc->script_count; i++)
1579 pScriptTags[i] = psc->scripts[i].tag;
1583 if (searchingFor == psc->scripts[i].tag)
1585 pScriptTags[0] = psc->scripts[i].tag;
1595 static void GSUB_initialize_language_cache(LoadedScript *script)
1599 if (script->gsub_table)
1602 const OT_Script* table = script->gsub_table;
1603 script->language_count = GET_BE_WORD(table->LangSysCount);
1604 offset = GET_BE_WORD(table->DefaultLangSys);
1607 script->default_language.tag = MS_MAKE_TAG('d','f','l','t');
1608 script->default_language.gsub_table = (const BYTE*)table + offset;
1611 if (script->language_count)
1613 TRACE("Deflang %p, LangCount %i\n",script->default_language.gsub_table, script->language_count);
1615 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1617 for (i = 0; i < script->language_count; i++)
1619 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1620 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]);
1621 script->languages[i].gsub_table = ((const BYTE*)table + offset);
1627 static void GPOS_expand_language_cache(LoadedScript *script)
1630 const OT_Script* table = script->gpos_table;
1636 offset = GET_BE_WORD(table->DefaultLangSys);
1638 script->default_language.gpos_table = (const BYTE*)table + offset;
1640 count = GET_BE_WORD(table->LangSysCount);
1642 TRACE("Deflang %p, LangCount %i\n",script->default_language.gpos_table, count);
1643 if (!script->language_count)
1646 script->language_count = count;
1648 script->languages = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LoadedLanguage) * script->language_count);
1650 for (i = 0; i < script->language_count; i++)
1652 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1653 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]);
1654 script->languages[i].gpos_table = ((const BYTE*)table + offset);
1660 for (i = 0; i < count; i++)
1662 int offset = GET_BE_WORD(table->LangSysRecord[i].LangSys);
1663 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]);
1665 for (j = 0; j < script->language_count; j++)
1667 if (script->languages[j].tag == tag)
1669 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1673 if (j == script->language_count)
1675 script->language_count++;
1676 script->languages = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,script->languages, sizeof(LoadedLanguage) * script->language_count);
1677 script->languages[j].tag = tag;
1678 script->languages[j].gpos_table = ((const BYTE*)table + offset);
1684 static void _initialize_language_cache(LoadedScript *script)
1686 if (!script->language_count)
1688 GSUB_initialize_language_cache(script);
1689 GPOS_expand_language_cache(script);
1693 HRESULT OpenType_GetFontLanguageTags(ScriptCache *psc, OPENTYPE_TAG script_tag, OPENTYPE_TAG searchingFor, int cMaxTags, OPENTYPE_TAG *pLanguageTags, int *pcTags)
1697 LoadedScript *script = NULL;
1699 _initialize_script_cache(psc);
1701 for (i = 0; i < psc->script_count; i++)
1703 if (psc->scripts[i].tag == script_tag)
1705 script = &psc->scripts[i];
1711 return E_INVALIDARG;
1713 _initialize_language_cache(script);
1715 if (!searchingFor && cMaxTags < script->language_count)
1717 else if (searchingFor)
1720 *pcTags = script->language_count;
1722 for (i = 0; i < script->language_count; i++)
1725 pLanguageTags[i] = script->languages[i].tag;
1729 if (searchingFor == script->languages[i].tag)
1731 pLanguageTags[0] = script->languages[i].tag;
1739 if (script->default_language.gsub_table)
1742 pLanguageTags[i] = script->default_language.tag;
1744 if (searchingFor && FAILED(rc))
1746 pLanguageTags[0] = script->default_language.tag;
1749 *pcTags = (*pcTags) + 1;
1756 static void GSUB_initialize_feature_cache(LPCVOID table, LoadedLanguage *language)
1760 if (language->gsub_table)
1762 const OT_LangSys *lang = language->gsub_table;
1763 const GSUB_Header *header = (const GSUB_Header *)table;
1764 const OT_FeatureList *feature_list;
1766 language->feature_count = GET_BE_WORD(lang->FeatureCount);
1767 TRACE("%i features\n",language->feature_count);
1769 if (language->feature_count)
1771 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1773 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1775 for (i = 0; i < language->feature_count; i++)
1777 const OT_Feature *feature;
1779 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1781 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]);
1782 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1783 feature = (const OT_Feature*)language->features[i].feature;
1784 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1785 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1786 for (j = 0; j < language->features[i].lookup_count; j++)
1787 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1793 static void GPOS_expand_feature_cache(LPCVOID table, LoadedLanguage *language)
1796 const OT_LangSys *lang = language->gpos_table;
1797 const GPOS_Header *header = (const GPOS_Header *)table;
1798 const OT_FeatureList *feature_list;
1803 count = GET_BE_WORD(lang->FeatureCount);
1804 feature_list = (const OT_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
1806 TRACE("%i features\n",count);
1807 if (!language->feature_count)
1809 language->feature_count = count;
1811 if (language->feature_count)
1813 language->features = HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature)*language->feature_count);
1815 for (i = 0; i < language->feature_count; i++)
1817 const OT_Feature *feature;
1819 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1821 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]);
1822 language->features[i].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1823 feature = (const OT_Feature*)language->features[i].feature;
1824 language->features[i].lookup_count = GET_BE_WORD(feature->LookupCount);
1825 language->features[i].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[i].lookup_count);
1826 for (j = 0; j < language->features[i].lookup_count; j++)
1827 language->features[i].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1833 language->features = HeapReAlloc(GetProcessHeap(),0,language->features, sizeof(LoadedFeature)*(language->feature_count + count));
1835 for (i = 0; i < count; i++)
1837 const OT_Feature *feature;
1839 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1840 int idx = language->feature_count + i;
1842 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]);
1843 language->features[idx].feature = ((const BYTE*)feature_list + GET_BE_WORD(feature_list->FeatureRecord[index].Feature));
1844 feature = (const OT_Feature*)language->features[idx].feature;
1845 language->features[idx].lookup_count = GET_BE_WORD(feature->LookupCount);
1846 language->features[idx].lookups = HeapAlloc(GetProcessHeap(),0,sizeof(WORD) * language->features[idx].lookup_count);
1847 for (j = 0; j < language->features[idx].lookup_count; j++)
1848 language->features[idx].lookups[j] = GET_BE_WORD(feature->LookupListIndex[j]);
1850 language->feature_count += count;
1854 static void _initialize_feature_cache(ScriptCache *psc, LoadedLanguage *language)
1856 if (!language->feature_count)
1858 GSUB_initialize_feature_cache(psc->GSUB_Table, language);
1859 GPOS_expand_feature_cache(psc->GPOS_Table, language);
1863 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)
1867 LoadedScript *script = NULL;
1868 LoadedLanguage *language = NULL;
1870 _initialize_script_cache(psc);
1872 for (i = 0; i < psc->script_count; i++)
1874 if (psc->scripts[i].tag == script_tag)
1876 script = &psc->scripts[i];
1887 return E_INVALIDARG;
1890 _initialize_language_cache(script);
1892 if ((script->default_language.gsub_table || script->default_language.gpos_table) && script->default_language.tag == language_tag)
1893 language = &script->default_language;
1896 for (i = 0; i < script->language_count; i++)
1898 if (script->languages[i].tag == language_tag)
1900 language = &script->languages[i];
1912 _initialize_feature_cache(psc, language);
1914 *pcTags = language->feature_count;
1916 if (!searchingFor && cMaxTags < *pcTags)
1918 else if (searchingFor)
1921 for (i = 0; i < language->feature_count; i++)
1924 pFeatureTags[i] = language->features[i].tag;
1928 if (searchingFor == language->features[i].tag)
1930 pFeatureTags[0] = language->features[i].tag;
1933 *feature = &language->features[i];