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

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
25
#include "winnls.h"
26
#include "winreg.h"
27 28 29
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
30 31 32 33 34 35

#include "objbase.h"

#include "gdiplus.h"
#include "gdiplus_private.h"

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 106 107 108 109 110 111 112 113 114 115 116 117
/* PANOSE is 10 bytes in size, need to pack the structure properly */
#include "pshpack2.h"
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;

typedef struct
{
    ULONG Version;
    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;
#include "poppack.h"

#ifdef WORDS_BIGENDIAN
#define GET_BE_WORD(x) (x)
#define GET_BE_DWORD(x) (x)
#else
#define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
#define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
#endif

#define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
                    ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
                    ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
#define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
#define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')

118 119
static GpFontCollection installedFontCollection = {0};

120 121 122 123 124 125 126 127 128
static CRITICAL_SECTION font_cs;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &font_cs,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": font_cs") }
};
static CRITICAL_SECTION font_cs = { &critsect_debug, -1, 0, 0, 0, 0 };

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*******************************************************************************
 * GdipCreateFont [GDIPLUS.@]
 *
 * Create a new font based off of a FontFamily
 *
 * PARAMS
 *  *fontFamily     [I] Family to base the font off of
 *  emSize          [I] Size of the font
 *  style           [I] Bitwise OR of FontStyle enumeration
 *  unit            [I] Unit emSize is measured in
 *  **font          [I] the resulting Font object
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter if fontfamily or font is NULL.
 *  FAILURE: FontFamilyNotFound if an invalid FontFamily is given
 *
 * NOTES
 *  UnitDisplay is unsupported.
 *  emSize is stored separately from lfHeight, to hold the fraction.
 */
GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
                        REAL emSize, INT style, Unit unit, GpFont **font)
{
153 154 155 156
    HFONT hfont;
    OUTLINETEXTMETRICW otm;
    LOGFONTW lfw;
    HDC hdc;
157
    GpStatus stat;
158
    int ret;
159

160
    if (!fontFamily || !font || emSize < 0.0)
161 162 163 164 165
        return InvalidParameter;

    TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
            debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);

166
    memset(&lfw, 0, sizeof(lfw));
167

168 169
    stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
    if (stat != Ok) return stat;
170

171
    lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi, FALSE);
172 173 174 175 176 177 178 179 180 181 182 183
    lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
    lfw.lfItalic = style & FontStyleItalic;
    lfw.lfUnderline = style & FontStyleUnderline;
    lfw.lfStrikeOut = style & FontStyleStrikeout;

    hfont = CreateFontIndirectW(&lfw);
    hdc = CreateCompatibleDC(0);
    SelectObject(hdc, hfont);
    otm.otmSize = sizeof(otm);
    ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
    DeleteDC(hdc);
    DeleteObject(hfont);
184

185
    if (!ret) return NotTrueTypeFont;
186

187
    *font = heap_alloc_zero(sizeof(GpFont));
188
    if (!*font) return OutOfMemory;
189 190 191

    (*font)->unit = unit;
    (*font)->emSize = emSize;
192
    (*font)->otm = otm;
193
    GdipCloneFontFamily((GpFontFamily*)fontFamily, &(*font)->family);
194

195 196
    TRACE("<-- %p\n", *font);

197 198 199
    return Ok;
}

200 201 202
/*******************************************************************************
 * GdipCreateFontFromLogfontW [GDIPLUS.@]
 */
203 204 205
GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
    GDIPCONST LOGFONTW *logfont, GpFont **font)
{
206
    HFONT hfont, oldfont;
207
    OUTLINETEXTMETRICW otm;
208
    WCHAR facename[LF_FACESIZE];
209
    GpStatus stat;
210
    int ret;
211

212 213
    TRACE("(%p, %p, %p)\n", hdc, logfont, font);

214
    if (!hdc || !logfont || !font)
215 216
        return InvalidParameter;

217
    hfont = CreateFontIndirectW(logfont);
218
    oldfont = SelectObject(hdc, hfont);
219 220
    otm.otmSize = sizeof(otm);
    ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
221
    GetTextFaceW(hdc, LF_FACESIZE, facename);
222 223
    SelectObject(hdc, oldfont);
    DeleteObject(hfont);
224

225
    if (!ret) return NotTrueTypeFont;
226

227
    *font = heap_alloc_zero(sizeof(GpFont));
228
    if (!*font) return OutOfMemory;
229

230
    (*font)->unit = UnitWorld;
231
    (*font)->emSize = otm.otmTextMetrics.tmHeight - otm.otmTextMetrics.tmInternalLeading;
232
    (*font)->otm = otm;
233

234
    stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
235 236
    if (stat != Ok)
    {
237
        heap_free(*font);
238 239 240
        return NotTrueTypeFont;
    }

241 242
    TRACE("<-- %p\n", *font);

243 244
    return Ok;
}
245

246 247 248
/*******************************************************************************
 * GdipCreateFontFromLogfontA [GDIPLUS.@]
 */
249 250 251 252 253
GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
    GDIPCONST LOGFONTA *lfa, GpFont **font)
{
    LOGFONTW lfw;

254 255
    TRACE("(%p, %p, %p)\n", hdc, lfa, font);

256 257 258
    if(!lfa || !font)
        return InvalidParameter;

259
    memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
260 261 262 263

    if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
        return GenericError;

264
    return GdipCreateFontFromLogfontW(hdc, &lfw, font);
265
}
266

267 268 269
/*******************************************************************************
 * GdipDeleteFont [GDIPLUS.@]
 */
270 271
GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
{
272 273
    TRACE("(%p)\n", font);

274 275 276
    if(!font)
        return InvalidParameter;

277
    GdipDeleteFontFamily(font->family);
278
    heap_free(font);
279 280 281 282

    return Ok;
}

283 284 285
/*******************************************************************************
 * GdipCreateFontFromDC [GDIPLUS.@]
 */
286 287 288 289 290
GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
{
    HFONT hfont;
    LOGFONTW lfw;

291 292
    TRACE("(%p, %p)\n", hdc, font);

293 294 295
    if(!font)
        return InvalidParameter;

296
    hfont = GetCurrentObject(hdc, OBJ_FONT);
297 298 299 300 301 302 303 304 305
    if(!hfont)
        return GenericError;

    if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
        return GenericError;

    return GdipCreateFontFromLogfontW(hdc, &lfw, font);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/*******************************************************************************
 * GdipGetFamily [GDIPLUS.@]
 *
 * Returns the FontFamily for the specified Font
 *
 * PARAMS
 *  font    [I] Font to request from
 *  family  [O] Resulting FontFamily object
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: An element of GpStatus
 */
GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
{
321
    TRACE("%p %p\n", font, family);
322

323 324 325
    if (!(font && family))
        return InvalidParameter;

326
    return GdipCloneFontFamily(font->family, family);
327 328
}

329 330 331 332 333
static REAL get_font_size(const GpFont *font)
{
    return font->emSize;
}

334 335 336 337 338 339 340 341 342 343 344
/******************************************************************************
 * GdipGetFontSize [GDIPLUS.@]
 *
 * Returns the size of the font in Units
 *
 * PARAMS
 *  *font       [I] The font to retrieve size from
 *  *size       [O] Pointer to hold retrieved value
 *
 * RETURNS
 *  SUCCESS: Ok
345
 *  FAILURE: InvalidParameter (font or size was NULL)
346 347 348 349 350 351
 *
 * NOTES
 *  Size returned is actually emSize -- not internal size used for drawing.
 */
GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
{
352 353
    TRACE("(%p, %p)\n", font, size);

354 355
    if (!(font && size)) return InvalidParameter;

356
    *size = get_font_size(font);
357
    TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
358 359 360 361

    return Ok;
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
static INT get_font_style(const GpFont *font)
{
    INT style;

    if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
        style = FontStyleBold;
    else
        style = FontStyleRegular;
    if (font->otm.otmTextMetrics.tmItalic)
        style |= FontStyleItalic;
    if (font->otm.otmTextMetrics.tmUnderlined)
        style |= FontStyleUnderline;
    if (font->otm.otmTextMetrics.tmStruckOut)
        style |= FontStyleStrikeout;

    return style;
}

380 381 382 383 384 385 386 387 388 389 390 391 392
/*******************************************************************************
 * GdipGetFontStyle [GDIPLUS.@]
 *
 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
 *
 * PARAMS
 *  font    [I] font to request from
 *  style   [O] resulting pointer to a FontStyle enumeration
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter
 */
393 394
GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
{
395 396 397 398 399
    TRACE("%p %p\n", font, style);

    if (!(font && style))
        return InvalidParameter;

400 401
    *style = get_font_style(font);
    TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
402

403
    return Ok;
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417 418
/*******************************************************************************
 * GdipGetFontUnit  [GDIPLUS.@]
 *
 * PARAMS
 *  font    [I] Font to retrieve from
 *  unit    [O] Return value
 *
 * RETURNS
 *  FAILURE: font or unit was NULL
 *  OK: otherwise
 */
GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
{
419 420
    TRACE("(%p, %p)\n", font, unit);

421 422 423
    if (!(font && unit)) return InvalidParameter;

    *unit = font->unit;
424
    TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
425 426 427 428

    return Ok;
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/*******************************************************************************
 * GdipGetLogFontA [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
    LOGFONTA *lfa)
{
    GpStatus status;
    LOGFONTW lfw;

    TRACE("(%p, %p, %p)\n", font, graphics, lfa);

    status = GdipGetLogFontW(font, graphics, &lfw);
    if(status != Ok)
        return status;

    memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );

446
    if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
447 448 449 450 451
        return GenericError;

    return Ok;
}

452 453 454 455
/*******************************************************************************
 * GdipGetLogFontW [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
456
{
457
    REAL angle, rel_height, height;
458
    GpMatrix matrix;
459
    GpPointF pt[3];
460

461 462 463 464 465
    TRACE("(%p, %p, %p)\n", font, graphics, lf);

    if (!font || !graphics || !lf)
        return InvalidParameter;

466
    matrix = graphics->worldtrans;
467

468
    if (font->unit == UnitPixel || font->unit == UnitWorld)
469
    {
470
        height = units_to_pixels(font->emSize, graphics->unit, graphics->yres, graphics->printer_display);
471
        if (graphics->unit != UnitDisplay)
472
            GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
473 474 475 476
    }
    else
    {
        if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
477
            height = units_to_pixels(font->emSize, font->unit, graphics->xres, graphics->printer_display);
478
        else
479
            height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
480 481
    }

482 483 484 485 486 487
    pt[0].X = 0.0;
    pt[0].Y = 0.0;
    pt[1].X = 1.0;
    pt[1].Y = 0.0;
    pt[2].X = 0.0;
    pt[2].Y = 1.0;
488
    GdipTransformMatrixPoints(&matrix, pt, 3);
489 490 491 492 493
    angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
    rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
                      (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));

    lf->lfHeight = -gdip_round(height * rel_height);
494
    lf->lfWidth = 0;
495 496 497 498 499 500
    lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
    if (lf->lfEscapement < 0)
    {
        lf->lfEscapement += 3600;
        lf->lfOrientation += 3600;
    }
501 502 503 504 505 506 507 508 509
    lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
    lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
    lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
    lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
    lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
    lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
    lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lf->lfQuality = DEFAULT_QUALITY;
    lf->lfPitchAndFamily = 0;
510
    lstrcpyW(lf->lfFaceName, font->family->FamilyName);
511

512
    TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
513 514 515

    return Ok;
}
516

517 518 519
/*******************************************************************************
 * GdipCloneFont [GDIPLUS.@]
 */
520 521
GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
{
522 523
    TRACE("(%p, %p)\n", font, cloneFont);

524 525 526
    if(!font || !cloneFont)
        return InvalidParameter;

527
    *cloneFont = heap_alloc_zero(sizeof(GpFont));
528 529 530
    if(!*cloneFont)    return OutOfMemory;

    **cloneFont = *font;
531
    return Ok;
532
}
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*******************************************************************************
 * GdipGetFontHeight [GDIPLUS.@]
 * PARAMS
 *  font        [I] Font to retrieve height from
 *  graphics    [I] The current graphics context
 *  height      [O] Resulting height
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: Another element of GpStatus
 *
 * NOTES
 *  Forwards to GdipGetFontHeightGivenDPI
 */
GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
        GDIPCONST GpGraphics *graphics, REAL *height)
{
    REAL dpi;
551
    GpStatus stat;
552
    REAL font_height;
553 554 555

    TRACE("%p %p %p\n", font, graphics, height);

556 557
    if (!font || !height) return InvalidParameter;

558 559 560 561
    stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
    if (stat != Ok) return stat;

    if (!graphics)
562
    {
563 564 565 566
        *height = font_height;
        TRACE("%s,%d => %f\n",
              debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
        return Ok;
567
    }
568

569 570 571
    stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
    if (stat != Ok) return stat;

572
    *height = pixels_to_units(font_height, graphics->unit, dpi, graphics->printer_display);
573 574 575 576

    TRACE("%s,%d(unit %d) => %f\n",
          debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
    return Ok;
577 578
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
/*******************************************************************************
 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
 * PARAMS
 *  font        [I] Font to retrieve DPI from
 *  dpi         [I] DPI to assume
 *  height      [O] Return value
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter if font or height is NULL
 *
 * NOTES
 *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
 *  (for anything other than unit Pixel)
 */
GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
{
596 597 598
    GpStatus stat;
    INT style;
    UINT16 line_spacing, em_height;
599
    REAL font_size;
600 601

    if (!font || !height) return InvalidParameter;
602

603
    TRACE("%p (%s), %f, %p\n", font,
604
            debugstr_w(font->family->FamilyName), dpi, height);
605

606
    font_size = units_to_pixels(get_font_size(font), font->unit, dpi, FALSE);
607
    style = get_font_style(font);
608 609 610 611
    stat = GdipGetLineSpacing(font->family, style, &line_spacing);
    if (stat != Ok) return stat;
    stat = GdipGetEmHeight(font->family, style, &em_height);
    if (stat != Ok) return stat;
612

613
    *height = (REAL)line_spacing * font_size / (REAL)em_height;
614

615 616
    TRACE("%s,%d => %f\n",
          debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
617

618
    return Ok;
619 620
}

621 622 623 624 625 626
/***********************************************************************
 * Borrowed from GDI32:
 *
 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
 *     We have to use other types because of the FONTENUMPROCW definition.
 */
627 628 629
static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
                            const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
{
630 631 632
    const ENUMLOGFONTW *elfW = (const ENUMLOGFONTW *)elf;
    LOGFONTW *lf = (LOGFONTW *)lParam;

633
    if (type & RASTER_FONTTYPE)
634 635
        return 1;

636 637 638
    *lf = *elf;
    /* replace substituted font name by a real one */
    lstrcpynW(lf->lfFaceName, elfW->elfFullName, LF_FACESIZE);
639 640 641
    return 0;
}

642 643
struct font_metrics
{
644
    WCHAR facename[LF_FACESIZE];
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 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
    UINT16 em_height, ascent, descent, line_spacing; /* in font units */
    int dpi;
};

static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
{
    OUTLINETEXTMETRICW otm;
    TT_OS2_V2 tt_os2;
    TT_HHEA tt_hori;
    LONG size;
    UINT16 line_gap;

    otm.otmSize = sizeof(otm);
    if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;

    fm->em_height = otm.otmEMSquare;
    fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);

    memset(&tt_hori, 0, sizeof(tt_hori));
    if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
    {
        fm->ascent = GET_BE_WORD(tt_hori.Ascender);
        fm->descent = -GET_BE_WORD(tt_hori.Descender);
        TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
        line_gap = GET_BE_WORD(tt_hori.LineGap);
        fm->line_spacing = fm->ascent + fm->descent + line_gap;
        TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
        if (fm->ascent + fm->descent != 0) return TRUE;
    }

    size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
    if (size == GDI_ERROR) return FALSE;

    if (size > sizeof(tt_os2)) size = sizeof(tt_os2);

    memset(&tt_os2, 0, sizeof(tt_os2));
    if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;

    fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
    fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
    TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
    if (fm->ascent + fm->descent == 0)
    {
        fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
        fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
        TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
    }
    line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
    fm->line_spacing = fm->ascent + fm->descent + line_gap;
    TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
    return TRUE;
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/*******************************************************************************
 * GdipCreateFontFamilyFromName [GDIPLUS.@]
 *
 * Creates a font family object based on a supplied name
 *
 * PARAMS
 *  name               [I] Name of the font
 *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
 *  FontFamily         [O] Pointer to the resulting FontFamily object
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
 *  FAILURE: Invalid parameter if FontFamily or name is NULL
 *
 * NOTES
 *   If fontCollection is NULL then the object is not part of any collection
 *
 */

GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
719 720
                                        GpFontCollection *collection,
                                        GpFontFamily **family)
721
{
722 723 724 725
    HDC hdc;
    LOGFONTW lf;
    GpStatus status;
    int i;
726

727
    TRACE("%s, %p %p\n", debugstr_w(name), collection, family);
728

729
    if (!name || !family)
730 731
        return InvalidParameter;

732 733 734 735 736
    if (!collection)
    {
        status = GdipNewInstalledFontCollection(&collection);
        if (status != Ok) return status;
    }
737

738
    status = FontFamilyNotFound;
739

740
    hdc = CreateCompatibleDC(0);
741

742 743 744 745 746 747
    if (!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
    {
        for (i = 0; i < collection->count; i++)
        {
            if (!wcsicmp(lf.lfFaceName, collection->FontFamilies[i]->FamilyName))
            {
748
                status = GdipCloneFontFamily(collection->FontFamilies[i], family);
749 750 751 752 753
                TRACE("<-- %p\n", *family);
                break;
            }
        }
    }
754

755 756
    DeleteDC(hdc);
    return status;
757 758
}

759 760 761 762 763 764 765 766 767 768 769 770
/*******************************************************************************
 * GdipCloneFontFamily [GDIPLUS.@]
 *
 * Creates a deep copy of a Font Family object
 *
 * PARAMS
 *  FontFamily          [I] Font to clone
 *  clonedFontFamily    [O] The resulting cloned font
 *
 * RETURNS
 *  SUCCESS: Ok
 */
771
GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily *family, GpFontFamily **clone)
772
{
773 774
    if (!family || !clone)
        return InvalidParameter;
775

776
    TRACE("%p (%s), %p\n", family, debugstr_w(family->FamilyName), clone);
777

778
    *clone = family;
779 780 781 782

    if (!family->installed)
        InterlockedIncrement(&family->ref);

783
    return Ok;
784 785
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
/*******************************************************************************
 * GdipGetFamilyName [GDIPLUS.@]
 *
 * Returns the family name into name
 *
 * PARAMS
 *  *family     [I] Family to retrieve from
 *  *name       [O] WCHARS of the family name
 *  LANGID      [I] charset
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter if family is NULL
 *
 * NOTES
801
 *   If name is NULL, XP and Vista crash but not Windows 7+
802 803 804 805
 */
GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
                                       WCHAR *name, LANGID language)
{
806 807
    static int lang_fixme;

808 809
    TRACE("%p, %p, %d\n", family, name, language);

810 811 812
    if (family == NULL)
         return InvalidParameter;

813 814
    if (name == NULL)
         return Ok;
815

816
    if (language != LANG_NEUTRAL && !lang_fixme++)
817 818 819 820 821 822 823
        FIXME("No support for handling of multiple languages!\n");

    lstrcpynW (name, family->FamilyName, LF_FACESIZE);

    return Ok;
}

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

/*****************************************************************************
 * GdipDeleteFontFamily [GDIPLUS.@]
 *
 * Removes the specified FontFamily
 *
 * PARAMS
 *  *FontFamily         [I] The family to delete
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter if FontFamily is NULL.
 *
 */
GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
{
    if (!FontFamily)
        return InvalidParameter;

843 844 845 846 847
    if (!FontFamily->installed && !InterlockedDecrement(&FontFamily->ref))
    {
        heap_free(FontFamily);
    }

848 849
    return Ok;
}
850

851 852
GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
        INT style, UINT16* CellAscent)
853 854 855
{
    if (!(family && CellAscent)) return InvalidParameter;

856 857
    *CellAscent = family->ascent;
    TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
858

859
    return Ok;
860 861
}

862 863
GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
        INT style, UINT16* CellDescent)
864
{
865 866
    TRACE("(%p, %d, %p)\n", family, style, CellDescent);

867 868
    if (!(family && CellDescent)) return InvalidParameter;

869 870
    *CellDescent = family->descent;
    TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
871

872
    return Ok;
873 874
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
/*******************************************************************************
 * GdipGetEmHeight [GDIPLUS.@]
 *
 * Gets the height of the specified family in EmHeights
 *
 * PARAMS
 *  family      [I] Family to retrieve from
 *  style       [I] (optional) style
 *  EmHeight    [O] return value
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter
 */
GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
{
    if (!(family && EmHeight)) return InvalidParameter;

893
    TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
894

895 896
    *EmHeight = family->em_height;
    TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
897 898

    return Ok;
899 900 901
}


902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
/*******************************************************************************
 * GdipGetLineSpacing [GDIPLUS.@]
 *
 * Returns the line spacing in design units
 *
 * PARAMS
 *  family      [I] Family to retrieve from
 *  style       [I] (Optional) font style
 *  LineSpacing [O] Return value
 *
 * RETURNS
 *  SUCCESS: Ok
 *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
 */
GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
        INT style, UINT16* LineSpacing)
{
919
    TRACE("%p, %d, %p\n", family, style, LineSpacing);
920

921 922
    if (!(family && LineSpacing))
        return InvalidParameter;
923

924 925
    if (style) FIXME("ignoring style\n");

926 927
    *LineSpacing = family->line_spacing;
    TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
928 929

    return Ok;
930 931
}

932 933 934
static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
                            const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
{
935
    INT fontstyle = FontStyleRegular;
936 937 938 939 940 941 942 943 944 945 946

    if (!ntm) return 1;

    if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
    if (ntm->tmItalic) fontstyle |= FontStyleItalic;
    if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
    if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;

    return (INT)lParam != fontstyle;
}

947 948 949
GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
        INT style, BOOL* IsStyleAvailable)
{
950 951 952
    HDC hdc;

    TRACE("%p %d %p\n", family, style, IsStyleAvailable);
953 954 955 956

    if (!(family && IsStyleAvailable))
        return InvalidParameter;

957 958
    *IsStyleAvailable = FALSE;

959
    hdc = CreateCompatibleDC(0);
960 961 962 963

    if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
        *IsStyleAvailable = TRUE;

964
    DeleteDC(hdc);
965 966

    return Ok;
967 968
}

969 970 971 972 973 974 975 976 977 978 979 980 981 982
/*****************************************************************************
 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
 *
 * Obtains a serif family (Courier New on Windows)
 *
 * PARAMS
 *  **nativeFamily         [I] Where the font will be stored
 *
 * RETURNS
 *  InvalidParameter if nativeFamily is NULL.
 *  Ok otherwise.
 */
GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
{
983
    GpStatus stat;
984 985 986

    if (nativeFamily == NULL) return InvalidParameter;

987
    stat = GdipCreateFontFamilyFromName(L"Courier New", NULL, nativeFamily);
988 989

    if (stat == FontFamilyNotFound)
990
        stat = GdipCreateFontFamilyFromName(L"Liberation Mono", NULL, nativeFamily);
991 992 993 994 995

    if (stat == FontFamilyNotFound)
        ERR("Missing 'Courier New' font\n");

    return stat;
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
}

/*****************************************************************************
 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
 *
 * Obtains a serif family (Times New Roman on Windows)
 *
 * PARAMS
 *  **nativeFamily         [I] Where the font will be stored
 *
 * RETURNS
 *  InvalidParameter if nativeFamily is NULL.
 *  Ok otherwise.
 */
GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
{
1012
    GpStatus stat;
1013

1014 1015
    TRACE("(%p)\n", nativeFamily);

1016 1017
    if (nativeFamily == NULL) return InvalidParameter;

1018
    stat = GdipCreateFontFamilyFromName(L"Times New Roman", NULL, nativeFamily);
1019 1020

    if (stat == FontFamilyNotFound)
1021
        stat = GdipCreateFontFamilyFromName(L"Liberation Serif", NULL, nativeFamily);
1022 1023 1024 1025 1026

    if (stat == FontFamilyNotFound)
        ERR("Missing 'Times New Roman' font\n");

    return stat;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

/*****************************************************************************
 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
 *
 * Obtains a serif family (Microsoft Sans Serif on Windows)
 *
 * PARAMS
 *  **nativeFamily         [I] Where the font will be stored
 *
 * RETURNS
 *  InvalidParameter if nativeFamily is NULL.
 *  Ok otherwise.
 */
GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
{
1043
    GpStatus stat;
1044

1045 1046
    TRACE("(%p)\n", nativeFamily);

1047 1048
    if (nativeFamily == NULL) return InvalidParameter;

1049
    stat = GdipCreateFontFamilyFromName(L"Microsoft Sans Serif", NULL, nativeFamily);
1050 1051 1052

    if (stat == FontFamilyNotFound)
        /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1053
        stat = GdipCreateFontFamilyFromName(L"Tahoma", NULL, nativeFamily);
1054 1055

    return stat;
1056
}
1057

1058
/*****************************************************************************
1059
 * GdipNewPrivateFontCollection [GDIPLUS.@]
1060
 */
1061 1062
GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
{
1063
    TRACE("%p\n", fontCollection);
1064 1065 1066 1067

    if (!fontCollection)
        return InvalidParameter;

1068
    *fontCollection = heap_alloc_zero(sizeof(GpFontCollection));
1069 1070 1071 1072
    if (!*fontCollection) return OutOfMemory;

    (*fontCollection)->FontFamilies = NULL;
    (*fontCollection)->count = 0;
1073
    (*fontCollection)->allocated = 0;
1074 1075 1076

    TRACE("<-- %p\n", *fontCollection);

1077
    return Ok;
1078
}
1079

1080 1081 1082
/*****************************************************************************
 * GdipDeletePrivateFontCollection [GDIPLUS.@]
 */
1083 1084
GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
{
1085 1086 1087
    INT i;

    TRACE("%p\n", fontCollection);
1088 1089 1090 1091

    if (!fontCollection)
        return InvalidParameter;

1092
    for (i = 0; i < (*fontCollection)->count; i++) GdipDeleteFontFamily((*fontCollection)->FontFamilies[i]);
1093
    heap_free((*fontCollection)->FontFamilies);
1094
    heap_free(*fontCollection);
1095 1096

    return Ok;
1097
}
1098

1099 1100 1101
/*****************************************************************************
 * GdipPrivateAddFontFile [GDIPLUS.@]
 */
1102
GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1103
{
1104 1105 1106 1107 1108 1109 1110 1111
    HANDLE file, mapping;
    LARGE_INTEGER size;
    void *mem;
    GpStatus status;

    TRACE("%p, %s\n", collection, debugstr_w(name));

    if (!collection || !name) return InvalidParameter;
1112

1113 1114 1115 1116 1117 1118
    file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (file == INVALID_HANDLE_VALUE) return InvalidParameter;

    if (!GetFileSizeEx(file, &size) || size.u.HighPart)
    {
        CloseHandle(file);
1119
        return InvalidParameter;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    }

    mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
    CloseHandle(file);
    if (!mapping) return InvalidParameter;

    mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
    CloseHandle(mapping);
    if (!mem) return InvalidParameter;

    /* GdipPrivateAddMemoryFont creates a copy of the memory block */
    status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
    UnmapViewOfFile(mem);
1133

1134
    return status;
1135
}
1136

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
#define TT_PLATFORM_APPLE_UNICODE   0
#define TT_PLATFORM_MACINTOSH       1
#define TT_PLATFORM_MICROSOFT       3

#define TT_APPLE_ID_DEFAULT     0
#define TT_APPLE_ID_ISO_10646   2
#define TT_APPLE_ID_UNICODE_2_0 3

#define TT_MS_ID_SYMBOL_CS  0
#define TT_MS_ID_UNICODE_CS 1

#define TT_MAC_ID_SIMPLIFIED_CHINESE    25
1149 1150 1151

#define NAME_ID_FULL_FONT_NAME  4

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
typedef struct {
    USHORT major_version;
    USHORT minor_version;
    USHORT tables_no;
    USHORT search_range;
    USHORT entry_selector;
    USHORT range_shift;
} tt_header;

typedef struct {
    char tag[4];        /* table name */
    ULONG check_sum;    /* Check sum */
    ULONG offset;       /* Offset from beginning of file */
    ULONG length;       /* length of the table in bytes */
} tt_table_directory;

typedef struct {
    USHORT format;          /* format selector. Always 0 */
    USHORT count;           /* Name Records count */
    USHORT string_offset;   /* Offset for strings storage, * from start of the table */
} tt_name_table;

typedef struct {
    USHORT platform_id;
    USHORT encoding_id;
    USHORT language_id;
    USHORT name_id;
    USHORT length;
    USHORT offset;      /* from start of storage area */
} tt_name_record;

/* Copied from gdi32/freetype.c */

static const LANGID mac_langid_table[] =
{
    MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_ENGLISH */
    MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_FRENCH */
    MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_GERMAN */
    MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_ITALIAN */
    MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_DUTCH */
    MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_SWEDISH */
    MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_SPANISH */
    MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_DANISH */
    MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_PORTUGUESE */
    MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_NORWEGIAN */
    MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_HEBREW */
    MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_JAPANESE */
    MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_ARABIC */
    MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_FINNISH */
    MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_GREEK */
    MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_ICELANDIC */
    MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_MALTESE */
    MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_TURKISH */
    MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_CROATIAN */
    MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT),    /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
    MAKELANGID(LANG_URDU,SUBLANG_DEFAULT),                   /* TT_MAC_LANGID_URDU */
    MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_HINDI */
    MAKELANGID(LANG_THAI,SUBLANG_DEFAULT),                   /* TT_MAC_LANGID_THAI */
    MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_KOREAN */
    MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_LITHUANIAN */
    MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_POLISH */
    MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_HUNGARIAN */
    MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_ESTONIAN */
    MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_LETTISH */
    MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT),                   /* TT_MAC_LANGID_SAAMISK */
    MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_FAEROESE */
    MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_FARSI */
    MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_RUSSIAN */
    MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT),     /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
    MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN),            /* TT_MAC_LANGID_FLEMISH */
    MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_IRISH */
    MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_ALBANIAN */
    MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_ROMANIAN */
    MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_CZECH */
    MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_SLOVAK */
    MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_SLOVENIAN */
    0,                                                       /* TT_MAC_LANGID_YIDDISH */
    MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_SERBIAN */
    MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_MACEDONIAN */
    MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_BULGARIAN */
    MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_UKRAINIAN */
    MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_BYELORUSSIAN */
    MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_UZBEK */
    MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_KAZAKH */
    MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC),           /* TT_MAC_LANGID_AZERBAIJANI */
    0,                                                       /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
    MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_ARMENIAN */
    MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_GEORGIAN */
    0,                                                       /* TT_MAC_LANGID_MOLDAVIAN */
    MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_KIRGHIZ */
    MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_TAJIKI */
    MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_TURKMEN */
    MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_MONGOLIAN */
    MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
    MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_PASHTO */
    0,                                                       /* TT_MAC_LANGID_KURDISH */
    MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_KASHMIRI */
    MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_SINDHI */
    MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_TIBETAN */
    MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_NEPALI */
    MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_SANSKRIT */
    MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_MARATHI */
    MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_BENGALI */
    MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_ASSAMESE */
    MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_GUJARATI */
    MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_PUNJABI */
    MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_ORIYA */
    MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_MALAYALAM */
    MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_KANNADA */
    MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_TAMIL */
    MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_TELUGU */
    MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_SINHALESE */
    0,                                                       /* TT_MAC_LANGID_BURMESE */
    MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_KHMER */
    MAKELANGID(LANG_LAO,SUBLANG_DEFAULT),                    /* TT_MAC_LANGID_LAO */
    MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_VIETNAMESE */
    MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT),             /* TT_MAC_LANGID_INDONESIAN */
    0,                                                       /* TT_MAC_LANGID_TAGALOG */
    MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
    0,                                                       /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
    MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_AMHARIC */
    MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_TIGRINYA */
    0,                                                       /* TT_MAC_LANGID_GALLA */
    0,                                                       /* TT_MAC_LANGID_SOMALI */
    MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_SWAHILI */
    0,                                                       /* TT_MAC_LANGID_RUANDA */
    0,                                                       /* TT_MAC_LANGID_RUNDI */
    0,                                                       /* TT_MAC_LANGID_CHEWA */
    MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_MALAGASY */
    MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_ESPERANTO */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* 95-111 */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,          /* 112-127 */
    MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_WELSH */
    MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_BASQUE */
    MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_CATALAN */
    0,                                                       /* TT_MAC_LANGID_LATIN */
    MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT),                /* TT_MAC_LANGID_QUECHUA */
    0,                                                       /* TT_MAC_LANGID_GUARANI */
    0,                                                       /* TT_MAC_LANGID_AYMARA */
    MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT),                  /* TT_MAC_LANGID_TATAR */
    MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_UIGHUR */
    0,                                                       /* TT_MAC_LANGID_DZONGKHA */
    0,                                                       /* TT_MAC_LANGID_JAVANESE */
    0,                                                       /* TT_MAC_LANGID_SUNDANESE */
    MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT),               /* TT_MAC_LANGID_GALICIAN */
    MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_AFRIKAANS */
    MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT),                 /* TT_MAC_LANGID_BRETON */
    MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT),              /* TT_MAC_LANGID_INUKTITUT */
    MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT),        /* TT_MAC_LANGID_SCOTTISH_GAELIC */
    MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT),            /* TT_MAC_LANGID_MANX_GAELIC */
    MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND),            /* TT_MAC_LANGID_IRISH_GAELIC */
    0,                                                       /* TT_MAC_LANGID_TONGAN */
    0,                                                       /* TT_MAC_LANGID_GREEK_POLYTONIC */
    MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT),            /* TT_MAC_LANGID_GREELANDIC */
    MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN),              /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
};
1308

1309 1310 1311 1312 1313 1314
static inline WORD get_mac_code_page( const tt_name_record *name )
{
    WORD encoding_id = GET_BE_WORD(name->encoding_id);
    if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008;  /* special case */
    return 10000 + encoding_id;
}
1315

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
static int match_name_table_language( const tt_name_record *name, LANGID lang )
{
    LANGID name_lang;

    switch (GET_BE_WORD(name->platform_id))
    {
    case TT_PLATFORM_MICROSOFT:
        switch (GET_BE_WORD(name->encoding_id))
        {
        case TT_MS_ID_UNICODE_CS:
        case TT_MS_ID_SYMBOL_CS:
            name_lang = GET_BE_WORD(name->language_id);
            break;
        default:
            return 0;
        }
        break;
    case TT_PLATFORM_MACINTOSH:
        if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
        name_lang = GET_BE_WORD(name->language_id);
1336
        if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1337 1338 1339 1340 1341 1342 1343 1344 1345
        name_lang = mac_langid_table[name_lang];
        break;
    case TT_PLATFORM_APPLE_UNICODE:
        switch (GET_BE_WORD(name->encoding_id))
        {
        case TT_APPLE_ID_DEFAULT:
        case TT_APPLE_ID_ISO_10646:
        case TT_APPLE_ID_UNICODE_2_0:
            name_lang = GET_BE_WORD(name->language_id);
1346
            if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
            name_lang = mac_langid_table[name_lang];
            break;
        default:
            return 0;
        }
        break;
    default:
        return 0;
    }
    if (name_lang == lang) return 3;
    if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
    if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
    return 0;
}

1362
static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data )
1363 1364 1365
{
    WORD name_len = GET_BE_WORD(name->length);
    WORD codepage;
1366 1367
    WCHAR *ret;
    int len;
1368 1369 1370 1371 1372

    switch (GET_BE_WORD(name->platform_id))
    {
    case TT_PLATFORM_APPLE_UNICODE:
    case TT_PLATFORM_MICROSOFT:
1373
        ret = heap_alloc((name_len / 2 + 1) * sizeof(WCHAR));
1374 1375 1376 1377 1378 1379
        for (len = 0; len < name_len / 2; len++)
            ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
        ret[len] = 0;
        return ret;
    case TT_PLATFORM_MACINTOSH:
        codepage = get_mac_code_page( name );
1380
        len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, NULL, 0 ) + 1;
1381 1382
        if (!len)
            return NULL;
1383 1384
        ret = heap_alloc(len * sizeof(WCHAR));
        len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len - 1 );
1385 1386 1387 1388 1389 1390
        ret[len] = 0;
        return ret;
    }
    return NULL;
}

1391
static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id )
1392
{
1393 1394 1395 1396 1397 1398 1399 1400
    LANGID lang = GetSystemDefaultLangID();
    const tt_header *header;
    const tt_name_table *name_table;
    const tt_name_record *name_record;
    DWORD pos, ofs, count;
    int i, res, best_lang = 0, best_index = -1;

    if (sizeof(tt_header) > size)
1401
        return NULL;
1402 1403
    header = (const tt_header*)mem;
    count = GET_BE_WORD(header->tables_no);
1404

1405
    if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1406 1407
        return NULL;

1408 1409
    pos = sizeof(*header);
    for (i = 0; i < count; i++)
1410
    {
1411 1412 1413
        const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
        pos += sizeof(*table_directory);
        if (memcmp(table_directory->tag, "name", 4) == 0)
1414
        {
1415
            ofs = GET_BE_DWORD(table_directory->offset);
1416 1417 1418
            break;
        }
    }
1419
    if (i >= count)
1420 1421
        return NULL;

1422 1423 1424
    if (ofs >= size)
        return NULL;
    pos = ofs + sizeof(*name_table);
1425 1426
    if (pos > size)
        return NULL;
1427 1428 1429 1430 1431
    name_table = (const tt_name_table*)&mem[ofs];
    count =  GET_BE_WORD(name_table->count);
    if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
    ofs += GET_BE_WORD(name_table->string_offset);
    for (i=0; i<count; i++)
1432
    {
1433 1434
        name_record = (const tt_name_record*)&mem[pos];
        pos += sizeof(*name_record);
1435 1436 1437
        if (pos > size)
            return NULL;

1438 1439 1440 1441 1442 1443
        if (GET_BE_WORD(name_record->name_id) != id) continue;
        if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
        if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;

        res = match_name_table_language( name_record, lang );
        if (res > best_lang)
1444
        {
1445 1446
            best_lang = res;
            best_index = i;
1447 1448
        }
    }
1449 1450 1451

    if (best_lang)
    {
1452
        WCHAR *ret;
1453
        name_record = (const tt_name_record*)(name_table + 1) + best_index;
1454
        ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset) );
1455 1456 1457 1458
        TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
                GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
        return ret;
    }
1459 1460 1461
    return NULL;
}

1462 1463 1464
struct add_font_param
{
    GpFontCollection *collection;
1465
    BOOL is_system;
1466
    GpStatus stat;
1467
    HDC hdc;
1468 1469
};

1470 1471
static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);

1472 1473 1474 1475 1476 1477
/*****************************************************************************
 * GdipPrivateAddMemoryFont [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
        GDIPCONST void* memory, INT length)
{
1478
    WCHAR *name;
1479 1480
    DWORD count = 0;
    HANDLE font;
1481
    GpStatus ret = Ok;
1482
    TRACE("%p, %p, %d\n", fontCollection, memory, length);
1483

1484
    if (!fontCollection || !memory || !length)
1485 1486
        return InvalidParameter;

1487
    name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME);
1488 1489 1490 1491 1492 1493
    if (!name)
        return OutOfMemory;

    font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
    TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
    if (!font || !count)
1494 1495
        ret = InvalidParameter;
    else
1496
    {
1497
        struct add_font_param param;
1498 1499
        LOGFONTW lfw;

1500
        param.hdc = CreateCompatibleDC(0);
1501

1502 1503 1504 1505
        /* Truncate name if necessary, GDI32 can't deal with long names */
        if(lstrlenW(name) > LF_FACESIZE - 1)
            name[LF_FACESIZE - 1] = 0;

1506 1507 1508 1509
        lfw.lfCharSet = DEFAULT_CHARSET;
        lstrcpyW(lfw.lfFaceName, name);
        lfw.lfPitchAndFamily = 0;

1510
        param.collection = fontCollection;
1511
        param.is_system = FALSE;
1512
        if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1513
            ret = param.stat;
1514

1515
        DeleteDC(param.hdc);
1516
    }
1517 1518
    heap_free(name);
    return ret;
1519 1520
}

1521 1522 1523
/*****************************************************************************
 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
 */
1524 1525 1526
GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
        GpFontCollection* fontCollection, INT* numFound)
{
1527
    TRACE("%p, %p\n", fontCollection, numFound);
1528 1529 1530 1531

    if (!(fontCollection && numFound))
        return InvalidParameter;

1532 1533
    *numFound = fontCollection->count;
    return Ok;
1534
}
1535

1536 1537 1538
/*****************************************************************************
 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
 */
1539 1540 1541 1542
GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
        GpFontCollection* fontCollection, INT numSought,
        GpFontFamily* gpfamilies[], INT* numFound)
{
1543 1544 1545
    INT i;

    TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1546 1547 1548 1549

    if (!(fontCollection && gpfamilies && numFound))
        return InvalidParameter;

1550 1551
    memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);

1552
    for (i = 0; i < numSought && i < fontCollection->count; i++)
1553
    {
1554
        /* caller is responsible for cloning these if it keeps references */
1555
        gpfamilies[i] = fontCollection->FontFamilies[i];
1556
    }
1557

1558
    *numFound = i;
1559

1560
    return Ok;
1561
}
1562

1563 1564
void free_installed_fonts(void)
{
1565 1566 1567 1568
    INT i;

    for (i = 0; i < installedFontCollection.count; i++)
        heap_free(installedFontCollection.FontFamilies[i]);
1569
    heap_free(installedFontCollection.FontFamilies);
1570

1571 1572 1573 1574 1575 1576 1577
    installedFontCollection.FontFamilies = NULL;
    installedFontCollection.allocated = 0;
}

static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
        DWORD type, LPARAM lParam)
{
1578 1579
    struct add_font_param *param = (struct add_font_param *)lParam;
    GpFontCollection *fonts = param->collection;
1580 1581 1582
    GpFontFamily *family;
    HFONT hfont, old_hfont;
    struct font_metrics fm;
1583 1584
    int i;

1585 1586
    param->stat = Ok;

1587 1588 1589
    if (type == RASTER_FONTTYPE)
        return 1;

1590 1591 1592 1593
    /* skip rotated fonts */
    if (lfw->lfFaceName[0] == '@')
        return 1;

1594
    if (fonts->count && wcsicmp(lfw->lfFaceName, fonts->FontFamilies[fonts->count-1]->FamilyName) == 0)
1595
        return 1;
1596 1597 1598 1599

    if (fonts->allocated == fonts->count)
    {
        INT new_alloc_count = fonts->allocated+50;
1600
        GpFontFamily** new_family_list = heap_alloc(new_alloc_count*sizeof(void*));
1601 1602

        if (!new_family_list)
1603 1604
        {
            param->stat = OutOfMemory;
1605
            return 0;
1606
        }
1607 1608

        memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1609
        heap_free(fonts->FontFamilies);
1610 1611 1612 1613
        fonts->FontFamilies = new_family_list;
        fonts->allocated = new_alloc_count;
    }

1614 1615
    family = heap_alloc(sizeof(*family));
    if (!family)
1616
    {
1617 1618
        if (param->is_system)
            return 1;
1619 1620

        param->stat = OutOfMemory;
1621
        return 0;
1622
    }
1623

1624 1625 1626
    /* skip duplicates */
    for (i=0; i<fonts->count; i++)
    {
1627
        if (wcsicmp(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1628
        {
1629
            heap_free(family);
1630 1631 1632 1633
            return 1;
        }
    }

1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
    hfont = CreateFontIndirectW(lfw);
    old_hfont = SelectObject(param->hdc, hfont);

    if (!get_font_metrics(param->hdc, &fm))
    {
        SelectObject(param->hdc, old_hfont);
        DeleteObject(hfont);

        heap_free(family);
        param->stat = OutOfMemory;
        return 0;
    }

    SelectObject(param->hdc, old_hfont);
    DeleteObject(hfont);

    family->em_height = fm.em_height;
    family->ascent = fm.ascent;
    family->descent = fm.descent;
    family->line_spacing = fm.line_spacing;
    family->dpi = fm.dpi;
1655 1656
    family->installed = param->is_system;
    family->ref = 1;
1657 1658 1659

    lstrcpyW(family->FamilyName, lfw->lfFaceName);

1660 1661
    fonts->FontFamilies[fonts->count++] = family;

1662 1663 1664
    return 1;
}

1665 1666 1667
GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
        GpFontCollection** fontCollection)
{
1668
    TRACE("(%p)\n",fontCollection);
1669 1670 1671 1672

    if (!fontCollection)
        return InvalidParameter;

1673
    EnterCriticalSection( &font_cs );
1674 1675
    if (installedFontCollection.count == 0)
    {
1676
        struct add_font_param param;
1677 1678
        LOGFONTW lfw;

1679
        param.hdc = CreateCompatibleDC(0);
1680 1681 1682 1683 1684

        lfw.lfCharSet = DEFAULT_CHARSET;
        lfw.lfFaceName[0] = 0;
        lfw.lfPitchAndFamily = 0;

1685
        param.collection = &installedFontCollection;
1686
        param.is_system = TRUE;
1687
        if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1688 1689
        {
            free_installed_fonts();
1690
            DeleteDC(param.hdc);
1691
            LeaveCriticalSection( &font_cs );
1692
            return param.stat;
1693 1694
        }

1695
        DeleteDC(param.hdc);
1696
    }
1697
    LeaveCriticalSection( &font_cs );
1698

1699 1700 1701
    *fontCollection = &installedFontCollection;

    return Ok;
1702
}