Release 1.3.1.
[wine/hramrach.git] / dlls / usp10 / shape.c
blobcc0a3c07b86c0ccab0bfd92862b227279dd70f37
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},
310 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
312 const GSUB_CoverageFormat1* cf1;
314 cf1 = table;
316 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
318 int count = GET_BE_WORD(cf1->GlyphCount);
319 int i;
320 TRACE("Coverage Format 1, %i glyphs\n",count);
321 for (i = 0; i < count; i++)
322 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
323 return i;
324 return -1;
326 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
328 const GSUB_CoverageFormat2* cf2;
329 int i;
330 int count;
331 cf2 = (const GSUB_CoverageFormat2*)cf1;
333 count = GET_BE_WORD(cf2->RangeCount);
334 TRACE("Coverage Format 2, %i ranges\n",count);
335 for (i = 0; i < count; i++)
337 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
338 return -1;
339 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
340 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
342 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
343 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
346 return -1;
348 else
349 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
351 return -1;
354 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
356 const GSUB_ScriptList *script;
357 const GSUB_Script *deflt = NULL;
358 int i;
359 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
361 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
362 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
364 const GSUB_Script *scr;
365 int offset;
367 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
368 scr = (const GSUB_Script*)((const BYTE*)script + offset);
370 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
371 return scr;
372 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
373 deflt = scr;
375 return deflt;
378 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
380 int i;
381 int offset;
382 const GSUB_LangSys *Lang;
384 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
386 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
388 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
389 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
391 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
392 return Lang;
394 offset = GET_BE_WORD(script->DefaultLangSys);
395 if (offset)
397 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
398 return Lang;
400 return NULL;
403 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
405 int i;
406 const GSUB_FeatureList *feature;
407 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
409 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
410 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
412 int index = GET_BE_WORD(lang->FeatureIndex[i]);
413 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
415 const GSUB_Feature *feat;
416 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
417 return feat;
420 return NULL;
423 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
425 int j;
426 TRACE("Single Substitution Subtable\n");
428 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
430 int offset;
431 const GSUB_SingleSubstFormat1 *ssf1;
432 offset = GET_BE_WORD(look->SubTable[j]);
433 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
434 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
436 int offset = GET_BE_WORD(ssf1->Coverage);
437 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
438 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
440 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
441 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
442 TRACE(" 0x%x\n",glyphs[glyph_index]);
443 return glyph_index + 1;
446 else
448 const GSUB_SingleSubstFormat2 *ssf2;
449 INT index;
450 INT offset;
452 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
453 offset = GET_BE_WORD(ssf1->Coverage);
454 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
455 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
456 TRACE(" Coverage index %i\n",index);
457 if (index != -1)
459 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
460 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
461 TRACE("0x%x\n",glyphs[glyph_index]);
462 return glyph_index + 1;
466 return GSUB_E_NOGLYPH;
469 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
471 int j;
472 TRACE("Alternate Substitution Subtable\n");
474 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
476 int offset;
477 const GSUB_AlternateSubstFormat1 *asf1;
478 INT index;
480 offset = GET_BE_WORD(look->SubTable[j]);
481 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
482 offset = GET_BE_WORD(asf1->Coverage);
484 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
485 if (index != -1)
487 const GSUB_AlternateSet *as;
488 offset = GET_BE_WORD(asf1->AlternateSet[index]);
489 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
490 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
492 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
493 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
494 TRACE(" 0x%x\n",glyphs[glyph_index]);
495 return glyph_index + 1;
498 return GSUB_E_NOGLYPH;
501 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
503 int j;
505 TRACE("Ligature Substitution Subtable\n");
506 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
508 const GSUB_LigatureSubstFormat1 *lsf1;
509 int offset,index;
511 offset = GET_BE_WORD(look->SubTable[j]);
512 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
513 offset = GET_BE_WORD(lsf1->Coverage);
514 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
515 TRACE(" Coverage index %i\n",index);
516 if (index != -1)
518 const GSUB_LigatureSet *ls;
519 int k, count;
521 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
522 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
523 count = GET_BE_WORD(ls->LigatureCount);
524 TRACE(" LigatureSet has %i members\n",count);
525 for (k = 0; k < count; k++)
527 const GSUB_Ligature *lig;
528 int CompCount,l,CompIndex;
530 offset = GET_BE_WORD(ls->Ligature[k]);
531 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
532 CompCount = GET_BE_WORD(lig->CompCount) - 1;
533 CompIndex = glyph_index+write_dir;
534 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
536 int CompGlyph;
537 CompGlyph = GET_BE_WORD(lig->Component[l]);
538 if (CompGlyph != glyphs[CompIndex])
539 break;
540 CompIndex += write_dir;
542 if (l == CompCount)
544 int replaceIdx = glyph_index;
545 if (write_dir < 0)
546 replaceIdx = glyph_index - CompCount;
548 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
549 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
550 TRACE("0x%x\n",glyphs[replaceIdx]);
551 if (CompCount > 0)
553 int j;
554 for (j = replaceIdx + 1; j < *glyph_count; j++)
555 glyphs[j] =glyphs[j+CompCount];
556 *glyph_count = *glyph_count - CompCount;
558 return replaceIdx + 1;
563 return GSUB_E_NOGLYPH;
566 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
568 int j;
569 BOOL done = FALSE;
571 TRACE("Chaining Contextual Substitution Subtable\n");
572 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
574 const GSUB_ChainContextSubstFormat1 *ccsf1;
575 int offset;
576 int dirLookahead = write_dir;
577 int dirBacktrack = -1 * write_dir;
579 offset = GET_BE_WORD(look->SubTable[j]);
580 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
581 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
583 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
584 return -1;
586 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
588 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
589 return -1;
591 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
593 int k;
594 int indexGlyphs;
595 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
596 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
597 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
598 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
599 int newIndex = glyph_index;
601 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
603 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
605 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
607 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
608 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
609 break;
611 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
612 return -1;
613 TRACE("Matched Backtrack\n");
615 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
617 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
618 for (k = 0; k < indexGlyphs; k++)
620 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
621 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
622 break;
624 if (k != indexGlyphs)
625 return -1;
626 TRACE("Matched IndexGlyphs\n");
628 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
630 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
632 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
633 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
634 break;
636 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
637 return -1;
638 TRACE("Matched LookAhead\n");
640 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
642 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
644 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
645 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
647 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
648 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
649 if (newIndex == -1)
651 ERR("Chain failed to generate a glyph\n");
652 return -1;
655 return newIndex + 1;
658 return -1;
661 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
663 int offset;
664 const GSUB_LookupTable *look;
666 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
667 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
668 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
669 switch(GET_BE_WORD(look->LookupType))
671 case 1:
672 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
673 case 3:
674 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
675 case 4:
676 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
677 case 6:
678 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
679 default:
680 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
682 return GSUB_E_NOGLYPH;
685 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
687 int i;
688 int out_index = GSUB_E_NOGLYPH;
689 const GSUB_LookupList *lookup;
691 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
693 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
694 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
696 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
697 if (out_index != GSUB_E_NOGLYPH)
698 break;
700 if (out_index == GSUB_E_NOGLYPH)
701 TRACE("lookups found no glyphs\n");
702 return out_index;
705 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
707 UINT charset;
709 if (ShapingData[psa->eScript].otTag[0] != 0)
710 return ShapingData[psa->eScript].otTag;
713 * fall back to the font charset
715 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
716 switch (charset)
718 case ANSI_CHARSET: return "latn";
719 case BALTIC_CHARSET: return "latn"; /* ?? */
720 case CHINESEBIG5_CHARSET: return "hani";
721 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
722 case GB2312_CHARSET: return "hani";
723 case GREEK_CHARSET: return "grek";
724 case HANGUL_CHARSET: return "hang";
725 case RUSSIAN_CHARSET: return "cyrl";
726 case SHIFTJIS_CHARSET: return "kana";
727 case TURKISH_CHARSET: return "latn"; /* ?? */
728 case VIETNAMESE_CHARSET: return "latn";
729 case JOHAB_CHARSET: return "latn"; /* ?? */
730 case ARABIC_CHARSET: return "arab";
731 case HEBREW_CHARSET: return "hebr";
732 case THAI_CHARSET: return "thai";
733 default: return "latn";
737 static LPCVOID load_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache *psc, const char* feat)
739 const GSUB_Feature *feature;
740 int i;
742 for (i = 0; i < psc->feature_count; i++)
743 if (strncmp(psc->features[i].tag,feat,4)==0)
744 return psc->features[i].feature;
746 feature = NULL;
748 if (psc->GSUB_Table)
750 const GSUB_Script *script;
751 const GSUB_LangSys *language;
753 script = GSUB_get_script_table(psc->GSUB_Table, get_opentype_script(hdc,psa));
754 if (script)
756 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
757 if (language)
758 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
761 /* try in the default (latin) table */
762 if (!feature)
764 script = GSUB_get_script_table(psc->GSUB_Table, "latn");
765 if (script)
767 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
768 if (language)
769 feature = GSUB_get_feature(psc->GSUB_Table, language, feat);
774 TRACE("Feature %s located at %p\n",debugstr_an(feat,4),feature);
776 psc->feature_count++;
778 if (psc->features)
779 psc->features = HeapReAlloc(GetProcessHeap(), 0, psc->features, psc->feature_count * sizeof(LoadedFeature));
780 else
781 psc->features = HeapAlloc(GetProcessHeap(), 0, psc->feature_count * sizeof(LoadedFeature));
783 lstrcpynA(psc->features[psc->feature_count - 1].tag, feat, 5);
784 psc->features[psc->feature_count - 1].feature = feature;
785 return feature;
788 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)
790 const GSUB_Feature *feature;
792 feature = load_GSUB_feature(hdc, psa, psc, feat);
793 if (!feature)
794 return GSUB_E_NOFEATURE;
796 TRACE("applying feature %s\n",feat);
797 return GSUB_apply_feature(psc->GSUB_Table, feature, glyphs, index, write_dir, glyph_count);
800 static VOID *load_gsub_table(HDC hdc)
802 VOID* GSUB_Table = NULL;
803 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
804 if (length != GDI_ERROR)
806 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
807 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
808 TRACE("Loaded GSUB table of %i bytes\n",length);
810 return GSUB_Table;
813 static void UpdateClusters(int nextIndex, int changeCount, int write_dir, int chars, WORD* pwLogClust )
815 if (changeCount == 0)
816 return;
817 else
819 int i;
820 int target_glyph = nextIndex - 1;
821 int target_index = -1;
822 int replacing_glyph = -1;
823 int changed = 0;
825 if (write_dir > 0)
826 for (i = 0; i < chars; i++)
828 if (pwLogClust[i] == target_glyph)
830 target_index = i;
831 break;
834 else
835 for (i = chars - 1; i >= 0; i--)
837 if (pwLogClust[i] == target_glyph)
839 target_index = i;
840 break;
843 if (target_index == -1)
845 ERR("Unable to find target glyph\n");
846 return;
849 if (changeCount < 0)
851 /* merge glyphs */
852 for(i = target_index; i < chars && i >= 0; i+=write_dir)
854 if (pwLogClust[i] == target_glyph)
855 continue;
856 if(pwLogClust[i] == replacing_glyph)
857 pwLogClust[i] = target_glyph;
858 else
860 changed--;
861 if (changed >= changeCount)
863 replacing_glyph = pwLogClust[i];
864 pwLogClust[i] = target_glyph;
866 else
867 break;
872 /* renumber trailing indexes*/
873 for(i = target_index; i < chars && i >= 0; i+=write_dir)
875 if (pwLogClust[i] != target_glyph)
876 pwLogClust[i] += changeCount;
881 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 )
883 int i;
885 if (psc->GSUB_Table)
887 const GSUB_Feature *feature;
889 feature = load_GSUB_feature(hdc, psa, psc, feat);
890 if (!feature)
891 return GSUB_E_NOFEATURE;
893 i = 0;
894 TRACE("applying feature %s\n",debugstr_an(feat,4));
895 while(i < *pcGlyphs)
897 INT nextIndex;
898 INT prevCount = *pcGlyphs;
899 nextIndex = GSUB_apply_feature(psc->GSUB_Table, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
900 if (nextIndex > GSUB_E_NOGLYPH)
902 UpdateClusters(nextIndex, *pcGlyphs - prevCount, write_dir, cChars, pwLogClust);
903 i = nextIndex;
905 else
906 i++;
908 return *pcGlyphs;
910 return GSUB_E_NOFEATURE;
913 static WCHAR neighbour_char(int i, int delta, const WCHAR* chars, INT cchLen)
915 if (i + delta < 0)
916 return 0;
917 if ( i+ delta >= cchLen)
918 return 0;
920 i += delta;
922 return chars[i];
925 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
927 if (i + delta < 0)
929 if (psa->fLinkBefore)
930 return jtR;
931 else
932 return jtU;
934 if ( i+ delta >= cchLen)
936 if (psa->fLinkAfter)
937 return jtL;
938 else
939 return jtU;
942 i += delta;
944 if (context_type[i] == jtT)
945 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
946 else
947 return context_type[i];
950 static inline BOOL right_join_causing(CHAR joining_type)
952 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
955 static inline BOOL left_join_causing(CHAR joining_type)
957 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
960 static inline BOOL word_break_causing(WCHAR chr)
962 /* we are working within a string of characters already guareented to
963 be within one script, Syriac, so we do not worry about any characers
964 other than the space character outside of that range */
965 return (chr == 0 || chr == 0x20 );
969 * ContextualShape_Arabic
971 static void ContextualShape_Arabic(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
973 CHAR *context_type;
974 INT *context_shape;
975 INT dirR, dirL;
976 int i;
978 if (*pcGlyphs != cChars)
980 ERR("Number of Glyphs and Chars need to match at the beginning\n");
981 return;
984 if (!psa->fLogicalOrder && psa->fRTL)
986 dirR = 1;
987 dirL = -1;
989 else
991 dirR = -1;
992 dirL = 1;
995 if (!psc->GSUB_Table)
996 psc->GSUB_Table = load_gsub_table(hdc);
998 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
999 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1001 for (i = 0; i < cChars; i++)
1002 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1004 for (i = 0; i < cChars; i++)
1006 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1007 context_shape[i] = Xr;
1008 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1009 context_shape[i] = Xl;
1010 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)))
1011 context_shape[i] = Xm;
1012 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1013 context_shape[i] = Xr;
1014 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1015 context_shape[i] = Xl;
1016 else
1017 context_shape[i] = Xn;
1020 /* Contextual Shaping */
1021 i = 0;
1022 while(i < *pcGlyphs)
1024 BOOL shaped = FALSE;
1026 if (psc->GSUB_Table)
1028 INT nextIndex;
1029 INT prevCount = *pcGlyphs;
1030 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1031 if (nextIndex > GSUB_E_NOGLYPH)
1033 i = nextIndex;
1034 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1036 shaped = (nextIndex > GSUB_E_NOGLYPH);
1039 if (!shaped)
1041 WORD newGlyph = pwOutGlyphs[i];
1042 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
1044 /* fall back to presentation form B */
1045 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
1046 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
1047 pwOutGlyphs[i] = newGlyph;
1049 i++;
1053 HeapFree(GetProcessHeap(),0,context_shape);
1054 HeapFree(GetProcessHeap(),0,context_type);
1058 * ContextualShape_Syriac
1061 #define ALAPH 0x710
1062 #define DALATH 0x715
1063 #define RISH 0x72A
1065 static void ContextualShape_Syriac(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1067 CHAR *context_type;
1068 INT *context_shape;
1069 INT dirR, dirL;
1070 int i;
1072 if (*pcGlyphs != cChars)
1074 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1075 return;
1078 if (!psa->fLogicalOrder && psa->fRTL)
1080 dirR = 1;
1081 dirL = -1;
1083 else
1085 dirR = -1;
1086 dirL = 1;
1089 if (!psc->GSUB_Table)
1090 psc->GSUB_Table = load_gsub_table(hdc);
1092 if (!psc->GSUB_Table)
1093 return;
1095 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
1096 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
1098 for (i = 0; i < cChars; i++)
1099 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
1101 for (i = 0; i < cChars; i++)
1103 if (pwcChars[i] == ALAPH)
1105 WCHAR rchar = neighbour_char(i,dirR,pwcChars,cChars);
1107 if (left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1108 context_shape[i] = Afj;
1109 else if ( rchar != DALATH && rchar != RISH &&
1110 !left_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)) &&
1111 word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1112 context_shape[i] = Afn;
1113 else if ( (rchar == DALATH || rchar == RISH) && word_break_causing(neighbour_char(i,dirL,pwcChars,cChars)))
1114 context_shape[i] = Afx;
1116 else if (context_type[i] == jtR &&
1117 right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1118 context_shape[i] = Xr;
1119 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1120 context_shape[i] = Xl;
1121 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)))
1122 context_shape[i] = Xm;
1123 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
1124 context_shape[i] = Xr;
1125 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
1126 context_shape[i] = Xl;
1127 else
1128 context_shape[i] = Xn;
1131 /* Contextual Shaping */
1132 i = 0;
1133 while(i < *pcGlyphs)
1135 INT nextIndex;
1136 INT prevCount = *pcGlyphs;
1137 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
1138 if (nextIndex > GSUB_E_NOGLYPH)
1140 UpdateClusters(nextIndex, *pcGlyphs - prevCount, dirL, cChars, pwLogClust);
1141 i = nextIndex;
1145 HeapFree(GetProcessHeap(),0,context_shape);
1146 HeapFree(GetProcessHeap(),0,context_type);
1149 void SHAPE_ContextualShaping(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, WORD *pwLogClust)
1151 if (ShapingData[psa->eScript].contextProc)
1152 ShapingData[psa->eScript].contextProc(hdc, psc, psa, pwcChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1155 void SHAPE_ApplyOpenTypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, const TEXTRANGE_PROPERTIES *rpRangeProperties, WORD *pwLogClust)
1157 int i;
1158 INT dirL;
1160 if (!rpRangeProperties)
1161 return;
1163 if (!psc->GSUB_Table)
1164 psc->GSUB_Table = load_gsub_table(hdc);
1166 if (!psc->GSUB_Table)
1167 return;
1169 if (!psa->fLogicalOrder && psa->fRTL)
1170 dirL = -1;
1171 else
1172 dirL = 1;
1174 for (i = 0; i < rpRangeProperties->cotfRecords; i++)
1176 if (rpRangeProperties->potfRecords[i].lParameter > 0)
1177 apply_GSUB_feature(hdc, psa, psc, pwOutGlyphs, dirL, pcGlyphs, cChars, (const char*)&rpRangeProperties->potfRecords[i].tagFeature, pwLogClust);
1181 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs, INT cChars, WORD *pwLogClust)
1183 const TEXTRANGE_PROPERTIES *rpRangeProperties;
1184 rpRangeProperties = &ShapingData[psa->eScript].defaultTextRange;
1186 SHAPE_ApplyOpenTypeFeatures(hdc, psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, rpRangeProperties, pwLogClust);