2 * Implementation of Uniscribe Script Processor (usp10.dll)
4 * Copyright 2005 Steven Edwards for CodeWeavers
5 * Copyright 2006 Hans Leidekker
6 * Copyright 2010 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Uniscribe allows for processing of complex scripts such as joining
24 * and filtering characters and bi-directional text with custom line breaks.
36 #include "usp10_internal.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
43 typedef struct _scriptRange
52 static const scriptRange scriptRanges[] = {
53 /* Basic Latin: U+0000–U+007A */
54 /* Latin-1 Supplement: U+0080–U+00FF */
55 /* Latin Extended-A: U+0100–U+017F */
56 /* Latin Extended-B: U+0180–U+024F */
57 /* IPA Extensions: U+0250–U+02AF */
58 { Script_Latin, 0x00, 0x2af , Script_Numeric, Script_Punctuation},
59 /* Greek: U+0370–U+03FF */
60 { Script_Greek, 0x370, 0x3ff, 0, 0},
61 /* Cyrillic: U+0400–U+04FF */
62 /* Cyrillic Supplement: U+0500–U+052F */
63 { Script_Cyrillic, 0x400, 0x52f, 0, 0},
64 /* Armenian: U+0530–U+058F */
65 { Script_Armenian, 0x530, 0x58f, 0, 0},
66 /* Hebrew: U+0590–U+05FF */
67 { Script_Hebrew, 0x590, 0x5ff, 0, 0},
68 /* Arabic: U+0600–U+06FF */
69 { Script_Arabic, 0x600, 0x6ef, Script_Arabic_Numeric, 0},
70 /* Defined by Windows */
71 { Script_Persian, 0x6f0, 0x6f9, 0, 0},
72 /* Continue Arabic: U+0600–U+06FF */
73 { Script_Arabic, 0x6fa, 0x6ff, 0, 0},
74 /* Syriac: U+0700–U+074F*/
75 { Script_Syriac, 0x700, 0x74f, 0, 0},
76 /* Arabic Supplement: U+0750–U+077F */
77 { Script_Arabic, 0x750, 0x77f, 0, 0},
78 /* Thaana: U+0780–U+07BF */
79 { Script_Thaana, 0x780, 0x7bf, 0, 0},
80 /* Sinhala: U+0D80–U+0DFF */
81 { Script_Sinhala, 0xd80, 0xdff, 0, 0},
82 /* Thai: U+0E00–U+0E7F */
83 { Script_Thai, 0xe00, 0xe7f, Script_Thai_Numeric, 0},
84 /* Lao: U+0E80–U+0EFF */
85 { Script_Lao, 0xe80, 0xeff, Script_Lao_Numeric, 0},
86 /* Tibetan: U+0F00–U+0FFF */
87 { Script_Tibetan, 0xf00, 0xfff, Script_Tibetan_Numeric, 0},
88 /* Georgian: U+10A0–U+10FF */
89 { Script_Georgian, 0x10a0, 0x10ff, 0, 0},
90 /* Phonetic Extensions: U+1D00–U+1DBF */
91 { Script_Latin, 0x1d00, 0x1dbf, 0, 0},
92 /* Latin Extended Additional: U+1E00–U+1EFF */
93 { Script_Latin, 0x1e00, 0x1eff, 0, 0},
94 /* Greek Extended: U+1F00–U+1FFF */
95 { Script_Greek, 0x1f00, 0x1fff, 0, 0},
96 /* Latin Extended-C: U+2C60–U+2C7F */
97 { Script_Latin, 0x2c60, 0x2c7f, 0, 0},
98 /* Georgian: U+2D00–U+2D2F */
99 { Script_Georgian, 0x2d00, 0x2d2f, 0, 0},
100 /* Cyrillic Extended-A: U+2DE0–U+2DFF */
101 { Script_Cyrillic, 0x2de0, 0x2dff, 0, 0},
102 /* Cyrillic Extended-B: U+A640–U+A69F */
103 { Script_Cyrillic, 0xa640, 0xa69f, 0, 0},
104 /* Modifier Tone Letters: U+A700–U+A71F */
105 /* Latin Extended-D: U+A720–U+A7FF */
106 { Script_Latin, 0xa700, 0xa7ff, 0, 0},
107 /* Phags-pa: U+A840–U+A87F */
108 { Script_Phags_pa, 0xa840, 0xa87f, 0, 0},
109 /* Latin Ligatures: U+FB00–U+FB06 */
110 { Script_Latin, 0xfb00, 0xfb06, 0, 0},
111 /* Armenian ligatures U+FB13..U+FB17 */
112 { Script_Armenian, 0xfb13, 0xfb17, 0, 0},
113 /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
114 { Script_Hebrew, 0xfb1d, 0xfb4f, 0, 0},
115 /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
116 { Script_Arabic, 0xfb50, 0xfdff, 0, 0},
117 /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
118 { Script_Arabic, 0xfe70, 0xfeff, 0, 0},
120 { SCRIPT_UNDEFINED, 0, 0, 0}
123 typedef struct _scriptData
126 SCRIPT_PROPERTIES props;
129 /* the must be in order so that the index matches the Script value */
130 static const scriptData scriptInformation[] = {
131 {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
132 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
133 {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
134 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
135 {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
136 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
137 {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
138 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
139 {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
140 {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0}},
141 {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
142 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
143 {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
144 {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0}},
145 {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
146 {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
147 {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
148 {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
149 {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
150 {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0}},
151 {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
152 {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
153 {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
154 {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
155 {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
156 {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
157 {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
158 {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
159 {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
160 {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
161 {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
162 {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
163 {{Script_Sinhala, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
164 {LANG_SINHALESE, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
165 {{Script_Tibetan, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
166 {LANG_TIBETAN, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0}},
167 {{Script_Tibetan_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
168 {LANG_TIBETAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
169 {{Script_Phags_pa, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
170 {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
171 {{Script_Thai, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
172 {LANG_THAI, 0, 1, 1, 1, THAI_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 1}},
173 {{Script_Thai_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
174 {LANG_THAI, 1, 1, 0, 0, THAI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
175 {{Script_Lao, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
176 {LANG_LAO, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0}},
177 {{Script_Lao_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
178 {LANG_LAO, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
181 static const SCRIPT_PROPERTIES *script_props[] =
183 &scriptInformation[0].props, &scriptInformation[1].props,
184 &scriptInformation[2].props, &scriptInformation[3].props,
185 &scriptInformation[4].props, &scriptInformation[5].props,
186 &scriptInformation[6].props, &scriptInformation[7].props,
187 &scriptInformation[8].props, &scriptInformation[9].props,
188 &scriptInformation[10].props, &scriptInformation[11].props,
189 &scriptInformation[12].props, &scriptInformation[13].props,
190 &scriptInformation[14].props, &scriptInformation[15].props,
191 &scriptInformation[16].props, &scriptInformation[17].props,
192 &scriptInformation[18].props, &scriptInformation[19].props,
193 &scriptInformation[20].props, &scriptInformation[21].props,
194 &scriptInformation[22].props, &scriptInformation[23].props
202 SCRIPT_VISATTR* psva;
217 StringGlyphs* glyphs;
218 SCRIPT_LOGATTR* logattrs;
222 static inline void *heap_alloc(SIZE_T size)
224 return HeapAlloc(GetProcessHeap(), 0, size);
227 static inline void *heap_alloc_zero(SIZE_T size)
229 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
232 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
234 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
237 static inline BOOL heap_free(LPVOID mem)
239 return HeapFree(GetProcessHeap(), 0, mem);
242 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
244 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
247 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
249 return ((ScriptCache *)*psc)->tm.tmHeight;
252 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
254 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
257 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
259 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
261 if (!block) return 0;
262 return block[c & GLYPH_BLOCK_MASK];
265 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
267 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
269 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
270 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
273 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
275 static const ABC nil;
276 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
278 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
279 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
283 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
285 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
287 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
288 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
292 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
296 if (!psc) return E_INVALIDARG;
297 if (*psc) return S_OK;
298 if (!hdc) return E_PENDING;
300 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
301 if (!GetTextMetricsW(hdc, &sc->tm))
306 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
312 TRACE("<- %p\n", sc);
316 static WCHAR mirror_char( WCHAR ch )
318 extern const WCHAR wine_mirror_map[];
319 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
322 static WORD get_char_script( WCHAR ch)
327 if (ch == 0xc || ch == 0x20 || ch == 0x202f)
330 GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
333 return SCRIPT_UNDEFINED;
336 return Script_Control;
341 if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
344 if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
346 if (scriptRanges[i].numericScript && type & C1_DIGIT)
347 return scriptRanges[i].numericScript;
348 if (scriptRanges[i].punctScript && type & C1_PUNCT)
349 return scriptRanges[i].punctScript;
350 return scriptRanges[i].script;
355 return SCRIPT_UNDEFINED;
358 /***********************************************************************
362 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
366 case DLL_PROCESS_ATTACH:
367 DisableThreadLibraryCalls(hInstDLL);
369 case DLL_PROCESS_DETACH:
375 /***********************************************************************
376 * ScriptFreeCache (USP10.@)
378 * Free a script cache.
381 * psc [I/O] Script cache.
385 * Failure: Non-zero HRESULT value.
387 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
394 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
396 heap_free(((ScriptCache *)*psc)->glyphs[i]);
397 heap_free(((ScriptCache *)*psc)->widths[i]);
399 heap_free(((ScriptCache *)*psc)->GSUB_Table);
400 heap_free(((ScriptCache *)*psc)->features);
407 /***********************************************************************
408 * ScriptGetProperties (USP10.@)
410 * Retrieve a list of script properties.
413 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
414 * num [I] Pointer to the number of scripts.
418 * Failure: Non-zero HRESULT value.
421 * Behaviour matches WinXP.
423 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
425 TRACE("(%p,%p)\n", props, num);
427 if (!props && !num) return E_INVALIDARG;
429 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
430 if (props) *props = script_props;
435 /***********************************************************************
436 * ScriptGetFontProperties (USP10.@)
438 * Get information on special glyphs.
441 * hdc [I] Device context.
442 * psc [I/O] Opaque pointer to a script cache.
443 * sfp [O] Font properties structure.
445 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
449 TRACE("%p,%p,%p\n", hdc, psc, sfp);
451 if (!sfp) return E_INVALIDARG;
452 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
454 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
457 /* return something sensible? */
459 sfp->wgDefault = get_cache_default_char(psc);
461 sfp->wgKashida = 0xffff;
462 sfp->iKashidaWidth = 0;
467 /***********************************************************************
468 * ScriptRecordDigitSubstitution (USP10.@)
470 * Record digit substitution settings for a given locale.
473 * locale [I] Locale identifier.
474 * sds [I] Structure to record substitution settings.
478 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
481 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
483 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
487 TRACE("0x%x, %p\n", locale, sds);
489 /* This implementation appears to be correct for all languages, but it's
490 * not clear if sds->DigitSubstitute is ever set to anything except
491 * CONTEXT or NONE in reality */
493 if (!sds) return E_POINTER;
495 locale = ConvertDefaultLocale(locale);
497 if (!IsValidLocale(locale, LCID_INSTALLED))
500 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
501 sds->TraditionalDigitLanguage = plgid;
503 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
504 sds->NationalDigitLanguage = plgid;
506 sds->NationalDigitLanguage = LANG_ENGLISH;
508 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
509 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
514 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
515 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
517 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
520 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
523 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
526 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
534 /***********************************************************************
535 * ScriptApplyDigitSubstitution (USP10.@)
537 * Apply digit substitution settings.
540 * sds [I] Structure with recorded substitution settings.
541 * sc [I] Script control structure.
542 * ss [I] Script state structure.
546 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
548 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
549 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
551 SCRIPT_DIGITSUBSTITUTE psds;
553 TRACE("%p, %p, %p\n", sds, sc, ss);
555 if (!sc || !ss) return E_POINTER;
559 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
563 sc->uDefaultLanguage = LANG_ENGLISH;
564 sc->fContextDigits = 0;
565 ss->fDigitSubstitute = 0;
567 switch (sds->DigitSubstitute) {
568 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
569 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
570 case SCRIPT_DIGITSUBSTITUTE_NONE:
571 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
578 /***********************************************************************
579 * ScriptItemize (USP10.@)
581 * Split a Unicode string into shapeable parts.
584 * pwcInChars [I] String to split.
585 * cInChars [I] Number of characters in pwcInChars.
586 * cMaxItems [I] Maximum number of items to return.
587 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
588 * psState [I] Pointer to a SCRIPT_STATE structure.
589 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
590 * pcItems [O] Number of script items returned.
594 * Failure: Non-zero HRESULT value.
596 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
597 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
598 SCRIPT_ITEM *pItems, int *pcItems)
601 #define Numeric_space 0x0020
603 int cnt = 0, index = 0, str = 0;
604 int New_Script = SCRIPT_UNDEFINED;
606 WORD *strength = NULL;
609 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
610 psControl, psState, pItems, pcItems);
612 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
615 if (psState && psControl)
618 levels = heap_alloc_zero(cInChars * sizeof(WORD));
620 return E_OUTOFMEMORY;
622 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
623 baselevel = levels[0];
624 for (i = 0; i < cInChars; i++)
625 if (levels[i]!=levels[0])
627 if (i >= cInChars && !odd(baselevel))
634 if (!psControl->fMergeNeutralItems)
636 strength = heap_alloc_zero(cInChars * sizeof(WORD));
637 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
642 while (pwcInChars[cnt] == Numeric_space && cnt < cInChars)
645 if (cnt == cInChars) /* All Spaces */
648 New_Script = get_char_script(pwcInChars[cnt]);
651 pItems[index].iCharPos = 0;
652 pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
660 pItems[index].a.fRTL = odd(levels[cnt]);
661 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
662 pItems[index].a.s.uBidiLevel = levels[cnt];
664 else if (!pItems[index].a.s.uBidiLevel)
666 pItems[index].a.s.uBidiLevel = baselevel;
667 pItems[index].a.fLayoutRTL = odd(baselevel);
668 pItems[index].a.fRTL = odd(baselevel);
671 TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
672 levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
673 pItems[index].iCharPos);
675 for (cnt=1; cnt < cInChars; cnt++)
677 if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel && (!strength || (strength[cnt] == 0 || strength[cnt] == str))))
680 if(pwcInChars[cnt] != Numeric_space)
681 New_Script = get_char_script(pwcInChars[cnt]);
685 while (cnt + j < cInChars - 1 && pwcInChars[cnt+j] == Numeric_space)
687 New_Script = get_char_script(pwcInChars[cnt+j]);
690 if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel || (strength && (strength[cnt] != str)))) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
692 TRACE("New_Level = %i, New_Strength = %i, New_Script=%d, eScript=%d\n", levels?levels[cnt]:-1, strength?strength[cnt]:str, New_Script, pItems[index].a.eScript);
694 if (strength && strength[cnt] != 0)
698 if (index+1 > cMaxItems)
699 return E_OUTOFMEMORY;
701 pItems[index].iCharPos = cnt;
702 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
704 pItems[index].a = scriptInformation[New_Script].a;
707 pItems[index].a.fRTL = odd(levels[cnt]);
708 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
709 pItems[index].a.s.uBidiLevel = levels[cnt];
711 else if (!pItems[index].a.s.uBidiLevel)
713 pItems[index].a.s.uBidiLevel = baselevel;
714 pItems[index].a.fLayoutRTL = odd(baselevel);
715 pItems[index].a.fRTL = odd(baselevel);
718 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
722 /* While not strictly necessary according to the spec, make sure the n+1
723 * item is set up to prevent random behaviour if the caller erroneously
724 * checks the n+1 structure */
726 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
728 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
730 /* Set one SCRIPT_STATE item being returned */
731 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
732 if (pcItems) *pcItems = index;
734 /* Set SCRIPT_ITEM */
735 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
741 /***********************************************************************
742 * ScriptStringAnalyse (USP10.@)
745 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
746 int cGlyphs, int iCharset, DWORD dwFlags,
747 int iReqWidth, SCRIPT_CONTROL *psControl,
748 SCRIPT_STATE *psState, const int *piDx,
749 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
750 SCRIPT_STRING_ANALYSIS *pssa)
752 HRESULT hr = E_OUTOFMEMORY;
753 StringAnalysis *analysis = NULL;
754 int i, num_items = 255;
756 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
757 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
758 psControl, psState, piDx, pTabdef, pbInClass, pssa);
762 FIXME("Only Unicode strings are supported\n");
765 if (cString < 1 || !pString) return E_INVALIDARG;
766 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
768 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
769 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
771 /* FIXME: handle clipping */
772 analysis->clip_len = cString;
775 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
776 &analysis->numItems);
778 while (hr == E_OUTOFMEMORY)
783 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
786 analysis->pItem = tmp;
787 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
788 &analysis->numItems);
790 if (hr != S_OK) goto error;
792 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
793 ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
797 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
800 for (i = 0; i < analysis->numItems; i++)
802 SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
803 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
804 int numGlyphs = 1.5 * cChar + 16;
805 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
806 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
807 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
808 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
809 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
810 ABC *abc = heap_alloc_zero(sizeof(ABC));
811 int numGlyphsReturned;
813 /* FIXME: non unicode strings */
814 const WCHAR* pStr = (const WCHAR*)pString;
815 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
816 cChar, numGlyphs, &analysis->pItem[i].a,
817 glyphs, pwLogClust, psva, &numGlyphsReturned);
818 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
819 piAdvance, pGoffset, abc);
821 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
822 analysis->glyphs[i].glyphs = glyphs;
823 analysis->glyphs[i].pwLogClust = pwLogClust;
824 analysis->glyphs[i].piAdvance = piAdvance;
825 analysis->glyphs[i].psva = psva;
826 analysis->glyphs[i].pGoffset = pGoffset;
827 analysis->glyphs[i].abc = abc;
828 analysis->glyphs[i].iMaxPosX= -1;
835 heap_free(analysis->glyphs);
836 heap_free(analysis->logattrs);
837 heap_free(analysis->pItem);
838 heap_free(analysis->sc);
843 /***********************************************************************
844 * ScriptStringOut (USP10.@)
846 * This function takes the output of ScriptStringAnalyse and joins the segments
847 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
848 * only processes glyphs.
851 * ssa [I] buffer to hold the analysed string components
852 * iX [I] X axis displacement for output
853 * iY [I] Y axis displacement for output
854 * uOptions [I] flags controling output processing
855 * prc [I] rectangle coordinates
856 * iMinSel [I] starting pos for substringing output string
857 * iMaxSel [I] ending pos for substringing output string
858 * fDisabled [I] controls text highlighting
862 * Failure: is the value returned by ScriptTextOut
864 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
873 StringAnalysis *analysis;
878 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
879 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
881 if (!(analysis = ssa)) return E_INVALIDARG;
884 * Get storage for the output buffer for the consolidated strings
887 for (item = 0; item < analysis->numItems; item++)
889 cnt += analysis->glyphs[item].numGlyphs;
891 if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
894 * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
896 uOptions |= ETO_GLYPH_INDEX;
897 analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
900 * Copy the string items into the output buffer
903 TRACE("numItems %d\n", analysis->numItems);
906 for (item = 0; item < analysis->numItems; item++)
908 memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
909 sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
911 TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
912 for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
913 TRACE("%04x", glyphs[x]);
916 cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
919 hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
920 uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
921 analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
922 TRACE("ScriptTextOut hr=%08x\n", hr);
925 * Free the output buffer and script cache
931 /***********************************************************************
932 * ScriptStringCPtoX (USP10.@)
935 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
939 StringAnalysis* analysis = ssa;
941 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
943 if (!ssa || !pX) return S_FALSE;
945 /* icp out of range */
948 analysis->invalid = TRUE;
952 for(i=0; i<analysis->numItems; i++)
954 int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
956 /* initialize max extents for uninitialized runs */
957 if (analysis->glyphs[i].iMaxPosX == -1)
959 if (analysis->pItem[i].a.fRTL)
960 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
961 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
962 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
964 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
965 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
966 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
971 runningX += analysis->glyphs[i].iMaxPosX;
976 ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
977 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
978 &analysis->pItem[i].a, &offset);
985 /* icp out of range */
986 analysis->invalid = TRUE;
990 /***********************************************************************
991 * ScriptStringXtoCP (USP10.@)
994 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
996 StringAnalysis* analysis = ssa;
1000 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1002 if (!ssa || !piCh || !piTrailing) return S_FALSE;
1007 if (analysis->pItem[0].a.fRTL)
1010 *piTrailing = FALSE;
1020 for(i=0; i<analysis->numItems; i++)
1022 int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1023 /* initialize max extents for uninitialized runs */
1024 if (analysis->glyphs[i].iMaxPosX == -1)
1026 if (analysis->pItem[i].a.fRTL)
1027 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1028 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1029 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1031 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1032 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1033 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1036 if (iX > analysis->glyphs[i].iMaxPosX)
1038 iX -= analysis->glyphs[i].iMaxPosX;
1043 ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1044 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1045 &analysis->pItem[i].a, piCh, piTrailing);
1052 *piCh = analysis->pItem[analysis->numItems].iCharPos;
1053 *piTrailing = FALSE;
1059 /***********************************************************************
1060 * ScriptStringFree (USP10.@)
1062 * Free a string analysis.
1065 * pssa [I] string analysis.
1069 * Failure: Non-zero HRESULT value.
1071 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1073 StringAnalysis* analysis;
1077 TRACE("(%p)\n", pssa);
1079 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1081 invalid = analysis->invalid;
1082 ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
1084 for (i = 0; i < analysis->numItems; i++)
1086 heap_free(analysis->glyphs[i].glyphs);
1087 heap_free(analysis->glyphs[i].pwLogClust);
1088 heap_free(analysis->glyphs[i].piAdvance);
1089 heap_free(analysis->glyphs[i].psva);
1090 heap_free(analysis->glyphs[i].pGoffset);
1091 heap_free(analysis->glyphs[i].abc);
1094 heap_free(analysis->glyphs);
1095 heap_free(analysis->pItem);
1096 heap_free(analysis->logattrs);
1097 heap_free(analysis->sz);
1098 heap_free(analysis->sc);
1099 heap_free(analysis);
1101 if (invalid) return E_INVALIDARG;
1105 /***********************************************************************
1106 * ScriptCPtoX (USP10.@)
1109 HRESULT WINAPI ScriptCPtoX(int iCP,
1113 const WORD *pwLogClust,
1114 const SCRIPT_VISATTR *psva,
1115 const int *piAdvance,
1116 const SCRIPT_ANALYSIS *psa,
1124 float special_size = 0.0;
1128 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1129 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1132 if (psa->fRTL && ! psa->fLogicalOrder)
1140 int max_clust = pwLogClust[0];
1142 for (item=0; item < cGlyphs; item++)
1143 if (pwLogClust[item] > max_clust)
1145 ERR("We do not handle non reversed clusters properly\n");
1150 for (item = max_clust; item >=0; item --)
1151 iMaxPos += piAdvance[item];
1155 for (item=0; item < iCP && item < cGlyphs; item++)
1157 if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
1160 int clust = pwLogClust[item];
1165 for (check = item+1; check < cGlyphs; check++)
1167 if (pwLogClust[check] == clust)
1176 if (check >= cGlyphs && !iMaxPos)
1178 for (check = clust; check < cGlyphs; check++)
1179 special_size += piAdvance[check];
1181 special_size /= (cChars - item);
1182 iPosX += special_size;
1185 iPosX += piAdvance[clust] / (float)clust_size;
1187 else if (iSpecial != -1)
1188 iPosX += special_size;
1189 else /* (iCluster != -1) */
1190 iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1195 iPosX = iMaxPos - iPosX;
1201 TRACE("*piX=%d\n", *piX);
1205 /***********************************************************************
1206 * ScriptXtoCP (USP10.@)
1209 HRESULT WINAPI ScriptXtoCP(int iX,
1212 const WORD *pwLogClust,
1213 const SCRIPT_VISATTR *psva,
1214 const int *piAdvance,
1215 const SCRIPT_ANALYSIS *psa,
1225 float special_size = 0.0;
1228 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1229 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1230 psa, piCP, piTrailing);
1232 if (psa->fRTL && ! psa->fLogicalOrder)
1237 int max_clust = pwLogClust[0];
1246 for (item=0; item < cGlyphs; item++)
1247 if (pwLogClust[item] > max_clust)
1249 ERR("We do not handle non reversed clusters properly\n");
1261 iPosX = iLastPosX = 0;
1266 for (; iPosX <= iX && item < cGlyphs && item >= 0; item+=direction)
1269 if (iSpecial == -1 &&
1272 ((direction > 0 && iCluster+clust_size <= item) ||
1273 (direction < 0 && iCluster-clust_size >= item))
1279 int clust = pwLogClust[item];
1284 for (check = item+direction; check < cGlyphs && check >= 0; check+=direction)
1286 if (pwLogClust[check] == clust)
1295 if (check >= cGlyphs && direction > 0)
1297 for (check = clust; check < cGlyphs; check++)
1298 special_size += piAdvance[check];
1300 special_size /= (cChars - item);
1301 iPosX += special_size;
1304 iPosX += piAdvance[clust] / (float)clust_size;
1306 else if (iSpecial != -1)
1307 iPosX += special_size;
1308 else /* (iCluster != -1) */
1309 iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1316 if (item < cGlyphs && ((iPosX - iLastPosX) / 2.0) + iX > iPosX)
1323 if (iX == iLastPosX)
1325 if (iX >= iLastPosX && iX <= iPosX)
1328 if (iLastPosX == iX)
1330 else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
1338 TRACE("*piCP=%d\n", *piCP);
1339 TRACE("*piTrailing=%d\n", *piTrailing);
1343 /***********************************************************************
1344 * ScriptBreak (USP10.@)
1346 * Retrieve line break information.
1349 * chars [I] Array of characters.
1350 * sa [I] String analysis.
1351 * la [I] Array of logical attribute structures.
1357 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1361 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1363 if (!la) return S_FALSE;
1365 for (i = 0; i < count; i++)
1367 memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1369 /* FIXME: set the other flags */
1370 la[i].fWhiteSpace = (chars[i] == ' ');
1371 la[i].fCharStop = 1;
1373 if (i > 0 && la[i - 1].fWhiteSpace)
1375 la[i].fSoftBreak = 1;
1376 la[i].fWordStop = 1;
1382 /***********************************************************************
1383 * ScriptIsComplex (USP10.@)
1385 * Determine if a string is complex.
1388 * chars [I] Array of characters to test.
1389 * len [I] Length in characters.
1397 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1401 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1403 for (i = 0; i < len; i++)
1407 if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1410 script = get_char_script(chars[i]);
1411 if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1412 (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1418 /***********************************************************************
1419 * ScriptShape (USP10.@)
1421 * Produce glyphs and visual attributes for a run.
1424 * hdc [I] Device context.
1425 * psc [I/O] Opaque pointer to a script cache.
1426 * pwcChars [I] Array of characters specifying the run.
1427 * cChars [I] Number of characters in pwcChars.
1428 * cMaxGlyphs [I] Length of pwOutGlyphs.
1429 * psa [I/O] Script analysis.
1430 * pwOutGlyphs [O] Array of glyphs.
1431 * pwLogClust [O] Array of logical cluster info.
1432 * psva [O] Array of visual attributes.
1433 * pcGlyphs [O] Number of glyphs returned.
1437 * Failure: Non-zero HRESULT value.
1439 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
1440 int cChars, int cMaxGlyphs,
1441 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1442 SCRIPT_VISATTR *psva, int *pcGlyphs)
1448 TRACE("(%p, %p, %s, %d, %d, %p, %p, %p, %p, %p)\n", hdc, psc, debugstr_wn(pwcChars, cChars),
1449 cChars, cMaxGlyphs, psa, pwOutGlyphs, pwLogClust, psva, pcGlyphs);
1451 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1452 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1454 if (!psva || !pcGlyphs) return E_INVALIDARG;
1455 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1456 rtl = (!psa->fLogicalOrder && psa->fRTL);
1459 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1460 if (!pwLogClust) return E_FAIL;
1462 /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
1463 for (i = 0; i < cChars; i++)
1466 if (rtl) idx = cChars - 1 - i;
1467 /* FIXME: set to better values */
1468 psva[i].uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1469 psva[i].fClusterStart = 1;
1470 psva[i].fDiacritic = 0;
1471 psva[i].fZeroWidth = 0;
1472 psva[i].fReserved = 0;
1473 psva[i].fShapeReserved = 0;
1475 pwLogClust[i] = idx;
1478 if (!psa->fNoGlyphIndex)
1481 if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
1483 rChars = heap_alloc(sizeof(WCHAR) * cChars);
1484 if (!rChars) return E_OUTOFMEMORY;
1485 for (i = 0; i < cChars; i++)
1489 if (rtl) idx = cChars - 1 - i;
1491 chInput = mirror_char(pwcChars[idx]);
1493 chInput = pwcChars[idx];
1494 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1497 if (!hdc) return E_PENDING;
1498 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1499 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1501 rChars[i] = chInput;
1504 if (get_cache_pitch_family(psc) & TMPF_TRUETYPE)
1506 SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1507 SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
1513 TRACE("no glyph translation\n");
1514 for (i = 0; i < cChars; i++)
1517 /* No mirroring done here */
1518 if (rtl) idx = cChars - 1 - i;
1519 pwOutGlyphs[i] = pwcChars[idx];
1526 /***********************************************************************
1527 * ScriptPlace (USP10.@)
1529 * Produce advance widths for a run.
1532 * hdc [I] Device context.
1533 * psc [I/O] Opaque pointer to a script cache.
1534 * pwGlyphs [I] Array of glyphs.
1535 * cGlyphs [I] Number of glyphs in pwGlyphs.
1536 * psva [I] Array of visual attributes.
1537 * psa [I/O] String analysis.
1538 * piAdvance [O] Array of advance widths.
1539 * pGoffset [O] Glyph offsets.
1540 * pABC [O] Combined ABC width.
1544 * Failure: Non-zero HRESULT value.
1546 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
1547 int cGlyphs, const SCRIPT_VISATTR *psva,
1548 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1553 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1554 piAdvance, pGoffset, pABC);
1556 if (!psva) return E_INVALIDARG;
1557 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1558 if (!pGoffset) return E_FAIL;
1560 if (pABC) memset(pABC, 0, sizeof(ABC));
1561 for (i = 0; i < cGlyphs; i++)
1564 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1566 if (!hdc) return E_PENDING;
1567 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1569 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1574 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1576 abc.abcA = abc.abcC = 0;
1578 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1582 pABC->abcA += abc.abcA;
1583 pABC->abcB += abc.abcB;
1584 pABC->abcC += abc.abcC;
1586 /* FIXME: set to more reasonable values */
1587 pGoffset[i].du = pGoffset[i].dv = 0;
1588 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1591 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1595 /***********************************************************************
1596 * ScriptGetCMap (USP10.@)
1598 * Retrieve glyph indices.
1601 * hdc [I] Device context.
1602 * psc [I/O] Opaque pointer to a script cache.
1603 * pwcInChars [I] Array of Unicode characters.
1604 * cChars [I] Number of characters in pwcInChars.
1605 * dwFlags [I] Flags.
1606 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
1610 * Failure: Non-zero HRESULT value.
1612 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1613 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1618 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1619 cChars, dwFlags, pwOutGlyphs);
1621 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1625 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1627 for (i = 0; i < cChars; i++)
1630 if (dwFlags == SGCM_RTL)
1631 inChar = mirror_char(pwcInChars[i]);
1633 inChar = pwcInChars[i];
1634 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1637 if (!hdc) return E_PENDING;
1638 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1639 if (glyph == 0xffff)
1644 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1650 TRACE("no glyph translation\n");
1651 for (i = 0; i < cChars; i++)
1654 if (dwFlags == SGCM_RTL)
1655 inChar = mirror_char(pwcInChars[i]);
1657 inChar = pwcInChars[i];
1658 pwOutGlyphs[i] = inChar;
1664 /***********************************************************************
1665 * ScriptTextOut (USP10.@)
1668 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
1669 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
1670 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1671 const int *piJustify, const GOFFSET *pGoffset)
1675 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1676 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1677 piAdvance, piJustify, pGoffset);
1679 if (!hdc || !psc) return E_INVALIDARG;
1680 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1682 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1683 fuOptions |= ETO_IGNORELANGUAGE;
1684 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
1685 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
1687 if (psa->fRTL && psa->fLogicalOrder)
1692 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1694 return E_OUTOFMEMORY;
1696 for (i = 0; i < cGlyphs; i++)
1697 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
1699 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
1701 heap_free(rtlGlyphs);
1704 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
1710 /***********************************************************************
1711 * ScriptCacheGetHeight (USP10.@)
1713 * Retrieve the height of the font in the cache.
1716 * hdc [I] Device context.
1717 * psc [I/O] Opaque pointer to a script cache.
1718 * height [O] Receives font height.
1722 * Failure: Non-zero HRESULT value.
1724 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
1728 TRACE("(%p, %p, %p)\n", hdc, psc, height);
1730 if (!height) return E_INVALIDARG;
1731 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1733 *height = get_cache_height(psc);
1737 /***********************************************************************
1738 * ScriptGetGlyphABCWidth (USP10.@)
1740 * Retrieve the width of a glyph.
1743 * hdc [I] Device context.
1744 * psc [I/O] Opaque pointer to a script cache.
1745 * glyph [I] Glyph to retrieve the width for.
1746 * abc [O] ABC widths of the glyph.
1750 * Failure: Non-zero HRESULT value.
1752 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
1756 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
1758 if (!abc) return E_INVALIDARG;
1759 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1761 if (!get_cache_glyph_widths(psc, glyph, abc))
1763 if (!hdc) return E_PENDING;
1764 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1766 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
1771 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
1773 abc->abcA = abc->abcC = 0;
1775 set_cache_glyph_widths(psc, glyph, abc);
1780 /***********************************************************************
1781 * ScriptLayout (USP10.@)
1783 * Map embedding levels to visual and/or logical order.
1786 * runs [I] Size of level array.
1787 * level [I] Array of embedding levels.
1788 * vistolog [O] Map of embedding levels from visual to logical order.
1789 * logtovis [O] Map of embedding levels from logical to visual order.
1793 * Failure: Non-zero HRESULT value.
1796 * This stub works correctly for any sequence of a single
1797 * embedding level but not for sequences of different
1798 * embedding levels, i.e. mixtures of RTL and LTR scripts.
1800 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
1805 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
1807 if (!level || (!vistolog && !logtovis))
1808 return E_INVALIDARG;
1810 indexs = heap_alloc(sizeof(int) * runs);
1812 return E_OUTOFMEMORY;
1817 for( ich = 0; ich < runs; ich++)
1822 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1823 for (ich = 0; ich < runs; ich++)
1824 vistolog[ich] = indexs[ich];
1830 for( ich = 0; ich < runs; ich++)
1835 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1836 for (ich = 0; ich < runs; ich++)
1837 logtovis[ich] = indexs[ich];
1844 /***********************************************************************
1845 * ScriptStringGetLogicalWidths (USP10.@)
1847 * Returns logical widths from a string analysis.
1850 * ssa [I] string analysis.
1851 * piDx [O] logical widths returned.
1855 * Failure: a non-zero HRESULT.
1857 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
1860 StringAnalysis *analysis = ssa;
1862 TRACE("%p, %p\n", ssa, piDx);
1864 if (!analysis) return S_FALSE;
1866 for (i = 0; i < analysis->numItems; i++)
1868 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1870 piDx[next] = analysis->glyphs[i].piAdvance[j];
1877 /***********************************************************************
1878 * ScriptStringValidate (USP10.@)
1880 * Validate a string analysis.
1883 * ssa [I] string analysis.
1887 * Failure: S_FALSE if invalid sequences are found
1888 * or a non-zero HRESULT if it fails.
1890 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
1892 StringAnalysis *analysis = ssa;
1894 TRACE("(%p)\n", ssa);
1896 if (!analysis) return E_INVALIDARG;
1897 return (analysis->invalid) ? S_FALSE : S_OK;
1900 /***********************************************************************
1901 * ScriptString_pSize (USP10.@)
1903 * Retrieve width and height of an analysed string.
1906 * ssa [I] string analysis.
1909 * Success: Pointer to a SIZE structure.
1912 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
1915 StringAnalysis *analysis = ssa;
1917 TRACE("(%p)\n", ssa);
1919 if (!analysis) return NULL;
1923 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
1924 analysis->sz->cy = analysis->sc->tm.tmHeight;
1926 analysis->sz->cx = 0;
1927 for (i = 0; i < analysis->numItems; i++)
1928 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1929 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
1931 return analysis->sz;
1934 /***********************************************************************
1935 * ScriptString_pLogAttr (USP10.@)
1937 * Retrieve logical attributes of an analysed string.
1940 * ssa [I] string analysis.
1943 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
1946 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
1948 StringAnalysis *analysis = ssa;
1950 TRACE("(%p)\n", ssa);
1952 if (!analysis) return NULL;
1953 return analysis->logattrs;
1956 /***********************************************************************
1957 * ScriptString_pcOutChars (USP10.@)
1959 * Retrieve the length of a string after clipping.
1962 * ssa [I] String analysis.
1965 * Success: Pointer to the length.
1968 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
1970 StringAnalysis *analysis = ssa;
1972 TRACE("(%p)\n", ssa);
1974 if (!analysis) return NULL;
1975 return &analysis->clip_len;
1978 /***********************************************************************
1979 * ScriptStringGetOrder (USP10.@)
1981 * Retrieve a glyph order map.
1984 * ssa [I] String analysis.
1985 * order [I/O] Array of glyph positions.
1989 * Failure: a non-zero HRESULT.
1991 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
1995 StringAnalysis *analysis = ssa;
1997 TRACE("(%p)\n", ssa);
1999 if (!analysis) return S_FALSE;
2001 /* FIXME: handle RTL scripts */
2002 for (i = 0, k = 0; i < analysis->numItems; i++)
2003 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
2009 /***********************************************************************
2010 * ScriptGetLogicalWidths (USP10.@)
2012 * Convert advance widths to logical widths.
2015 * sa [I] Script analysis.
2016 * nbchars [I] Number of characters.
2017 * nbglyphs [I] Number of glyphs.
2018 * glyph_width [I] Array of glyph widths.
2019 * log_clust [I] Array of logical clusters.
2020 * sva [I] Visual attributes.
2021 * widths [O] Array of logical widths.
2025 * Failure: a non-zero HRESULT.
2027 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
2028 const int *glyph_width, const WORD *log_clust,
2029 const SCRIPT_VISATTR *sva, int *widths)
2033 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
2034 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
2037 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
2041 /***********************************************************************
2042 * ScriptApplyLogicalWidth (USP10.@)
2044 * Generate glyph advance widths.
2047 * dx [I] Array of logical advance widths.
2048 * num_chars [I] Number of characters.
2049 * num_glyphs [I] Number of glyphs.
2050 * log_clust [I] Array of logical clusters.
2051 * sva [I] Visual attributes.
2052 * advance [I] Array of glyph advance widths.
2053 * sa [I] Script analysis.
2054 * abc [I/O] Summed ABC widths.
2055 * justify [O] Array of glyph advance widths.
2059 * Failure: a non-zero HRESULT.
2061 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
2062 const WORD *log_clust, const SCRIPT_VISATTR *sva,
2063 const int *advance, const SCRIPT_ANALYSIS *sa,
2064 ABC *abc, int *justify)
2068 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
2069 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
2071 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
2075 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
2076 int num_glyphs, int dx, int min_kashida, int *justify)
2080 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
2082 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];