wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[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 "windows.h"
22 #include "gdiplus.h"
23 #include "wine/test.h"
24
25 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
26
27 static const WCHAR arial[] = {'A','r','i','a','l','\0'};
28 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'};
29 static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
30 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
31 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
32 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
33
34 static const char *debugstr_w(LPCWSTR str)
35 {
36    static char buf[1024];
37    WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
38    return buf;
39 }
40
41
42 static void test_createfont(void)
43 {
44     GpFontFamily* fontfamily = NULL, *fontfamily2;
45     GpFont* font = NULL;
46     GpStatus stat;
47     Unit unit;
48     UINT i;
49     REAL size;
50     WCHAR familyname[LF_FACESIZE];
51
52     stat = GdipCreateFontFamilyFromName(nonexistent, NULL, &fontfamily);
53     expect (FontFamilyNotFound, stat);
54     stat = GdipDeleteFont(font);
55     expect (InvalidParameter, stat);
56     stat = GdipCreateFontFamilyFromName(arial, NULL, &fontfamily);
57     if(stat == FontFamilyNotFound)
58     {
59         skip("Arial not installed\n");
60         return;
61     }
62     expect (Ok, stat);
63     stat = GdipCreateFont(fontfamily, 12, FontStyleRegular, UnitPoint, &font);
64     expect (Ok, stat);
65     stat = GdipGetFontUnit (font, &unit);
66     expect (Ok, stat);
67     expect (UnitPoint, unit);
68
69     stat = GdipGetFamily(font, &fontfamily2);
70     expect(Ok, stat);
71     stat = GdipGetFamilyName(fontfamily2, familyname, 0);
72     expect(Ok, stat);
73     ok (lstrcmpiW(arial, familyname) == 0, "Expected arial, got %s\n",
74             debugstr_w(familyname));
75     stat = GdipDeleteFontFamily(fontfamily2);
76     expect(Ok, stat);
77
78     /* Test to see if returned size is based on unit (its not) */
79     GdipGetFontSize(font, &size);
80     ok (size == 12, "Expected 12, got %f\n", size);
81     GdipDeleteFont(font);
82
83     /* Make sure everything is converted correctly for all Units */
84     for (i = UnitWorld; i <=UnitMillimeter; i++)
85     {
86         if (i == UnitDisplay) continue; /* Crashes WindowsXP, wtf? */
87         GdipCreateFont(fontfamily, 24, FontStyleRegular, i, &font);
88         GdipGetFontSize (font, &size);
89         ok (size == 24, "Expected 24, got %f (with unit: %d)\n", size, i);
90         GdipGetFontUnit (font, &unit);
91         expect (i, unit);
92         GdipDeleteFont(font);
93     }
94
95     GdipDeleteFontFamily(fontfamily);
96 }
97
98 static void test_logfont(void)
99 {
100     LOGFONTW lfw, lfw2;
101     GpFont *font;
102     GpStatus stat;
103     GpGraphics *graphics;
104     HDC hdc = GetDC(0);
105     INT style;
106
107     GdipCreateFromHDC(hdc, &graphics);
108     memset(&lfw, 0, sizeof(LOGFONTW));
109     memset(&lfw2, 0xff, sizeof(LOGFONTW));
110
111     /* empty FaceName */
112     lfw.lfFaceName[0] = 0;
113     stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
114
115     expect(NotTrueTypeFont, stat);
116
117     memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
118
119     stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
120     if (stat == FileNotFound)
121     {
122         skip("Arial not installed.\n");
123         return;
124     }
125     expect(Ok, stat);
126     stat = GdipGetLogFontW(font, graphics, &lfw2);
127     expect(Ok, stat);
128
129     ok(lfw2.lfHeight < 0, "Expected negative height\n");
130     expect(0, lfw2.lfWidth);
131     expect(0, lfw2.lfEscapement);
132     expect(0, lfw2.lfOrientation);
133     ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
134     expect(0, lfw2.lfItalic);
135     expect(0, lfw2.lfUnderline);
136     expect(0, lfw2.lfStrikeOut);
137     expect(GetTextCharset(hdc), lfw2.lfCharSet);
138     expect(0, lfw2.lfOutPrecision);
139     expect(0, lfw2.lfClipPrecision);
140     expect(0, lfw2.lfQuality);
141     expect(0, lfw2.lfPitchAndFamily);
142
143     GdipDeleteFont(font);
144
145     memset(&lfw, 0, sizeof(LOGFONTW));
146     lfw.lfHeight = 25;
147     lfw.lfWidth = 25;
148     lfw.lfEscapement = lfw.lfOrientation = 50;
149     lfw.lfItalic = lfw.lfUnderline = lfw.lfStrikeOut = TRUE;
150
151     memset(&lfw2, 0xff, sizeof(LOGFONTW));
152     memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
153
154     stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
155     expect(Ok, stat);
156     stat = GdipGetLogFontW(font, graphics, &lfw2);
157     expect(Ok, stat);
158
159     ok(lfw2.lfHeight < 0, "Expected negative height\n");
160     expect(0, lfw2.lfWidth);
161     expect(0, lfw2.lfEscapement);
162     expect(0, lfw2.lfOrientation);
163     ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
164     expect(TRUE, lfw2.lfItalic);
165     expect(TRUE, lfw2.lfUnderline);
166     expect(TRUE, lfw2.lfStrikeOut);
167     expect(GetTextCharset(hdc), lfw2.lfCharSet);
168     expect(0, lfw2.lfOutPrecision);
169     expect(0, lfw2.lfClipPrecision);
170     expect(0, lfw2.lfQuality);
171     expect(0, lfw2.lfPitchAndFamily);
172
173     stat = GdipGetFontStyle(font, &style);
174     expect(Ok, stat);
175     ok (style == (FontStyleItalic | FontStyleUnderline | FontStyleStrikeout),
176             "Expected , got %d\n", style);
177
178     GdipDeleteFont(font);
179
180     GdipDeleteGraphics(graphics);
181     ReleaseDC(0, hdc);
182 }
183
184 static void test_fontfamily (void)
185 {
186     GpFontFamily *family, *clonedFontFamily;
187     WCHAR itsName[LF_FACESIZE];
188     GpStatus stat;
189
190     /* FontFamily cannot be NULL */
191     stat = GdipCreateFontFamilyFromName (arial , NULL, NULL);
192     expect (InvalidParameter, stat);
193
194     /* FontFamily must be able to actually find the family.
195      * If it can't, any subsequent calls should fail.
196      */
197     stat = GdipCreateFontFamilyFromName (nonexistent, NULL, &family);
198     expect (FontFamilyNotFound, stat);
199
200     /* Bitmap fonts are not found */
201 todo_wine
202 {
203     stat = GdipCreateFontFamilyFromName (MSSansSerif, NULL, &family);
204     expect (FontFamilyNotFound, stat);
205 }
206
207     stat = GdipCreateFontFamilyFromName (arial, NULL, &family);
208     if(stat == FontFamilyNotFound)
209     {
210         skip("Arial not installed\n");
211         return;
212     }
213     expect (Ok, stat);
214
215     stat = GdipGetFamilyName (family, itsName, LANG_NEUTRAL);
216     expect (Ok, stat);
217     expect (0, lstrcmpiW(itsName, arial));
218
219     if (0)
220     {
221         /* Crashes on Windows XP SP2, Vista, and so Wine as well */
222         stat = GdipGetFamilyName (family, NULL, LANG_NEUTRAL);
223         expect (Ok, stat);
224     }
225
226     /* Make sure we don't read old data */
227     ZeroMemory (itsName, sizeof(itsName));
228     stat = GdipCloneFontFamily(family, &clonedFontFamily);
229     expect (Ok, stat);
230     GdipDeleteFontFamily(family);
231     stat = GdipGetFamilyName(clonedFontFamily, itsName, LANG_NEUTRAL);
232     expect(Ok, stat);
233     expect(0, lstrcmpiW(itsName, arial));
234
235     GdipDeleteFontFamily(clonedFontFamily);
236 }
237
238 static void test_fontfamily_properties (void)
239 {
240     GpFontFamily* FontFamily = NULL;
241     GpStatus stat;
242     UINT16 result = 0;
243
244     stat = GdipCreateFontFamilyFromName(arial, NULL, &FontFamily);
245     if(stat == FontFamilyNotFound)
246         skip("Arial not installed\n");
247     else
248     {
249         stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
250         expect(Ok, stat);
251         ok (result == 2355, "Expected 2355, got %d\n", result);
252         result = 0;
253         stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
254         expect(Ok, stat);
255         ok(result == 2048, "Expected 2048, got %d\n", result);
256         result = 0;
257         stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
258         expect(Ok, stat);
259         ok(result == 1854, "Expected 1854, got %d\n", result);
260         result = 0;
261         stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
262         ok(result == 434, "Expected 434, got %d\n", result);
263         GdipDeleteFontFamily(FontFamily);
264     }
265
266     stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, &FontFamily);
267     if(stat == FontFamilyNotFound)
268         skip("Times New Roman not installed\n");
269     else
270     {
271         result = 0;
272         stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
273         expect(Ok, stat);
274         ok(result == 2355, "Expected 2355, got %d\n", result);
275         result = 0;
276         stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
277         expect(Ok, stat);
278         ok(result == 2048, "Expected 2048, got %d\n", result);
279         result = 0;
280         stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
281         expect(Ok, stat);
282         ok(result == 1825, "Expected 1825, got %d\n", result);
283         result = 0;
284         stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
285         ok(result == 443, "Expected 443 got %d\n", result);
286         GdipDeleteFontFamily(FontFamily);
287     }
288 }
289
290 static void test_getgenerics (void)
291 {
292     GpStatus stat;
293     GpFontFamily* family;
294     WCHAR familyName[LF_FACESIZE];
295     ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));
296
297     stat = GdipGetGenericFontFamilySansSerif (&family);
298     if (stat == FontFamilyNotFound)
299     {
300         skip("Microsoft Sans Serif not installed\n");
301         goto serif;
302     }
303     expect (Ok, stat);
304     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
305     expect (Ok, stat);
306     ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0) ||
307         (lstrcmpiW(familyName,MSSansSerif) == 0),
308         "Expected Microsoft Sans Serif or MS Sans Serif, got %s\n",
309         debugstr_w(familyName));
310     stat = GdipDeleteFontFamily (family);
311     expect (Ok, stat);
312
313 serif:
314     stat = GdipGetGenericFontFamilySerif (&family);
315     if (stat == FontFamilyNotFound)
316     {
317         skip("Times New Roman not installed\n");
318         goto monospace;
319     }
320     expect (Ok, stat);
321     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
322     expect (Ok, stat);
323     ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
324         "Expected Times New Roman, got %s\n", debugstr_w(familyName));
325     stat = GdipDeleteFontFamily (family);
326     expect (Ok, stat);
327
328 monospace:
329     stat = GdipGetGenericFontFamilyMonospace (&family);
330     if (stat == FontFamilyNotFound)
331     {
332         skip("Courier New not installed\n");
333         return;
334     }
335     expect (Ok, stat);
336     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
337     expect (Ok, stat);
338     ok (lstrcmpiW(familyName, CourierNew) == 0,
339         "Expected Courier New, got %s\n", debugstr_w(familyName));
340     stat = GdipDeleteFontFamily (family);
341     expect (Ok, stat);
342 }
343
344 START_TEST(font)
345 {
346     struct GdiplusStartupInput gdiplusStartupInput;
347     ULONG_PTR gdiplusToken;
348
349     gdiplusStartupInput.GdiplusVersion              = 1;
350     gdiplusStartupInput.DebugEventCallback          = NULL;
351     gdiplusStartupInput.SuppressBackgroundThread    = 0;
352     gdiplusStartupInput.SuppressExternalCodecs      = 0;
353
354     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
355
356     test_createfont();
357     test_logfont();
358     test_fontfamily();
359     test_fontfamily_properties();
360     test_getgenerics();
361
362     GdiplusShutdown(gdiplusToken);
363 }