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
},
308 {{ NULL
, 0}, "thaa", NULL
},
311 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
313 const GSUB_CoverageFormat1
* cf1
;
317 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
319 int count
= GET_BE_WORD(cf1
->GlyphCount
);
321 TRACE("Coverage Format 1, %i glyphs\n",count
);
322 for (i
= 0; i
< count
; i
++)
323 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
327 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
329 const GSUB_CoverageFormat2
* cf2
;
332 cf2
= (const GSUB_CoverageFormat2
*)cf1
;
334 count
= GET_BE_WORD(cf2
->RangeCount
);
335 TRACE("Coverage Format 2, %i ranges\n",count
);
336 for (i
= 0; i
< count
; i
++)
338 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
340 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
341 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
343 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
344 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
350 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
355 static const GSUB_Script
* GSUB_get_script_table( const GSUB_Header
* header
, const char* tag
)
357 const GSUB_ScriptList
*script
;
358 const GSUB_Script
*deflt
= NULL
;
360 script
= (const GSUB_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
362 TRACE("%i scripts in this font\n",GET_BE_WORD(script
->ScriptCount
));
363 for (i
= 0; i
< GET_BE_WORD(script
->ScriptCount
); i
++)
365 const GSUB_Script
*scr
;
368 offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
369 scr
= (const GSUB_Script
*)((const BYTE
*)script
+ offset
);
371 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, tag
,4)==0)
373 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
379 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
383 const GSUB_LangSys
*Lang
;
385 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script
->DefaultLangSys
), GET_BE_WORD(script
->LangSysCount
));
387 for (i
= 0; i
< GET_BE_WORD(script
->LangSysCount
) ; i
++)
389 offset
= GET_BE_WORD(script
->LangSysRecord
[i
].LangSys
);
390 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
392 if ( strncmp(script
->LangSysRecord
[i
].LangSysTag
,tag
,4)==0)
395 offset
= GET_BE_WORD(script
->DefaultLangSys
);
398 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
404 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
407 const GSUB_FeatureList
*feature
;
408 feature
= (const GSUB_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
410 TRACE("%i features\n",GET_BE_WORD(lang
->FeatureCount
));
411 for (i
= 0; i
< GET_BE_WORD(lang
->FeatureCount
); i
++)
413 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
414 if (strncmp(feature
->FeatureRecord
[index
].FeatureTag
,tag
,4)==0)
416 const GSUB_Feature
*feat
;
417 feat
= (const GSUB_Feature
*)((const BYTE
*)feature
+ GET_BE_WORD(feature
->FeatureRecord
[index
].Feature
));
424 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
427 TRACE("Single Substitution Subtable\n");
429 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
432 const GSUB_SingleSubstFormat1
*ssf1
;
433 offset
= GET_BE_WORD(look
->SubTable
[j
]);
434 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
435 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
437 int offset
= GET_BE_WORD(ssf1
->Coverage
);
438 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
439 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
441 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
442 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
443 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
444 return glyph_index
+ 1;
449 const GSUB_SingleSubstFormat2
*ssf2
;
453 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
454 offset
= GET_BE_WORD(ssf1
->Coverage
);
455 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
456 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
457 TRACE(" Coverage index %i\n",index
);
460 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
461 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
462 TRACE("0x%x\n",glyphs
[glyph_index
]);
463 return glyph_index
+ 1;
467 return GSUB_E_NOGLYPH
;
470 static INT
GSUB_apply_AlternateSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
473 TRACE("Alternate Substitution Subtable\n");
475 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
478 const GSUB_AlternateSubstFormat1
*asf1
;
481 offset
= GET_BE_WORD(look
->SubTable
[j
]);
482 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
483 offset
= GET_BE_WORD(asf1
->Coverage
);
485 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
488 const GSUB_AlternateSet
*as
;
489 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
490 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
491 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
493 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
494 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
495 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
496 return glyph_index
+ 1;
499 return GSUB_E_NOGLYPH
;
502 static INT
GSUB_apply_LigatureSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
506 TRACE("Ligature Substitution Subtable\n");
507 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
509 const GSUB_LigatureSubstFormat1
*lsf1
;
512 offset
= GET_BE_WORD(look
->SubTable
[j
]);
513 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
514 offset
= GET_BE_WORD(lsf1
->Coverage
);
515 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
516 TRACE(" Coverage index %i\n",index
);
519 const GSUB_LigatureSet
*ls
;
522 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
523 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
524 count
= GET_BE_WORD(ls
->LigatureCount
);
525 TRACE(" LigatureSet has %i members\n",count
);
526 for (k
= 0; k
< count
; k
++)
528 const GSUB_Ligature
*lig
;
529 int CompCount
,l
,CompIndex
;
531 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
532 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
533 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
534 CompIndex
= glyph_index
+write_dir
;
535 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
538 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
539 if (CompGlyph
!= glyphs
[CompIndex
])
541 CompIndex
+= write_dir
;
545 int replaceIdx
= glyph_index
;
547 replaceIdx
= glyph_index
- CompCount
;
549 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
550 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
551 TRACE("0x%x\n",glyphs
[replaceIdx
]);
555 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
556 glyphs
[j
] =glyphs
[j
+CompCount
];
557 *glyph_count
= *glyph_count
- CompCount
;
559 return replaceIdx
+ 1;
564 return GSUB_E_NOGLYPH
;
567 static INT
GSUB_apply_ChainContextSubst(const GSUB_LookupList
* lookup
, const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
572 TRACE("Chaining Contextual Substitution Subtable\n");
573 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
575 const GSUB_ChainContextSubstFormat1
*ccsf1
;
577 int dirLookahead
= write_dir
;
578 int dirBacktrack
= -1 * write_dir
;
580 offset
= GET_BE_WORD(look
->SubTable
[j
]);
581 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
582 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
584 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
587 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
589 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
592 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
596 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
597 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
598 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
599 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
600 int newIndex
= glyph_index
;
602 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
604 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
606 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
608 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
609 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
612 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
614 TRACE("Matched Backtrack\n");
616 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
618 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
619 for (k
= 0; k
< indexGlyphs
; k
++)
621 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
622 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
625 if (k
!= indexGlyphs
)
627 TRACE("Matched IndexGlyphs\n");
629 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
631 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
633 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
634 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
+1))]) == -1)
637 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
639 TRACE("Matched LookAhead\n");
641 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
643 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
645 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
646 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
648 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
649 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
652 ERR("Chain failed to generate a glyph\n");
662 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
665 const GSUB_LookupTable
*look
;
667 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
668 look
= (const GSUB_LookupTable
*)((const BYTE
*)lookup
+ offset
);
669 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
670 switch(GET_BE_WORD(look
->LookupType
))
673 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
675 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
677 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
679 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
681 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
683 return GSUB_E_NOGLYPH
;
686 static INT
GSUB_apply_feature(const GSUB_Header
* header
, const GSUB_Feature
* feature
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
689 int out_index
= GSUB_E_NOGLYPH
;
690 const GSUB_LookupList
*lookup
;
692 lookup
= (const GSUB_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
694 TRACE("%i lookups\n", GET_BE_WORD(feature
->LookupCount
));
695 for (i
= 0; i
< GET_BE_WORD(feature
->LookupCount
); i
++)
697 out_index
= GSUB_apply_lookup(lookup
, GET_BE_WORD(feature
->LookupListIndex
[i
]), glyphs
, glyph_index
, write_dir
, glyph_count
);
698 if (out_index
!= GSUB_E_NOGLYPH
)
701 if (out_index
== GSUB_E_NOGLYPH
)
702 TRACE("lookups found no glyphs\n");
706 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
710 if (ShapingData
[psa
->eScript
].otTag
[0] != 0)
711 return ShapingData
[psa
->eScript
].otTag
;
714 * fall back to the font charset
716 charset
= GetTextCharsetInfo(hdc
, NULL
, 0x0);
719 case ANSI_CHARSET
: return "latn";
720 case BALTIC_CHARSET
: return "latn"; /* ?? */
721 case CHINESEBIG5_CHARSET
: return "hani";
722 case EASTEUROPE_CHARSET
: return "latn"; /* ?? */
723 case GB2312_CHARSET
: return "hani";
724 case GREEK_CHARSET
: return "grek";
725 case HANGUL_CHARSET
: return "hang";
726 case RUSSIAN_CHARSET
: return "cyrl";
727 case SHIFTJIS_CHARSET
: return "kana";
728 case TURKISH_CHARSET
: return "latn"; /* ?? */
729 case VIETNAMESE_CHARSET
: return "latn";
730 case JOHAB_CHARSET
: return "latn"; /* ?? */
731 case ARABIC_CHARSET
: return "arab";
732 case HEBREW_CHARSET
: return "hebr";
733 case THAI_CHARSET
: return "thai";
734 default: return "latn";
738 static LPCVOID
load_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, const char* feat
)
740 const GSUB_Feature
*feature
;
743 for (i
= 0; i
< psc
->feature_count
; i
++)
744 if (strncmp(psc
->features
[i
].tag
,feat
,4)==0)
745 return psc
->features
[i
].feature
;
751 const GSUB_Script
*script
;
752 const GSUB_LangSys
*language
;
754 script
= GSUB_get_script_table(psc
->GSUB_Table
, get_opentype_script(hdc
,psa
));
757 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
759 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
762 /* try in the default (latin) table */
765 script
= GSUB_get_script_table(psc
->GSUB_Table
, "latn");
768 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
770 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
775 TRACE("Feature %s located at %p\n",debugstr_an(feat
,4),feature
);
777 psc
->feature_count
++;
780 psc
->features
= HeapReAlloc(GetProcessHeap(), 0, psc
->features
, psc
->feature_count
* sizeof(LoadedFeature
));
782 psc
->features
= HeapAlloc(GetProcessHeap(), 0, psc
->feature_count
* sizeof(LoadedFeature
));
784 lstrcpynA(psc
->features
[psc
->feature_count
- 1].tag
, feat
, 5);
785 psc
->features
[psc
->feature_count
- 1].feature
= feature
;
789 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
)
791 const GSUB_Feature
*feature
;
793 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
795 return GSUB_E_NOFEATURE
;
797 TRACE("applying feature %s\n",feat
);
798 return GSUB_apply_feature(psc
->GSUB_Table
, feature
, glyphs
, index
, write_dir
, glyph_count
);
801 static VOID
*load_gsub_table(HDC hdc
)
803 VOID
* GSUB_Table
= NULL
;
804 int length
= GetFontData(hdc
, GSUB_TAG
, 0, NULL
, 0);
805 if (length
!= GDI_ERROR
)
807 GSUB_Table
= HeapAlloc(GetProcessHeap(),0,length
);
808 GetFontData(hdc
, GSUB_TAG
, 0, GSUB_Table
, length
);
809 TRACE("Loaded GSUB table of %i bytes\n",length
);
814 static void UpdateClusters(int nextIndex
, int changeCount
, int write_dir
, int chars
, WORD
* pwLogClust
)
816 if (changeCount
== 0)
821 int target_glyph
= nextIndex
- 1;
822 int target_index
= -1;
823 int replacing_glyph
= -1;
827 for (i
= 0; i
< chars
; i
++)
829 if (pwLogClust
[i
] == target_glyph
)
836 for (i
= chars
- 1; i
>= 0; i
--)
838 if (pwLogClust
[i
] == target_glyph
)
844 if (target_index
== -1)
846 ERR("Unable to find target glyph\n");
853 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
855 if (pwLogClust
[i
] == target_glyph
)
857 if(pwLogClust
[i
] == replacing_glyph
)
858 pwLogClust
[i
] = target_glyph
;
862 if (changed
>= changeCount
)
864 replacing_glyph
= pwLogClust
[i
];
865 pwLogClust
[i
] = target_glyph
;
873 /* renumber trailing indexes*/
874 for(i
= target_index
; i
< chars
&& i
>= 0; i
+=write_dir
)
876 if (pwLogClust
[i
] != target_glyph
)
877 pwLogClust
[i
] += changeCount
;
882 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
)
888 const GSUB_Feature
*feature
;
890 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
892 return GSUB_E_NOFEATURE
;
895 TRACE("applying feature %s\n",debugstr_an(feat
,4));
899 INT prevCount
= *pcGlyphs
;
900 nextIndex
= GSUB_apply_feature(psc
->GSUB_Table
, feature
, pwOutGlyphs
, i
, write_dir
, pcGlyphs
);
901 if (nextIndex
> GSUB_E_NOGLYPH
)
903 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, write_dir
, cChars
, pwLogClust
);
911 return GSUB_E_NOFEATURE
;
914 static WCHAR
neighbour_char(int i
, int delta
, const WCHAR
* chars
, INT cchLen
)
918 if ( i
+ delta
>= cchLen
)
926 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
930 if (psa
->fLinkBefore
)
935 if ( i
+ delta
>= cchLen
)
945 if (context_type
[i
] == jtT
)
946 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
948 return context_type
[i
];
951 static inline BOOL
right_join_causing(CHAR joining_type
)
953 return (joining_type
== jtL
|| joining_type
== jtD
|| joining_type
== jtC
);
956 static inline BOOL
left_join_causing(CHAR joining_type
)
958 return (joining_type
== jtR
|| joining_type
== jtD
|| joining_type
== jtC
);
961 static inline BOOL
word_break_causing(WCHAR chr
)
963 /* we are working within a string of characters already guareented to
964 be within one script, Syriac, so we do not worry about any characers
965 other than the space character outside of that range */
966 return (chr
== 0 || chr
== 0x20 );
970 * ContextualShape_Arabic
972 static void ContextualShape_Arabic(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
979 if (*pcGlyphs
!= cChars
)
981 ERR("Number of Glyphs and Chars need to match at the beginning\n");
985 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
996 if (!psc
->GSUB_Table
)
997 psc
->GSUB_Table
= load_gsub_table(hdc
);
999 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1000 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1002 for (i
= 0; i
< cChars
; i
++)
1003 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1005 for (i
= 0; i
< cChars
; i
++)
1007 if (context_type
[i
] == jtR
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1008 context_shape
[i
] = Xr
;
1009 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1010 context_shape
[i
] = Xl
;
1011 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
)))
1012 context_shape
[i
] = Xm
;
1013 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1014 context_shape
[i
] = Xr
;
1015 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1016 context_shape
[i
] = Xl
;
1018 context_shape
[i
] = Xn
;
1021 /* Contextual Shaping */
1023 while(i
< *pcGlyphs
)
1025 BOOL shaped
= FALSE
;
1027 if (psc
->GSUB_Table
)
1030 INT prevCount
= *pcGlyphs
;
1031 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1032 if (nextIndex
> GSUB_E_NOGLYPH
)
1035 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1037 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
1042 WORD newGlyph
= pwOutGlyphs
[i
];
1043 if (pwcChars
[i
] >= FIRST_ARABIC_CHAR
&& pwcChars
[i
] <= LAST_ARABIC_CHAR
)
1045 /* fall back to presentation form B */
1046 WCHAR context_char
= wine_shaping_forms
[pwcChars
[i
] - FIRST_ARABIC_CHAR
][context_shape
[i
]];
1047 if (context_char
!= pwcChars
[i
] && GetGlyphIndicesW(hdc
, &context_char
, 1, &newGlyph
, 0) != GDI_ERROR
&& newGlyph
!= 0x0000)
1048 pwOutGlyphs
[i
] = newGlyph
;
1054 HeapFree(GetProcessHeap(),0,context_shape
);
1055 HeapFree(GetProcessHeap(),0,context_type
);
1059 * ContextualShape_Syriac
1063 #define DALATH 0x715
1066 static void ContextualShape_Syriac(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1073 if (*pcGlyphs
!= cChars
)
1075 ERR("Number of Glyphs and Chars need to match at the beginning\n");
1079 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1090 if (!psc
->GSUB_Table
)
1091 psc
->GSUB_Table
= load_gsub_table(hdc
);
1093 if (!psc
->GSUB_Table
)
1096 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
1097 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
1099 for (i
= 0; i
< cChars
; i
++)
1100 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
1102 for (i
= 0; i
< cChars
; i
++)
1104 if (pwcChars
[i
] == ALAPH
)
1106 WCHAR rchar
= neighbour_char(i
,dirR
,pwcChars
,cChars
);
1108 if (left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1109 context_shape
[i
] = Afj
;
1110 else if ( rchar
!= DALATH
&& rchar
!= RISH
&&
1111 !left_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)) &&
1112 word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1113 context_shape
[i
] = Afn
;
1114 else if ( (rchar
== DALATH
|| rchar
== RISH
) && word_break_causing(neighbour_char(i
,dirL
,pwcChars
,cChars
)))
1115 context_shape
[i
] = Afx
;
1117 else if (context_type
[i
] == jtR
&&
1118 right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1119 context_shape
[i
] = Xr
;
1120 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1121 context_shape
[i
] = Xl
;
1122 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
)))
1123 context_shape
[i
] = Xm
;
1124 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
1125 context_shape
[i
] = Xr
;
1126 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
1127 context_shape
[i
] = Xl
;
1129 context_shape
[i
] = Xn
;
1132 /* Contextual Shaping */
1134 while(i
< *pcGlyphs
)
1137 INT prevCount
= *pcGlyphs
;
1138 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
1139 if (nextIndex
> GSUB_E_NOGLYPH
)
1141 UpdateClusters(nextIndex
, *pcGlyphs
- prevCount
, dirL
, cChars
, pwLogClust
);
1146 HeapFree(GetProcessHeap(),0,context_shape
);
1147 HeapFree(GetProcessHeap(),0,context_type
);
1150 void SHAPE_ContextualShaping(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, WORD
*pwLogClust
)
1152 if (ShapingData
[psa
->eScript
].contextProc
)
1153 ShapingData
[psa
->eScript
].contextProc(hdc
, psc
, psa
, pwcChars
, cChars
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, pwLogClust
);
1156 void SHAPE_ApplyOpenTypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, INT cChars
, const TEXTRANGE_PROPERTIES
*rpRangeProperties
, WORD
*pwLogClust
)
1161 if (!rpRangeProperties
)
1164 if (!psc
->GSUB_Table
)
1165 psc
->GSUB_Table
= load_gsub_table(hdc
);
1167 if (!psc
->GSUB_Table
)
1170 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
1175 for (i
= 0; i
< rpRangeProperties
->cotfRecords
; i
++)
1177 if (rpRangeProperties
->potfRecords
[i
].lParameter
> 0)
1178 apply_GSUB_feature(hdc
, psa
, psc
, pwOutGlyphs
, dirL
, pcGlyphs
, cChars
, (const char*)&rpRangeProperties
->potfRecords
[i
].tagFeature
, pwLogClust
);
1182 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, INT cChars
, WORD
*pwLogClust
)
1184 const TEXTRANGE_PROPERTIES
*rpRangeProperties
;
1185 rpRangeProperties
= &ShapingData
[psa
->eScript
].defaultTextRange
;
1187 SHAPE_ApplyOpenTypeFeatures(hdc
, psc
, psa
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, cChars
, rpRangeProperties
, pwLogClust
);