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 /* Georgian: U+10A0–U+10FF */
81 { Script_Georgian, 0x10a0, 0x10ff, 0, 0},
82 /* Phonetic Extensions: U+1D00–U+1DBF */
83 { Script_Latin, 0x1d00, 0x1dbf, 0, 0},
84 /* Latin Extended Additional: U+1E00–U+1EFF */
85 { Script_Latin, 0x1e00, 0x1eff, 0, 0},
86 /* Greek Extended: U+1F00–U+1FFF */
87 { Script_Greek, 0x1f00, 0x1fff, 0, 0},
88 /* Latin Extended-C: U+2C60–U+2C7F */
89 { Script_Latin, 0x2c60, 0x2c7f, 0, 0},
90 /* Georgian: U+2D00–U+2D2F */
91 { Script_Georgian, 0x2d00, 0x2d2f, 0, 0},
92 /* Cyrillic Extended-A: U+2DE0–U+2DFF */
93 { Script_Cyrillic, 0x2de0, 0x2dff, 0, 0},
94 /* Cyrillic Extended-B: U+A640–U+A69F */
95 { Script_Cyrillic, 0xa640, 0xa69f, 0, 0},
96 /* Modifier Tone Letters: U+A700–U+A71F */
97 /* Latin Extended-D: U+A720–U+A7FF */
98 { Script_Latin, 0xa700, 0xa7ff, 0, 0},
99 /* Latin Ligatures: U+FB00–U+FB06 */
100 { Script_Latin, 0xfb00, 0xfb06, 0, 0},
101 /* Armenian ligatures U+FB13..U+FB17 */
102 { Script_Armenian, 0xfb13, 0xfb17, 0, 0},
103 /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
104 { Script_Hebrew, 0xfb1d, 0xfb4f, 0, 0},
105 /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
106 { Script_Arabic, 0xfb50, 0xfdff, 0, 0},
107 /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
108 { Script_Arabic, 0xfe70, 0xfeff, 0, 0},
110 { SCRIPT_UNDEFINED, 0, 0, 0}
113 typedef struct _scriptData
116 SCRIPT_PROPERTIES props;
119 /* the must be in order so that the index matches the Script value */
120 static const scriptData scriptInformation[] = {
121 {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
122 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
123 {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
124 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
125 {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
126 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
127 {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
128 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
129 {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
130 {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0}},
131 {{Script_Punctuation, 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_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
134 {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0}},
135 {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
136 {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
137 {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
138 {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
139 {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
140 {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0}},
141 {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
142 {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
143 {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
144 {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
145 {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
146 {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
147 {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
148 {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
149 {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
150 {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
151 {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
152 {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
155 static const SCRIPT_PROPERTIES *script_props[] =
157 &scriptInformation[0].props, &scriptInformation[1].props,
158 &scriptInformation[2].props, &scriptInformation[3].props,
159 &scriptInformation[4].props, &scriptInformation[5].props,
160 &scriptInformation[6].props, &scriptInformation[7].props,
161 &scriptInformation[8].props, &scriptInformation[9].props,
162 &scriptInformation[10].props, &scriptInformation[11].props,
163 &scriptInformation[12].props, &scriptInformation[13].props,
164 &scriptInformation[14].props, &scriptInformation[15].props
172 SCRIPT_VISATTR* psva;
186 StringGlyphs* glyphs;
187 SCRIPT_LOGATTR* logattrs;
191 static inline void *heap_alloc(SIZE_T size)
193 return HeapAlloc(GetProcessHeap(), 0, size);
196 static inline void *heap_alloc_zero(SIZE_T size)
198 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
201 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
203 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
206 static inline BOOL heap_free(LPVOID mem)
208 return HeapFree(GetProcessHeap(), 0, mem);
211 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
213 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
216 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
218 return ((ScriptCache *)*psc)->tm.tmHeight;
221 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
223 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
226 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
228 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
230 if (!block) return 0;
231 return block[c & GLYPH_BLOCK_MASK];
234 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
236 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
238 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
239 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
242 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
244 static const ABC nil;
245 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
247 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
248 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
252 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
254 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
256 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
257 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
261 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
265 if (!psc) return E_INVALIDARG;
266 if (*psc) return S_OK;
267 if (!hdc) return E_PENDING;
269 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
270 if (!GetTextMetricsW(hdc, &sc->tm))
275 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
281 TRACE("<- %p\n", sc);
285 static WCHAR mirror_char( WCHAR ch )
287 extern const WCHAR wine_mirror_map[];
288 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
291 static WORD get_char_script( WCHAR ch)
296 if (ch == 0xc || ch == 0x20 || ch == 0x202f)
299 GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
302 return SCRIPT_UNDEFINED;
305 return Script_Control;
310 if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
313 if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
315 if (scriptRanges[i].numericScript && type & C1_DIGIT)
316 return scriptRanges[i].numericScript;
317 if (scriptRanges[i].punctScript && type & C1_PUNCT)
318 return scriptRanges[i].punctScript;
319 return scriptRanges[i].script;
324 return SCRIPT_UNDEFINED;
327 /***********************************************************************
331 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
335 case DLL_PROCESS_ATTACH:
336 DisableThreadLibraryCalls(hInstDLL);
338 case DLL_PROCESS_DETACH:
344 /***********************************************************************
345 * ScriptFreeCache (USP10.@)
347 * Free a script cache.
350 * psc [I/O] Script cache.
354 * Failure: Non-zero HRESULT value.
356 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
363 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
365 heap_free(((ScriptCache *)*psc)->glyphs[i]);
366 heap_free(((ScriptCache *)*psc)->widths[i]);
368 heap_free(((ScriptCache *)*psc)->GSUB_Table);
369 heap_free(((ScriptCache *)*psc)->features);
376 /***********************************************************************
377 * ScriptGetProperties (USP10.@)
379 * Retrieve a list of script properties.
382 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
383 * num [I] Pointer to the number of scripts.
387 * Failure: Non-zero HRESULT value.
390 * Behaviour matches WinXP.
392 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
394 TRACE("(%p,%p)\n", props, num);
396 if (!props && !num) return E_INVALIDARG;
398 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
399 if (props) *props = script_props;
404 /***********************************************************************
405 * ScriptGetFontProperties (USP10.@)
407 * Get information on special glyphs.
410 * hdc [I] Device context.
411 * psc [I/O] Opaque pointer to a script cache.
412 * sfp [O] Font properties structure.
414 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
418 TRACE("%p,%p,%p\n", hdc, psc, sfp);
420 if (!sfp) return E_INVALIDARG;
421 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
423 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
426 /* return something sensible? */
428 sfp->wgDefault = get_cache_default_char(psc);
430 sfp->wgKashida = 0xffff;
431 sfp->iKashidaWidth = 0;
436 /***********************************************************************
437 * ScriptRecordDigitSubstitution (USP10.@)
439 * Record digit substitution settings for a given locale.
442 * locale [I] Locale identifier.
443 * sds [I] Structure to record substitution settings.
447 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
450 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
452 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
456 TRACE("0x%x, %p\n", locale, sds);
458 /* This implementation appears to be correct for all languages, but it's
459 * not clear if sds->DigitSubstitute is ever set to anything except
460 * CONTEXT or NONE in reality */
462 if (!sds) return E_POINTER;
464 locale = ConvertDefaultLocale(locale);
466 if (!IsValidLocale(locale, LCID_INSTALLED))
469 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
470 sds->TraditionalDigitLanguage = plgid;
472 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
473 sds->NationalDigitLanguage = plgid;
475 sds->NationalDigitLanguage = LANG_ENGLISH;
477 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
478 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
483 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
484 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
486 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
489 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
492 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
495 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
503 /***********************************************************************
504 * ScriptApplyDigitSubstitution (USP10.@)
506 * Apply digit substitution settings.
509 * sds [I] Structure with recorded substitution settings.
510 * sc [I] Script control structure.
511 * ss [I] Script state structure.
515 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
517 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
518 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
520 SCRIPT_DIGITSUBSTITUTE psds;
522 TRACE("%p, %p, %p\n", sds, sc, ss);
524 if (!sc || !ss) return E_POINTER;
528 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
532 sc->uDefaultLanguage = LANG_ENGLISH;
533 sc->fContextDigits = 0;
534 ss->fDigitSubstitute = 0;
536 switch (sds->DigitSubstitute) {
537 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
538 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
539 case SCRIPT_DIGITSUBSTITUTE_NONE:
540 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
547 /***********************************************************************
548 * ScriptItemize (USP10.@)
550 * Split a Unicode string into shapeable parts.
553 * pwcInChars [I] String to split.
554 * cInChars [I] Number of characters in pwcInChars.
555 * cMaxItems [I] Maximum number of items to return.
556 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
557 * psState [I] Pointer to a SCRIPT_STATE structure.
558 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
559 * pcItems [O] Number of script items returned.
563 * Failure: Non-zero HRESULT value.
565 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
566 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
567 SCRIPT_ITEM *pItems, int *pcItems)
570 #define Numeric_space 0x0020
572 int cnt = 0, index = 0;
573 int New_Script = SCRIPT_UNDEFINED;
577 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
578 psControl, psState, pItems, pcItems);
580 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
583 if (psState && psControl)
586 levels = heap_alloc_zero(cInChars * sizeof(WORD));
588 return E_OUTOFMEMORY;
590 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
591 baselevel = levels[0];
592 for (i = 0; i < cInChars; i++)
593 if (levels[i]!=levels[0])
602 pItems[index].iCharPos = 0;
603 pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
607 pItems[index].a.fRTL = odd(levels[cnt]);
608 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
609 pItems[index].a.s.uBidiLevel = levels[cnt];
611 else if (!pItems[index].a.s.uBidiLevel)
613 pItems[index].a.s.uBidiLevel = baselevel;
614 pItems[index].a.fLayoutRTL = odd(baselevel);
615 pItems[index].a.fRTL = odd(baselevel);
618 TRACE("New_Level=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
619 levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript, index, cnt,
620 pItems[index].iCharPos);
622 for (cnt=1; cnt < cInChars; cnt++)
624 if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel))
627 if(pwcInChars[cnt] != Numeric_space)
628 New_Script = get_char_script(pwcInChars[cnt]);
630 if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel)) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
632 TRACE("New_Level = %i, New_Script=%d, eScript=%d ", levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript);
634 if (index+1 > cMaxItems)
635 return E_OUTOFMEMORY;
637 pItems[index].iCharPos = cnt;
638 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
640 pItems[index].a = scriptInformation[New_Script].a;
643 pItems[index].a.fRTL = odd(levels[cnt]);
644 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
645 pItems[index].a.s.uBidiLevel = levels[cnt];
647 else if (!pItems[index].a.s.uBidiLevel)
649 pItems[index].a.s.uBidiLevel = baselevel;
650 pItems[index].a.fLayoutRTL = odd(baselevel);
651 pItems[index].a.fRTL = odd(baselevel);
654 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
658 /* While not strictly necessary according to the spec, make sure the n+1
659 * item is set up to prevent random behaviour if the caller erroneously
660 * checks the n+1 structure */
662 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
664 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
666 /* Set one SCRIPT_STATE item being returned */
667 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
668 if (pcItems) *pcItems = index;
670 /* Set SCRIPT_ITEM */
671 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
676 /***********************************************************************
677 * ScriptStringAnalyse (USP10.@)
680 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
681 int cGlyphs, int iCharset, DWORD dwFlags,
682 int iReqWidth, SCRIPT_CONTROL *psControl,
683 SCRIPT_STATE *psState, const int *piDx,
684 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
685 SCRIPT_STRING_ANALYSIS *pssa)
687 HRESULT hr = E_OUTOFMEMORY;
688 StringAnalysis *analysis = NULL;
689 int i, num_items = 255;
691 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
692 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
693 psControl, psState, piDx, pTabdef, pbInClass, pssa);
697 FIXME("Only Unicode strings are supported\n");
700 if (cString < 1 || !pString) return E_INVALIDARG;
701 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
703 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
704 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
706 /* FIXME: handle clipping */
707 analysis->clip_len = cString;
710 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
711 &analysis->numItems);
713 while (hr == E_OUTOFMEMORY)
718 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
721 analysis->pItem = tmp;
722 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
723 &analysis->numItems);
725 if (hr != S_OK) goto error;
727 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
728 ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
732 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
735 for (i = 0; i < analysis->numItems; i++)
737 SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
738 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
739 int numGlyphs = 1.5 * cChar + 16;
740 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
741 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
742 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
743 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
744 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
745 ABC *abc = heap_alloc_zero(sizeof(ABC));
746 int numGlyphsReturned;
748 /* FIXME: non unicode strings */
749 const WCHAR* pStr = (const WCHAR*)pString;
750 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
751 cChar, numGlyphs, &analysis->pItem[i].a,
752 glyphs, pwLogClust, psva, &numGlyphsReturned);
753 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
754 piAdvance, pGoffset, abc);
756 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
757 analysis->glyphs[i].glyphs = glyphs;
758 analysis->glyphs[i].pwLogClust = pwLogClust;
759 analysis->glyphs[i].piAdvance = piAdvance;
760 analysis->glyphs[i].psva = psva;
761 analysis->glyphs[i].pGoffset = pGoffset;
762 analysis->glyphs[i].abc = abc;
769 heap_free(analysis->glyphs);
770 heap_free(analysis->logattrs);
771 heap_free(analysis->pItem);
772 heap_free(analysis->sc);
777 /***********************************************************************
778 * ScriptStringOut (USP10.@)
780 * This function takes the output of ScriptStringAnalyse and joins the segments
781 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
782 * only processes glyphs.
785 * ssa [I] buffer to hold the analysed string components
786 * iX [I] X axis displacement for output
787 * iY [I] Y axis displacement for output
788 * uOptions [I] flags controling output processing
789 * prc [I] rectangle coordinates
790 * iMinSel [I] starting pos for substringing output string
791 * iMaxSel [I] ending pos for substringing output string
792 * fDisabled [I] controls text highlighting
796 * Failure: is the value returned by ScriptTextOut
798 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
807 StringAnalysis *analysis;
812 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
813 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
815 if (!(analysis = ssa)) return E_INVALIDARG;
818 * Get storage for the output buffer for the consolidated strings
821 for (item = 0; item < analysis->numItems; item++)
823 cnt += analysis->glyphs[item].numGlyphs;
825 if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
828 * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
830 uOptions |= ETO_GLYPH_INDEX;
831 analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
834 * Copy the string items into the output buffer
837 TRACE("numItems %d\n", analysis->numItems);
840 for (item = 0; item < analysis->numItems; item++)
842 memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
843 sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
845 TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
846 for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
847 TRACE("%04x", glyphs[x]);
850 cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
853 hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
854 uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
855 analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
856 TRACE("ScriptTextOut hr=%08x\n", hr);
859 * Free the output buffer and script cache
865 /***********************************************************************
866 * ScriptStringCPtoX (USP10.@)
869 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
874 StringAnalysis* analysis = ssa;
876 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
878 if (!ssa || !pX) return S_FALSE;
880 /* icp out of range */
883 analysis->invalid = TRUE;
887 for(i=0; i<analysis->numItems; i++)
889 for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
891 if(runningCp == icp && fTrailing == FALSE)
896 runningX += analysis->glyphs[i].piAdvance[j];
897 if(runningCp == icp && fTrailing == TRUE)
906 /* icp out of range */
907 analysis->invalid = TRUE;
911 /***********************************************************************
912 * ScriptStringXtoCP (USP10.@)
915 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
917 StringAnalysis* analysis = ssa;
924 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
926 if (!ssa || !piCh || !piTrailing) return S_FALSE;
936 for(i=0; i<analysis->numItems; i++)
938 for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
940 width = analysis->glyphs[i].piAdvance[j];
941 if(iX < (runningX + width))
944 if((iX - runningX) > width/2)
956 *piCh = analysis->pItem[analysis->numItems].iCharPos;
963 /***********************************************************************
964 * ScriptStringFree (USP10.@)
966 * Free a string analysis.
969 * pssa [I] string analysis.
973 * Failure: Non-zero HRESULT value.
975 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
977 StringAnalysis* analysis;
981 TRACE("(%p)\n", pssa);
983 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
985 invalid = analysis->invalid;
986 ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
988 for (i = 0; i < analysis->numItems; i++)
990 heap_free(analysis->glyphs[i].glyphs);
991 heap_free(analysis->glyphs[i].pwLogClust);
992 heap_free(analysis->glyphs[i].piAdvance);
993 heap_free(analysis->glyphs[i].psva);
994 heap_free(analysis->glyphs[i].pGoffset);
995 heap_free(analysis->glyphs[i].abc);
998 heap_free(analysis->glyphs);
999 heap_free(analysis->pItem);
1000 heap_free(analysis->logattrs);
1001 heap_free(analysis->sz);
1002 heap_free(analysis->sc);
1003 heap_free(analysis);
1005 if (invalid) return E_INVALIDARG;
1009 /***********************************************************************
1010 * ScriptCPtoX (USP10.@)
1013 HRESULT WINAPI ScriptCPtoX(int iCP,
1017 const WORD *pwLogClust,
1018 const SCRIPT_VISATTR *psva,
1019 const int *piAdvance,
1020 const SCRIPT_ANALYSIS *psa,
1026 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1027 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1029 for (item=0; item < cGlyphs; item++) /* total piAdvance */
1030 fMaxPosX += piAdvance[item];
1031 iPosX = (fMaxPosX/cGlyphs)*(iCP+fTrailing);
1032 if (iPosX > fMaxPosX)
1034 *piX = iPosX; /* Return something in range */
1036 TRACE("*piX=%d\n", *piX);
1040 /***********************************************************************
1041 * ScriptXtoCP (USP10.@)
1044 HRESULT WINAPI ScriptXtoCP(int iX,
1047 const WORD *pwLogClust,
1048 const SCRIPT_VISATTR *psva,
1049 const int *piAdvance,
1050 const SCRIPT_ANALYSIS *psa,
1058 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1059 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1060 psa, piCP, piTrailing);
1061 if (iX < 0) /* iX is before start of run */
1068 for (item=0; item < cGlyphs; item++) /* total piAdvance */
1069 fMaxPosX += piAdvance[item];
1071 if (iX >= fMaxPosX) /* iX too large */
1074 *piTrailing = FALSE;
1078 fAvePosX = fMaxPosX / cGlyphs;
1080 for (item = 1; item < cGlyphs && iPosX < iX; item++)
1082 if (iPosX - iX > fAvePosX/2)
1085 *piTrailing = 1; /* yep we are over halfway */
1087 *piCP = item -1; /* Return character position */
1088 TRACE("*piCP=%d iPposX=%d\n", *piCP, iPosX);
1092 /***********************************************************************
1093 * ScriptBreak (USP10.@)
1095 * Retrieve line break information.
1098 * chars [I] Array of characters.
1099 * sa [I] String analysis.
1100 * la [I] Array of logical attribute structures.
1106 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1110 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1112 if (!la) return S_FALSE;
1114 for (i = 0; i < count; i++)
1116 memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1118 /* FIXME: set the other flags */
1119 la[i].fWhiteSpace = (chars[i] == ' ');
1120 la[i].fCharStop = 1;
1122 if (i > 0 && la[i - 1].fWhiteSpace)
1124 la[i].fSoftBreak = 1;
1125 la[i].fWordStop = 1;
1131 /***********************************************************************
1132 * ScriptIsComplex (USP10.@)
1134 * Determine if a string is complex.
1137 * chars [I] Array of characters to test.
1138 * len [I] Length in characters.
1146 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1150 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1152 for (i = 0; i < len; i++)
1156 if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1159 script = get_char_script(chars[i]);
1160 if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1161 (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1167 /***********************************************************************
1168 * ScriptShape (USP10.@)
1170 * Produce glyphs and visual attributes for a run.
1173 * hdc [I] Device context.
1174 * psc [I/O] Opaque pointer to a script cache.
1175 * pwcChars [I] Array of characters specifying the run.
1176 * cChars [I] Number of characters in pwcChars.
1177 * cMaxGlyphs [I] Length of pwOutGlyphs.
1178 * psa [I/O] Script analysis.
1179 * pwOutGlyphs [O] Array of glyphs.
1180 * pwLogClust [O] Array of logical cluster info.
1181 * psva [O] Array of visual attributes.
1182 * pcGlyphs [O] Number of glyphs returned.
1186 * Failure: Non-zero HRESULT value.
1188 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
1189 int cChars, int cMaxGlyphs,
1190 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1191 SCRIPT_VISATTR *psva, int *pcGlyphs)
1197 TRACE("(%p, %p, %s, %d, %d, %p, %p, %p, %p, %p)\n", hdc, psc, debugstr_wn(pwcChars, cChars),
1198 cChars, cMaxGlyphs, psa, pwOutGlyphs, pwLogClust, psva, pcGlyphs);
1200 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1201 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1203 if (!psva || !pcGlyphs) return E_INVALIDARG;
1204 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1205 rtl = (!psa->fLogicalOrder && psa->fRTL);
1208 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1209 if (!pwLogClust) return E_FAIL;
1211 /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
1212 for (i = 0; i < cChars; i++)
1215 if (rtl) idx = cChars - 1 - i;
1216 /* FIXME: set to better values */
1217 psva[i].uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1218 psva[i].fClusterStart = 1;
1219 psva[i].fDiacritic = 0;
1220 psva[i].fZeroWidth = 0;
1221 psva[i].fReserved = 0;
1222 psva[i].fShapeReserved = 0;
1224 pwLogClust[i] = idx;
1227 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1229 WCHAR *rChars = heap_alloc(sizeof(WCHAR) * cChars);
1230 if (!rChars) return E_OUTOFMEMORY;
1231 for (i = 0; i < cChars; i++)
1235 if (rtl) idx = cChars - 1 - i;
1237 chInput = mirror_char(pwcChars[idx]);
1239 chInput = pwcChars[idx];
1240 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1243 if (!hdc) return E_PENDING;
1244 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1245 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1247 rChars[i] = chInput;
1249 SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1250 SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
1255 TRACE("no glyph translation\n");
1256 for (i = 0; i < cChars; i++)
1259 /* No mirroring done here */
1260 if (rtl) idx = cChars - 1 - i;
1261 pwOutGlyphs[i] = pwcChars[idx];
1268 /***********************************************************************
1269 * ScriptPlace (USP10.@)
1271 * Produce advance widths for a run.
1274 * hdc [I] Device context.
1275 * psc [I/O] Opaque pointer to a script cache.
1276 * pwGlyphs [I] Array of glyphs.
1277 * cGlyphs [I] Number of glyphs in pwGlyphs.
1278 * psva [I] Array of visual attributes.
1279 * psa [I/O] String analysis.
1280 * piAdvance [O] Array of advance widths.
1281 * pGoffset [O] Glyph offsets.
1282 * pABC [O] Combined ABC width.
1286 * Failure: Non-zero HRESULT value.
1288 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
1289 int cGlyphs, const SCRIPT_VISATTR *psva,
1290 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1295 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1296 piAdvance, pGoffset, pABC);
1298 if (!psva) return E_INVALIDARG;
1299 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1300 if (!pGoffset) return E_FAIL;
1302 if (pABC) memset(pABC, 0, sizeof(ABC));
1303 for (i = 0; i < cGlyphs; i++)
1306 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1308 if (!hdc) return E_PENDING;
1309 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1311 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1316 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1318 abc.abcA = abc.abcC = 0;
1320 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1324 pABC->abcA += abc.abcA;
1325 pABC->abcB += abc.abcB;
1326 pABC->abcC += abc.abcC;
1328 /* FIXME: set to more reasonable values */
1329 pGoffset[i].du = pGoffset[i].dv = 0;
1330 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1333 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1337 /***********************************************************************
1338 * ScriptGetCMap (USP10.@)
1340 * Retrieve glyph indices.
1343 * hdc [I] Device context.
1344 * psc [I/O] Opaque pointer to a script cache.
1345 * pwcInChars [I] Array of Unicode characters.
1346 * cChars [I] Number of characters in pwcInChars.
1347 * dwFlags [I] Flags.
1348 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
1352 * Failure: Non-zero HRESULT value.
1354 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1355 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1360 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1361 cChars, dwFlags, pwOutGlyphs);
1363 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1367 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1369 for (i = 0; i < cChars; i++)
1372 if (dwFlags == SGCM_RTL)
1373 inChar = mirror_char(pwcInChars[i]);
1375 inChar = pwcInChars[i];
1376 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1379 if (!hdc) return E_PENDING;
1380 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1381 if (glyph == 0xffff)
1386 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1392 TRACE("no glyph translation\n");
1393 for (i = 0; i < cChars; i++)
1396 if (dwFlags == SGCM_RTL)
1397 inChar = mirror_char(pwcInChars[i]);
1399 inChar = pwcInChars[i];
1400 pwOutGlyphs[i] = inChar;
1406 /***********************************************************************
1407 * ScriptTextOut (USP10.@)
1410 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
1411 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
1412 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1413 const int *piJustify, const GOFFSET *pGoffset)
1417 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1418 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1419 piAdvance, piJustify, pGoffset);
1421 if (!hdc || !psc) return E_INVALIDARG;
1422 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1424 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1425 fuOptions |= ETO_IGNORELANGUAGE;
1426 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
1427 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
1429 if (psa->fRTL && psa->fLogicalOrder)
1434 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1436 return E_OUTOFMEMORY;
1438 for (i = 0; i < cGlyphs; i++)
1439 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
1441 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
1443 heap_free(rtlGlyphs);
1446 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
1452 /***********************************************************************
1453 * ScriptCacheGetHeight (USP10.@)
1455 * Retrieve the height of the font in the cache.
1458 * hdc [I] Device context.
1459 * psc [I/O] Opaque pointer to a script cache.
1460 * height [O] Receives font height.
1464 * Failure: Non-zero HRESULT value.
1466 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
1470 TRACE("(%p, %p, %p)\n", hdc, psc, height);
1472 if (!height) return E_INVALIDARG;
1473 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1475 *height = get_cache_height(psc);
1479 /***********************************************************************
1480 * ScriptGetGlyphABCWidth (USP10.@)
1482 * Retrieve the width of a glyph.
1485 * hdc [I] Device context.
1486 * psc [I/O] Opaque pointer to a script cache.
1487 * glyph [I] Glyph to retrieve the width for.
1488 * abc [O] ABC widths of the glyph.
1492 * Failure: Non-zero HRESULT value.
1494 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
1498 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
1500 if (!abc) return E_INVALIDARG;
1501 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1503 if (!get_cache_glyph_widths(psc, glyph, abc))
1505 if (!hdc) return E_PENDING;
1506 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1508 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
1513 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
1515 abc->abcA = abc->abcC = 0;
1517 set_cache_glyph_widths(psc, glyph, abc);
1522 /***********************************************************************
1523 * ScriptLayout (USP10.@)
1525 * Map embedding levels to visual and/or logical order.
1528 * runs [I] Size of level array.
1529 * level [I] Array of embedding levels.
1530 * vistolog [O] Map of embedding levels from visual to logical order.
1531 * logtovis [O] Map of embedding levels from logical to visual order.
1535 * Failure: Non-zero HRESULT value.
1538 * This stub works correctly for any sequence of a single
1539 * embedding level but not for sequences of different
1540 * embedding levels, i.e. mixtures of RTL and LTR scripts.
1542 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
1547 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
1549 if (!level || (!vistolog && !logtovis))
1550 return E_INVALIDARG;
1552 indexs = heap_alloc(sizeof(int) * runs);
1554 return E_OUTOFMEMORY;
1559 for( ich = 0; ich < runs; ich++)
1564 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1565 for (ich = 0; ich < runs; ich++)
1566 vistolog[ich] = indexs[ich];
1572 for( ich = 0; ich < runs; ich++)
1577 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1578 for (ich = 0; ich < runs; ich++)
1579 logtovis[ich] = indexs[ich];
1586 /***********************************************************************
1587 * ScriptStringGetLogicalWidths (USP10.@)
1589 * Returns logical widths from a string analysis.
1592 * ssa [I] string analysis.
1593 * piDx [O] logical widths returned.
1597 * Failure: a non-zero HRESULT.
1599 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
1602 StringAnalysis *analysis = ssa;
1604 TRACE("%p, %p\n", ssa, piDx);
1606 if (!analysis) return S_FALSE;
1608 for (i = 0; i < analysis->numItems; i++)
1610 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1612 piDx[next] = analysis->glyphs[i].piAdvance[j];
1619 /***********************************************************************
1620 * ScriptStringValidate (USP10.@)
1622 * Validate a string analysis.
1625 * ssa [I] string analysis.
1629 * Failure: S_FALSE if invalid sequences are found
1630 * or a non-zero HRESULT if it fails.
1632 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
1634 StringAnalysis *analysis = ssa;
1636 TRACE("(%p)\n", ssa);
1638 if (!analysis) return E_INVALIDARG;
1639 return (analysis->invalid) ? S_FALSE : S_OK;
1642 /***********************************************************************
1643 * ScriptString_pSize (USP10.@)
1645 * Retrieve width and height of an analysed string.
1648 * ssa [I] string analysis.
1651 * Success: Pointer to a SIZE structure.
1654 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
1657 StringAnalysis *analysis = ssa;
1659 TRACE("(%p)\n", ssa);
1661 if (!analysis) return NULL;
1665 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
1666 analysis->sz->cy = analysis->sc->tm.tmHeight;
1668 analysis->sz->cx = 0;
1669 for (i = 0; i < analysis->numItems; i++)
1670 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1671 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
1673 return analysis->sz;
1676 /***********************************************************************
1677 * ScriptString_pLogAttr (USP10.@)
1679 * Retrieve logical attributes of an analysed string.
1682 * ssa [I] string analysis.
1685 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
1688 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
1690 StringAnalysis *analysis = ssa;
1692 TRACE("(%p)\n", ssa);
1694 if (!analysis) return NULL;
1695 return analysis->logattrs;
1698 /***********************************************************************
1699 * ScriptString_pcOutChars (USP10.@)
1701 * Retrieve the length of a string after clipping.
1704 * ssa [I] String analysis.
1707 * Success: Pointer to the length.
1710 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
1712 StringAnalysis *analysis = ssa;
1714 TRACE("(%p)\n", ssa);
1716 if (!analysis) return NULL;
1717 return &analysis->clip_len;
1720 /***********************************************************************
1721 * ScriptStringGetOrder (USP10.@)
1723 * Retrieve a glyph order map.
1726 * ssa [I] String analysis.
1727 * order [I/O] Array of glyph positions.
1731 * Failure: a non-zero HRESULT.
1733 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
1737 StringAnalysis *analysis = ssa;
1739 TRACE("(%p)\n", ssa);
1741 if (!analysis) return S_FALSE;
1743 /* FIXME: handle RTL scripts */
1744 for (i = 0, k = 0; i < analysis->numItems; i++)
1745 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
1751 /***********************************************************************
1752 * ScriptGetLogicalWidths (USP10.@)
1754 * Convert advance widths to logical widths.
1757 * sa [I] Script analysis.
1758 * nbchars [I] Number of characters.
1759 * nbglyphs [I] Number of glyphs.
1760 * glyph_width [I] Array of glyph widths.
1761 * log_clust [I] Array of logical clusters.
1762 * sva [I] Visual attributes.
1763 * widths [O] Array of logical widths.
1767 * Failure: a non-zero HRESULT.
1769 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
1770 const int *glyph_width, const WORD *log_clust,
1771 const SCRIPT_VISATTR *sva, int *widths)
1775 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
1776 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
1779 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
1783 /***********************************************************************
1784 * ScriptApplyLogicalWidth (USP10.@)
1786 * Generate glyph advance widths.
1789 * dx [I] Array of logical advance widths.
1790 * num_chars [I] Number of characters.
1791 * num_glyphs [I] Number of glyphs.
1792 * log_clust [I] Array of logical clusters.
1793 * sva [I] Visual attributes.
1794 * advance [I] Array of glyph advance widths.
1795 * sa [I] Script analysis.
1796 * abc [I/O] Summed ABC widths.
1797 * justify [O] Array of glyph advance widths.
1801 * Failure: a non-zero HRESULT.
1803 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
1804 const WORD *log_clust, const SCRIPT_VISATTR *sva,
1805 const int *advance, const SCRIPT_ANALYSIS *sa,
1806 ABC *abc, int *justify)
1810 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
1811 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
1813 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
1817 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
1818 int num_glyphs, int dx, int min_kashida, int *justify)
1822 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
1824 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];