sync gdiplus with wine 1.1.35
[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 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 LOGFONTA lfa, lfa2;
101 GpFont *font;
102 GpStatus stat;
103 GpGraphics *graphics;
104 HDC hdc = GetDC(0);
105 INT style;
106
107 GdipCreateFromHDC(hdc, &graphics);
108 memset(&lfa, 0, sizeof(LOGFONTA));
109 memset(&lfa2, 0xff, sizeof(LOGFONTA));
110
111 /* empty FaceName */
112 lfa.lfFaceName[0] = 0;
113 stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
114 expect(NotTrueTypeFont, stat);
115
116 lstrcpyA(lfa.lfFaceName, "Arial");
117
118 stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
119 if (stat == FileNotFound)
120 {
121 skip("Arial not installed.\n");
122 return;
123 }
124 expect(Ok, stat);
125 stat = GdipGetLogFontA(font, graphics, &lfa2);
126 expect(Ok, stat);
127
128 ok(lfa2.lfHeight < 0, "Expected negative height\n");
129 expect(0, lfa2.lfWidth);
130 expect(0, lfa2.lfEscapement);
131 expect(0, lfa2.lfOrientation);
132 ok((lfa2.lfWeight >= 100) && (lfa2.lfWeight <= 900), "Expected weight to be set\n");
133 expect(0, lfa2.lfItalic);
134 expect(0, lfa2.lfUnderline);
135 expect(0, lfa2.lfStrikeOut);
136 expect(GetTextCharset(hdc), lfa2.lfCharSet);
137 expect(0, lfa2.lfOutPrecision);
138 expect(0, lfa2.lfClipPrecision);
139 expect(0, lfa2.lfQuality);
140 expect(0, lfa2.lfPitchAndFamily);
141
142 GdipDeleteFont(font);
143
144 memset(&lfa, 0, sizeof(LOGFONTA));
145 lfa.lfHeight = 25;
146 lfa.lfWidth = 25;
147 lfa.lfEscapement = lfa.lfOrientation = 50;
148 lfa.lfItalic = lfa.lfUnderline = lfa.lfStrikeOut = TRUE;
149
150 memset(&lfa2, 0xff, sizeof(LOGFONTA));
151 lstrcpyA(lfa.lfFaceName, "Arial");
152
153 stat = GdipCreateFontFromLogfontA(hdc, &lfa, &font);
154 expect(Ok, stat);
155 stat = GdipGetLogFontA(font, graphics, &lfa2);
156 expect(Ok, stat);
157
158 ok(lfa2.lfHeight < 0, "Expected negative height\n");
159 expect(0, lfa2.lfWidth);
160 expect(0, lfa2.lfEscapement);
161 expect(0, lfa2.lfOrientation);
162 ok((lfa2.lfWeight >= 100) && (lfa2.lfWeight <= 900), "Expected weight to be set\n");
163 expect(TRUE, lfa2.lfItalic);
164 expect(TRUE, lfa2.lfUnderline);
165 expect(TRUE, lfa2.lfStrikeOut);
166 expect(GetTextCharset(hdc), lfa2.lfCharSet);
167 expect(0, lfa2.lfOutPrecision);
168 expect(0, lfa2.lfClipPrecision);
169 expect(0, lfa2.lfQuality);
170 expect(0, lfa2.lfPitchAndFamily);
171
172 stat = GdipGetFontStyle(font, &style);
173 expect(Ok, stat);
174 ok (style == (FontStyleItalic | FontStyleUnderline | FontStyleStrikeout),
175 "Expected , got %d\n", style);
176
177 GdipDeleteFont(font);
178
179 GdipDeleteGraphics(graphics);
180 ReleaseDC(0, hdc);
181 }
182
183 static void test_fontfamily (void)
184 {
185 GpFontFamily *family, *clonedFontFamily;
186 WCHAR itsName[LF_FACESIZE];
187 GpStatus stat;
188
189 /* FontFamily cannot be NULL */
190 stat = GdipCreateFontFamilyFromName (arial , NULL, NULL);
191 expect (InvalidParameter, stat);
192
193 /* FontFamily must be able to actually find the family.
194 * If it can't, any subsequent calls should fail.
195 */
196 stat = GdipCreateFontFamilyFromName (nonexistent, NULL, &family);
197 expect (FontFamilyNotFound, stat);
198
199 /* Bitmap fonts are not found */
200 todo_wine
201 {
202 stat = GdipCreateFontFamilyFromName (MSSansSerif, NULL, &family);
203 expect (FontFamilyNotFound, stat);
204 }
205
206 stat = GdipCreateFontFamilyFromName (arial, NULL, &family);
207 if(stat == FontFamilyNotFound)
208 {
209 skip("Arial not installed\n");
210 return;
211 }
212 expect (Ok, stat);
213
214 stat = GdipGetFamilyName (family, itsName, LANG_NEUTRAL);
215 expect (Ok, stat);
216 expect (0, lstrcmpiW(itsName, arial));
217
218 if (0)
219 {
220 /* Crashes on Windows XP SP2, Vista, and so Wine as well */
221 stat = GdipGetFamilyName (family, NULL, LANG_NEUTRAL);
222 expect (Ok, stat);
223 }
224
225 /* Make sure we don't read old data */
226 ZeroMemory (itsName, sizeof(itsName));
227 stat = GdipCloneFontFamily(family, &clonedFontFamily);
228 expect (Ok, stat);
229 GdipDeleteFontFamily(family);
230 stat = GdipGetFamilyName(clonedFontFamily, itsName, LANG_NEUTRAL);
231 expect(Ok, stat);
232 expect(0, lstrcmpiW(itsName, arial));
233
234 GdipDeleteFontFamily(clonedFontFamily);
235 }
236
237 static void test_fontfamily_properties (void)
238 {
239 GpFontFamily* FontFamily = NULL;
240 GpStatus stat;
241 UINT16 result = 0;
242
243 stat = GdipCreateFontFamilyFromName(arial, NULL, &FontFamily);
244 if(stat == FontFamilyNotFound)
245 skip("Arial not installed\n");
246 else
247 {
248 stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
249 expect(Ok, stat);
250 ok (result == 2355, "Expected 2355, got %d\n", result);
251 result = 0;
252 stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
253 expect(Ok, stat);
254 ok(result == 2048, "Expected 2048, got %d\n", result);
255 result = 0;
256 stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
257 expect(Ok, stat);
258 ok(result == 1854, "Expected 1854, got %d\n", result);
259 result = 0;
260 stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
261 ok(result == 434, "Expected 434, got %d\n", result);
262 GdipDeleteFontFamily(FontFamily);
263 }
264
265 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, &FontFamily);
266 if(stat == FontFamilyNotFound)
267 skip("Times New Roman not installed\n");
268 else
269 {
270 result = 0;
271 stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
272 expect(Ok, stat);
273 ok(result == 2355, "Expected 2355, got %d\n", result);
274 result = 0;
275 stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
276 expect(Ok, stat);
277 ok(result == 2048, "Expected 2048, got %d\n", result);
278 result = 0;
279 stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
280 expect(Ok, stat);
281 ok(result == 1825, "Expected 1825, got %d\n", result);
282 result = 0;
283 stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
284 ok(result == 443, "Expected 443 got %d\n", result);
285 GdipDeleteFontFamily(FontFamily);
286 }
287 }
288
289 static void test_getgenerics (void)
290 {
291 GpStatus stat;
292 GpFontFamily* family;
293 WCHAR familyName[LF_FACESIZE];
294 ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));
295
296 stat = GdipGetGenericFontFamilySansSerif (&family);
297 if (stat == FontFamilyNotFound)
298 {
299 skip("Microsoft Sans Serif not installed\n");
300 goto serif;
301 }
302 expect (Ok, stat);
303 stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
304 expect (Ok, stat);
305 ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0) ||
306 (lstrcmpiW(familyName,MSSansSerif) == 0),
307 "Expected Microsoft Sans Serif or MS Sans Serif, got %s\n",
308 debugstr_w(familyName));
309 stat = GdipDeleteFontFamily (family);
310 expect (Ok, stat);
311
312 serif:
313 stat = GdipGetGenericFontFamilySerif (&family);
314 if (stat == FontFamilyNotFound)
315 {
316 skip("Times New Roman not installed\n");
317 goto monospace;
318 }
319 expect (Ok, stat);
320 stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
321 expect (Ok, stat);
322 ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
323 "Expected Times New Roman, got %s\n", debugstr_w(familyName));
324 stat = GdipDeleteFontFamily (family);
325 expect (Ok, stat);
326
327 monospace:
328 stat = GdipGetGenericFontFamilyMonospace (&family);
329 if (stat == FontFamilyNotFound)
330 {
331 skip("Courier New not installed\n");
332 return;
333 }
334 expect (Ok, stat);
335 stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
336 expect (Ok, stat);
337 ok (lstrcmpiW(familyName, CourierNew) == 0,
338 "Expected Courier New, got %s\n", debugstr_w(familyName));
339 stat = GdipDeleteFontFamily (family);
340 expect (Ok, stat);
341 }
342
343 static void test_installedfonts (void)
344 {
345 GpStatus stat;
346 GpFontCollection* collection=NULL;
347
348 stat = GdipNewInstalledFontCollection(NULL);
349 expect (InvalidParameter, stat);
350
351 stat = GdipNewInstalledFontCollection(&collection);
352 expect (Ok, stat);
353 ok (collection != NULL, "got NULL font collection\n");
354 }
355
356 START_TEST(font)
357 {
358 struct GdiplusStartupInput gdiplusStartupInput;
359 ULONG_PTR gdiplusToken;
360
361 gdiplusStartupInput.GdiplusVersion = 1;
362 gdiplusStartupInput.DebugEventCallback = NULL;
363 gdiplusStartupInput.SuppressBackgroundThread = 0;
364 gdiplusStartupInput.SuppressExternalCodecs = 0;
365
366 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
367
368 test_createfont();
369 test_logfont();
370 test_fontfamily();
371 test_fontfamily_properties();
372 test_getgenerics();
373 test_installedfonts();
374
375 GdiplusShutdown(gdiplusToken);
376 }