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