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);
46 extern const unsigned short wine_shaping_table[];
47 extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
69 #ifdef WORDS_BIGENDIAN
70 #define GET_BE_WORD(x) (x)
72 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
75 /* These are all structures needed for the GSUB table */
76 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
77 ( ( (ULONG)_x4 << 24 ) | \
78 ( (ULONG)_x3 << 16 ) | \
79 ( (ULONG)_x2 << 8 ) | \
82 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
83 #define GSUB_E_NOFEATURE -2
84 #define GSUB_E_NOGLYPH -1
100 GSUB_ScriptRecord ScriptRecord[1];
106 } GSUB_LangSysRecord;
111 GSUB_LangSysRecord LangSysRecord[1];
115 WORD LookupOrder; /* Reserved */
116 WORD ReqFeatureIndex;
118 WORD FeatureIndex[1];
124 } GSUB_FeatureRecord;
128 GSUB_FeatureRecord FeatureRecord[1];
132 WORD FeatureParams; /* Reserved */
134 WORD LookupListIndex[1];
153 } GSUB_CoverageFormat1;
158 WORD StartCoverageIndex;
164 GSUB_RangeRecord RangeRecord[1];
165 } GSUB_CoverageFormat2;
168 WORD SubstFormat; /* = 1 */
171 } GSUB_SingleSubstFormat1;
174 WORD SubstFormat; /* = 2 */
178 }GSUB_SingleSubstFormat2;
181 WORD SubstFormat; /* = 1 */
185 }GSUB_LigatureSubstFormat1;
200 WORD LookupListIndex;
202 }GSUB_SubstLookupRecord;
205 WORD SubstFormat; /* = 1 */
207 WORD ChainSubRuleSetCount;
208 WORD ChainSubRuleSet[1];
209 }GSUB_ChainContextSubstFormat1;
212 WORD SubstFormat; /* = 3 */
213 WORD BacktrackGlyphCount;
215 }GSUB_ChainContextSubstFormat3_1;
218 WORD InputGlyphCount;
220 }GSUB_ChainContextSubstFormat3_2;
223 WORD LookaheadGlyphCount;
225 }GSUB_ChainContextSubstFormat3_3;
229 GSUB_SubstLookupRecord SubstLookupRecord[1];
230 }GSUB_ChainContextSubstFormat3_4;
233 WORD SubstFormat; /* = 1 */
235 WORD AlternateSetCount;
236 WORD AlternateSet[1];
237 } GSUB_AlternateSubstFormat1;
244 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
246 /* the orders of joined_forms and contextual_features need to line up */
247 static const char* contextual_features[] =
259 static OPENTYPE_FEATURE_RECORD standard_features[] =
261 { 0x6167696c /*liga*/, 1},
262 { 0x67696c63 /*clig*/, 1},
265 static OPENTYPE_FEATURE_RECORD arabic_features[] =
267 { 0x67696c72 /*rlig*/, 1},
268 { 0x746c6163 /*calt*/, 1},
269 { 0x6167696c /*liga*/, 1},
270 { 0x67696c64 /*dlig*/, 1},
271 { 0x68777363 /*cswh*/, 1},
272 { 0x7465736d /*mset*/, 1},
275 static OPENTYPE_FEATURE_RECORD hebrew_features[] =
277 { 0x67696c64 /*dlig*/, 1},
280 static OPENTYPE_FEATURE_RECORD syriac_features[] =
282 { 0x67696c72 /*rlig*/, 1},
283 { 0x746c6163 /*calt*/, 1},
284 { 0x6167696c /*liga*/, 1},
285 { 0x67696c64 /*dlig*/, 1},
288 typedef struct ScriptShapeDataTag {
289 TEXTRANGE_PROPERTIES defaultTextRange;
291 ContextualShapingProc contextProc;
294 /* in order of scripts */
295 static const ScriptShapeData ShapingData[] =
297 {{ standard_features, 2}, "", NULL},
298 {{ standard_features, 2}, "latn", NULL},
299 {{ standard_features, 2}, "latn", NULL},
300 {{ standard_features, 2}, "latn", NULL},
301 {{ standard_features, 2}, "" , NULL},
302 {{ standard_features, 2}, "latn", NULL},
303 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
304 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
305 {{ hebrew_features, 1}, "hebr", NULL},
306 {{ syriac_features, 4}, "syrc", ContextualShape_Syriac},
307 {{ arabic_features, 6}, "arab", ContextualShape_Arabic},
308 {{ NULL, 0}, "thaa", NULL},
309 {{ standard_features, 2}, "grek", NULL},
310 {{ standard_features, 2}, "cyrl", NULL},
311 {{ standard_features, 2}, "armn", NULL},
312 {{ standard_features, 2}, "geor", NULL},
315 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
317 const GSUB_CoverageFormat1* cf1;
321 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
323 int count = GET_BE_WORD(cf1->GlyphCount);
325 TRACE("Coverage Format 1, %i glyphs\n",count);
326 for (i = 0; i < count; i++)
327 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
331 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
333 const GSUB_CoverageFormat2* cf2;
336 cf2 = (const GSUB_CoverageFormat2*)cf1;
338 count = GET_BE_WORD(cf2->RangeCount);
339 TRACE("Coverage Format 2, %i ranges\n",count);
340 for (i = 0; i < count; i++)
342 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
344 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
345 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
347 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
348 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
354 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
359 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
361 const GSUB_ScriptList *script;
362 const GSUB_Script *deflt = NULL;
364 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
366 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
367 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
369 const GSUB_Script *scr;
372 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
373 scr = (const GSUB_Script*)((const BYTE*)script + offset);
375 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
377 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
383 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
387 const GSUB_LangSys *Lang;
389 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
391 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
393 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
394 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
396 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
399 offset = GET_BE_WORD(script->DefaultLangSys);
402 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
408 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
411 const GSUB_FeatureList *feature;
412 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
414 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
415 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
417 int index = GET_BE_WORD(lang->FeatureIndex[i]);
418 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
420 const GSUB_Feature *feat;
421 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
428 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
431 TRACE("Single Substitution Subtable\n");
433 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
436 const GSUB_SingleSubstFormat1 *ssf1;
437 offset = GET_BE_WORD(look->SubTable[j]);
438 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
439 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
441 int offset = GET_BE_WORD(ssf1->Coverage);
442 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
443 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
445 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
446 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
447 TRACE(" 0x%x\n",glyphs[glyph_index]);
448 return glyph_index + 1;
453 const GSUB_SingleSubstFormat2 *ssf2;
457 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
458 offset = GET_BE_WORD(ssf1->Coverage);
459 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
460 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
461 TRACE(" Coverage index %i\n",index);
464 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
465 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
466 TRACE("0x%x\n",glyphs[glyph_index]);
467 return glyph_index + 1;
471 return GSUB_E_NOGLYPH;
474 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
477 TRACE("Alternate Substitution Subtable\n");
479 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
482 const GSUB_AlternateSubstFormat1 *asf1;
485 offset = GET_BE_WORD(look->SubTable[j]);
486 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
487 offset = GET_BE_WORD(asf1->Coverage);
489 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
492 const GSUB_AlternateSet *as;
493 offset = GET_BE_WORD(asf1->AlternateSet[index]);
494 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
495 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
497 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
498 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
499 TRACE(" 0x%x\n",glyphs[glyph_index]);
500 return glyph_index + 1;
503 return GSUB_E_NOGLYPH;
506 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
510 TRACE("Ligature Substitution Subtable\n");
511 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
513 const GSUB_LigatureSubstFormat1 *lsf1;
516 offset = GET_BE_WORD(look->SubTable[j]);
517 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
518 offset = GET_BE_WORD(lsf1->Coverage);
519 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
520 TRACE(" Coverage index %i\n",index);
523 const GSUB_LigatureSet *ls;
526 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
527 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
528 count = GET_BE_WORD(ls->LigatureCount);
529 TRACE(" LigatureSet has %i members\n",count);
530 for (k = 0; k < count; k++)
532 const GSUB_Ligature *lig;
533 int CompCount,l,CompIndex;
535 offset = GET_BE_WORD(ls->Ligature[k]);
536 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
537 CompCount = GET_BE_WORD(lig->CompCount) - 1;
538 CompIndex = glyph_index+write_dir;
539 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
542 CompGlyph = GET_BE_WORD(lig->Component[l]);
543 if (CompGlyph != glyphs[CompIndex])
545 CompIndex += write_dir;
549 int replaceIdx = glyph_index;
551 replaceIdx = glyph_index - CompCount;
553 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
554 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
555 TRACE("0x%x\n",glyphs[replaceIdx]);
559 for (j = replaceIdx + 1; j < *glyph_count; j++)
560 glyphs[j] =glyphs[j+CompCount];
561 *glyph_count = *glyph_count - CompCount;
563 return replaceIdx + 1;
568 return GSUB_E_NOGLYPH;
571 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
576 TRACE("Chaining Contextual Substitution Subtable\n");
577 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
579 const GSUB_ChainContextSubstFormat1 *ccsf1;
581 int dirLookahead = write_dir;
582 int dirBacktrack = -1 * write_dir;
584 offset = GET_BE_WORD(look->SubTable[j]);
585 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
586 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
588 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
591 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
593 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
596 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
600 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
601 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
602 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
603 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
604 int newIndex = glyph_index;
606 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
608 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
610 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
612 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
613 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
616 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
618 TRACE("Matched Backtrack\n");
620 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
622 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
623 for (k = 0; k < indexGlyphs; k++)
625 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
626 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
629 if (k != indexGlyphs)
631 TRACE("Matched IndexGlyphs\n");
633 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
635 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
637 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
638 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
641 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
643 TRACE("Matched LookAhead\n");
645 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
647 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
649 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
650 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
652 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
653 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
656 ERR("Chain failed to generate a glyph\n");
666 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
669 const GSUB_LookupTable *look;
671 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
672 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
673 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
674 switch(GET_BE_WORD(look->LookupType))
677 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
679 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
681 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
683 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
685 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
687 return GSUB_E_NOGLYPH;
690 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
693 int out_index = GSUB_E_NOGLYPH;
694 const GSUB_LookupList *lookup;
696 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
698 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
699 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
701 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
702 if (out_index != GSUB_E_NOGLYPH)
705 if (out_index == GSUB_E_NOGLYPH)
706 TRACE("lookups found no glyphs\n");
710 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
714 if (ShapingData[psa->eScript].otTag[0] != 0)
715 return ShapingData[psa->eScript].otTag;
718 * fall back to the font charset
720 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
723 case ANSI_CHARSET: return "latn";
724 case BALTIC_CHARSET: return "latn"; /* ?? */
725 case CHINESEBIG5_CHARSET: return "hani";
726 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
727 case GB2312_CHARSET: return "hani";
728 case GREEK_CHARSET: return "grek";
729 case HANGUL_CHARSET: return "hang";
730 case RUSSIAN_CHARSET: return "cyrl";
731 case SHIFTJIS_CHARSET: return "kana";
732 case TURKISH_CHARSET: return "latn"; /* ?? */
733 case VIETNAMESE_CHARSET: return "latn";
734 case JOHAB_CHARSET: return "latn"; /* ?? */
735 case ARABIC_CHARSET: return "arab";
736 case HEBREW_CHARSET: return "hebr";
737 case THAI_CHARSET: return "thai";
738 default: return "latn";
742 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
744 const GSUB_Feature *feature;
747 for (i = 0; i < psc->feature_count; i++)
748 if (strncmp(psc->features[i].tag,feat,4)==0)
749 return psc->features[i].feature;
755 const GSUB_Script *script;
756 const GSUB_LangSys *language;
758 script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa));
761 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
763 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
766 /* try in the default (latin) table */
769 script = GSUB_get_script_table(psc->GSUB_Table, "latn");
772 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
774 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
779 TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
781 psc->feature_count++;
784 psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
786 psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
788 lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
789 psc->features[psc->feature_count - 1].feature = feature;
793 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)
795 const GSUB_Feature *feature;
797 feature = load_GSUB_feature(hdc, psa, psc, feat);
799 return GSUB_E_NOFEATURE;
801 TRACE("applying feature %s\n",feat);
802 return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
805 static VOID *load_gsub_table(HDC hdc)
807 VOID* GSUB_Table = NULL;
808 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
809 if (length != GDI_ERROR)
811 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
812 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
813 TRACE("Loaded GSUB table of %i bytes\n",length);
818 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
820 if (changeCount == 0)
825 int target_glyph = nextIndex - 1;
826 int target_index = -1;
827 int replacing_glyph = -1;
831 for (i = 0; i < chars; i++)
833 if (pwLogClust[i] == target_glyph)
840 for (i = chars - 1; i >= 0; i--)
842 if (pwLogClust[i] == target_glyph)
848 if (target_index == -1)
850 ERR("Unable to find target glyph\n");
857 for(i = target_index; i < chars && i >= 0; i+=write_dir)
859 if (pwLogClust[i] == target_glyph)
861 if(pwLogClust[i] == replacing_glyph)
862 pwLogClust[i] = target_glyph;
866 if (changed >= changeCount)
868 replacing_glyph = pwLogClust[i];
869 pwLogClust[i] = target_glyph;
877 /* renumber trailing indexes*/
878 for(i = target_index; i < chars && i >= 0; i+=write_dir)
880 if (pwLogClust[i] != target_glyph)
881 pwLogClust[i] += changeCount;
886 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 )
892 const GSUB_Feature *feature;
894 feature = load_GSUB_feature(hdc, psa, psc, feat);
896 return GSUB_E_NOFEATURE;
899 TRACE("applying feature %s\n",debugstr_an(feat,4));
903 INT prevCount = *pcGlyphs;
904 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
905 if (nextIndex > GSUB_E_NOGLYPH)
907 UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
915 return GSUB_E_NOFEATURE;
918 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
922 if ( i+ delta >= cchLen)
930 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
934 if (psa->fLinkBefore)
939 if ( i+ delta >= cchLen)
949 if (context_type[i] == jtT)
950 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
952 return context_type[i];
955 static inline BOOL right_join_causing(CHAR joining_type)
957 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
960 static inline BOOL left_join_causing(CHAR joining_type)
962 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
965 static inline BOOL word_break_causing(WCHAR chr)
967 /* we are working within a string of characters already guareented to
968 be within one script, Syriac, so we do not worry about any characers
969 other than the space character outside of that range */
970 return (chr == 0 || chr == 0x20 );
974 * ContextualShape_Arabic
976 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
983 if (*pcGlyphs != cChars)
985 ERR("Number of Glyphs and Chars need to match at the beginning\n");
989 if (!psa->fLogicalOrder && psa->fRTL)
1000 if (!psc->GSUB_Table)
1001 psc->GSUB_Table = load_gsub_table(hdc);
1003 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1004 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1006 for (i = 0; i < cChars; i++)
1007 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1009 for (i = 0; i < cChars; i++)
1011 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1012 context_shape[i] = Xr;
1013 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1014 context_shape[i] = Xl;
1015 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)))
1016 context_shape[i] = Xm;
1017 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1018 context_shape[i] = Xr;
1019 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1020 context_shape[i] = Xl;
1022 context_shape[i] = Xn;
1025 /* Contextual Shaping */
1027 while(i < *pcGlyphs)
1029 BOOL shaped = FALSE;
1031 if (psc->GSUB_Table)
1034 INT prevCount = *pcGlyphs;
1035 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1036 if (nextIndex > GSUB_E_NOGLYPH)
1039 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1041 shaped = (nextIndex > GSUB_E_NOGLYPH);
1046 WORD newGlyph = pwOutGlyphs[i];
1047 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1049 /* fall back to presentation form B */
1050 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1051 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1052 pwOutGlyphs[i] = newGlyph;
1058 HeapFree(GetProcessHeap(),0,context_shape);
1059 HeapFree(GetProcessHeap(),0,context_type);
1063 * ContextualShape_Syriac
1067 #define DALATH 0x715
1070 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1077 if (*pcGlyphs != cChars)
1079 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1083 if (!psa->fLogicalOrder && psa->fRTL)
1094 if (!psc->GSUB_Table)
1095 psc->GSUB_Table = load_gsub_table(hdc);
1097 if (!psc->GSUB_Table)
1100 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1101 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1103 for (i = 0; i < cChars; i++)
1104 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1106 for (i = 0; i < cChars; i++)
1108 if (pwcChars[i] == ALAPH)
1110 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1112 if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1113 context_shape[i] = Afj;
1114 else if ( rchar != DALATH && rchar != RISH &&
1115 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1116 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1117 context_shape[i] = Afn;
1118 else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1119 context_shape[i] = Afx;
1121 else if (context_type[i] == jtR &&
1122 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1123 context_shape[i] = Xr;
1124 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1125 context_shape[i] = Xl;
1126 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)))
1127 context_shape[i] = Xm;
1128 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1129 context_shape[i] = Xr;
1130 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1131 context_shape[i] = Xl;
1133 context_shape[i] = Xn;
1136 /* Contextual Shaping */
1138 while(i < *pcGlyphs)
1141 INT prevCount = *pcGlyphs;
1142 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1143 if (nextIndex > GSUB_E_NOGLYPH)
1145 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1150 HeapFree(GetProcessHeap(),0,context_shape);
1151 HeapFree(GetProcessHeap(),0,context_type);
1154 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1156 if (ShapingData[psa->eScript].contextProc)
1157 ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1160 void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1165 if (!rpRangeProperties)
1168 if (!psc->GSUB_Table)
1169 psc->GSUB_Table = load_gsub_table(hdc);
1171 if (!psc->GSUB_Table)
1174 if (!psa->fLogicalOrder && psa->fRTL)
1179 for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1181 if (rpRangeProperties->potfRecords[i].lParameter > 0)
1182 apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1186 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1188 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1189 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1191 SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);