wined3d: Use context->ops->prepare_upload_bo() in wined3d_device_context_map() if...
[wine.git] / dlls / gdiplus / font.c
blob64604367da30ee6e1e6f7c433450253678e0061e
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, FALSE);
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 GdipCloneFontFamily((GpFontFamily*)fontFamily, &(*font)->family);
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 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, graphics->printer_display);
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, graphics->printer_display);
478 else
479 height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
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 lstrcpyW(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 TRACE("(%p, %p)\n", font, cloneFont);
524 if(!font || !cloneFont)
525 return InvalidParameter;
527 *cloneFont = heap_alloc_zero(sizeof(GpFont));
528 if(!*cloneFont) return OutOfMemory;
530 **cloneFont = *font;
531 return Ok;
534 /*******************************************************************************
535 * GdipGetFontHeight [GDIPLUS.@]
536 * PARAMS
537 * font [I] Font to retrieve height from
538 * graphics [I] The current graphics context
539 * height [O] Resulting height
540 * RETURNS
541 * SUCCESS: Ok
542 * FAILURE: Another element of GpStatus
544 * NOTES
545 * Forwards to GdipGetFontHeightGivenDPI
547 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
548 GDIPCONST GpGraphics *graphics, REAL *height)
550 REAL dpi;
551 GpStatus stat;
552 REAL font_height;
554 TRACE("%p %p %p\n", font, graphics, height);
556 if (!font || !height) return InvalidParameter;
558 stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
559 if (stat != Ok) return stat;
561 if (!graphics)
563 *height = font_height;
564 TRACE("%s,%d => %f\n",
565 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
566 return Ok;
569 stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
570 if (stat != Ok) return stat;
572 *height = pixels_to_units(font_height, graphics->unit, dpi, graphics->printer_display);
574 TRACE("%s,%d(unit %d) => %f\n",
575 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
576 return Ok;
579 /*******************************************************************************
580 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
581 * PARAMS
582 * font [I] Font to retrieve DPI from
583 * dpi [I] DPI to assume
584 * height [O] Return value
586 * RETURNS
587 * SUCCESS: Ok
588 * FAILURE: InvalidParameter if font or height is NULL
590 * NOTES
591 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
592 * (for anything other than unit Pixel)
594 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
596 GpStatus stat;
597 INT style;
598 UINT16 line_spacing, em_height;
599 REAL font_size;
601 if (!font || !height) return InvalidParameter;
603 TRACE("%p (%s), %f, %p\n", font,
604 debugstr_w(font->family->FamilyName), dpi, height);
606 font_size = units_to_pixels(get_font_size(font), font->unit, dpi, FALSE);
607 style = get_font_style(font);
608 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
609 if (stat != Ok) return stat;
610 stat = GdipGetEmHeight(font->family, style, &em_height);
611 if (stat != Ok) return stat;
613 *height = (REAL)line_spacing * font_size / (REAL)em_height;
615 TRACE("%s,%d => %f\n",
616 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
618 return Ok;
621 /***********************************************************************
622 * Borrowed from GDI32:
624 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
625 * We have to use other types because of the FONTENUMPROCW definition.
627 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
628 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
630 const ENUMLOGFONTW *elfW = (const ENUMLOGFONTW *)elf;
631 LOGFONTW *lf = (LOGFONTW *)lParam;
633 if (type & RASTER_FONTTYPE)
634 return 1;
636 *lf = *elf;
637 /* replace substituted font name by a real one */
638 lstrcpynW(lf->lfFaceName, elfW->elfFullName, LF_FACESIZE);
639 return 0;
642 struct font_metrics
644 WCHAR facename[LF_FACESIZE];
645 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
646 int dpi;
649 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
651 OUTLINETEXTMETRICW otm;
652 TT_OS2_V2 tt_os2;
653 TT_HHEA tt_hori;
654 LONG size;
655 UINT16 line_gap;
657 otm.otmSize = sizeof(otm);
658 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
660 fm->em_height = otm.otmEMSquare;
661 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
663 memset(&tt_hori, 0, sizeof(tt_hori));
664 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
666 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
667 fm->descent = -GET_BE_WORD(tt_hori.Descender);
668 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
669 line_gap = GET_BE_WORD(tt_hori.LineGap);
670 fm->line_spacing = fm->ascent + fm->descent + line_gap;
671 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
672 if (fm->ascent + fm->descent != 0) return TRUE;
675 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
676 if (size == GDI_ERROR) return FALSE;
678 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
680 memset(&tt_os2, 0, sizeof(tt_os2));
681 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
683 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
684 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
685 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
686 if (fm->ascent + fm->descent == 0)
688 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
689 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
690 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
692 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
693 fm->line_spacing = fm->ascent + fm->descent + line_gap;
694 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
695 return TRUE;
698 /*******************************************************************************
699 * GdipCreateFontFamilyFromName [GDIPLUS.@]
701 * Creates a font family object based on a supplied name
703 * PARAMS
704 * name [I] Name of the font
705 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
706 * FontFamily [O] Pointer to the resulting FontFamily object
708 * RETURNS
709 * SUCCESS: Ok
710 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
711 * FAILURE: Invalid parameter if FontFamily or name is NULL
713 * NOTES
714 * If fontCollection is NULL then the object is not part of any collection
718 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
719 GpFontCollection *collection,
720 GpFontFamily **family)
722 HDC hdc;
723 LOGFONTW lf;
724 GpStatus status;
725 int i;
727 TRACE("%s, %p %p\n", debugstr_w(name), collection, family);
729 if (!name || !family)
730 return InvalidParameter;
732 if (!collection)
734 status = GdipNewInstalledFontCollection(&collection);
735 if (status != Ok) return status;
738 status = FontFamilyNotFound;
740 hdc = CreateCompatibleDC(0);
742 if (!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
744 for (i = 0; i < collection->count; i++)
746 if (!wcsicmp(lf.lfFaceName, collection->FontFamilies[i]->FamilyName))
748 status = GdipCloneFontFamily(collection->FontFamilies[i], family);
749 TRACE("<-- %p\n", *family);
750 break;
755 DeleteDC(hdc);
756 return status;
759 /*******************************************************************************
760 * GdipCloneFontFamily [GDIPLUS.@]
762 * Creates a deep copy of a Font Family object
764 * PARAMS
765 * FontFamily [I] Font to clone
766 * clonedFontFamily [O] The resulting cloned font
768 * RETURNS
769 * SUCCESS: Ok
771 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily *family, GpFontFamily **clone)
773 if (!family || !clone)
774 return InvalidParameter;
776 TRACE("%p (%s), %p\n", family, debugstr_w(family->FamilyName), clone);
778 *clone = family;
780 if (!family->installed)
781 InterlockedIncrement(&family->ref);
783 return Ok;
786 /*******************************************************************************
787 * GdipGetFamilyName [GDIPLUS.@]
789 * Returns the family name into name
791 * PARAMS
792 * *family [I] Family to retrieve from
793 * *name [O] WCHARS of the family name
794 * LANGID [I] charset
796 * RETURNS
797 * SUCCESS: Ok
798 * FAILURE: InvalidParameter if family is NULL
800 * NOTES
801 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
803 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
804 WCHAR *name, LANGID language)
806 static int lang_fixme;
808 if (family == NULL)
809 return InvalidParameter;
811 TRACE("%p, %p, %d\n", family, name, language);
813 if (language != LANG_NEUTRAL && !lang_fixme++)
814 FIXME("No support for handling of multiple languages!\n");
816 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
818 return Ok;
822 /*****************************************************************************
823 * GdipDeleteFontFamily [GDIPLUS.@]
825 * Removes the specified FontFamily
827 * PARAMS
828 * *FontFamily [I] The family to delete
830 * RETURNS
831 * SUCCESS: Ok
832 * FAILURE: InvalidParameter if FontFamily is NULL.
835 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
837 if (!FontFamily)
838 return InvalidParameter;
840 if (!FontFamily->installed && !InterlockedDecrement(&FontFamily->ref))
842 heap_free(FontFamily);
845 return Ok;
848 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
849 INT style, UINT16* CellAscent)
851 if (!(family && CellAscent)) return InvalidParameter;
853 *CellAscent = family->ascent;
854 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
856 return Ok;
859 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
860 INT style, UINT16* CellDescent)
862 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
864 if (!(family && CellDescent)) return InvalidParameter;
866 *CellDescent = family->descent;
867 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
869 return Ok;
872 /*******************************************************************************
873 * GdipGetEmHeight [GDIPLUS.@]
875 * Gets the height of the specified family in EmHeights
877 * PARAMS
878 * family [I] Family to retrieve from
879 * style [I] (optional) style
880 * EmHeight [O] return value
882 * RETURNS
883 * SUCCESS: Ok
884 * FAILURE: InvalidParameter
886 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
888 if (!(family && EmHeight)) return InvalidParameter;
890 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
892 *EmHeight = family->em_height;
893 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
895 return Ok;
899 /*******************************************************************************
900 * GdipGetLineSpacing [GDIPLUS.@]
902 * Returns the line spacing in design units
904 * PARAMS
905 * family [I] Family to retrieve from
906 * style [I] (Optional) font style
907 * LineSpacing [O] Return value
909 * RETURNS
910 * SUCCESS: Ok
911 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
913 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
914 INT style, UINT16* LineSpacing)
916 TRACE("%p, %d, %p\n", family, style, LineSpacing);
918 if (!(family && LineSpacing))
919 return InvalidParameter;
921 if (style) FIXME("ignoring style\n");
923 *LineSpacing = family->line_spacing;
924 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
926 return Ok;
929 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
930 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
932 INT fontstyle = FontStyleRegular;
934 if (!ntm) return 1;
936 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
937 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
938 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
939 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
941 return (INT)lParam != fontstyle;
944 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
945 INT style, BOOL* IsStyleAvailable)
947 HDC hdc;
949 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
951 if (!(family && IsStyleAvailable))
952 return InvalidParameter;
954 *IsStyleAvailable = FALSE;
956 hdc = CreateCompatibleDC(0);
958 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
959 *IsStyleAvailable = TRUE;
961 DeleteDC(hdc);
963 return Ok;
966 /*****************************************************************************
967 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
969 * Obtains a serif family (Courier New on Windows)
971 * PARAMS
972 * **nativeFamily [I] Where the font will be stored
974 * RETURNS
975 * InvalidParameter if nativeFamily is NULL.
976 * Ok otherwise.
978 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
980 GpStatus stat;
982 if (nativeFamily == NULL) return InvalidParameter;
984 stat = GdipCreateFontFamilyFromName(L"Courier New", NULL, nativeFamily);
986 if (stat == FontFamilyNotFound)
987 stat = GdipCreateFontFamilyFromName(L"Liberation Mono", NULL, nativeFamily);
989 if (stat == FontFamilyNotFound)
990 ERR("Missing 'Courier New' font\n");
992 return stat;
995 /*****************************************************************************
996 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
998 * Obtains a serif family (Times New Roman on Windows)
1000 * PARAMS
1001 * **nativeFamily [I] Where the font will be stored
1003 * RETURNS
1004 * InvalidParameter if nativeFamily is NULL.
1005 * Ok otherwise.
1007 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1009 GpStatus stat;
1011 TRACE("(%p)\n", nativeFamily);
1013 if (nativeFamily == NULL) return InvalidParameter;
1015 stat = GdipCreateFontFamilyFromName(L"Times New Roman", NULL, nativeFamily);
1017 if (stat == FontFamilyNotFound)
1018 stat = GdipCreateFontFamilyFromName(L"Liberation Serif", NULL, nativeFamily);
1020 if (stat == FontFamilyNotFound)
1021 ERR("Missing 'Times New Roman' font\n");
1023 return stat;
1026 /*****************************************************************************
1027 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1029 * Obtains a serif family (Microsoft Sans Serif on Windows)
1031 * PARAMS
1032 * **nativeFamily [I] Where the font will be stored
1034 * RETURNS
1035 * InvalidParameter if nativeFamily is NULL.
1036 * Ok otherwise.
1038 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1040 GpStatus stat;
1042 TRACE("(%p)\n", nativeFamily);
1044 if (nativeFamily == NULL) return InvalidParameter;
1046 stat = GdipCreateFontFamilyFromName(L"Microsoft Sans Serif", NULL, nativeFamily);
1048 if (stat == FontFamilyNotFound)
1049 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1050 stat = GdipCreateFontFamilyFromName(L"Tahoma", NULL, nativeFamily);
1052 return stat;
1055 /*****************************************************************************
1056 * GdipNewPrivateFontCollection [GDIPLUS.@]
1058 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1060 TRACE("%p\n", fontCollection);
1062 if (!fontCollection)
1063 return InvalidParameter;
1065 *fontCollection = heap_alloc_zero(sizeof(GpFontCollection));
1066 if (!*fontCollection) return OutOfMemory;
1068 (*fontCollection)->FontFamilies = NULL;
1069 (*fontCollection)->count = 0;
1070 (*fontCollection)->allocated = 0;
1072 TRACE("<-- %p\n", *fontCollection);
1074 return Ok;
1077 /*****************************************************************************
1078 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1080 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1082 INT i;
1084 TRACE("%p\n", fontCollection);
1086 if (!fontCollection)
1087 return InvalidParameter;
1089 for (i = 0; i < (*fontCollection)->count; i++) GdipDeleteFontFamily((*fontCollection)->FontFamilies[i]);
1090 heap_free((*fontCollection)->FontFamilies);
1091 heap_free(*fontCollection);
1093 return Ok;
1096 /*****************************************************************************
1097 * GdipPrivateAddFontFile [GDIPLUS.@]
1099 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1101 HANDLE file, mapping;
1102 LARGE_INTEGER size;
1103 void *mem;
1104 GpStatus status;
1106 TRACE("%p, %s\n", collection, debugstr_w(name));
1108 if (!collection || !name) return InvalidParameter;
1110 file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1111 if (file == INVALID_HANDLE_VALUE) return InvalidParameter;
1113 if (!GetFileSizeEx(file, &size) || size.u.HighPart)
1115 CloseHandle(file);
1116 return InvalidParameter;
1119 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
1120 CloseHandle(file);
1121 if (!mapping) return InvalidParameter;
1123 mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1124 CloseHandle(mapping);
1125 if (!mem) return InvalidParameter;
1127 /* GdipPrivateAddMemoryFont creates a copy of the memory block */
1128 status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
1129 UnmapViewOfFile(mem);
1131 return status;
1134 #define TT_PLATFORM_APPLE_UNICODE 0
1135 #define TT_PLATFORM_MACINTOSH 1
1136 #define TT_PLATFORM_MICROSOFT 3
1138 #define TT_APPLE_ID_DEFAULT 0
1139 #define TT_APPLE_ID_ISO_10646 2
1140 #define TT_APPLE_ID_UNICODE_2_0 3
1142 #define TT_MS_ID_SYMBOL_CS 0
1143 #define TT_MS_ID_UNICODE_CS 1
1145 #define TT_MAC_ID_SIMPLIFIED_CHINESE 25
1147 #define NAME_ID_FULL_FONT_NAME 4
1149 typedef struct {
1150 USHORT major_version;
1151 USHORT minor_version;
1152 USHORT tables_no;
1153 USHORT search_range;
1154 USHORT entry_selector;
1155 USHORT range_shift;
1156 } tt_header;
1158 typedef struct {
1159 char tag[4]; /* table name */
1160 ULONG check_sum; /* Check sum */
1161 ULONG offset; /* Offset from beginning of file */
1162 ULONG length; /* length of the table in bytes */
1163 } tt_table_directory;
1165 typedef struct {
1166 USHORT format; /* format selector. Always 0 */
1167 USHORT count; /* Name Records count */
1168 USHORT string_offset; /* Offset for strings storage, * from start of the table */
1169 } tt_name_table;
1171 typedef struct {
1172 USHORT platform_id;
1173 USHORT encoding_id;
1174 USHORT language_id;
1175 USHORT name_id;
1176 USHORT length;
1177 USHORT offset; /* from start of storage area */
1178 } tt_name_record;
1180 /* Copied from gdi32/freetype.c */
1182 static const LANGID mac_langid_table[] =
1184 MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
1185 MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FRENCH */
1186 MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GERMAN */
1187 MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ITALIAN */
1188 MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DUTCH */
1189 MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWEDISH */
1190 MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SPANISH */
1191 MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DANISH */
1192 MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PORTUGUESE */
1193 MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NORWEGIAN */
1194 MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HEBREW */
1195 MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_JAPANESE */
1196 MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARABIC */
1197 MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FINNISH */
1198 MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREEK */
1199 MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ICELANDIC */
1200 MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALTESE */
1201 MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKISH */
1202 MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CROATIAN */
1203 MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
1204 MAKELANGID(LANG_URDU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_URDU */
1205 MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HINDI */
1206 MAKELANGID(LANG_THAI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_THAI */
1207 MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KOREAN */
1208 MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LITHUANIAN */
1209 MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_POLISH */
1210 MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HUNGARIAN */
1211 MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESTONIAN */
1212 MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LETTISH */
1213 MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SAAMISK */
1214 MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FAEROESE */
1215 MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FARSI */
1216 MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_RUSSIAN */
1217 MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
1218 MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), /* TT_MAC_LANGID_FLEMISH */
1219 MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_IRISH */
1220 MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ALBANIAN */
1221 MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ROMANIAN */
1222 MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CZECH */
1223 MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVAK */
1224 MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVENIAN */
1225 0, /* TT_MAC_LANGID_YIDDISH */
1226 MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SERBIAN */
1227 MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MACEDONIAN */
1228 MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BULGARIAN */
1229 MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UKRAINIAN */
1230 MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BYELORUSSIAN */
1231 MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UZBEK */
1232 MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KAZAKH */
1233 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC), /* TT_MAC_LANGID_AZERBAIJANI */
1234 0, /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
1235 MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARMENIAN */
1236 MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GEORGIAN */
1237 0, /* TT_MAC_LANGID_MOLDAVIAN */
1238 MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KIRGHIZ */
1239 MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAJIKI */
1240 MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKMEN */
1241 MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MONGOLIAN */
1242 MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
1243 MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PASHTO */
1244 0, /* TT_MAC_LANGID_KURDISH */
1245 MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KASHMIRI */
1246 MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINDHI */
1247 MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIBETAN */
1248 MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NEPALI */
1249 MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SANSKRIT */
1250 MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MARATHI */
1251 MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BENGALI */
1252 MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ASSAMESE */
1253 MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GUJARATI */
1254 MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PUNJABI */
1255 MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ORIYA */
1256 MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAYALAM */
1257 MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KANNADA */
1258 MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAMIL */
1259 MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TELUGU */
1260 MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINHALESE */
1261 0, /* TT_MAC_LANGID_BURMESE */
1262 MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KHMER */
1263 MAKELANGID(LANG_LAO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LAO */
1264 MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_VIETNAMESE */
1265 MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INDONESIAN */
1266 0, /* TT_MAC_LANGID_TAGALOG */
1267 MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
1268 0, /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
1269 MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AMHARIC */
1270 MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIGRINYA */
1271 0, /* TT_MAC_LANGID_GALLA */
1272 0, /* TT_MAC_LANGID_SOMALI */
1273 MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWAHILI */
1274 0, /* TT_MAC_LANGID_RUANDA */
1275 0, /* TT_MAC_LANGID_RUNDI */
1276 0, /* TT_MAC_LANGID_CHEWA */
1277 MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAGASY */
1278 MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESPERANTO */
1279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 95-111 */
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112-127 */
1281 MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_WELSH */
1282 MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BASQUE */
1283 MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CATALAN */
1284 0, /* TT_MAC_LANGID_LATIN */
1285 MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_QUECHUA */
1286 0, /* TT_MAC_LANGID_GUARANI */
1287 0, /* TT_MAC_LANGID_AYMARA */
1288 MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TATAR */
1289 MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UIGHUR */
1290 0, /* TT_MAC_LANGID_DZONGKHA */
1291 0, /* TT_MAC_LANGID_JAVANESE */
1292 0, /* TT_MAC_LANGID_SUNDANESE */
1293 MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GALICIAN */
1294 MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AFRIKAANS */
1295 MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BRETON */
1296 MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INUKTITUT */
1297 MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SCOTTISH_GAELIC */
1298 MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MANX_GAELIC */
1299 MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND), /* TT_MAC_LANGID_IRISH_GAELIC */
1300 0, /* TT_MAC_LANGID_TONGAN */
1301 0, /* TT_MAC_LANGID_GREEK_POLYTONIC */
1302 MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREELANDIC */
1303 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN), /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
1306 static inline WORD get_mac_code_page( const tt_name_record *name )
1308 WORD encoding_id = GET_BE_WORD(name->encoding_id);
1309 if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008; /* special case */
1310 return 10000 + encoding_id;
1313 static int match_name_table_language( const tt_name_record *name, LANGID lang )
1315 LANGID name_lang;
1317 switch (GET_BE_WORD(name->platform_id))
1319 case TT_PLATFORM_MICROSOFT:
1320 switch (GET_BE_WORD(name->encoding_id))
1322 case TT_MS_ID_UNICODE_CS:
1323 case TT_MS_ID_SYMBOL_CS:
1324 name_lang = GET_BE_WORD(name->language_id);
1325 break;
1326 default:
1327 return 0;
1329 break;
1330 case TT_PLATFORM_MACINTOSH:
1331 if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
1332 name_lang = GET_BE_WORD(name->language_id);
1333 if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1334 name_lang = mac_langid_table[name_lang];
1335 break;
1336 case TT_PLATFORM_APPLE_UNICODE:
1337 switch (GET_BE_WORD(name->encoding_id))
1339 case TT_APPLE_ID_DEFAULT:
1340 case TT_APPLE_ID_ISO_10646:
1341 case TT_APPLE_ID_UNICODE_2_0:
1342 name_lang = GET_BE_WORD(name->language_id);
1343 if (name_lang >= ARRAY_SIZE(mac_langid_table)) return 0;
1344 name_lang = mac_langid_table[name_lang];
1345 break;
1346 default:
1347 return 0;
1349 break;
1350 default:
1351 return 0;
1353 if (name_lang == lang) return 3;
1354 if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
1355 if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
1356 return 0;
1359 static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data )
1361 WORD name_len = GET_BE_WORD(name->length);
1362 WORD codepage;
1363 WCHAR *ret;
1364 int len;
1366 switch (GET_BE_WORD(name->platform_id))
1368 case TT_PLATFORM_APPLE_UNICODE:
1369 case TT_PLATFORM_MICROSOFT:
1370 ret = heap_alloc((name_len / 2 + 1) * sizeof(WCHAR));
1371 for (len = 0; len < name_len / 2; len++)
1372 ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
1373 ret[len] = 0;
1374 return ret;
1375 case TT_PLATFORM_MACINTOSH:
1376 codepage = get_mac_code_page( name );
1377 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, NULL, 0 ) + 1;
1378 if (!len)
1379 return NULL;
1380 ret = heap_alloc(len * sizeof(WCHAR));
1381 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len - 1 );
1382 ret[len] = 0;
1383 return ret;
1385 return NULL;
1388 static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id )
1390 LANGID lang = GetSystemDefaultLangID();
1391 const tt_header *header;
1392 const tt_name_table *name_table;
1393 const tt_name_record *name_record;
1394 DWORD pos, ofs, count;
1395 int i, res, best_lang = 0, best_index = -1;
1397 if (sizeof(tt_header) > size)
1398 return NULL;
1399 header = (const tt_header*)mem;
1400 count = GET_BE_WORD(header->tables_no);
1402 if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1403 return NULL;
1405 pos = sizeof(*header);
1406 for (i = 0; i < count; i++)
1408 const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
1409 pos += sizeof(*table_directory);
1410 if (memcmp(table_directory->tag, "name", 4) == 0)
1412 ofs = GET_BE_DWORD(table_directory->offset);
1413 break;
1416 if (i >= count)
1417 return NULL;
1419 if (ofs >= size)
1420 return NULL;
1421 pos = ofs + sizeof(*name_table);
1422 if (pos > size)
1423 return NULL;
1424 name_table = (const tt_name_table*)&mem[ofs];
1425 count = GET_BE_WORD(name_table->count);
1426 if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
1427 ofs += GET_BE_WORD(name_table->string_offset);
1428 for (i=0; i<count; i++)
1430 name_record = (const tt_name_record*)&mem[pos];
1431 pos += sizeof(*name_record);
1432 if (pos > size)
1433 return NULL;
1435 if (GET_BE_WORD(name_record->name_id) != id) continue;
1436 if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
1437 if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;
1439 res = match_name_table_language( name_record, lang );
1440 if (res > best_lang)
1442 best_lang = res;
1443 best_index = i;
1447 if (best_lang)
1449 WCHAR *ret;
1450 name_record = (const tt_name_record*)(name_table + 1) + best_index;
1451 ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset) );
1452 TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
1453 GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
1454 return ret;
1456 return NULL;
1459 struct add_font_param
1461 GpFontCollection *collection;
1462 BOOL is_system;
1463 GpStatus stat;
1464 HDC hdc;
1467 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1469 /*****************************************************************************
1470 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1472 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1473 GDIPCONST void* memory, INT length)
1475 WCHAR *name;
1476 DWORD count = 0;
1477 HANDLE font;
1478 GpStatus ret = Ok;
1479 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1481 if (!fontCollection || !memory || !length)
1482 return InvalidParameter;
1484 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME);
1485 if (!name)
1486 return OutOfMemory;
1488 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1489 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1490 if (!font || !count)
1491 ret = InvalidParameter;
1492 else
1494 struct add_font_param param;
1495 LOGFONTW lfw;
1497 param.hdc = CreateCompatibleDC(0);
1499 /* Truncate name if necessary, GDI32 can't deal with long names */
1500 if(lstrlenW(name) > LF_FACESIZE - 1)
1501 name[LF_FACESIZE - 1] = 0;
1503 lfw.lfCharSet = DEFAULT_CHARSET;
1504 lstrcpyW(lfw.lfFaceName, name);
1505 lfw.lfPitchAndFamily = 0;
1507 param.collection = fontCollection;
1508 param.is_system = FALSE;
1509 if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1510 ret = param.stat;
1512 DeleteDC(param.hdc);
1514 heap_free(name);
1515 return ret;
1518 /*****************************************************************************
1519 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1521 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1522 GpFontCollection* fontCollection, INT* numFound)
1524 TRACE("%p, %p\n", fontCollection, numFound);
1526 if (!(fontCollection && numFound))
1527 return InvalidParameter;
1529 *numFound = fontCollection->count;
1530 return Ok;
1533 /*****************************************************************************
1534 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1536 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1537 GpFontCollection* fontCollection, INT numSought,
1538 GpFontFamily* gpfamilies[], INT* numFound)
1540 INT i;
1542 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1544 if (!(fontCollection && gpfamilies && numFound))
1545 return InvalidParameter;
1547 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1549 for (i = 0; i < numSought && i < fontCollection->count; i++)
1551 /* caller is responsible for cloning these if it keeps references */
1552 gpfamilies[i] = fontCollection->FontFamilies[i];
1555 *numFound = i;
1557 return Ok;
1560 void free_installed_fonts(void)
1562 INT i;
1564 for (i = 0; i < installedFontCollection.count; i++)
1565 heap_free(installedFontCollection.FontFamilies[i]);
1566 heap_free(installedFontCollection.FontFamilies);
1568 installedFontCollection.FontFamilies = NULL;
1569 installedFontCollection.allocated = 0;
1572 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1573 DWORD type, LPARAM lParam)
1575 struct add_font_param *param = (struct add_font_param *)lParam;
1576 GpFontCollection *fonts = param->collection;
1577 GpFontFamily *family;
1578 HFONT hfont, old_hfont;
1579 struct font_metrics fm;
1580 int i;
1582 param->stat = Ok;
1584 if (type == RASTER_FONTTYPE)
1585 return 1;
1587 /* skip rotated fonts */
1588 if (lfw->lfFaceName[0] == '@')
1589 return 1;
1591 if (fonts->count && wcsicmp(lfw->lfFaceName, fonts->FontFamilies[fonts->count-1]->FamilyName) == 0)
1592 return 1;
1594 if (fonts->allocated == fonts->count)
1596 INT new_alloc_count = fonts->allocated+50;
1597 GpFontFamily** new_family_list = heap_alloc(new_alloc_count*sizeof(void*));
1599 if (!new_family_list)
1601 param->stat = OutOfMemory;
1602 return 0;
1605 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1606 heap_free(fonts->FontFamilies);
1607 fonts->FontFamilies = new_family_list;
1608 fonts->allocated = new_alloc_count;
1611 family = heap_alloc(sizeof(*family));
1612 if (!family)
1614 if (param->is_system)
1615 return 1;
1617 param->stat = OutOfMemory;
1618 return 0;
1621 /* skip duplicates */
1622 for (i=0; i<fonts->count; i++)
1624 if (wcsicmp(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1626 heap_free(family);
1627 return 1;
1631 hfont = CreateFontIndirectW(lfw);
1632 old_hfont = SelectObject(param->hdc, hfont);
1634 if (!get_font_metrics(param->hdc, &fm))
1636 SelectObject(param->hdc, old_hfont);
1637 DeleteObject(hfont);
1639 heap_free(family);
1640 param->stat = OutOfMemory;
1641 return 0;
1644 SelectObject(param->hdc, old_hfont);
1645 DeleteObject(hfont);
1647 family->em_height = fm.em_height;
1648 family->ascent = fm.ascent;
1649 family->descent = fm.descent;
1650 family->line_spacing = fm.line_spacing;
1651 family->dpi = fm.dpi;
1652 family->installed = param->is_system;
1653 family->ref = 1;
1655 lstrcpyW(family->FamilyName, lfw->lfFaceName);
1657 fonts->FontFamilies[fonts->count++] = family;
1659 return 1;
1662 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1663 GpFontCollection** fontCollection)
1665 TRACE("(%p)\n",fontCollection);
1667 if (!fontCollection)
1668 return InvalidParameter;
1670 EnterCriticalSection( &font_cs );
1671 if (installedFontCollection.count == 0)
1673 struct add_font_param param;
1674 LOGFONTW lfw;
1676 param.hdc = CreateCompatibleDC(0);
1678 lfw.lfCharSet = DEFAULT_CHARSET;
1679 lfw.lfFaceName[0] = 0;
1680 lfw.lfPitchAndFamily = 0;
1682 param.collection = &installedFontCollection;
1683 param.is_system = TRUE;
1684 if (!EnumFontFamiliesExW(param.hdc, &lfw, add_font_proc, (LPARAM)&param, 0))
1686 free_installed_fonts();
1687 DeleteDC(param.hdc);
1688 LeaveCriticalSection( &font_cs );
1689 return param.stat;
1692 DeleteDC(param.hdc);
1694 LeaveCriticalSection( &font_cs );
1696 *fontCollection = &installedFontCollection;
1698 return Ok;