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-2018 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
;
47 /* HACK!! Fix XFORMOBJ then use 1:16 / 16:1 */
48 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
50 FT_Library g_FreeTypeLibrary
;
52 /* special font names */
53 static const UNICODE_STRING g_MarlettW
= RTL_CONSTANT_STRING(L
"Marlett");
56 static UNICODE_STRING g_FontRegPath
=
57 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
60 /* The FreeType library is not thread safe, so we have
61 to serialize access to it */
62 static PFAST_MUTEX g_FreeTypeLock
;
64 static LIST_ENTRY g_FontListHead
;
65 static PFAST_MUTEX g_FontListLock
;
66 static BOOL g_RenderingEnabled
= TRUE
;
68 #define IntLockGlobalFonts() \
69 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(g_FontListLock)
71 #define IntUnLockGlobalFonts() \
72 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(g_FontListLock)
74 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
75 ASSERT(g_FontListLock->Owner == KeGetCurrentThread())
77 #define IntLockFreeType() \
78 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(g_FreeTypeLock)
80 #define IntUnLockFreeType() \
81 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(g_FreeTypeLock)
83 #define ASSERT_FREETYPE_LOCK_HELD() \
84 ASSERT(g_FreeTypeLock->Owner == KeGetCurrentThread())
86 #define ASSERT_FREETYPE_LOCK_NOT_HELD() \
87 ASSERT(g_FreeTypeLock->Owner != KeGetCurrentThread())
89 #define MAX_FONT_CACHE 256
91 static LIST_ENTRY g_FontCacheListHead
;
92 static UINT g_FontCacheNumEntries
;
94 static PWCHAR g_ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
104 L
"Vietnamese", /* 08 */
105 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
113 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
118 * For TranslateCharsetInfo
121 #define MAXTCIINDEX 32
122 static const CHARSETINFO g_FontTci
[MAXTCIINDEX
] =
125 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
126 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
127 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
128 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
129 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
130 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
131 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
132 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
133 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
134 /* reserved by ANSI */
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}} },
140 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
141 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
143 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
144 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
145 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
146 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
147 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
148 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
149 /* Reserved for alternate ANSI and OEM */
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 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
157 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
158 /* Reserved for system */
159 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
160 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
168 /* Get charset from specified codepage.
169 g_FontTci is used also in TranslateCharsetInfo. */
170 BYTE FASTCALL
IntCharSetFromCodePage(UINT uCodePage
)
174 if (uCodePage
== CP_OEMCP
)
177 if (uCodePage
== CP_MACCP
)
180 for (i
= 0; i
< MAXTCIINDEX
; ++i
)
182 if (g_FontTci
[i
].ciACP
== 0)
185 if (g_FontTci
[i
].ciACP
== uCodePage
)
186 return g_FontTci
[i
].ciCharset
;
189 return DEFAULT_CHARSET
;
193 static RTL_STATIC_LIST_HEAD(g_FontSubstListHead
);
196 SharedMem_AddRef(PSHARED_MEM Ptr
)
198 ASSERT_FREETYPE_LOCK_HELD();
204 SharedFaceCache_Init(PSHARED_FACE_CACHE Cache
)
206 Cache
->OutlineRequiredSize
= 0;
207 RtlInitUnicodeString(&Cache
->FontFamily
, NULL
);
208 RtlInitUnicodeString(&Cache
->FullName
, NULL
);
212 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
215 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
220 Ptr
->Memory
= Memory
;
221 SharedFaceCache_Init(&Ptr
->EnglishUS
);
222 SharedFaceCache_Init(&Ptr
->UserLanguage
);
224 SharedMem_AddRef(Memory
);
225 DPRINT("Creating SharedFace for %s\n", Face
->family_name
? Face
->family_name
: "<NULL>");
231 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
234 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
237 Ptr
->Buffer
= Buffer
;
238 Ptr
->BufferSize
= BufferSize
;
240 Ptr
->IsMapping
= IsMapping
;
241 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
247 SharedFace_AddRef(PSHARED_FACE Ptr
)
249 ASSERT_FREETYPE_LOCK_HELD();
255 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
257 ASSERT_FREETYPE_LOCK_HELD();
259 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
260 RemoveEntryList(&Entry
->ListEntry
);
261 ExFreePoolWithTag(Entry
, TAG_FONT
);
262 g_FontCacheNumEntries
--;
263 ASSERT(g_FontCacheNumEntries
<= MAX_FONT_CACHE
);
267 RemoveCacheEntries(FT_Face Face
)
269 PLIST_ENTRY CurrentEntry
;
270 PFONT_CACHE_ENTRY FontEntry
;
272 ASSERT_FREETYPE_LOCK_HELD();
274 CurrentEntry
= g_FontCacheListHead
.Flink
;
275 while (CurrentEntry
!= &g_FontCacheListHead
)
277 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
278 CurrentEntry
= CurrentEntry
->Flink
;
280 if (FontEntry
->Face
== Face
)
282 RemoveCachedEntry(FontEntry
);
287 static void SharedMem_Release(PSHARED_MEM Ptr
)
289 ASSERT_FREETYPE_LOCK_HELD();
290 ASSERT(Ptr
->RefCount
> 0);
292 if (Ptr
->RefCount
<= 0)
296 if (Ptr
->RefCount
== 0)
298 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
300 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
302 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
303 ExFreePoolWithTag(Ptr
, TAG_FONT
);
308 SharedFaceCache_Release(PSHARED_FACE_CACHE Cache
)
310 RtlFreeUnicodeString(&Cache
->FontFamily
);
311 RtlFreeUnicodeString(&Cache
->FullName
);
315 SharedFace_Release(PSHARED_FACE Ptr
)
318 ASSERT(Ptr
->RefCount
> 0);
320 if (Ptr
->RefCount
<= 0)
324 if (Ptr
->RefCount
== 0)
326 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
? Ptr
->Face
->family_name
: "<NULL>");
327 RemoveCacheEntries(Ptr
->Face
);
328 FT_Done_Face(Ptr
->Face
);
329 SharedMem_Release(Ptr
->Memory
);
330 SharedFaceCache_Release(&Ptr
->EnglishUS
);
331 SharedFaceCache_Release(&Ptr
->UserLanguage
);
332 ExFreePoolWithTag(Ptr
, TAG_FONT
);
339 * IntLoadFontSubstList --- loads the list of font substitutes
342 IntLoadFontSubstList(PLIST_ENTRY pHead
)
346 OBJECT_ATTRIBUTES ObjectAttributes
;
347 KEY_FULL_INFORMATION KeyFullInfo
;
349 UNICODE_STRING FromW
, ToW
;
350 BYTE InfoBuffer
[128];
351 PKEY_VALUE_FULL_INFORMATION pInfo
;
352 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
354 PFONTSUBST_ENTRY pEntry
;
357 /* the FontSubstitutes registry key */
358 static UNICODE_STRING FontSubstKey
=
359 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
360 L
"Microsoft\\Windows NT\\CurrentVersion\\"
363 /* open registry key */
364 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
365 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
367 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
368 if (!NT_SUCCESS(Status
))
370 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
371 return FALSE
; /* failure */
374 /* query count of values */
375 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
376 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
377 if (!NT_SUCCESS(Status
))
379 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
381 return FALSE
; /* failure */
385 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
388 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
389 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
390 if (!NT_SUCCESS(Status
))
392 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
396 /* create FromW string */
397 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
398 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
399 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
400 Success
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
403 Status
= STATUS_INSUFFICIENT_RESOURCES
;
404 DPRINT("RtlCreateUnicodeString failed\n");
409 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
410 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
411 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
412 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
414 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
415 RtlFreeUnicodeString(&FromW
);
419 /* create ToW string */
420 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
421 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
422 pch
[Length
] = UNICODE_NULL
; /* truncate */
423 Success
= RtlCreateUnicodeString(&ToW
, pch
);
426 Status
= STATUS_INSUFFICIENT_RESOURCES
;
427 DPRINT("RtlCreateUnicodeString failed\n");
428 RtlFreeUnicodeString(&FromW
);
432 /* does charset exist? (from) */
433 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
434 pch
= wcsrchr(FromW
.Buffer
, L
',');
439 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
440 /* parse charset number */
441 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
444 /* does charset exist? (to) */
445 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
446 pch
= wcsrchr(ToW
.Buffer
, L
',');
451 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
452 /* parse charset number */
453 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
456 /* allocate an entry */
457 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
460 DPRINT("ExAllocatePoolWithTag failed\n");
461 RtlFreeUnicodeString(&FromW
);
462 RtlFreeUnicodeString(&ToW
);
466 /* store to *pEntry */
467 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
468 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
469 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
470 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
472 /* insert pEntry to *pHead */
473 InsertTailList(pHead
, &pEntry
->ListEntry
);
479 return NT_SUCCESS(Status
);
483 InitFontSupport(VOID
)
487 InitializeListHead(&g_FontListHead
);
488 InitializeListHead(&g_FontCacheListHead
);
489 g_FontCacheNumEntries
= 0;
490 /* Fast Mutexes must be allocated from non paged pool */
491 g_FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
492 if (g_FontListLock
== NULL
)
497 ExInitializeFastMutex(g_FontListLock
);
498 g_FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
499 if (g_FreeTypeLock
== NULL
)
503 ExInitializeFastMutex(g_FreeTypeLock
);
505 ulError
= FT_Init_FreeType(&g_FreeTypeLibrary
);
508 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
512 IntLoadSystemFonts();
513 IntLoadFontSubstList(&g_FontSubstListHead
);
519 FtSetCoordinateTransform(
526 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
528 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
529 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
532 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
533 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
536 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
537 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
540 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
541 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
543 /* Set the transformation matrix */
544 FT_Set_Transform(face
, &ftmatrix
, 0);
548 SubstituteFontByList(PLIST_ENTRY pHead
,
549 PUNICODE_STRING pOutputName
,
550 PUNICODE_STRING pInputName
,
551 BYTE RequestedCharSet
,
552 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
554 PLIST_ENTRY pListEntry
;
555 PFONTSUBST_ENTRY pSubstEntry
;
556 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
558 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
559 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
561 /* for each list entry */
562 for (pListEntry
= pHead
->Flink
;
564 pListEntry
= pListEntry
->Flink
)
567 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
569 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
571 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
572 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
574 continue; /* not matched */
577 /* does charset number exist? (to) */
578 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
580 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
584 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
587 /* does font name match? */
588 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
591 continue; /* not matched */
594 /* update *pOutputName */
595 *pOutputName
= pSubstEntry
->FontNames
[FONTSUBST_TO
];
597 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
599 /* update CharSetMap */
600 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
601 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
603 return TRUE
; /* success */
610 SubstituteFontRecurse(LOGFONTW
* pLogFont
)
612 UINT RecurseCount
= 5;
613 UNICODE_STRING OutputNameW
= { 0 };
614 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
616 UNICODE_STRING InputNameW
;
618 if (pLogFont
->lfFaceName
[0] == UNICODE_NULL
)
621 RtlInitUnicodeString(&InputNameW
, pLogFont
->lfFaceName
);
623 while (RecurseCount
-- > 0)
625 Found
= SubstituteFontByList(&g_FontSubstListHead
,
626 &OutputNameW
, &InputNameW
,
627 pLogFont
->lfCharSet
, CharSetMap
);
631 RtlStringCchCopyW(pLogFont
->lfFaceName
, LF_FACESIZE
, OutputNameW
.Buffer
);
633 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
634 CharSetMap
[FONTSUBST_FROM
] == pLogFont
->lfCharSet
)
636 pLogFont
->lfCharSet
= CharSetMap
[FONTSUBST_TO
];
640 return TRUE
; /* success */
646 * Search the system font directory and adds each font found.
649 IntLoadSystemFonts(VOID
)
651 OBJECT_ATTRIBUTES ObjectAttributes
;
652 UNICODE_STRING Directory
, FileName
, TempString
;
653 IO_STATUS_BLOCK Iosb
;
656 PFILE_DIRECTORY_INFORMATION DirInfo
;
657 BOOLEAN bRestartScan
= TRUE
;
660 static UNICODE_STRING SearchPatterns
[] =
662 RTL_CONSTANT_STRING(L
"*.ttf"),
663 RTL_CONSTANT_STRING(L
"*.ttc"),
664 RTL_CONSTANT_STRING(L
"*.otf"),
665 RTL_CONSTANT_STRING(L
"*.otc"),
666 RTL_CONSTANT_STRING(L
"*.fon"),
667 RTL_CONSTANT_STRING(L
"*.fnt")
670 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
672 InitializeObjectAttributes(
675 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
681 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
684 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
685 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
687 if (NT_SUCCESS(Status
))
689 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
691 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
692 if (DirInfoBuffer
== NULL
)
698 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
699 if (FileName
.Buffer
== NULL
)
701 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
706 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
710 Status
= ZwQueryDirectoryFile(
718 FileDirectoryInformation
,
723 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
728 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
731 TempString
.Buffer
= DirInfo
->FileName
;
733 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
734 RtlCopyUnicodeString(&FileName
, &Directory
);
735 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
736 IntGdiAddFontResource(&FileName
, 0);
737 if (DirInfo
->NextEntryOffset
== 0)
739 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
742 bRestartScan
= FALSE
;
745 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
746 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
753 ItalicFromStyle(const char *style_name
)
755 if (style_name
== NULL
|| style_name
[0] == 0)
757 if (strstr(style_name
, "Italic") != NULL
)
759 if (strstr(style_name
, "Oblique") != NULL
)
765 WeightFromStyle(const char *style_name
)
767 if (style_name
== NULL
|| style_name
[0] == 0)
769 if (strstr(style_name
, "Regular") != NULL
)
771 if (strstr(style_name
, "Normal") != NULL
)
773 if (strstr(style_name
, "SemiBold") != NULL
)
775 if (strstr(style_name
, "UltraBold") != NULL
)
777 if (strstr(style_name
, "DemiBold") != NULL
)
779 if (strstr(style_name
, "ExtraBold") != NULL
)
781 if (strstr(style_name
, "Bold") != NULL
)
783 if (strstr(style_name
, "UltraLight") != NULL
)
784 return FW_ULTRALIGHT
;
785 if (strstr(style_name
, "ExtraLight") != NULL
)
786 return FW_EXTRALIGHT
;
787 if (strstr(style_name
, "Light") != NULL
)
789 if (strstr(style_name
, "Hairline") != NULL
)
791 if (strstr(style_name
, "Book") != NULL
)
793 if (strstr(style_name
, "ExtraBlack") != NULL
)
795 if (strstr(style_name
, "UltraBlack") != NULL
)
797 if (strstr(style_name
, "Black") != NULL
)
799 if (strstr(style_name
, "Medium") != NULL
)
801 if (strstr(style_name
, "Thin") != NULL
)
803 if (strstr(style_name
, "Heavy") != NULL
)
809 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
);
812 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
813 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
817 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
821 ANSI_STRING AnsiFaceName
;
822 FT_WinFNT_HeaderRec WinFNT
;
823 INT FaceCount
= 0, CharSetCount
= 0;
824 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
825 DWORD Characteristics
= pLoadFont
->Characteristics
;
826 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
829 FT_UShort os2_version
;
830 FT_ULong os2_ulCodePageRange1
;
831 FT_UShort os2_usWeightClass
;
833 if (SharedFace
== NULL
&& CharSetIndex
== -1)
835 /* load a face from memory */
837 Error
= FT_New_Memory_Face(
839 pLoadFont
->Memory
->Buffer
,
840 pLoadFont
->Memory
->BufferSize
,
841 ((FontIndex
!= -1) ? FontIndex
: 0),
845 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
849 if (!Error
&& FT_IS_SFNT(Face
))
850 pLoadFont
->IsTrueType
= TRUE
;
852 if (Error
|| SharedFace
== NULL
)
855 SharedFace_Release(SharedFace
);
857 if (Error
== FT_Err_Unknown_File_Format
)
858 DPRINT1("Unknown font file format\n");
860 DPRINT1("Error reading font (error code: %d)\n", Error
);
861 return 0; /* failure */
866 Face
= SharedFace
->Face
;
868 SharedFace_AddRef(SharedFace
);
872 /* allocate a FONT_ENTRY */
873 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
876 SharedFace_Release(SharedFace
);
877 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
878 return 0; /* failure */
881 /* allocate a FONTGDI */
882 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
885 SharedFace_Release(SharedFace
);
886 ExFreePoolWithTag(Entry
, TAG_FONT
);
887 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
888 return 0; /* failure */
894 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
895 pFileName
->Length
+ sizeof(UNICODE_NULL
),
897 if (FontGDI
->Filename
== NULL
)
900 SharedFace_Release(SharedFace
);
901 ExFreePoolWithTag(Entry
, TAG_FONT
);
902 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
903 return 0; /* failure */
905 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
906 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
910 FontGDI
->Filename
= NULL
;
912 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
915 if (FontGDI
->Filename
)
916 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
918 SharedFace_Release(SharedFace
);
919 ExFreePoolWithTag(Entry
, TAG_FONT
);
923 PrivateEntry
->Entry
= Entry
;
924 if (pLoadFont
->PrivateEntry
)
926 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
930 InitializeListHead(&PrivateEntry
->ListEntry
);
931 pLoadFont
->PrivateEntry
= PrivateEntry
;
936 FontGDI
->SharedFace
= SharedFace
;
937 FontGDI
->CharSet
= ANSI_CHARSET
;
938 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
939 FontGDI
->RequestItalic
= FALSE
;
940 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
941 FontGDI
->RequestWeight
= FW_NORMAL
;
943 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
944 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
945 if (!NT_SUCCESS(Status
))
949 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
951 pLoadFont
->PrivateEntry
= NULL
;
955 RemoveEntryList(&PrivateEntry
->ListEntry
);
957 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
959 if (FontGDI
->Filename
)
960 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
962 SharedFace_Release(SharedFace
);
963 ExFreePoolWithTag(Entry
, TAG_FONT
);
969 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
972 os2_version
= pOS2
->version
;
973 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
974 os2_usWeightClass
= pOS2
->usWeightClass
;
978 if (pOS2
&& os2_version
>= 1)
980 /* get charset and weight from OS/2 header */
982 /* Make sure we do not use this pointer anymore */
985 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
987 if (os2_ulCodePageRange1
& (1 << BitIndex
))
989 if (g_FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
992 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
993 CharSetIndex
== CharSetCount
)
995 FontGDI
->CharSet
= g_FontTci
[BitIndex
].ciCharset
;
1002 /* set actual weight */
1003 FontGDI
->OriginalWeight
= os2_usWeightClass
;
1007 /* get charset from WinFNT header */
1009 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
1012 FontGDI
->CharSet
= WinFNT
.charset
;
1014 IntUnLockFreeType();
1017 /* FIXME: CharSet is invalid on Marlett */
1018 if (RtlEqualUnicodeString(&Entry
->FaceName
, &g_MarlettW
, TRUE
))
1020 FontGDI
->CharSet
= SYMBOL_CHARSET
;
1024 DPRINT("Font loaded: %s (%s)\n",
1025 Face
->family_name
? Face
->family_name
: "<NULL>",
1026 Face
->style_name
? Face
->style_name
: "<NULL>");
1027 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
1028 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
1031 IntRequestFontSize(NULL
, FontGDI
, 0, 0);
1032 IntUnLockFreeType();
1034 /* Add this font resource to the font table */
1035 Entry
->Font
= FontGDI
;
1036 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1038 if (Characteristics
& FR_PRIVATE
)
1041 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1042 IntLockProcessPrivateFonts(Win32Process
);
1043 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1044 IntUnLockProcessPrivateFonts(Win32Process
);
1049 IntLockGlobalFonts();
1050 InsertTailList(&g_FontListHead
, &Entry
->ListEntry
);
1051 IntUnLockGlobalFonts();
1054 if (FontIndex
== -1)
1056 if (FT_IS_SFNT(Face
))
1058 TT_Face TrueType
= (TT_Face
)Face
;
1059 if (TrueType
->ttc_header
.count
> 1)
1062 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1064 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1071 if (CharSetIndex
== -1)
1075 if (pLoadFont
->RegValueName
.Length
== 0)
1077 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1081 UNICODE_STRING NewString
;
1082 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1083 NewString
.Length
= 0;
1084 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1085 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1086 NewString
.MaximumLength
,
1088 NewString
.Buffer
[0] = UNICODE_NULL
;
1090 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1091 RtlAppendUnicodeToString(&NewString
, L
" & ");
1092 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1094 RtlFreeUnicodeString(pValueName
);
1095 *pValueName
= NewString
;
1098 for (i
= 1; i
< CharSetCount
; ++i
)
1100 /* Do not count charsets towards 'faces' loaded */
1101 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1105 return FaceCount
; /* number of loaded faces */
1109 * IntGdiAddFontResource
1111 * Adds the font resource from the specified file to the system.
1115 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1119 PVOID Buffer
= NULL
;
1120 IO_STATUS_BLOCK Iosb
;
1121 PVOID SectionObject
;
1122 SIZE_T ViewSize
= 0;
1123 LARGE_INTEGER SectionSize
;
1124 OBJECT_ATTRIBUTES ObjectAttributes
;
1125 GDI_LOAD_FONT LoadFont
;
1128 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1130 /* Open the font file */
1131 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1132 Status
= ZwOpenFile(
1134 FILE_GENERIC_READ
| SYNCHRONIZE
,
1138 FILE_SYNCHRONOUS_IO_NONALERT
);
1139 if (!NT_SUCCESS(Status
))
1141 DPRINT("Could not load font file: %wZ\n", FileName
);
1145 SectionSize
.QuadPart
= 0LL;
1146 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1147 NULL
, &SectionSize
, PAGE_READONLY
,
1148 SEC_COMMIT
, FileHandle
, NULL
);
1149 if (!NT_SUCCESS(Status
))
1151 DPRINT("Could not map file: %wZ\n", FileName
);
1152 ZwClose(FileHandle
);
1155 ZwClose(FileHandle
);
1157 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1158 if (!NT_SUCCESS(Status
))
1160 DPRINT("Could not map file: %wZ\n", FileName
);
1161 ObDereferenceObject(SectionObject
);
1165 LoadFont
.pFileName
= FileName
;
1166 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1167 LoadFont
.Characteristics
= Characteristics
;
1168 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1169 LoadFont
.IsTrueType
= FALSE
;
1170 LoadFont
.PrivateEntry
= NULL
;
1171 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1173 ObDereferenceObject(SectionObject
);
1175 /* Release our copy */
1177 SharedMem_Release(LoadFont
.Memory
);
1178 IntUnLockFreeType();
1182 if (LoadFont
.IsTrueType
)
1184 /* append " (TrueType)" */
1185 UNICODE_STRING NewString
;
1188 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1189 NewString
.Length
= 0;
1190 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1191 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1192 NewString
.MaximumLength
,
1194 NewString
.Buffer
[0] = UNICODE_NULL
;
1196 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1197 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1198 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1199 LoadFont
.RegValueName
= NewString
;
1203 InitializeObjectAttributes(&ObjectAttributes
, &g_FontRegPath
,
1204 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1206 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1207 if (NT_SUCCESS(Status
))
1210 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1214 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1215 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1216 pFileName
, DataSize
);
1221 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1227 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1229 GDI_LOAD_FONT LoadFont
;
1230 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1234 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1241 memcpy(BufferCopy
, Buffer
, dwSize
);
1243 LoadFont
.pFileName
= NULL
;
1244 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1245 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1246 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1247 LoadFont
.IsTrueType
= FALSE
;
1248 LoadFont
.PrivateEntry
= NULL
;
1249 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1251 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1253 /* Release our copy */
1255 SharedMem_Release(LoadFont
.Memory
);
1256 IntUnLockFreeType();
1260 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1261 if (EntryCollection
)
1263 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1264 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1265 IntLockProcessPrivateFonts(Win32Process
);
1266 EntryCollection
->Handle
= ULongToHandle(++Win32Process
->PrivateMemFontHandleCount
);
1267 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1268 IntUnLockProcessPrivateFonts(Win32Process
);
1269 Ret
= EntryCollection
->Handle
;
1272 *pNumAdded
= FaceCount
;
1277 // FIXME: Add RemoveFontResource
1279 static VOID FASTCALL
1280 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1282 PFONTGDI FontGDI
= FontEntry
->Font
;
1283 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1285 if (FontGDI
->Filename
)
1286 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1288 EngFreeMem(FontGDI
);
1289 SharedFace_Release(SharedFace
);
1290 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1294 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1297 PFONT_ENTRY_MEM FontEntry
;
1299 while (!IsListEmpty(&Head
->ListEntry
))
1301 Entry
= RemoveHeadList(&Head
->ListEntry
);
1302 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1304 CleanupFontEntry(FontEntry
->Entry
);
1305 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1308 CleanupFontEntry(Head
->Entry
);
1309 ExFreePoolWithTag(Head
, TAG_FONT
);
1312 static VOID FASTCALL
1313 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1315 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1316 PLIST_ENTRY ListEntry
;
1317 RemoveEntryList(&Collection
->ListEntry
);
1320 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1321 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1323 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1324 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1326 } while (FontMemEntry
!= Collection
->Entry
);
1330 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1333 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1334 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1335 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1337 IntLockProcessPrivateFonts(Win32Process
);
1338 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1339 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1341 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1343 if (CurrentEntry
->Handle
== hMMFont
)
1345 EntryCollection
= CurrentEntry
;
1346 UnlinkFontMemCollection(CurrentEntry
);
1350 Entry
= Entry
->Flink
;
1352 IntUnLockProcessPrivateFonts(Win32Process
);
1354 if (EntryCollection
)
1356 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1357 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1365 IntGdiCleanupPrivateFontsForProcess(VOID
)
1367 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1369 PFONT_ENTRY_COLL_MEM EntryCollection
;
1371 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1374 EntryCollection
= NULL
;
1376 IntLockProcessPrivateFonts(Win32Process
);
1377 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1379 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1380 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1381 UnlinkFontMemCollection(EntryCollection
);
1383 IntUnLockProcessPrivateFonts(Win32Process
);
1385 if (EntryCollection
)
1387 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1388 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1392 /* No Mem fonts anymore, see if we have any other private fonts left */
1394 IntLockProcessPrivateFonts(Win32Process
);
1395 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1397 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1399 IntUnLockProcessPrivateFonts(Win32Process
);
1403 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1411 IntIsFontRenderingEnabled(VOID
)
1413 BOOL Ret
= g_RenderingEnabled
;
1416 hDC
= IntGetScreenDC();
1418 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && g_RenderingEnabled
;
1424 IntEnableFontRendering(BOOL Enable
)
1426 g_RenderingEnabled
= Enable
;
1429 FT_Render_Mode FASTCALL
1430 IntGetFontRenderMode(LOGFONTW
*logfont
)
1432 switch (logfont
->lfQuality
)
1434 case ANTIALIASED_QUALITY
:
1436 case NONANTIALIASED_QUALITY
:
1437 return FT_RENDER_MODE_MONO
;
1439 return FT_RENDER_MODE_LIGHT
;
1440 /* case CLEARTYPE_QUALITY:
1441 return FT_RENDER_MODE_LCD; */
1443 return FT_RENDER_MODE_NORMAL
;
1448 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1453 plfont
= LFONT_AllocFontWithHandle();
1456 return STATUS_NO_MEMORY
;
1459 ExInitializePushLock(&plfont
->lock
);
1460 *NewFont
= plfont
->BaseObject
.hHmgr
;
1461 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1462 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1463 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1465 /* This should really depend on whether GM_ADVANCED is set */
1466 plf
->lfOrientation
= plf
->lfEscapement
;
1468 LFONT_UnlockFont(plfont
);
1470 return STATUS_SUCCESS
;
1473 /*************************************************************************
1474 * TranslateCharsetInfo
1476 * Fills a CHARSETINFO structure for a character set, code page, or
1477 * font. This allows making the correspondance between different labelings
1478 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1479 * of the same encoding.
1481 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1482 * only one codepage should be set in *Src.
1485 * TRUE on success, FALSE on failure.
1488 static BOOLEAN APIENTRY
1489 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1490 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1491 if flags == TCI_SRCCHARSET: a character set value
1492 if flags == TCI_SRCCODEPAGE: a code page value */
1493 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1494 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1500 case TCI_SRCFONTSIG
:
1501 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1506 case TCI_SRCCODEPAGE
:
1507 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciACP
)
1512 case TCI_SRCCHARSET
:
1513 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciCharset
)
1522 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== g_FontTci
[Index
].ciCharset
)
1527 RtlCopyMemory(Cs
, &g_FontTci
[Index
], sizeof(CHARSETINFO
));
1533 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1537 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1539 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1545 static void FASTCALL
1546 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1547 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1548 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1550 FT_Fixed XScale
, YScale
;
1551 int Ascent
, Descent
;
1552 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1554 XScale
= Face
->size
->metrics
.x_scale
;
1555 YScale
= Face
->size
->metrics
.y_scale
;
1559 TM
->tmHeight
= pFNT
->pixel_height
;
1560 TM
->tmAscent
= pFNT
->ascent
;
1561 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1562 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1563 TM
->tmExternalLeading
= pFNT
->external_leading
;
1564 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1565 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1567 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1568 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1569 TM
->tmFirstChar
= pFNT
->first_char
;
1570 TM
->tmLastChar
= pFNT
->last_char
;
1571 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1572 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1573 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1576 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1577 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1578 TM
->tmUnderlined
= pFNT
->underline
;
1579 TM
->tmStruckOut
= pFNT
->strike_out
;
1580 TM
->tmCharSet
= pFNT
->charset
;
1584 TM
->tmWeight
= FontGDI
->RequestWeight
;
1585 TM
->tmItalic
= FontGDI
->RequestItalic
;
1586 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1587 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1588 TM
->tmCharSet
= FontGDI
->CharSet
;
1593 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1595 Ascent
= pHori
->Ascender
;
1596 Descent
= -pHori
->Descender
;
1600 Ascent
= pOS2
->usWinAscent
;
1601 Descent
= pOS2
->usWinDescent
;
1604 if (FontGDI
->Magic
!= FONTGDI_MAGIC
)
1606 IntRequestFontSize(NULL
, FontGDI
, 0, 0);
1608 TM
->tmAscent
= FontGDI
->tmAscent
;
1609 TM
->tmDescent
= FontGDI
->tmDescent
;
1610 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1611 TM
->tmInternalLeading
= FontGDI
->tmInternalLeading
;
1614 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1616 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1617 - ((Ascent
+ Descent
)
1618 - (pHori
->Ascender
- pHori
->Descender
)),
1619 YScale
) + 32) >> 6);
1621 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1622 if (TM
->tmAveCharWidth
== 0)
1624 TM
->tmAveCharWidth
= 1;
1627 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1628 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1632 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1636 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1637 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1639 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1643 TM
->tmWeight
= FontGDI
->RequestWeight
;
1648 TM
->tmDigitizedAspectX
= 96;
1649 TM
->tmDigitizedAspectY
= 96;
1650 if (face_has_symbol_charmap(Face
) ||
1651 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1653 USHORT cpOEM
, cpAnsi
;
1655 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1656 TM
->tmFirstChar
= 0;
1659 case 1257: /* Baltic */
1660 TM
->tmLastChar
= 0xf8fd;
1663 TM
->tmLastChar
= 0xf0ff;
1665 TM
->tmBreakChar
= 0x20;
1666 TM
->tmDefaultChar
= 0x1f;
1670 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1671 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1673 if(pOS2
->usFirstCharIndex
<= 1)
1674 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1675 else if (pOS2
->usFirstCharIndex
> 0xff)
1676 TM
->tmBreakChar
= 0x20;
1678 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1679 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1684 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1685 TM
->tmUnderlined
= FALSE
;
1686 TM
->tmStruckOut
= FALSE
;
1690 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1692 TM
->tmItalic
= 0xFF;
1698 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1699 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1702 if (!FT_IS_FIXED_WIDTH(Face
))
1704 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1706 case PAN_PROP_MONOSPACED
:
1707 TM
->tmPitchAndFamily
= 0;
1710 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1716 TM
->tmPitchAndFamily
= 0;
1719 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1721 case PAN_FAMILY_SCRIPT
:
1722 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1724 case PAN_FAMILY_DECORATIVE
:
1725 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1730 case PAN_FAMILY_TEXT_DISPLAY
:
1731 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1732 /* Which is clearly not what the panose spec says. */
1733 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1735 TM
->tmPitchAndFamily
= FF_MODERN
;
1739 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1744 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1747 case PAN_SERIF_COVE
:
1748 case PAN_SERIF_OBTUSE_COVE
:
1749 case PAN_SERIF_SQUARE_COVE
:
1750 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1751 case PAN_SERIF_SQUARE
:
1752 case PAN_SERIF_THIN
:
1753 case PAN_SERIF_BONE
:
1754 case PAN_SERIF_EXAGGERATED
:
1755 case PAN_SERIF_TRIANGLE
:
1756 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1759 case PAN_SERIF_NORMAL_SANS
:
1760 case PAN_SERIF_OBTUSE_SANS
:
1761 case PAN_SERIF_PERP_SANS
:
1762 case PAN_SERIF_FLARED
:
1763 case PAN_SERIF_ROUNDED
:
1764 TM
->tmPitchAndFamily
|= FF_SWISS
;
1770 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1773 if (FT_IS_SCALABLE(Face
))
1775 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1777 if (FT_IS_SFNT(Face
))
1779 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1782 TM
->tmCharSet
= FontGDI
->CharSet
;
1785 static void FASTCALL
1786 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1787 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1788 FT_WinFNT_HeaderRec
*pFNT
)
1790 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1794 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1795 FT_UShort NameID
, FT_UShort LangID
);
1797 /*************************************************************
1798 * IntGetOutlineTextMetrics
1802 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1804 OUTLINETEXTMETRICW
*Otm
)
1807 TT_HoriHeader
*pHori
;
1808 TT_Postscript
*pPost
;
1809 FT_Fixed XScale
, YScale
;
1810 FT_WinFNT_HeaderRec Win
;
1813 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1814 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1815 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1816 FT_Face Face
= SharedFace
->Face
;
1818 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1820 return Cache
->OutlineRequiredSize
;
1824 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1825 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1828 RtlInitUnicodeString(&FaceNameW
, NULL
);
1829 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1832 RtlInitUnicodeString(&StyleNameW
, NULL
);
1833 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1835 /* unique name (full name) */
1836 RtlInitUnicodeString(&FullNameW
, NULL
);
1837 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1839 if (!Cache
->OutlineRequiredSize
)
1842 Needed
= sizeof(OUTLINETEXTMETRICW
);
1843 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1844 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1845 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1846 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1848 Cache
->OutlineRequiredSize
= Needed
;
1851 if (Size
< Cache
->OutlineRequiredSize
)
1853 RtlFreeUnicodeString(&FamilyNameW
);
1854 RtlFreeUnicodeString(&FaceNameW
);
1855 RtlFreeUnicodeString(&StyleNameW
);
1856 RtlFreeUnicodeString(&FullNameW
);
1857 return Cache
->OutlineRequiredSize
;
1860 XScale
= Face
->size
->metrics
.x_scale
;
1861 YScale
= Face
->size
->metrics
.y_scale
;
1864 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1867 IntUnLockFreeType();
1868 DPRINT1("Can't find OS/2 table - not TT font?\n");
1869 RtlFreeUnicodeString(&FamilyNameW
);
1870 RtlFreeUnicodeString(&FaceNameW
);
1871 RtlFreeUnicodeString(&StyleNameW
);
1872 RtlFreeUnicodeString(&FullNameW
);
1876 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1879 IntUnLockFreeType();
1880 DPRINT1("Can't find HHEA table - not TT font?\n");
1881 RtlFreeUnicodeString(&FamilyNameW
);
1882 RtlFreeUnicodeString(&FaceNameW
);
1883 RtlFreeUnicodeString(&StyleNameW
);
1884 RtlFreeUnicodeString(&FullNameW
);
1888 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1890 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1892 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1894 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1897 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1898 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1899 Otm
->otmfsType
= pOS2
->fsType
;
1900 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1901 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1902 Otm
->otmItalicAngle
= 0; /* POST table */
1903 Otm
->otmEMSquare
= Face
->units_per_EM
;
1904 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1905 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1906 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1907 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1908 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1909 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1910 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1911 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1912 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1913 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1914 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1915 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1916 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1917 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1918 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1919 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1920 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1921 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1922 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1923 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1924 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1925 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1926 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1929 Otm
->otmsUnderscoreSize
= 0;
1930 Otm
->otmsUnderscorePosition
= 0;
1934 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1935 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1938 IntUnLockFreeType();
1940 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1943 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1944 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1945 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1948 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1949 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1950 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1953 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1954 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1955 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1957 /* unique name (full name) */
1958 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1959 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1960 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1962 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1964 RtlFreeUnicodeString(&FamilyNameW
);
1965 RtlFreeUnicodeString(&FaceNameW
);
1966 RtlFreeUnicodeString(&StyleNameW
);
1967 RtlFreeUnicodeString(&FullNameW
);
1969 return Cache
->OutlineRequiredSize
;
1972 static PFONTGDI FASTCALL
1973 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1976 PFONT_ENTRY CurrentEntry
;
1977 ANSI_STRING EntryFaceNameA
;
1978 UNICODE_STRING EntryFaceNameW
;
1982 Entry
= Head
->Flink
;
1983 while (Entry
!= Head
)
1985 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1987 FontGDI
= CurrentEntry
->Font
;
1990 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1991 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1992 if (!NT_SUCCESS(status
))
1997 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1999 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2000 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2003 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
2005 RtlFreeUnicodeString(&EntryFaceNameW
);
2009 RtlFreeUnicodeString(&EntryFaceNameW
);
2010 Entry
= Entry
->Flink
;
2016 static PFONTGDI FASTCALL
2017 FindFaceNameInLists(PUNICODE_STRING FaceName
)
2019 PPROCESSINFO Win32Process
;
2022 /* Search the process local list.
2023 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
2024 Win32Process
= PsGetCurrentProcessWin32Process();
2025 IntLockProcessPrivateFonts(Win32Process
);
2026 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
2027 IntUnLockProcessPrivateFonts(Win32Process
);
2033 /* Search the global list */
2034 IntLockGlobalFonts();
2035 Font
= FindFaceNameInList(FaceName
, &g_FontListHead
);
2036 IntUnLockGlobalFonts();
2041 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2043 CharSetFromLangID(LANGID LangID
)
2045 /* FIXME: Add more and fix if wrong */
2046 switch (PRIMARYLANGID(LangID
))
2049 switch (SUBLANGID(LangID
))
2051 case SUBLANG_CHINESE_TRADITIONAL
:
2052 return CHINESEBIG5_CHARSET
;
2053 case SUBLANG_CHINESE_SIMPLIFIED
:
2057 return GB2312_CHARSET
;
2059 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2060 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2061 return EASTEUROPE_CHARSET
;
2063 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2064 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2065 return RUSSIAN_CHARSET
;
2067 case LANG_ARABIC
: return ARABIC_CHARSET
;
2068 case LANG_GREEK
: return GREEK_CHARSET
;
2069 case LANG_HEBREW
: return HEBREW_CHARSET
;
2070 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2071 case LANG_KOREAN
: return JOHAB_CHARSET
;
2072 case LANG_TURKISH
: return TURKISH_CHARSET
;
2073 case LANG_THAI
: return THAI_CHARSET
;
2074 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2075 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2077 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2078 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2079 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2080 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2081 case LANG_SWEDISH
: default:
2082 return ANSI_CHARSET
;
2087 SwapEndian(LPVOID pvData
, DWORD Size
)
2089 BYTE b
, *pb
= pvData
;
2101 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2103 NTSTATUS Status
= STATUS_NO_MEMORY
;
2106 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2109 Tmp
.MaximumLength
= Source
->MaximumLength
;
2111 RtlCopyUnicodeString(&Tmp
, Source
);
2113 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2114 Destination
->Length
= Tmp
.Length
;
2115 Destination
->Buffer
= Tmp
.Buffer
;
2117 Status
= STATUS_SUCCESS
;
2124 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2125 FT_UShort NameID
, FT_UShort LangID
)
2128 INT i
, Count
, BestIndex
, Score
, BestScore
;
2130 NTSTATUS Status
= STATUS_NOT_FOUND
;
2131 ANSI_STRING AnsiName
;
2132 PSHARED_FACE_CACHE Cache
;
2133 FT_Face Face
= SharedFace
->Face
;
2135 RtlFreeUnicodeString(pNameW
);
2138 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2140 Cache
= &SharedFace
->EnglishUS
;
2144 Cache
= &SharedFace
->UserLanguage
;
2147 /* use cache if available */
2148 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2150 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2152 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2154 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2160 Count
= FT_Get_Sfnt_Name_Count(Face
);
2161 for (i
= 0; i
< Count
; ++i
)
2163 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2166 continue; /* failure */
2169 if (Name
.name_id
!= NameID
)
2171 continue; /* mismatched */
2174 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2175 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2176 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2178 continue; /* not Microsoft Unicode name */
2181 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2182 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2184 continue; /* invalid string */
2187 if (Name
.language_id
== LangID
)
2191 break; /* best match */
2193 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2197 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2206 if (Score
> BestScore
)
2215 /* store the best name */
2216 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2219 /* NOTE: Name.string is not null-terminated */
2221 Tmp
.Buffer
= (PWCH
)Name
.string
;
2222 Tmp
.Length
= Tmp
.MaximumLength
= Name
.string_len
;
2225 pNameW
->MaximumLength
= Name
.string_len
+ sizeof(WCHAR
);
2226 pNameW
->Buffer
= ExAllocatePoolWithTag(PagedPool
, pNameW
->MaximumLength
, TAG_USTR
);
2230 Status
= RtlAppendUnicodeStringToString(pNameW
, &Tmp
);
2231 if (Status
== STATUS_SUCCESS
)
2233 /* Convert UTF-16 big endian to little endian */
2234 SwapEndian(pNameW
->Buffer
, pNameW
->Length
);
2239 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2244 if (!NT_SUCCESS(Status
))
2247 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2249 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2250 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2254 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2255 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2259 if (NT_SUCCESS(Status
))
2262 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2264 ASSERT_FREETYPE_LOCK_NOT_HELD();
2266 if (!Cache
->FontFamily
.Buffer
)
2267 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2268 IntUnLockFreeType();
2270 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2272 ASSERT_FREETYPE_LOCK_NOT_HELD();
2274 if (!Cache
->FullName
.Buffer
)
2275 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2276 IntUnLockFreeType();
2283 static void FASTCALL
2284 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2285 LPCWSTR FullName
, PFONTGDI FontGDI
)
2288 UNICODE_STRING StyleW
;
2291 CHARSETINFO CharSetInfo
;
2293 OUTLINETEXTMETRICW
*Otm
;
2296 NEWTEXTMETRICW
*Ntm
;
2299 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2300 FT_Face Face
= SharedFace
->Face
;
2301 UNICODE_STRING NameW
;
2303 RtlInitUnicodeString(&NameW
, NULL
);
2304 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2305 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2306 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2311 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2314 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2318 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2319 TM
= &Otm
->otmTextMetrics
;
2321 Lf
->lfHeight
= TM
->tmHeight
;
2322 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2323 Lf
->lfWeight
= TM
->tmWeight
;
2324 Lf
->lfItalic
= TM
->tmItalic
;
2325 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2326 Lf
->lfCharSet
= TM
->tmCharSet
;
2327 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2328 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2329 Lf
->lfQuality
= PROOF_QUALITY
;
2331 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2332 Ntm
->tmHeight
= TM
->tmHeight
;
2333 Ntm
->tmAscent
= TM
->tmAscent
;
2334 Ntm
->tmDescent
= TM
->tmDescent
;
2335 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2336 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2337 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2338 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2339 Ntm
->tmWeight
= TM
->tmWeight
;
2340 Ntm
->tmOverhang
= TM
->tmOverhang
;
2341 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2342 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2343 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2344 Ntm
->tmLastChar
= TM
->tmLastChar
;
2345 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2346 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2347 Ntm
->tmItalic
= TM
->tmItalic
;
2348 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2349 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2350 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2351 Ntm
->tmCharSet
= TM
->tmCharSet
;
2352 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2354 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2356 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2358 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2359 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2360 Ntm
->ntmAvgWidth
= 0;
2362 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2363 ? TRUETYPE_FONTTYPE
: 0);
2365 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2366 Info
->FontType
|= RASTER_FONTTYPE
;
2371 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2373 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2377 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2379 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2380 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2383 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2385 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2386 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2387 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2388 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2389 if (!NT_SUCCESS(status
))
2393 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2396 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2400 IntUnLockFreeType();
2404 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2405 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2406 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2407 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2408 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2409 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2411 if (0 == pOS2
->version
)
2415 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2416 fs
.fsCsb
[0] |= FS_LATIN1
;
2418 fs
.fsCsb
[0] |= FS_SYMBOL
;
2420 IntUnLockFreeType();
2422 if (fs
.fsCsb
[0] == 0)
2424 /* Let's see if we can find any interesting cmaps */
2425 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2427 switch (Face
->charmaps
[i
]->encoding
)
2429 case FT_ENCODING_UNICODE
:
2430 case FT_ENCODING_APPLE_ROMAN
:
2431 fs
.fsCsb
[0] |= FS_LATIN1
;
2433 case FT_ENCODING_MS_SYMBOL
:
2434 fs
.fsCsb
[0] |= FS_SYMBOL
;
2442 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2445 if (fs
.fsCsb
[0] & fs0
)
2447 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2449 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2451 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2453 if (g_ElfScripts
[i
])
2454 wcscpy(Info
->EnumLogFontEx
.elfScript
, g_ElfScripts
[i
]);
2457 DPRINT1("Unknown elfscript for bit %u\n", i
);
2462 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2466 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2469 UNICODE_STRING InfoFaceName
;
2471 for (i
= 0; i
< InfoEntries
; i
++)
2473 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2474 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2483 static BOOLEAN FASTCALL
2484 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2485 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2487 UNICODE_STRING LogFontFaceName
;
2489 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2490 if (0 != LogFontFaceName
.Length
&&
2491 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2496 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2499 static BOOL FASTCALL
2500 FontFamilyFound(PFONTFAMILYINFO InfoEntry
,
2501 PFONTFAMILYINFO Info
, DWORD InfoCount
)
2503 LPLOGFONTW plf1
= &InfoEntry
->EnumLogFontEx
.elfLogFont
;
2504 LPWSTR pFullName1
= InfoEntry
->EnumLogFontEx
.elfFullName
;
2508 for (i
= 0; i
< InfoCount
; ++i
)
2510 LPLOGFONTW plf2
= &Info
[i
].EnumLogFontEx
.elfLogFont
;
2511 if (plf1
->lfCharSet
!= plf2
->lfCharSet
)
2514 pFullName2
= Info
[i
].EnumLogFontEx
.elfFullName
;
2515 if (_wcsicmp(pFullName1
, pFullName2
) != 0)
2523 static BOOLEAN FASTCALL
2524 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2525 PFONTFAMILYINFO Info
,
2531 PFONT_ENTRY CurrentEntry
;
2533 FONTFAMILYINFO InfoEntry
;
2534 DWORD Count
= *pCount
;
2536 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
2538 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2539 FontGDI
= CurrentEntry
->Font
;
2542 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&&
2543 LogFont
->lfCharSet
!= FontGDI
->CharSet
)
2548 if (LogFont
->lfFaceName
[0] == UNICODE_NULL
)
2550 if (Count
< MaxCount
)
2552 FontFamilyFillInfo(&Info
[Count
], NULL
, NULL
, FontGDI
);
2558 FontFamilyFillInfo(&InfoEntry
, NULL
, NULL
, FontGDI
);
2560 if (_wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0 &&
2561 _wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfFullName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0)
2566 if (!FontFamilyFound(&InfoEntry
, Info
, min(Count
, MaxCount
)))
2568 if (Count
< MaxCount
)
2570 RtlCopyMemory(&Info
[Count
], &InfoEntry
, sizeof(InfoEntry
));
2581 static BOOLEAN FASTCALL
2582 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2583 PFONTFAMILYINFO Info
,
2587 PLIST_ENTRY pEntry
, pHead
= &g_FontSubstListHead
;
2588 PFONTSUBST_ENTRY pCurrentEntry
;
2589 PUNICODE_STRING pFromW
;
2591 LOGFONTW lf
= *LogFont
;
2592 UNICODE_STRING NameW
;
2594 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2596 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2598 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2599 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2601 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2602 continue; /* mismatch */
2605 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2606 SubstituteFontRecurse(&lf
);
2608 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2609 FontGDI
= FindFaceNameInLists(&NameW
);
2610 if (FontGDI
== NULL
)
2612 continue; /* no real font */
2615 if (*pCount
< MaxCount
)
2617 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2627 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2631 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2632 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2633 lprs
->nLanguageID
= gusLanguageID
;
2636 EngSetLastError(ERROR_INVALID_PARAMETER
);
2646 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2647 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2648 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2649 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2652 FT_BitmapGlyph APIENTRY
2657 FT_Render_Mode RenderMode
,
2660 PLIST_ENTRY CurrentEntry
;
2661 PFONT_CACHE_ENTRY FontEntry
;
2663 ASSERT_FREETYPE_LOCK_HELD();
2665 CurrentEntry
= g_FontCacheListHead
.Flink
;
2666 while (CurrentEntry
!= &g_FontCacheListHead
)
2668 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2669 if ((FontEntry
->Face
== Face
) &&
2670 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2671 (FontEntry
->Height
== Height
) &&
2672 (FontEntry
->RenderMode
== RenderMode
) &&
2673 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2675 CurrentEntry
= CurrentEntry
->Flink
;
2678 if (CurrentEntry
== &g_FontCacheListHead
)
2683 RemoveEntryList(CurrentEntry
);
2684 InsertHeadList(&g_FontCacheListHead
, CurrentEntry
);
2685 return FontEntry
->BitmapGlyph
;
2689 FT_BitmapGlyph APIENTRY
2692 FT_GlyphSlot GlyphSlot
,
2693 FT_Render_Mode RenderMode
)
2697 FT_Bitmap AlignedBitmap
;
2698 FT_BitmapGlyph BitmapGlyph
;
2700 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2703 DPRINT1("Failure getting glyph.\n");
2707 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2710 FT_Done_Glyph(Glyph
);
2711 DPRINT1("Failure rendering glyph.\n");
2715 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2716 FT_Bitmap_New(&AlignedBitmap
);
2717 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2719 DPRINT1("Conversion failed\n");
2720 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2724 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2725 BitmapGlyph
->bitmap
= AlignedBitmap
;
2730 FT_BitmapGlyph APIENTRY
2736 FT_GlyphSlot GlyphSlot
,
2737 FT_Render_Mode RenderMode
)
2741 PFONT_CACHE_ENTRY NewEntry
;
2742 FT_Bitmap AlignedBitmap
;
2743 FT_BitmapGlyph BitmapGlyph
;
2745 ASSERT_FREETYPE_LOCK_HELD();
2747 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2750 DPRINT1("Failure caching glyph.\n");
2754 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2757 FT_Done_Glyph(GlyphCopy
);
2758 DPRINT1("Failure rendering glyph.\n");
2762 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2765 DPRINT1("Alloc failure caching glyph.\n");
2766 FT_Done_Glyph(GlyphCopy
);
2770 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2771 FT_Bitmap_New(&AlignedBitmap
);
2772 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2774 DPRINT1("Conversion failed\n");
2775 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2776 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2777 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2781 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2782 BitmapGlyph
->bitmap
= AlignedBitmap
;
2784 NewEntry
->GlyphIndex
= GlyphIndex
;
2785 NewEntry
->Face
= Face
;
2786 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2787 NewEntry
->Height
= Height
;
2788 NewEntry
->RenderMode
= RenderMode
;
2789 NewEntry
->mxWorldToDevice
= *pmx
;
2791 InsertHeadList(&g_FontCacheListHead
, &NewEntry
->ListEntry
);
2792 if (++g_FontCacheNumEntries
> MAX_FONT_CACHE
)
2794 NewEntry
= CONTAINING_RECORD(g_FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2795 RemoveCachedEntry(NewEntry
);
2802 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2804 pt
->x
.value
= vec
->x
>> 6;
2805 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2806 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2807 pt
->y
.value
= vec
->y
>> 6;
2808 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2809 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2813 This function builds an FT_Fixed from a float. It puts the integer part
2814 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2815 It fails if the integer part of the float number is greater than SHORT_MAX.
2817 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2820 unsigned short fract
= (f
- value
) * 0xFFFF;
2821 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2825 This function builds an FT_Fixed from a FIXED. It simply put f.value
2826 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2828 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2830 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2833 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2835 TTPOLYGONHEADER
*pph
;
2837 int needed
= 0, point
= 0, contour
, first_pt
;
2838 unsigned int pph_start
, cpfx
;
2841 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2843 /* Ignore contours containing one point */
2844 if (point
== outline
->contours
[contour
])
2851 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2855 pph
->dwType
= TT_POLYGON_TYPE
;
2856 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2858 needed
+= sizeof(*pph
);
2860 while (point
<= outline
->contours
[contour
])
2862 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2863 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2864 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2869 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2872 } while (point
<= outline
->contours
[contour
] &&
2873 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2874 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2875 /* At the end of a contour Windows adds the start point, but
2877 if (point
> outline
->contours
[contour
] &&
2878 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2881 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2884 else if (point
<= outline
->contours
[contour
] &&
2885 outline
->tags
[point
] & FT_Curve_Tag_On
)
2887 /* add closing pt for bezier */
2889 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2898 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2901 pph
->cb
= needed
- pph_start
;
2906 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2908 /* Convert the quadratic Beziers to cubic Beziers.
2909 The parametric eqn for a cubic Bezier is, from PLRM:
2910 r(t) = at^3 + bt^2 + ct + r0
2911 with the control points:
2916 A quadratic Bezier has the form:
2917 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2919 So equating powers of t leads to:
2920 r1 = 2/3 p1 + 1/3 p0
2921 r2 = 2/3 p1 + 1/3 p2
2922 and of course r0 = p0, r3 = p2
2924 int contour
, point
= 0, first_pt
;
2925 TTPOLYGONHEADER
*pph
;
2927 DWORD pph_start
, cpfx
, type
;
2928 FT_Vector cubic_control
[4];
2929 unsigned int needed
= 0;
2931 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2934 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2938 pph
->dwType
= TT_POLYGON_TYPE
;
2939 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2941 needed
+= sizeof(*pph
);
2943 while (point
<= outline
->contours
[contour
])
2945 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2946 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2947 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2951 if (type
== TT_PRIM_LINE
)
2954 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2960 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2963 /* FIXME: Possible optimization in endpoint calculation
2964 if there are two consecutive curves */
2965 cubic_control
[0] = outline
->points
[point
-1];
2966 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2968 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2969 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2970 cubic_control
[0].x
>>= 1;
2971 cubic_control
[0].y
>>= 1;
2973 if (point
+1 > outline
->contours
[contour
])
2974 cubic_control
[3] = outline
->points
[first_pt
];
2977 cubic_control
[3] = outline
->points
[point
+1];
2978 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2980 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2981 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2982 cubic_control
[3].x
>>= 1;
2983 cubic_control
[3].y
>>= 1;
2986 /* r1 = 1/3 p0 + 2/3 p1
2987 r2 = 1/3 p2 + 2/3 p1 */
2988 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2989 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2990 cubic_control
[2] = cubic_control
[1];
2991 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2992 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2993 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2994 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2997 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2998 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2999 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
3004 } while (point
<= outline
->contours
[contour
] &&
3005 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
3006 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
3007 /* At the end of a contour Windows adds the start point,
3008 but only for Beziers and we've already done that.
3010 if (point
<= outline
->contours
[contour
] &&
3011 outline
->tags
[point
] & FT_Curve_Tag_On
)
3013 /* This is the closing pt of a bezier, but we've already
3014 added it, so just inc point and carry on */
3022 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
3025 pph
->cb
= needed
- pph_start
;
3031 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
)
3034 FT_Size_RequestRec req
;
3035 FT_Face face
= FontGDI
->SharedFace
->Face
;
3037 TT_HoriHeader
*pHori
;
3038 FT_WinFNT_HeaderRec WinFNT
;
3039 LONG Ascent
, Descent
, Sum
, EmHeight64
;
3041 lfWidth
= abs(lfWidth
);
3046 DPRINT("lfHeight and lfWidth are zero.\n");
3058 ASSERT_FREETYPE_LOCK_HELD();
3059 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(face
, FT_SFNT_OS2
);
3060 pHori
= (TT_HoriHeader
*)FT_Get_Sfnt_Table(face
, FT_SFNT_HHEA
);
3062 if (!pOS2
|| !pHori
)
3064 error
= FT_Get_WinFNT_Header(face
, &WinFNT
);
3068 FontGDI
->tmHeight
= WinFNT
.pixel_height
;
3069 FontGDI
->tmAscent
= WinFNT
.ascent
;
3070 FontGDI
->tmDescent
= FontGDI
->tmHeight
- FontGDI
->tmAscent
;
3071 FontGDI
->tmInternalLeading
= WinFNT
.internal_leading
;
3072 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3073 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3074 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3075 FontGDI
->Magic
= FONTGDI_MAGIC
;
3077 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3079 req
.height
= (FT_Long
)(FontGDI
->EmHeight
<< 6);
3080 req
.horiResolution
= 0;
3081 req
.vertResolution
= 0;
3082 return FT_Request_Size(face
, &req
);
3087 /* case (A): lfHeight is positive */
3088 Sum
= pOS2
->usWinAscent
+ pOS2
->usWinDescent
;
3091 Ascent
= pHori
->Ascender
;
3092 Descent
= -pHori
->Descender
;
3093 Sum
= Ascent
+ Descent
;
3097 Ascent
= pOS2
->usWinAscent
;
3098 Descent
= pOS2
->usWinDescent
;
3101 FontGDI
->tmAscent
= FT_MulDiv(lfHeight
, Ascent
, Sum
);
3102 FontGDI
->tmDescent
= FT_MulDiv(lfHeight
, Descent
, Sum
);
3103 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3104 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
- FT_MulDiv(lfHeight
, face
->units_per_EM
, Sum
);
3106 else if (lfHeight
< 0)
3108 /* case (B): lfHeight is negative */
3109 FontGDI
->tmAscent
= FT_MulDiv(-lfHeight
, pOS2
->usWinAscent
, face
->units_per_EM
);
3110 FontGDI
->tmDescent
= FT_MulDiv(-lfHeight
, pOS2
->usWinDescent
, face
->units_per_EM
);
3111 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3112 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
+ lfHeight
;
3115 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3116 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3117 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3118 FontGDI
->Magic
= FONTGDI_MAGIC
;
3121 EmHeight64
= (FontGDI
->EmHeight
<< 6) + 31;
3123 EmHeight64
= (FontGDI
->EmHeight
<< 6);
3125 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3127 req
.height
= EmHeight64
;
3128 req
.horiResolution
= 0;
3129 req
.vertResolution
= 0;
3130 return FT_Request_Size(face
, &req
);
3135 TextIntUpdateSize(PDC dc
,
3142 FT_CharMap charmap
, found
;
3148 face
= FontGDI
->SharedFace
->Face
;
3149 if (face
->charmap
== NULL
)
3151 DPRINT("WARNING: No charmap selected!\n");
3152 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3155 for (n
= 0; n
< face
->num_charmaps
; n
++)
3157 charmap
= face
->charmaps
[n
];
3158 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
3159 if (charmap
->encoding
!= 0)
3167 DPRINT1("WARNING: Could not find desired charmap!\n");
3171 error
= FT_Set_Charmap(face
, found
);
3174 DPRINT1("WARNING: Could not set the charmap!\n");
3179 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3181 error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3184 IntUnLockFreeType();
3188 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3197 * Based on WineEngGetGlyphOutline
3202 ftGdiGetGlyphOutline(
3210 BOOL bIgnoreRotation
)
3212 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3220 FT_UInt glyph_index
;
3221 DWORD width
, height
, pitch
, needed
= 0;
3222 FT_Bitmap ft_bitmap
;
3224 INT left
, right
, top
= 0, bottom
= 0;
3226 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3227 FLOAT eM11
, widthRatio
= 1.0;
3228 FT_Matrix transMat
= identityMat
;
3229 BOOL needsTransform
= FALSE
;