2 * Implementation of Shaping for the Uniscribe Script Processor (usp10.dll)
4 * Copyright 2010 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "usp10_internal.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe
);
37 #define FIRST_ARABIC_CHAR 0x0600
38 #define LAST_ARABIC_CHAR 0x06ff
40 extern const unsigned short wine_shaping_table
[];
41 extern const unsigned short wine_shaping_forms
[LAST_ARABIC_CHAR
- FIRST_ARABIC_CHAR
+ 1][4];
59 #ifdef WORDS_BIGENDIAN
60 #define GET_BE_WORD(x) (x)
62 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
65 /* These are all structures needed for the GSUB table */
66 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
67 ( ( (ULONG)_x4 << 24 ) | \
68 ( (ULONG)_x3 << 16 ) | \
69 ( (ULONG)_x2 << 8 ) | \
72 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
73 #define GSUB_E_NOFEATURE -2
74 #define GSUB_E_NOGLYPH -1
90 GSUB_ScriptRecord ScriptRecord
[1];
101 GSUB_LangSysRecord LangSysRecord
[1];
105 WORD LookupOrder
; /* Reserved */
106 WORD ReqFeatureIndex
;
108 WORD FeatureIndex
[1];
114 } GSUB_FeatureRecord
;
118 GSUB_FeatureRecord FeatureRecord
[1];
122 WORD FeatureParams
; /* Reserved */
124 WORD LookupListIndex
[1];
143 } GSUB_CoverageFormat1
;
148 WORD StartCoverageIndex
;
154 GSUB_RangeRecord RangeRecord
[1];
155 } GSUB_CoverageFormat2
;
158 WORD SubstFormat
; /* = 1 */
161 } GSUB_SingleSubstFormat1
;
164 WORD SubstFormat
; /* = 2 */
168 }GSUB_SingleSubstFormat2
;
171 WORD SubstFormat
; /* = 1 */
175 }GSUB_LigatureSubstFormat1
;
190 WORD LookupListIndex
;
192 }GSUB_SubstLookupRecord
;
195 WORD SubstFormat
; /* = 1 */
197 WORD ChainSubRuleSetCount
;
198 WORD ChainSubRuleSet
[1];
199 }GSUB_ChainContextSubstFormat1
;
202 WORD SubstFormat
; /* = 3 */
203 WORD BacktrackGlyphCount
;
205 }GSUB_ChainContextSubstFormat3_1
;
208 WORD InputGlyphCount
;
210 }GSUB_ChainContextSubstFormat3_2
;
213 WORD LookaheadGlyphCount
;
215 }GSUB_ChainContextSubstFormat3_3
;
219 GSUB_SubstLookupRecord SubstLookupRecord
[1];
220 }GSUB_ChainContextSubstFormat3_4
;
223 WORD SubstFormat
; /* = 1 */
225 WORD AlternateSetCount
;
226 WORD AlternateSet
[1];
227 } GSUB_AlternateSubstFormat1
;
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
[] =
245 static const char* arabic_GSUB_features
[] =
256 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
258 const GSUB_CoverageFormat1
* cf1
;
262 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
264 int count
= GET_BE_WORD(cf1
->GlyphCount
);
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
]))
272 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
274 const GSUB_CoverageFormat2
* cf2
;
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
))
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
));
295 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
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
;
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
;
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)
318 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
324 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
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)
340 offset
= GET_BE_WORD(script
->DefaultLangSys
);
343 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
349 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
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
));
369 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
372 TRACE("Single Substitution Subtable\n");
374 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
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;
394 const GSUB_SingleSubstFormat2
*ssf2
;
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
);
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
)
418 TRACE("Alternate Substitution Subtable\n");
420 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
423 const GSUB_AlternateSubstFormat1
*asf1
;
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
]);
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
)
451 TRACE("Ligature Substitution Subtable\n");
452 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
454 const GSUB_LigatureSubstFormat1
*lsf1
;
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
);
464 const GSUB_LigatureSet
*ls
;
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
++)
483 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
484 if (CompGlyph
!= glyphs
[CompIndex
])
486 CompIndex
+= write_dir
;
490 int replaceIdx
= glyph_index
;
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
]);
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
)
517 TRACE("Chaining Contextual Substitution Subtable\n");
518 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
520 const GSUB_ChainContextSubstFormat1
*ccsf1
;
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");
532 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
534 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
537 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
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)
557 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
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)
570 if (k
!= indexGlyphs
)
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)
582 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
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
);
597 ERR("Chain failed to generate a glyph\n");
607 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
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
))
618 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
620 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
622 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
624 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
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
)
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
)
646 if (out_index
== GSUB_E_NOGLYPH
)
647 TRACE("lookups found no glyphs\n");
651 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
655 switch (psa
->eScript
)
671 * fall back to the font charset
673 charset
= GetTextCharsetInfo(hdc
, NULL
, 0x0);
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
;
703 return GSUB_E_NOFEATURE
;
707 script
= GSUB_get_script_table(header
, get_opentype_script(hdc
,psa
));
710 TRACE("Script not found\n");
711 return GSUB_E_NOFEATURE
;
713 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
716 TRACE("Language not found\n");
717 return GSUB_E_NOFEATURE
;
719 feature
= GSUB_get_feature(header
, language
, feat
);
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
);
742 static int apply_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, void* GSUB_Table
, WORD
*pwOutGlyphs
, int write_dir
, INT
* pcGlyphs
, const char* feat
)
748 const GSUB_Header
*header
;
749 const GSUB_Script
*script
;
750 const GSUB_LangSys
*language
;
751 const GSUB_Feature
*feature
;
754 return GSUB_E_NOFEATURE
;
758 script
= GSUB_get_script_table(header
, get_opentype_script(hdc
,psa
));
761 TRACE("Script not found\n");
762 return GSUB_E_NOFEATURE
;
764 language
= GSUB_get_lang_table(script
, "xxxx");
767 TRACE("Language not found\n");
768 return GSUB_E_NOFEATURE
;
770 feature
= GSUB_get_feature(header
, language
, feat
);
773 TRACE("%s feature not found\n",feat
);
774 return GSUB_E_NOFEATURE
;
778 TRACE("applying feature %s\n",feat
);
782 nextIndex
= GSUB_apply_feature(header
, feature
, pwOutGlyphs
, i
, write_dir
, pcGlyphs
);
783 if (nextIndex
> GSUB_E_NOGLYPH
)
790 return GSUB_E_NOFEATURE
;
793 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
797 if (psa
->fLinkBefore
)
802 if ( i
+ delta
>= cchLen
)
812 if (context_type
[i
] == jtT
)
813 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
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
)
837 if (psa
->eScript
!= Script_Arabic
)
840 if (*pcGlyphs
!= cChars
)
842 ERR("Number of Glyphs and Chars need to match at the beginning\n");
847 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
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
;
880 context_shape
[i
] = Xn
;
883 /* Contextual Shaping */
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
)
895 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
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
;
913 while (arabic_GSUB_features
[i
] != NULL
)
915 apply_GSUB_feature(hdc
, psa
, psc
->GSUB_Table
, pwOutGlyphs
, dirL
, pcGlyphs
, arabic_GSUB_features
[i
]);
919 HeapFree(GetProcessHeap(),0,context_shape
);
920 HeapFree(GetProcessHeap(),0,context_type
);