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