makefiles: Add support for Automake-style silent make rules.
[wine.git] / dlls / gdiplus / font.c
blob05663b71aad8678bda79b438d25322fb58a5c19c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
31 #include "objbase.h"
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
36 /* PANOSE is 10 bytes in size, need to pack the structure properly */
37 #include "pshpack2.h"
38 typedef struct
40 USHORT version;
41 SHORT xAvgCharWidth;
42 USHORT usWeightClass;
43 USHORT usWidthClass;
44 SHORT fsType;
45 SHORT ySubscriptXSize;
46 SHORT ySubscriptYSize;
47 SHORT ySubscriptXOffset;
48 SHORT ySubscriptYOffset;
49 SHORT ySuperscriptXSize;
50 SHORT ySuperscriptYSize;
51 SHORT ySuperscriptXOffset;
52 SHORT ySuperscriptYOffset;
53 SHORT yStrikeoutSize;
54 SHORT yStrikeoutPosition;
55 SHORT sFamilyClass;
56 PANOSE panose;
57 ULONG ulUnicodeRange1;
58 ULONG ulUnicodeRange2;
59 ULONG ulUnicodeRange3;
60 ULONG ulUnicodeRange4;
61 CHAR achVendID[4];
62 USHORT fsSelection;
63 USHORT usFirstCharIndex;
64 USHORT usLastCharIndex;
65 /* According to the Apple spec, original version didn't have the below fields,
66 * version numbers were taken from the OpenType spec.
68 /* version 0 (TrueType 1.5) */
69 USHORT sTypoAscender;
70 USHORT sTypoDescender;
71 USHORT sTypoLineGap;
72 USHORT usWinAscent;
73 USHORT usWinDescent;
74 /* version 1 (TrueType 1.66) */
75 ULONG ulCodePageRange1;
76 ULONG ulCodePageRange2;
77 /* version 2 (OpenType 1.2) */
78 SHORT sxHeight;
79 SHORT sCapHeight;
80 USHORT usDefaultChar;
81 USHORT usBreakChar;
82 USHORT usMaxContext;
83 } TT_OS2_V2;
85 typedef struct
87 ULONG Version;
88 SHORT Ascender;
89 SHORT Descender;
90 SHORT LineGap;
91 USHORT advanceWidthMax;
92 SHORT minLeftSideBearing;
93 SHORT minRightSideBearing;
94 SHORT xMaxExtent;
95 SHORT caretSlopeRise;
96 SHORT caretSlopeRun;
97 SHORT caretOffset;
98 SHORT reserved[4];
99 SHORT metricDataFormat;
100 USHORT numberOfHMetrics;
101 } TT_HHEA;
102 #include "poppack.h"
104 #ifdef WORDS_BIGENDIAN
105 #define GET_BE_WORD(x) (x)
106 #define GET_BE_DWORD(x) (x)
107 #else
108 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
109 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
110 #endif
112 #define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
113 ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
114 ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
115 #define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
116 #define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
118 static GpFontCollection installedFontCollection = {0};
120 static CRITICAL_SECTION font_cs;
121 static CRITICAL_SECTION_DEBUG critsect_debug =
123 0, 0, &font_cs,
124 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
125 0, 0, { (DWORD_PTR)(__FILE__ ": font_cs") }
127 static CRITICAL_SECTION font_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
129 /*******************************************************************************
130 * GdipCreateFont [GDIPLUS.@]
132 * Create a new font based off of a FontFamily
134 * PARAMS
135 * *fontFamily [I] Family to base the font off of
136 * emSize [I] Size of the font
137 * style [I] Bitwise OR of FontStyle enumeration
138 * unit [I] Unit emSize is measured in
139 * **font [I] the resulting Font object
141 * RETURNS
142 * SUCCESS: Ok
143 * FAILURE: InvalidParameter if fontfamily or font is NULL.
144 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
146 * NOTES
147 * UnitDisplay is unsupported.
148 * emSize is stored separately from lfHeight, to hold the fraction.
150 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
151 REAL emSize, INT style, Unit unit, GpFont **font)
153 HFONT hfont;
154 OUTLINETEXTMETRICW otm;
155 LOGFONTW lfw;
156 HDC hdc;
157 GpStatus stat;
158 int ret;
160 if (!fontFamily || !font || emSize < 0.0)
161 return InvalidParameter;
163 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
164 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
166 memset(&lfw, 0, sizeof(lfw));
168 stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
169 if (stat != Ok) return stat;
171 lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
172 lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
173 lfw.lfItalic = style & FontStyleItalic;
174 lfw.lfUnderline = style & FontStyleUnderline;
175 lfw.lfStrikeOut = style & FontStyleStrikeout;
177 hfont = CreateFontIndirectW(&lfw);
178 hdc = CreateCompatibleDC(0);
179 SelectObject(hdc, hfont);
180 otm.otmSize = sizeof(otm);
181 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
182 DeleteDC(hdc);
183 DeleteObject(hfont);
185 if (!ret) return NotTrueTypeFont;
187 *font = heap_alloc_zero(sizeof(GpFont));
188 if (!*font) return OutOfMemory;
190 (*font)->unit = unit;
191 (*font)->emSize = emSize;
192 (*font)->otm = otm;
193 (*font)->family = (GpFontFamily *)fontFamily;
195 TRACE("<-- %p\n", *font);
197 return Ok;
200 /*******************************************************************************
201 * GdipCreateFontFromLogfontW [GDIPLUS.@]
203 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
204 GDIPCONST LOGFONTW *logfont, GpFont **font)
206 HFONT hfont, oldfont;
207 OUTLINETEXTMETRICW otm;
208 WCHAR facename[LF_FACESIZE];
209 GpStatus stat;
210 int ret;
212 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
214 if (!hdc || !logfont || !font)
215 return InvalidParameter;
217 hfont = CreateFontIndirectW(logfont);
218 oldfont = SelectObject(hdc, hfont);
219 otm.otmSize = sizeof(otm);
220 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
221 GetTextFaceW(hdc, LF_FACESIZE, facename);
222 SelectObject(hdc, oldfont);
223 DeleteObject(hfont);
225 if (!ret) return NotTrueTypeFont;
227 *font = heap_alloc_zero(sizeof(GpFont));
228 if (!*font) return OutOfMemory;
230 (*font)->unit = UnitWorld;
231 (*font)->emSize = otm.otmTextMetrics.tmHeight - otm.otmTextMetrics.tmInternalLeading;
232 (*font)->otm = otm;
234 stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
235 if (stat != Ok)
237 heap_free(*font);
238 return NotTrueTypeFont;
241 TRACE("<-- %p\n", *font);
243 return Ok;
246 /*******************************************************************************
247 * GdipCreateFontFromLogfontA [GDIPLUS.@]
249 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
250 GDIPCONST LOGFONTA *lfa, GpFont **font)
252 LOGFONTW lfw;
254 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
256 if(!lfa || !font)
257 return InvalidParameter;
259 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
261 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
262 return GenericError;
264 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
267 /*******************************************************************************
268 * GdipDeleteFont [GDIPLUS.@]
270 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
272 TRACE("(%p)\n", font);
274 if(!font)
275 return InvalidParameter;
277 GdipDeleteFontFamily(font->family);
278 heap_free(font);
280 return Ok;
283 /*******************************************************************************
284 * GdipCreateFontFromDC [GDIPLUS.@]
286 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
288 HFONT hfont;
289 LOGFONTW lfw;
291 TRACE("(%p, %p)\n", hdc, font);
293 if(!font)
294 return InvalidParameter;
296 hfont = GetCurrentObject(hdc, OBJ_FONT);
297 if(!hfont)
298 return GenericError;
300 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
301 return GenericError;
303 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
306 /*******************************************************************************
307 * GdipGetFamily [GDIPLUS.@]
309 * Returns the FontFamily for the specified Font
311 * PARAMS
312 * font [I] Font to request from
313 * family [O] Resulting FontFamily object
315 * RETURNS
316 * SUCCESS: Ok
317 * FAILURE: An element of GpStatus
319 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
321 TRACE("%p %p\n", font, family);
323 if (!(font && family))
324 return InvalidParameter;
326 *family = font->family;
327 return Ok;
330 static REAL get_font_size(const GpFont *font)
332 return font->emSize;
335 /******************************************************************************
336 * GdipGetFontSize [GDIPLUS.@]
338 * Returns the size of the font in Units
340 * PARAMS
341 * *font [I] The font to retrieve size from
342 * *size [O] Pointer to hold retrieved value
344 * RETURNS
345 * SUCCESS: Ok
346 * FAILURE: InvalidParameter (font or size was NULL)
348 * NOTES
349 * Size returned is actually emSize -- not internal size used for drawing.
351 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
353 TRACE("(%p, %p)\n", font, size);
355 if (!(font && size)) return InvalidParameter;
357 *size = get_font_size(font);
358 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
360 return Ok;
363 static INT get_font_style(const GpFont *font)
365 INT style;
367 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
368 style = FontStyleBold;
369 else
370 style = FontStyleRegular;
371 if (font->otm.otmTextMetrics.tmItalic)
372 style |= FontStyleItalic;
373 if (font->otm.otmTextMetrics.tmUnderlined)
374 style |= FontStyleUnderline;
375 if (font->otm.otmTextMetrics.tmStruckOut)
376 style |= FontStyleStrikeout;
378 return style;
381 /*******************************************************************************
382 * GdipGetFontStyle [GDIPLUS.@]
384 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
386 * PARAMS
387 * font [I] font to request from
388 * style [O] resulting pointer to a FontStyle enumeration
390 * RETURNS
391 * SUCCESS: Ok
392 * FAILURE: InvalidParameter
394 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
396 TRACE("%p %p\n", font, style);
398 if (!(font && style))
399 return InvalidParameter;
401 *style = get_font_style(font);
402 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
404 return Ok;
407 /*******************************************************************************
408 * GdipGetFontUnit [GDIPLUS.@]
410 * PARAMS
411 * font [I] Font to retrieve from
412 * unit [O] Return value
414 * RETURNS
415 * FAILURE: font or unit was NULL
416 * OK: otherwise
418 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
420 TRACE("(%p, %p)\n", font, unit);
422 if (!(font && unit)) return InvalidParameter;
424 *unit = font->unit;
425 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
427 return Ok;
430 /*******************************************************************************
431 * GdipGetLogFontA [GDIPLUS.@]
433 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
434 LOGFONTA *lfa)
436 GpStatus status;
437 LOGFONTW lfw;
439 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
441 status = GdipGetLogFontW(font, graphics, &lfw);
442 if(status != Ok)
443 return status;
445 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
447 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
448 return GenericError;
450 return Ok;
453 /*******************************************************************************
454 * GdipGetLogFontW [GDIPLUS.@]
456 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
458 REAL angle, rel_height, height;
459 GpMatrix matrix;
460 GpPointF pt[3];
462 TRACE("(%p, %p, %p)\n", font, graphics, lf);
464 if (!font || !graphics || !lf)
465 return InvalidParameter;
467 matrix = graphics->worldtrans;
469 if (font->unit == UnitPixel || font->unit == UnitWorld)
471 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
472 if (graphics->unit != UnitDisplay)
473 GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
475 else
477 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
478 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
479 else
480 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
483 pt[0].X = 0.0;
484 pt[0].Y = 0.0;
485 pt[1].X = 1.0;
486 pt[1].Y = 0.0;
487 pt[2].X = 0.0;
488 pt[2].Y = 1.0;
489 GdipTransformMatrixPoints(&matrix, pt, 3);
490 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
491 rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
492 (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
494 lf->lfHeight = -gdip_round(height * rel_height);
495 lf->lfWidth = 0;
496 lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
497 if (lf->lfEscapement < 0)
499 lf->lfEscapement += 3600;
500 lf->lfOrientation += 3600;
502 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
503 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
504 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
505 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
506 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
507 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
508 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
509 lf->lfQuality = DEFAULT_QUALITY;
510 lf->lfPitchAndFamily = 0;
511 lstrcpyW(lf->lfFaceName, font->family->FamilyName);
513 TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
515 return Ok;
518 /*******************************************************************************
519 * GdipCloneFont [GDIPLUS.@]
521 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
523 TRACE("(%p, %p)\n", font, cloneFont);
525 if(!font || !cloneFont)
526 return InvalidParameter;
528 *cloneFont = heap_alloc_zero(sizeof(GpFont));
529 if(!*cloneFont) return OutOfMemory;
531 **cloneFont = *font;
532 return Ok;
535 /*******************************************************************************
536 * GdipGetFontHeight [GDIPLUS.@]
537 * PARAMS
538 * font [I] Font to retrieve height from
539 * graphics [I] The current graphics context
540 * height [O] Resulting height
541 * RETURNS
542 * SUCCESS: Ok
543 * FAILURE: Another element of GpStatus
545 * NOTES
546 * Forwards to GdipGetFontHeightGivenDPI
548 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
549 GDIPCONST GpGraphics *graphics, REAL *height)
551 REAL dpi;
552 GpStatus stat;
553 REAL font_height;
555 TRACE("%p %p %p\n", font, graphics, height);
557 if (!font || !height) return InvalidParameter;
559 stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
560 if (stat != Ok) return stat;
562 if (!graphics)
564 *height = font_height;
565 TRACE("%s,%d => %f\n",
566 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
567 return Ok;
570 stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
571 if (stat != Ok) return stat;
573 *height = pixels_to_units(font_height, graphics->unit, dpi);
575 TRACE("%s,%d(unit %d) => %f\n",
576 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
577 return Ok;
580 /*******************************************************************************
581 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
582 * PARAMS
583 * font [I] Font to retrieve DPI from
584 * dpi [I] DPI to assume
585 * height [O] Return value
587 * RETURNS
588 * SUCCESS: Ok
589 * FAILURE: InvalidParameter if font or height is NULL
591 * NOTES
592 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
593 * (for anything other than unit Pixel)
595 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
597 GpStatus stat;
598 INT style;
599 UINT16 line_spacing, em_height;
600 REAL font_size;
602 if (!font || !height) return InvalidParameter;
604 TRACE("%p (%s), %f, %p\n", font,
605 debugstr_w(font->family->FamilyName), dpi, height);
607 font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
608 style = get_font_style(font);
609 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
610 if (stat != Ok) return stat;
611 stat = GdipGetEmHeight(font->family, style, &em_height);
612 if (stat != Ok) return stat;
614 *height = (REAL)line_spacing * font_size / (REAL)em_height;
616 TRACE("%s,%d => %f\n",
617 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
619 return Ok;
622 /***********************************************************************
623 * Borrowed from GDI32:
625 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
626 * We have to use other types because of the FONTENUMPROCW definition.
628 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
629 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
631 const ENUMLOGFONTW *elfW = (const ENUMLOGFONTW *)elf;
632 LOGFONTW *lf = (LOGFONTW *)lParam;
634 if (type & RASTER_FONTTYPE)
635 return 1;
637 *lf = *elf;
638 /* replace substituted font name by a real one */
639 lstrcpynW(lf->lfFaceName, elfW->elfFullName, LF_FACESIZE);
640 return 0;
643 struct font_metrics
645 WCHAR facename[LF_FACESIZE];
646 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
647 int dpi;
650 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
652 OUTLINETEXTMETRICW otm;
653 TT_OS2_V2 tt_os2;
654 TT_HHEA tt_hori;
655 LONG size;
656 UINT16 line_gap;
658 otm.otmSize = sizeof(otm);
659 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
661 fm->em_height = otm.otmEMSquare;
662 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
664 memset(&tt_hori, 0, sizeof(tt_hori));
665 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
667 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
668 fm->descent = -GET_BE_WORD(tt_hori.Descender);
669 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
670 line_gap = GET_BE_WORD(tt_hori.LineGap);
671 fm->line_spacing = fm->ascent + fm->descent + line_gap;
672 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
673 if (fm->ascent + fm->descent != 0) return TRUE;
676 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
677 if (size == GDI_ERROR) return FALSE;
679 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
681 memset(&tt_os2, 0, sizeof(tt_os2));
682 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
684 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
685 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
686 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
687 if (fm->ascent + fm->descent == 0)
689 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
690 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
691 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
693 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
694 fm->line_spacing = fm->ascent + fm->descent + line_gap;
695 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
696 return TRUE;
699 /*******************************************************************************
700 * GdipCreateFontFamilyFromName [GDIPLUS.@]
702 * Creates a font family object based on a supplied name
704 * PARAMS
705 * name [I] Name of the font
706 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
707 * FontFamily [O] Pointer to the resulting FontFamily object
709 * RETURNS
710 * SUCCESS: Ok
711 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
712 * FAILURE: Invalid parameter if FontFamily or name is NULL
714 * NOTES
715 * If fontCollection is NULL then the object is not part of any collection
719 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
720 GpFontCollection *collection,
721 GpFontFamily **family)
723 HDC hdc;
724 LOGFONTW lf;
725 GpStatus status;
726 int i;
728 TRACE("%s, %p %p\n", debugstr_w(name), collection, family);
730 if (!name || !family)
731 return InvalidParameter;
733 if (!collection)
735 status = GdipNewInstalledFontCollection(&collection);
736 if (status != Ok) return status;
739 status = FontFamilyNotFound;
741 hdc = CreateCompatibleDC(0);
743 if (!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
745 for (i = 0; i < collection->count; i++)
747 if (!wcsicmp(lf.lfFaceName, collection->FontFamilies[i]->FamilyName))
749 *family = collection->FontFamilies[i];
750 TRACE("<-- %p\n", *family);
751 status = Ok;
752 break;
757 DeleteDC(hdc);
758 return status;
761 /*******************************************************************************
762 * GdipCloneFontFamily [GDIPLUS.@]
764 * Creates a deep copy of a Font Family object
766 * PARAMS
767 * FontFamily [I] Font to clone
768 * clonedFontFamily [O] The resulting cloned font
770 * RETURNS
771 * SUCCESS: Ok
773 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily *family, GpFontFamily **clone)
775 if (!family || !clone)
776 return InvalidParameter;
778 TRACE("%p (%s), %p\n", family, debugstr_w(family->FamilyName), clone);
780 *clone = family;
781 return Ok;
784 /*******************************************************************************
785 * GdipGetFamilyName [GDIPLUS.@]
787 * Returns the family name into name
789 * PARAMS
790 * *family [I] Family to retrieve from
791 * *name [O] WCHARS of the family name
792 * LANGID [I] charset
794 * RETURNS
795 * SUCCESS: Ok
796 * FAILURE: InvalidParameter if family is NULL
798 * NOTES
799 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
801 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
802 WCHAR *name, LANGID language)
804 static int lang_fixme;
806 if (family == NULL)
807 return InvalidParameter;
809 TRACE("%p, %p, %d\n", family, name, language);
811 if (language != LANG_NEUTRAL && !lang_fixme++)
812 FIXME("No support for handling of multiple languages!\n");
814 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
816 return Ok;
820 /*****************************************************************************
821 * GdipDeleteFontFamily [GDIPLUS.@]
823 * Removes the specified FontFamily
825 * PARAMS
826 * *FontFamily [I] The family to delete
828 * RETURNS
829 * SUCCESS: Ok
830 * FAILURE: InvalidParameter if FontFamily is NULL.
833 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
835 if (!FontFamily)
836 return InvalidParameter;
838 return Ok;
841 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
842 INT style, UINT16* CellAscent)
844 if (!(family && CellAscent)) return InvalidParameter;
846 *CellAscent = family->ascent;
847 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
849 return Ok;
852 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
853 INT style, UINT16* CellDescent)
855 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
857 if (!(family && CellDescent)) return InvalidParameter;
859 *CellDescent = family->descent;
860 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
862 return Ok;
865 /*******************************************************************************
866 * GdipGetEmHeight [GDIPLUS.@]
868 * Gets the height of the specified family in EmHeights
870 * PARAMS
871 * family [I] Family to retrieve from
872 * style [I] (optional) style
873 * EmHeight [O] return value
875 * RETURNS
876 * SUCCESS: Ok
877 * FAILURE: InvalidParameter
879 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
881 if (!(family && EmHeight)) return InvalidParameter;
883 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
885 *EmHeight = family->em_height;
886 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
888 return Ok;
892 /*******************************************************************************
893 * GdipGetLineSpacing [GDIPLUS.@]
895 * Returns the line spacing in design units
897 * PARAMS
898 * family [I] Family to retrieve from
899 * style [I] (Optional) font style
900 * LineSpacing [O] Return value
902 * RETURNS
903 * SUCCESS: Ok
904 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
906 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
907 INT style, UINT16* LineSpacing)
909 TRACE("%p, %d, %p\n", family, style, LineSpacing);
911 if (!(family && LineSpacing))
912 return InvalidParameter;
914 if (style) FIXME("ignoring style\n");
916 *LineSpacing = family->line_spacing;
917 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
919 return Ok;
922 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
923 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
925 INT fontstyle = FontStyleRegular;
927 if (!ntm) return 1;
929 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
930 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
931 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
932 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
934 return (INT)lParam != fontstyle;
937 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
938 INT style, BOOL* IsStyleAvailable)
940 HDC hdc;
942 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
944 if (!(family && IsStyleAvailable))
945 return InvalidParameter;
947 *IsStyleAvailable = FALSE;
949 hdc = CreateCompatibleDC(0);
951 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
952 *IsStyleAvailable = TRUE;
954 DeleteDC(hdc);
956 return Ok;
959 /*****************************************************************************
960 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
962 * Obtains a serif family (Courier New on Windows)
964 * PARAMS
965 * **nativeFamily [I] Where the font will be stored
967 * RETURNS
968 * InvalidParameter if nativeFamily is NULL.
969 * Ok otherwise.
971 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
973 GpStatus stat;
975 if (nativeFamily == NULL) return InvalidParameter;
977 stat = GdipCreateFontFamilyFromName(L"Courier New", NULL, nativeFamily);
979 if (stat == FontFamilyNotFound)
980 stat = GdipCreateFontFamilyFromName(L"Liberation Mono", NULL, nativeFamily);
982 if (stat == FontFamilyNotFound)
983 ERR("Missing 'Courier New' font\n");
985 return stat;
988 /*****************************************************************************
989 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
991 * Obtains a serif family (Times New Roman on Windows)
993 * PARAMS
994 * **nativeFamily [I] Where the font will be stored
996 * RETURNS
997 * InvalidParameter if nativeFamily is NULL.
998 * Ok otherwise.
1000 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1002 GpStatus stat;
1004 TRACE("(%p)\n", nativeFamily);
1006 if (nativeFamily == NULL) return InvalidParameter;
1008 stat = GdipCreateFontFamilyFromName(L"Times New Roman", NULL, nativeFamily);
1010 if (stat == FontFamilyNotFound)
1011 stat = GdipCreateFontFamilyFromName(L"Liberation Serif", NULL, nativeFamily);
1013 if (stat == FontFamilyNotFound)
1014 ERR("Missing 'Times New Roman' font\n");
1016 return stat;
1019 /*****************************************************************************
1020 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1022 * Obtains a serif family (Microsoft Sans Serif on Windows)
1024 * PARAMS
1025 * **nativeFamily [I] Where the font will be stored
1027 * RETURNS
1028 * InvalidParameter if nativeFamily is NULL.
1029 * Ok otherwise.
1031 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1033 GpStatus stat;
1035 TRACE("(%p)\n", nativeFamily);
1037 if (nativeFamily == NULL) return InvalidParameter;
1039 stat = GdipCreateFontFamilyFromName(L"Microsoft Sans Serif", NULL, nativeFamily);
1041 if (stat == FontFamilyNotFound)
1042 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1043 stat = GdipCreateFontFamilyFromName(L"Tahoma", NULL, nativeFamily);
1045 return stat;
1048 /*****************************************************************************
1049 * GdipNewPrivateFontCollection [GDIPLUS.@]
1051 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1053 TRACE("%p\n", fontCollection);
1055 if (!fontCollection)
1056 return InvalidParameter;
1058 *fontCollection = heap_alloc_zero(sizeof(GpFontCollection));
1059 if (!*fontCollection) return OutOfMemory;
1061 (*fontCollection)->FontFamilies = NULL;
1062 (*fontCollection)->count = 0;
1063 (*fontCollection)->allocated = 0;
1065 TRACE("<-- %p\n", *fontCollection);
1067 return Ok;
1070 /*****************************************************************************
1071 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1073 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1075 INT i;
1077 TRACE("%p\n", fontCollection);
1079 if (!fontCollection)
1080 return InvalidParameter;
1082 for (i = 0; i < (*fontCollection)->count; i++) heap_free((*fontCollection)->FontFamilies[i]);
1083 heap_free((*fontCollection)->FontFamilies);
1084 heap_free(*fontCollection);
1086 return Ok;
1089 /*****************************************************************************
1090 * GdipPrivateAddFontFile [GDIPLUS.@]
1092 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1094 HANDLE file, mapping;
1095 LARGE_INTEGER size;
1096 void *mem;
1097 GpStatus status;
1099 TRACE("%p, %s\n", collection, debugstr_w(name));
1101 if (!collection || !name) return InvalidParameter;
1103 file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1104 if (file == INVALID_HANDLE_VALUE) return InvalidParameter;
1106 if (!GetFileSizeEx(file, &size) || size.u.HighPart)
1108 CloseHandle(file);
1109 return InvalidParameter;
1112 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
1113 CloseHandle(file);
1114 if (!mapping) return InvalidParameter;
1116 mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1117 CloseHandle(mapping);
1118 if (!mem) return InvalidParameter;
1120 /* GdipPrivateAddMemoryFont creates a copy of the memory block */
1121 status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
1122 UnmapViewOfFile(mem);
1124 return status;
1127 #define TT_PLATFORM_APPLE_UNICODE 0
1128 #define TT_PLATFORM_MACINTOSH 1
1129 #define TT_PLATFORM_MICROSOFT 3
1131 #define TT_APPLE_ID_DEFAULT 0
1132 #define TT_APPLE_ID_ISO_10646 2
1133 #define TT_APPLE_ID_UNICODE_2_0 3
1135 #define TT_MS_ID_SYMBOL_CS 0
1136 #define TT_MS_ID_UNICODE_CS 1
1138 #define TT_MAC_ID_SIMPLIFIED_CHINESE 25
1140 #define NAME_ID_FULL_FONT_NAME 4
1142 typedef struct {
1143 USHORT major_version;
1144 USHORT minor_version;
1145 USHORT tables_no;
1146 USHORT search_range;
1147 USHORT entry_selector;
1148 USHORT range_shift;
1149 } tt_header;
1151 typedef struct {
1152 char tag[4]; /* table name */
1153 ULONG check_sum; /* Check sum */
1154 ULONG offset; /* Offset from beginning of file */
1155 ULONG length; /* length of the table in bytes */
1156 } tt_table_directory;
1158 typedef struct {
1159 USHORT format; /* format selector. Always 0 */
1160 USHORT count; /* Name Records count */
1161 USHORT string_offset; /* Offset for strings storage, * from start of the table */
1162 } tt_name_table;
1164 typedef struct {
1165 USHORT platform_id;
1166 USHORT encoding_id;
1167 USHORT language_id;
1168 USHORT name_id;
1169 USHORT length;
1170 USHORT offset; /* from start of storage area */
1171 } tt_name_record;
1173 /* Copied from gdi32/freetype.c */
1175 static const LANGID mac_langid_table[] =
1177 MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
1178 MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FRENCH */
1179 MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GERMAN */
1180 MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ITALIAN */
1181 MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DUTCH */
1182 MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWEDISH */
1183 MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SPANISH */
1184 MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DANISH */
1185 MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PORTUGUESE */
1186 MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NORWEGIAN */
1187 MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HEBREW */
1188 MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_JAPANESE */
1189 MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARABIC */
1190 MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FINNISH */
1191 MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREEK */
1192 MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ICELANDIC */
1193 MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALTESE */
1194 MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKISH */
1195 MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CROATIAN */
1196 MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
1197 MAKELANGID(LANG_URDU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_URDU */
1198 MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HINDI */
1199 MAKELANGID(LANG_THAI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_THAI */
1200 MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KOREAN */
1201 MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LITHUANIAN */
1202 MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_POLISH */
1203 MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HUNGARIAN */
1204 MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESTONIAN */
1205 MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LETTISH */
1206 MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SAAMISK */
1207 MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FAEROESE */
1208 MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FARSI */
1209 MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_RUSSIAN */
1210 MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
1211 MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), /* TT_MAC_LANGID_FLEMISH */
1212 MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_IRISH */
1213 MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ALBANIAN */
1214 MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ROMANIAN */
1215 MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CZECH */
1216 MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVAK */
1217 MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVENIAN */
1218 0, /* TT_MAC_LANGID_YIDDISH */
1219 MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SERBIAN */
1220 MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MACEDONIAN */
1221 MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BULGARIAN */
1222 MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UKRAINIAN */
1223 MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BYELORUSSIAN */
1224 MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UZBEK */
1225 MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KAZAKH */
1226 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC), /* TT_MAC_LANGID_AZERBAIJANI */
1227 0, /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
1228 MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARMENIAN */
1229 MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GEORGIAN */
1230 0, /* TT_MAC_LANGID_MOLDAVIAN */
1231 MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KIRGHIZ */
1232 MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAJIKI */
1233 MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKMEN */
1234 MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MONGOLIAN */
1235 MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
1236 MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PASHTO */
1237 0, /* TT_MAC_LANGID_KURDISH */
1238 MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KASHMIRI */
1239 MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINDHI */
1240 MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIBETAN */
1241 MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NEPALI */
1242 MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SANSKRIT */
1243 MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MARATHI */
1244 MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BENGALI */
1245 MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ASSAMESE */
1246 MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GUJARATI */
1247 MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PUNJABI */
1248 MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ORIYA */
1249 MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAYALAM */
1250 MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KANNADA */
1251 MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAMIL */
1252 MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TELUGU */
1253 MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINHALESE */
1254 0, /* TT_MAC_LANGID_BURMESE */
1255 MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KHMER */
1256 MAKELANGID(LANG_LAO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LAO */
1257 MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_VIETNAMESE */
1258 MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INDONESIAN */
1259 0, /* TT_MAC_LANGID_TAGALOG */
1260 MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
1261 0, /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
1262 MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AMHARIC */
1263 MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIGRINYA */
1264 0, /* TT_MAC_LANGID_GALLA */
1265 0, /* TT_MAC_LANGID_SOMALI */
1266 MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWAHILI */
1267 0, /* TT_MAC_LANGID_RUANDA */
1268 0, /* TT_MAC_LANGID_RUNDI */
1269 0, /* TT_MAC_LANGID_CHEWA */
1270 MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAGASY */
1271 MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESPERANTO */
1272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 95-111 */
1273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112-127 */
1274 MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_WELSH */
1275 MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BASQUE */
1276 MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CATALAN */
1277 0, /* TT_MAC_LANGID_LATIN */
1278 MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_QUECHUA */
1279 0, /* TT_MAC_LANGID_GUARANI */
1280 0, /* TT_MAC_LANGID_AYMARA */
1281 MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TATAR */
1282 MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UIGHUR */
1283 0, /* TT_MAC_LANGID_DZONGKHA */
1284 0, /* TT_MAC_LANGID_JAVANESE */
1285 0, /* TT_MAC_LANGID_SUNDANESE */
1286 MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GALICIAN */
1287 MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AFRIKAANS */
1288 MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BRETON */
1289 MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INUKTITUT */
1290 MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SCOTTISH_GAELIC */
1291 MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MANX_GAELIC */
1292 MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND), /* TT_MAC_LANGID_IRISH_GAELIC */
1293 0, /* TT_MAC_LANGID_TONGAN */
1294 0, /* TT_MAC_LANGID_GREEK_POLYTONIC */
1295 MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREELANDIC */
1296 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN), /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
1299 static inline WORD get_mac_code_page( const tt_name_record *name )
1301 WORD encoding_id = GET_BE_WORD(name->encoding_id);
1302 if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008; /* special case */
1303 return 10000 + encoding_id;
1306 static int match_name_table_language( const tt_name_record *name, LANGID lang )
1308 LANGID name_lang;
1310 switch (GET_BE_WORD(name->platform_id))
1312 case TT_PLATFORM_MICROSOFT:
1313 switch (GET_BE_WORD(name->encoding_id))
1315 case TT_MS_ID_UNICODE_CS:
1316 case TT_MS_ID_SYMBOL_CS:
1317 name_lang = GET_BE_WORD(name->language_id);
1318 break;
1319 default:
1320 return 0;
1322 break;
1323 case TT_PLATFORM_MACINTOSH:
1324 if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
1325 name_lang = GET_BE_WORD(name->language_id);
1326 if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1327 name_lang = mac_langid_table[name_lang];
1328 break;
1329 case TT_PLATFORM_APPLE_UNICODE:
1330 switch (GET_BE_WORD(name->encoding_id))
1332 case TT_APPLE_ID_DEFAULT:
1333 case TT_APPLE_ID_ISO_10646:
1334 case TT_APPLE_ID_UNICODE_2_0:
1335 name_lang = GET_BE_WORD(name->language_id);
1336 if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1337 name_lang = mac_langid_table[name_lang];
1338 break;
1339 default:
1340 return 0;
1342 break;
1343 default:
1344 return 0;
1346 if (name_lang == lang) return 3;
1347 if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
1348 if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
1349 return 0;
1352 static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data )
1354 WORD name_len = GET_BE_WORD(name->length);
1355 WORD codepage;
1356 WCHAR *ret;
1357 int len;
1359 switch (GET_BE_WORD(name->platform_id))
1361 case TT_PLATFORM_APPLE_UNICODE:
1362 case TT_PLATFORM_MICROSOFT:
1363 ret = heap_alloc((name_len / 2 + 1) * sizeof(WCHAR));
1364 for (len = 0; len < name_len / 2; len++)
1365 ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
1366 ret[len] = 0;
1367 return ret;
1368 case TT_PLATFORM_MACINTOSH:
1369 codepage = get_mac_code_page( name );
1370 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, NULL, 0 ) + 1;
1371 if (!len)
1372 return NULL;
1373 ret = heap_alloc(len * sizeof(WCHAR));
1374 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len - 1 );
1375 ret[len] = 0;
1376 return ret;
1378 return NULL;
1381 static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id )
1383 LANGID lang = GetSystemDefaultLangID();
1384 const tt_header *header;
1385 const tt_name_table *name_table;
1386 const tt_name_record *name_record;
1387 DWORD pos, ofs, count;
1388 int i, res, best_lang = 0, best_index = -1;
1390 if (sizeof(tt_header) > size)
1391 return NULL;
1392 header = (const tt_header*)mem;
1393 count = GET_BE_WORD(header->tables_no);
1395 if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1396 return NULL;
1398 pos = sizeof(*header);
1399 for (i = 0; i < count; i++)
1401 const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
1402 pos += sizeof(*table_directory);
1403 if (memcmp(table_directory->tag, "name", 4) == 0)
1405 ofs = GET_BE_DWORD(table_directory->offset);
1406 break;
1409 if (i >= count)
1410 return NULL;
1412 if (ofs >= size)
1413 return NULL;
1414 pos = ofs + sizeof(*name_table);
1415 if (pos > size)
1416 return NULL;
1417 name_table = (const tt_name_table*)&mem[ofs];
1418 count = GET_BE_WORD(name_table->count);
1419 if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
1420 ofs += GET_BE_WORD(name_table->string_offset);
1421 for (i=0; i<count; i++)
1423 name_record = (const tt_name_record*)&mem[pos];
1424 pos += sizeof(*name_record);
1425 if (pos > size)
1426 return NULL;
1428 if (GET_BE_WORD(name_record->name_id) != id) continue;
1429 if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
1430 if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;
1432 res = match_name_table_language( name_record, lang );
1433 if (res > best_lang)
1435 best_lang = res;
1436 best_index = i;
1440 if (best_lang)
1442 WCHAR *ret;
1443 name_record = (const tt_name_record*)(name_table + 1) + best_index;
1444 ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset) );
1445 TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
1446 GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
1447 return ret;
1449 return NULL;
1452 struct add_font_param
1454 GpFontCollection *collection;
1455 BOOL is_system;
1456 GpStatus stat;
1457 HDC hdc;
1460 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1462 /*****************************************************************************
1463 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1465 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1466 GDIPCONST void* memory, INT length)
1468 WCHAR *name;
1469 DWORD count = 0;
1470 HANDLE font;
1471 GpStatus ret = Ok;
1472 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1474 if (!fontCollection || !memory || !length)
1475 return InvalidParameter;
1477 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME);
1478 if (!name)
1479 return OutOfMemory;
1481 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1482 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1483 if (!font || !count)
1484 ret = InvalidParameter;
1485 else
1487 struct add_font_param param;
1488 LOGFONTW lfw;
1490 param.hdc = CreateCompatibleDC(0);
1492 /* Truncate name if necessary, GDI32 can't deal with long names */
1493 if(lstrlenW(name) > LF_FACESIZE - 1)
1494 name[LF_FACESIZE - 1] = 0;
1496 lfw.lfCharSet = DEFAULT_CHARSET;
1497 lstrcpyW(lfw.lfFaceName, name);
1498 lfw.lfPitchAndFamily = 0;
1500 param.collection = fontCollection;
1501 param.is_system = FALSE;
1502 if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1503 ret = param.stat;
1505 DeleteDC(param.hdc);
1507 heap_free(name);
1508 return ret;
1511 /*****************************************************************************
1512 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1514 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1515 GpFontCollection* fontCollection, INT* numFound)
1517 TRACE("%p, %p\n", fontCollection, numFound);
1519 if (!(fontCollection && numFound))
1520 return InvalidParameter;
1522 *numFound = fontCollection->count;
1523 return Ok;
1526 /*****************************************************************************
1527 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1529 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1530 GpFontCollection* fontCollection, INT numSought,
1531 GpFontFamily* gpfamilies[], INT* numFound)
1533 INT i;
1535 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1537 if (!(fontCollection && gpfamilies && numFound))
1538 return InvalidParameter;
1540 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1542 for (i = 0; i < numSought && i < fontCollection->count; i++)
1544 gpfamilies[i] = fontCollection->FontFamilies[i];
1547 *numFound = i;
1549 return Ok;
1552 void free_installed_fonts(void)
1554 INT i;
1556 for (i = 0; i < installedFontCollection.count; i++)
1557 heap_free(installedFontCollection.FontFamilies[i]);
1558 heap_free(installedFontCollection.FontFamilies);
1560 installedFontCollection.FontFamilies = NULL;
1561 installedFontCollection.allocated = 0;
1564 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1565 DWORD type, LPARAM lParam)
1567 struct add_font_param *param = (struct add_font_param *)lParam;
1568 GpFontCollection *fonts = param->collection;
1569 GpFontFamily *family;
1570 HFONT hfont, old_hfont;
1571 struct font_metrics fm;
1572 int i;
1574 param->stat = Ok;
1576 if (type == RASTER_FONTTYPE)
1577 return 1;
1579 /* skip rotated fonts */
1580 if (lfw->lfFaceName[0] == '@')
1581 return 1;
1583 if (fonts->count && wcsicmp(lfw->lfFaceName, fonts->FontFamilies[fonts->count-1]->FamilyName) == 0)
1584 return 1;
1586 if (fonts->allocated == fonts->count)
1588 INT new_alloc_count = fonts->allocated+50;
1589 GpFontFamily** new_family_list = heap_alloc(new_alloc_count*sizeof(void*));
1591 if (!new_family_list)
1593 param->stat = OutOfMemory;
1594 return 0;
1597 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1598 heap_free(fonts->FontFamilies);
1599 fonts->FontFamilies = new_family_list;
1600 fonts->allocated = new_alloc_count;
1603 family = heap_alloc(sizeof(*family));
1604 if (!family)
1606 if (param->is_system)
1607 return 1;
1609 param->stat = OutOfMemory;
1610 return 0;
1613 /* skip duplicates */
1614 for (i=0; i<fonts->count; i++)
1616 if (wcsicmp(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1618 heap_free(family);
1619 return 1;
1623 hfont = CreateFontIndirectW(lfw);
1624 old_hfont = SelectObject(param->hdc, hfont);
1626 if (!get_font_metrics(param->hdc, &fm))
1628 SelectObject(param->hdc, old_hfont);
1629 DeleteObject(hfont);
1631 heap_free(family);
1632 param->stat = OutOfMemory;
1633 return 0;
1636 SelectObject(param->hdc, old_hfont);
1637 DeleteObject(hfont);
1639 family->em_height = fm.em_height;
1640 family->ascent = fm.ascent;
1641 family->descent = fm.descent;
1642 family->line_spacing = fm.line_spacing;
1643 family->dpi = fm.dpi;
1645 lstrcpyW(family->FamilyName, lfw->lfFaceName);
1647 fonts->FontFamilies[fonts->count++] = family;
1649 return 1;
1652 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1653 GpFontCollection** fontCollection)
1655 TRACE("(%p)\n",fontCollection);
1657 if (!fontCollection)
1658 return InvalidParameter;
1660 EnterCriticalSection( &font_cs );
1661 if (installedFontCollection.count == 0)
1663 struct add_font_param param;
1664 LOGFONTW lfw;
1666 param.hdc = CreateCompatibleDC(0);
1668 lfw.lfCharSet = DEFAULT_CHARSET;
1669 lfw.lfFaceName[0] = 0;
1670 lfw.lfPitchAndFamily = 0;
1672 param.collection = &installedFontCollection;
1673 param.is_system = TRUE;
1674 if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1676 free_installed_fonts();
1677 DeleteDC(param.hdc);
1678 LeaveCriticalSection( &font_cs );
1679 return param.stat;
1682 DeleteDC(param.hdc);
1684 LeaveCriticalSection( &font_cs );
1686 *fontCollection = &installedFontCollection;
1688 return Ok;