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 const char* required_arabic_features
[] =
285 static OPENTYPE_FEATURE_RECORD hebrew_features
[] =
287 { 0x67696c64 /*dlig*/, 1},
290 static OPENTYPE_FEATURE_RECORD syriac_features
[] =
292 { 0x67696c72 /*rlig*/, 1},
293 { 0x746c6163 /*calt*/, 1},
294 { 0x6167696c /*liga*/, 1},
295 { 0x67696c64 /*dlig*/, 1},
298 static const char* required_syriac_features
[] =
310 static OPENTYPE_FEATURE_RECORD sinhala_features
[] =
313 { 0x6e686b61 /*akhn*/, 1},
314 { 0x66687072 /*rphf*/, 1},
315 { 0x75746176 /*vatu*/, 1},
316 { 0x66747370 /*pstf*/, 1},
317 /* Presentation forms */
318 { 0x73776c62 /*blws*/, 1},
319 { 0x73766261 /*abvs*/, 1},
320 { 0x73747370 /*psts*/, 1},
323 static OPENTYPE_FEATURE_RECORD tibetan_features
[] =
325 { 0x73766261 /*abvs*/, 1},
326 { 0x73776c62 /*blws*/, 1},
329 static OPENTYPE_FEATURE_RECORD thai_features
[] =
331 { 0x706d6363 /*ccmp*/, 1},
334 static const char* required_lao_features
[] =
340 typedef struct ScriptShapeDataTag
{
341 TEXTRANGE_PROPERTIES defaultTextRange
;
342 const char** requiredFeatures
;
344 ContextualShapingProc contextProc
;
347 /* in order of scripts */
348 static const ScriptShapeData ShapingData
[] =
350 {{ standard_features
, 2}, NULL
, "", NULL
},
351 {{ standard_features
, 2}, NULL
, "latn", NULL
},
352 {{ standard_features
, 2}, NULL
, "latn", NULL
},
353 {{ standard_features
, 2}, NULL
, "latn", NULL
},
354 {{ standard_features
, 2}, NULL
, "" , NULL
},
355 {{ standard_features
, 2}, NULL
, "latn", NULL
},
356 {{ arabic_features
, 6}, required_arabic_features
, "arab", ContextualShape_Arabic
},
357 {{ arabic_features
, 6}, required_arabic_features
, "arab", ContextualShape_Arabic
},
358 {{ hebrew_features
, 1}, NULL
, "hebr", NULL
},
359 {{ syriac_features
, 4}, required_syriac_features
, "syrc", ContextualShape_Syriac
},
360 {{ arabic_features
, 6}, required_arabic_features
, "arab", ContextualShape_Arabic
},
361 {{ NULL
, 0}, NULL
, "thaa", NULL
},
362 {{ standard_features
, 2}, NULL
, "grek", NULL
},
363 {{ standard_features
, 2}, NULL
, "cyrl", NULL
},
364 {{ standard_features
, 2}, NULL
, "armn", NULL
},
365 {{ standard_features
, 2}, NULL
, "geor", NULL
},
366 {{ sinhala_features
, 7}, NULL
, "sinh", NULL
},
367 {{ tibetan_features
, 2}, NULL
, "tibt", NULL
},
368 {{ tibetan_features
, 2}, NULL
, "tibt", NULL
},
369 {{ tibetan_features
, 2}, NULL
, "phag", ContextualShape_Phags_pa
},
370 {{ thai_features
, 1}, NULL
, "thai", NULL
},
371 {{ thai_features
, 1}, NULL
, "thai", NULL
},
372 {{ thai_features
, 1}, required_lao_features
, "lao", NULL
},
373 {{ thai_features
, 1}, required_lao_features
, "lao", NULL
},
376 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
378 const GSUB_CoverageFormat1
* cf1
;
382 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
384 int count
= GET_BE_WORD(cf1
->GlyphCount
);
386 TRACE("Coverage Format 1, %i glyphs\n",count
);
387 for (i
= 0; i
< count
; i
++)
388 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
392 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
394 const GSUB_CoverageFormat2
* cf2
;
397 cf2
= (const GSUB_CoverageFormat2
*)cf1
;
399 count
= GET_BE_WORD(cf2
->RangeCount
);
400 TRACE("Coverage Format 2, %i ranges\n",count
);
401 for (i
= 0; i
< count
; i
++)
403 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
405 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
406 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
408 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
409 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
415 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
420 static const GSUB_Script
* GSUB_get_script_table( const GSUB_Header
* header
, const char* tag
)
422 const GSUB_ScriptList
*script
;
423 const GSUB_Script
*deflt
= NULL
;
425 script
= (const GSUB_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
427 TRACE("%i scripts in this font\n",GET_BE_WORD(script
->ScriptCount
));
428 for (i
= 0; i
< GET_BE_WORD(script
->ScriptCount
); i
++)
430 const GSUB_Script
*scr
;
433 offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
434 scr
= (const GSUB_Script
*)((const BYTE
*)script
+ offset
);
436 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, tag
,4)==0)
438 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
444 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
448 const GSUB_LangSys
*Lang
;
450 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script
->DefaultLangSys
), GET_BE_WORD(script
->LangSysCount
));
452 for (i
= 0; i
< GET_BE_WORD(script
->LangSysCount
) ; i
++)
454 offset
= GET_BE_WORD(script
->LangSysRecord
[i
].LangSys
);
455 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
457 if ( strncmp(script
->LangSysRecord
[i
].LangSysTag
,tag
,4)==0)
460 offset
= GET_BE_WORD(script
->DefaultLangSys
);
463 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
469 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
472 const GSUB_FeatureList
*feature
;
473 feature
= (const GSUB_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
475 TRACE("%i features\n",GET_BE_WORD(lang
->FeatureCount
));
476 for (i
= 0; i
< GET_BE_WORD(lang
->FeatureCount
); i
++)
478 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
479 if (strncmp(feature
->FeatureRecord
[index
].FeatureTag
,tag
,4)==0)
481 const GSUB_Feature
*feat
;
482 feat
= (const GSUB_Feature
*)((const BYTE
*)feature
+ GET_BE_WORD(feature
->FeatureRecord
[index
].Feature
));
489 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
492 TRACE("Single Substitution Subtable\n");
494 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
497 const GSUB_SingleSubstFormat1
*ssf1
;
498 offset
= GET_BE_WORD(look
->SubTable
[j
]);
499 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
500 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
502 int offset
= GET_BE_WORD(ssf1
->Coverage
);
503 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
504 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
506 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
507 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
508 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
509 return glyph_index
+ 1;
514 const GSUB_SingleSubstFormat2
*ssf2
;
518 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
519 offset
= GET_BE_WORD(ssf1
->Coverage
);
520 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
521 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
522 TRACE(" Coverage index %i\n",index
);
525 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
526 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
527 TRACE("0x%x\n",glyphs
[glyph_index
]);
528 return glyph_index
+ 1;
532 return GSUB_E_NOGLYPH
;
535 static INT
GSUB_apply_AlternateSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
538 TRACE("Alternate Substitution Subtable\n");
540 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
543 const GSUB_AlternateSubstFormat1
*asf1
;
546 offset
= GET_BE_WORD(look
->SubTable
[j
]);
547 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
548 offset
= GET_BE_WORD(asf1
->Coverage
);
550 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
553 const GSUB_AlternateSet
*as
;
554 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
555 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
556 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
558 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
559 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
560 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
561 return glyph_index
+ 1;
564 return GSUB_E_NOGLYPH
;
567 static INT
GSUB_apply_LigatureSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
571 TRACE("Ligature Substitution Subtable\n");
572 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
574 const GSUB_LigatureSubstFormat1
*lsf1
;
577 offset
= GET_BE_WORD(look
->SubTable
[j
]);
578 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
579 offset
= GET_BE_WORD(lsf1
->Coverage
);
580 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
581 TRACE(" Coverage index %i\n",index
);
584 const GSUB_LigatureSet
*ls
;
587 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
588 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
589 count
= GET_BE_WORD(ls
->LigatureCount
);
590 TRACE(" LigatureSet has %i members\n",count
);
591 for (k
= 0; k
< count
; k
++)
593 const GSUB_Ligature
*lig
;
594 int CompCount
,l
,CompIndex
;
596 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
597 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
598 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
599 CompIndex
= glyph_index
+write_dir
;
600 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
603 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
604 if (CompGlyph
!= glyphs
[CompIndex
])
606 CompIndex
+= write_dir
;
610 int replaceIdx
= glyph_index
;
612 replaceIdx
= glyph_index
- CompCount
;
614 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
615 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
616 TRACE("0x%x\n",glyphs
[replaceIdx
]);
620 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
621 glyphs
[j
] =glyphs
[j
+CompCount
];
622 *glyph_count
= *glyph_count
- CompCount
;
624 return replaceIdx
+ 1;
629 return GSUB_E_NOGLYPH
;
632 static INT
GSUB_apply_ChainContextSubst(const GSUB_LookupList
* lookup
, const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
637 TRACE("Chaining Contextual Substitution Subtable\n");
638 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
640 const GSUB_ChainContextSubstFormat1
*ccsf1
;
642 int dirLookahead
= write_dir
;
643 int dirBacktrack
= -1 * write_dir
;
645 offset
= GET_BE_WORD(look
->SubTable
[j
]);
646 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
647 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
649 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
652 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
654 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
657 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
661 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
662 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
663 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
664 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
665 int newIndex
= glyph_index
;
667 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
669 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
671 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
673 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
674 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
677 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
679 TRACE("Matched Backtrack\n");
681 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
683 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
684 for (k
= 0; k
< indexGlyphs
; k
++)
686 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
687 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
690 if (k
!= indexGlyphs
)
692 TRACE("Matched IndexGlyphs\n");
694 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
696 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
698 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
699 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
+1))]) == -1)
702 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
704 TRACE("Matched LookAhead\n");
706 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
708 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
710 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
711 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
713 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
714 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
717 ERR("Chain failed to generate a glyph\n");
727 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
730 const GSUB_LookupTable
*look
;
732 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
733 look
= (const GSUB_LookupTable
*)((const BYTE
*)lookup
+ offset
);
734 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
735 switch(GET_BE_WORD(look
->LookupType
))
738 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
740 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
742 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
744 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
746 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
748 return GSUB_E_NOGLYPH
;
751 static INT
GSUB_apply_feature(const GSUB_Header
* header
, const GSUB_Feature
* feature
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
754 int out_index
= GSUB_E_NOGLYPH
;
755 const GSUB_LookupList
*lookup
;
757 lookup
= (const GSUB_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
759 TRACE("%i lookups\n", GET_BE_WORD(feature
->LookupCount
));
760 for (i
= 0; i
< GET_BE_WORD(feature
->LookupCount
); i
++)
762 out_index
= GSUB_apply_lookup(lookup
, GET_BE_WORD(feature
->LookupListIndex
[i
]), glyphs
, glyph_index
, write_dir
, glyph_count
);
763 if (out_index
!= GSUB_E_NOGLYPH
)
766 if (out_index
== GSUB_E_NOGLYPH
)
767 TRACE("lookups found no glyphs\n");
771 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
775 if (ShapingData
[psa
->eScript
].otTag
[0] != 0)
776 return ShapingData
[psa
->eScript
].otTag
;
779 * fall back to the font charset
781 charset
= GetTextCharsetInfo(hdc
, NULL
, 0x0);
784 case ANSI_CHARSET
: return "latn";
785 case BALTIC_CHARSET
: return "latn"; /* ?? */
786 case CHINESEBIG5_CHARSET
: return "hani";
787 case EASTEUROPE_CHARSET
: return "latn"; /* ?? */
788 case GB2312_CHARSET
: return "hani";
789 case GREEK_CHARSET
: return "grek";
790 case HANGUL_CHARSET
: return "hang";
791 case RUSSIAN_CHARSET
: return "cyrl";
792 case SHIFTJIS_CHARSET
: return "kana";
793 case TURKISH_CHARSET
: return "latn"; /* ?? */
794 case VIETNAMESE_CHARSET
: return "latn";
795 case JOHAB_CHARSET
: return "latn"; /* ?? */
796 case ARABIC_CHARSET
: return "arab";
797 case HEBREW_CHARSET
: return "hebr";
798 case THAI_CHARSET
: return "thai";
799 default: return "latn";
803 static LPCVOID
load_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, const char* feat
)
805 const GSUB_Feature
*feature
;
808 for (i
= 0; i
< psc
->feature_count
; i
++)
809 if (strncmp(psc
->features
[i
].tag
,feat
,4)==0)
810 return psc
->features
[i
].feature
;
816 const GSUB_Script
*script
;
817 const GSUB_LangSys
*language
;
819 script
= GSUB_get_script_table(psc
->GSUB_Table
, get_opentype_script(hdc
,psa
));
822 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
824 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
827 /* try in the default (latin) table */
830 script
= GSUB_get_script_table(psc
->GSUB_Table
, "latn");
833 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
835 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
840 TRACE("Feature %s located at %p\n",debugstr_an(feat
,4),feature
);
842 psc
->feature_count
++;
845 psc
->features
= HeapReAlloc(GetProcessHeap(), 0, psc
->features
, psc
->feature_count
* sizeof(LoadedFeature
));
847 psc
->features
= HeapAlloc(GetProcessHeap(), 0, psc
->feature_count
* sizeof(LoadedFeature
));
849 lstrcpynA(psc
->features
[psc
->feature_count
- 1].tag
, feat
, 5);
850 psc
->features
[psc
->feature_count
- 1].feature
= feature
;
854 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
)
856 const GSUB_Feature
*feature
;
858 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
860 return GSUB_E_NOFEATURE
;
862 TRACE("applying feature %s\n",feat
);
863 return GSUB_apply_feature(psc
->GSUB_Table
, feature
, glyphs
, index
, write_dir
, glyph_count
);
866 static VOID
*load_gsub_table(HDC hdc
)
868 VOID
* GSUB_Table
= NULL
;
869 int length
= GetFontData(hdc
, GSUB_TAG
, 0, NULL
, 0);
870 if (length
!= GDI_ERROR
)
872 GSUB_Table
= HeapAlloc(GetProcessHeap(),0,length
);
873 GetFontData(hdc
, GSUB_TAG
, 0, GSUB_Table
, length
);
874 TRACE("Loaded GSUB table of %i bytes\n",length
);
879 static void UpdateClusters(int nextIndex
, int changeCount
, int write_dir
, int chars
, WORD
* pwLogClust
)
881 if (changeCount
== 0)
886 int target_glyph
= nextIndex
- 1;
887 int target_index
= -1;
888 int replacing_glyph
= -1;
892 for (i
= 0; i
< chars
; i
++)
894 if (pwLogClust
[i
] == target_glyph
)
901 for (i
= chars
- 1; i
>= 0; i
--)
903 if (pwLogClust
[i
] == target_glyph
)
909 if (target_index
== -1)
911 ERR("Unable to find target glyph\n");
918 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
920 if (pwLogClust
[i
] == target_glyph
)
922 if(pwLogClust
[i
] == replacing_glyph
)
923 pwLogClust
[i
] = target_glyph
;
927 if (changed
>= changeCount
)
929 replacing_glyph
= pwLogClust
[i
];
930 pwLogClust
[i
] = target_glyph
;
938 /* renumber trailing indexes*/
939 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
941 if (pwLogClust
[i
] != target_glyph
)
942 pwLogClust
[i
] += changeCount
;
947 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
)
953 const GSUB_Feature
*feature
;
955 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
957 return GSUB_E_NOFEATURE
;
960 TRACE("applying feature %s\n",debugstr_an(feat
,4));
964 INT prevCount
= *pcGlyphs
;
965 nextIndex
= GSUB_apply_feature(psc
->GSUB_Table
, feature
, pwOutGlyphs
, i
, write_dir
, pcGlyphs
);
966 if (nextIndex
> GSUB_E_NOGLYPH
)
968 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, write_dir
, cChars
, pwLogClust
);
976 return GSUB_E_NOFEATURE
;
979 static WCHAR
neighbour_char(int i
, int delta
, const WCHAR
* chars
, INT cchLen
)
983 if ( i
+ delta
>= cchLen
)
991 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
995 if (psa
->fLinkBefore
)
1000 if ( i
+ delta
>= cchLen
)
1002 if (psa
->fLinkAfter
)
1010 if (context_type
[i
] == jtT
)
1011 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
1013 return context_type
[i
];
1016 static inline BOOL
right_join_causing(CHAR joining_type
)
1018 return (joining_type
== jtL
|| joining_type
== jtD
|| joining_type
== jtC
);
1021 static inline BOOL
left_join_causing(CHAR joining_type
)
1023 return (joining_type
== jtR
|| joining_type
== jtD
|| joining_type
== jtC
);
1026 static inline BOOL
word_break_causing(WCHAR chr
)
1028 /* we are working within a string of characters already guareented to
1029 be within one script, Syriac, so we do not worry about any characers
1030 other than the space character outside of that range */
1031 return (chr
== 0 || chr
== 0x20 );
1035 * ContextualShape_Arabic
1037 static void ContextualShape_Arabic(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1044 if (*pcGlyphs
!= cChars
)
1046 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1050 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1061 if (!psc
->GSUB_Table
)
1062 psc
->GSUB_Table
= load_gsub_table(hdc
);
1064 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1065 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1067 for (i
= 0; i
< cChars
; i
++)
1068 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1070 for (i
= 0; i
< cChars
; i
++)
1072 if (context_type
[i
] == jtR
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1073 context_shape
[i
] = Xr
;
1074 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1075 context_shape
[i
] = Xl
;
1076 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
)))
1077 context_shape
[i
] = Xm
;
1078 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1079 context_shape
[i
] = Xr
;
1080 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1081 context_shape
[i
] = Xl
;
1083 context_shape
[i
] = Xn
;
1086 /* Contextual Shaping */
1088 while(i
< *pcGlyphs
)
1090 BOOL shaped
= FALSE
;
1092 if (psc
->GSUB_Table
)
1095 INT prevCount
= *pcGlyphs
;
1096 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1097 if (nextIndex
> GSUB_E_NOGLYPH
)
1100 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1102 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
1107 WORD newGlyph
= pwOutGlyphs
[i
];
1108 if (pwcChars
[i
] >= FIRST_ARABIC_CHAR
&& pwcChars
[i
] <= LAST_ARABIC_CHAR
)
1110 /* fall back to presentation form B */
1111 WCHAR context_char
= wine_shaping_forms
[pwcChars
[i
] - FIRST_ARABIC_CHAR
][context_shape
[i
]];
1112 if (context_char
!= pwcChars
[i
] && GetGlyphIndicesW(hdc
, &context_char
, 1, &newGlyph
, 0) != GDI_ERROR
&& newGlyph
!= 0x0000)
1113 pwOutGlyphs
[i
] = newGlyph
;
1119 HeapFree(GetProcessHeap(),0,context_shape
);
1120 HeapFree(GetProcessHeap(),0,context_type
);
1124 * ContextualShape_Syriac
1128 #define DALATH 0x715
1131 static void ContextualShape_Syriac(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1138 if (*pcGlyphs
!= cChars
)
1140 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1144 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1155 if (!psc
->GSUB_Table
)
1156 psc
->GSUB_Table
= load_gsub_table(hdc
);
1158 if (!psc
->GSUB_Table
)
1161 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1162 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1164 for (i
= 0; i
< cChars
; i
++)
1165 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1167 for (i
= 0; i
< cChars
; i
++)
1169 if (pwcChars
[i
] == ALAPH
)
1171 WCHAR rchar
= neighbour_char(i
,dirR
,pwcChars
,cChars
);
1173 if (left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1174 context_shape
[i
] = Afj
;
1175 else if ( rchar
!= DALATH
&& rchar
!= RISH
&&
1176 !left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) &&
1177 word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1178 context_shape
[i
] = Afn
;
1179 else if ( (rchar
== DALATH
|| rchar
== RISH
) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1180 context_shape
[i
] = Afx
;
1182 else if (context_type
[i
] == jtR
&&
1183 right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1184 context_shape
[i
] = Xr
;
1185 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1186 context_shape
[i
] = Xl
;
1187 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
)))
1188 context_shape
[i
] = Xm
;
1189 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1190 context_shape
[i
] = Xr
;
1191 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1192 context_shape
[i
] = Xl
;
1194 context_shape
[i
] = Xn
;
1197 /* Contextual Shaping */
1199 while(i
< *pcGlyphs
)
1202 INT prevCount
= *pcGlyphs
;
1203 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1204 if (nextIndex
> GSUB_E_NOGLYPH
)
1206 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1211 HeapFree(GetProcessHeap(),0,context_shape
);
1212 HeapFree(GetProcessHeap(),0,context_type
);
1216 * ContextualShape_Phags_pa
1219 #define phags_pa_CANDRABINDU 0xA873
1220 #define phags_pa_START 0xA840
1221 #define phags_pa_END 0xA87F
1223 static void ContextualShape_Phags_pa(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1229 if (*pcGlyphs
!= cChars
)
1231 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1235 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1246 if (!psc
->GSUB_Table
)
1247 psc
->GSUB_Table
= load_gsub_table(hdc
);
1249 if (!psc
->GSUB_Table
)
1252 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1254 for (i
= 0; i
< cChars
; i
++)
1256 if (pwcChars
[i
] >= phags_pa_START
&& pwcChars
[i
] <= phags_pa_END
)
1258 WCHAR rchar
= neighbour_char(i
,dirR
,pwcChars
,cChars
);
1259 WCHAR lchar
= neighbour_char(i
,dirL
,pwcChars
,cChars
);
1260 BOOL jrchar
= (rchar
!= phags_pa_CANDRABINDU
&& rchar
>= phags_pa_START
&& rchar
<= phags_pa_END
);
1261 BOOL jlchar
= (lchar
!= phags_pa_CANDRABINDU
&& lchar
>= phags_pa_START
&& lchar
<= phags_pa_END
);
1263 if (jrchar
&& jlchar
)
1264 context_shape
[i
] = Xm
;
1266 context_shape
[i
] = Xr
;
1268 context_shape
[i
] = Xl
;
1270 context_shape
[i
] = Xn
;
1273 context_shape
[i
] = -1;
1276 /* Contextual Shaping */
1278 while(i
< *pcGlyphs
)
1280 if (context_shape
[i
] >= 0)
1283 INT prevCount
= *pcGlyphs
;
1284 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1285 if (nextIndex
> GSUB_E_NOGLYPH
)
1287 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1297 HeapFree(GetProcessHeap(),0,context_shape
);
1300 void SHAPE_ContextualShaping(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1302 if (ShapingData
[psa
->eScript
].contextProc
)
1303 ShapingData
[psa
->eScript
].contextProc(hdc
, psc
, psa
, pwcChars
, cChars
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, pwLogClust
);
1306 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
)
1311 if (!rpRangeProperties
)
1314 if (!psc
->GSUB_Table
)
1315 psc
->GSUB_Table
= load_gsub_table(hdc
);
1317 if (!psc
->GSUB_Table
)
1320 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1325 for (i
= 0; i
< rpRangeProperties
->cotfRecords
; i
++)
1327 if (rpRangeProperties
->potfRecords
[i
].lParameter
> 0)
1328 apply_GSUB_feature(hdc
, psa
, psc
, pwOutGlyphs
, dirL
, pcGlyphs
, cChars
, (const char*)&rpRangeProperties
->potfRecords
[i
].tagFeature
, pwLogClust
);
1332 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, INT cChars
, WORD
*pwLogClust
)
1334 const TEXTRANGE_PROPERTIES
*rpRangeProperties
;
1335 rpRangeProperties
= &ShapingData
[psa
->eScript
].defaultTextRange
;
1337 SHAPE_ApplyOpenTypeFeatures(hdc
, psc
, psa
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, cChars
, rpRangeProperties
, pwLogClust
);
1340 HRESULT
SHAPE_CheckFontForRequiredFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
)
1342 const GSUB_Feature
*feature
;
1345 if (!ShapingData
[psa
->eScript
].requiredFeatures
)
1348 if (!psc
->GSUB_Table
)
1349 psc
->GSUB_Table
= load_gsub_table(hdc
);
1351 /* we need to have at least one of the required features */
1353 while (ShapingData
[psa
->eScript
].requiredFeatures
[i
])
1355 feature
= load_GSUB_feature(hdc
, psa
, psc
, ShapingData
[psa
->eScript
].requiredFeatures
[i
]);
1361 return USP_E_SCRIPT_NOT_IN_FONT
;