mmdevapi/tests: Fix wrong buffer unit and memory leaks.
[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     /* Devanagari: U+0900–U+097F */
81     { Script_Devanagari, 0x900,  0x97f,  Script_Devanagari_Numeric, 0},
82     /* Bengali: U+0980–U+09FF */
83     { Script_Bengali,    0x980,  0x9ff,  Script_Bengali_Numeric, 0},
84     /* Gurmukhi: U+0A00–U+0A7F*/
85     { Script_Gurmukhi,   0xa00,  0xa7f,  Script_Gurmukhi_Numeric, 0},
86     /* Gujarati: U+0A80–U+0AFF*/
87     { Script_Gujarati,   0xa80,  0xaff,  Script_Gujarati_Numeric, 0},
88     /* Oriya: U+0B00–U+0B7F */
89     { Script_Oriya,      0xb00,  0xb7f,  Script_Oriya_Numeric, 0},
90     /* Tamil: U+0B80–U+0BFF */
91     { Script_Tamil,      0xb80,  0xbff,  Script_Tamil_Numeric, 0},
92     /* Telugu: U+0C00–U+0C7F */
93     { Script_Telugu,     0xc00,  0xc7f,  Script_Telugu_Numeric, 0},
94     /* Kannada: U+0C80–U+0CFF */
95     { Script_Kannada,    0xc80,  0xcff,  Script_Kannada_Numeric, 0},
96     /* Malayalam: U+0D00–U+0D7F */
97     { Script_Malayalam,  0xd00,  0xd7f,  Script_Malayalam_Numeric, 0},
98     /* Sinhala: U+0D80–U+0DFF */
99     { Script_Sinhala,   0xd80,  0xdff,  0, 0},
100     /* Thai: U+0E00–U+0E7F */
101     { Script_Thai,      0xe00,  0xe7f,  Script_Thai_Numeric, 0},
102     /* Lao: U+0E80–U+0EFF */
103     { Script_Lao,       0xe80,  0xeff,  Script_Lao_Numeric, 0},
104     /* Tibetan: U+0F00–U+0FFF */
105     { Script_Tibetan,   0xf00,  0xfff,  Script_Tibetan_Numeric, 0},
106     /* Georgian: U+10A0–U+10FF */
107     { Script_Georgian,   0x10a0,  0x10ff,  0, 0},
108     /* Vedic Extensions: U+1CD0-U+1CFF */
109     { Script_Devanagari, 0x1cd0, 0x1cff, Script_Devanagari_Numeric, 0},
110     /* Phonetic Extensions: U+1D00–U+1DBF */
111     { Script_Latin,      0x1d00, 0x1dbf, 0, 0},
112     /* Latin Extended Additional: U+1E00–U+1EFF */
113     { Script_Latin,      0x1e00, 0x1eff, 0, 0},
114     /* Greek Extended: U+1F00–U+1FFF */
115     { Script_Greek,      0x1f00, 0x1fff, 0, 0},
116     /* Latin Extended-C: U+2C60–U+2C7F */
117     { Script_Latin,      0x2c60, 0x2c7f, 0, 0},
118     /* Georgian: U+2D00–U+2D2F */
119     { Script_Georgian,   0x2d00,  0x2d2f,  0, 0},
120     /* Cyrillic Extended-A: U+2DE0–U+2DFF */
121     { Script_Cyrillic,   0x2de0, 0x2dff,  0, 0},
122     /* Cyrillic Extended-B: U+A640–U+A69F */
123     { Script_Cyrillic,   0xa640, 0xa69f,  0, 0},
124     /* Modifier Tone Letters: U+A700–U+A71F */
125     /* Latin Extended-D: U+A720–U+A7FF */
126     { Script_Latin,      0xa700, 0xa7ff, 0, 0},
127     /* Phags-pa: U+A840–U+A87F */
128     { Script_Phags_pa,   0xa840, 0xa87f, 0, 0},
129     /* Devanagari Extended: U+A8E0-U+A8FF */
130     { Script_Devanagari, 0xa8e0, 0xa8ff, Script_Devanagari_Numeric, 0},
131     /* Latin Ligatures: U+FB00–U+FB06 */
132     { Script_Latin,      0xfb00, 0xfb06, 0, 0},
133     /* Armenian ligatures U+FB13..U+FB17 */
134     { Script_Armenian,   0xfb13, 0xfb17,  0, 0},
135     /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
136     { Script_Hebrew,     0xfb1d, 0xfb4f, 0, 0},
137     /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
138     { Script_Arabic,     0xfb50, 0xfdff, 0, 0},
139     /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
140     { Script_Arabic,     0xfe70, 0xfeff, 0, 0},
141     /* END */
142     { SCRIPT_UNDEFINED,  0, 0, 0}
143 };
144
145 typedef struct _scriptData
146 {
147     SCRIPT_ANALYSIS a;
148     SCRIPT_PROPERTIES props;
149     OPENTYPE_TAG scriptTag;
150 } scriptData;
151
152 /* the must be in order so that the index matches the Script value */
153 static const scriptData scriptInformation[] = {
154     {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
155      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
156      0x00000000},
157     {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
158      {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
159      MS_MAKE_TAG('l','a','t','n')},
160     {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
161      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
162      0x00000000},
163     {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
164      {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
165      0x00000000},
166     {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
167      {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0},
168      0x00000000},
169     {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
170      {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
171      0x00000000},
172     {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
173      {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0},
174      MS_MAKE_TAG('a','r','a','b')},
175     {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
176      {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
177      MS_MAKE_TAG('a','r','a','b')},
178     {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
179      {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
180      MS_MAKE_TAG('h','e','b','r')},
181     {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
182      {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0},
183      MS_MAKE_TAG('s','y','r','c')},
184     {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
185      {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
186      MS_MAKE_TAG('s','y','r','c')},
187     {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
188      {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
189      MS_MAKE_TAG('t','h','a','a')},
190     {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
191      {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
192      MS_MAKE_TAG('g','r','e','k')},
193     {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
194      {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
195      MS_MAKE_TAG('c','y','r','l')},
196     {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
197      {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
198      MS_MAKE_TAG('a','r','m','n')},
199     {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
200      {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
201      MS_MAKE_TAG('g','e','o','r')},
202     {{Script_Sinhala, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
203      {LANG_SINHALESE, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
204      MS_MAKE_TAG('s','i','n','h')},
205     {{Script_Tibetan, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
206      {LANG_TIBETAN, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
207      MS_MAKE_TAG('t','i','b','t')},
208     {{Script_Tibetan_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
209      {LANG_TIBETAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
210      MS_MAKE_TAG('t','i','b','t')},
211     {{Script_Phags_pa, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
212      {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
213      MS_MAKE_TAG('p','h','a','g')},
214     {{Script_Thai, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
215      {LANG_THAI, 0, 1, 1, 1, THAI_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 1},
216      MS_MAKE_TAG('t','h','a','i')},
217     {{Script_Thai_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
218      {LANG_THAI, 1, 1, 0, 0, THAI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
219      MS_MAKE_TAG('t','h','a','i')},
220     {{Script_Lao, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
221      {LANG_LAO, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
222      MS_MAKE_TAG('l','a','o',' ')},
223     {{Script_Lao_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
224      {LANG_LAO, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
225      MS_MAKE_TAG('l','a','o',' ')},
226     {{Script_Devanagari, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
227      {LANG_HINDI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
228      MS_MAKE_TAG('d','e','v','a')},
229     {{Script_Devanagari_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
230      {LANG_HINDI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
231      MS_MAKE_TAG('d','e','v','a')},
232     {{Script_Bengali, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
233      {LANG_BENGALI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
234      MS_MAKE_TAG('b','e','n','g')},
235     {{Script_Bengali_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
236      {LANG_BENGALI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
237      MS_MAKE_TAG('b','e','n','g')},
238     {{Script_Bengali_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
239      {LANG_BENGALI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
240      MS_MAKE_TAG('b','e','n','g')},
241     {{Script_Gurmukhi, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
242      {LANG_PUNJABI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
243      MS_MAKE_TAG('g','u','r','u')},
244     {{Script_Gurmukhi_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
245      {LANG_PUNJABI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
246      MS_MAKE_TAG('g','u','r','u')},
247     {{Script_Gujarati, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
248      {LANG_GUJARATI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
249      MS_MAKE_TAG('g','u','j','r')},
250     {{Script_Gujarati_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
251      {LANG_GUJARATI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
252      MS_MAKE_TAG('g','u','j','r')},
253     {{Script_Gujarati_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
254      {LANG_GUJARATI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
255      MS_MAKE_TAG('g','u','j','r')},
256     {{Script_Oriya, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
257      {LANG_ORIYA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
258      MS_MAKE_TAG('o','r','y','a')},
259     {{Script_Oriya_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
260      {LANG_ORIYA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
261      MS_MAKE_TAG('o','r','y','a')},
262     {{Script_Tamil, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
263      {LANG_TAMIL, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
264      MS_MAKE_TAG('t','a','m','l')},
265     {{Script_Tamil_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
266      {LANG_TAMIL, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
267      MS_MAKE_TAG('t','a','m','l')},
268     {{Script_Telugu, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
269      {LANG_TELUGU, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
270      MS_MAKE_TAG('t','e','l','u')},
271     {{Script_Telugu_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
272      {LANG_TELUGU, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
273      MS_MAKE_TAG('t','e','l','u')},
274     {{Script_Kannada, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
275      {LANG_KANNADA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
276      MS_MAKE_TAG('k','n','d','a')},
277     {{Script_Kannada_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
278      {LANG_KANNADA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
279      MS_MAKE_TAG('k','n','d','a')},
280     {{Script_Malayalam, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
281      {LANG_MALAYALAM, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
282      MS_MAKE_TAG('m','l','y','m')},
283     {{Script_Malayalam_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
284      {LANG_MALAYALAM, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
285      MS_MAKE_TAG('m','l','y','m')},
286 };
287
288 static const SCRIPT_PROPERTIES *script_props[] =
289 {
290     &scriptInformation[0].props, &scriptInformation[1].props,
291     &scriptInformation[2].props, &scriptInformation[3].props,
292     &scriptInformation[4].props, &scriptInformation[5].props,
293     &scriptInformation[6].props, &scriptInformation[7].props,
294     &scriptInformation[8].props, &scriptInformation[9].props,
295     &scriptInformation[10].props, &scriptInformation[11].props,
296     &scriptInformation[12].props, &scriptInformation[13].props,
297     &scriptInformation[14].props, &scriptInformation[15].props,
298     &scriptInformation[16].props, &scriptInformation[17].props,
299     &scriptInformation[18].props, &scriptInformation[19].props,
300     &scriptInformation[20].props, &scriptInformation[21].props,
301     &scriptInformation[22].props, &scriptInformation[23].props,
302     &scriptInformation[24].props, &scriptInformation[25].props,
303     &scriptInformation[26].props, &scriptInformation[27].props,
304     &scriptInformation[28].props, &scriptInformation[29].props,
305     &scriptInformation[30].props, &scriptInformation[31].props,
306     &scriptInformation[32].props, &scriptInformation[33].props,
307     &scriptInformation[34].props, &scriptInformation[35].props,
308     &scriptInformation[36].props, &scriptInformation[37].props,
309     &scriptInformation[38].props, &scriptInformation[39].props,
310     &scriptInformation[40].props, &scriptInformation[41].props,
311     &scriptInformation[42].props, &scriptInformation[43].props
312 };
313
314 typedef struct {
315     int numGlyphs;
316     WORD* glyphs;
317     WORD* pwLogClust;
318     int* piAdvance;
319     SCRIPT_VISATTR* psva;
320     GOFFSET* pGoffset;
321     ABC* abc;
322     int iMaxPosX;
323 } StringGlyphs;
324
325 typedef struct {
326     HDC hdc;
327     BOOL invalid;
328     int clip_len;
329     ScriptCache *sc;
330     int cItems;
331     int cMaxGlyphs;
332     SCRIPT_ITEM* pItem;
333     int numItems;
334     StringGlyphs* glyphs;
335     SCRIPT_LOGATTR* logattrs;
336     SIZE* sz;
337 } StringAnalysis;
338
339 static inline void *heap_alloc(SIZE_T size)
340 {
341     return HeapAlloc(GetProcessHeap(), 0, size);
342 }
343
344 static inline void *heap_alloc_zero(SIZE_T size)
345 {
346     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
347 }
348
349 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
350 {
351     return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
352 }
353
354 static inline BOOL heap_free(LPVOID mem)
355 {
356     return HeapFree(GetProcessHeap(), 0, mem);
357 }
358
359 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
360 {
361     return ((ScriptCache *)*psc)->tm.tmDefaultChar;
362 }
363
364 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
365 {
366     return ((ScriptCache *)*psc)->tm.tmHeight;
367 }
368
369 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
370 {
371     return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
372 }
373
374 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
375 {
376     WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
377
378     if (!block) return 0;
379     return block[c & GLYPH_BLOCK_MASK];
380 }
381
382 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
383 {
384     WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
385
386     if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
387     return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
388 }
389
390 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
391 {
392     static const ABC nil;
393     ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
394
395     if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
396     memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
397     return TRUE;
398 }
399
400 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
401 {
402     ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
403
404     if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
405     memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
406     return TRUE;
407 }
408
409 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
410 {
411     ScriptCache *sc;
412
413     if (!psc) return E_INVALIDARG;
414     if (*psc) return S_OK;
415     if (!hdc) return E_PENDING;
416
417     if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
418     if (!GetTextMetricsW(hdc, &sc->tm))
419     {
420         heap_free(sc);
421         return E_INVALIDARG;
422     }
423     if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
424     {
425         heap_free(sc);
426         return E_INVALIDARG;
427     }
428     *psc = sc;
429     TRACE("<- %p\n", sc);
430     return S_OK;
431 }
432
433 static WCHAR mirror_char( WCHAR ch )
434 {
435     extern const WCHAR wine_mirror_map[];
436     return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
437 }
438
439 static WORD get_char_script( WCHAR ch)
440 {
441     WORD type = 0;
442     int i;
443
444     if (ch == 0xc || ch == 0x20 || ch == 0x202f)
445         return Script_CR;
446
447     GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
448
449     if (type == 0)
450         return SCRIPT_UNDEFINED;
451
452     if (type & C1_CNTRL)
453         return Script_Control;
454
455     i = 0;
456     do
457     {
458         if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
459             break;
460
461         if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
462         {
463             if (scriptRanges[i].numericScript && type & C1_DIGIT)
464                 return scriptRanges[i].numericScript;
465             if (scriptRanges[i].punctScript && type & C1_PUNCT)
466                 return scriptRanges[i].punctScript;
467             return scriptRanges[i].script;
468         }
469         i++;
470     } while (1);
471
472     return SCRIPT_UNDEFINED;
473 }
474
475 /***********************************************************************
476  *      DllMain
477  *
478  */
479 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
480 {
481     switch(fdwReason)
482     {
483     case DLL_PROCESS_ATTACH:
484         DisableThreadLibraryCalls(hInstDLL);
485         break;
486     case DLL_PROCESS_DETACH:
487         break;
488     }
489     return TRUE;
490 }
491
492 /***********************************************************************
493  *      ScriptFreeCache (USP10.@)
494  *
495  * Free a script cache.
496  *
497  * PARAMS
498  *   psc [I/O] Script cache.
499  *
500  * RETURNS
501  *  Success: S_OK
502  *  Failure: Non-zero HRESULT value.
503  */
504 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
505 {
506     TRACE("%p\n", psc);
507
508     if (psc && *psc)
509     {
510         unsigned int i;
511         for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
512         {
513             heap_free(((ScriptCache *)*psc)->glyphs[i]);
514             heap_free(((ScriptCache *)*psc)->widths[i]);
515         }
516         heap_free(((ScriptCache *)*psc)->GSUB_Table);
517         heap_free(((ScriptCache *)*psc)->features);
518         heap_free(*psc);
519         *psc = NULL;
520     }
521     return S_OK;
522 }
523
524 /***********************************************************************
525  *      ScriptGetProperties (USP10.@)
526  *
527  * Retrieve a list of script properties.
528  *
529  * PARAMS
530  *  props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
531  *  num   [I] Pointer to the number of scripts.
532  *
533  * RETURNS
534  *  Success: S_OK
535  *  Failure: Non-zero HRESULT value.
536  *
537  * NOTES
538  *  Behaviour matches WinXP.
539  */
540 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
541 {
542     TRACE("(%p,%p)\n", props, num);
543
544     if (!props && !num) return E_INVALIDARG;
545
546     if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
547     if (props) *props = script_props;
548
549     return S_OK;
550 }
551
552 /***********************************************************************
553  *      ScriptGetFontProperties (USP10.@)
554  *
555  * Get information on special glyphs.
556  *
557  * PARAMS
558  *  hdc [I]   Device context.
559  *  psc [I/O] Opaque pointer to a script cache.
560  *  sfp [O]   Font properties structure.
561  */
562 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
563 {
564     HRESULT hr;
565
566     TRACE("%p,%p,%p\n", hdc, psc, sfp);
567
568     if (!sfp) return E_INVALIDARG;
569     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
570
571     if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
572         return E_INVALIDARG;
573
574     /* return something sensible? */
575     sfp->wgBlank = 0;
576     sfp->wgDefault = get_cache_default_char(psc);
577     sfp->wgInvalid = 0;
578     sfp->wgKashida = 0xffff;
579     sfp->iKashidaWidth = 0;
580
581     return S_OK;
582 }
583
584 /***********************************************************************
585  *      ScriptRecordDigitSubstitution (USP10.@)
586  *
587  *  Record digit substitution settings for a given locale.
588  *
589  *  PARAMS
590  *   locale [I] Locale identifier.
591  *   sds    [I] Structure to record substitution settings.
592  *
593  *  RETURNS
594  *   Success: S_OK
595  *   Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
596  *
597  *  SEE ALSO
598  *   http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
599  */
600 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
601 {
602     DWORD plgid, sub;
603
604     TRACE("0x%x, %p\n", locale, sds);
605
606     /* This implementation appears to be correct for all languages, but it's
607      * not clear if sds->DigitSubstitute is ever set to anything except 
608      * CONTEXT or NONE in reality */
609
610     if (!sds) return E_POINTER;
611
612     locale = ConvertDefaultLocale(locale);
613
614     if (!IsValidLocale(locale, LCID_INSTALLED))
615         return E_INVALIDARG;
616
617     plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
618     sds->TraditionalDigitLanguage = plgid;
619
620     if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
621         sds->NationalDigitLanguage = plgid;
622     else
623         sds->NationalDigitLanguage = LANG_ENGLISH;
624
625     if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
626                         (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
627
628     switch (sub)
629     {
630     case 0: 
631         if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
632             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
633         else
634             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
635         break;
636     case 1:
637         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
638         break;
639     case 2:
640         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
641         break;
642     default:
643         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
644         break;
645     }
646
647     sds->dwReserved = 0;
648     return S_OK;
649 }
650
651 /***********************************************************************
652  *      ScriptApplyDigitSubstitution (USP10.@)
653  *
654  *  Apply digit substitution settings.
655  *
656  *  PARAMS
657  *   sds [I] Structure with recorded substitution settings.
658  *   sc  [I] Script control structure.
659  *   ss  [I] Script state structure.
660  *
661  *  RETURNS
662  *   Success: S_OK
663  *   Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
664  */
665 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds, 
666                                             SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
667 {
668     SCRIPT_DIGITSUBSTITUTE psds;
669
670     TRACE("%p, %p, %p\n", sds, sc, ss);
671
672     if (!sc || !ss) return E_POINTER;
673     if (!sds)
674     {
675         sds = &psds;
676         if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
677             return E_INVALIDARG;
678     }
679
680     sc->uDefaultLanguage = LANG_ENGLISH;
681     sc->fContextDigits = 0;
682     ss->fDigitSubstitute = 0;
683
684     switch (sds->DigitSubstitute) {
685         case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
686         case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
687         case SCRIPT_DIGITSUBSTITUTE_NONE:
688         case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
689             return S_OK;
690         default:
691             return E_INVALIDARG;
692     }
693 }
694
695 /***********************************************************************
696  *      ScriptItemizeOpenType (USP10.@)
697  *
698  * Split a Unicode string into shapeable parts.
699  *
700  * PARAMS
701  *  pwcInChars  [I] String to split.
702  *  cInChars    [I] Number of characters in pwcInChars.
703  *  cMaxItems   [I] Maximum number of items to return.
704  *  psControl   [I] Pointer to a SCRIPT_CONTROL structure.
705  *  psState     [I] Pointer to a SCRIPT_STATE structure.
706  *  pItems      [O] Buffer to receive SCRIPT_ITEM structures.
707  *  pScriptTags [O] Buffer to receive OPENTYPE_TAGs.
708  *  pcItems     [O] Number of script items returned.
709  *
710  * RETURNS
711  *  Success: S_OK
712  *  Failure: Non-zero HRESULT value.
713  */
714 HRESULT WINAPI ScriptItemizeOpenType(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
715                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
716                              SCRIPT_ITEM *pItems, OPENTYPE_TAG *pScriptTags, int *pcItems)
717 {
718
719 #define Numeric_space 0x0020
720 #define ZWNJ 0x200C
721 #define ZWJ  0x200D
722
723     int   cnt = 0, index = 0, str = 0;
724     int   New_Script = SCRIPT_UNDEFINED;
725     WORD  *levels = NULL;
726     WORD  *strength = NULL;
727     WORD  baselevel = 0;
728
729     TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
730           psControl, psState, pItems, pcItems);
731
732     if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
733         return E_INVALIDARG;
734
735     if (psState && psControl)
736     {
737         int i;
738         levels = heap_alloc_zero(cInChars * sizeof(WORD));
739         if (!levels)
740             return E_OUTOFMEMORY;
741
742         BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
743         baselevel = levels[0];
744         for (i = 0; i < cInChars; i++)
745             if (levels[i]!=levels[0])
746                 break;
747         if (i >= cInChars && !odd(baselevel))
748         {
749             heap_free(levels);
750             levels = NULL;
751         }
752         else
753         {
754             if (!psControl->fMergeNeutralItems)
755             {
756                 strength = heap_alloc_zero(cInChars * sizeof(WORD));
757                 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
758             }
759         }
760     }
761
762     while ((pwcInChars[cnt] == Numeric_space || pwcInChars[cnt] == ZWJ || pwcInChars[cnt] == ZWNJ) && cnt < cInChars)
763         cnt++;
764
765     if (cnt == cInChars) /* All Spaces */
766     {
767         cnt = 0;
768         New_Script = get_char_script(pwcInChars[cnt]);
769     }
770
771     pItems[index].iCharPos = 0;
772     pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
773     pScriptTags[index] = scriptInformation[get_char_script(pwcInChars[cnt])].scriptTag;
774
775     if (strength)
776         str = strength[cnt];
777
778     cnt = 0;
779     if (levels)
780     {
781         pItems[index].a.fRTL = odd(levels[cnt]);
782         pItems[index].a.fLayoutRTL = odd(levels[cnt]);
783         pItems[index].a.s.uBidiLevel = levels[cnt];
784     }
785     else if (!pItems[index].a.s.uBidiLevel)
786     {
787         pItems[index].a.s.uBidiLevel = baselevel;
788         pItems[index].a.fLayoutRTL = odd(baselevel);
789         pItems[index].a.fRTL = odd(baselevel);
790     }
791
792     TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
793           levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
794           pItems[index].iCharPos);
795
796     for (cnt=1; cnt < cInChars; cnt++)
797     {
798         if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel && (!strength || (strength[cnt] == 0 || strength[cnt] == str))))
799             continue;
800
801         if(pwcInChars[cnt] != Numeric_space && pwcInChars[cnt] != ZWJ && pwcInChars[cnt] != ZWNJ)
802             New_Script = get_char_script(pwcInChars[cnt]);
803         else if (levels)
804         {
805             int j = 1;
806             while (cnt + j < cInChars - 1 && (pwcInChars[cnt+j] == Numeric_space || pwcInChars[cnt+j] == ZWJ || pwcInChars[cnt+j] == ZWNJ))
807                 j++;
808             New_Script = get_char_script(pwcInChars[cnt+j]);
809         }
810
811         if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel || (strength && (strength[cnt] != str)))) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
812         {
813             TRACE("New_Level = %i, New_Strength = %i, New_Script=%d, eScript=%d\n", levels?levels[cnt]:-1, strength?strength[cnt]:str, New_Script, pItems[index].a.eScript);
814
815             if (strength && strength[cnt] != 0)
816                 str = strength[cnt];
817
818             index++;
819             if  (index+1 > cMaxItems)
820                 return E_OUTOFMEMORY;
821
822             pItems[index].iCharPos = cnt;
823             memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
824
825             pItems[index].a = scriptInformation[New_Script].a;
826             pScriptTags[index] = scriptInformation[New_Script].scriptTag;
827             if (levels)
828             {
829                 pItems[index].a.fRTL = odd(levels[cnt]);
830                 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
831                 pItems[index].a.s.uBidiLevel = levels[cnt];
832             }
833             else if (!pItems[index].a.s.uBidiLevel)
834             {
835                 pItems[index].a.s.uBidiLevel = baselevel;
836                 pItems[index].a.fLayoutRTL = odd(baselevel);
837                 pItems[index].a.fRTL = odd(baselevel);
838             }
839
840             TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
841         }
842     }
843
844     /* While not strictly necessary according to the spec, make sure the n+1
845      * item is set up to prevent random behaviour if the caller erroneously
846      * checks the n+1 structure                                              */
847     index++;
848     memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
849
850     TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
851
852     /*  Set one SCRIPT_STATE item being returned  */
853     if  (index + 1 > cMaxItems) return E_OUTOFMEMORY;
854     if (pcItems) *pcItems = index;
855
856     /*  Set SCRIPT_ITEM                                     */
857     pItems[index].iCharPos = cnt;         /* the last item contains the ptr to the lastchar */
858     heap_free(levels);
859     heap_free(strength);
860     return S_OK;
861 }
862
863 /***********************************************************************
864  *      ScriptItemize (USP10.@)
865  *
866  * Split a Unicode string into shapeable parts.
867  *
868  * PARAMS
869  *  pwcInChars [I] String to split.
870  *  cInChars   [I] Number of characters in pwcInChars.
871  *  cMaxItems  [I] Maximum number of items to return.
872  *  psControl  [I] Pointer to a SCRIPT_CONTROL structure.
873  *  psState    [I] Pointer to a SCRIPT_STATE structure.
874  *  pItems     [O] Buffer to receive SCRIPT_ITEM structures.
875  *  pcItems    [O] Number of script items returned.
876  *
877  * RETURNS
878  *  Success: S_OK
879  *  Failure: Non-zero HRESULT value.
880  */
881 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
882                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
883                              SCRIPT_ITEM *pItems, int *pcItems)
884 {
885     OPENTYPE_TAG *discarded_tags;
886     HRESULT res;
887
888     discarded_tags = heap_alloc(cMaxItems * sizeof(OPENTYPE_TAG));
889     if (!discarded_tags)
890         return E_OUTOFMEMORY;
891     res = ScriptItemizeOpenType(pwcInChars, cInChars, cMaxItems, psControl, psState, pItems, discarded_tags, pcItems);
892     heap_free(discarded_tags);
893     return res;
894 }
895
896 /***********************************************************************
897  *      ScriptStringAnalyse (USP10.@)
898  *
899  */
900 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
901                                    int cGlyphs, int iCharset, DWORD dwFlags,
902                                    int iReqWidth, SCRIPT_CONTROL *psControl,
903                                    SCRIPT_STATE *psState, const int *piDx,
904                                    SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
905                                    SCRIPT_STRING_ANALYSIS *pssa)
906 {
907     HRESULT hr = E_OUTOFMEMORY;
908     StringAnalysis *analysis = NULL;
909     int i, num_items = 255;
910
911     TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
912           hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
913           psControl, psState, piDx, pTabdef, pbInClass, pssa);
914
915     if (iCharset != -1)
916     {
917         FIXME("Only Unicode strings are supported\n");
918         return E_INVALIDARG;
919     }
920     if (cString < 1 || !pString) return E_INVALIDARG;
921     if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
922
923     if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
924     if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
925
926     /* FIXME: handle clipping */
927     analysis->clip_len = cString;
928     analysis->hdc = hdc;
929
930     hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
931                        &analysis->numItems);
932
933     while (hr == E_OUTOFMEMORY)
934     {
935         SCRIPT_ITEM *tmp;
936
937         num_items *= 2;
938         if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
939             goto error;
940
941         analysis->pItem = tmp;
942         hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
943                            &analysis->numItems);
944     }
945     if (hr != S_OK) goto error;
946
947     if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
948         ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
949     else
950         goto error;
951
952     if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
953         goto error;
954
955     for (i = 0; i < analysis->numItems; i++)
956     {
957         SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
958         int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
959         int numGlyphs = 1.5 * cChar + 16;
960         WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
961         WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
962         int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
963         SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
964         GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
965         ABC *abc = heap_alloc_zero(sizeof(ABC));
966         int numGlyphsReturned;
967
968         /* FIXME: non unicode strings */
969         const WCHAR* pStr = (const WCHAR*)pString;
970         hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
971                          cChar, numGlyphs, &analysis->pItem[i].a,
972                          glyphs, pwLogClust, psva, &numGlyphsReturned);
973         hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
974                          piAdvance, pGoffset, abc);
975
976         analysis->glyphs[i].numGlyphs = numGlyphsReturned;
977         analysis->glyphs[i].glyphs = glyphs;
978         analysis->glyphs[i].pwLogClust = pwLogClust;
979         analysis->glyphs[i].piAdvance = piAdvance;
980         analysis->glyphs[i].psva = psva;
981         analysis->glyphs[i].pGoffset = pGoffset;
982         analysis->glyphs[i].abc = abc;
983         analysis->glyphs[i].iMaxPosX= -1;
984     }
985
986     *pssa = analysis;
987     return S_OK;
988
989 error:
990     heap_free(analysis->glyphs);
991     heap_free(analysis->logattrs);
992     heap_free(analysis->pItem);
993     heap_free(analysis->sc);
994     heap_free(analysis);
995     return hr;
996 }
997
998 /***********************************************************************
999  *      ScriptStringOut (USP10.@)
1000  *
1001  * This function takes the output of ScriptStringAnalyse and joins the segments
1002  * of glyphs and passes the resulting string to ScriptTextOut.  ScriptStringOut
1003  * only processes glyphs.
1004  *
1005  * Parameters:
1006  *  ssa       [I] buffer to hold the analysed string components
1007  *  iX        [I] X axis displacement for output
1008  *  iY        [I] Y axis displacement for output
1009  *  uOptions  [I] flags controling output processing
1010  *  prc       [I] rectangle coordinates
1011  *  iMinSel   [I] starting pos for substringing output string
1012  *  iMaxSel   [I] ending pos for substringing output string
1013  *  fDisabled [I] controls text highlighting
1014  *
1015  *  RETURNS
1016  *   Success: S_OK
1017  *   Failure: is the value returned by ScriptTextOut
1018  */
1019 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
1020                                int iX,
1021                                int iY, 
1022                                UINT uOptions, 
1023                                const RECT *prc, 
1024                                int iMinSel, 
1025                                int iMaxSel,
1026                                BOOL fDisabled)
1027 {
1028     StringAnalysis *analysis;
1029     WORD *glyphs;
1030     int   item, cnt, x;
1031     HRESULT hr;
1032
1033     TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
1034          ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
1035
1036     if (!(analysis = ssa)) return E_INVALIDARG;
1037
1038     /*
1039      * Get storage for the output buffer for the consolidated strings
1040      */
1041     cnt = 0;
1042     for (item = 0; item < analysis->numItems; item++)
1043     {
1044         cnt += analysis->glyphs[item].numGlyphs;
1045     }
1046     if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
1047
1048     /*
1049      * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
1050      */
1051     uOptions |= ETO_GLYPH_INDEX;
1052     analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
1053
1054     /*
1055      * Copy the string items into the output buffer
1056      */
1057
1058     TRACE("numItems %d\n", analysis->numItems);
1059
1060     cnt = 0;
1061     for (item = 0; item < analysis->numItems; item++)
1062     {
1063         memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
1064               sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
1065
1066         TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
1067         for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
1068             TRACE("%04x", glyphs[x]);
1069         TRACE("\n");
1070
1071         cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
1072     }
1073
1074     hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
1075                        uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
1076                        analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
1077     TRACE("ScriptTextOut hr=%08x\n", hr);
1078
1079     /*
1080      * Free the output buffer and script cache
1081      */
1082     heap_free(glyphs);
1083     return hr;
1084 }
1085
1086 /***********************************************************************
1087  *      ScriptStringCPtoX (USP10.@)
1088  *
1089  */
1090 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
1091 {
1092     int i;
1093     int runningX = 0;
1094     StringAnalysis* analysis = ssa;
1095
1096     TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
1097
1098     if (!ssa || !pX) return S_FALSE;
1099
1100     /* icp out of range */
1101     if(icp < 0)
1102     {
1103         analysis->invalid = TRUE;
1104         return E_INVALIDARG;
1105     }
1106
1107     for(i=0; i<analysis->numItems; i++)
1108     {
1109         int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1110         int offset;
1111         /* initialize max extents for uninitialized runs */
1112         if (analysis->glyphs[i].iMaxPosX == -1)
1113         {
1114             if (analysis->pItem[i].a.fRTL)
1115                 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1116                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1117                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1118             else
1119                 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1120                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1121                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1122         }
1123
1124         if (icp >= CP)
1125         {
1126             runningX += analysis->glyphs[i].iMaxPosX;
1127             icp -= CP;
1128             continue;
1129         }
1130
1131         ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1132                     analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1133                     &analysis->pItem[i].a, &offset);
1134         runningX += offset;
1135
1136         *pX = runningX;
1137         return S_OK;
1138     }
1139
1140     /* icp out of range */
1141     analysis->invalid = TRUE;
1142     return E_INVALIDARG;
1143 }
1144
1145 /***********************************************************************
1146  *      ScriptStringXtoCP (USP10.@)
1147  *
1148  */
1149 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
1150 {
1151     StringAnalysis* analysis = ssa;
1152     int i;
1153     int runningCp = 0;
1154
1155     TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1156
1157     if (!ssa || !piCh || !piTrailing) return S_FALSE;
1158
1159     /* out of range */
1160     if(iX < 0)
1161     {
1162         if (analysis->pItem[0].a.fRTL)
1163         {
1164             *piCh = 1;
1165             *piTrailing = FALSE;
1166         }
1167         else
1168         {
1169             *piCh = -1;
1170             *piTrailing = TRUE;
1171         }
1172         return S_OK;
1173     }
1174
1175     for(i=0; i<analysis->numItems; i++)
1176     {
1177         int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1178         /* initialize max extents for uninitialized runs */
1179         if (analysis->glyphs[i].iMaxPosX == -1)
1180         {
1181             if (analysis->pItem[i].a.fRTL)
1182                 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1183                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1184                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1185             else
1186                 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1187                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1188                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1189         }
1190
1191         if (iX > analysis->glyphs[i].iMaxPosX)
1192         {
1193             iX -= analysis->glyphs[i].iMaxPosX;
1194             runningCp += CP;
1195             continue;
1196         }
1197
1198         ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1199                     analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1200                     &analysis->pItem[i].a, piCh, piTrailing);
1201         *piCh += runningCp;
1202
1203         return S_OK;
1204     }
1205
1206     /* out of range */
1207     *piCh = analysis->pItem[analysis->numItems].iCharPos;
1208     *piTrailing = FALSE;
1209
1210     return S_OK;
1211 }
1212
1213
1214 /***********************************************************************
1215  *      ScriptStringFree (USP10.@)
1216  *
1217  * Free a string analysis.
1218  *
1219  * PARAMS
1220  *  pssa [I] string analysis.
1221  *
1222  * RETURNS
1223  *  Success: S_OK
1224  *  Failure: Non-zero HRESULT value.
1225  */
1226 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1227 {
1228     StringAnalysis* analysis;
1229     BOOL invalid;
1230     int i;
1231
1232     TRACE("(%p)\n", pssa);
1233
1234     if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1235
1236     invalid = analysis->invalid;
1237     ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
1238
1239     for (i = 0; i < analysis->numItems; i++)
1240     {
1241         heap_free(analysis->glyphs[i].glyphs);
1242         heap_free(analysis->glyphs[i].pwLogClust);
1243         heap_free(analysis->glyphs[i].piAdvance);
1244         heap_free(analysis->glyphs[i].psva);
1245         heap_free(analysis->glyphs[i].pGoffset);
1246         heap_free(analysis->glyphs[i].abc);
1247     }
1248
1249     heap_free(analysis->glyphs);
1250     heap_free(analysis->pItem);
1251     heap_free(analysis->logattrs);
1252     heap_free(analysis->sz);
1253     heap_free(analysis->sc);
1254     heap_free(analysis);
1255
1256     if (invalid) return E_INVALIDARG;
1257     return S_OK;
1258 }
1259
1260 /***********************************************************************
1261  *      ScriptCPtoX (USP10.@)
1262  *
1263  */
1264 HRESULT WINAPI ScriptCPtoX(int iCP,
1265                            BOOL fTrailing,
1266                            int cChars,
1267                            int cGlyphs,
1268                            const WORD *pwLogClust,
1269                            const SCRIPT_VISATTR *psva,
1270                            const int *piAdvance,
1271                            const SCRIPT_ANALYSIS *psa,
1272                            int *piX)
1273 {
1274     int item;
1275     float iPosX;
1276     int iSpecial = -1;
1277     int iCluster = -1;
1278     int clust_size = 1;
1279     float special_size = 0.0;
1280     int iMaxPos = 0;
1281     BOOL rtl = FALSE;
1282
1283     TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1284           iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1285           psa, piX);
1286
1287     if (psa->fRTL && ! psa->fLogicalOrder)
1288         rtl = TRUE;
1289
1290     if (fTrailing)
1291         iCP++;
1292
1293     if (rtl)
1294     {
1295         int max_clust = pwLogClust[0];
1296
1297         for (item=0; item < cGlyphs; item++)
1298             if (pwLogClust[item] > max_clust)
1299             {
1300                 ERR("We do not handle non reversed clusters properly\n");
1301                 break;
1302             }
1303
1304         iMaxPos = 0;
1305         for (item = max_clust; item >=0; item --)
1306             iMaxPos += piAdvance[item];
1307     }
1308
1309     iPosX = 0.0;
1310     for (item=0; item < iCP && item < cGlyphs; item++)
1311     {
1312         if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
1313         {
1314             int check;
1315             int clust = pwLogClust[item];
1316
1317             clust_size = 1;
1318             iCluster = -1;
1319
1320             for (check = item+1; check < cGlyphs; check++)
1321             {
1322                 if (pwLogClust[check] == clust)
1323                 {
1324                     clust_size ++;
1325                     if (iCluster == -1)
1326                         iCluster = item;
1327                 }
1328                 else break;
1329             }
1330
1331             if (check >= cGlyphs && !iMaxPos)
1332             {
1333                 for (check = clust; check < cGlyphs; check++)
1334                     special_size += piAdvance[check];
1335                 iSpecial = item;
1336                 special_size /= (cChars - item);
1337                 iPosX += special_size;
1338             }
1339             else
1340                 iPosX += piAdvance[clust] / (float)clust_size;
1341         }
1342         else if (iSpecial != -1)
1343             iPosX += special_size;
1344         else /* (iCluster != -1) */
1345             iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1346     }
1347
1348     if (iMaxPos > 0)
1349     {
1350         iPosX = iMaxPos - iPosX;
1351         if (iPosX < 0)
1352             iPosX = 0;
1353     }
1354
1355     *piX = iPosX;
1356     TRACE("*piX=%d\n", *piX);
1357     return S_OK;
1358 }
1359
1360 /***********************************************************************
1361  *      ScriptXtoCP (USP10.@)
1362  *
1363  */
1364 HRESULT WINAPI ScriptXtoCP(int iX,
1365                            int cChars,
1366                            int cGlyphs,
1367                            const WORD *pwLogClust,
1368                            const SCRIPT_VISATTR *psva,
1369                            const int *piAdvance,
1370                            const SCRIPT_ANALYSIS *psa,
1371                            int *piCP,
1372                            int *piTrailing)
1373 {
1374     int item;
1375     float iPosX;
1376     float iLastPosX;
1377     int iSpecial = -1;
1378     int iCluster = -1;
1379     int clust_size = 1;
1380     float special_size = 0.0;
1381     int direction = 1;
1382
1383     TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1384           iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1385           psa, piCP, piTrailing);
1386
1387     if (psa->fRTL && ! psa->fLogicalOrder)
1388         direction = -1;
1389
1390     if (direction<0)
1391     {
1392         int max_clust = pwLogClust[0];
1393
1394         if (iX < 0)
1395         {
1396             *piCP = cGlyphs;
1397             *piTrailing = 0;
1398             return S_OK;
1399         }
1400
1401         for (item=0; item < cGlyphs; item++)
1402             if (pwLogClust[item] > max_clust)
1403             {
1404                 ERR("We do not handle non reversed clusters properly\n");
1405                 break;
1406             }
1407     }
1408
1409     if (iX < 0)
1410     {
1411         *piCP = -1;
1412         *piTrailing = 1;
1413         return S_OK;
1414     }
1415
1416     iPosX = iLastPosX = 0;
1417     if (direction > 0)
1418         item = 0;
1419     else
1420         item = cGlyphs - 1;
1421     for (; iPosX <= iX && item < cGlyphs && item >= 0; item+=direction)
1422     {
1423         iLastPosX = iPosX;
1424         if (iSpecial == -1 &&
1425              (iCluster == -1 ||
1426               (iCluster != -1 &&
1427                  ((direction > 0 && iCluster+clust_size <= item) ||
1428                   (direction < 0 && iCluster-clust_size >= item))
1429               )
1430              )
1431             )
1432         {
1433             int check;
1434             int clust = pwLogClust[item];
1435
1436             clust_size = 1;
1437             iCluster = -1;
1438
1439             for (check = item+direction; check < cGlyphs && check >= 0; check+=direction)
1440             {
1441                 if (pwLogClust[check] == clust)
1442                 {
1443                     clust_size ++;
1444                     if (iCluster == -1)
1445                         iCluster = item;
1446                 }
1447                 else break;
1448             }
1449
1450             if (check >= cGlyphs && direction > 0)
1451             {
1452                 for (check = clust; check < cGlyphs; check++)
1453                     special_size += piAdvance[check];
1454                 iSpecial = item;
1455                 special_size /= (cChars - item);
1456                 iPosX += special_size;
1457             }
1458             else
1459                 iPosX += piAdvance[clust] / (float)clust_size;
1460         }
1461         else if (iSpecial != -1)
1462             iPosX += special_size;
1463         else /* (iCluster != -1) */
1464             iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1465     }
1466
1467     if (direction > 0)
1468     {
1469         if (iPosX > iX)
1470             item--;
1471         if (item < cGlyphs && ((iPosX - iLastPosX) / 2.0) + iX > iPosX)
1472             *piTrailing = 1;
1473         else
1474             *piTrailing = 0;
1475     }
1476     else
1477     {
1478         if (iX == iLastPosX)
1479             item++;
1480         if (iX >= iLastPosX && iX <= iPosX)
1481             item++;
1482
1483         if (iLastPosX == iX)
1484             *piTrailing = 0;
1485         else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
1486             *piTrailing = 1;
1487         else
1488             *piTrailing = 0;
1489     }
1490
1491     *piCP = item;
1492
1493     TRACE("*piCP=%d\n", *piCP);
1494     TRACE("*piTrailing=%d\n", *piTrailing);
1495     return S_OK;
1496 }
1497
1498 /***********************************************************************
1499  *      ScriptBreak (USP10.@)
1500  *
1501  *  Retrieve line break information.
1502  *
1503  *  PARAMS
1504  *   chars [I] Array of characters.
1505  *   sa    [I] String analysis.
1506  *   la    [I] Array of logical attribute structures.
1507  *
1508  *  RETURNS
1509  *   Success: S_OK
1510  *   Failure: S_FALSE
1511  */
1512 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1513 {
1514     int i;
1515
1516     TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1517
1518     if (!la) return S_FALSE;
1519
1520     for (i = 0; i < count; i++)
1521     {
1522         memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1523
1524         /* FIXME: set the other flags */
1525         la[i].fWhiteSpace = (chars[i] == ' ');
1526         la[i].fCharStop = 1;
1527
1528         if (i > 0 && la[i - 1].fWhiteSpace)
1529         {
1530             la[i].fSoftBreak = 1;
1531             la[i].fWordStop = 1;
1532         }
1533     }
1534     return S_OK;
1535 }
1536
1537 /***********************************************************************
1538  *      ScriptIsComplex (USP10.@)
1539  *
1540  *  Determine if a string is complex.
1541  *
1542  *  PARAMS
1543  *   chars [I] Array of characters to test.
1544  *   len   [I] Length in characters.
1545  *   flag  [I] Flag.
1546  *
1547  *  RETURNS
1548  *   Success: S_OK
1549  *   Failure: S_FALSE
1550  *
1551  */
1552 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1553 {
1554     int i;
1555
1556     TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1557
1558     for (i = 0; i < len; i++)
1559     {
1560         int script;
1561
1562         if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1563             return S_OK;
1564
1565         script = get_char_script(chars[i]);
1566         if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1567             (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1568             return S_OK;
1569     }
1570     return S_FALSE;
1571 }
1572
1573 /***********************************************************************
1574  *      ScriptShapeOpenType (USP10.@)
1575  *
1576  * Produce glyphs and visual attributes for a run.
1577  *
1578  * PARAMS
1579  *  hdc         [I]   Device context.
1580  *  psc         [I/O] Opaque pointer to a script cache.
1581  *  psa         [I/O] Script analysis.
1582  *  tagScript   [I]   The OpenType tag for the Script
1583  *  tagLangSys  [I]   The OpenType tag for the Language
1584  *  rcRangeChars[I]   Array of Character counts in each range
1585  *  rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
1586  *  cRanges     [I]   Count of ranges
1587  *  pwcChars    [I]   Array of characters specifying the run.
1588  *  cChars      [I]   Number of characters in pwcChars.
1589  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1590  *  pwLogClust  [O]   Array of logical cluster info.
1591  *  pCharProps  [O]   Array of character property values
1592  *  pwOutGlyphs [O]   Array of glyphs.
1593  *  pOutGlyphProps [O]  Array of attributes for the retrieved glyphs
1594  *  pcGlyphs    [O]   Number of glyphs returned.
1595  *
1596  * RETURNS
1597  *  Success: S_OK
1598  *  Failure: Non-zero HRESULT value.
1599  */
1600 HRESULT WINAPI ScriptShapeOpenType( HDC hdc, SCRIPT_CACHE *psc,
1601                                     SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
1602                                     OPENTYPE_TAG tagLangSys, int *rcRangeChars,
1603                                     TEXTRANGE_PROPERTIES **rpRangeProperties,
1604                                     int cRanges, const WCHAR *pwcChars, int cChars,
1605                                     int cMaxGlyphs, WORD *pwLogClust,
1606                                     SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs,
1607                                     SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs)
1608 {
1609     HRESULT hr;
1610     unsigned int i;
1611     BOOL rtl;
1612
1613     TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %d, %d, %p, %p, %p, %p, %p )\n",
1614      hdc, psc, psa,
1615      debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
1616      rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
1617      cChars, cMaxGlyphs, pwLogClust, pCharProps, pwOutGlyphs, pOutGlyphProps, pcGlyphs);
1618
1619     if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1620                    psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1621
1622     if (!pOutGlyphProps || !pcGlyphs || !pCharProps) return E_INVALIDARG;
1623     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1624
1625     if (cRanges)
1626         FIXME("Ranges not supported yet\n");
1627
1628     rtl = (!psa->fLogicalOrder && psa->fRTL);
1629
1630     *pcGlyphs = cChars;
1631     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1632     if (!pwLogClust) return E_FAIL;
1633
1634     ((ScriptCache *)*psc)->userScript = tagScript;
1635     ((ScriptCache *)*psc)->userLang = tagLangSys;
1636
1637     /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
1638     for (i = 0; i < cChars; i++)
1639     {
1640         int idx = i;
1641         if (rtl) idx = cChars - 1 - i;
1642         /* FIXME: set to better values */
1643         pOutGlyphProps[i].sva.uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1644         pOutGlyphProps[i].sva.fClusterStart  = 1;
1645         pOutGlyphProps[i].sva.fDiacritic     = 0;
1646         pOutGlyphProps[i].sva.fZeroWidth     = 0;
1647         pOutGlyphProps[i].sva.fReserved      = 0;
1648         pOutGlyphProps[i].sva.fShapeReserved = 0;
1649
1650         /* FIXME: have the shaping engine set this */
1651         pCharProps[i].fCanGlyphAlone = 0;
1652
1653         pwLogClust[i] = idx;
1654     }
1655
1656     if (!psa->fNoGlyphIndex)
1657     {
1658         WCHAR *rChars;
1659         if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
1660
1661         rChars = heap_alloc(sizeof(WCHAR) * cChars);
1662         if (!rChars) return E_OUTOFMEMORY;
1663         for (i = 0; i < cChars; i++)
1664         {
1665             int idx = i;
1666             WCHAR chInput;
1667             if (rtl) idx = cChars - 1 - i;
1668             if (psa->fRTL)
1669                 chInput = mirror_char(pwcChars[idx]);
1670             else
1671                 chInput = pwcChars[idx];
1672             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1673             {
1674                 WORD glyph;
1675                 if (!hdc) return E_PENDING;
1676                 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1677                 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1678             }
1679             rChars[i] = chInput;
1680         }
1681
1682         if (get_cache_pitch_family(psc) & TMPF_TRUETYPE)
1683         {
1684             SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1685             SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
1686             SHAPE_CharGlyphProp(hdc, (ScriptCache *)*psc, psa, pwcChars, cChars, pwOutGlyphs, *pcGlyphs, pwLogClust, pCharProps, pOutGlyphProps);
1687         }
1688         heap_free(rChars);
1689     }
1690     else
1691     {
1692         TRACE("no glyph translation\n");
1693         for (i = 0; i < cChars; i++)
1694         {
1695             int idx = i;
1696             /* No mirroring done here */
1697             if (rtl) idx = cChars - 1 - i;
1698             pwOutGlyphs[i] = pwcChars[idx];
1699         }
1700     }
1701
1702     return S_OK;
1703 }
1704
1705
1706 /***********************************************************************
1707  *      ScriptShape (USP10.@)
1708  *
1709  * Produce glyphs and visual attributes for a run.
1710  *
1711  * PARAMS
1712  *  hdc         [I]   Device context.
1713  *  psc         [I/O] Opaque pointer to a script cache.
1714  *  pwcChars    [I]   Array of characters specifying the run.
1715  *  cChars      [I]   Number of characters in pwcChars.
1716  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1717  *  psa         [I/O] Script analysis.
1718  *  pwOutGlyphs [O]   Array of glyphs.
1719  *  pwLogClust  [O]   Array of logical cluster info.
1720  *  psva        [O]   Array of visual attributes.
1721  *  pcGlyphs    [O]   Number of glyphs returned.
1722  *
1723  * RETURNS
1724  *  Success: S_OK
1725  *  Failure: Non-zero HRESULT value.
1726  */
1727 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
1728                            int cChars, int cMaxGlyphs,
1729                            SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1730                            SCRIPT_VISATTR *psva, int *pcGlyphs)
1731 {
1732     HRESULT hr;
1733     int i;
1734     SCRIPT_CHARPROP *charProps;
1735     SCRIPT_GLYPHPROP *glyphProps;
1736
1737     if (!psva || !pcGlyphs) return E_INVALIDARG;
1738     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1739
1740     charProps = heap_alloc_zero(sizeof(SCRIPT_CHARPROP)*cChars);
1741     if (!charProps) return E_OUTOFMEMORY;
1742     glyphProps = heap_alloc_zero(sizeof(SCRIPT_GLYPHPROP)*cMaxGlyphs);
1743     if (!glyphProps) return E_OUTOFMEMORY;
1744
1745     hr = ScriptShapeOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, pwcChars, cChars, cMaxGlyphs, pwLogClust, charProps, pwOutGlyphs, glyphProps, pcGlyphs);
1746
1747     if (SUCCEEDED(hr))
1748     {
1749         for (i = 0; i < *pcGlyphs; i++)
1750             psva[i] = glyphProps[i].sva;
1751     }
1752
1753     heap_free(charProps);
1754     heap_free(glyphProps);
1755
1756     return hr;
1757 }
1758
1759 /***********************************************************************
1760  *      ScriptPlaceOpenType (USP10.@)
1761  *
1762  * Produce advance widths for a run.
1763  *
1764  * PARAMS
1765  *  hdc       [I]   Device context.
1766  *  psc       [I/O] Opaque pointer to a script cache.
1767  *  psa       [I/O] String analysis.
1768  *  tagScript   [I]   The OpenType tag for the Script
1769  *  tagLangSys  [I]   The OpenType tag for the Language
1770  *  rcRangeChars[I]   Array of Character counts in each range
1771  *  rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
1772  *  cRanges     [I]   Count of ranges
1773  *  pwcChars    [I]   Array of characters specifying the run.
1774  *  pwLogClust  [I]   Array of logical cluster info
1775  *  pCharProps  [I]   Array of character property values
1776  *  cChars      [I]   Number of characters in pwcChars.
1777  *  pwGlyphs  [I]   Array of glyphs.
1778  *  pGlyphProps [I]  Array of attributes for the retrieved glyphs
1779  *  cGlyphs [I] Count of Glyphs
1780  *  piAdvance [O]   Array of advance widths.
1781  *  pGoffset  [O]   Glyph offsets.
1782  *  pABC      [O]   Combined ABC width.
1783  *
1784  * RETURNS
1785  *  Success: S_OK
1786  *  Failure: Non-zero HRESULT value.
1787  */
1788
1789 HRESULT WINAPI ScriptPlaceOpenType( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
1790                                     OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys,
1791                                     int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties,
1792                                     int cRanges, const WCHAR *pwcChars, WORD *pwLogClust,
1793                                     SCRIPT_CHARPROP *pCharProps, int cChars,
1794                                     const WORD *pwGlyphs, const SCRIPT_GLYPHPROP *pGlyphProps,
1795                                     int cGlyphs, int *piAdvance,
1796                                     GOFFSET *pGoffset, ABC *pABC
1797 )
1798 {
1799     HRESULT hr;
1800     int i;
1801
1802     TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %p, %p, %d, %p, %p, %d, %p %p %p)\n",
1803      hdc, psc, psa,
1804      debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
1805      rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
1806      pwLogClust, pCharProps, cChars, pwGlyphs, pGlyphProps, cGlyphs, piAdvance,
1807      pGoffset, pABC);
1808
1809     if (!pGlyphProps) return E_INVALIDARG;
1810     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1811     if (!pGoffset) return E_FAIL;
1812
1813     if (cRanges)
1814         FIXME("Ranges not supported yet\n");
1815
1816     ((ScriptCache *)*psc)->userScript = tagScript;
1817     ((ScriptCache *)*psc)->userLang = tagLangSys;
1818
1819     if (pABC) memset(pABC, 0, sizeof(ABC));
1820     for (i = 0; i < cGlyphs; i++)
1821     {
1822         ABC abc;
1823         if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1824         {
1825             if (!hdc) return E_PENDING;
1826             if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1827             {
1828                 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1829             }
1830             else
1831             {
1832                 INT width;
1833                 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1834                 abc.abcB = width;
1835                 abc.abcA = abc.abcC = 0;
1836             }
1837             set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1838         }
1839         if (pABC)
1840         {
1841             pABC->abcA += abc.abcA;
1842             pABC->abcB += abc.abcB;
1843             pABC->abcC += abc.abcC;
1844         }
1845         /* FIXME: set to more reasonable values */
1846         pGoffset[i].du = pGoffset[i].dv = 0;
1847         if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1848     }
1849
1850     if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1851     return S_OK;
1852 }
1853
1854 /***********************************************************************
1855  *      ScriptPlace (USP10.@)
1856  *
1857  * Produce advance widths for a run.
1858  *
1859  * PARAMS
1860  *  hdc       [I]   Device context.
1861  *  psc       [I/O] Opaque pointer to a script cache.
1862  *  pwGlyphs  [I]   Array of glyphs.
1863  *  cGlyphs   [I]   Number of glyphs in pwGlyphs.
1864  *  psva      [I]   Array of visual attributes.
1865  *  psa       [I/O] String analysis.
1866  *  piAdvance [O]   Array of advance widths.
1867  *  pGoffset  [O]   Glyph offsets.
1868  *  pABC      [O]   Combined ABC width.
1869  *
1870  * RETURNS
1871  *  Success: S_OK
1872  *  Failure: Non-zero HRESULT value.
1873  */
1874 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
1875                            int cGlyphs, const SCRIPT_VISATTR *psva,
1876                            SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1877 {
1878     HRESULT hr;
1879     SCRIPT_GLYPHPROP *glyphProps;
1880     int i;
1881
1882     TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n",  hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1883           piAdvance, pGoffset, pABC);
1884
1885     if (!psva) return E_INVALIDARG;
1886     if (!pGoffset) return E_FAIL;
1887
1888     glyphProps = heap_alloc(sizeof(SCRIPT_GLYPHPROP)*cGlyphs);
1889     if (!glyphProps) return E_OUTOFMEMORY;
1890
1891     for (i = 0; i < cGlyphs; i++)
1892         glyphProps[i].sva = psva[i];
1893
1894     hr = ScriptPlaceOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, NULL, NULL, NULL, 0, pwGlyphs, glyphProps, cGlyphs, piAdvance, pGoffset, pABC);
1895
1896     heap_free(glyphProps);
1897
1898     return hr;
1899 }
1900
1901 /***********************************************************************
1902  *      ScriptGetCMap (USP10.@)
1903  *
1904  * Retrieve glyph indices.
1905  *
1906  * PARAMS
1907  *  hdc         [I]   Device context.
1908  *  psc         [I/O] Opaque pointer to a script cache.
1909  *  pwcInChars  [I]   Array of Unicode characters.
1910  *  cChars      [I]   Number of characters in pwcInChars.
1911  *  dwFlags     [I]   Flags.
1912  *  pwOutGlyphs [O]   Buffer to receive the array of glyph indices.
1913  *
1914  * RETURNS
1915  *  Success: S_OK
1916  *  Failure: Non-zero HRESULT value.
1917  */
1918 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1919                              int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1920 {
1921     HRESULT hr;
1922     int i;
1923
1924     TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1925           cChars, dwFlags, pwOutGlyphs);
1926
1927     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1928
1929     hr = S_OK;
1930
1931     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1932     {
1933         for (i = 0; i < cChars; i++)
1934         {
1935             WCHAR inChar;
1936             if (dwFlags == SGCM_RTL)
1937                 inChar = mirror_char(pwcInChars[i]);
1938             else
1939                 inChar = pwcInChars[i];
1940             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1941             {
1942                 WORD glyph;
1943                 if (!hdc) return E_PENDING;
1944                 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1945                 if (glyph == 0xffff)
1946                 {
1947                     hr = S_FALSE;
1948                     glyph = 0x0;
1949                 }
1950                 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1951             }
1952         }
1953     }
1954     else
1955     {
1956         TRACE("no glyph translation\n");
1957         for (i = 0; i < cChars; i++)
1958         {
1959             WCHAR inChar;
1960             if (dwFlags == SGCM_RTL)
1961                 inChar = mirror_char(pwcInChars[i]);
1962             else
1963                 inChar = pwcInChars[i];
1964             pwOutGlyphs[i] = inChar;
1965         }
1966     }
1967     return hr;
1968 }
1969
1970 /***********************************************************************
1971  *      ScriptTextOut (USP10.@)
1972  *
1973  */
1974 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions, 
1975                              const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved, 
1976                              int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1977                              const int *piJustify, const GOFFSET *pGoffset)
1978 {
1979     HRESULT hr = S_OK;
1980
1981     TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1982          hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1983          piAdvance, piJustify, pGoffset);
1984
1985     if (!hdc || !psc) return E_INVALIDARG;
1986     if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1987
1988     fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1989     fuOptions |= ETO_IGNORELANGUAGE;
1990     if  (!psa->fNoGlyphIndex)                                     /* Have Glyphs?                      */
1991         fuOptions |= ETO_GLYPH_INDEX;                             /* Say don't do translation to glyph */
1992
1993     if (psa->fRTL && psa->fLogicalOrder)
1994     {
1995         int i;
1996         WORD *rtlGlyphs;
1997
1998         rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1999         if (!rtlGlyphs)
2000             return E_OUTOFMEMORY;
2001
2002         for (i = 0; i < cGlyphs; i++)
2003             rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
2004
2005         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
2006             hr = S_FALSE;
2007         heap_free(rtlGlyphs);
2008     }
2009     else
2010         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
2011             hr = S_FALSE;
2012
2013     return hr;
2014 }
2015
2016 /***********************************************************************
2017  *      ScriptCacheGetHeight (USP10.@)
2018  *
2019  * Retrieve the height of the font in the cache.
2020  *
2021  * PARAMS
2022  *  hdc    [I]    Device context.
2023  *  psc    [I/O]  Opaque pointer to a script cache.
2024  *  height [O]    Receives font height.
2025  *
2026  * RETURNS
2027  *  Success: S_OK
2028  *  Failure: Non-zero HRESULT value.
2029  */
2030 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
2031 {
2032     HRESULT hr;
2033
2034     TRACE("(%p, %p, %p)\n", hdc, psc, height);
2035
2036     if (!height) return E_INVALIDARG;
2037     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2038
2039     *height = get_cache_height(psc);
2040     return S_OK;
2041 }
2042
2043 /***********************************************************************
2044  *      ScriptGetGlyphABCWidth (USP10.@)
2045  *
2046  * Retrieve the width of a glyph.
2047  *
2048  * PARAMS
2049  *  hdc    [I]    Device context.
2050  *  psc    [I/O]  Opaque pointer to a script cache.
2051  *  glyph  [I]    Glyph to retrieve the width for.
2052  *  abc    [O]    ABC widths of the glyph.
2053  *
2054  * RETURNS
2055  *  Success: S_OK
2056  *  Failure: Non-zero HRESULT value.
2057  */
2058 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
2059 {
2060     HRESULT hr;
2061
2062     TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
2063
2064     if (!abc) return E_INVALIDARG;
2065     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2066
2067     if (!get_cache_glyph_widths(psc, glyph, abc))
2068     {
2069         if (!hdc) return E_PENDING;
2070         if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2071         {
2072             if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
2073         }
2074         else
2075         {
2076             INT width;
2077             if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
2078             abc->abcB = width;
2079             abc->abcA = abc->abcC = 0;
2080         }
2081         set_cache_glyph_widths(psc, glyph, abc);
2082     }
2083     return S_OK;
2084 }
2085
2086 /***********************************************************************
2087  *      ScriptLayout (USP10.@)
2088  *
2089  * Map embedding levels to visual and/or logical order.
2090  *
2091  * PARAMS
2092  *  runs     [I] Size of level array.
2093  *  level    [I] Array of embedding levels.
2094  *  vistolog [O] Map of embedding levels from visual to logical order.
2095  *  logtovis [O] Map of embedding levels from logical to visual order.
2096  *
2097  * RETURNS
2098  *  Success: S_OK
2099  *  Failure: Non-zero HRESULT value.
2100  *
2101  * BUGS
2102  *  This stub works correctly for any sequence of a single
2103  *  embedding level but not for sequences of different
2104  *  embedding levels, i.e. mixtures of RTL and LTR scripts.
2105  */
2106 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
2107 {
2108     int* indexs;
2109     int ich;
2110
2111     TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
2112
2113     if (!level || (!vistolog && !logtovis))
2114         return E_INVALIDARG;
2115
2116     indexs = heap_alloc(sizeof(int) * runs);
2117     if (!indexs)
2118         return E_OUTOFMEMORY;
2119
2120
2121     if (vistolog)
2122     {
2123         for( ich = 0; ich < runs; ich++)
2124             indexs[ich] = ich;
2125
2126         ich = 0;
2127         while (ich < runs)
2128             ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2129         for (ich = 0; ich < runs; ich++)
2130             vistolog[ich] = indexs[ich];
2131     }
2132
2133
2134     if (logtovis)
2135     {
2136         for( ich = 0; ich < runs; ich++)
2137             indexs[ich] = ich;
2138
2139         ich = 0;
2140         while (ich < runs)
2141             ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2142         for (ich = 0; ich < runs; ich++)
2143             logtovis[ich] = indexs[ich];
2144     }
2145     heap_free(indexs);
2146
2147     return S_OK;
2148 }
2149
2150 /***********************************************************************
2151  *      ScriptStringGetLogicalWidths (USP10.@)
2152  *
2153  * Returns logical widths from a string analysis.
2154  *
2155  * PARAMS
2156  *  ssa  [I] string analysis.
2157  *  piDx [O] logical widths returned.
2158  *
2159  * RETURNS
2160  *  Success: S_OK
2161  *  Failure: a non-zero HRESULT.
2162  */
2163 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
2164 {
2165     int i, j, next = 0;
2166     StringAnalysis *analysis = ssa;
2167
2168     TRACE("%p, %p\n", ssa, piDx);
2169
2170     if (!analysis) return S_FALSE;
2171
2172     for (i = 0; i < analysis->numItems; i++)
2173     {
2174         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
2175         {
2176             piDx[next] = analysis->glyphs[i].piAdvance[j];
2177             next++;
2178         }
2179     }
2180     return S_OK;
2181 }
2182
2183 /***********************************************************************
2184  *      ScriptStringValidate (USP10.@)
2185  *
2186  * Validate a string analysis.
2187  *
2188  * PARAMS
2189  *  ssa [I] string analysis.
2190  *
2191  * RETURNS
2192  *  Success: S_OK
2193  *  Failure: S_FALSE if invalid sequences are found
2194  *           or a non-zero HRESULT if it fails.
2195  */
2196 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
2197 {
2198     StringAnalysis *analysis = ssa;
2199
2200     TRACE("(%p)\n", ssa);
2201
2202     if (!analysis) return E_INVALIDARG;
2203     return (analysis->invalid) ? S_FALSE : S_OK;
2204 }
2205
2206 /***********************************************************************
2207  *      ScriptString_pSize (USP10.@)
2208  *
2209  * Retrieve width and height of an analysed string.
2210  *
2211  * PARAMS
2212  *  ssa [I] string analysis.
2213  *
2214  * RETURNS
2215  *  Success: Pointer to a SIZE structure.
2216  *  Failure: NULL
2217  */
2218 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
2219 {
2220     int i, j;
2221     StringAnalysis *analysis = ssa;
2222
2223     TRACE("(%p)\n", ssa);
2224
2225     if (!analysis) return NULL;
2226
2227     if (!analysis->sz)
2228     {
2229         if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
2230         analysis->sz->cy = analysis->sc->tm.tmHeight;
2231
2232         analysis->sz->cx = 0;
2233         for (i = 0; i < analysis->numItems; i++)
2234             for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
2235                 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
2236     }
2237     return analysis->sz;
2238 }
2239
2240 /***********************************************************************
2241  *      ScriptString_pLogAttr (USP10.@)
2242  *
2243  * Retrieve logical attributes of an analysed string.
2244  *
2245  * PARAMS
2246  *  ssa [I] string analysis.
2247  *
2248  * RETURNS
2249  *  Success: Pointer to an array of SCRIPT_LOGATTR structures.
2250  *  Failure: NULL
2251  */
2252 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
2253 {
2254     StringAnalysis *analysis = ssa;
2255
2256     TRACE("(%p)\n", ssa);
2257
2258     if (!analysis) return NULL;
2259     return analysis->logattrs;
2260 }
2261
2262 /***********************************************************************
2263  *      ScriptString_pcOutChars (USP10.@)
2264  *
2265  * Retrieve the length of a string after clipping.
2266  *
2267  * PARAMS
2268  *  ssa [I] String analysis.
2269  *
2270  * RETURNS
2271  *  Success: Pointer to the length.
2272  *  Failure: NULL
2273  */
2274 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
2275 {
2276     StringAnalysis *analysis = ssa;
2277
2278     TRACE("(%p)\n", ssa);
2279
2280     if (!analysis) return NULL;
2281     return &analysis->clip_len;
2282 }
2283
2284 /***********************************************************************
2285  *      ScriptStringGetOrder (USP10.@)
2286  *
2287  * Retrieve a glyph order map.
2288  *
2289  * PARAMS
2290  *  ssa   [I]   String analysis.
2291  *  order [I/O] Array of glyph positions.
2292  *
2293  * RETURNS
2294  *  Success: S_OK
2295  *  Failure: a non-zero HRESULT.
2296  */
2297 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
2298 {
2299     int i, j;
2300     unsigned int k;
2301     StringAnalysis *analysis = ssa;
2302
2303     TRACE("(%p)\n", ssa);
2304
2305     if (!analysis) return S_FALSE;
2306
2307     /* FIXME: handle RTL scripts */
2308     for (i = 0, k = 0; i < analysis->numItems; i++)
2309         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
2310             order[k] = k;
2311
2312     return S_OK;
2313 }
2314
2315 /***********************************************************************
2316  *      ScriptGetLogicalWidths (USP10.@)
2317  *
2318  * Convert advance widths to logical widths.
2319  *
2320  * PARAMS
2321  *  sa          [I] Script analysis.
2322  *  nbchars     [I] Number of characters.
2323  *  nbglyphs    [I] Number of glyphs.
2324  *  glyph_width [I] Array of glyph widths.
2325  *  log_clust   [I] Array of logical clusters.
2326  *  sva         [I] Visual attributes.
2327  *  widths      [O] Array of logical widths.
2328  *
2329  * RETURNS
2330  *  Success: S_OK
2331  *  Failure: a non-zero HRESULT.
2332  */
2333 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
2334                                       const int *glyph_width, const WORD *log_clust,
2335                                       const SCRIPT_VISATTR *sva, int *widths)
2336 {
2337     int i;
2338
2339     TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
2340           sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
2341
2342     /* FIXME */
2343     for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
2344     return S_OK;
2345 }
2346
2347 /***********************************************************************
2348  *      ScriptApplyLogicalWidth (USP10.@)
2349  *
2350  * Generate glyph advance widths.
2351  *
2352  * PARAMS
2353  *  dx          [I]   Array of logical advance widths.
2354  *  num_chars   [I]   Number of characters.
2355  *  num_glyphs  [I]   Number of glyphs.
2356  *  log_clust   [I]   Array of logical clusters.
2357  *  sva         [I]   Visual attributes.
2358  *  advance     [I]   Array of glyph advance widths.
2359  *  sa          [I]   Script analysis.
2360  *  abc         [I/O] Summed ABC widths.
2361  *  justify     [O]   Array of glyph advance widths.
2362  *
2363  * RETURNS
2364  *  Success: S_OK
2365  *  Failure: a non-zero HRESULT.
2366  */
2367 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
2368                                        const WORD *log_clust, const SCRIPT_VISATTR *sva,
2369                                        const int *advance, const SCRIPT_ANALYSIS *sa,
2370                                        ABC *abc, int *justify)
2371 {
2372     int i;
2373
2374     FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
2375           dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
2376
2377     for (i = 0; i < num_chars; i++) justify[i] = advance[i];
2378     return S_OK;
2379 }
2380
2381 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
2382                              int num_glyphs, int dx, int min_kashida, int *justify)
2383 {
2384     int i;
2385
2386     FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
2387
2388     for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
2389     return S_OK;
2390 }