* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / gdiplus / font.c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "gdiplus_private.h"
21
22 /* PANOSE is 10 bytes in size, need to pack the structure properly */
23 #include <pshpack2.h>
24 typedef struct
25 {
26 USHORT version;
27 SHORT xAvgCharWidth;
28 USHORT usWeightClass;
29 USHORT usWidthClass;
30 SHORT fsType;
31 SHORT ySubscriptXSize;
32 SHORT ySubscriptYSize;
33 SHORT ySubscriptXOffset;
34 SHORT ySubscriptYOffset;
35 SHORT ySuperscriptXSize;
36 SHORT ySuperscriptYSize;
37 SHORT ySuperscriptXOffset;
38 SHORT ySuperscriptYOffset;
39 SHORT yStrikeoutSize;
40 SHORT yStrikeoutPosition;
41 SHORT sFamilyClass;
42 PANOSE panose;
43 ULONG ulUnicodeRange1;
44 ULONG ulUnicodeRange2;
45 ULONG ulUnicodeRange3;
46 ULONG ulUnicodeRange4;
47 CHAR achVendID[4];
48 USHORT fsSelection;
49 USHORT usFirstCharIndex;
50 USHORT usLastCharIndex;
51 /* According to the Apple spec, original version didn't have the below fields,
52 * version numbers were taken from the OpenType spec.
53 */
54 /* version 0 (TrueType 1.5) */
55 USHORT sTypoAscender;
56 USHORT sTypoDescender;
57 USHORT sTypoLineGap;
58 USHORT usWinAscent;
59 USHORT usWinDescent;
60 /* version 1 (TrueType 1.66) */
61 ULONG ulCodePageRange1;
62 ULONG ulCodePageRange2;
63 /* version 2 (OpenType 1.2) */
64 SHORT sxHeight;
65 SHORT sCapHeight;
66 USHORT usDefaultChar;
67 USHORT usBreakChar;
68 USHORT usMaxContext;
69 } TT_OS2_V2;
70
71 typedef struct
72 {
73 ULONG Version;
74 SHORT Ascender;
75 SHORT Descender;
76 SHORT LineGap;
77 USHORT advanceWidthMax;
78 SHORT minLeftSideBearing;
79 SHORT minRightSideBearing;
80 SHORT xMaxExtent;
81 SHORT caretSlopeRise;
82 SHORT caretSlopeRun;
83 SHORT caretOffset;
84 SHORT reserved[4];
85 SHORT metricDataFormat;
86 USHORT numberOfHMetrics;
87 } TT_HHEA;
88 #include <poppack.h>
89
90 #ifdef WORDS_BIGENDIAN
91 #define GET_BE_WORD(x) (x)
92 #define GET_BE_DWORD(x) (x)
93 #else
94 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
95 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
96 #endif
97
98 #define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
99 ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
100 ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
101 #define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
102 #define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
103
104 static GpStatus clone_font_family(const GpFontFamily *, GpFontFamily **);
105
106 static GpFontCollection installedFontCollection = {0};
107
108 /*******************************************************************************
109 * GdipCreateFont [GDIPLUS.@]
110 *
111 * Create a new font based off of a FontFamily
112 *
113 * PARAMS
114 * *fontFamily [I] Family to base the font off of
115 * emSize [I] Size of the font
116 * style [I] Bitwise OR of FontStyle enumeration
117 * unit [I] Unit emSize is measured in
118 * **font [I] the resulting Font object
119 *
120 * RETURNS
121 * SUCCESS: Ok
122 * FAILURE: InvalidParameter if fontfamily or font is NULL.
123 * FAILURE: FontFamilyNotFound if an invalid FontFamily is given
124 *
125 * NOTES
126 * UnitDisplay is unsupported.
127 * emSize is stored separately from lfHeight, to hold the fraction.
128 */
129 GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
130 REAL emSize, INT style, Unit unit, GpFont **font)
131 {
132 HFONT hfont;
133 OUTLINETEXTMETRICW otm;
134 LOGFONTW lfw;
135 HDC hdc;
136 GpStatus stat;
137 int ret;
138
139 if (!fontFamily || !font || emSize < 0.0)
140 return InvalidParameter;
141
142 TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
143 debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
144
145 memset(&lfw, 0, sizeof(lfw));
146
147 stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
148 if (stat != Ok) return stat;
149
150 lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
151 lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
152 lfw.lfItalic = style & FontStyleItalic;
153 lfw.lfUnderline = style & FontStyleUnderline;
154 lfw.lfStrikeOut = style & FontStyleStrikeout;
155
156 hfont = CreateFontIndirectW(&lfw);
157 hdc = CreateCompatibleDC(0);
158 SelectObject(hdc, hfont);
159 otm.otmSize = sizeof(otm);
160 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
161 DeleteDC(hdc);
162 DeleteObject(hfont);
163
164 if (!ret) return NotTrueTypeFont;
165
166 *font = GdipAlloc(sizeof(GpFont));
167 if (!*font) return OutOfMemory;
168
169 (*font)->unit = unit;
170 (*font)->emSize = emSize;
171 (*font)->otm = otm;
172
173 stat = clone_font_family(fontFamily, &(*font)->family);
174 if (stat != Ok)
175 {
176 GdipFree(*font);
177 return stat;
178 }
179
180 TRACE("<-- %p\n", *font);
181
182 return Ok;
183 }
184
185 /*******************************************************************************
186 * GdipCreateFontFromLogfontW [GDIPLUS.@]
187 */
188 GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
189 GDIPCONST LOGFONTW *logfont, GpFont **font)
190 {
191 HFONT hfont, oldfont;
192 OUTLINETEXTMETRICW otm;
193 WCHAR facename[LF_FACESIZE];
194 GpStatus stat;
195 int ret;
196
197 TRACE("(%p, %p, %p)\n", hdc, logfont, font);
198
199 if (!hdc || !logfont || !font)
200 return InvalidParameter;
201
202 hfont = CreateFontIndirectW(logfont);
203 oldfont = SelectObject(hdc, hfont);
204 otm.otmSize = sizeof(otm);
205 ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
206 GetTextFaceW(hdc, LF_FACESIZE, facename);
207 SelectObject(hdc, oldfont);
208 DeleteObject(hfont);
209
210 if (!ret) return NotTrueTypeFont;
211
212 *font = GdipAlloc(sizeof(GpFont));
213 if (!*font) return OutOfMemory;
214
215 (*font)->unit = UnitWorld;
216 (*font)->emSize = otm.otmTextMetrics.tmAscent;
217 (*font)->otm = otm;
218
219 stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
220 if (stat != Ok)
221 {
222 GdipFree(*font);
223 return NotTrueTypeFont;
224 }
225
226 TRACE("<-- %p\n", *font);
227
228 return Ok;
229 }
230
231 /*******************************************************************************
232 * GdipCreateFontFromLogfontA [GDIPLUS.@]
233 */
234 GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
235 GDIPCONST LOGFONTA *lfa, GpFont **font)
236 {
237 LOGFONTW lfw;
238
239 TRACE("(%p, %p, %p)\n", hdc, lfa, font);
240
241 if(!lfa || !font)
242 return InvalidParameter;
243
244 memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
245
246 if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
247 return GenericError;
248
249 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
250 }
251
252 /*******************************************************************************
253 * GdipDeleteFont [GDIPLUS.@]
254 */
255 GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
256 {
257 TRACE("(%p)\n", font);
258
259 if(!font)
260 return InvalidParameter;
261
262 GdipDeleteFontFamily(font->family);
263 GdipFree(font);
264
265 return Ok;
266 }
267
268 /*******************************************************************************
269 * GdipCreateFontFromDC [GDIPLUS.@]
270 */
271 GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
272 {
273 HFONT hfont;
274 LOGFONTW lfw;
275
276 TRACE("(%p, %p)\n", hdc, font);
277
278 if(!font)
279 return InvalidParameter;
280
281 hfont = GetCurrentObject(hdc, OBJ_FONT);
282 if(!hfont)
283 return GenericError;
284
285 if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
286 return GenericError;
287
288 return GdipCreateFontFromLogfontW(hdc, &lfw, font);
289 }
290
291 /*******************************************************************************
292 * GdipGetFamily [GDIPLUS.@]
293 *
294 * Returns the FontFamily for the specified Font
295 *
296 * PARAMS
297 * font [I] Font to request from
298 * family [O] Resulting FontFamily object
299 *
300 * RETURNS
301 * SUCCESS: Ok
302 * FAILURE: An element of GpStatus
303 */
304 GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
305 {
306 TRACE("%p %p\n", font, family);
307
308 if (!(font && family))
309 return InvalidParameter;
310
311 return GdipCloneFontFamily(font->family, family);
312 }
313
314 static REAL get_font_size(const GpFont *font)
315 {
316 return font->emSize;
317 }
318
319 /******************************************************************************
320 * GdipGetFontSize [GDIPLUS.@]
321 *
322 * Returns the size of the font in Units
323 *
324 * PARAMS
325 * *font [I] The font to retrieve size from
326 * *size [O] Pointer to hold retrieved value
327 *
328 * RETURNS
329 * SUCCESS: Ok
330 * FAILURE: InvalidParameter (font or size was NULL)
331 *
332 * NOTES
333 * Size returned is actually emSize -- not internal size used for drawing.
334 */
335 GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
336 {
337 TRACE("(%p, %p)\n", font, size);
338
339 if (!(font && size)) return InvalidParameter;
340
341 *size = get_font_size(font);
342 TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
343
344 return Ok;
345 }
346
347 static INT get_font_style(const GpFont *font)
348 {
349 INT style;
350
351 if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
352 style = FontStyleBold;
353 else
354 style = FontStyleRegular;
355 if (font->otm.otmTextMetrics.tmItalic)
356 style |= FontStyleItalic;
357 if (font->otm.otmTextMetrics.tmUnderlined)
358 style |= FontStyleUnderline;
359 if (font->otm.otmTextMetrics.tmStruckOut)
360 style |= FontStyleStrikeout;
361
362 return style;
363 }
364
365 /*******************************************************************************
366 * GdipGetFontStyle [GDIPLUS.@]
367 *
368 * Gets the font's style, returned in bitwise OR of FontStyle enumeration
369 *
370 * PARAMS
371 * font [I] font to request from
372 * style [O] resulting pointer to a FontStyle enumeration
373 *
374 * RETURNS
375 * SUCCESS: Ok
376 * FAILURE: InvalidParameter
377 */
378 GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
379 {
380 TRACE("%p %p\n", font, style);
381
382 if (!(font && style))
383 return InvalidParameter;
384
385 *style = get_font_style(font);
386 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
387
388 return Ok;
389 }
390
391 /*******************************************************************************
392 * GdipGetFontUnit [GDIPLUS.@]
393 *
394 * PARAMS
395 * font [I] Font to retrieve from
396 * unit [O] Return value
397 *
398 * RETURNS
399 * FAILURE: font or unit was NULL
400 * OK: otherwise
401 */
402 GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
403 {
404 TRACE("(%p, %p)\n", font, unit);
405
406 if (!(font && unit)) return InvalidParameter;
407
408 *unit = font->unit;
409 TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
410
411 return Ok;
412 }
413
414 /*******************************************************************************
415 * GdipGetLogFontA [GDIPLUS.@]
416 */
417 GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
418 LOGFONTA *lfa)
419 {
420 GpStatus status;
421 LOGFONTW lfw;
422
423 TRACE("(%p, %p, %p)\n", font, graphics, lfa);
424
425 status = GdipGetLogFontW(font, graphics, &lfw);
426 if(status != Ok)
427 return status;
428
429 memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
430
431 if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
432 return GenericError;
433
434 return Ok;
435 }
436
437 /*******************************************************************************
438 * GdipGetLogFontW [GDIPLUS.@]
439 */
440 GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
441 {
442 REAL angle, rel_height, height;
443 GpMatrix matrix;
444 GpPointF pt[3];
445
446 TRACE("(%p, %p, %p)\n", font, graphics, lf);
447
448 if (!font || !graphics || !lf)
449 return InvalidParameter;
450
451 matrix = graphics->worldtrans;
452
453 if (font->unit == UnitPixel)
454 {
455 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
456 if (graphics->unit != UnitDisplay)
457 GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
458 }
459 else
460 {
461 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
462 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
463 else
464 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
465 }
466
467 pt[0].X = 0.0;
468 pt[0].Y = 0.0;
469 pt[1].X = 1.0;
470 pt[1].Y = 0.0;
471 pt[2].X = 0.0;
472 pt[2].Y = 1.0;
473 GdipTransformMatrixPoints(&matrix, pt, 3);
474 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
475 rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
476 (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
477
478 lf->lfHeight = -gdip_round(height * rel_height);
479 lf->lfWidth = 0;
480 lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
481 if (lf->lfEscapement < 0)
482 {
483 lf->lfEscapement += 3600;
484 lf->lfOrientation += 3600;
485 }
486 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
487 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
488 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
489 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
490 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
491 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
492 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
493 lf->lfQuality = DEFAULT_QUALITY;
494 lf->lfPitchAndFamily = 0;
495 strcpyW(lf->lfFaceName, font->family->FamilyName);
496
497 TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
498
499 return Ok;
500 }
501
502 /*******************************************************************************
503 * GdipCloneFont [GDIPLUS.@]
504 */
505 GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
506 {
507 GpStatus stat;
508
509 TRACE("(%p, %p)\n", font, cloneFont);
510
511 if(!font || !cloneFont)
512 return InvalidParameter;
513
514 *cloneFont = GdipAlloc(sizeof(GpFont));
515 if(!*cloneFont) return OutOfMemory;
516
517 **cloneFont = *font;
518 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
519 if (stat != Ok) GdipFree(*cloneFont);
520
521 return stat;
522 }
523
524 /*******************************************************************************
525 * GdipGetFontHeight [GDIPLUS.@]
526 * PARAMS
527 * font [I] Font to retrieve height from
528 * graphics [I] The current graphics context
529 * height [O] Resulting height
530 * RETURNS
531 * SUCCESS: Ok
532 * FAILURE: Another element of GpStatus
533 *
534 * NOTES
535 * Forwards to GdipGetFontHeightGivenDPI
536 */
537 GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
538 GDIPCONST GpGraphics *graphics, REAL *height)
539 {
540 REAL dpi;
541 GpStatus stat;
542 REAL font_height;
543
544 TRACE("%p %p %p\n", font, graphics, height);
545
546 stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
547 if (stat != Ok) return stat;
548
549 if (!graphics)
550 {
551 *height = font_height;
552 TRACE("%s,%d => %f\n",
553 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
554 return Ok;
555 }
556
557 stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
558 if (stat != Ok) return stat;
559
560 *height = pixels_to_units(font_height, graphics->unit, dpi);
561
562 TRACE("%s,%d(unit %d) => %f\n",
563 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
564 return Ok;
565 }
566
567 /*******************************************************************************
568 * GdipGetFontHeightGivenDPI [GDIPLUS.@]
569 * PARAMS
570 * font [I] Font to retrieve DPI from
571 * dpi [I] DPI to assume
572 * height [O] Return value
573 *
574 * RETURNS
575 * SUCCESS: Ok
576 * FAILURE: InvalidParameter if font or height is NULL
577 *
578 * NOTES
579 * According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
580 * (for anything other than unit Pixel)
581 */
582 GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
583 {
584 GpStatus stat;
585 INT style;
586 UINT16 line_spacing, em_height;
587 REAL font_size;
588
589 if (!font || !height) return InvalidParameter;
590
591 TRACE("%p (%s), %f, %p\n", font,
592 debugstr_w(font->family->FamilyName), dpi, height);
593
594 font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
595 style = get_font_style(font);
596 stat = GdipGetLineSpacing(font->family, style, &line_spacing);
597 if (stat != Ok) return stat;
598 stat = GdipGetEmHeight(font->family, style, &em_height);
599 if (stat != Ok) return stat;
600
601 *height = (REAL)line_spacing * font_size / (REAL)em_height;
602
603 TRACE("%s,%d => %f\n",
604 debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
605
606 return Ok;
607 }
608
609 /***********************************************************************
610 * Borrowed from GDI32:
611 *
612 * Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
613 * We have to use other types because of the FONTENUMPROCW definition.
614 */
615 static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
616 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
617 {
618 if (type & RASTER_FONTTYPE)
619 return 1;
620
621 *(LOGFONTW *)lParam = *elf;
622
623 return 0;
624 }
625
626 struct font_metrics
627 {
628 WCHAR facename[LF_FACESIZE];
629 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
630 int dpi;
631 };
632
633 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
634 {
635 OUTLINETEXTMETRICW otm;
636 TT_OS2_V2 tt_os2;
637 TT_HHEA tt_hori;
638 LONG size;
639 UINT16 line_gap;
640
641 otm.otmSize = sizeof(otm);
642 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
643
644 GetTextFaceW(hdc, LF_FACESIZE, fm->facename);
645
646 fm->em_height = otm.otmEMSquare;
647 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
648
649 memset(&tt_hori, 0, sizeof(tt_hori));
650 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
651 {
652 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
653 fm->descent = -GET_BE_WORD(tt_hori.Descender);
654 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
655 line_gap = GET_BE_WORD(tt_hori.LineGap);
656 fm->line_spacing = fm->ascent + fm->descent + line_gap;
657 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
658 if (fm->ascent + fm->descent != 0) return TRUE;
659 }
660
661 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
662 if (size == GDI_ERROR) return FALSE;
663
664 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
665
666 memset(&tt_os2, 0, sizeof(tt_os2));
667 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
668
669 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
670 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
671 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
672 if (fm->ascent + fm->descent == 0)
673 {
674 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
675 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
676 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
677 }
678 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
679 fm->line_spacing = fm->ascent + fm->descent + line_gap;
680 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
681 return TRUE;
682 }
683
684 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
685 {
686 LOGFONTW lf;
687 HDC hdc = CreateCompatibleDC(0);
688 GpStatus ret = FontFamilyNotFound;
689
690 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
691 {
692 HFONT hfont, old_font;
693
694 hfont = CreateFontIndirectW(&lf);
695 old_font = SelectObject(hdc, hfont);
696 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
697 SelectObject(hdc, old_font);
698 DeleteObject(hfont);
699 }
700
701 DeleteDC(hdc);
702 return ret;
703 }
704
705 /*******************************************************************************
706 * GdipCreateFontFamilyFromName [GDIPLUS.@]
707 *
708 * Creates a font family object based on a supplied name
709 *
710 * PARAMS
711 * name [I] Name of the font
712 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
713 * FontFamily [O] Pointer to the resulting FontFamily object
714 *
715 * RETURNS
716 * SUCCESS: Ok
717 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
718 * FAILURE: Invalid parameter if FontFamily or name is NULL
719 *
720 * NOTES
721 * If fontCollection is NULL then the object is not part of any collection
722 *
723 */
724
725 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
726 GpFontCollection *fontCollection,
727 GpFontFamily **FontFamily)
728 {
729 GpStatus stat;
730 GpFontFamily* ffamily;
731 struct font_metrics fm;
732
733 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
734
735 if (!(name && FontFamily))
736 return InvalidParameter;
737 if (fontCollection)
738 FIXME("No support for FontCollections yet!\n");
739
740 stat = find_installed_font(name, &fm);
741 if (stat != Ok) return stat;
742
743 ffamily = GdipAlloc(sizeof (GpFontFamily));
744 if (!ffamily) return OutOfMemory;
745
746 lstrcpyW(ffamily->FamilyName, fm.facename);
747 ffamily->em_height = fm.em_height;
748 ffamily->ascent = fm.ascent;
749 ffamily->descent = fm.descent;
750 ffamily->line_spacing = fm.line_spacing;
751 ffamily->dpi = fm.dpi;
752
753 *FontFamily = ffamily;
754
755 TRACE("<-- %p\n", ffamily);
756
757 return Ok;
758 }
759
760 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
761 {
762 *clone = GdipAlloc(sizeof(GpFontFamily));
763 if (!*clone) return OutOfMemory;
764
765 **clone = *family;
766
767 return Ok;
768 }
769
770 /*******************************************************************************
771 * GdipCloneFontFamily [GDIPLUS.@]
772 *
773 * Creates a deep copy of a Font Family object
774 *
775 * PARAMS
776 * FontFamily [I] Font to clone
777 * clonedFontFamily [O] The resulting cloned font
778 *
779 * RETURNS
780 * SUCCESS: Ok
781 */
782 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
783 {
784 GpStatus status;
785
786 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
787
788 TRACE("%p (%s), %p\n", FontFamily,
789 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
790
791 status = clone_font_family(FontFamily, clonedFontFamily);
792 if (status != Ok) return status;
793
794 TRACE("<-- %p\n", *clonedFontFamily);
795
796 return Ok;
797 }
798
799 /*******************************************************************************
800 * GdipGetFamilyName [GDIPLUS.@]
801 *
802 * Returns the family name into name
803 *
804 * PARAMS
805 * *family [I] Family to retrieve from
806 * *name [O] WCHARS of the family name
807 * LANGID [I] charset
808 *
809 * RETURNS
810 * SUCCESS: Ok
811 * FAILURE: InvalidParameter if family is NULL
812 *
813 * NOTES
814 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
815 */
816 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
817 WCHAR *name, LANGID language)
818 {
819 static int lang_fixme;
820
821 if (family == NULL)
822 return InvalidParameter;
823
824 TRACE("%p, %p, %d\n", family, name, language);
825
826 if (language != LANG_NEUTRAL && !lang_fixme++)
827 FIXME("No support for handling of multiple languages!\n");
828
829 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
830
831 return Ok;
832 }
833
834
835 /*****************************************************************************
836 * GdipDeleteFontFamily [GDIPLUS.@]
837 *
838 * Removes the specified FontFamily
839 *
840 * PARAMS
841 * *FontFamily [I] The family to delete
842 *
843 * RETURNS
844 * SUCCESS: Ok
845 * FAILURE: InvalidParameter if FontFamily is NULL.
846 *
847 */
848 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
849 {
850 if (!FontFamily)
851 return InvalidParameter;
852 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
853
854 GdipFree (FontFamily);
855
856 return Ok;
857 }
858
859 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
860 INT style, UINT16* CellAscent)
861 {
862 if (!(family && CellAscent)) return InvalidParameter;
863
864 *CellAscent = family->ascent;
865 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
866
867 return Ok;
868 }
869
870 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
871 INT style, UINT16* CellDescent)
872 {
873 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
874
875 if (!(family && CellDescent)) return InvalidParameter;
876
877 *CellDescent = family->descent;
878 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
879
880 return Ok;
881 }
882
883 /*******************************************************************************
884 * GdipGetEmHeight [GDIPLUS.@]
885 *
886 * Gets the height of the specified family in EmHeights
887 *
888 * PARAMS
889 * family [I] Family to retrieve from
890 * style [I] (optional) style
891 * EmHeight [O] return value
892 *
893 * RETURNS
894 * SUCCESS: Ok
895 * FAILURE: InvalidParameter
896 */
897 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
898 {
899 if (!(family && EmHeight)) return InvalidParameter;
900
901 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
902
903 *EmHeight = family->em_height;
904 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
905
906 return Ok;
907 }
908
909
910 /*******************************************************************************
911 * GdipGetLineSpacing [GDIPLUS.@]
912 *
913 * Returns the line spacing in design units
914 *
915 * PARAMS
916 * family [I] Family to retrieve from
917 * style [I] (Optional) font style
918 * LineSpacing [O] Return value
919 *
920 * RETURNS
921 * SUCCESS: Ok
922 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
923 */
924 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
925 INT style, UINT16* LineSpacing)
926 {
927 TRACE("%p, %d, %p\n", family, style, LineSpacing);
928
929 if (!(family && LineSpacing))
930 return InvalidParameter;
931
932 if (style) FIXME("ignoring style\n");
933
934 *LineSpacing = family->line_spacing;
935 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
936
937 return Ok;
938 }
939
940 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
941 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
942 {
943 INT fontstyle = FontStyleRegular;
944
945 if (!ntm) return 1;
946
947 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
948 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
949 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
950 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
951
952 return (INT)lParam != fontstyle;
953 }
954
955 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
956 INT style, BOOL* IsStyleAvailable)
957 {
958 HDC hdc;
959
960 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
961
962 if (!(family && IsStyleAvailable))
963 return InvalidParameter;
964
965 *IsStyleAvailable = FALSE;
966
967 hdc = CreateCompatibleDC(0);
968
969 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
970 *IsStyleAvailable = TRUE;
971
972 DeleteDC(hdc);
973
974 return Ok;
975 }
976
977 /*****************************************************************************
978 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
979 *
980 * Obtains a serif family (Courier New on Windows)
981 *
982 * PARAMS
983 * **nativeFamily [I] Where the font will be stored
984 *
985 * RETURNS
986 * InvalidParameter if nativeFamily is NULL.
987 * Ok otherwise.
988 */
989 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
990 {
991 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
992 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
993 GpStatus stat;
994
995 if (nativeFamily == NULL) return InvalidParameter;
996
997 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
998
999 if (stat == FontFamilyNotFound)
1000 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1001
1002 if (stat == FontFamilyNotFound)
1003 ERR("Missing 'Courier New' font\n");
1004
1005 return stat;
1006 }
1007
1008 /*****************************************************************************
1009 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1010 *
1011 * Obtains a serif family (Times New Roman on Windows)
1012 *
1013 * PARAMS
1014 * **nativeFamily [I] Where the font will be stored
1015 *
1016 * RETURNS
1017 * InvalidParameter if nativeFamily is NULL.
1018 * Ok otherwise.
1019 */
1020 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1021 {
1022 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1023 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1024 GpStatus stat;
1025
1026 TRACE("(%p)\n", nativeFamily);
1027
1028 if (nativeFamily == NULL) return InvalidParameter;
1029
1030 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1031
1032 if (stat == FontFamilyNotFound)
1033 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1034
1035 if (stat == FontFamilyNotFound)
1036 ERR("Missing 'Times New Roman' font\n");
1037
1038 return stat;
1039 }
1040
1041 /*****************************************************************************
1042 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1043 *
1044 * Obtains a serif family (Microsoft Sans Serif on Windows)
1045 *
1046 * PARAMS
1047 * **nativeFamily [I] Where the font will be stored
1048 *
1049 * RETURNS
1050 * InvalidParameter if nativeFamily is NULL.
1051 * Ok otherwise.
1052 */
1053 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1054 {
1055 GpStatus stat;
1056 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1057 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1058
1059 TRACE("(%p)\n", nativeFamily);
1060
1061 if (nativeFamily == NULL) return InvalidParameter;
1062
1063 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1064
1065 if (stat == FontFamilyNotFound)
1066 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1067 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1068
1069 return stat;
1070 }
1071
1072 /*****************************************************************************
1073 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1074 */
1075 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1076 {
1077 TRACE("%p\n", fontCollection);
1078
1079 if (!fontCollection)
1080 return InvalidParameter;
1081
1082 *fontCollection = GdipAlloc(sizeof(GpFontCollection));
1083 if (!*fontCollection) return OutOfMemory;
1084
1085 (*fontCollection)->FontFamilies = NULL;
1086 (*fontCollection)->count = 0;
1087 (*fontCollection)->allocated = 0;
1088
1089 TRACE("<-- %p\n", *fontCollection);
1090
1091 return Ok;
1092 }
1093
1094 /*****************************************************************************
1095 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1096 */
1097 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1098 {
1099 INT i;
1100
1101 TRACE("%p\n", fontCollection);
1102
1103 if (!fontCollection)
1104 return InvalidParameter;
1105
1106 for (i = 0; i < (*fontCollection)->count; i++) GdipFree((*fontCollection)->FontFamilies[i]);
1107 GdipFree(*fontCollection);
1108
1109 return Ok;
1110 }
1111
1112 /*****************************************************************************
1113 * GdipPrivateAddFontFile [GDIPLUS.@]
1114 */
1115 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1116 {
1117 HANDLE file, mapping;
1118 LARGE_INTEGER size;
1119 void *mem;
1120 GpStatus status;
1121
1122 TRACE("%p, %s\n", collection, debugstr_w(name));
1123
1124 if (!collection || !name) return InvalidParameter;
1125
1126 file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1127 if (file == INVALID_HANDLE_VALUE) return InvalidParameter;
1128
1129 if (!GetFileSizeEx(file, &size) || size.u.HighPart)
1130 {
1131 CloseHandle(file);
1132 return InvalidParameter;
1133 }
1134
1135 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
1136 CloseHandle(file);
1137 if (!mapping) return InvalidParameter;
1138
1139 mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1140 CloseHandle(mapping);
1141 if (!mem) return InvalidParameter;
1142
1143 /* GdipPrivateAddMemoryFont creates a copy of the memory block */
1144 status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
1145 UnmapViewOfFile(mem);
1146
1147 return status;
1148 }
1149
1150 #define TT_PLATFORM_APPLE_UNICODE 0
1151 #define TT_PLATFORM_MACINTOSH 1
1152 #define TT_PLATFORM_MICROSOFT 3
1153
1154 #define TT_APPLE_ID_DEFAULT 0
1155 #define TT_APPLE_ID_ISO_10646 2
1156 #define TT_APPLE_ID_UNICODE_2_0 3
1157
1158 #define TT_MS_ID_SYMBOL_CS 0
1159 #define TT_MS_ID_UNICODE_CS 1
1160
1161 #define TT_MAC_ID_SIMPLIFIED_CHINESE 25
1162
1163 #define NAME_ID_FULL_FONT_NAME 4
1164
1165 typedef struct {
1166 USHORT major_version;
1167 USHORT minor_version;
1168 USHORT tables_no;
1169 USHORT search_range;
1170 USHORT entry_selector;
1171 USHORT range_shift;
1172 } tt_header;
1173
1174 typedef struct {
1175 char tag[4]; /* table name */
1176 ULONG check_sum; /* Check sum */
1177 ULONG offset; /* Offset from beginning of file */
1178 ULONG length; /* length of the table in bytes */
1179 } tt_table_directory;
1180
1181 typedef struct {
1182 USHORT format; /* format selector. Always 0 */
1183 USHORT count; /* Name Records count */
1184 USHORT string_offset; /* Offset for strings storage, * from start of the table */
1185 } tt_name_table;
1186
1187 typedef struct {
1188 USHORT platform_id;
1189 USHORT encoding_id;
1190 USHORT language_id;
1191 USHORT name_id;
1192 USHORT length;
1193 USHORT offset; /* from start of storage area */
1194 } tt_name_record;
1195
1196 /* Copied from gdi32/freetype.c */
1197
1198 static const LANGID mac_langid_table[] =
1199 {
1200 MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
1201 MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FRENCH */
1202 MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GERMAN */
1203 MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ITALIAN */
1204 MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DUTCH */
1205 MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWEDISH */
1206 MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SPANISH */
1207 MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DANISH */
1208 MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PORTUGUESE */
1209 MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NORWEGIAN */
1210 MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HEBREW */
1211 MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_JAPANESE */
1212 MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARABIC */
1213 MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FINNISH */
1214 MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREEK */
1215 MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ICELANDIC */
1216 MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALTESE */
1217 MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKISH */
1218 MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CROATIAN */
1219 MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
1220 MAKELANGID(LANG_URDU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_URDU */
1221 MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HINDI */
1222 MAKELANGID(LANG_THAI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_THAI */
1223 MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KOREAN */
1224 MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LITHUANIAN */
1225 MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_POLISH */
1226 MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HUNGARIAN */
1227 MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESTONIAN */
1228 MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LETTISH */
1229 MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SAAMISK */
1230 MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FAEROESE */
1231 MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FARSI */
1232 MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_RUSSIAN */
1233 MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
1234 MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), /* TT_MAC_LANGID_FLEMISH */
1235 MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_IRISH */
1236 MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ALBANIAN */
1237 MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ROMANIAN */
1238 MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CZECH */
1239 MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVAK */
1240 MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVENIAN */
1241 0, /* TT_MAC_LANGID_YIDDISH */
1242 MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SERBIAN */
1243 MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MACEDONIAN */
1244 MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BULGARIAN */
1245 MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UKRAINIAN */
1246 MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BYELORUSSIAN */
1247 MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UZBEK */
1248 MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KAZAKH */
1249 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC), /* TT_MAC_LANGID_AZERBAIJANI */
1250 0, /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
1251 MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARMENIAN */
1252 MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GEORGIAN */
1253 0, /* TT_MAC_LANGID_MOLDAVIAN */
1254 MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KIRGHIZ */
1255 MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAJIKI */
1256 MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKMEN */
1257 MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MONGOLIAN */
1258 MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
1259 MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PASHTO */
1260 0, /* TT_MAC_LANGID_KURDISH */
1261 MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KASHMIRI */
1262 MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINDHI */
1263 MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIBETAN */
1264 MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NEPALI */
1265 MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SANSKRIT */
1266 MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MARATHI */
1267 MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BENGALI */
1268 MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ASSAMESE */
1269 MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GUJARATI */
1270 MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PUNJABI */
1271 MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ORIYA */
1272 MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAYALAM */
1273 MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KANNADA */
1274 MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAMIL */
1275 MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TELUGU */
1276 MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINHALESE */
1277 0, /* TT_MAC_LANGID_BURMESE */
1278 MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KHMER */
1279 MAKELANGID(LANG_LAO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LAO */
1280 MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_VIETNAMESE */
1281 MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INDONESIAN */
1282 0, /* TT_MAC_LANGID_TAGALOG */
1283 MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
1284 0, /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
1285 MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AMHARIC */
1286 MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIGRINYA */
1287 0, /* TT_MAC_LANGID_GALLA */
1288 0, /* TT_MAC_LANGID_SOMALI */
1289 MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWAHILI */
1290 0, /* TT_MAC_LANGID_RUANDA */
1291 0, /* TT_MAC_LANGID_RUNDI */
1292 0, /* TT_MAC_LANGID_CHEWA */
1293 MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAGASY */
1294 MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESPERANTO */
1295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 95-111 */
1296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112-127 */
1297 MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_WELSH */
1298 MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BASQUE */
1299 MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CATALAN */
1300 0, /* TT_MAC_LANGID_LATIN */
1301 MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_QUECHUA */
1302 0, /* TT_MAC_LANGID_GUARANI */
1303 0, /* TT_MAC_LANGID_AYMARA */
1304 MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TATAR */
1305 MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UIGHUR */
1306 0, /* TT_MAC_LANGID_DZONGKHA */
1307 0, /* TT_MAC_LANGID_JAVANESE */
1308 0, /* TT_MAC_LANGID_SUNDANESE */
1309 MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GALICIAN */
1310 MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AFRIKAANS */
1311 MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BRETON */
1312 MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INUKTITUT */
1313 MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SCOTTISH_GAELIC */
1314 MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MANX_GAELIC */
1315 MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND), /* TT_MAC_LANGID_IRISH_GAELIC */
1316 0, /* TT_MAC_LANGID_TONGAN */
1317 0, /* TT_MAC_LANGID_GREEK_POLYTONIC */
1318 MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREELANDIC */
1319 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN), /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
1320 };
1321
1322 static inline WORD get_mac_code_page( const tt_name_record *name )
1323 {
1324 WORD encoding_id = GET_BE_WORD(name->encoding_id);
1325 if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008; /* special case */
1326 return 10000 + encoding_id;
1327 }
1328
1329 static int match_name_table_language( const tt_name_record *name, LANGID lang )
1330 {
1331 LANGID name_lang;
1332
1333 switch (GET_BE_WORD(name->platform_id))
1334 {
1335 case TT_PLATFORM_MICROSOFT:
1336 switch (GET_BE_WORD(name->encoding_id))
1337 {
1338 case TT_MS_ID_UNICODE_CS:
1339 case TT_MS_ID_SYMBOL_CS:
1340 name_lang = GET_BE_WORD(name->language_id);
1341 break;
1342 default:
1343 return 0;
1344 }
1345 break;
1346 case TT_PLATFORM_MACINTOSH:
1347 if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
1348 name_lang = GET_BE_WORD(name->language_id);
1349 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1350 name_lang = mac_langid_table[name_lang];
1351 break;
1352 case TT_PLATFORM_APPLE_UNICODE:
1353 switch (GET_BE_WORD(name->encoding_id))
1354 {
1355 case TT_APPLE_ID_DEFAULT:
1356 case TT_APPLE_ID_ISO_10646:
1357 case TT_APPLE_ID_UNICODE_2_0:
1358 name_lang = GET_BE_WORD(name->language_id);
1359 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1360 name_lang = mac_langid_table[name_lang];
1361 break;
1362 default:
1363 return 0;
1364 }
1365 break;
1366 default:
1367 return 0;
1368 }
1369 if (name_lang == lang) return 3;
1370 if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
1371 if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
1372 return 0;
1373 }
1374
1375 static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data, WCHAR *ret, DWORD len )
1376 {
1377 WORD name_len = GET_BE_WORD(name->length);
1378 WORD codepage;
1379
1380 switch (GET_BE_WORD(name->platform_id))
1381 {
1382 case TT_PLATFORM_APPLE_UNICODE:
1383 case TT_PLATFORM_MICROSOFT:
1384 if (name_len >= len*sizeof(WCHAR))
1385 return NULL;
1386 for (len = 0; len < name_len / 2; len++)
1387 ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
1388 ret[len] = 0;
1389 return ret;
1390 case TT_PLATFORM_MACINTOSH:
1391 codepage = get_mac_code_page( name );
1392 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len-1 );
1393 if (!len)
1394 return NULL;
1395 ret[len] = 0;
1396 return ret;
1397 }
1398 return NULL;
1399 }
1400
1401 static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1402 {
1403 LANGID lang = GetSystemDefaultLangID();
1404 const tt_header *header;
1405 const tt_name_table *name_table;
1406 const tt_name_record *name_record;
1407 DWORD pos, ofs, count;
1408 int i, res, best_lang = 0, best_index = -1;
1409
1410 if (sizeof(tt_header) > size)
1411 return NULL;
1412 header = (const tt_header*)mem;
1413 count = GET_BE_WORD(header->tables_no);
1414
1415 if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1416 return NULL;
1417
1418 pos = sizeof(*header);
1419 for (i = 0; i < count; i++)
1420 {
1421 const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
1422 pos += sizeof(*table_directory);
1423 if (memcmp(table_directory->tag, "name", 4) == 0)
1424 {
1425 ofs = GET_BE_DWORD(table_directory->offset);
1426 break;
1427 }
1428 }
1429 if (i >= count)
1430 return NULL;
1431
1432 if (ofs >= size)
1433 return NULL;
1434 pos = ofs + sizeof(*name_table);
1435 if (pos > size)
1436 return NULL;
1437 name_table = (const tt_name_table*)&mem[ofs];
1438 count = GET_BE_WORD(name_table->count);
1439 if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
1440 ofs += GET_BE_WORD(name_table->string_offset);
1441 for (i=0; i<count; i++)
1442 {
1443 name_record = (const tt_name_record*)&mem[pos];
1444 pos += sizeof(*name_record);
1445 if (pos > size)
1446 return NULL;
1447
1448 if (GET_BE_WORD(name_record->name_id) != id) continue;
1449 if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
1450 if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;
1451
1452 res = match_name_table_language( name_record, lang );
1453 if (res > best_lang)
1454 {
1455 best_lang = res;
1456 best_index = i;
1457 }
1458 }
1459
1460 if (best_lang)
1461 {
1462 name_record = (const tt_name_record*)(name_table + 1) + best_index;
1463 ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset), ret, len );
1464 TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
1465 GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
1466 return ret;
1467 }
1468 return NULL;
1469 }
1470
1471 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1472
1473 /*****************************************************************************
1474 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1475 */
1476 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1477 GDIPCONST void* memory, INT length)
1478 {
1479 WCHAR buf[32], *name;
1480 DWORD count = 0;
1481 HANDLE font;
1482 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1483
1484 if (!fontCollection || !memory || !length)
1485 return InvalidParameter;
1486
1487 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1488 if (!name)
1489 return OutOfMemory;
1490
1491 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1492 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1493 if (!font || !count)
1494 return InvalidParameter;
1495
1496 if (count)
1497 {
1498 HDC hdc;
1499 LOGFONTW lfw;
1500
1501 hdc = CreateCompatibleDC(0);
1502
1503 lfw.lfCharSet = DEFAULT_CHARSET;
1504 lstrcpyW(lfw.lfFaceName, name);
1505 lfw.lfPitchAndFamily = 0;
1506
1507 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1508 {
1509 ReleaseDC(0, hdc);
1510 return OutOfMemory;
1511 }
1512
1513 DeleteDC(hdc);
1514 }
1515 return Ok;
1516 }
1517
1518 /*****************************************************************************
1519 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1520 */
1521 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1522 GpFontCollection* fontCollection, INT* numFound)
1523 {
1524 TRACE("%p, %p\n", fontCollection, numFound);
1525
1526 if (!(fontCollection && numFound))
1527 return InvalidParameter;
1528
1529 *numFound = fontCollection->count;
1530 return Ok;
1531 }
1532
1533 /*****************************************************************************
1534 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1535 */
1536 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1537 GpFontCollection* fontCollection, INT numSought,
1538 GpFontFamily* gpfamilies[], INT* numFound)
1539 {
1540 INT i;
1541 GpStatus stat=Ok;
1542
1543 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1544
1545 if (!(fontCollection && gpfamilies && numFound))
1546 return InvalidParameter;
1547
1548 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1549
1550 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1551 {
1552 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1553 }
1554
1555 if (stat == Ok)
1556 *numFound = i;
1557 else
1558 {
1559 int numToFree=i;
1560 for (i=0; i<numToFree; i++)
1561 {
1562 GdipDeleteFontFamily(gpfamilies[i]);
1563 gpfamilies[i] = NULL;
1564 }
1565 }
1566
1567 return stat;
1568 }
1569
1570 void free_installed_fonts(void)
1571 {
1572 while (installedFontCollection.count)
1573 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1574 HeapFree(GetProcessHeap(), 0, installedFontCollection.FontFamilies);
1575 installedFontCollection.FontFamilies = NULL;
1576 installedFontCollection.allocated = 0;
1577 }
1578
1579 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1580 DWORD type, LPARAM lParam)
1581 {
1582 GpFontCollection* fonts = (GpFontCollection*)lParam;
1583 int i;
1584
1585 if (type == RASTER_FONTTYPE)
1586 return 1;
1587
1588 /* skip duplicates */
1589 for (i=0; i<fonts->count; i++)
1590 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1591 return 1;
1592
1593 if (fonts->allocated == fonts->count)
1594 {
1595 INT new_alloc_count = fonts->allocated+50;
1596 GpFontFamily** new_family_list = HeapAlloc(GetProcessHeap(), 0, new_alloc_count*sizeof(void*));
1597
1598 if (!new_family_list)
1599 return 0;
1600
1601 memcpy(new_family_list, fonts->FontFamilies, fonts->count*sizeof(void*));
1602 HeapFree(GetProcessHeap(), 0, fonts->FontFamilies);
1603 fonts->FontFamilies = new_family_list;
1604 fonts->allocated = new_alloc_count;
1605 }
1606
1607 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1608 fonts->count++;
1609 else
1610 return 0;
1611
1612 return 1;
1613 }
1614
1615 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1616 GpFontCollection** fontCollection)
1617 {
1618 TRACE("(%p)\n",fontCollection);
1619
1620 if (!fontCollection)
1621 return InvalidParameter;
1622
1623 if (installedFontCollection.count == 0)
1624 {
1625 HDC hdc;
1626 LOGFONTW lfw;
1627
1628 hdc = CreateCompatibleDC(0);
1629
1630 lfw.lfCharSet = DEFAULT_CHARSET;
1631 lfw.lfFaceName[0] = 0;
1632 lfw.lfPitchAndFamily = 0;
1633
1634 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1635 {
1636 free_installed_fonts();
1637 ReleaseDC(0, hdc);
1638 return OutOfMemory;
1639 }
1640
1641 DeleteDC(hdc);
1642 }
1643
1644 *fontCollection = &installedFontCollection;
1645
1646 return Ok;
1647 }