2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
34 #include "gdiplus_private.h"
36 static const REAL mm_per_inch = 25.4;
37 static const REAL inch_per_point = 1.0/72.0;
39 static inline REAL get_dpi (void)
44 GdipCreateFromHDC (hdc, &graphics);
45 GdipGetDpiX(graphics, &dpi);
46 GdipDeleteGraphics(graphics);
52 static inline REAL point_to_pixel (REAL point)
54 return point * get_dpi() * inch_per_point;
57 static inline REAL inch_to_pixel (REAL inch)
59 return inch * get_dpi();
62 static inline REAL document_to_pixel (REAL doc)
64 return doc * (get_dpi() / 300.0); /* Per MSDN */
67 static inline REAL mm_to_pixel (REAL mm)
69 return mm * (get_dpi() / mm_per_inch);
72 /*******************************************************************************
73 * GdipCreateFont [GDIPLUS.@]
75 * Create a new font based off of a FontFamily
78 * *fontFamily [I] Family to base the font off of
79 * emSize [I] Size of the font
80 * style [I] Bitwise OR of FontStyle enumeration
81 * unit [I] Unit emSize is measured in
82 * **font [I] the resulting Font object
86 * FAILURE: InvalidParameter if fontfamily or font is NULL.
87 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
90 * UnitDisplay is unsupported.
91 * emSize is stored separately from lfHeight, to hold the fraction.
93 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
94 REAL emSize, INT style, Unit unit, GpFont **font)
96 WCHAR facename[LF_FACESIZE];
98 const NEWTEXTMETRICW* tmw;
101 if (!fontFamily || !font)
102 return InvalidParameter;
104 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
105 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
107 stat = GdipGetFamilyName (fontFamily, facename, 0);
108 if (stat != Ok) return stat;
109 *font = GdipAlloc(sizeof(GpFont));
111 tmw = &fontFamily->tmw;
112 lfw = &((*font)->lfw);
113 ZeroMemory(&(*lfw), sizeof(*lfw));
115 lfw->lfWeight = tmw->tmWeight;
116 lfw->lfItalic = tmw->tmItalic;
117 lfw->lfUnderline = tmw->tmUnderlined;
118 lfw->lfStrikeOut = tmw->tmStruckOut;
119 lfw->lfCharSet = tmw->tmCharSet;
120 lfw->lfPitchAndFamily = tmw->tmPitchAndFamily;
121 lstrcpynW(lfw->lfFaceName, facename, LF_FACESIZE);
126 /* FIXME: Figure out when World != Pixel */
127 lfw->lfHeight = emSize; break;
129 FIXME("Unknown behavior for UnitDisplay! Please report!\n");
130 /* FIXME: Figure out how this works...
131 * MSDN says that if "DISPLAY" is a monitor, then pixel should be
132 * used. That's not what I got. Tests on Windows revealed no output,
133 * and the tests in tests/font crash windows */
134 lfw->lfHeight = 0; break;
136 lfw->lfHeight = emSize; break;
138 lfw->lfHeight = point_to_pixel(emSize); break;
140 lfw->lfHeight = inch_to_pixel(emSize); break;
142 lfw->lfHeight = document_to_pixel(emSize); break;
144 lfw->lfHeight = mm_to_pixel(emSize); break;
149 lfw->lfWeight = style & FontStyleBold ? 700 : 400;
150 lfw->lfItalic = style & FontStyleItalic;
151 lfw->lfUnderline = style & FontStyleUnderline;
152 lfw->lfStrikeOut = style & FontStyleStrikeout;
154 (*font)->unit = unit;
155 (*font)->emSize = emSize;
160 /*******************************************************************************
161 * GdipCreateFontFromLogfontW [GDIPLUS.@]
163 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
164 GDIPCONST LOGFONTW *logfont, GpFont **font)
166 HFONT hfont, oldfont;
169 if(!logfont || !font)
170 return InvalidParameter;
172 *font = GdipAlloc(sizeof(GpFont));
173 if(!*font) return OutOfMemory;
175 memcpy((*font)->lfw.lfFaceName, logfont->lfFaceName, LF_FACESIZE *
177 (*font)->lfw.lfHeight = logfont->lfHeight;
178 (*font)->lfw.lfItalic = logfont->lfItalic;
179 (*font)->lfw.lfUnderline = logfont->lfUnderline;
180 (*font)->lfw.lfStrikeOut = logfont->lfStrikeOut;
182 (*font)->emSize = logfont->lfHeight;
183 (*font)->unit = UnitPixel;
185 hfont = CreateFontIndirectW(&(*font)->lfw);
186 oldfont = SelectObject(hdc, hfont);
187 GetTextMetricsW(hdc, &textmet);
189 (*font)->lfw.lfHeight = -textmet.tmHeight;
190 (*font)->lfw.lfWeight = textmet.tmWeight;
192 SelectObject(hdc, oldfont);
198 /*******************************************************************************
199 * GdipCreateFontFromLogfontA [GDIPLUS.@]
201 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
202 GDIPCONST LOGFONTA *lfa, GpFont **font)
207 return InvalidParameter;
209 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
211 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
214 GdipCreateFontFromLogfontW(hdc, &lfw, font);
219 /*******************************************************************************
220 * GdipDeleteFont [GDIPLUS.@]
222 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
225 return InvalidParameter;
232 /*******************************************************************************
233 * GdipCreateFontFromDC [GDIPLUS.@]
235 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
241 return InvalidParameter;
243 hfont = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
247 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
250 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
253 /*******************************************************************************
254 * GdipGetFamily [GDIPLUS.@]
256 * Returns the FontFamily for the specified Font
259 * font [I] Font to request from
260 * family [O] Resulting FontFamily object
264 * FAILURE: An element of GpStatus
266 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
268 FIXME("stub: %p %p\n", font, family);
270 return NotImplemented;
273 /******************************************************************************
274 * GdipGetFontSize [GDIPLUS.@]
276 * Returns the size of the font in Units
279 * *font [I] The font to retrieve size from
280 * *size [O] Pointer to hold retrieved value
284 * FAILURE: InvalidParamter (font or size was NULL)
287 * Size returned is actually emSize -- not internal size used for drawing.
289 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
291 if (!(font && size)) return InvalidParameter;
293 *size = font->emSize;
298 /*******************************************************************************
299 * GdipGetFontUnit [GDIPLUS.@]
302 * font [I] Font to retrieve from
303 * unit [O] Return value
306 * FAILURE: font or unit was NULL
309 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
311 if (!(font && unit)) return InvalidParameter;
318 /*******************************************************************************
319 * GdipGetLogFontW [GDIPLUS.@]
321 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
324 /* FIXME: use graphics */
325 if(!font || !graphics || !lfw)
326 return InvalidParameter;
333 /*******************************************************************************
334 * GdipCloneFont [GDIPLUS.@]
336 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
338 if(!font || !cloneFont)
339 return InvalidParameter;
341 *cloneFont = GdipAlloc(sizeof(GpFont));
342 if(!*cloneFont) return OutOfMemory;
349 /*******************************************************************************
350 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
352 * font [I] Font to retrieve DPI from
353 * dpi [I] DPI to assume
354 * height [O] Return value
358 * FAILURE: InvalidParameter if font or height is NULL
361 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
362 * (for anything other than unit Pixel)
364 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
366 if (!(font && height)) return InvalidParameter;
368 FIXME("%p (%s), %f, %p\n", font,
369 debugstr_w(font->lfw.lfFaceName), dpi, height);
371 return NotImplemented;
374 /***********************************************************************
375 * Borrowed from GDI32:
377 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
378 * We have to use other types because of the FONTENUMPROCW definition.
380 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
381 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
388 *(NEWTEXTMETRICW*)lParam = *(const NEWTEXTMETRICW*)ntm;
393 static BOOL find_installed_font(const WCHAR *name, NEWTEXTMETRICW *ntm)
398 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)ntm))
405 /*******************************************************************************
406 * GdipCreateFontFamilyFromName [GDIPLUS.@]
408 * Creates a font family object based on a supplied name
411 * name [I] Name of the font
412 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
413 * FontFamily [O] Pointer to the resulting FontFamily object
417 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
418 * FAILURE: Invalid parameter if FontFamily or name is NULL
421 * If fontCollection is NULL then the object is not part of any collection
425 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
426 GpFontCollection *fontCollection,
427 GpFontFamily **FontFamily)
429 GpFontFamily* ffamily;
432 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
434 if (!(name && FontFamily))
435 return InvalidParameter;
437 FIXME("No support for FontCollections yet!\n");
439 if (!find_installed_font(name, &ntm))
440 return FontFamilyNotFound;
442 ffamily = GdipAlloc(sizeof (GpFontFamily));
443 if (!ffamily) return OutOfMemory;
446 lstrcpynW(ffamily->FamilyName, name, LF_FACESIZE);
448 *FontFamily = ffamily;
453 /*******************************************************************************
454 * GdipCloneFontFamily [GDIPLUS.@]
456 * Creates a deep copy of a Font Family object
459 * FontFamily [I] Font to clone
460 * clonedFontFamily [O] The resulting cloned font
465 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
467 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
469 TRACE("stub: %p (%s), %p\n", FontFamily,
470 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
472 *clonedFontFamily = GdipAlloc(sizeof(GpFontFamily));
473 if (!*clonedFontFamily) return OutOfMemory;
475 (*clonedFontFamily)->tmw = FontFamily->tmw;
476 lstrcpyW((*clonedFontFamily)->FamilyName, FontFamily->FamilyName);
481 /*******************************************************************************
482 * GdipGetFamilyName [GDIPLUS.@]
484 * Returns the family name into name
487 * *family [I] Family to retrieve from
488 * *name [O] WCHARS of the family name
493 * FAILURE: InvalidParameter if family is NULL
496 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
498 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
499 WCHAR *name, LANGID language)
502 return InvalidParameter;
504 TRACE("%p, %p, %d\n", family, name, language);
506 if (language != LANG_NEUTRAL)
507 FIXME("No support for handling of multiple languages!\n");
509 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
515 /*****************************************************************************
516 * GdipDeleteFontFamily [GDIPLUS.@]
518 * Removes the specified FontFamily
521 * *FontFamily [I] The family to delete
525 * FAILURE: InvalidParameter if FontFamily is NULL.
528 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
531 return InvalidParameter;
532 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
534 GdipFree (FontFamily);
539 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
540 INT style, UINT16* CellAscent)
542 if (!(family && CellAscent)) return InvalidParameter;
544 *CellAscent = family->tmw.tmAscent;
549 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
550 INT style, UINT16* CellDescent)
552 if (!(family && CellDescent)) return InvalidParameter;
554 *CellDescent = family->tmw.tmDescent;
559 /*******************************************************************************
560 * GdipGetEmHeight [GDIPLUS.@]
562 * Gets the height of the specified family in EmHeights
565 * family [I] Family to retrieve from
566 * style [I] (optional) style
567 * EmHeight [O] return value
571 * FAILURE: InvalidParameter
573 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
575 if (!(family && EmHeight)) return InvalidParameter;
577 TRACE("%p (%s), %d, %p, stub!\n", family,
578 debugstr_w(family->FamilyName), style, EmHeight);
580 *EmHeight = family->tmw.ntmSizeEM;
586 /*******************************************************************************
587 * GdipGetLineSpacing [GDIPLUS.@]
589 * Returns the line spacing in design units
592 * family [I] Family to retrieve from
593 * style [I] (Optional) font style
594 * LineSpacing [O] Return value
598 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
600 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
601 INT style, UINT16* LineSpacing)
603 if (!(family && LineSpacing)) return InvalidParameter;
607 return NotImplemented;
610 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
611 INT style, BOOL* IsStyleAvailable)
613 FIXME("%p %d %p stub!\n", family, style, IsStyleAvailable);
615 if (!(family && IsStyleAvailable))
616 return InvalidParameter;
618 return NotImplemented;
621 /*****************************************************************************
622 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
624 * Obtains a serif family (Courier New on Windows)
627 * **nativeFamily [I] Where the font will be stored
630 * InvalidParameter if nativeFamily is NULL.
633 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
635 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
637 if (nativeFamily == NULL) return InvalidParameter;
639 return GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
642 /*****************************************************************************
643 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
645 * Obtains a serif family (Times New Roman on Windows)
648 * **nativeFamily [I] Where the font will be stored
651 * InvalidParameter if nativeFamily is NULL.
654 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
656 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
658 if (nativeFamily == NULL) return InvalidParameter;
660 return GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
663 /*****************************************************************************
664 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
666 * Obtains a serif family (Microsoft Sans Serif on Windows)
669 * **nativeFamily [I] Where the font will be stored
672 * InvalidParameter if nativeFamily is NULL.
675 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
677 /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
679 static const WCHAR MSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
681 if (nativeFamily == NULL) return InvalidParameter;
683 return GdipCreateFontFamilyFromName(MSSansSerif, NULL, nativeFamily);
686 /*****************************************************************************
687 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
689 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
691 FIXME("stub %p\n", fontCollection);
694 return InvalidParameter;
696 return NotImplemented;
699 /*****************************************************************************
700 * GdipDeletePrivateFontCollection [GDIPLUS.@]
702 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
704 FIXME("stub %p\n", fontCollection);
707 return InvalidParameter;
709 return NotImplemented;
712 /*****************************************************************************
713 * GdipPrivateAddFontFile [GDIPLUS.@]
715 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection* fontCollection,
716 GDIPCONST WCHAR* filename)
718 FIXME("stub: %p, %s\n", fontCollection, debugstr_w(filename));
720 if (!(fontCollection && filename))
721 return InvalidParameter;
723 return NotImplemented;
726 /*****************************************************************************
727 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
729 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
730 GpFontCollection* fontCollection, INT* numFound)
732 FIXME("stub: %p, %p\n", fontCollection, numFound);
734 if (!(fontCollection && numFound))
735 return InvalidParameter;
737 return NotImplemented;
740 /*****************************************************************************
741 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
743 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
744 GpFontCollection* fontCollection, INT numSought,
745 GpFontFamily* gpfamilies[], INT* numFound)
747 FIXME("stub: %p, %d, %p, %p\n", fontCollection, numSought, gpfamilies,
750 if (!(fontCollection && gpfamilies && numFound))
751 return InvalidParameter;
753 return NotImplemented;