2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: win32ss/gdi/ntgdi/freetype.c
5 * PURPOSE: FreeType font engine interface
6 * PROGRAMMERS: Copyright 2001 Huw D M Davies for CodeWeavers.
7 * Copyright 2006 Dmitry Timoshkov for CodeWeavers.
8 * Copyright 2016-2019 Katayama Hirofumi MZ.
11 /** Includes ******************************************************************/
16 #include FT_TYPE1_TABLES_H
17 #include FT_TRUETYPE_TABLES_H
18 #include FT_TRUETYPE_TAGS_H
19 #include FT_TRIGONOMETRY_H
22 #include FT_WINFONTS_H
23 #include FT_SFNT_NAMES_H
24 #include FT_SYNTHESIS_H
25 #include FT_TRUETYPE_IDS_H
27 #ifndef FT_INTERNAL_INTERNAL_H
28 #define FT_INTERNAL_INTERNAL_H <freetype/internal/internal.h>
29 #include FT_INTERNAL_INTERNAL_H
31 #include FT_INTERNAL_TRUETYPE_TYPES_H
33 #include <gdi/eng/floatobj.h>
39 /* TPMF_FIXED_PITCH is confusing; brain-dead api */
40 #ifndef _TMPF_VARIABLE_PITCH
41 #define _TMPF_VARIABLE_PITCH TMPF_FIXED_PITCH
44 extern const MATRIX gmxWorldToDeviceDefault
;
45 extern const MATRIX gmxWorldToPageDefault
;
46 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
48 /* HACK!! Fix XFORMOBJ then use 1:16 / 16:1 */
49 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
51 FT_Library g_FreeTypeLibrary
;
54 static UNICODE_STRING g_FontRegPath
=
55 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
58 /* The FreeType library is not thread safe, so we have
59 to serialize access to it */
60 static PFAST_MUTEX g_FreeTypeLock
;
62 static LIST_ENTRY g_FontListHead
;
63 static PFAST_MUTEX g_FontListLock
;
64 static BOOL g_RenderingEnabled
= TRUE
;
66 #define IntLockGlobalFonts() \
67 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(g_FontListLock)
69 #define IntUnLockGlobalFonts() \
70 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(g_FontListLock)
72 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
73 ASSERT(g_FontListLock->Owner == KeGetCurrentThread())
75 #define IntLockFreeType() \
76 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(g_FreeTypeLock)
78 #define IntUnLockFreeType() \
79 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(g_FreeTypeLock)
81 #define ASSERT_FREETYPE_LOCK_HELD() \
82 ASSERT(g_FreeTypeLock->Owner == KeGetCurrentThread())
84 #define ASSERT_FREETYPE_LOCK_NOT_HELD() \
85 ASSERT(g_FreeTypeLock->Owner != KeGetCurrentThread())
87 #define MAX_FONT_CACHE 256
89 static LIST_ENTRY g_FontCacheListHead
;
90 static UINT g_FontCacheNumEntries
;
92 static PWCHAR g_ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
102 L
"Vietnamese", /* 08 */
103 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
111 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
116 * For TranslateCharsetInfo
119 #define MAXTCIINDEX 32
120 static const CHARSETINFO g_FontTci
[MAXTCIINDEX
] =
123 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
124 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
125 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
126 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
127 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
128 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
129 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
130 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
131 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
132 /* reserved by ANSI */
133 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
134 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
135 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
136 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
137 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
138 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
139 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
141 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
142 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
143 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
144 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
145 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
146 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
147 /* Reserved for alternate ANSI and OEM */
148 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
149 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
150 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
151 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
152 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
153 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
154 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
155 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
156 /* Reserved for system */
157 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
158 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
166 /* Get charset from specified codepage.
167 g_FontTci is used also in TranslateCharsetInfo. */
168 BYTE FASTCALL
IntCharSetFromCodePage(UINT uCodePage
)
172 if (uCodePage
== CP_OEMCP
)
175 if (uCodePage
== CP_MACCP
)
178 for (i
= 0; i
< MAXTCIINDEX
; ++i
)
180 if (g_FontTci
[i
].ciACP
== 0)
183 if (g_FontTci
[i
].ciACP
== uCodePage
)
184 return g_FontTci
[i
].ciCharset
;
187 return DEFAULT_CHARSET
;
191 static RTL_STATIC_LIST_HEAD(g_FontSubstListHead
);
194 SharedMem_AddRef(PSHARED_MEM Ptr
)
196 ASSERT_FREETYPE_LOCK_HELD();
202 SharedFaceCache_Init(PSHARED_FACE_CACHE Cache
)
204 Cache
->OutlineRequiredSize
= 0;
205 RtlInitUnicodeString(&Cache
->FontFamily
, NULL
);
206 RtlInitUnicodeString(&Cache
->FullName
, NULL
);
210 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
213 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
218 Ptr
->Memory
= Memory
;
219 SharedFaceCache_Init(&Ptr
->EnglishUS
);
220 SharedFaceCache_Init(&Ptr
->UserLanguage
);
222 SharedMem_AddRef(Memory
);
223 DPRINT("Creating SharedFace for %s\n", Face
->family_name
? Face
->family_name
: "<NULL>");
229 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
232 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
235 Ptr
->Buffer
= Buffer
;
236 Ptr
->BufferSize
= BufferSize
;
238 Ptr
->IsMapping
= IsMapping
;
239 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
245 SharedFace_AddRef(PSHARED_FACE Ptr
)
247 ASSERT_FREETYPE_LOCK_HELD();
253 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
255 ASSERT_FREETYPE_LOCK_HELD();
257 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
258 RemoveEntryList(&Entry
->ListEntry
);
259 ExFreePoolWithTag(Entry
, TAG_FONT
);
260 g_FontCacheNumEntries
--;
261 ASSERT(g_FontCacheNumEntries
<= MAX_FONT_CACHE
);
265 RemoveCacheEntries(FT_Face Face
)
267 PLIST_ENTRY CurrentEntry
, NextEntry
;
268 PFONT_CACHE_ENTRY FontEntry
;
270 ASSERT_FREETYPE_LOCK_HELD();
272 for (CurrentEntry
= g_FontCacheListHead
.Flink
;
273 CurrentEntry
!= &g_FontCacheListHead
;
274 CurrentEntry
= NextEntry
)
276 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
277 NextEntry
= CurrentEntry
->Flink
;
279 if (FontEntry
->Face
== Face
)
281 RemoveCachedEntry(FontEntry
);
286 static void SharedMem_Release(PSHARED_MEM Ptr
)
288 ASSERT_FREETYPE_LOCK_HELD();
289 ASSERT(Ptr
->RefCount
> 0);
291 if (Ptr
->RefCount
<= 0)
295 if (Ptr
->RefCount
== 0)
297 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
299 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
301 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
302 ExFreePoolWithTag(Ptr
, TAG_FONT
);
307 SharedFaceCache_Release(PSHARED_FACE_CACHE Cache
)
309 RtlFreeUnicodeString(&Cache
->FontFamily
);
310 RtlFreeUnicodeString(&Cache
->FullName
);
314 SharedFace_Release(PSHARED_FACE Ptr
)
317 ASSERT(Ptr
->RefCount
> 0);
319 if (Ptr
->RefCount
<= 0)
323 if (Ptr
->RefCount
== 0)
325 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
? Ptr
->Face
->family_name
: "<NULL>");
326 RemoveCacheEntries(Ptr
->Face
);
327 FT_Done_Face(Ptr
->Face
);
328 SharedMem_Release(Ptr
->Memory
);
329 SharedFaceCache_Release(&Ptr
->EnglishUS
);
330 SharedFaceCache_Release(&Ptr
->UserLanguage
);
331 ExFreePoolWithTag(Ptr
, TAG_FONT
);
338 CleanupFontEntryEx(PFONT_ENTRY FontEntry
, PFONTGDI FontGDI
)
340 // PFONTGDI FontGDI = FontEntry->Font;
341 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
343 if (FontGDI
->Filename
)
344 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
346 if (FontEntry
->StyleName
.Buffer
)
347 RtlFreeUnicodeString(&FontEntry
->StyleName
);
349 if (FontEntry
->FaceName
.Buffer
)
350 RtlFreeUnicodeString(&FontEntry
->FaceName
);
353 SharedFace_Release(SharedFace
);
354 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
357 static __inline VOID FASTCALL
358 CleanupFontEntry(PFONT_ENTRY FontEntry
)
360 CleanupFontEntryEx(FontEntry
, FontEntry
->Font
);
364 static __inline
void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
366 pt
->x
.value
= vec
->x
>> 6;
367 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
368 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
369 pt
->y
.value
= vec
->y
>> 6;
370 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
371 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
375 This function builds an FT_Fixed from a FIXED. It simply put f.value
376 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
378 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
380 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
385 VOID
DumpFontEntry(PFONT_ENTRY FontEntry
)
387 const char *family_name
;
388 const char *style_name
;
390 PFONTGDI FontGDI
= FontEntry
->Font
;
394 DPRINT("FontGDI NULL\n");
398 Face
= (FontGDI
->SharedFace
? FontGDI
->SharedFace
->Face
: NULL
);
401 family_name
= Face
->family_name
;
402 style_name
= Face
->style_name
;
406 family_name
= "<invalid>";
407 style_name
= "<invalid>";
410 DPRINT("family_name '%s', style_name '%s', FaceName '%wZ', StyleName '%wZ', FontGDI %p, "
411 "FontObj %p, iUnique %lu, SharedFace %p, Face %p, CharSet %u, Filename '%S'\n",
414 &FontEntry
->FaceName
,
415 &FontEntry
->StyleName
,
425 VOID
DumpFontList(PLIST_ENTRY Head
)
428 PFONT_ENTRY CurrentEntry
;
430 DPRINT("## DumpFontList(%p)\n", Head
);
432 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
434 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
435 DumpFontEntry(CurrentEntry
);
439 VOID
DumpFontSubstEntry(PFONTSUBST_ENTRY pSubstEntry
)
441 DPRINT("%wZ,%u -> %wZ,%u\n",
442 &pSubstEntry
->FontNames
[FONTSUBST_FROM
],
443 pSubstEntry
->CharSets
[FONTSUBST_FROM
],
444 &pSubstEntry
->FontNames
[FONTSUBST_TO
],
445 pSubstEntry
->CharSets
[FONTSUBST_TO
]);
448 VOID
DumpFontSubstList(VOID
)
450 PLIST_ENTRY pHead
= &g_FontSubstListHead
;
451 PLIST_ENTRY pListEntry
;
452 PFONTSUBST_ENTRY pSubstEntry
;
454 DPRINT("## DumpFontSubstList\n");
456 for (pListEntry
= pHead
->Flink
;
458 pListEntry
= pListEntry
->Flink
)
461 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
463 DumpFontSubstEntry(pSubstEntry
);
467 VOID
DumpPrivateFontList(BOOL bDoLock
)
469 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
475 IntLockProcessPrivateFonts(Win32Process
);
477 DumpFontList(&Win32Process
->PrivateFontListHead
);
480 IntUnLockProcessPrivateFonts(Win32Process
);
483 VOID
DumpGlobalFontList(BOOL bDoLock
)
486 IntLockGlobalFonts();
488 DumpFontList(&g_FontListHead
);
491 IntUnLockGlobalFonts();
494 VOID
DumpFontInfo(BOOL bDoLock
)
496 DumpGlobalFontList(bDoLock
);
497 DumpPrivateFontList(bDoLock
);
503 * IntLoadFontSubstList --- loads the list of font substitutes
506 IntLoadFontSubstList(PLIST_ENTRY pHead
)
510 OBJECT_ATTRIBUTES ObjectAttributes
;
511 KEY_FULL_INFORMATION KeyFullInfo
;
513 UNICODE_STRING FromW
, ToW
;
514 BYTE InfoBuffer
[128];
515 PKEY_VALUE_FULL_INFORMATION pInfo
;
516 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
518 PFONTSUBST_ENTRY pEntry
;
521 /* the FontSubstitutes registry key */
522 static UNICODE_STRING FontSubstKey
=
523 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
524 L
"Microsoft\\Windows NT\\CurrentVersion\\"
527 /* open registry key */
528 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
529 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
531 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
532 if (!NT_SUCCESS(Status
))
534 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
535 return FALSE
; /* failure */
538 /* query count of values */
539 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
540 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
541 if (!NT_SUCCESS(Status
))
543 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
545 return FALSE
; /* failure */
549 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
552 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
553 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
554 if (!NT_SUCCESS(Status
))
556 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
560 /* create FromW string */
561 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
562 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
563 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
564 Success
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
567 Status
= STATUS_INSUFFICIENT_RESOURCES
;
568 DPRINT("RtlCreateUnicodeString failed\n");
573 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
574 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
575 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
576 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
578 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
579 RtlFreeUnicodeString(&FromW
);
583 /* create ToW string */
584 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
585 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
586 pch
[Length
] = UNICODE_NULL
; /* truncate */
587 Success
= RtlCreateUnicodeString(&ToW
, pch
);
590 Status
= STATUS_INSUFFICIENT_RESOURCES
;
591 DPRINT("RtlCreateUnicodeString failed\n");
592 RtlFreeUnicodeString(&FromW
);
596 /* does charset exist? (from) */
597 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
598 pch
= wcsrchr(FromW
.Buffer
, L
',');
603 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
604 /* parse charset number */
605 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
608 /* does charset exist? (to) */
609 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
610 pch
= wcsrchr(ToW
.Buffer
, L
',');
615 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
616 /* parse charset number */
617 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
620 /* is it identical? */
621 if (RtlEqualUnicodeString(&FromW
, &ToW
, TRUE
) &&
622 CharSets
[FONTSUBST_FROM
] == CharSets
[FONTSUBST_TO
])
624 RtlFreeUnicodeString(&FromW
);
625 RtlFreeUnicodeString(&ToW
);
629 /* allocate an entry */
630 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
633 DPRINT("ExAllocatePoolWithTag failed\n");
634 RtlFreeUnicodeString(&FromW
);
635 RtlFreeUnicodeString(&ToW
);
639 /* store to *pEntry */
640 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
641 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
642 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
643 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
645 /* insert pEntry to *pHead */
646 InsertTailList(pHead
, &pEntry
->ListEntry
);
652 return NT_SUCCESS(Status
);
656 InitFontSupport(VOID
)
660 InitializeListHead(&g_FontListHead
);
661 InitializeListHead(&g_FontCacheListHead
);
662 g_FontCacheNumEntries
= 0;
663 /* Fast Mutexes must be allocated from non paged pool */
664 g_FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
665 if (g_FontListLock
== NULL
)
670 ExInitializeFastMutex(g_FontListLock
);
671 g_FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
672 if (g_FreeTypeLock
== NULL
)
676 ExInitializeFastMutex(g_FreeTypeLock
);
678 ulError
= FT_Init_FreeType(&g_FreeTypeLibrary
);
681 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
685 IntLoadSystemFonts();
686 IntLoadFontSubstList(&g_FontSubstListHead
);
695 LONG FASTCALL
IntWidthMatrix(FT_Face face
, FT_Matrix
*pmat
, LONG lfWidth
)
706 ASSERT_FREETYPE_LOCK_HELD();
707 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(face
, FT_SFNT_OS2
);
711 XScale
= face
->size
->metrics
.x_scale
;
712 tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
713 if (tmAveCharWidth
== 0)
718 if (lfWidth
== tmAveCharWidth
)
721 pmat
->xx
= INT_TO_FIXED(lfWidth
) / tmAveCharWidth
;
724 pmat
->yy
= INT_TO_FIXED(1);
728 VOID FASTCALL
IntEscapeMatrix(FT_Matrix
*pmat
, LONG lfEscapement
)
731 /* Convert the angle in tenths of degrees into degrees as a 16.16 fixed-point value */
732 FT_Angle angle
= INT_TO_FIXED(lfEscapement
) / 10;
733 FT_Vector_Unit(&vecAngle
, angle
);
734 pmat
->xx
= vecAngle
.x
;
735 pmat
->xy
= -vecAngle
.y
;
736 pmat
->yx
= -pmat
->xy
;
741 FtMatrixFromMx(FT_Matrix
*pmat
, PMATRIX pmx
)
745 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
747 FLOATOBJ_MulLong(&ef
, 0x00010000);
748 pmat
->xx
= FLOATOBJ_GetLong(&ef
);
751 FLOATOBJ_MulLong(&ef
, 0x00010000);
752 pmat
->xy
= FLOATOBJ_GetLong(&ef
);
755 FLOATOBJ_MulLong(&ef
, 0x00010000);
756 pmat
->yx
= FLOATOBJ_GetLong(&ef
);
759 FLOATOBJ_MulLong(&ef
, 0x00010000);
760 pmat
->yy
= FLOATOBJ_GetLong(&ef
);
764 FtSetCoordinateTransform(
771 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
773 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
774 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
777 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
778 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
781 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
782 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
785 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
786 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
788 /* Set the transformation matrix */
789 FT_Set_Transform(face
, &ftmatrix
, 0);
793 SubstituteFontByList(PLIST_ENTRY pHead
,
794 PUNICODE_STRING pOutputName
,
795 PUNICODE_STRING pInputName
,
796 BYTE RequestedCharSet
,
797 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
799 PLIST_ENTRY pListEntry
;
800 PFONTSUBST_ENTRY pSubstEntry
;
801 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
803 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
804 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
806 /* for each list entry */
807 for (pListEntry
= pHead
->Flink
;
809 pListEntry
= pListEntry
->Flink
)
812 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
814 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
816 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
817 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
819 continue; /* not matched */
822 /* does charset number exist? (to) */
823 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
825 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
829 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
832 /* does font name match? */
833 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
836 continue; /* not matched */
839 /* update *pOutputName */
840 *pOutputName
= pSubstEntry
->FontNames
[FONTSUBST_TO
];
842 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
844 /* update CharSetMap */
845 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
846 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
848 return TRUE
; /* success */
855 IntUnicodeStringToBuffer(LPWSTR pszBuffer
, SIZE_T cbBuffer
, const UNICODE_STRING
*pString
)
857 SIZE_T cbLength
= pString
->Length
;
859 if (cbBuffer
< sizeof(UNICODE_NULL
))
862 if (cbLength
> cbBuffer
- sizeof(UNICODE_NULL
))
863 cbLength
= cbBuffer
- sizeof(UNICODE_NULL
);
865 RtlCopyMemory(pszBuffer
, pString
->Buffer
, cbLength
);
866 pszBuffer
[cbLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
870 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
872 NTSTATUS Status
= STATUS_NO_MEMORY
;
875 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
878 Tmp
.MaximumLength
= Source
->MaximumLength
;
880 RtlCopyUnicodeString(&Tmp
, Source
);
882 Destination
->MaximumLength
= Tmp
.MaximumLength
;
883 Destination
->Length
= Tmp
.Length
;
884 Destination
->Buffer
= Tmp
.Buffer
;
886 Status
= STATUS_SUCCESS
;
893 SubstituteFontRecurse(LOGFONTW
* pLogFont
)
895 UINT RecurseCount
= 5;
896 UNICODE_STRING OutputNameW
= { 0 };
897 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
899 UNICODE_STRING InputNameW
;
901 if (pLogFont
->lfFaceName
[0] == UNICODE_NULL
)
904 RtlInitUnicodeString(&InputNameW
, pLogFont
->lfFaceName
);
906 while (RecurseCount
-- > 0)
908 Found
= SubstituteFontByList(&g_FontSubstListHead
,
909 &OutputNameW
, &InputNameW
,
910 pLogFont
->lfCharSet
, CharSetMap
);
914 IntUnicodeStringToBuffer(pLogFont
->lfFaceName
, sizeof(pLogFont
->lfFaceName
), &OutputNameW
);
916 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
917 CharSetMap
[FONTSUBST_FROM
] == pLogFont
->lfCharSet
)
919 pLogFont
->lfCharSet
= CharSetMap
[FONTSUBST_TO
];
923 return TRUE
; /* success */
929 * Search the system font directory and adds each font found.
932 IntLoadSystemFonts(VOID
)
934 OBJECT_ATTRIBUTES ObjectAttributes
;
935 UNICODE_STRING Directory
, FileName
, TempString
;
936 IO_STATUS_BLOCK Iosb
;
939 PFILE_DIRECTORY_INFORMATION DirInfo
;
940 BOOLEAN bRestartScan
= TRUE
;
943 static UNICODE_STRING SearchPatterns
[] =
945 RTL_CONSTANT_STRING(L
"*.ttf"),
946 RTL_CONSTANT_STRING(L
"*.ttc"),
947 RTL_CONSTANT_STRING(L
"*.otf"),
948 RTL_CONSTANT_STRING(L
"*.otc"),
949 RTL_CONSTANT_STRING(L
"*.fon"),
950 RTL_CONSTANT_STRING(L
"*.fnt")
953 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
955 InitializeObjectAttributes(
958 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
964 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
967 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
968 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
970 if (NT_SUCCESS(Status
))
972 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
974 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
975 if (DirInfoBuffer
== NULL
)
981 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
982 if (FileName
.Buffer
== NULL
)
984 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
989 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
993 Status
= ZwQueryDirectoryFile(
1001 FileDirectoryInformation
,
1006 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
1011 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
1014 TempString
.Buffer
= DirInfo
->FileName
;
1016 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
1017 RtlCopyUnicodeString(&FileName
, &Directory
);
1018 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
1019 IntGdiAddFontResource(&FileName
, 0);
1020 if (DirInfo
->NextEntryOffset
== 0)
1022 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
1025 bRestartScan
= FALSE
;
1028 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
1029 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
1031 ZwClose(hDirectory
);
1036 ItalicFromStyle(const char *style_name
)
1038 if (style_name
== NULL
|| style_name
[0] == 0)
1040 if (strstr(style_name
, "Italic") != NULL
)
1042 if (strstr(style_name
, "Oblique") != NULL
)
1048 WeightFromStyle(const char *style_name
)
1050 if (style_name
== NULL
|| style_name
[0] == 0)
1052 if (strstr(style_name
, "Regular") != NULL
)
1054 if (strstr(style_name
, "Normal") != NULL
)
1056 if (strstr(style_name
, "SemiBold") != NULL
)
1058 if (strstr(style_name
, "UltraBold") != NULL
)
1059 return FW_ULTRABOLD
;
1060 if (strstr(style_name
, "DemiBold") != NULL
)
1062 if (strstr(style_name
, "ExtraBold") != NULL
)
1063 return FW_EXTRABOLD
;
1064 if (strstr(style_name
, "Bold") != NULL
)
1066 if (strstr(style_name
, "UltraLight") != NULL
)
1067 return FW_ULTRALIGHT
;
1068 if (strstr(style_name
, "ExtraLight") != NULL
)
1069 return FW_EXTRALIGHT
;
1070 if (strstr(style_name
, "Light") != NULL
)
1072 if (strstr(style_name
, "Hairline") != NULL
)
1074 if (strstr(style_name
, "Book") != NULL
)
1076 if (strstr(style_name
, "ExtraBlack") != NULL
)
1078 if (strstr(style_name
, "UltraBlack") != NULL
)
1080 if (strstr(style_name
, "Black") != NULL
)
1082 if (strstr(style_name
, "Medium") != NULL
)
1084 if (strstr(style_name
, "Thin") != NULL
)
1086 if (strstr(style_name
, "Heavy") != NULL
)
1092 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
);
1094 /* NOTE: If nIndex < 0 then return the number of charsets. */
1095 UINT FASTCALL
IntGetCharSet(INT nIndex
, FT_ULong CodePageRange1
)
1097 UINT BitIndex
, CharSet
;
1100 if (CodePageRange1
== 0)
1102 return (nIndex
< 0) ? 1 : DEFAULT_CHARSET
;
1105 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
1107 if (CodePageRange1
& (1 << BitIndex
))
1109 CharSet
= g_FontTci
[BitIndex
].ciCharset
;
1110 if ((nIndex
>= 0) && (nCount
== (UINT
)nIndex
))
1118 return (nIndex
< 0) ? nCount
: ANSI_CHARSET
;
1121 /* pixels to points */
1122 #define PX2PT(pixels) FT_MulDiv((pixels), 72, 96)
1125 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
)
1129 PFONT_ENTRY_MEM PrivateEntry
;
1133 ANSI_STRING AnsiString
;
1134 FT_WinFNT_HeaderRec WinFNT
;
1135 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
1136 DWORD Characteristics
= pLoadFont
->Characteristics
;
1137 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
1139 FT_ULong os2_ulCodePageRange1
;
1140 PSHARED_FACE SharedFace
;
1141 INT iCharSet
, CharSetCount
;
1142 FT_Long iFace
, FaceCount
;
1143 LIST_ENTRY LoadedFontList
;
1147 UNICODE_STRING NewString
;
1150 /* Retrieve the number of faces */
1152 Error
= FT_New_Memory_Face(g_FreeTypeLibrary
,
1153 pLoadFont
->Memory
->Buffer
,
1154 pLoadFont
->Memory
->BufferSize
,
1159 FaceCount
= Face
->num_faces
;
1162 IntUnLockFreeType();
1166 if (Error
== FT_Err_Unknown_File_Format
)
1167 DPRINT1("Unknown font file format\n");
1169 DPRINT1("Error reading font (FT_Error: %d)\n", Error
);
1170 return 0; /* failure */
1174 * Initialize the temporary font list that needs to be appended to the
1175 * global or per-process font table, in case font enumeration successes.
1176 * If an error happens while loading and enumerating the fonts, this list
1177 * is used to cleanup the allocated resources.
1179 InitializeListHead(&LoadedFontList
);
1182 * Enumerate each typeface in the font.
1184 for (iFace
= 0; iFace
< FaceCount
; ++iFace
)
1190 Error
= FT_New_Memory_Face(g_FreeTypeLibrary
,
1191 pLoadFont
->Memory
->Buffer
,
1192 pLoadFont
->Memory
->BufferSize
,
1197 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
1199 IntUnLockFreeType();
1201 if (Error
|| !SharedFace
)
1203 DPRINT1("Error reading font (FT_Error: %d)\n", Error
);
1204 goto Finish
; /* failure */
1207 /* os2_ulCodePageRange1 and CharSetCount and IsTrueType */
1208 os2_ulCodePageRange1
= 0;
1209 if (FT_IS_SFNT(Face
))
1212 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
1215 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
1217 IntUnLockFreeType();
1219 CharSetCount
= IntGetCharSet(-1, os2_ulCodePageRange1
);
1220 pLoadFont
->IsTrueType
= TRUE
;
1225 pLoadFont
->IsTrueType
= FALSE
;
1229 * Enumerate all supported character sets for the selected typeface.
1231 for (iCharSet
= 0; iCharSet
< CharSetCount
; ++iCharSet
)
1234 * Add a reference to SharedFace only when iCharSet is > 0,
1235 * since the first reference has been already done by the
1236 * SharedFace_Create() call above.
1241 SharedFace_AddRef(SharedFace
);
1242 IntUnLockFreeType();
1245 /* Allocate a FONT_ENTRY */
1246 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
1249 DPRINT1("Failed to allocate FONT_ENTRY\n");
1250 SharedFace_Release(SharedFace
);
1251 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1252 goto Finish
; /* failure */
1255 /* Allocate a FONTGDI */
1256 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
1259 DPRINT1("Failed to allocate FontGDI\n");
1260 SharedFace_Release(SharedFace
);
1261 ExFreePoolWithTag(Entry
, TAG_FONT
);
1262 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1263 goto Finish
; /* failure */
1267 FontGDI
->SharedFace
= SharedFace
;
1268 FontGDI
->CharSet
= ANSI_CHARSET
;
1269 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
1270 FontGDI
->RequestItalic
= FALSE
;
1271 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
1272 FontGDI
->RequestWeight
= FW_NORMAL
;
1274 /* Entry->FaceName */
1275 RtlInitAnsiString(&AnsiString
, Face
->family_name
);
1276 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiString
, TRUE
);
1277 if (!NT_SUCCESS(Status
))
1279 DPRINT1("Failed to allocate Entry->FaceName\n");
1280 CleanupFontEntryEx(Entry
, FontGDI
);
1281 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1282 goto Finish
; /* failure */
1285 /* Entry->StyleName */
1286 RtlInitUnicodeString(&Entry
->StyleName
, NULL
);
1287 if (Face
->style_name
&& Face
->style_name
[0] &&
1288 strcmp(Face
->style_name
, "Regular") != 0)
1290 RtlInitAnsiString(&AnsiString
, Face
->style_name
);
1291 Status
= RtlAnsiStringToUnicodeString(&Entry
->StyleName
, &AnsiString
, TRUE
);
1292 if (!NT_SUCCESS(Status
))
1294 DPRINT1("Failed to allocate Entry->StyleName\n");
1295 CleanupFontEntryEx(Entry
, FontGDI
);
1296 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1297 goto Finish
; /* failure */
1301 /* FontGDI->CharSet */
1302 if (FT_IS_SFNT(Face
))
1304 FontGDI
->CharSet
= IntGetCharSet(iCharSet
, os2_ulCodePageRange1
);
1309 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
1312 FontGDI
->CharSet
= WinFNT
.charset
;
1314 IntUnLockFreeType();
1317 /* Set the file name */
1320 // TODO: Since this Filename is common to all the faces+charsets
1321 // inside the given font, it may be worth to somehow cache it
1322 // only once and share it amongst all these faces+charsets.
1324 Length
= pFileName
->Length
+ sizeof(UNICODE_NULL
);
1325 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
, Length
, GDITAG_PFF
);
1326 if (FontGDI
->Filename
== NULL
)
1328 DPRINT1("Failed to allocate FontGDI->Filename\n");
1329 CleanupFontEntryEx(Entry
, FontGDI
);
1330 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1331 goto Finish
; /* failure */
1333 IntUnicodeStringToBuffer(FontGDI
->Filename
, Length
, pFileName
);
1337 /* This is a memory font, initialize a suitable entry */
1339 FontGDI
->Filename
= NULL
;
1341 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
1344 DPRINT1("Failed to allocate PrivateEntry\n");
1345 CleanupFontEntryEx(Entry
, FontGDI
);
1346 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1347 goto Finish
; /* failure */
1350 PrivateEntry
->Entry
= Entry
;
1351 if (pLoadFont
->PrivateEntry
)
1353 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
1357 InitializeListHead(&PrivateEntry
->ListEntry
);
1358 pLoadFont
->PrivateEntry
= PrivateEntry
;
1362 /* Add this font resource to the font table */
1363 Entry
->Font
= FontGDI
;
1364 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1365 InsertTailList(&LoadedFontList
, &Entry
->ListEntry
);
1367 DPRINT("Font loaded: %s (%s), CharSet %u, Num glyphs %d\n",
1368 Face
->family_name
, Face
->style_name
, FontGDI
->CharSet
, Face
->num_glyphs
);
1372 /* Error = */ IntRequestFontSize(NULL
, FontGDI
, 0, 0);
1373 IntUnLockFreeType();
1376 * Initialize and build the registry font value entry,
1377 * only in the case we load fonts from a file and not from memory.
1381 NameLength
= Entry
->FaceName
.Length
;
1382 if (pValueName
->Length
== 0)
1384 if (FT_IS_SFNT(Face
))
1386 Status
= DuplicateUnicodeString(&Entry
->FaceName
, pValueName
);
1387 // TODO: Check return value
1392 _itow(PX2PT(FontGDI
->EmHeight
), szSize
+1, 10);
1394 Length
= NameLength
+ (wcslen(szSize
) + 1) * sizeof(WCHAR
);
1395 pszBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_USTR
);
1398 RtlInitEmptyUnicodeString(pValueName
, pszBuffer
, (USHORT
)Length
);
1399 RtlCopyUnicodeString(pValueName
, &Entry
->FaceName
);
1400 RtlAppendUnicodeToString(pValueName
, szSize
);
1410 if (FT_IS_SFNT(Face
))
1412 Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + NameLength
+ sizeof(UNICODE_NULL
);
1413 pszBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_USTR
);
1416 RtlInitEmptyUnicodeString(&NewString
, pszBuffer
, (USHORT
)Length
);
1417 RtlCopyUnicodeString(&NewString
, pValueName
);
1418 RtlAppendUnicodeToString(&NewString
, L
" & ");
1419 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1429 _itow(PX2PT(FontGDI
->EmHeight
), szSize
+1, 10);
1431 Length
= pValueName
->Length
+ (wcslen(szSize
) + 1) * sizeof(WCHAR
);
1432 pszBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_USTR
);
1435 RtlInitEmptyUnicodeString(&NewString
, pszBuffer
, (USHORT
)Length
);
1436 RtlCopyUnicodeString(&NewString
, pValueName
);
1437 RtlAppendUnicodeToString(&NewString
, szSize
);
1445 RtlFreeUnicodeString(pValueName
);
1446 *pValueName
= NewString
;
1451 if (iFace
== FaceCount
)
1454 * We succeeded, append the created font entries into the correct font table.
1456 PLIST_ENTRY ListToAppend
;
1458 /* No typefaces were present */
1461 ASSERT(IsListEmpty(&LoadedFontList
));
1465 ASSERT(!IsListEmpty(&LoadedFontList
));
1468 * Remove the temporary font list' head and reinitialize it.
1469 * This effectively empties the list and at the same time transforms
1470 * 'ListToAppend' into a headless list, ready to be appended to the
1471 * suitable font table.
1473 ListToAppend
= LoadedFontList
.Flink
;
1474 RemoveEntryList(&LoadedFontList
);
1475 InitializeListHead(&LoadedFontList
);
1477 if (Characteristics
& FR_PRIVATE
)
1480 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1481 IntLockProcessPrivateFonts(Win32Process
);
1482 AppendTailList(&Win32Process
->PrivateFontListHead
, ListToAppend
);
1483 IntUnLockProcessPrivateFonts(Win32Process
);
1488 IntLockGlobalFonts();
1489 AppendTailList(&g_FontListHead
, ListToAppend
);
1490 IntUnLockGlobalFonts();
1493 return FaceCount
; /* Number of loaded faces */
1497 /* We failed, cleanup the resources */
1498 PLIST_ENTRY ListEntry
;
1500 if (pLoadFont
->PrivateEntry
)
1502 while (!IsListEmpty(&pLoadFont
->PrivateEntry
->ListEntry
))
1504 ListEntry
= RemoveHeadList(&pLoadFont
->PrivateEntry
->ListEntry
);
1505 PrivateEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1506 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
1508 ExFreePoolWithTag(pLoadFont
->PrivateEntry
, TAG_FONT
);
1509 pLoadFont
->PrivateEntry
= NULL
;
1512 while (!IsListEmpty(&LoadedFontList
))
1514 ListEntry
= RemoveHeadList(&LoadedFontList
);
1515 Entry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY
, ListEntry
);
1516 CleanupFontEntry(Entry
);
1519 return 0; /* No faces have been added */
1524 * IntGdiAddFontResource
1526 * Adds the font resource from the specified file to the system.
1530 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1534 PVOID Buffer
= NULL
;
1535 IO_STATUS_BLOCK Iosb
;
1536 PVOID SectionObject
;
1537 SIZE_T ViewSize
= 0;
1538 LARGE_INTEGER SectionSize
;
1539 OBJECT_ATTRIBUTES ObjectAttributes
;
1540 GDI_LOAD_FONT LoadFont
;
1543 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1545 /* Open the font file */
1546 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1547 Status
= ZwOpenFile(
1549 FILE_GENERIC_READ
| SYNCHRONIZE
,
1553 FILE_SYNCHRONOUS_IO_NONALERT
);
1554 if (!NT_SUCCESS(Status
))
1556 DPRINT("Could not load font file: %wZ\n", FileName
);
1560 SectionSize
.QuadPart
= 0LL;
1561 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1562 NULL
, &SectionSize
, PAGE_READONLY
,
1563 SEC_COMMIT
, FileHandle
, NULL
);
1564 if (!NT_SUCCESS(Status
))
1566 DPRINT("Could not map file: %wZ\n", FileName
);
1567 ZwClose(FileHandle
);
1570 ZwClose(FileHandle
);
1572 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1573 if (!NT_SUCCESS(Status
))
1575 DPRINT("Could not map file: %wZ\n", FileName
);
1576 ObDereferenceObject(SectionObject
);
1580 LoadFont
.pFileName
= FileName
;
1581 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1582 LoadFont
.Characteristics
= Characteristics
;
1583 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1584 LoadFont
.IsTrueType
= FALSE
;
1585 LoadFont
.PrivateEntry
= NULL
;
1586 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
);
1588 /* Release our copy */
1590 SharedMem_Release(LoadFont
.Memory
);
1591 IntUnLockFreeType();
1593 ObDereferenceObject(SectionObject
);
1595 /* Save the loaded font name into the registry */
1598 if (LoadFont
.IsTrueType
)
1600 /* Append " (TrueType)" */
1601 UNICODE_STRING NewString
;
1605 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
+ sizeof(UNICODE_NULL
);
1606 pszBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_USTR
);
1609 RtlInitEmptyUnicodeString(&NewString
, pszBuffer
, (USHORT
)Length
);
1610 NewString
.Buffer
[0] = UNICODE_NULL
;
1611 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1612 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1613 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1614 LoadFont
.RegValueName
= NewString
;
1622 InitializeObjectAttributes(&ObjectAttributes
, &g_FontRegPath
,
1623 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1625 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1626 if (NT_SUCCESS(Status
))
1629 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1633 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1634 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1635 pFileName
, DataSize
);
1640 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1646 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1649 GDI_LOAD_FONT LoadFont
;
1650 PFONT_ENTRY_COLL_MEM EntryCollection
;
1653 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1659 RtlCopyMemory(BufferCopy
, Buffer
, dwSize
);
1661 LoadFont
.pFileName
= NULL
;
1662 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1663 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1664 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1665 LoadFont
.IsTrueType
= FALSE
;
1666 LoadFont
.PrivateEntry
= NULL
;
1667 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
);
1669 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1671 /* Release our copy */
1673 SharedMem_Release(LoadFont
.Memory
);
1674 IntUnLockFreeType();
1678 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1679 if (EntryCollection
)
1681 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1682 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1683 IntLockProcessPrivateFonts(Win32Process
);
1684 EntryCollection
->Handle
= ULongToHandle(++Win32Process
->PrivateMemFontHandleCount
);
1685 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1686 IntUnLockProcessPrivateFonts(Win32Process
);
1687 Ret
= EntryCollection
->Handle
;
1690 *pNumAdded
= FaceCount
;
1695 // FIXME: Add RemoveFontResource
1698 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1701 PFONT_ENTRY_MEM FontEntry
;
1703 while (!IsListEmpty(&Head
->ListEntry
))
1705 Entry
= RemoveHeadList(&Head
->ListEntry
);
1706 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1708 CleanupFontEntry(FontEntry
->Entry
);
1709 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1712 CleanupFontEntry(Head
->Entry
);
1713 ExFreePoolWithTag(Head
, TAG_FONT
);
1716 static VOID FASTCALL
1717 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1719 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1720 PLIST_ENTRY ListEntry
;
1721 RemoveEntryList(&Collection
->ListEntry
);
1724 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1725 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1727 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1728 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1730 } while (FontMemEntry
!= Collection
->Entry
);
1734 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1737 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1738 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1739 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1741 IntLockProcessPrivateFonts(Win32Process
);
1742 for (Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1743 Entry
!= &Win32Process
->PrivateMemFontListHead
;
1744 Entry
= Entry
->Flink
)
1746 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1748 if (CurrentEntry
->Handle
== hMMFont
)
1750 EntryCollection
= CurrentEntry
;
1751 UnlinkFontMemCollection(CurrentEntry
);
1755 IntUnLockProcessPrivateFonts(Win32Process
);
1757 if (EntryCollection
)
1759 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1760 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1768 IntGdiCleanupPrivateFontsForProcess(VOID
)
1770 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1772 PFONT_ENTRY_COLL_MEM EntryCollection
;
1774 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1777 EntryCollection
= NULL
;
1779 IntLockProcessPrivateFonts(Win32Process
);
1780 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1782 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1783 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1784 UnlinkFontMemCollection(EntryCollection
);
1786 IntUnLockProcessPrivateFonts(Win32Process
);
1788 if (EntryCollection
)
1790 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1791 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1795 /* No Mem fonts anymore, see if we have any other private fonts left */
1797 IntLockProcessPrivateFonts(Win32Process
);
1798 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1800 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1802 IntUnLockProcessPrivateFonts(Win32Process
);
1806 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1814 IntIsFontRenderingEnabled(VOID
)
1816 return (gpsi
->BitsPixel
> 8) && g_RenderingEnabled
;
1820 IntEnableFontRendering(BOOL Enable
)
1822 g_RenderingEnabled
= Enable
;
1825 FT_Render_Mode FASTCALL
1826 IntGetFontRenderMode(LOGFONTW
*logfont
)
1828 switch (logfont
->lfQuality
)
1830 case ANTIALIASED_QUALITY
:
1832 case NONANTIALIASED_QUALITY
:
1833 return FT_RENDER_MODE_MONO
;
1835 return FT_RENDER_MODE_LIGHT
;
1836 case CLEARTYPE_QUALITY
:
1837 if (!gspv
.bFontSmoothing
)
1839 if (!gspv
.uiFontSmoothingType
)
1841 return FT_RENDER_MODE_LCD
;
1843 return FT_RENDER_MODE_NORMAL
;
1848 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1854 plfont
= LFONT_AllocFontWithHandle();
1857 return STATUS_NO_MEMORY
;
1860 ExInitializePushLock(&plfont
->lock
);
1861 *NewFont
= plfont
->BaseObject
.hHmgr
;
1862 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1863 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1864 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1866 /* This should really depend on whether GM_ADVANCED is set */
1867 plf
->lfOrientation
= plf
->lfEscapement
;
1869 LFONT_UnlockFont(plfont
);
1871 return STATUS_SUCCESS
;
1874 /*************************************************************************
1875 * TranslateCharsetInfo
1877 * Fills a CHARSETINFO structure for a character set, code page, or
1878 * font. This allows making the correspondance between different labelings
1879 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1880 * of the same encoding.
1882 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1883 * only one codepage should be set in *Src.
1886 * TRUE on success, FALSE on failure.
1889 static BOOLEAN APIENTRY
1890 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1891 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1892 if flags == TCI_SRCCHARSET: a character set value
1893 if flags == TCI_SRCCODEPAGE: a code page value */
1894 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1895 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1901 case TCI_SRCFONTSIG
:
1902 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1907 case TCI_SRCCODEPAGE
:
1908 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciACP
)
1913 case TCI_SRCCHARSET
:
1914 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciCharset
)
1926 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== g_FontTci
[Index
].ciCharset
)
1931 RtlCopyMemory(Cs
, &g_FontTci
[Index
], sizeof(CHARSETINFO
));
1937 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1941 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1943 if (ft_face
->charmaps
[i
]->platform_id
== TT_PLATFORM_MICROSOFT
&&
1944 ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1952 static void FASTCALL
1953 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1954 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1955 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1957 FT_Fixed XScale
, YScale
;
1958 int Ascent
, Descent
;
1959 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1961 ASSERT_FREETYPE_LOCK_HELD();
1963 XScale
= Face
->size
->metrics
.x_scale
;
1964 YScale
= Face
->size
->metrics
.y_scale
;
1968 TM
->tmHeight
= pFNT
->pixel_height
;
1969 TM
->tmAscent
= pFNT
->ascent
;
1970 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1971 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1972 TM
->tmExternalLeading
= pFNT
->external_leading
;
1973 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1974 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1976 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1977 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1978 TM
->tmFirstChar
= pFNT
->first_char
;
1979 TM
->tmLastChar
= pFNT
->last_char
;
1980 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1981 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1982 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1985 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1986 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1987 TM
->tmUnderlined
= pFNT
->underline
;
1988 TM
->tmStruckOut
= pFNT
->strike_out
;
1989 TM
->tmCharSet
= pFNT
->charset
;
1993 TM
->tmWeight
= FontGDI
->RequestWeight
;
1994 TM
->tmItalic
= FontGDI
->RequestItalic
;
1995 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1996 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1997 TM
->tmCharSet
= FontGDI
->CharSet
;
2002 if ((FT_Short
)pOS2
->usWinAscent
+ (FT_Short
)pOS2
->usWinDescent
== 0)
2004 Ascent
= pHori
->Ascender
;
2005 Descent
= -pHori
->Descender
;
2009 Ascent
= (FT_Short
)pOS2
->usWinAscent
;
2010 Descent
= (FT_Short
)pOS2
->usWinDescent
;
2013 TM
->tmAscent
= FontGDI
->tmAscent
;
2014 TM
->tmDescent
= FontGDI
->tmDescent
;
2015 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
2016 TM
->tmInternalLeading
= FontGDI
->tmInternalLeading
;
2019 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
2021 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
2022 - ((Ascent
+ Descent
)
2023 - (pHori
->Ascender
- pHori
->Descender
)),
2024 YScale
) + 32) >> 6);
2026 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
2027 if (TM
->tmAveCharWidth
== 0)
2029 TM
->tmAveCharWidth
= 1;
2032 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
2033 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
2037 TM
->tmWeight
= FontGDI
->OriginalWeight
;
2041 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
2042 FontGDI
->OriginalWeight
!= FW_NORMAL
)
2044 TM
->tmWeight
= FontGDI
->OriginalWeight
;
2048 TM
->tmWeight
= FontGDI
->RequestWeight
;
2053 TM
->tmDigitizedAspectX
= 96;
2054 TM
->tmDigitizedAspectY
= 96;
2055 if (face_has_symbol_charmap(Face
) ||
2056 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
2058 USHORT cpOEM
, cpAnsi
;
2060 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
2061 TM
->tmFirstChar
= 0;
2064 case 1257: /* Baltic */
2065 TM
->tmLastChar
= 0xf8fd;
2068 TM
->tmLastChar
= 0xf0ff;
2070 TM
->tmBreakChar
= 0x20;
2071 TM
->tmDefaultChar
= 0x1f;
2075 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
2076 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
2078 if(pOS2
->usFirstCharIndex
<= 1)
2079 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
2080 else if (pOS2
->usFirstCharIndex
> 0xff)
2081 TM
->tmBreakChar
= 0x20;
2083 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
2084 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
2089 TM
->tmItalic
= FontGDI
->OriginalItalic
;
2090 TM
->tmUnderlined
= FALSE
;
2091 TM
->tmStruckOut
= FALSE
;
2095 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
2097 TM
->tmItalic
= 0xFF;
2103 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
2104 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
2107 if (!FT_IS_FIXED_WIDTH(Face
))
2109 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
2111 case PAN_PROP_MONOSPACED
:
2112 TM
->tmPitchAndFamily
= 0;
2115 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
2121 TM
->tmPitchAndFamily
= 0;
2124 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
2126 case PAN_FAMILY_SCRIPT
:
2127 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
2129 case PAN_FAMILY_DECORATIVE
:
2130 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
2135 case PAN_FAMILY_TEXT_DISPLAY
:
2136 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
2137 /* Which is clearly not what the panose spec says. */
2138 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
2140 TM
->tmPitchAndFamily
= FF_MODERN
;
2144 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
2149 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
2152 case PAN_SERIF_COVE
:
2153 case PAN_SERIF_OBTUSE_COVE
:
2154 case PAN_SERIF_SQUARE_COVE
:
2155 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
2156 case PAN_SERIF_SQUARE
:
2157 case PAN_SERIF_THIN
:
2158 case PAN_SERIF_BONE
:
2159 case PAN_SERIF_EXAGGERATED
:
2160 case PAN_SERIF_TRIANGLE
:
2161 TM
->tmPitchAndFamily
|= FF_ROMAN
;
2164 case PAN_SERIF_NORMAL_SANS
:
2165 case PAN_SERIF_OBTUSE_SANS
:
2166 case PAN_SERIF_PERP_SANS
:
2167 case PAN_SERIF_FLARED
:
2168 case PAN_SERIF_ROUNDED
:
2169 TM
->tmPitchAndFamily
|= FF_SWISS
;
2175 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
2178 if (FT_IS_SCALABLE(Face
))
2180 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
2182 if (FT_IS_SFNT(Face
))
2184 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
2187 TM
->tmCharSet
= FontGDI
->CharSet
;
2190 static void FASTCALL
2191 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
2192 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
2193 FT_WinFNT_HeaderRec
*pFNT
)
2195 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
2199 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2200 FT_UShort NameID
, FT_UShort LangID
);
2202 typedef struct FONT_NAMES
2204 UNICODE_STRING FamilyNameW
; /* family name (TT_NAME_ID_FONT_FAMILY) */
2205 UNICODE_STRING FaceNameW
; /* face name (TT_NAME_ID_FULL_NAME) */
2206 UNICODE_STRING StyleNameW
; /* style name (TT_NAME_ID_FONT_SUBFAMILY) */
2207 UNICODE_STRING FullNameW
; /* unique name (TT_NAME_ID_UNIQUE_ID) */
2208 ULONG OtmSize
; /* size of OUTLINETEXTMETRICW with extra data */
2209 } FONT_NAMES
, *LPFONT_NAMES
;
2211 static __inline
void FASTCALL
2212 IntInitFontNames(FONT_NAMES
*Names
, PSHARED_FACE SharedFace
)
2216 RtlInitUnicodeString(&Names
->FamilyNameW
, NULL
);
2217 RtlInitUnicodeString(&Names
->FaceNameW
, NULL
);
2218 RtlInitUnicodeString(&Names
->StyleNameW
, NULL
);
2219 RtlInitUnicodeString(&Names
->FullNameW
, NULL
);
2222 IntGetFontLocalizedName(&Names
->FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
2224 IntGetFontLocalizedName(&Names
->FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
2226 IntGetFontLocalizedName(&Names
->StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
2227 /* unique name (full name) */
2228 IntGetFontLocalizedName(&Names
->FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
2230 /* Calculate the size of OUTLINETEXTMETRICW with extra data */
2231 OtmSize
= sizeof(OUTLINETEXTMETRICW
) +
2232 Names
->FamilyNameW
.Length
+ sizeof(UNICODE_NULL
) +
2233 Names
->FaceNameW
.Length
+ sizeof(UNICODE_NULL
) +
2234 Names
->StyleNameW
.Length
+ sizeof(UNICODE_NULL
) +
2235 Names
->FullNameW
.Length
+ sizeof(UNICODE_NULL
);
2236 Names
->OtmSize
= OtmSize
;
2239 static __inline SIZE_T FASTCALL
2240 IntStoreName(const UNICODE_STRING
*pName
, BYTE
*pb
)
2242 RtlCopyMemory(pb
, pName
->Buffer
, pName
->Length
);
2243 *(WCHAR
*)&pb
[pName
->Length
] = UNICODE_NULL
;
2244 return pName
->Length
+ sizeof(UNICODE_NULL
);
2247 static __inline BYTE
*FASTCALL
2248 IntStoreFontNames(const FONT_NAMES
*Names
, OUTLINETEXTMETRICW
*Otm
)
2250 BYTE
*pb
= (BYTE
*)Otm
+ sizeof(OUTLINETEXTMETRICW
);
2253 Otm
->otmpFamilyName
= (LPSTR
)(pb
- (BYTE
*) Otm
);
2254 pb
+= IntStoreName(&Names
->FamilyNameW
, pb
);
2257 Otm
->otmpFaceName
= (LPSTR
)(pb
- (BYTE
*) Otm
);
2258 pb
+= IntStoreName(&Names
->FaceNameW
, pb
);
2261 Otm
->otmpStyleName
= (LPSTR
)(pb
- (BYTE
*) Otm
);
2262 pb
+= IntStoreName(&Names
->StyleNameW
, pb
);
2264 /* unique name (full name) */
2265 Otm
->otmpFullName
= (LPSTR
)(pb
- (BYTE
*) Otm
);
2266 pb
+= IntStoreName(&Names
->FullNameW
, pb
);
2271 static __inline
void FASTCALL
2272 IntFreeFontNames(FONT_NAMES
*Names
)
2274 RtlFreeUnicodeString(&Names
->FamilyNameW
);
2275 RtlFreeUnicodeString(&Names
->FaceNameW
);
2276 RtlFreeUnicodeString(&Names
->StyleNameW
);
2277 RtlFreeUnicodeString(&Names
->FullNameW
);
2280 /*************************************************************
2281 * IntGetOutlineTextMetrics
2285 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
2287 OUTLINETEXTMETRICW
*Otm
)
2290 TT_HoriHeader
*pHori
;
2291 TT_Postscript
*pPost
;
2292 FT_Fixed XScale
, YScale
;
2293 FT_WinFNT_HeaderRec WinFNT
;
2296 FONT_NAMES FontNames
;
2297 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2298 PSHARED_FACE_CACHE Cache
;
2299 FT_Face Face
= SharedFace
->Face
;
2301 if (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
)
2303 Cache
= &SharedFace
->EnglishUS
;
2307 Cache
= &SharedFace
->UserLanguage
;
2310 if (Size
== 0 && Cache
->OutlineRequiredSize
> 0)
2312 ASSERT(Otm
== NULL
);
2313 return Cache
->OutlineRequiredSize
;
2316 IntInitFontNames(&FontNames
, SharedFace
);
2317 Cache
->OutlineRequiredSize
= FontNames
.OtmSize
;
2321 ASSERT(Otm
== NULL
);
2322 IntFreeFontNames(&FontNames
);
2323 return Cache
->OutlineRequiredSize
;
2326 ASSERT(Otm
!= NULL
);
2328 if (Size
< Cache
->OutlineRequiredSize
)
2330 DPRINT1("Size %u < OutlineRequiredSize %u\n", Size
,
2331 Cache
->OutlineRequiredSize
);
2332 IntFreeFontNames(&FontNames
);
2333 return 0; /* failure */
2336 XScale
= Face
->size
->metrics
.x_scale
;
2337 YScale
= Face
->size
->metrics
.y_scale
;
2341 pOS2
= FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
2342 pHori
= FT_Get_Sfnt_Table(Face
, FT_SFNT_HHEA
);
2343 pPost
= FT_Get_Sfnt_Table(Face
, FT_SFNT_POST
); /* We can live with this failing */
2344 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
2346 if (pOS2
== NULL
&& Error
)
2348 IntUnLockFreeType();
2349 DPRINT1("Can't find OS/2 table - not TT font?\n");
2350 IntFreeFontNames(&FontNames
);
2354 if (pHori
== NULL
&& Error
)
2356 IntUnLockFreeType();
2357 DPRINT1("Can't find HHEA table - not TT font?\n");
2358 IntFreeFontNames(&FontNames
);
2362 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
2364 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &WinFNT
: 0);
2370 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
2371 Otm
->otmfsSelection
= pOS2
->fsSelection
;
2372 Otm
->otmfsType
= pOS2
->fsType
;
2373 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
2374 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
2375 Otm
->otmItalicAngle
= 0; /* POST table */
2376 Otm
->otmEMSquare
= Face
->units_per_EM
;
2378 #define SCALE_X(value) ((FT_MulFix((value), XScale) + 32) >> 6)
2379 #define SCALE_Y(value) ((FT_MulFix((value), YScale) + 32) >> 6)
2381 Otm
->otmAscent
= SCALE_Y(pOS2
->sTypoAscender
);
2382 Otm
->otmDescent
= SCALE_Y(pOS2
->sTypoDescender
);
2383 Otm
->otmLineGap
= SCALE_Y(pOS2
->sTypoLineGap
);
2384 Otm
->otmsCapEmHeight
= SCALE_Y(pOS2
->sCapHeight
);
2385 Otm
->otmsXHeight
= SCALE_Y(pOS2
->sxHeight
);
2386 Otm
->otmrcFontBox
.left
= SCALE_X(Face
->bbox
.xMin
);
2387 Otm
->otmrcFontBox
.right
= SCALE_X(Face
->bbox
.xMax
);
2388 Otm
->otmrcFontBox
.top
= SCALE_Y(Face
->bbox
.yMax
);
2389 Otm
->otmrcFontBox
.bottom
= SCALE_Y(Face
->bbox
.yMin
);
2390 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
2391 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
2392 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
2393 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
2394 Otm
->otmptSubscriptSize
.x
= SCALE_X(pOS2
->ySubscriptXSize
);
2395 Otm
->otmptSubscriptSize
.y
= SCALE_Y(pOS2
->ySubscriptYSize
);
2396 Otm
->otmptSubscriptOffset
.x
= SCALE_X(pOS2
->ySubscriptXOffset
);
2397 Otm
->otmptSubscriptOffset
.y
= SCALE_Y(pOS2
->ySubscriptYOffset
);
2398 Otm
->otmptSuperscriptSize
.x
= SCALE_X(pOS2
->ySuperscriptXSize
);
2399 Otm
->otmptSuperscriptSize
.y
= SCALE_Y(pOS2
->ySuperscriptYSize
);
2400 Otm
->otmptSuperscriptOffset
.x
= SCALE_X(pOS2
->ySuperscriptXOffset
);
2401 Otm
->otmptSuperscriptOffset
.y
= SCALE_Y(pOS2
->ySuperscriptYOffset
);
2402 Otm
->otmsStrikeoutSize
= SCALE_Y(pOS2
->yStrikeoutSize
);
2403 Otm
->otmsStrikeoutPosition
= SCALE_Y(pOS2
->yStrikeoutPosition
);
2407 Otm
->otmsUnderscoreSize
= 0;
2408 Otm
->otmsUnderscorePosition
= 0;
2412 Otm
->otmsUnderscoreSize
= SCALE_Y(pPost
->underlineThickness
);
2413 Otm
->otmsUnderscorePosition
= SCALE_Y(pPost
->underlinePosition
);
2420 IntUnLockFreeType();
2422 pb
= IntStoreFontNames(&FontNames
, Otm
);
2423 ASSERT(pb
- (BYTE
*)Otm
== Cache
->OutlineRequiredSize
);
2425 IntFreeFontNames(&FontNames
);
2427 return Cache
->OutlineRequiredSize
;
2430 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2432 CharSetFromLangID(LANGID LangID
)
2434 /* FIXME: Add more and fix if wrong */
2435 switch (PRIMARYLANGID(LangID
))
2438 switch (SUBLANGID(LangID
))
2440 case SUBLANG_CHINESE_TRADITIONAL
:
2441 return CHINESEBIG5_CHARSET
;
2442 case SUBLANG_CHINESE_SIMPLIFIED
:
2446 return GB2312_CHARSET
;
2448 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2449 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2450 return EASTEUROPE_CHARSET
;
2452 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2453 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2454 return RUSSIAN_CHARSET
;
2456 case LANG_ARABIC
: return ARABIC_CHARSET
;
2457 case LANG_GREEK
: return GREEK_CHARSET
;
2458 case LANG_HEBREW
: return HEBREW_CHARSET
;
2459 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2460 case LANG_KOREAN
: return JOHAB_CHARSET
;
2461 case LANG_TURKISH
: return TURKISH_CHARSET
;
2462 case LANG_THAI
: return THAI_CHARSET
;
2463 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2464 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2466 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2467 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2468 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2469 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2470 case LANG_SWEDISH
: default:
2471 return ANSI_CHARSET
;
2476 SwapEndian(LPVOID pvData
, DWORD Size
)
2478 BYTE b
, *pb
= pvData
;
2490 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2491 FT_UShort NameID
, FT_UShort LangID
)
2494 INT i
, Count
, BestIndex
, Score
, BestScore
;
2496 NTSTATUS Status
= STATUS_NOT_FOUND
;
2497 ANSI_STRING AnsiName
;
2498 PSHARED_FACE_CACHE Cache
;
2499 FT_Face Face
= SharedFace
->Face
;
2501 RtlFreeUnicodeString(pNameW
);
2504 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2506 Cache
= &SharedFace
->EnglishUS
;
2510 Cache
= &SharedFace
->UserLanguage
;
2513 /* use cache if available */
2514 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2516 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2518 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2520 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2526 Count
= FT_Get_Sfnt_Name_Count(Face
);
2527 for (i
= 0; i
< Count
; ++i
)
2529 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2532 continue; /* failure */
2535 if (Name
.name_id
!= NameID
)
2537 continue; /* mismatched */
2540 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2541 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2542 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2544 continue; /* not Microsoft Unicode name */
2547 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2548 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2550 continue; /* invalid string */
2553 if (Name
.language_id
== LangID
)
2557 break; /* best match */
2559 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2563 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2572 if (Score
> BestScore
)
2581 /* store the best name */
2582 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2585 /* NOTE: Name.string is not null-terminated */
2587 Tmp
.Buffer
= (PWCH
)Name
.string
;
2588 Tmp
.Length
= Tmp
.MaximumLength
= Name
.string_len
;
2591 pNameW
->MaximumLength
= Name
.string_len
+ sizeof(WCHAR
);
2592 pNameW
->Buffer
= ExAllocatePoolWithTag(PagedPool
, pNameW
->MaximumLength
, TAG_USTR
);
2596 Status
= RtlAppendUnicodeStringToString(pNameW
, &Tmp
);
2597 if (Status
== STATUS_SUCCESS
)
2599 /* Convert UTF-16 big endian to little endian */
2600 SwapEndian(pNameW
->Buffer
, pNameW
->Length
);
2605 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2610 if (!NT_SUCCESS(Status
))
2613 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2615 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2616 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2620 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2621 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2625 if (NT_SUCCESS(Status
))
2628 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2630 ASSERT_FREETYPE_LOCK_NOT_HELD();
2632 if (!Cache
->FontFamily
.Buffer
)
2633 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2634 IntUnLockFreeType();
2636 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2638 ASSERT_FREETYPE_LOCK_NOT_HELD();
2640 if (!Cache
->FullName
.Buffer
)
2641 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2642 IntUnLockFreeType();
2649 static void FASTCALL
2650 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2651 LPCWSTR FullName
, PFONTGDI FontGDI
)
2654 UNICODE_STRING StyleW
;
2657 CHARSETINFO CharSetInfo
;
2659 OUTLINETEXTMETRICW
*Otm
;
2662 NEWTEXTMETRICW
*Ntm
;
2665 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2666 FT_Face Face
= SharedFace
->Face
;
2667 UNICODE_STRING NameW
;
2669 RtlInitUnicodeString(&NameW
, NULL
);
2670 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2671 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2672 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2677 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2680 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2684 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2685 TM
= &Otm
->otmTextMetrics
;
2687 Lf
->lfHeight
= TM
->tmHeight
;
2688 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2689 Lf
->lfWeight
= TM
->tmWeight
;
2690 Lf
->lfItalic
= TM
->tmItalic
;
2691 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2692 Lf
->lfCharSet
= TM
->tmCharSet
;
2693 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2694 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2695 Lf
->lfQuality
= PROOF_QUALITY
;
2697 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2698 Ntm
->tmHeight
= TM
->tmHeight
;
2699 Ntm
->tmAscent
= TM
->tmAscent
;
2700 Ntm
->tmDescent
= TM
->tmDescent
;
2701 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2702 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2703 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2704 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2705 Ntm
->tmWeight
= TM
->tmWeight
;
2706 Ntm
->tmOverhang
= TM
->tmOverhang
;
2707 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2708 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2709 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2710 Ntm
->tmLastChar
= TM
->tmLastChar
;
2711 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2712 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2713 Ntm
->tmItalic
= TM
->tmItalic
;
2714 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2715 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2716 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2717 Ntm
->tmCharSet
= TM
->tmCharSet
;
2718 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2720 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2722 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2724 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2725 ? TRUETYPE_FONTTYPE
: 0);
2727 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2728 Info
->FontType
|= RASTER_FONTTYPE
;
2733 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2735 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2739 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2741 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2742 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2745 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2746 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2747 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2748 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2749 if (!NT_SUCCESS(status
))
2751 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2754 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2757 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2761 IntUnLockFreeType();
2762 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2766 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2767 Ntm
->ntmCellHeight
= (FT_Short
)pOS2
->usWinAscent
+ (FT_Short
)pOS2
->usWinDescent
;
2768 Ntm
->ntmAvgWidth
= 0;
2770 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2772 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2773 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2774 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2775 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2776 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2777 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2779 if (0 == pOS2
->version
)
2783 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2784 fs
.fsCsb
[0] |= FS_LATIN1
;
2786 fs
.fsCsb
[0] |= FS_SYMBOL
;
2788 IntUnLockFreeType();
2790 if (fs
.fsCsb
[0] == 0)
2792 /* Let's see if we can find any interesting cmaps */
2793 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2795 switch (Face
->charmaps
[i
]->encoding
)
2797 case FT_ENCODING_UNICODE
:
2798 case FT_ENCODING_APPLE_ROMAN
:
2799 fs
.fsCsb
[0] |= FS_LATIN1
;
2801 case FT_ENCODING_MS_SYMBOL
:
2802 fs
.fsCsb
[0] |= FS_SYMBOL
;
2810 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2813 if (fs
.fsCsb
[0] & fs0
)
2815 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2817 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2819 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2821 if (g_ElfScripts
[i
])
2822 wcscpy(Info
->EnumLogFontEx
.elfScript
, g_ElfScripts
[i
]);
2825 DPRINT1("Unknown elfscript for bit %u\n", i
);
2830 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2833 static BOOLEAN FASTCALL
2834 GetFontFamilyInfoForList(const LOGFONTW
*LogFont
,
2835 PFONTFAMILYINFO Info
,
2836 LPCWSTR NominalName
,
2842 PFONT_ENTRY CurrentEntry
;
2844 FONTFAMILYINFO InfoEntry
;
2845 LONG Count
= *pCount
;
2847 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
2849 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2850 FontGDI
= CurrentEntry
->Font
;
2853 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&&
2854 LogFont
->lfCharSet
!= FontGDI
->CharSet
)
2856 continue; /* charset mismatch */
2859 /* get one info entry */
2860 FontFamilyFillInfo(&InfoEntry
, NULL
, NULL
, FontGDI
);
2862 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2865 if (_wcsnicmp(LogFont
->lfFaceName
,
2866 InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
,
2867 RTL_NUMBER_OF(LogFont
->lfFaceName
) - 1) != 0 &&
2868 _wcsnicmp(LogFont
->lfFaceName
,
2869 InfoEntry
.EnumLogFontEx
.elfFullName
,
2870 RTL_NUMBER_OF(LogFont
->lfFaceName
) - 1) != 0)
2878 /* store the nominal name */
2879 RtlStringCbCopyW(InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
,
2880 sizeof(InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
),
2884 /* store one entry to Info */
2885 if (0 <= Count
&& Count
< MaxCount
)
2887 RtlCopyMemory(&Info
[Count
], &InfoEntry
, sizeof(InfoEntry
));
2897 static BOOLEAN FASTCALL
2898 GetFontFamilyInfoForSubstitutes(const LOGFONTW
*LogFont
,
2899 PFONTFAMILYINFO Info
,
2903 PLIST_ENTRY pEntry
, pHead
= &g_FontSubstListHead
;
2904 PFONTSUBST_ENTRY pCurrentEntry
;
2905 PUNICODE_STRING pFromW
, pToW
;
2906 LOGFONTW lf
= *LogFont
;
2907 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
2909 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2911 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2913 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2914 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2917 if (_wcsicmp(LogFont
->lfFaceName
, pFromW
->Buffer
) != 0)
2918 continue; /* mismatch */
2921 pToW
= &pCurrentEntry
->FontNames
[FONTSUBST_TO
];
2922 if (RtlEqualUnicodeString(pFromW
, pToW
, TRUE
) &&
2923 pCurrentEntry
->CharSets
[FONTSUBST_FROM
] ==
2924 pCurrentEntry
->CharSets
[FONTSUBST_TO
])
2926 /* identical mapping */
2930 /* substitute and get the real name */
2931 IntUnicodeStringToBuffer(lf
.lfFaceName
, sizeof(lf
.lfFaceName
), pFromW
);
2932 SubstituteFontRecurse(&lf
);
2933 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&& LogFont
->lfCharSet
!= lf
.lfCharSet
)
2936 /* search in global fonts */
2937 IntLockGlobalFonts();
2938 GetFontFamilyInfoForList(&lf
, Info
, pFromW
->Buffer
, pCount
, MaxCount
, &g_FontListHead
);
2939 IntUnLockGlobalFonts();
2941 /* search in private fonts */
2942 IntLockProcessPrivateFonts(Win32Process
);
2943 GetFontFamilyInfoForList(&lf
, Info
, pFromW
->Buffer
, pCount
, MaxCount
,
2944 &Win32Process
->PrivateFontListHead
);
2945 IntUnLockProcessPrivateFonts(Win32Process
);
2947 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2949 /* it's already matched to the exact name and charset if the name
2950 was specified at here, then so don't scan more for another name */
2960 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2964 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2965 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2966 lprs
->nLanguageID
= gusLanguageID
;
2969 EngSetLastError(ERROR_INVALID_PARAMETER
);
2979 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2980 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2981 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2982 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2985 FT_BitmapGlyph APIENTRY
2990 FT_Render_Mode RenderMode
,
2993 PLIST_ENTRY CurrentEntry
;
2994 PFONT_CACHE_ENTRY FontEntry
;
2996 ASSERT_FREETYPE_LOCK_HELD();
2998 for (CurrentEntry
= g_FontCacheListHead
.Flink
;
2999 CurrentEntry
!= &g_FontCacheListHead
;
3000 CurrentEntry
= CurrentEntry
->Flink
)
3002 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
3003 if ((FontEntry
->Face
== Face
) &&
3004 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
3005 (FontEntry
->Height
== Height
) &&
3006 (FontEntry
->RenderMode
== RenderMode
) &&
3007 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
3011 if (CurrentEntry
== &g_FontCacheListHead
)
3016 RemoveEntryList(CurrentEntry
);
3017 InsertHeadList(&g_FontCacheListHead
, CurrentEntry
);
3018 return FontEntry
->BitmapGlyph
;
3022 FT_BitmapGlyph APIENTRY
3025 FT_GlyphSlot GlyphSlot
,
3026 FT_Render_Mode RenderMode
)
3030 FT_Bitmap AlignedBitmap
;
3031 FT_BitmapGlyph BitmapGlyph
;
3033 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
3036 DPRINT1("Failure getting glyph.\n");
3040 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
3043 FT_Done_Glyph(Glyph
);
3044 DPRINT1("Failure rendering glyph.\n");
3048 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
3049 FT_Bitmap_New(&AlignedBitmap
);
3050 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
3052 DPRINT1("Conversion failed\n");
3053 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
3057 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
3058 BitmapGlyph
->bitmap
= AlignedBitmap
;
3063 FT_BitmapGlyph APIENTRY
3069 FT_GlyphSlot GlyphSlot
,
3070 FT_Render_Mode RenderMode
)
3074 PFONT_CACHE_ENTRY NewEntry
;
3075 FT_Bitmap AlignedBitmap
;
3076 FT_BitmapGlyph BitmapGlyph
;
3078 ASSERT_FREETYPE_LOCK_HELD();
3080 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
3083 DPRINT1("Failure caching glyph.\n");
3087 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
3090 FT_Done_Glyph(GlyphCopy
);
3091 DPRINT1("Failure rendering glyph.\n");
3095 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
3098 DPRINT1("Alloc failure caching glyph.\n");
3099 FT_Done_Glyph(GlyphCopy
);
3103 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
3104 FT_Bitmap_New(&AlignedBitmap
);
3105 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
3107 DPRINT1("Conversion failed\n");
3108 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
3109 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
3110 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
3114 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
3115 BitmapGlyph
->bitmap
= AlignedBitmap
;
3117 NewEntry
->GlyphIndex
= GlyphIndex
;
3118 NewEntry
->Face
= Face
;
3119 NewEntry
->BitmapGlyph
= BitmapGlyph
;
3120 NewEntry
->Height
= Height
;
3121 NewEntry
->RenderMode
= RenderMode
;
3122 NewEntry
->mxWorldToDevice
= *pmx
;
3124 InsertHeadList(&g_FontCacheListHead
, &NewEntry
->ListEntry
);
3125 if (++g_FontCacheNumEntries
> MAX_FONT_CACHE
)
3127 NewEntry
= CONTAINING_RECORD(g_FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
3128 RemoveCachedEntry(NewEntry
);
3135 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
3137 TTPOLYGONHEADER
*pph
;
3139 int needed
= 0, point
= 0, contour
, first_pt
;
3140 unsigned int pph_start
, cpfx
;
3143 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
3145 /* Ignore contours containing one point */
3146 if (point
== outline
->contours
[contour
])
3153 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
3157 pph
->dwType
= TT_POLYGON_TYPE
;
3158 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
3160 needed
+= sizeof(*pph
);
3162 while (point
<= outline
->contours
[contour
])
3164 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
3165 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
3166 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
3171 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
3174 } while (point
<= outline
->contours
[contour
] &&
3175 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
3176 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
3177 /* At the end of a contour Windows adds the start point, but
3179 if (point
> outline
->contours
[contour
] &&
3180 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
3183 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
3186 else if (point
<= outline
->contours
[contour
] &&
3187 outline
->tags
[point
] & FT_Curve_Tag_On
)
3189 /* add closing pt for bezier */
3191 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
3200 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
3203 pph
->cb
= needed
- pph_start
;
3208 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
3210 /* Convert the quadratic Beziers to cubic Beziers.
3211 The parametric eqn for a cubic Bezier is, from PLRM:
3212 r(t) = at^3 + bt^2 + ct + r0
3213 with the control points:
3218 A quadratic Bezier has the form:
3219 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
3221 So equating powers of t leads to:
3222 r1 = 2/3 p1 + 1/3 p0
3223 r2 = 2/3 p1 + 1/3 p2
3224 and of course r0 = p0, r3 = p2
3226 int contour
, point
= 0, first_pt
;
3227 TTPOLYGONHEADER
*pph
;
3229 DWORD pph_start
, cpfx
, type
;
3230 FT_Vector cubic_control
[4];
3231 unsigned int needed
= 0;
3233 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
3236 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
3240 pph
->dwType
= TT_POLYGON_TYPE
;
3241 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
3243 needed
+= sizeof(*pph
);
3245 while (point
<= outline
->contours
[contour
])
3247 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
3248 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
3249 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
3253 if (type
== TT_PRIM_LINE
)
3256 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
3262 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
3265 /* FIXME: Possible optimization in endpoint calculation
3266 if there are two consecutive curves */
3267 cubic_control
[0] = outline
->points
[point
-1];
3268 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
3270 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
3271 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
3272 cubic_control
[0].x
>>= 1;
3273 cubic_control
[0].y
>>= 1;
3275 if (point
+1 > outline
->contours
[contour
])
3276 cubic_control
[3] = outline
->points
[first_pt
];
3279 cubic_control
[3] = outline
->points
[point
+1];
3280 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
3282 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
3283 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
3284 cubic_control
[3].x
>>= 1;
3285 cubic_control
[3].y
>>= 1;
3288 /* r1 = 1/3 p0 + 2/3 p1
3289 r2 = 1/3 p2 + 2/3 p1 */
3290 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
3291 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
3292 cubic_control
[2] = cubic_control
[1];
3293 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
3294 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
3295 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
3296 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
3299 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
3300 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
3301 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
3306 } while (point
<= outline
->contours
[contour
] &&
3307 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
3308 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
3309 /* At the end of a contour Windows adds the start point,
3310 but only for Beziers and we've already done that.
3312 if (point
<= outline
->contours
[contour
] &&
3313 outline
->tags
[point
] & FT_Curve_Tag_On
)
3315 /* This is the closing pt of a bezier, but we've already
3316 added it, so just inc point and carry on */
3324 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
3327 pph
->cb
= needed
- pph_start
;
3333 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
)
3336 FT_Size_RequestRec req
;
3337 FT_Face face
= FontGDI
->SharedFace
->Face
;
3339 TT_HoriHeader
*pHori
;
3340 FT_WinFNT_HeaderRec WinFNT
;
3341 LONG Ascent
, Descent
, Sum
, EmHeight64
;
3343 lfWidth
= abs(lfWidth
);
3348 DPRINT("lfHeight and lfWidth are zero.\n");
3360 ASSERT_FREETYPE_LOCK_HELD();
3361 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(face
, FT_SFNT_OS2
);
3362 pHori
= (TT_HoriHeader
*)FT_Get_Sfnt_Table(face
, FT_SFNT_HHEA
);
3364 if (!pOS2
|| !pHori
)
3366 error
= FT_Get_WinFNT_Header(face
, &WinFNT
);
3369 DPRINT1("%s: Failed to request font size.\n", face
->family_name
);
3374 FontGDI
->tmHeight
= WinFNT
.pixel_height
;
3375 FontGDI
->tmAscent
= WinFNT
.ascent
;
3376 FontGDI
->tmDescent
= FontGDI
->tmHeight
- FontGDI
->tmAscent
;
3377 FontGDI
->tmInternalLeading
= WinFNT
.internal_leading
;
3378 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3379 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3380 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3381 FontGDI
->Magic
= FONTGDI_MAGIC
;
3386 * NOTE: We cast TT_OS2.usWinAscent and TT_OS2.usWinDescent to signed FT_Short.
3387 * Why? See: https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswindescent
3389 * > usWinDescent is "usually" a positive value ...
3391 * We can read it as "not always". See CORE-14994.
3392 * See also: https://docs.microsoft.com/en-us/typography/opentype/spec/os2#fsselection
3394 #define FM_SEL_USE_TYPO_METRICS 0x80
3397 /* case (A): lfHeight is positive */
3398 Sum
= (FT_Short
)pOS2
->usWinAscent
+ (FT_Short
)pOS2
->usWinDescent
;
3399 if (Sum
== 0 || (pOS2
->fsSelection
& FM_SEL_USE_TYPO_METRICS
))
3401 Ascent
= pHori
->Ascender
;
3402 Descent
= -pHori
->Descender
;
3403 Sum
= Ascent
+ Descent
;
3407 Ascent
= (FT_Short
)pOS2
->usWinAscent
;
3408 Descent
= (FT_Short
)pOS2
->usWinDescent
;
3411 FontGDI
->tmAscent
= FT_MulDiv(lfHeight
, Ascent
, Sum
);
3412 FontGDI
->tmDescent
= FT_MulDiv(lfHeight
, Descent
, Sum
);
3413 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3414 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
- FT_MulDiv(lfHeight
, face
->units_per_EM
, Sum
);
3416 else if (lfHeight
< 0)
3418 /* case (B): lfHeight is negative */
3419 if (pOS2
->fsSelection
& FM_SEL_USE_TYPO_METRICS
)
3421 FontGDI
->tmAscent
= FT_MulDiv(-lfHeight
, pHori
->Ascender
, face
->units_per_EM
);
3422 FontGDI
->tmDescent
= FT_MulDiv(-lfHeight
, -pHori
->Descender
, face
->units_per_EM
);
3426 FontGDI
->tmAscent
= FT_MulDiv(-lfHeight
, (FT_Short
)pOS2
->usWinAscent
, face
->units_per_EM
);
3427 FontGDI
->tmDescent
= FT_MulDiv(-lfHeight
, (FT_Short
)pOS2
->usWinDescent
, face
->units_per_EM
);
3429 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3430 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
+ lfHeight
;
3432 #undef FM_SEL_USE_TYPO_METRICS
3434 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3435 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3436 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3437 FontGDI
->Magic
= FONTGDI_MAGIC
;
3439 EmHeight64
= (FontGDI
->EmHeight
<< 6);
3441 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3443 req
.height
= EmHeight64
;
3444 req
.horiResolution
= 0;
3445 req
.vertResolution
= 0;
3446 return FT_Request_Size(face
, &req
);
3451 TextIntUpdateSize(PDC dc
,
3458 FT_CharMap charmap
, found
;
3464 face
= FontGDI
->SharedFace
->Face
;
3465 if (face
->charmap
== NULL
)
3467 DPRINT("WARNING: No charmap selected!\n");
3468 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3471 for (n
= 0; n
< face
->num_charmaps
; n
++)
3473 charmap
= face
->charmaps
[n
];
3474 if (charmap
->encoding
== FT_ENCODING_UNICODE
)
3482 for (n
= 0; n
< face
->num_charmaps
; n
++)
3484 charmap
= face
->charmaps
[n
];
3485 if (charmap
->platform_id
== TT_PLATFORM_APPLE_UNICODE
)
3494 for (n
= 0; n
< face
->num_charmaps
; n
++)
3496 charmap
= face
->charmaps
[n
];
3497 if (charmap
->encoding
== FT_ENCODING_MS_SYMBOL
)
3504 if (!found
&& face
->num_charmaps
> 0)
3506 found
= face
->charmaps
[0];
3510 DPRINT1("WARNING: Could not find desired charmap!\n");
3514 DPRINT("Found charmap encoding: %i\n", found
->encoding
);
3515 error
= FT_Set_Charmap(face
, found
);
3518 DPRINT1("WARNING: Could not set the charmap!\n");
3523 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3525 error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3528 IntUnLockFreeType();
3532 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3539 static inline FT_UInt FASTCALL
3540 get_glyph_index_symbol(FT_Face ft_face
, UINT glyph
)
3544 if (glyph
< 0x100) glyph
+= 0xf000;
3545 /* there are a number of old pre-Unicode "broken" TTFs, which
3546 do have symbols at U+00XX instead of U+f0XX */
3547 if (!(ret
= FT_Get_Char_Index(ft_face
, glyph
)))
3548 ret
= FT_Get_Char_Index(ft_face
, glyph
- 0xf000);
3553 static inline FT_UInt FASTCALL
3554 get_glyph_index(FT_Face ft_face
, UINT glyph
)
3558 if (face_has_symbol_charmap(ft_face
))
3560 ret
= get_glyph_index_symbol(ft_face
, glyph
);
3565 return FT_Get_Char_Index(ft_face
, glyph
);
3568 static inline FT_UInt FASTCALL
3569 get_glyph_index_flagged(FT_Face face
, FT_ULong code
, DWORD indexed_flag
, DWORD flags
)
3571 FT_UInt glyph_index
;
3572 if (flags
& indexed_flag
)
3578 glyph_index
= get_glyph_index(face
, code
);
3584 * Based on WineEngGetGlyphOutline
3589 ftGdiGetGlyphOutline(
3597 BOOL bIgnoreRotation
)
3606 FT_UInt glyph_index
;
3607 DWORD width
, height
, pitch
, needed
= 0;
3608 FT_Bitmap ft_bitmap
;
3610 INT left
, right
, top
= 0, bottom
= 0;
3611 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3612 FLOATOBJ eM11
, widthRatio
, eTemp
;
3613 FT_Matrix transMat
= identityMat
;
3614 BOOL needsTransform
= FALSE
;
3617 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3618 OUTLINETEXTMETRICW
*potm
;
3623 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3624 cjBuf
, pvBuf
, pmat2
);
3626 pdcattr
= dc
->pdcattr
;
3628 XFORMOBJ_vInit(&xo
, &dc
->pdcattr
->mxWorldToDevice
);
3629 XFORMOBJ_iGetXform(&xo
, &xform
);
3630 FLOATOBJ_SetFloat(&eM11
, xform
.eM11
);
3632 hFont
= pdcattr
->hlfntNew
;
3633 TextObj
= RealizeFontInit(hFont
);
3637 EngSetLastError(ERROR_INVALID_HANDLE
);
3640 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3641 ft_face
= FontGDI
->SharedFace
->Face
;
3643 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3644 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3645 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3647 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3648 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3651 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3652 TEXTOBJ_UnlockText(TextObj
);
3655 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3658 /* FIXME: last error? */
3659 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3660 TEXTOBJ_UnlockText(TextObj
);
3665 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3666 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3668 TEXTOBJ_UnlockText(TextObj
);
3670 glyph_index
= get_glyph_index_flagged(ft_face
, wch
, GGO_GLYPH_INDEX
, iFormat
);
3671 iFormat
&= ~GGO_GLYPH_INDEX
;
3673 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3674 load_flags
|= FT_LOAD_NO_BITMAP
;
3676 if (iFormat
& GGO_UNHINTED
)
3678 load_flags
|= FT_LOAD_NO_HINTING
;
3679 iFormat
&= ~GGO_UNHINTED
;
3682 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3685 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3686 IntUnLockFreeType();
3687 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3690 IntUnLockFreeType();
3692 FLOATOBJ_Set1(&widthRatio
);
3693 if (aveWidth
&& potm
)
3695 // widthRatio = aveWidth * eM11 / potm->otmTextMetrics.tmAveCharWidth
3696 FLOATOBJ_SetLong(&widthRatio
, aveWidth
);
3697 FLOATOBJ_Mul(&widthRatio
, &eM11
);
3698 FLOATOBJ_DivLong(&widthRatio
, potm
->otmTextMetrics
.tmAveCharWidth
);
3701 //left = (INT)(ft_face->glyph->metrics.horiBearingX * widthRatio) & -64;
3702 FLOATOBJ_SetLong(&eTemp
, ft_face
->glyph
->metrics
.horiBearingX
);
3703 FLOATOBJ_Mul(&eTemp
, &widthRatio
);
3704 left
= FLOATOBJ_GetLong(&eTemp
) & -64;
3706 //right = (INT)((ft_face->glyph->metrics.horiBearingX +
3707 // ft_face->glyph->metrics.width) * widthRatio + 63) & -64;
3708 FLOATOBJ_SetLong(&eTemp
, ft_face
->glyph
->metrics
.horiBearingX
* ft_face
->glyph
->metrics
.width
);
3709 FLOATOBJ_Mul(&eTemp
, &widthRatio
);
3710 FLOATOBJ_AddLong(&eTemp
, 63);
3711 right
= FLOATOBJ_GetLong(&eTemp
) & -64;
3713 //adv = (INT)((ft_face->glyph->metrics.horiAdvance * widthRatio) + 63) >> 6;
3714 FLOATOBJ_SetLong(&eTemp
, ft_face
->glyph
->metrics
.horiAdvance
);
3715 FLOATOBJ_Mul(&eTemp
, &widthRatio
);
3716 FLOATOBJ_AddLong(&eTemp
, 63);
3717 adv
= FLOATOBJ_GetLong(&eTemp
) >> 6;
3720 bbx
= (right
- left
) >> 6;
3722 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3726 /* Width scaling transform */
3727 if (!FLOATOBJ_Equal1(&widthRatio
))
3732 FLOATOBJ_MulLong(&eTemp
, 1 << 16);
3734 scaleMat
.xx
= FLOATOBJ_GetLong(&eTemp
);
3737 scaleMat
.yy
= INT_TO_FIXED(1);
3738 FT_Matrix_Multiply(&scaleMat
, &transMat
);
3739 needsTransform
= TRUE
;
3742 /* World transform */
3745 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3747 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3748 FtMatrixFromMx(&ftmatrix
, pmx
);
3750 if (memcmp(&ftmatrix
, &identityMat
, sizeof(identityMat
)) != 0)
3752 FT_Matrix_Multiply(&ftmatrix
, &transMat
);
3753 needsTransform
= TRUE
;
3757 /* Rotation transform */
3760 FT_Matrix rotationMat
;
3761 DPRINT("Rotation Trans!\n");
3762 IntEscapeMatrix(&rotationMat
, orientation
);
3763 FT_Matrix_Multiply(&rotationMat
, &transMat
);
3764 needsTransform
= TRUE
;
3767 /* Extra transformation specified by caller */
3771 DPRINT("MAT2 Matrix Trans!\n");
3772 extraMat
.xx
= FT_FixedFromFIXED(pmat2
->eM11
);
3773 extraMat
.xy
= FT_FixedFromFIXED(pmat2
->eM21
);
3774 extraMat
.yx
= FT_FixedFromFIXED(pmat2
->eM12
);
3775 extraMat
.yy
= FT_FixedFromFIXED(pmat2
->eM22
);
3776 FT_Matrix_Multiply(&extraMat
, &transMat
);
3777 needsTransform
= TRUE
;
3780 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
); /* It looks like we are finished with potm ATM. */
3782 if (!needsTransform
)
3784 DPRINT("No Need to be Transformed!\n");
3785 top
= (ft_face
->glyph
->metrics
.horiBearingY
+ 63) & -64;
3786 bottom
= (ft_face
->glyph
->metrics
.horiBearingY
-
3787 ft_face
->glyph
->metrics
.height
) & -64;
3788 gm
.gmCellIncX
= adv
;
3795 for (xc
= 0; xc
< 2; xc
++)
3797 for (yc
= 0; yc
< 2; yc
++)
3799 vec
.x
= (ft_face
->glyph
->metrics
.horiBearingX
+
3800 xc
* ft_face
->glyph
->metrics
.width
);
3801 vec
.y
= ft_face
->glyph
->metrics
.horiBearingY
-
3802 yc
* ft_face
->glyph
->metrics
.height
;
3803 DPRINT("Vec %ld,%ld\n", vec
.x
, vec
.y
);
3804 FT_Vector_Transform(&vec
, &transMat
);
3805 if (xc
== 0 && yc
== 0)
3807 left
= right
= vec
.x
;
3808 top
= bottom
= vec
.y
;
3812 if (vec
.x
< left
) left
= vec
.x
;
3813 else if (vec
.x
> right
) right
= vec
.x
;
3814 if (vec
.y
< bottom
) bottom
= vec
.y
;
3815 else if (vec
.y
> top
) top
= vec
.y
;
3820 right
= (right
+ 63) & -64;
3821 bottom
= bottom
& -64;
3822 top
= (top
+ 63) & -64;
3824 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left
, top
, right
, bottom
);
3825 vec
.x
= ft_face
->glyph
->metrics
.horiAdvance
;
3827 FT_Vector_Transform(&vec
, &transMat
);
3828 gm
.gmCellIncX
= (vec
.x
+63) >> 6;
3829 gm
.gmCellIncY
= -((vec
.y
+63) >> 6);
3831 gm
.gmBlackBoxX
= (right
- left
) >> 6;
3832 gm
.gmBlackBoxY
= (top
- bottom
) >> 6;
3833 gm
.gmptGlyphOrigin
.x
= left
>> 6;
3834 gm
.gmptGlyphOrigin
.y
= top
>> 6;
3836 DPRINT("CX %d CY %d BBX %u BBY %u GOX %d GOY %d\n",
3837 gm
.gmCellIncX
, gm
.gmCellIncY
,
3838 gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
3839 gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
3841 IntUnLockFreeType();
3844 if (iFormat
== GGO_METRICS
)
3846 DPRINT("GGO_METRICS Exit!\n");
3848 return 1; /* FIXME */
3851 if (ft_face
->glyph
->format
!= ft_glyph_format_outline
&& iFormat
!= GGO_BITMAP
)
3853 DPRINT1("Loaded a bitmap\n");
3861 width
= gm
.gmBlackBoxX
;
3862 height
= gm
.gmBlackBoxY
;
3863 pitch
= ((width
+ 31) >> 5) << 2;
3864 needed
= pitch
* height
;
3866 if (!pvBuf
|| !cjBuf
) break;
3867 if (!needed
) return GDI_ERROR
; /* empty glyph */
3871 switch (ft_face
->glyph
->format
)
3873 case ft_glyph_format_bitmap
:
3875 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3876 INT w
= min( pitch
, (ft_face
->glyph
->bitmap
.width
+ 7) >> 3 );
3877 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3880 RtlCopyMemory(dst
, src
, w
);
3881 src
+= ft_face
->glyph
->bitmap
.pitch
;
3887 case ft_glyph_format_outline
:
3889 ft_bitmap
.width
= width
;
3890 ft_bitmap
.rows
= height
;
3891 ft_bitmap
.pitch
= pitch
;
3892 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
3893 ft_bitmap
.buffer
= pvBuf
;
3898 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3900 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3901 /* Note: FreeType will only set 'black' bits for us. */
3902 RtlZeroMemory(pvBuf
, needed
);
3903 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3904 IntUnLockFreeType();
3909 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3916 case GGO_GRAY2_BITMAP
:
3917 case GGO_GRAY4_BITMAP
:
3918 case GGO_GRAY8_BITMAP
:
3920 unsigned int mult
, row
, col
;
3923 width
= gm
.gmBlackBoxX
;
3924 height
= gm
.gmBlackBoxY
;
3925 pitch
= (width
+ 3) / 4 * 4;
3926 needed
= pitch
* height
;
3928 if (!pvBuf
|| !cjBuf
) break;
3929 if (!needed
) return GDI_ERROR
; /* empty glyph */
3933 switch (ft_face
->glyph
->format
)
3935 case ft_glyph_format_bitmap
:
3937 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3938 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3942 for (x
= 0; (UINT
)x
< pitch
; x
++)
3944 if (x
< ft_face
->glyph
->bitmap
.width
)
3945 dst
[x
] = (src
[x
/ 8] & (1 << ( (7 - (x
% 8))))) ? 0xff : 0;
3949 src
+= ft_face
->glyph
->bitmap
.pitch
;
3954 case ft_glyph_format_outline
:
3956 ft_bitmap
.width
= width
;
3957 ft_bitmap
.rows
= height
;
3958 ft_bitmap
.pitch
= pitch
;
3959 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
3960 ft_bitmap
.buffer
= pvBuf
;
3965 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3967 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3968 RtlZeroMemory(ft_bitmap
.buffer
, cjBuf
);
3969 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3970 IntUnLockFreeType();
3972 if (iFormat
== GGO_GRAY2_BITMAP
)
3974 else if (iFormat
== GGO_GRAY4_BITMAP
)
3976 else if (iFormat
== GGO_GRAY8_BITMAP
)
3984 for (row
= 0; row
< height
; row
++)
3987 for (col
= 0; col
< width
; col
++, ptr
++)
3989 *ptr
= (((int)*ptr
) * mult
+ 128) / 256;
3997 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
4006 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
4008 if (cjBuf
== 0) pvBuf
= NULL
; /* This is okay, need cjBuf to allocate. */
4011 if (needsTransform
&& pvBuf
) FT_Outline_Transform(outline
, &transMat
);
4013 needed
= get_native_glyph_outline(outline
, cjBuf
, NULL
);
4015 if (!pvBuf
|| !cjBuf
)
4017 IntUnLockFreeType();
4022 IntUnLockFreeType();
4025 get_native_glyph_outline(outline
, cjBuf
, pvBuf
);
4026 IntUnLockFreeType();
4032 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
4033 if (cjBuf
== 0) pvBuf
= NULL
;
4035 if (needsTransform
&& pvBuf
)
4038 FT_Outline_Transform(outline
, &transMat
);
4039 IntUnLockFreeType();
4041 needed
= get_bezier_glyph_outline(outline
, cjBuf
, NULL
);
4043 if (!pvBuf
|| !cjBuf
)
4048 get_bezier_glyph_outline(outline
, cjBuf
, pvBuf
);
4053 DPRINT1("Unsupported format %u\n", iFormat
);
4057 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed
);
4064 TextIntGetTextExtentPoint(PDC dc
,
4077 FT_BitmapGlyph realglyph
;
4078 INT error
, glyph_index
, i
, previous
;
4079 ULONGLONG TotalWidth64
= 0;
4081 FT_Render_Mode RenderMode
;
4083 PMATRIX pmxWorldToDevice
;
4085 BOOL EmuBold
, EmuItalic
;
4086 LONG ascender
, descender
;
4088 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
4090 face
= FontGDI
->SharedFace
->Face
;
4098 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
4100 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4101 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
4102 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
4104 Render
= IntIsFontRenderingEnabled();
4106 RenderMode
= IntGetFontRenderMode(plf
);
4108 RenderMode
= FT_RENDER_MODE_MONO
;
4110 /* Get the DC's world-to-device transformation matrix */
4111 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
4112 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
4114 use_kerning
= FT_HAS_KERNING(face
);
4117 for (i
= 0; i
< Count
; i
++)
4119 glyph_index
= get_glyph_index_flagged(face
, *String
, GTEF_INDICES
, fl
);
4121 if (EmuBold
|| EmuItalic
)
4124 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
4125 RenderMode
, pmxWorldToDevice
);
4127 if (EmuBold
|| EmuItalic
|| !realglyph
)
4129 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
4132 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
4136 glyph
= face
->glyph
;
4137 if (EmuBold
|| EmuItalic
)
4140 FT_GlyphSlot_Embolden(glyph
);
4142 FT_GlyphSlot_Oblique(glyph
);
4143 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
4147 realglyph
= ftGdiGlyphCacheSet(face
,
4157 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
4162 /* Retrieve kerning distance */
4163 if (use_kerning
&& previous
&& glyph_index
)
4166 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
4167 TotalWidth64
+= delta
.x
;
4170 TotalWidth64
+= realglyph
->root
.advance
.x
>> 10;
4172 if (((TotalWidth64
+ 32) >> 6) <= MaxExtent
&& NULL
!= Fit
)
4178 Dx
[i
] = (TotalWidth64
+ 32) >> 6;
4181 /* Bold and italic do not use the cache */
4182 if (EmuBold
|| EmuItalic
)
4184 FT_Done_Glyph((FT_Glyph
)realglyph
);
4187 previous
= glyph_index
;
4190 ASSERT(FontGDI
->Magic
== FONTGDI_MAGIC
);
4191 ascender
= FontGDI
->tmAscent
; /* Units above baseline */
4192 descender
= FontGDI
->tmDescent
; /* Units below baseline */
4193 IntUnLockFreeType();
4195 Size
->cx
= (TotalWidth64
+ 32) >> 6;
4196 Size
->cy
= ascender
+ descender
;
4204 ftGdiGetTextCharsetInfo(
4206 LPFONTSIGNATURE lpSig
,
4210 UINT Ret
= DEFAULT_CHARSET
;
4220 USHORT usACP
, usOEM
;
4222 pdcattr
= Dc
->pdcattr
;
4223 hFont
= pdcattr
->hlfntNew
;
4224 TextObj
= RealizeFontInit(hFont
);
4228 EngSetLastError(ERROR_INVALID_HANDLE
);
4231 FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
4232 Face
= FontGdi
->SharedFace
->Face
;
4233 TEXTOBJ_UnlockText(TextObj
);
4235 memset(&fs
, 0, sizeof(FONTSIGNATURE
));
4237 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
4240 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
4241 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
4242 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
4243 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
4244 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
4245 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
4246 if (pOS2
->version
== 0)
4250 if (FT_Get_First_Char( Face
, &dummy
) < 0x100)
4251 fs
.fsCsb
[0] |= FS_LATIN1
;
4253 fs
.fsCsb
[0] |= FS_SYMBOL
;
4257 IntUnLockFreeType();
4258 DPRINT("Csb 1=%x 0=%x\n", fs
.fsCsb
[1],fs
.fsCsb
[0]);
4259 if (fs
.fsCsb
[0] == 0)
4260 { /* Let's see if we can find any interesting cmaps */
4261 for (i
= 0; i
< Face
->num_charmaps
; i
++)
4263 switch (Face
->charmaps
[i
]->encoding
)
4265 case FT_ENCODING_UNICODE
:
4266 case FT_ENCODING_APPLE_ROMAN
:
4267 fs
.fsCsb
[0] |= FS_LATIN1
;
4269 case FT_ENCODING_MS_SYMBOL
:
4270 fs
.fsCsb
[0] |= FS_SYMBOL
;
4279 RtlCopyMemory(lpSig
, &fs
, sizeof(FONTSIGNATURE
));
4282 RtlGetDefaultCodePage(&usACP
, &usOEM
);
4285 if (IntTranslateCharsetInfo(&cp
, &csi
, TCI_SRCCODEPAGE
))
4286 if (csi
.fs
.fsCsb
[0] & fs
.fsCsb
[0])
4289 Ret
= csi
.ciCharset
;
4293 for (i
= 0; i
< MAXTCIINDEX
; i
++)
4296 if (fs
.fsCsb
[0] & fs0
)
4298 if (IntTranslateCharsetInfo(&fs0
, &csi
, TCI_SRCFONTSIG
))
4302 Ret
= csi
.ciCharset
;
4306 DPRINT1("TCI failing on %x\n", fs0
);
4310 DPRINT("CharSet %u CodePage %u\n", csi
.ciCharset
, csi
.ciACP
);
4311 return (MAKELONG(csi
.ciACP
, csi
.ciCharset
));
4317 ftGetFontUnicodeRanges(PFONTGDI Font
, PGLYPHSET glyphset
)
4320 DWORD num_ranges
= 0;
4321 FT_Face face
= Font
->SharedFace
->Face
;
4323 if (face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
4325 FT_UInt glyph_code
= 0;
4326 FT_ULong char_code
, char_code_prev
;
4328 char_code_prev
= char_code
= FT_Get_First_Char(face
, &glyph_code
);
4330 DPRINT("Face encoding FT_ENCODING_UNICODE, number of glyphs %ld, first glyph %u, first char %04lx\n",
4331 face
->num_glyphs
, glyph_code
, char_code
);
4333 if (!glyph_code
) return 0;
4337 glyphset
->ranges
[0].wcLow
= (USHORT
)char_code
;
4338 glyphset
->ranges
[0].cGlyphs
= 0;
4339 glyphset
->cGlyphsSupported
= 0;
4345 if (char_code
< char_code_prev
)
4347 DPRINT1("Expected increasing char code from FT_Get_Next_Char\n");
4350 if (char_code
- char_code_prev
> 1)
4355 glyphset
->ranges
[num_ranges
- 1].wcLow
= (USHORT
)char_code
;
4356 glyphset
->ranges
[num_ranges
- 1].cGlyphs
= 1;
4357 glyphset
->cGlyphsSupported
++;
4362 glyphset
->ranges
[num_ranges
- 1].cGlyphs
++;
4363 glyphset
->cGlyphsSupported
++;
4365 char_code_prev
= char_code
;
4366 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_code
);
4370 DPRINT1("Encoding %i not supported\n", face
->charmap
->encoding
);
4372 size
= sizeof(GLYPHSET
) + sizeof(WCRANGE
) * (num_ranges
- 1);
4375 glyphset
->cbThis
= size
;
4376 glyphset
->cRanges
= num_ranges
;
4377 glyphset
->flAccel
= 0;
4385 ftGdiGetTextMetricsW(
4387 PTMW_INTERNAL ptmwi
)
4395 TT_HoriHeader
*pHori
;
4396 FT_WinFNT_HeaderRec Win
;
4398 NTSTATUS Status
= STATUS_SUCCESS
;
4403 EngSetLastError(STATUS_INVALID_PARAMETER
);
4407 if (!(dc
= DC_LockDc(hDC
)))
4409 EngSetLastError(ERROR_INVALID_HANDLE
);
4412 pdcattr
= dc
->pdcattr
;
4413 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
4414 if (NULL
!= TextObj
)
4416 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4417 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
4419 Face
= FontGDI
->SharedFace
->Face
;
4422 Error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
4423 FtSetCoordinateTransform(Face
, DC_pmxWorldToDevice(dc
));
4424 IntUnLockFreeType();
4428 DPRINT1("Error in setting pixel sizes: %u\n", Error
);
4429 Status
= STATUS_UNSUCCESSFUL
;
4433 FT_Face Face
= FontGDI
->SharedFace
->Face
;
4434 Status
= STATUS_SUCCESS
;
4437 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
4440 DPRINT1("Can't find OS/2 table - not TT font?\n");
4441 Status
= STATUS_INTERNAL_ERROR
;
4444 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
4447 DPRINT1("Can't find HHEA table - not TT font?\n");
4448 Status
= STATUS_INTERNAL_ERROR
;
4451 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
4453 if (NT_SUCCESS(Status
) || !Error
)
4455 FillTM(&ptmwi
->TextMetric
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
4457 /* FIXME: Fill Diff member */
4458 RtlZeroMemory(&ptmwi
->Diff
, sizeof(ptmwi
->Diff
));
4461 IntUnLockFreeType();
4463 TEXTOBJ_UnlockText(TextObj
);
4467 Status
= STATUS_INVALID_HANDLE
;
4471 if (!NT_SUCCESS(Status
))
4473 SetLastNtError(Status
);
4488 DWORD Result
= GDI_ERROR
;
4489 FT_Face Face
= FontGdi
->SharedFace
->Face
;
4493 if (FT_IS_SFNT(Face
))
4496 Table
= Table
>> 24 | Table
<< 24 | (Table
>> 8 & 0xFF00) |
4497 (Table
<< 8 & 0xFF0000);
4499 if (!Buffer
) Size
= 0;
4504 FT_ULong Needed
= 0;
4506 Error
= FT_Load_Sfnt_Table(Face
, Table
, Offset
, NULL
, &Needed
);
4508 if ( !Error
&& Needed
< Size
) Size
= Needed
;
4510 if (!FT_Load_Sfnt_Table(Face
, Table
, Offset
, Buffer
, &Size
))
4514 IntUnLockFreeType();
4519 #define GOT_PENALTY(name, value) Penalty += (value)
4521 // NOTE: See Table 1. of https://msdn.microsoft.com/en-us/library/ms969909.aspx
4523 GetFontPenalty(const LOGFONTW
* LogFont
,
4524 const OUTLINETEXTMETRICW
* Otm
,
4525 const char * style_name
)
4530 BOOL fNeedScaling
= FALSE
;
4531 const BYTE UserCharSet
= CharSetFromLangID(gusLanguageID
);
4532 const TEXTMETRICW
* TM
= &Otm
->otmTextMetrics
;
4538 /* FIXME: IntSizeSynth Penalty 20 */
4539 /* FIXME: SmallPenalty Penalty 1 */
4540 /* FIXME: FaceNameSubst Penalty 500 */
4542 Byte
= LogFont
->lfCharSet
;
4544 if (Byte
!= TM
->tmCharSet
)
4546 if (Byte
!= DEFAULT_CHARSET
&& Byte
!= ANSI_CHARSET
)
4548 /* CharSet Penalty 65000 */
4549 /* Requested charset does not match the candidate's. */
4550 GOT_PENALTY("CharSet", 65000);
4554 if (UserCharSet
!= TM
->tmCharSet
)
4556 /* UNDOCUMENTED: Not user language */
4557 GOT_PENALTY("UNDOCUMENTED:NotUserLanguage", 100);
4559 if (ANSI_CHARSET
!= TM
->tmCharSet
)
4561 /* UNDOCUMENTED: Not ANSI charset */
4562 GOT_PENALTY("UNDOCUMENTED:NotAnsiCharSet", 100);
4568 Byte
= LogFont
->lfOutPrecision
;
4571 case OUT_DEFAULT_PRECIS
:
4574 case OUT_DEVICE_PRECIS
:
4575 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
) ||
4576 !(TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
)))
4578 /* OutputPrecision Penalty 19000 */
4579 /* Requested OUT_STROKE_PRECIS, but the device can't do it
4580 or the candidate is not a vector font. */
4581 GOT_PENALTY("OutputPrecision", 19000);
4585 if (TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
))
4587 /* OutputPrecision Penalty 19000 */
4588 /* Or OUT_STROKE_PRECIS not requested, and the candidate
4589 is a vector font that requires GDI support. */
4590 GOT_PENALTY("OutputPrecision", 19000);
4595 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4596 if (Byte
== DEFAULT_PITCH
)
4597 Byte
= VARIABLE_PITCH
;
4598 if (Byte
== FIXED_PITCH
)
4600 if (TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
)
4602 /* FixedPitch Penalty 15000 */
4603 /* Requested a fixed pitch font, but the candidate is a
4604 variable pitch font. */
4605 GOT_PENALTY("FixedPitch", 15000);
4608 if (Byte
== VARIABLE_PITCH
)
4610 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4612 /* PitchVariable Penalty 350 */
4613 /* Requested a variable pitch font, but the candidate is not a
4614 variable pitch font. */
4615 GOT_PENALTY("PitchVariable", 350);
4619 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4620 if (Byte
== DEFAULT_PITCH
)
4622 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4624 /* DefaultPitchFixed Penalty 1 */
4625 /* Requested DEFAULT_PITCH, but the candidate is fixed pitch. */
4626 GOT_PENALTY("DefaultPitchFixed", 1);
4630 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
4632 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
4636 /* localized family name */
4639 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4641 /* localized full name */
4644 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
4645 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4649 /* FaceName Penalty 10000 */
4650 /* Requested a face name, but the candidate's face name
4652 GOT_PENALTY("FaceName", 10000);
4656 Byte
= (LogFont
->lfPitchAndFamily
& 0xF0);
4657 if (Byte
!= FF_DONTCARE
)
4659 if (Byte
!= (TM
->tmPitchAndFamily
& 0xF0))
4661 /* Family Penalty 9000 */
4662 /* Requested a family, but the candidate's family is different. */
4663 GOT_PENALTY("Family", 9000);
4667 if ((TM
->tmPitchAndFamily
& 0xF0) == FF_DONTCARE
)
4669 /* FamilyUnknown Penalty 8000 */
4670 /* Requested a family, but the candidate has no family. */
4671 GOT_PENALTY("FamilyUnknown", 8000);
4674 /* Is the candidate a non-vector font? */
4675 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4677 /* Is lfHeight specified? */
4678 if (LogFont
->lfHeight
!= 0)
4680 if (labs(LogFont
->lfHeight
) < TM
->tmHeight
)
4682 /* HeightBigger Penalty 600 */
4683 /* The candidate is a nonvector font and is bigger than the
4684 requested height. */
4685 GOT_PENALTY("HeightBigger", 600);
4686 /* HeightBiggerDifference Penalty 150 */
4687 /* The candidate is a raster font and is larger than the
4688 requested height. Penalty * height difference */
4689 GOT_PENALTY("HeightBiggerDifference", 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
)));
4691 fNeedScaling
= TRUE
;
4693 if (TM
->tmHeight
< labs(LogFont
->lfHeight
))
4695 /* HeightSmaller Penalty 150 */
4696 /* The candidate is a raster font and is smaller than the
4697 requested height. Penalty * height difference */
4698 GOT_PENALTY("HeightSmaller", 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
)));
4700 fNeedScaling
= TRUE
;
4705 switch (LogFont
->lfPitchAndFamily
& 0xF0)
4707 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4708 switch (TM
->tmPitchAndFamily
& 0xF0)
4710 case FF_DECORATIVE
: case FF_SCRIPT
:
4711 /* FamilyUnlikely Penalty 50 */
4712 /* Requested a roman/modern/swiss family, but the
4713 candidate is decorative/script. */
4714 GOT_PENALTY("FamilyUnlikely", 50);
4720 case FF_DECORATIVE
: case FF_SCRIPT
:
4721 switch (TM
->tmPitchAndFamily
& 0xF0)
4723 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4724 /* FamilyUnlikely Penalty 50 */
4725 /* Or requested decorative/script, and the candidate is
4726 roman/modern/swiss. */
4727 GOT_PENALTY("FamilyUnlikely", 50);
4736 if (LogFont
->lfWidth
!= 0)
4738 if (LogFont
->lfWidth
!= TM
->tmAveCharWidth
)
4740 /* Width Penalty 50 */
4741 /* Requested a nonzero width, but the candidate's width
4742 doesn't match. Penalty * width difference */
4743 GOT_PENALTY("Width", 50 * labs(LogFont
->lfWidth
- TM
->tmAveCharWidth
));
4745 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4746 fNeedScaling
= TRUE
;
4752 /* SizeSynth Penalty 50 */
4753 /* The candidate is a raster font that needs scaling by GDI. */
4754 GOT_PENALTY("SizeSynth", 50);
4757 if (!!LogFont
->lfItalic
!= !!TM
->tmItalic
)
4759 if (!LogFont
->lfItalic
&& ItalicFromStyle(style_name
))
4761 /* Italic Penalty 4 */
4762 /* Requested font and candidate font do not agree on italic status,
4763 and the desired result cannot be simulated. */
4764 /* Adjusted to 40 to satisfy (Oblique Penalty > Book Penalty). */
4765 GOT_PENALTY("Italic", 40);
4767 else if (LogFont
->lfItalic
&& !ItalicFromStyle(style_name
))
4769 /* ItalicSim Penalty 1 */
4770 /* Requested italic font but the candidate is not italic,
4771 although italics can be simulated. */
4772 GOT_PENALTY("ItalicSim", 1);
4776 if (LogFont
->lfOutPrecision
== OUT_TT_PRECIS
)
4778 if (!(TM
->tmPitchAndFamily
& TMPF_TRUETYPE
))
4780 /* NotTrueType Penalty 4 */
4781 /* Requested OUT_TT_PRECIS, but the candidate is not a
4783 GOT_PENALTY("NotTrueType", 4);
4787 Long
= LogFont
->lfWeight
;
4788 if (LogFont
->lfWeight
== FW_DONTCARE
)
4790 if (Long
!= TM
->tmWeight
)
4792 /* Weight Penalty 3 */
4793 /* The candidate's weight does not match the requested weight.
4794 Penalty * (weight difference/10) */
4795 GOT_PENALTY("Weight", 3 * (labs(Long
- TM
->tmWeight
) / 10));
4798 if (!LogFont
->lfUnderline
&& TM
->tmUnderlined
)
4800 /* Underline Penalty 3 */
4801 /* Requested font has no underline, but the candidate is
4803 GOT_PENALTY("Underline", 3);
4806 if (!LogFont
->lfStrikeOut
&& TM
->tmStruckOut
)
4808 /* StrikeOut Penalty 3 */
4809 /* Requested font has no strike-out, but the candidate is
4811 GOT_PENALTY("StrikeOut", 3);
4814 /* Is the candidate a non-vector font? */
4815 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4817 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
< LogFont
->lfHeight
)
4819 /* VectorHeightSmaller Penalty 2 */
4820 /* Candidate is a vector font that is smaller than the
4821 requested height. Penalty * height difference */
4822 GOT_PENALTY("VectorHeightSmaller", 2 * labs(TM
->tmHeight
- LogFont
->lfHeight
));
4824 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
> LogFont
->lfHeight
)
4826 /* VectorHeightBigger Penalty 1 */
4827 /* Candidate is a vector font that is bigger than the
4828 requested height. Penalty * height difference */
4829 GOT_PENALTY("VectorHeightBigger", 1 * labs(TM
->tmHeight
- LogFont
->lfHeight
));
4833 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
))
4835 /* DeviceFavor Penalty 2 */
4836 /* Extra penalty for all nondevice fonts. */
4837 GOT_PENALTY("DeviceFavor", 2);
4840 if (TM
->tmAveCharWidth
>= 5 && TM
->tmHeight
>= 5)
4842 if (TM
->tmAveCharWidth
/ TM
->tmHeight
>= 3)
4844 /* Aspect Penalty 30 */
4845 /* The aspect rate is >= 3. It seems like a bad font. */
4846 GOT_PENALTY("Aspect", ((TM
->tmAveCharWidth
/ TM
->tmHeight
) - 2) * 30);
4848 else if (TM
->tmHeight
/ TM
->tmAveCharWidth
>= 3)
4850 /* Aspect Penalty 30 */
4851 /* The aspect rate is >= 3. It seems like a bad font. */
4852 GOT_PENALTY("Aspect", ((TM
->tmHeight
/ TM
->tmAveCharWidth
) - 2) * 30);
4858 DPRINT("WARNING: Penalty:%ld < 200: RequestedNameW:%ls, "
4859 "ActualNameW:%ls, lfCharSet:%d, lfWeight:%ld, "
4860 "tmCharSet:%d, tmWeight:%ld\n",
4861 Penalty
, LogFont
->lfFaceName
, ActualNameW
,
4862 LogFont
->lfCharSet
, LogFont
->lfWeight
,
4863 TM
->tmCharSet
, TM
->tmWeight
);
4866 return Penalty
; /* success */
4871 static __inline VOID
4872 FindBestFontFromList(FONTOBJ
**FontObj
, ULONG
*MatchPenalty
,
4873 const LOGFONTW
*LogFont
,
4874 const PLIST_ENTRY Head
)
4878 PFONT_ENTRY CurrentEntry
;
4880 OUTLINETEXTMETRICW
*Otm
= NULL
;
4881 UINT OtmSize
, OldOtmSize
= 0;
4885 ASSERT(MatchPenalty
);
4889 /* Start with a pretty big buffer */
4891 Otm
= ExAllocatePoolWithTag(PagedPool
, OldOtmSize
, GDITAG_TEXT
);
4893 /* get the FontObj of lowest penalty */
4894 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
4896 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
4898 FontGDI
= CurrentEntry
->Font
;
4900 Face
= FontGDI
->SharedFace
->Face
;
4902 /* get text metrics */
4903 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
4904 if (OtmSize
> OldOtmSize
)
4907 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4908 Otm
= ExAllocatePoolWithTag(PagedPool
, OtmSize
, GDITAG_TEXT
);
4911 /* update FontObj if lowest penalty */
4915 IntRequestFontSize(NULL
, FontGDI
, LogFont
->lfWidth
, LogFont
->lfHeight
);
4916 IntUnLockFreeType();
4918 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, OtmSize
, Otm
);
4922 OldOtmSize
= OtmSize
;
4924 Penalty
= GetFontPenalty(LogFont
, Otm
, Face
->style_name
);
4925 if (*MatchPenalty
== 0xFFFFFFFF || Penalty
< *MatchPenalty
)
4927 *FontObj
= GDIToObj(FontGDI
, FONT
);
4928 *MatchPenalty
= Penalty
;
4934 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4940 IntFontType(PFONTGDI Font
)
4942 PS_FontInfoRec psfInfo
;
4943 FT_ULong tmp_size
= 0;
4944 FT_Face Face
= Font
->SharedFace
->Face
;
4946 ASSERT_FREETYPE_LOCK_NOT_HELD();
4949 if (FT_HAS_MULTIPLE_MASTERS(Face
))
4950 Font
->FontObj
.flFontType
|= FO_MULTIPLEMASTER
;
4951 if (FT_HAS_VERTICAL(Face
))
4952 Font
->FontObj
.flFontType
|= FO_VERT_FACE
;
4953 if (!FT_IS_SCALABLE(Face
))
4954 Font
->FontObj
.flFontType
|= FO_TYPE_RASTER
;
4955 if (FT_IS_SFNT(Face
))
4957 Font
->FontObj
.flFontType
|= FO_TYPE_TRUETYPE
;
4958 if (FT_Get_Sfnt_Table(Face
, ft_sfnt_post
))
4959 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4961 if (!FT_Get_PS_Font_Info(Face
, &psfInfo
))
4963 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4965 /* Check for the presence of the 'CFF ' table to check if the font is Type1 */
4966 if (!FT_Load_Sfnt_Table(Face
, TTAG_CFF
, 0, NULL
, &tmp_size
))
4968 Font
->FontObj
.flFontType
|= (FO_CFF
|FO_POSTSCRIPT
);
4971 IntUnLockFreeType();
4975 MatchFontName(PSHARED_FACE SharedFace
, LPCWSTR lfFaceName
, FT_UShort NameID
, FT_UShort LangID
)
4978 UNICODE_STRING Name1
, Name2
;
4980 if (lfFaceName
[0] == UNICODE_NULL
)
4983 RtlInitUnicodeString(&Name1
, lfFaceName
);
4985 RtlInitUnicodeString(&Name2
, NULL
);
4986 Status
= IntGetFontLocalizedName(&Name2
, SharedFace
, NameID
, LangID
);
4988 if (NT_SUCCESS(Status
))
4990 if (RtlCompareUnicodeString(&Name1
, &Name2
, TRUE
) == 0)
4992 RtlFreeUnicodeString(&Name2
);
4996 RtlFreeUnicodeString(&Name2
);
5003 MatchFontNames(PSHARED_FACE SharedFace
, LPCWSTR lfFaceName
)
5005 if (MatchFontName(SharedFace
, lfFaceName
, TT_NAME_ID_FONT_FAMILY
, LANG_ENGLISH
) ||
5006 MatchFontName(SharedFace
, lfFaceName
, TT_NAME_ID_FULL_NAME
, LANG_ENGLISH
))
5010 if (PRIMARYLANGID(gusLanguageID
) != LANG_ENGLISH
)
5012 if (MatchFontName(SharedFace
, lfFaceName
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
) ||
5013 MatchFontName(SharedFace
, lfFaceName
, TT_NAME_ID_FULL_NAME
, gusLanguageID
))
5023 TextIntRealizeFont(HFONT FontHandle
, PTEXTOBJ pTextObj
)
5025 NTSTATUS Status
= STATUS_SUCCESS
;
5027 PPROCESSINFO Win32Process
;
5030 LOGFONTW SubstitutedLogFont
;
5035 TextObj
= TEXTOBJ_LockText(FontHandle
);
5036 if (NULL
== TextObj
)
5038 return STATUS_INVALID_HANDLE
;
5041 if (TextObj
->fl
& TEXTOBJECT_INIT
)
5043 TEXTOBJ_UnlockText(TextObj
);
5044 return STATUS_SUCCESS
;
5052 pLogFont
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
5055 SubstitutedLogFont
= *pLogFont
;
5056 DPRINT("Font '%S,%u' is substituted by: ", pLogFont
->lfFaceName
, pLogFont
->lfCharSet
);
5057 SubstituteFontRecurse(&SubstitutedLogFont
);
5058 DPRINT("'%S,%u'.\n", SubstitutedLogFont
.lfFaceName
, SubstitutedLogFont
.lfCharSet
);
5060 MatchPenalty
= 0xFFFFFFFF;
5061 TextObj
->Font
= NULL
;
5063 Win32Process
= PsGetCurrentProcessWin32Process();
5065 /* Search private fonts */
5066 IntLockProcessPrivateFonts(Win32Process
);
5067 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
5068 &Win32Process
->PrivateFontListHead
);
5069 IntUnLockProcessPrivateFonts(Win32Process
);
5071 /* Search system fonts */
5072 IntLockGlobalFonts();
5073 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
5075 IntUnLockGlobalFonts();
5077 if (NULL
== TextObj
->Font
)
5079 DPRINT1("Request font %S not found, no fonts loaded at all\n",
5080 pLogFont
->lfFaceName
);
5081 Status
= STATUS_NOT_FOUND
;
5085 UNICODE_STRING Name
;
5086 PFONTGDI FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
5087 PSHARED_FACE SharedFace
= FontGdi
->SharedFace
;
5090 IntRequestFontSize(NULL
, FontGdi
, pLogFont
->lfWidth
, pLogFont
->lfHeight
);
5091 IntUnLockFreeType();
5093 TextObj
->TextFace
[0] = UNICODE_NULL
;
5094 if (MatchFontNames(SharedFace
, SubstitutedLogFont
.lfFaceName
))
5096 RtlStringCchCopyW(TextObj
->TextFace
, _countof(TextObj
->TextFace
), pLogFont
->lfFaceName
);
5100 RtlInitUnicodeString(&Name
, NULL
);
5101 Status
= IntGetFontLocalizedName(&Name
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
5102 if (NT_SUCCESS(Status
))
5104 /* truncated copy */
5105 IntUnicodeStringToBuffer(TextObj
->TextFace
, sizeof(TextObj
->TextFace
), &Name
);
5106 RtlFreeUnicodeString(&Name
);
5110 // Need hdev, when freetype is loaded need to create DEVOBJ for
5111 // Consumer and Producer.
5112 TextObj
->Font
->iUniq
= 1; // Now it can be cached.
5113 IntFontType(FontGdi
);
5114 FontGdi
->flType
= TextObj
->Font
->flFontType
;
5115 FontGdi
->RequestUnderline
= pLogFont
->lfUnderline
? 0xFF : 0;
5116 FontGdi
->RequestStrikeOut
= pLogFont
->lfStrikeOut
? 0xFF : 0;
5117 FontGdi
->RequestItalic
= pLogFont
->lfItalic
? 0xFF : 0;
5118 if (pLogFont
->lfWeight
!= FW_DONTCARE
)
5119 FontGdi
->RequestWeight
= pLogFont
->lfWeight
;
5121 FontGdi
->RequestWeight
= FW_NORMAL
;
5123 Face
= FontGdi
->SharedFace
->Face
;
5125 //FontGdi->OriginalWeight = WeightFromStyle(Face->style_name);
5127 if (!FontGdi
->OriginalItalic
)
5128 FontGdi
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
5130 TextObj
->fl
|= TEXTOBJECT_INIT
;
5131 Status
= STATUS_SUCCESS
;
5134 if (!pTextObj
) TEXTOBJ_UnlockText(TextObj
);
5136 ASSERT((NT_SUCCESS(Status
) ^ (NULL
== TextObj
->Font
)) != 0);
5146 POBJECT_NAME_INFORMATION NameInfo
,
5148 PUNICODE_STRING FileName
)
5151 OBJECT_ATTRIBUTES ObjectAttributes
;
5153 IO_STATUS_BLOCK IoStatusBlock
;
5156 InitializeObjectAttributes(&ObjectAttributes
,
5158 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
5162 Status
= ZwOpenFile(
5164 0, // FILE_READ_ATTRIBUTES,
5167 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
5170 if (!NT_SUCCESS(Status
))
5172 DPRINT("ZwOpenFile() failed (Status = 0x%lx)\n", Status
);
5176 Status
= ZwQueryObject(hFile
, ObjectNameInformation
, NameInfo
, Size
, &Desired
);
5178 if (!NT_SUCCESS(Status
))
5180 DPRINT("ZwQueryObject() failed (Status = %lx)\n", Status
);
5188 EqualFamilyInfo(const FONTFAMILYINFO
*pInfo1
, const FONTFAMILYINFO
*pInfo2
)
5190 const ENUMLOGFONTEXW
*pLog1
= &pInfo1
->EnumLogFontEx
;
5191 const ENUMLOGFONTEXW
*pLog2
= &pInfo2
->EnumLogFontEx
;
5192 const LOGFONTW
*plf1
= &pLog1
->elfLogFont
;
5193 const LOGFONTW
*plf2
= &pLog2
->elfLogFont
;
5195 if (_wcsicmp(plf1
->lfFaceName
, plf2
->lfFaceName
) != 0)
5200 if (_wcsicmp(pLog1
->elfStyle
, pLog2
->elfStyle
) != 0)
5209 IntAddNameFromFamInfo(LPWSTR psz
, FONTFAMILYINFO
*FamInfo
)
5211 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfLogFont
.lfFaceName
);
5212 if (FamInfo
->EnumLogFontEx
.elfStyle
[0] &&
5213 _wcsicmp(FamInfo
->EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
5216 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfStyle
);
5222 IntGdiGetFontResourceInfo(
5223 PUNICODE_STRING FileName
,
5228 UNICODE_STRING EntryFileName
;
5229 POBJECT_NAME_INFORMATION NameInfo1
= NULL
, NameInfo2
= NULL
;
5230 PLIST_ENTRY ListEntry
;
5231 PFONT_ENTRY FontEntry
;
5232 ULONG Size
, i
, Count
;
5235 FONTFAMILYINFO
*FamInfo
;
5236 const ULONG MaxFamInfo
= 64;
5237 const ULONG MAX_FAM_INFO_BYTES
= sizeof(FONTFAMILYINFO
) * MaxFamInfo
;
5239 const ULONG NAMEINFO_SIZE
= sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
* sizeof(WCHAR
);
5241 DPRINT("IntGdiGetFontResourceInfo: dwType == %lu\n", dwType
);
5245 /* Create buffer for full path name */
5246 NameInfo1
= ExAllocatePoolWithTag(PagedPool
, NAMEINFO_SIZE
, TAG_FINF
);
5250 /* Get the full path name */
5251 if (!IntGetFullFileName(NameInfo1
, NAMEINFO_SIZE
, FileName
))
5254 /* Create a buffer for the entries' names */
5255 NameInfo2
= ExAllocatePoolWithTag(PagedPool
, NAMEINFO_SIZE
, TAG_FINF
);
5259 FamInfo
= ExAllocatePoolWithTag(PagedPool
, MAX_FAM_INFO_BYTES
, TAG_FINF
);
5262 if (!NameInfo1
|| !NameInfo2
|| !FamInfo
)
5265 ExFreePoolWithTag(NameInfo2
, TAG_FINF
);
5268 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
5270 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5276 /* Try to find the pathname in the global font list */
5277 IntLockGlobalFonts();
5278 for (ListEntry
= g_FontListHead
.Flink
; ListEntry
!= &g_FontListHead
;
5279 ListEntry
= ListEntry
->Flink
)
5281 FontEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY
, ListEntry
);
5282 if (FontEntry
->Font
->Filename
== NULL
)
5285 RtlInitUnicodeString(&EntryFileName
, FontEntry
->Font
->Filename
);
5286 if (!IntGetFullFileName(NameInfo2
, NAMEINFO_SIZE
, &EntryFileName
))
5289 if (!RtlEqualUnicodeString(&NameInfo1
->Name
, &NameInfo2
->Name
, FALSE
))
5293 FontFamilyFillInfo(&FamInfo
[Count
], FontEntry
->FaceName
.Buffer
,
5294 NULL
, FontEntry
->Font
);
5295 for (i
= 0; i
< Count
; ++i
)
5297 if (EqualFamilyInfo(&FamInfo
[i
], &FamInfo
[Count
]))
5307 if (Count
>= MaxFamInfo
)
5311 IntUnLockGlobalFonts();
5313 /* Free the buffers */
5314 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
5315 ExFreePoolWithTag(NameInfo2
, TAG_FINF
);
5317 if (Count
== 0 && dwType
!= 5)
5319 /* Font could not be found in system table
5320 dwType == 5 will still handle this */
5321 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
5328 case 0: /* FIXME: Returns 1 or 2, don't know what this is atm */
5329 Size
= sizeof(DWORD
);
5337 if (*pdwBytes
>= Size
)
5339 *(DWORD
*)pBuffer
= Count
;
5346 case 1: /* copy the font title */
5347 /* calculate the required size */
5349 for (i
= 0; i
< Count
; ++i
)
5352 Size
+= 3; /* " & " */
5353 Size
+= wcslen(FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
5354 if (FamInfo
[i
].EnumLogFontEx
.elfStyle
[0] &&
5355 _wcsicmp(FamInfo
[i
].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
5357 Size
+= 1 + wcslen(FamInfo
[i
].EnumLogFontEx
.elfStyle
);
5360 Size
+= 2; /* "\0\0" */
5361 Size
*= sizeof(WCHAR
);
5370 if (*pdwBytes
>= Size
)
5372 /* store font title to buffer */
5373 WCHAR
*psz
= pBuffer
;
5375 for (i
= 0; i
< Count
; ++i
)
5378 wcscat(psz
, L
" & ");
5379 IntAddNameFromFamInfo(psz
, &FamInfo
[i
]);
5381 psz
[wcslen(psz
) + 1] = UNICODE_NULL
;
5387 *pdwBytes
= 1024; /* this is confirmed value */
5392 case 2: /* Copy an array of LOGFONTW */
5393 Size
= Count
* sizeof(LOGFONTW
);
5401 if (*pdwBytes
>= Size
)
5403 pbBuffer
= (LPBYTE
)pBuffer
;
5404 for (i
= 0; i
< Count
; ++i
)
5406 FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfWidth
= 0;
5407 RtlCopyMemory(pbBuffer
, &FamInfo
[i
].EnumLogFontEx
.elfLogFont
, sizeof(LOGFONTW
));
5408 pbBuffer
+= sizeof(LOGFONTW
);
5416 *pdwBytes
= 1024; /* this is confirmed value */
5421 Size
= sizeof(DWORD
);
5429 if (*pdwBytes
>= Size
)
5431 /* FIXME: What exactly is copied here? */
5432 *(DWORD
*)pBuffer
= 1;
5439 case 4: /* full file path */
5440 if (FileName
->Length
>= 4 * sizeof(WCHAR
))
5442 /* The beginning of FileName is \??\ */
5443 LPWSTR pch
= FileName
->Buffer
+ 4;
5444 DWORD Length
= FileName
->Length
- 4 * sizeof(WCHAR
);
5446 Size
= Length
+ sizeof(WCHAR
);
5454 if (*pdwBytes
>= Size
)
5456 RtlCopyMemory(pBuffer
, pch
, Size
);
5464 case 5: /* Looks like a BOOL that is copied, TRUE, if the font was not found */
5465 Size
= sizeof(BOOL
);
5473 if (*pdwBytes
>= Size
)
5475 *(BOOL
*)pBuffer
= Count
== 0;
5482 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
5490 ftGdiRealizationInfo(PFONTGDI Font
, PREALIZATION_INFO Info
)
5492 if (FT_HAS_FIXED_SIZES(Font
->SharedFace
->Face
))
5493 Info
->iTechnology
= RI_TECH_BITMAP
;
5496 if (FT_IS_SCALABLE(Font
->SharedFace
->Face
))
5497 Info
->iTechnology
= RI_TECH_SCALABLE
;
5499 Info
->iTechnology
= RI_TECH_FIXED
;
5501 Info
->iUniq
= Font
->FontObj
.iUniq
;
5502 Info
->dwUnknown
= -1;
5509 ftGdiGetKerningPairs( PFONTGDI Font
,
5511 LPKERNINGPAIR pKerningPair
)
5515 FT_Face face
= Font
->SharedFace
->Face
;
5517 if (FT_HAS_KERNING(face
) && face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
5519 FT_UInt previous_index
= 0, glyph_index
= 0;
5520 FT_ULong char_code
, char_previous
;
5523 char_previous
= char_code
= FT_Get_First_Char(face
, &glyph_index
);
5529 if (previous_index
&& glyph_index
)
5531 FT_Get_Kerning(face
, previous_index
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
5533 if (pKerningPair
&& cPairs
)
5535 pKerningPair
[i
].wFirst
= char_previous
;
5536 pKerningPair
[i
].wSecond
= char_code
;
5537 pKerningPair
[i
].iKernAmount
= delta
.x
;
5539 if (i
== cPairs
) break;
5543 previous_index
= glyph_index
;
5544 char_previous
= char_code
;
5545 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_index
);
5547 IntUnLockFreeType();
5553 ///////////////////////////////////////////////////////////////////////////
5555 // Functions needing sorting.
5557 ///////////////////////////////////////////////////////////////////////////
5560 IntGetFontFamilyInfo(HDC Dc
,
5561 const LOGFONTW
*SafeLogFont
,
5562 PFONTFAMILYINFO SafeInfo
,
5565 LONG AvailCount
= 0;
5566 PPROCESSINFO Win32Process
;
5568 /* Enumerate font families in the global list */
5569 IntLockGlobalFonts();
5570 if (!GetFontFamilyInfoForList(SafeLogFont
, SafeInfo
, NULL
, &AvailCount
,
5571 InfoCount
, &g_FontListHead
))
5573 IntUnLockGlobalFonts();
5576 IntUnLockGlobalFonts();
5578 /* Enumerate font families in the process local list */
5579 Win32Process
= PsGetCurrentProcessWin32Process();
5580 IntLockProcessPrivateFonts(Win32Process
);
5581 if (!GetFontFamilyInfoForList(SafeLogFont
, SafeInfo
, NULL
, &AvailCount
, InfoCount
,
5582 &Win32Process
->PrivateFontListHead
))
5584 IntUnLockProcessPrivateFonts(Win32Process
);
5587 IntUnLockProcessPrivateFonts(Win32Process
);
5589 /* Enumerate font families in the registry */
5590 if (!GetFontFamilyInfoForSubstitutes(SafeLogFont
, SafeInfo
, &AvailCount
, InfoCount
))
5599 NtGdiGetFontFamilyInfo(HDC Dc
,
5600 const LOGFONTW
*UnsafeLogFont
,
5601 PFONTFAMILYINFO UnsafeInfo
,
5602 LPLONG UnsafeInfoCount
)
5606 PFONTFAMILYINFO Info
;
5607 LONG GotCount
, AvailCount
, SafeInfoCount
;
5610 if (UnsafeLogFont
== NULL
|| UnsafeInfo
== NULL
|| UnsafeInfoCount
== NULL
)
5612 EngSetLastError(ERROR_INVALID_PARAMETER
);
5616 Status
= MmCopyFromCaller(&SafeInfoCount
, UnsafeInfoCount
, sizeof(SafeInfoCount
));
5617 if (!NT_SUCCESS(Status
))
5619 EngSetLastError(ERROR_INVALID_PARAMETER
);
5623 Status
= MmCopyToCaller(UnsafeInfoCount
, &GotCount
, sizeof(*UnsafeInfoCount
));
5624 if (!NT_SUCCESS(Status
))
5626 EngSetLastError(ERROR_INVALID_PARAMETER
);
5629 Status
= MmCopyFromCaller(&LogFont
, UnsafeLogFont
, sizeof(LOGFONTW
));
5630 if (!NT_SUCCESS(Status
))
5632 EngSetLastError(ERROR_INVALID_PARAMETER
);
5635 if (SafeInfoCount
<= 0)
5637 EngSetLastError(ERROR_INVALID_PARAMETER
);
5641 /* Allocate space for a safe copy */
5642 Status
= RtlULongMult(SafeInfoCount
, sizeof(FONTFAMILYINFO
), &DataSize
);
5643 if (!NT_SUCCESS(Status
) || DataSize
> LONG_MAX
)
5645 DPRINT1("Overflowed.\n");
5646 EngSetLastError(ERROR_INVALID_PARAMETER
);
5649 Info
= ExAllocatePoolWithTag(PagedPool
, DataSize
, GDITAG_TEXT
);
5652 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5656 /* Retrieve the information */
5657 AvailCount
= IntGetFontFamilyInfo(Dc
, &LogFont
, Info
, SafeInfoCount
);
5658 GotCount
= min(AvailCount
, SafeInfoCount
);
5659 SafeInfoCount
= AvailCount
;
5661 /* Return data to caller */
5664 Status
= RtlULongMult(GotCount
, sizeof(FONTFAMILYINFO
), &DataSize
);
5665 if (!NT_SUCCESS(Status
) || DataSize
> LONG_MAX
)
5667 DPRINT1("Overflowed.\n");
5668 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5669 EngSetLastError(ERROR_INVALID_PARAMETER
);
5672 Status
= MmCopyToCaller(UnsafeInfo
, Info
, DataSize
);
5673 if (!NT_SUCCESS(Status
))
5675 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5676 EngSetLastError(ERROR_INVALID_PARAMETER
);
5679 Status
= MmCopyToCaller(UnsafeInfoCount
, &SafeInfoCount
, sizeof(*UnsafeInfoCount
));
5680 if (!NT_SUCCESS(Status
))
5682 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5683 EngSetLastError(ERROR_INVALID_PARAMETER
);
5688 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5695 ScaleLong(LONG lValue
, PFLOATOBJ pef
)
5699 /* Check if we have scaling different from 1 */
5700 if (!FLOATOBJ_Equal(pef
, (PFLOATOBJ
)&gef1
))
5702 /* Need to multiply */
5703 FLOATOBJ_SetLong(&efTemp
, lValue
);
5704 FLOATOBJ_Mul(&efTemp
, pef
);
5705 lValue
= FLOATOBJ_GetLong(&efTemp
);
5718 IN OPTIONAL PRECTL lprc
,
5721 IN OPTIONAL LPINT Dx
,
5722 IN DWORD dwCodePage
)
5726 * Call EngTextOut, which does the real work (calling DrvTextOut where
5732 SURFACE
*psurf
= NULL
;
5733 int error
, glyph_index
, i
;
5736 FT_BitmapGlyph realglyph
;
5737 LONGLONG TextLeft
, RealXStart
;
5738 ULONG TextTop
, previous
, BackgroundLeft
;
5739 FT_Bool use_kerning
;
5740 RECTL DestRect
, MaskRect
;
5741 POINTL SourcePoint
, BrushOrigin
;
5742 HBITMAP HSourceGlyph
;
5743 SURFOBJ
*SourceGlyphSurf
;
5748 PTEXTOBJ TextObj
= NULL
;
5749 EXLATEOBJ exloRGB2Dst
, exloDst2RGB
;
5750 FT_Render_Mode RenderMode
;
5753 BOOL DoBreak
= FALSE
;
5755 PMATRIX pmxWorldToDevice
;
5756 LONG fixAscender
, fixDescender
;
5759 BOOL EmuBold
, EmuItalic
;
5763 /* Check if String is valid */
5764 if ((Count
> 0xFFFF) || (Count
> 0 && String
== NULL
))
5766 EngSetLastError(ERROR_INVALID_PARAMETER
);
5770 Render
= IntIsFontRenderingEnabled();
5772 if (PATH_IsPathOpen(dc
->dclevel
))
5774 bResult
= PATH_ExtTextOut(dc
,
5778 (const RECTL
*)lprc
,
5785 DC_vPrepareDCsForBlit(dc
, NULL
, NULL
, NULL
);
5787 if (!dc
->dclevel
.pSurface
)
5789 /* Memory DC with no surface selected */
5794 pdcattr
= dc
->pdcattr
;
5796 if (lprc
&& (fuOptions
& (ETO_OPAQUE
| ETO_CLIPPED
)))
5798 IntLPtoDP(dc
, (POINT
*)lprc
, 2);
5801 if (pdcattr
->flTextAlign
& TA_UPDATECP
)
5803 Start
.x
= pdcattr
->ptlCurrent
.x
;
5804 Start
.y
= pdcattr
->ptlCurrent
.y
;
5810 IntLPtoDP(dc
, &Start
, 1);
5811 RealXStart
= ((LONGLONG
)Start
.x
+ dc
->ptlDCOrig
.x
) << 6;
5812 YStart
= Start
.y
+ dc
->ptlDCOrig
.y
;
5821 if ((fuOptions
& ETO_OPAQUE
) && lprc
)
5823 DestRect
.left
= lprc
->left
;
5824 DestRect
.top
= lprc
->top
;
5825 DestRect
.right
= lprc
->right
;
5826 DestRect
.bottom
= lprc
->bottom
;
5828 DestRect
.left
+= dc
->ptlDCOrig
.x
;
5829 DestRect
.top
+= dc
->ptlDCOrig
.y
;
5830 DestRect
.right
+= dc
->ptlDCOrig
.x
;
5831 DestRect
.bottom
+= dc
->ptlDCOrig
.y
;
5833 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5835 IntUpdateBoundsRect(dc
, &DestRect
);
5838 if (pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
5839 DC_vUpdateBackgroundBrush(dc
);
5840 if (dc
->dctype
== DCTYPE_DIRECT
)
5841 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5843 psurf
= dc
->dclevel
.pSurface
;
5853 &dc
->eboBackground
.BrushObject
,
5855 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5857 if (dc
->dctype
== DCTYPE_DIRECT
)
5858 MouseSafetyOnDrawEnd(dc
->ppdev
);
5860 fuOptions
&= ~ETO_OPAQUE
;
5864 if (pdcattr
->jBkMode
== OPAQUE
)
5866 fuOptions
|= ETO_OPAQUE
;
5870 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
5871 if (TextObj
== NULL
)
5877 FontObj
= TextObj
->Font
;
5879 FontGDI
= ObjToGDI(FontObj
, FONT
);
5883 face
= FontGDI
->SharedFace
->Face
;
5885 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
5886 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
5887 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
5890 RenderMode
= IntGetFontRenderMode(plf
);
5892 RenderMode
= FT_RENDER_MODE_MONO
;
5894 if (!TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
))
5896 IntUnLockFreeType();
5901 /* NOTE: Don't trust face->size->metrics.ascender and descender values. */
5902 if (dc
->pdcattr
->iGraphicsMode
== GM_ADVANCED
)
5904 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
5905 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5907 fixAscender
= ScaleLong(FontGDI
->tmAscent
, &pmxWorldToDevice
->efM22
) << 6;
5908 fixDescender
= ScaleLong(FontGDI
->tmDescent
, &pmxWorldToDevice
->efM22
) << 6;
5912 pmxWorldToDevice
= (PMATRIX
)&gmxWorldToDeviceDefault
;
5913 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5915 fixAscender
= FontGDI
->tmAscent
<< 6;
5916 fixDescender
= FontGDI
->tmDescent
<< 6;
5920 * Process the vertical alignment and determine the yoff.
5922 #define VALIGN_MASK (TA_TOP | TA_BASELINE | TA_BOTTOM)
5923 if ((pdcattr
->flTextAlign
& VALIGN_MASK
) == TA_BASELINE
)
5925 else if ((pdcattr
->flTextAlign
& VALIGN_MASK
) == TA_BOTTOM
)
5926 yoff
= -(fixDescender
>> 6);
5928 yoff
= fixAscender
>> 6;
5931 use_kerning
= FT_HAS_KERNING(face
);
5935 * Process the horizontal alignment and modify XStart accordingly.
5937 DxShift
= fuOptions
& ETO_PDY
? 1 : 0;
5938 if (pdcattr
->flTextAlign
& (TA_RIGHT
| TA_CENTER
))
5940 ULONGLONG TextWidth
= 0;
5941 LPCWSTR TempText
= String
;
5945 * Calculate width of the text.
5950 iStart
= Count
< 2 ? 0 : Count
- 2;
5951 TextWidth
= Count
< 2 ? 0 : (Dx
[(Count
-2)<<DxShift
] << 6);
5957 TempText
= String
+ iStart
;
5959 for (i
= iStart
; i
< Count
; i
++)
5961 glyph_index
= get_glyph_index_flagged(face
, *TempText
, ETO_GLYPH_INDEX
, fuOptions
);
5963 if (EmuBold
|| EmuItalic
)
5966 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
5967 RenderMode
, pmxWorldToDevice
);
5970 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5973 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
5976 glyph
= face
->glyph
;
5977 if (EmuBold
|| EmuItalic
)
5980 FT_GlyphSlot_Embolden(glyph
);
5982 FT_GlyphSlot_Oblique(glyph
);
5983 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5987 realglyph
= ftGdiGlyphCacheSet(face
,
5996 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5997 IntUnLockFreeType();
6002 /* Retrieve kerning distance */
6003 if (use_kerning
&& previous
&& glyph_index
)
6006 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
6007 TextWidth
+= delta
.x
;
6010 TextWidth
+= realglyph
->root
.advance
.x
>> 10;
6012 if (EmuBold
|| EmuItalic
)
6014 FT_Done_Glyph((FT_Glyph
)realglyph
);
6018 previous
= glyph_index
;
6024 if ((pdcattr
->flTextAlign
& TA_CENTER
) == TA_CENTER
)
6026 RealXStart
-= TextWidth
/ 2;
6030 RealXStart
-= TextWidth
;
6034 psurf
= dc
->dclevel
.pSurface
;
6035 SurfObj
= &psurf
->SurfObj
;
6037 if ((fuOptions
& ETO_OPAQUE
) && (dc
->pdcattr
->ulDirty_
& DIRTY_BACKGROUND
))
6038 DC_vUpdateBackgroundBrush(dc
) ;
6040 if(dc
->pdcattr
->ulDirty_
& DIRTY_TEXT
)
6041 DC_vUpdateTextBrush(dc
) ;
6043 if (!face
->units_per_EM
)
6049 thickness
= face
->underline_thickness
*
6050 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
6055 if ((fuOptions
& ETO_OPAQUE
) && plf
->lfItalic
)
6057 /* Draw background */
6058 TextLeft
= RealXStart
;
6060 BackgroundLeft
= (RealXStart
+ 32) >> 6;
6061 for (i
= 0; i
< Count
; ++i
)
6063 glyph_index
= get_glyph_index_flagged(face
, String
[i
], ETO_GLYPH_INDEX
, fuOptions
);
6065 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6068 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
6069 IntUnLockFreeType();
6073 glyph
= face
->glyph
;
6075 FT_GlyphSlot_Embolden(glyph
);
6077 FT_GlyphSlot_Oblique(glyph
);
6078 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
6081 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
6082 IntUnLockFreeType();
6086 /* retrieve kerning distance and move pen position */
6087 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
6090 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
6091 TextLeft
+= delta
.x
;
6093 DPRINT("TextLeft: %I64d\n", TextLeft
);
6094 DPRINT("TextTop: %lu\n", TextTop
);
6095 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
6097 DestRect
.left
= BackgroundLeft
;
6098 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
6099 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
6100 DestRect
.bottom
= DestRect
.top
+ ((fixAscender
+ fixDescender
) >> 6);
6101 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
6102 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
6104 IntUpdateBoundsRect(dc
, &DestRect
);
6115 &dc
->eboBackground
.BrushObject
,
6117 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
6118 MouseSafetyOnDrawEnd(dc
->ppdev
);
6119 BackgroundLeft
= DestRect
.right
;
6121 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
6122 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
6123 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
6124 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
6126 bitSize
.cx
= realglyph
->bitmap
.width
;
6127 bitSize
.cy
= realglyph
->bitmap
.rows
;
6128 MaskRect
.right
= realglyph
->bitmap
.width
;
6129 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
6133 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
6134 DPRINT("New TextLeft: %I64d\n", TextLeft
);
6138 // FIXME this should probably be a matrix transform with TextTop as well.
6139 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
6140 if (FLOATOBJ_Equal0(&Scale
))
6141 FLOATOBJ_Set1(&Scale
);
6143 /* do the shift before multiplying to preserve precision */
6144 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
6145 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
6146 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
6151 TextTop
-= Dx
[2 * i
+ 1] << 6;
6154 previous
= glyph_index
;
6156 if (EmuBold
|| EmuItalic
)
6158 FT_Done_Glyph((FT_Glyph
)realglyph
);
6164 EXLATEOBJ_vInitialize(&exloRGB2Dst
, &gpalRGB
, psurf
->ppal
, 0, 0, 0);
6165 EXLATEOBJ_vInitialize(&exloDst2RGB
, psurf
->ppal
, &gpalRGB
, 0, 0, 0);
6167 /* Assume success */
6171 * The main rendering loop.
6173 TextLeft
= RealXStart
;
6175 BackgroundLeft
= (RealXStart
+ 32) >> 6;
6176 for (i
= 0; i
< Count
; ++i
)
6178 glyph_index
= get_glyph_index_flagged(face
, String
[i
], ETO_GLYPH_INDEX
, fuOptions
);
6180 if (EmuBold
|| EmuItalic
)
6183 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
6184 RenderMode
, pmxWorldToDevice
);
6187 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6190 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
6195 glyph
= face
->glyph
;
6196 if (EmuBold
|| EmuItalic
)
6199 FT_GlyphSlot_Embolden(glyph
);
6201 FT_GlyphSlot_Oblique(glyph
);
6202 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
6206 realglyph
= ftGdiGlyphCacheSet(face
,
6215 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
6221 /* retrieve kerning distance and move pen position */
6222 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
6225 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
6226 TextLeft
+= delta
.x
;
6228 DPRINT("TextLeft: %I64d\n", TextLeft
);
6229 DPRINT("TextTop: %lu\n", TextTop
);
6230 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
6232 if ((fuOptions
& ETO_OPAQUE
) && !plf
->lfItalic
)
6234 DestRect
.left
= BackgroundLeft
;
6235 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
6236 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
6237 DestRect
.bottom
= DestRect
.top
+ ((fixAscender
+ fixDescender
) >> 6);
6239 if (dc
->dctype
== DCTYPE_DIRECT
)
6240 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
6242 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
6244 IntUpdateBoundsRect(dc
, &DestRect
);
6255 &dc
->eboBackground
.BrushObject
,
6257 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
6259 if (dc
->dctype
== DCTYPE_DIRECT
)
6260 MouseSafetyOnDrawEnd(dc
->ppdev
);
6262 BackgroundLeft
= DestRect
.right
;
6265 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
6266 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
6267 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
6268 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
6270 bitSize
.cx
= realglyph
->bitmap
.width
;
6271 bitSize
.cy
= realglyph
->bitmap
.rows
;
6272 MaskRect
.right
= realglyph
->bitmap
.width
;
6273 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
6275 /* Check if the bitmap has any pixels */
6276 if ((bitSize
.cx
!= 0) && (bitSize
.cy
!= 0))
6279 * We should create the bitmap out of the loop at the biggest possible
6280 * glyph size. Then use memset with 0 to clear it and sourcerect to
6281 * limit the work of the transbitblt.
6283 HSourceGlyph
= EngCreateBitmap(bitSize
, realglyph
->bitmap
.pitch
,
6284 BMF_8BPP
, BMF_TOPDOWN
,
6285 realglyph
->bitmap
.buffer
);
6286 if ( !HSourceGlyph
)
6288 DPRINT1("WARNING: EngCreateBitmap() failed!\n");
6289 // FT_Done_Glyph(realglyph);
6293 SourceGlyphSurf
= EngLockSurface((HSURF
)HSourceGlyph
);
6294 if ( !SourceGlyphSurf
)
6296 EngDeleteSurface((HSURF
)HSourceGlyph
);
6297 DPRINT1("WARNING: EngLockSurface() failed!\n");
6303 * Use the font data as a mask to paint onto the DCs surface using a
6306 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
6307 DestRect
.right
>= lprc
->right
+ dc
->ptlDCOrig
.x
)
6309 // We do the check '>=' instead of '>' to possibly save an iteration
6310 // through this loop, since it's breaking after the drawing is done,
6311 // and x is always incremented.
6312 DestRect
.right
= lprc
->right
+ dc
->ptlDCOrig
.x
;
6315 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
6316 DestRect
.bottom
>= lprc
->bottom
+ dc
->ptlDCOrig
.y
)
6318 DestRect
.bottom
= lprc
->bottom
+ dc
->ptlDCOrig
.y
;
6321 if (dc
->dctype
== DCTYPE_DIRECT
)
6322 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
6332 &dc
->eboText
.BrushObject
,
6335 DPRINT1("Failed to MaskBlt a glyph!\n");
6338 if (dc
->dctype
== DCTYPE_DIRECT
)
6339 MouseSafetyOnDrawEnd(dc
->ppdev
) ;
6341 EngUnlockSurface(SourceGlyphSurf
);
6342 EngDeleteSurface((HSURF
)HSourceGlyph
);
6350 if (plf
->lfUnderline
)
6353 if (!face
->units_per_EM
)
6359 position
= face
->underline_position
*
6360 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
6362 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
6366 &dc
->eboText
.BrushObject
,
6368 TextTop
+ yoff
- position
+ i
,
6369 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
6370 TextTop
+ yoff
- position
+ i
,
6372 ROP2_TO_MIX(R2_COPYPEN
));
6375 if (plf
->lfStrikeOut
)
6378 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
6382 &dc
->eboText
.BrushObject
,
6384 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
6385 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
6386 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
6388 ROP2_TO_MIX(R2_COPYPEN
));
6394 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
6395 DPRINT("New TextLeft: %I64d\n", TextLeft
);
6399 // FIXME this should probably be a matrix transform with TextTop as well.
6400 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
6401 if (FLOATOBJ_Equal0(&Scale
))
6402 FLOATOBJ_Set1(&Scale
);
6404 /* do the shift before multiplying to preserve precision */
6405 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
6406 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
6407 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
6412 TextTop
-= Dx
[2 * i
+ 1] << 6;
6415 previous
= glyph_index
;
6417 if (EmuBold
|| EmuItalic
)
6419 FT_Done_Glyph((FT_Glyph
)realglyph
);
6424 if (pdcattr
->flTextAlign
& TA_UPDATECP
) {
6425 pdcattr
->ptlCurrent
.x
= DestRect
.right
- dc
->ptlDCOrig
.x
;
6428 IntUnLockFreeType();
6430 EXLATEOBJ_vCleanup(&exloRGB2Dst
);
6431 EXLATEOBJ_vCleanup(&exloDst2RGB
);
6435 DC_vFinishBlit(dc
, NULL
);
6437 if (TextObj
!= NULL
)
6438 TEXTOBJ_UnlockText(TextObj
);
6451 IN OPTIONAL PRECTL lprc
,
6454 IN OPTIONAL LPINT Dx
,
6455 IN DWORD dwCodePage
)
6460 // TODO: Write test-cases to exactly match real Windows in different
6461 // bad parameters (e.g. does Windows check the DC or the RECT first?).
6462 dc
= DC_LockDc(hDC
);
6465 EngSetLastError(ERROR_INVALID_HANDLE
);
6469 bResult
= IntExtTextOutW( dc
,
6484 #define STACK_TEXT_BUFFER_SIZE 100
6492 IN OPTIONAL LPRECT UnsafeRect
,
6493 IN LPWSTR UnsafeString
,
6495 IN OPTIONAL LPINT UnsafeDx
,
6496 IN DWORD dwCodePage
)
6498 BOOL Result
= FALSE
;
6499 NTSTATUS Status
= STATUS_SUCCESS
;
6501 BYTE LocalBuffer
[STACK_TEXT_BUFFER_SIZE
];
6502 PVOID Buffer
= LocalBuffer
;
6503 LPCWSTR SafeString
= NULL
;
6504 LPINT SafeDx
= NULL
;
6505 ULONG BufSize
, StringSize
, DxSize
= 0;
6507 /* Check if String is valid */
6508 if ((Count
> 0xFFFF) || (Count
> 0 && UnsafeString
== NULL
))
6510 EngSetLastError(ERROR_INVALID_PARAMETER
);
6516 /* Calculate buffer size for string and Dx values */
6517 BufSize
= StringSize
= Count
* sizeof(WCHAR
);
6520 /* If ETO_PDY is specified, we have pairs of INTs */
6521 DxSize
= (Count
* sizeof(INT
)) * (fuOptions
& ETO_PDY
? 2 : 1);
6525 /* Check if our local buffer is large enough */
6526 if (BufSize
> STACK_TEXT_BUFFER_SIZE
)
6528 /* It's not, allocate a temp buffer */
6529 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufSize
, GDITAG_TEXT
);
6536 /* Probe and copy user mode data to the buffer */
6539 /* Put the Dx before the String to assure alignment of 4 */
6540 SafeString
= (LPCWSTR
)(((ULONG_PTR
)Buffer
) + DxSize
);
6542 /* Probe and copy the string */
6543 ProbeForRead(UnsafeString
, StringSize
, 1);
6544 RtlCopyMemory((PVOID
)SafeString
, UnsafeString
, StringSize
);
6546 /* If we have Dx values... */
6549 /* ... probe and copy them */
6551 ProbeForRead(UnsafeDx
, DxSize
, 1);
6552 RtlCopyMemory(SafeDx
, UnsafeDx
, DxSize
);
6555 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6557 Status
= _SEH2_GetExceptionCode();
6560 if (!NT_SUCCESS(Status
))
6566 /* If we have a rect, copy it */
6571 ProbeForRead(UnsafeRect
, sizeof(RECT
), 1);
6572 SafeRect
= *UnsafeRect
;
6574 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6576 Status
= _SEH2_GetExceptionCode();
6579 if (!NT_SUCCESS(Status
))
6585 /* Finally call the internal routine */
6586 Result
= GreExtTextOutW(hDC
,
6597 /* If we allocated a buffer, free it */
6598 if (Buffer
!= LocalBuffer
)
6600 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
6612 NtGdiGetCharABCWidthsW(
6616 IN OPTIONAL PWCHAR UnSafepwch
,
6621 LPABCFLOAT SafeBuffF
= NULL
;
6627 FT_CharMap charmap
, found
= NULL
;
6628 UINT i
, glyph_index
, BufferSize
;
6630 NTSTATUS Status
= STATUS_SUCCESS
;
6631 PMATRIX pmxWorldToDevice
;
6632 PWCHAR Safepwch
= NULL
;
6637 EngSetLastError(ERROR_INVALID_PARAMETER
);
6643 UINT pwchSize
= Count
* sizeof(WCHAR
);
6644 Safepwch
= ExAllocatePoolWithTag(PagedPool
, pwchSize
, GDITAG_TEXT
);
6648 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6654 ProbeForRead(UnSafepwch
, pwchSize
, 1);
6655 RtlCopyMemory(Safepwch
, UnSafepwch
, pwchSize
);
6657 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6659 Status
= _SEH2_GetExceptionCode();
6664 if (!NT_SUCCESS(Status
))
6667 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6669 EngSetLastError(Status
);
6673 BufferSize
= Count
* sizeof(ABC
); // Same size!
6674 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6675 if (!fl
) SafeBuffF
= (LPABCFLOAT
) SafeBuff
;
6676 if (SafeBuff
== NULL
)
6680 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6682 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6686 dc
= DC_LockDc(hDC
);
6689 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6692 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6694 EngSetLastError(ERROR_INVALID_HANDLE
);
6697 pdcattr
= dc
->pdcattr
;
6698 hFont
= pdcattr
->hlfntNew
;
6699 TextObj
= RealizeFontInit(hFont
);
6701 /* Get the DC's world-to-device transformation matrix */
6702 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6705 if (TextObj
== NULL
)
6707 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6710 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6712 EngSetLastError(ERROR_INVALID_HANDLE
);
6716 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6718 face
= FontGDI
->SharedFace
->Face
;
6719 if (face
->charmap
== NULL
)
6721 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6723 charmap
= face
->charmaps
[i
];
6724 if (charmap
->encoding
!= 0)
6733 DPRINT1("WARNING: Could not find desired charmap!\n");
6734 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6737 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6739 EngSetLastError(ERROR_INVALID_HANDLE
);
6744 FT_Set_Charmap(face
, found
);
6745 IntUnLockFreeType();
6748 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6750 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6751 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6753 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6755 int adv
, lsb
, bbx
, left
, right
;
6759 glyph_index
= get_glyph_index_flagged(face
, Safepwch
[i
- FirstChar
], GCABCW_INDICES
, fl
);
6763 glyph_index
= get_glyph_index_flagged(face
, i
, GCABCW_INDICES
, fl
);
6765 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6767 left
= (INT
)face
->glyph
->metrics
.horiBearingX
& -64;
6768 right
= (INT
)((face
->glyph
->metrics
.horiBearingX
+ face
->glyph
->metrics
.width
) + 63) & -64;
6769 adv
= (face
->glyph
->advance
.x
+ 32) >> 6;
6771 // int test = (INT)(face->glyph->metrics.horiAdvance + 63) >> 6;
6772 // DPRINT1("Advance Wine %d and Advance Ros %d\n",test, adv ); /* It's the same! */
6775 bbx
= (right
- left
) >> 6;
6777 DPRINT1("lsb %d and bbx %d\n", lsb, bbx );
6781 SafeBuffF
[i
- FirstChar
].abcfA
= (FLOAT
) lsb
;
6782 SafeBuffF
[i
- FirstChar
].abcfB
= (FLOAT
) bbx
;
6783 SafeBuffF
[i
- FirstChar
].abcfC
= (FLOAT
) (adv
- lsb
- bbx
);
6787 SafeBuff
[i
- FirstChar
].abcA
= lsb
;
6788 SafeBuff
[i
- FirstChar
].abcB
= bbx
;
6789 SafeBuff
[i
- FirstChar
].abcC
= adv
- lsb
- bbx
;
6792 IntUnLockFreeType();
6793 TEXTOBJ_UnlockText(TextObj
);
6794 Status
= MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6796 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6799 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6801 if (!NT_SUCCESS(Status
))
6803 SetLastNtError(Status
);
6807 DPRINT("NtGdiGetCharABCWidths Worked!\n");
6820 IN OPTIONAL PWCHAR UnSafepwc
,
6824 NTSTATUS Status
= STATUS_SUCCESS
;
6826 PFLOAT SafeBuffF
= NULL
;
6832 FT_CharMap charmap
, found
= NULL
;
6833 UINT i
, glyph_index
, BufferSize
;
6835 PMATRIX pmxWorldToDevice
;
6836 PWCHAR Safepwc
= NULL
;
6841 UINT pwcSize
= Count
* sizeof(WCHAR
);
6842 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6846 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6851 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6852 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6854 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6856 Status
= _SEH2_GetExceptionCode();
6861 if (!NT_SUCCESS(Status
))
6863 EngSetLastError(Status
);
6867 BufferSize
= Count
* sizeof(INT
); // Same size!
6868 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6869 if (!fl
) SafeBuffF
= (PFLOAT
) SafeBuff
;
6870 if (SafeBuff
== NULL
)
6873 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6875 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6879 dc
= DC_LockDc(hDC
);
6883 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6885 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6886 EngSetLastError(ERROR_INVALID_HANDLE
);
6889 pdcattr
= dc
->pdcattr
;
6890 hFont
= pdcattr
->hlfntNew
;
6891 TextObj
= RealizeFontInit(hFont
);
6892 /* Get the DC's world-to-device transformation matrix */
6893 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6896 if (TextObj
== NULL
)
6899 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6901 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6902 EngSetLastError(ERROR_INVALID_HANDLE
);
6906 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6908 face
= FontGDI
->SharedFace
->Face
;
6909 if (face
->charmap
== NULL
)
6911 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6913 charmap
= face
->charmaps
[i
];
6914 if (charmap
->encoding
!= 0)
6923 DPRINT1("WARNING: Could not find desired charmap!\n");
6926 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6928 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6929 EngSetLastError(ERROR_INVALID_HANDLE
);
6934 FT_Set_Charmap(face
, found
);
6935 IntUnLockFreeType();
6938 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6940 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6941 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6943 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6947 glyph_index
= get_glyph_index_flagged(face
, Safepwc
[i
- FirstChar
], GCW_INDICES
, fl
);
6951 glyph_index
= get_glyph_index_flagged(face
, i
, GCW_INDICES
, fl
);
6953 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6955 SafeBuffF
[i
- FirstChar
] = (FLOAT
) ((face
->glyph
->advance
.x
+ 32) >> 6);
6957 SafeBuff
[i
- FirstChar
] = (face
->glyph
->advance
.x
+ 32) >> 6;
6959 IntUnLockFreeType();
6960 TEXTOBJ_UnlockText(TextObj
);
6961 MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6964 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6966 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6974 // TODO: Move this code into NtGdiGetGlyphIndicesWInternal and wrap
6975 // NtGdiGetGlyphIndicesW around NtGdiGetGlyphIndicesWInternal instead.
6976 // NOTE: See also GreGetGlyphIndicesW.
6981 NtGdiGetGlyphIndicesW(
6983 _In_reads_opt_(cwc
) LPCWSTR pwc
,
6985 _Out_writes_opt_(cwc
) LPWORD pgi
,
6993 NTSTATUS Status
= STATUS_SUCCESS
;
6994 OUTLINETEXTMETRICW
*potm
;
6996 WCHAR DefChar
= 0xffff;
6997 PWSTR Buffer
= NULL
;
6998 ULONG Size
, pwcSize
;
6999 PWSTR Safepwc
= NULL
;
7000 LPCWSTR UnSafepwc
= pwc
;
7001 LPWORD UnSafepgi
= pgi
;
7007 DPRINT1("cwc < 0\n");
7011 if (!UnSafepwc
&& !UnSafepgi
&& cwc
> 0)
7013 DPRINT1("!UnSafepwc && !UnSafepgi && cwc > 0\n");
7017 if (!UnSafepwc
!= !UnSafepgi
)
7019 DPRINT1("UnSafepwc == %p, UnSafepgi = %p\n", UnSafepwc
, UnSafepgi
);
7024 dc
= DC_LockDc(hdc
);
7027 DPRINT1("!DC_LockDC\n");
7030 pdcattr
= dc
->pdcattr
;
7031 hFont
= pdcattr
->hlfntNew
;
7032 TextObj
= RealizeFontInit(hFont
);
7036 DPRINT1("!TextObj\n");
7039 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
7040 TEXTOBJ_UnlockText(TextObj
);
7044 if (!UnSafepwc
&& !UnSafepgi
)
7046 Face
= FontGDI
->SharedFace
->Face
;
7047 return Face
->num_glyphs
;
7051 Status
= STATUS_UNSUCCESSFUL
;
7056 Buffer
= ExAllocatePoolWithTag(PagedPool
, cwc
* sizeof(WORD
), GDITAG_TEXT
);
7059 DPRINT1("ExAllocatePoolWithTag\n");
7064 if (iMode
& GGI_MARK_NONEXISTING_GLYPHS
)
7070 Face
= FontGDI
->SharedFace
->Face
;
7071 if (FT_IS_SFNT(Face
))
7074 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
7075 DefChar
= (pOS2
->usDefaultChar
? get_glyph_index(Face
, pOS2
->usDefaultChar
) : 0);
7076 IntUnLockFreeType();
7080 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
7083 Status
= STATUS_UNSUCCESSFUL
;
7087 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
7090 Status
= STATUS_INSUFFICIENT_RESOURCES
;
7094 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
7096 DefChar
= potm
->otmTextMetrics
.tmDefaultChar
;
7097 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
7101 /* Allocate for Safepwc */
7102 pwcSize
= cwc
* sizeof(WCHAR
);
7103 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
7106 Status
= STATUS_NO_MEMORY
;
7107 DPRINT1("!Safepwc\n");
7113 ProbeForRead(UnSafepwc
, pwcSize
, 1);
7114 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
7116 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
7118 Status
= _SEH2_GetExceptionCode();
7122 if (!NT_SUCCESS(Status
))
7124 DPRINT1("Status: %08lX\n", Status
);
7128 /* Get glyph indeces */
7130 for (i
= 0; i
< cwc
; i
++)
7132 Buffer
[i
] = get_glyph_index(FontGDI
->SharedFace
->Face
, Safepwc
[i
]);
7135 Buffer
[i
] = DefChar
;
7138 IntUnLockFreeType();
7142 ProbeForWrite(UnSafepgi
, cwc
* sizeof(WORD
), 1);
7143 RtlCopyMemory(UnSafepgi
, Buffer
, cwc
* sizeof(WORD
));
7145 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
7147 Status
= _SEH2_GetExceptionCode();
7154 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
7156 if (Safepwc
!= NULL
)
7158 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
7161 if (NT_SUCCESS(Status
))