2 * Implementation of Shaping for the Uniscribe Script Processor (usp10.dll)
4 * Copyright 2010 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
31 #include "usp10_internal.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
37 #define FIRST_ARABIC_CHAR 0x0600
38 #define LAST_ARABIC_CHAR 0x06ff
40 typedef VOID (*ContextualShapingProc)(HDC, ScriptCache*, SCRIPT_ANALYSIS*,
41 WCHAR*, INT, WORD*, INT*, INT, WORD*);
43 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
44 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
45 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust);
47 extern const unsigned short wine_shaping_table[];
48 extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
70 #ifdef WORDS_BIGENDIAN
71 #define GET_BE_WORD(x) (x)
73 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
76 /* These are all structures needed for the GSUB table */
77 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
78 #define GSUB_E_NOFEATURE -2
79 #define GSUB_E_NOGLYPH -1
95 GSUB_ScriptRecord ScriptRecord[1];
101 } GSUB_LangSysRecord;
106 GSUB_LangSysRecord LangSysRecord[1];
110 WORD LookupOrder; /* Reserved */
111 WORD ReqFeatureIndex;
113 WORD FeatureIndex[1];
119 } GSUB_FeatureRecord;
123 GSUB_FeatureRecord FeatureRecord[1];
127 WORD FeatureParams; /* Reserved */
129 WORD LookupListIndex[1];
148 } GSUB_CoverageFormat1;
153 WORD StartCoverageIndex;
159 GSUB_RangeRecord RangeRecord[1];
160 } GSUB_CoverageFormat2;
163 WORD SubstFormat; /* = 1 */
166 } GSUB_SingleSubstFormat1;
169 WORD SubstFormat; /* = 2 */
173 }GSUB_SingleSubstFormat2;
176 WORD SubstFormat; /* = 1 */
180 }GSUB_LigatureSubstFormat1;
195 WORD LookupListIndex;
197 }GSUB_SubstLookupRecord;
200 WORD SubstFormat; /* = 1 */
202 WORD ChainSubRuleSetCount;
203 WORD ChainSubRuleSet[1];
204 }GSUB_ChainContextSubstFormat1;
207 WORD SubstFormat; /* = 3 */
208 WORD BacktrackGlyphCount;
210 }GSUB_ChainContextSubstFormat3_1;
213 WORD InputGlyphCount;
215 }GSUB_ChainContextSubstFormat3_2;
218 WORD LookaheadGlyphCount;
220 }GSUB_ChainContextSubstFormat3_3;
224 GSUB_SubstLookupRecord SubstLookupRecord[1];
225 }GSUB_ChainContextSubstFormat3_4;
228 WORD SubstFormat; /* = 1 */
230 WORD AlternateSetCount;
231 WORD AlternateSet[1];
232 } GSUB_AlternateSubstFormat1;
239 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
241 /* the orders of joined_forms and contextual_features need to line up */
242 static const char* contextual_features[] =
254 static OPENTYPE_FEATURE_RECORD standard_features[] =
256 { MS_MAKE_TAG('l','i','g','a'), 1},
257 { MS_MAKE_TAG('c','l','i','g'), 1},
260 static OPENTYPE_FEATURE_RECORD arabic_features[] =
262 { MS_MAKE_TAG('r','l','i','g'), 1},
263 { MS_MAKE_TAG('c','a','l','t'), 1},
264 { MS_MAKE_TAG('l','i','g','a'), 1},
265 { MS_MAKE_TAG('d','l','i','g'), 1},
266 { MS_MAKE_TAG('c','s','w','h'), 1},
267 { MS_MAKE_TAG('m','s','e','t'), 1},
270 static const char* required_arabic_features[] =
279 static OPENTYPE_FEATURE_RECORD hebrew_features[] =
281 { MS_MAKE_TAG('d','l','i','g'), 1},
284 static OPENTYPE_FEATURE_RECORD syriac_features[] =
286 { MS_MAKE_TAG('r','l','i','g'), 1},
287 { MS_MAKE_TAG('c','a','l','t'), 1},
288 { MS_MAKE_TAG('l','i','g','a'), 1},
289 { MS_MAKE_TAG('d','l','i','g'), 1},
292 static const char* required_syriac_features[] =
304 static OPENTYPE_FEATURE_RECORD sinhala_features[] =
307 { MS_MAKE_TAG('a','k','h','n'), 1},
308 { MS_MAKE_TAG('r','p','h','f'), 1},
309 { MS_MAKE_TAG('v','a','t','u'), 1},
310 { MS_MAKE_TAG('p','s','t','f'), 1},
311 /* Presentation forms */
312 { MS_MAKE_TAG('b','l','w','s'), 1},
313 { MS_MAKE_TAG('a','b','v','s'), 1},
314 { MS_MAKE_TAG('p','s','t','s'), 1},
317 static OPENTYPE_FEATURE_RECORD tibetan_features[] =
319 { MS_MAKE_TAG('a','b','v','s'), 1},
320 { MS_MAKE_TAG('b','l','w','s'), 1},
323 static OPENTYPE_FEATURE_RECORD thai_features[] =
325 { MS_MAKE_TAG('c','c','m','p'), 1},
328 static const char* required_lao_features[] =
334 typedef struct ScriptShapeDataTag {
335 TEXTRANGE_PROPERTIES defaultTextRange;
336 const char** requiredFeatures;
338 ContextualShapingProc contextProc;
341 /* in order of scripts */
342 static const ScriptShapeData ShapingData[] =
344 {{ standard_features, 2}, NULL, "", NULL},
345 {{ standard_features, 2}, NULL, "latn", NULL},
346 {{ standard_features, 2}, NULL, "latn", NULL},
347 {{ standard_features, 2}, NULL, "latn", NULL},
348 {{ standard_features, 2}, NULL, "" , NULL},
349 {{ standard_features, 2}, NULL, "latn", NULL},
350 {{ arabic_features, 6}, required_arabic_features, "arab", ContextualShape_Arabic},
351 {{ arabic_features, 6}, required_arabic_features, "arab", ContextualShape_Arabic},
352 {{ hebrew_features, 1}, NULL, "hebr", NULL},
353 {{ syriac_features, 4}, required_syriac_features, "syrc", ContextualShape_Syriac},
354 {{ arabic_features, 6}, required_arabic_features, "arab", ContextualShape_Arabic},
355 {{ NULL, 0}, NULL, "thaa", NULL},
356 {{ standard_features, 2}, NULL, "grek", NULL},
357 {{ standard_features, 2}, NULL, "cyrl", NULL},
358 {{ standard_features, 2}, NULL, "armn", NULL},
359 {{ standard_features, 2}, NULL, "geor", NULL},
360 {{ sinhala_features, 7}, NULL, "sinh", NULL},
361 {{ tibetan_features, 2}, NULL, "tibt", NULL},
362 {{ tibetan_features, 2}, NULL, "tibt", NULL},
363 {{ tibetan_features, 2}, NULL, "phag", ContextualShape_Phags_pa},
364 {{ thai_features, 1}, NULL, "thai", NULL},
365 {{ thai_features, 1}, NULL, "thai", NULL},
366 {{ thai_features, 1}, required_lao_features, "lao", NULL},
367 {{ thai_features, 1}, required_lao_features, "lao", NULL},
370 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
372 const GSUB_CoverageFormat1* cf1;
376 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
378 int count = GET_BE_WORD(cf1->GlyphCount);
380 TRACE("Coverage Format 1, %i glyphs\n",count);
381 for (i = 0; i < count; i++)
382 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
386 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
388 const GSUB_CoverageFormat2* cf2;
391 cf2 = (const GSUB_CoverageFormat2*)cf1;
393 count = GET_BE_WORD(cf2->RangeCount);
394 TRACE("Coverage Format 2, %i ranges\n",count);
395 for (i = 0; i < count; i++)
397 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
399 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
400 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
402 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
403 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
409 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
414 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
416 const GSUB_ScriptList *script;
417 const GSUB_Script *deflt = NULL;
419 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
421 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
422 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
424 const GSUB_Script *scr;
427 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
428 scr = (const GSUB_Script*)((const BYTE*)script + offset);
430 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
432 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
438 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
442 const GSUB_LangSys *Lang;
444 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
446 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
448 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
449 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
451 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
454 offset = GET_BE_WORD(script->DefaultLangSys);
457 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
463 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
466 const GSUB_FeatureList *feature;
467 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
469 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
470 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
472 int index = GET_BE_WORD(lang->FeatureIndex[i]);
473 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
475 const GSUB_Feature *feat;
476 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
483 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
486 TRACE("Single Substitution Subtable\n");
488 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
491 const GSUB_SingleSubstFormat1 *ssf1;
492 offset = GET_BE_WORD(look->SubTable[j]);
493 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
494 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
496 int offset = GET_BE_WORD(ssf1->Coverage);
497 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
498 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
500 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
501 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
502 TRACE(" 0x%x\n",glyphs[glyph_index]);
503 return glyph_index + 1;
508 const GSUB_SingleSubstFormat2 *ssf2;
512 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
513 offset = GET_BE_WORD(ssf1->Coverage);
514 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
515 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
516 TRACE(" Coverage index %i\n",index);
519 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
520 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
521 TRACE("0x%x\n",glyphs[glyph_index]);
522 return glyph_index + 1;
526 return GSUB_E_NOGLYPH;
529 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
532 TRACE("Alternate Substitution Subtable\n");
534 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
537 const GSUB_AlternateSubstFormat1 *asf1;
540 offset = GET_BE_WORD(look->SubTable[j]);
541 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
542 offset = GET_BE_WORD(asf1->Coverage);
544 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
547 const GSUB_AlternateSet *as;
548 offset = GET_BE_WORD(asf1->AlternateSet[index]);
549 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
550 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
552 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
553 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
554 TRACE(" 0x%x\n",glyphs[glyph_index]);
555 return glyph_index + 1;
558 return GSUB_E_NOGLYPH;
561 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
565 TRACE("Ligature Substitution Subtable\n");
566 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
568 const GSUB_LigatureSubstFormat1 *lsf1;
571 offset = GET_BE_WORD(look->SubTable[j]);
572 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
573 offset = GET_BE_WORD(lsf1->Coverage);
574 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
575 TRACE(" Coverage index %i\n",index);
578 const GSUB_LigatureSet *ls;
581 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
582 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
583 count = GET_BE_WORD(ls->LigatureCount);
584 TRACE(" LigatureSet has %i members\n",count);
585 for (k = 0; k < count; k++)
587 const GSUB_Ligature *lig;
588 int CompCount,l,CompIndex;
590 offset = GET_BE_WORD(ls->Ligature[k]);
591 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
592 CompCount = GET_BE_WORD(lig->CompCount) - 1;
593 CompIndex = glyph_index+write_dir;
594 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
597 CompGlyph = GET_BE_WORD(lig->Component[l]);
598 if (CompGlyph != glyphs[CompIndex])
600 CompIndex += write_dir;
604 int replaceIdx = glyph_index;
606 replaceIdx = glyph_index - CompCount;
608 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
609 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
610 TRACE("0x%x\n",glyphs[replaceIdx]);
614 for (j = replaceIdx + 1; j < *glyph_count; j++)
615 glyphs[j] =glyphs[j+CompCount];
616 *glyph_count = *glyph_count - CompCount;
618 return replaceIdx + 1;
623 return GSUB_E_NOGLYPH;
626 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
631 TRACE("Chaining Contextual Substitution Subtable\n");
632 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
634 const GSUB_ChainContextSubstFormat1 *ccsf1;
636 int dirLookahead = write_dir;
637 int dirBacktrack = -1 * write_dir;
639 offset = GET_BE_WORD(look->SubTable[j]);
640 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
641 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
643 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
646 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
648 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
651 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
655 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
656 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
657 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
658 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
659 int newIndex = glyph_index;
661 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
663 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
665 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
667 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
668 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
671 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
673 TRACE("Matched Backtrack\n");
675 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
677 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
678 for (k = 0; k < indexGlyphs; k++)
680 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
681 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
684 if (k != indexGlyphs)
686 TRACE("Matched IndexGlyphs\n");
688 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
690 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
692 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
693 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k))]) == -1)
696 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
698 TRACE("Matched LookAhead\n");
700 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
702 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
704 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
705 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
707 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
708 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
711 ERR("Chain failed to generate a glyph\n");
721 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
724 const GSUB_LookupTable *look;
726 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
727 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
728 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
729 switch(GET_BE_WORD(look->LookupType))
732 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
734 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
736 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
738 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
740 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
742 return GSUB_E_NOGLYPH;
745 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
748 int out_index = GSUB_E_NOGLYPH;
749 const GSUB_LookupList *lookup;
751 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
753 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
754 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
756 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
757 if (out_index != GSUB_E_NOGLYPH)
760 if (out_index == GSUB_E_NOGLYPH)
761 TRACE("lookups found no glyphs\n");
765 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc)
769 if (psc->userScript != 0)
770 return (char*)&psc->userScript;
772 if (ShapingData[psa->eScript].otTag[0] != 0)
773 return ShapingData[psa->eScript].otTag;
776 * fall back to the font charset
778 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
781 case ANSI_CHARSET: return "latn";
782 case BALTIC_CHARSET: return "latn"; /* ?? */
783 case CHINESEBIG5_CHARSET: return "hani";
784 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
785 case GB2312_CHARSET: return "hani";
786 case GREEK_CHARSET: return "grek";
787 case HANGUL_CHARSET: return "hang";
788 case RUSSIAN_CHARSET: return "cyrl";
789 case SHIFTJIS_CHARSET: return "kana";
790 case TURKISH_CHARSET: return "latn"; /* ?? */
791 case VIETNAMESE_CHARSET: return "latn";
792 case JOHAB_CHARSET: return "latn"; /* ?? */
793 case ARABIC_CHARSET: return "arab";
794 case HEBREW_CHARSET: return "hebr";
795 case THAI_CHARSET: return "thai";
796 default: return "latn";
800 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
802 const GSUB_Feature *feature;
805 for (i = 0; i < psc->feature_count; i++)
806 if (strncmp(psc->features[i].tag,feat,4)==0)
807 return psc->features[i].feature;
813 const GSUB_Script *script;
814 const GSUB_LangSys *language;
816 script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa,psc));
819 if (psc->userLang != 0)
820 language = GSUB_get_lang_table(script,(char*)&psc->userLang);
822 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
824 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
827 /* try in the default (latin) table */
830 script = GSUB_get_script_table(psc->GSUB_Table, "latn");
833 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
835 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
840 TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
842 psc->feature_count++;
845 psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
847 psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
849 lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
850 psc->features[psc->feature_count - 1].feature = feature;
854 static INT apply_GSUB_feature_to_glyph(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, WORD *glyphs, INT index, INT write_dir, INT* glyph_count, const char* feat)
856 const GSUB_Feature *feature;
858 feature = load_GSUB_feature(hdc, psa, psc, feat);
860 return GSUB_E_NOFEATURE;
862 TRACE("applying feature %s\n",feat);
863 return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
866 static VOID *load_gsub_table(HDC hdc)
868 VOID* GSUB_Table = NULL;
869 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
870 if (length != GDI_ERROR)
872 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
873 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
874 TRACE("Loaded GSUB table of %i bytes\n",length);
879 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
881 if (changeCount == 0)
886 int target_glyph = nextIndex - 1;
887 int target_index = -1;
888 int replacing_glyph = -1;
892 for (i = 0; i < chars; i++)
894 if (pwLogClust[i] == target_glyph)
901 for (i = chars - 1; i >= 0; i--)
903 if (pwLogClust[i] == target_glyph)
909 if (target_index == -1)
911 ERR("Unable to find target glyph\n");
918 for(i = target_index; i < chars && i >= 0; i+=write_dir)
920 if (pwLogClust[i] == target_glyph)
922 if(pwLogClust[i] == replacing_glyph)
923 pwLogClust[i] = target_glyph;
927 if (changed >= changeCount)
929 replacing_glyph = pwLogClust[i];
930 pwLogClust[i] = target_glyph;
938 /* renumber trailing indexes*/
939 for(i = target_index; i < chars && i >= 0; i+=write_dir)
941 if (pwLogClust[i] != target_glyph)
942 pwLogClust[i] += changeCount;
947 static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, WORD *pwOutGlyphs, int write_dir, INT* pcGlyphs, INT cChars, const char* feat, WORD *pwLogClust )
953 const GSUB_Feature *feature;
955 feature = load_GSUB_feature(hdc, psa, psc, feat);
957 return GSUB_E_NOFEATURE;
960 TRACE("applying feature %s\n",debugstr_an(feat,4));
964 INT prevCount = *pcGlyphs;
965 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
966 if (nextIndex > GSUB_E_NOGLYPH)
968 UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
976 return GSUB_E_NOFEATURE;
979 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
983 if ( i+ delta >= cchLen)
991 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
995 if (psa->fLinkBefore)
1000 if ( i+ delta >= cchLen)
1002 if (psa->fLinkAfter)
1010 if (context_type[i] == jtT)
1011 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
1013 return context_type[i];
1016 static inline BOOL right_join_causing(CHAR joining_type)
1018 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
1021 static inline BOOL left_join_causing(CHAR joining_type)
1023 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
1026 static inline BOOL word_break_causing(WCHAR chr)
1028 /* we are working within a string of characters already guareented to
1029 be within one script, Syriac, so we do not worry about any characers
1030 other than the space character outside of that range */
1031 return (chr == 0 || chr == 0x20 );
1035 * ContextualShape_Arabic
1037 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1044 if (*pcGlyphs != cChars)
1046 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1050 if (!psa->fLogicalOrder && psa->fRTL)
1061 if (!psc->GSUB_Table)
1062 psc->GSUB_Table = load_gsub_table(hdc);
1064 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1065 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1067 for (i = 0; i < cChars; i++)
1068 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1070 for (i = 0; i < cChars; i++)
1072 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1073 context_shape[i] = Xr;
1074 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1075 context_shape[i] = Xl;
1076 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)) && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1077 context_shape[i] = Xm;
1078 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1079 context_shape[i] = Xr;
1080 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1081 context_shape[i] = Xl;
1083 context_shape[i] = Xn;
1086 /* Contextual Shaping */
1088 while(i < *pcGlyphs)
1090 BOOL shaped = FALSE;
1092 if (psc->GSUB_Table)
1095 INT prevCount = *pcGlyphs;
1096 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1097 if (nextIndex > GSUB_E_NOGLYPH)
1100 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1102 shaped = (nextIndex > GSUB_E_NOGLYPH);
1107 WORD newGlyph = pwOutGlyphs[i];
1108 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1110 /* fall back to presentation form B */
1111 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1112 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1113 pwOutGlyphs[i] = newGlyph;
1119 HeapFree(GetProcessHeap(),0,context_shape);
1120 HeapFree(GetProcessHeap(),0,context_type);
1124 * ContextualShape_Syriac
1128 #define DALATH 0x715
1131 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1138 if (*pcGlyphs != cChars)
1140 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1144 if (!psa->fLogicalOrder && psa->fRTL)
1155 if (!psc->GSUB_Table)
1156 psc->GSUB_Table = load_gsub_table(hdc);
1158 if (!psc->GSUB_Table)
1161 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1162 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1164 for (i = 0; i < cChars; i++)
1165 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1167 for (i = 0; i < cChars; i++)
1169 if (pwcChars[i] == ALAPH)
1171 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1173 if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1174 context_shape[i] = Afj;
1175 else if ( rchar != DALATH && rchar != RISH &&
1176 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1177 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1178 context_shape[i] = Afn;
1179 else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1180 context_shape[i] = Afx;
1182 context_shape[i] = Xn;
1184 else if (context_type[i] == jtR &&
1185 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1186 context_shape[i] = Xr;
1187 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1188 context_shape[i] = Xl;
1189 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)) && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1190 context_shape[i] = Xm;
1191 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1192 context_shape[i] = Xr;
1193 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1194 context_shape[i] = Xl;
1196 context_shape[i] = Xn;
1199 /* Contextual Shaping */
1201 while(i < *pcGlyphs)
1204 INT prevCount = *pcGlyphs;
1205 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1206 if (nextIndex > GSUB_E_NOGLYPH)
1208 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1215 HeapFree(GetProcessHeap(),0,context_shape);
1216 HeapFree(GetProcessHeap(),0,context_type);
1220 * ContextualShape_Phags_pa
1223 #define phags_pa_CANDRABINDU 0xA873
1224 #define phags_pa_START 0xA840
1225 #define phags_pa_END 0xA87F
1227 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1233 if (*pcGlyphs != cChars)
1235 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1239 if (!psa->fLogicalOrder && psa->fRTL)
1250 if (!psc->GSUB_Table)
1251 psc->GSUB_Table = load_gsub_table(hdc);
1253 if (!psc->GSUB_Table)
1256 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1258 for (i = 0; i < cChars; i++)
1260 if (pwcChars[i] >= phags_pa_START && pwcChars[i] <= phags_pa_END)
1262 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1263 WCHAR lchar = neighbour_char(i,dirL,pwcChars,cChars);
1264 BOOL jrchar = (rchar != phags_pa_CANDRABINDU && rchar >= phags_pa_START && rchar <= phags_pa_END);
1265 BOOL jlchar = (lchar != phags_pa_CANDRABINDU && lchar >= phags_pa_START && lchar <= phags_pa_END);
1267 if (jrchar && jlchar)
1268 context_shape[i] = Xm;
1270 context_shape[i] = Xr;
1272 context_shape[i] = Xl;
1274 context_shape[i] = Xn;
1277 context_shape[i] = -1;
1280 /* Contextual Shaping */
1282 while(i < *pcGlyphs)
1284 if (context_shape[i] >= 0)
1287 INT prevCount = *pcGlyphs;
1288 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1289 if (nextIndex > GSUB_E_NOGLYPH)
1291 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1301 HeapFree(GetProcessHeap(),0,context_shape);
1304 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1306 if (ShapingData[psa->eScript].contextProc)
1307 ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1310 static void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1315 if (!rpRangeProperties)
1318 if (!psc->GSUB_Table)
1319 psc->GSUB_Table = load_gsub_table(hdc);
1321 if (!psc->GSUB_Table)
1324 if (!psa->fLogicalOrder && psa->fRTL)
1329 for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1331 if (rpRangeProperties->potfRecords[i].lParameter > 0)
1332 apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1336 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1338 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1339 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1341 SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);
1344 HRESULT SHAPE_CheckFontForRequiredFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa)
1346 const GSUB_Feature *feature;
1349 if (!ShapingData[psa->eScript].requiredFeatures)
1352 if (!psc->GSUB_Table)
1353 psc->GSUB_Table = load_gsub_table(hdc);
1355 /* we need to have at least one of the required features */
1357 while (ShapingData[psa->eScript].requiredFeatures[i])
1359 feature = load_GSUB_feature(hdc, psa, psc, ShapingData[psa->eScript].requiredFeatures[i]);
1365 return USP_E_SCRIPT_NOT_IN_FONT;