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