merge trunk head (37902)
[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 nonexistant[] = {'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(nonexistant, 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
81 static void test_logfont(void)
82 {
83 LOGFONTW lfw, lfw2;
84 GpFont *font;
85 GpStatus stat;
86 GpGraphics *graphics;
87 HDC hdc = GetDC(0);
88
89 GdipCreateFromHDC(hdc, &graphics);
90 memset(&lfw, 0, sizeof(LOGFONTW));
91 memset(&lfw2, 0xff, sizeof(LOGFONTW));
92 memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
93
94 stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
95 expect(Ok, stat);
96 stat = GdipGetLogFontW(font, graphics, &lfw2);
97 expect(Ok, stat);
98
99 ok(lfw2.lfHeight < 0, "Expected negative height\n");
100 expect(0, lfw2.lfWidth);
101 expect(0, lfw2.lfEscapement);
102 expect(0, lfw2.lfOrientation);
103 ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
104 expect(0, lfw2.lfItalic);
105 expect(0, lfw2.lfUnderline);
106 expect(0, lfw2.lfStrikeOut);
107 expect(0, lfw2.lfCharSet);
108 expect(0, lfw2.lfOutPrecision);
109 expect(0, lfw2.lfClipPrecision);
110 expect(0, lfw2.lfQuality);
111 expect(0, lfw2.lfPitchAndFamily);
112
113 GdipDeleteFont(font);
114
115 memset(&lfw, 0, sizeof(LOGFONTW));
116 lfw.lfHeight = 25;
117 lfw.lfWidth = 25;
118 lfw.lfEscapement = lfw.lfOrientation = 50;
119 lfw.lfItalic = lfw.lfUnderline = lfw.lfStrikeOut = TRUE;
120
121 memset(&lfw2, 0xff, sizeof(LOGFONTW));
122 memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));
123
124 stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
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(TRUE, lfw2.lfItalic);
135 expect(TRUE, lfw2.lfUnderline);
136 expect(TRUE, lfw2.lfStrikeOut);
137 expect(0, 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 GdipDeleteGraphics(graphics);
146 ReleaseDC(0, hdc);
147 }
148
149 static void test_fontfamily (void)
150 {
151 GpFontFamily** family = NULL;
152 WCHAR itsName[LF_FACESIZE];
153 GpStatus stat;
154
155 /* FontFamily can not be NULL */
156 stat = GdipCreateFontFamilyFromName (arial , NULL, family);
157 expect (InvalidParameter, stat);
158
159 family = GdipAlloc (sizeof (GpFontFamily*));
160
161 /* FontFamily must be able to actually find the family.
162 * If it can't, any subsequent calls should fail
163 *
164 * We currently fail (meaning we don't) because we don't actually
165 * test to see if we can successfully get a family
166 */
167 stat = GdipCreateFontFamilyFromName (nonexistant, NULL, family);
168 expect (FontFamilyNotFound, stat);
169 stat = GdipGetFamilyName (*family,itsName, LANG_NEUTRAL);
170 expect (InvalidParameter, stat);
171 ok ((lstrcmpiW(itsName,nonexistant) != 0),
172 "Expected a non-zero value for nonexistant font!\n");
173 stat = GdipDeleteFontFamily(*family);
174 expect (InvalidParameter, stat);
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 stat = GdipDeleteFontFamily(*family);
191 expect (Ok, stat);
192
193 GdipFree (family);
194 }
195
196
197 static void test_getgenerics (void)
198 {
199 GpStatus stat;
200 GpFontFamily** family;
201 WCHAR familyName[LF_FACESIZE];
202 ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));
203
204 family = GdipAlloc (sizeof (GpFontFamily*));
205
206 stat = GdipGetGenericFontFamilySansSerif (family);
207 expect (Ok, stat);
208 stat = GdipGetFamilyName (*family, familyName, LANG_NEUTRAL);
209 expect (Ok, stat);
210 ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0) ||
211 (lstrcmpiW(familyName,MSSansSerif) == 0),
212 "Expected Microsoft Sans Serif or MS Sans Serif, got %s\n",
213 debugstr_w(familyName));
214 stat = GdipDeleteFontFamily (*family);
215 expect (Ok, stat);
216
217 stat = GdipGetGenericFontFamilySerif (family);
218 expect (Ok, stat);
219 stat = GdipGetFamilyName (*family, familyName, LANG_NEUTRAL);
220 expect (Ok, stat);
221 ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
222 "Expected Times New Roman, got %s\n", debugstr_w(familyName));
223 stat = GdipDeleteFontFamily (*family);
224 expect (Ok, stat);
225
226 stat = GdipGetGenericFontFamilyMonospace (family);
227 expect (Ok, stat);
228 stat = GdipGetFamilyName (*family, familyName, LANG_NEUTRAL);
229 expect (Ok, stat);
230 ok (lstrcmpiW(familyName, CourierNew) == 0,
231 "Expected Courier New, got %s\n", debugstr_w(familyName));
232 stat = GdipDeleteFontFamily (*family);
233 expect (Ok, stat);
234
235 GdipFree (family);
236 }
237
238 START_TEST(font)
239 {
240 struct GdiplusStartupInput gdiplusStartupInput;
241 ULONG_PTR gdiplusToken;
242
243 gdiplusStartupInput.GdiplusVersion = 1;
244 gdiplusStartupInput.DebugEventCallback = NULL;
245 gdiplusStartupInput.SuppressBackgroundThread = 0;
246 gdiplusStartupInput.SuppressExternalCodecs = 0;
247
248 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
249
250 test_createfont();
251 test_logfont();
252 test_fontfamily();
253 test_getgenerics();
254
255 GdiplusShutdown(gdiplusToken);
256 }