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 GpFontCollection installedFontCollection = {0};
123 /*******************************************************************************
124 * GdipCreateFont [GDIPLUS.@]
126 * Create a new font based off of a FontFamily
129 * *fontFamily [I] Family to base the font off of
130 * emSize [I] Size of the font
131 * style [I] Bitwise OR of FontStyle enumeration
132 * unit [I] Unit emSize is measured in
133 * **font [I] the resulting Font object
137 * FAILURE: InvalidParameter if fontfamily or font is NULL.
138 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
141 * UnitDisplay is unsupported.
142 * emSize is stored separately from lfHeight, to hold the fraction.
144 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
145 REAL emSize, INT style, Unit unit, GpFont **font)
148 OUTLINETEXTMETRICW otm;
154 if (!fontFamily || !font || emSize < 0.0)
155 return InvalidParameter;
157 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
158 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
160 memset(&lfw, 0, sizeof(lfw));
162 stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
163 if (stat != Ok) return stat;
165 lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
166 lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
167 lfw.lfItalic = style & FontStyleItalic;
168 lfw.lfUnderline = style & FontStyleUnderline;
169 lfw.lfStrikeOut = style & FontStyleStrikeout;
171 hfont = CreateFontIndirectW(&lfw);
172 hdc = CreateCompatibleDC(0);
173 SelectObject(hdc, hfont);
174 otm.otmSize = sizeof(otm);
175 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
179 if (!ret) return NotTrueTypeFont;
181 *font = GdipAlloc(sizeof(GpFont));
182 if (!*font) return OutOfMemory;
184 (*font)->unit = unit;
185 (*font)->emSize = emSize;
188 stat = clone_font_family(fontFamily, &(*font)->family);
195 TRACE("<-- %p\n", *font);
200 /*******************************************************************************
201 * GdipCreateFontFromLogfontW [GDIPLUS.@]
203 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
204 GDIPCONST LOGFONTW *logfont, GpFont **font)
206 HFONT hfont, oldfont;
207 OUTLINETEXTMETRICW otm;
211 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
213 if (!hdc || !logfont || !font)
214 return InvalidParameter;
216 hfont = CreateFontIndirectW(logfont);
217 oldfont = SelectObject(hdc, hfont);
218 otm.otmSize = sizeof(otm);
219 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
220 SelectObject(hdc, oldfont);
223 if (!ret) return NotTrueTypeFont;
225 *font = GdipAlloc(sizeof(GpFont));
226 if (!*font) return OutOfMemory;
228 (*font)->unit = UnitWorld;
229 (*font)->emSize = otm.otmTextMetrics.tmAscent;
232 stat = GdipCreateFontFamilyFromName(logfont->lfFaceName, NULL, &(*font)->family);
236 return NotTrueTypeFont;
239 TRACE("<-- %p\n", *font);
244 /*******************************************************************************
245 * GdipCreateFontFromLogfontA [GDIPLUS.@]
247 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
248 GDIPCONST LOGFONTA *lfa, GpFont **font)
252 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
255 return InvalidParameter;
257 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
259 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
262 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
265 /*******************************************************************************
266 * GdipDeleteFont [GDIPLUS.@]
268 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
270 TRACE("(%p)\n", font);
273 return InvalidParameter;
275 GdipDeleteFontFamily(font->family);
281 /*******************************************************************************
282 * GdipCreateFontFromDC [GDIPLUS.@]
284 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
289 TRACE("(%p, %p)\n", hdc, font);
292 return InvalidParameter;
294 hfont = GetCurrentObject(hdc, OBJ_FONT);
298 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
301 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
304 /*******************************************************************************
305 * GdipGetFamily [GDIPLUS.@]
307 * Returns the FontFamily for the specified Font
310 * font [I] Font to request from
311 * family [O] Resulting FontFamily object
315 * FAILURE: An element of GpStatus
317 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
319 TRACE("%p %p\n", font, family);
321 if (!(font && family))
322 return InvalidParameter;
324 return GdipCloneFontFamily(font->family, family);
327 static REAL get_font_size(const GpFont *font)
332 /******************************************************************************
333 * GdipGetFontSize [GDIPLUS.@]
335 * Returns the size of the font in Units
338 * *font [I] The font to retrieve size from
339 * *size [O] Pointer to hold retrieved value
343 * FAILURE: InvalidParameter (font or size was NULL)
346 * Size returned is actually emSize -- not internal size used for drawing.
348 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
350 TRACE("(%p, %p)\n", font, size);
352 if (!(font && size)) return InvalidParameter;
354 *size = get_font_size(font);
355 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
360 static INT get_font_style(const GpFont *font)
364 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
365 style = FontStyleBold;
367 style = FontStyleRegular;
368 if (font->otm.otmTextMetrics.tmItalic)
369 style |= FontStyleItalic;
370 if (font->otm.otmTextMetrics.tmUnderlined)
371 style |= FontStyleUnderline;
372 if (font->otm.otmTextMetrics.tmStruckOut)
373 style |= FontStyleStrikeout;
378 /*******************************************************************************
379 * GdipGetFontStyle [GDIPLUS.@]
381 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
384 * font [I] font to request from
385 * style [O] resulting pointer to a FontStyle enumeration
389 * FAILURE: InvalidParameter
391 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
393 TRACE("%p %p\n", font, style);
395 if (!(font && style))
396 return InvalidParameter;
398 *style = get_font_style(font);
399 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
404 /*******************************************************************************
405 * GdipGetFontUnit [GDIPLUS.@]
408 * font [I] Font to retrieve from
409 * unit [O] Return value
412 * FAILURE: font or unit was NULL
415 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
417 TRACE("(%p, %p)\n", font, unit);
419 if (!(font && unit)) return InvalidParameter;
422 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
427 /*******************************************************************************
428 * GdipGetLogFontA [GDIPLUS.@]
430 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
436 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
438 status = GdipGetLogFontW(font, graphics, &lfw);
442 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
444 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
450 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
452 /* FIXME: use graphics */
453 lf->lfHeight = -units_to_pixels(font->emSize, font->unit, font->family->dpi);
455 lf->lfEscapement = 0;
456 lf->lfOrientation = 0;
457 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
458 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
459 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
460 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
461 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
462 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
463 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
464 lf->lfQuality = DEFAULT_QUALITY;
465 lf->lfPitchAndFamily = 0;
466 strcpyW(lf->lfFaceName, font->family->FamilyName);
469 /*******************************************************************************
470 * GdipGetLogFontW [GDIPLUS.@]
472 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
475 TRACE("(%p, %p, %p)\n", font, graphics, lfw);
477 if(!font || !graphics || !lfw)
478 return InvalidParameter;
480 get_log_fontW(font, graphics, lfw);
481 TRACE("=> %s,%d\n", debugstr_w(lfw->lfFaceName), lfw->lfHeight);
486 /*******************************************************************************
487 * GdipCloneFont [GDIPLUS.@]
489 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
493 TRACE("(%p, %p)\n", font, cloneFont);
495 if(!font || !cloneFont)
496 return InvalidParameter;
498 *cloneFont = GdipAlloc(sizeof(GpFont));
499 if(!*cloneFont) return OutOfMemory;
502 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
503 if (stat != Ok) GdipFree(*cloneFont);
508 /*******************************************************************************
509 * GdipGetFontHeight [GDIPLUS.@]
511 * font [I] Font to retrieve height from
512 * graphics [I] The current graphics context
513 * height [O] Resulting height
516 * FAILURE: Another element of GpStatus
519 * Forwards to GdipGetFontHeightGivenDPI
521 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
522 GDIPCONST GpGraphics *graphics, REAL *height)
527 TRACE("%p %p %p\n", font, graphics, height);
531 stat = GdipGetDpiY((GpGraphics*)graphics, &dpi);
532 if (stat != Ok) return stat;
535 dpi = font->family->dpi;
537 return GdipGetFontHeightGivenDPI(font, dpi, height);
540 /*******************************************************************************
541 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
543 * font [I] Font to retrieve DPI from
544 * dpi [I] DPI to assume
545 * height [O] Return value
549 * FAILURE: InvalidParameter if font or height is NULL
552 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
553 * (for anything other than unit Pixel)
555 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
559 UINT16 line_spacing, em_height;
562 if (!font || !height) return InvalidParameter;
564 TRACE("%p (%s), %f, %p\n", font,
565 debugstr_w(font->family->FamilyName), dpi, height);
567 font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
568 style = get_font_style(font);
569 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
570 if (stat != Ok) return stat;
571 stat = GdipGetEmHeight(font->family, style, &em_height);
572 if (stat != Ok) return stat;
574 *height = (REAL)line_spacing * font_size / (REAL)em_height;
576 TRACE("%s,%d => %f\n",
577 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
582 /***********************************************************************
583 * Borrowed from GDI32:
585 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
586 * We have to use other types because of the FONTENUMPROCW definition.
588 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
589 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
591 if (type & RASTER_FONTTYPE)
594 *(LOGFONTW *)lParam = *elf;
601 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
605 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
607 OUTLINETEXTMETRICW otm;
613 otm.otmSize = sizeof(otm);
614 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
616 fm->em_height = otm.otmEMSquare;
617 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
619 memset(&tt_hori, 0, sizeof(tt_hori));
620 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
622 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
623 fm->descent = -GET_BE_WORD(tt_hori.Descender);
624 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
625 line_gap = GET_BE_WORD(tt_hori.LineGap);
626 fm->line_spacing = fm->ascent + fm->descent + line_gap;
627 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
628 if (fm->ascent + fm->descent != 0) return TRUE;
631 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
632 if (size == GDI_ERROR) return FALSE;
634 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
636 memset(&tt_os2, 0, sizeof(tt_os2));
637 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
639 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
640 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
641 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
642 if (fm->ascent + fm->descent == 0)
644 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
645 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
646 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
648 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
649 fm->line_spacing = fm->ascent + fm->descent + line_gap;
650 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
654 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
657 HDC hdc = CreateCompatibleDC(0);
658 GpStatus ret = FontFamilyNotFound;
660 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
662 HFONT hfont, old_font;
664 hfont = CreateFontIndirectW(&lf);
665 old_font = SelectObject(hdc, hfont);
666 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
667 SelectObject(hdc, old_font);
675 /*******************************************************************************
676 * GdipCreateFontFamilyFromName [GDIPLUS.@]
678 * Creates a font family object based on a supplied name
681 * name [I] Name of the font
682 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
683 * FontFamily [O] Pointer to the resulting FontFamily object
687 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
688 * FAILURE: Invalid parameter if FontFamily or name is NULL
691 * If fontCollection is NULL then the object is not part of any collection
695 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
696 GpFontCollection *fontCollection,
697 GpFontFamily **FontFamily)
700 GpFontFamily* ffamily;
701 struct font_metrics fm;
703 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
705 if (!(name && FontFamily))
706 return InvalidParameter;
708 FIXME("No support for FontCollections yet!\n");
710 stat = find_installed_font(name, &fm);
711 if (stat != Ok) return stat;
713 ffamily = GdipAlloc(sizeof (GpFontFamily));
714 if (!ffamily) return OutOfMemory;
716 lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
717 ffamily->em_height = fm.em_height;
718 ffamily->ascent = fm.ascent;
719 ffamily->descent = fm.descent;
720 ffamily->line_spacing = fm.line_spacing;
721 ffamily->dpi = fm.dpi;
723 *FontFamily = ffamily;
725 TRACE("<-- %p\n", ffamily);
730 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
732 *clone = GdipAlloc(sizeof(GpFontFamily));
733 if (!*clone) return OutOfMemory;
740 /*******************************************************************************
741 * GdipCloneFontFamily [GDIPLUS.@]
743 * Creates a deep copy of a Font Family object
746 * FontFamily [I] Font to clone
747 * clonedFontFamily [O] The resulting cloned font
752 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
756 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
758 TRACE("%p (%s), %p\n", FontFamily,
759 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
761 status = clone_font_family(FontFamily, clonedFontFamily);
762 if (status != Ok) return status;
764 TRACE("<-- %p\n", *clonedFontFamily);
769 /*******************************************************************************
770 * GdipGetFamilyName [GDIPLUS.@]
772 * Returns the family name into name
775 * *family [I] Family to retrieve from
776 * *name [O] WCHARS of the family name
781 * FAILURE: InvalidParameter if family is NULL
784 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
786 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
787 WCHAR *name, LANGID language)
789 static int lang_fixme;
792 return InvalidParameter;
794 TRACE("%p, %p, %d\n", family, name, language);
796 if (language != LANG_NEUTRAL && !lang_fixme++)
797 FIXME("No support for handling of multiple languages!\n");
799 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
805 /*****************************************************************************
806 * GdipDeleteFontFamily [GDIPLUS.@]
808 * Removes the specified FontFamily
811 * *FontFamily [I] The family to delete
815 * FAILURE: InvalidParameter if FontFamily is NULL.
818 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
821 return InvalidParameter;
822 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
824 GdipFree (FontFamily);
829 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
830 INT style, UINT16* CellAscent)
832 if (!(family && CellAscent)) return InvalidParameter;
834 *CellAscent = family->ascent;
835 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
840 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
841 INT style, UINT16* CellDescent)
843 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
845 if (!(family && CellDescent)) return InvalidParameter;
847 *CellDescent = family->descent;
848 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
853 /*******************************************************************************
854 * GdipGetEmHeight [GDIPLUS.@]
856 * Gets the height of the specified family in EmHeights
859 * family [I] Family to retrieve from
860 * style [I] (optional) style
861 * EmHeight [O] return value
865 * FAILURE: InvalidParameter
867 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
869 if (!(family && EmHeight)) return InvalidParameter;
871 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
873 *EmHeight = family->em_height;
874 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
880 /*******************************************************************************
881 * GdipGetLineSpacing [GDIPLUS.@]
883 * Returns the line spacing in design units
886 * family [I] Family to retrieve from
887 * style [I] (Optional) font style
888 * LineSpacing [O] Return value
892 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
894 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
895 INT style, UINT16* LineSpacing)
897 TRACE("%p, %d, %p\n", family, style, LineSpacing);
899 if (!(family && LineSpacing))
900 return InvalidParameter;
902 if (style) FIXME("ignoring style\n");
904 *LineSpacing = family->line_spacing;
905 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
910 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
911 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
913 INT fontstyle = FontStyleRegular;
917 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
918 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
919 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
920 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
922 return (INT)lParam != fontstyle;
925 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
926 INT style, BOOL* IsStyleAvailable)
930 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
932 if (!(family && IsStyleAvailable))
933 return InvalidParameter;
935 *IsStyleAvailable = FALSE;
939 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
940 *IsStyleAvailable = TRUE;
947 /*****************************************************************************
948 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
950 * Obtains a serif family (Courier New on Windows)
953 * **nativeFamily [I] Where the font will be stored
956 * InvalidParameter if nativeFamily is NULL.
959 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
961 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
962 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
965 if (nativeFamily == NULL) return InvalidParameter;
967 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
969 if (stat == FontFamilyNotFound)
970 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
972 if (stat == FontFamilyNotFound)
973 ERR("Missing 'Courier New' font\n");
978 /*****************************************************************************
979 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
981 * Obtains a serif family (Times New Roman on Windows)
984 * **nativeFamily [I] Where the font will be stored
987 * InvalidParameter if nativeFamily is NULL.
990 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
992 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
993 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
996 TRACE("(%p)\n", nativeFamily);
998 if (nativeFamily == NULL) return InvalidParameter;
1000 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1002 if (stat == FontFamilyNotFound)
1003 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1005 if (stat == FontFamilyNotFound)
1006 ERR("Missing 'Times New Roman' font\n");
1011 /*****************************************************************************
1012 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1014 * Obtains a serif family (Microsoft Sans Serif on Windows)
1017 * **nativeFamily [I] Where the font will be stored
1020 * InvalidParameter if nativeFamily is NULL.
1023 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1026 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1027 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1029 TRACE("(%p)\n", nativeFamily);
1031 if (nativeFamily == NULL) return InvalidParameter;
1033 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1035 if (stat == FontFamilyNotFound)
1036 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1037 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1042 /*****************************************************************************
1043 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1045 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1047 TRACE("%p\n", fontCollection);
1049 if (!fontCollection)
1050 return InvalidParameter;
1052 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1053 if (!*fontCollection) return OutOfMemory;
1055 (*fontCollection)->FontFamilies = NULL;
1056 (*fontCollection)->count = 0;
1057 (*fontCollection)->allocated = 0;
1059 TRACE("<-- %p\n", *fontCollection);
1064 /*****************************************************************************
1065 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1067 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1071 TRACE("%p\n", fontCollection);
1073 if (!fontCollection)
1074 return InvalidParameter;
1076 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1077 GdipFree(*fontCollection);
1082 /*****************************************************************************
1083 * GdipPrivateAddFontFile [GDIPLUS.@]
1085 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
1086 GDIPCONST WCHAR* filename)
1088 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
1090 if (!(fontCollection && filename))
1091 return InvalidParameter;
1093 return NotImplemented;
1096 /* Copied from msi/font.c */
1098 typedef struct _tagTT_OFFSET_TABLE {
1099 USHORT uMajorVersion;
1100 USHORT uMinorVersion;
1101 USHORT uNumOfTables;
1102 USHORT uSearchRange;
1103 USHORT uEntrySelector;
1107 typedef struct _tagTT_TABLE_DIRECTORY {
1108 char szTag[4]; /* table name */
1109 ULONG uCheckSum; /* Check sum */
1110 ULONG uOffset; /* Offset from beginning of file */
1111 ULONG uLength; /* length of the table in bytes */
1112 } TT_TABLE_DIRECTORY;
1114 typedef struct _tagTT_NAME_TABLE_HEADER {
1115 USHORT uFSelector; /* format selector. Always 0 */
1116 USHORT uNRCount; /* Name Records count */
1117 USHORT uStorageOffset; /* Offset for strings storage,
1118 * from start of the table */
1119 } TT_NAME_TABLE_HEADER;
1121 #define NAME_ID_FULL_FONT_NAME 4
1122 #define NAME_ID_VERSION 5
1124 typedef struct _tagTT_NAME_RECORD {
1129 USHORT uStringLength;
1130 USHORT uStringOffset; /* from start of storage area */
1133 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1134 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1137 * Code based off of code located here
1138 * http://www.codeproject.com/gdi/fontnamefromfile.asp
1140 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1142 const TT_TABLE_DIRECTORY *tblDir;
1143 TT_OFFSET_TABLE ttOffsetTable;
1144 TT_NAME_TABLE_HEADER ttNTHeader;
1145 TT_NAME_RECORD ttRecord;
1149 if (sizeof(TT_OFFSET_TABLE) > size)
1151 ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1152 ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1153 ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1154 ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1156 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1159 pos = sizeof(ttOffsetTable);
1160 for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1162 tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1163 pos += sizeof(*tblDir);
1164 if (memcmp(tblDir->szTag,"name",4)==0)
1166 ofs = SWAPLONG(tblDir->uOffset);
1170 if (i >= ttOffsetTable.uNumOfTables)
1173 pos = ofs + sizeof(ttNTHeader);
1176 ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1177 ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1178 ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1179 for(i=0; i<ttNTHeader.uNRCount; i++)
1181 ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1182 pos += sizeof(ttRecord);
1186 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1187 if (ttRecord.uNameID == id)
1191 ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1192 ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1193 if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1195 buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1196 len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1204 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1206 /*****************************************************************************
1207 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1209 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1210 GDIPCONST void* memory, INT length)
1212 WCHAR buf[32], *name;
1215 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1217 if (!fontCollection || !memory || !length)
1218 return InvalidParameter;
1220 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1224 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1225 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1226 if (!font || !count)
1227 return InvalidParameter;
1236 lfw.lfCharSet = DEFAULT_CHARSET;
1237 lstrcpyW(lfw.lfFaceName, name);
1238 lfw.lfPitchAndFamily = 0;
1240 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1251 /*****************************************************************************
1252 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1254 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1255 GpFontCollection* fontCollection, INT* numFound)
1257 TRACE("%p, %p\n", fontCollection, numFound);
1259 if (!(fontCollection && numFound))
1260 return InvalidParameter;
1262 *numFound = fontCollection->count;
1266 /*****************************************************************************
1267 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1269 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1270 GpFontCollection* fontCollection, INT numSought,
1271 GpFontFamily* gpfamilies[], INT* numFound)
1276 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1278 if (!(fontCollection && gpfamilies && numFound))
1279 return InvalidParameter;
1281 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1283 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1285 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1293 for (i=0; i<numToFree; i++)
1295 GdipDeleteFontFamily(gpfamilies[i]);
1296 gpfamilies[i] = NULL;
1303 void free_installed_fonts(void)
1305 while (installedFontCollection.count)
1306 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1307 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1308 installedFontCollection.FontFamilies = NULL;
1309 installedFontCollection.allocated = 0;
1312 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1313 DWORD type, LPARAM lParam)
1315 GpFontCollection* fonts = (GpFontCollection*)lParam;
1318 if (type == RASTER_FONTTYPE)
1321 /* skip duplicates */
1322 for (i=0; i<fonts->count; i++)
1323 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1326 if (fonts->allocated == fonts->count)
1328 INT new_alloc_count = fonts->allocated+50;
1329 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1331 if (!new_family_list)
1334 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1335 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1336 fonts->FontFamilies = new_family_list;
1337 fonts->allocated = new_alloc_count;
1340 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1348 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1349 GpFontCollection** fontCollection)
1351 TRACE("(%p)\n",fontCollection);
1353 if (!fontCollection)
1354 return InvalidParameter;
1356 if (installedFontCollection.count == 0)
1363 lfw.lfCharSet = DEFAULT_CHARSET;
1364 lfw.lfFaceName[0] = 0;
1365 lfw.lfPitchAndFamily = 0;
1367 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1369 free_installed_fonts();
1377 *fontCollection = &installedFontCollection;