wined3d: Rename surface_calculate_size() to wined3d_format_calculate_size().
[wine.git] / dlls / usp10 / shape.c
blob210022d08702bb86f8e61c6d4dc52347e3fd7f31
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 extern const unsigned short wine_shaping_table[];
41 extern const unsigned short wine_shaping_forms[LAST_ARABIC_CHAR - FIRST_ARABIC_CHAR + 1][4];
43 enum joining_types {
44 jtU,
45 jtT,
46 jtR,
47 jtL,
48 jtD,
49 jtC
52 enum joined_forms {
53 Xn=0,
54 Xr,
55 Xl,
59 #ifdef WORDS_BIGENDIAN
60 #define GET_BE_WORD(x) (x)
61 #else
62 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
63 #endif
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 ) | \
70 (ULONG)_x1 )
72 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
73 #define GSUB_E_NOFEATURE -2
74 #define GSUB_E_NOGLYPH -1
76 typedef struct {
77 DWORD version;
78 WORD ScriptList;
79 WORD FeatureList;
80 WORD LookupList;
81 } GSUB_Header;
83 typedef struct {
84 CHAR ScriptTag[4];
85 WORD Script;
86 } GSUB_ScriptRecord;
88 typedef struct {
89 WORD ScriptCount;
90 GSUB_ScriptRecord ScriptRecord[1];
91 } GSUB_ScriptList;
93 typedef struct {
94 CHAR LangSysTag[4];
95 WORD LangSys;
96 } GSUB_LangSysRecord;
98 typedef struct {
99 WORD DefaultLangSys;
100 WORD LangSysCount;
101 GSUB_LangSysRecord LangSysRecord[1];
102 } GSUB_Script;
104 typedef struct {
105 WORD LookupOrder; /* Reserved */
106 WORD ReqFeatureIndex;
107 WORD FeatureCount;
108 WORD FeatureIndex[1];
109 } GSUB_LangSys;
111 typedef struct {
112 CHAR FeatureTag[4];
113 WORD Feature;
114 } GSUB_FeatureRecord;
116 typedef struct {
117 WORD FeatureCount;
118 GSUB_FeatureRecord FeatureRecord[1];
119 } GSUB_FeatureList;
121 typedef struct {
122 WORD FeatureParams; /* Reserved */
123 WORD LookupCount;
124 WORD LookupListIndex[1];
125 } GSUB_Feature;
127 typedef struct {
128 WORD LookupCount;
129 WORD Lookup[1];
130 } GSUB_LookupList;
132 typedef struct {
133 WORD LookupType;
134 WORD LookupFlag;
135 WORD SubTableCount;
136 WORD SubTable[1];
137 } GSUB_LookupTable;
139 typedef struct {
140 WORD CoverageFormat;
141 WORD GlyphCount;
142 WORD GlyphArray[1];
143 } GSUB_CoverageFormat1;
145 typedef struct {
146 WORD Start;
147 WORD End;
148 WORD StartCoverageIndex;
149 } GSUB_RangeRecord;
151 typedef struct {
152 WORD CoverageFormat;
153 WORD RangeCount;
154 GSUB_RangeRecord RangeRecord[1];
155 } GSUB_CoverageFormat2;
157 typedef struct {
158 WORD SubstFormat; /* = 1 */
159 WORD Coverage;
160 WORD DeltaGlyphID;
161 } GSUB_SingleSubstFormat1;
163 typedef struct {
164 WORD SubstFormat; /* = 2 */
165 WORD Coverage;
166 WORD GlyphCount;
167 WORD Substitute[1];
168 }GSUB_SingleSubstFormat2;
170 typedef struct {
171 WORD SubstFormat; /* = 1 */
172 WORD Coverage;
173 WORD LigSetCount;
174 WORD LigatureSet[1];
175 }GSUB_LigatureSubstFormat1;
177 typedef struct {
178 WORD LigatureCount;
179 WORD Ligature[1];
180 }GSUB_LigatureSet;
182 typedef struct{
183 WORD LigGlyph;
184 WORD CompCount;
185 WORD Component[1];
186 }GSUB_Ligature;
188 typedef struct{
189 WORD SequenceIndex;
190 WORD LookupListIndex;
192 }GSUB_SubstLookupRecord;
194 typedef struct{
195 WORD SubstFormat; /* = 1 */
196 WORD Coverage;
197 WORD ChainSubRuleSetCount;
198 WORD ChainSubRuleSet[1];
199 }GSUB_ChainContextSubstFormat1;
201 typedef struct {
202 WORD SubstFormat; /* = 3 */
203 WORD BacktrackGlyphCount;
204 WORD Coverage[1];
205 }GSUB_ChainContextSubstFormat3_1;
207 typedef struct{
208 WORD InputGlyphCount;
209 WORD Coverage[1];
210 }GSUB_ChainContextSubstFormat3_2;
212 typedef struct{
213 WORD LookaheadGlyphCount;
214 WORD Coverage[1];
215 }GSUB_ChainContextSubstFormat3_3;
217 typedef struct{
218 WORD SubstCount;
219 GSUB_SubstLookupRecord SubstLookupRecord[1];
220 }GSUB_ChainContextSubstFormat3_4;
222 typedef struct {
223 WORD SubstFormat; /* = 1 */
224 WORD Coverage;
225 WORD AlternateSetCount;
226 WORD AlternateSet[1];
227 } GSUB_AlternateSubstFormat1;
229 typedef struct{
230 WORD GlyphCount;
231 WORD Alternate[1];
232 } GSUB_AlternateSet;
234 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count);
236 /* the orders of joined_forms and contextual_features need to line up */
237 static const char* contextual_features[] =
239 "isol",
240 "fina",
241 "init",
242 "medi"
245 static const char* arabic_GSUB_features[] =
247 "rlig",
248 "calt",
249 "liga",
250 "dlig",
251 "cswh",
252 "mset",
253 NULL
256 static INT GSUB_is_glyph_covered(LPCVOID table , UINT glyph)
258 const GSUB_CoverageFormat1* cf1;
260 cf1 = table;
262 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
264 int count = GET_BE_WORD(cf1->GlyphCount);
265 int i;
266 TRACE("Coverage Format 1, %i glyphs\n",count);
267 for (i = 0; i < count; i++)
268 if (glyph == GET_BE_WORD(cf1->GlyphArray[i]))
269 return i;
270 return -1;
272 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
274 const GSUB_CoverageFormat2* cf2;
275 int i;
276 int count;
277 cf2 = (const GSUB_CoverageFormat2*)cf1;
279 count = GET_BE_WORD(cf2->RangeCount);
280 TRACE("Coverage Format 2, %i ranges\n",count);
281 for (i = 0; i < count; i++)
283 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start))
284 return -1;
285 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
286 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
288 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
289 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
292 return -1;
294 else
295 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
297 return -1;
300 static const GSUB_Script* GSUB_get_script_table( const GSUB_Header* header, const char* tag)
302 const GSUB_ScriptList *script;
303 const GSUB_Script *deflt = NULL;
304 int i;
305 script = (const GSUB_ScriptList*)((const BYTE*)header + GET_BE_WORD(header->ScriptList));
307 TRACE("%i scripts in this font\n",GET_BE_WORD(script->ScriptCount));
308 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
310 const GSUB_Script *scr;
311 int offset;
313 offset = GET_BE_WORD(script->ScriptRecord[i].Script);
314 scr = (const GSUB_Script*)((const BYTE*)script + offset);
316 if (strncmp(script->ScriptRecord[i].ScriptTag, tag,4)==0)
317 return scr;
318 if (strncmp(script->ScriptRecord[i].ScriptTag, "dflt",4)==0)
319 deflt = scr;
321 return deflt;
324 static const GSUB_LangSys* GSUB_get_lang_table( const GSUB_Script* script, const char* tag)
326 int i;
327 int offset;
328 const GSUB_LangSys *Lang;
330 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
332 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
334 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
335 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
337 if ( strncmp(script->LangSysRecord[i].LangSysTag,tag,4)==0)
338 return Lang;
340 offset = GET_BE_WORD(script->DefaultLangSys);
341 if (offset)
343 Lang = (const GSUB_LangSys*)((const BYTE*)script + offset);
344 return Lang;
346 return NULL;
349 static const GSUB_Feature * GSUB_get_feature(const GSUB_Header *header, const GSUB_LangSys *lang, const char* tag)
351 int i;
352 const GSUB_FeatureList *feature;
353 feature = (const GSUB_FeatureList*)((const BYTE*)header + GET_BE_WORD(header->FeatureList));
355 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
356 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
358 int index = GET_BE_WORD(lang->FeatureIndex[i]);
359 if (strncmp(feature->FeatureRecord[index].FeatureTag,tag,4)==0)
361 const GSUB_Feature *feat;
362 feat = (const GSUB_Feature*)((const BYTE*)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
363 return feat;
366 return NULL;
369 static INT GSUB_apply_SingleSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
371 int j;
372 TRACE("Single Substitution Subtable\n");
374 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
376 int offset;
377 const GSUB_SingleSubstFormat1 *ssf1;
378 offset = GET_BE_WORD(look->SubTable[j]);
379 ssf1 = (const GSUB_SingleSubstFormat1*)((const BYTE*)look+offset);
380 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
382 int offset = GET_BE_WORD(ssf1->Coverage);
383 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
384 if (GSUB_is_glyph_covered((const BYTE*)ssf1+offset, glyphs[glyph_index]) != -1)
386 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
387 glyphs[glyph_index] = glyphs[glyph_index] + GET_BE_WORD(ssf1->DeltaGlyphID);
388 TRACE(" 0x%x\n",glyphs[glyph_index]);
389 return glyph_index + 1;
392 else
394 const GSUB_SingleSubstFormat2 *ssf2;
395 INT index;
396 INT offset;
398 ssf2 = (const GSUB_SingleSubstFormat2 *)ssf1;
399 offset = GET_BE_WORD(ssf1->Coverage);
400 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
401 index = GSUB_is_glyph_covered((const BYTE*)ssf2+offset, glyphs[glyph_index]);
402 TRACE(" Coverage index %i\n",index);
403 if (index != -1)
405 TRACE(" Glyph is 0x%x ->",glyphs[glyph_index]);
406 glyphs[glyph_index] = GET_BE_WORD(ssf2->Substitute[index]);
407 TRACE("0x%x\n",glyphs[glyph_index]);
408 return glyph_index + 1;
412 return GSUB_E_NOGLYPH;
415 static INT GSUB_apply_AlternateSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
417 int j;
418 TRACE("Alternate Substitution Subtable\n");
420 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
422 int offset;
423 const GSUB_AlternateSubstFormat1 *asf1;
424 INT index;
426 offset = GET_BE_WORD(look->SubTable[j]);
427 asf1 = (const GSUB_AlternateSubstFormat1*)((const BYTE*)look+offset);
428 offset = GET_BE_WORD(asf1->Coverage);
430 index = GSUB_is_glyph_covered((const BYTE*)asf1+offset, glyphs[glyph_index]);
431 if (index != -1)
433 const GSUB_AlternateSet *as;
434 offset = GET_BE_WORD(asf1->AlternateSet[index]);
435 as = (const GSUB_AlternateSet*)((const BYTE*)asf1+offset);
436 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as->GlyphCount));
438 TRACE(" Glyph 0x%x ->",glyphs[glyph_index]);
439 glyphs[glyph_index] = GET_BE_WORD(as->Alternate[0]);
440 TRACE(" 0x%x\n",glyphs[glyph_index]);
441 return glyph_index + 1;
444 return GSUB_E_NOGLYPH;
447 static INT GSUB_apply_LigatureSubst(const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
449 int j;
451 TRACE("Ligature Substitution Subtable\n");
452 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
454 const GSUB_LigatureSubstFormat1 *lsf1;
455 int offset,index;
457 offset = GET_BE_WORD(look->SubTable[j]);
458 lsf1 = (const GSUB_LigatureSubstFormat1*)((const BYTE*)look+offset);
459 offset = GET_BE_WORD(lsf1->Coverage);
460 index = GSUB_is_glyph_covered((const BYTE*)lsf1+offset, glyphs[glyph_index]);
461 TRACE(" Coverage index %i\n",index);
462 if (index != -1)
464 const GSUB_LigatureSet *ls;
465 int k, count;
467 offset = GET_BE_WORD(lsf1->LigatureSet[index]);
468 ls = (const GSUB_LigatureSet*)((const BYTE*)lsf1+offset);
469 count = GET_BE_WORD(ls->LigatureCount);
470 TRACE(" LigatureSet has %i members\n",count);
471 for (k = 0; k < count; k++)
473 const GSUB_Ligature *lig;
474 int CompCount,l,CompIndex;
476 offset = GET_BE_WORD(ls->Ligature[k]);
477 lig = (const GSUB_Ligature*)((const BYTE*)ls+offset);
478 CompCount = GET_BE_WORD(lig->CompCount) - 1;
479 CompIndex = glyph_index+write_dir;
480 for (l = 0; l < CompCount && CompIndex >= 0 && CompIndex < *glyph_count; l++)
482 int CompGlyph;
483 CompGlyph = GET_BE_WORD(lig->Component[l]);
484 if (CompGlyph != glyphs[CompIndex])
485 break;
486 CompIndex += write_dir;
488 if (l == CompCount)
490 int replaceIdx = glyph_index;
491 if (write_dir < 0)
492 replaceIdx = glyph_index - CompCount;
494 TRACE(" Glyph is 0x%x (+%i) ->",glyphs[glyph_index],CompCount);
495 glyphs[replaceIdx] = GET_BE_WORD(lig->LigGlyph);
496 TRACE("0x%x\n",glyphs[replaceIdx]);
497 if (CompCount > 0)
499 int j;
500 for (j = replaceIdx + 1; j < *glyph_count; j++)
501 glyphs[j] =glyphs[j+CompCount];
502 *glyph_count = *glyph_count - CompCount;
504 return replaceIdx + 1;
509 return GSUB_E_NOGLYPH;
512 static INT GSUB_apply_ChainContextSubst(const GSUB_LookupList* lookup, const GSUB_LookupTable *look, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
514 int j;
515 BOOL done = FALSE;
517 TRACE("Chaining Contextual Substitution Subtable\n");
518 for (j = 0; j < GET_BE_WORD(look->SubTableCount) && !done; j++)
520 const GSUB_ChainContextSubstFormat1 *ccsf1;
521 int offset;
522 int dirLookahead = write_dir;
523 int dirBacktrack = -1 * write_dir;
525 offset = GET_BE_WORD(look->SubTable[j]);
526 ccsf1 = (const GSUB_ChainContextSubstFormat1*)((const BYTE*)look+offset);
527 if (GET_BE_WORD(ccsf1->SubstFormat) == 1)
529 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
530 return -1;
532 else if (GET_BE_WORD(ccsf1->SubstFormat) == 2)
534 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
535 return -1;
537 else if (GET_BE_WORD(ccsf1->SubstFormat) == 3)
539 int k;
540 int indexGlyphs;
541 const GSUB_ChainContextSubstFormat3_1 *ccsf3_1;
542 const GSUB_ChainContextSubstFormat3_2 *ccsf3_2;
543 const GSUB_ChainContextSubstFormat3_3 *ccsf3_3;
544 const GSUB_ChainContextSubstFormat3_4 *ccsf3_4;
545 int newIndex = glyph_index;
547 ccsf3_1 = (const GSUB_ChainContextSubstFormat3_1 *)ccsf1;
549 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
551 for (k = 0; k < GET_BE_WORD(ccsf3_1->BacktrackGlyphCount); k++)
553 offset = GET_BE_WORD(ccsf3_1->Coverage[k]);
554 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirBacktrack * (k+1))]) == -1)
555 break;
557 if (k != GET_BE_WORD(ccsf3_1->BacktrackGlyphCount))
558 return -1;
559 TRACE("Matched Backtrack\n");
561 ccsf3_2 = (const GSUB_ChainContextSubstFormat3_2 *)(((LPBYTE)ccsf1)+sizeof(GSUB_ChainContextSubstFormat3_1) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_1->BacktrackGlyphCount)-1)));
563 indexGlyphs = GET_BE_WORD(ccsf3_2->InputGlyphCount);
564 for (k = 0; k < indexGlyphs; k++)
566 offset = GET_BE_WORD(ccsf3_2->Coverage[k]);
567 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (write_dir * k)]) == -1)
568 break;
570 if (k != indexGlyphs)
571 return -1;
572 TRACE("Matched IndexGlyphs\n");
574 ccsf3_3 = (const GSUB_ChainContextSubstFormat3_3 *)(((LPBYTE)ccsf3_2)+sizeof(GSUB_ChainContextSubstFormat3_2) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_2->InputGlyphCount)-1)));
576 for (k = 0; k < GET_BE_WORD(ccsf3_3->LookaheadGlyphCount); k++)
578 offset = GET_BE_WORD(ccsf3_3->Coverage[k]);
579 if (GSUB_is_glyph_covered((const BYTE*)ccsf3_1+offset, glyphs[glyph_index + (dirLookahead * (indexGlyphs + k+1))]) == -1)
580 break;
582 if (k != GET_BE_WORD(ccsf3_3->LookaheadGlyphCount))
583 return -1;
584 TRACE("Matched LookAhead\n");
586 ccsf3_4 = (const GSUB_ChainContextSubstFormat3_4 *)(((LPBYTE)ccsf3_3)+sizeof(GSUB_ChainContextSubstFormat3_3) + (sizeof(WORD) * (GET_BE_WORD(ccsf3_3->LookaheadGlyphCount)-1)));
588 for (k = 0; k < GET_BE_WORD(ccsf3_4->SubstCount); k++)
590 int lookupIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].LookupListIndex);
591 int SequenceIndex = GET_BE_WORD(ccsf3_4->SubstLookupRecord[k].SequenceIndex) * write_dir;
593 TRACE("SUBST: %i -> %i %i\n",k, SequenceIndex, lookupIndex);
594 newIndex = GSUB_apply_lookup(lookup, lookupIndex, glyphs, glyph_index + SequenceIndex, write_dir, glyph_count);
595 if (newIndex == -1)
597 ERR("Chain failed to generate a glyph\n");
598 return -1;
601 return newIndex + 1;
604 return -1;
607 static INT GSUB_apply_lookup(const GSUB_LookupList* lookup, INT lookup_index, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
609 int offset;
610 const GSUB_LookupTable *look;
612 offset = GET_BE_WORD(lookup->Lookup[lookup_index]);
613 look = (const GSUB_LookupTable*)((const BYTE*)lookup + offset);
614 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
615 switch(GET_BE_WORD(look->LookupType))
617 case 1:
618 return GSUB_apply_SingleSubst(look, glyphs, glyph_index, write_dir, glyph_count);
619 case 3:
620 return GSUB_apply_AlternateSubst(look, glyphs, glyph_index, write_dir, glyph_count);
621 case 4:
622 return GSUB_apply_LigatureSubst(look, glyphs, glyph_index, write_dir, glyph_count);
623 case 6:
624 return GSUB_apply_ChainContextSubst(lookup, look, glyphs, glyph_index, write_dir, glyph_count);
625 default:
626 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look->LookupType));
628 return GSUB_E_NOGLYPH;
631 static INT GSUB_apply_feature(const GSUB_Header * header, const GSUB_Feature* feature, WORD *glyphs, INT glyph_index, INT write_dir, INT *glyph_count)
633 int i;
634 int out_index = GSUB_E_NOGLYPH;
635 const GSUB_LookupList *lookup;
637 lookup = (const GSUB_LookupList*)((const BYTE*)header + GET_BE_WORD(header->LookupList));
639 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
640 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
642 out_index = GSUB_apply_lookup(lookup, GET_BE_WORD(feature->LookupListIndex[i]), glyphs, glyph_index, write_dir, glyph_count);
643 if (out_index != GSUB_E_NOGLYPH)
644 break;
646 if (out_index == GSUB_E_NOGLYPH)
647 TRACE("lookups found no glyphs\n");
648 return out_index;
651 static const char* get_opentype_script(HDC hdc, SCRIPT_ANALYSIS *psa)
653 UINT charset;
655 switch (psa->eScript)
657 case Script_Arabic:
658 return "arab";
659 case Script_Syriac:
660 return "syrc";
661 case Script_Hebrew:
662 return "hebr";
663 case Script_Latin:
664 case Script_Numeric:
665 case Script_CR:
666 case Script_LF:
667 return "latn";
671 * fall back to the font charset
673 charset = GetTextCharsetInfo(hdc, NULL, 0x0);
674 switch (charset)
676 case ANSI_CHARSET: return "latn";
677 case BALTIC_CHARSET: return "latn"; /* ?? */
678 case CHINESEBIG5_CHARSET: return "hani";
679 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
680 case GB2312_CHARSET: return "hani";
681 case GREEK_CHARSET: return "grek";
682 case HANGUL_CHARSET: return "hang";
683 case RUSSIAN_CHARSET: return "cyrl";
684 case SHIFTJIS_CHARSET: return "kana";
685 case TURKISH_CHARSET: return "latn"; /* ?? */
686 case VIETNAMESE_CHARSET: return "latn";
687 case JOHAB_CHARSET: return "latn"; /* ?? */
688 case ARABIC_CHARSET: return "arab";
689 case HEBREW_CHARSET: return "hebr";
690 case THAI_CHARSET: return "thai";
691 default: return "latn";
695 static INT apply_GSUB_feature_to_glyph(HDC hdc, SCRIPT_ANALYSIS *psa, void* GSUB_Table, WORD *glyphs, INT index, INT write_dir, INT* glyph_count, const char* feat)
697 const GSUB_Header *header;
698 const GSUB_Script *script;
699 const GSUB_LangSys *language;
700 const GSUB_Feature *feature;
702 if (!GSUB_Table)
703 return GSUB_E_NOFEATURE;
705 header = GSUB_Table;
707 script = GSUB_get_script_table(header, get_opentype_script(hdc,psa));
708 if (!script)
710 TRACE("Script not found\n");
711 return GSUB_E_NOFEATURE;
713 language = GSUB_get_lang_table(script, "xxxx"); /* Need to get Lang tag */
714 if (!language)
716 TRACE("Language not found\n");
717 return GSUB_E_NOFEATURE;
719 feature = GSUB_get_feature(header, language, feat);
720 if (!feature)
722 TRACE("%s feature not found\n",feat);
723 return GSUB_E_NOFEATURE;
725 TRACE("applying feature %s\n",feat);
726 return GSUB_apply_feature(header, feature, glyphs, index, write_dir, glyph_count);
729 static VOID *load_gsub_table(HDC hdc)
731 VOID* GSUB_Table = NULL;
732 int length = GetFontData(hdc, GSUB_TAG , 0, NULL, 0);
733 if (length != GDI_ERROR)
735 GSUB_Table = HeapAlloc(GetProcessHeap(),0,length);
736 GetFontData(hdc, GSUB_TAG , 0, GSUB_Table, length);
737 TRACE("Loaded GSUB table of %i bytes\n",length);
739 return GSUB_Table;
742 static int apply_GSUB_feature(HDC hdc, SCRIPT_ANALYSIS *psa, void* GSUB_Table, WORD *pwOutGlyphs, int write_dir, INT* pcGlyphs, const char* feat)
744 int i;
746 if (GSUB_Table)
748 const GSUB_Header *header;
749 const GSUB_Script *script;
750 const GSUB_LangSys *language;
751 const GSUB_Feature *feature;
753 if (!GSUB_Table)
754 return GSUB_E_NOFEATURE;
756 header = GSUB_Table;
758 script = GSUB_get_script_table(header, get_opentype_script(hdc,psa));
759 if (!script)
761 TRACE("Script not found\n");
762 return GSUB_E_NOFEATURE;
764 language = GSUB_get_lang_table(script, "xxxx");
765 if (!language)
767 TRACE("Language not found\n");
768 return GSUB_E_NOFEATURE;
770 feature = GSUB_get_feature(header, language, feat);
771 if (!feature)
773 TRACE("%s feature not found\n",feat);
774 return GSUB_E_NOFEATURE;
777 i = 0;
778 TRACE("applying feature %s\n",feat);
779 while(i < *pcGlyphs)
781 INT nextIndex;
782 nextIndex = GSUB_apply_feature(header, feature, pwOutGlyphs, i, write_dir, pcGlyphs);
783 if (nextIndex > GSUB_E_NOGLYPH)
784 i = nextIndex;
785 else
786 i++;
788 return *pcGlyphs;
790 return GSUB_E_NOFEATURE;
793 static CHAR neighbour_joining_type(int i, int delta, const CHAR* context_type, INT cchLen, SCRIPT_ANALYSIS *psa)
795 if (i + delta < 0)
797 if (psa->fLinkBefore)
798 return jtR;
799 else
800 return jtU;
802 if ( i+ delta >= cchLen)
804 if (psa->fLinkAfter)
805 return jtL;
806 else
807 return jtU;
810 i += delta;
812 if (context_type[i] == jtT)
813 return neighbour_joining_type(i,delta,context_type,cchLen,psa);
814 else
815 return context_type[i];
818 static inline BOOL right_join_causing(CHAR joining_type)
820 return (joining_type == jtL || joining_type == jtD || joining_type == jtC);
823 static inline BOOL left_join_causing(CHAR joining_type)
825 return (joining_type == jtR || joining_type == jtD || joining_type == jtC);
828 /* SHAPE_ShapeArabicGlyphs
830 void SHAPE_ShapeArabicGlyphs(HDC hdc, ScriptCache *psc, SCRIPT_ANALYSIS *psa, WCHAR* pwcChars, INT cChars, WORD* pwOutGlyphs, INT* pcGlyphs, INT cMaxGlyphs)
832 CHAR *context_type;
833 INT *context_shape;
834 INT dirR, dirL;
835 int i;
837 if (psa->eScript != Script_Arabic)
838 return;
840 if (*pcGlyphs != cChars)
842 ERR("Number of Glyphs and Chars need to match at the beginning\n");
843 return;
847 if (!psa->fLogicalOrder && psa->fRTL)
849 dirR = 1;
850 dirL = -1;
852 else
854 dirR = -1;
855 dirL = 1;
858 if (!psc->GSUB_Table)
859 psc->GSUB_Table = load_gsub_table(hdc);
861 context_type = HeapAlloc(GetProcessHeap(),0,cChars);
862 context_shape = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * cChars);
864 for (i = 0; i < cChars; i++)
865 context_type[i] = wine_shaping_table[wine_shaping_table[pwcChars[i] >> 8] + (pwcChars[i] & 0xff)];
867 for (i = 0; i < cChars; i++)
869 if (context_type[i] == jtR && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
870 context_shape[i] = Xr;
871 else if (context_type[i] == jtL && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
872 context_shape[i] = Xl;
873 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)))
874 context_shape[i] = Xm;
875 else if (context_type[i] == jtD && right_join_causing(neighbour_joining_type(i,dirR,context_type,cChars,psa)))
876 context_shape[i] = Xr;
877 else if (context_type[i] == jtD && left_join_causing(neighbour_joining_type(i,dirL,context_type,cChars,psa)))
878 context_shape[i] = Xl;
879 else
880 context_shape[i] = Xn;
883 /* Contextual Shaping */
884 i = 0;
885 while(i < *pcGlyphs)
887 BOOL shaped = FALSE;
889 if (psc->GSUB_Table)
891 INT nextIndex;
892 nextIndex = apply_GSUB_feature_to_glyph(hdc, psa, psc->GSUB_Table, pwOutGlyphs, i, dirL, pcGlyphs, contextual_features[context_shape[i]]);
893 if (nextIndex > GSUB_E_NOGLYPH)
894 i = nextIndex;
895 shaped = (nextIndex > GSUB_E_NOGLYPH);
898 if (!shaped)
900 WORD newGlyph = pwOutGlyphs[i];
901 if (pwcChars[i] >= FIRST_ARABIC_CHAR && pwcChars[i] <= LAST_ARABIC_CHAR)
903 /* fall back to presentation form B */
904 WCHAR context_char = wine_shaping_forms[pwcChars[i] - FIRST_ARABIC_CHAR][context_shape[i]];
905 if (context_char != pwcChars[i] && GetGlyphIndicesW(hdc, &context_char, 1, &newGlyph, 0) != GDI_ERROR && newGlyph != 0x0000)
906 pwOutGlyphs[i] = newGlyph;
908 i++;
912 i = 0;
913 while (arabic_GSUB_features[i] != NULL)
915 apply_GSUB_feature(hdc, psa, psc->GSUB_Table, pwOutGlyphs, dirL, pcGlyphs, arabic_GSUB_features[i]);
916 i++;
919 HeapFree(GetProcessHeap(),0,context_shape);
920 HeapFree(GetProcessHeap(),0,context_type);