usp10: Add Thaana script.
[wine/multimedia.git] / dlls / usp10 / shape.c
blob3644069821de6febf2a5b92860dd1dfb5dbd9bef
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "usp10.h"
29 #include "winternl.h"
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];
49 enum joining_types {
50 jtU,
51 jtT,
52 jtR,
53 jtL,
54 jtD,
55 jtC
58 enum joined_forms {
59 Xn=0,
60 Xr,
61 Xl,
62 Xm,
63 /* Syriac Alaph */
64 Afj,
65 Afn,
66 Afx
69 #ifdef WORDS_BIGENDIAN
70 #define GET_BE_WORD(x) (x)
71 #else
72 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
73 #endif
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 ) | \
80 (ULONG)_x1 )
82 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
83 #define GSUB_E_NOFEATURE -2
84 #define GSUB_E_NOGLYPH -1
86 typedef struct {
87 DWORD version;
88 WORD ScriptList;
89 WORD FeatureList;
90 WORD LookupList;
91 } GSUB_Header;
93 typedef struct {
94 CHAR ScriptTag[4];
95 WORD Script;
96 } GSUB_ScriptRecord;
98 typedef struct {
99 WORD ScriptCount;
100 GSUB_ScriptRecord ScriptRecord[1];
101 } GSUB_ScriptList;
103 typedef struct {
104 CHAR LangSysTag[4];
105 WORD LangSys;
106 } GSUB_LangSysRecord;
108 typedef struct {
109 WORD DefaultLangSys;
110 WORD LangSysCount;
111 GSUB_LangSysRecord LangSysRecord[1];
112 } GSUB_Script;
114 typedef struct {
115 WORD LookupOrder; /* Reserved */
116 WORD ReqFeatureIndex;
117 WORD FeatureCount;
118 WORD FeatureIndex[1];
119 } GSUB_LangSys;
121 typedef struct {
122 CHAR FeatureTag[4];
123 WORD Feature;
124 } GSUB_FeatureRecord;
126 typedef struct {
127 WORD FeatureCount;
128 GSUB_FeatureRecord FeatureRecord[1];
129 } GSUB_FeatureList;
131 typedef struct {
132 WORD FeatureParams; /* Reserved */
133 WORD LookupCount;
134 WORD LookupListIndex[1];
135 } GSUB_Feature;
137 typedef struct {
138 WORD LookupCount;
139 WORD Lookup[1];
140 } GSUB_LookupList;
142 typedef struct {
143 WORD LookupType;
144 WORD LookupFlag;
145 WORD SubTableCount;
146 WORD SubTable[1];
147 } GSUB_LookupTable;
149 typedef struct {
150 WORD CoverageFormat;
151 WORD GlyphCount;
152 WORD GlyphArray[1];
153 } GSUB_CoverageFormat1;
155 typedef struct {
156 WORD Start;
157 WORD End;
158 WORD StartCoverageIndex;
159 } GSUB_RangeRecord;
161 typedef struct {
162 WORD CoverageFormat;
163 WORD RangeCount;
164 GSUB_RangeRecord RangeRecord[1];
165 } GSUB_CoverageFormat2;
167 typedef struct {
168 WORD SubstFormat; /* = 1 */
169 WORD Coverage;
170 WORD DeltaGlyphID;
171 } GSUB_SingleSubstFormat1;
173 typedef struct {
174 WORD SubstFormat; /* = 2 */
175 WORD Coverage;
176 WORD GlyphCount;
177 WORD Substitute[1];
178 }GSUB_SingleSubstFormat2;
180 typedef struct {
181 WORD SubstFormat; /* = 1 */
182 WORD Coverage;
183 WORD LigSetCount;
184 WORD LigatureSet[1];
185 }GSUB_LigatureSubstFormat1;
187 typedef struct {
188 WORD LigatureCount;
189 WORD Ligature[1];
190 }GSUB_LigatureSet;
192 typedef struct{
193 WORD LigGlyph;
194 WORD CompCount;
195 WORD Component[1];
196 }GSUB_Ligature;
198 typedef struct{
199 WORD SequenceIndex;
200 WORD LookupListIndex;
202 }GSUB_SubstLookupRecord;
204 typedef struct{
205 WORD SubstFormat; /* = 1 */
206 WORD Coverage;
207 WORD ChainSubRuleSetCount;
208 WORD ChainSubRuleSet[1];
209 }GSUB_ChainContextSubstFormat1;
211 typedef struct {
212 WORD SubstFormat; /* = 3 */
213 WORD BacktrackGlyphCount;
214 WORD Coverage[1];
215 }GSUB_ChainContextSubstFormat3_1;
217 typedef struct{
218 WORD InputGlyphCount;
219 WORD Coverage[1];
220 }GSUB_ChainContextSubstFormat3_2;
222 typedef struct{
223 WORD LookaheadGlyphCount;
224 WORD Coverage[1];
225 }GSUB_ChainContextSubstFormat3_3;
227 typedef struct{
228 WORD SubstCount;
229 GSUB_SubstLookupRecord SubstLookupRecord[1];
230 }GSUB_ChainContextSubstFormat3_4;
232 typedef struct {
233 WORD SubstFormat; /* = 1 */
234 WORD Coverage;
235 WORD AlternateSetCount;
236 WORD AlternateSet[1];
237 } GSUB_AlternateSubstFormat1;
239 typedef struct{
240 WORD GlyphCount;
241 WORD Alternate[1];
242 } GSUB_AlternateSet;
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[] =
249 "isol",
250 "fina",
251 "init",
252 "medi",
253 /* Syriac Alaph */
254 "med2",
255 "fin2",
256 "fin3"
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;
290 CHAR otTag[5];
291 ContextualShapingProc contextProc;
292 } ScriptShapeData;
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},
311 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
313 const GSUB_CoverageFormat1* cf1;
315 cf1 = table;
317 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
319 int count = GET_BE_WORD(cf1->GlyphCount);
320 int i;
321 TRACE("Coverage Format 1, %i glyphs\n",count);
322 for (i = 0; i < count; i++)
323 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
324 return i;
325 return -1;
327 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
329 const GSUB_CoverageFormat2* cf2;
330 int i;
331 int count;
332 cf2 = (const GSUB_CoverageFormat2*)cf1;
334 count = GET_BE_WORD(cf2->RangeCount);
335 TRACE("Coverage Format 2, %i ranges\n",count);
336 for (i = 0; i < count; i++)
338 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
339 return -1;
340 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
341 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
343 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
344 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
347 return -1;
349 else
350 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
352 return -1;
355 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
357 const GSUB_ScriptList *script;
358 const GSUB_Script *deflt = NULL;
359 int i;
360 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
362 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
363 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
365 const GSUB_Script *scr;
366 int offset;
368 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
369 scr = (const GSUB_Script*)((const BYTE*)script + offset);
371 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
372 return scr;
373 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
374 deflt = scr;
376 return deflt;
379 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
381 int i;
382 int offset;
383 const GSUB_LangSys *Lang;
385 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
387 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
389 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
390 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
392 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
393 return Lang;
395 offset = GET_BE_WORD(script->DefaultLangSys);
396 if (offset)
398 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
399 return Lang;
401 return NULL;
404 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
406 int i;
407 const GSUB_FeatureList *feature;
408 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
410 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
411 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
413 int index = GET_BE_WORD(lang->FeatureIndex[i]);
414 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
416 const GSUB_Feature *feat;
417 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
418 return feat;
421 return NULL;
424 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
426 int j;
427 TRACE("Single Substitution Subtable\n");
429 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
431 int offset;
432 const GSUB_SingleSubstFormat1 *ssf1;
433 offset = GET_BE_WORD(look->SubTable[j]);
434 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
435 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
437 int offset = GET_BE_WORD(ssf1->Coverage);
438 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
439 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
441 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
442 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
443 TRACE(" 0x%x\n",glyphs[glyph_index]);
444 return glyph_index + 1;
447 else
449 const GSUB_SingleSubstFormat2 *ssf2;
450 INT index;
451 INT offset;
453 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
454 offset = GET_BE_WORD(ssf1->Coverage);
455 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
456 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
457 TRACE(" Coverage index %i\n",index);
458 if (index != -1)
460 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
461 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
462 TRACE("0x%x\n",glyphs[glyph_index]);
463 return glyph_index + 1;
467 return GSUB_E_NOGLYPH;
470 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
472 int j;
473 TRACE("Alternate Substitution Subtable\n");
475 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
477 int offset;
478 const GSUB_AlternateSubstFormat1 *asf1;
479 INT index;
481 offset = GET_BE_WORD(look->SubTable[j]);
482 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
483 offset = GET_BE_WORD(asf1->Coverage);
485 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
486 if (index != -1)
488 const GSUB_AlternateSet *as;
489 offset = GET_BE_WORD(asf1->AlternateSet[index]);
490 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
491 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
493 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
494 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
495 TRACE(" 0x%x\n",glyphs[glyph_index]);
496 return glyph_index + 1;
499 return GSUB_E_NOGLYPH;
502 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
504 int j;
506 TRACE("Ligature Substitution Subtable\n");
507 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
509 const GSUB_LigatureSubstFormat1 *lsf1;
510 int offset,index;
512 offset = GET_BE_WORD(look->SubTable[j]);
513 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
514 offset = GET_BE_WORD(lsf1->Coverage);
515 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
516 TRACE(" Coverage index %i\n",index);
517 if (index != -1)
519 const GSUB_LigatureSet *ls;
520 int k, count;
522 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
523 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
524 count = GET_BE_WORD(ls->LigatureCount);
525 TRACE(" LigatureSet has %i members\n",count);
526 for (k = 0; k < count; k++)
528 const GSUB_Ligature *lig;
529 int CompCount,l,CompIndex;
531 offset = GET_BE_WORD(ls->Ligature[k]);
532 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
533 CompCount = GET_BE_WORD(lig->CompCount) - 1;
534 CompIndex = glyph_index+write_dir;
535 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
537 int CompGlyph;
538 CompGlyph = GET_BE_WORD(lig->Component[l]);
539 if (CompGlyph != glyphs[CompIndex])
540 break;
541 CompIndex += write_dir;
543 if (l == CompCount)
545 int replaceIdx = glyph_index;
546 if (write_dir < 0)
547 replaceIdx = glyph_index - CompCount;
549 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
550 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
551 TRACE("0x%x\n",glyphs[replaceIdx]);
552 if (CompCount > 0)
554 int j;
555 for (j = replaceIdx + 1; j < *glyph_count; j++)
556 glyphs[j] =glyphs[j+CompCount];
557 *glyph_count = *glyph_count - CompCount;
559 return replaceIdx + 1;
564 return GSUB_E_NOGLYPH;
567 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
569 int j;
570 BOOL done = FALSE;
572 TRACE("Chaining Contextual Substitution Subtable\n");
573 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
575 const GSUB_ChainContextSubstFormat1 *ccsf1;
576 int offset;
577 int dirLookahead = write_dir;
578 int dirBacktrack = -1 * write_dir;
580 offset = GET_BE_WORD(look->SubTable[j]);
581 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
582 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
584 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
585 return -1;
587 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
589 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
590 return -1;
592 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
594 int k;
595 int indexGlyphs;
596 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
597 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
598 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
599 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
600 int newIndex = glyph_index;
602 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
604 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
606 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
608 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
609 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
610 break;
612 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
613 return -1;
614 TRACE("Matched Backtrack\n");
616 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
618 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
619 for (k = 0; k < indexGlyphs; k++)
621 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
622 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
623 break;
625 if (k != indexGlyphs)
626 return -1;
627 TRACE("Matched IndexGlyphs\n");
629 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
631 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
633 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
634 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
635 break;
637 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
638 return -1;
639 TRACE("Matched LookAhead\n");
641 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
643 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
645 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
646 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
648 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
649 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
650 if (newIndex == -1)
652 ERR("Chain failed to generate a glyph\n");
653 return -1;
656 return newIndex + 1;
659 return -1;
662 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
664 int offset;
665 const GSUB_LookupTable *look;
667 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
668 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
669 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
670 switch(GET_BE_WORD(look->LookupType))
672 case 1:
673 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
674 case 3:
675 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
676 case 4:
677 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
678 case 6:
679 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
680 default:
681 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
683 return GSUB_E_NOGLYPH;
686 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
688 int i;
689 int out_index = GSUB_E_NOGLYPH;
690 const GSUB_LookupList *lookup;
692 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
694 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
695 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
697 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
698 if (out_index != GSUB_E_NOGLYPH)
699 break;
701 if (out_index == GSUB_E_NOGLYPH)
702 TRACE("lookups found no glyphs\n");
703 return out_index;
706 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
708 UINT charset;
710 if (ShapingData[psa->eScript].otTag[0] != 0)
711 return ShapingData[psa->eScript].otTag;
714 * fall back to the font charset
716 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
717 switch (charset)
719 case ANSI_CHARSET: return "latn";
720 case BALTIC_CHARSET: return "latn"; /* ?? */
721 case CHINESEBIG5_CHARSET: return "hani";
722 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
723 case GB2312_CHARSET: return "hani";
724 case GREEK_CHARSET: return "grek";
725 case HANGUL_CHARSET: return "hang";
726 case RUSSIAN_CHARSET: return "cyrl";
727 case SHIFTJIS_CHARSET: return "kana";
728 case TURKISH_CHARSET: return "latn"; /* ?? */
729 case VIETNAMESE_CHARSET: return "latn";
730 case JOHAB_CHARSET: return "latn"; /* ?? */
731 case ARABIC_CHARSET: return "arab";
732 case HEBREW_CHARSET: return "hebr";
733 case THAI_CHARSET: return "thai";
734 default: return "latn";
738 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
740 const GSUB_Feature *feature;
741 int i;
743 for (i = 0; i < psc->feature_count; i++)
744 if (strncmp(psc->features[i].tag,feat,4)==0)
745 return psc->features[i].feature;
747 feature = NULL;
749 if (psc->GSUB_Table)
751 const GSUB_Script *script;
752 const GSUB_LangSys *language;
754 script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa));
755 if (script)
757 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
758 if (language)
759 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
762 /* try in the default (latin) table */
763 if (!feature)
765 script = GSUB_get_script_table(psc->GSUB_Table, "latn");
766 if (script)
768 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
769 if (language)
770 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
775 TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
777 psc->feature_count++;
779 if (psc->features)
780 psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
781 else
782 psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
784 lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
785 psc->features[psc->feature_count - 1].feature = feature;
786 return feature;
789 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)
791 const GSUB_Feature *feature;
793 feature = load_GSUB_feature(hdc, psa, psc, feat);
794 if (!feature)
795 return GSUB_E_NOFEATURE;
797 TRACE("applying feature %s\n",feat);
798 return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
801 static VOID *load_gsub_table(HDC hdc)
803 VOID* GSUB_Table = NULL;
804 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
805 if (length != GDI_ERROR)
807 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
808 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
809 TRACE("Loaded GSUB table of %i bytes\n",length);
811 return GSUB_Table;
814 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
816 if (changeCount == 0)
817 return;
818 else
820 int i;
821 int target_glyph = nextIndex - 1;
822 int target_index = -1;
823 int replacing_glyph = -1;
824 int changed = 0;
826 if (write_dir > 0)
827 for (i = 0; i < chars; i++)
829 if (pwLogClust[i] == target_glyph)
831 target_index = i;
832 break;
835 else
836 for (i = chars - 1; i >= 0; i--)
838 if (pwLogClust[i] == target_glyph)
840 target_index = i;
841 break;
844 if (target_index == -1)
846 ERR("Unable to find target glyph\n");
847 return;
850 if (changeCount < 0)
852 /* merge glyphs */
853 for(i = target_index; i < chars && i >= 0; i+=write_dir)
855 if (pwLogClust[i] == target_glyph)
856 continue;
857 if(pwLogClust[i] == replacing_glyph)
858 pwLogClust[i] = target_glyph;
859 else
861 changed--;
862 if (changed >= changeCount)
864 replacing_glyph = pwLogClust[i];
865 pwLogClust[i] = target_glyph;
867 else
868 break;
873 /* renumber trailing indexes*/
874 for(i = target_index; i < chars && i >= 0; i+=write_dir)
876 if (pwLogClust[i] != target_glyph)
877 pwLogClust[i] += changeCount;
882 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 )
884 int i;
886 if (psc->GSUB_Table)
888 const GSUB_Feature *feature;
890 feature = load_GSUB_feature(hdc, psa, psc, feat);
891 if (!feature)
892 return GSUB_E_NOFEATURE;
894 i = 0;
895 TRACE("applying feature %s\n",debugstr_an(feat,4));
896 while(i < *pcGlyphs)
898 INT nextIndex;
899 INT prevCount = *pcGlyphs;
900 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
901 if (nextIndex > GSUB_E_NOGLYPH)
903 UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
904 i = nextIndex;
906 else
907 i++;
909 return *pcGlyphs;
911 return GSUB_E_NOFEATURE;
914 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
916 if (i + delta < 0)
917 return 0;
918 if ( i+ delta >= cchLen)
919 return 0;
921 i += delta;
923 return chars[i];
926 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
928 if (i + delta < 0)
930 if (psa->fLinkBefore)
931 return jtR;
932 else
933 return jtU;
935 if ( i+ delta >= cchLen)
937 if (psa->fLinkAfter)
938 return jtL;
939 else
940 return jtU;
943 i += delta;
945 if (context_type[i] == jtT)
946 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
947 else
948 return context_type[i];
951 static inline BOOL right_join_causing(CHAR joining_type)
953 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
956 static inline BOOL left_join_causing(CHAR joining_type)
958 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
961 static inline BOOL word_break_causing(WCHAR chr)
963 /* we are working within a string of characters already guareented to
964 be within one script, Syriac, so we do not worry about any characers
965 other than the space character outside of that range */
966 return (chr == 0 || chr == 0x20 );
970 * ContextualShape_Arabic
972 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
974 CHAR *context_type;
975 INT *context_shape;
976 INT dirR, dirL;
977 int i;
979 if (*pcGlyphs != cChars)
981 ERR("Number of Glyphs and Chars need to match at the beginning\n");
982 return;
985 if (!psa->fLogicalOrder && psa->fRTL)
987 dirR = 1;
988 dirL = -1;
990 else
992 dirR = -1;
993 dirL = 1;
996 if (!psc->GSUB_Table)
997 psc->GSUB_Table = load_gsub_table(hdc);
999 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1000 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1002 for (i = 0; i < cChars; i++)
1003 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1005 for (i = 0; i < cChars; i++)
1007 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1008 context_shape[i] = Xr;
1009 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1010 context_shape[i] = Xl;
1011 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)))
1012 context_shape[i] = Xm;
1013 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1014 context_shape[i] = Xr;
1015 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1016 context_shape[i] = Xl;
1017 else
1018 context_shape[i] = Xn;
1021 /* Contextual Shaping */
1022 i = 0;
1023 while(i < *pcGlyphs)
1025 BOOL shaped = FALSE;
1027 if (psc->GSUB_Table)
1029 INT nextIndex;
1030 INT prevCount = *pcGlyphs;
1031 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1032 if (nextIndex > GSUB_E_NOGLYPH)
1034 i = nextIndex;
1035 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1037 shaped = (nextIndex > GSUB_E_NOGLYPH);
1040 if (!shaped)
1042 WORD newGlyph = pwOutGlyphs[i];
1043 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1045 /* fall back to presentation form B */
1046 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1047 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1048 pwOutGlyphs[i] = newGlyph;
1050 i++;
1054 HeapFree(GetProcessHeap(),0,context_shape);
1055 HeapFree(GetProcessHeap(),0,context_type);
1059 * ContextualShape_Syriac
1062 #define ALAPH 0x710
1063 #define DALATH 0x715
1064 #define RISH 0x72A
1066 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1068 CHAR *context_type;
1069 INT *context_shape;
1070 INT dirR, dirL;
1071 int i;
1073 if (*pcGlyphs != cChars)
1075 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1076 return;
1079 if (!psa->fLogicalOrder && psa->fRTL)
1081 dirR = 1;
1082 dirL = -1;
1084 else
1086 dirR = -1;
1087 dirL = 1;
1090 if (!psc->GSUB_Table)
1091 psc->GSUB_Table = load_gsub_table(hdc);
1093 if (!psc->GSUB_Table)
1094 return;
1096 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1097 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1099 for (i = 0; i < cChars; i++)
1100 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1102 for (i = 0; i < cChars; i++)
1104 if (pwcChars[i] == ALAPH)
1106 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1108 if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1109 context_shape[i] = Afj;
1110 else if ( rchar != DALATH && rchar != RISH &&
1111 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1112 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1113 context_shape[i] = Afn;
1114 else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1115 context_shape[i] = Afx;
1117 else if (context_type[i] == jtR &&
1118 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1119 context_shape[i] = Xr;
1120 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1121 context_shape[i] = Xl;
1122 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)))
1123 context_shape[i] = Xm;
1124 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1125 context_shape[i] = Xr;
1126 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1127 context_shape[i] = Xl;
1128 else
1129 context_shape[i] = Xn;
1132 /* Contextual Shaping */
1133 i = 0;
1134 while(i < *pcGlyphs)
1136 INT nextIndex;
1137 INT prevCount = *pcGlyphs;
1138 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1139 if (nextIndex > GSUB_E_NOGLYPH)
1141 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1142 i = nextIndex;
1146 HeapFree(GetProcessHeap(),0,context_shape);
1147 HeapFree(GetProcessHeap(),0,context_type);
1150 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1152 if (ShapingData[psa->eScript].contextProc)
1153 ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1156 void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1158 int i;
1159 INT dirL;
1161 if (!rpRangeProperties)
1162 return;
1164 if (!psc->GSUB_Table)
1165 psc->GSUB_Table = load_gsub_table(hdc);
1167 if (!psc->GSUB_Table)
1168 return;
1170 if (!psa->fLogicalOrder && psa->fRTL)
1171 dirL = -1;
1172 else
1173 dirL = 1;
1175 for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1177 if (rpRangeProperties->potfRecords[i].lParameter > 0)
1178 apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1182 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1184 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1185 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1187 SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);