comctl32: Fix a logical operator typo.
[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)->GDEF_Table);
518         heap_free(((ScriptCache *)*psc)->features);
519         heap_free(*psc);
520         *psc = NULL;
521     }
522     return S_OK;
523 }
524
525 /***********************************************************************
526  *      ScriptGetProperties (USP10.@)
527  *
528  * Retrieve a list of script properties.
529  *
530  * PARAMS
531  *  props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
532  *  num   [I] Pointer to the number of scripts.
533  *
534  * RETURNS
535  *  Success: S_OK
536  *  Failure: Non-zero HRESULT value.
537  *
538  * NOTES
539  *  Behaviour matches WinXP.
540  */
541 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
542 {
543     TRACE("(%p,%p)\n", props, num);
544
545     if (!props && !num) return E_INVALIDARG;
546
547     if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
548     if (props) *props = script_props;
549
550     return S_OK;
551 }
552
553 /***********************************************************************
554  *      ScriptGetFontProperties (USP10.@)
555  *
556  * Get information on special glyphs.
557  *
558  * PARAMS
559  *  hdc [I]   Device context.
560  *  psc [I/O] Opaque pointer to a script cache.
561  *  sfp [O]   Font properties structure.
562  */
563 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
564 {
565     HRESULT hr;
566
567     TRACE("%p,%p,%p\n", hdc, psc, sfp);
568
569     if (!sfp) return E_INVALIDARG;
570     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
571
572     if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
573         return E_INVALIDARG;
574
575     /* return something sensible? */
576     sfp->wgBlank = 0;
577     sfp->wgDefault = get_cache_default_char(psc);
578     sfp->wgInvalid = 0;
579     sfp->wgKashida = 0xffff;
580     sfp->iKashidaWidth = 0;
581
582     return S_OK;
583 }
584
585 /***********************************************************************
586  *      ScriptRecordDigitSubstitution (USP10.@)
587  *
588  *  Record digit substitution settings for a given locale.
589  *
590  *  PARAMS
591  *   locale [I] Locale identifier.
592  *   sds    [I] Structure to record substitution settings.
593  *
594  *  RETURNS
595  *   Success: S_OK
596  *   Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
597  *
598  *  SEE ALSO
599  *   http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
600  */
601 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
602 {
603     DWORD plgid, sub;
604
605     TRACE("0x%x, %p\n", locale, sds);
606
607     /* This implementation appears to be correct for all languages, but it's
608      * not clear if sds->DigitSubstitute is ever set to anything except 
609      * CONTEXT or NONE in reality */
610
611     if (!sds) return E_POINTER;
612
613     locale = ConvertDefaultLocale(locale);
614
615     if (!IsValidLocale(locale, LCID_INSTALLED))
616         return E_INVALIDARG;
617
618     plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
619     sds->TraditionalDigitLanguage = plgid;
620
621     if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
622         sds->NationalDigitLanguage = plgid;
623     else
624         sds->NationalDigitLanguage = LANG_ENGLISH;
625
626     if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
627                         (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
628
629     switch (sub)
630     {
631     case 0: 
632         if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
633             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
634         else
635             sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
636         break;
637     case 1:
638         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
639         break;
640     case 2:
641         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
642         break;
643     default:
644         sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
645         break;
646     }
647
648     sds->dwReserved = 0;
649     return S_OK;
650 }
651
652 /***********************************************************************
653  *      ScriptApplyDigitSubstitution (USP10.@)
654  *
655  *  Apply digit substitution settings.
656  *
657  *  PARAMS
658  *   sds [I] Structure with recorded substitution settings.
659  *   sc  [I] Script control structure.
660  *   ss  [I] Script state structure.
661  *
662  *  RETURNS
663  *   Success: S_OK
664  *   Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
665  */
666 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds, 
667                                             SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
668 {
669     SCRIPT_DIGITSUBSTITUTE psds;
670
671     TRACE("%p, %p, %p\n", sds, sc, ss);
672
673     if (!sc || !ss) return E_POINTER;
674     if (!sds)
675     {
676         sds = &psds;
677         if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
678             return E_INVALIDARG;
679     }
680
681     sc->uDefaultLanguage = LANG_ENGLISH;
682     sc->fContextDigits = 0;
683     ss->fDigitSubstitute = 0;
684
685     switch (sds->DigitSubstitute) {
686         case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
687         case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
688         case SCRIPT_DIGITSUBSTITUTE_NONE:
689         case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
690             return S_OK;
691         default:
692             return E_INVALIDARG;
693     }
694 }
695
696 /***********************************************************************
697  *      ScriptItemizeOpenType (USP10.@)
698  *
699  * Split a Unicode string into shapeable parts.
700  *
701  * PARAMS
702  *  pwcInChars  [I] String to split.
703  *  cInChars    [I] Number of characters in pwcInChars.
704  *  cMaxItems   [I] Maximum number of items to return.
705  *  psControl   [I] Pointer to a SCRIPT_CONTROL structure.
706  *  psState     [I] Pointer to a SCRIPT_STATE structure.
707  *  pItems      [O] Buffer to receive SCRIPT_ITEM structures.
708  *  pScriptTags [O] Buffer to receive OPENTYPE_TAGs.
709  *  pcItems     [O] Number of script items returned.
710  *
711  * RETURNS
712  *  Success: S_OK
713  *  Failure: Non-zero HRESULT value.
714  */
715 HRESULT WINAPI ScriptItemizeOpenType(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
716                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
717                              SCRIPT_ITEM *pItems, OPENTYPE_TAG *pScriptTags, int *pcItems)
718 {
719
720 #define Numeric_space 0x0020
721 #define ZWNJ 0x200C
722 #define ZWJ  0x200D
723
724     int   cnt = 0, index = 0, str = 0;
725     int   New_Script = SCRIPT_UNDEFINED;
726     WORD  *levels = NULL;
727     WORD  *strength = NULL;
728     WORD  baselevel = 0;
729
730     TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
731           psControl, psState, pItems, pcItems);
732
733     if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
734         return E_INVALIDARG;
735
736     if (psState && psControl)
737     {
738         int i;
739         levels = heap_alloc_zero(cInChars * sizeof(WORD));
740         if (!levels)
741             return E_OUTOFMEMORY;
742
743         BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
744         baselevel = levels[0];
745         for (i = 0; i < cInChars; i++)
746             if (levels[i]!=levels[0])
747                 break;
748         if (i >= cInChars && !odd(baselevel))
749         {
750             heap_free(levels);
751             levels = NULL;
752         }
753         else
754         {
755             if (!psControl->fMergeNeutralItems)
756             {
757                 strength = heap_alloc_zero(cInChars * sizeof(WORD));
758                 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
759             }
760         }
761     }
762
763     while ((pwcInChars[cnt] == Numeric_space || pwcInChars[cnt] == ZWJ || pwcInChars[cnt] == ZWNJ) && cnt < cInChars)
764         cnt++;
765
766     if (cnt == cInChars) /* All Spaces */
767     {
768         cnt = 0;
769         New_Script = get_char_script(pwcInChars[cnt]);
770     }
771
772     pItems[index].iCharPos = 0;
773     pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
774     pScriptTags[index] = scriptInformation[get_char_script(pwcInChars[cnt])].scriptTag;
775
776     if (strength)
777         str = strength[cnt];
778
779     cnt = 0;
780     if (levels)
781     {
782         pItems[index].a.fRTL = odd(levels[cnt]);
783         pItems[index].a.fLayoutRTL = odd(levels[cnt]);
784         pItems[index].a.s.uBidiLevel = levels[cnt];
785     }
786     else if (!pItems[index].a.s.uBidiLevel)
787     {
788         pItems[index].a.s.uBidiLevel = baselevel;
789         pItems[index].a.fLayoutRTL = odd(baselevel);
790         pItems[index].a.fRTL = odd(baselevel);
791     }
792
793     TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
794           levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
795           pItems[index].iCharPos);
796
797     for (cnt=1; cnt < cInChars; cnt++)
798     {
799         if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel && (!strength || (strength[cnt] == 0 || strength[cnt] == str))))
800             continue;
801
802         if(pwcInChars[cnt] != Numeric_space && pwcInChars[cnt] != ZWJ && pwcInChars[cnt] != ZWNJ)
803             New_Script = get_char_script(pwcInChars[cnt]);
804         else if (levels)
805         {
806             int j = 1;
807             while (cnt + j < cInChars - 1 && (pwcInChars[cnt+j] == Numeric_space || pwcInChars[cnt+j] == ZWJ || pwcInChars[cnt+j] == ZWNJ))
808                 j++;
809             New_Script = get_char_script(pwcInChars[cnt+j]);
810         }
811
812         if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel || (strength && (strength[cnt] != str)))) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
813         {
814             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);
815
816             if (strength && strength[cnt] != 0)
817                 str = strength[cnt];
818
819             index++;
820             if  (index+1 > cMaxItems)
821                 return E_OUTOFMEMORY;
822
823             pItems[index].iCharPos = cnt;
824             memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
825
826             pItems[index].a = scriptInformation[New_Script].a;
827             pScriptTags[index] = scriptInformation[New_Script].scriptTag;
828             if (levels)
829             {
830                 pItems[index].a.fRTL = odd(levels[cnt]);
831                 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
832                 pItems[index].a.s.uBidiLevel = levels[cnt];
833             }
834             else if (!pItems[index].a.s.uBidiLevel)
835             {
836                 pItems[index].a.s.uBidiLevel = baselevel;
837                 pItems[index].a.fLayoutRTL = odd(baselevel);
838                 pItems[index].a.fRTL = odd(baselevel);
839             }
840
841             TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
842         }
843     }
844
845     /* While not strictly necessary according to the spec, make sure the n+1
846      * item is set up to prevent random behaviour if the caller erroneously
847      * checks the n+1 structure                                              */
848     index++;
849     memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
850
851     TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
852
853     /*  Set one SCRIPT_STATE item being returned  */
854     if  (index + 1 > cMaxItems) return E_OUTOFMEMORY;
855     if (pcItems) *pcItems = index;
856
857     /*  Set SCRIPT_ITEM                                     */
858     pItems[index].iCharPos = cnt;         /* the last item contains the ptr to the lastchar */
859     heap_free(levels);
860     heap_free(strength);
861     return S_OK;
862 }
863
864 /***********************************************************************
865  *      ScriptItemize (USP10.@)
866  *
867  * Split a Unicode string into shapeable parts.
868  *
869  * PARAMS
870  *  pwcInChars [I] String to split.
871  *  cInChars   [I] Number of characters in pwcInChars.
872  *  cMaxItems  [I] Maximum number of items to return.
873  *  psControl  [I] Pointer to a SCRIPT_CONTROL structure.
874  *  psState    [I] Pointer to a SCRIPT_STATE structure.
875  *  pItems     [O] Buffer to receive SCRIPT_ITEM structures.
876  *  pcItems    [O] Number of script items returned.
877  *
878  * RETURNS
879  *  Success: S_OK
880  *  Failure: Non-zero HRESULT value.
881  */
882 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
883                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
884                              SCRIPT_ITEM *pItems, int *pcItems)
885 {
886     OPENTYPE_TAG *discarded_tags;
887     HRESULT res;
888
889     discarded_tags = heap_alloc(cMaxItems * sizeof(OPENTYPE_TAG));
890     if (!discarded_tags)
891         return E_OUTOFMEMORY;
892     res = ScriptItemizeOpenType(pwcInChars, cInChars, cMaxItems, psControl, psState, pItems, discarded_tags, pcItems);
893     heap_free(discarded_tags);
894     return res;
895 }
896
897 /***********************************************************************
898  *      ScriptStringAnalyse (USP10.@)
899  *
900  */
901 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
902                                    int cGlyphs, int iCharset, DWORD dwFlags,
903                                    int iReqWidth, SCRIPT_CONTROL *psControl,
904                                    SCRIPT_STATE *psState, const int *piDx,
905                                    SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
906                                    SCRIPT_STRING_ANALYSIS *pssa)
907 {
908     HRESULT hr = E_OUTOFMEMORY;
909     StringAnalysis *analysis = NULL;
910     SCRIPT_CONTROL sControl;
911     SCRIPT_STATE sState;
912     int i, num_items = 255;
913
914     TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
915           hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
916           psControl, psState, piDx, pTabdef, pbInClass, pssa);
917
918     if (iCharset != -1)
919     {
920         FIXME("Only Unicode strings are supported\n");
921         return E_INVALIDARG;
922     }
923     if (cString < 1 || !pString) return E_INVALIDARG;
924     if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
925
926     if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
927     if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
928
929     /* FIXME: handle clipping */
930     analysis->clip_len = cString;
931     analysis->hdc = hdc;
932
933     if (psState)
934         sState = *psState;
935     else
936         memset(&sState, 0, sizeof(SCRIPT_STATE));
937
938     if (psControl)
939         sControl = *psControl;
940     else
941         memset(&sControl, 0, sizeof(SCRIPT_CONTROL));
942
943     hr = ScriptItemize(pString, cString, num_items, &sControl, &sState, analysis->pItem,
944                        &analysis->numItems);
945
946     while (hr == E_OUTOFMEMORY)
947     {
948         SCRIPT_ITEM *tmp;
949
950         num_items *= 2;
951         if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
952             goto error;
953
954         analysis->pItem = tmp;
955         hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
956                            &analysis->numItems);
957     }
958     if (hr != S_OK) goto error;
959
960     if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
961         ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
962     else
963         goto error;
964
965     if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
966         goto error;
967
968     for (i = 0; i < analysis->numItems; i++)
969     {
970         SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
971         int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
972         int numGlyphs = 1.5 * cChar + 16;
973         WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
974         WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
975         int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
976         SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
977         GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
978         ABC *abc = heap_alloc_zero(sizeof(ABC));
979         int numGlyphsReturned;
980
981         /* FIXME: non unicode strings */
982         const WCHAR* pStr = (const WCHAR*)pString;
983         hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
984                          cChar, numGlyphs, &analysis->pItem[i].a,
985                          glyphs, pwLogClust, psva, &numGlyphsReturned);
986         hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
987                          piAdvance, pGoffset, abc);
988
989         analysis->glyphs[i].numGlyphs = numGlyphsReturned;
990         analysis->glyphs[i].glyphs = glyphs;
991         analysis->glyphs[i].pwLogClust = pwLogClust;
992         analysis->glyphs[i].piAdvance = piAdvance;
993         analysis->glyphs[i].psva = psva;
994         analysis->glyphs[i].pGoffset = pGoffset;
995         analysis->glyphs[i].abc = abc;
996         analysis->glyphs[i].iMaxPosX= -1;
997     }
998
999     *pssa = analysis;
1000     return S_OK;
1001
1002 error:
1003     heap_free(analysis->glyphs);
1004     heap_free(analysis->logattrs);
1005     heap_free(analysis->pItem);
1006     heap_free(analysis->sc);
1007     heap_free(analysis);
1008     return hr;
1009 }
1010
1011 /***********************************************************************
1012  *      ScriptStringOut (USP10.@)
1013  *
1014  * This function takes the output of ScriptStringAnalyse and joins the segments
1015  * of glyphs and passes the resulting string to ScriptTextOut.  ScriptStringOut
1016  * only processes glyphs.
1017  *
1018  * Parameters:
1019  *  ssa       [I] buffer to hold the analysed string components
1020  *  iX        [I] X axis displacement for output
1021  *  iY        [I] Y axis displacement for output
1022  *  uOptions  [I] flags controling output processing
1023  *  prc       [I] rectangle coordinates
1024  *  iMinSel   [I] starting pos for substringing output string
1025  *  iMaxSel   [I] ending pos for substringing output string
1026  *  fDisabled [I] controls text highlighting
1027  *
1028  *  RETURNS
1029  *   Success: S_OK
1030  *   Failure: is the value returned by ScriptTextOut
1031  */
1032 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
1033                                int iX,
1034                                int iY, 
1035                                UINT uOptions, 
1036                                const RECT *prc, 
1037                                int iMinSel, 
1038                                int iMaxSel,
1039                                BOOL fDisabled)
1040 {
1041     StringAnalysis *analysis;
1042     WORD *glyphs;
1043     int   item, cnt, x;
1044     HRESULT hr;
1045
1046     TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
1047          ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
1048
1049     if (!(analysis = ssa)) return E_INVALIDARG;
1050
1051     /*
1052      * Get storage for the output buffer for the consolidated strings
1053      */
1054     cnt = 0;
1055     for (item = 0; item < analysis->numItems; item++)
1056     {
1057         cnt += analysis->glyphs[item].numGlyphs;
1058     }
1059     if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
1060
1061     /*
1062      * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
1063      */
1064     uOptions |= ETO_GLYPH_INDEX;
1065     analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
1066
1067     /*
1068      * Copy the string items into the output buffer
1069      */
1070
1071     TRACE("numItems %d\n", analysis->numItems);
1072
1073     cnt = 0;
1074     for (item = 0; item < analysis->numItems; item++)
1075     {
1076         memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
1077               sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
1078
1079         TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
1080         for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
1081             TRACE("%04x", glyphs[x]);
1082         TRACE("\n");
1083
1084         cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
1085     }
1086
1087     hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
1088                        uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
1089                        analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
1090     TRACE("ScriptTextOut hr=%08x\n", hr);
1091
1092     /*
1093      * Free the output buffer and script cache
1094      */
1095     heap_free(glyphs);
1096     return hr;
1097 }
1098
1099 /***********************************************************************
1100  *      ScriptStringCPtoX (USP10.@)
1101  *
1102  */
1103 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
1104 {
1105     int i;
1106     int runningX = 0;
1107     StringAnalysis* analysis = ssa;
1108
1109     TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
1110
1111     if (!ssa || !pX) return S_FALSE;
1112
1113     /* icp out of range */
1114     if(icp < 0)
1115     {
1116         analysis->invalid = TRUE;
1117         return E_INVALIDARG;
1118     }
1119
1120     for(i=0; i<analysis->numItems; i++)
1121     {
1122         int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1123         int offset;
1124         /* initialize max extents for uninitialized runs */
1125         if (analysis->glyphs[i].iMaxPosX == -1)
1126         {
1127             if (analysis->pItem[i].a.fRTL)
1128                 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1129                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1130                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1131             else
1132                 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1133                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1134                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1135         }
1136
1137         if (icp >= CP)
1138         {
1139             runningX += analysis->glyphs[i].iMaxPosX;
1140             icp -= CP;
1141             continue;
1142         }
1143
1144         ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1145                     analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1146                     &analysis->pItem[i].a, &offset);
1147         runningX += offset;
1148
1149         *pX = runningX;
1150         return S_OK;
1151     }
1152
1153     /* icp out of range */
1154     analysis->invalid = TRUE;
1155     return E_INVALIDARG;
1156 }
1157
1158 /***********************************************************************
1159  *      ScriptStringXtoCP (USP10.@)
1160  *
1161  */
1162 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
1163 {
1164     StringAnalysis* analysis = ssa;
1165     int i;
1166     int runningCp = 0;
1167
1168     TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1169
1170     if (!ssa || !piCh || !piTrailing) return S_FALSE;
1171
1172     /* out of range */
1173     if(iX < 0)
1174     {
1175         if (analysis->pItem[0].a.fRTL)
1176         {
1177             *piCh = 1;
1178             *piTrailing = FALSE;
1179         }
1180         else
1181         {
1182             *piCh = -1;
1183             *piTrailing = TRUE;
1184         }
1185         return S_OK;
1186     }
1187
1188     for(i=0; i<analysis->numItems; i++)
1189     {
1190         int CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1191         /* initialize max extents for uninitialized runs */
1192         if (analysis->glyphs[i].iMaxPosX == -1)
1193         {
1194             if (analysis->pItem[i].a.fRTL)
1195                 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1196                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1197                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1198             else
1199                 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1200                             analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1201                             &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1202         }
1203
1204         if (iX > analysis->glyphs[i].iMaxPosX)
1205         {
1206             iX -= analysis->glyphs[i].iMaxPosX;
1207             runningCp += CP;
1208             continue;
1209         }
1210
1211         ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1212                     analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1213                     &analysis->pItem[i].a, piCh, piTrailing);
1214         *piCh += runningCp;
1215
1216         return S_OK;
1217     }
1218
1219     /* out of range */
1220     *piCh = analysis->pItem[analysis->numItems].iCharPos;
1221     *piTrailing = FALSE;
1222
1223     return S_OK;
1224 }
1225
1226
1227 /***********************************************************************
1228  *      ScriptStringFree (USP10.@)
1229  *
1230  * Free a string analysis.
1231  *
1232  * PARAMS
1233  *  pssa [I] string analysis.
1234  *
1235  * RETURNS
1236  *  Success: S_OK
1237  *  Failure: Non-zero HRESULT value.
1238  */
1239 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1240 {
1241     StringAnalysis* analysis;
1242     BOOL invalid;
1243     int i;
1244
1245     TRACE("(%p)\n", pssa);
1246
1247     if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1248
1249     invalid = analysis->invalid;
1250     ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
1251
1252     for (i = 0; i < analysis->numItems; i++)
1253     {
1254         heap_free(analysis->glyphs[i].glyphs);
1255         heap_free(analysis->glyphs[i].pwLogClust);
1256         heap_free(analysis->glyphs[i].piAdvance);
1257         heap_free(analysis->glyphs[i].psva);
1258         heap_free(analysis->glyphs[i].pGoffset);
1259         heap_free(analysis->glyphs[i].abc);
1260     }
1261
1262     heap_free(analysis->glyphs);
1263     heap_free(analysis->pItem);
1264     heap_free(analysis->logattrs);
1265     heap_free(analysis->sz);
1266     heap_free(analysis->sc);
1267     heap_free(analysis);
1268
1269     if (invalid) return E_INVALIDARG;
1270     return S_OK;
1271 }
1272
1273 /***********************************************************************
1274  *      ScriptCPtoX (USP10.@)
1275  *
1276  */
1277 HRESULT WINAPI ScriptCPtoX(int iCP,
1278                            BOOL fTrailing,
1279                            int cChars,
1280                            int cGlyphs,
1281                            const WORD *pwLogClust,
1282                            const SCRIPT_VISATTR *psva,
1283                            const int *piAdvance,
1284                            const SCRIPT_ANALYSIS *psa,
1285                            int *piX)
1286 {
1287     int item;
1288     float iPosX;
1289     int iSpecial = -1;
1290     int iCluster = -1;
1291     int clust_size = 1;
1292     float special_size = 0.0;
1293     int iMaxPos = 0;
1294     BOOL rtl = FALSE;
1295
1296     TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1297           iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1298           psa, piX);
1299
1300     if (psa->fRTL && ! psa->fLogicalOrder)
1301         rtl = TRUE;
1302
1303     if (fTrailing)
1304         iCP++;
1305
1306     if (rtl)
1307     {
1308         int max_clust = pwLogClust[0];
1309
1310         for (item=0; item < cGlyphs; item++)
1311             if (pwLogClust[item] > max_clust)
1312             {
1313                 ERR("We do not handle non reversed clusters properly\n");
1314                 break;
1315             }
1316
1317         iMaxPos = 0;
1318         for (item = max_clust; item >=0; item --)
1319             iMaxPos += piAdvance[item];
1320     }
1321
1322     iPosX = 0.0;
1323     for (item=0; item < iCP && item < cChars; item++)
1324     {
1325         if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
1326         {
1327             int check;
1328             int clust = pwLogClust[item];
1329
1330             clust_size = 1;
1331             iCluster = -1;
1332
1333             for (check = item+1; check < cChars; check++)
1334             {
1335                 if (pwLogClust[check] == clust)
1336                 {
1337                     clust_size ++;
1338                     if (iCluster == -1)
1339                         iCluster = item;
1340                 }
1341                 else break;
1342             }
1343
1344             if (check >= cChars && !iMaxPos)
1345             {
1346                 for (check = clust; check < cChars; check++)
1347                     special_size += piAdvance[check];
1348                 iSpecial = item;
1349                 special_size /= (cChars - item);
1350                 iPosX += special_size;
1351             }
1352             else
1353                 iPosX += piAdvance[clust] / (float)clust_size;
1354         }
1355         else if (iSpecial != -1)
1356             iPosX += special_size;
1357         else /* (iCluster != -1) */
1358             iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1359     }
1360
1361     if (iMaxPos > 0)
1362     {
1363         iPosX = iMaxPos - iPosX;
1364         if (iPosX < 0)
1365             iPosX = 0;
1366     }
1367
1368     *piX = iPosX;
1369     TRACE("*piX=%d\n", *piX);
1370     return S_OK;
1371 }
1372
1373 /***********************************************************************
1374  *      ScriptXtoCP (USP10.@)
1375  *
1376  */
1377 HRESULT WINAPI ScriptXtoCP(int iX,
1378                            int cChars,
1379                            int cGlyphs,
1380                            const WORD *pwLogClust,
1381                            const SCRIPT_VISATTR *psva,
1382                            const int *piAdvance,
1383                            const SCRIPT_ANALYSIS *psa,
1384                            int *piCP,
1385                            int *piTrailing)
1386 {
1387     int item;
1388     float iPosX;
1389     float iLastPosX;
1390     int iSpecial = -1;
1391     int iCluster = -1;
1392     int clust_size = 1;
1393     float special_size = 0.0;
1394     int direction = 1;
1395
1396     TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1397           iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1398           psa, piCP, piTrailing);
1399
1400     if (psa->fRTL && ! psa->fLogicalOrder)
1401         direction = -1;
1402
1403     if (direction<0)
1404     {
1405         int max_clust = pwLogClust[0];
1406
1407         if (iX < 0)
1408         {
1409             *piCP = cChars;
1410             *piTrailing = 0;
1411             return S_OK;
1412         }
1413
1414         for (item=0; item < cChars; item++)
1415             if (pwLogClust[item] > max_clust)
1416             {
1417                 ERR("We do not handle non reversed clusters properly\n");
1418                 break;
1419             }
1420     }
1421
1422     if (iX < 0)
1423     {
1424         *piCP = -1;
1425         *piTrailing = 1;
1426         return S_OK;
1427     }
1428
1429     iPosX = iLastPosX = 0;
1430     if (direction > 0)
1431         item = 0;
1432     else
1433         item = cChars - 1;
1434     for (; iPosX <= iX && item < cChars && item >= 0; item+=direction)
1435     {
1436         iLastPosX = iPosX;
1437         if (iSpecial == -1 &&
1438              (iCluster == -1 ||
1439               (iCluster != -1 &&
1440                  ((direction > 0 && iCluster+clust_size <= item) ||
1441                   (direction < 0 && iCluster-clust_size >= item))
1442               )
1443              )
1444             )
1445         {
1446             int check;
1447             int clust = pwLogClust[item];
1448
1449             clust_size = 1;
1450             iCluster = -1;
1451
1452             for (check = item+direction; check < cChars && check >= 0; check+=direction)
1453             {
1454                 if (pwLogClust[check] == clust)
1455                 {
1456                     clust_size ++;
1457                     if (iCluster == -1)
1458                         iCluster = item;
1459                 }
1460                 else break;
1461             }
1462
1463             if (check >= cChars && direction > 0)
1464             {
1465                 for (check = clust; check < cChars; check++)
1466                     special_size += piAdvance[check];
1467                 iSpecial = item;
1468                 special_size /= (cChars - item);
1469                 iPosX += special_size;
1470             }
1471             else
1472                 iPosX += piAdvance[clust] / (float)clust_size;
1473         }
1474         else if (iSpecial != -1)
1475             iPosX += special_size;
1476         else /* (iCluster != -1) */
1477             iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1478     }
1479
1480     if (direction > 0)
1481     {
1482         if (iPosX > iX)
1483             item--;
1484         if (item < cChars && ((iPosX - iLastPosX) / 2.0) + iX > iPosX)
1485             *piTrailing = 1;
1486         else
1487             *piTrailing = 0;
1488     }
1489     else
1490     {
1491         if (iX == iLastPosX)
1492             item++;
1493         if (iX >= iLastPosX && iX <= iPosX)
1494             item++;
1495
1496         if (iLastPosX == iX)
1497             *piTrailing = 0;
1498         else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
1499             *piTrailing = 1;
1500         else
1501             *piTrailing = 0;
1502     }
1503
1504     *piCP = item;
1505
1506     TRACE("*piCP=%d\n", *piCP);
1507     TRACE("*piTrailing=%d\n", *piTrailing);
1508     return S_OK;
1509 }
1510
1511 /***********************************************************************
1512  *      ScriptBreak (USP10.@)
1513  *
1514  *  Retrieve line break information.
1515  *
1516  *  PARAMS
1517  *   chars [I] Array of characters.
1518  *   sa    [I] String analysis.
1519  *   la    [I] Array of logical attribute structures.
1520  *
1521  *  RETURNS
1522  *   Success: S_OK
1523  *   Failure: S_FALSE
1524  */
1525 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1526 {
1527     TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1528
1529     if (!la) return S_FALSE;
1530
1531     BREAK_line(chars, count, sa, la);
1532
1533     return S_OK;
1534 }
1535
1536 /***********************************************************************
1537  *      ScriptIsComplex (USP10.@)
1538  *
1539  *  Determine if a string is complex.
1540  *
1541  *  PARAMS
1542  *   chars [I] Array of characters to test.
1543  *   len   [I] Length in characters.
1544  *   flag  [I] Flag.
1545  *
1546  *  RETURNS
1547  *   Success: S_OK
1548  *   Failure: S_FALSE
1549  *
1550  */
1551 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1552 {
1553     int i;
1554
1555     TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1556
1557     for (i = 0; i < len; i++)
1558     {
1559         int script;
1560
1561         if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1562             return S_OK;
1563
1564         script = get_char_script(chars[i]);
1565         if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1566             (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1567             return S_OK;
1568     }
1569     return S_FALSE;
1570 }
1571
1572 /***********************************************************************
1573  *      ScriptShapeOpenType (USP10.@)
1574  *
1575  * Produce glyphs and visual attributes for a run.
1576  *
1577  * PARAMS
1578  *  hdc         [I]   Device context.
1579  *  psc         [I/O] Opaque pointer to a script cache.
1580  *  psa         [I/O] Script analysis.
1581  *  tagScript   [I]   The OpenType tag for the Script
1582  *  tagLangSys  [I]   The OpenType tag for the Language
1583  *  rcRangeChars[I]   Array of Character counts in each range
1584  *  rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
1585  *  cRanges     [I]   Count of ranges
1586  *  pwcChars    [I]   Array of characters specifying the run.
1587  *  cChars      [I]   Number of characters in pwcChars.
1588  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1589  *  pwLogClust  [O]   Array of logical cluster info.
1590  *  pCharProps  [O]   Array of character property values
1591  *  pwOutGlyphs [O]   Array of glyphs.
1592  *  pOutGlyphProps [O]  Array of attributes for the retrieved glyphs
1593  *  pcGlyphs    [O]   Number of glyphs returned.
1594  *
1595  * RETURNS
1596  *  Success: S_OK
1597  *  Failure: Non-zero HRESULT value.
1598  */
1599 HRESULT WINAPI ScriptShapeOpenType( HDC hdc, SCRIPT_CACHE *psc,
1600                                     SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
1601                                     OPENTYPE_TAG tagLangSys, int *rcRangeChars,
1602                                     TEXTRANGE_PROPERTIES **rpRangeProperties,
1603                                     int cRanges, const WCHAR *pwcChars, int cChars,
1604                                     int cMaxGlyphs, WORD *pwLogClust,
1605                                     SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs,
1606                                     SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs)
1607 {
1608     HRESULT hr;
1609     unsigned int i;
1610     BOOL rtl;
1611
1612     TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %d, %d, %p, %p, %p, %p, %p )\n",
1613      hdc, psc, psa,
1614      debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
1615      rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
1616      cChars, cMaxGlyphs, pwLogClust, pCharProps, pwOutGlyphs, pOutGlyphProps, pcGlyphs);
1617
1618     if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1619                    psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1620
1621     if (!pOutGlyphProps || !pcGlyphs || !pCharProps) return E_INVALIDARG;
1622     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1623
1624     if (cRanges)
1625         FIXME("Ranges not supported yet\n");
1626
1627     rtl = (psa && !psa->fLogicalOrder && psa->fRTL);
1628
1629     *pcGlyphs = cChars;
1630     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1631     if (!pwLogClust) return E_FAIL;
1632
1633     ((ScriptCache *)*psc)->userScript = tagScript;
1634     ((ScriptCache *)*psc)->userLang = tagLangSys;
1635
1636     /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
1637     for (i = 0; i < cChars; i++)
1638     {
1639         int idx = i;
1640         if (rtl) idx = cChars - 1 - i;
1641         /* FIXME: set to better values */
1642         pOutGlyphProps[i].sva.uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1643         pOutGlyphProps[i].sva.fClusterStart  = 1;
1644         pOutGlyphProps[i].sva.fDiacritic     = 0;
1645         pOutGlyphProps[i].sva.fZeroWidth     = 0;
1646         pOutGlyphProps[i].sva.fReserved      = 0;
1647         pOutGlyphProps[i].sva.fShapeReserved = 0;
1648
1649         /* FIXME: have the shaping engine set this */
1650         pCharProps[i].fCanGlyphAlone = 0;
1651
1652         pwLogClust[i] = idx;
1653     }
1654
1655     if (psa && !psa->fNoGlyphIndex)
1656     {
1657         WCHAR *rChars;
1658         if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
1659
1660         rChars = heap_alloc(sizeof(WCHAR) * cChars);
1661         if (!rChars) return E_OUTOFMEMORY;
1662         for (i = 0; i < cChars; i++)
1663         {
1664             int idx = i;
1665             WCHAR chInput;
1666             if (rtl) idx = cChars - 1 - i;
1667             if (psa->fRTL)
1668                 chInput = mirror_char(pwcChars[idx]);
1669             else
1670                 chInput = pwcChars[idx];
1671             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1672             {
1673                 WORD glyph;
1674                 if (!hdc) return E_PENDING;
1675                 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1676                 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1677             }
1678             rChars[i] = chInput;
1679         }
1680
1681         if (get_cache_pitch_family(psc) & TMPF_TRUETYPE)
1682         {
1683             SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
1684             SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
1685             SHAPE_CharGlyphProp(hdc, (ScriptCache *)*psc, psa, pwcChars, cChars, pwOutGlyphs, *pcGlyphs, pwLogClust, pCharProps, pOutGlyphProps);
1686         }
1687         heap_free(rChars);
1688     }
1689     else
1690     {
1691         TRACE("no glyph translation\n");
1692         for (i = 0; i < cChars; i++)
1693         {
1694             int idx = i;
1695             /* No mirroring done here */
1696             if (rtl) idx = cChars - 1 - i;
1697             pwOutGlyphs[i] = pwcChars[idx];
1698         }
1699     }
1700
1701     return S_OK;
1702 }
1703
1704
1705 /***********************************************************************
1706  *      ScriptShape (USP10.@)
1707  *
1708  * Produce glyphs and visual attributes for a run.
1709  *
1710  * PARAMS
1711  *  hdc         [I]   Device context.
1712  *  psc         [I/O] Opaque pointer to a script cache.
1713  *  pwcChars    [I]   Array of characters specifying the run.
1714  *  cChars      [I]   Number of characters in pwcChars.
1715  *  cMaxGlyphs  [I]   Length of pwOutGlyphs.
1716  *  psa         [I/O] Script analysis.
1717  *  pwOutGlyphs [O]   Array of glyphs.
1718  *  pwLogClust  [O]   Array of logical cluster info.
1719  *  psva        [O]   Array of visual attributes.
1720  *  pcGlyphs    [O]   Number of glyphs returned.
1721  *
1722  * RETURNS
1723  *  Success: S_OK
1724  *  Failure: Non-zero HRESULT value.
1725  */
1726 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
1727                            int cChars, int cMaxGlyphs,
1728                            SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1729                            SCRIPT_VISATTR *psva, int *pcGlyphs)
1730 {
1731     HRESULT hr;
1732     int i;
1733     SCRIPT_CHARPROP *charProps;
1734     SCRIPT_GLYPHPROP *glyphProps;
1735
1736     if (!psva || !pcGlyphs) return E_INVALIDARG;
1737     if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1738
1739     charProps = heap_alloc_zero(sizeof(SCRIPT_CHARPROP)*cChars);
1740     if (!charProps) return E_OUTOFMEMORY;
1741     glyphProps = heap_alloc_zero(sizeof(SCRIPT_GLYPHPROP)*cMaxGlyphs);
1742     if (!glyphProps) return E_OUTOFMEMORY;
1743
1744     hr = ScriptShapeOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, pwcChars, cChars, cMaxGlyphs, pwLogClust, charProps, pwOutGlyphs, glyphProps, pcGlyphs);
1745
1746     if (SUCCEEDED(hr))
1747     {
1748         for (i = 0; i < *pcGlyphs; i++)
1749             psva[i] = glyphProps[i].sva;
1750     }
1751
1752     heap_free(charProps);
1753     heap_free(glyphProps);
1754
1755     return hr;
1756 }
1757
1758 /***********************************************************************
1759  *      ScriptPlaceOpenType (USP10.@)
1760  *
1761  * Produce advance widths for a run.
1762  *
1763  * PARAMS
1764  *  hdc       [I]   Device context.
1765  *  psc       [I/O] Opaque pointer to a script cache.
1766  *  psa       [I/O] String analysis.
1767  *  tagScript   [I]   The OpenType tag for the Script
1768  *  tagLangSys  [I]   The OpenType tag for the Language
1769  *  rcRangeChars[I]   Array of Character counts in each range
1770  *  rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
1771  *  cRanges     [I]   Count of ranges
1772  *  pwcChars    [I]   Array of characters specifying the run.
1773  *  pwLogClust  [I]   Array of logical cluster info
1774  *  pCharProps  [I]   Array of character property values
1775  *  cChars      [I]   Number of characters in pwcChars.
1776  *  pwGlyphs  [I]   Array of glyphs.
1777  *  pGlyphProps [I]  Array of attributes for the retrieved glyphs
1778  *  cGlyphs [I] Count of Glyphs
1779  *  piAdvance [O]   Array of advance widths.
1780  *  pGoffset  [O]   Glyph offsets.
1781  *  pABC      [O]   Combined ABC width.
1782  *
1783  * RETURNS
1784  *  Success: S_OK
1785  *  Failure: Non-zero HRESULT value.
1786  */
1787
1788 HRESULT WINAPI ScriptPlaceOpenType( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
1789                                     OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys,
1790                                     int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties,
1791                                     int cRanges, const WCHAR *pwcChars, WORD *pwLogClust,
1792                                     SCRIPT_CHARPROP *pCharProps, int cChars,
1793                                     const WORD *pwGlyphs, const SCRIPT_GLYPHPROP *pGlyphProps,
1794                                     int cGlyphs, int *piAdvance,
1795                                     GOFFSET *pGoffset, ABC *pABC
1796 )
1797 {
1798     HRESULT hr;
1799     int i;
1800
1801     TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %p, %p, %d, %p, %p, %d, %p %p %p)\n",
1802      hdc, psc, psa,
1803      debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
1804      rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
1805      pwLogClust, pCharProps, cChars, pwGlyphs, pGlyphProps, cGlyphs, piAdvance,
1806      pGoffset, pABC);
1807
1808     if (!pGlyphProps) return E_INVALIDARG;
1809     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1810     if (!pGoffset) return E_FAIL;
1811
1812     if (cRanges)
1813         FIXME("Ranges not supported yet\n");
1814
1815     ((ScriptCache *)*psc)->userScript = tagScript;
1816     ((ScriptCache *)*psc)->userLang = tagLangSys;
1817
1818     if (pABC) memset(pABC, 0, sizeof(ABC));
1819     for (i = 0; i < cGlyphs; i++)
1820     {
1821         ABC abc;
1822         if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1823         {
1824             if (!hdc) return E_PENDING;
1825             if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1826             {
1827                 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1828             }
1829             else
1830             {
1831                 INT width;
1832                 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1833                 abc.abcB = width;
1834                 abc.abcA = abc.abcC = 0;
1835             }
1836             set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1837         }
1838         if (pABC)
1839         {
1840             pABC->abcA += abc.abcA;
1841             pABC->abcB += abc.abcB;
1842             pABC->abcC += abc.abcC;
1843         }
1844         /* FIXME: set to more reasonable values */
1845         pGoffset[i].du = pGoffset[i].dv = 0;
1846         if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1847     }
1848
1849     if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1850     return S_OK;
1851 }
1852
1853 /***********************************************************************
1854  *      ScriptPlace (USP10.@)
1855  *
1856  * Produce advance widths for a run.
1857  *
1858  * PARAMS
1859  *  hdc       [I]   Device context.
1860  *  psc       [I/O] Opaque pointer to a script cache.
1861  *  pwGlyphs  [I]   Array of glyphs.
1862  *  cGlyphs   [I]   Number of glyphs in pwGlyphs.
1863  *  psva      [I]   Array of visual attributes.
1864  *  psa       [I/O] String analysis.
1865  *  piAdvance [O]   Array of advance widths.
1866  *  pGoffset  [O]   Glyph offsets.
1867  *  pABC      [O]   Combined ABC width.
1868  *
1869  * RETURNS
1870  *  Success: S_OK
1871  *  Failure: Non-zero HRESULT value.
1872  */
1873 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
1874                            int cGlyphs, const SCRIPT_VISATTR *psva,
1875                            SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1876 {
1877     HRESULT hr;
1878     SCRIPT_GLYPHPROP *glyphProps;
1879     int i;
1880
1881     TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n",  hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1882           piAdvance, pGoffset, pABC);
1883
1884     if (!psva) return E_INVALIDARG;
1885     if (!pGoffset) return E_FAIL;
1886
1887     glyphProps = heap_alloc(sizeof(SCRIPT_GLYPHPROP)*cGlyphs);
1888     if (!glyphProps) return E_OUTOFMEMORY;
1889
1890     for (i = 0; i < cGlyphs; i++)
1891         glyphProps[i].sva = psva[i];
1892
1893     hr = ScriptPlaceOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, NULL, NULL, NULL, 0, pwGlyphs, glyphProps, cGlyphs, piAdvance, pGoffset, pABC);
1894
1895     heap_free(glyphProps);
1896
1897     return hr;
1898 }
1899
1900 /***********************************************************************
1901  *      ScriptGetCMap (USP10.@)
1902  *
1903  * Retrieve glyph indices.
1904  *
1905  * PARAMS
1906  *  hdc         [I]   Device context.
1907  *  psc         [I/O] Opaque pointer to a script cache.
1908  *  pwcInChars  [I]   Array of Unicode characters.
1909  *  cChars      [I]   Number of characters in pwcInChars.
1910  *  dwFlags     [I]   Flags.
1911  *  pwOutGlyphs [O]   Buffer to receive the array of glyph indices.
1912  *
1913  * RETURNS
1914  *  Success: S_OK
1915  *  Failure: Non-zero HRESULT value.
1916  */
1917 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1918                              int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1919 {
1920     HRESULT hr;
1921     int i;
1922
1923     TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1924           cChars, dwFlags, pwOutGlyphs);
1925
1926     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1927
1928     hr = S_OK;
1929
1930     if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1931     {
1932         for (i = 0; i < cChars; i++)
1933         {
1934             WCHAR inChar;
1935             if (dwFlags == SGCM_RTL)
1936                 inChar = mirror_char(pwcInChars[i]);
1937             else
1938                 inChar = pwcInChars[i];
1939             if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1940             {
1941                 WORD glyph;
1942                 if (!hdc) return E_PENDING;
1943                 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1944                 if (glyph == 0xffff)
1945                 {
1946                     hr = S_FALSE;
1947                     glyph = 0x0;
1948                 }
1949                 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1950             }
1951         }
1952     }
1953     else
1954     {
1955         TRACE("no glyph translation\n");
1956         for (i = 0; i < cChars; i++)
1957         {
1958             WCHAR inChar;
1959             if (dwFlags == SGCM_RTL)
1960                 inChar = mirror_char(pwcInChars[i]);
1961             else
1962                 inChar = pwcInChars[i];
1963             pwOutGlyphs[i] = inChar;
1964         }
1965     }
1966     return hr;
1967 }
1968
1969 /***********************************************************************
1970  *      ScriptTextOut (USP10.@)
1971  *
1972  */
1973 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions, 
1974                              const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved, 
1975                              int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1976                              const int *piJustify, const GOFFSET *pGoffset)
1977 {
1978     HRESULT hr = S_OK;
1979
1980     TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1981          hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1982          piAdvance, piJustify, pGoffset);
1983
1984     if (!hdc || !psc) return E_INVALIDARG;
1985     if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1986
1987     fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1988     fuOptions |= ETO_IGNORELANGUAGE;
1989     if  (!psa->fNoGlyphIndex)                                     /* Have Glyphs?                      */
1990         fuOptions |= ETO_GLYPH_INDEX;                             /* Say don't do translation to glyph */
1991
1992     if (psa->fRTL && psa->fLogicalOrder)
1993     {
1994         int i;
1995         WORD *rtlGlyphs;
1996
1997         rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1998         if (!rtlGlyphs)
1999             return E_OUTOFMEMORY;
2000
2001         for (i = 0; i < cGlyphs; i++)
2002             rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
2003
2004         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
2005             hr = S_FALSE;
2006         heap_free(rtlGlyphs);
2007     }
2008     else
2009         if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
2010             hr = S_FALSE;
2011
2012     return hr;
2013 }
2014
2015 /***********************************************************************
2016  *      ScriptCacheGetHeight (USP10.@)
2017  *
2018  * Retrieve the height of the font in the cache.
2019  *
2020  * PARAMS
2021  *  hdc    [I]    Device context.
2022  *  psc    [I/O]  Opaque pointer to a script cache.
2023  *  height [O]    Receives font height.
2024  *
2025  * RETURNS
2026  *  Success: S_OK
2027  *  Failure: Non-zero HRESULT value.
2028  */
2029 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
2030 {
2031     HRESULT hr;
2032
2033     TRACE("(%p, %p, %p)\n", hdc, psc, height);
2034
2035     if (!height) return E_INVALIDARG;
2036     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2037
2038     *height = get_cache_height(psc);
2039     return S_OK;
2040 }
2041
2042 /***********************************************************************
2043  *      ScriptGetGlyphABCWidth (USP10.@)
2044  *
2045  * Retrieve the width of a glyph.
2046  *
2047  * PARAMS
2048  *  hdc    [I]    Device context.
2049  *  psc    [I/O]  Opaque pointer to a script cache.
2050  *  glyph  [I]    Glyph to retrieve the width for.
2051  *  abc    [O]    ABC widths of the glyph.
2052  *
2053  * RETURNS
2054  *  Success: S_OK
2055  *  Failure: Non-zero HRESULT value.
2056  */
2057 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
2058 {
2059     HRESULT hr;
2060
2061     TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
2062
2063     if (!abc) return E_INVALIDARG;
2064     if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2065
2066     if (!get_cache_glyph_widths(psc, glyph, abc))
2067     {
2068         if (!hdc) return E_PENDING;
2069         if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2070         {
2071             if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
2072         }
2073         else
2074         {
2075             INT width;
2076             if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
2077             abc->abcB = width;
2078             abc->abcA = abc->abcC = 0;
2079         }
2080         set_cache_glyph_widths(psc, glyph, abc);
2081     }
2082     return S_OK;
2083 }
2084
2085 /***********************************************************************
2086  *      ScriptLayout (USP10.@)
2087  *
2088  * Map embedding levels to visual and/or logical order.
2089  *
2090  * PARAMS
2091  *  runs     [I] Size of level array.
2092  *  level    [I] Array of embedding levels.
2093  *  vistolog [O] Map of embedding levels from visual to logical order.
2094  *  logtovis [O] Map of embedding levels from logical to visual order.
2095  *
2096  * RETURNS
2097  *  Success: S_OK
2098  *  Failure: Non-zero HRESULT value.
2099  *
2100  * BUGS
2101  *  This stub works correctly for any sequence of a single
2102  *  embedding level but not for sequences of different
2103  *  embedding levels, i.e. mixtures of RTL and LTR scripts.
2104  */
2105 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
2106 {
2107     int* indexs;
2108     int ich;
2109
2110     TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
2111
2112     if (!level || (!vistolog && !logtovis))
2113         return E_INVALIDARG;
2114
2115     indexs = heap_alloc(sizeof(int) * runs);
2116     if (!indexs)
2117         return E_OUTOFMEMORY;
2118
2119
2120     if (vistolog)
2121     {
2122         for( ich = 0; ich < runs; ich++)
2123             indexs[ich] = ich;
2124
2125         ich = 0;
2126         while (ich < runs)
2127             ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2128         for (ich = 0; ich < runs; ich++)
2129             vistolog[ich] = indexs[ich];
2130     }
2131
2132
2133     if (logtovis)
2134     {
2135         for( ich = 0; ich < runs; ich++)
2136             indexs[ich] = ich;
2137
2138         ich = 0;
2139         while (ich < runs)
2140             ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2141         for (ich = 0; ich < runs; ich++)
2142             logtovis[ich] = indexs[ich];
2143     }
2144     heap_free(indexs);
2145
2146     return S_OK;
2147 }
2148
2149 /***********************************************************************
2150  *      ScriptStringGetLogicalWidths (USP10.@)
2151  *
2152  * Returns logical widths from a string analysis.
2153  *
2154  * PARAMS
2155  *  ssa  [I] string analysis.
2156  *  piDx [O] logical widths returned.
2157  *
2158  * RETURNS
2159  *  Success: S_OK
2160  *  Failure: a non-zero HRESULT.
2161  */
2162 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
2163 {
2164     int i, j, next = 0;
2165     StringAnalysis *analysis = ssa;
2166
2167     TRACE("%p, %p\n", ssa, piDx);
2168
2169     if (!analysis) return S_FALSE;
2170
2171     for (i = 0; i < analysis->numItems; i++)
2172     {
2173         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
2174         {
2175             piDx[next] = analysis->glyphs[i].piAdvance[j];
2176             next++;
2177         }
2178     }
2179     return S_OK;
2180 }
2181
2182 /***********************************************************************
2183  *      ScriptStringValidate (USP10.@)
2184  *
2185  * Validate a string analysis.
2186  *
2187  * PARAMS
2188  *  ssa [I] string analysis.
2189  *
2190  * RETURNS
2191  *  Success: S_OK
2192  *  Failure: S_FALSE if invalid sequences are found
2193  *           or a non-zero HRESULT if it fails.
2194  */
2195 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
2196 {
2197     StringAnalysis *analysis = ssa;
2198
2199     TRACE("(%p)\n", ssa);
2200
2201     if (!analysis) return E_INVALIDARG;
2202     return (analysis->invalid) ? S_FALSE : S_OK;
2203 }
2204
2205 /***********************************************************************
2206  *      ScriptString_pSize (USP10.@)
2207  *
2208  * Retrieve width and height of an analysed string.
2209  *
2210  * PARAMS
2211  *  ssa [I] string analysis.
2212  *
2213  * RETURNS
2214  *  Success: Pointer to a SIZE structure.
2215  *  Failure: NULL
2216  */
2217 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
2218 {
2219     int i, j;
2220     StringAnalysis *analysis = ssa;
2221
2222     TRACE("(%p)\n", ssa);
2223
2224     if (!analysis) return NULL;
2225
2226     if (!analysis->sz)
2227     {
2228         if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
2229         analysis->sz->cy = analysis->sc->tm.tmHeight;
2230
2231         analysis->sz->cx = 0;
2232         for (i = 0; i < analysis->numItems; i++)
2233             for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
2234                 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
2235     }
2236     return analysis->sz;
2237 }
2238
2239 /***********************************************************************
2240  *      ScriptString_pLogAttr (USP10.@)
2241  *
2242  * Retrieve logical attributes of an analysed string.
2243  *
2244  * PARAMS
2245  *  ssa [I] string analysis.
2246  *
2247  * RETURNS
2248  *  Success: Pointer to an array of SCRIPT_LOGATTR structures.
2249  *  Failure: NULL
2250  */
2251 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
2252 {
2253     StringAnalysis *analysis = ssa;
2254
2255     TRACE("(%p)\n", ssa);
2256
2257     if (!analysis) return NULL;
2258     return analysis->logattrs;
2259 }
2260
2261 /***********************************************************************
2262  *      ScriptString_pcOutChars (USP10.@)
2263  *
2264  * Retrieve the length of a string after clipping.
2265  *
2266  * PARAMS
2267  *  ssa [I] String analysis.
2268  *
2269  * RETURNS
2270  *  Success: Pointer to the length.
2271  *  Failure: NULL
2272  */
2273 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
2274 {
2275     StringAnalysis *analysis = ssa;
2276
2277     TRACE("(%p)\n", ssa);
2278
2279     if (!analysis) return NULL;
2280     return &analysis->clip_len;
2281 }
2282
2283 /***********************************************************************
2284  *      ScriptStringGetOrder (USP10.@)
2285  *
2286  * Retrieve a glyph order map.
2287  *
2288  * PARAMS
2289  *  ssa   [I]   String analysis.
2290  *  order [I/O] Array of glyph positions.
2291  *
2292  * RETURNS
2293  *  Success: S_OK
2294  *  Failure: a non-zero HRESULT.
2295  */
2296 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
2297 {
2298     int i, j;
2299     unsigned int k;
2300     StringAnalysis *analysis = ssa;
2301
2302     TRACE("(%p)\n", ssa);
2303
2304     if (!analysis) return S_FALSE;
2305
2306     /* FIXME: handle RTL scripts */
2307     for (i = 0, k = 0; i < analysis->numItems; i++)
2308         for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
2309             order[k] = k;
2310
2311     return S_OK;
2312 }
2313
2314 /***********************************************************************
2315  *      ScriptGetLogicalWidths (USP10.@)
2316  *
2317  * Convert advance widths to logical widths.
2318  *
2319  * PARAMS
2320  *  sa          [I] Script analysis.
2321  *  nbchars     [I] Number of characters.
2322  *  nbglyphs    [I] Number of glyphs.
2323  *  glyph_width [I] Array of glyph widths.
2324  *  log_clust   [I] Array of logical clusters.
2325  *  sva         [I] Visual attributes.
2326  *  widths      [O] Array of logical widths.
2327  *
2328  * RETURNS
2329  *  Success: S_OK
2330  *  Failure: a non-zero HRESULT.
2331  */
2332 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
2333                                       const int *glyph_width, const WORD *log_clust,
2334                                       const SCRIPT_VISATTR *sva, int *widths)
2335 {
2336     int i;
2337
2338     TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
2339           sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
2340
2341     /* FIXME */
2342     for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
2343     return S_OK;
2344 }
2345
2346 /***********************************************************************
2347  *      ScriptApplyLogicalWidth (USP10.@)
2348  *
2349  * Generate glyph advance widths.
2350  *
2351  * PARAMS
2352  *  dx          [I]   Array of logical advance widths.
2353  *  num_chars   [I]   Number of characters.
2354  *  num_glyphs  [I]   Number of glyphs.
2355  *  log_clust   [I]   Array of logical clusters.
2356  *  sva         [I]   Visual attributes.
2357  *  advance     [I]   Array of glyph advance widths.
2358  *  sa          [I]   Script analysis.
2359  *  abc         [I/O] Summed ABC widths.
2360  *  justify     [O]   Array of glyph advance widths.
2361  *
2362  * RETURNS
2363  *  Success: S_OK
2364  *  Failure: a non-zero HRESULT.
2365  */
2366 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
2367                                        const WORD *log_clust, const SCRIPT_VISATTR *sva,
2368                                        const int *advance, const SCRIPT_ANALYSIS *sa,
2369                                        ABC *abc, int *justify)
2370 {
2371     int i;
2372
2373     FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
2374           dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
2375
2376     for (i = 0; i < num_chars; i++) justify[i] = advance[i];
2377     return S_OK;
2378 }
2379
2380 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
2381                              int num_glyphs, int dx, int min_kashida, int *justify)
2382 {
2383     int i;
2384
2385     FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
2386
2387     for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
2388     return S_OK;
2389 }