include: Assorted spelling fixes.
[wine] / dlls / gdiplus / font.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  * Copyright (C) 2012 Dmitry Timoshkov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
31
32 #include "objbase.h"
33
34 #include "gdiplus.h"
35 #include "gdiplus_private.h"
36
37 /* PANOSE is 10 bytes in size, need to pack the structure properly */
38 #include "pshpack2.h"
39 typedef struct
40 {
41     USHORT version;
42     SHORT xAvgCharWidth;
43     USHORT usWeightClass;
44     USHORT usWidthClass;
45     SHORT fsType;
46     SHORT ySubscriptXSize;
47     SHORT ySubscriptYSize;
48     SHORT ySubscriptXOffset;
49     SHORT ySubscriptYOffset;
50     SHORT ySuperscriptXSize;
51     SHORT ySuperscriptYSize;
52     SHORT ySuperscriptXOffset;
53     SHORT ySuperscriptYOffset;
54     SHORT yStrikeoutSize;
55     SHORT yStrikeoutPosition;
56     SHORT sFamilyClass;
57     PANOSE panose;
58     ULONG ulUnicodeRange1;
59     ULONG ulUnicodeRange2;
60     ULONG ulUnicodeRange3;
61     ULONG ulUnicodeRange4;
62     CHAR achVendID[4];
63     USHORT fsSelection;
64     USHORT usFirstCharIndex;
65     USHORT usLastCharIndex;
66     /* According to the Apple spec, original version didn't have the below fields,
67      * version numbers were taken from the OpenType spec.
68      */
69     /* version 0 (TrueType 1.5) */
70     USHORT sTypoAscender;
71     USHORT sTypoDescender;
72     USHORT sTypoLineGap;
73     USHORT usWinAscent;
74     USHORT usWinDescent;
75     /* version 1 (TrueType 1.66) */
76     ULONG ulCodePageRange1;
77     ULONG ulCodePageRange2;
78     /* version 2 (OpenType 1.2) */
79     SHORT sxHeight;
80     SHORT sCapHeight;
81     USHORT usDefaultChar;
82     USHORT usBreakChar;
83     USHORT usMaxContext;
84 } TT_OS2_V2;
85
86 typedef struct
87 {
88     ULONG Version;
89     SHORT Ascender;
90     SHORT Descender;
91     SHORT LineGap;
92     USHORT advanceWidthMax;
93     SHORT minLeftSideBearing;
94     SHORT minRightSideBearing;
95     SHORT xMaxExtent;
96     SHORT caretSlopeRise;
97     SHORT caretSlopeRun;
98     SHORT caretOffset;
99     SHORT reserved[4];
100     SHORT metricDataFormat;
101     USHORT numberOfHMetrics;
102 } TT_HHEA;
103 #include "poppack.h"
104
105 #ifdef WORDS_BIGENDIAN
106 #define GET_BE_WORD(x) (x)
107 #define GET_BE_DWORD(x) (x)
108 #else
109 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
110 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
111 #endif
112
113 #define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
114                     ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
115                     ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
116 #define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
117 #define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
118
119 static GpStatus clone_font_family(const GpFontFamily *, GpFontFamily **);
120
121 static GpFontCollection installedFontCollection = {0};
122
123 /*******************************************************************************
124  * GdipCreateFont [GDIPLUS.@]
125  *
126  * Create a new font based off of a FontFamily
127  *
128  * PARAMS
129  *  *fontFamily     [I] Family to base the font off of
130  *  emSize          [I] Size of the font
131  *  style           [I] Bitwise OR of FontStyle enumeration
132  *  unit            [I] Unit emSize is measured in
133  *  **font          [I] the resulting Font object
134  *
135  * RETURNS
136  *  SUCCESS: Ok
137  *  FAILURE: InvalidParameter if fontfamily or font is NULL.
138  *  FAILURE: FontFamilyNotFound if an invalid FontFamily is given
139  *
140  * NOTES
141  *  UnitDisplay is unsupported.
142  *  emSize is stored separately from lfHeight, to hold the fraction.
143  */
144 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
145                         REAL emSize, INT style, Unit unit, GpFont **font)
146 {
147     HFONT hfont;
148     OUTLINETEXTMETRICW otm;
149     LOGFONTW lfw;
150     HDC hdc;
151     GpStatus stat;
152     int ret;
153
154     if (!fontFamily || !font || emSize < 0.0)
155         return InvalidParameter;
156
157     TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
158             debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
159
160     memset(&lfw, 0, sizeof(lfw));
161
162     stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
163     if (stat != Ok) return stat;
164
165     lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
166     lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
167     lfw.lfItalic = style & FontStyleItalic;
168     lfw.lfUnderline = style & FontStyleUnderline;
169     lfw.lfStrikeOut = style & FontStyleStrikeout;
170
171     hfont = CreateFontIndirectW(&lfw);
172     hdc = CreateCompatibleDC(0);
173     SelectObject(hdc, hfont);
174     otm.otmSize = sizeof(otm);
175     ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
176     DeleteDC(hdc);
177     DeleteObject(hfont);
178
179     if (!ret) return NotTrueTypeFont;
180
181     *font = GdipAlloc(sizeof(GpFont));
182     if (!*font) return OutOfMemory;
183
184     (*font)->unit = unit;
185     (*font)->emSize = emSize;
186     (*font)->otm = otm;
187
188     stat = clone_font_family(fontFamily, &(*font)->family);
189     if (stat != Ok)
190     {
191         GdipFree(*font);
192         return stat;
193     }
194
195     TRACE("<-- %p\n", *font);
196
197     return Ok;
198 }
199
200 /*******************************************************************************
201  * GdipCreateFontFromLogfontW [GDIPLUS.@]
202  */
203 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
204     GDIPCONST LOGFONTW *logfont, GpFont **font)
205 {
206     HFONT hfont, oldfont;
207     OUTLINETEXTMETRICW otm;
208     WCHAR facename[LF_FACESIZE];
209     GpStatus stat;
210     int ret;
211
212     TRACE("(%p, %p, %p)\n", hdc, logfont, font);
213
214     if (!hdc || !logfont || !font)
215         return InvalidParameter;
216
217     hfont = CreateFontIndirectW(logfont);
218     oldfont = SelectObject(hdc, hfont);
219     otm.otmSize = sizeof(otm);
220     ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
221     GetTextFaceW(hdc, LF_FACESIZE, facename);
222     SelectObject(hdc, oldfont);
223     DeleteObject(hfont);
224
225     if (!ret) return NotTrueTypeFont;
226
227     *font = GdipAlloc(sizeof(GpFont));
228     if (!*font) return OutOfMemory;
229
230     (*font)->unit = UnitWorld;
231     (*font)->emSize = otm.otmTextMetrics.tmAscent;
232     (*font)->otm = otm;
233
234     stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
235     if (stat != Ok)
236     {
237         GdipFree(*font);
238         return NotTrueTypeFont;
239     }
240
241     TRACE("<-- %p\n", *font);
242
243     return Ok;
244 }
245
246 /*******************************************************************************
247  * GdipCreateFontFromLogfontA [GDIPLUS.@]
248  */
249 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
250     GDIPCONST LOGFONTA *lfa, GpFont **font)
251 {
252     LOGFONTW lfw;
253
254     TRACE("(%p, %p, %p)\n", hdc, lfa, font);
255
256     if(!lfa || !font)
257         return InvalidParameter;
258
259     memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
260
261     if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
262         return GenericError;
263
264     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
265 }
266
267 /*******************************************************************************
268  * GdipDeleteFont [GDIPLUS.@]
269  */
270 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
271 {
272     TRACE("(%p)\n", font);
273
274     if(!font)
275         return InvalidParameter;
276
277     GdipDeleteFontFamily(font->family);
278     GdipFree(font);
279
280     return Ok;
281 }
282
283 /*******************************************************************************
284  * GdipCreateFontFromDC [GDIPLUS.@]
285  */
286 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
287 {
288     HFONT hfont;
289     LOGFONTW lfw;
290
291     TRACE("(%p, %p)\n", hdc, font);
292
293     if(!font)
294         return InvalidParameter;
295
296     hfont = GetCurrentObject(hdc, OBJ_FONT);
297     if(!hfont)
298         return GenericError;
299
300     if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
301         return GenericError;
302
303     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
304 }
305
306 /*******************************************************************************
307  * GdipGetFamily [GDIPLUS.@]
308  *
309  * Returns the FontFamily for the specified Font
310  *
311  * PARAMS
312  *  font    [I] Font to request from
313  *  family  [O] Resulting FontFamily object
314  *
315  * RETURNS
316  *  SUCCESS: Ok
317  *  FAILURE: An element of GpStatus
318  */
319 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
320 {
321     TRACE("%p %p\n", font, family);
322
323     if (!(font && family))
324         return InvalidParameter;
325
326     return GdipCloneFontFamily(font->family, family);
327 }
328
329 static REAL get_font_size(const GpFont *font)
330 {
331     return font->emSize;
332 }
333
334 /******************************************************************************
335  * GdipGetFontSize [GDIPLUS.@]
336  *
337  * Returns the size of the font in Units
338  *
339  * PARAMS
340  *  *font       [I] The font to retrieve size from
341  *  *size       [O] Pointer to hold retrieved value
342  *
343  * RETURNS
344  *  SUCCESS: Ok
345  *  FAILURE: InvalidParameter (font or size was NULL)
346  *
347  * NOTES
348  *  Size returned is actually emSize -- not internal size used for drawing.
349  */
350 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
351 {
352     TRACE("(%p, %p)\n", font, size);
353
354     if (!(font && size)) return InvalidParameter;
355
356     *size = get_font_size(font);
357     TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
358
359     return Ok;
360 }
361
362 static INT get_font_style(const GpFont *font)
363 {
364     INT style;
365
366     if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
367         style = FontStyleBold;
368     else
369         style = FontStyleRegular;
370     if (font->otm.otmTextMetrics.tmItalic)
371         style |= FontStyleItalic;
372     if (font->otm.otmTextMetrics.tmUnderlined)
373         style |= FontStyleUnderline;
374     if (font->otm.otmTextMetrics.tmStruckOut)
375         style |= FontStyleStrikeout;
376
377     return style;
378 }
379
380 /*******************************************************************************
381  * GdipGetFontStyle [GDIPLUS.@]
382  *
383  * Gets the font's style, returned in bitwise OR of FontStyle enumeration
384  *
385  * PARAMS
386  *  font    [I] font to request from
387  *  style   [O] resulting pointer to a FontStyle enumeration
388  *
389  * RETURNS
390  *  SUCCESS: Ok
391  *  FAILURE: InvalidParameter
392  */
393 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
394 {
395     TRACE("%p %p\n", font, style);
396
397     if (!(font && style))
398         return InvalidParameter;
399
400     *style = get_font_style(font);
401     TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
402
403     return Ok;
404 }
405
406 /*******************************************************************************
407  * GdipGetFontUnit  [GDIPLUS.@]
408  *
409  * PARAMS
410  *  font    [I] Font to retrieve from
411  *  unit    [O] Return value
412  *
413  * RETURNS
414  *  FAILURE: font or unit was NULL
415  *  OK: otherwise
416  */
417 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
418 {
419     TRACE("(%p, %p)\n", font, unit);
420
421     if (!(font && unit)) return InvalidParameter;
422
423     *unit = font->unit;
424     TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
425
426     return Ok;
427 }
428
429 /*******************************************************************************
430  * GdipGetLogFontA [GDIPLUS.@]
431  */
432 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
433     LOGFONTA *lfa)
434 {
435     GpStatus status;
436     LOGFONTW lfw;
437
438     TRACE("(%p, %p, %p)\n", font, graphics, lfa);
439
440     status = GdipGetLogFontW(font, graphics, &lfw);
441     if(status != Ok)
442         return status;
443
444     memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
445
446     if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
447         return GenericError;
448
449     return Ok;
450 }
451
452 /*******************************************************************************
453  * GdipGetLogFontW [GDIPLUS.@]
454  */
455 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     TRACE("(%p, %p, %p)\n", font, graphics, lf);
462
463     if (!font || !graphics || !lf)
464         return InvalidParameter;
465
466     matrix = graphics->worldtrans;
467
468     if (font->unit == UnitPixel)
469     {
470         height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
471         if (graphics->unit != UnitDisplay)
472             GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
473     }
474     else
475     {
476         if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
477             height = units_to_pixels(font->emSize, font->unit, graphics->xres);
478         else
479             height = units_to_pixels(font->emSize, font->unit, graphics->yres);
480     }
481
482     pt[0].X = 0.0;
483     pt[0].Y = 0.0;
484     pt[1].X = 1.0;
485     pt[1].Y = 0.0;
486     pt[2].X = 0.0;
487     pt[2].Y = 1.0;
488     GdipTransformMatrixPoints(&matrix, pt, 3);
489     angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
490     rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
491                       (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
492
493     lf->lfHeight = -gdip_round(height * rel_height);
494     lf->lfWidth = 0;
495     lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
496     if (lf->lfEscapement < 0)
497     {
498         lf->lfEscapement += 3600;
499         lf->lfOrientation += 3600;
500     }
501     lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
502     lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
503     lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
504     lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
505     lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
506     lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
507     lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
508     lf->lfQuality = DEFAULT_QUALITY;
509     lf->lfPitchAndFamily = 0;
510     strcpyW(lf->lfFaceName, font->family->FamilyName);
511
512     TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
513
514     return Ok;
515 }
516
517 /*******************************************************************************
518  * GdipCloneFont [GDIPLUS.@]
519  */
520 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
521 {
522     GpStatus stat;
523
524     TRACE("(%p, %p)\n", font, cloneFont);
525
526     if(!font || !cloneFont)
527         return InvalidParameter;
528
529     *cloneFont = GdipAlloc(sizeof(GpFont));
530     if(!*cloneFont)    return OutOfMemory;
531
532     **cloneFont = *font;
533     stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
534     if (stat != Ok) GdipFree(*cloneFont);
535
536     return stat;
537 }
538
539 /*******************************************************************************
540  * GdipGetFontHeight [GDIPLUS.@]
541  * PARAMS
542  *  font        [I] Font to retrieve height from
543  *  graphics    [I] The current graphics context
544  *  height      [O] Resulting height
545  * RETURNS
546  *  SUCCESS: Ok
547  *  FAILURE: Another element of GpStatus
548  *
549  * NOTES
550  *  Forwards to GdipGetFontHeightGivenDPI
551  */
552 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
553         GDIPCONST GpGraphics *graphics, REAL *height)
554 {
555     REAL dpi;
556     GpStatus stat;
557     REAL font_height;
558
559     TRACE("%p %p %p\n", font, graphics, height);
560
561     stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
562     if (stat != Ok) return stat;
563
564     if (!graphics)
565     {
566         *height = font_height;
567         TRACE("%s,%d => %f\n",
568               debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
569         return Ok;
570     }
571
572     stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
573     if (stat != Ok) return stat;
574
575     *height = pixels_to_units(font_height, graphics->unit, dpi);
576
577     TRACE("%s,%d(unit %d) => %f\n",
578           debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
579     return Ok;
580 }
581
582 /*******************************************************************************
583  * GdipGetFontHeightGivenDPI [GDIPLUS.@]
584  * PARAMS
585  *  font        [I] Font to retrieve DPI from
586  *  dpi         [I] DPI to assume
587  *  height      [O] Return value
588  *
589  * RETURNS
590  *  SUCCESS: Ok
591  *  FAILURE: InvalidParameter if font or height is NULL
592  *
593  * NOTES
594  *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
595  *  (for anything other than unit Pixel)
596  */
597 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
598 {
599     GpStatus stat;
600     INT style;
601     UINT16 line_spacing, em_height;
602     REAL font_size;
603
604     if (!font || !height) return InvalidParameter;
605
606     TRACE("%p (%s), %f, %p\n", font,
607             debugstr_w(font->family->FamilyName), dpi, height);
608
609     font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
610     style = get_font_style(font);
611     stat = GdipGetLineSpacing(font->family, style, &line_spacing);
612     if (stat != Ok) return stat;
613     stat = GdipGetEmHeight(font->family, style, &em_height);
614     if (stat != Ok) return stat;
615
616     *height = (REAL)line_spacing * font_size / (REAL)em_height;
617
618     TRACE("%s,%d => %f\n",
619           debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
620
621     return Ok;
622 }
623
624 /***********************************************************************
625  * Borrowed from GDI32:
626  *
627  * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
628  *     We have to use other types because of the FONTENUMPROCW definition.
629  */
630 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
631                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
632 {
633     if (type & RASTER_FONTTYPE)
634         return 1;
635
636     *(LOGFONTW *)lParam = *elf;
637
638     return 0;
639 }
640
641 struct font_metrics
642 {
643     WCHAR facename[LF_FACESIZE];
644     UINT16 em_height, ascent, descent, line_spacing; /* in font units */
645     int dpi;
646 };
647
648 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
649 {
650     OUTLINETEXTMETRICW otm;
651     TT_OS2_V2 tt_os2;
652     TT_HHEA tt_hori;
653     LONG size;
654     UINT16 line_gap;
655
656     otm.otmSize = sizeof(otm);
657     if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
658
659     GetTextFaceW(hdc, LF_FACESIZE, fm->facename);
660
661     fm->em_height = otm.otmEMSquare;
662     fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
663
664     memset(&tt_hori, 0, sizeof(tt_hori));
665     if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
666     {
667         fm->ascent = GET_BE_WORD(tt_hori.Ascender);
668         fm->descent = -GET_BE_WORD(tt_hori.Descender);
669         TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
670         line_gap = GET_BE_WORD(tt_hori.LineGap);
671         fm->line_spacing = fm->ascent + fm->descent + line_gap;
672         TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
673         if (fm->ascent + fm->descent != 0) return TRUE;
674     }
675
676     size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
677     if (size == GDI_ERROR) return FALSE;
678
679     if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
680
681     memset(&tt_os2, 0, sizeof(tt_os2));
682     if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
683
684     fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
685     fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
686     TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
687     if (fm->ascent + fm->descent == 0)
688     {
689         fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
690         fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
691         TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
692     }
693     line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
694     fm->line_spacing = fm->ascent + fm->descent + line_gap;
695     TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
696     return TRUE;
697 }
698
699 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
700 {
701     LOGFONTW lf;
702     HDC hdc = CreateCompatibleDC(0);
703     GpStatus ret = FontFamilyNotFound;
704
705     if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
706     {
707         HFONT hfont, old_font;
708
709         hfont = CreateFontIndirectW(&lf);
710         old_font = SelectObject(hdc, hfont);
711         ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
712         SelectObject(hdc, old_font);
713         DeleteObject(hfont);
714     }
715
716     DeleteDC(hdc);
717     return ret;
718 }
719
720 /*******************************************************************************
721  * GdipCreateFontFamilyFromName [GDIPLUS.@]
722  *
723  * Creates a font family object based on a supplied name
724  *
725  * PARAMS
726  *  name               [I] Name of the font
727  *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
728  *  FontFamily         [O] Pointer to the resulting FontFamily object
729  *
730  * RETURNS
731  *  SUCCESS: Ok
732  *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
733  *  FAILURE: Invalid parameter if FontFamily or name is NULL
734  *
735  * NOTES
736  *   If fontCollection is NULL then the object is not part of any collection
737  *
738  */
739
740 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
741                                         GpFontCollection *fontCollection,
742                                         GpFontFamily **FontFamily)
743 {
744     GpStatus stat;
745     GpFontFamily* ffamily;
746     struct font_metrics fm;
747
748     TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
749
750     if (!(name && FontFamily))
751         return InvalidParameter;
752     if (fontCollection)
753         FIXME("No support for FontCollections yet!\n");
754
755     stat = find_installed_font(name, &fm);
756     if (stat != Ok) return stat;
757
758     ffamily = GdipAlloc(sizeof (GpFontFamily));
759     if (!ffamily) return OutOfMemory;
760
761     lstrcpyW(ffamily->FamilyName, fm.facename);
762     ffamily->em_height = fm.em_height;
763     ffamily->ascent = fm.ascent;
764     ffamily->descent = fm.descent;
765     ffamily->line_spacing = fm.line_spacing;
766     ffamily->dpi = fm.dpi;
767
768     *FontFamily = ffamily;
769
770     TRACE("<-- %p\n", ffamily);
771
772     return Ok;
773 }
774
775 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
776 {
777     *clone = GdipAlloc(sizeof(GpFontFamily));
778     if (!*clone) return OutOfMemory;
779
780     **clone = *family;
781
782     return Ok;
783 }
784
785 /*******************************************************************************
786  * GdipCloneFontFamily [GDIPLUS.@]
787  *
788  * Creates a deep copy of a Font Family object
789  *
790  * PARAMS
791  *  FontFamily          [I] Font to clone
792  *  clonedFontFamily    [O] The resulting cloned font
793  *
794  * RETURNS
795  *  SUCCESS: Ok
796  */
797 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
798 {
799     GpStatus status;
800
801     if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
802
803     TRACE("%p (%s), %p\n", FontFamily,
804             debugstr_w(FontFamily->FamilyName), clonedFontFamily);
805
806     status = clone_font_family(FontFamily, clonedFontFamily);
807     if (status != Ok) return status;
808
809     TRACE("<-- %p\n", *clonedFontFamily);
810
811     return Ok;
812 }
813
814 /*******************************************************************************
815  * GdipGetFamilyName [GDIPLUS.@]
816  *
817  * Returns the family name into name
818  *
819  * PARAMS
820  *  *family     [I] Family to retrieve from
821  *  *name       [O] WCHARS of the family name
822  *  LANGID      [I] charset
823  *
824  * RETURNS
825  *  SUCCESS: Ok
826  *  FAILURE: InvalidParameter if family is NULL
827  *
828  * NOTES
829  *   If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
830  */
831 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
832                                        WCHAR *name, LANGID language)
833 {
834     static int lang_fixme;
835
836     if (family == NULL)
837          return InvalidParameter;
838
839     TRACE("%p, %p, %d\n", family, name, language);
840
841     if (language != LANG_NEUTRAL && !lang_fixme++)
842         FIXME("No support for handling of multiple languages!\n");
843
844     lstrcpynW (name, family->FamilyName, LF_FACESIZE);
845
846     return Ok;
847 }
848
849
850 /*****************************************************************************
851  * GdipDeleteFontFamily [GDIPLUS.@]
852  *
853  * Removes the specified FontFamily
854  *
855  * PARAMS
856  *  *FontFamily         [I] The family to delete
857  *
858  * RETURNS
859  *  SUCCESS: Ok
860  *  FAILURE: InvalidParameter if FontFamily is NULL.
861  *
862  */
863 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
864 {
865     if (!FontFamily)
866         return InvalidParameter;
867     TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
868
869     GdipFree (FontFamily);
870
871     return Ok;
872 }
873
874 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
875         INT style, UINT16* CellAscent)
876 {
877     if (!(family && CellAscent)) return InvalidParameter;
878
879     *CellAscent = family->ascent;
880     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
881
882     return Ok;
883 }
884
885 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
886         INT style, UINT16* CellDescent)
887 {
888     TRACE("(%p, %d, %p)\n", family, style, CellDescent);
889
890     if (!(family && CellDescent)) return InvalidParameter;
891
892     *CellDescent = family->descent;
893     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
894
895     return Ok;
896 }
897
898 /*******************************************************************************
899  * GdipGetEmHeight [GDIPLUS.@]
900  *
901  * Gets the height of the specified family in EmHeights
902  *
903  * PARAMS
904  *  family      [I] Family to retrieve from
905  *  style       [I] (optional) style
906  *  EmHeight    [O] return value
907  *
908  * RETURNS
909  *  SUCCESS: Ok
910  *  FAILURE: InvalidParameter
911  */
912 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
913 {
914     if (!(family && EmHeight)) return InvalidParameter;
915
916     TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
917
918     *EmHeight = family->em_height;
919     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
920
921     return Ok;
922 }
923
924
925 /*******************************************************************************
926  * GdipGetLineSpacing [GDIPLUS.@]
927  *
928  * Returns the line spacing in design units
929  *
930  * PARAMS
931  *  family      [I] Family to retrieve from
932  *  style       [I] (Optional) font style
933  *  LineSpacing [O] Return value
934  *
935  * RETURNS
936  *  SUCCESS: Ok
937  *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
938  */
939 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
940         INT style, UINT16* LineSpacing)
941 {
942     TRACE("%p, %d, %p\n", family, style, LineSpacing);
943
944     if (!(family && LineSpacing))
945         return InvalidParameter;
946
947     if (style) FIXME("ignoring style\n");
948
949     *LineSpacing = family->line_spacing;
950     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
951
952     return Ok;
953 }
954
955 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
956                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
957 {
958     INT fontstyle = FontStyleRegular;
959
960     if (!ntm) return 1;
961
962     if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
963     if (ntm->tmItalic) fontstyle |= FontStyleItalic;
964     if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
965     if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
966
967     return (INT)lParam != fontstyle;
968 }
969
970 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
971         INT style, BOOL* IsStyleAvailable)
972 {
973     HDC hdc;
974
975     TRACE("%p %d %p\n", family, style, IsStyleAvailable);
976
977     if (!(family && IsStyleAvailable))
978         return InvalidParameter;
979
980     *IsStyleAvailable = FALSE;
981
982     hdc = GetDC(0);
983
984     if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
985         *IsStyleAvailable = TRUE;
986
987     ReleaseDC(0, hdc);
988
989     return Ok;
990 }
991
992 /*****************************************************************************
993  * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
994  *
995  * Obtains a serif family (Courier New on Windows)
996  *
997  * PARAMS
998  *  **nativeFamily         [I] Where the font will be stored
999  *
1000  * RETURNS
1001  *  InvalidParameter if nativeFamily is NULL.
1002  *  Ok otherwise.
1003  */
1004 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
1005 {
1006     static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
1007     static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
1008     GpStatus stat;
1009
1010     if (nativeFamily == NULL) return InvalidParameter;
1011
1012     stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
1013
1014     if (stat == FontFamilyNotFound)
1015         stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1016
1017     if (stat == FontFamilyNotFound)
1018         ERR("Missing 'Courier New' font\n");
1019
1020     return stat;
1021 }
1022
1023 /*****************************************************************************
1024  * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1025  *
1026  * Obtains a serif family (Times New Roman on Windows)
1027  *
1028  * PARAMS
1029  *  **nativeFamily         [I] Where the font will be stored
1030  *
1031  * RETURNS
1032  *  InvalidParameter if nativeFamily is NULL.
1033  *  Ok otherwise.
1034  */
1035 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1036 {
1037     static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1038     static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1039     GpStatus stat;
1040
1041     TRACE("(%p)\n", nativeFamily);
1042
1043     if (nativeFamily == NULL) return InvalidParameter;
1044
1045     stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1046
1047     if (stat == FontFamilyNotFound)
1048         stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1049
1050     if (stat == FontFamilyNotFound)
1051         ERR("Missing 'Times New Roman' font\n");
1052
1053     return stat;
1054 }
1055
1056 /*****************************************************************************
1057  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1058  *
1059  * Obtains a serif family (Microsoft Sans Serif on Windows)
1060  *
1061  * PARAMS
1062  *  **nativeFamily         [I] Where the font will be stored
1063  *
1064  * RETURNS
1065  *  InvalidParameter if nativeFamily is NULL.
1066  *  Ok otherwise.
1067  */
1068 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1069 {
1070     GpStatus stat;
1071     static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1072     static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1073
1074     TRACE("(%p)\n", nativeFamily);
1075
1076     if (nativeFamily == NULL) return InvalidParameter;
1077
1078     stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1079
1080     if (stat == FontFamilyNotFound)
1081         /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1082         stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1083
1084     return stat;
1085 }
1086
1087 /*****************************************************************************
1088  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1089  */
1090 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1091 {
1092     TRACE("%p\n", fontCollection);
1093
1094     if (!fontCollection)
1095         return InvalidParameter;
1096
1097     *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1098     if (!*fontCollection) return OutOfMemory;
1099
1100     (*fontCollection)->FontFamilies = NULL;
1101     (*fontCollection)->count = 0;
1102     (*fontCollection)->allocated = 0;
1103
1104     TRACE("<-- %p\n", *fontCollection);
1105
1106     return Ok;
1107 }
1108
1109 /*****************************************************************************
1110  * GdipDeletePrivateFontCollection [GDIPLUS.@]
1111  */
1112 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1113 {
1114     INT i;
1115
1116     TRACE("%p\n", fontCollection);
1117
1118     if (!fontCollection)
1119         return InvalidParameter;
1120
1121     for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1122     GdipFree(*fontCollection);
1123
1124     return Ok;
1125 }
1126
1127 /*****************************************************************************
1128  * GdipPrivateAddFontFile [GDIPLUS.@]
1129  */
1130 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
1131         GDIPCONST WCHAR* filename)
1132 {
1133     FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
1134
1135     if (!(fontCollection && filename))
1136         return InvalidParameter;
1137
1138     return NotImplemented;
1139 }
1140
1141 /* Copied from msi/font.c */
1142
1143 typedef struct _tagTT_OFFSET_TABLE {
1144     USHORT uMajorVersion;
1145     USHORT uMinorVersion;
1146     USHORT uNumOfTables;
1147     USHORT uSearchRange;
1148     USHORT uEntrySelector;
1149     USHORT uRangeShift;
1150 } TT_OFFSET_TABLE;
1151
1152 typedef struct _tagTT_TABLE_DIRECTORY {
1153     char szTag[4]; /* table name */
1154     ULONG uCheckSum; /* Check sum */
1155     ULONG uOffset; /* Offset from beginning of file */
1156     ULONG uLength; /* length of the table in bytes */
1157 } TT_TABLE_DIRECTORY;
1158
1159 typedef struct _tagTT_NAME_TABLE_HEADER {
1160     USHORT uFSelector; /* format selector. Always 0 */
1161     USHORT uNRCount; /* Name Records count */
1162     USHORT uStorageOffset; /* Offset for strings storage,
1163                             * from start of the table */
1164 } TT_NAME_TABLE_HEADER;
1165
1166 #define NAME_ID_FULL_FONT_NAME  4
1167 #define NAME_ID_VERSION         5
1168
1169 typedef struct _tagTT_NAME_RECORD {
1170     USHORT uPlatformID;
1171     USHORT uEncodingID;
1172     USHORT uLanguageID;
1173     USHORT uNameID;
1174     USHORT uStringLength;
1175     USHORT uStringOffset; /* from start of storage area */
1176 } TT_NAME_RECORD;
1177
1178 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1179 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1180
1181 /*
1182  * Code based off of code located here
1183  * http://www.codeproject.com/gdi/fontnamefromfile.asp
1184  */
1185 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1186 {
1187     const TT_TABLE_DIRECTORY *tblDir;
1188     TT_OFFSET_TABLE ttOffsetTable;
1189     TT_NAME_TABLE_HEADER ttNTHeader;
1190     TT_NAME_RECORD ttRecord;
1191     DWORD ofs, pos;
1192     int i;
1193
1194     if (sizeof(TT_OFFSET_TABLE) > size)
1195         return NULL;
1196     ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1197     ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1198     ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1199     ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1200
1201     if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1202         return NULL;
1203
1204     pos = sizeof(ttOffsetTable);
1205     for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1206     {
1207         tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1208         pos += sizeof(*tblDir);
1209         if (memcmp(tblDir->szTag,"name",4)==0)
1210         {
1211             ofs = SWAPLONG(tblDir->uOffset);
1212             break;
1213         }
1214     }
1215     if (i >= ttOffsetTable.uNumOfTables)
1216         return NULL;
1217
1218     pos = ofs + sizeof(ttNTHeader);
1219     if (pos > size)
1220         return NULL;
1221     ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1222     ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1223     ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1224     for(i=0; i<ttNTHeader.uNRCount; i++)
1225     {
1226         ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1227         pos += sizeof(ttRecord);
1228         if (pos > size)
1229             return NULL;
1230
1231         ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1232         if (ttRecord.uNameID == id)
1233         {
1234             const char *buf;
1235
1236             ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1237             ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1238             if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1239                 return NULL;
1240             buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1241             len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1242             ret[len] = 0;
1243             return ret;
1244         }
1245     }
1246     return NULL;
1247 }
1248
1249 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1250
1251 /*****************************************************************************
1252  * GdipPrivateAddMemoryFont [GDIPLUS.@]
1253  */
1254 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1255         GDIPCONST void* memory, INT length)
1256 {
1257     WCHAR buf[32], *name;
1258     DWORD count = 0;
1259     HANDLE font;
1260     TRACE("%p, %p, %d\n", fontCollection, memory, length);
1261
1262     if (!fontCollection || !memory || !length)
1263         return InvalidParameter;
1264
1265     name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1266     if (!name)
1267         return OutOfMemory;
1268
1269     font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1270     TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1271     if (!font || !count)
1272         return InvalidParameter;
1273
1274     if (count)
1275     {
1276         HDC hdc;
1277         LOGFONTW lfw;
1278
1279         hdc = GetDC(0);
1280
1281         lfw.lfCharSet = DEFAULT_CHARSET;
1282         lstrcpyW(lfw.lfFaceName, name);
1283         lfw.lfPitchAndFamily = 0;
1284
1285         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1286         {
1287             ReleaseDC(0, hdc);
1288             return OutOfMemory;
1289         }
1290
1291         ReleaseDC(0, hdc);
1292     }
1293     return Ok;
1294 }
1295
1296 /*****************************************************************************
1297  * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1298  */
1299 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1300         GpFontCollection* fontCollection, INT* numFound)
1301 {
1302     TRACE("%p, %p\n", fontCollection, numFound);
1303
1304     if (!(fontCollection && numFound))
1305         return InvalidParameter;
1306
1307     *numFound = fontCollection->count;
1308     return Ok;
1309 }
1310
1311 /*****************************************************************************
1312  * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1313  */
1314 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1315         GpFontCollection* fontCollection, INT numSought,
1316         GpFontFamily* gpfamilies[], INT* numFound)
1317 {
1318     INT i;
1319     GpStatus stat=Ok;
1320
1321     TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1322
1323     if (!(fontCollection && gpfamilies && numFound))
1324         return InvalidParameter;
1325
1326     memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1327
1328     for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1329     {
1330         stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1331     }
1332
1333     if (stat == Ok)
1334         *numFound = i;
1335     else
1336     {
1337         int numToFree=i;
1338         for (i=0; i<numToFree; i++)
1339         {
1340             GdipDeleteFontFamily(gpfamilies[i]);
1341             gpfamilies[i] = NULL;
1342         }
1343     }
1344
1345     return stat;
1346 }
1347
1348 void free_installed_fonts(void)
1349 {
1350     while (installedFontCollection.count)
1351         GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1352     HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1353     installedFontCollection.FontFamilies = NULL;
1354     installedFontCollection.allocated = 0;
1355 }
1356
1357 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1358         DWORD type, LPARAM lParam)
1359 {
1360     GpFontCollection* fonts = (GpFontCollection*)lParam;
1361     int i;
1362
1363     if (type == RASTER_FONTTYPE)
1364         return 1;
1365
1366     /* skip duplicates */
1367     for (i=0; i<fonts->count; i++)
1368         if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1369             return 1;
1370
1371     if (fonts->allocated == fonts->count)
1372     {
1373         INT new_alloc_count = fonts->allocated+50;
1374         GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1375
1376         if (!new_family_list)
1377             return 0;
1378
1379         memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1380         HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1381         fonts->FontFamilies = new_family_list;
1382         fonts->allocated = new_alloc_count;
1383     }
1384
1385     if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1386         fonts->count++;
1387     else
1388         return 0;
1389
1390     return 1;
1391 }
1392
1393 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1394         GpFontCollection** fontCollection)
1395 {
1396     TRACE("(%p)\n",fontCollection);
1397
1398     if (!fontCollection)
1399         return InvalidParameter;
1400
1401     if (installedFontCollection.count == 0)
1402     {
1403         HDC hdc;
1404         LOGFONTW lfw;
1405
1406         hdc = GetDC(0);
1407
1408         lfw.lfCharSet = DEFAULT_CHARSET;
1409         lfw.lfFaceName[0] = 0;
1410         lfw.lfPitchAndFamily = 0;
1411
1412         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1413         {
1414             free_installed_fonts();
1415             ReleaseDC(0, hdc);
1416             return OutOfMemory;
1417         }
1418
1419         ReleaseDC(0, hdc);
1420     }
1421
1422     *fontCollection = &installedFontCollection;
1423
1424     return Ok;
1425 }