gdiplus: Add support for decoding TIFF images.
[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     TRACE("<-- %p\n", *font);
162
163     return Ok;
164 }
165
166 /*******************************************************************************
167  * GdipCreateFontFromLogfontW [GDIPLUS.@]
168  */
169 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
170     GDIPCONST LOGFONTW *logfont, GpFont **font)
171 {
172     HFONT hfont, oldfont;
173     TEXTMETRICW textmet;
174
175     TRACE("(%p, %p, %p)\n", hdc, logfont, font);
176
177     if(!logfont || !font)
178         return InvalidParameter;
179
180     if (logfont->lfFaceName[0] == 0)
181         return NotTrueTypeFont;
182
183     *font = GdipAlloc(sizeof(GpFont));
184     if(!*font)  return OutOfMemory;
185
186     memcpy((*font)->lfw.lfFaceName, logfont->lfFaceName, LF_FACESIZE *
187            sizeof(WCHAR));
188     (*font)->lfw.lfHeight = logfont->lfHeight;
189     (*font)->lfw.lfItalic = logfont->lfItalic;
190     (*font)->lfw.lfUnderline = logfont->lfUnderline;
191     (*font)->lfw.lfStrikeOut = logfont->lfStrikeOut;
192
193     (*font)->emSize = logfont->lfHeight;
194     (*font)->unit = UnitPixel;
195
196     hfont = CreateFontIndirectW(&(*font)->lfw);
197     oldfont = SelectObject(hdc, hfont);
198     GetTextMetricsW(hdc, &textmet);
199
200     (*font)->lfw.lfHeight = -(textmet.tmHeight-textmet.tmInternalLeading);
201     (*font)->lfw.lfWeight = textmet.tmWeight;
202     (*font)->lfw.lfCharSet = textmet.tmCharSet;
203
204     (*font)->height = 1; /* FIXME: need NEWTEXTMETRIC.ntmSizeEM here */
205     (*font)->line_spacing = textmet.tmAscent + textmet.tmDescent + textmet.tmExternalLeading;
206
207     SelectObject(hdc, oldfont);
208     DeleteObject(hfont);
209
210     TRACE("<-- %p\n", *font);
211
212     return Ok;
213 }
214
215 /*******************************************************************************
216  * GdipCreateFontFromLogfontA [GDIPLUS.@]
217  */
218 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
219     GDIPCONST LOGFONTA *lfa, GpFont **font)
220 {
221     LOGFONTW lfw;
222
223     TRACE("(%p, %p, %p)\n", hdc, lfa, font);
224
225     if(!lfa || !font)
226         return InvalidParameter;
227
228     memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
229
230     if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
231         return GenericError;
232
233     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
234 }
235
236 /*******************************************************************************
237  * GdipDeleteFont [GDIPLUS.@]
238  */
239 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
240 {
241     TRACE("(%p)\n", font);
242
243     if(!font)
244         return InvalidParameter;
245
246     GdipFree(font);
247
248     return Ok;
249 }
250
251 /*******************************************************************************
252  * GdipCreateFontFromDC [GDIPLUS.@]
253  */
254 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
255 {
256     HFONT hfont;
257     LOGFONTW lfw;
258
259     TRACE("(%p, %p)\n", hdc, font);
260
261     if(!font)
262         return InvalidParameter;
263
264     hfont = GetCurrentObject(hdc, OBJ_FONT);
265     if(!hfont)
266         return GenericError;
267
268     if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
269         return GenericError;
270
271     return GdipCreateFontFromLogfontW(hdc, &lfw, font);
272 }
273
274 /*******************************************************************************
275  * GdipGetFamily [GDIPLUS.@]
276  *
277  * Returns the FontFamily for the specified Font
278  *
279  * PARAMS
280  *  font    [I] Font to request from
281  *  family  [O] Resulting FontFamily object
282  *
283  * RETURNS
284  *  SUCCESS: Ok
285  *  FAILURE: An element of GpStatus
286  */
287 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
288 {
289     TRACE("%p %p\n", font, family);
290
291     if (!(font && family))
292         return InvalidParameter;
293
294     return GdipCreateFontFamilyFromName(font->lfw.lfFaceName, NULL, family);
295 }
296
297 /******************************************************************************
298  * GdipGetFontSize [GDIPLUS.@]
299  *
300  * Returns the size of the font in Units
301  *
302  * PARAMS
303  *  *font       [I] The font to retrieve size from
304  *  *size       [O] Pointer to hold retrieved value
305  *
306  * RETURNS
307  *  SUCCESS: Ok
308  *  FAILURE: InvalidParameter (font or size was NULL)
309  *
310  * NOTES
311  *  Size returned is actually emSize -- not internal size used for drawing.
312  */
313 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
314 {
315     TRACE("(%p, %p)\n", font, size);
316
317     if (!(font && size)) return InvalidParameter;
318
319     *size = font->emSize;
320
321     return Ok;
322 }
323
324 /*******************************************************************************
325  * GdipGetFontStyle [GDIPLUS.@]
326  *
327  * Gets the font's style, returned in bitwise OR of FontStyle enumeration
328  *
329  * PARAMS
330  *  font    [I] font to request from
331  *  style   [O] resulting pointer to a FontStyle enumeration
332  *
333  * RETURNS
334  *  SUCCESS: Ok
335  *  FAILURE: InvalidParameter
336  */
337 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
338 {
339     TRACE("%p %p\n", font, style);
340
341     if (!(font && style))
342         return InvalidParameter;
343
344     if (font->lfw.lfWeight > 400)
345         *style = FontStyleBold;
346     else
347         *style = 0;
348     if (font->lfw.lfItalic)
349         *style |= FontStyleItalic;
350     if (font->lfw.lfUnderline)
351         *style |= FontStyleUnderline;
352     if (font->lfw.lfStrikeOut)
353         *style |= FontStyleStrikeout;
354
355     return Ok;
356 }
357
358 /*******************************************************************************
359  * GdipGetFontUnit  [GDIPLUS.@]
360  *
361  * PARAMS
362  *  font    [I] Font to retrieve from
363  *  unit    [O] Return value
364  *
365  * RETURNS
366  *  FAILURE: font or unit was NULL
367  *  OK: otherwise
368  */
369 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
370 {
371     TRACE("(%p, %p)\n", font, unit);
372
373     if (!(font && unit)) return InvalidParameter;
374
375     *unit = font->unit;
376
377     return Ok;
378 }
379
380 /*******************************************************************************
381  * GdipGetLogFontA [GDIPLUS.@]
382  */
383 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
384     LOGFONTA *lfa)
385 {
386     GpStatus status;
387     LOGFONTW lfw;
388
389     TRACE("(%p, %p, %p)\n", font, graphics, lfa);
390
391     status = GdipGetLogFontW(font, graphics, &lfw);
392     if(status != Ok)
393         return status;
394
395     memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
396
397     if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
398         return GenericError;
399
400     return Ok;
401 }
402
403 /*******************************************************************************
404  * GdipGetLogFontW [GDIPLUS.@]
405  */
406 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
407     LOGFONTW *lfw)
408 {
409     TRACE("(%p, %p, %p)\n", font, graphics, lfw);
410
411     /* FIXME: use graphics */
412     if(!font || !graphics || !lfw)
413         return InvalidParameter;
414
415     *lfw = font->lfw;
416
417     return Ok;
418 }
419
420 /*******************************************************************************
421  * GdipCloneFont [GDIPLUS.@]
422  */
423 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
424 {
425     TRACE("(%p, %p)\n", font, cloneFont);
426
427     if(!font || !cloneFont)
428         return InvalidParameter;
429
430     *cloneFont = GdipAlloc(sizeof(GpFont));
431     if(!*cloneFont)    return OutOfMemory;
432
433     **cloneFont = *font;
434
435     return Ok;
436 }
437
438 /*******************************************************************************
439  * GdipGetFontHeight [GDIPLUS.@]
440  * PARAMS
441  *  font        [I] Font to retrieve height from
442  *  graphics    [I] The current graphics context
443  *  height      [O] Resulting height
444  * RETURNS
445  *  SUCCESS: Ok
446  *  FAILURE: Another element of GpStatus
447  *
448  * NOTES
449  *  Forwards to GdipGetFontHeightGivenDPI
450  */
451 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
452         GDIPCONST GpGraphics *graphics, REAL *height)
453 {
454     REAL dpi;
455
456     TRACE("%p %p %p\n", font, graphics, height);
457
458     dpi = GetDeviceCaps(graphics->hdc, LOGPIXELSY);
459
460     return GdipGetFontHeightGivenDPI(font, dpi, height);
461 }
462
463 /*******************************************************************************
464  * GdipGetFontHeightGivenDPI [GDIPLUS.@]
465  * PARAMS
466  *  font        [I] Font to retrieve DPI from
467  *  dpi         [I] DPI to assume
468  *  height      [O] Return value
469  *
470  * RETURNS
471  *  SUCCESS: Ok
472  *  FAILURE: InvalidParameter if font or height is NULL
473  *
474  * NOTES
475  *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
476  *  (for anything other than unit Pixel)
477  */
478 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
479 {
480     REAL font_height;
481
482     TRACE("%p (%s), %f, %p\n", font,
483             debugstr_w(font->lfw.lfFaceName), dpi, height);
484
485     if (!(font && height)) return InvalidParameter;
486
487     font_height = font->line_spacing * (font->emSize / font->height);
488
489     switch (font->unit)
490     {
491         case UnitPixel:
492             *height = font_height;
493             break;
494         case UnitPoint:
495             *height = font_height * dpi * inch_per_point;
496             break;
497         case UnitInch:
498             *height = font_height * dpi;
499             break;
500         case UnitDocument:
501             *height = font_height * (dpi / 300.0);
502             break;
503         case UnitMillimeter:
504             *height = font_height * (dpi / mm_per_inch);
505             break;
506         default:
507             FIXME("Unhandled unit type: %d\n", font->unit);
508             return NotImplemented;
509     }
510
511     return Ok;
512 }
513
514 /***********************************************************************
515  * Borrowed from GDI32:
516  *
517  * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
518  *     We have to use other types because of the FONTENUMPROCW definition.
519  */
520 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
521                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
522 {
523     if (!ntm)
524     {
525         return 1;
526     }
527
528     *(NEWTEXTMETRICW*)lParam = *(const NEWTEXTMETRICW*)ntm;
529
530     return 0;
531 }
532
533 static BOOL find_installed_font(const WCHAR *name, NEWTEXTMETRICW *ntm)
534 {
535     HDC hdc = GetDC(0);
536     BOOL ret = FALSE;
537
538     if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)ntm))
539         ret = TRUE;
540
541     ReleaseDC(0, hdc);
542     return ret;
543 }
544
545 /*******************************************************************************
546  * GdipCreateFontFamilyFromName [GDIPLUS.@]
547  *
548  * Creates a font family object based on a supplied name
549  *
550  * PARAMS
551  *  name               [I] Name of the font
552  *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
553  *  FontFamily         [O] Pointer to the resulting FontFamily object
554  *
555  * RETURNS
556  *  SUCCESS: Ok
557  *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
558  *  FAILURE: Invalid parameter if FontFamily or name is NULL
559  *
560  * NOTES
561  *   If fontCollection is NULL then the object is not part of any collection
562  *
563  */
564
565 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
566                                         GpFontCollection *fontCollection,
567                                         GpFontFamily **FontFamily)
568 {
569     GpFontFamily* ffamily;
570     NEWTEXTMETRICW ntm;
571
572     TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
573
574     if (!(name && FontFamily))
575         return InvalidParameter;
576     if (fontCollection)
577         FIXME("No support for FontCollections yet!\n");
578
579     if (!find_installed_font(name, &ntm))
580         return FontFamilyNotFound;
581
582     ffamily = GdipAlloc(sizeof (GpFontFamily));
583     if (!ffamily) return OutOfMemory;
584
585     ffamily->tmw = ntm;
586     lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
587
588     *FontFamily = ffamily;
589
590     TRACE("<-- %p\n", ffamily);
591
592     return Ok;
593 }
594
595 /*******************************************************************************
596  * GdipCloneFontFamily [GDIPLUS.@]
597  *
598  * Creates a deep copy of a Font Family object
599  *
600  * PARAMS
601  *  FontFamily          [I] Font to clone
602  *  clonedFontFamily    [O] The resulting cloned font
603  *
604  * RETURNS
605  *  SUCCESS: Ok
606  */
607 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
608 {
609     if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
610
611     TRACE("stub: %p (%s), %p\n", FontFamily,
612             debugstr_w(FontFamily->FamilyName), clonedFontFamily);
613
614     *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
615     if (!*clonedFontFamily) return OutOfMemory;
616
617     (*clonedFontFamily)->tmw = FontFamily->tmw;
618     lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
619
620     TRACE("<-- %p\n", *clonedFontFamily);
621
622     return Ok;
623 }
624
625 /*******************************************************************************
626  * GdipGetFamilyName [GDIPLUS.@]
627  *
628  * Returns the family name into name
629  *
630  * PARAMS
631  *  *family     [I] Family to retrieve from
632  *  *name       [O] WCHARS of the family name
633  *  LANGID      [I] charset
634  *
635  * RETURNS
636  *  SUCCESS: Ok
637  *  FAILURE: InvalidParameter if family is NULL
638  *
639  * NOTES
640  *   If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
641  */
642 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
643                                        WCHAR *name, LANGID language)
644 {
645     if (family == NULL)
646          return InvalidParameter;
647
648     TRACE("%p, %p, %d\n", family, name, language);
649
650     if (language != LANG_NEUTRAL)
651         FIXME("No support for handling of multiple languages!\n");
652
653     lstrcpynW (name, family->FamilyName, LF_FACESIZE);
654
655     return Ok;
656 }
657
658
659 /*****************************************************************************
660  * GdipDeleteFontFamily [GDIPLUS.@]
661  *
662  * Removes the specified FontFamily
663  *
664  * PARAMS
665  *  *FontFamily         [I] The family to delete
666  *
667  * RETURNS
668  *  SUCCESS: Ok
669  *  FAILURE: InvalidParameter if FontFamily is NULL.
670  *
671  */
672 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
673 {
674     if (!FontFamily)
675         return InvalidParameter;
676     TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
677
678     GdipFree (FontFamily);
679
680     return Ok;
681 }
682
683 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
684         INT style, UINT16* CellAscent)
685 {
686     if (!(family && CellAscent)) return InvalidParameter;
687
688     *CellAscent = family->tmw.tmAscent;
689
690     return Ok;
691 }
692
693 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
694         INT style, UINT16* CellDescent)
695 {
696     TRACE("(%p, %d, %p)\n", family, style, CellDescent);
697
698     if (!(family && CellDescent)) return InvalidParameter;
699
700     *CellDescent = family->tmw.tmDescent;
701
702     return Ok;
703 }
704
705 /*******************************************************************************
706  * GdipGetEmHeight [GDIPLUS.@]
707  *
708  * Gets the height of the specified family in EmHeights
709  *
710  * PARAMS
711  *  family      [I] Family to retrieve from
712  *  style       [I] (optional) style
713  *  EmHeight    [O] return value
714  *
715  * RETURNS
716  *  SUCCESS: Ok
717  *  FAILURE: InvalidParameter
718  */
719 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
720 {
721     if (!(family && EmHeight)) return InvalidParameter;
722
723     TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
724
725     *EmHeight = family->tmw.ntmSizeEM;
726
727     return Ok;
728 }
729
730
731 /*******************************************************************************
732  * GdipGetLineSpacing [GDIPLUS.@]
733  *
734  * Returns the line spacing in design units
735  *
736  * PARAMS
737  *  family      [I] Family to retrieve from
738  *  style       [I] (Optional) font style
739  *  LineSpacing [O] Return value
740  *
741  * RETURNS
742  *  SUCCESS: Ok
743  *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
744  */
745 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
746         INT style, UINT16* LineSpacing)
747 {
748     TRACE("%p, %d, %p\n", family, style, LineSpacing);
749
750     if (!(family && LineSpacing))
751         return InvalidParameter;
752
753     if (style) FIXME("ignoring style\n");
754
755     *LineSpacing = family->tmw.tmAscent + family->tmw.tmDescent + family->tmw.tmExternalLeading;
756
757     return Ok;
758 }
759
760 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
761         INT style, BOOL* IsStyleAvailable)
762 {
763     FIXME("%p %d %p stub!\n", family, style, IsStyleAvailable);
764
765     if (!(family && IsStyleAvailable))
766         return InvalidParameter;
767
768     return NotImplemented;
769 }
770
771 /*****************************************************************************
772  * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
773  *
774  * Obtains a serif family (Courier New on Windows)
775  *
776  * PARAMS
777  *  **nativeFamily         [I] Where the font will be stored
778  *
779  * RETURNS
780  *  InvalidParameter if nativeFamily is NULL.
781  *  Ok otherwise.
782  */
783 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
784 {
785     static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
786
787     if (nativeFamily == NULL) return InvalidParameter;
788
789     return GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
790 }
791
792 /*****************************************************************************
793  * GdipGetGenericFontFamilySerif [GDIPLUS.@]
794  *
795  * Obtains a serif family (Times New Roman on Windows)
796  *
797  * PARAMS
798  *  **nativeFamily         [I] Where the font will be stored
799  *
800  * RETURNS
801  *  InvalidParameter if nativeFamily is NULL.
802  *  Ok otherwise.
803  */
804 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
805 {
806     static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
807
808     TRACE("(%p)\n", nativeFamily);
809
810     if (nativeFamily == NULL) return InvalidParameter;
811
812     return GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
813 }
814
815 /*****************************************************************************
816  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
817  *
818  * Obtains a serif family (Microsoft Sans Serif on Windows)
819  *
820  * PARAMS
821  *  **nativeFamily         [I] Where the font will be stored
822  *
823  * RETURNS
824  *  InvalidParameter if nativeFamily is NULL.
825  *  Ok otherwise.
826  */
827 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
828 {
829     /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
830      * affect anything */
831     static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
832
833     TRACE("(%p)\n", nativeFamily);
834
835     if (nativeFamily == NULL) return InvalidParameter;
836
837     return GdipCreateFontFamilyFromName(MSSansSerif, NULL, nativeFamily);
838 }
839
840 /*****************************************************************************
841  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
842  */
843 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
844 {
845     TRACE("%p\n", fontCollection);
846
847     if (!fontCollection)
848         return InvalidParameter;
849
850     *fontCollection = GdipAlloc(sizeof(GpFontCollection));
851     if (!*fontCollection) return OutOfMemory;
852
853     (*fontCollection)->FontFamilies = NULL;
854     (*fontCollection)->count = 0;
855     (*fontCollection)->allocated = 0;
856
857     TRACE("<-- %p\n", *fontCollection);
858
859     return Ok;
860 }
861
862 /*****************************************************************************
863  * GdipDeletePrivateFontCollection [GDIPLUS.@]
864  */
865 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
866 {
867     INT i;
868
869     TRACE("%p\n", fontCollection);
870
871     if (!fontCollection)
872         return InvalidParameter;
873
874     for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
875     GdipFree(*fontCollection);
876
877     return Ok;
878 }
879
880 /*****************************************************************************
881  * GdipPrivateAddFontFile [GDIPLUS.@]
882  */
883 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
884         GDIPCONST WCHAR* filename)
885 {
886     FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
887
888     if (!(fontCollection && filename))
889         return InvalidParameter;
890
891     return NotImplemented;
892 }
893
894 /*****************************************************************************
895  * GdipPrivateAddMemoryFont [GDIPLUS.@]
896  */
897 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
898         GDIPCONST void* memory, INT length)
899 {
900     FIXME("%p, %p, %d\n", fontCollection, memory, length);
901
902     if (!(fontCollection && memory && length))
903         return InvalidParameter;
904
905     return Ok;
906 }
907
908 /*****************************************************************************
909  * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
910  */
911 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
912         GpFontCollection* fontCollection, INT* numFound)
913 {
914     TRACE("%p, %p\n", fontCollection, numFound);
915
916     if (!(fontCollection && numFound))
917         return InvalidParameter;
918
919     *numFound = fontCollection->count;
920     return Ok;
921 }
922
923 /*****************************************************************************
924  * GdipGetFontCollectionFamilyList [GDIPLUS.@]
925  */
926 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
927         GpFontCollection* fontCollection, INT numSought,
928         GpFontFamily* gpfamilies[], INT* numFound)
929 {
930     INT i;
931
932     TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
933
934     if (!(fontCollection && gpfamilies && numFound))
935         return InvalidParameter;
936
937     for (i = 0; i < numSought && i < fontCollection->count; i++)
938     {
939         gpfamilies[i] = fontCollection->FontFamilies[i];
940     }
941     *numFound = i;
942     return Ok;
943 }
944
945 void free_installed_fonts(void)
946 {
947     while (installedFontCollection.count)
948         GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
949     HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
950     installedFontCollection.FontFamilies = NULL;
951     installedFontCollection.allocated = 0;
952 }
953
954 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
955         DWORD type, LPARAM lParam)
956 {
957     GpFontCollection* fonts = (GpFontCollection*)lParam;
958     int i;
959
960     /* skip duplicates */
961     for (i=0; i<fonts->count; i++)
962         if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
963             return 1;
964
965     if (fonts->allocated == fonts->count)
966     {
967         INT new_alloc_count = fonts->allocated+50;
968         GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
969
970         if (!new_family_list)
971             return 0;
972
973         memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
974         HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
975         fonts->FontFamilies = new_family_list;
976         fonts->allocated = new_alloc_count;
977     }
978
979     if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
980         fonts->count++;
981     else
982         return 0;
983
984     return 1;
985 }
986
987 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
988         GpFontCollection** fontCollection)
989 {
990     TRACE("(%p)\n",fontCollection);
991
992     if (!fontCollection)
993         return InvalidParameter;
994
995     if (installedFontCollection.count == 0)
996     {
997         HDC hdc;
998         LOGFONTW lfw;
999
1000         hdc = GetDC(0);
1001
1002         lfw.lfCharSet = DEFAULT_CHARSET;
1003         lfw.lfFaceName[0] = 0;
1004         lfw.lfPitchAndFamily = 0;
1005
1006         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1007         {
1008             free_installed_fonts();
1009             ReleaseDC(0, hdc);
1010             return OutOfMemory;
1011         }
1012
1013         ReleaseDC(0, hdc);
1014     }
1015
1016     *fontCollection = &installedFontCollection;
1017
1018     return Ok;
1019 }