mscoree/tests: Added some simple tests for GetCORVersion.
[wine] / dlls / usp10 / usp10.c
1 /*
2  * Implementation of Uniscribe Script Processor (usp10.dll)
3  *
4  * Copyright 2005 Steven Edwards for CodeWeavers
5  * Copyright 2006 Hans Leidekker
6  * Copyright 2010 CodeWeavers, Aric Stewart
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * Notes:
23  * Uniscribe allows for processing of complex scripts such as joining
24  * and filtering characters and bi-directional text with custom line breaks.
25  */
26
27 #include <stdarg.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "usp10.h"
35
36 #include "usp10_internal.h"
37
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
42
43 typedef struct _scriptRange
44 {
45     WORD script;
46     WORD rangeFirst;
47     WORD rangeLast;
48     WORD numericScript;
49     WORD punctScript;
50 } scriptRange;
51
52 static const scriptRange scriptRanges[] = {
53     /* Basic Latin: U+0000–U+007A */
54     /* Latin-1 Supplement: U+0080–U+00FF */
55     /* Latin Extended-A: U+0100–U+017F */
56     /* Latin Extended-B: U+0180–U+024F */
57     /* IPA Extensions: U+0250–U+02AF */
58     { Script_Latin,      0x00,   0x2af ,  Script_Numeric, Script_Punctuation},
59     /* Greek: U+0370–U+03FF */
60     { Script_Greek,      0x370,  0x3ff,  0, 0},
61     /* Cyrillic: U+0400–U+04FF */
62     /* Cyrillic Supplement: U+0500–U+052F */
63     { Script_Cyrillic,   0x400,  0x52f,  0, 0},
64     /* Armenian: U+0530–U+058F */
65     { Script_Armenian,   0x530,  0x58f,  0, 0},
66     /* Hebrew: U+0590–U+05FF */
67     { Script_Hebrew,     0x590,  0x5ff,  0, 0},
68     /* Arabic: U+0600–U+06FF */
69     { Script_Arabic,     0x600,  0x6ef,  Script_Arabic_Numeric, 0},
70     /* Defined by Windows */
71     { Script_Persian,    0x6f0,  0x6f9,  0, 0},
72     /* Continue Arabic: U+0600–U+06FF */
73     { Script_Arabic,     0x6fa,  0x6ff,  0, 0},
74     /* Syriac: U+0700–U+074F*/
75     { Script_Syriac,     0x700,  0x74f,  0, 0},
76     /* Arabic Supplement: U+0750–U+077F */
77     { Script_Arabic,     0x750,  0x77f,  0, 0},
78     /* Thaana: U+0780–U+07BF */
79     { Script_Thaana,     0x780,  0x7bf,  0, 0},
80     /* Georgian: U+10A0–U+10FF */
81     { Script_Georgian,   0x10a0,  0x10ff,  0, 0},
82     /* Phonetic Extensions: U+1D00–U+1DBF */
83     { Script_Latin,      0x1d00, 0x1dbf, 0, 0},
84     /* Latin Extended Additional: U+1E00–U+1EFF */
85     { Script_Latin,      0x1e00, 0x1eff, 0, 0},
86     /* Greek Extended: U+1F00–U+1FFF */
87     { Script_Greek,      0x1f00, 0x1fff, 0, 0},
88     /* Latin Extended-C: U+2C60–U+2C7F */
89     { Script_Latin,      0x2c60, 0x2c7f, 0, 0},
90     /* Georgian: U+2D00–U+2D2F */
91     { Script_Georgian,   0x2d00,  0x2d2f,  0, 0},
92     /* Cyrillic Extended-A: U+2DE0–U+2DFF */
93     { Script_Cyrillic,   0x2de0, 0x2dff,  0, 0},
94     /* Cyrillic Extended-B: U+A640–U+A69F */
95     { Script_Cyrillic,   0xa640, 0xa69f,  0, 0},
96     /* Modifier Tone Letters: U+A700–U+A71F */
97     /* Latin Extended-D: U+A720–U+A7FF */
98     { Script_Latin,      0xa700, 0xa7ff, 0, 0},
99     /* Latin Ligatures: U+FB00–U+FB06 */
100     { Script_Latin,      0xfb00, 0xfb06, 0, 0},
101     /* Armenian ligatures U+FB13..U+FB17 */
102     { Script_Armenian,   0xfb13, 0xfb17,  0, 0},
103     /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
104     { Script_Hebrew,     0xfb1d, 0xfb4f, 0, 0},
105     /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
106     { Script_Arabic,     0xfb50, 0xfdff, 0, 0},
107     /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
108     { Script_Arabic,     0xfe70, 0xfeff, 0, 0},
109     /* END */
110     { SCRIPT_UNDEFINED,  0, 0, 0}
111 };
112
113 typedef struct _scriptData
114 {
115     SCRIPT_ANALYSIS a;
116     SCRIPT_PROPERTIES props;
117 } scriptData;
118
119 /* the must be in order so that the index matches the Script value */
120 static const scriptData scriptInformation[] = {
121     {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
122      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
123     {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
124      {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
125     {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
126      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
127     {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
128      {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
129     {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
130      {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0}},
131     {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
132      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
133     {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
134      {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0}},
135     {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
136      {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
137     {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
138      {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
139     {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
140      {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0}},
141     {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
142      {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
143     {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
144      {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
145     {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
146      {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
147     {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
148      {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
149     {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
150      {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
151     {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
152      {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
153 };
154
155 static const SCRIPT_PROPERTIES *script_props[] =
156 {
157     &scriptInformation[0].props, &scriptInformation[1].props,
158     &scriptInformation[2].props, &scriptInformation[3].props,
159     &scriptInformation[4].props, &scriptInformation[5].props,
160     &scriptInformation[6].props, &scriptInformation[7].props,
161     &scriptInformation[8].props, &scriptInformation[9].props,
162     &scriptInformation[10].props, &scriptInformation[11].props,
163     &scriptInformation[12].props, &scriptInformation[13].props,
164     &scriptInformation[14].props, &scriptInformation[15].props
165 };
166
167 typedef struct {
168     int numGlyphs;
169     WORD* glyphs;
170     WORD* pwLogClust;
171     int* piAdvance;
172     SCRIPT_VISATTR* psva;
173     GOFFSET* pGoffset;
174     ABC* abc;
175 } StringGlyphs;
176
177 typedef struct {
178     HDC hdc;
179     BOOL invalid;
180     int clip_len;
181     ScriptCache *sc;
182     int cItems;
183     int cMaxGlyphs;
184     SCRIPT_ITEM* pItem;
185     int numItems;
186     StringGlyphs* glyphs;
187     SCRIPT_LOGATTR* logattrs;
188     SIZE* sz;
189 } StringAnalysis;
190
191 static inline void *heap_alloc(SIZE_T size)
192 {
193     return HeapAlloc(GetProcessHeap(), 0, size);
194 }
195
196 static inline void *heap_alloc_zero(SIZE_T size)
197 {
198     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
199 }
200
201 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
202 {
203     return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
204 }
205
206 static inline BOOL heap_free(LPVOID mem)
207 {
208     return HeapFree(GetProcessHeap(), 0, mem);
209 }
210
211 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
212 {
213     return ((ScriptCache *)*psc)->tm.tmDefaultChar;
214 }
215
216 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
217 {
218     return ((ScriptCache *)*psc)->tm.tmHeight;
219 }
220
221 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
222 {
223     return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
224 }
225
226 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
227 {
228     WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
229
230     if (!block) return 0;
231     return block[c & GLYPH_BLOCK_MASK];
232 }
233
234 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
235 {
236     WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
237
238     if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
239     return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
240 }
241
242 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
243 {
244     static const ABC nil;
245     ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
246
247     if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
248     memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
249     return TRUE;
250 }
251
252 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
253 {
254     ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
255
256     if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
257     memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
258     return TRUE;
259 }
260
261 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
262 {
263     ScriptCache *sc;
264
265     if (!psc) return E_INVALIDARG;
266     if (*psc) return S_OK;
267     if (!hdc) return E_PENDING;
268
269     if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
270     if (!GetTextMetricsW(hdc, &sc->tm))
271     {
272         heap_free(sc);
273         return E_INVALIDARG;
274     }
275     if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
276     {
277         heap_free(sc);
278         return E_INVALIDARG;
279     }
280     *psc = sc;
281     TRACE("<- %p\n", sc);
282     return S_OK;
283 }
284
285 static WCHAR mirror_char( WCHAR ch )
286 {
287     extern const WCHAR wine_mirror_map[];
288     return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
289 }
290
291 static WORD get_char_script( WCHAR ch)
292 {
293     WORD type = 0;
294     int i;
295
296     if (ch == 0xc || ch == 0x20 || ch == 0x202f)
297         return Script_CR;
298
299     GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
300
301     if (type == 0)
302         return SCRIPT_UNDEFINED;
303
304     if (type & C1_CNTRL)
305         return Script_Control;
306
307     i = 0;
308     do
309     {
310         if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
311             break;
312
313         if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
314         {
315             if (scriptRanges[i].numericScript && type & C1_DIGIT)
316                 return scriptRanges[i].numericScript;
317             if (scriptRanges[i].punctScript && type & C1_PUNCT)
318                 return scriptRanges[i].punctScript;
319             return scriptRanges[i].script;
320         }
321         i++;
322     } while (1);
323
324     return SCRIPT_UNDEFINED;
325 }
326
327 /***********************************************************************
328  *      DllMain
329  *
330  */
331 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
332 {
333     switch(fdwReason)
334     {
335     case DLL_PROCESS_ATTACH:
336         DisableThreadLibraryCalls(hInstDLL);
337         break;
338     case DLL_PROCESS_DETACH:
339         break;
340     }
341     return TRUE;
342 }
343
344 /***********************************************************************
345  *      ScriptFreeCache (USP10.@)
346  *
347  * Free a script cache.
348  *
349  * PARAMS
350  *   psc [I/O] Script cache.
351  *
352  * RETURNS
353  *  Success: S_OK
354  *  Failure: Non-zero HRESULT value.
355  */
356 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
357 {
358     TRACE("%p\n", psc);
359
360     if (psc && *psc)
361     {
362         unsigned int i;
363         for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
364         {
365             heap_free(((ScriptCache *)*psc)->glyphs[i]);
366             heap_free(((ScriptCache *)*psc)->widths[i]);
367         }
368         heap_free(((ScriptCache *)*psc)->GSUB_Table);
369         heap_free(((ScriptCache *)*psc)->features);
370         heap_free(*psc);
371         *psc = NULL;
372     }
373     return S_OK;
374 }
375
376 /***********************************************************************
377  *      ScriptGetProperties (USP10.@)
378  *
379  * Retrieve a list of script properties.
380  *
381  * PARAMS
382  *  props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
383  *  num   [I] Pointer to the number of scripts.
384  *
385  * RETURNS
386  *  Success: S_OK
387  *  Failure: Non-zero HRESULT value.
388  *
389  * NOTES
390  *  Behaviour matches WinXP.
391  */
392 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
393 {
394     TRACE("(%p,%p)\n", props, num);
395
396     if (!props && !num) return E_INVALIDARG;
397
398     if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
399     if (props) *props = script_props;
400
401     return S_OK;
402 }
403
404 /***********************************************************************
405  *      ScriptGetFontProperties (USP10.@)
406  *
407  * Get information on special glyphs.
408  *
409  * PARAMS
410  *  hdc [I]   Device context.
411  *  psc [I/O] Opaque pointer to a script cache.
412  *  sfp [O]   Font properties structure.
413  */
414 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
415 {
416     HRESULT hr;
417
418     TRACE("%p,%p,%p\n", hdc, psc, sfp);
419
420     if (!sfp) return E_INVALIDARG;
421     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
422
423     if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
424         return E_INVALIDARG;
425
426     /* return something sensible? */
427     sfp->wgBlank = 0;
428     sfp->wgDefault = get_cache_default_char(psc);
429     sfp->wgInvalid = 0;
430     sfp->wgKashida = 0xffff;
431     sfp->iKashidaWidth = 0;
432
433     return S_OK;
434 }
435
436 /***********************************************************************
437  *      ScriptRecordDigitSubstitution (USP10.@)
438  *
439  *  Record digit substitution settings for a given locale.
440  *
441  *  PARAMS
442  *   locale [I] Locale identifier.
443  *   sds    [I] Structure to record substitution settings.
444  *
445  *  RETURNS
446  *   Success: S_OK
447  *   Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
448  *
449  *  SEE ALSO
450  *   http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
451  */
452 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
453 {
454     DWORD plgid, sub;
455
456     TRACE("0x%x, %p\n", locale, sds);
457
458     /* This implementation appears to be correct for all languages, but it's
459      * not clear if sds->DigitSubstitute is ever set to anything except 
460      * CONTEXT or NONE in reality */
461
462     if (!sds) return E_POINTER;
463
464     locale = ConvertDefaultLocale(locale);
465
466     if (!IsValidLocale(locale, LCID_INSTALLED))
467         return E_INVALIDARG;
468
469     plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
470     sds->TraditionalDigitLanguage = plgid;
471
472     if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
473         sds->NationalDigitLanguage = plgid;
474     else
475         sds->NationalDigitLanguage = LANG_ENGLISH;
476
477     if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
478                         (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
479
480     switch (sub)
481     {
482     case 0: 
483         if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
484             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
485         else
486             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
487         break;
488     case 1:
489         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
490         break;
491     case 2:
492         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
493         break;
494     default:
495         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
496         break;
497     }
498
499     sds->dwReserved = 0;
500     return S_OK;
501 }
502
503 /***********************************************************************
504  *      ScriptApplyDigitSubstitution (USP10.@)
505  *
506  *  Apply digit substitution settings.
507  *
508  *  PARAMS
509  *   sds [I] Structure with recorded substitution settings.
510  *   sc  [I] Script control structure.
511  *   ss  [I] Script state structure.
512  *
513  *  RETURNS
514  *   Success: S_OK
515  *   Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
516  */
517 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds, 
518                                             SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
519 {
520     SCRIPT_DIGITSUBSTITUTE psds;
521
522     TRACE("%p, %p, %p\n", sds, sc, ss);
523
524     if (!sc || !ss) return E_POINTER;
525     if (!sds)
526     {
527         sds = &psds;
528         if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
529             return E_INVALIDARG;
530     }
531
532     sc->uDefaultLanguage = LANG_ENGLISH;
533     sc->fContextDigits = 0;
534     ss->fDigitSubstitute = 0;
535
536     switch (sds->DigitSubstitute) {
537         case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
538         case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
539         case SCRIPT_DIGITSUBSTITUTE_NONE:
540         case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
541             return S_OK;
542         default:
543             return E_INVALIDARG;
544     }
545 }
546
547 /***********************************************************************
548  *      ScriptItemize (USP10.@)
549  *
550  * Split a Unicode string into shapeable parts.
551  *
552  * PARAMS
553  *  pwcInChars [I] String to split.
554  *  cInChars   [I] Number of characters in pwcInChars.
555  *  cMaxItems  [I] Maximum number of items to return.
556  *  psControl  [I] Pointer to a SCRIPT_CONTROL structure.
557  *  psState    [I] Pointer to a SCRIPT_STATE structure.
558  *  pItems     [O] Buffer to receive SCRIPT_ITEM structures.
559  *  pcItems    [O] Number of script items returned.
560  *
561  * RETURNS
562  *  Success: S_OK
563  *  Failure: Non-zero HRESULT value.
564  */
565 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
566                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
567                              SCRIPT_ITEM *pItems, int *pcItems)
568 {
569
570 #define Numeric_space 0x0020
571
572     int   cnt = 0, index = 0;
573     int   New_Script = SCRIPT_UNDEFINED;
574     WORD  *levels = NULL;
575     WORD  baselevel = 0;
576
577     TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
578           psControl, psState, pItems, pcItems);
579
580     if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
581         return E_INVALIDARG;
582
583     if (psState && psControl)
584     {
585         int i;
586         levels = heap_alloc_zero(cInChars * sizeof(WORD));
587         if (!levels)
588             return E_OUTOFMEMORY;
589
590         BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
591         baselevel = levels[0];
592         for (i = 0; i < cInChars; i++)
593             if (levels[i]!=levels[0])
594                 break;
595         if (i >= cInChars)
596         {
597             heap_free(levels);
598             levels = NULL;
599         }
600     }
601
602     pItems[index].iCharPos = 0;
603     pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
604
605     if (levels)
606     {
607         pItems[index].a.fRTL = odd(levels[cnt]);
608         pItems[index].a.fLayoutRTL = odd(levels[cnt]);
609         pItems[index].a.s.uBidiLevel = levels[cnt];
610     }
611     else if (!pItems[index].a.s.uBidiLevel)
612     {
613         pItems[index].a.s.uBidiLevel = baselevel;
614         pItems[index].a.fLayoutRTL = odd(baselevel);
615         pItems[index].a.fRTL = odd(baselevel);
616     }
617
618     TRACE("New_Level=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
619           levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript, index, cnt,
620           pItems[index].iCharPos);
621
622     for (cnt=1; cnt < cInChars; cnt++)
623     {
624         if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel))
625             continue;
626
627         if(pwcInChars[cnt] != Numeric_space)
628             New_Script = get_char_script(pwcInChars[cnt]);
629
630         if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel)) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
631         {
632             TRACE("New_Level = %i, New_Script=%d, eScript=%d ", levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript);
633             index++;
634             if  (index+1 > cMaxItems)
635                 return E_OUTOFMEMORY;
636
637             pItems[index].iCharPos = cnt;
638             memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
639
640             pItems[index].a = scriptInformation[New_Script].a;
641             if (levels)
642             {
643                 pItems[index].a.fRTL = odd(levels[cnt]);
644                 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
645                 pItems[index].a.s.uBidiLevel = levels[cnt];
646             }
647             else if (!pItems[index].a.s.uBidiLevel)
648             {
649                 pItems[index].a.s.uBidiLevel = baselevel;
650                 pItems[index].a.fLayoutRTL = odd(baselevel);
651                 pItems[index].a.fRTL = odd(baselevel);
652             }
653
654             TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
655         }
656     }
657
658     /* While not strictly necessary according to the spec, make sure the n+1
659      * item is set up to prevent random behaviour if the caller erroneously
660      * checks the n+1 structure                                              */
661     index++;
662     memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
663
664     TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
665
666     /*  Set one SCRIPT_STATE item being returned  */
667     if  (index + 1 > cMaxItems) return E_OUTOFMEMORY;
668     if (pcItems) *pcItems = index;
669
670     /*  Set SCRIPT_ITEM                                     */
671     pItems[index].iCharPos = cnt;         /* the last item contains the ptr to the lastchar */
672     heap_free(levels);
673     return S_OK;
674 }
675
676 /***********************************************************************
677  *      ScriptStringAnalyse (USP10.@)
678  *
679  */
680 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
681                                    int cGlyphs, int iCharset, DWORD dwFlags,
682                                    int iReqWidth, SCRIPT_CONTROL *psControl,
683                                    SCRIPT_STATE *psState, const int *piDx,
684                                    SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
685                                    SCRIPT_STRING_ANALYSIS *pssa)
686 {
687     HRESULT hr = E_OUTOFMEMORY;
688     StringAnalysis *analysis = NULL;
689     int i, num_items = 255;
690
691     TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
692           hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
693           psControl, psState, piDx, pTabdef, pbInClass, pssa);
694
695     if (iCharset != -1)
696     {
697         FIXME("Only Unicode strings are supported\n");
698         return E_INVALIDARG;
699     }
700     if (cString < 1 || !pString) return E_INVALIDARG;
701     if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
702
703     if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
704     if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
705
706     /* FIXME: handle clipping */
707     analysis->clip_len = cString;
708     analysis->hdc = hdc;
709
710     hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
711                        &analysis->numItems);
712
713     while (hr == E_OUTOFMEMORY)
714     {
715         SCRIPT_ITEM *tmp;
716
717         num_items *= 2;
718         if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
719             goto error;
720
721         analysis->pItem = tmp;
722         hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
723                            &analysis->numItems);
724     }
725     if (hr != S_OK) goto error;
726
727     if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
728         ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
729     else
730         goto error;
731
732     if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
733         goto error;
734
735     for (i = 0; i < analysis->numItems; i++)
736     {
737         SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
738         int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
739         int numGlyphs = 1.5 * cChar + 16;
740         WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
741         WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
742         int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
743         SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
744         GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
745         ABC *abc = heap_alloc_zero(sizeof(ABC));
746         int numGlyphsReturned;
747
748         /* FIXME: non unicode strings */
749         const WCHAR* pStr = (const WCHAR*)pString;
750         hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
751                          cChar, numGlyphs, &analysis->pItem[i].a,
752                          glyphs, pwLogClust, psva, &numGlyphsReturned);
753         hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
754                          piAdvance, pGoffset, abc);
755
756         analysis->glyphs[i].numGlyphs = numGlyphsReturned;
757         analysis->glyphs[i].glyphs = glyphs;
758         analysis->glyphs[i].pwLogClust = pwLogClust;
759         analysis->glyphs[i].piAdvance = piAdvance;
760         analysis->glyphs[i].psva = psva;
761         analysis->glyphs[i].pGoffset = pGoffset;
762         analysis->glyphs[i].abc = abc;
763     }
764
765     *pssa = analysis;
766     return S_OK;
767
768 error:
769     heap_free(analysis->glyphs);
770     heap_free(analysis->logattrs);
771     heap_free(analysis->pItem);
772     heap_free(analysis->sc);
773     heap_free(analysis);
774     return hr;
775 }
776
777 /***********************************************************************
778  *      ScriptStringOut (USP10.@)
779  *
780  * This function takes the output of ScriptStringAnalyse and joins the segments
781  * of glyphs and passes the resulting string to ScriptTextOut.  ScriptStringOut
782  * only processes glyphs.
783  *
784  * Parameters:
785  *  ssa       [I] buffer to hold the analysed string components
786  *  iX        [I] X axis displacement for output
787  *  iY        [I] Y axis displacement for output
788  *  uOptions  [I] flags controling output processing
789  *  prc       [I] rectangle coordinates
790  *  iMinSel   [I] starting pos for substringing output string
791  *  iMaxSel   [I] ending pos for substringing output string
792  *  fDisabled [I] controls text highlighting
793  *
794  *  RETURNS
795  *   Success: S_OK
796  *   Failure: is the value returned by ScriptTextOut
797  */
798 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
799                                int iX,
800                                int iY, 
801                                UINT uOptions, 
802                                const RECT *prc, 
803                                int iMinSel, 
804                                int iMaxSel,
805                                BOOL fDisabled)
806 {
807     StringAnalysis *analysis;
808     WORD *glyphs;
809     int   item, cnt, x;
810     HRESULT hr;
811
812     TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
813          ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
814
815     if (!(analysis = ssa)) return E_INVALIDARG;
816
817     /*
818      * Get storage for the output buffer for the consolidated strings
819      */
820     cnt = 0;
821     for (item = 0; item < analysis->numItems; item++)
822     {
823         cnt += analysis->glyphs[item].numGlyphs;
824     }
825     if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
826
827     /*
828      * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
829      */
830     uOptions |= ETO_GLYPH_INDEX;
831     analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
832
833     /*
834      * Copy the string items into the output buffer
835      */
836
837     TRACE("numItems %d\n", analysis->numItems);
838
839     cnt = 0;
840     for (item = 0; item < analysis->numItems; item++)
841     {
842         memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
843               sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
844
845         TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
846         for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
847             TRACE("%04x", glyphs[x]);
848         TRACE("\n");
849
850         cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
851     }
852
853     hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
854                        uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
855                        analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
856     TRACE("ScriptTextOut hr=%08x\n", hr);
857
858     /*
859      * Free the output buffer and script cache
860      */
861     heap_free(glyphs);
862     return hr;
863 }
864
865 /***********************************************************************
866  *      ScriptStringCPtoX (USP10.@)
867  *
868  */
869 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
870 {
871     int i, j;
872     int runningX = 0;
873     int runningCp = 0;
874     StringAnalysis* analysis = ssa;
875
876     TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
877
878     if (!ssa || !pX) return S_FALSE;
879
880     /* icp out of range */
881     if(icp < 0)
882     {
883         analysis->invalid = TRUE;
884         return E_INVALIDARG;
885     }
886
887     for(i=0; i<analysis->numItems; i++)
888     {
889         for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
890         {
891             if(runningCp == icp && fTrailing == FALSE)
892             {
893                 *pX = runningX;
894                 return S_OK;
895             }
896             runningX += analysis->glyphs[i].piAdvance[j];
897             if(runningCp == icp && fTrailing == TRUE)
898             {
899                 *pX = runningX;
900                 return S_OK;
901             }
902             runningCp++;
903         }
904     }
905
906     /* icp out of range */
907     analysis->invalid = TRUE;
908     return E_INVALIDARG;
909 }
910
911 /***********************************************************************
912  *      ScriptStringXtoCP (USP10.@)
913  *
914  */
915 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing) 
916 {
917     StringAnalysis* analysis = ssa;
918     int i;
919     int j;
920     int runningX = 0;
921     int runningCp = 0;
922     int width;
923
924     TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
925
926     if (!ssa || !piCh || !piTrailing) return S_FALSE;
927
928     /* out of range */
929     if(iX < 0)
930     {
931         *piCh = -1;
932         *piTrailing = TRUE;
933         return S_OK;
934     }
935
936     for(i=0; i<analysis->numItems; i++)
937     {
938         for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
939         {
940             width = analysis->glyphs[i].piAdvance[j];
941             if(iX < (runningX + width))
942             {
943                 *piCh = runningCp;
944                 if((iX - runningX) > width/2)
945                     *piTrailing = TRUE;
946                 else
947                     *piTrailing = FALSE;
948                 return S_OK;
949             }
950             runningX += width;
951             runningCp++;
952         }
953     }
954
955     /* out of range */
956     *piCh = analysis->pItem[analysis->numItems].iCharPos;
957     *piTrailing = FALSE;
958
959     return S_OK;
960 }
961
962
963 /***********************************************************************
964  *      ScriptStringFree (USP10.@)
965  *
966  * Free a string analysis.
967  *
968  * PARAMS
969  *  pssa [I] string analysis.
970  *
971  * RETURNS
972  *  Success: S_OK
973  *  Failure: Non-zero HRESULT value.
974  */
975 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
976 {
977     StringAnalysis* analysis;
978     BOOL invalid;
979     int i;
980
981     TRACE("(%p)\n", pssa);
982
983     if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
984
985     invalid = analysis->invalid;
986     ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
987
988     for (i = 0; i < analysis->numItems; i++)
989     {
990         heap_free(analysis->glyphs[i].glyphs);
991         heap_free(analysis->glyphs[i].pwLogClust);
992         heap_free(analysis->glyphs[i].piAdvance);
993         heap_free(analysis->glyphs[i].psva);
994         heap_free(analysis->glyphs[i].pGoffset);
995         heap_free(analysis->glyphs[i].abc);
996     }
997
998     heap_free(analysis->glyphs);
999     heap_free(analysis->pItem);
1000     heap_free(analysis->logattrs);
1001     heap_free(analysis->sz);
1002     heap_free(analysis->sc);
1003     heap_free(analysis);
1004
1005     if (invalid) return E_INVALIDARG;
1006     return S_OK;
1007 }
1008
1009 /***********************************************************************
1010  *      ScriptCPtoX (USP10.@)
1011  *
1012  */
1013 HRESULT WINAPI ScriptCPtoX(int iCP,
1014                            BOOL fTrailing,
1015                            int cChars,
1016                            int cGlyphs,
1017                            const WORD *pwLogClust,
1018                            const SCRIPT_VISATTR *psva,
1019                            const int *piAdvance,
1020                            const SCRIPT_ANALYSIS *psa,
1021                            int *piX)
1022 {
1023     int  item;
1024     int  iPosX;
1025     float  fMaxPosX = 0;
1026     TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1027           iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1028           psa, piX);
1029     for (item=0; item < cGlyphs; item++)            /* total piAdvance           */
1030         fMaxPosX += piAdvance[item];
1031     iPosX = (fMaxPosX/cGlyphs)*(iCP+fTrailing);
1032     if  (iPosX > fMaxPosX)
1033         iPosX = fMaxPosX;
1034     *piX = iPosX;                                    /* Return something in range */
1035
1036     TRACE("*piX=%d\n", *piX);
1037     return S_OK;
1038 }
1039
1040 /***********************************************************************
1041  *      ScriptXtoCP (USP10.@)
1042  *
1043  */
1044 HRESULT WINAPI ScriptXtoCP(int iX,
1045                            int cChars,
1046                            int cGlyphs,
1047                            const WORD *pwLogClust,
1048                            const SCRIPT_VISATTR *psva,
1049                            const int *piAdvance,
1050                            const SCRIPT_ANALYSIS *psa,
1051                            int *piCP,
1052                            int *piTrailing)
1053 {
1054     int item;
1055     int iPosX;
1056     float fMaxPosX = 1;
1057     float fAvePosX;
1058     TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1059           iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1060           psa, piCP, piTrailing);
1061     if  (iX < 0)                                    /* iX is before start of run */
1062     {
1063         *piCP = -1;
1064         *piTrailing = TRUE;
1065         return S_OK;
1066     }
1067
1068     for (item=0; item < cGlyphs; item++)            /* total piAdvance           */
1069         fMaxPosX += piAdvance[item];
1070
1071     if  (iX >= fMaxPosX)                            /* iX too large              */
1072     {
1073         *piCP = cChars;
1074         *piTrailing = FALSE;
1075         return S_OK;
1076     }
1077
1078     fAvePosX = fMaxPosX / cGlyphs;
1079     iPosX = fAvePosX;
1080     for (item = 1; item < cGlyphs  && iPosX < iX; item++)
1081         iPosX += fAvePosX;
1082     if  (iPosX - iX > fAvePosX/2)
1083         *piTrailing = 0;
1084     else
1085         *piTrailing = 1;                            /* yep we are over halfway */
1086
1087     *piCP = item -1;                                /* Return character position */
1088     TRACE("*piCP=%d iPposX=%d\n", *piCP, iPosX);
1089     return S_OK;
1090 }
1091
1092 /***********************************************************************
1093  *      ScriptBreak (USP10.@)
1094  *
1095  *  Retrieve line break information.
1096  *
1097  *  PARAMS
1098  *   chars [I] Array of characters.
1099  *   sa    [I] String analysis.
1100  *   la    [I] Array of logical attribute structures.
1101  *
1102  *  RETURNS
1103  *   Success: S_OK
1104  *   Failure: S_FALSE
1105  */
1106 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1107 {
1108     int i;
1109
1110     TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1111
1112     if (!la) return S_FALSE;
1113
1114     for (i = 0; i < count; i++)
1115     {
1116         memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1117
1118         /* FIXME: set the other flags */
1119         la[i].fWhiteSpace = (chars[i] == ' ');
1120         la[i].fCharStop = 1;
1121
1122         if (i > 0 && la[i - 1].fWhiteSpace)
1123         {
1124             la[i].fSoftBreak = 1;
1125             la[i].fWordStop = 1;
1126         }
1127     }
1128     return S_OK;
1129 }
1130
1131 /***********************************************************************
1132  *      ScriptIsComplex (USP10.@)
1133  *
1134  *  Determine if a string is complex.
1135  *
1136  *  PARAMS
1137  *   chars [I] Array of characters to test.
1138  *   len   [I] Length in characters.
1139  *   flag  [I] Flag.
1140  *
1141  *  RETURNS
1142  *   Success: S_OK
1143  *   Failure: S_FALSE
1144  *
1145  */
1146 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1147 {
1148     int i;
1149
1150     TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1151
1152     for (i = 0; i < len; i++)
1153     {
1154         int script;
1155
1156         if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1157             return S_OK;
1158
1159         script = get_char_script(chars[i]);
1160         if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1161             (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1162             return S_OK;
1163     }
1164     return S_FALSE;
1165 }
1166
1167 /***********************************************************************
1168  *      ScriptShape (USP10.@)
1169  *
1170  * Produce glyphs and visual attributes for a run.
1171  *
1172  * PARAMS
1173  *  hdc         [I]   Device context.
1174  *  psc         [I/O] Opaque pointer to a script cache.
1175  *  pwcChars    [I]   Array of characters specifying the run.
1176  *  cChars      [I]   Number of characters in pwcChars.
1177  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1178  *  psa         [I/O] Script analysis.
1179  *  pwOutGlyphs [O]   Array of glyphs.
1180  *  pwLogClust  [O]   Array of logical cluster info.
1181  *  psva        [O]   Array of visual attributes.
1182  *  pcGlyphs    [O]   Number of glyphs returned.
1183  *
1184  * RETURNS
1185  *  Success: S_OK
1186  *  Failure: Non-zero HRESULT value.
1187  */
1188 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars, 
1189                            int cChars, int cMaxGlyphs,
1190                            SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1191                            SCRIPT_VISATTR *psva, int *pcGlyphs)
1192 {
1193     HRESULT hr;
1194     unsigned int i;
1195     BOOL rtl;
1196
1197     TRACE("(%p, %p, %s, %d, %d, %p, %p, %p, %p, %p)\n", hdc, psc, debugstr_wn(pwcChars, cChars),
1198           cChars, cMaxGlyphs, psa, pwOutGlyphs, pwLogClust, psva, pcGlyphs);
1199
1200     if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1201                    psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1202
1203     if (!psva || !pcGlyphs) return E_INVALIDARG;
1204     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1205     rtl = (!psa->fLogicalOrder && psa->fRTL);
1206
1207     *pcGlyphs = cChars;
1208     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1209     if (!pwLogClust) return E_FAIL;
1210
1211     /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
1212     for (i = 0; i < cChars; i++)
1213     {
1214         int idx = i;
1215         if (rtl) idx = cChars - 1 - i;
1216         /* FIXME: set to better values */
1217         psva[i].uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1218         psva[i].fClusterStart  = 1;
1219         psva[i].fDiacritic     = 0;
1220         psva[i].fZeroWidth     = 0;
1221         psva[i].fReserved      = 0;
1222         psva[i].fShapeReserved = 0;
1223
1224         pwLogClust[i] = idx;
1225     }
1226
1227     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1228     {
1229         WCHAR *rChars = heap_alloc(sizeof(WCHAR) * cChars);
1230         if (!rChars) return E_OUTOFMEMORY;
1231         for (i = 0; i < cChars; i++)
1232         {
1233             int idx = i;
1234             WCHAR chInput;
1235             if (rtl) idx = cChars - 1 - i;
1236             if (psa->fRTL)
1237                 chInput = mirror_char(pwcChars[idx]);
1238             else
1239                 chInput = pwcChars[idx];
1240             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1241             {
1242                 WORD glyph;
1243                 if (!hdc) return E_PENDING;
1244                 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1245                 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1246             }
1247             rChars[i] = chInput;
1248         }
1249         SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1250         SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
1251         heap_free(rChars);
1252     }
1253     else
1254     {
1255         TRACE("no glyph translation\n");
1256         for (i = 0; i < cChars; i++)
1257         {
1258             int idx = i;
1259             /* No mirroring done here */
1260             if (rtl) idx = cChars - 1 - i;
1261             pwOutGlyphs[i] = pwcChars[idx];
1262         }
1263     }
1264
1265     return S_OK;
1266 }
1267
1268 /***********************************************************************
1269  *      ScriptPlace (USP10.@)
1270  *
1271  * Produce advance widths for a run.
1272  *
1273  * PARAMS
1274  *  hdc       [I]   Device context.
1275  *  psc       [I/O] Opaque pointer to a script cache.
1276  *  pwGlyphs  [I]   Array of glyphs.
1277  *  cGlyphs   [I]   Number of glyphs in pwGlyphs.
1278  *  psva      [I]   Array of visual attributes.
1279  *  psa       [I/O] String analysis.
1280  *  piAdvance [O]   Array of advance widths.
1281  *  pGoffset  [O]   Glyph offsets.
1282  *  pABC      [O]   Combined ABC width.
1283  *
1284  * RETURNS
1285  *  Success: S_OK
1286  *  Failure: Non-zero HRESULT value.
1287  */
1288 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs, 
1289                            int cGlyphs, const SCRIPT_VISATTR *psva,
1290                            SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1291 {
1292     HRESULT hr;
1293     int i;
1294
1295     TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n",  hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1296           piAdvance, pGoffset, pABC);
1297
1298     if (!psva) return E_INVALIDARG;
1299     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1300     if (!pGoffset) return E_FAIL;
1301
1302     if (pABC) memset(pABC, 0, sizeof(ABC));
1303     for (i = 0; i < cGlyphs; i++)
1304     {
1305         ABC abc;
1306         if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1307         {
1308             if (!hdc) return E_PENDING;
1309             if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1310             {
1311                 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1312             }
1313             else
1314             {
1315                 INT width;
1316                 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1317                 abc.abcB = width;
1318                 abc.abcA = abc.abcC = 0;
1319             }
1320             set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1321         }
1322         if (pABC)
1323         {
1324             pABC->abcA += abc.abcA;
1325             pABC->abcB += abc.abcB;
1326             pABC->abcC += abc.abcC;
1327         }
1328         /* FIXME: set to more reasonable values */
1329         pGoffset[i].du = pGoffset[i].dv = 0;
1330         if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1331     }
1332
1333     if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1334     return S_OK;
1335 }
1336
1337 /***********************************************************************
1338  *      ScriptGetCMap (USP10.@)
1339  *
1340  * Retrieve glyph indices.
1341  *
1342  * PARAMS
1343  *  hdc         [I]   Device context.
1344  *  psc         [I/O] Opaque pointer to a script cache.
1345  *  pwcInChars  [I]   Array of Unicode characters.
1346  *  cChars      [I]   Number of characters in pwcInChars.
1347  *  dwFlags     [I]   Flags.
1348  *  pwOutGlyphs [O]   Buffer to receive the array of glyph indices.
1349  *
1350  * RETURNS
1351  *  Success: S_OK
1352  *  Failure: Non-zero HRESULT value.
1353  */
1354 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1355                              int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1356 {
1357     HRESULT hr;
1358     int i;
1359
1360     TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1361           cChars, dwFlags, pwOutGlyphs);
1362
1363     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1364
1365     hr = S_OK;
1366
1367     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1368     {
1369         for (i = 0; i < cChars; i++)
1370         {
1371             WCHAR inChar;
1372             if (dwFlags == SGCM_RTL)
1373                 inChar = mirror_char(pwcInChars[i]);
1374             else
1375                 inChar = pwcInChars[i];
1376             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1377             {
1378                 WORD glyph;
1379                 if (!hdc) return E_PENDING;
1380                 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1381                 if (glyph == 0xffff)
1382                 {
1383                     hr = S_FALSE;
1384                     glyph = 0x0;
1385                 }
1386                 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1387             }
1388         }
1389     }
1390     else
1391     {
1392         TRACE("no glyph translation\n");
1393         for (i = 0; i < cChars; i++)
1394         {
1395             WCHAR inChar;
1396             if (dwFlags == SGCM_RTL)
1397                 inChar = mirror_char(pwcInChars[i]);
1398             else
1399                 inChar = pwcInChars[i];
1400             pwOutGlyphs[i] = inChar;
1401         }
1402     }
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     HRESULT hr = S_OK;
1416
1417     TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1418          hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1419          piAdvance, piJustify, pGoffset);
1420
1421     if (!hdc || !psc) return E_INVALIDARG;
1422     if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1423
1424     fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1425     fuOptions |= ETO_IGNORELANGUAGE;
1426     if  (!psa->fNoGlyphIndex)                                     /* Have Glyphs?                      */
1427         fuOptions |= ETO_GLYPH_INDEX;                             /* Say don't do translation to glyph */
1428
1429     if (psa->fRTL && psa->fLogicalOrder)
1430     {
1431         int i;
1432         WORD *rtlGlyphs;
1433
1434         rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1435         if (!rtlGlyphs)
1436             return E_OUTOFMEMORY;
1437
1438         for (i = 0; i < cGlyphs; i++)
1439             rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
1440
1441         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
1442             hr = S_FALSE;
1443         heap_free(rtlGlyphs);
1444     }
1445     else
1446         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
1447             hr = S_FALSE;
1448
1449     return hr;
1450 }
1451
1452 /***********************************************************************
1453  *      ScriptCacheGetHeight (USP10.@)
1454  *
1455  * Retrieve the height of the font in the cache.
1456  *
1457  * PARAMS
1458  *  hdc    [I]    Device context.
1459  *  psc    [I/O]  Opaque pointer to a script cache.
1460  *  height [O]    Receives font height.
1461  *
1462  * RETURNS
1463  *  Success: S_OK
1464  *  Failure: Non-zero HRESULT value.
1465  */
1466 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
1467 {
1468     HRESULT hr;
1469
1470     TRACE("(%p, %p, %p)\n", hdc, psc, height);
1471
1472     if (!height) return E_INVALIDARG;
1473     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1474
1475     *height = get_cache_height(psc);
1476     return S_OK;
1477 }
1478
1479 /***********************************************************************
1480  *      ScriptGetGlyphABCWidth (USP10.@)
1481  *
1482  * Retrieve the width of a glyph.
1483  *
1484  * PARAMS
1485  *  hdc    [I]    Device context.
1486  *  psc    [I/O]  Opaque pointer to a script cache.
1487  *  glyph  [I]    Glyph to retrieve the width for.
1488  *  abc    [O]    ABC widths of the glyph.
1489  *
1490  * RETURNS
1491  *  Success: S_OK
1492  *  Failure: Non-zero HRESULT value.
1493  */
1494 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
1495 {
1496     HRESULT hr;
1497
1498     TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
1499
1500     if (!abc) return E_INVALIDARG;
1501     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1502
1503     if (!get_cache_glyph_widths(psc, glyph, abc))
1504     {
1505         if (!hdc) return E_PENDING;
1506         if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1507         {
1508             if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
1509         }
1510         else
1511         {
1512             INT width;
1513             if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
1514             abc->abcB = width;
1515             abc->abcA = abc->abcC = 0;
1516         }
1517         set_cache_glyph_widths(psc, glyph, abc);
1518     }
1519     return S_OK;
1520 }
1521
1522 /***********************************************************************
1523  *      ScriptLayout (USP10.@)
1524  *
1525  * Map embedding levels to visual and/or logical order.
1526  *
1527  * PARAMS
1528  *  runs     [I] Size of level array.
1529  *  level    [I] Array of embedding levels.
1530  *  vistolog [O] Map of embedding levels from visual to logical order.
1531  *  logtovis [O] Map of embedding levels from logical to visual order.
1532  *
1533  * RETURNS
1534  *  Success: S_OK
1535  *  Failure: Non-zero HRESULT value.
1536  *
1537  * BUGS
1538  *  This stub works correctly for any sequence of a single
1539  *  embedding level but not for sequences of different
1540  *  embedding levels, i.e. mixtures of RTL and LTR scripts.
1541  */
1542 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
1543 {
1544     int* indexs;
1545     int ich;
1546
1547     TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
1548
1549     if (!level || (!vistolog && !logtovis))
1550         return E_INVALIDARG;
1551
1552     indexs = heap_alloc(sizeof(int) * runs);
1553     if (!indexs)
1554         return E_OUTOFMEMORY;
1555
1556
1557     if (vistolog)
1558     {
1559         for( ich = 0; ich < runs; ich++)
1560             indexs[ich] = ich;
1561
1562         ich = 0;
1563         while (ich < runs)
1564             ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1565         for (ich = 0; ich < runs; ich++)
1566             vistolog[ich] = indexs[ich];
1567     }
1568
1569
1570     if (logtovis)
1571     {
1572         for( ich = 0; ich < runs; ich++)
1573             indexs[ich] = ich;
1574
1575         ich = 0;
1576         while (ich < runs)
1577             ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1578         for (ich = 0; ich < runs; ich++)
1579             logtovis[ich] = indexs[ich];
1580     }
1581     heap_free(indexs);
1582
1583     return S_OK;
1584 }
1585
1586 /***********************************************************************
1587  *      ScriptStringGetLogicalWidths (USP10.@)
1588  *
1589  * Returns logical widths from a string analysis.
1590  *
1591  * PARAMS
1592  *  ssa  [I] string analysis.
1593  *  piDx [O] logical widths returned.
1594  *
1595  * RETURNS
1596  *  Success: S_OK
1597  *  Failure: a non-zero HRESULT.
1598  */
1599 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
1600 {
1601     int i, j, next = 0;
1602     StringAnalysis *analysis = ssa;
1603
1604     TRACE("%p, %p\n", ssa, piDx);
1605
1606     if (!analysis) return S_FALSE;
1607
1608     for (i = 0; i < analysis->numItems; i++)
1609     {
1610         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1611         {
1612             piDx[next] = analysis->glyphs[i].piAdvance[j];
1613             next++;
1614         }
1615     }
1616     return S_OK;
1617 }
1618
1619 /***********************************************************************
1620  *      ScriptStringValidate (USP10.@)
1621  *
1622  * Validate a string analysis.
1623  *
1624  * PARAMS
1625  *  ssa [I] string analysis.
1626  *
1627  * RETURNS
1628  *  Success: S_OK
1629  *  Failure: S_FALSE if invalid sequences are found
1630  *           or a non-zero HRESULT if it fails.
1631  */
1632 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
1633 {
1634     StringAnalysis *analysis = ssa;
1635
1636     TRACE("(%p)\n", ssa);
1637
1638     if (!analysis) return E_INVALIDARG;
1639     return (analysis->invalid) ? S_FALSE : S_OK;
1640 }
1641
1642 /***********************************************************************
1643  *      ScriptString_pSize (USP10.@)
1644  *
1645  * Retrieve width and height of an analysed string.
1646  *
1647  * PARAMS
1648  *  ssa [I] string analysis.
1649  *
1650  * RETURNS
1651  *  Success: Pointer to a SIZE structure.
1652  *  Failure: NULL
1653  */
1654 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
1655 {
1656     int i, j;
1657     StringAnalysis *analysis = ssa;
1658
1659     TRACE("(%p)\n", ssa);
1660
1661     if (!analysis) return NULL;
1662
1663     if (!analysis->sz)
1664     {
1665         if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
1666         analysis->sz->cy = analysis->sc->tm.tmHeight;
1667
1668         analysis->sz->cx = 0;
1669         for (i = 0; i < analysis->numItems; i++)
1670             for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1671                 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
1672     }
1673     return analysis->sz;
1674 }
1675
1676 /***********************************************************************
1677  *      ScriptString_pLogAttr (USP10.@)
1678  *
1679  * Retrieve logical attributes of an analysed string.
1680  *
1681  * PARAMS
1682  *  ssa [I] string analysis.
1683  *
1684  * RETURNS
1685  *  Success: Pointer to an array of SCRIPT_LOGATTR structures.
1686  *  Failure: NULL
1687  */
1688 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
1689 {
1690     StringAnalysis *analysis = ssa;
1691
1692     TRACE("(%p)\n", ssa);
1693
1694     if (!analysis) return NULL;
1695     return analysis->logattrs;
1696 }
1697
1698 /***********************************************************************
1699  *      ScriptString_pcOutChars (USP10.@)
1700  *
1701  * Retrieve the length of a string after clipping.
1702  *
1703  * PARAMS
1704  *  ssa [I] String analysis.
1705  *
1706  * RETURNS
1707  *  Success: Pointer to the length.
1708  *  Failure: NULL
1709  */
1710 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
1711 {
1712     StringAnalysis *analysis = ssa;
1713
1714     TRACE("(%p)\n", ssa);
1715
1716     if (!analysis) return NULL;
1717     return &analysis->clip_len;
1718 }
1719
1720 /***********************************************************************
1721  *      ScriptStringGetOrder (USP10.@)
1722  *
1723  * Retrieve a glyph order map.
1724  *
1725  * PARAMS
1726  *  ssa   [I]   String analysis.
1727  *  order [I/O] Array of glyph positions.
1728  *
1729  * RETURNS
1730  *  Success: S_OK
1731  *  Failure: a non-zero HRESULT.
1732  */
1733 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
1734 {
1735     int i, j;
1736     unsigned int k;
1737     StringAnalysis *analysis = ssa;
1738
1739     TRACE("(%p)\n", ssa);
1740
1741     if (!analysis) return S_FALSE;
1742
1743     /* FIXME: handle RTL scripts */
1744     for (i = 0, k = 0; i < analysis->numItems; i++)
1745         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
1746             order[k] = k;
1747
1748     return S_OK;
1749 }
1750
1751 /***********************************************************************
1752  *      ScriptGetLogicalWidths (USP10.@)
1753  *
1754  * Convert advance widths to logical widths.
1755  *
1756  * PARAMS
1757  *  sa          [I] Script analysis.
1758  *  nbchars     [I] Number of characters.
1759  *  nbglyphs    [I] Number of glyphs.
1760  *  glyph_width [I] Array of glyph widths.
1761  *  log_clust   [I] Array of logical clusters.
1762  *  sva         [I] Visual attributes.
1763  *  widths      [O] Array of logical widths.
1764  *
1765  * RETURNS
1766  *  Success: S_OK
1767  *  Failure: a non-zero HRESULT.
1768  */
1769 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
1770                                       const int *glyph_width, const WORD *log_clust,
1771                                       const SCRIPT_VISATTR *sva, int *widths)
1772 {
1773     int i;
1774
1775     TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
1776           sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
1777
1778     /* FIXME */
1779     for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
1780     return S_OK;
1781 }
1782
1783 /***********************************************************************
1784  *      ScriptApplyLogicalWidth (USP10.@)
1785  *
1786  * Generate glyph advance widths.
1787  *
1788  * PARAMS
1789  *  dx          [I]   Array of logical advance widths.
1790  *  num_chars   [I]   Number of characters.
1791  *  num_glyphs  [I]   Number of glyphs.
1792  *  log_clust   [I]   Array of logical clusters.
1793  *  sva         [I]   Visual attributes.
1794  *  advance     [I]   Array of glyph advance widths.
1795  *  sa          [I]   Script analysis.
1796  *  abc         [I/O] Summed ABC widths.
1797  *  justify     [O]   Array of glyph advance widths.
1798  *
1799  * RETURNS
1800  *  Success: S_OK
1801  *  Failure: a non-zero HRESULT.
1802  */
1803 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
1804                                        const WORD *log_clust, const SCRIPT_VISATTR *sva,
1805                                        const int *advance, const SCRIPT_ANALYSIS *sa,
1806                                        ABC *abc, int *justify)
1807 {
1808     int i;
1809
1810     FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
1811           dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
1812
1813     for (i = 0; i < num_chars; i++) justify[i] = advance[i];
1814     return S_OK;
1815 }
1816
1817 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
1818                              int num_glyphs, int dx, int min_kashida, int *justify)
1819 {
1820     int i;
1821
1822     FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
1823
1824     for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
1825     return S_OK;
1826 }