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