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 GSUB_ScriptRecord ScriptRecord
[1];
130 } GSUB_LangSysRecord
;
135 GSUB_LangSysRecord LangSysRecord
[1];
139 WORD LookupOrder
; /* Reserved */
140 WORD ReqFeatureIndex
;
142 WORD FeatureIndex
[1];
148 } GSUB_FeatureRecord
;
152 GSUB_FeatureRecord FeatureRecord
[1];
156 WORD FeatureParams
; /* Reserved */
158 WORD LookupListIndex
[1];
177 } GSUB_CoverageFormat1
;
182 WORD StartCoverageIndex
;
188 GSUB_RangeRecord RangeRecord
[1];
189 } GSUB_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
;
284 static VOID
*load_CMAP_format12_table(HDC hdc
, ScriptCache
*psc
)
286 CMAP_Header
*CMAP_Table
= NULL
;
290 if (!psc
->CMAP_Table
)
292 length
= GetFontData(hdc
, CMAP_TAG
, 0, NULL
, 0);
293 if (length
!= GDI_ERROR
)
295 psc
->CMAP_Table
= HeapAlloc(GetProcessHeap(),0,length
);
296 GetFontData(hdc
, CMAP_TAG
, 0, psc
->CMAP_Table
, length
);
297 TRACE("Loaded cmap table of %i bytes\n",length
);
303 CMAP_Table
= psc
->CMAP_Table
;
305 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
); i
++)
307 if ( (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) == 3) &&
308 (GET_BE_WORD(CMAP_Table
->tables
[i
].encodingID
) == 10) )
310 CMAP_SegmentedCoverage
*format
= (CMAP_SegmentedCoverage
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
311 if (GET_BE_WORD(format
->format
) == 12)
318 static int compare_group(const void *a
, const void* b
)
320 const DWORD
*chr
= a
;
321 const CMAP_SegmentedCoverage_group
*group
= b
;
323 if (*chr
< GET_BE_DWORD(group
->startCharCode
))
325 if (*chr
> GET_BE_DWORD(group
->endCharCode
))
330 DWORD
OpenType_CMAP_GetGlyphIndex(HDC hdc
, ScriptCache
*psc
, DWORD utf32c
, LPWORD pgi
, DWORD flags
)
332 /* BMP: use gdi32 for ease */
333 if (utf32c
< 0x10000)
336 return GetGlyphIndicesW(hdc
,&ch
, 1, pgi
, flags
);
339 if (!psc
->CMAP_format12_Table
)
340 psc
->CMAP_format12_Table
= load_CMAP_format12_table(hdc
, psc
);
342 if (flags
& GGI_MARK_NONEXISTING_GLYPHS
)
347 if (psc
->CMAP_format12_Table
)
349 CMAP_SegmentedCoverage
*format
= NULL
;
350 CMAP_SegmentedCoverage_group
*group
= NULL
;
352 format
= (CMAP_SegmentedCoverage
*)psc
->CMAP_format12_Table
;
354 group
= bsearch(&utf32c
, format
->groups
, GET_BE_DWORD(format
->nGroups
),
355 sizeof(CMAP_SegmentedCoverage_group
), compare_group
);
359 DWORD offset
= utf32c
- GET_BE_DWORD(group
->startCharCode
);
360 *pgi
= GET_BE_DWORD(group
->startGlyphID
) + offset
;
371 static WORD
GDEF_get_glyph_class(const GDEF_Header
*header
, WORD glyph
)
375 const GDEF_ClassDefFormat1
*cf1
;
380 offset
= GET_BE_WORD(header
->GlyphClassDef
);
384 cf1
= (GDEF_ClassDefFormat1
*)(((BYTE
*)header
)+offset
);
385 if (GET_BE_WORD(cf1
->ClassFormat
) == 1)
387 if (glyph
>= GET_BE_WORD(cf1
->StartGlyph
))
389 int index
= glyph
- GET_BE_WORD(cf1
->StartGlyph
);
390 if (index
< GET_BE_WORD(cf1
->GlyphCount
))
391 class = GET_BE_WORD(cf1
->ClassValueArray
[index
]);
394 else if (GET_BE_WORD(cf1
->ClassFormat
) == 2)
396 const GDEF_ClassDefFormat2
*cf2
= (GDEF_ClassDefFormat2
*)cf1
;
398 top
= GET_BE_WORD(cf2
->ClassRangeCount
);
399 for (i
= 0; i
< top
; i
++)
401 if (glyph
>= GET_BE_WORD(cf2
->ClassRangeRecord
[i
].Start
) &&
402 glyph
<= GET_BE_WORD(cf2
->ClassRangeRecord
[i
].End
))
404 class = GET_BE_WORD(cf2
->ClassRangeRecord
[i
].Class
);
410 ERR("Unknown Class Format %i\n",GET_BE_WORD(cf1
->ClassFormat
));
415 static VOID
*load_gdef_table(HDC hdc
)
417 VOID
* GDEF_Table
= NULL
;
418 int length
= GetFontData(hdc
, GDEF_TAG
, 0, NULL
, 0);
419 if (length
!= GDI_ERROR
)
421 GDEF_Table
= HeapAlloc(GetProcessHeap(),0,length
);
422 GetFontData(hdc
, GDEF_TAG
, 0, GDEF_Table
, length
);
423 TRACE("Loaded GDEF table of %i bytes\n",length
);
428 void OpenType_GDEF_UpdateGlyphProps(HDC hdc
, ScriptCache
*psc
, const WORD
*pwGlyphs
, const WORD cGlyphs
, WORD
* pwLogClust
, const WORD cChars
, SCRIPT_GLYPHPROP
*pGlyphProp
)
432 if (!psc
->GDEF_Table
)
433 psc
->GDEF_Table
= load_gdef_table(hdc
);
435 for (i
= 0; i
< cGlyphs
; i
++)
441 k
= USP10_FindGlyphInLogClust(pwLogClust
, cChars
, i
);
444 for (; k
< cChars
&& pwLogClust
[k
] == i
; k
++)
448 class = GDEF_get_glyph_class(psc
->GDEF_Table
, pwGlyphs
[i
]);
454 pGlyphProp
[i
].sva
.fClusterStart
= 1;
455 pGlyphProp
[i
].sva
.fDiacritic
= 0;
456 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
459 pGlyphProp
[i
].sva
.fClusterStart
= 1;
460 pGlyphProp
[i
].sva
.fDiacritic
= 0;
461 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
464 pGlyphProp
[i
].sva
.fClusterStart
= 0;
465 pGlyphProp
[i
].sva
.fDiacritic
= 1;
466 pGlyphProp
[i
].sva
.fZeroWidth
= 1;
469 pGlyphProp
[i
].sva
.fClusterStart
= 0;
470 pGlyphProp
[i
].sva
.fDiacritic
= 0;
471 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
474 ERR("Unknown glyph class %i\n",class);
475 pGlyphProp
[i
].sva
.fClusterStart
= 1;
476 pGlyphProp
[i
].sva
.fDiacritic
= 0;
477 pGlyphProp
[i
].sva
.fZeroWidth
= 0;
481 pGlyphProp
[i
].sva
.fClusterStart
= 0;
488 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
);
490 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
492 const GSUB_CoverageFormat1
* cf1
;
496 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
498 int count
= GET_BE_WORD(cf1
->GlyphCount
);
500 TRACE("Coverage Format 1, %i glyphs\n",count
);
501 for (i
= 0; i
< count
; i
++)
502 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
506 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
508 const GSUB_CoverageFormat2
* cf2
;
511 cf2
= (const GSUB_CoverageFormat2
*)cf1
;
513 count
= GET_BE_WORD(cf2
->RangeCount
);
514 TRACE("Coverage Format 2, %i ranges\n",count
);
515 for (i
= 0; i
< count
; i
++)
517 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
519 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
520 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
522 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
523 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
529 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
534 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
537 TRACE("Single Substitution Subtable\n");
539 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
542 const GSUB_SingleSubstFormat1
*ssf1
;
543 offset
= GET_BE_WORD(look
->SubTable
[j
]);
544 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
545 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
547 int offset
= GET_BE_WORD(ssf1
->Coverage
);
548 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
549 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
551 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
552 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
553 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
554 return glyph_index
+ write_dir
;
559 const GSUB_SingleSubstFormat2
*ssf2
;
563 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
564 offset
= GET_BE_WORD(ssf1
->Coverage
);
565 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
566 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
567 TRACE(" Coverage index %i\n",index
);
570 if (glyphs
[glyph_index
] == GET_BE_WORD(ssf2
->Substitute
[index
]))
571 return GSUB_E_NOGLYPH
;
573 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
574 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
575 TRACE("0x%x\n",glyphs
[glyph_index
]);
576 return glyph_index
+ write_dir
;
580 return GSUB_E_NOGLYPH
;
583 static INT
GSUB_apply_MultipleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
586 TRACE("Multiple Substitution Subtable\n");
588 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
591 const GSUB_MultipleSubstFormat1
*msf1
;
592 offset
= GET_BE_WORD(look
->SubTable
[j
]);
593 msf1
= (const GSUB_MultipleSubstFormat1
*)((const BYTE
*)look
+offset
);
595 offset
= GET_BE_WORD(msf1
->Coverage
);
596 index
= GSUB_is_glyph_covered((const BYTE
*)msf1
+offset
, glyphs
[glyph_index
]);
599 const GSUB_Sequence
*seq
;
602 offset
= GET_BE_WORD(msf1
->Sequence
[index
]);
603 seq
= (const GSUB_Sequence
*)((const BYTE
*)msf1
+offset
);
604 sub_count
= GET_BE_WORD(seq
->GlyphCount
);
605 TRACE(" Glyph 0x%x (+%i)->",glyphs
[glyph_index
],(sub_count
-1));
607 for (j
= (*glyph_count
)+(sub_count
-1); j
> glyph_index
; j
--)
608 glyphs
[j
] =glyphs
[j
-(sub_count
-1)];
610 for (j
= 0; j
< sub_count
; j
++)
612 glyphs
[glyph_index
+ (sub_count
-1) - j
] = GET_BE_WORD(seq
->Substitute
[j
]);
614 glyphs
[glyph_index
+ j
] = GET_BE_WORD(seq
->Substitute
[j
]);
616 *glyph_count
= *glyph_count
+ (sub_count
- 1);
618 if (TRACE_ON(uniscribe
))
620 for (j
= 0; j
< sub_count
; j
++)
621 TRACE(" 0x%x",glyphs
[glyph_index
+j
]);
625 return glyph_index
+ (sub_count
* write_dir
);
628 return GSUB_E_NOGLYPH
;
631 static INT
GSUB_apply_AlternateSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
634 TRACE("Alternate Substitution Subtable\n");
636 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
639 const GSUB_AlternateSubstFormat1
*asf1
;
642 offset
= GET_BE_WORD(look
->SubTable
[j
]);
643 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
644 offset
= GET_BE_WORD(asf1
->Coverage
);
646 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
649 const GSUB_AlternateSet
*as
;
650 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
651 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
652 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
653 if (glyphs
[glyph_index
] == GET_BE_WORD(as
->Alternate
[0]))
654 return GSUB_E_NOGLYPH
;
656 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
657 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
658 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
659 return glyph_index
+ write_dir
;
662 return GSUB_E_NOGLYPH
;
665 static INT
GSUB_apply_LigatureSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
669 TRACE("Ligature Substitution Subtable\n");
670 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
672 const GSUB_LigatureSubstFormat1
*lsf1
;
675 offset
= GET_BE_WORD(look
->SubTable
[j
]);
676 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
677 offset
= GET_BE_WORD(lsf1
->Coverage
);
678 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
679 TRACE(" Coverage index %i\n",index
);
682 const GSUB_LigatureSet
*ls
;
685 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
686 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
687 count
= GET_BE_WORD(ls
->LigatureCount
);
688 TRACE(" LigatureSet has %i members\n",count
);
689 for (k
= 0; k
< count
; k
++)
691 const GSUB_Ligature
*lig
;
692 int CompCount
,l
,CompIndex
;
694 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
695 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
696 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
697 CompIndex
= glyph_index
+write_dir
;
698 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
701 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
702 if (CompGlyph
!= glyphs
[CompIndex
])
704 CompIndex
+= write_dir
;
708 int replaceIdx
= glyph_index
;
710 replaceIdx
= glyph_index
- CompCount
;
712 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
713 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
714 TRACE("0x%x\n",glyphs
[replaceIdx
]);
718 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
719 glyphs
[j
] =glyphs
[j
+CompCount
];
720 *glyph_count
= *glyph_count
- CompCount
;
722 return replaceIdx
+ write_dir
;
727 return GSUB_E_NOGLYPH
;
730 static INT
GSUB_apply_ChainContextSubst(const GSUB_LookupList
* lookup
, const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
735 TRACE("Chaining Contextual Substitution Subtable\n");
736 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
738 const GSUB_ChainContextSubstFormat1
*ccsf1
;
740 int dirLookahead
= write_dir
;
741 int dirBacktrack
= -1 * write_dir
;
743 offset
= GET_BE_WORD(look
->SubTable
[j
]);
744 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
745 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
747 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
750 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
752 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
755 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
759 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
760 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
761 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
762 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
763 int newIndex
= glyph_index
;
765 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
767 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
769 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
771 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
772 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
775 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
777 TRACE("Matched Backtrack\n");
779 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
781 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
782 for (k
= 0; k
< indexGlyphs
; k
++)
784 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
785 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
788 if (k
!= indexGlyphs
)
790 TRACE("Matched IndexGlyphs\n");
792 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
794 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
796 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
797 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
))]) == -1)
800 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
802 TRACE("Matched LookAhead\n");
804 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
806 if (GET_BE_WORD(ccsf3_4
->SubstCount
))
808 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
810 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
811 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
813 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
814 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
817 ERR("Chain failed to generate a glyph\n");
823 else return GSUB_E_NOGLYPH
;
829 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
832 const GSUB_LookupTable
*look
;
834 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
835 look
= (const GSUB_LookupTable
*)((const BYTE
*)lookup
+ offset
);
836 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
837 switch(GET_BE_WORD(look
->LookupType
))
840 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
842 return GSUB_apply_MultipleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
844 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
846 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
848 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
850 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
852 return GSUB_E_NOGLYPH
;
855 INT
OpenType_apply_GSUB_lookup(LPCVOID table
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
857 const GSUB_Header
*header
= (const GSUB_Header
*)table
;
858 const GSUB_LookupList
*lookup
= (const GSUB_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
860 return GSUB_apply_lookup(lookup
, lookup_index
, glyphs
, glyph_index
, write_dir
, glyph_count
);
863 static void GSUB_initialize_script_cache(ScriptCache
*psc
)
867 if (!psc
->script_count
)
869 const GSUB_ScriptList
*script
;
870 const GSUB_Header
* header
= (const GSUB_Header
*)psc
->GSUB_Table
;
871 script
= (const GSUB_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
872 psc
->script_count
= GET_BE_WORD(script
->ScriptCount
);
873 TRACE("initializing %i scripts in this font\n",psc
->script_count
);
874 if (psc
->script_count
)
876 psc
->scripts
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(LoadedScript
) * psc
->script_count
);
877 for (i
= 0; i
< psc
->script_count
; i
++)
879 int offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
880 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]);
881 psc
->scripts
[i
].table
= ((const BYTE
*)script
+ offset
);
887 HRESULT
OpenType_GSUB_GetFontScriptTags(ScriptCache
*psc
, OPENTYPE_TAG searchingFor
, int cMaxTags
, OPENTYPE_TAG
*pScriptTags
, int *pcTags
, LPCVOID
* script_table
)
892 GSUB_initialize_script_cache(psc
);
893 *pcTags
= psc
->script_count
;
895 if (!searchingFor
&& cMaxTags
< *pcTags
)
897 else if (searchingFor
)
898 rc
= USP_E_SCRIPT_NOT_IN_FONT
;
900 for (i
= 0; i
< psc
->script_count
; i
++)
903 pScriptTags
[i
] = psc
->scripts
[i
].tag
;
907 if (searchingFor
== psc
->scripts
[i
].tag
)
909 pScriptTags
[0] = psc
->scripts
[i
].tag
;
912 *script_table
= psc
->scripts
[i
].table
;
921 static void GSUB_initialize_language_cache(LoadedScript
*script
)
925 if (!script
->language_count
)
928 const GSUB_Script
* table
= script
->table
;
929 script
->language_count
= GET_BE_WORD(table
->LangSysCount
);
930 offset
= GET_BE_WORD(table
->DefaultLangSys
);
933 script
->default_language
.tag
= MS_MAKE_TAG('d','f','l','t');
934 script
->default_language
.table
= (const BYTE
*)table
+ offset
;
937 if (script
->language_count
)
939 TRACE("Deflang %p, LangCount %i\n",script
->default_language
.table
, script
->language_count
);
941 script
->languages
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LoadedLanguage
) * script
->language_count
);
943 for (i
= 0; i
< script
->language_count
; i
++)
945 int offset
= GET_BE_WORD(table
->LangSysRecord
[i
].LangSys
);
946 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]);
947 script
->languages
[i
].table
= ((const BYTE
*)table
+ offset
);
953 HRESULT
OpenType_GSUB_GetFontLanguageTags(ScriptCache
*psc
, OPENTYPE_TAG script_tag
, OPENTYPE_TAG searchingFor
, int cMaxTags
, OPENTYPE_TAG
*pLanguageTags
, int *pcTags
, LPCVOID
* language_table
)
957 LoadedScript
*script
= NULL
;
959 GSUB_initialize_script_cache(psc
);
961 for (i
= 0; i
< psc
->script_count
; i
++)
963 if (psc
->scripts
[i
].tag
== script_tag
)
965 script
= &psc
->scripts
[i
];
973 GSUB_initialize_language_cache(script
);
975 if (!searchingFor
&& cMaxTags
< script
->language_count
)
977 else if (searchingFor
)
980 *pcTags
= script
->language_count
;
982 for (i
= 0; i
< script
->language_count
; i
++)
985 pLanguageTags
[i
] = script
->languages
[i
].tag
;
989 if (searchingFor
== script
->languages
[i
].tag
)
991 pLanguageTags
[0] = script
->languages
[i
].tag
;
994 *language_table
= script
->languages
[i
].table
;
1001 if (script
->default_language
.table
)
1004 pLanguageTags
[i
] = script
->default_language
.tag
;
1006 if (searchingFor
&& FAILED(rc
))
1008 pLanguageTags
[0] = script
->default_language
.tag
;
1010 *language_table
= script
->default_language
.table
;
1013 *pcTags
= (*pcTags
) + 1;
1020 static void GSUB_initialize_feature_cache(LPCVOID table
, LoadedLanguage
*language
)
1024 if (!language
->feature_count
)
1026 const GSUB_LangSys
*lang
= language
->table
;
1027 const GSUB_Header
*header
= (const GSUB_Header
*)table
;
1028 const GSUB_FeatureList
*feature_list
;
1030 language
->feature_count
= GET_BE_WORD(lang
->FeatureCount
);
1031 TRACE("%i features\n",language
->feature_count
);
1033 if (language
->feature_count
)
1035 language
->features
= HeapAlloc(GetProcessHeap(),0,sizeof(LoadedFeature
)*language
->feature_count
);
1037 feature_list
= (const GSUB_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
1039 for (i
= 0; i
< language
->feature_count
; i
++)
1041 const GSUB_Feature
*feature
;
1043 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
1045 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]);
1046 language
->features
[i
].feature
= ((const BYTE
*)feature_list
+ GET_BE_WORD(feature_list
->FeatureRecord
[index
].Feature
));
1047 feature
= (const GSUB_Feature
*)language
->features
[i
].feature
;
1048 language
->features
[i
].lookup_count
= GET_BE_WORD(feature
->LookupCount
);
1049 language
->features
[i
].lookups
= HeapAlloc(GetProcessHeap(),0,sizeof(WORD
) * language
->features
[i
].lookup_count
);
1050 for (j
= 0; j
< language
->features
[i
].lookup_count
; j
++)
1051 language
->features
[i
].lookups
[j
] = GET_BE_WORD(feature
->LookupListIndex
[j
]);
1057 HRESULT
OpenType_GSUB_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
)
1061 LoadedScript
*script
= NULL
;
1062 LoadedLanguage
*language
= NULL
;
1064 GSUB_initialize_script_cache(psc
);
1066 for (i
= 0; i
< psc
->script_count
; i
++)
1068 if (psc
->scripts
[i
].tag
== script_tag
)
1070 script
= &psc
->scripts
[i
];
1081 return E_INVALIDARG
;
1084 GSUB_initialize_language_cache(script
);
1086 if (script
->default_language
.table
&& script
->default_language
.tag
== language_tag
)
1087 language
= &script
->default_language
;
1090 for (i
= 0; i
< script
->language_count
; i
++)
1092 if (script
->languages
[i
].tag
== language_tag
)
1094 language
= &script
->languages
[i
];
1106 GSUB_initialize_feature_cache(psc
->GSUB_Table
, language
);
1108 *pcTags
= language
->feature_count
;
1110 if (!searchingFor
&& cMaxTags
< *pcTags
)
1112 else if (searchingFor
)
1115 for (i
= 0; i
< language
->feature_count
; i
++)
1118 pFeatureTags
[i
] = language
->features
[i
].tag
;
1122 if (searchingFor
== language
->features
[i
].tag
)
1124 pFeatureTags
[0] = language
->features
[i
].tag
;
1127 *feature
= &language
->features
[i
];