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