gdiplus: Use WIC to decode BMP files.
[wine] / dlls / gdiplus / font.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winnls.h"
25 #include "winreg.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
30
31 #include "objbase.h"
32
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
35
36 static const REAL mm_per_inch = 25.4;
37 static const REAL inch_per_point = 1.0/72.0;
38
39 static GpFontCollection installedFontCollection = {0};
40
41 static inline REAL get_dpi (void)
42 {
43     REAL dpi;
44     GpGraphics *graphics;
45     HDC hdc = GetDC(0);
46     GdipCreateFromHDC (hdc, &graphics);
47     GdipGetDpiX(graphics, &dpi);
48     GdipDeleteGraphics(graphics);
49     ReleaseDC (0, hdc);
50
51     return dpi;
52 }
53
54 static inline REAL point_to_pixel (REAL point)
55 {
56     return point * get_dpi() * inch_per_point;
57 }
58
59 static inline REAL inch_to_pixel (REAL inch)
60 {
61     return inch * get_dpi();
62 }
63
64 static inline REAL document_to_pixel (REAL doc)
65 {
66     return doc * (get_dpi() / 300.0); /* Per MSDN */
67 }
68
69 static inline REAL mm_to_pixel (REAL mm)
70 {
71     return mm * (get_dpi() / mm_per_inch);
72 }
73
74 /*******************************************************************************
75  * GdipCreateFont [GDIPLUS.@]
76  *
77  * Create a new font based off of a FontFamily
78  *
79  * PARAMS
80  *  *fontFamily     [I] Family to base the font off of
81  *  emSize          [I] Size of the font
82  *  style           [I] Bitwise OR of FontStyle enumeration
83  *  unit            [I] Unit emSize is measured in
84  *  **font          [I] the resulting Font object
85  *
86  * RETURNS
87  *  SUCCESS: Ok
88  *  FAILURE: InvalidParameter if fontfamily or font is NULL.
89  *  FAILURE: FontFamilyNotFound if an invalid FontFamily is given
90  *
91  * NOTES
92  *  UnitDisplay is unsupported.
93  *  emSize is stored separately from lfHeight, to hold the fraction.
94  */
95 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
96                         REAL emSize, INT style, Unit unit, GpFont **font)
97 {
98     WCHAR facename[LF_FACESIZE];
99     LOGFONTW* lfw;
100     const NEWTEXTMETRICW* tmw;
101     GpStatus stat;
102
103     if (!fontFamily || !font)
104         return InvalidParameter;
105
106     TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
107             debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
108
109     stat = GdipGetFamilyName (fontFamily, facename, 0);
110     if (stat != Ok) return stat;
111     *font = GdipAlloc(sizeof(GpFont));
112
113     tmw = &fontFamily->tmw;
114     lfw = &((*font)->lfw);
115     ZeroMemory(&(*lfw), sizeof(*lfw));
116
117     lfw->lfWeight = tmw->tmWeight;
118     lfw->lfItalic = tmw->tmItalic;
119     lfw->lfUnderline = tmw->tmUnderlined;
120     lfw->lfStrikeOut = tmw->tmStruckOut;
121     lfw->lfCharSet = tmw->tmCharSet;
122     lfw->lfPitchAndFamily = tmw->tmPitchAndFamily;
123     lstrcpynW(lfw->lfFaceName, facename, LF_FACESIZE);
124
125     switch (unit)
126     {
127         case UnitWorld:
128             /* FIXME: Figure out when World != Pixel */
129             lfw->lfHeight = emSize; break;
130         case UnitDisplay:
131             FIXME("Unknown behavior for UnitDisplay! Please report!\n");
132             /* FIXME: Figure out how this works...
133              * MSDN says that if "DISPLAY" is a monitor, then pixel should be
134              * used. That's not what I got. Tests on Windows revealed no output,
135              * and the tests in tests/font crash windows */
136             lfw->lfHeight = 0; break;
137         case UnitPixel:
138             lfw->lfHeight = emSize; break;
139         case UnitPoint:
140             lfw->lfHeight = point_to_pixel(emSize); break;
141         case UnitInch:
142             lfw->lfHeight = inch_to_pixel(emSize); break;
143         case UnitDocument:
144             lfw->lfHeight = document_to_pixel(emSize); break;
145         case UnitMillimeter:
146             lfw->lfHeight = mm_to_pixel(emSize); break;
147     }
148
149     lfw->lfHeight *= -1;
150
151     lfw->lfWeight = style & FontStyleBold ? 700 : 400;
152     lfw->lfItalic = style & FontStyleItalic;
153     lfw->lfUnderline = style & FontStyleUnderline;
154     lfw->lfStrikeOut = style & FontStyleStrikeout;
155
156     (*font)->unit = unit;
157     (*font)->emSize = emSize;
158     (*font)->height = tmw->ntmSizeEM;
159     (*font)->line_spacing = tmw->tmAscent + tmw->tmDescent + tmw->tmExternalLeading;
160
161     return Ok;
162 }
163
164 /*******************************************************************************
165  * GdipCreateFontFromLogfontW [GDIPLUS.@]
166  */
167 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
168     GDIPCONST LOGFONTW *logfont, GpFont **font)
169 {
170     HFONT hfont, oldfont;
171     TEXTMETRICW textmet;
172
173     TRACE("(%p, %p, %p)\n", hdc, logfont, font);
174
175     if(!logfont || !font)
176         return InvalidParameter;
177
178     if (logfont->lfFaceName[0] == 0)
179         return NotTrueTypeFont;
180
181     *font = GdipAlloc(sizeof(GpFont));
182     if(!*font)  return OutOfMemory;
183
184     memcpy((*font)->lfw.lfFaceName, logfont->lfFaceName, LF_FACESIZE *
185            sizeof(WCHAR));
186     (*font)->lfw.lfHeight = logfont->lfHeight;
187     (*font)->lfw.lfItalic = logfont->lfItalic;
188     (*font)->lfw.lfUnderline = logfont->lfUnderline;
189     (*font)->lfw.lfStrikeOut = logfont->lfStrikeOut;
190
191     (*font)->emSize = logfont->lfHeight;
192     (*font)->unit = UnitPixel;
193
194     hfont = CreateFontIndirectW(&(*font)->lfw);
195     oldfont = SelectObject(hdc, hfont);
196     GetTextMetricsW(hdc, &textmet);
197
198     (*font)->lfw.lfHeight = -(textmet.tmHeight-textmet.tmInternalLeading);
199     (*font)->lfw.lfWeight = textmet.tmWeight;
200     (*font)->lfw.lfCharSet = textmet.tmCharSet;
201
202     (*font)->height = 1; /* FIXME: need NEWTEXTMETRIC.ntmSizeEM here */
203     (*font)->line_spacing = textmet.tmAscent + textmet.tmDescent + textmet.tmExternalLeading;
204
205     SelectObject(hdc, oldfont);
206     DeleteObject(hfont);
207
208     return Ok;
209 }
210
211 /*******************************************************************************
212  * GdipCreateFontFromLogfontA [GDIPLUS.@]
213  */
214 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
215     GDIPCONST LOGFONTA *lfa, GpFont **font)
216 {
217     LOGFONTW lfw;
218
219     TRACE("(%p, %p, %p)\n", hdc, lfa, font);
220
221     if(!lfa || !font)
222         return InvalidParameter;
223
224     memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
225
226     if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
227         return GenericError;
228
229     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
230 }
231
232 /*******************************************************************************
233  * GdipDeleteFont [GDIPLUS.@]
234  */
235 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
236 {
237     TRACE("(%p)\n", font);
238
239     if(!font)
240         return InvalidParameter;
241
242     GdipFree(font);
243
244     return Ok;
245 }
246
247 /*******************************************************************************
248  * GdipCreateFontFromDC [GDIPLUS.@]
249  */
250 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
251 {
252     HFONT hfont;
253     LOGFONTW lfw;
254
255     TRACE("(%p, %p)\n", hdc, font);
256
257     if(!font)
258         return InvalidParameter;
259
260     hfont = GetCurrentObject(hdc, OBJ_FONT);
261     if(!hfont)
262         return GenericError;
263
264     if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
265         return GenericError;
266
267     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
268 }
269
270 /*******************************************************************************
271  * GdipGetFamily [GDIPLUS.@]
272  *
273  * Returns the FontFamily for the specified Font
274  *
275  * PARAMS
276  *  font    [I] Font to request from
277  *  family  [O] Resulting FontFamily object
278  *
279  * RETURNS
280  *  SUCCESS: Ok
281  *  FAILURE: An element of GpStatus
282  */
283 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
284 {
285     TRACE("%p %p\n", font, family);
286
287     if (!(font && family))
288         return InvalidParameter;
289
290     return GdipCreateFontFamilyFromName(font->lfw.lfFaceName, NULL, family);
291 }
292
293 /******************************************************************************
294  * GdipGetFontSize [GDIPLUS.@]
295  *
296  * Returns the size of the font in Units
297  *
298  * PARAMS
299  *  *font       [I] The font to retrieve size from
300  *  *size       [O] Pointer to hold retrieved value
301  *
302  * RETURNS
303  *  SUCCESS: Ok
304  *  FAILURE: InvalidParameter (font or size was NULL)
305  *
306  * NOTES
307  *  Size returned is actually emSize -- not internal size used for drawing.
308  */
309 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
310 {
311     TRACE("(%p, %p)\n", font, size);
312
313     if (!(font && size)) return InvalidParameter;
314
315     *size = font->emSize;
316
317     return Ok;
318 }
319
320 /*******************************************************************************
321  * GdipGetFontStyle [GDIPLUS.@]
322  *
323  * Gets the font's style, returned in bitwise OR of FontStyle enumeration
324  *
325  * PARAMS
326  *  font    [I] font to request from
327  *  style   [O] resulting pointer to a FontStyle enumeration
328  *
329  * RETURNS
330  *  SUCCESS: Ok
331  *  FAILURE: InvalidParameter
332  */
333 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
334 {
335     TRACE("%p %p\n", font, style);
336
337     if (!(font && style))
338         return InvalidParameter;
339
340     if (font->lfw.lfWeight > 400)
341         *style = FontStyleBold;
342     else
343         *style = 0;
344     if (font->lfw.lfItalic)
345         *style |= FontStyleItalic;
346     if (font->lfw.lfUnderline)
347         *style |= FontStyleUnderline;
348     if (font->lfw.lfStrikeOut)
349         *style |= FontStyleStrikeout;
350
351     return Ok;
352 }
353
354 /*******************************************************************************
355  * GdipGetFontUnit  [GDIPLUS.@]
356  *
357  * PARAMS
358  *  font    [I] Font to retrieve from
359  *  unit    [O] Return value
360  *
361  * RETURNS
362  *  FAILURE: font or unit was NULL
363  *  OK: otherwise
364  */
365 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
366 {
367     TRACE("(%p, %p)\n", font, unit);
368
369     if (!(font && unit)) return InvalidParameter;
370
371     *unit = font->unit;
372
373     return Ok;
374 }
375
376 /*******************************************************************************
377  * GdipGetLogFontA [GDIPLUS.@]
378  */
379 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
380     LOGFONTA *lfa)
381 {
382     GpStatus status;
383     LOGFONTW lfw;
384
385     TRACE("(%p, %p, %p)\n", font, graphics, lfa);
386
387     status = GdipGetLogFontW(font, graphics, &lfw);
388     if(status != Ok)
389         return status;
390
391     memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
392
393     if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
394         return GenericError;
395
396     return Ok;
397 }
398
399 /*******************************************************************************
400  * GdipGetLogFontW [GDIPLUS.@]
401  */
402 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
403     LOGFONTW *lfw)
404 {
405     TRACE("(%p, %p, %p)\n", font, graphics, lfw);
406
407     /* FIXME: use graphics */
408     if(!font || !graphics || !lfw)
409         return InvalidParameter;
410
411     *lfw = font->lfw;
412
413     return Ok;
414 }
415
416 /*******************************************************************************
417  * GdipCloneFont [GDIPLUS.@]
418  */
419 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
420 {
421     TRACE("(%p, %p)\n", font, cloneFont);
422
423     if(!font || !cloneFont)
424         return InvalidParameter;
425
426     *cloneFont = GdipAlloc(sizeof(GpFont));
427     if(!*cloneFont)    return OutOfMemory;
428
429     **cloneFont = *font;
430
431     return Ok;
432 }
433
434 /*******************************************************************************
435  * GdipGetFontHeight [GDIPLUS.@]
436  * PARAMS
437  *  font        [I] Font to retrieve height from
438  *  graphics    [I] The current graphics context
439  *  height      [O] Resulting height
440  * RETURNS
441  *  SUCCESS: Ok
442  *  FAILURE: Another element of GpStatus
443  *
444  * NOTES
445  *  Forwards to GdipGetFontHeightGivenDPI
446  */
447 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
448         GDIPCONST GpGraphics *graphics, REAL *height)
449 {
450     REAL dpi;
451
452     TRACE("%p %p %p\n", font, graphics, height);
453
454     dpi = GetDeviceCaps(graphics->hdc, LOGPIXELSY);
455
456     return GdipGetFontHeightGivenDPI(font, dpi, height);
457 }
458
459 /*******************************************************************************
460  * GdipGetFontHeightGivenDPI [GDIPLUS.@]
461  * PARAMS
462  *  font        [I] Font to retrieve DPI from
463  *  dpi         [I] DPI to assume
464  *  height      [O] Return value
465  *
466  * RETURNS
467  *  SUCCESS: Ok
468  *  FAILURE: InvalidParameter if font or height is NULL
469  *
470  * NOTES
471  *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
472  *  (for anything other than unit Pixel)
473  */
474 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
475 {
476     REAL font_height;
477
478     TRACE("%p (%s), %f, %p\n", font,
479             debugstr_w(font->lfw.lfFaceName), dpi, height);
480
481     if (!(font && height)) return InvalidParameter;
482
483     font_height = font->line_spacing * (font->emSize / font->height);
484
485     switch (font->unit)
486     {
487         case UnitPixel:
488             *height = font_height;
489             break;
490         case UnitPoint:
491             *height = font_height * dpi * inch_per_point;
492             break;
493         case UnitInch:
494             *height = font_height * dpi;
495             break;
496         case UnitDocument:
497             *height = font_height * (dpi / 300.0);
498             break;
499         case UnitMillimeter:
500             *height = font_height * (dpi / mm_per_inch);
501             break;
502         default:
503             FIXME("Unhandled unit type: %d\n", font->unit);
504             return NotImplemented;
505     }
506
507     return Ok;
508 }
509
510 /***********************************************************************
511  * Borrowed from GDI32:
512  *
513  * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
514  *     We have to use other types because of the FONTENUMPROCW definition.
515  */
516 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
517                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
518 {
519     if (!ntm)
520     {
521         return 1;
522     }
523
524     *(NEWTEXTMETRICW*)lParam = *(const NEWTEXTMETRICW*)ntm;
525
526     return 0;
527 }
528
529 static BOOL find_installed_font(const WCHAR *name, NEWTEXTMETRICW *ntm)
530 {
531     HDC hdc = GetDC(0);
532     BOOL ret = FALSE;
533
534     if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)ntm))
535         ret = TRUE;
536
537     ReleaseDC(0, hdc);
538     return ret;
539 }
540
541 /*******************************************************************************
542  * GdipCreateFontFamilyFromName [GDIPLUS.@]
543  *
544  * Creates a font family object based on a supplied name
545  *
546  * PARAMS
547  *  name               [I] Name of the font
548  *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
549  *  FontFamily         [O] Pointer to the resulting FontFamily object
550  *
551  * RETURNS
552  *  SUCCESS: Ok
553  *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
554  *  FAILURE: Invalid parameter if FontFamily or name is NULL
555  *
556  * NOTES
557  *   If fontCollection is NULL then the object is not part of any collection
558  *
559  */
560
561 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
562                                         GpFontCollection *fontCollection,
563                                         GpFontFamily **FontFamily)
564 {
565     GpFontFamily* ffamily;
566     NEWTEXTMETRICW ntm;
567
568     TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
569
570     if (!(name && FontFamily))
571         return InvalidParameter;
572     if (fontCollection)
573         FIXME("No support for FontCollections yet!\n");
574
575     if (!find_installed_font(name, &ntm))
576         return FontFamilyNotFound;
577
578     ffamily = GdipAlloc(sizeof (GpFontFamily));
579     if (!ffamily) return OutOfMemory;
580
581     ffamily->tmw = ntm;
582     lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
583
584     *FontFamily = ffamily;
585
586     return Ok;
587 }
588
589 /*******************************************************************************
590  * GdipCloneFontFamily [GDIPLUS.@]
591  *
592  * Creates a deep copy of a Font Family object
593  *
594  * PARAMS
595  *  FontFamily          [I] Font to clone
596  *  clonedFontFamily    [O] The resulting cloned font
597  *
598  * RETURNS
599  *  SUCCESS: Ok
600  */
601 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
602 {
603     if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
604
605     TRACE("stub: %p (%s), %p\n", FontFamily,
606             debugstr_w(FontFamily->FamilyName), clonedFontFamily);
607
608     *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
609     if (!*clonedFontFamily) return OutOfMemory;
610
611     (*clonedFontFamily)->tmw = FontFamily->tmw;
612     lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
613
614     return Ok;
615 }
616
617 /*******************************************************************************
618  * GdipGetFamilyName [GDIPLUS.@]
619  *
620  * Returns the family name into name
621  *
622  * PARAMS
623  *  *family     [I] Family to retrieve from
624  *  *name       [O] WCHARS of the family name
625  *  LANGID      [I] charset
626  *
627  * RETURNS
628  *  SUCCESS: Ok
629  *  FAILURE: InvalidParameter if family is NULL
630  *
631  * NOTES
632  *   If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
633  */
634 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
635                                        WCHAR *name, LANGID language)
636 {
637     if (family == NULL)
638          return InvalidParameter;
639
640     TRACE("%p, %p, %d\n", family, name, language);
641
642     if (language != LANG_NEUTRAL)
643         FIXME("No support for handling of multiple languages!\n");
644
645     lstrcpynW (name, family->FamilyName, LF_FACESIZE);
646
647     return Ok;
648 }
649
650
651 /*****************************************************************************
652  * GdipDeleteFontFamily [GDIPLUS.@]
653  *
654  * Removes the specified FontFamily
655  *
656  * PARAMS
657  *  *FontFamily         [I] The family to delete
658  *
659  * RETURNS
660  *  SUCCESS: Ok
661  *  FAILURE: InvalidParameter if FontFamily is NULL.
662  *
663  */
664 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
665 {
666     if (!FontFamily)
667         return InvalidParameter;
668     TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
669
670     GdipFree (FontFamily);
671
672     return Ok;
673 }
674
675 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
676         INT style, UINT16* CellAscent)
677 {
678     if (!(family && CellAscent)) return InvalidParameter;
679
680     *CellAscent = family->tmw.tmAscent;
681
682     return Ok;
683 }
684
685 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
686         INT style, UINT16* CellDescent)
687 {
688     TRACE("(%p, %d, %p)\n", family, style, CellDescent);
689
690     if (!(family && CellDescent)) return InvalidParameter;
691
692     *CellDescent = family->tmw.tmDescent;
693
694     return Ok;
695 }
696
697 /*******************************************************************************
698  * GdipGetEmHeight [GDIPLUS.@]
699  *
700  * Gets the height of the specified family in EmHeights
701  *
702  * PARAMS
703  *  family      [I] Family to retrieve from
704  *  style       [I] (optional) style
705  *  EmHeight    [O] return value
706  *
707  * RETURNS
708  *  SUCCESS: Ok
709  *  FAILURE: InvalidParameter
710  */
711 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
712 {
713     if (!(family && EmHeight)) return InvalidParameter;
714
715     TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
716
717     *EmHeight = family->tmw.ntmSizeEM;
718
719     return Ok;
720 }
721
722
723 /*******************************************************************************
724  * GdipGetLineSpacing [GDIPLUS.@]
725  *
726  * Returns the line spacing in design units
727  *
728  * PARAMS
729  *  family      [I] Family to retrieve from
730  *  style       [I] (Optional) font style
731  *  LineSpacing [O] Return value
732  *
733  * RETURNS
734  *  SUCCESS: Ok
735  *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
736  */
737 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
738         INT style, UINT16* LineSpacing)
739 {
740     TRACE("%p, %d, %p\n", family, style, LineSpacing);
741
742     if (!(family && LineSpacing))
743         return InvalidParameter;
744
745     if (style) FIXME("ignoring style\n");
746
747     *LineSpacing = family->tmw.tmAscent + family->tmw.tmDescent + family->tmw.tmExternalLeading;
748
749     return Ok;
750 }
751
752 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
753         INT style, BOOL* IsStyleAvailable)
754 {
755     FIXME("%p %d %p stub!\n", family, style, IsStyleAvailable);
756
757     if (!(family && IsStyleAvailable))
758         return InvalidParameter;
759
760     return NotImplemented;
761 }
762
763 /*****************************************************************************
764  * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
765  *
766  * Obtains a serif family (Courier New on Windows)
767  *
768  * PARAMS
769  *  **nativeFamily         [I] Where the font will be stored
770  *
771  * RETURNS
772  *  InvalidParameter if nativeFamily is NULL.
773  *  Ok otherwise.
774  */
775 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
776 {
777     static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
778
779     if (nativeFamily == NULL) return InvalidParameter;
780
781     return GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
782 }
783
784 /*****************************************************************************
785  * GdipGetGenericFontFamilySerif [GDIPLUS.@]
786  *
787  * Obtains a serif family (Times New Roman on Windows)
788  *
789  * PARAMS
790  *  **nativeFamily         [I] Where the font will be stored
791  *
792  * RETURNS
793  *  InvalidParameter if nativeFamily is NULL.
794  *  Ok otherwise.
795  */
796 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
797 {
798     static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
799
800     TRACE("(%p)\n", nativeFamily);
801
802     if (nativeFamily == NULL) return InvalidParameter;
803
804     return GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
805 }
806
807 /*****************************************************************************
808  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
809  *
810  * Obtains a serif family (Microsoft Sans Serif on Windows)
811  *
812  * PARAMS
813  *  **nativeFamily         [I] Where the font will be stored
814  *
815  * RETURNS
816  *  InvalidParameter if nativeFamily is NULL.
817  *  Ok otherwise.
818  */
819 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
820 {
821     /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
822      * affect anything */
823     static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
824
825     TRACE("(%p)\n", nativeFamily);
826
827     if (nativeFamily == NULL) return InvalidParameter;
828
829     return GdipCreateFontFamilyFromName(MSSansSerif, NULL, nativeFamily);
830 }
831
832 /*****************************************************************************
833  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
834  */
835 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
836 {
837     TRACE("%p\n", fontCollection);
838
839     if (!fontCollection)
840         return InvalidParameter;
841
842     *fontCollection = GdipAlloc(sizeof(GpFontCollection));
843     if (!*fontCollection) return OutOfMemory;
844
845     (*fontCollection)->FontFamilies = NULL;
846     (*fontCollection)->count = 0;
847     (*fontCollection)->allocated = 0;
848     return Ok;
849 }
850
851 /*****************************************************************************
852  * GdipDeletePrivateFontCollection [GDIPLUS.@]
853  */
854 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
855 {
856     INT i;
857
858     TRACE("%p\n", fontCollection);
859
860     if (!fontCollection)
861         return InvalidParameter;
862
863     for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
864     GdipFree(*fontCollection);
865
866     return Ok;
867 }
868
869 /*****************************************************************************
870  * GdipPrivateAddFontFile [GDIPLUS.@]
871  */
872 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
873         GDIPCONST WCHAR* filename)
874 {
875     FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
876
877     if (!(fontCollection && filename))
878         return InvalidParameter;
879
880     return NotImplemented;
881 }
882
883 /*****************************************************************************
884  * GdipPrivateAddMemoryFont [GDIPLUS.@]
885  */
886 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
887         GDIPCONST void* memory, INT length)
888 {
889     FIXME("%p, %p, %d\n", fontCollection, memory, length);
890
891     if (!(fontCollection && memory && length))
892         return InvalidParameter;
893
894     return Ok;
895 }
896
897 /*****************************************************************************
898  * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
899  */
900 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
901         GpFontCollection* fontCollection, INT* numFound)
902 {
903     TRACE("%p, %p\n", fontCollection, numFound);
904
905     if (!(fontCollection && numFound))
906         return InvalidParameter;
907
908     *numFound = fontCollection->count;
909     return Ok;
910 }
911
912 /*****************************************************************************
913  * GdipGetFontCollectionFamilyList [GDIPLUS.@]
914  */
915 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
916         GpFontCollection* fontCollection, INT numSought,
917         GpFontFamily* gpfamilies[], INT* numFound)
918 {
919     INT i;
920
921     TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
922
923     if (!(fontCollection && gpfamilies && numFound))
924         return InvalidParameter;
925
926     for (i = 0; i < numSought && i < fontCollection->count; i++)
927     {
928         gpfamilies[i] = fontCollection->FontFamilies[i];
929     }
930     *numFound = i;
931     return Ok;
932 }
933
934 void free_installed_fonts(void)
935 {
936     while (installedFontCollection.count)
937         GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
938     HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
939     installedFontCollection.FontFamilies = NULL;
940     installedFontCollection.allocated = 0;
941 }
942
943 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
944         DWORD type, LPARAM lParam)
945 {
946     GpFontCollection* fonts = (GpFontCollection*)lParam;
947     int i;
948
949     /* skip duplicates */
950     for (i=0; i<fonts->count; i++)
951         if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
952             return 1;
953
954     if (fonts->allocated == fonts->count)
955     {
956         INT new_alloc_count = fonts->allocated+50;
957         GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
958
959         if (!new_family_list)
960             return 0;
961
962         memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
963         HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
964         fonts->FontFamilies = new_family_list;
965         fonts->allocated = new_alloc_count;
966     }
967
968     if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
969         fonts->count++;
970     else
971         return 0;
972
973     return 1;
974 }
975
976 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
977         GpFontCollection** fontCollection)
978 {
979     TRACE("(%p)\n",fontCollection);
980
981     if (!fontCollection)
982         return InvalidParameter;
983
984     if (installedFontCollection.count == 0)
985     {
986         HDC hdc;
987         LOGFONTW lfw;
988
989         hdc = GetDC(0);
990
991         lfw.lfCharSet = DEFAULT_CHARSET;
992         lfw.lfFaceName[0] = 0;
993         lfw.lfPitchAndFamily = 0;
994
995         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
996         {
997             free_installed_fonts();
998             ReleaseDC(0, hdc);
999             return OutOfMemory;
1000         }
1001
1002         ReleaseDC(0, hdc);
1003     }
1004
1005     *fontCollection = &installedFontCollection;
1006
1007     return Ok;
1008 }