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
52 /* special font names */
53 static const UNICODE_STRING MarlettW
= RTL_CONSTANT_STRING(L
"Marlett");
54 static const UNICODE_STRING SystemW
= RTL_CONSTANT_STRING(L
"System");
55 static const UNICODE_STRING FixedSysW
= RTL_CONSTANT_STRING(L
"FixedSys");
58 static UNICODE_STRING FontRegPath
=
59 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
62 /* The FreeType library is not thread safe, so we have
63 to serialize access to it */
64 static PFAST_MUTEX FreeTypeLock
;
66 static LIST_ENTRY FontListHead
;
67 static PFAST_MUTEX FontListLock
;
68 static BOOL RenderingEnabled
= TRUE
;
70 #define IntLockGlobalFonts \
71 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FontListLock)
73 #define IntUnLockGlobalFonts \
74 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FontListLock)
76 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
77 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
79 #define IntLockFreeType \
80 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FreeTypeLock)
82 #define IntUnLockFreeType \
83 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FreeTypeLock)
85 #define ASSERT_FREETYPE_LOCK_HELD() \
86 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
88 #define MAX_FONT_CACHE 256
90 static LIST_ENTRY FontCacheListHead
;
91 static UINT FontCacheNumEntries
;
93 static PWCHAR ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
103 L
"Vietnamese", /* 08 */
104 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
112 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
117 * For TranslateCharsetInfo
120 #define MAXTCIINDEX 32
121 static const CHARSETINFO FontTci
[MAXTCIINDEX
] =
124 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
125 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
126 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
127 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
128 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
129 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
130 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
131 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
132 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
133 /* reserved by ANSI */
134 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
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}} },
142 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
143 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
144 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
145 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
146 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
147 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
148 /* Reserved for alternate ANSI and OEM */
149 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
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 /* Reserved for system */
158 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
159 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
163 static RTL_STATIC_LIST_HEAD(FontSubstListHead
);
166 SharedMem_AddRef(PSHARED_MEM Ptr
)
168 ASSERT_FREETYPE_LOCK_HELD();
174 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
177 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
182 Ptr
->Memory
= Memory
;
183 SharedMem_AddRef(Memory
);
184 DPRINT("Creating SharedFace for %s\n", Face
->family_name
);
190 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
193 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
196 Ptr
->Buffer
= Buffer
;
197 Ptr
->BufferSize
= BufferSize
;
199 Ptr
->IsMapping
= IsMapping
;
200 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
206 SharedFace_AddRef(PSHARED_FACE Ptr
)
208 ASSERT_FREETYPE_LOCK_HELD();
214 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
216 ASSERT_FREETYPE_LOCK_HELD();
218 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
219 RemoveEntryList(&Entry
->ListEntry
);
220 ExFreePoolWithTag(Entry
, TAG_FONT
);
221 FontCacheNumEntries
--;
222 ASSERT(FontCacheNumEntries
<= MAX_FONT_CACHE
);
226 RemoveCacheEntries(FT_Face Face
)
228 PLIST_ENTRY CurrentEntry
;
229 PFONT_CACHE_ENTRY FontEntry
;
231 ASSERT_FREETYPE_LOCK_HELD();
233 CurrentEntry
= FontCacheListHead
.Flink
;
234 while (CurrentEntry
!= &FontCacheListHead
)
236 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
237 CurrentEntry
= CurrentEntry
->Flink
;
239 if (FontEntry
->Face
== Face
)
241 RemoveCachedEntry(FontEntry
);
246 static void SharedMem_Release(PSHARED_MEM Ptr
)
248 ASSERT_FREETYPE_LOCK_HELD();
249 ASSERT(Ptr
->RefCount
> 0);
251 if (Ptr
->RefCount
<= 0)
255 if (Ptr
->RefCount
== 0)
257 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
259 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
261 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
262 ExFreePoolWithTag(Ptr
, TAG_FONT
);
267 SharedFace_Release(PSHARED_FACE Ptr
)
270 ASSERT(Ptr
->RefCount
> 0);
272 if (Ptr
->RefCount
<= 0)
276 if (Ptr
->RefCount
== 0)
278 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
);
279 RemoveCacheEntries(Ptr
->Face
);
280 FT_Done_Face(Ptr
->Face
);
281 SharedMem_Release(Ptr
->Memory
);
282 ExFreePoolWithTag(Ptr
, TAG_FONT
);
289 * IntLoadFontSubstList --- loads the list of font substitutes
292 IntLoadFontSubstList(PLIST_ENTRY pHead
)
296 OBJECT_ATTRIBUTES ObjectAttributes
;
297 KEY_FULL_INFORMATION KeyFullInfo
;
299 UNICODE_STRING FromW
, ToW
;
300 BYTE InfoBuffer
[128];
301 PKEY_VALUE_FULL_INFORMATION pInfo
;
302 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
304 PFONTSUBST_ENTRY pEntry
;
306 /* the FontSubstitutes registry key */
307 static UNICODE_STRING FontSubstKey
=
308 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
309 L
"Microsoft\\Windows NT\\CurrentVersion\\"
312 /* open registry key */
313 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
314 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
316 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
317 if (!NT_SUCCESS(Status
))
319 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
320 return FALSE
; /* failure */
323 /* query count of values */
324 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
325 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
326 if (!NT_SUCCESS(Status
))
328 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
330 return FALSE
; /* failure */
334 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
337 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
338 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
339 if (!NT_SUCCESS(Status
))
341 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
345 /* create FromW string */
346 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
347 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
348 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
349 Status
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
350 if (!NT_SUCCESS(Status
))
352 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
357 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
358 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
359 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
360 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
362 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
363 RtlFreeUnicodeString(&FromW
);
367 /* create ToW string */
368 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
369 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
370 pch
[Length
] = UNICODE_NULL
; /* truncate */
371 Status
= RtlCreateUnicodeString(&ToW
, pch
);
372 if (!NT_SUCCESS(Status
))
374 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
375 RtlFreeUnicodeString(&FromW
);
379 /* does charset exist? (from) */
380 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
381 pch
= wcsrchr(FromW
.Buffer
, L
',');
386 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
387 /* parse charset number */
388 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
391 /* does charset exist? (to) */
392 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
393 pch
= wcsrchr(ToW
.Buffer
, L
',');
398 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
399 /* parse charset number */
400 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
403 /* allocate an entry */
404 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
407 DPRINT("ExAllocatePoolWithTag failed\n");
408 RtlFreeUnicodeString(&FromW
);
409 RtlFreeUnicodeString(&ToW
);
413 /* store to *pEntry */
414 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
415 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
416 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
417 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
419 /* insert pEntry to *pHead */
420 InsertTailList(pHead
, &pEntry
->ListEntry
);
426 return NT_SUCCESS(Status
);
430 InitFontSupport(VOID
)
434 InitializeListHead(&FontListHead
);
435 InitializeListHead(&FontCacheListHead
);
436 FontCacheNumEntries
= 0;
437 /* Fast Mutexes must be allocated from non paged pool */
438 FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
439 if (FontListLock
== NULL
)
444 ExInitializeFastMutex(FontListLock
);
445 FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
446 if (FreeTypeLock
== NULL
)
450 ExInitializeFastMutex(FreeTypeLock
);
452 ulError
= FT_Init_FreeType(&library
);
455 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
459 IntLoadSystemFonts();
460 IntLoadFontSubstList(&FontSubstListHead
);
466 FtSetCoordinateTransform(
473 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
475 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
476 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
479 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
480 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
483 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
484 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
487 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
488 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
490 /* Set the transformation matrix */
491 FT_Set_Transform(face
, &ftmatrix
, 0);
495 SubstituteFontByList(PLIST_ENTRY pHead
,
496 PUNICODE_STRING pOutputName
,
497 PUNICODE_STRING pInputName
,
498 BYTE RequestedCharSet
,
499 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
502 PLIST_ENTRY pListEntry
;
503 PFONTSUBST_ENTRY pSubstEntry
;
504 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
506 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
507 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
509 /* for each list entry */
510 for (pListEntry
= pHead
->Flink
;
512 pListEntry
= pListEntry
->Flink
)
515 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
517 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
519 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
520 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
522 continue; /* not matched */
525 /* does charset number exist? (to) */
526 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
528 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
532 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
535 /* does font name match? */
536 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
539 continue; /* not matched */
542 /* update *pOutputName */
543 RtlFreeUnicodeString(pOutputName
);
544 Status
= RtlCreateUnicodeString(pOutputName
,
545 pSubstEntry
->FontNames
[FONTSUBST_TO
].Buffer
);
546 if (!NT_SUCCESS(Status
))
548 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
549 continue; /* cannot create string */
552 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
554 /* update CharSetMap */
555 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
556 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
558 return TRUE
; /* success */
565 SubstituteFontRecurse(PUNICODE_STRING pInOutName
, BYTE
*pRequestedCharSet
)
567 UINT RecurseCount
= 5;
568 UNICODE_STRING OutputNameW
= { 0 };
569 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
572 if (pInOutName
->Buffer
[0] == UNICODE_NULL
)
575 while (RecurseCount
-- > 0)
577 RtlInitUnicodeString(&OutputNameW
, NULL
);
578 Found
= SubstituteFontByList(&FontSubstListHead
,
579 &OutputNameW
, pInOutName
,
580 *pRequestedCharSet
, CharSetMap
);
584 /* update *pInOutName and *pRequestedCharSet */
585 RtlFreeUnicodeString(pInOutName
);
586 *pInOutName
= OutputNameW
;
587 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
588 CharSetMap
[FONTSUBST_FROM
] == *pRequestedCharSet
)
590 *pRequestedCharSet
= CharSetMap
[FONTSUBST_TO
];
594 return TRUE
; /* success */
600 * Search the system font directory and adds each font found.
603 IntLoadSystemFonts(VOID
)
605 OBJECT_ATTRIBUTES ObjectAttributes
;
606 UNICODE_STRING Directory
, FileName
, TempString
;
607 IO_STATUS_BLOCK Iosb
;
610 PFILE_DIRECTORY_INFORMATION DirInfo
;
611 BOOLEAN bRestartScan
= TRUE
;
614 static UNICODE_STRING SearchPatterns
[] =
616 RTL_CONSTANT_STRING(L
"*.ttf"),
617 RTL_CONSTANT_STRING(L
"*.ttc"),
618 RTL_CONSTANT_STRING(L
"*.otf"),
619 RTL_CONSTANT_STRING(L
"*.otc"),
620 RTL_CONSTANT_STRING(L
"*.fon"),
621 RTL_CONSTANT_STRING(L
"*.fnt")
624 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
626 InitializeObjectAttributes(
629 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
635 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
638 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
639 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
641 if (NT_SUCCESS(Status
))
643 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
645 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
646 if (DirInfoBuffer
== NULL
)
652 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
653 if (FileName
.Buffer
== NULL
)
655 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
660 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
664 Status
= ZwQueryDirectoryFile(
672 FileDirectoryInformation
,
677 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
682 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
685 TempString
.Buffer
= DirInfo
->FileName
;
687 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
688 RtlCopyUnicodeString(&FileName
, &Directory
);
689 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
690 IntGdiAddFontResource(&FileName
, 0);
691 if (DirInfo
->NextEntryOffset
== 0)
693 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
696 bRestartScan
= FALSE
;
699 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
700 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
707 ItalicFromStyle(const char *style_name
)
709 if (style_name
== NULL
|| style_name
[0] == 0)
711 if (strstr(style_name
, "Italic") != NULL
)
713 if (strstr(style_name
, "Oblique") != NULL
)
719 WeightFromStyle(const char *style_name
)
721 if (style_name
== NULL
|| style_name
[0] == 0)
723 if (strstr(style_name
, "Regular") != NULL
)
725 if (strstr(style_name
, "Normal") != NULL
)
727 if (strstr(style_name
, "SemiBold") != NULL
)
729 if (strstr(style_name
, "UltraBold") != NULL
)
731 if (strstr(style_name
, "DemiBold") != NULL
)
733 if (strstr(style_name
, "ExtraBold") != NULL
)
735 if (strstr(style_name
, "Bold") != NULL
)
737 if (strstr(style_name
, "UltraLight") != NULL
)
738 return FW_ULTRALIGHT
;
739 if (strstr(style_name
, "ExtraLight") != NULL
)
740 return FW_EXTRALIGHT
;
741 if (strstr(style_name
, "Light") != NULL
)
743 if (strstr(style_name
, "Hairline") != NULL
)
745 if (strstr(style_name
, "Book") != NULL
)
747 if (strstr(style_name
, "ExtraBlack") != NULL
)
749 if (strstr(style_name
, "UltraBlack") != NULL
)
751 if (strstr(style_name
, "Black") != NULL
)
753 if (strstr(style_name
, "Medium") != NULL
)
755 if (strstr(style_name
, "Thin") != NULL
)
757 if (strstr(style_name
, "Heavy") != NULL
)
763 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
764 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
768 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
772 ANSI_STRING AnsiFaceName
;
773 FT_WinFNT_HeaderRec WinFNT
;
774 INT FaceCount
= 0, CharSetCount
= 0;
775 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
776 DWORD Characteristics
= pLoadFont
->Characteristics
;
777 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
780 FT_UShort os2_version
;
781 FT_ULong os2_ulCodePageRange1
;
782 FT_UShort os2_usWeightClass
;
784 if (SharedFace
== NULL
&& CharSetIndex
== -1)
786 /* load a face from memory */
788 Error
= FT_New_Memory_Face(
790 pLoadFont
->Memory
->Buffer
,
791 pLoadFont
->Memory
->BufferSize
,
792 ((FontIndex
!= -1) ? FontIndex
: 0),
796 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
800 if (FT_IS_SFNT(Face
))
801 pLoadFont
->IsTrueType
= TRUE
;
803 if (Error
|| SharedFace
== NULL
)
806 SharedFace_Release(SharedFace
);
808 if (Error
== FT_Err_Unknown_File_Format
)
809 DPRINT1("Unknown font file format\n");
811 DPRINT1("Error reading font (error code: %d)\n", Error
);
812 return 0; /* failure */
817 Face
= SharedFace
->Face
;
819 SharedFace_AddRef(SharedFace
);
823 /* allocate a FONT_ENTRY */
824 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
827 SharedFace_Release(SharedFace
);
828 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
829 return 0; /* failure */
832 /* allocate a FONTGDI */
833 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
836 SharedFace_Release(SharedFace
);
837 ExFreePoolWithTag(Entry
, TAG_FONT
);
838 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
839 return 0; /* failure */
845 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
846 pFileName
->Length
+ sizeof(UNICODE_NULL
),
848 if (FontGDI
->Filename
== NULL
)
851 SharedFace_Release(SharedFace
);
852 ExFreePoolWithTag(Entry
, TAG_FONT
);
853 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
854 return 0; /* failure */
856 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
857 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
861 FontGDI
->Filename
= NULL
;
863 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
866 if (FontGDI
->Filename
)
867 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
869 SharedFace_Release(SharedFace
);
870 ExFreePoolWithTag(Entry
, TAG_FONT
);
874 PrivateEntry
->Entry
= Entry
;
875 if (pLoadFont
->PrivateEntry
)
877 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
881 InitializeListHead(&PrivateEntry
->ListEntry
);
882 pLoadFont
->PrivateEntry
= PrivateEntry
;
887 FontGDI
->SharedFace
= SharedFace
;
888 FontGDI
->CharSet
= ANSI_CHARSET
;
889 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
890 FontGDI
->RequestItalic
= FALSE
;
891 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
892 FontGDI
->RequestWeight
= FW_NORMAL
;
894 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
895 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
896 if (!NT_SUCCESS(Status
))
900 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
902 pLoadFont
->PrivateEntry
= NULL
;
906 RemoveEntryList(&PrivateEntry
->ListEntry
);
908 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
910 if (FontGDI
->Filename
)
911 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
913 SharedFace_Release(SharedFace
);
914 ExFreePoolWithTag(Entry
, TAG_FONT
);
920 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
923 os2_version
= pOS2
->version
;
924 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
925 os2_usWeightClass
= pOS2
->usWeightClass
;
929 if (pOS2
&& os2_version
>= 1)
931 /* get charset and weight from OS/2 header */
933 /* Make sure we do not use this pointer anymore */
936 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
938 if (os2_ulCodePageRange1
& (1 << BitIndex
))
940 if (FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
943 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
944 CharSetIndex
== CharSetCount
)
946 FontGDI
->CharSet
= FontTci
[BitIndex
].ciCharset
;
953 /* set actual weight */
954 FontGDI
->OriginalWeight
= os2_usWeightClass
;
958 /* get charset from WinFNT header */
960 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
963 FontGDI
->CharSet
= WinFNT
.charset
;
968 /* FIXME: CharSet is invalid on Marlett */
969 if (RtlEqualUnicodeString(&Entry
->FaceName
, &MarlettW
, TRUE
))
971 FontGDI
->CharSet
= SYMBOL_CHARSET
;
975 DPRINT("Font loaded: %s (%s)\n", Face
->family_name
, Face
->style_name
);
976 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
977 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
979 /* Add this font resource to the font table */
980 Entry
->Font
= FontGDI
;
981 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
983 if (Characteristics
& FR_PRIVATE
)
986 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
987 IntLockProcessPrivateFonts(Win32Process
);
988 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
989 IntUnLockProcessPrivateFonts(Win32Process
);
995 InsertTailList(&FontListHead
, &Entry
->ListEntry
);
996 IntUnLockGlobalFonts
;
1001 if (FT_IS_SFNT(Face
))
1003 TT_Face TrueType
= (TT_Face
)Face
;
1004 if (TrueType
->ttc_header
.count
> 1)
1007 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1009 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1016 if (CharSetIndex
== -1)
1020 if (pLoadFont
->RegValueName
.Length
== 0)
1022 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1026 UNICODE_STRING NewString
;
1027 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1028 NewString
.Length
= 0;
1029 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1030 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1031 NewString
.MaximumLength
,
1033 NewString
.Buffer
[0] = UNICODE_NULL
;
1035 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1036 RtlAppendUnicodeToString(&NewString
, L
" & ");
1037 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1039 RtlFreeUnicodeString(pValueName
);
1040 *pValueName
= NewString
;
1043 for (i
= 1; i
< CharSetCount
; ++i
)
1045 /* Do not count charsets towards 'faces' loaded */
1046 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1050 return FaceCount
; /* number of loaded faces */
1054 * IntGdiAddFontResource
1056 * Adds the font resource from the specified file to the system.
1060 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1064 PVOID Buffer
= NULL
;
1065 IO_STATUS_BLOCK Iosb
;
1066 PVOID SectionObject
;
1068 LARGE_INTEGER SectionSize
;
1069 OBJECT_ATTRIBUTES ObjectAttributes
;
1070 GDI_LOAD_FONT LoadFont
;
1073 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1075 /* Open the font file */
1076 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1077 Status
= ZwOpenFile(
1079 FILE_GENERIC_READ
| SYNCHRONIZE
,
1083 FILE_SYNCHRONOUS_IO_NONALERT
);
1084 if (!NT_SUCCESS(Status
))
1086 DPRINT("Could not load font file: %wZ\n", FileName
);
1090 SectionSize
.QuadPart
= 0LL;
1091 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1092 NULL
, &SectionSize
, PAGE_READONLY
,
1093 SEC_COMMIT
, FileHandle
, NULL
);
1094 if (!NT_SUCCESS(Status
))
1096 DPRINT("Could not map file: %wZ\n", FileName
);
1097 ZwClose(FileHandle
);
1100 ZwClose(FileHandle
);
1102 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1103 if (!NT_SUCCESS(Status
))
1105 DPRINT("Could not map file: %wZ\n", FileName
);
1106 ObDereferenceObject(SectionObject
);
1110 LoadFont
.pFileName
= FileName
;
1111 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1112 LoadFont
.Characteristics
= Characteristics
;
1113 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1114 LoadFont
.IsTrueType
= FALSE
;
1115 LoadFont
.PrivateEntry
= NULL
;
1116 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1118 ObDereferenceObject(SectionObject
);
1120 /* Release our copy */
1122 SharedMem_Release(LoadFont
.Memory
);
1127 if (LoadFont
.IsTrueType
)
1129 /* append " (TrueType)" */
1130 UNICODE_STRING NewString
;
1133 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1134 NewString
.Length
= 0;
1135 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1136 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1137 NewString
.MaximumLength
,
1139 NewString
.Buffer
[0] = UNICODE_NULL
;
1141 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1142 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1143 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1144 LoadFont
.RegValueName
= NewString
;
1148 InitializeObjectAttributes(&ObjectAttributes
, &FontRegPath
,
1149 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1151 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1152 if (NT_SUCCESS(Status
))
1155 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1159 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1160 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1161 pFileName
, DataSize
);
1166 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1172 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1174 GDI_LOAD_FONT LoadFont
;
1175 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1179 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1186 memcpy(BufferCopy
, Buffer
, dwSize
);
1188 LoadFont
.pFileName
= NULL
;
1189 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1190 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1191 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1192 LoadFont
.IsTrueType
= FALSE
;
1193 LoadFont
.PrivateEntry
= NULL
;
1194 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1196 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1198 /* Release our copy */
1200 SharedMem_Release(LoadFont
.Memory
);
1205 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1206 if (EntryCollection
)
1208 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1209 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1210 IntLockProcessPrivateFonts(Win32Process
);
1211 EntryCollection
->Handle
= ++Win32Process
->PrivateMemFontHandleCount
;
1212 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1213 IntUnLockProcessPrivateFonts(Win32Process
);
1214 Ret
= (HANDLE
)EntryCollection
->Handle
;
1217 *pNumAdded
= FaceCount
;
1222 // FIXME: Add RemoveFontResource
1224 static VOID FASTCALL
1225 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1227 PFONTGDI FontGDI
= FontEntry
->Font
;
1228 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1230 if (FontGDI
->Filename
)
1231 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1233 EngFreeMem(FontGDI
);
1234 SharedFace_Release(SharedFace
);
1235 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1239 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1242 PFONT_ENTRY_MEM FontEntry
;
1244 while (!IsListEmpty(&Head
->ListEntry
))
1246 Entry
= RemoveHeadList(&Head
->ListEntry
);
1247 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1249 CleanupFontEntry(FontEntry
->Entry
);
1250 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1253 CleanupFontEntry(Head
->Entry
);
1254 ExFreePoolWithTag(Head
, TAG_FONT
);
1257 static VOID FASTCALL
1258 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1260 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1261 PLIST_ENTRY ListEntry
;
1262 RemoveEntryList(&Collection
->ListEntry
);
1265 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1266 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1268 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1269 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1271 } while (FontMemEntry
!= Collection
->Entry
);
1275 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1278 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1279 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1280 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1282 IntLockProcessPrivateFonts(Win32Process
);
1283 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1284 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1286 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1288 if (CurrentEntry
->Handle
== (UINT
)hMMFont
)
1290 EntryCollection
= CurrentEntry
;
1291 UnlinkFontMemCollection(CurrentEntry
);
1295 Entry
= Entry
->Flink
;
1297 IntUnLockProcessPrivateFonts(Win32Process
);
1299 if (EntryCollection
)
1301 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1302 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1310 IntGdiCleanupPrivateFontsForProcess(VOID
)
1312 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1314 PFONT_ENTRY_COLL_MEM EntryCollection
;
1316 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1319 EntryCollection
= NULL
;
1321 IntLockProcessPrivateFonts(Win32Process
);
1322 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1324 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1325 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1326 UnlinkFontMemCollection(EntryCollection
);
1328 IntUnLockProcessPrivateFonts(Win32Process
);
1330 if (EntryCollection
)
1332 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1333 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1337 /* No Mem fonts anymore, see if we have any other private fonts left */
1339 IntLockProcessPrivateFonts(Win32Process
);
1340 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1342 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1344 IntUnLockProcessPrivateFonts(Win32Process
);
1348 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1356 IntIsFontRenderingEnabled(VOID
)
1358 BOOL Ret
= RenderingEnabled
;
1361 hDC
= IntGetScreenDC();
1363 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && RenderingEnabled
;
1369 IntEnableFontRendering(BOOL Enable
)
1371 RenderingEnabled
= Enable
;
1374 FT_Render_Mode FASTCALL
1375 IntGetFontRenderMode(LOGFONTW
*logfont
)
1377 switch (logfont
->lfQuality
)
1379 case ANTIALIASED_QUALITY
:
1380 case NONANTIALIASED_QUALITY
:
1381 return FT_RENDER_MODE_MONO
;
1383 return FT_RENDER_MODE_LIGHT
;
1384 /* case CLEARTYPE_QUALITY:
1385 return FT_RENDER_MODE_LCD; */
1387 return FT_RENDER_MODE_NORMAL
;
1392 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1397 plfont
= LFONT_AllocFontWithHandle();
1400 return STATUS_NO_MEMORY
;
1403 ExInitializePushLock(&plfont
->lock
);
1404 *NewFont
= plfont
->BaseObject
.hHmgr
;
1405 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1406 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1407 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1409 /* This should really depend on whether GM_ADVANCED is set */
1410 plf
->lfOrientation
= plf
->lfEscapement
;
1412 LFONT_UnlockFont(plfont
);
1414 return STATUS_SUCCESS
;
1417 /*************************************************************************
1418 * TranslateCharsetInfo
1420 * Fills a CHARSETINFO structure for a character set, code page, or
1421 * font. This allows making the correspondance between different labelings
1422 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1423 * of the same encoding.
1425 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1426 * only one codepage should be set in *Src.
1429 * TRUE on success, FALSE on failure.
1432 static BOOLEAN APIENTRY
1433 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1434 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1435 if flags == TCI_SRCCHARSET: a character set value
1436 if flags == TCI_SRCCODEPAGE: a code page value */
1437 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1438 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1444 case TCI_SRCFONTSIG
:
1445 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1450 case TCI_SRCCODEPAGE
:
1451 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciACP
)
1456 case TCI_SRCCHARSET
:
1457 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciCharset
)
1466 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== FontTci
[Index
].ciCharset
)
1471 RtlCopyMemory(Cs
, &FontTci
[Index
], sizeof(CHARSETINFO
));
1477 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1481 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1483 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1490 static void FASTCALL
1491 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1492 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1493 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1495 FT_Fixed XScale
, YScale
;
1496 int Ascent
, Descent
;
1497 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1499 XScale
= Face
->size
->metrics
.x_scale
;
1500 YScale
= Face
->size
->metrics
.y_scale
;
1504 TM
->tmHeight
= pFNT
->pixel_height
;
1505 TM
->tmAscent
= pFNT
->ascent
;
1506 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1507 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1508 TM
->tmExternalLeading
= pFNT
->external_leading
;
1509 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1510 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1512 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1513 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1514 TM
->tmFirstChar
= pFNT
->first_char
;
1515 TM
->tmLastChar
= pFNT
->last_char
;
1516 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1517 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1518 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1521 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1522 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1523 TM
->tmUnderlined
= pFNT
->underline
;
1524 TM
->tmStruckOut
= pFNT
->strike_out
;
1525 TM
->tmCharSet
= pFNT
->charset
;
1529 TM
->tmWeight
= FontGDI
->RequestWeight
;
1530 TM
->tmItalic
= FontGDI
->RequestItalic
;
1531 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1532 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1533 TM
->tmCharSet
= FontGDI
->CharSet
;
1538 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1540 Ascent
= pHori
->Ascender
;
1541 Descent
= -pHori
->Descender
;
1545 Ascent
= pOS2
->usWinAscent
;
1546 Descent
= pOS2
->usWinDescent
;
1549 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1550 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1551 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1552 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1553 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1554 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1556 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1558 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1561 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1563 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1564 - ((Ascent
+ Descent
)
1565 - (pHori
->Ascender
- pHori
->Descender
)),
1566 YScale
) + 32) >> 6);
1568 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1569 if (TM
->tmAveCharWidth
== 0)
1571 TM
->tmAveCharWidth
= 1;
1574 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1575 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1579 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1583 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1584 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1586 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1590 TM
->tmWeight
= FontGDI
->RequestWeight
;
1595 TM
->tmDigitizedAspectX
= 96;
1596 TM
->tmDigitizedAspectY
= 96;
1597 if (face_has_symbol_charmap(Face
) ||
1598 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1600 USHORT cpOEM
, cpAnsi
;
1602 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1603 TM
->tmFirstChar
= 0;
1606 case 1257: /* Baltic */
1607 TM
->tmLastChar
= 0xf8fd;
1610 TM
->tmLastChar
= 0xf0ff;
1612 TM
->tmBreakChar
= 0x20;
1613 TM
->tmDefaultChar
= 0x1f;
1617 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1618 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1620 if(pOS2
->usFirstCharIndex
<= 1)
1621 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1622 else if (pOS2
->usFirstCharIndex
> 0xff)
1623 TM
->tmBreakChar
= 0x20;
1625 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1626 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1631 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1632 TM
->tmUnderlined
= FALSE
;
1633 TM
->tmStruckOut
= FALSE
;
1637 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1639 TM
->tmItalic
= 0xFF;
1645 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1646 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1649 if (!FT_IS_FIXED_WIDTH(Face
))
1651 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1653 case PAN_PROP_MONOSPACED
:
1654 TM
->tmPitchAndFamily
= 0;
1657 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1663 TM
->tmPitchAndFamily
= 0;
1666 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1668 case PAN_FAMILY_SCRIPT
:
1669 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1671 case PAN_FAMILY_DECORATIVE
:
1672 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1677 case PAN_FAMILY_TEXT_DISPLAY
:
1678 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1679 /* Which is clearly not what the panose spec says. */
1680 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1682 TM
->tmPitchAndFamily
= FF_MODERN
;
1686 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1691 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1694 case PAN_SERIF_COVE
:
1695 case PAN_SERIF_OBTUSE_COVE
:
1696 case PAN_SERIF_SQUARE_COVE
:
1697 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1698 case PAN_SERIF_SQUARE
:
1699 case PAN_SERIF_THIN
:
1700 case PAN_SERIF_BONE
:
1701 case PAN_SERIF_EXAGGERATED
:
1702 case PAN_SERIF_TRIANGLE
:
1703 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1706 case PAN_SERIF_NORMAL_SANS
:
1707 case PAN_SERIF_OBTUSE_SANS
:
1708 case PAN_SERIF_PERP_SANS
:
1709 case PAN_SERIF_FLARED
:
1710 case PAN_SERIF_ROUNDED
:
1711 TM
->tmPitchAndFamily
|= FF_SWISS
;
1717 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1720 if (FT_IS_SCALABLE(Face
))
1722 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1724 if (FT_IS_SFNT(Face
))
1726 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1729 TM
->tmCharSet
= FontGDI
->CharSet
;
1732 static void FASTCALL
1733 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1734 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1735 FT_WinFNT_HeaderRec
*pFNT
)
1737 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1740 /*************************************************************
1741 * IntGetOutlineTextMetrics
1745 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1747 OUTLINETEXTMETRICW
*Otm
)
1751 TT_HoriHeader
*pHori
;
1752 TT_Postscript
*pPost
;
1753 FT_Fixed XScale
, YScale
;
1754 ANSI_STRING FamilyNameA
, StyleNameA
;
1755 UNICODE_STRING FamilyNameW
, StyleNameW
, Regular
;
1756 FT_WinFNT_HeaderRec Win
;
1760 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1762 Needed
= sizeof(OUTLINETEXTMETRICW
);
1764 RtlInitAnsiString(&FamilyNameA
, Face
->family_name
);
1765 status
= RtlAnsiStringToUnicodeString(&FamilyNameW
, &FamilyNameA
, TRUE
);
1766 if (!NT_SUCCESS(status
))
1771 RtlInitAnsiString(&StyleNameA
, Face
->style_name
);
1772 status
= RtlAnsiStringToUnicodeString(&StyleNameW
, &StyleNameA
, TRUE
);
1773 if (!NT_SUCCESS(status
))
1775 RtlFreeUnicodeString(&FamilyNameW
);
1779 /* These names should be read from the TT name table */
1781 /* Length of otmpFamilyName */
1782 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1784 RtlInitUnicodeString(&Regular
, L
"Regular");
1785 /* Length of otmpFaceName */
1786 if (RtlEqualUnicodeString(&StyleNameW
, &Regular
, TRUE
))
1788 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
); /* Just the family name */
1792 Needed
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1); /* family + " " + style */
1795 /* Length of otmpStyleName */
1796 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1798 /* Length of otmpFullName */
1799 Needed
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1);
1803 RtlFreeUnicodeString(&FamilyNameW
);
1804 RtlFreeUnicodeString(&StyleNameW
);
1808 XScale
= Face
->size
->metrics
.x_scale
;
1809 YScale
= Face
->size
->metrics
.y_scale
;
1812 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1816 DPRINT1("Can't find OS/2 table - not TT font?\n");
1817 RtlFreeUnicodeString(&StyleNameW
);
1818 RtlFreeUnicodeString(&FamilyNameW
);
1822 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1826 DPRINT1("Can't find HHEA table - not TT font?\n");
1827 RtlFreeUnicodeString(&StyleNameW
);
1828 RtlFreeUnicodeString(&FamilyNameW
);
1832 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1834 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1836 Otm
->otmSize
= Needed
;
1838 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1841 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1842 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1843 Otm
->otmfsType
= pOS2
->fsType
;
1844 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1845 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1846 Otm
->otmItalicAngle
= 0; /* POST table */
1847 Otm
->otmEMSquare
= Face
->units_per_EM
;
1848 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1849 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1850 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1851 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1852 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1853 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1854 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1855 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1856 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1857 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1858 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1859 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1860 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1861 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1862 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1863 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1864 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1865 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1866 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1867 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1868 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1869 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1870 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1873 Otm
->otmsUnderscoreSize
= 0;
1874 Otm
->otmsUnderscorePosition
= 0;
1878 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1879 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1884 /* otmp* members should clearly have type ptrdiff_t, but M$ knows best */
1885 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1886 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1887 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1888 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1889 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1890 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1891 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1892 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1893 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1894 if (!RtlEqualUnicodeString(&StyleNameW
, &Regular
, TRUE
))
1896 wcscat((WCHAR
*) Cp
, L
" ");
1897 wcscat((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1898 Cp
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1);
1902 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1904 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1905 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1906 wcscat((WCHAR
*) Cp
, L
" ");
1907 wcscat((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1909 RtlFreeUnicodeString(&StyleNameW
);
1910 RtlFreeUnicodeString(&FamilyNameW
);
1915 static PFONTGDI FASTCALL
1916 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1919 PFONT_ENTRY CurrentEntry
;
1920 ANSI_STRING EntryFaceNameA
;
1921 UNICODE_STRING EntryFaceNameW
;
1925 Entry
= Head
->Flink
;
1926 while (Entry
!= Head
)
1928 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1930 FontGDI
= CurrentEntry
->Font
;
1933 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1934 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1935 if (!NT_SUCCESS(status
))
1940 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1942 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1943 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1946 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
1948 RtlFreeUnicodeString(&EntryFaceNameW
);
1952 RtlFreeUnicodeString(&EntryFaceNameW
);
1953 Entry
= Entry
->Flink
;
1959 static PFONTGDI FASTCALL
1960 FindFaceNameInLists(PUNICODE_STRING FaceName
)
1962 PPROCESSINFO Win32Process
;
1965 /* Search the process local list.
1966 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
1967 Win32Process
= PsGetCurrentProcessWin32Process();
1968 IntLockProcessPrivateFonts(Win32Process
);
1969 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
1970 IntUnLockProcessPrivateFonts(Win32Process
);
1976 /* Search the global list */
1978 Font
= FindFaceNameInList(FaceName
, &FontListHead
);
1979 IntUnLockGlobalFonts
;
1984 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
1986 CharSetFromLangID(LANGID LangID
)
1988 /* FIXME: Add more and fix if wrong */
1989 switch (PRIMARYLANGID(LangID
))
1992 switch (SUBLANGID(LangID
))
1994 case SUBLANG_CHINESE_TRADITIONAL
:
1995 return CHINESEBIG5_CHARSET
;
1996 case SUBLANG_CHINESE_SIMPLIFIED
:
2000 return GB2312_CHARSET
;
2002 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2003 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2004 return EASTEUROPE_CHARSET
;
2006 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2007 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2008 return RUSSIAN_CHARSET
;
2010 case LANG_ARABIC
: return ARABIC_CHARSET
;
2011 case LANG_GREEK
: return GREEK_CHARSET
;
2012 case LANG_HEBREW
: return HEBREW_CHARSET
;
2013 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2014 case LANG_KOREAN
: return JOHAB_CHARSET
;
2015 case LANG_TURKISH
: return TURKISH_CHARSET
;
2016 case LANG_THAI
: return THAI_CHARSET
;
2017 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2018 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2020 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2021 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2022 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2023 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2024 case LANG_SWEDISH
: default:
2025 return ANSI_CHARSET
;
2030 SwapEndian(LPVOID pvData
, DWORD Size
)
2032 BYTE b
, *pb
= pvData
;
2044 IntGetFontLocalizedName(PUNICODE_STRING pLocalNameW
, FT_Face Face
)
2048 WCHAR Buf
[LF_FACESIZE
];
2049 NTSTATUS Status
= STATUS_NOT_FOUND
;
2051 RtlInitUnicodeString(pLocalNameW
, NULL
);
2053 Count
= FT_Get_Sfnt_Name_Count(Face
);
2054 for (i
= 0; i
< Count
; ++i
)
2056 FT_Get_Sfnt_Name(Face
, i
, &Name
);
2057 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2058 Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
)
2060 continue; /* not Microsoft Unicode name */
2063 if (Name
.name_id
!= TT_NAME_ID_FONT_FAMILY
||
2064 Name
.string
== NULL
|| Name
.string_len
== 0 ||
2065 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2067 continue; /* not family name */
2070 if (sizeof(Buf
) < Name
.string_len
+ sizeof(UNICODE_NULL
))
2072 continue; /* name too long */
2075 /* NOTE: Name.string is not null-terminated */
2076 RtlCopyMemory(Buf
, Name
.string
, Name
.string_len
);
2077 Buf
[Name
.string_len
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2079 /* Convert UTF-16 big endian to little endian */
2080 SwapEndian(Buf
, Name
.string_len
);
2082 DPRINT("IntGetFontLocalizedName: %S (%d)\n", Buf
, Name
.string_len
);
2085 Status
= RtlCreateUnicodeString(pLocalNameW
, Buf
);
2092 static void FASTCALL
2093 FontFamilyFillInfo(PFONTFAMILYINFO Info
, PCWSTR FaceName
, PFONTGDI FontGDI
)
2096 UNICODE_STRING StyleW
;
2099 CHARSETINFO CharSetInfo
;
2101 OUTLINETEXTMETRICW
*Otm
;
2104 NEWTEXTMETRICW
*Ntm
;
2107 FT_Face Face
= FontGDI
->SharedFace
->Face
;
2109 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2110 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2111 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2116 IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2118 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2119 TM
= &Otm
->otmTextMetrics
;
2121 Lf
->lfHeight
= TM
->tmHeight
;
2122 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2123 Lf
->lfWeight
= TM
->tmWeight
;
2124 Lf
->lfItalic
= TM
->tmItalic
;
2125 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2126 Lf
->lfCharSet
= TM
->tmCharSet
;
2127 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2128 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2129 Lf
->lfQuality
= PROOF_QUALITY
;
2131 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2132 Ntm
->tmHeight
= TM
->tmHeight
;
2133 Ntm
->tmAscent
= TM
->tmAscent
;
2134 Ntm
->tmDescent
= TM
->tmDescent
;
2135 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2136 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2137 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2138 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2139 Ntm
->tmWeight
= TM
->tmWeight
;
2140 Ntm
->tmOverhang
= TM
->tmOverhang
;
2141 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2142 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2143 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2144 Ntm
->tmLastChar
= TM
->tmLastChar
;
2145 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2146 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2147 Ntm
->tmItalic
= TM
->tmItalic
;
2148 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2149 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2150 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2151 Ntm
->tmCharSet
= TM
->tmCharSet
;
2152 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2154 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2156 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2158 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2159 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2160 Ntm
->ntmAvgWidth
= 0;
2162 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2163 ? TRUETYPE_FONTTYPE
: 0);
2165 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2166 Info
->FontType
|= RASTER_FONTTYPE
;
2168 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2170 /* try the localized name */
2171 status
= STATUS_UNSUCCESSFUL
;
2172 if (CharSetFromLangID(gusLanguageID
) == FontGDI
->CharSet
)
2174 /* get localized name */
2175 UNICODE_STRING LocalNameW
;
2176 status
= IntGetFontLocalizedName(&LocalNameW
, Face
);
2177 if (NT_SUCCESS(status
))
2180 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfLogFont
.lfFaceName
,
2181 sizeof(Info
->EnumLogFontEx
.elfLogFont
.lfFaceName
),
2183 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2184 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2187 RtlFreeUnicodeString(&LocalNameW
);
2190 /* if localized name was unavailable */
2191 if (!NT_SUCCESS(status
))
2193 /* store English name */
2194 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfLogFont
.lfFaceName
,
2195 sizeof(Info
->EnumLogFontEx
.elfLogFont
.lfFaceName
),
2197 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2198 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2202 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2203 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2204 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2205 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2206 if (!NT_SUCCESS(status
))
2212 if (wcslen(Info
->EnumLogFontEx
.elfFullName
) +
2213 StyleW
.Length
/ sizeof(WCHAR
) + 1 <=
2214 sizeof(Info
->EnumLogFontEx
.elfFullName
))
2216 wcscat(Info
->EnumLogFontEx
.elfFullName
, L
" ");
2217 wcscat(Info
->EnumLogFontEx
.elfFullName
, StyleW
.Buffer
);
2221 Info
->EnumLogFontEx
.elfLogFont
.lfCharSet
= DEFAULT_CHARSET
;
2222 Info
->EnumLogFontEx
.elfScript
[0] = L
'\0';
2224 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2232 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2233 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2234 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2235 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2236 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2237 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2239 if (0 == pOS2
->version
)
2243 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2244 fs
.fsCsb
[0] |= FS_LATIN1
;
2246 fs
.fsCsb
[0] |= FS_SYMBOL
;
2250 if (fs
.fsCsb
[0] == 0)
2252 /* Let's see if we can find any interesting cmaps */
2253 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2255 switch (Face
->charmaps
[i
]->encoding
)
2257 case FT_ENCODING_UNICODE
:
2258 case FT_ENCODING_APPLE_ROMAN
:
2259 fs
.fsCsb
[0] |= FS_LATIN1
;
2261 case FT_ENCODING_MS_SYMBOL
:
2262 fs
.fsCsb
[0] |= FS_SYMBOL
;
2270 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2273 if (fs
.fsCsb
[0] & fs0
)
2275 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2277 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2279 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2281 Info
->EnumLogFontEx
.elfLogFont
.lfCharSet
= CharSetInfo
.ciCharset
;
2283 wcscpy(Info
->EnumLogFontEx
.elfScript
, ElfScripts
[i
]);
2286 DPRINT1("Unknown elfscript for bit %u\n", i
);
2291 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2295 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2298 UNICODE_STRING InfoFaceName
;
2300 for (i
= 0; i
< InfoEntries
; i
++)
2302 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2303 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2312 static BOOLEAN FASTCALL
2313 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2314 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2316 UNICODE_STRING LogFontFaceName
;
2318 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2319 if (0 != LogFontFaceName
.Length
&&
2320 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2325 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2328 static BOOLEAN FASTCALL
2329 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2330 PFONTFAMILYINFO Info
,
2336 PFONT_ENTRY CurrentEntry
;
2337 ANSI_STRING EntryFaceNameA
;
2338 UNICODE_STRING EntryFaceNameW
;
2342 Entry
= Head
->Flink
;
2343 while (Entry
!= Head
)
2345 CurrentEntry
= (PFONT_ENTRY
) CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2347 FontGDI
= CurrentEntry
->Font
;
2350 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
2351 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
2352 if (!NT_SUCCESS(status
))
2357 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
2359 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2360 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2363 if (FontFamilyInclude(LogFont
, &EntryFaceNameW
, Info
, min(*Count
, Size
)))
2367 FontFamilyFillInfo(Info
+ *Count
, EntryFaceNameW
.Buffer
, FontGDI
);
2371 RtlFreeUnicodeString(&EntryFaceNameW
);
2372 Entry
= Entry
->Flink
;
2378 typedef struct FontFamilyInfoCallbackContext
2381 PFONTFAMILYINFO Info
;
2384 } FONT_FAMILY_INFO_CALLBACK_CONTEXT
, *PFONT_FAMILY_INFO_CALLBACK_CONTEXT
;
2386 _Function_class_(RTL_QUERY_REGISTRY_ROUTINE
)
2387 static NTSTATUS APIENTRY
2388 FontFamilyInfoQueryRegistryCallback(IN PWSTR ValueName
, IN ULONG ValueType
,
2389 IN PVOID ValueData
, IN ULONG ValueLength
,
2390 IN PVOID Context
, IN PVOID EntryContext
)
2392 PFONT_FAMILY_INFO_CALLBACK_CONTEXT InfoContext
;
2393 UNICODE_STRING RegistryName
, RegistryValue
;
2397 if (REG_SZ
!= ValueType
)
2399 return STATUS_SUCCESS
;
2401 InfoContext
= (PFONT_FAMILY_INFO_CALLBACK_CONTEXT
) Context
;
2402 RtlInitUnicodeString(&RegistryName
, ValueName
);
2404 /* Do we need to include this font family? */
2405 if (FontFamilyInclude(InfoContext
->LogFont
, &RegistryName
, InfoContext
->Info
,
2406 min(InfoContext
->Count
, InfoContext
->Size
)))
2408 RtlInitUnicodeString(&RegistryValue
, (PCWSTR
) ValueData
);
2409 Existing
= FindFaceNameInInfo(&RegistryValue
, InfoContext
->Info
,
2410 min(InfoContext
->Count
, InfoContext
->Size
));
2413 /* We already have the information about the "real" font. Just copy it */
2414 if (InfoContext
->Count
< InfoContext
->Size
)
2416 InfoContext
->Info
[InfoContext
->Count
] = InfoContext
->Info
[Existing
];
2417 RtlStringCbCopyNW(InfoContext
->Info
[InfoContext
->Count
].EnumLogFontEx
.elfLogFont
.lfFaceName
,
2418 sizeof(InfoContext
->Info
[InfoContext
->Count
].EnumLogFontEx
.elfLogFont
.lfFaceName
),
2419 RegistryName
.Buffer
,
2420 RegistryName
.Length
);
2422 InfoContext
->Count
++;
2423 return STATUS_SUCCESS
;
2426 /* Try to find information about the "real" font */
2427 FontGDI
= FindFaceNameInLists(&RegistryValue
);
2428 if (NULL
== FontGDI
)
2430 /* "Real" font not found, discard this registry entry */
2431 return STATUS_SUCCESS
;
2434 /* Return info about the "real" font but with the name of the alias */
2435 if (InfoContext
->Count
< InfoContext
->Size
)
2437 FontFamilyFillInfo(InfoContext
->Info
+ InfoContext
->Count
,
2438 RegistryName
.Buffer
, FontGDI
);
2440 InfoContext
->Count
++;
2441 return STATUS_SUCCESS
;
2444 return STATUS_SUCCESS
;
2447 static BOOLEAN FASTCALL
2448 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2449 PFONTFAMILYINFO Info
,
2453 RTL_QUERY_REGISTRY_TABLE QueryTable
[2] = {{0}};
2454 FONT_FAMILY_INFO_CALLBACK_CONTEXT Context
;
2457 /* Enumerate font families found in HKLM\Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes
2458 The real work is done in the registry callback function */
2459 Context
.LogFont
= LogFont
;
2460 Context
.Info
= Info
;
2461 Context
.Count
= *Count
;
2462 Context
.Size
= Size
;
2464 QueryTable
[0].QueryRoutine
= FontFamilyInfoQueryRegistryCallback
;
2465 QueryTable
[0].Flags
= 0;
2466 QueryTable
[0].Name
= NULL
;
2467 QueryTable
[0].EntryContext
= NULL
;
2468 QueryTable
[0].DefaultType
= REG_NONE
;
2469 QueryTable
[0].DefaultData
= NULL
;
2470 QueryTable
[0].DefaultLength
= 0;
2472 QueryTable
[1].QueryRoutine
= NULL
;
2473 QueryTable
[1].Name
= NULL
;
2475 Status
= RtlQueryRegistryValues(RTL_REGISTRY_WINDOWS_NT
,
2480 if (NT_SUCCESS(Status
))
2482 *Count
= Context
.Count
;
2485 return NT_SUCCESS(Status
) || STATUS_OBJECT_NAME_NOT_FOUND
== Status
;
2490 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2494 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2495 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2496 lprs
->nLanguageID
= gusLanguageID
;
2499 EngSetLastError(ERROR_INVALID_PARAMETER
);
2509 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2510 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2511 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2512 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2515 FT_BitmapGlyph APIENTRY
2522 PLIST_ENTRY CurrentEntry
;
2523 PFONT_CACHE_ENTRY FontEntry
;
2525 ASSERT_FREETYPE_LOCK_HELD();
2527 CurrentEntry
= FontCacheListHead
.Flink
;
2528 while (CurrentEntry
!= &FontCacheListHead
)
2530 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2531 if ((FontEntry
->Face
== Face
) &&
2532 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2533 (FontEntry
->Height
== Height
) &&
2534 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2536 CurrentEntry
= CurrentEntry
->Flink
;
2539 if (CurrentEntry
== &FontCacheListHead
)
2544 RemoveEntryList(CurrentEntry
);
2545 InsertHeadList(&FontCacheListHead
, CurrentEntry
);
2546 return FontEntry
->BitmapGlyph
;
2550 FT_BitmapGlyph APIENTRY
2553 FT_GlyphSlot GlyphSlot
,
2554 FT_Render_Mode RenderMode
)
2558 FT_Bitmap AlignedBitmap
;
2559 FT_BitmapGlyph BitmapGlyph
;
2561 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2564 DPRINT1("Failure getting glyph.\n");
2568 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2571 FT_Done_Glyph(Glyph
);
2572 DPRINT1("Failure rendering glyph.\n");
2576 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2577 FT_Bitmap_New(&AlignedBitmap
);
2578 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2580 DPRINT1("Conversion failed\n");
2581 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2585 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2586 BitmapGlyph
->bitmap
= AlignedBitmap
;
2591 FT_BitmapGlyph APIENTRY
2597 FT_GlyphSlot GlyphSlot
,
2598 FT_Render_Mode RenderMode
)
2602 PFONT_CACHE_ENTRY NewEntry
;
2603 FT_Bitmap AlignedBitmap
;
2604 FT_BitmapGlyph BitmapGlyph
;
2606 ASSERT_FREETYPE_LOCK_HELD();
2608 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2611 DPRINT1("Failure caching glyph.\n");
2615 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2618 FT_Done_Glyph(GlyphCopy
);
2619 DPRINT1("Failure rendering glyph.\n");
2623 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2626 DPRINT1("Alloc failure caching glyph.\n");
2627 FT_Done_Glyph(GlyphCopy
);
2631 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2632 FT_Bitmap_New(&AlignedBitmap
);
2633 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2635 DPRINT1("Conversion failed\n");
2636 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2637 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2641 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2642 BitmapGlyph
->bitmap
= AlignedBitmap
;
2644 NewEntry
->GlyphIndex
= GlyphIndex
;
2645 NewEntry
->Face
= Face
;
2646 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2647 NewEntry
->Height
= Height
;
2648 NewEntry
->mxWorldToDevice
= *pmx
;
2650 InsertHeadList(&FontCacheListHead
, &NewEntry
->ListEntry
);
2651 if (++FontCacheNumEntries
> MAX_FONT_CACHE
)
2653 NewEntry
= CONTAINING_RECORD(FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2654 RemoveCachedEntry(NewEntry
);
2661 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2663 pt
->x
.value
= vec
->x
>> 6;
2664 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2665 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2666 pt
->y
.value
= vec
->y
>> 6;
2667 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2668 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2672 This function builds an FT_Fixed from a float. It puts the integer part
2673 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2674 It fails if the integer part of the float number is greater than SHORT_MAX.
2676 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2679 unsigned short fract
= (f
- value
) * 0xFFFF;
2680 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2684 This function builds an FT_Fixed from a FIXED. It simply put f.value
2685 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2687 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2689 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2692 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2694 TTPOLYGONHEADER
*pph
;
2696 int needed
= 0, point
= 0, contour
, first_pt
;
2697 unsigned int pph_start
, cpfx
;
2700 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2702 /* Ignore contours containing one point */
2703 if (point
== outline
->contours
[contour
])
2710 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2714 pph
->dwType
= TT_POLYGON_TYPE
;
2715 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2717 needed
+= sizeof(*pph
);
2719 while (point
<= outline
->contours
[contour
])
2721 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2722 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2723 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2728 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2731 } while (point
<= outline
->contours
[contour
] &&
2732 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2733 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2734 /* At the end of a contour Windows adds the start point, but
2736 if (point
> outline
->contours
[contour
] &&
2737 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2740 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2743 else if (point
<= outline
->contours
[contour
] &&
2744 outline
->tags
[point
] & FT_Curve_Tag_On
)
2746 /* add closing pt for bezier */
2748 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2757 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2760 pph
->cb
= needed
- pph_start
;
2765 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2767 /* Convert the quadratic Beziers to cubic Beziers.
2768 The parametric eqn for a cubic Bezier is, from PLRM:
2769 r(t) = at^3 + bt^2 + ct + r0
2770 with the control points:
2775 A quadratic Bezier has the form:
2776 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2778 So equating powers of t leads to:
2779 r1 = 2/3 p1 + 1/3 p0
2780 r2 = 2/3 p1 + 1/3 p2
2781 and of course r0 = p0, r3 = p2
2783 int contour
, point
= 0, first_pt
;
2784 TTPOLYGONHEADER
*pph
;
2786 DWORD pph_start
, cpfx
, type
;
2787 FT_Vector cubic_control
[4];
2788 unsigned int needed
= 0;
2790 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2793 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2797 pph
->dwType
= TT_POLYGON_TYPE
;
2798 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2800 needed
+= sizeof(*pph
);
2802 while (point
<= outline
->contours
[contour
])
2804 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2805 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2806 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2810 if (type
== TT_PRIM_LINE
)
2813 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2819 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2822 /* FIXME: Possible optimization in endpoint calculation
2823 if there are two consecutive curves */
2824 cubic_control
[0] = outline
->points
[point
-1];
2825 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2827 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2828 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2829 cubic_control
[0].x
>>= 1;
2830 cubic_control
[0].y
>>= 1;
2832 if (point
+1 > outline
->contours
[contour
])
2833 cubic_control
[3] = outline
->points
[first_pt
];
2836 cubic_control
[3] = outline
->points
[point
+1];
2837 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2839 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2840 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2841 cubic_control
[3].x
>>= 1;
2842 cubic_control
[3].y
>>= 1;
2845 /* r1 = 1/3 p0 + 2/3 p1
2846 r2 = 1/3 p2 + 2/3 p1 */
2847 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2848 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2849 cubic_control
[2] = cubic_control
[1];
2850 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2851 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2852 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2853 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2856 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2857 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2858 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
2863 } while (point
<= outline
->contours
[contour
] &&
2864 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2865 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2866 /* At the end of a contour Windows adds the start point,
2867 but only for Beziers and we've already done that.
2869 if (point
<= outline
->contours
[contour
] &&
2870 outline
->tags
[point
] & FT_Curve_Tag_On
)
2872 /* This is the closing pt of a bezier, but we've already
2873 added it, so just inc point and carry on */
2881 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2884 pph
->cb
= needed
- pph_start
;
2890 IntRequestFontSize(PDC dc
, FT_Face face
, LONG Width
, LONG Height
)
2892 FT_Size_RequestRec req
;
2903 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
2913 if (Width
> 0xFFFFU
)
2915 if (Height
> 0xFFFFU
)
2918 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
2919 req
.width
= (FT_Long
)(Width
<< 6);
2920 req
.height
= (FT_Long
)(Height
<< 6);
2921 req
.horiResolution
= 0;
2922 req
.vertResolution
= 0;
2923 return FT_Request_Size(face
, &req
);
2928 TextIntUpdateSize(PDC dc
,
2935 FT_CharMap charmap
, found
;
2941 face
= FontGDI
->SharedFace
->Face
;
2942 if (face
->charmap
== NULL
)
2944 DPRINT("WARNING: No charmap selected!\n");
2945 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
2948 for (n
= 0; n
< face
->num_charmaps
; n
++)
2950 charmap
= face
->charmaps
[n
];
2951 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
2952 if (charmap
->encoding
!= 0)
2960 DPRINT1("WARNING: Could not find desired charmap!\n");
2964 error
= FT_Set_Charmap(face
, found
);
2967 DPRINT1("WARNING: Could not set the charmap!\n");
2972 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
2974 error
= IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
2981 DPRINT1("Error in setting pixel sizes: %d\n", error
);
2990 * Based on WineEngGetGlyphOutline
2995 ftGdiGetGlyphOutline(
3003 BOOL bIgnoreRotation
)
3005 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3013 FT_UInt glyph_index
;
3014 DWORD width
, height
, pitch
, needed
= 0;
3015 FT_Bitmap ft_bitmap
;
3017 INT left
, right
, top
= 0, bottom
= 0;
3019 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3020 FLOAT eM11
, widthRatio
= 1.0;
3021 FT_Matrix transMat
= identityMat
;
3022 BOOL needsTransform
= FALSE
;
3025 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3026 OUTLINETEXTMETRICW
*potm
;
3030 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3031 cjBuf
, pvBuf
, pmat2
);
3033 pdcattr
= dc
->pdcattr
;
3035 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3038 hFont
= pdcattr
->hlfntNew
;
3039 TextObj
= RealizeFontInit(hFont
);
3043 EngSetLastError(ERROR_INVALID_HANDLE
);
3046 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3047 ft_face
= FontGDI
->SharedFace
->Face
;
3049 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3050 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3051 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3053 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3054 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3057 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3058 TEXTOBJ_UnlockText(TextObj
);
3061 IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3064 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3065 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3067 TEXTOBJ_UnlockText(TextObj
);
3069 if (iFormat
& GGO_GLYPH_INDEX
)
3072 iFormat
&= ~GGO_GLYPH_INDEX
;
3074 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3076 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3077 load_flags
|= FT_LOAD_NO_BITMAP
;
3079 if (iFormat
& GGO_UNHINTED
)
3081 load_flags
|= FT_LOAD_NO_HINTING
;
3082 iFormat
&= ~GGO_UNHINTED
;
3085 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3088 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3090 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3095 if (aveWidth
&& potm
)
3097 widthRatio
= (FLOAT
)aveWidth
* eM11
/
3098 (FLOAT
) potm
->otmTextMetrics
.tmAveCharWidth
;
3101 left
= (INT
)(ft_face
->glyph
->metrics
.horiBearingX
* widthRatio
) & -64;
3102 right
= (INT
)((ft_face
->glyph
->metrics
.horiBearingX
+
3103 ft_face
->glyph
->metrics
.width
) * widthRatio
+ 63) & -64;
3105 adv
= (INT
)((ft_face
->glyph
->metrics
.horiAdvance
* widthRatio
) + 63) >> 6;
3107 bbx
= (right
- left
) >> 6;
3109 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3113 /* Scaling transform */
3120 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3122 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3123 efTemp
= pmx
->efM11
;
3124 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3125 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
3127 efTemp
= pmx
->efM12
;
3128 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3129 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
3131 efTemp
= pmx
->efM21
;
3132 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3133 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
3135 efTemp
= pmx
->efM22
;
3136 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3137 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);