2 * Unit test suite for fonts
4 * Copyright 2002 Mike McCormack
5 * Copyright 2004 Dmitry Timoshkov
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
32 static void check_font(const char* test, const LOGFONTA* lf, HFONT hfont)
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)
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);
52 static HFONT create_font(const char* test, const LOGFONTA* lf)
54 HFONT hfont = CreateFontIndirectA(lf);
55 ok(hfont != 0, "%s: CreateFontIndirect failed\n", test);
57 check_font(test, lf, hfont);
61 static void test_logfont(void)
66 memset(&lf, 0, sizeof lf);
68 lf.lfCharSet = ANSI_CHARSET;
69 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
70 lf.lfWeight = FW_DONTCARE;
73 lf.lfQuality = DEFAULT_QUALITY;
75 lstrcpyA(lf.lfFaceName, "Arial");
76 hfont = create_font("Arial", &lf);
79 memset(&lf, 'A', sizeof(lf));
80 hfont = CreateFontIndirectA(&lf);
81 ok(hfont != 0, "CreateFontIndirectA with strange LOGFONT failed\n");
83 lf.lfFaceName[LF_FACESIZE - 1] = 0;
84 check_font("AAA...", &lf, hfont);
88 static INT CALLBACK font_enum_proc(const LOGFONT *elf, const TEXTMETRIC *ntm, DWORD type, LPARAM lParam)
90 if (type & RASTER_FONTTYPE)
92 LOGFONT *lf = (LOGFONT *)lParam;
94 return 0; /* stop enumeration */
97 return 1; /* continue enumeration */
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)
113 old_hfont = SelectObject(hdc, hfont);
115 GetTextMetricsA(hdc, &tm);
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);
122 GetTextExtentPoint32A(hdc, test_str, test_str_len, &size);
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);
127 GetCharWidthA(hdc, 'A', 'A', &width);
129 ok(width == width_orig * scale_x, "%d != %d\n", width, width_orig * scale_x);
131 SelectObject(hdc, old_hfont);
134 /* see whether GDI scales bitmap font metrics */
135 static void test_bitmap_font(void)
137 static const char test_str[11] = "Test String";
140 HFONT hfont, old_hfont;
143 INT ret, i, width_orig, height_orig;
147 /* "System" has only 1 pixel size defined, otherwise the test breaks */
148 ret = EnumFontFamiliesA(hdc, "System", font_enum_proc, (LPARAM)&bitmap_lf);
152 trace("no bitmap fonts were found, skipping the test\n");
156 trace("found bitmap font %s, height %ld\n", bitmap_lf.lfFaceName, bitmap_lf.lfHeight);
158 height_orig = bitmap_lf.lfHeight;
159 hfont = create_font("bitmap", &bitmap_lf);
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);
168 /* test fractional scaling */
169 for (i = 1; i < height_orig; i++)
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);
176 /* test integer scaling 3x2 */
177 bitmap_lf.lfHeight = height_orig * 2;
178 bitmap_lf.lfWidth *= 3;
179 hfont = create_font("3x2", &bitmap_lf);
182 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 2);
186 /* test integer scaling 3x3 */
187 bitmap_lf.lfHeight = height_orig * 3;
188 bitmap_lf.lfWidth = 0;
189 hfont = create_font("3x3", &bitmap_lf);
193 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 3);
200 static INT CALLBACK find_font_proc(const LOGFONT *elf, const TEXTMETRIC *ntm, DWORD type, LPARAM lParam)
202 LOGFONT *lf = (LOGFONT *)lParam;
204 if (elf->lfHeight == lf->lfHeight && !strcmp(elf->lfFaceName, lf->lfFaceName))
207 return 0; /* stop enumeration */
209 return 1; /* continue enumeration */
212 static void test_bitmap_font_metrics(void)
214 static const struct font_data
216 const char face_name[LF_FACESIZE];
217 int weight, height, ascent, descent, int_leading, ext_leading;
218 int ave_char_width, max_char_width;
221 { "MS Sans Serif", FW_NORMAL, 13, 11, 2, 2, 0, 5, 11 },
222 { "MS Sans Serif", FW_NORMAL, 16, 13, 3, 3, 0, 7, 14 },
223 { "MS Sans Serif", FW_NORMAL, 20, 16, 4, 4, 0, 8, 16 },
224 { "MS Sans Serif", FW_NORMAL, 24, 19, 5, 6, 0, 9, 20 },
225 { "MS Sans Serif", FW_NORMAL, 29, 23, 6, 5, 0, 12, 25 },
226 { "MS Sans Serif", FW_NORMAL, 37, 29, 8, 5, 0, 16, 32 },
227 { "MS Serif", FW_NORMAL, 10, 8, 2, 2, 0, 5, 8 },
228 { "MS Serif", FW_NORMAL, 11, 9, 2, 2, 0, 5, 9 },
229 { "MS Serif", FW_NORMAL, 13, 11, 2, 2, 0, 5, 12 },
230 { "MS Serif", FW_NORMAL, 16, 13, 3, 3, 0, 6, 16 },
231 { "MS Serif", FW_NORMAL, 19, 15, 4, 3, 0, 8, 19 },
232 { "MS Serif", FW_NORMAL, 21, 16, 5, 3, 0, 9, 23 },
233 { "MS Serif", FW_NORMAL, 27, 21, 6, 3, 0, 12, 27 },
234 { "MS Serif", FW_NORMAL, 35, 27, 8, 3, 0, 16, 34 },
235 { "Courier", FW_NORMAL, 13, 11, 2, 0, 0, 8, 8 },
236 { "Courier", FW_NORMAL, 16, 13, 3, 0, 0, 9, 9 },
237 { "Courier", FW_NORMAL, 20, 16, 4, 0, 0, 12, 12 },
238 { "System", FW_BOLD, 16, 13, 3, 3, 0, 7, 15 },
239 { "Small Fonts", FW_NORMAL, 3, 2, 1, 0, 0, 1, 2 },
240 { "Small Fonts", FW_NORMAL, 5, 4, 1, 1, 0, 3, 4 },
241 { "Small Fonts", FW_NORMAL, 6, 5, 1, 1, 0, 3, 13 },
242 { "Small Fonts", FW_NORMAL, 8, 7, 1, 1, 0, 4, 7 },
243 { "Small Fonts", FW_NORMAL, 10, 8, 2, 2, 0, 4, 8 },
244 { "Small Fonts", FW_NORMAL, 11, 9, 2, 2, 0, 5, 9 }
245 /* FIXME: add "Fixedsys", "Terminal" */
249 HFONT hfont, old_hfont;
253 hdc = CreateCompatibleDC(0);
256 for (i = 0; i < sizeof(fd)/sizeof(fd[0]); i++)
258 memset(&lf, 0, sizeof(lf));
260 lf.lfHeight = fd[i].height;
261 strcpy(lf.lfFaceName, fd[i].face_name);
262 ret = EnumFontFamilies(hdc, fd[i].face_name, find_font_proc, (LPARAM)&lf);
265 trace("font %s height %d not found\n", fd[i].face_name, fd[i].height);
269 trace("found font %s, height %ld\n", lf.lfFaceName, lf.lfHeight);
271 hfont = create_font(lf.lfFaceName, &lf);
272 old_hfont = SelectObject(hdc, hfont);
273 ok(GetTextMetrics(hdc, &tm), "GetTextMetrics error %ld\n", GetLastError());
275 ok(tm.tmWeight == fd[i].weight, "%s(%d): tm.tmWeight %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmWeight, fd[i].weight);
276 ok(tm.tmHeight == fd[i].height, "%s(%d): tm.tmHeight %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmHeight, fd[i].height);
277 ok(tm.tmAscent == fd[i].ascent, "%s(%d): tm.tmAscent %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmAscent, fd[i].ascent);
278 ok(tm.tmDescent == fd[i].descent, "%s(%d): tm.tmDescent %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmDescent, fd[i].descent);
279 ok(tm.tmInternalLeading == fd[i].int_leading, "%s(%d): tm.tmInternalLeading %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmInternalLeading, fd[i].int_leading);
280 ok(tm.tmExternalLeading == fd[i].ext_leading, "%s(%d): tm.tmExternalLeading %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmExternalLeading, fd[i].ext_leading);
281 ok(tm.tmAveCharWidth == fd[i].ave_char_width, "%s(%d): tm.tmAveCharWidth %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmAveCharWidth, fd[i].ave_char_width);
282 ok(tm.tmMaxCharWidth == fd[i].max_char_width, "%s(%d): tm.tmMaxCharWidth %ld != %d\n", fd[i].face_name, fd[i].height, tm.tmMaxCharWidth, fd[i].max_char_width);
284 SelectObject(hdc, old_hfont);
291 static void test_GdiGetCharDimensions(void)
297 LONG avgwidth, height;
298 static const char szAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
299 typedef LONG (WINAPI *fnGdiGetCharDimensions)(HDC hdc, LPTEXTMETRICW lptm, LONG *height);
300 fnGdiGetCharDimensions GdiGetCharDimensions = (fnGdiGetCharDimensions)GetProcAddress(LoadLibrary("gdi32"), "GdiGetCharDimensions");
301 if (!GdiGetCharDimensions) return;
303 hdc = CreateCompatibleDC(NULL);
305 GetTextExtentPoint(hdc, szAlphabet, strlen(szAlphabet), &size);
306 avgwidth = ((size.cx / 26) + 1) / 2;
308 ret = GdiGetCharDimensions(hdc, &tm, &height);
309 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
310 ok(height == tm.tmHeight, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", tm.tmHeight, height);
312 ret = GdiGetCharDimensions(hdc, &tm, NULL);
313 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
315 ret = GdiGetCharDimensions(hdc, NULL, NULL);
316 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
319 ret = GdiGetCharDimensions(hdc, NULL, &height);
320 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
321 ok(height == size.cy, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", size.cy, height);
326 static void test_GetCharABCWidthsW(void)
330 typedef BOOL (WINAPI *fnGetCharABCWidthsW)(HDC hdc, UINT first, UINT last, LPABC abc);
331 fnGetCharABCWidthsW GetCharABCWidthsW = (fnGetCharABCWidthsW)GetProcAddress(LoadLibrary("gdi32"), "GetCharABCWidthsW");
332 if (!GetCharABCWidthsW) return;
334 ret = GetCharABCWidthsW(NULL, 'a', 'a', abc);
335 ok(!ret, "GetCharABCWidthsW should have returned FALSE\n");
338 static void test_text_extents(void)
346 memset(&lf, 0, sizeof(lf));
347 strcpy(lf.lfFaceName, "Arial");
350 hfont = CreateFontIndirectA(&lf);
352 hfont = SelectObject(hdc, hfont);
353 GetTextMetricsA(hdc, &tm);
354 GetTextExtentPointA(hdc, "o", 1, &sz);
355 ok(sz.cy == tm.tmHeight, "cy %ld tmHeight %ld\n", sz.cy, tm.tmHeight);
357 SelectObject(hdc, hfont);
359 ReleaseDC(NULL, hdc);
366 test_bitmap_font_metrics();
367 test_GdiGetCharDimensions();
368 test_GetCharABCWidthsW();