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 MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
78 ( ( (ULONG)_x4 << 24 ) | \
79 ( (ULONG)_x3 << 16 ) | \
80 ( (ULONG)_x2 << 8 ) | \
83 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
84 #define GSUB_E_NOFEATURE -2
85 #define GSUB_E_NOGLYPH -1
101 GSUB_ScriptRecord ScriptRecord[1];
107 } GSUB_LangSysRecord;
112 GSUB_LangSysRecord LangSysRecord[1];
116 WORD LookupOrder; /* Reserved */
117 WORD ReqFeatureIndex;
119 WORD FeatureIndex[1];
125 } GSUB_FeatureRecord;
129 GSUB_FeatureRecord FeatureRecord[1];
133 WORD FeatureParams; /* Reserved */
135 WORD LookupListIndex[1];
154 } GSUB_CoverageFormat1;
159 WORD StartCoverageIndex;
165 GSUB_RangeRecord RangeRecord[1];
166 } GSUB_CoverageFormat2;
169 WORD SubstFormat; /* = 1 */
172 } GSUB_SingleSubstFormat1;
175 WORD SubstFormat; /* = 2 */
179 }GSUB_SingleSubstFormat2;
182 WORD SubstFormat; /* = 1 */
186 }GSUB_LigatureSubstFormat1;
201 WORD LookupListIndex;
203 }GSUB_SubstLookupRecord;
206 WORD SubstFormat; /* = 1 */
208 WORD ChainSubRuleSetCount;
209 WORD ChainSubRuleSet[1];
210 }GSUB_ChainContextSubstFormat1;
213 WORD SubstFormat; /* = 3 */
214 WORD BacktrackGlyphCount;
216 }GSUB_ChainContextSubstFormat3_1;
219 WORD InputGlyphCount;
221 }GSUB_ChainContextSubstFormat3_2;
224 WORD LookaheadGlyphCount;
226 }GSUB_ChainContextSubstFormat3_3;
230 GSUB_SubstLookupRecord SubstLookupRecord[1];
231 }GSUB_ChainContextSubstFormat3_4;
234 WORD SubstFormat; /* = 1 */
236 WORD AlternateSetCount;
237 WORD AlternateSet[1];
238 } GSUB_AlternateSubstFormat1;
245 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
247 /* the orders of joined_forms and contextual_features need to line up */
248 static const char* contextual_features[] =
260 static OPENTYPE_FEATURE_RECORD standard_features[] =
262 { 0x6167696c /*liga*/, 1},
263 { 0x67696c63 /*clig*/, 1},
266 static OPENTYPE_FEATURE_RECORD arabic_features[] =
268 { 0x67696c72 /*rlig*/, 1},
269 { 0x746c6163 /*calt*/, 1},
270 { 0x6167696c /*liga*/, 1},
271 { 0x67696c64 /*dlig*/, 1},
272 { 0x68777363 /*cswh*/, 1},
273 { 0x7465736d /*mset*/, 1},
276 static OPENTYPE_FEATURE_RECORD hebrew_features[] =
278 { 0x67696c64 /*dlig*/, 1},
281 static OPENTYPE_FEATURE_RECORD syriac_features[] =
283 { 0x67696c72 /*rlig*/, 1},
284 { 0x746c6163 /*calt*/, 1},
285 { 0x6167696c /*liga*/, 1},
286 { 0x67696c64 /*dlig*/, 1},
289 static OPENTYPE_FEATURE_RECORD sinhala_features[] =
292 { 0x6e686b61 /*akhn*/, 1},
293 { 0x66687072 /*rphf*/, 1},
294 { 0x75746176 /*vatu*/, 1},
295 { 0x66747370 /*pstf*/, 1},
296 /* Presentation forms */
297 { 0x73776c62 /*blws*/, 1},
298 { 0x73766261 /*abvs*/, 1},
299 { 0x73747370 /*psts*/, 1},
302 static OPENTYPE_FEATURE_RECORD tibetan_features[] =
304 { 0x73766261 /*abvs*/, 1},
305 { 0x73776c62 /*blws*/, 1},
308 typedef struct ScriptShapeDataTag {
309 TEXTRANGE_PROPERTIES defaultTextRange;
311 ContextualShapingProc contextProc;
314 /* in order of scripts */
315 static const ScriptShapeData ShapingData[] =
317 {{ standard_features, 2}, "", NULL},
318 {{ standard_features, 2}, "latn", NULL},
319 {{ standard_features, 2}, "latn", NULL},
320 {{ standard_features, 2}, "latn", NULL},
321 {{ standard_features, 2}, "" , NULL},
322 {{ standard_features, 2}, "latn", NULL},
323 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
324 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
325 {{ hebrew_features, 1}, "hebr", NULL},
326 {{ syriac_features, 4}, "syrc", ContextualShape_Syriac},
327 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
328 {{ NULL, 0}, "thaa", NULL},
329 {{ standard_features, 2}, "grek", NULL},
330 {{ standard_features, 2}, "cyrl", NULL},
331 {{ standard_features, 2}, "armn", NULL},
332 {{ standard_features, 2}, "geor", NULL},
333 {{ sinhala_features, 7}, "sinh", NULL},
334 {{ tibetan_features, 2}, "tibt", NULL},
335 {{ tibetan_features, 2}, "tibt", NULL},
336 {{ tibetan_features, 2}, "phag", ContextualShape_Phags_pa},
339 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
341 const GSUB_CoverageFormat1* cf1;
345 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
347 int count = GET_BE_WORD(cf1->GlyphCount);
349 TRACE("Coverage Format 1, %i glyphs\n",count);
350 for (i = 0; i < count; i++)
351 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
355 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
357 const GSUB_CoverageFormat2* cf2;
360 cf2 = (const GSUB_CoverageFormat2*)cf1;
362 count = GET_BE_WORD(cf2->RangeCount);
363 TRACE("Coverage Format 2, %i ranges\n",count);
364 for (i = 0; i < count; i++)
366 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
368 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
369 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
371 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
372 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
378 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
383 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
385 const GSUB_ScriptList *script;
386 const GSUB_Script *deflt = NULL;
388 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
390 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
391 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
393 const GSUB_Script *scr;
396 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
397 scr = (const GSUB_Script*)((const BYTE*)script + offset);
399 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
401 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
407 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
411 const GSUB_LangSys *Lang;
413 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
415 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
417 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
418 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
420 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
423 offset = GET_BE_WORD(script->DefaultLangSys);
426 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
432 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
435 const GSUB_FeatureList *feature;
436 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
438 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
439 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
441 int index = GET_BE_WORD(lang->FeatureIndex[i]);
442 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
444 const GSUB_Feature *feat;
445 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
452 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
455 TRACE("Single Substitution Subtable\n");
457 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
460 const GSUB_SingleSubstFormat1 *ssf1;
461 offset = GET_BE_WORD(look->SubTable[j]);
462 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
463 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
465 int offset = GET_BE_WORD(ssf1->Coverage);
466 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
467 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
469 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
470 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
471 TRACE(" 0x%x\n",glyphs[glyph_index]);
472 return glyph_index + 1;
477 const GSUB_SingleSubstFormat2 *ssf2;
481 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
482 offset = GET_BE_WORD(ssf1->Coverage);
483 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
484 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
485 TRACE(" Coverage index %i\n",index);
488 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
489 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
490 TRACE("0x%x\n",glyphs[glyph_index]);
491 return glyph_index + 1;
495 return GSUB_E_NOGLYPH;
498 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
501 TRACE("Alternate Substitution Subtable\n");
503 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
506 const GSUB_AlternateSubstFormat1 *asf1;
509 offset = GET_BE_WORD(look->SubTable[j]);
510 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
511 offset = GET_BE_WORD(asf1->Coverage);
513 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
516 const GSUB_AlternateSet *as;
517 offset = GET_BE_WORD(asf1->AlternateSet[index]);
518 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
519 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
521 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
522 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
523 TRACE(" 0x%x\n",glyphs[glyph_index]);
524 return glyph_index + 1;
527 return GSUB_E_NOGLYPH;
530 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
534 TRACE("Ligature Substitution Subtable\n");
535 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
537 const GSUB_LigatureSubstFormat1 *lsf1;
540 offset = GET_BE_WORD(look->SubTable[j]);
541 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
542 offset = GET_BE_WORD(lsf1->Coverage);
543 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
544 TRACE(" Coverage index %i\n",index);
547 const GSUB_LigatureSet *ls;
550 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
551 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
552 count = GET_BE_WORD(ls->LigatureCount);
553 TRACE(" LigatureSet has %i members\n",count);
554 for (k = 0; k < count; k++)
556 const GSUB_Ligature *lig;
557 int CompCount,l,CompIndex;
559 offset = GET_BE_WORD(ls->Ligature[k]);
560 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
561 CompCount = GET_BE_WORD(lig->CompCount) - 1;
562 CompIndex = glyph_index+write_dir;
563 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
566 CompGlyph = GET_BE_WORD(lig->Component[l]);
567 if (CompGlyph != glyphs[CompIndex])
569 CompIndex += write_dir;
573 int replaceIdx = glyph_index;
575 replaceIdx = glyph_index - CompCount;
577 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
578 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
579 TRACE("0x%x\n",glyphs[replaceIdx]);
583 for (j = replaceIdx + 1; j < *glyph_count; j++)
584 glyphs[j] =glyphs[j+CompCount];
585 *glyph_count = *glyph_count - CompCount;
587 return replaceIdx + 1;
592 return GSUB_E_NOGLYPH;
595 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
600 TRACE("Chaining Contextual Substitution Subtable\n");
601 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
603 const GSUB_ChainContextSubstFormat1 *ccsf1;
605 int dirLookahead = write_dir;
606 int dirBacktrack = -1 * write_dir;
608 offset = GET_BE_WORD(look->SubTable[j]);
609 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
610 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
612 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
615 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
617 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
620 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
624 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
625 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
626 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
627 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
628 int newIndex = glyph_index;
630 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
632 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
634 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
636 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
637 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
640 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
642 TRACE("Matched Backtrack\n");
644 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
646 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
647 for (k = 0; k < indexGlyphs; k++)
649 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
650 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
653 if (k != indexGlyphs)
655 TRACE("Matched IndexGlyphs\n");
657 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
659 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
661 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
662 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
665 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
667 TRACE("Matched LookAhead\n");
669 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
671 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
673 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
674 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
676 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
677 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
680 ERR("Chain failed to generate a glyph\n");
690 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
693 const GSUB_LookupTable *look;
695 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
696 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
697 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
698 switch(GET_BE_WORD(look->LookupType))
701 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
703 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
705 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
707 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
709 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
711 return GSUB_E_NOGLYPH;
714 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
717 int out_index = GSUB_E_NOGLYPH;
718 const GSUB_LookupList *lookup;
720 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
722 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
723 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
725 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
726 if (out_index != GSUB_E_NOGLYPH)
729 if (out_index == GSUB_E_NOGLYPH)
730 TRACE("lookups found no glyphs\n");
734 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
738 if (ShapingData[psa->eScript].otTag[0] != 0)
739 return ShapingData[psa->eScript].otTag;
742 * fall back to the font charset
744 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
747 case ANSI_CHARSET: return "latn";
748 case BALTIC_CHARSET: return "latn"; /* ?? */
749 case CHINESEBIG5_CHARSET: return "hani";
750 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
751 case GB2312_CHARSET: return "hani";
752 case GREEK_CHARSET: return "grek";
753 case HANGUL_CHARSET: return "hang";
754 case RUSSIAN_CHARSET: return "cyrl";
755 case SHIFTJIS_CHARSET: return "kana";
756 case TURKISH_CHARSET: return "latn"; /* ?? */
757 case VIETNAMESE_CHARSET: return "latn";
758 case JOHAB_CHARSET: return "latn"; /* ?? */
759 case ARABIC_CHARSET: return "arab";
760 case HEBREW_CHARSET: return "hebr";
761 case THAI_CHARSET: return "thai";
762 default: return "latn";
766 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
768 const GSUB_Feature *feature;
771 for (i = 0; i < psc->feature_count; i++)
772 if (strncmp(psc->features[i].tag,feat,4)==0)
773 return psc->features[i].feature;
779 const GSUB_Script *script;
780 const GSUB_LangSys *language;
782 script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa));
785 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
787 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
790 /* try in the default (latin) table */
793 script = GSUB_get_script_table(psc->GSUB_Table, "latn");
796 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
798 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
803 TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
805 psc->feature_count++;
808 psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
810 psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
812 lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
813 psc->features[psc->feature_count - 1].feature = feature;
817 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)
819 const GSUB_Feature *feature;
821 feature = load_GSUB_feature(hdc, psa, psc, feat);
823 return GSUB_E_NOFEATURE;
825 TRACE("applying feature %s\n",feat);
826 return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
829 static VOID *load_gsub_table(HDC hdc)
831 VOID* GSUB_Table = NULL;
832 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
833 if (length != GDI_ERROR)
835 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
836 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
837 TRACE("Loaded GSUB table of %i bytes\n",length);
842 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
844 if (changeCount == 0)
849 int target_glyph = nextIndex - 1;
850 int target_index = -1;
851 int replacing_glyph = -1;
855 for (i = 0; i < chars; i++)
857 if (pwLogClust[i] == target_glyph)
864 for (i = chars - 1; i >= 0; i--)
866 if (pwLogClust[i] == target_glyph)
872 if (target_index == -1)
874 ERR("Unable to find target glyph\n");
881 for(i = target_index; i < chars && i >= 0; i+=write_dir)
883 if (pwLogClust[i] == target_glyph)
885 if(pwLogClust[i] == replacing_glyph)
886 pwLogClust[i] = target_glyph;
890 if (changed >= changeCount)
892 replacing_glyph = pwLogClust[i];
893 pwLogClust[i] = target_glyph;
901 /* renumber trailing indexes*/
902 for(i = target_index; i < chars && i >= 0; i+=write_dir)
904 if (pwLogClust[i] != target_glyph)
905 pwLogClust[i] += changeCount;
910 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 )
916 const GSUB_Feature *feature;
918 feature = load_GSUB_feature(hdc, psa, psc, feat);
920 return GSUB_E_NOFEATURE;
923 TRACE("applying feature %s\n",debugstr_an(feat,4));
927 INT prevCount = *pcGlyphs;
928 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
929 if (nextIndex > GSUB_E_NOGLYPH)
931 UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
939 return GSUB_E_NOFEATURE;
942 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
946 if ( i+ delta >= cchLen)
954 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
958 if (psa->fLinkBefore)
963 if ( i+ delta >= cchLen)
973 if (context_type[i] == jtT)
974 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
976 return context_type[i];
979 static inline BOOL right_join_causing(CHAR joining_type)
981 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
984 static inline BOOL left_join_causing(CHAR joining_type)
986 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
989 static inline BOOL word_break_causing(WCHAR chr)
991 /* we are working within a string of characters already guareented to
992 be within one script, Syriac, so we do not worry about any characers
993 other than the space character outside of that range */
994 return (chr == 0 || chr == 0x20 );
998 * ContextualShape_Arabic
1000 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1007 if (*pcGlyphs != cChars)
1009 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1013 if (!psa->fLogicalOrder && psa->fRTL)
1024 if (!psc->GSUB_Table)
1025 psc->GSUB_Table = load_gsub_table(hdc);
1027 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1028 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1030 for (i = 0; i < cChars; i++)
1031 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1033 for (i = 0; i < cChars; i++)
1035 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1036 context_shape[i] = Xr;
1037 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1038 context_shape[i] = Xl;
1039 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)))
1040 context_shape[i] = Xm;
1041 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1042 context_shape[i] = Xr;
1043 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1044 context_shape[i] = Xl;
1046 context_shape[i] = Xn;
1049 /* Contextual Shaping */
1051 while(i < *pcGlyphs)
1053 BOOL shaped = FALSE;
1055 if (psc->GSUB_Table)
1058 INT prevCount = *pcGlyphs;
1059 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1060 if (nextIndex > GSUB_E_NOGLYPH)
1063 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1065 shaped = (nextIndex > GSUB_E_NOGLYPH);
1070 WORD newGlyph = pwOutGlyphs[i];
1071 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1073 /* fall back to presentation form B */
1074 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1075 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1076 pwOutGlyphs[i] = newGlyph;
1082 HeapFree(GetProcessHeap(),0,context_shape);
1083 HeapFree(GetProcessHeap(),0,context_type);
1087 * ContextualShape_Syriac
1091 #define DALATH 0x715
1094 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1101 if (*pcGlyphs != cChars)
1103 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1107 if (!psa->fLogicalOrder && psa->fRTL)
1118 if (!psc->GSUB_Table)
1119 psc->GSUB_Table = load_gsub_table(hdc);
1121 if (!psc->GSUB_Table)
1124 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1125 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1127 for (i = 0; i < cChars; i++)
1128 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1130 for (i = 0; i < cChars; i++)
1132 if (pwcChars[i] == ALAPH)
1134 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1136 if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1137 context_shape[i] = Afj;
1138 else if ( rchar != DALATH && rchar != RISH &&
1139 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1140 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1141 context_shape[i] = Afn;
1142 else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1143 context_shape[i] = Afx;
1145 else if (context_type[i] == jtR &&
1146 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1147 context_shape[i] = Xr;
1148 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1149 context_shape[i] = Xl;
1150 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)))
1151 context_shape[i] = Xm;
1152 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1153 context_shape[i] = Xr;
1154 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1155 context_shape[i] = Xl;
1157 context_shape[i] = Xn;
1160 /* Contextual Shaping */
1162 while(i < *pcGlyphs)
1165 INT prevCount = *pcGlyphs;
1166 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1167 if (nextIndex > GSUB_E_NOGLYPH)
1169 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1174 HeapFree(GetProcessHeap(),0,context_shape);
1175 HeapFree(GetProcessHeap(),0,context_type);
1179 * ContextualShape_Phags_pa
1182 #define phags_pa_CANDRABINDU 0xA873
1183 #define phags_pa_START 0xA840
1184 #define phags_pa_END 0xA87F
1186 static void ContextualShape_Phags_pa(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1192 if (*pcGlyphs != cChars)
1194 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1198 if (!psa->fLogicalOrder && psa->fRTL)
1209 if (!psc->GSUB_Table)
1210 psc->GSUB_Table = load_gsub_table(hdc);
1212 if (!psc->GSUB_Table)
1215 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1217 for (i = 0; i < cChars; i++)
1219 if (pwcChars[i] >= phags_pa_START && pwcChars[i] <= phags_pa_END)
1221 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1222 WCHAR lchar = neighbour_char(i,dirL,pwcChars,cChars);
1223 BOOL jrchar = (rchar != phags_pa_CANDRABINDU && rchar >= phags_pa_START && rchar <= phags_pa_END);
1224 BOOL jlchar = (lchar != phags_pa_CANDRABINDU && lchar >= phags_pa_START && lchar <= phags_pa_END);
1226 if (jrchar && jlchar)
1227 context_shape[i] = Xm;
1229 context_shape[i] = Xr;
1231 context_shape[i] = Xl;
1233 context_shape[i] = Xn;
1236 context_shape[i] = -1;
1239 /* Contextual Shaping */
1241 while(i < *pcGlyphs)
1243 if (context_shape[i] >= 0)
1246 INT prevCount = *pcGlyphs;
1247 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1248 if (nextIndex > GSUB_E_NOGLYPH)
1250 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1260 HeapFree(GetProcessHeap(),0,context_shape);
1263 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1265 if (ShapingData[psa->eScript].contextProc)
1266 ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1269 void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1274 if (!rpRangeProperties)
1277 if (!psc->GSUB_Table)
1278 psc->GSUB_Table = load_gsub_table(hdc);
1280 if (!psc->GSUB_Table)
1283 if (!psa->fLogicalOrder && psa->fRTL)
1288 for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1290 if (rpRangeProperties->potfRecords[i].lParameter > 0)
1291 apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1295 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1297 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1298 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1300 SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);