forked from wine-mirror/wine
/
font.c
1666 lines (1386 loc) · 53.8 KB
/
font.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007 Google (Evan Stade)
* Copyright (C) 2012 Dmitry Timoshkov
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winnls.h"
#include "winreg.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL (gdiplus);
#include "objbase.h"
#include "gdiplus.h"
#include "gdiplus_private.h"
/* PANOSE is 10 bytes in size, need to pack the structure properly */
#include "pshpack2.h"
typedef struct
{
USHORT version;
SHORT xAvgCharWidth;
USHORT usWeightClass;
USHORT usWidthClass;
SHORT fsType;
SHORT ySubscriptXSize;
SHORT ySubscriptYSize;
SHORT ySubscriptXOffset;
SHORT ySubscriptYOffset;
SHORT ySuperscriptXSize;
SHORT ySuperscriptYSize;
SHORT ySuperscriptXOffset;
SHORT ySuperscriptYOffset;
SHORT yStrikeoutSize;
SHORT yStrikeoutPosition;
SHORT sFamilyClass;
PANOSE panose;
ULONG ulUnicodeRange1;
ULONG ulUnicodeRange2;
ULONG ulUnicodeRange3;
ULONG ulUnicodeRange4;
CHAR achVendID[4];
USHORT fsSelection;
USHORT usFirstCharIndex;
USHORT usLastCharIndex;
/* According to the Apple spec, original version didn't have the below fields,
* version numbers were taken from the OpenType spec.
*/
/* version 0 (TrueType 1.5) */
USHORT sTypoAscender;
USHORT sTypoDescender;
USHORT sTypoLineGap;
USHORT usWinAscent;
USHORT usWinDescent;
/* version 1 (TrueType 1.66) */
ULONG ulCodePageRange1;
ULONG ulCodePageRange2;
/* version 2 (OpenType 1.2) */
SHORT sxHeight;
SHORT sCapHeight;
USHORT usDefaultChar;
USHORT usBreakChar;
USHORT usMaxContext;
} TT_OS2_V2;
typedef struct
{
ULONG Version;
SHORT Ascender;
SHORT Descender;
SHORT LineGap;
USHORT advanceWidthMax;
SHORT minLeftSideBearing;
SHORT minRightSideBearing;
SHORT xMaxExtent;
SHORT caretSlopeRise;
SHORT caretSlopeRun;
SHORT caretOffset;
SHORT reserved[4];
SHORT metricDataFormat;
USHORT numberOfHMetrics;
} TT_HHEA;
#include "poppack.h"
#ifdef WORDS_BIGENDIAN
#define GET_BE_WORD(x) (x)
#define GET_BE_DWORD(x) (x)
#else
#define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
#define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)));
#endif
#define MS_MAKE_TAG(ch0, ch1, ch2, ch3) \
((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
#define MS_OS2_TAG MS_MAKE_TAG('O','S','/','2')
#define MS_HHEA_TAG MS_MAKE_TAG('h','h','e','a')
static GpStatus clone_font_family(const GpFontFamily *, GpFontFamily **);
static GpFontCollection installedFontCollection = {0};
/*******************************************************************************
* GdipCreateFont [GDIPLUS.@]
*
* Create a new font based off of a FontFamily
*
* PARAMS
* *fontFamily [I] Family to base the font off of
* emSize [I] Size of the font
* style [I] Bitwise OR of FontStyle enumeration
* unit [I] Unit emSize is measured in
* **font [I] the resulting Font object
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter if fontfamily or font is NULL.
* FAILURE: FontFamilyNotFound if an invalid FontFamily is given
*
* NOTES
* UnitDisplay is unsupported.
* emSize is stored separately from lfHeight, to hold the fraction.
*/
GpStatus WINGDIPAPI GdipCreateFont(GDIPCONST GpFontFamily *fontFamily,
REAL emSize, INT style, Unit unit, GpFont **font)
{
HFONT hfont;
OUTLINETEXTMETRICW otm;
LOGFONTW lfw;
HDC hdc;
GpStatus stat;
int ret;
if (!fontFamily || !font || emSize < 0.0)
return InvalidParameter;
TRACE("%p (%s), %f, %d, %d, %p\n", fontFamily,
debugstr_w(fontFamily->FamilyName), emSize, style, unit, font);
memset(&lfw, 0, sizeof(lfw));
stat = GdipGetFamilyName(fontFamily, lfw.lfFaceName, LANG_NEUTRAL);
if (stat != Ok) return stat;
lfw.lfHeight = -units_to_pixels(emSize, unit, fontFamily->dpi);
lfw.lfWeight = style & FontStyleBold ? FW_BOLD : FW_REGULAR;
lfw.lfItalic = style & FontStyleItalic;
lfw.lfUnderline = style & FontStyleUnderline;
lfw.lfStrikeOut = style & FontStyleStrikeout;
hfont = CreateFontIndirectW(&lfw);
hdc = CreateCompatibleDC(0);
SelectObject(hdc, hfont);
otm.otmSize = sizeof(otm);
ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
DeleteDC(hdc);
DeleteObject(hfont);
if (!ret) return NotTrueTypeFont;
*font = heap_alloc_zero(sizeof(GpFont));
if (!*font) return OutOfMemory;
(*font)->unit = unit;
(*font)->emSize = emSize;
(*font)->otm = otm;
stat = clone_font_family(fontFamily, &(*font)->family);
if (stat != Ok)
{
heap_free(*font);
return stat;
}
TRACE("<-- %p\n", *font);
return Ok;
}
/*******************************************************************************
* GdipCreateFontFromLogfontW [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipCreateFontFromLogfontW(HDC hdc,
GDIPCONST LOGFONTW *logfont, GpFont **font)
{
HFONT hfont, oldfont;
OUTLINETEXTMETRICW otm;
WCHAR facename[LF_FACESIZE];
GpStatus stat;
int ret;
TRACE("(%p, %p, %p)\n", hdc, logfont, font);
if (!hdc || !logfont || !font)
return InvalidParameter;
hfont = CreateFontIndirectW(logfont);
oldfont = SelectObject(hdc, hfont);
otm.otmSize = sizeof(otm);
ret = GetOutlineTextMetricsW(hdc, otm.otmSize, &otm);
GetTextFaceW(hdc, LF_FACESIZE, facename);
SelectObject(hdc, oldfont);
DeleteObject(hfont);
if (!ret) return NotTrueTypeFont;
*font = heap_alloc_zero(sizeof(GpFont));
if (!*font) return OutOfMemory;
(*font)->unit = UnitWorld;
(*font)->emSize = otm.otmTextMetrics.tmAscent;
(*font)->otm = otm;
stat = GdipCreateFontFamilyFromName(facename, NULL, &(*font)->family);
if (stat != Ok)
{
heap_free(*font);
return NotTrueTypeFont;
}
TRACE("<-- %p\n", *font);
return Ok;
}
/*******************************************************************************
* GdipCreateFontFromLogfontA [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipCreateFontFromLogfontA(HDC hdc,
GDIPCONST LOGFONTA *lfa, GpFont **font)
{
LOGFONTW lfw;
TRACE("(%p, %p, %p)\n", hdc, lfa, font);
if(!lfa || !font)
return InvalidParameter;
memcpy(&lfw, lfa, FIELD_OFFSET(LOGFONTA,lfFaceName) );
if(!MultiByteToWideChar(CP_ACP, 0, lfa->lfFaceName, -1, lfw.lfFaceName, LF_FACESIZE))
return GenericError;
return GdipCreateFontFromLogfontW(hdc, &lfw, font);
}
/*******************************************************************************
* GdipDeleteFont [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipDeleteFont(GpFont* font)
{
TRACE("(%p)\n", font);
if(!font)
return InvalidParameter;
GdipDeleteFontFamily(font->family);
heap_free(font);
return Ok;
}
/*******************************************************************************
* GdipCreateFontFromDC [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipCreateFontFromDC(HDC hdc, GpFont **font)
{
HFONT hfont;
LOGFONTW lfw;
TRACE("(%p, %p)\n", hdc, font);
if(!font)
return InvalidParameter;
hfont = GetCurrentObject(hdc, OBJ_FONT);
if(!hfont)
return GenericError;
if(!GetObjectW(hfont, sizeof(LOGFONTW), &lfw))
return GenericError;
return GdipCreateFontFromLogfontW(hdc, &lfw, font);
}
/*******************************************************************************
* GdipGetFamily [GDIPLUS.@]
*
* Returns the FontFamily for the specified Font
*
* PARAMS
* font [I] Font to request from
* family [O] Resulting FontFamily object
*
* RETURNS
* SUCCESS: Ok
* FAILURE: An element of GpStatus
*/
GpStatus WINGDIPAPI GdipGetFamily(GpFont *font, GpFontFamily **family)
{
TRACE("%p %p\n", font, family);
if (!(font && family))
return InvalidParameter;
return GdipCloneFontFamily(font->family, family);
}
static REAL get_font_size(const GpFont *font)
{
return font->emSize;
}
/******************************************************************************
* GdipGetFontSize [GDIPLUS.@]
*
* Returns the size of the font in Units
*
* PARAMS
* *font [I] The font to retrieve size from
* *size [O] Pointer to hold retrieved value
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter (font or size was NULL)
*
* NOTES
* Size returned is actually emSize -- not internal size used for drawing.
*/
GpStatus WINGDIPAPI GdipGetFontSize(GpFont *font, REAL *size)
{
TRACE("(%p, %p)\n", font, size);
if (!(font && size)) return InvalidParameter;
*size = get_font_size(font);
TRACE("%s,%d => %f\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *size);
return Ok;
}
static INT get_font_style(const GpFont *font)
{
INT style;
if (font->otm.otmTextMetrics.tmWeight > FW_REGULAR)
style = FontStyleBold;
else
style = FontStyleRegular;
if (font->otm.otmTextMetrics.tmItalic)
style |= FontStyleItalic;
if (font->otm.otmTextMetrics.tmUnderlined)
style |= FontStyleUnderline;
if (font->otm.otmTextMetrics.tmStruckOut)
style |= FontStyleStrikeout;
return style;
}
/*******************************************************************************
* GdipGetFontStyle [GDIPLUS.@]
*
* Gets the font's style, returned in bitwise OR of FontStyle enumeration
*
* PARAMS
* font [I] font to request from
* style [O] resulting pointer to a FontStyle enumeration
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter
*/
GpStatus WINGDIPAPI GdipGetFontStyle(GpFont *font, INT *style)
{
TRACE("%p %p\n", font, style);
if (!(font && style))
return InvalidParameter;
*style = get_font_style(font);
TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *style);
return Ok;
}
/*******************************************************************************
* GdipGetFontUnit [GDIPLUS.@]
*
* PARAMS
* font [I] Font to retrieve from
* unit [O] Return value
*
* RETURNS
* FAILURE: font or unit was NULL
* OK: otherwise
*/
GpStatus WINGDIPAPI GdipGetFontUnit(GpFont *font, Unit *unit)
{
TRACE("(%p, %p)\n", font, unit);
if (!(font && unit)) return InvalidParameter;
*unit = font->unit;
TRACE("%s,%d => %d\n", debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *unit);
return Ok;
}
/*******************************************************************************
* GdipGetLogFontA [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipGetLogFontA(GpFont *font, GpGraphics *graphics,
LOGFONTA *lfa)
{
GpStatus status;
LOGFONTW lfw;
TRACE("(%p, %p, %p)\n", font, graphics, lfa);
status = GdipGetLogFontW(font, graphics, &lfw);
if(status != Ok)
return status;
memcpy(lfa, &lfw, FIELD_OFFSET(LOGFONTA,lfFaceName) );
if(!WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1, lfa->lfFaceName, LF_FACESIZE, NULL, NULL))
return GenericError;
return Ok;
}
/*******************************************************************************
* GdipGetLogFontW [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipGetLogFontW(GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
{
REAL angle, rel_height, height;
GpMatrix matrix;
GpPointF pt[3];
TRACE("(%p, %p, %p)\n", font, graphics, lf);
if (!font || !graphics || !lf)
return InvalidParameter;
matrix = graphics->worldtrans;
if (font->unit == UnitPixel || font->unit == UnitWorld)
{
height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
if (graphics->unit != UnitDisplay)
GdipScaleMatrix(&matrix, graphics->scale, graphics->scale, MatrixOrderAppend);
}
else
{
if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
height = units_to_pixels(font->emSize, font->unit, graphics->xres);
else
height = units_to_pixels(font->emSize, font->unit, graphics->yres);
}
pt[0].X = 0.0;
pt[0].Y = 0.0;
pt[1].X = 1.0;
pt[1].Y = 0.0;
pt[2].X = 0.0;
pt[2].Y = 1.0;
GdipTransformMatrixPoints(&matrix, pt, 3);
angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
rel_height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y)+
(pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
lf->lfHeight = -gdip_round(height * rel_height);
lf->lfWidth = 0;
lf->lfEscapement = lf->lfOrientation = gdip_round((angle / M_PI) * 1800.0);
if (lf->lfEscapement < 0)
{
lf->lfEscapement += 3600;
lf->lfOrientation += 3600;
}
lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf->lfQuality = DEFAULT_QUALITY;
lf->lfPitchAndFamily = 0;
strcpyW(lf->lfFaceName, font->family->FamilyName);
TRACE("=> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfHeight);
return Ok;
}
/*******************************************************************************
* GdipCloneFont [GDIPLUS.@]
*/
GpStatus WINGDIPAPI GdipCloneFont(GpFont *font, GpFont **cloneFont)
{
GpStatus stat;
TRACE("(%p, %p)\n", font, cloneFont);
if(!font || !cloneFont)
return InvalidParameter;
*cloneFont = heap_alloc_zero(sizeof(GpFont));
if(!*cloneFont) return OutOfMemory;
**cloneFont = *font;
stat = GdipCloneFontFamily(font->family, &(*cloneFont)->family);
if (stat != Ok) heap_free(*cloneFont);
return stat;
}
/*******************************************************************************
* GdipGetFontHeight [GDIPLUS.@]
* PARAMS
* font [I] Font to retrieve height from
* graphics [I] The current graphics context
* height [O] Resulting height
* RETURNS
* SUCCESS: Ok
* FAILURE: Another element of GpStatus
*
* NOTES
* Forwards to GdipGetFontHeightGivenDPI
*/
GpStatus WINGDIPAPI GdipGetFontHeight(GDIPCONST GpFont *font,
GDIPCONST GpGraphics *graphics, REAL *height)
{
REAL dpi;
GpStatus stat;
REAL font_height;
TRACE("%p %p %p\n", font, graphics, height);
stat = GdipGetFontHeightGivenDPI(font, font->family->dpi, &font_height);
if (stat != Ok) return stat;
if (!graphics)
{
*height = font_height;
TRACE("%s,%d => %f\n",
debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
return Ok;
}
stat = GdipGetDpiY((GpGraphics *)graphics, &dpi);
if (stat != Ok) return stat;
*height = pixels_to_units(font_height, graphics->unit, dpi);
TRACE("%s,%d(unit %d) => %f\n",
debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, graphics->unit, *height);
return Ok;
}
/*******************************************************************************
* GdipGetFontHeightGivenDPI [GDIPLUS.@]
* PARAMS
* font [I] Font to retrieve DPI from
* dpi [I] DPI to assume
* height [O] Return value
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter if font or height is NULL
*
* NOTES
* According to MSDN, the result is (lineSpacing)*(fontSize / emHeight)*dpi
* (for anything other than unit Pixel)
*/
GpStatus WINGDIPAPI GdipGetFontHeightGivenDPI(GDIPCONST GpFont *font, REAL dpi, REAL *height)
{
GpStatus stat;
INT style;
UINT16 line_spacing, em_height;
REAL font_size;
if (!font || !height) return InvalidParameter;
TRACE("%p (%s), %f, %p\n", font,
debugstr_w(font->family->FamilyName), dpi, height);
font_size = units_to_pixels(get_font_size(font), font->unit, dpi);
style = get_font_style(font);
stat = GdipGetLineSpacing(font->family, style, &line_spacing);
if (stat != Ok) return stat;
stat = GdipGetEmHeight(font->family, style, &em_height);
if (stat != Ok) return stat;
*height = (REAL)line_spacing * font_size / (REAL)em_height;
TRACE("%s,%d => %f\n",
debugstr_w(font->family->FamilyName), font->otm.otmTextMetrics.tmHeight, *height);
return Ok;
}
/***********************************************************************
* Borrowed from GDI32:
*
* Elf is really an ENUMLOGFONTEXW, and ntm is a NEWTEXTMETRICEXW.
* We have to use other types because of the FONTENUMPROCW definition.
*/
static INT CALLBACK is_font_installed_proc(const LOGFONTW *elf,
const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
{
const ENUMLOGFONTW *elfW = (const ENUMLOGFONTW *)elf;
LOGFONTW *lf = (LOGFONTW *)lParam;
if (type & RASTER_FONTTYPE)
return 1;
*lf = *elf;
/* replace substituted font name by a real one */
lstrcpynW(lf->lfFaceName, elfW->elfFullName, LF_FACESIZE);
return 0;
}
struct font_metrics
{
WCHAR facename[LF_FACESIZE];
UINT16 em_height, ascent, descent, line_spacing; /* in font units */
int dpi;
};
static BOOL get_font_metrics(HDC hdc, struct font_metrics *fm)
{
OUTLINETEXTMETRICW otm;
TT_OS2_V2 tt_os2;
TT_HHEA tt_hori;
LONG size;
UINT16 line_gap;
otm.otmSize = sizeof(otm);
if (!GetOutlineTextMetricsW(hdc, otm.otmSize, &otm)) return FALSE;
fm->em_height = otm.otmEMSquare;
fm->dpi = GetDeviceCaps(hdc, LOGPIXELSY);
memset(&tt_hori, 0, sizeof(tt_hori));
if (GetFontData(hdc, MS_HHEA_TAG, 0, &tt_hori, sizeof(tt_hori)) != GDI_ERROR)
{
fm->ascent = GET_BE_WORD(tt_hori.Ascender);
fm->descent = -GET_BE_WORD(tt_hori.Descender);
TRACE("hhea: ascent %d, descent %d\n", fm->ascent, fm->descent);
line_gap = GET_BE_WORD(tt_hori.LineGap);
fm->line_spacing = fm->ascent + fm->descent + line_gap;
TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
if (fm->ascent + fm->descent != 0) return TRUE;
}
size = GetFontData(hdc, MS_OS2_TAG, 0, NULL, 0);
if (size == GDI_ERROR) return FALSE;
if (size > sizeof(tt_os2)) size = sizeof(tt_os2);
memset(&tt_os2, 0, sizeof(tt_os2));
if (GetFontData(hdc, MS_OS2_TAG, 0, &tt_os2, size) != size) return FALSE;
fm->ascent = GET_BE_WORD(tt_os2.usWinAscent);
fm->descent = GET_BE_WORD(tt_os2.usWinDescent);
TRACE("usWinAscent %u, usWinDescent %u\n", fm->ascent, fm->descent);
if (fm->ascent + fm->descent == 0)
{
fm->ascent = GET_BE_WORD(tt_os2.sTypoAscender);
fm->descent = GET_BE_WORD(tt_os2.sTypoDescender);
TRACE("sTypoAscender %u, sTypoDescender %u\n", fm->ascent, fm->descent);
}
line_gap = GET_BE_WORD(tt_os2.sTypoLineGap);
fm->line_spacing = fm->ascent + fm->descent + line_gap;
TRACE("line_gap %u, line_spacing %u\n", line_gap, fm->line_spacing);
return TRUE;
}
static GpStatus find_installed_font(const WCHAR *name, struct font_metrics *fm)
{
LOGFONTW lf;
HDC hdc = CreateCompatibleDC(0);
GpStatus ret = FontFamilyNotFound;
if(!EnumFontFamiliesW(hdc, name, is_font_installed_proc, (LPARAM)&lf))
{
HFONT hfont, old_font;
strcpyW(fm->facename, lf.lfFaceName);
hfont = CreateFontIndirectW(&lf);
old_font = SelectObject(hdc, hfont);
ret = get_font_metrics(hdc, fm) ? Ok : NotTrueTypeFont;
SelectObject(hdc, old_font);
DeleteObject(hfont);
}
DeleteDC(hdc);
return ret;
}
/*******************************************************************************
* GdipCreateFontFamilyFromName [GDIPLUS.@]
*
* Creates a font family object based on a supplied name
*
* PARAMS
* name [I] Name of the font
* fontCollection [I] What font collection (if any) the font belongs to (may be NULL)
* FontFamily [O] Pointer to the resulting FontFamily object
*
* RETURNS
* SUCCESS: Ok
* FAILURE: FamilyNotFound if the requested FontFamily does not exist on the system
* FAILURE: Invalid parameter if FontFamily or name is NULL
*
* NOTES
* If fontCollection is NULL then the object is not part of any collection
*
*/
GpStatus WINGDIPAPI GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
GpFontCollection *fontCollection,
GpFontFamily **FontFamily)
{
GpStatus stat;
GpFontFamily* ffamily;
struct font_metrics fm;
TRACE("%s, %p %p\n", debugstr_w(name), fontCollection, FontFamily);
if (!(name && FontFamily))
return InvalidParameter;
if (fontCollection)
FIXME("No support for FontCollections yet!\n");
stat = find_installed_font(name, &fm);
if (stat != Ok) return stat;
ffamily = heap_alloc_zero(sizeof (GpFontFamily));
if (!ffamily) return OutOfMemory;
lstrcpyW(ffamily->FamilyName, fm.facename);
ffamily->em_height = fm.em_height;
ffamily->ascent = fm.ascent;
ffamily->descent = fm.descent;
ffamily->line_spacing = fm.line_spacing;
ffamily->dpi = fm.dpi;
*FontFamily = ffamily;
TRACE("<-- %p\n", ffamily);
return Ok;
}
static GpStatus clone_font_family(const GpFontFamily *family, GpFontFamily **clone)
{
*clone = heap_alloc_zero(sizeof(GpFontFamily));
if (!*clone) return OutOfMemory;
**clone = *family;
return Ok;
}
/*******************************************************************************
* GdipCloneFontFamily [GDIPLUS.@]
*
* Creates a deep copy of a Font Family object
*
* PARAMS
* FontFamily [I] Font to clone
* clonedFontFamily [O] The resulting cloned font
*
* RETURNS
* SUCCESS: Ok
*/
GpStatus WINGDIPAPI GdipCloneFontFamily(GpFontFamily* FontFamily, GpFontFamily** clonedFontFamily)
{
GpStatus status;
if (!(FontFamily && clonedFontFamily)) return InvalidParameter;
TRACE("%p (%s), %p\n", FontFamily,
debugstr_w(FontFamily->FamilyName), clonedFontFamily);
status = clone_font_family(FontFamily, clonedFontFamily);
if (status != Ok) return status;
TRACE("<-- %p\n", *clonedFontFamily);
return Ok;
}
/*******************************************************************************
* GdipGetFamilyName [GDIPLUS.@]
*
* Returns the family name into name
*
* PARAMS
* *family [I] Family to retrieve from
* *name [O] WCHARS of the family name
* LANGID [I] charset
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter if family is NULL
*
* NOTES
* If name is a NULL ptr, then both XP and Vista will crash (so we do as well)
*/
GpStatus WINGDIPAPI GdipGetFamilyName (GDIPCONST GpFontFamily *family,
WCHAR *name, LANGID language)
{
static int lang_fixme;
if (family == NULL)
return InvalidParameter;
TRACE("%p, %p, %d\n", family, name, language);
if (language != LANG_NEUTRAL && !lang_fixme++)
FIXME("No support for handling of multiple languages!\n");
lstrcpynW (name, family->FamilyName, LF_FACESIZE);
return Ok;
}
/*****************************************************************************
* GdipDeleteFontFamily [GDIPLUS.@]
*
* Removes the specified FontFamily
*
* PARAMS
* *FontFamily [I] The family to delete
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter if FontFamily is NULL.
*
*/
GpStatus WINGDIPAPI GdipDeleteFontFamily(GpFontFamily *FontFamily)
{
if (!FontFamily)
return InvalidParameter;
TRACE("Deleting %p (%s)\n", FontFamily, debugstr_w(FontFamily->FamilyName));
heap_free (FontFamily);
return Ok;
}
GpStatus WINGDIPAPI GdipGetCellAscent(GDIPCONST GpFontFamily *family,
INT style, UINT16* CellAscent)
{
if (!(family && CellAscent)) return InvalidParameter;
*CellAscent = family->ascent;
TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellAscent);
return Ok;
}
GpStatus WINGDIPAPI GdipGetCellDescent(GDIPCONST GpFontFamily *family,
INT style, UINT16* CellDescent)
{
TRACE("(%p, %d, %p)\n", family, style, CellDescent);
if (!(family && CellDescent)) return InvalidParameter;
*CellDescent = family->descent;
TRACE("%s => %u\n", debugstr_w(family->FamilyName), *CellDescent);
return Ok;
}
/*******************************************************************************
* GdipGetEmHeight [GDIPLUS.@]
*
* Gets the height of the specified family in EmHeights
*
* PARAMS
* family [I] Family to retrieve from
* style [I] (optional) style
* EmHeight [O] return value
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter
*/
GpStatus WINGDIPAPI GdipGetEmHeight(GDIPCONST GpFontFamily *family, INT style, UINT16* EmHeight)
{
if (!(family && EmHeight)) return InvalidParameter;
TRACE("%p (%s), %d, %p\n", family, debugstr_w(family->FamilyName), style, EmHeight);
*EmHeight = family->em_height;
TRACE("%s => %u\n", debugstr_w(family->FamilyName), *EmHeight);
return Ok;
}
/*******************************************************************************
* GdipGetLineSpacing [GDIPLUS.@]
*
* Returns the line spacing in design units
*
* PARAMS
* family [I] Family to retrieve from
* style [I] (Optional) font style
* LineSpacing [O] Return value
*
* RETURNS
* SUCCESS: Ok
* FAILURE: InvalidParameter (family or LineSpacing was NULL)
*/
GpStatus WINGDIPAPI GdipGetLineSpacing(GDIPCONST GpFontFamily *family,
INT style, UINT16* LineSpacing)
{
TRACE("%p, %d, %p\n", family, style, LineSpacing);
if (!(family && LineSpacing))
return InvalidParameter;
if (style) FIXME("ignoring style\n");
*LineSpacing = family->line_spacing;
TRACE("%s => %u\n", debugstr_w(family->FamilyName), *LineSpacing);
return Ok;
}
static INT CALLBACK font_has_style_proc(const LOGFONTW *elf,
const TEXTMETRICW *ntm, DWORD type, LPARAM lParam)
{
INT fontstyle = FontStyleRegular;
if (!ntm) return 1;
if (ntm->tmWeight >= FW_BOLD) fontstyle |= FontStyleBold;
if (ntm->tmItalic) fontstyle |= FontStyleItalic;
if (ntm->tmUnderlined) fontstyle |= FontStyleUnderline;
if (ntm->tmStruckOut) fontstyle |= FontStyleStrikeout;
return (INT)lParam != fontstyle;
}
GpStatus WINGDIPAPI GdipIsStyleAvailable(GDIPCONST GpFontFamily* family,
INT style, BOOL* IsStyleAvailable)
{
HDC hdc;
TRACE("%p %d %p\n", family, style, IsStyleAvailable);
if (!(family && IsStyleAvailable))
return InvalidParameter;
*IsStyleAvailable = FALSE;
hdc = CreateCompatibleDC(0);
if(!EnumFontFamiliesW(hdc, family->FamilyName, font_has_style_proc, (LPARAM)style))
*IsStyleAvailable = TRUE;
DeleteDC(hdc);
return Ok;
}
/*****************************************************************************
* GdipGetGenericFontFamilyMonospace [GDIPLUS.@]
*
* Obtains a serif family (Courier New on Windows)
*