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