font.c 11.6 KB
Newer Older
Evan Stade's avatar
Evan Stade committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Unit test suite for fonts
 *
 * Copyright (C) 2007 Google (Evan Stade)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "windows.h"
#include "gdiplus.h"
#include "wine/test.h"

#define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)

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 30 31 32
static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
Evan Stade's avatar
Evan Stade committed
33

34 35 36 37 38 39 40
static const char *debugstr_w(LPCWSTR str)
{
   static char buf[1024];
   WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
   return buf;
}

41 42 43

static void test_createfont(void)
{
44
    GpFontFamily* fontfamily = NULL, *fontfamily2;
45 46 47 48 49
    GpFont* font = NULL;
    GpStatus stat;
    Unit unit;
    UINT i;
    REAL size;
50
    WCHAR familyname[LF_FACESIZE];
51

52
    stat = GdipCreateFontFamilyFromName(nonexistent, NULL, &fontfamily);
53 54 55 56
    expect (FontFamilyNotFound, stat);
    stat = GdipDeleteFont(font);
    expect (InvalidParameter, stat);
    stat = GdipCreateFontFamilyFromName(arial, NULL, &fontfamily);
57 58 59 60 61
    if(stat == FontFamilyNotFound)
    {
        skip("Arial not installed\n");
        return;
    }
62 63 64 65 66 67 68
    expect (Ok, stat);
    stat = GdipCreateFont(fontfamily, 12, FontStyleRegular, UnitPoint, &font);
    expect (Ok, stat);
    stat = GdipGetFontUnit (font, &unit);
    expect (Ok, stat);
    expect (UnitPoint, unit);

69 70 71 72 73 74 75 76 77
    stat = GdipGetFamily(font, &fontfamily2);
    expect(Ok, stat);
    stat = GdipGetFamilyName(fontfamily2, familyname, 0);
    expect(Ok, stat);
    ok (lstrcmpiW(arial, familyname) == 0, "Expected arial, got %s\n",
            debugstr_w(familyname));
    stat = GdipDeleteFontFamily(fontfamily2);
    expect(Ok, stat);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    /* Test to see if returned size is based on unit (its not) */
    GdipGetFontSize(font, &size);
    ok (size == 12, "Expected 12, got %f\n", size);
    GdipDeleteFont(font);

    /* Make sure everything is converted correctly for all Units */
    for (i = UnitWorld; i <=UnitMillimeter; i++)
    {
        if (i == UnitDisplay) continue; /* Crashes WindowsXP, wtf? */
        GdipCreateFont(fontfamily, 24, FontStyleRegular, i, &font);
        GdipGetFontSize (font, &size);
        ok (size == 24, "Expected 24, got %f (with unit: %d)\n", size, i);
        GdipGetFontUnit (font, &unit);
        expect (i, unit);
        GdipDeleteFont(font);
    }
94 95

    GdipDeleteFontFamily(fontfamily);
96 97
}

Evan Stade's avatar
Evan Stade committed
98 99 100 101 102 103 104
static void test_logfont(void)
{
    LOGFONTW lfw, lfw2;
    GpFont *font;
    GpStatus stat;
    GpGraphics *graphics;
    HDC hdc = GetDC(0);
105
    INT style;
Evan Stade's avatar
Evan Stade committed
106 107 108 109

    GdipCreateFromHDC(hdc, &graphics);
    memset(&lfw, 0, sizeof(LOGFONTW));
    memset(&lfw2, 0xff, sizeof(LOGFONTW));
110 111 112 113 114 115 116

    /* empty FaceName */
    lfw.lfFaceName[0] = 0;
    stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);

    expect(NotTrueTypeFont, stat);

Evan Stade's avatar
Evan Stade committed
117 118 119
    memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));

    stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
120 121 122 123 124
    if (stat == FileNotFound)
    {
        skip("Arial not installed.\n");
        return;
    }
Evan Stade's avatar
Evan Stade committed
125 126 127 128
    expect(Ok, stat);
    stat = GdipGetLogFontW(font, graphics, &lfw2);
    expect(Ok, stat);

129
    ok(lfw2.lfHeight < 0, "Expected negative height\n");
Evan Stade's avatar
Evan Stade committed
130 131 132
    expect(0, lfw2.lfWidth);
    expect(0, lfw2.lfEscapement);
    expect(0, lfw2.lfOrientation);
133
    ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
Evan Stade's avatar
Evan Stade committed
134 135 136 137 138 139 140 141 142
    expect(0, lfw2.lfItalic);
    expect(0, lfw2.lfUnderline);
    expect(0, lfw2.lfStrikeOut);
    expect(0, lfw2.lfCharSet);
    expect(0, lfw2.lfOutPrecision);
    expect(0, lfw2.lfClipPrecision);
    expect(0, lfw2.lfQuality);
    expect(0, lfw2.lfPitchAndFamily);

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    GdipDeleteFont(font);

    memset(&lfw, 0, sizeof(LOGFONTW));
    lfw.lfHeight = 25;
    lfw.lfWidth = 25;
    lfw.lfEscapement = lfw.lfOrientation = 50;
    lfw.lfItalic = lfw.lfUnderline = lfw.lfStrikeOut = TRUE;

    memset(&lfw2, 0xff, sizeof(LOGFONTW));
    memcpy(&lfw.lfFaceName, arial, 6 * sizeof(WCHAR));

    stat = GdipCreateFontFromLogfontW(hdc, &lfw, &font);
    expect(Ok, stat);
    stat = GdipGetLogFontW(font, graphics, &lfw2);
    expect(Ok, stat);

    ok(lfw2.lfHeight < 0, "Expected negative height\n");
    expect(0, lfw2.lfWidth);
    expect(0, lfw2.lfEscapement);
    expect(0, lfw2.lfOrientation);
    ok((lfw2.lfWeight >= 100) && (lfw2.lfWeight <= 900), "Expected weight to be set\n");
164 165 166
    expect(TRUE, lfw2.lfItalic);
    expect(TRUE, lfw2.lfUnderline);
    expect(TRUE, lfw2.lfStrikeOut);
167 168 169 170 171 172
    expect(0, lfw2.lfCharSet);
    expect(0, lfw2.lfOutPrecision);
    expect(0, lfw2.lfClipPrecision);
    expect(0, lfw2.lfQuality);
    expect(0, lfw2.lfPitchAndFamily);

173 174 175 176 177
    stat = GdipGetFontStyle(font, &style);
    expect(Ok, stat);
    ok (style == (FontStyleItalic | FontStyleUnderline | FontStyleStrikeout),
            "Expected , got %d\n", style);

178 179
    GdipDeleteFont(font);

Evan Stade's avatar
Evan Stade committed
180 181 182 183
    GdipDeleteGraphics(graphics);
    ReleaseDC(0, hdc);
}

184 185
static void test_fontfamily (void)
{
186
    GpFontFamily *family, *clonedFontFamily;
187 188 189
    WCHAR itsName[LF_FACESIZE];
    GpStatus stat;

190
    /* FontFamily cannot be NULL */
191
    stat = GdipCreateFontFamilyFromName (arial , NULL, NULL);
192 193 194
    expect (InvalidParameter, stat);

    /* FontFamily must be able to actually find the family.
195
     * If it can't, any subsequent calls should fail.
196
     */
197
    stat = GdipCreateFontFamilyFromName (nonexistent, NULL, &family);
198 199
    expect (FontFamilyNotFound, stat);

200 201 202 203 204 205 206
    /* Bitmap fonts are not found */
todo_wine
{
    stat = GdipCreateFontFamilyFromName (MSSansSerif, NULL, &family);
    expect (FontFamilyNotFound, stat);
}

207
    stat = GdipCreateFontFamilyFromName (arial, NULL, &family);
208 209 210 211 212
    if(stat == FontFamilyNotFound)
    {
        skip("Arial not installed\n");
        return;
    }
213 214
    expect (Ok, stat);

215
    stat = GdipGetFamilyName (family, itsName, LANG_NEUTRAL);
216
    expect (Ok, stat);
217
    expect (0, lstrcmpiW(itsName, arial));
218 219 220 221

    if (0)
    {
        /* Crashes on Windows XP SP2, Vista, and so Wine as well */
222
        stat = GdipGetFamilyName (family, NULL, LANG_NEUTRAL);
223 224 225
        expect (Ok, stat);
    }

226 227 228 229
    /* Make sure we don't read old data */
    ZeroMemory (itsName, sizeof(itsName));
    stat = GdipCloneFontFamily(family, &clonedFontFamily);
    expect (Ok, stat);
230
    GdipDeleteFontFamily(family);
231 232 233 234 235
    stat = GdipGetFamilyName(clonedFontFamily, itsName, LANG_NEUTRAL);
    expect(Ok, stat);
    expect(0, lstrcmpiW(itsName, arial));

    GdipDeleteFontFamily(clonedFontFamily);
236 237
}

238 239 240 241 242 243
static void test_fontfamily_properties (void)
{
    GpFontFamily* FontFamily = NULL;
    GpStatus stat;
    UINT16 result = 0;

244 245 246 247 248
    stat = GdipCreateFontFamilyFromName(arial, NULL, &FontFamily);
    if(stat == FontFamilyNotFound)
        skip("Arial not installed\n");
    else
    {
249 250
todo_wine
{
251 252 253
        stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok (result == 2355, "Expected 2355, got %d\n", result);
254
}
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        result = 0;
        stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok(result == 2048, "Expected 2048, got %d\n", result);
        result = 0;
        stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok(result == 1854, "Expected 1854, got %d\n", result);
        result = 0;
        stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
        ok(result == 434, "Expected 434, got %d\n", result);
        GdipDeleteFontFamily(FontFamily);
    }

    stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, &FontFamily);
    if(stat == FontFamilyNotFound)
        skip("Times New Roman not installed\n");
    else
    {
        result = 0;
275 276
todo_wine
{
277 278 279
        stat = GdipGetLineSpacing(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok(result == 2355, "Expected 2355, got %d\n", result);
280
}
281 282 283 284 285 286 287 288 289 290 291 292 293
        result = 0;
        stat = GdipGetEmHeight(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok(result == 2048, "Expected 2048, got %d\n", result);
        result = 0;
        stat = GdipGetCellAscent(FontFamily, FontStyleRegular, &result);
        expect(Ok, stat);
        ok(result == 1825, "Expected 1825, got %d\n", result);
        result = 0;
        stat = GdipGetCellDescent(FontFamily, FontStyleRegular, &result);
        ok(result == 443, "Expected 443 got %d\n", result);
        GdipDeleteFontFamily(FontFamily);
    }
294
}
295

296 297 298
static void test_getgenerics (void)
{
    GpStatus stat;
299
    GpFontFamily* family;
300 301 302
    WCHAR familyName[LF_FACESIZE];
    ZeroMemory(familyName, sizeof(familyName)/sizeof(WCHAR));

303
    stat = GdipGetGenericFontFamilySansSerif (&family);
304 305 306 307 308
    if (stat == FontFamilyNotFound)
    {
        skip("Microsoft Sans Serif not installed\n");
        goto serif;
    }
309
    expect (Ok, stat);
310
    stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
311 312 313 314 315
    expect (Ok, stat);
    ok ((lstrcmpiW(familyName, MicrosoftSansSerif) == 0) ||
        (lstrcmpiW(familyName,MSSansSerif) == 0),
        "Expected Microsoft Sans Serif or MS Sans Serif, got %s\n",
        debugstr_w(familyName));
316
    stat = GdipDeleteFontFamily (family);
317 318
    expect (Ok, stat);

319
serif:
320
    stat = GdipGetGenericFontFamilySerif (&family);
321 322 323 324 325
    if (stat == FontFamilyNotFound)
    {
        skip("Times New Roman not installed\n");
        goto monospace;
    }
326
    expect (Ok, stat);
327
    stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
328 329 330
    expect (Ok, stat);
    ok (lstrcmpiW(familyName, TimesNewRoman) == 0,
        "Expected Times New Roman, got %s\n", debugstr_w(familyName));
331
    stat = GdipDeleteFontFamily (family);
332 333
    expect (Ok, stat);

334
monospace:
335
    stat = GdipGetGenericFontFamilyMonospace (&family);
336 337 338 339 340
    if (stat == FontFamilyNotFound)
    {
        skip("Courier New not installed\n");
        return;
    }
341
    expect (Ok, stat);
342
    stat = GdipGetFamilyName (family, familyName, LANG_NEUTRAL);
343 344 345
    expect (Ok, stat);
    ok (lstrcmpiW(familyName, CourierNew) == 0,
        "Expected Courier New, got %s\n", debugstr_w(familyName));
346
    stat = GdipDeleteFontFamily (family);
347 348 349
    expect (Ok, stat);
}

Evan Stade's avatar
Evan Stade committed
350 351 352 353 354 355 356 357 358 359 360 361
START_TEST(font)
{
    struct GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;

    gdiplusStartupInput.GdiplusVersion              = 1;
    gdiplusStartupInput.DebugEventCallback          = NULL;
    gdiplusStartupInput.SuppressBackgroundThread    = 0;
    gdiplusStartupInput.SuppressExternalCodecs      = 0;

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

362
    test_createfont();
Evan Stade's avatar
Evan Stade committed
363
    test_logfont();
364
    test_fontfamily();
365
    test_fontfamily_properties();
366
    test_getgenerics();
Evan Stade's avatar
Evan Stade committed
367 368 369

    GdiplusShutdown(gdiplusToken);
}