wintrust: Add FindCertsByIssuer stub.
[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[5];
36     int iCharPos;
37     int fRTL;
38     int fLayoutRTL;
39     int uBidiLevel;
40     ULONG scriptTag;
41 } itemTest;
42
43 typedef struct _shapeTest_char {
44     WORD wLogClust;
45     SCRIPT_CHARPROP CharProp;
46 } shapeTest_char;
47
48 typedef struct _shapeTest_glyph {
49     int Glyph;
50     SCRIPT_GLYPHPROP GlyphProp;
51 } shapeTest_glyph;
52
53 /* Uniscribe 1.6 calls */
54 static HRESULT (WINAPI *pScriptItemizeOpenType)( const WCHAR *pwcInChars, int cInChars, int cMaxItems, const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState, SCRIPT_ITEM *pItems, ULONG *pScriptTags, int *pcItems);
55
56 static HRESULT (WINAPI *pScriptShapeOpenType)( HDC hdc, SCRIPT_CACHE *psc, SCRIPT_ANALYSIS *psa, OPENTYPE_TAG tagScript, OPENTYPE_TAG tagLangSys, int *rcRangeChars, TEXTRANGE_PROPERTIES **rpRangeProperties, int cRanges, const WCHAR *pwcChars, int cChars, int cMaxGlyphs, WORD *pwLogClust, SCRIPT_CHARPROP *pCharProps, WORD *pwOutGlyphs, SCRIPT_GLYPHPROP *pOutGlyphProps, int *pcGlyphs);
57
58 static DWORD (WINAPI *pGetGlyphIndicesW)(HDC hdc, LPCWSTR lpstr, INT count, LPWORD pgi, DWORD flags);
59
60 static inline void _test_items_ok(LPCWSTR string, DWORD cchString,
61                          SCRIPT_CONTROL *Control, SCRIPT_STATE *State,
62                          DWORD nItems, const itemTest* items, BOOL nItemsToDo,
63                          INT nItemsBroken)
64 {
65     HRESULT hr;
66     int x, outnItems;
67     SCRIPT_ITEM outpItems[15];
68     ULONG tags[15] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
69
70     if (pScriptItemizeOpenType)
71         hr = pScriptItemizeOpenType(string, cchString, 15, Control, State, outpItems, tags, &outnItems);
72     else
73         hr = ScriptItemize(string, cchString, 15, Control, State, outpItems, &outnItems);
74
75     winetest_ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
76     if (nItemsBroken && broken(nItemsBroken == outnItems))
77     {
78         winetest_win_skip("This test broken on this platform\n");
79         return;
80     }
81     if (nItemsToDo)
82         todo_wine winetest_ok(outnItems == nItems, "Wrong number of items\n");
83     else
84         winetest_ok(outnItems == nItems, "Wrong number of items\n");
85     for (x = 0; x <= outnItems; x++)
86     {
87         if (items[x].todo_flag[0])
88             todo_wine winetest_ok(outpItems[x].iCharPos == items[x].iCharPos, "%i:Wrong CharPos\n",x);
89         else
90             winetest_ok(outpItems[x].iCharPos == items[x].iCharPos, "%i:Wrong CharPos (%i)\n",x,outpItems[x].iCharPos);
91
92         if (items[x].todo_flag[1])
93             todo_wine winetest_ok(outpItems[x].a.fRTL == items[x].fRTL, "%i:Wrong fRTL\n",x);
94         else
95             winetest_ok(outpItems[x].a.fRTL == items[x].fRTL, "%i:Wrong fRTL(%i)\n",x,outpItems[x].a.fRTL);
96         if (items[x].todo_flag[2])
97             todo_wine winetest_ok(outpItems[x].a.fLayoutRTL == items[x].fLayoutRTL, "%i:Wrong fLayoutRTL\n",x);
98         else
99             winetest_ok(outpItems[x].a.fLayoutRTL == items[x].fLayoutRTL, "%i:Wrong fLayoutRTL(%i)\n",x,outpItems[x].a.fLayoutRTL);
100         if (items[x].todo_flag[3])
101             todo_wine winetest_ok(outpItems[x].a.s.uBidiLevel == items[x].uBidiLevel, "%i:Wrong BidiLevel\n",x);
102         else
103             winetest_ok(outpItems[x].a.s.uBidiLevel == items[x].uBidiLevel, "%i:Wrong BidiLevel(%i)\n",x,outpItems[x].a.s.uBidiLevel);
104         if (x != outnItems)
105             winetest_ok(outpItems[x].a.eScript != SCRIPT_UNDEFINED, "%i: Undefined script\n",x);
106         if (pScriptItemizeOpenType)
107         {
108             if (items[x].todo_flag[4])
109                 todo_wine winetest_ok(tags[x] == items[x].scriptTag,"%i:Incorrect Script Tag %x != %x\n",x,tags[x],items[x].scriptTag);
110             else
111                 winetest_ok(tags[x] == items[x].scriptTag,"%i:Incorrect Script Tag %x != %x\n",x,tags[x],items[x].scriptTag);
112         }
113     }
114 }
115
116 #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)
117
118 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
119           ( ( (ULONG)_x4 << 24 ) |     \
120             ( (ULONG)_x3 << 16 ) |     \
121             ( (ULONG)_x2 <<  8 ) |     \
122               (ULONG)_x1         )
123
124 #define latn_tag MS_MAKE_TAG('l','a','t','n')
125 #define arab_tag MS_MAKE_TAG('a','r','a','b')
126 #define thai_tag MS_MAKE_TAG('t','h','a','i')
127 #define hebr_tag MS_MAKE_TAG('h','e','b','r')
128 #define syrc_tag MS_MAKE_TAG('s','y','r','c')
129 #define deva_tag MS_MAKE_TAG('d','e','v','a')
130 #define beng_tag MS_MAKE_TAG('b','e','n','g')
131 #define guru_tag MS_MAKE_TAG('g','u','r','u')
132 #define gujr_tag MS_MAKE_TAG('g','u','j','r')
133 #define orya_tag MS_MAKE_TAG('o','r','y','a')
134 #define taml_tag MS_MAKE_TAG('t','a','m','l')
135 #define telu_tag MS_MAKE_TAG('t','e','l','u')
136 #define knda_tag MS_MAKE_TAG('k','n','d','a')
137 #define mlym_tag MS_MAKE_TAG('m','l','y','m')
138
139 static void test_ScriptItemize( void )
140 {
141     static const WCHAR test1[] = {'t', 'e', 's', 't',0};
142     static const itemTest t11[2] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},4,0,0,0,-1}};
143     static const itemTest t12[2] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},4,0,0,0,-1}};
144
145     static const WCHAR test1b[] = {' ', ' ', ' ', ' ',0};
146     static const itemTest t1b1[2] = {{{0,0,0,0,0},0,0,0,0,0},{{0,0,0,0,0},4,0,0,0,-1}};
147     static const itemTest t1b2[2] = {{{0,0,0,0,0},0,1,1,1,0},{{0,0,0,0,0},4,0,0,0,-1}};
148
149     static const WCHAR test1c[] = {' ', ' ', ' ', '1', '2', ' ',0};
150     static const itemTest t1c1[2] = {{{0,0,0,0,0},0,0,0,0,0},{{0,0,0,0,0},6,0,0,0,-1}};
151     static const itemTest t1c2[4] = {{{0,0,0,0,0},0,1,1,1,0},{{0,0,1,0,0},3,0,1,2,0},{{0,0,0,0,0},5,1,1,1,0},{{0,0,0,0,0},6,0,0,0,-1}};
152
153     /* Arabic, English*/
154     static const WCHAR test2[] = {'1','2','3','-','5','2',0x064a,0x064f,0x0633,0x0627,0x0648,0x0650,0x064a,'7','1','.',0};
155     static const itemTest t21[7] = {{{0,0,0,0,0},0,0,0,0,0},{{0,0,0,0,0},3,0,0,0,0},{{0,0,0,0,0},4,0,0,0,0},{{0,0,0,0,0},6,1,1,1,arab_tag},{{0,0,0,0,0},13,0,0,0,0},{{0,0,0,0,0},15,0,0,0,0},{{0,0,0,0,0},16,0,0,0,-1}};
156     static const itemTest t22[5] = {{{0,0,0,1,0},0,0,0,2,0},{{0,0,0,0,0},6,1,1,1,arab_tag},{{0,0,1,0,0},13,0,1,2,0},{{0,0,0,0,0},15,0,0,0,0},{{0,0,0,0,0},16,0,0,0,-1}};
157     static const itemTest t23[5] = {{{0,0,1,0,0},0,0,1,2,0},{{0,0,0,0,0},6,1,1,1,arab_tag},{{0,0,1,0,0},13,0,1,2,0},{{0,0,0,0,0},15,1,1,1,0},{{0,0,0,0,0},16,0,0,0,-1}};
158
159     static const WCHAR test2b[] = {'A','B','C','-','D','E','F',' ',0x0621,0x0623,0x0624,0};
160     static const itemTest t2b1[5] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},3,0,0,0,0},{{0,0,0,0,0},4,0,0,0,latn_tag},{{0,0,0,0,0},8,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
161     static const itemTest t2b2[5] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},3,0,0,2,0},{{0,0,0,0,0},4,0,0,2,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
162     static const itemTest t2b3[3] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
163
164     /* leading space */
165     static const WCHAR test2c[] = {' ',0x0621,0x0623,0x0624,'A','B','C','-','D','E','F',0};
166     static const itemTest t2c1[5] = {{{0,0,0,0,0},0,1,1,1,arab_tag},{{0,0,0,0,0},4,0,0,0,latn_tag},{{0,0,0,0,0},7,0,0,0,0},{{0,0,0,0,0},8,0,0,0,latn_tag},{{0,0,0,0,0},11,0,0,0,-1}};
167     static const itemTest t2c2[6] = {{{0,0,0,0,1},0,0,0,0,0},{{0,0,0,0,0},1,1,1,1,arab_tag},{{0,0,0,0,0},4,0,0,0,latn_tag},{{0,0,0,0,0},7,0,0,0,0},{{0,0,0,0,0},8,0,0,0,latn_tag},{{0,0,0,0,0},11,0,0,0,-1}};
168     static const itemTest t2c3[5] = {{{0,0,0,0,0},0,1,1,1,arab_tag},{{0,0,0,0,0},4,0,0,2,latn_tag},{{0,0,0,0,0},7,0,0,2,0},{{0,0,0,0,0},8,0,0,2,latn_tag},{{0,0,0,0,0},11,0,0,0,-1}};
169     static const itemTest t2c4[3] = {{{0,0,0,0,0},0,1,1,1,arab_tag},{{0,0,0,0,0},4,0,0,2,latn_tag},{{0,0,0,0,0},11,0,0,0,-1}};
170
171     /* trailing space */
172     static const WCHAR test2d[] = {'A','B','C','-','D','E','F',0x0621,0x0623,0x0624,' ',0};
173     static const itemTest t2d1[5] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},3,0,0,0,0},{{0,0,0,0,0},4,0,0,0,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
174     static const itemTest t2d2[6] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},3,0,0,0,0},{{0,0,0,0,0},4,0,0,0,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},10,0,0,0,0},{{0,0,0,0,0},11,0,0,0,-1}};
175     static const itemTest t2d3[5] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},3,0,0,2,0},{{0,0,0,0,0},4,0,0,2,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
176     static const itemTest t2d4[3] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},7,1,1,1,arab_tag},{{0,0,0,0,0},11,0,0,0,-1}};
177
178     /* Thai */
179     static const WCHAR test3[] =
180 {0x0e04,0x0e27,0x0e32,0x0e21,0x0e1e,0x0e22,0x0e32,0x0e22,0x0e32, 0x0e21
181 ,0x0e2d,0x0e22,0x0e39,0x0e48,0x0e17,0x0e35,0x0e48,0x0e44,0x0e2b,0x0e19
182 ,0x0e04,0x0e27,0x0e32,0x0e21,0x0e2a, 0x0e33,0x0e40,0x0e23,0x0e47,0x0e08,
183  0x0e2d,0x0e22,0x0e39,0x0e48,0x0e17,0x0e35,0x0e48,0x0e19,0x0e31,0x0e48,0x0e19,0};
184
185     static const itemTest t31[2] = {{{0,0,0,0,0},0,0,0,0,thai_tag},{{0,0,0,0,0},41,0,0,0,-1}};
186     static const itemTest t32[2] = {{{0,0,0,0,0},0,0,0,2,thai_tag},{{0,0,0,0,0},41,0,0,0,-1}};
187
188     static const WCHAR test4[]  = {'1','2','3','-','5','2',' ','i','s',' ','7','1','.',0};
189
190     static const itemTest t41[6] = {{{0,0,0,0,0},0,0,0,0,0},{{0,0,0,0,0},3,0,0,0,0},{{0,0,0,0,0},4,0,0,0,0},{{0,0,0,0,0},7,0,0,0,latn_tag},{{0,0,0,0,0},10,0,0,0,0},{{0,0,0,0,0},12,0,0,0,-1}};
191     static const itemTest t42[5] = {{{0,0,1,0,0},0,0,1,2,0},{{0,0,0,0,1},6,1,1,1,0},{{0,0,0,0,0},7,0,0,2,latn_tag},{{0,0,0,0,0},10,0,0,2,0},{{0,0,0,0,0},12,0,0,0,-1}};
192     static const itemTest t43[4] = {{{0,0,1,0,0},0,0,1,2,0},{{0,0,0,0,1},6,1,1,1,0},{{0,0,0,0,0},7,0,0,2,latn_tag},{{0,0,0,0,0},12,0,0,0,-1}};
193
194     /* Arabic */
195     static const WCHAR test5[]  =
196 {0x0627,0x0644,0x0635,0x0651,0x0650,0x062d,0x0629,0x064f,' ',0x062a,0x064e,
197 0x0627,0x062c,0x064c,' ',0x0639,0x064e,0x0644,0x0649,' ',
198 0x0631,0x064f,0x0624,0x0648,0x0633,0x0650,' ',0x0627,0x0644
199 ,0x0623,0x0635,0x0650,0x062d,0x0651,0x064e,0x0627,0x0621,0x0650,0};
200     static const itemTest t51[2] = {{{0,0,0,0,0},0,1,1,1,arab_tag},{{0,0,0,0,0},38,0,0,0,-1}};
201
202     /* Hebrew */
203     static const WCHAR test6[]  = {0x05e9, 0x05dc, 0x05d5, 0x05dd, '.',0};
204     static const itemTest t61[3] = {{{0,0,0,0,0},0,1,1,1,hebr_tag},{{0,0,0,0,0},4,0,0,0,0},{{0,0,0,0,0},5,0,0,0,-1}};
205     static const itemTest t62[3] = {{{0,0,0,0,0},0,1,1,1,hebr_tag},{{0,0,0,0,0},4,1,1,1,0},{{0,0,0,0,0},5,0,0,0,-1}};
206     static const itemTest t63[2] = {{{0,0,0,0,0},0,1,1,1,hebr_tag},{{0,0,0,0,0},5,0,0,0,-1}};
207     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};
208     static const itemTest t71[4] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},9,1,1,1,hebr_tag},{{0,0,0,0,0},19,0,0,0,latn_tag},{{0,0,0,0,0},29,0,0,0,-1}};
209     static const itemTest t72[4] = {{{0,0,0,0,0},0,0,0,0,latn_tag},{{0,0,0,0,0},9,1,1,1,hebr_tag},{{0,0,0,0,0},18,0,0,0,latn_tag},{{0,0,0,0,0},29,0,0,0,-1}};
210     static const itemTest t73[4] = {{{0,0,0,0,0},0,0,0,2,latn_tag},{{0,0,0,0,0},8,1,1,1,hebr_tag},{{0,0,0,0,0},19,0,0,2,latn_tag},{{0,0,0,0,0},29,0,0,0,-1}};
211     static const WCHAR test8[] = {0x0633, 0x0644, 0x0627, 0x0645,0};
212     static const itemTest t81[2] = {{{0,0,0,0,0},0,1,1,1,arab_tag},{{0,0,0,0,0},4,0,0,0,-1}};
213
214     /* Syriac  (Like Arabic )*/
215     static const WCHAR test9[] = {0x0710, 0x0712, 0x0712, 0x0714, '.',0};
216     static const itemTest t91[3] = {{{0,0,0,0,0},0,1,1,1,syrc_tag},{{0,0,0,0,0},4,0,0,0,0},{{0,0,0,0,0},5,0,0,0,-1}};
217     static const itemTest t92[3] = {{{0,0,0,0,0},0,1,1,1,syrc_tag},{{0,0,0,0,0},4,1,1,1,0},{{0,0,0,0,0},5,0,0,0,-1}};
218     static const itemTest t93[2] = {{{0,0,0,0,0},0,1,1,1,syrc_tag},{{0,0,0,0,0},5,0,0,0,-1}};
219
220     static const WCHAR test10[] = {0x0717, 0x0718, 0x071a, 0x071b,0};
221     static const itemTest t101[2] = {{{0,0,0,0,0},0,1,1,1,syrc_tag},{{0,0,0,0,0},4,0,0,0,-1}};
222
223     /* Devanagari */
224     static const WCHAR test11[] = {0x0926, 0x0947, 0x0935, 0x0928, 0x093e, 0x0917, 0x0930, 0x0940};
225     static const itemTest t111[2] = {{{0,0,0,0,0},0,0,0,0,deva_tag},{{0,0,0,0,0},8,0,0,0,-1}};
226     static const itemTest t112[2] = {{{0,0,0,0,0},0,0,0,2,deva_tag},{{0,0,0,0,0},8,0,0,0,-1}};
227
228     /* Bengali */
229     static const WCHAR test12[] = {0x09ac, 0x09be, 0x0982, 0x09b2, 0x09be};
230     static const itemTest t121[2] = {{{0,0,0,0,0},0,0,0,0,beng_tag},{{0,0,0,0,0},5,0,0,0,-1}};
231     static const itemTest t122[2] = {{{0,0,0,0,0},0,0,0,2,beng_tag},{{0,0,0,0,0},5,0,0,0,-1}};
232
233     /* Gurmukhi */
234     static const WCHAR test13[] = {0x0a17, 0x0a41, 0x0a30, 0x0a2e, 0x0a41, 0x0a16, 0x0a40};
235     static const itemTest t131[2] = {{{0,0,0,0,0},0,0,0,0,guru_tag},{{0,0,0,0,0},7,0,0,0,-1}};
236     static const itemTest t132[2] = {{{0,0,0,0,0},0,0,0,2,guru_tag},{{0,0,0,0,0},7,0,0,0,-1}};
237
238     /* Gujarati */
239     static const WCHAR test14[] = {0x0a97, 0x0ac1, 0x0a9c, 0x0ab0, 0x0abe, 0x0aa4, 0x0ac0};
240     static const itemTest t141[2] = {{{0,0,0,0,0},0,0,0,0,gujr_tag},{{0,0,0,0,0},7,0,0,0,-1}};
241     static const itemTest t142[2] = {{{0,0,0,0,0},0,0,0,2,gujr_tag},{{0,0,0,0,0},7,0,0,0,-1}};
242
243     /* Oriya */
244     static const WCHAR test15[] = {0x0b13, 0x0b21, 0x0b3c, 0x0b3f, 0x0b06};
245     static const itemTest t151[2] = {{{0,0,0,0,0},0,0,0,0,orya_tag},{{0,0,0,0,0},5,0,0,0,-1}};
246     static const itemTest t152[2] = {{{0,0,0,0,0},0,0,0,2,orya_tag},{{0,0,0,0,0},5,0,0,0,-1}};
247
248     /* Tamil */
249     static const WCHAR test16[] = {0x0ba4, 0x0bae, 0x0bbf, 0x0bb4, 0x0bcd};
250     static const itemTest t161[2] = {{{0,0,0,0,0},0,0,0,0,taml_tag},{{0,0,0,0,0},5,0,0,0,-1}};
251     static const itemTest t162[2] = {{{0,0,0,0,0},0,0,0,2,taml_tag},{{0,0,0,0,0},5,0,0,0,-1}};
252
253     /* Telugu */
254     static const WCHAR test17[] = {0x0c24, 0x0c46, 0x0c32, 0x0c41, 0x0c17, 0x0c41};
255     static const itemTest t171[2] = {{{0,0,0,0,0},0,0,0,0,telu_tag},{{0,0,0,0,0},6,0,0,0,-1}};
256     static const itemTest t172[2] = {{{0,0,0,0,0},0,0,0,2,telu_tag},{{0,0,0,0,0},6,0,0,0,-1}};
257
258     /* Kannada */
259     static const WCHAR test18[] = {0x0c95, 0x0ca8, 0x0ccd, 0x0ca8, 0x0ca1};
260     static const itemTest t181[2] = {{{0,0,0,0,0},0,0,0,0,knda_tag},{{0,0,0,0,0},5,0,0,0,-1}};
261     static const itemTest t182[2] = {{{0,0,0,0,0},0,0,0,2,knda_tag},{{0,0,0,0,0},5,0,0,0,-1}};
262
263     /* Malayalam */
264     static const WCHAR test19[] = {0x0d2e, 0x0d32, 0x0d2f, 0x0d3e, 0x0d33, 0x0d02};
265     static const itemTest t191[2] = {{{0,0,0,0,0},0,0,0,0,mlym_tag},{{0,0,0,0,0},6,0,0,0,-1}};
266     static const itemTest t192[2] = {{{0,0,0,0,0},0,0,0,2,mlym_tag},{{0,0,0,0,0},6,0,0,0,-1}};
267
268     SCRIPT_ITEM items[15];
269     SCRIPT_CONTROL  Control;
270     SCRIPT_STATE    State;
271     HRESULT hr;
272     HMODULE usp10;
273     int nItems;
274
275     usp10 = LoadLibraryA("usp10.dll");
276     ok (usp10 != 0,"Unable to LoadLibrary on usp10.dll\n");
277     pScriptItemizeOpenType = (void*)GetProcAddress(usp10, "ScriptItemizeOpenType");
278     pScriptShapeOpenType = (void*)GetProcAddress(usp10, "ScriptShapeOpenType");
279     pGetGlyphIndicesW = (void*)GetProcAddress(GetModuleHandleA("gdi32.dll"), "GetGlyphIndicesW");
280
281     memset(&Control, 0, sizeof(Control));
282     memset(&State, 0, sizeof(State));
283
284     hr = ScriptItemize(NULL, 4, 10, &Control, &State, items, NULL);
285     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pwcInChars is NULL\n");
286
287     hr = ScriptItemize(test1, 4, 10, &Control, &State, NULL, NULL);
288     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pItems is NULL\n");
289
290     hr = ScriptItemize(test1, 4, 1, &Control, &State, items, NULL);
291     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cMaxItems < 2.\n");
292
293     hr = ScriptItemize(test1, 0, 10, NULL, NULL, items, &nItems);
294     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cInChars is 0\n");
295
296     test_items_ok(test1,4,NULL,NULL,1,t11,FALSE,0);
297     test_items_ok(test1b,4,NULL,NULL,1,t1b1,FALSE,0);
298     test_items_ok(test1c,6,NULL,NULL,1,t1c1,FALSE,0);
299     test_items_ok(test2,16,NULL,NULL,6,t21,FALSE,0);
300     test_items_ok(test2b,11,NULL,NULL,4,t2b1,FALSE,0);
301     test_items_ok(test2c,11,NULL,NULL,4,t2c1,FALSE,0);
302     test_items_ok(test2d,11,NULL,NULL,4,t2d1,FALSE,0);
303     test_items_ok(test3,41,NULL,NULL,1,t31,FALSE,0);
304     test_items_ok(test4,12,NULL,NULL,5,t41,FALSE,0);
305     test_items_ok(test5,38,NULL,NULL,1,t51,FALSE,0);
306     test_items_ok(test6,5,NULL,NULL,2,t61,FALSE,0);
307     test_items_ok(test7,29,NULL,NULL,3,t71,FALSE,0);
308     test_items_ok(test8,4,NULL,NULL,1,t81,FALSE,0);
309     test_items_ok(test9,5,NULL,NULL,2,t91,FALSE,0);
310     test_items_ok(test10,4,NULL,NULL,1,t101,FALSE,0);
311     test_items_ok(test11,8,NULL,NULL,1,t111,FALSE,0);
312     test_items_ok(test12,5,NULL,NULL,1,t121,FALSE,0);
313     test_items_ok(test13,7,NULL,NULL,1,t131,FALSE,0);
314     test_items_ok(test14,7,NULL,NULL,1,t141,FALSE,0);
315     test_items_ok(test15,5,NULL,NULL,1,t151,FALSE,0);
316     test_items_ok(test16,5,NULL,NULL,1,t161,FALSE,0);
317     test_items_ok(test17,6,NULL,NULL,1,t171,FALSE,0);
318     test_items_ok(test18,5,NULL,NULL,1,t181,FALSE,0);
319     test_items_ok(test19,6,NULL,NULL,1,t191,FALSE,0);
320
321     State.uBidiLevel = 0;
322     test_items_ok(test1,4,&Control,&State,1,t11,FALSE,0);
323     test_items_ok(test1b,4,&Control,&State,1,t1b1,FALSE,0);
324     test_items_ok(test1c,6,&Control,&State,1,t1c1,FALSE,0);
325     test_items_ok(test2,16,&Control,&State,4,t22,FALSE,0);
326     test_items_ok(test2b,11,&Control,&State,4,t2b1,FALSE,0);
327     test_items_ok(test2c,11,&Control,&State,5,t2c2,FALSE,0);
328     test_items_ok(test2d,11,&Control,&State,5,t2d2,FALSE,0);
329     test_items_ok(test3,41,&Control,&State,1,t31,FALSE,0);
330     test_items_ok(test4,12,&Control,&State,5,t41,FALSE,0);
331     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
332     test_items_ok(test6,5,&Control,&State,2,t61,FALSE,0);
333     test_items_ok(test7,29,&Control,&State,3,t72,FALSE,0);
334     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
335     test_items_ok(test9,5,&Control,&State,2,t91,FALSE,0);
336     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
337     test_items_ok(test11,8,&Control,&State,1,t111,FALSE,0);
338     test_items_ok(test12,5,&Control,&State,1,t121,FALSE,0);
339     test_items_ok(test13,7,&Control,&State,1,t131,FALSE,0);
340     test_items_ok(test14,7,&Control,&State,1,t141,FALSE,0);
341     test_items_ok(test15,5,&Control,&State,1,t151,FALSE,0);
342     test_items_ok(test16,5,&Control,&State,1,t161,FALSE,0);
343     test_items_ok(test17,6,&Control,&State,1,t171,FALSE,0);
344     test_items_ok(test18,5,&Control,&State,1,t181,FALSE,0);
345     test_items_ok(test19,6,&Control,&State,1,t191,FALSE,0);
346
347     State.uBidiLevel = 1;
348     test_items_ok(test1,4,&Control,&State,1,t12,FALSE,0);
349     test_items_ok(test1b,4,&Control,&State,1,t1b2,FALSE,0);
350     test_items_ok(test1c,6,&Control,&State,3,t1c2,FALSE,0);
351     test_items_ok(test2,16,&Control,&State,4,t23,FALSE,0);
352     test_items_ok(test2b,11,&Control,&State,4,t2b2,FALSE,0);
353     test_items_ok(test2c,11,&Control,&State,4,t2c3,FALSE,0);
354     test_items_ok(test2d,11,&Control,&State,4,t2d3,FALSE,0);
355     test_items_ok(test3,41,&Control,&State,1,t32,FALSE,0);
356     test_items_ok(test4,12,&Control,&State,4,t42,FALSE,0);
357     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
358     test_items_ok(test6,5,&Control,&State,2,t62,FALSE,0);
359     test_items_ok(test7,29,&Control,&State,3,t73,FALSE,0);
360     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
361     test_items_ok(test9,5,&Control,&State,2,t92,FALSE,0);
362     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
363     test_items_ok(test11,8,&Control,&State,1,t112,FALSE,0);
364     test_items_ok(test12,5,&Control,&State,1,t122,FALSE,0);
365     test_items_ok(test13,7,&Control,&State,1,t132,FALSE,0);
366     test_items_ok(test14,7,&Control,&State,1,t142,FALSE,0);
367     test_items_ok(test15,5,&Control,&State,1,t152,FALSE,0);
368     test_items_ok(test16,5,&Control,&State,1,t162,FALSE,0);
369     test_items_ok(test17,6,&Control,&State,1,t172,FALSE,0);
370     test_items_ok(test18,5,&Control,&State,1,t182,FALSE,0);
371     test_items_ok(test19,6,&Control,&State,1,t192,FALSE,0);
372
373     State.uBidiLevel = 1;
374     Control.fMergeNeutralItems = TRUE;
375     test_items_ok(test1,4,&Control,&State,1,t12,FALSE,0);
376     test_items_ok(test1b,4,&Control,&State,1,t1b2,FALSE,0);
377     test_items_ok(test1c,6,&Control,&State,3,t1c2,FALSE,0);
378     test_items_ok(test2,16,&Control,&State,4,t23,FALSE,0);
379     test_items_ok(test2b,11,&Control,&State,2,t2b3,FALSE,4);
380     test_items_ok(test2c,11,&Control,&State,2,t2c4,FALSE,4);
381     test_items_ok(test2d,11,&Control,&State,2,t2d4,FALSE,4);
382     test_items_ok(test3,41,&Control,&State,1,t32,FALSE,0);
383     test_items_ok(test4,12,&Control,&State,3,t43,FALSE,4);
384     test_items_ok(test5,38,&Control,&State,1,t51,FALSE,0);
385     test_items_ok(test6,5,&Control,&State,1,t63,FALSE,2);
386     test_items_ok(test7,29,&Control,&State,3,t73,FALSE,0);
387     test_items_ok(test8,4,&Control,&State,1,t81,FALSE,0);
388     test_items_ok(test9,5,&Control,&State,1,t93,FALSE,2);
389     test_items_ok(test10,4,&Control,&State,1,t101,FALSE,0);
390     test_items_ok(test11,8,&Control,&State,1,t112,FALSE,0);
391     test_items_ok(test12,5,&Control,&State,1,t122,FALSE,0);
392     test_items_ok(test13,7,&Control,&State,1,t132,FALSE,0);
393     test_items_ok(test14,7,&Control,&State,1,t142,FALSE,0);
394     test_items_ok(test15,5,&Control,&State,1,t152,FALSE,0);
395     test_items_ok(test16,5,&Control,&State,1,t162,FALSE,0);
396     test_items_ok(test17,6,&Control,&State,1,t172,FALSE,0);
397     test_items_ok(test18,5,&Control,&State,1,t182,FALSE,0);
398     test_items_ok(test19,6,&Control,&State,1,t192,FALSE,0);
399 }
400
401 static inline void _test_shape_ok(int valid, HDC hdc, LPCWSTR string,
402                          DWORD cchString, SCRIPT_CONTROL *Control,
403                          SCRIPT_STATE *State, DWORD item, DWORD nGlyphs,
404                          const shapeTest_char* charItems,
405                          const shapeTest_glyph* glyphItems)
406 {
407     HRESULT hr;
408     int x, outnItems=0, outnGlyphs=0;
409     SCRIPT_ITEM outpItems[15];
410     SCRIPT_CACHE sc = NULL;
411     WORD *glyphs;
412     WORD *logclust;
413     int maxGlyphs = cchString * 1.5;
414     SCRIPT_GLYPHPROP *glyphProp;
415     SCRIPT_CHARPROP  *charProp;
416     ULONG tags[15];
417
418     hr = pScriptItemizeOpenType(string, cchString, 15, Control, State, outpItems, tags, &outnItems);
419     if (hr == USP_E_SCRIPT_NOT_IN_FONT)
420     {
421         if (valid > 0)
422             winetest_win_skip("Select font does not support script\n");
423         else
424             winetest_trace("Select font does not support script\n");
425         return;
426     }
427     if (valid > 0)
428         winetest_ok(!hr, "ScriptItemizeOpenType should return S_OK not %08x\n", hr);
429     else if (hr)
430         winetest_trace("ScriptItemizeOpenType should return S_OK not %08x\n", hr);
431
432
433     if (outnItems <= item)
434     {
435         if (valid > 0)
436             winetest_win_skip("Did not get enough items\n");
437         else
438             winetest_trace("Did not get enough items\n");
439         return;
440     }
441
442     logclust = HeapAlloc(GetProcessHeap(), 0, sizeof(WORD) * cchString);
443     memset(logclust,'a',sizeof(WORD) * cchString);
444     charProp = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_CHARPROP) * cchString);
445     memset(charProp,'a',sizeof(SCRIPT_CHARPROP) * cchString);
446     glyphs = HeapAlloc(GetProcessHeap(), 0, sizeof(WORD) * maxGlyphs);
447     memset(glyphs,'a',sizeof(WORD) * cchString);
448     glyphProp = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_GLYPHPROP) * maxGlyphs);
449     memset(glyphProp,'a',sizeof(SCRIPT_GLYPHPROP) * cchString);
450
451     hr = pScriptShapeOpenType(hdc, &sc, &outpItems[item].a, tags[item], 0x00000000, NULL, NULL, 0, string, cchString, maxGlyphs, logclust, charProp, glyphs, glyphProp, &outnGlyphs);
452     if (valid > 0)
453         winetest_ok(hr == S_OK, "ScriptShapeOpenType failed (%x)\n",hr);
454     else if (hr != S_OK)
455         winetest_trace("ScriptShapeOpenType failed (%x)\n",hr);
456     if (FAILED(hr))
457         return;
458
459     for (x = 0; x < cchString; x++)
460     {
461         if (valid > 0)
462             winetest_ok(logclust[x] == charItems[x].wLogClust, "%i: invalid LogClust(%i)\n",x,logclust[x]);
463         else if (logclust[x] != charItems[x].wLogClust)
464             winetest_trace("%i: invalid LogClust(%i)\n",x,logclust[x]);
465         if (valid > 0)
466             winetest_ok(charProp[x].fCanGlyphAlone == charItems[x].CharProp.fCanGlyphAlone, "%i: invalid fCanGlyphAlone\n",x);
467         else if (charProp[x].fCanGlyphAlone != charItems[x].CharProp.fCanGlyphAlone)
468             winetest_trace("%i: invalid fCanGlyphAlone\n",x);
469     }
470
471     if (valid > 0)
472         winetest_ok(nGlyphs == outnGlyphs, "got incorrect number of glyphs (%i)\n",outnGlyphs);
473     else if (nGlyphs != outnGlyphs)
474         winetest_trace("got incorrect number of glyphs (%i)\n",outnGlyphs);
475     for (x = 0; x < outnGlyphs; x++)
476     {
477         if (glyphItems[x].Glyph)
478         {
479             if (valid > 0)
480                 winetest_ok(glyphs[x]!=0, "%i: Glyph not present when it should be\n",x);
481             else if (glyphs[x]==0)
482                 winetest_trace("%i: Glyph not present when it should be\n",x);
483         }
484         else
485         {
486             if (valid > 0)
487                 winetest_ok(glyphs[x]==0, "%i: Glyph present when it should not be\n",x);
488             else if (glyphs[x]!=0)
489                 winetest_trace("%i: Glyph present when it should not be\n",x);
490         }
491         if (valid > 0)
492             winetest_ok(glyphProp[x].sva.uJustification == glyphItems[x].GlyphProp.sva.uJustification, "%i: uJustification incorrect (%i)\n",x,glyphProp[x].sva.uJustification);
493         else if (glyphProp[x].sva.uJustification != glyphItems[x].GlyphProp.sva.uJustification)
494             winetest_trace("%i: uJustification incorrect (%i)\n",x,glyphProp[x].sva.uJustification);
495         if (valid > 0)
496             winetest_ok(glyphProp[x].sva.fClusterStart == glyphItems[x].GlyphProp.sva.fClusterStart, "%i: fClusterStart incorrect (%i)\n",x,glyphProp[x].sva.fClusterStart);
497         else if (glyphProp[x].sva.fClusterStart != glyphItems[x].GlyphProp.sva.fClusterStart)
498             winetest_trace("%i: fClusterStart incorrect (%i)\n",x,glyphProp[x].sva.fClusterStart);
499         if (valid > 0)
500             winetest_ok(glyphProp[x].sva.fDiacritic == glyphItems[x].GlyphProp.sva.fDiacritic, "%i: fDiacritic incorrect (%i)\n",x,glyphProp[x].sva.fDiacritic);
501         else if (glyphProp[x].sva.fDiacritic != glyphItems[x].GlyphProp.sva.fDiacritic)
502             winetest_trace("%i: fDiacritic incorrect (%i)\n",x,glyphProp[x].sva.fDiacritic);
503         if (valid > 0)
504             winetest_ok(glyphProp[x].sva.fZeroWidth == glyphItems[x].GlyphProp.sva.fZeroWidth, "%i: fZeroWidth incorrect (%i)\n",x,glyphProp[x].sva.fZeroWidth);
505         else if (glyphProp[x].sva.fZeroWidth != glyphItems[x].GlyphProp.sva.fZeroWidth)
506             winetest_trace("%i: fZeroWidth incorrect (%i)\n",x,glyphProp[x].sva.fZeroWidth);
507     }
508
509     HeapFree(GetProcessHeap(),0,logclust);
510     HeapFree(GetProcessHeap(),0,charProp);
511     HeapFree(GetProcessHeap(),0,glyphs);
512     HeapFree(GetProcessHeap(),0,glyphProp);
513     ScriptFreeCache(&sc);
514 }
515
516 #define test_shape_ok(a,b,c,d,e,f,g,h,i) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _test_shape_ok(1,a,b,c,d,e,f,g,h,i)
517
518 #define test_shape_ok_valid(v,a,b,c,d,e,f,g,h,i) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _test_shape_ok(v,a,b,c,d,e,f,g,h,i)
519
520 typedef struct tagRangeP {
521     BYTE range;
522     LOGFONTA lf;
523 } fontEnumParam;
524
525 static int CALLBACK enumFontProc( const LOGFONT *lpelfe, const TEXTMETRIC *lpntme,
526                            DWORD FontType, LPARAM lParam)
527 {
528     NEWTEXTMETRICEX *ntme = (NEWTEXTMETRICEX*)lpntme;
529     fontEnumParam *rp = (fontEnumParam*) lParam;
530     int idx = 0;
531     DWORD i;
532     DWORD mask = 0;
533
534     if (FontType != TRUETYPE_FONTTYPE)
535         return 1;
536
537     i = rp->range;
538     while (i > sizeof(DWORD)*8)
539     {
540         idx++;
541         i -= (sizeof(DWORD)*8);
542     }
543     if (idx > 3)
544         return 0;
545
546     mask = 1 << i;
547
548     if (ntme->ntmFontSig.fsUsb[idx] & mask)
549     {
550         memcpy(&(rp->lf),lpelfe,sizeof(LOGFONT));
551         return 0;
552     }
553     return 1;
554 }
555
556 static int _find_font_for_range(HDC hdc, const CHAR *recommended, BYTE range, const WCHAR check, HFONT *hfont, HFONT *origFont)
557 {
558     int rc = 0;
559     fontEnumParam lParam;
560
561     lParam.range = range;
562     memset(&lParam.lf,0,sizeof(LOGFONT));
563     *hfont = NULL;
564
565     if (recommended)
566     {
567         lstrcpyA(lParam.lf.lfFaceName, recommended);
568         if (!EnumFontFamiliesExA(hdc, &lParam.lf, enumFontProc, (LPARAM)&lParam, 0))
569         {
570             *hfont = CreateFontIndirectA(&lParam.lf);
571             if (*hfont)
572             {
573                 winetest_trace("using font %s\n",lParam.lf.lfFaceName);
574                 rc = 1;
575             }
576         }
577     }
578
579     if (!*hfont)
580     {
581         memset(&lParam.lf,0,sizeof(LOGFONT));
582         lParam.lf.lfCharSet = DEFAULT_CHARSET;
583
584         if (!EnumFontFamiliesExA(hdc, &lParam.lf, enumFontProc, (LPARAM)&lParam, 0) && lParam.lf.lfFaceName[0])
585         {
586             *hfont = CreateFontIndirectA(&lParam.lf);
587             if (*hfont)
588                 winetest_trace("trying font %s: failures will only be warnings\n",lParam.lf.lfFaceName);
589         }
590     }
591
592     if (*hfont)
593     {
594         WORD glyph = 0;
595
596         *origFont = SelectObject(hdc,*hfont);
597         if (pGetGlyphIndicesW && (pGetGlyphIndicesW(hdc, &check, 1, &glyph, 0) == GDI_ERROR || glyph ==0))
598         {
599             winetest_trace("    Font fails to contain required glyphs\n");
600             SelectObject(hdc,*origFont);
601             DeleteObject(*hfont);
602             *hfont=NULL;
603             rc = 0;
604         }
605         else if (!rc)
606             rc = -1;
607     }
608     else
609         winetest_trace("Failed to find usable font\n");
610
611     return rc;
612 }
613
614 #define find_font_for_range(a,b,c,d,e,f) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _find_font_for_range(a,b,c,d,e,f)
615
616 static void test_ScriptShapeOpenType(HDC hdc)
617 {
618     HRESULT hr;
619     SCRIPT_CACHE sc = NULL;
620     WORD glyphs[4], logclust[4];
621     SCRIPT_GLYPHPROP glyphProp[4];
622     SCRIPT_ITEM items[2];
623     ULONG tags[2];
624     SCRIPT_CONTROL  Control;
625     SCRIPT_STATE    State;
626     int nb, outnItems;
627     HFONT hfont, hfont_orig;
628     int test_valid;
629
630     static const WCHAR test1[] = {'w', 'i', 'n', 'e',0};
631     static const shapeTest_char t1_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{3,{0,0}}};
632     static const shapeTest_glyph t1_g[] = {
633                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
634                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
635                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
636                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}} };
637
638     static const WCHAR test2[] = {0x202B, 'i', 'n', 0x202C,0};
639     static const shapeTest_char t2_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{3,{0,0}}};
640     static const shapeTest_glyph t2_g[] = {
641                             {0,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
642                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
643                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
644                             {0,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}} };
645
646     /* Hebrew */
647     static const WCHAR test_hebrew[]  = {0x05e9, 0x05dc, 0x05d5, 0x05dd,0};
648     static const shapeTest_char hebrew_c[] = {{3,{0,0}},{2,{0,0}},{1,{0,0}},{0,{0,0}}};
649     static const shapeTest_glyph hebrew_g[] = {
650                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
651                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
652                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
653                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}} };
654
655     /* Arabic */
656     static const WCHAR test_arabic[] = {0x0633,0x0644,0x0627,0x0645,0};
657     static const shapeTest_char arabic_c[] = {{2,{0,0}},{1,{0,0}},{1,{0,0}},{0,{0,0}}};
658     static const shapeTest_glyph arabic_g[] = {
659                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
660                             {1,{{SCRIPT_JUSTIFY_ARABIC_NORMAL,1,0,0,0,0},0}},
661                             {1,{{SCRIPT_JUSTIFY_ARABIC_SEEN,1,0,0,0,0},0}} };
662
663     /* Thai */
664     static const WCHAR test_thai[] = {0x0e2a, 0x0e04, 0x0e23, 0x0e34, 0x0e1b, 0x0e15, 0x0e4c, 0x0e44, 0x0e17, 0x0e22,};
665     static const shapeTest_char thai_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{2,{0,0}},{4,{0,0}},{5,{0,0}},{5,{0,0}},{7,{0,0}},{8,{0,0}},{9,{0,0}}};
666     static const shapeTest_glyph thai_g[] = {
667                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
668                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
669                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
670                             {1,{{SCRIPT_JUSTIFY_CHARACTER,0,1,1,0,0},0}},
671                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
672                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
673                             {1,{{SCRIPT_JUSTIFY_CHARACTER,0,1,1,0,0},0}},
674                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
675                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
676                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}}};
677
678     /* Syriac */
679     static const WCHAR test_syriac[] = {0x0710, 0x0710, 0x0710, 0x0728, 0x0718, 0x0723,0};
680     static const shapeTest_char syriac_c[] = {{5,{0,0}},{4,{0,0}},{3,{0,0}},{2,{0,0}},{1,{0,0}},{0,{0,0}}};
681     static const shapeTest_glyph syriac_g[] = {
682                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
683                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
684                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
685                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
686                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
687                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
688
689     /* Thaana */
690     static const WCHAR test_thaana[] = {0x078a, 0x07ae, 0x0792, 0x07b0, 0x0020, 0x0796, 0x07aa, 0x0789, 0x07b0, 0x0795, 0x07ac, 0x0791, 0x07b0};
691     static const shapeTest_char thaana_c[] = {{12,{0,0}},{12,{0,0}},{10,{0,0}},{10,{0,0}},{8,{1,0}},{7,{0,0}},{7,{0,0}},{5,{0,0}},{5,{0,0}},{3,{0,0}},{3,{0,0}},{1,{0,0}},{1,{0,0}}};
692     static const shapeTest_glyph thaana_g[] = {
693                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
694                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
695                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
696                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
697                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
698                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
699                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
700                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
701                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
702                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
703                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
704                             {1,{{SCRIPT_JUSTIFY_NONE,0,1,1,0,0},0}},
705                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
706
707     /* Phags-pa */
708     static const WCHAR test_phagspa[] = {0xa84f, 0xa861, 0xa843, 0x0020, 0xa863, 0xa861, 0xa859, 0x0020, 0xa850, 0xa85c, 0xa85e};
709     static const shapeTest_char phagspa_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{3,{1,0}},{4,{0,0}},{5,{0,0}},{6,{0,0}},{7,{1,0}},{8,{0,0}},{9,{0,0}},{10,{0,0}}};
710     static const shapeTest_glyph phagspa_g[] = {
711                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
712                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
713                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
714                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
715                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
716                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
717                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
718                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
719                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
720                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
721                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
722
723     /* Lao */
724     static const WCHAR test_lao[] = {0x0ead, 0x0eb1, 0x0e81, 0x0eaa, 0x0ead, 0x0e99, 0x0ea5, 0x0eb2, 0x0ea7, 0};
725     static const shapeTest_char lao_c[] = {{0,{0,0}},{0,{0,0}},{2,{0,0}},{3,{0,0}},{4,{0,0}},{5,{0,0}},{6,{0,0}},{7,{0,0}},{8,{0,0}}};
726     static const shapeTest_glyph lao_g[] = {
727                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
728                             {1,{{SCRIPT_JUSTIFY_CHARACTER,0,1,1,0,0},0}},
729                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
730                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
731                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
732                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
733                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
734                             {1,{{SCRIPT_JUSTIFY_CHARACTER,1,0,0,0,0},0}},
735                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
736
737     /* Tibetan */
738     static const WCHAR test_tibetan[] = {0x0f04, 0x0f05, 0x0f0e, 0x0020, 0x0f51, 0x0f7c, 0x0f53, 0x0f0b, 0x0f5a, 0x0f53, 0x0f0b, 0x0f51, 0x0f44, 0x0f0b, 0x0f54, 0x0f7c, 0x0f0d};
739     static const shapeTest_char tibetan_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{3,{1,0}},{4,{0,0}},{4,{0,0}},{6,{0,0}},{7,{0,0}},{8,{0,0}},{9,{0,0}},{10,{0,0}},{11,{0,0}},{12,{0,0}},{13,{0,0}},{14,{0,0}},{14,{0,0}},{16,{0,0}}};
740     static const shapeTest_glyph tibetan_g[] = {
741                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
742                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
743                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
744                             {1,{{SCRIPT_JUSTIFY_BLANK,1,0,0,0,0},0}},
745                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
746                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
747                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
748                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
749                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
750                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
751                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
752                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
753                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
754                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
755                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
756                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
757                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
758
759     /* Devanagari */
760     static const WCHAR test_devanagari[] = {0x0926, 0x0947, 0x0935, 0x0928, 0x093e, 0x0917, 0x0930, 0x0940};
761     static const shapeTest_char devanagari_c[] = {{0,{0,0}},{0,{0,0}},{2,{0,0}},{3,{0,0}},{3,{0,0}},{5,{0,0}},{6,{0,0}},{6,{0,0}}};
762     static const shapeTest_glyph devanagari_g[] = {
763                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
764                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
765                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
766                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
767                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
768                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
769                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
770                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
771
772     /* Bengali */
773     static const WCHAR test_bengali[] = {0x09ac, 0x09be, 0x0982, 0x09b2, 0x09be};
774     static const shapeTest_char bengali_c[] = {{0,{0,0}},{0,{0,0}},{0,{0,0}},{3,{0,0}},{3,{0,0}}};
775     static const shapeTest_glyph bengali_g[] = {
776                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
777                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
778                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
779                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
780                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
781
782     /* Gurmukhi */
783     static const WCHAR test_gurmukhi[] = {0x0a17, 0x0a41, 0x0a30, 0x0a2e, 0x0a41, 0x0a16, 0x0a40};
784     static const shapeTest_char gurmukhi_c[] = {{0,{0,0}},{0,{0,0}},{2,{0,0}},{3,{0,0}},{3,{0,0}},{5,{0,0}},{5,{0,0}}};
785     static const shapeTest_glyph gurmukhi_g[] = {
786                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
787                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
788                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
789                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
790                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
791                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
792                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
793
794     /* Gujarati */
795     static const WCHAR test_gujarati[] = {0x0a97, 0x0ac1, 0x0a9c, 0x0ab0, 0x0abe, 0x0aa4, 0x0ac0};
796     static const shapeTest_char gujarati_c[] = {{0,{0,0}},{0,{0,0}},{2,{0,0}},{3,{0,0}},{3,{0,0}},{5,{0,0}},{5,{0,0}}};
797     static const shapeTest_glyph gujarati_g[] = {
798                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
799                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
800                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
801                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
802                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
803                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
804                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
805
806     /* Oriya */
807     static const WCHAR test_oriya[] = {0x0b13, 0x0b21, 0x0b3c, 0x0b3f, 0x0b06};
808     static const shapeTest_char oriya_c[] = {{0,{0,0}},{1,{0,0}},{1,{0,0}},{1,{0,0}},{3,{0,0}}};
809     static const shapeTest_glyph oriya_g[] = {
810                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
811                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
812                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
813                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
814
815     /* Tamil */
816     static const WCHAR test_tamil[] = {0x0ba4, 0x0bae, 0x0bbf, 0x0bb4, 0x0bcd};
817     static const shapeTest_char tamil_c[] = {{0,{0,0}},{1,{0,0}},{1,{0,0}},{3,{0,0}},{3,{0,0}}};
818     static const shapeTest_glyph tamil_g[] = {
819                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
820                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
821                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
822                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}} };
823
824     /* Telugu */
825     static const WCHAR test_telugu[] = {0x0c24, 0x0c46, 0x0c32, 0x0c41, 0x0c17, 0x0c41};
826     static const shapeTest_char telugu_c[] = {{0,{0,0}},{0,{0,0}},{2,{0,0}},{2,{0,0}},{4,{0,0}},{4,{0,0}}};
827     static const shapeTest_glyph telugu_g[] = {
828                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
829                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
830                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
831                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
832                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
833                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
834
835     /* Malayalam */
836     static const WCHAR test_malayalam[] = {0x0d2e, 0x0d32, 0x0d2f, 0x0d3e, 0x0d33, 0x0d02};
837     static const shapeTest_char malayalam_c[] = {{0,{0,0}},{1,{0,0}},{2,{0,0}},{2,{0,0}},{4,{0,0}},{4,{0,0}}};
838     static const shapeTest_glyph malayalam_g[] = {
839                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
840                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
841                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
842                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}},
843                             {1,{{SCRIPT_JUSTIFY_NONE,1,0,0,0,0},0}},
844                             {1,{{SCRIPT_JUSTIFY_NONE,0,0,0,0,0},0}} };
845
846     if (!pScriptItemizeOpenType || !pScriptShapeOpenType)
847     {
848         win_skip("ScriptShapeOpenType not available on this platform\n");
849         return;
850     }
851
852     memset(&Control, 0 , sizeof(Control));
853     memset(&State, 0 , sizeof(State));
854
855     hr = pScriptItemizeOpenType(test1, 4, 2, &Control, &State, items, tags, &outnItems);
856     ok(!hr, "ScriptItemizeOpenType should return S_OK not %08x\n", hr);
857     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
858
859     hr = pScriptShapeOpenType(hdc, &sc, &items[0].a, tags[0], 0x00000000, NULL, NULL, 0, test1, 4, 4, NULL, NULL, glyphs, NULL, &nb);
860     ok(hr == E_INVALIDARG, "ScriptShapeOpenType should return E_INVALIDARG not %08x\n", hr);
861
862     hr = pScriptShapeOpenType(hdc, &sc, &items[0].a, tags[0], 0x00000000, NULL, NULL, 0, test1, 4, 4, NULL, NULL, glyphs, glyphProp, NULL);
863     ok(hr == E_INVALIDARG, "ScriptShapeOpenType should return E_INVALIDARG not %08x\n", hr);
864
865     hr = pScriptShapeOpenType(NULL, &sc, &items[0].a, tags[0], 0x00000000, NULL, NULL, 0, test1, 4, 4, NULL, NULL, glyphs, glyphProp, &nb);
866     ok(hr == E_INVALIDARG, "ScriptShapeOpenType should return E_PENDING not %08x\n", hr);
867
868     hr = pScriptShapeOpenType(hdc, &sc, &items[0].a, tags[0], 0x00000000, NULL, NULL, 0, test1, 4, 4, NULL, NULL, glyphs, glyphProp, &nb);
869     ok( hr == E_INVALIDARG,
870        "ScriptShapeOpenType should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
871     hr = pScriptShapeOpenType(hdc, &sc, &items[0].a, tags[0], 0x00000000, NULL, NULL, 0, test1, 4, 4, logclust, NULL, glyphs, glyphProp, &nb);
872     ok(hr == E_INVALIDARG, "ScriptShapeOpenType should return E_INVALIDARG not %08x\n", hr);
873
874     ScriptFreeCache(&sc);
875
876     test_shape_ok(hdc, test1, 4, &Control, &State, 0, 4, t1_c, t1_g);
877     test_shape_ok(hdc, test2, 4, &Control, &State, 1, 4, t2_c, t2_g);
878
879     test_valid = find_font_for_range(hdc, "Microsoft Sans Serif", 11, test_hebrew[0], &hfont, &hfont_orig);
880     if (hfont != NULL)
881     {
882         test_shape_ok_valid(test_valid, hdc, test_hebrew, 4, &Control, &State, 0, 4, hebrew_c, hebrew_g);
883         SelectObject(hdc, hfont_orig);
884         DeleteObject(hfont);
885     }
886
887     test_valid = find_font_for_range(hdc, "Microsoft Sans Serif", 13, test_arabic[0], &hfont, &hfont_orig);
888     if (hfont != NULL)
889     {
890         test_shape_ok_valid(test_valid, hdc, test_arabic, 4, &Control, &State, 0, 3, arabic_c, arabic_g);
891         SelectObject(hdc, hfont_orig);
892         DeleteObject(hfont);
893     }
894
895     test_valid = find_font_for_range(hdc, "Microsoft Sans Serif", 24, test_thai[0], &hfont, &hfont_orig);
896     if (hfont != NULL)
897     {
898         test_shape_ok_valid(test_valid, hdc, test_thai, 10, &Control, &State, 0, 10, thai_c, thai_g);
899         SelectObject(hdc, hfont_orig);
900         DeleteObject(hfont);
901     }
902
903     test_valid = find_font_for_range(hdc, "Estrangelo Edessa", 71, test_syriac[0], &hfont, &hfont_orig);
904     if (hfont != NULL)
905     {
906         test_shape_ok_valid(test_valid, hdc, test_syriac, 6, &Control, &State, 0, 6, syriac_c, syriac_g);
907         SelectObject(hdc, hfont_orig);
908         DeleteObject(hfont);
909     }
910
911     test_valid = find_font_for_range(hdc, "MV Boli", 72, test_thaana[0], &hfont, &hfont_orig);
912     if (hfont != NULL)
913     {
914         test_shape_ok_valid(test_valid, hdc, test_thaana, 13, &Control, &State, 0, 13, thaana_c, thaana_g);
915         SelectObject(hdc, hfont_orig);
916         DeleteObject(hfont);
917     }
918
919     test_valid = find_font_for_range(hdc, "Microsoft PhagsPa", 53, test_phagspa[0], &hfont, &hfont_orig);
920     if (hfont != NULL)
921     {
922         test_shape_ok_valid(test_valid, hdc, test_phagspa, 11, &Control, &State, 0, 11, phagspa_c, phagspa_g);
923         SelectObject(hdc, hfont_orig);
924         DeleteObject(hfont);
925     }
926
927     test_valid = find_font_for_range(hdc, "DokChampa", 25, test_lao[0], &hfont, &hfont_orig);
928     if (hfont != NULL)
929     {
930         test_shape_ok_valid(test_valid, hdc, test_lao, 9, &Control, &State, 0, 9, lao_c, lao_g);
931         SelectObject(hdc, hfont_orig);
932         DeleteObject(hfont);
933     }
934
935     test_valid = find_font_for_range(hdc, "Microsoft Himalaya", 70, test_tibetan[0], &hfont, &hfont_orig);
936     if (hfont != NULL)
937     {
938         test_shape_ok_valid(test_valid, hdc, test_tibetan, 17, &Control, &State, 0, 17, tibetan_c, tibetan_g);
939         SelectObject(hdc, hfont_orig);
940         DeleteObject(hfont);
941     }
942
943     test_valid = find_font_for_range(hdc, "Mangal", 15, test_devanagari[0], &hfont, &hfont_orig);
944     if (hfont != NULL)
945     {
946         test_shape_ok_valid(test_valid, hdc, test_devanagari, 8, &Control, &State, 0, 8, devanagari_c, devanagari_g);
947         SelectObject(hdc, hfont_orig);
948         DeleteObject(hfont);
949     }
950
951     test_valid = find_font_for_range(hdc, "Vrinda", 16, test_bengali[0], &hfont, &hfont_orig);
952     if (hfont != NULL)
953     {
954         test_shape_ok_valid(test_valid, hdc, test_bengali, 5, &Control, &State, 0, 5, bengali_c, bengali_g);
955         SelectObject(hdc, hfont_orig);
956         DeleteObject(hfont);
957     }
958
959     test_valid = find_font_for_range(hdc, "Raavi", 17, test_gurmukhi[0], &hfont, &hfont_orig);
960     if (hfont != NULL)
961     {
962         test_shape_ok_valid(test_valid, hdc, test_gurmukhi, 7, &Control, &State, 0, 7, gurmukhi_c, gurmukhi_g);
963         SelectObject(hdc, hfont_orig);
964         DeleteObject(hfont);
965     }
966
967     test_valid = find_font_for_range(hdc, "Shruti", 18, test_gujarati[0], &hfont, &hfont_orig);
968     if (hfont != NULL)
969     {
970         test_shape_ok_valid(test_valid, hdc, test_gujarati, 7, &Control, &State, 0, 7, gujarati_c, gujarati_g);
971         SelectObject(hdc, hfont_orig);
972         DeleteObject(hfont);
973     }
974
975     test_valid = find_font_for_range(hdc, "Kalinga", 19, test_oriya[0], &hfont, &hfont_orig);
976     if (hfont != NULL)
977     {
978         test_shape_ok_valid(test_valid, hdc, test_oriya, 5, &Control, &State, 0, 4, oriya_c, oriya_g);
979         SelectObject(hdc, hfont_orig);
980         DeleteObject(hfont);
981     }
982
983     test_valid = find_font_for_range(hdc, "Latha", 20, test_tamil[0], &hfont, &hfont_orig);
984     if (hfont != NULL)
985     {
986         test_shape_ok_valid(test_valid, hdc, test_tamil, 5, &Control, &State, 0, 4, tamil_c, tamil_g);
987         SelectObject(hdc, hfont_orig);
988         DeleteObject(hfont);
989     }
990
991     test_valid = find_font_for_range(hdc, "Gautami", 21, test_telugu[0], &hfont, &hfont_orig);
992     if (hfont != NULL)
993     {
994         test_shape_ok_valid(test_valid, hdc, test_telugu, 6, &Control, &State, 0, 6, telugu_c, telugu_g);
995         SelectObject(hdc, hfont_orig);
996         DeleteObject(hfont);
997     }
998
999     test_valid = find_font_for_range(hdc, "Kartika", 23, test_malayalam[0], &hfont, &hfont_orig);
1000     if (hfont != NULL)
1001     {
1002         test_shape_ok_valid(test_valid, hdc, test_malayalam, 6, &Control, &State, 0, 6, malayalam_c, malayalam_g);
1003         SelectObject(hdc, hfont_orig);
1004         DeleteObject(hfont);
1005     }
1006 }
1007
1008 static void test_ScriptShape(HDC hdc)
1009 {
1010     static const WCHAR test1[] = {'w', 'i', 'n', 'e',0};
1011     static const WCHAR test2[] = {0x202B, 'i', 'n', 0x202C,0};
1012     HRESULT hr;
1013     SCRIPT_CACHE sc = NULL;
1014     WORD glyphs[4], glyphs2[4], logclust[4];
1015     SCRIPT_VISATTR attrs[4];
1016     SCRIPT_ITEM items[2];
1017     int nb;
1018
1019     hr = ScriptItemize(test1, 4, 2, NULL, NULL, items, NULL);
1020     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
1021     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1022
1023     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, NULL, &nb);
1024     ok(hr == E_INVALIDARG, "ScriptShape should return E_INVALIDARG not %08x\n", hr);
1025
1026     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, NULL);
1027     ok(hr == E_INVALIDARG, "ScriptShape should return E_INVALIDARG not %08x\n", hr);
1028
1029     hr = ScriptShape(NULL, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, &nb);
1030     ok(hr == E_PENDING, "ScriptShape should return E_PENDING not %08x\n", hr);
1031
1032     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, NULL, attrs, &nb);
1033     ok(broken(hr == S_OK) ||
1034        hr == E_INVALIDARG || /* Vista, W2K8 */
1035        hr == E_FAIL, /* WIN7 */
1036        "ScriptShape should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
1037     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1038
1039     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
1040     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
1041     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1042
1043
1044     memset(glyphs,-1,sizeof(glyphs));
1045     memset(logclust,-1,sizeof(logclust));
1046     memset(attrs,-1,sizeof(attrs));
1047     hr = ScriptShape(NULL, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
1048     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
1049     ok(nb == 4, "Wrong number of items\n");
1050     ok(logclust[0] == 0, "clusters out of order\n");
1051     ok(logclust[1] == 1, "clusters out of order\n");
1052     ok(logclust[2] == 2, "clusters out of order\n");
1053     ok(logclust[3] == 3, "clusters out of order\n");
1054     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1055     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1056     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1057     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1058     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
1059     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
1060     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
1061     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
1062     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
1063     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
1064     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
1065     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
1066     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
1067     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
1068     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
1069     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
1070
1071     ScriptFreeCache(&sc);
1072     sc = NULL;
1073
1074     memset(glyphs2,-1,sizeof(glyphs2));
1075     memset(logclust,-1,sizeof(logclust));
1076     memset(attrs,-1,sizeof(attrs));
1077     hr = ScriptShape(hdc, &sc, test2, 4, 4, &items[0].a, glyphs2, logclust, attrs, &nb);
1078     ok(hr == S_OK, "ScriptShape should return S_OK not %08x\n", hr);
1079     ok(nb == 4, "Wrong number of items\n");
1080     ok(glyphs2[0] == 0 || broken(glyphs2[0] == 0x80), "Incorrect glyph for 0x202B\n");
1081     ok(glyphs2[3] == 0 || broken(glyphs2[3] == 0x80), "Incorrect glyph for 0x202C\n");
1082     ok(logclust[0] == 0, "clusters out of order\n");
1083     ok(logclust[1] == 1, "clusters out of order\n");
1084     ok(logclust[2] == 2, "clusters out of order\n");
1085     ok(logclust[3] == 3, "clusters out of order\n");
1086     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1087     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1088     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1089     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1090     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
1091     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
1092     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
1093     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
1094     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
1095     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
1096     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
1097     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
1098     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
1099     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
1100     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
1101     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
1102
1103     /* modify LTR to RTL */
1104     items[0].a.fRTL = 1;
1105     memset(glyphs2,-1,sizeof(glyphs2));
1106     memset(logclust,-1,sizeof(logclust));
1107     memset(attrs,-1,sizeof(attrs));
1108     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs2, logclust, attrs, &nb);
1109     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
1110     ok(nb == 4, "Wrong number of items\n");
1111     ok(glyphs2[0] == glyphs[3], "Glyphs not reordered properly\n");
1112     ok(glyphs2[1] == glyphs[2], "Glyphs not reordered properly\n");
1113     ok(glyphs2[2] == glyphs[1], "Glyphs not reordered properly\n");
1114     ok(glyphs2[3] == glyphs[0], "Glyphs not reordered properly\n");
1115     ok(logclust[0] == 3, "clusters out of order\n");
1116     ok(logclust[1] == 2, "clusters out of order\n");
1117     ok(logclust[2] == 1, "clusters out of order\n");
1118     ok(logclust[3] == 0, "clusters out of order\n");
1119     ok(attrs[0].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1120     ok(attrs[1].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1121     ok(attrs[2].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1122     ok(attrs[3].uJustification == SCRIPT_JUSTIFY_CHARACTER, "uJustification incorrect\n");
1123     ok(attrs[0].fClusterStart == 1, "fClusterStart incorrect\n");
1124     ok(attrs[1].fClusterStart == 1, "fClusterStart incorrect\n");
1125     ok(attrs[2].fClusterStart == 1, "fClusterStart incorrect\n");
1126     ok(attrs[3].fClusterStart == 1, "fClusterStart incorrect\n");
1127     ok(attrs[0].fDiacritic == 0, "fDiacritic incorrect\n");
1128     ok(attrs[1].fDiacritic == 0, "fDiacritic incorrect\n");
1129     ok(attrs[2].fDiacritic == 0, "fDiacritic incorrect\n");
1130     ok(attrs[3].fDiacritic == 0, "fDiacritic incorrect\n");
1131     ok(attrs[0].fZeroWidth == 0, "fZeroWidth incorrect\n");
1132     ok(attrs[1].fZeroWidth == 0, "fZeroWidth incorrect\n");
1133     ok(attrs[2].fZeroWidth == 0, "fZeroWidth incorrect\n");
1134     ok(attrs[3].fZeroWidth == 0, "fZeroWidth incorrect\n");
1135
1136     ScriptFreeCache(&sc);
1137 }
1138
1139 static void test_ScriptPlace(HDC hdc)
1140 {
1141     static const WCHAR test1[] = {'t', 'e', 's', 't',0};
1142     BOOL ret;
1143     HRESULT hr;
1144     SCRIPT_CACHE sc = NULL;
1145     WORD glyphs[4], logclust[4];
1146     SCRIPT_VISATTR attrs[4];
1147     SCRIPT_ITEM items[2];
1148     int nb, widths[4];
1149     GOFFSET offset[4];
1150     ABC abc[4];
1151
1152     hr = ScriptItemize(test1, 4, 2, NULL, NULL, items, NULL);
1153     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
1154     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1155
1156     hr = ScriptShape(hdc, &sc, test1, 4, 4, &items[0].a, glyphs, logclust, attrs, &nb);
1157     ok(!hr, "ScriptShape should return S_OK not %08x\n", hr);
1158     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1159
1160     hr = ScriptPlace(hdc, &sc, glyphs, 4, NULL, &items[0].a, widths, NULL, NULL);
1161     ok(hr == E_INVALIDARG, "ScriptPlace should return E_INVALIDARG not %08x\n", hr);
1162
1163     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, NULL, NULL);
1164     ok(broken(hr == E_PENDING) ||
1165        hr == E_INVALIDARG || /* Vista, W2K8 */
1166        hr == E_FAIL, /* WIN7 */
1167        "ScriptPlace should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
1168
1169     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, offset, NULL);
1170     ok(hr == E_PENDING, "ScriptPlace should return E_PENDING not %08x\n", hr);
1171
1172     hr = ScriptPlace(NULL, &sc, glyphs, 4, attrs, &items[0].a, widths, NULL, abc);
1173     ok(broken(hr == E_PENDING) ||
1174        hr == E_INVALIDARG || /* Vista, W2K8 */
1175        hr == E_FAIL, /* WIN7 */
1176        "ScriptPlace should return E_FAIL or E_INVALIDARG, not %08x\n", hr);
1177
1178     hr = ScriptPlace(hdc, &sc, glyphs, 4, attrs, &items[0].a, widths, offset, NULL);
1179     ok(!hr, "ScriptPlace should return S_OK not %08x\n", hr);
1180     ok(items[0].a.fNoGlyphIndex == FALSE, "fNoGlyphIndex TRUE\n");
1181
1182     ret = ExtTextOutW(hdc, 1, 1, 0, NULL, glyphs, 4, widths);
1183     ok(ret, "ExtTextOutW should return TRUE\n");
1184
1185     ScriptFreeCache(&sc);
1186 }
1187
1188 static void test_ScriptItemIzeShapePlace(HDC hdc, unsigned short pwOutGlyphs[256])
1189 {
1190     HRESULT         hr;
1191     int             iMaxProps;
1192     const SCRIPT_PROPERTIES **ppSp;
1193
1194     int             cInChars;
1195     int             cMaxItems;
1196     SCRIPT_ITEM     pItem[255];
1197     int             pcItems;
1198     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0}; 
1199     WCHAR           TestItem2[] = {'T', 'e', 's', 't', 'b', 0}; 
1200     WCHAR           TestItem3[] = {'T', 'e', 's', 't', 'c',' ','1','2','3',' ',' ','e','n','d',0};
1201     WCHAR           TestItem4[] = {'T', 'e', 's', 't', 'd',' ',0x0684,0x0694,0x06a4,' ',' ','\r','\n','e','n','d',0};
1202     WCHAR           TestItem5[] = {0x0684,'T','e','s','t','e',' ',0x0684,0x0694,0x06a4,' ',' ','e','n','d',0};
1203     WCHAR           TestItem6[] = {'T', 'e', 's', 't', 'f',' ',' ',' ','\r','\n','e','n','d',0};
1204
1205     SCRIPT_CACHE    psc;
1206     int             cChars;
1207     int             cMaxGlyphs;
1208     unsigned short  pwOutGlyphs1[256];
1209     unsigned short  pwOutGlyphs2[256];
1210     unsigned short  pwLogClust[256];
1211     SCRIPT_VISATTR  psva[256];
1212     int             pcGlyphs;
1213     int             piAdvance[256];
1214     GOFFSET         pGoffset[256];
1215     ABC             pABC[256];
1216     int             cnt;
1217
1218     /* Start testing usp10 functions                                                         */
1219     /* This test determines that the pointer returned by ScriptGetProperties is valid
1220      * by checking a known value in the table                                                */
1221     hr = ScriptGetProperties(&ppSp, &iMaxProps);
1222     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
1223     trace("number of script properties %d\n", iMaxProps);
1224     ok (iMaxProps > 0, "Number of scripts returned should not be 0\n");
1225     if  (iMaxProps > 0)
1226          ok( ppSp[0]->langid == 0, "Langid[0] not = to 0\n"); /* Check a known value to ensure   */
1227                                                               /* ptrs work                       */
1228
1229     /* This is a valid test that will cause parsing to take place                             */
1230     cInChars = 5;
1231     cMaxItems = 255;
1232     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1233     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1234     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
1235      *  returned.                                                                             */
1236     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
1237     if (pcItems > 0)
1238         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
1239             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1240             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
1241
1242     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
1243      * ie. ScriptItemize has succeeded and that pItem has been set                            */
1244     cInChars = 5;
1245     if (hr == 0) {
1246         psc = NULL;                                   /* must be null on first call           */
1247         cChars = cInChars;
1248         cMaxGlyphs = cInChars;
1249         hr = ScriptShape(NULL, &psc, TestItem1, cChars,
1250                          cMaxGlyphs, &pItem[0].a,
1251                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1252         ok (hr == E_PENDING, "If psc is NULL (%08x) the E_PENDING should be returned\n", hr);
1253         cMaxGlyphs = 4;
1254         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
1255                          cMaxGlyphs, &pItem[0].a,
1256                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1257         ok (hr == E_OUTOFMEMORY, "If not enough output area cChars (%d) is > than CMaxGlyphs "
1258                                  "(%d) but not E_OUTOFMEMORY\n",
1259                                  cChars, cMaxGlyphs);
1260         cMaxGlyphs = 256;
1261         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
1262                          cMaxGlyphs, &pItem[0].a,
1263                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1264         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1265         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1266         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1267         if (hr ==0) {
1268             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1269                              pGoffset, pABC);
1270             ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
1271             hr = ScriptPlace(NULL, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1272                              pGoffset, pABC);
1273             ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
1274             for (cnt=0; cnt < pcGlyphs; cnt++)
1275                 pwOutGlyphs[cnt] = pwOutGlyphs1[cnt];                 /* Send to next function */
1276         }
1277
1278         /* This test will check to make sure that SCRIPT_CACHE is reused and that not translation   *
1279          * takes place if fNoGlyphIndex is set.                                                     */
1280
1281         cInChars = 5;
1282         cMaxItems = 255;
1283         hr = ScriptItemize(TestItem2, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1284         ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1285         /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is   *
1286          *  returned.                                                                               */
1287         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
1288                             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1289                              pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
1290         /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue                    */
1291         if (hr == 0) {
1292              cChars = cInChars;
1293              cMaxGlyphs = 256;
1294              pItem[0].a.fNoGlyphIndex = 1;                /* say no translate                     */
1295              hr = ScriptShape(NULL, &psc, TestItem2, cChars,
1296                               cMaxGlyphs, &pItem[0].a,
1297                               pwOutGlyphs2, pwLogClust, psva, &pcGlyphs);
1298              ok (hr != E_PENDING, "If psc should not be NULL (%08x) and the E_PENDING should be returned\n", hr);
1299              ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1300              ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1301              ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1302              for (cnt=0; cnt < cChars && TestItem2[cnt] == pwOutGlyphs2[cnt]; cnt++) {}
1303              ok (cnt == cChars, "Translation to place when told not to. WCHAR %d - %04x != %04x\n",
1304                            cnt, TestItem2[cnt], pwOutGlyphs2[cnt]);
1305              if (hr ==0) {
1306                  hr = ScriptPlace(hdc, &psc, pwOutGlyphs2, pcGlyphs, psva, &pItem[0].a, piAdvance,
1307                                   pGoffset, pABC);
1308                  ok (hr == 0, "ScriptPlace should return 0 not (%08x)\n", hr);
1309              }
1310         }
1311         ScriptFreeCache( &psc);
1312         ok (!psc, "psc is not null after ScriptFreeCache\n");
1313
1314     }
1315
1316     /* This is a valid test that will cause parsing to take place and create 3 script_items   */
1317     cInChars = (sizeof(TestItem3)/2)-1;
1318     cMaxItems = 255;
1319     hr = ScriptItemize(TestItem3, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1320     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1321     if  (hr == 0)
1322         {
1323         ok (pcItems == 3, "The number of SCRIPT_ITEMS should be 3 not %d\n", pcItems);
1324         if (pcItems > 2)
1325         {
1326             ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == 6,
1327                 "Start pos [0] not = 0 (%d) or end pos [1] not = %d\n",
1328                 pItem[0].iCharPos, pItem[1].iCharPos);
1329             ok (pItem[1].iCharPos == 6 && pItem[2].iCharPos == 11,
1330                 "Start pos [1] not = 6 (%d) or end pos [2] not = 11 (%d)\n",
1331                 pItem[1].iCharPos, pItem[2].iCharPos);
1332             ok (pItem[2].iCharPos == 11 && pItem[3].iCharPos == cInChars,
1333                 "Start pos [2] not = 11 (%d) or end [3] pos not = 14 (%d), cInChars = %d\n",
1334                 pItem[2].iCharPos, pItem[3].iCharPos, cInChars);
1335         }
1336     }
1337
1338     /* This is a valid test that will cause parsing to take place and create 5 script_items   */
1339     cInChars = (sizeof(TestItem4)/2)-1;
1340     cMaxItems = 255;
1341     hr = ScriptItemize(TestItem4, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1342     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1343     if  (hr == 0)
1344         {
1345         ok (pcItems == 5, "The number of SCRIPT_ITEMS should be 5 not %d\n", pcItems);
1346         if (pcItems > 4)
1347         {
1348             ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == 6,
1349                 "Start pos [0] not = 0 (%d) or end pos [1] not = %d\n",
1350                 pItem[0].iCharPos, pItem[1].iCharPos);
1351             ok (pItem[0].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
1352                                                pItem[0].a.s.uBidiLevel);
1353             ok (pItem[1].iCharPos == 6 && pItem[2].iCharPos == 11,
1354                 "Start pos [1] not = 6 (%d) or end pos [2] not = 11 (%d)\n",
1355                 pItem[1].iCharPos, pItem[2].iCharPos);
1356             ok (pItem[1].a.s.uBidiLevel == 1, "Should have been bidi=1 not %d\n",
1357                                               pItem[1].a.s.uBidiLevel);
1358             ok (pItem[2].iCharPos == 11 && pItem[3].iCharPos == 12,
1359                 "Start pos [2] not = 11 (%d) or end [3] pos not = 12 (%d)\n",
1360                 pItem[2].iCharPos, pItem[3].iCharPos);
1361             ok (pItem[2].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
1362                                                pItem[2].a.s.uBidiLevel);
1363             ok (pItem[3].iCharPos == 12 && pItem[4].iCharPos == 13,
1364                 "Start pos [3] not = 12 (%d) or end [4] pos not = 13 (%d)\n",
1365                 pItem[3].iCharPos, pItem[4].iCharPos);
1366             ok (pItem[3].a.s.uBidiLevel == 0, "Should have been bidi=0 not %d\n",
1367                                                pItem[3].a.s.uBidiLevel);
1368             ok (pItem[4].iCharPos == 13 && pItem[5].iCharPos == cInChars,
1369                 "Start pos [4] not = 13 (%d) or end [5] pos not = 16 (%d), cInChars = %d\n",
1370                 pItem[4].iCharPos, pItem[5].iCharPos, cInChars);
1371         }
1372     }
1373
1374     /*
1375      * This test is for when the first unicode character requires bidi support
1376      */
1377     cInChars = (sizeof(TestItem5)-1)/sizeof(WCHAR);
1378     hr = ScriptItemize(TestItem5, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1379     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1380     ok (pcItems == 4, "There should have been 4 items, found %d\n", pcItems);
1381     ok (pItem[0].a.s.uBidiLevel == 1, "The first character should have been bidi=1 not %d\n",
1382                                        pItem[0].a.s.uBidiLevel);
1383
1384     /* This test checks to make sure that the test to see if there are sufficient buffers to store  *
1385      * the pointer to the last char works.  Note that windows often needs a greater number of       *
1386      * SCRIPT_ITEMS to process a string than is returned in pcItems.                                */
1387     cInChars = (sizeof(TestItem6)/2)-1;
1388     cMaxItems = 4;
1389     hr = ScriptItemize(TestItem6, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1390     ok (hr == E_OUTOFMEMORY, "ScriptItemize should return E_OUTOFMEMORY, returned %08x\n", hr);
1391
1392 }
1393
1394 static void test_ScriptGetCMap(HDC hdc, unsigned short pwOutGlyphs[256])
1395 {
1396     HRESULT         hr;
1397     SCRIPT_CACHE    psc = NULL;
1398     int             cInChars;
1399     int             cChars;
1400     unsigned short  pwOutGlyphs2[256];
1401     unsigned short  pwOutGlyphs3[256];
1402     DWORD           dwFlags;
1403     int             cnt;
1404
1405     static const WCHAR TestItem1[] = {'T', 'e', 's', 't', 'a', 0};
1406     static const WCHAR TestItem2[] = {0x202B, 'i', 'n', 0x202C,0};
1407     static const WCHAR TestItem3[] = {'a','b','c','d','(','<','{','[',0x2039,0};
1408     static const WCHAR TestItem3b[] = {'a','b','c','d',')','>','}',']',0x203A,0};
1409
1410     /*  Check to make sure that SCRIPT_CACHE gets allocated ok                     */
1411     dwFlags = 0;
1412     cInChars = cChars = 5;
1413     /* Some sanity checks for ScriptGetCMap */
1414
1415     hr = ScriptGetCMap(NULL, NULL, NULL, 0, 0, NULL);
1416     ok( hr == E_INVALIDARG, "(NULL,NULL,NULL,0,0,NULL), "
1417                             "expected E_INVALIDARG, got %08x\n", hr);
1418
1419     hr = ScriptGetCMap(NULL, NULL, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
1420     ok( hr == E_INVALIDARG, "(NULL,NULL,TestItem1, cInChars, dwFlags, pwOutGlyphs3), "
1421                             "expected E_INVALIDARG, got %08x\n", hr);
1422
1423     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1424     psc = NULL;
1425     hr = ScriptGetCMap(NULL, &psc, TestItem1, cInChars, 0, pwOutGlyphs3);
1426     ok( hr == E_PENDING, "(NULL,&psc,NULL,0,0,NULL), expected E_PENDING, "
1427                          "got %08x\n", hr);
1428     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1429
1430     /* Set psc to NULL but add hdc, to be able to check if a pointer is returned in psc */
1431     psc = NULL;
1432     hr = ScriptGetCMap(hdc, &psc, TestItem1, cInChars, 0, pwOutGlyphs3);
1433     ok( hr == S_OK, "ScriptGetCMap(NULL,&psc,NULL,0,0,NULL), expected S_OK, "
1434                     "got %08x\n", hr);
1435     ok( psc != NULL, "ScritpGetCMap expected psc to be not NULL\n");
1436     ScriptFreeCache( &psc);
1437
1438     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1439     psc = NULL;
1440     hr = ScriptGetCMap(NULL, &psc, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
1441     ok( hr == E_PENDING, "(NULL,&psc,), expected E_PENDING, got %08x\n", hr);
1442     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1443     /*  Check to see if the results are the same as those returned by ScriptShape  */
1444     hr = ScriptGetCMap(hdc, &psc, TestItem1, cInChars, dwFlags, pwOutGlyphs3);
1445     ok (hr == 0, "ScriptGetCMap should return 0 not (%08x)\n", hr);
1446     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1447     for (cnt=0; cnt < cChars && pwOutGlyphs[cnt] == pwOutGlyphs3[cnt]; cnt++) {}
1448     ok (cnt == cInChars, "Translation not correct. WCHAR %d - %04x != %04x\n",
1449                          cnt, pwOutGlyphs[cnt], pwOutGlyphs3[cnt]);
1450
1451     ScriptFreeCache( &psc);
1452     ok (!psc, "psc is not null after ScriptFreeCache\n");
1453
1454     cInChars = cChars = 4;
1455     hr = ScriptGetCMap(hdc, &psc, TestItem2, cInChars, dwFlags, pwOutGlyphs3);
1456     ok (hr == S_FALSE, "ScriptGetCMap should return S_FALSE not (%08x)\n", hr);
1457     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1458     ok(pwOutGlyphs3[0] == 0 || broken(pwOutGlyphs3[0] == 0x80), "Glyph 0 should be default glyph\n");
1459     ok(pwOutGlyphs3[3] == 0 || broken(pwOutGlyphs3[0] == 0x80), "Glyph 0 should be default glyph\n");
1460
1461
1462     cInChars = cChars = 9;
1463     hr = ScriptGetCMap(hdc, &psc, TestItem3b, cInChars, dwFlags, pwOutGlyphs2);
1464     ok (hr == S_OK, "ScriptGetCMap should return S_OK not (%08x)\n", hr);
1465     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1466
1467     cInChars = cChars = 9;
1468     dwFlags = SGCM_RTL;
1469     hr = ScriptGetCMap(hdc, &psc, TestItem3, cInChars, dwFlags, pwOutGlyphs3);
1470     ok (hr == S_OK, "ScriptGetCMap should return S_OK not (%08x)\n", hr);
1471     ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1472     ok(pwOutGlyphs3[0] == pwOutGlyphs2[0], "glyph incorrectly altered\n");
1473     ok(pwOutGlyphs3[1] == pwOutGlyphs2[1], "glyph incorreclty altered\n");
1474     ok(pwOutGlyphs3[2] == pwOutGlyphs2[2], "glyph incorreclty altered\n");
1475     ok(pwOutGlyphs3[3] == pwOutGlyphs2[3], "glyph incorreclty altered\n");
1476     ok(pwOutGlyphs3[4] == pwOutGlyphs2[4], "glyph not mirrored correctly\n");
1477     ok(pwOutGlyphs3[5] == pwOutGlyphs2[5], "glyph not mirrored correctly\n");
1478     ok(pwOutGlyphs3[6] == pwOutGlyphs2[6], "glyph not mirrored correctly\n");
1479     ok(pwOutGlyphs3[7] == pwOutGlyphs2[7], "glyph not mirrored correctly\n");
1480     ok(pwOutGlyphs3[8] == pwOutGlyphs2[8], "glyph not mirrored correctly\n");
1481
1482     ScriptFreeCache( &psc);
1483     ok (!psc, "psc is not null after ScriptFreeCache\n");
1484 }
1485
1486 static void test_ScriptGetFontProperties(HDC hdc)
1487 {
1488     HRESULT         hr;
1489     SCRIPT_CACHE    psc,old_psc;
1490     SCRIPT_FONTPROPERTIES sfp;
1491
1492     /* Some sanity checks for ScriptGetFontProperties */
1493
1494     hr = ScriptGetFontProperties(NULL,NULL,NULL);
1495     ok( hr == E_INVALIDARG, "(NULL,NULL,NULL), expected E_INVALIDARG, got %08x\n", hr);
1496
1497     hr = ScriptGetFontProperties(NULL,NULL,&sfp);
1498     ok( hr == E_INVALIDARG, "(NULL,NULL,&sfp), expected E_INVALIDARG, got %08x\n", hr);
1499
1500     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1501     psc = NULL;
1502     hr = ScriptGetFontProperties(NULL,&psc,NULL);
1503     ok( hr == E_INVALIDARG, "(NULL,&psc,NULL), expected E_INVALIDARG, got %08x\n", hr);
1504     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1505
1506     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1507     psc = NULL;
1508     hr = ScriptGetFontProperties(NULL,&psc,&sfp);
1509     ok( hr == E_PENDING, "(NULL,&psc,&sfp), expected E_PENDING, got %08x\n", hr);
1510     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1511
1512     hr = ScriptGetFontProperties(hdc,NULL,NULL);
1513     ok( hr == E_INVALIDARG, "(hdc,NULL,NULL), expected E_INVALIDARG, got %08x\n", hr);
1514
1515     hr = ScriptGetFontProperties(hdc,NULL,&sfp);
1516     ok( hr == E_INVALIDARG, "(hdc,NULL,&sfp), expected E_INVALIDARG, got %08x\n", hr);
1517
1518     /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1519     psc = NULL;
1520     hr = ScriptGetFontProperties(hdc,&psc,NULL);
1521     ok( hr == E_INVALIDARG, "(hdc,&psc,NULL), expected E_INVALIDARG, got %08x\n", hr);
1522     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1523
1524     /* Pass an invalid sfp */
1525     psc = NULL;
1526     sfp.cBytes = sizeof(SCRIPT_FONTPROPERTIES) - 1;
1527     hr = ScriptGetFontProperties(hdc,&psc,&sfp);
1528     ok( hr == E_INVALIDARG, "(hdc,&psc,&sfp) invalid, expected E_INVALIDARG, got %08x\n", hr);
1529     ok( psc != NULL, "Expected a pointer in psc, got NULL\n");
1530     ScriptFreeCache(&psc);
1531     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1532
1533     /* Give it the correct cBytes, we don't care about what's coming back */
1534     sfp.cBytes = sizeof(SCRIPT_FONTPROPERTIES);
1535     psc = NULL;
1536     hr = ScriptGetFontProperties(hdc,&psc,&sfp);
1537     ok( hr == S_OK, "(hdc,&psc,&sfp) partly initialized, expected S_OK, got %08x\n", hr);
1538     ok( psc != NULL, "Expected a pointer in psc, got NULL\n");
1539
1540     /* Save the psc pointer */
1541     old_psc = psc;
1542     /* Now a NULL hdc again */
1543     hr = ScriptGetFontProperties(NULL,&psc,&sfp);
1544     ok( hr == S_OK, "(NULL,&psc,&sfp), expected S_OK, got %08x\n", hr);
1545     ok( psc == old_psc, "Expected psc not to be changed, was %p is now %p\n", old_psc, psc);
1546     ScriptFreeCache(&psc);
1547     ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1548 }
1549
1550 static void test_ScriptTextOut(HDC hdc)
1551 {
1552     HRESULT         hr;
1553
1554     int             cInChars;
1555     int             cMaxItems;
1556     SCRIPT_ITEM     pItem[255];
1557     int             pcItems;
1558     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0}; 
1559
1560     SCRIPT_CACHE    psc;
1561     int             cChars;
1562     int             cMaxGlyphs;
1563     unsigned short  pwOutGlyphs1[256];
1564     WORD            pwLogClust[256];
1565     SCRIPT_VISATTR  psva[256];
1566     int             pcGlyphs;
1567     int             piAdvance[256];
1568     GOFFSET         pGoffset[256];
1569     ABC             pABC[256];
1570     RECT            rect;
1571     int             piX;
1572     int             iCP = 1;
1573     BOOL            fTrailing = FALSE;
1574     SCRIPT_LOGATTR  *psla;
1575     SCRIPT_LOGATTR  sla[256];
1576
1577     /* This is a valid test that will cause parsing to take place                             */
1578     cInChars = 5;
1579     cMaxItems = 255;
1580     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1581     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1582     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
1583      *  returned.                                                                             */
1584     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
1585     if (pcItems > 0)
1586         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
1587             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1588             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
1589
1590     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
1591      * ie. ScriptItemize has succeeded and that pItem has been set                            */
1592     cInChars = 5;
1593     if (hr == 0) {
1594         psc = NULL;                                   /* must be null on first call           */
1595         cChars = cInChars;
1596         cMaxGlyphs = 256;
1597         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
1598                          cMaxGlyphs, &pItem[0].a,
1599                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1600         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1601         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1602         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1603         if (hr ==0) {
1604             /* Note hdc is needed as glyph info is not yet in psc                  */
1605             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1606                              pGoffset, pABC);
1607             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
1608             ScriptFreeCache(&psc);              /* Get rid of psc for next test set */
1609             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1610
1611             hr = ScriptTextOut(NULL, NULL, 0, 0, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL);
1612             ok (hr == E_INVALIDARG, "Should return 0 not (%08x)\n", hr);
1613
1614             hr = ScriptTextOut(NULL, NULL, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1615                                piAdvance, NULL, pGoffset);
1616             ok( hr == E_INVALIDARG, "(NULL,NULL,TestItem1, cInChars, dwFlags, pwOutGlyphs3), "
1617                                     "expected E_INVALIDARG, got %08x\n", hr);
1618
1619             /* Set psc to NULL, to be able to check if a pointer is returned in psc */
1620             psc = NULL;
1621             hr = ScriptTextOut(NULL, &psc, 0, 0, 0, NULL, NULL, NULL, 0, NULL, 0,
1622                                NULL, NULL, NULL);
1623             ok( hr == E_INVALIDARG, "(NULL,&psc,NULL,0,0,0,NULL,), expected E_INVALIDARG, "
1624                                     "got %08x\n", hr);
1625             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1626
1627             /* hdc is required for this one rather than the usual optional          */
1628             psc = NULL;
1629             hr = ScriptTextOut(NULL, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1630                                piAdvance, NULL, pGoffset);
1631             ok( hr == E_INVALIDARG, "(NULL,&psc,), expected E_INVALIDARG, got %08x\n", hr);
1632             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1633
1634             /* Set that it returns 0 status */
1635             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1636                                piAdvance, NULL, pGoffset);
1637             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
1638
1639             /* Test Rect Rgn is acceptable */
1640             rect.top = 10;
1641             rect.bottom = 20;
1642             rect.left = 10;
1643             rect.right = 40;
1644             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, &rect, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1645                                piAdvance, NULL, pGoffset);
1646             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
1647
1648             iCP = 1;
1649             hr = ScriptCPtoX(iCP, fTrailing, cChars, pcGlyphs, (const WORD *) &pwLogClust,
1650                             (const SCRIPT_VISATTR *) &psva, (const int *)&piAdvance, &pItem[0].a, &piX);
1651             ok(hr == S_OK, "ScriptCPtoX Stub should return S_OK not %08x\n", hr);
1652
1653             psla = (SCRIPT_LOGATTR *)&sla;
1654             hr = ScriptBreak(TestItem1, cChars, &pItem[0].a, psla);
1655             ok(hr == S_OK, "ScriptBreak Stub should return S_OK not %08x\n", hr);
1656
1657             /* Clean up and go   */
1658             ScriptFreeCache(&psc);
1659             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1660         }
1661     }
1662 }
1663
1664 static void test_ScriptTextOut2(HDC hdc)
1665 {
1666 /*  Intent is to validate that the HDC passed into ScriptTextOut is
1667  *  used instead of the (possibly) invalid cached one
1668  */
1669     HRESULT         hr;
1670
1671     HDC             hdc1, hdc2;
1672     int             cInChars;
1673     int             cMaxItems;
1674     SCRIPT_ITEM     pItem[255];
1675     int             pcItems;
1676     WCHAR           TestItem1[] = {'T', 'e', 's', 't', 'a', 0};
1677
1678     SCRIPT_CACHE    psc;
1679     int             cChars;
1680     int             cMaxGlyphs;
1681     unsigned short  pwOutGlyphs1[256];
1682     WORD            pwLogClust[256];
1683     SCRIPT_VISATTR  psva[256];
1684     int             pcGlyphs;
1685     int             piAdvance[256];
1686     GOFFSET         pGoffset[256];
1687     ABC             pABC[256];
1688
1689     /* Create an extra DC that will be used until the ScriptTextOut */
1690     hdc1 = CreateCompatibleDC(hdc);
1691     ok (hdc1 != 0, "CreateCompatibleDC failed to create a DC\n");
1692     hdc2 = CreateCompatibleDC(hdc);
1693     ok (hdc2 != 0, "CreateCompatibleDC failed to create a DC\n");
1694
1695     /* This is a valid test that will cause parsing to take place                             */
1696     cInChars = 5;
1697     cMaxItems = 255;
1698     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1699     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1700     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
1701      *  returned.                                                                             */
1702     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
1703     if (pcItems > 0)
1704         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
1705             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1706             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
1707
1708     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
1709      * ie. ScriptItemize has succeeded and that pItem has been set                            */
1710     cInChars = 5;
1711     if (hr == 0) {
1712         psc = NULL;                                   /* must be null on first call           */
1713         cChars = cInChars;
1714         cMaxGlyphs = 256;
1715         hr = ScriptShape(hdc2, &psc, TestItem1, cChars,
1716                          cMaxGlyphs, &pItem[0].a,
1717                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1718         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1719         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1720         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1721         if (hr ==0) {
1722             /* Note hdc is needed as glyph info is not yet in psc                  */
1723             hr = ScriptPlace(hdc2, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1724                              pGoffset, pABC);
1725             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
1726
1727             /*   key part!!!   cached dc is being deleted  */
1728             hr = DeleteDC(hdc2);
1729             ok(hr == 1, "DeleteDC should return 1 not %08x\n", hr);
1730
1731             /* At this point the cached hdc (hdc2) has been destroyed,
1732              * however, we are passing in a *real* hdc (the original hdc).
1733              * The text should be written to that DC
1734              */
1735             hr = ScriptTextOut(hdc1, &psc, 0, 0, 0, NULL, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1736                                piAdvance, NULL, pGoffset);
1737             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
1738             ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1739
1740             DeleteDC(hdc1);
1741
1742             /* Clean up and go   */
1743             ScriptFreeCache(&psc);
1744             ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1745         }
1746     }
1747 }
1748
1749 static void test_ScriptTextOut3(HDC hdc)
1750 {
1751     HRESULT         hr;
1752
1753     int             cInChars;
1754     int             cMaxItems;
1755     SCRIPT_ITEM     pItem[255];
1756     int             pcItems;
1757     WCHAR           TestItem1[] = {' ','\r', 0};
1758
1759     SCRIPT_CACHE    psc;
1760     int             cChars;
1761     int             cMaxGlyphs;
1762     unsigned short  pwOutGlyphs1[256];
1763     WORD            pwLogClust[256];
1764     SCRIPT_VISATTR  psva[256];
1765     int             pcGlyphs;
1766     int             piAdvance[256];
1767     GOFFSET         pGoffset[256];
1768     ABC             pABC[256];
1769     RECT            rect;
1770
1771     /* This is to ensure that nonexistent glyphs are translated into a valid glyph number */
1772     cInChars = 2;
1773     cMaxItems = 255;
1774     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
1775     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", hr);
1776     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
1777      *  returned.                                                                             */
1778     ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
1779     if (pcItems > 0)
1780         ok (pItem[0].iCharPos == 0 && pItem[2].iCharPos == cInChars,
1781             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
1782             pItem[0].iCharPos, cInChars, pItem[2].iCharPos);
1783
1784     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
1785      * ie. ScriptItemize has succeeded and that pItem has been set                            */
1786     cInChars = 2;
1787     if (hr == 0) {
1788         psc = NULL;                                   /* must be null on first call           */
1789         cChars = cInChars;
1790         cMaxGlyphs = 256;
1791         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
1792                          cMaxGlyphs, &pItem[0].a,
1793                          pwOutGlyphs1, pwLogClust, psva, &pcGlyphs);
1794         ok (hr == 0, "ScriptShape should return 0 not (%08x)\n", hr);
1795         ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
1796         ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
1797         if (hr ==0) {
1798             /* Note hdc is needed as glyph info is not yet in psc                  */
1799             hr = ScriptPlace(hdc, &psc, pwOutGlyphs1, pcGlyphs, psva, &pItem[0].a, piAdvance,
1800                              pGoffset, pABC);
1801             ok (hr == 0, "Should return 0 not (%08x)\n", hr);
1802
1803             /* Test Rect Rgn is acceptable */
1804             rect.top = 10;
1805             rect.bottom = 20;
1806             rect.left = 10;
1807             rect.right = 40;
1808             hr = ScriptTextOut(hdc, &psc, 0, 0, 0, &rect, &pItem[0].a, NULL, 0, pwOutGlyphs1, pcGlyphs,
1809                                piAdvance, NULL, pGoffset);
1810             ok (hr == 0, "ScriptTextOut should return 0 not (%08x)\n", hr);
1811
1812         }
1813         /* Clean up and go   */
1814         ScriptFreeCache(&psc);
1815         ok( psc == NULL, "Expected psc to be NULL, got %p\n", psc);
1816     }
1817 }
1818
1819 #define test_item_ScriptXtoX(a,b,c,d,e,f) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _test_item_ScriptXtoX(a,b,c,d,e,f)
1820
1821 static void _test_item_ScriptXtoX(SCRIPT_ANALYSIS *psa, int cChars, int cGlyphs, const int* offsets, const WORD *pwLogClust, const int* piAdvance )
1822 {
1823     int iX, iCP;
1824     int icChars, icGlyphs;
1825     int piCP, piX;
1826     HRESULT hr;
1827     SCRIPT_VISATTR psva[10];
1828     int piTrailing;
1829     BOOL fTrailing;
1830     int direction;
1831
1832     memset(psva,0,sizeof(psva));
1833     direction = (psa->fRTL)?-1:+1;
1834
1835     for(iCP = 0; iCP < cChars; iCP++)
1836     {
1837         iX = offsets[iCP];
1838         icChars = cChars;
1839         icGlyphs = cGlyphs;
1840         hr = ScriptXtoCP(iX, icChars, icGlyphs, pwLogClust, psva, piAdvance, psa, &piCP, &piTrailing);
1841         winetest_ok(hr == S_OK, "ScriptXtoCP: should return S_OK not %08x\n", hr);
1842         winetest_ok(piCP == iCP, "ScriptXtoCP: iX=%d should return piCP=%d not %d\n", iX, iCP, piCP);
1843         winetest_ok(piTrailing == 0, "ScriptXtoCP: iX=%d should return piTrailing=0 not %d\n", iX, piTrailing);
1844     }
1845
1846     for(iCP = 0; iCP < cChars; iCP++)
1847     {
1848         iX = offsets[iCP]+direction;
1849         icChars = cChars;
1850         icGlyphs = cGlyphs;
1851         hr = ScriptXtoCP(iX, icChars, icGlyphs, pwLogClust, psva, piAdvance, psa, &piCP, &piTrailing);
1852         winetest_ok(hr == S_OK, "ScriptXtoCP leading: should return S_OK not %08x\n", hr);
1853         winetest_ok(piCP == iCP, "ScriptXtoCP leading: iX=%d should return piCP=%d not %d\n", iX, iCP, piCP);
1854         winetest_ok(piTrailing == 0, "ScriptXtoCP leading: iX=%d should return piTrailing=0 not %d\n", iX, piTrailing);
1855     }
1856
1857     for(iCP = 0; iCP < cChars; iCP++)
1858     {
1859         iX = offsets[iCP+1]-direction;
1860         icChars = cChars;
1861         icGlyphs = cGlyphs;
1862         hr = ScriptXtoCP(iX, icChars, icGlyphs, pwLogClust, psva, piAdvance, psa, &piCP, &piTrailing);
1863         winetest_ok(hr == S_OK, "ScriptXtoCP trailing: should return S_OK not %08x\n", hr);
1864         winetest_ok(piCP == iCP, "ScriptXtoCP trailing: iX=%d should return piCP=%d not %d\n", iX, iCP, piCP);
1865         winetest_ok(piTrailing == 1, "ScriptXtoCP trailing: iX=%d should return piTrailing=1 not %d\n", iX, piTrailing);
1866     }
1867
1868     for(iCP = 0; iCP <= cChars+1; iCP++)
1869     {
1870         fTrailing = FALSE;
1871         icChars = cChars;
1872         icGlyphs = cGlyphs;
1873         hr = ScriptCPtoX(iCP, fTrailing, icChars, icGlyphs, pwLogClust, psva, piAdvance, psa, &piX);
1874         winetest_ok(hr == S_OK, "ScriptCPtoX: should return S_OK not %08x\n", hr);
1875         winetest_ok(piX == offsets[iCP],
1876            "ScriptCPtoX: iCP=%d should return piX=%d not %d\n", iCP, offsets[iCP], piX);
1877     }
1878
1879     for(iCP = 0; iCP <= cChars+1; iCP++)
1880     {
1881         fTrailing = TRUE;
1882         icChars = cChars;
1883         icGlyphs = cGlyphs;
1884         hr = ScriptCPtoX(iCP, fTrailing, icChars, icGlyphs, pwLogClust, psva, piAdvance, psa, &piX);
1885         winetest_ok(hr == S_OK, "ScriptCPtoX trailing: should return S_OK not %08x\n", hr);
1886         winetest_ok(piX == offsets[iCP+1],
1887            "ScriptCPtoX trailing: iCP=%d should return piX=%d not %d\n", iCP, offsets[iCP+1], piX);
1888     }
1889 }
1890
1891 static void test_ScriptXtoX(void)
1892 /****************************************************************************************
1893  *  This routine tests the ScriptXtoCP and ScriptCPtoX functions using static variables *
1894  ****************************************************************************************/
1895 {
1896     WORD pwLogClust[10] = {0, 0, 0, 1, 1, 2, 2, 3, 3, 3};
1897     WORD pwLogClust_RTL[10] = {3, 3, 3, 2, 2, 1, 1, 0, 0, 0};
1898     WORD pwLogClust_2[7] = {4, 3, 3, 2, 1, 0 ,0};
1899     int piAdvance[10] = {201, 190, 210, 180, 170, 204, 189, 195, 212, 203};
1900     int piAdvance_2[5] = {39, 26, 19, 17, 11};
1901     static const int offsets[13] = {0, 67, 134, 201, 296, 391, 496, 601, 1052, 1503, 1954, 1954, 1954};
1902     static const int offsets_RTL[13] = {781, 721, 661, 601, 496, 391, 296, 201, 134, 67, 0, 0, 0};
1903     static const int offsets_2[10] = {112, 101, 92, 84, 65, 39, 19, 0, 0, 0};
1904     SCRIPT_VISATTR psva[10];
1905     SCRIPT_ANALYSIS sa;
1906     int iX;
1907     int piCP;
1908     int piTrailing;
1909     HRESULT hr;
1910
1911     memset(&sa, 0 , sizeof(SCRIPT_ANALYSIS));
1912     memset(psva, 0, sizeof(psva));
1913
1914     sa.fRTL = FALSE;
1915     hr = ScriptXtoCP(-1, 10, 10, pwLogClust, psva, piAdvance, &sa, &piCP, &piTrailing);
1916     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1917     if (piTrailing)
1918         ok(piCP == -1, "Negative iX should return piCP=-1 not %d\n", piCP);
1919     else /* win2k3 */
1920         ok(piCP == 10, "Negative iX should return piCP=10 not %d\n", piCP);
1921
1922     for (iX = 0; iX <= 7; iX++)
1923     {
1924         WORD clust = 0;
1925         INT advance = 16;
1926         hr = ScriptXtoCP(iX, 1, 1, &clust, psva, &advance, &sa, &piCP, &piTrailing);
1927         ok(piCP==0 && piTrailing==0,"%i should return 0(%i) and 0(%i)\n",iX, piCP,piTrailing);
1928     }
1929     for (iX = 8; iX < 16; iX++)
1930     {
1931         WORD clust = 0;
1932         INT advance = 16;
1933         hr = ScriptXtoCP(iX, 1, 1, &clust, psva, &advance, &sa, &piCP, &piTrailing);
1934         ok(piCP==0 && piTrailing==1,"%i should return 0(%i) and 1(%i)\n",iX, piCP,piTrailing);
1935     }
1936
1937     sa.fRTL = TRUE;
1938     hr = ScriptXtoCP(-1, 10, 10, pwLogClust_RTL, psva, piAdvance, &sa, &piCP, &piTrailing);
1939     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1940     if (piTrailing)
1941         ok(piCP == -1, "Negative iX should return piCP=-1 not %d\n", piCP);
1942     else /* win2k3 */
1943         ok(piCP == 10, "Negative iX should return piCP=10 not %d\n", piCP);
1944
1945     iX = 1954;
1946     hr = ScriptXtoCP(1954, 10, 10, pwLogClust_RTL, psva, piAdvance, &sa, &piCP, &piTrailing);
1947     ok(hr == S_OK, "ScriptXtoCP should return S_OK not %08x\n", hr);
1948     ok(piCP == -1, "iX=%d should return piCP=-1 not %d\n", iX, piCP);
1949     ok(piTrailing == 1, "iX=%d should return piTrailing=1 not %d\n", iX, piTrailing);
1950
1951     for (iX = 1; iX <= 8; iX++)
1952     {
1953         WORD clust = 0;
1954         INT advance = 16;
1955         hr = ScriptXtoCP(iX, 1, 1, &clust, psva, &advance, &sa, &piCP, &piTrailing);
1956         ok(piCP==0 && piTrailing==1,"%i should return 0(%i) and 1(%i)\n",iX,piCP,piTrailing);
1957     }
1958     for (iX = 9; iX < 16; iX++)
1959     {
1960         WORD clust = 0;
1961         INT advance = 16;
1962         hr = ScriptXtoCP(iX, 1, 1, &clust, psva, &advance, &sa, &piCP, &piTrailing);
1963         ok(piCP==0 && piTrailing==0,"%i should return 0(%i) and 0(%i)\n",iX,piCP,piTrailing);
1964     }
1965
1966     sa.fRTL = FALSE;
1967     test_item_ScriptXtoX(&sa, 10, 10, offsets, pwLogClust, piAdvance);
1968     sa.fRTL = TRUE;
1969     test_item_ScriptXtoX(&sa, 10, 10, offsets_RTL, pwLogClust_RTL, piAdvance);
1970     test_item_ScriptXtoX(&sa, 7, 5, offsets_2, pwLogClust_2, piAdvance_2);
1971 }
1972
1973 static void test_ScriptString(HDC hdc)
1974 {
1975 /*******************************************************************************************
1976  *
1977  * This set of tests are for the string functions of uniscribe.  The ScriptStringAnalyse
1978  * function allocates memory pointed to by the SCRIPT_STRING_ANALYSIS ssa pointer.  This
1979  * memory is freed by ScriptStringFree.  There needs to be a valid hdc for this as
1980  * ScriptStringAnalyse calls ScriptSItemize, ScriptShape and ScriptPlace which require it.
1981  *
1982  */
1983
1984     HRESULT         hr;
1985     WCHAR           teststr[] = {'T','e','s','t','1',' ','a','2','b','3', '\0'};
1986     int             len = (sizeof(teststr) / sizeof(WCHAR)) - 1;
1987     int             Glyphs = len * 2 + 16;
1988     int             Charset;
1989     DWORD           Flags = SSA_GLYPHS;
1990     int             ReqWidth = 100;
1991     const int       Dx[5] = {10, 10, 10, 10, 10};
1992     const BYTE      InClass = 0;
1993     SCRIPT_STRING_ANALYSIS ssa = NULL;
1994
1995     int             X = 10; 
1996     int             Y = 100;
1997     UINT            Options = 0; 
1998     const RECT      rc = {0, 50, 100, 100}; 
1999     int             MinSel = 0;
2000     int             MaxSel = 0;
2001     BOOL            Disabled = FALSE;
2002     const int      *clip_len;
2003     int            i;
2004     UINT           *order;
2005
2006
2007     Charset = -1;     /* this flag indicates unicode input */
2008     /* Test without hdc to get E_PENDING */
2009     hr = ScriptStringAnalyse( NULL, teststr, len, Glyphs, Charset, Flags,
2010                               ReqWidth, NULL, NULL, Dx, NULL,
2011                               &InClass, &ssa);
2012     ok(hr == E_PENDING, "ScriptStringAnalyse Stub should return E_PENDING not %08x\n", hr);
2013
2014     /* test with hdc, this should be a valid test  */
2015     hr = ScriptStringAnalyse( hdc, teststr, len, Glyphs, Charset, Flags,
2016                               ReqWidth, NULL, NULL, Dx, NULL,
2017                               &InClass, &ssa);
2018     ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
2019     ScriptStringFree(&ssa);
2020
2021     /* test makes sure that a call with a valid pssa still works */
2022     hr = ScriptStringAnalyse( hdc, teststr, len, Glyphs, Charset, Flags,
2023                               ReqWidth, NULL, NULL, Dx, NULL,
2024                               &InClass, &ssa);
2025     ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
2026     ok(ssa != NULL, "ScriptStringAnalyse pssa should not be NULL\n");
2027
2028     if (hr == S_OK)
2029     {
2030         hr = ScriptStringOut(ssa, X, Y, Options, &rc, MinSel, MaxSel, Disabled);
2031         ok(hr == S_OK, "ScriptStringOut should return S_OK not %08x\n", hr);
2032     }
2033
2034      clip_len = ScriptString_pcOutChars(ssa);
2035      ok(*clip_len == len, "ScriptString_pcOutChars failed, got %d, expected %d\n", *clip_len, len);
2036
2037      order = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *clip_len * sizeof(UINT));
2038      hr = ScriptStringGetOrder(ssa, order);
2039      ok(hr == S_OK, "ScriptStringGetOrder failed, got %08x, expected S_OK\n", hr);
2040
2041      for (i = 0; i < *clip_len; i++) ok(order[i] == i, "%d: got %d expected %d\n", i, order[i], i);
2042      HeapFree(GetProcessHeap(), 0, order);
2043
2044      hr = ScriptStringFree(&ssa);
2045      ok(hr == S_OK, "ScriptStringFree should return S_OK not %08x\n", hr);
2046 }
2047
2048 static void test_ScriptStringXtoCP_CPtoX(HDC hdc)
2049 {
2050 /*****************************************************************************************
2051  *
2052  * This test is for the ScriptStringXtoCP and ScriptStringXtoCP functions.  Due to the
2053  * nature of the fonts between Windows and Wine, the test is implemented by generating
2054  * values using one one function then checking the output of the second.  In this way
2055  * the validity of the functions is established using Windows as a base and confirming
2056  * similar behaviour in wine.
2057  */
2058
2059     HRESULT         hr;
2060     static const WCHAR teststr1[]  = {0x05e9, 'i', 0x05dc, 'n', 0x05d5, 'e', 0x05dd, '.',0};
2061     static const BOOL rtl[] = {1, 0, 1, 0, 1, 0, 1, 0};
2062     void            *String = (WCHAR *) &teststr1;      /* ScriptStringAnalysis needs void */
2063     int             String_len = (sizeof(teststr1)/sizeof(WCHAR))-1;
2064     int             Glyphs = String_len * 2 + 16;       /* size of buffer as recommended  */
2065     int             Charset = -1;                       /* unicode                        */
2066     DWORD           Flags = SSA_GLYPHS;
2067     int             ReqWidth = 100;
2068     const BYTE      InClass = 0;
2069     SCRIPT_STRING_ANALYSIS ssa = NULL;
2070
2071     int             Ch;                                  /* Character position in string */
2072     int             iTrailing;
2073     int             Cp;                                  /* Character position in string */
2074     int             X;
2075     int             trail,lead;
2076     BOOL            fTrailing;
2077
2078     /* Test with hdc, this should be a valid test
2079      * Here we generate an SCRIPT_STRING_ANALYSIS that will be used as input to the
2080      * following character positions to X and X to character position functions.
2081      */
2082
2083     hr = ScriptStringAnalyse( hdc, String, String_len, Glyphs, Charset, Flags,
2084                               ReqWidth, NULL, NULL, NULL, NULL,
2085                               &InClass, &ssa);
2086     ok(hr == S_OK ||
2087        hr == E_INVALIDARG, /* NT */
2088        "ScriptStringAnalyse should return S_OK or E_INVALIDARG not %08x\n", hr);
2089
2090     if  (hr == S_OK)
2091     {
2092         ok(ssa != NULL, "ScriptStringAnalyse ssa should not be NULL\n");
2093
2094         /*
2095          * Loop to generate character positions to provide starting positions for the
2096          * ScriptStringCPtoX and ScriptStringXtoCP functions
2097          */
2098         for (Cp = 0; Cp < String_len; Cp++)
2099         {
2100             /* The fTrailing flag is used to indicate whether the X being returned is at
2101              * the beginning or the end of the character. What happens here is that if
2102              * fTrailing indicates the end of the character, ie. FALSE, then ScriptStringXtoCP
2103              * returns the beginning of the next character and iTrailing is FALSE.  So for this
2104              * loop iTrailing will be FALSE in both cases.
2105              */
2106             hr = ScriptStringCPtoX(ssa, Cp, TRUE, &trail);
2107             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2108             hr = ScriptStringCPtoX(ssa, Cp, FALSE, &lead);
2109             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2110             if (rtl[Cp])
2111                 ok(lead > trail, "Leading values should be after trailing for rtl characters(%i)\n",Cp);
2112             else
2113                 ok(lead < trail, "Trailing values should be after leading for ltr characters(%i)\n",Cp);
2114
2115             /* move by 1 pixel so that we are not inbetween 2 characters.  That could result in being the lead of a rtl and
2116                at the same time the trail of an ltr */
2117
2118             /* inside the leading edge */
2119             X = lead;
2120             if (rtl[Cp]) X--; else X++;
2121             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2122             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2123             ok(Cp == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp, Ch, trail);
2124             ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
2125                                   iTrailing, X);
2126
2127             /* inside the trailing edge */
2128             X = trail;
2129             if (rtl[Cp]) X++; else X--;
2130             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2131             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2132             ok(Cp == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp, Ch, trail);
2133             ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
2134                                   iTrailing, X);
2135
2136             /* outside the "trailing" edge */
2137             if (Cp < String_len-1)
2138             {
2139                 if (rtl[Cp]) X = lead; else X = trail;
2140                 X++;
2141                 hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2142                 ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2143                 ok(Cp + 1 == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp + 1, Ch, trail);
2144                 if (rtl[Cp+1])
2145                     ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
2146                                           iTrailing, X);
2147                 else
2148                     ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
2149                                           iTrailing, X);
2150             }
2151
2152             /* outside the "leading" edge */
2153             if (Cp != 0)
2154             {
2155                 if (rtl[Cp]) X = trail; else X = lead;
2156                 X--;
2157                 hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2158                 ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2159                 ok(Cp - 1 == Ch, "ScriptStringXtoCP should return Ch = %d not %d for X = %d\n", Cp - 1, Ch, trail);
2160                 if (Cp != 0  && rtl[Cp-1])
2161                     ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = %d\n",
2162                                           iTrailing, X);
2163                 else
2164                     ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = %d\n",
2165                                           iTrailing, X);
2166             }
2167         }
2168
2169         /* Check beyond the leading boundary of the whole string */
2170         if (rtl[0])
2171         {
2172             /* having a leading rtl character seems to confuse usp */
2173             /* this looks to be a windows bug we should emulate */
2174             hr = ScriptStringCPtoX(ssa, 0, TRUE, &X);
2175             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2176             X--;
2177             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2178             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2179             ok(Ch == 1, "ScriptStringXtoCP should return Ch = 1 not %d for X outside leading edge when rtl\n", Ch);
2180             ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = outside leading edge when rtl\n",
2181                                        iTrailing);
2182         }
2183         else
2184         {
2185             hr = ScriptStringCPtoX(ssa, 0, FALSE, &X);
2186             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2187             X--;
2188             hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2189             ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2190             ok(Ch == -1, "ScriptStringXtoCP should return Ch = -1 not %d for X outside leading edge\n", Ch);
2191             ok(iTrailing == TRUE, "ScriptStringXtoCP should return iTrailing = 1 not %d for X = outside leading edge\n",
2192                                        iTrailing);
2193         }
2194
2195         /* Check beyond the end boundary of the whole string */
2196         if (rtl[String_len-1])
2197         {
2198             hr = ScriptStringCPtoX(ssa, String_len-1, FALSE, &X);
2199             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2200         }
2201         else
2202         {
2203             hr = ScriptStringCPtoX(ssa, String_len-1, TRUE, &X);
2204             ok(hr == S_OK, "ScriptStringCPtoX should return S_OK not %08x\n", hr);
2205         }
2206         X++;
2207         hr = ScriptStringXtoCP(ssa, X, &Ch, &iTrailing);
2208         ok(hr == S_OK, "ScriptStringXtoCP should return S_OK not %08x\n", hr);
2209         ok(Ch == String_len, "ScriptStringXtoCP should return Ch = %i not %d for X outside trailing edge\n", String_len, Ch);
2210         ok(iTrailing == FALSE, "ScriptStringXtoCP should return iTrailing = 0 not %d for X = outside trailing edge\n",
2211                                    iTrailing);
2212
2213         /*
2214          * Cleanup the SSA for the next round of tests
2215          */
2216         hr = ScriptStringFree(&ssa);
2217         ok(hr == S_OK, "ScriptStringFree should return S_OK not %08x\n", hr);
2218
2219         /*
2220          * Test to see that exceeding the number of chars returns E_INVALIDARG.  First
2221          * generate an SSA for the subsequent tests.
2222          */
2223         hr = ScriptStringAnalyse( hdc, String, String_len, Glyphs, Charset, Flags,
2224                                   ReqWidth, NULL, NULL, NULL, NULL,
2225                                   &InClass, &ssa);
2226         ok(hr == S_OK, "ScriptStringAnalyse should return S_OK not %08x\n", hr);
2227
2228         /*
2229          * When ScriptStringCPtoX is called with a character position Cp that exceeds the
2230          * string length, return E_INVALIDARG.  This also invalidates the ssa so a
2231          * ScriptStringFree should also fail.
2232          */
2233         fTrailing = FALSE;
2234         Cp = String_len + 1;
2235         hr = ScriptStringCPtoX(ssa, Cp, fTrailing, &X);
2236         ok(hr == E_INVALIDARG, "ScriptStringCPtoX should return E_INVALIDARG not %08x\n", hr);
2237
2238         ScriptStringFree(&ssa);
2239     }
2240 }
2241
2242 static void test_ScriptCacheGetHeight(HDC hdc)
2243 {
2244     HRESULT hr;
2245     SCRIPT_CACHE sc = NULL;
2246     LONG height;
2247
2248     hr = ScriptCacheGetHeight(NULL, NULL, NULL);
2249     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2250
2251     hr = ScriptCacheGetHeight(NULL, &sc, NULL);
2252     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2253
2254     hr = ScriptCacheGetHeight(NULL, &sc, &height);
2255     ok(hr == E_PENDING, "expected E_PENDING, got 0x%08x\n", hr);
2256
2257     height = 0;
2258
2259     hr = ScriptCacheGetHeight(hdc, &sc, &height);
2260     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2261     ok(height > 0, "expected height > 0\n");
2262
2263     ScriptFreeCache(&sc);
2264 }
2265
2266 static void test_ScriptGetGlyphABCWidth(HDC hdc)
2267 {
2268     HRESULT hr;
2269     SCRIPT_CACHE sc = NULL;
2270     ABC abc;
2271
2272     hr = ScriptGetGlyphABCWidth(NULL, NULL, 'a', NULL);
2273     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2274
2275     hr = ScriptGetGlyphABCWidth(NULL, &sc, 'a', NULL);
2276     ok(broken(hr == E_PENDING) ||
2277        hr == E_INVALIDARG, /* WIN7 */
2278        "expected E_INVALIDARG, got 0x%08x\n", hr);
2279
2280     hr = ScriptGetGlyphABCWidth(NULL, &sc, 'a', &abc);
2281     ok(hr == E_PENDING, "expected E_PENDING, got 0x%08x\n", hr);
2282
2283     if (0) {    /* crashes on WinXP */
2284     hr = ScriptGetGlyphABCWidth(hdc, &sc, 'a', NULL);
2285     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2286     }
2287
2288     hr = ScriptGetGlyphABCWidth(hdc, &sc, 'a', &abc);
2289     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2290
2291     ScriptFreeCache(&sc);
2292 }
2293
2294 static void test_ScriptLayout(void)
2295 {
2296     HRESULT hr;
2297     static const BYTE levels[][10] =
2298     {
2299         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2300         { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
2301         { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
2302         { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
2303
2304         { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1},
2305         { 1, 1, 1, 2, 2, 2, 1, 1, 1, 1 },
2306         { 2, 2, 2, 1, 1, 1, 2, 2, 2, 2 },
2307         { 0, 0, 1, 1, 2, 2, 1, 1, 0, 0 },
2308         { 1, 1, 2, 2, 3, 3, 2, 2, 1, 1 },
2309
2310         { 0, 0, 1, 1, 2, 2, 1, 1, 0, 1 },
2311         { 1, 0, 1, 2, 2, 1, 2, 1, 0, 1 },
2312     };
2313     static const int expect_l2v[][10] =
2314     {
2315         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
2316         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
2317         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
2318         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
2319
2320         { 0, 1, 2, 3, 4, 9, 8 ,7 ,6, 5},
2321 /**/    { 9, 8, 7, 4, 5, 6, 3 ,2 ,1, 0},
2322 /**/    { 7, 8, 9, 6, 5, 4, 0 ,1 ,2, 3},
2323         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
2324         { 9, 8, 2, 3, 5, 4, 6 ,7 ,1, 0},
2325
2326         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
2327 /**/    { 0, 1, 7, 5, 6, 4, 3 ,2 ,8, 9},
2328     };
2329     static const int expect_v2l[][10] =
2330     {
2331         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
2332         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
2333         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
2334         { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
2335
2336         { 0, 1, 2, 3, 4, 9, 8 ,7 ,6, 5},
2337         { 9, 8, 7, 6, 3, 4, 5 ,2 ,1, 0},
2338         { 6, 7, 8, 9, 5, 4, 3 ,0 ,1, 2},
2339         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
2340         { 9, 8, 2, 3, 5, 4, 6 ,7 ,1, 0},
2341
2342         { 0, 1, 7, 6, 4, 5, 3 ,2 ,8, 9},
2343         { 0, 1, 7, 6, 5, 3, 4 ,2 ,8, 9},
2344     };
2345
2346     int i, j, vistolog[sizeof(levels[0])], logtovis[sizeof(levels[0])];
2347
2348     hr = ScriptLayout(sizeof(levels[0]), NULL, vistolog, logtovis);
2349     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2350
2351     hr = ScriptLayout(sizeof(levels[0]), levels[0], NULL, NULL);
2352     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
2353
2354     for (i = 0; i < sizeof(levels)/sizeof(levels[0]); i++)
2355     {
2356         hr = ScriptLayout(sizeof(levels[0]), levels[i], vistolog, logtovis);
2357         ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
2358
2359         for (j = 0; j < sizeof(levels[i]); j++)
2360         {
2361             ok(expect_v2l[i][j] == vistolog[j],
2362                "failure: levels[%d][%d] = %d, vistolog[%d] = %d\n",
2363                i, j, levels[i][j], j, vistolog[j] );
2364         }
2365
2366         for (j = 0; j < sizeof(levels[i]); j++)
2367         {
2368             ok(expect_l2v[i][j] == logtovis[j],
2369                "failure: levels[%d][%d] = %d, logtovis[%d] = %d\n",
2370                i, j, levels[i][j], j, logtovis[j] );
2371         }
2372     }
2373 }
2374
2375 static BOOL CALLBACK enum_proc(LGRPID group, LCID lcid, LPSTR locale, LONG_PTR lparam)
2376 {
2377     HRESULT hr;
2378     SCRIPT_DIGITSUBSTITUTE sds;
2379     SCRIPT_CONTROL sc;
2380     SCRIPT_STATE ss;
2381     LCID lcid_old;
2382
2383     if (!IsValidLocale(lcid, LCID_INSTALLED)) return TRUE;
2384
2385     memset(&sds, 0, sizeof(sds));
2386     memset(&sc, 0, sizeof(sc));
2387     memset(&ss, 0, sizeof(ss));
2388
2389     lcid_old = GetThreadLocale();
2390     if (!SetThreadLocale(lcid)) return TRUE;
2391
2392     hr = ScriptRecordDigitSubstitution(lcid, &sds);
2393     ok(hr == S_OK, "ScriptRecordDigitSubstitution failed: 0x%08x\n", hr);
2394
2395     hr = ScriptApplyDigitSubstitution(&sds, &sc, &ss);
2396     ok(hr == S_OK, "ScriptApplyDigitSubstitution failed: 0x%08x\n", hr);
2397
2398     SetThreadLocale(lcid_old);
2399     return TRUE;
2400 }
2401
2402 static void test_digit_substitution(void)
2403 {
2404     BOOL ret;
2405     unsigned int i;
2406     static const LGRPID groups[] =
2407     {
2408         LGRPID_WESTERN_EUROPE,
2409         LGRPID_CENTRAL_EUROPE,
2410         LGRPID_BALTIC,
2411         LGRPID_GREEK,
2412         LGRPID_CYRILLIC,
2413         LGRPID_TURKISH,
2414         LGRPID_JAPANESE,
2415         LGRPID_KOREAN,
2416         LGRPID_TRADITIONAL_CHINESE,
2417         LGRPID_SIMPLIFIED_CHINESE,
2418         LGRPID_THAI,
2419         LGRPID_HEBREW,
2420         LGRPID_ARABIC,
2421         LGRPID_VIETNAMESE,
2422         LGRPID_INDIC,
2423         LGRPID_GEORGIAN,
2424         LGRPID_ARMENIAN
2425     };
2426     HMODULE hKernel32;
2427     static BOOL (WINAPI * pEnumLanguageGroupLocalesA)(LANGGROUPLOCALE_ENUMPROCA,LGRPID,DWORD,LONG_PTR);
2428
2429     hKernel32 = GetModuleHandleA("kernel32.dll");
2430     pEnumLanguageGroupLocalesA = (void*)GetProcAddress(hKernel32, "EnumLanguageGroupLocalesA");
2431
2432     if (!pEnumLanguageGroupLocalesA)
2433     {
2434         win_skip("EnumLanguageGroupLocalesA not available on this platform\n");
2435         return;
2436     }
2437
2438     for (i = 0; i < sizeof(groups)/sizeof(groups[0]); i++)
2439     {
2440         ret = pEnumLanguageGroupLocalesA(enum_proc, groups[i], 0, 0);
2441         if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2442         {
2443             win_skip("EnumLanguageGroupLocalesA not implemented on this platform\n");
2444             break;
2445         }
2446         
2447         ok(ret, "EnumLanguageGroupLocalesA failed unexpectedly: %u\n", GetLastError());
2448     }
2449 }
2450
2451 static void test_ScriptGetProperties(void)
2452 {
2453     const SCRIPT_PROPERTIES **props;
2454     HRESULT hr;
2455     int num;
2456
2457     hr = ScriptGetProperties(NULL, NULL);
2458     ok(hr == E_INVALIDARG, "ScriptGetProperties succeeded\n");
2459
2460     hr = ScriptGetProperties(NULL, &num);
2461     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
2462
2463     hr = ScriptGetProperties(&props, NULL);
2464     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
2465
2466     hr = ScriptGetProperties(&props, &num);
2467     ok(hr == S_OK, "ScriptGetProperties failed: 0x%08x\n", hr);
2468 }
2469
2470 static void test_ScriptBreak(void)
2471 {
2472     static const WCHAR test[] = {' ','\r','\n',0};
2473     SCRIPT_ITEM items[4];
2474     SCRIPT_LOGATTR la;
2475     HRESULT hr;
2476
2477     hr = ScriptItemize(test, 3, 4, NULL, NULL, items, NULL);
2478     ok(!hr, "ScriptItemize should return S_OK not %08x\n", hr);
2479
2480     memset(&la, 0, sizeof(la));
2481     hr = ScriptBreak(test, 1, &items[0].a, &la);
2482     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
2483
2484     ok(!la.fSoftBreak, "fSoftBreak set\n");
2485     ok(la.fWhiteSpace, "fWhiteSpace not set\n");
2486     ok(la.fCharStop, "fCharStop not set\n");
2487     ok(!la.fWordStop, "fWordStop set\n");
2488     ok(!la.fInvalid, "fInvalid set\n");
2489     ok(!la.fReserved, "fReserved set\n");
2490
2491     memset(&la, 0, sizeof(la));
2492     hr = ScriptBreak(test + 1, 1, &items[1].a, &la);
2493     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
2494
2495     ok(!la.fSoftBreak, "fSoftBreak set\n");
2496     ok(!la.fWhiteSpace, "fWhiteSpace set\n");
2497     ok(la.fCharStop, "fCharStop not set\n");
2498     ok(!la.fWordStop, "fWordStop set\n");
2499     ok(!la.fInvalid, "fInvalid set\n");
2500     ok(!la.fReserved, "fReserved set\n");
2501
2502     memset(&la, 0, sizeof(la));
2503     hr = ScriptBreak(test + 2, 1, &items[2].a, &la);
2504     ok(!hr, "ScriptBreak should return S_OK not %08x\n", hr);
2505
2506     ok(!la.fSoftBreak, "fSoftBreak set\n");
2507     ok(!la.fWhiteSpace, "fWhiteSpace set\n");
2508     ok(la.fCharStop, "fCharStop not set\n");
2509     ok(!la.fWordStop, "fWordStop set\n");
2510     ok(!la.fInvalid, "fInvalid set\n");
2511     ok(!la.fReserved, "fReserved set\n");
2512 }
2513
2514 static void test_newlines(void)
2515 {
2516     static const WCHAR test1[] = {'t','e','x','t','\r','t','e','x','t',0};
2517     static const WCHAR test2[] = {'t','e','x','t','\n','t','e','x','t',0};
2518     static const WCHAR test3[] = {'t','e','x','t','\r','\n','t','e','x','t',0};
2519     static const WCHAR test4[] = {'t','e','x','t','\n','\r','t','e','x','t',0};
2520     static const WCHAR test5[] = {'1','2','3','4','\n','\r','1','2','3','4',0};
2521     SCRIPT_ITEM items[5];
2522     HRESULT hr;
2523     int count;
2524
2525     count = 0;
2526     hr = ScriptItemize(test1, lstrlenW(test1), 5, NULL, NULL, items, &count);
2527     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
2528     ok(count == 3, "got %d expected 3\n", count);
2529
2530     count = 0;
2531     hr = ScriptItemize(test2, lstrlenW(test2), 5, NULL, NULL, items, &count);
2532     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
2533     ok(count == 3, "got %d expected 3\n", count);
2534
2535     count = 0;
2536     hr = ScriptItemize(test3, lstrlenW(test3), 5, NULL, NULL, items, &count);
2537     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
2538     ok(count == 4, "got %d expected 4\n", count);
2539
2540     count = 0;
2541     hr = ScriptItemize(test4, lstrlenW(test4), 5, NULL, NULL, items, &count);
2542     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
2543     ok(count == 4, "got %d expected 4\n", count);
2544
2545     count = 0;
2546     hr = ScriptItemize(test5, lstrlenW(test5), 5, NULL, NULL, items, &count);
2547     ok(hr == S_OK, "ScriptItemize failed: 0x%08x\n", hr);
2548     ok(count == 4, "got %d expected 4\n", count);
2549 }
2550
2551 START_TEST(usp10)
2552 {
2553     HWND            hwnd;
2554     HDC             hdc;
2555     LOGFONTA        lf;
2556     HFONT           hfont;
2557
2558     unsigned short  pwOutGlyphs[256];
2559
2560     /* We need a valid HDC to drive a lot of Script functions which requires the following    *
2561      * to set up for the tests.                                                               */
2562     hwnd = CreateWindowExA(0, "static", "", WS_POPUP, 0,0,100,100,
2563                            0, 0, 0, NULL);
2564     assert(hwnd != 0);
2565     ShowWindow(hwnd, SW_SHOW);
2566     UpdateWindow(hwnd);
2567
2568     hdc = GetDC(hwnd);                                      /* We now have a hdc             */
2569     ok( hdc != NULL, "HDC failed to be created %p\n", hdc);
2570
2571     memset(&lf, 0, sizeof(LOGFONTA));
2572     lstrcpyA(lf.lfFaceName, "Tahoma");
2573     lf.lfHeight = 10;
2574     lf.lfWeight = 3;
2575     lf.lfWidth = 10;
2576
2577     hfont = SelectObject(hdc, CreateFontIndirectA(&lf));
2578     ok(hfont != NULL, "SelectObject failed: %p\n", hfont);
2579
2580     test_ScriptItemize();
2581     test_ScriptItemIzeShapePlace(hdc,pwOutGlyphs);
2582     test_ScriptGetCMap(hdc, pwOutGlyphs);
2583     test_ScriptCacheGetHeight(hdc);
2584     test_ScriptGetGlyphABCWidth(hdc);
2585     test_ScriptShape(hdc);
2586     test_ScriptShapeOpenType(hdc);
2587     test_ScriptPlace(hdc);
2588
2589     test_ScriptGetFontProperties(hdc);
2590     test_ScriptTextOut(hdc);
2591     test_ScriptTextOut2(hdc);
2592     test_ScriptTextOut3(hdc);
2593     test_ScriptXtoX();
2594     test_ScriptString(hdc);
2595     test_ScriptStringXtoCP_CPtoX(hdc);
2596
2597     test_ScriptLayout();
2598     test_digit_substitution();
2599     test_ScriptGetProperties();
2600     test_ScriptBreak();
2601     test_newlines();
2602
2603     ReleaseDC(hwnd, hdc);
2604     DestroyWindow(hwnd);
2605 }