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
;
335 } GPOS_MarkBasePosFormat1
;
343 GPOS_BaseRecord BaseRecord
[1];
353 GPOS_MarkRecord MarkRecord
[1];
360 static VOID
*load_CMAP_format12_table(HDC hdc
, ScriptCache
*psc
)
362 CMAP_Header
*CMAP_Table
= NULL
;
366 if (!psc
->CMAP_Table
)
368 length
= GetFontData(hdc
, CMAP_TAG
, 0, NULL
, 0);
369 if (length
!= GDI_ERROR
)
371 psc
->CMAP_Table
= HeapAlloc(GetProcessHeap(),0,length
);
372 GetFontData(hdc
, CMAP_TAG
, 0, psc
->CMAP_Table
, length
);
373 TRACE("Loaded cmap table of %i bytes\n",length
);
379 CMAP_Table
= psc
->CMAP_Table
;
381 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
); i
++)
383 if ( (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) == 3) &&
384 (GET_BE_WORD(CMAP_Table
->tables
[i
].encodingID
) == 10) )
386 CMAP_SegmentedCoverage
*format
= (CMAP_SegmentedCoverage
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
387 if (GET_BE_WORD(format
->format
) == 12)
394 static int compare_group(const void *a
, const void* b
)
396 const DWORD
*chr
= a
;
397 const CMAP_SegmentedCoverage_group
*group
= b
;
399 if (*chr
< GET_BE_DWORD(group
->startCharCode
))
401 if (*chr
> GET_BE_DWORD(group
->endCharCode
))
406 DWORD
OpenType_CMAP_GetGlyphIndex(HDC hdc
, ScriptCache
*psc
, DWORD utf32c
, LPWORD pgi
, DWORD flags
)
408 /* BMP: use gdi32 for ease */
409 if (utf32c
< 0x10000)
412 return GetGlyphIndicesW(hdc
,&ch
, 1, pgi
, flags
);
415 if (!psc
->CMAP_format12_Table
)
416 psc
->CMAP_format12_Table
= load_CMAP_format12_table(hdc
, psc
);
418 if (flags
& GGI_MARK_NONEXISTING_GLYPHS
)
423 if (psc
->CMAP_format12_Table
)
425 CMAP_SegmentedCoverage
*format
= NULL
;
426 CMAP_SegmentedCoverage_group
*group
= NULL
;
428 format
= (CMAP_SegmentedCoverage
*)psc
->CMAP_format12_Table
;
430 group
= bsearch(&utf32c
, format
->groups
, GET_BE_DWORD(format
->nGroups
),
431 sizeof(CMAP_SegmentedCoverage_group
), compare_group
);
435 DWORD offset
= utf32c
- GET_BE_DWORD(group
->startCharCode
);
436 *pgi
= GET_BE_DWORD(group
->startGlyphID
) + offset
;
447 static WORD
GDEF_get_glyph_class(const GDEF_Header
*header
, WORD glyph
)
451 const GDEF_ClassDefFormat1
*cf1
;
456 offset
= GET_BE_WORD(header
->GlyphClassDef
);
460 cf1
= (GDEF_ClassDefFormat1
*)(((BYTE
*)header
)+offset
);
461 if (GET_BE_WORD(cf1
->ClassFormat
) == 1)
463 if (glyph
>= GET_BE_WORD(cf1
->StartGlyph
))
465 int index
= glyph
- GET_BE_WORD(cf1
->StartGlyph
);
466 if (index
< GET_BE_WORD(cf1
->GlyphCount
))
467 class = GET_BE_WORD(cf1
->ClassValueArray
[index
]);
470 else if (GET_BE_WORD(cf1
->ClassFormat
) == 2)
472 const GDEF_ClassDefFormat2
*cf2
= (GDEF_ClassDefFormat2
*)cf1
;
474 top
= GET_BE_WORD(cf2
->ClassRangeCount
);
475 for (i
= 0; i
< top
; i
++)
477 if (glyph
>= GET_BE_WORD(cf2
->ClassRangeRecord
[i
].Start
) &&
478 glyph
<= GET_BE_WORD(cf2
->ClassRangeRecord
[i
].End
))
480 class = GET_BE_WORD(cf2
->ClassRangeRecord
[i
].Class
);
486 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1
->ClassFormat
));
491 static VOID
*load_gdef_table(HDC hdc
)
493 VOID
* GDEF_Table
= NULL
;
494 int length
= GetFontData(hdc
, GDEF_TAG
, 0, NULL
, 0);
495 if (length
!= GDI_ERROR
)
497 GDEF_Table
= HeapAlloc(GetProcessHeap(),0,length
);
498 GetFontData(hdc
, GDEF_TAG
, 0, GDEF_Table
, length
);
499 TRACE("Loaded GDEF table of %i bytes\n",length
);
504 void OpenType_GDEF_UpdateGlyphProps(HDC hdc
, ScriptCache
*psc
, const WORD
*pwGlyphs
, const WORD cGlyphs
, WORD
* pwLogClust
, const WORD cChars
, SCRIPT_GLYPHPROP
*pGlyphProp
)
508 if (!psc
->GDEF_Table
)
509 psc
->GDEF_Table
= load_gdef_table(hdc
);
511 for (i
= 0; i
< cGlyphs
; i
++)
517 k
= USP10_FindGlyphInLogClust(pwLogClust
, cChars
, i
);
520 for (; k
< cChars
&& pwLogClust
[k
] == i
; k
++)
524 class = GDEF_get_glyph_class(psc
->GDEF_Table
, pwGlyphs
[i
]);
530 pGlyphProp
[i
].sva
.fClusterStart
= 1;
531 pGlyphProp
[i
].sva
.fDiacritic
= 0;
532 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
535 pGlyphProp
[i
].sva
.fClusterStart
= 1;
536 pGlyphProp
[i
].sva
.fDiacritic
= 0;
537 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
540 pGlyphProp
[i
].sva
.fClusterStart
= 0;
541 pGlyphProp
[i
].sva
.fDiacritic
= 1;
542 pGlyphProp
[i
].sva
.fZeroWidth
= 1;
545 pGlyphProp
[i
].sva
.fClusterStart
= 0;
546 pGlyphProp
[i
].sva
.fDiacritic
= 0;
547 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
550 ERR("Unknown glyph class %i\n",class);
551 pGlyphProp
[i
].sva
.fClusterStart
= 1;
552 pGlyphProp
[i
].sva
.fDiacritic
= 0;
553 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
557 pGlyphProp
[i
].sva
.fClusterStart
= 0;
564 static INT
GSUB_apply_lookup(const OT_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
);
566 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
568 const OT_CoverageFormat1
* cf1
;
572 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
574 int count
= GET_BE_WORD(cf1
->GlyphCount
);
576 TRACE("Coverage Format 1, %i glyphs\n",count
);
577 for (i
= 0; i
< count
; i
++)
578 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
582 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
584 const OT_CoverageFormat2
* cf2
;
587 cf2
= (const OT_CoverageFormat2
*)cf1
;
589 count
= GET_BE_WORD(cf2
->RangeCount
);
590 TRACE("Coverage Format 2, %i ranges\n",count
);
591 for (i
= 0; i
< count
; i
++)
593 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
595 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
596 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
598 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
599 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
605 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
610 static INT
GSUB_apply_SingleSubst(const OT_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
613 TRACE("Single Substitution Subtable\n");
615 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
618 const GSUB_SingleSubstFormat1
*ssf1
;
619 offset
= GET_BE_WORD(look
->SubTable
[j
]);
620 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
621 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
623 int offset
= GET_BE_WORD(ssf1
->Coverage
);
624 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
625 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
627 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
628 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
629 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
630 return glyph_index
+ write_dir
;
635 const GSUB_SingleSubstFormat2
*ssf2
;
639 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
640 offset
= GET_BE_WORD(ssf1
->Coverage
);
641 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
642 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
643 TRACE(" Coverage index %i\n",index
);
646 if (glyphs
[glyph_index
] == GET_BE_WORD(ssf2
->Substitute
[index
]))
647 return GSUB_E_NOGLYPH
;
649 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
650 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
651 TRACE("0x%x\n",glyphs
[glyph_index
]);
652 return glyph_index
+ write_dir
;
656 return GSUB_E_NOGLYPH
;
659 static INT
GSUB_apply_MultipleSubst(const OT_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
662 TRACE("Multiple Substitution Subtable\n");
664 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
667 const GSUB_MultipleSubstFormat1
*msf1
;
668 offset
= GET_BE_WORD(look
->SubTable
[j
]);
669 msf1
= (const GSUB_MultipleSubstFormat1
*)((const BYTE
*)look
+offset
);
671 offset
= GET_BE_WORD(msf1
->Coverage
);
672 index
= GSUB_is_glyph_covered((const BYTE
*)msf1
+offset
, glyphs
[glyph_index
]);
675 const GSUB_Sequence
*seq
;
678 offset
= GET_BE_WORD(msf1
->Sequence
[index
]);
679 seq
= (const GSUB_Sequence
*)((const BYTE
*)msf1
+offset
);
680 sub_count
= GET_BE_WORD(seq
->GlyphCount
);
681 TRACE(" Glyph 0x%x (+%i)->",glyphs
[glyph_index
],(sub_count
-1));
683 for (j
= (*glyph_count
)+(sub_count
-1); j
> glyph_index
; j
--)
684 glyphs
[j
] =glyphs
[j
-(sub_count
-1)];
686 for (j
= 0; j
< sub_count
; j
++)
688 glyphs
[glyph_index
+ (sub_count
-1) - j
] = GET_BE_WORD(seq
->Substitute
[j
]);
690 glyphs
[glyph_index
+ j
] = GET_BE_WORD(seq
->Substitute
[j
]);
692 *glyph_count
= *glyph_count
+ (sub_count
- 1);
694 if (TRACE_ON(uniscribe
))
696 for (j
= 0; j
< sub_count
; j
++)
697 TRACE(" 0x%x",glyphs
[glyph_index
+j
]);
701 return glyph_index
+ (sub_count
* write_dir
);
704 return GSUB_E_NOGLYPH
;
707 static INT
GSUB_apply_AlternateSubst(const OT_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
710 TRACE("Alternate Substitution Subtable\n");
712 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
715 const GSUB_AlternateSubstFormat1
*asf1
;
718 offset
= GET_BE_WORD(look
->SubTable
[j
]);
719 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
720 offset
= GET_BE_WORD(asf1
->Coverage
);
722 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
725 const GSUB_AlternateSet
*as
;
726 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
727 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
728 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
729 if (glyphs
[glyph_index
] == GET_BE_WORD(as
->Alternate
[0]))
730 return GSUB_E_NOGLYPH
;
732 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
733 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
734 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
735 return glyph_index
+ write_dir
;
738 return GSUB_E_NOGLYPH
;
741 static INT
GSUB_apply_LigatureSubst(const OT_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
745 TRACE("Ligature Substitution Subtable\n");
746 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
748 const GSUB_LigatureSubstFormat1
*lsf1
;
751 offset
= GET_BE_WORD(look
->SubTable
[j
]);
752 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
753 offset
= GET_BE_WORD(lsf1
->Coverage
);
754 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
755 TRACE(" Coverage index %i\n",index
);
758 const GSUB_LigatureSet
*ls
;
761 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
762 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
763 count
= GET_BE_WORD(ls
->LigatureCount
);
764 TRACE(" LigatureSet has %i members\n",count
);
765 for (k
= 0; k
< count
; k
++)
767 const GSUB_Ligature
*lig
;
768 int CompCount
,l
,CompIndex
;
770 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
771 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
772 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
773 CompIndex
= glyph_index
+write_dir
;
774 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
777 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
778 if (CompGlyph
!= glyphs
[CompIndex
])
780 CompIndex
+= write_dir
;
784 int replaceIdx
= glyph_index
;
786 replaceIdx
= glyph_index
- CompCount
;
788 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
789 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
790 TRACE("0x%x\n",glyphs
[replaceIdx
]);
794 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
795 glyphs
[j
] =glyphs
[j
+CompCount
];
796 *glyph_count
= *glyph_count
- CompCount
;
798 return replaceIdx
+ write_dir
;
803 return GSUB_E_NOGLYPH
;
806 static INT
GSUB_apply_ChainContextSubst(const OT_LookupList
* lookup
, const OT_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
811 TRACE("Chaining Contextual Substitution Subtable\n");
812 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
814 const GSUB_ChainContextSubstFormat1
*ccsf1
;
816 int dirLookahead
= write_dir
;
817 int dirBacktrack
= -1 * write_dir
;
819 offset
= GET_BE_WORD(look
->SubTable
[j
]);
820 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
821 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
823 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
826 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
828 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
831 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
835 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
836 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
837 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
838 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
839 int newIndex
= glyph_index
;
841 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
843 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
845 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
847 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
848 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
851 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
853 TRACE("Matched Backtrack\n");
855 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
857 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
858 for (k
= 0; k
< indexGlyphs
; k
++)
860 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
861 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
864 if (k
!= indexGlyphs
)
866 TRACE("Matched IndexGlyphs\n");
868 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
870 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
872 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
873 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
))]) == -1)
876 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
878 TRACE("Matched LookAhead\n");
880 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
882 if (GET_BE_WORD(ccsf3_4
->SubstCount
))
884 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
886 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
887 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
889 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
890 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
893 ERR("Chain failed to generate a glyph\n");
899 else return GSUB_E_NOGLYPH
;
905 static INT
GSUB_apply_lookup(const OT_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
908 const OT_LookupTable
*look
;
910 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
911 look
= (const OT_LookupTable
*)((const BYTE
*)lookup
+ offset
);
912 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
913 switch(GET_BE_WORD(look
->LookupType
))
916 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
918 return GSUB_apply_MultipleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
920 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
922 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
924 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
926 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
928 return GSUB_E_NOGLYPH
;
931 INT
OpenType_apply_GSUB_lookup(LPCVOID table
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
933 const GSUB_Header
*header
= (const GSUB_Header
*)table
;
934 const OT_LookupList
*lookup
= (const OT_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
936 return GSUB_apply_lookup(lookup
, lookup_index
, glyphs
, glyph_index
, write_dir
, glyph_count
);
942 static INT
GPOS_get_device_table_value(const OT_DeviceTable
*DeviceTable
, WORD ppem
)
944 static const WORD mask
[3] = {3,0xf,0xff};
945 if (DeviceTable
&& ppem
>= GET_BE_WORD(DeviceTable
->StartSize
) && ppem
<= GET_BE_WORD(DeviceTable
->EndSize
))
947 int format
= GET_BE_WORD(DeviceTable
->DeltaFormat
);
948 int index
= ppem
- GET_BE_WORD(DeviceTable
->StartSize
);
950 TRACE("device table, format %i, index %i\n",format
, index
);
951 index
= index
<< format
;
952 value
= (DeviceTable
->DeltaValue
[index
/sizeof(WORD
)] << (index
%sizeof(WORD
)))&mask
[format
-1];
953 TRACE("offset %i, value %i\n",index
, value
);
954 if (value
> mask
[format
-1]/2)
955 value
= -1 * ((mask
[format
-1]+1) - value
);
961 static VOID
GPOS_get_anchor_values(LPCVOID table
, LPPOINT pt
, WORD ppem
)
963 const GPOS_AnchorFormat1
* anchor1
= (const GPOS_AnchorFormat1
*)table
;
965 switch (GET_BE_WORD(anchor1
->AnchorFormat
))
969 TRACE("Anchor Format 1\n");
970 pt
->x
= (short)GET_BE_WORD(anchor1
->XCoordinate
);
971 pt
->y
= (short)GET_BE_WORD(anchor1
->YCoordinate
);
976 const GPOS_AnchorFormat2
* anchor2
= (const GPOS_AnchorFormat2
*)table
;
977 TRACE("Anchor Format 2\n");
978 pt
->x
= (short)GET_BE_WORD(anchor2
->XCoordinate
);
979 pt
->y
= (short)GET_BE_WORD(anchor2
->YCoordinate
);
985 const GPOS_AnchorFormat3
* anchor3
= (const GPOS_AnchorFormat3
*)table
;
986 TRACE("Anchor Format 3\n");
987 pt
->x
= (short)GET_BE_WORD(anchor3
->XCoordinate
);
988 pt
->y
= (short)GET_BE_WORD(anchor3
->YCoordinate
);
989 offset
= GET_BE_WORD(anchor3
->XDeviceTable
);
990 TRACE("ppem %i\n",ppem
);
993 const OT_DeviceTable
* DeviceTableX
= NULL
;
994 DeviceTableX
= (const OT_DeviceTable
*)((const BYTE
*)anchor3
+ offset
);
995 pt
->x
+= GPOS_get_device_table_value(DeviceTableX
, ppem
);
997 offset
= GET_BE_WORD(anchor3
->YDeviceTable
);
1000 const OT_DeviceTable
* DeviceTableY
= NULL
;
1001 DeviceTableY
= (const OT_DeviceTable
*)((const BYTE
*)anchor3
+ offset
);
1002 pt
->y
+= GPOS_get_device_table_value(DeviceTableY
, ppem
);
1007 ERR("Unknown Anchor Format %i\n",GET_BE_WORD(anchor1
->AnchorFormat
));
1013 static void GPOS_convert_design_units_to_device(LPOUTLINETEXTMETRICW lpotm
, LPLOGFONTW lplogfont
, int desX
, int desY
, double *devX
, double *devY
)
1015 int emHeight
= lpotm
->otmTextMetrics
.tmAscent
+ lpotm
->otmTextMetrics
.tmDescent
- lpotm
->otmTextMetrics
.tmInternalLeading
;
1017 TRACE("emHeight %i lfWidth %i\n",emHeight
, lplogfont
->lfWidth
);
1018 *devX
= (desX
* emHeight
) / (double)lpotm
->otmEMSquare
;
1019 *devY
= (desY
* emHeight
) / (double)lpotm
->otmEMSquare
;
1020 if (lplogfont
->lfWidth
)
1021 FIXME("Font with lfWidth set no handled properly\n");
1024 static INT
GPOS_get_value_record(WORD ValueFormat
, const WORD data
[], GPOS_ValueRecord
*record
)
1027 if (ValueFormat
& 0x0001) record
->XPlacement
= GET_BE_WORD(data
[offset
++]);
1028 if (ValueFormat
& 0x0002) record
->YPlacement
= GET_BE_WORD(data
[offset
++]);
1029 if (ValueFormat
& 0x0004) record
->XAdvance
= GET_BE_WORD(data
[offset
++]);
1030 if (ValueFormat
& 0x0008) record
->YAdvance
= GET_BE_WORD(data
[offset
++]);
1031 if (ValueFormat
& 0x0010) record
->XPlaDevice
= GET_BE_WORD(data
[offset
++]);
1032 if (ValueFormat
& 0x0020) record
->YPlaDevice
= GET_BE_WORD(data
[offset
++]);
1033 if (ValueFormat
& 0x0040) record
->XAdvDevice
= GET_BE_WORD(data
[offset
++]);
1034 if (ValueFormat
& 0x0080) record
->YAdvDevice
= GET_BE_WORD(data
[offset
++]);
1038 static VOID
GPOS_get_value_record_offsets(const BYTE
* head
, GPOS_ValueRecord
*ValueRecord
, WORD ValueFormat
, INT ppem
, LPPOINT ptPlacement
, LPPOINT ptAdvance
)
1040 if (ValueFormat
& 0x0001) ptPlacement
->x
+= (short)ValueRecord
->XPlacement
;
1041 if (ValueFormat
& 0x0002) ptPlacement
->y
+= (short)ValueRecord
->YPlacement
;
1042 if (ValueFormat
& 0x0004) ptAdvance
->x
+= (short)ValueRecord
->XAdvance
;
1043 if (ValueFormat
& 0x0008) ptAdvance
->y
+= (short)ValueRecord
->YAdvance
;
1044 if (ValueFormat
& 0x0010) ptPlacement
->x
+= GPOS_get_device_table_value((const OT_DeviceTable
*)(head
+ ValueRecord
->XPlaDevice
), ppem
);
1045 if (ValueFormat
& 0x0020) ptPlacement
->y
+= GPOS_get_device_table_value((const OT_DeviceTable
*)(head
+ ValueRecord
->YPlaDevice
), ppem
);
1046 if (ValueFormat
& 0x0040) ptAdvance
->x
+= GPOS_get_device_table_value((const OT_DeviceTable
*)(head
+ ValueRecord
->XAdvDevice
), ppem
);
1047 if (ValueFormat
& 0x0080) ptAdvance
->y
+= GPOS_get_device_table_value((const OT_DeviceTable
*)(head
+ ValueRecord
->YAdvDevice
), ppem
);
1048 if (ValueFormat
& 0xFF00) FIXME("Unhandled Value Format %x\n",ValueFormat
&0xFF00);
1051 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
)
1055 TRACE("MarkToBase Attachment Positioning Subtable\n");
1057 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
1060 const GPOS_MarkBasePosFormat1
*mbpf1
;
1061 offset
= GET_BE_WORD(look
->SubTable
[j
]);
1062 mbpf1
= (const GPOS_MarkBasePosFormat1
*)((const BYTE
*)look
+offset
);
1063 if (GET_BE_WORD(mbpf1
->PosFormat
) == 1)
1065 int offset
= GET_BE_WORD(mbpf1
->MarkCoverage
);
1067 mark_index
= GSUB_is_glyph_covered((const BYTE
*)mbpf1
+offset
, glyphs
[glyph_index
]);
1068 if (mark_index
!= -1)
1071 offset
= GET_BE_WORD(mbpf1
->BaseCoverage
);
1072 base_index
= GSUB_is_glyph_covered((const BYTE
*)mbpf1
+offset
, glyphs
[glyph_index
- write_dir
]);
1073 if (base_index
!= -1)
1075 const GPOS_MarkArray
*ma
;
1076 const GPOS_MarkRecord
*mr
;
1077 const GPOS_BaseArray
*ba
;
1078 const GPOS_BaseRecord
*br
;
1080 int class_count
= GET_BE_WORD(mbpf1
->ClassCount
);
1081 int baserecord_size
;
1084 TRACE("Mark %x(%i) and base %x(%i)\n",glyphs
[glyph_index
], mark_index
, glyphs
[glyph_index
- write_dir
], base_index
);
1085 offset
= GET_BE_WORD(mbpf1
->MarkArray
);
1086 ma
= (const GPOS_MarkArray
*)((const BYTE
*)mbpf1
+ offset
);
1087 if (mark_index
> GET_BE_WORD(ma
->MarkCount
))
1089 ERR("Mark index exeeded mark count\n");
1092 mr
= &ma
->MarkRecord
[mark_index
];
1093 mark_class
= GET_BE_WORD(mr
->Class
);
1094 TRACE("Mark Class %i total classes %i\n",mark_class
,class_count
);
1095 offset
= GET_BE_WORD(mbpf1
->BaseArray
);
1096 ba
= (const GPOS_BaseArray
*)((const BYTE
*)mbpf1
+ offset
);
1097 baserecord_size
= class_count
* sizeof(WORD
);
1098 br
= (const GPOS_BaseRecord
*)((const BYTE
*)ba
+ sizeof(WORD
) + (baserecord_size
* base_index
));
1099 offset
= GET_BE_WORD(br
->BaseAnchor
[mark_class
]);
1100 GPOS_get_anchor_values((const BYTE
*)ba
+ offset
, &base_pt
, ppem
);
1101 offset
= GET_BE_WORD(mr
->MarkAnchor
);
1102 GPOS_get_anchor_values((const BYTE
*)ma
+ offset
, &mark_pt
, ppem
);
1103 TRACE("Offset on base is %i,%i design units\n",base_pt
.x
,base_pt
.y
);
1104 TRACE("Offset on mark is %i,%i design units\n",mark_pt
.x
, mark_pt
.y
);
1105 pt
->x
+= base_pt
.x
- mark_pt
.x
;
1106 pt
->y
+= base_pt
.y
- mark_pt
.y
;
1107 TRACE("Resulting cumulative offset is %i,%i design units\n",pt
->x
,pt
->y
);
1112 FIXME("Unhandled Mark To Base Format %i\n",GET_BE_WORD(mbpf1
->PosFormat
));
1116 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
)
1119 const OT_LookupTable
*look
;
1120 int ppem
= lpotm
->otmTextMetrics
.tmAscent
+ lpotm
->otmTextMetrics
.tmDescent
- lpotm
->otmTextMetrics
.tmInternalLeading
;
1122 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
1123 look
= (const OT_LookupTable
*)((const BYTE
*)lookup
+ offset
);
1124 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
1125 switch(GET_BE_WORD(look
->LookupType
))
1131 GPOS_apply_MarkToBase(look
, glyphs
, glyph_index
, write_dir
, glyph_count
, ppem
, &desU
);
1132 if (desU
.x
|| desU
.y
)
1134 GPOS_convert_design_units_to_device(lpotm
, lplogfont
, desU
.x
, desU
.y
, &devX
, &devY
);
1135 pGoffset
[glyph_index
].du
+= ((int)(devX
+0.5) - piAdvance
[glyph_index
-1]);
1136 pGoffset
[glyph_index
].dv
+= (int)(devY
+0.5);
1141 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
1143 return glyph_index
+1;
1146 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
)
1148 const GPOS_Header
*header
= (const GPOS_Header
*)table
;
1149 const OT_LookupList
*lookup
= (const OT_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
1151 return GPOS_apply_lookup(lpotm
, lplogfont
, piAdvance
, lookup
, lookup_index
, glyphs
, glyph_index
, write_dir
, glyph_count
, pGoffset
);
1154 static void GSUB_initialize_script_cache(ScriptCache
*psc
)
1158 if (psc
->GSUB_Table
)
1160 const OT_ScriptList
*script
;
1161 const GSUB_Header
* header
= (const GSUB_Header
*)psc
->GSUB_Table
;
1162 script
= (const OT_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
1163 psc
->script_count
= GET_BE_WORD(script
->ScriptCount
);
1164 TRACE("initializing %i scripts in this font\n",psc
->script_count
);
1165 if (psc
->script_count
)
1167 psc
->scripts
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(LoadedScript
) * psc
->script_count
);
1168 for (i
= 0; i
< psc
->script_count
; i
++)
1170 int offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
1171 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]);
1172 psc
->scripts
[i
].gsub_table
= ((const BYTE
*)script
+ offset
);
1178 static void GPOS_expand_script_cache(ScriptCache
*psc
)
1181 const OT_ScriptList
*script
;
1182 const GPOS_Header
* header
= (const GPOS_Header
*)psc
->GPOS_Table
;
1187 script
= (const OT_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
1188 count
= GET_BE_WORD(script
->ScriptCount
);
1190 if (!psc
->script_count
)
1192 psc
->script_count
= count
;
1193 TRACE("initializing %i scripts in this font\n",psc
->script_count
);
1194 if (psc
->script_count
)
1196 psc
->scripts
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(LoadedScript
) * psc
->script_count
);
1197 for (i
= 0; i
< psc
->script_count
; i
++)
1199 int offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
1200 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]);
1201 psc
->scripts
[i
].gpos_table
= ((const BYTE
*)script
+ offset
);
1207 for (i
= 0; i
< count
; i
++)
1210 int offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
1211 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]);
1212 for (j
= 0; j
< psc
->script_count
; j
++)
1214 if (psc
->scripts
[j
].tag
== tag
)
1216 psc
->scripts
[j
].gpos_table
= ((const BYTE
*)script
+ offset
);
1220 if (j
== psc
->script_count
)
1222 psc
->script_count
++;
1223 psc
->scripts
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,psc
->scripts
, sizeof(LoadedScript
) * psc
->script_count
);
1224 psc
->scripts
[j
].tag
= tag
;
1225 psc
->scripts
[j
].gpos_table
= ((const BYTE
*)script
+ offset
);
1231 static void _initialize_script_cache(ScriptCache
*psc
)
1233 if (!psc
->script_count
)
1235 GSUB_initialize_script_cache(psc
);
1236 GPOS_expand_script_cache(psc
);
1240 HRESULT
OpenType_GetFontScriptTags(ScriptCache
*psc
, OPENTYPE_TAG searchingFor
, int cMaxTags
, OPENTYPE_TAG
*pScriptTags
, int *pcTags
)
1245 _initialize_script_cache(psc
);
1247 *pcTags
= psc
->script_count
;
1249 if (!searchingFor
&& cMaxTags
< *pcTags
)
1251 else if (searchingFor
)
1252 rc
= USP_E_SCRIPT_NOT_IN_FONT
;
1254 for (i
= 0; i
< psc
->script_count
; i
++)
1257 pScriptTags
[i
] = psc
->scripts
[i
].tag
;
1261 if (searchingFor
== psc
->scripts
[i
].tag
)
1263 pScriptTags
[0] = psc
->scripts
[i
].tag
;
1273 static void GSUB_initialize_language_cache(LoadedScript
*script
)
1277 if (script
->gsub_table
)
1280 const OT_Script
* table
= script
->gsub_table
;
1281 script
->language_count
= GET_BE_WORD(table
->LangSysCount
);
1282 offset
= GET_BE_WORD(table
->DefaultLangSys
);
1285 script
->default_language
.tag
= MS_MAKE_TAG('d','f','l','t');
1286 script
->default_language
.gsub_table
= (const BYTE
*)table
+ offset
;
1289 if (script
->language_count
)
1291 TRACE("Deflang %p, LangCount %i\n",script
->default_language
.gsub_table
, script
->language_count
);
1293 script
->languages
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LoadedLanguage
) * script
->language_count
);
1295 for (i
= 0; i
< script
->language_count
; i
++)
1297 int offset
= GET_BE_WORD(table
->LangSysRecord
[i
].LangSys
);
1298 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]);
1299 script
->languages
[i
].gsub_table
= ((const BYTE
*)table
+ offset
);
1305 static void GPOS_expand_language_cache(LoadedScript
*script
)
1308 const OT_Script
* table
= script
->gpos_table
;
1314 offset
= GET_BE_WORD(table
->DefaultLangSys
);
1316 script
->default_language
.gpos_table
= (const BYTE
*)table
+ offset
;
1318 count
= GET_BE_WORD(table
->LangSysCount
);
1320 TRACE("Deflang %p, LangCount %i\n",script
->default_language
.gpos_table
, count
);
1321 if (!script
->language_count
)
1324 script
->language_count
= count
;
1326 script
->languages
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LoadedLanguage
) * script
->language_count
);
1328 for (i
= 0; i
< script
->language_count
; i
++)
1330 int offset
= GET_BE_WORD(table
->LangSysRecord
[i
].LangSys
);
1331 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]);
1332 script
->languages
[i
].gpos_table
= ((const BYTE
*)table
+ offset
);
1338 for (i
= 0; i
< count
; i
++)
1340 int offset
= GET_BE_WORD(table
->LangSysRecord
[i
].LangSys
);
1341 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]);
1343 for (j
= 0; j
< script
->language_count
; j
++)
1345 if (script
->languages
[j
].tag
== tag
)
1347 script
->languages
[j
].gpos_table
= ((const BYTE
*)table
+ offset
);
1351 if (j
== script
->language_count
)
1353 script
->language_count
++;
1354 script
->languages
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,script
->languages
, sizeof(LoadedLanguage
) * script
->language_count
);
1355 script
->languages
[j
].tag
= tag
;
1356 script
->languages
[j
].gpos_table
= ((const BYTE
*)table
+ offset
);
1362 static void _initialize_language_cache(LoadedScript
*script
)
1364 if (!script
->language_count
)
1366 GSUB_initialize_language_cache(script
);
1367 GPOS_expand_language_cache(script
);
1371 HRESULT
OpenType_GetFontLanguageTags(ScriptCache
*psc
, OPENTYPE_TAG script_tag
, OPENTYPE_TAG searchingFor
, int cMaxTags
, OPENTYPE_TAG
*pLanguageTags
, int *pcTags
)
1375 LoadedScript
*script
= NULL
;
1377 _initialize_script_cache(psc
);
1379 for (i
= 0; i
< psc
->script_count
; i
++)
1381 if (psc
->scripts
[i
].tag
== script_tag
)
1383 script
= &psc
->scripts
[i
];
1389 return E_INVALIDARG
;
1391 _initialize_language_cache(script
);
1393 if (!searchingFor
&& cMaxTags
< script
->language_count
)
1395 else if (searchingFor
)
1398 *pcTags
= script
->language_count
;
1400 for (i
= 0; i
< script
->language_count
; i
++)
1403 pLanguageTags
[i
] = script
->languages
[i
].tag
;
1407 if (searchingFor
== script
->languages
[i
].tag
)
1409 pLanguageTags
[0] = script
->languages
[i
].tag
;
1417 if (script
->default_language
.gsub_table
)
1420 pLanguageTags
[i
] = script
->default_language
.tag
;
1422 if (searchingFor
&& FAILED(rc
))
1424 pLanguageTags
[0] = script
->default_language
.tag
;
1427 *pcTags
= (*pcTags
) + 1;
1434 static void GSUB_initialize_feature_cache(LPCVOID table
, LoadedLanguage
*language
)
1438 if (language
->gsub_table
)
1440 const OT_LangSys
*lang
= language
->gsub_table
;
1441 const GSUB_Header
*header
= (const GSUB_Header
*)table
;
1442 const OT_FeatureList
*feature_list
;
1444 language
->feature_count
= GET_BE_WORD(lang
->FeatureCount
);
1445 TRACE("%i features\n",language
->feature_count
);
1447 if (language
->feature_count
)
1449 language
->features
= HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature
)*language
->feature_count
);
1451 feature_list
= (const OT_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
1453 for (i
= 0; i
< language
->feature_count
; i
++)
1455 const OT_Feature
*feature
;
1457 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
1459 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]);
1460 language
->features
[i
].feature
= ((const BYTE
*)feature_list
+ GET_BE_WORD(feature_list
->FeatureRecord
[index
].Feature
));
1461 feature
= (const OT_Feature
*)language
->features
[i
].feature
;
1462 language
->features
[i
].lookup_count
= GET_BE_WORD(feature
->LookupCount
);
1463 language
->features
[i
].lookups
= HeapAlloc(GetProcessHeap(),0,sizeof(WORD
) * language
->features
[i
].lookup_count
);
1464 for (j
= 0; j
< language
->features
[i
].lookup_count
; j
++)
1465 language
->features
[i
].lookups
[j
] = GET_BE_WORD(feature
->LookupListIndex
[j
]);
1471 static void GPOS_expand_feature_cache(LPCVOID table
, LoadedLanguage
*language
)
1474 const OT_LangSys
*lang
= language
->gpos_table
;
1475 const GPOS_Header
*header
= (const GPOS_Header
*)table
;
1476 const OT_FeatureList
*feature_list
;
1481 count
= GET_BE_WORD(lang
->FeatureCount
);
1482 feature_list
= (const OT_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
1484 TRACE("%i features\n",count
);
1485 if (!language
->feature_count
)
1487 language
->feature_count
= count
;
1489 if (language
->feature_count
)
1491 language
->features
= HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature
)*language
->feature_count
);
1493 for (i
= 0; i
< language
->feature_count
; i
++)
1495 const OT_Feature
*feature
;
1497 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
1499 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]);
1500 language
->features
[i
].feature
= ((const BYTE
*)feature_list
+ GET_BE_WORD(feature_list
->FeatureRecord
[index
].Feature
));
1501 feature
= (const OT_Feature
*)language
->features
[i
].feature
;
1502 language
->features
[i
].lookup_count
= GET_BE_WORD(feature
->LookupCount
);
1503 language
->features
[i
].lookups
= HeapAlloc(GetProcessHeap(),0,sizeof(WORD
) * language
->features
[i
].lookup_count
);
1504 for (j
= 0; j
< language
->features
[i
].lookup_count
; j
++)
1505 language
->features
[i
].lookups
[j
] = GET_BE_WORD(feature
->LookupListIndex
[j
]);
1511 language
->features
= HeapReAlloc(GetProcessHeap(),0,language
->features
, sizeof(LoadedFeature
)*(language
->feature_count
+ count
));
1513 for (i
= 0; i
< count
; i
++)
1515 const OT_Feature
*feature
;
1517 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
1518 int idx
= language
->feature_count
+ i
;
1520 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]);
1521 language
->features
[idx
].feature
= ((const BYTE
*)feature_list
+ GET_BE_WORD(feature_list
->FeatureRecord
[index
].Feature
));
1522 feature
= (const OT_Feature
*)language
->features
[idx
].feature
;
1523 language
->features
[idx
].lookup_count
= GET_BE_WORD(feature
->LookupCount
);
1524 language
->features
[idx
].lookups
= HeapAlloc(GetProcessHeap(),0,sizeof(WORD
) * language
->features
[idx
].lookup_count
);
1525 for (j
= 0; j
< language
->features
[idx
].lookup_count
; j
++)
1526 language
->features
[idx
].lookups
[j
] = GET_BE_WORD(feature
->LookupListIndex
[j
]);
1528 language
->feature_count
+= count
;
1532 static void _initialize_feature_cache(ScriptCache
*psc
, LoadedLanguage
*language
)
1534 if (!language
->feature_count
)
1536 GSUB_initialize_feature_cache(psc
->GSUB_Table
, language
);
1537 GPOS_expand_feature_cache(psc
->GPOS_Table
, language
);
1541 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
)
1545 LoadedScript
*script
= NULL
;
1546 LoadedLanguage
*language
= NULL
;
1548 _initialize_script_cache(psc
);
1550 for (i
= 0; i
< psc
->script_count
; i
++)
1552 if (psc
->scripts
[i
].tag
== script_tag
)
1554 script
= &psc
->scripts
[i
];
1565 return E_INVALIDARG
;
1568 _initialize_language_cache(script
);
1570 if ((script
->default_language
.gsub_table
|| script
->default_language
.gpos_table
) && script
->default_language
.tag
== language_tag
)
1571 language
= &script
->default_language
;
1574 for (i
= 0; i
< script
->language_count
; i
++)
1576 if (script
->languages
[i
].tag
== language_tag
)
1578 language
= &script
->languages
[i
];
1590 _initialize_feature_cache(psc
, language
);
1592 *pcTags
= language
->feature_count
;
1594 if (!searchingFor
&& cMaxTags
< *pcTags
)
1596 else if (searchingFor
)
1599 for (i
= 0; i
< language
->feature_count
; i
++)
1602 pFeatureTags
[i
] = language
->features
[i
].tag
;
1606 if (searchingFor
== language
->features
[i
].tag
)
1608 pFeatureTags
[0] = language
->features
[i
].tag
;
1611 *feature
= &language
->features
[i
];