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 /* is it identical? */
457 if (RtlEqualUnicodeString(&FromW
, &ToW
, TRUE
) &&
458 CharSets
[FONTSUBST_FROM
] == CharSets
[FONTSUBST_TO
])
460 RtlFreeUnicodeString(&FromW
);
461 RtlFreeUnicodeString(&ToW
);
465 /* allocate an entry */
466 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
469 DPRINT("ExAllocatePoolWithTag failed\n");
470 RtlFreeUnicodeString(&FromW
);
471 RtlFreeUnicodeString(&ToW
);
475 /* store to *pEntry */
476 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
477 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
478 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
479 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
481 /* insert pEntry to *pHead */
482 InsertTailList(pHead
, &pEntry
->ListEntry
);
488 return NT_SUCCESS(Status
);
492 InitFontSupport(VOID
)
496 InitializeListHead(&g_FontListHead
);
497 InitializeListHead(&g_FontCacheListHead
);
498 g_FontCacheNumEntries
= 0;
499 /* Fast Mutexes must be allocated from non paged pool */
500 g_FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
501 if (g_FontListLock
== NULL
)
506 ExInitializeFastMutex(g_FontListLock
);
507 g_FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
508 if (g_FreeTypeLock
== NULL
)
512 ExInitializeFastMutex(g_FreeTypeLock
);
514 ulError
= FT_Init_FreeType(&g_FreeTypeLibrary
);
517 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
521 IntLoadSystemFonts();
522 IntLoadFontSubstList(&g_FontSubstListHead
);
528 FtSetCoordinateTransform(
535 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
537 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
538 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
541 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
542 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
545 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
546 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
549 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
550 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
552 /* Set the transformation matrix */
553 FT_Set_Transform(face
, &ftmatrix
, 0);
557 SubstituteFontByList(PLIST_ENTRY pHead
,
558 PUNICODE_STRING pOutputName
,
559 PUNICODE_STRING pInputName
,
560 BYTE RequestedCharSet
,
561 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
563 PLIST_ENTRY pListEntry
;
564 PFONTSUBST_ENTRY pSubstEntry
;
565 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
567 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
568 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
570 /* for each list entry */
571 for (pListEntry
= pHead
->Flink
;
573 pListEntry
= pListEntry
->Flink
)
576 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
578 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
580 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
581 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
583 continue; /* not matched */
586 /* does charset number exist? (to) */
587 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
589 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
593 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
596 /* does font name match? */
597 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
600 continue; /* not matched */
603 /* update *pOutputName */
604 *pOutputName
= pSubstEntry
->FontNames
[FONTSUBST_TO
];
606 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
608 /* update CharSetMap */
609 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
610 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
612 return TRUE
; /* success */
619 SubstituteFontRecurse(LOGFONTW
* pLogFont
)
621 UINT RecurseCount
= 5;
622 UNICODE_STRING OutputNameW
= { 0 };
623 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
625 UNICODE_STRING InputNameW
;
627 if (pLogFont
->lfFaceName
[0] == UNICODE_NULL
)
630 RtlInitUnicodeString(&InputNameW
, pLogFont
->lfFaceName
);
632 while (RecurseCount
-- > 0)
634 Found
= SubstituteFontByList(&g_FontSubstListHead
,
635 &OutputNameW
, &InputNameW
,
636 pLogFont
->lfCharSet
, CharSetMap
);
640 RtlStringCchCopyW(pLogFont
->lfFaceName
, LF_FACESIZE
, OutputNameW
.Buffer
);
642 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
643 CharSetMap
[FONTSUBST_FROM
] == pLogFont
->lfCharSet
)
645 pLogFont
->lfCharSet
= CharSetMap
[FONTSUBST_TO
];
649 return TRUE
; /* success */
655 * Search the system font directory and adds each font found.
658 IntLoadSystemFonts(VOID
)
660 OBJECT_ATTRIBUTES ObjectAttributes
;
661 UNICODE_STRING Directory
, FileName
, TempString
;
662 IO_STATUS_BLOCK Iosb
;
665 PFILE_DIRECTORY_INFORMATION DirInfo
;
666 BOOLEAN bRestartScan
= TRUE
;
669 static UNICODE_STRING SearchPatterns
[] =
671 RTL_CONSTANT_STRING(L
"*.ttf"),
672 RTL_CONSTANT_STRING(L
"*.ttc"),
673 RTL_CONSTANT_STRING(L
"*.otf"),
674 RTL_CONSTANT_STRING(L
"*.otc"),
675 RTL_CONSTANT_STRING(L
"*.fon"),
676 RTL_CONSTANT_STRING(L
"*.fnt")
679 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
681 InitializeObjectAttributes(
684 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
690 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
693 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
694 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
696 if (NT_SUCCESS(Status
))
698 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
700 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
701 if (DirInfoBuffer
== NULL
)
707 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
708 if (FileName
.Buffer
== NULL
)
710 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
715 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
719 Status
= ZwQueryDirectoryFile(
727 FileDirectoryInformation
,
732 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
737 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
740 TempString
.Buffer
= DirInfo
->FileName
;
742 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
743 RtlCopyUnicodeString(&FileName
, &Directory
);
744 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
745 IntGdiAddFontResource(&FileName
, 0);
746 if (DirInfo
->NextEntryOffset
== 0)
748 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
751 bRestartScan
= FALSE
;
754 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
755 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
762 ItalicFromStyle(const char *style_name
)
764 if (style_name
== NULL
|| style_name
[0] == 0)
766 if (strstr(style_name
, "Italic") != NULL
)
768 if (strstr(style_name
, "Oblique") != NULL
)
774 WeightFromStyle(const char *style_name
)
776 if (style_name
== NULL
|| style_name
[0] == 0)
778 if (strstr(style_name
, "Regular") != NULL
)
780 if (strstr(style_name
, "Normal") != NULL
)
782 if (strstr(style_name
, "SemiBold") != NULL
)
784 if (strstr(style_name
, "UltraBold") != NULL
)
786 if (strstr(style_name
, "DemiBold") != NULL
)
788 if (strstr(style_name
, "ExtraBold") != NULL
)
790 if (strstr(style_name
, "Bold") != NULL
)
792 if (strstr(style_name
, "UltraLight") != NULL
)
793 return FW_ULTRALIGHT
;
794 if (strstr(style_name
, "ExtraLight") != NULL
)
795 return FW_EXTRALIGHT
;
796 if (strstr(style_name
, "Light") != NULL
)
798 if (strstr(style_name
, "Hairline") != NULL
)
800 if (strstr(style_name
, "Book") != NULL
)
802 if (strstr(style_name
, "ExtraBlack") != NULL
)
804 if (strstr(style_name
, "UltraBlack") != NULL
)
806 if (strstr(style_name
, "Black") != NULL
)
808 if (strstr(style_name
, "Medium") != NULL
)
810 if (strstr(style_name
, "Thin") != NULL
)
812 if (strstr(style_name
, "Heavy") != NULL
)
818 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
);
821 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
822 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
826 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
830 ANSI_STRING AnsiFaceName
;
831 FT_WinFNT_HeaderRec WinFNT
;
832 INT FaceCount
= 0, CharSetCount
= 0;
833 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
834 DWORD Characteristics
= pLoadFont
->Characteristics
;
835 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
838 FT_UShort os2_version
;
839 FT_ULong os2_ulCodePageRange1
;
840 FT_UShort os2_usWeightClass
;
842 if (SharedFace
== NULL
&& CharSetIndex
== -1)
844 /* load a face from memory */
846 Error
= FT_New_Memory_Face(
848 pLoadFont
->Memory
->Buffer
,
849 pLoadFont
->Memory
->BufferSize
,
850 ((FontIndex
!= -1) ? FontIndex
: 0),
854 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
858 if (!Error
&& FT_IS_SFNT(Face
))
859 pLoadFont
->IsTrueType
= TRUE
;
861 if (Error
|| SharedFace
== NULL
)
864 SharedFace_Release(SharedFace
);
866 if (Error
== FT_Err_Unknown_File_Format
)
867 DPRINT1("Unknown font file format\n");
869 DPRINT1("Error reading font (error code: %d)\n", Error
);
870 return 0; /* failure */
875 Face
= SharedFace
->Face
;
877 SharedFace_AddRef(SharedFace
);
881 /* allocate a FONT_ENTRY */
882 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
885 SharedFace_Release(SharedFace
);
886 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
887 return 0; /* failure */
890 /* allocate a FONTGDI */
891 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
894 SharedFace_Release(SharedFace
);
895 ExFreePoolWithTag(Entry
, TAG_FONT
);
896 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
897 return 0; /* failure */
903 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
904 pFileName
->Length
+ sizeof(UNICODE_NULL
),
906 if (FontGDI
->Filename
== NULL
)
909 SharedFace_Release(SharedFace
);
910 ExFreePoolWithTag(Entry
, TAG_FONT
);
911 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
912 return 0; /* failure */
914 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
915 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
919 FontGDI
->Filename
= NULL
;
921 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
924 if (FontGDI
->Filename
)
925 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
927 SharedFace_Release(SharedFace
);
928 ExFreePoolWithTag(Entry
, TAG_FONT
);
932 PrivateEntry
->Entry
= Entry
;
933 if (pLoadFont
->PrivateEntry
)
935 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
939 InitializeListHead(&PrivateEntry
->ListEntry
);
940 pLoadFont
->PrivateEntry
= PrivateEntry
;
945 FontGDI
->SharedFace
= SharedFace
;
946 FontGDI
->CharSet
= ANSI_CHARSET
;
947 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
948 FontGDI
->RequestItalic
= FALSE
;
949 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
950 FontGDI
->RequestWeight
= FW_NORMAL
;
952 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
953 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
954 if (!NT_SUCCESS(Status
))
958 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
960 pLoadFont
->PrivateEntry
= NULL
;
964 RemoveEntryList(&PrivateEntry
->ListEntry
);
966 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
968 if (FontGDI
->Filename
)
969 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
971 SharedFace_Release(SharedFace
);
972 ExFreePoolWithTag(Entry
, TAG_FONT
);
978 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
981 os2_version
= pOS2
->version
;
982 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
983 os2_usWeightClass
= pOS2
->usWeightClass
;
987 if (pOS2
&& os2_version
>= 1)
989 /* get charset and weight from OS/2 header */
991 /* Make sure we do not use this pointer anymore */
994 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
996 if (os2_ulCodePageRange1
& (1 << BitIndex
))
998 if (g_FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
1001 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
1002 CharSetIndex
== CharSetCount
)
1004 FontGDI
->CharSet
= g_FontTci
[BitIndex
].ciCharset
;
1011 /* set actual weight */
1012 FontGDI
->OriginalWeight
= os2_usWeightClass
;
1016 /* get charset from WinFNT header */
1018 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
1021 FontGDI
->CharSet
= WinFNT
.charset
;
1023 IntUnLockFreeType();
1026 /* FIXME: CharSet is invalid on Marlett */
1027 if (RtlEqualUnicodeString(&Entry
->FaceName
, &g_MarlettW
, TRUE
))
1029 FontGDI
->CharSet
= SYMBOL_CHARSET
;
1033 DPRINT("Font loaded: %s (%s)\n",
1034 Face
->family_name
? Face
->family_name
: "<NULL>",
1035 Face
->style_name
? Face
->style_name
: "<NULL>");
1036 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
1037 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
1040 IntRequestFontSize(NULL
, FontGDI
, 0, 0);
1041 IntUnLockFreeType();
1043 /* Add this font resource to the font table */
1044 Entry
->Font
= FontGDI
;
1045 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1047 if (Characteristics
& FR_PRIVATE
)
1050 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1051 IntLockProcessPrivateFonts(Win32Process
);
1052 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1053 IntUnLockProcessPrivateFonts(Win32Process
);
1058 IntLockGlobalFonts();
1059 InsertTailList(&g_FontListHead
, &Entry
->ListEntry
);
1060 IntUnLockGlobalFonts();
1063 if (FontIndex
== -1)
1065 if (FT_IS_SFNT(Face
))
1067 TT_Face TrueType
= (TT_Face
)Face
;
1068 if (TrueType
->ttc_header
.count
> 1)
1071 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1073 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1080 if (CharSetIndex
== -1)
1084 if (pLoadFont
->RegValueName
.Length
== 0)
1086 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1090 UNICODE_STRING NewString
;
1091 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1092 NewString
.Length
= 0;
1093 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1094 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1095 NewString
.MaximumLength
,
1097 NewString
.Buffer
[0] = UNICODE_NULL
;
1099 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1100 RtlAppendUnicodeToString(&NewString
, L
" & ");
1101 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1103 RtlFreeUnicodeString(pValueName
);
1104 *pValueName
= NewString
;
1107 for (i
= 1; i
< CharSetCount
; ++i
)
1109 /* Do not count charsets towards 'faces' loaded */
1110 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1114 return FaceCount
; /* number of loaded faces */
1118 * IntGdiAddFontResource
1120 * Adds the font resource from the specified file to the system.
1124 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1128 PVOID Buffer
= NULL
;
1129 IO_STATUS_BLOCK Iosb
;
1130 PVOID SectionObject
;
1131 SIZE_T ViewSize
= 0;
1132 LARGE_INTEGER SectionSize
;
1133 OBJECT_ATTRIBUTES ObjectAttributes
;
1134 GDI_LOAD_FONT LoadFont
;
1137 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1139 /* Open the font file */
1140 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1141 Status
= ZwOpenFile(
1143 FILE_GENERIC_READ
| SYNCHRONIZE
,
1147 FILE_SYNCHRONOUS_IO_NONALERT
);
1148 if (!NT_SUCCESS(Status
))
1150 DPRINT("Could not load font file: %wZ\n", FileName
);
1154 SectionSize
.QuadPart
= 0LL;
1155 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1156 NULL
, &SectionSize
, PAGE_READONLY
,
1157 SEC_COMMIT
, FileHandle
, NULL
);
1158 if (!NT_SUCCESS(Status
))
1160 DPRINT("Could not map file: %wZ\n", FileName
);
1161 ZwClose(FileHandle
);
1164 ZwClose(FileHandle
);
1166 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1167 if (!NT_SUCCESS(Status
))
1169 DPRINT("Could not map file: %wZ\n", FileName
);
1170 ObDereferenceObject(SectionObject
);
1174 LoadFont
.pFileName
= FileName
;
1175 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1176 LoadFont
.Characteristics
= Characteristics
;
1177 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1178 LoadFont
.IsTrueType
= FALSE
;
1179 LoadFont
.PrivateEntry
= NULL
;
1180 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1182 ObDereferenceObject(SectionObject
);
1184 /* Release our copy */
1186 SharedMem_Release(LoadFont
.Memory
);
1187 IntUnLockFreeType();
1191 if (LoadFont
.IsTrueType
)
1193 /* append " (TrueType)" */
1194 UNICODE_STRING NewString
;
1197 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1198 NewString
.Length
= 0;
1199 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1200 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1201 NewString
.MaximumLength
,
1203 NewString
.Buffer
[0] = UNICODE_NULL
;
1205 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1206 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1207 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1208 LoadFont
.RegValueName
= NewString
;
1212 InitializeObjectAttributes(&ObjectAttributes
, &g_FontRegPath
,
1213 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1215 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1216 if (NT_SUCCESS(Status
))
1219 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1223 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1224 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1225 pFileName
, DataSize
);
1230 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1236 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1238 GDI_LOAD_FONT LoadFont
;
1239 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1243 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1250 memcpy(BufferCopy
, Buffer
, dwSize
);
1252 LoadFont
.pFileName
= NULL
;
1253 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1254 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1255 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1256 LoadFont
.IsTrueType
= FALSE
;
1257 LoadFont
.PrivateEntry
= NULL
;
1258 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1260 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1262 /* Release our copy */
1264 SharedMem_Release(LoadFont
.Memory
);
1265 IntUnLockFreeType();
1269 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1270 if (EntryCollection
)
1272 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1273 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1274 IntLockProcessPrivateFonts(Win32Process
);
1275 EntryCollection
->Handle
= ULongToHandle(++Win32Process
->PrivateMemFontHandleCount
);
1276 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1277 IntUnLockProcessPrivateFonts(Win32Process
);
1278 Ret
= EntryCollection
->Handle
;
1281 *pNumAdded
= FaceCount
;
1286 // FIXME: Add RemoveFontResource
1288 static VOID FASTCALL
1289 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1291 PFONTGDI FontGDI
= FontEntry
->Font
;
1292 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1294 if (FontGDI
->Filename
)
1295 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1297 EngFreeMem(FontGDI
);
1298 SharedFace_Release(SharedFace
);
1299 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1303 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1306 PFONT_ENTRY_MEM FontEntry
;
1308 while (!IsListEmpty(&Head
->ListEntry
))
1310 Entry
= RemoveHeadList(&Head
->ListEntry
);
1311 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1313 CleanupFontEntry(FontEntry
->Entry
);
1314 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1317 CleanupFontEntry(Head
->Entry
);
1318 ExFreePoolWithTag(Head
, TAG_FONT
);
1321 static VOID FASTCALL
1322 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1324 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1325 PLIST_ENTRY ListEntry
;
1326 RemoveEntryList(&Collection
->ListEntry
);
1329 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1330 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1332 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1333 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1335 } while (FontMemEntry
!= Collection
->Entry
);
1339 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1342 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1343 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1344 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1346 IntLockProcessPrivateFonts(Win32Process
);
1347 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1348 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1350 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1352 if (CurrentEntry
->Handle
== hMMFont
)
1354 EntryCollection
= CurrentEntry
;
1355 UnlinkFontMemCollection(CurrentEntry
);
1359 Entry
= Entry
->Flink
;
1361 IntUnLockProcessPrivateFonts(Win32Process
);
1363 if (EntryCollection
)
1365 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1366 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1374 IntGdiCleanupPrivateFontsForProcess(VOID
)
1376 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1378 PFONT_ENTRY_COLL_MEM EntryCollection
;
1380 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1383 EntryCollection
= NULL
;
1385 IntLockProcessPrivateFonts(Win32Process
);
1386 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1388 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1389 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1390 UnlinkFontMemCollection(EntryCollection
);
1392 IntUnLockProcessPrivateFonts(Win32Process
);
1394 if (EntryCollection
)
1396 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1397 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1401 /* No Mem fonts anymore, see if we have any other private fonts left */
1403 IntLockProcessPrivateFonts(Win32Process
);
1404 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1406 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1408 IntUnLockProcessPrivateFonts(Win32Process
);
1412 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1420 IntIsFontRenderingEnabled(VOID
)
1422 BOOL Ret
= g_RenderingEnabled
;
1425 hDC
= IntGetScreenDC();
1427 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && g_RenderingEnabled
;
1433 IntEnableFontRendering(BOOL Enable
)
1435 g_RenderingEnabled
= Enable
;
1438 FT_Render_Mode FASTCALL
1439 IntGetFontRenderMode(LOGFONTW
*logfont
)
1441 switch (logfont
->lfQuality
)
1443 case ANTIALIASED_QUALITY
:
1445 case NONANTIALIASED_QUALITY
:
1446 return FT_RENDER_MODE_MONO
;
1448 return FT_RENDER_MODE_LIGHT
;
1449 /* case CLEARTYPE_QUALITY:
1450 return FT_RENDER_MODE_LCD; */
1452 return FT_RENDER_MODE_NORMAL
;
1457 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1462 plfont
= LFONT_AllocFontWithHandle();
1465 return STATUS_NO_MEMORY
;
1468 ExInitializePushLock(&plfont
->lock
);
1469 *NewFont
= plfont
->BaseObject
.hHmgr
;
1470 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1471 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1472 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1474 /* This should really depend on whether GM_ADVANCED is set */
1475 plf
->lfOrientation
= plf
->lfEscapement
;
1477 LFONT_UnlockFont(plfont
);
1479 return STATUS_SUCCESS
;
1482 /*************************************************************************
1483 * TranslateCharsetInfo
1485 * Fills a CHARSETINFO structure for a character set, code page, or
1486 * font. This allows making the correspondance between different labelings
1487 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1488 * of the same encoding.
1490 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1491 * only one codepage should be set in *Src.
1494 * TRUE on success, FALSE on failure.
1497 static BOOLEAN APIENTRY
1498 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1499 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1500 if flags == TCI_SRCCHARSET: a character set value
1501 if flags == TCI_SRCCODEPAGE: a code page value */
1502 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1503 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1509 case TCI_SRCFONTSIG
:
1510 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1515 case TCI_SRCCODEPAGE
:
1516 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciACP
)
1521 case TCI_SRCCHARSET
:
1522 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciCharset
)
1531 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== g_FontTci
[Index
].ciCharset
)
1536 RtlCopyMemory(Cs
, &g_FontTci
[Index
], sizeof(CHARSETINFO
));
1542 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1546 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1548 if (ft_face
->charmaps
[i
]->platform_id
== TT_PLATFORM_MICROSOFT
&&
1549 ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1557 static void FASTCALL
1558 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1559 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1560 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1562 FT_Fixed XScale
, YScale
;
1563 int Ascent
, Descent
;
1564 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1566 XScale
= Face
->size
->metrics
.x_scale
;
1567 YScale
= Face
->size
->metrics
.y_scale
;
1571 TM
->tmHeight
= pFNT
->pixel_height
;
1572 TM
->tmAscent
= pFNT
->ascent
;
1573 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1574 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1575 TM
->tmExternalLeading
= pFNT
->external_leading
;
1576 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1577 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1579 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1580 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1581 TM
->tmFirstChar
= pFNT
->first_char
;
1582 TM
->tmLastChar
= pFNT
->last_char
;
1583 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1584 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1585 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1588 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1589 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1590 TM
->tmUnderlined
= pFNT
->underline
;
1591 TM
->tmStruckOut
= pFNT
->strike_out
;
1592 TM
->tmCharSet
= pFNT
->charset
;
1596 TM
->tmWeight
= FontGDI
->RequestWeight
;
1597 TM
->tmItalic
= FontGDI
->RequestItalic
;
1598 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1599 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1600 TM
->tmCharSet
= FontGDI
->CharSet
;
1605 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1607 Ascent
= pHori
->Ascender
;
1608 Descent
= -pHori
->Descender
;
1612 Ascent
= pOS2
->usWinAscent
;
1613 Descent
= pOS2
->usWinDescent
;
1616 if (FontGDI
->Magic
!= FONTGDI_MAGIC
)
1618 IntRequestFontSize(NULL
, FontGDI
, 0, 0);
1620 TM
->tmAscent
= FontGDI
->tmAscent
;
1621 TM
->tmDescent
= FontGDI
->tmDescent
;
1622 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1623 TM
->tmInternalLeading
= FontGDI
->tmInternalLeading
;
1626 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1628 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1629 - ((Ascent
+ Descent
)
1630 - (pHori
->Ascender
- pHori
->Descender
)),
1631 YScale
) + 32) >> 6);
1633 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1634 if (TM
->tmAveCharWidth
== 0)
1636 TM
->tmAveCharWidth
= 1;
1639 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1640 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1644 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1648 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1649 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1651 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1655 TM
->tmWeight
= FontGDI
->RequestWeight
;
1660 TM
->tmDigitizedAspectX
= 96;
1661 TM
->tmDigitizedAspectY
= 96;
1662 if (face_has_symbol_charmap(Face
) ||
1663 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1665 USHORT cpOEM
, cpAnsi
;
1667 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1668 TM
->tmFirstChar
= 0;
1671 case 1257: /* Baltic */
1672 TM
->tmLastChar
= 0xf8fd;
1675 TM
->tmLastChar
= 0xf0ff;
1677 TM
->tmBreakChar
= 0x20;
1678 TM
->tmDefaultChar
= 0x1f;
1682 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1683 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1685 if(pOS2
->usFirstCharIndex
<= 1)
1686 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1687 else if (pOS2
->usFirstCharIndex
> 0xff)
1688 TM
->tmBreakChar
= 0x20;
1690 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1691 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1696 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1697 TM
->tmUnderlined
= FALSE
;
1698 TM
->tmStruckOut
= FALSE
;
1702 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1704 TM
->tmItalic
= 0xFF;
1710 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1711 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1714 if (!FT_IS_FIXED_WIDTH(Face
))
1716 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1718 case PAN_PROP_MONOSPACED
:
1719 TM
->tmPitchAndFamily
= 0;
1722 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1728 TM
->tmPitchAndFamily
= 0;
1731 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1733 case PAN_FAMILY_SCRIPT
:
1734 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1736 case PAN_FAMILY_DECORATIVE
:
1737 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1742 case PAN_FAMILY_TEXT_DISPLAY
:
1743 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1744 /* Which is clearly not what the panose spec says. */
1745 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1747 TM
->tmPitchAndFamily
= FF_MODERN
;
1751 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1756 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1759 case PAN_SERIF_COVE
:
1760 case PAN_SERIF_OBTUSE_COVE
:
1761 case PAN_SERIF_SQUARE_COVE
:
1762 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1763 case PAN_SERIF_SQUARE
:
1764 case PAN_SERIF_THIN
:
1765 case PAN_SERIF_BONE
:
1766 case PAN_SERIF_EXAGGERATED
:
1767 case PAN_SERIF_TRIANGLE
:
1768 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1771 case PAN_SERIF_NORMAL_SANS
:
1772 case PAN_SERIF_OBTUSE_SANS
:
1773 case PAN_SERIF_PERP_SANS
:
1774 case PAN_SERIF_FLARED
:
1775 case PAN_SERIF_ROUNDED
:
1776 TM
->tmPitchAndFamily
|= FF_SWISS
;
1782 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1785 if (FT_IS_SCALABLE(Face
))
1787 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1789 if (FT_IS_SFNT(Face
))
1791 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1794 TM
->tmCharSet
= FontGDI
->CharSet
;
1797 static void FASTCALL
1798 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1799 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1800 FT_WinFNT_HeaderRec
*pFNT
)
1802 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1806 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1807 FT_UShort NameID
, FT_UShort LangID
);
1809 /*************************************************************
1810 * IntGetOutlineTextMetrics
1814 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1816 OUTLINETEXTMETRICW
*Otm
)
1819 TT_HoriHeader
*pHori
;
1820 TT_Postscript
*pPost
;
1821 FT_Fixed XScale
, YScale
;
1822 FT_WinFNT_HeaderRec Win
;
1825 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1826 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1827 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1828 FT_Face Face
= SharedFace
->Face
;
1830 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1832 return Cache
->OutlineRequiredSize
;
1836 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1837 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1840 RtlInitUnicodeString(&FaceNameW
, NULL
);
1841 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1844 RtlInitUnicodeString(&StyleNameW
, NULL
);
1845 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1847 /* unique name (full name) */
1848 RtlInitUnicodeString(&FullNameW
, NULL
);
1849 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1851 if (!Cache
->OutlineRequiredSize
)
1854 Needed
= sizeof(OUTLINETEXTMETRICW
);
1855 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1856 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1857 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1858 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1860 Cache
->OutlineRequiredSize
= Needed
;
1863 if (Size
< Cache
->OutlineRequiredSize
)
1865 RtlFreeUnicodeString(&FamilyNameW
);
1866 RtlFreeUnicodeString(&FaceNameW
);
1867 RtlFreeUnicodeString(&StyleNameW
);
1868 RtlFreeUnicodeString(&FullNameW
);
1869 return Cache
->OutlineRequiredSize
;
1872 XScale
= Face
->size
->metrics
.x_scale
;
1873 YScale
= Face
->size
->metrics
.y_scale
;
1876 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1879 IntUnLockFreeType();
1880 DPRINT1("Can't find OS/2 table - not TT font?\n");
1881 RtlFreeUnicodeString(&FamilyNameW
);
1882 RtlFreeUnicodeString(&FaceNameW
);
1883 RtlFreeUnicodeString(&StyleNameW
);
1884 RtlFreeUnicodeString(&FullNameW
);
1888 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1891 IntUnLockFreeType();
1892 DPRINT1("Can't find HHEA table - not TT font?\n");
1893 RtlFreeUnicodeString(&FamilyNameW
);
1894 RtlFreeUnicodeString(&FaceNameW
);
1895 RtlFreeUnicodeString(&StyleNameW
);
1896 RtlFreeUnicodeString(&FullNameW
);
1900 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1902 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1904 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1906 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1909 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1910 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1911 Otm
->otmfsType
= pOS2
->fsType
;
1912 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1913 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1914 Otm
->otmItalicAngle
= 0; /* POST table */
1915 Otm
->otmEMSquare
= Face
->units_per_EM
;
1916 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1917 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1918 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1919 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1920 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1921 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1922 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1923 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1924 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1925 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1926 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1927 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1928 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1929 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1930 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1931 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1932 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1933 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1934 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1935 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1936 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1937 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1938 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1941 Otm
->otmsUnderscoreSize
= 0;
1942 Otm
->otmsUnderscorePosition
= 0;
1946 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1947 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1950 IntUnLockFreeType();
1952 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1955 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1956 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1957 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1960 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1961 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1962 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1965 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1966 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1967 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1969 /* unique name (full name) */
1970 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1971 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1972 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1974 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1976 RtlFreeUnicodeString(&FamilyNameW
);
1977 RtlFreeUnicodeString(&FaceNameW
);
1978 RtlFreeUnicodeString(&StyleNameW
);
1979 RtlFreeUnicodeString(&FullNameW
);
1981 return Cache
->OutlineRequiredSize
;
1984 static PFONTGDI FASTCALL
1985 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1988 PFONT_ENTRY CurrentEntry
;
1989 ANSI_STRING EntryFaceNameA
;
1990 UNICODE_STRING EntryFaceNameW
;
1994 Entry
= Head
->Flink
;
1995 while (Entry
!= Head
)
1997 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1999 FontGDI
= CurrentEntry
->Font
;
2002 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
2003 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
2004 if (!NT_SUCCESS(status
))
2009 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
2011 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2012 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2015 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
2017 RtlFreeUnicodeString(&EntryFaceNameW
);
2021 RtlFreeUnicodeString(&EntryFaceNameW
);
2022 Entry
= Entry
->Flink
;
2028 static PFONTGDI FASTCALL
2029 FindFaceNameInLists(PUNICODE_STRING FaceName
)
2031 PPROCESSINFO Win32Process
;
2034 /* Search the process local list.
2035 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
2036 Win32Process
= PsGetCurrentProcessWin32Process();
2037 IntLockProcessPrivateFonts(Win32Process
);
2038 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
2039 IntUnLockProcessPrivateFonts(Win32Process
);
2045 /* Search the global list */
2046 IntLockGlobalFonts();
2047 Font
= FindFaceNameInList(FaceName
, &g_FontListHead
);
2048 IntUnLockGlobalFonts();
2053 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2055 CharSetFromLangID(LANGID LangID
)
2057 /* FIXME: Add more and fix if wrong */
2058 switch (PRIMARYLANGID(LangID
))
2061 switch (SUBLANGID(LangID
))
2063 case SUBLANG_CHINESE_TRADITIONAL
:
2064 return CHINESEBIG5_CHARSET
;
2065 case SUBLANG_CHINESE_SIMPLIFIED
:
2069 return GB2312_CHARSET
;
2071 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2072 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2073 return EASTEUROPE_CHARSET
;
2075 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2076 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2077 return RUSSIAN_CHARSET
;
2079 case LANG_ARABIC
: return ARABIC_CHARSET
;
2080 case LANG_GREEK
: return GREEK_CHARSET
;
2081 case LANG_HEBREW
: return HEBREW_CHARSET
;
2082 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2083 case LANG_KOREAN
: return JOHAB_CHARSET
;
2084 case LANG_TURKISH
: return TURKISH_CHARSET
;
2085 case LANG_THAI
: return THAI_CHARSET
;
2086 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2087 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2089 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2090 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2091 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2092 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2093 case LANG_SWEDISH
: default:
2094 return ANSI_CHARSET
;
2099 SwapEndian(LPVOID pvData
, DWORD Size
)
2101 BYTE b
, *pb
= pvData
;
2113 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2115 NTSTATUS Status
= STATUS_NO_MEMORY
;
2118 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2121 Tmp
.MaximumLength
= Source
->MaximumLength
;
2123 RtlCopyUnicodeString(&Tmp
, Source
);
2125 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2126 Destination
->Length
= Tmp
.Length
;
2127 Destination
->Buffer
= Tmp
.Buffer
;
2129 Status
= STATUS_SUCCESS
;
2136 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2137 FT_UShort NameID
, FT_UShort LangID
)
2140 INT i
, Count
, BestIndex
, Score
, BestScore
;
2142 NTSTATUS Status
= STATUS_NOT_FOUND
;
2143 ANSI_STRING AnsiName
;
2144 PSHARED_FACE_CACHE Cache
;
2145 FT_Face Face
= SharedFace
->Face
;
2147 RtlFreeUnicodeString(pNameW
);
2150 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2152 Cache
= &SharedFace
->EnglishUS
;
2156 Cache
= &SharedFace
->UserLanguage
;
2159 /* use cache if available */
2160 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2162 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2164 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2166 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2172 Count
= FT_Get_Sfnt_Name_Count(Face
);
2173 for (i
= 0; i
< Count
; ++i
)
2175 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2178 continue; /* failure */
2181 if (Name
.name_id
!= NameID
)
2183 continue; /* mismatched */
2186 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2187 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2188 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2190 continue; /* not Microsoft Unicode name */
2193 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2194 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2196 continue; /* invalid string */
2199 if (Name
.language_id
== LangID
)
2203 break; /* best match */
2205 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2209 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2218 if (Score
> BestScore
)
2227 /* store the best name */
2228 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2231 /* NOTE: Name.string is not null-terminated */
2233 Tmp
.Buffer
= (PWCH
)Name
.string
;
2234 Tmp
.Length
= Tmp
.MaximumLength
= Name
.string_len
;
2237 pNameW
->MaximumLength
= Name
.string_len
+ sizeof(WCHAR
);
2238 pNameW
->Buffer
= ExAllocatePoolWithTag(PagedPool
, pNameW
->MaximumLength
, TAG_USTR
);
2242 Status
= RtlAppendUnicodeStringToString(pNameW
, &Tmp
);
2243 if (Status
== STATUS_SUCCESS
)
2245 /* Convert UTF-16 big endian to little endian */
2246 SwapEndian(pNameW
->Buffer
, pNameW
->Length
);
2251 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2256 if (!NT_SUCCESS(Status
))
2259 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2261 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2262 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2266 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2267 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2271 if (NT_SUCCESS(Status
))
2274 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2276 ASSERT_FREETYPE_LOCK_NOT_HELD();
2278 if (!Cache
->FontFamily
.Buffer
)
2279 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2280 IntUnLockFreeType();
2282 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2284 ASSERT_FREETYPE_LOCK_NOT_HELD();
2286 if (!Cache
->FullName
.Buffer
)
2287 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2288 IntUnLockFreeType();
2295 static void FASTCALL
2296 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2297 LPCWSTR FullName
, PFONTGDI FontGDI
)
2300 UNICODE_STRING StyleW
;
2303 CHARSETINFO CharSetInfo
;
2305 OUTLINETEXTMETRICW
*Otm
;
2308 NEWTEXTMETRICW
*Ntm
;
2311 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2312 FT_Face Face
= SharedFace
->Face
;
2313 UNICODE_STRING NameW
;
2315 RtlInitUnicodeString(&NameW
, NULL
);
2316 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2317 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2318 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2323 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2326 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2330 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2331 TM
= &Otm
->otmTextMetrics
;
2333 Lf
->lfHeight
= TM
->tmHeight
;
2334 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2335 Lf
->lfWeight
= TM
->tmWeight
;
2336 Lf
->lfItalic
= TM
->tmItalic
;
2337 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2338 Lf
->lfCharSet
= TM
->tmCharSet
;
2339 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2340 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2341 Lf
->lfQuality
= PROOF_QUALITY
;
2343 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2344 Ntm
->tmHeight
= TM
->tmHeight
;
2345 Ntm
->tmAscent
= TM
->tmAscent
;
2346 Ntm
->tmDescent
= TM
->tmDescent
;
2347 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2348 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2349 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2350 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2351 Ntm
->tmWeight
= TM
->tmWeight
;
2352 Ntm
->tmOverhang
= TM
->tmOverhang
;
2353 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2354 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2355 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2356 Ntm
->tmLastChar
= TM
->tmLastChar
;
2357 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2358 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2359 Ntm
->tmItalic
= TM
->tmItalic
;
2360 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2361 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2362 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2363 Ntm
->tmCharSet
= TM
->tmCharSet
;
2364 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2366 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2368 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2370 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2371 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2372 Ntm
->ntmAvgWidth
= 0;
2374 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2375 ? TRUETYPE_FONTTYPE
: 0);
2377 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2378 Info
->FontType
|= RASTER_FONTTYPE
;
2383 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2385 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2389 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2391 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2392 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2395 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2397 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2398 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2399 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2400 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2401 if (!NT_SUCCESS(status
))
2405 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2408 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2412 IntUnLockFreeType();
2416 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2417 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2418 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2419 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2420 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2421 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2423 if (0 == pOS2
->version
)
2427 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2428 fs
.fsCsb
[0] |= FS_LATIN1
;
2430 fs
.fsCsb
[0] |= FS_SYMBOL
;
2432 IntUnLockFreeType();
2434 if (fs
.fsCsb
[0] == 0)
2436 /* Let's see if we can find any interesting cmaps */
2437 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2439 switch (Face
->charmaps
[i
]->encoding
)
2441 case FT_ENCODING_UNICODE
:
2442 case FT_ENCODING_APPLE_ROMAN
:
2443 fs
.fsCsb
[0] |= FS_LATIN1
;
2445 case FT_ENCODING_MS_SYMBOL
:
2446 fs
.fsCsb
[0] |= FS_SYMBOL
;
2454 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2457 if (fs
.fsCsb
[0] & fs0
)
2459 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2461 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2463 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2465 if (g_ElfScripts
[i
])
2466 wcscpy(Info
->EnumLogFontEx
.elfScript
, g_ElfScripts
[i
]);
2469 DPRINT1("Unknown elfscript for bit %u\n", i
);
2474 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2478 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2481 UNICODE_STRING InfoFaceName
;
2483 for (i
= 0; i
< InfoEntries
; i
++)
2485 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2486 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2495 static BOOLEAN FASTCALL
2496 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2497 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2499 UNICODE_STRING LogFontFaceName
;
2501 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2502 if (0 != LogFontFaceName
.Length
&&
2503 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2508 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2511 static BOOL FASTCALL
2512 FontFamilyFound(PFONTFAMILYINFO InfoEntry
,
2513 PFONTFAMILYINFO Info
, DWORD InfoCount
)
2515 LPLOGFONTW plf1
= &InfoEntry
->EnumLogFontEx
.elfLogFont
;
2516 LPWSTR pFullName1
= InfoEntry
->EnumLogFontEx
.elfFullName
;
2520 for (i
= 0; i
< InfoCount
; ++i
)
2522 LPLOGFONTW plf2
= &Info
[i
].EnumLogFontEx
.elfLogFont
;
2523 if (plf1
->lfCharSet
!= plf2
->lfCharSet
)
2526 pFullName2
= Info
[i
].EnumLogFontEx
.elfFullName
;
2527 if (_wcsicmp(pFullName1
, pFullName2
) != 0)
2535 static BOOLEAN FASTCALL
2536 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2537 PFONTFAMILYINFO Info
,
2543 PFONT_ENTRY CurrentEntry
;
2545 FONTFAMILYINFO InfoEntry
;
2546 DWORD Count
= *pCount
;
2548 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
2550 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2551 FontGDI
= CurrentEntry
->Font
;
2554 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&&
2555 LogFont
->lfCharSet
!= FontGDI
->CharSet
)
2560 if (LogFont
->lfFaceName
[0] == UNICODE_NULL
)
2562 if (Count
< MaxCount
)
2564 FontFamilyFillInfo(&Info
[Count
], NULL
, NULL
, FontGDI
);
2570 FontFamilyFillInfo(&InfoEntry
, NULL
, NULL
, FontGDI
);
2572 if (_wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0 &&
2573 _wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfFullName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0)
2578 if (!FontFamilyFound(&InfoEntry
, Info
, min(Count
, MaxCount
)))
2580 if (Count
< MaxCount
)
2582 RtlCopyMemory(&Info
[Count
], &InfoEntry
, sizeof(InfoEntry
));
2593 static BOOLEAN FASTCALL
2594 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2595 PFONTFAMILYINFO Info
,
2599 PLIST_ENTRY pEntry
, pHead
= &g_FontSubstListHead
;
2600 PFONTSUBST_ENTRY pCurrentEntry
;
2601 PUNICODE_STRING pFromW
;
2603 LOGFONTW lf
= *LogFont
;
2604 UNICODE_STRING NameW
;
2606 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2608 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2610 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2611 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2613 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2614 continue; /* mismatch */
2617 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2618 SubstituteFontRecurse(&lf
);
2620 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2621 FontGDI
= FindFaceNameInLists(&NameW
);
2622 if (FontGDI
== NULL
)
2624 continue; /* no real font */
2627 if (*pCount
< MaxCount
)
2629 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2639 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2643 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2644 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2645 lprs
->nLanguageID
= gusLanguageID
;
2648 EngSetLastError(ERROR_INVALID_PARAMETER
);
2658 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2659 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2660 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2661 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2664 FT_BitmapGlyph APIENTRY
2669 FT_Render_Mode RenderMode
,
2672 PLIST_ENTRY CurrentEntry
;
2673 PFONT_CACHE_ENTRY FontEntry
;
2675 ASSERT_FREETYPE_LOCK_HELD();
2677 CurrentEntry
= g_FontCacheListHead
.Flink
;
2678 while (CurrentEntry
!= &g_FontCacheListHead
)
2680 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2681 if ((FontEntry
->Face
== Face
) &&
2682 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2683 (FontEntry
->Height
== Height
) &&
2684 (FontEntry
->RenderMode
== RenderMode
) &&
2685 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2687 CurrentEntry
= CurrentEntry
->Flink
;
2690 if (CurrentEntry
== &g_FontCacheListHead
)
2695 RemoveEntryList(CurrentEntry
);
2696 InsertHeadList(&g_FontCacheListHead
, CurrentEntry
);
2697 return FontEntry
->BitmapGlyph
;
2701 FT_BitmapGlyph APIENTRY
2704 FT_GlyphSlot GlyphSlot
,
2705 FT_Render_Mode RenderMode
)
2709 FT_Bitmap AlignedBitmap
;
2710 FT_BitmapGlyph BitmapGlyph
;
2712 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2715 DPRINT1("Failure getting glyph.\n");
2719 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2722 FT_Done_Glyph(Glyph
);
2723 DPRINT1("Failure rendering glyph.\n");
2727 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2728 FT_Bitmap_New(&AlignedBitmap
);
2729 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2731 DPRINT1("Conversion failed\n");
2732 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2736 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2737 BitmapGlyph
->bitmap
= AlignedBitmap
;
2742 FT_BitmapGlyph APIENTRY
2748 FT_GlyphSlot GlyphSlot
,
2749 FT_Render_Mode RenderMode
)
2753 PFONT_CACHE_ENTRY NewEntry
;
2754 FT_Bitmap AlignedBitmap
;
2755 FT_BitmapGlyph BitmapGlyph
;
2757 ASSERT_FREETYPE_LOCK_HELD();
2759 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2762 DPRINT1("Failure caching glyph.\n");
2766 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2769 FT_Done_Glyph(GlyphCopy
);
2770 DPRINT1("Failure rendering glyph.\n");
2774 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2777 DPRINT1("Alloc failure caching glyph.\n");
2778 FT_Done_Glyph(GlyphCopy
);
2782 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2783 FT_Bitmap_New(&AlignedBitmap
);
2784 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2786 DPRINT1("Conversion failed\n");
2787 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2788 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2789 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2793 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2794 BitmapGlyph
->bitmap
= AlignedBitmap
;
2796 NewEntry
->GlyphIndex
= GlyphIndex
;
2797 NewEntry
->Face
= Face
;
2798 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2799 NewEntry
->Height
= Height
;
2800 NewEntry
->RenderMode
= RenderMode
;
2801 NewEntry
->mxWorldToDevice
= *pmx
;
2803 InsertHeadList(&g_FontCacheListHead
, &NewEntry
->ListEntry
);
2804 if (++g_FontCacheNumEntries
> MAX_FONT_CACHE
)
2806 NewEntry
= CONTAINING_RECORD(g_FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2807 RemoveCachedEntry(NewEntry
);
2814 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2816 pt
->x
.value
= vec
->x
>> 6;
2817 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2818 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2819 pt
->y
.value
= vec
->y
>> 6;
2820 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2821 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2825 This function builds an FT_Fixed from a float. It puts the integer part
2826 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2827 It fails if the integer part of the float number is greater than SHORT_MAX.
2829 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2832 unsigned short fract
= (f
- value
) * 0xFFFF;
2833 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2837 This function builds an FT_Fixed from a FIXED. It simply put f.value
2838 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2840 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2842 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2845 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2847 TTPOLYGONHEADER
*pph
;
2849 int needed
= 0, point
= 0, contour
, first_pt
;
2850 unsigned int pph_start
, cpfx
;
2853 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2855 /* Ignore contours containing one point */
2856 if (point
== outline
->contours
[contour
])
2863 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2867 pph
->dwType
= TT_POLYGON_TYPE
;
2868 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2870 needed
+= sizeof(*pph
);
2872 while (point
<= outline
->contours
[contour
])
2874 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2875 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2876 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2881 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2884 } while (point
<= outline
->contours
[contour
] &&
2885 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2886 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2887 /* At the end of a contour Windows adds the start point, but
2889 if (point
> outline
->contours
[contour
] &&
2890 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2893 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2896 else if (point
<= outline
->contours
[contour
] &&
2897 outline
->tags
[point
] & FT_Curve_Tag_On
)
2899 /* add closing pt for bezier */
2901 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2910 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2913 pph
->cb
= needed
- pph_start
;
2918 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2920 /* Convert the quadratic Beziers to cubic Beziers.
2921 The parametric eqn for a cubic Bezier is, from PLRM:
2922 r(t) = at^3 + bt^2 + ct + r0
2923 with the control points:
2928 A quadratic Bezier has the form:
2929 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2931 So equating powers of t leads to:
2932 r1 = 2/3 p1 + 1/3 p0
2933 r2 = 2/3 p1 + 1/3 p2
2934 and of course r0 = p0, r3 = p2
2936 int contour
, point
= 0, first_pt
;
2937 TTPOLYGONHEADER
*pph
;
2939 DWORD pph_start
, cpfx
, type
;
2940 FT_Vector cubic_control
[4];
2941 unsigned int needed
= 0;
2943 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2946 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2950 pph
->dwType
= TT_POLYGON_TYPE
;
2951 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2953 needed
+= sizeof(*pph
);
2955 while (point
<= outline
->contours
[contour
])
2957 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2958 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2959 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2963 if (type
== TT_PRIM_LINE
)
2966 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2972 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2975 /* FIXME: Possible optimization in endpoint calculation
2976 if there are two consecutive curves */
2977 cubic_control
[0] = outline
->points
[point
-1];
2978 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2980 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2981 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2982 cubic_control
[0].x
>>= 1;
2983 cubic_control
[0].y
>>= 1;
2985 if (point
+1 > outline
->contours
[contour
])
2986 cubic_control
[3] = outline
->points
[first_pt
];
2989 cubic_control
[3] = outline
->points
[point
+1];
2990 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2992 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2993 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2994 cubic_control
[3].x
>>= 1;
2995 cubic_control
[3].y
>>= 1;
2998 /* r1 = 1/3 p0 + 2/3 p1
2999 r2 = 1/3 p2 + 2/3 p1 */
3000 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
3001 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
3002 cubic_control
[2] = cubic_control
[1];
3003 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
3004 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
3005 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
3006 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
3009 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
3010 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
3011 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
3016 } while (point
<= outline
->contours
[contour
] &&
3017 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
3018 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
3019 /* At the end of a contour Windows adds the start point,
3020 but only for Beziers and we've already done that.
3022 if (point
<= outline
->contours
[contour
] &&
3023 outline
->tags
[point
] & FT_Curve_Tag_On
)
3025 /* This is the closing pt of a bezier, but we've already
3026 added it, so just inc point and carry on */
3034 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
3037 pph
->cb
= needed
- pph_start
;
3043 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG lfWidth
, LONG lfHeight
)
3046 FT_Size_RequestRec req
;
3047 FT_Face face
= FontGDI
->SharedFace
->Face
;
3049 TT_HoriHeader
*pHori
;
3050 FT_WinFNT_HeaderRec WinFNT
;
3051 LONG Ascent
, Descent
, Sum
, EmHeight64
;
3053 lfWidth
= abs(lfWidth
);
3058 DPRINT("lfHeight and lfWidth are zero.\n");
3070 ASSERT_FREETYPE_LOCK_HELD();
3071 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(face
, FT_SFNT_OS2
);
3072 pHori
= (TT_HoriHeader
*)FT_Get_Sfnt_Table(face
, FT_SFNT_HHEA
);
3074 if (!pOS2
|| !pHori
)
3076 error
= FT_Get_WinFNT_Header(face
, &WinFNT
);
3080 FontGDI
->tmHeight
= WinFNT
.pixel_height
;
3081 FontGDI
->tmAscent
= WinFNT
.ascent
;
3082 FontGDI
->tmDescent
= FontGDI
->tmHeight
- FontGDI
->tmAscent
;
3083 FontGDI
->tmInternalLeading
= WinFNT
.internal_leading
;
3084 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3085 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3086 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3087 FontGDI
->Magic
= FONTGDI_MAGIC
;
3089 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3091 req
.height
= (FT_Long
)(FontGDI
->EmHeight
<< 6);
3092 req
.horiResolution
= 0;
3093 req
.vertResolution
= 0;
3094 return FT_Request_Size(face
, &req
);
3099 /* case (A): lfHeight is positive */
3100 Sum
= pOS2
->usWinAscent
+ pOS2
->usWinDescent
;
3103 Ascent
= pHori
->Ascender
;
3104 Descent
= -pHori
->Descender
;
3105 Sum
= Ascent
+ Descent
;
3109 Ascent
= pOS2
->usWinAscent
;
3110 Descent
= pOS2
->usWinDescent
;
3113 FontGDI
->tmAscent
= FT_MulDiv(lfHeight
, Ascent
, Sum
);
3114 FontGDI
->tmDescent
= FT_MulDiv(lfHeight
, Descent
, Sum
);
3115 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3116 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
- FT_MulDiv(lfHeight
, face
->units_per_EM
, Sum
);
3118 else if (lfHeight
< 0)
3120 /* case (B): lfHeight is negative */
3121 FontGDI
->tmAscent
= FT_MulDiv(-lfHeight
, pOS2
->usWinAscent
, face
->units_per_EM
);
3122 FontGDI
->tmDescent
= FT_MulDiv(-lfHeight
, pOS2
->usWinDescent
, face
->units_per_EM
);
3123 FontGDI
->tmHeight
= FontGDI
->tmAscent
+ FontGDI
->tmDescent
;
3124 FontGDI
->tmInternalLeading
= FontGDI
->tmHeight
+ lfHeight
;
3127 FontGDI
->EmHeight
= FontGDI
->tmHeight
- FontGDI
->tmInternalLeading
;
3128 FontGDI
->EmHeight
= max(FontGDI
->EmHeight
, 1);
3129 FontGDI
->EmHeight
= min(FontGDI
->EmHeight
, USHORT_MAX
);
3130 FontGDI
->Magic
= FONTGDI_MAGIC
;
3133 EmHeight64
= (FontGDI
->EmHeight
<< 6) + 31;
3135 EmHeight64
= (FontGDI
->EmHeight
<< 6);
3137 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3139 req
.height
= EmHeight64
;
3140 req
.horiResolution
= 0;
3141 req
.vertResolution
= 0;
3142 return FT_Request_Size(face
, &req
);
3147 TextIntUpdateSize(PDC dc
,
3154 FT_CharMap charmap
, found
;
3160 face
= FontGDI
->SharedFace
->Face
;
3161 if (face
->charmap
== NULL
)
3163 DPRINT("WARNING: No charmap selected!\n");
3164 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3167 for (n
= 0; n
< face
->num_charmaps
; n
++)
3169 charmap
= face
->charmaps
[n
];
3170 if (charmap
->encoding
== FT_ENCODING_UNICODE
)
3178 for (n
= 0; n
< face
->num_charmaps
; n
++)
3180 charmap
= face
->charmaps
[n
];
3181 if (charmap
->encoding
== FT_ENCODING_MS_SYMBOL
)
3190 DPRINT1("WARNING: Could not find desired charmap!\n");
3194 DPRINT("Found charmap encoding: %i\n", found
->encoding
);
3195 error
= FT_Set_Charmap(face
, found
);
3198 DPRINT1("WARNING: Could not set the charmap!\n");
3203 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3205 error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3208 IntUnLockFreeType();
3212 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3219 static inline FT_UInt FASTCALL
3220 get_glyph_index_symbol(FT_Face ft_face
, UINT glyph
)
3224 if (glyph
< 0x100) glyph
+= 0xf000;
3225 /* there are a number of old pre-Unicode "broken" TTFs, which
3226 do have symbols at U+00XX instead of U+f0XX */
3227 if (!(ret
= FT_Get_Char_Index(ft_face
, glyph
)))
3228 ret
= FT_Get_Char_Index(ft_face
, glyph
- 0xf000);
3233 static inline FT_UInt FASTCALL
3234 get_glyph_index(FT_Face ft_face
, UINT glyph
)
3238 if (face_has_symbol_charmap(ft_face
))
3240 ret
= get_glyph_index_symbol(ft_face
, glyph
);
3245 return FT_Get_Char_Index(ft_face
, glyph
);
3248 static inline FT_UInt FASTCALL
3249 get_glyph_index_flagged(FT_Face face
, FT_ULong code
, DWORD indexed_flag
, DWORD flags
)
3251 FT_UInt glyph_index
;
3252 if (flags
& indexed_flag
)
3258 glyph_index
= get_glyph_index(face
, code
);
3264 * Based on WineEngGetGlyphOutline
3269 ftGdiGetGlyphOutline(
3277 BOOL bIgnoreRotation
)
3279 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3287 FT_UInt glyph_index
;
3288 DWORD width
, height
, pitch
, needed
= 0;
3289 FT_Bitmap ft_bitmap
;
3291 INT left
, right
, top
= 0, bottom
= 0;
3293 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3294 FLOAT eM11
, widthRatio
= 1.0;
3295 FT_Matrix transMat
= identityMat
;
3296 BOOL needsTransform
= FALSE
;
3299 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3300 OUTLINETEXTMETRICW
*potm
;
3304 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3305 cjBuf
, pvBuf
, pmat2
);
3307 pdcattr
= dc
->pdcattr
;
3309 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3312 hFont
= pdcattr
->hlfntNew
;
3313 TextObj
= RealizeFontInit(hFont
);
3317 EngSetLastError(ERROR_INVALID_HANDLE
);
3320 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3321 ft_face
= FontGDI
->SharedFace
->Face
;
3323 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3324 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3325 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3327 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3328 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3331 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3332 TEXTOBJ_UnlockText(TextObj
);
3335 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3338 /* FIXME: last error? */
3339 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3340 TEXTOBJ_UnlockText(TextObj
);
3345 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3346 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3348 TEXTOBJ_UnlockText(TextObj
);
3350 glyph_index
= get_glyph_index_flagged(ft_face
, wch
, GGO_GLYPH_INDEX
, iFormat
);
3351 iFormat
&= ~GGO_GLYPH_INDEX
;
3353 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3354 load_flags
|= FT_LOAD_NO_BITMAP
;
3356 if (iFormat
& GGO_UNHINTED
)
3358 load_flags
|= FT_LOAD_NO_HINTING
;
3359 iFormat
&= ~GGO_UNHINTED
;
3362 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3365 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3366 IntUnLockFreeType();
3367 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3370 IntUnLockFreeType();
3372 if (aveWidth
&& potm
)
3374 widthRatio
= (FLOAT
)aveWidth
* eM11
/
3375 (FLOAT
) potm
->otmTextMetrics
.tmAveCharWidth
;
3378 left
= (INT
)(ft_face
->glyph
->metrics
.horiBearingX
* widthRatio
) & -64;
3379 right
= (INT
)((ft_face
->glyph
->metrics
.horiBearingX
+
3380 ft_face
->glyph
->metrics
.width
) * widthRatio
+ 63) & -64;
3382 adv
= (INT
)((ft_face
->glyph
->metrics
.horiAdvance
* widthRatio
) + 63) >> 6;
3384 bbx
= (right
- left
) >> 6;
3386 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3390 /* Width scaling transform */
3391 if (widthRatio
!= 1.0)
3394 scaleMat
.xx
= FT_FixedFromFloat(widthRatio
);
3397 scaleMat
.yy
= FT_FixedFromFloat(1.0);
3399 FT_Matrix_Multiply(&scaleMat
, &transMat
);
3400 needsTransform
= TRUE
;
3403 /* World transform */
3407 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3409 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3410 efTemp
= pmx
->efM11
;
3411 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3412 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
3414 efTemp
= pmx
->efM12
;
3415 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3416 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
3418 efTemp
= pmx
->efM21
;
3419 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3420 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
3422 efTemp
= pmx
->efM22
;
3423 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3424 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
3426 if (memcmp(&ftmatrix
, &identityMat
, sizeof(identityMat
)) != 0)
3428 FT_Matrix_Multiply(&ftmatrix
, &transMat
);
3429 needsTransform
= TRUE
;
3433 /* Rotation transform */
3436 FT_Matrix rotationMat
;
3438 DPRINT("Rotation Trans!\n");
3439 angle
= FT_FixedFromFloat((float)orientation
/ 10.0);
3440 FT_Vector_Unit(&vecAngle
, angle
);
3441 rotationMat
.xx
= vecAngle
.x
;
3442 rotationMat
.xy
= -vecAngle
.y
;
3443 rotationMat
.yx
= -rotationMat
.xy
;
3444 rotationMat
.yy
= rotationMat
.xx
;
3445 FT_Matrix_Multiply(&rotationMat
, &transMat
);
3446 needsTransform
= TRUE
;
3449 /* Extra transformation specified by caller */
3453 DPRINT("MAT2 Matrix Trans!\n");
3454 extraMat
.xx
= FT_FixedFromFIXED(pmat2
->eM11
);
3455 extraMat
.xy
= FT_FixedFromFIXED(pmat2
->eM21
);
3456 extraMat
.yx
= FT_FixedFromFIXED(pmat2
->eM12
);
3457 extraMat
.yy
= FT_FixedFromFIXED(pmat2
->eM22
);
3458 FT_Matrix_Multiply(&extraMat
, &transMat
);
3459 needsTransform
= TRUE
;
3462 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
); /* It looks like we are finished with potm ATM. */
3464 if (!needsTransform
)
3466 DPRINT("No Need to be Transformed!\n");
3467 top
= (ft_face
->glyph
->metrics
.horiBearingY
+ 63) & -64;
3468 bottom
= (ft_face
->glyph
->metrics
.horiBearingY
-
3469 ft_face
->glyph
->metrics
.height
) & -64;
3470 gm
.gmCellIncX
= adv
;
3477 for (xc
= 0; xc
< 2; xc
++)
3479 for (yc
= 0; yc
< 2; yc
++)
3481 vec
.x
= (ft_face
->glyph
->metrics
.horiBearingX
+
3482 xc
* ft_face
->glyph
->metrics
.width
);
3483 vec
.y
= ft_face
->glyph
->metrics
.horiBearingY
-
3484 yc
* ft_face
->glyph
->metrics
.height
;
3485 DPRINT("Vec %ld,%ld\n", vec
.x
, vec
.y
);
3486 FT_Vector_Transform(&vec
, &transMat
);
3487 if (xc
== 0 && yc
== 0)
3489 left
= right
= vec
.x
;
3490 top
= bottom
= vec
.y
;
3494 if (vec
.x
< left
) left
= vec
.x
;
3495 else if (vec
.x
> right
) right
= vec
.x
;
3496 if (vec
.y
< bottom
) bottom
= vec
.y
;
3497 else if (vec
.y
> top
) top
= vec
.y
;
3502 right
= (right
+ 63) & -64;
3503 bottom
= bottom
& -64;
3504 top
= (top
+ 63) & -64;
3506 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left
, top
, right
, bottom
);
3507 vec
.x
= ft_face
->glyph
->metrics
.horiAdvance
;
3509 FT_Vector_Transform(&vec
, &transMat
);
3510 gm
.gmCellIncX
= (vec
.x
+63) >> 6;
3511 gm
.gmCellIncY
= -((vec
.y
+63) >> 6);
3513 gm
.gmBlackBoxX
= (right
- left
) >> 6;
3514 gm
.gmBlackBoxY
= (top
- bottom
) >> 6;
3515 gm
.gmptGlyphOrigin
.x
= left
>> 6;
3516 gm
.gmptGlyphOrigin
.y
= top
>> 6;
3518 DPRINT("CX %d CY %d BBX %u BBY %u GOX %d GOY %d\n",
3519 gm
.gmCellIncX
, gm
.gmCellIncY
,
3520 gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
3521 gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
3523 IntUnLockFreeType();
3526 if (iFormat
== GGO_METRICS
)
3528 DPRINT("GGO_METRICS Exit!\n");
3530 return 1; /* FIXME */
3533 if (ft_face
->glyph
->format
!= ft_glyph_format_outline
&& iFormat
!= GGO_BITMAP
)
3535 DPRINT1("Loaded a bitmap\n");
3542 width
= gm
.gmBlackBoxX
;
3543 height
= gm
.gmBlackBoxY
;
3544 pitch
= ((width
+ 31) >> 5) << 2;
3545 needed
= pitch
* height
;
3547 if (!pvBuf
|| !cjBuf
) break;
3548 if (!needed
) return GDI_ERROR
; /* empty glyph */
3552 switch (ft_face
->glyph
->format
)
3554 case ft_glyph_format_bitmap
:
3556 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3557 INT w
= min( pitch
, (ft_face
->glyph
->bitmap
.width
+ 7) >> 3 );
3558 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3561 RtlCopyMemory(dst
, src
, w
);
3562 src
+= ft_face
->glyph
->bitmap
.pitch
;
3568 case ft_glyph_format_outline
:
3569 ft_bitmap
.width
= width
;
3570 ft_bitmap
.rows
= height
;
3571 ft_bitmap
.pitch
= pitch
;
3572 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
3573 ft_bitmap
.buffer
= pvBuf
;
3578 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3580 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3581 /* Note: FreeType will only set 'black' bits for us. */
3582 RtlZeroMemory(pvBuf
, needed
);
3583 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3584 IntUnLockFreeType();
3588 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3593 case GGO_GRAY2_BITMAP
:
3594 case GGO_GRAY4_BITMAP
:
3595 case GGO_GRAY8_BITMAP
:
3597 unsigned int mult
, row
, col
;
3600 width
= gm
.gmBlackBoxX
;
3601 height
= gm
.gmBlackBoxY
;
3602 pitch
= (width
+ 3) / 4 * 4;
3603 needed
= pitch
* height
;
3605 if (!pvBuf
|| !cjBuf
) break;
3606 if (!needed
) return GDI_ERROR
; /* empty glyph */
3610 switch (ft_face
->glyph
->format
)
3612 case ft_glyph_format_bitmap
:
3614 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3615 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3619 for (x
= 0; (UINT
)x
< pitch
; x
++)
3621 if (x
< ft_face
->glyph
->bitmap
.width
)
3622 dst
[x
] = (src
[x
/ 8] & (1 << ( (7 - (x
% 8))))) ? 0xff : 0;
3626 src
+= ft_face
->glyph
->bitmap
.pitch
;
3631 case ft_glyph_format_outline
:
3633 ft_bitmap
.width
= width
;
3634 ft_bitmap
.rows
= height
;
3635 ft_bitmap
.pitch
= pitch
;
3636 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
3637 ft_bitmap
.buffer
= pvBuf
;
3642 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3644 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3645 RtlZeroMemory(ft_bitmap
.buffer
, cjBuf
);
3646 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3647 IntUnLockFreeType();
3649 if (iFormat
== GGO_GRAY2_BITMAP
)
3651 else if (iFormat
== GGO_GRAY4_BITMAP
)
3653 else if (iFormat
== GGO_GRAY8_BITMAP
)
3661 for (row
= 0; row
< height
; row
++)
3664 for (col
= 0; col
< width
; col
++, ptr
++)
3666 *ptr
= (((int)*ptr
) * mult
+ 128) / 256;
3674 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3681 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3683 if (cjBuf
== 0) pvBuf
= NULL
; /* This is okay, need cjBuf to allocate. */
3686 if (needsTransform
&& pvBuf
) FT_Outline_Transform(outline
, &transMat
);
3688 needed
= get_native_glyph_outline(outline
, cjBuf
, NULL
);
3690 if (!pvBuf
|| !cjBuf
)
3692 IntUnLockFreeType();
3697 IntUnLockFreeType();
3700 get_native_glyph_outline(outline
, cjBuf
, pvBuf
);
3701 IntUnLockFreeType();
3706 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3707 if (cjBuf
== 0) pvBuf
= NULL
;
3709 if (needsTransform
&& pvBuf
)
3712 FT_Outline_Transform(outline
, &transMat
);
3713 IntUnLockFreeType();
3715 needed
= get_bezier_glyph_outline(outline
, cjBuf
, NULL
);
3717 if (!pvBuf
|| !cjBuf
)
3722 get_bezier_glyph_outline(outline
, cjBuf
, pvBuf
);
3727 DPRINT1("Unsupported format %u\n", iFormat
);
3731 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed
);
3738 TextIntGetTextExtentPoint(PDC dc
,
3751 FT_BitmapGlyph realglyph
;
3752 INT error
, glyph_index
, i
, previous
;
3753 ULONGLONG TotalWidth
= 0;
3755 FT_Render_Mode RenderMode
;
3757 PMATRIX pmxWorldToDevice
;
3759 BOOL EmuBold
, EmuItalic
;
3760 LONG ascender
, descender
;
3762 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3764 face
= FontGDI
->SharedFace
->Face
;
3772 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3774 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3775 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
3776 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
3778 Render
= IntIsFontRenderingEnabled();
3780 RenderMode
= IntGetFontRenderMode(plf
);
3782 RenderMode
= FT_RENDER_MODE_MONO
;
3784 /* Get the DC's world-to-device transformation matrix */
3785 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
3786 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
3788 use_kerning
= FT_HAS_KERNING(face
);
3791 for (i
= 0; i
< Count
; i
++)
3793 glyph_index
= get_glyph_index_flagged(face
, *String
, GTEF_INDICES
, fl
);
3795 if (EmuBold
|| EmuItalic
)
3798 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
3799 RenderMode
, pmxWorldToDevice
);
3801 if (EmuBold
|| EmuItalic
|| !realglyph
)
3803 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
3806 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
3810 glyph
= face
->glyph
;
3811 if (EmuBold
|| EmuItalic
)
3814 FT_GlyphSlot_Embolden(glyph
);
3816 FT_GlyphSlot_Oblique(glyph
);
3817 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
3821 realglyph
= ftGdiGlyphCacheSet(face
,
3831 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
3836 /* Retrieve kerning distance */
3837 if (use_kerning
&& previous
&& glyph_index
)
3840 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
3841 TotalWidth
+= delta
.x
;
3844 TotalWidth
+= realglyph
->root
.advance
.x
>> 10;
3846 if (((TotalWidth
+ 32) >> 6) <= MaxExtent
&& NULL
!= Fit
)
3852 Dx
[i
] = (TotalWidth
+ 32) >> 6;
3855 if (EmuBold
|| EmuItalic
)
3857 FT_Done_Glyph((FT_Glyph
)realglyph
);
3861 previous
= glyph_index
;
3864 ASSERT(FontGDI
->Magic
== FONTGDI_MAGIC
);
3865 ascender
= FontGDI
->tmAscent
; /* Units above baseline */
3866 descender
= FontGDI
->tmDescent
; /* Units below baseline */
3867 IntUnLockFreeType();
3869 Size
->cx
= (TotalWidth
+ 32) >> 6;
3870 Size
->cy
= ascender
+ descender
;
3878 ftGdiGetTextCharsetInfo(
3880 LPFONTSIGNATURE lpSig
,
3884 UINT Ret
= DEFAULT_CHARSET
;
3894 USHORT usACP
, usOEM
;
3896 pdcattr
= Dc
->pdcattr
;
3897 hFont
= pdcattr
->hlfntNew
;
3898 TextObj
= RealizeFontInit(hFont
);
3902 EngSetLastError(ERROR_INVALID_HANDLE
);
3905 FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
3906 Face
= FontGdi
->SharedFace
->Face
;
3907 TEXTOBJ_UnlockText(TextObj
);
3910 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
3911 IntUnLockFreeType();
3912 memset(&fs
, 0, sizeof(FONTSIGNATURE
));
3915 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
3916 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
3917 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
3918 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
3919 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
3920 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
3921 if (pOS2
->version
== 0)
3925 if (FT_Get_First_Char( Face
, &dummy
) < 0x100)
3926 fs
.fsCsb
[0] |= FS_LATIN1
;
3928 fs
.fsCsb
[0] |= FS_SYMBOL
;
3931 DPRINT("Csb 1=%x 0=%x\n", fs
.fsCsb
[1],fs
.fsCsb
[0]);
3932 if (fs
.fsCsb
[0] == 0)
3933 { /* Let's see if we can find any interesting cmaps */
3934 for (i
= 0; i
< Face
->num_charmaps
; i
++)
3936 switch (Face
->charmaps
[i
]->encoding
)
3938 case FT_ENCODING_UNICODE
:
3939 case FT_ENCODING_APPLE_ROMAN
:
3940 fs
.fsCsb
[0] |= FS_LATIN1
;
3942 case FT_ENCODING_MS_SYMBOL
:
3943 fs
.fsCsb
[0] |= FS_SYMBOL
;
3952 RtlCopyMemory(lpSig
, &fs
, sizeof(FONTSIGNATURE
));
3955 RtlGetDefaultCodePage(&usACP
, &usOEM
);
3958 if (IntTranslateCharsetInfo(&cp
, &csi
, TCI_SRCCODEPAGE
))
3959 if (csi
.fs
.fsCsb
[0] & fs
.fsCsb
[0])
3962 Ret
= csi
.ciCharset
;
3966 for (i
= 0; i
< MAXTCIINDEX
; i
++)
3969 if (fs
.fsCsb
[0] & fs0
)
3971 if (IntTranslateCharsetInfo(&fs0
, &csi
, TCI_SRCFONTSIG
))
3975 Ret
= csi
.ciCharset
;
3979 DPRINT1("TCI failing on %x\n", fs0
);
3983 DPRINT("CharSet %u CodePage %u\n", csi
.ciCharset
, csi
.ciACP
);
3984 return (MAKELONG(csi
.ciACP
, csi
.ciCharset
));
3990 ftGetFontUnicodeRanges(PFONTGDI Font
, PGLYPHSET glyphset
)
3993 DWORD num_ranges
= 0;
3994 FT_Face face
= Font
->SharedFace
->Face
;
3996 if (face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
3998 FT_UInt glyph_code
= 0;
3999 FT_ULong char_code
, char_code_prev
;
4001 char_code_prev
= char_code
= FT_Get_First_Char(face
, &glyph_code
);
4003 DPRINT("Face encoding FT_ENCODING_UNICODE, number of glyphs %ld, first glyph %u, first char %04lx\n",
4004 face
->num_glyphs
, glyph_code
, char_code
);
4006 if (!glyph_code
) return 0;
4010 glyphset
->ranges
[0].wcLow
= (USHORT
)char_code
;
4011 glyphset
->ranges
[0].cGlyphs
= 0;
4012 glyphset
->cGlyphsSupported
= 0;
4018 if (char_code
< char_code_prev
)
4020 DPRINT1("Expected increasing char code from FT_Get_Next_Char\n");
4023 if (char_code
- char_code_prev
> 1)
4028 glyphset
->ranges
[num_ranges
- 1].wcLow
= (USHORT
)char_code
;
4029 glyphset
->ranges
[num_ranges
- 1].cGlyphs
= 1;
4030 glyphset
->cGlyphsSupported
++;
4035 glyphset
->ranges
[num_ranges
- 1].cGlyphs
++;
4036 glyphset
->cGlyphsSupported
++;
4038 char_code_prev
= char_code
;
4039 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_code
);
4043 DPRINT1("Encoding %i not supported\n", face
->charmap
->encoding
);
4045 size
= sizeof(GLYPHSET
) + sizeof(WCRANGE
) * (num_ranges
- 1);
4048 glyphset
->cbThis
= size
;
4049 glyphset
->cRanges
= num_ranges
;
4050 glyphset
->flAccel
= 0;
4058 ftGdiGetTextMetricsW(
4060 PTMW_INTERNAL ptmwi
)
4068 TT_HoriHeader
*pHori
;
4069 FT_WinFNT_HeaderRec Win
;
4071 NTSTATUS Status
= STATUS_SUCCESS
;
4076 EngSetLastError(STATUS_INVALID_PARAMETER
);
4080 if (!(dc
= DC_LockDc(hDC
)))
4082 EngSetLastError(ERROR_INVALID_HANDLE
);
4085 pdcattr
= dc
->pdcattr
;
4086 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
4087 if (NULL
!= TextObj
)
4089 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4090 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
4092 Face
= FontGDI
->SharedFace
->Face
;
4095 Error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
4096 FtSetCoordinateTransform(Face
, DC_pmxWorldToDevice(dc
));
4097 IntUnLockFreeType();
4101 DPRINT1("Error in setting pixel sizes: %u\n", Error
);
4102 Status
= STATUS_UNSUCCESSFUL
;
4106 FT_Face Face
= FontGDI
->SharedFace
->Face
;
4107 Status
= STATUS_SUCCESS
;
4110 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
4113 DPRINT1("Can't find OS/2 table - not TT font?\n");
4114 Status
= STATUS_INTERNAL_ERROR
;
4117 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
4120 DPRINT1("Can't find HHEA table - not TT font?\n");
4121 Status
= STATUS_INTERNAL_ERROR
;
4124 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
4126 IntUnLockFreeType();
4128 if (NT_SUCCESS(Status
))
4130 FillTM(&ptmwi
->TextMetric
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
4132 /* FIXME: Fill Diff member */
4133 RtlZeroMemory(&ptmwi
->Diff
, sizeof(ptmwi
->Diff
));
4136 TEXTOBJ_UnlockText(TextObj
);
4140 Status
= STATUS_INVALID_HANDLE
;
4144 if (!NT_SUCCESS(Status
))
4146 SetLastNtError(Status
);
4161 DWORD Result
= GDI_ERROR
;
4162 FT_Face Face
= FontGdi
->SharedFace
->Face
;
4166 if (FT_IS_SFNT(Face
))
4169 Table
= Table
>> 24 | Table
<< 24 | (Table
>> 8 & 0xFF00) |
4170 (Table
<< 8 & 0xFF0000);
4172 if (!Buffer
) Size
= 0;
4177 FT_ULong Needed
= 0;
4179 Error
= FT_Load_Sfnt_Table(Face
, Table
, Offset
, NULL
, &Needed
);
4181 if ( !Error
&& Needed
< Size
) Size
= Needed
;
4183 if (!FT_Load_Sfnt_Table(Face
, Table
, Offset
, Buffer
, &Size
))
4187 IntUnLockFreeType();
4192 // NOTE: See Table 1. of https://msdn.microsoft.com/en-us/library/ms969909.aspx
4194 GetFontPenalty(const LOGFONTW
* LogFont
,
4195 const OUTLINETEXTMETRICW
* Otm
,
4196 const char * style_name
)
4201 BOOL fNeedScaling
= FALSE
;
4202 const BYTE UserCharSet
= CharSetFromLangID(gusLanguageID
);
4203 const TEXTMETRICW
* TM
= &Otm
->otmTextMetrics
;
4209 /* FIXME: IntSizeSynth Penalty 20 */
4210 /* FIXME: SmallPenalty Penalty 1 */
4211 /* FIXME: FaceNameSubst Penalty 500 */
4213 Byte
= LogFont
->lfCharSet
;
4214 if (Byte
== DEFAULT_CHARSET
)
4216 if (_wcsicmp(LogFont
->lfFaceName
, L
"Marlett") == 0)
4218 if (Byte
== ANSI_CHARSET
)
4220 DPRINT("Warning: FIXME: It's Marlett but ANSI_CHARSET.\n");
4222 /* We assume SYMBOL_CHARSET for "Marlett" font */
4223 Byte
= SYMBOL_CHARSET
;
4227 if (Byte
!= TM
->tmCharSet
)
4229 if (Byte
!= DEFAULT_CHARSET
&& Byte
!= ANSI_CHARSET
)
4231 /* CharSet Penalty 65000 */
4232 /* Requested charset does not match the candidate's. */
4237 if (UserCharSet
!= TM
->tmCharSet
)
4241 if (ANSI_CHARSET
!= TM
->tmCharSet
)
4250 Byte
= LogFont
->lfOutPrecision
;
4251 if (Byte
== OUT_DEFAULT_PRECIS
)
4252 Byte
= OUT_OUTLINE_PRECIS
; /* Is it OK? */
4255 case OUT_DEVICE_PRECIS
:
4256 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
) ||
4257 !(TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
)))
4259 /* OutputPrecision Penalty 19000 */
4260 /* Requested OUT_STROKE_PRECIS, but the device can't do it
4261 or the candidate is not a vector font. */
4266 if (TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
))
4268 /* OutputPrecision Penalty 19000 */
4269 /* Or OUT_STROKE_PRECIS not requested, and the candidate
4270 is a vector font that requires GDI support. */
4276 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4277 if (Byte
== DEFAULT_PITCH
)
4278 Byte
= VARIABLE_PITCH
;
4279 if ((Byte
& FIXED_PITCH
) || (Byte
& MONO_FONT
))
4281 if (TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
)
4283 /* FixedPitch Penalty 15000 */
4284 /* Requested a fixed pitch font, but the candidate is a
4285 variable pitch font. */
4289 if (Byte
& VARIABLE_PITCH
)
4291 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4293 /* PitchVariable Penalty 350 */
4294 /* Requested a variable pitch font, but the candidate is not a
4295 variable pitch font. */
4300 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4301 if (Byte
== DEFAULT_PITCH
)
4303 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4305 /* DefaultPitchFixed Penalty 1 */
4306 /* Requested DEFAULT_PITCH, but the candidate is fixed pitch. */
4311 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
4313 if (LogFont
->lfFaceName
[0])
4317 /* localized family name */
4320 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4322 /* localized full name */
4325 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
4326 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4330 /* FaceName Penalty 10000 */
4331 /* Requested a face name, but the candidate's face name
4337 Byte
= (LogFont
->lfPitchAndFamily
& 0xF0);
4338 if (Byte
!= FF_DONTCARE
)
4340 if (Byte
& FF_MODERN
)
4342 if (TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
)
4344 /* FixedPitch Penalty 15000 */
4345 /* Requested a fixed pitch font, but the candidate is a
4346 variable pitch font. */
4351 if ((Byte
& FF_ROMAN
) || (Byte
& FF_SWISS
))
4353 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4355 /* PitchVariable Penalty 350 */
4356 /* Requested a variable pitch font, but the candidate is not a
4357 variable pitch font. */
4362 #define FF_MASK (FF_DECORATIVE | FF_SCRIPT | FF_SWISS)
4363 if ((Byte
& FF_MASK
) != (TM
->tmPitchAndFamily
& FF_MASK
))
4365 /* Family Penalty 9000 */
4366 /* Requested a family, but the candidate's family is different. */
4371 if ((TM
->tmPitchAndFamily
& 0xF0) == FF_DONTCARE
)
4373 /* FamilyUnknown Penalty 8000 */
4374 /* Requested a family, but the candidate has no family. */
4379 /* Is the candidate a non-vector font? */
4380 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4382 /* Is lfHeight specified? */
4383 if (LogFont
->lfHeight
!= 0)
4385 if (labs(LogFont
->lfHeight
) < TM
->tmHeight
)
4387 /* HeightBigger Penalty 600 */
4388 /* The candidate is a nonvector font and is bigger than the
4389 requested height. */
4391 /* HeightBiggerDifference Penalty 150 */
4392 /* The candidate is a raster font and is larger than the
4393 requested height. Penalty * height difference */
4394 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4396 fNeedScaling
= TRUE
;
4398 if (TM
->tmHeight
< labs(LogFont
->lfHeight
))
4400 /* HeightSmaller Penalty 150 */
4401 /* The candidate is a raster font and is smaller than the
4402 requested height. Penalty * height difference */
4403 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4405 fNeedScaling
= TRUE
;
4410 switch (LogFont
->lfPitchAndFamily
& 0xF0)
4412 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4413 switch (TM
->tmPitchAndFamily
& 0xF0)
4415 case FF_DECORATIVE
: case FF_SCRIPT
:
4416 /* FamilyUnlikely Penalty 50 */
4417 /* Requested a roman/modern/swiss family, but the
4418 candidate is decorative/script. */
4425 case FF_DECORATIVE
: case FF_SCRIPT
:
4426 switch (TM
->tmPitchAndFamily
& 0xF0)
4428 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4429 /* FamilyUnlikely Penalty 50 */
4430 /* Or requested decorative/script, and the candidate is
4431 roman/modern/swiss. */
4441 if (LogFont
->lfWidth
!= 0)
4443 if (LogFont
->lfWidth
!= TM
->tmAveCharWidth
)
4445 /* Width Penalty 50 */
4446 /* Requested a nonzero width, but the candidate's width
4447 doesn't match. Penalty * width difference */
4448 Penalty
+= 50 * labs(LogFont
->lfWidth
- TM
->tmAveCharWidth
);
4450 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4451 fNeedScaling
= TRUE
;
4457 /* SizeSynth Penalty 50 */
4458 /* The candidate is a raster font that needs scaling by GDI. */
4462 if (!!LogFont
->lfItalic
!= !!TM
->tmItalic
)
4464 if (!LogFont
->lfItalic
&& ItalicFromStyle(style_name
))
4466 /* Italic Penalty 4 */
4467 /* Requested font and candidate font do not agree on italic status,
4468 and the desired result cannot be simulated. */
4469 /* Adjusted to 40 to satisfy (Oblique Penalty > Book Penalty). */
4472 else if (LogFont
->lfItalic
&& !ItalicFromStyle(style_name
))
4474 /* ItalicSim Penalty 1 */
4475 /* Requested italic font but the candidate is not italic,
4476 although italics can be simulated. */
4481 if (LogFont
->lfOutPrecision
== OUT_TT_PRECIS
)
4483 if (!(TM
->tmPitchAndFamily
& TMPF_TRUETYPE
))
4485 /* NotTrueType Penalty 4 */
4486 /* Requested OUT_TT_PRECIS, but the candidate is not a
4492 Long
= LogFont
->lfWeight
;
4493 if (LogFont
->lfWeight
== FW_DONTCARE
)
4495 if (Long
!= TM
->tmWeight
)
4497 /* Weight Penalty 3 */
4498 /* The candidate's weight does not match the requested weight.
4499 Penalty * (weight difference/10) */
4500 Penalty
+= 3 * (labs(Long
- TM
->tmWeight
) / 10);
4503 if (!LogFont
->lfUnderline
&& TM
->tmUnderlined
)
4505 /* Underline Penalty 3 */
4506 /* Requested font has no underline, but the candidate is
4511 if (!LogFont
->lfStrikeOut
&& TM
->tmStruckOut
)
4513 /* StrikeOut Penalty 3 */
4514 /* Requested font has no strike-out, but the candidate is
4519 /* Is the candidate a non-vector font? */
4520 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4522 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
< LogFont
->lfHeight
)
4524 /* VectorHeightSmaller Penalty 2 */
4525 /* Candidate is a vector font that is smaller than the
4526 requested height. Penalty * height difference */
4527 Penalty
+= 2 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4529 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
> LogFont
->lfHeight
)
4531 /* VectorHeightBigger Penalty 1 */
4532 /* Candidate is a vector font that is bigger than the
4533 requested height. Penalty * height difference */
4534 Penalty
+= 1 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4538 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
))
4540 /* DeviceFavor Penalty 2 */
4541 /* Extra penalty for all nondevice fonts. */
4545 if (TM
->tmAveCharWidth
>= 5 && TM
->tmHeight
>= 5)
4547 if (TM
->tmAveCharWidth
/ TM
->tmHeight
>= 3)
4549 /* Aspect Penalty 30 */
4550 /* The aspect rate is >= 3. It seems like a bad font. */
4551 Penalty
+= ((TM
->tmAveCharWidth
/ TM
->tmHeight
) - 2) * 30;
4553 else if (TM
->tmHeight
/ TM
->tmAveCharWidth
>= 3)
4555 /* Aspect Penalty 30 */
4556 /* The aspect rate is >= 3. It seems like a bad font. */
4557 Penalty
+= ((TM
->tmHeight
/ TM
->tmAveCharWidth
) - 2) * 30;
4563 DPRINT("WARNING: Penalty:%ld < 200: RequestedNameW:%ls, "
4564 "ActualNameW:%ls, lfCharSet:%d, lfWeight:%ld, "
4565 "tmCharSet:%d, tmWeight:%ld\n",
4566 Penalty
, LogFont
->lfFaceName
, ActualNameW
,
4567 LogFont
->lfCharSet
, LogFont
->lfWeight
,
4568 TM
->tmCharSet
, TM
->tmWeight
);
4571 return Penalty
; /* success */
4574 static __inline VOID
4575 FindBestFontFromList(FONTOBJ
**FontObj
, ULONG
*MatchPenalty
,
4576 const LOGFONTW
*LogFont
,
4577 const PLIST_ENTRY Head
)
4581 PFONT_ENTRY CurrentEntry
;
4583 OUTLINETEXTMETRICW
*Otm
= NULL
;
4584 UINT OtmSize
, OldOtmSize
= 0;
4588 ASSERT(MatchPenalty
);
4592 /* Start with a pretty big buffer */
4594 Otm
= ExAllocatePoolWithTag(PagedPool
, OldOtmSize
, GDITAG_TEXT
);
4596 /* get the FontObj of lowest penalty */
4597 Entry
= Head
->Flink
;
4598 while (Entry
!= Head
)
4600 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
4601 Entry
= Entry
->Flink
;
4603 FontGDI
= CurrentEntry
->Font
;
4605 Face
= FontGDI
->SharedFace
->Face
;
4607 /* get text metrics */
4608 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
4609 if (OtmSize
> OldOtmSize
)
4612 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4613 Otm
= ExAllocatePoolWithTag(PagedPool
, OtmSize
, GDITAG_TEXT
);
4616 /* update FontObj if lowest penalty */
4620 IntRequestFontSize(NULL
, FontGDI
, LogFont
->lfWidth
, LogFont
->lfHeight
);
4621 IntUnLockFreeType();
4623 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, OtmSize
, Otm
);
4627 OldOtmSize
= OtmSize
;
4629 Penalty
= GetFontPenalty(LogFont
, Otm
, Face
->style_name
);
4630 if (*MatchPenalty
== 0xFFFFFFFF || Penalty
< *MatchPenalty
)
4632 *FontObj
= GDIToObj(FontGDI
, FONT
);
4633 *MatchPenalty
= Penalty
;
4639 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4645 IntFontType(PFONTGDI Font
)
4647 PS_FontInfoRec psfInfo
;
4648 FT_ULong tmp_size
= 0;
4649 FT_Face Face
= Font
->SharedFace
->Face
;
4651 if (FT_HAS_MULTIPLE_MASTERS(Face
))
4652 Font
->FontObj
.flFontType
|= FO_MULTIPLEMASTER
;
4653 if (FT_HAS_VERTICAL(Face
))
4654 Font
->FontObj
.flFontType
|= FO_VERT_FACE
;
4655 if (!FT_IS_SCALABLE(Face
))
4656 Font
->FontObj
.flFontType
|= FO_TYPE_RASTER
;
4657 if (FT_IS_SFNT(Face
))
4659 Font
->FontObj
.flFontType
|= FO_TYPE_TRUETYPE
;
4660 if (FT_Get_Sfnt_Table(Face
, ft_sfnt_post
))
4661 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4663 if (!FT_Get_PS_Font_Info(Face
, &psfInfo
))
4665 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4667 /* Check for the presence of the 'CFF ' table to check if the font is Type1 */
4668 if (!FT_Load_Sfnt_Table(Face
, TTAG_CFF
, 0, NULL
, &tmp_size
))
4670 Font
->FontObj
.flFontType
|= (FO_CFF
|FO_POSTSCRIPT
);
4676 TextIntRealizeFont(HFONT FontHandle
, PTEXTOBJ pTextObj
)
4678 NTSTATUS Status
= STATUS_SUCCESS
;
4680 PPROCESSINFO Win32Process
;
4683 LOGFONTW SubstitutedLogFont
;
4688 TextObj
= TEXTOBJ_LockText(FontHandle
);
4689 if (NULL
== TextObj
)
4691 return STATUS_INVALID_HANDLE
;
4694 if (TextObj
->fl
& TEXTOBJECT_INIT
)
4696 TEXTOBJ_UnlockText(TextObj
);
4697 return STATUS_SUCCESS
;
4705 pLogFont
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4708 SubstitutedLogFont
= *pLogFont
;
4709 DPRINT("Font '%S,%u' is substituted by: ", pLogFont
->lfFaceName
, pLogFont
->lfCharSet
);
4710 SubstituteFontRecurse(&SubstitutedLogFont
);
4711 DPRINT("'%S,%u'.\n", SubstitutedLogFont
.lfFaceName
, SubstitutedLogFont
.lfCharSet
);
4713 MatchPenalty
= 0xFFFFFFFF;
4714 TextObj
->Font
= NULL
;
4716 Win32Process
= PsGetCurrentProcessWin32Process();
4718 /* Search private fonts */
4719 IntLockProcessPrivateFonts(Win32Process
);
4720 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
4721 &Win32Process
->PrivateFontListHead
);
4722 IntUnLockProcessPrivateFonts(Win32Process
);
4724 /* Search system fonts */
4725 IntLockGlobalFonts();
4726 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
4728 IntUnLockGlobalFonts();
4730 if (NULL
== TextObj
->Font
)
4732 DPRINT1("Request font %S not found, no fonts loaded at all\n",
4733 pLogFont
->lfFaceName
);
4734 Status
= STATUS_NOT_FOUND
;
4738 UNICODE_STRING FaceName
;
4739 PFONTGDI FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
4742 IntRequestFontSize(NULL
, FontGdi
, pLogFont
->lfWidth
, pLogFont
->lfHeight
);
4743 IntUnLockFreeType();
4745 RtlInitUnicodeString(&FaceName
, NULL
);
4746 IntGetFontLocalizedName(&FaceName
, FontGdi
->SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
4748 /* truncated copy */
4749 FaceName
.Length
= (USHORT
)min(FaceName
.Length
, (LF_FACESIZE
- 1) * sizeof(WCHAR
));
4750 FaceName
.MaximumLength
= (USHORT
)(FaceName
.Length
+ sizeof(UNICODE_NULL
));
4751 RtlCopyMemory(TextObj
->FaceName
, FaceName
.Buffer
, FaceName
.MaximumLength
);
4753 RtlFreeUnicodeString(&FaceName
);
4755 // Need hdev, when freetype is loaded need to create DEVOBJ for
4756 // Consumer and Producer.
4757 TextObj
->Font
->iUniq
= 1; // Now it can be cached.
4758 IntFontType(FontGdi
);
4759 FontGdi
->flType
= TextObj
->Font
->flFontType
;
4760 FontGdi
->RequestUnderline
= pLogFont
->lfUnderline
? 0xFF : 0;
4761 FontGdi
->RequestStrikeOut
= pLogFont
->lfStrikeOut
? 0xFF : 0;
4762 FontGdi
->RequestItalic
= pLogFont
->lfItalic
? 0xFF : 0;
4763 if (pLogFont
->lfWeight
!= FW_DONTCARE
)
4764 FontGdi
->RequestWeight
= pLogFont
->lfWeight
;
4766 FontGdi
->RequestWeight
= FW_NORMAL
;
4768 Face
= FontGdi
->SharedFace
->Face
;
4770 //FontGdi->OriginalWeight = WeightFromStyle(Face->style_name);
4772 if (!FontGdi
->OriginalItalic
)
4773 FontGdi
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
4775 TextObj
->fl
|= TEXTOBJECT_INIT
;
4776 Status
= STATUS_SUCCESS
;
4779 if (!pTextObj
) TEXTOBJ_UnlockText(TextObj
);
4781 ASSERT((NT_SUCCESS(Status
) ^ (NULL
== TextObj
->Font
)) != 0);
4791 POBJECT_NAME_INFORMATION NameInfo
,
4793 PUNICODE_STRING FileName
)
4796 OBJECT_ATTRIBUTES ObjectAttributes
;
4798 IO_STATUS_BLOCK IoStatusBlock
;
4801 InitializeObjectAttributes(&ObjectAttributes
,
4803 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
4807 Status
= ZwOpenFile(
4809 0, // FILE_READ_ATTRIBUTES,
4812 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
4815 if (!NT_SUCCESS(Status
))
4817 DPRINT("ZwOpenFile() failed (Status = 0x%lx)\n", Status
);
4821 Status
= ZwQueryObject(hFile
, ObjectNameInformation
, NameInfo
, Size
, &Desired
);
4823 if (!NT_SUCCESS(Status
))
4825 DPRINT("ZwQueryObject() failed (Status = %lx)\n", Status
);
4833 EqualFamilyInfo(const FONTFAMILYINFO
*pInfo1
, const FONTFAMILYINFO
*pInfo2
)
4835 const ENUMLOGFONTEXW
*pLog1
= &pInfo1
->EnumLogFontEx
;
4836 const ENUMLOGFONTEXW
*pLog2
= &pInfo2
->EnumLogFontEx
;
4837 const LOGFONTW
*plf1
= &pLog1
->elfLogFont
;
4838 const LOGFONTW
*plf2
= &pLog2
->elfLogFont
;
4840 if (_wcsicmp(plf1
->lfFaceName
, plf2
->lfFaceName
) != 0)
4845 if (_wcsicmp(pLog1
->elfStyle
, pLog2
->elfStyle
) != 0)
4854 IntAddNameFromFamInfo(LPWSTR psz
, FONTFAMILYINFO
*FamInfo
)
4856 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfLogFont
.lfFaceName
);
4857 if (FamInfo
->EnumLogFontEx
.elfStyle
[0] &&
4858 _wcsicmp(FamInfo
->EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4861 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfStyle
);
4867 IntGdiGetFontResourceInfo(
4868 PUNICODE_STRING FileName
,
4873 UNICODE_STRING EntryFileName
;
4874 POBJECT_NAME_INFORMATION NameInfo1
, NameInfo2
;
4875 PLIST_ENTRY ListEntry
;
4876 PFONT_ENTRY FontEntry
;
4877 ULONG Size
, i
, Count
;
4880 FONTFAMILYINFO
*FamInfo
;
4881 const ULONG MaxFamInfo
= 64;
4884 DPRINT("IntGdiGetFontResourceInfo: dwType == %lu\n", dwType
);
4886 /* Create buffer for full path name */
4887 Size
= sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
* sizeof(WCHAR
);
4888 NameInfo1
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4891 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4895 /* Get the full path name */
4896 if (!IntGetFullFileName(NameInfo1
, Size
, FileName
))
4898 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4902 /* Create a buffer for the entries' names */
4903 NameInfo2
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4906 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4907 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4911 FamInfo
= ExAllocatePoolWithTag(PagedPool
,
4912 sizeof(FONTFAMILYINFO
) * MaxFamInfo
,
4916 ExFreePoolWithTag(NameInfo2
, TAG_FINF
);
4917 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4918 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4921 /* Try to find the pathname in the global font list */
4923 IntLockGlobalFonts();
4924 for (ListEntry
= g_FontListHead
.Flink
; ListEntry
!= &g_FontListHead
;
4925 ListEntry
= ListEntry
->Flink
)
4927 FontEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY
, ListEntry
);
4928 if (FontEntry
->Font
->Filename
== NULL
)
4931 RtlInitUnicodeString(&EntryFileName
, FontEntry
->Font
->Filename
);
4932 if (!IntGetFullFileName(NameInfo2
, Size
, &EntryFileName
))
4935 if (!RtlEqualUnicodeString(&NameInfo1
->Name
, &NameInfo2
->Name
, FALSE
))
4939 FontFamilyFillInfo(&FamInfo
[Count
], FontEntry
->FaceName
.Buffer
,
4940 NULL
, FontEntry
->Font
);
4941 for (i
= 0; i
< Count
; ++i
)
4943 if (EqualFamilyInfo(&FamInfo
[i
], &FamInfo
[Count
]))
4953 if (Count
>= MaxFamInfo
)
4957 IntUnLockGlobalFonts();
4959 /* Free the buffers */
4960 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4961 ExFreePool(NameInfo2
);
4963 if (Count
== 0 && dwType
!= 5)
4965 /* Font could not be found in system table
4966 dwType == 5 will still handle this */
4967 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
4974 case 0: /* FIXME: Returns 1 or 2, don't know what this is atm */
4975 Size
= sizeof(DWORD
);
4983 if (*pdwBytes
>= Size
)
4985 *(DWORD
*)pBuffer
= Count
;
4992 case 1: /* copy the font title */
4993 /* calculate the required size */
4995 Size
+= wcslen(FamInfo
[0].EnumLogFontEx
.elfLogFont
.lfFaceName
);
4996 if (FamInfo
[0].EnumLogFontEx
.elfStyle
[0] &&
4997 _wcsicmp(FamInfo
[0].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4999 Size
+= 1 + wcslen(FamInfo
[0].EnumLogFontEx
.elfStyle
);
5001 for (i
= 1; i
< Count
; ++i
)
5003 Size
+= 3; /* " & " */
5004 Size
+= wcslen(FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
5005 if (FamInfo
[i
].EnumLogFontEx
.elfStyle
[0] &&
5006 _wcsicmp(FamInfo
[i
].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
5008 Size
+= 1 + wcslen(FamInfo
[i
].EnumLogFontEx
.elfStyle
);
5011 Size
+= 2; /* "\0\0" */
5012 Size
*= sizeof(WCHAR
);
5021 if (*pdwBytes
>= Size
)
5023 /* store font title to buffer */
5024 WCHAR
*psz
= pBuffer
;
5026 IntAddNameFromFamInfo(psz
, &FamInfo
[0]);
5027 for (i
= 1; i
< Count
; ++i
)
5029 wcscat(psz
, L
" & ");
5030 IntAddNameFromFamInfo(psz
, &FamInfo
[i
]);
5032 psz
[wcslen(psz
) + 1] = UNICODE_NULL
;
5038 *pdwBytes
= 1024; /* this is confirmed value */
5043 case 2: /* Copy an array of LOGFONTW */
5044 Size
= Count
* sizeof(LOGFONTW
);
5052 if (*pdwBytes
>= Size
)
5054 pbBuffer
= (LPBYTE
)pBuffer
;
5055 for (i
= 0; i
< Count
; ++i
)
5057 FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfWidth
= 0;
5058 RtlCopyMemory(pbBuffer
, &FamInfo
[i
].EnumLogFontEx
.elfLogFont
, sizeof(LOGFONTW
));
5059 pbBuffer
+= sizeof(LOGFONTW
);
5067 *pdwBytes
= 1024; /* this is confirmed value */
5072 Size
= sizeof(DWORD
);
5080 if (*pdwBytes
>= Size
)
5082 /* FIXME: What exactly is copied here? */
5083 *(DWORD
*)pBuffer
= 1;
5090 case 4: /* full file path */
5091 if (FileName
->Length
>= 4 * sizeof(WCHAR
))
5093 /* The beginning of FileName is \??\ */
5094 LPWSTR pch
= FileName
->Buffer
+ 4;
5095 DWORD Length
= FileName
->Length
- 4 * sizeof(WCHAR
);
5097 Size
= Length
+ sizeof(WCHAR
);
5105 if (*pdwBytes
>= Size
)
5107 RtlCopyMemory(pBuffer
, pch
, Size
);
5115 case 5: /* Looks like a BOOL that is copied, TRUE, if the font was not found */
5116 Size
= sizeof(BOOL
);
5124 if (*pdwBytes
>= Size
)
5126 *(BOOL
*)pBuffer
= Count
== 0;
5133 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
5141 ftGdiRealizationInfo(PFONTGDI Font
, PREALIZATION_INFO Info
)
5143 if (FT_HAS_FIXED_SIZES(Font
->SharedFace
->Face
))
5144 Info
->iTechnology
= RI_TECH_BITMAP
;
5147 if (FT_IS_SCALABLE(Font
->SharedFace
->Face
))
5148 Info
->iTechnology
= RI_TECH_SCALABLE
;
5150 Info
->iTechnology
= RI_TECH_FIXED
;
5152 Info
->iUniq
= Font
->FontObj
.iUniq
;
5153 Info
->dwUnknown
= -1;
5160 ftGdiGetKerningPairs( PFONTGDI Font
,
5162 LPKERNINGPAIR pKerningPair
)
5166 FT_Face face
= Font
->SharedFace
->Face
;
5168 if (FT_HAS_KERNING(face
) && face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
5170 FT_UInt previous_index
= 0, glyph_index
= 0;
5171 FT_ULong char_code
, char_previous
;
5174 char_previous
= char_code
= FT_Get_First_Char(face
, &glyph_index
);
5180 if (previous_index
&& glyph_index
)
5182 FT_Get_Kerning(face
, previous_index
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
5184 if (pKerningPair
&& cPairs
)
5186 pKerningPair
[i
].wFirst
= char_previous
;
5187 pKerningPair
[i
].wSecond
= char_code
;
5188 pKerningPair
[i
].iKernAmount
= delta
.x
;
5190 if (i
== cPairs
) break;
5194 previous_index
= glyph_index
;
5195 char_previous
= char_code
;
5196 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_index
);
5198 IntUnLockFreeType();
5204 ///////////////////////////////////////////////////////////////////////////
5206 // Functions needing sorting.
5208 ///////////////////////////////////////////////////////////////////////////
5210 NtGdiGetFontFamilyInfo(HDC Dc
,
5211 LPLOGFONTW UnsafeLogFont
,
5212 PFONTFAMILYINFO UnsafeInfo
,
5217 PFONTFAMILYINFO Info
;
5219 PPROCESSINFO Win32Process
;
5221 /* Make a safe copy */
5222 Status
= MmCopyFromCaller(&LogFont
, UnsafeLogFont
, sizeof(LOGFONTW
));
5223 if (! NT_SUCCESS(Status
))
5225 EngSetLastError(ERROR_INVALID_PARAMETER
);
5229 /* Allocate space for a safe copy */
5230 Info
= ExAllocatePoolWithTag(PagedPool
, Size
* sizeof(FONTFAMILYINFO
), GDITAG_TEXT
);
5233 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5237 /* Enumerate font families in the global list */
5238 IntLockGlobalFonts();
5240 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
, &g_FontListHead
) )
5242 IntUnLockGlobalFonts();
5243 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5246 IntUnLockGlobalFonts();
5248 /* Enumerate font families in the process local list */
5249 Win32Process
= PsGetCurrentProcessWin32Process();
5250 IntLockProcessPrivateFonts(Win32Process
);
5251 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
,
5252 &Win32Process
->PrivateFontListHead
))
5254 IntUnLockProcessPrivateFonts(Win32Process
);
5255 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5258 IntUnLockProcessPrivateFonts(Win32Process
);
5260 /* Enumerate font families in the registry */
5261 if (! GetFontFamilyInfoForSubstitutes(&LogFont
, Info
, &Count
, Size
))
5263 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5267 /* Return data to caller */
5270 Status
= MmCopyToCaller(UnsafeInfo
, Info
,
5271 (Count
< Size
? Count
: Size
) * sizeof(FONTFAMILYINFO
));
5272 if (! NT_SUCCESS(Status
))
5274 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5275 EngSetLastError(ERROR_INVALID_PARAMETER
);
5280 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5287 ScaleLong(LONG lValue
, PFLOATOBJ pef
)
5291 /* Check if we have scaling different from 1 */
5292 if (!FLOATOBJ_Equal(pef
, (PFLOATOBJ
)&gef1
))
5294 /* Need to multiply */
5295 FLOATOBJ_SetLong(&efTemp
, lValue
);
5296 FLOATOBJ_Mul(&efTemp
, pef
);
5297 lValue
= FLOATOBJ_GetLong(&efTemp
);
5310 IN OPTIONAL PRECTL lprc
,
5313 IN OPTIONAL LPINT Dx
,
5314 IN DWORD dwCodePage
)
5318 * Call EngTextOut, which does the real work (calling DrvTextOut where
5325 SURFACE
*psurf
= NULL
;
5326 int error
, glyph_index
, i
;
5329 FT_BitmapGlyph realglyph
;
5330 LONGLONG TextLeft
, RealXStart
;
5331 ULONG TextTop
, previous
, BackgroundLeft
;
5332 FT_Bool use_kerning
;
5333 RECTL DestRect
, MaskRect
;
5334 POINTL SourcePoint
, BrushOrigin
;
5335 HBITMAP HSourceGlyph
;
5336 SURFOBJ
*SourceGlyphSurf
;
5341 PTEXTOBJ TextObj
= NULL
;
5342 EXLATEOBJ exloRGB2Dst
, exloDst2RGB
;
5343 FT_Render_Mode RenderMode
;
5346 BOOL DoBreak
= FALSE
;
5348 PMATRIX pmxWorldToDevice
;
5349 LONG fixAscender
, fixDescender
;
5352 BOOL EmuBold
, EmuItalic
;
5356 /* Check if String is valid */
5357 if ((Count
> 0xFFFF) || (Count
> 0 && String
== NULL
))
5359 EngSetLastError(ERROR_INVALID_PARAMETER
);
5363 /* NOTE: This function locks the screen DC, so it must never be called
5364 with a DC already locked */
5365 Render
= IntIsFontRenderingEnabled();
5367 // TODO: Write test-cases to exactly match real Windows in different
5368 // bad parameters (e.g. does Windows check the DC or the RECT first?).
5369 dc
= DC_LockDc(hDC
);
5372 EngSetLastError(ERROR_INVALID_HANDLE
);
5376 if (PATH_IsPathOpen(dc
->dclevel
))
5378 bResult
= PATH_ExtTextOut(dc
,
5382 (const RECTL
*)lprc
,
5390 DC_vPrepareDCsForBlit(dc
, NULL
, NULL
, NULL
);
5392 if (!dc
->dclevel
.pSurface
)
5394 /* Memory DC with no surface selected */
5399 pdcattr
= dc
->pdcattr
;
5401 if (lprc
&& (fuOptions
& (ETO_OPAQUE
| ETO_CLIPPED
)))
5403 IntLPtoDP(dc
, (POINT
*)lprc
, 2);
5406 if (pdcattr
->lTextAlign
& TA_UPDATECP
)
5408 Start
.x
= pdcattr
->ptlCurrent
.x
;
5409 Start
.y
= pdcattr
->ptlCurrent
.y
;
5415 IntLPtoDP(dc
, &Start
, 1);
5416 RealXStart
= ((LONGLONG
)Start
.x
+ dc
->ptlDCOrig
.x
) << 6;
5417 YStart
= Start
.y
+ dc
->ptlDCOrig
.y
;
5426 if ((fuOptions
& ETO_OPAQUE
) && lprc
)
5428 DestRect
.left
= lprc
->left
;
5429 DestRect
.top
= lprc
->top
;
5430 DestRect
.right
= lprc
->right
;
5431 DestRect
.bottom
= lprc
->bottom
;
5433 DestRect
.left
+= dc
->ptlDCOrig
.x
;
5434 DestRect
.top
+= dc
->ptlDCOrig
.y
;
5435 DestRect
.right
+= dc
->ptlDCOrig
.x
;
5436 DestRect
.bottom
+= dc
->ptlDCOrig
.y
;
5438 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5440 IntUpdateBoundsRect(dc
, &DestRect
);
5443 if (pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
5444 DC_vUpdateBackgroundBrush(dc
);
5445 if (dc
->dctype
== DCTYPE_DIRECT
)
5446 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5448 psurf
= dc
->dclevel
.pSurface
;
5458 &dc
->eboBackground
.BrushObject
,
5460 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5462 if (dc
->dctype
== DCTYPE_DIRECT
)
5463 MouseSafetyOnDrawEnd(dc
->ppdev
);
5465 fuOptions
&= ~ETO_OPAQUE
;
5469 if (pdcattr
->jBkMode
== OPAQUE
)
5471 fuOptions
|= ETO_OPAQUE
;
5475 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
5476 if (TextObj
== NULL
)
5482 FontObj
= TextObj
->Font
;
5484 FontGDI
= ObjToGDI(FontObj
, FONT
);
5488 face
= FontGDI
->SharedFace
->Face
;
5490 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
5491 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
5492 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
5495 RenderMode
= IntGetFontRenderMode(plf
);
5497 RenderMode
= FT_RENDER_MODE_MONO
;
5499 if (!TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
))
5501 IntUnLockFreeType();
5506 /* NOTE: Don't trust face->size->metrics.ascender and descender values. */
5507 if (dc
->pdcattr
->iGraphicsMode
== GM_ADVANCED
)
5509 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
5510 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5512 fixAscender
= ScaleLong(FontGDI
->tmAscent
, &pmxWorldToDevice
->efM22
) << 6;
5513 fixDescender
= ScaleLong(FontGDI
->tmDescent
, &pmxWorldToDevice
->efM22
) << 6;
5517 pmxWorldToDevice
= (PMATRIX
)&gmxWorldToDeviceDefault
;
5518 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5520 fixAscender
= FontGDI
->tmAscent
<< 6;
5521 fixDescender
= FontGDI
->tmDescent
<< 6;
5525 * Process the vertical alignment and determine the yoff.
5527 #define VALIGN_MASK (TA_TOP | TA_BASELINE | TA_BOTTOM)
5528 if ((pdcattr
->lTextAlign
& VALIGN_MASK
) == TA_BASELINE
)
5530 else if ((pdcattr
->lTextAlign
& VALIGN_MASK
) == TA_BOTTOM
)
5531 yoff
= -(fixDescender
>> 6);
5533 yoff
= fixAscender
>> 6;
5536 use_kerning
= FT_HAS_KERNING(face
);
5540 * Process the horizontal alignment and modify XStart accordingly.
5542 DxShift
= fuOptions
& ETO_PDY
? 1 : 0;
5543 if (pdcattr
->lTextAlign
& (TA_RIGHT
| TA_CENTER
))
5545 ULONGLONG TextWidth
= 0;
5546 LPCWSTR TempText
= String
;
5550 * Calculate width of the text.
5555 iStart
= Count
< 2 ? 0 : Count
- 2;
5556 TextWidth
= Count
< 2 ? 0 : (Dx
[(Count
-2)<<DxShift
] << 6);
5562 TempText
= String
+ iStart
;
5564 for (i
= iStart
; i
< Count
; i
++)
5566 glyph_index
= get_glyph_index_flagged(face
, *TempText
, ETO_GLYPH_INDEX
, fuOptions
);
5568 if (EmuBold
|| EmuItalic
)
5571 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
5572 RenderMode
, pmxWorldToDevice
);
5575 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5578 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
5581 glyph
= face
->glyph
;
5582 if (EmuBold
|| EmuItalic
)
5585 FT_GlyphSlot_Embolden(glyph
);
5587 FT_GlyphSlot_Oblique(glyph
);
5588 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5592 realglyph
= ftGdiGlyphCacheSet(face
,
5601 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5602 IntUnLockFreeType();
5607 /* Retrieve kerning distance */
5608 if (use_kerning
&& previous
&& glyph_index
)
5611 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5612 TextWidth
+= delta
.x
;
5615 TextWidth
+= realglyph
->root
.advance
.x
>> 10;
5617 if (EmuBold
|| EmuItalic
)
5619 FT_Done_Glyph((FT_Glyph
)realglyph
);
5623 previous
= glyph_index
;
5629 if ((pdcattr
->lTextAlign
& TA_CENTER
) == TA_CENTER
)
5631 RealXStart
-= TextWidth
/ 2;
5635 RealXStart
-= TextWidth
;
5639 psurf
= dc
->dclevel
.pSurface
;
5640 SurfObj
= &psurf
->SurfObj
;
5642 if ((fuOptions
& ETO_OPAQUE
) && (dc
->pdcattr
->ulDirty_
& DIRTY_BACKGROUND
))
5643 DC_vUpdateBackgroundBrush(dc
) ;
5645 if(dc
->pdcattr
->ulDirty_
& DIRTY_TEXT
)
5646 DC_vUpdateTextBrush(dc
) ;
5648 if (!face
->units_per_EM
)
5654 thickness
= face
->underline_thickness
*
5655 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5660 if ((fuOptions
& ETO_OPAQUE
) && plf
->lfItalic
)
5662 /* Draw background */
5663 TextLeft
= RealXStart
;
5665 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5666 for (i
= 0; i
< Count
; ++i
)
5668 glyph_index
= get_glyph_index_flagged(face
, String
[i
], ETO_GLYPH_INDEX
, fuOptions
);
5670 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5673 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5674 IntUnLockFreeType();
5678 glyph
= face
->glyph
;
5680 FT_GlyphSlot_Embolden(glyph
);
5682 FT_GlyphSlot_Oblique(glyph
);
5683 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5686 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5687 IntUnLockFreeType();
5691 /* retrieve kerning distance and move pen position */
5692 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5695 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5696 TextLeft
+= delta
.x
;
5698 DPRINT("TextLeft: %I64d\n", TextLeft
);
5699 DPRINT("TextTop: %lu\n", TextTop
);
5700 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5702 DestRect
.left
= BackgroundLeft
;
5703 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5704 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5705 DestRect
.bottom
= DestRect
.top
+ ((fixAscender
+ fixDescender
) >> 6);
5706 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5707 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5709 IntUpdateBoundsRect(dc
, &DestRect
);
5720 &dc
->eboBackground
.BrushObject
,
5722 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5723 MouseSafetyOnDrawEnd(dc
->ppdev
);
5724 BackgroundLeft
= DestRect
.right
;
5726 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5727 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5728 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5729 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5731 bitSize
.cx
= realglyph
->bitmap
.width
;
5732 bitSize
.cy
= realglyph
->bitmap
.rows
;
5733 MaskRect
.right
= realglyph
->bitmap
.width
;
5734 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5738 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
5739 DPRINT("New TextLeft: %I64d\n", TextLeft
);
5743 // FIXME this should probably be a matrix transform with TextTop as well.
5744 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
5745 if (FLOATOBJ_Equal0(&Scale
))
5746 FLOATOBJ_Set1(&Scale
);
5748 /* do the shift before multiplying to preserve precision */
5749 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
5750 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
5751 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
5756 TextTop
-= Dx
[2 * i
+ 1] << 6;
5759 previous
= glyph_index
;
5761 if (EmuBold
|| EmuItalic
)
5763 FT_Done_Glyph((FT_Glyph
)realglyph
);
5769 EXLATEOBJ_vInitialize(&exloRGB2Dst
, &gpalRGB
, psurf
->ppal
, 0, 0, 0);
5770 EXLATEOBJ_vInitialize(&exloDst2RGB
, psurf
->ppal
, &gpalRGB
, 0, 0, 0);
5772 /* Assume success */
5776 * The main rendering loop.
5778 TextLeft
= RealXStart
;
5780 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5781 for (i
= 0; i
< Count
; ++i
)
5783 glyph_index
= get_glyph_index_flagged(face
, String
[i
], ETO_GLYPH_INDEX
, fuOptions
);
5785 if (EmuBold
|| EmuItalic
)
5788 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
5789 RenderMode
, pmxWorldToDevice
);
5792 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5795 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5800 glyph
= face
->glyph
;
5801 if (EmuBold
|| EmuItalic
)
5804 FT_GlyphSlot_Embolden(glyph
);
5806 FT_GlyphSlot_Oblique(glyph
);
5807 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5811 realglyph
= ftGdiGlyphCacheSet(face
,
5820 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5826 /* retrieve kerning distance and move pen position */
5827 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5830 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5831 TextLeft
+= delta
.x
;
5833 DPRINT("TextLeft: %I64d\n", TextLeft
);
5834 DPRINT("TextTop: %lu\n", TextTop
);
5835 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5837 if ((fuOptions
& ETO_OPAQUE
) && !plf
->lfItalic
)
5839 DestRect
.left
= BackgroundLeft
;
5840 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5841 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5842 DestRect
.bottom
= DestRect
.top
+ ((fixAscender
+ fixDescender
) >> 6);
5844 if (dc
->dctype
== DCTYPE_DIRECT
)
5845 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5847 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5849 IntUpdateBoundsRect(dc
, &DestRect
);
5860 &dc
->eboBackground
.BrushObject
,
5862 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5864 if (dc
->dctype
== DCTYPE_DIRECT
)
5865 MouseSafetyOnDrawEnd(dc
->ppdev
);
5867 BackgroundLeft
= DestRect
.right
;
5870 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5871 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5872 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5873 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5875 bitSize
.cx
= realglyph
->bitmap
.width
;
5876 bitSize
.cy
= realglyph
->bitmap
.rows
;
5877 MaskRect
.right
= realglyph
->bitmap
.width
;
5878 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5880 /* Check if the bitmap has any pixels */
5881 if ((bitSize
.cx
!= 0) && (bitSize
.cy
!= 0))
5884 * We should create the bitmap out of the loop at the biggest possible
5885 * glyph size. Then use memset with 0 to clear it and sourcerect to
5886 * limit the work of the transbitblt.
5888 HSourceGlyph
= EngCreateBitmap(bitSize
, realglyph
->bitmap
.pitch
,
5889 BMF_8BPP
, BMF_TOPDOWN
,
5890 realglyph
->bitmap
.buffer
);
5891 if ( !HSourceGlyph
)
5893 DPRINT1("WARNING: EngCreateBitmap() failed!\n");
5894 // FT_Done_Glyph(realglyph);
5898 SourceGlyphSurf
= EngLockSurface((HSURF
)HSourceGlyph
);
5899 if ( !SourceGlyphSurf
)
5901 EngDeleteSurface((HSURF
)HSourceGlyph
);
5902 DPRINT1("WARNING: EngLockSurface() failed!\n");
5908 * Use the font data as a mask to paint onto the DCs surface using a
5911 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5912 DestRect
.right
>= lprc
->right
+ dc
->ptlDCOrig
.x
)
5914 // We do the check '>=' instead of '>' to possibly save an iteration
5915 // through this loop, since it's breaking after the drawing is done,
5916 // and x is always incremented.
5917 DestRect
.right
= lprc
->right
+ dc
->ptlDCOrig
.x
;
5920 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5921 DestRect
.bottom
>= lprc
->bottom
+ dc
->ptlDCOrig
.y
)
5923 DestRect
.bottom
= lprc
->bottom
+ dc
->ptlDCOrig
.y
;
5926 if (dc
->dctype
== DCTYPE_DIRECT
)
5927 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5937 &dc
->eboText
.BrushObject
,
5940 DPRINT1("Failed to MaskBlt a glyph!\n");
5943 if (dc
->dctype
== DCTYPE_DIRECT
)
5944 MouseSafetyOnDrawEnd(dc
->ppdev
) ;
5946 EngUnlockSurface(SourceGlyphSurf
);
5947 EngDeleteSurface((HSURF
)HSourceGlyph
);
5955 if (plf
->lfUnderline
)
5958 if (!face
->units_per_EM
)
5964 position
= face
->underline_position
*
5965 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5967 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5971 &dc
->eboText
.BrushObject
,
5973 TextTop
+ yoff
- position
+ i
,
5974 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5975 TextTop
+ yoff
- position
+ i
,
5977 ROP2_TO_MIX(R2_COPYPEN
));
5980 if (plf
->lfStrikeOut
)
5983 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5987 &dc
->eboText
.BrushObject
,
5989 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5990 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5991 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5993 ROP2_TO_MIX(R2_COPYPEN
));
5999 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
6000 DPRINT("New TextLeft: %I64d\n", TextLeft
);
6004 // FIXME this should probably be a matrix transform with TextTop as well.
6005 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
6006 if (FLOATOBJ_Equal0(&Scale
))
6007 FLOATOBJ_Set1(&Scale
);
6009 /* do the shift before multiplying to preserve precision */
6010 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
6011 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
6012 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
6017 TextTop
-= Dx
[2 * i
+ 1] << 6;
6020 previous
= glyph_index
;
6022 if (EmuBold
|| EmuItalic
)
6024 FT_Done_Glyph((FT_Glyph
)realglyph
);
6029 if (pdcattr
->lTextAlign
& TA_UPDATECP
) {
6030 pdcattr
->ptlCurrent
.x
= DestRect
.right
- dc
->ptlDCOrig
.x
;
6033 IntUnLockFreeType();
6035 EXLATEOBJ_vCleanup(&exloRGB2Dst
);
6036 EXLATEOBJ_vCleanup(&exloDst2RGB
);
6040 DC_vFinishBlit(dc
, NULL
);
6042 if (TextObj
!= NULL
)
6043 TEXTOBJ_UnlockText(TextObj
);
6050 #define STACK_TEXT_BUFFER_SIZE 100
6058 IN OPTIONAL LPRECT UnsafeRect
,
6059 IN LPWSTR UnsafeString
,
6061 IN OPTIONAL LPINT UnsafeDx
,
6062 IN DWORD dwCodePage
)
6064 BOOL Result
= FALSE
;
6065 NTSTATUS Status
= STATUS_SUCCESS
;
6067 BYTE LocalBuffer
[STACK_TEXT_BUFFER_SIZE
];
6068 PVOID Buffer
= LocalBuffer
;
6069 LPCWSTR SafeString
= NULL
;
6070 LPINT SafeDx
= NULL
;
6071 ULONG BufSize
, StringSize
, DxSize
= 0;
6073 /* Check if String is valid */
6074 if ((Count
> 0xFFFF) || (Count
> 0 && UnsafeString
== NULL
))
6076 EngSetLastError(ERROR_INVALID_PARAMETER
);
6082 /* Calculate buffer size for string and Dx values */
6083 BufSize
= StringSize
= Count
* sizeof(WCHAR
);
6086 /* If ETO_PDY is specified, we have pairs of INTs */
6087 DxSize
= (Count
* sizeof(INT
)) * (fuOptions
& ETO_PDY
? 2 : 1);
6091 /* Check if our local buffer is large enough */
6092 if (BufSize
> STACK_TEXT_BUFFER_SIZE
)
6094 /* It's not, allocate a temp buffer */
6095 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufSize
, GDITAG_TEXT
);
6102 /* Probe and copy user mode data to the buffer */
6105 /* Put the Dx before the String to assure alignment of 4 */
6106 SafeString
= (LPCWSTR
)(((ULONG_PTR
)Buffer
) + DxSize
);
6108 /* Probe and copy the string */
6109 ProbeForRead(UnsafeString
, StringSize
, 1);
6110 memcpy((PVOID
)SafeString
, UnsafeString
, StringSize
);
6112 /* If we have Dx values... */
6115 /* ... probe and copy them */
6117 ProbeForRead(UnsafeDx
, DxSize
, 1);
6118 memcpy(SafeDx
, UnsafeDx
, DxSize
);
6121 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6123 Status
= _SEH2_GetExceptionCode();
6126 if (!NT_SUCCESS(Status
))
6132 /* If we have a rect, copy it */
6137 ProbeForRead(UnsafeRect
, sizeof(RECT
), 1);
6138 SafeRect
= *UnsafeRect
;
6140 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6142 Status
= _SEH2_GetExceptionCode();
6145 if (!NT_SUCCESS(Status
))
6151 /* Finally call the internal routine */
6152 Result
= GreExtTextOutW(hDC
,
6163 /* If we allocated a buffer, free it */
6164 if (Buffer
!= LocalBuffer
)
6166 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
6178 NtGdiGetCharABCWidthsW(
6182 IN OPTIONAL PWCHAR UnSafepwch
,
6187 LPABCFLOAT SafeBuffF
= NULL
;
6193 FT_CharMap charmap
, found
= NULL
;
6194 UINT i
, glyph_index
, BufferSize
;
6196 NTSTATUS Status
= STATUS_SUCCESS
;
6197 PMATRIX pmxWorldToDevice
;
6198 PWCHAR Safepwch
= NULL
;
6203 EngSetLastError(ERROR_INVALID_PARAMETER
);
6209 UINT pwchSize
= Count
* sizeof(WCHAR
);
6210 Safepwch
= ExAllocatePoolWithTag(PagedPool
, pwchSize
, GDITAG_TEXT
);
6214 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6220 ProbeForRead(UnSafepwch
, pwchSize
, 1);
6221 RtlCopyMemory(Safepwch
, UnSafepwch
, pwchSize
);
6223 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6225 Status
= _SEH2_GetExceptionCode();
6230 if (!NT_SUCCESS(Status
))
6233 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6235 EngSetLastError(Status
);
6239 BufferSize
= Count
* sizeof(ABC
); // Same size!
6240 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6241 if (!fl
) SafeBuffF
= (LPABCFLOAT
) SafeBuff
;
6242 if (SafeBuff
== NULL
)
6246 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6248 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6252 dc
= DC_LockDc(hDC
);
6255 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6258 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6260 EngSetLastError(ERROR_INVALID_HANDLE
);
6263 pdcattr
= dc
->pdcattr
;
6264 hFont
= pdcattr
->hlfntNew
;
6265 TextObj
= RealizeFontInit(hFont
);
6267 /* Get the DC's world-to-device transformation matrix */
6268 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6271 if (TextObj
== NULL
)
6273 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6276 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6278 EngSetLastError(ERROR_INVALID_HANDLE
);
6282 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6284 face
= FontGDI
->SharedFace
->Face
;
6285 if (face
->charmap
== NULL
)
6287 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6289 charmap
= face
->charmaps
[i
];
6290 if (charmap
->encoding
!= 0)
6299 DPRINT1("WARNING: Could not find desired charmap!\n");
6300 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6303 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6305 EngSetLastError(ERROR_INVALID_HANDLE
);
6310 FT_Set_Charmap(face
, found
);
6311 IntUnLockFreeType();
6314 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6316 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6317 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6319 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6321 int adv
, lsb
, bbx
, left
, right
;
6325 glyph_index
= get_glyph_index_flagged(face
, Safepwch
[i
- FirstChar
], GCABCW_INDICES
, fl
);
6329 glyph_index
= get_glyph_index_flagged(face
, i
, GCABCW_INDICES
, fl
);
6331 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6333 left
= (INT
)face
->glyph
->metrics
.horiBearingX
& -64;
6334 right
= (INT
)((face
->glyph
->metrics
.horiBearingX
+ face
->glyph
->metrics
.width
) + 63) & -64;
6335 adv
= (face
->glyph
->advance
.x
+ 32) >> 6;
6337 // int test = (INT)(face->glyph->metrics.horiAdvance + 63) >> 6;
6338 // DPRINT1("Advance Wine %d and Advance Ros %d\n",test, adv ); /* It's the same! */
6341 bbx
= (right
- left
) >> 6;
6343 DPRINT1("lsb %d and bbx %d\n", lsb, bbx );
6347 SafeBuffF
[i
- FirstChar
].abcfA
= (FLOAT
) lsb
;
6348 SafeBuffF
[i
- FirstChar
].abcfB
= (FLOAT
) bbx
;
6349 SafeBuffF
[i
- FirstChar
].abcfC
= (FLOAT
) (adv
- lsb
- bbx
);
6353 SafeBuff
[i
- FirstChar
].abcA
= lsb
;
6354 SafeBuff
[i
- FirstChar
].abcB
= bbx
;
6355 SafeBuff
[i
- FirstChar
].abcC
= adv
- lsb
- bbx
;
6358 IntUnLockFreeType();
6359 TEXTOBJ_UnlockText(TextObj
);
6360 Status
= MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6362 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6365 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6367 if (! NT_SUCCESS(Status
))
6369 SetLastNtError(Status
);
6373 DPRINT("NtGdiGetCharABCWidths Worked!\n");
6386 IN OPTIONAL PWCHAR UnSafepwc
,
6390 NTSTATUS Status
= STATUS_SUCCESS
;
6392 PFLOAT SafeBuffF
= NULL
;
6398 FT_CharMap charmap
, found
= NULL
;
6399 UINT i
, glyph_index
, BufferSize
;
6401 PMATRIX pmxWorldToDevice
;
6402 PWCHAR Safepwc
= NULL
;
6407 UINT pwcSize
= Count
* sizeof(WCHAR
);
6408 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6412 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6417 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6418 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6420 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6422 Status
= _SEH2_GetExceptionCode();
6427 if (!NT_SUCCESS(Status
))
6429 EngSetLastError(Status
);
6433 BufferSize
= Count
* sizeof(INT
); // Same size!
6434 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6435 if (!fl
) SafeBuffF
= (PFLOAT
) SafeBuff
;
6436 if (SafeBuff
== NULL
)
6439 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6441 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6445 dc
= DC_LockDc(hDC
);
6449 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6451 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6452 EngSetLastError(ERROR_INVALID_HANDLE
);
6455 pdcattr
= dc
->pdcattr
;
6456 hFont
= pdcattr
->hlfntNew
;
6457 TextObj
= RealizeFontInit(hFont
);
6458 /* Get the DC's world-to-device transformation matrix */
6459 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6462 if (TextObj
== NULL
)
6465 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6467 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6468 EngSetLastError(ERROR_INVALID_HANDLE
);
6472 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6474 face
= FontGDI
->SharedFace
->Face
;
6475 if (face
->charmap
== NULL
)
6477 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6479 charmap
= face
->charmaps
[i
];
6480 if (charmap
->encoding
!= 0)
6489 DPRINT1("WARNING: Could not find desired charmap!\n");
6492 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6494 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6495 EngSetLastError(ERROR_INVALID_HANDLE
);
6500 FT_Set_Charmap(face
, found
);
6501 IntUnLockFreeType();
6504 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6506 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6507 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6509 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6513 glyph_index
= get_glyph_index_flagged(face
, Safepwc
[i
- FirstChar
], GCW_INDICES
, fl
);
6517 glyph_index
= get_glyph_index_flagged(face
, i
, GCW_INDICES
, fl
);
6519 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6521 SafeBuffF
[i
- FirstChar
] = (FLOAT
) ((face
->glyph
->advance
.x
+ 32) >> 6);
6523 SafeBuff
[i
- FirstChar
] = (face
->glyph
->advance
.x
+ 32) >> 6;
6525 IntUnLockFreeType();
6526 TEXTOBJ_UnlockText(TextObj
);
6527 MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6530 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6532 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6540 // TODO: Move this code into NtGdiGetGlyphIndicesWInternal and wrap
6541 // NtGdiGetGlyphIndicesW around NtGdiGetGlyphIndicesWInternal instead.
6542 // NOTE: See also GreGetGlyphIndicesW.
6547 NtGdiGetGlyphIndicesW(
6549 _In_reads_opt_(cwc
) LPCWSTR pwc
,
6551 _Out_writes_opt_(cwc
) LPWORD pgi
,
6559 NTSTATUS Status
= STATUS_SUCCESS
;
6560 OUTLINETEXTMETRICW
*potm
;
6562 WCHAR DefChar
= 0xffff;
6563 PWSTR Buffer
= NULL
;
6564 ULONG Size
, pwcSize
;
6565 PWSTR Safepwc
= NULL
;
6566 LPCWSTR UnSafepwc
= pwc
;
6567 LPWORD UnSafepgi
= pgi
;
6569 /* Check for integer overflow */
6570 if (cwc
& 0x80000000) // (INT_MAX + 1) == INT_MIN
6573 if (!UnSafepwc
&& !UnSafepgi
)
6576 if (!UnSafepwc
|| !UnSafepgi
)
6578 DPRINT1("UnSafepwc == %p, UnSafepgi = %p\n", UnSafepwc
, UnSafepgi
);
6582 // TODO: Special undocumented case!
6583 if (!pwc
&& !pgi
&& (cwc
== 0))
6585 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (!pwc && !pgi && (cwc == 0)) is UNIMPLEMENTED!\n");
6589 // FIXME: This is a hack!! (triggered by e.g. Word 2010). See CORE-12825
6592 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (cwc == 0) is UNIMPLEMENTED!\n");
6596 dc
= DC_LockDc(hdc
);
6601 pdcattr
= dc
->pdcattr
;
6602 hFont
= pdcattr
->hlfntNew
;
6603 TextObj
= RealizeFontInit(hFont
);
6610 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6611 TEXTOBJ_UnlockText(TextObj
);
6613 Buffer
= ExAllocatePoolWithTag(PagedPool
, cwc
*sizeof(WORD
), GDITAG_TEXT
);
6619 if (iMode
& GGI_MARK_NONEXISTING_GLYPHS
)
6625 FT_Face Face
= FontGDI
->SharedFace
->Face
;
6626 if (FT_IS_SFNT(Face
))
6628 TT_OS2
*pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
6629 DefChar
= (pOS2
->usDefaultChar
? get_glyph_index(Face
, pOS2
->usDefaultChar
) : 0);
6633 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
6634 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
6640 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
6642 DefChar
= potm
->otmTextMetrics
.tmDefaultChar
;
6643 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
6647 pwcSize
= cwc
* sizeof(WCHAR
);
6648 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6652 Status
= STATUS_NO_MEMORY
;
6658 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6659 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6661 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6663 Status
= _SEH2_GetExceptionCode();
6667 if (!NT_SUCCESS(Status
)) goto ErrorRet
;
6671 for (i
= 0; i
< cwc
; i
++)
6673 Buffer
[i
] = get_glyph_index(FontGDI
->SharedFace
->Face
, Safepwc
[i
]);
6676 Buffer
[i
] = DefChar
;
6680 IntUnLockFreeType();
6684 ProbeForWrite(UnSafepgi
, cwc
* sizeof(WORD
), 1);
6685 RtlCopyMemory(UnSafepgi
, Buffer
, cwc
* sizeof(WORD
));
6687 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6689 Status
= _SEH2_GetExceptionCode();
6694 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
6695 if (Safepwc
!= NULL
)
6697 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6699 if (NT_SUCCESS(Status
)) return cwc
;