include: New ddk/hidtypes.h include file.
[wine.git] / dlls / gdiplus / font.c
blob850f44a713983caf44927ea63292245223e65c5d
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"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
32 #include "objbase.h"
34 #include "gdiplus.h"
35 #include "gdiplus_private.h"
37 /* PANOSE is 10 bytes in size, need to pack the structure properly */
38 #include "pshpack2.h"
39 typedef struct
41 USHORT version;
42 SHORT xAvgCharWidth;
43 USHORT usWeightClass;
44 USHORT usWidthClass;
45 SHORT fsType;
46 SHORT ySubscriptXSize;
47 SHORT ySubscriptYSize;
48 SHORT ySubscriptXOffset;
49 SHORT ySubscriptYOffset;
50 SHORT ySuperscriptXSize;
51 SHORT ySuperscriptYSize;
52 SHORT ySuperscriptXOffset;
53 SHORT ySuperscriptYOffset;
54 SHORT yStrikeoutSize;
55 SHORT yStrikeoutPosition;
56 SHORT sFamilyClass;
57 PANOSE panose;
58 ULONG ulUnicodeRange1;
59 ULONG ulUnicodeRange2;
60 ULONG ulUnicodeRange3;
61 ULONG ulUnicodeRange4;
62 CHAR achVendID[4];
63 USHORT fsSelection;
64 USHORT usFirstCharIndex;
65 USHORT usLastCharIndex;
66 /* According to the Apple spec, original version didn't have the below fields,
67 * version numbers were taken from the OpenType spec.
69 /* version 0 (TrueType 1.5) */
70 USHORT sTypoAscender;
71 USHORT sTypoDescender;
72 USHORT sTypoLineGap;
73 USHORT usWinAscent;
74 USHORT usWinDescent;
75 /* version 1 (TrueType 1.66) */
76 ULONG ulCodePageRange1;
77 ULONG ulCodePageRange2;
78 /* version 2 (OpenType 1.2) */
79 SHORT sxHeight;
80 SHORT sCapHeight;
81 USHORT usDefaultChar;
82 USHORT usBreakChar;
83 USHORT usMaxContext;
84 } TT_OS2_V2;
86 typedef struct
88 ULONG Version;
89 SHORT Ascender;
90 SHORT Descender;
91 SHORT LineGap;
92 USHORT advanceWidthMax;
93 SHORT minLeftSideBearing;
94 SHORT minRightSideBearing;
95 SHORT xMaxExtent;
96 SHORT caretSlopeRise;
97 SHORT caretSlopeRun;
98 SHORT caretOffset;
99 SHORT reserved[4];
100 SHORT metricDataFormat;
101 USHORT numberOfHMetrics;
102 } TT_HHEA;
103 #include "poppack.h"
105 #ifdef WORDS_BIGENDIAN
106 #define GET_BE_WORD(x) (x)
107 #define GET_BE_DWORD(x) (x)
108 #else
109 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
110 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
111 #endif
113 #define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
114 ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
115 ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
116 #define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
117 #define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
119 static GpStatus clone_font_family(const GpFontFamily *, GpFontFamily **);
121 static GpFontCollection installedFontCollection = {0};
123 /*******************************************************************************
124 * GdipCreateFont [GDIPLUS.@]
126 * Create a new font based off of a FontFamily
128 * PARAMS
129 * *fontFamily [I] Family to base the font off of
130 * emSize [I] Size of the font
131 * style [I] Bitwise OR of FontStyle enumeration
132 * unit [I] Unit emSize is measured in
133 * **font [I] the resulting Font object
135 * RETURNS
136 * SUCCESS: Ok
137 * FAILURE: InvalidParameter if fontfamily or font is NULL.
138 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
140 * NOTES
141 * UnitDisplay is unsupported.
142 * emSize is stored separately from lfHeight, to hold the fraction.
144 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
145 REAL emSize, INT style, Unit unit, GpFont **font)
147 HFONT hfont;
148 OUTLINETEXTMETRICW otm;
149 LOGFONTW lfw;
150 HDC hdc;
151 GpStatus stat;
152 int ret;
154 if (!fontFamily || !font || emSize < 0.0)
155 return InvalidParameter;
157 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
158 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
160 memset(&lfw, 0, sizeof(lfw));
162 stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
163 if (stat != Ok) return stat;
165 lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
166 lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
167 lfw.lfItalic = style & FontStyleItalic;
168 lfw.lfUnderline = style & FontStyleUnderline;
169 lfw.lfStrikeOut = style & FontStyleStrikeout;
171 hfont = CreateFontIndirectW(&lfw);
172 hdc = CreateCompatibleDC(0);
173 SelectObject(hdc, hfont);
174 otm.otmSize = sizeof(otm);
175 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
176 DeleteDC(hdc);
177 DeleteObject(hfont);
179 if (!ret) return NotTrueTypeFont;
181 *font = GdipAlloc(sizeof(GpFont));
182 if (!*font) return OutOfMemory;
184 (*font)->unit = unit;
185 (*font)->emSize = emSize;
186 (*font)->otm = otm;
188 stat = clone_font_family(fontFamily, &(*font)->family);
189 if (stat != Ok)
191 GdipFree(*font);
192 return stat;
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 = GdipAlloc(sizeof(GpFont));
228 if (!*font) return OutOfMemory;
230 (*font)->unit = UnitWorld;
231 (*font)->emSize = otm.otmTextMetrics.tmAscent;
232 (*font)->otm = otm;
234 stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
235 if (stat != Ok)
237 GdipFree(*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 GdipFree(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 return GdipCloneFontFamily(font->family, family);
329 static REAL get_font_size(const GpFont *font)
331 return font->emSize;
334 /******************************************************************************
335 * GdipGetFontSize [GDIPLUS.@]
337 * Returns the size of the font in Units
339 * PARAMS
340 * *font [I] The font to retrieve size from
341 * *size [O] Pointer to hold retrieved value
343 * RETURNS
344 * SUCCESS: Ok
345 * FAILURE: InvalidParameter (font or size was NULL)
347 * NOTES
348 * Size returned is actually emSize -- not internal size used for drawing.
350 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
352 TRACE("(%p, %p)\n", font, size);
354 if (!(font && size)) return InvalidParameter;
356 *size = get_font_size(font);
357 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
359 return Ok;
362 static INT get_font_style(const GpFont *font)
364 INT style;
366 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
367 style = FontStyleBold;
368 else
369 style = FontStyleRegular;
370 if (font->otm.otmTextMetrics.tmItalic)
371 style |= FontStyleItalic;
372 if (font->otm.otmTextMetrics.tmUnderlined)
373 style |= FontStyleUnderline;
374 if (font->otm.otmTextMetrics.tmStruckOut)
375 style |= FontStyleStrikeout;
377 return style;
380 /*******************************************************************************
381 * GdipGetFontStyle [GDIPLUS.@]
383 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
385 * PARAMS
386 * font [I] font to request from
387 * style [O] resulting pointer to a FontStyle enumeration
389 * RETURNS
390 * SUCCESS: Ok
391 * FAILURE: InvalidParameter
393 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
395 TRACE("%p %p\n", font, style);
397 if (!(font && style))
398 return InvalidParameter;
400 *style = get_font_style(font);
401 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
403 return Ok;
406 /*******************************************************************************
407 * GdipGetFontUnit [GDIPLUS.@]
409 * PARAMS
410 * font [I] Font to retrieve from
411 * unit [O] Return value
413 * RETURNS
414 * FAILURE: font or unit was NULL
415 * OK: otherwise
417 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
419 TRACE("(%p, %p)\n", font, unit);
421 if (!(font && unit)) return InvalidParameter;
423 *unit = font->unit;
424 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
426 return Ok;
429 /*******************************************************************************
430 * GdipGetLogFontA [GDIPLUS.@]
432 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
433 LOGFONTA *lfa)
435 GpStatus status;
436 LOGFONTW lfw;
438 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
440 status = GdipGetLogFontW(font, graphics, &lfw);
441 if(status != Ok)
442 return status;
444 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
446 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
447 return GenericError;
449 return Ok;
452 /*******************************************************************************
453 * GdipGetLogFontW [GDIPLUS.@]
455 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
457 REAL angle, rel_height, height;
458 GpMatrix matrix;
459 GpPointF pt[3];
461 TRACE("(%p, %p, %p)\n", font, graphics, lf);
463 if (!font || !graphics || !lf)
464 return InvalidParameter;
466 matrix = graphics->worldtrans;
468 if (font->unit == UnitPixel || font->unit == UnitWorld)
470 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
471 if (graphics->unit != UnitDisplay)
472 GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
474 else
476 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
477 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
478 else
479 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
482 pt[0].X = 0.0;
483 pt[0].Y = 0.0;
484 pt[1].X = 1.0;
485 pt[1].Y = 0.0;
486 pt[2].X = 0.0;
487 pt[2].Y = 1.0;
488 GdipTransformMatrixPoints(&matrix, pt, 3);
489 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
490 rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
491 (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
493 lf->lfHeight = -gdip_round(height * rel_height);
494 lf->lfWidth = 0;
495 lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
496 if (lf->lfEscapement < 0)
498 lf->lfEscapement += 3600;
499 lf->lfOrientation += 3600;
501 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
502 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
503 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
504 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
505 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
506 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
507 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
508 lf->lfQuality = DEFAULT_QUALITY;
509 lf->lfPitchAndFamily = 0;
510 strcpyW(lf->lfFaceName, font->family->FamilyName);
512 TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
514 return Ok;
517 /*******************************************************************************
518 * GdipCloneFont [GDIPLUS.@]
520 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
522 GpStatus stat;
524 TRACE("(%p, %p)\n", font, cloneFont);
526 if(!font || !cloneFont)
527 return InvalidParameter;
529 *cloneFont = GdipAlloc(sizeof(GpFont));
530 if(!*cloneFont) return OutOfMemory;
532 **cloneFont = *font;
533 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
534 if (stat != Ok) GdipFree(*cloneFont);
536 return stat;
539 /*******************************************************************************
540 * GdipGetFontHeight [GDIPLUS.@]
541 * PARAMS
542 * font [I] Font to retrieve height from
543 * graphics [I] The current graphics context
544 * height [O] Resulting height
545 * RETURNS
546 * SUCCESS: Ok
547 * FAILURE: Another element of GpStatus
549 * NOTES
550 * Forwards to GdipGetFontHeightGivenDPI
552 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
553 GDIPCONST GpGraphics *graphics, REAL *height)
555 REAL dpi;
556 GpStatus stat;
557 REAL font_height;
559 TRACE("%p %p %p\n", font, graphics, height);
561 stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
562 if (stat != Ok) return stat;
564 if (!graphics)
566 *height = font_height;
567 TRACE("%s,%d => %f\n",
568 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
569 return Ok;
572 stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
573 if (stat != Ok) return stat;
575 *height = pixels_to_units(font_height, graphics->unit, dpi);
577 TRACE("%s,%d(unit %d) => %f\n",
578 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
579 return Ok;
582 /*******************************************************************************
583 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
584 * PARAMS
585 * font [I] Font to retrieve DPI from
586 * dpi [I] DPI to assume
587 * height [O] Return value
589 * RETURNS
590 * SUCCESS: Ok
591 * FAILURE: InvalidParameter if font or height is NULL
593 * NOTES
594 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
595 * (for anything other than unit Pixel)
597 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
599 GpStatus stat;
600 INT style;
601 UINT16 line_spacing, em_height;
602 REAL font_size;
604 if (!font || !height) return InvalidParameter;
606 TRACE("%p (%s), %f, %p\n", font,
607 debugstr_w(font->family->FamilyName), dpi, height);
609 font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
610 style = get_font_style(font);
611 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
612 if (stat != Ok) return stat;
613 stat = GdipGetEmHeight(font->family, style, &em_height);
614 if (stat != Ok) return stat;
616 *height = (REAL)line_spacing * font_size / (REAL)em_height;
618 TRACE("%s,%d => %f\n",
619 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
621 return Ok;
624 /***********************************************************************
625 * Borrowed from GDI32:
627 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
628 * We have to use other types because of the FONTENUMPROCW definition.
630 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
631 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
633 if (type & RASTER_FONTTYPE)
634 return 1;
636 *(LOGFONTW *)lParam = *elf;
638 return 0;
641 struct font_metrics
643 WCHAR facename[LF_FACESIZE];
644 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
645 int dpi;
648 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
650 OUTLINETEXTMETRICW otm;
651 TT_OS2_V2 tt_os2;
652 TT_HHEA tt_hori;
653 LONG size;
654 UINT16 line_gap;
656 otm.otmSize = sizeof(otm);
657 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
659 GetTextFaceW(hdc, LF_FACESIZE, fm->facename);
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 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
701 LOGFONTW lf;
702 HDC hdc = CreateCompatibleDC(0);
703 GpStatus ret = FontFamilyNotFound;
705 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
707 HFONT hfont, old_font;
709 hfont = CreateFontIndirectW(&lf);
710 old_font = SelectObject(hdc, hfont);
711 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
712 SelectObject(hdc, old_font);
713 DeleteObject(hfont);
716 DeleteDC(hdc);
717 return ret;
720 /*******************************************************************************
721 * GdipCreateFontFamilyFromName [GDIPLUS.@]
723 * Creates a font family object based on a supplied name
725 * PARAMS
726 * name [I] Name of the font
727 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
728 * FontFamily [O] Pointer to the resulting FontFamily object
730 * RETURNS
731 * SUCCESS: Ok
732 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
733 * FAILURE: Invalid parameter if FontFamily or name is NULL
735 * NOTES
736 * If fontCollection is NULL then the object is not part of any collection
740 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
741 GpFontCollection *fontCollection,
742 GpFontFamily **FontFamily)
744 GpStatus stat;
745 GpFontFamily* ffamily;
746 struct font_metrics fm;
748 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
750 if (!(name && FontFamily))
751 return InvalidParameter;
752 if (fontCollection)
753 FIXME("No support for FontCollections yet!\n");
755 stat = find_installed_font(name, &fm);
756 if (stat != Ok) return stat;
758 ffamily = GdipAlloc(sizeof (GpFontFamily));
759 if (!ffamily) return OutOfMemory;
761 lstrcpyW(ffamily->FamilyName, fm.facename);
762 ffamily->em_height = fm.em_height;
763 ffamily->ascent = fm.ascent;
764 ffamily->descent = fm.descent;
765 ffamily->line_spacing = fm.line_spacing;
766 ffamily->dpi = fm.dpi;
768 *FontFamily = ffamily;
770 TRACE("<-- %p\n", ffamily);
772 return Ok;
775 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
777 *clone = GdipAlloc(sizeof(GpFontFamily));
778 if (!*clone) return OutOfMemory;
780 **clone = *family;
782 return Ok;
785 /*******************************************************************************
786 * GdipCloneFontFamily [GDIPLUS.@]
788 * Creates a deep copy of a Font Family object
790 * PARAMS
791 * FontFamily [I] Font to clone
792 * clonedFontFamily [O] The resulting cloned font
794 * RETURNS
795 * SUCCESS: Ok
797 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
799 GpStatus status;
801 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
803 TRACE("%p (%s), %p\n", FontFamily,
804 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
806 status = clone_font_family(FontFamily, clonedFontFamily);
807 if (status != Ok) return status;
809 TRACE("<-- %p\n", *clonedFontFamily);
811 return Ok;
814 /*******************************************************************************
815 * GdipGetFamilyName [GDIPLUS.@]
817 * Returns the family name into name
819 * PARAMS
820 * *family [I] Family to retrieve from
821 * *name [O] WCHARS of the family name
822 * LANGID [I] charset
824 * RETURNS
825 * SUCCESS: Ok
826 * FAILURE: InvalidParameter if family is NULL
828 * NOTES
829 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
831 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
832 WCHAR *name, LANGID language)
834 static int lang_fixme;
836 if (family == NULL)
837 return InvalidParameter;
839 TRACE("%p, %p, %d\n", family, name, language);
841 if (language != LANG_NEUTRAL && !lang_fixme++)
842 FIXME("No support for handling of multiple languages!\n");
844 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
846 return Ok;
850 /*****************************************************************************
851 * GdipDeleteFontFamily [GDIPLUS.@]
853 * Removes the specified FontFamily
855 * PARAMS
856 * *FontFamily [I] The family to delete
858 * RETURNS
859 * SUCCESS: Ok
860 * FAILURE: InvalidParameter if FontFamily is NULL.
863 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
865 if (!FontFamily)
866 return InvalidParameter;
867 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
869 GdipFree (FontFamily);
871 return Ok;
874 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
875 INT style, UINT16* CellAscent)
877 if (!(family && CellAscent)) return InvalidParameter;
879 *CellAscent = family->ascent;
880 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
882 return Ok;
885 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
886 INT style, UINT16* CellDescent)
888 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
890 if (!(family && CellDescent)) return InvalidParameter;
892 *CellDescent = family->descent;
893 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
895 return Ok;
898 /*******************************************************************************
899 * GdipGetEmHeight [GDIPLUS.@]
901 * Gets the height of the specified family in EmHeights
903 * PARAMS
904 * family [I] Family to retrieve from
905 * style [I] (optional) style
906 * EmHeight [O] return value
908 * RETURNS
909 * SUCCESS: Ok
910 * FAILURE: InvalidParameter
912 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
914 if (!(family && EmHeight)) return InvalidParameter;
916 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
918 *EmHeight = family->em_height;
919 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
921 return Ok;
925 /*******************************************************************************
926 * GdipGetLineSpacing [GDIPLUS.@]
928 * Returns the line spacing in design units
930 * PARAMS
931 * family [I] Family to retrieve from
932 * style [I] (Optional) font style
933 * LineSpacing [O] Return value
935 * RETURNS
936 * SUCCESS: Ok
937 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
939 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
940 INT style, UINT16* LineSpacing)
942 TRACE("%p, %d, %p\n", family, style, LineSpacing);
944 if (!(family && LineSpacing))
945 return InvalidParameter;
947 if (style) FIXME("ignoring style\n");
949 *LineSpacing = family->line_spacing;
950 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
952 return Ok;
955 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
956 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
958 INT fontstyle = FontStyleRegular;
960 if (!ntm) return 1;
962 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
963 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
964 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
965 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
967 return (INT)lParam != fontstyle;
970 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
971 INT style, BOOL* IsStyleAvailable)
973 HDC hdc;
975 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
977 if (!(family && IsStyleAvailable))
978 return InvalidParameter;
980 *IsStyleAvailable = FALSE;
982 hdc = CreateCompatibleDC(0);
984 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
985 *IsStyleAvailable = TRUE;
987 DeleteDC(hdc);
989 return Ok;
992 /*****************************************************************************
993 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
995 * Obtains a serif family (Courier New on Windows)
997 * PARAMS
998 * **nativeFamily [I] Where the font will be stored
1000 * RETURNS
1001 * InvalidParameter if nativeFamily is NULL.
1002 * Ok otherwise.
1004 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
1006 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
1007 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
1008 GpStatus stat;
1010 if (nativeFamily == NULL) return InvalidParameter;
1012 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
1014 if (stat == FontFamilyNotFound)
1015 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1017 if (stat == FontFamilyNotFound)
1018 ERR("Missing 'Courier New' font\n");
1020 return stat;
1023 /*****************************************************************************
1024 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1026 * Obtains a serif family (Times New Roman on Windows)
1028 * PARAMS
1029 * **nativeFamily [I] Where the font will be stored
1031 * RETURNS
1032 * InvalidParameter if nativeFamily is NULL.
1033 * Ok otherwise.
1035 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1037 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1038 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1039 GpStatus stat;
1041 TRACE("(%p)\n", nativeFamily);
1043 if (nativeFamily == NULL) return InvalidParameter;
1045 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1047 if (stat == FontFamilyNotFound)
1048 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1050 if (stat == FontFamilyNotFound)
1051 ERR("Missing 'Times New Roman' font\n");
1053 return stat;
1056 /*****************************************************************************
1057 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1059 * Obtains a serif family (Microsoft Sans Serif on Windows)
1061 * PARAMS
1062 * **nativeFamily [I] Where the font will be stored
1064 * RETURNS
1065 * InvalidParameter if nativeFamily is NULL.
1066 * Ok otherwise.
1068 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1070 GpStatus stat;
1071 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1072 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1074 TRACE("(%p)\n", nativeFamily);
1076 if (nativeFamily == NULL) return InvalidParameter;
1078 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1080 if (stat == FontFamilyNotFound)
1081 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1082 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1084 return stat;
1087 /*****************************************************************************
1088 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1090 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1092 TRACE("%p\n", fontCollection);
1094 if (!fontCollection)
1095 return InvalidParameter;
1097 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1098 if (!*fontCollection) return OutOfMemory;
1100 (*fontCollection)->FontFamilies = NULL;
1101 (*fontCollection)->count = 0;
1102 (*fontCollection)->allocated = 0;
1104 TRACE("<-- %p\n", *fontCollection);
1106 return Ok;
1109 /*****************************************************************************
1110 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1112 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1114 INT i;
1116 TRACE("%p\n", fontCollection);
1118 if (!fontCollection)
1119 return InvalidParameter;
1121 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1122 GdipFree(*fontCollection);
1124 return Ok;
1127 /*****************************************************************************
1128 * GdipPrivateAddFontFile [GDIPLUS.@]
1130 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1132 HANDLE file, mapping;
1133 LARGE_INTEGER size;
1134 void *mem;
1135 GpStatus status;
1137 TRACE("%p, %s\n", collection, debugstr_w(name));
1139 if (!collection || !name) return InvalidParameter;
1141 file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1142 if (file == INVALID_HANDLE_VALUE) return InvalidParameter;
1144 if (!GetFileSizeEx(file, &size) || size.u.HighPart)
1146 CloseHandle(file);
1147 return InvalidParameter;
1150 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
1151 CloseHandle(file);
1152 if (!mapping) return InvalidParameter;
1154 mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1155 CloseHandle(mapping);
1156 if (!mem) return InvalidParameter;
1158 /* GdipPrivateAddMemoryFont creates a copy of the memory block */
1159 status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
1160 UnmapViewOfFile(mem);
1162 return status;
1165 #define TT_PLATFORM_APPLE_UNICODE 0
1166 #define TT_PLATFORM_MACINTOSH 1
1167 #define TT_PLATFORM_MICROSOFT 3
1169 #define TT_APPLE_ID_DEFAULT 0
1170 #define TT_APPLE_ID_ISO_10646 2
1171 #define TT_APPLE_ID_UNICODE_2_0 3
1173 #define TT_MS_ID_SYMBOL_CS 0
1174 #define TT_MS_ID_UNICODE_CS 1
1176 #define TT_MAC_ID_SIMPLIFIED_CHINESE 25
1178 #define NAME_ID_FULL_FONT_NAME 4
1180 typedef struct {
1181 USHORT major_version;
1182 USHORT minor_version;
1183 USHORT tables_no;
1184 USHORT search_range;
1185 USHORT entry_selector;
1186 USHORT range_shift;
1187 } tt_header;
1189 typedef struct {
1190 char tag[4]; /* table name */
1191 ULONG check_sum; /* Check sum */
1192 ULONG offset; /* Offset from beginning of file */
1193 ULONG length; /* length of the table in bytes */
1194 } tt_table_directory;
1196 typedef struct {
1197 USHORT format; /* format selector. Always 0 */
1198 USHORT count; /* Name Records count */
1199 USHORT string_offset; /* Offset for strings storage, * from start of the table */
1200 } tt_name_table;
1202 typedef struct {
1203 USHORT platform_id;
1204 USHORT encoding_id;
1205 USHORT language_id;
1206 USHORT name_id;
1207 USHORT length;
1208 USHORT offset; /* from start of storage area */
1209 } tt_name_record;
1211 /* Copied from gdi32/freetype.c */
1213 static const LANGID mac_langid_table[] =
1215 MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
1216 MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FRENCH */
1217 MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GERMAN */
1218 MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ITALIAN */
1219 MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DUTCH */
1220 MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWEDISH */
1221 MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SPANISH */
1222 MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DANISH */
1223 MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PORTUGUESE */
1224 MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NORWEGIAN */
1225 MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HEBREW */
1226 MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_JAPANESE */
1227 MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARABIC */
1228 MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FINNISH */
1229 MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREEK */
1230 MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ICELANDIC */
1231 MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALTESE */
1232 MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKISH */
1233 MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CROATIAN */
1234 MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
1235 MAKELANGID(LANG_URDU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_URDU */
1236 MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HINDI */
1237 MAKELANGID(LANG_THAI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_THAI */
1238 MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KOREAN */
1239 MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LITHUANIAN */
1240 MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_POLISH */
1241 MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HUNGARIAN */
1242 MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESTONIAN */
1243 MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LETTISH */
1244 MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SAAMISK */
1245 MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FAEROESE */
1246 MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FARSI */
1247 MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_RUSSIAN */
1248 MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
1249 MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), /* TT_MAC_LANGID_FLEMISH */
1250 MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_IRISH */
1251 MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ALBANIAN */
1252 MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ROMANIAN */
1253 MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CZECH */
1254 MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVAK */
1255 MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVENIAN */
1256 0, /* TT_MAC_LANGID_YIDDISH */
1257 MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SERBIAN */
1258 MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MACEDONIAN */
1259 MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BULGARIAN */
1260 MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UKRAINIAN */
1261 MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BYELORUSSIAN */
1262 MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UZBEK */
1263 MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KAZAKH */
1264 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC), /* TT_MAC_LANGID_AZERBAIJANI */
1265 0, /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
1266 MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARMENIAN */
1267 MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GEORGIAN */
1268 0, /* TT_MAC_LANGID_MOLDAVIAN */
1269 MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KIRGHIZ */
1270 MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAJIKI */
1271 MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKMEN */
1272 MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MONGOLIAN */
1273 MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
1274 MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PASHTO */
1275 0, /* TT_MAC_LANGID_KURDISH */
1276 MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KASHMIRI */
1277 MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINDHI */
1278 MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIBETAN */
1279 MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NEPALI */
1280 MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SANSKRIT */
1281 MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MARATHI */
1282 MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BENGALI */
1283 MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ASSAMESE */
1284 MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GUJARATI */
1285 MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PUNJABI */
1286 MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ORIYA */
1287 MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAYALAM */
1288 MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KANNADA */
1289 MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAMIL */
1290 MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TELUGU */
1291 MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINHALESE */
1292 0, /* TT_MAC_LANGID_BURMESE */
1293 MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KHMER */
1294 MAKELANGID(LANG_LAO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LAO */
1295 MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_VIETNAMESE */
1296 MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INDONESIAN */
1297 0, /* TT_MAC_LANGID_TAGALOG */
1298 MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
1299 0, /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
1300 MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AMHARIC */
1301 MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIGRINYA */
1302 0, /* TT_MAC_LANGID_GALLA */
1303 0, /* TT_MAC_LANGID_SOMALI */
1304 MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWAHILI */
1305 0, /* TT_MAC_LANGID_RUANDA */
1306 0, /* TT_MAC_LANGID_RUNDI */
1307 0, /* TT_MAC_LANGID_CHEWA */
1308 MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAGASY */
1309 MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESPERANTO */
1310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 95-111 */
1311 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112-127 */
1312 MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_WELSH */
1313 MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BASQUE */
1314 MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CATALAN */
1315 0, /* TT_MAC_LANGID_LATIN */
1316 MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_QUECHUA */
1317 0, /* TT_MAC_LANGID_GUARANI */
1318 0, /* TT_MAC_LANGID_AYMARA */
1319 MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TATAR */
1320 MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UIGHUR */
1321 0, /* TT_MAC_LANGID_DZONGKHA */
1322 0, /* TT_MAC_LANGID_JAVANESE */
1323 0, /* TT_MAC_LANGID_SUNDANESE */
1324 MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GALICIAN */
1325 MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AFRIKAANS */
1326 MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BRETON */
1327 MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INUKTITUT */
1328 MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SCOTTISH_GAELIC */
1329 MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MANX_GAELIC */
1330 MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND), /* TT_MAC_LANGID_IRISH_GAELIC */
1331 0, /* TT_MAC_LANGID_TONGAN */
1332 0, /* TT_MAC_LANGID_GREEK_POLYTONIC */
1333 MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREELANDIC */
1334 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN), /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
1337 static inline WORD get_mac_code_page( const tt_name_record *name )
1339 WORD encoding_id = GET_BE_WORD(name->encoding_id);
1340 if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008; /* special case */
1341 return 10000 + encoding_id;
1344 static int match_name_table_language( const tt_name_record *name, LANGID lang )
1346 LANGID name_lang;
1348 switch (GET_BE_WORD(name->platform_id))
1350 case TT_PLATFORM_MICROSOFT:
1351 switch (GET_BE_WORD(name->encoding_id))
1353 case TT_MS_ID_UNICODE_CS:
1354 case TT_MS_ID_SYMBOL_CS:
1355 name_lang = GET_BE_WORD(name->language_id);
1356 break;
1357 default:
1358 return 0;
1360 break;
1361 case TT_PLATFORM_MACINTOSH:
1362 if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
1363 name_lang = GET_BE_WORD(name->language_id);
1364 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1365 name_lang = mac_langid_table[name_lang];
1366 break;
1367 case TT_PLATFORM_APPLE_UNICODE:
1368 switch (GET_BE_WORD(name->encoding_id))
1370 case TT_APPLE_ID_DEFAULT:
1371 case TT_APPLE_ID_ISO_10646:
1372 case TT_APPLE_ID_UNICODE_2_0:
1373 name_lang = GET_BE_WORD(name->language_id);
1374 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1375 name_lang = mac_langid_table[name_lang];
1376 break;
1377 default:
1378 return 0;
1380 break;
1381 default:
1382 return 0;
1384 if (name_lang == lang) return 3;
1385 if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
1386 if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
1387 return 0;
1390 static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data, WCHAR *ret, DWORD len )
1392 WORD name_len = GET_BE_WORD(name->length);
1393 WORD codepage;
1395 switch (GET_BE_WORD(name->platform_id))
1397 case TT_PLATFORM_APPLE_UNICODE:
1398 case TT_PLATFORM_MICROSOFT:
1399 if (name_len >= len*sizeof(WCHAR))
1400 return NULL;
1401 for (len = 0; len < name_len / 2; len++)
1402 ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
1403 ret[len] = 0;
1404 return ret;
1405 case TT_PLATFORM_MACINTOSH:
1406 codepage = get_mac_code_page( name );
1407 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len-1 );
1408 if (!len)
1409 return NULL;
1410 ret[len] = 0;
1411 return ret;
1413 return NULL;
1416 static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1418 LANGID lang = GetSystemDefaultLangID();
1419 const tt_header *header;
1420 const tt_name_table *name_table;
1421 const tt_name_record *name_record;
1422 DWORD pos, ofs, count;
1423 int i, res, best_lang = 0, best_index = -1;
1425 if (sizeof(tt_header) > size)
1426 return NULL;
1427 header = (const tt_header*)mem;
1428 count = GET_BE_WORD(header->tables_no);
1430 if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1431 return NULL;
1433 pos = sizeof(*header);
1434 for (i = 0; i < count; i++)
1436 const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
1437 pos += sizeof(*table_directory);
1438 if (memcmp(table_directory->tag, "name", 4) == 0)
1440 ofs = GET_BE_DWORD(table_directory->offset);
1441 break;
1444 if (i >= count)
1445 return NULL;
1447 if (ofs >= size)
1448 return NULL;
1449 pos = ofs + sizeof(*name_table);
1450 if (pos > size)
1451 return NULL;
1452 name_table = (const tt_name_table*)&mem[ofs];
1453 count = GET_BE_WORD(name_table->count);
1454 if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
1455 ofs += GET_BE_WORD(name_table->string_offset);
1456 for (i=0; i<count; i++)
1458 name_record = (const tt_name_record*)&mem[pos];
1459 pos += sizeof(*name_record);
1460 if (pos > size)
1461 return NULL;
1463 if (GET_BE_WORD(name_record->name_id) != id) continue;
1464 if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
1465 if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;
1467 res = match_name_table_language( name_record, lang );
1468 if (res > best_lang)
1470 best_lang = res;
1471 best_index = i;
1475 if (best_lang)
1477 name_record = (const tt_name_record*)(name_table + 1) + best_index;
1478 ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset), ret, len );
1479 TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
1480 GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
1481 return ret;
1483 return NULL;
1486 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1488 /*****************************************************************************
1489 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1491 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1492 GDIPCONST void* memory, INT length)
1494 WCHAR buf[32], *name;
1495 DWORD count = 0;
1496 HANDLE font;
1497 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1499 if (!fontCollection || !memory || !length)
1500 return InvalidParameter;
1502 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1503 if (!name)
1504 return OutOfMemory;
1506 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1507 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1508 if (!font || !count)
1509 return InvalidParameter;
1511 if (count)
1513 HDC hdc;
1514 LOGFONTW lfw;
1516 hdc = CreateCompatibleDC(0);
1518 lfw.lfCharSet = DEFAULT_CHARSET;
1519 lstrcpyW(lfw.lfFaceName, name);
1520 lfw.lfPitchAndFamily = 0;
1522 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1524 DeleteDC(hdc);
1525 return OutOfMemory;
1528 DeleteDC(hdc);
1530 return Ok;
1533 /*****************************************************************************
1534 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1536 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1537 GpFontCollection* fontCollection, INT* numFound)
1539 TRACE("%p, %p\n", fontCollection, numFound);
1541 if (!(fontCollection && numFound))
1542 return InvalidParameter;
1544 *numFound = fontCollection->count;
1545 return Ok;
1548 /*****************************************************************************
1549 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1551 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1552 GpFontCollection* fontCollection, INT numSought,
1553 GpFontFamily* gpfamilies[], INT* numFound)
1555 INT i;
1556 GpStatus stat=Ok;
1558 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1560 if (!(fontCollection && gpfamilies && numFound))
1561 return InvalidParameter;
1563 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1565 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1567 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1570 if (stat == Ok)
1571 *numFound = i;
1572 else
1574 int numToFree=i;
1575 for (i=0; i<numToFree; i++)
1577 GdipDeleteFontFamily(gpfamilies[i]);
1578 gpfamilies[i] = NULL;
1582 return stat;
1585 void free_installed_fonts(void)
1587 while (installedFontCollection.count)
1588 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1589 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1590 installedFontCollection.FontFamilies = NULL;
1591 installedFontCollection.allocated = 0;
1594 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1595 DWORD type, LPARAM lParam)
1597 GpFontCollection* fonts = (GpFontCollection*)lParam;
1598 int i;
1600 if (type == RASTER_FONTTYPE)
1601 return 1;
1603 /* skip duplicates */
1604 for (i=0; i<fonts->count; i++)
1605 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1606 return 1;
1608 if (fonts->allocated == fonts->count)
1610 INT new_alloc_count = fonts->allocated+50;
1611 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1613 if (!new_family_list)
1614 return 0;
1616 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1617 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1618 fonts->FontFamilies = new_family_list;
1619 fonts->allocated = new_alloc_count;
1622 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1623 fonts->count++;
1624 else
1625 return 0;
1627 return 1;
1630 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1631 GpFontCollection** fontCollection)
1633 TRACE("(%p)\n",fontCollection);
1635 if (!fontCollection)
1636 return InvalidParameter;
1638 if (installedFontCollection.count == 0)
1640 HDC hdc;
1641 LOGFONTW lfw;
1643 hdc = CreateCompatibleDC(0);
1645 lfw.lfCharSet = DEFAULT_CHARSET;
1646 lfw.lfFaceName[0] = 0;
1647 lfw.lfPitchAndFamily = 0;
1649 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1651 free_installed_fonts();
1652 DeleteDC(hdc);
1653 return OutOfMemory;
1656 DeleteDC(hdc);
1659 *fontCollection = &installedFontCollection;
1661 return Ok;