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 extern const unsigned short wine_shaping_table[];
41 extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
59 #ifdef WORDS_BIGENDIAN
60 #define GET_BE_WORD(x) (x)
62 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
65 /* These are all structures needed for the GSUB table */
66 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
67 ( ( (ULONG)_x4 << 24 ) | \
68 ( (ULONG)_x3 << 16 ) | \
69 ( (ULONG)_x2 << 8 ) | \
72 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
88 GSUB_ScriptRecord ScriptRecord[1];
99 GSUB_LangSysRecord LangSysRecord[1];
103 WORD LookupOrder; /* Reserved */
104 WORD ReqFeatureIndex;
106 WORD FeatureIndex[1];
112 } GSUB_FeatureRecord;
116 GSUB_FeatureRecord FeatureRecord[1];
120 WORD FeatureParams; /* Reserved */
122 WORD LookupListIndex[1];
141 } GSUB_CoverageFormat1;
146 WORD StartCoverageIndex;
152 GSUB_RangeRecord RangeRecord[1];
153 } GSUB_CoverageFormat2;
156 WORD SubstFormat; /* = 1 */
159 } GSUB_SingleSubstFormat1;
162 WORD SubstFormat; /* = 2 */
166 }GSUB_SingleSubstFormat2;
168 /* the orders of joined_forms and contextual_features need to line up */
169 static const char* contextual_features[] =
177 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
179 const GSUB_CoverageFormat1* cf1;
183 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
185 int count = GET_BE_WORD(cf1->GlyphCount);
187 TRACE("Coverage Format 1, %i glyphs\n",count);
188 for (i = 0; i < count; i++)
189 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
193 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
195 const GSUB_CoverageFormat2* cf2;
198 cf2 = (const GSUB_CoverageFormat2*)cf1;
200 count = GET_BE_WORD(cf2->RangeCount);
201 TRACE("Coverage Format 2, %i ranges\n",count);
202 for (i = 0; i < count; i++)
204 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
206 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
207 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
209 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
210 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
216 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
221 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
223 const GSUB_ScriptList *script;
224 const GSUB_Script *deflt = NULL;
226 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
228 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
229 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
231 const GSUB_Script *scr;
234 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
235 scr = (const GSUB_Script*)((const BYTE*)script + offset);
237 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
239 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
245 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
249 const GSUB_LangSys *Lang;
251 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
253 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
255 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
256 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
258 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
261 offset = GET_BE_WORD(script->DefaultLangSys);
264 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
270 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
273 const GSUB_FeatureList *feature;
274 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
276 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
277 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
279 int index = GET_BE_WORD(lang->FeatureIndex[i]);
280 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
282 const GSUB_Feature *feat;
283 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
290 static UINT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, UINT glyph)
294 const GSUB_LookupList *lookup;
295 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
297 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
298 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
300 const GSUB_LookupTable *look;
301 offset = GET_BE_WORD(lookup->Lookup[GET_BE_WORD(feature->LookupListIndex[i])]);
302 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
303 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
304 if (GET_BE_WORD(look->LookupType) != 1)
305 FIXME("We only handle SubType 1 (%i)\n",GET_BE_WORD(look->LookupType));
310 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
312 const GSUB_SingleSubstFormat1 *ssf1;
313 offset = GET_BE_WORD(look->SubTable[j]);
314 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
315 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
317 int offset = GET_BE_WORD(ssf1->Coverage);
318 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
319 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyph) != -1)
321 TRACE(" Glyph 0x%x ->",glyph);
322 glyph += GET_BE_WORD(ssf1->DeltaGlyphID);
323 TRACE(" 0x%x\n",glyph);
328 const GSUB_SingleSubstFormat2 *ssf2;
332 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
333 offset = GET_BE_WORD(ssf1->Coverage);
334 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
335 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyph);
336 TRACE(" Coverage index %i\n",index);
339 TRACE(" Glyph is 0x%x ->",glyph);
340 glyph = GET_BE_WORD(ssf2->Substitute[index]);
341 TRACE("0x%x\n",glyph);
350 static const char* get_opentype_script(HDC hdc)
353 * I am not sure if this is the correct way to generate our script tag
355 UINT charset = GetTextCharsetInfo(hdc, NULL, 0x0);
359 case ANSI_CHARSET: return "latn";
360 case BALTIC_CHARSET: return "latn"; /* ?? */
361 case CHINESEBIG5_CHARSET: return "hani";
362 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
363 case GB2312_CHARSET: return "hani";
364 case GREEK_CHARSET: return "grek";
365 case HANGUL_CHARSET: return "hang";
366 case RUSSIAN_CHARSET: return "cyrl";
367 case SHIFTJIS_CHARSET: return "kana";
368 case TURKISH_CHARSET: return "latn"; /* ?? */
369 case VIETNAMESE_CHARSET: return "latn";
370 case JOHAB_CHARSET: return "latn"; /* ?? */
371 case ARABIC_CHARSET: return "arab";
372 case HEBREW_CHARSET: return "hebr";
373 case THAI_CHARSET: return "thai";
374 default: return "latn";
378 static WORD get_GSUB_feature_glyph(HDC hdc, void* GSUB_Table, UINT glyph, const char* feat)
380 const GSUB_Header *header;
381 const GSUB_Script *script;
382 const GSUB_LangSys *language;
383 const GSUB_Feature *feature;
390 script = GSUB_get_script_table(header, get_opentype_script(hdc));
393 TRACE("Script not found\n");
396 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
399 TRACE("Language not found\n");
402 feature = GSUB_get_feature(header, language, feat);
405 TRACE("%s feature not found\n",feat);
408 return GSUB_apply_feature(header, feature, glyph);
411 static VOID *load_gsub_table(HDC hdc)
413 VOID* GSUB_Table = NULL;
414 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
415 if (length != GDI_ERROR)
417 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
418 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
419 TRACE("Loaded GSUB table of %i bytes\n",length);
424 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
428 if (psa->fLinkBefore)
433 if ( i+ delta >= cchLen)
443 if (context_type[i] == jtT)
444 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
446 return context_type[i];
449 static inline BOOL right_join_causing(CHAR joining_type)
451 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
454 static inline BOOL left_join_causing(CHAR joining_type)
456 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
459 /* SHAPE_ShapeArabicGlyphs
461 void SHAPE_ShapeArabicGlyphs(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT cMaxGlyphs)
468 if (psa->eScript != Script_Arabic)
471 if (!psa->fLogicalOrder && psa->fRTL)
482 if (!psc->GSUB_Table)
483 psc->GSUB_Table = load_gsub_table(hdc);
485 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
486 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
488 for (i = 0; i < cChars; i++)
489 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
491 for (i = 0; i < cChars; i++)
493 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
494 context_shape[i] = Xr;
495 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
496 context_shape[i] = Xl;
497 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)))
498 context_shape[i] = Xm;
499 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
500 context_shape[i] = Xr;
501 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
502 context_shape[i] = Xl;
504 context_shape[i] = Xn;
507 for (i = 0; i < cChars; i++)
509 WORD newGlyph = pwOutGlyphs[i];
512 newGlyph = get_GSUB_feature_glyph(hdc, psc->GSUB_Table, pwOutGlyphs[i], contextual_features[context_shape[i]]);
513 if (newGlyph == pwOutGlyphs[i] && pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
515 /* fall back to presentation form B */
516 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
517 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
518 pwOutGlyphs[i] = newGlyph;
520 else if (newGlyph != pwOutGlyphs[i])
521 pwOutGlyphs[i] = newGlyph;
524 HeapFree(GetProcessHeap(),0,context_shape);
525 HeapFree(GetProcessHeap(),0,context_type);