usp10: Add Tifinagh script.
[wine/wine-gecko.git] / dlls / usp10 / usp10.c
blobed482fea384f8881b33c4b79a5fdb0cc75e69c6c
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 /* Myanmar: U+1000–U+109F */
111 { Script_Myanmar, 0x1000, 0x109f, Script_Myanmar_Numeric, 0},
112 /* Georgian: U+10A0–U+10FF */
113 { Script_Georgian, 0x10a0, 0x10ff, 0, 0},
114 /* Hangul Jamo: U+1100–U+11FF */
115 { Script_Hangul, 0x1100, 0x11ff, 0, 0},
116 /* Ethiopic: U+1200–U+137F */
117 /* Ethiopic Extensions: U+1380–U+139F */
118 { Script_Ethiopic, 0x1200, 0x139f, 0, 0},
119 /* Khmer: U+1780–U+17FF */
120 { Script_Khmer, 0x1780, 0x17ff, Script_Khmer_Numeric, 0},
121 /* Mongolian: U+1800–U+18AF */
122 { Script_Mongolian, 0x1800, 0x18af, Script_Mongolian_Numeric, 0},
123 /* Tai Le: U+1950–U+197F */
124 { Script_Tai_Le, 0x1950, 0x197f, 0, 0},
125 /* New Tai Lue: U+1980–U+19DF */
126 { Script_New_Tai_Lue,0x1980, 0x19df, Script_New_Tai_Lue_Numeric, 0},
127 /* Khmer Symbols: U+19E0–U+19FF */
128 { Script_Khmer, 0x19e0, 0x19ff, Script_Khmer_Numeric, 0},
129 /* Vedic Extensions: U+1CD0-U+1CFF */
130 { Script_Devanagari, 0x1cd0, 0x1cff, Script_Devanagari_Numeric, 0},
131 /* Phonetic Extensions: U+1D00–U+1DBF */
132 { Script_Latin, 0x1d00, 0x1dbf, 0, 0},
133 /* Combining Diacritical Marks Supplement: U+1DC0–U+1DFF */
134 { Script_Diacritical,0x1dc0, 0x1dff, 0, 0},
135 /* Latin Extended Additional: U+1E00–U+1EFF */
136 { Script_Latin, 0x1e00, 0x1eff, 0, 0},
137 /* Greek Extended: U+1F00–U+1FFF */
138 { Script_Greek, 0x1f00, 0x1fff, 0, 0},
139 /* General Punctuation: U+2000 –U+206f */
140 { Script_Latin, 0x2000, 0x206f, 0, 0},
141 /* Superscripts and Subscripts : U+2070 –U+209f */
142 /* Currency Symbols : U+20a0 –U+20cf */
143 { Script_Numeric2, 0x2070, 0x2070, 0, 0},
144 { Script_Latin, 0x2071, 0x2073, 0, 0},
145 { Script_Numeric2, 0x2074, 0x2079, 0, 0},
146 { Script_Latin, 0x207a, 0x207f, 0, 0},
147 { Script_Numeric2, 0x2080, 0x2089, 0, 0},
148 { Script_Latin, 0x208a, 0x20cf, 0, 0},
149 /* Letterlike Symbols : U+2100 –U+214f */
150 /* Number Forms : U+2150 –U+218f */
151 /* Arrows : U+2190 –U+21ff */
152 /* Mathematical Operators : U+2200 –U+22ff */
153 /* Miscellaneous Technical : U+2300 –U+23ff */
154 /* Control Pictures : U+2400 –U+243f */
155 /* Optical Character Recognition : U+2440 –U+245f */
156 /* Enclosed Alphanumerics : U+2460 –U+24ff */
157 /* Box Drawing : U+2500 –U+25ff */
158 /* Block Elements : U+2580 –U+259f */
159 /* Geometric Shapes : U+25a0 –U+25ff */
160 /* Miscellaneous Symbols : U+2600 –U+26ff */
161 /* Dingbats : U+2700 –U+27bf */
162 /* Miscellaneous Mathematical Symbols-A : U+27c0 –U+27ef */
163 /* Supplemental Arrows-A : U+27f0 –U+27ff */
164 { Script_Latin, 0x2100, 0x27ff, 0, 0},
165 /* Supplemental Arrows-B : U+2900 –U+297f */
166 /* Miscellaneous Mathematical Symbols-B : U+2980 –U+29ff */
167 /* Supplemental Mathematical Operators : U+2a00 –U+2aff */
168 /* Miscellaneous Symbols and Arrows : U+2b00 –U+2bff */
169 { Script_Latin, 0x2900, 0x2bff, 0, 0},
170 /* Latin Extended-C: U+2C60–U+2C7F */
171 { Script_Latin, 0x2c60, 0x2c7f, 0, 0},
172 /* Georgian: U+2D00–U+2D2F */
173 { Script_Georgian, 0x2d00, 0x2d2f, 0, 0},
174 /* Tifinagh: U+2D30–U+2D7F */
175 { Script_Tifinagh, 0x2d30, 0x2d7f, 0, 0},
176 /* Ethiopic Extensions: U+2D80–U+2DDF */
177 { Script_Ethiopic, 0x2d80, 0x2ddf, 0, 0},
178 /* Cyrillic Extended-A: U+2DE0–U+2DFF */
179 { Script_Cyrillic, 0x2de0, 0x2dff, 0, 0},
180 /* CJK Radicals Supplement: U+2E80–U+2EFF */
181 /* Kangxi Radicals: U+2F00–U+2FDF */
182 { Script_CJK_Han, 0x2e80, 0x2fdf, 0, 0},
183 /* Ideographic Description Characters: U+2FF0–U+2FFF */
184 { Script_Ideograph ,0x2ff0, 0x2fff, 0, 0},
185 /* CJK Symbols and Punctuation: U+3000–U+303F */
186 { Script_Ideograph ,0x3000, 0x3004, 0, 0},
187 { Script_CJK_Han ,0x3005, 0x3005, 0, 0},
188 { Script_Ideograph ,0x3006, 0x3006, 0, 0},
189 { Script_CJK_Han ,0x3007, 0x3007, 0, 0},
190 { Script_Ideograph ,0x3008, 0x3020, 0, 0},
191 { Script_CJK_Han ,0x3021, 0x3029, 0, 0},
192 { Script_Ideograph ,0x302a, 0x3030, 0, 0},
193 /* Kana Marks: */
194 { Script_Kana ,0x3031, 0x3035, 0, 0},
195 { Script_Ideograph ,0x3036, 0x3037, 0, 0},
196 { Script_CJK_Han ,0x3038, 0x303b, 0, 0},
197 { Script_Ideograph ,0x303c, 0x303f, 0, 0},
198 /* Hiragana: U+3040–U+309F */
199 /* Katakana: U+30A0–U+30FF */
200 { Script_Kana ,0x3040, 0x30ff, 0, 0},
201 /* Bopomofo: U+3100–U+312F */
202 { Script_Bopomofo ,0x3100, 0x312f, 0, 0},
203 /* Hangul Compatibility Jamo: U+3130–U+318F */
204 { Script_Hangul ,0x3130, 0x318f, 0, 0},
205 /* Kanbun: U+3190–U+319F */
206 { Script_Ideograph ,0x3190, 0x319f, 0, 0},
207 /* Bopomofo Extended: U+31A0–U+31BF */
208 { Script_Bopomofo ,0x31a0, 0x31bf, 0, 0},
209 /* CJK Strokes: U+31C0–U+31EF */
210 { Script_Ideograph ,0x31c0, 0x31ef, 0, 0},
211 /* Katakana Phonetic Extensions: U+31F0–U+31FF */
212 { Script_Kana ,0x31f0, 0x31ff, 0, 0},
213 /* Enclosed CJK Letters and Months: U+3200–U+32FF */
214 { Script_Hangul ,0x3200, 0x321f, 0, 0},
215 { Script_Ideograph ,0x3220, 0x325f, 0, 0},
216 { Script_Hangul ,0x3260, 0x327f, 0, 0},
217 { Script_Ideograph ,0x3280, 0x32ef, 0, 0},
218 { Script_Kana ,0x32d0, 0x31ff, 0, 0},
219 /* CJK Compatibility: U+3300–U+33FF*/
220 { Script_Kana ,0x3300, 0x3357, 0, 0},
221 { Script_Ideograph ,0x3358, 0x33ff, 0, 0},
222 /* CJK Unified Ideographs Extension A: U+3400–U+4DBF */
223 { Script_CJK_Han ,0x3400, 0x4dbf, 0, 0},
224 /* CJK Unified Ideographs: U+4E00–U+9FFF */
225 { Script_CJK_Han ,0x4e00, 0x9fff, 0, 0},
226 /* Yi: U+A000–U+A4CF */
227 { Script_Yi ,0xa000, 0xa4cf, 0, 0},
228 /* Cyrillic Extended-B: U+A640–U+A69F */
229 { Script_Cyrillic, 0xa640, 0xa69f, 0, 0},
230 /* Modifier Tone Letters: U+A700–U+A71F */
231 /* Latin Extended-D: U+A720–U+A7FF */
232 { Script_Latin, 0xa700, 0xa7ff, 0, 0},
233 /* Phags-pa: U+A840–U+A87F */
234 { Script_Phags_pa, 0xa840, 0xa87f, 0, 0},
235 /* Devanagari Extended: U+A8E0-U+A8FF */
236 { Script_Devanagari, 0xa8e0, 0xa8ff, Script_Devanagari_Numeric, 0},
237 /* Myanmar Extended-A: U+AA60–U+AA7F */
238 { Script_Myanmar, 0xaa60, 0xaa7f, Script_Myanmar_Numeric, 0},
239 /* Hangul Jamo Extended-A: U+A960–U+A97F */
240 { Script_Hangul, 0xa960, 0xa97f, 0, 0},
241 /* Hangul Syllables: U+AC00–U+D7A3 */
242 { Script_Hangul, 0xac00, 0xd7a3, 0, 0},
243 /* Hangul Jamo Extended-B: U+D7B0–U+D7FF */
244 { Script_Hangul, 0xd7b0, 0xd7ff, 0, 0},
245 /* CJK Compatibility Ideographs: U+F900–U+FAFF */
246 { Script_CJK_Han ,0xf900, 0xfaff, 0, 0},
247 /* Latin Ligatures: U+FB00–U+FB06 */
248 { Script_Latin, 0xfb00, 0xfb06, 0, 0},
249 /* Armenian ligatures U+FB13..U+FB17 */
250 { Script_Armenian, 0xfb13, 0xfb17, 0, 0},
251 /* Alphabetic Presentation Forms: U+FB1D–U+FB4F */
252 { Script_Hebrew, 0xfb1d, 0xfb4f, 0, 0},
253 /* Arabic Presentation Forms-A: U+FB50–U+FDFF*/
254 { Script_Arabic, 0xfb50, 0xfdff, 0, 0},
255 /* Vertical Forms: U+FE10–U+FE1F */
256 /* Combining Half Marks: U+FE20–U+FE2F */
257 /* CJK Compatibility Forms: U+FE30–U+FE4F */
258 /* Small Form Variants: U+FE50–U+FE6F */
259 { Script_Ideograph ,0xfe10, 0xfe6f, 0, 0},
260 /* Arabic Presentation Forms-B: U+FE70–U+FEFF*/
261 { Script_Arabic, 0xfe70, 0xfeff, 0, 0},
262 /* Halfwidth and Fullwidth Forms: U+FF00–FFEF */
263 { Script_Ideograph ,0xff00, 0xff64, Script_Numeric2, 0},
264 { Script_Kana ,0xff65, 0xff9f, 0, 0},
265 { Script_Hangul ,0xffa0, 0xffdf, 0, 0},
266 { Script_Ideograph ,0xffe0, 0xffef, 0, 0},
267 /* END */
268 { SCRIPT_UNDEFINED, 0, 0, 0}
271 typedef struct _scriptData
273 SCRIPT_ANALYSIS a;
274 SCRIPT_PROPERTIES props;
275 OPENTYPE_TAG scriptTag;
276 WCHAR fallbackFont[LF_FACESIZE];
277 } scriptData;
279 /* the must be in order so that the index matches the Script value */
280 static const scriptData scriptInformation[] = {
281 {{SCRIPT_UNDEFINED, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
282 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
283 0x00000000,
284 {0}},
285 {{Script_Latin, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
286 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
287 MS_MAKE_TAG('l','a','t','n'),
288 {0}},
289 {{Script_CR, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
290 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
291 0x00000000,
292 {0}},
293 {{Script_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
294 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
295 0x00000000,
296 {0}},
297 {{Script_Control, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
298 {LANG_ENGLISH, 0, 1, 0, 0, ANSI_CHARSET, 1, 0, 0, 0, 0, 0, 1, 0, 0},
299 0x00000000,
300 {0}},
301 {{Script_Punctuation, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
302 {LANG_NEUTRAL, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
303 0x00000000,
304 {0}},
305 {{Script_Arabic, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
306 {LANG_ARABIC, 0, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 1, 0},
307 MS_MAKE_TAG('a','r','a','b'),
308 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
309 {{Script_Arabic_Numeric, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
310 {LANG_ARABIC, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
311 MS_MAKE_TAG('a','r','a','b'),
312 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
313 {{Script_Hebrew, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
314 {LANG_HEBREW, 0, 1, 0, 1, HEBREW_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 MS_MAKE_TAG('h','e','b','r'),
316 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
317 {{Script_Syriac, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
318 {LANG_SYRIAC, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 1, 0},
319 MS_MAKE_TAG('s','y','r','c'),
320 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
321 {{Script_Persian, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
322 {LANG_PERSIAN, 1, 1, 0, 0, ARABIC_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
323 MS_MAKE_TAG('s','y','r','c'),
324 {'E','s','t','r','a','n','g','e','l','o',' ','E','d','e','s','s','a',0}},
325 {{Script_Thaana, 1, 1, 0, 0, 0, 0, { 1,0,0,0,0,0,0,0,0,0,0}},
326 {LANG_DIVEHI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
327 MS_MAKE_TAG('t','h','a','a'),
328 {'M','V',' ','B','o','l','i',0}},
329 {{Script_Greek, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
330 {LANG_GREEK, 0, 0, 0, 0, GREEK_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
331 MS_MAKE_TAG('g','r','e','k'),
332 {0}},
333 {{Script_Cyrillic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
334 {LANG_RUSSIAN, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
335 MS_MAKE_TAG('c','y','r','l'),
336 {0}},
337 {{Script_Armenian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
338 {LANG_ARMENIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
339 MS_MAKE_TAG('a','r','m','n'),
340 {'S','y','l','f','a','e','n',0}},
341 {{Script_Georgian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
342 {LANG_GEORGIAN, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
343 MS_MAKE_TAG('g','e','o','r'),
344 {'S','y','l','f','a','e','n',0}},
345 {{Script_Sinhala, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
346 {LANG_SINHALESE, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
347 MS_MAKE_TAG('s','i','n','h'),
348 {'I','s','k','o','o','l','a',' ','P','o','t','a',0}},
349 {{Script_Tibetan, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
350 {LANG_TIBETAN, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
351 MS_MAKE_TAG('t','i','b','t'),
352 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
353 {{Script_Tibetan_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
354 {LANG_TIBETAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
355 MS_MAKE_TAG('t','i','b','t'),
356 {'M','i','c','r','o','s','o','f','t',' ','H','i','m','a','l','a','y','a',0}},
357 {{Script_Phags_pa, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
358 {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
359 MS_MAKE_TAG('p','h','a','g'),
360 {'M','i','c','r','o','s','o','f','t',' ','P','h','a','g','s','P','a',0}},
361 {{Script_Thai, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
362 {LANG_THAI, 0, 1, 1, 1, THAI_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 1},
363 MS_MAKE_TAG('t','h','a','i'),
364 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
365 {{Script_Thai_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
366 {LANG_THAI, 1, 1, 0, 0, THAI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
367 MS_MAKE_TAG('t','h','a','i'),
368 {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f',0}},
369 {{Script_Lao, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
370 {LANG_LAO, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 1, 0, 1, 0, 0, 0, 0},
371 MS_MAKE_TAG('l','a','o',' '),
372 {'D','o','k','C','h','a','m','p','a',0}},
373 {{Script_Lao_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
374 {LANG_LAO, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
375 MS_MAKE_TAG('l','a','o',' '),
376 {'D','o','k','C','h','a','m','p','a',0}},
377 {{Script_Devanagari, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
378 {LANG_HINDI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
379 MS_MAKE_TAG('d','e','v','a'),
380 {'M','a','n','g','a','l',0}},
381 {{Script_Devanagari_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
382 {LANG_HINDI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
383 MS_MAKE_TAG('d','e','v','a'),
384 {'M','a','n','g','a','l',0}},
385 {{Script_Bengali, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
386 {LANG_BENGALI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
387 MS_MAKE_TAG('b','e','n','g'),
388 {'V','r','i','n','d','a',0}},
389 {{Script_Bengali_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
390 {LANG_BENGALI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
391 MS_MAKE_TAG('b','e','n','g'),
392 {'V','r','i','n','d','a',0}},
393 {{Script_Bengali_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
394 {LANG_BENGALI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
395 MS_MAKE_TAG('b','e','n','g'),
396 {'V','r','i','n','d','a',0}},
397 {{Script_Gurmukhi, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
398 {LANG_PUNJABI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
399 MS_MAKE_TAG('g','u','r','u'),
400 {'R','a','a','v','i',0}},
401 {{Script_Gurmukhi_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
402 {LANG_PUNJABI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
403 MS_MAKE_TAG('g','u','r','u'),
404 {'R','a','a','v','i',0}},
405 {{Script_Gujarati, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
406 {LANG_GUJARATI, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
407 MS_MAKE_TAG('g','u','j','r'),
408 {'S','h','r','u','t','i',0}},
409 {{Script_Gujarati_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
410 {LANG_GUJARATI, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
411 MS_MAKE_TAG('g','u','j','r'),
412 {'S','h','r','u','t','i',0}},
413 {{Script_Gujarati_Currency, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
414 {LANG_GUJARATI, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
415 MS_MAKE_TAG('g','u','j','r'),
416 {'S','h','r','u','t','i',0}},
417 {{Script_Oriya, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
418 {LANG_ORIYA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
419 MS_MAKE_TAG('o','r','y','a'),
420 {'K','a','l','i','n','g','a',0}},
421 {{Script_Oriya_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
422 {LANG_ORIYA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
423 MS_MAKE_TAG('o','r','y','a'),
424 {'K','a','l','i','n','g','a',0}},
425 {{Script_Tamil, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
426 {LANG_TAMIL, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
427 MS_MAKE_TAG('t','a','m','l'),
428 {'L','a','t','h','a',0}},
429 {{Script_Tamil_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
430 {LANG_TAMIL, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
431 MS_MAKE_TAG('t','a','m','l'),
432 {'L','a','t','h','a',0}},
433 {{Script_Telugu, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
434 {LANG_TELUGU, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
435 MS_MAKE_TAG('t','e','l','u'),
436 {'G','a','u','t','a','m','i',0}},
437 {{Script_Telugu_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
438 {LANG_TELUGU, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
439 MS_MAKE_TAG('t','e','l','u'),
440 {'G','a','u','t','a','m','i',0}},
441 {{Script_Kannada, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
442 {LANG_KANNADA, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
443 MS_MAKE_TAG('k','n','d','a'),
444 {'T','u','n','g','a',0}},
445 {{Script_Kannada_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
446 {LANG_KANNADA, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
447 MS_MAKE_TAG('k','n','d','a'),
448 {'T','u','n','g','a',0}},
449 {{Script_Malayalam, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
450 {LANG_MALAYALAM, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
451 MS_MAKE_TAG('m','l','y','m'),
452 {'K','a','r','t','i','k','a',0}},
453 {{Script_Malayalam_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
454 {LANG_MALAYALAM, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
455 MS_MAKE_TAG('m','l','y','m'),
456 {'K','a','r','t','i','k','a',0}},
457 {{Script_Diacritical, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
458 {LANG_ENGLISH, 0, 1, 0, 1, ANSI_CHARSET, 0, 0, 0, 0, 0, 1, 1, 0, 0},
459 0x00000000,
460 {0}},
461 {{Script_Punctuation2, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
462 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
463 MS_MAKE_TAG('l','a','t','n'),
464 {0}},
465 {{Script_Numeric2, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
466 {LANG_ENGLISH, 1, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
467 0x00000000,
468 {0}},
469 {{Script_Myanmar, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
470 {0x55, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
471 MS_MAKE_TAG('m','y','m','r'),
472 {0}},
473 {{Script_Myanmar_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
474 {0x55, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
475 MS_MAKE_TAG('m','y','m','r'),
476 {0}},
477 {{Script_Tai_Le, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
478 {0, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
479 MS_MAKE_TAG('t','a','l','e'),
480 {'M','i','c','r','o','s','o','f','t',' ','T','a','i',' ','L','e'}},
481 {{Script_New_Tai_Lue, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
482 {0, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
483 MS_MAKE_TAG('t','a','l','u'),
484 {'M','i','c','r','o','s','o','f','t',' ','N','e','w',' ','T','a','i',' ','L','u','e'}},
485 {{Script_New_Tai_Lue_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
486 {0, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
487 MS_MAKE_TAG('t','a','l','u'),
488 {'M','i','c','r','o','s','o','f','t',' ','N','e','w',' ','T','a','i',' ','L','u','e'}},
489 {{Script_Khmer, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
490 {0x53, 0, 1, 1, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 1, 0, 0, 0, 0},
491 MS_MAKE_TAG('k','h','m','r'),
492 {'D','a','u','n','P','e','n','h'}},
493 {{Script_Khmer, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
494 {0x53, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
495 MS_MAKE_TAG('k','h','m','r'),
496 {'D','a','u','n','P','e','n','h'}},
497 {{Script_CJK_Han, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
498 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
499 MS_MAKE_TAG('h','a','n','i'),
500 {0}},
501 {{Script_Ideograph, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
502 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
503 MS_MAKE_TAG('h','a','n','i'),
504 {0}},
505 {{Script_Bopomofo, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
506 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
507 MS_MAKE_TAG('b','o','p','o'),
508 {0}},
509 {{Script_Kana, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
510 {LANG_ENGLISH, 0, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
511 MS_MAKE_TAG('k','a','n','a'),
512 {0}},
513 {{Script_Hangul, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
514 {LANG_KOREAN, 0, 1, 0, 1, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
515 MS_MAKE_TAG('h','a','n','g'),
516 {0}},
517 {{Script_Yi, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
518 {LANG_ENGLISH, 0, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 1, 0, 0},
519 MS_MAKE_TAG('y','i',' ',' '),
520 {'M','i','c','r','o','s','o','f','t',' ','Y','i',' ','B','a','i','t','i'}},
521 {{Script_Ethiopic, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
522 {0x5e, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
523 MS_MAKE_TAG('e','t','h','i'),
524 {'N','y','a','l','a'}},
525 {{Script_Ethiopic_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
526 {0x5e, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
527 MS_MAKE_TAG('e','t','h','i'),
528 {'N','y','a','l','a'}},
529 {{Script_Mongolian, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
530 {LANG_MONGOLIAN, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
531 MS_MAKE_TAG('m','o','n','g'),
532 {'M','o','n','g','o','l','i','a','n',' ','B','a','i','t','i'}},
533 {{Script_Mongolian_Numeric, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
534 {LANG_MONGOLIAN, 1, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
535 MS_MAKE_TAG('m','o','n','g'),
536 {'M','o','n','g','o','l','i','a','n',' ','B','a','i','t','i'}},
537 {{Script_Tifinagh, 0, 0, 0, 0, 0, 0, { 0,0,0,0,0,0,0,0,0,0,0}},
538 {0, 0, 1, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, 0, 0, 0, 0, 0},
539 MS_MAKE_TAG('t','f','n','g'),
540 {'E','b','r','i','m','a'}},
543 static const SCRIPT_PROPERTIES *script_props[] =
545 &scriptInformation[0].props, &scriptInformation[1].props,
546 &scriptInformation[2].props, &scriptInformation[3].props,
547 &scriptInformation[4].props, &scriptInformation[5].props,
548 &scriptInformation[6].props, &scriptInformation[7].props,
549 &scriptInformation[8].props, &scriptInformation[9].props,
550 &scriptInformation[10].props, &scriptInformation[11].props,
551 &scriptInformation[12].props, &scriptInformation[13].props,
552 &scriptInformation[14].props, &scriptInformation[15].props,
553 &scriptInformation[16].props, &scriptInformation[17].props,
554 &scriptInformation[18].props, &scriptInformation[19].props,
555 &scriptInformation[20].props, &scriptInformation[21].props,
556 &scriptInformation[22].props, &scriptInformation[23].props,
557 &scriptInformation[24].props, &scriptInformation[25].props,
558 &scriptInformation[26].props, &scriptInformation[27].props,
559 &scriptInformation[28].props, &scriptInformation[29].props,
560 &scriptInformation[30].props, &scriptInformation[31].props,
561 &scriptInformation[32].props, &scriptInformation[33].props,
562 &scriptInformation[34].props, &scriptInformation[35].props,
563 &scriptInformation[36].props, &scriptInformation[37].props,
564 &scriptInformation[38].props, &scriptInformation[39].props,
565 &scriptInformation[40].props, &scriptInformation[41].props,
566 &scriptInformation[42].props, &scriptInformation[43].props,
567 &scriptInformation[44].props, &scriptInformation[45].props,
568 &scriptInformation[46].props, &scriptInformation[47].props,
569 &scriptInformation[48].props, &scriptInformation[49].props,
570 &scriptInformation[50].props, &scriptInformation[51].props,
571 &scriptInformation[52].props, &scriptInformation[53].props,
572 &scriptInformation[54].props, &scriptInformation[55].props,
573 &scriptInformation[56].props, &scriptInformation[57].props,
574 &scriptInformation[58].props, &scriptInformation[59].props,
575 &scriptInformation[60].props, &scriptInformation[61].props,
576 &scriptInformation[62].props, &scriptInformation[63].props,
577 &scriptInformation[64].props
580 typedef struct {
581 ScriptCache *sc;
582 int numGlyphs;
583 WORD* glyphs;
584 WORD* pwLogClust;
585 int* piAdvance;
586 SCRIPT_VISATTR* psva;
587 GOFFSET* pGoffset;
588 ABC* abc;
589 int iMaxPosX;
590 HFONT fallbackFont;
591 } StringGlyphs;
593 typedef struct {
594 HDC hdc;
595 DWORD dwFlags;
596 BOOL invalid;
597 int clip_len;
598 int cItems;
599 int cMaxGlyphs;
600 SCRIPT_ITEM* pItem;
601 int numItems;
602 StringGlyphs* glyphs;
603 SCRIPT_LOGATTR* logattrs;
604 SIZE* sz;
605 int* logical2visual;
606 } StringAnalysis;
608 static inline void *heap_alloc(SIZE_T size)
610 return HeapAlloc(GetProcessHeap(), 0, size);
613 static inline void *heap_alloc_zero(SIZE_T size)
615 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
618 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
620 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
623 static inline BOOL heap_free(LPVOID mem)
625 return HeapFree(GetProcessHeap(), 0, mem);
628 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
630 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
633 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
635 return ((ScriptCache *)*psc)->tm.tmHeight;
638 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
640 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
643 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
645 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
647 if (!block) return 0;
648 return block[c & GLYPH_BLOCK_MASK];
651 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
653 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
655 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
656 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
659 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
661 static const ABC nil;
662 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
664 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
665 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
666 return TRUE;
669 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
671 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
673 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
674 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
675 return TRUE;
678 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
680 ScriptCache *sc;
682 if (!psc) return E_INVALIDARG;
683 if (*psc) return S_OK;
684 if (!hdc) return E_PENDING;
686 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
687 if (!GetTextMetricsW(hdc, &sc->tm))
689 heap_free(sc);
690 return E_INVALIDARG;
692 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
694 heap_free(sc);
695 return E_INVALIDARG;
697 sc->sfnt = (GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, NULL, 0)!=GDI_ERROR);
698 *psc = sc;
699 TRACE("<- %p\n", sc);
700 return S_OK;
703 static WCHAR mirror_char( WCHAR ch )
705 extern const WCHAR wine_mirror_map[];
706 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
709 static WORD get_char_script( WCHAR ch)
711 static const WCHAR latin_punc[] = {'#','$','&','\'',',',';','<','>','?','@','\\','^','_','`','{','|','}','~', 0x00a0, 0};
712 WORD type = 0;
713 int i;
715 if (ch == 0xc || ch == 0x20 || ch == 0x202f)
716 return Script_CR;
718 /* These punctuation are separated out as Latin punctuation */
719 if (strchrW(latin_punc,ch))
720 return Script_Punctuation2;
722 /* These chars are itemized as Punctuation by Windows */
723 if (ch == 0x2212 || ch == 0x2044)
724 return Script_Punctuation;
726 GetStringTypeW(CT_CTYPE1, &ch, 1, &type);
728 if (type == 0)
729 return SCRIPT_UNDEFINED;
731 if (type & C1_CNTRL)
732 return Script_Control;
734 i = 0;
737 if (ch < scriptRanges[i].rangeFirst || scriptRanges[i].script == SCRIPT_UNDEFINED)
738 break;
740 if (ch >= scriptRanges[i].rangeFirst && ch <= scriptRanges[i].rangeLast)
742 if (scriptRanges[i].numericScript && type & C1_DIGIT)
743 return scriptRanges[i].numericScript;
744 if (scriptRanges[i].punctScript && type & C1_PUNCT)
745 return scriptRanges[i].punctScript;
746 return scriptRanges[i].script;
748 i++;
749 } while (1);
751 return SCRIPT_UNDEFINED;
754 /***********************************************************************
755 * DllMain
758 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
760 switch(fdwReason)
762 case DLL_PROCESS_ATTACH:
763 DisableThreadLibraryCalls(hInstDLL);
764 break;
765 case DLL_PROCESS_DETACH:
766 break;
768 return TRUE;
771 /***********************************************************************
772 * ScriptFreeCache (USP10.@)
774 * Free a script cache.
776 * PARAMS
777 * psc [I/O] Script cache.
779 * RETURNS
780 * Success: S_OK
781 * Failure: Non-zero HRESULT value.
783 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
785 TRACE("%p\n", psc);
787 if (psc && *psc)
789 unsigned int i;
790 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
792 heap_free(((ScriptCache *)*psc)->glyphs[i]);
793 heap_free(((ScriptCache *)*psc)->widths[i]);
795 heap_free(((ScriptCache *)*psc)->GSUB_Table);
796 heap_free(((ScriptCache *)*psc)->GDEF_Table);
797 heap_free(((ScriptCache *)*psc)->features);
798 heap_free(*psc);
799 *psc = NULL;
801 return S_OK;
804 /***********************************************************************
805 * ScriptGetProperties (USP10.@)
807 * Retrieve a list of script properties.
809 * PARAMS
810 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
811 * num [I] Pointer to the number of scripts.
813 * RETURNS
814 * Success: S_OK
815 * Failure: Non-zero HRESULT value.
817 * NOTES
818 * Behaviour matches WinXP.
820 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
822 TRACE("(%p,%p)\n", props, num);
824 if (!props && !num) return E_INVALIDARG;
826 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
827 if (props) *props = script_props;
829 return S_OK;
832 /***********************************************************************
833 * ScriptGetFontProperties (USP10.@)
835 * Get information on special glyphs.
837 * PARAMS
838 * hdc [I] Device context.
839 * psc [I/O] Opaque pointer to a script cache.
840 * sfp [O] Font properties structure.
842 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
844 HRESULT hr;
846 TRACE("%p,%p,%p\n", hdc, psc, sfp);
848 if (!sfp) return E_INVALIDARG;
849 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
851 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
852 return E_INVALIDARG;
854 /* return something sensible? */
855 sfp->wgBlank = 0;
856 sfp->wgDefault = get_cache_default_char(psc);
857 sfp->wgInvalid = 0;
858 sfp->wgKashida = 0xffff;
859 sfp->iKashidaWidth = 0;
861 return S_OK;
864 /***********************************************************************
865 * ScriptRecordDigitSubstitution (USP10.@)
867 * Record digit substitution settings for a given locale.
869 * PARAMS
870 * locale [I] Locale identifier.
871 * sds [I] Structure to record substitution settings.
873 * RETURNS
874 * Success: S_OK
875 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
877 * SEE ALSO
878 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
880 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
882 DWORD plgid, sub;
884 TRACE("0x%x, %p\n", locale, sds);
886 /* This implementation appears to be correct for all languages, but it's
887 * not clear if sds->DigitSubstitute is ever set to anything except
888 * CONTEXT or NONE in reality */
890 if (!sds) return E_POINTER;
892 locale = ConvertDefaultLocale(locale);
894 if (!IsValidLocale(locale, LCID_INSTALLED))
895 return E_INVALIDARG;
897 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
898 sds->TraditionalDigitLanguage = plgid;
900 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
901 sds->NationalDigitLanguage = plgid;
902 else
903 sds->NationalDigitLanguage = LANG_ENGLISH;
905 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
906 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
908 switch (sub)
910 case 0:
911 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
912 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
913 else
914 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
915 break;
916 case 1:
917 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
918 break;
919 case 2:
920 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
921 break;
922 default:
923 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
924 break;
927 sds->dwReserved = 0;
928 return S_OK;
931 /***********************************************************************
932 * ScriptApplyDigitSubstitution (USP10.@)
934 * Apply digit substitution settings.
936 * PARAMS
937 * sds [I] Structure with recorded substitution settings.
938 * sc [I] Script control structure.
939 * ss [I] Script state structure.
941 * RETURNS
942 * Success: S_OK
943 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
945 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
946 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
948 SCRIPT_DIGITSUBSTITUTE psds;
950 TRACE("%p, %p, %p\n", sds, sc, ss);
952 if (!sc || !ss) return E_POINTER;
953 if (!sds)
955 sds = &psds;
956 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
957 return E_INVALIDARG;
960 sc->uDefaultLanguage = LANG_ENGLISH;
961 sc->fContextDigits = 0;
962 ss->fDigitSubstitute = 0;
964 switch (sds->DigitSubstitute) {
965 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
966 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
967 case SCRIPT_DIGITSUBSTITUTE_NONE:
968 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
969 return S_OK;
970 default:
971 return E_INVALIDARG;
975 static inline BOOL is_indic(WORD script)
977 return (script >= Script_Devanagari && script <= Script_Malayalam_Numeric);
980 static inline WORD base_indic(WORD script)
982 switch (script)
984 case Script_Devanagari:
985 case Script_Devanagari_Numeric: return Script_Devanagari;
986 case Script_Bengali:
987 case Script_Bengali_Numeric:
988 case Script_Bengali_Currency: return Script_Bengali;
989 case Script_Gurmukhi:
990 case Script_Gurmukhi_Numeric: return Script_Gurmukhi;
991 case Script_Gujarati:
992 case Script_Gujarati_Numeric:
993 case Script_Gujarati_Currency: return Script_Gujarati;
994 case Script_Oriya:
995 case Script_Oriya_Numeric: return Script_Oriya;
996 case Script_Tamil:
997 case Script_Tamil_Numeric: return Script_Tamil;
998 case Script_Telugu:
999 case Script_Telugu_Numeric: return Script_Telugu;
1000 case Script_Kannada:
1001 case Script_Kannada_Numeric: return Script_Kannada;
1002 case Script_Malayalam:
1003 case Script_Malayalam_Numeric: return Script_Malayalam;
1004 default:
1005 return -1;
1009 /***********************************************************************
1010 * ScriptItemizeOpenType (USP10.@)
1012 * Split a Unicode string into shapeable parts.
1014 * PARAMS
1015 * pwcInChars [I] String to split.
1016 * cInChars [I] Number of characters in pwcInChars.
1017 * cMaxItems [I] Maximum number of items to return.
1018 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
1019 * psState [I] Pointer to a SCRIPT_STATE structure.
1020 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
1021 * pScriptTags [O] Buffer to receive OPENTYPE_TAGs.
1022 * pcItems [O] Number of script items returned.
1024 * RETURNS
1025 * Success: S_OK
1026 * Failure: Non-zero HRESULT value.
1028 HRESULT WINAPI ScriptItemizeOpenType(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
1029 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
1030 SCRIPT_ITEM *pItems, OPENTYPE_TAG *pScriptTags, int *pcItems)
1033 #define Numeric_space 0x0020
1034 #define ZWNJ 0x200C
1035 #define ZWJ 0x200D
1037 int cnt = 0, index = 0, str = 0;
1038 int New_Script = -1;
1039 int i;
1040 WORD *levels = NULL;
1041 WORD *strength = NULL;
1042 WORD *scripts = NULL;
1043 WORD baselevel = 0;
1044 BOOL new_run;
1045 WORD last_indic = -1;
1046 WORD layoutRTL = 0;
1047 BOOL forceLevels = FALSE;
1049 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
1050 psControl, psState, pItems, pcItems);
1052 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
1053 return E_INVALIDARG;
1055 scripts = heap_alloc(cInChars * sizeof(WORD));
1056 if (!scripts)
1057 return E_OUTOFMEMORY;
1059 for (i = 0; i < cInChars; i++)
1061 scripts[i] = get_char_script(pwcInChars[i]);
1062 /* Devanagari danda (U+0964) and double danda (U+0965) are used for
1063 all Indic scripts */
1064 if ((pwcInChars[i] == 0x964 || pwcInChars[i] ==0x965) && last_indic > 0)
1065 scripts[i] = last_indic;
1066 else if (is_indic(scripts[i]))
1067 last_indic = base_indic(scripts[i]);
1069 /* Some unicode points (Zero Width Space U+200B -
1070 Right-to-Left Mark U+200F) will force us into bidi mode */
1071 if (!forceLevels && pwcInChars[i] >= 0x200B && pwcInChars[i] <= 0x200F)
1072 forceLevels = TRUE;
1074 /* Diacritical marks merge with other scripts */
1075 if (scripts[i] == Script_Diacritical && i > 0)
1076 scripts[i] = scripts[i-1];
1079 for (i = 0; i < cInChars; i++)
1081 /* Joiners get merged preferencially right */
1082 if (i > 0 && (pwcInChars[i] == ZWJ || pwcInChars[i] == ZWNJ))
1084 int j;
1085 if (i+1 == cInChars)
1086 scripts[i] = scripts[i-1];
1087 else
1089 for (j = i+1; j < cInChars; j++)
1091 if (pwcInChars[j] != ZWJ && pwcInChars[j] != ZWNJ && pwcInChars[j] != Numeric_space)
1093 scripts[i] = scripts[j];
1094 break;
1101 if (psState && psControl)
1103 levels = heap_alloc_zero(cInChars * sizeof(WORD));
1104 if (!levels)
1106 heap_free(scripts);
1107 return E_OUTOFMEMORY;
1110 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
1111 baselevel = levels[0];
1112 for (i = 0; i < cInChars; i++)
1113 if (levels[i]!=levels[0])
1114 break;
1115 if (i >= cInChars && !odd(baselevel) && !odd(psState->uBidiLevel) && !forceLevels)
1117 heap_free(levels);
1118 levels = NULL;
1120 else
1122 BOOL inNumber = FALSE;
1123 static WCHAR math_punc[] = {'#','$','%','+',',','-','.','/',':',0x2212, 0x2044, 0x00a0,0};
1125 strength = heap_alloc_zero(cInChars * sizeof(WORD));
1126 if (!strength)
1128 heap_free(scripts);
1129 heap_free(levels);
1130 return E_OUTOFMEMORY;
1132 BIDI_GetStrengths(pwcInChars, cInChars, psControl, strength);
1134 /* We currently mis-level leading Diacriticals */
1135 if (scripts[0] == Script_Diacritical)
1136 for (i = 0; i < cInChars && scripts[0] == Script_Diacritical; i++)
1138 levels[i] = odd(levels[i])?levels[i]+1:levels[i];
1139 strength[i] = BIDI_STRONG;
1142 for (i = 0; i < cInChars; i++)
1144 /* Script_Numeric and select puncuation at level 0 get bumped to level 2 */
1145 if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && inNumber && strchrW(math_punc,pwcInChars[i]))
1147 scripts[i] = Script_Numeric;
1148 levels[i] = 2;
1150 else if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && scripts[i] == Script_Numeric)
1152 levels[i] = 2;
1153 inNumber = TRUE;
1155 else
1156 inNumber = FALSE;
1158 /* Joiners get merged preferencially right */
1159 if (i > 0 && (pwcInChars[i] == ZWJ || pwcInChars[i] == ZWNJ))
1161 int j;
1162 if (i+1 == cInChars && levels[i-1] == levels[i])
1163 strength[i] = strength[i-1];
1164 else
1165 for (j = i+1; j < cInChars && levels[i] == levels[j]; j++)
1166 if (pwcInChars[j] != ZWJ && pwcInChars[j] != ZWNJ && pwcInChars[j] != Numeric_space)
1168 strength[i] = strength[j];
1169 break;
1173 if (psControl->fMergeNeutralItems)
1175 /* Merge the neutrals */
1176 for (i = 0; i < cInChars; i++)
1178 if (strength[i] == BIDI_NEUTRAL || strength[i] == BIDI_WEAK)
1180 int j;
1181 for (j = i; j > 0; j--)
1183 if (levels[i] != levels[j])
1184 break;
1185 if ((strength[j] == BIDI_STRONG) || (strength[i] == BIDI_NEUTRAL && strength[j] == BIDI_WEAK))
1187 scripts[i] = scripts[j];
1188 strength[i] = strength[j];
1189 break;
1193 /* Try going the other way */
1194 if (strength[i] == BIDI_NEUTRAL || strength[i] == BIDI_WEAK)
1196 int j;
1197 for (j = i; j < cInChars; j++)
1199 if (levels[i] != levels[j])
1200 break;
1201 if ((strength[j] == BIDI_STRONG) || (strength[i] == BIDI_NEUTRAL && strength[j] == BIDI_WEAK))
1203 scripts[i] = scripts[j];
1204 strength[i] = strength[j];
1205 break;
1214 while ((!levels || (levels && levels[cnt+1] == levels[0])) && (pwcInChars[cnt] == Numeric_space) && cnt < cInChars)
1215 cnt++;
1217 if (cnt == cInChars) /* All Spaces */
1219 cnt = 0;
1220 New_Script = scripts[cnt];
1223 pItems[index].iCharPos = 0;
1224 pItems[index].a = scriptInformation[scripts[cnt]].a;
1225 pScriptTags[index] = scriptInformation[scripts[cnt]].scriptTag;
1227 if (strength && strength[cnt] == BIDI_STRONG)
1228 str = strength[cnt];
1229 else if (strength)
1230 str = strength[0];
1232 cnt = 0;
1234 if (levels)
1236 if (strength[cnt] == BIDI_STRONG)
1237 layoutRTL = (odd(levels[cnt]))?1:0;
1238 else
1239 layoutRTL = (psState->uBidiLevel || odd(levels[cnt]))?1:0;
1240 pItems[index].a.fRTL = odd(levels[cnt]);
1241 pItems[index].a.fLayoutRTL = layoutRTL;
1242 pItems[index].a.s.uBidiLevel = levels[cnt];
1244 else if (!pItems[index].a.s.uBidiLevel)
1246 layoutRTL = (odd(baselevel))?1:0;
1247 pItems[index].a.s.uBidiLevel = baselevel;
1248 pItems[index].a.fLayoutRTL = odd(baselevel);
1249 pItems[index].a.fRTL = odd(baselevel);
1252 TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
1253 levels?levels[cnt]:-1, str, New_Script, pItems[index].a.eScript, index, cnt,
1254 pItems[index].iCharPos);
1256 for (cnt=1; cnt < cInChars; cnt++)
1258 if(pwcInChars[cnt] != Numeric_space)
1259 New_Script = scripts[cnt];
1260 else if (levels)
1262 int j = 1;
1263 while (cnt + j < cInChars - 1 && pwcInChars[cnt+j] == Numeric_space && levels[cnt] == levels[cnt+j])
1264 j++;
1265 if (cnt + j < cInChars && levels[cnt] == levels[cnt+j])
1266 New_Script = scripts[cnt+j];
1267 else
1268 New_Script = scripts[cnt];
1271 new_run = FALSE;
1272 /* merge space strengths*/
1273 if (strength && strength[cnt] == BIDI_STRONG && str != BIDI_STRONG && New_Script == pItems[index].a.eScript)
1274 str = BIDI_STRONG;
1276 if (strength && strength[cnt] == BIDI_NEUTRAL && str == BIDI_STRONG && pwcInChars[cnt] != Numeric_space && New_Script == pItems[index].a.eScript)
1277 str = BIDI_NEUTRAL;
1279 /* changes in level */
1280 if (levels && (levels[cnt] != pItems[index].a.s.uBidiLevel))
1282 TRACE("Level break(%i/%i)\n",pItems[index].a.s.uBidiLevel,levels[cnt]);
1283 new_run = TRUE;
1285 /* changes in strength */
1286 else if (strength && pwcInChars[cnt] != Numeric_space && str != strength[cnt])
1288 TRACE("Strength break (%i/%i)\n",str,strength[cnt]);
1289 new_run = TRUE;
1291 /* changes in script */
1292 else if (((pwcInChars[cnt] != Numeric_space) && (New_Script != -1) && (New_Script != pItems[index].a.eScript)) || (New_Script == Script_Control))
1294 TRACE("Script break(%i/%i)\n",pItems[index].a.eScript,New_Script);
1295 new_run = TRUE;
1298 if (!new_run && strength && str == BIDI_STRONG)
1300 layoutRTL = odd(levels[cnt])?1:0;
1301 pItems[index].a.fLayoutRTL = layoutRTL;
1304 if (new_run)
1306 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);
1308 index++;
1309 if (index+1 > cMaxItems)
1310 return E_OUTOFMEMORY;
1312 if (strength)
1313 str = strength[cnt];
1315 pItems[index].iCharPos = cnt;
1316 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
1318 pItems[index].a = scriptInformation[New_Script].a;
1319 pScriptTags[index] = scriptInformation[New_Script].scriptTag;
1320 if (levels)
1322 if (levels[cnt] == 0)
1323 layoutRTL = 0;
1324 else
1325 layoutRTL = (layoutRTL || odd(levels[cnt]))?1:0;
1326 pItems[index].a.fRTL = odd(levels[cnt]);
1327 pItems[index].a.fLayoutRTL = layoutRTL;
1328 pItems[index].a.s.uBidiLevel = levels[cnt];
1330 else if (!pItems[index].a.s.uBidiLevel)
1332 pItems[index].a.s.uBidiLevel = baselevel;
1333 pItems[index].a.fLayoutRTL = layoutRTL;
1334 pItems[index].a.fRTL = odd(baselevel);
1337 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
1341 /* While not strictly necessary according to the spec, make sure the n+1
1342 * item is set up to prevent random behaviour if the caller erroneously
1343 * checks the n+1 structure */
1344 index++;
1345 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
1347 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
1349 /* Set one SCRIPT_STATE item being returned */
1350 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
1351 if (pcItems) *pcItems = index;
1353 /* Set SCRIPT_ITEM */
1354 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
1355 heap_free(levels);
1356 heap_free(strength);
1357 heap_free(scripts);
1358 return S_OK;
1361 /***********************************************************************
1362 * ScriptItemize (USP10.@)
1364 * Split a Unicode string into shapeable parts.
1366 * PARAMS
1367 * pwcInChars [I] String to split.
1368 * cInChars [I] Number of characters in pwcInChars.
1369 * cMaxItems [I] Maximum number of items to return.
1370 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
1371 * psState [I] Pointer to a SCRIPT_STATE structure.
1372 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
1373 * pcItems [O] Number of script items returned.
1375 * RETURNS
1376 * Success: S_OK
1377 * Failure: Non-zero HRESULT value.
1379 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
1380 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
1381 SCRIPT_ITEM *pItems, int *pcItems)
1383 OPENTYPE_TAG *discarded_tags;
1384 HRESULT res;
1386 discarded_tags = heap_alloc(cMaxItems * sizeof(OPENTYPE_TAG));
1387 if (!discarded_tags)
1388 return E_OUTOFMEMORY;
1389 res = ScriptItemizeOpenType(pwcInChars, cInChars, cMaxItems, psControl, psState, pItems, discarded_tags, pcItems);
1390 heap_free(discarded_tags);
1391 return res;
1394 static inline int getGivenTabWidth(ScriptCache *psc, SCRIPT_TABDEF *pTabdef, int charPos, int current_x)
1396 int defWidth;
1397 int cTabStops=0;
1398 INT *lpTabPos = NULL;
1399 INT nTabOrg = 0;
1400 INT x = 0;
1402 if (pTabdef)
1403 lpTabPos = pTabdef->pTabStops;
1405 if (pTabdef && pTabdef->iTabOrigin)
1407 if (pTabdef->iScale)
1408 nTabOrg = (pTabdef->iTabOrigin * pTabdef->iScale)/4;
1409 else
1410 nTabOrg = pTabdef->iTabOrigin * psc->tm.tmAveCharWidth;
1413 if (pTabdef)
1414 cTabStops = pTabdef->cTabStops;
1416 if (cTabStops == 1)
1418 if (pTabdef->iScale)
1419 defWidth = ((pTabdef->pTabStops[0])*pTabdef->iScale) / 4;
1420 else
1421 defWidth = (pTabdef->pTabStops[0])*psc->tm.tmAveCharWidth;
1422 cTabStops = 0;
1424 else
1425 defWidth = 8 * psc->tm.tmAveCharWidth;
1427 for (; cTabStops>0 ; lpTabPos++, cTabStops--)
1429 int position = *lpTabPos;
1430 if (position < 0)
1431 position = -1 * position;
1432 if (pTabdef->iScale)
1433 position = (position * pTabdef->iScale) / 4;
1434 else
1435 position = position * psc->tm.tmAveCharWidth;
1437 if( nTabOrg + position > current_x)
1439 if( *lpTabPos >= 0)
1441 /* a left aligned tab */
1442 x = (nTabOrg + *lpTabPos) - current_x;
1443 break;
1445 else
1447 FIXME("Negative tabstop\n");
1448 break;
1452 if ((!cTabStops) && (defWidth > 0))
1453 x =((((current_x - nTabOrg) / defWidth)+1) * defWidth) - current_x;
1454 else if ((!cTabStops) && (defWidth < 0))
1455 FIXME("TODO: Negative defWidth\n");
1457 return x;
1460 /***********************************************************************
1461 * Helper function for ScriptStringAnalyse
1463 static BOOL requires_fallback(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
1464 const WCHAR *pwcInChars, int cChars )
1466 /* FIXME: When to properly fallback is still a bit of a mystery */
1467 WORD *glyphs;
1469 if (psa->fNoGlyphIndex)
1470 return FALSE;
1472 if (init_script_cache(hdc, psc) != S_OK)
1473 return FALSE;
1475 if (SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa) != S_OK)
1476 return TRUE;
1478 glyphs = heap_alloc(sizeof(WORD) * cChars);
1479 if (!glyphs)
1480 return FALSE;
1481 if (ScriptGetCMap(hdc, psc, pwcInChars, cChars, 0, glyphs) != S_OK)
1483 heap_free(glyphs);
1484 return TRUE;
1486 heap_free(glyphs);
1488 return FALSE;
1491 static void find_fallback_font(DWORD scriptid, LPWSTR FaceName)
1493 HKEY hkey;
1495 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Uniscribe\\Fallback", &hkey))
1497 static const WCHAR szFmt[] = {'%','x',0};
1498 WCHAR value[10];
1499 DWORD count = LF_FACESIZE * sizeof(WCHAR);
1500 DWORD type;
1502 sprintfW(value, szFmt, scriptInformation[scriptid].scriptTag);
1503 if (RegQueryValueExW(hkey, value, 0, &type, (LPBYTE)FaceName, &count))
1504 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1505 RegCloseKey(hkey);
1507 else
1508 lstrcpyW(FaceName,scriptInformation[scriptid].fallbackFont);
1511 /***********************************************************************
1512 * ScriptStringAnalyse (USP10.@)
1515 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
1516 int cGlyphs, int iCharset, DWORD dwFlags,
1517 int iReqWidth, SCRIPT_CONTROL *psControl,
1518 SCRIPT_STATE *psState, const int *piDx,
1519 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
1520 SCRIPT_STRING_ANALYSIS *pssa)
1522 HRESULT hr = E_OUTOFMEMORY;
1523 StringAnalysis *analysis = NULL;
1524 SCRIPT_CONTROL sControl;
1525 SCRIPT_STATE sState;
1526 int i, num_items = 255;
1527 BYTE *BidiLevel;
1528 WCHAR *iString = NULL;
1530 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
1531 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
1532 psControl, psState, piDx, pTabdef, pbInClass, pssa);
1534 if (iCharset != -1)
1536 FIXME("Only Unicode strings are supported\n");
1537 return E_INVALIDARG;
1539 if (cString < 1 || !pString) return E_INVALIDARG;
1540 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
1542 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
1543 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
1545 /* FIXME: handle clipping */
1546 analysis->clip_len = cString;
1547 analysis->hdc = hdc;
1548 analysis->dwFlags = dwFlags;
1550 if (psState)
1551 sState = *psState;
1552 else
1553 memset(&sState, 0, sizeof(SCRIPT_STATE));
1555 if (psControl)
1556 sControl = *psControl;
1557 else
1558 memset(&sControl, 0, sizeof(SCRIPT_CONTROL));
1560 if (dwFlags & SSA_PASSWORD)
1562 iString = heap_alloc(sizeof(WCHAR)*cString);
1563 if (!iString)
1565 hr = E_OUTOFMEMORY;
1566 goto error;
1568 for (i = 0; i < cString; i++)
1569 iString[i] = *((const WCHAR *)pString);
1570 pString = iString;
1573 hr = ScriptItemize(pString, cString, num_items, &sControl, &sState, analysis->pItem,
1574 &analysis->numItems);
1576 while (hr == E_OUTOFMEMORY)
1578 SCRIPT_ITEM *tmp;
1580 num_items *= 2;
1581 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
1582 goto error;
1584 analysis->pItem = tmp;
1585 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
1586 &analysis->numItems);
1588 if (hr != S_OK) goto error;
1590 /* set back to out of memory for default goto error behaviour */
1591 hr = E_OUTOFMEMORY;
1593 if (dwFlags & SSA_BREAK)
1595 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
1597 for (i = 0; i < analysis->numItems; i++)
1598 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]);
1600 else
1601 goto error;
1604 if (!(analysis->logical2visual = heap_alloc_zero(sizeof(int) * analysis->numItems)))
1605 goto error;
1606 if (!(BidiLevel = heap_alloc_zero(analysis->numItems)))
1607 goto error;
1609 if (dwFlags & SSA_GLYPHS)
1611 int tab_x = 0;
1612 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
1614 heap_free(BidiLevel);
1615 goto error;
1618 for (i = 0; i < analysis->numItems; i++)
1620 SCRIPT_CACHE *sc = (SCRIPT_CACHE*)&analysis->glyphs[i].sc;
1621 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1622 int numGlyphs = 1.5 * cChar + 16;
1623 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
1624 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
1625 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
1626 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * numGlyphs);
1627 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
1628 ABC *abc = heap_alloc_zero(sizeof(ABC));
1629 int numGlyphsReturned;
1630 HFONT originalFont = 0x0;
1632 /* FIXME: non unicode strings */
1633 const WCHAR* pStr = (const WCHAR*)pString;
1634 analysis->glyphs[i].fallbackFont = NULL;
1636 if (!glyphs || !pwLogClust || !piAdvance || !psva || !pGoffset || !abc)
1638 heap_free (BidiLevel);
1639 heap_free (glyphs);
1640 heap_free (pwLogClust);
1641 heap_free (piAdvance);
1642 heap_free (psva);
1643 heap_free (pGoffset);
1644 heap_free (abc);
1645 hr = E_OUTOFMEMORY;
1646 goto error;
1649 if ((dwFlags & SSA_FALLBACK) && requires_fallback(hdc, sc, &analysis->pItem[i].a, &pStr[analysis->pItem[i].iCharPos], cChar))
1651 LOGFONTW lf;
1652 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), & lf);
1653 lf.lfCharSet = scriptInformation[analysis->pItem[i].a.eScript].props.bCharSet;
1654 find_fallback_font(analysis->pItem[i].a.eScript, lf.lfFaceName);
1655 analysis->glyphs[i].fallbackFont = CreateFontIndirectW(&lf);
1656 if (analysis->glyphs[i].fallbackFont)
1658 ScriptFreeCache(sc);
1659 originalFont = SelectObject(hdc, analysis->glyphs[i].fallbackFont);
1663 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
1664 cChar, numGlyphs, &analysis->pItem[i].a,
1665 glyphs, pwLogClust, psva, &numGlyphsReturned);
1666 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
1667 piAdvance, pGoffset, abc);
1668 if (originalFont)
1669 SelectObject(hdc,originalFont);
1671 if (dwFlags & SSA_TAB)
1673 int tabi = 0;
1674 for (tabi = 0; tabi < cChar; tabi++)
1676 if (pStr[analysis->pItem[i].iCharPos+tabi] == 0x0009)
1677 piAdvance[tabi] = getGivenTabWidth(analysis->glyphs[i].sc, pTabdef, analysis->pItem[i].iCharPos+tabi, tab_x);
1678 tab_x+=piAdvance[tabi];
1682 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
1683 analysis->glyphs[i].glyphs = glyphs;
1684 analysis->glyphs[i].pwLogClust = pwLogClust;
1685 analysis->glyphs[i].piAdvance = piAdvance;
1686 analysis->glyphs[i].psva = psva;
1687 analysis->glyphs[i].pGoffset = pGoffset;
1688 analysis->glyphs[i].abc = abc;
1689 analysis->glyphs[i].iMaxPosX= -1;
1691 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1694 else
1696 for (i = 0; i < analysis->numItems; i++)
1697 BidiLevel[i] = analysis->pItem[i].a.s.uBidiLevel;
1700 ScriptLayout(analysis->numItems, BidiLevel, NULL, analysis->logical2visual);
1701 heap_free(BidiLevel);
1703 *pssa = analysis;
1704 heap_free(iString);
1705 return S_OK;
1707 error:
1708 heap_free(iString);
1709 heap_free(analysis->glyphs);
1710 heap_free(analysis->logattrs);
1711 heap_free(analysis->pItem);
1712 heap_free(analysis->logical2visual);
1713 heap_free(analysis);
1714 return hr;
1717 static inline BOOL does_glyph_start_cluster(const SCRIPT_VISATTR *pva, const WORD *pwLogClust, int cChars, int glyph, int direction)
1719 int i;
1721 if (pva[glyph].fClusterStart)
1722 return TRUE;
1723 for (i = 0; i < cChars; i++)
1724 if (pwLogClust[i] == glyph) break;
1725 if (i != cChars)
1726 return TRUE;
1728 return FALSE;
1732 static HRESULT SS_ItemOut( SCRIPT_STRING_ANALYSIS ssa,
1733 int iX,
1734 int iY,
1735 int iItem,
1736 int cStart,
1737 int cEnd,
1738 UINT uOptions,
1739 const RECT *prc,
1740 BOOL fSelected,
1741 BOOL fDisabled)
1743 StringAnalysis *analysis;
1744 int off_x = 0;
1745 HRESULT hr;
1746 COLORREF BkColor = 0x0;
1747 COLORREF TextColor = 0x0;
1748 INT BkMode = 0;
1749 INT runStart, runEnd;
1750 INT iGlyph, cGlyphs;
1751 HFONT oldFont = 0x0;
1753 TRACE("(%p,%d,%d,%d,%d,%d, 0x%1x, %d, %d)\n",
1754 ssa, iX, iY, iItem, cStart, cEnd, uOptions, fSelected, fDisabled);
1756 if (!(analysis = ssa)) return E_INVALIDARG;
1758 if ((cStart >= 0 && analysis->pItem[iItem+1].iCharPos <= cStart) ||
1759 (cEnd >= 0 && analysis->pItem[iItem].iCharPos >= cEnd))
1760 return S_OK;
1762 if (fSelected)
1764 BkMode = GetBkMode(analysis->hdc);
1765 SetBkMode( analysis->hdc, OPAQUE);
1766 BkColor = GetBkColor(analysis->hdc);
1767 SetBkColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHT));
1768 if (!fDisabled)
1770 TextColor = GetTextColor(analysis->hdc);
1771 SetTextColor(analysis->hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1774 if (analysis->glyphs[iItem].fallbackFont)
1775 oldFont = SelectObject(analysis->hdc, analysis->glyphs[iItem].fallbackFont);
1777 if (cStart >= 0 && analysis->pItem[iItem+1].iCharPos > cStart && analysis->pItem[iItem].iCharPos <= cStart)
1778 runStart = cStart - analysis->pItem[iItem].iCharPos;
1779 else
1780 runStart = 0;
1781 if (cEnd >= 0 && analysis->pItem[iItem+1].iCharPos > cEnd && analysis->pItem[iItem].iCharPos <= cEnd)
1782 runEnd = (cEnd-1) - analysis->pItem[iItem].iCharPos;
1783 else
1784 runEnd = (analysis->pItem[iItem+1].iCharPos - analysis->pItem[iItem].iCharPos) - 1;
1786 if (analysis->pItem[iItem].a.fRTL)
1788 if (cEnd >= 0 && cEnd < analysis->pItem[iItem+1].iCharPos)
1789 ScriptStringCPtoX(ssa, cEnd, FALSE, &off_x);
1790 else
1791 ScriptStringCPtoX(ssa, analysis->pItem[iItem+1].iCharPos-1, TRUE, &off_x);
1793 else
1795 if (cStart >=0 && runStart)
1796 ScriptStringCPtoX(ssa, cStart, FALSE, &off_x);
1797 else
1798 ScriptStringCPtoX(ssa, analysis->pItem[iItem].iCharPos, FALSE, &off_x);
1801 if (analysis->pItem[iItem].a.fRTL)
1802 iGlyph = analysis->glyphs[iItem].pwLogClust[runEnd];
1803 else
1804 iGlyph = analysis->glyphs[iItem].pwLogClust[runStart];
1806 if (analysis->pItem[iItem].a.fRTL)
1807 cGlyphs = analysis->glyphs[iItem].pwLogClust[runStart] - iGlyph;
1808 else
1809 cGlyphs = analysis->glyphs[iItem].pwLogClust[runEnd] - iGlyph;
1811 cGlyphs++;
1813 if (cEnd < 0 || scriptInformation[analysis->pItem[iItem].a.eScript].props.fNeedsCaretInfo)
1815 INT direction;
1816 INT clust_glyph;
1818 clust_glyph = iGlyph + cGlyphs;
1819 if (analysis->pItem[iItem].a.fRTL)
1820 direction = -1;
1821 else
1822 direction = 1;
1824 while(clust_glyph < analysis->glyphs[iItem].numGlyphs &&
1825 !does_glyph_start_cluster(analysis->glyphs[iItem].psva, analysis->glyphs[iItem].pwLogClust, (analysis->pItem[iItem+1].iCharPos - analysis->pItem[iItem].iCharPos), clust_glyph, direction))
1827 cGlyphs++;
1828 clust_glyph++;
1832 hr = ScriptTextOut(analysis->hdc,
1833 (SCRIPT_CACHE *)&analysis->glyphs[iItem].sc, iX + off_x,
1834 iY, uOptions, prc, &analysis->pItem[iItem].a, NULL, 0,
1835 &analysis->glyphs[iItem].glyphs[iGlyph], cGlyphs,
1836 &analysis->glyphs[iItem].piAdvance[iGlyph], NULL,
1837 &analysis->glyphs[iItem].pGoffset[iGlyph]);
1839 TRACE("ScriptTextOut hr=%08x\n", hr);
1841 if (fSelected)
1843 SetBkColor(analysis->hdc, BkColor);
1844 SetBkMode( analysis->hdc, BkMode);
1845 if (!fDisabled)
1846 SetTextColor(analysis->hdc, TextColor);
1848 if (analysis->glyphs[iItem].fallbackFont)
1849 SelectObject(analysis->hdc, oldFont);
1851 return hr;
1854 /***********************************************************************
1855 * ScriptStringOut (USP10.@)
1857 * This function takes the output of ScriptStringAnalyse and joins the segments
1858 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
1859 * only processes glyphs.
1861 * Parameters:
1862 * ssa [I] buffer to hold the analysed string components
1863 * iX [I] X axis displacement for output
1864 * iY [I] Y axis displacement for output
1865 * uOptions [I] flags controling output processing
1866 * prc [I] rectangle coordinates
1867 * iMinSel [I] starting pos for substringing output string
1868 * iMaxSel [I] ending pos for substringing output string
1869 * fDisabled [I] controls text highlighting
1871 * RETURNS
1872 * Success: S_OK
1873 * Failure: is the value returned by ScriptTextOut
1875 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
1876 int iX,
1877 int iY,
1878 UINT uOptions,
1879 const RECT *prc,
1880 int iMinSel,
1881 int iMaxSel,
1882 BOOL fDisabled)
1884 StringAnalysis *analysis;
1885 int item;
1886 HRESULT hr;
1888 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
1889 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
1891 if (!(analysis = ssa)) return E_INVALIDARG;
1892 if (!(analysis->dwFlags & SSA_GLYPHS)) return E_INVALIDARG;
1894 for (item = 0; item < analysis->numItems; item++)
1896 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], -1, -1, uOptions, prc, FALSE, fDisabled);
1897 if (FAILED(hr))
1898 return hr;
1901 if (iMinSel < iMaxSel && (iMinSel > 0 || iMaxSel > 0))
1903 if (iMaxSel > 0 && iMinSel < 0)
1904 iMinSel = 0;
1905 for (item = 0; item < analysis->numItems; item++)
1907 hr = SS_ItemOut( ssa, iX, iY, analysis->logical2visual[item], iMinSel, iMaxSel, uOptions, prc, TRUE, fDisabled);
1908 if (FAILED(hr))
1909 return hr;
1913 return S_OK;
1916 /***********************************************************************
1917 * ScriptStringCPtoX (USP10.@)
1920 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
1922 int item;
1923 int runningX = 0;
1924 StringAnalysis* analysis = ssa;
1926 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
1928 if (!ssa || !pX) return S_FALSE;
1929 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1931 /* icp out of range */
1932 if(icp < 0)
1934 analysis->invalid = TRUE;
1935 return E_INVALIDARG;
1938 for(item=0; item<analysis->numItems; item++)
1940 int CP, i;
1941 int offset;
1943 i = analysis->logical2visual[item];
1944 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
1945 /* initialize max extents for uninitialized runs */
1946 if (analysis->glyphs[i].iMaxPosX == -1)
1948 if (analysis->pItem[i].a.fRTL)
1949 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1950 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1951 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1952 else
1953 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1954 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1955 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
1958 if (icp >= analysis->pItem[i+1].iCharPos || icp < analysis->pItem[i].iCharPos)
1960 runningX += analysis->glyphs[i].iMaxPosX;
1961 continue;
1964 icp -= analysis->pItem[i].iCharPos;
1965 ScriptCPtoX(icp, fTrailing, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
1966 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
1967 &analysis->pItem[i].a, &offset);
1968 runningX += offset;
1970 *pX = runningX;
1971 return S_OK;
1974 /* icp out of range */
1975 analysis->invalid = TRUE;
1976 return E_INVALIDARG;
1979 /***********************************************************************
1980 * ScriptStringXtoCP (USP10.@)
1983 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
1985 StringAnalysis* analysis = ssa;
1986 int item;
1988 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
1990 if (!ssa || !piCh || !piTrailing) return S_FALSE;
1991 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
1993 /* out of range */
1994 if(iX < 0)
1996 if (analysis->pItem[0].a.fRTL)
1998 *piCh = 1;
1999 *piTrailing = FALSE;
2001 else
2003 *piCh = -1;
2004 *piTrailing = TRUE;
2006 return S_OK;
2009 for(item=0; item<analysis->numItems; item++)
2011 int i;
2012 int CP;
2014 for (i = 0; i < analysis->numItems && analysis->logical2visual[i] != item; i++)
2015 /* nothing */;
2017 CP = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
2018 /* initialize max extents for uninitialized runs */
2019 if (analysis->glyphs[i].iMaxPosX == -1)
2021 if (analysis->pItem[i].a.fRTL)
2022 ScriptCPtoX(0, FALSE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
2023 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
2024 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
2025 else
2026 ScriptCPtoX(CP, TRUE, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
2027 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
2028 &analysis->pItem[i].a, &analysis->glyphs[i].iMaxPosX);
2031 if (iX > analysis->glyphs[i].iMaxPosX)
2033 iX -= analysis->glyphs[i].iMaxPosX;
2034 continue;
2037 ScriptXtoCP(iX, CP, analysis->glyphs[i].numGlyphs, analysis->glyphs[i].pwLogClust,
2038 analysis->glyphs[i].psva, analysis->glyphs[i].piAdvance,
2039 &analysis->pItem[i].a, piCh, piTrailing);
2040 *piCh += analysis->pItem[i].iCharPos;
2042 return S_OK;
2045 /* out of range */
2046 *piCh = analysis->pItem[analysis->numItems].iCharPos;
2047 *piTrailing = FALSE;
2049 return S_OK;
2053 /***********************************************************************
2054 * ScriptStringFree (USP10.@)
2056 * Free a string analysis.
2058 * PARAMS
2059 * pssa [I] string analysis.
2061 * RETURNS
2062 * Success: S_OK
2063 * Failure: Non-zero HRESULT value.
2065 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
2067 StringAnalysis* analysis;
2068 BOOL invalid;
2069 int i;
2071 TRACE("(%p)\n", pssa);
2073 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
2075 invalid = analysis->invalid;
2077 if (analysis->glyphs)
2079 for (i = 0; i < analysis->numItems; i++)
2081 heap_free(analysis->glyphs[i].glyphs);
2082 heap_free(analysis->glyphs[i].pwLogClust);
2083 heap_free(analysis->glyphs[i].piAdvance);
2084 heap_free(analysis->glyphs[i].psva);
2085 heap_free(analysis->glyphs[i].pGoffset);
2086 heap_free(analysis->glyphs[i].abc);
2087 if (analysis->glyphs[i].fallbackFont)
2088 DeleteObject(analysis->glyphs[i].fallbackFont);
2089 ScriptFreeCache((SCRIPT_CACHE *)&analysis->glyphs[i].sc);
2090 heap_free(analysis->glyphs[i].sc);
2092 heap_free(analysis->glyphs);
2095 heap_free(analysis->pItem);
2096 heap_free(analysis->logattrs);
2097 heap_free(analysis->sz);
2098 heap_free(analysis->logical2visual);
2099 heap_free(analysis);
2101 if (invalid) return E_INVALIDARG;
2102 return S_OK;
2105 static inline int get_cluster_size(const WORD *pwLogClust, int cChars, int item,
2106 int direction, int* iCluster, int *check_out)
2108 int clust_size = 1;
2109 int check;
2110 WORD clust = pwLogClust[item];
2112 for (check = item+direction; check < cChars && check >= 0; check+=direction)
2114 if (pwLogClust[check] == clust)
2116 clust_size ++;
2117 if (iCluster && *iCluster == -1)
2118 *iCluster = item;
2120 else break;
2123 if (check_out)
2124 *check_out = check;
2126 return clust_size;
2129 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)
2131 int advance;
2132 int log_clust_max = 0;
2133 int i;
2135 advance = piAdvance[glyph];
2137 for (i = 0; i < cChars; i++)
2139 if (pwLogClust[i] > log_clust_max)
2140 log_clust_max = pwLogClust[i];
2143 if (glyph > log_clust_max)
2144 return advance;
2146 for (glyph+=direction; glyph < cGlyphs && glyph >= 0; glyph +=direction)
2149 if (does_glyph_start_cluster(pva, pwLogClust, cChars, glyph, direction))
2150 break;
2151 if (glyph > log_clust_max)
2152 break;
2153 advance += piAdvance[glyph];
2156 return advance;
2159 /***********************************************************************
2160 * ScriptCPtoX (USP10.@)
2163 HRESULT WINAPI ScriptCPtoX(int iCP,
2164 BOOL fTrailing,
2165 int cChars,
2166 int cGlyphs,
2167 const WORD *pwLogClust,
2168 const SCRIPT_VISATTR *psva,
2169 const int *piAdvance,
2170 const SCRIPT_ANALYSIS *psa,
2171 int *piX)
2173 int item;
2174 float iPosX;
2175 int iSpecial = -1;
2176 int iCluster = -1;
2177 int clust_size = 1;
2178 float special_size = 0.0;
2179 int iMaxPos = 0;
2180 int advance = 0;
2181 BOOL rtl = FALSE;
2183 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
2184 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
2185 psa, piX);
2187 if (psa->fRTL && ! psa->fLogicalOrder)
2188 rtl = TRUE;
2190 if (fTrailing)
2191 iCP++;
2193 if (rtl)
2195 int max_clust = pwLogClust[0];
2197 for (item=0; item < cGlyphs; item++)
2198 if (pwLogClust[item] > max_clust)
2200 ERR("We do not handle non reversed clusters properly\n");
2201 break;
2204 iMaxPos = 0;
2205 for (item = max_clust; item >=0; item --)
2206 iMaxPos += piAdvance[item];
2209 iPosX = 0.0;
2210 for (item=0; item < iCP && item < cChars; item++)
2212 if (iSpecial == -1 && (iCluster == -1 || (iCluster != -1 && iCluster+clust_size <= item)))
2214 int check;
2215 int clust = pwLogClust[item];
2217 iCluster = -1;
2218 clust_size = get_cluster_size(pwLogClust, cChars, item, 1, &iCluster,
2219 &check);
2221 advance = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, clust, 1);
2223 if (check >= cChars && !iMaxPos)
2225 for (check = clust; check < cChars; check++)
2226 special_size += get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, check, 1);
2227 iSpecial = item;
2228 special_size /= (cChars - item);
2229 iPosX += special_size;
2231 else
2233 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2235 clust_size --;
2236 if (clust_size == 0)
2237 iPosX += advance;
2239 else
2240 iPosX += advance / (float)clust_size;
2243 else if (iSpecial != -1)
2244 iPosX += special_size;
2245 else /* (iCluster != -1) */
2247 int adv = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, pwLogClust[iCluster], 1);
2248 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2250 clust_size --;
2251 if (clust_size == 0)
2252 iPosX += adv;
2254 else
2255 iPosX += adv / (float)clust_size;
2259 if (iMaxPos > 0)
2261 iPosX = iMaxPos - iPosX;
2262 if (iPosX < 0)
2263 iPosX = 0;
2266 *piX = iPosX;
2267 TRACE("*piX=%d\n", *piX);
2268 return S_OK;
2271 /***********************************************************************
2272 * ScriptXtoCP (USP10.@)
2275 HRESULT WINAPI ScriptXtoCP(int iX,
2276 int cChars,
2277 int cGlyphs,
2278 const WORD *pwLogClust,
2279 const SCRIPT_VISATTR *psva,
2280 const int *piAdvance,
2281 const SCRIPT_ANALYSIS *psa,
2282 int *piCP,
2283 int *piTrailing)
2285 int item;
2286 float iPosX;
2287 float iLastPosX;
2288 int iSpecial = -1;
2289 int iCluster = -1;
2290 int clust_size = 1;
2291 int cjump = 0;
2292 int advance;
2293 float special_size = 0.0;
2294 int direction = 1;
2296 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
2297 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
2298 psa, piCP, piTrailing);
2300 if (psa->fRTL && ! psa->fLogicalOrder)
2301 direction = -1;
2303 if (direction<0)
2305 int max_clust = pwLogClust[0];
2307 if (iX < 0)
2309 *piCP = cChars;
2310 *piTrailing = 0;
2311 return S_OK;
2314 for (item=0; item < cChars; item++)
2315 if (pwLogClust[item] > max_clust)
2317 ERR("We do not handle non reversed clusters properly\n");
2318 break;
2322 if (iX < 0)
2324 *piCP = -1;
2325 *piTrailing = 1;
2326 return S_OK;
2329 iPosX = iLastPosX = 0;
2330 if (direction > 0)
2331 item = 0;
2332 else
2333 item = cChars - 1;
2334 for (; iPosX <= iX && item < cChars && item >= 0; item+=direction)
2336 iLastPosX = iPosX;
2337 if (iSpecial == -1 &&
2338 (iCluster == -1 ||
2339 (iCluster != -1 &&
2340 ((direction > 0 && iCluster+clust_size <= item) ||
2341 (direction < 0 && iCluster-clust_size >= item))
2346 int check;
2347 int clust = pwLogClust[item];
2349 iCluster = -1;
2350 cjump = 0;
2351 clust_size = get_cluster_size(pwLogClust, cChars, item, direction,
2352 &iCluster, &check);
2353 advance = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, clust, direction);
2355 if (check >= cChars && direction > 0)
2357 for (check = clust; check < cChars; check++)
2358 special_size += get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, check, direction);
2359 iSpecial = item;
2360 special_size /= (cChars - item);
2361 iPosX += special_size;
2363 else
2365 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2367 if (!cjump)
2368 iPosX += advance;
2369 cjump++;
2371 else
2372 iPosX += advance / (float)clust_size;
2375 else if (iSpecial != -1)
2376 iPosX += special_size;
2377 else /* (iCluster != -1) */
2379 int adv = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, pwLogClust[iCluster], direction);
2380 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
2382 if (!cjump)
2383 iPosX += adv;
2384 cjump++;
2386 else
2387 iPosX += adv / (float)clust_size;
2391 if (direction > 0)
2393 if (iPosX > iX)
2394 item--;
2395 if (item < cChars && ((iPosX - iLastPosX) / 2.0) + iX >= iPosX)
2397 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
2398 item+=(clust_size-1);
2399 *piTrailing = 1;
2401 else
2402 *piTrailing = 0;
2404 else
2406 if (iX == iLastPosX)
2407 item++;
2408 if (iX >= iLastPosX && iX <= iPosX)
2409 item++;
2411 if (iLastPosX == iX)
2412 *piTrailing = 0;
2413 else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
2415 if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
2416 item-=(clust_size-1);
2417 *piTrailing = 1;
2419 else
2420 *piTrailing = 0;
2423 *piCP = item;
2425 TRACE("*piCP=%d\n", *piCP);
2426 TRACE("*piTrailing=%d\n", *piTrailing);
2427 return S_OK;
2430 /***********************************************************************
2431 * ScriptBreak (USP10.@)
2433 * Retrieve line break information.
2435 * PARAMS
2436 * chars [I] Array of characters.
2437 * sa [I] String analysis.
2438 * la [I] Array of logical attribute structures.
2440 * RETURNS
2441 * Success: S_OK
2442 * Failure: S_FALSE
2444 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
2446 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
2448 if (!la) return S_FALSE;
2450 BREAK_line(chars, count, sa, la);
2452 return S_OK;
2455 /***********************************************************************
2456 * ScriptIsComplex (USP10.@)
2458 * Determine if a string is complex.
2460 * PARAMS
2461 * chars [I] Array of characters to test.
2462 * len [I] Length in characters.
2463 * flag [I] Flag.
2465 * RETURNS
2466 * Success: S_OK
2467 * Failure: S_FALSE
2470 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
2472 int i;
2474 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
2476 for (i = 0; i < len; i++)
2478 int script;
2480 if ((flag & SIC_ASCIIDIGIT) && chars[i] >= 0x30 && chars[i] <= 0x39)
2481 return S_OK;
2483 script = get_char_script(chars[i]);
2484 if ((scriptInformation[script].props.fComplex && (flag & SIC_COMPLEX))||
2485 (!scriptInformation[script].props.fComplex && (flag & SIC_NEUTRAL)))
2486 return S_OK;
2488 return S_FALSE;
2491 /***********************************************************************
2492 * ScriptShapeOpenType (USP10.@)
2494 * Produce glyphs and visual attributes for a run.
2496 * PARAMS
2497 * hdc [I] Device context.
2498 * psc [I/O] Opaque pointer to a script cache.
2499 * psa [I/O] Script analysis.
2500 * tagScript [I] The OpenType tag for the Script
2501 * tagLangSys [I] The OpenType tag for the Language
2502 * rcRangeChars[I] Array of Character counts in each range
2503 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
2504 * cRanges [I] Count of ranges
2505 * pwcChars [I] Array of characters specifying the run.
2506 * cChars [I] Number of characters in pwcChars.
2507 * cMaxGlyphs [I] Length of pwOutGlyphs.
2508 * pwLogClust [O] Array of logical cluster info.
2509 * pCharProps [O] Array of character property values
2510 * pwOutGlyphs [O] Array of glyphs.
2511 * pOutGlyphProps [O] Array of attributes for the retrieved glyphs
2512 * pcGlyphs [O] Number of glyphs returned.
2514 * RETURNS
2515 * Success: S_OK
2516 * Failure: Non-zero HRESULT value.
2518 HRESULT WINAPI ScriptShapeOpenType( HDC hdc, SCRIPT_CACHE *psc,
2519 SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript,
2520 OPENTYPE_TAG tagLangSys, int *rcRangeChars,
2521 TEXTRANGE_PROPERTIES **rpRangeProperties,
2522 int cRanges, const WCHAR *pwcChars, int cChars,
2523 int cMaxGlyphs, WORD *pwLogClust,
2524 SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs,
2525 SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs)
2527 HRESULT hr;
2528 unsigned int i;
2529 BOOL rtl;
2531 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %d, %d, %p, %p, %p, %p, %p )\n",
2532 hdc, psc, psa,
2533 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2534 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2535 cChars, cMaxGlyphs, pwLogClust, pCharProps, pwOutGlyphs, pOutGlyphProps, pcGlyphs);
2537 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
2538 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
2540 if (!pOutGlyphProps || !pcGlyphs || !pCharProps) return E_INVALIDARG;
2541 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2543 if (cRanges)
2544 FIXME("Ranges not supported yet\n");
2546 rtl = (psa && !psa->fLogicalOrder && psa->fRTL);
2548 *pcGlyphs = cChars;
2549 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2550 if (!pwLogClust) return E_FAIL;
2552 ((ScriptCache *)*psc)->userScript = tagScript;
2553 ((ScriptCache *)*psc)->userLang = tagLangSys;
2555 /* set fNoGlyphIndex non truetype/opentype fonts */
2556 if (!psa->fNoGlyphIndex && !((ScriptCache *)*psc)->sfnt)
2557 psa->fNoGlyphIndex = TRUE;
2559 /* Initialize a SCRIPT_VISATTR and LogClust for each char in this run */
2560 for (i = 0; i < cChars; i++)
2562 int idx = i;
2563 if (rtl) idx = cChars - 1 - i;
2564 /* FIXME: set to better values */
2565 pOutGlyphProps[i].sva.uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
2566 pOutGlyphProps[i].sva.fClusterStart = 1;
2567 pOutGlyphProps[i].sva.fDiacritic = 0;
2568 pOutGlyphProps[i].sva.fZeroWidth = 0;
2569 pOutGlyphProps[i].sva.fReserved = 0;
2570 pOutGlyphProps[i].sva.fShapeReserved = 0;
2572 /* FIXME: have the shaping engine set this */
2573 pCharProps[i].fCanGlyphAlone = 0;
2575 pwLogClust[i] = idx;
2578 if (psa && !psa->fNoGlyphIndex)
2580 WCHAR *rChars;
2581 if ((hr = SHAPE_CheckFontForRequiredFeatures(hdc, (ScriptCache *)*psc, psa)) != S_OK) return hr;
2583 rChars = heap_alloc(sizeof(WCHAR) * cChars);
2584 if (!rChars) return E_OUTOFMEMORY;
2585 for (i = 0; i < cChars; i++)
2587 int idx = i;
2588 WCHAR chInput;
2589 if (rtl) idx = cChars - 1 - i;
2590 if (psa->fRTL)
2591 chInput = mirror_char(pwcChars[idx]);
2592 else
2593 chInput = pwcChars[idx];
2594 /* special case for tabs */
2595 if (chInput == 0x0009)
2596 chInput = 0x0020;
2597 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
2599 WORD glyph;
2600 if (!hdc)
2602 heap_free(rChars);
2603 return E_PENDING;
2605 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR)
2607 heap_free(rChars);
2608 return S_FALSE;
2610 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
2612 rChars[i] = chInput;
2615 SHAPE_ContextualShaping(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, pcGlyphs, cMaxGlyphs, pwLogClust);
2616 SHAPE_ApplyDefaultOpentypeFeatures(hdc, (ScriptCache *)*psc, psa, pwOutGlyphs, pcGlyphs, cMaxGlyphs, cChars, pwLogClust);
2617 SHAPE_CharGlyphProp(hdc, (ScriptCache *)*psc, psa, pwcChars, cChars, pwOutGlyphs, *pcGlyphs, pwLogClust, pCharProps, pOutGlyphProps);
2618 heap_free(rChars);
2620 else
2622 TRACE("no glyph translation\n");
2623 for (i = 0; i < cChars; i++)
2625 int idx = i;
2626 /* No mirroring done here */
2627 if (rtl) idx = cChars - 1 - i;
2628 pwOutGlyphs[i] = pwcChars[idx];
2632 return S_OK;
2636 /***********************************************************************
2637 * ScriptShape (USP10.@)
2639 * Produce glyphs and visual attributes for a run.
2641 * PARAMS
2642 * hdc [I] Device context.
2643 * psc [I/O] Opaque pointer to a script cache.
2644 * pwcChars [I] Array of characters specifying the run.
2645 * cChars [I] Number of characters in pwcChars.
2646 * cMaxGlyphs [I] Length of pwOutGlyphs.
2647 * psa [I/O] Script analysis.
2648 * pwOutGlyphs [O] Array of glyphs.
2649 * pwLogClust [O] Array of logical cluster info.
2650 * psva [O] Array of visual attributes.
2651 * pcGlyphs [O] Number of glyphs returned.
2653 * RETURNS
2654 * Success: S_OK
2655 * Failure: Non-zero HRESULT value.
2657 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
2658 int cChars, int cMaxGlyphs,
2659 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
2660 SCRIPT_VISATTR *psva, int *pcGlyphs)
2662 HRESULT hr;
2663 int i;
2664 SCRIPT_CHARPROP *charProps;
2665 SCRIPT_GLYPHPROP *glyphProps;
2667 if (!psva || !pcGlyphs) return E_INVALIDARG;
2668 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
2670 charProps = heap_alloc_zero(sizeof(SCRIPT_CHARPROP)*cChars);
2671 if (!charProps) return E_OUTOFMEMORY;
2672 glyphProps = heap_alloc_zero(sizeof(SCRIPT_GLYPHPROP)*cMaxGlyphs);
2673 if (!glyphProps)
2675 heap_free(charProps);
2676 return E_OUTOFMEMORY;
2679 hr = ScriptShapeOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, pwcChars, cChars, cMaxGlyphs, pwLogClust, charProps, pwOutGlyphs, glyphProps, pcGlyphs);
2681 if (SUCCEEDED(hr))
2683 for (i = 0; i < *pcGlyphs; i++)
2684 psva[i] = glyphProps[i].sva;
2687 heap_free(charProps);
2688 heap_free(glyphProps);
2690 return hr;
2693 /***********************************************************************
2694 * ScriptPlaceOpenType (USP10.@)
2696 * Produce advance widths for a run.
2698 * PARAMS
2699 * hdc [I] Device context.
2700 * psc [I/O] Opaque pointer to a script cache.
2701 * psa [I/O] String analysis.
2702 * tagScript [I] The OpenType tag for the Script
2703 * tagLangSys [I] The OpenType tag for the Language
2704 * rcRangeChars[I] Array of Character counts in each range
2705 * rpRangeProperties [I] Array of TEXTRANGE_PROPERTIES structures
2706 * cRanges [I] Count of ranges
2707 * pwcChars [I] Array of characters specifying the run.
2708 * pwLogClust [I] Array of logical cluster info
2709 * pCharProps [I] Array of character property values
2710 * cChars [I] Number of characters in pwcChars.
2711 * pwGlyphs [I] Array of glyphs.
2712 * pGlyphProps [I] Array of attributes for the retrieved glyphs
2713 * cGlyphs [I] Count of Glyphs
2714 * piAdvance [O] Array of advance widths.
2715 * pGoffset [O] Glyph offsets.
2716 * pABC [O] Combined ABC width.
2718 * RETURNS
2719 * Success: S_OK
2720 * Failure: Non-zero HRESULT value.
2723 HRESULT WINAPI ScriptPlaceOpenType( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa,
2724 OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys,
2725 int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties,
2726 int cRanges, const WCHAR *pwcChars, WORD *pwLogClust,
2727 SCRIPT_CHARPROP *pCharProps, int cChars,
2728 const WORD *pwGlyphs, const SCRIPT_GLYPHPROP *pGlyphProps,
2729 int cGlyphs, int *piAdvance,
2730 GOFFSET *pGoffset, ABC *pABC
2733 HRESULT hr;
2734 int i;
2736 TRACE("(%p, %p, %p, %s, %s, %p, %p, %d, %s, %p, %p, %d, %p, %p, %d, %p %p %p)\n",
2737 hdc, psc, psa,
2738 debugstr_an((char*)&tagScript,4), debugstr_an((char*)&tagLangSys,4),
2739 rcRangeChars, rpRangeProperties, cRanges, debugstr_wn(pwcChars, cChars),
2740 pwLogClust, pCharProps, cChars, pwGlyphs, pGlyphProps, cGlyphs, piAdvance,
2741 pGoffset, pABC);
2743 if (!pGlyphProps) return E_INVALIDARG;
2744 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2745 if (!pGoffset) return E_FAIL;
2747 if (cRanges)
2748 FIXME("Ranges not supported yet\n");
2750 ((ScriptCache *)*psc)->userScript = tagScript;
2751 ((ScriptCache *)*psc)->userLang = tagLangSys;
2753 if (pABC) memset(pABC, 0, sizeof(ABC));
2754 for (i = 0; i < cGlyphs; i++)
2756 ABC abc;
2757 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
2759 if (!hdc) return E_PENDING;
2760 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
2762 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
2764 else
2766 INT width;
2767 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
2768 abc.abcB = width;
2769 abc.abcA = abc.abcC = 0;
2771 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
2773 if (pABC)
2775 pABC->abcA += abc.abcA;
2776 pABC->abcB += abc.abcB;
2777 pABC->abcC += abc.abcC;
2779 /* FIXME: set to more reasonable values */
2780 pGoffset[i].du = pGoffset[i].dv = 0;
2781 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
2784 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
2785 return S_OK;
2788 /***********************************************************************
2789 * ScriptPlace (USP10.@)
2791 * Produce advance widths for a run.
2793 * PARAMS
2794 * hdc [I] Device context.
2795 * psc [I/O] Opaque pointer to a script cache.
2796 * pwGlyphs [I] Array of glyphs.
2797 * cGlyphs [I] Number of glyphs in pwGlyphs.
2798 * psva [I] Array of visual attributes.
2799 * psa [I/O] String analysis.
2800 * piAdvance [O] Array of advance widths.
2801 * pGoffset [O] Glyph offsets.
2802 * pABC [O] Combined ABC width.
2804 * RETURNS
2805 * Success: S_OK
2806 * Failure: Non-zero HRESULT value.
2808 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
2809 int cGlyphs, const SCRIPT_VISATTR *psva,
2810 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
2812 HRESULT hr;
2813 SCRIPT_GLYPHPROP *glyphProps;
2814 int i;
2816 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
2817 piAdvance, pGoffset, pABC);
2819 if (!psva) return E_INVALIDARG;
2820 if (!pGoffset) return E_FAIL;
2822 glyphProps = heap_alloc(sizeof(SCRIPT_GLYPHPROP)*cGlyphs);
2823 if (!glyphProps) return E_OUTOFMEMORY;
2825 for (i = 0; i < cGlyphs; i++)
2826 glyphProps[i].sva = psva[i];
2828 hr = ScriptPlaceOpenType(hdc, psc, psa, scriptInformation[psa->eScript].scriptTag, 0, NULL, NULL, 0, NULL, NULL, NULL, 0, pwGlyphs, glyphProps, cGlyphs, piAdvance, pGoffset, pABC);
2830 heap_free(glyphProps);
2832 return hr;
2835 /***********************************************************************
2836 * ScriptGetCMap (USP10.@)
2838 * Retrieve glyph indices.
2840 * PARAMS
2841 * hdc [I] Device context.
2842 * psc [I/O] Opaque pointer to a script cache.
2843 * pwcInChars [I] Array of Unicode characters.
2844 * cChars [I] Number of characters in pwcInChars.
2845 * dwFlags [I] Flags.
2846 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
2848 * RETURNS
2849 * Success: S_OK
2850 * Failure: Non-zero HRESULT value.
2852 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
2853 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
2855 HRESULT hr;
2856 int i;
2858 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
2859 cChars, dwFlags, pwOutGlyphs);
2861 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2863 hr = S_OK;
2865 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
2867 for (i = 0; i < cChars; i++)
2869 WCHAR inChar;
2870 if (dwFlags == SGCM_RTL)
2871 inChar = mirror_char(pwcInChars[i]);
2872 else
2873 inChar = pwcInChars[i];
2874 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
2876 WORD glyph;
2877 if (!hdc) return E_PENDING;
2878 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
2879 if (glyph == 0xffff)
2881 hr = S_FALSE;
2882 glyph = 0x0;
2884 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
2888 else
2890 TRACE("no glyph translation\n");
2891 for (i = 0; i < cChars; i++)
2893 WCHAR inChar;
2894 if (dwFlags == SGCM_RTL)
2895 inChar = mirror_char(pwcInChars[i]);
2896 else
2897 inChar = pwcInChars[i];
2898 pwOutGlyphs[i] = inChar;
2901 return hr;
2904 /***********************************************************************
2905 * ScriptTextOut (USP10.@)
2908 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
2909 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
2910 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
2911 const int *piJustify, const GOFFSET *pGoffset)
2913 HRESULT hr = S_OK;
2915 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
2916 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
2917 piAdvance, piJustify, pGoffset);
2919 if (!hdc || !psc) return E_INVALIDARG;
2920 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
2922 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
2923 fuOptions |= ETO_IGNORELANGUAGE;
2924 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
2925 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
2927 if (psa->fRTL && psa->fLogicalOrder)
2929 int i;
2930 WORD *rtlGlyphs;
2932 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
2933 if (!rtlGlyphs)
2934 return E_OUTOFMEMORY;
2936 for (i = 0; i < cGlyphs; i++)
2937 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
2939 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
2940 hr = S_FALSE;
2941 heap_free(rtlGlyphs);
2943 else
2944 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
2945 hr = S_FALSE;
2947 return hr;
2950 /***********************************************************************
2951 * ScriptCacheGetHeight (USP10.@)
2953 * Retrieve the height of the font in the cache.
2955 * PARAMS
2956 * hdc [I] Device context.
2957 * psc [I/O] Opaque pointer to a script cache.
2958 * height [O] Receives font height.
2960 * RETURNS
2961 * Success: S_OK
2962 * Failure: Non-zero HRESULT value.
2964 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
2966 HRESULT hr;
2968 TRACE("(%p, %p, %p)\n", hdc, psc, height);
2970 if (!height) return E_INVALIDARG;
2971 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
2973 *height = get_cache_height(psc);
2974 return S_OK;
2977 /***********************************************************************
2978 * ScriptGetGlyphABCWidth (USP10.@)
2980 * Retrieve the width of a glyph.
2982 * PARAMS
2983 * hdc [I] Device context.
2984 * psc [I/O] Opaque pointer to a script cache.
2985 * glyph [I] Glyph to retrieve the width for.
2986 * abc [O] ABC widths of the glyph.
2988 * RETURNS
2989 * Success: S_OK
2990 * Failure: Non-zero HRESULT value.
2992 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
2994 HRESULT hr;
2996 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
2998 if (!abc) return E_INVALIDARG;
2999 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
3001 if (!get_cache_glyph_widths(psc, glyph, abc))
3003 if (!hdc) return E_PENDING;
3004 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
3006 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
3008 else
3010 INT width;
3011 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
3012 abc->abcB = width;
3013 abc->abcA = abc->abcC = 0;
3015 set_cache_glyph_widths(psc, glyph, abc);
3017 return S_OK;
3020 /***********************************************************************
3021 * ScriptLayout (USP10.@)
3023 * Map embedding levels to visual and/or logical order.
3025 * PARAMS
3026 * runs [I] Size of level array.
3027 * level [I] Array of embedding levels.
3028 * vistolog [O] Map of embedding levels from visual to logical order.
3029 * logtovis [O] Map of embedding levels from logical to visual order.
3031 * RETURNS
3032 * Success: S_OK
3033 * Failure: Non-zero HRESULT value.
3035 * BUGS
3036 * This stub works correctly for any sequence of a single
3037 * embedding level but not for sequences of different
3038 * embedding levels, i.e. mixtures of RTL and LTR scripts.
3040 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
3042 int* indexs;
3043 int ich;
3045 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
3047 if (!level || (!vistolog && !logtovis))
3048 return E_INVALIDARG;
3050 indexs = heap_alloc(sizeof(int) * runs);
3051 if (!indexs)
3052 return E_OUTOFMEMORY;
3055 if (vistolog)
3057 for( ich = 0; ich < runs; ich++)
3058 indexs[ich] = ich;
3060 ich = 0;
3061 while (ich < runs)
3062 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
3063 for (ich = 0; ich < runs; ich++)
3064 vistolog[ich] = indexs[ich];
3068 if (logtovis)
3070 for( ich = 0; ich < runs; ich++)
3071 indexs[ich] = ich;
3073 ich = 0;
3074 while (ich < runs)
3075 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
3076 for (ich = 0; ich < runs; ich++)
3077 logtovis[ich] = indexs[ich];
3079 heap_free(indexs);
3081 return S_OK;
3084 /***********************************************************************
3085 * ScriptStringGetLogicalWidths (USP10.@)
3087 * Returns logical widths from a string analysis.
3089 * PARAMS
3090 * ssa [I] string analysis.
3091 * piDx [O] logical widths returned.
3093 * RETURNS
3094 * Success: S_OK
3095 * Failure: a non-zero HRESULT.
3097 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
3099 int i, j, next = 0;
3100 StringAnalysis *analysis = ssa;
3102 TRACE("%p, %p\n", ssa, piDx);
3104 if (!analysis) return S_FALSE;
3105 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
3107 for (i = 0; i < analysis->numItems; i++)
3109 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
3110 int direction = 1;
3112 if (analysis->pItem[i].a.fRTL && ! analysis->pItem[i].a.fLogicalOrder)
3113 direction = -1;
3115 for (j = 0; j < cChar; j++)
3117 int k;
3118 int glyph = analysis->glyphs[i].pwLogClust[j];
3119 int clust_size = get_cluster_size(analysis->glyphs[i].pwLogClust,
3120 cChar, j, direction, NULL, NULL);
3121 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);
3123 for (k = 0; k < clust_size; k++)
3125 piDx[next] = advance / clust_size;
3126 next++;
3127 if (k) j++;
3131 return S_OK;
3134 /***********************************************************************
3135 * ScriptStringValidate (USP10.@)
3137 * Validate a string analysis.
3139 * PARAMS
3140 * ssa [I] string analysis.
3142 * RETURNS
3143 * Success: S_OK
3144 * Failure: S_FALSE if invalid sequences are found
3145 * or a non-zero HRESULT if it fails.
3147 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
3149 StringAnalysis *analysis = ssa;
3151 TRACE("(%p)\n", ssa);
3153 if (!analysis) return E_INVALIDARG;
3154 return (analysis->invalid) ? S_FALSE : S_OK;
3157 /***********************************************************************
3158 * ScriptString_pSize (USP10.@)
3160 * Retrieve width and height of an analysed string.
3162 * PARAMS
3163 * ssa [I] string analysis.
3165 * RETURNS
3166 * Success: Pointer to a SIZE structure.
3167 * Failure: NULL
3169 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
3171 int i, j;
3172 StringAnalysis *analysis = ssa;
3174 TRACE("(%p)\n", ssa);
3176 if (!analysis) return NULL;
3177 if (!(analysis->dwFlags & SSA_GLYPHS)) return NULL;
3179 if (!analysis->sz)
3181 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
3182 analysis->sz->cy = analysis->glyphs[0].sc->tm.tmHeight;
3184 analysis->sz->cx = 0;
3185 for (i = 0; i < analysis->numItems; i++)
3187 if (analysis->glyphs[i].sc->tm.tmHeight > analysis->sz->cy)
3188 analysis->sz->cy = analysis->glyphs[i].sc->tm.tmHeight;
3189 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
3190 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
3193 return analysis->sz;
3196 /***********************************************************************
3197 * ScriptString_pLogAttr (USP10.@)
3199 * Retrieve logical attributes of an analysed string.
3201 * PARAMS
3202 * ssa [I] string analysis.
3204 * RETURNS
3205 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
3206 * Failure: NULL
3208 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
3210 StringAnalysis *analysis = ssa;
3212 TRACE("(%p)\n", ssa);
3214 if (!analysis) return NULL;
3215 if (!(analysis->dwFlags & SSA_BREAK)) return NULL;
3216 return analysis->logattrs;
3219 /***********************************************************************
3220 * ScriptString_pcOutChars (USP10.@)
3222 * Retrieve the length of a string after clipping.
3224 * PARAMS
3225 * ssa [I] String analysis.
3227 * RETURNS
3228 * Success: Pointer to the length.
3229 * Failure: NULL
3231 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
3233 StringAnalysis *analysis = ssa;
3235 TRACE("(%p)\n", ssa);
3237 if (!analysis) return NULL;
3238 return &analysis->clip_len;
3241 /***********************************************************************
3242 * ScriptStringGetOrder (USP10.@)
3244 * Retrieve a glyph order map.
3246 * PARAMS
3247 * ssa [I] String analysis.
3248 * order [I/O] Array of glyph positions.
3250 * RETURNS
3251 * Success: S_OK
3252 * Failure: a non-zero HRESULT.
3254 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
3256 int i, j;
3257 unsigned int k;
3258 StringAnalysis *analysis = ssa;
3260 TRACE("(%p)\n", ssa);
3262 if (!analysis) return S_FALSE;
3263 if (!(analysis->dwFlags & SSA_GLYPHS)) return S_FALSE;
3265 /* FIXME: handle RTL scripts */
3266 for (i = 0, k = 0; i < analysis->numItems; i++)
3267 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
3268 order[k] = k;
3270 return S_OK;
3273 /***********************************************************************
3274 * ScriptGetLogicalWidths (USP10.@)
3276 * Convert advance widths to logical widths.
3278 * PARAMS
3279 * sa [I] Script analysis.
3280 * nbchars [I] Number of characters.
3281 * nbglyphs [I] Number of glyphs.
3282 * glyph_width [I] Array of glyph widths.
3283 * log_clust [I] Array of logical clusters.
3284 * sva [I] Visual attributes.
3285 * widths [O] Array of logical widths.
3287 * RETURNS
3288 * Success: S_OK
3289 * Failure: a non-zero HRESULT.
3291 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
3292 const int *glyph_width, const WORD *log_clust,
3293 const SCRIPT_VISATTR *sva, int *widths)
3295 int i;
3297 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
3298 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
3300 /* FIXME */
3301 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
3302 return S_OK;
3305 /***********************************************************************
3306 * ScriptApplyLogicalWidth (USP10.@)
3308 * Generate glyph advance widths.
3310 * PARAMS
3311 * dx [I] Array of logical advance widths.
3312 * num_chars [I] Number of characters.
3313 * num_glyphs [I] Number of glyphs.
3314 * log_clust [I] Array of logical clusters.
3315 * sva [I] Visual attributes.
3316 * advance [I] Array of glyph advance widths.
3317 * sa [I] Script analysis.
3318 * abc [I/O] Summed ABC widths.
3319 * justify [O] Array of glyph advance widths.
3321 * RETURNS
3322 * Success: S_OK
3323 * Failure: a non-zero HRESULT.
3325 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
3326 const WORD *log_clust, const SCRIPT_VISATTR *sva,
3327 const int *advance, const SCRIPT_ANALYSIS *sa,
3328 ABC *abc, int *justify)
3330 int i;
3332 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
3333 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
3335 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
3336 return S_OK;
3339 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
3340 int num_glyphs, int dx, int min_kashida, int *justify)
3342 int i;
3344 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
3346 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];
3347 return S_OK;