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");
57 static UNICODE_STRING FontRegPath
=
58 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
61 /* The FreeType library is not thread safe, so we have
62 to serialize access to it */
63 static PFAST_MUTEX FreeTypeLock
;
65 static LIST_ENTRY FontListHead
;
66 static PFAST_MUTEX FontListLock
;
67 static BOOL RenderingEnabled
= TRUE
;
69 #define IntLockGlobalFonts \
70 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FontListLock)
72 #define IntUnLockGlobalFonts \
73 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FontListLock)
75 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
76 ASSERT(FontListLock->Owner == KeGetCurrentThread())
78 #define IntLockFreeType \
79 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FreeTypeLock)
81 #define IntUnLockFreeType \
82 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FreeTypeLock)
84 #define ASSERT_FREETYPE_LOCK_HELD() \
85 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
87 #define ASSERT_FREETYPE_LOCK_NOT_HELD() \
88 ASSERT(FreeTypeLock->Owner != KeGetCurrentThread())
90 #define MAX_FONT_CACHE 256
92 static LIST_ENTRY FontCacheListHead
;
93 static UINT FontCacheNumEntries
;
95 static PWCHAR ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
105 L
"Vietnamese", /* 08 */
106 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
114 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
119 * For TranslateCharsetInfo
122 #define MAXTCIINDEX 32
123 static const CHARSETINFO FontTci
[MAXTCIINDEX
] =
126 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
127 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
128 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
129 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
130 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
131 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
132 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
133 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
134 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
135 /* reserved by ANSI */
136 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
137 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
138 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
139 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
140 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
141 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
142 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
144 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
145 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
146 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
147 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
148 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
149 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
150 /* Reserved for alternate ANSI and OEM */
151 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
152 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
153 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
154 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
155 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
156 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
157 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
158 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
159 /* Reserved for system */
160 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
161 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
165 static RTL_STATIC_LIST_HEAD(FontSubstListHead
);
168 SharedMem_AddRef(PSHARED_MEM Ptr
)
170 ASSERT_FREETYPE_LOCK_HELD();
176 SharedFaceCache_Init(PSHARED_FACE_CACHE Cache
)
178 Cache
->OutlineRequiredSize
= 0;
179 RtlInitUnicodeString(&Cache
->FontFamily
, NULL
);
180 RtlInitUnicodeString(&Cache
->FullName
, NULL
);
184 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
187 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
192 Ptr
->Memory
= Memory
;
193 SharedFaceCache_Init(&Ptr
->EnglishUS
);
194 SharedFaceCache_Init(&Ptr
->UserLanguage
);
196 SharedMem_AddRef(Memory
);
197 DPRINT("Creating SharedFace for %s\n", Face
->family_name
);
203 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
206 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
209 Ptr
->Buffer
= Buffer
;
210 Ptr
->BufferSize
= BufferSize
;
212 Ptr
->IsMapping
= IsMapping
;
213 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
219 SharedFace_AddRef(PSHARED_FACE Ptr
)
221 ASSERT_FREETYPE_LOCK_HELD();
227 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
229 ASSERT_FREETYPE_LOCK_HELD();
231 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
232 RemoveEntryList(&Entry
->ListEntry
);
233 ExFreePoolWithTag(Entry
, TAG_FONT
);
234 FontCacheNumEntries
--;
235 ASSERT(FontCacheNumEntries
<= MAX_FONT_CACHE
);
239 RemoveCacheEntries(FT_Face Face
)
241 PLIST_ENTRY CurrentEntry
;
242 PFONT_CACHE_ENTRY FontEntry
;
244 ASSERT_FREETYPE_LOCK_HELD();
246 CurrentEntry
= FontCacheListHead
.Flink
;
247 while (CurrentEntry
!= &FontCacheListHead
)
249 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
250 CurrentEntry
= CurrentEntry
->Flink
;
252 if (FontEntry
->Face
== Face
)
254 RemoveCachedEntry(FontEntry
);
259 static void SharedMem_Release(PSHARED_MEM Ptr
)
261 ASSERT_FREETYPE_LOCK_HELD();
262 ASSERT(Ptr
->RefCount
> 0);
264 if (Ptr
->RefCount
<= 0)
268 if (Ptr
->RefCount
== 0)
270 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
272 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
274 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
275 ExFreePoolWithTag(Ptr
, TAG_FONT
);
280 SharedFaceCache_Release(PSHARED_FACE_CACHE Cache
)
282 RtlFreeUnicodeString(&Cache
->FontFamily
);
283 RtlFreeUnicodeString(&Cache
->FullName
);
287 SharedFace_Release(PSHARED_FACE Ptr
)
290 ASSERT(Ptr
->RefCount
> 0);
292 if (Ptr
->RefCount
<= 0)
296 if (Ptr
->RefCount
== 0)
298 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
);
299 RemoveCacheEntries(Ptr
->Face
);
300 FT_Done_Face(Ptr
->Face
);
301 SharedMem_Release(Ptr
->Memory
);
302 SharedFaceCache_Release(&Ptr
->EnglishUS
);
303 SharedFaceCache_Release(&Ptr
->UserLanguage
);
304 ExFreePoolWithTag(Ptr
, TAG_FONT
);
311 * IntLoadFontSubstList --- loads the list of font substitutes
314 IntLoadFontSubstList(PLIST_ENTRY pHead
)
318 OBJECT_ATTRIBUTES ObjectAttributes
;
319 KEY_FULL_INFORMATION KeyFullInfo
;
321 UNICODE_STRING FromW
, ToW
;
322 BYTE InfoBuffer
[128];
323 PKEY_VALUE_FULL_INFORMATION pInfo
;
324 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
326 PFONTSUBST_ENTRY pEntry
;
328 /* the FontSubstitutes registry key */
329 static UNICODE_STRING FontSubstKey
=
330 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
331 L
"Microsoft\\Windows NT\\CurrentVersion\\"
334 /* open registry key */
335 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
336 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
338 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
339 if (!NT_SUCCESS(Status
))
341 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
342 return FALSE
; /* failure */
345 /* query count of values */
346 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
347 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
348 if (!NT_SUCCESS(Status
))
350 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
352 return FALSE
; /* failure */
356 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
359 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
360 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
361 if (!NT_SUCCESS(Status
))
363 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
367 /* create FromW string */
368 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
369 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
370 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
371 Status
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
372 if (!NT_SUCCESS(Status
))
374 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
379 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
380 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
381 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
382 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
384 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
385 RtlFreeUnicodeString(&FromW
);
389 /* create ToW string */
390 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
391 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
392 pch
[Length
] = UNICODE_NULL
; /* truncate */
393 Status
= RtlCreateUnicodeString(&ToW
, pch
);
394 if (!NT_SUCCESS(Status
))
396 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
397 RtlFreeUnicodeString(&FromW
);
401 /* does charset exist? (from) */
402 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
403 pch
= wcsrchr(FromW
.Buffer
, L
',');
408 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
409 /* parse charset number */
410 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
413 /* does charset exist? (to) */
414 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
415 pch
= wcsrchr(ToW
.Buffer
, L
',');
420 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
421 /* parse charset number */
422 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
425 /* allocate an entry */
426 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
429 DPRINT("ExAllocatePoolWithTag failed\n");
430 RtlFreeUnicodeString(&FromW
);
431 RtlFreeUnicodeString(&ToW
);
435 /* store to *pEntry */
436 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
437 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
438 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
439 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
441 /* insert pEntry to *pHead */
442 InsertTailList(pHead
, &pEntry
->ListEntry
);
448 return NT_SUCCESS(Status
);
452 InitFontSupport(VOID
)
456 InitializeListHead(&FontListHead
);
457 InitializeListHead(&FontCacheListHead
);
458 FontCacheNumEntries
= 0;
459 /* Fast Mutexes must be allocated from non paged pool */
460 FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
461 if (FontListLock
== NULL
)
466 ExInitializeFastMutex(FontListLock
);
467 FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
468 if (FreeTypeLock
== NULL
)
472 ExInitializeFastMutex(FreeTypeLock
);
474 ulError
= FT_Init_FreeType(&library
);
477 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
481 IntLoadSystemFonts();
482 IntLoadFontSubstList(&FontSubstListHead
);
488 FtSetCoordinateTransform(
495 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
497 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
498 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
501 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
502 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
505 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
506 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
509 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
510 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
512 /* Set the transformation matrix */
513 FT_Set_Transform(face
, &ftmatrix
, 0);
517 SubstituteFontByList(PLIST_ENTRY pHead
,
518 PUNICODE_STRING pOutputName
,
519 PUNICODE_STRING pInputName
,
520 BYTE RequestedCharSet
,
521 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
523 PLIST_ENTRY pListEntry
;
524 PFONTSUBST_ENTRY pSubstEntry
;
525 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
527 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
528 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
530 /* for each list entry */
531 for (pListEntry
= pHead
->Flink
;
533 pListEntry
= pListEntry
->Flink
)
536 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
538 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
540 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
541 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
543 continue; /* not matched */
546 /* does charset number exist? (to) */
547 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
549 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
553 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
556 /* does font name match? */
557 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
560 continue; /* not matched */
563 /* update *pOutputName */
564 *pOutputName
= pSubstEntry
->FontNames
[FONTSUBST_TO
];
566 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
568 /* update CharSetMap */
569 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
570 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
572 return TRUE
; /* success */
579 SubstituteFontRecurse(LOGFONTW
* pLogFont
)
581 UINT RecurseCount
= 5;
582 UNICODE_STRING OutputNameW
= { 0 };
583 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
585 UNICODE_STRING InputNameW
;
587 if (pLogFont
->lfFaceName
[0] == UNICODE_NULL
)
590 RtlInitUnicodeString(&InputNameW
, pLogFont
->lfFaceName
);
592 while (RecurseCount
-- > 0)
594 Found
= SubstituteFontByList(&FontSubstListHead
,
595 &OutputNameW
, &InputNameW
,
596 pLogFont
->lfCharSet
, CharSetMap
);
600 RtlStringCchCopyW(pLogFont
->lfFaceName
, LF_FACESIZE
, OutputNameW
.Buffer
);
602 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
603 CharSetMap
[FONTSUBST_FROM
] == pLogFont
->lfCharSet
)
605 pLogFont
->lfCharSet
= CharSetMap
[FONTSUBST_TO
];
609 return TRUE
; /* success */
615 * Search the system font directory and adds each font found.
618 IntLoadSystemFonts(VOID
)
620 OBJECT_ATTRIBUTES ObjectAttributes
;
621 UNICODE_STRING Directory
, FileName
, TempString
;
622 IO_STATUS_BLOCK Iosb
;
625 PFILE_DIRECTORY_INFORMATION DirInfo
;
626 BOOLEAN bRestartScan
= TRUE
;
629 static UNICODE_STRING SearchPatterns
[] =
631 RTL_CONSTANT_STRING(L
"*.ttf"),
632 RTL_CONSTANT_STRING(L
"*.ttc"),
633 RTL_CONSTANT_STRING(L
"*.otf"),
634 RTL_CONSTANT_STRING(L
"*.otc"),
635 RTL_CONSTANT_STRING(L
"*.fon"),
636 RTL_CONSTANT_STRING(L
"*.fnt")
639 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
641 InitializeObjectAttributes(
644 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
650 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
653 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
654 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
656 if (NT_SUCCESS(Status
))
658 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
660 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
661 if (DirInfoBuffer
== NULL
)
667 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
668 if (FileName
.Buffer
== NULL
)
670 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
675 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
679 Status
= ZwQueryDirectoryFile(
687 FileDirectoryInformation
,
692 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
697 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
700 TempString
.Buffer
= DirInfo
->FileName
;
702 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
703 RtlCopyUnicodeString(&FileName
, &Directory
);
704 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
705 IntGdiAddFontResource(&FileName
, 0);
706 if (DirInfo
->NextEntryOffset
== 0)
708 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
711 bRestartScan
= FALSE
;
714 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
715 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
722 ItalicFromStyle(const char *style_name
)
724 if (style_name
== NULL
|| style_name
[0] == 0)
726 if (strstr(style_name
, "Italic") != NULL
)
728 if (strstr(style_name
, "Oblique") != NULL
)
734 WeightFromStyle(const char *style_name
)
736 if (style_name
== NULL
|| style_name
[0] == 0)
738 if (strstr(style_name
, "Regular") != NULL
)
740 if (strstr(style_name
, "Normal") != NULL
)
742 if (strstr(style_name
, "SemiBold") != NULL
)
744 if (strstr(style_name
, "UltraBold") != NULL
)
746 if (strstr(style_name
, "DemiBold") != NULL
)
748 if (strstr(style_name
, "ExtraBold") != NULL
)
750 if (strstr(style_name
, "Bold") != NULL
)
752 if (strstr(style_name
, "UltraLight") != NULL
)
753 return FW_ULTRALIGHT
;
754 if (strstr(style_name
, "ExtraLight") != NULL
)
755 return FW_EXTRALIGHT
;
756 if (strstr(style_name
, "Light") != NULL
)
758 if (strstr(style_name
, "Hairline") != NULL
)
760 if (strstr(style_name
, "Book") != NULL
)
762 if (strstr(style_name
, "ExtraBlack") != NULL
)
764 if (strstr(style_name
, "UltraBlack") != NULL
)
766 if (strstr(style_name
, "Black") != NULL
)
768 if (strstr(style_name
, "Medium") != NULL
)
770 if (strstr(style_name
, "Thin") != NULL
)
772 if (strstr(style_name
, "Heavy") != NULL
)
778 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
779 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
783 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
787 ANSI_STRING AnsiFaceName
;
788 FT_WinFNT_HeaderRec WinFNT
;
789 INT FaceCount
= 0, CharSetCount
= 0;
790 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
791 DWORD Characteristics
= pLoadFont
->Characteristics
;
792 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
795 FT_UShort os2_version
;
796 FT_ULong os2_ulCodePageRange1
;
797 FT_UShort os2_usWeightClass
;
799 if (SharedFace
== NULL
&& CharSetIndex
== -1)
801 /* load a face from memory */
803 Error
= FT_New_Memory_Face(
805 pLoadFont
->Memory
->Buffer
,
806 pLoadFont
->Memory
->BufferSize
,
807 ((FontIndex
!= -1) ? FontIndex
: 0),
811 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
815 if (!Error
&& FT_IS_SFNT(Face
))
816 pLoadFont
->IsTrueType
= TRUE
;
818 if (Error
|| SharedFace
== NULL
)
821 SharedFace_Release(SharedFace
);
823 if (Error
== FT_Err_Unknown_File_Format
)
824 DPRINT1("Unknown font file format\n");
826 DPRINT1("Error reading font (error code: %d)\n", Error
);
827 return 0; /* failure */
832 Face
= SharedFace
->Face
;
834 SharedFace_AddRef(SharedFace
);
838 /* allocate a FONT_ENTRY */
839 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
842 SharedFace_Release(SharedFace
);
843 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
844 return 0; /* failure */
847 /* allocate a FONTGDI */
848 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
851 SharedFace_Release(SharedFace
);
852 ExFreePoolWithTag(Entry
, TAG_FONT
);
853 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
854 return 0; /* failure */
860 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
861 pFileName
->Length
+ sizeof(UNICODE_NULL
),
863 if (FontGDI
->Filename
== NULL
)
866 SharedFace_Release(SharedFace
);
867 ExFreePoolWithTag(Entry
, TAG_FONT
);
868 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
869 return 0; /* failure */
871 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
872 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
876 FontGDI
->Filename
= NULL
;
878 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
881 if (FontGDI
->Filename
)
882 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
884 SharedFace_Release(SharedFace
);
885 ExFreePoolWithTag(Entry
, TAG_FONT
);
889 PrivateEntry
->Entry
= Entry
;
890 if (pLoadFont
->PrivateEntry
)
892 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
896 InitializeListHead(&PrivateEntry
->ListEntry
);
897 pLoadFont
->PrivateEntry
= PrivateEntry
;
902 FontGDI
->SharedFace
= SharedFace
;
903 FontGDI
->CharSet
= ANSI_CHARSET
;
904 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
905 FontGDI
->RequestItalic
= FALSE
;
906 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
907 FontGDI
->RequestWeight
= FW_NORMAL
;
909 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
910 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
911 if (!NT_SUCCESS(Status
))
915 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
917 pLoadFont
->PrivateEntry
= NULL
;
921 RemoveEntryList(&PrivateEntry
->ListEntry
);
923 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
925 if (FontGDI
->Filename
)
926 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
928 SharedFace_Release(SharedFace
);
929 ExFreePoolWithTag(Entry
, TAG_FONT
);
935 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
938 os2_version
= pOS2
->version
;
939 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
940 os2_usWeightClass
= pOS2
->usWeightClass
;
944 if (pOS2
&& os2_version
>= 1)
946 /* get charset and weight from OS/2 header */
948 /* Make sure we do not use this pointer anymore */
951 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
953 if (os2_ulCodePageRange1
& (1 << BitIndex
))
955 if (FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
958 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
959 CharSetIndex
== CharSetCount
)
961 FontGDI
->CharSet
= FontTci
[BitIndex
].ciCharset
;
968 /* set actual weight */
969 FontGDI
->OriginalWeight
= os2_usWeightClass
;
973 /* get charset from WinFNT header */
975 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
978 FontGDI
->CharSet
= WinFNT
.charset
;
983 /* FIXME: CharSet is invalid on Marlett */
984 if (RtlEqualUnicodeString(&Entry
->FaceName
, &MarlettW
, TRUE
))
986 FontGDI
->CharSet
= SYMBOL_CHARSET
;
990 DPRINT("Font loaded: %s (%s)\n", Face
->family_name
, Face
->style_name
);
991 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
992 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
994 /* Add this font resource to the font table */
995 Entry
->Font
= FontGDI
;
996 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
998 if (Characteristics
& FR_PRIVATE
)
1001 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1002 IntLockProcessPrivateFonts(Win32Process
);
1003 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
1004 IntUnLockProcessPrivateFonts(Win32Process
);
1010 InsertTailList(&FontListHead
, &Entry
->ListEntry
);
1011 IntUnLockGlobalFonts
;
1014 if (FontIndex
== -1)
1016 if (FT_IS_SFNT(Face
))
1018 TT_Face TrueType
= (TT_Face
)Face
;
1019 if (TrueType
->ttc_header
.count
> 1)
1022 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1024 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1031 if (CharSetIndex
== -1)
1035 if (pLoadFont
->RegValueName
.Length
== 0)
1037 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1041 UNICODE_STRING NewString
;
1042 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1043 NewString
.Length
= 0;
1044 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1045 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1046 NewString
.MaximumLength
,
1048 NewString
.Buffer
[0] = UNICODE_NULL
;
1050 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1051 RtlAppendUnicodeToString(&NewString
, L
" & ");
1052 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1054 RtlFreeUnicodeString(pValueName
);
1055 *pValueName
= NewString
;
1058 for (i
= 1; i
< CharSetCount
; ++i
)
1060 /* Do not count charsets towards 'faces' loaded */
1061 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1065 return FaceCount
; /* number of loaded faces */
1069 * IntGdiAddFontResource
1071 * Adds the font resource from the specified file to the system.
1075 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1079 PVOID Buffer
= NULL
;
1080 IO_STATUS_BLOCK Iosb
;
1081 PVOID SectionObject
;
1083 LARGE_INTEGER SectionSize
;
1084 OBJECT_ATTRIBUTES ObjectAttributes
;
1085 GDI_LOAD_FONT LoadFont
;
1088 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1090 /* Open the font file */
1091 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1092 Status
= ZwOpenFile(
1094 FILE_GENERIC_READ
| SYNCHRONIZE
,
1098 FILE_SYNCHRONOUS_IO_NONALERT
);
1099 if (!NT_SUCCESS(Status
))
1101 DPRINT("Could not load font file: %wZ\n", FileName
);
1105 SectionSize
.QuadPart
= 0LL;
1106 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1107 NULL
, &SectionSize
, PAGE_READONLY
,
1108 SEC_COMMIT
, FileHandle
, NULL
);
1109 if (!NT_SUCCESS(Status
))
1111 DPRINT("Could not map file: %wZ\n", FileName
);
1112 ZwClose(FileHandle
);
1115 ZwClose(FileHandle
);
1117 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1118 if (!NT_SUCCESS(Status
))
1120 DPRINT("Could not map file: %wZ\n", FileName
);
1121 ObDereferenceObject(SectionObject
);
1125 LoadFont
.pFileName
= FileName
;
1126 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1127 LoadFont
.Characteristics
= Characteristics
;
1128 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1129 LoadFont
.IsTrueType
= FALSE
;
1130 LoadFont
.PrivateEntry
= NULL
;
1131 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1133 ObDereferenceObject(SectionObject
);
1135 /* Release our copy */
1137 SharedMem_Release(LoadFont
.Memory
);
1142 if (LoadFont
.IsTrueType
)
1144 /* append " (TrueType)" */
1145 UNICODE_STRING NewString
;
1148 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1149 NewString
.Length
= 0;
1150 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1151 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1152 NewString
.MaximumLength
,
1154 NewString
.Buffer
[0] = UNICODE_NULL
;
1156 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1157 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1158 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1159 LoadFont
.RegValueName
= NewString
;
1163 InitializeObjectAttributes(&ObjectAttributes
, &FontRegPath
,
1164 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1166 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1167 if (NT_SUCCESS(Status
))
1170 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1174 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1175 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1176 pFileName
, DataSize
);
1181 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1187 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1189 GDI_LOAD_FONT LoadFont
;
1190 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1194 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1201 memcpy(BufferCopy
, Buffer
, dwSize
);
1203 LoadFont
.pFileName
= NULL
;
1204 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1205 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1206 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1207 LoadFont
.IsTrueType
= FALSE
;
1208 LoadFont
.PrivateEntry
= NULL
;
1209 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1211 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1213 /* Release our copy */
1215 SharedMem_Release(LoadFont
.Memory
);
1220 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1221 if (EntryCollection
)
1223 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1224 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1225 IntLockProcessPrivateFonts(Win32Process
);
1226 EntryCollection
->Handle
= ++Win32Process
->PrivateMemFontHandleCount
;
1227 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1228 IntUnLockProcessPrivateFonts(Win32Process
);
1229 Ret
= (HANDLE
)EntryCollection
->Handle
;
1232 *pNumAdded
= FaceCount
;
1237 // FIXME: Add RemoveFontResource
1239 static VOID FASTCALL
1240 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1242 PFONTGDI FontGDI
= FontEntry
->Font
;
1243 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1245 if (FontGDI
->Filename
)
1246 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1248 EngFreeMem(FontGDI
);
1249 SharedFace_Release(SharedFace
);
1250 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1254 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1257 PFONT_ENTRY_MEM FontEntry
;
1259 while (!IsListEmpty(&Head
->ListEntry
))
1261 Entry
= RemoveHeadList(&Head
->ListEntry
);
1262 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1264 CleanupFontEntry(FontEntry
->Entry
);
1265 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1268 CleanupFontEntry(Head
->Entry
);
1269 ExFreePoolWithTag(Head
, TAG_FONT
);
1272 static VOID FASTCALL
1273 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1275 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1276 PLIST_ENTRY ListEntry
;
1277 RemoveEntryList(&Collection
->ListEntry
);
1280 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1281 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1283 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1284 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1286 } while (FontMemEntry
!= Collection
->Entry
);
1290 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1293 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1294 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1295 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1297 IntLockProcessPrivateFonts(Win32Process
);
1298 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1299 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1301 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1303 if (CurrentEntry
->Handle
== (UINT
)hMMFont
)
1305 EntryCollection
= CurrentEntry
;
1306 UnlinkFontMemCollection(CurrentEntry
);
1310 Entry
= Entry
->Flink
;
1312 IntUnLockProcessPrivateFonts(Win32Process
);
1314 if (EntryCollection
)
1316 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1317 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1325 IntGdiCleanupPrivateFontsForProcess(VOID
)
1327 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1329 PFONT_ENTRY_COLL_MEM EntryCollection
;
1331 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1334 EntryCollection
= NULL
;
1336 IntLockProcessPrivateFonts(Win32Process
);
1337 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1339 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1340 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1341 UnlinkFontMemCollection(EntryCollection
);
1343 IntUnLockProcessPrivateFonts(Win32Process
);
1345 if (EntryCollection
)
1347 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1348 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1352 /* No Mem fonts anymore, see if we have any other private fonts left */
1354 IntLockProcessPrivateFonts(Win32Process
);
1355 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1357 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1359 IntUnLockProcessPrivateFonts(Win32Process
);
1363 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1371 IntIsFontRenderingEnabled(VOID
)
1373 BOOL Ret
= RenderingEnabled
;
1376 hDC
= IntGetScreenDC();
1378 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && RenderingEnabled
;
1384 IntEnableFontRendering(BOOL Enable
)
1386 RenderingEnabled
= Enable
;
1389 FT_Render_Mode FASTCALL
1390 IntGetFontRenderMode(LOGFONTW
*logfont
)
1392 switch (logfont
->lfQuality
)
1394 case ANTIALIASED_QUALITY
:
1395 case NONANTIALIASED_QUALITY
:
1396 return FT_RENDER_MODE_MONO
;
1398 return FT_RENDER_MODE_LIGHT
;
1399 /* case CLEARTYPE_QUALITY:
1400 return FT_RENDER_MODE_LCD; */
1402 return FT_RENDER_MODE_NORMAL
;
1407 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1412 plfont
= LFONT_AllocFontWithHandle();
1415 return STATUS_NO_MEMORY
;
1418 ExInitializePushLock(&plfont
->lock
);
1419 *NewFont
= plfont
->BaseObject
.hHmgr
;
1420 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1421 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1422 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1424 /* This should really depend on whether GM_ADVANCED is set */
1425 plf
->lfOrientation
= plf
->lfEscapement
;
1427 LFONT_UnlockFont(plfont
);
1429 return STATUS_SUCCESS
;
1432 /*************************************************************************
1433 * TranslateCharsetInfo
1435 * Fills a CHARSETINFO structure for a character set, code page, or
1436 * font. This allows making the correspondance between different labelings
1437 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1438 * of the same encoding.
1440 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1441 * only one codepage should be set in *Src.
1444 * TRUE on success, FALSE on failure.
1447 static BOOLEAN APIENTRY
1448 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1449 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1450 if flags == TCI_SRCCHARSET: a character set value
1451 if flags == TCI_SRCCODEPAGE: a code page value */
1452 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1453 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1459 case TCI_SRCFONTSIG
:
1460 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1465 case TCI_SRCCODEPAGE
:
1466 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciACP
)
1471 case TCI_SRCCHARSET
:
1472 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciCharset
)
1481 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== FontTci
[Index
].ciCharset
)
1486 RtlCopyMemory(Cs
, &FontTci
[Index
], sizeof(CHARSETINFO
));
1492 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1496 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1498 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1505 static void FASTCALL
1506 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1507 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1508 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1510 FT_Fixed XScale
, YScale
;
1511 int Ascent
, Descent
;
1512 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1514 XScale
= Face
->size
->metrics
.x_scale
;
1515 YScale
= Face
->size
->metrics
.y_scale
;
1519 TM
->tmHeight
= pFNT
->pixel_height
;
1520 TM
->tmAscent
= pFNT
->ascent
;
1521 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1522 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1523 TM
->tmExternalLeading
= pFNT
->external_leading
;
1524 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1525 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1527 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1528 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1529 TM
->tmFirstChar
= pFNT
->first_char
;
1530 TM
->tmLastChar
= pFNT
->last_char
;
1531 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1532 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1533 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1536 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1537 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1538 TM
->tmUnderlined
= pFNT
->underline
;
1539 TM
->tmStruckOut
= pFNT
->strike_out
;
1540 TM
->tmCharSet
= pFNT
->charset
;
1544 TM
->tmWeight
= FontGDI
->RequestWeight
;
1545 TM
->tmItalic
= FontGDI
->RequestItalic
;
1546 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1547 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1548 TM
->tmCharSet
= FontGDI
->CharSet
;
1553 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1555 Ascent
= pHori
->Ascender
;
1556 Descent
= -pHori
->Descender
;
1560 Ascent
= pOS2
->usWinAscent
;
1561 Descent
= pOS2
->usWinDescent
;
1564 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1565 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1566 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1567 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1568 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1569 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1571 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1573 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1576 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1578 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1579 - ((Ascent
+ Descent
)
1580 - (pHori
->Ascender
- pHori
->Descender
)),
1581 YScale
) + 32) >> 6);
1583 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1584 if (TM
->tmAveCharWidth
== 0)
1586 TM
->tmAveCharWidth
= 1;
1589 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1590 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1594 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1598 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1599 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1601 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1605 TM
->tmWeight
= FontGDI
->RequestWeight
;
1610 TM
->tmDigitizedAspectX
= 96;
1611 TM
->tmDigitizedAspectY
= 96;
1612 if (face_has_symbol_charmap(Face
) ||
1613 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1615 USHORT cpOEM
, cpAnsi
;
1617 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1618 TM
->tmFirstChar
= 0;
1621 case 1257: /* Baltic */
1622 TM
->tmLastChar
= 0xf8fd;
1625 TM
->tmLastChar
= 0xf0ff;
1627 TM
->tmBreakChar
= 0x20;
1628 TM
->tmDefaultChar
= 0x1f;
1632 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1633 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1635 if(pOS2
->usFirstCharIndex
<= 1)
1636 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1637 else if (pOS2
->usFirstCharIndex
> 0xff)
1638 TM
->tmBreakChar
= 0x20;
1640 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1641 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1646 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1647 TM
->tmUnderlined
= FALSE
;
1648 TM
->tmStruckOut
= FALSE
;
1652 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1654 TM
->tmItalic
= 0xFF;
1660 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1661 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1664 if (!FT_IS_FIXED_WIDTH(Face
))
1666 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1668 case PAN_PROP_MONOSPACED
:
1669 TM
->tmPitchAndFamily
= 0;
1672 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1678 TM
->tmPitchAndFamily
= 0;
1681 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1683 case PAN_FAMILY_SCRIPT
:
1684 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1686 case PAN_FAMILY_DECORATIVE
:
1687 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1692 case PAN_FAMILY_TEXT_DISPLAY
:
1693 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1694 /* Which is clearly not what the panose spec says. */
1695 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1697 TM
->tmPitchAndFamily
= FF_MODERN
;
1701 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1706 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1709 case PAN_SERIF_COVE
:
1710 case PAN_SERIF_OBTUSE_COVE
:
1711 case PAN_SERIF_SQUARE_COVE
:
1712 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1713 case PAN_SERIF_SQUARE
:
1714 case PAN_SERIF_THIN
:
1715 case PAN_SERIF_BONE
:
1716 case PAN_SERIF_EXAGGERATED
:
1717 case PAN_SERIF_TRIANGLE
:
1718 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1721 case PAN_SERIF_NORMAL_SANS
:
1722 case PAN_SERIF_OBTUSE_SANS
:
1723 case PAN_SERIF_PERP_SANS
:
1724 case PAN_SERIF_FLARED
:
1725 case PAN_SERIF_ROUNDED
:
1726 TM
->tmPitchAndFamily
|= FF_SWISS
;
1732 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1735 if (FT_IS_SCALABLE(Face
))
1737 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1739 if (FT_IS_SFNT(Face
))
1741 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1744 TM
->tmCharSet
= FontGDI
->CharSet
;
1747 static void FASTCALL
1748 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1749 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1750 FT_WinFNT_HeaderRec
*pFNT
)
1752 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1756 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
1757 FT_UShort NameID
, FT_UShort LangID
);
1759 /*************************************************************
1760 * IntGetOutlineTextMetrics
1764 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1766 OUTLINETEXTMETRICW
*Otm
)
1769 TT_HoriHeader
*pHori
;
1770 TT_Postscript
*pPost
;
1771 FT_Fixed XScale
, YScale
;
1772 FT_WinFNT_HeaderRec Win
;
1775 UNICODE_STRING FamilyNameW
, FaceNameW
, StyleNameW
, FullNameW
;
1776 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1777 PSHARED_FACE_CACHE Cache
= (PRIMARYLANGID(gusLanguageID
) == LANG_ENGLISH
) ? &SharedFace
->EnglishUS
: &SharedFace
->UserLanguage
;
1778 FT_Face Face
= SharedFace
->Face
;
1780 if (Cache
->OutlineRequiredSize
&& Size
< Cache
->OutlineRequiredSize
)
1782 return Cache
->OutlineRequiredSize
;
1786 RtlInitUnicodeString(&FamilyNameW
, NULL
);
1787 IntGetFontLocalizedName(&FamilyNameW
, SharedFace
, TT_NAME_ID_FONT_FAMILY
, gusLanguageID
);
1790 RtlInitUnicodeString(&FaceNameW
, NULL
);
1791 IntGetFontLocalizedName(&FaceNameW
, SharedFace
, TT_NAME_ID_FULL_NAME
, gusLanguageID
);
1794 RtlInitUnicodeString(&StyleNameW
, NULL
);
1795 IntGetFontLocalizedName(&StyleNameW
, SharedFace
, TT_NAME_ID_FONT_SUBFAMILY
, gusLanguageID
);
1797 /* unique name (full name) */
1798 RtlInitUnicodeString(&FullNameW
, NULL
);
1799 IntGetFontLocalizedName(&FullNameW
, SharedFace
, TT_NAME_ID_UNIQUE_ID
, gusLanguageID
);
1801 if (!Cache
->OutlineRequiredSize
)
1804 Needed
= sizeof(OUTLINETEXTMETRICW
);
1805 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1806 Needed
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1807 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1808 Needed
+= FullNameW
.Length
+ sizeof(WCHAR
);
1810 Cache
->OutlineRequiredSize
= Needed
;
1813 if (Size
< Cache
->OutlineRequiredSize
)
1815 RtlFreeUnicodeString(&FamilyNameW
);
1816 RtlFreeUnicodeString(&FaceNameW
);
1817 RtlFreeUnicodeString(&StyleNameW
);
1818 RtlFreeUnicodeString(&FullNameW
);
1819 return Cache
->OutlineRequiredSize
;
1822 XScale
= Face
->size
->metrics
.x_scale
;
1823 YScale
= Face
->size
->metrics
.y_scale
;
1826 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1830 DPRINT1("Can't find OS/2 table - not TT font?\n");
1831 RtlFreeUnicodeString(&FamilyNameW
);
1832 RtlFreeUnicodeString(&FaceNameW
);
1833 RtlFreeUnicodeString(&StyleNameW
);
1834 RtlFreeUnicodeString(&FullNameW
);
1838 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1842 DPRINT1("Can't find HHEA table - not TT font?\n");
1843 RtlFreeUnicodeString(&FamilyNameW
);
1844 RtlFreeUnicodeString(&FaceNameW
);
1845 RtlFreeUnicodeString(&StyleNameW
);
1846 RtlFreeUnicodeString(&FullNameW
);
1850 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1852 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1854 Otm
->otmSize
= Cache
->OutlineRequiredSize
;
1856 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1859 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1860 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1861 Otm
->otmfsType
= pOS2
->fsType
;
1862 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1863 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1864 Otm
->otmItalicAngle
= 0; /* POST table */
1865 Otm
->otmEMSquare
= Face
->units_per_EM
;
1866 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1867 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1868 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1869 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1870 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1871 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1872 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1873 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1874 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1875 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1876 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1877 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1878 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1879 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1880 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1881 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1882 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1883 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1884 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1885 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1886 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1887 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1888 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1891 Otm
->otmsUnderscoreSize
= 0;
1892 Otm
->otmsUnderscorePosition
= 0;
1896 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1897 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1902 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1905 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1906 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1907 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1910 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1911 wcscpy((WCHAR
*) Cp
, FaceNameW
.Buffer
);
1912 Cp
+= FaceNameW
.Length
+ sizeof(WCHAR
);
1915 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1916 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1917 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1919 /* unique name (full name) */
1920 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1921 wcscpy((WCHAR
*) Cp
, FullNameW
.Buffer
);
1922 Cp
+= FullNameW
.Length
+ sizeof(WCHAR
);
1924 ASSERT(Cp
- (char*)Otm
== Cache
->OutlineRequiredSize
);
1926 RtlFreeUnicodeString(&FamilyNameW
);
1927 RtlFreeUnicodeString(&FaceNameW
);
1928 RtlFreeUnicodeString(&StyleNameW
);
1929 RtlFreeUnicodeString(&FullNameW
);
1931 return Cache
->OutlineRequiredSize
;
1934 static PFONTGDI FASTCALL
1935 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1938 PFONT_ENTRY CurrentEntry
;
1939 ANSI_STRING EntryFaceNameA
;
1940 UNICODE_STRING EntryFaceNameW
;
1944 Entry
= Head
->Flink
;
1945 while (Entry
!= Head
)
1947 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1949 FontGDI
= CurrentEntry
->Font
;
1952 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1953 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1954 if (!NT_SUCCESS(status
))
1959 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1961 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1962 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1965 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
1967 RtlFreeUnicodeString(&EntryFaceNameW
);
1971 RtlFreeUnicodeString(&EntryFaceNameW
);
1972 Entry
= Entry
->Flink
;
1978 static PFONTGDI FASTCALL
1979 FindFaceNameInLists(PUNICODE_STRING FaceName
)
1981 PPROCESSINFO Win32Process
;
1984 /* Search the process local list.
1985 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
1986 Win32Process
= PsGetCurrentProcessWin32Process();
1987 IntLockProcessPrivateFonts(Win32Process
);
1988 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
1989 IntUnLockProcessPrivateFonts(Win32Process
);
1995 /* Search the global list */
1997 Font
= FindFaceNameInList(FaceName
, &FontListHead
);
1998 IntUnLockGlobalFonts
;
2003 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
2005 CharSetFromLangID(LANGID LangID
)
2007 /* FIXME: Add more and fix if wrong */
2008 switch (PRIMARYLANGID(LangID
))
2011 switch (SUBLANGID(LangID
))
2013 case SUBLANG_CHINESE_TRADITIONAL
:
2014 return CHINESEBIG5_CHARSET
;
2015 case SUBLANG_CHINESE_SIMPLIFIED
:
2019 return GB2312_CHARSET
;
2021 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2022 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2023 return EASTEUROPE_CHARSET
;
2025 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2026 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2027 return RUSSIAN_CHARSET
;
2029 case LANG_ARABIC
: return ARABIC_CHARSET
;
2030 case LANG_GREEK
: return GREEK_CHARSET
;
2031 case LANG_HEBREW
: return HEBREW_CHARSET
;
2032 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2033 case LANG_KOREAN
: return JOHAB_CHARSET
;
2034 case LANG_TURKISH
: return TURKISH_CHARSET
;
2035 case LANG_THAI
: return THAI_CHARSET
;
2036 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2037 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2039 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2040 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2041 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2042 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2043 case LANG_SWEDISH
: default:
2044 return ANSI_CHARSET
;
2049 SwapEndian(LPVOID pvData
, DWORD Size
)
2051 BYTE b
, *pb
= pvData
;
2063 DuplicateUnicodeString(PUNICODE_STRING Source
, PUNICODE_STRING Destination
)
2065 NTSTATUS Status
= STATUS_NO_MEMORY
;
2068 Tmp
.Buffer
= ExAllocatePoolWithTag(PagedPool
, Source
->MaximumLength
, TAG_USTR
);
2071 Tmp
.MaximumLength
= Source
->MaximumLength
;
2073 RtlCopyUnicodeString(&Tmp
, Source
);
2075 Destination
->MaximumLength
= Tmp
.MaximumLength
;
2076 Destination
->Length
= Tmp
.Length
;
2077 Destination
->Buffer
= Tmp
.Buffer
;
2079 Status
= STATUS_SUCCESS
;
2086 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, PSHARED_FACE SharedFace
,
2087 FT_UShort NameID
, FT_UShort LangID
)
2090 INT i
, Count
, BestIndex
, Score
, BestScore
;
2091 WCHAR Buf
[LF_FULLFACESIZE
];
2093 NTSTATUS Status
= STATUS_NOT_FOUND
;
2094 ANSI_STRING AnsiName
;
2095 PSHARED_FACE_CACHE Cache
;
2096 FT_Face Face
= SharedFace
->Face
;
2098 RtlFreeUnicodeString(pNameW
);
2101 if (PRIMARYLANGID(LangID
) == LANG_ENGLISH
)
2103 Cache
= &SharedFace
->EnglishUS
;
2107 Cache
= &SharedFace
->UserLanguage
;
2110 /* use cache if available */
2111 if (NameID
== TT_NAME_ID_FONT_FAMILY
&& Cache
->FontFamily
.Buffer
)
2113 return DuplicateUnicodeString(&Cache
->FontFamily
, pNameW
);
2115 if (NameID
== TT_NAME_ID_FULL_NAME
&& Cache
->FullName
.Buffer
)
2117 return DuplicateUnicodeString(&Cache
->FullName
, pNameW
);
2123 Count
= FT_Get_Sfnt_Name_Count(Face
);
2124 for (i
= 0; i
< Count
; ++i
)
2126 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2129 continue; /* failure */
2132 if (Name
.name_id
!= NameID
)
2134 continue; /* mismatched */
2137 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2138 (Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
&&
2139 Name
.encoding_id
!= TT_MS_ID_SYMBOL_CS
))
2141 continue; /* not Microsoft Unicode name */
2144 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2145 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2147 continue; /* invalid string */
2150 if (sizeof(Buf
) < Name
.string_len
+ sizeof(UNICODE_NULL
))
2152 continue; /* name too long */
2155 if (Name
.language_id
== LangID
)
2159 break; /* best match */
2161 else if (PRIMARYLANGID(Name
.language_id
) == PRIMARYLANGID(LangID
))
2165 else if (PRIMARYLANGID(Name
.language_id
) == LANG_ENGLISH
)
2174 if (Score
> BestScore
)
2183 /* store the best name */
2184 Error
= (Score
== 30) ? 0 : FT_Get_Sfnt_Name(Face
, BestIndex
, &Name
);
2187 /* NOTE: Name.string is not null-terminated */
2188 RtlCopyMemory(Buf
, Name
.string
, Name
.string_len
);
2189 Buf
[Name
.string_len
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2191 /* Convert UTF-16 big endian to little endian */
2192 SwapEndian(Buf
, Name
.string_len
);
2194 Status
= RtlCreateUnicodeString(pNameW
, Buf
);
2198 if (!NT_SUCCESS(Status
))
2201 if (NameID
== TT_NAME_ID_FONT_SUBFAMILY
)
2203 RtlInitAnsiString(&AnsiName
, Face
->style_name
);
2204 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2208 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2209 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2213 if (NT_SUCCESS(Status
))
2216 if (NameID
== TT_NAME_ID_FONT_FAMILY
)
2218 ASSERT_FREETYPE_LOCK_NOT_HELD();
2220 if (!Cache
->FontFamily
.Buffer
)
2221 DuplicateUnicodeString(pNameW
, &Cache
->FontFamily
);
2224 else if (NameID
== TT_NAME_ID_FULL_NAME
)
2226 ASSERT_FREETYPE_LOCK_NOT_HELD();
2228 if (!Cache
->FullName
.Buffer
)
2229 DuplicateUnicodeString(pNameW
, &Cache
->FullName
);
2237 static void FASTCALL
2238 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2239 LPCWSTR FullName
, PFONTGDI FontGDI
)
2242 UNICODE_STRING StyleW
;
2245 CHARSETINFO CharSetInfo
;
2247 OUTLINETEXTMETRICW
*Otm
;
2250 NEWTEXTMETRICW
*Ntm
;
2253 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
2254 FT_Face Face
= SharedFace
->Face
;
2255 UNICODE_STRING NameW
;
2257 RtlInitUnicodeString(&NameW
, NULL
);
2258 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2259 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2260 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2265 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2268 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2272 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2273 TM
= &Otm
->otmTextMetrics
;
2275 Lf
->lfHeight
= TM
->tmHeight
;
2276 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2277 Lf
->lfWeight
= TM
->tmWeight
;
2278 Lf
->lfItalic
= TM
->tmItalic
;
2279 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2280 Lf
->lfCharSet
= TM
->tmCharSet
;
2281 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2282 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2283 Lf
->lfQuality
= PROOF_QUALITY
;
2285 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2286 Ntm
->tmHeight
= TM
->tmHeight
;
2287 Ntm
->tmAscent
= TM
->tmAscent
;
2288 Ntm
->tmDescent
= TM
->tmDescent
;
2289 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2290 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2291 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2292 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2293 Ntm
->tmWeight
= TM
->tmWeight
;
2294 Ntm
->tmOverhang
= TM
->tmOverhang
;
2295 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2296 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2297 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2298 Ntm
->tmLastChar
= TM
->tmLastChar
;
2299 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2300 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2301 Ntm
->tmItalic
= TM
->tmItalic
;
2302 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2303 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2304 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2305 Ntm
->tmCharSet
= TM
->tmCharSet
;
2306 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2308 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2310 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2312 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2313 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2314 Ntm
->ntmAvgWidth
= 0;
2316 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2317 ? TRUETYPE_FONTTYPE
: 0);
2319 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2320 Info
->FontType
|= RASTER_FONTTYPE
;
2325 FaceName
= (WCHAR
*)((ULONG_PTR
)Otm
+ (ULONG_PTR
)Otm
->otmpFamilyName
);
2327 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2331 FullName
= (WCHAR
*)((ULONG_PTR
) Otm
+ (ULONG_PTR
)Otm
->otmpFaceName
);
2333 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2334 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2337 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2339 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2340 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2341 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2342 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2343 if (!NT_SUCCESS(status
))
2347 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2350 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2358 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2359 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2360 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2361 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2362 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2363 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2365 if (0 == pOS2
->version
)
2369 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2370 fs
.fsCsb
[0] |= FS_LATIN1
;
2372 fs
.fsCsb
[0] |= FS_SYMBOL
;
2376 if (fs
.fsCsb
[0] == 0)
2378 /* Let's see if we can find any interesting cmaps */
2379 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2381 switch (Face
->charmaps
[i
]->encoding
)
2383 case FT_ENCODING_UNICODE
:
2384 case FT_ENCODING_APPLE_ROMAN
:
2385 fs
.fsCsb
[0] |= FS_LATIN1
;
2387 case FT_ENCODING_MS_SYMBOL
:
2388 fs
.fsCsb
[0] |= FS_SYMBOL
;
2396 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2399 if (fs
.fsCsb
[0] & fs0
)
2401 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2403 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2405 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2408 wcscpy(Info
->EnumLogFontEx
.elfScript
, ElfScripts
[i
]);
2411 DPRINT1("Unknown elfscript for bit %u\n", i
);
2416 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2420 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2423 UNICODE_STRING InfoFaceName
;
2425 for (i
= 0; i
< InfoEntries
; i
++)
2427 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2428 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2437 static BOOLEAN FASTCALL
2438 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2439 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2441 UNICODE_STRING LogFontFaceName
;
2443 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2444 if (0 != LogFontFaceName
.Length
&&
2445 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2450 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2453 static BOOLEAN FASTCALL
2454 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2455 PFONTFAMILYINFO Info
,
2461 PFONT_ENTRY CurrentEntry
;
2462 ANSI_STRING EntryFaceNameA
;
2463 UNICODE_STRING EntryFaceNameW
;
2467 Entry
= Head
->Flink
;
2468 while (Entry
!= Head
)
2470 CurrentEntry
= (PFONT_ENTRY
) CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2472 FontGDI
= CurrentEntry
->Font
;
2475 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
2476 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
2477 if (!NT_SUCCESS(status
))
2482 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
2484 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2485 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2488 if (FontFamilyInclude(LogFont
, &EntryFaceNameW
, Info
, min(*Count
, Size
)))
2492 FontFamilyFillInfo(Info
+ *Count
, EntryFaceNameW
.Buffer
,
2497 RtlFreeUnicodeString(&EntryFaceNameW
);
2498 Entry
= Entry
->Flink
;
2504 static BOOLEAN FASTCALL
2505 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2506 PFONTFAMILYINFO Info
,
2510 PLIST_ENTRY pEntry
, pHead
= &FontSubstListHead
;
2511 PFONTSUBST_ENTRY pCurrentEntry
;
2512 PUNICODE_STRING pFromW
;
2514 LOGFONTW lf
= *LogFont
;
2515 UNICODE_STRING NameW
;
2517 for (pEntry
= pHead
->Flink
; pEntry
!= pHead
; pEntry
= pEntry
->Flink
)
2519 pCurrentEntry
= CONTAINING_RECORD(pEntry
, FONTSUBST_ENTRY
, ListEntry
);
2521 pFromW
= &pCurrentEntry
->FontNames
[FONTSUBST_FROM
];
2522 if (LogFont
->lfFaceName
[0] != UNICODE_NULL
)
2524 if (!FontFamilyInclude(LogFont
, pFromW
, Info
, min(*pCount
, MaxCount
)))
2525 continue; /* mismatch */
2528 RtlStringCchCopyW(lf
.lfFaceName
, LF_FACESIZE
, pFromW
->Buffer
);
2529 SubstituteFontRecurse(&lf
);
2531 RtlInitUnicodeString(&NameW
, lf
.lfFaceName
);
2532 FontGDI
= FindFaceNameInLists(&NameW
);
2533 if (FontGDI
== NULL
)
2535 continue; /* no real font */
2538 if (*pCount
< MaxCount
)
2540 FontFamilyFillInfo(&Info
[*pCount
], pFromW
->Buffer
, NULL
, FontGDI
);
2550 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2554 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2555 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2556 lprs
->nLanguageID
= gusLanguageID
;
2559 EngSetLastError(ERROR_INVALID_PARAMETER
);
2569 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2570 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2571 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2572 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2575 FT_BitmapGlyph APIENTRY
2582 PLIST_ENTRY CurrentEntry
;
2583 PFONT_CACHE_ENTRY FontEntry
;
2585 ASSERT_FREETYPE_LOCK_HELD();
2587 CurrentEntry
= FontCacheListHead
.Flink
;
2588 while (CurrentEntry
!= &FontCacheListHead
)
2590 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2591 if ((FontEntry
->Face
== Face
) &&
2592 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2593 (FontEntry
->Height
== Height
) &&
2594 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2596 CurrentEntry
= CurrentEntry
->Flink
;
2599 if (CurrentEntry
== &FontCacheListHead
)
2604 RemoveEntryList(CurrentEntry
);
2605 InsertHeadList(&FontCacheListHead
, CurrentEntry
);
2606 return FontEntry
->BitmapGlyph
;
2610 FT_BitmapGlyph APIENTRY
2613 FT_GlyphSlot GlyphSlot
,
2614 FT_Render_Mode RenderMode
)
2618 FT_Bitmap AlignedBitmap
;
2619 FT_BitmapGlyph BitmapGlyph
;
2621 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2624 DPRINT1("Failure getting glyph.\n");
2628 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2631 FT_Done_Glyph(Glyph
);
2632 DPRINT1("Failure rendering glyph.\n");
2636 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2637 FT_Bitmap_New(&AlignedBitmap
);
2638 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2640 DPRINT1("Conversion failed\n");
2641 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2645 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2646 BitmapGlyph
->bitmap
= AlignedBitmap
;
2651 FT_BitmapGlyph APIENTRY
2657 FT_GlyphSlot GlyphSlot
,
2658 FT_Render_Mode RenderMode
)
2662 PFONT_CACHE_ENTRY NewEntry
;
2663 FT_Bitmap AlignedBitmap
;
2664 FT_BitmapGlyph BitmapGlyph
;
2666 ASSERT_FREETYPE_LOCK_HELD();
2668 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2671 DPRINT1("Failure caching glyph.\n");
2675 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2678 FT_Done_Glyph(GlyphCopy
);
2679 DPRINT1("Failure rendering glyph.\n");
2683 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2686 DPRINT1("Alloc failure caching glyph.\n");
2687 FT_Done_Glyph(GlyphCopy
);
2691 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2692 FT_Bitmap_New(&AlignedBitmap
);
2693 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2695 DPRINT1("Conversion failed\n");
2696 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2697 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2698 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2702 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2703 BitmapGlyph
->bitmap
= AlignedBitmap
;
2705 NewEntry
->GlyphIndex
= GlyphIndex
;
2706 NewEntry
->Face
= Face
;
2707 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2708 NewEntry
->Height
= Height
;
2709 NewEntry
->mxWorldToDevice
= *pmx
;
2711 InsertHeadList(&FontCacheListHead
, &NewEntry
->ListEntry
);
2712 if (++FontCacheNumEntries
> MAX_FONT_CACHE
)
2714 NewEntry
= CONTAINING_RECORD(FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2715 RemoveCachedEntry(NewEntry
);
2722 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2724 pt
->x
.value
= vec
->x
>> 6;
2725 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2726 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2727 pt
->y
.value
= vec
->y
>> 6;
2728 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2729 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2733 This function builds an FT_Fixed from a float. It puts the integer part
2734 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2735 It fails if the integer part of the float number is greater than SHORT_MAX.
2737 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2740 unsigned short fract
= (f
- value
) * 0xFFFF;
2741 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2745 This function builds an FT_Fixed from a FIXED. It simply put f.value
2746 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2748 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2750 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2753 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2755 TTPOLYGONHEADER
*pph
;
2757 int needed
= 0, point
= 0, contour
, first_pt
;
2758 unsigned int pph_start
, cpfx
;
2761 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2763 /* Ignore contours containing one point */
2764 if (point
== outline
->contours
[contour
])
2771 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2775 pph
->dwType
= TT_POLYGON_TYPE
;
2776 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2778 needed
+= sizeof(*pph
);
2780 while (point
<= outline
->contours
[contour
])
2782 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2783 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2784 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2789 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2792 } while (point
<= outline
->contours
[contour
] &&
2793 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2794 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2795 /* At the end of a contour Windows adds the start point, but
2797 if (point
> outline
->contours
[contour
] &&
2798 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2801 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2804 else if (point
<= outline
->contours
[contour
] &&
2805 outline
->tags
[point
] & FT_Curve_Tag_On
)
2807 /* add closing pt for bezier */
2809 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2818 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2821 pph
->cb
= needed
- pph_start
;
2826 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2828 /* Convert the quadratic Beziers to cubic Beziers.
2829 The parametric eqn for a cubic Bezier is, from PLRM:
2830 r(t) = at^3 + bt^2 + ct + r0
2831 with the control points:
2836 A quadratic Bezier has the form:
2837 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2839 So equating powers of t leads to:
2840 r1 = 2/3 p1 + 1/3 p0
2841 r2 = 2/3 p1 + 1/3 p2
2842 and of course r0 = p0, r3 = p2
2844 int contour
, point
= 0, first_pt
;
2845 TTPOLYGONHEADER
*pph
;
2847 DWORD pph_start
, cpfx
, type
;
2848 FT_Vector cubic_control
[4];
2849 unsigned int needed
= 0;
2851 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2854 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2858 pph
->dwType
= TT_POLYGON_TYPE
;
2859 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2861 needed
+= sizeof(*pph
);
2863 while (point
<= outline
->contours
[contour
])
2865 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2866 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2867 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2871 if (type
== TT_PRIM_LINE
)
2874 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2880 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2883 /* FIXME: Possible optimization in endpoint calculation
2884 if there are two consecutive curves */
2885 cubic_control
[0] = outline
->points
[point
-1];
2886 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2888 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2889 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2890 cubic_control
[0].x
>>= 1;
2891 cubic_control
[0].y
>>= 1;
2893 if (point
+1 > outline
->contours
[contour
])
2894 cubic_control
[3] = outline
->points
[first_pt
];
2897 cubic_control
[3] = outline
->points
[point
+1];
2898 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2900 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2901 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2902 cubic_control
[3].x
>>= 1;
2903 cubic_control
[3].y
>>= 1;
2906 /* r1 = 1/3 p0 + 2/3 p1
2907 r2 = 1/3 p2 + 2/3 p1 */
2908 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2909 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2910 cubic_control
[2] = cubic_control
[1];
2911 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2912 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2913 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2914 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2917 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2918 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2919 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
2924 } while (point
<= outline
->contours
[contour
] &&
2925 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2926 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2927 /* At the end of a contour Windows adds the start point,
2928 but only for Beziers and we've already done that.
2930 if (point
<= outline
->contours
[contour
] &&
2931 outline
->tags
[point
] & FT_Curve_Tag_On
)
2933 /* This is the closing pt of a bezier, but we've already
2934 added it, so just inc point and carry on */
2942 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2945 pph
->cb
= needed
- pph_start
;
2951 IntRequestFontSize(PDC dc
, FT_Face face
, LONG Width
, LONG Height
)
2953 FT_Size_RequestRec req
;
2964 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
2974 if (Width
> 0xFFFFU
)
2976 if (Height
> 0xFFFFU
)
2979 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
2980 req
.width
= (FT_Long
)(Width
<< 6);
2981 req
.height
= (FT_Long
)(Height
<< 6);
2982 req
.horiResolution
= 0;
2983 req
.vertResolution
= 0;
2984 return FT_Request_Size(face
, &req
);
2989 TextIntUpdateSize(PDC dc
,
2996 FT_CharMap charmap
, found
;
3002 face
= FontGDI
->SharedFace
->Face
;
3003 if (face
->charmap
== NULL
)
3005 DPRINT("WARNING: No charmap selected!\n");
3006 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
3009 for (n
= 0; n
< face
->num_charmaps
; n
++)
3011 charmap
= face
->charmaps
[n
];
3012 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
3013 if (charmap
->encoding
!= 0)
3021 DPRINT1("WARNING: Could not find desired charmap!\n");
3025 error
= FT_Set_Charmap(face
, found
);
3028 DPRINT1("WARNING: Could not set the charmap!\n");
3033 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3035 error
= IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
3042 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3051 * Based on WineEngGetGlyphOutline
3056 ftGdiGetGlyphOutline(
3064 BOOL bIgnoreRotation
)
3066 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3074 FT_UInt glyph_index
;
3075 DWORD width
, height
, pitch
, needed
= 0;
3076 FT_Bitmap ft_bitmap
;
3078 INT left
, right
, top
= 0, bottom
= 0;
3080 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3081 FLOAT eM11
, widthRatio
= 1.0;
3082 FT_Matrix transMat
= identityMat
;
3083 BOOL needsTransform
= FALSE
;
3086 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3087 OUTLINETEXTMETRICW
*potm
;
3091 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3092 cjBuf
, pvBuf
, pmat2
);
3094 pdcattr
= dc
->pdcattr
;
3096 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3099 hFont
= pdcattr
->hlfntNew
;
3100 TextObj
= RealizeFontInit(hFont
);
3104 EngSetLastError(ERROR_INVALID_HANDLE
);
3107 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3108 ft_face
= FontGDI
->SharedFace
->Face
;
3110 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3111 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3112 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3114 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3115 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3118 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3119 TEXTOBJ_UnlockText(TextObj
);
3122 Size
= IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3125 /* FIXME: last error? */
3126 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3127 TEXTOBJ_UnlockText(TextObj
);
3132 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3133 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3135 TEXTOBJ_UnlockText(TextObj
);
3137 if (iFormat
& GGO_GLYPH_INDEX
)
3140 iFormat
&= ~GGO_GLYPH_INDEX
;
3142 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3144 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3145 load_flags
|= FT_LOAD_NO_BITMAP
;
3147 if (iFormat
& GGO_UNHINTED
)
3149 load_flags
|= FT_LOAD_NO_HINTING
;
3150 iFormat
&= ~GGO_UNHINTED
;