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 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
810 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
814 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
818 ANSI_STRING AnsiFaceName
;
819 FT_WinFNT_HeaderRec WinFNT
;
820 INT FaceCount
= 0, CharSetCount
= 0;
821 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
822 DWORD Characteristics
= pLoadFont
->Characteristics
;
823 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
826 FT_UShort os2_version
;
827 FT_ULong os2_ulCodePageRange1
;
828 FT_UShort os2_usWeightClass
;
830 if (SharedFace
== NULL
&& CharSetIndex
== -1)
832 /* load a face from memory */
834 Error
= FT_New_Memory_Face(
836 pLoadFont
->Memory
->Buffer
,
837 pLoadFont
->Memory
->BufferSize
,
838 ((FontIndex
!= -1) ? FontIndex
: 0),
842 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
846 if (!Error
&& FT_IS_SFNT(Face
))
847 pLoadFont
->IsTrueType
= TRUE
;
849 if (Error
|| SharedFace
== NULL
)
852 SharedFace_Release(SharedFace
);
854 if (Error
== FT_Err_Unknown_File_Format
)
855 DPRINT1("Unknown font file format\n");
857 DPRINT1("Error reading font (error code: %d)\n", Error
);
858 return 0; /* failure */
863 Face
= SharedFace
->Face
;
865 SharedFace_AddRef(SharedFace
);
869 /* allocate a FONT_ENTRY */
870 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
873 SharedFace_Release(SharedFace
);
874 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
875 return 0; /* failure */
878 /* allocate a FONTGDI */
879 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
882 SharedFace_Release(SharedFace
);
883 ExFreePoolWithTag(Entry
, TAG_FONT
);
884 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
885 return 0; /* failure */
891 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
892 pFileName
->Length
+ sizeof(UNICODE_NULL
),
894 if (FontGDI
->Filename
== NULL
)
897 SharedFace_Release(SharedFace
);
898 ExFreePoolWithTag(Entry
, TAG_FONT
);
899 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
900 return 0; /* failure */
902 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
903 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
907 FontGDI
->Filename
= NULL
;
909 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
912 if (FontGDI
->Filename
)
913 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
915 SharedFace_Release(SharedFace
);
916 ExFreePoolWithTag(Entry
, TAG_FONT
);
920 PrivateEntry
->Entry
= Entry
;
921 if (pLoadFont
->PrivateEntry
)
923 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
927 InitializeListHead(&PrivateEntry
->ListEntry
);
928 pLoadFont
->PrivateEntry
= PrivateEntry
;
933 FontGDI
->SharedFace
= SharedFace
;
934 FontGDI
->CharSet
= ANSI_CHARSET
;
935 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
936 FontGDI
->RequestItalic
= FALSE
;
937 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
938 FontGDI
->RequestWeight
= FW_NORMAL
;
940 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
941 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
942 if (!NT_SUCCESS(Status
))
946 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
948 pLoadFont
->PrivateEntry
= NULL
;
952 RemoveEntryList(&PrivateEntry
->ListEntry
);
954 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
956 if (FontGDI
->Filename
)
957 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
959 SharedFace_Release(SharedFace
);
960 ExFreePoolWithTag(Entry
, TAG_FONT
);
966 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
969 os2_version
= pOS2
->version
;
970 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
971 os2_usWeightClass
= pOS2
->usWeightClass
;
975 if (pOS2
&& os2_version
>= 1)
977 /* get charset and weight from OS/2 header */
979 /* Make sure we do not use this pointer anymore */
982 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
984 if (os2_ulCodePageRange1
& (1 << BitIndex
))
986 if (g_FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
989 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
990 CharSetIndex
== CharSetCount
)
992 FontGDI
->CharSet
= g_FontTci
[BitIndex
].ciCharset
;
999 /* set actual weight */
1000 FontGDI
->OriginalWeight
= os2_usWeightClass
;
1004 /* get charset from WinFNT header */
1006 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
1009 FontGDI
->CharSet
= WinFNT
.charset
;
1011 IntUnLockFreeType();
1014 /* FIXME: CharSet is invalid on Marlett */
1015 if (RtlEqualUnicodeString(&Entry
->FaceName
, &g_MarlettW
, TRUE
))
1017 FontGDI
->CharSet
= SYMBOL_CHARSET
;
1021 DPRINT("Font loaded: %s (%s)\n",
1022 Face
->family_name
? Face
->family_name
: "<NULL>",
1023 Face
->style_name
? Face
->style_name
: "<NULL>");
1024 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
1025 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
1027 /* Add this font resource to the font table */
1028 Entry
->Font
= FontGDI
;
1029 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1031 if (Characteristics
& FR_PRIVATE
)
1034 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1035 IntLockProcessPrivateFonts(Win32Process
);
1036 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1037 IntUnLockProcessPrivateFonts(Win32Process
);
1042 IntLockGlobalFonts();
1043 InsertTailList(&g_FontListHead
, &Entry
->ListEntry
);
1044 IntUnLockGlobalFonts();
1047 if (FontIndex
== -1)
1049 if (FT_IS_SFNT(Face
))
1051 TT_Face TrueType
= (TT_Face
)Face
;
1052 if (TrueType
->ttc_header
.count
> 1)
1055 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1057 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1064 if (CharSetIndex
== -1)
1068 if (pLoadFont
->RegValueName
.Length
== 0)
1070 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1074 UNICODE_STRING NewString
;
1075 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1076 NewString
.Length
= 0;
1077 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1078 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1079 NewString
.MaximumLength
,
1081 NewString
.Buffer
[0] = UNICODE_NULL
;
1083 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1084 RtlAppendUnicodeToString(&NewString
, L
" & ");
1085 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1087 RtlFreeUnicodeString(pValueName
);
1088 *pValueName
= NewString
;
1091 for (i
= 1; i
< CharSetCount
; ++i
)
1093 /* Do not count charsets towards 'faces' loaded */
1094 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1098 return FaceCount
; /* number of loaded faces */
1102 * IntGdiAddFontResource
1104 * Adds the font resource from the specified file to the system.
1108 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1112 PVOID Buffer
= NULL
;
1113 IO_STATUS_BLOCK Iosb
;
1114 PVOID SectionObject
;
1115 SIZE_T ViewSize
= 0;
1116 LARGE_INTEGER SectionSize
;
1117 OBJECT_ATTRIBUTES ObjectAttributes
;
1118 GDI_LOAD_FONT LoadFont
;
1121 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1123 /* Open the font file */
1124 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1125 Status
= ZwOpenFile(
1127 FILE_GENERIC_READ
| SYNCHRONIZE
,
1131 FILE_SYNCHRONOUS_IO_NONALERT
);
1132 if (!NT_SUCCESS(Status
))
1134 DPRINT("Could not load font file: %wZ\n", FileName
);
1138 SectionSize
.QuadPart
= 0LL;
1139 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1140 NULL
, &SectionSize
, PAGE_READONLY
,
1141 SEC_COMMIT
, FileHandle
, NULL
);
1142 if (!NT_SUCCESS(Status
))
1144 DPRINT("Could not map file: %wZ\n", FileName
);
1145 ZwClose(FileHandle
);
1148 ZwClose(FileHandle
);
1150 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1151 if (!NT_SUCCESS(Status
))
1153 DPRINT("Could not map file: %wZ\n", FileName
);
1154 ObDereferenceObject(SectionObject
);
1158 LoadFont
.pFileName
= FileName
;
1159 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1160 LoadFont
.Characteristics
= Characteristics
;
1161 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1162 LoadFont
.IsTrueType
= FALSE
;
1163 LoadFont
.PrivateEntry
= NULL
;
1164 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1166 ObDereferenceObject(SectionObject
);
1168 /* Release our copy */
1170 SharedMem_Release(LoadFont
.Memory
);
1171 IntUnLockFreeType();
1175 if (LoadFont
.IsTrueType
)
1177 /* append " (TrueType)" */
1178 UNICODE_STRING NewString
;
1181 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1182 NewString
.Length
= 0;
1183 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1184 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1185 NewString
.MaximumLength
,
1187 NewString
.Buffer
[0] = UNICODE_NULL
;
1189 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1190 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1191 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1192 LoadFont
.RegValueName
= NewString
;
1196 InitializeObjectAttributes(&ObjectAttributes
, &g_FontRegPath
,
1197 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1199 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1200 if (NT_SUCCESS(Status
))
1203 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1207 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1208 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1209 pFileName
, DataSize
);
1214 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1220 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1222 GDI_LOAD_FONT LoadFont
;
1223 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1227 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1234 memcpy(BufferCopy
, Buffer
, dwSize
);
1236 LoadFont
.pFileName
= NULL
;
1237 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1238 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1239 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1240 LoadFont
.IsTrueType
= FALSE
;
1241 LoadFont
.PrivateEntry
= NULL
;
1242 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1244 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1246 /* Release our copy */
1248 SharedMem_Release(LoadFont
.Memory
);
1249 IntUnLockFreeType();
1253 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1254 if (EntryCollection
)
1256 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1257 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1258 IntLockProcessPrivateFonts(Win32Process
);
1259 EntryCollection
->Handle
= ULongToHandle(++Win32Process
->PrivateMemFontHandleCount
);
1260 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1261 IntUnLockProcessPrivateFonts(Win32Process
);
1262 Ret
= EntryCollection
->Handle
;
1265 *pNumAdded
= FaceCount
;
1270 // FIXME: Add RemoveFontResource
1272 static VOID FASTCALL
1273 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1275 PFONTGDI FontGDI
= FontEntry
->Font
;
1276 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1278 if (FontGDI
->Filename
)
1279 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1281 EngFreeMem(FontGDI
);
1282 SharedFace_Release(SharedFace
);
1283 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1287 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1290 PFONT_ENTRY_MEM FontEntry
;
1292 while (!IsListEmpty(&Head
->ListEntry
))
1294 Entry
= RemoveHeadList(&Head
->ListEntry
);
1295 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1297 CleanupFontEntry(FontEntry
->Entry
);
1298 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1301 CleanupFontEntry(Head
->Entry
);
1302 ExFreePoolWithTag(Head
, TAG_FONT
);
1305 static VOID FASTCALL
1306 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1308 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1309 PLIST_ENTRY ListEntry
;
1310 RemoveEntryList(&Collection
->ListEntry
);
1313 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1314 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1316 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1317 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1319 } while (FontMemEntry
!= Collection
->Entry
);
1323 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1326 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1327 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1328 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1330 IntLockProcessPrivateFonts(Win32Process
);
1331 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1332 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1334 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1336 if (CurrentEntry
->Handle
== hMMFont
)
1338 EntryCollection
= CurrentEntry
;
1339 UnlinkFontMemCollection(CurrentEntry
);
1343 Entry
= Entry
->Flink
;
1345 IntUnLockProcessPrivateFonts(Win32Process
);
1347 if (EntryCollection
)
1349 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1350 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1358 IntGdiCleanupPrivateFontsForProcess(VOID
)
1360 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1362 PFONT_ENTRY_COLL_MEM EntryCollection
;
1364 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1367 EntryCollection
= NULL
;
1369 IntLockProcessPrivateFonts(Win32Process
);
1370 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1372 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1373 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1374 UnlinkFontMemCollection(EntryCollection
);
1376 IntUnLockProcessPrivateFonts(Win32Process
);
1378 if (EntryCollection
)
1380 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1381 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1385 /* No Mem fonts anymore, see if we have any other private fonts left */
1387 IntLockProcessPrivateFonts(Win32Process
);
1388 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1390 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1392 IntUnLockProcessPrivateFonts(Win32Process
);
1396 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1404 IntIsFontRenderingEnabled(VOID
)
1406 BOOL Ret
= g_RenderingEnabled
;
1409 hDC
= IntGetScreenDC();
1411 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && g_RenderingEnabled
;
1417 IntEnableFontRendering(BOOL Enable
)
1419 g_RenderingEnabled
= Enable
;
1422 FT_Render_Mode FASTCALL
1423 IntGetFontRenderMode(LOGFONTW
*logfont
)
1425 switch (logfont
->lfQuality
)
1427 case ANTIALIASED_QUALITY
:
1429 case NONANTIALIASED_QUALITY
:
1430 return FT_RENDER_MODE_MONO
;
1432 return FT_RENDER_MODE_LIGHT
;
1433 /* case CLEARTYPE_QUALITY:
1434 return FT_RENDER_MODE_LCD; */
1436 return FT_RENDER_MODE_NORMAL
;
1441 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1446 plfont
= LFONT_AllocFontWithHandle();
1449 return STATUS_NO_MEMORY
;
1452 ExInitializePushLock(&plfont
->lock
);
1453 *NewFont
= plfont
->BaseObject
.hHmgr
;
1454 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1455 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1456 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1458 /* This should really depend on whether GM_ADVANCED is set */
1459 plf
->lfOrientation
= plf
->lfEscapement
;
1461 LFONT_UnlockFont(plfont
);
1463 return STATUS_SUCCESS
;
1466 /*************************************************************************
1467 * TranslateCharsetInfo
1469 * Fills a CHARSETINFO structure for a character set, code page, or
1470 * font. This allows making the correspondance between different labelings
1471 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1472 * of the same encoding.
1474 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1475 * only one codepage should be set in *Src.
1478 * TRUE on success, FALSE on failure.
1481 static BOOLEAN APIENTRY
1482 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1483 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1484 if flags == TCI_SRCCHARSET: a character set value
1485 if flags == TCI_SRCCODEPAGE: a code page value */
1486 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1487 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1493 case TCI_SRCFONTSIG
:
1494 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1499 case TCI_SRCCODEPAGE
:
1500 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciACP
)
1505 case TCI_SRCCHARSET
:
1506 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciCharset
)
1515 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== g_FontTci
[Index
].ciCharset
)
1520 RtlCopyMemory(Cs
, &g_FontTci
[Index
], sizeof(CHARSETINFO
));
1526 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1530 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1532 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1539 static void FASTCALL
1540 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1541 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1542 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1544 FT_Fixed XScale
, YScale
;
1545 int Ascent
, Descent
;
1546 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1548 XScale
= Face
->size
->metrics
.x_scale
;
1549 YScale
= Face
->size
->metrics
.y_scale
;
1553 TM
->tmHeight
= pFNT
->pixel_height
;
1554 TM
->tmAscent
= pFNT
->ascent
;
1555 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1556 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1557 TM
->tmExternalLeading
= pFNT
->external_leading
;
1558 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1559 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1561 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1562 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1563 TM
->tmFirstChar
= pFNT
->first_char
;
1564 TM
->tmLastChar
= pFNT
->last_char
;
1565 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1566 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1567 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1570 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1571 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1572 TM
->tmUnderlined
= pFNT
->underline
;
1573 TM
->tmStruckOut
= pFNT
->strike_out
;
1574 TM
->tmCharSet
= pFNT
->charset
;
1578 TM
->tmWeight
= FontGDI
->RequestWeight
;
1579 TM
->tmItalic
= FontGDI
->RequestItalic
;
1580 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1581 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1582 TM
->tmCharSet
= FontGDI
->CharSet
;
1587 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1589 Ascent
= pHori
->Ascender
;
1590 Descent
= -pHori
->Descender
;
1594 Ascent
= pOS2
->usWinAscent
;
1595 Descent
= pOS2
->usWinDescent
;
1598 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1599 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1600 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1601 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1602 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1603 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1605 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1607 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1610 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1612 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1613 - ((Ascent
+ Descent
)
1614 - (pHori
->Ascender
- pHori
->Descender
)),
1615 YScale
) + 32) >> 6);
1617 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1618 if (TM
->tmAveCharWidth
== 0)
1620 TM
->tmAveCharWidth
= 1;
1623 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1624 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1628 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1632 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1633 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1635 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1639 TM
->tmWeight
= FontGDI
->RequestWeight
;
1644 TM
->tmDigitizedAspectX
= 96;
1645 TM
->tmDigitizedAspectY
= 96;
1646 if (face_has_symbol_charmap(Face
) ||
1647 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1649 USHORT cpOEM
, cpAnsi
;
1651 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1652 TM
->tmFirstChar
= 0;
1655 case 1257: /* Baltic */
1656 TM
->tmLastChar
= 0xf8fd;
1659 TM
->tmLastChar
= 0xf0ff;
1661 TM
->tmBreakChar
= 0x20;
1662 TM
->tmDefaultChar
= 0x1f;
1666 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1667 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1669 if(pOS2
->usFirstCharIndex
<= 1)
1670 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1671 else if (pOS2
->usFirstCharIndex
> 0xff)
1672 TM
->tmBreakChar
= 0x20;
1674 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1675 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1680 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1681 TM
->tmUnderlined
= FALSE
;
1682 TM
->tmStruckOut
= FALSE
;
1686 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1688 TM
->tmItalic
= 0xFF;
1694 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1695 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1698 if (!FT_IS_FIXED_WIDTH(Face
))
1700 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1702 case PAN_PROP_MONOSPACED
:
1703 TM
->tmPitchAndFamily
= 0;
1706 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1712 TM
->tmPitchAndFamily
= 0;
1715 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1717 case PAN_FAMILY_SCRIPT
:
1718 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1720 case PAN_FAMILY_DECORATIVE
:
1721 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1726 case PAN_FAMILY_TEXT_DISPLAY
:
1727 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1728 /* Which is clearly not what the panose spec says. */
1729 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1731 TM
->tmPitchAndFamily
= FF_MODERN
;
1735 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1740 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1743 case PAN_SERIF_COVE
:
1744 case PAN_SERIF_OBTUSE_COVE
:
1745 case PAN_SERIF_SQUARE_COVE
:
1746 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1747 case PAN_SERIF_SQUARE
:
1748 case PAN_SERIF_THIN
:
1749 case PAN_SERIF_BONE
:
1750 case PAN_SERIF_EXAGGERATED
:
1751 case PAN_SERIF_TRIANGLE
:
1752 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1755 case PAN_SERIF_NORMAL_SANS
:
1756 case PAN_SERIF_OBTUSE_SANS
:
1757 case PAN_SERIF_PERP_SANS
:
1758 case PAN_SERIF_FLARED
:
1759 case PAN_SERIF_ROUNDED
:
1760 TM
->tmPitchAndFamily
|= FF_SWISS
;
1766 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1769 if (FT_IS_SCALABLE(Face
))
1771 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1773 if (FT_IS_SFNT(Face
))
1775 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1778 TM
->tmCharSet
= FontGDI
->CharSet
;
1781 static void FASTCALL
1782 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1783 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1784 FT_WinFNT_HeaderRec
*pFNT
)
1786 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1790 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1791 FT_UShort NameID
, FT_UShort LangID
);
1793 /*************************************************************
1794 * IntGetOutlineTextMetrics
1798 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1800 OUTLINETEXTMETRICW
*Otm
)
1803 TT_HoriHeader
*pHori
;
1804 TT_Postscript
*pPost
;
1805 FT_Fixed XScale
, YScale
;
1806 FT_WinFNT_HeaderRec Win
;
1809 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1810 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1811 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1812 FT_Face Face
= SharedFace
->Face
;
1814 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1816 return Cache
->OutlineRequiredSize
;
1820 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1821 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1824 RtlInitUnicodeString(&FaceNameW
, NULL
);
1825 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1828 RtlInitUnicodeString(&StyleNameW
, NULL
);
1829 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1831 /* unique name (full name) */
1832 RtlInitUnicodeString(&FullNameW
, NULL
);
1833 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1835 if (!Cache
->OutlineRequiredSize
)
1838 Needed
= sizeof(OUTLINETEXTMETRICW
);
1839 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1840 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1841 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1842 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1844 Cache
->OutlineRequiredSize
= Needed
;
1847 if (Size
< Cache
->OutlineRequiredSize
)
1849 RtlFreeUnicodeString(&FamilyNameW
);
1850 RtlFreeUnicodeString(&FaceNameW
);
1851 RtlFreeUnicodeString(&StyleNameW
);
1852 RtlFreeUnicodeString(&FullNameW
);
1853 return Cache
->OutlineRequiredSize
;
1856 XScale
= Face
->size
->metrics
.x_scale
;
1857 YScale
= Face
->size
->metrics
.y_scale
;
1860 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1863 IntUnLockFreeType();
1864 DPRINT1("Can't find OS/2 table - not TT font?\n");
1865 RtlFreeUnicodeString(&FamilyNameW
);
1866 RtlFreeUnicodeString(&FaceNameW
);
1867 RtlFreeUnicodeString(&StyleNameW
);
1868 RtlFreeUnicodeString(&FullNameW
);
1872 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1875 IntUnLockFreeType();
1876 DPRINT1("Can't find HHEA table - not TT font?\n");
1877 RtlFreeUnicodeString(&FamilyNameW
);
1878 RtlFreeUnicodeString(&FaceNameW
);
1879 RtlFreeUnicodeString(&StyleNameW
);
1880 RtlFreeUnicodeString(&FullNameW
);
1884 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1886 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1888 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1890 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1893 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1894 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1895 Otm
->otmfsType
= pOS2
->fsType
;
1896 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1897 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1898 Otm
->otmItalicAngle
= 0; /* POST table */
1899 Otm
->otmEMSquare
= Face
->units_per_EM
;
1900 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1901 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1902 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1903 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1904 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1905 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1906 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1907 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1908 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1909 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1910 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1911 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1912 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1913 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1914 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1915 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1916 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1917 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1918 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1919 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1920 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1921 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1922 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1925 Otm
->otmsUnderscoreSize
= 0;
1926 Otm
->otmsUnderscorePosition
= 0;
1930 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1931 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1934 IntUnLockFreeType();
1936 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1939 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1940 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1941 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1944 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1945 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1946 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1949 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1950 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1951 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1953 /* unique name (full name) */
1954 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1955 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1956 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1958 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1960 RtlFreeUnicodeString(&FamilyNameW
);
1961 RtlFreeUnicodeString(&FaceNameW
);
1962 RtlFreeUnicodeString(&StyleNameW
);
1963 RtlFreeUnicodeString(&FullNameW
);
1965 return Cache
->OutlineRequiredSize
;
1968 static PFONTGDI FASTCALL
1969 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1972 PFONT_ENTRY CurrentEntry
;
1973 ANSI_STRING EntryFaceNameA
;
1974 UNICODE_STRING EntryFaceNameW
;
1978 Entry
= Head
->Flink
;
1979 while (Entry
!= Head
)
1981 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1983 FontGDI
= CurrentEntry
->Font
;
1986 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1987 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1988 if (!NT_SUCCESS(status
))
1993 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1995 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1996 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1999 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
2001 RtlFreeUnicodeString(&EntryFaceNameW
);
2005 RtlFreeUnicodeString(&EntryFaceNameW
);
2006 Entry
= Entry
->Flink
;
2012 static PFONTGDI FASTCALL
2013 FindFaceNameInLists(PUNICODE_STRING FaceName
)
2015 PPROCESSINFO Win32Process
;
2018 /* Search the process local list.
2019 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
2020 Win32Process
= PsGetCurrentProcessWin32Process();
2021 IntLockProcessPrivateFonts(Win32Process
);
2022 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
2023 IntUnLockProcessPrivateFonts(Win32Process
);
2029 /* Search the global list */
2030 IntLockGlobalFonts();
2031 Font
= FindFaceNameInList(FaceName
, &g_FontListHead
);
2032 IntUnLockGlobalFonts();
2037 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2039 CharSetFromLangID(LANGID LangID
)
2041 /* FIXME: Add more and fix if wrong */
2042 switch (PRIMARYLANGID(LangID
))
2045 switch (SUBLANGID(LangID
))
2047 case SUBLANG_CHINESE_TRADITIONAL
:
2048 return CHINESEBIG5_CHARSET
;
2049 case SUBLANG_CHINESE_SIMPLIFIED
:
2053 return GB2312_CHARSET
;
2055 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2056 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2057 return EASTEUROPE_CHARSET
;
2059 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2060 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2061 return RUSSIAN_CHARSET
;
2063 case LANG_ARABIC
: return ARABIC_CHARSET
;
2064 case LANG_GREEK
: return GREEK_CHARSET
;
2065 case LANG_HEBREW
: return HEBREW_CHARSET
;
2066 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2067 case LANG_KOREAN
: return JOHAB_CHARSET
;
2068 case LANG_TURKISH
: return TURKISH_CHARSET
;
2069 case LANG_THAI
: return THAI_CHARSET
;
2070 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2071 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2073 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2074 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2075 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2076 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2077 case LANG_SWEDISH
: default:
2078 return ANSI_CHARSET
;
2083 SwapEndian(LPVOID pvData
, DWORD Size
)
2085 BYTE b
, *pb
= pvData
;
2097 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2099 NTSTATUS Status
= STATUS_NO_MEMORY
;
2102 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2105 Tmp
.MaximumLength
= Source
->MaximumLength
;
2107 RtlCopyUnicodeString(&Tmp
, Source
);
2109 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2110 Destination
->Length
= Tmp
.Length
;
2111 Destination
->Buffer
= Tmp
.Buffer
;
2113 Status
= STATUS_SUCCESS
;
2120 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2121 FT_UShort NameID
, FT_UShort LangID
)
2124 INT i
, Count
, BestIndex
, Score
, BestScore
;
2126 NTSTATUS Status
= STATUS_NOT_FOUND
;
2127 ANSI_STRING AnsiName
;
2128 PSHARED_FACE_CACHE Cache
;
2129 FT_Face Face
= SharedFace
->Face
;
2131 RtlFreeUnicodeString(pNameW
);
2134 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2136 Cache
= &SharedFace
->EnglishUS
;
2140 Cache
= &SharedFace
->UserLanguage
;
2143 /* use cache if available */
2144 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2146 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2148 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2150 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2156 Count
= FT_Get_Sfnt_Name_Count(Face
);
2157 for (i
= 0; i
< Count
; ++i
)
2159 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2162 continue; /* failure */
2165 if (Name
.name_id
!= NameID
)
2167 continue; /* mismatched */
2170 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2171 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2172 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2174 continue; /* not Microsoft Unicode name */
2177 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2178 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2180 continue; /* invalid string */
2183 if (Name
.language_id
== LangID
)
2187 break; /* best match */
2189 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2193 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2202 if (Score
> BestScore
)
2211 /* store the best name */
2212 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2215 /* NOTE: Name.string is not null-terminated */
2217 Tmp
.Buffer
= (PWCH
)Name
.string
;
2218 Tmp
.Length
= Tmp
.MaximumLength
= Name
.string_len
;
2221 pNameW
->MaximumLength
= Name
.string_len
+ sizeof(WCHAR
);
2222 pNameW
->Buffer
= ExAllocatePoolWithTag(PagedPool
, pNameW
->MaximumLength
, TAG_USTR
);
2226 Status
= RtlAppendUnicodeStringToString(pNameW
, &Tmp
);
2227 if (Status
== STATUS_SUCCESS
)
2229 /* Convert UTF-16 big endian to little endian */
2230 SwapEndian(pNameW
->Buffer
, pNameW
->Length
);
2235 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2240 if (!NT_SUCCESS(Status
))
2243 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2245 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2246 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2250 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2251 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2255 if (NT_SUCCESS(Status
))
2258 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2260 ASSERT_FREETYPE_LOCK_NOT_HELD();
2262 if (!Cache
->FontFamily
.Buffer
)
2263 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2264 IntUnLockFreeType();
2266 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2268 ASSERT_FREETYPE_LOCK_NOT_HELD();
2270 if (!Cache
->FullName
.Buffer
)
2271 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2272 IntUnLockFreeType();
2279 static void FASTCALL
2280 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2281 LPCWSTR FullName
, PFONTGDI FontGDI
)
2284 UNICODE_STRING StyleW
;
2287 CHARSETINFO CharSetInfo
;
2289 OUTLINETEXTMETRICW
*Otm
;
2292 NEWTEXTMETRICW
*Ntm
;
2295 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2296 FT_Face Face
= SharedFace
->Face
;
2297 UNICODE_STRING NameW
;
2299 RtlInitUnicodeString(&NameW
, NULL
);
2300 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2301 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2302 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2307 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2310 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2314 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2315 TM
= &Otm
->otmTextMetrics
;
2317 Lf
->lfHeight
= TM
->tmHeight
;
2318 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2319 Lf
->lfWeight
= TM
->tmWeight
;
2320 Lf
->lfItalic
= TM
->tmItalic
;
2321 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2322 Lf
->lfCharSet
= TM
->tmCharSet
;
2323 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2324 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2325 Lf
->lfQuality
= PROOF_QUALITY
;
2327 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2328 Ntm
->tmHeight
= TM
->tmHeight
;
2329 Ntm
->tmAscent
= TM
->tmAscent
;
2330 Ntm
->tmDescent
= TM
->tmDescent
;
2331 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2332 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2333 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2334 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2335 Ntm
->tmWeight
= TM
->tmWeight
;
2336 Ntm
->tmOverhang
= TM
->tmOverhang
;
2337 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2338 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2339 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2340 Ntm
->tmLastChar
= TM
->tmLastChar
;
2341 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2342 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2343 Ntm
->tmItalic
= TM
->tmItalic
;
2344 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2345 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2346 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2347 Ntm
->tmCharSet
= TM
->tmCharSet
;
2348 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2350 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2352 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2354 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2355 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2356 Ntm
->ntmAvgWidth
= 0;
2358 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2359 ? TRUETYPE_FONTTYPE
: 0);
2361 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2362 Info
->FontType
|= RASTER_FONTTYPE
;
2367 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2369 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2373 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2375 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2376 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2379 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2381 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2382 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2383 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2384 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2385 if (!NT_SUCCESS(status
))
2389 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2392 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2396 IntUnLockFreeType();
2400 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2401 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2402 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2403 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2404 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2405 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2407 if (0 == pOS2
->version
)
2411 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2412 fs
.fsCsb
[0] |= FS_LATIN1
;
2414 fs
.fsCsb
[0] |= FS_SYMBOL
;
2416 IntUnLockFreeType();
2418 if (fs
.fsCsb
[0] == 0)
2420 /* Let's see if we can find any interesting cmaps */
2421 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2423 switch (Face
->charmaps
[i
]->encoding
)
2425 case FT_ENCODING_UNICODE
:
2426 case FT_ENCODING_APPLE_ROMAN
:
2427 fs
.fsCsb
[0] |= FS_LATIN1
;
2429 case FT_ENCODING_MS_SYMBOL
:
2430 fs
.fsCsb
[0] |= FS_SYMBOL
;
2438 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2441 if (fs
.fsCsb
[0] & fs0
)
2443 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2445 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2447 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2449 if (g_ElfScripts
[i
])
2450 wcscpy(Info
->EnumLogFontEx
.elfScript
, g_ElfScripts
[i
]);
2453 DPRINT1("Unknown elfscript for bit %u\n", i
);
2458 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2462 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2465 UNICODE_STRING InfoFaceName
;
2467 for (i
= 0; i
< InfoEntries
; i
++)
2469 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2470 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2479 static BOOLEAN FASTCALL
2480 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2481 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2483 UNICODE_STRING LogFontFaceName
;
2485 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2486 if (0 != LogFontFaceName
.Length
&&
2487 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2492 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2495 static BOOL FASTCALL
2496 FontFamilyFound(PFONTFAMILYINFO InfoEntry
,
2497 PFONTFAMILYINFO Info
, DWORD InfoCount
)
2499 LPLOGFONTW plf1
= &InfoEntry
->EnumLogFontEx
.elfLogFont
;
2500 LPWSTR pFullName1
= InfoEntry
->EnumLogFontEx
.elfFullName
;
2504 for (i
= 0; i
< InfoCount
; ++i
)
2506 LPLOGFONTW plf2
= &Info
[i
].EnumLogFontEx
.elfLogFont
;
2507 if (plf1
->lfCharSet
!= plf2
->lfCharSet
)
2510 pFullName2
= Info
[i
].EnumLogFontEx
.elfFullName
;
2511 if (_wcsicmp(pFullName1
, pFullName2
) != 0)
2519 static BOOLEAN FASTCALL
2520 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2521 PFONTFAMILYINFO Info
,
2527 PFONT_ENTRY CurrentEntry
;
2529 FONTFAMILYINFO InfoEntry
;
2530 DWORD Count
= *pCount
;
2532 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
2534 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2535 FontGDI
= CurrentEntry
->Font
;
2538 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&&
2539 LogFont
->lfCharSet
!= FontGDI
->CharSet
)
2544 if (LogFont
->lfFaceName
[0] == UNICODE_NULL
)
2546 if (Count
< MaxCount
)
2548 FontFamilyFillInfo(&Info
[Count
], NULL
, NULL
, FontGDI
);
2554 FontFamilyFillInfo(&InfoEntry
, NULL
, NULL
, FontGDI
);
2556 if (_wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0 &&
2557 _wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfFullName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0)
2562 if (!FontFamilyFound(&InfoEntry
, Info
, min(Count
, MaxCount
)))
2564 if (Count
< MaxCount
)
2566 RtlCopyMemory(&Info
[Count
], &InfoEntry
, sizeof(InfoEntry
));
2577 static BOOLEAN FASTCALL
2578 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2579 PFONTFAMILYINFO Info
,
2583 PLIST_ENTRY pEntry
, pHead
= &g_FontSubstListHead
;
2584 PFONTSUBST_ENTRY pCurrentEntry
;
2585 PUNICODE_STRING pFromW
;
2587 LOGFONTW lf
= *LogFont
;
2588 UNICODE_STRING NameW
;
2590 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2592 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2594 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2595 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2597 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2598 continue; /* mismatch */
2601 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2602 SubstituteFontRecurse(&lf
);
2604 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2605 FontGDI
= FindFaceNameInLists(&NameW
);
2606 if (FontGDI
== NULL
)
2608 continue; /* no real font */
2611 if (*pCount
< MaxCount
)
2613 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2623 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2627 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2628 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2629 lprs
->nLanguageID
= gusLanguageID
;
2632 EngSetLastError(ERROR_INVALID_PARAMETER
);
2642 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2643 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2644 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2645 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2648 FT_BitmapGlyph APIENTRY
2653 FT_Render_Mode RenderMode
,
2656 PLIST_ENTRY CurrentEntry
;
2657 PFONT_CACHE_ENTRY FontEntry
;
2659 ASSERT_FREETYPE_LOCK_HELD();
2661 CurrentEntry
= g_FontCacheListHead
.Flink
;
2662 while (CurrentEntry
!= &g_FontCacheListHead
)
2664 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2665 if ((FontEntry
->Face
== Face
) &&
2666 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2667 (FontEntry
->Height
== Height
) &&
2668 (FontEntry
->RenderMode
== RenderMode
) &&
2669 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2671 CurrentEntry
= CurrentEntry
->Flink
;
2674 if (CurrentEntry
== &g_FontCacheListHead
)
2679 RemoveEntryList(CurrentEntry
);
2680 InsertHeadList(&g_FontCacheListHead
, CurrentEntry
);
2681 return FontEntry
->BitmapGlyph
;
2685 FT_BitmapGlyph APIENTRY
2688 FT_GlyphSlot GlyphSlot
,
2689 FT_Render_Mode RenderMode
)
2693 FT_Bitmap AlignedBitmap
;
2694 FT_BitmapGlyph BitmapGlyph
;
2696 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2699 DPRINT1("Failure getting glyph.\n");
2703 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2706 FT_Done_Glyph(Glyph
);
2707 DPRINT1("Failure rendering glyph.\n");
2711 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2712 FT_Bitmap_New(&AlignedBitmap
);
2713 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2715 DPRINT1("Conversion failed\n");
2716 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2720 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2721 BitmapGlyph
->bitmap
= AlignedBitmap
;
2726 FT_BitmapGlyph APIENTRY
2732 FT_GlyphSlot GlyphSlot
,
2733 FT_Render_Mode RenderMode
)
2737 PFONT_CACHE_ENTRY NewEntry
;
2738 FT_Bitmap AlignedBitmap
;
2739 FT_BitmapGlyph BitmapGlyph
;
2741 ASSERT_FREETYPE_LOCK_HELD();
2743 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2746 DPRINT1("Failure caching glyph.\n");
2750 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2753 FT_Done_Glyph(GlyphCopy
);
2754 DPRINT1("Failure rendering glyph.\n");
2758 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2761 DPRINT1("Alloc failure caching glyph.\n");
2762 FT_Done_Glyph(GlyphCopy
);
2766 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2767 FT_Bitmap_New(&AlignedBitmap
);
2768 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2770 DPRINT1("Conversion failed\n");
2771 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2772 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2773 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2777 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2778 BitmapGlyph
->bitmap
= AlignedBitmap
;
2780 NewEntry
->GlyphIndex
= GlyphIndex
;
2781 NewEntry
->Face
= Face
;
2782 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2783 NewEntry
->Height
= Height
;
2784 NewEntry
->RenderMode
= RenderMode
;
2785 NewEntry
->mxWorldToDevice
= *pmx
;
2787 InsertHeadList(&g_FontCacheListHead
, &NewEntry
->ListEntry
);
2788 if (++g_FontCacheNumEntries
> MAX_FONT_CACHE
)
2790 NewEntry
= CONTAINING_RECORD(g_FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2791 RemoveCachedEntry(NewEntry
);
2798 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2800 pt
->x
.value
= vec
->x
>> 6;
2801 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2802 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2803 pt
->y
.value
= vec
->y
>> 6;
2804 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2805 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2809 This function builds an FT_Fixed from a float. It puts the integer part
2810 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2811 It fails if the integer part of the float number is greater than SHORT_MAX.
2813 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2816 unsigned short fract
= (f
- value
) * 0xFFFF;
2817 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2821 This function builds an FT_Fixed from a FIXED. It simply put f.value
2822 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2824 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2826 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2829 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2831 TTPOLYGONHEADER
*pph
;
2833 int needed
= 0, point
= 0, contour
, first_pt
;
2834 unsigned int pph_start
, cpfx
;
2837 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2839 /* Ignore contours containing one point */
2840 if (point
== outline
->contours
[contour
])
2847 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2851 pph
->dwType
= TT_POLYGON_TYPE
;
2852 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2854 needed
+= sizeof(*pph
);
2856 while (point
<= outline
->contours
[contour
])
2858 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2859 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2860 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2865 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2868 } while (point
<= outline
->contours
[contour
] &&
2869 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2870 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2871 /* At the end of a contour Windows adds the start point, but
2873 if (point
> outline
->contours
[contour
] &&
2874 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2877 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2880 else if (point
<= outline
->contours
[contour
] &&
2881 outline
->tags
[point
] & FT_Curve_Tag_On
)
2883 /* add closing pt for bezier */
2885 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2894 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2897 pph
->cb
= needed
- pph_start
;
2902 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2904 /* Convert the quadratic Beziers to cubic Beziers.
2905 The parametric eqn for a cubic Bezier is, from PLRM:
2906 r(t) = at^3 + bt^2 + ct + r0
2907 with the control points:
2912 A quadratic Bezier has the form:
2913 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2915 So equating powers of t leads to:
2916 r1 = 2/3 p1 + 1/3 p0
2917 r2 = 2/3 p1 + 1/3 p2
2918 and of course r0 = p0, r3 = p2
2920 int contour
, point
= 0, first_pt
;
2921 TTPOLYGONHEADER
*pph
;
2923 DWORD pph_start
, cpfx
, type
;
2924 FT_Vector cubic_control
[4];
2925 unsigned int needed
= 0;
2927 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2930 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2934 pph
->dwType
= TT_POLYGON_TYPE
;
2935 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2937 needed
+= sizeof(*pph
);
2939 while (point
<= outline
->contours
[contour
])
2941 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2942 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2943 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2947 if (type
== TT_PRIM_LINE
)
2950 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2956 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2959 /* FIXME: Possible optimization in endpoint calculation
2960 if there are two consecutive curves */
2961 cubic_control
[0] = outline
->points
[point
-1];
2962 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2964 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2965 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2966 cubic_control
[0].x
>>= 1;
2967 cubic_control
[0].y
>>= 1;
2969 if (point
+1 > outline
->contours
[contour
])
2970 cubic_control
[3] = outline
->points
[first_pt
];
2973 cubic_control
[3] = outline
->points
[point
+1];
2974 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2976 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2977 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2978 cubic_control
[3].x
>>= 1;
2979 cubic_control
[3].y
>>= 1;
2982 /* r1 = 1/3 p0 + 2/3 p1
2983 r2 = 1/3 p2 + 2/3 p1 */
2984 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2985 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2986 cubic_control
[2] = cubic_control
[1];
2987 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2988 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2989 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2990 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2993 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2994 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2995 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
3000 } while (point
<= outline
->contours
[contour
] &&
3001 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
3002 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
3003 /* At the end of a contour Windows adds the start point,
3004 but only for Beziers and we've already done that.
3006 if (point
<= outline
->contours
[contour
] &&
3007 outline
->tags
[point
] & FT_Curve_Tag_On
)
3009 /* This is the closing pt of a bezier, but we've already
3010 added it, so just inc point and carry on */
3018 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
3021 pph
->cb
= needed
- pph_start
;
3027 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG Width
, LONG Height
)
3029 FT_Size_RequestRec req
;
3030 FT_Face face
= FontGDI
->SharedFace
->Face
;
3041 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
3051 if (Width
> 0xFFFFU
)
3053 if (Height
> 0xFFFFU
)
3056 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3057 req
.width
= (FT_Long
)(Width
<< 6);
3058 req
.height
= (FT_Long
)(Height
<< 6);
3059 req
.horiResolution
= 0;
3060 req
.vertResolution
= 0;
3061 return FT_Request_Size(face
, &req
);
3066 TextIntUpdateSize(PDC dc
,
3073 FT_CharMap charmap
, found
;
3079 face
= FontGDI
->SharedFace
->Face
;
3080 if (face
->charmap
== NULL
)
3082 DPRINT("WARNING: No charmap selected!\n");
3083 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3086 for (n
= 0; n
< face
->num_charmaps
; n
++)
3088 charmap
= face
->charmaps
[n
];
3089 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
3090 if (charmap
->encoding
!= 0)
3098 DPRINT1("WARNING: Could not find desired charmap!\n");
3102 error
= FT_Set_Charmap(face
, found
);
3105 DPRINT1("WARNING: Could not set the charmap!\n");
3110 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3112 error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3115 IntUnLockFreeType();
3119 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3128 * Based on WineEngGetGlyphOutline
3133 ftGdiGetGlyphOutline(
3141 BOOL bIgnoreRotation
)
3143 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3151 FT_UInt glyph_index
;
3152 DWORD width
, height
, pitch
, needed
= 0;
3153 FT_Bitmap ft_bitmap
;
3155 INT left
, right
, top
= 0, bottom
= 0;
3157 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3158 FLOAT eM11
, widthRatio
= 1.0;
3159 FT_Matrix transMat
= identityMat
;
3160 BOOL needsTransform
= FALSE
;
3163 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3164 OUTLINETEXTMETRICW
*potm
;
3168 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3169 cjBuf
, pvBuf
, pmat2
);
3171 pdcattr
= dc
->pdcattr
;
3173 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3176 hFont
= pdcattr
->hlfntNew
;
3177 TextObj
= RealizeFontInit(hFont
);
3181 EngSetLastError(ERROR_INVALID_HANDLE
);
3184 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3185 ft_face
= FontGDI
->SharedFace
->Face
;
3187 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3188 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3189 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3191 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3192 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3195 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3196 TEXTOBJ_UnlockText(TextObj
);
3199 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3202 /* FIXME: last error? */
3203 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3204 TEXTOBJ_UnlockText(TextObj
);
3209 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3210 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3212 TEXTOBJ_UnlockText(TextObj
);
3214 if (iFormat
& GGO_GLYPH_INDEX
)
3217 iFormat
&= ~GGO_GLYPH_INDEX
;
3219 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3221 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3222 load_flags
|= FT_LOAD_NO_BITMAP
;
3224 if (iFormat
& GGO_UNHINTED
)
3226 load_flags
|= FT_LOAD_NO_HINTING
;
3227 iFormat
&= ~GGO_UNHINTED
;
3230 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3233 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3234 IntUnLockFreeType();
3235 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3238 IntUnLockFreeType();
3240 if (aveWidth
&& potm
)
3242 widthRatio
= (FLOAT
)aveWidth
* eM11
/
3243 (FLOAT
) potm
->otmTextMetrics
.tmAveCharWidth
;
3246 left
= (INT
)(ft_face
->glyph
->metrics
.horiBearingX
* widthRatio
) & -64;
3247 right
= (INT
)((ft_face
->glyph
->metrics
.horiBearingX
+
3248 ft_face
->glyph
->metrics
.width
) * widthRatio
+ 63) & -64;
3250 adv
= (INT
)((ft_face
->glyph
->metrics
.horiAdvance
* widthRatio
) + 63) >> 6;
3252 bbx
= (right
- left
) >> 6;
3254 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3258 /* Scaling transform */
3265 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3267 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3268 efTemp
= pmx
->efM11
;
3269 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3270 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
3272 efTemp
= pmx
->efM12
;
3273 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3274 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
3276 efTemp
= pmx
->efM21
;
3277 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3278 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
3280 efTemp
= pmx
->efM22
;
3281 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3282 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
3284 FT_Matrix_Multiply(&ftmatrix
, &transMat
);
3285 needsTransform
= TRUE
;
3288 /* Rotation transform */
3291 FT_Matrix rotationMat
;
3293 DPRINT("Rotation Trans!\n");
3294 angle
= FT_FixedFromFloat((float)orientation
/ 10.0);
3295 FT_Vector_Unit(&vecAngle
, angle
);
3296 rotationMat
.xx
= vecAngle
.x
;
3297 rotationMat
.xy
= -vecAngle
.y
;
3298 rotationMat
.yx
= -rotationMat
.xy
;
3299 rotationMat
.yy
= rotationMat
.xx
;
3300 FT_Matrix_Multiply(&rotationMat
, &transMat
);
3301 needsTransform
= TRUE
;
3304 /* Extra transformation specified by caller */
3308 DPRINT("MAT2 Matrix Trans!\n");
3309 extraMat
.xx
= FT_FixedFromFIXED(pmat2
->eM11
);
3310 extraMat
.xy
= FT_FixedFromFIXED(pmat2
->eM21
);
3311 extraMat
.yx
= FT_FixedFromFIXED(pmat2
->eM12
);
3312 extraMat
.yy
= FT_FixedFromFIXED(pmat2
->eM22
);
3313 FT_Matrix_Multiply(&extraMat
, &transMat
);
3314 needsTransform
= TRUE
;
3317 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
); /* It looks like we are finished with potm ATM. */
3319 if (!needsTransform
)
3321 DPRINT("No Need to be Transformed!\n");
3322 top
= (ft_face
->glyph
->metrics
.horiBearingY
+ 63) & -64;
3323 bottom
= (ft_face
->glyph
->metrics
.horiBearingY
-
3324 ft_face
->glyph
->metrics
.height
) & -64;
3325 gm
.gmCellIncX
= adv
;
3332 for (xc
= 0; xc
< 2; xc
++)
3334 for (yc
= 0; yc
< 2; yc
++)
3336 vec
.x
= (ft_face
->glyph
->metrics
.horiBearingX
+
3337 xc
* ft_face
->glyph
->metrics
.width
);
3338 vec
.y
= ft_face
->glyph
->metrics
.horiBearingY
-
3339 yc
* ft_face
->glyph
->metrics
.height
;
3340 DPRINT("Vec %ld,%ld\n", vec
.x
, vec
.y
);
3341 FT_Vector_Transform(&vec
, &transMat
);
3342 if (xc
== 0 && yc
== 0)
3344 left
= right
= vec
.x
;
3345 top
= bottom
= vec
.y
;
3349 if (vec
.x
< left
) left
= vec
.x
;
3350 else if (vec
.x
> right
) right
= vec
.x
;
3351 if (vec
.y
< bottom
) bottom
= vec
.y
;
3352 else if (vec
.y
> top
) top
= vec
.y
;
3357 right
= (right
+ 63) & -64;
3358 bottom
= bottom
& -64;
3359 top
= (top
+ 63) & -64;
3361 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left
, top
, right
, bottom
);
3362 vec
.x
= ft_face
->glyph
->metrics
.horiAdvance
;
3364 FT_Vector_Transform(&vec
, &transMat
);
3365 gm
.gmCellIncX
= (vec
.x
+63) >> 6;
3366 gm
.gmCellIncY
= -((vec
.y
+63) >> 6);
3368 gm
.gmBlackBoxX
= (right
- left
) >> 6;
3369 gm
.gmBlackBoxY
= (top
- bottom
) >> 6;
3370 gm
.gmptGlyphOrigin
.x
= left
>> 6;
3371 gm
.gmptGlyphOrigin
.y
= top
>> 6;
3373 DPRINT("CX %d CY %d BBX %u BBY %u GOX %d GOY %d\n",
3374 gm
.gmCellIncX
, gm
.gmCellIncY
,
3375 gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
3376 gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
3378 IntUnLockFreeType();
3381 if (iFormat
== GGO_METRICS
)
3383 DPRINT("GGO_METRICS Exit!\n");
3385 return 1; /* FIXME */
3388 if (ft_face
->glyph
->format
!= ft_glyph_format_outline
&& iFormat
!= GGO_BITMAP
)
3390 DPRINT1("Loaded a bitmap\n");
3397 width
= gm
.gmBlackBoxX
;
3398 height
= gm
.gmBlackBoxY
;
3399 pitch
= ((width
+ 31) >> 5) << 2;
3400 needed
= pitch
* height
;
3402 if (!pvBuf
|| !cjBuf
) break;
3403 if (!needed
) return GDI_ERROR
; /* empty glyph */
3407 switch (ft_face
->glyph
->format
)
3409 case ft_glyph_format_bitmap
:
3411 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3412 INT w
= min( pitch
, (ft_face
->glyph
->bitmap
.width
+ 7) >> 3 );
3413 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3416 RtlCopyMemory(dst
, src
, w
);
3417 src
+= ft_face
->glyph
->bitmap
.pitch
;
3423 case ft_glyph_format_outline
:
3424 ft_bitmap
.width
= width
;
3425 ft_bitmap
.rows
= height
;
3426 ft_bitmap
.pitch
= pitch
;
3427 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
3428 ft_bitmap
.buffer
= pvBuf
;
3433 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3435 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3436 /* Note: FreeType will only set 'black' bits for us. */
3437 RtlZeroMemory(pvBuf
, needed
);
3438 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3439 IntUnLockFreeType();
3443 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3448 case GGO_GRAY2_BITMAP
:
3449 case GGO_GRAY4_BITMAP
:
3450 case GGO_GRAY8_BITMAP
:
3452 unsigned int mult
, row
, col
;
3455 width
= gm
.gmBlackBoxX
;
3456 height
= gm
.gmBlackBoxY
;
3457 pitch
= (width
+ 3) / 4 * 4;
3458 needed
= pitch
* height
;
3460 if (!pvBuf
|| !cjBuf
) break;
3461 if (!needed
) return GDI_ERROR
; /* empty glyph */
3465 switch (ft_face
->glyph
->format
)
3467 case ft_glyph_format_bitmap
:
3469 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3470 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3474 for (x
= 0; (UINT
)x
< pitch
; x
++)
3476 if (x
< ft_face
->glyph
->bitmap
.width
)
3477 dst
[x
] = (src
[x
/ 8] & (1 << ( (7 - (x
% 8))))) ? 0xff : 0;
3481 src
+= ft_face
->glyph
->bitmap
.pitch
;
3486 case ft_glyph_format_outline
:
3488 ft_bitmap
.width
= width
;
3489 ft_bitmap
.rows
= height
;
3490 ft_bitmap
.pitch
= pitch
;
3491 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
3492 ft_bitmap
.buffer
= pvBuf
;
3497 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3499 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3500 RtlZeroMemory(ft_bitmap
.buffer
, cjBuf
);
3501 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3502 IntUnLockFreeType();
3504 if (iFormat
== GGO_GRAY2_BITMAP
)
3506 else if (iFormat
== GGO_GRAY4_BITMAP
)
3508 else if (iFormat
== GGO_GRAY8_BITMAP
)
3516 for (row
= 0; row
< height
; row
++)
3519 for (col
= 0; col
< width
; col
++, ptr
++)
3521 *ptr
= (((int)*ptr
) * mult
+ 128) / 256;
3529 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3536 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3538 if (cjBuf
== 0) pvBuf
= NULL
; /* This is okay, need cjBuf to allocate. */
3541 if (needsTransform
&& pvBuf
) FT_Outline_Transform(outline
, &transMat
);
3543 needed
= get_native_glyph_outline(outline
, cjBuf
, NULL
);
3545 if (!pvBuf
|| !cjBuf
)
3547 IntUnLockFreeType();
3552 IntUnLockFreeType();
3555 get_native_glyph_outline(outline
, cjBuf
, pvBuf
);
3556 IntUnLockFreeType();
3561 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3562 if (cjBuf
== 0) pvBuf
= NULL
;
3564 if (needsTransform
&& pvBuf
)
3567 FT_Outline_Transform(outline
, &transMat
);
3568 IntUnLockFreeType();
3570 needed
= get_bezier_glyph_outline(outline
, cjBuf
, NULL
);
3572 if (!pvBuf
|| !cjBuf
)
3577 get_bezier_glyph_outline(outline
, cjBuf
, pvBuf
);
3582 DPRINT1("Unsupported format %u\n", iFormat
);
3586 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed
);
3593 TextIntGetTextExtentPoint(PDC dc
,
3606 FT_BitmapGlyph realglyph
;
3607 INT error
, glyph_index
, i
, previous
;
3608 ULONGLONG TotalWidth
= 0;
3610 FT_Render_Mode RenderMode
;
3612 PMATRIX pmxWorldToDevice
;
3614 BOOL EmuBold
, EmuItalic
;
3615 LONG ascender
, descender
;
3617 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3619 face
= FontGDI
->SharedFace
->Face
;
3627 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3629 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3630 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
3631 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
3633 Render
= IntIsFontRenderingEnabled();
3635 RenderMode
= IntGetFontRenderMode(plf
);
3637 RenderMode
= FT_RENDER_MODE_MONO
;
3639 /* Get the DC's world-to-device transformation matrix */
3640 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
3641 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
3643 use_kerning
= FT_HAS_KERNING(face
);
3646 for (i
= 0; i
< Count
; i
++)
3648 if (fl
& GTEF_INDICES
)
3649 glyph_index
= *String
;
3651 glyph_index
= FT_Get_Char_Index(face
, *String
);
3653 if (EmuBold
|| EmuItalic
)
3656 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
3657 RenderMode
, pmxWorldToDevice
);
3659 if (EmuBold
|| EmuItalic
|| !realglyph
)
3661 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
3664 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
3668 glyph
= face
->glyph
;
3669 if (EmuBold
|| EmuItalic
)
3672 FT_GlyphSlot_Embolden(glyph
);
3674 FT_GlyphSlot_Oblique(glyph
);
3675 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
3679 realglyph
= ftGdiGlyphCacheSet(face
,
3689 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
3694 /* Retrieve kerning distance */
3695 if (use_kerning
&& previous
&& glyph_index
)
3698 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
3699 TotalWidth
+= delta
.x
;
3702 TotalWidth
+= realglyph
->root
.advance
.x
>> 10;
3704 if (((TotalWidth
+ 32) >> 6) <= MaxExtent
&& NULL
!= Fit
)
3710 Dx
[i
] = (TotalWidth
+ 32) >> 6;
3713 if (EmuBold
|| EmuItalic
)
3715 FT_Done_Glyph((FT_Glyph
)realglyph
);
3719 previous
= glyph_index
;
3722 ascender
= (face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
3723 descender
= (32 - face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
3724 IntUnLockFreeType();
3726 Size
->cx
= (TotalWidth
+ 32) >> 6;
3727 Size
->cy
= ascender
+ descender
;
3735 ftGdiGetTextCharsetInfo(