push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / gdiplus / font.c
blob86a32231cc7e99d691ecc5ba33ac56451b8a3cca
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 GdipDeleteGraphics(graphics);
46 ReleaseDC (0, hdc);
48 return dpi;
51 static inline REAL point_to_pixel (REAL point)
53 return point * 1.5;
56 static inline REAL inch_to_pixel (REAL inch)
58 return inch * get_dpi();
61 static inline REAL document_to_pixel (REAL doc)
63 return doc * (get_dpi() / 300.0); /* Per MSDN */
66 static inline REAL mm_to_pixel (REAL mm)
68 return mm * (get_dpi() / mm_per_pixel);
71 /*******************************************************************************
72 * GdipCreateFont [GDIPLUS.@]
74 * Create a new font based off of a FontFamily
76 * PARAMS
77 * *fontFamily [I] Family to base the font off of
78 * emSize [I] Size of the font
79 * style [I] Bitwise OR of FontStyle enumeration
80 * unit [I] Unit emSize is measured in
81 * **font [I] the resulting Font object
83 * RETURNS
84 * SUCCESS: Ok
85 * FAILURE: InvalidParameter if fontfamily or font is NULL.
86 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
88 * NOTES
89 * UnitDisplay is unsupported.
90 * emSize is stored separately from lfHeight, to hold the fraction.
92 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
93 REAL emSize, INT style, Unit unit, GpFont **font)
95 WCHAR facename[LF_FACESIZE];
96 LOGFONTW* lfw;
97 const TEXTMETRICW* tmw;
98 GpStatus stat;
100 if ((!fontFamily && fontFamily->FamilyName && font))
101 return InvalidParameter;
103 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
104 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
106 stat = GdipGetFamilyName (fontFamily, facename, 0);
107 if (stat != Ok) return stat;
108 *font = GdipAlloc(sizeof(GpFont));
110 tmw = &fontFamily->tmw;
111 lfw = &((*font)->lfw);
112 ZeroMemory(&(*lfw), sizeof(*lfw));
114 lfw->lfWeight = tmw->tmWeight;
115 lfw->lfItalic = tmw->tmItalic;
116 lfw->lfUnderline = tmw->tmUnderlined;
117 lfw->lfStrikeOut = tmw->tmStruckOut;
118 lfw->lfCharSet = tmw->tmCharSet;
119 lfw->lfPitchAndFamily = tmw->tmPitchAndFamily;
120 lstrcpynW((lfw->lfFaceName), facename, sizeof(WCHAR) * LF_FACESIZE);
122 switch (unit)
124 case UnitWorld:
125 /* FIXME: Figure out when World != Pixel */
126 lfw->lfHeight = emSize; break;
127 case UnitDisplay:
128 FIXME("Unknown behavior for UnitDisplay! Please report!\n");
129 /* FIXME: Figure out how this works...
130 * MSDN says that if "DISPLAY" is a monitor, then pixel should be
131 * used. That's not what I got. Tests on Windows revealed no output,
132 * and the tests in tests/font crash windows */
133 lfw->lfHeight = 0; break;
134 case UnitPixel:
135 lfw->lfHeight = emSize; break;
136 case UnitPoint:
137 lfw->lfHeight = point_to_pixel(emSize); break;
138 case UnitInch:
139 lfw->lfHeight = inch_to_pixel(emSize); break;
140 case UnitDocument:
141 lfw->lfHeight = document_to_pixel(emSize); break;
142 case UnitMillimeter:
143 lfw->lfHeight = mm_to_pixel(emSize); break;
146 lfw->lfHeight *= -1;
148 lfw->lfWeight = style & FontStyleBold ? 700 : 400;
149 lfw->lfItalic = style & FontStyleItalic;
150 lfw->lfUnderline = style & FontStyleUnderline;
151 lfw->lfStrikeOut = style & FontStyleStrikeout;
153 (*font)->unit = unit;
154 (*font)->emSize = emSize;
156 return Ok;
159 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
160 GDIPCONST LOGFONTW *logfont, GpFont **font)
162 HFONT hfont, oldfont;
163 TEXTMETRICW textmet;
165 if(!logfont || !font)
166 return InvalidParameter;
168 *font = GdipAlloc(sizeof(GpFont));
169 if(!*font) return OutOfMemory;
171 memcpy(&(*font)->lfw.lfFaceName, logfont->lfFaceName, LF_FACESIZE *
172 sizeof(WCHAR));
173 (*font)->lfw.lfHeight = logfont->lfHeight;
174 (*font)->lfw.lfItalic = logfont->lfItalic;
175 (*font)->lfw.lfUnderline = logfont->lfUnderline;
176 (*font)->lfw.lfStrikeOut = logfont->lfStrikeOut;
178 (*font)->emSize = logfont->lfHeight;
179 (*font)->unit = UnitPixel;
181 hfont = CreateFontIndirectW(&(*font)->lfw);
182 oldfont = SelectObject(hdc, hfont);
183 GetTextMetricsW(hdc, &textmet);
185 (*font)->lfw.lfHeight = -textmet.tmHeight;
186 (*font)->lfw.lfWeight = textmet.tmWeight;
188 SelectObject(hdc, oldfont);
189 DeleteObject(hfont);
191 return Ok;
194 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
195 GDIPCONST LOGFONTA *lfa, GpFont **font)
197 LOGFONTW lfw;
199 if(!lfa || !font)
200 return InvalidParameter;
202 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
204 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
205 return GenericError;
207 GdipCreateFontFromLogfontW(hdc, &lfw, font);
209 return Ok;
212 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
214 if(!font)
215 return InvalidParameter;
217 GdipFree(font);
219 return Ok;
222 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
224 HFONT hfont;
225 LOGFONTW lfw;
227 if(!font)
228 return InvalidParameter;
230 hfont = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
231 if(!hfont)
232 return GenericError;
234 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
235 return GenericError;
237 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
240 /******************************************************************************
241 * GdipGetFontSize [GDIPLUS.@]
243 * Returns the size of the font in Units
245 * PARAMS
246 * *font [I] The font to retrieve size from
247 * *size [O] Pointer to hold retrieved value
249 * RETURNS
250 * SUCCESS: Ok
251 * FAILURE: InvalidParamter (font or size was NULL)
253 * NOTES
254 * Size returned is actually emSize -- not internal size used for drawing.
256 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
258 if (!(font && size)) return InvalidParameter;
260 *size = font->emSize;
262 return Ok;
265 /*******************************************************************************
266 * GdipGetFontUnit [GDIPLUS.@]
268 * PARAMS
269 * font [I] Font to retrieve from
270 * unit [O] Return value
272 * RETURNS
273 * FAILURE: font or unit was NULL
274 * OK: otherwise
276 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
278 if (!(font && unit)) return InvalidParameter;
280 *unit = font->unit;
282 return Ok;
285 /* FIXME: use graphics */
286 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics,
287 LOGFONTW *lfw)
289 if(!font || !graphics || !lfw)
290 return InvalidParameter;
292 *lfw = font->lfw;
294 return Ok;
297 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
299 if(!font || !cloneFont)
300 return InvalidParameter;
302 *cloneFont = GdipAlloc(sizeof(GpFont));
303 if(!*cloneFont) return OutOfMemory;
305 **cloneFont = *font;
307 return Ok;
310 /* Borrowed from GDI32 */
311 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
312 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
314 return 0;
317 static BOOL is_font_installed(const WCHAR *name)
319 HDC hdc = GetDC(0);
320 BOOL ret = FALSE;
322 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, 0))
323 ret = TRUE;
325 ReleaseDC(0, hdc);
326 return ret;
329 /*******************************************************************************
330 * GdipCreateFontFamilyFromName [GDIPLUS.@]
332 * Creates a font family object based on a supplied name
334 * PARAMS
335 * name [I] Name of the font
336 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
337 * FontFamily [O] Pointer to the resulting FontFamily object
339 * RETURNS
340 * SUCCESS: Ok
341 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
342 * FAILURE: Invalid parameter if FontFamily or name is NULL
344 * NOTES
345 * If fontCollection is NULL then the object is not part of any collection
349 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
350 GpFontCollection *fontCollection,
351 GpFontFamily **FontFamily)
353 GpFontFamily* ffamily;
354 HDC hdc;
355 HFONT hFont, hfont_old;
356 LOGFONTW lfw;
358 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
360 if (!(name && FontFamily))
361 return InvalidParameter;
362 if (fontCollection)
363 FIXME("No support for FontCollections yet!\n");
364 if (!is_font_installed(name))
365 return FontFamilyNotFound;
367 ffamily = GdipAlloc(sizeof (GpFontFamily));
368 if (!ffamily) return OutOfMemory;
370 hdc = GetDC(0);
371 lstrcpynW(lfw.lfFaceName, name, sizeof(WCHAR) * LF_FACESIZE);
372 hFont = CreateFontIndirectW (&lfw);
373 hfont_old = SelectObject(hdc, hFont);
375 GetTextMetricsW(hdc, &ffamily->tmw);
376 DeleteObject(SelectObject(hdc, hfont_old));
378 ffamily->FamilyName = GdipAlloc(LF_FACESIZE * sizeof (WCHAR));
379 if (!ffamily->FamilyName)
381 GdipFree(ffamily);
382 ReleaseDC(0, hdc);
383 return OutOfMemory;
386 lstrcpynW(ffamily->FamilyName, name, sizeof(WCHAR) * LF_FACESIZE);
388 *FontFamily = ffamily;
389 ReleaseDC(0, hdc);
391 return Ok;
394 /*******************************************************************************
395 * GdipGetFamilyName [GDIPLUS.@]
397 * Returns the family name into name
399 * PARAMS
400 * *family [I] Family to retrieve from
401 * *name [O] WCHARS of the family name
402 * LANGID [I] charset
404 * RETURNS
405 * SUCCESS: Ok
406 * FAILURE: InvalidParameter if family is NULL
408 * NOTES
409 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
411 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
412 WCHAR *name, LANGID language)
414 if (family == NULL)
415 return InvalidParameter;
417 TRACE("%p, %p, %d\n", family, name, language);
419 if (language != LANG_NEUTRAL)
420 FIXME("No support for handling of multiple languages!\n");
422 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
424 return Ok;
428 /*****************************************************************************
429 * GdipDeleteFontFamily [GDIPLUS.@]
431 * Removes the specified FontFamily
433 * PARAMS
434 * *FontFamily [I] The family to delete
436 * RETURNS
437 * SUCCESS: Ok
438 * FAILURE: InvalidParameter if FontFamily is NULL.
441 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
443 if (!FontFamily)
444 return InvalidParameter;
445 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
447 if (FontFamily->FamilyName) GdipFree (FontFamily->FamilyName);
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);