usp10: Break out get_cluster_size and use it to correct ScriptStringGetLogicalWidths...
[wine/multimedia.git] / dlls / usp10 / usp10.c
blob941b918fa45e999049487e07df7d31df28bd2a7e
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 /* Latin-1 Supplement: U+0080–U+00FF */
56 /* Latin Extended-A: U+0100–U+017F */
57 /* Latin Extended-B: U+0180–U+024F */
58 /* IPA Extensions: U+0250–U+02AF */
59 { Script_Latin, 0x00, 0x2af , Script_Numeric, Script_Punctuation},
60 /* Greek: U+0370–U+03FF */
61 { Script_Greek, 0x370, 0x3ff, 0, 0},
62 /* Cyrillic: U+0400–U+04FF */
63 /* Cyrillic Supplement: U+0500–U+052F */
64 { Script_Cyrillic, 0x400, 0x52f, 0, 0},
65 /* Armenian: U+0530–U+058F */
66 { Script_Armenian, 0x530, 0x58f, 0, 0},
67 /* Hebrew: U+0590–U+05FF */
68 { Script_Hebrew, 0x590, 0x5ff, 0, 0},
69 /* Arabic: U+0600–U+06FF */
70 { Script_Arabic, 0x600, 0x6ef, Script_Arabic_Numeric, 0},
71 /* Defined by Windows */
72 { Script_Persian, 0x6f0, 0x6f9, 0, 0},
73 /* Continue Arabic: U+0600–U+06FF */
74 { Script_Arabic, 0x6fa, 0x6ff, 0, 0},
75 /* Syriac: U+0700–U+074F*/
76 { Script_Syriac, 0x700, 0x74f, 0, 0},
77 /* Arabic Supplement: U+0750–U+077F */
78 { Script_Arabic, 0x750, 0x77f, 0, 0},
79 /* Thaana: U+0780–U+07BF */
80 { Script_Thaana, 0x780, 0x7bf, 0, 0},
81 /* Devanagari: U+0900–U+097F */
82 { Script_Devanagari, 0x900, 0x97f, Script_Devanagari_Numeric, 0},
83 /* Bengali: U+0980–U+09FF */
84 { Script_Bengali, 0x980, 0x9ff, Script_Bengali_Numeric, 0},
85 /* Gurmukhi: U+0A00–U+0A7F*/
86 { Script_Gurmukhi, 0xa00, 0xa7f, Script_Gurmukhi_Numeric, 0},
87 /* Gujarati: U+0A80–U+0AFF*/
88 { Script_Gujarati, 0xa80, 0xaff, Script_Gujarati_Numeric, 0},
89 /* Oriya: U+0B00–U+0B7F */
90 { Script_Oriya, 0xb00, 0xb7f, Script_Oriya_Numeric, 0},
91 /* Tamil: U+0B80–U+0BFF */
92 { Script_Tamil, 0xb80, 0xbff, Script_Tamil_Numeric, 0},
93 /* Telugu: U+0C00–U+0C7F */
94 { Script_Telugu, 0xc00, 0xc7f, Script_Telugu_Numeric, 0},
95 /* Kannada: U+0C80–U+0CFF */
96 { Script_Kannada, 0xc80, 0xcff, Script_Kannada_Numeric, 0},
97 /* Malayalam: U+0D00–U+0D7F */
98 { Script_Malayalam, 0xd00, 0xd7f, Script_Malayalam_Numeric, 0},
99 /* Sinhala: U+0D80–U+0DFF */
100 { Script_Sinhala, 0xd80, 0xdff, 0, 0},
101 /* Thai: U+0E00–U+0E7F */
102 { Script_Thai, 0xe00, 0xe7f, Script_Thai_Numeric, 0},
103 /* Lao: U+0E80–U+0EFF */
104 { Script_Lao, 0xe80, 0xeff, Script_Lao_Numeric, 0},
105 /* Tibetan: U+0F00–U+0FFF */
106 { Script_Tibetan, 0xf00, 0xfff, Script_Tibetan_Numeric, 0},
107 /* Georgian: U+10A0–U+10FF */
108 { Script_Georgian, 0x10a0, 0x10ff, 0, 0},
109 /* Vedic Extensions: U+1CD0-U+1CFF */
110 { Script_Devanagari, 0x1cd0, 0x1cff, Script_Devanagari_Numeric, 0},
111 /* Phonetic Extensions: U+1D00–U+1DBF */
112 { Script_Latin, 0x1d00, 0x1dbf, 0, 0},
113 /* Latin Extended Additional: U+1E00–U+1EFF */
114 { Script_Latin, 0x1e00, 0x1eff, 0, 0},
115 /* Greek Extended: U+1F00–U+1FFF */
116 { Script_Greek, 0x1f00, 0x1fff, 0, 0},
117 /* Latin Extended-C: U+2C60–U+2C7F */
118 { Script_Latin, 0x2c60, 0x2c7f, 0, 0},
119 /* Georgian: U+2D00–U+2D2F */
120 { Script_Georgian, 0x2d00, 0x2d2f, 0, 0},
121 /* Cyrillic Extended-A: U+2DE0–U+2DFF */
122 { Script_Cyrillic, 0x2de0, 0x2dff, 0, 0},
123 /* Cyrillic Extended-B: U+A640–U+A69F */
124 { Script_Cyrillic, 0xa640, 0xa69f, 0, 0},
125 /* Modifier Tone Letters: U+A700–U+A71F */
126 /* Latin Extended-D: U+A720–U+A7FF */
127 { Script_Latin, 0xa700, 0xa7ff, 0, 0},
128 /* Phags-pa: U+A840–U+A87F */
129 { Script_Phags_pa, 0xa840, 0xa87f, 0, 0},
130 /* Devanagari Extended: U+A8E0-U+A8FF */
131 { Script_Devanagari, 0xa8e0, 0xa8ff, Script_Devanagari_Numeric, 0},
132 /* Latin Ligatures: U+FB00–U+FB06 */
133 { Script_Latin, 0xfb00, 0xfb06, 0, 0},
134 /* Armenian ligatures U+FB13..U+FB17 */
135 { Script_Armenian, 0xfb13, 0xfb17, 0, 0},
136 /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
137 { Script_Hebrew, 0xfb1d, 0xfb4f, 0, 0},
138 /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
139 { Script_Arabic, 0xfb50, 0xfdff, 0, 0},
140 /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
141 { Script_Arabic, 0xfe70, 0xfeff, 0, 0},
142 /* END */
143 { SCRIPT_UNDEFINED, 0, 0, 0}
146 typedef struct _scriptData
148 SCRIPT_ANALYSIS a;
149 SCRIPT_PROPERTIES props;
150 OPENTYPE_TAG scriptTag;
151 WCHAR fallbackFont[LF_FACESIZE];
152 } scriptData;
154 /* the must be in order so that the index matches the Script value */
155 static const scriptData scriptInformation[] = {
156 {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
157 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
158 0x00000000,
159 {0}},
160 {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
161 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
162 MS_MAKE_TAG('l','a','t','n'),
163 {0}},
164 {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
165 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
166 0x00000000,
167 {0}},
168 {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
169 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
170 0x00000000,
171 {0}},
172 {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
173 {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0},
174 0x00000000,
175 {0}},
176 {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
177 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
178 0x00000000,
179 {0}},
180 {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
181 {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0},
182 MS_MAKE_TAG('a','r','a','b'),
183 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
184 {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
185 {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
186 MS_MAKE_TAG('a','r','a','b'),
187 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
188 {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
189 {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
190 MS_MAKE_TAG('h','e','b','r'),
191 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
192 {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
193 {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0},
194 MS_MAKE_TAG('s','y','r','c'),
195 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
196 {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
197 {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
198 MS_MAKE_TAG('s','y','r','c'),
199 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
200 {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
201 {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
202 MS_MAKE_TAG('t','h','a','a'),
203 {'M','V',' ','B','o','l','i',0}},
204 {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
205 {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
206 MS_MAKE_TAG('g','r','e','k'),
207 {0}},
208 {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
209 {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
210 MS_MAKE_TAG('c','y','r','l'),
211 {0}},
212 {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
213 {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
214 MS_MAKE_TAG('a','r','m','n'),
215 {'S','y','l','f','a','e','n',0}},
216 {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
217 {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
218 MS_MAKE_TAG('g','e','o','r'),
219 {'S','y','l','f','a','e','n',0}},
220 {{Script_Sinhala, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
221 {LANG_SINHALESE, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
222 MS_MAKE_TAG('s','i','n','h'),
223 {'I','s','k','o','o','l','a',' ','P','o','t','a',0}},
224 {{Script_Tibetan, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
225 {LANG_TIBETAN, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
226 MS_MAKE_TAG('t','i','b','t'),
227 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
228 {{Script_Tibetan_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
229 {LANG_TIBETAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
230 MS_MAKE_TAG('t','i','b','t'),
231 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
232 {{Script_Phags_pa, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
233 {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
234 MS_MAKE_TAG('p','h','a','g'),
235 {'M','i','c','r','o','s','o','f','t',' ','P','h','a','g','s','P','a',0}},
236 {{Script_Thai, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
237 {LANG_THAI, 0, 1, 1, 1, THAI_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 1},
238 MS_MAKE_TAG('t','h','a','i'),
239 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
240 {{Script_Thai_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
241 {LANG_THAI, 1, 1, 0, 0, THAI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
242 MS_MAKE_TAG('t','h','a','i'),
243 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
244 {{Script_Lao, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
245 {LANG_LAO, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
246 MS_MAKE_TAG('l','a','o',' '),
247 {'D','o','k','C','h','a','m','p','a',0}},
248 {{Script_Lao_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
249 {LANG_LAO, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
250 MS_MAKE_TAG('l','a','o',' '),
251 {'D','o','k','C','h','a','m','p','a',0}},
252 {{Script_Devanagari, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
253 {LANG_HINDI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
254 MS_MAKE_TAG('d','e','v','a'),
255 {'M','a','n','g','a','l',0}},
256 {{Script_Devanagari_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
257 {LANG_HINDI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
258 MS_MAKE_TAG('d','e','v','a'),
259 {'M','a','n','g','a','l',0}},
260 {{Script_Bengali, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
261 {LANG_BENGALI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
262 MS_MAKE_TAG('b','e','n','g'),
263 {'V','r','i','n','d','a',0}},
264 {{Script_Bengali_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
265 {LANG_BENGALI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
266 MS_MAKE_TAG('b','e','n','g'),
267 {'V','r','i','n','d','a',0}},
268 {{Script_Bengali_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
269 {LANG_BENGALI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
270 MS_MAKE_TAG('b','e','n','g'),
271 {'V','r','i','n','d','a',0}},
272 {{Script_Gurmukhi, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
273 {LANG_PUNJABI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
274 MS_MAKE_TAG('g','u','r','u'),
275 {'R','a','a','v','i',0}},
276 {{Script_Gurmukhi_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
277 {LANG_PUNJABI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
278 MS_MAKE_TAG('g','u','r','u'),
279 {'R','a','a','v','i',0}},
280 {{Script_Gujarati, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
281 {LANG_GUJARATI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
282 MS_MAKE_TAG('g','u','j','r'),
283 {'S','h','r','u','t','i',0}},
284 {{Script_Gujarati_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
285 {LANG_GUJARATI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
286 MS_MAKE_TAG('g','u','j','r'),
287 {'S','h','r','u','t','i',0}},
288 {{Script_Gujarati_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
289 {LANG_GUJARATI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
290 MS_MAKE_TAG('g','u','j','r'),
291 {'S','h','r','u','t','i',0}},
292 {{Script_Oriya, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
293 {LANG_ORIYA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
294 MS_MAKE_TAG('o','r','y','a'),
295 {'K','a','l','i','n','g','a',0}},
296 {{Script_Oriya_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
297 {LANG_ORIYA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
298 MS_MAKE_TAG('o','r','y','a'),
299 {'K','a','l','i','n','g','a',0}},
300 {{Script_Tamil, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
301 {LANG_TAMIL, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
302 MS_MAKE_TAG('t','a','m','l'),
303 {'L','a','t','h','a',0}},
304 {{Script_Tamil_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
305 {LANG_TAMIL, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
306 MS_MAKE_TAG('t','a','m','l'),
307 {'L','a','t','h','a',0}},
308 {{Script_Telugu, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
309 {LANG_TELUGU, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
310 MS_MAKE_TAG('t','e','l','u'),
311 {'G','a','u','t','a','m','i',0}},
312 {{Script_Telugu_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
313 {LANG_TELUGU, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
314 MS_MAKE_TAG('t','e','l','u'),
315 {'G','a','u','t','a','m','i',0}},
316 {{Script_Kannada, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
317 {LANG_KANNADA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
318 MS_MAKE_TAG('k','n','d','a'),
319 {'T','u','n','g','a',0}},
320 {{Script_Kannada_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
321 {LANG_KANNADA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
322 MS_MAKE_TAG('k','n','d','a'),
323 {'T','u','n','g','a',0}},
324 {{Script_Malayalam, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
325 {LANG_MALAYALAM, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
326 MS_MAKE_TAG('m','l','y','m'),
327 {'K','a','r','t','i','k','a',0}},
328 {{Script_Malayalam_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
329 {LANG_MALAYALAM, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
330 MS_MAKE_TAG('m','l','y','m'),
331 {'K','a','r','t','i','k','a',0}},
334 static const SCRIPT_PROPERTIES *script_props[] =
336 &scriptInformation[0].props, &scriptInformation[1].props,
337 &scriptInformation[2].props, &scriptInformation[3].props,
338 &scriptInformation[4].props, &scriptInformation[5].props,
339 &scriptInformation[6].props, &scriptInformation[7].props,
340 &scriptInformation[8].props, &scriptInformation[9].props,
341 &scriptInformation[10].props, &scriptInformation[11].props,
342 &scriptInformation[12].props, &scriptInformation[13].props,
343 &scriptInformation[14].props, &scriptInformation[15].props,
344 &scriptInformation[16].props, &scriptInformation[17].props,
345 &scriptInformation[18].props, &scriptInformation[19].props,
346 &scriptInformation[20].props, &scriptInformation[21].props,
347 &scriptInformation[22].props, &scriptInformation[23].props,
348 &scriptInformation[24].props, &scriptInformation[25].props,
349 &scriptInformation[26].props, &scriptInformation[27].props,
350 &scriptInformation[28].props, &scriptInformation[29].props,
351 &scriptInformation[30].props, &scriptInformation[31].props,
352 &scriptInformation[32].props, &scriptInformation[33].props,
353 &scriptInformation[34].props, &scriptInformation[35].props,
354 &scriptInformation[36].props, &scriptInformation[37].props,
355 &scriptInformation[38].props, &scriptInformation[39].props,
356 &scriptInformation[40].props, &scriptInformation[41].props,
357 &scriptInformation[42].props, &scriptInformation[43].props
360 typedef struct {
361 ScriptCache *sc;
362 int numGlyphs;
363 WORD* glyphs;
364 WORD* pwLogClust;
365 int* piAdvance;
366 SCRIPT_VISATTR* psva;
367 GOFFSET* pGoffset;
368 ABC* abc;
369 int iMaxPosX;
370 HFONT fallbackFont;
371 } StringGlyphs;
373 typedef struct {
374 HDC hdc;
375 DWORD dwFlags;
376 BOOL invalid;
377 int clip_len;
378 int cItems;
379 int cMaxGlyphs;
380 SCRIPT_ITEM* pItem;
381 int numItems;
382 StringGlyphs* glyphs;
383 SCRIPT_LOGATTR* logattrs;
384 SIZE* sz;
385 int* logical2visual;
386 } StringAnalysis;
388 static inline void *heap_alloc(SIZE_T size)
390 return HeapAlloc(GetProcessHeap(), 0, size);
393 static inline void *heap_alloc_zero(SIZE_T size)
395 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
398 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
400 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
403 static inline BOOL heap_free(LPVOID mem)
405 return HeapFree(GetProcessHeap(), 0, mem);
408 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
410 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
413 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
415 return ((ScriptCache *)*psc)->tm.tmHeight;
418 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
420 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
423 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
425 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
427 if (!block) return 0;
428 return block[c & GLYPH_BLOCK_MASK];
431 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
433 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
435 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
436 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
439 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
441 static const ABC nil;
442 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
444 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
445 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
446 return TRUE;
449 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
451 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
453 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
454 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
455 return TRUE;
458 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
460 ScriptCache *sc;
462 if (!psc) return E_INVALIDARG;
463 if (*psc) return S_OK;
464 if (!hdc) return E_PENDING;
466 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
467 if (!GetTextMetricsW(hdc, &sc->tm))
469 heap_free(sc);
470 return E_INVALIDARG;
472 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
474 heap_free(sc);
475 return E_INVALIDARG;
477 sc->sfnt = (GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, NULL, 0)!=GDI_ERROR);
478 *psc = sc;
479 TRACE("<- %p\n", sc);
480 return S_OK;
483 static WCHAR mirror_char( WCHAR ch )
485 extern const WCHAR wine_mirror_map[];
486 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
489 static WORD get_char_script( WCHAR ch)
491 WORD type = 0;
492 int i;
494 if (ch == 0xc || ch == 0x20 || ch == 0x202f)
495 return Script_CR;
497 GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
499 if (type == 0)
500 return SCRIPT_UNDEFINED;
502 if (type & C1_CNTRL)
503 return Script_Control;
505 i = 0;
508 if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
509 break;
511 if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
513 if (scriptRanges[i].numericScript && type & C1_DIGIT)
514 return scriptRanges[i].numericScript;
515 if (scriptRanges[i].punctScript && type & C1_PUNCT)
516 return scriptRanges[i].punctScript;
517 return scriptRanges[i].script;
519 i++;
520 } while (1);
522 return SCRIPT_UNDEFINED;
525 /***********************************************************************
526 * DllMain
529 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
531 switch(fdwReason)
533 case DLL_PROCESS_ATTACH:
534 DisableThreadLibraryCalls(hInstDLL);
535 break;
536 case DLL_PROCESS_DETACH:
537 break;
539 return TRUE;
542 /***********************************************************************
543 * ScriptFreeCache (USP10.@)
545 * Free a script cache.
547 * PARAMS
548 * psc [I/O] Script cache.
550 * RETURNS
551 * Success: S_OK
552 * Failure: Non-zero HRESULT value.
554 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
556 TRACE("%p\n", psc);
558 if (psc && *psc)
560 unsigned int i;
561 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
563 heap_free(((ScriptCache *)*psc)->glyphs[i]);
564 heap_free(((ScriptCache *)*psc)->widths[i]);
566 heap_free(((ScriptCache *)*psc)->GSUB_Table);
567 heap_free(((ScriptCache *)*psc)->GDEF_Table);
568 heap_free(((ScriptCache *)*psc)->features);
569 heap_free(*psc);
570 *psc = NULL;
572 return S_OK;
575 /***********************************************************************
576 * ScriptGetProperties (USP10.@)
578 * Retrieve a list of script properties.
580 * PARAMS
581 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
582 * num [I] Pointer to the number of scripts.
584 * RETURNS
585 * Success: S_OK
586 * Failure: Non-zero HRESULT value.
588 * NOTES
589 * Behaviour matches WinXP.
591 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
593 TRACE("(%p,%p)\n", props, num);
595 if (!props && !num) return E_INVALIDARG;
597 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
598 if (props) *props = script_props;
600 return S_OK;
603 /***********************************************************************
604 * ScriptGetFontProperties (USP10.@)
606 * Get information on special glyphs.
608 * PARAMS
609 * hdc [I] Device context.
610 * psc [I/O] Opaque pointer to a script cache.
611 * sfp [O] Font properties structure.
613 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
615 HRESULT hr;
617 TRACE("%p,%p,%p\n", hdc, psc, sfp);
619 if (!sfp) return E_INVALIDARG;
620 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
622 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
623 return E_INVALIDARG;
625 /* return something sensible? */
626 sfp->wgBlank = 0;
627 sfp->wgDefault = get_cache_default_char(psc);
628 sfp->wgInvalid = 0;
629 sfp->wgKashida = 0xffff;
630 sfp->iKashidaWidth = 0;
632 return S_OK;
635 /***********************************************************************
636 * ScriptRecordDigitSubstitution (USP10.@)
638 * Record digit substitution settings for a given locale.
640 * PARAMS
641 * locale [I] Locale identifier.
642 * sds [I] Structure to record substitution settings.
644 * RETURNS
645 * Success: S_OK
646 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
648 * SEE ALSO
649 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
651 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
653 DWORD plgid, sub;
655 TRACE("0x%x, %p\n", locale, sds);
657 /* This implementation appears to be correct for all languages, but it's
658 * not clear if sds->DigitSubstitute is ever set to anything except
659 * CONTEXT or NONE in reality */
661 if (!sds) return E_POINTER;
663 locale = ConvertDefaultLocale(locale);
665 if (!IsValidLocale(locale, LCID_INSTALLED))
666 return E_INVALIDARG;
668 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
669 sds->TraditionalDigitLanguage = plgid;
671 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
672 sds->NationalDigitLanguage = plgid;
673 else
674 sds->NationalDigitLanguage = LANG_ENGLISH;
676 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
677 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
679 switch (sub)
681 case 0:
682 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
683 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
684 else
685 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
686 break;
687 case 1:
688 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
689 break;
690 case 2:
691 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
692 break;
693 default:
694 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
695 break;
698 sds->dwReserved = 0;
699 return S_OK;
702 /***********************************************************************
703 * ScriptApplyDigitSubstitution (USP10.@)
705 * Apply digit substitution settings.
707 * PARAMS
708 * sds [I] Structure with recorded substitution settings.
709 * sc [I] Script control structure.
710 * ss [I] Script state structure.
712 * RETURNS
713 * Success: S_OK
714 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
716 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
717 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
719 SCRIPT_DIGITSUBSTITUTE psds;
721 TRACE("%p, %p, %p\n", sds, sc, ss);
723 if (!sc || !ss) return E_POINTER;
724 if (!sds)
726 sds = &psds;
727 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
728 return E_INVALIDARG;
731 sc->uDefaultLanguage = LANG_ENGLISH;
732 sc->fContextDigits = 0;
733 ss->fDigitSubstitute = 0;
735 switch (sds->DigitSubstitute) {
736 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
737 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
738 case SCRIPT_DIGITSUBSTITUTE_NONE:
739 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
740 return S_OK;
741 default:
742 return E_INVALIDARG;
746 /***********************************************************************
747 * ScriptItemizeOpenType (USP10.@)
749 * Split a Unicode string into shapeable parts.
751 * PARAMS
752 * pwcInChars [I] String to split.
753 * cInChars [I] Number of characters in pwcInChars.
754 * cMaxItems [I] Maximum number of items to return.
755 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
756 * psState [I] Pointer to a SCRIPT_STATE structure.
757 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
758 * pScriptTags [O] Buffer to receive OPENTYPE_TAGs.
759 * pcItems [O] Number of script items returned.
761 * RETURNS
762 * Success: S_OK
763 * Failure: Non-zero HRESULT value.
765 HRESULT WINAPI ScriptItemizeOpenType(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
766 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
767 SCRIPT_ITEM *pItems, OPENTYPE_TAG *pScriptTags, int *pcItems)
770 #define Numeric_space 0x0020
771 #define ZWNJ 0x200C
772 #define ZWJ 0x200D
774 int cnt = 0, index = 0, str = 0;
775 int New_Script = SCRIPT_UNDEFINED;
776 WORD *levels = NULL;
777 WORD *strength = NULL;
778 WORD baselevel = 0;
780 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
781 psControl, psState, pItems, pcItems);
783 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
784 return E_INVALIDARG;
786 if (psState && psControl)
788 int i;
789 levels = heap_alloc_zero(cInChars * sizeof(WORD));
790 if (!levels)
791 return E_OUTOFMEMORY;
793 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
794 baselevel = levels[0];
795 for (i = 0; i < cInChars; i++)
796 if (levels[i]!=levels[0])
797 break;
798 if (i >= cInChars && !odd(baselevel))
800 heap_free(levels);
801 levels = NULL;
803 else
805 if (!psControl->fMergeNeutralItems)
807 strength = heap_alloc_zero(cInChars * sizeof(WORD));
808 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
813 while ((pwcInChars[cnt] == Numeric_space || pwcInChars[cnt] == ZWJ || pwcInChars[cnt] == ZWNJ) && cnt < cInChars)
814 cnt++;
816 if (cnt == cInChars) /* All Spaces */
818 cnt = 0;
819 New_Script = get_char_script(pwcInChars[cnt]);
822 pItems[index].iCharPos = 0;
823 pItems[index].a = scriptInformation[get_char_script(pwcInChars[cnt])].a;
824 pScriptTags[index] = scriptInformation[get_char_script(pwcInChars[cnt])].scriptTag;
826 if (strength)
827 str = strength[cnt];
829 cnt = 0;
830 if (levels)
832 pItems[index].a.fRTL = odd(levels[cnt]);
833 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
834 pItems[index].a.s.uBidiLevel = levels[cnt];
836 else if (!pItems[index].a.s.uBidiLevel)
838 pItems[index].a.s.uBidiLevel = baselevel;
839 pItems[index].a.fLayoutRTL = odd(baselevel);
840 pItems[index].a.fRTL = odd(baselevel);
843 TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
844 levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
845 pItems[index].iCharPos);
847 for (cnt=1; cnt < cInChars; cnt++)
849 if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel && (!strength || (strength[cnt] == 0 || strength[cnt] == str))))
850 continue;
852 if(pwcInChars[cnt] != Numeric_space && pwcInChars[cnt] != ZWJ && pwcInChars[cnt] != ZWNJ)
853 New_Script = get_char_script(pwcInChars[cnt]);
854 else if (levels)
856 int j = 1;
857 while (cnt + j < cInChars - 1 && (pwcInChars[cnt+j] == Numeric_space || pwcInChars[cnt+j] == ZWJ || pwcInChars[cnt+j] == ZWNJ))
858 j++;
859 New_Script = get_char_script(pwcInChars[cnt+j]);
862 if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel || (strength && (strength[cnt] != str)))) || New_Script != pItems[index].a.eScript || New_Script == Script_Control)
864 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);
866 if (strength && strength[cnt] != 0)
867 str = strength[cnt];
869 index++;
870 if (index+1 > cMaxItems)
871 return E_OUTOFMEMORY;
873 pItems[index].iCharPos = cnt;
874 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
876 pItems[index].a = scriptInformation[New_Script].a;
877 pScriptTags[index] = scriptInformation[New_Script].scriptTag;
878 if (levels)
880 pItems[index].a.fRTL = odd(levels[cnt]);
881 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
882 pItems[index].a.s.uBidiLevel = levels[cnt];
884 else if (!pItems[index].a.s.uBidiLevel)
886 pItems[index].a.s.uBidiLevel = baselevel;
887 pItems[index].a.fLayoutRTL = odd(baselevel);
888 pItems[index].a.fRTL = odd(baselevel);
891 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
895 /* While not strictly necessary according to the spec, make sure the n+1
896 * item is set up to prevent random behaviour if the caller erroneously
897 * checks the n+1 structure */
898 index++;
899 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
901 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
903 /* Set one SCRIPT_STATE item being returned */
904 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
905 if (pcItems) *pcItems = index;
907 /* Set SCRIPT_ITEM */
908 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
909 heap_free(levels);
910 heap_free(strength);
911 return S_OK;
914 /***********************************************************************
915 * ScriptItemize (USP10.@)
917 * Split a Unicode string into shapeable parts.
919 * PARAMS
920 * pwcInChars [I] String to split.
921 * cInChars [I] Number of characters in pwcInChars.
922 * cMaxItems [I] Maximum number of items to return.
923 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
924 * psState [I] Pointer to a SCRIPT_STATE structure.
925 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
926 * pcItems [O] Number of script items returned.
928 * RETURNS
929 * Success: S_OK
930 * Failure: Non-zero HRESULT value.
932 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
933 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
934 SCRIPT_ITEM *pItems, int *pcItems)
936 OPENTYPE_TAG *discarded_tags;
937 HRESULT res;
939 discarded_tags = heap_alloc(cMaxItems * sizeof(OPENTYPE_TAG));
940 if (!discarded_tags)
941 return E_OUTOFMEMORY;
942 res = ScriptItemizeOpenType(pwcInChars, cInChars, cMaxItems, psControl, psState, pItems, discarded_tags, pcItems);
943 heap_free(discarded_tags);
944 return res;
947 static inline int getGivenTabWidth(ScriptCache *psc, SCRIPT_TABDEF *pTabdef, int charPos, int current_x)
949 int defWidth;
950 int cTabStops=0;
951 INT *lpTabPos = NULL;
952 INT nTabOrg = 0;
953 INT x = 0;
955 if (pTabdef)
956 lpTabPos = pTabdef->pTabStops;
958 if (pTabdef && pTabdef->iTabOrigin)
960 if (pTabdef->iScale)
961 nTabOrg = (pTabdef->iTabOrigin * pTabdef->iScale)/4;
962 else
963 nTabOrg = pTabdef->iTabOrigin * psc->tm.tmAveCharWidth;
966 if (pTabdef)
967 cTabStops = pTabdef->cTabStops;
969 if (cTabStops == 1)
971 if (pTabdef->iScale)
972 defWidth = ((pTabdef->pTabStops[0])*pTabdef->iScale) / 4;
973 else
974 defWidth = (pTabdef->pTabStops[0])*psc->tm.tmAveCharWidth;
975 cTabStops = 0;
977 else
978 defWidth = 8 * psc->tm.tmAveCharWidth;
980 for (; cTabStops>0 ; lpTabPos++, cTabStops--)
982 int position = *lpTabPos;
983 if (position < 0)
984 position = -1 * position;
985 if (pTabdef->iScale)
986 position = (position * pTabdef->iScale) / 4;
987 else
988 position = position * psc->tm.tmAveCharWidth;
990 if( nTabOrg + position > current_x)
992 if( *lpTabPos >= 0)
994 /* a left aligned tab */
995 x = (nTabOrg + *lpTabPos) - current_x;
996 break;
998 else
1000 FIXME("Negative tabstop\n");
1001 break;
1005 if ((!cTabStops) && (defWidth > 0))
1006 x =((((current_x - nTabOrg) / defWidth)+1) * defWidth) - current_x;
1007 else if ((!cTabStops) && (defWidth < 0))
1008 FIXME("TODO: Negative defWidth\n");
1010 return x;
1013 /***********************************************************************
1014 * Helper function for ScriptStringAnalyse
1016 static BOOL requires_fallback(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
1017 const WCHAR *pwcInChars, int cChars )
1019 /* FIXME: When to properly fallback is still a bit of a mystery */
1020 WORD *glyphs;
1022 if (psa->fNoGlyphIndex)
1023 return FALSE;
1025 if (init_script_cache(hdc, psc) != S_OK)
1026 return FALSE;
1028 if (SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa) != S_OK)
1029 return TRUE;
1031 glyphs = heap_alloc(sizeof(WORD) * cChars);
1032 if (!glyphs)
1033 return FALSE;
1034 if (ScriptGetCMap(hdc, psc, pwcInChars, cChars, 0, glyphs) != S_OK)
1036 heap_free(glyphs);
1037 return TRUE;
1039 heap_free(glyphs);
1041 return FALSE;
1044 static void find_fallback_font(DWORD scriptid, LPWSTR FaceName)
1046 HKEY hkey;
1048 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Uniscribe\\Fallback", &hkey))
1050 static const WCHAR szFmt[] = {'%','x',0};
1051 WCHAR value[10];
1052 DWORD count = LF_FACESIZE * sizeof(WCHAR);
1053 DWORD type;
1055 sprintfW(value, szFmt, scriptInformation[scriptid].scriptTag);
1056 if (RegQueryValueExW(hkey, value, 0, &type, (LPBYTE)FaceName, &count))
1057 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1058 RegCloseKey(hkey);
1060 else
1061 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1064 /***********************************************************************
1065 * ScriptStringAnalyse (USP10.@)
1068 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
1069 int cGlyphs, int iCharset, DWORD dwFlags,
1070 int iReqWidth, SCRIPT_CONTROL *psControl,
1071 SCRIPT_STATE *psState, const int *piDx,
1072 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
1073 SCRIPT_STRING_ANALYSIS *pssa)
1075 HRESULT hr = E_OUTOFMEMORY;
1076 StringAnalysis *analysis = NULL;
1077 SCRIPT_CONTROL sControl;
1078 SCRIPT_STATE sState;
1079 int i, num_items = 255;
1080 BYTE *BidiLevel;
1081 WCHAR *iString = NULL;
1083 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
1084 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
1085 psControl, psState, piDx, pTabdef, pbInClass, pssa);
1087 if (iCharset != -1)
1089 FIXME("Only Unicode strings are supported\n");
1090 return E_INVALIDARG;
1092 if (cString < 1 || !pString) return E_INVALIDARG;
1093 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
1095 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
1096 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
1098 /* FIXME: handle clipping */
1099 analysis->clip_len = cString;
1100 analysis->hdc = hdc;
1101 analysis->dwFlags = dwFlags;
1103 if (psState)
1104 sState = *psState;
1105 else
1106 memset(&sState, 0, sizeof(SCRIPT_STATE));
1108 if (psControl)
1109 sControl = *psControl;
1110 else
1111 memset(&sControl, 0, sizeof(SCRIPT_CONTROL));
1113 if (dwFlags & SSA_PASSWORD)
1115 iString = heap_alloc(sizeof(WCHAR)*cString);
1116 if (!iString)
1118 hr = E_OUTOFMEMORY;
1119 goto error;
1121 for (i = 0; i < cString; i++)
1122 iString[i] = *((const WCHAR *)pString);
1123 pString = iString;
1126 hr = ScriptItemize(pString, cString, num_items, &sControl, &sState, analysis->pItem,
1127 &analysis->numItems);
1129 while (hr == E_OUTOFMEMORY)
1131 SCRIPT_ITEM *tmp;
1133 num_items *= 2;
1134 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
1135 goto error;
1137 analysis->pItem = tmp;
1138 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
1139 &analysis->numItems);
1141 if (hr != S_OK) goto error;
1143 /* set back to out of memory for default goto error behaviour */
1144 hr = E_OUTOFMEMORY;
1146 if (dwFlags & SSA_BREAK)
1148 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
1150 for (i = 0; i < analysis->numItems; i++)
1151 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]);
1153 else
1154 goto error;
1157 if (!(analysis->logical2visual = heap_alloc_zero(sizeof(int) * analysis->numItems)))
1158 goto error;
1159 if (!(BidiLevel = heap_alloc_zero(analysis->numItems)))
1160 goto error;
1162 if (dwFlags & SSA_GLYPHS)
1164 int tab_x = 0;
1165 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
1166 goto error;
1168 for (i = 0; i < analysis->numItems; i++)
1170 SCRIPT_CACHE *sc = (SCRIPT_CACHE*)&analysis->glyphs[i].sc;
1171 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1172 int numGlyphs = 1.5 * cChar + 16;
1173 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
1174 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
1175 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
1176 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * numGlyphs);
1177 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
1178 ABC *abc = heap_alloc_zero(sizeof(ABC));
1179 int numGlyphsReturned;
1180 HFONT originalFont = 0x0;
1182 /* FIXME: non unicode strings */
1183 const WCHAR* pStr = (const WCHAR*)pString;
1184 analysis->glyphs[i].fallbackFont = NULL;
1186 if (!glyphs || !pwLogClust || !piAdvance || !psva || !pGoffset || !abc)
1188 heap_free (glyphs);
1189 heap_free (pwLogClust);
1190 heap_free (piAdvance);
1191 heap_free (psva);
1192 heap_free (pGoffset);
1193 heap_free (abc);
1194 hr = E_OUTOFMEMORY;
1195 goto error;
1198 if ((dwFlags & SSA_FALLBACK) && requires_fallback(hdc, sc, &analysis->pItem[i].a, &pStr[analysis->pItem[i].iCharPos], cChar))
1200 LOGFONTW lf;
1201 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), & lf);
1202 lf.lfCharSet = scriptInformation[analysis->pItem[i].a.eScript].props.bCharSet;
1203 find_fallback_font(analysis->pItem[i].a.eScript, lf.lfFaceName);
1204 analysis->glyphs[i].fallbackFont = CreateFontIndirectW(&lf);
1205 if (analysis->glyphs[i].fallbackFont)
1207 ScriptFreeCache(sc);
1208 originalFont = SelectObject(hdc, analysis->glyphs[i].fallbackFont);
1212 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
1213 cChar, numGlyphs, &analysis->pItem[i].a,
1214 glyphs, pwLogClust, psva, &numGlyphsReturned);
1215 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
1216 piAdvance, pGoffset, abc);
1217 if (originalFont)
1218 SelectObject(hdc,originalFont);
1220 if (dwFlags & SSA_TAB)
1222 int tabi = 0;
1223 for (tabi = 0; tabi < cChar; tabi++)
1225 if (pStr[analysis->pItem[i].iCharPos+tabi] == 0x0009)
1226 piAdvance[tabi] = getGivenTabWidth(analysis->glyphs[i].sc, pTabdef, analysis->pItem[i].iCharPos+tabi, tab_x);
1227 tab_x+=piAdvance[tabi];
1231 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
1232 analysis->glyphs[i].glyphs = glyphs;
1233 analysis->glyphs[i].pwLogClust = pwLogClust;
1234 analysis->glyphs[i].piAdvance = piAdvance;
1235 analysis->glyphs[i].psva = psva;
1236 analysis->glyphs[i].pGoffset = pGoffset;
1237 analysis->glyphs[i].abc = abc;
1238 analysis->glyphs[i].iMaxPosX= -1;
1240 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1243 else
1245 for (i = 0; i < analysis->numItems; i++)
1246 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1249 ScriptLayout(analysis->numItems, BidiLevel, NULL, analysis->logical2visual);
1250 heap_free(BidiLevel);
1252 *pssa = analysis;
1253 heap_free(iString);
1254 return S_OK;
1256 error:
1257 heap_free(iString);
1258 heap_free(analysis->glyphs);
1259 heap_free(analysis->logattrs);
1260 heap_free(analysis->pItem);
1261 heap_free(analysis->logical2visual);
1262 heap_free(analysis);
1263 return hr;
1266 static HRESULT SS_ItemOut( SCRIPT_STRING_ANALYSIS ssa,
1267 int iX,
1268 int iY,
1269 int iItem,
1270 int cStart,
1271 int cEnd,
1272 UINT uOptions,
1273 const RECT *prc,
1274 BOOL fSelected,
1275 BOOL fDisabled)
1277 StringAnalysis *analysis;
1278 int off_x = 0;
1279 HRESULT hr;
1280 COLORREF BkColor = 0x0;
1281 COLORREF TextColor = 0x0;
1282 INT BkMode = 0;
1283 INT runStart, runEnd;
1284 INT iGlyph, cGlyphs;
1285 HFONT oldFont = 0x0;
1287 TRACE("(%p,%d,%d,%d,%d,%d, 0x%1x, %d, %d)\n",
1288 ssa, iX, iY, iItem, cStart, cEnd, uOptions, fSelected, fDisabled);
1290 if (!(analysis = ssa)) return E_INVALIDARG;
1292 if ((cStart >= 0 && analysis->pItem[iItem+1].iCharPos <= cStart) ||
1293 (cEnd >= 0 && analysis->pItem[iItem].iCharPos >= cEnd))
1294 return S_OK;
1296 if (fSelected)
1298 BkMode = GetBkMode(analysis->hdc);
1299 SetBkMode( analysis->hdc, OPAQUE);
1300 BkColor = GetBkColor(analysis->hdc);
1301 SetBkColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHT));
1302 if (!fDisabled)
1304 TextColor = GetTextColor(analysis->hdc);
1305 SetTextColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1308 if (analysis->glyphs[iItem].fallbackFont)
1309 oldFont = SelectObject(analysis->hdc, analysis->glyphs[iItem].fallbackFont);
1311 if (cStart >= 0 && analysis->pItem[iItem+1].iCharPos > cStart && analysis->pItem[iItem].iCharPos <= cStart)
1312 runStart = cStart - analysis->pItem[iItem].iCharPos;
1313 else
1314 runStart = 0;
1315 if (cEnd >= 0 && analysis->pItem[iItem+1].iCharPos > cEnd && analysis->pItem[iItem].iCharPos <= cEnd)
1316 runEnd = (cEnd-1) - analysis->pItem[iItem].iCharPos;
1317 else
1318 runEnd = (analysis->pItem[iItem+1].iCharPos - analysis->pItem[iItem].iCharPos) - 1;
1320 if (analysis->pItem[iItem].a.fRTL)
1322 if (cEnd >= 0 && cEnd < analysis->pItem[iItem+1].iCharPos)
1323 ScriptStringCPtoX(ssa, cEnd, FALSE, &off_x);
1324 else
1325 ScriptStringCPtoX(ssa, analysis->pItem[iItem+1].iCharPos-1, TRUE, &off_x);
1327 else
1329 if (cStart >=0 && runStart)
1330 ScriptStringCPtoX(ssa, cStart, FALSE, &off_x);
1331 else
1332 ScriptStringCPtoX(ssa, analysis->pItem[iItem].iCharPos, FALSE, &off_x);
1335 if (analysis->pItem[iItem].a.fRTL)
1336 iGlyph = analysis->glyphs[iItem].pwLogClust[runEnd];
1337 else
1338 iGlyph = analysis->glyphs[iItem].pwLogClust[runStart];
1340 if (analysis->pItem[iItem].a.fRTL)
1341 cGlyphs = analysis->glyphs[iItem].pwLogClust[runStart] - iGlyph;
1342 else
1343 cGlyphs = analysis->glyphs[iItem].pwLogClust[runEnd] - iGlyph;
1345 cGlyphs++;
1347 hr = ScriptTextOut(analysis->hdc,
1348 (SCRIPT_CACHE *)&analysis->glyphs[iItem].sc, iX + off_x,
1349 iY, uOptions, prc, &analysis->pItem[iItem].a, NULL, 0,
1350 &analysis->glyphs[iItem].glyphs[iGlyph], cGlyphs,
1351 &analysis->glyphs[iItem].piAdvance[iGlyph], NULL,
1352 &analysis->glyphs[iItem].pGoffset[iGlyph]);
1354 TRACE("ScriptTextOut hr=%08x\n", hr);
1356 if (fSelected)
1358 SetBkColor(analysis->hdc, BkColor);
1359 SetBkMode( analysis->hdc, BkMode);
1360 if (!fDisabled)
1361 SetTextColor(analysis->hdc, TextColor);
1363 if (analysis->glyphs[iItem].fallbackFont)
1364 SelectObject(analysis->hdc, oldFont);
1366 return hr;
1369 /***********************************************************************
1370 * ScriptStringOut (USP10.@)
1372 * This function takes the output of ScriptStringAnalyse and joins the segments
1373 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
1374 * only processes glyphs.
1376 * Parameters:
1377 * ssa [I] buffer to hold the analysed string components
1378 * iX [I] X axis displacement for output
1379 * iY [I] Y axis displacement for output
1380 * uOptions [I] flags controling output processing
1381 * prc [I] rectangle coordinates
1382 * iMinSel [I] starting pos for substringing output string
1383 * iMaxSel [I] ending pos for substringing output string
1384 * fDisabled [I] controls text highlighting
1386 * RETURNS
1387 * Success: S_OK
1388 * Failure: is the value returned by ScriptTextOut
1390 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
1391 int iX,
1392 int iY,
1393 UINT uOptions,
1394 const RECT *prc,
1395 int iMinSel,
1396 int iMaxSel,
1397 BOOL fDisabled)
1399 StringAnalysis *analysis;
1400 int item;
1401 HRESULT hr;
1403 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
1404 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
1406 if (!(analysis = ssa)) return E_INVALIDARG;
1407 if (!(analysis->dwFlags & SSA_GLYPHS)) return E_INVALIDARG;
1409 for (item = 0; item < analysis->numItems; item++)
1411 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], -1, -1, uOptions, prc, FALSE, fDisabled);
1412 if (FAILED(hr))
1413 return hr;
1416 if (iMinSel < iMaxSel && (iMinSel > 0 || iMaxSel > 0))
1418 if (iMaxSel > 0 && iMinSel < 0)
1419 iMinSel = 0;
1420 for (item = 0; item < analysis->numItems; item++)
1422 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], iMinSel, iMaxSel, uOptions, prc, TRUE, fDisabled);
1423 if (FAILED(hr))
1424 return hr;
1428 return S_OK;
1431 /***********************************************************************
1432 * ScriptStringCPtoX (USP10.@)
1435 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
1437 int item;
1438 int runningX = 0;
1439 StringAnalysis* analysis = ssa;
1441 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
1443 if (!ssa || !pX) return S_FALSE;
1444 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1446 /* icp out of range */
1447 if(icp < 0)
1449 analysis->invalid = TRUE;
1450 return E_INVALIDARG;
1453 for(item=0; item<analysis->numItems; item++)
1455 int CP, i;
1456 int offset;
1458 i = analysis->logical2visual[item];
1459 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1460 /* initialize max extents for uninitialized runs */
1461 if (analysis->glyphs[i].iMaxPosX == -1)
1463 if (analysis->pItem[i].a.fRTL)
1464 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1465 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1466 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1467 else
1468 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1469 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1470 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1473 if (icp >= analysis->pItem[i+1].iCharPos || icp < analysis->pItem[i].iCharPos)
1475 runningX += analysis->glyphs[i].iMaxPosX;
1476 continue;
1479 icp -= analysis->pItem[i].iCharPos;
1480 ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1481 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1482 &analysis->pItem[i].a, &offset);
1483 runningX += offset;
1485 *pX = runningX;
1486 return S_OK;
1489 /* icp out of range */
1490 analysis->invalid = TRUE;
1491 return E_INVALIDARG;
1494 /***********************************************************************
1495 * ScriptStringXtoCP (USP10.@)
1498 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
1500 StringAnalysis* analysis = ssa;
1501 int item;
1503 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1505 if (!ssa || !piCh || !piTrailing) return S_FALSE;
1506 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1508 /* out of range */
1509 if(iX < 0)
1511 if (analysis->pItem[0].a.fRTL)
1513 *piCh = 1;
1514 *piTrailing = FALSE;
1516 else
1518 *piCh = -1;
1519 *piTrailing = TRUE;
1521 return S_OK;
1524 for(item=0; item<analysis->numItems; item++)
1526 int i;
1527 int CP;
1529 for (i = 0; i < analysis->numItems && analysis->logical2visual[i] != item; i++)
1530 /* nothing */;
1532 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1533 /* initialize max extents for uninitialized runs */
1534 if (analysis->glyphs[i].iMaxPosX == -1)
1536 if (analysis->pItem[i].a.fRTL)
1537 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1538 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1539 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1540 else
1541 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1542 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1543 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1546 if (iX > analysis->glyphs[i].iMaxPosX)
1548 iX -= analysis->glyphs[i].iMaxPosX;
1549 continue;
1552 ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1553 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1554 &analysis->pItem[i].a, piCh, piTrailing);
1555 *piCh += analysis->pItem[i].iCharPos;
1557 return S_OK;
1560 /* out of range */
1561 *piCh = analysis->pItem[analysis->numItems].iCharPos;
1562 *piTrailing = FALSE;
1564 return S_OK;
1568 /***********************************************************************
1569 * ScriptStringFree (USP10.@)
1571 * Free a string analysis.
1573 * PARAMS
1574 * pssa [I] string analysis.
1576 * RETURNS
1577 * Success: S_OK
1578 * Failure: Non-zero HRESULT value.
1580 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
1582 StringAnalysis* analysis;
1583 BOOL invalid;
1584 int i;
1586 TRACE("(%p)\n", pssa);
1588 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
1590 invalid = analysis->invalid;
1592 if (analysis->glyphs)
1594 for (i = 0; i < analysis->numItems; i++)
1596 heap_free(analysis->glyphs[i].glyphs);
1597 heap_free(analysis->glyphs[i].pwLogClust);
1598 heap_free(analysis->glyphs[i].piAdvance);
1599 heap_free(analysis->glyphs[i].psva);
1600 heap_free(analysis->glyphs[i].pGoffset);
1601 heap_free(analysis->glyphs[i].abc);
1602 if (analysis->glyphs[i].fallbackFont)
1603 DeleteObject(analysis->glyphs[i].fallbackFont);
1604 ScriptFreeCache((SCRIPT_CACHE *)&analysis->glyphs[i].sc);
1605 heap_free(analysis->glyphs[i].sc);
1607 heap_free(analysis->glyphs);
1610 heap_free(analysis->pItem);
1611 heap_free(analysis->logattrs);
1612 heap_free(analysis->sz);
1613 heap_free(analysis->logical2visual);
1614 heap_free(analysis);
1616 if (invalid) return E_INVALIDARG;
1617 return S_OK;
1620 static inline int get_cluster_size(const WORD *pwLogClust, int cChars, int item,
1621 int direction, int* iCluster, int *check_out)
1623 int clust_size = 1;
1624 int check;
1625 WORD clust = pwLogClust[item];
1627 for (check = item+direction; check < cChars && check >= 0; check+=direction)
1629 if (pwLogClust[check] == clust)
1631 clust_size ++;
1632 if (iCluster && *iCluster == -1)
1633 *iCluster = item;
1635 else break;
1638 if (check_out)
1639 *check_out = check;
1641 return clust_size;
1644 /***********************************************************************
1645 * ScriptCPtoX (USP10.@)
1648 HRESULT WINAPI ScriptCPtoX(int iCP,
1649 BOOL fTrailing,
1650 int cChars,
1651 int cGlyphs,
1652 const WORD *pwLogClust,
1653 const SCRIPT_VISATTR *psva,
1654 const int *piAdvance,
1655 const SCRIPT_ANALYSIS *psa,
1656 int *piX)
1658 int item;
1659 float iPosX;
1660 int iSpecial = -1;
1661 int iCluster = -1;
1662 int clust_size = 1;
1663 float special_size = 0.0;
1664 int iMaxPos = 0;
1665 BOOL rtl = FALSE;
1667 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1668 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1669 psa, piX);
1671 if (psa->fRTL && ! psa->fLogicalOrder)
1672 rtl = TRUE;
1674 if (fTrailing)
1675 iCP++;
1677 if (rtl)
1679 int max_clust = pwLogClust[0];
1681 for (item=0; item < cGlyphs; item++)
1682 if (pwLogClust[item] > max_clust)
1684 ERR("We do not handle non reversed clusters properly\n");
1685 break;
1688 iMaxPos = 0;
1689 for (item = max_clust; item >=0; item --)
1690 iMaxPos += piAdvance[item];
1693 iPosX = 0.0;
1694 for (item=0; item < iCP && item < cChars; item++)
1696 if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
1698 int check;
1699 int clust = pwLogClust[item];
1701 iCluster = -1;
1702 clust_size = get_cluster_size(pwLogClust, cChars, item, 1, &iCluster,
1703 &check);
1705 if (check >= cChars && !iMaxPos)
1707 for (check = clust; check < cChars; check++)
1708 special_size += piAdvance[check];
1709 iSpecial = item;
1710 special_size /= (cChars - item);
1711 iPosX += special_size;
1713 else
1715 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
1717 clust_size --;
1718 if (clust_size == 0)
1719 iPosX += piAdvance[clust];
1721 else
1722 iPosX += piAdvance[clust] / (float)clust_size;
1725 else if (iSpecial != -1)
1726 iPosX += special_size;
1727 else /* (iCluster != -1) */
1729 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
1731 clust_size --;
1732 if (clust_size == 0)
1733 iPosX += piAdvance[pwLogClust[iCluster]];
1735 else
1736 iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1740 if (iMaxPos > 0)
1742 iPosX = iMaxPos - iPosX;
1743 if (iPosX < 0)
1744 iPosX = 0;
1747 *piX = iPosX;
1748 TRACE("*piX=%d\n", *piX);
1749 return S_OK;
1752 /***********************************************************************
1753 * ScriptXtoCP (USP10.@)
1756 HRESULT WINAPI ScriptXtoCP(int iX,
1757 int cChars,
1758 int cGlyphs,
1759 const WORD *pwLogClust,
1760 const SCRIPT_VISATTR *psva,
1761 const int *piAdvance,
1762 const SCRIPT_ANALYSIS *psa,
1763 int *piCP,
1764 int *piTrailing)
1766 int item;
1767 float iPosX;
1768 float iLastPosX;
1769 int iSpecial = -1;
1770 int iCluster = -1;
1771 int clust_size = 1;
1772 int cjump = 0;
1773 float special_size = 0.0;
1774 int direction = 1;
1776 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1777 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1778 psa, piCP, piTrailing);
1780 if (psa->fRTL && ! psa->fLogicalOrder)
1781 direction = -1;
1783 if (direction<0)
1785 int max_clust = pwLogClust[0];
1787 if (iX < 0)
1789 *piCP = cChars;
1790 *piTrailing = 0;
1791 return S_OK;
1794 for (item=0; item < cChars; item++)
1795 if (pwLogClust[item] > max_clust)
1797 ERR("We do not handle non reversed clusters properly\n");
1798 break;
1802 if (iX < 0)
1804 *piCP = -1;
1805 *piTrailing = 1;
1806 return S_OK;
1809 iPosX = iLastPosX = 0;
1810 if (direction > 0)
1811 item = 0;
1812 else
1813 item = cChars - 1;
1814 for (; iPosX <= iX && item < cChars && item >= 0; item+=direction)
1816 iLastPosX = iPosX;
1817 if (iSpecial == -1 &&
1818 (iCluster == -1 ||
1819 (iCluster != -1 &&
1820 ((direction > 0 && iCluster+clust_size <= item) ||
1821 (direction < 0 && iCluster-clust_size >= item))
1826 int check;
1827 int clust = pwLogClust[item];
1829 iCluster = -1;
1830 cjump = 0;
1831 clust_size = get_cluster_size(pwLogClust, cChars, item, direction,
1832 &iCluster, &check);
1834 if (check >= cChars && direction > 0)
1836 for (check = clust; check < cChars; check++)
1837 special_size += piAdvance[check];
1838 iSpecial = item;
1839 special_size /= (cChars - item);
1840 iPosX += special_size;
1842 else
1844 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
1846 if (!cjump)
1847 iPosX += piAdvance[clust];
1848 cjump++;
1850 else
1851 iPosX += piAdvance[clust] / (float)clust_size;
1854 else if (iSpecial != -1)
1855 iPosX += special_size;
1856 else /* (iCluster != -1) */
1858 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
1860 if (!cjump)
1861 iPosX += piAdvance[pwLogClust[iCluster]];
1862 cjump++;
1864 else
1865 iPosX += piAdvance[pwLogClust[iCluster]] / (float)clust_size;
1869 if (direction > 0)
1871 if (iPosX > iX)
1872 item--;
1873 if (item < cChars && ((iPosX - iLastPosX) / 2.0) + iX >= iPosX)
1875 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
1876 item+=(clust_size-1);
1877 *piTrailing = 1;
1879 else
1880 *piTrailing = 0;
1882 else
1884 if (iX == iLastPosX)
1885 item++;
1886 if (iX >= iLastPosX && iX <= iPosX)
1887 item++;
1889 if (iLastPosX == iX)
1890 *piTrailing = 0;
1891 else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
1893 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
1894 item-=(clust_size-1);
1895 *piTrailing = 1;
1897 else
1898 *piTrailing = 0;
1901 *piCP = item;
1903 TRACE("*piCP=%d\n", *piCP);
1904 TRACE("*piTrailing=%d\n", *piTrailing);
1905 return S_OK;
1908 /***********************************************************************
1909 * ScriptBreak (USP10.@)
1911 * Retrieve line break information.
1913 * PARAMS
1914 * chars [I] Array of characters.
1915 * sa [I] String analysis.
1916 * la [I] Array of logical attribute structures.
1918 * RETURNS
1919 * Success: S_OK
1920 * Failure: S_FALSE
1922 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1924 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1926 if (!la) return S_FALSE;
1928 BREAK_line(chars, count, sa, la);
1930 return S_OK;
1933 /***********************************************************************
1934 * ScriptIsComplex (USP10.@)
1936 * Determine if a string is complex.
1938 * PARAMS
1939 * chars [I] Array of characters to test.
1940 * len [I] Length in characters.
1941 * flag [I] Flag.
1943 * RETURNS
1944 * Success: S_OK
1945 * Failure: S_FALSE
1948 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1950 int i;
1952 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1954 for (i = 0; i < len; i++)
1956 int script;
1958 if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
1959 return S_OK;
1961 script = get_char_script(chars[i]);
1962 if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
1963 (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
1964 return S_OK;
1966 return S_FALSE;
1969 /***********************************************************************
1970 * ScriptShapeOpenType (USP10.@)
1972 * Produce glyphs and visual attributes for a run.
1974 * PARAMS
1975 * hdc [I] Device context.
1976 * psc [I/O] Opaque pointer to a script cache.
1977 * psa [I/O] Script analysis.
1978 * tagScript [I] The OpenType tag for the Script
1979 * tagLangSys [I] The OpenType tag for the Language
1980 * rcRangeChars[I] Array of Character counts in each range
1981 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
1982 * cRanges [I] Count of ranges
1983 * pwcChars [I] Array of characters specifying the run.
1984 * cChars [I] Number of characters in pwcChars.
1985 * cMaxGlyphs [I] Length of pwOutGlyphs.
1986 * pwLogClust [O] Array of logical cluster info.
1987 * pCharProps [O] Array of character property values
1988 * pwOutGlyphs [O] Array of glyphs.
1989 * pOutGlyphProps [O] Array of attributes for the retrieved glyphs
1990 * pcGlyphs [O] Number of glyphs returned.
1992 * RETURNS
1993 * Success: S_OK
1994 * Failure: Non-zero HRESULT value.
1996 HRESULT WINAPI ScriptShapeOpenType( HDC hdc, SCRIPT_CACHE *psc,
1997 SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
1998 OPENTYPE_TAG tagLangSys, int *rcRangeChars,
1999 TEXTRANGE_PROPERTIES **rpRangeProperties,
2000 int cRanges, const WCHAR *pwcChars, int cChars,
2001 int cMaxGlyphs, WORD *pwLogClust,
2002 SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs,
2003 SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs)
2005 HRESULT hr;
2006 unsigned int i;
2007 BOOL rtl;
2009 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %d, %d, %p, %p, %p, %p, %p )\n",
2010 hdc, psc, psa,
2011 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2012 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2013 cChars, cMaxGlyphs, pwLogClust, pCharProps, pwOutGlyphs, pOutGlyphProps, pcGlyphs);
2015 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
2016 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
2018 if (!pOutGlyphProps || !pcGlyphs || !pCharProps) return E_INVALIDARG;
2019 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2021 if (cRanges)
2022 FIXME("Ranges not supported yet\n");
2024 rtl = (psa && !psa->fLogicalOrder && psa->fRTL);
2026 *pcGlyphs = cChars;
2027 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2028 if (!pwLogClust) return E_FAIL;
2030 ((ScriptCache *)*psc)->userScript = tagScript;
2031 ((ScriptCache *)*psc)->userLang = tagLangSys;
2033 /* set fNoGlyphIndex non truetype/opentype fonts */
2034 if (!psa->fNoGlyphIndex && !((ScriptCache *)*psc)->sfnt)
2035 psa->fNoGlyphIndex = TRUE;
2037 /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
2038 for (i = 0; i < cChars; i++)
2040 int idx = i;
2041 if (rtl) idx = cChars - 1 - i;
2042 /* FIXME: set to better values */
2043 pOutGlyphProps[i].sva.uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
2044 pOutGlyphProps[i].sva.fClusterStart = 1;
2045 pOutGlyphProps[i].sva.fDiacritic = 0;
2046 pOutGlyphProps[i].sva.fZeroWidth = 0;
2047 pOutGlyphProps[i].sva.fReserved = 0;
2048 pOutGlyphProps[i].sva.fShapeReserved = 0;
2050 /* FIXME: have the shaping engine set this */
2051 pCharProps[i].fCanGlyphAlone = 0;
2053 pwLogClust[i] = idx;
2056 if (psa && !psa->fNoGlyphIndex)
2058 WCHAR *rChars;
2059 if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
2061 rChars = heap_alloc(sizeof(WCHAR) * cChars);
2062 if (!rChars) return E_OUTOFMEMORY;
2063 for (i = 0; i < cChars; i++)
2065 int idx = i;
2066 WCHAR chInput;
2067 if (rtl) idx = cChars - 1 - i;
2068 if (psa->fRTL)
2069 chInput = mirror_char(pwcChars[idx]);
2070 else
2071 chInput = pwcChars[idx];
2072 /* special case for tabs */
2073 if (chInput == 0x0009)
2074 chInput = 0x0020;
2075 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
2077 WORD glyph;
2078 if (!hdc) return E_PENDING;
2079 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
2080 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
2082 rChars[i] = chInput;
2085 SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
2086 SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
2087 SHAPE_CharGlyphProp(hdc, (ScriptCache *)*psc, psa, pwcChars, cChars, pwOutGlyphs, *pcGlyphs, pwLogClust, pCharProps, pOutGlyphProps);
2088 heap_free(rChars);
2090 else
2092 TRACE("no glyph translation\n");
2093 for (i = 0; i < cChars; i++)
2095 int idx = i;
2096 /* No mirroring done here */
2097 if (rtl) idx = cChars - 1 - i;
2098 pwOutGlyphs[i] = pwcChars[idx];
2102 return S_OK;
2106 /***********************************************************************
2107 * ScriptShape (USP10.@)
2109 * Produce glyphs and visual attributes for a run.
2111 * PARAMS
2112 * hdc [I] Device context.
2113 * psc [I/O] Opaque pointer to a script cache.
2114 * pwcChars [I] Array of characters specifying the run.
2115 * cChars [I] Number of characters in pwcChars.
2116 * cMaxGlyphs [I] Length of pwOutGlyphs.
2117 * psa [I/O] Script analysis.
2118 * pwOutGlyphs [O] Array of glyphs.
2119 * pwLogClust [O] Array of logical cluster info.
2120 * psva [O] Array of visual attributes.
2121 * pcGlyphs [O] Number of glyphs returned.
2123 * RETURNS
2124 * Success: S_OK
2125 * Failure: Non-zero HRESULT value.
2127 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
2128 int cChars, int cMaxGlyphs,
2129 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
2130 SCRIPT_VISATTR *psva, int *pcGlyphs)
2132 HRESULT hr;
2133 int i;
2134 SCRIPT_CHARPROP *charProps;
2135 SCRIPT_GLYPHPROP *glyphProps;
2137 if (!psva || !pcGlyphs) return E_INVALIDARG;
2138 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2140 charProps = heap_alloc_zero(sizeof(SCRIPT_CHARPROP)*cChars);
2141 if (!charProps) return E_OUTOFMEMORY;
2142 glyphProps = heap_alloc_zero(sizeof(SCRIPT_GLYPHPROP)*cMaxGlyphs);
2143 if (!glyphProps) return E_OUTOFMEMORY;
2145 hr = ScriptShapeOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, pwcChars, cChars, cMaxGlyphs, pwLogClust, charProps, pwOutGlyphs, glyphProps, pcGlyphs);
2147 if (SUCCEEDED(hr))
2149 for (i = 0; i < *pcGlyphs; i++)
2150 psva[i] = glyphProps[i].sva;
2153 heap_free(charProps);
2154 heap_free(glyphProps);
2156 return hr;
2159 /***********************************************************************
2160 * ScriptPlaceOpenType (USP10.@)
2162 * Produce advance widths for a run.
2164 * PARAMS
2165 * hdc [I] Device context.
2166 * psc [I/O] Opaque pointer to a script cache.
2167 * psa [I/O] String analysis.
2168 * tagScript [I] The OpenType tag for the Script
2169 * tagLangSys [I] The OpenType tag for the Language
2170 * rcRangeChars[I] Array of Character counts in each range
2171 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
2172 * cRanges [I] Count of ranges
2173 * pwcChars [I] Array of characters specifying the run.
2174 * pwLogClust [I] Array of logical cluster info
2175 * pCharProps [I] Array of character property values
2176 * cChars [I] Number of characters in pwcChars.
2177 * pwGlyphs [I] Array of glyphs.
2178 * pGlyphProps [I] Array of attributes for the retrieved glyphs
2179 * cGlyphs [I] Count of Glyphs
2180 * piAdvance [O] Array of advance widths.
2181 * pGoffset [O] Glyph offsets.
2182 * pABC [O] Combined ABC width.
2184 * RETURNS
2185 * Success: S_OK
2186 * Failure: Non-zero HRESULT value.
2189 HRESULT WINAPI ScriptPlaceOpenType( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
2190 OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys,
2191 int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties,
2192 int cRanges, const WCHAR *pwcChars, WORD *pwLogClust,
2193 SCRIPT_CHARPROP *pCharProps, int cChars,
2194 const WORD *pwGlyphs, const SCRIPT_GLYPHPROP *pGlyphProps,
2195 int cGlyphs, int *piAdvance,
2196 GOFFSET *pGoffset, ABC *pABC
2199 HRESULT hr;
2200 int i;
2202 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %p, %p, %d, %p, %p, %d, %p %p %p)\n",
2203 hdc, psc, psa,
2204 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2205 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2206 pwLogClust, pCharProps, cChars, pwGlyphs, pGlyphProps, cGlyphs, piAdvance,
2207 pGoffset, pABC);
2209 if (!pGlyphProps) return E_INVALIDARG;
2210 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2211 if (!pGoffset) return E_FAIL;
2213 if (cRanges)
2214 FIXME("Ranges not supported yet\n");
2216 ((ScriptCache *)*psc)->userScript = tagScript;
2217 ((ScriptCache *)*psc)->userLang = tagLangSys;
2219 if (pABC) memset(pABC, 0, sizeof(ABC));
2220 for (i = 0; i < cGlyphs; i++)
2222 ABC abc;
2223 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
2225 if (!hdc) return E_PENDING;
2226 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
2228 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
2230 else
2232 INT width;
2233 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
2234 abc.abcB = width;
2235 abc.abcA = abc.abcC = 0;
2237 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
2239 if (pABC)
2241 pABC->abcA += abc.abcA;
2242 pABC->abcB += abc.abcB;
2243 pABC->abcC += abc.abcC;
2245 /* FIXME: set to more reasonable values */
2246 pGoffset[i].du = pGoffset[i].dv = 0;
2247 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
2250 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
2251 return S_OK;
2254 /***********************************************************************
2255 * ScriptPlace (USP10.@)
2257 * Produce advance widths for a run.
2259 * PARAMS
2260 * hdc [I] Device context.
2261 * psc [I/O] Opaque pointer to a script cache.
2262 * pwGlyphs [I] Array of glyphs.
2263 * cGlyphs [I] Number of glyphs in pwGlyphs.
2264 * psva [I] Array of visual attributes.
2265 * psa [I/O] String analysis.
2266 * piAdvance [O] Array of advance widths.
2267 * pGoffset [O] Glyph offsets.
2268 * pABC [O] Combined ABC width.
2270 * RETURNS
2271 * Success: S_OK
2272 * Failure: Non-zero HRESULT value.
2274 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
2275 int cGlyphs, const SCRIPT_VISATTR *psva,
2276 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
2278 HRESULT hr;
2279 SCRIPT_GLYPHPROP *glyphProps;
2280 int i;
2282 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
2283 piAdvance, pGoffset, pABC);
2285 if (!psva) return E_INVALIDARG;
2286 if (!pGoffset) return E_FAIL;
2288 glyphProps = heap_alloc(sizeof(SCRIPT_GLYPHPROP)*cGlyphs);
2289 if (!glyphProps) return E_OUTOFMEMORY;
2291 for (i = 0; i < cGlyphs; i++)
2292 glyphProps[i].sva = psva[i];
2294 hr = ScriptPlaceOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, NULL, NULL, NULL, 0, pwGlyphs, glyphProps, cGlyphs, piAdvance, pGoffset, pABC);
2296 heap_free(glyphProps);
2298 return hr;
2301 /***********************************************************************
2302 * ScriptGetCMap (USP10.@)
2304 * Retrieve glyph indices.
2306 * PARAMS
2307 * hdc [I] Device context.
2308 * psc [I/O] Opaque pointer to a script cache.
2309 * pwcInChars [I] Array of Unicode characters.
2310 * cChars [I] Number of characters in pwcInChars.
2311 * dwFlags [I] Flags.
2312 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
2314 * RETURNS
2315 * Success: S_OK
2316 * Failure: Non-zero HRESULT value.
2318 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
2319 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
2321 HRESULT hr;
2322 int i;
2324 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
2325 cChars, dwFlags, pwOutGlyphs);
2327 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2329 hr = S_OK;
2331 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2333 for (i = 0; i < cChars; i++)
2335 WCHAR inChar;
2336 if (dwFlags == SGCM_RTL)
2337 inChar = mirror_char(pwcInChars[i]);
2338 else
2339 inChar = pwcInChars[i];
2340 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
2342 WORD glyph;
2343 if (!hdc) return E_PENDING;
2344 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
2345 if (glyph == 0xffff)
2347 hr = S_FALSE;
2348 glyph = 0x0;
2350 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
2354 else
2356 TRACE("no glyph translation\n");
2357 for (i = 0; i < cChars; i++)
2359 WCHAR inChar;
2360 if (dwFlags == SGCM_RTL)
2361 inChar = mirror_char(pwcInChars[i]);
2362 else
2363 inChar = pwcInChars[i];
2364 pwOutGlyphs[i] = inChar;
2367 return hr;
2370 /***********************************************************************
2371 * ScriptTextOut (USP10.@)
2374 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
2375 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
2376 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
2377 const int *piJustify, const GOFFSET *pGoffset)
2379 HRESULT hr = S_OK;
2381 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
2382 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
2383 piAdvance, piJustify, pGoffset);
2385 if (!hdc || !psc) return E_INVALIDARG;
2386 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
2388 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
2389 fuOptions |= ETO_IGNORELANGUAGE;
2390 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
2391 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
2393 if (psa->fRTL && psa->fLogicalOrder)
2395 int i;
2396 WORD *rtlGlyphs;
2398 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
2399 if (!rtlGlyphs)
2400 return E_OUTOFMEMORY;
2402 for (i = 0; i < cGlyphs; i++)
2403 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
2405 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
2406 hr = S_FALSE;
2407 heap_free(rtlGlyphs);
2409 else
2410 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
2411 hr = S_FALSE;
2413 return hr;
2416 /***********************************************************************
2417 * ScriptCacheGetHeight (USP10.@)
2419 * Retrieve the height of the font in the cache.
2421 * PARAMS
2422 * hdc [I] Device context.
2423 * psc [I/O] Opaque pointer to a script cache.
2424 * height [O] Receives font height.
2426 * RETURNS
2427 * Success: S_OK
2428 * Failure: Non-zero HRESULT value.
2430 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
2432 HRESULT hr;
2434 TRACE("(%p, %p, %p)\n", hdc, psc, height);
2436 if (!height) return E_INVALIDARG;
2437 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2439 *height = get_cache_height(psc);
2440 return S_OK;
2443 /***********************************************************************
2444 * ScriptGetGlyphABCWidth (USP10.@)
2446 * Retrieve the width of a glyph.
2448 * PARAMS
2449 * hdc [I] Device context.
2450 * psc [I/O] Opaque pointer to a script cache.
2451 * glyph [I] Glyph to retrieve the width for.
2452 * abc [O] ABC widths of the glyph.
2454 * RETURNS
2455 * Success: S_OK
2456 * Failure: Non-zero HRESULT value.
2458 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
2460 HRESULT hr;
2462 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
2464 if (!abc) return E_INVALIDARG;
2465 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2467 if (!get_cache_glyph_widths(psc, glyph, abc))
2469 if (!hdc) return E_PENDING;
2470 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2472 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
2474 else
2476 INT width;
2477 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
2478 abc->abcB = width;
2479 abc->abcA = abc->abcC = 0;
2481 set_cache_glyph_widths(psc, glyph, abc);
2483 return S_OK;
2486 /***********************************************************************
2487 * ScriptLayout (USP10.@)
2489 * Map embedding levels to visual and/or logical order.
2491 * PARAMS
2492 * runs [I] Size of level array.
2493 * level [I] Array of embedding levels.
2494 * vistolog [O] Map of embedding levels from visual to logical order.
2495 * logtovis [O] Map of embedding levels from logical to visual order.
2497 * RETURNS
2498 * Success: S_OK
2499 * Failure: Non-zero HRESULT value.
2501 * BUGS
2502 * This stub works correctly for any sequence of a single
2503 * embedding level but not for sequences of different
2504 * embedding levels, i.e. mixtures of RTL and LTR scripts.
2506 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
2508 int* indexs;
2509 int ich;
2511 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
2513 if (!level || (!vistolog && !logtovis))
2514 return E_INVALIDARG;
2516 indexs = heap_alloc(sizeof(int) * runs);
2517 if (!indexs)
2518 return E_OUTOFMEMORY;
2521 if (vistolog)
2523 for( ich = 0; ich < runs; ich++)
2524 indexs[ich] = ich;
2526 ich = 0;
2527 while (ich < runs)
2528 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2529 for (ich = 0; ich < runs; ich++)
2530 vistolog[ich] = indexs[ich];
2534 if (logtovis)
2536 for( ich = 0; ich < runs; ich++)
2537 indexs[ich] = ich;
2539 ich = 0;
2540 while (ich < runs)
2541 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
2542 for (ich = 0; ich < runs; ich++)
2543 logtovis[ich] = indexs[ich];
2545 heap_free(indexs);
2547 return S_OK;
2550 /***********************************************************************
2551 * ScriptStringGetLogicalWidths (USP10.@)
2553 * Returns logical widths from a string analysis.
2555 * PARAMS
2556 * ssa [I] string analysis.
2557 * piDx [O] logical widths returned.
2559 * RETURNS
2560 * Success: S_OK
2561 * Failure: a non-zero HRESULT.
2563 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
2565 int i, j, next = 0;
2566 StringAnalysis *analysis = ssa;
2568 TRACE("%p, %p\n", ssa, piDx);
2570 if (!analysis) return S_FALSE;
2571 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
2573 for (i = 0; i < analysis->numItems; i++)
2575 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
2576 int direction = 1;
2578 if (analysis->pItem[i].a.fRTL && ! analysis->pItem[i].a.fLogicalOrder)
2579 direction = -1;
2581 for (j = 0; j < cChar; j++)
2583 int k;
2584 int glyph = analysis->glyphs[i].pwLogClust[j];
2585 int clust_size = get_cluster_size(analysis->glyphs[i].pwLogClust,
2586 cChar, j, direction, NULL, NULL);
2587 for (k = 0; k < clust_size; k++)
2589 piDx[next] = analysis->glyphs[i].piAdvance[glyph] / clust_size;
2590 next++;
2591 if (k) j++;
2595 return S_OK;
2598 /***********************************************************************
2599 * ScriptStringValidate (USP10.@)
2601 * Validate a string analysis.
2603 * PARAMS
2604 * ssa [I] string analysis.
2606 * RETURNS
2607 * Success: S_OK
2608 * Failure: S_FALSE if invalid sequences are found
2609 * or a non-zero HRESULT if it fails.
2611 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
2613 StringAnalysis *analysis = ssa;
2615 TRACE("(%p)\n", ssa);
2617 if (!analysis) return E_INVALIDARG;
2618 return (analysis->invalid) ? S_FALSE : S_OK;
2621 /***********************************************************************
2622 * ScriptString_pSize (USP10.@)
2624 * Retrieve width and height of an analysed string.
2626 * PARAMS
2627 * ssa [I] string analysis.
2629 * RETURNS
2630 * Success: Pointer to a SIZE structure.
2631 * Failure: NULL
2633 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
2635 int i, j;
2636 StringAnalysis *analysis = ssa;
2638 TRACE("(%p)\n", ssa);
2640 if (!analysis) return NULL;
2641 if (!(analysis->dwFlags & SSA_GLYPHS)) return NULL;
2643 if (!analysis->sz)
2645 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
2646 analysis->sz->cy = analysis->glyphs[0].sc->tm.tmHeight;
2648 analysis->sz->cx = 0;
2649 for (i = 0; i < analysis->numItems; i++)
2651 if (analysis->glyphs[i].sc->tm.tmHeight > analysis->sz->cy)
2652 analysis->sz->cy = analysis->glyphs[i].sc->tm.tmHeight;
2653 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
2654 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
2657 return analysis->sz;
2660 /***********************************************************************
2661 * ScriptString_pLogAttr (USP10.@)
2663 * Retrieve logical attributes of an analysed string.
2665 * PARAMS
2666 * ssa [I] string analysis.
2668 * RETURNS
2669 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
2670 * Failure: NULL
2672 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
2674 StringAnalysis *analysis = ssa;
2676 TRACE("(%p)\n", ssa);
2678 if (!analysis) return NULL;
2679 if (!(analysis->dwFlags & SSA_BREAK)) return NULL;
2680 return analysis->logattrs;
2683 /***********************************************************************
2684 * ScriptString_pcOutChars (USP10.@)
2686 * Retrieve the length of a string after clipping.
2688 * PARAMS
2689 * ssa [I] String analysis.
2691 * RETURNS
2692 * Success: Pointer to the length.
2693 * Failure: NULL
2695 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
2697 StringAnalysis *analysis = ssa;
2699 TRACE("(%p)\n", ssa);
2701 if (!analysis) return NULL;
2702 return &analysis->clip_len;
2705 /***********************************************************************
2706 * ScriptStringGetOrder (USP10.@)
2708 * Retrieve a glyph order map.
2710 * PARAMS
2711 * ssa [I] String analysis.
2712 * order [I/O] Array of glyph positions.
2714 * RETURNS
2715 * Success: S_OK
2716 * Failure: a non-zero HRESULT.
2718 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
2720 int i, j;
2721 unsigned int k;
2722 StringAnalysis *analysis = ssa;
2724 TRACE("(%p)\n", ssa);
2726 if (!analysis) return S_FALSE;
2727 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
2729 /* FIXME: handle RTL scripts */
2730 for (i = 0, k = 0; i < analysis->numItems; i++)
2731 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
2732 order[k] = k;
2734 return S_OK;
2737 /***********************************************************************
2738 * ScriptGetLogicalWidths (USP10.@)
2740 * Convert advance widths to logical widths.
2742 * PARAMS
2743 * sa [I] Script analysis.
2744 * nbchars [I] Number of characters.
2745 * nbglyphs [I] Number of glyphs.
2746 * glyph_width [I] Array of glyph widths.
2747 * log_clust [I] Array of logical clusters.
2748 * sva [I] Visual attributes.
2749 * widths [O] Array of logical widths.
2751 * RETURNS
2752 * Success: S_OK
2753 * Failure: a non-zero HRESULT.
2755 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
2756 const int *glyph_width, const WORD *log_clust,
2757 const SCRIPT_VISATTR *sva, int *widths)
2759 int i;
2761 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
2762 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
2764 /* FIXME */
2765 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
2766 return S_OK;
2769 /***********************************************************************
2770 * ScriptApplyLogicalWidth (USP10.@)
2772 * Generate glyph advance widths.
2774 * PARAMS
2775 * dx [I] Array of logical advance widths.
2776 * num_chars [I] Number of characters.
2777 * num_glyphs [I] Number of glyphs.
2778 * log_clust [I] Array of logical clusters.
2779 * sva [I] Visual attributes.
2780 * advance [I] Array of glyph advance widths.
2781 * sa [I] Script analysis.
2782 * abc [I/O] Summed ABC widths.
2783 * justify [O] Array of glyph advance widths.
2785 * RETURNS
2786 * Success: S_OK
2787 * Failure: a non-zero HRESULT.
2789 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
2790 const WORD *log_clust, const SCRIPT_VISATTR *sva,
2791 const int *advance, const SCRIPT_ANALYSIS *sa,
2792 ABC *abc, int *justify)
2794 int i;
2796 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
2797 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
2799 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
2800 return S_OK;
2803 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
2804 int num_glyphs, int dx, int min_kashida, int *justify)
2806 int i;
2808 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
2810 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
2811 return S_OK;