shell32/tests: Add a few more ShellExecute() file URL tests.
[wine] / dlls / dwrite / tests / font.c
1 /*
2  *    Font related tests
3  *
4  * Copyright 2012 Nikolay Sivov for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include "windows.h"
24
25 #include "initguid.h"
26 #include "dwrite.h"
27
28 #include "wine/test.h"
29
30 #define EXPECT_HR(hr,hr_exp) \
31     ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)
32
33 #define EXPECT_REF(obj,ref) _expect_ref((IUnknown*)obj, ref, __LINE__)
34 static void _expect_ref(IUnknown* obj, ULONG ref, int line)
35 {
36     ULONG rc = IUnknown_AddRef(obj);
37     IUnknown_Release(obj);
38     ok_(__FILE__,line)(rc-1 == ref, "expected refcount %d, got %d\n", ref, rc-1);
39 }
40
41 static IDWriteFactory *factory;
42
43 static void test_CreateFontFromLOGFONT(void)
44 {
45     static const WCHAR arialW[] = {'A','r','i','a','l',0};
46     static const WCHAR blahW[]  = {'B','l','a','h','!',0};
47     IDWriteGdiInterop *interop;
48     DWRITE_FONT_WEIGHT weight;
49     DWRITE_FONT_STYLE style;
50     IDWriteFont *font;
51     LOGFONTW logfont;
52     LONG weights[][2] = {
53         {FW_NORMAL, DWRITE_FONT_WEIGHT_NORMAL},
54         {FW_BOLD, DWRITE_FONT_WEIGHT_BOLD},
55         {  0, DWRITE_FONT_WEIGHT_NORMAL},
56         { 50, DWRITE_FONT_WEIGHT_NORMAL},
57         {150, DWRITE_FONT_WEIGHT_NORMAL},
58         {250, DWRITE_FONT_WEIGHT_NORMAL},
59         {350, DWRITE_FONT_WEIGHT_NORMAL},
60         {450, DWRITE_FONT_WEIGHT_NORMAL},
61         {650, DWRITE_FONT_WEIGHT_BOLD},
62         {750, DWRITE_FONT_WEIGHT_BOLD},
63         {850, DWRITE_FONT_WEIGHT_BOLD},
64         {950, DWRITE_FONT_WEIGHT_BOLD},
65         {960, DWRITE_FONT_WEIGHT_BOLD},
66     };
67     HRESULT hr;
68     BOOL ret;
69     int i;
70
71     hr = IDWriteFactory_GetGdiInterop(factory, &interop);
72     EXPECT_HR(hr, S_OK);
73
74 if (0)
75     /* null out parameter crashes this call */
76     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, NULL, NULL);
77
78     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, NULL, &font);
79     EXPECT_HR(hr, E_INVALIDARG);
80
81     memset(&logfont, 0, sizeof(logfont));
82     logfont.lfHeight = 12;
83     logfont.lfWidth  = 12;
84     logfont.lfWeight = FW_NORMAL;
85     logfont.lfItalic = 1;
86     lstrcpyW(logfont.lfFaceName, arialW);
87
88     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
89     EXPECT_HR(hr, S_OK);
90
91     /* now check properties */
92     weight = IDWriteFont_GetWeight(font);
93 todo_wine
94     ok(weight == DWRITE_FONT_WEIGHT_NORMAL, "got %d\n", weight);
95
96     style = IDWriteFont_GetStyle(font);
97     ok(style == DWRITE_FONT_STYLE_ITALIC, "got %d\n", style);
98
99     ret = IDWriteFont_IsSymbolFont(font);
100     ok(!ret, "got %d\n", ret);
101
102     IDWriteFont_Release(font);
103
104     /* weight values */
105     for (i = 0; i < sizeof(weights)/(2*sizeof(LONG)); i++)
106     {
107         memset(&logfont, 0, sizeof(logfont));
108         logfont.lfHeight = 12;
109         logfont.lfWidth  = 12;
110         logfont.lfWeight = weights[i][0];
111         lstrcpyW(logfont.lfFaceName, arialW);
112
113         hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
114         EXPECT_HR(hr, S_OK);
115
116         weight = IDWriteFont_GetWeight(font);
117     todo_wine
118         ok(weight == weights[i][1],
119             "%d: got %d, expected %d\n", i, weight, weights[i][1]);
120         IDWriteFont_Release(font);
121     }
122
123     /* weight not from enum */
124     memset(&logfont, 0, sizeof(logfont));
125     logfont.lfHeight = 12;
126     logfont.lfWidth  = 12;
127     logfont.lfWeight = 550;
128     lstrcpyW(logfont.lfFaceName, arialW);
129
130     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
131     EXPECT_HR(hr, S_OK);
132
133     weight = IDWriteFont_GetWeight(font);
134 todo_wine
135     ok(weight == DWRITE_FONT_WEIGHT_NORMAL || broken(weight == DWRITE_FONT_WEIGHT_BOLD) /* win7 w/o SP */,
136         "got %d\n", weight);
137     IDWriteFont_Release(font);
138
139     /* empty or nonexistent face name */
140     memset(&logfont, 0, sizeof(logfont));
141     logfont.lfHeight = 12;
142     logfont.lfWidth  = 12;
143     logfont.lfWeight = FW_NORMAL;
144     lstrcpyW(logfont.lfFaceName, blahW);
145
146     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
147 todo_wine
148     EXPECT_HR(hr, DWRITE_E_NOFONT);
149
150     memset(&logfont, 0, sizeof(logfont));
151     logfont.lfHeight = 12;
152     logfont.lfWidth  = 12;
153     logfont.lfWeight = FW_NORMAL;
154
155     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
156 todo_wine
157     EXPECT_HR(hr, DWRITE_E_NOFONT);
158
159     IDWriteGdiInterop_Release(interop);
160 }
161
162 static void test_CreateBitmapRenderTarget(void)
163 {
164     IDWriteBitmapRenderTarget *target, *target2;
165     IDWriteGdiInterop *interop;
166     DIBSECTION ds;
167     HBITMAP hbm;
168     HRESULT hr;
169     SIZE size;
170     HDC hdc;
171     int ret;
172
173     hr = IDWriteFactory_GetGdiInterop(factory, &interop);
174     EXPECT_HR(hr, S_OK);
175
176     target = NULL;
177     hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 0, 0, &target);
178     EXPECT_HR(hr, S_OK);
179
180 if (0) /* crashes on native */
181     hr = IDWriteBitmapRenderTarget_GetSize(target, NULL);
182
183     size.cx = size.cy = -1;
184     hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
185     EXPECT_HR(hr, S_OK);
186     ok(size.cx == 0, "got %d\n", size.cx);
187     ok(size.cy == 0, "got %d\n", size.cy);
188
189     target2 = NULL;
190     hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 0, 0, &target2);
191     EXPECT_HR(hr, S_OK);
192     ok(target != target2, "got %p, %p\n", target2, target);
193     IDWriteBitmapRenderTarget_Release(target2);
194
195     hdc = IDWriteBitmapRenderTarget_GetMemoryDC(target);
196     ok(hdc != NULL, "got %p\n", hdc);
197
198     hbm = GetCurrentObject(hdc, OBJ_BITMAP);
199     ok(hbm != NULL, "got %p\n", hbm);
200
201     /* check DIB properties */
202     ret = GetObjectW(hbm, sizeof(ds), &ds);
203     ok(ret == sizeof(BITMAP), "got %d\n", ret);
204     ok(ds.dsBm.bmWidth == 1, "got %d\n", ds.dsBm.bmWidth);
205     ok(ds.dsBm.bmHeight == 1, "got %d\n", ds.dsBm.bmHeight);
206     ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
207     ok(ds.dsBm.bmBitsPixel == 1, "got %d\n", ds.dsBm.bmBitsPixel);
208     ok(!ds.dsBm.bmBits, "got %p\n", ds.dsBm.bmBits);
209
210     IDWriteBitmapRenderTarget_Release(target);
211
212     hbm = GetCurrentObject(hdc, OBJ_BITMAP);
213     ok(!hbm, "got %p\n", hbm);
214
215     target = NULL;
216     hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 10, 5, &target);
217     EXPECT_HR(hr, S_OK);
218
219     hdc = IDWriteBitmapRenderTarget_GetMemoryDC(target);
220     ok(hdc != NULL, "got %p\n", hdc);
221
222     hbm = GetCurrentObject(hdc, OBJ_BITMAP);
223     ok(hbm != NULL, "got %p\n", hbm);
224
225     /* check DIB properties */
226     ret = GetObjectW(hbm, sizeof(ds), &ds);
227     ok(ret == sizeof(ds), "got %d\n", ret);
228     ok(ds.dsBm.bmWidth == 10, "got %d\n", ds.dsBm.bmWidth);
229     ok(ds.dsBm.bmHeight == 5, "got %d\n", ds.dsBm.bmHeight);
230     ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
231     ok(ds.dsBm.bmBitsPixel == 32, "got %d\n", ds.dsBm.bmBitsPixel);
232     ok(ds.dsBm.bmBits != NULL, "got %p\n", ds.dsBm.bmBits);
233
234     size.cx = size.cy = -1;
235     hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
236     EXPECT_HR(hr, S_OK);
237     ok(size.cx == 10, "got %d\n", size.cx);
238     ok(size.cy == 5, "got %d\n", size.cy);
239
240     IDWriteBitmapRenderTarget_Release(target);
241
242     IDWriteGdiInterop_Release(interop);
243 }
244
245 static void test_GetFontFamily(void)
246 {
247     static const WCHAR arialW[] = {'A','r','i','a','l',0};
248     IDWriteFontFamily *family, *family2;
249     IDWriteGdiInterop *interop;
250     IDWriteFont *font;
251     LOGFONTW logfont;
252     HRESULT hr;
253
254     hr = IDWriteFactory_GetGdiInterop(factory, &interop);
255     EXPECT_HR(hr, S_OK);
256
257     memset(&logfont, 0, sizeof(logfont));
258     logfont.lfHeight = 12;
259     logfont.lfWidth  = 12;
260     logfont.lfWeight = FW_NORMAL;
261     logfont.lfItalic = 1;
262     lstrcpyW(logfont.lfFaceName, arialW);
263
264     hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
265     EXPECT_HR(hr, S_OK);
266
267 if (0) /* crashes on native */
268     hr = IDWriteFont_GetFontFamily(font, NULL);
269
270     EXPECT_REF(font, 1);
271     hr = IDWriteFont_GetFontFamily(font, &family);
272     EXPECT_HR(hr, S_OK);
273     EXPECT_REF(font, 1);
274     EXPECT_REF(family, 2);
275
276     hr = IDWriteFont_GetFontFamily(font, &family2);
277     EXPECT_HR(hr, S_OK);
278     ok(family2 == family, "got %p, previous %p\n", family2, family);
279     EXPECT_REF(font, 1);
280     EXPECT_REF(family, 3);
281     IDWriteFontFamily_Release(family2);
282
283     hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFontFamily, (void**)&family2);
284     EXPECT_HR(hr, E_NOINTERFACE);
285     ok(family2 == NULL, "got %p\n", family2);
286
287     IDWriteFontFamily_Release(family);
288     IDWriteFont_Release(font);
289     IDWriteGdiInterop_Release(interop);
290 }
291
292 START_TEST(font)
293 {
294     HRESULT hr;
295
296     hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IDWriteFactory, (IUnknown**)&factory);
297     ok(hr == S_OK, "got 0x%08x\n", hr);
298     if (hr != S_OK)
299     {
300         win_skip("failed to create factory\n");
301         return;
302     }
303
304     test_CreateFontFromLOGFONT();
305     test_CreateBitmapRenderTarget();
306     test_GetFontFamily();
307
308     IDWriteFactory_Release(factory);
309 }