added gdi32 wine regression tests
[reactos.git] / reactos / regtests / winetests / gdi32 / gdiobj.c
1 /*
2 * Unit test suite for GDI objects
3 *
4 * Copyright 2002 Mike McCormack
5 * Copyright 2004 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28
29 #include "wine/test.h"
30
31
32 static void check_font(const char* test, const LOGFONTA* lf, HFONT hfont)
33 {
34 LOGFONTA getobj_lf;
35 int ret, minlen = 0;
36
37 if (!hfont)
38 return;
39
40 ret = GetObject(hfont, sizeof(getobj_lf), &getobj_lf);
41 /* NT4 tries to be clever and only returns the minimum length */
42 while (lf->lfFaceName[minlen] && minlen < LF_FACESIZE-1)
43 minlen++;
44 minlen += FIELD_OFFSET(LOGFONTA, lfFaceName) + 1;
45 ok(ret == sizeof(LOGFONTA) || ret == minlen,
46 "%s: GetObject returned %d expected %d or %d\n", test, ret, sizeof(LOGFONTA), minlen);
47 ok(!memcmp(&lf, &lf, FIELD_OFFSET(LOGFONTA, lfFaceName)), "%s: fonts don't match\n", test);
48 ok(!lstrcmpA(lf->lfFaceName, getobj_lf.lfFaceName),
49 "%s: font names don't match: %s != %s\n", test, lf->lfFaceName, getobj_lf.lfFaceName);
50 }
51
52 static HFONT create_font(const char* test, const LOGFONTA* lf)
53 {
54 HFONT hfont = CreateFontIndirectA(lf);
55 ok(hfont != 0, "%s: CreateFontIndirect failed\n", test);
56 if (hfont)
57 check_font(test, lf, hfont);
58 return hfont;
59 }
60
61 static void test_logfont(void)
62 {
63 LOGFONTA lf;
64 HFONT hfont;
65
66 memset(&lf, 0, sizeof lf);
67
68 lf.lfCharSet = ANSI_CHARSET;
69 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
70 lf.lfWeight = FW_DONTCARE;
71 lf.lfHeight = 16;
72 lf.lfWidth = 16;
73 lf.lfQuality = DEFAULT_QUALITY;
74
75 lstrcpyA(lf.lfFaceName, "Arial");
76 hfont = create_font("Arial", &lf);
77 DeleteObject(hfont);
78
79 memset(&lf, 'A', sizeof(lf));
80 hfont = CreateFontIndirectA(&lf);
81 ok(hfont != 0, "CreateFontIndirectA with strange LOGFONT failed\n");
82
83 lf.lfFaceName[LF_FACESIZE - 1] = 0;
84 check_font("AAA...", &lf, hfont);
85 DeleteObject(hfont);
86 }
87
88 static INT CALLBACK font_enum_proc(const LOGFONT *elf, const TEXTMETRIC *ntm, DWORD type, LPARAM lParam)
89 {
90 if (type & RASTER_FONTTYPE)
91 {
92 LOGFONT *lf = (LOGFONT *)lParam;
93 *lf = *elf;
94 return 0; /* stop enumeration */
95 }
96
97 return 1; /* continue enumeration */
98 }
99
100 static void test_font_metrics(HDC hdc, HFONT hfont, const char *test_str,
101 INT test_str_len, const TEXTMETRICA *tm_orig,
102 const SIZE *size_orig, INT width_orig,
103 INT scale_x, INT scale_y)
104 {
105 HFONT old_hfont;
106 TEXTMETRICA tm;
107 SIZE size;
108 INT width;
109
110 if (!hfont)
111 return;
112
113 old_hfont = SelectObject(hdc, hfont);
114
115 GetTextMetricsA(hdc, &tm);
116
117 ok(tm.tmHeight == tm_orig->tmHeight * scale_y, "%ld != %ld\n", tm.tmHeight, tm_orig->tmHeight * scale_y);
118 ok(tm.tmAscent == tm_orig->tmAscent * scale_y, "%ld != %ld\n", tm.tmAscent, tm_orig->tmAscent * scale_y);
119 ok(tm.tmDescent == tm_orig->tmDescent * scale_y, "%ld != %ld\n", tm.tmDescent, tm_orig->tmDescent * scale_y);
120 ok(tm.tmAveCharWidth == tm_orig->tmAveCharWidth * scale_x, "%ld != %ld\n", tm.tmAveCharWidth, tm_orig->tmAveCharWidth * scale_x);
121
122 GetTextExtentPoint32A(hdc, test_str, test_str_len, &size);
123
124 ok(size.cx == size_orig->cx * scale_x, "%ld != %ld\n", size.cx, size_orig->cx * scale_x);
125 ok(size.cy == size_orig->cy * scale_y, "%ld != %ld\n", size.cy, size_orig->cy * scale_y);
126
127 GetCharWidthA(hdc, 'A', 'A', &width);
128
129 ok(width == width_orig * scale_x, "%d != %d\n", width, width_orig * scale_x);
130
131 SelectObject(hdc, old_hfont);
132 }
133
134 /* see whether GDI scales bitmap font metrics */
135 static void test_bitmap_font(void)
136 {
137 static const char test_str[11] = "Test String";
138 HDC hdc;
139 LOGFONTA bitmap_lf;
140 HFONT hfont, old_hfont;
141 TEXTMETRICA tm_orig;
142 SIZE size_orig;
143 INT ret, i, width_orig, height_orig;
144
145 hdc = GetDC(0);
146
147 /* "System" has only 1 pixel size defined, otherwise the test breaks */
148 ret = EnumFontFamiliesA(hdc, "System", font_enum_proc, (LPARAM)&bitmap_lf);
149 if (ret)
150 {
151 ReleaseDC(0, hdc);
152 trace("no bitmap fonts were found, skipping the test\n");
153 return;
154 }
155
156 trace("found bitmap font %s, height %ld\n", bitmap_lf.lfFaceName, bitmap_lf.lfHeight);
157
158 height_orig = bitmap_lf.lfHeight;
159 hfont = create_font("bitmap", &bitmap_lf);
160
161 old_hfont = SelectObject(hdc, hfont);
162 ok(GetTextMetricsA(hdc, &tm_orig), "GetTextMetricsA failed\n");
163 ok(GetTextExtentPoint32A(hdc, test_str, sizeof(test_str), &size_orig), "GetTextExtentPoint32A failed\n");
164 ok(GetCharWidthA(hdc, 'A', 'A', &width_orig), "GetCharWidthA failed\n");
165 SelectObject(hdc, old_hfont);
166 DeleteObject(hfont);
167
168 /* test fractional scaling */
169 for (i = 1; i < height_orig; i++)
170 {
171 hfont = create_font("fractional", &bitmap_lf);
172 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 1, 1);
173 DeleteObject(hfont);
174 }
175
176 /* test integer scaling 3x2 */
177 bitmap_lf.lfHeight = height_orig * 2;
178 bitmap_lf.lfWidth *= 3;
179 hfont = create_font("3x2", &bitmap_lf);
180 todo_wine
181 {
182 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 2);
183 }
184 DeleteObject(hfont);
185
186 /* test integer scaling 3x3 */
187 bitmap_lf.lfHeight = height_orig * 3;
188 bitmap_lf.lfWidth = 0;
189 hfont = create_font("3x3", &bitmap_lf);
190
191 todo_wine
192 {
193 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 3);
194 }
195 DeleteObject(hfont);
196
197 ReleaseDC(0, hdc);
198 }
199
200 static void test_gdi_objects(void)
201 {
202 BYTE buff[256];
203 HDC hdc = GetDC(NULL);
204 HPEN hp;
205 int i;
206 BOOL ret;
207
208 /* SelectObject() with a NULL DC returns 0 and sets ERROR_INVALID_HANDLE.
209 * Note: Under XP at least invalid ptrs can also be passed, not just NULL;
210 * Don't test that here in case it crashes earlier win versions.
211 */
212 SetLastError(0);
213 hp = SelectObject(NULL, GetStockObject(BLACK_PEN));
214 ok(!hp && GetLastError() == ERROR_INVALID_HANDLE,
215 "SelectObject(NULL DC) expected 0, ERROR_INVALID_HANDLE, got %p, 0x%08lx\n",
216 hp, GetLastError());
217
218 /* With a valid DC and a NULL object, the call returns 0 but does not SetLastError() */
219 SetLastError(0);
220 hp = SelectObject(hdc, NULL);
221 ok(!hp && !GetLastError(),
222 "SelectObject(NULL obj) expected 0, NO_ERROR, got %p, 0x%08lx\n",
223 hp, GetLastError());
224
225 /* The DC is unaffected by the NULL SelectObject */
226 SetLastError(0);
227 hp = SelectObject(hdc, GetStockObject(BLACK_PEN));
228 ok(hp && !GetLastError(),
229 "SelectObject(post NULL) expected non-null, NO_ERROR, got %p, 0x%08lx\n",
230 hp, GetLastError());
231
232 /* GetCurrentObject does not SetLastError() on a null object */
233 SetLastError(0);
234 hp = GetCurrentObject(NULL, OBJ_PEN);
235 ok(!hp && !GetLastError(),
236 "GetCurrentObject(NULL DC) expected 0, NO_ERROR, got %p, 0x%08lx\n",
237 hp, GetLastError());
238
239 /* DeleteObject does not SetLastError() on a null object */
240 ret = DeleteObject(NULL);
241 ok( !ret && !GetLastError(),
242 "DeleteObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
243 ret, GetLastError());
244
245 /* GetObject does not SetLastError() on a null object */
246 SetLastError(0);
247 i = GetObjectA(NULL, sizeof(buff), buff);
248 ok (!i && !GetLastError(),
249 "GetObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
250 i, GetLastError());
251
252 /* GetObjectType does SetLastError() on a null object */
253 SetLastError(0);
254 i = GetObjectType(NULL);
255 ok (!i && GetLastError() == ERROR_INVALID_HANDLE,
256 "GetObjectType(NULL obj), expected 0, ERROR_INVALID_HANDLE, got %d, 0x%08lx\n",
257 i, GetLastError());
258
259 /* UnrealizeObject does not SetLastError() on a null object */
260 SetLastError(0);
261 i = UnrealizeObject(NULL);
262 ok (!i && !GetLastError(),
263 "UnrealizeObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
264 i, GetLastError());
265
266 ReleaseDC(NULL, hdc);
267 }
268
269 static void test_GdiGetCharDimensions(void)
270 {
271 HDC hdc;
272 TEXTMETRICW tm;
273 LONG ret;
274 SIZE size;
275 LONG avgwidth, height;
276 static const char szAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
277 typedef LONG (WINAPI *fnGdiGetCharDimensions)(HDC hdc, LPTEXTMETRICW lptm, LONG *height);
278 fnGdiGetCharDimensions GdiGetCharDimensions = (fnGdiGetCharDimensions)GetProcAddress(LoadLibrary("gdi32"), "GdiGetCharDimensions");
279 if (!GdiGetCharDimensions) return;
280
281 hdc = CreateCompatibleDC(NULL);
282
283 GetTextExtentPoint(hdc, szAlphabet, strlen(szAlphabet), &size);
284 avgwidth = ((size.cx / 26) + 1) / 2;
285
286 ret = GdiGetCharDimensions(hdc, &tm, &height);
287 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
288 ok(height == tm.tmHeight, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", tm.tmHeight, height);
289
290 ret = GdiGetCharDimensions(hdc, &tm, NULL);
291 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
292
293 ret = GdiGetCharDimensions(hdc, NULL, NULL);
294 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
295
296 height = 0;
297 ret = GdiGetCharDimensions(hdc, NULL, &height);
298 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
299 ok(height == size.cy, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", size.cy, height);
300
301 DeleteDC(hdc);
302 }
303
304 static void test_text_extents(void)
305 {
306 LOGFONTA lf;
307 TEXTMETRICA tm;
308 HDC hdc;
309 HFONT hfont;
310 SIZE sz;
311
312 memset(&lf, 0, sizeof(lf));
313 strcpy(lf.lfFaceName, "Arial");
314 lf.lfHeight = 20;
315
316 hfont = CreateFontIndirectA(&lf);
317 hdc = GetDC(0);
318 hfont = SelectObject(hdc, hfont);
319 GetTextMetricsA(hdc, &tm);
320 GetTextExtentPointA(hdc, "o", 1, &sz);
321 ok(sz.cy == tm.tmHeight, "cy %ld tmHeight %ld\n", sz.cy, tm.tmHeight);
322
323 SelectObject(hdc, hfont);
324 DeleteObject(hfont);
325 ReleaseDC(NULL, hdc);
326 }
327
328 START_TEST(gdiobj)
329 {
330 test_logfont();
331 test_bitmap_font();
332 test_gdi_objects();
333 test_GdiGetCharDimensions();
334 test_text_extents();
335 }