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