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 GpFontCollection installedFontCollection
= {0};
41 static inline REAL
get_dpi (void)
46 GdipCreateFromHDC (hdc
, &graphics
);
47 GdipGetDpiX(graphics
, &dpi
);
48 GdipDeleteGraphics(graphics
);
54 static inline REAL
point_to_pixel (REAL point
)
56 return point
* get_dpi() * inch_per_point
;
59 static inline REAL
inch_to_pixel (REAL inch
)
61 return inch
* get_dpi();
64 static inline REAL
document_to_pixel (REAL doc
)
66 return doc
* (get_dpi() / 300.0); /* Per MSDN */
69 static inline REAL
mm_to_pixel (REAL mm
)
71 return mm
* (get_dpi() / mm_per_inch
);
74 /*******************************************************************************
75 * GdipCreateFont [GDIPLUS.@]
77 * Create a new font based off of a FontFamily
80 * *fontFamily [I] Family to base the font off of
81 * emSize [I] Size of the font
82 * style [I] Bitwise OR of FontStyle enumeration
83 * unit [I] Unit emSize is measured in
84 * **font [I] the resulting Font object
88 * FAILURE: InvalidParameter if fontfamily or font is NULL.
89 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
92 * UnitDisplay is unsupported.
93 * emSize is stored separately from lfHeight, to hold the fraction.
95 GpStatus WINGDIPAPI
GdipCreateFont(GDIPCONST GpFontFamily
*fontFamily
,
96 REAL emSize
, INT style
, Unit unit
, GpFont
**font
)
98 WCHAR facename
[LF_FACESIZE
];
100 const NEWTEXTMETRICW
* tmw
;
103 if (!fontFamily
|| !font
)
104 return InvalidParameter
;
106 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily
,
107 debugstr_w(fontFamily
->FamilyName
), emSize
, style
, unit
, font
);
109 stat
= GdipGetFamilyName (fontFamily
, facename
, 0);
110 if (stat
!= Ok
) return stat
;
111 *font
= GdipAlloc(sizeof(GpFont
));
113 tmw
= &fontFamily
->tmw
;
114 lfw
= &((*font
)->lfw
);
115 ZeroMemory(&(*lfw
), sizeof(*lfw
));
117 lfw
->lfWeight
= tmw
->tmWeight
;
118 lfw
->lfItalic
= tmw
->tmItalic
;
119 lfw
->lfUnderline
= tmw
->tmUnderlined
;
120 lfw
->lfStrikeOut
= tmw
->tmStruckOut
;
121 lfw
->lfCharSet
= tmw
->tmCharSet
;
122 lfw
->lfPitchAndFamily
= tmw
->tmPitchAndFamily
;
123 lstrcpynW(lfw
->lfFaceName
, facename
, LF_FACESIZE
);
128 /* FIXME: Figure out when World != Pixel */
129 (*font
)->pixel_size
= emSize
; break;
131 FIXME("Unknown behavior for UnitDisplay! Please report!\n");
132 /* FIXME: Figure out how this works...
133 * MSDN says that if "DISPLAY" is a monitor, then pixel should be
134 * used. That's not what I got. Tests on Windows revealed no output,
135 * and the tests in tests/font crash windows */
136 (*font
)->pixel_size
= 0; break;
138 (*font
)->pixel_size
= emSize
; break;
140 (*font
)->pixel_size
= point_to_pixel(emSize
); break;
142 (*font
)->pixel_size
= inch_to_pixel(emSize
); break;
144 (*font
)->pixel_size
= document_to_pixel(emSize
); break;
146 (*font
)->pixel_size
= mm_to_pixel(emSize
); break;
149 lfw
->lfHeight
= (*font
)->pixel_size
* -1;
151 lfw
->lfWeight
= style
& FontStyleBold
? 700 : 400;
152 lfw
->lfItalic
= style
& FontStyleItalic
;
153 lfw
->lfUnderline
= style
& FontStyleUnderline
;
154 lfw
->lfStrikeOut
= style
& FontStyleStrikeout
;
156 (*font
)->unit
= unit
;
157 (*font
)->emSize
= emSize
;
158 (*font
)->height
= tmw
->ntmSizeEM
;
159 (*font
)->line_spacing
= tmw
->tmAscent
+ tmw
->tmDescent
+ tmw
->tmExternalLeading
;
161 TRACE("<-- %p\n", *font
);
166 /*******************************************************************************
167 * GdipCreateFontFromLogfontW [GDIPLUS.@]
169 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontW(HDC hdc
,
170 GDIPCONST LOGFONTW
*logfont
, GpFont
**font
)
172 HFONT hfont
, oldfont
;
175 TRACE("(%p, %p, %p)\n", hdc
, logfont
, font
);
177 if(!logfont
|| !font
)
178 return InvalidParameter
;
180 if (logfont
->lfFaceName
[0] == 0)
181 return NotTrueTypeFont
;
183 *font
= GdipAlloc(sizeof(GpFont
));
184 if(!*font
) return OutOfMemory
;
186 memcpy((*font
)->lfw
.lfFaceName
, logfont
->lfFaceName
, LF_FACESIZE
*
188 (*font
)->lfw
.lfHeight
= logfont
->lfHeight
;
189 (*font
)->lfw
.lfItalic
= logfont
->lfItalic
;
190 (*font
)->lfw
.lfUnderline
= logfont
->lfUnderline
;
191 (*font
)->lfw
.lfStrikeOut
= logfont
->lfStrikeOut
;
193 hfont
= CreateFontIndirectW(&(*font
)->lfw
);
194 oldfont
= SelectObject(hdc
, hfont
);
195 GetTextMetricsW(hdc
, &textmet
);
197 (*font
)->lfw
.lfHeight
= -(textmet
.tmHeight
-textmet
.tmInternalLeading
);
198 (*font
)->lfw
.lfWeight
= textmet
.tmWeight
;
199 (*font
)->lfw
.lfCharSet
= textmet
.tmCharSet
;
201 (*font
)->pixel_size
= (*font
)->emSize
= textmet
.tmHeight
;
202 (*font
)->unit
= UnitPixel
;
203 (*font
)->height
= 1; /* FIXME: need NEWTEXTMETRIC.ntmSizeEM here */
204 (*font
)->line_spacing
= textmet
.tmAscent
+ textmet
.tmDescent
+ textmet
.tmExternalLeading
;
206 SelectObject(hdc
, oldfont
);
209 TRACE("<-- %p\n", *font
);
214 /*******************************************************************************
215 * GdipCreateFontFromLogfontA [GDIPLUS.@]
217 GpStatus WINGDIPAPI
GdipCreateFontFromLogfontA(HDC hdc
,
218 GDIPCONST LOGFONTA
*lfa
, GpFont
**font
)
222 TRACE("(%p, %p, %p)\n", hdc
, lfa
, font
);
225 return InvalidParameter
;
227 memcpy(&lfw
, lfa
, FIELD_OFFSET(LOGFONTA
,lfFaceName
) );
229 if(!MultiByteToWideChar(CP_ACP
, 0, lfa
->lfFaceName
, -1, lfw
.lfFaceName
, LF_FACESIZE
))
232 return GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
235 /*******************************************************************************
236 * GdipDeleteFont [GDIPLUS.@]
238 GpStatus WINGDIPAPI
GdipDeleteFont(GpFont
* font
)
240 TRACE("(%p)\n", font
);
243 return InvalidParameter
;
250 /*******************************************************************************
251 * GdipCreateFontFromDC [GDIPLUS.@]
253 GpStatus WINGDIPAPI
GdipCreateFontFromDC(HDC hdc
, GpFont
**font
)
258 TRACE("(%p, %p)\n", hdc
, font
);
261 return InvalidParameter
;
263 hfont
= GetCurrentObject(hdc
, OBJ_FONT
);
267 if(!GetObjectW(hfont
, sizeof(LOGFONTW
), &lfw
))
270 return GdipCreateFontFromLogfontW(hdc
, &lfw
, font
);
273 /*******************************************************************************
274 * GdipGetFamily [GDIPLUS.@]
276 * Returns the FontFamily for the specified Font
279 * font [I] Font to request from
280 * family [O] Resulting FontFamily object
284 * FAILURE: An element of GpStatus
286 GpStatus WINGDIPAPI
GdipGetFamily(GpFont
*font
, GpFontFamily
**family
)
288 TRACE("%p %p\n", font
, family
);
290 if (!(font
&& family
))
291 return InvalidParameter
;
293 return GdipCreateFontFamilyFromName(font
->lfw
.lfFaceName
, NULL
, family
);
296 /******************************************************************************
297 * GdipGetFontSize [GDIPLUS.@]
299 * Returns the size of the font in Units
302 * *font [I] The font to retrieve size from
303 * *size [O] Pointer to hold retrieved value
307 * FAILURE: InvalidParameter (font or size was NULL)
310 * Size returned is actually emSize -- not internal size used for drawing.
312 GpStatus WINGDIPAPI
GdipGetFontSize(GpFont
*font
, REAL
*size
)
314 TRACE("(%p, %p)\n", font
, size
);
316 if (!(font
&& size
)) return InvalidParameter
;
318 *size
= font
->emSize
;
323 /*******************************************************************************
324 * GdipGetFontStyle [GDIPLUS.@]
326 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
329 * font [I] font to request from
330 * style [O] resulting pointer to a FontStyle enumeration
334 * FAILURE: InvalidParameter
336 GpStatus WINGDIPAPI
GdipGetFontStyle(GpFont
*font
, INT
*style
)
338 TRACE("%p %p\n", font
, style
);
340 if (!(font
&& style
))
341 return InvalidParameter
;
343 if (font
->lfw
.lfWeight
> 400)
344 *style
= FontStyleBold
;
347 if (font
->lfw
.lfItalic
)
348 *style
|= FontStyleItalic
;
349 if (font
->lfw
.lfUnderline
)
350 *style
|= FontStyleUnderline
;
351 if (font
->lfw
.lfStrikeOut
)
352 *style
|= FontStyleStrikeout
;
357 /*******************************************************************************
358 * GdipGetFontUnit [GDIPLUS.@]
361 * font [I] Font to retrieve from
362 * unit [O] Return value
365 * FAILURE: font or unit was NULL
368 GpStatus WINGDIPAPI
GdipGetFontUnit(GpFont
*font
, Unit
*unit
)
370 TRACE("(%p, %p)\n", font
, unit
);
372 if (!(font
&& unit
)) return InvalidParameter
;
379 /*******************************************************************************
380 * GdipGetLogFontA [GDIPLUS.@]
382 GpStatus WINGDIPAPI
GdipGetLogFontA(GpFont
*font
, GpGraphics
*graphics
,
388 TRACE("(%p, %p, %p)\n", font
, graphics
, lfa
);
390 status
= GdipGetLogFontW(font
, graphics
, &lfw
);
394 memcpy(lfa
, &lfw
, FIELD_OFFSET(LOGFONTA
,lfFaceName
) );
396 if(!WideCharToMultiByte(CP_ACP
, 0, lfw
.lfFaceName
, -1, lfa
->lfFaceName
, LF_FACESIZE
, NULL
, NULL
))
402 /*******************************************************************************
403 * GdipGetLogFontW [GDIPLUS.@]
405 GpStatus WINGDIPAPI
GdipGetLogFontW(GpFont
*font
, GpGraphics
*graphics
,
408 TRACE("(%p, %p, %p)\n", font
, graphics
, lfw
);
410 /* FIXME: use graphics */
411 if(!font
|| !graphics
|| !lfw
)
412 return InvalidParameter
;
419 /*******************************************************************************
420 * GdipCloneFont [GDIPLUS.@]
422 GpStatus WINGDIPAPI
GdipCloneFont(GpFont
*font
, GpFont
**cloneFont
)
424 TRACE("(%p, %p)\n", font
, cloneFont
);
426 if(!font
|| !cloneFont
)
427 return InvalidParameter
;
429 *cloneFont
= GdipAlloc(sizeof(GpFont
));
430 if(!*cloneFont
) return OutOfMemory
;
437 /*******************************************************************************
438 * GdipGetFontHeight [GDIPLUS.@]
440 * font [I] Font to retrieve height from
441 * graphics [I] The current graphics context
442 * height [O] Resulting height
445 * FAILURE: Another element of GpStatus
448 * Forwards to GdipGetFontHeightGivenDPI
450 GpStatus WINGDIPAPI
GdipGetFontHeight(GDIPCONST GpFont
*font
,
451 GDIPCONST GpGraphics
*graphics
, REAL
*height
)
456 TRACE("%p %p %p\n", font
, graphics
, height
);
458 stat
= GdipGetDpiY((GpGraphics
*)graphics
, &dpi
);
461 stat
= GdipGetFontHeightGivenDPI(font
, dpi
, height
);
466 /*******************************************************************************
467 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
469 * font [I] Font to retrieve DPI from
470 * dpi [I] DPI to assume
471 * height [O] Return value
475 * FAILURE: InvalidParameter if font or height is NULL
478 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
479 * (for anything other than unit Pixel)
481 GpStatus WINGDIPAPI
GdipGetFontHeightGivenDPI(GDIPCONST GpFont
*font
, REAL dpi
, REAL
*height
)
485 TRACE("%p (%s), %f, %p\n", font
,
486 debugstr_w(font
->lfw
.lfFaceName
), dpi
, height
);
488 if (!(font
&& height
)) return InvalidParameter
;
490 font_height
= font
->line_spacing
* (font
->emSize
/ font
->height
);
496 *height
= font_height
;
499 *height
= font_height
* dpi
* inch_per_point
;
502 *height
= font_height
* dpi
;
505 *height
= font_height
* (dpi
/ 300.0);
508 *height
= font_height
* (dpi
/ mm_per_inch
);
511 FIXME("Unhandled unit type: %d\n", font
->unit
);
512 return NotImplemented
;
518 /***********************************************************************
519 * Borrowed from GDI32:
521 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
522 * We have to use other types because of the FONTENUMPROCW definition.
524 static INT CALLBACK
is_font_installed_proc(const LOGFONTW
*elf
,
525 const TEXTMETRICW
*ntm
, DWORD type
, LPARAM lParam
)
527 if (!ntm
|| type
== RASTER_FONTTYPE
)
532 *(NEWTEXTMETRICW
*)lParam
= *(const NEWTEXTMETRICW
*)ntm
;
537 static BOOL
find_installed_font(const WCHAR
*name
, NEWTEXTMETRICW
*ntm
)
542 if(!EnumFontFamiliesW(hdc
, name
, is_font_installed_proc
, (LPARAM
)ntm
))
549 /*******************************************************************************
550 * GdipCreateFontFamilyFromName [GDIPLUS.@]
552 * Creates a font family object based on a supplied name
555 * name [I] Name of the font
556 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
557 * FontFamily [O] Pointer to the resulting FontFamily object
561 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
562 * FAILURE: Invalid parameter if FontFamily or name is NULL
565 * If fontCollection is NULL then the object is not part of any collection
569 GpStatus WINGDIPAPI
GdipCreateFontFamilyFromName(GDIPCONST WCHAR
*name
,
570 GpFontCollection
*fontCollection
,
571 GpFontFamily
**FontFamily
)
573 GpFontFamily
* ffamily
;
576 TRACE("%s, %p %p\n", debugstr_w(name
), fontCollection
, FontFamily
);
578 if (!(name
&& FontFamily
))
579 return InvalidParameter
;
581 FIXME("No support for FontCollections yet!\n");
583 if (!find_installed_font(name
, &ntm
))
584 return FontFamilyNotFound
;
586 ffamily
= GdipAlloc(sizeof (GpFontFamily
));
587 if (!ffamily
) return OutOfMemory
;
590 lstrcpynW(ffamily
->FamilyName
, name
, LF_FACESIZE
);
592 *FontFamily
= ffamily
;
594 TRACE("<-- %p\n", ffamily
);
599 /*******************************************************************************
600 * GdipCloneFontFamily [GDIPLUS.@]
602 * Creates a deep copy of a Font Family object
605 * FontFamily [I] Font to clone
606 * clonedFontFamily [O] The resulting cloned font
611 GpStatus WINGDIPAPI
GdipCloneFontFamily(GpFontFamily
* FontFamily
, GpFontFamily
** clonedFontFamily
)
613 if (!(FontFamily
&& clonedFontFamily
)) return InvalidParameter
;
615 TRACE("stub: %p (%s), %p\n", FontFamily
,
616 debugstr_w(FontFamily
->FamilyName
), clonedFontFamily
);
618 *clonedFontFamily
= GdipAlloc(sizeof(GpFontFamily
));
619 if (!*clonedFontFamily
) return OutOfMemory
;
621 (*clonedFontFamily
)->tmw
= FontFamily
->tmw
;
622 lstrcpyW((*clonedFontFamily
)->FamilyName
, FontFamily
->FamilyName
);
624 TRACE("<-- %p\n", *clonedFontFamily
);
629 /*******************************************************************************
630 * GdipGetFamilyName [GDIPLUS.@]
632 * Returns the family name into name
635 * *family [I] Family to retrieve from
636 * *name [O] WCHARS of the family name
641 * FAILURE: InvalidParameter if family is NULL
644 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
646 GpStatus WINGDIPAPI
GdipGetFamilyName (GDIPCONST GpFontFamily
*family
,
647 WCHAR
*name
, LANGID language
)
649 static int lang_fixme
;
652 return InvalidParameter
;
654 TRACE("%p, %p, %d\n", family
, name
, language
);
656 if (language
!= LANG_NEUTRAL
&& !lang_fixme
++)
657 FIXME("No support for handling of multiple languages!\n");
659 lstrcpynW (name
, family
->FamilyName
, LF_FACESIZE
);
665 /*****************************************************************************
666 * GdipDeleteFontFamily [GDIPLUS.@]
668 * Removes the specified FontFamily
671 * *FontFamily [I] The family to delete
675 * FAILURE: InvalidParameter if FontFamily is NULL.
678 GpStatus WINGDIPAPI
GdipDeleteFontFamily(GpFontFamily
*FontFamily
)
681 return InvalidParameter
;
682 TRACE("Deleting %p (%s)\n", FontFamily
, debugstr_w(FontFamily
->FamilyName
));
684 GdipFree (FontFamily
);
689 GpStatus WINGDIPAPI
GdipGetCellAscent(GDIPCONST GpFontFamily
*family
,
690 INT style
, UINT16
* CellAscent
)
692 if (!(family
&& CellAscent
)) return InvalidParameter
;
694 *CellAscent
= family
->tmw
.tmAscent
;
699 GpStatus WINGDIPAPI
GdipGetCellDescent(GDIPCONST GpFontFamily
*family
,
700 INT style
, UINT16
* CellDescent
)
702 TRACE("(%p, %d, %p)\n", family
, style
, CellDescent
);
704 if (!(family
&& CellDescent
)) return InvalidParameter
;
706 *CellDescent
= family
->tmw
.tmDescent
;
711 /*******************************************************************************
712 * GdipGetEmHeight [GDIPLUS.@]
714 * Gets the height of the specified family in EmHeights
717 * family [I] Family to retrieve from
718 * style [I] (optional) style
719 * EmHeight [O] return value
723 * FAILURE: InvalidParameter
725 GpStatus WINGDIPAPI
GdipGetEmHeight(GDIPCONST GpFontFamily
*family
, INT style
, UINT16
* EmHeight
)
727 if (!(family
&& EmHeight
)) return InvalidParameter
;
729 TRACE("%p (%s), %d, %p\n", family
, debugstr_w(family
->FamilyName
), style
, EmHeight
);
731 *EmHeight
= family
->tmw
.ntmSizeEM
;
737 /*******************************************************************************
738 * GdipGetLineSpacing [GDIPLUS.@]
740 * Returns the line spacing in design units
743 * family [I] Family to retrieve from
744 * style [I] (Optional) font style
745 * LineSpacing [O] Return value
749 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
751 GpStatus WINGDIPAPI
GdipGetLineSpacing(GDIPCONST GpFontFamily
*family
,
752 INT style
, UINT16
* LineSpacing
)
754 TRACE("%p, %d, %p\n", family
, style
, LineSpacing
);
756 if (!(family
&& LineSpacing
))
757 return InvalidParameter
;
759 if (style
) FIXME("ignoring style\n");
761 *LineSpacing
= family
->tmw
.tmAscent
+ family
->tmw
.tmDescent
+ family
->tmw
.tmExternalLeading
;
766 static INT CALLBACK
font_has_style_proc(const LOGFONTW
*elf
,
767 const TEXTMETRICW
*ntm
, DWORD type
, LPARAM lParam
)
773 if (ntm
->tmWeight
>= FW_BOLD
) fontstyle
|= FontStyleBold
;
774 if (ntm
->tmItalic
) fontstyle
|= FontStyleItalic
;
775 if (ntm
->tmUnderlined
) fontstyle
|= FontStyleUnderline
;
776 if (ntm
->tmStruckOut
) fontstyle
|= FontStyleStrikeout
;
778 return (INT
)lParam
!= fontstyle
;
781 GpStatus WINGDIPAPI
GdipIsStyleAvailable(GDIPCONST GpFontFamily
* family
,
782 INT style
, BOOL
* IsStyleAvailable
)
786 TRACE("%p %d %p\n", family
, style
, IsStyleAvailable
);
788 if (!(family
&& IsStyleAvailable
))
789 return InvalidParameter
;
791 *IsStyleAvailable
= FALSE
;
795 if(!EnumFontFamiliesW(hdc
, family
->FamilyName
, font_has_style_proc
, (LPARAM
)style
))
796 *IsStyleAvailable
= TRUE
;
803 /*****************************************************************************
804 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
806 * Obtains a serif family (Courier New on Windows)
809 * **nativeFamily [I] Where the font will be stored
812 * InvalidParameter if nativeFamily is NULL.
815 GpStatus WINGDIPAPI
GdipGetGenericFontFamilyMonospace(GpFontFamily
**nativeFamily
)
817 static const WCHAR CourierNew
[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
818 static const WCHAR LiberationMono
[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
821 if (nativeFamily
== NULL
) return InvalidParameter
;
823 stat
= GdipCreateFontFamilyFromName(CourierNew
, NULL
, nativeFamily
);
825 if (stat
== FontFamilyNotFound
)
826 stat
= GdipCreateFontFamilyFromName(LiberationMono
, NULL
, nativeFamily
);
828 if (stat
== FontFamilyNotFound
)
829 ERR("Missing 'Courier New' font\n");
834 /*****************************************************************************
835 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
837 * Obtains a serif family (Times New Roman on Windows)
840 * **nativeFamily [I] Where the font will be stored
843 * InvalidParameter if nativeFamily is NULL.
846 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySerif(GpFontFamily
**nativeFamily
)
848 static const WCHAR TimesNewRoman
[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
849 static const WCHAR LiberationSerif
[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
852 TRACE("(%p)\n", nativeFamily
);
854 if (nativeFamily
== NULL
) return InvalidParameter
;
856 stat
= GdipCreateFontFamilyFromName(TimesNewRoman
, NULL
, nativeFamily
);
858 if (stat
== FontFamilyNotFound
)
859 stat
= GdipCreateFontFamilyFromName(LiberationSerif
, NULL
, nativeFamily
);
861 if (stat
== FontFamilyNotFound
)
862 ERR("Missing 'Times New Roman' font\n");
867 /*****************************************************************************
868 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
870 * Obtains a serif family (Microsoft Sans Serif on Windows)
873 * **nativeFamily [I] Where the font will be stored
876 * InvalidParameter if nativeFamily is NULL.
879 GpStatus WINGDIPAPI
GdipGetGenericFontFamilySansSerif(GpFontFamily
**nativeFamily
)
882 static const WCHAR MicrosoftSansSerif
[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
883 static const WCHAR Tahoma
[] = {'T','a','h','o','m','a','\0'};
885 TRACE("(%p)\n", nativeFamily
);
887 if (nativeFamily
== NULL
) return InvalidParameter
;
889 stat
= GdipCreateFontFamilyFromName(MicrosoftSansSerif
, NULL
, nativeFamily
);
891 if (stat
== FontFamilyNotFound
)
892 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
893 stat
= GdipCreateFontFamilyFromName(Tahoma
, NULL
, nativeFamily
);
898 /*****************************************************************************
899 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
901 GpStatus WINGDIPAPI
GdipNewPrivateFontCollection(GpFontCollection
** fontCollection
)
903 TRACE("%p\n", fontCollection
);
906 return InvalidParameter
;
908 *fontCollection
= GdipAlloc(sizeof(GpFontCollection
));
909 if (!*fontCollection
) return OutOfMemory
;
911 (*fontCollection
)->FontFamilies
= NULL
;
912 (*fontCollection
)->count
= 0;
913 (*fontCollection
)->allocated
= 0;
915 TRACE("<-- %p\n", *fontCollection
);
920 /*****************************************************************************
921 * GdipDeletePrivateFontCollection [GDIPLUS.@]
923 GpStatus WINGDIPAPI
GdipDeletePrivateFontCollection(GpFontCollection
**fontCollection
)
927 TRACE("%p\n", fontCollection
);
930 return InvalidParameter
;
932 for (i
= 0; i
< (*fontCollection
)->count
; i
++) GdipFree((*fontCollection
)->FontFamilies
[i
]);
933 GdipFree(*fontCollection
);
938 /*****************************************************************************
939 * GdipPrivateAddFontFile [GDIPLUS.@]
941 GpStatus WINGDIPAPI
GdipPrivateAddFontFile(GpFontCollection
* fontCollection
,
942 GDIPCONST WCHAR
* filename
)
944 FIXME("stub: %p, %s\n", fontCollection
, debugstr_w(filename
));
946 if (!(fontCollection
&& filename
))
947 return InvalidParameter
;
949 return NotImplemented
;
952 /* Copied from msi/font.c */
954 typedef struct _tagTT_OFFSET_TABLE
{
955 USHORT uMajorVersion
;
956 USHORT uMinorVersion
;
959 USHORT uEntrySelector
;
963 typedef struct _tagTT_TABLE_DIRECTORY
{
964 char szTag
[4]; /* table name */
965 ULONG uCheckSum
; /* Check sum */
966 ULONG uOffset
; /* Offset from beginning of file */
967 ULONG uLength
; /* length of the table in bytes */
968 } TT_TABLE_DIRECTORY
;
970 typedef struct _tagTT_NAME_TABLE_HEADER
{
971 USHORT uFSelector
; /* format selector. Always 0 */
972 USHORT uNRCount
; /* Name Records count */
973 USHORT uStorageOffset
; /* Offset for strings storage,
974 * from start of the table */
975 } TT_NAME_TABLE_HEADER
;
977 #define NAME_ID_FULL_FONT_NAME 4
978 #define NAME_ID_VERSION 5
980 typedef struct _tagTT_NAME_RECORD
{
985 USHORT uStringLength
;
986 USHORT uStringOffset
; /* from start of storage area */
989 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
990 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
993 * Code based off of code located here
994 * http://www.codeproject.com/gdi/fontnamefromfile.asp
996 static WCHAR
*load_ttf_name_id( const char *mem
, DWORD_PTR size
, DWORD id
, WCHAR
*ret
, DWORD len
)
998 const TT_TABLE_DIRECTORY
*tblDir
;
999 TT_OFFSET_TABLE ttOffsetTable
;
1000 TT_NAME_TABLE_HEADER ttNTHeader
;
1001 TT_NAME_RECORD ttRecord
;
1005 if (sizeof(TT_OFFSET_TABLE
) > size
)
1007 ttOffsetTable
= *(TT_OFFSET_TABLE
*)mem
;
1008 ttOffsetTable
.uNumOfTables
= SWAPWORD(ttOffsetTable
.uNumOfTables
);
1009 ttOffsetTable
.uMajorVersion
= SWAPWORD(ttOffsetTable
.uMajorVersion
);
1010 ttOffsetTable
.uMinorVersion
= SWAPWORD(ttOffsetTable
.uMinorVersion
);
1012 if (ttOffsetTable
.uMajorVersion
!= 1 || ttOffsetTable
.uMinorVersion
!= 0)
1015 pos
= sizeof(ttOffsetTable
);
1016 for (i
= 0; i
< ttOffsetTable
.uNumOfTables
; i
++)
1018 tblDir
= (const TT_TABLE_DIRECTORY
*)&mem
[pos
];
1019 pos
+= sizeof(*tblDir
);
1020 if (memcmp(tblDir
->szTag
,"name",4)==0)
1022 ofs
= SWAPLONG(tblDir
->uOffset
);
1026 if (i
>= ttOffsetTable
.uNumOfTables
)
1029 pos
= ofs
+ sizeof(ttNTHeader
);
1032 ttNTHeader
= *(TT_NAME_TABLE_HEADER
*)&mem
[ofs
];
1033 ttNTHeader
.uNRCount
= SWAPWORD(ttNTHeader
.uNRCount
);
1034 ttNTHeader
.uStorageOffset
= SWAPWORD(ttNTHeader
.uStorageOffset
);
1035 for(i
=0; i
<ttNTHeader
.uNRCount
; i
++)
1037 ttRecord
= *(TT_NAME_RECORD
*)&mem
[pos
];
1038 pos
+= sizeof(ttRecord
);
1042 ttRecord
.uNameID
= SWAPWORD(ttRecord
.uNameID
);
1043 if (ttRecord
.uNameID
== id
)
1047 ttRecord
.uStringLength
= SWAPWORD(ttRecord
.uStringLength
);
1048 ttRecord
.uStringOffset
= SWAPWORD(ttRecord
.uStringOffset
);
1049 if (ofs
+ ttRecord
.uStringOffset
+ ttNTHeader
.uStorageOffset
+ ttRecord
.uStringLength
> size
)
1051 buf
= mem
+ ofs
+ ttRecord
.uStringOffset
+ ttNTHeader
.uStorageOffset
;
1052 len
= MultiByteToWideChar(CP_ACP
, 0, buf
, ttRecord
.uStringLength
, ret
, len
-1);
1060 static INT CALLBACK
add_font_proc(const LOGFONTW
*lfw
, const TEXTMETRICW
*ntm
, DWORD type
, LPARAM lParam
);
1062 /*****************************************************************************
1063 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1065 GpStatus WINGDIPAPI
GdipPrivateAddMemoryFont(GpFontCollection
* fontCollection
,
1066 GDIPCONST
void* memory
, INT length
)
1068 WCHAR buf
[32], *name
;
1071 TRACE("%p, %p, %d\n", fontCollection
, memory
, length
);
1073 if (!fontCollection
|| !memory
|| !length
)
1074 return InvalidParameter
;
1076 name
= load_ttf_name_id(memory
, length
, NAME_ID_FULL_FONT_NAME
, buf
, sizeof(buf
)/sizeof(*buf
));
1080 font
= AddFontMemResourceEx((void*)memory
, length
, NULL
, &count
);
1081 TRACE("%s: %p/%u\n", debugstr_w(name
), font
, count
);
1082 if (!font
|| !count
)
1083 return InvalidParameter
;
1092 lfw
.lfCharSet
= DEFAULT_CHARSET
;
1093 lstrcpyW(lfw
.lfFaceName
, name
);
1094 lfw
.lfPitchAndFamily
= 0;
1096 if (!EnumFontFamiliesExW(hdc
, &lfw
, add_font_proc
, (LPARAM
)fontCollection
, 0))
1107 /*****************************************************************************
1108 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1110 GpStatus WINGDIPAPI
GdipGetFontCollectionFamilyCount(
1111 GpFontCollection
* fontCollection
, INT
* numFound
)
1113 TRACE("%p, %p\n", fontCollection
, numFound
);
1115 if (!(fontCollection
&& numFound
))
1116 return InvalidParameter
;
1118 *numFound
= fontCollection
->count
;
1122 /*****************************************************************************
1123 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1125 GpStatus WINGDIPAPI
GdipGetFontCollectionFamilyList(
1126 GpFontCollection
* fontCollection
, INT numSought
,
1127 GpFontFamily
* gpfamilies
[], INT
* numFound
)
1132 TRACE("%p, %d, %p, %p\n", fontCollection
, numSought
, gpfamilies
, numFound
);
1134 if (!(fontCollection
&& gpfamilies
&& numFound
))
1135 return InvalidParameter
;
1137 memset(gpfamilies
, 0, sizeof(*gpfamilies
) * numSought
);
1139 for (i
= 0; i
< numSought
&& i
< fontCollection
->count
&& stat
== Ok
; i
++)
1141 stat
= GdipCloneFontFamily(fontCollection
->FontFamilies
[i
], &gpfamilies
[i
]);
1149 for (i
=0; i
<numToFree
; i
++)
1151 GdipDeleteFontFamily(gpfamilies
[i
]);
1152 gpfamilies
[i
] = NULL
;
1159 void free_installed_fonts(void)
1161 while (installedFontCollection
.count
)
1162 GdipDeleteFontFamily(installedFontCollection
.FontFamilies
[--installedFontCollection
.count
]);
1163 HeapFree(GetProcessHeap(), 0, installedFontCollection
.FontFamilies
);
1164 installedFontCollection
.FontFamilies
= NULL
;
1165 installedFontCollection
.allocated
= 0;
1168 static INT CALLBACK
add_font_proc(const LOGFONTW
*lfw
, const TEXTMETRICW
*ntm
,
1169 DWORD type
, LPARAM lParam
)
1171 GpFontCollection
* fonts
= (GpFontCollection
*)lParam
;
1174 if (type
== RASTER_FONTTYPE
)
1177 /* skip duplicates */
1178 for (i
=0; i
<fonts
->count
; i
++)
1179 if (strcmpiW(lfw
->lfFaceName
, fonts
->FontFamilies
[i
]->FamilyName
) == 0)
1182 if (fonts
->allocated
== fonts
->count
)
1184 INT new_alloc_count
= fonts
->allocated
+50;
1185 GpFontFamily
** new_family_list
= HeapAlloc(GetProcessHeap(), 0, new_alloc_count
*sizeof(void*));
1187 if (!new_family_list
)
1190 memcpy(new_family_list
, fonts
->FontFamilies
, fonts
->count
*sizeof(void*));
1191 HeapFree(GetProcessHeap(), 0, fonts
->FontFamilies
);
1192 fonts
->FontFamilies
= new_family_list
;
1193 fonts
->allocated
= new_alloc_count
;
1196 if (GdipCreateFontFamilyFromName(lfw
->lfFaceName
, NULL
, &fonts
->FontFamilies
[fonts
->count
]) == Ok
)
1204 GpStatus WINGDIPAPI
GdipNewInstalledFontCollection(
1205 GpFontCollection
** fontCollection
)
1207 TRACE("(%p)\n",fontCollection
);
1209 if (!fontCollection
)
1210 return InvalidParameter
;
1212 if (installedFontCollection
.count
== 0)
1219 lfw
.lfCharSet
= DEFAULT_CHARSET
;
1220 lfw
.lfFaceName
[0] = 0;
1221 lfw
.lfPitchAndFamily
= 0;
1223 if (!EnumFontFamiliesExW(hdc
, &lfw
, add_font_proc
, (LPARAM
)&installedFontCollection
, 0))
1225 free_installed_fonts();
1233 *fontCollection
= &installedFontCollection
;