msvcp90: Initialize state in mb_to_wc.
[wine.git] / dlls / gdiplus / font.c
blobe4f979c48193d407ff4df7528ce5efbcc6529172
1 /*
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
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
32 #include "objbase.h"
34 #include "gdiplus.h"
35 #include "gdiplus_private.h"
37 /* PANOSE is 10 bytes in size, need to pack the structure properly */
38 #include "pshpack2.h"
39 typedef struct
41 USHORT version;
42 SHORT xAvgCharWidth;
43 USHORT usWeightClass;
44 USHORT usWidthClass;
45 SHORT fsType;
46 SHORT ySubscriptXSize;
47 SHORT ySubscriptYSize;
48 SHORT ySubscriptXOffset;
49 SHORT ySubscriptYOffset;
50 SHORT ySuperscriptXSize;
51 SHORT ySuperscriptYSize;
52 SHORT ySuperscriptXOffset;
53 SHORT ySuperscriptYOffset;
54 SHORT yStrikeoutSize;
55 SHORT yStrikeoutPosition;
56 SHORT sFamilyClass;
57 PANOSE panose;
58 ULONG ulUnicodeRange1;
59 ULONG ulUnicodeRange2;
60 ULONG ulUnicodeRange3;
61 ULONG ulUnicodeRange4;
62 CHAR achVendID[4];
63 USHORT fsSelection;
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) */
70 USHORT sTypoAscender;
71 USHORT sTypoDescender;
72 USHORT sTypoLineGap;
73 USHORT usWinAscent;
74 USHORT usWinDescent;
75 /* version 1 (TrueType 1.66) */
76 ULONG ulCodePageRange1;
77 ULONG ulCodePageRange2;
78 /* version 2 (OpenType 1.2) */
79 SHORT sxHeight;
80 SHORT sCapHeight;
81 USHORT usDefaultChar;
82 USHORT usBreakChar;
83 USHORT usMaxContext;
84 } TT_OS2_V2;
86 typedef struct
88 ULONG Version;
89 SHORT Ascender;
90 SHORT Descender;
91 SHORT LineGap;
92 USHORT advanceWidthMax;
93 SHORT minLeftSideBearing;
94 SHORT minRightSideBearing;
95 SHORT xMaxExtent;
96 SHORT caretSlopeRise;
97 SHORT caretSlopeRun;
98 SHORT caretOffset;
99 SHORT reserved[4];
100 SHORT metricDataFormat;
101 USHORT numberOfHMetrics;
102 } TT_HHEA;
103 #include "poppack.h"
105 #ifdef WORDS_BIGENDIAN
106 #define GET_BE_WORD(x) (x)
107 #define GET_BE_DWORD(x) (x)
108 #else
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)));
111 #endif
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
128 * PARAMS
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
135 * RETURNS
136 * SUCCESS: Ok
137 * FAILURE: InvalidParameter if fontfamily or font is NULL.
138 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
140 * NOTES
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)
147 HFONT hfont;
148 OUTLINETEXTMETRICW otm;
149 LOGFONTW lfw;
150 HDC hdc;
151 GpStatus stat;
152 int ret;
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);
176 DeleteDC(hdc);
177 DeleteObject(hfont);
179 if (!ret) return NotTrueTypeFont;
181 *font = GdipAlloc(sizeof(GpFont));
182 if (!*font) return OutOfMemory;
184 (*font)->unit = unit;
185 (*font)->emSize = emSize;
186 (*font)->otm = otm;
188 stat = clone_font_family(fontFamily, &(*font)->family);
189 if (stat != Ok)
191 GdipFree(*font);
192 return stat;
195 TRACE("<-- %p\n", *font);
197 return Ok;
200 /*******************************************************************************
201 * GdipCreateFontFromLogfontW [GDIPLUS.@]
203 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
204 GDIPCONST LOGFONTW *logfont, GpFont **font)
206 HFONT hfont, oldfont;
207 OUTLINETEXTMETRICW otm;
208 WCHAR facename[LF_FACESIZE];
209 GpStatus stat;
210 int ret;
212 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
214 if (!hdc || !logfont || !font)
215 return InvalidParameter;
217 hfont = CreateFontIndirectW(logfont);
218 oldfont = SelectObject(hdc, hfont);
219 otm.otmSize = sizeof(otm);
220 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
221 GetTextFaceW(hdc, LF_FACESIZE, facename);
222 SelectObject(hdc, oldfont);
223 DeleteObject(hfont);
225 if (!ret) return NotTrueTypeFont;
227 *font = GdipAlloc(sizeof(GpFont));
228 if (!*font) return OutOfMemory;
230 (*font)->unit = UnitWorld;
231 (*font)->emSize = otm.otmTextMetrics.tmAscent;
232 (*font)->otm = otm;
234 stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
235 if (stat != Ok)
237 GdipFree(*font);
238 return NotTrueTypeFont;
241 TRACE("<-- %p\n", *font);
243 return Ok;
246 /*******************************************************************************
247 * GdipCreateFontFromLogfontA [GDIPLUS.@]
249 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
250 GDIPCONST LOGFONTA *lfa, GpFont **font)
252 LOGFONTW lfw;
254 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
256 if(!lfa || !font)
257 return InvalidParameter;
259 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
261 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
262 return GenericError;
264 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
267 /*******************************************************************************
268 * GdipDeleteFont [GDIPLUS.@]
270 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
272 TRACE("(%p)\n", font);
274 if(!font)
275 return InvalidParameter;
277 GdipDeleteFontFamily(font->family);
278 GdipFree(font);
280 return Ok;
283 /*******************************************************************************
284 * GdipCreateFontFromDC [GDIPLUS.@]
286 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
288 HFONT hfont;
289 LOGFONTW lfw;
291 TRACE("(%p, %p)\n", hdc, font);
293 if(!font)
294 return InvalidParameter;
296 hfont = GetCurrentObject(hdc, OBJ_FONT);
297 if(!hfont)
298 return GenericError;
300 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
301 return GenericError;
303 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
306 /*******************************************************************************
307 * GdipGetFamily [GDIPLUS.@]
309 * Returns the FontFamily for the specified Font
311 * PARAMS
312 * font [I] Font to request from
313 * family [O] Resulting FontFamily object
315 * RETURNS
316 * SUCCESS: Ok
317 * FAILURE: An element of GpStatus
319 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
321 TRACE("%p %p\n", font, family);
323 if (!(font && family))
324 return InvalidParameter;
326 return GdipCloneFontFamily(font->family, family);
329 static REAL get_font_size(const GpFont *font)
331 return font->emSize;
334 /******************************************************************************
335 * GdipGetFontSize [GDIPLUS.@]
337 * Returns the size of the font in Units
339 * PARAMS
340 * *font [I] The font to retrieve size from
341 * *size [O] Pointer to hold retrieved value
343 * RETURNS
344 * SUCCESS: Ok
345 * FAILURE: InvalidParameter (font or size was NULL)
347 * NOTES
348 * Size returned is actually emSize -- not internal size used for drawing.
350 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
352 TRACE("(%p, %p)\n", font, size);
354 if (!(font && size)) return InvalidParameter;
356 *size = get_font_size(font);
357 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
359 return Ok;
362 static INT get_font_style(const GpFont *font)
364 INT style;
366 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
367 style = FontStyleBold;
368 else
369 style = FontStyleRegular;
370 if (font->otm.otmTextMetrics.tmItalic)
371 style |= FontStyleItalic;
372 if (font->otm.otmTextMetrics.tmUnderlined)
373 style |= FontStyleUnderline;
374 if (font->otm.otmTextMetrics.tmStruckOut)
375 style |= FontStyleStrikeout;
377 return style;
380 /*******************************************************************************
381 * GdipGetFontStyle [GDIPLUS.@]
383 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
385 * PARAMS
386 * font [I] font to request from
387 * style [O] resulting pointer to a FontStyle enumeration
389 * RETURNS
390 * SUCCESS: Ok
391 * FAILURE: InvalidParameter
393 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
395 TRACE("%p %p\n", font, style);
397 if (!(font && style))
398 return InvalidParameter;
400 *style = get_font_style(font);
401 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
403 return Ok;
406 /*******************************************************************************
407 * GdipGetFontUnit [GDIPLUS.@]
409 * PARAMS
410 * font [I] Font to retrieve from
411 * unit [O] Return value
413 * RETURNS
414 * FAILURE: font or unit was NULL
415 * OK: otherwise
417 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
419 TRACE("(%p, %p)\n", font, unit);
421 if (!(font && unit)) return InvalidParameter;
423 *unit = font->unit;
424 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
426 return Ok;
429 /*******************************************************************************
430 * GdipGetLogFontA [GDIPLUS.@]
432 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
433 LOGFONTA *lfa)
435 GpStatus status;
436 LOGFONTW lfw;
438 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
440 status = GdipGetLogFontW(font, graphics, &lfw);
441 if(status != Ok)
442 return status;
444 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
446 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
447 return GenericError;
449 return Ok;
452 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
454 REAL height;
456 if (font->unit == UnitPixel)
458 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
459 if (graphics->unit != UnitDisplay)
460 height *= graphics->scale;
462 else
464 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
465 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
466 else
467 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
470 lf->lfHeight = -(height + 0.5);
471 lf->lfWidth = 0;
472 lf->lfEscapement = 0;
473 lf->lfOrientation = 0;
474 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
475 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
476 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
477 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
478 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
479 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
480 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
481 lf->lfQuality = DEFAULT_QUALITY;
482 lf->lfPitchAndFamily = 0;
483 strcpyW(lf->lfFaceName, font->family->FamilyName);
486 /*******************************************************************************
487 * GdipGetLogFontW [GDIPLUS.@]
489 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
490 LOGFONTW *lfw)
492 TRACE("(%p, %p, %p)\n", font, graphics, lfw);
494 if(!font || !graphics || !lfw)
495 return InvalidParameter;
497 get_log_fontW(font, graphics, lfw);
498 TRACE("=> %s,%d\n", debugstr_w(lfw->lfFaceName), lfw->lfHeight);
500 return Ok;
503 /*******************************************************************************
504 * GdipCloneFont [GDIPLUS.@]
506 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
508 GpStatus stat;
510 TRACE("(%p, %p)\n", font, cloneFont);
512 if(!font || !cloneFont)
513 return InvalidParameter;
515 *cloneFont = GdipAlloc(sizeof(GpFont));
516 if(!*cloneFont) return OutOfMemory;
518 **cloneFont = *font;
519 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
520 if (stat != Ok) GdipFree(*cloneFont);
522 return stat;
525 /*******************************************************************************
526 * GdipGetFontHeight [GDIPLUS.@]
527 * PARAMS
528 * font [I] Font to retrieve height from
529 * graphics [I] The current graphics context
530 * height [O] Resulting height
531 * RETURNS
532 * SUCCESS: Ok
533 * FAILURE: Another element of GpStatus
535 * NOTES
536 * Forwards to GdipGetFontHeightGivenDPI
538 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
539 GDIPCONST GpGraphics *graphics, REAL *height)
541 REAL dpi;
542 GpStatus stat;
543 REAL font_height;
545 TRACE("%p %p %p\n", font, graphics, height);
547 stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
548 if (stat != Ok) return stat;
550 if (!graphics)
552 *height = font_height;
553 TRACE("%s,%d => %f\n",
554 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
555 return Ok;
558 stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
559 if (stat != Ok) return stat;
561 *height = pixels_to_units(font_height, graphics->unit, dpi);
563 TRACE("%s,%d(unit %d) => %f\n",
564 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
565 return Ok;
568 /*******************************************************************************
569 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
570 * PARAMS
571 * font [I] Font to retrieve DPI from
572 * dpi [I] DPI to assume
573 * height [O] Return value
575 * RETURNS
576 * SUCCESS: Ok
577 * FAILURE: InvalidParameter if font or height is NULL
579 * NOTES
580 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
581 * (for anything other than unit Pixel)
583 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
585 GpStatus stat;
586 INT style;
587 UINT16 line_spacing, em_height;
588 REAL font_size;
590 if (!font || !height) return InvalidParameter;
592 TRACE("%p (%s), %f, %p\n", font,
593 debugstr_w(font->family->FamilyName), dpi, height);
595 font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
596 style = get_font_style(font);
597 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
598 if (stat != Ok) return stat;
599 stat = GdipGetEmHeight(font->family, style, &em_height);
600 if (stat != Ok) return stat;
602 *height = (REAL)line_spacing * font_size / (REAL)em_height;
604 TRACE("%s,%d => %f\n",
605 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
607 return Ok;
610 /***********************************************************************
611 * Borrowed from GDI32:
613 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
614 * We have to use other types because of the FONTENUMPROCW definition.
616 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
617 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
619 if (type & RASTER_FONTTYPE)
620 return 1;
622 *(LOGFONTW *)lParam = *elf;
624 return 0;
627 struct font_metrics
629 WCHAR facename[LF_FACESIZE];
630 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
631 int dpi;
634 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
636 OUTLINETEXTMETRICW otm;
637 TT_OS2_V2 tt_os2;
638 TT_HHEA tt_hori;
639 LONG size;
640 UINT16 line_gap;
642 otm.otmSize = sizeof(otm);
643 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
645 GetTextFaceW(hdc, LF_FACESIZE, fm->facename);
647 fm->em_height = otm.otmEMSquare;
648 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
650 memset(&tt_hori, 0, sizeof(tt_hori));
651 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
653 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
654 fm->descent = -GET_BE_WORD(tt_hori.Descender);
655 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
656 line_gap = GET_BE_WORD(tt_hori.LineGap);
657 fm->line_spacing = fm->ascent + fm->descent + line_gap;
658 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
659 if (fm->ascent + fm->descent != 0) return TRUE;
662 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
663 if (size == GDI_ERROR) return FALSE;
665 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
667 memset(&tt_os2, 0, sizeof(tt_os2));
668 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
670 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
671 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
672 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
673 if (fm->ascent + fm->descent == 0)
675 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
676 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
677 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
679 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
680 fm->line_spacing = fm->ascent + fm->descent + line_gap;
681 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
682 return TRUE;
685 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
687 LOGFONTW lf;
688 HDC hdc = CreateCompatibleDC(0);
689 GpStatus ret = FontFamilyNotFound;
691 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
693 HFONT hfont, old_font;
695 hfont = CreateFontIndirectW(&lf);
696 old_font = SelectObject(hdc, hfont);
697 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
698 SelectObject(hdc, old_font);
699 DeleteObject(hfont);
702 DeleteDC(hdc);
703 return ret;
706 /*******************************************************************************
707 * GdipCreateFontFamilyFromName [GDIPLUS.@]
709 * Creates a font family object based on a supplied name
711 * PARAMS
712 * name [I] Name of the font
713 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
714 * FontFamily [O] Pointer to the resulting FontFamily object
716 * RETURNS
717 * SUCCESS: Ok
718 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
719 * FAILURE: Invalid parameter if FontFamily or name is NULL
721 * NOTES
722 * If fontCollection is NULL then the object is not part of any collection
726 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
727 GpFontCollection *fontCollection,
728 GpFontFamily **FontFamily)
730 GpStatus stat;
731 GpFontFamily* ffamily;
732 struct font_metrics fm;
734 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
736 if (!(name && FontFamily))
737 return InvalidParameter;
738 if (fontCollection)
739 FIXME("No support for FontCollections yet!\n");
741 stat = find_installed_font(name, &fm);
742 if (stat != Ok) return stat;
744 ffamily = GdipAlloc(sizeof (GpFontFamily));
745 if (!ffamily) return OutOfMemory;
747 lstrcpyW(ffamily->FamilyName, fm.facename);
748 ffamily->em_height = fm.em_height;
749 ffamily->ascent = fm.ascent;
750 ffamily->descent = fm.descent;
751 ffamily->line_spacing = fm.line_spacing;
752 ffamily->dpi = fm.dpi;
754 *FontFamily = ffamily;
756 TRACE("<-- %p\n", ffamily);
758 return Ok;
761 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
763 *clone = GdipAlloc(sizeof(GpFontFamily));
764 if (!*clone) return OutOfMemory;
766 **clone = *family;
768 return Ok;
771 /*******************************************************************************
772 * GdipCloneFontFamily [GDIPLUS.@]
774 * Creates a deep copy of a Font Family object
776 * PARAMS
777 * FontFamily [I] Font to clone
778 * clonedFontFamily [O] The resulting cloned font
780 * RETURNS
781 * SUCCESS: Ok
783 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
785 GpStatus status;
787 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
789 TRACE("%p (%s), %p\n", FontFamily,
790 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
792 status = clone_font_family(FontFamily, clonedFontFamily);
793 if (status != Ok) return status;
795 TRACE("<-- %p\n", *clonedFontFamily);
797 return Ok;
800 /*******************************************************************************
801 * GdipGetFamilyName [GDIPLUS.@]
803 * Returns the family name into name
805 * PARAMS
806 * *family [I] Family to retrieve from
807 * *name [O] WCHARS of the family name
808 * LANGID [I] charset
810 * RETURNS
811 * SUCCESS: Ok
812 * FAILURE: InvalidParameter if family is NULL
814 * NOTES
815 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
817 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
818 WCHAR *name, LANGID language)
820 static int lang_fixme;
822 if (family == NULL)
823 return InvalidParameter;
825 TRACE("%p, %p, %d\n", family, name, language);
827 if (language != LANG_NEUTRAL && !lang_fixme++)
828 FIXME("No support for handling of multiple languages!\n");
830 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
832 return Ok;
836 /*****************************************************************************
837 * GdipDeleteFontFamily [GDIPLUS.@]
839 * Removes the specified FontFamily
841 * PARAMS
842 * *FontFamily [I] The family to delete
844 * RETURNS
845 * SUCCESS: Ok
846 * FAILURE: InvalidParameter if FontFamily is NULL.
849 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
851 if (!FontFamily)
852 return InvalidParameter;
853 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
855 GdipFree (FontFamily);
857 return Ok;
860 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
861 INT style, UINT16* CellAscent)
863 if (!(family && CellAscent)) return InvalidParameter;
865 *CellAscent = family->ascent;
866 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
868 return Ok;
871 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
872 INT style, UINT16* CellDescent)
874 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
876 if (!(family && CellDescent)) return InvalidParameter;
878 *CellDescent = family->descent;
879 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
881 return Ok;
884 /*******************************************************************************
885 * GdipGetEmHeight [GDIPLUS.@]
887 * Gets the height of the specified family in EmHeights
889 * PARAMS
890 * family [I] Family to retrieve from
891 * style [I] (optional) style
892 * EmHeight [O] return value
894 * RETURNS
895 * SUCCESS: Ok
896 * FAILURE: InvalidParameter
898 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
900 if (!(family && EmHeight)) return InvalidParameter;
902 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
904 *EmHeight = family->em_height;
905 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
907 return Ok;
911 /*******************************************************************************
912 * GdipGetLineSpacing [GDIPLUS.@]
914 * Returns the line spacing in design units
916 * PARAMS
917 * family [I] Family to retrieve from
918 * style [I] (Optional) font style
919 * LineSpacing [O] Return value
921 * RETURNS
922 * SUCCESS: Ok
923 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
925 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
926 INT style, UINT16* LineSpacing)
928 TRACE("%p, %d, %p\n", family, style, LineSpacing);
930 if (!(family && LineSpacing))
931 return InvalidParameter;
933 if (style) FIXME("ignoring style\n");
935 *LineSpacing = family->line_spacing;
936 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
938 return Ok;
941 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
942 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
944 INT fontstyle = FontStyleRegular;
946 if (!ntm) return 1;
948 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
949 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
950 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
951 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
953 return (INT)lParam != fontstyle;
956 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
957 INT style, BOOL* IsStyleAvailable)
959 HDC hdc;
961 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
963 if (!(family && IsStyleAvailable))
964 return InvalidParameter;
966 *IsStyleAvailable = FALSE;
968 hdc = GetDC(0);
970 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
971 *IsStyleAvailable = TRUE;
973 ReleaseDC(0, hdc);
975 return Ok;
978 /*****************************************************************************
979 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
981 * Obtains a serif family (Courier New on Windows)
983 * PARAMS
984 * **nativeFamily [I] Where the font will be stored
986 * RETURNS
987 * InvalidParameter if nativeFamily is NULL.
988 * Ok otherwise.
990 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
992 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
993 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
994 GpStatus stat;
996 if (nativeFamily == NULL) return InvalidParameter;
998 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
1000 if (stat == FontFamilyNotFound)
1001 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1003 if (stat == FontFamilyNotFound)
1004 ERR("Missing 'Courier New' font\n");
1006 return stat;
1009 /*****************************************************************************
1010 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1012 * Obtains a serif family (Times New Roman on Windows)
1014 * PARAMS
1015 * **nativeFamily [I] Where the font will be stored
1017 * RETURNS
1018 * InvalidParameter if nativeFamily is NULL.
1019 * Ok otherwise.
1021 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1023 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1024 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1025 GpStatus stat;
1027 TRACE("(%p)\n", nativeFamily);
1029 if (nativeFamily == NULL) return InvalidParameter;
1031 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1033 if (stat == FontFamilyNotFound)
1034 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1036 if (stat == FontFamilyNotFound)
1037 ERR("Missing 'Times New Roman' font\n");
1039 return stat;
1042 /*****************************************************************************
1043 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1045 * Obtains a serif family (Microsoft Sans Serif on Windows)
1047 * PARAMS
1048 * **nativeFamily [I] Where the font will be stored
1050 * RETURNS
1051 * InvalidParameter if nativeFamily is NULL.
1052 * Ok otherwise.
1054 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1056 GpStatus stat;
1057 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1058 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1060 TRACE("(%p)\n", nativeFamily);
1062 if (nativeFamily == NULL) return InvalidParameter;
1064 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1066 if (stat == FontFamilyNotFound)
1067 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1068 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1070 return stat;
1073 /*****************************************************************************
1074 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1076 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1078 TRACE("%p\n", fontCollection);
1080 if (!fontCollection)
1081 return InvalidParameter;
1083 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1084 if (!*fontCollection) return OutOfMemory;
1086 (*fontCollection)->FontFamilies = NULL;
1087 (*fontCollection)->count = 0;
1088 (*fontCollection)->allocated = 0;
1090 TRACE("<-- %p\n", *fontCollection);
1092 return Ok;
1095 /*****************************************************************************
1096 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1098 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1100 INT i;
1102 TRACE("%p\n", fontCollection);
1104 if (!fontCollection)
1105 return InvalidParameter;
1107 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1108 GdipFree(*fontCollection);
1110 return Ok;
1113 /*****************************************************************************
1114 * GdipPrivateAddFontFile [GDIPLUS.@]
1116 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
1117 GDIPCONST WCHAR* filename)
1119 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
1121 if (!(fontCollection && filename))
1122 return InvalidParameter;
1124 return NotImplemented;
1127 /* Copied from msi/font.c */
1129 typedef struct _tagTT_OFFSET_TABLE {
1130 USHORT uMajorVersion;
1131 USHORT uMinorVersion;
1132 USHORT uNumOfTables;
1133 USHORT uSearchRange;
1134 USHORT uEntrySelector;
1135 USHORT uRangeShift;
1136 } TT_OFFSET_TABLE;
1138 typedef struct _tagTT_TABLE_DIRECTORY {
1139 char szTag[4]; /* table name */
1140 ULONG uCheckSum; /* Check sum */
1141 ULONG uOffset; /* Offset from beginning of file */
1142 ULONG uLength; /* length of the table in bytes */
1143 } TT_TABLE_DIRECTORY;
1145 typedef struct _tagTT_NAME_TABLE_HEADER {
1146 USHORT uFSelector; /* format selector. Always 0 */
1147 USHORT uNRCount; /* Name Records count */
1148 USHORT uStorageOffset; /* Offset for strings storage,
1149 * from start of the table */
1150 } TT_NAME_TABLE_HEADER;
1152 #define NAME_ID_FULL_FONT_NAME 4
1153 #define NAME_ID_VERSION 5
1155 typedef struct _tagTT_NAME_RECORD {
1156 USHORT uPlatformID;
1157 USHORT uEncodingID;
1158 USHORT uLanguageID;
1159 USHORT uNameID;
1160 USHORT uStringLength;
1161 USHORT uStringOffset; /* from start of storage area */
1162 } TT_NAME_RECORD;
1164 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
1165 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
1168 * Code based off of code located here
1169 * http://www.codeproject.com/gdi/fontnamefromfile.asp
1171 static WCHAR *load_ttf_name_id( const char *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1173 const TT_TABLE_DIRECTORY *tblDir;
1174 TT_OFFSET_TABLE ttOffsetTable;
1175 TT_NAME_TABLE_HEADER ttNTHeader;
1176 TT_NAME_RECORD ttRecord;
1177 DWORD ofs, pos;
1178 int i;
1180 if (sizeof(TT_OFFSET_TABLE) > size)
1181 return NULL;
1182 ttOffsetTable = *(TT_OFFSET_TABLE*)mem;
1183 ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
1184 ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
1185 ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
1187 if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
1188 return NULL;
1190 pos = sizeof(ttOffsetTable);
1191 for (i = 0; i < ttOffsetTable.uNumOfTables; i++)
1193 tblDir = (const TT_TABLE_DIRECTORY*)&mem[pos];
1194 pos += sizeof(*tblDir);
1195 if (memcmp(tblDir->szTag,"name",4)==0)
1197 ofs = SWAPLONG(tblDir->uOffset);
1198 break;
1201 if (i >= ttOffsetTable.uNumOfTables)
1202 return NULL;
1204 pos = ofs + sizeof(ttNTHeader);
1205 if (pos > size)
1206 return NULL;
1207 ttNTHeader = *(TT_NAME_TABLE_HEADER*)&mem[ofs];
1208 ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
1209 ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
1210 for(i=0; i<ttNTHeader.uNRCount; i++)
1212 ttRecord = *(TT_NAME_RECORD*)&mem[pos];
1213 pos += sizeof(ttRecord);
1214 if (pos > size)
1215 return NULL;
1217 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
1218 if (ttRecord.uNameID == id)
1220 const char *buf;
1222 ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
1223 ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
1224 if (ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset + ttRecord.uStringLength > size)
1225 return NULL;
1226 buf = mem + ofs + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
1227 len = MultiByteToWideChar(CP_ACP, 0, buf, ttRecord.uStringLength, ret, len-1);
1228 ret[len] = 0;
1229 return ret;
1232 return NULL;
1235 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1237 /*****************************************************************************
1238 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1240 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1241 GDIPCONST void* memory, INT length)
1243 WCHAR buf[32], *name;
1244 DWORD count = 0;
1245 HANDLE font;
1246 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1248 if (!fontCollection || !memory || !length)
1249 return InvalidParameter;
1251 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1252 if (!name)
1253 return OutOfMemory;
1255 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1256 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1257 if (!font || !count)
1258 return InvalidParameter;
1260 if (count)
1262 HDC hdc;
1263 LOGFONTW lfw;
1265 hdc = GetDC(0);
1267 lfw.lfCharSet = DEFAULT_CHARSET;
1268 lstrcpyW(lfw.lfFaceName, name);
1269 lfw.lfPitchAndFamily = 0;
1271 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1273 ReleaseDC(0, hdc);
1274 return OutOfMemory;
1277 ReleaseDC(0, hdc);
1279 return Ok;
1282 /*****************************************************************************
1283 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1285 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1286 GpFontCollection* fontCollection, INT* numFound)
1288 TRACE("%p, %p\n", fontCollection, numFound);
1290 if (!(fontCollection && numFound))
1291 return InvalidParameter;
1293 *numFound = fontCollection->count;
1294 return Ok;
1297 /*****************************************************************************
1298 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1300 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1301 GpFontCollection* fontCollection, INT numSought,
1302 GpFontFamily* gpfamilies[], INT* numFound)
1304 INT i;
1305 GpStatus stat=Ok;
1307 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1309 if (!(fontCollection && gpfamilies && numFound))
1310 return InvalidParameter;
1312 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1314 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1316 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1319 if (stat == Ok)
1320 *numFound = i;
1321 else
1323 int numToFree=i;
1324 for (i=0; i<numToFree; i++)
1326 GdipDeleteFontFamily(gpfamilies[i]);
1327 gpfamilies[i] = NULL;
1331 return stat;
1334 void free_installed_fonts(void)
1336 while (installedFontCollection.count)
1337 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1338 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1339 installedFontCollection.FontFamilies = NULL;
1340 installedFontCollection.allocated = 0;
1343 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1344 DWORD type, LPARAM lParam)
1346 GpFontCollection* fonts = (GpFontCollection*)lParam;
1347 int i;
1349 if (type == RASTER_FONTTYPE)
1350 return 1;
1352 /* skip duplicates */
1353 for (i=0; i<fonts->count; i++)
1354 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1355 return 1;
1357 if (fonts->allocated == fonts->count)
1359 INT new_alloc_count = fonts->allocated+50;
1360 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1362 if (!new_family_list)
1363 return 0;
1365 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1366 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1367 fonts->FontFamilies = new_family_list;
1368 fonts->allocated = new_alloc_count;
1371 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1372 fonts->count++;
1373 else
1374 return 0;
1376 return 1;
1379 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1380 GpFontCollection** fontCollection)
1382 TRACE("(%p)\n",fontCollection);
1384 if (!fontCollection)
1385 return InvalidParameter;
1387 if (installedFontCollection.count == 0)
1389 HDC hdc;
1390 LOGFONTW lfw;
1392 hdc = GetDC(0);
1394 lfw.lfCharSet = DEFAULT_CHARSET;
1395 lfw.lfFaceName[0] = 0;
1396 lfw.lfPitchAndFamily = 0;
1398 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1400 free_installed_fonts();
1401 ReleaseDC(0, hdc);
1402 return OutOfMemory;
1405 ReleaseDC(0, hdc);
1408 *fontCollection = &installedFontCollection;
1410 return Ok;