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
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
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)
46 GdipCreateFromHDC (hdc, &graphics);
47 GdipGetDpiX(graphics, &dpi);
48 GdipDeleteGraphics(graphics);
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
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
88 * FAILURE: InvalidParameter if fontfamily or font is NULL.
89 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
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];
100 const OUTLINETEXTMETRICW *otm;
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 otm = &fontFamily->otm;
114 lfw = &((*font)->lfw);
115 ZeroMemory(&(*lfw), sizeof(*lfw));
117 lfw->lfWeight = otm->otmTextMetrics.tmWeight;
118 lfw->lfItalic = otm->otmTextMetrics.tmItalic;
119 lfw->lfUnderline = otm->otmTextMetrics.tmUnderlined;
120 lfw->lfStrikeOut = otm->otmTextMetrics.tmStruckOut;
121 lfw->lfCharSet = otm->otmTextMetrics.tmCharSet;
122 lfw->lfPitchAndFamily = otm->otmTextMetrics.tmPitchAndFamily;
123 lstrcpynW(lfw->lfFaceName, facename, LF_FACESIZE);
128 /* FIXME: Figure out when World != Pixel */
129 (*font)->pixel_size = emSize; break;
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;
138 (*font)->pixel_size = emSize; break;
140 (*font)->pixel_size = point_to_pixel(emSize); break;
142 (*font)->pixel_size = inch_to_pixel(emSize); break;
144 (*font)->pixel_size = document_to_pixel(emSize); break;
146 (*font)->pixel_size = mm_to_pixel(emSize); break;
149 lfw->lfHeight = (*font)->pixel_size * -1;
151 lfw->lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
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 = otm->otmEMSquare;
159 (*font)->line_spacing = otm->otmTextMetrics.tmAscent + otm->otmTextMetrics.tmDescent + otm->otmTextMetrics.tmExternalLeading;
161 TRACE("<-- %p\n", *font);
166 /*******************************************************************************
167 * GdipCreateFontFromLogfontW [GDIPLUS.@]
169 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
170 GDIPCONST LOGFONTW *logfont, GpFont **font)
172 HFONT hfont, oldfont;
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 *
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 hfont = CreateFontIndirectW(&(*font)->lfw);
194 oldfont = SelectObject(hdc, hfont);
195 GetTextMetricsW(hdc, &textmet);
197 (*font)->lfw.lfHeight = -(textmet.tmHeight-textmet.tmInternalLeading);
198 (*font)->lfw.lfWeight = textmet.tmWeight;
199 (*font)->lfw.lfCharSet = textmet.tmCharSet;
201 (*font)->pixel_size = (*font)->emSize = textmet.tmHeight;
202 (*font)->unit = UnitPixel;
203 (*font)->height = 1; /* FIXME: need NEWTEXTMETRIC.ntmSizeEM here */
204 (*font)->line_spacing = textmet.tmAscent + textmet.tmDescent + textmet.tmExternalLeading;
206 SelectObject(hdc, oldfont);
209 TRACE("<-- %p\n", *font);
214 /*******************************************************************************
215 * GdipCreateFontFromLogfontA [GDIPLUS.@]
217 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
218 GDIPCONST LOGFONTA *lfa, GpFont **font)
222 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
225 return InvalidParameter;
227 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
229 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
232 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
235 /*******************************************************************************
236 * GdipDeleteFont [GDIPLUS.@]
238 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
240 TRACE("(%p)\n", font);
243 return InvalidParameter;
250 /*******************************************************************************
251 * GdipCreateFontFromDC [GDIPLUS.@]
253 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
258 TRACE("(%p, %p)\n", hdc, font);
261 return InvalidParameter;
263 hfont = GetCurrentObject(hdc, OBJ_FONT);
267 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
270 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
273 /*******************************************************************************
274 * GdipGetFamily [GDIPLUS.@]
276 * Returns the FontFamily for the specified Font
279 * font [I] Font to request from
280 * family [O] Resulting FontFamily object
284 * FAILURE: An element of GpStatus
286 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
288 TRACE("%p %p\n", font, family);
290 if (!(font && family))
291 return InvalidParameter;
293 return GdipCreateFontFamilyFromName(font->lfw.lfFaceName, NULL, family);
296 /******************************************************************************
297 * GdipGetFontSize [GDIPLUS.@]
299 * Returns the size of the font in Units
302 * *font [I] The font to retrieve size from
303 * *size [O] Pointer to hold retrieved value
307 * FAILURE: InvalidParameter (font or size was NULL)
310 * Size returned is actually emSize -- not internal size used for drawing.
312 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
314 TRACE("(%p, %p)\n", font, size);
316 if (!(font && size)) return InvalidParameter;
318 *size = font->emSize;
319 TRACE("%s,%d => %f\n", debugstr_w(font->lfw.lfFaceName), font->lfw.lfHeight, *size);
324 /*******************************************************************************
325 * GdipGetFontStyle [GDIPLUS.@]
327 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
330 * font [I] font to request from
331 * style [O] resulting pointer to a FontStyle enumeration
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 > FW_REGULAR)
345 *style = FontStyleBold;
347 *style = FontStyleRegular;
348 if (font->lfw.lfItalic)
349 *style |= FontStyleItalic;
350 if (font->lfw.lfUnderline)
351 *style |= FontStyleUnderline;
352 if (font->lfw.lfStrikeOut)
353 *style |= FontStyleStrikeout;
358 /*******************************************************************************
359 * GdipGetFontUnit [GDIPLUS.@]
362 * font [I] Font to retrieve from
363 * unit [O] Return value
366 * FAILURE: font or unit was NULL
369 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
371 TRACE("(%p, %p)\n", font, unit);
373 if (!(font && unit)) return InvalidParameter;
376 TRACE("%s,%d => %d\n", debugstr_w(font->lfw.lfFaceName), font->lfw.lfHeight, *unit);
381 /*******************************************************************************
382 * GdipGetLogFontA [GDIPLUS.@]
384 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
390 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
392 status = GdipGetLogFontW(font, graphics, &lfw);
396 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
398 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
404 /*******************************************************************************
405 * GdipGetLogFontW [GDIPLUS.@]
407 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
410 TRACE("(%p, %p, %p)\n", font, graphics, lfw);
412 /* FIXME: use graphics */
413 if(!font || !graphics || !lfw)
414 return InvalidParameter;
417 TRACE("=> %s,%d\n", debugstr_w(font->lfw.lfFaceName), font->lfw.lfHeight);
422 /*******************************************************************************
423 * GdipCloneFont [GDIPLUS.@]
425 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
427 TRACE("(%p, %p)\n", font, cloneFont);
429 if(!font || !cloneFont)
430 return InvalidParameter;
432 *cloneFont = GdipAlloc(sizeof(GpFont));
433 if(!*cloneFont) return OutOfMemory;
440 /*******************************************************************************
441 * GdipGetFontHeight [GDIPLUS.@]
443 * font [I] Font to retrieve height from
444 * graphics [I] The current graphics context
445 * height [O] Resulting height
448 * FAILURE: Another element of GpStatus
451 * Forwards to GdipGetFontHeightGivenDPI
453 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
454 GDIPCONST GpGraphics *graphics, REAL *height)
459 TRACE("%p %p %p\n", font, graphics, height);
461 stat = GdipGetDpiY((GpGraphics*)graphics, &dpi);
464 stat = GdipGetFontHeightGivenDPI(font, dpi, height);
469 /*******************************************************************************
470 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
472 * font [I] Font to retrieve DPI from
473 * dpi [I] DPI to assume
474 * height [O] Return value
478 * FAILURE: InvalidParameter if font or height is NULL
481 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
482 * (for anything other than unit Pixel)
484 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
488 TRACE("%p (%s), %f, %p\n", font,
489 debugstr_w(font->lfw.lfFaceName), dpi, height);
491 if (!(font && height)) return InvalidParameter;
493 font_height = font->line_spacing * (font->emSize / font->height);
499 *height = font_height;
502 *height = font_height * dpi * inch_per_point;
505 *height = font_height * dpi;
508 *height = font_height * (dpi / 300.0);
511 *height = font_height * (dpi / mm_per_inch);
514 FIXME("Unhandled unit type: %d\n", font->unit);
515 return NotImplemented;
518 TRACE("%s,%d(unit %d) => %f\n",
519 debugstr_w(font->lfw.lfFaceName), font->lfw.lfHeight, font->unit, *height);
524 /***********************************************************************
525 * Borrowed from GDI32:
527 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
528 * We have to use other types because of the FONTENUMPROCW definition.
530 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
531 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
533 if (!ntm || type == RASTER_FONTTYPE)
538 *(LOGFONTW *)lParam = *elf;
543 static BOOL find_installed_font(const WCHAR *name, OUTLINETEXTMETRICW *otm)
546 HDC hdc = CreateCompatibleDC(0);
549 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
554 hfont = CreateFontIndirectW(&lf);
555 hfont = SelectObject(hdc, hfont);
557 otm->otmSize = sizeof(*otm);
558 if (GetOutlineTextMetricsW(hdc, otm->otmSize, otm))
561 DeleteObject(SelectObject(hdc, hfont));
568 /*******************************************************************************
569 * GdipCreateFontFamilyFromName [GDIPLUS.@]
571 * Creates a font family object based on a supplied name
574 * name [I] Name of the font
575 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
576 * FontFamily [O] Pointer to the resulting FontFamily object
580 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
581 * FAILURE: Invalid parameter if FontFamily or name is NULL
584 * If fontCollection is NULL then the object is not part of any collection
588 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
589 GpFontCollection *fontCollection,
590 GpFontFamily **FontFamily)
592 GpFontFamily* ffamily;
593 OUTLINETEXTMETRICW otm;
595 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
597 if (!(name && FontFamily))
598 return InvalidParameter;
600 FIXME("No support for FontCollections yet!\n");
602 if (!find_installed_font(name, &otm))
603 return FontFamilyNotFound;
605 ffamily = GdipAlloc(sizeof (GpFontFamily));
606 if (!ffamily) return OutOfMemory;
609 lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
611 *FontFamily = ffamily;
613 TRACE("<-- %p\n", ffamily);
618 /*******************************************************************************
619 * GdipCloneFontFamily [GDIPLUS.@]
621 * Creates a deep copy of a Font Family object
624 * FontFamily [I] Font to clone
625 * clonedFontFamily [O] The resulting cloned font
630 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
632 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
634 TRACE("stub: %p (%s), %p\n", FontFamily,
635 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
637 *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
638 if (!*clonedFontFamily) return OutOfMemory;
640 (*clonedFontFamily)->otm = FontFamily->otm;
641 lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
643 TRACE("<-- %p\n", *clonedFontFamily);
648 /*******************************************************************************
649 * GdipGetFamilyName [GDIPLUS.@]
651 * Returns the family name into name
654 * *family [I] Family to retrieve from
655 * *name [O] WCHARS of the family name
660 * FAILURE: InvalidParameter if family is NULL
663 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
665 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
666 WCHAR *name, LANGID language)
668 static int lang_fixme;
671 return InvalidParameter;
673 TRACE("%p, %p, %d\n", family, name, language);
675 if (language != LANG_NEUTRAL && !lang_fixme++)
676 FIXME("No support for handling of multiple languages!\n");
678 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
684 /*****************************************************************************
685 * GdipDeleteFontFamily [GDIPLUS.@]
687 * Removes the specified FontFamily
690 * *FontFamily [I] The family to delete
694 * FAILURE: InvalidParameter if FontFamily is NULL.
697 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
700 return InvalidParameter;
701 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
703 GdipFree (FontFamily);
708 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
709 INT style, UINT16* CellAscent)
711 if (!(family && CellAscent)) return InvalidParameter;
713 *CellAscent = family->otm.otmTextMetrics.tmAscent;
714 TRACE("%d => %u\n", family->otm.otmTextMetrics.tmHeight, *CellAscent);
719 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
720 INT style, UINT16* CellDescent)
722 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
724 if (!(family && CellDescent)) return InvalidParameter;
726 *CellDescent = family->otm.otmTextMetrics.tmDescent;
727 TRACE("%d => %u\n", family->otm.otmTextMetrics.tmHeight, *CellDescent);
732 /*******************************************************************************
733 * GdipGetEmHeight [GDIPLUS.@]
735 * Gets the height of the specified family in EmHeights
738 * family [I] Family to retrieve from
739 * style [I] (optional) style
740 * EmHeight [O] return value
744 * FAILURE: InvalidParameter
746 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
748 if (!(family && EmHeight)) return InvalidParameter;
750 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
752 *EmHeight = family->otm.otmEMSquare;
753 TRACE("%d => %u\n", family->otm.otmTextMetrics.tmHeight, *EmHeight);
759 /*******************************************************************************
760 * GdipGetLineSpacing [GDIPLUS.@]
762 * Returns the line spacing in design units
765 * family [I] Family to retrieve from
766 * style [I] (Optional) font style
767 * LineSpacing [O] Return value
771 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
773 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
774 INT style, UINT16* LineSpacing)
776 TRACE("%p, %d, %p\n", family, style, LineSpacing);
778 if (!(family && LineSpacing))
779 return InvalidParameter;
781 if (style) FIXME("ignoring style\n");
783 *LineSpacing = family->otm.otmTextMetrics.tmAscent + family->otm.otmTextMetrics.tmDescent + family->otm.otmTextMetrics.tmExternalLeading;
784 TRACE("%d => %u\n", family->otm.otmTextMetrics.tmHeight, *LineSpacing);
789 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
790 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
792 INT fontstyle = FontStyleRegular;
796 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
797 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
798 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
799 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
801 return (INT)lParam != fontstyle;
804 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
805 INT style, BOOL* IsStyleAvailable)
809 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
811 if (!(family && IsStyleAvailable))
812 return InvalidParameter;
814 *IsStyleAvailable = FALSE;
818 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
819 *IsStyleAvailable = TRUE;
826 /*****************************************************************************
827 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
829 * Obtains a serif family (Courier New on Windows)
832 * **nativeFamily [I] Where the font will be stored
835 * InvalidParameter if nativeFamily is NULL.
838 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
840 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
841 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
844 if (nativeFamily == NULL) return InvalidParameter;
846 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
848 if (stat == FontFamilyNotFound)
849 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
851 if (stat == FontFamilyNotFound)
852 ERR("Missing 'Courier New' font\n");
857 /*****************************************************************************
858 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
860 * Obtains a serif family (Times New Roman on Windows)
863 * **nativeFamily [I] Where the font will be stored
866 * InvalidParameter if nativeFamily is NULL.
869 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
871 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
872 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
875 TRACE("(%p)\n", nativeFamily);
877 if (nativeFamily == NULL) return InvalidParameter;
879 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
881 if (stat == FontFamilyNotFound)
882 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
884 if (stat == FontFamilyNotFound)
885 ERR("Missing 'Times New Roman' font\n");
890 /*****************************************************************************
891 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
893 * Obtains a serif family (Microsoft Sans Serif on Windows)
896 * **nativeFamily [I] Where the font will be stored
899 * InvalidParameter if nativeFamily is NULL.
902 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
905 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
906 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
908 TRACE("(%p)\n", nativeFamily);
910 if (nativeFamily == NULL) return InvalidParameter;
912 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
914 if (stat == FontFamilyNotFound)
915 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
916 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
921 /*****************************************************************************
922 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
924 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
926 TRACE("%p\n", fontCollection);
929 return InvalidParameter;
931 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
932 if (!*fontCollection) return OutOfMemory;
934 (*fontCollection)->FontFamilies = NULL;
935 (*fontCollection)->count = 0;
936 (*fontCollection)->allocated = 0;
938 TRACE("<-- %p\n", *fontCollection);
943 /*****************************************************************************
944 * GdipDeletePrivateFontCollection [GDIPLUS.@]
946 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
950 TRACE("%p\n", fontCollection);
953 return InvalidParameter;
955 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
956 GdipFree(*fontCollection);
961 /*****************************************************************************
962 * GdipPrivateAddFontFile [GDIPLUS.@]
964 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
965 GDIPCONST WCHAR* filename)
967 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
969 if (!(fontCollection && filename))
970 return InvalidParameter;
972 return NotImplemented;
975 /* Copied from msi/font.c */
977 typedef struct _tagTT_OFFSET_TABLE {
978 USHORT uMajorVersion;
979 USHORT uMinorVersion;
982 USHORT uEntrySelector;
986 typedef struct _tagTT_TABLE_DIRECTORY {
987 char szTag[4]; /* table name */
988 ULONG uCheckSum; /* Check sum */
989 ULONG uOffset; /* Offset from beginning of file */
990 ULONG uLength; /* length of the table in bytes */
991 } TT_TABLE_DIRECTORY;
993 typedef struct _tagTT_NAME_TABLE_HEADER {
994 USHORT uFSelector; /* format selector. Always 0 */
995 USHORT uNRCount; /* Name Records count */
996 USHORT uStorageOffset; /* Offset for strings storage,
997 * from start of the table */
998 } TT_NAME_TABLE_HEADER;
1000 #define NAME_ID_FULL_FONT_NAME 4
1001 #define NAME_ID_VERSION 5
1003 typedef struct _tagTT_NAME_RECORD {
1008 USHORT uStringLength;
1009 USHORT uStringOffset; /* from start of storage area */
1012 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1013 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1016 * Code based off of code located here
1017 * http://www.codeproject.com/gdi/fontnamefromfile.asp
1019 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1021 const TT_TABLE_DIRECTORY *tblDir;
1022 TT_OFFSET_TABLE ttOffsetTable;
1023 TT_NAME_TABLE_HEADER ttNTHeader;
1024 TT_NAME_RECORD ttRecord;
1028 if (sizeof(TT_OFFSET_TABLE) > size)
1030 ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1031 ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1032 ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1033 ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1035 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1038 pos = sizeof(ttOffsetTable);
1039 for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1041 tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1042 pos += sizeof(*tblDir);
1043 if (memcmp(tblDir->szTag,"name",4)==0)
1045 ofs = SWAPLONG(tblDir->uOffset);
1049 if (i >= ttOffsetTable.uNumOfTables)
1052 pos = ofs + sizeof(ttNTHeader);
1055 ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1056 ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1057 ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1058 for(i=0; i<ttNTHeader.uNRCount; i++)
1060 ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1061 pos += sizeof(ttRecord);
1065 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1066 if (ttRecord.uNameID == id)
1070 ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1071 ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1072 if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1074 buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1075 len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1083 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1085 /*****************************************************************************
1086 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1088 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1089 GDIPCONST void* memory, INT length)
1091 WCHAR buf[32], *name;
1094 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1096 if (!fontCollection || !memory || !length)
1097 return InvalidParameter;
1099 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1103 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1104 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1105 if (!font || !count)
1106 return InvalidParameter;
1115 lfw.lfCharSet = DEFAULT_CHARSET;
1116 lstrcpyW(lfw.lfFaceName, name);
1117 lfw.lfPitchAndFamily = 0;
1119 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1130 /*****************************************************************************
1131 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1133 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1134 GpFontCollection* fontCollection, INT* numFound)
1136 TRACE("%p, %p\n", fontCollection, numFound);
1138 if (!(fontCollection && numFound))
1139 return InvalidParameter;
1141 *numFound = fontCollection->count;
1145 /*****************************************************************************
1146 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1148 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1149 GpFontCollection* fontCollection, INT numSought,
1150 GpFontFamily* gpfamilies[], INT* numFound)
1155 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1157 if (!(fontCollection && gpfamilies && numFound))
1158 return InvalidParameter;
1160 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1162 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1164 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1172 for (i=0; i<numToFree; i++)
1174 GdipDeleteFontFamily(gpfamilies[i]);
1175 gpfamilies[i] = NULL;
1182 void free_installed_fonts(void)
1184 while (installedFontCollection.count)
1185 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1186 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1187 installedFontCollection.FontFamilies = NULL;
1188 installedFontCollection.allocated = 0;
1191 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1192 DWORD type, LPARAM lParam)
1194 GpFontCollection* fonts = (GpFontCollection*)lParam;
1197 if (type == RASTER_FONTTYPE)
1200 /* skip duplicates */
1201 for (i=0; i<fonts->count; i++)
1202 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1205 if (fonts->allocated == fonts->count)
1207 INT new_alloc_count = fonts->allocated+50;
1208 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1210 if (!new_family_list)
1213 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1214 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1215 fonts->FontFamilies = new_family_list;
1216 fonts->allocated = new_alloc_count;
1219 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1227 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1228 GpFontCollection** fontCollection)
1230 TRACE("(%p)\n",fontCollection);
1232 if (!fontCollection)
1233 return InvalidParameter;
1235 if (installedFontCollection.count == 0)
1242 lfw.lfCharSet = DEFAULT_CHARSET;
1243 lfw.lfFaceName[0] = 0;
1244 lfw.lfPitchAndFamily = 0;
1246 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1248 free_installed_fonts();
1256 *fontCollection = &installedFontCollection;