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 TEXTMETRICW
* tmw
;
101 if (!fontFamily
|| !fontFamily
->FamilyName
|| !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
, sizeof(WCHAR
) * 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 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontW(HDC hdc
,
161 GDIPCONST LOGFONTW
*logfont
, GpFont
**font
)
163 HFONT hfont
, oldfont
;
166 if(!logfont
|| !font
)
167 return InvalidParameter
;
169 *font
= GdipAlloc(sizeof(GpFont
));
170 if(!*font
) return OutOfMemory
;
172 memcpy(&(*font
)->lfw
.lfFaceName
, logfont
->lfFaceName
, LF_FACESIZE
*
174 (*font
)->lfw
.lfHeight
= logfont
->lfHeight
;
175 (*font
)->lfw
.lfItalic
= logfont
->lfItalic
;
176 (*font
)->lfw
.lfUnderline
= logfont
->lfUnderline
;
177 (*font
)->lfw
.lfStrikeOut
= logfont
->lfStrikeOut
;
179 (*font
)->emSize
= logfont
->lfHeight
;
180 (*font
)->unit
= UnitPixel
;
182 hfont
= CreateFontIndirectW(&(*font
)->lfw
);
183 oldfont
= SelectObject(hdc
, hfont
);
184 GetTextMetricsW(hdc
, &textmet
);
186 (*font
)->lfw
.lfHeight
= -textmet
.tmHeight
;
187 (*font
)->lfw
.lfWeight
= textmet
.tmWeight
;
189 SelectObject(hdc
, oldfont
);
195 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontA(HDC hdc
,
196 GDIPCONST LOGFONTA
*lfa
, GpFont
**font
)
201 return InvalidParameter
;
203 memcpy(&lfw
, lfa
, FIELD_OFFSET(LOGFONTA
,lfFaceName
) );
205 if(!MultiByteToWideChar(CP_ACP
, 0, lfa
->lfFaceName
, -1, lfw
.lfFaceName
, LF_FACESIZE
))
208 GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
213 GpStatus WINGDIPAPI
GdipDeleteFont(GpFont
* font
)
216 return InvalidParameter
;
223 GpStatus WINGDIPAPI
GdipCreateFontFromDC(HDC hdc
, GpFont
**font
)
229 return InvalidParameter
;
231 hfont
= (HFONT
)GetCurrentObject(hdc
, OBJ_FONT
);
235 if(!GetObjectW(hfont
, sizeof(LOGFONTW
), &lfw
))
238 return GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
241 /******************************************************************************
242 * GdipGetFontSize [GDIPLUS.@]
244 * Returns the size of the font in Units
247 * *font [I] The font to retrieve size from
248 * *size [O] Pointer to hold retrieved value
252 * FAILURE: InvalidParamter (font or size was NULL)
255 * Size returned is actually emSize -- not internal size used for drawing.
257 GpStatus WINGDIPAPI
GdipGetFontSize(GpFont
*font
, REAL
*size
)
259 if (!(font
&& size
)) return InvalidParameter
;
261 *size
= font
->emSize
;
266 /*******************************************************************************
267 * GdipGetFontUnit [GDIPLUS.@]
270 * font [I] Font to retrieve from
271 * unit [O] Return value
274 * FAILURE: font or unit was NULL
277 GpStatus WINGDIPAPI
GdipGetFontUnit(GpFont
*font
, Unit
*unit
)
279 if (!(font
&& unit
)) return InvalidParameter
;
286 /* FIXME: use graphics */
287 GpStatus WINGDIPAPI
GdipGetLogFontW(GpFont
*font
, GpGraphics
*graphics
,
290 if(!font
|| !graphics
|| !lfw
)
291 return InvalidParameter
;
298 GpStatus WINGDIPAPI
GdipCloneFont(GpFont
*font
, GpFont
**cloneFont
)
300 if(!font
|| !cloneFont
)
301 return InvalidParameter
;
303 *cloneFont
= GdipAlloc(sizeof(GpFont
));
304 if(!*cloneFont
) return OutOfMemory
;
311 /* Borrowed from GDI32 */
312 static INT CALLBACK
is_font_installed_proc(const LOGFONTW
*elf
,
313 const TEXTMETRICW
*ntm
, DWORD type
, LPARAM lParam
)
318 static BOOL
is_font_installed(const WCHAR
*name
)
323 if(!EnumFontFamiliesW(hdc
, name
, is_font_installed_proc
, 0))
330 /*******************************************************************************
331 * GdipCreateFontFamilyFromName [GDIPLUS.@]
333 * Creates a font family object based on a supplied name
336 * name [I] Name of the font
337 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
338 * FontFamily [O] Pointer to the resulting FontFamily object
342 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
343 * FAILURE: Invalid parameter if FontFamily or name is NULL
346 * If fontCollection is NULL then the object is not part of any collection
350 GpStatus WINGDIPAPI
GdipCreateFontFamilyFromName(GDIPCONST WCHAR
*name
,
351 GpFontCollection
*fontCollection
,
352 GpFontFamily
**FontFamily
)
354 GpFontFamily
* ffamily
;
356 HFONT hFont
, hfont_old
;
359 TRACE("%s, %p %p\n", debugstr_w(name
), fontCollection
, FontFamily
);
361 if (!(name
&& FontFamily
))
362 return InvalidParameter
;
364 FIXME("No support for FontCollections yet!\n");
365 if (!is_font_installed(name
))
366 return FontFamilyNotFound
;
368 ffamily
= GdipAlloc(sizeof (GpFontFamily
));
369 if (!ffamily
) return OutOfMemory
;
372 lstrcpynW(lfw
.lfFaceName
, name
, sizeof(WCHAR
) * LF_FACESIZE
);
373 lfw
.lfCharSet
= DEFAULT_CHARSET
;
374 lfw
.lfEscapement
= lfw
.lfOrientation
= 0;
376 hFont
= CreateFontIndirectW (&lfw
);
377 hfont_old
= SelectObject(hdc
, hFont
);
379 GetTextMetricsW(hdc
, &ffamily
->tmw
);
380 DeleteObject(SelectObject(hdc
, hfont_old
));
382 ffamily
->FamilyName
= GdipAlloc(LF_FACESIZE
* sizeof (WCHAR
));
383 if (!ffamily
->FamilyName
)
390 lstrcpynW(ffamily
->FamilyName
, name
, sizeof(WCHAR
) * LF_FACESIZE
);
392 *FontFamily
= ffamily
;
398 /*******************************************************************************
399 * GdipCloneFontFamily [GDIPLUS.@]
401 * Creates a deep copy of a Font Family object
404 * FontFamily [I] Font to clone
405 * clonedFontFamily [O] The resulting cloned font
410 GpStatus WINGDIPAPI
GdipCloneFontFamily(GpFontFamily
* FontFamily
, GpFontFamily
** clonedFontFamily
)
412 if (!(FontFamily
&& clonedFontFamily
)) return InvalidParameter
;
414 TRACE("stub: %p (%s), %p\n", FontFamily
,
415 debugstr_w(FontFamily
->FamilyName
), clonedFontFamily
);
417 *clonedFontFamily
= GdipAlloc(sizeof(GpFontFamily
));
418 if (!*clonedFontFamily
) return OutOfMemory
;
420 **clonedFontFamily
= *FontFamily
;
422 (*clonedFontFamily
)->FamilyName
= GdipAlloc(sizeof(WCHAR
) * LF_FACESIZE
);
423 if (!(*clonedFontFamily
)->FamilyName
)
425 GdipFree (clonedFontFamily
);
429 lstrcpynW((*clonedFontFamily
)->FamilyName
, FontFamily
->FamilyName
,
435 /*******************************************************************************
436 * GdipGetFamilyName [GDIPLUS.@]
438 * Returns the family name into name
441 * *family [I] Family to retrieve from
442 * *name [O] WCHARS of the family name
447 * FAILURE: InvalidParameter if family is NULL
450 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
452 GpStatus WINGDIPAPI
GdipGetFamilyName (GDIPCONST GpFontFamily
*family
,
453 WCHAR
*name
, LANGID language
)
456 return InvalidParameter
;
458 TRACE("%p, %p, %d\n", family
, name
, language
);
460 if (language
!= LANG_NEUTRAL
)
461 FIXME("No support for handling of multiple languages!\n");
463 lstrcpynW (name
, family
->FamilyName
, LF_FACESIZE
);
469 /*****************************************************************************
470 * GdipDeleteFontFamily [GDIPLUS.@]
472 * Removes the specified FontFamily
475 * *FontFamily [I] The family to delete
479 * FAILURE: InvalidParameter if FontFamily is NULL.
482 GpStatus WINGDIPAPI
GdipDeleteFontFamily(GpFontFamily
*FontFamily
)
485 return InvalidParameter
;
486 TRACE("Deleting %p (%s)\n", FontFamily
, debugstr_w(FontFamily
->FamilyName
));
488 GdipFree (FontFamily
->FamilyName
);
489 GdipFree (FontFamily
);
494 /*****************************************************************************
495 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
497 * Obtains a serif family (Courier New on Windows)
500 * **nativeFamily [I] Where the font will be stored
503 * InvalidParameter if nativeFamily is NULL.
506 GpStatus WINGDIPAPI
GdipGetGenericFontFamilyMonospace(GpFontFamily
**nativeFamily
)
508 static const WCHAR CourierNew
[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
510 if (nativeFamily
== NULL
) return InvalidParameter
;
512 return GdipCreateFontFamilyFromName(CourierNew
, NULL
, nativeFamily
);
515 /*****************************************************************************
516 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
518 * Obtains a serif family (Times New Roman on Windows)
521 * **nativeFamily [I] Where the font will be stored
524 * InvalidParameter if nativeFamily is NULL.
527 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySerif(GpFontFamily
**nativeFamily
)
529 static const WCHAR TimesNewRoman
[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
531 if (nativeFamily
== NULL
) return InvalidParameter
;
533 return GdipCreateFontFamilyFromName(TimesNewRoman
, NULL
, nativeFamily
);
536 /*****************************************************************************
537 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
539 * Obtains a serif family (Microsoft Sans Serif on Windows)
542 * **nativeFamily [I] Where the font will be stored
545 * InvalidParameter if nativeFamily is NULL.
548 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySansSerif(GpFontFamily
**nativeFamily
)
550 /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
552 static const WCHAR MSSansSerif
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
554 if (nativeFamily
== NULL
) return InvalidParameter
;
556 return GdipCreateFontFamilyFromName(MSSansSerif
, NULL
, nativeFamily
);