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
);
45 static void ContextualShape_Phags_pa(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
);
47 extern const unsigned short wine_shaping_table
[];
48 extern const unsigned short wine_shaping_forms
[LAST_ARABIC_CHAR
- FIRST_ARABIC_CHAR
+ 1][4];
70 #ifdef WORDS_BIGENDIAN
71 #define GET_BE_WORD(x) (x)
73 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
76 /* These are all structures needed for the GSUB table */
77 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
78 ( ( (ULONG)_x4 << 24 ) | \
79 ( (ULONG)_x3 << 16 ) | \
80 ( (ULONG)_x2 << 8 ) | \
83 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
84 #define GSUB_E_NOFEATURE -2
85 #define GSUB_E_NOGLYPH -1
101 GSUB_ScriptRecord ScriptRecord
[1];
107 } GSUB_LangSysRecord
;
112 GSUB_LangSysRecord LangSysRecord
[1];
116 WORD LookupOrder
; /* Reserved */
117 WORD ReqFeatureIndex
;
119 WORD FeatureIndex
[1];
125 } GSUB_FeatureRecord
;
129 GSUB_FeatureRecord FeatureRecord
[1];
133 WORD FeatureParams
; /* Reserved */
135 WORD LookupListIndex
[1];
154 } GSUB_CoverageFormat1
;
159 WORD StartCoverageIndex
;
165 GSUB_RangeRecord RangeRecord
[1];
166 } GSUB_CoverageFormat2
;
169 WORD SubstFormat
; /* = 1 */
172 } GSUB_SingleSubstFormat1
;
175 WORD SubstFormat
; /* = 2 */
179 }GSUB_SingleSubstFormat2
;
182 WORD SubstFormat
; /* = 1 */
186 }GSUB_LigatureSubstFormat1
;
201 WORD LookupListIndex
;
203 }GSUB_SubstLookupRecord
;
206 WORD SubstFormat
; /* = 1 */
208 WORD ChainSubRuleSetCount
;
209 WORD ChainSubRuleSet
[1];
210 }GSUB_ChainContextSubstFormat1
;
213 WORD SubstFormat
; /* = 3 */
214 WORD BacktrackGlyphCount
;
216 }GSUB_ChainContextSubstFormat3_1
;
219 WORD InputGlyphCount
;
221 }GSUB_ChainContextSubstFormat3_2
;
224 WORD LookaheadGlyphCount
;
226 }GSUB_ChainContextSubstFormat3_3
;
230 GSUB_SubstLookupRecord SubstLookupRecord
[1];
231 }GSUB_ChainContextSubstFormat3_4
;
234 WORD SubstFormat
; /* = 1 */
236 WORD AlternateSetCount
;
237 WORD AlternateSet
[1];
238 } GSUB_AlternateSubstFormat1
;
245 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
);
247 /* the orders of joined_forms and contextual_features need to line up */
248 static const char* contextual_features
[] =
260 static OPENTYPE_FEATURE_RECORD standard_features
[] =
262 { 0x6167696c /*liga*/, 1},
263 { 0x67696c63 /*clig*/, 1},
266 static OPENTYPE_FEATURE_RECORD arabic_features
[] =
268 { 0x67696c72 /*rlig*/, 1},
269 { 0x746c6163 /*calt*/, 1},
270 { 0x6167696c /*liga*/, 1},
271 { 0x67696c64 /*dlig*/, 1},
272 { 0x68777363 /*cswh*/, 1},
273 { 0x7465736d /*mset*/, 1},
276 static OPENTYPE_FEATURE_RECORD hebrew_features
[] =
278 { 0x67696c64 /*dlig*/, 1},
281 static OPENTYPE_FEATURE_RECORD syriac_features
[] =
283 { 0x67696c72 /*rlig*/, 1},
284 { 0x746c6163 /*calt*/, 1},
285 { 0x6167696c /*liga*/, 1},
286 { 0x67696c64 /*dlig*/, 1},
289 static OPENTYPE_FEATURE_RECORD sinhala_features
[] =
292 { 0x6e686b61 /*akhn*/, 1},
293 { 0x66687072 /*rphf*/, 1},
294 { 0x75746176 /*vatu*/, 1},
295 { 0x66747370 /*pstf*/, 1},
296 /* Presentation forms */
297 { 0x73776c62 /*blws*/, 1},
298 { 0x73766261 /*abvs*/, 1},
299 { 0x73747370 /*psts*/, 1},
302 static OPENTYPE_FEATURE_RECORD tibetan_features
[] =
304 { 0x73766261 /*abvs*/, 1},
305 { 0x73776c62 /*blws*/, 1},
308 static OPENTYPE_FEATURE_RECORD thai_features
[] =
310 { 0x706d6363 /*ccmp*/, 1},
313 typedef struct ScriptShapeDataTag
{
314 TEXTRANGE_PROPERTIES defaultTextRange
;
316 ContextualShapingProc contextProc
;
319 /* in order of scripts */
320 static const ScriptShapeData ShapingData
[] =
322 {{ standard_features
, 2}, "", NULL
},
323 {{ standard_features
, 2}, "latn", NULL
},
324 {{ standard_features
, 2}, "latn", NULL
},
325 {{ standard_features
, 2}, "latn", NULL
},
326 {{ standard_features
, 2}, "" , NULL
},
327 {{ standard_features
, 2}, "latn", NULL
},
328 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
329 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
330 {{ hebrew_features
, 1}, "hebr", NULL
},
331 {{ syriac_features
, 4}, "syrc", ContextualShape_Syriac
},
332 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
333 {{ NULL
, 0}, "thaa", NULL
},
334 {{ standard_features
, 2}, "grek", NULL
},
335 {{ standard_features
, 2}, "cyrl", NULL
},
336 {{ standard_features
, 2}, "armn", NULL
},
337 {{ standard_features
, 2}, "geor", NULL
},
338 {{ sinhala_features
, 7}, "sinh", NULL
},
339 {{ tibetan_features
, 2}, "tibt", NULL
},
340 {{ tibetan_features
, 2}, "tibt", NULL
},
341 {{ tibetan_features
, 2}, "phag", ContextualShape_Phags_pa
},
342 {{ thai_features
, 1}, "thai", NULL
},
343 {{ thai_features
, 1}, "thai", NULL
},
344 {{ thai_features
, 1}, "lao", NULL
},
345 {{ thai_features
, 1}, "lao", NULL
},
348 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
350 const GSUB_CoverageFormat1
* cf1
;
354 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
356 int count
= GET_BE_WORD(cf1
->GlyphCount
);
358 TRACE("Coverage Format 1, %i glyphs\n",count
);
359 for (i
= 0; i
< count
; i
++)
360 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
364 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
366 const GSUB_CoverageFormat2
* cf2
;
369 cf2
= (const GSUB_CoverageFormat2
*)cf1
;
371 count
= GET_BE_WORD(cf2
->RangeCount
);
372 TRACE("Coverage Format 2, %i ranges\n",count
);
373 for (i
= 0; i
< count
; i
++)
375 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
377 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
378 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
380 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
381 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
387 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
392 static const GSUB_Script
* GSUB_get_script_table( const GSUB_Header
* header
, const char* tag
)
394 const GSUB_ScriptList
*script
;
395 const GSUB_Script
*deflt
= NULL
;
397 script
= (const GSUB_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
399 TRACE("%i scripts in this font\n",GET_BE_WORD(script
->ScriptCount
));
400 for (i
= 0; i
< GET_BE_WORD(script
->ScriptCount
); i
++)
402 const GSUB_Script
*scr
;
405 offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
406 scr
= (const GSUB_Script
*)((const BYTE
*)script
+ offset
);
408 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, tag
,4)==0)
410 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
416 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
420 const GSUB_LangSys
*Lang
;
422 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script
->DefaultLangSys
), GET_BE_WORD(script
->LangSysCount
));
424 for (i
= 0; i
< GET_BE_WORD(script
->LangSysCount
) ; i
++)
426 offset
= GET_BE_WORD(script
->LangSysRecord
[i
].LangSys
);
427 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
429 if ( strncmp(script
->LangSysRecord
[i
].LangSysTag
,tag
,4)==0)
432 offset
= GET_BE_WORD(script
->DefaultLangSys
);
435 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
441 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
444 const GSUB_FeatureList
*feature
;
445 feature
= (const GSUB_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
447 TRACE("%i features\n",GET_BE_WORD(lang
->FeatureCount
));
448 for (i
= 0; i
< GET_BE_WORD(lang
->FeatureCount
); i
++)
450 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
451 if (strncmp(feature
->FeatureRecord
[index
].FeatureTag
,tag
,4)==0)
453 const GSUB_Feature
*feat
;
454 feat
= (const GSUB_Feature
*)((const BYTE
*)feature
+ GET_BE_WORD(feature
->FeatureRecord
[index
].Feature
));
461 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
464 TRACE("Single Substitution Subtable\n");
466 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
469 const GSUB_SingleSubstFormat1
*ssf1
;
470 offset
= GET_BE_WORD(look
->SubTable
[j
]);
471 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
472 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
474 int offset
= GET_BE_WORD(ssf1
->Coverage
);
475 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
476 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
478 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
479 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
480 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
481 return glyph_index
+ 1;
486 const GSUB_SingleSubstFormat2
*ssf2
;
490 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
491 offset
= GET_BE_WORD(ssf1
->Coverage
);
492 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
493 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
494 TRACE(" Coverage index %i\n",index
);
497 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
498 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
499 TRACE("0x%x\n",glyphs
[glyph_index
]);
500 return glyph_index
+ 1;
504 return GSUB_E_NOGLYPH
;
507 static INT
GSUB_apply_AlternateSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
510 TRACE("Alternate Substitution Subtable\n");
512 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
515 const GSUB_AlternateSubstFormat1
*asf1
;
518 offset
= GET_BE_WORD(look
->SubTable
[j
]);
519 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
520 offset
= GET_BE_WORD(asf1
->Coverage
);
522 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
525 const GSUB_AlternateSet
*as
;
526 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
527 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
528 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
530 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
531 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
532 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
533 return glyph_index
+ 1;
536 return GSUB_E_NOGLYPH
;
539 static INT
GSUB_apply_LigatureSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
543 TRACE("Ligature Substitution Subtable\n");
544 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
546 const GSUB_LigatureSubstFormat1
*lsf1
;
549 offset
= GET_BE_WORD(look
->SubTable
[j
]);
550 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
551 offset
= GET_BE_WORD(lsf1
->Coverage
);
552 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
553 TRACE(" Coverage index %i\n",index
);
556 const GSUB_LigatureSet
*ls
;
559 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
560 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
561 count
= GET_BE_WORD(ls
->LigatureCount
);
562 TRACE(" LigatureSet has %i members\n",count
);
563 for (k
= 0; k
< count
; k
++)
565 const GSUB_Ligature
*lig
;
566 int CompCount
,l
,CompIndex
;
568 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
569 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
570 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
571 CompIndex
= glyph_index
+write_dir
;
572 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
575 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
576 if (CompGlyph
!= glyphs
[CompIndex
])
578 CompIndex
+= write_dir
;
582 int replaceIdx
= glyph_index
;
584 replaceIdx
= glyph_index
- CompCount
;
586 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
587 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
588 TRACE("0x%x\n",glyphs
[replaceIdx
]);
592 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
593 glyphs
[j
] =glyphs
[j
+CompCount
];
594 *glyph_count
= *glyph_count
- CompCount
;
596 return replaceIdx
+ 1;
601 return GSUB_E_NOGLYPH
;
604 static INT
GSUB_apply_ChainContextSubst(const GSUB_LookupList
* lookup
, const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
609 TRACE("Chaining Contextual Substitution Subtable\n");
610 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
612 const GSUB_ChainContextSubstFormat1
*ccsf1
;
614 int dirLookahead
= write_dir
;
615 int dirBacktrack
= -1 * write_dir
;
617 offset
= GET_BE_WORD(look
->SubTable
[j
]);
618 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
619 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
621 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
624 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
626 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
629 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
633 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
634 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
635 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
636 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
637 int newIndex
= glyph_index
;
639 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
641 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
643 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
645 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
646 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
649 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
651 TRACE("Matched Backtrack\n");
653 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
655 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
656 for (k
= 0; k
< indexGlyphs
; k
++)
658 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
659 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
662 if (k
!= indexGlyphs
)
664 TRACE("Matched IndexGlyphs\n");
666 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
668 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
670 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
671 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
+1))]) == -1)
674 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
676 TRACE("Matched LookAhead\n");
678 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
680 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
682 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
683 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
685 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
686 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
689 ERR("Chain failed to generate a glyph\n");
699 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
702 const GSUB_LookupTable
*look
;
704 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
705 look
= (const GSUB_LookupTable
*)((const BYTE
*)lookup
+ offset
);
706 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
707 switch(GET_BE_WORD(look
->LookupType
))
710 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
712 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
714 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
716 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
718 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
720 return GSUB_E_NOGLYPH
;
723 static INT
GSUB_apply_feature(const GSUB_Header
* header
, const GSUB_Feature
* feature
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
726 int out_index
= GSUB_E_NOGLYPH
;
727 const GSUB_LookupList
*lookup
;
729 lookup
= (const GSUB_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
731 TRACE("%i lookups\n", GET_BE_WORD(feature
->LookupCount
));
732 for (i
= 0; i
< GET_BE_WORD(feature
->LookupCount
); i
++)
734 out_index
= GSUB_apply_lookup(lookup
, GET_BE_WORD(feature
->LookupListIndex
[i
]), glyphs
, glyph_index
, write_dir
, glyph_count
);
735 if (out_index
!= GSUB_E_NOGLYPH
)
738 if (out_index
== GSUB_E_NOGLYPH
)
739 TRACE("lookups found no glyphs\n");
743 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
747 if (ShapingData
[psa
->eScript
].otTag
[0] != 0)
748 return ShapingData
[psa
->eScript
].otTag
;
751 * fall back to the font charset
753 charset
= GetTextCharsetInfo(hdc
, NULL
, 0x0);
756 case ANSI_CHARSET
: return "latn";
757 case BALTIC_CHARSET
: return "latn"; /* ?? */
758 case CHINESEBIG5_CHARSET
: return "hani";
759 case EASTEUROPE_CHARSET
: return "latn"; /* ?? */
760 case GB2312_CHARSET
: return "hani";
761 case GREEK_CHARSET
: return "grek";
762 case HANGUL_CHARSET
: return "hang";
763 case RUSSIAN_CHARSET
: return "cyrl";
764 case SHIFTJIS_CHARSET
: return "kana";
765 case TURKISH_CHARSET
: return "latn"; /* ?? */
766 case VIETNAMESE_CHARSET
: return "latn";
767 case JOHAB_CHARSET
: return "latn"; /* ?? */
768 case ARABIC_CHARSET
: return "arab";
769 case HEBREW_CHARSET
: return "hebr";
770 case THAI_CHARSET
: return "thai";
771 default: return "latn";
775 static LPCVOID
load_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, const char* feat
)
777 const GSUB_Feature
*feature
;
780 for (i
= 0; i
< psc
->feature_count
; i
++)
781 if (strncmp(psc
->features
[i
].tag
,feat
,4)==0)
782 return psc
->features
[i
].feature
;
788 const GSUB_Script
*script
;
789 const GSUB_LangSys
*language
;
791 script
= GSUB_get_script_table(psc
->GSUB_Table
, get_opentype_script(hdc
,psa
));
794 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
796 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
799 /* try in the default (latin) table */
802 script
= GSUB_get_script_table(psc
->GSUB_Table
, "latn");
805 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
807 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
812 TRACE("Feature %s located at %p\n",debugstr_an(feat
,4),feature
);
814 psc
->feature_count
++;
817 psc
->features
= HeapReAlloc(GetProcessHeap(), 0, psc
->features
, psc
->feature_count
* sizeof(LoadedFeature
));
819 psc
->features
= HeapAlloc(GetProcessHeap(), 0, psc
->feature_count
* sizeof(LoadedFeature
));
821 lstrcpynA(psc
->features
[psc
->feature_count
- 1].tag
, feat
, 5);
822 psc
->features
[psc
->feature_count
- 1].feature
= feature
;
826 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
)
828 const GSUB_Feature
*feature
;
830 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
832 return GSUB_E_NOFEATURE
;
834 TRACE("applying feature %s\n",feat
);
835 return GSUB_apply_feature(psc
->GSUB_Table
, feature
, glyphs
, index
, write_dir
, glyph_count
);
838 static VOID
*load_gsub_table(HDC hdc
)
840 VOID
* GSUB_Table
= NULL
;
841 int length
= GetFontData(hdc
, GSUB_TAG
, 0, NULL
, 0);
842 if (length
!= GDI_ERROR
)
844 GSUB_Table
= HeapAlloc(GetProcessHeap(),0,length
);
845 GetFontData(hdc
, GSUB_TAG
, 0, GSUB_Table
, length
);
846 TRACE("Loaded GSUB table of %i bytes\n",length
);
851 static void UpdateClusters(int nextIndex
, int changeCount
, int write_dir
, int chars
, WORD
* pwLogClust
)
853 if (changeCount
== 0)
858 int target_glyph
= nextIndex
- 1;
859 int target_index
= -1;
860 int replacing_glyph
= -1;
864 for (i
= 0; i
< chars
; i
++)
866 if (pwLogClust
[i
] == target_glyph
)
873 for (i
= chars
- 1; i
>= 0; i
--)
875 if (pwLogClust
[i
] == target_glyph
)
881 if (target_index
== -1)
883 ERR("Unable to find target glyph\n");
890 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
892 if (pwLogClust
[i
] == target_glyph
)
894 if(pwLogClust
[i
] == replacing_glyph
)
895 pwLogClust
[i
] = target_glyph
;
899 if (changed
>= changeCount
)
901 replacing_glyph
= pwLogClust
[i
];
902 pwLogClust
[i
] = target_glyph
;
910 /* renumber trailing indexes*/
911 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
913 if (pwLogClust
[i
] != target_glyph
)
914 pwLogClust
[i
] += changeCount
;
919 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
)
925 const GSUB_Feature
*feature
;
927 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
929 return GSUB_E_NOFEATURE
;
932 TRACE("applying feature %s\n",debugstr_an(feat
,4));
936 INT prevCount
= *pcGlyphs
;
937 nextIndex
= GSUB_apply_feature(psc
->GSUB_Table
, feature
, pwOutGlyphs
, i
, write_dir
, pcGlyphs
);
938 if (nextIndex
> GSUB_E_NOGLYPH
)
940 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, write_dir
, cChars
, pwLogClust
);
948 return GSUB_E_NOFEATURE
;
951 static WCHAR
neighbour_char(int i
, int delta
, const WCHAR
* chars
, INT cchLen
)
955 if ( i
+ delta
>= cchLen
)
963 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
967 if (psa
->fLinkBefore
)
972 if ( i
+ delta
>= cchLen
)
982 if (context_type
[i
] == jtT
)
983 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
985 return context_type
[i
];
988 static inline BOOL
right_join_causing(CHAR joining_type
)
990 return (joining_type
== jtL
|| joining_type
== jtD
|| joining_type
== jtC
);
993 static inline BOOL
left_join_causing(CHAR joining_type
)
995 return (joining_type
== jtR
|| joining_type
== jtD
|| joining_type
== jtC
);
998 static inline BOOL
word_break_causing(WCHAR chr
)
1000 /* we are working within a string of characters already guareented to
1001 be within one script, Syriac, so we do not worry about any characers
1002 other than the space character outside of that range */
1003 return (chr
== 0 || chr
== 0x20 );
1007 * ContextualShape_Arabic
1009 static void ContextualShape_Arabic(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1016 if (*pcGlyphs
!= cChars
)
1018 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1022 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1033 if (!psc
->GSUB_Table
)
1034 psc
->GSUB_Table
= load_gsub_table(hdc
);
1036 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1037 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1039 for (i
= 0; i
< cChars
; i
++)
1040 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1042 for (i
= 0; i
< cChars
; i
++)
1044 if (context_type
[i
] == jtR
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1045 context_shape
[i
] = Xr
;
1046 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1047 context_shape
[i
] = Xl
;
1048 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
)))
1049 context_shape
[i
] = Xm
;
1050 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1051 context_shape
[i
] = Xr
;
1052 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1053 context_shape
[i
] = Xl
;
1055 context_shape
[i
] = Xn
;
1058 /* Contextual Shaping */
1060 while(i
< *pcGlyphs
)
1062 BOOL shaped
= FALSE
;
1064 if (psc
->GSUB_Table
)
1067 INT prevCount
= *pcGlyphs
;
1068 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1069 if (nextIndex
> GSUB_E_NOGLYPH
)
1072 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1074 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
1079 WORD newGlyph
= pwOutGlyphs
[i
];
1080 if (pwcChars
[i
] >= FIRST_ARABIC_CHAR
&& pwcChars
[i
] <= LAST_ARABIC_CHAR
)
1082 /* fall back to presentation form B */
1083 WCHAR context_char
= wine_shaping_forms
[pwcChars
[i
] - FIRST_ARABIC_CHAR
][context_shape
[i
]];
1084 if (context_char
!= pwcChars
[i
] && GetGlyphIndicesW(hdc
, &context_char
, 1, &newGlyph
, 0) != GDI_ERROR
&& newGlyph
!= 0x0000)
1085 pwOutGlyphs
[i
] = newGlyph
;
1091 HeapFree(GetProcessHeap(),0,context_shape
);
1092 HeapFree(GetProcessHeap(),0,context_type
);
1096 * ContextualShape_Syriac
1100 #define DALATH 0x715
1103 static void ContextualShape_Syriac(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1110 if (*pcGlyphs
!= cChars
)
1112 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1116 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1127 if (!psc
->GSUB_Table
)
1128 psc
->GSUB_Table
= load_gsub_table(hdc
);
1130 if (!psc
->GSUB_Table
)
1133 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1134 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1136 for (i
= 0; i
< cChars
; i
++)
1137 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1139 for (i
= 0; i
< cChars
; i
++)
1141 if (pwcChars
[i
] == ALAPH
)
1143 WCHAR rchar
= neighbour_char(i
,dirR
,pwcChars
,cChars
);
1145 if (left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1146 context_shape
[i
] = Afj
;
1147 else if ( rchar
!= DALATH
&& rchar
!= RISH
&&
1148 !left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) &&
1149 word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1150 context_shape
[i
] = Afn
;
1151 else if ( (rchar
== DALATH
|| rchar
== RISH
) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1152 context_shape
[i
] = Afx
;
1154 else if (context_type
[i
] == jtR
&&
1155 right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1156 context_shape
[i
] = Xr
;
1157 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1158 context_shape
[i
] = Xl
;
1159 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
)))
1160 context_shape
[i
] = Xm
;
1161 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1162 context_shape
[i
] = Xr
;
1163 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1164 context_shape
[i
] = Xl
;
1166 context_shape
[i
] = Xn
;
1169 /* Contextual Shaping */
1171 while(i
< *pcGlyphs
)
1174 INT prevCount
= *pcGlyphs
;
1175 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1176 if (nextIndex
> GSUB_E_NOGLYPH
)
1178 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1183 HeapFree(GetProcessHeap(),0,context_shape
);
1184 HeapFree(GetProcessHeap(),0,context_type
);
1188 * ContextualShape_Phags_pa
1191 #define phags_pa_CANDRABINDU 0xA873
1192 #define phags_pa_START 0xA840
1193 #define phags_pa_END 0xA87F
1195 static void ContextualShape_Phags_pa(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1201 if (*pcGlyphs
!= cChars
)
1203 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1207 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1218 if (!psc
->GSUB_Table
)
1219 psc
->GSUB_Table
= load_gsub_table(hdc
);
1221 if (!psc
->GSUB_Table
)
1224 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1226 for (i
= 0; i
< cChars
; i
++)
1228 if (pwcChars
[i
] >= phags_pa_START
&& pwcChars
[i
] <= phags_pa_END
)
1230 WCHAR rchar
= neighbour_char(i
,dirR
,pwcChars
,cChars
);
1231 WCHAR lchar
= neighbour_char(i
,dirL
,pwcChars
,cChars
);
1232 BOOL jrchar
= (rchar
!= phags_pa_CANDRABINDU
&& rchar
>= phags_pa_START
&& rchar
<= phags_pa_END
);
1233 BOOL jlchar
= (lchar
!= phags_pa_CANDRABINDU
&& lchar
>= phags_pa_START
&& lchar
<= phags_pa_END
);
1235 if (jrchar
&& jlchar
)
1236 context_shape
[i
] = Xm
;
1238 context_shape
[i
] = Xr
;
1240 context_shape
[i
] = Xl
;
1242 context_shape
[i
] = Xn
;
1245 context_shape
[i
] = -1;
1248 /* Contextual Shaping */
1250 while(i
< *pcGlyphs
)
1252 if (context_shape
[i
] >= 0)
1255 INT prevCount
= *pcGlyphs
;
1256 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1257 if (nextIndex
> GSUB_E_NOGLYPH
)
1259 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1269 HeapFree(GetProcessHeap(),0,context_shape
);
1272 void SHAPE_ContextualShaping(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1274 if (ShapingData
[psa
->eScript
].contextProc
)
1275 ShapingData
[psa
->eScript
].contextProc(hdc
, psc
, psa
, pwcChars
, cChars
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, pwLogClust
);
1278 static void SHAPE_ApplyOpenTypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, INT cChars
, const TEXTRANGE_PROPERTIES
*rpRangeProperties
, WORD
*pwLogClust
)
1283 if (!rpRangeProperties
)
1286 if (!psc
->GSUB_Table
)
1287 psc
->GSUB_Table
= load_gsub_table(hdc
);
1289 if (!psc
->GSUB_Table
)
1292 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1297 for (i
= 0; i
< rpRangeProperties
->cotfRecords
; i
++)
1299 if (rpRangeProperties
->potfRecords
[i
].lParameter
> 0)
1300 apply_GSUB_feature(hdc
, psa
, psc
, pwOutGlyphs
, dirL
, pcGlyphs
, cChars
, (const char*)&rpRangeProperties
->potfRecords
[i
].tagFeature
, pwLogClust
);
1304 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, INT cChars
, WORD
*pwLogClust
)
1306 const TEXTRANGE_PROPERTIES
*rpRangeProperties
;
1307 rpRangeProperties
= &ShapingData
[psa
->eScript
].defaultTextRange
;
1309 SHAPE_ApplyOpenTypeFeatures(hdc
, psc
, psa
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, cChars
, rpRangeProperties
, pwLogClust
);