wined3d: texturefactor-> fragment states.
[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;
45     GpFont* font = NULL;
46     GpStatus stat;
47     Unit unit;
48     UINT i;
49     REAL size;
50
51     stat = GdipCreateFontFamilyFromName(nonexistent, NULL, &fontfamily);
52     expect (FontFamilyNotFound, stat);
53     stat = GdipDeleteFont(font);
54     expect (InvalidParameter, stat);
55     stat = GdipCreateFontFamilyFromName(arial, NULL, &fontfamily);
56     expect (Ok, stat);
57     stat = GdipCreateFont(fontfamily, 12, FontStyleRegular, UnitPoint, &font);
58     expect (Ok, stat);
59     stat = GdipGetFontUnit (font, &unit);
60     expect (Ok, stat);
61     expect (UnitPoint, unit);
62
63     /* Test to see if returned size is based on unit (its not) */
64     GdipGetFontSize(font, &size);
65     ok (size == 12, "Expected 12, got %f\n", size);
66     GdipDeleteFont(font);
67
68     /* Make sure everything is converted correctly for all Units */
69     for (i = UnitWorld; i <=UnitMillimeter; i++)
70     {
71         if (i == UnitDisplay) continue; /* Crashes WindowsXP, wtf? */
72         GdipCreateFont(fontfamily, 24, FontStyleRegular, i, &font);
73         GdipGetFontSize (font, &size);
74         ok (size == 24, "Expected 24, got %f (with unit: %d)\n", size, i);
75         GdipGetFontUnit (font, &unit);
76         expect (i, unit);
77         GdipDeleteFont(font);
78     }
79
80     GdipDeleteFontFamily(fontfamily);
81 }
82
83 static void test_logfont(void)
84 {
85     LOGFONTW lfw, lfw2;
86     GpFont *font;
87     GpStatus stat;
88     GpGraphics *graphics;
89     HDC hdc = GetDC(0);
90
91     GdipCreateFromHDC(hdc, &graphics);
92     memset(&lfw, 0, sizeof(LOGFONTW));
93     memset(&lfw2, 0xff, sizeof(LOGFONTW));
94     memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
95
96     stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
97     expect(Ok, stat);
98     stat = GdipGetLogFontW(font, graphics, &lfw2);
99     expect(Ok, stat);
100
101     ok(lfw2.lfHeight < 0, "Expected negative height\n");
102     expect(0, lfw2.lfWidth);
103     expect(0, lfw2.lfEscapement);
104     expect(0, lfw2.lfOrientation);
105     ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
106     expect(0, lfw2.lfItalic);
107     expect(0, lfw2.lfUnderline);
108     expect(0, lfw2.lfStrikeOut);
109     expect(0, lfw2.lfCharSet);
110     expect(0, lfw2.lfOutPrecision);
111     expect(0, lfw2.lfClipPrecision);
112     expect(0, lfw2.lfQuality);
113     expect(0, lfw2.lfPitchAndFamily);
114
115     GdipDeleteFont(font);
116
117     memset(&lfw, 0, sizeof(LOGFONTW));
118     lfw.lfHeight = 25;
119     lfw.lfWidth = 25;
120     lfw.lfEscapement = lfw.lfOrientation = 50;
121     lfw.lfItalic = lfw.lfUnderline = lfw.lfStrikeOut = TRUE;
122
123     memset(&lfw2, 0xff, sizeof(LOGFONTW));
124     memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
125
126     stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
127     expect(Ok, stat);
128     stat = GdipGetLogFontW(font, graphics, &lfw2);
129     expect(Ok, stat);
130
131     ok(lfw2.lfHeight < 0, "Expected negative height\n");
132     expect(0, lfw2.lfWidth);
133     expect(0, lfw2.lfEscapement);
134     expect(0, lfw2.lfOrientation);
135     ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
136     expect(TRUE, lfw2.lfItalic);
137     expect(TRUE, lfw2.lfUnderline);
138     expect(TRUE, lfw2.lfStrikeOut);
139     expect(0, lfw2.lfCharSet);
140     expect(0, lfw2.lfOutPrecision);
141     expect(0, lfw2.lfClipPrecision);
142     expect(0, lfw2.lfQuality);
143     expect(0, lfw2.lfPitchAndFamily);
144
145     GdipDeleteFont(font);
146
147     GdipDeleteGraphics(graphics);
148     ReleaseDC(0, hdc);
149 }
150
151 static void test_fontfamily (void)
152 {
153     GpFontFamily *family, *clonedFontFamily;
154     WCHAR itsName[LF_FACESIZE];
155     GpStatus stat;
156
157     /* FontFamily can not be NULL */
158     stat = GdipCreateFontFamilyFromName (arial , NULL, NULL);
159     expect (InvalidParameter, stat);
160
161     /* FontFamily must be able to actually find the family.
162      * If it can't, any subsequent calls should fail.
163      */
164     stat = GdipCreateFontFamilyFromName (nonexistent, NULL, &family);
165     expect (FontFamilyNotFound, stat);
166     ok ((lstrcmpiW(itsName, nonexistent) != 0),
167         "Expected a non-zero value for nonexistent font!\n");
168
169     /* Bitmap fonts are not found */
170 todo_wine
171 {
172     stat = GdipCreateFontFamilyFromName (MSSansSerif, NULL, &family);
173     expect (FontFamilyNotFound, stat);
174 }
175
176     stat = GdipCreateFontFamilyFromName (arial, NULL, &family);
177     expect (Ok, stat);
178
179     stat = GdipGetFamilyName (family, itsName, LANG_NEUTRAL);
180     expect (Ok, stat);
181     expect (0, lstrcmpiW(itsName, arial));
182
183     if (0)
184     {
185         /* Crashes on Windows XP SP2, Vista, and so Wine as well */
186         stat = GdipGetFamilyName (family, NULL, LANG_NEUTRAL);
187         expect (Ok, stat);
188     }
189
190     /* Make sure we don't read old data */
191     ZeroMemory (itsName, sizeof(itsName));
192     stat = GdipCloneFontFamily(family, &clonedFontFamily);
193     expect (Ok, stat);
194     GdipDeleteFontFamily(family);
195     stat = GdipGetFamilyName(clonedFontFamily, itsName, LANG_NEUTRAL);
196     expect(Ok, stat);
197     expect(0, lstrcmpiW(itsName, arial));
198
199     GdipDeleteFontFamily(clonedFontFamily);
200 }
201
202 static void test_fontfamily_properties (void)
203 {
204     GpFontFamily* FontFamily = NULL;
205     GpStatus stat;
206     UINT16 result = 0;
207
208     GdipCreateFontFamilyFromName(arial, NULL, &FontFamily);
209 todo_wine
210 {
211     stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
212     expect(Ok, stat);
213     ok (result == 2355, "Expected 2355, got %d\n", result);
214 }
215     result = 0;
216     stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
217     expect(Ok, stat);
218     ok(result == 2048, "Expected 2048, got %d\n", result);
219     result = 0;
220     stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
221     expect(Ok, stat);
222     ok(result == 1854, "Expected 1854, got %d\n", result);
223     result = 0;
224     stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
225     ok(result == 434, "Expected 434, got %d\n", result);
226     GdipDeleteFontFamily(FontFamily);
227
228     GdipCreateFontFamilyFromName(TimesNewRoman, NULL, &FontFamily);
229     result = 0;
230 todo_wine
231 {
232     stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
233     expect(Ok, stat);
234     ok(result == 2355, "Expected 2355, got %d\n", result);
235 }
236     result = 0;
237     stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
238     expect(Ok, stat);
239     ok(result == 2048, "Expected 2048, got %d\n", result);
240     result = 0;
241     stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
242     expect(Ok, stat);
243     ok(result == 1825, "Expected 1825, got %d\n", result);
244     result = 0;
245     stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
246     ok(result == 443, "Expected 443 got %d\n", result);
247     GdipDeleteFontFamily(FontFamily);
248 }
249
250 static void test_getgenerics (void)
251 {
252     GpStatus stat;
253     GpFontFamily* family;
254     WCHAR familyName[LF_FACESIZE];
255     ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));
256
257     stat = GdipGetGenericFontFamilySansSerif (&family);
258     expect (Ok, stat);
259     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
260     expect (Ok, stat);
261     ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0) ||
262         (lstrcmpiW(familyName,MSSansSerif) == 0),
263         "Expected Microsoft Sans Serif or MS Sans Serif, got %s\n",
264         debugstr_w(familyName));
265     stat = GdipDeleteFontFamily (family);
266     expect (Ok, stat);
267
268     stat = GdipGetGenericFontFamilySerif (&family);
269     expect (Ok, stat);
270     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
271     expect (Ok, stat);
272     ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
273         "Expected Times New Roman, got %s\n", debugstr_w(familyName));
274     stat = GdipDeleteFontFamily (family);
275     expect (Ok, stat);
276
277     stat = GdipGetGenericFontFamilyMonospace (&family);
278     expect (Ok, stat);
279     stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
280     expect (Ok, stat);
281     ok (lstrcmpiW(familyName, CourierNew) == 0,
282         "Expected Courier New, got %s\n", debugstr_w(familyName));
283     stat = GdipDeleteFontFamily (family);
284     expect (Ok, stat);
285 }
286
287 START_TEST(font)
288 {
289     struct GdiplusStartupInput gdiplusStartupInput;
290     ULONG_PTR gdiplusToken;
291
292     gdiplusStartupInput.GdiplusVersion              = 1;
293     gdiplusStartupInput.DebugEventCallback          = NULL;
294     gdiplusStartupInput.SuppressBackgroundThread    = 0;
295     gdiplusStartupInput.SuppressExternalCodecs      = 0;
296
297     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
298
299     test_createfont();
300     test_logfont();
301     test_fontfamily();
302     test_fontfamily_properties();
303     test_getgenerics();
304
305     GdiplusShutdown(gdiplusToken);
306 }