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
);
43 static void ContextualShape_Arabic(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
);
45 extern const unsigned short wine_shaping_table
[];
46 extern const unsigned short wine_shaping_forms
[LAST_ARABIC_CHAR
- FIRST_ARABIC_CHAR
+ 1][4];
64 #ifdef WORDS_BIGENDIAN
65 #define GET_BE_WORD(x) (x)
67 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
70 /* These are all structures needed for the GSUB table */
71 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
72 ( ( (ULONG)_x4 << 24 ) | \
73 ( (ULONG)_x3 << 16 ) | \
74 ( (ULONG)_x2 << 8 ) | \
77 #define GSUB_TAG MS_MAKE_TAG('G', 'S', 'U', 'B')
78 #define GSUB_E_NOFEATURE -2
79 #define GSUB_E_NOGLYPH -1
95 GSUB_ScriptRecord ScriptRecord
[1];
101 } GSUB_LangSysRecord
;
106 GSUB_LangSysRecord LangSysRecord
[1];
110 WORD LookupOrder
; /* Reserved */
111 WORD ReqFeatureIndex
;
113 WORD FeatureIndex
[1];
119 } GSUB_FeatureRecord
;
123 GSUB_FeatureRecord FeatureRecord
[1];
127 WORD FeatureParams
; /* Reserved */
129 WORD LookupListIndex
[1];
148 } GSUB_CoverageFormat1
;
153 WORD StartCoverageIndex
;
159 GSUB_RangeRecord RangeRecord
[1];
160 } GSUB_CoverageFormat2
;
163 WORD SubstFormat
; /* = 1 */
166 } GSUB_SingleSubstFormat1
;
169 WORD SubstFormat
; /* = 2 */
173 }GSUB_SingleSubstFormat2
;
176 WORD SubstFormat
; /* = 1 */
180 }GSUB_LigatureSubstFormat1
;
195 WORD LookupListIndex
;
197 }GSUB_SubstLookupRecord
;
200 WORD SubstFormat
; /* = 1 */
202 WORD ChainSubRuleSetCount
;
203 WORD ChainSubRuleSet
[1];
204 }GSUB_ChainContextSubstFormat1
;
207 WORD SubstFormat
; /* = 3 */
208 WORD BacktrackGlyphCount
;
210 }GSUB_ChainContextSubstFormat3_1
;
213 WORD InputGlyphCount
;
215 }GSUB_ChainContextSubstFormat3_2
;
218 WORD LookaheadGlyphCount
;
220 }GSUB_ChainContextSubstFormat3_3
;
224 GSUB_SubstLookupRecord SubstLookupRecord
[1];
225 }GSUB_ChainContextSubstFormat3_4
;
228 WORD SubstFormat
; /* = 1 */
230 WORD AlternateSetCount
;
231 WORD AlternateSet
[1];
232 } GSUB_AlternateSubstFormat1
;
239 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
);
241 /* the orders of joined_forms and contextual_features need to line up */
242 static const char* contextual_features
[] =
250 static OPENTYPE_FEATURE_RECORD standard_features
[] =
252 { 0x6167696c /*liga*/, 1},
253 { 0x67696c63 /*clig*/, 1},
256 static OPENTYPE_FEATURE_RECORD arabic_features
[] =
258 { 0x67696c72 /*rlig*/, 1},
259 { 0x746c6163 /*calt*/, 1},
260 { 0x6167696c /*liga*/, 1},
261 { 0x67696c64 /*dlig*/, 1},
262 { 0x68777363 /*cswh*/, 1},
263 { 0x7465736d /*mset*/, 1},
266 static OPENTYPE_FEATURE_RECORD hebrew_features
[] =
268 { 0x67696c64 /*dlig*/, 1},
271 static OPENTYPE_FEATURE_RECORD syriac_features
[] =
273 { 0x67696c72 /*rlig*/, 1},
274 { 0x746c6163 /*calt*/, 1},
275 { 0x6167696c /*liga*/, 1},
276 { 0x67696c64 /*dlig*/, 1},
279 typedef struct ScriptShapeDataTag
{
280 TEXTRANGE_PROPERTIES defaultTextRange
;
282 ContextualShapingProc contextProc
;
285 /* in order of scripts */
286 static const ScriptShapeData ShapingData
[] =
288 {{ standard_features
, 2}, "", NULL
},
289 {{ standard_features
, 2}, "latn", NULL
},
290 {{ standard_features
, 2}, "latn", NULL
},
291 {{ standard_features
, 2}, "latn", NULL
},
292 {{ standard_features
, 2}, "" , NULL
},
293 {{ standard_features
, 2}, "latn", NULL
},
294 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
295 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
296 {{ hebrew_features
, 1}, "hebr", NULL
},
297 {{ syriac_features
, 4}, "syrc", NULL
},
298 {{ arabic_features
, 6}, "arab", ContextualShape_Arabic
},
301 static INT
GSUB_is_glyph_covered(LPCVOID table
, UINT glyph
)
303 const GSUB_CoverageFormat1
* cf1
;
307 if (GET_BE_WORD(cf1
->CoverageFormat
) == 1)
309 int count
= GET_BE_WORD(cf1
->GlyphCount
);
311 TRACE("Coverage Format 1, %i glyphs\n",count
);
312 for (i
= 0; i
< count
; i
++)
313 if (glyph
== GET_BE_WORD(cf1
->GlyphArray
[i
]))
317 else if (GET_BE_WORD(cf1
->CoverageFormat
) == 2)
319 const GSUB_CoverageFormat2
* cf2
;
322 cf2
= (const GSUB_CoverageFormat2
*)cf1
;
324 count
= GET_BE_WORD(cf2
->RangeCount
);
325 TRACE("Coverage Format 2, %i ranges\n",count
);
326 for (i
= 0; i
< count
; i
++)
328 if (glyph
< GET_BE_WORD(cf2
->RangeRecord
[i
].Start
))
330 if ((glyph
>= GET_BE_WORD(cf2
->RangeRecord
[i
].Start
)) &&
331 (glyph
<= GET_BE_WORD(cf2
->RangeRecord
[i
].End
)))
333 return (GET_BE_WORD(cf2
->RangeRecord
[i
].StartCoverageIndex
) +
334 glyph
- GET_BE_WORD(cf2
->RangeRecord
[i
].Start
));
340 ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1
->CoverageFormat
));
345 static const GSUB_Script
* GSUB_get_script_table( const GSUB_Header
* header
, const char* tag
)
347 const GSUB_ScriptList
*script
;
348 const GSUB_Script
*deflt
= NULL
;
350 script
= (const GSUB_ScriptList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->ScriptList
));
352 TRACE("%i scripts in this font\n",GET_BE_WORD(script
->ScriptCount
));
353 for (i
= 0; i
< GET_BE_WORD(script
->ScriptCount
); i
++)
355 const GSUB_Script
*scr
;
358 offset
= GET_BE_WORD(script
->ScriptRecord
[i
].Script
);
359 scr
= (const GSUB_Script
*)((const BYTE
*)script
+ offset
);
361 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, tag
,4)==0)
363 if (strncmp(script
->ScriptRecord
[i
].ScriptTag
, "dflt",4)==0)
369 static const GSUB_LangSys
* GSUB_get_lang_table( const GSUB_Script
* script
, const char* tag
)
373 const GSUB_LangSys
*Lang
;
375 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script
->DefaultLangSys
), GET_BE_WORD(script
->LangSysCount
));
377 for (i
= 0; i
< GET_BE_WORD(script
->LangSysCount
) ; i
++)
379 offset
= GET_BE_WORD(script
->LangSysRecord
[i
].LangSys
);
380 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
382 if ( strncmp(script
->LangSysRecord
[i
].LangSysTag
,tag
,4)==0)
385 offset
= GET_BE_WORD(script
->DefaultLangSys
);
388 Lang
= (const GSUB_LangSys
*)((const BYTE
*)script
+ offset
);
394 static const GSUB_Feature
* GSUB_get_feature(const GSUB_Header
*header
, const GSUB_LangSys
*lang
, const char* tag
)
397 const GSUB_FeatureList
*feature
;
398 feature
= (const GSUB_FeatureList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->FeatureList
));
400 TRACE("%i features\n",GET_BE_WORD(lang
->FeatureCount
));
401 for (i
= 0; i
< GET_BE_WORD(lang
->FeatureCount
); i
++)
403 int index
= GET_BE_WORD(lang
->FeatureIndex
[i
]);
404 if (strncmp(feature
->FeatureRecord
[index
].FeatureTag
,tag
,4)==0)
406 const GSUB_Feature
*feat
;
407 feat
= (const GSUB_Feature
*)((const BYTE
*)feature
+ GET_BE_WORD(feature
->FeatureRecord
[index
].Feature
));
414 static INT
GSUB_apply_SingleSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
417 TRACE("Single Substitution Subtable\n");
419 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
422 const GSUB_SingleSubstFormat1
*ssf1
;
423 offset
= GET_BE_WORD(look
->SubTable
[j
]);
424 ssf1
= (const GSUB_SingleSubstFormat1
*)((const BYTE
*)look
+offset
);
425 if (GET_BE_WORD(ssf1
->SubstFormat
) == 1)
427 int offset
= GET_BE_WORD(ssf1
->Coverage
);
428 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1
->DeltaGlyphID
));
429 if (GSUB_is_glyph_covered((const BYTE
*)ssf1
+offset
, glyphs
[glyph_index
]) != -1)
431 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
432 glyphs
[glyph_index
] = glyphs
[glyph_index
] + GET_BE_WORD(ssf1
->DeltaGlyphID
);
433 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
434 return glyph_index
+ 1;
439 const GSUB_SingleSubstFormat2
*ssf2
;
443 ssf2
= (const GSUB_SingleSubstFormat2
*)ssf1
;
444 offset
= GET_BE_WORD(ssf1
->Coverage
);
445 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2
->GlyphCount
));
446 index
= GSUB_is_glyph_covered((const BYTE
*)ssf2
+offset
, glyphs
[glyph_index
]);
447 TRACE(" Coverage index %i\n",index
);
450 TRACE(" Glyph is 0x%x ->",glyphs
[glyph_index
]);
451 glyphs
[glyph_index
] = GET_BE_WORD(ssf2
->Substitute
[index
]);
452 TRACE("0x%x\n",glyphs
[glyph_index
]);
453 return glyph_index
+ 1;
457 return GSUB_E_NOGLYPH
;
460 static INT
GSUB_apply_AlternateSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
463 TRACE("Alternate Substitution Subtable\n");
465 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
468 const GSUB_AlternateSubstFormat1
*asf1
;
471 offset
= GET_BE_WORD(look
->SubTable
[j
]);
472 asf1
= (const GSUB_AlternateSubstFormat1
*)((const BYTE
*)look
+offset
);
473 offset
= GET_BE_WORD(asf1
->Coverage
);
475 index
= GSUB_is_glyph_covered((const BYTE
*)asf1
+offset
, glyphs
[glyph_index
]);
478 const GSUB_AlternateSet
*as
;
479 offset
= GET_BE_WORD(asf1
->AlternateSet
[index
]);
480 as
= (const GSUB_AlternateSet
*)((const BYTE
*)asf1
+offset
);
481 FIXME("%i alternates, picking index 0\n",GET_BE_WORD(as
->GlyphCount
));
483 TRACE(" Glyph 0x%x ->",glyphs
[glyph_index
]);
484 glyphs
[glyph_index
] = GET_BE_WORD(as
->Alternate
[0]);
485 TRACE(" 0x%x\n",glyphs
[glyph_index
]);
486 return glyph_index
+ 1;
489 return GSUB_E_NOGLYPH
;
492 static INT
GSUB_apply_LigatureSubst(const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
496 TRACE("Ligature Substitution Subtable\n");
497 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
); j
++)
499 const GSUB_LigatureSubstFormat1
*lsf1
;
502 offset
= GET_BE_WORD(look
->SubTable
[j
]);
503 lsf1
= (const GSUB_LigatureSubstFormat1
*)((const BYTE
*)look
+offset
);
504 offset
= GET_BE_WORD(lsf1
->Coverage
);
505 index
= GSUB_is_glyph_covered((const BYTE
*)lsf1
+offset
, glyphs
[glyph_index
]);
506 TRACE(" Coverage index %i\n",index
);
509 const GSUB_LigatureSet
*ls
;
512 offset
= GET_BE_WORD(lsf1
->LigatureSet
[index
]);
513 ls
= (const GSUB_LigatureSet
*)((const BYTE
*)lsf1
+offset
);
514 count
= GET_BE_WORD(ls
->LigatureCount
);
515 TRACE(" LigatureSet has %i members\n",count
);
516 for (k
= 0; k
< count
; k
++)
518 const GSUB_Ligature
*lig
;
519 int CompCount
,l
,CompIndex
;
521 offset
= GET_BE_WORD(ls
->Ligature
[k
]);
522 lig
= (const GSUB_Ligature
*)((const BYTE
*)ls
+offset
);
523 CompCount
= GET_BE_WORD(lig
->CompCount
) - 1;
524 CompIndex
= glyph_index
+write_dir
;
525 for (l
= 0; l
< CompCount
&& CompIndex
>= 0 && CompIndex
< *glyph_count
; l
++)
528 CompGlyph
= GET_BE_WORD(lig
->Component
[l
]);
529 if (CompGlyph
!= glyphs
[CompIndex
])
531 CompIndex
+= write_dir
;
535 int replaceIdx
= glyph_index
;
537 replaceIdx
= glyph_index
- CompCount
;
539 TRACE(" Glyph is 0x%x (+%i) ->",glyphs
[glyph_index
],CompCount
);
540 glyphs
[replaceIdx
] = GET_BE_WORD(lig
->LigGlyph
);
541 TRACE("0x%x\n",glyphs
[replaceIdx
]);
545 for (j
= replaceIdx
+ 1; j
< *glyph_count
; j
++)
546 glyphs
[j
] =glyphs
[j
+CompCount
];
547 *glyph_count
= *glyph_count
- CompCount
;
549 return replaceIdx
+ 1;
554 return GSUB_E_NOGLYPH
;
557 static INT
GSUB_apply_ChainContextSubst(const GSUB_LookupList
* lookup
, const GSUB_LookupTable
*look
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
562 TRACE("Chaining Contextual Substitution Subtable\n");
563 for (j
= 0; j
< GET_BE_WORD(look
->SubTableCount
) && !done
; j
++)
565 const GSUB_ChainContextSubstFormat1
*ccsf1
;
567 int dirLookahead
= write_dir
;
568 int dirBacktrack
= -1 * write_dir
;
570 offset
= GET_BE_WORD(look
->SubTable
[j
]);
571 ccsf1
= (const GSUB_ChainContextSubstFormat1
*)((const BYTE
*)look
+offset
);
572 if (GET_BE_WORD(ccsf1
->SubstFormat
) == 1)
574 FIXME(" TODO: subtype 1 (Simple context glyph substitution)\n");
577 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 2)
579 FIXME(" TODO: subtype 2 (Class-based Chaining Context Glyph Substitution)\n");
582 else if (GET_BE_WORD(ccsf1
->SubstFormat
) == 3)
586 const GSUB_ChainContextSubstFormat3_1
*ccsf3_1
;
587 const GSUB_ChainContextSubstFormat3_2
*ccsf3_2
;
588 const GSUB_ChainContextSubstFormat3_3
*ccsf3_3
;
589 const GSUB_ChainContextSubstFormat3_4
*ccsf3_4
;
590 int newIndex
= glyph_index
;
592 ccsf3_1
= (const GSUB_ChainContextSubstFormat3_1
*)ccsf1
;
594 TRACE(" subtype 3 (Coverage-based Chaining Context Glyph Substitution)\n");
596 for (k
= 0; k
< GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
); k
++)
598 offset
= GET_BE_WORD(ccsf3_1
->Coverage
[k
]);
599 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirBacktrack
* (k
+1))]) == -1)
602 if (k
!= GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
))
604 TRACE("Matched Backtrack\n");
606 ccsf3_2
= (const GSUB_ChainContextSubstFormat3_2
*)(((LPBYTE
)ccsf1
)+sizeof(GSUB_ChainContextSubstFormat3_1
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_1
->BacktrackGlyphCount
)-1)));
608 indexGlyphs
= GET_BE_WORD(ccsf3_2
->InputGlyphCount
);
609 for (k
= 0; k
< indexGlyphs
; k
++)
611 offset
= GET_BE_WORD(ccsf3_2
->Coverage
[k
]);
612 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (write_dir
* k
)]) == -1)
615 if (k
!= indexGlyphs
)
617 TRACE("Matched IndexGlyphs\n");
619 ccsf3_3
= (const GSUB_ChainContextSubstFormat3_3
*)(((LPBYTE
)ccsf3_2
)+sizeof(GSUB_ChainContextSubstFormat3_2
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_2
->InputGlyphCount
)-1)));
621 for (k
= 0; k
< GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
); k
++)
623 offset
= GET_BE_WORD(ccsf3_3
->Coverage
[k
]);
624 if (GSUB_is_glyph_covered((const BYTE
*)ccsf3_1
+offset
, glyphs
[glyph_index
+ (dirLookahead
* (indexGlyphs
+ k
+1))]) == -1)
627 if (k
!= GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
))
629 TRACE("Matched LookAhead\n");
631 ccsf3_4
= (const GSUB_ChainContextSubstFormat3_4
*)(((LPBYTE
)ccsf3_3
)+sizeof(GSUB_ChainContextSubstFormat3_3
) + (sizeof(WORD
) * (GET_BE_WORD(ccsf3_3
->LookaheadGlyphCount
)-1)));
633 for (k
= 0; k
< GET_BE_WORD(ccsf3_4
->SubstCount
); k
++)
635 int lookupIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].LookupListIndex
);
636 int SequenceIndex
= GET_BE_WORD(ccsf3_4
->SubstLookupRecord
[k
].SequenceIndex
) * write_dir
;
638 TRACE("SUBST: %i -> %i %i\n",k
, SequenceIndex
, lookupIndex
);
639 newIndex
= GSUB_apply_lookup(lookup
, lookupIndex
, glyphs
, glyph_index
+ SequenceIndex
, write_dir
, glyph_count
);
642 ERR("Chain failed to generate a glyph\n");
652 static INT
GSUB_apply_lookup(const GSUB_LookupList
* lookup
, INT lookup_index
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
655 const GSUB_LookupTable
*look
;
657 offset
= GET_BE_WORD(lookup
->Lookup
[lookup_index
]);
658 look
= (const GSUB_LookupTable
*)((const BYTE
*)lookup
+ offset
);
659 TRACE("type %i, flag %x, subtables %i\n",GET_BE_WORD(look
->LookupType
),GET_BE_WORD(look
->LookupFlag
),GET_BE_WORD(look
->SubTableCount
));
660 switch(GET_BE_WORD(look
->LookupType
))
663 return GSUB_apply_SingleSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
665 return GSUB_apply_AlternateSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
667 return GSUB_apply_LigatureSubst(look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
669 return GSUB_apply_ChainContextSubst(lookup
, look
, glyphs
, glyph_index
, write_dir
, glyph_count
);
671 FIXME("We do not handle SubType %i\n",GET_BE_WORD(look
->LookupType
));
673 return GSUB_E_NOGLYPH
;
676 static INT
GSUB_apply_feature(const GSUB_Header
* header
, const GSUB_Feature
* feature
, WORD
*glyphs
, INT glyph_index
, INT write_dir
, INT
*glyph_count
)
679 int out_index
= GSUB_E_NOGLYPH
;
680 const GSUB_LookupList
*lookup
;
682 lookup
= (const GSUB_LookupList
*)((const BYTE
*)header
+ GET_BE_WORD(header
->LookupList
));
684 TRACE("%i lookups\n", GET_BE_WORD(feature
->LookupCount
));
685 for (i
= 0; i
< GET_BE_WORD(feature
->LookupCount
); i
++)
687 out_index
= GSUB_apply_lookup(lookup
, GET_BE_WORD(feature
->LookupListIndex
[i
]), glyphs
, glyph_index
, write_dir
, glyph_count
);
688 if (out_index
!= GSUB_E_NOGLYPH
)
691 if (out_index
== GSUB_E_NOGLYPH
)
692 TRACE("lookups found no glyphs\n");
696 static const char* get_opentype_script(HDC hdc
, SCRIPT_ANALYSIS
*psa
)
700 if (ShapingData
[psa
->eScript
].otTag
[0] != 0)
701 return ShapingData
[psa
->eScript
].otTag
;
704 * fall back to the font charset
706 charset
= GetTextCharsetInfo(hdc
, NULL
, 0x0);
709 case ANSI_CHARSET
: return "latn";
710 case BALTIC_CHARSET
: return "latn"; /* ?? */
711 case CHINESEBIG5_CHARSET
: return "hani";
712 case EASTEUROPE_CHARSET
: return "latn"; /* ?? */
713 case GB2312_CHARSET
: return "hani";
714 case GREEK_CHARSET
: return "grek";
715 case HANGUL_CHARSET
: return "hang";
716 case RUSSIAN_CHARSET
: return "cyrl";
717 case SHIFTJIS_CHARSET
: return "kana";
718 case TURKISH_CHARSET
: return "latn"; /* ?? */
719 case VIETNAMESE_CHARSET
: return "latn";
720 case JOHAB_CHARSET
: return "latn"; /* ?? */
721 case ARABIC_CHARSET
: return "arab";
722 case HEBREW_CHARSET
: return "hebr";
723 case THAI_CHARSET
: return "thai";
724 default: return "latn";
728 static LPCVOID
load_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, const char* feat
)
730 const GSUB_Feature
*feature
;
733 for (i
= 0; i
< psc
->feature_count
; i
++)
734 if (strncmp(psc
->features
[i
].tag
,feat
,4)==0)
735 return psc
->features
[i
].feature
;
741 const GSUB_Script
*script
;
742 const GSUB_LangSys
*language
;
744 script
= GSUB_get_script_table(psc
->GSUB_Table
, get_opentype_script(hdc
,psa
));
747 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
749 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
752 /* try in the default (latin) table */
755 script
= GSUB_get_script_table(psc
->GSUB_Table
, "latn");
758 language
= GSUB_get_lang_table(script
, "xxxx"); /* Need to get Lang tag */
760 feature
= GSUB_get_feature(psc
->GSUB_Table
, language
, feat
);
765 TRACE("Feature %s located at %p\n",debugstr_an(feat
,4),feature
);
767 psc
->feature_count
++;
770 psc
->features
= HeapReAlloc(GetProcessHeap(), 0, psc
->features
, psc
->feature_count
* sizeof(LoadedFeature
));
772 psc
->features
= HeapAlloc(GetProcessHeap(), 0, psc
->feature_count
* sizeof(LoadedFeature
));
774 lstrcpynA(psc
->features
[psc
->feature_count
- 1].tag
,feat
,4);
775 psc
->features
[psc
->feature_count
- 1].feature
= feature
;
779 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
)
781 const GSUB_Feature
*feature
;
783 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
785 return GSUB_E_NOFEATURE
;
787 TRACE("applying feature %s\n",feat
);
788 return GSUB_apply_feature(psc
->GSUB_Table
, feature
, glyphs
, index
, write_dir
, glyph_count
);
791 static VOID
*load_gsub_table(HDC hdc
)
793 VOID
* GSUB_Table
= NULL
;
794 int length
= GetFontData(hdc
, GSUB_TAG
, 0, NULL
, 0);
795 if (length
!= GDI_ERROR
)
797 GSUB_Table
= HeapAlloc(GetProcessHeap(),0,length
);
798 GetFontData(hdc
, GSUB_TAG
, 0, GSUB_Table
, length
);
799 TRACE("Loaded GSUB table of %i bytes\n",length
);
804 static int apply_GSUB_feature(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
* psc
, WORD
*pwOutGlyphs
, int write_dir
, INT
* pcGlyphs
, const char* feat
)
810 const GSUB_Feature
*feature
;
812 feature
= load_GSUB_feature(hdc
, psa
, psc
, feat
);
814 return GSUB_E_NOFEATURE
;
817 TRACE("applying feature %s\n",debugstr_an(feat
,4));
821 nextIndex
= GSUB_apply_feature(psc
->GSUB_Table
, feature
, pwOutGlyphs
, i
, write_dir
, pcGlyphs
);
822 if (nextIndex
> GSUB_E_NOGLYPH
)
829 return GSUB_E_NOFEATURE
;
832 static CHAR
neighbour_joining_type(int i
, int delta
, const CHAR
* context_type
, INT cchLen
, SCRIPT_ANALYSIS
*psa
)
836 if (psa
->fLinkBefore
)
841 if ( i
+ delta
>= cchLen
)
851 if (context_type
[i
] == jtT
)
852 return neighbour_joining_type(i
,delta
,context_type
,cchLen
,psa
);
854 return context_type
[i
];
857 static inline BOOL
right_join_causing(CHAR joining_type
)
859 return (joining_type
== jtL
|| joining_type
== jtD
|| joining_type
== jtC
);
862 static inline BOOL
left_join_causing(CHAR joining_type
)
864 return (joining_type
== jtR
|| joining_type
== jtD
|| joining_type
== jtC
);
868 * ContextualShape_Arabic
870 static void ContextualShape_Arabic(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
)
877 if (*pcGlyphs
!= cChars
)
879 ERR("Number of Glyphs and Chars need to match at the beginning\n");
883 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
894 if (!psc
->GSUB_Table
)
895 psc
->GSUB_Table
= load_gsub_table(hdc
);
897 context_type
= HeapAlloc(GetProcessHeap(),0,cChars
);
898 context_shape
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * cChars
);
900 for (i
= 0; i
< cChars
; i
++)
901 context_type
[i
] = wine_shaping_table
[wine_shaping_table
[pwcChars
[i
] >> 8] + (pwcChars
[i
] & 0xff)];
903 for (i
= 0; i
< cChars
; i
++)
905 if (context_type
[i
] == jtR
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
906 context_shape
[i
] = Xr
;
907 else if (context_type
[i
] == jtL
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
908 context_shape
[i
] = Xl
;
909 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
)))
910 context_shape
[i
] = Xm
;
911 else if (context_type
[i
] == jtD
&& right_join_causing(neighbour_joining_type(i
,dirR
,context_type
,cChars
,psa
)))
912 context_shape
[i
] = Xr
;
913 else if (context_type
[i
] == jtD
&& left_join_causing(neighbour_joining_type(i
,dirL
,context_type
,cChars
,psa
)))
914 context_shape
[i
] = Xl
;
916 context_shape
[i
] = Xn
;
919 /* Contextual Shaping */
928 nextIndex
= apply_GSUB_feature_to_glyph(hdc
, psa
, psc
, pwOutGlyphs
, i
, dirL
, pcGlyphs
, contextual_features
[context_shape
[i
]]);
929 if (nextIndex
> GSUB_E_NOGLYPH
)
931 shaped
= (nextIndex
> GSUB_E_NOGLYPH
);
936 WORD newGlyph
= pwOutGlyphs
[i
];
937 if (pwcChars
[i
] >= FIRST_ARABIC_CHAR
&& pwcChars
[i
] <= LAST_ARABIC_CHAR
)
939 /* fall back to presentation form B */
940 WCHAR context_char
= wine_shaping_forms
[pwcChars
[i
] - FIRST_ARABIC_CHAR
][context_shape
[i
]];
941 if (context_char
!= pwcChars
[i
] && GetGlyphIndicesW(hdc
, &context_char
, 1, &newGlyph
, 0) != GDI_ERROR
&& newGlyph
!= 0x0000)
942 pwOutGlyphs
[i
] = newGlyph
;
948 HeapFree(GetProcessHeap(),0,context_shape
);
949 HeapFree(GetProcessHeap(),0,context_type
);
952 void SHAPE_ContextualShaping(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WCHAR
* pwcChars
, INT cChars
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
)
954 if (ShapingData
[psa
->eScript
].contextProc
)
955 ShapingData
[psa
->eScript
].contextProc(hdc
, psc
, psa
, pwcChars
, cChars
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
);
958 void SHAPE_ApplyOpenTypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
, const TEXTRANGE_PROPERTIES
*rpRangeProperties
)
963 if (!rpRangeProperties
)
966 if (!psc
->GSUB_Table
)
967 psc
->GSUB_Table
= load_gsub_table(hdc
);
969 if (!psc
->GSUB_Table
)
972 if (!psa
->fLogicalOrder
&& psa
->fRTL
)
977 for (i
= 0; i
< rpRangeProperties
->cotfRecords
; i
++)
979 if (rpRangeProperties
->potfRecords
[i
].lParameter
> 0)
980 apply_GSUB_feature(hdc
, psa
, psc
, pwOutGlyphs
, dirL
, pcGlyphs
, (const char*)&rpRangeProperties
->potfRecords
[i
].tagFeature
);
984 void SHAPE_ApplyDefaultOpentypeFeatures(HDC hdc
, ScriptCache
*psc
, SCRIPT_ANALYSIS
*psa
, WORD
* pwOutGlyphs
, INT
* pcGlyphs
, INT cMaxGlyphs
)
986 const TEXTRANGE_PROPERTIES
*rpRangeProperties
;
987 rpRangeProperties
= &ShapingData
[psa
->eScript
].defaultTextRange
;
989 SHAPE_ApplyOpenTypeFeatures(hdc
, psc
, psa
, pwOutGlyphs
, pcGlyphs
, cMaxGlyphs
, rpRangeProperties
);