font.c 315 KB
Newer Older
1 2 3
/*
 *    Font related tests
 *
4
 * Copyright 2012, 2014-2017 Nikolay Sivov for CodeWeavers
5
 * Copyright 2014 Aric Stewart for CodeWeavers
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
 */

22
#include <math.h>
23
#include <limits.h>
24

25 26 27
#define COBJMACROS

#include "windows.h"
28
#include "winternl.h"
29
#include "dwrite_3.h"
30 31
#include "initguid.h"
#include "d2d1.h"
32 33 34

#include "wine/test.h"

35 36 37 38 39 40 41 42
#define MS_CMAP_TAG DWRITE_MAKE_OPENTYPE_TAG('c','m','a','p')
#define MS_VDMX_TAG DWRITE_MAKE_OPENTYPE_TAG('V','D','M','X')
#define MS_GASP_TAG DWRITE_MAKE_OPENTYPE_TAG('g','a','s','p')
#define MS_CPAL_TAG DWRITE_MAKE_OPENTYPE_TAG('C','P','A','L')
#define MS_0S2_TAG  DWRITE_MAKE_OPENTYPE_TAG('O','S','/','2')
#define MS_HEAD_TAG DWRITE_MAKE_OPENTYPE_TAG('h','e','a','d')
#define MS_HHEA_TAG DWRITE_MAKE_OPENTYPE_TAG('h','h','e','a')
#define MS_POST_TAG DWRITE_MAKE_OPENTYPE_TAG('p','o','s','t')
43
#define MS_GSUB_TAG DWRITE_MAKE_OPENTYPE_TAG('G','S','U','B')
44
#define MS_KERN_TAG DWRITE_MAKE_OPENTYPE_TAG('k','e','r','n')
45 46
#define MS_GLYF_TAG DWRITE_MAKE_OPENTYPE_TAG('g','l','y','f')
#define MS_CFF__TAG DWRITE_MAKE_OPENTYPE_TAG('C','F','F',' ')
47
#define MS_CFF2_TAG DWRITE_MAKE_OPENTYPE_TAG('C','F','F','2')
48
#define MS_COLR_TAG DWRITE_MAKE_OPENTYPE_TAG('C','O','L','R')
49
#define MS_SVG__TAG DWRITE_MAKE_OPENTYPE_TAG('S','V','G',' ')
50 51
#define MS_SBIX_TAG DWRITE_MAKE_OPENTYPE_TAG('s','b','i','x')
#define MS_MAXP_TAG DWRITE_MAKE_OPENTYPE_TAG('m','a','x','p')
52
#define MS_CBLC_TAG DWRITE_MAKE_OPENTYPE_TAG('C','B','L','C')
53 54 55 56 57

/* 'sbix' formats */
#define MS_PNG__TAG DWRITE_MAKE_OPENTYPE_TAG('p','n','g',' ')
#define MS_JPG__TAG DWRITE_MAKE_OPENTYPE_TAG('j','p','g',' ')
#define MS_TIFF_TAG DWRITE_MAKE_OPENTYPE_TAG('t','i','f','f')
58

59 60 61
#define MS_WOFF_TAG DWRITE_MAKE_OPENTYPE_TAG('w','O','F','F')
#define MS_WOF2_TAG DWRITE_MAKE_OPENTYPE_TAG('w','O','F','2')

62 63
#ifdef WORDS_BIGENDIAN
#define GET_BE_WORD(x) (x)
64
#define GET_BE_DWORD(x) (x)
65 66
#define GET_LE_WORD(x) RtlUshortByteSwap(x)
#define GET_LE_DWORD(x) RtlUlongByteSwap(x)
67 68
#else
#define GET_BE_WORD(x) RtlUshortByteSwap(x)
69
#define GET_BE_DWORD(x) RtlUlongByteSwap(x)
70 71
#define GET_LE_WORD(x) (x)
#define GET_LE_DWORD(x) (x)
72
#endif
73

74 75 76
#define EXPECT_HR(hr,hr_exp) \
    ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#define DEFINE_EXPECT(func) \
    static BOOL expect_ ## func = FALSE, called_ ## func = FALSE

#define SET_EXPECT(func) \
    do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)

#define CHECK_EXPECT2(func) \
    do { \
        ok(expect_ ##func, "unexpected call " #func "\n"); \
        called_ ## func = TRUE; \
    }while(0)

#define CHECK_EXPECT(func) \
    do { \
        CHECK_EXPECT2(func); \
        expect_ ## func = FALSE; \
    }while(0)

#define CHECK_CALLED(func) \
    do { \
        ok(called_ ## func, "expected " #func "\n"); \
        expect_ ## func = called_ ## func = FALSE; \
    }while(0)

#define CLEAR_CALLED(func) \
    expect_ ## func = called_ ## func = FALSE

DEFINE_EXPECT(setfillmode);

106 107 108
#define EXPECT_REF(obj,ref) _expect_ref((IUnknown*)obj, ref, __LINE__)
static void _expect_ref(IUnknown* obj, ULONG ref, int line)
{
109 110 111 112
    ULONG rc;
    IUnknown_AddRef(obj);
    rc = IUnknown_Release(obj);
    ok_(__FILE__,line)(rc == ref, "expected refcount %d, got %d\n", ref, rc);
113 114
}

115 116 117 118 119 120 121 122 123
#define EXPECT_REF_BROKEN(obj,ref,brokenref) _expect_ref_broken((IUnknown*)obj, ref, brokenref, __LINE__)
static void _expect_ref_broken(IUnknown* obj, ULONG ref, ULONG brokenref, int line)
{
    ULONG rc;
    IUnknown_AddRef(obj);
    rc = IUnknown_Release(obj);
    ok_(__FILE__,line)(rc == ref || broken(rc == brokenref), "expected refcount %d, got %d\n", ref, rc);
}

124
static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t size)
125
{
126
    return HeapAlloc(GetProcessHeap(), 0, size);
127 128 129 130 131 132 133
}

static inline BOOL heap_free(void *mem)
{
    return HeapFree(GetProcessHeap(), 0, mem);
}

134
static const WCHAR test_fontfile[] = {'w','i','n','e','_','t','e','s','t','_','f','o','n','t','.','t','t','f',0};
135
static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
136
static const WCHAR arialW[] = {'A','r','i','a','l',0};
137 138
static const WCHAR tahomaUppercaseW[] = {'T','A','H','O','M','A',0};
static const WCHAR tahomaStrangecaseW[] = {'t','A','h','O','m','A',0};
139
static const WCHAR blahW[]  = {'B','l','a','h','!',0};
140
static const WCHAR emojiW[] = {'S','e','g','o','e',' ','U','I',' ','E','m','o','j','i',0};
141

142 143 144 145
/* PANOSE is 10 bytes in size, need to pack the structure properly */
#include "pshpack2.h"
typedef struct
{
146 147
    USHORT majorVersion;
    USHORT minorVersion;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    ULONG revision;
    ULONG checksumadj;
    ULONG magic;
    USHORT flags;
    USHORT unitsPerEm;
    ULONGLONG created;
    ULONGLONG modified;
    SHORT xMin;
    SHORT yMin;
    SHORT xMax;
    SHORT yMax;
    USHORT macStyle;
    USHORT lowestRecPPEM;
    SHORT direction_hint;
    SHORT index_format;
    SHORT glyphdata_format;
} TT_HEAD;

166 167 168 169 170 171 172 173 174 175 176
enum TT_HEAD_MACSTYLE
{
    TT_HEAD_MACSTYLE_BOLD      = 1 << 0,
    TT_HEAD_MACSTYLE_ITALIC    = 1 << 1,
    TT_HEAD_MACSTYLE_UNDERLINE = 1 << 2,
    TT_HEAD_MACSTYLE_OUTLINE   = 1 << 3,
    TT_HEAD_MACSTYLE_SHADOW    = 1 << 4,
    TT_HEAD_MACSTYLE_CONDENSED = 1 << 5,
    TT_HEAD_MACSTYLE_EXTENDED  = 1 << 6,
};

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
typedef struct
{
    USHORT version;
    SHORT xAvgCharWidth;
    USHORT usWeightClass;
    USHORT usWidthClass;
    SHORT fsType;
    SHORT ySubscriptXSize;
    SHORT ySubscriptYSize;
    SHORT ySubscriptXOffset;
    SHORT ySubscriptYOffset;
    SHORT ySuperscriptXSize;
    SHORT ySuperscriptYSize;
    SHORT ySuperscriptXOffset;
    SHORT ySuperscriptYOffset;
    SHORT yStrikeoutSize;
    SHORT yStrikeoutPosition;
    SHORT sFamilyClass;
    PANOSE panose;
    ULONG ulUnicodeRange1;
    ULONG ulUnicodeRange2;
    ULONG ulUnicodeRange3;
    ULONG ulUnicodeRange4;
    CHAR achVendID[4];
    USHORT fsSelection;
    USHORT usFirstCharIndex;
    USHORT usLastCharIndex;
    /* According to the Apple spec, original version didn't have the below fields,
     * version numbers were taken from the OpenType spec.
     */
    /* version 0 (TrueType 1.5) */
    USHORT sTypoAscender;
    USHORT sTypoDescender;
    USHORT sTypoLineGap;
    USHORT usWinAscent;
    USHORT usWinDescent;
    /* version 1 (TrueType 1.66) */
    ULONG ulCodePageRange1;
    ULONG ulCodePageRange2;
    /* version 2 (OpenType 1.2) */
    SHORT sxHeight;
    SHORT sCapHeight;
    USHORT usDefaultChar;
    USHORT usBreakChar;
    USHORT usMaxContext;
} TT_OS2_V2;

224 225 226 227 228 229 230 231 232 233 234 235 236
enum OS2_FSSELECTION {
    OS2_FSSELECTION_ITALIC           = 1 << 0,
    OS2_FSSELECTION_UNDERSCORE       = 1 << 1,
    OS2_FSSELECTION_NEGATIVE         = 1 << 2,
    OS2_FSSELECTION_OUTLINED         = 1 << 3,
    OS2_FSSELECTION_STRIKEOUT        = 1 << 4,
    OS2_FSSELECTION_BOLD             = 1 << 5,
    OS2_FSSELECTION_REGULAR          = 1 << 6,
    OS2_FSSELECTION_USE_TYPO_METRICS = 1 << 7,
    OS2_FSSELECTION_WWS              = 1 << 8,
    OS2_FSSELECTION_OBLIQUE          = 1 << 9
};

237 238 239 240 241 242 243 244 245 246 247 248
typedef struct {
    ULONG Version;
    ULONG italicAngle;
    SHORT underlinePosition;
    SHORT underlineThickness;
    ULONG fixed_pitch;
    ULONG minmemType42;
    ULONG maxmemType42;
    ULONG minmemType1;
    ULONG maxmemType1;
} TT_POST;

249
typedef struct {
250 251
    USHORT majorVersion;
    USHORT minorVersion;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    SHORT  ascender;
    SHORT  descender;
    SHORT  linegap;
    USHORT advanceWidthMax;
    SHORT  minLeftSideBearing;
    SHORT  minRightSideBearing;
    SHORT  xMaxExtent;
    SHORT  caretSlopeRise;
    SHORT  caretSlopeRun;
    SHORT  caretOffset;
    SHORT  reserved[4];
    SHORT  metricDataFormat;
    USHORT numberOfHMetrics;
} TT_HHEA;

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
typedef struct {
    DWORD version;
    WORD ScriptList;
    WORD FeatureList;
    WORD LookupList;
} GSUB_Header;

typedef struct {
    CHAR FeatureTag[4];
    WORD Feature;
} OT_FeatureRecord;

typedef struct {
    WORD FeatureCount;
    OT_FeatureRecord FeatureRecord[1];
} OT_FeatureList;

typedef struct {
    WORD FeatureParams;
    WORD LookupCount;
    WORD LookupListIndex[1];
} OT_Feature;

typedef struct {
    WORD LookupCount;
    WORD Lookup[1];
} OT_LookupList;

typedef struct {
    WORD LookupType;
    WORD LookupFlag;
    WORD SubTableCount;
    WORD SubTable[1];
} OT_LookupTable;

typedef struct {
    WORD SubstFormat;
    WORD Coverage;
    WORD DeltaGlyphID;
} GSUB_SingleSubstFormat1;

typedef struct {
    WORD SubstFormat;
    WORD Coverage;
    WORD GlyphCount;
    WORD Substitute[1];
} GSUB_SingleSubstFormat2;

typedef struct {
    WORD SubstFormat;
    WORD ExtensionLookupType;
    DWORD ExtensionOffset;
} GSUB_ExtensionPosFormat1;

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
typedef struct {
    WORD version;
    WORD flags;
    DWORD numStrikes;
    DWORD strikeOffset[1];
} sbix_header;

typedef struct {
    WORD ppem;
    WORD ppi;
    DWORD glyphDataOffsets[1];
} sbix_strike;

typedef struct {
    WORD originOffsetX;
    WORD originOffsetY;
    DWORD graphicType;
    BYTE data[1];
} sbix_glyph_data;

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
typedef struct {
    WORD majorVersion;
    WORD minorVersion;
    DWORD numSizes;
} CBLCHeader;

typedef struct {
    BYTE res[12];
} sbitLineMetrics;

typedef struct {
    DWORD indexSubTableArrayOffset;
    DWORD indexTablesSize;
    DWORD numberofIndexSubTables;
    DWORD colorRef;
    sbitLineMetrics hori;
    sbitLineMetrics vert;
    WORD startGlyphIndex;
    WORD endGlyphIndex;
    BYTE ppemX;
    BYTE ppemY;
    BYTE bitDepth;
    BYTE flags;
} CBLCBitmapSizeTable;

366 367 368 369 370
typedef struct {
    DWORD version;
    WORD numGlyphs;
} maxp;

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
struct WOFFHeader
{
    ULONG  signature;
    ULONG  flavor;
    ULONG  length;
    USHORT numTables;
    USHORT reserved;
    ULONG  totalSfntSize;
    USHORT majorVersion;
    USHORT minorVersion;
    ULONG  metaOffset;
    ULONG  metaLength;
    ULONG  metaOrigLength;
    ULONG  privOffset;
    ULONG  privLength;
};

struct WOFFHeader2
{
    ULONG  signature;
    ULONG  flavor;
    ULONG  length;
    USHORT numTables;
    USHORT reserved;
    ULONG  totalSfntSize;
    ULONG  totalCompressedSize;
    USHORT majorVersion;
    USHORT minorVersion;
    ULONG  metaOffset;
    ULONG  metaLength;
    ULONG  metaOrigLength;
    ULONG  privOffset;
    ULONG  privLength;
};

406 407
#include "poppack.h"

408 409 410 411 412 413 414
static void *create_factory_iid(REFIID riid)
{
    IUnknown *factory = NULL;
    DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, riid, &factory);
    return factory;
}

415 416
static IDWriteFactory *create_factory(void)
{
417 418
    IDWriteFactory *factory = create_factory_iid(&IID_IDWriteFactory);
    ok(factory != NULL, "Failed to create factory.\n");
419 420 421
    return factory;
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static IDWriteFontFace *create_fontface(IDWriteFactory *factory)
{
    static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
    IDWriteGdiInterop *interop;
    IDWriteFontFace *fontface;
    IDWriteFont *font;
    LOGFONTW logfont;
    HRESULT hr;

    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFont_Release(font);
    IDWriteGdiInterop_Release(interop);

    return fontface;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
static IDWriteFont *get_font(IDWriteFactory *factory, const WCHAR *name, DWRITE_FONT_STYLE style)
{
    IDWriteFontCollection *collection;
    IDWriteFontFamily *family;
    IDWriteFont *font = NULL;
    UINT32 index;
    BOOL exists;
    HRESULT hr;

    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    index = ~0;
    exists = FALSE;
    hr = IDWriteFontCollection_FindFamilyName(collection, name, &index, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    if (!exists) goto not_found;

    hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, style, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFontFamily_Release(family);
not_found:
    IDWriteFontCollection_Release(collection);
    return font;
}

static IDWriteFont *get_tahoma_instance(IDWriteFactory *factory, DWRITE_FONT_STYLE style)
{
    IDWriteFont *font = get_font(factory, tahomaW, style);
    ok(font != NULL, "failed to get Tahoma\n");
    return font;
}

491
static WCHAR *create_testfontfile(const WCHAR *filename)
492
{
493
    static WCHAR pathW[MAX_PATH];
494 495 496 497
    DWORD written;
    HANDLE file;
    HRSRC res;
    void *ptr;
498 499 500 501 502 503 504

    GetTempPathW(sizeof(pathW)/sizeof(WCHAR), pathW);
    lstrcatW(pathW, filename);

    file = CreateFileW(pathW, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
    ok(file != INVALID_HANDLE_VALUE, "file creation failed, at %s, error %d\n", wine_dbgstr_w(pathW),
        GetLastError());
505 506 507 508 509 510 511

    res = FindResourceA(GetModuleHandleA(NULL), (LPCSTR)MAKEINTRESOURCE(1), (LPCSTR)RT_RCDATA);
    ok( res != 0, "couldn't find resource\n" );
    ptr = LockResource( LoadResource( GetModuleHandleA(NULL), res ));
    WriteFile( file, ptr, SizeofResource( GetModuleHandleA(NULL), res ), &written, NULL );
    ok( written == SizeofResource( GetModuleHandleA(NULL), res ), "couldn't write resource\n" );
    CloseHandle( file );
512 513 514 515 516 517 518 519 520

    return pathW;
}

#define DELETE_FONTFILE(filename) _delete_testfontfile(filename, __LINE__)
static void _delete_testfontfile(const WCHAR *filename, int line)
{
    BOOL ret = DeleteFileW(filename);
    ok_(__FILE__,line)(ret, "failed to delete file %s, error %d\n", wine_dbgstr_w(filename), GetLastError());
521 522
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
struct test_fontenumerator
{
    IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
    LONG ref;

    DWORD index;
    IDWriteFontFile *font_file;
};

static inline struct test_fontenumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
{
    return CONTAINING_RECORD(iface, struct test_fontenumerator, IDWriteFontFileEnumerator_iface);
}

static HRESULT WINAPI singlefontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileEnumerator))
    {
        *obj = iface;
        IDWriteFontFileEnumerator_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI singlefontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
{
    struct test_fontenumerator *This = impl_from_IDWriteFontFileEnumerator(iface);
    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI singlefontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
{
    struct test_fontenumerator *This = impl_from_IDWriteFontFileEnumerator(iface);
    ULONG ref = InterlockedDecrement(&This->ref);
    if (!ref) {
        IDWriteFontFile_Release(This->font_file);
        heap_free(This);
    }
    return ref;
}

static HRESULT WINAPI singlefontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **font_file)
{
    struct test_fontenumerator *This = impl_from_IDWriteFontFileEnumerator(iface);
    IDWriteFontFile_AddRef(This->font_file);
    *font_file = This->font_file;
    return S_OK;
}

static HRESULT WINAPI singlefontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
{
    struct test_fontenumerator *This = impl_from_IDWriteFontFileEnumerator(iface);

    if (This->index > 1) {
        *current = FALSE;
        return S_OK;
    }

    This->index++;
    *current = TRUE;
    return S_OK;
}

static const struct IDWriteFontFileEnumeratorVtbl singlefontfileenumeratorvtbl =
{
    singlefontfileenumerator_QueryInterface,
    singlefontfileenumerator_AddRef,
    singlefontfileenumerator_Release,
    singlefontfileenumerator_MoveNext,
    singlefontfileenumerator_GetCurrentFontFile
};

static HRESULT create_enumerator(IDWriteFontFile *font_file, IDWriteFontFileEnumerator **ret)
{
    struct test_fontenumerator *enumerator;

    enumerator = heap_alloc(sizeof(struct test_fontenumerator));
    if (!enumerator)
        return E_OUTOFMEMORY;

    enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &singlefontfileenumeratorvtbl;
    enumerator->ref = 1;
    enumerator->index = 0;
    enumerator->font_file = font_file;
    IDWriteFontFile_AddRef(font_file);

    *ret = &enumerator->IDWriteFontFileEnumerator_iface;
    return S_OK;
}

struct test_fontcollectionloader
{
    IDWriteFontCollectionLoader IDWriteFontFileCollectionLoader_iface;
    IDWriteFontFileLoader *loader;
};

static inline struct test_fontcollectionloader *impl_from_IDWriteFontFileCollectionLoader(IDWriteFontCollectionLoader* iface)
{
    return CONTAINING_RECORD(iface, struct test_fontcollectionloader, IDWriteFontFileCollectionLoader_iface);
}

static HRESULT WINAPI resourcecollectionloader_QueryInterface(IDWriteFontCollectionLoader *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontCollectionLoader))
    {
        *obj = iface;
        IDWriteFontCollectionLoader_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI resourcecollectionloader_AddRef(IDWriteFontCollectionLoader *iface)
{
    return 2;
}

static ULONG WINAPI resourcecollectionloader_Release(IDWriteFontCollectionLoader *iface)
{
    return 1;
}

static HRESULT WINAPI resourcecollectionloader_CreateEnumeratorFromKey(IDWriteFontCollectionLoader *iface, IDWriteFactory *factory,
    const void * collectionKey, UINT32  collectionKeySize, IDWriteFontFileEnumerator ** fontFileEnumerator)
{
    struct test_fontcollectionloader *This = impl_from_IDWriteFontFileCollectionLoader(iface);
    IDWriteFontFile *font_file;
    HRESULT hr;

    IDWriteFactory_CreateCustomFontFileReference(factory, collectionKey, collectionKeySize, This->loader, &font_file);

    hr = create_enumerator(font_file, fontFileEnumerator);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFontFile_Release(font_file);
    return hr;
}

static const struct IDWriteFontCollectionLoaderVtbl resourcecollectionloadervtbl = {
    resourcecollectionloader_QueryInterface,
    resourcecollectionloader_AddRef,
    resourcecollectionloader_Release,
    resourcecollectionloader_CreateEnumeratorFromKey
};

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
/* Here is a functional custom font set of interfaces */
struct test_fontdatastream
{
    IDWriteFontFileStream IDWriteFontFileStream_iface;
    LONG ref;

    LPVOID data;
    DWORD size;
};

static inline struct test_fontdatastream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream* iface)
{
    return CONTAINING_RECORD(iface, struct test_fontdatastream, IDWriteFontFileStream_iface);
}

static HRESULT WINAPI fontdatastream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
    {
        *obj = iface;
        IDWriteFontFileStream_AddRef(iface);
        return S_OK;
    }
    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI fontdatastream_AddRef(IDWriteFontFileStream *iface)
{
    struct test_fontdatastream *This = impl_from_IDWriteFontFileStream(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
    return ref;
}

static ULONG WINAPI fontdatastream_Release(IDWriteFontFileStream *iface)
{
    struct test_fontdatastream *This = impl_from_IDWriteFontFileStream(iface);
    ULONG ref = InterlockedDecrement(&This->ref);
    if (ref == 0)
        HeapFree(GetProcessHeap(), 0, This);
    return ref;
}

static HRESULT WINAPI fontdatastream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
{
    struct test_fontdatastream *This = impl_from_IDWriteFontFileStream(iface);
    *fragment_context = NULL;
    if (offset+fragment_size > This->size)
    {
        *fragment_start = NULL;
        return E_FAIL;
    }
    else
    {
        *fragment_start = (BYTE*)This->data + offset;
        return S_OK;
    }
}

static void WINAPI fontdatastream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
{
    /* Do Nothing */
}

static HRESULT WINAPI fontdatastream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
{
    struct test_fontdatastream *This = impl_from_IDWriteFontFileStream(iface);
    *size = This->size;
    return S_OK;
}

static HRESULT WINAPI fontdatastream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
{
    return E_NOTIMPL;
}

static const IDWriteFontFileStreamVtbl fontdatastreamvtbl =
{
    fontdatastream_QueryInterface,
    fontdatastream_AddRef,
    fontdatastream_Release,
    fontdatastream_ReadFileFragment,
    fontdatastream_ReleaseFileFragment,
    fontdatastream_GetFileSize,
    fontdatastream_GetLastWriteTime
};

static HRESULT create_fontdatastream(LPVOID data, UINT size, IDWriteFontFileStream** iface)
{
    struct test_fontdatastream *This = HeapAlloc(GetProcessHeap(), 0, sizeof(struct test_fontdatastream));
    if (!This)
        return E_OUTOFMEMORY;

    This->data = data;
    This->size = size;
    This->ref = 1;
    This->IDWriteFontFileStream_iface.lpVtbl = &fontdatastreamvtbl;

    *iface = &This->IDWriteFontFileStream_iface;
    return S_OK;
}

static HRESULT WINAPI resourcefontfileloader_QueryInterface(IDWriteFontFileLoader *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader))
    {
        *obj = iface;
        return S_OK;
    }
    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI resourcefontfileloader_AddRef(IDWriteFontFileLoader *iface)
{
    return 2;
}

static ULONG WINAPI resourcefontfileloader_Release(IDWriteFontFileLoader *iface)
{
    return 1;
}

792 793
static HRESULT WINAPI resourcefontfileloader_CreateStreamFromKey(IDWriteFontFileLoader *iface, const void *ref_key, UINT32 key_size,
    IDWriteFontFileStream **stream)
794 795 796 797 798
{
    LPVOID data;
    DWORD size;
    HGLOBAL mem;

799
    mem = LoadResource(GetModuleHandleA(NULL), *(HRSRC*)ref_key);
800 801 802
    ok(mem != NULL, "Failed to lock font resource\n");
    if (mem)
    {
803
        size = SizeofResource(GetModuleHandleA(NULL), *(HRSRC*)ref_key);
804
        data = LockResource(mem);
805
        return create_fontdatastream(data, size, stream);
806 807 808 809 810 811 812 813 814 815 816
    }
    return E_FAIL;
}

static const struct IDWriteFontFileLoaderVtbl resourcefontfileloadervtbl = {
    resourcefontfileloader_QueryInterface,
    resourcefontfileloader_AddRef,
    resourcefontfileloader_Release,
    resourcefontfileloader_CreateStreamFromKey
};

817
static IDWriteFontFileLoader rloader = { &resourcefontfileloadervtbl };
818

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static D2D1_POINT_2F g_startpoints[2];
static int g_startpoint_count;

static HRESULT WINAPI test_geometrysink_QueryInterface(ID2D1SimplifiedGeometrySink *iface, REFIID riid, void **ret)
{
    if (IsEqualIID(riid, &IID_ID2D1SimplifiedGeometrySink) ||
        IsEqualIID(riid, &IID_IUnknown))
    {
        *ret = iface;
        ID2D1SimplifiedGeometrySink_AddRef(iface);
        return S_OK;
    }

    *ret = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI test_geometrysink_AddRef(ID2D1SimplifiedGeometrySink *iface)
{
    return 2;
}

static ULONG WINAPI test_geometrysink_Release(ID2D1SimplifiedGeometrySink *iface)
{
    return 1;
}

static void WINAPI test_geometrysink_SetFillMode(ID2D1SimplifiedGeometrySink *iface, D2D1_FILL_MODE mode)
{
848
    CHECK_EXPECT(setfillmode);
849 850 851 852 853 854 855 856 857 858 859 860
    ok(mode == D2D1_FILL_MODE_WINDING, "fill mode %d\n", mode);
}

static void WINAPI test_geometrysink_SetSegmentFlags(ID2D1SimplifiedGeometrySink *iface, D2D1_PATH_SEGMENT flags)
{
    ok(0, "unexpected SetSegmentFlags() - flags %d\n", flags);
}

static void WINAPI test_geometrysink_BeginFigure(ID2D1SimplifiedGeometrySink *iface,
    D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
{
    ok(figureBegin == D2D1_FIGURE_BEGIN_FILLED, "begin figure %d\n", figureBegin);
861 862 863
    if (g_startpoint_count < sizeof(g_startpoints)/sizeof(g_startpoints[0]))
        g_startpoints[g_startpoint_count] = startPoint;
    g_startpoint_count++;
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
}

static void WINAPI test_geometrysink_AddLines(ID2D1SimplifiedGeometrySink *iface,
    const D2D1_POINT_2F *points, UINT32 count)
{
}

static void WINAPI test_geometrysink_AddBeziers(ID2D1SimplifiedGeometrySink *iface,
    const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
{
}

static void WINAPI test_geometrysink_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
{
    ok(figureEnd == D2D1_FIGURE_END_CLOSED, "end figure %d\n", figureEnd);
}

static HRESULT WINAPI test_geometrysink_Close(ID2D1SimplifiedGeometrySink *iface)
{
    ok(0, "unexpected Close()\n");
    return E_NOTIMPL;
}

static const ID2D1SimplifiedGeometrySinkVtbl test_geometrysink_vtbl = {
    test_geometrysink_QueryInterface,
    test_geometrysink_AddRef,
    test_geometrysink_Release,
    test_geometrysink_SetFillMode,
    test_geometrysink_SetSegmentFlags,
    test_geometrysink_BeginFigure,
    test_geometrysink_AddLines,
    test_geometrysink_AddBeziers,
    test_geometrysink_EndFigure,
    test_geometrysink_Close
};

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
static void WINAPI test_geometrysink2_BeginFigure(ID2D1SimplifiedGeometrySink *iface,
    D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
{
    ok(0, "unexpected call\n");
}

static void WINAPI test_geometrysink2_AddLines(ID2D1SimplifiedGeometrySink *iface,
    const D2D1_POINT_2F *points, UINT32 count)
{
    ok(0, "unexpected call\n");
}

static void WINAPI test_geometrysink2_AddBeziers(ID2D1SimplifiedGeometrySink *iface,
    const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
{
    ok(0, "unexpected call\n");
}

static void WINAPI test_geometrysink2_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
{
    ok(0, "unexpected call\n");
}

static const ID2D1SimplifiedGeometrySinkVtbl test_geometrysink2_vtbl = {
    test_geometrysink_QueryInterface,
    test_geometrysink_AddRef,
    test_geometrysink_Release,
    test_geometrysink_SetFillMode,
    test_geometrysink_SetSegmentFlags,
    test_geometrysink2_BeginFigure,
    test_geometrysink2_AddLines,
    test_geometrysink2_AddBeziers,
    test_geometrysink2_EndFigure,
    test_geometrysink_Close
};

936
static ID2D1SimplifiedGeometrySink test_geomsink = { &test_geometrysink_vtbl };
937
static ID2D1SimplifiedGeometrySink test_geomsink2 = { &test_geometrysink2_vtbl };
938

939 940
static void test_CreateFontFromLOGFONT(void)
{
941
    static const WCHAR tahomaspW[] = {'T','a','h','o','m','a',' ',0};
942
    IDWriteGdiInterop1 *interop1;
943 944 945 946 947
    IDWriteGdiInterop *interop;
    DWRITE_FONT_WEIGHT weight;
    DWRITE_FONT_STYLE style;
    IDWriteFont *font;
    LOGFONTW logfont;
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    LONG weights[][2] = {
        {FW_NORMAL, DWRITE_FONT_WEIGHT_NORMAL},
        {FW_BOLD, DWRITE_FONT_WEIGHT_BOLD},
        {  0, DWRITE_FONT_WEIGHT_NORMAL},
        { 50, DWRITE_FONT_WEIGHT_NORMAL},
        {150, DWRITE_FONT_WEIGHT_NORMAL},
        {250, DWRITE_FONT_WEIGHT_NORMAL},
        {350, DWRITE_FONT_WEIGHT_NORMAL},
        {450, DWRITE_FONT_WEIGHT_NORMAL},
        {650, DWRITE_FONT_WEIGHT_BOLD},
        {750, DWRITE_FONT_WEIGHT_BOLD},
        {850, DWRITE_FONT_WEIGHT_BOLD},
        {950, DWRITE_FONT_WEIGHT_BOLD},
        {960, DWRITE_FONT_WEIGHT_BOLD},
    };
963
    OUTLINETEXTMETRICW otm;
964
    IDWriteFactory *factory;
965 966
    HRESULT hr;
    BOOL ret;
967 968
    HDC hdc;
    HFONT hfont;
969
    BOOL exists;
970
    ULONG ref;
971
    int i;
972
    UINT r;
973

974 975
    factory = create_factory();

976
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
977
    ok(hr == S_OK, "got %#x\n", hr);
978

979 980 981
    if (0)
        /* null out parameter crashes this call */
        hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, NULL, NULL);
982

983
    font = (void*)0xdeadbeef;
984 985
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, NULL, &font);
    EXPECT_HR(hr, E_INVALIDARG);
986
    ok(font == NULL, "got %p\n", font);
987 988 989 990 991 992

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
993
    lstrcpyW(logfont.lfFaceName, tahomaW);
994 995 996 997

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    EXPECT_HR(hr, S_OK);

998 999 1000 1001 1002 1003 1004 1005 1006 1007
    hfont = CreateFontIndirectW(&logfont);
    hdc = CreateCompatibleDC(0);
    SelectObject(hdc, hfont);

    otm.otmSize = sizeof(otm);
    r = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
    ok(r, "got %d\n", r);
    DeleteDC(hdc);
    DeleteObject(hfont);

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    exists = TRUE;
    hr = IDWriteFont_HasCharacter(font, 0xd800, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == FALSE, "got %d\n", exists);

    exists = FALSE;
    hr = IDWriteFont_HasCharacter(font, 0x20, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == TRUE, "got %d\n", exists);

1018 1019 1020 1021 1022
    /* now check properties */
    weight = IDWriteFont_GetWeight(font);
    ok(weight == DWRITE_FONT_WEIGHT_NORMAL, "got %d\n", weight);

    style = IDWriteFont_GetStyle(font);
1023
    ok(style == DWRITE_FONT_STYLE_OBLIQUE, "got %d\n", style);
1024
    ok(otm.otmfsSelection & 1, "got 0x%08x\n", otm.otmfsSelection);
1025

1026 1027 1028 1029 1030
    ret = IDWriteFont_IsSymbolFont(font);
    ok(!ret, "got %d\n", ret);

    IDWriteFont_Release(font);

1031 1032 1033 1034 1035 1036 1037
    /* weight values */
    for (i = 0; i < sizeof(weights)/(2*sizeof(LONG)); i++)
    {
        memset(&logfont, 0, sizeof(logfont));
        logfont.lfHeight = 12;
        logfont.lfWidth  = 12;
        logfont.lfWeight = weights[i][0];
1038
        lstrcpyW(logfont.lfFaceName, tahomaW);
1039 1040 1041 1042 1043 1044 1045

        hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
        EXPECT_HR(hr, S_OK);

        weight = IDWriteFont_GetWeight(font);
        ok(weight == weights[i][1],
            "%d: got %d, expected %d\n", i, weight, weights[i][1]);
1046

1047 1048 1049 1050 1051 1052 1053 1054
        IDWriteFont_Release(font);
    }

    /* weight not from enum */
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = 550;
1055
    lstrcpyW(logfont.lfFaceName, tahomaW);
1056

1057
    font = NULL;
1058
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
1059
    ok(hr == S_OK, "got 0x%08x\n", hr);
1060 1061

    weight = IDWriteFont_GetWeight(font);
1062
    ok(weight == DWRITE_FONT_WEIGHT_NORMAL || weight == DWRITE_FONT_WEIGHT_BOLD,
1063
        "got %d\n", weight);
1064

1065 1066 1067 1068 1069 1070 1071 1072 1073
    IDWriteFont_Release(font);

    /* empty or nonexistent face name */
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    lstrcpyW(logfont.lfFaceName, blahW);

1074
    font = (void*)0xdeadbeef;
1075
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
1076
    ok(hr == DWRITE_E_NOFONT, "got 0x%08x\n", hr);
1077
    ok(font == NULL, "got %p\n", font);
1078

1079
    /* Try with name 'Tahoma ' */
1080 1081 1082 1083
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
1084
    lstrcpyW(logfont.lfFaceName, tahomaspW);
1085

1086
    font = (void*)0xdeadbeef;
1087
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
1088
    ok(hr == DWRITE_E_NOFONT, "got 0x%08x\n", hr);
1089 1090
    ok(font == NULL, "got %p\n", font);

1091
    /* empty string as a facename */
1092 1093 1094 1095 1096 1097 1098
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;

    font = (void*)0xdeadbeef;
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
1099
    ok(hr == DWRITE_E_NOFONT, "got 0x%08x\n", hr);
1100
    ok(font == NULL, "got %p\n", font);
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    /* IDWriteGdiInterop1::CreateFontFromLOGFONT() */
    hr = IDWriteGdiInterop_QueryInterface(interop, &IID_IDWriteGdiInterop1, (void**)&interop1);
    if (hr == S_OK) {
        memset(&logfont, 0, sizeof(logfont));
        logfont.lfHeight = 12;
        logfont.lfWidth  = 12;
        logfont.lfWeight = FW_NORMAL;
        logfont.lfItalic = 1;
        lstrcpyW(logfont.lfFaceName, tahomaW);

        hr = IDWriteGdiInterop1_CreateFontFromLOGFONT(interop1, &logfont, NULL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        IDWriteFont_Release(font);
        IDWriteGdiInterop1_Release(interop1);
    }
    else
        win_skip("IDWriteGdiInterop1 is not supported, skipping CreateFontFromLOGFONT() tests.\n");

1121 1122 1123 1124
    ref = IDWriteGdiInterop_Release(interop);
    ok(ref == 0, "interop is not released, %u\n", ref);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory is not released, %u\n", ref);
1125 1126
}

1127 1128 1129
static void test_CreateBitmapRenderTarget(void)
{
    IDWriteBitmapRenderTarget *target, *target2;
1130
    IDWriteBitmapRenderTarget1 *target1;
1131
    IDWriteRenderingParams *params;
1132
    IDWriteGdiInterop *interop;
1133
    IDWriteFontFace *fontface;
1134
    IDWriteFactory *factory;
1135
    DWRITE_GLYPH_RUN run;
1136
    HBITMAP hbm, hbm2;
1137
    UINT16 glyphs[2];
1138
    DWRITE_MATRIX m;
1139
    DIBSECTION ds;
1140
    XFORM xform;
1141
    COLORREF c;
1142
    HRESULT hr;
1143
    FLOAT pdip;
1144
    SIZE size;
1145
    ULONG ref;
1146
    UINT32 ch;
1147 1148 1149
    HDC hdc;
    int ret;

1150 1151
    factory = create_factory();

1152 1153 1154 1155 1156 1157 1158
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    EXPECT_HR(hr, S_OK);

    target = NULL;
    hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 0, 0, &target);
    EXPECT_HR(hr, S_OK);

1159 1160
    if (0) /* crashes on native */
        hr = IDWriteBitmapRenderTarget_GetSize(target, NULL);
1161 1162 1163 1164 1165 1166 1167

    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    EXPECT_HR(hr, S_OK);
    ok(size.cx == 0, "got %d\n", size.cx);
    ok(size.cy == 0, "got %d\n", size.cy);

1168 1169 1170 1171 1172 1173 1174 1175 1176
    target2 = NULL;
    hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 0, 0, &target2);
    EXPECT_HR(hr, S_OK);
    ok(target != target2, "got %p, %p\n", target2, target);
    IDWriteBitmapRenderTarget_Release(target2);

    hdc = IDWriteBitmapRenderTarget_GetMemoryDC(target);
    ok(hdc != NULL, "got %p\n", hdc);

1177 1178 1179 1180
    /* test mode */
    ret = GetGraphicsMode(hdc);
    ok(ret == GM_ADVANCED, "got %d\n", ret);

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    hbm = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm != NULL, "got %p\n", hbm);

    /* check DIB properties */
    ret = GetObjectW(hbm, sizeof(ds), &ds);
    ok(ret == sizeof(BITMAP), "got %d\n", ret);
    ok(ds.dsBm.bmWidth == 1, "got %d\n", ds.dsBm.bmWidth);
    ok(ds.dsBm.bmHeight == 1, "got %d\n", ds.dsBm.bmHeight);
    ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
    ok(ds.dsBm.bmBitsPixel == 1, "got %d\n", ds.dsBm.bmBitsPixel);
    ok(!ds.dsBm.bmBits, "got %p\n", ds.dsBm.bmBits);

    IDWriteBitmapRenderTarget_Release(target);

    hbm = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(!hbm, "got %p\n", hbm);

    target = NULL;
    hr = IDWriteGdiInterop_CreateBitmapRenderTarget(interop, NULL, 10, 5, &target);
    EXPECT_HR(hr, S_OK);

    hdc = IDWriteBitmapRenderTarget_GetMemoryDC(target);
    ok(hdc != NULL, "got %p\n", hdc);

1205 1206 1207 1208 1209 1210 1211 1212
    /* test context settings */
    c = GetTextColor(hdc);
    ok(c == RGB(0, 0, 0), "got 0x%08x\n", c);
    ret = GetBkMode(hdc);
    ok(ret == OPAQUE, "got %d\n", ret);
    c = GetBkColor(hdc);
    ok(c == RGB(255, 255, 255), "got 0x%08x\n", c);

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    hbm = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm != NULL, "got %p\n", hbm);

    /* check DIB properties */
    ret = GetObjectW(hbm, sizeof(ds), &ds);
    ok(ret == sizeof(ds), "got %d\n", ret);
    ok(ds.dsBm.bmWidth == 10, "got %d\n", ds.dsBm.bmWidth);
    ok(ds.dsBm.bmHeight == 5, "got %d\n", ds.dsBm.bmHeight);
    ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
    ok(ds.dsBm.bmBitsPixel == 32, "got %d\n", ds.dsBm.bmBitsPixel);
    ok(ds.dsBm.bmBits != NULL, "got %p\n", ds.dsBm.bmBits);

1225 1226 1227 1228 1229 1230
    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    EXPECT_HR(hr, S_OK);
    ok(size.cx == 10, "got %d\n", size.cx);
    ok(size.cy == 5, "got %d\n", size.cy);

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    /* resize to same size */
    hr = IDWriteBitmapRenderTarget_Resize(target, 10, 5);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hbm2 = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm2 == hbm, "got %p, %p\n", hbm2, hbm);

    /* shrink */
    hr = IDWriteBitmapRenderTarget_Resize(target, 5, 5);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1242 1243 1244 1245 1246 1247
    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size.cx == 5, "got %d\n", size.cx);
    ok(size.cy == 5, "got %d\n", size.cy);

1248 1249 1250 1251 1252 1253
    hbm2 = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm2 != hbm, "got %p, %p\n", hbm2, hbm);

    hr = IDWriteBitmapRenderTarget_Resize(target, 20, 5);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1254 1255 1256 1257 1258 1259
    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size.cx == 20, "got %d\n", size.cx);
    ok(size.cy == 5, "got %d\n", size.cy);

1260 1261 1262 1263 1264
    hbm2 = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm2 != hbm, "got %p, %p\n", hbm2, hbm);

    hr = IDWriteBitmapRenderTarget_Resize(target, 1, 5);
    ok(hr == S_OK, "got 0x%08x\n", hr);
1265

1266 1267 1268 1269 1270 1271
    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size.cx == 1, "got %d\n", size.cx);
    ok(size.cy == 5, "got %d\n", size.cy);

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    hbm2 = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm2 != hbm, "got %p, %p\n", hbm2, hbm);

    ret = GetObjectW(hbm2, sizeof(ds), &ds);
    ok(ret == sizeof(ds), "got %d\n", ret);
    ok(ds.dsBm.bmWidth == 1, "got %d\n", ds.dsBm.bmWidth);
    ok(ds.dsBm.bmHeight == 5, "got %d\n", ds.dsBm.bmHeight);
    ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
    ok(ds.dsBm.bmBitsPixel == 32, "got %d\n", ds.dsBm.bmBitsPixel);
    ok(ds.dsBm.bmBits != NULL, "got %p\n", ds.dsBm.bmBits);

    /* empty rectangle */
    hr = IDWriteBitmapRenderTarget_Resize(target, 0, 5);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1287 1288 1289 1290 1291 1292
    size.cx = size.cy = -1;
    hr = IDWriteBitmapRenderTarget_GetSize(target, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size.cx == 0, "got %d\n", size.cx);
    ok(size.cy == 5, "got %d\n", size.cy);

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    hbm2 = GetCurrentObject(hdc, OBJ_BITMAP);
    ok(hbm2 != hbm, "got %p, %p\n", hbm2, hbm);

    ret = GetObjectW(hbm2, sizeof(ds), &ds);
    ok(ret == sizeof(BITMAP), "got %d\n", ret);
    ok(ds.dsBm.bmWidth == 1, "got %d\n", ds.dsBm.bmWidth);
    ok(ds.dsBm.bmHeight == 1, "got %d\n", ds.dsBm.bmHeight);
    ok(ds.dsBm.bmPlanes == 1, "got %d\n", ds.dsBm.bmPlanes);
    ok(ds.dsBm.bmBitsPixel == 1, "got %d\n", ds.dsBm.bmBitsPixel);
    ok(!ds.dsBm.bmBits, "got %p\n", ds.dsBm.bmBits);

1304
    /* transform tests, current hdc transform is not immediately affected */
1305 1306
    if (0) /* crashes on native */
        hr = IDWriteBitmapRenderTarget_GetCurrentTransform(target, NULL);
1307 1308 1309 1310 1311

    memset(&m, 0xcc, sizeof(m));
    hr = IDWriteBitmapRenderTarget_GetCurrentTransform(target, &m);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(m.m11 == 1.0 && m.m22 == 1.0 && m.m12 == 0.0 && m.m21 == 0.0, "got %.1f,%.1f,%.1f,%.1f\n", m.m11, m.m22, m.m12, m.m21);
1312
    ok(m.dx == 0.0 && m.dy == 0.0, "got %.1f,%.1f\n", m.dx, m.dy);
1313 1314 1315 1316
    ret = GetWorldTransform(hdc, &xform);
    ok(ret, "got %d\n", ret);
    ok(xform.eM11 == 1.0 && xform.eM22 == 1.0 && xform.eM12 == 0.0 && xform.eM21 == 0.0, "got wrong transform\n");
    ok(xform.eDx == 0.0 && xform.eDy == 0.0, "got %.1f,%.1f\n", xform.eDx, xform.eDy);
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    memset(&m, 0, sizeof(m));
    hr = IDWriteBitmapRenderTarget_SetCurrentTransform(target, &m);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&m, 0xcc, sizeof(m));
    hr = IDWriteBitmapRenderTarget_GetCurrentTransform(target, &m);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(m.m11 == 0.0 && m.m22 == 0.0 && m.m12 == 0.0 && m.m21 == 0.0, "got %.1f,%.1f,%.1f,%.1f\n", m.m11, m.m22, m.m12, m.m21);
    ok(m.dx == 0.0 && m.dy == 0.0, "got %.1f,%.1f\n", m.dx, m.dy);
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
    ret = GetWorldTransform(hdc, &xform);
    ok(ret, "got %d\n", ret);
    ok(xform.eM11 == 1.0 && xform.eM22 == 1.0 && xform.eM12 == 0.0 && xform.eM21 == 0.0, "got wrong transform\n");
    ok(xform.eDx == 0.0 && xform.eDy == 0.0, "got %.1f,%.1f\n", xform.eDx, xform.eDy);

    memset(&m, 0, sizeof(m));
    m.m11 = 2.0; m.m22 = 1.0;
    hr = IDWriteBitmapRenderTarget_SetCurrentTransform(target, &m);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ret = GetWorldTransform(hdc, &xform);
    ok(ret, "got %d\n", ret);
    ok(xform.eM11 == 1.0 && xform.eM22 == 1.0 && xform.eM12 == 0.0 && xform.eM21 == 0.0, "got wrong transform\n");
    ok(xform.eDx == 0.0 && xform.eDy == 0.0, "got %.1f,%.1f\n", xform.eDx, xform.eDy);
1340 1341 1342 1343 1344 1345 1346 1347

    hr = IDWriteBitmapRenderTarget_SetCurrentTransform(target, NULL);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&m, 0xcc, sizeof(m));
    hr = IDWriteBitmapRenderTarget_GetCurrentTransform(target, &m);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(m.m11 == 1.0 && m.m22 == 1.0 && m.m12 == 0.0 && m.m21 == 0.0, "got %.1f,%.1f,%.1f,%.1f\n", m.m11, m.m22, m.m12, m.m21);
1348 1349
    ok(m.dx == 0.0 && m.dy == 0.0, "got %.1f,%.1f\n", m.dx, m.dy);

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    /* pixels per dip */
    pdip = IDWriteBitmapRenderTarget_GetPixelsPerDip(target);
    ok(pdip == 1.0, "got %.2f\n", pdip);

    hr = IDWriteBitmapRenderTarget_SetPixelsPerDip(target, 2.0);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteBitmapRenderTarget_SetPixelsPerDip(target, -1.0);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = IDWriteBitmapRenderTarget_SetPixelsPerDip(target, 0.0);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    pdip = IDWriteBitmapRenderTarget_GetPixelsPerDip(target);
    ok(pdip == 2.0, "got %.2f\n", pdip);

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    hr = IDWriteBitmapRenderTarget_QueryInterface(target, &IID_IDWriteBitmapRenderTarget1, (void**)&target1);
    if (hr == S_OK) {
        DWRITE_TEXT_ANTIALIAS_MODE mode;

        mode = IDWriteBitmapRenderTarget1_GetTextAntialiasMode(target1);
        ok(mode == DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE, "got %d\n", mode);

        hr = IDWriteBitmapRenderTarget1_SetTextAntialiasMode(target1, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE+1);
        ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

        mode = IDWriteBitmapRenderTarget1_GetTextAntialiasMode(target1);
        ok(mode == DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE, "got %d\n", mode);

        hr = IDWriteBitmapRenderTarget1_SetTextAntialiasMode(target1, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        mode = IDWriteBitmapRenderTarget1_GetTextAntialiasMode(target1);
        ok(mode == DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE, "got %d\n", mode);

        IDWriteBitmapRenderTarget1_Release(target1);
    }
    else
        win_skip("IDWriteBitmapRenderTarget1 is not supported.\n");

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    /* DrawGlyphRun() argument validation. */
    hr = IDWriteBitmapRenderTarget_Resize(target, 16, 16);
    ok(hr == S_OK, "Failed to resize target, hr %#x.\n", hr);

    fontface = create_fontface(factory);

    ch = 'A';
    glyphs[0] = 0;
    hr = IDWriteFontFace_GetGlyphIndices(fontface, &ch, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyphs[0] > 0, "got %u\n", glyphs[0]);
    glyphs[1] = glyphs[0];

    memset(&run, 0, sizeof(run));
    run.fontFace = fontface;
    run.fontEmSize = 12.0f;
    run.glyphCount = 2;
    run.glyphIndices = glyphs;

    hr = IDWriteFactory_CreateCustomRenderingParams(factory, 1.0f, 0.0f, 0.0f, DWRITE_PIXEL_GEOMETRY_FLAT,
            DWRITE_RENDERING_MODE_DEFAULT, &params);
    ok(hr == S_OK, "Failed to create rendering params, hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_NATURAL,
        &run, NULL, RGB(255, 0, 0), NULL);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_GDI_NATURAL + 1,
        &run, NULL, RGB(255, 0, 0), NULL);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_GDI_NATURAL + 1,
        &run, params, RGB(255, 0, 0), NULL);
    ok(hr == E_INVALIDARG || broken(hr == S_OK) /* Vista */, "Unexpected hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_GDI_NATURAL,
        &run, params, RGB(255, 0, 0), NULL);
    ok(hr == S_OK, "Failed to draw a run, hr %#x.\n", hr);

    IDWriteRenderingParams_Release(params);

    /* Zero sized target returns earlier. */
    hr = IDWriteBitmapRenderTarget_Resize(target, 0, 16);
    ok(hr == S_OK, "Failed to resize target, hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_NATURAL,
        &run, NULL, RGB(255, 0, 0), NULL);
    ok(hr == S_OK, "Unexpected hr %#x.\n", hr);

    hr = IDWriteBitmapRenderTarget_DrawGlyphRun(target, 0.0f, 0.0f, DWRITE_MEASURING_MODE_GDI_NATURAL + 1,
        &run, params, RGB(255, 0, 0), NULL);
    ok(hr == S_OK, "Unexpected hr %#x.\n", hr);

    IDWriteFontFace_Release(fontface);

1445 1446 1447 1448 1449 1450
    ref = IDWriteBitmapRenderTarget_Release(target);
    ok(ref == 0, "render target not released, %u\n", ref);
    ref = IDWriteGdiInterop_Release(interop);
    ok(ref == 0, "interop not released, %u\n", ref);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
1451 1452
}

1453 1454
static void test_GetFontFamily(void)
{
1455 1456
    IDWriteFontCollection *collection, *collection2;
    IDWriteFontCollection *syscoll;
1457
    IDWriteFontFamily *family, *family2;
1458
    IDWriteFontFamily1 *family1;
1459
    IDWriteGdiInterop *interop;
1460
    IDWriteFont *font, *font2;
1461
    IDWriteFactory *factory;
1462 1463
    LOGFONTW logfont;
    HRESULT hr;
1464
    ULONG ref;
1465

1466 1467
    factory = create_factory();

1468 1469 1470
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    EXPECT_HR(hr, S_OK);

1471 1472 1473
    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscoll, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1474 1475 1476 1477 1478
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
1479
    lstrcpyW(logfont.lfFaceName, tahomaW);
1480 1481

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
1482 1483 1484 1485 1486
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(font2 != font, "got %p, %p\n", font2, font);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507

if (0) /* crashes on native */
    hr = IDWriteFont_GetFontFamily(font, NULL);

    EXPECT_REF(font, 1);
    hr = IDWriteFont_GetFontFamily(font, &family);
    EXPECT_HR(hr, S_OK);
    EXPECT_REF(font, 1);
    EXPECT_REF(family, 2);

    hr = IDWriteFont_GetFontFamily(font, &family2);
    EXPECT_HR(hr, S_OK);
    ok(family2 == family, "got %p, previous %p\n", family2, family);
    EXPECT_REF(font, 1);
    EXPECT_REF(family, 3);
    IDWriteFontFamily_Release(family2);

    hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFontFamily, (void**)&family2);
    EXPECT_HR(hr, E_NOINTERFACE);
    ok(family2 == NULL, "got %p\n", family2);

1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    hr = IDWriteFont_GetFontFamily(font2, &family2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(family2 != family, "got %p, %p\n", family2, family);

    collection = NULL;
    hr = IDWriteFontFamily_GetFontCollection(family, &collection);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    collection2 = NULL;
    hr = IDWriteFontFamily_GetFontCollection(family2, &collection2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(collection == collection2, "got %p, %p\n", collection, collection2);
    ok(collection == syscoll, "got %p, %p\n", collection, syscoll);

1522 1523 1524 1525 1526
    IDWriteFont_Release(font);
    IDWriteFont_Release(font2);

    hr = IDWriteFontFamily_QueryInterface(family, &IID_IDWriteFontFamily1, (void**)&family1);
    if (hr == S_OK) {
1527
        IDWriteFontFaceReference *ref, *ref1;
1528
        IDWriteFontList *fontlist;
1529 1530 1531 1532 1533 1534 1535
        IDWriteFont3 *font3;
        IDWriteFont1 *font1;

        font3 = (void*)0xdeadbeef;
        hr = IDWriteFontFamily1_GetFont(family1, ~0u, &font3);
        ok(hr == E_FAIL, "got 0x%08x\n", hr);
        ok(font3 == NULL, "got %p\n", font3);
1536

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
        hr = IDWriteFontFamily1_GetFont(family1, 0, &font3);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFont3_QueryInterface(font3, &IID_IDWriteFont, (void**)&font);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFont_Release(font);

        hr = IDWriteFont3_QueryInterface(font3, &IID_IDWriteFont1, (void**)&font1);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFont1_Release(font1);

1548 1549 1550 1551 1552 1553 1554
        hr = IDWriteFontFamily1_QueryInterface(family1, &IID_IDWriteFontList1, (void**)&fontlist);
        ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily1_QueryInterface(family1, &IID_IDWriteFontList, (void**)&fontlist);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFontList_Release(fontlist);

1555
        IDWriteFont3_Release(font3);
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

        hr = IDWriteFontFamily1_GetFontFaceReference(family1, 0, &ref);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily1_GetFontFaceReference(family1, 0, &ref1);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(ref != ref1, "got %p, %p\n", ref, ref1);

        IDWriteFontFaceReference_Release(ref);
        IDWriteFontFaceReference_Release(ref1);

1567 1568 1569 1570 1571
        IDWriteFontFamily1_Release(family1);
    }
    else
        win_skip("IDWriteFontFamily1 is not supported.\n");

1572 1573 1574 1575
    IDWriteFontCollection_Release(syscoll);
    IDWriteFontCollection_Release(collection2);
    IDWriteFontCollection_Release(collection);
    IDWriteFontFamily_Release(family2);
1576 1577
    IDWriteFontFamily_Release(family);
    IDWriteGdiInterop_Release(interop);
1578 1579
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
1580 1581
}

1582 1583 1584 1585 1586
static void test_GetFamilyNames(void)
{
    IDWriteFontFamily *family;
    IDWriteLocalizedStrings *names, *names2;
    IDWriteGdiInterop *interop;
1587
    IDWriteFactory *factory;
1588 1589
    IDWriteFont *font;
    LOGFONTW logfont;
1590
    WCHAR buffer[100];
1591
    HRESULT hr;
1592
    UINT32 len;
1593
    ULONG ref;
1594

1595 1596
    factory = create_factory();

1597 1598 1599 1600 1601 1602 1603 1604
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    EXPECT_HR(hr, S_OK);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
1605
    lstrcpyW(logfont.lfFaceName, tahomaW);
1606 1607 1608 1609 1610 1611 1612

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    EXPECT_HR(hr, S_OK);

    hr = IDWriteFont_GetFontFamily(font, &family);
    EXPECT_HR(hr, S_OK);

1613 1614
    if (0) /* crashes on native */
        hr = IDWriteFontFamily_GetFamilyNames(family, NULL);
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626

    hr = IDWriteFontFamily_GetFamilyNames(family, &names);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(names, 1);

    hr = IDWriteFontFamily_GetFamilyNames(family, &names2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(names2, 1);
    ok(names != names2, "got %p, was %p\n", names2, names);

    IDWriteLocalizedStrings_Release(names2);

1627
    /* GetStringLength */
1628 1629
    if (0) /* crashes on native */
        hr = IDWriteLocalizedStrings_GetStringLength(names, 0, NULL);
1630

1631 1632 1633 1634 1635
    len = 100;
    hr = IDWriteLocalizedStrings_GetStringLength(names, 10, &len);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(len == (UINT32)-1, "got %u\n", len);

1636 1637 1638 1639 1640
    len = 0;
    hr = IDWriteLocalizedStrings_GetStringLength(names, 0, &len);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(len > 0, "got %u\n", len);

1641 1642 1643 1644 1645
    /* GetString */
    hr = IDWriteLocalizedStrings_GetString(names, 0, NULL, 0);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);

    hr = IDWriteLocalizedStrings_GetString(names, 10, NULL, 0);
1646
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
1647

1648 1649
    if (0)
        hr = IDWriteLocalizedStrings_GetString(names, 0, NULL, 100);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669

    buffer[0] = 1;
    hr = IDWriteLocalizedStrings_GetString(names, 10, buffer, 100);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(buffer[0] == 0, "got %x\n", buffer[0]);

    buffer[0] = 1;
    hr = IDWriteLocalizedStrings_GetString(names, 0, buffer, len-1);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buffer[0] == 0, "got %x\n", buffer[0]);

    buffer[0] = 1;
    hr = IDWriteLocalizedStrings_GetString(names, 0, buffer, len);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buffer[0] == 0, "got %x\n", buffer[0]);

    buffer[0] = 0;
    hr = IDWriteLocalizedStrings_GetString(names, 0, buffer, len+1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(buffer[0] != 0, "got %x\n", buffer[0]);
1670 1671 1672

    IDWriteLocalizedStrings_Release(names);

1673 1674 1675
    IDWriteFontFamily_Release(family);
    IDWriteFont_Release(font);
    IDWriteGdiInterop_Release(interop);
1676 1677
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
1678 1679
}

1680 1681 1682
static void test_CreateFontFace(void)
{
    IDWriteFontFace *fontface, *fontface2;
1683
    IDWriteFontCollection *collection;
1684 1685
    DWRITE_FONT_FILE_TYPE file_type;
    DWRITE_FONT_FACE_TYPE face_type;
1686
    IDWriteGdiInterop *interop;
1687 1688
    IDWriteFont *font, *font2;
    IDWriteFontFamily *family;
1689
    IDWriteFactory *factory;
1690
    IDWriteFontFile *file;
1691
    LOGFONTW logfont;
1692 1693
    BOOL supported;
    UINT32 count;
1694
    WCHAR *path;
1695 1696
    HRESULT hr;

1697 1698
    factory = create_factory();

1699 1700 1701 1702 1703 1704 1705 1706
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    EXPECT_HR(hr, S_OK);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
1707
    lstrcpyW(logfont.lfFaceName, tahomaW);
1708

1709
    font = NULL;
1710 1711 1712
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1713 1714 1715 1716 1717
    font2 = NULL;
    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(font != font2, "got %p, %p\n", font, font2);

1718 1719 1720
    hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFontFace, (void**)&fontface);
    ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);

1721 1722
    if (0) /* crashes on native */
        hr = IDWriteFont_CreateFontFace(font, NULL);
1723

1724
    fontface = NULL;
1725 1726 1727
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1728
    fontface2 = NULL;
1729 1730 1731
    hr = IDWriteFont_CreateFontFace(font, &fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface == fontface2, "got %p, was %p\n", fontface2, fontface);
1732 1733 1734 1735 1736 1737 1738 1739 1740
    IDWriteFontFace_Release(fontface2);

    fontface2 = NULL;
    hr = IDWriteFont_CreateFontFace(font2, &fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface == fontface2, "got %p, was %p\n", fontface2, fontface);
    IDWriteFontFace_Release(fontface2);

    IDWriteFont_Release(font2);
1741 1742 1743 1744
    IDWriteFont_Release(font);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFont, (void**)&font);
    ok(hr == E_NOINTERFACE || broken(hr == E_NOTIMPL), "got 0x%08x\n", hr);
1745 1746 1747

    IDWriteFontFace_Release(fontface);
    IDWriteGdiInterop_Release(interop);
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

    /* Create from system collection */
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    font = NULL;
    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    font2 = NULL;
    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(font != font2, "got %p, %p\n", font, font2);

    fontface = NULL;
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    fontface2 = NULL;
    hr = IDWriteFont_CreateFontFace(font2, &fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface == fontface2, "got %p, was %p\n", fontface2, fontface);

    IDWriteFontFace_Release(fontface);
    IDWriteFontFace_Release(fontface2);
    IDWriteFont_Release(font2);
    IDWriteFont_Release(font);
    IDWriteFontFamily_Release(family);
    IDWriteFontCollection_Release(collection);
1782 1783

    /* IDWriteFactory::CreateFontFace() */
1784
    path = create_testfontfile(test_fontfile);
1785 1786
    factory = create_factory();

1787
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
    ok(hr == S_OK, "got 0x%08x\n",hr);

    supported = FALSE;
    file_type = DWRITE_FONT_FILE_TYPE_UNKNOWN;
    face_type = DWRITE_FONT_FACE_TYPE_CFF;
    count = 0;
    hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &count);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(supported == TRUE, "got %i\n", supported);
    ok(file_type == DWRITE_FONT_FILE_TYPE_TRUETYPE, "got %i\n", file_type);
    ok(face_type == DWRITE_FONT_FACE_TYPE_TRUETYPE, "got %i\n", face_type);
    ok(count == 1, "got %i\n", count);

1801 1802 1803 1804 1805 1806 1807
    /* invalid simulation flags */
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_CFF, 1, &file, 0, ~0u, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_CFF, 1, &file, 0, 0xf, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

1808 1809 1810 1811
    /* try mismatching face type, the one that's not supported */
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_CFF, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == DWRITE_E_FILEFORMAT, "got 0x%08x\n", hr);

1812
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_OPENTYPE_COLLECTION, 1, &file, 0,
1813
        DWRITE_FONT_SIMULATIONS_NONE, &fontface);
1814
    ok(hr == DWRITE_E_FILEFORMAT || broken(hr == E_FAIL) /* < win10 */, "got 0x%08x\n", hr);
1815 1816 1817 1818 1819

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_RAW_CFF, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
todo_wine
    ok(hr == DWRITE_E_UNSUPPORTEDOPERATION || broken(hr == E_INVALIDARG) /* older versions */, "got 0x%08x\n", hr);

1820
    fontface = (void*)0xdeadbeef;
1821 1822
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TYPE1, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1823
    ok(fontface == NULL, "got %p\n", fontface);
1824

1825
    fontface = (void*)0xdeadbeef;
1826 1827
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_VECTOR, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1828
    ok(fontface == NULL, "got %p\n", fontface);
1829

1830
    fontface = (void*)0xdeadbeef;
1831 1832
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_BITMAP, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1833
    ok(fontface == NULL, "got %p\n", fontface);
1834

1835
    fontface = NULL;
1836
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_UNKNOWN, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
1837 1838 1839 1840 1841 1842 1843 1844
todo_wine
    ok(hr == S_OK || broken(hr == E_INVALIDARG) /* < win10 */, "got 0x%08x\n", hr);
    if (hr == S_OK) {
        ok(fontface != NULL, "got %p\n", fontface);
        face_type = IDWriteFontFace_GetType(fontface);
        ok(face_type == DWRITE_FONT_FACE_TYPE_TRUETYPE, "got %d\n", face_type);
        IDWriteFontFace_Release(fontface);
    }
1845 1846

    IDWriteFontFile_Release(file);
1847
    IDWriteFactory_Release(factory);
1848
    DELETE_FONTFILE(path);
1849 1850
}

1851
static void get_expected_font_metrics(IDWriteFontFace *fontface, DWRITE_FONT_METRICS1 *metrics)
1852
{
1853
    void *os2_context, *head_context, *post_context, *hhea_context;
1854 1855 1856
    const TT_OS2_V2 *tt_os2;
    const TT_HEAD *tt_head;
    const TT_POST *tt_post;
1857
    const TT_HHEA *tt_hhea;
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
    UINT32 size;
    BOOL exists;
    HRESULT hr;

    memset(metrics, 0, sizeof(*metrics));

    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_0S2_TAG, (const void**)&tt_os2, &size, &os2_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_HEAD_TAG, (const void**)&tt_head, &size, &head_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
1868 1869
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_HHEA_TAG, (const void**)&tt_hhea, &size, &hhea_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
1870 1871 1872
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_POST_TAG, (const void**)&tt_post, &size, &post_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);

1873
    if (tt_head) {
1874
        metrics->designUnitsPerEm = GET_BE_WORD(tt_head->unitsPerEm);
1875 1876 1877 1878 1879
        metrics->glyphBoxLeft = GET_BE_WORD(tt_head->xMin);
        metrics->glyphBoxTop = GET_BE_WORD(tt_head->yMax);
        metrics->glyphBoxRight = GET_BE_WORD(tt_head->xMax);
        metrics->glyphBoxBottom = GET_BE_WORD(tt_head->yMin);
    }
1880 1881

    if (tt_os2) {
1882 1883 1884 1885
        if (GET_BE_WORD(tt_os2->fsSelection) & OS2_FSSELECTION_USE_TYPO_METRICS) {
            SHORT descent = GET_BE_WORD(tt_os2->sTypoDescender);
            metrics->ascent = GET_BE_WORD(tt_os2->sTypoAscender);
            metrics->descent = descent < 0 ? -descent : 0;
1886
            metrics->lineGap = GET_BE_WORD(tt_os2->sTypoLineGap);
1887 1888 1889 1890 1891 1892 1893
            metrics->hasTypographicMetrics = TRUE;
        }
        else {
            metrics->ascent  = GET_BE_WORD(tt_os2->usWinAscent);
            /* Some fonts have usWinDescent value stored as signed short, which could be wrongly
               interpreted as large unsigned value. */
            metrics->descent = abs((SHORT)GET_BE_WORD(tt_os2->usWinDescent));
1894 1895 1896 1897 1898 1899 1900 1901 1902

            if (tt_hhea) {
                SHORT descender = (SHORT)GET_BE_WORD(tt_hhea->descender);
                INT32 linegap;

                linegap = GET_BE_WORD(tt_hhea->ascender) + abs(descender) + GET_BE_WORD(tt_hhea->linegap) -
                    metrics->ascent - metrics->descent;
                metrics->lineGap = linegap > 0 ? linegap : 0;
            }
1903 1904
        }

1905 1906
        metrics->strikethroughPosition  = GET_BE_WORD(tt_os2->yStrikeoutPosition);
        metrics->strikethroughThickness = GET_BE_WORD(tt_os2->yStrikeoutSize);
1907 1908 1909 1910 1911 1912 1913 1914 1915

        metrics->subscriptPositionX = GET_BE_WORD(tt_os2->ySubscriptXOffset);
        metrics->subscriptPositionY = -GET_BE_WORD(tt_os2->ySubscriptYOffset);
        metrics->subscriptSizeX = GET_BE_WORD(tt_os2->ySubscriptXSize);
        metrics->subscriptSizeY = GET_BE_WORD(tt_os2->ySubscriptYSize);
        metrics->superscriptPositionX = GET_BE_WORD(tt_os2->ySuperscriptXOffset);
        metrics->superscriptPositionY = GET_BE_WORD(tt_os2->ySuperscriptYOffset);
        metrics->superscriptSizeX = GET_BE_WORD(tt_os2->ySuperscriptXSize);
        metrics->superscriptSizeY = GET_BE_WORD(tt_os2->ySuperscriptYSize);
1916 1917 1918 1919 1920 1921 1922
    }

    if (tt_post) {
        metrics->underlinePosition = GET_BE_WORD(tt_post->underlinePosition);
        metrics->underlineThickness = GET_BE_WORD(tt_post->underlineThickness);
    }

1923
    if (metrics->underlineThickness == 0)
1924
        metrics->underlineThickness = metrics->designUnitsPerEm / 14;
1925 1926
    if (metrics->strikethroughThickness == 0)
        metrics->strikethroughThickness = metrics->underlineThickness;
1927 1928 1929 1930 1931

    if (tt_os2)
        IDWriteFontFace_ReleaseFontTable(fontface, os2_context);
    if (tt_head)
        IDWriteFontFace_ReleaseFontTable(fontface, head_context);
1932 1933
    if (tt_hhea)
        IDWriteFontFace_ReleaseFontTable(fontface, hhea_context);
1934 1935 1936 1937
    if (tt_post)
        IDWriteFontFace_ReleaseFontTable(fontface, post_context);
}

1938 1939
static void check_font_metrics(const WCHAR *nameW, BOOL has_metrics1, const DWRITE_FONT_METRICS *got,
    const DWRITE_FONT_METRICS1 *expected)
1940 1941 1942
{
    ok(got->designUnitsPerEm == expected->designUnitsPerEm, "font %s: designUnitsPerEm %u, expected %u\n",
        wine_dbgstr_w(nameW), got->designUnitsPerEm, expected->designUnitsPerEm);
1943 1944 1945 1946
    ok(got->ascent == expected->ascent, "font %s: ascent %u, expected %u\n", wine_dbgstr_w(nameW), got->ascent,
        expected->ascent);
    ok(got->descent == expected->descent, "font %s: descent %u, expected %u\n", wine_dbgstr_w(nameW), got->descent,
        expected->descent);
1947 1948
    ok(got->lineGap == expected->lineGap, "font %s: lineGap %d, expected %d\n", wine_dbgstr_w(nameW), got->lineGap,
        expected->lineGap);
1949 1950 1951 1952 1953 1954 1955 1956
    ok(got->underlinePosition == expected->underlinePosition, "font %s: underlinePosition %d, expected %d\n",
        wine_dbgstr_w(nameW), got->underlinePosition, expected->underlinePosition);
    ok(got->underlineThickness == expected->underlineThickness, "font %s: underlineThickness %u, "
        "expected %u\n", wine_dbgstr_w(nameW), got->underlineThickness, expected->underlineThickness);
    ok(got->strikethroughPosition == expected->strikethroughPosition, "font %s: strikethroughPosition %d, expected %d\n",
        wine_dbgstr_w(nameW), got->strikethroughPosition, expected->strikethroughPosition);
    ok(got->strikethroughThickness == expected->strikethroughThickness, "font %s: strikethroughThickness %u, "
        "expected %u\n", wine_dbgstr_w(nameW), got->strikethroughThickness, expected->strikethroughThickness);
1957 1958 1959 1960 1961

    if (has_metrics1) {
        const DWRITE_FONT_METRICS1 *m1 = (const DWRITE_FONT_METRICS1*)got;
        ok(m1->hasTypographicMetrics == expected->hasTypographicMetrics, "font %s: hasTypographicMetrics %d, "
            "expected %d\n", wine_dbgstr_w(nameW), m1->hasTypographicMetrics, expected->hasTypographicMetrics);
1962 1963 1964 1965 1966 1967 1968 1969
        ok(m1->glyphBoxLeft == expected->glyphBoxLeft, "font %s: glyphBoxLeft %d, expected %d\n", wine_dbgstr_w(nameW),
            m1->glyphBoxLeft, expected->glyphBoxLeft);
        ok(m1->glyphBoxTop == expected->glyphBoxTop, "font %s: glyphBoxTop %d, expected %d\n", wine_dbgstr_w(nameW),
            m1->glyphBoxTop, expected->glyphBoxTop);
        ok(m1->glyphBoxRight == expected->glyphBoxRight, "font %s: glyphBoxRight %d, expected %d\n", wine_dbgstr_w(nameW),
            m1->glyphBoxRight, expected->glyphBoxRight);
        ok(m1->glyphBoxBottom == expected->glyphBoxBottom, "font %s: glyphBoxBottom %d, expected %d\n", wine_dbgstr_w(nameW),
            m1->glyphBoxBottom, expected->glyphBoxBottom);
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986

        ok(m1->subscriptPositionX == expected->subscriptPositionX, "font %s: subscriptPositionX %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->subscriptPositionX, expected->subscriptPositionX);
        ok(m1->subscriptPositionY == expected->subscriptPositionY, "font %s: subscriptPositionY %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->subscriptPositionY, expected->subscriptPositionY);
        ok(m1->subscriptSizeX == expected->subscriptSizeX, "font %s: subscriptSizeX %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->subscriptSizeX, expected->subscriptSizeX);
        ok(m1->subscriptSizeY == expected->subscriptSizeY, "font %s: subscriptSizeY %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->subscriptSizeY, expected->subscriptSizeY);
        ok(m1->superscriptPositionX == expected->superscriptPositionX, "font %s: superscriptPositionX %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->superscriptPositionX, expected->superscriptPositionX);
        ok(m1->superscriptPositionY == expected->superscriptPositionY, "font %s: superscriptPositionY %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->superscriptPositionY, expected->superscriptPositionY);
        ok(m1->superscriptSizeX == expected->superscriptSizeX, "font %s: superscriptSizeX %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->superscriptSizeX, expected->superscriptSizeX);
        ok(m1->superscriptSizeY == expected->superscriptSizeY, "font %s: superscriptSizeY %d, expected %d\n",
            wine_dbgstr_w(nameW), m1->superscriptSizeY, expected->superscriptSizeY);
1987
    }
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
}

static void get_enus_string(IDWriteLocalizedStrings *strings, WCHAR *buff, UINT32 size)
{
    static const WCHAR enusW[] = {'e','n','-','u','s',0};
    BOOL exists = FALSE;
    UINT32 index;
    HRESULT hr;

    hr = IDWriteLocalizedStrings_FindLocaleName(strings, enusW, &index, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists, "got %d\n", exists);

    hr = IDWriteLocalizedStrings_GetString(strings, index, buff, size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
}

2005 2006
static void test_GetMetrics(void)
{
2007
    DWRITE_FONT_METRICS metrics, metrics2;
2008
    IDWriteFontCollection *syscollection;
2009
    IDWriteGdiInterop *interop;
2010
    IDWriteFontFace *fontface;
2011
    IDWriteFactory *factory;
2012
    OUTLINETEXTMETRICW otm;
2013
    IDWriteFontFile *file;
2014
    IDWriteFont1 *font1;
2015 2016
    IDWriteFont *font;
    LOGFONTW logfont;
2017
    UINT32 count, i;
2018 2019 2020
    HRESULT hr;
    HDC hdc;
    HFONT hfont;
2021
    ULONG ref;
2022 2023
    int ret;

2024 2025
    factory = create_factory();

2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    EXPECT_HR(hr, S_OK);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hfont = CreateFontIndirectW(&logfont);
    hdc = CreateCompatibleDC(0);
    SelectObject(hdc, hfont);

    otm.otmSize = sizeof(otm);
    ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
    ok(ret, "got %d\n", ret);
    DeleteDC(hdc);
    DeleteObject(hfont);

2049 2050
    if (0) /* crashes on native */
        IDWriteFont_GetMetrics(font, NULL);
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065

    memset(&metrics, 0, sizeof(metrics));
    IDWriteFont_GetMetrics(font, &metrics);

    ok(metrics.designUnitsPerEm != 0, "designUnitsPerEm %u\n", metrics.designUnitsPerEm);
    ok(metrics.ascent != 0, "ascent %u\n", metrics.ascent);
    ok(metrics.descent != 0, "descent %u\n", metrics.descent);
    ok(metrics.lineGap == 0, "lineGap %d\n", metrics.lineGap);
    ok(metrics.capHeight, "capHeight %u\n", metrics.capHeight);
    ok(metrics.xHeight != 0, "xHeight %u\n", metrics.xHeight);
    ok(metrics.underlinePosition < 0, "underlinePosition %d\n", metrics.underlinePosition);
    ok(metrics.underlineThickness != 0, "underlineThickness %u\n", metrics.underlineThickness);
    ok(metrics.strikethroughPosition > 0, "strikethroughPosition %d\n", metrics.strikethroughPosition);
    ok(metrics.strikethroughThickness != 0, "strikethroughThickness %u\n", metrics.strikethroughThickness);

2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&metrics, 0, sizeof(metrics));
    IDWriteFontFace_GetMetrics(fontface, &metrics);

    ok(metrics.designUnitsPerEm != 0, "designUnitsPerEm %u\n", metrics.designUnitsPerEm);
    ok(metrics.ascent != 0, "ascent %u\n", metrics.ascent);
    ok(metrics.descent != 0, "descent %u\n", metrics.descent);
    ok(metrics.lineGap == 0, "lineGap %d\n", metrics.lineGap);
    ok(metrics.capHeight, "capHeight %u\n", metrics.capHeight);
    ok(metrics.xHeight != 0, "xHeight %u\n", metrics.xHeight);
    ok(metrics.underlinePosition < 0, "underlinePosition %d\n", metrics.underlinePosition);
    ok(metrics.underlineThickness != 0, "underlineThickness %u\n", metrics.underlineThickness);
    ok(metrics.strikethroughPosition > 0, "strikethroughPosition %d\n", metrics.strikethroughPosition);
    ok(metrics.strikethroughThickness != 0, "strikethroughThickness %u\n", metrics.strikethroughThickness);

2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
    hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont1, (void**)&font1);
    if (hr == S_OK) {
        DWRITE_FONT_METRICS1 metrics1;
        IDWriteFontFace1 *fontface1;

        memset(&metrics1, 0, sizeof(metrics1));
        IDWriteFont1_GetMetrics(font1, &metrics1);

        ok(metrics1.designUnitsPerEm != 0, "designUnitsPerEm %u\n", metrics1.designUnitsPerEm);
        ok(metrics1.ascent != 0, "ascent %u\n", metrics1.ascent);
        ok(metrics1.descent != 0, "descent %u\n", metrics1.descent);
        ok(metrics1.lineGap == 0, "lineGap %d\n", metrics1.lineGap);
        ok(metrics1.capHeight, "capHeight %u\n", metrics1.capHeight);
        ok(metrics1.xHeight != 0, "xHeight %u\n", metrics1.xHeight);
        ok(metrics1.underlinePosition < 0, "underlinePosition %d\n", metrics1.underlinePosition);
        ok(metrics1.underlineThickness != 0, "underlineThickness %u\n", metrics1.underlineThickness);
        ok(metrics1.strikethroughPosition > 0, "strikethroughPosition %d\n", metrics1.strikethroughPosition);
        ok(metrics1.strikethroughThickness != 0, "strikethroughThickness %u\n", metrics1.strikethroughThickness);
        ok(metrics1.glyphBoxLeft < 0, "glyphBoxLeft %d\n", metrics1.glyphBoxLeft);
        ok(metrics1.glyphBoxTop > 0, "glyphBoxTop %d\n", metrics1.glyphBoxTop);
        ok(metrics1.glyphBoxRight > 0, "glyphBoxRight %d\n", metrics1.glyphBoxRight);
        ok(metrics1.glyphBoxBottom < 0, "glyphBoxBottom %d\n", metrics1.glyphBoxBottom);
        ok(metrics1.subscriptPositionY < 0, "subscriptPositionY %d\n", metrics1.subscriptPositionY);
        ok(metrics1.subscriptSizeX > 0, "subscriptSizeX %d\n", metrics1.subscriptSizeX);
        ok(metrics1.subscriptSizeY > 0, "subscriptSizeY %d\n", metrics1.subscriptSizeY);
        ok(metrics1.superscriptPositionY > 0, "superscriptPositionY %d\n", metrics1.superscriptPositionY);
        ok(metrics1.superscriptSizeX > 0, "superscriptSizeX %d\n", metrics1.superscriptSizeX);
        ok(metrics1.superscriptSizeY > 0, "superscriptSizeY %d\n", metrics1.superscriptSizeY);
        ok(!metrics1.hasTypographicMetrics, "hasTypographicMetrics %d\n", metrics1.hasTypographicMetrics);

        hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        memset(&metrics1, 0, sizeof(metrics1));
        IDWriteFontFace1_GetMetrics(fontface1, &metrics1);

        ok(metrics1.designUnitsPerEm != 0, "designUnitsPerEm %u\n", metrics1.designUnitsPerEm);
        ok(metrics1.ascent != 0, "ascent %u\n", metrics1.ascent);
        ok(metrics1.descent != 0, "descent %u\n", metrics1.descent);
        ok(metrics1.lineGap == 0, "lineGap %d\n", metrics1.lineGap);
        ok(metrics1.capHeight, "capHeight %u\n", metrics1.capHeight);
        ok(metrics1.xHeight != 0, "xHeight %u\n", metrics1.xHeight);
        ok(metrics1.underlinePosition < 0, "underlinePosition %d\n", metrics1.underlinePosition);
        ok(metrics1.underlineThickness != 0, "underlineThickness %u\n", metrics1.underlineThickness);
        ok(metrics1.strikethroughPosition > 0, "strikethroughPosition %d\n", metrics1.strikethroughPosition);
        ok(metrics1.strikethroughThickness != 0, "strikethroughThickness %u\n", metrics1.strikethroughThickness);
        ok(metrics1.glyphBoxLeft < 0, "glyphBoxLeft %d\n", metrics1.glyphBoxLeft);
        ok(metrics1.glyphBoxTop > 0, "glyphBoxTop %d\n", metrics1.glyphBoxTop);
        ok(metrics1.glyphBoxRight > 0, "glyphBoxRight %d\n", metrics1.glyphBoxRight);
        ok(metrics1.glyphBoxBottom < 0, "glyphBoxBottom %d\n", metrics1.glyphBoxBottom);
        ok(metrics1.subscriptPositionY < 0, "subscriptPositionY %d\n", metrics1.subscriptPositionY);
        ok(metrics1.subscriptSizeX > 0, "subscriptSizeX %d\n", metrics1.subscriptSizeX);
        ok(metrics1.subscriptSizeY > 0, "subscriptSizeY %d\n", metrics1.subscriptSizeY);
        ok(metrics1.superscriptPositionY > 0, "superscriptPositionY %d\n", metrics1.superscriptPositionY);
        ok(metrics1.superscriptSizeX > 0, "superscriptSizeX %d\n", metrics1.superscriptSizeX);
        ok(metrics1.superscriptSizeY > 0, "superscriptSizeY %d\n", metrics1.superscriptSizeY);
        ok(!metrics1.hasTypographicMetrics, "hasTypographicMetrics %d\n", metrics1.hasTypographicMetrics);

        IDWriteFontFace1_Release(fontface1);
        IDWriteFont1_Release(font1);
    }
    else
        win_skip("DWRITE_FONT_METRICS1 is not supported.\n");

2147
    IDWriteFontFace_Release(fontface);
2148 2149
    IDWriteFont_Release(font);
    IDWriteGdiInterop_Release(interop);
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186

    /* bold simulation affects returned font metrics */
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_NORMAL);

    /* create regulat Tahoma with bold simulation */
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    count = 1;
    hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFontFace_GetMetrics(fontface, &metrics);
    ok(IDWriteFontFace_GetSimulations(fontface) == 0, "wrong simulations flags\n");
    IDWriteFontFace_Release(fontface);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file,
        0, DWRITE_FONT_SIMULATIONS_BOLD, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFace_GetMetrics(fontface, &metrics2);
    ok(IDWriteFontFace_GetSimulations(fontface) == DWRITE_FONT_SIMULATIONS_BOLD, "wrong simulations flags\n");

    ok(metrics.ascent == metrics2.ascent, "got %u, %u\n", metrics2.ascent, metrics.ascent);
    ok(metrics.descent == metrics2.descent, "got %u, %u\n", metrics2.descent, metrics.descent);
    ok(metrics.lineGap == metrics2.lineGap, "got %d, %d\n", metrics2.lineGap, metrics.lineGap);
    ok(metrics.capHeight == metrics2.capHeight, "got %u, %u\n", metrics2.capHeight, metrics.capHeight);
    ok(metrics.xHeight == metrics2.xHeight, "got %u, %u\n", metrics2.xHeight, metrics.xHeight);
    ok(metrics.underlinePosition == metrics2.underlinePosition, "got %d, %d\n", metrics2.underlinePosition,
        metrics.underlinePosition);
    ok(metrics.underlineThickness == metrics2.underlineThickness, "got %u, %u\n", metrics2.underlineThickness,
        metrics.underlineThickness);
    ok(metrics.strikethroughPosition == metrics2.strikethroughPosition, "got %d, %d\n", metrics2.strikethroughPosition,
        metrics.strikethroughPosition);
    ok(metrics.strikethroughThickness == metrics2.strikethroughThickness, "got %u, %u\n", metrics2.strikethroughThickness,
        metrics.strikethroughThickness);

    IDWriteFontFile_Release(file);
2187
    IDWriteFontFace_Release(fontface);
2188 2189
    IDWriteFont_Release(font);

2190 2191 2192 2193 2194 2195
    /* test metrics for whole system collection */
    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
2196
        DWRITE_FONT_METRICS1 expected_metrics, metrics1;
2197
        IDWriteLocalizedStrings *names;
2198
        IDWriteFontFace1 *fontface1;
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
        IDWriteFontFamily *family;
        IDWriteFont *font;
        WCHAR nameW[256];

        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
            DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFont_CreateFontFace(font, &fontface);
        ok(hr == S_OK, "got 0x%08x\n", hr);

2213 2214 2215
        fontface1 = NULL;
        IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);

2216 2217 2218 2219 2220 2221 2222 2223 2224
        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_enus_string(names, nameW, sizeof(nameW)/sizeof(nameW[0]));

        IDWriteLocalizedStrings_Release(names);
        IDWriteFont_Release(font);

        get_expected_font_metrics(fontface, &expected_metrics);
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
        if (fontface1) {
            IDWriteFontFace1_GetMetrics(fontface1, &metrics1);
            check_font_metrics(nameW, TRUE, (const DWRITE_FONT_METRICS*)&metrics1, &expected_metrics);
        }
        else {
            IDWriteFontFace_GetMetrics(fontface, &metrics);
            check_font_metrics(nameW, FALSE, &metrics, &expected_metrics);
        }

        if (fontface1)
            IDWriteFontFace1_Release(fontface1);
2236 2237 2238 2239
        IDWriteFontFace_Release(fontface);
        IDWriteFontFamily_Release(family);
    }
    IDWriteFontCollection_Release(syscollection);
2240 2241
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
2242 2243
}

2244 2245
static void test_system_fontcollection(void)
{
2246
    IDWriteFontCollection *collection, *coll2;
2247
    IDWriteLocalFontFileLoader *localloader;
2248
    IDWriteFontCollection1 *collection1;
2249
    IDWriteFactory *factory, *factory2;
2250
    IDWriteFontFileLoader *loader;
2251
    IDWriteFontFamily *family;
2252 2253 2254
    IDWriteFontFace *fontface;
    IDWriteFontFile *file;
    IDWriteFont *font;
2255
    HRESULT hr;
2256
    ULONG ref;
2257 2258 2259
    UINT32 i;
    BOOL ret;

2260 2261
    factory = create_factory();

2262 2263 2264
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
    hr = IDWriteFactory_GetSystemFontCollection(factory, &coll2, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(coll2 == collection, "got %p, was %p\n", coll2, collection);
    IDWriteFontCollection_Release(coll2);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &coll2, TRUE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(coll2 == collection, "got %p, was %p\n", coll2, collection);
    IDWriteFontCollection_Release(coll2);

2275
    factory2 = create_factory();
2276 2277 2278 2279 2280 2281
    hr = IDWriteFactory_GetSystemFontCollection(factory2, &coll2, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(coll2 != collection, "got %p, was %p\n", coll2, collection);
    IDWriteFontCollection_Release(coll2);
    IDWriteFactory_Release(factory2);

2282 2283 2284
    i = IDWriteFontCollection_GetFontFamilyCount(collection);
    ok(i, "got %u\n", i);

2285 2286 2287 2288 2289 2290
    /* invalid index */
    family = (void*)0xdeadbeef;
    hr = IDWriteFontCollection_GetFontFamily(collection, i, &family);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(family == NULL, "got %p\n", family);

2291 2292 2293 2294 2295 2296 2297
    ret = FALSE;
    i = (UINT32)-1;
    hr = IDWriteFontCollection_FindFamilyName(collection, tahomaW, &i, &ret);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(ret, "got %d\n", ret);
    ok(i != (UINT32)-1, "got %u\n", i);

2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
    ret = FALSE;
    i = (UINT32)-1;
    hr = IDWriteFontCollection_FindFamilyName(collection, tahomaUppercaseW, &i, &ret);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(ret, "got %d\n", ret);
    ok(i != (UINT32)-1, "got %u\n", i);

    ret = FALSE;
    i = (UINT32)-1;
    hr = IDWriteFontCollection_FindFamilyName(collection, tahomaStrangecaseW, &i, &ret);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(ret, "got %d\n", ret);
    ok(i != (UINT32)-1, "got %u\n", i);

2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
    /* get back local file loader */
    hr = IDWriteFontCollection_GetFontFamily(collection, i, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFamily_Release(family);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    i = 1;
    file = NULL;
    hr = IDWriteFontFace_GetFiles(fontface, &i, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(file != NULL, "got %p\n", file);
2330
    IDWriteFontFace_Release(fontface);
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteLocalFontFileLoader, (void**)&localloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteLocalFontFileLoader_Release(localloader);

    /* local loader is not registered by default */
    hr = IDWriteFactory_RegisterFontFileLoader(factory, loader);
2342
    ok(hr == S_OK || broken(hr == DWRITE_E_ALREADYREGISTERED), "got 0x%08x\n", hr);
2343
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, loader);
2344
    ok(hr == S_OK || broken(hr == E_INVALIDARG), "got 0x%08x\n", hr);
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357

    /* try with a different factory */
    factory2 = create_factory();
    hr = IDWriteFactory_RegisterFontFileLoader(factory2, loader);
    ok(hr == S_OK || broken(hr == DWRITE_E_ALREADYREGISTERED), "got 0x%08x\n", hr);
    hr = IDWriteFactory_RegisterFontFileLoader(factory2, loader);
    ok(hr == DWRITE_E_ALREADYREGISTERED, "got 0x%08x\n", hr);
    hr = IDWriteFactory_UnregisterFontFileLoader(factory2, loader);
    ok(hr == S_OK || broken(hr == E_INVALIDARG), "got 0x%08x\n", hr);
    hr = IDWriteFactory_UnregisterFontFileLoader(factory2, loader);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    IDWriteFactory_Release(factory2);

2358
    IDWriteFontFileLoader_Release(loader);
2359

2360 2361 2362 2363 2364 2365
    ret = TRUE;
    i = 0;
    hr = IDWriteFontCollection_FindFamilyName(collection, blahW, &i, &ret);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!ret, "got %d\n", ret);
    ok(i == (UINT32)-1, "got %u\n", i);
2366

2367 2368
    hr = IDWriteFontCollection_QueryInterface(collection, &IID_IDWriteFontCollection1, (void**)&collection1);
    if (hr == S_OK) {
2369
        IDWriteFontSet *fontset, *fontset2;
2370
        IDWriteFontFamily1 *family1;
2371
        IDWriteFactory3 *factory3;
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381

        hr = IDWriteFontCollection1_QueryInterface(collection1, &IID_IDWriteFontCollection, (void**)&coll2);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(coll2 == collection, "got %p, %p\n", collection, coll2);
        IDWriteFontCollection_Release(coll2);

        family1 = (void*)0xdeadbeef;
        hr = IDWriteFontCollection1_GetFontFamily(collection1, ~0u, &family1);
        ok(hr == E_FAIL, "got 0x%08x\n", hr);
        ok(family1 == NULL, "got %p\n", family1);
2382

2383 2384 2385
        hr = IDWriteFontCollection1_GetFontFamily(collection1, 0, &family1);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFontFamily1_Release(family1);
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418

        /* system fontset */
        EXPECT_REF(collection1, 2);
        EXPECT_REF(factory, 2);
        hr = IDWriteFontCollection1_GetFontSet(collection1, &fontset);
    todo_wine
        ok(hr == S_OK, "Failed to get fontset, hr %#x.\n", hr);
    if (hr == S_OK) {
        EXPECT_REF(collection1, 2);
        EXPECT_REF(factory, 2);
        EXPECT_REF(fontset, 1);

        hr = IDWriteFontCollection1_GetFontSet(collection1, &fontset2);
        ok(hr == S_OK, "Failed to get fontset, hr %#x.\n", hr);
        ok(fontset != fontset2, "Expected new fontset instance.\n");
        EXPECT_REF(fontset2, 1);
        IDWriteFontSet_Release(fontset2);

        hr = IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory3, (void **)&factory3);
        ok(hr == S_OK, "Failed to get IDWriteFactory3 interface, hr %#x.\n", hr);

        EXPECT_REF(factory, 3);
        hr = IDWriteFactory3_GetSystemFontSet(factory3, &fontset2);
        ok(hr == S_OK, "Failed to get system font set, hr %#x.\n", hr);
        ok(fontset != fontset2, "Expected new fontset instance.\n");
        EXPECT_REF(fontset2, 1);
        EXPECT_REF(factory, 4);

        IDWriteFontSet_Release(fontset2);
        IDWriteFontSet_Release(fontset);

        IDWriteFactory3_Release(factory3);
    }
2419 2420 2421 2422 2423
        IDWriteFontCollection1_Release(collection1);
    }
    else
        win_skip("IDWriteFontCollection1 is not supported.\n");

2424 2425 2426 2427
    ref = IDWriteFontCollection_Release(collection);
    ok(ref == 0, "collection not released, %u\n", ref);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
2428 2429
}

2430
static void get_logfont_from_font(IDWriteFont *font, LOGFONTW *logfont)
2431
{
2432 2433
    void *os2_context, *head_context;
    IDWriteLocalizedStrings *names;
2434
    DWRITE_FONT_SIMULATIONS sim;
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
    IDWriteFontFace *fontface;
    const TT_OS2_V2 *tt_os2;
    DWRITE_FONT_STYLE style;
    const TT_HEAD *tt_head;
    LONG weight;
    UINT32 size;
    BOOL exists;
    HRESULT hr;

    /* These are rendering time properties. */
    logfont->lfHeight = 0;
    logfont->lfWidth = 0;
    logfont->lfEscapement = 0;
    logfont->lfOrientation = 0;
    logfont->lfUnderline = 0;
    logfont->lfStrikeOut = 0;

    logfont->lfWeight = 0;
    logfont->lfItalic = 0;

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "Failed to create font face, %#x\n", hr);

    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_0S2_TAG, (const void **)&tt_os2, &size,
        &os2_context, &exists);
    ok(hr == S_OK, "Failed to get OS/2 table, %#x\n", hr);

    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_HEAD_TAG, (const void **)&tt_head, &size,
        &head_context, &exists);
    ok(hr == S_OK, "Failed to get head table, %#x\n", hr);

    sim = IDWriteFont_GetSimulations(font);

    /* lfWeight */
    weight = FW_REGULAR;
    if (tt_os2) {
        USHORT usWeightClass = GET_BE_WORD(tt_os2->usWeightClass);

        if (usWeightClass >= 1 && usWeightClass <= 9)
            usWeightClass *= 100;

        if (usWeightClass > DWRITE_FONT_WEIGHT_ULTRA_BLACK)
            weight = DWRITE_FONT_WEIGHT_ULTRA_BLACK;
        else if (usWeightClass > 0)
            weight = usWeightClass;
    }
    else if (tt_head) {
        USHORT macStyle = GET_BE_WORD(tt_head->macStyle);
        if (macStyle & TT_HEAD_MACSTYLE_BOLD)
            weight = DWRITE_FONT_WEIGHT_BOLD;
    }
    if (sim & DWRITE_FONT_SIMULATIONS_BOLD)
        weight += (FW_BOLD - FW_REGULAR) / 2 + 1;
    logfont->lfWeight = weight;

    /* lfItalic */
    if (IDWriteFont_GetSimulations(font) & DWRITE_FONT_SIMULATIONS_OBLIQUE)
        logfont->lfItalic = 1;

    style = IDWriteFont_GetStyle(font);
    if (!logfont->lfItalic && ((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE))) {
        if (tt_os2) {
            USHORT fsSelection = GET_BE_WORD(tt_os2->fsSelection);
            logfont->lfItalic = !!(fsSelection & OS2_FSSELECTION_ITALIC);
        }
        else if (tt_head) {
            USHORT macStyle = GET_BE_WORD(tt_head->macStyle);
            logfont->lfItalic = !!(macStyle & TT_HEAD_MACSTYLE_ITALIC);
        }
    }

    /* lfFaceName */
    exists = FALSE;
    logfont->lfFaceName[0] = 0;
    hr = IDWriteFont_GetInformationalStrings(font, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, &names, &exists);
    if (SUCCEEDED(hr)) {
        if (exists) {
            static const WCHAR enusW[] = {'e','n','-','u','s',0};
            WCHAR localeW[LOCALE_NAME_MAX_LENGTH];
            UINT32 index;

            /* Fallback to en-us if there's no string for user locale. */
            exists = FALSE;
            if (GetSystemDefaultLocaleName(localeW, sizeof(localeW)/sizeof(WCHAR)))
                IDWriteLocalizedStrings_FindLocaleName(names, localeW, &index, &exists);

            if (!exists)
                IDWriteLocalizedStrings_FindLocaleName(names, enusW, &index, &exists);

            if (exists)
                IDWriteLocalizedStrings_GetString(names, index, logfont->lfFaceName, sizeof(logfont->lfFaceName)/sizeof(WCHAR));
        }

        IDWriteLocalizedStrings_Release(names);
    }

    if (tt_os2)
        IDWriteFontFace_ReleaseFontTable(fontface, os2_context);
    if (tt_head)
        IDWriteFontFace_ReleaseFontTable(fontface, head_context);
    IDWriteFontFace_Release(fontface);
}

static void test_ConvertFontFaceToLOGFONT(void)
{
2540
    IDWriteFontCollection *collection;
2541 2542
    IDWriteGdiInterop *interop;
    IDWriteFontFace *fontface;
2543
    IDWriteFactory *factory;
2544
    LOGFONTW logfont;
2545
    UINT32 count, i;
2546
    HRESULT hr;
2547
    ULONG ref;
2548

2549 2550
    factory = create_factory();

2551 2552 2553
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

2554 2555 2556 2557 2558 2559 2560 2561 2562
if (0) /* crashes on native */
{
    hr = IDWriteGdiInterop_ConvertFontFaceToLOGFONT(interop, NULL, NULL);
    hr = IDWriteGdiInterop_ConvertFontFaceToLOGFONT(interop, fontface, NULL);
}
    memset(&logfont, 0xcc, sizeof(logfont));
    hr = IDWriteGdiInterop_ConvertFontFaceToLOGFONT(interop, NULL, &logfont);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(logfont.lfFaceName[0] == 0, "got face name %s\n", wine_dbgstr_w(logfont.lfFaceName));
2563

2564
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
2565 2566
    ok(hr == S_OK, "got 0x%08x\n", hr);

2567 2568 2569 2570 2571 2572 2573 2574 2575
    count = IDWriteFontCollection_GetFontFamilyCount(collection);
    for (i = 0; i < count; i++) {
        WCHAR nameW[128], familynameW[64], facenameW[64];
        IDWriteLocalizedStrings *names;
        DWRITE_FONT_SIMULATIONS sim;
        IDWriteFontFamily *family;
        UINT32 font_count, j;
        IDWriteFont *font;
        LOGFONTW lf;
2576

2577 2578
        hr = IDWriteFontCollection_GetFontFamily(collection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);
2579

2580 2581
        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);
2582

2583 2584
        get_enus_string(names, familynameW, sizeof(familynameW)/sizeof(familynameW[0]));
        IDWriteLocalizedStrings_Release(names);
2585

2586
        font_count = IDWriteFontFamily_GetFontCount(family);
2587

2588 2589 2590
        for (j = 0; j < font_count; j++) {
            static const WCHAR spaceW[] = {' ', 0};
            IDWriteFontFace *fontface;
2591

2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
            hr = IDWriteFontFamily_GetFont(family, j, &font);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            hr = IDWriteFont_GetFaceNames(font, &names);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            get_enus_string(names, facenameW, sizeof(facenameW)/sizeof(facenameW[0]));
            IDWriteLocalizedStrings_Release(names);

            lstrcpyW(nameW, familynameW);
            lstrcatW(nameW, spaceW);
            lstrcatW(nameW, facenameW);

            hr = IDWriteFont_CreateFontFace(font, &fontface);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            memset(&logfont, 0xcc, sizeof(logfont));
            hr = IDWriteGdiInterop_ConvertFontFaceToLOGFONT(interop, fontface, &logfont);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            sim = IDWriteFontFace_GetSimulations(fontface);
            get_logfont_from_font(font, &lf);
2614

2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
            ok(logfont.lfWeight == lf.lfWeight, "%s: unexpected lfWeight %d, expected lfWeight %d, font weight %d, "
                "bold simulation %s\n", wine_dbgstr_w(nameW), logfont.lfWeight, lf.lfWeight, IDWriteFont_GetWeight(font),
                sim & DWRITE_FONT_SIMULATIONS_BOLD ? "yes" : "no");
            ok(logfont.lfItalic == lf.lfItalic, "%s: unexpected italic flag %d, oblique simulation %s\n",
                wine_dbgstr_w(nameW), logfont.lfItalic, sim & DWRITE_FONT_SIMULATIONS_OBLIQUE ? "yes" : "no");
            ok(!lstrcmpW(logfont.lfFaceName, lf.lfFaceName), "%s: unexpected facename %s, expected %s\n",
                wine_dbgstr_w(nameW), wine_dbgstr_w(logfont.lfFaceName), wine_dbgstr_w(lf.lfFaceName));

            ok(logfont.lfOutPrecision == OUT_OUTLINE_PRECIS, "%s: unexpected output precision %d\n", wine_dbgstr_w(nameW),
                logfont.lfOutPrecision);
            ok(logfont.lfClipPrecision == CLIP_DEFAULT_PRECIS, "%s: unexpected clipping precision %d\n", wine_dbgstr_w(nameW),
                logfont.lfClipPrecision);
            ok(logfont.lfQuality == DEFAULT_QUALITY, "%s: unexpected quality %d\n", wine_dbgstr_w(nameW), logfont.lfQuality);
            ok(logfont.lfPitchAndFamily == DEFAULT_PITCH, "%s: unexpected pitch %d\n", wine_dbgstr_w(nameW),
                logfont.lfPitchAndFamily);

            IDWriteFontFace_Release(fontface);
            IDWriteFont_Release(font);
        }

        IDWriteFontFamily_Release(family);
    }

    IDWriteFontCollection_Release(collection);
2639
    IDWriteGdiInterop_Release(interop);
2640 2641
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
2642 2643
}

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
static HRESULT WINAPI fontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileEnumerator))
    {
        *obj = iface;
        IDWriteFontFileEnumerator_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI fontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
{
    return 2;
}

static ULONG WINAPI fontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
{
    return 1;
}

static HRESULT WINAPI fontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
{
    *file = NULL;
    return E_FAIL;
}

static HRESULT WINAPI fontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
{
    *current = FALSE;
    return S_OK;
}

static const struct IDWriteFontFileEnumeratorVtbl dwritefontfileenumeratorvtbl =
{
    fontfileenumerator_QueryInterface,
    fontfileenumerator_AddRef,
    fontfileenumerator_Release,
    fontfileenumerator_MoveNext,
    fontfileenumerator_GetCurrentFontFile,
};

2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
struct collection_loader
{
    IDWriteFontCollectionLoader IDWriteFontCollectionLoader_iface;
    LONG ref;
};

static inline struct collection_loader *impl_from_IDWriteFontCollectionLoader(IDWriteFontCollectionLoader *iface)
{
    return CONTAINING_RECORD(iface, struct collection_loader, IDWriteFontCollectionLoader_iface);
}

2697 2698
static HRESULT WINAPI fontcollectionloader_QueryInterface(IDWriteFontCollectionLoader *iface, REFIID riid, void **obj)
{
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
    struct collection_loader *loader = impl_from_IDWriteFontCollectionLoader(iface);

    if (IsEqualIID(&IID_IDWriteFontCollectionLoader, riid) ||
            IsEqualIID(&IID_IUnknown, riid))
    {
        *obj = &loader->IDWriteFontCollectionLoader_iface;
        IDWriteFontCollectionLoader_AddRef(iface);
        return S_OK;
    }

    *obj = NULL;
    return E_NOINTERFACE;
2711 2712 2713 2714
}

static ULONG WINAPI fontcollectionloader_AddRef(IDWriteFontCollectionLoader *iface)
{
2715 2716
    struct collection_loader *loader = impl_from_IDWriteFontCollectionLoader(iface);
    return InterlockedIncrement(&loader->ref);
2717 2718 2719 2720
}

static ULONG WINAPI fontcollectionloader_Release(IDWriteFontCollectionLoader *iface)
{
2721 2722 2723 2724 2725 2726 2727
    struct collection_loader *loader = impl_from_IDWriteFontCollectionLoader(iface);
    ULONG ref = InterlockedDecrement(&loader->ref);

    if (!ref)
        heap_free(loader);

    return ref;
2728 2729
}

2730 2731
static HRESULT WINAPI fontcollectionloader_CreateEnumeratorFromKey(IDWriteFontCollectionLoader *iface, IDWriteFactory *factory, const void *key,
    UINT32 key_size, IDWriteFontFileEnumerator **ret)
2732
{
2733 2734
    static IDWriteFontFileEnumerator enumerator = { &dwritefontfileenumeratorvtbl };
    *ret = &enumerator;
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
    return S_OK;
}

static const struct IDWriteFontCollectionLoaderVtbl dwritefontcollectionloadervtbl = {
    fontcollectionloader_QueryInterface,
    fontcollectionloader_AddRef,
    fontcollectionloader_Release,
    fontcollectionloader_CreateEnumeratorFromKey
};

2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
static IDWriteFontCollectionLoader *create_collection_loader(void)
{
    struct collection_loader *loader = heap_alloc(sizeof(*loader));

    loader->IDWriteFontCollectionLoader_iface.lpVtbl = &dwritefontcollectionloadervtbl;
    loader->ref = 1;

    return &loader->IDWriteFontCollectionLoader_iface;
}

2755 2756
static void test_CustomFontCollection(void)
{
2757
    static const WCHAR fontnameW[] = {'w','i','n','e','_','t','e','s','t',0};
2758
    IDWriteFontCollectionLoader *loader, *loader2, *loader3;
2759 2760 2761 2762 2763 2764 2765 2766
    IDWriteFontCollection *font_collection = NULL;
    static IDWriteFontFileLoader rloader = { &resourcefontfileloadervtbl };
    struct test_fontcollectionloader resource_collection = { { &resourcecollectionloadervtbl }, &rloader };
    IDWriteFontFamily *family, *family2, *family3;
    IDWriteFontFace *idfontface, *idfontface2;
    IDWriteFontFile *fontfile, *fontfile2;
    IDWriteLocalizedStrings *string;
    IDWriteFont *idfont, *idfont2;
2767
    IDWriteFactory *factory;
2768 2769
    UINT32 index, count;
    BOOL exists;
2770
    HRESULT hr;
2771
    HRSRC font;
2772
    ULONG ref;
2773

2774 2775
    factory = create_factory();

2776 2777 2778 2779
    loader = create_collection_loader();
    loader2 = create_collection_loader();
    loader3 = create_collection_loader();

2780 2781 2782 2783 2784 2785
    hr = IDWriteFactory_RegisterFontCollectionLoader(factory, NULL);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = IDWriteFactory_UnregisterFontCollectionLoader(factory, NULL);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

2786 2787 2788 2789
    EXPECT_REF(loader, 1);
    EXPECT_REF(loader2, 1);

    hr = IDWriteFactory_RegisterFontCollectionLoader(factory, loader);
2790
    ok(hr == S_OK, "got 0x%08x\n", hr);
2791
    hr = IDWriteFactory_RegisterFontCollectionLoader(factory, loader2);
2792
    ok(hr == S_OK, "got 0x%08x\n", hr);
2793
    hr = IDWriteFactory_RegisterFontCollectionLoader(factory, loader);
2794 2795
    ok(hr == DWRITE_E_ALREADYREGISTERED, "got 0x%08x\n", hr);

2796 2797 2798
    EXPECT_REF(loader, 2);
    EXPECT_REF(loader2, 2);

2799 2800 2801 2802 2803
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFactory_RegisterFontCollectionLoader(factory, &resource_collection.IDWriteFontFileCollectionLoader_iface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

2804
    /* Loader wasn't registered. */
2805
    font_collection = (void*)0xdeadbeef;
2806
    hr = IDWriteFactory_CreateCustomFontCollection(factory, loader3, "Billy", 6, &font_collection);
2807
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2808
    ok(font_collection == NULL, "got %p\n", font_collection);
2809

2810 2811
    EXPECT_REF(factory, 1);
    hr = IDWriteFactory_CreateCustomFontCollection(factory, loader, "Billy", 6, &font_collection);
2812
    ok(hr == S_OK, "got 0x%08x\n", hr);
2813 2814 2815
todo_wine
    EXPECT_REF(factory, 1);
    EXPECT_REF(loader, 2);
2816 2817
    IDWriteFontCollection_Release(font_collection);

2818
    hr = IDWriteFactory_CreateCustomFontCollection(factory, loader2, "Billy", 6, &font_collection);
2819 2820 2821
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontCollection_Release(font_collection);

2822
    font_collection = (void*)0xdeadbeef;
2823 2824
    hr = IDWriteFactory_CreateCustomFontCollection(factory, (IDWriteFontCollectionLoader*)0xdeadbeef, "Billy", 6, &font_collection);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2825
    ok(font_collection == NULL, "got %p\n", font_collection);
2826 2827 2828 2829 2830 2831 2832

    font = FindResourceA(GetModuleHandleA(NULL), (LPCSTR)MAKEINTRESOURCE(1), (LPCSTR)RT_RCDATA);
    ok(font != NULL, "Failed to find font resource\n");

    hr = IDWriteFactory_CreateCustomFontCollection(factory, &resource_collection.IDWriteFontFileCollectionLoader_iface,
        &font, sizeof(HRSRC), &font_collection);
    ok(hr == S_OK, "got 0x%08x\n",hr);
2833
    EXPECT_REF(font_collection, 1);
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870

    index = 1;
    exists = FALSE;
    hr = IDWriteFontCollection_FindFamilyName(font_collection, fontnameW, &index, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(index == 0, "got index %i\n", index);
    ok(exists, "got exists %i\n", exists);

    count = IDWriteFontCollection_GetFontFamilyCount(font_collection);
    ok(count == 1, "got %u\n", count);

    family = NULL;
    hr = IDWriteFontCollection_GetFontFamily(font_collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(family, 1);

    family2 = NULL;
    hr = IDWriteFontCollection_GetFontFamily(font_collection, 0, &family2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(family2, 1);
    ok(family != family2, "got %p, %p\n", family, family2);

    hr = IDWriteFontFamily_GetFont(family, 0, &idfont);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(idfont, 1);
    EXPECT_REF(family, 2);
    hr = IDWriteFontFamily_GetFont(family, 0, &idfont2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(idfont2, 1);
    EXPECT_REF(family, 3);
    ok(idfont != idfont2, "got %p, %p\n", idfont, idfont2);
    IDWriteFont_Release(idfont2);

    hr = IDWriteFont_GetInformationalStrings(idfont, DWRITE_INFORMATIONAL_STRING_COPYRIGHT_NOTICE, &string, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists, "got %d\n", exists);
    EXPECT_REF(string, 1);
2871
    IDWriteLocalizedStrings_Release(string);
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889

    family3 = NULL;
    hr = IDWriteFont_GetFontFamily(idfont, &family3);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(family, 3);
    ok(family == family3, "got %p, %p\n", family, family3);
    IDWriteFontFamily_Release(family3);

    idfontface = NULL;
    hr = IDWriteFont_CreateFontFace(idfont, &idfontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(idfont, 1);

    idfont2 = NULL;
    hr = IDWriteFontFamily_GetFont(family2, 0, &idfont2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(idfont2, 1);
    EXPECT_REF(idfont, 1);
2890
    ok(idfont2 != idfont, "Font instances should not match\n");
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917

    idfontface2 = NULL;
    hr = IDWriteFont_CreateFontFace(idfont2, &idfontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(idfontface2 == idfontface, "fontfaces should match\n");

    index = 1;
    fontfile = NULL;
    hr = IDWriteFontFace_GetFiles(idfontface, &index, &fontfile);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    index = 1;
    fontfile2 = NULL;
    hr = IDWriteFontFace_GetFiles(idfontface2, &index, &fontfile2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontfile == fontfile2, "fontfiles should match\n");

    IDWriteFont_Release(idfont);
    IDWriteFont_Release(idfont2);
    IDWriteFontFile_Release(fontfile);
    IDWriteFontFile_Release(fontfile2);
    IDWriteFontFace_Release(idfontface);
    IDWriteFontFace_Release(idfontface2);
    IDWriteFontFamily_Release(family2);
    IDWriteFontFamily_Release(family);
    IDWriteFontCollection_Release(font_collection);

2918
    hr = IDWriteFactory_UnregisterFontCollectionLoader(factory, loader);
2919
    ok(hr == S_OK, "got 0x%08x\n", hr);
2920
    hr = IDWriteFactory_UnregisterFontCollectionLoader(factory, loader);
2921
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
2922
    hr = IDWriteFactory_UnregisterFontCollectionLoader(factory, loader2);
2923
    ok(hr == S_OK, "got 0x%08x\n", hr);
2924 2925 2926 2927
    hr = IDWriteFactory_UnregisterFontCollectionLoader(factory, &resource_collection.IDWriteFontFileCollectionLoader_iface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
2928

2929 2930 2931 2932 2933 2934
    IDWriteFontCollectionLoader_Release(loader);
    IDWriteFontCollectionLoader_Release(loader2);
    IDWriteFontCollectionLoader_Release(loader3);

    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
2935 2936
}

2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
static HRESULT WINAPI fontfileloader_QueryInterface(IDWriteFontFileLoader *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader))
    {
        *obj = iface;
        IDWriteFontFileLoader_AddRef(iface);
        return S_OK;
    }

    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI fontfileloader_AddRef(IDWriteFontFileLoader *iface)
{
    return 2;
}

static ULONG WINAPI fontfileloader_Release(IDWriteFontFileLoader *iface)
{
    return 1;
}

2960 2961
static HRESULT WINAPI fontfileloader_CreateStreamFromKey(IDWriteFontFileLoader *iface, const void *ref_key, UINT32 key_size,
    IDWriteFontFileStream **stream)
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
{
    return 0x8faecafe;
}

static const struct IDWriteFontFileLoaderVtbl dwritefontfileloadervtbl = {
    fontfileloader_QueryInterface,
    fontfileloader_AddRef,
    fontfileloader_Release,
    fontfileloader_CreateStreamFromKey
};

2973
static void test_CreateCustomFontFileReference(void)
2974 2975 2976
{
    IDWriteFontFileLoader floader = { &dwritefontfileloadervtbl };
    IDWriteFontFileLoader floader2 = { &dwritefontfileloadervtbl };
2977
    IDWriteFontFileLoader floader3 = { &dwritefontfileloadervtbl };
2978
    IDWriteFactory *factory, *factory2;
2979
    IDWriteFontFileLoader *loader;
2980 2981 2982 2983 2984 2985
    IDWriteFontFile *file, *file2;
    BOOL support;
    DWRITE_FONT_FILE_TYPE file_type;
    DWRITE_FONT_FACE_TYPE face_type;
    UINT32 count;
    IDWriteFontFace *face, *face2;
2986
    HRESULT hr;
2987
    HRSRC fontrsrc;
2988
    UINT32 codePoints[1] = {0xa8};
2989
    UINT16 indices[2];
2990 2991 2992
    const void *key;
    UINT32 key_size;
    WCHAR *path;
2993
    ULONG ref;
2994 2995

    path = create_testfontfile(test_fontfile);
2996

2997
    factory = create_factory();
2998
    factory2 = create_factory();
2999

3000
if (0) { /* crashes on win10 */
3001 3002
    hr = IDWriteFactory_RegisterFontFileLoader(factory, NULL);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3003
}
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020
    /* local loader is accepted too */
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateCustomFontFileReference(factory, key, key_size, loader, &file2);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFontFile_Release(file2);
    IDWriteFontFile_Release(file);
    IDWriteFontFileLoader_Release(loader);

3021 3022 3023 3024 3025 3026
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &floader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &floader2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &floader);
    ok(hr == DWRITE_E_ALREADYREGISTERED, "got 0x%08x\n", hr);
3027 3028
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3029

3030 3031
    file = NULL;
    hr = IDWriteFactory_CreateCustomFontFileReference(factory, "test", 4, &floader, &file);
3032
    ok(hr == S_OK, "got 0x%08x\n", hr);
3033
    IDWriteFontFile_Release(file);
3034

3035
    file = (void*)0xdeadbeef;
3036
    hr = IDWriteFactory_CreateCustomFontFileReference(factory, "test", 4, &floader3, &file);
3037
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3038
    ok(file == NULL, "got %p\n", file);
3039

3040
    file = (void*)0xdeadbeef;
3041
    hr = IDWriteFactory_CreateCustomFontFileReference(factory, "test", 4, NULL, &file);
3042
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3043
    ok(file == NULL, "got %p\n", file);
3044

3045 3046
    file = NULL;
    hr = IDWriteFactory_CreateCustomFontFileReference(factory, "test", 4, &floader, &file);
3047
    ok(hr == S_OK, "got 0x%08x\n", hr);
3048 3049 3050 3051 3052

    file_type = DWRITE_FONT_FILE_TYPE_TRUETYPE;
    face_type = DWRITE_FONT_FACE_TYPE_TRUETYPE;
    support = TRUE;
    count = 1;
3053 3054
    hr = IDWriteFontFile_Analyze(file, &support, &file_type, &face_type, &count);
    ok(hr == 0x8faecafe, "got 0x%08x\n", hr);
3055
    ok(support == FALSE, "got %i\n", support);
3056 3057
    ok(file_type == DWRITE_FONT_FILE_TYPE_UNKNOWN, "got %i\n", file_type);
    ok(face_type == DWRITE_FONT_FACE_TYPE_UNKNOWN, "got %i\n", face_type);
3058
    ok(count == 0, "got %i\n", count);
3059

3060
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_CFF, 1, &file, 0, 0, &face);
3061
    ok(hr == 0x8faecafe, "got 0x%08x\n", hr);
3062
    IDWriteFontFile_Release(file);
3063

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
    fontrsrc = FindResourceA(GetModuleHandleA(NULL), (LPCSTR)MAKEINTRESOURCE(1), (LPCSTR)RT_RCDATA);
    ok(fontrsrc != NULL, "Failed to find font resource\n");

    hr = IDWriteFactory_CreateCustomFontFileReference(factory, &fontrsrc, sizeof(HRSRC), &rloader, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    file_type = DWRITE_FONT_FILE_TYPE_UNKNOWN;
    face_type = DWRITE_FONT_FACE_TYPE_UNKNOWN;
    support = FALSE;
    count = 0;
    hr = IDWriteFontFile_Analyze(file, &support, &file_type, &face_type, &count);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(support == TRUE, "got %i\n", support);
    ok(file_type == DWRITE_FONT_FILE_TYPE_TRUETYPE, "got %i\n", file_type);
    ok(face_type == DWRITE_FONT_FACE_TYPE_TRUETYPE, "got %i\n", face_type);
    ok(count == 1, "got %i\n", count);

    /* invalid index */
3082
    face = (void*)0xdeadbeef;
3083 3084
    hr = IDWriteFactory_CreateFontFace(factory, face_type, 1, &file, 1, DWRITE_FONT_SIMULATIONS_NONE, &face);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3085
    ok(face == NULL, "got %p\n", face);
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095

    hr = IDWriteFactory_CreateFontFace(factory, face_type, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &face);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, face_type, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &face2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    /* fontface instances are reused starting with win7 */
    ok(face == face2 || broken(face != face2), "got %p, %p\n", face, face2);
    IDWriteFontFace_Release(face2);

3096
    /* file was created with different factory */
3097
    face2 = NULL;
3098
    hr = IDWriteFactory_CreateFontFace(factory2, face_type, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &face2);
3099 3100
todo_wine
    ok(hr == S_OK, "got 0x%08x\n", hr);
3101
if (face2) {
3102
    IDWriteFontFace_Release(face2);
3103
}
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115
    file2 = NULL;
    hr = IDWriteFactory_CreateCustomFontFileReference(factory, &fontrsrc, sizeof(HRSRC), &rloader, &file2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(file != file2, "got %p, %p\n", file, file2);

    hr = IDWriteFactory_CreateFontFace(factory, face_type, 1, &file2, 0, DWRITE_FONT_SIMULATIONS_NONE, &face2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    /* fontface instances are reused starting with win7 */
    ok(face == face2 || broken(face != face2), "got %p, %p\n", face, face2);
    IDWriteFontFace_Release(face2);
    IDWriteFontFile_Release(file2);

3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
    hr = IDWriteFontFace_GetGlyphIndices(face, NULL, 0, NULL);
    ok(hr == E_INVALIDARG || broken(hr == S_OK) /* win8 */, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_GetGlyphIndices(face, codePoints, 0, NULL);
    ok(hr == E_INVALIDARG || broken(hr == S_OK) /* win8 */, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_GetGlyphIndices(face, codePoints, 0, indices);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_GetGlyphIndices(face, NULL, 0, indices);
    ok(hr == E_INVALIDARG || broken(hr == S_OK) /* win8 */, "got 0x%08x\n", hr);

    indices[0] = indices[1] = 11;
    hr = IDWriteFontFace_GetGlyphIndices(face, NULL, 1, indices);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(indices[0] == 0, "got index %i\n", indices[0]);
    ok(indices[1] == 11, "got index %i\n", indices[1]);

3134 3135
    if (0) /* crashes on native */
        hr = IDWriteFontFace_GetGlyphIndices(face, NULL, 1, NULL);
3136

3137 3138
    hr = IDWriteFontFace_GetGlyphIndices(face, codePoints, 1, indices);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3139
    ok(indices[0] == 7, "Unexpected glyph index, %u.\n", indices[0]);
3140 3141
    IDWriteFontFace_Release(face);
    IDWriteFontFile_Release(file);
3142

3143 3144 3145 3146 3147 3148
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &floader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &floader);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &floader2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3149 3150
    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3151

3152 3153 3154 3155
    ref = IDWriteFactory_Release(factory2);
    ok(ref == 0, "factory not released, %u\n", ref);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3156
    DELETE_FONTFILE(path);
3157 3158
}

3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
static void test_CreateFontFileReference(void)
{
    HRESULT hr;
    IDWriteFontFile *ffile = NULL;
    BOOL support;
    DWRITE_FONT_FILE_TYPE type;
    DWRITE_FONT_FACE_TYPE face;
    UINT32 count;
    IDWriteFontFace *fface = NULL;
    IDWriteFactory *factory;
3169
    WCHAR *path;
3170
    ULONG ref;
3171

3172
    path = create_testfontfile(test_fontfile);
3173 3174
    factory = create_factory();

3175 3176 3177 3178 3179
    ffile = (void*)0xdeadbeef;
    hr = IDWriteFactory_CreateFontFileReference(factory, NULL, NULL, &ffile);
    ok(hr == E_INVALIDARG, "got 0x%08x\n",hr);
    ok(ffile == NULL, "got %p\n", ffile);

3180
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &ffile);
3181 3182
    ok(hr == S_OK, "got 0x%08x\n",hr);

3183 3184 3185 3186 3187
    support = FALSE;
    type = DWRITE_FONT_FILE_TYPE_UNKNOWN;
    face = DWRITE_FONT_FACE_TYPE_CFF;
    count = 0;
    hr = IDWriteFontFile_Analyze(ffile, &support, &type, &face, &count);
3188 3189 3190 3191 3192 3193
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(support == TRUE, "got %i\n", support);
    ok(type == DWRITE_FONT_FILE_TYPE_TRUETYPE, "got %i\n", type);
    ok(face == DWRITE_FONT_FACE_TYPE_TRUETYPE, "got %i\n", face);
    ok(count == 1, "got %i\n", count);

3194 3195
    hr = IDWriteFactory_CreateFontFace(factory, face, 1, &ffile, 0, DWRITE_FONT_SIMULATIONS_NONE, &fface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3196 3197 3198

    IDWriteFontFace_Release(fface);
    IDWriteFontFile_Release(ffile);
3199 3200
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3201

3202
    DELETE_FONTFILE(path);
3203 3204
}

3205 3206 3207 3208 3209
static void test_shared_isolated(void)
{
    IDWriteFactory *isolated, *isolated2;
    IDWriteFactory *shared, *shared2;
    HRESULT hr;
3210
    ULONG ref;
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224

    /* invalid type */
    shared = NULL;
    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED+1, &IID_IDWriteFactory, (IUnknown**)&shared);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(shared != NULL, "got %p\n", shared);
    IDWriteFactory_Release(shared);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IDWriteFactory, (IUnknown**)&shared);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IDWriteFactory, (IUnknown**)&shared2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(shared == shared2, "got %p, and %p\n", shared, shared2);
3225 3226 3227 3228 3229
    IDWriteFactory_Release(shared2);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IUnknown, (IUnknown**)&shared2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(shared == shared2, "got %p, and %p\n", shared, shared2);
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247

    IDWriteFactory_Release(shared);
    IDWriteFactory_Release(shared2);

    /* we got 2 references, released 2 - still same pointer is returned */
    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IDWriteFactory, (IUnknown**)&shared2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(shared == shared2, "got %p, and %p\n", shared, shared2);
    IDWriteFactory_Release(shared2);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IDWriteFactory, (IUnknown**)&isolated);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IDWriteFactory, (IUnknown**)&isolated2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(isolated != isolated2, "got %p, and %p\n", isolated, isolated2);
    IDWriteFactory_Release(isolated2);

3248 3249 3250 3251
    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IUnknown, (IUnknown**)&isolated2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFactory_Release(isolated2);

3252 3253 3254 3255
    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED+1, &IID_IDWriteFactory, (IUnknown**)&isolated2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(shared != isolated2, "got %p, and %p\n", shared, isolated2);

3256 3257 3258 3259
    ref = IDWriteFactory_Release(isolated);
    ok(ref == 0, "factory not released, %u\n", ref);
    ref = IDWriteFactory_Release(isolated2);
    ok(ref == 0, "factory not released, %u\n", ref);
3260 3261
}

3262 3263 3264 3265 3266 3267
static void test_GetUnicodeRanges(void)
{
    DWRITE_UNICODE_RANGE *ranges, r;
    IDWriteFontFile *ffile = NULL;
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
3268
    IDWriteFactory *factory;
3269 3270 3271
    UINT32 count;
    HRESULT hr;
    HRSRC font;
3272
    ULONG ref;
3273

3274 3275
    factory = create_factory();

3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
    hr = IDWriteFactory_RegisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    font = FindResourceA(GetModuleHandleA(NULL), (LPCSTR)MAKEINTRESOURCE(1), (LPCSTR)RT_RCDATA);
    ok(font != NULL, "Failed to find font resource\n");

    hr = IDWriteFactory_CreateCustomFontFileReference(factory, &font, sizeof(HRSRC), &rloader, &ffile);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &ffile, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(ffile);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    IDWriteFontFace_Release(fontface);
    if (hr != S_OK) {
        win_skip("GetUnicodeRanges() is not supported.\n");
3293
        IDWriteFactory_Release(factory);
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
        return;
    }

    count = 0;
    hr = IDWriteFontFace1_GetUnicodeRanges(fontface1, 0, NULL, &count);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(count > 0, "got %u\n", count);

    count = 1;
    hr = IDWriteFontFace1_GetUnicodeRanges(fontface1, 1, NULL, &count);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(count == 0, "got %u\n", count);

    count = 0;
    hr = IDWriteFontFace1_GetUnicodeRanges(fontface1, 1, &r, &count);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(count > 1, "got %u\n", count);

    ranges = heap_alloc(count*sizeof(DWRITE_UNICODE_RANGE));
    hr = IDWriteFontFace1_GetUnicodeRanges(fontface1, count, ranges, &count);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    ranges[0].first = ranges[0].last = 0;
    hr = IDWriteFontFace1_GetUnicodeRanges(fontface1, 1, ranges, &count);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(ranges[0].first != 0 && ranges[0].last != 0, "got 0x%x-0x%0x\n", ranges[0].first, ranges[0].last);

    heap_free(ranges);

    hr = IDWriteFactory_UnregisterFontFileLoader(factory, &rloader);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    IDWriteFontFace1_Release(fontface1);
3327 3328
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3329 3330
}

3331 3332 3333 3334 3335 3336
static void test_GetFontFromFontFace(void)
{
    IDWriteFontFace *fontface, *fontface2;
    IDWriteFontCollection *collection;
    IDWriteFont *font, *font2, *font3;
    IDWriteFontFamily *family;
3337
    IDWriteFactory *factory;
3338
    IDWriteFontFile *file;
3339
    WCHAR *path;
3340
    HRESULT hr;
3341
    ULONG ref;
3342

3343 3344
    factory = create_factory();

3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    font2 = NULL;
    hr = IDWriteFontCollection_GetFontFromFontFace(collection, fontface, &font2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(font2 != font, "got %p, %p\n", font2, font);

    font3 = NULL;
    hr = IDWriteFontCollection_GetFontFromFontFace(collection, fontface, &font3);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(font3 != font && font3 != font2, "got %p, %p, %p\n", font3, font2, font);

    hr = IDWriteFont_CreateFontFace(font2, &fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface2 == fontface, "got %p, %p\n", fontface2, fontface);
    IDWriteFontFace_Release(fontface2);

    hr = IDWriteFont_CreateFontFace(font3, &fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface2 == fontface, "got %p, %p\n", fontface2, fontface);
    IDWriteFontFace_Release(fontface2);
3377 3378 3379 3380 3381 3382
    IDWriteFontFace_Release(fontface);
    IDWriteFont_Release(font3);
    IDWriteFactory_Release(factory);

    /* fontface that wasn't created from this collection */
    factory = create_factory();
3383
    path = create_testfontfile(test_fontfile);
3384

3385
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
    ok(hr == S_OK, "got 0x%08x\n",hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontCollection_GetFontFromFontFace(collection, fontface, &font3);
    ok(hr == DWRITE_E_NOFONT, "got 0x%08x\n", hr);
    ok(font3 == NULL, "got %p\n", font3);
    IDWriteFontFace_Release(fontface);
3396 3397 3398 3399 3400

    IDWriteFont_Release(font);
    IDWriteFont_Release(font2);
    IDWriteFontFamily_Release(family);
    IDWriteFontCollection_Release(collection);
3401 3402
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3403
    DELETE_FONTFILE(path);
3404 3405
}

3406 3407 3408
static void test_GetFirstMatchingFont(void)
{
    DWRITE_FONT_SIMULATIONS simulations;
3409 3410
    IDWriteFontCollection *collection;
    IDWriteFontFamily *family;
3411 3412
    IDWriteFont *font, *font2;
    IDWriteFactory *factory;
3413
    HRESULT hr;
3414
    ULONG ref;
3415 3416 3417

    factory = create_factory();

3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3431
    ok(font != font2, "got %p, %p\n", font, font2);
3432
    IDWriteFont_Release(font);
3433
    IDWriteFont_Release(font2);
3434

3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
    /* out-of-range font props are allowed */
    hr = IDWriteFontFamily_GetFirstMatchingFont(family, 1000, DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, 10, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
        10, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

3449 3450
    IDWriteFontFamily_Release(family);

3451
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_ITALIC);
3452 3453
    simulations = IDWriteFont_GetSimulations(font);
    ok(simulations == DWRITE_FONT_SIMULATIONS_OBLIQUE, "%d\n", simulations);
3454
    IDWriteFont_Release(font);
3455

3456
    IDWriteFontCollection_Release(collection);
3457 3458
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
}

static void test_GetMatchingFonts(void)
{
    IDWriteFontCollection *collection;
    IDWriteFontFamily *family;
    IDWriteFactory *factory;
    IDWriteFontList *fontlist, *fontlist2;
    IDWriteFontList1 *fontlist1;
    HRESULT hr;
3469
    ULONG ref;
3470 3471 3472 3473 3474 3475 3476 3477 3478

    factory = create_factory();

    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
    /* out-of-range font props are allowed */
    hr = IDWriteFontFamily_GetMatchingFonts(family, 1000, DWRITE_FONT_STRETCH_NORMAL,
        DWRITE_FONT_STYLE_NORMAL, &fontlist);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontList_Release(fontlist);

    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL, 10,
        DWRITE_FONT_STYLE_NORMAL, &fontlist);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontList_Release(fontlist);

    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
        10, &fontlist);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontList_Release(fontlist);

3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &fontlist);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &fontlist2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontlist != fontlist2, "got %p, %p\n", fontlist, fontlist2);
    IDWriteFontList_Release(fontlist2);

    hr = IDWriteFontList_QueryInterface(fontlist, &IID_IDWriteFontList1, (void**)&fontlist1);
    if (hr == S_OK) {
3507
        IDWriteFontFaceReference *ref, *ref1;
3508
        IDWriteFont3 *font;
3509 3510 3511 3512
        UINT32 count;

        count = IDWriteFontList1_GetFontCount(fontlist1);
        ok(count > 0, "got %u\n", count);
3513 3514 3515 3516 3517

        font = (void*)0xdeadbeef;
        hr = IDWriteFontList1_GetFont(fontlist1, ~0u, &font);
        ok(hr == E_FAIL, "got 0x%08x\n", hr);
        ok(font == NULL, "got %p\n", font);
3518 3519

        font = (void*)0xdeadbeef;
3520
        hr = IDWriteFontList1_GetFont(fontlist1, count, &font);
3521 3522 3523 3524 3525 3526 3527
        ok(hr == E_FAIL, "got 0x%08x\n", hr);
        ok(font == NULL, "got %p\n", font);

        hr = IDWriteFontList1_GetFont(fontlist1, 0, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFont3_Release(font);

3528 3529 3530 3531 3532 3533 3534 3535 3536
        hr = IDWriteFontList1_GetFontFaceReference(fontlist1, 0, &ref);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontList1_GetFontFaceReference(fontlist1, 0, &ref1);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(ref != ref1, "got %p, %p\n", ref, ref1);

        IDWriteFontFaceReference_Release(ref1);
        IDWriteFontFaceReference_Release(ref);
3537 3538 3539 3540 3541 3542 3543 3544 3545
        IDWriteFontList1_Release(fontlist1);
    }
    else
        win_skip("IDWriteFontList1 is not supported.\n");

    IDWriteFontList_Release(fontlist);
    IDWriteFontFamily_Release(family);

    IDWriteFontCollection_Release(collection);
3546 3547
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3548 3549
}

3550 3551 3552 3553 3554
static void test_GetInformationalStrings(void)
{
    IDWriteLocalizedStrings *strings, *strings2;
    IDWriteFontCollection *collection;
    IDWriteFontFamily *family;
3555
    IDWriteFactory *factory;
3556 3557 3558
    IDWriteFont *font;
    BOOL exists;
    HRESULT hr;
3559
    ULONG ref;
3560

3561 3562
    factory = create_factory();

3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    exists = TRUE;
    strings = (void*)0xdeadbeef;
    hr = IDWriteFont_GetInformationalStrings(font, DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME+1, &strings, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == FALSE, "got %d\n", exists);
    ok(strings == NULL, "got %p\n", strings);
3579

3580 3581 3582 3583 3584 3585
    exists = TRUE;
    strings = NULL;
    hr = IDWriteFont_GetInformationalStrings(font, DWRITE_INFORMATIONAL_STRING_NONE, &strings, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == FALSE, "got %d\n", exists);

3586 3587 3588 3589 3590 3591
    exists = FALSE;
    strings = NULL;
    hr = IDWriteFont_GetInformationalStrings(font, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, &strings, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == TRUE, "got %d\n", exists);

3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
    /* strings instance is not reused */
    strings2 = NULL;
    hr = IDWriteFont_GetInformationalStrings(font, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, &strings2, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(strings2 != strings, "got %p, %p\n", strings2, strings);

    IDWriteLocalizedStrings_Release(strings);
    IDWriteLocalizedStrings_Release(strings2);
    IDWriteFont_Release(font);
    IDWriteFontFamily_Release(family);
    IDWriteFontCollection_Release(collection);
3603 3604
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3605 3606 3607 3608 3609
}

static void test_GetGdiInterop(void)
{
    IDWriteGdiInterop *interop, *interop2;
3610
    IDWriteFactory *factory, *factory2;
3611 3612 3613
    IDWriteFont *font;
    LOGFONTW logfont;
    HRESULT hr;
3614
    ULONG ref;
3615

3616 3617
    factory = create_factory();

3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648
    interop = NULL;
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    interop2 = NULL;
    hr = IDWriteFactory_GetGdiInterop(factory, &interop2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(interop == interop2, "got %p, %p\n", interop, interop2);
    IDWriteGdiInterop_Release(interop2);

    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IDWriteFactory, (IUnknown**)&factory2);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    /* each factory gets its own interop */
    interop2 = NULL;
    hr = IDWriteFactory_GetGdiInterop(factory2, &interop2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(interop != interop2, "got %p, %p\n", interop, interop2);

    /* release factory - interop still works */
    IDWriteFactory_Release(factory2);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop2, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);
3649
    IDWriteFont_Release(font);
3650 3651 3652

    IDWriteGdiInterop_Release(interop2);
    IDWriteGdiInterop_Release(interop);
3653 3654
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3655 3656
}

3657 3658 3659 3660
static void test_CreateFontFaceFromHdc(void)
{
    IDWriteGdiInterop *interop;
    IDWriteFontFace *fontface;
3661
    IDWriteFactory *factory;
3662 3663
    HFONT hfont, oldhfont;
    LOGFONTW logfont;
3664
    LOGFONTA lf;
3665
    HRESULT hr;
3666
    ULONG ref;
3667 3668
    HDC hdc;

3669 3670
    factory = create_factory();

3671 3672 3673 3674
    interop = NULL;
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

3675
    /* Invalid HDC. */
3676
    fontface = (void*)0xdeadbeef;
3677 3678
    hr = IDWriteGdiInterop_CreateFontFaceFromHdc(interop, NULL, &fontface);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
3679
    ok(fontface == NULL, "got %p\n", fontface);
3680

3681 3682 3683 3684 3685
    fontface = (void *)0xdeadbeef;
    hr = IDWriteGdiInterop_CreateFontFaceFromHdc(interop, (HDC)0xdeadbeef, &fontface);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(fontface == NULL, "got %p\n", fontface);

3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hfont = CreateFontIndirectW(&logfont);
    hdc = CreateCompatibleDC(0);
    oldhfont = SelectObject(hdc, hfont);

    fontface = NULL;
    hr = IDWriteGdiInterop_CreateFontFaceFromHdc(interop, hdc, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFace_Release(fontface);
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
    DeleteObject(SelectObject(hdc, oldhfont));

    /* Select bitmap font MS Sans Serif, format that's not supported by DirectWrite. */
    memset(&lf, 0, sizeof(lf));
    lf.lfHeight = -12;
    strcpy(lf.lfFaceName, "MS Sans Serif");

    hfont = CreateFontIndirectA(&lf);
    oldhfont = SelectObject(hdc, hfont);

    fontface = (void *)0xdeadbeef;
    hr = IDWriteGdiInterop_CreateFontFaceFromHdc(interop, hdc, &fontface);
    ok(hr == DWRITE_E_FILEFORMAT || broken(hr == E_FAIL) /* Vista */, "got 0x%08x\n", hr);
    ok(fontface == NULL, "got %p\n", fontface);

3716 3717 3718 3719
    DeleteObject(SelectObject(hdc, oldhfont));
    DeleteDC(hdc);

    IDWriteGdiInterop_Release(interop);
3720 3721
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3722 3723
}

3724 3725 3726 3727 3728 3729 3730 3731 3732
static void test_GetSimulations(void)
{
    DWRITE_FONT_SIMULATIONS simulations;
    IDWriteGdiInterop *interop;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFont *font;
    LOGFONTW logfont;
    HRESULT hr;
3733
    ULONG ref;
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769

    factory = create_factory();

    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    simulations = IDWriteFont_GetSimulations(font);
    ok(simulations == DWRITE_FONT_SIMULATIONS_OBLIQUE, "got %d\n", simulations);
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    simulations = IDWriteFontFace_GetSimulations(fontface);
    ok(simulations == DWRITE_FONT_SIMULATIONS_OBLIQUE, "got %d\n", simulations);
    IDWriteFontFace_Release(fontface);
    IDWriteFont_Release(font);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 0;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    simulations = IDWriteFont_GetSimulations(font);
3770
    ok(simulations == DWRITE_FONT_SIMULATIONS_NONE, "got %d\n", simulations);
3771 3772 3773
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    simulations = IDWriteFontFace_GetSimulations(fontface);
3774
    ok(simulations == DWRITE_FONT_SIMULATIONS_NONE, "got %d\n", simulations);
3775 3776 3777 3778
    IDWriteFontFace_Release(fontface);
    IDWriteFont_Release(font);

    IDWriteGdiInterop_Release(interop);
3779 3780
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3781 3782
}

3783 3784 3785
static void test_GetFaceNames(void)
{
    static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
3786
    static const WCHAR enus2W[] = {'e','n','-','U','s',0};
3787 3788 3789 3790
    static const WCHAR enusW[] = {'e','n','-','u','s',0};
    IDWriteLocalizedStrings *strings, *strings2;
    IDWriteGdiInterop *interop;
    IDWriteFactory *factory;
3791
    UINT32 count, index;
3792 3793 3794
    IDWriteFont *font;
    LOGFONTW logfont;
    WCHAR buffW[255];
3795
    BOOL exists;
3796
    HRESULT hr;
3797
    ULONG ref;
3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824

    factory = create_factory();

    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = 12;
    logfont.lfWidth  = 12;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfItalic = 1;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hr = IDWriteGdiInterop_CreateFontFromLOGFONT(interop, &logfont, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFont_GetFaceNames(font, &strings);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFont_GetFaceNames(font, &strings2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(strings != strings2, "got %p, %p\n", strings2, strings);
    IDWriteLocalizedStrings_Release(strings2);

    count = IDWriteLocalizedStrings_GetCount(strings);
    ok(count == 1, "got %d\n", count);

3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
    index = 1;
    exists = FALSE;
    hr = IDWriteLocalizedStrings_FindLocaleName(strings, enus2W, &index, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(index == 0 && exists, "got %d, %d\n", index, exists);

    count = 0;
    hr = IDWriteLocalizedStrings_GetLocaleNameLength(strings, 1, &count);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(count == ~0, "got %d\n", count);

3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
    /* for simulated faces names are also simulated */
    buffW[0] = 0;
    hr = IDWriteLocalizedStrings_GetLocaleName(strings, 0, buffW, sizeof(buffW)/sizeof(WCHAR));
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!lstrcmpW(buffW, enusW), "got %s\n", wine_dbgstr_w(buffW));

    buffW[0] = 0;
    hr = IDWriteLocalizedStrings_GetString(strings, 0, buffW, sizeof(buffW)/sizeof(WCHAR));
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!lstrcmpW(buffW, obliqueW), "got %s\n", wine_dbgstr_w(buffW));
    IDWriteLocalizedStrings_Release(strings);

    IDWriteFont_Release(font);
    IDWriteGdiInterop_Release(interop);
3850 3851
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3852 3853
}

3854 3855 3856 3857 3858 3859
struct local_refkey
{
    FILETIME writetime;
    WCHAR name[1];
};

3860 3861
static void test_TryGetFontTable(void)
{
3862 3863 3864 3865
    IDWriteLocalFontFileLoader *localloader;
    WIN32_FILE_ATTRIBUTE_DATA info;
    const struct local_refkey *key;
    IDWriteFontFileLoader *loader;
3866 3867 3868 3869 3870
    const void *table, *table2;
    IDWriteFontFace *fontface;
    void *context, *context2;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
3871 3872 3873
    WCHAR buffW[MAX_PATH];
    BOOL exists, ret;
    UINT32 size, len;
3874
    WCHAR *path;
3875
    HRESULT hr;
3876
    ULONG ref;
3877

3878
    path = create_testfontfile(test_fontfile);
3879 3880 3881

    factory = create_factory();

3882
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
3883 3884
    ok(hr == S_OK, "got 0x%08x\n",hr);

3885 3886 3887 3888 3889 3890
    key = NULL;
    size = 0;
    hr = IDWriteFontFile_GetReferenceKey(file, (const void**)&key, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size != 0, "got %u\n", size);

3891
    ret = GetFileAttributesExW(path, GetFileExInfoStandard, &info);
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
    ok(ret, "got %d\n", ret);
    ok(!memcmp(&info.ftLastWriteTime, &key->writetime, sizeof(key->writetime)), "got wrong write time\n");

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteLocalFontFileLoader, (void**)&localloader);
    IDWriteFontFileLoader_Release(loader);

    hr = IDWriteLocalFontFileLoader_GetFilePathLengthFromKey(localloader, key, size, &len);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(lstrlenW(key->name) == len, "path length %d\n", len);

    hr = IDWriteLocalFontFileLoader_GetFilePathFromKey(localloader, key, size, buffW, sizeof(buffW)/sizeof(WCHAR));
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!lstrcmpW(buffW, key->name), "got %s, expected %s\n", wine_dbgstr_w(buffW), wine_dbgstr_w(key->name));
    IDWriteLocalFontFileLoader_Release(localloader);

3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, 0, &fontface);
    ok(hr == S_OK, "got 0x%08x\n",hr);

    exists = FALSE;
    context = (void*)0xdeadbeef;
    table = NULL;
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_CMAP_TAG, &table, &size, &context, &exists);
    ok(hr == S_OK, "got 0x%08x\n",hr);
    ok(exists == TRUE, "got %d\n", exists);
    ok(context == NULL && table != NULL, "cmap: context %p, table %p\n", context, table);

    exists = FALSE;
    context2 = (void*)0xdeadbeef;
    table2 = NULL;
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_CMAP_TAG, &table2, &size, &context2, &exists);
    ok(hr == S_OK, "got 0x%08x\n",hr);
    ok(exists == TRUE, "got %d\n", exists);
    ok(context2 == context && table2 == table, "cmap: context2 %p, table2 %p\n", context2, table2);

    IDWriteFontFace_ReleaseFontTable(fontface, context2);
    IDWriteFontFace_ReleaseFontTable(fontface, context);

3931 3932 3933 3934 3935 3936 3937 3938 3939
    /* table does not exist */
    exists = TRUE;
    context = (void*)0xdeadbeef;
    table = (void*)0xdeadbeef;
    hr = IDWriteFontFace_TryGetFontTable(fontface, 0xabababab, &table, &size, &context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists == FALSE, "got %d\n", exists);
    ok(context == NULL && table == NULL, "got context %p, table pointer %p\n", context, table);

3940 3941
    IDWriteFontFace_Release(fontface);
    IDWriteFontFile_Release(file);
3942 3943
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
3944
    DELETE_FONTFILE(path);
3945 3946
}

3947 3948 3949 3950 3951 3952 3953 3954
static void test_ConvertFontToLOGFONT(void)
{
    IDWriteFactory *factory, *factory2;
    IDWriteFontCollection *collection;
    IDWriteGdiInterop *interop;
    IDWriteFontFamily *family;
    IDWriteFont *font;
    LOGFONTW logfont;
3955
    UINT32 i, count;
3956 3957
    BOOL system;
    HRESULT hr;
3958
    ULONG ref;
3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981

    factory = create_factory();
    factory2 = create_factory();

    interop = NULL;
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_GetSystemFontCollection(factory2, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

if (0) { /* crashes on native */
    IDWriteGdiInterop_ConvertFontToLOGFONT(interop, NULL, NULL, NULL);
    IDWriteGdiInterop_ConvertFontToLOGFONT(interop, NULL, &logfont, NULL);
    IDWriteGdiInterop_ConvertFontToLOGFONT(interop, font, NULL, &system);
}
3982 3983

    memset(&logfont, 0xcc, sizeof(logfont));
3984 3985 3986 3987
    system = TRUE;
    hr = IDWriteGdiInterop_ConvertFontToLOGFONT(interop, NULL, &logfont, &system);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(!system, "got %d\n", system);
3988
    ok(logfont.lfFaceName[0] == 0, "got face name %s\n", wine_dbgstr_w(logfont.lfFaceName));
3989

3990 3991 3992 3993 3994 3995 3996 3997 3998
    count = IDWriteFontCollection_GetFontFamilyCount(collection);
    for (i = 0; i < count; i++) {
        WCHAR nameW[128], familynameW[64], facenameW[64];
        IDWriteLocalizedStrings *names;
        DWRITE_FONT_SIMULATIONS sim;
        IDWriteFontFamily *family;
        UINT32 font_count, j;
        IDWriteFont *font;
        LOGFONTW lf;
3999

4000 4001
        hr = IDWriteFontCollection_GetFontFamily(collection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);
4002

4003 4004
        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);
4005

4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
        get_enus_string(names, familynameW, sizeof(familynameW)/sizeof(familynameW[0]));
        IDWriteLocalizedStrings_Release(names);

        font_count = IDWriteFontFamily_GetFontCount(family);

        for (j = 0; j < font_count; j++) {
            static const WCHAR spaceW[] = {' ', 0};

            hr = IDWriteFontFamily_GetFont(family, j, &font);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            hr = IDWriteFont_GetFaceNames(font, &names);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            get_enus_string(names, facenameW, sizeof(facenameW)/sizeof(facenameW[0]));
            IDWriteLocalizedStrings_Release(names);

            lstrcpyW(nameW, familynameW);
            lstrcatW(nameW, spaceW);
            lstrcatW(nameW, facenameW);

            system = FALSE;
            memset(&logfont, 0xcc, sizeof(logfont));
            hr = IDWriteGdiInterop_ConvertFontToLOGFONT(interop, font, &logfont, &system);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(system, "got %d\n", system);

            sim = IDWriteFont_GetSimulations(font);

            get_logfont_from_font(font, &lf);
4036 4037 4038
            ok(logfont.lfWeight == lf.lfWeight, "%s: unexpected lfWeight %d, expected lfWeight %d, font weight %d, "
                "bold simulation %s\n", wine_dbgstr_w(nameW), logfont.lfWeight, lf.lfWeight, IDWriteFont_GetWeight(font),
                sim & DWRITE_FONT_SIMULATIONS_BOLD ? "yes" : "no");
4039 4040
            ok(logfont.lfItalic == lf.lfItalic, "%s: unexpected italic flag %d, oblique simulation %s\n",
                wine_dbgstr_w(nameW), logfont.lfItalic, sim & DWRITE_FONT_SIMULATIONS_OBLIQUE ? "yes" : "no");
4041 4042
            ok(!lstrcmpW(logfont.lfFaceName, lf.lfFaceName), "%s: unexpected facename %s, expected %s\n",
                wine_dbgstr_w(nameW), wine_dbgstr_w(logfont.lfFaceName), wine_dbgstr_w(lf.lfFaceName));
4043

4044 4045 4046 4047 4048 4049 4050
            ok(logfont.lfOutPrecision == OUT_OUTLINE_PRECIS, "%s: unexpected output precision %d\n", wine_dbgstr_w(nameW),
                logfont.lfOutPrecision);
            ok(logfont.lfClipPrecision == CLIP_DEFAULT_PRECIS, "%s: unexpected clipping precision %d\n", wine_dbgstr_w(nameW),
                logfont.lfClipPrecision);
            ok(logfont.lfQuality == DEFAULT_QUALITY, "%s: unexpected quality %d\n", wine_dbgstr_w(nameW), logfont.lfQuality);
            ok(logfont.lfPitchAndFamily == DEFAULT_PITCH, "%s: unexpected pitch %d\n", wine_dbgstr_w(nameW),
                logfont.lfPitchAndFamily);
4051 4052 4053 4054 4055 4056

            IDWriteFont_Release(font);
        }

        IDWriteFontFamily_Release(family);
    }
4057 4058 4059 4060 4061 4062 4063

    IDWriteFactory_Release(factory2);

    IDWriteFontCollection_Release(collection);
    IDWriteFontFamily_Release(family);
    IDWriteFont_Release(font);
    IDWriteGdiInterop_Release(interop);
4064 4065
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4066 4067
}

4068 4069 4070 4071 4072 4073 4074
static void test_CreateStreamFromKey(void)
{
    IDWriteLocalFontFileLoader *localloader;
    IDWriteFontFileStream *stream, *stream2;
    IDWriteFontFileLoader *loader;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
4075
    UINT64 writetime;
4076
    WCHAR *path;
4077 4078 4079
    void *key;
    UINT32 size;
    HRESULT hr;
4080
    ULONG ref;
4081 4082 4083

    factory = create_factory();

4084
    path = create_testfontfile(test_fontfile);
4085

4086
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105
    ok(hr == S_OK, "got 0x%08x\n",hr);

    key = NULL;
    size = 0;
    hr = IDWriteFontFile_GetReferenceKey(file, (const void**)&key, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size != 0, "got %u\n", size);

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteLocalFontFileLoader, (void**)&localloader);
    IDWriteFontFileLoader_Release(loader);

    hr = IDWriteLocalFontFileLoader_CreateStreamFromKey(localloader, key, size, &stream);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(stream, 1);

    hr = IDWriteLocalFontFileLoader_CreateStreamFromKey(localloader, key, size, &stream2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4106 4107 4108
    ok(stream == stream2 || broken(stream != stream2) /* Win7 SP0 */, "got %p, %p\n", stream, stream2);
    if (stream == stream2)
        EXPECT_REF(stream, 2);
4109 4110 4111 4112 4113 4114
    IDWriteFontFileStream_Release(stream);
    IDWriteFontFileStream_Release(stream2);

    hr = IDWriteLocalFontFileLoader_CreateStreamFromKey(localloader, key, size, &stream);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    EXPECT_REF(stream, 1);
4115 4116 4117 4118

    writetime = 0;
    hr = IDWriteFontFileStream_GetLastWriteTime(stream, &writetime);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4119
    ok(writetime != 0, "got %s\n", wine_dbgstr_longlong(writetime));
4120

4121
    IDWriteFontFileStream_Release(stream);
4122
    IDWriteFontFile_Release(file);
4123 4124

    IDWriteLocalFontFileLoader_Release(localloader);
4125 4126
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4127
    DELETE_FONTFILE(path);
4128 4129
}

4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140
static void test_ReadFileFragment(void)
{
    IDWriteLocalFontFileLoader *localloader;
    IDWriteFontFileStream *stream;
    IDWriteFontFileLoader *loader;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    const void *fragment, *fragment2;
    void *key, *context, *context2;
    UINT64 filesize;
    UINT32 size;
4141
    WCHAR *path;
4142
    HRESULT hr;
4143
    ULONG ref;
4144 4145 4146

    factory = create_factory();

4147
    path = create_testfontfile(test_fontfile);
4148

4149
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
    ok(hr == S_OK, "got 0x%08x\n",hr);

    key = NULL;
    size = 0;
    hr = IDWriteFontFile_GetReferenceKey(file, (const void**)&key, &size);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(size != 0, "got %u\n", size);

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteLocalFontFileLoader, (void**)&localloader);
    IDWriteFontFileLoader_Release(loader);

    hr = IDWriteLocalFontFileLoader_CreateStreamFromKey(localloader, key, size, &stream);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFileStream_GetFileSize(stream, &filesize);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    /* reading past the end of the stream */
    fragment = (void*)0xdeadbeef;
    context = (void*)0xdeadbeef;
    hr = IDWriteFontFileStream_ReadFileFragment(stream, &fragment, 0, filesize+1, &context);
    ok(hr == E_FAIL, "got 0x%08x\n", hr);
    ok(context == NULL, "got %p\n", context);
    ok(fragment == NULL, "got %p\n", fragment);
4176

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
    fragment = (void*)0xdeadbeef;
    context = (void*)0xdeadbeef;
    hr = IDWriteFontFileStream_ReadFileFragment(stream, &fragment, 0, filesize, &context);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(context == NULL, "got %p\n", context);
    ok(fragment != NULL, "got %p\n", fragment);

    fragment2 = (void*)0xdeadbeef;
    context2 = (void*)0xdeadbeef;
    hr = IDWriteFontFileStream_ReadFileFragment(stream, &fragment2, 0, filesize, &context2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(context2 == NULL, "got %p\n", context2);
    ok(fragment == fragment2, "got %p, %p\n", fragment, fragment2);
4190

4191 4192 4193
    IDWriteFontFileStream_ReleaseFileFragment(stream, context);
    IDWriteFontFileStream_ReleaseFileFragment(stream, context2);

4194 4195 4196 4197 4198 4199 4200 4201 4202
    /* fragment is released, try again */
    fragment = (void*)0xdeadbeef;
    context = (void*)0xdeadbeef;
    hr = IDWriteFontFileStream_ReadFileFragment(stream, &fragment, 0, filesize, &context);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(context == NULL, "got %p\n", context);
    ok(fragment == fragment2, "got %p, %p\n", fragment, fragment2);
    IDWriteFontFileStream_ReleaseFileFragment(stream, context);

4203
    IDWriteFontFile_Release(file);
4204 4205
    IDWriteFontFileStream_Release(stream);
    IDWriteLocalFontFileLoader_Release(localloader);
4206 4207
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4208
    DELETE_FONTFILE(path);
4209 4210
}

4211 4212 4213 4214 4215 4216 4217 4218
static void test_GetDesignGlyphMetrics(void)
{
    DWRITE_GLYPH_METRICS metrics[2];
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    UINT16 indices[2];
    UINT32 codepoint;
4219
    WCHAR *path;
4220
    HRESULT hr;
4221
    ULONG ref;
4222 4223 4224

    factory = create_factory();

4225
    path = create_testfontfile(test_fontfile);
4226

4227
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258
    ok(hr == S_OK, "got 0x%08x\n",hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file,
        0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n",hr);
    IDWriteFontFile_Release(file);

    codepoint = 'A';
    indices[0] = 0;
    hr = IDWriteFontFace_GetGlyphIndices(fontface, &codepoint, 1, &indices[0]);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(indices[0] > 0, "got %u\n", indices[0]);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, NULL, 0, metrics, FALSE);
    ok(hr == E_INVALIDARG, "got 0x%08x\n",hr);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, NULL, 1, metrics, FALSE);
    ok(hr == E_INVALIDARG, "got 0x%08x\n",hr);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, indices, 0, metrics, FALSE);
    ok(hr == S_OK, "got 0x%08x\n",hr);

    /* missing glyphs are ignored */
    indices[1] = 1;
    memset(metrics, 0xcc, sizeof(metrics));
    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, indices, 2, metrics, FALSE);
    ok(hr == S_OK, "got 0x%08x\n",hr);
    ok(metrics[0].advanceWidth == 1000, "got %d\n", metrics[0].advanceWidth);
    ok(metrics[1].advanceWidth == 0, "got %d\n", metrics[1].advanceWidth);

    IDWriteFontFace_Release(fontface);
4259 4260
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4261
    DELETE_FONTFILE(path);
4262 4263
}

4264 4265 4266 4267 4268 4269 4270 4271
static void test_IsMonospacedFont(void)
{
    static const WCHAR courierW[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
    IDWriteFontCollection *collection;
    IDWriteFactory *factory;
    UINT32 index;
    BOOL exists;
    HRESULT hr;
4272
    ULONG ref;
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315

    factory = create_factory();
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    exists = FALSE;
    hr = IDWriteFontCollection_FindFamilyName(collection, courierW, &index, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    if (exists) {
        IDWriteFontFamily *family;
        IDWriteFont1 *font1;
        IDWriteFont *font;

        hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
            DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFontFamily_Release(family);

        hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont1, (void**)&font1);
        if (hr == S_OK) {
            IDWriteFontFace1 *fontface1;
            IDWriteFontFace *fontface;
            BOOL is_monospaced;

            is_monospaced = IDWriteFont1_IsMonospacedFont(font1);
            ok(is_monospaced, "got %d\n", is_monospaced);

            hr = IDWriteFont1_CreateFontFace(font1, &fontface);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            is_monospaced = IDWriteFontFace1_IsMonospacedFont(fontface1);
            ok(is_monospaced, "got %d\n", is_monospaced);
            IDWriteFontFace1_Release(fontface1);

            IDWriteFontFace_Release(fontface);
            IDWriteFont1_Release(font1);
        }
        else
            win_skip("IsMonospacedFont() is not supported.\n");
4316 4317

        IDWriteFont_Release(font);
4318 4319 4320 4321
    }
    else
        skip("Courier New font not found.\n");

4322 4323
    ref = IDWriteFontCollection_Release(collection);
    ok(ref == 0, "factory not released, %u\n", ref);
4324 4325
}

4326 4327 4328 4329 4330 4331
static void test_GetDesignGlyphAdvances(void)
{
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
4332
    WCHAR *path;
4333
    HRESULT hr;
4334
    ULONG ref;
4335 4336 4337

    factory = create_factory();

4338
    path = create_testfontfile(test_fontfile);
4339

4340
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file,
        0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    if (hr == S_OK) {
        UINT32 codepoint;
        UINT16 index;
        INT32 advance;

        codepoint = 'A';
        index = 0;
        hr = IDWriteFontFace1_GetGlyphIndices(fontface1, &codepoint, 1, &index);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(index > 0, "got %u\n", index);

        advance = 0;
        hr = IDWriteFontFace1_GetDesignGlyphAdvances(fontface1, 1, &index, &advance, FALSE);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(advance == 1000, "got %i\n", advance);

        advance = 0;
        hr = IDWriteFontFace1_GetDesignGlyphAdvances(fontface1, 1, &index, &advance, TRUE);
        ok(hr == S_OK, "got 0x%08x\n", hr);
4368
    todo_wine
4369 4370 4371 4372 4373 4374 4375 4376
        ok(advance == 2048, "got %i\n", advance);

        IDWriteFontFace1_Release(fontface1);
    }
    else
        win_skip("GetDesignGlyphAdvances() is not supported.\n");

    IDWriteFontFace_Release(fontface);
4377 4378
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4379
    DELETE_FONTFILE(path);
4380 4381
}

4382 4383 4384 4385 4386 4387 4388 4389 4390
static void test_GetGlyphRunOutline(void)
{
    DWRITE_GLYPH_OFFSET offsets[2];
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    IDWriteFontFace *face;
    UINT32 codepoint;
    FLOAT advances[2];
    UINT16 glyphs[2];
4391
    WCHAR *path;
4392
    HRESULT hr;
4393
    ULONG ref;
4394

4395
    path = create_testfontfile(test_fontfile);
4396 4397
    factory = create_factory();

4398
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428
    ok(hr == S_OK, "got 0x%08x\n",hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &face);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    codepoint = 'A';
    glyphs[0] = 0;
    hr = IDWriteFontFace_GetGlyphIndices(face, &codepoint, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyphs[0] > 0, "got %u\n", glyphs[0]);
    glyphs[1] = glyphs[0];

    hr = IDWriteFontFace_GetGlyphRunOutline(face, 2048.0, glyphs, advances, offsets, 1, FALSE, FALSE, NULL);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_GetGlyphRunOutline(face, 2048.0, NULL, NULL, offsets, 1, FALSE, FALSE, &test_geomsink);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    advances[0] = 1.0;
    advances[1] = 0.0;

    offsets[0].advanceOffset = 1.0;
    offsets[0].ascenderOffset = 1.0;
    offsets[1].advanceOffset = 0.0;
    offsets[1].ascenderOffset = 0.0;

    /* default advances, no offsets */
    memset(g_startpoints, 0, sizeof(g_startpoints));
    g_startpoint_count = 0;
4429
    SET_EXPECT(setfillmode);
4430 4431 4432 4433 4434 4435 4436 4437
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, NULL, 2, FALSE, FALSE, &test_geomsink);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(g_startpoint_count == 2, "got %d\n", g_startpoint_count);
    if (g_startpoint_count == 2) {
        /* glyph advance of 500 is applied */
        ok(g_startpoints[0].x == 229.5 && g_startpoints[0].y == -629.0, "0: got (%.2f,%.2f)\n", g_startpoints[0].x, g_startpoints[0].y);
        ok(g_startpoints[1].x == 729.5 && g_startpoints[1].y == -629.0, "1: got (%.2f,%.2f)\n", g_startpoints[1].x, g_startpoints[1].y);
    }
4438
    CHECK_CALLED(setfillmode);
4439 4440 4441 4442

    /* default advances, no offsets, RTL */
    memset(g_startpoints, 0, sizeof(g_startpoints));
    g_startpoint_count = 0;
4443
    SET_EXPECT(setfillmode);
4444 4445 4446 4447 4448 4449 4450 4451
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, NULL, 2, FALSE, TRUE, &test_geomsink);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(g_startpoint_count == 2, "got %d\n", g_startpoint_count);
    if (g_startpoint_count == 2) {
        /* advance is -500 now */
        ok(g_startpoints[0].x == -270.5 && g_startpoints[0].y == -629.0, "0: got (%.2f,%.2f)\n", g_startpoints[0].x, g_startpoints[0].y);
        ok(g_startpoints[1].x == -770.5 && g_startpoints[1].y == -629.0, "1: got (%.2f,%.2f)\n", g_startpoints[1].x, g_startpoints[1].y);
    }
4452
    CHECK_CALLED(setfillmode);
4453 4454 4455 4456

    /* default advances, additional offsets */
    memset(g_startpoints, 0, sizeof(g_startpoints));
    g_startpoint_count = 0;
4457
    SET_EXPECT(setfillmode);
4458 4459 4460 4461 4462 4463 4464 4465
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, offsets, 2, FALSE, FALSE, &test_geomsink);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(g_startpoint_count == 2, "got %d\n", g_startpoint_count);
    if (g_startpoint_count == 2) {
        /* offsets applied to first contour */
        ok(g_startpoints[0].x == 230.5 && g_startpoints[0].y == -630.0, "0: got (%.2f,%.2f)\n", g_startpoints[0].x, g_startpoints[0].y);
        ok(g_startpoints[1].x == 729.5 && g_startpoints[1].y == -629.0, "1: got (%.2f,%.2f)\n", g_startpoints[1].x, g_startpoints[1].y);
    }
4466
    CHECK_CALLED(setfillmode);
4467 4468 4469 4470

    /* default advances, additional offsets, RTL */
    memset(g_startpoints, 0, sizeof(g_startpoints));
    g_startpoint_count = 0;
4471
    SET_EXPECT(setfillmode);
4472 4473 4474 4475 4476 4477 4478
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, offsets, 2, FALSE, TRUE, &test_geomsink);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(g_startpoint_count == 2, "got %d\n", g_startpoint_count);
    if (g_startpoint_count == 2) {
        ok(g_startpoints[0].x == -271.5 && g_startpoints[0].y == -630.0, "0: got (%.2f,%.2f)\n", g_startpoints[0].x, g_startpoints[0].y);
        ok(g_startpoints[1].x == -770.5 && g_startpoints[1].y == -629.0, "1: got (%.2f,%.2f)\n", g_startpoints[1].x, g_startpoints[1].y);
    }
4479
    CHECK_CALLED(setfillmode);
4480 4481 4482 4483

    /* custom advances and offsets, offset turns total advance value to zero */
    memset(g_startpoints, 0, sizeof(g_startpoints));
    g_startpoint_count = 0;
4484
    SET_EXPECT(setfillmode);
4485 4486 4487 4488 4489 4490 4491
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, advances, offsets, 2, FALSE, FALSE, &test_geomsink);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(g_startpoint_count == 2, "got %d\n", g_startpoint_count);
    if (g_startpoint_count == 2) {
        ok(g_startpoints[0].x == 230.5 && g_startpoints[0].y == -630.0, "0: got (%.2f,%.2f)\n", g_startpoints[0].x, g_startpoints[0].y);
        ok(g_startpoints[1].x == 230.5 && g_startpoints[1].y == -629.0, "1: got (%.2f,%.2f)\n", g_startpoints[1].x, g_startpoints[1].y);
    }
4492
    CHECK_CALLED(setfillmode);
4493

4494 4495 4496
    /* 0 glyph count */
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, NULL, 0, FALSE, FALSE, &test_geomsink2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4497

4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
    /* Glyph with open figure, single contour point. */
    codepoint = 'B';
    glyphs[0] = 0;
    hr = IDWriteFontFace_GetGlyphIndices(face, &codepoint, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyphs[0] > 0, "got %u\n", glyphs[0]);

    SET_EXPECT(setfillmode);
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, NULL, 1, FALSE, FALSE, &test_geomsink2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    CHECK_CALLED(setfillmode);

4510 4511
    IDWriteFactory_Release(factory);
    IDWriteFontFace_Release(face);
4512
    DELETE_FONTFILE(path);
4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529

    /* space glyph */
    factory = create_factory();
    face = create_fontface(factory);

    codepoint = ' ';
    glyphs[0] = 0;
    hr = IDWriteFontFace_GetGlyphIndices(face, &codepoint, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyphs[0] > 0, "got %u\n", glyphs[0]);

    SET_EXPECT(setfillmode);
    hr = IDWriteFontFace_GetGlyphRunOutline(face, 1024.0, glyphs, NULL, NULL, 1, FALSE, FALSE, &test_geomsink2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    CHECK_CALLED(setfillmode);

    IDWriteFontFace_Release(face);
4530 4531
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4532 4533
}

4534 4535 4536
static void test_GetEudcFontCollection(void)
{
    IDWriteFontCollection *coll, *coll2;
4537
    IDWriteFactory1 *factory;
4538
    HRESULT hr;
4539
    ULONG ref;
4540

4541 4542
    factory = create_factory_iid(&IID_IDWriteFactory1);
    if (!factory) {
4543 4544 4545 4546
        win_skip("GetEudcFontCollection() is not supported.\n");
        return;
    }

4547 4548
    EXPECT_REF(factory, 1);
    hr = IDWriteFactory1_GetEudcFontCollection(factory, &coll, FALSE);
4549
    ok(hr == S_OK, "got 0x%08x\n", hr);
4550 4551
    EXPECT_REF(factory, 2);
    hr = IDWriteFactory1_GetEudcFontCollection(factory, &coll2, FALSE);
4552
    ok(hr == S_OK, "got 0x%08x\n", hr);
4553
    EXPECT_REF(factory, 2);
4554 4555 4556 4557
    ok(coll == coll2, "got %p, %p\n", coll, coll2);
    IDWriteFontCollection_Release(coll);
    IDWriteFontCollection_Release(coll2);

4558
    ref = IDWriteFactory1_Release(factory);
4559
    ok(ref == 0, "factory not released, %u\n", ref);
4560 4561
}

4562 4563 4564 4565 4566 4567 4568 4569 4570
static void test_GetCaretMetrics(void)
{
    DWRITE_FONT_METRICS1 metrics;
    IDWriteFontFace1 *fontface1;
    DWRITE_CARET_METRICS caret;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    IDWriteFont *font;
4571
    WCHAR *path;
4572
    HRESULT hr;
4573
    ULONG ref;
4574

4575
    path = create_testfontfile(test_fontfile);
4576 4577
    factory = create_factory();

4578
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4579 4580 4581 4582 4583 4584 4585 4586 4587 4588
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    IDWriteFontFace_Release(fontface);
    if (hr != S_OK) {
        win_skip("GetCaretMetrics() is not supported.\n");
4589 4590
        ref = IDWriteFactory_Release(factory);
        ok(ref == 0, "factory not released, %u\n", ref);
4591
        DELETE_FONTFILE(path);
4592 4593 4594 4595 4596 4597 4598 4599 4600
        return;
    }

    memset(&caret, 0xcc, sizeof(caret));
    IDWriteFontFace1_GetCaretMetrics(fontface1, &caret);
    ok(caret.slopeRise == 1, "got %d\n", caret.slopeRise);
    ok(caret.slopeRun == 0, "got %d\n", caret.slopeRun);
    ok(caret.offset == 0, "got %d\n", caret.offset);
    IDWriteFontFace1_Release(fontface1);
4601
    IDWriteFactory_Release(factory);
4602 4603

    /* now with Tahoma Normal */
4604
    factory = create_factory();
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_NORMAL);
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFace_Release(fontface);

    memset(&caret, 0xcc, sizeof(caret));
    IDWriteFontFace1_GetCaretMetrics(fontface1, &caret);
    ok(caret.slopeRise == 1, "got %d\n", caret.slopeRise);
    ok(caret.slopeRun == 0, "got %d\n", caret.slopeRun);
    ok(caret.offset == 0, "got %d\n", caret.offset);
    IDWriteFontFace1_Release(fontface1);

    /* simulated italic */
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_ITALIC);
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFace_Release(fontface);

    IDWriteFontFace1_GetMetrics(fontface1, &metrics);

    memset(&caret, 0xcc, sizeof(caret));
    IDWriteFontFace1_GetCaretMetrics(fontface1, &caret);
    ok(caret.slopeRise == metrics.designUnitsPerEm, "got %d\n", caret.slopeRise);
    ok(caret.slopeRun > 0, "got %d\n", caret.slopeRun);
    ok(caret.offset == 0, "got %d\n", caret.offset);
    IDWriteFontFace1_Release(fontface1);

4638 4639
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4640
    DELETE_FONTFILE(path);
4641 4642
}

4643 4644 4645 4646 4647 4648
static void test_GetGlyphCount(void)
{
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    UINT16 count;
4649 4650
    WCHAR *path;
    HRESULT hr;
4651
    ULONG ref;
4652

4653
    path = create_testfontfile(test_fontfile);
4654 4655
    factory = create_factory();

4656
    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
4657 4658 4659 4660 4661 4662 4663
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    count = IDWriteFontFace_GetGlyphCount(fontface);
4664
    ok(count == 8, "got %u\n", count);
4665 4666

    IDWriteFontFace_Release(fontface);
4667 4668
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4669
    DELETE_FONTFILE(path);
4670 4671
}

4672 4673 4674 4675 4676 4677 4678 4679
static void test_GetKerningPairAdjustments(void)
{
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFontFile *file;
    WCHAR *path;
    HRESULT hr;
4680
    ULONG ref;
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698

    path = create_testfontfile(test_fontfile);
    factory = create_factory();

    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFactory_CreateFontFace(factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &file, 0, DWRITE_FONT_SIMULATIONS_NONE, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    if (hr == S_OK) {
        INT32 adjustments[1];

        hr = IDWriteFontFace1_GetKerningPairAdjustments(fontface1, 0, NULL, NULL);
        ok(hr == E_INVALIDARG || broken(hr == S_OK) /* win8 */, "got 0x%08x\n", hr);

4699 4700
        if (0) /* crashes on native */
            hr = IDWriteFontFace1_GetKerningPairAdjustments(fontface1, 1, NULL, NULL);
4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712

        adjustments[0] = 1;
        hr = IDWriteFontFace1_GetKerningPairAdjustments(fontface1, 1, NULL, adjustments);
        ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
        ok(adjustments[0] == 0, "got %d\n", adjustments[0]);

        IDWriteFontFace1_Release(fontface1);
    }
    else
        win_skip("GetKerningPairAdjustments() is not supported.\n");

    IDWriteFontFace_Release(fontface);
4713 4714
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4715 4716 4717
    DELETE_FONTFILE(path);
}

4718 4719 4720 4721 4722
static void test_CreateRenderingParams(void)
{
    IDWriteRenderingParams2 *params2;
    IDWriteRenderingParams1 *params1;
    IDWriteRenderingParams *params;
4723
    DWRITE_RENDERING_MODE mode;
4724
    IDWriteFactory3 *factory3;
4725 4726
    IDWriteFactory *factory;
    HRESULT hr;
4727
    ULONG ref;
4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760

    factory = create_factory();

    hr = IDWriteFactory_CreateCustomRenderingParams(factory, 1.0, 0.0, 0.0, DWRITE_PIXEL_GEOMETRY_FLAT,
        DWRITE_RENDERING_MODE_DEFAULT, &params);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteRenderingParams_QueryInterface(params, &IID_IDWriteRenderingParams1, (void**)&params1);
    if (hr == S_OK) {
        FLOAT enhcontrast;

        /* test what enhanced contrast setting set by default to */
        enhcontrast = IDWriteRenderingParams1_GetGrayscaleEnhancedContrast(params1);
        ok(enhcontrast == 1.0, "got %.2f\n", enhcontrast);
        IDWriteRenderingParams1_Release(params1);

        hr = IDWriteRenderingParams_QueryInterface(params, &IID_IDWriteRenderingParams2, (void**)&params2);
        if (hr == S_OK) {
            DWRITE_GRID_FIT_MODE gridfit;

            /* default gridfit mode */
            gridfit = IDWriteRenderingParams2_GetGridFitMode(params2);
            ok(gridfit == DWRITE_GRID_FIT_MODE_DEFAULT, "got %d\n", gridfit);

            IDWriteRenderingParams2_Release(params2);
        }
        else
            win_skip("IDWriteRenderingParams2 not supported.\n");
    }
    else
        win_skip("IDWriteRenderingParams1 not supported.\n");

    IDWriteRenderingParams_Release(params);
4761 4762 4763 4764 4765 4766 4767 4768

    hr = IDWriteFactory_CreateRenderingParams(factory, &params);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    mode = IDWriteRenderingParams_GetRenderingMode(params);
    ok(mode == DWRITE_RENDERING_MODE_DEFAULT, "got %d\n", mode);
    IDWriteRenderingParams_Release(params);

4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789
    hr = IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory3, (void**)&factory3);
    if (hr == S_OK) {
        IDWriteRenderingParams3 *params3;

        hr = IDWriteFactory3_CreateCustomRenderingParams(factory3, 1.0f, 0.0f, 0.0f, 1.0f, DWRITE_PIXEL_GEOMETRY_FLAT,
            DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC_DOWNSAMPLED, DWRITE_GRID_FIT_MODE_DEFAULT, &params3);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteRenderingParams3_QueryInterface(params3, &IID_IDWriteRenderingParams, (void**)&params);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        mode = IDWriteRenderingParams_GetRenderingMode(params);
        ok(mode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, "got %d\n", mode);

        IDWriteRenderingParams_Release(params);
        IDWriteRenderingParams3_Release(params3);
        IDWriteFactory3_Release(factory3);
    }
    else
        win_skip("IDWriteRenderingParams3 not supported.\n");

4790 4791
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
4792
}
4793

4794 4795
static void test_CreateGlyphRunAnalysis(void)
{
4796 4797 4798 4799 4800 4801 4802 4803
    static const DWRITE_RENDERING_MODE rendermodes[] = {
        DWRITE_RENDERING_MODE_ALIASED,
        DWRITE_RENDERING_MODE_GDI_CLASSIC,
        DWRITE_RENDERING_MODE_GDI_NATURAL,
        DWRITE_RENDERING_MODE_NATURAL,
        DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC,
    };

4804
    IDWriteGlyphRunAnalysis *analysis, *analysis2;
4805
    IDWriteRenderingParams *params;
4806
    IDWriteFactory3 *factory3;
4807
    IDWriteFactory2 *factory2;
4808 4809 4810
    IDWriteFactory *factory;
    DWRITE_GLYPH_RUN run;
    IDWriteFontFace *face;
4811
    UINT16 glyph, glyphs[10];
4812
    FLOAT advances[2];
4813 4814
    HRESULT hr;
    UINT32 ch;
4815
    RECT rect, rect2;
4816
    DWRITE_GLYPH_OFFSET offsets[2];
4817
    DWRITE_GLYPH_METRICS metrics;
4818
    DWRITE_FONT_METRICS fm;
4819
    DWRITE_MATRIX m;
4820 4821
    ULONG size;
    BYTE *bits;
4822
    ULONG ref;
4823
    int i;
4824 4825 4826 4827 4828

    factory = create_factory();
    face = create_fontface(factory);

    ch = 'A';
4829 4830
    glyph = 0;
    hr = IDWriteFontFace_GetGlyphIndices(face, &ch, 1, &glyph);
4831
    ok(hr == S_OK, "got 0x%08x\n", hr);
4832 4833 4834 4835
    ok(glyph > 0, "got %u\n", glyph);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(face, &glyph, 1, &metrics, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4836
    advances[0] = metrics.advanceWidth;
4837

4838 4839
    offsets[0].advanceOffset = 0.0;
    offsets[0].ascenderOffset = 0.0;
4840 4841 4842 4843

    run.fontFace = face;
    run.fontEmSize = 24.0;
    run.glyphCount = 1;
4844
    run.glyphIndices = &glyph;
4845 4846
    run.glyphAdvances = advances;
    run.glyphOffsets = offsets;
4847 4848 4849
    run.isSideways = FALSE;
    run.bidiLevel = 0;

4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
    /* zero ppdip */
    analysis = (void*)0xdeadbeef;
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 0.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(analysis == NULL, "got %p\n", analysis);

    /* negative ppdip */
    analysis = (void*)0xdeadbeef;
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, -1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(analysis == NULL, "got %p\n", analysis);

4866
    /* default mode is not allowed */
4867
    analysis = (void*)0xdeadbeef;
4868 4869 4870 4871
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_DEFAULT, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
4872
    ok(analysis == NULL, "got %p\n", analysis);
4873 4874

    /* outline too */
4875
    analysis = (void*)0xdeadbeef;
4876 4877 4878 4879
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_OUTLINE, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
4880
    ok(analysis == NULL, "got %p\n", analysis);
4881 4882

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
4883
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
4884 4885
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4886 4887 4888 4889 4890 4891 4892

    /* invalid texture type */
    memset(&rect, 0xcc, sizeof(rect));
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1+1, &rect);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(rect.left == 0 && rect.right == 0 &&
       rect.top == 0 && rect.bottom == 0, "unexpected rect\n");
4893 4894

    /* check how origin affects bounds */
4895
    SetRectEmpty(&rect);
4896 4897 4898 4899 4900
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect), "got empty rect\n");
    IDWriteGlyphRunAnalysis_Release(analysis);

4901 4902 4903 4904 4905
    /* doubled ppdip */
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 2.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);
4906
    SetRectEmpty(&rect2);
4907 4908 4909 4910 4911 4912
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(rect.right - rect.left < rect2.right - rect2.left, "expected wider rect\n");
    ok(rect.bottom - rect.top < rect2.bottom - rect2.top, "expected taller rect\n");
    IDWriteGlyphRunAnalysis_Release(analysis);

4913 4914 4915 4916 4917
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        10.0, -5.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

4918
    SetRectEmpty(&rect2);
4919 4920 4921
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect2), "got empty rect\n");
4922 4923
    IDWriteGlyphRunAnalysis_Release(analysis);

4924 4925 4926 4927
    ok(!EqualRect(&rect, &rect2), "got equal bounds\n");
    OffsetRect(&rect, 10, -5);
    ok(EqualRect(&rect, &rect2), "got different bounds\n");

4928 4929 4930 4931 4932 4933 4934
    for (i = 0; i < sizeof(rendermodes)/sizeof(rendermodes[0]); i++) {
        hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
            rendermodes[i], DWRITE_MEASURING_MODE_NATURAL,
            0.0, 0.0, &analysis);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        if (rendermodes[i] == DWRITE_RENDERING_MODE_ALIASED) {
4935
            SetRectEmpty(&rect);
4936 4937 4938
            hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(!IsRectEmpty(&rect), "got empty rect\n");
4939

4940
            SetRect(&rect, 0, 0, 1, 1);
4941 4942 4943 4944 4945
            hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(IsRectEmpty(&rect), "unexpected empty rect\n");
        }
        else {
4946
            SetRect(&rect, 0, 0, 1, 1);
4947 4948 4949 4950
            hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(IsRectEmpty(&rect), "got empty rect\n");

4951
            SetRectEmpty(&rect);
4952 4953 4954 4955 4956 4957 4958 4959
            hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(!IsRectEmpty(&rect), "got empty rect\n");
        }

        IDWriteGlyphRunAnalysis_Release(analysis);
    }

4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971
    IDWriteFontFace_GetMetrics(run.fontFace, &fm);

    /* check bbox for a single glyph run */
    for (run.fontEmSize = 1.0; run.fontEmSize <= 100.0; run.fontEmSize += 1.0) {
        DWRITE_GLYPH_METRICS gm;
        LONG bboxX, bboxY;

        hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
            DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
            0.0, 0.0, &analysis);
        ok(hr == S_OK, "got 0x%08x\n", hr);

4972
        SetRectEmpty(&rect);
4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985
        hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFace_GetGdiCompatibleGlyphMetrics(run.fontFace, run.fontEmSize, 1.0, NULL,
             DWRITE_MEASURING_MODE_GDI_CLASSIC, run.glyphIndices, 1, &gm, run.isSideways);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        /* metrics are in design units */
        bboxX = (int)floorf((gm.advanceWidth - gm.leftSideBearing - gm.rightSideBearing) * run.fontEmSize / fm.designUnitsPerEm + 0.5f);
        bboxY = (int)floorf((gm.advanceHeight - gm.topSideBearing - gm.bottomSideBearing) * run.fontEmSize / fm.designUnitsPerEm + 0.5f);

        rect.right -= rect.left;
        rect.bottom -= rect.top;
4986 4987
        ok(abs(bboxX - rect.right) <= 2, "%.0f: bbox width %d, from metrics %d\n", run.fontEmSize, rect.right, bboxX);
        ok(abs(bboxY - rect.bottom) <= 2, "%.0f: bbox height %d, from metrics %d\n", run.fontEmSize, rect.bottom, bboxY);
4988 4989 4990 4991

        IDWriteGlyphRunAnalysis_Release(analysis);
    }

4992 4993 4994 4995 4996
    /* without offsets */
    run.fontFace = face;
    run.fontEmSize = 24.0;
    run.glyphCount = 1;
    run.glyphIndices = &glyph;
4997
    run.glyphAdvances = advances;
4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035
    run.glyphOffsets = NULL;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&rect);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect), "got empty bounds\n");

    IDWriteGlyphRunAnalysis_Release(analysis);

    /* without explicit advances */
    run.fontFace = face;
    run.fontEmSize = 24.0;
    run.glyphCount = 1;
    run.glyphIndices = &glyph;
    run.glyphAdvances = NULL;
    run.glyphOffsets = NULL;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&rect);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect), "got empty bounds\n");

    IDWriteGlyphRunAnalysis_Release(analysis);

5036
    /* test that advances are scaled according to ppdip too */
5037
    glyphs[0] = glyphs[1] = glyph;
5038
    advances[0] = advances[1] = 100.0f;
5039 5040 5041 5042
    run.fontFace = face;
    run.fontEmSize = 24.0;
    run.glyphCount = 2;
    run.glyphIndices = glyphs;
5043
    run.glyphAdvances = advances;
5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057
    run.glyphOffsets = NULL;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&rect2);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect2), "got empty bounds\n");
    ok(!EqualRect(&rect, &rect2), "got wrong rect2\n");
5058 5059 5060 5061 5062 5063 5064
    ok((rect2.right - rect.left) > advances[0], "got rect width %d for advance %f\n", rect.right - rect.left, advances[0]);
    IDWriteGlyphRunAnalysis_Release(analysis);

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 2.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);
5065

5066 5067 5068 5069
    SetRectEmpty(&rect);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok((rect.right - rect.left) > 2 * advances[0], "got rect width %d for advance %f\n", rect.right - rect.left, advances[0]);
5070 5071
    IDWriteGlyphRunAnalysis_Release(analysis);

5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095
    /* with scaling transform */
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&rect);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&rect), "got rect width %d\n", rect.right - rect.left);
    IDWriteGlyphRunAnalysis_Release(analysis);

    memset(&m, 0, sizeof(m));
    m.m11 = 2.0;
    m.m22 = 1.0;
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, &m,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&rect2);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok((rect2.right - rect2.left) > (rect.right - rect.left), "got rect width %d\n", rect2.right - rect2.left);
5096 5097 5098 5099 5100 5101 5102 5103 5104

    /* instances are not reused for same runs */
    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, &m,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis2);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(analysis2 != analysis, "got %p, previous instance %p\n", analysis2, analysis);
    IDWriteGlyphRunAnalysis_Release(analysis2);

5105 5106
    IDWriteGlyphRunAnalysis_Release(analysis);

5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133
    if (IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory2, (void **)&factory2) == S_OK) {
        FLOAT gamma, contrast, cleartype_level;

        /* Invalid antialias mode. */
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_ALIASED,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DEFAULT, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE + 1,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid grid fit mode. */
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_ALIASED,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_ENABLED + 1, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid rendering mode. */
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_OUTLINE,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_ENABLED, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid measuring mode. */
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_ALIASED,
                DWRITE_MEASURING_MODE_GDI_NATURAL + 1, DWRITE_GRID_FIT_MODE_ENABLED, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

5134
        /* Win8 does not accept default grid fitting mode. */
5135 5136 5137
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_NATURAL,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DEFAULT, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f,  0.0f, &analysis);
5138 5139 5140
        ok(hr == S_OK || broken(hr == E_INVALIDARG) /* Win8 */, "Failed to create analysis, hr %#x.\n", hr);
        if (hr == S_OK)
            IDWriteGlyphRunAnalysis_Release(analysis);
5141

5142 5143 5144 5145 5146
        /* Natural mode, grayscale antialiased. */
        hr = IDWriteFactory2_CreateGlyphRunAnalysis(factory2, &run, NULL, DWRITE_RENDERING_MODE_NATURAL,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DISABLED, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f,  0.0f, &analysis);
        ok(hr == S_OK, "Failed to create analysis, hr %#x.\n", hr);
5147

5148 5149 5150 5151
        SetRect(&rect, 0, 1, 0, 1);
        hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
        ok(hr == S_OK, "Failed to get texture bounds, hr %#x.\n", hr);
        ok(IsRectEmpty(&rect), "Expected empty bbox.\n");
5152

5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186
        SetRectEmpty(&rect);
        hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
        ok(hr == S_OK, "Failed to get texture bounds, hr %#x.\n", hr);
        ok(!IsRectEmpty(&rect), "Unexpected empty bbox.\n");

        size = (rect.right - rect.left) * (rect.bottom - rect.top);
        bits = HeapAlloc(GetProcessHeap(), 0, size);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect, bits, size);
        ok(hr == S_OK, "Failed to get alpha texture, hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect, bits, size - 1);
        ok(hr == E_NOT_SUFFICIENT_BUFFER, "Unexpected hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect, bits, size);
        ok(hr == DWRITE_E_UNSUPPORTEDOPERATION, "Unexpected hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect, bits, size - 1);
    todo_wine
        ok(hr == DWRITE_E_UNSUPPORTEDOPERATION, "Unexpected hr %#x.\n", hr);

        HeapFree(GetProcessHeap(), 0, bits);

        hr = IDWriteFactory_CreateCustomRenderingParams(factory, 0.1f, 0.0f, 1.0f, DWRITE_PIXEL_GEOMETRY_FLAT,
                DWRITE_RENDERING_MODE_NATURAL, &params);
        ok(hr == S_OK, "Failed to create custom parameters, hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_GetAlphaBlendParams(analysis, params, &gamma, &contrast, &cleartype_level);
        ok(hr == S_OK, "Failed to get alpha blend params, hr %#x.\n", hr);
    todo_wine
        ok(cleartype_level == 0.0f, "Unexpected cleartype level %f.\n", cleartype_level);

        IDWriteRenderingParams_Release(params);
        IDWriteGlyphRunAnalysis_Release(analysis);
5187 5188 5189 5190

        IDWriteFactory2_Release(factory2);
    }

5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261
    if (IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory3, (void **)&factory3) == S_OK) {

        /* Invalid antialias mode. */
        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_ALIASED,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DEFAULT, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE + 1,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid grid fit mode. */
        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_ALIASED,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_ENABLED + 1, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid rendering mode. */
        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_OUTLINE,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_ENABLED, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        /* Invalid measuring mode. */
        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_ALIASED,
                DWRITE_MEASURING_MODE_GDI_NATURAL + 1, DWRITE_GRID_FIT_MODE_ENABLED,
                DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE, 0.0f, 0.0f, &analysis);
        ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_NATURAL,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DEFAULT, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f,  0.0f, &analysis);
        ok(hr == S_OK, "Failed to create analysis, hr %#x.\n", hr);
        IDWriteGlyphRunAnalysis_Release(analysis);

        /* Natural mode, grayscale antialiased. */
        hr = IDWriteFactory3_CreateGlyphRunAnalysis(factory3, &run, NULL, DWRITE_RENDERING_MODE1_NATURAL,
                DWRITE_MEASURING_MODE_NATURAL, DWRITE_GRID_FIT_MODE_DISABLED, DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE,
                0.0f,  0.0f, &analysis);
        ok(hr == S_OK, "Failed to create analysis, hr %#x.\n", hr);

        SetRect(&rect, 0, 1, 0, 1);
        hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
        ok(hr == S_OK, "Failed to get texture bounds, hr %#x.\n", hr);
        ok(IsRectEmpty(&rect), "Expected empty bbox.\n");

        SetRectEmpty(&rect);
        hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect);
        ok(hr == S_OK, "Failed to get texture bounds, hr %#x.\n", hr);
        ok(!IsRectEmpty(&rect), "Unexpected empty bbox.\n");

        size = (rect.right - rect.left) * (rect.bottom - rect.top);
        bits = HeapAlloc(GetProcessHeap(), 0, size);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect, bits, size);
        ok(hr == S_OK, "Failed to get alpha texture, hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &rect, bits, size - 1);
        ok(hr == E_NOT_SUFFICIENT_BUFFER, "Unexpected hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect, bits, size);
        ok(hr == DWRITE_E_UNSUPPORTEDOPERATION, "Unexpected hr %#x.\n", hr);

        hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &rect, bits, size - 1);
    todo_wine
        ok(hr == DWRITE_E_UNSUPPORTEDOPERATION, "Unexpected hr %#x.\n", hr);

        HeapFree(GetProcessHeap(), 0, bits);

        IDWriteGlyphRunAnalysis_Release(analysis);

        IDWriteFactory3_Release(factory3);
    }

5262
    IDWriteFontFace_Release(face);
5263 5264
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
5265 5266
}

5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
#define round(x) ((int)floor((x) + 0.5))

struct VDMX_Header
{
    WORD version;
    WORD numRecs;
    WORD numRatios;
};

struct VDMX_Ratio
{
    BYTE bCharSet;
    BYTE xRatio;
    BYTE yStartRatio;
    BYTE yEndRatio;
};

struct VDMX_group
{
    WORD recs;
    BYTE startsz;
    BYTE endsz;
};

struct VDMX_vTable
{
    WORD yPelHeight;
    SHORT yMax;
    SHORT yMin;
};

static const struct VDMX_group *find_vdmx_group(const struct VDMX_Header *hdr)
{
    WORD num_ratios, i, group_offset = 0;
    struct VDMX_Ratio *ratios = (struct VDMX_Ratio*)(hdr + 1);
    BYTE dev_x_ratio = 1, dev_y_ratio = 1;

    num_ratios = GET_BE_WORD(hdr->numRatios);

    for (i = 0; i < num_ratios; i++)
    {
        if (!ratios[i].bCharSet) continue;

        if ((ratios[i].xRatio == 0 && ratios[i].yStartRatio == 0 &&
             ratios[i].yEndRatio == 0) ||
	   (ratios[i].xRatio == dev_x_ratio && ratios[i].yStartRatio <= dev_y_ratio &&
            ratios[i].yEndRatio >= dev_y_ratio))
        {
            group_offset = GET_BE_WORD(*((WORD *)(ratios + num_ratios) + i));
            break;
        }
    }
    if (group_offset)
        return (const struct VDMX_group *)((BYTE *)hdr + group_offset);
    return NULL;
}

static BOOL get_vdmx_size(const struct VDMX_group *group, int emsize, int *a, int *d)
{
    WORD recs, i;
    const struct VDMX_vTable *tables;

    if (!group) return FALSE;

    recs = GET_BE_WORD(group->recs);
    if (emsize < group->startsz || emsize >= group->endsz) return FALSE;

    tables = (const struct VDMX_vTable *)(group + 1);
    for (i = 0; i < recs; i++)
    {
        WORD ppem = GET_BE_WORD(tables[i].yPelHeight);
        if (ppem > emsize)
        {
            /* FIXME: Supposed to interpolate */
            trace("FIXME interpolate %d\n", emsize);
            return FALSE;
        }

        if (ppem == emsize)
        {
            *a = (SHORT)GET_BE_WORD(tables[i].yMax);
            *d = -(SHORT)GET_BE_WORD(tables[i].yMin);
            return TRUE;
        }
    }
    return FALSE;
}

5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
static void test_metrics_cmp(FLOAT emsize, const DWRITE_FONT_METRICS *metrics, const DWRITE_FONT_METRICS1 *expected)
{
    ok(metrics->designUnitsPerEm == expected->designUnitsPerEm, "%.2f: emsize: got %u expect %u\n",
           emsize, metrics->designUnitsPerEm, expected->designUnitsPerEm);
    ok(metrics->ascent == expected->ascent, "%.2f a: got %u expect %u\n",
           emsize, metrics->ascent, expected->ascent);
    ok(metrics->descent == expected->descent, "%.2f d: got %u expect %u\n",
           emsize, metrics->descent, expected->descent);
    ok(metrics->lineGap == expected->lineGap, "%.2f lg: got %d expect %d\n",
           emsize, metrics->lineGap, expected->lineGap);
    ok(metrics->capHeight == expected->capHeight, "%.2f capH: got %u expect %u\n",
           emsize, metrics->capHeight, expected->capHeight);
    ok(metrics->xHeight == expected->xHeight, "%.2f xH: got %u expect %u\n",
           emsize, metrics->xHeight, expected->xHeight);
    ok(metrics->underlinePosition == expected->underlinePosition, "%.2f ulP: got %d expect %d\n",
            emsize, metrics->underlinePosition, expected->underlinePosition);
    ok(metrics->underlineThickness == expected->underlineThickness, "%.2f ulTh: got %u expect %u\n",
           emsize, metrics->underlineThickness, expected->underlineThickness);
    ok(metrics->strikethroughPosition == expected->strikethroughPosition, "%.2f stP: got %d expect %d\n",
           emsize, metrics->strikethroughPosition, expected->strikethroughPosition);
    ok(metrics->strikethroughThickness == expected->strikethroughThickness, "%.2f stTh: got %u expect %u\n",
           emsize, metrics->strikethroughThickness, expected->strikethroughThickness);
}

static void test_metrics1_cmp(FLOAT emsize, const DWRITE_FONT_METRICS1 *metrics, const DWRITE_FONT_METRICS1 *expected)
{
    ok(metrics->designUnitsPerEm == expected->designUnitsPerEm, "%.2f: emsize: got %u expect %u\n",
           emsize, metrics->designUnitsPerEm, expected->designUnitsPerEm);
    ok(metrics->ascent == expected->ascent, "%.2f a: got %u expect %u\n",
           emsize, metrics->ascent, expected->ascent);
    ok(metrics->descent == expected->descent, "%.2f d: got %u expect %u\n",
           emsize, metrics->descent, expected->descent);
    ok(metrics->lineGap == expected->lineGap, "%.2f lg: got %d expect %d\n",
           emsize, metrics->lineGap, expected->lineGap);
    ok(metrics->capHeight == expected->capHeight, "%.2f capH: got %u expect %u\n",
           emsize, metrics->capHeight, expected->capHeight);
    ok(metrics->xHeight == expected->xHeight, "%.2f xH: got %u expect %u\n",
           emsize, metrics->xHeight, expected->xHeight);
    ok(metrics->underlinePosition == expected->underlinePosition, "%.2f ulP: got %d expect %d\n",
            emsize, metrics->underlinePosition, expected->underlinePosition);
    ok(metrics->underlineThickness == expected->underlineThickness, "%.2f ulTh: got %u expect %u\n",
           emsize, metrics->underlineThickness, expected->underlineThickness);
    ok(metrics->strikethroughPosition == expected->strikethroughPosition, "%.2f stP: got %d expect %d\n",
           emsize, metrics->strikethroughPosition, expected->strikethroughPosition);
    ok(metrics->strikethroughThickness == expected->strikethroughThickness, "%.2f stTh: got %u expect %u\n",
           emsize, metrics->strikethroughThickness, expected->strikethroughThickness);
    ok(metrics->glyphBoxLeft == expected->glyphBoxLeft, "%.2f box left: got %d expect %d\n",
           emsize, metrics->glyphBoxLeft, expected->glyphBoxLeft);
if (0) { /* this is not consistent */
    ok(metrics->glyphBoxTop == expected->glyphBoxTop, "%.2f box top: got %d expect %d\n",
           emsize, metrics->glyphBoxTop, expected->glyphBoxTop);
    ok(metrics->glyphBoxRight == expected->glyphBoxRight, "%.2f box right: got %d expect %d\n",
           emsize, metrics->glyphBoxRight, expected->glyphBoxRight);
}
    ok(metrics->glyphBoxBottom == expected->glyphBoxBottom, "%.2f box bottom: got %d expect %d\n",
           emsize, metrics->glyphBoxBottom, expected->glyphBoxBottom);
    ok(metrics->subscriptPositionX == expected->subscriptPositionX, "%.2f subX: got %d expect %d\n",
           emsize, metrics->subscriptPositionX, expected->subscriptPositionX);
    ok(metrics->subscriptPositionY == expected->subscriptPositionY, "%.2f subY: got %d expect %d\n",
           emsize, metrics->subscriptPositionY, expected->subscriptPositionY);
    ok(metrics->subscriptSizeX == expected->subscriptSizeX, "%.2f subsizeX: got %d expect %d\n",
           emsize, metrics->subscriptSizeX, expected->subscriptSizeX);
    ok(metrics->subscriptPositionY == expected->subscriptPositionY, "%.2f subsizeY: got %d expect %d\n",
           emsize, metrics->subscriptSizeY, expected->subscriptSizeY);
    ok(metrics->superscriptPositionX == expected->superscriptPositionX, "%.2f supX: got %d expect %d\n",
           emsize, metrics->superscriptPositionX, expected->superscriptPositionX);
if (0)
    ok(metrics->superscriptPositionY == expected->superscriptPositionY, "%.2f supY: got %d expect %d\n",
           emsize, metrics->superscriptPositionY, expected->superscriptPositionY);
    ok(metrics->superscriptSizeX == expected->superscriptSizeX, "%.2f supsizeX: got %d expect %d\n",
           emsize, metrics->superscriptSizeX, expected->superscriptSizeX);
    ok(metrics->superscriptSizeY == expected->superscriptSizeY, "%.2f supsizeY: got %d expect %d\n",
           emsize, metrics->superscriptSizeY, expected->superscriptSizeY);
    ok(metrics->hasTypographicMetrics == expected->hasTypographicMetrics, "%.2f hastypo: got %d expect %d\n",
           emsize, metrics->hasTypographicMetrics, expected->hasTypographicMetrics);
}

5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458
struct compatmetrics_test {
    DWRITE_MATRIX m;
    FLOAT ppdip;
    FLOAT emsize;
};

static struct compatmetrics_test compatmetrics_tests[] = {
    { { 0.0, 0.0, 0.0,  1.0, 0.0, 0.0 }, 1.0, 5.0 },
    { { 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, 1.0, 5.0 },
    { { 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, 2.0, 5.0 },
    { { 0.0, 0.0, 0.0,  3.0, 0.0, 0.0 }, 2.0, 5.0 },
    { { 0.0, 0.0, 0.0, -3.0, 0.0, 0.0 }, 2.0, 5.0 },
    { { 1.0, 0.0, 0.0,  1.0, 0.0, 0.0 }, 2.0, 5.0 },
    { { 1.0, 0.0, 0.0,  1.0, 5.0, 0.0 }, 2.0, 5.0 },
    { { 1.0, 0.0, 0.0,  1.0, 0.0, 5.0 }, 2.0, 5.0 },
};

static void get_expected_metrics(IDWriteFontFace *fontface, struct compatmetrics_test *ptr,
    DWRITE_FONT_METRICS *expected)
{
    HRESULT hr;

    memset(expected, 0, sizeof(*expected));
    hr = IDWriteFontFace_GetGdiCompatibleMetrics(fontface, ptr->ppdip * fabsf(ptr->m.m22) * ptr->emsize, 1.0, NULL, expected);
    ok(hr == S_OK, "got %08x\n", hr);
}

5459
static void test_gdicompat_metrics(IDWriteFontFace *face)
5460
{
5461
    IDWriteFontFace1 *fontface1 = NULL;
5462
    HRESULT hr;
5463 5464
    DWRITE_FONT_METRICS design_metrics, comp_metrics;
    DWRITE_FONT_METRICS1 design_metrics1, expected;
5465
    FLOAT emsize, scale;
5466
    int ascent, descent;
5467 5468 5469 5470 5471
    const struct VDMX_Header *vdmx;
    UINT32 vdmx_len;
    void *vdmx_ctx;
    BOOL exists;
    const struct VDMX_group *vdmx_group = NULL;
5472
    int i;
5473

5474 5475 5476 5477 5478 5479 5480 5481 5482 5483
    hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace1, (void**)&fontface1);
    if (hr != S_OK)
        win_skip("gdi compatible DWRITE_FONT_METRICS1 are not supported.\n");

    if (fontface1) {
        IDWriteFontFace1_GetMetrics(fontface1, &design_metrics1);
        memcpy(&design_metrics, &design_metrics1, sizeof(design_metrics));
    }
    else
        IDWriteFontFace_GetMetrics(face, &design_metrics);
5484 5485 5486 5487 5488 5489 5490 5491

    hr = IDWriteFontFace_TryGetFontTable(face, MS_VDMX_TAG, (const void **)&vdmx,
                                         &vdmx_len, &vdmx_ctx, &exists);
    if (hr != S_OK || !exists)
        vdmx = NULL;
    else
        vdmx_group = find_vdmx_group(vdmx);

5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
    /* negative emsize */
    memset(&comp_metrics, 0xcc, sizeof(comp_metrics));
    memset(&expected, 0, sizeof(expected));
    hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, -10.0, 1.0, NULL, &comp_metrics);
    ok(hr == E_INVALIDARG, "got %08x\n", hr);
    test_metrics_cmp(0.0, &comp_metrics, &expected);

    /* zero emsize */
    memset(&comp_metrics, 0xcc, sizeof(comp_metrics));
    memset(&expected, 0, sizeof(expected));
    hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, 0.0, 1.0, NULL, &comp_metrics);
    ok(hr == E_INVALIDARG, "got %08x\n", hr);
    test_metrics_cmp(0.0, &comp_metrics, &expected);

5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
    /* zero pixels per dip */
    memset(&comp_metrics, 0xcc, sizeof(comp_metrics));
    memset(&expected, 0, sizeof(expected));
    hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, 5.0, 0.0, NULL, &comp_metrics);
    ok(hr == E_INVALIDARG, "got %08x\n", hr);
    test_metrics_cmp(5.0, &comp_metrics, &expected);

    memset(&comp_metrics, 0xcc, sizeof(comp_metrics));
    hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, 5.0, -1.0, NULL, &comp_metrics);
    ok(hr == E_INVALIDARG, "got %08x\n", hr);
    test_metrics_cmp(5.0, &comp_metrics, &expected);

5518 5519
    for (i = 0; i < sizeof(compatmetrics_tests)/sizeof(compatmetrics_tests[0]); i++) {
        struct compatmetrics_test *ptr = &compatmetrics_tests[i];
5520

5521 5522 5523 5524 5525
        get_expected_metrics(face, ptr, (DWRITE_FONT_METRICS*)&expected);
        hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, ptr->emsize, ptr->ppdip, &ptr->m, &comp_metrics);
        ok(hr == S_OK, "got %08x\n", hr);
        test_metrics_cmp(ptr->emsize, &comp_metrics, &expected);
    }
5526

5527 5528
    for (emsize = 5; emsize <= design_metrics.designUnitsPerEm; emsize++)
    {
5529 5530 5531 5532 5533 5534 5535 5536 5537 5538
        DWRITE_FONT_METRICS1 comp_metrics1, expected;

        if (fontface1) {
            hr = IDWriteFontFace1_GetGdiCompatibleMetrics(fontface1, emsize, 1.0, NULL, &comp_metrics1);
            ok(hr == S_OK, "got %08x\n", hr);
        }
        else {
            hr = IDWriteFontFace_GetGdiCompatibleMetrics(face, emsize, 1.0, NULL, &comp_metrics);
            ok(hr == S_OK, "got %08x\n", hr);
        }
5539

5540
        scale = emsize / design_metrics.designUnitsPerEm;
5541 5542 5543 5544 5545 5546
        if (!get_vdmx_size(vdmx_group, emsize, &ascent, &descent))
        {
            ascent = round(design_metrics.ascent * scale);
            descent = round(design_metrics.descent * scale);
        }

5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570
        expected.designUnitsPerEm = design_metrics.designUnitsPerEm;
        expected.ascent = round(ascent / scale );
        expected.descent = round(descent / scale );
        expected.lineGap = round(round(design_metrics.lineGap * scale) / scale);
        expected.capHeight = round(round(design_metrics.capHeight * scale) / scale);
        expected.xHeight = round(round(design_metrics.xHeight * scale) / scale);
        expected.underlinePosition = round(round(design_metrics.underlinePosition * scale) / scale);
        expected.underlineThickness = round(round(design_metrics.underlineThickness * scale) / scale);
        expected.strikethroughPosition = round(round(design_metrics.strikethroughPosition * scale) / scale);
        expected.strikethroughThickness = round(round(design_metrics.strikethroughThickness * scale) / scale);

        if (fontface1) {
            expected.glyphBoxLeft = round(round(design_metrics1.glyphBoxLeft * scale) / scale);

        if (0) { /* those two fail on Tahoma and Win7 */
            expected.glyphBoxTop = round(round(design_metrics1.glyphBoxTop * scale) / scale);
            expected.glyphBoxRight = round(round(design_metrics1.glyphBoxRight * scale) / scale);
        }
            expected.glyphBoxBottom = round(round(design_metrics1.glyphBoxBottom * scale) / scale);
            expected.subscriptPositionX = round(round(design_metrics1.subscriptPositionX * scale) / scale);
            expected.subscriptPositionY = round(round(design_metrics1.subscriptPositionY * scale) / scale);
            expected.subscriptSizeX = round(round(design_metrics1.subscriptSizeX * scale) / scale);
            expected.subscriptSizeY = round(round(design_metrics1.subscriptSizeY * scale) / scale);
            expected.superscriptPositionX = round(round(design_metrics1.superscriptPositionX * scale) / scale);
5571
        if (0) /* this fails for 3 emsizes, Tahoma from [5, 2048] range */ {
5572
            expected.superscriptPositionY = round(round(design_metrics1.superscriptPositionY * scale) / scale);
5573
        }
5574 5575 5576 5577 5578 5579 5580 5581
            expected.superscriptSizeX = round(round(design_metrics1.superscriptSizeX * scale) / scale);
            expected.superscriptSizeY = round(round(design_metrics1.superscriptSizeY * scale) / scale);
            expected.hasTypographicMetrics = design_metrics1.hasTypographicMetrics;

            test_metrics1_cmp(emsize, &comp_metrics1, &expected);
        }
        else
            test_metrics_cmp(emsize, &comp_metrics, &expected);
5582 5583 5584

    }

5585 5586
    if (fontface1)
        IDWriteFontFace1_Release(fontface1);
5587 5588 5589 5590 5591 5592 5593 5594 5595
    if (vdmx) IDWriteFontFace_ReleaseFontTable(face, vdmx_ctx);
}

static void test_GetGdiCompatibleMetrics(void)
{
    IDWriteFactory *factory;
    IDWriteFont *font;
    IDWriteFontFace *fontface;
    HRESULT hr;
5596
    ULONG ref;
5597 5598 5599 5600 5601 5602 5603

    factory = create_factory();

    font = get_font(factory, tahomaW, DWRITE_FONT_STYLE_NORMAL);
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);
5604
    test_gdicompat_metrics(fontface);
5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615
    IDWriteFontFace_Release(fontface);

    font = get_font(factory, arialW, DWRITE_FONT_STYLE_NORMAL);
    if (!font)
        skip("Skipping tests with Arial\n");
    else
    {
        hr = IDWriteFont_CreateFontFace(font, &fontface);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFont_Release(font);

5616
        test_gdicompat_metrics(fontface);
5617 5618 5619
        IDWriteFontFace_Release(fontface);
    }

5620 5621
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
5622 5623
}

5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
static void get_expected_panose(IDWriteFont1 *font, DWRITE_PANOSE *panose)
{
    IDWriteFontFace *fontface;
    const TT_OS2_V2 *tt_os2;
    void *os2_context;
    UINT32 size;
    BOOL exists;
    HRESULT hr;

    memset(panose, 0, sizeof(*panose));

    hr = IDWriteFont1_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_0S2_TAG, (const void **)&tt_os2, &size, &os2_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    if (tt_os2) {
        memcpy(panose, &tt_os2->panose, sizeof(*panose));
        IDWriteFontFace_ReleaseFontTable(fontface, os2_context);
    }

    IDWriteFontFace_Release(fontface);
}

5649 5650
static void test_GetPanose(void)
{
5651
    IDWriteFontCollection *syscollection;
5652 5653 5654
    IDWriteFactory *factory;
    IDWriteFont1 *font1;
    IDWriteFont *font;
5655
    UINT count, i;
5656
    HRESULT hr;
5657
    ULONG ref;
5658 5659 5660 5661

    factory = create_factory();
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_NORMAL);

5662
    hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont1, (void **)&font1);
5663
    IDWriteFont_Release(font);
5664 5665

    if (FAILED(hr)) {
5666 5667
        ref = IDWriteFactory_Release(factory);
        ok(ref == 0, "factory not released, %u\n", ref);
5668 5669 5670
        win_skip("GetPanose() is not supported.\n");
        return;
    }
5671
    IDWriteFont1_Release(font1);
5672 5673 5674 5675 5676 5677 5678 5679

    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
        DWRITE_PANOSE panose, expected_panose;
        IDWriteLocalizedStrings *names;
5680 5681
        IDWriteFontFace3 *fontface3;
        IDWriteFontFace *fontface;
5682 5683 5684 5685
        IDWriteFontFamily *family;
        IDWriteFont1 *font1;
        IDWriteFont *font;
        WCHAR nameW[256];
5686

5687 5688
        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);
5689

5690 5691 5692
        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
            DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);
5693

5694 5695 5696 5697 5698
        hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont1, (void **)&font1);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFont_Release(font);

        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
5699 5700
        ok(hr == S_OK, "got 0x%08x\n", hr);

5701
        get_enus_string(names, nameW, sizeof(nameW)/sizeof(nameW[0]));
5702

5703
        IDWriteLocalizedStrings_Release(names);
5704

5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733
        IDWriteFont1_GetPanose(font1, &panose);
        get_expected_panose(font1, &expected_panose);

        ok(panose.values[0] == expected_panose.values[0], "%s: values[0] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[0], expected_panose.values[0]);
        ok(panose.values[1] == expected_panose.values[1], "%s: values[1] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[1], expected_panose.values[1]);
        ok(panose.values[2] == expected_panose.values[2], "%s: values[2] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[2], expected_panose.values[2]);
        ok(panose.values[3] == expected_panose.values[3], "%s: values[3] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[3], expected_panose.values[3]);
        ok(panose.values[4] == expected_panose.values[4], "%s: values[4] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[4], expected_panose.values[4]);
        ok(panose.values[5] == expected_panose.values[5], "%s: values[5] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[5], expected_panose.values[5]);
        ok(panose.values[6] == expected_panose.values[6], "%s: values[6] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[6], expected_panose.values[6]);
        ok(panose.values[7] == expected_panose.values[7], "%s: values[7] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[7], expected_panose.values[7]);
        ok(panose.values[8] == expected_panose.values[8], "%s: values[8] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[8], expected_panose.values[8]);
        ok(panose.values[9] == expected_panose.values[9], "%s: values[9] %#x, expected %#x.\n", wine_dbgstr_w(nameW),
            panose.values[9], expected_panose.values[9]);

        hr = IDWriteFont1_CreateFontFace(font1, &fontface);
        ok(hr == S_OK, "Failed to create a font face, %#x.\n", hr);
        if (IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace3, (void **)&fontface3) == S_OK) {
            ok(!memcmp(&panose, &expected_panose, sizeof(panose)), "%s: Unexpected panose from font face.\n",
                wine_dbgstr_w(nameW));
5734 5735
            IDWriteFontFace3_Release(fontface3);
        }
5736
        IDWriteFontFace_Release(fontface);
5737

5738
        IDWriteFont1_Release(font1);
5739
        IDWriteFontFamily_Release(family);
5740 5741
    }

5742
    IDWriteFontCollection_Release(syscollection);
5743 5744
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
5745 5746
}

5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
static INT32 get_gdi_font_advance(HDC hdc, FLOAT emsize)
{
    LOGFONTW logfont;
    HFONT hfont;
    BOOL ret;
    ABC abc;

    memset(&logfont, 0, sizeof(logfont));
    logfont.lfHeight = (LONG)-emsize;
    logfont.lfWeight = FW_NORMAL;
    logfont.lfQuality = CLEARTYPE_QUALITY;
    lstrcpyW(logfont.lfFaceName, tahomaW);

    hfont = CreateFontIndirectW(&logfont);
    SelectObject(hdc, hfont);

    ret = GetCharABCWidthsW(hdc, 'A', 'A', &abc);
    ok(ret, "got %d\n", ret);

    DeleteObject(hfont);

    return abc.abcA + abc.abcB + abc.abcC;
}

static void test_GetGdiCompatibleGlyphAdvances(void)
{
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFont *font;
    HRESULT hr;
    HDC hdc;
    UINT32 codepoint;
    UINT16 glyph;
    FLOAT emsize;
    DWRITE_FONT_METRICS1 fm;
    INT32 advance;
5784
    ULONG ref;
5785 5786 5787 5788 5789 5790 5791 5792

    factory = create_factory();
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_NORMAL);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

5793
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void **)&fontface1);
5794 5795 5796
    IDWriteFontFace_Release(fontface);

    if (hr != S_OK) {
5797 5798
        ref = IDWriteFactory_Release(factory);
        ok(ref == 0, "factory not released, %u\n", ref);
5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850
        win_skip("GetGdiCompatibleGlyphAdvances() is not supported\n");
        return;
    }

    codepoint = 'A';
    glyph = 0;
    hr = IDWriteFontFace1_GetGlyphIndices(fontface1, &codepoint, 1, &glyph);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyph > 0, "got %u\n", glyph);

    /* zero emsize */
    advance = 1;
    hr = IDWriteFontFace1_GetGdiCompatibleGlyphAdvances(fontface1, 0.0,
        1.0, NULL, FALSE, FALSE, 1, &glyph, &advance);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(advance == 0, "got %d\n", advance);

    /* negative emsize */
    advance = 1;
    hr = IDWriteFontFace1_GetGdiCompatibleGlyphAdvances(fontface1, -1.0,
        1.0, NULL, FALSE, FALSE, 1, &glyph, &advance);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(advance == 0, "got %d\n", advance);

    /* zero ppdip */
    advance = 1;
    hr = IDWriteFontFace1_GetGdiCompatibleGlyphAdvances(fontface1, 1.0,
        0.0, NULL, FALSE, FALSE, 1, &glyph, &advance);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(advance == 0, "got %d\n", advance);

    /* negative ppdip */
    advance = 1;
    hr = IDWriteFontFace1_GetGdiCompatibleGlyphAdvances(fontface1, 1.0,
        -1.0, NULL, FALSE, FALSE, 1, &glyph, &advance);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(advance == 0, "got %d\n", advance);

    IDWriteFontFace1_GetMetrics(fontface1, &fm);

    hdc = CreateCompatibleDC(0);

    for (emsize = 1.0; emsize <= fm.designUnitsPerEm; emsize += 1.0) {
        INT32 gdi_advance;

        gdi_advance = get_gdi_font_advance(hdc, emsize);
        hr = IDWriteFontFace1_GetGdiCompatibleGlyphAdvances(fontface1, emsize,
            1.0, NULL, FALSE, FALSE, 1, &glyph, &advance);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        /* advance is in design units */
        advance = (int)floorf(emsize * advance / fm.designUnitsPerEm + 0.5f);
5851
        ok((advance - gdi_advance) <= 2, "%.0f: got advance %d, expected %d\n", emsize, advance, gdi_advance);
5852 5853 5854 5855
    }

    DeleteObject(hdc);

5856 5857 5858
    IDWriteFontFace1_Release(fontface1);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
5859 5860
}

5861
static WORD get_gasp_flags(IDWriteFontFace *fontface, FLOAT emsize, FLOAT ppdip)
5862 5863 5864 5865 5866 5867 5868 5869 5870
{
    WORD num_recs, version;
    const WORD *ptr;
    WORD flags = 0;
    UINT32 size;
    BOOL exists;
    void *ctxt;
    HRESULT hr;

5871 5872
    emsize *= ppdip;

5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982
    exists = FALSE;
    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_GASP_TAG,
        (const void**)&ptr, &size, &ctxt, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    if (!exists)
        goto done;

    version  = GET_BE_WORD( *ptr++ );
    num_recs = GET_BE_WORD( *ptr++ );
    if (version > 1 || size < (num_recs * 2 + 2) * sizeof(WORD)) {
        ok(0, "unsupported gasp table: ver %d size %d recs %d\n", version, size, num_recs);
        goto done;
    }

    while (num_recs--)
    {
        flags = GET_BE_WORD( *(ptr + 1) );
        if (emsize <= GET_BE_WORD( *ptr )) break;
        ptr += 2;
    }

done:
    IDWriteFontFace_ReleaseFontTable(fontface, ctxt);
    return flags;
}

#define GASP_GRIDFIT             0x0001
#define GASP_DOGRAY              0x0002
#define GASP_SYMMETRIC_GRIDFIT   0x0004
#define GASP_SYMMETRIC_SMOOTHING 0x0008

static BOOL g_is_vista;
static DWRITE_RENDERING_MODE get_expected_rendering_mode(FLOAT emsize, WORD gasp, DWRITE_MEASURING_MODE mode,
    DWRITE_OUTLINE_THRESHOLD threshold)
{
    static const FLOAT aa_threshold = 100.0f;
    static const FLOAT a_threshold = 350.0f;
    static const FLOAT naturalemsize = 20.0f;
    FLOAT v;

    /* outline threshold */
    if (g_is_vista)
        v = mode == DWRITE_MEASURING_MODE_NATURAL ? aa_threshold : a_threshold;
    else
        v = threshold == DWRITE_OUTLINE_THRESHOLD_ANTIALIASED ? aa_threshold : a_threshold;

    if (emsize >= v)
        return DWRITE_RENDERING_MODE_OUTLINE;

    switch (mode)
    {
    case DWRITE_MEASURING_MODE_NATURAL:
        if (!(gasp & GASP_SYMMETRIC_SMOOTHING) && (emsize <= naturalemsize))
            return DWRITE_RENDERING_MODE_NATURAL;
        else
            return DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
    case DWRITE_MEASURING_MODE_GDI_CLASSIC:
        return DWRITE_RENDERING_MODE_GDI_CLASSIC;
    case DWRITE_MEASURING_MODE_GDI_NATURAL:
        return DWRITE_RENDERING_MODE_GDI_NATURAL;
    default:
        ;
    }

    /* should be unreachable */
    return DWRITE_RENDERING_MODE_DEFAULT;
}

static DWRITE_GRID_FIT_MODE get_expected_gridfit_mode(FLOAT emsize, WORD gasp, DWRITE_MEASURING_MODE mode,
    DWRITE_OUTLINE_THRESHOLD threshold)
{
    static const FLOAT aa_threshold = 100.0f;
    static const FLOAT a_threshold = 350.0f;
    FLOAT v;

    v = threshold == DWRITE_OUTLINE_THRESHOLD_ANTIALIASED ? aa_threshold : a_threshold;
    if (emsize >= v)
        return DWRITE_GRID_FIT_MODE_DISABLED;

    if (mode == DWRITE_MEASURING_MODE_GDI_CLASSIC || mode == DWRITE_MEASURING_MODE_GDI_NATURAL)
        return DWRITE_GRID_FIT_MODE_ENABLED;

    return (gasp & (GASP_GRIDFIT|GASP_SYMMETRIC_GRIDFIT)) ? DWRITE_GRID_FIT_MODE_ENABLED : DWRITE_GRID_FIT_MODE_DISABLED;
}

struct recommendedmode_test
{
    DWRITE_MEASURING_MODE measuring;
    DWRITE_OUTLINE_THRESHOLD threshold;
};

static const struct recommendedmode_test recmode_tests[] = {
    { DWRITE_MEASURING_MODE_NATURAL,     DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
    { DWRITE_MEASURING_MODE_GDI_CLASSIC, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
    { DWRITE_MEASURING_MODE_GDI_NATURAL, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
};

static const struct recommendedmode_test recmode_tests1[] = {
    { DWRITE_MEASURING_MODE_NATURAL,     DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
    { DWRITE_MEASURING_MODE_GDI_CLASSIC, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
    { DWRITE_MEASURING_MODE_GDI_NATURAL, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED },
    { DWRITE_MEASURING_MODE_NATURAL,     DWRITE_OUTLINE_THRESHOLD_ALIASED },
    { DWRITE_MEASURING_MODE_GDI_CLASSIC, DWRITE_OUTLINE_THRESHOLD_ALIASED },
    { DWRITE_MEASURING_MODE_GDI_NATURAL, DWRITE_OUTLINE_THRESHOLD_ALIASED },
};

static void test_GetRecommendedRenderingMode(void)
{
    IDWriteRenderingParams *params;
5983
    IDWriteFontFace3 *fontface3;
5984 5985 5986 5987 5988 5989 5990
    IDWriteFontFace2 *fontface2;
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace  *fontface;
    DWRITE_RENDERING_MODE mode;
    IDWriteFactory *factory;
    FLOAT emsize;
    HRESULT hr;
5991
    ULONG ref;
5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005

    factory = create_factory();
    fontface = create_fontface(factory);

    fontface1 = NULL;
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    if (hr != S_OK)
        win_skip("IDWriteFontFace1::GetRecommendedRenderingMode() is not supported.\n");

    fontface2 = NULL;
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace2, (void**)&fontface2);
    if (hr != S_OK)
        win_skip("IDWriteFontFace2::GetRecommendedRenderingMode() is not supported.\n");

6006 6007 6008 6009 6010
    fontface3 = NULL;
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace3, (void**)&fontface3);
    if (hr != S_OK)
        win_skip("IDWriteFontFace3::GetRecommendedRenderingMode() is not supported.\n");

6011 6012 6013
    if (0) /* crashes on native */
        hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, 3.0, 1.0,
            DWRITE_MEASURING_MODE_GDI_CLASSIC, NULL, NULL);
6014 6015 6016 6017 6018 6019

    mode = 10;
    hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, 3.0, 1.0,
        DWRITE_MEASURING_MODE_GDI_CLASSIC, NULL, &mode);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(mode == DWRITE_RENDERING_MODE_DEFAULT, "got %d\n", mode);
6020

6021 6022 6023 6024 6025 6026 6027 6028
    hr = IDWriteFactory_CreateRenderingParams(factory, &params);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    /* detect old dwrite version, that is using higher threshold value */
    g_is_vista = fontface1 == NULL;

    for (emsize = 1.0; emsize < 500.0; emsize += 1.0) {
        DWRITE_RENDERING_MODE expected;
6029
        FLOAT ppdip;
6030
        WORD gasp;
6031
        int i;
6032 6033

        for (i = 0; i < sizeof(recmode_tests)/sizeof(recmode_tests[0]); i++) {
6034 6035 6036 6037 6038 6039 6040 6041 6042 6043
            ppdip = 1.0f;
            mode = 10;
            gasp = get_gasp_flags(fontface, emsize, ppdip);
            expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests[i].measuring, recmode_tests[i].threshold);
            hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, emsize, ppdip, recmode_tests[i].measuring, params, &mode);
            ok(hr == S_OK, "got 0x%08x\n", hr);
            ok(mode == expected, "%.2f/%d: got %d, ppdip %f, flags 0x%04x, expected %d\n", emsize, i, mode, ppdip, gasp, expected);

            /* some ppdip variants */
            ppdip = 0.5f;
6044
            mode = 10;
6045 6046 6047
            gasp = get_gasp_flags(fontface, emsize, ppdip);
            expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests[i].measuring, recmode_tests[i].threshold);
            hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, emsize, ppdip, recmode_tests[i].measuring, params, &mode);
6048
            ok(hr == S_OK, "got 0x%08x\n", hr);
6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069
            ok(mode == expected, "%.2f/%d: got %d, ppdip %f, flags 0x%04x, expected %d\n", emsize, i, mode, ppdip, gasp, expected);

            /* Only test larger sizes to workaround Win7 differences, where unscaled natural emsize threshold is used;
               Win8 and Win10 handle this as expected. */
            if (emsize > 20.0f) {
                ppdip = 1.5f;
                mode = 10;
                gasp = get_gasp_flags(fontface, emsize, ppdip);
                expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests[i].measuring, recmode_tests[i].threshold);
                hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, emsize, ppdip, recmode_tests[i].measuring, params, &mode);
                ok(hr == S_OK, "got 0x%08x\n", hr);
                ok(mode == expected, "%.2f/%d: got %d, ppdip %f, flags 0x%04x, expected %d\n", emsize, i, mode, ppdip, gasp, expected);

                ppdip = 2.0f;
                mode = 10;
                gasp = get_gasp_flags(fontface, emsize, ppdip);
                expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests[i].measuring, recmode_tests[i].threshold);
                hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, emsize, ppdip, recmode_tests[i].measuring, params, &mode);
                ok(hr == S_OK, "got 0x%08x\n", hr);
                ok(mode == expected, "%.2f/%d: got %d, ppdip %f, flags 0x%04x, expected %d\n", emsize, i, mode, ppdip, gasp, expected);
            }
6070 6071 6072 6073 6074
        }

        /* IDWriteFontFace1 offers another variant of this method */
        if (fontface1) {
            for (i = 0; i < sizeof(recmode_tests1)/sizeof(recmode_tests1[0]); i++) {
6075 6076 6077 6078
                FLOAT dpi;

                ppdip = 1.0f;
                dpi = 96.0f * ppdip;
6079
                mode = 10;
6080 6081 6082
                gasp = get_gasp_flags(fontface, emsize, ppdip);
                expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi, dpi,
6083 6084
                    NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                ok(hr == S_OK, "got 0x%08x\n", hr);
6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150
                ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                /* Only test larger sizes to workaround Win7 differences, where unscaled natural emsize threshold is used;
                   Win8 and Win10 handle this as expected. */
                if (emsize > 20.0f) {
                    ppdip = 2.0f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi, dpi,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                    ppdip = 0.5f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi, dpi,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                    /* try different dpis for X and Y direction */
                    ppdip = 1.0f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi * 0.5f, dpi,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                    ppdip = 1.0f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi, dpi * 0.5f,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                    ppdip = 2.0f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi * 0.5f, dpi,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);

                    ppdip = 2.0f;
                    dpi = 96.0f * ppdip;
                    mode = 10;
                    gasp = get_gasp_flags(fontface, emsize, ppdip);
                    expected = get_expected_rendering_mode(emsize * ppdip, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                    hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, emsize, dpi, dpi * 0.5f,
                        NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, &mode);
                    ok(hr == S_OK, "got 0x%08x\n", hr);
                    ok(mode == expected, "%.2f/%d: got %d, dpi %f, flags 0x%04x, expected %d\n", emsize, i, mode, dpi, gasp, expected);
                }
6151 6152 6153 6154 6155 6156 6157
            }
        }

        /* IDWriteFontFace2 - another one */
        if (fontface2) {
            DWRITE_GRID_FIT_MODE gridfit, expected_gridfit;

6158
            gasp = get_gasp_flags(fontface, emsize, 1.0f);
6159 6160
            for (i = 0; i < sizeof(recmode_tests1)/sizeof(recmode_tests1[0]); i++) {
                mode = 10;
6161 6162 6163 6164
                expected = get_expected_rendering_mode(emsize, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                expected_gridfit = get_expected_gridfit_mode(emsize, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                hr = IDWriteFontFace2_GetRecommendedRenderingMode(fontface2, emsize, 96.0f, 96.0f,
                    NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, params, &mode, &gridfit);
6165 6166 6167 6168 6169 6170
                ok(hr == S_OK, "got 0x%08x\n", hr);
                ok(mode == expected, "%.2f: got %d, flags 0x%04x, expected %d\n", emsize, mode, gasp, expected);
                ok(gridfit == expected_gridfit, "%.2f/%d: gridfit: got %d, flags 0x%04x, expected %d\n", emsize, i, gridfit,
                    gasp, expected_gridfit);
            }
        }
6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189

        /* IDWriteFontFace3 - and another one */
        if (fontface3) {
            DWRITE_GRID_FIT_MODE gridfit, expected_gridfit;
            DWRITE_RENDERING_MODE1 mode1, expected1;

            gasp = get_gasp_flags(fontface, emsize, 1.0f);
            for (i = 0; i < sizeof(recmode_tests1)/sizeof(recmode_tests1[0]); i++) {
                mode1 = 10;
                expected1 = get_expected_rendering_mode(emsize, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                expected_gridfit = get_expected_gridfit_mode(emsize, gasp, recmode_tests1[i].measuring, recmode_tests1[i].threshold);
                hr = IDWriteFontFace3_GetRecommendedRenderingMode(fontface3, emsize, 96.0f, 96.0f,
                    NULL, FALSE, recmode_tests1[i].threshold, recmode_tests1[i].measuring, params, &mode1, &gridfit);
                ok(hr == S_OK, "got 0x%08x\n", hr);
                ok(mode1 == expected1, "%.2f: got %d, flags 0x%04x, expected %d\n", emsize, mode1, gasp, expected1);
                ok(gridfit == expected_gridfit, "%.2f/%d: gridfit: got %d, flags 0x%04x, expected %d\n", emsize, i, gridfit,
                    gasp, expected_gridfit);
            }
        }
6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202
    }

    IDWriteRenderingParams_Release(params);

    /* test how parameters override returned modes */
    hr = IDWriteFactory_CreateCustomRenderingParams(factory, 1.0, 0.0, 0.0, DWRITE_PIXEL_GEOMETRY_FLAT,
        DWRITE_RENDERING_MODE_GDI_CLASSIC, &params);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    mode = 10;
    hr = IDWriteFontFace_GetRecommendedRenderingMode(fontface, 500.0, 1.0, DWRITE_MEASURING_MODE_NATURAL, params, &mode);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(mode == DWRITE_RENDERING_MODE_GDI_CLASSIC, "got %d\n", mode);
6203

6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217
    IDWriteRenderingParams_Release(params);

    if (fontface2) {
        IDWriteRenderingParams2 *params2;
        IDWriteFactory2 *factory2;
        DWRITE_GRID_FIT_MODE gridfit;

        hr = IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory2, (void**)&factory2);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFactory2_CreateCustomRenderingParams(factory2, 1.0, 0.0, 0.0, 0.5, DWRITE_PIXEL_GEOMETRY_FLAT,
            DWRITE_RENDERING_MODE_OUTLINE, DWRITE_GRID_FIT_MODE_ENABLED, &params2);
        ok(hr == S_OK, "got 0x%08x\n", hr);

6218 6219 6220 6221 6222 6223 6224 6225 6226
        mode = 10;
        gridfit = 10;
        hr = IDWriteFontFace2_GetRecommendedRenderingMode(fontface2, 5.0, 96.0, 96.0,
            NULL, FALSE, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
            NULL, &mode, &gridfit);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(mode == DWRITE_RENDERING_MODE_GDI_CLASSIC, "got %d\n", mode);
        ok(gridfit == DWRITE_GRID_FIT_MODE_ENABLED, "got %d\n", gridfit);

6227 6228 6229 6230 6231 6232 6233 6234
        mode = 10;
        gridfit = 10;
        hr = IDWriteFontFace2_GetRecommendedRenderingMode(fontface2, 5.0, 96.0, 96.0,
            NULL, FALSE, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
            (IDWriteRenderingParams*)params2, &mode, &gridfit);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(mode == DWRITE_RENDERING_MODE_OUTLINE, "got %d\n", mode);
        ok(gridfit == DWRITE_GRID_FIT_MODE_ENABLED, "got %d\n", gridfit);
6235

6236 6237 6238 6239
        IDWriteRenderingParams2_Release(params2);
        IDWriteFactory2_Release(factory2);
    }

6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257
    if (fontface3) {
        IDWriteRenderingParams3 *params3;
        IDWriteRenderingParams2 *params2;
        IDWriteRenderingParams *params;
        IDWriteFactory3 *factory3;
        DWRITE_GRID_FIT_MODE gridfit;
        DWRITE_RENDERING_MODE1 mode1;

        hr = IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory3, (void**)&factory3);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFactory3_CreateCustomRenderingParams(factory3, 1.0f, 0.0f, 0.0f, 0.5f, DWRITE_PIXEL_GEOMETRY_FLAT,
            DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC_DOWNSAMPLED, DWRITE_GRID_FIT_MODE_ENABLED, &params3);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        mode1 = IDWriteRenderingParams3_GetRenderingMode1(params3);
        ok(mode1 == DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC_DOWNSAMPLED, "got %d\n", mode1);

6258 6259
        mode = IDWriteRenderingParams3_GetRenderingMode(params3);
        ok(mode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, "got %d\n", mode);
6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298

        hr = IDWriteRenderingParams3_QueryInterface(params3, &IID_IDWriteRenderingParams, (void**)&params);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(params == (IDWriteRenderingParams*)params3, "got %p, %p\n", params3, params);
        mode = IDWriteRenderingParams_GetRenderingMode(params);
        ok(mode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, "got %d\n", mode);
        IDWriteRenderingParams_Release(params);

        hr = IDWriteRenderingParams3_QueryInterface(params3, &IID_IDWriteRenderingParams2, (void**)&params2);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(params2 == (IDWriteRenderingParams2*)params3, "got %p, %p\n", params3, params2);
        mode = IDWriteRenderingParams2_GetRenderingMode(params2);
        ok(mode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, "got %d\n", mode);
        IDWriteRenderingParams2_Release(params2);

        mode = 10;
        gridfit = 10;
        hr = IDWriteFontFace2_GetRecommendedRenderingMode(fontface2, 5.0f, 96.0f, 96.0f,
            NULL, FALSE, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
            NULL, &mode, &gridfit);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(mode == DWRITE_RENDERING_MODE_GDI_CLASSIC, "got %d\n", mode);
        ok(gridfit == DWRITE_GRID_FIT_MODE_ENABLED, "got %d\n", gridfit);

        mode = 10;
        gridfit = 10;
        hr = IDWriteFontFace2_GetRecommendedRenderingMode(fontface2, 5.0f, 96.0f, 96.0f,
            NULL, FALSE, DWRITE_OUTLINE_THRESHOLD_ANTIALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
            (IDWriteRenderingParams*)params3, &mode, &gridfit);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(mode == DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, "got %d\n", mode);
        ok(gridfit == DWRITE_GRID_FIT_MODE_ENABLED, "got %d\n", gridfit);

        IDWriteRenderingParams3_Release(params3);
        IDWriteFactory3_Release(factory3);
    }

    if (fontface3)
        IDWriteFontFace3_Release(fontface3);
6299 6300 6301 6302 6303
    if (fontface2)
        IDWriteFontFace2_Release(fontface2);
    if (fontface1)
        IDWriteFontFace1_Release(fontface1);
    IDWriteFontFace_Release(fontface);
6304 6305
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6306 6307
}

6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
static inline BOOL float_eq(FLOAT left, FLOAT right)
{
    int x = *(int *)&left;
    int y = *(int *)&right;

    if (x < 0)
        x = INT_MIN - x;
    if (y < 0)
        y = INT_MIN - y;

    return abs(x - y) <= 8;
}

static void test_GetAlphaBlendParams(void)
{
    static const DWRITE_RENDERING_MODE rendermodes[] = {
        DWRITE_RENDERING_MODE_ALIASED,
        DWRITE_RENDERING_MODE_GDI_CLASSIC,
        DWRITE_RENDERING_MODE_GDI_NATURAL,
        DWRITE_RENDERING_MODE_NATURAL,
        DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC,
    };

    IDWriteGlyphRunAnalysis *analysis;
    FLOAT gamma, contrast, ctlevel;
    IDWriteRenderingParams *params;
    DWRITE_GLYPH_METRICS metrics;
    DWRITE_GLYPH_OFFSET offset;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    DWRITE_GLYPH_RUN run;
    FLOAT advance, expected_gdi_gamma;
    UINT value = 0;
    UINT16 glyph;
    UINT32 ch, i;
    HRESULT hr;
6344
    ULONG ref;
6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413
    BOOL ret;

    factory = create_factory();
    fontface = create_fontface(factory);

    ch = 'A';
    glyph = 0;
    hr = IDWriteFontFace_GetGlyphIndices(fontface, &ch, 1, &glyph);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyph > 0, "got %u\n", glyph);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, &glyph, 1, &metrics, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    advance = metrics.advanceWidth;

    offset.advanceOffset = 0.0;
    offset.ascenderOffset = 0.0;

    run.fontFace = fontface;
    run.fontEmSize = 24.0;
    run.glyphCount = 1;
    run.glyphIndices = &glyph;
    run.glyphAdvances = &advance;
    run.glyphOffsets = &offset;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    hr = IDWriteFactory_CreateCustomRenderingParams(factory, 0.9, 0.3, 0.1, DWRITE_PIXEL_GEOMETRY_RGB,
        DWRITE_RENDERING_MODE_DEFAULT, &params);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    value = 0;
    ret = SystemParametersInfoW(SPI_GETFONTSMOOTHINGCONTRAST, 0, &value, 0);
    ok(ret, "got %d\n", ret);
    expected_gdi_gamma = (FLOAT)(value / 1000.0);

    for (i = 0; i < sizeof(rendermodes)/sizeof(rendermodes[0]); i++) {
        hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
            rendermodes[i], DWRITE_MEASURING_MODE_NATURAL,
            0.0, 0.0, &analysis);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        gamma = contrast = ctlevel = -1.0;
        hr = IDWriteGlyphRunAnalysis_GetAlphaBlendParams(analysis, NULL, &gamma, &contrast, &ctlevel);
        ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
        ok(gamma == -1.0, "got %.2f\n", gamma);
        ok(contrast == -1.0, "got %.2f\n", contrast);
        ok(ctlevel == -1.0, "got %.2f\n", ctlevel);

        gamma = contrast = ctlevel = -1.0;
        hr = IDWriteGlyphRunAnalysis_GetAlphaBlendParams(analysis, params, &gamma, &contrast, &ctlevel);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        if (rendermodes[i] == DWRITE_RENDERING_MODE_GDI_CLASSIC || rendermodes[i] == DWRITE_RENDERING_MODE_GDI_NATURAL) {
            ok(float_eq(gamma, expected_gdi_gamma), "got %.2f, expected %.2f\n", gamma, expected_gdi_gamma);
            ok(contrast == 0.0f, "got %.2f\n", contrast);
            ok(ctlevel == 1.0f, "got %.2f\n", ctlevel);
        }
        else {
            ok(gamma == 0.9f, "got %.2f\n", gamma);
            ok(contrast == 0.3f, "got %.2f\n", contrast);
            ok(ctlevel == 0.1f, "got %.2f\n", ctlevel);
        }

        IDWriteGlyphRunAnalysis_Release(analysis);
    }

    IDWriteRenderingParams_Release(params);
    IDWriteFontFace_Release(fontface);
6414 6415
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6416 6417
}

6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431
static void test_CreateAlphaTexture(void)
{
    IDWriteGlyphRunAnalysis *analysis;
    DWRITE_GLYPH_METRICS metrics;
    DWRITE_GLYPH_OFFSET offset;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    DWRITE_GLYPH_RUN run;
    UINT32 ch, size;
    BYTE buff[1024];
    RECT bounds, r;
    FLOAT advance;
    UINT16 glyph;
    HRESULT hr;
6432
    ULONG ref;
6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556

    factory = create_factory();
    fontface = create_fontface(factory);

    ch = 'A';
    glyph = 0;
    hr = IDWriteFontFace_GetGlyphIndices(fontface, &ch, 1, &glyph);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(glyph > 0, "got %u\n", glyph);

    hr = IDWriteFontFace_GetDesignGlyphMetrics(fontface, &glyph, 1, &metrics, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    advance = metrics.advanceWidth;

    offset.advanceOffset = 0.0;
    offset.ascenderOffset = 0.0;

    run.fontFace = fontface;
    run.fontEmSize = 24.0;
    run.glyphCount = 1;
    run.glyphIndices = &glyph;
    run.glyphAdvances = &advance;
    run.glyphOffsets = &offset;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_NATURAL, DWRITE_MEASURING_MODE_NATURAL,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&bounds);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&bounds), "got empty rect\n");
    size = (bounds.right - bounds.left)*(bounds.bottom - bounds.top)*3;
    ok(sizeof(buff) >= size, "required %u\n", size);

    /* invalid type value */
    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1+1, &bounds, buff, sizeof(buff));
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds, buff, 2);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    /* vista version allows texture type mismatch, mark it broken for now */
    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds, buff, sizeof(buff));
    ok(hr == DWRITE_E_UNSUPPORTEDOPERATION || broken(hr == S_OK), "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf || broken(buff[0] == 0), "got %1x\n", buff[0]);

    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds, buff, size-1);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    IDWriteGlyphRunAnalysis_Release(analysis);

    hr = IDWriteFactory_CreateGlyphRunAnalysis(factory, &run, 1.0, NULL,
        DWRITE_RENDERING_MODE_ALIASED, DWRITE_MEASURING_MODE_GDI_CLASSIC,
        0.0, 0.0, &analysis);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    SetRectEmpty(&bounds);
    hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(!IsRectEmpty(&bounds), "got empty rect\n");
    size = (bounds.right - bounds.left)*(bounds.bottom - bounds.top);
    ok(sizeof(buff) >= size, "required %u\n", size);

    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, NULL, buff, sizeof(buff));
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, NULL, NULL, sizeof(buff));
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, NULL, buff, 0);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    /* buffer size is not enough */
    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds, buff, size-1);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    /* request texture for rectangle that doesn't intersect */
    memset(buff, 0xcf, sizeof(buff));
    r = bounds;
    OffsetRect(&r, (bounds.right - bounds.left)*2, 0);
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &r, buff, sizeof(buff));
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(buff[0] == 0, "got %1x\n", buff[0]);

    memset(buff, 0xcf, sizeof(buff));
    r = bounds;
    OffsetRect(&r, (bounds.right - bounds.left)*2, 0);
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &r, buff, sizeof(buff));
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(buff[0] == 0, "got %1x\n", buff[0]);

    /* request texture for rectangle that doesn't intersect, small buffer */
    memset(buff, 0xcf, sizeof(buff));
    r = bounds;
    OffsetRect(&r, (bounds.right - bounds.left)*2, 0);
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_ALIASED_1x1, &r, buff, size-1);
    ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf, "got %1x\n", buff[0]);

    /* vista version allows texture type mismatch, mark it broken for now */
    memset(buff, 0xcf, sizeof(buff));
    hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds, buff, sizeof(buff));
    ok(hr == DWRITE_E_UNSUPPORTEDOPERATION || broken(hr == S_OK), "got 0x%08x\n", hr);
    ok(buff[0] == 0xcf || broken(buff[0] == 0), "got %1x\n", buff[0]);

    IDWriteGlyphRunAnalysis_Release(analysis);
    IDWriteFontFace_Release(fontface);
6557 6558
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6559 6560
}

6561 6562 6563 6564 6565 6566 6567 6568
static void test_IsSymbolFont(void)
{
    static const WCHAR symbolW[] = {'S','y','m','b','o','l',0};
    IDWriteFontCollection *collection;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFont *font;
    HRESULT hr;
6569
    ULONG ref;
6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600
    BOOL ret;

    factory = create_factory();

    /* Tahoma */
    fontface = create_fontface(factory);
    ret = IDWriteFontFace_IsSymbolFont(fontface);
    ok(!ret, "got %d\n", ret);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontCollection_GetFontFromFontFace(collection, fontface, &font);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    ret = IDWriteFont_IsSymbolFont(font);
    ok(!ret, "got %d\n", ret);

    IDWriteFontCollection_Release(collection);
    IDWriteFont_Release(font);
    IDWriteFontFace_Release(fontface);

    /* Symbol */
    font = get_font(factory, symbolW, DWRITE_FONT_STYLE_NORMAL);
    ret = IDWriteFont_IsSymbolFont(font);
    ok(ret, "got %d\n", ret);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ret = IDWriteFontFace_IsSymbolFont(fontface);
    ok(ret, "got %d\n", ret);
6601
    IDWriteFontFace_Release(fontface);
6602 6603
    IDWriteFont_Release(font);

6604 6605
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6606 6607
}

6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629
struct CPAL_Header_0
{
    USHORT version;
    USHORT numPaletteEntries;
    USHORT numPalette;
    USHORT numColorRecords;
    ULONG  offsetFirstColorRecord;
    USHORT colorRecordIndices[1];
};

static void test_GetPaletteEntries(void)
{
    IDWriteFontFace2 *fontface2;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    IDWriteFont *font;
    DWRITE_COLOR_F color;
    UINT32 palettecount, entrycount, size, colorrecords;
    void *ctxt;
    const struct CPAL_Header_0 *cpal_header;
    HRESULT hr;
    BOOL exists;
6630
    ULONG ref;
6631 6632 6633 6634 6635 6636 6637 6638

    factory = create_factory();

    /* Tahoma, no color support */
    fontface = create_fontface(factory);
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace2, (void**)&fontface2);
    IDWriteFontFace_Release(fontface);
    if (hr != S_OK) {
6639 6640
        ref = IDWriteFactory_Release(factory);
        ok(ref == 0, "factory not released, %u\n", ref);
6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651
        win_skip("GetPaletteEntries() is not supported.\n");
        return;
    }

    hr = IDWriteFontFace2_GetPaletteEntries(fontface2, 0, 0, 1, &color);
    ok(hr == DWRITE_E_NOCOLOR, "got 0x%08x\n", hr);
    IDWriteFontFace2_Release(fontface2);

    /* Segoe UI Emoji, with color support */
    font = get_font(factory, emojiW, DWRITE_FONT_STYLE_NORMAL);
    if (!font) {
6652 6653
        ref = IDWriteFactory_Release(factory);
        ok(ref == 0, "factory not released, %u\n", ref);
6654 6655 6656 6657 6658 6659 6660 6661 6662
        skip("Segoe UI Emoji font not found.\n");
        return;
    }

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace2, (void**)&fontface2);
6663
    ok(hr == S_OK, "got 0x%08x\n", hr);
6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706
    IDWriteFontFace_Release(fontface);

    palettecount = IDWriteFontFace2_GetColorPaletteCount(fontface2);
    ok(palettecount >= 1, "got %u\n", palettecount);

    entrycount = IDWriteFontFace2_GetPaletteEntryCount(fontface2);
    ok(entrycount >= 1, "got %u\n", entrycount);

    exists = FALSE;
    hr = IDWriteFontFace2_TryGetFontTable(fontface2, MS_CPAL_TAG, (const void**)&cpal_header, &size, &ctxt, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(exists, "got %d\n", exists);
    colorrecords = GET_BE_WORD(cpal_header->numColorRecords);
    ok(colorrecords >= 1, "got %u\n", colorrecords);

    /* invalid palette index */
    color.r = color.g = color.b = color.a = 123.0;
    hr = IDWriteFontFace2_GetPaletteEntries(fontface2, palettecount, 0, 1, &color);
    ok(hr == DWRITE_E_NOCOLOR, "got 0x%08x\n", hr);
    ok(color.r == 123.0 && color.g == 123.0 && color.b == 123.0 && color.a == 123.0,
        "got wrong color %.2fx%.2fx%.2fx%.2f\n", color.r, color.g, color.b, color.a);

    /* invalid entry index */
    color.r = color.g = color.b = color.a = 123.0;
    hr = IDWriteFontFace2_GetPaletteEntries(fontface2, 0, entrycount, 1, &color);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
    ok(color.r == 123.0 && color.g == 123.0 && color.b == 123.0 && color.a == 123.0,
        "got wrong color %.2fx%.2fx%.2fx%.2f\n", color.r, color.g, color.b, color.a);

    color.r = color.g = color.b = color.a = 123.0;
    hr = IDWriteFontFace2_GetPaletteEntries(fontface2, 0, entrycount - 1, 1, &color);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(color.r != 123.0 && color.g != 123.0 && color.b != 123.0 && color.a != 123.0,
        "got wrong color %.2fx%.2fx%.2fx%.2f\n", color.r, color.g, color.b, color.a);

    /* zero return length */
    color.r = color.g = color.b = color.a = 123.0;
    hr = IDWriteFontFace2_GetPaletteEntries(fontface2, 0, 0, 0, &color);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(color.r == 123.0 && color.g == 123.0 && color.b == 123.0 && color.a == 123.0,
        "got wrong color %.2fx%.2fx%.2fx%.2f\n", color.r, color.g, color.b, color.a);

    IDWriteFontFace2_Release(fontface2);
6707 6708
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6709 6710
}

6711 6712 6713 6714
static void test_TranslateColorGlyphRun(void)
{
    IDWriteColorGlyphRunEnumerator *layers;
    const DWRITE_COLOR_GLYPH_RUN *colorrun;
6715
    IDWriteFontFace2 *fontface2;
6716
    IDWriteFontFace *fontface;
6717
    IDWriteFactory2 *factory;
6718 6719 6720 6721 6722 6723
    DWRITE_GLYPH_RUN run;
    UINT32 codepoints[2];
    IDWriteFont *font;
    UINT16 glyphs[2];
    BOOL hasrun;
    HRESULT hr;
6724
    ULONG ref;
6725

6726 6727
    factory = create_factory_iid(&IID_IDWriteFactory2);
    if (!factory) {
6728 6729 6730 6731 6732
        win_skip("TranslateColorGlyphRun() is not supported.\n");
        return;
    }

    /* Tahoma, no color support */
6733
    fontface = create_fontface((IDWriteFactory *)factory);
6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748

    codepoints[0] = 'A';
    hr = IDWriteFontFace_GetGlyphIndices(fontface, codepoints, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    run.fontFace = fontface;
    run.fontEmSize = 20.0f;
    run.glyphCount = 1;
    run.glyphIndices = glyphs;
    run.glyphAdvances = NULL;
    run.glyphOffsets = NULL;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    layers = (void*)0xdeadbeef;
6749
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6750 6751 6752 6753 6754 6755
        DWRITE_MEASURING_MODE_NATURAL, NULL, 0, &layers);
    ok(hr == DWRITE_E_NOCOLOR, "got 0x%08x\n", hr);
    ok(layers == NULL, "got %p\n", layers);
    IDWriteFontFace_Release(fontface);

    /* Segoe UI Emoji, with color support */
6756
    font = get_font((IDWriteFactory *)factory, emojiW, DWRITE_FONT_STYLE_NORMAL);
6757
    if (!font) {
6758
        IDWriteFactory2_Release(factory);
6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773
        skip("Segoe UI Emoji font not found.\n");
        return;
    }

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    codepoints[0] = 0x26c4;
    hr = IDWriteFontFace_GetGlyphIndices(fontface, codepoints, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    run.fontFace = fontface;

    layers = NULL;
6774
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6775 6776 6777 6778
        DWRITE_MEASURING_MODE_NATURAL, NULL, 0, &layers);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(layers != NULL, "got %p\n", layers);

6779
    for (;;) {
6780 6781 6782 6783 6784 6785
        hasrun = FALSE;
        hr = IDWriteColorGlyphRunEnumerator_MoveNext(layers, &hasrun);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        if (!hasrun)
            break;
6786 6787 6788 6789 6790 6791 6792 6793

        hr = IDWriteColorGlyphRunEnumerator_GetCurrentRun(layers, &colorrun);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        ok(colorrun->glyphRun.fontFace != NULL, "got fontface %p\n", colorrun->glyphRun.fontFace);
        ok(colorrun->glyphRun.fontEmSize == 20.0f, "got wrong font size %f\n", colorrun->glyphRun.fontEmSize);
        ok(colorrun->glyphRun.glyphCount > 0, "got wrong glyph count %u\n", colorrun->glyphRun.glyphCount);
        ok(colorrun->glyphRun.glyphIndices != NULL, "got null glyph indices %p\n", colorrun->glyphRun.glyphIndices);
        ok(colorrun->glyphRun.glyphAdvances != NULL, "got null glyph advances %p\n", colorrun->glyphRun.glyphAdvances);
6794 6795 6796 6797 6798 6799 6800
    }

    /* iterated all way through */
    hr = IDWriteColorGlyphRunEnumerator_GetCurrentRun(layers, &colorrun);
    ok(hr == E_NOT_VALID_STATE, "got 0x%08x\n", hr);

    IDWriteColorGlyphRunEnumerator_Release(layers);
6801

6802 6803 6804 6805 6806
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace2, (void**)&fontface2);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    /* invalid palette index */
    layers = (void*)0xdeadbeef;
6807
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6808 6809 6810 6811 6812 6813
        DWRITE_MEASURING_MODE_NATURAL, NULL, IDWriteFontFace2_GetColorPaletteCount(fontface2),
        &layers);
    ok(hr == DWRITE_E_NOCOLOR, "got 0x%08x\n", hr);
    ok(layers == NULL, "got %p\n", layers);

    layers = NULL;
6814
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6815 6816 6817 6818
        DWRITE_MEASURING_MODE_NATURAL, NULL, IDWriteFontFace2_GetColorPaletteCount(fontface2) - 1,
        &layers);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteColorGlyphRunEnumerator_Release(layers);
6819 6820 6821 6822 6823 6824 6825

    /* color font, glyph without color info */
    codepoints[0] = 'A';
    hr = IDWriteFontFace_GetGlyphIndices(fontface, codepoints, 1, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    layers = (void*)0xdeadbeef;
6826
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6827 6828 6829
        DWRITE_MEASURING_MODE_NATURAL, NULL, 0, &layers);
    ok(hr == DWRITE_E_NOCOLOR, "got 0x%08x\n", hr);
    ok(layers == NULL, "got %p\n", layers);
6830

6831 6832 6833 6834 6835 6836 6837 6838 6839 6840
    /* one glyph with, one without */
    codepoints[0] = 'A';
    codepoints[1] = 0x26c4;

    hr = IDWriteFontFace_GetGlyphIndices(fontface, codepoints, 2, glyphs);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    run.glyphCount = 2;

    layers = NULL;
6841
    hr = IDWriteFactory2_TranslateColorGlyphRun(factory, 0.0f, 0.0f, &run, NULL,
6842 6843 6844 6845 6846
        DWRITE_MEASURING_MODE_NATURAL, NULL, 0, &layers);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(layers != NULL, "got %p\n", layers);
    IDWriteColorGlyphRunEnumerator_Release(layers);

6847
    IDWriteFontFace2_Release(fontface2);
6848
    IDWriteFontFace_Release(fontface);
6849
    ref = IDWriteFactory2_Release(factory);
6850
    ok(ref == 0, "factory not released, %u\n", ref);
6851 6852
}

6853 6854 6855 6856 6857 6858 6859
static void test_HasCharacter(void)
{
    IDWriteFactory3 *factory3;
    IDWriteFactory *factory;
    IDWriteFont3 *font3;
    IDWriteFont *font;
    HRESULT hr;
6860
    ULONG ref;
6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880
    BOOL ret;

    factory = create_factory();

    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_NORMAL);
    ok(font != NULL, "failed to create font\n");

    /* Win8 is broken, QI claims to support IDWriteFont3, but in fact it does not */
    hr = IDWriteFactory_QueryInterface(factory, &IID_IDWriteFactory3, (void**)&factory3);
    if (hr == S_OK) {
        hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont3, (void**)&font3);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        ret = IDWriteFont3_HasCharacter(font3, 'A');
        ok(ret, "got %d\n", ret);

        IDWriteFont3_Release(font3);
        IDWriteFactory3_Release(factory3);
    }
    else
6881
        win_skip("IDWriteFont3 is not supported.\n");
6882 6883

    IDWriteFont_Release(font);
6884 6885
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
6886 6887
}

6888 6889 6890
static void test_CreateFontFaceReference(void)
{
    static const WCHAR dummyW[] = {'d','u','m','m','y',0};
6891 6892 6893
    IDWriteFontFace3 *fontface, *fontface1;
    IDWriteFontFaceReference *ref, *ref1;
    IDWriteFontFile *file, *file1;
6894
    IDWriteFactory3 *factory;
6895 6896
    IDWriteFont3 *font3;
    IDWriteFont *font;
6897
    ULONG refcount;
6898 6899 6900
    UINT32 index;
    WCHAR *path;
    HRESULT hr;
6901
    BOOL ret;
6902

6903 6904
    factory = create_factory_iid(&IID_IDWriteFactory3);
    if (!factory) {
6905 6906 6907 6908 6909 6910
        win_skip("CreateFontFaceReference() is not supported.\n");
        return;
    }

    path = create_testfontfile(test_fontfile);

6911
    hr = IDWriteFactory3_CreateFontFaceReference(factory, NULL, NULL, 0, DWRITE_FONT_SIMULATIONS_NONE, &ref);
6912 6913
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

6914
    /* out of range simulation flags */
6915
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 0, ~0u, &ref);
6916 6917 6918
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    /* test file is not a collection, but reference could still be created with non-zero face index */
6919
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 1, DWRITE_FONT_SIMULATIONS_NONE, &ref);
6920
    ok(hr == S_OK, "got 0x%08x\n", hr);
6921

6922 6923
    index = IDWriteFontFaceReference_GetFontFaceIndex(ref);
    ok(index == 1, "got %u\n", index);
6924 6925 6926 6927 6928 6929

    hr = IDWriteFontFaceReference_GetFontFile(ref, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFaceReference_CreateFontFace(ref, &fontface);
6930
todo_wine
6931 6932 6933
    ok(hr == DWRITE_E_FILEFORMAT, "got 0x%08x\n", hr);

    IDWriteFontFaceReference_Release(ref);
6934

6935
    /* path however has to be valid */
6936
    hr = IDWriteFactory3_CreateFontFaceReference(factory, dummyW, NULL, 0, DWRITE_FONT_SIMULATIONS_NONE, &ref);
6937 6938
todo_wine
    ok(hr == DWRITE_E_FILENOTFOUND, "got 0x%08x\n", hr);
6939 6940
    if (hr == S_OK)
        IDWriteFontFaceReference_Release(ref);
6941

6942 6943
    EXPECT_REF(factory, 1);
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 0, DWRITE_FONT_SIMULATIONS_NONE, &ref);
6944
    ok(hr == S_OK, "got 0x%08x\n", hr);
6945
    EXPECT_REF(factory, 2);
6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958

    /* new file is returned */
    hr = IDWriteFontFaceReference_GetFontFile(ref, &file);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFaceReference_GetFontFile(ref, &file1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(file != file1, "got %p, previous file %p\n", file1, file);

    IDWriteFontFile_Release(file);
    IDWriteFontFile_Release(file1);

    /* references are not reused */
6959
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 0, DWRITE_FONT_SIMULATIONS_NONE, &ref1);
6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(ref1 != ref, "got %p, previous ref %p\n", ref1, ref);

    /* created fontfaces are cached */
    hr = IDWriteFontFaceReference_CreateFontFace(ref, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFaceReference_CreateFontFace(ref1, &fontface1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface == fontface1, "got %p, expected %p\n", fontface1, fontface);
    IDWriteFontFace3_Release(fontface);
    IDWriteFontFace3_Release(fontface1);

6973 6974 6975 6976 6977
    /* reference equality */
    ret = IDWriteFontFaceReference_Equals(ref, ref1);
    ok(ret, "got %d\n", ret);
    IDWriteFontFaceReference_Release(ref1);

6978
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 1, DWRITE_FONT_SIMULATIONS_NONE, &ref1);
6979 6980 6981
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ret = IDWriteFontFaceReference_Equals(ref, ref1);
    ok(!ret, "got %d\n", ret);
6982
    IDWriteFontFaceReference_Release(ref1);
6983

6984
    hr = IDWriteFactory3_CreateFontFaceReference(factory, path, NULL, 0, DWRITE_FONT_SIMULATIONS_BOLD, &ref1);
6985 6986 6987 6988 6989
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ret = IDWriteFontFaceReference_Equals(ref, ref1);
    ok(!ret, "got %d\n", ret);
    IDWriteFontFaceReference_Release(ref1);

6990 6991
    IDWriteFontFaceReference_Release(ref);

6992
    /* create reference from a file */
6993
    hr = IDWriteFactory3_CreateFontFileReference(factory, path, NULL, &file);
6994 6995
    ok(hr == S_OK, "got 0x%08x\n", hr);

6996
    hr = IDWriteFactory3_CreateFontFaceReference_(factory, file, 0, DWRITE_FONT_SIMULATIONS_NONE, &ref);
6997 6998 6999 7000 7001 7002 7003 7004 7005
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFaceReference_GetFontFile(ref, &file1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(file != file1, "got %p, previous file %p\n", file1, file);

    IDWriteFontFaceReference_Release(ref);
    IDWriteFontFile_Release(file);
    IDWriteFontFile_Release(file1);
7006

7007
    /* references returned from IDWriteFont3 */
7008
    font = get_tahoma_instance((IDWriteFactory *)factory, DWRITE_FONT_STYLE_NORMAL);
7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021
    hr = IDWriteFont_QueryInterface(font, &IID_IDWriteFont3, (void**)&font3);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    IDWriteFont_Release(font);

    hr = IDWriteFont3_GetFontFaceReference(font3, &ref);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFont3_GetFontFaceReference(font3, &ref1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(ref != ref1, "got %p, %p\n", ref1, ref);

    IDWriteFontFaceReference_Release(ref);
    IDWriteFontFaceReference_Release(ref1);
7022 7023 7024 7025 7026 7027

    /* references returned from IDWriteFontFace3 */
    hr = IDWriteFont3_CreateFontFace(font3, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace3_GetFontFaceReference(fontface, &ref);
7028
todo_wine
7029 7030 7031
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace3_GetFontFaceReference(fontface, &ref1);
7032
todo_wine
7033
    ok(hr == S_OK, "got 0x%08x\n", hr);
7034
if (hr == S_OK)
7035 7036
    ok(ref == ref1, "got %p, %p\n", ref1, ref);

7037
if (hr == S_OK) {
7038 7039 7040 7041 7042 7043 7044 7045
    hr = IDWriteFontFaceReference_CreateFontFace(ref, &fontface1);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(fontface1 == fontface, "got %p, %p\n", fontface1, fontface);
    IDWriteFontFace3_Release(fontface1);

    IDWriteFontFaceReference_Release(ref);
    IDWriteFontFaceReference_Release(ref1);
}
7046
    IDWriteFontFace3_Release(fontface);
7047 7048
    IDWriteFont3_Release(font3);

7049
    refcount = IDWriteFactory3_Release(factory);
7050
    ok(refcount == 0, "factory not released, %u\n", refcount);
7051 7052 7053
    DELETE_FONTFILE(path);
}

7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076
static void get_expected_fontsig(IDWriteFont *font, FONTSIGNATURE *fontsig)
{
    void *os2_context;
    IDWriteFontFace *fontface;
    const TT_OS2_V2 *tt_os2;
    UINT32 size;
    BOOL exists;
    HRESULT hr;

    memset(fontsig, 0, sizeof(*fontsig));

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_TryGetFontTable(fontface, MS_0S2_TAG, (const void**)&tt_os2, &size, &os2_context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    if (tt_os2) {
        fontsig->fsUsb[0] = GET_BE_DWORD(tt_os2->ulUnicodeRange1);
        fontsig->fsUsb[1] = GET_BE_DWORD(tt_os2->ulUnicodeRange2);
        fontsig->fsUsb[2] = GET_BE_DWORD(tt_os2->ulUnicodeRange3);
        fontsig->fsUsb[3] = GET_BE_DWORD(tt_os2->ulUnicodeRange4);

7077 7078 7079 7080 7081 7082 7083 7084
        if (GET_BE_WORD(tt_os2->version) == 0) {
            fontsig->fsCsb[0] = 0;
            fontsig->fsCsb[1] = 0;
        }
        else {
            fontsig->fsCsb[0] = GET_BE_DWORD(tt_os2->ulCodePageRange1);
            fontsig->fsCsb[1] = GET_BE_DWORD(tt_os2->ulCodePageRange2);
        }
7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100

        IDWriteFontFace_ReleaseFontTable(fontface, os2_context);
    }

    IDWriteFontFace_Release(fontface);
}

static void test_GetFontSignature(void)
{
    IDWriteFontCollection *syscollection;
    IDWriteGdiInterop1 *interop1;
    IDWriteGdiInterop *interop;
    IDWriteFactory *factory;
    FONTSIGNATURE fontsig;
    UINT count, i;
    HRESULT hr;
7101
    ULONG ref;
7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163

    factory = create_factory();

    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteGdiInterop_QueryInterface(interop, &IID_IDWriteGdiInterop1, (void**)&interop1);
    IDWriteGdiInterop_Release(interop);
    if (FAILED(hr)) {
        win_skip("GetFontSignature() is not supported.\n");
        IDWriteGdiInterop_Release(interop);
        IDWriteFactory_Release(factory);
        return;
    };
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteGdiInterop1_GetFontSignature(interop1, NULL, &fontsig);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
        FONTSIGNATURE expected_signature;
        IDWriteLocalizedStrings *names;
        IDWriteFontFamily *family;
        IDWriteFont *font;
        WCHAR nameW[256];

        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
            DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_enus_string(names, nameW, sizeof(nameW)/sizeof(nameW[0]));

        IDWriteLocalizedStrings_Release(names);

        hr = IDWriteGdiInterop1_GetFontSignature(interop1, font, &fontsig);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_expected_fontsig(font, &expected_signature);

        ok(fontsig.fsUsb[0] == expected_signature.fsUsb[0], "%s: fsUsb[0] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsUsb[0], expected_signature.fsUsb[0]);
        ok(fontsig.fsUsb[1] == expected_signature.fsUsb[1], "%s: fsUsb[1] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsUsb[1], expected_signature.fsUsb[1]);
        ok(fontsig.fsUsb[2] == expected_signature.fsUsb[2], "%s: fsUsb[2] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsUsb[2], expected_signature.fsUsb[2]);
        ok(fontsig.fsUsb[3] == expected_signature.fsUsb[3], "%s: fsUsb[3] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsUsb[3], expected_signature.fsUsb[3]);

        ok(fontsig.fsCsb[0] == expected_signature.fsCsb[0], "%s: fsCsb[0] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsCsb[0], expected_signature.fsCsb[0]);
        ok(fontsig.fsCsb[1] == expected_signature.fsCsb[1], "%s: fsCsb[1] %#x, expected %#x\n", wine_dbgstr_w(nameW),
            fontsig.fsCsb[1], expected_signature.fsCsb[1]);
7164

7165 7166 7167 7168 7169 7170
        IDWriteFont_Release(font);
        IDWriteFontFamily_Release(family);
    }

    IDWriteGdiInterop1_Release(interop1);
    IDWriteFontCollection_Release(syscollection);
7171 7172
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
7173 7174
}

7175 7176 7177 7178 7179 7180 7181 7182
static void test_font_properties(void)
{
    IDWriteFontFace3 *fontface3;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    DWRITE_FONT_STYLE style;
    IDWriteFont *font;
    HRESULT hr;
7183
    ULONG ref;
7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205

    factory = create_factory();

    /* this creates simulated font */
    font = get_tahoma_instance(factory, DWRITE_FONT_STYLE_ITALIC);

    style = IDWriteFont_GetStyle(font);
    ok(style == DWRITE_FONT_STYLE_OBLIQUE, "got %u\n", style);

    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace3, (void**)&fontface3);
    IDWriteFontFace_Release(fontface);
    if (hr == S_OK) {
        style = IDWriteFontFace3_GetStyle(fontface3);
        ok(style == DWRITE_FONT_STYLE_OBLIQUE, "got %u\n", style);

        IDWriteFontFace3_Release(fontface3);
    }

    IDWriteFont_Release(font);
7206 7207
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
7208 7209
}

7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234
static BOOL has_vertical_glyph_variants(IDWriteFontFace1 *fontface)
{
    const OT_FeatureList *featurelist;
    const OT_LookupList *lookup_list;
    BOOL exists = FALSE, ret = FALSE;
    const GSUB_Header *header;
    const void *data;
    void *context;
    UINT32 size;
    HRESULT hr;
    UINT16 i;

    hr = IDWriteFontFace1_TryGetFontTable(fontface, MS_GSUB_TAG, &data, &size, &context, &exists);
    ok(hr == S_OK, "got 0x%08x\n", hr);

    if (!exists)
        return FALSE;

    header = data;
    featurelist = (OT_FeatureList*)((BYTE*)header + GET_BE_WORD(header->FeatureList));
    lookup_list = (const OT_LookupList*)((BYTE*)header + GET_BE_WORD(header->LookupList));

    for (i = 0; i < GET_BE_WORD(featurelist->FeatureCount); i++) {
        if (*(UINT32*)featurelist->FeatureRecord[i].FeatureTag == DWRITE_FONT_FEATURE_TAG_VERTICAL_WRITING) {
            const OT_Feature *feature = (const OT_Feature*)((BYTE*)featurelist + GET_BE_WORD(featurelist->FeatureRecord[i].Feature));
7235
            UINT16 lookup_count = GET_BE_WORD(feature->LookupCount), i, index, count, type;
7236 7237 7238 7239 7240 7241 7242
            const GSUB_SingleSubstFormat2 *subst2;
            const OT_LookupTable *lookup_table;
            UINT32 offset;

            if (lookup_count == 0)
                continue;

7243 7244 7245 7246
            for (i = 0; i < lookup_count; i++) {
                /* check if lookup is empty */
                index = GET_BE_WORD(feature->LookupListIndex[i]);
                lookup_table = (const OT_LookupTable*)((BYTE*)lookup_list + GET_BE_WORD(lookup_list->Lookup[index]));
7247

7248 7249
                type = GET_BE_WORD(lookup_table->LookupType);
                ok(type == 1 || type == 7, "got unexpected lookup type %u\n", type);
7250

7251 7252 7253
                count = GET_BE_WORD(lookup_table->SubTableCount);
                if (count == 0)
                    continue;
7254

7255
                ok(count > 0, "got unexpected subtable count %u\n", count);
7256

7257 7258 7259 7260 7261 7262 7263 7264
                offset = GET_BE_WORD(lookup_table->SubTable[0]);
                if (type == 7) {
                    const GSUB_ExtensionPosFormat1 *ext = (const GSUB_ExtensionPosFormat1 *)((const BYTE *)lookup_table + offset);
                    if (GET_BE_WORD(ext->SubstFormat) == 1)
                        offset += GET_BE_DWORD(ext->ExtensionOffset);
                    else
                        ok(0, "Unhandled Extension Substitution Format %u\n", GET_BE_WORD(ext->SubstFormat));
                }
7265

7266 7267 7268 7269 7270 7271 7272 7273 7274 7275
                subst2 = (const GSUB_SingleSubstFormat2*)((BYTE*)lookup_table + offset);
                index = GET_BE_WORD(subst2->SubstFormat);
                if (index == 1)
                    ok(0, "validate Single Substitution Format 1\n");
                else if (index == 2) {
                    /* SimSun-ExtB has 0 glyph count for this substitution */
                    if (GET_BE_WORD(subst2->GlyphCount) > 0) {
                        ret = TRUE;
                        break;
                    }
7276
                }
7277 7278
                else
                    ok(0, "unknown Single Substitution Format, %u\n", index);
7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295
            }
        }
    }

    IDWriteFontFace1_ReleaseFontTable(fontface, context);

    return ret;
}

static void test_HasVerticalGlyphVariants(void)
{
    IDWriteFontCollection *syscollection;
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    UINT32 count, i;
    HRESULT hr;
7296
    ULONG ref;
7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353

    factory = create_factory();
    fontface = create_fontface(factory);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    IDWriteFontFace_Release(fontface);
    if (hr != S_OK) {
        win_skip("HasVerticalGlyphVariants() is not supported.\n");
        IDWriteFactory_Release(factory);
        return;
    }
    IDWriteFontFace1_Release(fontface1);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
        IDWriteLocalizedStrings *names;
        BOOL expected_vert, has_vert;
        IDWriteFontFamily *family;
        IDWriteFont *font;
        WCHAR nameW[256];

        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
            DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFont_CreateFontFace(font, &fontface);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_enus_string(names, nameW, sizeof(nameW)/sizeof(nameW[0]));

        expected_vert = has_vertical_glyph_variants(fontface1);
        has_vert = IDWriteFontFace1_HasVerticalGlyphVariants(fontface1);

        ok(expected_vert == has_vert, "%s: expected vertical feature %d, got %d\n",
            wine_dbgstr_w(nameW), expected_vert, has_vert);

        IDWriteLocalizedStrings_Release(names);
        IDWriteFont_Release(font);

        IDWriteFontFace1_Release(fontface1);
        IDWriteFontFace_Release(fontface);
        IDWriteFontFamily_Release(family);
    }

    IDWriteFontCollection_Release(syscollection);
7354 7355
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
7356 7357
}

7358 7359 7360 7361 7362 7363 7364 7365
static void test_HasKerningPairs(void)
{
    IDWriteFontCollection *syscollection;
    IDWriteFontFace1 *fontface1;
    IDWriteFontFace *fontface;
    IDWriteFactory *factory;
    UINT32 count, i;
    HRESULT hr;
7366
    ULONG ref;
7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429

    factory = create_factory();
    fontface = create_fontface(factory);

    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void**)&fontface1);
    IDWriteFontFace_Release(fontface);
    if (hr != S_OK) {
        win_skip("HasKerningPairs() is not supported.\n");
        IDWriteFactory_Release(factory);
        return;
    }
    IDWriteFontFace1_Release(fontface1);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
        IDWriteLocalizedStrings *names;
        BOOL exists, has_kerningpairs;
        IDWriteFontFamily *family;
        IDWriteFont *font;
        WCHAR nameW[256];
        const void *data;
        void *context;
        UINT32 size;

        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
            DWRITE_FONT_STYLE_NORMAL, &font);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFont_CreateFontFace(font, &fontface);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace1, (void **)&fontface1);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_enus_string(names, nameW, sizeof(nameW)/sizeof(nameW[0]));

        exists = FALSE;
        hr = IDWriteFontFace1_TryGetFontTable(fontface1, MS_KERN_TAG, &data, &size, &context, &exists);
        ok(hr == S_OK, "got 0x%08x\n", hr);
        IDWriteFontFace1_ReleaseFontTable(fontface1, context);

        has_kerningpairs = IDWriteFontFace1_HasKerningPairs(fontface1);
        if (!exists)
            ok(!has_kerningpairs, "%s: expected %d, got %d\n", wine_dbgstr_w(nameW), exists, has_kerningpairs);

        IDWriteLocalizedStrings_Release(names);
        IDWriteFont_Release(font);

        IDWriteFontFace1_Release(fontface1);
        IDWriteFontFace_Release(fontface);
        IDWriteFontFamily_Release(family);
    }

    IDWriteFontCollection_Release(syscollection);
7430 7431
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
7432 7433
}

7434 7435
static void test_ComputeGlyphOrigins(void)
{
7436
    IDWriteFactory4 *factory;
7437 7438 7439 7440 7441 7442 7443
    DWRITE_GLYPH_RUN run;
    HRESULT hr;
    D2D1_POINT_2F origins[2];
    D2D1_POINT_2F baseline_origin;
    UINT16 glyphs[2];
    FLOAT advances[2];
    DWRITE_MATRIX m;
7444
    ULONG ref;
7445

7446 7447
    factory = create_factory_iid(&IID_IDWriteFactory4);
    if (!factory) {
7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467
        win_skip("ComputeGlyphOrigins() is not supported.\n");
        return;
    }

    advances[0] = 10.0f;
    advances[1] = 20.0f;

    run.fontFace = NULL;
    run.fontEmSize = 16.0f;
    run.glyphCount = 2;
    run.glyphIndices = glyphs;
    run.glyphAdvances = advances;
    run.glyphOffsets = NULL;
    run.isSideways = FALSE;
    run.bidiLevel = 0;

    baseline_origin.x = 123.0f;
    baseline_origin.y = 321.0f;

    memset(origins, 0, sizeof(origins));
7468
    hr = IDWriteFactory4_ComputeGlyphOrigins_(factory, &run, baseline_origin, origins);
7469 7470 7471 7472 7473
    ok(hr == S_OK, "got 0x%08x\n", hr);
    ok(origins[0].x == 123.0f && origins[0].y == 321.0f, "origins[0] %f,%f\n", origins[0].x, origins[0].y);
    ok(origins[1].x == 133.0f && origins[1].y == 321.0f, "origins[1] %f,%f\n", origins[1].x, origins[1].y);

    memset(origins, 0, sizeof(origins));
7474
    hr = IDWriteFactory4_ComputeGlyphOrigins(factory, &run, DWRITE_MEASURING_MODE_NATURAL, baseline_origin,
7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487
        NULL, origins);
    ok(origins[0].x == 123.0f && origins[0].y == 321.0f, "origins[0] %f,%f\n", origins[0].x, origins[0].y);
    ok(origins[1].x == 133.0f && origins[1].y == 321.0f, "origins[1] %f,%f\n", origins[1].x, origins[1].y);

    /* transform is not applied to returned origins */
    m.m11 = 2.0f;
    m.m12 = 0.0f;
    m.m21 = 0.0f;
    m.m22 = 1.0f;
    m.dx = 0.0f;
    m.dy = 0.0f;

    memset(origins, 0, sizeof(origins));
7488
    hr = IDWriteFactory4_ComputeGlyphOrigins(factory, &run, DWRITE_MEASURING_MODE_NATURAL, baseline_origin,
7489 7490 7491 7492
        &m, origins);
    ok(origins[0].x == 123.0f && origins[0].y == 321.0f, "origins[0] %f,%f\n", origins[0].x, origins[0].y);
    ok(origins[1].x == 133.0f && origins[1].y == 321.0f, "origins[1] %f,%f\n", origins[1].x, origins[1].y);

7493
    ref = IDWriteFactory4_Release(factory);
7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651
    ok(ref == 0, "factory not released, %u\n", ref);
}

static void test_object_lifetime(void)
{
    IDWriteFontCollection *collection, *collection2;
    IDWriteFontList *fontlist, *fontlist2;
    IDWriteGdiInterop *interop, *interop2;
    IDWriteFontFamily *family, *family2;
    IDWriteFontFace *fontface;
    IDWriteFont *font, *font2;
    IDWriteFactory *factory;
    HRESULT hr;
    ULONG ref;

    factory = create_factory();
    EXPECT_REF(factory, 1);

    /* system collection takes factory reference */
    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection, FALSE);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(collection, 1);
    EXPECT_REF(factory, 2);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &collection2, FALSE);
    ok(hr == S_OK, "got %#x\n", hr);
    ok(collection2 == collection, "expected same collection\n");

    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    IDWriteFontCollection_Release(collection2);

    IDWriteFontCollection_AddRef(collection);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);
    IDWriteFontCollection_Release(collection);

    EXPECT_REF(collection, 1);

    /* family takes collection reference */
    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(family, 1);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    hr = IDWriteFontCollection_GetFontFamily(collection, 0, &family2);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(family2, 1);
    EXPECT_REF(collection, 3);
    EXPECT_REF(factory, 2);

    IDWriteFontFamily_Release(family2);

    EXPECT_REF(family, 1);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    /* font takes family reference */
    hr = IDWriteFontFamily_GetFirstMatchingFont(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &font);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(family, 2);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    hr = IDWriteFont_GetFontFamily(font, &family2);
    ok(hr == S_OK, "got %#x\n", hr);
    ok(family2 == family, "unexpected family pointer\n");
    IDWriteFontFamily_Release(family2);

    EXPECT_REF(font, 1);
    EXPECT_REF(factory, 2);

    /* Fontface takes factory reference and nothing else. */
    hr = IDWriteFont_CreateFontFace(font, &fontface);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(font, 1);
    EXPECT_REF_BROKEN(fontface, 1, 2);
    EXPECT_REF(family, 2);
    EXPECT_REF(collection, 2);
    EXPECT_REF_BROKEN(factory, 3, 2);

    /* get font from fontface */
    hr = IDWriteFontCollection_GetFontFromFontFace(collection, fontface, &font2);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(font, 1);
    EXPECT_REF(font2, 1);
    EXPECT_REF_BROKEN(fontface, 1, 2);
    EXPECT_REF(family, 2);
    EXPECT_REF(collection, 3);
    EXPECT_REF_BROKEN(factory, 3, 2);

    IDWriteFont_Release(font2);
    IDWriteFontFace_Release(fontface);

    EXPECT_REF(font, 1);
    EXPECT_REF(family, 2);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    IDWriteFont_Release(font);

    EXPECT_REF(family, 1);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    /* Matching fonts list takes family reference. */
    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &fontlist);
    ok(hr == S_OK, "got %#x\n", hr);

    EXPECT_REF(family, 2);
    EXPECT_REF(collection, 2);
    EXPECT_REF(factory, 2);

    hr = IDWriteFontFamily_GetMatchingFonts(family, DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &fontlist2);
    ok(hr == S_OK, "got %#x\n", hr);
    ok(fontlist2 != fontlist, "unexpected font list\n");
    IDWriteFontList_Release(fontlist2);

    IDWriteFontList_Release(fontlist);

    IDWriteFontFamily_Release(family);
    EXPECT_REF(collection, 1);

    EXPECT_REF(factory, 2);
    ref = IDWriteFontCollection_Release(collection);
    ok(ref == 0, "collection not released, %u\n", ref);
    EXPECT_REF(factory, 1);

    /* GDI interop object takes factory reference */
    hr = IDWriteFactory_GetGdiInterop(factory, &interop);
    ok(hr == S_OK, "got %#x\n", hr);
    EXPECT_REF(interop, 1);
    EXPECT_REF(factory, 2);

    hr = IDWriteFactory_GetGdiInterop(factory, &interop2);
    ok(hr == S_OK, "got %#x\n", hr);
    ok(interop == interop2, "got unexpected interop pointer\n");

    EXPECT_REF(interop, 2);
    EXPECT_REF(factory, 2);

    IDWriteGdiInterop_Release(interop2);
    ref = IDWriteGdiInterop_Release(interop);
    ok(ref == 0, "interop not released, %u\n", ref);

    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
7652 7653
}

7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700
struct testowner_object
{
    IUnknown IUnknown_iface;
    LONG ref;
};

static inline struct testowner_object *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, struct testowner_object, IUnknown_iface);
}

static HRESULT WINAPI testowner_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown)) {
        *obj = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }

    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI testowner_AddRef(IUnknown *iface)
{
    struct testowner_object *object = impl_from_IUnknown(iface);
    return InterlockedIncrement(&object->ref);
}

static ULONG WINAPI testowner_Release(IUnknown *iface)
{
    struct testowner_object *object = impl_from_IUnknown(iface);
    return InterlockedDecrement(&object->ref);
}

static const IUnknownVtbl testownervtbl = {
    testowner_QueryInterface,
    testowner_AddRef,
    testowner_Release,
};

static void testowner_init(struct testowner_object *object)
{
    object->IUnknown_iface.lpVtbl = &testownervtbl;
    object->ref = 1;
}

7701 7702
static void test_inmemory_file_loader(void)
{
7703
    IDWriteFontFileStream *stream, *stream2, *stream3;
7704 7705
    IDWriteFontFileLoader *loader, *loader2;
    IDWriteInMemoryFontFileLoader *inmemory;
7706
    struct testowner_object ownerobject;
7707 7708
    const void *key, *data, *frag_start;
    UINT64 file_size, size, writetime;
7709 7710
    IDWriteFontFile *file, *file2;
    IDWriteFontFace *fontface;
7711
    void *context, *context2;
7712
    IDWriteFactory5 *factory;
7713
    UINT32 count, key_size;
7714
    DWORD ref_key;
7715 7716 7717
    HRESULT hr;
    ULONG ref;

7718 7719 7720
    factory = create_factory_iid(&IID_IDWriteFactory5);
    if (!factory) {
        win_skip("CreateInMemoryFontFileLoader() is not supported\n");
7721 7722 7723
        return;
    }

7724 7725
    EXPECT_REF(factory, 1);
    hr = IDWriteFactory5_CreateInMemoryFontFileLoader(factory, &loader);
7726
    ok(hr == S_OK, "got %#x\n", hr);
7727
    EXPECT_REF(factory, 1);
7728

7729
    testowner_init(&ownerobject);
7730
    fontface = create_fontface((IDWriteFactory *)factory);
7731

7732
    hr = IDWriteFactory5_CreateInMemoryFontFileLoader(factory, &loader2);
7733 7734 7735 7736 7737 7738 7739
    ok(hr == S_OK, "got %#x\n", hr);
    ok(loader != loader2, "unexpected pointer\n");
    IDWriteFontFileLoader_Release(loader2);

    hr = IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteInMemoryFontFileLoader, (void **)&inmemory);
    ok(hr == S_OK, "got %#x\n", hr);
    IDWriteFontFileLoader_Release(loader);
7740
    EXPECT_REF(inmemory, 1);
7741

7742 7743 7744
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(!count, "Unexpected file count %u.\n", count);

7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766
    /* Use whole font blob to construct in-memory file. */
    count = 1;
    hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
    ok(hr == S_OK, "got %#x\n", hr);

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "got %#x\n", hr);

    hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
    ok(hr == S_OK, "got %#x\n", hr);

    hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, &stream);
    ok(hr == S_OK, "got %#x\n", hr);
    IDWriteFontFileLoader_Release(loader);
    IDWriteFontFile_Release(file);

    hr = IDWriteFontFileStream_GetFileSize(stream, &file_size);
    ok(hr == S_OK, "got %#x\n", hr);

    hr = IDWriteFontFileStream_ReadFileFragment(stream, &data, 0, file_size, &context);
    ok(hr == S_OK, "got %#x\n", hr);

7767
    /* Not registered yet. */
7768
    hr = IDWriteInMemoryFontFileLoader_CreateInMemoryFontFileReference(inmemory, (IDWriteFactory *)factory, data,
7769 7770 7771
        file_size, NULL, &file);
    ok(hr == E_INVALIDARG, "got %#x\n", hr);

7772 7773 7774
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 1, "Unexpected file count %u.\n", count);

7775
    hr = IDWriteFactory5_RegisterFontFileLoader(factory, (IDWriteFontFileLoader *)inmemory);
7776
    ok(hr == S_OK, "got %#x\n", hr);
7777
    EXPECT_REF(inmemory, 2);
7778

7779
    EXPECT_REF(&ownerobject.IUnknown_iface, 1);
7780
    hr = IDWriteInMemoryFontFileLoader_CreateInMemoryFontFileReference(inmemory, (IDWriteFactory *)factory, data,
7781
        file_size, &ownerobject.IUnknown_iface, &file);
7782
    ok(hr == S_OK, "got %#x\n", hr);
7783 7784
    EXPECT_REF(&ownerobject.IUnknown_iface, 2);
    EXPECT_REF(inmemory, 3);
7785

7786 7787 7788
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 2, "Unexpected file count %u.\n", count);

7789
    hr = IDWriteInMemoryFontFileLoader_CreateInMemoryFontFileReference(inmemory, (IDWriteFactory *)factory, data,
7790
        file_size, &ownerobject.IUnknown_iface, &file2);
7791 7792
    ok(hr == S_OK, "got %#x\n", hr);
    ok(file2 != file, "got unexpected file\n");
7793 7794
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
    EXPECT_REF(inmemory, 4);
7795

7796 7797 7798
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 3, "Unexpected file count %u.\n", count);

7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811
    /* Check in-memory reference key format. */
    hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
    ok(hr == S_OK, "got %#x\n", hr);

    ok(key && *(DWORD*)key == 1, "got wrong ref key\n");
    ok(key_size == 4, "ref key size %u\n", key_size);

    hr = IDWriteFontFile_GetReferenceKey(file2, &key, &key_size);
    ok(hr == S_OK, "got %#x\n", hr);

    ok(key && *(DWORD*)key == 2, "got wrong ref key\n");
    ok(key_size == 4, "ref key size %u\n", key_size);

7812 7813 7814 7815 7816
    EXPECT_REF(inmemory, 4);
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, key, key_size, &stream2);
    ok(hr == S_OK, "Failed to create a stream, hr %#x.\n", hr);
    EXPECT_REF(stream2, 1);
    EXPECT_REF(inmemory, 4);
7817

7818 7819
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, key, key_size, &stream3);
    ok(hr == S_OK, "Failed to create a stream, hr %#x.\n", hr);
7820

7821
    ok(stream2 != stream3, "Unexpected stream.\n");
7822

7823 7824
    IDWriteFontFileStream_Release(stream2);
    IDWriteFontFileStream_Release(stream3);
7825

7826 7827
    /* Release file at index 1, create new one to see if index is reused. */
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
7828 7829
    ref = IDWriteFontFile_Release(file);
    ok(ref == 0, "File object not released, %u.\n", ref);
7830 7831
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);

7832 7833 7834
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 3, "Unexpected file count %u.\n", count);

7835
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
7836 7837
    ref = IDWriteFontFile_Release(file2);
    ok(ref == 0, "File object not released, %u.\n", ref);
7838
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
7839

7840 7841 7842
    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 3, "Unexpected file count %u.\n", count);

7843
    hr = IDWriteFactory5_UnregisterFontFileLoader(factory, (IDWriteFontFileLoader *)inmemory);
7844
    ok(hr == S_OK, "got %#x\n", hr);
7845
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
7846

7847 7848 7849 7850
    EXPECT_REF(&ownerobject.IUnknown_iface, 3);
    ref = IDWriteInMemoryFontFileLoader_Release(inmemory);
    ok(ref == 0, "loader not released, %u.\n", ref);
    EXPECT_REF(&ownerobject.IUnknown_iface, 1);
7851

7852
    /* Test reference key for first added file. */
7853
    hr = IDWriteFactory5_CreateInMemoryFontFileLoader(factory, &loader);
7854 7855 7856 7857 7858 7859
    ok(hr == S_OK, "Failed to create loader, hr %#x.\n", hr);

    hr = IDWriteFontFileLoader_QueryInterface(loader, &IID_IDWriteInMemoryFontFileLoader, (void **)&inmemory);
    ok(hr == S_OK, "Failed to get in-memory interface, hr %#x.\n", hr);
    IDWriteFontFileLoader_Release(loader);

7860
    hr = IDWriteFactory5_RegisterFontFileLoader(factory, (IDWriteFontFileLoader *)inmemory);
7861 7862 7863 7864 7865 7866
    ok(hr == S_OK, "Failed to register loader, hr %#x.\n", hr);

    ref_key = 0;
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, &ref_key, sizeof(ref_key), &stream2);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

7867
    hr = IDWriteInMemoryFontFileLoader_CreateInMemoryFontFileReference(inmemory, (IDWriteFactory *)factory, data,
7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919
        file_size, &ownerobject.IUnknown_iface, &file);
    ok(hr == S_OK, "Failed to create in-memory file reference, hr %#x.\n", hr);

    ref_key = 0;
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, &ref_key, sizeof(ref_key), &stream2);
    ok(hr == S_OK, "Failed to create a stream, hr %#x.\n", hr);

    context2 = (void *)0xdeadbeef;
    hr = IDWriteFontFileStream_ReadFileFragment(stream2, &frag_start, 0, file_size, &context2);
    ok(hr == S_OK, "Failed to read a fragment, hr %#x.\n", hr);
    ok(context == NULL, "Unexpected context %p.\n", context2);
    ok(frag_start == data, "Unexpected fragment pointer %p.\n", frag_start);

    hr = IDWriteFontFileStream_GetFileSize(stream2, &size);
    ok(hr == S_OK, "Failed to get file size, hr %#x.\n", hr);
    ok(size == file_size, "Unexpected file size.\n");

    IDWriteFontFileStream_ReleaseFileFragment(stream2, context2);

    writetime = 1;
    hr = IDWriteFontFileStream_GetLastWriteTime(stream2, &writetime);
    ok(hr == E_NOTIMPL, "Unexpected hr %#x.\n", hr);
    ok(writetime == 0, "Unexpected writetime.\n");

    IDWriteFontFileStream_Release(stream2);

    ref_key = 0;
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, NULL, sizeof(ref_key) - 1, &stream2);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

    ref_key = 0;
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, &ref_key, sizeof(ref_key) - 1, &stream2);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

    ref_key = 0;
    hr = IDWriteInMemoryFontFileLoader_CreateStreamFromKey(inmemory, &ref_key, sizeof(ref_key) + 1, &stream2);
    ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);

    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 1, "Unexpected file count %u.\n", count);

    hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
    ok(hr == S_OK, "Failed to get reference key, hr %#x.\n", hr);

    ok(key && *(DWORD*)key == 0, "Unexpected reference key.\n");
    ok(key_size == 4, "Unexpected key size %u.\n", key_size);

    IDWriteFontFile_Release(file);

    count = IDWriteInMemoryFontFileLoader_GetFileCount(inmemory);
    ok(count == 1, "Unexpected file count %u.\n", count);

7920
    hr = IDWriteFactory5_UnregisterFontFileLoader(factory, (IDWriteFontFileLoader *)inmemory);
7921 7922 7923 7924 7925 7926
    ok(hr == S_OK, "Failed to unregister loader, hr %#x.\n", hr);

    IDWriteFontFileStream_ReleaseFileFragment(stream, context);
    IDWriteFontFileStream_Release(stream);
    IDWriteFontFace_Release(fontface);

7927 7928 7929
    ref = IDWriteInMemoryFontFileLoader_Release(inmemory);
    ok(ref == 0, "loader not released, %u.\n", ref);

7930
    ref = IDWriteFactory5_Release(factory);
7931 7932 7933
    ok(ref == 0, "factory not released, %u\n", ref);
}

7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949
static BOOL face_has_table(IDWriteFontFace4 *fontface, UINT32 tag)
{
    BOOL exists = FALSE;
    const void *data;
    void *context;
    UINT32 size;
    HRESULT hr;

    hr = IDWriteFontFace4_TryGetFontTable(fontface, tag, &data, &size, &context, &exists);
    ok(hr == S_OK, "TryGetFontTable() failed, %#x\n", hr);
    if (exists)
        IDWriteFontFace4_ReleaseFontTable(fontface, context);

    return exists;
}

7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013
static DWORD get_sbix_formats(IDWriteFontFace4 *fontface)
{
    UINT32 size, s, num_strikes;
    const sbix_header *header;
    UINT16 g, num_glyphs;
    BOOL exists = FALSE;
    const maxp *maxp;
    const void *data;
    DWORD ret = 0;
    void *context;
    HRESULT hr;

    hr = IDWriteFontFace4_TryGetFontTable(fontface, MS_MAXP_TAG, &data, &size, &context, &exists);
    ok(hr == S_OK, "TryGetFontTable() failed, %#x\n", hr);
    ok(exists, "Expected maxp table\n");

    if (!exists)
        return 0;

    maxp = data;
    num_glyphs = GET_BE_WORD(maxp->numGlyphs);

    hr = IDWriteFontFace4_TryGetFontTable(fontface, MS_SBIX_TAG, &data, &size, &context, &exists);
    ok(hr == S_OK, "TryGetFontTable() failed, %#x\n", hr);
    ok(exists, "Expected sbix table\n");

    header = data;
    num_strikes = GET_BE_DWORD(header->numStrikes);

    for (s = 0; s < num_strikes; s++) {
        sbix_strike *strike = (sbix_strike *)((BYTE *)header + GET_BE_DWORD(header->strikeOffset[s]));

        for (g = 0; g < num_glyphs; g++) {
            DWORD offset = GET_BE_DWORD(strike->glyphDataOffsets[g]);
            DWORD offset_next = GET_BE_DWORD(strike->glyphDataOffsets[g + 1]);
            sbix_glyph_data *glyph_data;
            DWORD format;

            if (offset == offset_next)
                continue;

            glyph_data = (sbix_glyph_data *)((BYTE *)strike + offset);
            switch (format = glyph_data->graphicType)
            {
            case MS_PNG__TAG:
                ret |= DWRITE_GLYPH_IMAGE_FORMATS_PNG;
                break;
            case MS_JPG__TAG:
                ret |= DWRITE_GLYPH_IMAGE_FORMATS_JPEG;
                break;
            case MS_TIFF_TAG:
                ret |= DWRITE_GLYPH_IMAGE_FORMATS_TIFF;
                break;
            default:
                ok(0, "unexpected format, %#x\n", GET_BE_DWORD(format));
            }
        }
    }

    IDWriteFontFace4_ReleaseFontTable(fontface, context);

    return ret;
}

8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047
static DWORD get_cblc_formats(IDWriteFontFace4 *fontface)
{
    CBLCBitmapSizeTable *sizes;
    UINT32 num_sizes, size, s;
    BOOL exists = FALSE;
    CBLCHeader *header;
    DWORD ret = 0;
    void *context;
    HRESULT hr;

    hr = IDWriteFontFace4_TryGetFontTable(fontface, MS_CBLC_TAG, (const void **)&header, &size, &context, &exists);
    ok(hr == S_OK, "TryGetFontTable() failed, %#x\n", hr);
    ok(exists, "Expected CBLC table\n");

    if (!exists)
        return 0;

    num_sizes = GET_BE_DWORD(header->numSizes);
    sizes = (CBLCBitmapSizeTable *)(header + 1);

    for (s = 0; s < num_sizes; s++) {
        BYTE bpp = sizes->bitDepth;

        if (bpp == 1 || bpp == 2 || bpp == 4 || bpp == 8)
            ret |= DWRITE_GLYPH_IMAGE_FORMATS_PNG;
        else if (bpp == 32)
            ret |= DWRITE_GLYPH_IMAGE_FORMATS_PREMULTIPLIED_B8G8R8A8;
    }

    IDWriteFontFace4_ReleaseFontTable(fontface, context);

    return ret;
}

8048 8049 8050 8051 8052 8053 8054
static DWORD get_face_glyph_image_formats(IDWriteFontFace4 *fontface)
{
    DWORD ret = DWRITE_GLYPH_IMAGE_FORMATS_NONE;

    if (face_has_table(fontface, MS_GLYF_TAG))
        ret |= DWRITE_GLYPH_IMAGE_FORMATS_TRUETYPE;

8055 8056
    if (face_has_table(fontface, MS_CFF__TAG) ||
            face_has_table(fontface, MS_CFF2_TAG))
8057 8058 8059 8060 8061
        ret |= DWRITE_GLYPH_IMAGE_FORMATS_CFF;

    if (face_has_table(fontface, MS_COLR_TAG))
        ret |= DWRITE_GLYPH_IMAGE_FORMATS_COLR;

8062 8063 8064
    if (face_has_table(fontface, MS_SVG__TAG))
        ret |= DWRITE_GLYPH_IMAGE_FORMATS_SVG;

8065 8066 8067
    if (face_has_table(fontface, MS_SBIX_TAG))
        ret |= get_sbix_formats(fontface);

8068 8069 8070
    if (face_has_table(fontface, MS_CBLC_TAG))
        ret |= get_cblc_formats(fontface);

8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153
    return ret;
}

static void test_GetGlyphImageFormats(void)
{
    IDWriteFontCollection *syscollection;
    IDWriteFactory *factory;
    UINT32 i, count;
    HRESULT hr;
    ULONG ref;
    IDWriteFontFace *fontface;
    IDWriteFontFace4 *fontface4;

    factory = create_factory();

    fontface = create_fontface(factory);
    hr = IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace4, (void **)&fontface4);
    IDWriteFontFace_Release(fontface);
    if (FAILED(hr)) {
        win_skip("GetGlyphImageFormats() is not supported\n");
        IDWriteFactory_Release(factory);
        return;
    }
    IDWriteFontFace4_Release(fontface4);

    hr = IDWriteFactory_GetSystemFontCollection(factory, &syscollection, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    count = IDWriteFontCollection_GetFontFamilyCount(syscollection);

    for (i = 0; i < count; i++) {
        WCHAR familynameW[256], facenameW[128];
        IDWriteLocalizedStrings *names;
        IDWriteFontFamily *family;
        UINT32 j, fontcount;
        IDWriteFont *font;

        hr = IDWriteFontCollection_GetFontFamily(syscollection, i, &family);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        hr = IDWriteFontFamily_GetFamilyNames(family, &names);
        ok(hr == S_OK, "got 0x%08x\n", hr);

        get_enus_string(names, familynameW, sizeof(familynameW)/sizeof(*familynameW));
        IDWriteLocalizedStrings_Release(names);

        fontcount = IDWriteFontFamily_GetFontCount(family);
        for (j = 0; j < fontcount; j++) {
            DWORD formats, expected_formats;

            hr = IDWriteFontFamily_GetFont(family, j, &font);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            hr = IDWriteFont_CreateFontFace(font, &fontface);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            hr = IDWriteFont_GetFaceNames(font, &names);
            ok(hr == S_OK, "got 0x%08x\n", hr);

            get_enus_string(names, facenameW, sizeof(facenameW)/sizeof(*facenameW));

            IDWriteLocalizedStrings_Release(names);

            IDWriteFontFace_QueryInterface(fontface, &IID_IDWriteFontFace4, (void **)&fontface4);

            /* Mask describes font as a whole. */
            formats = IDWriteFontFace4_GetGlyphImageFormats(fontface4);
            expected_formats = get_face_glyph_image_formats(fontface4);
            ok(formats == expected_formats, "%s - %s, expected formats %#x, got formats %#x.\n",
                wine_dbgstr_w(familynameW), wine_dbgstr_w(facenameW), expected_formats, formats);

            IDWriteFontFace4_Release(fontface4);
            IDWriteFontFace_Release(fontface);
            IDWriteFont_Release(font);
        }

        IDWriteFontFamily_Release(family);
    }

    IDWriteFontCollection_Release(syscollection);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
}

8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217
static void test_CreateCustomRenderingParams(void)
{
    static const struct custom_params_test
    {
        FLOAT gamma;
        FLOAT contrast;
        FLOAT cleartype_level;
        DWRITE_PIXEL_GEOMETRY geometry;
        DWRITE_RENDERING_MODE rendering_mode;
        HRESULT hr;
    } params_tests[] =
    {
        {  0.0f,  0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.0f,  0.1f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.0f,  0.0f,  0.1f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        { -0.1f,  0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.1f, -0.1f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.1f,  0.0f, -0.1f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.1f,  0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL },
        {  0.01f, 0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_FLAT, DWRITE_RENDERING_MODE_NATURAL },
        {  0.1f,  0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_BGR + 1, DWRITE_RENDERING_MODE_NATURAL, E_INVALIDARG },
        {  0.1f,  0.0f,  0.0f, DWRITE_PIXEL_GEOMETRY_BGR, DWRITE_RENDERING_MODE_OUTLINE + 1, E_INVALIDARG },
        {  0.1f,  0.0f,  2.0f, DWRITE_PIXEL_GEOMETRY_BGR, DWRITE_RENDERING_MODE_NATURAL },
    };
    IDWriteFactory *factory;
    unsigned int i;
    HRESULT hr;
    ULONG ref;

    factory = create_factory();

    for (i = 0; i < sizeof(params_tests)/sizeof(*params_tests); i++) {
        IDWriteRenderingParams *params;

        params = (void *)0xdeadbeef;
        hr = IDWriteFactory_CreateCustomRenderingParams(factory, params_tests[i].gamma, params_tests[i].contrast,
                params_tests[i].cleartype_level, params_tests[i].geometry, params_tests[i].rendering_mode, &params);
        ok(hr == params_tests[i].hr, "%u: unexpected hr %#x, expected %#x.\n", i, hr, params_tests[i].hr);

        if (hr == S_OK) {
            ok(params_tests[i].gamma == IDWriteRenderingParams_GetGamma(params), "%u: unexpected gamma %f, expected %f.\n",
                    i, IDWriteRenderingParams_GetGamma(params), params_tests[i].gamma);
            ok(params_tests[i].contrast == IDWriteRenderingParams_GetEnhancedContrast(params),
                    "%u: unexpected contrast %f, expected %f.\n",
                    i, IDWriteRenderingParams_GetEnhancedContrast(params), params_tests[i].contrast);
            ok(params_tests[i].cleartype_level == IDWriteRenderingParams_GetClearTypeLevel(params),
                    "%u: unexpected ClearType level %f, expected %f.\n",
                    i, IDWriteRenderingParams_GetClearTypeLevel(params), params_tests[i].cleartype_level);
            ok(params_tests[i].geometry == IDWriteRenderingParams_GetPixelGeometry(params),
                    "%u: unexpected pixel geometry %u, expected %u.\n", i, IDWriteRenderingParams_GetPixelGeometry(params),
                    params_tests[i].geometry);
            ok(params_tests[i].rendering_mode == IDWriteRenderingParams_GetRenderingMode(params),
                    "%u: unexpected rendering mode %u, expected %u.\n", i, IDWriteRenderingParams_GetRenderingMode(params),
                    params_tests[i].rendering_mode);
            IDWriteRenderingParams_Release(params);
        }
        else
            ok(params == NULL, "%u: expected NULL interface pointer on failure.\n", i);
    }

    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
}

8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254
static void test_localfontfileloader(void)
{
    IDWriteFontFileLoader *loader, *loader2;
    IDWriteFactory *factory, *factory2;
    IDWriteFontFile *file, *file2;
    WCHAR *path;
    HRESULT hr;
    ULONG ref;

    factory = create_factory();
    factory2 = create_factory();

    path = create_testfontfile(test_fontfile);

    hr = IDWriteFactory_CreateFontFileReference(factory, path, NULL, &file);
    ok(hr == S_OK, "Failed to create file reference, hr %#x.\n", hr);

    hr = IDWriteFactory_CreateFontFileReference(factory2, path, NULL, &file2);
    ok(hr == S_OK, "Failed to create file reference, hr %#x.\n", hr);
    ok(file != file2, "Unexpected file instance.\n");

    hr = IDWriteFontFile_GetLoader(file, &loader);
    ok(hr == S_OK, "Failed to get loader, hr %#x.\n", hr);

    hr = IDWriteFontFile_GetLoader(file2, &loader2);
    ok(hr == S_OK, "Failed to get loader, hr %#x.\n", hr);
    ok(loader == loader2, "Unexpected loader instance\n");

    IDWriteFontFile_Release(file);
    IDWriteFontFile_Release(file2);
    IDWriteFontFileLoader_Release(loader);
    IDWriteFontFileLoader_Release(loader2);
    ref = IDWriteFactory_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
    DELETE_FONTFILE(path);
}

8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307
static void test_AnalyzeContainerType(void)
{
    struct WOFFHeader2 woff2_header;
    struct WOFFHeader woff_header;
    DWRITE_CONTAINER_TYPE type;
    IDWriteFactory5 *factory;

    factory = create_factory_iid(&IID_IDWriteFactory5);
    if (!factory) {
        win_skip("AnalyzeContainerType() is not supported.\n");
        return;
    }

    type = IDWriteFactory5_AnalyzeContainerType(factory, NULL, 0);
    ok(type == DWRITE_CONTAINER_TYPE_UNKNOWN, "Unexpected container type %u.\n", type);

    type = IDWriteFactory5_AnalyzeContainerType(factory, (void const *)0xdeadbeef, 0);
    ok(type == DWRITE_CONTAINER_TYPE_UNKNOWN, "Unexpected container type %u.\n", type);

    memset(&woff_header, 0xff, sizeof(woff_header));
    woff_header.signature = GET_LE_DWORD(MS_WOFF_TAG);
    woff_header.length = 0;
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff_header, sizeof(woff_header));
    ok(type == DWRITE_CONTAINER_TYPE_WOFF, "Unexpected container type %u.\n", type);

    memset(&woff_header, 0xff, sizeof(woff_header));
    woff_header.signature = GET_LE_DWORD(MS_WOFF_TAG);
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff_header, sizeof(woff_header.signature));
    ok(type == DWRITE_CONTAINER_TYPE_WOFF, "Unexpected container type %u.\n", type);

    memset(&woff_header, 0xff, sizeof(woff_header));
    woff_header.signature = GET_LE_DWORD(MS_WOFF_TAG);
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff_header, sizeof(woff_header.signature) - 1);
    ok(type == DWRITE_CONTAINER_TYPE_UNKNOWN, "Unexpected container type %u.\n", type);

    memset(&woff2_header, 0xff, sizeof(woff2_header));
    woff2_header.signature = GET_LE_DWORD(MS_WOF2_TAG);
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff2_header, sizeof(woff2_header));
    ok(type == DWRITE_CONTAINER_TYPE_WOFF2, "Unexpected container type %u.\n", type);

    memset(&woff2_header, 0xff, sizeof(woff2_header));
    woff2_header.signature = GET_LE_DWORD(MS_WOF2_TAG);
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff2_header, sizeof(woff2_header.signature));
    ok(type == DWRITE_CONTAINER_TYPE_WOFF2, "Unexpected container type %u.\n", type);

    memset(&woff2_header, 0xff, sizeof(woff2_header));
    woff2_header.signature = GET_LE_DWORD(MS_WOF2_TAG);
    type = IDWriteFactory5_AnalyzeContainerType(factory, &woff2_header, sizeof(woff2_header.signature) - 1);
    ok(type == DWRITE_CONTAINER_TYPE_UNKNOWN, "Unexpected container type %u.\n", type);

    IDWriteFactory5_Release(factory);
}

8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450
static void test_fontsetbuilder(void)
{
    IDWriteFontCollection1 *collection;
    IDWriteFontSetBuilder *builder;
    IDWriteFactory3 *factory;
    UINT32 count, i, ref;
    HRESULT hr;

    factory = create_factory_iid(&IID_IDWriteFactory3);
    if (!factory) {
        skip("IDWriteFontSetBuilder is not supported.\n");
        return;
    }

    EXPECT_REF(factory, 1);
    hr = IDWriteFactory3_CreateFontSetBuilder(factory, &builder);
todo_wine
    ok(hr == S_OK, "Failed to create font set builder, hr %#x.\n", hr);

    if (FAILED(hr)) {
        IDWriteFactory3_Release(factory);
        return;
    }

    EXPECT_REF(factory, 2);
    IDWriteFontSetBuilder_Release(builder);

    hr = IDWriteFactory3_GetSystemFontCollection(factory, FALSE, &collection, FALSE);
    ok(hr == S_OK, "Failed to get system collection, hr %#x.\n", hr);
    count = IDWriteFontCollection1_GetFontFamilyCount(collection);

    for (i = 0; i < count; i++) {
        IDWriteFontFamily1 *family;
        UINT32 j, fontcount;
        IDWriteFont3 *font;

        hr = IDWriteFontCollection1_GetFontFamily(collection, i, &family);
        ok(hr == S_OK, "Failed to get family, hr %#x.\n", hr);

        fontcount = IDWriteFontFamily1_GetFontCount(family);
        for (j = 0; j < fontcount; j++) {
            IDWriteFontFaceReference *ref, *ref2;
            IDWriteFontSet *fontset;
            UINT32 setcount, id;

            hr = IDWriteFontFamily1_GetFont(family, j, &font);
            ok(hr == S_OK, "Failed to get font, hr %#x.\n", hr);

            /* Create a set with a single font reference, test set properties. */
            hr = IDWriteFactory3_CreateFontSetBuilder(factory, &builder);
            ok(hr == S_OK, "Failed to create font set builder, hr %#x.\n", hr);

            hr = IDWriteFont3_GetFontFaceReference(font, &ref);
            ok(hr == S_OK, "Failed to get fontface reference, hr %#x.\n", hr);

            EXPECT_REF(ref, 1);
            hr = IDWriteFontSetBuilder_AddFontFaceReference(builder, ref);
            ok(hr == S_OK, "Failed to add fontface reference, hr %#x.\n", hr);
            EXPECT_REF(ref, 1);

            hr = IDWriteFontSetBuilder_CreateFontSet(builder, &fontset);
            ok(hr == S_OK, "Failed to create a font set, hr %#x.\n", hr);

            setcount = IDWriteFontSet_GetFontCount(fontset);
            ok(setcount == 1, "Unexpected font count %u.\n", setcount);

            hr = IDWriteFontSet_GetFontFaceReference(fontset, 0, &ref2);
            ok(hr == S_OK, "Failed to get font face reference, hr %#x.\n", hr);
            ok(ref2 != ref, "Unexpected reference.\n");
            IDWriteFontFaceReference_Release(ref2);

            for (id = DWRITE_FONT_PROPERTY_ID_FAMILY_NAME; id < DWRITE_FONT_PROPERTY_ID_TOTAL; id++) {
                static const WCHAR fmtW[] = {'%','u',0};
                IDWriteLocalizedStrings *values;
                WCHAR buffW[255], buff2W[255];
                UINT32 c, ivalue;
                BOOL exists;

                hr = IDWriteFontSet_GetPropertyValues(fontset, 0, id, &exists, &values);
                ok(hr == S_OK, "Failed to get property value, hr %#x.\n", hr);

                if (!exists)
                    continue;

                switch (id)
                {
                case DWRITE_FONT_PROPERTY_ID_WEIGHT:
                    ivalue = IDWriteFont3_GetWeight(font);
                    break;
                case DWRITE_FONT_PROPERTY_ID_STRETCH:
                    ivalue = IDWriteFont3_GetStretch(font);
                    break;
                case DWRITE_FONT_PROPERTY_ID_STYLE:
                    ivalue = IDWriteFont3_GetStyle(font);
                    break;
                default:
                    ;
                }

                switch (id)
                {
                case DWRITE_FONT_PROPERTY_ID_WEIGHT:
                case DWRITE_FONT_PROPERTY_ID_STRETCH:
                case DWRITE_FONT_PROPERTY_ID_STYLE:
                    c = IDWriteLocalizedStrings_GetCount(values);
                    ok(c == 1, "Unexpected string count %u.\n", c);

                    buffW[0] = 'a';
                    hr = IDWriteLocalizedStrings_GetLocaleName(values, 0, buffW, sizeof(buffW)/sizeof(buffW[0]));
                    ok(hr == S_OK, "Failed to get locale name, hr %#x.\n", hr);
                    ok(!*buffW, "Unexpected locale %s.\n", wine_dbgstr_w(buffW));

                    buff2W[0] = 0;
                    hr = IDWriteLocalizedStrings_GetString(values, 0, buff2W, sizeof(buff2W)/sizeof(buff2W[0]));
                    ok(hr == S_OK, "Failed to get property string, hr %#x.\n", hr);

                    wsprintfW(buffW, fmtW, ivalue);
                    ok(!lstrcmpW(buffW, buff2W), "Unexpected property value %s, expected %s.\n", wine_dbgstr_w(buff2W),
                        wine_dbgstr_w(buffW));
                    break;
                default:
                    ;
                }

                IDWriteLocalizedStrings_Release(values);
            }

            IDWriteFontSet_Release(fontset);
            IDWriteFontFaceReference_Release(ref);
            IDWriteFontSetBuilder_Release(builder);

            IDWriteFont3_Release(font);
        }

        IDWriteFontFamily1_Release(family);
    }

    IDWriteFontCollection1_Release(collection);

    ref = IDWriteFactory3_Release(factory);
    ok(ref == 0, "factory not released, %u\n", ref);
}

8451 8452
START_TEST(font)
{
8453
    IDWriteFactory *factory;
8454

8455
    if (!(factory = create_factory())) {
8456 8457 8458 8459
        win_skip("failed to create factory\n");
        return;
    }

8460
    test_object_lifetime();
8461
    test_CreateFontFromLOGFONT();
8462
    test_CreateBitmapRenderTarget();
8463
    test_GetFontFamily();
8464
    test_GetFamilyNames();
8465
    test_CreateFontFace();
8466
    test_GetMetrics();
8467
    test_system_fontcollection();
8468
    test_ConvertFontFaceToLOGFONT();
8469
    test_CustomFontCollection();
8470
    test_CreateCustomFontFileReference();
8471
    test_CreateFontFileReference();
8472
    test_shared_isolated();
8473
    test_GetUnicodeRanges();
8474 8475
    test_GetFontFromFontFace();
    test_GetFirstMatchingFont();
8476
    test_GetMatchingFonts();
8477 8478
    test_GetInformationalStrings();
    test_GetGdiInterop();
8479
    test_CreateFontFaceFromHdc();
8480
    test_GetSimulations();
8481
    test_GetFaceNames();
8482
    test_TryGetFontTable();
8483
    test_ConvertFontToLOGFONT();
8484
    test_CreateStreamFromKey();
8485
    test_ReadFileFragment();
8486
    test_GetDesignGlyphMetrics();
8487
    test_GetDesignGlyphAdvances();
8488
    test_IsMonospacedFont();
8489
    test_GetGlyphRunOutline();
8490
    test_GetEudcFontCollection();
8491
    test_GetCaretMetrics();
8492
    test_GetGlyphCount();
8493
    test_GetKerningPairAdjustments();
8494
    test_CreateRenderingParams();
8495
    test_CreateGlyphRunAnalysis();
8496
    test_GetGdiCompatibleMetrics();
8497
    test_GetPanose();
8498
    test_GetGdiCompatibleGlyphAdvances();
8499
    test_GetRecommendedRenderingMode();
8500
    test_GetAlphaBlendParams();
8501
    test_CreateAlphaTexture();
8502
    test_IsSymbolFont();
8503
    test_GetPaletteEntries();
8504
    test_TranslateColorGlyphRun();
8505
    test_HasCharacter();
8506
    test_CreateFontFaceReference();
8507
    test_GetFontSignature();
8508
    test_font_properties();
8509
    test_HasVerticalGlyphVariants();
8510
    test_HasKerningPairs();
8511
    test_ComputeGlyphOrigins();
8512
    test_inmemory_file_loader();
8513
    test_GetGlyphImageFormats();
8514
    test_CreateCustomRenderingParams();
8515
    test_localfontfileloader();
8516
    test_AnalyzeContainerType();
8517
    test_fontsetbuilder();
8518 8519 8520

    IDWriteFactory_Release(factory);
}