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_pixel
= 25.4;
38 static inline REAL
get_dpi (void)
43 GdipCreateFromHDC (hdc
, &graphics
);
44 GdipGetDpiX(graphics
, &dpi
);
50 static inline REAL
point_to_pixel (REAL point
)
55 static inline REAL
inch_to_pixel (REAL inch
)
57 return inch
* get_dpi();
60 static inline REAL
document_to_pixel (REAL doc
)
62 return doc
* (get_dpi() / 300.0); /* Per MSDN */
65 static inline REAL
mm_to_pixel (REAL mm
)
67 return mm
* (get_dpi() / mm_per_pixel
);
70 /*******************************************************************************
71 * GdipCreateFont [GDIPLUS.@]
73 * Create a new font based off of a FontFamily
76 * *fontFamily [I] Family to base the font off of
77 * emSize [I] Size of the font
78 * style [I] Bitwise OR of FontStyle enumeration
79 * unit [I] Unit emSize is measured in
80 * **font [I] the resulting Font object
84 * FAILURE: InvalidParameter if fontfamily or font is NULL.
85 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
88 * UnitDisplay is unsupported.
89 * emSize is stored separately from lfHeight, to hold the fraction.
91 GpStatus WINGDIPAPI
GdipCreateFont(GDIPCONST GpFontFamily
*fontFamily
,
92 REAL emSize
, INT style
, Unit unit
, GpFont
**font
)
94 WCHAR facename
[LF_FACESIZE
];
99 if ((!fontFamily
&& fontFamily
->FamilyName
&& font
))
100 return InvalidParameter
;
102 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily
,
103 debugstr_w(fontFamily
->FamilyName
), emSize
, style
, unit
, font
);
105 stat
= GdipGetFamilyName (fontFamily
, facename
, 0);
106 if (stat
!= Ok
) return stat
;
107 *font
= GdipAlloc(sizeof(GpFont
));
109 tmw
= fontFamily
->tmw
;
110 lfw
= &((*font
)->lfw
);
111 ZeroMemory(&(*lfw
), sizeof(*lfw
));
113 lfw
->lfWeight
= tmw
->tmWeight
;
114 lfw
->lfItalic
= tmw
->tmItalic
;
115 lfw
->lfUnderline
= tmw
->tmUnderlined
;
116 lfw
->lfStrikeOut
= tmw
->tmStruckOut
;
117 lfw
->lfCharSet
= tmw
->tmCharSet
;
118 lfw
->lfPitchAndFamily
= tmw
->tmPitchAndFamily
;
119 lstrcpynW((lfw
->lfFaceName
), facename
, sizeof(WCHAR
) * LF_FACESIZE
);
124 /* FIXME: Figure out when World != Pixel */
125 lfw
->lfHeight
= emSize
; break;
127 FIXME("Unknown behavior for UnitDisplay! Please report!\n");
128 /* FIXME: Figure out how this works...
129 * MSDN says that if "DISPLAY" is a monitor, then pixel should be
130 * used. That's not what I got. Tests on Windows revealed no output,
131 * and the tests in tests/font crash windows */
132 lfw
->lfHeight
= 0; break;
134 lfw
->lfHeight
= emSize
; break;
136 lfw
->lfHeight
= point_to_pixel(emSize
); break;
138 lfw
->lfHeight
= inch_to_pixel(emSize
); break;
140 lfw
->lfHeight
= document_to_pixel(emSize
); break;
142 lfw
->lfHeight
= mm_to_pixel(emSize
); break;
147 lfw
->lfWeight
= style
& FontStyleBold
? 700 : 400;
148 lfw
->lfItalic
= style
& FontStyleItalic
;
149 lfw
->lfUnderline
= style
& FontStyleUnderline
;
150 lfw
->lfStrikeOut
= style
& FontStyleStrikeout
;
152 (*font
)->unit
= unit
;
153 (*font
)->emSize
= emSize
;
158 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontW(HDC hdc
,
159 GDIPCONST LOGFONTW
*logfont
, GpFont
**font
)
161 HFONT hfont
, oldfont
;
164 if(!logfont
|| !font
)
165 return InvalidParameter
;
167 *font
= GdipAlloc(sizeof(GpFont
));
168 if(!*font
) return OutOfMemory
;
170 memcpy(&(*font
)->lfw
.lfFaceName
, logfont
->lfFaceName
, LF_FACESIZE
*
172 (*font
)->lfw
.lfHeight
= logfont
->lfHeight
;
173 (*font
)->lfw
.lfItalic
= logfont
->lfItalic
;
174 (*font
)->lfw
.lfUnderline
= logfont
->lfUnderline
;
175 (*font
)->lfw
.lfStrikeOut
= logfont
->lfStrikeOut
;
177 (*font
)->emSize
= logfont
->lfHeight
;
178 (*font
)->unit
= UnitPixel
;
180 hfont
= CreateFontIndirectW(&(*font
)->lfw
);
181 oldfont
= SelectObject(hdc
, hfont
);
182 GetTextMetricsW(hdc
, &textmet
);
184 (*font
)->lfw
.lfHeight
= -textmet
.tmHeight
;
185 (*font
)->lfw
.lfWeight
= textmet
.tmWeight
;
187 SelectObject(hdc
, oldfont
);
193 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontA(HDC hdc
,
194 GDIPCONST LOGFONTA
*lfa
, GpFont
**font
)
199 return InvalidParameter
;
201 memcpy(&lfw
, lfa
, FIELD_OFFSET(LOGFONTA
,lfFaceName
) );
203 if(!MultiByteToWideChar(CP_ACP
, 0, lfa
->lfFaceName
, -1, lfw
.lfFaceName
, LF_FACESIZE
))
206 GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
211 GpStatus WINGDIPAPI
GdipDeleteFont(GpFont
* font
)
214 return InvalidParameter
;
221 GpStatus WINGDIPAPI
GdipCreateFontFromDC(HDC hdc
, GpFont
**font
)
227 return InvalidParameter
;
229 hfont
= (HFONT
)GetCurrentObject(hdc
, OBJ_FONT
);
233 if(!GetObjectW(hfont
, sizeof(LOGFONTW
), &lfw
))
236 return GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
239 /******************************************************************************
240 * GdipGetFontSize [GDIPLUS.@]
242 * Returns the size of the font in Units
245 * *font [I] The font to retrieve size from
246 * *size [O] Pointer to hold retrieved value
250 * FAILURE: InvalidParamter (font or size was NULL)
253 * Size returned is actually emSize -- not internal size used for drawing.
255 GpStatus WINGDIPAPI
GdipGetFontSize(GpFont
*font
, REAL
*size
)
257 if (!(font
&& size
)) return InvalidParameter
;
259 *size
= font
->emSize
;
264 /*******************************************************************************
265 * GdipGetFontUnit [GDIPLUS.@]
268 * font [I] Font to retrieve from
269 * unit [O] Return value
272 * FAILURE: font or unit was NULL
275 GpStatus WINGDIPAPI
GdipGetFontUnit(GpFont
*font
, Unit
*unit
)
277 if (!(font
&& unit
)) return InvalidParameter
;
284 /* FIXME: use graphics */
285 GpStatus WINGDIPAPI
GdipGetLogFontW(GpFont
*font
, GpGraphics
*graphics
,
288 if(!font
|| !graphics
|| !lfw
)
289 return InvalidParameter
;
296 GpStatus WINGDIPAPI
GdipCloneFont(GpFont
*font
, GpFont
**cloneFont
)
298 if(!font
|| !cloneFont
)
299 return InvalidParameter
;
301 *cloneFont
= GdipAlloc(sizeof(GpFont
));
302 if(!*cloneFont
) return OutOfMemory
;
309 /* Borrowed from GDI32 */
310 static INT CALLBACK
is_font_installed_proc(const LOGFONTW
*elf
,
311 const TEXTMETRICW
*ntm
, DWORD type
, LPARAM lParam
)
316 static BOOL
is_font_installed(const WCHAR
*name
)
321 if(!EnumFontFamiliesW(hdc
, name
, is_font_installed_proc
, 0))
328 /*******************************************************************************
329 * GdipCreateFontFamilyFromName [GDIPLUS.@]
331 * Creates a font family object based on a supplied name
334 * name [I] Name of the font
335 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
336 * FontFamily [O] Pointer to the resulting FontFamily object
340 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
341 * FAILURE: Invalid parameter if FontFamily or name is NULL
344 * If fontCollection is NULL then the object is not part of any collection
348 GpStatus WINGDIPAPI
GdipCreateFontFamilyFromName(GDIPCONST WCHAR
*name
,
349 GpFontCollection
*fontCollection
,
350 GpFontFamily
**FontFamily
)
352 GpFontFamily
* ffamily
;
357 TRACE("%s, %p %p\n", debugstr_w(name
), fontCollection
, FontFamily
);
359 if (!(name
&& FontFamily
))
360 return InvalidParameter
;
362 FIXME("No support for FontCollections yet!\n");
363 if (!is_font_installed(name
))
364 return FontFamilyNotFound
;
366 ffamily
= GdipAlloc(sizeof (GpFontFamily
));
367 if (!ffamily
) return OutOfMemory
;
368 ffamily
->tmw
= GdipAlloc(sizeof (TEXTMETRICW
));
369 if (!ffamily
->tmw
) {GdipFree (ffamily
); return OutOfMemory
;}
372 lstrcpynW(lfw
.lfFaceName
, name
, sizeof(WCHAR
) * LF_FACESIZE
);
373 hFont
= CreateFontIndirectW (&lfw
);
374 SelectObject(hdc
, hFont
);
376 GetTextMetricsW(hdc
, ffamily
->tmw
);
378 ffamily
->FamilyName
= GdipAlloc(LF_FACESIZE
* sizeof (WCHAR
));
379 if (!ffamily
->FamilyName
)
385 lstrcpynW(ffamily
->FamilyName
, name
, sizeof(WCHAR
) * LF_FACESIZE
);
387 *FontFamily
= ffamily
;
393 /*******************************************************************************
394 * GdipGetFamilyName [GDIPLUS.@]
396 * Returns the family name into name
399 * *family [I] Family to retrieve from
400 * *name [O] WCHARS of the family name
405 * FAILURE: InvalidParameter if family is NULL
408 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
410 GpStatus WINGDIPAPI
GdipGetFamilyName (GDIPCONST GpFontFamily
*family
,
411 WCHAR
*name
, LANGID language
)
414 return InvalidParameter
;
416 TRACE("%p, %p, %d\n", family
, name
, language
);
418 if (language
!= LANG_NEUTRAL
)
419 FIXME("No support for handling of multiple languages!\n");
421 lstrcpynW (name
, family
->FamilyName
, LF_FACESIZE
);
427 /*****************************************************************************
428 * GdipDeleteFontFamily [GDIPLUS.@]
430 * Removes the specified FontFamily
433 * *FontFamily [I] The family to delete
437 * FAILURE: InvalidParameter if FontFamily is NULL.
440 GpStatus WINGDIPAPI
GdipDeleteFontFamily(GpFontFamily
*FontFamily
)
443 return InvalidParameter
;
444 TRACE("Deleting %p (%s)\n", FontFamily
, debugstr_w(FontFamily
->FamilyName
));
446 if (FontFamily
->FamilyName
) GdipFree (FontFamily
->FamilyName
);
447 if (FontFamily
->tmw
) GdipFree (FontFamily
->tmw
);
448 GdipFree (FontFamily
);
453 /*****************************************************************************
454 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
456 * Obtains a serif family (Courier New on Windows)
459 * **nativeFamily [I] Where the font will be stored
462 * InvalidParameter if nativeFamily is NULL.
465 GpStatus WINGDIPAPI
GdipGetGenericFontFamilyMonospace(GpFontFamily
**nativeFamily
)
467 static const WCHAR CourierNew
[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
469 if (nativeFamily
== NULL
) return InvalidParameter
;
471 return GdipCreateFontFamilyFromName(CourierNew
, NULL
, nativeFamily
);
474 /*****************************************************************************
475 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
477 * Obtains a serif family (Times New Roman on Windows)
480 * **nativeFamily [I] Where the font will be stored
483 * InvalidParameter if nativeFamily is NULL.
486 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySerif(GpFontFamily
**nativeFamily
)
488 static const WCHAR TimesNewRoman
[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
490 if (nativeFamily
== NULL
) return InvalidParameter
;
492 return GdipCreateFontFamilyFromName(TimesNewRoman
, NULL
, nativeFamily
);
495 /*****************************************************************************
496 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
498 * Obtains a serif family (Microsoft Sans Serif on Windows)
501 * **nativeFamily [I] Where the font will be stored
504 * InvalidParameter if nativeFamily is NULL.
507 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySansSerif(GpFontFamily
**nativeFamily
)
509 /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
511 static const WCHAR MSSansSerif
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
513 if (nativeFamily
== NULL
) return InvalidParameter
;
515 return GdipCreateFontFamilyFromName(MSSansSerif
, NULL
, nativeFamily
);