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