urlmon: Update MK protocol handling to IInternetProtocolEx.
[wine] / dlls / gdiplus / tests / font.c
1 /*
2  * Unit test suite for fonts
3  *
4  * Copyright (C) 2007 Google (Evan Stade)
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 #include <math.h>
22
23 #include "windows.h"
24 #include "gdiplus.h"
25 #include "wine/test.h"
26
27 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
28 #define expectf(expected, got) ok(fabs(expected - got) < 0.0001, "Expected %.2f, got %.2f\n", expected, got)
29
30 static const WCHAR arial[] = {'A','r','i','a','l','\0'};
31 static const WCHAR nonexistent[] = {'T','h','i','s','F','o','n','t','s','h','o','u','l','d','N','o','t','E','x','i','s','t','\0'};
32 static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
33 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
34 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
35 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
36 static const WCHAR Tahoma[] = {'T','a','h','o','m','a',0};
37
38 static void test_createfont(void)
39 {
40     GpFontFamily* fontfamily = NULL, *fontfamily2;
41     GpFont* font = NULL;
42     GpStatus stat;
43     Unit unit;
44     UINT i;
45     REAL size;
46     WCHAR familyname[LF_FACESIZE];
47
48     stat = GdipCreateFontFamilyFromName(nonexistent, NULL, &fontfamily);
49     expect (FontFamilyNotFound, stat);
50     stat = GdipDeleteFont(font);
51     expect (InvalidParameter, stat);
52     stat = GdipCreateFontFamilyFromName(arial, NULL, &fontfamily);
53     if(stat == FontFamilyNotFound)
54     {
55         skip("Arial not installed\n");
56         return;
57     }
58     expect (Ok, stat);
59     stat = GdipCreateFont(fontfamily, 12, FontStyleRegular, UnitPoint, &font);
60     expect (Ok, stat);
61     stat = GdipGetFontUnit (font, &unit);
62     expect (Ok, stat);
63     expect (UnitPoint, unit);
64
65     stat = GdipGetFamily(font, &fontfamily2);
66     expect(Ok, stat);
67     stat = GdipGetFamilyName(fontfamily2, familyname, 0);
68     expect(Ok, stat);
69     ok (lstrcmpiW(arial, familyname) == 0, "Expected arial, got %s\n",
70             wine_dbgstr_w(familyname));
71     stat = GdipDeleteFontFamily(fontfamily2);
72     expect(Ok, stat);
73
74     /* Test to see if returned size is based on unit (its not) */
75     GdipGetFontSize(font, &size);
76     ok (size == 12, "Expected 12, got %f\n", size);
77     GdipDeleteFont(font);
78
79     /* Make sure everything is converted correctly for all Units */
80     for (i = UnitWorld; i <=UnitMillimeter; i++)
81     {
82         if (i == UnitDisplay) continue; /* Crashes WindowsXP, wtf? */
83         GdipCreateFont(fontfamily, 24, FontStyleRegular, i, &font);
84         GdipGetFontSize (font, &size);
85         ok (size == 24, "Expected 24, got %f (with unit: %d)\n", size, i);
86         GdipGetFontUnit (font, &unit);
87         expect (i, unit);
88         GdipDeleteFont(font);
89     }
90
91     GdipDeleteFontFamily(fontfamily);
92 }
93
94 static void test_logfont(void)
95 {
96     LOGFONTA lfa, lfa2;
97     GpFont *font;
98     GpStatus stat;
99     GpGraphics *graphics;
100     HDC hdc = GetDC(0);
101     INT style;
102
103     GdipCreateFromHDC(hdc, &graphics);
104     memset(&lfa, 0, sizeof(LOGFONTA));
105     memset(&lfa2, 0xff, sizeof(LOGFONTA));
106
107     /* empty FaceName */
108     lfa.lfFaceName[0] = 0;
109     stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
110     expect(NotTrueTypeFont, stat);
111
112     lstrcpyA(lfa.lfFaceName, "Arial");
113
114     stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
115     if (stat == FileNotFound)
116     {
117         skip("Arial not installed.\n");
118         return;
119     }
120     expect(Ok, stat);
121     stat = GdipGetLogFontA(font, graphics, &lfa2);
122     expect(Ok, stat);
123
124     ok(lfa2.lfHeight < 0, "Expected negative height\n");
125     expect(0, lfa2.lfWidth);
126     expect(0, lfa2.lfEscapement);
127     expect(0, lfa2.lfOrientation);
128     ok((lfa2.lfWeight >= 100) && (lfa2.lfWeight <= 900), "Expected weight to be set\n");
129     expect(0, lfa2.lfItalic);
130     expect(0, lfa2.lfUnderline);
131     expect(0, lfa2.lfStrikeOut);
132     ok(lfa2.lfCharSet == GetTextCharset(hdc) || lfa2.lfCharSet == ANSI_CHARSET,
133         "Expected %x or %x, got %x\n", GetTextCharset(hdc), ANSI_CHARSET, lfa2.lfCharSet);
134     expect(0, lfa2.lfOutPrecision);
135     expect(0, lfa2.lfClipPrecision);
136     expect(0, lfa2.lfQuality);
137     expect(0, lfa2.lfPitchAndFamily);
138
139     GdipDeleteFont(font);
140
141     memset(&lfa, 0, sizeof(LOGFONTA));
142     lfa.lfHeight = 25;
143     lfa.lfWidth = 25;
144     lfa.lfEscapement = lfa.lfOrientation = 50;
145     lfa.lfItalic = lfa.lfUnderline = lfa.lfStrikeOut = TRUE;
146
147     memset(&lfa2, 0xff, sizeof(LOGFONTA));
148     lstrcpyA(lfa.lfFaceName, "Arial");
149
150     stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
151     expect(Ok, stat);
152     stat = GdipGetLogFontA(font, graphics, &lfa2);
153     expect(Ok, stat);
154
155     ok(lfa2.lfHeight < 0, "Expected negative height\n");
156     expect(0, lfa2.lfWidth);
157     expect(0, lfa2.lfEscapement);
158     expect(0, lfa2.lfOrientation);
159     ok((lfa2.lfWeight >= 100) && (lfa2.lfWeight <= 900), "Expected weight to be set\n");
160     expect(TRUE, lfa2.lfItalic);
161     expect(TRUE, lfa2.lfUnderline);
162     expect(TRUE, lfa2.lfStrikeOut);
163     ok(lfa2.lfCharSet == GetTextCharset(hdc) || lfa2.lfCharSet == ANSI_CHARSET,
164         "Expected %x or %x, got %x\n", GetTextCharset(hdc), ANSI_CHARSET, lfa2.lfCharSet);
165     expect(0, lfa2.lfOutPrecision);
166     expect(0, lfa2.lfClipPrecision);
167     expect(0, lfa2.lfQuality);
168     expect(0, lfa2.lfPitchAndFamily);
169
170     stat = GdipGetFontStyle(font, &style);
171     expect(Ok, stat);
172     ok (style == (FontStyleItalic | FontStyleUnderline | FontStyleStrikeout),
173             "Expected , got %d\n", style);
174
175     GdipDeleteFont(font);
176
177     GdipDeleteGraphics(graphics);
178     ReleaseDC(0, hdc);
179 }
180
181 static void test_fontfamily (void)
182 {
183     GpFontFamily *family, *clonedFontFamily;
184     WCHAR itsName[LF_FACESIZE];
185     GpStatus stat;
186
187     /* FontFamily cannot be NULL */
188     stat = GdipCreateFontFamilyFromName (arial , NULL, NULL);
189     expect (InvalidParameter, stat);
190
191     /* FontFamily must be able to actually find the family.
192      * If it can't, any subsequent calls should fail.
193      */
194     stat = GdipCreateFontFamilyFromName (nonexistent, NULL, &family);
195     expect (FontFamilyNotFound, stat);
196
197     /* Bitmap fonts are not found */
198     stat = GdipCreateFontFamilyFromName (MSSansSerif, NULL, &family);
199     expect (FontFamilyNotFound, stat);
200     if(stat == Ok) GdipDeleteFontFamily(family);
201
202     stat = GdipCreateFontFamilyFromName (arial, NULL, &family);
203     if(stat == FontFamilyNotFound)
204     {
205         skip("Arial not installed\n");
206         return;
207     }
208     expect (Ok, stat);
209
210     stat = GdipGetFamilyName (family, itsName, LANG_NEUTRAL);
211     expect (Ok, stat);
212     expect (0, lstrcmpiW(itsName, arial));
213
214     if (0)
215     {
216         /* Crashes on Windows XP SP2, Vista, and so Wine as well */
217         stat = GdipGetFamilyName (family, NULL, LANG_NEUTRAL);
218         expect (Ok, stat);
219     }
220
221     /* Make sure we don't read old data */
222     ZeroMemory (itsName, sizeof(itsName));
223     stat = GdipCloneFontFamily(family, &clonedFontFamily);
224     expect (Ok, stat);
225     GdipDeleteFontFamily(family);
226     stat = GdipGetFamilyName(clonedFontFamily, itsName, LANG_NEUTRAL);
227     expect(Ok, stat);
228     expect(0, lstrcmpiW(itsName, arial));
229
230     GdipDeleteFontFamily(clonedFontFamily);
231 }
232
233 static void test_fontfamily_properties (void)
234 {
235     GpFontFamily* FontFamily = NULL;
236     GpStatus stat;
237     UINT16 result = 0;
238
239     stat = GdipCreateFontFamilyFromName(arial, NULL, &FontFamily);
240     if(stat == FontFamilyNotFound)
241         skip("Arial not installed\n");
242     else
243     {
244         stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
245         expect(Ok, stat);
246         ok (result == 2355, "Expected 2355, got %d\n", result);
247         result = 0;
248         stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
249         expect(Ok, stat);
250         ok(result == 2048, "Expected 2048, got %d\n", result);
251         result = 0;
252         stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
253         expect(Ok, stat);
254         ok(result == 1854, "Expected 1854, got %d\n", result);
255         result = 0;
256         stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
257         ok(result == 434, "Expected 434, got %d\n", result);
258         GdipDeleteFontFamily(FontFamily);
259     }
260
261     stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, &FontFamily);
262     if(stat == FontFamilyNotFound)
263         skip("Times New Roman not installed\n");
264     else
265     {
266         result = 0;
267         stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
268         expect(Ok, stat);
269         ok(result == 2355, "Expected 2355, got %d\n", result);
270         result = 0;
271         stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
272         expect(Ok, stat);
273         ok(result == 2048, "Expected 2048, got %d\n", result);
274         result = 0;
275         stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
276         expect(Ok, stat);
277         ok(result == 1825, "Expected 1825, got %d\n", result);
278         result = 0;
279         stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
280         ok(result == 443, "Expected 443 got %d\n", result);
281         GdipDeleteFontFamily(FontFamily);
282     }
283 }
284
285 static void test_getgenerics (void)
286 {
287     GpStatus stat;
288     GpFontFamily* family;
289     WCHAR familyName[LF_FACESIZE];
290     ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));
291
292     stat = GdipGetGenericFontFamilySansSerif (&family);
293     if (stat == FontFamilyNotFound)
294     {
295         skip("Microsoft Sans Serif not installed\n");
296         goto serif;
297     }
298     expect (Ok, stat);
299     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
300     expect (Ok, stat);
301     if (!lstrcmpiW(familyName, Tahoma))
302         todo_wine ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0),
303                       "Expected Microsoft Sans Serif, got Tahoma\n");
304     else
305         ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0),
306             "Expected Microsoft Sans Serif, got %s\n", wine_dbgstr_w(familyName));
307     stat = GdipDeleteFontFamily (family);
308     expect (Ok, stat);
309
310 serif:
311     stat = GdipGetGenericFontFamilySerif (&family);
312     if (stat == FontFamilyNotFound)
313     {
314         skip("Times New Roman not installed\n");
315         goto monospace;
316     }
317     expect (Ok, stat);
318     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
319     expect (Ok, stat);
320     ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
321         "Expected Times New Roman, got %s\n", wine_dbgstr_w(familyName));
322     stat = GdipDeleteFontFamily (family);
323     expect (Ok, stat);
324
325 monospace:
326     stat = GdipGetGenericFontFamilyMonospace (&family);
327     if (stat == FontFamilyNotFound)
328     {
329         skip("Courier New not installed\n");
330         return;
331     }
332     expect (Ok, stat);
333     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
334     expect (Ok, stat);
335     ok (lstrcmpiW(familyName, CourierNew) == 0,
336         "Expected Courier New, got %s\n", wine_dbgstr_w(familyName));
337     stat = GdipDeleteFontFamily (family);
338     expect (Ok, stat);
339 }
340
341 static void test_installedfonts (void)
342 {
343     GpStatus stat;
344     GpFontCollection* collection=NULL;
345
346     stat = GdipNewInstalledFontCollection(NULL);
347     expect (InvalidParameter, stat);
348
349     stat = GdipNewInstalledFontCollection(&collection);
350     expect (Ok, stat);
351     ok (collection != NULL, "got NULL font collection\n");
352 }
353
354 static void test_heightgivendpi(void)
355 {
356     GpStatus stat;
357     GpFont* font = NULL;
358     GpFontFamily* fontfamily = NULL;
359     REAL height;
360
361     stat = GdipCreateFontFamilyFromName(arial, NULL, &fontfamily);
362     if(stat == FontFamilyNotFound)
363     {
364         skip("Arial not installed\n");
365         return;
366     }
367     expect(Ok, stat);
368
369     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitPixel, &font);
370     expect(Ok, stat);
371
372     stat = GdipGetFontHeightGivenDPI(NULL, 96, &height);
373     expect(InvalidParameter, stat);
374
375     stat = GdipGetFontHeightGivenDPI(font, 96, NULL);
376     expect(InvalidParameter, stat);
377
378     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
379     expect(Ok, stat);
380     expectf((REAL)34.497070, height);
381     GdipDeleteFont(font);
382
383     height = 12345;
384     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitWorld, &font);
385     expect(Ok, stat);
386     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
387     expect(Ok, stat);
388     expectf((REAL)34.497070, height);
389     GdipDeleteFont(font);
390
391     height = 12345;
392     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitPoint, &font);
393     expect(Ok, stat);
394     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
395     expect(Ok, stat);
396     expectf((REAL)45.996094, height);
397     GdipDeleteFont(font);
398
399     height = 12345;
400     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitInch, &font);
401     expect(Ok, stat);
402     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
403     expect(Ok, stat);
404     expectf((REAL)3311.718750, height);
405     GdipDeleteFont(font);
406
407     height = 12345;
408     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitDocument, &font);
409     expect(Ok, stat);
410     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
411     expect(Ok, stat);
412     expectf((REAL)11.039062, height);
413     GdipDeleteFont(font);
414
415     height = 12345;
416     stat = GdipCreateFont(fontfamily, 30, FontStyleRegular, UnitMillimeter, &font);
417     expect(Ok, stat);
418     stat = GdipGetFontHeightGivenDPI(font, 96, &height);
419     expect(Ok, stat);
420     expectf((REAL)130.382614, height);
421     GdipDeleteFont(font);
422
423     GdipDeleteFontFamily(fontfamily);
424 }
425
426 START_TEST(font)
427 {
428     struct GdiplusStartupInput gdiplusStartupInput;
429     ULONG_PTR gdiplusToken;
430
431     gdiplusStartupInput.GdiplusVersion              = 1;
432     gdiplusStartupInput.DebugEventCallback          = NULL;
433     gdiplusStartupInput.SuppressBackgroundThread    = 0;
434     gdiplusStartupInput.SuppressExternalCodecs      = 0;
435
436     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
437
438     test_createfont();
439     test_logfont();
440     test_fontfamily();
441     test_fontfamily_properties();
442     test_getgenerics();
443     test_installedfonts();
444     test_heightgivendpi();
445
446     GdiplusShutdown(gdiplusToken);
447 }