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 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];
69 #ifdef WORDS_BIGENDIAN
70 #define GET_BE_WORD(x) (x)
72 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
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 ) | \
82 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
83 #define GSUB_E_NOFEATURE -2
84 #define GSUB_E_NOGLYPH -1
100 GSUB_ScriptRecord ScriptRecord
[1];
106 } GSUB_LangSysRecord
;
111 GSUB_LangSysRecord LangSysRecord
[1];
115 WORD LookupOrder
; /* Reserved */
116 WORD ReqFeatureIndex
;
118 WORD FeatureIndex
[1];
124 } GSUB_FeatureRecord
;
128 GSUB_FeatureRecord FeatureRecord
[1];
132 WORD FeatureParams
; /* Reserved */
134 WORD LookupListIndex
[1];
153 } GSUB_CoverageFormat1
;
158 WORD StartCoverageIndex
;
164 GSUB_RangeRecord RangeRecord
[1];
165 } GSUB_CoverageFormat2
;
168 WORD SubstFormat
; /* = 1 */
171 } GSUB_SingleSubstFormat1
;
174 WORD SubstFormat
; /* = 2 */
178 }GSUB_SingleSubstFormat2
;
181 WORD SubstFormat
; /* = 1 */
185 }GSUB_LigatureSubstFormat1
;
200 WORD LookupListIndex
;
202 }GSUB_SubstLookupRecord
;
205 WORD SubstFormat
; /* = 1 */
207 WORD ChainSubRuleSetCount
;
208 WORD ChainSubRuleSet
[1];
209 }GSUB_ChainContextSubstFormat1
;
212 WORD SubstFormat
; /* = 3 */
213 WORD BacktrackGlyphCount
;
215 }GSUB_ChainContextSubstFormat3_1
;
218 WORD InputGlyphCount
;
220 }GSUB_ChainContextSubstFormat3_2
;
223 WORD LookaheadGlyphCount
;
225 }GSUB_ChainContextSubstFormat3_3
;
229 GSUB_SubstLookupRecord SubstLookupRecord
[1];
230 }GSUB_ChainContextSubstFormat3_4
;
233 WORD SubstFormat
; /* = 1 */
235 WORD AlternateSetCount
;
236 WORD AlternateSet
[1];
237 } GSUB_AlternateSubstFormat1
;
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
[] =
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
;
291 ContextualShapingProc contextProc
;
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
;
316 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
318 int count
= GET_BE_WORD(cf1
->GlyphCount
);
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
]))
326 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
328 const GSUB_CoverageFormat2
* cf2
;
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
))
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
));
349 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
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
;
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
;
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)
372 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
378 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
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)
394 offset
= GET_BE_WORD(script
->DefaultLangSys
);
397 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
403 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
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
));
423 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
426 TRACE("Single Substitution Subtable\n");
428 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
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;
448 const GSUB_SingleSubstFormat2
*ssf2
;
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
);
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
)
472 TRACE("Alternate Substitution Subtable\n");
474 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
477 const GSUB_AlternateSubstFormat1
*asf1
;
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
]);
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
)
505 TRACE("Ligature Substitution Subtable\n");
506 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
508 const GSUB_LigatureSubstFormat1
*lsf1
;
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
);
518 const GSUB_LigatureSet
*ls
;
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
++)
537 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
538 if (CompGlyph
!= glyphs
[CompIndex
])
540 CompIndex
+= write_dir
;
544 int replaceIdx
= glyph_index
;
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
]);
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
)
571 TRACE("Chaining Contextual Substitution Subtable\n");
572 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
574 const GSUB_ChainContextSubstFormat1
*ccsf1
;
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");
586 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
588 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
591 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
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)
611 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
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)
624 if (k
!= indexGlyphs
)
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)
636 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
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
);
651 ERR("Chain failed to generate a glyph\n");
661 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
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
))
672 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
674 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
676 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
678 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
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
)
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
)
700 if (out_index
== GSUB_E_NOGLYPH
)
701 TRACE("lookups found no glyphs\n");
705 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
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);
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
;
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
;
750 const GSUB_Script
*script
;
751 const GSUB_LangSys
*language
;
753 script
= GSUB_get_script_table(psc
->GSUB_Table
, get_opentype_script(hdc
,psa
));
756 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
758 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
761 /* try in the default (latin) table */
764 script
= GSUB_get_script_table(psc
->GSUB_Table
, "latn");
767 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
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
++;
779 psc
->features
= HeapReAlloc(GetProcessHeap(), 0, psc
->features
, psc
->feature_count
* sizeof(LoadedFeature
));
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
;
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
);
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
);
813 static void UpdateClusters(int nextIndex
, int changeCount
, int write_dir
, int chars
, WORD
* pwLogClust
)
815 if (changeCount
== 0)
820 int target_glyph
= nextIndex
- 1;
821 int target_index
= -1;
822 int replacing_glyph
= -1;
826 for (i
= 0; i
< chars
; i
++)
828 if (pwLogClust
[i
] == target_glyph
)
835 for (i
= chars
- 1; i
>= 0; i
--)
837 if (pwLogClust
[i
] == target_glyph
)
843 if (target_index
== -1)
845 ERR("Unable to find target glyph\n");
852 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
854 if (pwLogClust
[i
] == target_glyph
)
856 if(pwLogClust
[i
] == replacing_glyph
)
857 pwLogClust
[i
] = target_glyph
;
861 if (changed
>= changeCount
)
863 replacing_glyph
= pwLogClust
[i
];
864 pwLogClust
[i
] = target_glyph
;
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
)
887 const GSUB_Feature
*feature
;
889 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
891 return GSUB_E_NOFEATURE
;
894 TRACE("applying feature %s\n",debugstr_an(feat
,4));
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
);
910 return GSUB_E_NOFEATURE
;
913 static WCHAR
neighbour_char(int i
, int delta
, const WCHAR
* chars
, INT cchLen
)
917 if ( i
+ delta
>= cchLen
)
925 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
929 if (psa
->fLinkBefore
)
934 if ( i
+ delta
>= cchLen
)
944 if (context_type
[i
] == jtT
)
945 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
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
)
978 if (*pcGlyphs
!= cChars
)
980 ERR("Number of Glyphs and Chars need to match at the beginning\n");
984 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
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
;
1017 context_shape
[i
] = Xn
;
1020 /* Contextual Shaping */
1022 while(i
< *pcGlyphs
)
1024 BOOL shaped
= FALSE
;
1026 if (psc
->GSUB_Table
)
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
)
1034 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1036 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
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
;
1053 HeapFree(GetProcessHeap(),0,context_shape
);
1054 HeapFree(GetProcessHeap(),0,context_type
);
1058 * ContextualShape_Syriac
1062 #define DALATH 0x715
1065 static void ContextualShape_Syriac(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1072 if (*pcGlyphs
!= cChars
)
1074 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1078 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1089 if (!psc
->GSUB_Table
)
1090 psc
->GSUB_Table
= load_gsub_table(hdc
);
1092 if (!psc
->GSUB_Table
)
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
;
1128 context_shape
[i
] = Xn
;
1131 /* Contextual Shaping */
1133 while(i
< *pcGlyphs
)
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
);
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
)
1160 if (!rpRangeProperties
)
1163 if (!psc
->GSUB_Table
)
1164 psc
->GSUB_Table
= load_gsub_table(hdc
);
1166 if (!psc
->GSUB_Table
)
1169 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
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
);