dmloader: Mark internal symbols with hidden visibility.
[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             (*font)->pixel_size = 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             (*font)->pixel_size = 0; break;
137         case UnitPixel:
138             (*font)->pixel_size = emSize; break;
139         case UnitPoint:
140             (*font)->pixel_size = point_to_pixel(emSize); break;
141         case UnitInch:
142             (*font)->pixel_size = inch_to_pixel(emSize); break;
143         case UnitDocument:
144             (*font)->pixel_size = document_to_pixel(emSize); break;
145         case UnitMillimeter:
146             (*font)->pixel_size = mm_to_pixel(emSize); break;
147     }
148
149     lfw->lfHeight = (*font)->pixel_size * -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)->pixel_size = (*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     GpStatus stat;
456
457     TRACE("%p %p %p\n", font, graphics, height);
458
459     stat = GdipGetDpiY((GpGraphics*)graphics, &dpi);
460
461     if (stat == Ok)
462         stat = GdipGetFontHeightGivenDPI(font, dpi, height);
463
464     return stat;
465 }
466
467 /*******************************************************************************
468  * GdipGetFontHeightGivenDPI [GDIPLUS.@]
469  * PARAMS
470  *  font        [I] Font to retrieve DPI from
471  *  dpi         [I] DPI to assume
472  *  height      [O] Return value
473  *
474  * RETURNS
475  *  SUCCESS: Ok
476  *  FAILURE: InvalidParameter if font or height is NULL
477  *
478  * NOTES
479  *  According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
480  *  (for anything other than unit Pixel)
481  */
482 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
483 {
484     REAL font_height;
485
486     TRACE("%p (%s), %f, %p\n", font,
487             debugstr_w(font->lfw.lfFaceName), dpi, height);
488
489     if (!(font && height)) return InvalidParameter;
490
491     font_height = font->line_spacing * (font->emSize / font->height);
492
493     switch (font->unit)
494     {
495         case UnitPixel:
496         case UnitWorld:
497             *height = font_height;
498             break;
499         case UnitPoint:
500             *height = font_height * dpi * inch_per_point;
501             break;
502         case UnitInch:
503             *height = font_height * dpi;
504             break;
505         case UnitDocument:
506             *height = font_height * (dpi / 300.0);
507             break;
508         case UnitMillimeter:
509             *height = font_height * (dpi / mm_per_inch);
510             break;
511         default:
512             FIXME("Unhandled unit type: %d\n", font->unit);
513             return NotImplemented;
514     }
515
516     return Ok;
517 }
518
519 /***********************************************************************
520  * Borrowed from GDI32:
521  *
522  * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
523  *     We have to use other types because of the FONTENUMPROCW definition.
524  */
525 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
526                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
527 {
528     if (!ntm || type == RASTER_FONTTYPE)
529     {
530         return 1;
531     }
532
533     *(NEWTEXTMETRICW*)lParam = *(const NEWTEXTMETRICW*)ntm;
534
535     return 0;
536 }
537
538 static BOOL find_installed_font(const WCHAR *name, NEWTEXTMETRICW *ntm)
539 {
540     HDC hdc = GetDC(0);
541     BOOL ret = FALSE;
542
543     if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)ntm))
544         ret = TRUE;
545
546     ReleaseDC(0, hdc);
547     return ret;
548 }
549
550 /*******************************************************************************
551  * GdipCreateFontFamilyFromName [GDIPLUS.@]
552  *
553  * Creates a font family object based on a supplied name
554  *
555  * PARAMS
556  *  name               [I] Name of the font
557  *  fontCollection     [I] What font collection (if any) the font belongs to (may be NULL)
558  *  FontFamily         [O] Pointer to the resulting FontFamily object
559  *
560  * RETURNS
561  *  SUCCESS: Ok
562  *  FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
563  *  FAILURE: Invalid parameter if FontFamily or name is NULL
564  *
565  * NOTES
566  *   If fontCollection is NULL then the object is not part of any collection
567  *
568  */
569
570 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
571                                         GpFontCollection *fontCollection,
572                                         GpFontFamily **FontFamily)
573 {
574     GpFontFamily* ffamily;
575     NEWTEXTMETRICW ntm;
576
577     TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
578
579     if (!(name && FontFamily))
580         return InvalidParameter;
581     if (fontCollection)
582         FIXME("No support for FontCollections yet!\n");
583
584     if (!find_installed_font(name, &ntm))
585         return FontFamilyNotFound;
586
587     ffamily = GdipAlloc(sizeof (GpFontFamily));
588     if (!ffamily) return OutOfMemory;
589
590     ffamily->tmw = ntm;
591     lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
592
593     *FontFamily = ffamily;
594
595     TRACE("<-- %p\n", ffamily);
596
597     return Ok;
598 }
599
600 /*******************************************************************************
601  * GdipCloneFontFamily [GDIPLUS.@]
602  *
603  * Creates a deep copy of a Font Family object
604  *
605  * PARAMS
606  *  FontFamily          [I] Font to clone
607  *  clonedFontFamily    [O] The resulting cloned font
608  *
609  * RETURNS
610  *  SUCCESS: Ok
611  */
612 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
613 {
614     if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
615
616     TRACE("stub: %p (%s), %p\n", FontFamily,
617             debugstr_w(FontFamily->FamilyName), clonedFontFamily);
618
619     *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
620     if (!*clonedFontFamily) return OutOfMemory;
621
622     (*clonedFontFamily)->tmw = FontFamily->tmw;
623     lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
624
625     TRACE("<-- %p\n", *clonedFontFamily);
626
627     return Ok;
628 }
629
630 /*******************************************************************************
631  * GdipGetFamilyName [GDIPLUS.@]
632  *
633  * Returns the family name into name
634  *
635  * PARAMS
636  *  *family     [I] Family to retrieve from
637  *  *name       [O] WCHARS of the family name
638  *  LANGID      [I] charset
639  *
640  * RETURNS
641  *  SUCCESS: Ok
642  *  FAILURE: InvalidParameter if family is NULL
643  *
644  * NOTES
645  *   If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
646  */
647 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
648                                        WCHAR *name, LANGID language)
649 {
650     static int lang_fixme;
651
652     if (family == NULL)
653          return InvalidParameter;
654
655     TRACE("%p, %p, %d\n", family, name, language);
656
657     if (language != LANG_NEUTRAL && !lang_fixme++)
658         FIXME("No support for handling of multiple languages!\n");
659
660     lstrcpynW (name, family->FamilyName, LF_FACESIZE);
661
662     return Ok;
663 }
664
665
666 /*****************************************************************************
667  * GdipDeleteFontFamily [GDIPLUS.@]
668  *
669  * Removes the specified FontFamily
670  *
671  * PARAMS
672  *  *FontFamily         [I] The family to delete
673  *
674  * RETURNS
675  *  SUCCESS: Ok
676  *  FAILURE: InvalidParameter if FontFamily is NULL.
677  *
678  */
679 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
680 {
681     if (!FontFamily)
682         return InvalidParameter;
683     TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
684
685     GdipFree (FontFamily);
686
687     return Ok;
688 }
689
690 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
691         INT style, UINT16* CellAscent)
692 {
693     if (!(family && CellAscent)) return InvalidParameter;
694
695     *CellAscent = family->tmw.tmAscent;
696
697     return Ok;
698 }
699
700 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
701         INT style, UINT16* CellDescent)
702 {
703     TRACE("(%p, %d, %p)\n", family, style, CellDescent);
704
705     if (!(family && CellDescent)) return InvalidParameter;
706
707     *CellDescent = family->tmw.tmDescent;
708
709     return Ok;
710 }
711
712 /*******************************************************************************
713  * GdipGetEmHeight [GDIPLUS.@]
714  *
715  * Gets the height of the specified family in EmHeights
716  *
717  * PARAMS
718  *  family      [I] Family to retrieve from
719  *  style       [I] (optional) style
720  *  EmHeight    [O] return value
721  *
722  * RETURNS
723  *  SUCCESS: Ok
724  *  FAILURE: InvalidParameter
725  */
726 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
727 {
728     if (!(family && EmHeight)) return InvalidParameter;
729
730     TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
731
732     *EmHeight = family->tmw.ntmSizeEM;
733
734     return Ok;
735 }
736
737
738 /*******************************************************************************
739  * GdipGetLineSpacing [GDIPLUS.@]
740  *
741  * Returns the line spacing in design units
742  *
743  * PARAMS
744  *  family      [I] Family to retrieve from
745  *  style       [I] (Optional) font style
746  *  LineSpacing [O] Return value
747  *
748  * RETURNS
749  *  SUCCESS: Ok
750  *  FAILURE: InvalidParameter (family or LineSpacing was NULL)
751  */
752 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
753         INT style, UINT16* LineSpacing)
754 {
755     TRACE("%p, %d, %p\n", family, style, LineSpacing);
756
757     if (!(family && LineSpacing))
758         return InvalidParameter;
759
760     if (style) FIXME("ignoring style\n");
761
762     *LineSpacing = family->tmw.tmAscent + family->tmw.tmDescent + family->tmw.tmExternalLeading;
763
764     return Ok;
765 }
766
767 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
768                             const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
769 {
770     INT fontstyle=0;
771
772     if (!ntm) return 1;
773
774     if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
775     if (ntm->tmItalic) fontstyle |= FontStyleItalic;
776     if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
777     if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
778
779     return (INT)lParam != fontstyle;
780 }
781
782 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
783         INT style, BOOL* IsStyleAvailable)
784 {
785     HDC hdc;
786
787     TRACE("%p %d %p\n", family, style, IsStyleAvailable);
788
789     if (!(family && IsStyleAvailable))
790         return InvalidParameter;
791
792     *IsStyleAvailable = FALSE;
793
794     hdc = GetDC(0);
795
796     if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
797         *IsStyleAvailable = TRUE;
798
799     ReleaseDC(0, hdc);
800
801     return Ok;
802 }
803
804 /*****************************************************************************
805  * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
806  *
807  * Obtains a serif family (Courier New on Windows)
808  *
809  * PARAMS
810  *  **nativeFamily         [I] Where the font will be stored
811  *
812  * RETURNS
813  *  InvalidParameter if nativeFamily is NULL.
814  *  Ok otherwise.
815  */
816 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
817 {
818     static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
819
820     if (nativeFamily == NULL) return InvalidParameter;
821
822     return GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
823 }
824
825 /*****************************************************************************
826  * GdipGetGenericFontFamilySerif [GDIPLUS.@]
827  *
828  * Obtains a serif family (Times New Roman on Windows)
829  *
830  * PARAMS
831  *  **nativeFamily         [I] Where the font will be stored
832  *
833  * RETURNS
834  *  InvalidParameter if nativeFamily is NULL.
835  *  Ok otherwise.
836  */
837 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
838 {
839     static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
840     static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
841     GpStatus stat;
842
843     TRACE("(%p)\n", nativeFamily);
844
845     if (nativeFamily == NULL) return InvalidParameter;
846
847     stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
848
849     if (stat == FontFamilyNotFound)
850         stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
851
852     if (stat == FontFamilyNotFound)
853         ERR("Missing 'Times New Roman' font\n");
854
855     return stat;
856 }
857
858 /*****************************************************************************
859  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
860  *
861  * Obtains a serif family (Microsoft Sans Serif on Windows)
862  *
863  * PARAMS
864  *  **nativeFamily         [I] Where the font will be stored
865  *
866  * RETURNS
867  *  InvalidParameter if nativeFamily is NULL.
868  *  Ok otherwise.
869  */
870 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
871 {
872     GpStatus stat;
873     static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
874     static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
875
876     TRACE("(%p)\n", nativeFamily);
877
878     if (nativeFamily == NULL) return InvalidParameter;
879
880     stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
881
882     if (stat == FontFamilyNotFound)
883         /* FIXME: Microsoft Sans Serif is not installed on Wine. */
884         stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
885
886     return stat;
887 }
888
889 /*****************************************************************************
890  * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
891  */
892 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
893 {
894     TRACE("%p\n", fontCollection);
895
896     if (!fontCollection)
897         return InvalidParameter;
898
899     *fontCollection = GdipAlloc(sizeof(GpFontCollection));
900     if (!*fontCollection) return OutOfMemory;
901
902     (*fontCollection)->FontFamilies = NULL;
903     (*fontCollection)->count = 0;
904     (*fontCollection)->allocated = 0;
905
906     TRACE("<-- %p\n", *fontCollection);
907
908     return Ok;
909 }
910
911 /*****************************************************************************
912  * GdipDeletePrivateFontCollection [GDIPLUS.@]
913  */
914 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
915 {
916     INT i;
917
918     TRACE("%p\n", fontCollection);
919
920     if (!fontCollection)
921         return InvalidParameter;
922
923     for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
924     GdipFree(*fontCollection);
925
926     return Ok;
927 }
928
929 /*****************************************************************************
930  * GdipPrivateAddFontFile [GDIPLUS.@]
931  */
932 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
933         GDIPCONST WCHAR* filename)
934 {
935     FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
936
937     if (!(fontCollection && filename))
938         return InvalidParameter;
939
940     return NotImplemented;
941 }
942
943 /* Copied from msi/font.c */
944
945 typedef struct _tagTT_OFFSET_TABLE {
946     USHORT uMajorVersion;
947     USHORT uMinorVersion;
948     USHORT uNumOfTables;
949     USHORT uSearchRange;
950     USHORT uEntrySelector;
951     USHORT uRangeShift;
952 } TT_OFFSET_TABLE;
953
954 typedef struct _tagTT_TABLE_DIRECTORY {
955     char szTag[4]; /* table name */
956     ULONG uCheckSum; /* Check sum */
957     ULONG uOffset; /* Offset from beginning of file */
958     ULONG uLength; /* length of the table in bytes */
959 } TT_TABLE_DIRECTORY;
960
961 typedef struct _tagTT_NAME_TABLE_HEADER {
962     USHORT uFSelector; /* format selector. Always 0 */
963     USHORT uNRCount; /* Name Records count */
964     USHORT uStorageOffset; /* Offset for strings storage,
965                             * from start of the table */
966 } TT_NAME_TABLE_HEADER;
967
968 #define NAME_ID_FULL_FONT_NAME  4
969 #define NAME_ID_VERSION         5
970
971 typedef struct _tagTT_NAME_RECORD {
972     USHORT uPlatformID;
973     USHORT uEncodingID;
974     USHORT uLanguageID;
975     USHORT uNameID;
976     USHORT uStringLength;
977     USHORT uStringOffset; /* from start of storage area */
978 } TT_NAME_RECORD;
979
980 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
981 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
982
983 /*
984  * Code based off of code located here
985  * http://www.codeproject.com/gdi/fontnamefromfile.asp
986  */
987 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
988 {
989     const TT_TABLE_DIRECTORY *tblDir;
990     TT_OFFSET_TABLE ttOffsetTable;
991     TT_NAME_TABLE_HEADER ttNTHeader;
992     TT_NAME_RECORD ttRecord;
993     DWORD ofs, pos;
994     int i;
995
996     if (sizeof(TT_OFFSET_TABLE) > size)
997         return NULL;
998     ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
999     ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1000     ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1001     ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1002
1003     if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1004         return NULL;
1005
1006     pos = sizeof(ttOffsetTable);
1007     for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1008     {
1009         tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1010         pos += sizeof(*tblDir);
1011         if (memcmp(tblDir->szTag,"name",4)==0)
1012         {
1013             ofs = SWAPLONG(tblDir->uOffset);
1014             break;
1015         }
1016     }
1017     if (i >= ttOffsetTable.uNumOfTables)
1018         return NULL;
1019
1020     pos = ofs + sizeof(ttNTHeader);
1021     if (pos > size)
1022         return NULL;
1023     ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1024     ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1025     ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1026     for(i=0; i<ttNTHeader.uNRCount; i++)
1027     {
1028         ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1029         pos += sizeof(ttRecord);
1030         if (pos > size)
1031             return NULL;
1032
1033         ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1034         if (ttRecord.uNameID == id)
1035         {
1036             const char *buf;
1037
1038             ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1039             ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1040             if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1041                 return NULL;
1042             buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1043             len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1044             ret[len] = 0;
1045             return ret;
1046         }
1047     }
1048     return NULL;
1049 }
1050
1051 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1052
1053 /*****************************************************************************
1054  * GdipPrivateAddMemoryFont [GDIPLUS.@]
1055  */
1056 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1057         GDIPCONST void* memory, INT length)
1058 {
1059     WCHAR buf[32], *name;
1060     DWORD count = 0;
1061     HANDLE font;
1062     TRACE("%p, %p, %d\n", fontCollection, memory, length);
1063
1064     if (!fontCollection || !memory || !length)
1065         return InvalidParameter;
1066
1067     name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1068     if (!name)
1069         return OutOfMemory;
1070
1071     font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1072     TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1073     if (!font || !count)
1074         return InvalidParameter;
1075
1076     if (count)
1077     {
1078         HDC hdc;
1079         LOGFONTW lfw;
1080
1081         hdc = GetDC(0);
1082
1083         lfw.lfCharSet = DEFAULT_CHARSET;
1084         lstrcpyW(lfw.lfFaceName, name);
1085         lfw.lfPitchAndFamily = 0;
1086
1087         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1088         {
1089             ReleaseDC(0, hdc);
1090             return OutOfMemory;
1091         }
1092
1093         ReleaseDC(0, hdc);
1094     }
1095     return Ok;
1096 }
1097
1098 /*****************************************************************************
1099  * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1100  */
1101 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1102         GpFontCollection* fontCollection, INT* numFound)
1103 {
1104     TRACE("%p, %p\n", fontCollection, numFound);
1105
1106     if (!(fontCollection && numFound))
1107         return InvalidParameter;
1108
1109     *numFound = fontCollection->count;
1110     return Ok;
1111 }
1112
1113 /*****************************************************************************
1114  * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1115  */
1116 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1117         GpFontCollection* fontCollection, INT numSought,
1118         GpFontFamily* gpfamilies[], INT* numFound)
1119 {
1120     INT i;
1121     GpStatus stat=Ok;
1122
1123     TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1124
1125     if (!(fontCollection && gpfamilies && numFound))
1126         return InvalidParameter;
1127
1128     memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1129
1130     for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1131     {
1132         stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1133     }
1134
1135     if (stat == Ok)
1136         *numFound = i;
1137     else
1138     {
1139         int numToFree=i;
1140         for (i=0; i<numToFree; i++)
1141         {
1142             GdipDeleteFontFamily(gpfamilies[i]);
1143             gpfamilies[i] = NULL;
1144         }
1145     }
1146
1147     return stat;
1148 }
1149
1150 void free_installed_fonts(void)
1151 {
1152     while (installedFontCollection.count)
1153         GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1154     HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1155     installedFontCollection.FontFamilies = NULL;
1156     installedFontCollection.allocated = 0;
1157 }
1158
1159 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1160         DWORD type, LPARAM lParam)
1161 {
1162     GpFontCollection* fonts = (GpFontCollection*)lParam;
1163     int i;
1164
1165     if (type == RASTER_FONTTYPE)
1166         return 1;
1167
1168     /* skip duplicates */
1169     for (i=0; i<fonts->count; i++)
1170         if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1171             return 1;
1172
1173     if (fonts->allocated == fonts->count)
1174     {
1175         INT new_alloc_count = fonts->allocated+50;
1176         GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1177
1178         if (!new_family_list)
1179             return 0;
1180
1181         memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1182         HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1183         fonts->FontFamilies = new_family_list;
1184         fonts->allocated = new_alloc_count;
1185     }
1186
1187     if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1188         fonts->count++;
1189     else
1190         return 0;
1191
1192     return 1;
1193 }
1194
1195 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1196         GpFontCollection** fontCollection)
1197 {
1198     TRACE("(%p)\n",fontCollection);
1199
1200     if (!fontCollection)
1201         return InvalidParameter;
1202
1203     if (installedFontCollection.count == 0)
1204     {
1205         HDC hdc;
1206         LOGFONTW lfw;
1207
1208         hdc = GetDC(0);
1209
1210         lfw.lfCharSet = DEFAULT_CHARSET;
1211         lfw.lfFaceName[0] = 0;
1212         lfw.lfPitchAndFamily = 0;
1213
1214         if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1215         {
1216             free_installed_fonts();
1217             ReleaseDC(0, hdc);
1218             return OutOfMemory;
1219         }
1220
1221         ReleaseDC(0, hdc);
1222     }
1223
1224     *fontCollection = &installedFontCollection;
1225
1226     return Ok;
1227 }