msxml3: Ignore XML property ResolveExternals.
[wine] / dlls / usp10 / tests / usp10.c
1 /*
2  * Tests for usp10 dll
3  *
4  * Copyright 2006 Jeff Latimer
5  * Copyright 2006 Hans Leidekker
6  * Copyright 2010 CodeWeavers, Aric Stewart
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * Notes:
23  * Uniscribe allows for processing of complex scripts such as joining
24  * and filtering characters and bi-directional text with custom line breaks.
25  */
26
27 #include <assert.h>
28 #include <stdio.h>
29
30 #include <wine/test.h>
31 #include <windows.h>
32 #include <usp10.h>
33
34 typedef struct _itemTest {
35     char todo_flag[4];
36     int iCharPos;
37     int fRTL;
38     int fLayoutRTL;
39     int uBidiLevel;
40 } itemTest;
41
42 static inline void _test_items_ok(LPCWSTR string, DWORD cchString,
43                          SCRIPT_CONTROL *Control, SCRIPT_STATE *State,
44                          DWORD nItems, const itemTest* items, BOOL nItemsToDo,
45                          INT nItemsBroken)
46 {
47     HRESULT hr;
48     int x, outnItems;
49     SCRIPT_ITEM outpItems[15];
50
51     hr = ScriptItemize(string, cchString, 15, Control, State, outpItems, &outnItems);
52     winetest_ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
53     if (nItemsBroken && broken(nItemsBroken == outnItems))
54     {
55         winetest_win_skip("This test broken on this platform\n");
56         return;
57     }
58     if (nItemsToDo)
59         todo_wine winetest_ok(outnItems == nItems, "Wrong number of items\n");
60     else
61         winetest_ok(outnItems == nItems, "Wrong number of items\n");
62     for (x = 0; x <= outnItems; x++)
63     {
64         if (items[x].todo_flag[0])
65             todo_wine winetest_ok(outpItems[x].iCharPos == items[x].iCharPos, "%i:Wrong CharPos\n",x);
66         else
67             winetest_ok(outpItems[x].iCharPos == items[x].iCharPos, "%i:Wrong CharPos (%i)\n",x,outpItems[x].iCharPos);
68
69         if (items[x].todo_flag[1])
70             todo_wine winetest_ok(outpItems[x].a.fRTL == items[x].fRTL, "%i:Wrong fRTL\n",x);
71         else
72             winetest_ok(outpItems[x].a.fRTL == items[x].fRTL, "%i:Wrong fRTL(%i)\n",x,outpItems[x].a.fRTL);
73         if (items[x].todo_flag[2])
74             todo_wine winetest_ok(outpItems[x].a.fLayoutRTL == items[x].fLayoutRTL, "%i:Wrong fLayoutRTL\n",x);
75         else
76             winetest_ok(outpItems[x].a.fLayoutRTL == items[x].fLayoutRTL, "%i:Wrong fLayoutRTL(%i)\n",x,outpItems[x].a.fLayoutRTL);
77         if (items[x].todo_flag[3])
78             todo_wine winetest_ok(outpItems[x].a.s.uBidiLevel == items[x].uBidiLevel, "%i:Wrong BidiLevel\n",x);
79         else
80             winetest_ok(outpItems[x].a.s.uBidiLevel == items[x].uBidiLevel, "%i:Wrong BidiLevel(%i)\n",x,outpItems[x].a.s.uBidiLevel);
81         if (x != outnItems)
82             winetest_ok(outpItems[x].a.eScript != SCRIPT_UNDEFINED, "%i: Undefined script\n",x);
83     }
84 }
85
86 #define test_items_ok(a,b,c,d,e,f,g,h) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _test_items_ok(a,b,c,d,e,f,g,h)
87
88
89 static void test_ScriptItemize( void )
90 {
91     static const WCHAR test1[] = {'t', 'e', 's', 't',0};
92     static const itemTest t11[2] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},4,0,0,0}};
93     static const itemTest t12[2] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},4,0,0,0}};
94
95     static const WCHAR test1b[] = {' ', ' ', ' ', ' ',0};
96     static const itemTest t1b1[2] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},4,0,0,0}};
97     static const itemTest t1b2[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0}};
98
99     /* Arabic, English*/
100     static const WCHAR test2[] = {'1','2','3','-','5','2',0x064a,0x064f,0x0633,0x0627,0x0648,0x0650,0x064a,'7','1','.',0};
101     static const itemTest t21[7] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},3,0,0,0},{{0,0,0,0},4,0,0,0},{{0,0,0,0},6,1,1,1},{{0,0,0,0},13,0,0,0},{{0,0,0,0},15,0,0,0},{{0,0,0,0},16,0,0,0}};
102     static const itemTest t22[5] = {{{0,0,0,1},0,0,0,2},{{0,0,0,0},6,1,1,1},{{0,0,1,0},13,0,1,2},{{0,0,0,0},15,0,0,0},{{0,0,0,0},16,0,0,0}};
103     static const itemTest t23[5] = {{{0,0,1,0},0,0,1,2},{{0,0,0,0},6,1,1,1},{{0,0,1,0},13,0,1,2},{{0,0,0,0},15,1,1,1},{{0,0,0,0},16,0,0,0}};
104
105     static const WCHAR test2b[] = {'A','B','C','-','D','E','F',' ',0x0621,0x0623,0x0624,0};
106     static const itemTest t2b1[5] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},3,0,0,0},{{0,0,0,0},4,0,0,0},{{0,0,0,0},8,1,1,1},{{0,0,0,0},11,0,0,0}};
107     static const itemTest t2b2[5] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},3,0,0,2},{{0,0,0,0},4,0,0,2},{{0,0,0,0},7,1,1,1},{{0,0,0,0},11,0,0,0}};
108     static const itemTest t2b3[3] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},7,1,1,1},{{0,0,0,0},11,0,0,0}};
109
110     /* leading space */
111     static const WCHAR test2c[] = {' ',0x0621,0x0623,0x0624,'A','B','C','-','D','E','F',0};
112     static const itemTest t2c1[5] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0},{{0,0,0,0},7,0,0,0},{{0,0,0,0},8,0,0,0},{{0,0,0,0},11,0,0,0}};
113     static const itemTest t2c2[6] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},1,1,1,1},{{0,0,0,0},4,0,0,0},{{0,0,0,0},7,0,0,0},{{0,0,0,0},8,0,0,0},{{0,0,0,0},11,0,0,0}};
114     static const itemTest t2c3[5] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,2},{{0,0,0,0},7,0,0,2},{{0,0,0,0},8,0,0,2},{{0,0,0,0},11,0,0,0}};
115     static const itemTest t2c4[3] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,2},{{0,0,0,0},11,0,0,0}};
116
117     /* trailing space */
118     static const WCHAR test2d[] = {'A','B','C','-','D','E','F',0x0621,0x0623,0x0624,' ',0};
119     static const itemTest t2d1[5] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},3,0,0,0},{{0,0,0,0},4,0,0,0},{{0,0,0,0},7,1,1,1},{{0,0,0,0},11,0,0,0}};
120     static const itemTest t2d2[6] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},3,0,0,0},{{0,0,0,0},4,0,0,0},{{0,0,0,0},7,1,1,1},{{0,0,0,0},10,0,0,0},{{0,0,0,0},11,0,0,0}};
121     static const itemTest t2d3[5] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},3,0,0,2},{{0,0,0,0},4,0,0,2},{{0,0,0,0},7,1,1,1},{{0,0,0,0},11,0,0,0}};
122     static const itemTest t2d4[3] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},7,1,1,1},{{0,0,0,0},11,0,0,0}};
123
124     /* Thai */
125     static const WCHAR test3[] =
126 {0x0e04,0x0e27,0x0e32,0x0e21,0x0e1e,0x0e22,0x0e32,0x0e22,0x0e32, 0x0e21
127 ,0x0e2d,0x0e22,0x0e39,0x0e48,0x0e17,0x0e35,0x0e48,0x0e44,0x0e2b,0x0e19
128 ,0x0e04,0x0e27,0x0e32,0x0e21,0x0e2a, 0x0e33,0x0e40,0x0e23,0x0e47,0x0e08,
129  0x0e2d,0x0e22,0x0e39,0x0e48,0x0e17,0x0e35,0x0e48,0x0e19,0x0e31,0x0e48,0x0e19,0};
130
131     static const itemTest t31[2] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},41,0,0,0}};
132     static const itemTest t32[2] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},41,0,0,0}};
133
134     static const WCHAR test4[]  = {'1','2','3','-','5','2',' ','i','s',' ','7','1','.',0};
135
136     static const itemTest t41[6] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},3,0,0,0},{{0,0,0,0},4,0,0,0},{{0,0,0,0},7,0,0,0},{{0,0,0,0},10,0,0,0},{{0,0,0,0},12,0,0,0}};
137     static const itemTest t42[5] = {{{0,0,1,0},0,0,1,2},{{0,0,0,0},6,1,1,1},{{0,0,0,0},7,0,0,2},{{0,0,0,0},10,0,0,2},{{0,0,0,0},12,0,0,0}};
138     static const itemTest t43[4] = {{{0,0,1,0},0,0,1,2},{{0,0,0,0},6,1,1,1},{{0,0,0,0},7,0,0,2},{{0,0,0,0},12,0,0,0}};
139
140     /* Arabic */
141     static const WCHAR test5[]  =
142 {0x0627,0x0644,0x0635,0x0651,0x0650,0x062d,0x0629,0x064f,' ',0x062a,0x064e,
143 0x0627,0x062c,0x064c,' ',0x0639,0x064e,0x0644,0x0649,' ',
144 0x0631,0x064f,0x0624,0x0648,0x0633,0x0650,' ',0x0627,0x0644
145 ,0x0623,0x0635,0x0650,0x062d,0x0651,0x064e,0x0627,0x0621,0x0650,0};
146     static const itemTest t51[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},38,0,0,0}};
147
148     /* Hebrew */
149     static const WCHAR test6[]  = {0x05e9, 0x05dc, 0x05d5, 0x05dd, '.',0};
150     static const itemTest t61[3] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0},{{0,0,0,0},5,0,0,0}};
151     static const itemTest t62[3] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,1,1,1},{{0,0,0,0},5,0,0,0}};
152     static const itemTest t63[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},5,0,0,0}};
153     static const WCHAR test7[]  = {'p','a','r','t',' ','o','n','e',' ',0x05d7, 0x05dc, 0x05e7, ' ', 0x05e9, 0x05ea, 0x05d9, 0x05d9, 0x05dd, ' ','p','a','r','t',' ','t','h','r','e','e', 0};
154     static const itemTest t71[4] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},9,1,1,1},{{0,0,0,0},19,0,0,0},{{0,0,0,0},29,0,0,0}};
155     static const itemTest t72[4] = {{{0,0,0,0},0,0,0,0},{{0,0,0,0},9,1,1,1},{{0,0,0,0},18,0,0,0},{{0,0,0,0},29,0,0,0}};
156     static const itemTest t73[4] = {{{0,0,0,0},0,0,0,2},{{0,0,0,0},8,1,1,1},{{0,0,0,0},19,0,0,2},{{0,0,0,0},29,0,0,0}};
157     static const WCHAR test8[] = {0x0633, 0x0644, 0x0627, 0x0645,0};
158     static const itemTest t81[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0}};
159
160     /* Syriac  (Like Arabic )*/
161     static const WCHAR test9[] = {0x0710, 0x0712, 0x0712, 0x0714, '.',0};
162     static const itemTest t91[3] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0},{{0,0,0,0},5,0,0,0}};
163     static const itemTest t92[3] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,1,1,1},{{0,0,0,0},5,0,0,0}};
164     static const itemTest t93[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},5,0,0,0}};
165
166     static const WCHAR test10[] = {0x0717, 0x0718, 0x071a, 0x071b,0};
167     static const itemTest t101[2] = {{{0,0,0,0},0,1,1,1},{{0,0,0,0},4,0,0,0}};
168
169     SCRIPT_ITEM items[15];
170     SCRIPT_CONTROL  Control;
171     SCRIPT_STATE    State;
172     HRESULT hr;
173     int nItems;
174
175     memset(&Control, 0, sizeof(Control));
176     memset(&State, 0, sizeof(State));
177
178     hr = ScriptItemize(NULL, 4, 10, &Control, &State, items, NULL);
179     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pwcInChars is NULL\n");
180
181     hr = ScriptItemize(test1, 4, 10, &Control, &State, NULL, NULL);
182     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pItems is NULL\n");
183
184     hr = ScriptItemize(test1, 4, 1, &Control, &State, items, NULL);
185     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cMaxItems < 2.\n");
186
187     hr = ScriptItemize(test1, 0, 10, NULL, NULL, items, &nItems);
188     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cInChars is 0\n");
189
190     test_items_ok(test1,4,NULL,NULL,1,t11,FALSE,0);
191     test_items_ok(test1b,4,NULL,NULL,1,t1b1,FALSE,0);
192     test_items_ok(test2,16,NULL,NULL,6,t21,FALSE,0);
193     test_items_ok(test2b,11,NULL,NULL,4,t2b1,FALSE,0);
194     test_items_ok(test2c,11,NULL,NULL,4,t2c1,FALSE,0);
195     test_items_ok(test2d,11,NULL,NULL,4,t2d1,FALSE,0);
196     test_items_ok(test3,41,NULL,NULL,1,t31,FALSE,0);
197     test_items_ok(test4,12,NULL,NULL,5,t41,FALSE,0);
198     test_items_ok(test5,38,NULL,NULL,1,t51,FALSE,0);
199     test_items_ok(test6,5,NULL,NULL,2,t61,FALSE,0);
200     test_items_ok(test7,29,NULL,NULL,3,t71,FALSE,0);
201     test_items_ok(test8,4,NULL,NULL,1,t81,FALSE,0);
202     test_items_ok(test9,5,NULL,NULL,2,t91,FALSE,0);
203     test_items_ok(test10,4,NULL,NULL,1,t101,FALSE,0);
204
205     State.uBidiLevel = 0;
206     test_items_ok(test1,4,&Control,&State,1,t11,FALSE,0);
207     test_items_ok(test1b,4,&Control,&State,1,t1b1,FALSE,0);
208     test_items_ok(test2,16,&Control,&State,4,t22,FALSE,0);
209     test_items_ok(test2b,11,&Control,&State,4,t2b1,FALSE,0);
210     test_items_ok(test2c,11,&Control,&State,5,t2c2,FALSE,0);
211     test_items_ok(test2d,11,&Control,&State,5,t2d2,FALSE,0);
212     test_items_ok(test3,41,&Control,&State,1,t31,FALSE,0);
213     test_items_ok(test4,12,&Control,&State,5,t41,FALSE,0);
214     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
215     test_items_ok(test6,5,&Control,&State,2,t61,FALSE,0);
216     test_items_ok(test7,29,&Control,&State,3,t72,FALSE,0);
217     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
218     test_items_ok(test9,5,&Control,&State,2,t91,FALSE,0);
219     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
220
221     State.uBidiLevel = 1;
222     test_items_ok(test1,4,&Control,&State,1,t12,FALSE,0);
223     test_items_ok(test1b,4,&Control,&State,1,t1b2,FALSE,0);
224     test_items_ok(test2,16,&Control,&State,4,t23,FALSE,0);
225     test_items_ok(test2b,11,&Control,&State,4,t2b2,FALSE,0);
226     test_items_ok(test2c,11,&Control,&State,4,t2c3,FALSE,0);
227     test_items_ok(test2d,11,&Control,&State,4,t2d3,FALSE,0);
228     test_items_ok(test3,41,&Control,&State,1,t32,FALSE,0);
229     test_items_ok(test4,12,&Control,&State,4,t42,FALSE,0);
230     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
231     test_items_ok(test6,5,&Control,&State,2,t62,FALSE,0);
232     test_items_ok(test7,29,&Control,&State,3,t73,FALSE,0);
233     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
234     test_items_ok(test9,5,&Control,&State,2,t92,FALSE,0);
235     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
236
237     State.uBidiLevel = 1;
238     Control.fMergeNeutralItems = TRUE;
239     test_items_ok(test1,4,&Control,&State,1,t12,FALSE,0);
240     test_items_ok(test1b,4,&Control,&State,1,t1b2,FALSE,0);
241     test_items_ok(test2,16,&Control,&State,4,t23,FALSE,0);
242     test_items_ok(test2b,11,&Control,&State,2,t2b3,FALSE,4);
243     test_items_ok(test2c,11,&Control,&State,2,t2c4,FALSE,4);
244     test_items_ok(test2d,11,&Control,&State,2,t2d4,FALSE,4);
245     test_items_ok(test3,41,&Control,&State,1,t32,FALSE,0);
246     test_items_ok(test4,12,&Control,&State,3,t43,FALSE,4);
247     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
248     test_items_ok(test6,5,&Control,&State,1,t63,FALSE,2);
249     test_items_ok(test7,29,&Control,&State,3,t73,FALSE,0);
250     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
251     test_items_ok(test9,5,&Control,&State,1,t93,FALSE,2);
252     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
253 }
254
255
256 static void test_ScriptShape(HDC hdc)
257 {
258     static const WCHAR test1[] = {'w', 'i', 'n', 'e',0};
259     static const WCHAR test2[] = {0x202B, 'i', 'n', 0x202C,0};
260     HRESULT hr;
261     SCRIPT_CACHE sc = NULL;
262     WORD glyphs[4], glyphs2[4], logclust[4];
263     SCRIPT_VISATTR attrs[4];
264     SCRIPT_ITEM items[2];
265     int nb;
266
267     hr = ScriptItemize(test1, 4, 2, NULL, NULL, items, NULL);
268     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
269     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
270
271     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, NULL, &nb);
272     ok(hr == E_INVALIDARG, "ScriptShape should return E_INVALIDARG not %08x\n", hr);
273
274     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, NULL);
275     ok(hr == E_INVALIDARG, "ScriptShape should return E_INVALIDARG not %08x\n", hr);
276
277     hr = ScriptShape(NULL, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, &nb);
278     ok(hr == E_PENDING, "ScriptShape should return E_PENDING not %08x\n", hr);
279
280     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, &nb);
281     ok(broken(hr == S_OK) ||
282        hr == E_INVALIDARG || /* Vista, W2K8 */
283        hr == E_FAIL, /* WIN7 */
284        "ScriptShape should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
285     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
286
287     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
288     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
289     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
290
291
292     memset(glyphs,-1,sizeof(glyphs));
293     memset(logclust,-1,sizeof(logclust));
294     memset(attrs,-1,sizeof(attrs));
295     hr = ScriptShape(NULL, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
296     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
297     ok(nb == 4, "Wrong number of items\n");
298     ok(logclust[0] == 0, "clusters out of order\n");
299     ok(logclust[1] == 1, "clusters out of order\n");
300     ok(logclust[2] == 2, "clusters out of order\n");
301     ok(logclust[3] == 3, "clusters out of order\n");
302     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
303     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
304     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
305     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
306     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
307     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
308     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
309     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
310     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
311     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
312     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
313     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
314     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
315     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
316     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
317     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
318
319     ScriptFreeCache(&sc);
320     sc = NULL;
321
322     memset(glyphs2,-1,sizeof(glyphs2));
323     memset(logclust,-1,sizeof(logclust));
324     memset(attrs,-1,sizeof(attrs));
325     hr = ScriptShape(hdc, &sc, test2, 4, 4, &items[0].a, glyphs2, logclust, attrs, &nb);
326     ok(hr == S_OK, "ScriptShape should return S_OK not %08x\n", hr);
327     ok(nb == 4, "Wrong number of items\n");
328     ok(glyphs2[0] == 0 || broken(glyphs2[0] == 0x80), "Incorrect glyph for 0x202B\n");
329     ok(glyphs2[3] == 0 || broken(glyphs2[3] == 0x80), "Incorrect glyph for 0x202C\n");
330     ok(logclust[0] == 0, "clusters out of order\n");
331     ok(logclust[1] == 1, "clusters out of order\n");
332     ok(logclust[2] == 2, "clusters out of order\n");
333     ok(logclust[3] == 3, "clusters out of order\n");
334     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
335     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
336     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
337     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
338     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
339     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
340     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
341     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
342     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
343     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
344     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
345     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
346     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
347     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
348     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
349     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
350
351     /* modify LTR to RTL */
352     items[0].a.fRTL = 1;
353     memset(glyphs2,-1,sizeof(glyphs2));
354     memset(logclust,-1,sizeof(logclust));
355     memset(attrs,-1,sizeof(attrs));
356     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs2, logclust, attrs, &nb);
357     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
358     ok(nb == 4, "Wrong number of items\n");
359     ok(glyphs2[0] == glyphs[3], "Glyphs not reordered properly\n");
360     ok(glyphs2[1] == glyphs[2], "Glyphs not reordered properly\n");
361     ok(glyphs2[2] == glyphs[1], "Glyphs not reordered properly\n");
362     ok(glyphs2[3] == glyphs[0], "Glyphs not reordered properly\n");
363     ok(logclust[0] == 3, "clusters out of order\n");
364     ok(logclust[1] == 2, "clusters out of order\n");
365     ok(logclust[2] == 1, "clusters out of order\n");
366     ok(logclust[3] == 0, "clusters out of order\n");
367     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
368     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
369     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
370     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
371     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
372     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
373     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
374     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
375     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
376     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
377     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
378     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
379     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
380     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
381     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
382     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
383
384     ScriptFreeCache(&sc);
385 }
386
387 static void test_ScriptPlace(HDC hdc)
388 {
389     static const WCHAR test1[] = {'t', 'e', 's', 't',0};
390     BOOL ret;
391     HRESULT hr;
392     SCRIPT_CACHE sc = NULL;
393     WORD glyphs[4], logclust[4];
394     SCRIPT_VISATTR attrs[4];
395     SCRIPT_ITEM items[2];
396     int nb, widths[4];
397     GOFFSET offset[4];
398     ABC abc[4];
399
400     hr = ScriptItemize(test1, 4, 2, NULL, NULL, items, NULL);
401     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
402     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
403
404     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
405     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
406     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
407
408     hr = ScriptPlace(hdc, &sc, glyphs, 4, NULL, &items[0].a, widths, NULL, NULL);
409     ok(hr == E_INVALIDARG, "ScriptPlace should return E_INVALIDARG not %08x\n", hr);
410
411     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, NULL, NULL);
412     ok(broken(hr == E_PENDING) ||
413        hr == E_INVALIDARG || /* Vista, W2K8 */
414        hr == E_FAIL, /* WIN7 */
415        "ScriptPlace should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
416
417     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, offset, NULL);
418     ok(hr == E_PENDING, "ScriptPlace should return E_PENDING not %08x\n", hr);
419
420     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, NULL, abc);
421     ok(broken(hr == E_PENDING) ||
422        hr == E_INVALIDARG || /* Vista, W2K8 */
423        hr == E_FAIL, /* WIN7 */
424        "ScriptPlace should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
425
426     hr = ScriptPlace(hdc, &sc, glyphs, 4, attrs, &items[0].a, widths, offset, NULL);
427     ok(!hr, "ScriptPlace should return S_OK not %08x\n", hr);
428     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
429
430     ret = ExtTextOutW(hdc, 1, 1, 0, NULL, glyphs, 4, widths);
431     ok(ret, "ExtTextOutW should return TRUE\n");
432
433     ScriptFreeCache(&sc);
434 }
435
436 static void test_ScriptItemIzeShapePlace(HDC hdc, unsigned short pwOutGlyphs[256])
437 {
438     HRESULT         hr;
439     int             iMaxProps;
440     const SCRIPT_PROPERTIES **ppSp;
441
442     int             cInChars;
443     int             cMaxItems;
444     SCRIPT_ITEM     pItem[255];
445     int             pcItems;
446     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0}; 
447     WCHAR           TestItem2[] = {'T', 'e', 's', 't', 'b', 0}; 
448     WCHAR           TestItem3[] = {'T', 'e', 's', 't', 'c',' ','1','2','3',' ',' ','e','n','d',0};
449     WCHAR           TestItem4[] = {'T', 'e', 's', 't', 'd',' ',0x0684,0x0694,0x06a4,' ',' ','\r','\n','e','n','d',0};
450     WCHAR           TestItem5[] = {0x0684,'T','e','s','t','e',' ',0x0684,0x0694,0x06a4,' ',' ','e','n','d',0};
451     WCHAR           TestItem6[] = {'T', 'e', 's', 't', 'f',' ',' ',' ','\r','\n','e','n','d',0};
452
453     SCRIPT_CACHE    psc;
454     int             cChars;
455     int             cMaxGlyphs;
456     unsigned short  pwOutGlyphs1[256];
457     unsigned short  pwOutGlyphs2[256];
458     unsigned short  pwLogClust[256];
459     SCRIPT_VISATTR  psva[256];
460     int             pcGlyphs;
461     int             piAdvance[256];
462     GOFFSET         pGoffset[256];
463     ABC             pABC[256];
464     int             cnt;
465
466     /* Start testing usp10 functions                                                         */
467     /* This test determines that the pointer returned by ScriptGetProperties is valid
468      * by checking a known value in the table                                                */
469     hr = ScriptGetProperties(&ppSp, &iMaxProps);
470     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
471     trace("number of script properties %d\n", iMaxProps);
472     ok (iMaxProps > 0, "Number of scripts returned should not be 0\n");
473     if  (iMaxProps > 0)
474          ok( ppSp[0]->langid == 0, "Langid[0] not = to 0\n"); /* Check a known value to ensure   */
475                                                               /* ptrs work                       */
476
477     /* This is a valid test that will cause parsing to take place                             */
478     cInChars = 5;
479     cMaxItems = 255;
480     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
481     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
482     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
483      *  returned.                                                                             */
484     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
485     if (pcItems > 0)
486         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
487             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
488             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
489
490     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
491      * ie. ScriptItemize has succeeded and that pItem has been set                            */
492     cInChars = 5;
493     if (hr == 0) {
494         psc = NULL;                                   /* must be null on first call           */
495         cChars = cInChars;
496         cMaxGlyphs = cInChars;
497         hr = ScriptShape(NULL, &psc, TestItem1, cChars,
498                          cMaxGlyphs, &pItem[0].a,
499                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
500         ok (hr == E_PENDING, "If psc is NULL (%08x) the E_PENDING should be returned\n", hr);
501         cMaxGlyphs = 4;
502         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
503                          cMaxGlyphs, &pItem[0].a,
504                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
505         ok (hr == E_OUTOFMEMORY, "If not enough output area cChars (%d) is > than CMaxGlyphs "
506                                  "(%d) but not E_OUTOFMEMORY\n",
507                                  cChars, cMaxGlyphs);
508         cMaxGlyphs = 256;
509         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
510                          cMaxGlyphs, &pItem[0].a,
511                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
512         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
513         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
514         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
515         if (hr ==0) {
516             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
517                              pGoffset, pABC);
518             ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
519             hr = ScriptPlace(NULL, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
520                              pGoffset, pABC);
521             ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
522             for (cnt=0; cnt < pcGlyphs; cnt++)
523                 pwOutGlyphs[cnt] = pwOutGlyphs1[cnt];                 /* Send to next function */
524         }
525
526         /* This test will check to make sure that SCRIPT_CACHE is reused and that not translation   *
527          * takes place if fNoGlyphIndex is set.                                                     */
528
529         cInChars = 5;
530         cMaxItems = 255;
531         hr = ScriptItemize(TestItem2, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
532         ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
533         /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is   *
534          *  returned.                                                                               */
535         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
536                             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
537                              pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
538         /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue                    */
539         if (hr == 0) {
540              cChars = cInChars;
541              cMaxGlyphs = 256;
542              pItem[0].a.fNoGlyphIndex = 1;                /* say no translate                     */
543              hr = ScriptShape(NULL, &psc, TestItem2, cChars,
544                               cMaxGlyphs, &pItem[0].a,
545                               pwOutGlyphs2, pwLogClust, psva, &pcGlyphs);
546              ok (hr != E_PENDING, "If psc should not be NULL (%08x) and the E_PENDING should be returned\n", hr);
547              ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
548              ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
549              ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
550              for (cnt=0; cnt < cChars && TestItem2[cnt] == pwOutGlyphs2[cnt]; cnt++) {}
551              ok (cnt == cChars, "Translation to place when told not to. WCHAR %d - %04x != %04x\n",
552                            cnt, TestItem2[cnt], pwOutGlyphs2[cnt]);
553              if (hr ==0) {
554                  hr = ScriptPlace(hdc, &psc, pwOutGlyphs2, pcGlyphs, psva, &pItem[0].a, piAdvance,
555                                   pGoffset, pABC);
556                  ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
557              }
558         }
559         ScriptFreeCache( &psc);
560         ok (!psc, "psc is not null after ScriptFreeCache\n");
561
562     }
563
564     /* This is a valid test that will cause parsing to take place and create 3 script_items   */
565     cInChars = (sizeof(TestItem3)/2)-1;
566     cMaxItems = 255;
567     hr = ScriptItemize(TestItem3, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
568     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
569     if  (hr == 0)
570         {
571         ok (pcItems == 3, "The number of SCRIPT_ITEMS should be 3 not %d\n", pcItems);
572         if (pcItems > 2)
573         {
574             ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == 6,
575                 "Start pos [0] not = 0 (%d) or end pos [1] not = %d\n",
576                 pItem[0].iCharPos, pItem[1].iCharPos);
577             ok (pItem[1].iCharPos == 6 && pItem[2].iCharPos == 11,
578                 "Start pos [1] not = 6 (%d) or end pos [2] not = 11 (%d)\n",
579                 pItem[1].iCharPos, pItem[2].iCharPos);
580             ok (pItem[2].iCharPos == 11 && pItem[3].iCharPos == cInChars,
581                 "Start pos [2] not = 11 (%d) or end [3] pos not = 14 (%d), cInChars = %d\n",
582                 pItem[2].iCharPos, pItem[3].iCharPos, cInChars);
583         }
584     }
585
586     /* This is a valid test that will cause parsing to take place and create 5 script_items   */
587     cInChars = (sizeof(TestItem4)/2)-1;
588     cMaxItems = 255;
589     hr = ScriptItemize(TestItem4, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
590     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
591     if  (hr == 0)
592         {
593         ok (pcItems == 5, "The number of SCRIPT_ITEMS should be 5 not %d\n", pcItems);
594         if (pcItems > 4)
595         {
596             ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == 6,
597                 "Start pos [0] not = 0 (%d) or end pos [1] not = %d\n",
598                 pItem[0].iCharPos, pItem[1].iCharPos);
599             ok (pItem[0].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
600                                                pItem[0].a.s.uBidiLevel);
601             ok (pItem[1].iCharPos == 6 && pItem[2].iCharPos == 11,
602                 "Start pos [1] not = 6 (%d) or end pos [2] not = 11 (%d)\n",
603                 pItem[1].iCharPos, pItem[2].iCharPos);
604             ok (pItem[1].a.s.uBidiLevel == 1, "Should have been bidi=1 not %d\n",
605                                               pItem[1].a.s.uBidiLevel);
606             ok (pItem[2].iCharPos == 11 && pItem[3].iCharPos == 12,
607                 "Start pos [2] not = 11 (%d) or end [3] pos not = 12 (%d)\n",
608                 pItem[2].iCharPos, pItem[3].iCharPos);
609             ok (pItem[2].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
610                                                pItem[2].a.s.uBidiLevel);
611             ok (pItem[3].iCharPos == 12 && pItem[4].iCharPos == 13,
612                 "Start pos [3] not = 12 (%d) or end [4] pos not = 13 (%d)\n",
613                 pItem[3].iCharPos, pItem[4].iCharPos);
614             ok (pItem[3].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
615                                                pItem[3].a.s.uBidiLevel);
616             ok (pItem[4].iCharPos == 13 && pItem[5].iCharPos == cInChars,
617                 "Start pos [4] not = 13 (%d) or end [5] pos not = 16 (%d), cInChars = %d\n",
618                 pItem[4].iCharPos, pItem[5].iCharPos, cInChars);
619         }
620     }
621
622     /*
623      * This test is for when the first unicode character requires bidi support
624      */
625     cInChars = (sizeof(TestItem5)-1)/sizeof(WCHAR);
626     hr = ScriptItemize(TestItem5, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
627     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
628     ok (pcItems == 4, "There should have been 4 items, found %d\n", pcItems);
629     ok (pItem[0].a.s.uBidiLevel == 1, "The first character should have been bidi=1 not %d\n",
630                                        pItem[0].a.s.uBidiLevel);
631
632     /* This test checks to make sure that the test to see if there are sufficient buffers to store  *
633      * the pointer to the last char works.  Note that windows often needs a greater number of       *
634      * SCRIPT_ITEMS to process a string than is returned in pcItems.                                */
635     cInChars = (sizeof(TestItem6)/2)-1;
636     cMaxItems = 4;
637     hr = ScriptItemize(TestItem6, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
638     ok (hr == E_OUTOFMEMORY, "ScriptItemize should return E_OUTOFMEMORY, returned %08x\n", hr);
639
640 }
641
642 static void test_ScriptGetCMap(HDC hdc, unsigned short pwOutGlyphs[256])
643 {
644     HRESULT         hr;
645     SCRIPT_CACHE    psc = NULL;
646     int             cInChars;
647     int             cChars;
648     unsigned short  pwOutGlyphs2[256];
649     unsigned short  pwOutGlyphs3[256];
650     DWORD           dwFlags;
651     int             cnt;
652
653     static const WCHAR TestItem1[] = {'T', 'e', 's', 't', 'a', 0};
654     static const WCHAR TestItem2[] = {0x202B, 'i', 'n', 0x202C,0};
655     static const WCHAR TestItem3[] = {'a','b','c','d','(','<','{','[',0x2039,0};
656     static const WCHAR TestItem3b[] = {'a','b','c','d',')','>','}',']',0x203A,0};
657
658     /*  Check to make sure that SCRIPT_CACHE gets allocated ok                     */
659     dwFlags = 0;
660     cInChars = cChars = 5;
661     /* Some sanity checks for ScriptGetCMap */
662
663     hr = ScriptGetCMap(NULL, NULL, NULL, 0, 0, NULL);
664     ok( hr == E_INVALIDARG, "(NULL,NULL,NULL,0,0,NULL), "
665                             "expected E_INVALIDARG, got %08x\n", hr);
666
667     hr = ScriptGetCMap(NULL, NULL, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
668     ok( hr == E_INVALIDARG, "(NULL,NULL,TestItem1, cInChars, dwFlags, pwOutGlyphs3), "
669                             "expected E_INVALIDARG, got %08x\n", hr);
670
671     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
672     psc = NULL;
673     hr = ScriptGetCMap(NULL, &psc, TestItem1, cInChars, 0, pwOutGlyphs3);
674     ok( hr == E_PENDING, "(NULL,&psc,NULL,0,0,NULL), expected E_PENDING, "
675                          "got %08x\n", hr);
676     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
677
678     /* Set psc to NULL but add hdc, to be able to check if a pointer is returned in psc */
679     psc = NULL;
680     hr = ScriptGetCMap(hdc, &psc, TestItem1, cInChars, 0, pwOutGlyphs3);
681     ok( hr == S_OK, "ScriptGetCMap(NULL,&psc,NULL,0,0,NULL), expected S_OK, "
682                     "got %08x\n", hr);
683     ok( psc != NULL, "ScritpGetCMap expected psc to be not NULL\n");
684     ScriptFreeCache( &psc);
685
686     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
687     psc = NULL;
688     hr = ScriptGetCMap(NULL, &psc, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
689     ok( hr == E_PENDING, "(NULL,&psc,), expected E_PENDING, got %08x\n", hr);
690     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
691     /*  Check to see if the results are the same as those returned by ScriptShape  */
692     hr = ScriptGetCMap(hdc, &psc, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
693     ok (hr == 0, "ScriptGetCMap should return 0 not (%08x)\n", hr);
694     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
695     for (cnt=0; cnt < cChars && pwOutGlyphs[cnt] == pwOutGlyphs3[cnt]; cnt++) {}
696     ok (cnt == cInChars, "Translation not correct. WCHAR %d - %04x != %04x\n",
697                          cnt, pwOutGlyphs[cnt], pwOutGlyphs3[cnt]);
698
699     ScriptFreeCache( &psc);
700     ok (!psc, "psc is not null after ScriptFreeCache\n");
701
702     cInChars = cChars = 4;
703     hr = ScriptGetCMap(hdc, &psc, TestItem2, cInChars, dwFlags, pwOutGlyphs3);
704     ok (hr == S_FALSE, "ScriptGetCMap should return S_FALSE not (%08x)\n", hr);
705     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
706     ok(pwOutGlyphs3[0] == 0 || broken(pwOutGlyphs3[0] == 0x80), "Glyph 0 should be default glyph\n");
707     ok(pwOutGlyphs3[3] == 0 || broken(pwOutGlyphs3[0] == 0x80), "Glyph 0 should be default glyph\n");
708
709
710     cInChars = cChars = 9;
711     hr = ScriptGetCMap(hdc, &psc, TestItem3b, cInChars, dwFlags, pwOutGlyphs2);
712     ok (hr == S_OK, "ScriptGetCMap should return S_OK not (%08x)\n", hr);
713     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
714
715     cInChars = cChars = 9;
716     dwFlags = SGCM_RTL;
717     hr = ScriptGetCMap(hdc, &psc, TestItem3, cInChars, dwFlags, pwOutGlyphs3);
718     ok (hr == S_OK, "ScriptGetCMap should return S_OK not (%08x)\n", hr);
719     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
720     ok(pwOutGlyphs3[0] == pwOutGlyphs2[0], "glyph incorrectly altered\n");
721     ok(pwOutGlyphs3[1] == pwOutGlyphs2[1], "glyph incorreclty altered\n");
722     ok(pwOutGlyphs3[2] == pwOutGlyphs2[2], "glyph incorreclty altered\n");
723     ok(pwOutGlyphs3[3] == pwOutGlyphs2[3], "glyph incorreclty altered\n");
724     ok(pwOutGlyphs3[4] == pwOutGlyphs2[4], "glyph not mirrored correctly\n");
725     ok(pwOutGlyphs3[5] == pwOutGlyphs2[5], "glyph not mirrored correctly\n");
726     ok(pwOutGlyphs3[6] == pwOutGlyphs2[6], "glyph not mirrored correctly\n");
727     ok(pwOutGlyphs3[7] == pwOutGlyphs2[7], "glyph not mirrored correctly\n");
728     ok(pwOutGlyphs3[8] == pwOutGlyphs2[8], "glyph not mirrored correctly\n");
729
730     ScriptFreeCache( &psc);
731     ok (!psc, "psc is not null after ScriptFreeCache\n");
732 }
733
734 static void test_ScriptGetFontProperties(HDC hdc)
735 {
736     HRESULT         hr;
737     SCRIPT_CACHE    psc,old_psc;
738     SCRIPT_FONTPROPERTIES sfp;
739
740     /* Some sanity checks for ScriptGetFontProperties */
741
742     hr = ScriptGetFontProperties(NULL,NULL,NULL);
743     ok( hr == E_INVALIDARG, "(NULL,NULL,NULL), expected E_INVALIDARG, got %08x\n", hr);
744
745     hr = ScriptGetFontProperties(NULL,NULL,&sfp);
746     ok( hr == E_INVALIDARG, "(NULL,NULL,&sfp), expected E_INVALIDARG, got %08x\n", hr);
747
748     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
749     psc = NULL;
750     hr = ScriptGetFontProperties(NULL,&psc,NULL);
751     ok( hr == E_INVALIDARG, "(NULL,&psc,NULL), expected E_INVALIDARG, got %08x\n", hr);
752     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
753
754     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
755     psc = NULL;
756     hr = ScriptGetFontProperties(NULL,&psc,&sfp);
757     ok( hr == E_PENDING, "(NULL,&psc,&sfp), expected E_PENDING, got %08x\n", hr);
758     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
759
760     hr = ScriptGetFontProperties(hdc,NULL,NULL);
761     ok( hr == E_INVALIDARG, "(hdc,NULL,NULL), expected E_INVALIDARG, got %08x\n", hr);
762
763     hr = ScriptGetFontProperties(hdc,NULL,&sfp);
764     ok( hr == E_INVALIDARG, "(hdc,NULL,&sfp), expected E_INVALIDARG, got %08x\n", hr);
765
766     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
767     psc = NULL;
768     hr = ScriptGetFontProperties(hdc,&psc,NULL);
769     ok( hr == E_INVALIDARG, "(hdc,&psc,NULL), expected E_INVALIDARG, got %08x\n", hr);
770     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
771
772     /* Pass an invalid sfp */
773     psc = NULL;
774     sfp.cBytes = sizeof(SCRIPT_FONTPROPERTIES) - 1;
775     hr = ScriptGetFontProperties(hdc,&psc,&sfp);
776     ok( hr == E_INVALIDARG, "(hdc,&psc,&sfp) invalid, expected E_INVALIDARG, got %08x\n", hr);
777     ok( psc != NULL, "Expected a pointer in psc, got NULL\n");
778     ScriptFreeCache(&psc);
779     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
780
781     /* Give it the correct cBytes, we don't care about what's coming back */
782     sfp.cBytes = sizeof(SCRIPT_FONTPROPERTIES);
783     psc = NULL;
784     hr = ScriptGetFontProperties(hdc,&psc,&sfp);
785     ok( hr == S_OK, "(hdc,&psc,&sfp) partly initialized, expected S_OK, got %08x\n", hr);
786     ok( psc != NULL, "Expected a pointer in psc, got NULL\n");
787
788     /* Save the psc pointer */
789     old_psc = psc;
790     /* Now a NULL hdc again */
791     hr = ScriptGetFontProperties(NULL,&psc,&sfp);
792     ok( hr == S_OK, "(NULL,&psc,&sfp), expected S_OK, got %08x\n", hr);
793     ok( psc == old_psc, "Expected psc not to be changed, was %p is now %p\n", old_psc, psc);
794     ScriptFreeCache(&psc);
795     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
796 }
797
798 static void test_ScriptTextOut(HDC hdc)
799 {
800     HRESULT         hr;
801
802     int             cInChars;
803     int             cMaxItems;
804     SCRIPT_ITEM     pItem[255];
805     int             pcItems;
806     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0}; 
807
808     SCRIPT_CACHE    psc;
809     int             cChars;
810     int             cMaxGlyphs;
811     unsigned short  pwOutGlyphs1[256];
812     WORD            pwLogClust[256];
813     SCRIPT_VISATTR  psva[256];
814     int             pcGlyphs;
815     int             piAdvance[256];
816     GOFFSET         pGoffset[256];
817     ABC             pABC[256];
818     RECT            rect;
819     int             piX;
820     int             iCP = 1;
821     BOOL            fTrailing = FALSE;
822     SCRIPT_LOGATTR  *psla;
823     SCRIPT_LOGATTR  sla[256];
824
825     /* This is a valid test that will cause parsing to take place                             */
826     cInChars = 5;
827     cMaxItems = 255;
828     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
829     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
830     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
831      *  returned.                                                                             */
832     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
833     if (pcItems > 0)
834         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
835             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
836             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
837
838     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
839      * ie. ScriptItemize has succeeded and that pItem has been set                            */
840     cInChars = 5;
841     if (hr == 0) {
842         psc = NULL;                                   /* must be null on first call           */
843         cChars = cInChars;
844         cMaxGlyphs = 256;
845         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
846                          cMaxGlyphs, &pItem[0].a,
847                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
848         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
849         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
850         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
851         if (hr ==0) {
852             /* Note hdc is needed as glyph info is not yet in psc                  */
853             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
854                              pGoffset, pABC);
855             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
856             ScriptFreeCache(&psc);              /* Get rid of psc for next test set */
857             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
858
859             hr = ScriptTextOut(NULL, NULL, 0, 0, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL);
860             ok (hr == E_INVALIDARG, "Should return 0 not (%08x)\n", hr);
861
862             hr = ScriptTextOut(NULL, NULL, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
863                                piAdvance, NULL, pGoffset);
864             ok( hr == E_INVALIDARG, "(NULL,NULL,TestItem1, cInChars, dwFlags, pwOutGlyphs3), "
865                                     "expected E_INVALIDARG, got %08x\n", hr);
866
867             /* Set psc to NULL, to be able to check if a pointer is returned in psc */
868             psc = NULL;
869             hr = ScriptTextOut(NULL, &psc, 0, 0, 0, NULL, NULL, NULL, 0, NULL, 0,
870                                NULL, NULL, NULL);
871             ok( hr == E_INVALIDARG, "(NULL,&psc,NULL,0,0,0,NULL,), expected E_INVALIDARG, "
872                                     "got %08x\n", hr);
873             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
874
875             /* hdc is required for this one rather than the usual optional          */
876             psc = NULL;
877             hr = ScriptTextOut(NULL, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
878                                piAdvance, NULL, pGoffset);
879             ok( hr == E_INVALIDARG, "(NULL,&psc,), expected E_INVALIDARG, got %08x\n", hr);
880             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
881
882             /* Set that it returns 0 status */
883             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
884                                piAdvance, NULL, pGoffset);
885             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
886
887             /* Test Rect Rgn is acceptable */
888             rect.top = 10;
889             rect.bottom = 20;
890             rect.left = 10;
891             rect.right = 40;
892             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, &rect, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
893                                piAdvance, NULL, pGoffset);
894             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
895
896             iCP = 1;
897             hr = ScriptCPtoX(iCP, fTrailing, cChars, pcGlyphs, (const WORD *) &pwLogClust,
898                             (const SCRIPT_VISATTR *) &psva, (const int *)&piAdvance, &pItem[0].a, &piX);
899             ok(hr == S_OK, "ScriptCPtoX Stub should return S_OK not %08x\n", hr);
900
901             psla = (SCRIPT_LOGATTR *)&sla;
902             hr = ScriptBreak(TestItem1, cChars, &pItem[0].a, psla);
903             ok(hr == S_OK, "ScriptBreak Stub should return S_OK not %08x\n", hr);
904
905             /* Clean up and go   */
906             ScriptFreeCache(&psc);
907             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
908         }
909     }
910 }
911
912 static void test_ScriptTextOut2(HDC hdc)
913 {
914 /*  Intent is to validate that the HDC passed into ScriptTextOut is
915  *  used instead of the (possibly) invalid cached one
916  */
917     HRESULT         hr;
918
919     HDC             hdc1, hdc2;
920     int             cInChars;
921     int             cMaxItems;
922     SCRIPT_ITEM     pItem[255];
923     int             pcItems;
924     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0};
925
926     SCRIPT_CACHE    psc;
927     int             cChars;
928     int             cMaxGlyphs;
929     unsigned short  pwOutGlyphs1[256];
930     WORD            pwLogClust[256];
931     SCRIPT_VISATTR  psva[256];
932     int             pcGlyphs;
933     int             piAdvance[256];
934     GOFFSET         pGoffset[256];
935     ABC             pABC[256];
936
937     /* Create an extra DC that will be used until the ScriptTextOut */
938     hdc1 = CreateCompatibleDC(hdc);
939     ok (hdc1 != 0, "CreateCompatibleDC failed to create a DC\n");
940     hdc2 = CreateCompatibleDC(hdc);
941     ok (hdc2 != 0, "CreateCompatibleDC failed to create a DC\n");
942
943     /* This is a valid test that will cause parsing to take place                             */
944     cInChars = 5;
945     cMaxItems = 255;
946     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
947     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
948     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
949      *  returned.                                                                             */
950     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
951     if (pcItems > 0)
952         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
953             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
954             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
955
956     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
957      * ie. ScriptItemize has succeeded and that pItem has been set                            */
958     cInChars = 5;
959     if (hr == 0) {
960         psc = NULL;                                   /* must be null on first call           */
961         cChars = cInChars;
962         cMaxGlyphs = 256;
963         hr = ScriptShape(hdc2, &psc, TestItem1, cChars,
964                          cMaxGlyphs, &pItem[0].a,
965                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
966         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
967         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
968         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
969         if (hr ==0) {
970             /* Note hdc is needed as glyph info is not yet in psc                  */
971             hr = ScriptPlace(hdc2, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
972                              pGoffset, pABC);
973             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
974
975             /*   key part!!!   cached dc is being deleted  */
976             hr = DeleteDC(hdc2);
977             ok(hr == 1, "DeleteDC should return 1 not %08x\n", hr);
978
979             /* At this point the cached hdc (hdc2) has been destroyed,
980              * however, we are passing in a *real* hdc (the original hdc).
981              * The text should be written to that DC
982              */
983             hr = ScriptTextOut(hdc1, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
984                                piAdvance, NULL, pGoffset);
985             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
986             ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
987
988             DeleteDC(hdc1);
989
990             /* Clean up and go   */
991             ScriptFreeCache(&psc);
992             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
993         }
994     }
995 }
996
997 static void test_ScriptTextOut3(HDC hdc)
998 {
999     HRESULT         hr;
1000
1001     int             cInChars;
1002     int             cMaxItems;
1003     SCRIPT_ITEM     pItem[255];
1004     int             pcItems;
1005     WCHAR           TestItem1[] = {' ','\r', 0};
1006
1007     SCRIPT_CACHE    psc;
1008     int             cChars;
1009     int             cMaxGlyphs;
1010     unsigned short  pwOutGlyphs1[256];
1011     WORD            pwLogClust[256];
1012     SCRIPT_VISATTR  psva[256];
1013     int             pcGlyphs;
1014     int             piAdvance[256];
1015     GOFFSET         pGoffset[256];
1016     ABC             pABC[256];
1017     RECT            rect;
1018
1019     /* This is to ensure that non exisiting glyphs are translated into a valid glyph number */
1020     cInChars = 2;
1021     cMaxItems = 255;
1022     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1023     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1024     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
1025      *  returned.                                                                             */
1026     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
1027     if (pcItems > 0)
1028         ok (pItem[0].iCharPos == 0 && pItem[2].iCharPos == cInChars,
1029             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1030             pItem[0].iCharPos, cInChars, pItem[2].iCharPos);
1031
1032     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
1033      * ie. ScriptItemize has succeeded and that pItem has been set                            */
1034     cInChars = 2;
1035     if (hr == 0) {
1036         psc = NULL;                                   /* must be null on first call           */
1037         cChars = cInChars;
1038         cMaxGlyphs = 256;
1039         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
1040                          cMaxGlyphs, &pItem[0].a,
1041                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1042         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1043         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1044         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1045         if (hr ==0) {
1046             /* Note hdc is needed as glyph info is not yet in psc                  */
1047             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1048                              pGoffset, pABC);
1049             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
1050
1051             /* Test Rect Rgn is acceptable */
1052             rect.top = 10;
1053             rect.bottom = 20;
1054             rect.left = 10;
1055             rect.right = 40;
1056             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, &rect, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1057                                piAdvance, NULL, pGoffset);
1058             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
1059
1060         }
1061         /* Clean up and go   */
1062         ScriptFreeCache(&psc);
1063         ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1064     }
1065 }
1066
1067 static void test_ScriptXtoX(void)
1068 /****************************************************************************************
1069  *  This routine tests the ScriptXtoCP and ScriptCPtoX functions using static variables *
1070  ****************************************************************************************/
1071 {
1072     static const WCHAR test[] = {'t', 'e', 's', 't',0};
1073     SCRIPT_ITEM items[2];
1074     int iX, iCP;
1075     int cChars;
1076     int cGlyphs;
1077     WORD pwLogClustXtoCP[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1078     WORD pwLogClustCPtoX[10] = {0, 0, 0, 1, 1, 2, 2, 3, 3, 3};
1079     SCRIPT_VISATTR psva[10];
1080     int piAdvance[10] = {200, 190, 210, 180, 170, 204, 189, 195, 212, 203};
1081     int piCP, piX;
1082     int piTrailing;
1083     BOOL fTrailing;
1084     HRESULT hr;
1085
1086     hr = ScriptItemize(test, lstrlenW(test), sizeof(items)/sizeof(items[0]), NULL, NULL, items, NULL);
1087     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
1088
1089     iX = -1;
1090     cChars = 10;
1091     cGlyphs = 10;
1092     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1093     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1094     if (piTrailing)
1095         ok(piCP == -1, "Negative iX should return piCP=-1 not %d\n", piCP);
1096     else /* win2k3 */
1097         ok(piCP == 10, "Negative iX should return piCP=10 not %d\n", piCP);
1098
1099     iX = 1954;
1100     cChars = 10;
1101     cGlyphs = 10;
1102     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1103     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1104     if (piTrailing) /* win2k3 */
1105         ok(piCP == -1, "Negative iX should return piCP=-1 not %d\n", piCP);
1106     else
1107         ok(piCP == 10, "Negative iX should return piCP=10 not %d\n", piCP);
1108
1109     iX = 779;
1110     cChars = 10;
1111     cGlyphs = 10;
1112     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1113     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1114     ok(piCP == 3 ||
1115        piCP == -1, /* win2k3 */
1116        "iX=%d should return piCP=3 or piCP=-1 not %d\n", iX, piCP);
1117     ok(piTrailing == 1, "iX=%d should return piTrailing=1 not %d\n", iX, piTrailing);
1118
1119     iX = 780;
1120     cChars = 10;
1121     cGlyphs = 10;
1122     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1123     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1124     ok(piCP == 3 ||
1125        piCP == -1, /* win2k3 */
1126        "iX=%d should return piCP=3 or piCP=-1 not %d\n", iX, piCP);
1127     ok(piTrailing == 1, "iX=%d should return piTrailing=1 not %d\n", iX, piTrailing);
1128
1129     iX = 868;
1130     cChars = 10;
1131     cGlyphs = 10;
1132     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1133     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1134     ok(piCP == 4 ||
1135        piCP == -1, /* win2k3 */
1136        "iX=%d should return piCP=4 or piCP=-1 not %d\n", iX, piCP);
1137
1138     iX = 0;
1139     cChars = 10;
1140     cGlyphs = 10;
1141     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1142     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1143     ok(piCP == 0 ||
1144        piCP == 10, /* win2k3 */
1145        "iX=%d should return piCP=0 piCP=10 not %d\n", iX, piCP);
1146
1147     iX = 195;
1148     cChars = 10;
1149     cGlyphs = 10;
1150     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1151     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1152     ok(piCP == 0, "iX=%d should return piCP=0 not %d\n", iX, piCP);
1153
1154     iX = 196;
1155     cChars = 10;
1156     cGlyphs = 10;
1157     hr = ScriptXtoCP(iX, cChars, cGlyphs, pwLogClustXtoCP, psva, piAdvance, &items[0].a, &piCP, &piTrailing);
1158     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1159     ok(piCP == 1 ||
1160        piCP == 0, /* win2k3 */
1161        "iX=%d should return piCP=1 or piCP=0 not %d\n", iX, piCP);
1162
1163     iCP=0;
1164     fTrailing = FALSE;
1165     cChars = 10;
1166     cGlyphs = 10;
1167     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1168     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1169     ok(piX == 0,
1170        "iCP=%d should return piX=0 not %d\n", iCP, piX);
1171
1172     iCP=1;
1173     fTrailing = FALSE;
1174     cChars = 10;
1175     cGlyphs = 10;
1176     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1177     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1178     ok(piX == 66,
1179        "iCP=%d should return piX=66 not %d\n", iCP, piX);
1180
1181     iCP=2;
1182     fTrailing = FALSE;
1183     cChars = 10;
1184     cGlyphs = 10;
1185     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1186     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1187     ok(piX == 133,
1188        "iCP=%d should return piX=133 not %d\n", iCP, piX);
1189
1190     iCP=3;
1191     fTrailing = FALSE;
1192     cChars = 10;
1193     cGlyphs = 10;
1194     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1195     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1196     ok(piX == 200,
1197        "iCP=%d should return piX=200 not %d\n", iCP, piX);
1198
1199     iCP=4;
1200     fTrailing = FALSE;
1201     cChars = 10;
1202     cGlyphs = 10;
1203     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1204     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1205     ok(piX == 295,
1206        "iCP=%d should return piX=295 not %d\n", iCP, piX);
1207
1208     iCP=5;
1209     fTrailing = FALSE;
1210     cChars = 10;
1211     cGlyphs = 10;
1212     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1213     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1214     ok(piX == 390,
1215        "iCP=%d should return piX=390 not %d\n", iCP, piX);
1216
1217     iCP=6;
1218     fTrailing = FALSE;
1219     cChars = 10;
1220     cGlyphs = 10;
1221     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1222     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1223     ok(piX == 495,
1224        "iCP=%d should return piX=495 not %d\n", iCP, piX);
1225
1226     iCP=7;
1227     fTrailing = FALSE;
1228     cChars = 10;
1229     cGlyphs = 10;
1230     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1231     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1232     ok(piX == 600,
1233        "iCP=%d should return piX=600 not %d\n", iCP, piX);
1234
1235     iCP=8;
1236     fTrailing = FALSE;
1237     cChars = 10;
1238     cGlyphs = 10;
1239     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1240     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1241     ok(piX == 1051,
1242        "iCP=%d should return piX=1051 not %d\n", iCP, piX);
1243
1244     iCP=9;
1245     fTrailing = FALSE;
1246     cChars = 10;
1247     cGlyphs = 10;
1248     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1249     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1250     ok(piX == 1502,
1251        "iCP=%d should return piX=1502 not %d\n", iCP, piX);
1252
1253     iCP=10;
1254     fTrailing = FALSE;
1255     cChars = 10;
1256     cGlyphs = 10;
1257     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1258     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1259     ok(piX == 1953,
1260        "iCP=%d should return piX=1953 not %d\n", iCP, piX);
1261
1262     iCP=11;
1263     fTrailing = FALSE;
1264     cChars = 10;
1265     cGlyphs = 10;
1266     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1267     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1268     ok(piX == 1953,
1269        "iCP=%d should return piX=1953 %d\n", iCP, piX);
1270
1271     iCP=11;
1272     fTrailing = TRUE;
1273     cChars = 10;
1274     cGlyphs = 10;
1275     hr = ScriptCPtoX(iCP, fTrailing, cChars, cGlyphs, pwLogClustCPtoX, psva, piAdvance, &items[0].a, &piX);
1276     ok(hr == S_OK, "ScriptCPtoX should return S_OK not %08x\n", hr);
1277     ok(piX == 1953,
1278        "iCP=%d should return piX=1953 not %d\n", iCP, piX);
1279 }
1280
1281 static void test_ScriptString(HDC hdc)
1282 {
1283 /*******************************************************************************************
1284  *
1285  * This set of tests are for the string functions of uniscribe.  The ScriptStringAnalyse
1286  * function allocates memory pointed to by the SCRIPT_STRING_ANALYSIS ssa pointer.  This
1287  * memory if freed by ScriptStringFree.  There needs to be a valid hdc for this as
1288  * ScriptStringAnalyse calls ScriptSItemize, ScriptShape and ScriptPlace which require it.
1289  *
1290  */
1291
1292     HRESULT         hr;
1293     WCHAR           teststr[] = {'T','e','s','t','1',' ','a','2','b','3', '\0'};
1294     int             len = (sizeof(teststr) / sizeof(WCHAR)) - 1;
1295     int             Glyphs = len * 2 + 16;
1296     int             Charset;
1297     DWORD           Flags = SSA_GLYPHS;
1298     int             ReqWidth = 100;
1299     const int       Dx[5] = {10, 10, 10, 10, 10};
1300     const BYTE      InClass = 0;
1301     SCRIPT_STRING_ANALYSIS ssa = NULL;
1302
1303     int             X = 10; 
1304     int             Y = 100;
1305     UINT            Options = 0; 
1306     const RECT      rc = {0, 50, 100, 100}; 
1307     int             MinSel = 0;
1308     int             MaxSel = 0;
1309     BOOL            Disabled = FALSE;
1310     const int      *clip_len;
1311     int            i;
1312     UINT           *order;
1313
1314
1315     Charset = -1;     /* this flag indicates unicode input */
1316     /* Test without hdc to get E_PENDING */
1317     hr = ScriptStringAnalyse( NULL, teststr, len, Glyphs, Charset, Flags,
1318                               ReqWidth, NULL, NULL, Dx, NULL,
1319                               &InClass, &ssa);
1320     ok(hr == E_PENDING, "ScriptStringAnalyse Stub should return E_PENDING not %08x\n", hr);
1321
1322     /* test with hdc, this should be a valid test  */
1323     hr = ScriptStringAnalyse( hdc, teststr, len, Glyphs, Charset, Flags,
1324                               ReqWidth, NULL, NULL, Dx, NULL,
1325                               &InClass, &ssa);
1326     ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
1327     ScriptStringFree(&ssa);
1328
1329     /* test makes sure that a call with a valid pssa still works */
1330     hr = ScriptStringAnalyse( hdc, teststr, len, Glyphs, Charset, Flags,
1331                               ReqWidth, NULL, NULL, Dx, NULL,
1332                               &InClass, &ssa);
1333     ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
1334     ok(ssa != NULL, "ScriptStringAnalyse pssa should not be NULL\n");
1335
1336     if (hr == S_OK)
1337     {
1338         hr = ScriptStringOut(ssa, X, Y, Options, &rc, MinSel, MaxSel, Disabled);
1339         ok(hr == S_OK, "ScriptStringOut should return S_OK not %08x\n", hr);
1340     }
1341
1342      clip_len = ScriptString_pcOutChars(ssa);
1343      ok(*clip_len == len, "ScriptString_pcOutChars failed, got %d, expected %d\n", *clip_len, len);
1344
1345      order = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *clip_len * sizeof(UINT));
1346      hr = ScriptStringGetOrder(ssa, order);
1347      ok(hr == S_OK, "ScriptStringGetOrder failed, got %08x, expected S_OK\n", hr);
1348
1349      for (i = 0; i < *clip_len; i++) ok(order[i] == i, "%d: got %d expected %d\n", i, order[i], i);
1350      HeapFree(GetProcessHeap(), 0, order);
1351
1352      hr = ScriptStringFree(&ssa);
1353      ok(hr == S_OK, "ScriptStringFree should return S_OK not %08x\n", hr);
1354 }
1355
1356 static void test_ScriptStringXtoCP_CPtoX(HDC hdc)
1357 {
1358 /*****************************************************************************************
1359  *
1360  * This test is for the ScriptStringXtoCP and ScriptStringXtoCP functions.  Due to the
1361  * nature of the fonts between Windows and Wine, the test is implemented by generating
1362  * values using one one function then checking the output of the second.  In this way
1363  * the validity of the functions is established using Windows as a base and confirming
1364  * similar behaviour in wine.
1365  */
1366
1367     HRESULT         hr;
1368     static const WCHAR teststr1[]  = {0x05e9, 'i', 0x05dc, 'n', 0x05d5, 'e', 0x05dd, '.',0};
1369     static const BOOL rtl[] = {1, 0, 1, 0, 1, 0, 1, 0};
1370     void            *String = (WCHAR *) &teststr1;      /* ScriptStringAnalysis needs void */
1371     int             String_len = (sizeof(teststr1)/sizeof(WCHAR))-1;
1372     int             Glyphs = String_len * 2 + 16;       /* size of buffer as recommended  */
1373     int             Charset = -1;                       /* unicode                        */
1374     DWORD           Flags = SSA_GLYPHS;
1375     int             ReqWidth = 100;
1376     const BYTE      InClass = 0;
1377     SCRIPT_STRING_ANALYSIS ssa = NULL;
1378
1379     int             Ch;                                  /* Character position in string */
1380     int             iTrailing;
1381     int             Cp;                                  /* Character position in string */
1382     int             X;
1383     int             trail,lead;
1384     BOOL            fTrailing;
1385
1386     /* Test with hdc, this should be a valid test
1387      * Here we generate an SCRIPT_STRING_ANALYSIS that will be used as input to the
1388      * following character positions to X and X to character position functions.
1389      */
1390
1391     hr = ScriptStringAnalyse( hdc, String, String_len, Glyphs, Charset, Flags,
1392                               ReqWidth, NULL, NULL, NULL, NULL,
1393                               &InClass, &ssa);
1394     ok(hr == S_OK ||
1395        hr == E_INVALIDARG, /* NT */
1396        "ScriptStringAnalyse should return S_OK or E_INVALIDARG not %08x\n", hr);
1397
1398     if  (hr == S_OK)
1399     {
1400         ok(ssa != NULL, "ScriptStringAnalyse ssa should not be NULL\n");
1401
1402         /*
1403          * Loop to generate character positions to provide starting positions for the
1404          * ScriptStringCPtoX and ScriptStringXtoCP functions
1405          */
1406         for (Cp = 0; Cp < String_len; Cp++)
1407         {
1408             /* The fTrailing flag is used to indicate whether the X being returned is at
1409              * the beginning or the end of the character. What happens here is that if
1410              * fTrailing indicates the end of the character, ie. FALSE, then ScriptStringXtoCP
1411              * returns the beginning of the next character and iTrailing is FALSE.  So for this
1412              * loop iTrailing will be FALSE in both cases.
1413              */
1414             hr = ScriptStringCPtoX(ssa, Cp, TRUE, &trail);
1415             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1416             hr = ScriptStringCPtoX(ssa, Cp, FALSE, &lead);
1417             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1418             if (rtl[Cp])
1419                 ok(lead > trail, "Leading values should be after trialing for rtl chracters(%i)\n",Cp);
1420             else
1421                 ok(lead < trail, "Trailing values should be after leading for ltr chracters(%i)\n",Cp);
1422
1423             /* move by 1 pixel so that we are not inbetween 2 characters.  That could result in being the lead of a rtl and
1424                at the same time the trail of an ltr */
1425
1426             /* inside the leading edge */
1427             X = lead;
1428             if (rtl[Cp]) X--; else X++;
1429             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1430             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1431             ok(Cp == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp, Ch, trail);
1432             ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
1433                                   iTrailing, X);
1434
1435             /* inside the trailing edge */
1436             X = trail;
1437             if (rtl[Cp]) X++; else X--;
1438             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1439             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1440             ok(Cp == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp, Ch, trail);
1441             ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
1442                                   iTrailing, X);
1443
1444             /* outside the "trailing" edge */
1445             if (Cp < String_len-1)
1446             {
1447                 if (rtl[Cp]) X = lead; else X = trail;
1448                 X++;
1449                 hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1450                 ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1451                 ok(Cp + 1 == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp + 1, Ch, trail);
1452                 if (rtl[Cp+1])
1453                     ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
1454                                           iTrailing, X);
1455                 else
1456                     ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
1457                                           iTrailing, X);
1458             }
1459
1460             /* outside the "leading" edge */
1461             if (Cp != 0)
1462             {
1463                 if (rtl[Cp]) X = trail; else X = lead;
1464                 X--;
1465                 hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1466                 ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1467                 ok(Cp - 1 == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp - 1, Ch, trail);
1468                 if (Cp != 0  && rtl[Cp-1])
1469                     ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
1470                                           iTrailing, X);
1471                 else
1472                     ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
1473                                           iTrailing, X);
1474             }
1475         }
1476
1477         /* Check beyond the leading boundary of the whole string */
1478         if (rtl[0])
1479         {
1480             /* having a leading rtl character seems to confuse usp */
1481             /* this looks to be a windows bug we should emulate */
1482             hr = ScriptStringCPtoX(ssa, 0, TRUE, &X);
1483             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1484             X--;
1485             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1486             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1487             ok(Ch == 1, "ScriptStringXtoCP should return Ch = 1 not %d for X outside leading edge when rtl\n", Ch);
1488             ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = outside leading edge when rtl\n",
1489                                        iTrailing);
1490         }
1491         else
1492         {
1493             hr = ScriptStringCPtoX(ssa, 0, FALSE, &X);
1494             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1495             X--;
1496             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1497             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1498             ok(Ch == -1, "ScriptStringXtoCP should return Ch = -1 not %d for X outside leading edge\n", Ch);
1499             ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = outside leading edge\n",
1500                                        iTrailing);
1501         }
1502
1503         /* Check beyond the end boundary of the whole string */
1504         if (rtl[String_len-1])
1505         {
1506             hr = ScriptStringCPtoX(ssa, String_len-1, FALSE, &X);
1507             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1508         }
1509         else
1510         {
1511             hr = ScriptStringCPtoX(ssa, String_len-1, TRUE, &X);
1512             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
1513         }
1514         X++;
1515         hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
1516         ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
1517         ok(Ch == String_len, "ScriptStringXtoCP should return Ch = %i not %d for X outside trailing edge\n", String_len, Ch);
1518         ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = outside traling edge\n",
1519                                    iTrailing);
1520
1521         /*
1522          * Cleanup the SSA for the next round of tests
1523          */
1524         hr = ScriptStringFree(&ssa);
1525         ok(hr == S_OK, "ScriptStringFree should return S_OK not %08x\n", hr);
1526
1527         /*
1528          * Test to see that exceeding the number of chars returns E_INVALIDARG.  First
1529          * generate an SSA for the subsequent tests.
1530          */
1531         hr = ScriptStringAnalyse( hdc, String, String_len, Glyphs, Charset, Flags,
1532                                   ReqWidth, NULL, NULL, NULL, NULL,
1533                                   &InClass, &ssa);
1534         ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
1535
1536         /*
1537          * When ScriptStringCPtoX is called with a character position Cp that exceeds the
1538          * string length, return E_INVALIDARG.  This also invalidates the ssa so a
1539          * ScriptStringFree should also fail.
1540          */
1541         fTrailing = FALSE;
1542         Cp = String_len + 1;
1543         hr = ScriptStringCPtoX(ssa, Cp, fTrailing, &X);
1544         ok(hr == E_INVALIDARG, "ScriptStringCPtoX should return E_INVALIDARG not %08x\n", hr);
1545
1546         ScriptStringFree(&ssa);
1547     }
1548 }
1549
1550 static void test_ScriptCacheGetHeight(HDC hdc)
1551 {
1552     HRESULT hr;
1553     SCRIPT_CACHE sc = NULL;
1554     LONG height;
1555
1556     hr = ScriptCacheGetHeight(NULL, NULL, NULL);
1557     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1558
1559     hr = ScriptCacheGetHeight(NULL, &sc, NULL);
1560     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1561
1562     hr = ScriptCacheGetHeight(NULL, &sc, &height);
1563     ok(hr == E_PENDING, "expected E_PENDING, got 0x%08x\n", hr);
1564
1565     height = 0;
1566
1567     hr = ScriptCacheGetHeight(hdc, &sc, &height);
1568     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
1569     ok(height > 0, "expected height > 0\n");
1570
1571     ScriptFreeCache(&sc);
1572 }
1573
1574 static void test_ScriptGetGlyphABCWidth(HDC hdc)
1575 {
1576     HRESULT hr;
1577     SCRIPT_CACHE sc = NULL;
1578     ABC abc;
1579
1580     hr = ScriptGetGlyphABCWidth(NULL, NULL, 'a', NULL);
1581     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1582
1583     hr = ScriptGetGlyphABCWidth(NULL, &sc, 'a', NULL);
1584     ok(broken(hr == E_PENDING) ||
1585        hr == E_INVALIDARG, /* WIN7 */
1586        "expected E_INVALIDARG, got 0x%08x\n", hr);
1587
1588     hr = ScriptGetGlyphABCWidth(NULL, &sc, 'a', &abc);
1589     ok(hr == E_PENDING, "expected E_PENDING, got 0x%08x\n", hr);
1590
1591     if (0) {    /* crashes on WinXP */
1592     hr = ScriptGetGlyphABCWidth(hdc, &sc, 'a', NULL);
1593     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1594     }
1595
1596     hr = ScriptGetGlyphABCWidth(hdc, &sc, 'a', &abc);
1597     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
1598
1599     ScriptFreeCache(&sc);
1600 }
1601
1602 static void test_ScriptLayout(void)
1603 {
1604     HRESULT hr;
1605     static const BYTE levels[][10] =
1606     {
1607         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1608         { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
1609         { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
1610         { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
1611
1612         { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1},
1613         { 1, 1, 1, 2, 2, 2, 1, 1, 1, 1 },
1614         { 2, 2, 2, 1, 1, 1, 2, 2, 2, 2 },
1615         { 0, 0, 1, 1, 2, 2, 1, 1, 0, 0 },
1616         { 1, 1, 2, 2, 3, 3, 2, 2, 1, 1 },
1617
1618         { 0, 0, 1, 1, 2, 2, 1, 1, 0, 1 },
1619         { 1, 0, 1, 2, 2, 1, 2, 1, 0, 1 },
1620     };
1621     static const int expect_l2v[][10] =
1622     {
1623         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
1624         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
1625         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
1626         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
1627
1628         { 0, 1, 2, 3, 4, 9, 8 ,7 ,6, 5},
1629 /**/    { 9, 8, 7, 4, 5, 6, 3 ,2 ,1, 0},
1630 /**/    { 7, 8, 9, 6, 5, 4, 0 ,1 ,2, 3},
1631         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
1632         { 9, 8, 2, 3, 5, 4, 6 ,7 ,1, 0},
1633
1634         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
1635 /**/    { 0, 1, 7, 5, 6, 4, 3 ,2 ,8, 9},
1636     };
1637     static const int expect_v2l[][10] =
1638     {
1639         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
1640         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
1641         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
1642         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
1643
1644         { 0, 1, 2, 3, 4, 9, 8 ,7 ,6, 5},
1645         { 9, 8, 7, 6, 3, 4, 5 ,2 ,1, 0},
1646         { 6, 7, 8, 9, 5, 4, 3 ,0 ,1, 2},
1647         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
1648         { 9, 8, 2, 3, 5, 4, 6 ,7 ,1, 0},
1649
1650         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
1651         { 0, 1, 7, 6, 5, 3, 4 ,2 ,8, 9},
1652     };
1653
1654     int i, j, vistolog[sizeof(levels[0])], logtovis[sizeof(levels[0])];
1655
1656     hr = ScriptLayout(sizeof(levels[0]), NULL, vistolog, logtovis);
1657     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1658
1659     hr = ScriptLayout(sizeof(levels[0]), levels[0], NULL, NULL);
1660     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
1661
1662     for (i = 0; i < sizeof(levels)/sizeof(levels[0]); i++)
1663     {
1664         hr = ScriptLayout(sizeof(levels[0]), levels[i], vistolog, logtovis);
1665         ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
1666
1667         for (j = 0; j < sizeof(levels[i]); j++)
1668         {
1669             ok(expect_v2l[i][j] == vistolog[j],
1670                "failure: levels[%d][%d] = %d, vistolog[%d] = %d\n",
1671                i, j, levels[i][j], j, vistolog[j] );
1672         }
1673
1674         for (j = 0; j < sizeof(levels[i]); j++)
1675         {
1676             ok(expect_l2v[i][j] == logtovis[j],
1677                "failure: levels[%d][%d] = %d, logtovis[%d] = %d\n",
1678                i, j, levels[i][j], j, logtovis[j] );
1679         }
1680     }
1681 }
1682
1683 static BOOL CALLBACK enum_proc(LGRPID group, LCID lcid, LPSTR locale, LONG_PTR lparam)
1684 {
1685     HRESULT hr;
1686     SCRIPT_DIGITSUBSTITUTE sds;
1687     SCRIPT_CONTROL sc;
1688     SCRIPT_STATE ss;
1689     LCID lcid_old;
1690
1691     if (!IsValidLocale(lcid, LCID_INSTALLED)) return TRUE;
1692
1693     memset(&sds, 0, sizeof(sds));
1694     memset(&sc, 0, sizeof(sc));
1695     memset(&ss, 0, sizeof(ss));
1696
1697     lcid_old = GetThreadLocale();
1698     if (!SetThreadLocale(lcid)) return TRUE;
1699
1700     hr = ScriptRecordDigitSubstitution(lcid, &sds);
1701     ok(hr == S_OK, "ScriptRecordDigitSubstitution failed: 0x%08x\n", hr);
1702
1703     hr = ScriptApplyDigitSubstitution(&sds, &sc, &ss);
1704     ok(hr == S_OK, "ScriptApplyDigitSubstitution failed: 0x%08x\n", hr);
1705
1706     SetThreadLocale(lcid_old);
1707     return TRUE;
1708 }
1709
1710 static void test_digit_substitution(void)
1711 {
1712     BOOL ret;
1713     unsigned int i;
1714     static const LGRPID groups[] =
1715     {
1716         LGRPID_WESTERN_EUROPE,
1717         LGRPID_CENTRAL_EUROPE,
1718         LGRPID_BALTIC,
1719         LGRPID_GREEK,
1720         LGRPID_CYRILLIC,
1721         LGRPID_TURKISH,
1722         LGRPID_JAPANESE,
1723         LGRPID_KOREAN,
1724         LGRPID_TRADITIONAL_CHINESE,
1725         LGRPID_SIMPLIFIED_CHINESE,
1726         LGRPID_THAI,
1727         LGRPID_HEBREW,
1728         LGRPID_ARABIC,
1729         LGRPID_VIETNAMESE,
1730         LGRPID_INDIC,
1731         LGRPID_GEORGIAN,
1732         LGRPID_ARMENIAN
1733     };
1734     HMODULE hKernel32;
1735     static BOOL (WINAPI * pEnumLanguageGroupLocalesA)(LANGGROUPLOCALE_ENUMPROCA,LGRPID,DWORD,LONG_PTR);
1736
1737     hKernel32 = GetModuleHandleA("kernel32.dll");
1738     pEnumLanguageGroupLocalesA = (void*)GetProcAddress(hKernel32, "EnumLanguageGroupLocalesA");
1739
1740     if (!pEnumLanguageGroupLocalesA)
1741     {
1742         win_skip("EnumLanguageGroupLocalesA not available on this platform\n");
1743         return;
1744     }
1745
1746     for (i = 0; i < sizeof(groups)/sizeof(groups[0]); i++)
1747     {
1748         ret = pEnumLanguageGroupLocalesA(enum_proc, groups[i], 0, 0);
1749         if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1750         {
1751             win_skip("EnumLanguageGroupLocalesA not implemented on this platform\n");
1752             break;
1753         }
1754         
1755         ok(ret, "EnumLanguageGroupLocalesA failed unexpectedly: %u\n", GetLastError());
1756     }
1757 }
1758
1759 static void test_ScriptGetProperties(void)
1760 {
1761     const SCRIPT_PROPERTIES **props;
1762     HRESULT hr;
1763     int num;
1764
1765     hr = ScriptGetProperties(NULL, NULL);
1766     ok(hr == E_INVALIDARG, "ScriptGetProperties succeeded\n");
1767
1768     hr = ScriptGetProperties(NULL, &num);
1769     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
1770
1771     hr = ScriptGetProperties(&props, NULL);
1772     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
1773
1774     hr = ScriptGetProperties(&props, &num);
1775     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
1776 }
1777
1778 static void test_ScriptBreak(void)
1779 {
1780     static const WCHAR test[] = {' ','\r','\n',0};
1781     SCRIPT_ITEM items[4];
1782     SCRIPT_LOGATTR la;
1783     HRESULT hr;
1784
1785     hr = ScriptItemize(test, 3, 4, NULL, NULL, items, NULL);
1786     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
1787
1788     memset(&la, 0, sizeof(la));
1789     hr = ScriptBreak(test, 1, &items[0].a, &la);
1790     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
1791
1792     ok(!la.fSoftBreak, "fSoftBreak set\n");
1793     ok(la.fWhiteSpace, "fWhiteSpace not set\n");
1794     ok(la.fCharStop, "fCharStop not set\n");
1795     ok(!la.fWordStop, "fWordStop set\n");
1796     ok(!la.fInvalid, "fInvalid set\n");
1797     ok(!la.fReserved, "fReserved set\n");
1798
1799     memset(&la, 0, sizeof(la));
1800     hr = ScriptBreak(test + 1, 1, &items[1].a, &la);
1801     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
1802
1803     ok(!la.fSoftBreak, "fSoftBreak set\n");
1804     ok(!la.fWhiteSpace, "fWhiteSpace set\n");
1805     ok(la.fCharStop, "fCharStop not set\n");
1806     ok(!la.fWordStop, "fWordStop set\n");
1807     ok(!la.fInvalid, "fInvalid set\n");
1808     ok(!la.fReserved, "fReserved set\n");
1809
1810     memset(&la, 0, sizeof(la));
1811     hr = ScriptBreak(test + 2, 1, &items[2].a, &la);
1812     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
1813
1814     ok(!la.fSoftBreak, "fSoftBreak set\n");
1815     ok(!la.fWhiteSpace, "fWhiteSpace set\n");
1816     ok(la.fCharStop, "fCharStop not set\n");
1817     ok(!la.fWordStop, "fWordStop set\n");
1818     ok(!la.fInvalid, "fInvalid set\n");
1819     ok(!la.fReserved, "fReserved set\n");
1820 }
1821
1822 static void test_newlines(void)
1823 {
1824     static const WCHAR test1[] = {'t','e','x','t','\r','t','e','x','t',0};
1825     static const WCHAR test2[] = {'t','e','x','t','\n','t','e','x','t',0};
1826     static const WCHAR test3[] = {'t','e','x','t','\r','\n','t','e','x','t',0};
1827     static const WCHAR test4[] = {'t','e','x','t','\n','\r','t','e','x','t',0};
1828     static const WCHAR test5[] = {'1','2','3','4','\n','\r','1','2','3','4',0};
1829     SCRIPT_ITEM items[5];
1830     HRESULT hr;
1831     int count;
1832
1833     count = 0;
1834     hr = ScriptItemize(test1, lstrlenW(test1), 5, NULL, NULL, items, &count);
1835     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
1836     ok(count == 3, "got %d expected 3\n", count);
1837
1838     count = 0;
1839     hr = ScriptItemize(test2, lstrlenW(test2), 5, NULL, NULL, items, &count);
1840     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
1841     ok(count == 3, "got %d expected 3\n", count);
1842
1843     count = 0;
1844     hr = ScriptItemize(test3, lstrlenW(test3), 5, NULL, NULL, items, &count);
1845     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
1846     ok(count == 4, "got %d expected 4\n", count);
1847
1848     count = 0;
1849     hr = ScriptItemize(test4, lstrlenW(test4), 5, NULL, NULL, items, &count);
1850     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
1851     ok(count == 4, "got %d expected 4\n", count);
1852
1853     count = 0;
1854     hr = ScriptItemize(test5, lstrlenW(test5), 5, NULL, NULL, items, &count);
1855     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
1856     ok(count == 4, "got %d expected 4\n", count);
1857 }
1858
1859 START_TEST(usp10)
1860 {
1861     HWND            hwnd;
1862     HDC             hdc;
1863     LOGFONTA        lf;
1864     HFONT           hfont;
1865
1866     unsigned short  pwOutGlyphs[256];
1867
1868     /* We need a valid HDC to drive a lot of Script functions which requires the following    *
1869      * to set up for the tests.                                                               */
1870     hwnd = CreateWindowExA(0, "static", "", WS_POPUP, 0,0,100,100,
1871                            0, 0, 0, NULL);
1872     assert(hwnd != 0);
1873     ShowWindow(hwnd, SW_SHOW);
1874     UpdateWindow(hwnd);
1875
1876     hdc = GetDC(hwnd);                                      /* We now have a hdc             */
1877     ok( hdc != NULL, "HDC failed to be created %p\n", hdc);
1878
1879     memset(&lf, 0, sizeof(LOGFONTA));
1880     lstrcpyA(lf.lfFaceName, "Tahoma");
1881     lf.lfHeight = 10;
1882     lf.lfWeight = 3;
1883     lf.lfWidth = 10;
1884
1885     hfont = SelectObject(hdc, CreateFontIndirectA(&lf));
1886     ok(hfont != NULL, "SelectObject failed: %p\n", hfont);
1887
1888     test_ScriptItemize();
1889     test_ScriptItemIzeShapePlace(hdc,pwOutGlyphs);
1890     test_ScriptGetCMap(hdc, pwOutGlyphs);
1891     test_ScriptCacheGetHeight(hdc);
1892     test_ScriptGetGlyphABCWidth(hdc);
1893     test_ScriptShape(hdc);
1894     test_ScriptPlace(hdc);
1895
1896     test_ScriptGetFontProperties(hdc);
1897     test_ScriptTextOut(hdc);
1898     test_ScriptTextOut2(hdc);
1899     test_ScriptTextOut3(hdc);
1900     test_ScriptXtoX();
1901     test_ScriptString(hdc);
1902     test_ScriptStringXtoCP_CPtoX(hdc);
1903
1904     test_ScriptLayout();
1905     test_digit_substitution();
1906     test_ScriptGetProperties();
1907     test_ScriptBreak();
1908     test_newlines();
1909
1910     ReleaseDC(hwnd, hdc);
1911     DestroyWindow(hwnd);
1912 }