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-2017 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
51 static const WORD gusEnglishUS
= MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
);
53 /* special font names */
54 static const UNICODE_STRING MarlettW
= RTL_CONSTANT_STRING(L
"Marlett");
55 static const UNICODE_STRING SystemW
= RTL_CONSTANT_STRING(L
"System");
56 static const UNICODE_STRING FixedSysW
= RTL_CONSTANT_STRING(L
"FixedSys");
59 static UNICODE_STRING FontRegPath
=
60 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
63 /* The FreeType library is not thread safe, so we have
64 to serialize access to it */
65 static PFAST_MUTEX FreeTypeLock
;
67 static LIST_ENTRY FontListHead
;
68 static PFAST_MUTEX FontListLock
;
69 static BOOL RenderingEnabled
= TRUE
;
71 #define IntLockGlobalFonts \
72 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FontListLock)
74 #define IntUnLockGlobalFonts \
75 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FontListLock)
77 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
78 ASSERT(FontListLock->Owner == KeGetCurrentThread())
80 #define IntLockFreeType \
81 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FreeTypeLock)
83 #define IntUnLockFreeType \
84 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FreeTypeLock)
86 #define ASSERT_FREETYPE_LOCK_HELD() \
87 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
89 #define ASSERT_FREETYPE_LOCK_NOT_HELD() \
90 ASSERT(FreeTypeLock->Owner != KeGetCurrentThread())
92 #define MAX_FONT_CACHE 256
94 static LIST_ENTRY FontCacheListHead
;
95 static UINT FontCacheNumEntries
;
97 static PWCHAR ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
107 L
"Vietnamese", /* 08 */
108 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
116 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
121 * For TranslateCharsetInfo
124 #define MAXTCIINDEX 32
125 static const CHARSETINFO FontTci
[MAXTCIINDEX
] =
128 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
129 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
130 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
131 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
132 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
133 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
134 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
135 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
136 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
137 /* reserved by ANSI */
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}} },
142 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
143 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
144 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
146 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
147 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
148 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
149 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
150 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
151 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
152 /* Reserved for alternate ANSI and OEM */
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 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
159 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
160 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
161 /* Reserved for system */
162 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
163 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
167 static RTL_STATIC_LIST_HEAD(FontSubstListHead
);
170 SharedMem_AddRef(PSHARED_MEM Ptr
)
172 ASSERT_FREETYPE_LOCK_HELD();
178 SharedFaceCache_Init(PSHARED_FACE_CACHE Cache
)
180 Cache
->OutlineRequiredSize
= 0;
181 RtlInitUnicodeString(&Cache
->FontFamily
, NULL
);
182 RtlInitUnicodeString(&Cache
->FullName
, NULL
);
186 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
189 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
194 Ptr
->Memory
= Memory
;
195 SharedFaceCache_Init(&Ptr
->EnglishUS
);
196 SharedFaceCache_Init(&Ptr
->UserLanguage
);
198 SharedMem_AddRef(Memory
);
199 DPRINT("Creating SharedFace for %s\n", Face
->family_name
);
205 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
208 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
211 Ptr
->Buffer
= Buffer
;
212 Ptr
->BufferSize
= BufferSize
;
214 Ptr
->IsMapping
= IsMapping
;
215 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
221 SharedFace_AddRef(PSHARED_FACE Ptr
)
223 ASSERT_FREETYPE_LOCK_HELD();
229 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
231 ASSERT_FREETYPE_LOCK_HELD();
233 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
234 RemoveEntryList(&Entry
->ListEntry
);
235 ExFreePoolWithTag(Entry
, TAG_FONT
);
236 FontCacheNumEntries
--;
237 ASSERT(FontCacheNumEntries
<= MAX_FONT_CACHE
);
241 RemoveCacheEntries(FT_Face Face
)
243 PLIST_ENTRY CurrentEntry
;
244 PFONT_CACHE_ENTRY FontEntry
;
246 ASSERT_FREETYPE_LOCK_HELD();
248 CurrentEntry
= FontCacheListHead
.Flink
;
249 while (CurrentEntry
!= &FontCacheListHead
)
251 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
252 CurrentEntry
= CurrentEntry
->Flink
;
254 if (FontEntry
->Face
== Face
)
256 RemoveCachedEntry(FontEntry
);
261 static void SharedMem_Release(PSHARED_MEM Ptr
)
263 ASSERT_FREETYPE_LOCK_HELD();
264 ASSERT(Ptr
->RefCount
> 0);
266 if (Ptr
->RefCount
<= 0)
270 if (Ptr
->RefCount
== 0)
272 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
274 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
276 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
277 ExFreePoolWithTag(Ptr
, TAG_FONT
);
282 SharedFaceCache_Release(PSHARED_FACE_CACHE Cache
)
284 RtlFreeUnicodeString(&Cache
->FontFamily
);
285 RtlFreeUnicodeString(&Cache
->FullName
);
289 SharedFace_Release(PSHARED_FACE Ptr
)
292 ASSERT(Ptr
->RefCount
> 0);
294 if (Ptr
->RefCount
<= 0)
298 if (Ptr
->RefCount
== 0)
300 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
);
301 RemoveCacheEntries(Ptr
->Face
);
302 FT_Done_Face(Ptr
->Face
);
303 SharedMem_Release(Ptr
->Memory
);
304 SharedFaceCache_Release(&Ptr
->EnglishUS
);
305 SharedFaceCache_Release(&Ptr
->UserLanguage
);
306 ExFreePoolWithTag(Ptr
, TAG_FONT
);
313 * IntLoadFontSubstList --- loads the list of font substitutes
316 IntLoadFontSubstList(PLIST_ENTRY pHead
)
320 OBJECT_ATTRIBUTES ObjectAttributes
;
321 KEY_FULL_INFORMATION KeyFullInfo
;
323 UNICODE_STRING FromW
, ToW
;
324 BYTE InfoBuffer
[128];
325 PKEY_VALUE_FULL_INFORMATION pInfo
;
326 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
328 PFONTSUBST_ENTRY pEntry
;
330 /* the FontSubstitutes registry key */
331 static UNICODE_STRING FontSubstKey
=
332 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
333 L
"Microsoft\\Windows NT\\CurrentVersion\\"
336 /* open registry key */
337 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
338 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
340 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
341 if (!NT_SUCCESS(Status
))
343 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
344 return FALSE
; /* failure */
347 /* query count of values */
348 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
349 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
350 if (!NT_SUCCESS(Status
))
352 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
354 return FALSE
; /* failure */
358 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
361 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
362 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
363 if (!NT_SUCCESS(Status
))
365 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
369 /* create FromW string */
370 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
371 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
372 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
373 Status
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
374 if (!NT_SUCCESS(Status
))
376 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
381 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
382 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
383 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
384 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
386 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
387 RtlFreeUnicodeString(&FromW
);
391 /* create ToW string */
392 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
393 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
394 pch
[Length
] = UNICODE_NULL
; /* truncate */
395 Status
= RtlCreateUnicodeString(&ToW
, pch
);
396 if (!NT_SUCCESS(Status
))
398 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
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(&FontListHead
);
459 InitializeListHead(&FontCacheListHead
);
460 FontCacheNumEntries
= 0;
461 /* Fast Mutexes must be allocated from non paged pool */
462 FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
463 if (FontListLock
== NULL
)
468 ExInitializeFastMutex(FontListLock
);
469 FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
470 if (FreeTypeLock
== NULL
)
474 ExInitializeFastMutex(FreeTypeLock
);
476 ulError
= FT_Init_FreeType(&library
);
479 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
483 IntLoadSystemFonts();
484 IntLoadFontSubstList(&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(&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 (FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
960 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
961 CharSetIndex
== CharSetCount
)
963 FontGDI
->CharSet
= 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
, &MarlettW
, TRUE
))
988 FontGDI
->CharSet
= SYMBOL_CHARSET
;
992 DPRINT("Font loaded: %s (%s)\n", Face
->family_name
, Face
->style_name
);
993 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
994 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
996 /* Add this font resource to the font table */
997 Entry
->Font
= FontGDI
;
998 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
1000 if (Characteristics
& FR_PRIVATE
)
1003 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1004 IntLockProcessPrivateFonts(Win32Process
);
1005 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1006 IntUnLockProcessPrivateFonts(Win32Process
);
1012 InsertTailList(&FontListHead
, &Entry
->ListEntry
);
1013 IntUnLockGlobalFonts
;
1016 if (FontIndex
== -1)
1018 if (FT_IS_SFNT(Face
))
1020 TT_Face TrueType
= (TT_Face
)Face
;
1021 if (TrueType
->ttc_header
.count
> 1)
1024 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1026 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1033 if (CharSetIndex
== -1)
1037 if (pLoadFont
->RegValueName
.Length
== 0)
1039 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1043 UNICODE_STRING NewString
;
1044 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1045 NewString
.Length
= 0;
1046 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1047 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1048 NewString
.MaximumLength
,
1050 NewString
.Buffer
[0] = UNICODE_NULL
;
1052 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1053 RtlAppendUnicodeToString(&NewString
, L
" & ");
1054 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1056 RtlFreeUnicodeString(pValueName
);
1057 *pValueName
= NewString
;
1060 for (i
= 1; i
< CharSetCount
; ++i
)
1062 /* Do not count charsets towards 'faces' loaded */
1063 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1067 return FaceCount
; /* number of loaded faces */
1071 * IntGdiAddFontResource
1073 * Adds the font resource from the specified file to the system.
1077 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1081 PVOID Buffer
= NULL
;
1082 IO_STATUS_BLOCK Iosb
;
1083 PVOID SectionObject
;
1085 LARGE_INTEGER SectionSize
;
1086 OBJECT_ATTRIBUTES ObjectAttributes
;
1087 GDI_LOAD_FONT LoadFont
;
1090 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1092 /* Open the font file */
1093 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1094 Status
= ZwOpenFile(
1096 FILE_GENERIC_READ
| SYNCHRONIZE
,
1100 FILE_SYNCHRONOUS_IO_NONALERT
);
1101 if (!NT_SUCCESS(Status
))
1103 DPRINT("Could not load font file: %wZ\n", FileName
);
1107 SectionSize
.QuadPart
= 0LL;
1108 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1109 NULL
, &SectionSize
, PAGE_READONLY
,
1110 SEC_COMMIT
, FileHandle
, NULL
);
1111 if (!NT_SUCCESS(Status
))
1113 DPRINT("Could not map file: %wZ\n", FileName
);
1114 ZwClose(FileHandle
);
1117 ZwClose(FileHandle
);
1119 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1120 if (!NT_SUCCESS(Status
))
1122 DPRINT("Could not map file: %wZ\n", FileName
);
1123 ObDereferenceObject(SectionObject
);
1127 LoadFont
.pFileName
= FileName
;
1128 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1129 LoadFont
.Characteristics
= Characteristics
;
1130 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1131 LoadFont
.IsTrueType
= FALSE
;
1132 LoadFont
.PrivateEntry
= NULL
;
1133 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1135 ObDereferenceObject(SectionObject
);
1137 /* Release our copy */
1139 SharedMem_Release(LoadFont
.Memory
);
1144 if (LoadFont
.IsTrueType
)
1146 /* append " (TrueType)" */
1147 UNICODE_STRING NewString
;
1150 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1151 NewString
.Length
= 0;
1152 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1153 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1154 NewString
.MaximumLength
,
1156 NewString
.Buffer
[0] = UNICODE_NULL
;
1158 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1159 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1160 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1161 LoadFont
.RegValueName
= NewString
;
1165 InitializeObjectAttributes(&ObjectAttributes
, &FontRegPath
,
1166 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1168 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1169 if (NT_SUCCESS(Status
))
1172 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1176 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1177 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1178 pFileName
, DataSize
);
1183 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1189 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1191 GDI_LOAD_FONT LoadFont
;
1192 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1196 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1203 memcpy(BufferCopy
, Buffer
, dwSize
);
1205 LoadFont
.pFileName
= NULL
;
1206 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1207 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1208 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1209 LoadFont
.IsTrueType
= FALSE
;
1210 LoadFont
.PrivateEntry
= NULL
;
1211 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1213 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1215 /* Release our copy */
1217 SharedMem_Release(LoadFont
.Memory
);
1222 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1223 if (EntryCollection
)
1225 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1226 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1227 IntLockProcessPrivateFonts(Win32Process
);
1228 EntryCollection
->Handle
= ++Win32Process
->PrivateMemFontHandleCount
;
1229 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1230 IntUnLockProcessPrivateFonts(Win32Process
);
1231 Ret
= (HANDLE
)EntryCollection
->Handle
;
1234 *pNumAdded
= FaceCount
;
1239 // FIXME: Add RemoveFontResource
1241 static VOID FASTCALL
1242 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1244 PFONTGDI FontGDI
= FontEntry
->Font
;
1245 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1247 if (FontGDI
->Filename
)
1248 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1250 EngFreeMem(FontGDI
);
1251 SharedFace_Release(SharedFace
);
1252 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1256 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1259 PFONT_ENTRY_MEM FontEntry
;
1261 while (!IsListEmpty(&Head
->ListEntry
))
1263 Entry
= RemoveHeadList(&Head
->ListEntry
);
1264 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1266 CleanupFontEntry(FontEntry
->Entry
);
1267 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1270 CleanupFontEntry(Head
->Entry
);
1271 ExFreePoolWithTag(Head
, TAG_FONT
);
1274 static VOID FASTCALL
1275 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1277 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1278 PLIST_ENTRY ListEntry
;
1279 RemoveEntryList(&Collection
->ListEntry
);
1282 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1283 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1285 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1286 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1288 } while (FontMemEntry
!= Collection
->Entry
);
1292 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1295 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1296 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1297 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1299 IntLockProcessPrivateFonts(Win32Process
);
1300 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1301 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1303 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1305 if (CurrentEntry
->Handle
== (UINT
)hMMFont
)
1307 EntryCollection
= CurrentEntry
;
1308 UnlinkFontMemCollection(CurrentEntry
);
1312 Entry
= Entry
->Flink
;
1314 IntUnLockProcessPrivateFonts(Win32Process
);
1316 if (EntryCollection
)
1318 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1319 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1327 IntGdiCleanupPrivateFontsForProcess(VOID
)
1329 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1331 PFONT_ENTRY_COLL_MEM EntryCollection
;
1333 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1336 EntryCollection
= NULL
;
1338 IntLockProcessPrivateFonts(Win32Process
);
1339 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1341 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1342 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1343 UnlinkFontMemCollection(EntryCollection
);
1345 IntUnLockProcessPrivateFonts(Win32Process
);
1347 if (EntryCollection
)
1349 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1350 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1354 /* No Mem fonts anymore, see if we have any other private fonts left */
1356 IntLockProcessPrivateFonts(Win32Process
);
1357 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1359 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1361 IntUnLockProcessPrivateFonts(Win32Process
);
1365 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1373 IntIsFontRenderingEnabled(VOID
)
1375 BOOL Ret
= RenderingEnabled
;
1378 hDC
= IntGetScreenDC();
1380 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && RenderingEnabled
;
1386 IntEnableFontRendering(BOOL Enable
)
1388 RenderingEnabled
= Enable
;
1391 FT_Render_Mode FASTCALL
1392 IntGetFontRenderMode(LOGFONTW
*logfont
)
1394 switch (logfont
->lfQuality
)
1396 case ANTIALIASED_QUALITY
:
1397 case NONANTIALIASED_QUALITY
:
1398 return FT_RENDER_MODE_MONO
;
1400 return FT_RENDER_MODE_LIGHT
;
1401 /* case CLEARTYPE_QUALITY:
1402 return FT_RENDER_MODE_LCD; */
1404 return FT_RENDER_MODE_NORMAL
;
1409 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1414 plfont
= LFONT_AllocFontWithHandle();
1417 return STATUS_NO_MEMORY
;
1420 ExInitializePushLock(&plfont
->lock
);
1421 *NewFont
= plfont
->BaseObject
.hHmgr
;
1422 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1423 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1424 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1426 /* This should really depend on whether GM_ADVANCED is set */
1427 plf
->lfOrientation
= plf
->lfEscapement
;
1429 LFONT_UnlockFont(plfont
);
1431 return STATUS_SUCCESS
;
1434 /*************************************************************************
1435 * TranslateCharsetInfo
1437 * Fills a CHARSETINFO structure for a character set, code page, or
1438 * font. This allows making the correspondance between different labelings
1439 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1440 * of the same encoding.
1442 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1443 * only one codepage should be set in *Src.
1446 * TRUE on success, FALSE on failure.
1449 static BOOLEAN APIENTRY
1450 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1451 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1452 if flags == TCI_SRCCHARSET: a character set value
1453 if flags == TCI_SRCCODEPAGE: a code page value */
1454 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1455 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1461 case TCI_SRCFONTSIG
:
1462 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1467 case TCI_SRCCODEPAGE
:
1468 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciACP
)
1473 case TCI_SRCCHARSET
:
1474 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciCharset
)
1483 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== FontTci
[Index
].ciCharset
)
1488 RtlCopyMemory(Cs
, &FontTci
[Index
], sizeof(CHARSETINFO
));
1494 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1498 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1500 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1507 static void FASTCALL
1508 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1509 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1510 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1512 FT_Fixed XScale
, YScale
;
1513 int Ascent
, Descent
;
1514 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1516 XScale
= Face
->size
->metrics
.x_scale
;
1517 YScale
= Face
->size
->metrics
.y_scale
;
1521 TM
->tmHeight
= pFNT
->pixel_height
;
1522 TM
->tmAscent
= pFNT
->ascent
;
1523 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1524 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1525 TM
->tmExternalLeading
= pFNT
->external_leading
;
1526 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1527 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1529 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1530 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1531 TM
->tmFirstChar
= pFNT
->first_char
;
1532 TM
->tmLastChar
= pFNT
->last_char
;
1533 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1534 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1535 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1538 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1539 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1540 TM
->tmUnderlined
= pFNT
->underline
;
1541 TM
->tmStruckOut
= pFNT
->strike_out
;
1542 TM
->tmCharSet
= pFNT
->charset
;
1546 TM
->tmWeight
= FontGDI
->RequestWeight
;
1547 TM
->tmItalic
= FontGDI
->RequestItalic
;
1548 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1549 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1550 TM
->tmCharSet
= FontGDI
->CharSet
;
1555 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1557 Ascent
= pHori
->Ascender
;
1558 Descent
= -pHori
->Descender
;
1562 Ascent
= pOS2
->usWinAscent
;
1563 Descent
= pOS2
->usWinDescent
;
1566 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1567 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1568 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1569 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1570 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1571 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1573 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1575 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1578 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1580 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1581 - ((Ascent
+ Descent
)
1582 - (pHori
->Ascender
- pHori
->Descender
)),
1583 YScale
) + 32) >> 6);
1585 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1586 if (TM
->tmAveCharWidth
== 0)
1588 TM
->tmAveCharWidth
= 1;
1591 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1592 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1596 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1600 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1601 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1603 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1607 TM
->tmWeight
= FontGDI
->RequestWeight
;
1612 TM
->tmDigitizedAspectX
= 96;
1613 TM
->tmDigitizedAspectY
= 96;
1614 if (face_has_symbol_charmap(Face
) ||
1615 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1617 USHORT cpOEM
, cpAnsi
;
1619 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1620 TM
->tmFirstChar
= 0;
1623 case 1257: /* Baltic */
1624 TM
->tmLastChar
= 0xf8fd;
1627 TM
->tmLastChar
= 0xf0ff;
1629 TM
->tmBreakChar
= 0x20;
1630 TM
->tmDefaultChar
= 0x1f;
1634 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1635 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1637 if(pOS2
->usFirstCharIndex
<= 1)
1638 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1639 else if (pOS2
->usFirstCharIndex
> 0xff)
1640 TM
->tmBreakChar
= 0x20;
1642 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1643 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1648 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1649 TM
->tmUnderlined
= FALSE
;
1650 TM
->tmStruckOut
= FALSE
;
1654 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1656 TM
->tmItalic
= 0xFF;
1662 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1663 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1666 if (!FT_IS_FIXED_WIDTH(Face
))
1668 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1670 case PAN_PROP_MONOSPACED
:
1671 TM
->tmPitchAndFamily
= 0;
1674 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1680 TM
->tmPitchAndFamily
= 0;
1683 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1685 case PAN_FAMILY_SCRIPT
:
1686 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1688 case PAN_FAMILY_DECORATIVE
:
1689 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1694 case PAN_FAMILY_TEXT_DISPLAY
:
1695 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1696 /* Which is clearly not what the panose spec says. */
1697 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1699 TM
->tmPitchAndFamily
= FF_MODERN
;
1703 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1708 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1711 case PAN_SERIF_COVE
:
1712 case PAN_SERIF_OBTUSE_COVE
:
1713 case PAN_SERIF_SQUARE_COVE
:
1714 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1715 case PAN_SERIF_SQUARE
:
1716 case PAN_SERIF_THIN
:
1717 case PAN_SERIF_BONE
:
1718 case PAN_SERIF_EXAGGERATED
:
1719 case PAN_SERIF_TRIANGLE
:
1720 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1723 case PAN_SERIF_NORMAL_SANS
:
1724 case PAN_SERIF_OBTUSE_SANS
:
1725 case PAN_SERIF_PERP_SANS
:
1726 case PAN_SERIF_FLARED
:
1727 case PAN_SERIF_ROUNDED
:
1728 TM
->tmPitchAndFamily
|= FF_SWISS
;
1734 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1737 if (FT_IS_SCALABLE(Face
))
1739 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1741 if (FT_IS_SFNT(Face
))
1743 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1746 TM
->tmCharSet
= FontGDI
->CharSet
;
1749 static void FASTCALL
1750 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1751 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1752 FT_WinFNT_HeaderRec
*pFNT
)
1754 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1758 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1759 FT_UShort NameID
, FT_UShort LangID
);
1761 /*************************************************************
1762 * IntGetOutlineTextMetrics
1766 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1768 OUTLINETEXTMETRICW
*Otm
)
1771 TT_HoriHeader
*pHori
;
1772 TT_Postscript
*pPost
;
1773 FT_Fixed XScale
, YScale
;
1774 FT_WinFNT_HeaderRec Win
;
1777 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1778 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1779 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1780 FT_Face Face
= SharedFace
->Face
;
1782 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1784 return Cache
->OutlineRequiredSize
;
1788 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1789 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1792 RtlInitUnicodeString(&FaceNameW
, NULL
);
1793 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1796 RtlInitUnicodeString(&StyleNameW
, NULL
);
1797 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1799 /* unique name (full name) */
1800 RtlInitUnicodeString(&FullNameW
, NULL
);
1801 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1803 if (!Cache
->OutlineRequiredSize
)
1806 Needed
= sizeof(OUTLINETEXTMETRICW
);
1807 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1808 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1809 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1810 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1812 Cache
->OutlineRequiredSize
= Needed
;
1815 if (Size
< Cache
->OutlineRequiredSize
)
1817 RtlFreeUnicodeString(&FamilyNameW
);
1818 RtlFreeUnicodeString(&FaceNameW
);
1819 RtlFreeUnicodeString(&StyleNameW
);
1820 RtlFreeUnicodeString(&FullNameW
);
1821 return Cache
->OutlineRequiredSize
;
1824 XScale
= Face
->size
->metrics
.x_scale
;
1825 YScale
= Face
->size
->metrics
.y_scale
;
1828 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1832 DPRINT1("Can't find OS/2 table - not TT font?\n");
1833 RtlFreeUnicodeString(&FamilyNameW
);
1834 RtlFreeUnicodeString(&FaceNameW
);
1835 RtlFreeUnicodeString(&StyleNameW
);
1836 RtlFreeUnicodeString(&FullNameW
);
1840 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1844 DPRINT1("Can't find HHEA table - not TT font?\n");
1845 RtlFreeUnicodeString(&FamilyNameW
);
1846 RtlFreeUnicodeString(&FaceNameW
);
1847 RtlFreeUnicodeString(&StyleNameW
);
1848 RtlFreeUnicodeString(&FullNameW
);
1852 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1854 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1856 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1858 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1861 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1862 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1863 Otm
->otmfsType
= pOS2
->fsType
;
1864 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1865 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1866 Otm
->otmItalicAngle
= 0; /* POST table */
1867 Otm
->otmEMSquare
= Face
->units_per_EM
;
1868 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1869 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1870 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1871 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1872 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1873 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1874 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1875 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1876 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1877 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1878 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1879 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1880 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1881 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1882 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1883 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1884 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1885 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1886 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1887 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1888 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1889 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1890 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1893 Otm
->otmsUnderscoreSize
= 0;
1894 Otm
->otmsUnderscorePosition
= 0;
1898 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1899 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1904 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1907 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1908 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1909 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1912 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1913 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1914 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1917 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1918 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1919 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1921 /* unique name (full name) */
1922 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1923 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1924 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1926 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1928 RtlFreeUnicodeString(&FamilyNameW
);
1929 RtlFreeUnicodeString(&FaceNameW
);
1930 RtlFreeUnicodeString(&StyleNameW
);
1931 RtlFreeUnicodeString(&FullNameW
);
1933 return Cache
->OutlineRequiredSize
;
1936 static PFONTGDI FASTCALL
1937 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1940 PFONT_ENTRY CurrentEntry
;
1941 ANSI_STRING EntryFaceNameA
;
1942 UNICODE_STRING EntryFaceNameW
;
1946 Entry
= Head
->Flink
;
1947 while (Entry
!= Head
)
1949 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1951 FontGDI
= CurrentEntry
->Font
;
1954 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1955 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1956 if (!NT_SUCCESS(status
))
1961 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1963 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1964 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1967 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
1969 RtlFreeUnicodeString(&EntryFaceNameW
);
1973 RtlFreeUnicodeString(&EntryFaceNameW
);
1974 Entry
= Entry
->Flink
;
1980 static PFONTGDI FASTCALL
1981 FindFaceNameInLists(PUNICODE_STRING FaceName
)
1983 PPROCESSINFO Win32Process
;
1986 /* Search the process local list.
1987 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
1988 Win32Process
= PsGetCurrentProcessWin32Process();
1989 IntLockProcessPrivateFonts(Win32Process
);
1990 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
1991 IntUnLockProcessPrivateFonts(Win32Process
);
1997 /* Search the global list */
1999 Font
= FindFaceNameInList(FaceName
, &FontListHead
);
2000 IntUnLockGlobalFonts
;
2005 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2007 CharSetFromLangID(LANGID LangID
)
2009 /* FIXME: Add more and fix if wrong */
2010 switch (PRIMARYLANGID(LangID
))
2013 switch (SUBLANGID(LangID
))
2015 case SUBLANG_CHINESE_TRADITIONAL
:
2016 return CHINESEBIG5_CHARSET
;
2017 case SUBLANG_CHINESE_SIMPLIFIED
:
2021 return GB2312_CHARSET
;
2023 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2024 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2025 return EASTEUROPE_CHARSET
;
2027 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2028 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2029 return RUSSIAN_CHARSET
;
2031 case LANG_ARABIC
: return ARABIC_CHARSET
;
2032 case LANG_GREEK
: return GREEK_CHARSET
;
2033 case LANG_HEBREW
: return HEBREW_CHARSET
;
2034 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2035 case LANG_KOREAN
: return JOHAB_CHARSET
;
2036 case LANG_TURKISH
: return TURKISH_CHARSET
;
2037 case LANG_THAI
: return THAI_CHARSET
;
2038 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2039 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2041 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2042 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2043 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2044 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2045 case LANG_SWEDISH
: default:
2046 return ANSI_CHARSET
;
2051 SwapEndian(LPVOID pvData
, DWORD Size
)
2053 BYTE b
, *pb
= pvData
;
2065 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2067 NTSTATUS Status
= STATUS_NO_MEMORY
;
2070 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2073 Tmp
.MaximumLength
= Source
->MaximumLength
;
2075 RtlCopyUnicodeString(&Tmp
, Source
);
2077 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2078 Destination
->Length
= Tmp
.Length
;
2079 Destination
->Buffer
= Tmp
.Buffer
;
2081 Status
= STATUS_SUCCESS
;
2088 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2089 FT_UShort NameID
, FT_UShort LangID
)
2092 INT i
, Count
, BestIndex
, Score
, BestScore
;
2093 WCHAR Buf
[LF_FULLFACESIZE
];
2095 NTSTATUS Status
= STATUS_NOT_FOUND
;
2096 ANSI_STRING AnsiName
;
2097 PSHARED_FACE_CACHE Cache
;
2098 FT_Face Face
= SharedFace
->Face
;
2100 RtlFreeUnicodeString(pNameW
);
2103 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2105 Cache
= &SharedFace
->EnglishUS
;
2109 Cache
= &SharedFace
->UserLanguage
;
2112 /* use cache if available */
2113 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2115 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2117 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2119 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2125 Count
= FT_Get_Sfnt_Name_Count(Face
);
2126 for (i
= 0; i
< Count
; ++i
)
2128 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2131 continue; /* failure */
2134 if (Name
.name_id
!= NameID
)
2136 continue; /* mismatched */
2139 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2140 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2141 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2143 continue; /* not Microsoft Unicode name */
2146 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2147 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2149 continue; /* invalid string */
2152 if (sizeof(Buf
) < Name
.string_len
+ sizeof(UNICODE_NULL
))
2154 continue; /* name too long */
2157 if (Name
.language_id
== LangID
)
2161 break; /* best match */
2163 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2167 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2176 if (Score
> BestScore
)
2185 /* store the best name */
2186 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2189 /* NOTE: Name.string is not null-terminated */
2190 RtlCopyMemory(Buf
, Name
.string
, Name
.string_len
);
2191 Buf
[Name
.string_len
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2193 /* Convert UTF-16 big endian to little endian */
2194 SwapEndian(Buf
, Name
.string_len
);
2196 Status
= RtlCreateUnicodeString(pNameW
, Buf
);
2200 if (!NT_SUCCESS(Status
))
2203 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2205 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2206 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2210 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2211 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2215 if (NT_SUCCESS(Status
))
2218 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2220 ASSERT_FREETYPE_LOCK_NOT_HELD();
2222 if (!Cache
->FontFamily
.Buffer
)
2223 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2226 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2228 ASSERT_FREETYPE_LOCK_NOT_HELD();
2230 if (!Cache
->FullName
.Buffer
)
2231 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2239 static void FASTCALL
2240 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2241 LPCWSTR FullName
, PFONTGDI FontGDI
)
2244 UNICODE_STRING StyleW
;
2247 CHARSETINFO CharSetInfo
;
2249 OUTLINETEXTMETRICW
*Otm
;
2252 NEWTEXTMETRICW
*Ntm
;
2255 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2256 FT_Face Face
= SharedFace
->Face
;
2257 UNICODE_STRING NameW
;
2259 RtlInitUnicodeString(&NameW
, NULL
);
2260 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2261 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2262 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2267 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2270 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2274 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2275 TM
= &Otm
->otmTextMetrics
;
2277 Lf
->lfHeight
= TM
->tmHeight
;
2278 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2279 Lf
->lfWeight
= TM
->tmWeight
;
2280 Lf
->lfItalic
= TM
->tmItalic
;
2281 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2282 Lf
->lfCharSet
= TM
->tmCharSet
;
2283 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2284 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2285 Lf
->lfQuality
= PROOF_QUALITY
;
2287 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2288 Ntm
->tmHeight
= TM
->tmHeight
;
2289 Ntm
->tmAscent
= TM
->tmAscent
;
2290 Ntm
->tmDescent
= TM
->tmDescent
;
2291 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2292 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2293 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2294 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2295 Ntm
->tmWeight
= TM
->tmWeight
;
2296 Ntm
->tmOverhang
= TM
->tmOverhang
;
2297 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2298 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2299 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2300 Ntm
->tmLastChar
= TM
->tmLastChar
;
2301 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2302 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2303 Ntm
->tmItalic
= TM
->tmItalic
;
2304 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2305 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2306 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2307 Ntm
->tmCharSet
= TM
->tmCharSet
;
2308 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2310 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2312 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2314 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2315 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2316 Ntm
->ntmAvgWidth
= 0;
2318 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2319 ? TRUETYPE_FONTTYPE
: 0);
2321 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2322 Info
->FontType
|= RASTER_FONTTYPE
;
2327 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2329 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2333 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2335 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2336 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2339 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2341 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2342 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2343 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2344 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2345 if (!NT_SUCCESS(status
))
2349 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2352 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2360 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2361 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2362 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2363 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2364 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2365 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2367 if (0 == pOS2
->version
)
2371 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2372 fs
.fsCsb
[0] |= FS_LATIN1
;
2374 fs
.fsCsb
[0] |= FS_SYMBOL
;
2378 if (fs
.fsCsb
[0] == 0)
2380 /* Let's see if we can find any interesting cmaps */
2381 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2383 switch (Face
->charmaps
[i
]->encoding
)
2385 case FT_ENCODING_UNICODE
:
2386 case FT_ENCODING_APPLE_ROMAN
:
2387 fs
.fsCsb
[0] |= FS_LATIN1
;
2389 case FT_ENCODING_MS_SYMBOL
:
2390 fs
.fsCsb
[0] |= FS_SYMBOL
;
2398 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2401 if (fs
.fsCsb
[0] & fs0
)
2403 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2405 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2407 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2410 wcscpy(Info
->EnumLogFontEx
.elfScript
, ElfScripts
[i
]);
2413 DPRINT1("Unknown elfscript for bit %u\n", i
);
2418 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2422 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2425 UNICODE_STRING InfoFaceName
;
2427 for (i
= 0; i
< InfoEntries
; i
++)
2429 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2430 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2439 static BOOLEAN FASTCALL
2440 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2441 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2443 UNICODE_STRING LogFontFaceName
;
2445 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2446 if (0 != LogFontFaceName
.Length
&&
2447 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2452 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2455 static BOOLEAN FASTCALL
2456 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2457 PFONTFAMILYINFO Info
,
2463 PFONT_ENTRY CurrentEntry
;
2464 ANSI_STRING EntryFaceNameA
;
2465 UNICODE_STRING EntryFaceNameW
;
2469 Entry
= Head
->Flink
;
2470 while (Entry
!= Head
)
2472 CurrentEntry
= (PFONT_ENTRY
) CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2474 FontGDI
= CurrentEntry
->Font
;
2477 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
2478 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
2479 if (!NT_SUCCESS(status
))
2484 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
2486 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2487 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2490 if (FontFamilyInclude(LogFont
, &EntryFaceNameW
, Info
, min(*Count
, Size
)))
2494 FontFamilyFillInfo(Info
+ *Count
, EntryFaceNameW
.Buffer
,
2499 RtlFreeUnicodeString(&EntryFaceNameW
);
2500 Entry
= Entry
->Flink
;
2506 static BOOLEAN FASTCALL
2507 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2508 PFONTFAMILYINFO Info
,
2512 PLIST_ENTRY pEntry
, pHead
= &FontSubstListHead
;
2513 PFONTSUBST_ENTRY pCurrentEntry
;
2514 PUNICODE_STRING pFromW
;
2516 LOGFONTW lf
= *LogFont
;
2517 UNICODE_STRING NameW
;
2519 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2521 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2523 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2524 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2526 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2527 continue; /* mismatch */
2530 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2531 SubstituteFontRecurse(&lf
);
2533 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2534 FontGDI
= FindFaceNameInLists(&NameW
);
2535 if (FontGDI
== NULL
)
2537 continue; /* no real font */
2540 if (*pCount
< MaxCount
)
2542 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2552 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2556 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2557 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2558 lprs
->nLanguageID
= gusLanguageID
;
2561 EngSetLastError(ERROR_INVALID_PARAMETER
);
2571 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2572 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2573 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2574 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2577 FT_BitmapGlyph APIENTRY
2584 PLIST_ENTRY CurrentEntry
;
2585 PFONT_CACHE_ENTRY FontEntry
;
2587 ASSERT_FREETYPE_LOCK_HELD();
2589 CurrentEntry
= FontCacheListHead
.Flink
;
2590 while (CurrentEntry
!= &FontCacheListHead
)
2592 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2593 if ((FontEntry
->Face
== Face
) &&
2594 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2595 (FontEntry
->Height
== Height
) &&
2596 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2598 CurrentEntry
= CurrentEntry
->Flink
;
2601 if (CurrentEntry
== &FontCacheListHead
)
2606 RemoveEntryList(CurrentEntry
);
2607 InsertHeadList(&FontCacheListHead
, CurrentEntry
);
2608 return FontEntry
->BitmapGlyph
;
2612 FT_BitmapGlyph APIENTRY
2615 FT_GlyphSlot GlyphSlot
,
2616 FT_Render_Mode RenderMode
)
2620 FT_Bitmap AlignedBitmap
;
2621 FT_BitmapGlyph BitmapGlyph
;
2623 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2626 DPRINT1("Failure getting glyph.\n");
2630 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2633 FT_Done_Glyph(Glyph
);
2634 DPRINT1("Failure rendering glyph.\n");
2638 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2639 FT_Bitmap_New(&AlignedBitmap
);
2640 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2642 DPRINT1("Conversion failed\n");
2643 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2647 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2648 BitmapGlyph
->bitmap
= AlignedBitmap
;
2653 FT_BitmapGlyph APIENTRY
2659 FT_GlyphSlot GlyphSlot
,
2660 FT_Render_Mode RenderMode
)
2664 PFONT_CACHE_ENTRY NewEntry
;
2665 FT_Bitmap AlignedBitmap
;
2666 FT_BitmapGlyph BitmapGlyph
;
2668 ASSERT_FREETYPE_LOCK_HELD();
2670 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2673 DPRINT1("Failure caching glyph.\n");
2677 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2680 FT_Done_Glyph(GlyphCopy
);
2681 DPRINT1("Failure rendering glyph.\n");
2685 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2688 DPRINT1("Alloc failure caching glyph.\n");
2689 FT_Done_Glyph(GlyphCopy
);
2693 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2694 FT_Bitmap_New(&AlignedBitmap
);
2695 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2697 DPRINT1("Conversion failed\n");
2698 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2699 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2700 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2704 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2705 BitmapGlyph
->bitmap
= AlignedBitmap
;
2707 NewEntry
->GlyphIndex
= GlyphIndex
;
2708 NewEntry
->Face
= Face
;
2709 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2710 NewEntry
->Height
= Height
;
2711 NewEntry
->mxWorldToDevice
= *pmx
;
2713 InsertHeadList(&FontCacheListHead
, &NewEntry
->ListEntry
);
2714 if (++FontCacheNumEntries
> MAX_FONT_CACHE
)
2716 NewEntry
= CONTAINING_RECORD(FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2717 RemoveCachedEntry(NewEntry
);
2724 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2726 pt
->x
.value
= vec
->x
>> 6;
2727 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2728 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2729 pt
->y
.value
= vec
->y
>> 6;
2730 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2731 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2735 This function builds an FT_Fixed from a float. It puts the integer part
2736 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2737 It fails if the integer part of the float number is greater than SHORT_MAX.
2739 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2742 unsigned short fract
= (f
- value
) * 0xFFFF;
2743 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2747 This function builds an FT_Fixed from a FIXED. It simply put f.value
2748 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2750 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2752 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2755 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2757 TTPOLYGONHEADER
*pph
;
2759 int needed
= 0, point
= 0, contour
, first_pt
;
2760 unsigned int pph_start
, cpfx
;
2763 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2765 /* Ignore contours containing one point */
2766 if (point
== outline
->contours
[contour
])
2773 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2777 pph
->dwType
= TT_POLYGON_TYPE
;
2778 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2780 needed
+= sizeof(*pph
);
2782 while (point
<= outline
->contours
[contour
])
2784 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2785 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2786 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2791 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2794 } while (point
<= outline
->contours
[contour
] &&
2795 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2796 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2797 /* At the end of a contour Windows adds the start point, but
2799 if (point
> outline
->contours
[contour
] &&
2800 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2803 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2806 else if (point
<= outline
->contours
[contour
] &&
2807 outline
->tags
[point
] & FT_Curve_Tag_On
)
2809 /* add closing pt for bezier */
2811 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2820 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2823 pph
->cb
= needed
- pph_start
;
2828 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2830 /* Convert the quadratic Beziers to cubic Beziers.
2831 The parametric eqn for a cubic Bezier is, from PLRM:
2832 r(t) = at^3 + bt^2 + ct + r0
2833 with the control points:
2838 A quadratic Bezier has the form:
2839 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2841 So equating powers of t leads to:
2842 r1 = 2/3 p1 + 1/3 p0
2843 r2 = 2/3 p1 + 1/3 p2
2844 and of course r0 = p0, r3 = p2
2846 int contour
, point
= 0, first_pt
;
2847 TTPOLYGONHEADER
*pph
;
2849 DWORD pph_start
, cpfx
, type
;
2850 FT_Vector cubic_control
[4];
2851 unsigned int needed
= 0;
2853 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2856 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2860 pph
->dwType
= TT_POLYGON_TYPE
;
2861 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2863 needed
+= sizeof(*pph
);
2865 while (point
<= outline
->contours
[contour
])
2867 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2868 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2869 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2873 if (type
== TT_PRIM_LINE
)
2876 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2882 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2885 /* FIXME: Possible optimization in endpoint calculation
2886 if there are two consecutive curves */
2887 cubic_control
[0] = outline
->points
[point
-1];
2888 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2890 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2891 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2892 cubic_control
[0].x
>>= 1;
2893 cubic_control
[0].y
>>= 1;
2895 if (point
+1 > outline
->contours
[contour
])
2896 cubic_control
[3] = outline
->points
[first_pt
];
2899 cubic_control
[3] = outline
->points
[point
+1];
2900 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2902 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2903 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2904 cubic_control
[3].x
>>= 1;
2905 cubic_control
[3].y
>>= 1;
2908 /* r1 = 1/3 p0 + 2/3 p1
2909 r2 = 1/3 p2 + 2/3 p1 */
2910 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2911 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2912 cubic_control
[2] = cubic_control
[1];
2913 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2914 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2915 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2916 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2919 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2920 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2921 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
2926 } while (point
<= outline
->contours
[contour
] &&
2927 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2928 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2929 /* At the end of a contour Windows adds the start point,
2930 but only for Beziers and we've already done that.
2932 if (point
<= outline
->contours
[contour
] &&
2933 outline
->tags
[point
] & FT_Curve_Tag_On
)
2935 /* This is the closing pt of a bezier, but we've already
2936 added it, so just inc point and carry on */
2944 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2947 pph
->cb
= needed
- pph_start
;
2953 IntRequestFontSize(PDC dc
, FT_Face face
, LONG Width
, LONG Height
)
2955 FT_Size_RequestRec req
;
2966 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
2976 if (Width
> 0xFFFFU
)
2978 if (Height
> 0xFFFFU
)
2981 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
2982 req
.width
= (FT_Long
)(Width
<< 6);
2983 req
.height
= (FT_Long
)(Height
<< 6);
2984 req
.horiResolution
= 0;
2985 req
.vertResolution
= 0;
2986 return FT_Request_Size(face
, &req
);
2991 TextIntUpdateSize(PDC dc
,
2998 FT_CharMap charmap
, found
;
3004 face
= FontGDI
->SharedFace
->Face
;
3005 if (face
->charmap
== NULL
)
3007 DPRINT("WARNING: No charmap selected!\n");
3008 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3011 for (n
= 0; n
< face
->num_charmaps
; n
++)
3013 charmap
= face
->charmaps
[n
];
3014 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
3015 if (charmap
->encoding
!= 0)
3023 DPRINT1("WARNING: Could not find desired charmap!\n");
3027 error
= FT_Set_Charmap(face
, found
);
3030 DPRINT1("WARNING: Could not set the charmap!\n");
3035 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3037 error
= IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
3044 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3053 * Based on WineEngGetGlyphOutline
3058 ftGdiGetGlyphOutline(
3066 BOOL bIgnoreRotation
)
3068 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3076 FT_UInt glyph_index
;
3077 DWORD width
, height
, pitch
, needed
= 0;
3078 FT_Bitmap ft_bitmap
;
3080 INT left
, right
, top
= 0, bottom
= 0;
3082 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3083 FLOAT eM11
, widthRatio
= 1.0;
3084 FT_Matrix transMat
= identityMat
;
3085 BOOL needsTransform
= FALSE
;
3088 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3089 OUTLINETEXTMETRICW
*potm
;
3093 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3094 cjBuf
, pvBuf
, pmat2
);
3096 pdcattr
= dc
->pdcattr
;
3098 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3101 hFont
= pdcattr
->hlfntNew
;
3102 TextObj
= RealizeFontInit(hFont
);
3106 EngSetLastError(ERROR_INVALID_HANDLE
);
3109 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3110 ft_face
= FontGDI
->SharedFace
->Face
;
3112 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3113 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3114 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3116 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3117 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3120 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3121 TEXTOBJ_UnlockText(TextObj
);
3124 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3127 /* FIXME: last error? */
3128 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3129 TEXTOBJ_UnlockText(TextObj
);
3134 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3135 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3137 TEXTOBJ_UnlockText(TextObj
);
3139 if (iFormat
& GGO_GLYPH_INDEX
)
3142 iFormat
&= ~GGO_GLYPH_INDEX
;
3144 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3146 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3147 load_flags
|= FT_LOAD_NO_BITMAP
;
3149 if (iFormat
& GGO_UNHINTED
)