Merge commit '1a978cb460194e7f3db4c8d8bf6405411b2f9bcd' into epm
[wine/dcerpc.git] / dlls / gdiplus / font.c
blob0bb9fc7cda59fd711cdeac869f1f1a56f2cfdb70
1 /*
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winnls.h"
25 #include "winreg.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
31 #include "objbase.h"
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
36 static const REAL mm_per_pixel = 25.4;
38 static inline REAL get_dpi (void)
40 REAL dpi;
41 GpGraphics *graphics;
42 HDC hdc = GetDC(0);
43 GdipCreateFromHDC (hdc, &graphics);
44 GdipGetDpiX(graphics, &dpi);
45 ReleaseDC (0, hdc);
47 return dpi;
50 static inline REAL point_to_pixel (REAL point)
52 return point * 1.5;
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
75 * PARAMS
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
82 * RETURNS
83 * SUCCESS: Ok
84 * FAILURE: InvalidParameter if fontfamily or font is NULL.
85 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
87 * NOTES
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];
95 LOGFONTW* lfw;
96 TEXTMETRICW* tmw;
97 GpStatus stat;
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);
121 switch (unit)
123 case UnitWorld:
124 /* FIXME: Figure out when World != Pixel */
125 lfw->lfHeight = emSize; break;
126 case UnitDisplay:
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;
133 case UnitPixel:
134 lfw->lfHeight = emSize; break;
135 case UnitPoint:
136 lfw->lfHeight = point_to_pixel(emSize); break;
137 case UnitInch:
138 lfw->lfHeight = inch_to_pixel(emSize); break;
139 case UnitDocument:
140 lfw->lfHeight = document_to_pixel(emSize); break;
141 case UnitMillimeter:
142 lfw->lfHeight = mm_to_pixel(emSize); break;
145 lfw->lfHeight *= -1;
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;
155 return Ok;
158 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
159 GDIPCONST LOGFONTW *logfont, GpFont **font)
161 HFONT hfont, oldfont;
162 TEXTMETRICW textmet;
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 *
171 sizeof(WCHAR));
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);
188 DeleteObject(hfont);
190 return Ok;
193 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
194 GDIPCONST LOGFONTA *lfa, GpFont **font)
196 LOGFONTW lfw;
198 if(!lfa || !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))
204 return GenericError;
206 GdipCreateFontFromLogfontW(hdc, &lfw, font);
208 return Ok;
211 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
213 if(!font)
214 return InvalidParameter;
216 GdipFree(font);
218 return Ok;
221 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
223 HFONT hfont;
224 LOGFONTW lfw;
226 if(!font)
227 return InvalidParameter;
229 hfont = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
230 if(!hfont)
231 return GenericError;
233 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
234 return GenericError;
236 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
239 /******************************************************************************
240 * GdipGetFontSize [GDIPLUS.@]
242 * Returns the size of the font in Units
244 * PARAMS
245 * *font [I] The font to retrieve size from
246 * *size [O] Pointer to hold retrieved value
248 * RETURNS
249 * SUCCESS: Ok
250 * FAILURE: InvalidParamter (font or size was NULL)
252 * NOTES
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;
261 return Ok;
264 /*******************************************************************************
265 * GdipGetFontUnit [GDIPLUS.@]
267 * PARAMS
268 * font [I] Font to retrieve from
269 * unit [O] Return value
271 * RETURNS
272 * FAILURE: font or unit was NULL
273 * OK: otherwise
275 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
277 if (!(font && unit)) return InvalidParameter;
279 *unit = font->unit;
281 return Ok;
284 /* FIXME: use graphics */
285 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
286 LOGFONTW *lfw)
288 if(!font || !graphics || !lfw)
289 return InvalidParameter;
291 *lfw = font->lfw;
293 return Ok;
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;
304 **cloneFont = *font;
306 return Ok;
309 /* Borrowed from GDI32 */
310 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
311 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
313 return 0;
316 static BOOL is_font_installed(const WCHAR *name)
318 HDC hdc = GetDC(0);
319 BOOL ret = FALSE;
321 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, 0))
322 ret = TRUE;
324 ReleaseDC(0, hdc);
325 return ret;
328 /*******************************************************************************
329 * GdipCreateFontFamilyFromName [GDIPLUS.@]
331 * Creates a font family object based on a supplied name
333 * PARAMS
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
338 * RETURNS
339 * SUCCESS: Ok
340 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
341 * FAILURE: Invalid parameter if FontFamily or name is NULL
343 * NOTES
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;
353 HDC hdc;
354 HFONT hFont;
355 LOGFONTW lfw;
357 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
359 if (!(name && FontFamily))
360 return InvalidParameter;
361 if (fontCollection)
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;}
371 hdc = GetDC(0);
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)
381 GdipFree(ffamily);
382 return OutOfMemory;
385 lstrcpynW(ffamily->FamilyName, name, sizeof(WCHAR) * LF_FACESIZE);
387 *FontFamily = ffamily;
388 ReleaseDC(0, hdc);
390 return Ok;
393 /*******************************************************************************
394 * GdipGetFamilyName [GDIPLUS.@]
396 * Returns the family name into name
398 * PARAMS
399 * *family [I] Family to retrieve from
400 * *name [O] WCHARS of the family name
401 * LANGID [I] charset
403 * RETURNS
404 * SUCCESS: Ok
405 * FAILURE: InvalidParameter if family is NULL
407 * NOTES
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)
413 if (family == NULL)
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);
423 return Ok;
427 /*****************************************************************************
428 * GdipDeleteFontFamily [GDIPLUS.@]
430 * Removes the specified FontFamily
432 * PARAMS
433 * *FontFamily [I] The family to delete
435 * RETURNS
436 * SUCCESS: Ok
437 * FAILURE: InvalidParameter if FontFamily is NULL.
440 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
442 if (!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);
450 return Ok;
453 /*****************************************************************************
454 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
456 * Obtains a serif family (Courier New on Windows)
458 * PARAMS
459 * **nativeFamily [I] Where the font will be stored
461 * RETURNS
462 * InvalidParameter if nativeFamily is NULL.
463 * Ok otherwise.
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)
479 * PARAMS
480 * **nativeFamily [I] Where the font will be stored
482 * RETURNS
483 * InvalidParameter if nativeFamily is NULL.
484 * Ok otherwise.
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)
500 * PARAMS
501 * **nativeFamily [I] Where the font will be stored
503 * RETURNS
504 * InvalidParameter if nativeFamily is NULL.
505 * Ok otherwise.
507 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
509 /* FIXME: On Windows this is called Microsoft Sans Serif, this shouldn't
510 * affect anything */
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);