2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
35 #include "gdiplus_private.h"
37 /* PANOSE is 10 bytes in size, need to pack the structure properly */
46 SHORT ySubscriptXSize;
47 SHORT ySubscriptYSize;
48 SHORT ySubscriptXOffset;
49 SHORT ySubscriptYOffset;
50 SHORT ySuperscriptXSize;
51 SHORT ySuperscriptYSize;
52 SHORT ySuperscriptXOffset;
53 SHORT ySuperscriptYOffset;
55 SHORT yStrikeoutPosition;
58 ULONG ulUnicodeRange1;
59 ULONG ulUnicodeRange2;
60 ULONG ulUnicodeRange3;
61 ULONG ulUnicodeRange4;
64 USHORT usFirstCharIndex;
65 USHORT usLastCharIndex;
66 /* According to the Apple spec, original version didn't have the below fields,
67 * version numbers were taken from the OpenType spec.
69 /* version 0 (TrueType 1.5) */
71 USHORT sTypoDescender;
75 /* version 1 (TrueType 1.66) */
76 ULONG ulCodePageRange1;
77 ULONG ulCodePageRange2;
78 /* version 2 (OpenType 1.2) */
92 USHORT advanceWidthMax;
93 SHORT minLeftSideBearing;
94 SHORT minRightSideBearing;
100 SHORT metricDataFormat;
101 USHORT numberOfHMetrics;
105 #ifdef WORDS_BIGENDIAN
106 #define GET_BE_WORD(x) (x)
107 #define GET_BE_DWORD(x) (x)
109 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
110 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
113 #define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
114 ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
115 ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
116 #define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
117 #define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
119 static GpStatus clone_font_family(const GpFontFamily *, GpFontFamily **);
121 static const REAL mm_per_inch = 25.4;
122 static const REAL inch_per_point = 1.0/72.0;
124 static GpFontCollection installedFontCollection = {0};
126 static LONG em_size_to_pixel(REAL em_size, Unit unit, LONG dpi)
131 FIXME("Unhandled unit type: %d\n", unit);
136 /* FIXME: Figure out when World != Pixel */
139 FIXME("Unknown behavior for UnitDisplay! Please report!\n");
140 /* FIXME: Figure out how this works...
141 * MSDN says that if "DISPLAY" is a monitor, then pixel should be
142 * used. That's not what I got. Tests on Windows revealed no output,
143 * and the tests in tests/font crash windows */
146 return em_size * dpi * inch_per_point;
148 return em_size * dpi;
150 return em_size * dpi / 300.0; /* Per MSDN */
152 return em_size * dpi / mm_per_inch;
156 /*******************************************************************************
157 * GdipCreateFont [GDIPLUS.@]
159 * Create a new font based off of a FontFamily
162 * *fontFamily [I] Family to base the font off of
163 * emSize [I] Size of the font
164 * style [I] Bitwise OR of FontStyle enumeration
165 * unit [I] Unit emSize is measured in
166 * **font [I] the resulting Font object
170 * FAILURE: InvalidParameter if fontfamily or font is NULL.
171 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
174 * UnitDisplay is unsupported.
175 * emSize is stored separately from lfHeight, to hold the fraction.
177 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
178 REAL emSize, INT style, Unit unit, GpFont **font)
181 OUTLINETEXTMETRICW otm;
187 if (!fontFamily || !font || emSize < 0.0)
188 return InvalidParameter;
190 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
191 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
193 memset(&lfw, 0, sizeof(lfw));
195 stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
196 if (stat != Ok) return stat;
198 lfw.lfHeight = -em_size_to_pixel(emSize, unit, fontFamily->dpi);
199 lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
200 lfw.lfItalic = style & FontStyleItalic;
201 lfw.lfUnderline = style & FontStyleUnderline;
202 lfw.lfStrikeOut = style & FontStyleStrikeout;
204 hfont = CreateFontIndirectW(&lfw);
205 hdc = CreateCompatibleDC(0);
206 SelectObject(hdc, hfont);
207 otm.otmSize = sizeof(otm);
208 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
212 if (!ret) return NotTrueTypeFont;
214 *font = GdipAlloc(sizeof(GpFont));
215 if (!*font) return OutOfMemory;
217 (*font)->unit = unit;
218 (*font)->emSize = emSize;
221 stat = clone_font_family(fontFamily, &(*font)->family);
228 TRACE("<-- %p\n", *font);
233 /*******************************************************************************
234 * GdipCreateFontFromLogfontW [GDIPLUS.@]
236 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
237 GDIPCONST LOGFONTW *logfont, GpFont **font)
239 HFONT hfont, oldfont;
240 OUTLINETEXTMETRICW otm;
244 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
246 if (!hdc || !logfont || !font)
247 return InvalidParameter;
249 hfont = CreateFontIndirectW(logfont);
250 oldfont = SelectObject(hdc, hfont);
251 otm.otmSize = sizeof(otm);
252 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
253 SelectObject(hdc, oldfont);
256 if (!ret) return NotTrueTypeFont;
258 *font = GdipAlloc(sizeof(GpFont));
259 if (!*font) return OutOfMemory;
261 (*font)->unit = UnitWorld;
262 (*font)->emSize = otm.otmTextMetrics.tmAscent;
265 stat = GdipCreateFontFamilyFromName(logfont->lfFaceName, NULL, &(*font)->family);
269 return NotTrueTypeFont;
272 TRACE("<-- %p\n", *font);
277 /*******************************************************************************
278 * GdipCreateFontFromLogfontA [GDIPLUS.@]
280 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
281 GDIPCONST LOGFONTA *lfa, GpFont **font)
285 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
288 return InvalidParameter;
290 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
292 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
295 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
298 /*******************************************************************************
299 * GdipDeleteFont [GDIPLUS.@]
301 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
303 TRACE("(%p)\n", font);
306 return InvalidParameter;
308 GdipDeleteFontFamily(font->family);
314 /*******************************************************************************
315 * GdipCreateFontFromDC [GDIPLUS.@]
317 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
322 TRACE("(%p, %p)\n", hdc, font);
325 return InvalidParameter;
327 hfont = GetCurrentObject(hdc, OBJ_FONT);
331 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
334 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
337 /*******************************************************************************
338 * GdipGetFamily [GDIPLUS.@]
340 * Returns the FontFamily for the specified Font
343 * font [I] Font to request from
344 * family [O] Resulting FontFamily object
348 * FAILURE: An element of GpStatus
350 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
352 TRACE("%p %p\n", font, family);
354 if (!(font && family))
355 return InvalidParameter;
357 return GdipCloneFontFamily(font->family, family);
360 static REAL get_font_size(const GpFont *font)
365 /******************************************************************************
366 * GdipGetFontSize [GDIPLUS.@]
368 * Returns the size of the font in Units
371 * *font [I] The font to retrieve size from
372 * *size [O] Pointer to hold retrieved value
376 * FAILURE: InvalidParameter (font or size was NULL)
379 * Size returned is actually emSize -- not internal size used for drawing.
381 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
383 TRACE("(%p, %p)\n", font, size);
385 if (!(font && size)) return InvalidParameter;
387 *size = get_font_size(font);
388 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
393 static INT get_font_style(const GpFont *font)
397 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
398 style = FontStyleBold;
400 style = FontStyleRegular;
401 if (font->otm.otmTextMetrics.tmItalic)
402 style |= FontStyleItalic;
403 if (font->otm.otmTextMetrics.tmUnderlined)
404 style |= FontStyleUnderline;
405 if (font->otm.otmTextMetrics.tmStruckOut)
406 style |= FontStyleStrikeout;
411 /*******************************************************************************
412 * GdipGetFontStyle [GDIPLUS.@]
414 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
417 * font [I] font to request from
418 * style [O] resulting pointer to a FontStyle enumeration
422 * FAILURE: InvalidParameter
424 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
426 TRACE("%p %p\n", font, style);
428 if (!(font && style))
429 return InvalidParameter;
431 *style = get_font_style(font);
432 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
437 /*******************************************************************************
438 * GdipGetFontUnit [GDIPLUS.@]
441 * font [I] Font to retrieve from
442 * unit [O] Return value
445 * FAILURE: font or unit was NULL
448 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
450 TRACE("(%p, %p)\n", font, unit);
452 if (!(font && unit)) return InvalidParameter;
455 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
460 /*******************************************************************************
461 * GdipGetLogFontA [GDIPLUS.@]
463 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
469 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
471 status = GdipGetLogFontW(font, graphics, &lfw);
475 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
477 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
483 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
485 /* FIXME: use graphics */
486 lf->lfHeight = -em_size_to_pixel(font->emSize, font->unit, font->family->dpi);
488 lf->lfEscapement = 0;
489 lf->lfOrientation = 0;
490 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
491 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
492 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
493 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
494 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
495 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
496 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
497 lf->lfQuality = DEFAULT_QUALITY;
498 lf->lfPitchAndFamily = 0;
499 strcpyW(lf->lfFaceName, font->family->FamilyName);
502 /*******************************************************************************
503 * GdipGetLogFontW [GDIPLUS.@]
505 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
508 TRACE("(%p, %p, %p)\n", font, graphics, lfw);
510 if(!font || !graphics || !lfw)
511 return InvalidParameter;
513 get_log_fontW(font, graphics, lfw);
514 TRACE("=> %s,%d\n", debugstr_w(lfw->lfFaceName), lfw->lfHeight);
519 /*******************************************************************************
520 * GdipCloneFont [GDIPLUS.@]
522 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
526 TRACE("(%p, %p)\n", font, cloneFont);
528 if(!font || !cloneFont)
529 return InvalidParameter;
531 *cloneFont = GdipAlloc(sizeof(GpFont));
532 if(!*cloneFont) return OutOfMemory;
535 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
536 if (stat != Ok) GdipFree(*cloneFont);
541 /*******************************************************************************
542 * GdipGetFontHeight [GDIPLUS.@]
544 * font [I] Font to retrieve height from
545 * graphics [I] The current graphics context
546 * height [O] Resulting height
549 * FAILURE: Another element of GpStatus
552 * Forwards to GdipGetFontHeightGivenDPI
554 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
555 GDIPCONST GpGraphics *graphics, REAL *height)
560 TRACE("%p %p %p\n", font, graphics, height);
564 stat = GdipGetDpiY((GpGraphics*)graphics, &dpi);
565 if (stat != Ok) return stat;
568 dpi = font->family->dpi;
570 return GdipGetFontHeightGivenDPI(font, dpi, height);
573 /*******************************************************************************
574 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
576 * font [I] Font to retrieve DPI from
577 * dpi [I] DPI to assume
578 * height [O] Return value
582 * FAILURE: InvalidParameter if font or height is NULL
585 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
586 * (for anything other than unit Pixel)
588 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
592 UINT16 line_spacing, em_height;
593 REAL font_height, font_size;
595 if (!font || !height) return InvalidParameter;
597 TRACE("%p (%s), %f, %p\n", font,
598 debugstr_w(font->family->FamilyName), dpi, height);
600 font_size = get_font_size(font);
601 style = get_font_style(font);
602 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
603 if (stat != Ok) return stat;
604 stat = GdipGetEmHeight(font->family, style, &em_height);
605 if (stat != Ok) return stat;
607 font_height = (REAL)line_spacing * font_size / (REAL)em_height;
613 *height = font_height;
616 *height = font_height * dpi * inch_per_point;
619 *height = font_height * dpi;
622 *height = font_height * (dpi / 300.0);
625 *height = font_height * (dpi / mm_per_inch);
628 FIXME("Unhandled unit type: %d\n", font->unit);
629 return NotImplemented;
632 TRACE("%s,%d(unit %d) => %f\n",
633 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, font->unit, *height);
638 /***********************************************************************
639 * Borrowed from GDI32:
641 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
642 * We have to use other types because of the FONTENUMPROCW definition.
644 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
645 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
647 if (type != TRUETYPE_FONTTYPE)
650 *(LOGFONTW *)lParam = *elf;
657 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
661 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
663 OUTLINETEXTMETRICW otm;
669 otm.otmSize = sizeof(otm);
670 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
672 fm->em_height = otm.otmEMSquare;
673 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
675 memset(&tt_hori, 0, sizeof(tt_hori));
676 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
678 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
679 fm->descent = -GET_BE_WORD(tt_hori.Descender);
680 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
681 line_gap = GET_BE_WORD(tt_hori.LineGap);
682 fm->line_spacing = fm->ascent + fm->descent + line_gap;
683 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
684 if (fm->ascent + fm->descent != 0) return TRUE;
687 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
688 if (size == GDI_ERROR) return FALSE;
690 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
692 memset(&tt_os2, 0, sizeof(tt_os2));
693 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
695 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
696 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
697 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
698 if (fm->ascent + fm->descent == 0)
700 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
701 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
702 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
704 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
705 fm->line_spacing = fm->ascent + fm->descent + line_gap;
706 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
710 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
713 HDC hdc = CreateCompatibleDC(0);
714 GpStatus ret = FontFamilyNotFound;
716 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
718 HFONT hfont, old_font;
720 hfont = CreateFontIndirectW(&lf);
721 old_font = SelectObject(hdc, hfont);
722 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
723 SelectObject(hdc, old_font);
731 /*******************************************************************************
732 * GdipCreateFontFamilyFromName [GDIPLUS.@]
734 * Creates a font family object based on a supplied name
737 * name [I] Name of the font
738 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
739 * FontFamily [O] Pointer to the resulting FontFamily object
743 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
744 * FAILURE: Invalid parameter if FontFamily or name is NULL
747 * If fontCollection is NULL then the object is not part of any collection
751 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
752 GpFontCollection *fontCollection,
753 GpFontFamily **FontFamily)
756 GpFontFamily* ffamily;
757 struct font_metrics fm;
759 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
761 if (!(name && FontFamily))
762 return InvalidParameter;
764 FIXME("No support for FontCollections yet!\n");
766 stat = find_installed_font(name, &fm);
767 if (stat != Ok) return stat;
769 ffamily = GdipAlloc(sizeof (GpFontFamily));
770 if (!ffamily) return OutOfMemory;
772 lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
773 ffamily->em_height = fm.em_height;
774 ffamily->ascent = fm.ascent;
775 ffamily->descent = fm.descent;
776 ffamily->line_spacing = fm.line_spacing;
777 ffamily->dpi = fm.dpi;
779 *FontFamily = ffamily;
781 TRACE("<-- %p\n", ffamily);
786 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
788 *clone = GdipAlloc(sizeof(GpFontFamily));
789 if (!*clone) return OutOfMemory;
796 /*******************************************************************************
797 * GdipCloneFontFamily [GDIPLUS.@]
799 * Creates a deep copy of a Font Family object
802 * FontFamily [I] Font to clone
803 * clonedFontFamily [O] The resulting cloned font
808 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
812 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
814 TRACE("%p (%s), %p\n", FontFamily,
815 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
817 status = clone_font_family(FontFamily, clonedFontFamily);
818 if (status != Ok) return status;
820 TRACE("<-- %p\n", *clonedFontFamily);
825 /*******************************************************************************
826 * GdipGetFamilyName [GDIPLUS.@]
828 * Returns the family name into name
831 * *family [I] Family to retrieve from
832 * *name [O] WCHARS of the family name
837 * FAILURE: InvalidParameter if family is NULL
840 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
842 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
843 WCHAR *name, LANGID language)
845 static int lang_fixme;
848 return InvalidParameter;
850 TRACE("%p, %p, %d\n", family, name, language);
852 if (language != LANG_NEUTRAL && !lang_fixme++)
853 FIXME("No support for handling of multiple languages!\n");
855 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
861 /*****************************************************************************
862 * GdipDeleteFontFamily [GDIPLUS.@]
864 * Removes the specified FontFamily
867 * *FontFamily [I] The family to delete
871 * FAILURE: InvalidParameter if FontFamily is NULL.
874 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
877 return InvalidParameter;
878 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
880 GdipFree (FontFamily);
885 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
886 INT style, UINT16* CellAscent)
888 if (!(family && CellAscent)) return InvalidParameter;
890 *CellAscent = family->ascent;
891 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
896 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
897 INT style, UINT16* CellDescent)
899 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
901 if (!(family && CellDescent)) return InvalidParameter;
903 *CellDescent = family->descent;
904 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
909 /*******************************************************************************
910 * GdipGetEmHeight [GDIPLUS.@]
912 * Gets the height of the specified family in EmHeights
915 * family [I] Family to retrieve from
916 * style [I] (optional) style
917 * EmHeight [O] return value
921 * FAILURE: InvalidParameter
923 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
925 if (!(family && EmHeight)) return InvalidParameter;
927 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
929 *EmHeight = family->em_height;
930 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
936 /*******************************************************************************
937 * GdipGetLineSpacing [GDIPLUS.@]
939 * Returns the line spacing in design units
942 * family [I] Family to retrieve from
943 * style [I] (Optional) font style
944 * LineSpacing [O] Return value
948 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
950 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
951 INT style, UINT16* LineSpacing)
953 TRACE("%p, %d, %p\n", family, style, LineSpacing);
955 if (!(family && LineSpacing))
956 return InvalidParameter;
958 if (style) FIXME("ignoring style\n");
960 *LineSpacing = family->line_spacing;
961 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
966 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
967 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
969 INT fontstyle = FontStyleRegular;
973 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
974 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
975 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
976 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
978 return (INT)lParam != fontstyle;
981 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
982 INT style, BOOL* IsStyleAvailable)
986 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
988 if (!(family && IsStyleAvailable))
989 return InvalidParameter;
991 *IsStyleAvailable = FALSE;
995 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
996 *IsStyleAvailable = TRUE;
1003 /*****************************************************************************
1004 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
1006 * Obtains a serif family (Courier New on Windows)
1009 * **nativeFamily [I] Where the font will be stored
1012 * InvalidParameter if nativeFamily is NULL.
1015 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
1017 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
1018 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
1021 if (nativeFamily == NULL) return InvalidParameter;
1023 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
1025 if (stat == FontFamilyNotFound)
1026 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1028 if (stat == FontFamilyNotFound)
1029 ERR("Missing 'Courier New' font\n");
1034 /*****************************************************************************
1035 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1037 * Obtains a serif family (Times New Roman on Windows)
1040 * **nativeFamily [I] Where the font will be stored
1043 * InvalidParameter if nativeFamily is NULL.
1046 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1048 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1049 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1052 TRACE("(%p)\n", nativeFamily);
1054 if (nativeFamily == NULL) return InvalidParameter;
1056 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1058 if (stat == FontFamilyNotFound)
1059 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1061 if (stat == FontFamilyNotFound)
1062 ERR("Missing 'Times New Roman' font\n");
1067 /*****************************************************************************
1068 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1070 * Obtains a serif family (Microsoft Sans Serif on Windows)
1073 * **nativeFamily [I] Where the font will be stored
1076 * InvalidParameter if nativeFamily is NULL.
1079 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1082 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1083 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1085 TRACE("(%p)\n", nativeFamily);
1087 if (nativeFamily == NULL) return InvalidParameter;
1089 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1091 if (stat == FontFamilyNotFound)
1092 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1093 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1098 /*****************************************************************************
1099 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1101 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1103 TRACE("%p\n", fontCollection);
1105 if (!fontCollection)
1106 return InvalidParameter;
1108 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1109 if (!*fontCollection) return OutOfMemory;
1111 (*fontCollection)->FontFamilies = NULL;
1112 (*fontCollection)->count = 0;
1113 (*fontCollection)->allocated = 0;
1115 TRACE("<-- %p\n", *fontCollection);
1120 /*****************************************************************************
1121 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1123 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1127 TRACE("%p\n", fontCollection);
1129 if (!fontCollection)
1130 return InvalidParameter;
1132 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1133 GdipFree(*fontCollection);
1138 /*****************************************************************************
1139 * GdipPrivateAddFontFile [GDIPLUS.@]
1141 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
1142 GDIPCONST WCHAR* filename)
1144 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
1146 if (!(fontCollection && filename))
1147 return InvalidParameter;
1149 return NotImplemented;
1152 /* Copied from msi/font.c */
1154 typedef struct _tagTT_OFFSET_TABLE {
1155 USHORT uMajorVersion;
1156 USHORT uMinorVersion;
1157 USHORT uNumOfTables;
1158 USHORT uSearchRange;
1159 USHORT uEntrySelector;
1163 typedef struct _tagTT_TABLE_DIRECTORY {
1164 char szTag[4]; /* table name */
1165 ULONG uCheckSum; /* Check sum */
1166 ULONG uOffset; /* Offset from beginning of file */
1167 ULONG uLength; /* length of the table in bytes */
1168 } TT_TABLE_DIRECTORY;
1170 typedef struct _tagTT_NAME_TABLE_HEADER {
1171 USHORT uFSelector; /* format selector. Always 0 */
1172 USHORT uNRCount; /* Name Records count */
1173 USHORT uStorageOffset; /* Offset for strings storage,
1174 * from start of the table */
1175 } TT_NAME_TABLE_HEADER;
1177 #define NAME_ID_FULL_FONT_NAME 4
1178 #define NAME_ID_VERSION 5
1180 typedef struct _tagTT_NAME_RECORD {
1185 USHORT uStringLength;
1186 USHORT uStringOffset; /* from start of storage area */
1189 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1190 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1193 * Code based off of code located here
1194 * http://www.codeproject.com/gdi/fontnamefromfile.asp
1196 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1198 const TT_TABLE_DIRECTORY *tblDir;
1199 TT_OFFSET_TABLE ttOffsetTable;
1200 TT_NAME_TABLE_HEADER ttNTHeader;
1201 TT_NAME_RECORD ttRecord;
1205 if (sizeof(TT_OFFSET_TABLE) > size)
1207 ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1208 ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1209 ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1210 ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1212 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1215 pos = sizeof(ttOffsetTable);
1216 for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1218 tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1219 pos += sizeof(*tblDir);
1220 if (memcmp(tblDir->szTag,"name",4)==0)
1222 ofs = SWAPLONG(tblDir->uOffset);
1226 if (i >= ttOffsetTable.uNumOfTables)
1229 pos = ofs + sizeof(ttNTHeader);
1232 ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1233 ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1234 ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1235 for(i=0; i<ttNTHeader.uNRCount; i++)
1237 ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1238 pos += sizeof(ttRecord);
1242 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1243 if (ttRecord.uNameID == id)
1247 ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1248 ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1249 if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1251 buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1252 len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1260 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1262 /*****************************************************************************
1263 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1265 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1266 GDIPCONST void* memory, INT length)
1268 WCHAR buf[32], *name;
1271 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1273 if (!fontCollection || !memory || !length)
1274 return InvalidParameter;
1276 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1280 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1281 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1282 if (!font || !count)
1283 return InvalidParameter;
1292 lfw.lfCharSet = DEFAULT_CHARSET;
1293 lstrcpyW(lfw.lfFaceName, name);
1294 lfw.lfPitchAndFamily = 0;
1296 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1307 /*****************************************************************************
1308 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1310 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1311 GpFontCollection* fontCollection, INT* numFound)
1313 TRACE("%p, %p\n", fontCollection, numFound);
1315 if (!(fontCollection && numFound))
1316 return InvalidParameter;
1318 *numFound = fontCollection->count;
1322 /*****************************************************************************
1323 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1325 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1326 GpFontCollection* fontCollection, INT numSought,
1327 GpFontFamily* gpfamilies[], INT* numFound)
1332 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1334 if (!(fontCollection && gpfamilies && numFound))
1335 return InvalidParameter;
1337 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1339 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1341 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1349 for (i=0; i<numToFree; i++)
1351 GdipDeleteFontFamily(gpfamilies[i]);
1352 gpfamilies[i] = NULL;
1359 void free_installed_fonts(void)
1361 while (installedFontCollection.count)
1362 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1363 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1364 installedFontCollection.FontFamilies = NULL;
1365 installedFontCollection.allocated = 0;
1368 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1369 DWORD type, LPARAM lParam)
1371 GpFontCollection* fonts = (GpFontCollection*)lParam;
1374 if (type == RASTER_FONTTYPE)
1377 /* skip duplicates */
1378 for (i=0; i<fonts->count; i++)
1379 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1382 if (fonts->allocated == fonts->count)
1384 INT new_alloc_count = fonts->allocated+50;
1385 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1387 if (!new_family_list)
1390 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1391 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1392 fonts->FontFamilies = new_family_list;
1393 fonts->allocated = new_alloc_count;
1396 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1404 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1405 GpFontCollection** fontCollection)
1407 TRACE("(%p)\n",fontCollection);
1409 if (!fontCollection)
1410 return InvalidParameter;
1412 if (installedFontCollection.count == 0)
1419 lfw.lfCharSet = DEFAULT_CHARSET;
1420 lfw.lfFaceName[0] = 0;
1421 lfw.lfPitchAndFamily = 0;
1423 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1425 free_installed_fonts();
1433 *fontCollection = &installedFontCollection;