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