wbemprox: Store the class name in the class object.
[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     GpStatus stat;
209     int ret;
210
211     TRACE("(%p, %p, %p)\n", hdc, logfont, font);
212
213     if (!hdc || !logfont || !font)
214         return InvalidParameter;
215
216     hfont = CreateFontIndirectW(logfont);
217     oldfont = SelectObject(hdc, hfont);
218     otm.otmSize = sizeof(otm);
219     ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
220     SelectObject(hdc, oldfont);
221     DeleteObject(hfont);
222
223     if (!ret) return NotTrueTypeFont;
224
225     *font = GdipAlloc(sizeof(GpFont));
226     if (!*font) return OutOfMemory;
227
228     (*font)->unit = UnitWorld;
229     (*font)->emSize = otm.otmTextMetrics.tmAscent;
230     (*font)->otm = otm;
231
232     stat = GdipCreateFontFamilyFromName(logfont->lfFaceName, NULL, &(*font)->family);
233     if (stat != Ok)
234     {
235         GdipFree(*font);
236         return NotTrueTypeFont;
237     }
238
239     TRACE("<-- %p\n", *font);
240
241     return Ok;
242 }
243
244 /*******************************************************************************
245  * GdipCreateFontFromLogfontA [GDIPLUS.@]
246  */
247 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
248     GDIPCONST LOGFONTA *lfa, GpFont **font)
249 {
250     LOGFONTW lfw;
251
252     TRACE("(%p, %p, %p)\n", hdc, lfa, font);
253
254     if(!lfa || !font)
255         return InvalidParameter;
256
257     memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
258
259     if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
260         return GenericError;
261
262     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
263 }
264
265 /*******************************************************************************
266  * GdipDeleteFont [GDIPLUS.@]
267  */
268 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
269 {
270     TRACE("(%p)\n", font);
271
272     if(!font)
273         return InvalidParameter;
274
275     GdipDeleteFontFamily(font->family);
276     GdipFree(font);
277
278     return Ok;
279 }
280
281 /*******************************************************************************
282  * GdipCreateFontFromDC [GDIPLUS.@]
283  */
284 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
285 {
286     HFONT hfont;
287     LOGFONTW lfw;
288
289     TRACE("(%p, %p)\n", hdc, font);
290
291     if(!font)
292         return InvalidParameter;
293
294     hfont = GetCurrentObject(hdc, OBJ_FONT);
295     if(!hfont)
296         return GenericError;
297
298     if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
299         return GenericError;
300
301     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
302 }
303
304 /*******************************************************************************
305  * GdipGetFamily [GDIPLUS.@]
306  *
307  * Returns the FontFamily for the specified Font
308  *
309  * PARAMS
310  *  font    [I] Font to request from
311  *  family  [O] Resulting FontFamily object
312  *
313  * RETURNS
314  *  SUCCESS: Ok
315  *  FAILURE: An element of GpStatus
316  */
317 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
318 {
319     TRACE("%p %p\n", font, family);
320
321     if (!(font && family))
322         return InvalidParameter;
323
324     return GdipCloneFontFamily(font->family, family);
325 }
326
327 static REAL get_font_size(const GpFont *font)
328 {
329     return font->emSize;
330 }
331
332 /******************************************************************************
333  * GdipGetFontSize [GDIPLUS.@]
334  *
335  * Returns the size of the font in Units
336  *
337  * PARAMS
338  *  *font       [I] The font to retrieve size from
339  *  *size       [O] Pointer to hold retrieved value
340  *
341  * RETURNS
342  *  SUCCESS: Ok
343  *  FAILURE: InvalidParameter (font or size was NULL)
344  *
345  * NOTES
346  *  Size returned is actually emSize -- not internal size used for drawing.
347  */
348 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
349 {
350     TRACE("(%p, %p)\n", font, size);
351
352     if (!(font && size)) return InvalidParameter;
353
354     *size = get_font_size(font);
355     TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
356
357     return Ok;
358 }
359
360 static INT get_font_style(const GpFont *font)
361 {
362     INT style;
363
364     if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
365         style = FontStyleBold;
366     else
367         style = FontStyleRegular;
368     if (font->otm.otmTextMetrics.tmItalic)
369         style |= FontStyleItalic;
370     if (font->otm.otmTextMetrics.tmUnderlined)
371         style |= FontStyleUnderline;
372     if (font->otm.otmTextMetrics.tmStruckOut)
373         style |= FontStyleStrikeout;
374
375     return style;
376 }
377
378 /*******************************************************************************
379  * GdipGetFontStyle [GDIPLUS.@]
380  *
381  * Gets the font's style, returned in bitwise OR of FontStyle enumeration
382  *
383  * PARAMS
384  *  font    [I] font to request from
385  *  style   [O] resulting pointer to a FontStyle enumeration
386  *
387  * RETURNS
388  *  SUCCESS: Ok
389  *  FAILURE: InvalidParameter
390  */
391 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
392 {
393     TRACE("%p %p\n", font, style);
394
395     if (!(font && style))
396         return InvalidParameter;
397
398     *style = get_font_style(font);
399     TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
400
401     return Ok;
402 }
403
404 /*******************************************************************************
405  * GdipGetFontUnit  [GDIPLUS.@]
406  *
407  * PARAMS
408  *  font    [I] Font to retrieve from
409  *  unit    [O] Return value
410  *
411  * RETURNS
412  *  FAILURE: font or unit was NULL
413  *  OK: otherwise
414  */
415 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
416 {
417     TRACE("(%p, %p)\n", font, unit);
418
419     if (!(font && unit)) return InvalidParameter;
420
421     *unit = font->unit;
422     TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
423
424     return Ok;
425 }
426
427 /*******************************************************************************
428  * GdipGetLogFontA [GDIPLUS.@]
429  */
430 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
431     LOGFONTA *lfa)
432 {
433     GpStatus status;
434     LOGFONTW lfw;
435
436     TRACE("(%p, %p, %p)\n", font, graphics, lfa);
437
438     status = GdipGetLogFontW(font, graphics, &lfw);
439     if(status != Ok)
440         return status;
441
442     memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
443
444     if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
445         return GenericError;
446
447     return Ok;
448 }
449
450 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
451 {
452     /* FIXME: use graphics */
453     lf->lfHeight = -units_to_pixels(font->emSize, font->unit, font->family->dpi);
454     lf->lfWidth = 0;
455     lf->lfEscapement = 0;
456     lf->lfOrientation = 0;
457     lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
458     lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
459     lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
460     lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
461     lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
462     lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
463     lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
464     lf->lfQuality = DEFAULT_QUALITY;
465     lf->lfPitchAndFamily = 0;
466     strcpyW(lf->lfFaceName, font->family->FamilyName);
467 }
468
469 /*******************************************************************************
470  * GdipGetLogFontW [GDIPLUS.@]
471  */
472 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
473     LOGFONTW *lfw)
474 {
475     TRACE("(%p, %p, %p)\n", font, graphics, lfw);
476
477     if(!font || !graphics || !lfw)
478         return InvalidParameter;
479
480     get_log_fontW(font, graphics, lfw);
481     TRACE("=> %s,%d\n", debugstr_w(lfw->lfFaceName), lfw->lfHeight);
482
483     return Ok;
484 }
485
486 /*******************************************************************************
487  * GdipCloneFont [GDIPLUS.@]
488  */
489 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
490 {
491     GpStatus stat;
492
493     TRACE("(%p, %p)\n", font, cloneFont);
494
495     if(!font || !cloneFont)
496         return InvalidParameter;
497
498     *cloneFont = GdipAlloc(sizeof(GpFont));
499     if(!*cloneFont)    return OutOfMemory;
500
501     **cloneFont = *font;
502     stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
503     if (stat != Ok) GdipFree(*cloneFont);
504
505     return stat;
506 }
507
508 /*******************************************************************************
509  * GdipGetFontHeight [GDIPLUS.@]
510  * PARAMS
511  *  font        [I] Font to retrieve height from
512  *  graphics    [I] The current graphics context
513  *  height      [O] Resulting height
514  * RETURNS
515  *  SUCCESS: Ok
516  *  FAILURE: Another element of GpStatus
517  *
518  * NOTES
519  *  Forwards to GdipGetFontHeightGivenDPI
520  */
521 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
522         GDIPCONST GpGraphics *graphics, REAL *height)
523 {
524     REAL dpi;
525     GpStatus stat;
526     REAL font_height;
527
528     TRACE("%p %p %p\n", font, graphics, height);
529
530     stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
531     if (stat != Ok) return stat;
532
533     if (!graphics)
534     {
535         *height = font_height;
536         TRACE("%s,%d => %f\n",
537               debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
538         return Ok;
539     }
540
541     stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
542     if (stat != Ok) return stat;
543
544     *height = pixels_to_units(font_height, graphics->unit, dpi);
545
546     TRACE("%s,%d(unit %d) => %f\n",
547           debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
548     return Ok;
549 }
550
551 /*******************************************************************************
552  * GdipGetFontHeightGivenDPI [GDIPLUS.@]
553  * PARAMS
554  *  font        [I] Font to retrieve DPI from
555  *  dpi         [I] DPI to assume
556  *  height      [O] Return value
557  *
558  * RETURNS
559  *  SUCCESS: Ok
560  *  FAILURE: InvalidParameter if font or height is NULL
561  *
562  * NOTES
563  *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
564  *  (for anything other than unit Pixel)
565  */
566 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
567 {
568     GpStatus stat;
569     INT style;
570     UINT16 line_spacing, em_height;
571     REAL font_size;
572
573     if (!font || !height) return InvalidParameter;
574
575     TRACE("%p (%s), %f, %p\n", font,
576             debugstr_w(font->family->FamilyName), dpi, height);
577
578     font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
579     style = get_font_style(font);
580     stat = GdipGetLineSpacing(font->family, style, &line_spacing);
581     if (stat != Ok) return stat;
582     stat = GdipGetEmHeight(font->family, style, &em_height);
583     if (stat != Ok) return stat;
584
585     *height = (REAL)line_spacing * font_size / (REAL)em_height;
586
587     TRACE("%s,%d => %f\n",
588           debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
589
590     return Ok;
591 }
592
593 /***********************************************************************
594  * Borrowed from GDI32:
595  *
596  * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
597  *     We have to use other types because of the FONTENUMPROCW definition.
598  */
599 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
600                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
601 {
602     if (type & RASTER_FONTTYPE)
603         return 1;
604
605     *(LOGFONTW *)lParam = *elf;
606
607     return 0;
608 }
609
610 struct font_metrics
611 {
612     UINT16 em_height, ascent, descent, line_spacing; /* in font units */
613     int dpi;
614 };
615
616 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
617 {
618     OUTLINETEXTMETRICW otm;
619     TT_OS2_V2 tt_os2;
620     TT_HHEA tt_hori;
621     LONG size;
622     UINT16 line_gap;
623
624     otm.otmSize = sizeof(otm);
625     if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
626
627     fm->em_height = otm.otmEMSquare;
628     fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
629
630     memset(&tt_hori, 0, sizeof(tt_hori));
631     if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
632     {
633         fm->ascent = GET_BE_WORD(tt_hori.Ascender);
634         fm->descent = -GET_BE_WORD(tt_hori.Descender);
635         TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
636         line_gap = GET_BE_WORD(tt_hori.LineGap);
637         fm->line_spacing = fm->ascent + fm->descent + line_gap;
638         TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
639         if (fm->ascent + fm->descent != 0) return TRUE;
640     }
641
642     size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
643     if (size == GDI_ERROR) return FALSE;
644
645     if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
646
647     memset(&tt_os2, 0, sizeof(tt_os2));
648     if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
649
650     fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
651     fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
652     TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
653     if (fm->ascent + fm->descent == 0)
654     {
655         fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
656         fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
657         TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
658     }
659     line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
660     fm->line_spacing = fm->ascent + fm->descent + line_gap;
661     TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
662     return TRUE;
663 }
664
665 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
666 {
667     LOGFONTW lf;
668     HDC hdc = CreateCompatibleDC(0);
669     GpStatus ret = FontFamilyNotFound;
670
671     if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
672     {
673         HFONT hfont, old_font;
674
675         hfont = CreateFontIndirectW(&lf);
676         old_font = SelectObject(hdc, hfont);
677         ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
678         SelectObject(hdc, old_font);
679         DeleteObject(hfont);
680     }
681
682     DeleteDC(hdc);
683     return ret;
684 }
685
686 /*******************************************************************************
687  * GdipCreateFontFamilyFromName [GDIPLUS.@]
688  *
689  * Creates a font family object based on a supplied name
690  *
691  * PARAMS
692  *  name               [I] Name of the font
693  *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
694  *  FontFamily         [O] Pointer to the resulting FontFamily object
695  *
696  * RETURNS
697  *  SUCCESS: Ok
698  *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
699  *  FAILURE: Invalid parameter if FontFamily or name is NULL
700  *
701  * NOTES
702  *   If fontCollection is NULL then the object is not part of any collection
703  *
704  */
705
706 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
707                                         GpFontCollection *fontCollection,
708                                         GpFontFamily **FontFamily)
709 {
710     GpStatus stat;
711     GpFontFamily* ffamily;
712     struct font_metrics fm;
713
714     TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
715
716     if (!(name && FontFamily))
717         return InvalidParameter;
718     if (fontCollection)
719         FIXME("No support for FontCollections yet!\n");
720
721     stat = find_installed_font(name, &fm);
722     if (stat != Ok) return stat;
723
724     ffamily = GdipAlloc(sizeof (GpFontFamily));
725     if (!ffamily) return OutOfMemory;
726
727     lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
728     ffamily->em_height = fm.em_height;
729     ffamily->ascent = fm.ascent;
730     ffamily->descent = fm.descent;
731     ffamily->line_spacing = fm.line_spacing;
732     ffamily->dpi = fm.dpi;
733
734     *FontFamily = ffamily;
735
736     TRACE("<-- %p\n", ffamily);
737
738     return Ok;
739 }
740
741 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
742 {
743     *clone = GdipAlloc(sizeof(GpFontFamily));
744     if (!*clone) return OutOfMemory;
745
746     **clone = *family;
747
748     return Ok;
749 }
750
751 /*******************************************************************************
752  * GdipCloneFontFamily [GDIPLUS.@]
753  *
754  * Creates a deep copy of a Font Family object
755  *
756  * PARAMS
757  *  FontFamily          [I] Font to clone
758  *  clonedFontFamily    [O] The resulting cloned font
759  *
760  * RETURNS
761  *  SUCCESS: Ok
762  */
763 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
764 {
765     GpStatus status;
766
767     if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
768
769     TRACE("%p (%s), %p\n", FontFamily,
770             debugstr_w(FontFamily->FamilyName), clonedFontFamily);
771
772     status = clone_font_family(FontFamily, clonedFontFamily);
773     if (status != Ok) return status;
774
775     TRACE("<-- %p\n", *clonedFontFamily);
776
777     return Ok;
778 }
779
780 /*******************************************************************************
781  * GdipGetFamilyName [GDIPLUS.@]
782  *
783  * Returns the family name into name
784  *
785  * PARAMS
786  *  *family     [I] Family to retrieve from
787  *  *name       [O] WCHARS of the family name
788  *  LANGID      [I] charset
789  *
790  * RETURNS
791  *  SUCCESS: Ok
792  *  FAILURE: InvalidParameter if family is NULL
793  *
794  * NOTES
795  *   If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
796  */
797 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
798                                        WCHAR *name, LANGID language)
799 {
800     static int lang_fixme;
801
802     if (family == NULL)
803          return InvalidParameter;
804
805     TRACE("%p, %p, %d\n", family, name, language);
806
807     if (language != LANG_NEUTRAL && !lang_fixme++)
808         FIXME("No support for handling of multiple languages!\n");
809
810     lstrcpynW (name, family->FamilyName, LF_FACESIZE);
811
812     return Ok;
813 }
814
815
816 /*****************************************************************************
817  * GdipDeleteFontFamily [GDIPLUS.@]
818  *
819  * Removes the specified FontFamily
820  *
821  * PARAMS
822  *  *FontFamily         [I] The family to delete
823  *
824  * RETURNS
825  *  SUCCESS: Ok
826  *  FAILURE: InvalidParameter if FontFamily is NULL.
827  *
828  */
829 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
830 {
831     if (!FontFamily)
832         return InvalidParameter;
833     TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
834
835     GdipFree (FontFamily);
836
837     return Ok;
838 }
839
840 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
841         INT style, UINT16* CellAscent)
842 {
843     if (!(family && CellAscent)) return InvalidParameter;
844
845     *CellAscent = family->ascent;
846     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
847
848     return Ok;
849 }
850
851 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
852         INT style, UINT16* CellDescent)
853 {
854     TRACE("(%p, %d, %p)\n", family, style, CellDescent);
855
856     if (!(family && CellDescent)) return InvalidParameter;
857
858     *CellDescent = family->descent;
859     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
860
861     return Ok;
862 }
863
864 /*******************************************************************************
865  * GdipGetEmHeight [GDIPLUS.@]
866  *
867  * Gets the height of the specified family in EmHeights
868  *
869  * PARAMS
870  *  family      [I] Family to retrieve from
871  *  style       [I] (optional) style
872  *  EmHeight    [O] return value
873  *
874  * RETURNS
875  *  SUCCESS: Ok
876  *  FAILURE: InvalidParameter
877  */
878 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
879 {
880     if (!(family && EmHeight)) return InvalidParameter;
881
882     TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
883
884     *EmHeight = family->em_height;
885     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
886
887     return Ok;
888 }
889
890
891 /*******************************************************************************
892  * GdipGetLineSpacing [GDIPLUS.@]
893  *
894  * Returns the line spacing in design units
895  *
896  * PARAMS
897  *  family      [I] Family to retrieve from
898  *  style       [I] (Optional) font style
899  *  LineSpacing [O] Return value
900  *
901  * RETURNS
902  *  SUCCESS: Ok
903  *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
904  */
905 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
906         INT style, UINT16* LineSpacing)
907 {
908     TRACE("%p, %d, %p\n", family, style, LineSpacing);
909
910     if (!(family && LineSpacing))
911         return InvalidParameter;
912
913     if (style) FIXME("ignoring style\n");
914
915     *LineSpacing = family->line_spacing;
916     TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
917
918     return Ok;
919 }
920
921 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
922                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
923 {
924     INT fontstyle = FontStyleRegular;
925
926     if (!ntm) return 1;
927
928     if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
929     if (ntm->tmItalic) fontstyle |= FontStyleItalic;
930     if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
931     if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
932
933     return (INT)lParam != fontstyle;
934 }
935
936 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
937         INT style, BOOL* IsStyleAvailable)
938 {
939     HDC hdc;
940
941     TRACE("%p %d %p\n", family, style, IsStyleAvailable);
942
943     if (!(family && IsStyleAvailable))
944         return InvalidParameter;
945
946     *IsStyleAvailable = FALSE;
947
948     hdc = GetDC(0);
949
950     if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
951         *IsStyleAvailable = TRUE;
952
953     ReleaseDC(0, hdc);
954
955     return Ok;
956 }
957
958 /*****************************************************************************
959  * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
960  *
961  * Obtains a serif family (Courier New on Windows)
962  *
963  * PARAMS
964  *  **nativeFamily         [I] Where the font will be stored
965  *
966  * RETURNS
967  *  InvalidParameter if nativeFamily is NULL.
968  *  Ok otherwise.
969  */
970 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
971 {
972     static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
973     static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
974     GpStatus stat;
975
976     if (nativeFamily == NULL) return InvalidParameter;
977
978     stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
979
980     if (stat == FontFamilyNotFound)
981         stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
982
983     if (stat == FontFamilyNotFound)
984         ERR("Missing 'Courier New' font\n");
985
986     return stat;
987 }
988
989 /*****************************************************************************
990  * GdipGetGenericFontFamilySerif [GDIPLUS.@]
991  *
992  * Obtains a serif family (Times New Roman on Windows)
993  *
994  * PARAMS
995  *  **nativeFamily         [I] Where the font will be stored
996  *
997  * RETURNS
998  *  InvalidParameter if nativeFamily is NULL.
999  *  Ok otherwise.
1000  */
1001 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1002 {
1003     static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1004     static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1005     GpStatus stat;
1006
1007     TRACE("(%p)\n", nativeFamily);
1008
1009     if (nativeFamily == NULL) return InvalidParameter;
1010
1011     stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1012
1013     if (stat == FontFamilyNotFound)
1014         stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1015
1016     if (stat == FontFamilyNotFound)
1017         ERR("Missing 'Times New Roman' font\n");
1018
1019     return stat;
1020 }
1021
1022 /*****************************************************************************
1023  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1024  *
1025  * Obtains a serif family (Microsoft Sans Serif on Windows)
1026  *
1027  * PARAMS
1028  *  **nativeFamily         [I] Where the font will be stored
1029  *
1030  * RETURNS
1031  *  InvalidParameter if nativeFamily is NULL.
1032  *  Ok otherwise.
1033  */
1034 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1035 {
1036     GpStatus stat;
1037     static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1038     static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1039
1040     TRACE("(%p)\n", nativeFamily);
1041
1042     if (nativeFamily == NULL) return InvalidParameter;
1043
1044     stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1045
1046     if (stat == FontFamilyNotFound)
1047         /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1048         stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1049
1050     return stat;
1051 }
1052
1053 /*****************************************************************************
1054  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1055  */
1056 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1057 {
1058     TRACE("%p\n", fontCollection);
1059
1060     if (!fontCollection)
1061         return InvalidParameter;
1062
1063     *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1064     if (!*fontCollection) return OutOfMemory;
1065
1066     (*fontCollection)->FontFamilies = NULL;
1067     (*fontCollection)->count = 0;
1068     (*fontCollection)->allocated = 0;
1069
1070     TRACE("<-- %p\n", *fontCollection);
1071
1072     return Ok;
1073 }
1074
1075 /*****************************************************************************
1076  * GdipDeletePrivateFontCollection [GDIPLUS.@]
1077  */
1078 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1079 {
1080     INT i;
1081
1082     TRACE("%p\n", fontCollection);
1083
1084     if (!fontCollection)
1085         return InvalidParameter;
1086
1087     for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1088     GdipFree(*fontCollection);
1089
1090     return Ok;
1091 }
1092
1093 /*****************************************************************************
1094  * GdipPrivateAddFontFile [GDIPLUS.@]
1095  */
1096 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
1097         GDIPCONST WCHAR* filename)
1098 {
1099     FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
1100
1101     if (!(fontCollection && filename))
1102         return InvalidParameter;
1103
1104     return NotImplemented;
1105 }
1106
1107 /* Copied from msi/font.c */
1108
1109 typedef struct _tagTT_OFFSET_TABLE {
1110     USHORT uMajorVersion;
1111     USHORT uMinorVersion;
1112     USHORT uNumOfTables;
1113     USHORT uSearchRange;
1114     USHORT uEntrySelector;
1115     USHORT uRangeShift;
1116 } TT_OFFSET_TABLE;
1117
1118 typedef struct _tagTT_TABLE_DIRECTORY {
1119     char szTag[4]; /* table name */
1120     ULONG uCheckSum; /* Check sum */
1121     ULONG uOffset; /* Offset from beginning of file */
1122     ULONG uLength; /* length of the table in bytes */
1123 } TT_TABLE_DIRECTORY;
1124
1125 typedef struct _tagTT_NAME_TABLE_HEADER {
1126     USHORT uFSelector; /* format selector. Always 0 */
1127     USHORT uNRCount; /* Name Records count */
1128     USHORT uStorageOffset; /* Offset for strings storage,
1129                             * from start of the table */
1130 } TT_NAME_TABLE_HEADER;
1131
1132 #define NAME_ID_FULL_FONT_NAME  4
1133 #define NAME_ID_VERSION         5
1134
1135 typedef struct _tagTT_NAME_RECORD {
1136     USHORT uPlatformID;
1137     USHORT uEncodingID;
1138     USHORT uLanguageID;
1139     USHORT uNameID;
1140     USHORT uStringLength;
1141     USHORT uStringOffset; /* from start of storage area */
1142 } TT_NAME_RECORD;
1143
1144 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1145 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1146
1147 /*
1148  * Code based off of code located here
1149  * http://www.codeproject.com/gdi/fontnamefromfile.asp
1150  */
1151 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1152 {
1153     const TT_TABLE_DIRECTORY *tblDir;
1154     TT_OFFSET_TABLE ttOffsetTable;
1155     TT_NAME_TABLE_HEADER ttNTHeader;
1156     TT_NAME_RECORD ttRecord;
1157     DWORD ofs, pos;
1158     int i;
1159
1160     if (sizeof(TT_OFFSET_TABLE) > size)
1161         return NULL;
1162     ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1163     ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1164     ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1165     ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1166
1167     if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1168         return NULL;
1169
1170     pos = sizeof(ttOffsetTable);
1171     for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1172     {
1173         tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1174         pos += sizeof(*tblDir);
1175         if (memcmp(tblDir->szTag,"name",4)==0)
1176         {
1177             ofs = SWAPLONG(tblDir->uOffset);
1178             break;
1179         }
1180     }
1181     if (i >= ttOffsetTable.uNumOfTables)
1182         return NULL;
1183
1184     pos = ofs + sizeof(ttNTHeader);
1185     if (pos > size)
1186         return NULL;
1187     ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1188     ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1189     ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1190     for(i=0; i<ttNTHeader.uNRCount; i++)
1191     {
1192         ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1193         pos += sizeof(ttRecord);
1194         if (pos > size)
1195             return NULL;
1196
1197         ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1198         if (ttRecord.uNameID == id)
1199         {
1200             const char *buf;
1201
1202             ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1203             ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1204             if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1205                 return NULL;
1206             buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1207             len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1208             ret[len] = 0;
1209             return ret;
1210         }
1211     }
1212     return NULL;
1213 }
1214
1215 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1216
1217 /*****************************************************************************
1218  * GdipPrivateAddMemoryFont [GDIPLUS.@]
1219  */
1220 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1221         GDIPCONST void* memory, INT length)
1222 {
1223     WCHAR buf[32], *name;
1224     DWORD count = 0;
1225     HANDLE font;
1226     TRACE("%p, %p, %d\n", fontCollection, memory, length);
1227
1228     if (!fontCollection || !memory || !length)
1229         return InvalidParameter;
1230
1231     name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1232     if (!name)
1233         return OutOfMemory;
1234
1235     font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1236     TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1237     if (!font || !count)
1238         return InvalidParameter;
1239
1240     if (count)
1241     {
1242         HDC hdc;
1243         LOGFONTW lfw;
1244
1245         hdc = GetDC(0);
1246
1247         lfw.lfCharSet = DEFAULT_CHARSET;
1248         lstrcpyW(lfw.lfFaceName, name);
1249         lfw.lfPitchAndFamily = 0;
1250
1251         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1252         {
1253             ReleaseDC(0, hdc);
1254             return OutOfMemory;
1255         }
1256
1257         ReleaseDC(0, hdc);
1258     }
1259     return Ok;
1260 }
1261
1262 /*****************************************************************************
1263  * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1264  */
1265 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1266         GpFontCollection* fontCollection, INT* numFound)
1267 {
1268     TRACE("%p, %p\n", fontCollection, numFound);
1269
1270     if (!(fontCollection && numFound))
1271         return InvalidParameter;
1272
1273     *numFound = fontCollection->count;
1274     return Ok;
1275 }
1276
1277 /*****************************************************************************
1278  * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1279  */
1280 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1281         GpFontCollection* fontCollection, INT numSought,
1282         GpFontFamily* gpfamilies[], INT* numFound)
1283 {
1284     INT i;
1285     GpStatus stat=Ok;
1286
1287     TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1288
1289     if (!(fontCollection && gpfamilies && numFound))
1290         return InvalidParameter;
1291
1292     memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1293
1294     for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1295     {
1296         stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1297     }
1298
1299     if (stat == Ok)
1300         *numFound = i;
1301     else
1302     {
1303         int numToFree=i;
1304         for (i=0; i<numToFree; i++)
1305         {
1306             GdipDeleteFontFamily(gpfamilies[i]);
1307             gpfamilies[i] = NULL;
1308         }
1309     }
1310
1311     return stat;
1312 }
1313
1314 void free_installed_fonts(void)
1315 {
1316     while (installedFontCollection.count)
1317         GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1318     HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1319     installedFontCollection.FontFamilies = NULL;
1320     installedFontCollection.allocated = 0;
1321 }
1322
1323 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1324         DWORD type, LPARAM lParam)
1325 {
1326     GpFontCollection* fonts = (GpFontCollection*)lParam;
1327     int i;
1328
1329     if (type == RASTER_FONTTYPE)
1330         return 1;
1331
1332     /* skip duplicates */
1333     for (i=0; i<fonts->count; i++)
1334         if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1335             return 1;
1336
1337     if (fonts->allocated == fonts->count)
1338     {
1339         INT new_alloc_count = fonts->allocated+50;
1340         GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1341
1342         if (!new_family_list)
1343             return 0;
1344
1345         memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1346         HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1347         fonts->FontFamilies = new_family_list;
1348         fonts->allocated = new_alloc_count;
1349     }
1350
1351     if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1352         fonts->count++;
1353     else
1354         return 0;
1355
1356     return 1;
1357 }
1358
1359 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1360         GpFontCollection** fontCollection)
1361 {
1362     TRACE("(%p)\n",fontCollection);
1363
1364     if (!fontCollection)
1365         return InvalidParameter;
1366
1367     if (installedFontCollection.count == 0)
1368     {
1369         HDC hdc;
1370         LOGFONTW lfw;
1371
1372         hdc = GetDC(0);
1373
1374         lfw.lfCharSet = DEFAULT_CHARSET;
1375         lfw.lfFaceName[0] = 0;
1376         lfw.lfPitchAndFamily = 0;
1377
1378         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1379         {
1380             free_installed_fonts();
1381             ReleaseDC(0, hdc);
1382             return OutOfMemory;
1383         }
1384
1385         ReleaseDC(0, hdc);
1386     }
1387
1388     *fontCollection = &installedFontCollection;
1389
1390     return Ok;
1391 }