[GDIPLUS] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / 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 = heap_alloc_zero(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 heap_free(*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 = heap_alloc_zero(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 heap_free(*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 heap_free(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 || font->unit == UnitWorld)
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 = heap_alloc_zero(sizeof(GpFont));
515 if(!*cloneFont) return OutOfMemory;
516
517 **cloneFont = *font;
518 stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
519 if (stat != Ok) heap_free(*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 const ENUMLOGFONTW *elfW = (const ENUMLOGFONTW *)elf;
619 LOGFONTW *lf = (LOGFONTW *)lParam;
620
621 if (type & RASTER_FONTTYPE)
622 return 1;
623
624 *lf = *elf;
625 /* replace substituted font name by a real one */
626 lstrcpynW(lf->lfFaceName, elfW->elfFullName, LF_FACESIZE);
627 return 0;
628 }
629
630 struct font_metrics
631 {
632 WCHAR facename[LF_FACESIZE];
633 UINT16 em_height, ascent, descent, line_spacing; /* in font units */
634 int dpi;
635 };
636
637 static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
638 {
639 OUTLINETEXTMETRICW otm;
640 TT_OS2_V2 tt_os2;
641 TT_HHEA tt_hori;
642 LONG size;
643 UINT16 line_gap;
644
645 otm.otmSize = sizeof(otm);
646 if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
647
648 fm->em_height = otm.otmEMSquare;
649 fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
650
651 memset(&tt_hori, 0, sizeof(tt_hori));
652 if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
653 {
654 fm->ascent = GET_BE_WORD(tt_hori.Ascender);
655 fm->descent = -GET_BE_WORD(tt_hori.Descender);
656 TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
657 line_gap = GET_BE_WORD(tt_hori.LineGap);
658 fm->line_spacing = fm->ascent + fm->descent + line_gap;
659 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
660 if (fm->ascent + fm->descent != 0) return TRUE;
661 }
662
663 size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
664 if (size == GDI_ERROR) return FALSE;
665
666 if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
667
668 memset(&tt_os2, 0, sizeof(tt_os2));
669 if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
670
671 fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
672 fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
673 TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
674 if (fm->ascent + fm->descent == 0)
675 {
676 fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
677 fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
678 TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
679 }
680 line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
681 fm->line_spacing = fm->ascent + fm->descent + line_gap;
682 TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
683 return TRUE;
684 }
685
686 static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
687 {
688 LOGFONTW lf;
689 HDC hdc = CreateCompatibleDC(0);
690 GpStatus ret = FontFamilyNotFound;
691
692 if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
693 {
694 HFONT hfont, old_font;
695
696 strcpyW(fm->facename, lf.lfFaceName);
697
698 hfont = CreateFontIndirectW(&lf);
699 old_font = SelectObject(hdc, hfont);
700 ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
701 SelectObject(hdc, old_font);
702 DeleteObject(hfont);
703 }
704
705 DeleteDC(hdc);
706 return ret;
707 }
708
709 /*******************************************************************************
710 * GdipCreateFontFamilyFromName [GDIPLUS.@]
711 *
712 * Creates a font family object based on a supplied name
713 *
714 * PARAMS
715 * name [I] Name of the font
716 * fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
717 * FontFamily [O] Pointer to the resulting FontFamily object
718 *
719 * RETURNS
720 * SUCCESS: Ok
721 * FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
722 * FAILURE: Invalid parameter if FontFamily or name is NULL
723 *
724 * NOTES
725 * If fontCollection is NULL then the object is not part of any collection
726 *
727 */
728
729 GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
730 GpFontCollection *fontCollection,
731 GpFontFamily **FontFamily)
732 {
733 GpStatus stat;
734 GpFontFamily* ffamily;
735 struct font_metrics fm;
736
737 TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
738
739 if (!(name && FontFamily))
740 return InvalidParameter;
741 if (fontCollection)
742 FIXME("No support for FontCollections yet!\n");
743
744 stat = find_installed_font(name, &fm);
745 if (stat != Ok) return stat;
746
747 ffamily = heap_alloc_zero(sizeof (GpFontFamily));
748 if (!ffamily) return OutOfMemory;
749
750 lstrcpyW(ffamily->FamilyName, fm.facename);
751 ffamily->em_height = fm.em_height;
752 ffamily->ascent = fm.ascent;
753 ffamily->descent = fm.descent;
754 ffamily->line_spacing = fm.line_spacing;
755 ffamily->dpi = fm.dpi;
756
757 *FontFamily = ffamily;
758
759 TRACE("<-- %p\n", ffamily);
760
761 return Ok;
762 }
763
764 static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
765 {
766 *clone = heap_alloc_zero(sizeof(GpFontFamily));
767 if (!*clone) return OutOfMemory;
768
769 **clone = *family;
770
771 return Ok;
772 }
773
774 /*******************************************************************************
775 * GdipCloneFontFamily [GDIPLUS.@]
776 *
777 * Creates a deep copy of a Font Family object
778 *
779 * PARAMS
780 * FontFamily [I] Font to clone
781 * clonedFontFamily [O] The resulting cloned font
782 *
783 * RETURNS
784 * SUCCESS: Ok
785 */
786 GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
787 {
788 GpStatus status;
789
790 if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
791
792 TRACE("%p (%s), %p\n", FontFamily,
793 debugstr_w(FontFamily->FamilyName), clonedFontFamily);
794
795 status = clone_font_family(FontFamily, clonedFontFamily);
796 if (status != Ok) return status;
797
798 TRACE("<-- %p\n", *clonedFontFamily);
799
800 return Ok;
801 }
802
803 /*******************************************************************************
804 * GdipGetFamilyName [GDIPLUS.@]
805 *
806 * Returns the family name into name
807 *
808 * PARAMS
809 * *family [I] Family to retrieve from
810 * *name [O] WCHARS of the family name
811 * LANGID [I] charset
812 *
813 * RETURNS
814 * SUCCESS: Ok
815 * FAILURE: InvalidParameter if family is NULL
816 *
817 * NOTES
818 * If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
819 */
820 GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
821 WCHAR *name, LANGID language)
822 {
823 static int lang_fixme;
824
825 if (family == NULL)
826 return InvalidParameter;
827
828 TRACE("%p, %p, %d\n", family, name, language);
829
830 if (language != LANG_NEUTRAL && !lang_fixme++)
831 FIXME("No support for handling of multiple languages!\n");
832
833 lstrcpynW (name, family->FamilyName, LF_FACESIZE);
834
835 return Ok;
836 }
837
838
839 /*****************************************************************************
840 * GdipDeleteFontFamily [GDIPLUS.@]
841 *
842 * Removes the specified FontFamily
843 *
844 * PARAMS
845 * *FontFamily [I] The family to delete
846 *
847 * RETURNS
848 * SUCCESS: Ok
849 * FAILURE: InvalidParameter if FontFamily is NULL.
850 *
851 */
852 GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
853 {
854 if (!FontFamily)
855 return InvalidParameter;
856 TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
857
858 heap_free (FontFamily);
859
860 return Ok;
861 }
862
863 GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
864 INT style, UINT16* CellAscent)
865 {
866 if (!(family && CellAscent)) return InvalidParameter;
867
868 *CellAscent = family->ascent;
869 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
870
871 return Ok;
872 }
873
874 GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
875 INT style, UINT16* CellDescent)
876 {
877 TRACE("(%p, %d, %p)\n", family, style, CellDescent);
878
879 if (!(family && CellDescent)) return InvalidParameter;
880
881 *CellDescent = family->descent;
882 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
883
884 return Ok;
885 }
886
887 /*******************************************************************************
888 * GdipGetEmHeight [GDIPLUS.@]
889 *
890 * Gets the height of the specified family in EmHeights
891 *
892 * PARAMS
893 * family [I] Family to retrieve from
894 * style [I] (optional) style
895 * EmHeight [O] return value
896 *
897 * RETURNS
898 * SUCCESS: Ok
899 * FAILURE: InvalidParameter
900 */
901 GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
902 {
903 if (!(family && EmHeight)) return InvalidParameter;
904
905 TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
906
907 *EmHeight = family->em_height;
908 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
909
910 return Ok;
911 }
912
913
914 /*******************************************************************************
915 * GdipGetLineSpacing [GDIPLUS.@]
916 *
917 * Returns the line spacing in design units
918 *
919 * PARAMS
920 * family [I] Family to retrieve from
921 * style [I] (Optional) font style
922 * LineSpacing [O] Return value
923 *
924 * RETURNS
925 * SUCCESS: Ok
926 * FAILURE: InvalidParameter (family or LineSpacing was NULL)
927 */
928 GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
929 INT style, UINT16* LineSpacing)
930 {
931 TRACE("%p, %d, %p\n", family, style, LineSpacing);
932
933 if (!(family && LineSpacing))
934 return InvalidParameter;
935
936 if (style) FIXME("ignoring style\n");
937
938 *LineSpacing = family->line_spacing;
939 TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
940
941 return Ok;
942 }
943
944 static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
945 const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
946 {
947 INT fontstyle = FontStyleRegular;
948
949 if (!ntm) return 1;
950
951 if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
952 if (ntm->tmItalic) fontstyle |= FontStyleItalic;
953 if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
954 if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
955
956 return (INT)lParam != fontstyle;
957 }
958
959 GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
960 INT style, BOOL* IsStyleAvailable)
961 {
962 HDC hdc;
963
964 TRACE("%p %d %p\n", family, style, IsStyleAvailable);
965
966 if (!(family && IsStyleAvailable))
967 return InvalidParameter;
968
969 *IsStyleAvailable = FALSE;
970
971 hdc = CreateCompatibleDC(0);
972
973 if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
974 *IsStyleAvailable = TRUE;
975
976 DeleteDC(hdc);
977
978 return Ok;
979 }
980
981 /*****************************************************************************
982 * GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
983 *
984 * Obtains a serif family (Courier New on Windows)
985 *
986 * PARAMS
987 * **nativeFamily [I] Where the font will be stored
988 *
989 * RETURNS
990 * InvalidParameter if nativeFamily is NULL.
991 * Ok otherwise.
992 */
993 GpStatus WINGDIPAPI GdipGetGenericFontFamilyMonospace(GpFontFamily **nativeFamily)
994 {
995 static const WCHAR CourierNew[] = {'C','o','u','r','i','e','r',' ','N','e','w','\0'};
996 static const WCHAR LiberationMono[] = {'L','i','b','e','r','a','t','i','o','n',' ','M','o','n','o','\0'};
997 GpStatus stat;
998
999 if (nativeFamily == NULL) return InvalidParameter;
1000
1001 stat = GdipCreateFontFamilyFromName(CourierNew, NULL, nativeFamily);
1002
1003 if (stat == FontFamilyNotFound)
1004 stat = GdipCreateFontFamilyFromName(LiberationMono, NULL, nativeFamily);
1005
1006 if (stat == FontFamilyNotFound)
1007 ERR("Missing 'Courier New' font\n");
1008
1009 return stat;
1010 }
1011
1012 /*****************************************************************************
1013 * GdipGetGenericFontFamilySerif [GDIPLUS.@]
1014 *
1015 * Obtains a serif family (Times New Roman on Windows)
1016 *
1017 * PARAMS
1018 * **nativeFamily [I] Where the font will be stored
1019 *
1020 * RETURNS
1021 * InvalidParameter if nativeFamily is NULL.
1022 * Ok otherwise.
1023 */
1024 GpStatus WINGDIPAPI GdipGetGenericFontFamilySerif(GpFontFamily **nativeFamily)
1025 {
1026 static const WCHAR TimesNewRoman[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n','\0'};
1027 static const WCHAR LiberationSerif[] = {'L','i','b','e','r','a','t','i','o','n',' ','S','e','r','i','f','\0'};
1028 GpStatus stat;
1029
1030 TRACE("(%p)\n", nativeFamily);
1031
1032 if (nativeFamily == NULL) return InvalidParameter;
1033
1034 stat = GdipCreateFontFamilyFromName(TimesNewRoman, NULL, nativeFamily);
1035
1036 if (stat == FontFamilyNotFound)
1037 stat = GdipCreateFontFamilyFromName(LiberationSerif, NULL, nativeFamily);
1038
1039 if (stat == FontFamilyNotFound)
1040 ERR("Missing 'Times New Roman' font\n");
1041
1042 return stat;
1043 }
1044
1045 /*****************************************************************************
1046 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1047 *
1048 * Obtains a serif family (Microsoft Sans Serif on Windows)
1049 *
1050 * PARAMS
1051 * **nativeFamily [I] Where the font will be stored
1052 *
1053 * RETURNS
1054 * InvalidParameter if nativeFamily is NULL.
1055 * Ok otherwise.
1056 */
1057 GpStatus WINGDIPAPI GdipGetGenericFontFamilySansSerif(GpFontFamily **nativeFamily)
1058 {
1059 GpStatus stat;
1060 static const WCHAR MicrosoftSansSerif[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
1061 static const WCHAR Tahoma[] = {'T','a','h','o','m','a','\0'};
1062
1063 TRACE("(%p)\n", nativeFamily);
1064
1065 if (nativeFamily == NULL) return InvalidParameter;
1066
1067 stat = GdipCreateFontFamilyFromName(MicrosoftSansSerif, NULL, nativeFamily);
1068
1069 if (stat == FontFamilyNotFound)
1070 /* FIXME: Microsoft Sans Serif is not installed on Wine. */
1071 stat = GdipCreateFontFamilyFromName(Tahoma, NULL, nativeFamily);
1072
1073 return stat;
1074 }
1075
1076 /*****************************************************************************
1077 * GdipGetGenericFontFamilySansSerif [GDIPLUS.@]
1078 */
1079 GpStatus WINGDIPAPI GdipNewPrivateFontCollection(GpFontCollection** fontCollection)
1080 {
1081 TRACE("%p\n", fontCollection);
1082
1083 if (!fontCollection)
1084 return InvalidParameter;
1085
1086 *fontCollection = heap_alloc_zero(sizeof(GpFontCollection));
1087 if (!*fontCollection) return OutOfMemory;
1088
1089 (*fontCollection)->FontFamilies = NULL;
1090 (*fontCollection)->count = 0;
1091 (*fontCollection)->allocated = 0;
1092
1093 TRACE("<-- %p\n", *fontCollection);
1094
1095 return Ok;
1096 }
1097
1098 /*****************************************************************************
1099 * GdipDeletePrivateFontCollection [GDIPLUS.@]
1100 */
1101 GpStatus WINGDIPAPI GdipDeletePrivateFontCollection(GpFontCollection **fontCollection)
1102 {
1103 INT i;
1104
1105 TRACE("%p\n", fontCollection);
1106
1107 if (!fontCollection)
1108 return InvalidParameter;
1109
1110 for (i = 0; i < (*fontCollection)->count; i++) heap_free((*fontCollection)->FontFamilies[i]);
1111 heap_free(*fontCollection);
1112
1113 return Ok;
1114 }
1115
1116 /*****************************************************************************
1117 * GdipPrivateAddFontFile [GDIPLUS.@]
1118 */
1119 GpStatus WINGDIPAPI GdipPrivateAddFontFile(GpFontCollection *collection, GDIPCONST WCHAR *name)
1120 {
1121 HANDLE file, mapping;
1122 LARGE_INTEGER size;
1123 void *mem;
1124 GpStatus status;
1125
1126 TRACE("%p, %s\n", collection, debugstr_w(name));
1127
1128 if (!collection || !name) return InvalidParameter;
1129
1130 file = CreateFileW(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1131 if (file == INVALID_HANDLE_VALUE) return InvalidParameter;
1132
1133 if (!GetFileSizeEx(file, &size) || size.u.HighPart)
1134 {
1135 CloseHandle(file);
1136 return InvalidParameter;
1137 }
1138
1139 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
1140 CloseHandle(file);
1141 if (!mapping) return InvalidParameter;
1142
1143 mem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1144 CloseHandle(mapping);
1145 if (!mem) return InvalidParameter;
1146
1147 /* GdipPrivateAddMemoryFont creates a copy of the memory block */
1148 status = GdipPrivateAddMemoryFont(collection, mem, size.u.LowPart);
1149 UnmapViewOfFile(mem);
1150
1151 return status;
1152 }
1153
1154 #define TT_PLATFORM_APPLE_UNICODE 0
1155 #define TT_PLATFORM_MACINTOSH 1
1156 #define TT_PLATFORM_MICROSOFT 3
1157
1158 #define TT_APPLE_ID_DEFAULT 0
1159 #define TT_APPLE_ID_ISO_10646 2
1160 #define TT_APPLE_ID_UNICODE_2_0 3
1161
1162 #define TT_MS_ID_SYMBOL_CS 0
1163 #define TT_MS_ID_UNICODE_CS 1
1164
1165 #define TT_MAC_ID_SIMPLIFIED_CHINESE 25
1166
1167 #define NAME_ID_FULL_FONT_NAME 4
1168
1169 typedef struct {
1170 USHORT major_version;
1171 USHORT minor_version;
1172 USHORT tables_no;
1173 USHORT search_range;
1174 USHORT entry_selector;
1175 USHORT range_shift;
1176 } tt_header;
1177
1178 typedef struct {
1179 char tag[4]; /* table name */
1180 ULONG check_sum; /* Check sum */
1181 ULONG offset; /* Offset from beginning of file */
1182 ULONG length; /* length of the table in bytes */
1183 } tt_table_directory;
1184
1185 typedef struct {
1186 USHORT format; /* format selector. Always 0 */
1187 USHORT count; /* Name Records count */
1188 USHORT string_offset; /* Offset for strings storage, * from start of the table */
1189 } tt_name_table;
1190
1191 typedef struct {
1192 USHORT platform_id;
1193 USHORT encoding_id;
1194 USHORT language_id;
1195 USHORT name_id;
1196 USHORT length;
1197 USHORT offset; /* from start of storage area */
1198 } tt_name_record;
1199
1200 /* Copied from gdi32/freetype.c */
1201
1202 static const LANGID mac_langid_table[] =
1203 {
1204 MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
1205 MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FRENCH */
1206 MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GERMAN */
1207 MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ITALIAN */
1208 MAKELANGID(LANG_DUTCH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DUTCH */
1209 MAKELANGID(LANG_SWEDISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWEDISH */
1210 MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SPANISH */
1211 MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_DANISH */
1212 MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PORTUGUESE */
1213 MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NORWEGIAN */
1214 MAKELANGID(LANG_HEBREW,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HEBREW */
1215 MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_JAPANESE */
1216 MAKELANGID(LANG_ARABIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARABIC */
1217 MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FINNISH */
1218 MAKELANGID(LANG_GREEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREEK */
1219 MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ICELANDIC */
1220 MAKELANGID(LANG_MALTESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALTESE */
1221 MAKELANGID(LANG_TURKISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKISH */
1222 MAKELANGID(LANG_CROATIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CROATIAN */
1223 MAKELANGID(LANG_CHINESE_TRADITIONAL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_TRADITIONAL */
1224 MAKELANGID(LANG_URDU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_URDU */
1225 MAKELANGID(LANG_HINDI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HINDI */
1226 MAKELANGID(LANG_THAI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_THAI */
1227 MAKELANGID(LANG_KOREAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KOREAN */
1228 MAKELANGID(LANG_LITHUANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LITHUANIAN */
1229 MAKELANGID(LANG_POLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_POLISH */
1230 MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_HUNGARIAN */
1231 MAKELANGID(LANG_ESTONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESTONIAN */
1232 MAKELANGID(LANG_LATVIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LETTISH */
1233 MAKELANGID(LANG_SAMI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SAAMISK */
1234 MAKELANGID(LANG_FAEROESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FAEROESE */
1235 MAKELANGID(LANG_FARSI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_FARSI */
1236 MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_RUSSIAN */
1237 MAKELANGID(LANG_CHINESE_SIMPLIFIED,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CHINESE_SIMPLIFIED */
1238 MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), /* TT_MAC_LANGID_FLEMISH */
1239 MAKELANGID(LANG_IRISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_IRISH */
1240 MAKELANGID(LANG_ALBANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ALBANIAN */
1241 MAKELANGID(LANG_ROMANIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ROMANIAN */
1242 MAKELANGID(LANG_CZECH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CZECH */
1243 MAKELANGID(LANG_SLOVAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVAK */
1244 MAKELANGID(LANG_SLOVENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SLOVENIAN */
1245 0, /* TT_MAC_LANGID_YIDDISH */
1246 MAKELANGID(LANG_SERBIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SERBIAN */
1247 MAKELANGID(LANG_MACEDONIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MACEDONIAN */
1248 MAKELANGID(LANG_BULGARIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BULGARIAN */
1249 MAKELANGID(LANG_UKRAINIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UKRAINIAN */
1250 MAKELANGID(LANG_BELARUSIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BYELORUSSIAN */
1251 MAKELANGID(LANG_UZBEK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UZBEK */
1252 MAKELANGID(LANG_KAZAK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KAZAKH */
1253 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_CYRILLIC), /* TT_MAC_LANGID_AZERBAIJANI */
1254 0, /* TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT */
1255 MAKELANGID(LANG_ARMENIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ARMENIAN */
1256 MAKELANGID(LANG_GEORGIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GEORGIAN */
1257 0, /* TT_MAC_LANGID_MOLDAVIAN */
1258 MAKELANGID(LANG_KYRGYZ,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KIRGHIZ */
1259 MAKELANGID(LANG_TAJIK,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAJIKI */
1260 MAKELANGID(LANG_TURKMEN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TURKMEN */
1261 MAKELANGID(LANG_MONGOLIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MONGOLIAN */
1262 MAKELANGID(LANG_MONGOLIAN,SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA), /* TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT */
1263 MAKELANGID(LANG_PASHTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PASHTO */
1264 0, /* TT_MAC_LANGID_KURDISH */
1265 MAKELANGID(LANG_KASHMIRI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KASHMIRI */
1266 MAKELANGID(LANG_SINDHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINDHI */
1267 MAKELANGID(LANG_TIBETAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIBETAN */
1268 MAKELANGID(LANG_NEPALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_NEPALI */
1269 MAKELANGID(LANG_SANSKRIT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SANSKRIT */
1270 MAKELANGID(LANG_MARATHI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MARATHI */
1271 MAKELANGID(LANG_BENGALI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BENGALI */
1272 MAKELANGID(LANG_ASSAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ASSAMESE */
1273 MAKELANGID(LANG_GUJARATI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GUJARATI */
1274 MAKELANGID(LANG_PUNJABI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_PUNJABI */
1275 MAKELANGID(LANG_ORIYA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ORIYA */
1276 MAKELANGID(LANG_MALAYALAM,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAYALAM */
1277 MAKELANGID(LANG_KANNADA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KANNADA */
1278 MAKELANGID(LANG_TAMIL,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TAMIL */
1279 MAKELANGID(LANG_TELUGU,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TELUGU */
1280 MAKELANGID(LANG_SINHALESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SINHALESE */
1281 0, /* TT_MAC_LANGID_BURMESE */
1282 MAKELANGID(LANG_KHMER,SUBLANG_DEFAULT), /* TT_MAC_LANGID_KHMER */
1283 MAKELANGID(LANG_LAO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_LAO */
1284 MAKELANGID(LANG_VIETNAMESE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_VIETNAMESE */
1285 MAKELANGID(LANG_INDONESIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INDONESIAN */
1286 0, /* TT_MAC_LANGID_TAGALOG */
1287 MAKELANGID(LANG_MALAY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAY_ROMAN_SCRIPT */
1288 0, /* TT_MAC_LANGID_MALAY_ARABIC_SCRIPT */
1289 MAKELANGID(LANG_AMHARIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AMHARIC */
1290 MAKELANGID(LANG_TIGRIGNA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TIGRINYA */
1291 0, /* TT_MAC_LANGID_GALLA */
1292 0, /* TT_MAC_LANGID_SOMALI */
1293 MAKELANGID(LANG_SWAHILI,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SWAHILI */
1294 0, /* TT_MAC_LANGID_RUANDA */
1295 0, /* TT_MAC_LANGID_RUNDI */
1296 0, /* TT_MAC_LANGID_CHEWA */
1297 MAKELANGID(LANG_MALAGASY,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MALAGASY */
1298 MAKELANGID(LANG_ESPERANTO,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ESPERANTO */
1299 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 95-111 */
1300 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112-127 */
1301 MAKELANGID(LANG_WELSH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_WELSH */
1302 MAKELANGID(LANG_BASQUE,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BASQUE */
1303 MAKELANGID(LANG_CATALAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_CATALAN */
1304 0, /* TT_MAC_LANGID_LATIN */
1305 MAKELANGID(LANG_QUECHUA,SUBLANG_DEFAULT), /* TT_MAC_LANGID_QUECHUA */
1306 0, /* TT_MAC_LANGID_GUARANI */
1307 0, /* TT_MAC_LANGID_AYMARA */
1308 MAKELANGID(LANG_TATAR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_TATAR */
1309 MAKELANGID(LANG_UIGHUR,SUBLANG_DEFAULT), /* TT_MAC_LANGID_UIGHUR */
1310 0, /* TT_MAC_LANGID_DZONGKHA */
1311 0, /* TT_MAC_LANGID_JAVANESE */
1312 0, /* TT_MAC_LANGID_SUNDANESE */
1313 MAKELANGID(LANG_GALICIAN,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GALICIAN */
1314 MAKELANGID(LANG_AFRIKAANS,SUBLANG_DEFAULT), /* TT_MAC_LANGID_AFRIKAANS */
1315 MAKELANGID(LANG_BRETON,SUBLANG_DEFAULT), /* TT_MAC_LANGID_BRETON */
1316 MAKELANGID(LANG_INUKTITUT,SUBLANG_DEFAULT), /* TT_MAC_LANGID_INUKTITUT */
1317 MAKELANGID(LANG_SCOTTISH_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_SCOTTISH_GAELIC */
1318 MAKELANGID(LANG_MANX_GAELIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_MANX_GAELIC */
1319 MAKELANGID(LANG_IRISH,SUBLANG_IRISH_IRELAND), /* TT_MAC_LANGID_IRISH_GAELIC */
1320 0, /* TT_MAC_LANGID_TONGAN */
1321 0, /* TT_MAC_LANGID_GREEK_POLYTONIC */
1322 MAKELANGID(LANG_GREENLANDIC,SUBLANG_DEFAULT), /* TT_MAC_LANGID_GREELANDIC */
1323 MAKELANGID(LANG_AZERI,SUBLANG_AZERI_LATIN), /* TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT */
1324 };
1325
1326 static inline WORD get_mac_code_page( const tt_name_record *name )
1327 {
1328 WORD encoding_id = GET_BE_WORD(name->encoding_id);
1329 if (encoding_id == TT_MAC_ID_SIMPLIFIED_CHINESE) return 10008; /* special case */
1330 return 10000 + encoding_id;
1331 }
1332
1333 static int match_name_table_language( const tt_name_record *name, LANGID lang )
1334 {
1335 LANGID name_lang;
1336
1337 switch (GET_BE_WORD(name->platform_id))
1338 {
1339 case TT_PLATFORM_MICROSOFT:
1340 switch (GET_BE_WORD(name->encoding_id))
1341 {
1342 case TT_MS_ID_UNICODE_CS:
1343 case TT_MS_ID_SYMBOL_CS:
1344 name_lang = GET_BE_WORD(name->language_id);
1345 break;
1346 default:
1347 return 0;
1348 }
1349 break;
1350 case TT_PLATFORM_MACINTOSH:
1351 if (!IsValidCodePage( get_mac_code_page( name ))) return 0;
1352 name_lang = GET_BE_WORD(name->language_id);
1353 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1354 name_lang = mac_langid_table[name_lang];
1355 break;
1356 case TT_PLATFORM_APPLE_UNICODE:
1357 switch (GET_BE_WORD(name->encoding_id))
1358 {
1359 case TT_APPLE_ID_DEFAULT:
1360 case TT_APPLE_ID_ISO_10646:
1361 case TT_APPLE_ID_UNICODE_2_0:
1362 name_lang = GET_BE_WORD(name->language_id);
1363 if (name_lang >= sizeof(mac_langid_table)/sizeof(mac_langid_table[0])) return 0;
1364 name_lang = mac_langid_table[name_lang];
1365 break;
1366 default:
1367 return 0;
1368 }
1369 break;
1370 default:
1371 return 0;
1372 }
1373 if (name_lang == lang) return 3;
1374 if (PRIMARYLANGID( name_lang ) == PRIMARYLANGID( lang )) return 2;
1375 if (name_lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1;
1376 return 0;
1377 }
1378
1379 static WCHAR *copy_name_table_string( const tt_name_record *name, const BYTE *data, WCHAR *ret, DWORD len )
1380 {
1381 WORD name_len = GET_BE_WORD(name->length);
1382 WORD codepage;
1383
1384 switch (GET_BE_WORD(name->platform_id))
1385 {
1386 case TT_PLATFORM_APPLE_UNICODE:
1387 case TT_PLATFORM_MICROSOFT:
1388 if (name_len >= len*sizeof(WCHAR))
1389 return NULL;
1390 for (len = 0; len < name_len / 2; len++)
1391 ret[len] = (data[len * 2] << 8) | data[len * 2 + 1];
1392 ret[len] = 0;
1393 return ret;
1394 case TT_PLATFORM_MACINTOSH:
1395 codepage = get_mac_code_page( name );
1396 len = MultiByteToWideChar( codepage, 0, (char *)data, name_len, ret, len-1 );
1397 if (!len)
1398 return NULL;
1399 ret[len] = 0;
1400 return ret;
1401 }
1402 return NULL;
1403 }
1404
1405 static WCHAR *load_ttf_name_id( const BYTE *mem, DWORD_PTR size, DWORD id, WCHAR *ret, DWORD len )
1406 {
1407 LANGID lang = GetSystemDefaultLangID();
1408 const tt_header *header;
1409 const tt_name_table *name_table;
1410 const tt_name_record *name_record;
1411 DWORD pos, ofs, count;
1412 int i, res, best_lang = 0, best_index = -1;
1413
1414 if (sizeof(tt_header) > size)
1415 return NULL;
1416 header = (const tt_header*)mem;
1417 count = GET_BE_WORD(header->tables_no);
1418
1419 if (GET_BE_WORD(header->major_version) != 1 || GET_BE_WORD(header->minor_version) != 0)
1420 return NULL;
1421
1422 pos = sizeof(*header);
1423 for (i = 0; i < count; i++)
1424 {
1425 const tt_table_directory *table_directory = (const tt_table_directory*)&mem[pos];
1426 pos += sizeof(*table_directory);
1427 if (memcmp(table_directory->tag, "name", 4) == 0)
1428 {
1429 ofs = GET_BE_DWORD(table_directory->offset);
1430 break;
1431 }
1432 }
1433 if (i >= count)
1434 return NULL;
1435
1436 if (ofs >= size)
1437 return NULL;
1438 pos = ofs + sizeof(*name_table);
1439 if (pos > size)
1440 return NULL;
1441 name_table = (const tt_name_table*)&mem[ofs];
1442 count = GET_BE_WORD(name_table->count);
1443 if (GET_BE_WORD(name_table->string_offset) >= size - ofs) return NULL;
1444 ofs += GET_BE_WORD(name_table->string_offset);
1445 for (i=0; i<count; i++)
1446 {
1447 name_record = (const tt_name_record*)&mem[pos];
1448 pos += sizeof(*name_record);
1449 if (pos > size)
1450 return NULL;
1451
1452 if (GET_BE_WORD(name_record->name_id) != id) continue;
1453 if (GET_BE_WORD(name_record->offset) >= size - ofs) return NULL;
1454 if (GET_BE_WORD(name_record->length) > size - ofs - GET_BE_WORD(name_record->offset)) return NULL;
1455
1456 res = match_name_table_language( name_record, lang );
1457 if (res > best_lang)
1458 {
1459 best_lang = res;
1460 best_index = i;
1461 }
1462 }
1463
1464 if (best_lang)
1465 {
1466 name_record = (const tt_name_record*)(name_table + 1) + best_index;
1467 ret = copy_name_table_string( name_record, mem+ofs+GET_BE_WORD(name_record->offset), ret, len );
1468 TRACE( "name %u found platform %u lang %04x %s\n", GET_BE_WORD(name_record->name_id),
1469 GET_BE_WORD(name_record->platform_id), GET_BE_WORD(name_record->language_id), debugstr_w( ret ));
1470 return ret;
1471 }
1472 return NULL;
1473 }
1474
1475 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm, DWORD type, LPARAM lParam);
1476
1477 /*****************************************************************************
1478 * GdipPrivateAddMemoryFont [GDIPLUS.@]
1479 */
1480 GpStatus WINGDIPAPI GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
1481 GDIPCONST void* memory, INT length)
1482 {
1483 WCHAR buf[32], *name;
1484 DWORD count = 0;
1485 HANDLE font;
1486 TRACE("%p, %p, %d\n", fontCollection, memory, length);
1487
1488 if (!fontCollection || !memory || !length)
1489 return InvalidParameter;
1490
1491 name = load_ttf_name_id(memory, length, NAME_ID_FULL_FONT_NAME, buf, sizeof(buf)/sizeof(*buf));
1492 if (!name)
1493 return OutOfMemory;
1494
1495 font = AddFontMemResourceEx((void*)memory, length, NULL, &count);
1496 TRACE("%s: %p/%u\n", debugstr_w(name), font, count);
1497 if (!font || !count)
1498 return InvalidParameter;
1499
1500 if (count)
1501 {
1502 HDC hdc;
1503 LOGFONTW lfw;
1504
1505 hdc = CreateCompatibleDC(0);
1506
1507 lfw.lfCharSet = DEFAULT_CHARSET;
1508 lstrcpyW(lfw.lfFaceName, name);
1509 lfw.lfPitchAndFamily = 0;
1510
1511 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)fontCollection, 0))
1512 {
1513 DeleteDC(hdc);
1514 return OutOfMemory;
1515 }
1516
1517 DeleteDC(hdc);
1518 }
1519 return Ok;
1520 }
1521
1522 /*****************************************************************************
1523 * GdipGetFontCollectionFamilyCount [GDIPLUS.@]
1524 */
1525 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyCount(
1526 GpFontCollection* fontCollection, INT* numFound)
1527 {
1528 TRACE("%p, %p\n", fontCollection, numFound);
1529
1530 if (!(fontCollection && numFound))
1531 return InvalidParameter;
1532
1533 *numFound = fontCollection->count;
1534 return Ok;
1535 }
1536
1537 /*****************************************************************************
1538 * GdipGetFontCollectionFamilyList [GDIPLUS.@]
1539 */
1540 GpStatus WINGDIPAPI GdipGetFontCollectionFamilyList(
1541 GpFontCollection* fontCollection, INT numSought,
1542 GpFontFamily* gpfamilies[], INT* numFound)
1543 {
1544 INT i;
1545 GpStatus stat=Ok;
1546
1547 TRACE("%p, %d, %p, %p\n", fontCollection, numSought, gpfamilies, numFound);
1548
1549 if (!(fontCollection && gpfamilies && numFound))
1550 return InvalidParameter;
1551
1552 memset(gpfamilies, 0, sizeof(*gpfamilies) * numSought);
1553
1554 for (i = 0; i < numSought && i < fontCollection->count && stat == Ok; i++)
1555 {
1556 stat = GdipCloneFontFamily(fontCollection->FontFamilies[i], &gpfamilies[i]);
1557 }
1558
1559 if (stat == Ok)
1560 *numFound = i;
1561 else
1562 {
1563 int numToFree=i;
1564 for (i=0; i<numToFree; i++)
1565 {
1566 GdipDeleteFontFamily(gpfamilies[i]);
1567 gpfamilies[i] = NULL;
1568 }
1569 }
1570
1571 return stat;
1572 }
1573
1574 void free_installed_fonts(void)
1575 {
1576 while (installedFontCollection.count)
1577 GdipDeleteFontFamily(installedFontCollection.FontFamilies[--installedFontCollection.count]);
1578 heap_free(installedFontCollection.FontFamilies);
1579 installedFontCollection.FontFamilies = NULL;
1580 installedFontCollection.allocated = 0;
1581 }
1582
1583 static INT CALLBACK add_font_proc(const LOGFONTW *lfw, const TEXTMETRICW *ntm,
1584 DWORD type, LPARAM lParam)
1585 {
1586 GpFontCollection* fonts = (GpFontCollection*)lParam;
1587 int i;
1588
1589 if (type == RASTER_FONTTYPE)
1590 return 1;
1591
1592 /* skip duplicates */
1593 for (i=0; i<fonts->count; i++)
1594 if (strcmpiW(lfw->lfFaceName, fonts->FontFamilies[i]->FamilyName) == 0)
1595 return 1;
1596
1597 if (fonts->allocated == fonts->count)
1598 {
1599 INT new_alloc_count = fonts->allocated+50;
1600 GpFontFamily** new_family_list = heap_alloc(new_alloc_count*sizeof(void*));
1601
1602 if (!new_family_list)
1603 return 0;
1604
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;
1609 }
1610
1611 if (GdipCreateFontFamilyFromName(lfw->lfFaceName, NULL, &fonts->FontFamilies[fonts->count]) == Ok)
1612 fonts->count++;
1613 else
1614 return 0;
1615
1616 return 1;
1617 }
1618
1619 GpStatus WINGDIPAPI GdipNewInstalledFontCollection(
1620 GpFontCollection** fontCollection)
1621 {
1622 TRACE("(%p)\n",fontCollection);
1623
1624 if (!fontCollection)
1625 return InvalidParameter;
1626
1627 if (installedFontCollection.count == 0)
1628 {
1629 HDC hdc;
1630 LOGFONTW lfw;
1631
1632 hdc = CreateCompatibleDC(0);
1633
1634 lfw.lfCharSet = DEFAULT_CHARSET;
1635 lfw.lfFaceName[0] = 0;
1636 lfw.lfPitchAndFamily = 0;
1637
1638 if (!EnumFontFamiliesExW(hdc, &lfw, add_font_proc, (LPARAM)&installedFontCollection, 0))
1639 {
1640 free_installed_fonts();
1641 DeleteDC(hdc);
1642 return OutOfMemory;
1643 }
1644
1645 DeleteDC(hdc);
1646 }
1647
1648 *fontCollection = &installedFontCollection;
1649
1650 return Ok;
1651 }