server: Move socket async activation to sock_poll_event.
[wine] / dlls / usp10 / usp10.c
1 /*
2  * Implementation of Uniscribe Script Processor (usp10.dll)
3  *
4  * Copyright 2005 Steven Edwards for CodeWeavers
5  * Copyright 2006 Hans Leidekker
6  * Copyright 2010 CodeWeavers, Aric Stewart
7  *
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.
12  *
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.
17  *
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
21  *
22  * Notes:
23  * Uniscribe allows for processing of complex scripts such as joining
24  * and filtering characters and bi-directional text with custom line breaks.
25  */
26
27 #include <stdarg.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "usp10.h"
35
36 #include "usp10_internal.h"
37
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
42
43 static const SCRIPT_PROPERTIES props[] =
44 {
45     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
46     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
47     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
48     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
49     { 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
50     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
51     { 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
52     { 8, 0, 0, 0, 0, 161, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
53     { 25, 0, 0, 0, 0, 204, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
54     { 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
55     { 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
56     { 42, 0, 0, 0, 0, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
57     { 9, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
58     { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0 },
59     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
60     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
61     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
62     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
63     { 18, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
64     { 18, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
65     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
66     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
67     { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
68     { 9, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
69     { 13, 0, 1, 0, 1, 177, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
70     { 13, 0, 1, 0, 0, 177, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
71     { 1, 0, 1, 0, 0, 178, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
72     { 1, 1, 1, 0, 0, 178, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
73     { 41, 1, 1, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
74     { 32, 1, 1, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
75     { 90, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0 },
76     { 30, 0, 1, 1, 1, 222, 0, 0, 1, 0, 1, 0, 0, 0, 1 },
77     { 30, 1, 1, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
78     { 30, 0, 1, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
79     { 57, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
80     { 57, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
81     { 73, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
82     { 73, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
83     { 69, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
84     { 69, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
85     { 69, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
86     { 70, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
87     { 70, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
88     { 71, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
89     { 71, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
90     { 72, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
91     { 72, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
92     { 74, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
93     { 74, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
94     { 75, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
95     { 75, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
96     { 76, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
97     { 76, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
98     { 81, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0 },
99     { 81, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
100     { 84, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0 },
101     { 84, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
102     { 83, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
103     { 83, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
104     { 85, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
105     { 85, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
106     { 80, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
107     { 80, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
108     { 94, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
109     { 94, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
110     { 101, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
111     { 93, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
112     { 92, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
113     { 9, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
114     { 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
115     { 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
116     { 91, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
117     { 9, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
118     { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
119 };
120
121 static const SCRIPT_PROPERTIES *script_props[] =
122 {
123     &props[0], &props[1], &props[2], &props[3],
124     &props[4], &props[5], &props[6], &props[7],
125     &props[8], &props[9], &props[11], &props[12],
126     &props[13], &props[14], &props[15], &props[16],
127     &props[17], &props[18], &props[19], &props[20],
128     &props[21], &props[22], &props[23], &props[24],
129     &props[25], &props[26], &props[27], &props[28],
130     &props[29], &props[30], &props[31], &props[32],
131     &props[33], &props[34], &props[35], &props[36],
132     &props[37], &props[38], &props[39], &props[40],
133     &props[41], &props[42], &props[43], &props[44],
134     &props[45], &props[46], &props[47], &props[48],
135     &props[49], &props[50], &props[51], &props[52],
136     &props[53], &props[54], &props[55], &props[56],
137     &props[57], &props[58], &props[59], &props[60],
138     &props[61], &props[62], &props[63], &props[64],
139     &props[65], &props[66], &props[67], &props[68],
140     &props[69], &props[70], &props[71], &props[72],
141     &props[73]
142 };
143
144 #define GLYPH_BLOCK_SHIFT 8
145 #define GLYPH_BLOCK_SIZE  (1UL << GLYPH_BLOCK_SHIFT)
146 #define GLYPH_BLOCK_MASK  (GLYPH_BLOCK_SIZE - 1)
147 #define GLYPH_MAX         65536
148
149 typedef struct {
150     LOGFONTW lf;
151     TEXTMETRICW tm;
152     WORD *glyphs[GLYPH_MAX / GLYPH_BLOCK_SIZE];
153     ABC *widths[GLYPH_MAX / GLYPH_BLOCK_SIZE];
154 } ScriptCache;
155
156 typedef struct {
157     int numGlyphs;
158     WORD* glyphs;
159     WORD* pwLogClust;
160     int* piAdvance;
161     SCRIPT_VISATTR* psva;
162     GOFFSET* pGoffset;
163     ABC* abc;
164 } StringGlyphs;
165
166 typedef struct {
167     HDC hdc;
168     BOOL invalid;
169     int clip_len;
170     ScriptCache *sc;
171     int cItems;
172     int cMaxGlyphs;
173     SCRIPT_ITEM* pItem;
174     int numItems;
175     StringGlyphs* glyphs;
176     SCRIPT_LOGATTR* logattrs;
177     SIZE* sz;
178 } StringAnalysis;
179
180 static inline void *heap_alloc(SIZE_T size)
181 {
182     return HeapAlloc(GetProcessHeap(), 0, size);
183 }
184
185 static inline void *heap_alloc_zero(SIZE_T size)
186 {
187     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
188 }
189
190 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
191 {
192     return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
193 }
194
195 static inline BOOL heap_free(LPVOID mem)
196 {
197     return HeapFree(GetProcessHeap(), 0, mem);
198 }
199
200 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
201 {
202     return ((ScriptCache *)*psc)->tm.tmDefaultChar;
203 }
204
205 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
206 {
207     return ((ScriptCache *)*psc)->tm.tmHeight;
208 }
209
210 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
211 {
212     return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
213 }
214
215 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
216 {
217     WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
218
219     if (!block) return 0;
220     return block[c & GLYPH_BLOCK_MASK];
221 }
222
223 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
224 {
225     WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
226
227     if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
228     return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
229 }
230
231 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
232 {
233     static const ABC nil;
234     ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
235
236     if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
237     memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
238     return TRUE;
239 }
240
241 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
242 {
243     ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
244
245     if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
246     memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
247     return TRUE;
248 }
249
250 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
251 {
252     ScriptCache *sc;
253
254     if (!psc) return E_INVALIDARG;
255     if (*psc) return S_OK;
256     if (!hdc) return E_PENDING;
257
258     if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
259     if (!GetTextMetricsW(hdc, &sc->tm))
260     {
261         heap_free(sc);
262         return E_INVALIDARG;
263     }
264     if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
265     {
266         heap_free(sc);
267         return E_INVALIDARG;
268     }
269     *psc = sc;
270     TRACE("<- %p\n", sc);
271     return S_OK;
272 }
273
274 static WCHAR mirror_char( WCHAR ch )
275 {
276     extern const WCHAR wine_mirror_map[];
277     return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
278 }
279
280 /***********************************************************************
281  *      DllMain
282  *
283  */
284 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
285 {
286     switch(fdwReason)
287     {
288     case DLL_PROCESS_ATTACH:
289         DisableThreadLibraryCalls(hInstDLL);
290         break;
291     case DLL_PROCESS_DETACH:
292         break;
293     }
294     return TRUE;
295 }
296
297 /***********************************************************************
298  *      ScriptFreeCache (USP10.@)
299  *
300  * Free a script cache.
301  *
302  * PARAMS
303  *   psc [I/O] Script cache.
304  *
305  * RETURNS
306  *  Success: S_OK
307  *  Failure: Non-zero HRESULT value.
308  */
309 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
310 {
311     TRACE("%p\n", psc);
312
313     if (psc && *psc)
314     {
315         unsigned int i;
316         for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
317         {
318             heap_free(((ScriptCache *)*psc)->glyphs[i]);
319             heap_free(((ScriptCache *)*psc)->widths[i]);
320         }
321         heap_free(*psc);
322         *psc = NULL;
323     }
324     return S_OK;
325 }
326
327 /***********************************************************************
328  *      ScriptGetProperties (USP10.@)
329  *
330  * Retrieve a list of script properties.
331  *
332  * PARAMS
333  *  props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
334  *  num   [I] Pointer to the number of scripts.
335  *
336  * RETURNS
337  *  Success: S_OK
338  *  Failure: Non-zero HRESULT value.
339  *
340  * NOTES
341  *  Behaviour matches WinXP.
342  */
343 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
344 {
345     TRACE("(%p,%p)\n", props, num);
346
347     if (!props && !num) return E_INVALIDARG;
348
349     if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
350     if (props) *props = script_props;
351
352     return S_OK;
353 }
354
355 /***********************************************************************
356  *      ScriptGetFontProperties (USP10.@)
357  *
358  * Get information on special glyphs.
359  *
360  * PARAMS
361  *  hdc [I]   Device context.
362  *  psc [I/O] Opaque pointer to a script cache.
363  *  sfp [O]   Font properties structure.
364  */
365 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
366 {
367     HRESULT hr;
368
369     TRACE("%p,%p,%p\n", hdc, psc, sfp);
370
371     if (!sfp) return E_INVALIDARG;
372     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
373
374     if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
375         return E_INVALIDARG;
376
377     /* return something sensible? */
378     sfp->wgBlank = 0;
379     sfp->wgDefault = get_cache_default_char(psc);
380     sfp->wgInvalid = 0;
381     sfp->wgKashida = 0xffff;
382     sfp->iKashidaWidth = 0;
383
384     return S_OK;
385 }
386
387 /***********************************************************************
388  *      ScriptRecordDigitSubstitution (USP10.@)
389  *
390  *  Record digit substitution settings for a given locale.
391  *
392  *  PARAMS
393  *   locale [I] Locale identifier.
394  *   sds    [I] Structure to record substitution settings.
395  *
396  *  RETURNS
397  *   Success: S_OK
398  *   Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
399  *
400  *  SEE ALSO
401  *   http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
402  */
403 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
404 {
405     DWORD plgid, sub;
406
407     TRACE("0x%x, %p\n", locale, sds);
408
409     /* This implementation appears to be correct for all languages, but it's
410      * not clear if sds->DigitSubstitute is ever set to anything except 
411      * CONTEXT or NONE in reality */
412
413     if (!sds) return E_POINTER;
414
415     locale = ConvertDefaultLocale(locale);
416
417     if (!IsValidLocale(locale, LCID_INSTALLED))
418         return E_INVALIDARG;
419
420     plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
421     sds->TraditionalDigitLanguage = plgid;
422
423     if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
424         sds->NationalDigitLanguage = plgid;
425     else
426         sds->NationalDigitLanguage = LANG_ENGLISH;
427
428     if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
429                         (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
430
431     switch (sub)
432     {
433     case 0: 
434         if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
435             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
436         else
437             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
438         break;
439     case 1:
440         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
441         break;
442     case 2:
443         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
444         break;
445     default:
446         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
447         break;
448     }
449
450     sds->dwReserved = 0;
451     return S_OK;
452 }
453
454 /***********************************************************************
455  *      ScriptApplyDigitSubstitution (USP10.@)
456  *
457  *  Apply digit substitution settings.
458  *
459  *  PARAMS
460  *   sds [I] Structure with recorded substitution settings.
461  *   sc  [I] Script control structure.
462  *   ss  [I] Script state structure.
463  *
464  *  RETURNS
465  *   Success: S_OK
466  *   Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
467  */
468 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds, 
469                                             SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
470 {
471     SCRIPT_DIGITSUBSTITUTE psds;
472
473     TRACE("%p, %p, %p\n", sds, sc, ss);
474
475     if (!sc || !ss) return E_POINTER;
476     if (!sds)
477     {
478         sds = &psds;
479         if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
480             return E_INVALIDARG;
481     }
482
483     sc->uDefaultLanguage = LANG_ENGLISH;
484     sc->fContextDigits = 0;
485     ss->fDigitSubstitute = 0;
486
487     switch (sds->DigitSubstitute) {
488         case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
489         case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
490         case SCRIPT_DIGITSUBSTITUTE_NONE:
491         case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
492             return S_OK;
493         default:
494             return E_INVALIDARG;
495     }
496 }
497
498 /***********************************************************************
499  *      ScriptItemize (USP10.@)
500  *
501  * Split a Unicode string into shapeable parts.
502  *
503  * PARAMS
504  *  pwcInChars [I] String to split.
505  *  cInChars   [I] Number of characters in pwcInChars.
506  *  cMaxItems  [I] Maximum number of items to return.
507  *  psControl  [I] Pointer to a SCRIPT_CONTROL structure.
508  *  psState    [I] Pointer to a SCRIPT_STATE structure.
509  *  pItems     [O] Buffer to receive SCRIPT_ITEM structures.
510  *  pcItems    [O] Number of script items returned.
511  *
512  * RETURNS
513  *  Success: S_OK
514  *  Failure: Non-zero HRESULT value.
515  */
516 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
517                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
518                              SCRIPT_ITEM *pItems, int *pcItems)
519 {
520
521 #define Numeric_start 0x0030
522 #define Numeric_stop  0x0039
523 #define Numeric_space 0x0020
524 #define Arabic_start  0x0600
525 #define Arabic_stop   0x06ff
526 #define Hebrew_start  0x0590
527 #define Hebrew_stop   0x05ff
528 #define Syriac_start  0x0700
529 #define Syriac_stop   0x074f
530 #define Latin_start   0x0001
531 #define Latin_stop    0x024f
532 #define Script_Syriac  8
533 #define Script_Hebrew  7
534 #define Script_Arabic  6
535 #define Script_Latin   1
536 #define Script_Numeric 5
537 #define Script_CR      22
538 #define Script_LF      23
539
540     int   cnt = 0, index = 0;
541     int   New_Script = SCRIPT_UNDEFINED;
542     WORD  *levels = NULL;
543     WORD  baselevel = 0;
544
545     TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
546           psControl, psState, pItems, pcItems);
547
548     if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
549         return E_INVALIDARG;
550
551     if (psState && psControl)
552     {
553         int i;
554         levels = heap_alloc_zero(cInChars * sizeof(WORD));
555         if (!levels)
556             return E_OUTOFMEMORY;
557
558         BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
559         baselevel = levels[0];
560         for (i = 0; i < cInChars; i++)
561             if (levels[i]!=levels[0])
562                 break;
563         if (i >= cInChars)
564         {
565             heap_free(levels);
566             levels = NULL;
567         }
568     }
569
570     pItems[index].iCharPos = 0;
571     memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
572
573     if  (pwcInChars[cnt] == '\r')
574         pItems[index].a.eScript = Script_CR;
575     else
576     if  (pwcInChars[cnt] == '\n')
577         pItems[index].a.eScript = Script_LF;
578     else
579     if  (pwcInChars[cnt] >= Numeric_start && pwcInChars[cnt] <= Numeric_stop)
580         pItems[index].a.eScript = Script_Numeric;
581     else
582     if  (pwcInChars[cnt] >= Arabic_start && pwcInChars[cnt] <= Arabic_stop)
583         pItems[index].a.eScript = Script_Arabic;
584     else
585     if  (pwcInChars[cnt] >= Hebrew_start && pwcInChars[cnt] <= Hebrew_stop)
586         pItems[index].a.eScript = Script_Hebrew;
587     else
588     if  (pwcInChars[cnt] >= Syriac_start && pwcInChars[cnt] <= Syriac_stop)
589         pItems[index].a.eScript = Script_Syriac;
590     else
591     if  (pwcInChars[cnt] >= Latin_start && pwcInChars[cnt] <= Latin_stop)
592         pItems[index].a.eScript = Script_Latin;
593
594     if (levels)
595     {
596         pItems[index].a.fRTL = odd(levels[cnt]);
597         pItems[index].a.fLayoutRTL = odd(levels[cnt]);
598         pItems[index].a.s.uBidiLevel = levels[cnt];
599     }
600     else if ((pItems[index].a.eScript  == Script_Arabic) ||
601              (pItems[index].a.eScript  == Script_Hebrew) ||
602              (pItems[index].a.eScript  == Script_Syriac))
603     {
604         pItems[index].a.s.uBidiLevel = 1;
605         pItems[index].a.fRTL = 1;
606         pItems[index].a.fLayoutRTL = 1;
607     }
608     else
609     {
610         pItems[index].a.s.uBidiLevel = baselevel;
611         pItems[index].a.fLayoutRTL = odd(baselevel);
612         pItems[index].a.fRTL = odd(baselevel);
613     }
614
615     TRACE("New_Level=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
616           levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript, index, cnt,
617           pItems[index].iCharPos);
618
619     for (cnt=1; cnt < cInChars; cnt++)
620     {
621         if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel))
622             continue;
623
624         if  (pwcInChars[cnt] == '\r')
625             New_Script = Script_CR;
626         else
627         if  (pwcInChars[cnt] == '\n')
628             New_Script = Script_LF;
629         else
630         if  ((pwcInChars[cnt] >= Numeric_start && pwcInChars[cnt] <= Numeric_stop)
631              || (New_Script == Script_Numeric && pwcInChars[cnt] == Numeric_space))
632             New_Script = Script_Numeric;
633         else
634         if  ((pwcInChars[cnt] >= Arabic_start && pwcInChars[cnt] <= Arabic_stop)
635              || (New_Script == Script_Arabic && pwcInChars[cnt] == Numeric_space))
636             New_Script = Script_Arabic;
637         else
638         if  ((pwcInChars[cnt] >= Hebrew_start && pwcInChars[cnt] <= Hebrew_stop)
639              || (New_Script == Script_Hebrew && pwcInChars[cnt] == Numeric_space))
640             New_Script = Script_Hebrew;
641         else
642         if  ((pwcInChars[cnt] >= Syriac_start && pwcInChars[cnt] <= Syriac_stop)
643              || (New_Script == Script_Syriac && pwcInChars[cnt] == Numeric_space))
644             New_Script = Script_Syriac;
645         else
646         if  ((pwcInChars[cnt] >= Latin_start && pwcInChars[cnt] <= Latin_stop)
647              || (New_Script == Script_Latin && pwcInChars[cnt] == Numeric_space))
648             New_Script = Script_Latin;
649         else
650             New_Script = SCRIPT_UNDEFINED;
651
652         if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel)) || New_Script != pItems[index].a.eScript)
653         {
654             TRACE("New_Level = %i, New_Script=%d, eScript=%d ", levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript);
655             index++;
656             if  (index+1 > cMaxItems)
657                 return E_OUTOFMEMORY;
658
659             pItems[index].iCharPos = cnt;
660             memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
661
662             if (levels)
663             {
664                 pItems[index].a.fRTL = odd(levels[cnt]);
665                 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
666                 pItems[index].a.s.uBidiLevel = levels[cnt];
667             }
668             else if  ((New_Script == Script_Arabic) ||
669                       (New_Script == Script_Hebrew) ||
670                       (New_Script == Script_Syriac))
671             {
672                 pItems[index].a.s.uBidiLevel = 1;
673                 pItems[index].a.fRTL = 1;
674                 pItems[index].a.fLayoutRTL = 1;
675             }
676             else
677             {
678                 pItems[index].a.s.uBidiLevel = baselevel;
679                 pItems[index].a.fLayoutRTL = odd(baselevel);
680                 pItems[index].a.fRTL = odd(baselevel);
681             }
682
683             pItems[index].a.eScript = New_Script;
684
685             TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
686         }
687     }
688
689     /* While not strictly necessary according to the spec, make sure the n+1
690      * item is set up to prevent random behaviour if the caller erroneously
691      * checks the n+1 structure                                              */
692     index++;
693     memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
694
695     TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
696
697     /*  Set one SCRIPT_STATE item being returned  */
698     if  (index + 1 > cMaxItems) return E_OUTOFMEMORY;
699     if (pcItems) *pcItems = index;
700
701     /*  Set SCRIPT_ITEM                                     */
702     pItems[index].iCharPos = cnt;         /* the last item contains the ptr to the lastchar */
703     heap_free(levels);
704     return S_OK;
705 }
706
707 /***********************************************************************
708  *      ScriptStringAnalyse (USP10.@)
709  *
710  */
711 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
712                                    int cGlyphs, int iCharset, DWORD dwFlags,
713                                    int iReqWidth, SCRIPT_CONTROL *psControl,
714                                    SCRIPT_STATE *psState, const int *piDx,
715                                    SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
716                                    SCRIPT_STRING_ANALYSIS *pssa)
717 {
718     HRESULT hr = E_OUTOFMEMORY;
719     StringAnalysis *analysis = NULL;
720     int i, num_items = 255;
721
722     TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
723           hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
724           psControl, psState, piDx, pTabdef, pbInClass, pssa);
725
726     if (iCharset != -1)
727     {
728         FIXME("Only Unicode strings are supported\n");
729         return E_INVALIDARG;
730     }
731     if (cString < 1 || !pString) return E_INVALIDARG;
732     if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
733
734     if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
735     if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
736
737     /* FIXME: handle clipping */
738     analysis->clip_len = cString;
739     analysis->hdc = hdc;
740
741     hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
742                        &analysis->numItems);
743
744     while (hr == E_OUTOFMEMORY)
745     {
746         SCRIPT_ITEM *tmp;
747
748         num_items *= 2;
749         if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
750             goto error;
751
752         analysis->pItem = tmp;
753         hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
754                            &analysis->numItems);
755     }
756     if (hr != S_OK) goto error;
757
758     if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
759         ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
760     else
761         goto error;
762
763     if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
764         goto error;
765
766     for (i = 0; i < analysis->numItems; i++)
767     {
768         SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
769         int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
770         int numGlyphs = 1.5 * cChar + 16;
771         WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
772         WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
773         int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
774         SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
775         GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
776         ABC *abc = heap_alloc_zero(sizeof(ABC));
777         int numGlyphsReturned;
778
779         /* FIXME: non unicode strings */
780         const WCHAR* pStr = (const WCHAR*)pString;
781         hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
782                          cChar, numGlyphs, &analysis->pItem[i].a,
783                          glyphs, pwLogClust, psva, &numGlyphsReturned);
784         hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
785                          piAdvance, pGoffset, abc);
786
787         analysis->glyphs[i].numGlyphs = numGlyphsReturned;
788         analysis->glyphs[i].glyphs = glyphs;
789         analysis->glyphs[i].pwLogClust = pwLogClust;
790         analysis->glyphs[i].piAdvance = piAdvance;
791         analysis->glyphs[i].psva = psva;
792         analysis->glyphs[i].pGoffset = pGoffset;
793         analysis->glyphs[i].abc = abc;
794     }
795
796     *pssa = analysis;
797     return S_OK;
798
799 error:
800     heap_free(analysis->glyphs);
801     heap_free(analysis->logattrs);
802     heap_free(analysis->pItem);
803     heap_free(analysis->sc);
804     heap_free(analysis);
805     return hr;
806 }
807
808 /***********************************************************************
809  *      ScriptStringOut (USP10.@)
810  *
811  * This function takes the output of ScriptStringAnalyse and joins the segments
812  * of glyphs and passes the resulting string to ScriptTextOut.  ScriptStringOut
813  * only processes glyphs.
814  *
815  * Parameters:
816  *  ssa       [I] buffer to hold the analysed string components
817  *  iX        [I] X axis displacement for output
818  *  iY        [I] Y axis displacement for output
819  *  uOptions  [I] flags controling output processing
820  *  prc       [I] rectangle coordinates
821  *  iMinSel   [I] starting pos for substringing output string
822  *  iMaxSel   [I] ending pos for substringing output string
823  *  fDisabled [I] controls text highlighting
824  *
825  *  RETURNS
826  *   Success: S_OK
827  *   Failure: is the value returned by ScriptTextOut
828  */
829 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
830                                int iX,
831                                int iY, 
832                                UINT uOptions, 
833                                const RECT *prc, 
834                                int iMinSel, 
835                                int iMaxSel,
836                                BOOL fDisabled)
837 {
838     StringAnalysis *analysis;
839     WORD *glyphs;
840     int   item, cnt, x;
841     HRESULT hr;
842
843     TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
844          ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
845
846     if (!(analysis = ssa)) return E_INVALIDARG;
847
848     /*
849      * Get storage for the output buffer for the consolidated strings
850      */
851     cnt = 0;
852     for (item = 0; item < analysis->numItems; item++)
853     {
854         cnt += analysis->glyphs[item].numGlyphs;
855     }
856     if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
857
858     /*
859      * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
860      */
861     uOptions |= ETO_GLYPH_INDEX;
862     analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
863
864     /*
865      * Copy the string items into the output buffer
866      */
867
868     TRACE("numItems %d\n", analysis->numItems);
869
870     cnt = 0;
871     for (item = 0; item < analysis->numItems; item++)
872     {
873         memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
874               sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
875
876         TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
877         for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
878             TRACE("%04x", glyphs[x]);
879         TRACE("\n");
880
881         cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
882     }
883
884     hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
885                        uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
886                        analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
887     TRACE("ScriptTextOut hr=%08x\n", hr);
888
889     /*
890      * Free the output buffer and script cache
891      */
892     heap_free(glyphs);
893     return hr;
894 }
895
896 /***********************************************************************
897  *      ScriptStringCPtoX (USP10.@)
898  *
899  */
900 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
901 {
902     int i, j;
903     int runningX = 0;
904     int runningCp = 0;
905     StringAnalysis* analysis = ssa;
906
907     TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
908
909     if (!ssa || !pX) return S_FALSE;
910
911     /* icp out of range */
912     if(icp < 0)
913     {
914         analysis->invalid = TRUE;
915         return E_INVALIDARG;
916     }
917
918     for(i=0; i<analysis->numItems; i++)
919     {
920         for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
921         {
922             if(runningCp == icp && fTrailing == FALSE)
923             {
924                 *pX = runningX;
925                 return S_OK;
926             }
927             runningX += analysis->glyphs[i].piAdvance[j];
928             if(runningCp == icp && fTrailing == TRUE)
929             {
930                 *pX = runningX;
931                 return S_OK;
932             }
933             runningCp++;
934         }
935     }
936
937     /* icp out of range */
938     analysis->invalid = TRUE;
939     return E_INVALIDARG;
940 }
941
942 /***********************************************************************
943  *      ScriptStringXtoCP (USP10.@)
944  *
945  */
946 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing) 
947 {
948     StringAnalysis* analysis = ssa;
949     int i;
950     int j;
951     int runningX = 0;
952     int runningCp = 0;
953     int width;
954
955     TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
956
957     if (!ssa || !piCh || !piTrailing) return S_FALSE;
958
959     /* out of range */
960     if(iX < 0)
961     {
962         *piCh = -1;
963         *piTrailing = TRUE;
964         return S_OK;
965     }
966
967     for(i=0; i<analysis->numItems; i++)
968     {
969         for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
970         {
971             width = analysis->glyphs[i].piAdvance[j];
972             if(iX < (runningX + width))
973             {
974                 *piCh = runningCp;
975                 if((iX - runningX) > width/2)
976                     *piTrailing = TRUE;
977                 else
978                     *piTrailing = FALSE;
979                 return S_OK;
980             }
981             runningX += width;
982             runningCp++;
983         }
984     }
985
986     /* out of range */
987     *piCh = analysis->pItem[analysis->numItems].iCharPos;
988     *piTrailing = FALSE;
989
990     return S_OK;
991 }
992
993
994 /***********************************************************************
995  *      ScriptStringFree (USP10.@)
996  *
997  * Free a string analysis.
998  *
999  * PARAMS
1000  *  pssa [I] string analysis.
1001  *
1002  * RETURNS
1003  *  Success: S_OK
1004  *  Failure: Non-zero HRESULT value.
1005  */
1006 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1007 {
1008     StringAnalysis* analysis;
1009     BOOL invalid;
1010     int i;
1011
1012     TRACE("(%p)\n", pssa);
1013
1014     if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1015
1016     invalid = analysis->invalid;
1017     ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
1018
1019     for (i = 0; i < analysis->numItems; i++)
1020     {
1021         heap_free(analysis->glyphs[i].glyphs);
1022         heap_free(analysis->glyphs[i].pwLogClust);
1023         heap_free(analysis->glyphs[i].piAdvance);
1024         heap_free(analysis->glyphs[i].psva);
1025         heap_free(analysis->glyphs[i].pGoffset);
1026         heap_free(analysis->glyphs[i].abc);
1027     }
1028
1029     heap_free(analysis->glyphs);
1030     heap_free(analysis->pItem);
1031     heap_free(analysis->logattrs);
1032     heap_free(analysis->sz);
1033     heap_free(analysis->sc);
1034     heap_free(analysis);
1035
1036     if (invalid) return E_INVALIDARG;
1037     return S_OK;
1038 }
1039
1040 /***********************************************************************
1041  *      ScriptCPtoX (USP10.@)
1042  *
1043  */
1044 HRESULT WINAPI ScriptCPtoX(int iCP,
1045                            BOOL fTrailing,
1046                            int cChars,
1047                            int cGlyphs,
1048                            const WORD *pwLogClust,
1049                            const SCRIPT_VISATTR *psva,
1050                            const int *piAdvance,
1051                            const SCRIPT_ANALYSIS *psa,
1052                            int *piX)
1053 {
1054     int  item;
1055     int  iPosX;
1056     float  fMaxPosX = 0;
1057     TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1058           iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1059           psa, piX);
1060     for (item=0; item < cGlyphs; item++)            /* total piAdvance           */
1061         fMaxPosX += piAdvance[item];
1062     iPosX = (fMaxPosX/cGlyphs)*(iCP+fTrailing);
1063     if  (iPosX > fMaxPosX)
1064         iPosX = fMaxPosX;
1065     *piX = iPosX;                                    /* Return something in range */
1066
1067     TRACE("*piX=%d\n", *piX);
1068     return S_OK;
1069 }
1070
1071 /***********************************************************************
1072  *      ScriptXtoCP (USP10.@)
1073  *
1074  */
1075 HRESULT WINAPI ScriptXtoCP(int iX,
1076                            int cChars,
1077                            int cGlyphs,
1078                            const WORD *pwLogClust,
1079                            const SCRIPT_VISATTR *psva,
1080                            const int *piAdvance,
1081                            const SCRIPT_ANALYSIS *psa,
1082                            int *piCP,
1083                            int *piTrailing)
1084 {
1085     int item;
1086     int iPosX;
1087     float fMaxPosX = 1;
1088     float fAvePosX;
1089     TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1090           iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1091           psa, piCP, piTrailing);
1092     if  (iX < 0)                                    /* iX is before start of run */
1093     {
1094         *piCP = -1;
1095         *piTrailing = TRUE;
1096         return S_OK;
1097     }
1098
1099     for (item=0; item < cGlyphs; item++)            /* total piAdvance           */
1100         fMaxPosX += piAdvance[item];
1101
1102     if  (iX >= fMaxPosX)                            /* iX too large              */
1103     {
1104         *piCP = cChars;
1105         *piTrailing = FALSE;
1106         return S_OK;
1107     }
1108
1109     fAvePosX = fMaxPosX / cGlyphs;
1110     iPosX = fAvePosX;
1111     for (item = 1; item < cGlyphs  && iPosX < iX; item++)
1112         iPosX += fAvePosX;
1113     if  (iPosX - iX > fAvePosX/2)
1114         *piTrailing = 0;
1115     else
1116         *piTrailing = 1;                            /* yep we are over halfway */
1117
1118     *piCP = item -1;                                /* Return character position */
1119     TRACE("*piCP=%d iPposX=%d\n", *piCP, iPosX);
1120     return S_OK;
1121 }
1122
1123 /***********************************************************************
1124  *      ScriptBreak (USP10.@)
1125  *
1126  *  Retrieve line break information.
1127  *
1128  *  PARAMS
1129  *   chars [I] Array of characters.
1130  *   sa    [I] String analysis.
1131  *   la    [I] Array of logical attribute structures.
1132  *
1133  *  RETURNS
1134  *   Success: S_OK
1135  *   Failure: S_FALSE
1136  */
1137 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1138 {
1139     int i;
1140
1141     TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1142
1143     if (!la) return S_FALSE;
1144
1145     for (i = 0; i < count; i++)
1146     {
1147         memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1148
1149         /* FIXME: set the other flags */
1150         la[i].fWhiteSpace = (chars[i] == ' ');
1151         la[i].fCharStop = 1;
1152
1153         if (i > 0 && la[i - 1].fWhiteSpace)
1154         {
1155             la[i].fSoftBreak = 1;
1156             la[i].fWordStop = 1;
1157         }
1158     }
1159     return S_OK;
1160 }
1161
1162 static const struct
1163 {
1164     WCHAR start;
1165     WCHAR end;
1166     DWORD flag;
1167 }
1168 complex_ranges[] =
1169 {
1170     { 0, 0x0b, SIC_COMPLEX },
1171     { 0x0c, 0x0c, SIC_NEUTRAL },
1172     { 0x0d, 0x1f, SIC_COMPLEX },
1173     { 0x20, 0x2f, SIC_NEUTRAL },
1174     { 0x30, 0x39, SIC_ASCIIDIGIT },
1175     { 0x3a, 0x40, SIC_NEUTRAL },
1176     { 0x5b, 0x60, SIC_NEUTRAL },
1177     { 0x7b, 0x7e, SIC_NEUTRAL },
1178     { 0x7f, 0x9f, SIC_COMPLEX },
1179     { 0xa0, 0xa5, SIC_NEUTRAL },
1180     { 0xa7, 0xa8, SIC_NEUTRAL },
1181     { 0xab, 0xab, SIC_NEUTRAL },
1182     { 0xad, 0xad, SIC_NEUTRAL },
1183     { 0xaf, 0xaf, SIC_NEUTRAL },
1184     { 0xb0, 0xb1, SIC_NEUTRAL },
1185     { 0xb4, 0xb4, SIC_NEUTRAL },
1186     { 0xb6, 0xb8, SIC_NEUTRAL },
1187     { 0xbb, 0xbf, SIC_NEUTRAL },
1188     { 0xd7, 0xd7, SIC_NEUTRAL },
1189     { 0xf7, 0xf7, SIC_NEUTRAL },
1190     { 0x2b9, 0x2ba, SIC_NEUTRAL },
1191     { 0x2c2, 0x2cf, SIC_NEUTRAL },
1192     { 0x2d2, 0x2df, SIC_NEUTRAL },
1193     { 0x2e5, 0x2e9, SIC_COMPLEX },
1194     { 0x2ea, 0x2ed, SIC_NEUTRAL },
1195     { 0x300, 0x362, SIC_COMPLEX },
1196     { 0x530, 0x60b, SIC_COMPLEX },
1197     { 0x60c, 0x60d, SIC_NEUTRAL },
1198     { 0x60e, 0x669, SIC_COMPLEX },
1199     { 0x66a, 0x66a, SIC_NEUTRAL },
1200     { 0x66b, 0x6e8, SIC_COMPLEX },
1201     { 0x6e9, 0x6e9, SIC_NEUTRAL },
1202     { 0x6ea, 0x7bf, SIC_COMPLEX },
1203     { 0x900, 0x1360, SIC_COMPLEX },
1204     { 0x137d, 0x137f, SIC_COMPLEX },
1205     { 0x1680, 0x1680, SIC_NEUTRAL },
1206     { 0x1780, 0x18af, SIC_COMPLEX },
1207     { 0x2000, 0x200a, SIC_NEUTRAL },
1208     { 0x200b, 0x200f, SIC_COMPLEX },
1209     { 0x2010, 0x2016, SIC_NEUTRAL },
1210     { 0x2018, 0x2022, SIC_NEUTRAL },
1211     { 0x2024, 0x2028, SIC_NEUTRAL },
1212     { 0x2029, 0x202e, SIC_COMPLEX },
1213     { 0x202f, 0x2037, SIC_NEUTRAL },
1214     { 0x2039, 0x203c, SIC_NEUTRAL },
1215     { 0x2044, 0x2046, SIC_NEUTRAL },
1216     { 0x206a, 0x206f, SIC_COMPLEX },
1217     { 0x207a, 0x207e, SIC_NEUTRAL },
1218     { 0x208a, 0x20aa, SIC_NEUTRAL },
1219     { 0x20ac, 0x20cf, SIC_NEUTRAL },
1220     { 0x20d0, 0x20ff, SIC_COMPLEX },
1221     { 0x2103, 0x2103, SIC_NEUTRAL },
1222     { 0x2105, 0x2105, SIC_NEUTRAL },
1223     { 0x2109, 0x2109, SIC_NEUTRAL },
1224     { 0x2116, 0x2116, SIC_NEUTRAL },
1225     { 0x2121, 0x2122, SIC_NEUTRAL },
1226     { 0x212e, 0x212e, SIC_NEUTRAL },
1227     { 0x2153, 0x2154, SIC_NEUTRAL },
1228     { 0x215b, 0x215e, SIC_NEUTRAL },
1229     { 0x2190, 0x2199, SIC_NEUTRAL },
1230     { 0x21b8, 0x21b9, SIC_NEUTRAL },
1231     { 0x21d2, 0x21d2, SIC_NEUTRAL },
1232     { 0x21d4, 0x21d4, SIC_NEUTRAL },
1233     { 0x21e7, 0x21e7, SIC_NEUTRAL },
1234     { 0x2200, 0x2200, SIC_NEUTRAL },
1235     { 0x2202, 0x2203, SIC_NEUTRAL },
1236     { 0x2207, 0x2208, SIC_NEUTRAL },
1237     { 0x220b, 0x220b, SIC_NEUTRAL },
1238     { 0x220f, 0x220f, SIC_NEUTRAL },
1239     { 0x2211, 0x2213, SIC_NEUTRAL },
1240     { 0x2215, 0x2215, SIC_NEUTRAL },
1241     { 0x221a, 0x221a, SIC_NEUTRAL },
1242     { 0x221d, 0x2220, SIC_NEUTRAL },
1243     { 0x2223, 0x2223, SIC_NEUTRAL },
1244     { 0x2225, 0x2225, SIC_NEUTRAL },
1245     { 0x2227, 0x222c, SIC_NEUTRAL },
1246     { 0x222e, 0x222e, SIC_NEUTRAL },
1247     { 0x2234, 0x2237, SIC_NEUTRAL },
1248     { 0x223c, 0x223d, SIC_NEUTRAL },
1249     { 0x2248, 0x2248, SIC_NEUTRAL },
1250     { 0x224c, 0x224c, SIC_NEUTRAL },
1251     { 0x2252, 0x2252, SIC_NEUTRAL },
1252     { 0x2260, 0x2261, SIC_NEUTRAL },
1253     { 0x2264, 0x2267, SIC_NEUTRAL },
1254     { 0x226a, 0x226b, SIC_NEUTRAL },
1255     { 0x226e, 0x226f, SIC_NEUTRAL },
1256     { 0x2282, 0x2283, SIC_NEUTRAL },
1257     { 0x2286, 0x2287, SIC_NEUTRAL },
1258     { 0x2295, 0x2295, SIC_NEUTRAL },
1259     { 0x2299, 0x2299, SIC_NEUTRAL },
1260     { 0x22a5, 0x22a5, SIC_NEUTRAL },
1261     { 0x22bf, 0x22bf, SIC_NEUTRAL },
1262     { 0x2312, 0x2312, SIC_NEUTRAL },
1263     { 0x24ea, 0x24ea, SIC_COMPLEX },
1264     { 0x2500, 0x254b, SIC_NEUTRAL },
1265     { 0x2550, 0x256d, SIC_NEUTRAL },
1266     { 0x256e, 0x2574, SIC_NEUTRAL },
1267     { 0x2581, 0x258f, SIC_NEUTRAL },
1268     { 0x2592, 0x2595, SIC_NEUTRAL },
1269     { 0x25a0, 0x25a1, SIC_NEUTRAL },
1270     { 0x25a3, 0x25a9, SIC_NEUTRAL },
1271     { 0x25b2, 0x25b3, SIC_NEUTRAL },
1272     { 0x25b6, 0x25b7, SIC_NEUTRAL },
1273     { 0x25bc, 0x25bd, SIC_NEUTRAL },
1274     { 0x25c0, 0x25c1, SIC_NEUTRAL },
1275     { 0x25c6, 0x25c8, SIC_NEUTRAL },
1276     { 0x25cb, 0x25cb, SIC_NEUTRAL },
1277     { 0x25ce, 0x25d1, SIC_NEUTRAL },
1278     { 0x25e2, 0x25e5, SIC_NEUTRAL },
1279     { 0x25ef, 0x25ef, SIC_NEUTRAL },
1280     { 0x2605, 0x2606, SIC_NEUTRAL },
1281     { 0x2609, 0x2609, SIC_NEUTRAL },
1282     { 0x260e, 0x260f, SIC_NEUTRAL },
1283     { 0x261c, 0x261c, SIC_NEUTRAL },
1284     { 0x261e, 0x261e, SIC_NEUTRAL },
1285     { 0x2640, 0x2640, SIC_NEUTRAL },
1286     { 0x2642, 0x2642, SIC_NEUTRAL },
1287     { 0x2660, 0x2661, SIC_NEUTRAL },
1288     { 0x2663, 0x2665, SIC_NEUTRAL },
1289     { 0x2667, 0x266a, SIC_NEUTRAL },
1290     { 0x266c, 0x266d, SIC_NEUTRAL },
1291     { 0x266f, 0x266f, SIC_NEUTRAL },
1292     { 0x273d, 0x273d, SIC_NEUTRAL },
1293     { 0x2e80, 0x312f, SIC_COMPLEX },
1294     { 0x3190, 0x31bf, SIC_COMPLEX },
1295     { 0x31f0, 0x31ff, SIC_COMPLEX },
1296     { 0x3220, 0x325f, SIC_COMPLEX },
1297     { 0x3280, 0xa4ff, SIC_COMPLEX },
1298     { 0xd800, 0xdfff, SIC_COMPLEX },
1299     { 0xe000, 0xf8ff, SIC_NEUTRAL },
1300     { 0xf900, 0xfaff, SIC_COMPLEX },
1301     { 0xfb13, 0xfb28, SIC_COMPLEX },
1302     { 0xfb29, 0xfb29, SIC_NEUTRAL },
1303     { 0xfb2a, 0xfb4f, SIC_COMPLEX },
1304     { 0xfd3e, 0xfd3f, SIC_NEUTRAL },
1305     { 0xfdd0, 0xfdef, SIC_COMPLEX },
1306     { 0xfe20, 0xfe6f, SIC_COMPLEX },
1307     { 0xfeff, 0xfeff, SIC_COMPLEX },
1308     { 0xff01, 0xff5e, SIC_COMPLEX },
1309     { 0xff61, 0xff9f, SIC_COMPLEX },
1310     { 0xffe0, 0xffe6, SIC_COMPLEX },
1311     { 0xffe8, 0xffee, SIC_COMPLEX },
1312     { 0xfff9, 0xfffb, SIC_COMPLEX },
1313     { 0xfffe, 0xfffe, SIC_COMPLEX }
1314 };
1315
1316 /***********************************************************************
1317  *      ScriptIsComplex (USP10.@)
1318  * 
1319  *  Determine if a string is complex.
1320  *
1321  *  PARAMS
1322  *   chars [I] Array of characters to test.
1323  *   len   [I] Length in characters.
1324  *   flag  [I] Flag.
1325  *
1326  *  RETURNS
1327  *   Success: S_OK
1328  *   Failure: S_FALSE
1329  *
1330  *  NOTES
1331  *   Behaviour matches that of WinXP.
1332  */
1333 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1334 {
1335     int i;
1336     unsigned int j;
1337
1338     TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1339
1340     for (i = 0; i < len; i++)
1341     {
1342         for (j = 0; j < sizeof(complex_ranges)/sizeof(complex_ranges[0]); j++)
1343         {
1344             if (chars[i] >= complex_ranges[j].start &&
1345                 chars[i] <= complex_ranges[j].end &&
1346                 (flag & complex_ranges[j].flag)) return S_OK;
1347         }
1348     }
1349     return S_FALSE;
1350 }
1351
1352 /***********************************************************************
1353  *      ScriptShape (USP10.@)
1354  *
1355  * Produce glyphs and visual attributes for a run.
1356  *
1357  * PARAMS
1358  *  hdc         [I]   Device context.
1359  *  psc         [I/O] Opaque pointer to a script cache.
1360  *  pwcChars    [I]   Array of characters specifying the run.
1361  *  cChars      [I]   Number of characters in pwcChars.
1362  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1363  *  psa         [I/O] Script analysis.
1364  *  pwOutGlyphs [O]   Array of glyphs.
1365  *  pwLogClust  [O]   Array of logical cluster info.
1366  *  psva        [O]   Array of visual attributes.
1367  *  pcGlyphs    [O]   Number of glyphs returned.
1368  *
1369  * RETURNS
1370  *  Success: S_OK
1371  *  Failure: Non-zero HRESULT value.
1372  */
1373 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars, 
1374                            int cChars, int cMaxGlyphs,
1375                            SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1376                            SCRIPT_VISATTR *psva, int *pcGlyphs)
1377 {
1378     HRESULT hr;
1379     unsigned int i;
1380     BOOL rtl;
1381
1382     TRACE("(%p, %p, %s, %d, %d, %p, %p, %p, %p, %p)\n", hdc, psc, debugstr_wn(pwcChars, cChars),
1383           cChars, cMaxGlyphs, psa, pwOutGlyphs, pwLogClust, psva, pcGlyphs);
1384
1385     if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1386                    psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1387
1388     if (!psva || !pcGlyphs) return E_INVALIDARG;
1389     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1390     rtl = (!psa->fLogicalOrder && psa->fRTL);
1391
1392     *pcGlyphs = cChars;
1393     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1394     if (!pwLogClust) return E_FAIL;
1395
1396     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1397     {
1398         for (i = 0; i < cChars; i++)
1399         {
1400             int idx = i;
1401             WCHAR chInput;
1402             if (rtl) idx = cChars - 1 - i;
1403             if (psa->fRTL)
1404                 chInput = mirror_char(pwcChars[idx]);
1405             else
1406                 chInput = pwcChars[idx];
1407             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1408             {
1409                 WORD glyph;
1410                 if (!hdc) return E_PENDING;
1411                 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1412                 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1413             }
1414         }
1415     }
1416     else
1417     {
1418         TRACE("no glyph translation\n");
1419         for (i = 0; i < cChars; i++)
1420         {
1421             int idx = i;
1422             /* No mirroring done here */
1423             if (rtl) idx = cChars - 1 - i;
1424             pwOutGlyphs[i] = pwcChars[idx];
1425         }
1426     }
1427
1428     /* set up a valid SCRIPT_VISATTR and LogClust for each char in this run */
1429     for (i = 0; i < cChars; i++)
1430     {
1431         int idx = i;
1432         if (rtl) idx = cChars - 1 - i;
1433         /* FIXME: set to better values */
1434         psva[i].uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1435         psva[i].fClusterStart  = 1;
1436         psva[i].fDiacritic     = 0;
1437         psva[i].fZeroWidth     = 0;
1438         psva[i].fReserved      = 0;
1439         psva[i].fShapeReserved = 0;
1440
1441         pwLogClust[i] = idx;
1442     }
1443     return S_OK;
1444 }
1445
1446 /***********************************************************************
1447  *      ScriptPlace (USP10.@)
1448  *
1449  * Produce advance widths for a run.
1450  *
1451  * PARAMS
1452  *  hdc       [I]   Device context.
1453  *  psc       [I/O] Opaque pointer to a script cache.
1454  *  pwGlyphs  [I]   Array of glyphs.
1455  *  cGlyphs   [I]   Number of glyphs in pwGlyphs.
1456  *  psva      [I]   Array of visual attributes.
1457  *  psa       [I/O] String analysis.
1458  *  piAdvance [O]   Array of advance widths.
1459  *  pGoffset  [O]   Glyph offsets.
1460  *  pABC      [O]   Combined ABC width.
1461  *
1462  * RETURNS
1463  *  Success: S_OK
1464  *  Failure: Non-zero HRESULT value.
1465  */
1466 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs, 
1467                            int cGlyphs, const SCRIPT_VISATTR *psva,
1468                            SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1469 {
1470     HRESULT hr;
1471     int i;
1472
1473     TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n",  hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1474           piAdvance, pGoffset, pABC);
1475
1476     if (!psva) return E_INVALIDARG;
1477     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1478     if (!pGoffset) return E_FAIL;
1479
1480     if (pABC) memset(pABC, 0, sizeof(ABC));
1481     for (i = 0; i < cGlyphs; i++)
1482     {
1483         ABC abc;
1484         if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1485         {
1486             if (!hdc) return E_PENDING;
1487             if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1488             {
1489                 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1490             }
1491             else
1492             {
1493                 INT width;
1494                 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1495                 abc.abcB = width;
1496                 abc.abcA = abc.abcC = 0;
1497             }
1498             set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1499         }
1500         if (pABC)
1501         {
1502             pABC->abcA += abc.abcA;
1503             pABC->abcB += abc.abcB;
1504             pABC->abcC += abc.abcC;
1505         }
1506         /* FIXME: set to more reasonable values */
1507         pGoffset[i].du = pGoffset[i].dv = 0;
1508         if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1509     }
1510
1511     if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1512     return S_OK;
1513 }
1514
1515 /***********************************************************************
1516  *      ScriptGetCMap (USP10.@)
1517  *
1518  * Retrieve glyph indices.
1519  *
1520  * PARAMS
1521  *  hdc         [I]   Device context.
1522  *  psc         [I/O] Opaque pointer to a script cache.
1523  *  pwcInChars  [I]   Array of Unicode characters.
1524  *  cChars      [I]   Number of characters in pwcInChars.
1525  *  dwFlags     [I]   Flags.
1526  *  pwOutGlyphs [O]   Buffer to receive the array of glyph indices.
1527  *
1528  * RETURNS
1529  *  Success: S_OK
1530  *  Failure: Non-zero HRESULT value.
1531  */
1532 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1533                              int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1534 {
1535     HRESULT hr;
1536     int i;
1537
1538     TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1539           cChars, dwFlags, pwOutGlyphs);
1540
1541     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1542
1543     hr = S_OK;
1544
1545     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1546     {
1547         for (i = 0; i < cChars; i++)
1548         {
1549             WCHAR inChar;
1550             if (dwFlags == SGCM_RTL)
1551                 inChar = mirror_char(pwcInChars[i]);
1552             else
1553                 inChar = pwcInChars[i];
1554             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1555             {
1556                 WORD glyph;
1557                 if (!hdc) return E_PENDING;
1558                 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1559                 if (glyph == 0xffff)
1560                 {
1561                     hr = S_FALSE;
1562                     glyph = 0x0;
1563                 }
1564                 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1565             }
1566         }
1567     }
1568     else
1569     {
1570         TRACE("no glyph translation\n");
1571         for (i = 0; i < cChars; i++)
1572         {
1573             WCHAR inChar;
1574             if (dwFlags == SGCM_RTL)
1575                 inChar = mirror_char(pwcInChars[i]);
1576             else
1577                 inChar = pwcInChars[i];
1578             pwOutGlyphs[i] = inChar;
1579         }
1580     }
1581     return hr;
1582 }
1583
1584 /***********************************************************************
1585  *      ScriptTextOut (USP10.@)
1586  *
1587  */
1588 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions, 
1589                              const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved, 
1590                              int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1591                              const int *piJustify, const GOFFSET *pGoffset)
1592 {
1593     HRESULT hr = S_OK;
1594
1595     TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1596          hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1597          piAdvance, piJustify, pGoffset);
1598
1599     if (!hdc || !psc) return E_INVALIDARG;
1600     if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1601
1602     fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1603     fuOptions |= ETO_IGNORELANGUAGE;
1604     if  (!psa->fNoGlyphIndex)                                     /* Have Glyphs?                      */
1605         fuOptions |= ETO_GLYPH_INDEX;                             /* Say don't do translation to glyph */
1606
1607     if (psa->fRTL && psa->fLogicalOrder)
1608     {
1609         int i;
1610         WORD *rtlGlyphs;
1611
1612         rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1613         if (!rtlGlyphs)
1614             return E_OUTOFMEMORY;
1615
1616         for (i = 0; i < cGlyphs; i++)
1617             rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
1618
1619         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
1620             hr = S_FALSE;
1621         heap_free(rtlGlyphs);
1622     }
1623     else
1624         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
1625             hr = S_FALSE;
1626
1627     return hr;
1628 }
1629
1630 /***********************************************************************
1631  *      ScriptCacheGetHeight (USP10.@)
1632  *
1633  * Retrieve the height of the font in the cache.
1634  *
1635  * PARAMS
1636  *  hdc    [I]    Device context.
1637  *  psc    [I/O]  Opaque pointer to a script cache.
1638  *  height [O]    Receives font height.
1639  *
1640  * RETURNS
1641  *  Success: S_OK
1642  *  Failure: Non-zero HRESULT value.
1643  */
1644 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
1645 {
1646     HRESULT hr;
1647
1648     TRACE("(%p, %p, %p)\n", hdc, psc, height);
1649
1650     if (!height) return E_INVALIDARG;
1651     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1652
1653     *height = get_cache_height(psc);
1654     return S_OK;
1655 }
1656
1657 /***********************************************************************
1658  *      ScriptGetGlyphABCWidth (USP10.@)
1659  *
1660  * Retrieve the width of a glyph.
1661  *
1662  * PARAMS
1663  *  hdc    [I]    Device context.
1664  *  psc    [I/O]  Opaque pointer to a script cache.
1665  *  glyph  [I]    Glyph to retrieve the width for.
1666  *  abc    [O]    ABC widths of the glyph.
1667  *
1668  * RETURNS
1669  *  Success: S_OK
1670  *  Failure: Non-zero HRESULT value.
1671  */
1672 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
1673 {
1674     HRESULT hr;
1675
1676     TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
1677
1678     if (!abc) return E_INVALIDARG;
1679     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1680
1681     if (!get_cache_glyph_widths(psc, glyph, abc))
1682     {
1683         if (!hdc) return E_PENDING;
1684         if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1685         {
1686             if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
1687         }
1688         else
1689         {
1690             INT width;
1691             if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
1692             abc->abcB = width;
1693             abc->abcA = abc->abcC = 0;
1694         }
1695         set_cache_glyph_widths(psc, glyph, abc);
1696     }
1697     return S_OK;
1698 }
1699
1700 /***********************************************************************
1701  *      ScriptLayout (USP10.@)
1702  *
1703  * Map embedding levels to visual and/or logical order.
1704  *
1705  * PARAMS
1706  *  runs     [I] Size of level array.
1707  *  level    [I] Array of embedding levels.
1708  *  vistolog [O] Map of embedding levels from visual to logical order.
1709  *  logtovis [O] Map of embedding levels from logical to visual order.
1710  *
1711  * RETURNS
1712  *  Success: S_OK
1713  *  Failure: Non-zero HRESULT value.
1714  *
1715  * BUGS
1716  *  This stub works correctly for any sequence of a single
1717  *  embedding level but not for sequences of different
1718  *  embedding levels, i.e. mixtures of RTL and LTR scripts.
1719  */
1720 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
1721 {
1722     int* indexs;
1723     int ich;
1724
1725     TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
1726
1727     if (!level || (!vistolog && !logtovis))
1728         return E_INVALIDARG;
1729
1730     indexs = heap_alloc(sizeof(int) * runs);
1731     if (!indexs)
1732         return E_OUTOFMEMORY;
1733
1734
1735     if (vistolog)
1736     {
1737         for( ich = 0; ich < runs; ich++)
1738             indexs[ich] = ich;
1739
1740         ich = 0;
1741         while (ich < runs)
1742             ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1743         for (ich = 0; ich < runs; ich++)
1744             vistolog[ich] = indexs[ich];
1745     }
1746
1747
1748     if (logtovis)
1749     {
1750         for( ich = 0; ich < runs; ich++)
1751             indexs[ich] = ich;
1752
1753         ich = 0;
1754         while (ich < runs)
1755             ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1756         for (ich = 0; ich < runs; ich++)
1757             logtovis[ich] = indexs[ich];
1758     }
1759     heap_free(indexs);
1760
1761     return S_OK;
1762 }
1763
1764 /***********************************************************************
1765  *      ScriptStringGetLogicalWidths (USP10.@)
1766  *
1767  * Returns logical widths from a string analysis.
1768  *
1769  * PARAMS
1770  *  ssa  [I] string analysis.
1771  *  piDx [O] logical widths returned.
1772  *
1773  * RETURNS
1774  *  Success: S_OK
1775  *  Failure: a non-zero HRESULT.
1776  */
1777 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
1778 {
1779     int i, j, next = 0;
1780     StringAnalysis *analysis = ssa;
1781
1782     TRACE("%p, %p\n", ssa, piDx);
1783
1784     if (!analysis) return S_FALSE;
1785
1786     for (i = 0; i < analysis->numItems; i++)
1787     {
1788         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1789         {
1790             piDx[next] = analysis->glyphs[i].piAdvance[j];
1791             next++;
1792         }
1793     }
1794     return S_OK;
1795 }
1796
1797 /***********************************************************************
1798  *      ScriptStringValidate (USP10.@)
1799  *
1800  * Validate a string analysis.
1801  *
1802  * PARAMS
1803  *  ssa [I] string analysis.
1804  *
1805  * RETURNS
1806  *  Success: S_OK
1807  *  Failure: S_FALSE if invalid sequences are found
1808  *           or a non-zero HRESULT if it fails.
1809  */
1810 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
1811 {
1812     StringAnalysis *analysis = ssa;
1813
1814     TRACE("(%p)\n", ssa);
1815
1816     if (!analysis) return E_INVALIDARG;
1817     return (analysis->invalid) ? S_FALSE : S_OK;
1818 }
1819
1820 /***********************************************************************
1821  *      ScriptString_pSize (USP10.@)
1822  *
1823  * Retrieve width and height of an analysed string.
1824  *
1825  * PARAMS
1826  *  ssa [I] string analysis.
1827  *
1828  * RETURNS
1829  *  Success: Pointer to a SIZE structure.
1830  *  Failure: NULL
1831  */
1832 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
1833 {
1834     int i, j;
1835     StringAnalysis *analysis = ssa;
1836
1837     TRACE("(%p)\n", ssa);
1838
1839     if (!analysis) return NULL;
1840
1841     if (!analysis->sz)
1842     {
1843         if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
1844         analysis->sz->cy = analysis->sc->tm.tmHeight;
1845
1846         analysis->sz->cx = 0;
1847         for (i = 0; i < analysis->numItems; i++)
1848             for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1849                 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
1850     }
1851     return analysis->sz;
1852 }
1853
1854 /***********************************************************************
1855  *      ScriptString_pLogAttr (USP10.@)
1856  *
1857  * Retrieve logical attributes of an analysed string.
1858  *
1859  * PARAMS
1860  *  ssa [I] string analysis.
1861  *
1862  * RETURNS
1863  *  Success: Pointer to an array of SCRIPT_LOGATTR structures.
1864  *  Failure: NULL
1865  */
1866 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
1867 {
1868     StringAnalysis *analysis = ssa;
1869
1870     TRACE("(%p)\n", ssa);
1871
1872     if (!analysis) return NULL;
1873     return analysis->logattrs;
1874 }
1875
1876 /***********************************************************************
1877  *      ScriptString_pcOutChars (USP10.@)
1878  *
1879  * Retrieve the length of a string after clipping.
1880  *
1881  * PARAMS
1882  *  ssa [I] String analysis.
1883  *
1884  * RETURNS
1885  *  Success: Pointer to the length.
1886  *  Failure: NULL
1887  */
1888 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
1889 {
1890     StringAnalysis *analysis = ssa;
1891
1892     TRACE("(%p)\n", ssa);
1893
1894     if (!analysis) return NULL;
1895     return &analysis->clip_len;
1896 }
1897
1898 /***********************************************************************
1899  *      ScriptStringGetOrder (USP10.@)
1900  *
1901  * Retrieve a glyph order map.
1902  *
1903  * PARAMS
1904  *  ssa   [I]   String analysis.
1905  *  order [I/O] Array of glyph positions.
1906  *
1907  * RETURNS
1908  *  Success: S_OK
1909  *  Failure: a non-zero HRESULT.
1910  */
1911 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
1912 {
1913     int i, j;
1914     unsigned int k;
1915     StringAnalysis *analysis = ssa;
1916
1917     TRACE("(%p)\n", ssa);
1918
1919     if (!analysis) return S_FALSE;
1920
1921     /* FIXME: handle RTL scripts */
1922     for (i = 0, k = 0; i < analysis->numItems; i++)
1923         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
1924             order[k] = k;
1925
1926     return S_OK;
1927 }
1928
1929 /***********************************************************************
1930  *      ScriptGetLogicalWidths (USP10.@)
1931  *
1932  * Convert advance widths to logical widths.
1933  *
1934  * PARAMS
1935  *  sa          [I] Script analysis.
1936  *  nbchars     [I] Number of characters.
1937  *  nbglyphs    [I] Number of glyphs.
1938  *  glyph_width [I] Array of glyph widths.
1939  *  log_clust   [I] Array of logical clusters.
1940  *  sva         [I] Visual attributes.
1941  *  widths      [O] Array of logical widths.
1942  *
1943  * RETURNS
1944  *  Success: S_OK
1945  *  Failure: a non-zero HRESULT.
1946  */
1947 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
1948                                       const int *glyph_width, const WORD *log_clust,
1949                                       const SCRIPT_VISATTR *sva, int *widths)
1950 {
1951     int i;
1952
1953     TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
1954           sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
1955
1956     /* FIXME */
1957     for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
1958     return S_OK;
1959 }
1960
1961 /***********************************************************************
1962  *      ScriptApplyLogicalWidth (USP10.@)
1963  *
1964  * Generate glyph advance widths.
1965  *
1966  * PARAMS
1967  *  dx          [I]   Array of logical advance widths.
1968  *  num_chars   [I]   Number of characters.
1969  *  num_glyphs  [I]   Number of glyphs.
1970  *  log_clust   [I]   Array of logical clusters.
1971  *  sva         [I]   Visual attributes.
1972  *  advance     [I]   Array of glyph advance widths.
1973  *  sa          [I]   Script analysis.
1974  *  abc         [I/O] Summed ABC widths.
1975  *  justify     [O]   Array of glyph advance widths.
1976  *
1977  * RETURNS
1978  *  Success: S_OK
1979  *  Failure: a non-zero HRESULT.
1980  */
1981 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
1982                                        const WORD *log_clust, const SCRIPT_VISATTR *sva,
1983                                        const int *advance, const SCRIPT_ANALYSIS *sa,
1984                                        ABC *abc, int *justify)
1985 {
1986     int i;
1987
1988     FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
1989           dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
1990
1991     for (i = 0; i < num_chars; i++) justify[i] = advance[i];
1992     return S_OK;
1993 }
1994
1995 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
1996                              int num_glyphs, int dx, int min_kashida, int *justify)
1997 {
1998     int i;
1999
2000     FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
2001
2002     for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
2003     return S_OK;
2004 }