gdiplus: Do not create FontFamilies for bitmap fonts.
[wine/hacks.git] / dlls / gdiplus / font.c
blob3a29771c7103bf06a34c0aa5b1e58303cb0831e6
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
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"
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
31 #include "objbase.h"
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
36 static const REAL mm_per_inch = 25.4;
37 static const REAL inch_per_point = 1.0/72.0;
39 static GpFontCollection installedFontCollection = {0};
41 static inline REAL get_dpi (void)
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);
51 return dpi;
54 static inline REAL point_to_pixel (REAL point)
56 return point * get_dpi() * inch_per_point;
59 static inline REAL inch_to_pixel (REAL inch)
61 return inch * get_dpi();
64 static inline REAL document_to_pixel (REAL doc)
66 return doc * (get_dpi() / 300.0); /* Per MSDN */
69 static inline REAL mm_to_pixel (REAL mm)
71 return mm * (get_dpi() / mm_per_inch);
74 /*******************************************************************************
75 * GdipCreateFont [GDIPLUS.@]
77 * Create a new font based off of a FontFamily
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
86 * RETURNS
87 * SUCCESS: Ok
88 * FAILURE: InvalidParameter if fontfamily or font is NULL.
89 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
91 * NOTES
92 * UnitDisplay is unsupported.
93 * emSize is stored separately from lfHeight, to hold the fraction.
95 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
96 REAL emSize, INT style, Unit unit, GpFont **font)
98 WCHAR facename[LF_FACESIZE];
99 LOGFONTW* lfw;
100 const NEWTEXTMETRICW* tmw;
101 GpStatus stat;
103 if (!fontFamily || !font)
104 return InvalidParameter;
106 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
107 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
109 stat = GdipGetFamilyName (fontFamily, facename, 0);
110 if (stat != Ok) return stat;
111 *font = GdipAlloc(sizeof(GpFont));
113 tmw = &fontFamily->tmw;
114 lfw = &((*font)->lfw);
115 ZeroMemory(&(*lfw), sizeof(*lfw));
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);
125 switch (unit)
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;
149 lfw->lfHeight *= -1;
151 lfw->lfWeight = style & FontStyleBold ? 700 : 400;
152 lfw->lfItalic = style & FontStyleItalic;
153 lfw->lfUnderline = style & FontStyleUnderline;
154 lfw->lfStrikeOut = style & FontStyleStrikeout;
156 (*font)->unit = unit;
157 (*font)->emSize = emSize;
158 (*font)->height = tmw->ntmSizeEM;
159 (*font)->line_spacing = tmw->tmAscent + tmw->tmDescent + tmw->tmExternalLeading;
161 TRACE("<-- %p\n", *font);
163 return Ok;
166 /*******************************************************************************
167 * GdipCreateFontFromLogfontW [GDIPLUS.@]
169 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
170 GDIPCONST LOGFONTW *logfont, GpFont **font)
172 HFONT hfont, oldfont;
173 TEXTMETRICW textmet;
175 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
177 if(!logfont || !font)
178 return InvalidParameter;
180 if (logfont->lfFaceName[0] == 0)
181 return NotTrueTypeFont;
183 *font = GdipAlloc(sizeof(GpFont));
184 if(!*font) return OutOfMemory;
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;
193 (*font)->emSize = logfont->lfHeight;
194 (*font)->unit = UnitPixel;
196 hfont = CreateFontIndirectW(&(*font)->lfw);
197 oldfont = SelectObject(hdc, hfont);
198 GetTextMetricsW(hdc, &textmet);
200 (*font)->lfw.lfHeight = -(textmet.tmHeight-textmet.tmInternalLeading);
201 (*font)->lfw.lfWeight = textmet.tmWeight;
202 (*font)->lfw.lfCharSet = textmet.tmCharSet;
204 (*font)->height = 1; /* FIXME: need NEWTEXTMETRIC.ntmSizeEM here */
205 (*font)->line_spacing = textmet.tmAscent + textmet.tmDescent + textmet.tmExternalLeading;
207 SelectObject(hdc, oldfont);
208 DeleteObject(hfont);
210 TRACE("<-- %p\n", *font);
212 return Ok;
215 /*******************************************************************************
216 * GdipCreateFontFromLogfontA [GDIPLUS.@]
218 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
219 GDIPCONST LOGFONTA *lfa, GpFont **font)
221 LOGFONTW lfw;
223 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
225 if(!lfa || !font)
226 return InvalidParameter;
228 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
230 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
231 return GenericError;
233 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
236 /*******************************************************************************
237 * GdipDeleteFont [GDIPLUS.@]
239 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
241 TRACE("(%p)\n", font);
243 if(!font)
244 return InvalidParameter;
246 GdipFree(font);
248 return Ok;
251 /*******************************************************************************
252 * GdipCreateFontFromDC [GDIPLUS.@]
254 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
256 HFONT hfont;
257 LOGFONTW lfw;
259 TRACE("(%p, %p)\n", hdc, font);
261 if(!font)
262 return InvalidParameter;
264 hfont = GetCurrentObject(hdc, OBJ_FONT);
265 if(!hfont)
266 return GenericError;
268 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
269 return GenericError;
271 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
274 /*******************************************************************************
275 * GdipGetFamily [GDIPLUS.@]
277 * Returns the FontFamily for the specified Font
279 * PARAMS
280 * font [I] Font to request from
281 * family [O] Resulting FontFamily object
283 * RETURNS
284 * SUCCESS: Ok
285 * FAILURE: An element of GpStatus
287 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
289 TRACE("%p %p\n", font, family);
291 if (!(font && family))
292 return InvalidParameter;
294 return GdipCreateFontFamilyFromName(font->lfw.lfFaceName, NULL, family);
297 /******************************************************************************
298 * GdipGetFontSize [GDIPLUS.@]
300 * Returns the size of the font in Units
302 * PARAMS
303 * *font [I] The font to retrieve size from
304 * *size [O] Pointer to hold retrieved value
306 * RETURNS
307 * SUCCESS: Ok
308 * FAILURE: InvalidParameter (font or size was NULL)
310 * NOTES
311 * Size returned is actually emSize -- not internal size used for drawing.
313 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
315 TRACE("(%p, %p)\n", font, size);
317 if (!(font && size)) return InvalidParameter;
319 *size = font->emSize;
321 return Ok;
324 /*******************************************************************************
325 * GdipGetFontStyle [GDIPLUS.@]
327 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
329 * PARAMS
330 * font [I] font to request from
331 * style [O] resulting pointer to a FontStyle enumeration
333 * RETURNS
334 * SUCCESS: Ok
335 * FAILURE: InvalidParameter
337 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
339 TRACE("%p %p\n", font, style);
341 if (!(font && style))
342 return InvalidParameter;
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;
355 return Ok;
358 /*******************************************************************************
359 * GdipGetFontUnit [GDIPLUS.@]
361 * PARAMS
362 * font [I] Font to retrieve from
363 * unit [O] Return value
365 * RETURNS
366 * FAILURE: font or unit was NULL
367 * OK: otherwise
369 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
371 TRACE("(%p, %p)\n", font, unit);
373 if (!(font && unit)) return InvalidParameter;
375 *unit = font->unit;
377 return Ok;
380 /*******************************************************************************
381 * GdipGetLogFontA [GDIPLUS.@]
383 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
384 LOGFONTA *lfa)
386 GpStatus status;
387 LOGFONTW lfw;
389 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
391 status = GdipGetLogFontW(font, graphics, &lfw);
392 if(status != Ok)
393 return status;
395 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
397 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
398 return GenericError;
400 return Ok;
403 /*******************************************************************************
404 * GdipGetLogFontW [GDIPLUS.@]
406 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
407 LOGFONTW *lfw)
409 TRACE("(%p, %p, %p)\n", font, graphics, lfw);
411 /* FIXME: use graphics */
412 if(!font || !graphics || !lfw)
413 return InvalidParameter;
415 *lfw = font->lfw;
417 return Ok;
420 /*******************************************************************************
421 * GdipCloneFont [GDIPLUS.@]
423 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
425 TRACE("(%p, %p)\n", font, cloneFont);
427 if(!font || !cloneFont)
428 return InvalidParameter;
430 *cloneFont = GdipAlloc(sizeof(GpFont));
431 if(!*cloneFont) return OutOfMemory;
433 **cloneFont = *font;
435 return Ok;
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
448 * NOTES
449 * Forwards to GdipGetFontHeightGivenDPI
451 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
452 GDIPCONST GpGraphics *graphics, REAL *height)
454 REAL dpi;
456 TRACE("%p %p %p\n", font, graphics, height);
458 dpi = GetDeviceCaps(graphics->hdc, LOGPIXELSY);
460 return GdipGetFontHeightGivenDPI(font, dpi, height);
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
470 * RETURNS
471 * SUCCESS: Ok
472 * FAILURE: InvalidParameter if font or height is NULL
474 * NOTES
475 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
476 * (for anything other than unit Pixel)
478 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
480 REAL font_height;
482 TRACE("%p (%s), %f, %p\n", font,
483 debugstr_w(font->lfw.lfFaceName), dpi, height);
485 if (!(font && height)) return InvalidParameter;
487 font_height = font->line_spacing * (font->emSize / font->height);
489 switch (font->unit)
491 case UnitPixel:
492 case UnitWorld:
493 *height = font_height;
494 break;
495 case UnitPoint:
496 *height = font_height * dpi * inch_per_point;
497 break;
498 case UnitInch:
499 *height = font_height * dpi;
500 break;
501 case UnitDocument:
502 *height = font_height * (dpi / 300.0);
503 break;
504 case UnitMillimeter:
505 *height = font_height * (dpi / mm_per_inch);
506 break;
507 default:
508 FIXME("Unhandled unit type: %d\n", font->unit);
509 return NotImplemented;
512 return Ok;
515 /***********************************************************************
516 * Borrowed from GDI32:
518 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
519 * We have to use other types because of the FONTENUMPROCW definition.
521 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
522 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
524 if (!ntm || type == RASTER_FONTTYPE)
526 return 1;
529 *(NEWTEXTMETRICW*)lParam = *(const NEWTEXTMETRICW*)ntm;
531 return 0;
534 static BOOL find_installed_font(const WCHAR *name, NEWTEXTMETRICW *ntm)
536 HDC hdc = GetDC(0);
537 BOOL ret = FALSE;
539 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)ntm))
540 ret = TRUE;
542 ReleaseDC(0, hdc);
543 return ret;
546 /*******************************************************************************
547 * GdipCreateFontFamilyFromName [GDIPLUS.@]
549 * Creates a font family object based on a supplied name
551 * PARAMS
552 * name [I] Name of the font
553 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
554 * FontFamily [O] Pointer to the resulting FontFamily object
556 * RETURNS
557 * SUCCESS: Ok
558 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
559 * FAILURE: Invalid parameter if FontFamily or name is NULL
561 * NOTES
562 * If fontCollection is NULL then the object is not part of any collection
566 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
567 GpFontCollection *fontCollection,
568 GpFontFamily **FontFamily)
570 GpFontFamily* ffamily;
571 NEWTEXTMETRICW ntm;
573 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
575 if (!(name && FontFamily))
576 return InvalidParameter;
577 if (fontCollection)
578 FIXME("No support for FontCollections yet!\n");
580 if (!find_installed_font(name, &ntm))
581 return FontFamilyNotFound;
583 ffamily = GdipAlloc(sizeof (GpFontFamily));
584 if (!ffamily) return OutOfMemory;
586 ffamily->tmw = ntm;
587 lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
589 *FontFamily = ffamily;
591 TRACE("<-- %p\n", ffamily);
593 return Ok;
596 /*******************************************************************************
597 * GdipCloneFontFamily [GDIPLUS.@]
599 * Creates a deep copy of a Font Family object
601 * PARAMS
602 * FontFamily [I] Font to clone
603 * clonedFontFamily [O] The resulting cloned font
605 * RETURNS
606 * SUCCESS: Ok
608 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
610 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
612 TRACE("stub: %p (%s), %p\n", FontFamily,
613 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
615 *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
616 if (!*clonedFontFamily) return OutOfMemory;
618 (*clonedFontFamily)->tmw = FontFamily->tmw;
619 lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
621 TRACE("<-- %p\n", *clonedFontFamily);
623 return Ok;
626 /*******************************************************************************
627 * GdipGetFamilyName [GDIPLUS.@]
629 * Returns the family name into name
631 * PARAMS
632 * *family [I] Family to retrieve from
633 * *name [O] WCHARS of the family name
634 * LANGID [I] charset
636 * RETURNS
637 * SUCCESS: Ok
638 * FAILURE: InvalidParameter if family is NULL
640 * NOTES
641 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
643 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
644 WCHAR *name, LANGID language)
646 static int lang_fixme;
648 if (family == NULL)
649 return InvalidParameter;
651 TRACE("%p, %p, %d\n", family, name, language);
653 if (language != LANG_NEUTRAL && !lang_fixme++)
654 FIXME("No support for handling of multiple languages!\n");
656 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
658 return Ok;
662 /*****************************************************************************
663 * GdipDeleteFontFamily [GDIPLUS.@]
665 * Removes the specified FontFamily
667 * PARAMS
668 * *FontFamily [I] The family to delete
670 * RETURNS
671 * SUCCESS: Ok
672 * FAILURE: InvalidParameter if FontFamily is NULL.
675 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
677 if (!FontFamily)
678 return InvalidParameter;
679 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
681 GdipFree (FontFamily);
683 return Ok;
686 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
687 INT style, UINT16* CellAscent)
689 if (!(family && CellAscent)) return InvalidParameter;
691 *CellAscent = family->tmw.tmAscent;
693 return Ok;
696 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
697 INT style, UINT16* CellDescent)
699 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
701 if (!(family && CellDescent)) return InvalidParameter;
703 *CellDescent = family->tmw.tmDescent;
705 return Ok;
708 /*******************************************************************************
709 * GdipGetEmHeight [GDIPLUS.@]
711 * Gets the height of the specified family in EmHeights
713 * PARAMS
714 * family [I] Family to retrieve from
715 * style [I] (optional) style
716 * EmHeight [O] return value
718 * RETURNS
719 * SUCCESS: Ok
720 * FAILURE: InvalidParameter
722 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
724 if (!(family && EmHeight)) return InvalidParameter;
726 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
728 *EmHeight = family->tmw.ntmSizeEM;
730 return Ok;
734 /*******************************************************************************
735 * GdipGetLineSpacing [GDIPLUS.@]
737 * Returns the line spacing in design units
739 * PARAMS
740 * family [I] Family to retrieve from
741 * style [I] (Optional) font style
742 * LineSpacing [O] Return value
744 * RETURNS
745 * SUCCESS: Ok
746 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
748 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
749 INT style, UINT16* LineSpacing)
751 TRACE("%p, %d, %p\n", family, style, LineSpacing);
753 if (!(family && LineSpacing))
754 return InvalidParameter;
756 if (style) FIXME("ignoring style\n");
758 *LineSpacing = family->tmw.tmAscent + family->tmw.tmDescent + family->tmw.tmExternalLeading;
760 return Ok;
763 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
764 INT style, BOOL* IsStyleAvailable)
766 FIXME("%p %d %p stub!\n", family, style, IsStyleAvailable);
768 if (!(family && IsStyleAvailable))
769 return InvalidParameter;
771 return NotImplemented;
774 /*****************************************************************************
775 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
777 * Obtains a serif family (Courier New on Windows)
779 * PARAMS
780 * **nativeFamily [I] Where the font will be stored
782 * RETURNS
783 * InvalidParameter if nativeFamily is NULL.
784 * Ok otherwise.
786 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
788 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
790 if (nativeFamily == NULL) return InvalidParameter;
792 return GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
795 /*****************************************************************************
796 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
798 * Obtains a serif family (Times New Roman on Windows)
800 * PARAMS
801 * **nativeFamily [I] Where the font will be stored
803 * RETURNS
804 * InvalidParameter if nativeFamily is NULL.
805 * Ok otherwise.
807 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
809 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
811 TRACE("(%p)\n", nativeFamily);
813 if (nativeFamily == NULL) return InvalidParameter;
815 return GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
818 /*****************************************************************************
819 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
821 * Obtains a serif family (Microsoft Sans Serif on Windows)
823 * PARAMS
824 * **nativeFamily [I] Where the font will be stored
826 * RETURNS
827 * InvalidParameter if nativeFamily is NULL.
828 * Ok otherwise.
830 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
832 GpStatus stat;
833 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
834 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
836 TRACE("(%p)\n", nativeFamily);
838 if (nativeFamily == NULL) return InvalidParameter;
840 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
842 if (stat == FontFamilyNotFound)
843 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
844 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
846 return stat;
849 /*****************************************************************************
850 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
852 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
854 TRACE("%p\n", fontCollection);
856 if (!fontCollection)
857 return InvalidParameter;
859 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
860 if (!*fontCollection) return OutOfMemory;
862 (*fontCollection)->FontFamilies = NULL;
863 (*fontCollection)->count = 0;
864 (*fontCollection)->allocated = 0;
866 TRACE("<-- %p\n", *fontCollection);
868 return Ok;
871 /*****************************************************************************
872 * GdipDeletePrivateFontCollection [GDIPLUS.@]
874 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
876 INT i;
878 TRACE("%p\n", fontCollection);
880 if (!fontCollection)
881 return InvalidParameter;
883 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
884 GdipFree(*fontCollection);
886 return Ok;
889 /*****************************************************************************
890 * GdipPrivateAddFontFile [GDIPLUS.@]
892 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
893 GDIPCONST WCHAR* filename)
895 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
897 if (!(fontCollection && filename))
898 return InvalidParameter;
900 return NotImplemented;
903 /*****************************************************************************
904 * GdipPrivateAddMemoryFont [GDIPLUS.@]
906 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
907 GDIPCONST void* memory, INT length)
909 FIXME("%p, %p, %d\n", fontCollection, memory, length);
911 if (!(fontCollection && memory && length))
912 return InvalidParameter;
914 return Ok;
917 /*****************************************************************************
918 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
920 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
921 GpFontCollection* fontCollection, INT* numFound)
923 TRACE("%p, %p\n", fontCollection, numFound);
925 if (!(fontCollection && numFound))
926 return InvalidParameter;
928 *numFound = fontCollection->count;
929 return Ok;
932 /*****************************************************************************
933 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
935 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
936 GpFontCollection* fontCollection, INT numSought,
937 GpFontFamily* gpfamilies[], INT* numFound)
939 INT i;
941 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
943 if (!(fontCollection && gpfamilies && numFound))
944 return InvalidParameter;
946 for (i = 0; i < numSought && i < fontCollection->count; i++)
948 gpfamilies[i] = fontCollection->FontFamilies[i];
950 *numFound = i;
951 return Ok;
954 void free_installed_fonts(void)
956 while (installedFontCollection.count)
957 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
958 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
959 installedFontCollection.FontFamilies = NULL;
960 installedFontCollection.allocated = 0;
963 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
964 DWORD type, LPARAM lParam)
966 GpFontCollection* fonts = (GpFontCollection*)lParam;
967 int i;
969 if (type == RASTER_FONTTYPE)
970 return 1;
972 /* skip duplicates */
973 for (i=0; i<fonts->count; i++)
974 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
975 return 1;
977 if (fonts->allocated == fonts->count)
979 INT new_alloc_count = fonts->allocated+50;
980 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
982 if (!new_family_list)
983 return 0;
985 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
986 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
987 fonts->FontFamilies = new_family_list;
988 fonts->allocated = new_alloc_count;
991 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
992 fonts->count++;
993 else
994 return 0;
996 return 1;
999 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1000 GpFontCollection** fontCollection)
1002 TRACE("(%p)\n",fontCollection);
1004 if (!fontCollection)
1005 return InvalidParameter;
1007 if (installedFontCollection.count == 0)
1009 HDC hdc;
1010 LOGFONTW lfw;
1012 hdc = GetDC(0);
1014 lfw.lfCharSet = DEFAULT_CHARSET;
1015 lfw.lfFaceName[0] = 0;
1016 lfw.lfPitchAndFamily = 0;
1018 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1020 free_installed_fonts();
1021 ReleaseDC(0, hdc);
1022 return OutOfMemory;
1025 ReleaseDC(0, hdc);
1028 *fontCollection = &installedFontCollection;
1030 return Ok;