usp10: Fix memory leak.
[wine/multimedia.git] / dlls / usp10 / usp10.c
blob359d4a3b82dea5d406c170c28c52726edafac940
1 /*
2 * Implementation of Uniscribe Script Processor (usp10.dll)
4 * Copyright 2005 Steven Edwards for CodeWeavers
5 * Copyright 2006 Hans Leidekker
6 * Copyright 2010 CodeWeavers, Aric Stewart
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.
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.
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
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.
27 #include <stdarg.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "usp10.h"
37 #include "usp10_internal.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
44 typedef struct _scriptRange
46 WORD script;
47 WORD rangeFirst;
48 WORD rangeLast;
49 WORD numericScript;
50 WORD punctScript;
51 } scriptRange;
53 static const scriptRange scriptRanges[] = {
54 /* Basic Latin: U+0000–U+007A */
55 { Script_Latin, 0x00, 0x07a , Script_Numeric, Script_Punctuation},
56 /* Latin-1 Supplement: U+0080–U+00FF */
57 /* Latin Extended-A: U+0100–U+017F */
58 /* Latin Extended-B: U+0180–U+024F */
59 /* IPA Extensions: U+0250–U+02AF */
60 { Script_Latin, 0x80, 0x2af , Script_Numeric2, Script_Punctuation},
61 /* Combining Diacritical Marks : U+0300–U+036F */
62 { Script_Diacritical,0x300, 0x36f, 0, 0},
63 /* Greek: U+0370–U+03FF */
64 { Script_Greek, 0x370, 0x3ff, 0, 0},
65 /* Cyrillic: U+0400–U+04FF */
66 /* Cyrillic Supplement: U+0500–U+052F */
67 { Script_Cyrillic, 0x400, 0x52f, 0, 0},
68 /* Armenian: U+0530–U+058F */
69 { Script_Armenian, 0x530, 0x58f, 0, 0},
70 /* Hebrew: U+0590–U+05FF */
71 { Script_Hebrew, 0x590, 0x5ff, 0, 0},
72 /* Arabic: U+0600–U+06FF */
73 { Script_Arabic, 0x600, 0x6ef, Script_Arabic_Numeric, 0},
74 /* Defined by Windows */
75 { Script_Persian, 0x6f0, 0x6f9, 0, 0},
76 /* Continue Arabic: U+0600–U+06FF */
77 { Script_Arabic, 0x6fa, 0x6ff, 0, 0},
78 /* Syriac: U+0700–U+074F*/
79 { Script_Syriac, 0x700, 0x74f, 0, 0},
80 /* Arabic Supplement: U+0750–U+077F */
81 { Script_Arabic, 0x750, 0x77f, 0, 0},
82 /* Thaana: U+0780–U+07BF */
83 { Script_Thaana, 0x780, 0x7bf, 0, 0},
84 /* Devanagari: U+0900–U+097F */
85 { Script_Devanagari, 0x900, 0x97f, Script_Devanagari_Numeric, 0},
86 /* Bengali: U+0980–U+09FF */
87 { Script_Bengali, 0x980, 0x9ff, Script_Bengali_Numeric, 0},
88 /* Gurmukhi: U+0A00–U+0A7F*/
89 { Script_Gurmukhi, 0xa00, 0xa7f, Script_Gurmukhi_Numeric, 0},
90 /* Gujarati: U+0A80–U+0AFF*/
91 { Script_Gujarati, 0xa80, 0xaff, Script_Gujarati_Numeric, 0},
92 /* Oriya: U+0B00–U+0B7F */
93 { Script_Oriya, 0xb00, 0xb7f, Script_Oriya_Numeric, 0},
94 /* Tamil: U+0B80–U+0BFF */
95 { Script_Tamil, 0xb80, 0xbff, Script_Tamil_Numeric, 0},
96 /* Telugu: U+0C00–U+0C7F */
97 { Script_Telugu, 0xc00, 0xc7f, Script_Telugu_Numeric, 0},
98 /* Kannada: U+0C80–U+0CFF */
99 { Script_Kannada, 0xc80, 0xcff, Script_Kannada_Numeric, 0},
100 /* Malayalam: U+0D00–U+0D7F */
101 { Script_Malayalam, 0xd00, 0xd7f, Script_Malayalam_Numeric, 0},
102 /* Sinhala: U+0D80–U+0DFF */
103 { Script_Sinhala, 0xd80, 0xdff, 0, 0},
104 /* Thai: U+0E00–U+0E7F */
105 { Script_Thai, 0xe00, 0xe7f, Script_Thai_Numeric, 0},
106 /* Lao: U+0E80–U+0EFF */
107 { Script_Lao, 0xe80, 0xeff, Script_Lao_Numeric, 0},
108 /* Tibetan: U+0F00–U+0FFF */
109 { Script_Tibetan, 0xf00, 0xfff, 0, 0},
110 /* Georgian: U+10A0–U+10FF */
111 { Script_Georgian, 0x10a0, 0x10ff, 0, 0},
112 /* Vedic Extensions: U+1CD0-U+1CFF */
113 { Script_Devanagari, 0x1cd0, 0x1cff, Script_Devanagari_Numeric, 0},
114 /* Phonetic Extensions: U+1D00–U+1DBF */
115 { Script_Latin, 0x1d00, 0x1dbf, 0, 0},
116 /* Combining Diacritical Marks Supplement: U+1DC0–U+1DFF */
117 { Script_Diacritical,0x1dc0, 0x1dff, 0, 0},
118 /* Latin Extended Additional: U+1E00–U+1EFF */
119 { Script_Latin, 0x1e00, 0x1eff, 0, 0},
120 /* Greek Extended: U+1F00–U+1FFF */
121 { Script_Greek, 0x1f00, 0x1fff, 0, 0},
122 /* General Punctuation: U+2000 –U+206f */
123 { Script_Latin, 0x2000, 0x206f, 0, 0},
124 /* Superscripts and Subscripts : U+2070 –U+209f */
125 /* Currency Symbols : U+20a0 –U+20cf */
126 { Script_Numeric2, 0x2070, 0x2070, 0, 0},
127 { Script_Latin, 0x2071, 0x2073, 0, 0},
128 { Script_Numeric2, 0x2074, 0x2079, 0, 0},
129 { Script_Latin, 0x207a, 0x207f, 0, 0},
130 { Script_Numeric2, 0x2080, 0x2089, 0, 0},
131 { Script_Latin, 0x208a, 0x20cf, 0, 0},
132 /* Letterlike Symbols : U+2100 –U+214f */
133 /* Number Forms : U+2150 –U+218f */
134 /* Arrows : U+2190 –U+21ff */
135 /* Mathematical Operators : U+2200 –U+22ff */
136 /* Miscellaneous Technical : U+2300 –U+23ff */
137 /* Control Pictures : U+2400 –U+243f */
138 /* Optical Character Recognition : U+2440 –U+245f */
139 /* Enclosed Alphanumerics : U+2460 –U+24ff */
140 /* Box Drawing : U+2500 –U+25ff */
141 /* Block Elements : U+2580 –U+259f */
142 /* Geometric Shapes : U+25a0 –U+25ff */
143 /* Miscellaneous Symbols : U+2600 –U+26ff */
144 /* Dingbats : U+2700 –U+27bf */
145 /* Miscellaneous Mathematical Symbols-A : U+27c0 –U+27ef */
146 /* Supplemental Arrows-A : U+27f0 –U+27ff */
147 { Script_Latin, 0x2100, 0x27ff, 0, 0},
148 /* Supplemental Arrows-B : U+2900 –U+297f */
149 /* Miscellaneous Mathematical Symbols-B : U+2980 –U+29ff */
150 /* Supplemental Mathematical Operators : U+2a00 –U+2aff */
151 /* Miscellaneous Symbols and Arrows : U+2b00 –U+2bff */
152 { Script_Latin, 0x2900, 0x2bff, 0, 0},
153 /* Latin Extended-C: U+2C60–U+2C7F */
154 { Script_Latin, 0x2c60, 0x2c7f, 0, 0},
155 /* Georgian: U+2D00–U+2D2F */
156 { Script_Georgian, 0x2d00, 0x2d2f, 0, 0},
157 /* Cyrillic Extended-A: U+2DE0–U+2DFF */
158 { Script_Cyrillic, 0x2de0, 0x2dff, 0, 0},
159 /* Cyrillic Extended-B: U+A640–U+A69F */
160 { Script_Cyrillic, 0xa640, 0xa69f, 0, 0},
161 /* Modifier Tone Letters: U+A700–U+A71F */
162 /* Latin Extended-D: U+A720–U+A7FF */
163 { Script_Latin, 0xa700, 0xa7ff, 0, 0},
164 /* Phags-pa: U+A840–U+A87F */
165 { Script_Phags_pa, 0xa840, 0xa87f, 0, 0},
166 /* Devanagari Extended: U+A8E0-U+A8FF */
167 { Script_Devanagari, 0xa8e0, 0xa8ff, Script_Devanagari_Numeric, 0},
168 /* Latin Ligatures: U+FB00–U+FB06 */
169 { Script_Latin, 0xfb00, 0xfb06, 0, 0},
170 /* Armenian ligatures U+FB13..U+FB17 */
171 { Script_Armenian, 0xfb13, 0xfb17, 0, 0},
172 /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
173 { Script_Hebrew, 0xfb1d, 0xfb4f, 0, 0},
174 /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
175 { Script_Arabic, 0xfb50, 0xfdff, 0, 0},
176 /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
177 { Script_Arabic, 0xfe70, 0xfeff, 0, 0},
178 /* END */
179 { SCRIPT_UNDEFINED, 0, 0, 0}
182 typedef struct _scriptData
184 SCRIPT_ANALYSIS a;
185 SCRIPT_PROPERTIES props;
186 OPENTYPE_TAG scriptTag;
187 WCHAR fallbackFont[LF_FACESIZE];
188 } scriptData;
190 /* the must be in order so that the index matches the Script value */
191 static const scriptData scriptInformation[] = {
192 {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
193 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
194 0x00000000,
195 {0}},
196 {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
197 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
198 MS_MAKE_TAG('l','a','t','n'),
199 {0}},
200 {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
201 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
202 0x00000000,
203 {0}},
204 {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
205 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
206 0x00000000,
207 {0}},
208 {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
209 {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0},
210 0x00000000,
211 {0}},
212 {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
213 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
214 0x00000000,
215 {0}},
216 {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
217 {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0},
218 MS_MAKE_TAG('a','r','a','b'),
219 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
220 {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
221 {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
222 MS_MAKE_TAG('a','r','a','b'),
223 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
224 {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
225 {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
226 MS_MAKE_TAG('h','e','b','r'),
227 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
228 {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
229 {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0},
230 MS_MAKE_TAG('s','y','r','c'),
231 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
232 {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
233 {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
234 MS_MAKE_TAG('s','y','r','c'),
235 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
236 {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
237 {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
238 MS_MAKE_TAG('t','h','a','a'),
239 {'M','V',' ','B','o','l','i',0}},
240 {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
241 {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
242 MS_MAKE_TAG('g','r','e','k'),
243 {0}},
244 {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
245 {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
246 MS_MAKE_TAG('c','y','r','l'),
247 {0}},
248 {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
249 {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
250 MS_MAKE_TAG('a','r','m','n'),
251 {'S','y','l','f','a','e','n',0}},
252 {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
253 {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
254 MS_MAKE_TAG('g','e','o','r'),
255 {'S','y','l','f','a','e','n',0}},
256 {{Script_Sinhala, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
257 {LANG_SINHALESE, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
258 MS_MAKE_TAG('s','i','n','h'),
259 {'I','s','k','o','o','l','a',' ','P','o','t','a',0}},
260 {{Script_Tibetan, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
261 {LANG_TIBETAN, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
262 MS_MAKE_TAG('t','i','b','t'),
263 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
264 {{Script_Tibetan_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
265 {LANG_TIBETAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
266 MS_MAKE_TAG('t','i','b','t'),
267 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
268 {{Script_Phags_pa, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
269 {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
270 MS_MAKE_TAG('p','h','a','g'),
271 {'M','i','c','r','o','s','o','f','t',' ','P','h','a','g','s','P','a',0}},
272 {{Script_Thai, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
273 {LANG_THAI, 0, 1, 1, 1, THAI_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 1},
274 MS_MAKE_TAG('t','h','a','i'),
275 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
276 {{Script_Thai_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
277 {LANG_THAI, 1, 1, 0, 0, THAI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
278 MS_MAKE_TAG('t','h','a','i'),
279 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
280 {{Script_Lao, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
281 {LANG_LAO, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
282 MS_MAKE_TAG('l','a','o',' '),
283 {'D','o','k','C','h','a','m','p','a',0}},
284 {{Script_Lao_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
285 {LANG_LAO, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
286 MS_MAKE_TAG('l','a','o',' '),
287 {'D','o','k','C','h','a','m','p','a',0}},
288 {{Script_Devanagari, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
289 {LANG_HINDI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
290 MS_MAKE_TAG('d','e','v','a'),
291 {'M','a','n','g','a','l',0}},
292 {{Script_Devanagari_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
293 {LANG_HINDI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
294 MS_MAKE_TAG('d','e','v','a'),
295 {'M','a','n','g','a','l',0}},
296 {{Script_Bengali, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
297 {LANG_BENGALI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
298 MS_MAKE_TAG('b','e','n','g'),
299 {'V','r','i','n','d','a',0}},
300 {{Script_Bengali_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
301 {LANG_BENGALI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
302 MS_MAKE_TAG('b','e','n','g'),
303 {'V','r','i','n','d','a',0}},
304 {{Script_Bengali_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
305 {LANG_BENGALI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
306 MS_MAKE_TAG('b','e','n','g'),
307 {'V','r','i','n','d','a',0}},
308 {{Script_Gurmukhi, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
309 {LANG_PUNJABI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
310 MS_MAKE_TAG('g','u','r','u'),
311 {'R','a','a','v','i',0}},
312 {{Script_Gurmukhi_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
313 {LANG_PUNJABI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
314 MS_MAKE_TAG('g','u','r','u'),
315 {'R','a','a','v','i',0}},
316 {{Script_Gujarati, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
317 {LANG_GUJARATI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
318 MS_MAKE_TAG('g','u','j','r'),
319 {'S','h','r','u','t','i',0}},
320 {{Script_Gujarati_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
321 {LANG_GUJARATI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
322 MS_MAKE_TAG('g','u','j','r'),
323 {'S','h','r','u','t','i',0}},
324 {{Script_Gujarati_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
325 {LANG_GUJARATI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
326 MS_MAKE_TAG('g','u','j','r'),
327 {'S','h','r','u','t','i',0}},
328 {{Script_Oriya, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
329 {LANG_ORIYA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
330 MS_MAKE_TAG('o','r','y','a'),
331 {'K','a','l','i','n','g','a',0}},
332 {{Script_Oriya_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
333 {LANG_ORIYA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
334 MS_MAKE_TAG('o','r','y','a'),
335 {'K','a','l','i','n','g','a',0}},
336 {{Script_Tamil, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
337 {LANG_TAMIL, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
338 MS_MAKE_TAG('t','a','m','l'),
339 {'L','a','t','h','a',0}},
340 {{Script_Tamil_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
341 {LANG_TAMIL, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
342 MS_MAKE_TAG('t','a','m','l'),
343 {'L','a','t','h','a',0}},
344 {{Script_Telugu, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
345 {LANG_TELUGU, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
346 MS_MAKE_TAG('t','e','l','u'),
347 {'G','a','u','t','a','m','i',0}},
348 {{Script_Telugu_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
349 {LANG_TELUGU, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
350 MS_MAKE_TAG('t','e','l','u'),
351 {'G','a','u','t','a','m','i',0}},
352 {{Script_Kannada, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
353 {LANG_KANNADA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
354 MS_MAKE_TAG('k','n','d','a'),
355 {'T','u','n','g','a',0}},
356 {{Script_Kannada_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
357 {LANG_KANNADA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
358 MS_MAKE_TAG('k','n','d','a'),
359 {'T','u','n','g','a',0}},
360 {{Script_Malayalam, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
361 {LANG_MALAYALAM, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
362 MS_MAKE_TAG('m','l','y','m'),
363 {'K','a','r','t','i','k','a',0}},
364 {{Script_Malayalam_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
365 {LANG_MALAYALAM, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
366 MS_MAKE_TAG('m','l','y','m'),
367 {'K','a','r','t','i','k','a',0}},
368 {{Script_Diacritical, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
369 {LANG_ENGLISH, 0, 1, 0, 1, ANSI_CHARSET, 0, 0, 0, 0, 0, 1, 1, 0, 0},
370 0x00000000,
371 {0}},
372 {{Script_Punctuation2, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
373 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
374 MS_MAKE_TAG('l','a','t','n'),
375 {0}},
376 {{Script_Numeric2, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
377 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
378 0x00000000,
379 {0}},
382 static const SCRIPT_PROPERTIES *script_props[] =
384 &scriptInformation[0].props, &scriptInformation[1].props,
385 &scriptInformation[2].props, &scriptInformation[3].props,
386 &scriptInformation[4].props, &scriptInformation[5].props,
387 &scriptInformation[6].props, &scriptInformation[7].props,
388 &scriptInformation[8].props, &scriptInformation[9].props,
389 &scriptInformation[10].props, &scriptInformation[11].props,
390 &scriptInformation[12].props, &scriptInformation[13].props,
391 &scriptInformation[14].props, &scriptInformation[15].props,
392 &scriptInformation[16].props, &scriptInformation[17].props,
393 &scriptInformation[18].props, &scriptInformation[19].props,
394 &scriptInformation[20].props, &scriptInformation[21].props,
395 &scriptInformation[22].props, &scriptInformation[23].props,
396 &scriptInformation[24].props, &scriptInformation[25].props,
397 &scriptInformation[26].props, &scriptInformation[27].props,
398 &scriptInformation[28].props, &scriptInformation[29].props,
399 &scriptInformation[30].props, &scriptInformation[31].props,
400 &scriptInformation[32].props, &scriptInformation[33].props,
401 &scriptInformation[34].props, &scriptInformation[35].props,
402 &scriptInformation[36].props, &scriptInformation[37].props,
403 &scriptInformation[38].props, &scriptInformation[39].props,
404 &scriptInformation[40].props, &scriptInformation[41].props,
405 &scriptInformation[42].props, &scriptInformation[43].props,
406 &scriptInformation[44].props, &scriptInformation[45].props
409 typedef struct {
410 ScriptCache *sc;
411 int numGlyphs;
412 WORD* glyphs;
413 WORD* pwLogClust;
414 int* piAdvance;
415 SCRIPT_VISATTR* psva;
416 GOFFSET* pGoffset;
417 ABC* abc;
418 int iMaxPosX;
419 HFONT fallbackFont;
420 } StringGlyphs;
422 typedef struct {
423 HDC hdc;
424 DWORD dwFlags;
425 BOOL invalid;
426 int clip_len;
427 int cItems;
428 int cMaxGlyphs;
429 SCRIPT_ITEM* pItem;
430 int numItems;
431 StringGlyphs* glyphs;
432 SCRIPT_LOGATTR* logattrs;
433 SIZE* sz;
434 int* logical2visual;
435 } StringAnalysis;
437 static inline void *heap_alloc(SIZE_T size)
439 return HeapAlloc(GetProcessHeap(), 0, size);
442 static inline void *heap_alloc_zero(SIZE_T size)
444 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
447 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
449 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
452 static inline BOOL heap_free(LPVOID mem)
454 return HeapFree(GetProcessHeap(), 0, mem);
457 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
459 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
462 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
464 return ((ScriptCache *)*psc)->tm.tmHeight;
467 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
469 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
472 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
474 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
476 if (!block) return 0;
477 return block[c & GLYPH_BLOCK_MASK];
480 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
482 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
484 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
485 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
488 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
490 static const ABC nil;
491 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
493 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
494 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
495 return TRUE;
498 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
500 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
502 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
503 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
504 return TRUE;
507 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
509 ScriptCache *sc;
511 if (!psc) return E_INVALIDARG;
512 if (*psc) return S_OK;
513 if (!hdc) return E_PENDING;
515 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
516 if (!GetTextMetricsW(hdc, &sc->tm))
518 heap_free(sc);
519 return E_INVALIDARG;
521 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
523 heap_free(sc);
524 return E_INVALIDARG;
526 sc->sfnt = (GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, NULL, 0)!=GDI_ERROR);
527 *psc = sc;
528 TRACE("<- %p\n", sc);
529 return S_OK;
532 static WCHAR mirror_char( WCHAR ch )
534 extern const WCHAR wine_mirror_map[];
535 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
538 static WORD get_char_script( WCHAR ch)
540 static const WCHAR latin_punc[] = {'#','$','&','\'',',',';','<','>','?','@','\\','^','_','`','{','|','}','~', 0x00a0, 0};
541 WORD type = 0;
542 int i;
544 if (ch == 0xc || ch == 0x20 || ch == 0x202f)
545 return Script_CR;
547 /* These punctuation are separated out as Latin punctuation */
548 if (strchrW(latin_punc,ch))
549 return Script_Punctuation2;
551 /* These chars are itemized as Punctuation by Windows */
552 if (ch == 0x2212 || ch == 0x2044)
553 return Script_Punctuation;
555 GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
557 if (type == 0)
558 return SCRIPT_UNDEFINED;
560 if (type & C1_CNTRL)
561 return Script_Control;
563 i = 0;
566 if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
567 break;
569 if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
571 if (scriptRanges[i].numericScript && type & C1_DIGIT)
572 return scriptRanges[i].numericScript;
573 if (scriptRanges[i].punctScript && type & C1_PUNCT)
574 return scriptRanges[i].punctScript;
575 return scriptRanges[i].script;
577 i++;
578 } while (1);
580 return SCRIPT_UNDEFINED;
583 /***********************************************************************
584 * DllMain
587 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
589 switch(fdwReason)
591 case DLL_PROCESS_ATTACH:
592 DisableThreadLibraryCalls(hInstDLL);
593 break;
594 case DLL_PROCESS_DETACH:
595 break;
597 return TRUE;
600 /***********************************************************************
601 * ScriptFreeCache (USP10.@)
603 * Free a script cache.
605 * PARAMS
606 * psc [I/O] Script cache.
608 * RETURNS
609 * Success: S_OK
610 * Failure: Non-zero HRESULT value.
612 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
614 TRACE("%p\n", psc);
616 if (psc && *psc)
618 unsigned int i;
619 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
621 heap_free(((ScriptCache *)*psc)->glyphs[i]);
622 heap_free(((ScriptCache *)*psc)->widths[i]);
624 heap_free(((ScriptCache *)*psc)->GSUB_Table);
625 heap_free(((ScriptCache *)*psc)->GDEF_Table);
626 heap_free(((ScriptCache *)*psc)->features);
627 heap_free(*psc);
628 *psc = NULL;
630 return S_OK;
633 /***********************************************************************
634 * ScriptGetProperties (USP10.@)
636 * Retrieve a list of script properties.
638 * PARAMS
639 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
640 * num [I] Pointer to the number of scripts.
642 * RETURNS
643 * Success: S_OK
644 * Failure: Non-zero HRESULT value.
646 * NOTES
647 * Behaviour matches WinXP.
649 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
651 TRACE("(%p,%p)\n", props, num);
653 if (!props && !num) return E_INVALIDARG;
655 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
656 if (props) *props = script_props;
658 return S_OK;
661 /***********************************************************************
662 * ScriptGetFontProperties (USP10.@)
664 * Get information on special glyphs.
666 * PARAMS
667 * hdc [I] Device context.
668 * psc [I/O] Opaque pointer to a script cache.
669 * sfp [O] Font properties structure.
671 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
673 HRESULT hr;
675 TRACE("%p,%p,%p\n", hdc, psc, sfp);
677 if (!sfp) return E_INVALIDARG;
678 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
680 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
681 return E_INVALIDARG;
683 /* return something sensible? */
684 sfp->wgBlank = 0;
685 sfp->wgDefault = get_cache_default_char(psc);
686 sfp->wgInvalid = 0;
687 sfp->wgKashida = 0xffff;
688 sfp->iKashidaWidth = 0;
690 return S_OK;
693 /***********************************************************************
694 * ScriptRecordDigitSubstitution (USP10.@)
696 * Record digit substitution settings for a given locale.
698 * PARAMS
699 * locale [I] Locale identifier.
700 * sds [I] Structure to record substitution settings.
702 * RETURNS
703 * Success: S_OK
704 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
706 * SEE ALSO
707 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
709 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
711 DWORD plgid, sub;
713 TRACE("0x%x, %p\n", locale, sds);
715 /* This implementation appears to be correct for all languages, but it's
716 * not clear if sds->DigitSubstitute is ever set to anything except
717 * CONTEXT or NONE in reality */
719 if (!sds) return E_POINTER;
721 locale = ConvertDefaultLocale(locale);
723 if (!IsValidLocale(locale, LCID_INSTALLED))
724 return E_INVALIDARG;
726 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
727 sds->TraditionalDigitLanguage = plgid;
729 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
730 sds->NationalDigitLanguage = plgid;
731 else
732 sds->NationalDigitLanguage = LANG_ENGLISH;
734 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
735 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
737 switch (sub)
739 case 0:
740 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
741 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
742 else
743 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
744 break;
745 case 1:
746 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
747 break;
748 case 2:
749 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
750 break;
751 default:
752 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
753 break;
756 sds->dwReserved = 0;
757 return S_OK;
760 /***********************************************************************
761 * ScriptApplyDigitSubstitution (USP10.@)
763 * Apply digit substitution settings.
765 * PARAMS
766 * sds [I] Structure with recorded substitution settings.
767 * sc [I] Script control structure.
768 * ss [I] Script state structure.
770 * RETURNS
771 * Success: S_OK
772 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
774 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
775 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
777 SCRIPT_DIGITSUBSTITUTE psds;
779 TRACE("%p, %p, %p\n", sds, sc, ss);
781 if (!sc || !ss) return E_POINTER;
782 if (!sds)
784 sds = &psds;
785 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
786 return E_INVALIDARG;
789 sc->uDefaultLanguage = LANG_ENGLISH;
790 sc->fContextDigits = 0;
791 ss->fDigitSubstitute = 0;
793 switch (sds->DigitSubstitute) {
794 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
795 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
796 case SCRIPT_DIGITSUBSTITUTE_NONE:
797 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
798 return S_OK;
799 default:
800 return E_INVALIDARG;
804 static inline BOOL is_indic(WORD script)
806 return (script >= Script_Devanagari && script <= Script_Malayalam_Numeric);
809 static inline WORD base_indic(WORD script)
811 switch (script)
813 case Script_Devanagari:
814 case Script_Devanagari_Numeric: return Script_Devanagari;
815 case Script_Bengali:
816 case Script_Bengali_Numeric:
817 case Script_Bengali_Currency: return Script_Bengali;
818 case Script_Gurmukhi:
819 case Script_Gurmukhi_Numeric: return Script_Gurmukhi;
820 case Script_Gujarati:
821 case Script_Gujarati_Numeric:
822 case Script_Gujarati_Currency: return Script_Gujarati;
823 case Script_Oriya:
824 case Script_Oriya_Numeric: return Script_Oriya;
825 case Script_Tamil:
826 case Script_Tamil_Numeric: return Script_Tamil;
827 case Script_Telugu:
828 case Script_Telugu_Numeric: return Script_Telugu;
829 case Script_Kannada:
830 case Script_Kannada_Numeric: return Script_Kannada;
831 case Script_Malayalam:
832 case Script_Malayalam_Numeric: return Script_Malayalam;
833 default:
834 return -1;
838 /***********************************************************************
839 * ScriptItemizeOpenType (USP10.@)
841 * Split a Unicode string into shapeable parts.
843 * PARAMS
844 * pwcInChars [I] String to split.
845 * cInChars [I] Number of characters in pwcInChars.
846 * cMaxItems [I] Maximum number of items to return.
847 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
848 * psState [I] Pointer to a SCRIPT_STATE structure.
849 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
850 * pScriptTags [O] Buffer to receive OPENTYPE_TAGs.
851 * pcItems [O] Number of script items returned.
853 * RETURNS
854 * Success: S_OK
855 * Failure: Non-zero HRESULT value.
857 HRESULT WINAPI ScriptItemizeOpenType(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
858 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
859 SCRIPT_ITEM *pItems, OPENTYPE_TAG *pScriptTags, int *pcItems)
862 #define Numeric_space 0x0020
863 #define ZWNJ 0x200C
864 #define ZWJ 0x200D
866 int cnt = 0, index = 0, str = 0;
867 int New_Script = -1;
868 int i;
869 WORD *levels = NULL;
870 WORD *strength = NULL;
871 WORD *scripts = NULL;
872 WORD baselevel = 0;
873 BOOL new_run;
874 WORD last_indic = -1;
875 WORD layoutRTL = 0;
876 BOOL forceLevels = FALSE;
878 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
879 psControl, psState, pItems, pcItems);
881 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
882 return E_INVALIDARG;
884 scripts = heap_alloc(cInChars * sizeof(WORD));
885 if (!scripts)
886 return E_OUTOFMEMORY;
888 for (i = 0; i < cInChars; i++)
890 scripts[i] = get_char_script(pwcInChars[i]);
891 /* Devanagari danda (U+0964) and double danda (U+0965) are used for
892 all Indic scripts */
893 if ((pwcInChars[i] == 0x964 || pwcInChars[i] ==0x965) && last_indic > 0)
894 scripts[i] = last_indic;
895 else if (is_indic(scripts[i]))
896 last_indic = base_indic(scripts[i]);
898 /* Some unicode points (Zero Width Space U+200B -
899 Right-to-Left Mark U+200F) will force us into bidi mode */
900 if (!forceLevels && pwcInChars[i] >= 0x200B && pwcInChars[i] <= 0x200F)
901 forceLevels = TRUE;
903 /* Diacritical marks merge with other scripts */
904 if (scripts[i] == Script_Diacritical && i > 0)
905 scripts[i] = scripts[i-1];
908 for (i = 0; i < cInChars; i++)
910 /* Joiners get merged preferencially right */
911 if (i > 0 && (pwcInChars[i] == ZWJ || pwcInChars[i] == ZWNJ))
913 int j;
914 if (i+1 == cInChars)
915 scripts[i] = scripts[i-1];
916 else
918 for (j = i+1; j < cInChars; j++)
920 if (pwcInChars[j] != ZWJ && pwcInChars[j] != ZWNJ && pwcInChars[j] != Numeric_space)
922 scripts[i] = scripts[j];
923 break;
930 if (psState && psControl)
932 levels = heap_alloc_zero(cInChars * sizeof(WORD));
933 if (!levels)
935 heap_free(scripts);
936 return E_OUTOFMEMORY;
939 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
940 baselevel = levels[0];
941 for (i = 0; i < cInChars; i++)
942 if (levels[i]!=levels[0])
943 break;
944 if (i >= cInChars && !odd(baselevel) && !odd(psState->uBidiLevel) && !forceLevels)
946 heap_free(levels);
947 levels = NULL;
949 else
951 BOOL inNumber = FALSE;
952 static WCHAR math_punc[] = {'#','$','%','+',',','-','.','/',':',0x2212, 0x2044, 0x00a0,0};
954 strength = heap_alloc_zero(cInChars * sizeof(WORD));
955 if (!strength)
957 heap_free(scripts);
958 heap_free(levels);
959 return E_OUTOFMEMORY;
961 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
963 /* We currently mis-level leading Diacriticals */
964 if (scripts[0] == Script_Diacritical)
965 for (i = 0; i < cInChars && scripts[0] == Script_Diacritical; i++)
967 levels[i] = odd(levels[i])?levels[i]+1:levels[i];
968 strength[i] = BIDI_STRONG;
971 for (i = 0; i < cInChars; i++)
973 /* Script_Numeric and select puncuation at level 0 get bumped to level 2 */
974 if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && inNumber && strchrW(math_punc,pwcInChars[i]))
976 scripts[i] = Script_Numeric;
977 levels[i] = 2;
979 else if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && scripts[i] == Script_Numeric)
981 levels[i] = 2;
982 inNumber = TRUE;
984 else
985 inNumber = FALSE;
987 /* Joiners get merged preferencially right */
988 if (i > 0 && (pwcInChars[i] == ZWJ || pwcInChars[i] == ZWNJ))
990 int j;
991 if (i+1 == cInChars && levels[i-1] == levels[i])
992 strength[i] = strength[i-1];
993 else
994 for (j = i+1; j < cInChars && levels[i] == levels[j]; j++)
995 if (pwcInChars[j] != ZWJ && pwcInChars[j] != ZWNJ && pwcInChars[j] != Numeric_space)
997 strength[i] = strength[j];
998 break;
1002 if (psControl->fMergeNeutralItems)
1004 /* Merge the neutrals */
1005 for (i = 0; i < cInChars; i++)
1007 if (strength[i] == BIDI_NEUTRAL || strength[i] == BIDI_WEAK)
1009 int j;
1010 for (j = i; j > 0; j--)
1012 if (levels[i] != levels[j])
1013 break;
1014 if ((strength[j] == BIDI_STRONG) || (strength[i] == BIDI_NEUTRAL && strength[j] == BIDI_WEAK))
1016 scripts[i] = scripts[j];
1017 strength[i] = strength[j];
1018 break;
1022 /* Try going the other way */
1023 if (strength[i] == BIDI_NEUTRAL || strength[i] == BIDI_WEAK)
1025 int j;
1026 for (j = i; j < cInChars; j++)
1028 if (levels[i] != levels[j])
1029 break;
1030 if ((strength[j] == BIDI_STRONG) || (strength[i] == BIDI_NEUTRAL && strength[j] == BIDI_WEAK))
1032 scripts[i] = scripts[j];
1033 strength[i] = strength[j];
1034 break;
1043 while ((!levels || (levels && levels[cnt+1] == levels[0])) && (pwcInChars[cnt] == Numeric_space) && cnt < cInChars)
1044 cnt++;
1046 if (cnt == cInChars) /* All Spaces */
1048 cnt = 0;
1049 New_Script = scripts[cnt];
1052 pItems[index].iCharPos = 0;
1053 pItems[index].a = scriptInformation[scripts[cnt]].a;
1054 pScriptTags[index] = scriptInformation[scripts[cnt]].scriptTag;
1056 if (strength && strength[cnt] == BIDI_STRONG)
1057 str = strength[cnt];
1058 else if (strength)
1059 str = strength[0];
1061 cnt = 0;
1063 if (levels)
1065 if (strength[cnt] == BIDI_STRONG)
1066 layoutRTL = (odd(levels[cnt]))?1:0;
1067 else
1068 layoutRTL = (psState->uBidiLevel || odd(levels[cnt]))?1:0;
1069 pItems[index].a.fRTL = odd(levels[cnt]);
1070 pItems[index].a.fLayoutRTL = layoutRTL;
1071 pItems[index].a.s.uBidiLevel = levels[cnt];
1073 else if (!pItems[index].a.s.uBidiLevel)
1075 layoutRTL = (odd(baselevel))?1:0;
1076 pItems[index].a.s.uBidiLevel = baselevel;
1077 pItems[index].a.fLayoutRTL = odd(baselevel);
1078 pItems[index].a.fRTL = odd(baselevel);
1081 TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
1082 levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
1083 pItems[index].iCharPos);
1085 for (cnt=1; cnt < cInChars; cnt++)
1087 if(pwcInChars[cnt] != Numeric_space)
1088 New_Script = scripts[cnt];
1089 else if (levels)
1091 int j = 1;
1092 while (cnt + j < cInChars - 1 && pwcInChars[cnt+j] == Numeric_space && levels[cnt] == levels[cnt+j])
1093 j++;
1094 if (cnt + j < cInChars && levels[cnt] == levels[cnt+j])
1095 New_Script = scripts[cnt+j];
1096 else
1097 New_Script = scripts[cnt];
1100 new_run = FALSE;
1101 /* merge space strengths*/
1102 if (strength && strength[cnt] == BIDI_STRONG && str != BIDI_STRONG && New_Script == pItems[index].a.eScript)
1103 str = BIDI_STRONG;
1105 if (strength && strength[cnt] == BIDI_NEUTRAL && str == BIDI_STRONG && pwcInChars[cnt] != Numeric_space && New_Script == pItems[index].a.eScript)
1106 str = BIDI_NEUTRAL;
1108 /* changes in level */
1109 if (levels && (levels[cnt] != pItems[index].a.s.uBidiLevel))
1111 TRACE("Level break(%i/%i)\n",pItems[index].a.s.uBidiLevel,levels[cnt]);
1112 new_run = TRUE;
1114 /* changes in strength */
1115 else if (strength && pwcInChars[cnt] != Numeric_space && str != strength[cnt])
1117 TRACE("Strength break (%i/%i)\n",str,strength[cnt]);
1118 new_run = TRUE;
1120 /* changes in script */
1121 else if (((pwcInChars[cnt] != Numeric_space) && (New_Script != -1) && (New_Script != pItems[index].a.eScript)) || (New_Script == Script_Control))
1123 TRACE("Script break(%i/%i)\n",pItems[index].a.eScript,New_Script);
1124 new_run = TRUE;
1127 if (!new_run && strength && str == BIDI_STRONG)
1129 layoutRTL = odd(levels[cnt])?1:0;
1130 pItems[index].a.fLayoutRTL = layoutRTL;
1133 if (new_run)
1135 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);
1137 index++;
1138 if (index+1 > cMaxItems)
1139 return E_OUTOFMEMORY;
1141 if (strength)
1142 str = strength[cnt];
1144 pItems[index].iCharPos = cnt;
1145 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
1147 pItems[index].a = scriptInformation[New_Script].a;
1148 pScriptTags[index] = scriptInformation[New_Script].scriptTag;
1149 if (levels)
1151 if (levels[cnt] == 0)
1152 layoutRTL = 0;
1153 else
1154 layoutRTL = (layoutRTL || odd(levels[cnt]))?1:0;
1155 pItems[index].a.fRTL = odd(levels[cnt]);
1156 pItems[index].a.fLayoutRTL = layoutRTL;
1157 pItems[index].a.s.uBidiLevel = levels[cnt];
1159 else if (!pItems[index].a.s.uBidiLevel)
1161 pItems[index].a.s.uBidiLevel = baselevel;
1162 pItems[index].a.fLayoutRTL = layoutRTL;
1163 pItems[index].a.fRTL = odd(baselevel);
1166 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
1170 /* While not strictly necessary according to the spec, make sure the n+1
1171 * item is set up to prevent random behaviour if the caller erroneously
1172 * checks the n+1 structure */
1173 index++;
1174 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
1176 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
1178 /* Set one SCRIPT_STATE item being returned */
1179 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
1180 if (pcItems) *pcItems = index;
1182 /* Set SCRIPT_ITEM */
1183 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
1184 heap_free(levels);
1185 heap_free(strength);
1186 heap_free(scripts);
1187 return S_OK;
1190 /***********************************************************************
1191 * ScriptItemize (USP10.@)
1193 * Split a Unicode string into shapeable parts.
1195 * PARAMS
1196 * pwcInChars [I] String to split.
1197 * cInChars [I] Number of characters in pwcInChars.
1198 * cMaxItems [I] Maximum number of items to return.
1199 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
1200 * psState [I] Pointer to a SCRIPT_STATE structure.
1201 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
1202 * pcItems [O] Number of script items returned.
1204 * RETURNS
1205 * Success: S_OK
1206 * Failure: Non-zero HRESULT value.
1208 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
1209 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
1210 SCRIPT_ITEM *pItems, int *pcItems)
1212 OPENTYPE_TAG *discarded_tags;
1213 HRESULT res;
1215 discarded_tags = heap_alloc(cMaxItems * sizeof(OPENTYPE_TAG));
1216 if (!discarded_tags)
1217 return E_OUTOFMEMORY;
1218 res = ScriptItemizeOpenType(pwcInChars, cInChars, cMaxItems, psControl, psState, pItems, discarded_tags, pcItems);
1219 heap_free(discarded_tags);
1220 return res;
1223 static inline int getGivenTabWidth(ScriptCache *psc, SCRIPT_TABDEF *pTabdef, int charPos, int current_x)
1225 int defWidth;
1226 int cTabStops=0;
1227 INT *lpTabPos = NULL;
1228 INT nTabOrg = 0;
1229 INT x = 0;
1231 if (pTabdef)
1232 lpTabPos = pTabdef->pTabStops;
1234 if (pTabdef && pTabdef->iTabOrigin)
1236 if (pTabdef->iScale)
1237 nTabOrg = (pTabdef->iTabOrigin * pTabdef->iScale)/4;
1238 else
1239 nTabOrg = pTabdef->iTabOrigin * psc->tm.tmAveCharWidth;
1242 if (pTabdef)
1243 cTabStops = pTabdef->cTabStops;
1245 if (cTabStops == 1)
1247 if (pTabdef->iScale)
1248 defWidth = ((pTabdef->pTabStops[0])*pTabdef->iScale) / 4;
1249 else
1250 defWidth = (pTabdef->pTabStops[0])*psc->tm.tmAveCharWidth;
1251 cTabStops = 0;
1253 else
1254 defWidth = 8 * psc->tm.tmAveCharWidth;
1256 for (; cTabStops>0 ; lpTabPos++, cTabStops--)
1258 int position = *lpTabPos;
1259 if (position < 0)
1260 position = -1 * position;
1261 if (pTabdef->iScale)
1262 position = (position * pTabdef->iScale) / 4;
1263 else
1264 position = position * psc->tm.tmAveCharWidth;
1266 if( nTabOrg + position > current_x)
1268 if( *lpTabPos >= 0)
1270 /* a left aligned tab */
1271 x = (nTabOrg + *lpTabPos) - current_x;
1272 break;
1274 else
1276 FIXME("Negative tabstop\n");
1277 break;
1281 if ((!cTabStops) && (defWidth > 0))
1282 x =((((current_x - nTabOrg) / defWidth)+1) * defWidth) - current_x;
1283 else if ((!cTabStops) && (defWidth < 0))
1284 FIXME("TODO: Negative defWidth\n");
1286 return x;
1289 /***********************************************************************
1290 * Helper function for ScriptStringAnalyse
1292 static BOOL requires_fallback(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
1293 const WCHAR *pwcInChars, int cChars )
1295 /* FIXME: When to properly fallback is still a bit of a mystery */
1296 WORD *glyphs;
1298 if (psa->fNoGlyphIndex)
1299 return FALSE;
1301 if (init_script_cache(hdc, psc) != S_OK)
1302 return FALSE;
1304 if (SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa) != S_OK)
1305 return TRUE;
1307 glyphs = heap_alloc(sizeof(WORD) * cChars);
1308 if (!glyphs)
1309 return FALSE;
1310 if (ScriptGetCMap(hdc, psc, pwcInChars, cChars, 0, glyphs) != S_OK)
1312 heap_free(glyphs);
1313 return TRUE;
1315 heap_free(glyphs);
1317 return FALSE;
1320 static void find_fallback_font(DWORD scriptid, LPWSTR FaceName)
1322 HKEY hkey;
1324 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Uniscribe\\Fallback", &hkey))
1326 static const WCHAR szFmt[] = {'%','x',0};
1327 WCHAR value[10];
1328 DWORD count = LF_FACESIZE * sizeof(WCHAR);
1329 DWORD type;
1331 sprintfW(value, szFmt, scriptInformation[scriptid].scriptTag);
1332 if (RegQueryValueExW(hkey, value, 0, &type, (LPBYTE)FaceName, &count))
1333 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1334 RegCloseKey(hkey);
1336 else
1337 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1340 /***********************************************************************
1341 * ScriptStringAnalyse (USP10.@)
1344 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
1345 int cGlyphs, int iCharset, DWORD dwFlags,
1346 int iReqWidth, SCRIPT_CONTROL *psControl,
1347 SCRIPT_STATE *psState, const int *piDx,
1348 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
1349 SCRIPT_STRING_ANALYSIS *pssa)
1351 HRESULT hr = E_OUTOFMEMORY;
1352 StringAnalysis *analysis = NULL;
1353 SCRIPT_CONTROL sControl;
1354 SCRIPT_STATE sState;
1355 int i, num_items = 255;
1356 BYTE *BidiLevel;
1357 WCHAR *iString = NULL;
1359 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
1360 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
1361 psControl, psState, piDx, pTabdef, pbInClass, pssa);
1363 if (iCharset != -1)
1365 FIXME("Only Unicode strings are supported\n");
1366 return E_INVALIDARG;
1368 if (cString < 1 || !pString) return E_INVALIDARG;
1369 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
1371 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
1372 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
1374 /* FIXME: handle clipping */
1375 analysis->clip_len = cString;
1376 analysis->hdc = hdc;
1377 analysis->dwFlags = dwFlags;
1379 if (psState)
1380 sState = *psState;
1381 else
1382 memset(&sState, 0, sizeof(SCRIPT_STATE));
1384 if (psControl)
1385 sControl = *psControl;
1386 else
1387 memset(&sControl, 0, sizeof(SCRIPT_CONTROL));
1389 if (dwFlags & SSA_PASSWORD)
1391 iString = heap_alloc(sizeof(WCHAR)*cString);
1392 if (!iString)
1394 hr = E_OUTOFMEMORY;
1395 goto error;
1397 for (i = 0; i < cString; i++)
1398 iString[i] = *((const WCHAR *)pString);
1399 pString = iString;
1402 hr = ScriptItemize(pString, cString, num_items, &sControl, &sState, analysis->pItem,
1403 &analysis->numItems);
1405 while (hr == E_OUTOFMEMORY)
1407 SCRIPT_ITEM *tmp;
1409 num_items *= 2;
1410 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
1411 goto error;
1413 analysis->pItem = tmp;
1414 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
1415 &analysis->numItems);
1417 if (hr != S_OK) goto error;
1419 /* set back to out of memory for default goto error behaviour */
1420 hr = E_OUTOFMEMORY;
1422 if (dwFlags & SSA_BREAK)
1424 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
1426 for (i = 0; i < analysis->numItems; i++)
1427 ScriptBreak(&((LPWSTR)pString)[analysis->pItem[i].iCharPos], analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos, &analysis->pItem[i].a, &analysis->logattrs[analysis->pItem[i].iCharPos]);
1429 else
1430 goto error;
1433 if (!(analysis->logical2visual = heap_alloc_zero(sizeof(int) * analysis->numItems)))
1434 goto error;
1435 if (!(BidiLevel = heap_alloc_zero(analysis->numItems)))
1436 goto error;
1438 if (dwFlags & SSA_GLYPHS)
1440 int tab_x = 0;
1441 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
1443 heap_free(BidiLevel);
1444 goto error;
1447 for (i = 0; i < analysis->numItems; i++)
1449 SCRIPT_CACHE *sc = (SCRIPT_CACHE*)&analysis->glyphs[i].sc;
1450 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1451 int numGlyphs = 1.5 * cChar + 16;
1452 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
1453 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
1454 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
1455 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * numGlyphs);
1456 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
1457 ABC *abc = heap_alloc_zero(sizeof(ABC));
1458 int numGlyphsReturned;
1459 HFONT originalFont = 0x0;
1461 /* FIXME: non unicode strings */
1462 const WCHAR* pStr = (const WCHAR*)pString;
1463 analysis->glyphs[i].fallbackFont = NULL;
1465 if (!glyphs || !pwLogClust || !piAdvance || !psva || !pGoffset || !abc)
1467 heap_free (BidiLevel);
1468 heap_free (glyphs);
1469 heap_free (pwLogClust);
1470 heap_free (piAdvance);
1471 heap_free (psva);
1472 heap_free (pGoffset);
1473 heap_free (abc);
1474 hr = E_OUTOFMEMORY;
1475 goto error;
1478 if ((dwFlags & SSA_FALLBACK) && requires_fallback(hdc, sc, &analysis->pItem[i].a, &pStr[analysis->pItem[i].iCharPos], cChar))
1480 LOGFONTW lf;
1481 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), & lf);
1482 lf.lfCharSet = scriptInformation[analysis->pItem[i].a.eScript].props.bCharSet;
1483 find_fallback_font(analysis->pItem[i].a.eScript, lf.lfFaceName);
1484 analysis->glyphs[i].fallbackFont = CreateFontIndirectW(&lf);
1485 if (analysis->glyphs[i].fallbackFont)
1487 ScriptFreeCache(sc);
1488 originalFont = SelectObject(hdc, analysis->glyphs[i].fallbackFont);
1492 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
1493 cChar, numGlyphs, &analysis->pItem[i].a,
1494 glyphs, pwLogClust, psva, &numGlyphsReturned);
1495 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
1496 piAdvance, pGoffset, abc);
1497 if (originalFont)
1498 SelectObject(hdc,originalFont);
1500 if (dwFlags & SSA_TAB)
1502 int tabi = 0;
1503 for (tabi = 0; tabi < cChar; tabi++)
1505 if (pStr[analysis->pItem[i].iCharPos+tabi] == 0x0009)
1506 piAdvance[tabi] = getGivenTabWidth(analysis->glyphs[i].sc, pTabdef, analysis->pItem[i].iCharPos+tabi, tab_x);
1507 tab_x+=piAdvance[tabi];
1511 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
1512 analysis->glyphs[i].glyphs = glyphs;
1513 analysis->glyphs[i].pwLogClust = pwLogClust;
1514 analysis->glyphs[i].piAdvance = piAdvance;
1515 analysis->glyphs[i].psva = psva;
1516 analysis->glyphs[i].pGoffset = pGoffset;
1517 analysis->glyphs[i].abc = abc;
1518 analysis->glyphs[i].iMaxPosX= -1;
1520 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1523 else
1525 for (i = 0; i < analysis->numItems; i++)
1526 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1529 ScriptLayout(analysis->numItems, BidiLevel, NULL, analysis->logical2visual);
1530 heap_free(BidiLevel);
1532 *pssa = analysis;
1533 heap_free(iString);
1534 return S_OK;
1536 error:
1537 heap_free(iString);
1538 heap_free(analysis->glyphs);
1539 heap_free(analysis->logattrs);
1540 heap_free(analysis->pItem);
1541 heap_free(analysis->logical2visual);
1542 heap_free(analysis);
1543 return hr;
1546 static inline BOOL does_glyph_start_cluster(const SCRIPT_VISATTR *pva, const WORD *pwLogClust, int cChars, int glyph, int direction)
1548 int i;
1550 if (pva[glyph].fClusterStart)
1551 return TRUE;
1552 for (i = 0; i < cChars; i++)
1553 if (pwLogClust[i] == glyph) break;
1554 if (i != cChars)
1555 return TRUE;
1557 return FALSE;
1561 static HRESULT SS_ItemOut( SCRIPT_STRING_ANALYSIS ssa,
1562 int iX,
1563 int iY,
1564 int iItem,
1565 int cStart,
1566 int cEnd,
1567 UINT uOptions,
1568 const RECT *prc,
1569 BOOL fSelected,
1570 BOOL fDisabled)
1572 StringAnalysis *analysis;
1573 int off_x = 0;
1574 HRESULT hr;
1575 COLORREF BkColor = 0x0;
1576 COLORREF TextColor = 0x0;
1577 INT BkMode = 0;
1578 INT runStart, runEnd;
1579 INT iGlyph, cGlyphs;
1580 HFONT oldFont = 0x0;
1582 TRACE("(%p,%d,%d,%d,%d,%d, 0x%1x, %d, %d)\n",
1583 ssa, iX, iY, iItem, cStart, cEnd, uOptions, fSelected, fDisabled);
1585 if (!(analysis = ssa)) return E_INVALIDARG;
1587 if ((cStart >= 0 && analysis->pItem[iItem+1].iCharPos <= cStart) ||
1588 (cEnd >= 0 && analysis->pItem[iItem].iCharPos >= cEnd))
1589 return S_OK;
1591 if (fSelected)
1593 BkMode = GetBkMode(analysis->hdc);
1594 SetBkMode( analysis->hdc, OPAQUE);
1595 BkColor = GetBkColor(analysis->hdc);
1596 SetBkColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHT));
1597 if (!fDisabled)
1599 TextColor = GetTextColor(analysis->hdc);
1600 SetTextColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1603 if (analysis->glyphs[iItem].fallbackFont)
1604 oldFont = SelectObject(analysis->hdc, analysis->glyphs[iItem].fallbackFont);
1606 if (cStart >= 0 && analysis->pItem[iItem+1].iCharPos > cStart && analysis->pItem[iItem].iCharPos <= cStart)
1607 runStart = cStart - analysis->pItem[iItem].iCharPos;
1608 else
1609 runStart = 0;
1610 if (cEnd >= 0 && analysis->pItem[iItem+1].iCharPos > cEnd && analysis->pItem[iItem].iCharPos <= cEnd)
1611 runEnd = (cEnd-1) - analysis->pItem[iItem].iCharPos;
1612 else
1613 runEnd = (analysis->pItem[iItem+1].iCharPos - analysis->pItem[iItem].iCharPos) - 1;
1615 if (analysis->pItem[iItem].a.fRTL)
1617 if (cEnd >= 0 && cEnd < analysis->pItem[iItem+1].iCharPos)
1618 ScriptStringCPtoX(ssa, cEnd, FALSE, &off_x);
1619 else
1620 ScriptStringCPtoX(ssa, analysis->pItem[iItem+1].iCharPos-1, TRUE, &off_x);
1622 else
1624 if (cStart >=0 && runStart)
1625 ScriptStringCPtoX(ssa, cStart, FALSE, &off_x);
1626 else
1627 ScriptStringCPtoX(ssa, analysis->pItem[iItem].iCharPos, FALSE, &off_x);
1630 if (analysis->pItem[iItem].a.fRTL)
1631 iGlyph = analysis->glyphs[iItem].pwLogClust[runEnd];
1632 else
1633 iGlyph = analysis->glyphs[iItem].pwLogClust[runStart];
1635 if (analysis->pItem[iItem].a.fRTL)
1636 cGlyphs = analysis->glyphs[iItem].pwLogClust[runStart] - iGlyph;
1637 else
1638 cGlyphs = analysis->glyphs[iItem].pwLogClust[runEnd] - iGlyph;
1640 cGlyphs++;
1642 if (cEnd < 0 || scriptInformation[analysis->pItem[iItem].a.eScript].props.fNeedsCaretInfo)
1644 INT direction;
1645 INT clust_glyph;
1647 clust_glyph = iGlyph + cGlyphs;
1648 if (analysis->pItem[iItem].a.fRTL)
1649 direction = -1;
1650 else
1651 direction = 1;
1653 while(clust_glyph < analysis->glyphs[iItem].numGlyphs &&
1654 !does_glyph_start_cluster(analysis->glyphs[iItem].psva, analysis->glyphs[iItem].pwLogClust, (analysis->pItem[iItem+1].iCharPos - analysis->pItem[iItem].iCharPos), clust_glyph, direction))
1656 cGlyphs++;
1657 clust_glyph++;
1661 hr = ScriptTextOut(analysis->hdc,
1662 (SCRIPT_CACHE *)&analysis->glyphs[iItem].sc, iX + off_x,
1663 iY, uOptions, prc, &analysis->pItem[iItem].a, NULL, 0,
1664 &analysis->glyphs[iItem].glyphs[iGlyph], cGlyphs,
1665 &analysis->glyphs[iItem].piAdvance[iGlyph], NULL,
1666 &analysis->glyphs[iItem].pGoffset[iGlyph]);
1668 TRACE("ScriptTextOut hr=%08x\n", hr);
1670 if (fSelected)
1672 SetBkColor(analysis->hdc, BkColor);
1673 SetBkMode( analysis->hdc, BkMode);
1674 if (!fDisabled)
1675 SetTextColor(analysis->hdc, TextColor);
1677 if (analysis->glyphs[iItem].fallbackFont)
1678 SelectObject(analysis->hdc, oldFont);
1680 return hr;
1683 /***********************************************************************
1684 * ScriptStringOut (USP10.@)
1686 * This function takes the output of ScriptStringAnalyse and joins the segments
1687 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
1688 * only processes glyphs.
1690 * Parameters:
1691 * ssa [I] buffer to hold the analysed string components
1692 * iX [I] X axis displacement for output
1693 * iY [I] Y axis displacement for output
1694 * uOptions [I] flags controling output processing
1695 * prc [I] rectangle coordinates
1696 * iMinSel [I] starting pos for substringing output string
1697 * iMaxSel [I] ending pos for substringing output string
1698 * fDisabled [I] controls text highlighting
1700 * RETURNS
1701 * Success: S_OK
1702 * Failure: is the value returned by ScriptTextOut
1704 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
1705 int iX,
1706 int iY,
1707 UINT uOptions,
1708 const RECT *prc,
1709 int iMinSel,
1710 int iMaxSel,
1711 BOOL fDisabled)
1713 StringAnalysis *analysis;
1714 int item;
1715 HRESULT hr;
1717 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
1718 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
1720 if (!(analysis = ssa)) return E_INVALIDARG;
1721 if (!(analysis->dwFlags & SSA_GLYPHS)) return E_INVALIDARG;
1723 for (item = 0; item < analysis->numItems; item++)
1725 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], -1, -1, uOptions, prc, FALSE, fDisabled);
1726 if (FAILED(hr))
1727 return hr;
1730 if (iMinSel < iMaxSel && (iMinSel > 0 || iMaxSel > 0))
1732 if (iMaxSel > 0 && iMinSel < 0)
1733 iMinSel = 0;
1734 for (item = 0; item < analysis->numItems; item++)
1736 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], iMinSel, iMaxSel, uOptions, prc, TRUE, fDisabled);
1737 if (FAILED(hr))
1738 return hr;
1742 return S_OK;
1745 /***********************************************************************
1746 * ScriptStringCPtoX (USP10.@)
1749 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
1751 int item;
1752 int runningX = 0;
1753 StringAnalysis* analysis = ssa;
1755 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
1757 if (!ssa || !pX) return S_FALSE;
1758 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1760 /* icp out of range */
1761 if(icp < 0)
1763 analysis->invalid = TRUE;
1764 return E_INVALIDARG;
1767 for(item=0; item<analysis->numItems; item++)
1769 int CP, i;
1770 int offset;
1772 i = analysis->logical2visual[item];
1773 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1774 /* initialize max extents for uninitialized runs */
1775 if (analysis->glyphs[i].iMaxPosX == -1)
1777 if (analysis->pItem[i].a.fRTL)
1778 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1779 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1780 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1781 else
1782 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1783 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1784 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1787 if (icp >= analysis->pItem[i+1].iCharPos || icp < analysis->pItem[i].iCharPos)
1789 runningX += analysis->glyphs[i].iMaxPosX;
1790 continue;
1793 icp -= analysis->pItem[i].iCharPos;
1794 ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1795 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1796 &analysis->pItem[i].a, &offset);
1797 runningX += offset;
1799 *pX = runningX;
1800 return S_OK;
1803 /* icp out of range */
1804 analysis->invalid = TRUE;
1805 return E_INVALIDARG;
1808 /***********************************************************************
1809 * ScriptStringXtoCP (USP10.@)
1812 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
1814 StringAnalysis* analysis = ssa;
1815 int item;
1817 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1819 if (!ssa || !piCh || !piTrailing) return S_FALSE;
1820 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1822 /* out of range */
1823 if(iX < 0)
1825 if (analysis->pItem[0].a.fRTL)
1827 *piCh = 1;
1828 *piTrailing = FALSE;
1830 else
1832 *piCh = -1;
1833 *piTrailing = TRUE;
1835 return S_OK;
1838 for(item=0; item<analysis->numItems; item++)
1840 int i;
1841 int CP;
1843 for (i = 0; i < analysis->numItems && analysis->logical2visual[i] != item; i++)
1844 /* nothing */;
1846 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1847 /* initialize max extents for uninitialized runs */
1848 if (analysis->glyphs[i].iMaxPosX == -1)
1850 if (analysis->pItem[i].a.fRTL)
1851 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1852 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1853 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1854 else
1855 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1856 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1857 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1860 if (iX > analysis->glyphs[i].iMaxPosX)
1862 iX -= analysis->glyphs[i].iMaxPosX;
1863 continue;
1866 ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1867 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1868 &analysis->pItem[i].a, piCh, piTrailing);
1869 *piCh += analysis->pItem[i].iCharPos;
1871 return S_OK;
1874 /* out of range */
1875 *piCh = analysis->pItem[analysis->numItems].iCharPos;
1876 *piTrailing = FALSE;
1878 return S_OK;
1882 /***********************************************************************
1883 * ScriptStringFree (USP10.@)
1885 * Free a string analysis.
1887 * PARAMS
1888 * pssa [I] string analysis.
1890 * RETURNS
1891 * Success: S_OK
1892 * Failure: Non-zero HRESULT value.
1894 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1896 StringAnalysis* analysis;
1897 BOOL invalid;
1898 int i;
1900 TRACE("(%p)\n", pssa);
1902 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1904 invalid = analysis->invalid;
1906 if (analysis->glyphs)
1908 for (i = 0; i < analysis->numItems; i++)
1910 heap_free(analysis->glyphs[i].glyphs);
1911 heap_free(analysis->glyphs[i].pwLogClust);
1912 heap_free(analysis->glyphs[i].piAdvance);
1913 heap_free(analysis->glyphs[i].psva);
1914 heap_free(analysis->glyphs[i].pGoffset);
1915 heap_free(analysis->glyphs[i].abc);
1916 if (analysis->glyphs[i].fallbackFont)
1917 DeleteObject(analysis->glyphs[i].fallbackFont);
1918 ScriptFreeCache((SCRIPT_CACHE *)&analysis->glyphs[i].sc);
1919 heap_free(analysis->glyphs[i].sc);
1921 heap_free(analysis->glyphs);
1924 heap_free(analysis->pItem);
1925 heap_free(analysis->logattrs);
1926 heap_free(analysis->sz);
1927 heap_free(analysis->logical2visual);
1928 heap_free(analysis);
1930 if (invalid) return E_INVALIDARG;
1931 return S_OK;
1934 static inline int get_cluster_size(const WORD *pwLogClust, int cChars, int item,
1935 int direction, int* iCluster, int *check_out)
1937 int clust_size = 1;
1938 int check;
1939 WORD clust = pwLogClust[item];
1941 for (check = item+direction; check < cChars && check >= 0; check+=direction)
1943 if (pwLogClust[check] == clust)
1945 clust_size ++;
1946 if (iCluster && *iCluster == -1)
1947 *iCluster = item;
1949 else break;
1952 if (check_out)
1953 *check_out = check;
1955 return clust_size;
1958 static inline int get_glyph_cluster_advance(const int* piAdvance, const SCRIPT_VISATTR *pva, const WORD *pwLogClust, int cGlyphs, int cChars, int glyph, int direction)
1960 int advance;
1961 int log_clust_max = 0;
1962 int i;
1964 advance = piAdvance[glyph];
1966 for (i = 0; i < cChars; i++)
1968 if (pwLogClust[i] > log_clust_max)
1969 log_clust_max = pwLogClust[i];
1972 if (glyph > log_clust_max)
1973 return advance;
1975 for (glyph+=direction; glyph < cGlyphs && glyph >= 0; glyph +=direction)
1978 if (does_glyph_start_cluster(pva, pwLogClust, cChars, glyph, direction))
1979 break;
1980 if (glyph > log_clust_max)
1981 break;
1982 advance += piAdvance[glyph];
1985 return advance;
1988 /***********************************************************************
1989 * ScriptCPtoX (USP10.@)
1992 HRESULT WINAPI ScriptCPtoX(int iCP,
1993 BOOL fTrailing,
1994 int cChars,
1995 int cGlyphs,
1996 const WORD *pwLogClust,
1997 const SCRIPT_VISATTR *psva,
1998 const int *piAdvance,
1999 const SCRIPT_ANALYSIS *psa,
2000 int *piX)
2002 int item;
2003 float iPosX;
2004 int iSpecial = -1;
2005 int iCluster = -1;
2006 int clust_size = 1;
2007 float special_size = 0.0;
2008 int iMaxPos = 0;
2009 int advance = 0;
2010 BOOL rtl = FALSE;
2012 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
2013 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
2014 psa, piX);
2016 if (psa->fRTL && ! psa->fLogicalOrder)
2017 rtl = TRUE;
2019 if (fTrailing)
2020 iCP++;
2022 if (rtl)
2024 int max_clust = pwLogClust[0];
2026 for (item=0; item < cGlyphs; item++)
2027 if (pwLogClust[item] > max_clust)
2029 ERR("We do not handle non reversed clusters properly\n");
2030 break;
2033 iMaxPos = 0;
2034 for (item = max_clust; item >=0; item --)
2035 iMaxPos += piAdvance[item];
2038 iPosX = 0.0;
2039 for (item=0; item < iCP && item < cChars; item++)
2041 if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
2043 int check;
2044 int clust = pwLogClust[item];
2046 iCluster = -1;
2047 clust_size = get_cluster_size(pwLogClust, cChars, item, 1, &iCluster,
2048 &check);
2050 advance = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, clust, 1);
2052 if (check >= cChars && !iMaxPos)
2054 for (check = clust; check < cChars; check++)
2055 special_size += get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, check, 1);
2056 iSpecial = item;
2057 special_size /= (cChars - item);
2058 iPosX += special_size;
2060 else
2062 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2064 clust_size --;
2065 if (clust_size == 0)
2066 iPosX += advance;
2068 else
2069 iPosX += advance / (float)clust_size;
2072 else if (iSpecial != -1)
2073 iPosX += special_size;
2074 else /* (iCluster != -1) */
2076 int adv = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, pwLogClust[iCluster], 1);
2077 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2079 clust_size --;
2080 if (clust_size == 0)
2081 iPosX += adv;
2083 else
2084 iPosX += adv / (float)clust_size;
2088 if (iMaxPos > 0)
2090 iPosX = iMaxPos - iPosX;
2091 if (iPosX < 0)
2092 iPosX = 0;
2095 *piX = iPosX;
2096 TRACE("*piX=%d\n", *piX);
2097 return S_OK;
2100 /***********************************************************************
2101 * ScriptXtoCP (USP10.@)
2104 HRESULT WINAPI ScriptXtoCP(int iX,
2105 int cChars,
2106 int cGlyphs,
2107 const WORD *pwLogClust,
2108 const SCRIPT_VISATTR *psva,
2109 const int *piAdvance,
2110 const SCRIPT_ANALYSIS *psa,
2111 int *piCP,
2112 int *piTrailing)
2114 int item;
2115 float iPosX;
2116 float iLastPosX;
2117 int iSpecial = -1;
2118 int iCluster = -1;
2119 int clust_size = 1;
2120 int cjump = 0;
2121 int advance;
2122 float special_size = 0.0;
2123 int direction = 1;
2125 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
2126 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
2127 psa, piCP, piTrailing);
2129 if (psa->fRTL && ! psa->fLogicalOrder)
2130 direction = -1;
2132 if (direction<0)
2134 int max_clust = pwLogClust[0];
2136 if (iX < 0)
2138 *piCP = cChars;
2139 *piTrailing = 0;
2140 return S_OK;
2143 for (item=0; item < cChars; item++)
2144 if (pwLogClust[item] > max_clust)
2146 ERR("We do not handle non reversed clusters properly\n");
2147 break;
2151 if (iX < 0)
2153 *piCP = -1;
2154 *piTrailing = 1;
2155 return S_OK;
2158 iPosX = iLastPosX = 0;
2159 if (direction > 0)
2160 item = 0;
2161 else
2162 item = cChars - 1;
2163 for (; iPosX <= iX && item < cChars && item >= 0; item+=direction)
2165 iLastPosX = iPosX;
2166 if (iSpecial == -1 &&
2167 (iCluster == -1 ||
2168 (iCluster != -1 &&
2169 ((direction > 0 && iCluster+clust_size <= item) ||
2170 (direction < 0 && iCluster-clust_size >= item))
2175 int check;
2176 int clust = pwLogClust[item];
2178 iCluster = -1;
2179 cjump = 0;
2180 clust_size = get_cluster_size(pwLogClust, cChars, item, direction,
2181 &iCluster, &check);
2182 advance = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, clust, direction);
2184 if (check >= cChars && direction > 0)
2186 for (check = clust; check < cChars; check++)
2187 special_size += get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, check, direction);
2188 iSpecial = item;
2189 special_size /= (cChars - item);
2190 iPosX += special_size;
2192 else
2194 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2196 if (!cjump)
2197 iPosX += advance;
2198 cjump++;
2200 else
2201 iPosX += advance / (float)clust_size;
2204 else if (iSpecial != -1)
2205 iPosX += special_size;
2206 else /* (iCluster != -1) */
2208 int adv = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, pwLogClust[iCluster], direction);
2209 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2211 if (!cjump)
2212 iPosX += adv;
2213 cjump++;
2215 else
2216 iPosX += adv / (float)clust_size;
2220 if (direction > 0)
2222 if (iPosX > iX)
2223 item--;
2224 if (item < cChars && ((iPosX - iLastPosX) / 2.0) + iX >= iPosX)
2226 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
2227 item+=(clust_size-1);
2228 *piTrailing = 1;
2230 else
2231 *piTrailing = 0;
2233 else
2235 if (iX == iLastPosX)
2236 item++;
2237 if (iX >= iLastPosX && iX <= iPosX)
2238 item++;
2240 if (iLastPosX == iX)
2241 *piTrailing = 0;
2242 else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
2244 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
2245 item-=(clust_size-1);
2246 *piTrailing = 1;
2248 else
2249 *piTrailing = 0;
2252 *piCP = item;
2254 TRACE("*piCP=%d\n", *piCP);
2255 TRACE("*piTrailing=%d\n", *piTrailing);
2256 return S_OK;
2259 /***********************************************************************
2260 * ScriptBreak (USP10.@)
2262 * Retrieve line break information.
2264 * PARAMS
2265 * chars [I] Array of characters.
2266 * sa [I] String analysis.
2267 * la [I] Array of logical attribute structures.
2269 * RETURNS
2270 * Success: S_OK
2271 * Failure: S_FALSE
2273 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
2275 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
2277 if (!la) return S_FALSE;
2279 BREAK_line(chars, count, sa, la);
2281 return S_OK;
2284 /***********************************************************************
2285 * ScriptIsComplex (USP10.@)
2287 * Determine if a string is complex.
2289 * PARAMS
2290 * chars [I] Array of characters to test.
2291 * len [I] Length in characters.
2292 * flag [I] Flag.
2294 * RETURNS
2295 * Success: S_OK
2296 * Failure: S_FALSE
2299 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
2301 int i;
2303 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
2305 for (i = 0; i < len; i++)
2307 int script;
2309 if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
2310 return S_OK;
2312 script = get_char_script(chars[i]);
2313 if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
2314 (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
2315 return S_OK;
2317 return S_FALSE;
2320 /***********************************************************************
2321 * ScriptShapeOpenType (USP10.@)
2323 * Produce glyphs and visual attributes for a run.
2325 * PARAMS
2326 * hdc [I] Device context.
2327 * psc [I/O] Opaque pointer to a script cache.
2328 * psa [I/O] Script analysis.
2329 * tagScript [I] The OpenType tag for the Script
2330 * tagLangSys [I] The OpenType tag for the Language
2331 * rcRangeChars[I] Array of Character counts in each range
2332 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
2333 * cRanges [I] Count of ranges
2334 * pwcChars [I] Array of characters specifying the run.
2335 * cChars [I] Number of characters in pwcChars.
2336 * cMaxGlyphs [I] Length of pwOutGlyphs.
2337 * pwLogClust [O] Array of logical cluster info.
2338 * pCharProps [O] Array of character property values
2339 * pwOutGlyphs [O] Array of glyphs.
2340 * pOutGlyphProps [O] Array of attributes for the retrieved glyphs
2341 * pcGlyphs [O] Number of glyphs returned.
2343 * RETURNS
2344 * Success: S_OK
2345 * Failure: Non-zero HRESULT value.
2347 HRESULT WINAPI ScriptShapeOpenType( HDC hdc, SCRIPT_CACHE *psc,
2348 SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
2349 OPENTYPE_TAG tagLangSys, int *rcRangeChars,
2350 TEXTRANGE_PROPERTIES **rpRangeProperties,
2351 int cRanges, const WCHAR *pwcChars, int cChars,
2352 int cMaxGlyphs, WORD *pwLogClust,
2353 SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs,
2354 SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs)
2356 HRESULT hr;
2357 unsigned int i;
2358 BOOL rtl;
2360 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %d, %d, %p, %p, %p, %p, %p )\n",
2361 hdc, psc, psa,
2362 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2363 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2364 cChars, cMaxGlyphs, pwLogClust, pCharProps, pwOutGlyphs, pOutGlyphProps, pcGlyphs);
2366 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
2367 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
2369 if (!pOutGlyphProps || !pcGlyphs || !pCharProps) return E_INVALIDARG;
2370 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2372 if (cRanges)
2373 FIXME("Ranges not supported yet\n");
2375 rtl = (psa && !psa->fLogicalOrder && psa->fRTL);
2377 *pcGlyphs = cChars;
2378 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2379 if (!pwLogClust) return E_FAIL;
2381 ((ScriptCache *)*psc)->userScript = tagScript;
2382 ((ScriptCache *)*psc)->userLang = tagLangSys;
2384 /* set fNoGlyphIndex non truetype/opentype fonts */
2385 if (!psa->fNoGlyphIndex && !((ScriptCache *)*psc)->sfnt)
2386 psa->fNoGlyphIndex = TRUE;
2388 /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
2389 for (i = 0; i < cChars; i++)
2391 int idx = i;
2392 if (rtl) idx = cChars - 1 - i;
2393 /* FIXME: set to better values */
2394 pOutGlyphProps[i].sva.uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
2395 pOutGlyphProps[i].sva.fClusterStart = 1;
2396 pOutGlyphProps[i].sva.fDiacritic = 0;
2397 pOutGlyphProps[i].sva.fZeroWidth = 0;
2398 pOutGlyphProps[i].sva.fReserved = 0;
2399 pOutGlyphProps[i].sva.fShapeReserved = 0;
2401 /* FIXME: have the shaping engine set this */
2402 pCharProps[i].fCanGlyphAlone = 0;
2404 pwLogClust[i] = idx;
2407 if (psa && !psa->fNoGlyphIndex)
2409 WCHAR *rChars;
2410 if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
2412 rChars = heap_alloc(sizeof(WCHAR) * cChars);
2413 if (!rChars) return E_OUTOFMEMORY;
2414 for (i = 0; i < cChars; i++)
2416 int idx = i;
2417 WCHAR chInput;
2418 if (rtl) idx = cChars - 1 - i;
2419 if (psa->fRTL)
2420 chInput = mirror_char(pwcChars[idx]);
2421 else
2422 chInput = pwcChars[idx];
2423 /* special case for tabs */
2424 if (chInput == 0x0009)
2425 chInput = 0x0020;
2426 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
2428 WORD glyph;
2429 if (!hdc)
2431 heap_free(rChars);
2432 return E_PENDING;
2434 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR)
2436 heap_free(rChars);
2437 return S_FALSE;
2439 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
2441 rChars[i] = chInput;
2444 SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
2445 SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
2446 SHAPE_CharGlyphProp(hdc, (ScriptCache *)*psc, psa, pwcChars, cChars, pwOutGlyphs, *pcGlyphs, pwLogClust, pCharProps, pOutGlyphProps);
2447 heap_free(rChars);
2449 else
2451 TRACE("no glyph translation\n");
2452 for (i = 0; i < cChars; i++)
2454 int idx = i;
2455 /* No mirroring done here */
2456 if (rtl) idx = cChars - 1 - i;
2457 pwOutGlyphs[i] = pwcChars[idx];
2461 return S_OK;
2465 /***********************************************************************
2466 * ScriptShape (USP10.@)
2468 * Produce glyphs and visual attributes for a run.
2470 * PARAMS
2471 * hdc [I] Device context.
2472 * psc [I/O] Opaque pointer to a script cache.
2473 * pwcChars [I] Array of characters specifying the run.
2474 * cChars [I] Number of characters in pwcChars.
2475 * cMaxGlyphs [I] Length of pwOutGlyphs.
2476 * psa [I/O] Script analysis.
2477 * pwOutGlyphs [O] Array of glyphs.
2478 * pwLogClust [O] Array of logical cluster info.
2479 * psva [O] Array of visual attributes.
2480 * pcGlyphs [O] Number of glyphs returned.
2482 * RETURNS
2483 * Success: S_OK
2484 * Failure: Non-zero HRESULT value.
2486 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
2487 int cChars, int cMaxGlyphs,
2488 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
2489 SCRIPT_VISATTR *psva, int *pcGlyphs)
2491 HRESULT hr;
2492 int i;
2493 SCRIPT_CHARPROP *charProps;
2494 SCRIPT_GLYPHPROP *glyphProps;
2496 if (!psva || !pcGlyphs) return E_INVALIDARG;
2497 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2499 charProps = heap_alloc_zero(sizeof(SCRIPT_CHARPROP)*cChars);
2500 if (!charProps) return E_OUTOFMEMORY;
2501 glyphProps = heap_alloc_zero(sizeof(SCRIPT_GLYPHPROP)*cMaxGlyphs);
2502 if (!glyphProps)
2504 heap_free(charProps);
2505 return E_OUTOFMEMORY;
2508 hr = ScriptShapeOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, pwcChars, cChars, cMaxGlyphs, pwLogClust, charProps, pwOutGlyphs, glyphProps, pcGlyphs);
2510 if (SUCCEEDED(hr))
2512 for (i = 0; i < *pcGlyphs; i++)
2513 psva[i] = glyphProps[i].sva;
2516 heap_free(charProps);
2517 heap_free(glyphProps);
2519 return hr;
2522 /***********************************************************************
2523 * ScriptPlaceOpenType (USP10.@)
2525 * Produce advance widths for a run.
2527 * PARAMS
2528 * hdc [I] Device context.
2529 * psc [I/O] Opaque pointer to a script cache.
2530 * psa [I/O] String analysis.
2531 * tagScript [I] The OpenType tag for the Script
2532 * tagLangSys [I] The OpenType tag for the Language
2533 * rcRangeChars[I] Array of Character counts in each range
2534 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
2535 * cRanges [I] Count of ranges
2536 * pwcChars [I] Array of characters specifying the run.
2537 * pwLogClust [I] Array of logical cluster info
2538 * pCharProps [I] Array of character property values
2539 * cChars [I] Number of characters in pwcChars.
2540 * pwGlyphs [I] Array of glyphs.
2541 * pGlyphProps [I] Array of attributes for the retrieved glyphs
2542 * cGlyphs [I] Count of Glyphs
2543 * piAdvance [O] Array of advance widths.
2544 * pGoffset [O] Glyph offsets.
2545 * pABC [O] Combined ABC width.
2547 * RETURNS
2548 * Success: S_OK
2549 * Failure: Non-zero HRESULT value.
2552 HRESULT WINAPI ScriptPlaceOpenType( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
2553 OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys,
2554 int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties,
2555 int cRanges, const WCHAR *pwcChars, WORD *pwLogClust,
2556 SCRIPT_CHARPROP *pCharProps, int cChars,
2557 const WORD *pwGlyphs, const SCRIPT_GLYPHPROP *pGlyphProps,
2558 int cGlyphs, int *piAdvance,
2559 GOFFSET *pGoffset, ABC *pABC
2562 HRESULT hr;
2563 int i;
2565 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %p, %p, %d, %p, %p, %d, %p %p %p)\n",
2566 hdc, psc, psa,
2567 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2568 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2569 pwLogClust, pCharProps, cChars, pwGlyphs, pGlyphProps, cGlyphs, piAdvance,
2570 pGoffset, pABC);
2572 if (!pGlyphProps) return E_INVALIDARG;
2573 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2574 if (!pGoffset) return E_FAIL;
2576 if (cRanges)
2577 FIXME("Ranges not supported yet\n");
2579 ((ScriptCache *)*psc)->userScript = tagScript;
2580 ((ScriptCache *)*psc)->userLang = tagLangSys;
2582 if (pABC) memset(pABC, 0, sizeof(ABC));
2583 for (i = 0; i < cGlyphs; i++)
2585 ABC abc;
2586 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
2588 if (!hdc) return E_PENDING;
2589 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
2591 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
2593 else
2595 INT width;
2596 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
2597 abc.abcB = width;
2598 abc.abcA = abc.abcC = 0;
2600 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
2602 if (pABC)
2604 pABC->abcA += abc.abcA;
2605 pABC->abcB += abc.abcB;
2606 pABC->abcC += abc.abcC;
2608 /* FIXME: set to more reasonable values */
2609 pGoffset[i].du = pGoffset[i].dv = 0;
2610 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
2613 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
2614 return S_OK;
2617 /***********************************************************************
2618 * ScriptPlace (USP10.@)
2620 * Produce advance widths for a run.
2622 * PARAMS
2623 * hdc [I] Device context.
2624 * psc [I/O] Opaque pointer to a script cache.
2625 * pwGlyphs [I] Array of glyphs.
2626 * cGlyphs [I] Number of glyphs in pwGlyphs.
2627 * psva [I] Array of visual attributes.
2628 * psa [I/O] String analysis.
2629 * piAdvance [O] Array of advance widths.
2630 * pGoffset [O] Glyph offsets.
2631 * pABC [O] Combined ABC width.
2633 * RETURNS
2634 * Success: S_OK
2635 * Failure: Non-zero HRESULT value.
2637 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
2638 int cGlyphs, const SCRIPT_VISATTR *psva,
2639 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
2641 HRESULT hr;
2642 SCRIPT_GLYPHPROP *glyphProps;
2643 int i;
2645 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
2646 piAdvance, pGoffset, pABC);
2648 if (!psva) return E_INVALIDARG;
2649 if (!pGoffset) return E_FAIL;
2651 glyphProps = heap_alloc(sizeof(SCRIPT_GLYPHPROP)*cGlyphs);
2652 if (!glyphProps) return E_OUTOFMEMORY;
2654 for (i = 0; i < cGlyphs; i++)
2655 glyphProps[i].sva = psva[i];
2657 hr = ScriptPlaceOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, NULL, NULL, NULL, 0, pwGlyphs, glyphProps, cGlyphs, piAdvance, pGoffset, pABC);
2659 heap_free(glyphProps);
2661 return hr;
2664 /***********************************************************************
2665 * ScriptGetCMap (USP10.@)
2667 * Retrieve glyph indices.
2669 * PARAMS
2670 * hdc [I] Device context.
2671 * psc [I/O] Opaque pointer to a script cache.
2672 * pwcInChars [I] Array of Unicode characters.
2673 * cChars [I] Number of characters in pwcInChars.
2674 * dwFlags [I] Flags.
2675 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
2677 * RETURNS
2678 * Success: S_OK
2679 * Failure: Non-zero HRESULT value.
2681 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
2682 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
2684 HRESULT hr;
2685 int i;
2687 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
2688 cChars, dwFlags, pwOutGlyphs);
2690 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2692 hr = S_OK;
2694 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2696 for (i = 0; i < cChars; i++)
2698 WCHAR inChar;
2699 if (dwFlags == SGCM_RTL)
2700 inChar = mirror_char(pwcInChars[i]);
2701 else
2702 inChar = pwcInChars[i];
2703 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
2705 WORD glyph;
2706 if (!hdc) return E_PENDING;
2707 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
2708 if (glyph == 0xffff)
2710 hr = S_FALSE;
2711 glyph = 0x0;
2713 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
2717 else
2719 TRACE("no glyph translation\n");
2720 for (i = 0; i < cChars; i++)
2722 WCHAR inChar;
2723 if (dwFlags == SGCM_RTL)
2724 inChar = mirror_char(pwcInChars[i]);
2725 else
2726 inChar = pwcInChars[i];
2727 pwOutGlyphs[i] = inChar;
2730 return hr;
2733 /***********************************************************************
2734 * ScriptTextOut (USP10.@)
2737 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
2738 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
2739 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
2740 const int *piJustify, const GOFFSET *pGoffset)
2742 HRESULT hr = S_OK;
2744 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
2745 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
2746 piAdvance, piJustify, pGoffset);
2748 if (!hdc || !psc) return E_INVALIDARG;
2749 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
2751 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
2752 fuOptions |= ETO_IGNORELANGUAGE;
2753 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
2754 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
2756 if (psa->fRTL && psa->fLogicalOrder)
2758 int i;
2759 WORD *rtlGlyphs;
2761 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
2762 if (!rtlGlyphs)
2763 return E_OUTOFMEMORY;
2765 for (i = 0; i < cGlyphs; i++)
2766 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
2768 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
2769 hr = S_FALSE;
2770 heap_free(rtlGlyphs);
2772 else
2773 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
2774 hr = S_FALSE;
2776 return hr;
2779 /***********************************************************************
2780 * ScriptCacheGetHeight (USP10.@)
2782 * Retrieve the height of the font in the cache.
2784 * PARAMS
2785 * hdc [I] Device context.
2786 * psc [I/O] Opaque pointer to a script cache.
2787 * height [O] Receives font height.
2789 * RETURNS
2790 * Success: S_OK
2791 * Failure: Non-zero HRESULT value.
2793 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
2795 HRESULT hr;
2797 TRACE("(%p, %p, %p)\n", hdc, psc, height);
2799 if (!height) return E_INVALIDARG;
2800 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2802 *height = get_cache_height(psc);
2803 return S_OK;
2806 /***********************************************************************
2807 * ScriptGetGlyphABCWidth (USP10.@)
2809 * Retrieve the width of a glyph.
2811 * PARAMS
2812 * hdc [I] Device context.
2813 * psc [I/O] Opaque pointer to a script cache.
2814 * glyph [I] Glyph to retrieve the width for.
2815 * abc [O] ABC widths of the glyph.
2817 * RETURNS
2818 * Success: S_OK
2819 * Failure: Non-zero HRESULT value.
2821 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
2823 HRESULT hr;
2825 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
2827 if (!abc) return E_INVALIDARG;
2828 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2830 if (!get_cache_glyph_widths(psc, glyph, abc))
2832 if (!hdc) return E_PENDING;
2833 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2835 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
2837 else
2839 INT width;
2840 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
2841 abc->abcB = width;
2842 abc->abcA = abc->abcC = 0;
2844 set_cache_glyph_widths(psc, glyph, abc);
2846 return S_OK;
2849 /***********************************************************************
2850 * ScriptLayout (USP10.@)
2852 * Map embedding levels to visual and/or logical order.
2854 * PARAMS
2855 * runs [I] Size of level array.
2856 * level [I] Array of embedding levels.
2857 * vistolog [O] Map of embedding levels from visual to logical order.
2858 * logtovis [O] Map of embedding levels from logical to visual order.
2860 * RETURNS
2861 * Success: S_OK
2862 * Failure: Non-zero HRESULT value.
2864 * BUGS
2865 * This stub works correctly for any sequence of a single
2866 * embedding level but not for sequences of different
2867 * embedding levels, i.e. mixtures of RTL and LTR scripts.
2869 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
2871 int* indexs;
2872 int ich;
2874 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
2876 if (!level || (!vistolog && !logtovis))
2877 return E_INVALIDARG;
2879 indexs = heap_alloc(sizeof(int) * runs);
2880 if (!indexs)
2881 return E_OUTOFMEMORY;
2884 if (vistolog)
2886 for( ich = 0; ich < runs; ich++)
2887 indexs[ich] = ich;
2889 ich = 0;
2890 while (ich < runs)
2891 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2892 for (ich = 0; ich < runs; ich++)
2893 vistolog[ich] = indexs[ich];
2897 if (logtovis)
2899 for( ich = 0; ich < runs; ich++)
2900 indexs[ich] = ich;
2902 ich = 0;
2903 while (ich < runs)
2904 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2905 for (ich = 0; ich < runs; ich++)
2906 logtovis[ich] = indexs[ich];
2908 heap_free(indexs);
2910 return S_OK;
2913 /***********************************************************************
2914 * ScriptStringGetLogicalWidths (USP10.@)
2916 * Returns logical widths from a string analysis.
2918 * PARAMS
2919 * ssa [I] string analysis.
2920 * piDx [O] logical widths returned.
2922 * RETURNS
2923 * Success: S_OK
2924 * Failure: a non-zero HRESULT.
2926 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
2928 int i, j, next = 0;
2929 StringAnalysis *analysis = ssa;
2931 TRACE("%p, %p\n", ssa, piDx);
2933 if (!analysis) return S_FALSE;
2934 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
2936 for (i = 0; i < analysis->numItems; i++)
2938 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
2939 int direction = 1;
2941 if (analysis->pItem[i].a.fRTL && ! analysis->pItem[i].a.fLogicalOrder)
2942 direction = -1;
2944 for (j = 0; j < cChar; j++)
2946 int k;
2947 int glyph = analysis->glyphs[i].pwLogClust[j];
2948 int clust_size = get_cluster_size(analysis->glyphs[i].pwLogClust,
2949 cChar, j, direction, NULL, NULL);
2950 int advance = get_glyph_cluster_advance(analysis->glyphs[i].piAdvance, analysis->glyphs[i].psva, analysis->glyphs[i].pwLogClust, analysis->glyphs[i].numGlyphs, cChar, glyph, direction);
2952 for (k = 0; k < clust_size; k++)
2954 piDx[next] = advance / clust_size;
2955 next++;
2956 if (k) j++;
2960 return S_OK;
2963 /***********************************************************************
2964 * ScriptStringValidate (USP10.@)
2966 * Validate a string analysis.
2968 * PARAMS
2969 * ssa [I] string analysis.
2971 * RETURNS
2972 * Success: S_OK
2973 * Failure: S_FALSE if invalid sequences are found
2974 * or a non-zero HRESULT if it fails.
2976 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
2978 StringAnalysis *analysis = ssa;
2980 TRACE("(%p)\n", ssa);
2982 if (!analysis) return E_INVALIDARG;
2983 return (analysis->invalid) ? S_FALSE : S_OK;
2986 /***********************************************************************
2987 * ScriptString_pSize (USP10.@)
2989 * Retrieve width and height of an analysed string.
2991 * PARAMS
2992 * ssa [I] string analysis.
2994 * RETURNS
2995 * Success: Pointer to a SIZE structure.
2996 * Failure: NULL
2998 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
3000 int i, j;
3001 StringAnalysis *analysis = ssa;
3003 TRACE("(%p)\n", ssa);
3005 if (!analysis) return NULL;
3006 if (!(analysis->dwFlags & SSA_GLYPHS)) return NULL;
3008 if (!analysis->sz)
3010 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
3011 analysis->sz->cy = analysis->glyphs[0].sc->tm.tmHeight;
3013 analysis->sz->cx = 0;
3014 for (i = 0; i < analysis->numItems; i++)
3016 if (analysis->glyphs[i].sc->tm.tmHeight > analysis->sz->cy)
3017 analysis->sz->cy = analysis->glyphs[i].sc->tm.tmHeight;
3018 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
3019 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
3022 return analysis->sz;
3025 /***********************************************************************
3026 * ScriptString_pLogAttr (USP10.@)
3028 * Retrieve logical attributes of an analysed string.
3030 * PARAMS
3031 * ssa [I] string analysis.
3033 * RETURNS
3034 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
3035 * Failure: NULL
3037 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
3039 StringAnalysis *analysis = ssa;
3041 TRACE("(%p)\n", ssa);
3043 if (!analysis) return NULL;
3044 if (!(analysis->dwFlags & SSA_BREAK)) return NULL;
3045 return analysis->logattrs;
3048 /***********************************************************************
3049 * ScriptString_pcOutChars (USP10.@)
3051 * Retrieve the length of a string after clipping.
3053 * PARAMS
3054 * ssa [I] String analysis.
3056 * RETURNS
3057 * Success: Pointer to the length.
3058 * Failure: NULL
3060 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
3062 StringAnalysis *analysis = ssa;
3064 TRACE("(%p)\n", ssa);
3066 if (!analysis) return NULL;
3067 return &analysis->clip_len;
3070 /***********************************************************************
3071 * ScriptStringGetOrder (USP10.@)
3073 * Retrieve a glyph order map.
3075 * PARAMS
3076 * ssa [I] String analysis.
3077 * order [I/O] Array of glyph positions.
3079 * RETURNS
3080 * Success: S_OK
3081 * Failure: a non-zero HRESULT.
3083 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
3085 int i, j;
3086 unsigned int k;
3087 StringAnalysis *analysis = ssa;
3089 TRACE("(%p)\n", ssa);
3091 if (!analysis) return S_FALSE;
3092 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
3094 /* FIXME: handle RTL scripts */
3095 for (i = 0, k = 0; i < analysis->numItems; i++)
3096 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
3097 order[k] = k;
3099 return S_OK;
3102 /***********************************************************************
3103 * ScriptGetLogicalWidths (USP10.@)
3105 * Convert advance widths to logical widths.
3107 * PARAMS
3108 * sa [I] Script analysis.
3109 * nbchars [I] Number of characters.
3110 * nbglyphs [I] Number of glyphs.
3111 * glyph_width [I] Array of glyph widths.
3112 * log_clust [I] Array of logical clusters.
3113 * sva [I] Visual attributes.
3114 * widths [O] Array of logical widths.
3116 * RETURNS
3117 * Success: S_OK
3118 * Failure: a non-zero HRESULT.
3120 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
3121 const int *glyph_width, const WORD *log_clust,
3122 const SCRIPT_VISATTR *sva, int *widths)
3124 int i;
3126 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
3127 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
3129 /* FIXME */
3130 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
3131 return S_OK;
3134 /***********************************************************************
3135 * ScriptApplyLogicalWidth (USP10.@)
3137 * Generate glyph advance widths.
3139 * PARAMS
3140 * dx [I] Array of logical advance widths.
3141 * num_chars [I] Number of characters.
3142 * num_glyphs [I] Number of glyphs.
3143 * log_clust [I] Array of logical clusters.
3144 * sva [I] Visual attributes.
3145 * advance [I] Array of glyph advance widths.
3146 * sa [I] Script analysis.
3147 * abc [I/O] Summed ABC widths.
3148 * justify [O] Array of glyph advance widths.
3150 * RETURNS
3151 * Success: S_OK
3152 * Failure: a non-zero HRESULT.
3154 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
3155 const WORD *log_clust, const SCRIPT_VISATTR *sva,
3156 const int *advance, const SCRIPT_ANALYSIS *sa,
3157 ABC *abc, int *justify)
3159 int i;
3161 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
3162 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
3164 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
3165 return S_OK;
3168 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
3169 int num_glyphs, int dx, int min_kashida, int *justify)
3171 int i;
3173 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
3175 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
3176 return S_OK;