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}} }
164 static RTL_STATIC_LIST_HEAD(g_FontSubstListHead
);
167 SharedMem_AddRef(PSHARED_MEM Ptr
)
169 ASSERT_FREETYPE_LOCK_HELD();
175 SharedFaceCache_Init(PSHARED_FACE_CACHE Cache
)
177 Cache
->OutlineRequiredSize
= 0;
178 RtlInitUnicodeString(&Cache
->FontFamily
, NULL
);
179 RtlInitUnicodeString(&Cache
->FullName
, NULL
);
183 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
186 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
191 Ptr
->Memory
= Memory
;
192 SharedFaceCache_Init(&Ptr
->EnglishUS
);
193 SharedFaceCache_Init(&Ptr
->UserLanguage
);
195 SharedMem_AddRef(Memory
);
196 DPRINT("Creating SharedFace for %s\n", Face
->family_name
? Face
->family_name
: "<NULL>");
202 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
205 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
208 Ptr
->Buffer
= Buffer
;
209 Ptr
->BufferSize
= BufferSize
;
211 Ptr
->IsMapping
= IsMapping
;
212 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
218 SharedFace_AddRef(PSHARED_FACE Ptr
)
220 ASSERT_FREETYPE_LOCK_HELD();
226 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
228 ASSERT_FREETYPE_LOCK_HELD();
230 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
231 RemoveEntryList(&Entry
->ListEntry
);
232 ExFreePoolWithTag(Entry
, TAG_FONT
);
233 g_FontCacheNumEntries
--;
234 ASSERT(g_FontCacheNumEntries
<= MAX_FONT_CACHE
);
238 RemoveCacheEntries(FT_Face Face
)
240 PLIST_ENTRY CurrentEntry
;
241 PFONT_CACHE_ENTRY FontEntry
;
243 ASSERT_FREETYPE_LOCK_HELD();
245 CurrentEntry
= g_FontCacheListHead
.Flink
;
246 while (CurrentEntry
!= &g_FontCacheListHead
)
248 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
249 CurrentEntry
= CurrentEntry
->Flink
;
251 if (FontEntry
->Face
== Face
)
253 RemoveCachedEntry(FontEntry
);
258 static void SharedMem_Release(PSHARED_MEM Ptr
)
260 ASSERT_FREETYPE_LOCK_HELD();
261 ASSERT(Ptr
->RefCount
> 0);
263 if (Ptr
->RefCount
<= 0)
267 if (Ptr
->RefCount
== 0)
269 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
271 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
273 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
274 ExFreePoolWithTag(Ptr
, TAG_FONT
);
279 SharedFaceCache_Release(PSHARED_FACE_CACHE Cache
)
281 RtlFreeUnicodeString(&Cache
->FontFamily
);
282 RtlFreeUnicodeString(&Cache
->FullName
);
286 SharedFace_Release(PSHARED_FACE Ptr
)
289 ASSERT(Ptr
->RefCount
> 0);
291 if (Ptr
->RefCount
<= 0)
295 if (Ptr
->RefCount
== 0)
297 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
? Ptr
->Face
->family_name
: "<NULL>");
298 RemoveCacheEntries(Ptr
->Face
);
299 FT_Done_Face(Ptr
->Face
);
300 SharedMem_Release(Ptr
->Memory
);
301 SharedFaceCache_Release(&Ptr
->EnglishUS
);
302 SharedFaceCache_Release(&Ptr
->UserLanguage
);
303 ExFreePoolWithTag(Ptr
, TAG_FONT
);
310 * IntLoadFontSubstList --- loads the list of font substitutes
313 IntLoadFontSubstList(PLIST_ENTRY pHead
)
317 OBJECT_ATTRIBUTES ObjectAttributes
;
318 KEY_FULL_INFORMATION KeyFullInfo
;
320 UNICODE_STRING FromW
, ToW
;
321 BYTE InfoBuffer
[128];
322 PKEY_VALUE_FULL_INFORMATION pInfo
;
323 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
325 PFONTSUBST_ENTRY pEntry
;
328 /* the FontSubstitutes registry key */
329 static UNICODE_STRING FontSubstKey
=
330 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
331 L
"Microsoft\\Windows NT\\CurrentVersion\\"
334 /* open registry key */
335 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
336 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
338 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
339 if (!NT_SUCCESS(Status
))
341 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
342 return FALSE
; /* failure */
345 /* query count of values */
346 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
347 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
348 if (!NT_SUCCESS(Status
))
350 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
352 return FALSE
; /* failure */
356 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
359 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
360 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
361 if (!NT_SUCCESS(Status
))
363 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
367 /* create FromW string */
368 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
369 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
370 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
371 Success
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
374 Status
= STATUS_INSUFFICIENT_RESOURCES
;
375 DPRINT("RtlCreateUnicodeString failed\n");
380 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
381 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
382 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
383 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
385 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
386 RtlFreeUnicodeString(&FromW
);
390 /* create ToW string */
391 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
392 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
393 pch
[Length
] = UNICODE_NULL
; /* truncate */
394 Success
= RtlCreateUnicodeString(&ToW
, pch
);
397 Status
= STATUS_INSUFFICIENT_RESOURCES
;
398 DPRINT("RtlCreateUnicodeString failed\n");
399 RtlFreeUnicodeString(&FromW
);
403 /* does charset exist? (from) */
404 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
405 pch
= wcsrchr(FromW
.Buffer
, L
',');
410 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
411 /* parse charset number */
412 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
415 /* does charset exist? (to) */
416 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
417 pch
= wcsrchr(ToW
.Buffer
, L
',');
422 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
423 /* parse charset number */
424 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
427 /* allocate an entry */
428 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
431 DPRINT("ExAllocatePoolWithTag failed\n");
432 RtlFreeUnicodeString(&FromW
);
433 RtlFreeUnicodeString(&ToW
);
437 /* store to *pEntry */
438 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
439 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
440 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
441 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
443 /* insert pEntry to *pHead */
444 InsertTailList(pHead
, &pEntry
->ListEntry
);
450 return NT_SUCCESS(Status
);
454 InitFontSupport(VOID
)
458 InitializeListHead(&g_FontListHead
);
459 InitializeListHead(&g_FontCacheListHead
);
460 g_FontCacheNumEntries
= 0;
461 /* Fast Mutexes must be allocated from non paged pool */
462 g_FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
463 if (g_FontListLock
== NULL
)
468 ExInitializeFastMutex(g_FontListLock
);
469 g_FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
470 if (g_FreeTypeLock
== NULL
)
474 ExInitializeFastMutex(g_FreeTypeLock
);
476 ulError
= FT_Init_FreeType(&g_FreeTypeLibrary
);
479 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
483 IntLoadSystemFonts();
484 IntLoadFontSubstList(&g_FontSubstListHead
);
490 FtSetCoordinateTransform(
497 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
499 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
500 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
503 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
504 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
507 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
508 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
511 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
512 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
514 /* Set the transformation matrix */
515 FT_Set_Transform(face
, &ftmatrix
, 0);
519 SubstituteFontByList(PLIST_ENTRY pHead
,
520 PUNICODE_STRING pOutputName
,
521 PUNICODE_STRING pInputName
,
522 BYTE RequestedCharSet
,
523 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
525 PLIST_ENTRY pListEntry
;
526 PFONTSUBST_ENTRY pSubstEntry
;
527 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
529 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
530 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
532 /* for each list entry */
533 for (pListEntry
= pHead
->Flink
;
535 pListEntry
= pListEntry
->Flink
)
538 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
540 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
542 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
543 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
545 continue; /* not matched */
548 /* does charset number exist? (to) */
549 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
551 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
555 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
558 /* does font name match? */
559 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
562 continue; /* not matched */
565 /* update *pOutputName */
566 *pOutputName
= pSubstEntry
->FontNames
[FONTSUBST_TO
];
568 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
570 /* update CharSetMap */
571 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
572 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
574 return TRUE
; /* success */
581 SubstituteFontRecurse(LOGFONTW
* pLogFont
)
583 UINT RecurseCount
= 5;
584 UNICODE_STRING OutputNameW
= { 0 };
585 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
587 UNICODE_STRING InputNameW
;
589 if (pLogFont
->lfFaceName
[0] == UNICODE_NULL
)
592 RtlInitUnicodeString(&InputNameW
, pLogFont
->lfFaceName
);
594 while (RecurseCount
-- > 0)
596 Found
= SubstituteFontByList(&g_FontSubstListHead
,
597 &OutputNameW
, &InputNameW
,
598 pLogFont
->lfCharSet
, CharSetMap
);
602 RtlStringCchCopyW(pLogFont
->lfFaceName
, LF_FACESIZE
, OutputNameW
.Buffer
);
604 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
605 CharSetMap
[FONTSUBST_FROM
] == pLogFont
->lfCharSet
)
607 pLogFont
->lfCharSet
= CharSetMap
[FONTSUBST_TO
];
611 return TRUE
; /* success */
617 * Search the system font directory and adds each font found.
620 IntLoadSystemFonts(VOID
)
622 OBJECT_ATTRIBUTES ObjectAttributes
;
623 UNICODE_STRING Directory
, FileName
, TempString
;
624 IO_STATUS_BLOCK Iosb
;
627 PFILE_DIRECTORY_INFORMATION DirInfo
;
628 BOOLEAN bRestartScan
= TRUE
;
631 static UNICODE_STRING SearchPatterns
[] =
633 RTL_CONSTANT_STRING(L
"*.ttf"),
634 RTL_CONSTANT_STRING(L
"*.ttc"),
635 RTL_CONSTANT_STRING(L
"*.otf"),
636 RTL_CONSTANT_STRING(L
"*.otc"),
637 RTL_CONSTANT_STRING(L
"*.fon"),
638 RTL_CONSTANT_STRING(L
"*.fnt")
641 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
643 InitializeObjectAttributes(
646 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
652 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
655 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
656 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
658 if (NT_SUCCESS(Status
))
660 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
662 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
663 if (DirInfoBuffer
== NULL
)
669 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
670 if (FileName
.Buffer
== NULL
)
672 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
677 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
681 Status
= ZwQueryDirectoryFile(
689 FileDirectoryInformation
,
694 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
699 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
702 TempString
.Buffer
= DirInfo
->FileName
;
704 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
705 RtlCopyUnicodeString(&FileName
, &Directory
);
706 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
707 IntGdiAddFontResource(&FileName
, 0);
708 if (DirInfo
->NextEntryOffset
== 0)
710 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
713 bRestartScan
= FALSE
;
716 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
717 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
724 ItalicFromStyle(const char *style_name
)
726 if (style_name
== NULL
|| style_name
[0] == 0)
728 if (strstr(style_name
, "Italic") != NULL
)
730 if (strstr(style_name
, "Oblique") != NULL
)
736 WeightFromStyle(const char *style_name
)
738 if (style_name
== NULL
|| style_name
[0] == 0)
740 if (strstr(style_name
, "Regular") != NULL
)
742 if (strstr(style_name
, "Normal") != NULL
)
744 if (strstr(style_name
, "SemiBold") != NULL
)
746 if (strstr(style_name
, "UltraBold") != NULL
)
748 if (strstr(style_name
, "DemiBold") != NULL
)
750 if (strstr(style_name
, "ExtraBold") != NULL
)
752 if (strstr(style_name
, "Bold") != NULL
)
754 if (strstr(style_name
, "UltraLight") != NULL
)
755 return FW_ULTRALIGHT
;
756 if (strstr(style_name
, "ExtraLight") != NULL
)
757 return FW_EXTRALIGHT
;
758 if (strstr(style_name
, "Light") != NULL
)
760 if (strstr(style_name
, "Hairline") != NULL
)
762 if (strstr(style_name
, "Book") != NULL
)
764 if (strstr(style_name
, "ExtraBlack") != NULL
)
766 if (strstr(style_name
, "UltraBlack") != NULL
)
768 if (strstr(style_name
, "Black") != NULL
)
770 if (strstr(style_name
, "Medium") != NULL
)
772 if (strstr(style_name
, "Thin") != NULL
)
774 if (strstr(style_name
, "Heavy") != NULL
)
780 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
781 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
785 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
789 ANSI_STRING AnsiFaceName
;
790 FT_WinFNT_HeaderRec WinFNT
;
791 INT FaceCount
= 0, CharSetCount
= 0;
792 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
793 DWORD Characteristics
= pLoadFont
->Characteristics
;
794 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
797 FT_UShort os2_version
;
798 FT_ULong os2_ulCodePageRange1
;
799 FT_UShort os2_usWeightClass
;
801 if (SharedFace
== NULL
&& CharSetIndex
== -1)
803 /* load a face from memory */
805 Error
= FT_New_Memory_Face(
807 pLoadFont
->Memory
->Buffer
,
808 pLoadFont
->Memory
->BufferSize
,
809 ((FontIndex
!= -1) ? FontIndex
: 0),
813 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
817 if (!Error
&& FT_IS_SFNT(Face
))
818 pLoadFont
->IsTrueType
= TRUE
;
820 if (Error
|| SharedFace
== NULL
)
823 SharedFace_Release(SharedFace
);
825 if (Error
== FT_Err_Unknown_File_Format
)
826 DPRINT1("Unknown font file format\n");
828 DPRINT1("Error reading font (error code: %d)\n", Error
);
829 return 0; /* failure */
834 Face
= SharedFace
->Face
;
836 SharedFace_AddRef(SharedFace
);
840 /* allocate a FONT_ENTRY */
841 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
844 SharedFace_Release(SharedFace
);
845 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
846 return 0; /* failure */
849 /* allocate a FONTGDI */
850 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
853 SharedFace_Release(SharedFace
);
854 ExFreePoolWithTag(Entry
, TAG_FONT
);
855 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
856 return 0; /* failure */
862 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
863 pFileName
->Length
+ sizeof(UNICODE_NULL
),
865 if (FontGDI
->Filename
== NULL
)
868 SharedFace_Release(SharedFace
);
869 ExFreePoolWithTag(Entry
, TAG_FONT
);
870 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
871 return 0; /* failure */
873 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
874 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
878 FontGDI
->Filename
= NULL
;
880 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
883 if (FontGDI
->Filename
)
884 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
886 SharedFace_Release(SharedFace
);
887 ExFreePoolWithTag(Entry
, TAG_FONT
);
891 PrivateEntry
->Entry
= Entry
;
892 if (pLoadFont
->PrivateEntry
)
894 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
898 InitializeListHead(&PrivateEntry
->ListEntry
);
899 pLoadFont
->PrivateEntry
= PrivateEntry
;
904 FontGDI
->SharedFace
= SharedFace
;
905 FontGDI
->CharSet
= ANSI_CHARSET
;
906 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
907 FontGDI
->RequestItalic
= FALSE
;
908 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
909 FontGDI
->RequestWeight
= FW_NORMAL
;
911 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
912 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
913 if (!NT_SUCCESS(Status
))
917 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
919 pLoadFont
->PrivateEntry
= NULL
;
923 RemoveEntryList(&PrivateEntry
->ListEntry
);
925 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
927 if (FontGDI
->Filename
)
928 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
930 SharedFace_Release(SharedFace
);
931 ExFreePoolWithTag(Entry
, TAG_FONT
);
937 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
940 os2_version
= pOS2
->version
;
941 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
942 os2_usWeightClass
= pOS2
->usWeightClass
;
946 if (pOS2
&& os2_version
>= 1)
948 /* get charset and weight from OS/2 header */
950 /* Make sure we do not use this pointer anymore */
953 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
955 if (os2_ulCodePageRange1
& (1 << BitIndex
))
957 if (g_FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
960 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
961 CharSetIndex
== CharSetCount
)
963 FontGDI
->CharSet
= g_FontTci
[BitIndex
].ciCharset
;
970 /* set actual weight */
971 FontGDI
->OriginalWeight
= os2_usWeightClass
;
975 /* get charset from WinFNT header */
977 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
980 FontGDI
->CharSet
= WinFNT
.charset
;
985 /* FIXME: CharSet is invalid on Marlett */
986 if (RtlEqualUnicodeString(&Entry
->FaceName
, &g_MarlettW
, TRUE
))
988 FontGDI
->CharSet
= SYMBOL_CHARSET
;
992 DPRINT("Font loaded: %s (%s)\n",
993 Face
->family_name
? Face
->family_name
: "<NULL>",
994 Face
->style_name
? Face
->style_name
: "<NULL>");
995 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
996 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
998 /* Add this font resource to the font table */
999 Entry
->Font
= FontGDI
;
1000 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1002 if (Characteristics
& FR_PRIVATE
)
1005 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1006 IntLockProcessPrivateFonts(Win32Process
);
1007 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1008 IntUnLockProcessPrivateFonts(Win32Process
);
1013 IntLockGlobalFonts();
1014 InsertTailList(&g_FontListHead
, &Entry
->ListEntry
);
1015 IntUnLockGlobalFonts();
1018 if (FontIndex
== -1)
1020 if (FT_IS_SFNT(Face
))
1022 TT_Face TrueType
= (TT_Face
)Face
;
1023 if (TrueType
->ttc_header
.count
> 1)
1026 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1028 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1035 if (CharSetIndex
== -1)
1039 if (pLoadFont
->RegValueName
.Length
== 0)
1041 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1045 UNICODE_STRING NewString
;
1046 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1047 NewString
.Length
= 0;
1048 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1049 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1050 NewString
.MaximumLength
,
1052 NewString
.Buffer
[0] = UNICODE_NULL
;
1054 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1055 RtlAppendUnicodeToString(&NewString
, L
" & ");
1056 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1058 RtlFreeUnicodeString(pValueName
);
1059 *pValueName
= NewString
;
1062 for (i
= 1; i
< CharSetCount
; ++i
)
1064 /* Do not count charsets towards 'faces' loaded */
1065 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1069 return FaceCount
; /* number of loaded faces */
1073 * IntGdiAddFontResource
1075 * Adds the font resource from the specified file to the system.
1079 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1083 PVOID Buffer
= NULL
;
1084 IO_STATUS_BLOCK Iosb
;
1085 PVOID SectionObject
;
1086 SIZE_T ViewSize
= 0;
1087 LARGE_INTEGER SectionSize
;
1088 OBJECT_ATTRIBUTES ObjectAttributes
;
1089 GDI_LOAD_FONT LoadFont
;
1092 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1094 /* Open the font file */
1095 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1096 Status
= ZwOpenFile(
1098 FILE_GENERIC_READ
| SYNCHRONIZE
,
1102 FILE_SYNCHRONOUS_IO_NONALERT
);
1103 if (!NT_SUCCESS(Status
))
1105 DPRINT("Could not load font file: %wZ\n", FileName
);
1109 SectionSize
.QuadPart
= 0LL;
1110 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1111 NULL
, &SectionSize
, PAGE_READONLY
,
1112 SEC_COMMIT
, FileHandle
, NULL
);
1113 if (!NT_SUCCESS(Status
))
1115 DPRINT("Could not map file: %wZ\n", FileName
);
1116 ZwClose(FileHandle
);
1119 ZwClose(FileHandle
);
1121 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1122 if (!NT_SUCCESS(Status
))
1124 DPRINT("Could not map file: %wZ\n", FileName
);
1125 ObDereferenceObject(SectionObject
);
1129 LoadFont
.pFileName
= FileName
;
1130 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1131 LoadFont
.Characteristics
= Characteristics
;
1132 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1133 LoadFont
.IsTrueType
= FALSE
;
1134 LoadFont
.PrivateEntry
= NULL
;
1135 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1137 ObDereferenceObject(SectionObject
);
1139 /* Release our copy */
1141 SharedMem_Release(LoadFont
.Memory
);
1142 IntUnLockFreeType();
1146 if (LoadFont
.IsTrueType
)
1148 /* append " (TrueType)" */
1149 UNICODE_STRING NewString
;
1152 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1153 NewString
.Length
= 0;
1154 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1155 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1156 NewString
.MaximumLength
,
1158 NewString
.Buffer
[0] = UNICODE_NULL
;
1160 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1161 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1162 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1163 LoadFont
.RegValueName
= NewString
;
1167 InitializeObjectAttributes(&ObjectAttributes
, &g_FontRegPath
,
1168 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1170 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1171 if (NT_SUCCESS(Status
))
1174 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1178 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1179 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1180 pFileName
, DataSize
);
1185 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1191 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1193 GDI_LOAD_FONT LoadFont
;
1194 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1198 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1205 memcpy(BufferCopy
, Buffer
, dwSize
);
1207 LoadFont
.pFileName
= NULL
;
1208 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1209 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1210 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1211 LoadFont
.IsTrueType
= FALSE
;
1212 LoadFont
.PrivateEntry
= NULL
;
1213 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1215 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1217 /* Release our copy */
1219 SharedMem_Release(LoadFont
.Memory
);
1220 IntUnLockFreeType();
1224 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1225 if (EntryCollection
)
1227 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1228 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1229 IntLockProcessPrivateFonts(Win32Process
);
1230 EntryCollection
->Handle
= ULongToHandle(++Win32Process
->PrivateMemFontHandleCount
);
1231 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1232 IntUnLockProcessPrivateFonts(Win32Process
);
1233 Ret
= EntryCollection
->Handle
;
1236 *pNumAdded
= FaceCount
;
1241 // FIXME: Add RemoveFontResource
1243 static VOID FASTCALL
1244 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1246 PFONTGDI FontGDI
= FontEntry
->Font
;
1247 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1249 if (FontGDI
->Filename
)
1250 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1252 EngFreeMem(FontGDI
);
1253 SharedFace_Release(SharedFace
);
1254 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1258 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1261 PFONT_ENTRY_MEM FontEntry
;
1263 while (!IsListEmpty(&Head
->ListEntry
))
1265 Entry
= RemoveHeadList(&Head
->ListEntry
);
1266 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1268 CleanupFontEntry(FontEntry
->Entry
);
1269 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1272 CleanupFontEntry(Head
->Entry
);
1273 ExFreePoolWithTag(Head
, TAG_FONT
);
1276 static VOID FASTCALL
1277 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1279 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1280 PLIST_ENTRY ListEntry
;
1281 RemoveEntryList(&Collection
->ListEntry
);
1284 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1285 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1287 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1288 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1290 } while (FontMemEntry
!= Collection
->Entry
);
1294 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1297 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1298 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1299 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1301 IntLockProcessPrivateFonts(Win32Process
);
1302 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1303 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1305 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1307 if (CurrentEntry
->Handle
== hMMFont
)
1309 EntryCollection
= CurrentEntry
;
1310 UnlinkFontMemCollection(CurrentEntry
);
1314 Entry
= Entry
->Flink
;
1316 IntUnLockProcessPrivateFonts(Win32Process
);
1318 if (EntryCollection
)
1320 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1321 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1329 IntGdiCleanupPrivateFontsForProcess(VOID
)
1331 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1333 PFONT_ENTRY_COLL_MEM EntryCollection
;
1335 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1338 EntryCollection
= NULL
;
1340 IntLockProcessPrivateFonts(Win32Process
);
1341 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1343 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1344 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1345 UnlinkFontMemCollection(EntryCollection
);
1347 IntUnLockProcessPrivateFonts(Win32Process
);
1349 if (EntryCollection
)
1351 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1352 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1356 /* No Mem fonts anymore, see if we have any other private fonts left */
1358 IntLockProcessPrivateFonts(Win32Process
);
1359 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1361 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1363 IntUnLockProcessPrivateFonts(Win32Process
);
1367 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1375 IntIsFontRenderingEnabled(VOID
)
1377 BOOL Ret
= g_RenderingEnabled
;
1380 hDC
= IntGetScreenDC();
1382 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && g_RenderingEnabled
;
1388 IntEnableFontRendering(BOOL Enable
)
1390 g_RenderingEnabled
= Enable
;
1393 FT_Render_Mode FASTCALL
1394 IntGetFontRenderMode(LOGFONTW
*logfont
)
1396 switch (logfont
->lfQuality
)
1398 case ANTIALIASED_QUALITY
:
1400 case NONANTIALIASED_QUALITY
:
1401 return FT_RENDER_MODE_MONO
;
1403 return FT_RENDER_MODE_LIGHT
;
1404 /* case CLEARTYPE_QUALITY:
1405 return FT_RENDER_MODE_LCD; */
1407 return FT_RENDER_MODE_NORMAL
;
1412 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1417 plfont
= LFONT_AllocFontWithHandle();
1420 return STATUS_NO_MEMORY
;
1423 ExInitializePushLock(&plfont
->lock
);
1424 *NewFont
= plfont
->BaseObject
.hHmgr
;
1425 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1426 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1427 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1429 /* This should really depend on whether GM_ADVANCED is set */
1430 plf
->lfOrientation
= plf
->lfEscapement
;
1432 LFONT_UnlockFont(plfont
);
1434 return STATUS_SUCCESS
;
1437 /*************************************************************************
1438 * TranslateCharsetInfo
1440 * Fills a CHARSETINFO structure for a character set, code page, or
1441 * font. This allows making the correspondance between different labelings
1442 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1443 * of the same encoding.
1445 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1446 * only one codepage should be set in *Src.
1449 * TRUE on success, FALSE on failure.
1452 static BOOLEAN APIENTRY
1453 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1454 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1455 if flags == TCI_SRCCHARSET: a character set value
1456 if flags == TCI_SRCCODEPAGE: a code page value */
1457 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1458 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1464 case TCI_SRCFONTSIG
:
1465 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1470 case TCI_SRCCODEPAGE
:
1471 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciACP
)
1476 case TCI_SRCCHARSET
:
1477 while (Index
< MAXTCIINDEX
&& *Src
!= g_FontTci
[Index
].ciCharset
)
1486 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== g_FontTci
[Index
].ciCharset
)
1491 RtlCopyMemory(Cs
, &g_FontTci
[Index
], sizeof(CHARSETINFO
));
1497 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1501 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1503 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1510 static void FASTCALL
1511 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1512 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1513 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1515 FT_Fixed XScale
, YScale
;
1516 int Ascent
, Descent
;
1517 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1519 XScale
= Face
->size
->metrics
.x_scale
;
1520 YScale
= Face
->size
->metrics
.y_scale
;
1524 TM
->tmHeight
= pFNT
->pixel_height
;
1525 TM
->tmAscent
= pFNT
->ascent
;
1526 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1527 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1528 TM
->tmExternalLeading
= pFNT
->external_leading
;
1529 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1530 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1532 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1533 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1534 TM
->tmFirstChar
= pFNT
->first_char
;
1535 TM
->tmLastChar
= pFNT
->last_char
;
1536 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1537 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1538 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1541 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1542 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1543 TM
->tmUnderlined
= pFNT
->underline
;
1544 TM
->tmStruckOut
= pFNT
->strike_out
;
1545 TM
->tmCharSet
= pFNT
->charset
;
1549 TM
->tmWeight
= FontGDI
->RequestWeight
;
1550 TM
->tmItalic
= FontGDI
->RequestItalic
;
1551 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1552 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1553 TM
->tmCharSet
= FontGDI
->CharSet
;
1558 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1560 Ascent
= pHori
->Ascender
;
1561 Descent
= -pHori
->Descender
;
1565 Ascent
= pOS2
->usWinAscent
;
1566 Descent
= pOS2
->usWinDescent
;
1569 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1570 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1571 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1572 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1573 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1574 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1576 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1578 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1581 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1583 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1584 - ((Ascent
+ Descent
)
1585 - (pHori
->Ascender
- pHori
->Descender
)),
1586 YScale
) + 32) >> 6);
1588 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1589 if (TM
->tmAveCharWidth
== 0)
1591 TM
->tmAveCharWidth
= 1;
1594 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1595 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1599 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1603 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1604 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1606 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1610 TM
->tmWeight
= FontGDI
->RequestWeight
;
1615 TM
->tmDigitizedAspectX
= 96;
1616 TM
->tmDigitizedAspectY
= 96;
1617 if (face_has_symbol_charmap(Face
) ||
1618 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1620 USHORT cpOEM
, cpAnsi
;
1622 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1623 TM
->tmFirstChar
= 0;
1626 case 1257: /* Baltic */
1627 TM
->tmLastChar
= 0xf8fd;
1630 TM
->tmLastChar
= 0xf0ff;
1632 TM
->tmBreakChar
= 0x20;
1633 TM
->tmDefaultChar
= 0x1f;
1637 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1638 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1640 if(pOS2
->usFirstCharIndex
<= 1)
1641 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1642 else if (pOS2
->usFirstCharIndex
> 0xff)
1643 TM
->tmBreakChar
= 0x20;
1645 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1646 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1651 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1652 TM
->tmUnderlined
= FALSE
;
1653 TM
->tmStruckOut
= FALSE
;
1657 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1659 TM
->tmItalic
= 0xFF;
1665 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1666 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1669 if (!FT_IS_FIXED_WIDTH(Face
))
1671 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1673 case PAN_PROP_MONOSPACED
:
1674 TM
->tmPitchAndFamily
= 0;
1677 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1683 TM
->tmPitchAndFamily
= 0;
1686 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1688 case PAN_FAMILY_SCRIPT
:
1689 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1691 case PAN_FAMILY_DECORATIVE
:
1692 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1697 case PAN_FAMILY_TEXT_DISPLAY
:
1698 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1699 /* Which is clearly not what the panose spec says. */
1700 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1702 TM
->tmPitchAndFamily
= FF_MODERN
;
1706 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1711 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1714 case PAN_SERIF_COVE
:
1715 case PAN_SERIF_OBTUSE_COVE
:
1716 case PAN_SERIF_SQUARE_COVE
:
1717 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1718 case PAN_SERIF_SQUARE
:
1719 case PAN_SERIF_THIN
:
1720 case PAN_SERIF_BONE
:
1721 case PAN_SERIF_EXAGGERATED
:
1722 case PAN_SERIF_TRIANGLE
:
1723 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1726 case PAN_SERIF_NORMAL_SANS
:
1727 case PAN_SERIF_OBTUSE_SANS
:
1728 case PAN_SERIF_PERP_SANS
:
1729 case PAN_SERIF_FLARED
:
1730 case PAN_SERIF_ROUNDED
:
1731 TM
->tmPitchAndFamily
|= FF_SWISS
;
1737 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1740 if (FT_IS_SCALABLE(Face
))
1742 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1744 if (FT_IS_SFNT(Face
))
1746 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1749 TM
->tmCharSet
= FontGDI
->CharSet
;
1752 static void FASTCALL
1753 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1754 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1755 FT_WinFNT_HeaderRec
*pFNT
)
1757 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1761 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1762 FT_UShort NameID
, FT_UShort LangID
);
1764 /*************************************************************
1765 * IntGetOutlineTextMetrics
1769 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1771 OUTLINETEXTMETRICW
*Otm
)
1774 TT_HoriHeader
*pHori
;
1775 TT_Postscript
*pPost
;
1776 FT_Fixed XScale
, YScale
;
1777 FT_WinFNT_HeaderRec Win
;
1780 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1781 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1782 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1783 FT_Face Face
= SharedFace
->Face
;
1785 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1787 return Cache
->OutlineRequiredSize
;
1791 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1792 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1795 RtlInitUnicodeString(&FaceNameW
, NULL
);
1796 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1799 RtlInitUnicodeString(&StyleNameW
, NULL
);
1800 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1802 /* unique name (full name) */
1803 RtlInitUnicodeString(&FullNameW
, NULL
);
1804 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1806 if (!Cache
->OutlineRequiredSize
)
1809 Needed
= sizeof(OUTLINETEXTMETRICW
);
1810 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1811 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1812 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1813 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1815 Cache
->OutlineRequiredSize
= Needed
;
1818 if (Size
< Cache
->OutlineRequiredSize
)
1820 RtlFreeUnicodeString(&FamilyNameW
);
1821 RtlFreeUnicodeString(&FaceNameW
);
1822 RtlFreeUnicodeString(&StyleNameW
);
1823 RtlFreeUnicodeString(&FullNameW
);
1824 return Cache
->OutlineRequiredSize
;
1827 XScale
= Face
->size
->metrics
.x_scale
;
1828 YScale
= Face
->size
->metrics
.y_scale
;
1831 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1834 IntUnLockFreeType();
1835 DPRINT1("Can't find OS/2 table - not TT font?\n");
1836 RtlFreeUnicodeString(&FamilyNameW
);
1837 RtlFreeUnicodeString(&FaceNameW
);
1838 RtlFreeUnicodeString(&StyleNameW
);
1839 RtlFreeUnicodeString(&FullNameW
);
1843 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1846 IntUnLockFreeType();
1847 DPRINT1("Can't find HHEA table - not TT font?\n");
1848 RtlFreeUnicodeString(&FamilyNameW
);
1849 RtlFreeUnicodeString(&FaceNameW
);
1850 RtlFreeUnicodeString(&StyleNameW
);
1851 RtlFreeUnicodeString(&FullNameW
);
1855 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1857 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1859 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1861 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1864 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1865 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1866 Otm
->otmfsType
= pOS2
->fsType
;
1867 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1868 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1869 Otm
->otmItalicAngle
= 0; /* POST table */
1870 Otm
->otmEMSquare
= Face
->units_per_EM
;
1871 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1872 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1873 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1874 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1875 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1876 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1877 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1878 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1879 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1880 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1881 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1882 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1883 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1884 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1885 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1886 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1887 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1888 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1889 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1890 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1891 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1892 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1893 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1896 Otm
->otmsUnderscoreSize
= 0;
1897 Otm
->otmsUnderscorePosition
= 0;
1901 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1902 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1905 IntUnLockFreeType();
1907 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1910 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1911 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1912 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1915 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1916 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1917 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1920 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1921 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1922 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1924 /* unique name (full name) */
1925 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1926 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1927 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1929 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1931 RtlFreeUnicodeString(&FamilyNameW
);
1932 RtlFreeUnicodeString(&FaceNameW
);
1933 RtlFreeUnicodeString(&StyleNameW
);
1934 RtlFreeUnicodeString(&FullNameW
);
1936 return Cache
->OutlineRequiredSize
;
1939 static PFONTGDI FASTCALL
1940 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1943 PFONT_ENTRY CurrentEntry
;
1944 ANSI_STRING EntryFaceNameA
;
1945 UNICODE_STRING EntryFaceNameW
;
1949 Entry
= Head
->Flink
;
1950 while (Entry
!= Head
)
1952 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1954 FontGDI
= CurrentEntry
->Font
;
1957 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1958 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1959 if (!NT_SUCCESS(status
))
1964 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1966 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1967 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1970 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
1972 RtlFreeUnicodeString(&EntryFaceNameW
);
1976 RtlFreeUnicodeString(&EntryFaceNameW
);
1977 Entry
= Entry
->Flink
;
1983 static PFONTGDI FASTCALL
1984 FindFaceNameInLists(PUNICODE_STRING FaceName
)
1986 PPROCESSINFO Win32Process
;
1989 /* Search the process local list.
1990 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
1991 Win32Process
= PsGetCurrentProcessWin32Process();
1992 IntLockProcessPrivateFonts(Win32Process
);
1993 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
1994 IntUnLockProcessPrivateFonts(Win32Process
);
2000 /* Search the global list */
2001 IntLockGlobalFonts();
2002 Font
= FindFaceNameInList(FaceName
, &g_FontListHead
);
2003 IntUnLockGlobalFonts();
2008 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2010 CharSetFromLangID(LANGID LangID
)
2012 /* FIXME: Add more and fix if wrong */
2013 switch (PRIMARYLANGID(LangID
))
2016 switch (SUBLANGID(LangID
))
2018 case SUBLANG_CHINESE_TRADITIONAL
:
2019 return CHINESEBIG5_CHARSET
;
2020 case SUBLANG_CHINESE_SIMPLIFIED
:
2024 return GB2312_CHARSET
;
2026 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2027 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2028 return EASTEUROPE_CHARSET
;
2030 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2031 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2032 return RUSSIAN_CHARSET
;
2034 case LANG_ARABIC
: return ARABIC_CHARSET
;
2035 case LANG_GREEK
: return GREEK_CHARSET
;
2036 case LANG_HEBREW
: return HEBREW_CHARSET
;
2037 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2038 case LANG_KOREAN
: return JOHAB_CHARSET
;
2039 case LANG_TURKISH
: return TURKISH_CHARSET
;
2040 case LANG_THAI
: return THAI_CHARSET
;
2041 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2042 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2044 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2045 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2046 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2047 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2048 case LANG_SWEDISH
: default:
2049 return ANSI_CHARSET
;
2054 SwapEndian(LPVOID pvData
, DWORD Size
)
2056 BYTE b
, *pb
= pvData
;
2068 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2070 NTSTATUS Status
= STATUS_NO_MEMORY
;
2073 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2076 Tmp
.MaximumLength
= Source
->MaximumLength
;
2078 RtlCopyUnicodeString(&Tmp
, Source
);
2080 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2081 Destination
->Length
= Tmp
.Length
;
2082 Destination
->Buffer
= Tmp
.Buffer
;
2084 Status
= STATUS_SUCCESS
;
2091 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2092 FT_UShort NameID
, FT_UShort LangID
)
2095 INT i
, Count
, BestIndex
, Score
, BestScore
;
2097 NTSTATUS Status
= STATUS_NOT_FOUND
;
2098 ANSI_STRING AnsiName
;
2099 PSHARED_FACE_CACHE Cache
;
2100 FT_Face Face
= SharedFace
->Face
;
2102 RtlFreeUnicodeString(pNameW
);
2105 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2107 Cache
= &SharedFace
->EnglishUS
;
2111 Cache
= &SharedFace
->UserLanguage
;
2114 /* use cache if available */
2115 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2117 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2119 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2121 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2127 Count
= FT_Get_Sfnt_Name_Count(Face
);
2128 for (i
= 0; i
< Count
; ++i
)
2130 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2133 continue; /* failure */
2136 if (Name
.name_id
!= NameID
)
2138 continue; /* mismatched */
2141 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2142 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2143 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2145 continue; /* not Microsoft Unicode name */
2148 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2149 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2151 continue; /* invalid string */
2154 if (Name
.language_id
== LangID
)
2158 break; /* best match */
2160 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2164 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2173 if (Score
> BestScore
)
2182 /* store the best name */
2183 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2186 /* NOTE: Name.string is not null-terminated */
2188 Tmp
.Buffer
= (PWCH
)Name
.string
;
2189 Tmp
.Length
= Tmp
.MaximumLength
= Name
.string_len
;
2192 pNameW
->MaximumLength
= Name
.string_len
+ sizeof(WCHAR
);
2193 pNameW
->Buffer
= ExAllocatePoolWithTag(PagedPool
, pNameW
->MaximumLength
, TAG_USTR
);
2197 Status
= RtlAppendUnicodeStringToString(pNameW
, &Tmp
);
2198 if (Status
== STATUS_SUCCESS
)
2200 /* Convert UTF-16 big endian to little endian */
2201 SwapEndian(pNameW
->Buffer
, pNameW
->Length
);
2206 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2211 if (!NT_SUCCESS(Status
))
2214 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2216 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2217 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2221 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2222 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2226 if (NT_SUCCESS(Status
))
2229 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2231 ASSERT_FREETYPE_LOCK_NOT_HELD();
2233 if (!Cache
->FontFamily
.Buffer
)
2234 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2235 IntUnLockFreeType();
2237 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2239 ASSERT_FREETYPE_LOCK_NOT_HELD();
2241 if (!Cache
->FullName
.Buffer
)
2242 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2243 IntUnLockFreeType();
2250 static void FASTCALL
2251 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2252 LPCWSTR FullName
, PFONTGDI FontGDI
)
2255 UNICODE_STRING StyleW
;
2258 CHARSETINFO CharSetInfo
;
2260 OUTLINETEXTMETRICW
*Otm
;
2263 NEWTEXTMETRICW
*Ntm
;
2266 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2267 FT_Face Face
= SharedFace
->Face
;
2268 UNICODE_STRING NameW
;
2270 RtlInitUnicodeString(&NameW
, NULL
);
2271 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2272 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2273 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2278 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2281 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2285 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2286 TM
= &Otm
->otmTextMetrics
;
2288 Lf
->lfHeight
= TM
->tmHeight
;
2289 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2290 Lf
->lfWeight
= TM
->tmWeight
;
2291 Lf
->lfItalic
= TM
->tmItalic
;
2292 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2293 Lf
->lfCharSet
= TM
->tmCharSet
;
2294 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2295 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2296 Lf
->lfQuality
= PROOF_QUALITY
;
2298 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2299 Ntm
->tmHeight
= TM
->tmHeight
;
2300 Ntm
->tmAscent
= TM
->tmAscent
;
2301 Ntm
->tmDescent
= TM
->tmDescent
;
2302 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2303 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2304 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2305 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2306 Ntm
->tmWeight
= TM
->tmWeight
;
2307 Ntm
->tmOverhang
= TM
->tmOverhang
;
2308 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2309 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2310 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2311 Ntm
->tmLastChar
= TM
->tmLastChar
;
2312 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2313 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2314 Ntm
->tmItalic
= TM
->tmItalic
;
2315 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2316 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2317 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2318 Ntm
->tmCharSet
= TM
->tmCharSet
;
2319 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2321 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2323 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2325 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2326 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2327 Ntm
->ntmAvgWidth
= 0;
2329 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2330 ? TRUETYPE_FONTTYPE
: 0);
2332 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2333 Info
->FontType
|= RASTER_FONTTYPE
;
2338 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2340 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2344 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2346 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2347 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2350 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2352 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2353 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2354 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2355 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2356 if (!NT_SUCCESS(status
))
2360 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2363 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2367 IntUnLockFreeType();
2371 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2372 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2373 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2374 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2375 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2376 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2378 if (0 == pOS2
->version
)
2382 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2383 fs
.fsCsb
[0] |= FS_LATIN1
;
2385 fs
.fsCsb
[0] |= FS_SYMBOL
;
2387 IntUnLockFreeType();
2389 if (fs
.fsCsb
[0] == 0)
2391 /* Let's see if we can find any interesting cmaps */
2392 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2394 switch (Face
->charmaps
[i
]->encoding
)
2396 case FT_ENCODING_UNICODE
:
2397 case FT_ENCODING_APPLE_ROMAN
:
2398 fs
.fsCsb
[0] |= FS_LATIN1
;
2400 case FT_ENCODING_MS_SYMBOL
:
2401 fs
.fsCsb
[0] |= FS_SYMBOL
;
2409 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2412 if (fs
.fsCsb
[0] & fs0
)
2414 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2416 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2418 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2420 if (g_ElfScripts
[i
])
2421 wcscpy(Info
->EnumLogFontEx
.elfScript
, g_ElfScripts
[i
]);
2424 DPRINT1("Unknown elfscript for bit %u\n", i
);
2429 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2433 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2436 UNICODE_STRING InfoFaceName
;
2438 for (i
= 0; i
< InfoEntries
; i
++)
2440 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2441 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2450 static BOOLEAN FASTCALL
2451 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2452 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2454 UNICODE_STRING LogFontFaceName
;
2456 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2457 if (0 != LogFontFaceName
.Length
&&
2458 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2463 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2466 static BOOL FASTCALL
2467 FontFamilyFound(PFONTFAMILYINFO InfoEntry
,
2468 PFONTFAMILYINFO Info
, DWORD InfoCount
)
2470 LPLOGFONTW plf1
= &InfoEntry
->EnumLogFontEx
.elfLogFont
;
2471 LPWSTR pFullName1
= InfoEntry
->EnumLogFontEx
.elfFullName
;
2475 for (i
= 0; i
< InfoCount
; ++i
)
2477 LPLOGFONTW plf2
= &Info
[i
].EnumLogFontEx
.elfLogFont
;
2478 if (plf1
->lfCharSet
!= plf2
->lfCharSet
)
2481 pFullName2
= Info
[i
].EnumLogFontEx
.elfFullName
;
2482 if (_wcsicmp(pFullName1
, pFullName2
) != 0)
2490 static BOOLEAN FASTCALL
2491 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2492 PFONTFAMILYINFO Info
,
2498 PFONT_ENTRY CurrentEntry
;
2500 FONTFAMILYINFO InfoEntry
;
2501 DWORD Count
= *pCount
;
2503 for (Entry
= Head
->Flink
; Entry
!= Head
; Entry
= Entry
->Flink
)
2505 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2506 FontGDI
= CurrentEntry
->Font
;
2509 if (LogFont
->lfCharSet
!= DEFAULT_CHARSET
&&
2510 LogFont
->lfCharSet
!= FontGDI
->CharSet
)
2515 if (LogFont
->lfFaceName
[0] == UNICODE_NULL
)
2517 if (Count
< MaxCount
)
2519 FontFamilyFillInfo(&Info
[Count
], NULL
, NULL
, FontGDI
);
2525 FontFamilyFillInfo(&InfoEntry
, NULL
, NULL
, FontGDI
);
2527 if (_wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfLogFont
.lfFaceName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0 &&
2528 _wcsnicmp(LogFont
->lfFaceName
, InfoEntry
.EnumLogFontEx
.elfFullName
, RTL_NUMBER_OF(LogFont
->lfFaceName
)-1) != 0)
2533 if (!FontFamilyFound(&InfoEntry
, Info
, min(Count
, MaxCount
)))
2535 if (Count
< MaxCount
)
2537 RtlCopyMemory(&Info
[Count
], &InfoEntry
, sizeof(InfoEntry
));
2548 static BOOLEAN FASTCALL
2549 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2550 PFONTFAMILYINFO Info
,
2554 PLIST_ENTRY pEntry
, pHead
= &g_FontSubstListHead
;
2555 PFONTSUBST_ENTRY pCurrentEntry
;
2556 PUNICODE_STRING pFromW
;
2558 LOGFONTW lf
= *LogFont
;
2559 UNICODE_STRING NameW
;
2561 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2563 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2565 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2566 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2568 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2569 continue; /* mismatch */
2572 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2573 SubstituteFontRecurse(&lf
);
2575 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2576 FontGDI
= FindFaceNameInLists(&NameW
);
2577 if (FontGDI
== NULL
)
2579 continue; /* no real font */
2582 if (*pCount
< MaxCount
)
2584 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2594 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2598 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2599 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2600 lprs
->nLanguageID
= gusLanguageID
;
2603 EngSetLastError(ERROR_INVALID_PARAMETER
);
2613 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2614 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2615 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2616 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2619 FT_BitmapGlyph APIENTRY
2624 FT_Render_Mode RenderMode
,
2627 PLIST_ENTRY CurrentEntry
;
2628 PFONT_CACHE_ENTRY FontEntry
;
2630 ASSERT_FREETYPE_LOCK_HELD();
2632 CurrentEntry
= g_FontCacheListHead
.Flink
;
2633 while (CurrentEntry
!= &g_FontCacheListHead
)
2635 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2636 if ((FontEntry
->Face
== Face
) &&
2637 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2638 (FontEntry
->Height
== Height
) &&
2639 (FontEntry
->RenderMode
== RenderMode
) &&
2640 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2642 CurrentEntry
= CurrentEntry
->Flink
;
2645 if (CurrentEntry
== &g_FontCacheListHead
)
2650 RemoveEntryList(CurrentEntry
);
2651 InsertHeadList(&g_FontCacheListHead
, CurrentEntry
);
2652 return FontEntry
->BitmapGlyph
;
2656 FT_BitmapGlyph APIENTRY
2659 FT_GlyphSlot GlyphSlot
,
2660 FT_Render_Mode RenderMode
)
2664 FT_Bitmap AlignedBitmap
;
2665 FT_BitmapGlyph BitmapGlyph
;
2667 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2670 DPRINT1("Failure getting glyph.\n");
2674 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2677 FT_Done_Glyph(Glyph
);
2678 DPRINT1("Failure rendering glyph.\n");
2682 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2683 FT_Bitmap_New(&AlignedBitmap
);
2684 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2686 DPRINT1("Conversion failed\n");
2687 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2691 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2692 BitmapGlyph
->bitmap
= AlignedBitmap
;
2697 FT_BitmapGlyph APIENTRY
2703 FT_GlyphSlot GlyphSlot
,
2704 FT_Render_Mode RenderMode
)
2708 PFONT_CACHE_ENTRY NewEntry
;
2709 FT_Bitmap AlignedBitmap
;
2710 FT_BitmapGlyph BitmapGlyph
;
2712 ASSERT_FREETYPE_LOCK_HELD();
2714 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2717 DPRINT1("Failure caching glyph.\n");
2721 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2724 FT_Done_Glyph(GlyphCopy
);
2725 DPRINT1("Failure rendering glyph.\n");
2729 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2732 DPRINT1("Alloc failure caching glyph.\n");
2733 FT_Done_Glyph(GlyphCopy
);
2737 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2738 FT_Bitmap_New(&AlignedBitmap
);
2739 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2741 DPRINT1("Conversion failed\n");
2742 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2743 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2744 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2748 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2749 BitmapGlyph
->bitmap
= AlignedBitmap
;
2751 NewEntry
->GlyphIndex
= GlyphIndex
;
2752 NewEntry
->Face
= Face
;
2753 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2754 NewEntry
->Height
= Height
;
2755 NewEntry
->RenderMode
= RenderMode
;
2756 NewEntry
->mxWorldToDevice
= *pmx
;
2758 InsertHeadList(&g_FontCacheListHead
, &NewEntry
->ListEntry
);
2759 if (++g_FontCacheNumEntries
> MAX_FONT_CACHE
)
2761 NewEntry
= CONTAINING_RECORD(g_FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2762 RemoveCachedEntry(NewEntry
);
2769 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2771 pt
->x
.value
= vec
->x
>> 6;
2772 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2773 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2774 pt
->y
.value
= vec
->y
>> 6;
2775 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2776 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2780 This function builds an FT_Fixed from a float. It puts the integer part
2781 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2782 It fails if the integer part of the float number is greater than SHORT_MAX.
2784 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2787 unsigned short fract
= (f
- value
) * 0xFFFF;
2788 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2792 This function builds an FT_Fixed from a FIXED. It simply put f.value
2793 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2795 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2797 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2800 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2802 TTPOLYGONHEADER
*pph
;
2804 int needed
= 0, point
= 0, contour
, first_pt
;
2805 unsigned int pph_start
, cpfx
;
2808 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2810 /* Ignore contours containing one point */
2811 if (point
== outline
->contours
[contour
])
2818 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2822 pph
->dwType
= TT_POLYGON_TYPE
;
2823 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2825 needed
+= sizeof(*pph
);
2827 while (point
<= outline
->contours
[contour
])
2829 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2830 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2831 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2836 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2839 } while (point
<= outline
->contours
[contour
] &&
2840 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2841 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2842 /* At the end of a contour Windows adds the start point, but
2844 if (point
> outline
->contours
[contour
] &&
2845 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2848 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2851 else if (point
<= outline
->contours
[contour
] &&
2852 outline
->tags
[point
] & FT_Curve_Tag_On
)
2854 /* add closing pt for bezier */
2856 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2865 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2868 pph
->cb
= needed
- pph_start
;
2873 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2875 /* Convert the quadratic Beziers to cubic Beziers.
2876 The parametric eqn for a cubic Bezier is, from PLRM:
2877 r(t) = at^3 + bt^2 + ct + r0
2878 with the control points:
2883 A quadratic Bezier has the form:
2884 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2886 So equating powers of t leads to:
2887 r1 = 2/3 p1 + 1/3 p0
2888 r2 = 2/3 p1 + 1/3 p2
2889 and of course r0 = p0, r3 = p2
2891 int contour
, point
= 0, first_pt
;
2892 TTPOLYGONHEADER
*pph
;
2894 DWORD pph_start
, cpfx
, type
;
2895 FT_Vector cubic_control
[4];
2896 unsigned int needed
= 0;
2898 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2901 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2905 pph
->dwType
= TT_POLYGON_TYPE
;
2906 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2908 needed
+= sizeof(*pph
);
2910 while (point
<= outline
->contours
[contour
])
2912 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2913 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2914 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2918 if (type
== TT_PRIM_LINE
)
2921 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2927 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2930 /* FIXME: Possible optimization in endpoint calculation
2931 if there are two consecutive curves */
2932 cubic_control
[0] = outline
->points
[point
-1];
2933 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2935 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2936 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2937 cubic_control
[0].x
>>= 1;
2938 cubic_control
[0].y
>>= 1;
2940 if (point
+1 > outline
->contours
[contour
])
2941 cubic_control
[3] = outline
->points
[first_pt
];
2944 cubic_control
[3] = outline
->points
[point
+1];
2945 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2947 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2948 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2949 cubic_control
[3].x
>>= 1;
2950 cubic_control
[3].y
>>= 1;
2953 /* r1 = 1/3 p0 + 2/3 p1
2954 r2 = 1/3 p2 + 2/3 p1 */
2955 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2956 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2957 cubic_control
[2] = cubic_control
[1];
2958 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2959 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2960 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2961 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2964 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2965 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2966 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
2971 } while (point
<= outline
->contours
[contour
] &&
2972 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2973 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2974 /* At the end of a contour Windows adds the start point,
2975 but only for Beziers and we've already done that.
2977 if (point
<= outline
->contours
[contour
] &&
2978 outline
->tags
[point
] & FT_Curve_Tag_On
)
2980 /* This is the closing pt of a bezier, but we've already
2981 added it, so just inc point and carry on */
2989 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2992 pph
->cb
= needed
- pph_start
;
2998 IntRequestFontSize(PDC dc
, PFONTGDI FontGDI
, LONG Width
, LONG Height
)
3000 FT_Size_RequestRec req
;
3001 FT_Face face
= FontGDI
->SharedFace
->Face
;
3012 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
3022 if (Width
> 0xFFFFU
)
3024 if (Height
> 0xFFFFU
)
3027 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
3028 req
.width
= (FT_Long
)(Width
<< 6);
3029 req
.height
= (FT_Long
)(Height
<< 6);
3030 req
.horiResolution
= 0;
3031 req
.vertResolution
= 0;
3032 return FT_Request_Size(face
, &req
);
3037 TextIntUpdateSize(PDC dc
,
3044 FT_CharMap charmap
, found
;
3050 face
= FontGDI
->SharedFace
->Face
;
3051 if (face
->charmap
== NULL
)
3053 DPRINT("WARNING: No charmap selected!\n");
3054 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3057 for (n
= 0; n
< face
->num_charmaps
; n
++)
3059 charmap
= face
->charmaps
[n
];
3060 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
3061 if (charmap
->encoding
!= 0)
3069 DPRINT1("WARNING: Could not find desired charmap!\n");
3073 error
= FT_Set_Charmap(face
, found
);
3076 DPRINT1("WARNING: Could not set the charmap!\n");
3081 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3083 error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3086 IntUnLockFreeType();
3090 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3099 * Based on WineEngGetGlyphOutline
3104 ftGdiGetGlyphOutline(
3112 BOOL bIgnoreRotation
)
3114 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3122 FT_UInt glyph_index
;
3123 DWORD width
, height
, pitch
, needed
= 0;
3124 FT_Bitmap ft_bitmap
;
3126 INT left
, right
, top
= 0, bottom
= 0;
3128 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3129 FLOAT eM11
, widthRatio
= 1.0;
3130 FT_Matrix transMat
= identityMat
;
3131 BOOL needsTransform
= FALSE
;
3134 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3135 OUTLINETEXTMETRICW
*potm
;
3139 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3140 cjBuf
, pvBuf
, pmat2
);
3142 pdcattr
= dc
->pdcattr
;
3144 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3147 hFont
= pdcattr
->hlfntNew
;
3148 TextObj
= RealizeFontInit(hFont
);
3152 EngSetLastError(ERROR_INVALID_HANDLE
);
3155 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3156 ft_face
= FontGDI
->SharedFace
->Face
;
3158 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3159 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3160 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3162 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3163 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3166 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3167 TEXTOBJ_UnlockText(TextObj
);
3170 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3173 /* FIXME: last error? */
3174 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3175 TEXTOBJ_UnlockText(TextObj
);
3180 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3181 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3183 TEXTOBJ_UnlockText(TextObj
);
3185 if (iFormat
& GGO_GLYPH_INDEX
)
3188 iFormat
&= ~GGO_GLYPH_INDEX
;
3190 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3192 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3193 load_flags
|= FT_LOAD_NO_BITMAP
;
3195 if (iFormat
& GGO_UNHINTED
)
3197 load_flags
|= FT_LOAD_NO_HINTING
;
3198 iFormat
&= ~GGO_UNHINTED
;
3201 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3204 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3205 IntUnLockFreeType();
3206 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3209 IntUnLockFreeType();
3211 if (aveWidth
&& potm
)
3213 widthRatio
= (FLOAT
)aveWidth
* eM11
/
3214 (FLOAT
) potm
->otmTextMetrics
.tmAveCharWidth
;
3217 left
= (INT
)(ft_face
->glyph
->metrics
.horiBearingX
* widthRatio
) & -64;
3218 right
= (INT
)((ft_face
->glyph
->metrics
.horiBearingX
+
3219 ft_face
->glyph
->metrics
.width
) * widthRatio
+ 63) & -64;
3221 adv
= (INT
)((ft_face
->glyph
->metrics
.horiAdvance
* widthRatio
) + 63) >> 6;
3223 bbx
= (right
- left
) >> 6;
3225 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3229 /* Scaling transform */
3236 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3238 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3239 efTemp
= pmx
->efM11
;
3240 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3241 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
3243 efTemp
= pmx
->efM12
;
3244 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3245 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
3247 efTemp
= pmx
->efM21
;
3248 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3249 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
3251 efTemp
= pmx
->efM22
;
3252 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3253 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
3255 FT_Matrix_Multiply(&ftmatrix
, &transMat
);
3256 needsTransform
= TRUE
;
3259 /* Rotation transform */
3262 FT_Matrix rotationMat
;
3264 DPRINT("Rotation Trans!\n");
3265 angle
= FT_FixedFromFloat((float)orientation
/ 10.0);
3266 FT_Vector_Unit(&vecAngle
, angle
);
3267 rotationMat
.xx
= vecAngle
.x
;
3268 rotationMat
.xy
= -vecAngle
.y
;
3269 rotationMat
.yx
= -rotationMat
.xy
;
3270 rotationMat
.yy
= rotationMat
.xx
;
3271 FT_Matrix_Multiply(&rotationMat
, &transMat
);
3272 needsTransform
= TRUE
;
3275 /* Extra transformation specified by caller */
3279 DPRINT("MAT2 Matrix Trans!\n");
3280 extraMat
.xx
= FT_FixedFromFIXED(pmat2
->eM11
);
3281 extraMat
.xy
= FT_FixedFromFIXED(pmat2
->eM21
);
3282 extraMat
.yx
= FT_FixedFromFIXED(pmat2
->eM12
);
3283 extraMat
.yy
= FT_FixedFromFIXED(pmat2
->eM22
);
3284 FT_Matrix_Multiply(&extraMat
, &transMat
);
3285 needsTransform
= TRUE
;
3288 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
); /* It looks like we are finished with potm ATM. */
3290 if (!needsTransform
)
3292 DPRINT("No Need to be Transformed!\n");
3293 top
= (ft_face
->glyph
->metrics
.horiBearingY
+ 63) & -64;
3294 bottom
= (ft_face
->glyph
->metrics
.horiBearingY
-
3295 ft_face
->glyph
->metrics
.height
) & -64;
3296 gm
.gmCellIncX
= adv
;
3303 for (xc
= 0; xc
< 2; xc
++)
3305 for (yc
= 0; yc
< 2; yc
++)
3307 vec
.x
= (ft_face
->glyph
->metrics
.horiBearingX
+
3308 xc
* ft_face
->glyph
->metrics
.width
);
3309 vec
.y
= ft_face
->glyph
->metrics
.horiBearingY
-
3310 yc
* ft_face
->glyph
->metrics
.height
;
3311 DPRINT("Vec %ld,%ld\n", vec
.x
, vec
.y
);
3312 FT_Vector_Transform(&vec
, &transMat
);
3313 if (xc
== 0 && yc
== 0)
3315 left
= right
= vec
.x
;
3316 top
= bottom
= vec
.y
;
3320 if (vec
.x
< left
) left
= vec
.x
;
3321 else if (vec
.x
> right
) right
= vec
.x
;
3322 if (vec
.y
< bottom
) bottom
= vec
.y
;
3323 else if (vec
.y
> top
) top
= vec
.y
;
3328 right
= (right
+ 63) & -64;
3329 bottom
= bottom
& -64;
3330 top
= (top
+ 63) & -64;
3332 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left
, top
, right
, bottom
);
3333 vec
.x
= ft_face
->glyph
->metrics
.horiAdvance
;
3335 FT_Vector_Transform(&vec
, &transMat
);
3336 gm
.gmCellIncX
= (vec
.x
+63) >> 6;
3337 gm
.gmCellIncY
= -((vec
.y
+63) >> 6);
3339 gm
.gmBlackBoxX
= (right
- left
) >> 6;
3340 gm
.gmBlackBoxY
= (top
- bottom
) >> 6;
3341 gm
.gmptGlyphOrigin
.x
= left
>> 6;
3342 gm
.gmptGlyphOrigin
.y
= top
>> 6;
3344 DPRINT("CX %d CY %d BBX %u BBY %u GOX %d GOY %d\n",
3345 gm
.gmCellIncX
, gm
.gmCellIncY
,
3346 gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
3347 gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
3349 IntUnLockFreeType();
3352 if (iFormat
== GGO_METRICS
)
3354 DPRINT("GGO_METRICS Exit!\n");
3356 return 1; /* FIXME */
3359 if (ft_face
->glyph
->format
!= ft_glyph_format_outline
&& iFormat
!= GGO_BITMAP
)
3361 DPRINT1("Loaded a bitmap\n");
3368 width
= gm
.gmBlackBoxX
;
3369 height
= gm
.gmBlackBoxY
;
3370 pitch
= ((width
+ 31) >> 5) << 2;
3371 needed
= pitch
* height
;
3373 if (!pvBuf
|| !cjBuf
) break;
3374 if (!needed
) return GDI_ERROR
; /* empty glyph */
3378 switch (ft_face
->glyph
->format
)
3380 case ft_glyph_format_bitmap
:
3382 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3383 INT w
= min( pitch
, (ft_face
->glyph
->bitmap
.width
+ 7) >> 3 );
3384 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3387 RtlCopyMemory(dst
, src
, w
);
3388 src
+= ft_face
->glyph
->bitmap
.pitch
;
3394 case ft_glyph_format_outline
:
3395 ft_bitmap
.width
= width
;
3396 ft_bitmap
.rows
= height
;
3397 ft_bitmap
.pitch
= pitch
;
3398 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
3399 ft_bitmap
.buffer
= pvBuf
;
3404 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3406 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3407 /* Note: FreeType will only set 'black' bits for us. */
3408 RtlZeroMemory(pvBuf
, needed
);
3409 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3410 IntUnLockFreeType();
3414 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3419 case GGO_GRAY2_BITMAP
:
3420 case GGO_GRAY4_BITMAP
:
3421 case GGO_GRAY8_BITMAP
:
3423 unsigned int mult
, row
, col
;
3426 width
= gm
.gmBlackBoxX
;
3427 height
= gm
.gmBlackBoxY
;
3428 pitch
= (width
+ 3) / 4 * 4;
3429 needed
= pitch
* height
;
3431 if (!pvBuf
|| !cjBuf
) break;
3432 if (!needed
) return GDI_ERROR
; /* empty glyph */
3436 switch (ft_face
->glyph
->format
)
3438 case ft_glyph_format_bitmap
:
3440 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3441 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3445 for (x
= 0; (UINT
)x
< pitch
; x
++)
3447 if (x
< ft_face
->glyph
->bitmap
.width
)
3448 dst
[x
] = (src
[x
/ 8] & (1 << ( (7 - (x
% 8))))) ? 0xff : 0;
3452 src
+= ft_face
->glyph
->bitmap
.pitch
;
3457 case ft_glyph_format_outline
:
3459 ft_bitmap
.width
= width
;
3460 ft_bitmap
.rows
= height
;
3461 ft_bitmap
.pitch
= pitch
;
3462 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
3463 ft_bitmap
.buffer
= pvBuf
;
3468 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3470 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3471 RtlZeroMemory(ft_bitmap
.buffer
, cjBuf
);
3472 FT_Outline_Get_Bitmap(g_FreeTypeLibrary
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3473 IntUnLockFreeType();
3475 if (iFormat
== GGO_GRAY2_BITMAP
)
3477 else if (iFormat
== GGO_GRAY4_BITMAP
)
3479 else if (iFormat
== GGO_GRAY8_BITMAP
)
3487 for (row
= 0; row
< height
; row
++)
3490 for (col
= 0; col
< width
; col
++, ptr
++)
3492 *ptr
= (((int)*ptr
) * mult
+ 128) / 256;
3500 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3507 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3509 if (cjBuf
== 0) pvBuf
= NULL
; /* This is okay, need cjBuf to allocate. */
3512 if (needsTransform
&& pvBuf
) FT_Outline_Transform(outline
, &transMat
);
3514 needed
= get_native_glyph_outline(outline
, cjBuf
, NULL
);
3516 if (!pvBuf
|| !cjBuf
)
3518 IntUnLockFreeType();
3523 IntUnLockFreeType();
3526 get_native_glyph_outline(outline
, cjBuf
, pvBuf
);
3527 IntUnLockFreeType();
3532 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3533 if (cjBuf
== 0) pvBuf
= NULL
;
3535 if (needsTransform
&& pvBuf
)
3538 FT_Outline_Transform(outline
, &transMat
);
3539 IntUnLockFreeType();
3541 needed
= get_bezier_glyph_outline(outline
, cjBuf
, NULL
);
3543 if (!pvBuf
|| !cjBuf
)
3548 get_bezier_glyph_outline(outline
, cjBuf
, pvBuf
);
3553 DPRINT1("Unsupported format %u\n", iFormat
);
3557 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed
);
3564 TextIntGetTextExtentPoint(PDC dc
,
3577 FT_BitmapGlyph realglyph
;
3578 INT error
, glyph_index
, i
, previous
;
3579 ULONGLONG TotalWidth
= 0;
3581 FT_Render_Mode RenderMode
;
3583 PMATRIX pmxWorldToDevice
;
3585 BOOL EmuBold
, EmuItalic
;
3586 LONG ascender
, descender
;
3588 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3590 face
= FontGDI
->SharedFace
->Face
;
3598 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3600 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3601 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
3602 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
3604 Render
= IntIsFontRenderingEnabled();
3606 RenderMode
= IntGetFontRenderMode(plf
);
3608 RenderMode
= FT_RENDER_MODE_MONO
;
3610 /* Get the DC's world-to-device transformation matrix */
3611 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
3612 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
3614 use_kerning
= FT_HAS_KERNING(face
);
3617 for (i
= 0; i
< Count
; i
++)
3619 if (fl
& GTEF_INDICES
)
3620 glyph_index
= *String
;
3622 glyph_index
= FT_Get_Char_Index(face
, *String
);
3624 if (EmuBold
|| EmuItalic
)
3627 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
3628 RenderMode
, pmxWorldToDevice
);
3630 if (EmuBold
|| EmuItalic
|| !realglyph
)
3632 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
3635 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
3639 glyph
= face
->glyph
;
3640 if (EmuBold
|| EmuItalic
)
3643 FT_GlyphSlot_Embolden(glyph
);
3645 FT_GlyphSlot_Oblique(glyph
);
3646 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
3650 realglyph
= ftGdiGlyphCacheSet(face
,
3660 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
3665 /* Retrieve kerning distance */
3666 if (use_kerning
&& previous
&& glyph_index
)
3669 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
3670 TotalWidth
+= delta
.x
;
3673 TotalWidth
+= realglyph
->root
.advance
.x
>> 10;
3675 if (((TotalWidth
+ 32) >> 6) <= MaxExtent
&& NULL
!= Fit
)
3681 Dx
[i
] = (TotalWidth
+ 32) >> 6;
3684 if (EmuBold
|| EmuItalic
)
3686 FT_Done_Glyph((FT_Glyph
)realglyph
);
3690 previous
= glyph_index
;
3693 ascender
= (face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
3694 descender
= (32 - face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
3695 IntUnLockFreeType();
3697 Size
->cx
= (TotalWidth
+ 32) >> 6;
3698 Size
->cy
= ascender
+ descender
;
3706 ftGdiGetTextCharsetInfo(
3708 LPFONTSIGNATURE lpSig
,
3712 UINT Ret
= DEFAULT_CHARSET
;
3722 USHORT usACP
, usOEM
;
3724 pdcattr
= Dc
->pdcattr
;
3725 hFont
= pdcattr
->hlfntNew
;
3726 TextObj
= RealizeFontInit(hFont
);
3730 EngSetLastError(ERROR_INVALID_HANDLE
);
3733 FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
3734 Face
= FontGdi
->SharedFace
->Face
;
3735 TEXTOBJ_UnlockText(TextObj
);
3738 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
3739 IntUnLockFreeType();
3740 memset(&fs
, 0, sizeof(FONTSIGNATURE
));
3743 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
3744 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
3745 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
3746 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
3747 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
3748 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
3749 if (pOS2
->version
== 0)
3753 if (FT_Get_First_Char( Face
, &dummy
) < 0x100)
3754 fs
.fsCsb
[0] |= FS_LATIN1
;
3756 fs
.fsCsb
[0] |= FS_SYMBOL
;
3759 DPRINT("Csb 1=%x 0=%x\n", fs
.fsCsb
[1],fs
.fsCsb
[0]);
3760 if (fs
.fsCsb
[0] == 0)
3761 { /* Let's see if we can find any interesting cmaps */
3762 for (i
= 0; i
< Face
->num_charmaps
; i
++)
3764 switch (Face
->charmaps
[i
]->encoding
)
3766 case FT_ENCODING_UNICODE
:
3767 case FT_ENCODING_APPLE_ROMAN
:
3768 fs
.fsCsb
[0] |= FS_LATIN1
;
3770 case FT_ENCODING_MS_SYMBOL
:
3771 fs
.fsCsb
[0] |= FS_SYMBOL
;
3780 RtlCopyMemory(lpSig
, &fs
, sizeof(FONTSIGNATURE
));
3783 RtlGetDefaultCodePage(&usACP
, &usOEM
);
3786 if (IntTranslateCharsetInfo(&cp
, &csi
, TCI_SRCCODEPAGE
))
3787 if (csi
.fs
.fsCsb
[0] & fs
.fsCsb
[0])
3790 Ret
= csi
.ciCharset
;
3794 for (i
= 0; i
< MAXTCIINDEX
; i
++)
3797 if (fs
.fsCsb
[0] & fs0
)
3799 if (IntTranslateCharsetInfo(&fs0
, &csi
, TCI_SRCFONTSIG
))
3803 Ret
= csi
.ciCharset
;
3807 DPRINT1("TCI failing on %x\n", fs0
);
3811 DPRINT("CharSet %u CodePage %u\n", csi
.ciCharset
, csi
.ciACP
);
3812 return (MAKELONG(csi
.ciACP
, csi
.ciCharset
));
3818 ftGetFontUnicodeRanges(PFONTGDI Font
, PGLYPHSET glyphset
)
3821 DWORD num_ranges
= 0;
3822 FT_Face face
= Font
->SharedFace
->Face
;
3824 if (face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
3826 FT_UInt glyph_code
= 0;
3827 FT_ULong char_code
, char_code_prev
;
3829 char_code_prev
= char_code
= FT_Get_First_Char(face
, &glyph_code
);
3831 DPRINT("Face encoding FT_ENCODING_UNICODE, number of glyphs %ld, first glyph %u, first char %04lx\n",
3832 face
->num_glyphs
, glyph_code
, char_code
);
3834 if (!glyph_code
) return 0;
3838 glyphset
->ranges
[0].wcLow
= (USHORT
)char_code
;
3839 glyphset
->ranges
[0].cGlyphs
= 0;
3840 glyphset
->cGlyphsSupported
= 0;
3846 if (char_code
< char_code_prev
)
3848 DPRINT1("Expected increasing char code from FT_Get_Next_Char\n");
3851 if (char_code
- char_code_prev
> 1)
3856 glyphset
->ranges
[num_ranges
- 1].wcLow
= (USHORT
)char_code
;
3857 glyphset
->ranges
[num_ranges
- 1].cGlyphs
= 1;
3858 glyphset
->cGlyphsSupported
++;
3863 glyphset
->ranges
[num_ranges
- 1].cGlyphs
++;
3864 glyphset
->cGlyphsSupported
++;
3866 char_code_prev
= char_code
;
3867 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_code
);
3871 DPRINT1("Encoding %i not supported\n", face
->charmap
->encoding
);
3873 size
= sizeof(GLYPHSET
) + sizeof(WCRANGE
) * (num_ranges
- 1);
3876 glyphset
->cbThis
= size
;
3877 glyphset
->cRanges
= num_ranges
;
3878 glyphset
->flAccel
= 0;
3886 ftGdiGetTextMetricsW(
3888 PTMW_INTERNAL ptmwi
)
3896 TT_HoriHeader
*pHori
;
3897 FT_WinFNT_HeaderRec Win
;
3899 NTSTATUS Status
= STATUS_SUCCESS
;
3904 EngSetLastError(STATUS_INVALID_PARAMETER
);
3908 if (!(dc
= DC_LockDc(hDC
)))
3910 EngSetLastError(ERROR_INVALID_HANDLE
);
3913 pdcattr
= dc
->pdcattr
;
3914 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
3915 if (NULL
!= TextObj
)
3917 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3918 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3920 Face
= FontGDI
->SharedFace
->Face
;
3922 Error
= IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
3923 FtSetCoordinateTransform(Face
, DC_pmxWorldToDevice(dc
));
3924 IntUnLockFreeType();
3927 DPRINT1("Error in setting pixel sizes: %u\n", Error
);
3928 Status
= STATUS_UNSUCCESSFUL
;
3932 FT_Face Face
= FontGDI
->SharedFace
->Face
;
3933 Status
= STATUS_SUCCESS
;
3936 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
3939 DPRINT1("Can't find OS/2 table - not TT font?\n");
3940 Status
= STATUS_INTERNAL_ERROR
;
3943 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
3946 DPRINT1("Can't find HHEA table - not TT font?\n");
3947 Status
= STATUS_INTERNAL_ERROR
;
3950 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
3952 IntUnLockFreeType();
3954 if (NT_SUCCESS(Status
))
3956 FillTM(&ptmwi
->TextMetric
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
3958 /* FIXME: Fill Diff member */
3959 RtlZeroMemory(&ptmwi
->Diff
, sizeof(ptmwi
->Diff
));
3962 TEXTOBJ_UnlockText(TextObj
);
3966 Status
= STATUS_INVALID_HANDLE
;
3970 if (!NT_SUCCESS(Status
))
3972 SetLastNtError(Status
);
3987 DWORD Result
= GDI_ERROR
;
3988 FT_Face Face
= FontGdi
->SharedFace
->Face
;
3992 if (FT_IS_SFNT(Face
))
3995 Table
= Table
>> 24 | Table
<< 24 | (Table
>> 8 & 0xFF00) |
3996 (Table
<< 8 & 0xFF0000);
3998 if (!Buffer
) Size
= 0;
4003 FT_ULong Needed
= 0;
4005 Error
= FT_Load_Sfnt_Table(Face
, Table
, Offset
, NULL
, &Needed
);
4007 if ( !Error
&& Needed
< Size
) Size
= Needed
;
4009 if (!FT_Load_Sfnt_Table(Face
, Table
, Offset
, Buffer
, &Size
))
4013 IntUnLockFreeType();
4018 // NOTE: See Table 1. of https://msdn.microsoft.com/en-us/library/ms969909.aspx
4020 GetFontPenalty(const LOGFONTW
* LogFont
,
4021 const OUTLINETEXTMETRICW
* Otm
,
4022 const char * style_name
)
4027 BOOL fNeedScaling
= FALSE
;
4028 const BYTE UserCharSet
= CharSetFromLangID(gusLanguageID
);
4029 const TEXTMETRICW
* TM
= &Otm
->otmTextMetrics
;
4035 /* FIXME: Aspect Penalty 30 */
4036 /* FIXME: IntSizeSynth Penalty 20 */
4037 /* FIXME: SmallPenalty Penalty 1 */
4038 /* FIXME: FaceNameSubst Penalty 500 */
4040 Byte
= LogFont
->lfCharSet
;
4041 if (Byte
== DEFAULT_CHARSET
)
4043 if (_wcsicmp(LogFont
->lfFaceName
, L
"Marlett") == 0)
4045 if (Byte
== ANSI_CHARSET
)
4047 DPRINT("Warning: FIXME: It's Marlett but ANSI_CHARSET.\n");
4049 /* We assume SYMBOL_CHARSET for "Marlett" font */
4050 Byte
= SYMBOL_CHARSET
;
4054 if (Byte
!= TM
->tmCharSet
)
4056 if (Byte
!= DEFAULT_CHARSET
&& Byte
!= ANSI_CHARSET
)
4058 /* CharSet Penalty 65000 */
4059 /* Requested charset does not match the candidate's. */
4064 if (UserCharSet
!= TM
->tmCharSet
)
4068 if (ANSI_CHARSET
!= TM
->tmCharSet
)
4077 Byte
= LogFont
->lfOutPrecision
;
4078 if (Byte
== OUT_DEFAULT_PRECIS
)
4079 Byte
= OUT_OUTLINE_PRECIS
; /* Is it OK? */
4082 case OUT_DEVICE_PRECIS
:
4083 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
) ||
4084 !(TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
)))
4086 /* OutputPrecision Penalty 19000 */
4087 /* Requested OUT_STROKE_PRECIS, but the device can't do it
4088 or the candidate is not a vector font. */
4093 if (TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
))
4095 /* OutputPrecision Penalty 19000 */
4096 /* Or OUT_STROKE_PRECIS not requested, and the candidate
4097 is a vector font that requires GDI support. */
4103 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4104 if (Byte
== DEFAULT_PITCH
)
4105 Byte
= VARIABLE_PITCH
;
4106 if (Byte
== FIXED_PITCH
)
4108 if (TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
)
4110 /* FixedPitch Penalty 15000 */
4111 /* Requested a fixed pitch font, but the candidate is a
4112 variable pitch font. */
4116 if (Byte
== VARIABLE_PITCH
)
4118 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4120 /* PitchVariable Penalty 350 */
4121 /* Requested a variable pitch font, but the candidate is not a
4122 variable pitch font. */
4127 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4128 if (Byte
== DEFAULT_PITCH
)
4130 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4132 /* DefaultPitchFixed Penalty 1 */
4133 /* Requested DEFAULT_PITCH, but the candidate is fixed pitch. */
4138 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
4140 if (LogFont
->lfFaceName
[0])
4144 /* localized family name */
4147 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4149 /* localized full name */
4152 ActualNameW
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
4153 Found
= (_wcsicmp(LogFont
->lfFaceName
, ActualNameW
) == 0);
4157 /* FaceName Penalty 10000 */
4158 /* Requested a face name, but the candidate's face name
4164 Byte
= (LogFont
->lfPitchAndFamily
& 0xF0);
4165 if (Byte
!= FF_DONTCARE
)
4167 if (Byte
!= (TM
->tmPitchAndFamily
& 0xF0))
4169 /* Family Penalty 9000 */
4170 /* Requested a family, but the candidate's family is different. */
4173 if ((TM
->tmPitchAndFamily
& 0xF0) == FF_DONTCARE
)
4175 /* FamilyUnknown Penalty 8000 */
4176 /* Requested a family, but the candidate has no family. */
4181 /* Is the candidate a non-vector font? */
4182 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4184 /* Is lfHeight specified? */
4185 if (LogFont
->lfHeight
!= 0)
4187 if (labs(LogFont
->lfHeight
) < TM
->tmHeight
)
4189 /* HeightBigger Penalty 600 */
4190 /* The candidate is a nonvector font and is bigger than the
4191 requested height. */
4193 /* HeightBiggerDifference Penalty 150 */
4194 /* The candidate is a raster font and is larger than the
4195 requested height. Penalty * height difference */
4196 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4198 fNeedScaling
= TRUE
;
4200 if (TM
->tmHeight
< labs(LogFont
->lfHeight
))
4202 /* HeightSmaller Penalty 150 */
4203 /* The candidate is a raster font and is smaller than the
4204 requested height. Penalty * height difference */
4205 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4207 fNeedScaling
= TRUE
;
4212 switch (LogFont
->lfPitchAndFamily
& 0xF0)
4214 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4215 switch (TM
->tmPitchAndFamily
& 0xF0)
4217 case FF_DECORATIVE
: case FF_SCRIPT
:
4218 /* FamilyUnlikely Penalty 50 */
4219 /* Requested a roman/modern/swiss family, but the
4220 candidate is decorative/script. */
4227 case FF_DECORATIVE
: case FF_SCRIPT
:
4228 switch (TM
->tmPitchAndFamily
& 0xF0)
4230 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4231 /* FamilyUnlikely Penalty 50 */
4232 /* Or requested decorative/script, and the candidate is
4233 roman/modern/swiss. */
4243 if (LogFont
->lfWidth
!= 0)
4245 if (LogFont
->lfWidth
!= TM
->tmAveCharWidth
)
4247 /* Width Penalty 50 */
4248 /* Requested a nonzero width, but the candidate's width
4249 doesn't match. Penalty * width difference */
4250 Penalty
+= 50 * labs(LogFont
->lfWidth
- TM
->tmAveCharWidth
);
4252 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4253 fNeedScaling
= TRUE
;
4259 /* SizeSynth Penalty 50 */
4260 /* The candidate is a raster font that needs scaling by GDI. */
4264 if (!!LogFont
->lfItalic
!= !!TM
->tmItalic
)
4266 if (!LogFont
->lfItalic
&& ItalicFromStyle(style_name
))
4268 /* Italic Penalty 4 */
4269 /* Requested font and candidate font do not agree on italic status,
4270 and the desired result cannot be simulated. */
4271 /* Adjusted to 40 to satisfy (Oblique Penalty > Book Penalty). */
4274 else if (LogFont
->lfItalic
&& !ItalicFromStyle(style_name
))
4276 /* ItalicSim Penalty 1 */
4277 /* Requested italic font but the candidate is not italic,
4278 although italics can be simulated. */
4283 if (LogFont
->lfOutPrecision
== OUT_TT_PRECIS
)
4285 if (!(TM
->tmPitchAndFamily
& TMPF_TRUETYPE
))
4287 /* NotTrueType Penalty 4 */
4288 /* Requested OUT_TT_PRECIS, but the candidate is not a
4294 Long
= LogFont
->lfWeight
;
4295 if (LogFont
->lfWeight
== FW_DONTCARE
)
4297 if (Long
!= TM
->tmWeight
)
4299 /* Weight Penalty 3 */
4300 /* The candidate's weight does not match the requested weight.
4301 Penalty * (weight difference/10) */
4302 Penalty
+= 3 * (labs(Long
- TM
->tmWeight
) / 10);
4305 if (!LogFont
->lfUnderline
&& TM
->tmUnderlined
)
4307 /* Underline Penalty 3 */
4308 /* Requested font has no underline, but the candidate is
4313 if (!LogFont
->lfStrikeOut
&& TM
->tmStruckOut
)
4315 /* StrikeOut Penalty 3 */
4316 /* Requested font has no strike-out, but the candidate is
4321 /* Is the candidate a non-vector font? */
4322 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4324 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
< LogFont
->lfHeight
)
4326 /* VectorHeightSmaller Penalty 2 */
4327 /* Candidate is a vector font that is smaller than the
4328 requested height. Penalty * height difference */
4329 Penalty
+= 2 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4331 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
> LogFont
->lfHeight
)
4333 /* VectorHeightBigger Penalty 1 */
4334 /* Candidate is a vector font that is bigger than the
4335 requested height. Penalty * height difference */
4336 Penalty
+= 1 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4340 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
))
4342 /* DeviceFavor Penalty 2 */
4343 /* Extra penalty for all nondevice fonts. */
4349 DPRINT("WARNING: Penalty:%ld < 200: RequestedNameW:%ls, "
4350 "ActualNameW:%ls, lfCharSet:%d, lfWeight:%ld, "
4351 "tmCharSet:%d, tmWeight:%ld\n",
4352 Penalty
, LogFont
->lfFaceName
, ActualNameW
,
4353 LogFont
->lfCharSet
, LogFont
->lfWeight
,
4354 TM
->tmCharSet
, TM
->tmWeight
);
4357 return Penalty
; /* success */
4360 static __inline VOID
4361 FindBestFontFromList(FONTOBJ
**FontObj
, ULONG
*MatchPenalty
,
4362 const LOGFONTW
*LogFont
,
4363 const PLIST_ENTRY Head
)
4367 PFONT_ENTRY CurrentEntry
;
4369 OUTLINETEXTMETRICW
*Otm
= NULL
;
4370 UINT OtmSize
, OldOtmSize
= 0;
4374 ASSERT(MatchPenalty
);
4378 /* Start with a pretty big buffer */
4380 Otm
= ExAllocatePoolWithTag(PagedPool
, OldOtmSize
, GDITAG_TEXT
);
4382 /* get the FontObj of lowest penalty */
4383 Entry
= Head
->Flink
;
4384 while (Entry
!= Head
)
4386 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
4387 Entry
= Entry
->Flink
;
4389 FontGDI
= CurrentEntry
->Font
;
4391 Face
= FontGDI
->SharedFace
->Face
;
4393 /* get text metrics */
4394 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
4395 if (OtmSize
> OldOtmSize
)
4398 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4399 Otm
= ExAllocatePoolWithTag(PagedPool
, OtmSize
, GDITAG_TEXT
);
4402 /* update FontObj if lowest penalty */
4405 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, OtmSize
, Otm
);
4409 OldOtmSize
= OtmSize
;
4411 Penalty
= GetFontPenalty(LogFont
, Otm
, Face
->style_name
);
4412 if (*MatchPenalty
== 0xFFFFFFFF || Penalty
< *MatchPenalty
)
4414 *FontObj
= GDIToObj(FontGDI
, FONT
);
4415 *MatchPenalty
= Penalty
;
4421 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4427 IntFontType(PFONTGDI Font
)
4429 PS_FontInfoRec psfInfo
;
4430 FT_ULong tmp_size
= 0;
4431 FT_Face Face
= Font
->SharedFace
->Face
;
4433 if (FT_HAS_MULTIPLE_MASTERS(Face
))
4434 Font
->FontObj
.flFontType
|= FO_MULTIPLEMASTER
;
4435 if (FT_HAS_VERTICAL(Face
))
4436 Font
->FontObj
.flFontType
|= FO_VERT_FACE
;
4437 if (!FT_IS_SCALABLE(Face
))
4438 Font
->FontObj
.flFontType
|= FO_TYPE_RASTER
;
4439 if (FT_IS_SFNT(Face
))
4441 Font
->FontObj
.flFontType
|= FO_TYPE_TRUETYPE
;
4442 if (FT_Get_Sfnt_Table(Face
, ft_sfnt_post
))
4443 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4445 if (!FT_Get_PS_Font_Info(Face
, &psfInfo
))
4447 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4449 /* Check for the presence of the 'CFF ' table to check if the font is Type1 */
4450 if (!FT_Load_Sfnt_Table(Face
, TTAG_CFF
, 0, NULL
, &tmp_size
))
4452 Font
->FontObj
.flFontType
|= (FO_CFF
|FO_POSTSCRIPT
);
4458 TextIntRealizeFont(HFONT FontHandle
, PTEXTOBJ pTextObj
)
4460 NTSTATUS Status
= STATUS_SUCCESS
;
4462 PPROCESSINFO Win32Process
;
4465 LOGFONTW SubstitutedLogFont
;
4470 TextObj
= TEXTOBJ_LockText(FontHandle
);
4471 if (NULL
== TextObj
)
4473 return STATUS_INVALID_HANDLE
;
4476 if (TextObj
->fl
& TEXTOBJECT_INIT
)
4478 TEXTOBJ_UnlockText(TextObj
);
4479 return STATUS_SUCCESS
;
4487 pLogFont
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4490 SubstitutedLogFont
= *pLogFont
;
4491 DPRINT("Font '%S,%u' is substituted by: ", pLogFont
->lfFaceName
, pLogFont
->lfCharSet
);
4492 SubstituteFontRecurse(&SubstitutedLogFont
);
4493 DPRINT("'%S,%u'.\n", SubstitutedLogFont
.lfFaceName
, SubstitutedLogFont
.lfCharSet
);
4495 MatchPenalty
= 0xFFFFFFFF;
4496 TextObj
->Font
= NULL
;
4498 Win32Process
= PsGetCurrentProcessWin32Process();
4500 /* Search private fonts */
4501 IntLockProcessPrivateFonts(Win32Process
);
4502 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
4503 &Win32Process
->PrivateFontListHead
);
4504 IntUnLockProcessPrivateFonts(Win32Process
);
4506 /* Search system fonts */
4507 IntLockGlobalFonts();
4508 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, &SubstitutedLogFont
,
4510 IntUnLockGlobalFonts();
4512 if (NULL
== TextObj
->Font
)
4514 DPRINT1("Request font %S not found, no fonts loaded at all\n",
4515 pLogFont
->lfFaceName
);
4516 Status
= STATUS_NOT_FOUND
;
4520 PFONTGDI FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
4521 // Need hdev, when freetype is loaded need to create DEVOBJ for
4522 // Consumer and Producer.
4523 TextObj
->Font
->iUniq
= 1; // Now it can be cached.
4524 IntFontType(FontGdi
);
4525 FontGdi
->flType
= TextObj
->Font
->flFontType
;
4526 FontGdi
->RequestUnderline
= pLogFont
->lfUnderline
? 0xFF : 0;
4527 FontGdi
->RequestStrikeOut
= pLogFont
->lfStrikeOut
? 0xFF : 0;
4528 FontGdi
->RequestItalic
= pLogFont
->lfItalic
? 0xFF : 0;
4529 if (pLogFont
->lfWeight
!= FW_DONTCARE
)
4530 FontGdi
->RequestWeight
= pLogFont
->lfWeight
;
4532 FontGdi
->RequestWeight
= FW_NORMAL
;
4534 Face
= FontGdi
->SharedFace
->Face
;
4536 //FontGdi->OriginalWeight = WeightFromStyle(Face->style_name);
4538 if (!FontGdi
->OriginalItalic
)
4539 FontGdi
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
4541 TextObj
->fl
|= TEXTOBJECT_INIT
;
4542 Status
= STATUS_SUCCESS
;
4545 if (!pTextObj
) TEXTOBJ_UnlockText(TextObj
);
4547 ASSERT((NT_SUCCESS(Status
) ^ (NULL
== TextObj
->Font
)) != 0);
4557 POBJECT_NAME_INFORMATION NameInfo
,
4559 PUNICODE_STRING FileName
)
4562 OBJECT_ATTRIBUTES ObjectAttributes
;
4564 IO_STATUS_BLOCK IoStatusBlock
;
4567 InitializeObjectAttributes(&ObjectAttributes
,
4569 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
4573 Status
= ZwOpenFile(
4575 0, // FILE_READ_ATTRIBUTES,
4578 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
4581 if (!NT_SUCCESS(Status
))
4583 DPRINT("ZwOpenFile() failed (Status = 0x%lx)\n", Status
);
4587 Status
= ZwQueryObject(hFile
, ObjectNameInformation
, NameInfo
, Size
, &Desired
);
4589 if (!NT_SUCCESS(Status
))
4591 DPRINT("ZwQueryObject() failed (Status = %lx)\n", Status
);
4599 EqualFamilyInfo(const FONTFAMILYINFO
*pInfo1
, const FONTFAMILYINFO
*pInfo2
)
4601 const ENUMLOGFONTEXW
*pLog1
= &pInfo1
->EnumLogFontEx
;
4602 const ENUMLOGFONTEXW
*pLog2
= &pInfo2
->EnumLogFontEx
;
4603 const LOGFONTW
*plf1
= &pLog1
->elfLogFont
;
4604 const LOGFONTW
*plf2
= &pLog2
->elfLogFont
;
4606 if (_wcsicmp(plf1
->lfFaceName
, plf2
->lfFaceName
) != 0)
4611 if (_wcsicmp(pLog1
->elfStyle
, pLog2
->elfStyle
) != 0)
4620 IntAddNameFromFamInfo(LPWSTR psz
, FONTFAMILYINFO
*FamInfo
)
4622 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfLogFont
.lfFaceName
);
4623 if (FamInfo
->EnumLogFontEx
.elfStyle
[0] &&
4624 _wcsicmp(FamInfo
->EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4627 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfStyle
);
4633 IntGdiGetFontResourceInfo(
4634 PUNICODE_STRING FileName
,
4639 UNICODE_STRING EntryFileName
;
4640 POBJECT_NAME_INFORMATION NameInfo1
, NameInfo2
;
4641 PLIST_ENTRY ListEntry
;
4642 PFONT_ENTRY FontEntry
;
4643 ULONG Size
, i
, Count
;
4646 FONTFAMILYINFO
*FamInfo
;
4647 const ULONG MaxFamInfo
= 64;
4650 DPRINT("IntGdiGetFontResourceInfo: dwType == %lu\n", dwType
);
4652 /* Create buffer for full path name */
4653 Size
= sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
* sizeof(WCHAR
);
4654 NameInfo1
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4657 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4661 /* Get the full path name */
4662 if (!IntGetFullFileName(NameInfo1
, Size
, FileName
))
4664 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4668 /* Create a buffer for the entries' names */
4669 NameInfo2
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4672 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4673 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4677 FamInfo
= ExAllocatePoolWithTag(PagedPool
,
4678 sizeof(FONTFAMILYINFO
) * MaxFamInfo
,
4682 ExFreePoolWithTag(NameInfo2
, TAG_FINF
);
4683 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4684 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4687 /* Try to find the pathname in the global font list */
4689 IntLockGlobalFonts();
4690 for (ListEntry
= g_FontListHead
.Flink
; ListEntry
!= &g_FontListHead
;
4691 ListEntry
= ListEntry
->Flink
)
4693 FontEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY
, ListEntry
);
4694 if (FontEntry
->Font
->Filename
== NULL
)
4697 RtlInitUnicodeString(&EntryFileName
, FontEntry
->Font
->Filename
);
4698 if (!IntGetFullFileName(NameInfo2
, Size
, &EntryFileName
))
4701 if (!RtlEqualUnicodeString(&NameInfo1
->Name
, &NameInfo2
->Name
, FALSE
))
4705 FontFamilyFillInfo(&FamInfo
[Count
], FontEntry
->FaceName
.Buffer
,
4706 NULL
, FontEntry
->Font
);
4707 for (i
= 0; i
< Count
; ++i
)
4709 if (EqualFamilyInfo(&FamInfo
[i
], &FamInfo
[Count
]))
4719 if (Count
>= MaxFamInfo
)
4723 IntUnLockGlobalFonts();
4725 /* Free the buffers */
4726 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4727 ExFreePool(NameInfo2
);
4729 if (Count
== 0 && dwType
!= 5)
4731 /* Font could not be found in system table
4732 dwType == 5 will still handle this */
4733 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
4740 case 0: /* FIXME: Returns 1 or 2, don't know what this is atm */
4741 Size
= sizeof(DWORD
);
4749 if (*pdwBytes
>= Size
)
4751 *(DWORD
*)pBuffer
= Count
;
4758 case 1: /* copy the font title */
4759 /* calculate the required size */
4761 Size
+= wcslen(FamInfo
[0].EnumLogFontEx
.elfLogFont
.lfFaceName
);
4762 if (FamInfo
[0].EnumLogFontEx
.elfStyle
[0] &&
4763 _wcsicmp(FamInfo
[0].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4765 Size
+= 1 + wcslen(FamInfo
[0].EnumLogFontEx
.elfStyle
);
4767 for (i
= 1; i
< Count
; ++i
)
4769 Size
+= 3; /* " & " */
4770 Size
+= wcslen(FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
4771 if (FamInfo
[i
].EnumLogFontEx
.elfStyle
[0] &&
4772 _wcsicmp(FamInfo
[i
].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4774 Size
+= 1 + wcslen(FamInfo
[i
].EnumLogFontEx
.elfStyle
);
4777 Size
+= 2; /* "\0\0" */
4778 Size
*= sizeof(WCHAR
);
4787 if (*pdwBytes
>= Size
)
4789 /* store font title to buffer */
4790 WCHAR
*psz
= pBuffer
;
4792 IntAddNameFromFamInfo(psz
, &FamInfo
[0]);
4793 for (i
= 1; i
< Count
; ++i
)
4795 wcscat(psz
, L
" & ");
4796 IntAddNameFromFamInfo(psz
, &FamInfo
[i
]);
4798 psz
[wcslen(psz
) + 1] = UNICODE_NULL
;
4804 *pdwBytes
= 1024; /* this is confirmed value */
4809 case 2: /* Copy an array of LOGFONTW */
4810 Size
= Count
* sizeof(LOGFONTW
);
4818 if (*pdwBytes
>= Size
)
4820 pbBuffer
= (LPBYTE
)pBuffer
;
4821 for (i
= 0; i
< Count
; ++i
)
4823 FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfWidth
= 0;
4824 RtlCopyMemory(pbBuffer
, &FamInfo
[i
].EnumLogFontEx
.elfLogFont
, sizeof(LOGFONTW
));
4825 pbBuffer
+= sizeof(LOGFONTW
);
4833 *pdwBytes
= 1024; /* this is confirmed value */
4838 Size
= sizeof(DWORD
);
4846 if (*pdwBytes
>= Size
)
4848 /* FIXME: What exactly is copied here? */
4849 *(DWORD
*)pBuffer
= 1;
4856 case 4: /* full file path */
4857 if (FileName
->Length
>= 4 * sizeof(WCHAR
))
4859 /* The beginning of FileName is \??\ */
4860 LPWSTR pch
= FileName
->Buffer
+ 4;
4861 DWORD Length
= FileName
->Length
- 4 * sizeof(WCHAR
);
4863 Size
= Length
+ sizeof(WCHAR
);
4871 if (*pdwBytes
>= Size
)
4873 RtlCopyMemory(pBuffer
, pch
, Size
);
4881 case 5: /* Looks like a BOOL that is copied, TRUE, if the font was not found */
4882 Size
= sizeof(BOOL
);
4890 if (*pdwBytes
>= Size
)
4892 *(BOOL
*)pBuffer
= Count
== 0;
4899 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
4907 ftGdiRealizationInfo(PFONTGDI Font
, PREALIZATION_INFO Info
)
4909 if (FT_HAS_FIXED_SIZES(Font
->SharedFace
->Face
))
4910 Info
->iTechnology
= RI_TECH_BITMAP
;
4913 if (FT_IS_SCALABLE(Font
->SharedFace
->Face
))
4914 Info
->iTechnology
= RI_TECH_SCALABLE
;
4916 Info
->iTechnology
= RI_TECH_FIXED
;
4918 Info
->iUniq
= Font
->FontObj
.iUniq
;
4919 Info
->dwUnknown
= -1;
4926 ftGdiGetKerningPairs( PFONTGDI Font
,
4928 LPKERNINGPAIR pKerningPair
)
4932 FT_Face face
= Font
->SharedFace
->Face
;
4934 if (FT_HAS_KERNING(face
) && face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
4936 FT_UInt previous_index
= 0, glyph_index
= 0;
4937 FT_ULong char_code
, char_previous
;
4940 char_previous
= char_code
= FT_Get_First_Char(face
, &glyph_index
);
4946 if (previous_index
&& glyph_index
)
4948 FT_Get_Kerning(face
, previous_index
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
4950 if (pKerningPair
&& cPairs
)
4952 pKerningPair
[i
].wFirst
= char_previous
;
4953 pKerningPair
[i
].wSecond
= char_code
;
4954 pKerningPair
[i
].iKernAmount
= delta
.x
;
4956 if (i
== cPairs
) break;
4960 previous_index
= glyph_index
;
4961 char_previous
= char_code
;
4962 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_index
);
4964 IntUnLockFreeType();
4970 ///////////////////////////////////////////////////////////////////////////
4972 // Functions needing sorting.
4974 ///////////////////////////////////////////////////////////////////////////
4976 NtGdiGetFontFamilyInfo(HDC Dc
,
4977 LPLOGFONTW UnsafeLogFont
,
4978 PFONTFAMILYINFO UnsafeInfo
,
4983 PFONTFAMILYINFO Info
;
4985 PPROCESSINFO Win32Process
;
4987 /* Make a safe copy */
4988 Status
= MmCopyFromCaller(&LogFont
, UnsafeLogFont
, sizeof(LOGFONTW
));
4989 if (! NT_SUCCESS(Status
))
4991 EngSetLastError(ERROR_INVALID_PARAMETER
);
4995 /* Allocate space for a safe copy */
4996 Info
= ExAllocatePoolWithTag(PagedPool
, Size
* sizeof(FONTFAMILYINFO
), GDITAG_TEXT
);
4999 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5003 /* Enumerate font families in the global list */
5004 IntLockGlobalFonts();
5006 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
, &g_FontListHead
) )
5008 IntUnLockGlobalFonts();
5009 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5012 IntUnLockGlobalFonts();
5014 /* Enumerate font families in the process local list */
5015 Win32Process
= PsGetCurrentProcessWin32Process();
5016 IntLockProcessPrivateFonts(Win32Process
);
5017 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
,
5018 &Win32Process
->PrivateFontListHead
))
5020 IntUnLockProcessPrivateFonts(Win32Process
);
5021 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5024 IntUnLockProcessPrivateFonts(Win32Process
);
5026 /* Enumerate font families in the registry */
5027 if (! GetFontFamilyInfoForSubstitutes(&LogFont
, Info
, &Count
, Size
))
5029 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5033 /* Return data to caller */
5036 Status
= MmCopyToCaller(UnsafeInfo
, Info
,
5037 (Count
< Size
? Count
: Size
) * sizeof(FONTFAMILYINFO
));
5038 if (! NT_SUCCESS(Status
))
5040 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5041 EngSetLastError(ERROR_INVALID_PARAMETER
);
5046 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5053 ScaleLong(LONG lValue
, PFLOATOBJ pef
)
5057 /* Check if we have scaling different from 1 */
5058 if (!FLOATOBJ_Equal(pef
, (PFLOATOBJ
)&gef1
))
5060 /* Need to multiply */
5061 FLOATOBJ_SetLong(&efTemp
, lValue
);
5062 FLOATOBJ_Mul(&efTemp
, pef
);
5063 lValue
= FLOATOBJ_GetLong(&efTemp
);
5076 IN OPTIONAL PRECTL lprc
,
5079 IN OPTIONAL LPINT Dx
,
5080 IN DWORD dwCodePage
)
5084 * Call EngTextOut, which does the real work (calling DrvTextOut where
5091 SURFACE
*psurf
= NULL
;
5092 int error
, glyph_index
, i
;
5095 FT_BitmapGlyph realglyph
;
5096 LONGLONG TextLeft
, RealXStart
;
5097 ULONG TextTop
, previous
, BackgroundLeft
;
5098 FT_Bool use_kerning
;
5099 RECTL DestRect
, MaskRect
;
5100 POINTL SourcePoint
, BrushOrigin
;
5101 HBITMAP HSourceGlyph
;
5102 SURFOBJ
*SourceGlyphSurf
;
5107 PTEXTOBJ TextObj
= NULL
;
5108 EXLATEOBJ exloRGB2Dst
, exloDst2RGB
;
5109 FT_Render_Mode RenderMode
;
5112 BOOL DoBreak
= FALSE
;
5114 PMATRIX pmxWorldToDevice
;
5115 LONG fixAscender
, fixDescender
;
5118 BOOL EmuBold
, EmuItalic
;
5122 /* Check if String is valid */
5123 if ((Count
> 0xFFFF) || (Count
> 0 && String
== NULL
))
5125 EngSetLastError(ERROR_INVALID_PARAMETER
);
5129 /* NOTE: This function locks the screen DC, so it must never be called
5130 with a DC already locked */
5131 Render
= IntIsFontRenderingEnabled();
5133 // TODO: Write test-cases to exactly match real Windows in different
5134 // bad parameters (e.g. does Windows check the DC or the RECT first?).
5135 dc
= DC_LockDc(hDC
);
5138 EngSetLastError(ERROR_INVALID_HANDLE
);
5142 if (PATH_IsPathOpen(dc
->dclevel
))
5144 bResult
= PATH_ExtTextOut(dc
,
5148 (const RECTL
*)lprc
,
5156 DC_vPrepareDCsForBlit(dc
, NULL
, NULL
, NULL
);
5158 if (!dc
->dclevel
.pSurface
)
5160 /* Memory DC with no surface selected */
5165 pdcattr
= dc
->pdcattr
;
5167 if (lprc
&& (fuOptions
& (ETO_OPAQUE
| ETO_CLIPPED
)))
5169 IntLPtoDP(dc
, (POINT
*)lprc
, 2);
5172 if (pdcattr
->lTextAlign
& TA_UPDATECP
)
5174 Start
.x
= pdcattr
->ptlCurrent
.x
;
5175 Start
.y
= pdcattr
->ptlCurrent
.y
;
5181 IntLPtoDP(dc
, &Start
, 1);
5182 RealXStart
= ((LONGLONG
)Start
.x
+ dc
->ptlDCOrig
.x
) << 6;
5183 YStart
= Start
.y
+ dc
->ptlDCOrig
.y
;
5192 if ((fuOptions
& ETO_OPAQUE
) && lprc
)
5194 DestRect
.left
= lprc
->left
;
5195 DestRect
.top
= lprc
->top
;
5196 DestRect
.right
= lprc
->right
;
5197 DestRect
.bottom
= lprc
->bottom
;
5199 DestRect
.left
+= dc
->ptlDCOrig
.x
;
5200 DestRect
.top
+= dc
->ptlDCOrig
.y
;
5201 DestRect
.right
+= dc
->ptlDCOrig
.x
;
5202 DestRect
.bottom
+= dc
->ptlDCOrig
.y
;
5204 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5206 IntUpdateBoundsRect(dc
, &DestRect
);
5209 if (pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
5210 DC_vUpdateBackgroundBrush(dc
);
5211 if (dc
->dctype
== DCTYPE_DIRECT
)
5212 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5214 psurf
= dc
->dclevel
.pSurface
;
5224 &dc
->eboBackground
.BrushObject
,
5226 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5228 if (dc
->dctype
== DCTYPE_DIRECT
)
5229 MouseSafetyOnDrawEnd(dc
->ppdev
);
5231 fuOptions
&= ~ETO_OPAQUE
;
5235 if (pdcattr
->jBkMode
== OPAQUE
)
5237 fuOptions
|= ETO_OPAQUE
;
5241 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
5242 if (TextObj
== NULL
)
5248 FontObj
= TextObj
->Font
;
5250 FontGDI
= ObjToGDI(FontObj
, FONT
);
5254 face
= FontGDI
->SharedFace
->Face
;
5256 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
5257 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
5258 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
5261 RenderMode
= IntGetFontRenderMode(plf
);
5263 RenderMode
= FT_RENDER_MODE_MONO
;
5265 if (!TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
))
5267 IntUnLockFreeType();
5272 if (dc
->pdcattr
->iGraphicsMode
== GM_ADVANCED
)
5274 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
5275 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5277 fixAscender
= ScaleLong(face
->size
->metrics
.ascender
, &pmxWorldToDevice
->efM22
);
5278 fixDescender
= ScaleLong(face
->size
->metrics
.descender
, &pmxWorldToDevice
->efM22
);
5282 pmxWorldToDevice
= (PMATRIX
)&gmxWorldToDeviceDefault
;
5283 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5285 fixAscender
= face
->size
->metrics
.ascender
;
5286 fixDescender
= face
->size
->metrics
.descender
;
5290 * Process the vertical alignment and determine the yoff.
5293 if (pdcattr
->lTextAlign
& TA_BASELINE
)
5295 else if (pdcattr
->lTextAlign
& TA_BOTTOM
)
5296 yoff
= -fixDescender
>> 6;
5298 yoff
= fixAscender
>> 6;
5300 use_kerning
= FT_HAS_KERNING(face
);
5304 * Process the horizontal alignment and modify XStart accordingly.
5306 DxShift
= fuOptions
& ETO_PDY
? 1 : 0;
5307 if (pdcattr
->lTextAlign
& (TA_RIGHT
| TA_CENTER
))
5309 ULONGLONG TextWidth
= 0;
5310 LPCWSTR TempText
= String
;
5314 * Calculate width of the text.
5319 iStart
= Count
< 2 ? 0 : Count
- 2;
5320 TextWidth
= Count
< 2 ? 0 : (Dx
[(Count
-2)<<DxShift
] << 6);
5326 TempText
= String
+ iStart
;
5328 for (i
= iStart
; i
< Count
; i
++)
5330 if (fuOptions
& ETO_GLYPH_INDEX
)
5331 glyph_index
= *TempText
;
5333 glyph_index
= FT_Get_Char_Index(face
, *TempText
);
5335 if (EmuBold
|| EmuItalic
)
5338 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
5339 RenderMode
, pmxWorldToDevice
);
5342 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5345 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
5348 glyph
= face
->glyph
;
5349 if (EmuBold
|| EmuItalic
)
5352 FT_GlyphSlot_Embolden(glyph
);
5354 FT_GlyphSlot_Oblique(glyph
);
5355 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5359 realglyph
= ftGdiGlyphCacheSet(face
,
5368 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5369 IntUnLockFreeType();
5374 /* Retrieve kerning distance */
5375 if (use_kerning
&& previous
&& glyph_index
)
5378 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5379 TextWidth
+= delta
.x
;
5382 TextWidth
+= realglyph
->root
.advance
.x
>> 10;
5384 if (EmuBold
|| EmuItalic
)
5386 FT_Done_Glyph((FT_Glyph
)realglyph
);
5390 previous
= glyph_index
;
5396 if ((pdcattr
->lTextAlign
& TA_CENTER
) == TA_CENTER
)
5398 RealXStart
-= TextWidth
/ 2;
5402 RealXStart
-= TextWidth
;
5406 psurf
= dc
->dclevel
.pSurface
;
5407 SurfObj
= &psurf
->SurfObj
;
5409 if ((fuOptions
& ETO_OPAQUE
) && (dc
->pdcattr
->ulDirty_
& DIRTY_BACKGROUND
))
5410 DC_vUpdateBackgroundBrush(dc
) ;
5412 if(dc
->pdcattr
->ulDirty_
& DIRTY_TEXT
)
5413 DC_vUpdateTextBrush(dc
) ;
5415 if (!face
->units_per_EM
)
5421 thickness
= face
->underline_thickness
*
5422 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5427 if ((fuOptions
& ETO_OPAQUE
) && plf
->lfItalic
)
5429 /* Draw background */
5430 TextLeft
= RealXStart
;
5432 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5433 for (i
= 0; i
< Count
; ++i
)
5435 if (fuOptions
& ETO_GLYPH_INDEX
)
5436 glyph_index
= String
[i
];
5438 glyph_index
= FT_Get_Char_Index(face
, String
[i
]);
5440 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5443 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5444 IntUnLockFreeType();
5448 glyph
= face
->glyph
;
5450 FT_GlyphSlot_Embolden(glyph
);
5452 FT_GlyphSlot_Oblique(glyph
);
5453 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5456 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5457 IntUnLockFreeType();
5461 /* retrieve kerning distance and move pen position */
5462 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5465 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5466 TextLeft
+= delta
.x
;
5468 DPRINT("TextLeft: %I64d\n", TextLeft
);
5469 DPRINT("TextTop: %lu\n", TextTop
);
5470 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5472 DestRect
.left
= BackgroundLeft
;
5473 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5474 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5475 DestRect
.bottom
= TextTop
+ yoff
+ ((32 - fixDescender
) >> 6);
5476 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5477 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5479 IntUpdateBoundsRect(dc
, &DestRect
);
5490 &dc
->eboBackground
.BrushObject
,
5492 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5493 MouseSafetyOnDrawEnd(dc
->ppdev
);
5494 BackgroundLeft
= DestRect
.right
;
5496 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5497 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5498 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5499 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5501 bitSize
.cx
= realglyph
->bitmap
.width
;
5502 bitSize
.cy
= realglyph
->bitmap
.rows
;
5503 MaskRect
.right
= realglyph
->bitmap
.width
;
5504 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5508 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
5509 DPRINT("New TextLeft: %I64d\n", TextLeft
);
5513 // FIXME this should probably be a matrix transform with TextTop as well.
5514 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
5515 if (FLOATOBJ_Equal0(&Scale
))
5516 FLOATOBJ_Set1(&Scale
);
5518 /* do the shift before multiplying to preserve precision */
5519 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
5520 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
5521 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
5526 TextTop
-= Dx
[2 * i
+ 1] << 6;
5529 previous
= glyph_index
;
5531 if (EmuBold
|| EmuItalic
)
5533 FT_Done_Glyph((FT_Glyph
)realglyph
);
5539 EXLATEOBJ_vInitialize(&exloRGB2Dst
, &gpalRGB
, psurf
->ppal
, 0, 0, 0);
5540 EXLATEOBJ_vInitialize(&exloDst2RGB
, psurf
->ppal
, &gpalRGB
, 0, 0, 0);
5542 /* Assume success */
5546 * The main rendering loop.
5548 TextLeft
= RealXStart
;
5550 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5551 for (i
= 0; i
< Count
; ++i
)
5553 if (fuOptions
& ETO_GLYPH_INDEX
)
5554 glyph_index
= String
[i
];
5556 glyph_index
= FT_Get_Char_Index(face
, String
[i
]);
5558 if (EmuBold
|| EmuItalic
)
5561 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
, plf
->lfHeight
,
5562 RenderMode
, pmxWorldToDevice
);
5565 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5568 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5573 glyph
= face
->glyph
;
5574 if (EmuBold
|| EmuItalic
)
5577 FT_GlyphSlot_Embolden(glyph
);
5579 FT_GlyphSlot_Oblique(glyph
);
5580 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5584 realglyph
= ftGdiGlyphCacheSet(face
,
5593 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5599 /* retrieve kerning distance and move pen position */
5600 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5603 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5604 TextLeft
+= delta
.x
;
5606 DPRINT("TextLeft: %I64d\n", TextLeft
);
5607 DPRINT("TextTop: %lu\n", TextTop
);
5608 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5610 if ((fuOptions
& ETO_OPAQUE
) && !plf
->lfItalic
)
5612 DestRect
.left
= BackgroundLeft
;
5613 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5614 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5615 DestRect
.bottom
= TextTop
+ yoff
+ ((32 - fixDescender
) >> 6);
5617 if (dc
->dctype
== DCTYPE_DIRECT
)
5618 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5620 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5622 IntUpdateBoundsRect(dc
, &DestRect
);
5633 &dc
->eboBackground
.BrushObject
,
5635 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5637 if (dc
->dctype
== DCTYPE_DIRECT
)
5638 MouseSafetyOnDrawEnd(dc
->ppdev
);
5640 BackgroundLeft
= DestRect
.right
;
5643 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5644 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5645 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5646 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5648 bitSize
.cx
= realglyph
->bitmap
.width
;
5649 bitSize
.cy
= realglyph
->bitmap
.rows
;
5650 MaskRect
.right
= realglyph
->bitmap
.width
;
5651 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5653 /* Check if the bitmap has any pixels */
5654 if ((bitSize
.cx
!= 0) && (bitSize
.cy
!= 0))
5657 * We should create the bitmap out of the loop at the biggest possible
5658 * glyph size. Then use memset with 0 to clear it and sourcerect to
5659 * limit the work of the transbitblt.
5661 HSourceGlyph
= EngCreateBitmap(bitSize
, realglyph
->bitmap
.pitch
,
5662 BMF_8BPP
, BMF_TOPDOWN
,
5663 realglyph
->bitmap
.buffer
);
5664 if ( !HSourceGlyph
)
5666 DPRINT1("WARNING: EngCreateBitmap() failed!\n");
5667 // FT_Done_Glyph(realglyph);
5671 SourceGlyphSurf
= EngLockSurface((HSURF
)HSourceGlyph
);
5672 if ( !SourceGlyphSurf
)
5674 EngDeleteSurface((HSURF
)HSourceGlyph
);
5675 DPRINT1("WARNING: EngLockSurface() failed!\n");
5681 * Use the font data as a mask to paint onto the DCs surface using a
5684 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5685 DestRect
.right
>= lprc
->right
+ dc
->ptlDCOrig
.x
)
5687 // We do the check '>=' instead of '>' to possibly save an iteration
5688 // through this loop, since it's breaking after the drawing is done,
5689 // and x is always incremented.
5690 DestRect
.right
= lprc
->right
+ dc
->ptlDCOrig
.x
;
5693 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5694 DestRect
.bottom
>= lprc
->bottom
+ dc
->ptlDCOrig
.y
)
5696 DestRect
.bottom
= lprc
->bottom
+ dc
->ptlDCOrig
.y
;
5699 if (dc
->dctype
== DCTYPE_DIRECT
)
5700 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5710 &dc
->eboText
.BrushObject
,
5713 DPRINT1("Failed to MaskBlt a glyph!\n");
5716 if (dc
->dctype
== DCTYPE_DIRECT
)
5717 MouseSafetyOnDrawEnd(dc
->ppdev
) ;
5719 EngUnlockSurface(SourceGlyphSurf
);
5720 EngDeleteSurface((HSURF
)HSourceGlyph
);
5728 if (plf
->lfUnderline
)
5731 if (!face
->units_per_EM
)
5737 position
= face
->underline_position
*
5738 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5740 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5744 &dc
->eboText
.BrushObject
,
5746 TextTop
+ yoff
- position
+ i
,
5747 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5748 TextTop
+ yoff
- position
+ i
,
5750 ROP2_TO_MIX(R2_COPYPEN
));
5753 if (plf
->lfStrikeOut
)
5756 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5760 &dc
->eboText
.BrushObject
,
5762 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5763 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5764 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5766 ROP2_TO_MIX(R2_COPYPEN
));
5772 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
5773 DPRINT("New TextLeft: %I64d\n", TextLeft
);
5777 // FIXME this should probably be a matrix transform with TextTop as well.
5778 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
5779 if (FLOATOBJ_Equal0(&Scale
))
5780 FLOATOBJ_Set1(&Scale
);
5782 /* do the shift before multiplying to preserve precision */
5783 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
5784 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
5785 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
5790 TextTop
-= Dx
[2 * i
+ 1] << 6;
5793 previous
= glyph_index
;
5795 if (EmuBold
|| EmuItalic
)
5797 FT_Done_Glyph((FT_Glyph
)realglyph
);
5802 if (pdcattr
->lTextAlign
& TA_UPDATECP
) {
5803 pdcattr
->ptlCurrent
.x
= DestRect
.right
- dc
->ptlDCOrig
.x
;
5806 IntUnLockFreeType();
5808 EXLATEOBJ_vCleanup(&exloRGB2Dst
);
5809 EXLATEOBJ_vCleanup(&exloDst2RGB
);
5813 DC_vFinishBlit(dc
, NULL
);
5815 if (TextObj
!= NULL
)
5816 TEXTOBJ_UnlockText(TextObj
);
5823 #define STACK_TEXT_BUFFER_SIZE 100
5831 IN OPTIONAL LPRECT UnsafeRect
,
5832 IN LPWSTR UnsafeString
,
5834 IN OPTIONAL LPINT UnsafeDx
,
5835 IN DWORD dwCodePage
)
5837 BOOL Result
= FALSE
;
5838 NTSTATUS Status
= STATUS_SUCCESS
;
5840 BYTE LocalBuffer
[STACK_TEXT_BUFFER_SIZE
];
5841 PVOID Buffer
= LocalBuffer
;
5842 LPCWSTR SafeString
= NULL
;
5843 LPINT SafeDx
= NULL
;
5844 ULONG BufSize
, StringSize
, DxSize
= 0;
5846 /* Check if String is valid */
5847 if ((Count
> 0xFFFF) || (Count
> 0 && UnsafeString
== NULL
))
5849 EngSetLastError(ERROR_INVALID_PARAMETER
);
5855 /* Calculate buffer size for string and Dx values */
5856 BufSize
= StringSize
= Count
* sizeof(WCHAR
);
5859 /* If ETO_PDY is specified, we have pairs of INTs */
5860 DxSize
= (Count
* sizeof(INT
)) * (fuOptions
& ETO_PDY
? 2 : 1);
5864 /* Check if our local buffer is large enough */
5865 if (BufSize
> STACK_TEXT_BUFFER_SIZE
)
5867 /* It's not, allocate a temp buffer */
5868 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufSize
, GDITAG_TEXT
);
5875 /* Probe and copy user mode data to the buffer */
5878 /* Put the Dx before the String to assure alignment of 4 */
5879 SafeString
= (LPCWSTR
)(((ULONG_PTR
)Buffer
) + DxSize
);
5881 /* Probe and copy the string */
5882 ProbeForRead(UnsafeString
, StringSize
, 1);
5883 memcpy((PVOID
)SafeString
, UnsafeString
, StringSize
);
5885 /* If we have Dx values... */
5888 /* ... probe and copy them */
5890 ProbeForRead(UnsafeDx
, DxSize
, 1);
5891 memcpy(SafeDx
, UnsafeDx
, DxSize
);
5894 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
5896 Status
= _SEH2_GetExceptionCode();
5899 if (!NT_SUCCESS(Status
))
5905 /* If we have a rect, copy it */
5910 ProbeForRead(UnsafeRect
, sizeof(RECT
), 1);
5911 SafeRect
= *UnsafeRect
;
5913 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
5915 Status
= _SEH2_GetExceptionCode();
5918 if (!NT_SUCCESS(Status
))
5924 /* Finally call the internal routine */
5925 Result
= GreExtTextOutW(hDC
,
5936 /* If we allocated a buffer, free it */
5937 if (Buffer
!= LocalBuffer
)
5939 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
5951 NtGdiGetCharABCWidthsW(
5955 IN OPTIONAL PWCHAR UnSafepwch
,
5960 LPABCFLOAT SafeBuffF
= NULL
;
5966 FT_CharMap charmap
, found
= NULL
;
5967 UINT i
, glyph_index
, BufferSize
;
5969 NTSTATUS Status
= STATUS_SUCCESS
;
5970 PMATRIX pmxWorldToDevice
;
5971 PWCHAR Safepwch
= NULL
;
5976 EngSetLastError(ERROR_INVALID_PARAMETER
);
5982 UINT pwchSize
= Count
* sizeof(WCHAR
);
5983 Safepwch
= ExAllocatePoolWithTag(PagedPool
, pwchSize
, GDITAG_TEXT
);
5987 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5993 ProbeForRead(UnSafepwch
, pwchSize
, 1);
5994 RtlCopyMemory(Safepwch
, UnSafepwch
, pwchSize
);
5996 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
5998 Status
= _SEH2_GetExceptionCode();
6003 if (!NT_SUCCESS(Status
))
6006 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6008 EngSetLastError(Status
);
6012 BufferSize
= Count
* sizeof(ABC
); // Same size!
6013 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6014 if (!fl
) SafeBuffF
= (LPABCFLOAT
) SafeBuff
;
6015 if (SafeBuff
== NULL
)
6019 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6021 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6025 dc
= DC_LockDc(hDC
);
6028 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6031 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6033 EngSetLastError(ERROR_INVALID_HANDLE
);
6036 pdcattr
= dc
->pdcattr
;
6037 hFont
= pdcattr
->hlfntNew
;
6038 TextObj
= RealizeFontInit(hFont
);
6040 /* Get the DC's world-to-device transformation matrix */
6041 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6044 if (TextObj
== NULL
)
6046 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6049 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6051 EngSetLastError(ERROR_INVALID_HANDLE
);
6055 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6057 face
= FontGDI
->SharedFace
->Face
;
6058 if (face
->charmap
== NULL
)
6060 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6062 charmap
= face
->charmaps
[i
];
6063 if (charmap
->encoding
!= 0)
6072 DPRINT1("WARNING: Could not find desired charmap!\n");
6073 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6076 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6078 EngSetLastError(ERROR_INVALID_HANDLE
);
6083 FT_Set_Charmap(face
, found
);
6084 IntUnLockFreeType();
6087 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6089 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6090 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6092 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6094 int adv
, lsb
, bbx
, left
, right
;
6098 if (fl
& GCABCW_INDICES
)
6099 glyph_index
= Safepwch
[i
- FirstChar
];
6101 glyph_index
= FT_Get_Char_Index(face
, Safepwch
[i
- FirstChar
]);
6105 if (fl
& GCABCW_INDICES
)
6108 glyph_index
= FT_Get_Char_Index(face
, i
);
6110 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6112 left
= (INT
)face
->glyph
->metrics
.horiBearingX
& -64;
6113 right
= (INT
)((face
->glyph
->metrics
.horiBearingX
+ face
->glyph
->metrics
.width
) + 63) & -64;
6114 adv
= (face
->glyph
->advance
.x
+ 32) >> 6;
6116 // int test = (INT)(face->glyph->metrics.horiAdvance + 63) >> 6;
6117 // DPRINT1("Advance Wine %d and Advance Ros %d\n",test, adv ); /* It's the same! */
6120 bbx
= (right
- left
) >> 6;
6122 DPRINT1("lsb %d and bbx %d\n", lsb, bbx );
6126 SafeBuffF
[i
- FirstChar
].abcfA
= (FLOAT
) lsb
;
6127 SafeBuffF
[i
- FirstChar
].abcfB
= (FLOAT
) bbx
;
6128 SafeBuffF
[i
- FirstChar
].abcfC
= (FLOAT
) (adv
- lsb
- bbx
);
6132 SafeBuff
[i
- FirstChar
].abcA
= lsb
;
6133 SafeBuff
[i
- FirstChar
].abcB
= bbx
;
6134 SafeBuff
[i
- FirstChar
].abcC
= adv
- lsb
- bbx
;
6137 IntUnLockFreeType();
6138 TEXTOBJ_UnlockText(TextObj
);
6139 Status
= MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6141 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6144 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6146 if (! NT_SUCCESS(Status
))
6148 SetLastNtError(Status
);
6152 DPRINT("NtGdiGetCharABCWidths Worked!\n");
6165 IN OPTIONAL PWCHAR UnSafepwc
,
6169 NTSTATUS Status
= STATUS_SUCCESS
;
6171 PFLOAT SafeBuffF
= NULL
;
6177 FT_CharMap charmap
, found
= NULL
;
6178 UINT i
, glyph_index
, BufferSize
;
6180 PMATRIX pmxWorldToDevice
;
6181 PWCHAR Safepwc
= NULL
;
6186 UINT pwcSize
= Count
* sizeof(WCHAR
);
6187 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6191 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6196 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6197 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6199 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6201 Status
= _SEH2_GetExceptionCode();
6206 if (!NT_SUCCESS(Status
))
6208 EngSetLastError(Status
);
6212 BufferSize
= Count
* sizeof(INT
); // Same size!
6213 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6214 if (!fl
) SafeBuffF
= (PFLOAT
) SafeBuff
;
6215 if (SafeBuff
== NULL
)
6218 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6220 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6224 dc
= DC_LockDc(hDC
);
6228 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6230 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6231 EngSetLastError(ERROR_INVALID_HANDLE
);
6234 pdcattr
= dc
->pdcattr
;
6235 hFont
= pdcattr
->hlfntNew
;
6236 TextObj
= RealizeFontInit(hFont
);
6237 /* Get the DC's world-to-device transformation matrix */
6238 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6241 if (TextObj
== NULL
)
6244 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6246 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6247 EngSetLastError(ERROR_INVALID_HANDLE
);
6251 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6253 face
= FontGDI
->SharedFace
->Face
;
6254 if (face
->charmap
== NULL
)
6256 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6258 charmap
= face
->charmaps
[i
];
6259 if (charmap
->encoding
!= 0)
6268 DPRINT1("WARNING: Could not find desired charmap!\n");
6271 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6273 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6274 EngSetLastError(ERROR_INVALID_HANDLE
);
6279 FT_Set_Charmap(face
, found
);
6280 IntUnLockFreeType();
6283 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6285 IntRequestFontSize(dc
, FontGDI
, plf
->lfWidth
, plf
->lfHeight
);
6286 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6288 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6292 if (fl
& GCW_INDICES
)
6293 glyph_index
= Safepwc
[i
- FirstChar
];
6295 glyph_index
= FT_Get_Char_Index(face
, Safepwc
[i
- FirstChar
]);
6299 if (fl
& GCW_INDICES
)
6302 glyph_index
= FT_Get_Char_Index(face
, i
);
6304 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6306 SafeBuffF
[i
- FirstChar
] = (FLOAT
) ((face
->glyph
->advance
.x
+ 32) >> 6);
6308 SafeBuff
[i
- FirstChar
] = (face
->glyph
->advance
.x
+ 32) >> 6;
6310 IntUnLockFreeType();
6311 TEXTOBJ_UnlockText(TextObj
);
6312 MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6315 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6317 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6325 // TODO: Move this code into NtGdiGetGlyphIndicesWInternal and wrap
6326 // NtGdiGetGlyphIndicesW around NtGdiGetGlyphIndicesWInternal instead.
6327 // NOTE: See also GreGetGlyphIndicesW.
6332 NtGdiGetGlyphIndicesW(
6334 _In_reads_opt_(cwc
) LPCWSTR pwc
,
6336 _Out_writes_opt_(cwc
) LPWORD pgi
,
6344 NTSTATUS Status
= STATUS_SUCCESS
;
6345 OUTLINETEXTMETRICW
*potm
;
6347 WCHAR DefChar
= 0xffff;
6348 PWSTR Buffer
= NULL
;
6349 ULONG Size
, pwcSize
;
6350 PWSTR Safepwc
= NULL
;
6351 LPCWSTR UnSafepwc
= pwc
;
6352 LPWORD UnSafepgi
= pgi
;
6354 /* Check for integer overflow */
6355 if (cwc
& 0x80000000) // (INT_MAX + 1) == INT_MIN
6358 if (!UnSafepwc
&& !UnSafepgi
)
6361 if (!UnSafepwc
|| !UnSafepgi
)
6363 DPRINT1("UnSafepwc == %p, UnSafepgi = %p\n", UnSafepwc
, UnSafepgi
);
6367 // TODO: Special undocumented case!
6368 if (!pwc
&& !pgi
&& (cwc
== 0))
6370 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (!pwc && !pgi && (cwc == 0)) is UNIMPLEMENTED!\n");
6374 // FIXME: This is a hack!! (triggered by e.g. Word 2010). See CORE-12825
6377 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (cwc == 0) is UNIMPLEMENTED!\n");
6381 dc
= DC_LockDc(hdc
);
6386 pdcattr
= dc
->pdcattr
;
6387 hFont
= pdcattr
->hlfntNew
;
6388 TextObj
= RealizeFontInit(hFont
);
6395 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6396 TEXTOBJ_UnlockText(TextObj
);
6398 Buffer
= ExAllocatePoolWithTag(PagedPool
, cwc
*sizeof(WORD
), GDITAG_TEXT
);
6404 if (iMode
& GGI_MARK_NONEXISTING_GLYPHS
)
6410 FT_Face Face
= FontGDI
->SharedFace
->Face
;
6411 if (FT_IS_SFNT(Face
))
6413 TT_OS2
*pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
6414 DefChar
= (pOS2
->usDefaultChar
? FT_Get_Char_Index(Face
, pOS2
->usDefaultChar
) : 0);
6418 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
6419 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
6425 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
6427 DefChar
= potm
->otmTextMetrics
.tmDefaultChar
;
6428 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
6432 pwcSize
= cwc
* sizeof(WCHAR
);
6433 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6437 Status
= STATUS_NO_MEMORY
;
6443 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6444 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6446 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6448 Status
= _SEH2_GetExceptionCode();
6452 if (!NT_SUCCESS(Status
)) goto ErrorRet
;
6456 for (i
= 0; i
< cwc
; i
++)
6458 Buffer
[i
] = FT_Get_Char_Index(FontGDI
->SharedFace
->Face
, Safepwc
[i
]);
6461 Buffer
[i
] = DefChar
;
6465 IntUnLockFreeType();
6469 ProbeForWrite(UnSafepgi
, cwc
* sizeof(WORD
), 1);
6470 RtlCopyMemory(UnSafepgi
, Buffer
, cwc
* sizeof(WORD
));
6472 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6474 Status
= _SEH2_GetExceptionCode();
6479 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
6480 if (Safepwc
!= NULL
)
6482 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6484 if (NT_SUCCESS(Status
)) return cwc
;