2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: win32ss/gdi/ntgdi/freetype.c
5 * PURPOSE: FreeType font engine interface
6 * PROGRAMMERS: Copyright 2001 Huw D M Davies for CodeWeavers.
7 * Copyright 2006 Dmitry Timoshkov for CodeWeavers.
8 * Copyright 2016-2017 Katayama Hirofumi MZ.
11 /** Includes ******************************************************************/
16 #include FT_TYPE1_TABLES_H
17 #include FT_TRUETYPE_TABLES_H
18 #include FT_TRUETYPE_TAGS_H
19 #include FT_TRIGONOMETRY_H
22 #include FT_WINFONTS_H
23 #include FT_SFNT_NAMES_H
24 #include FT_SYNTHESIS_H
25 #include FT_TRUETYPE_IDS_H
27 #ifndef FT_INTERNAL_INTERNAL_H
28 #define FT_INTERNAL_INTERNAL_H <freetype/internal/internal.h>
29 #include FT_INTERNAL_INTERNAL_H
31 #include FT_INTERNAL_TRUETYPE_TYPES_H
33 #include <gdi/eng/floatobj.h>
39 /* TPMF_FIXED_PITCH is confusing; brain-dead api */
40 #ifndef _TMPF_VARIABLE_PITCH
41 #define _TMPF_VARIABLE_PITCH TMPF_FIXED_PITCH
44 extern const MATRIX gmxWorldToDeviceDefault
;
45 extern const MATRIX gmxWorldToPageDefault
;
47 /* HACK!! Fix XFORMOBJ then use 1:16 / 16:1 */
48 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
51 static const WORD gusEnglishUS
= MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
);
53 /* special font names */
54 static const UNICODE_STRING MarlettW
= RTL_CONSTANT_STRING(L
"Marlett");
55 static const UNICODE_STRING SystemW
= RTL_CONSTANT_STRING(L
"System");
56 static const UNICODE_STRING FixedSysW
= RTL_CONSTANT_STRING(L
"FixedSys");
59 static UNICODE_STRING FontRegPath
=
60 RTL_CONSTANT_STRING(L
"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
63 /* The FreeType library is not thread safe, so we have
64 to serialize access to it */
65 static PFAST_MUTEX FreeTypeLock
;
67 static LIST_ENTRY FontListHead
;
68 static PFAST_MUTEX FontListLock
;
69 static BOOL RenderingEnabled
= TRUE
;
71 #define IntLockGlobalFonts \
72 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FontListLock)
74 #define IntUnLockGlobalFonts \
75 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FontListLock)
77 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
78 ASSERT(FontListLock->Owner == KeGetCurrentThread())
80 #define IntLockFreeType \
81 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FreeTypeLock)
83 #define IntUnLockFreeType \
84 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FreeTypeLock)
86 #define ASSERT_FREETYPE_LOCK_HELD() \
87 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
89 #define MAX_FONT_CACHE 256
91 static LIST_ENTRY FontCacheListHead
;
92 static UINT FontCacheNumEntries
;
94 static PWCHAR ElfScripts
[32] = /* These are in the order of the fsCsb[0] bits */
104 L
"Vietnamese", /* 08 */
105 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* 15 */
113 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
118 * For TranslateCharsetInfo
121 #define MAXTCIINDEX 32
122 static const CHARSETINFO FontTci
[MAXTCIINDEX
] =
125 { ANSI_CHARSET
, 1252, {{0,0,0,0},{FS_LATIN1
,0}} },
126 { EASTEUROPE_CHARSET
, 1250, {{0,0,0,0},{FS_LATIN2
,0}} },
127 { RUSSIAN_CHARSET
, 1251, {{0,0,0,0},{FS_CYRILLIC
,0}} },
128 { GREEK_CHARSET
, 1253, {{0,0,0,0},{FS_GREEK
,0}} },
129 { TURKISH_CHARSET
, 1254, {{0,0,0,0},{FS_TURKISH
,0}} },
130 { HEBREW_CHARSET
, 1255, {{0,0,0,0},{FS_HEBREW
,0}} },
131 { ARABIC_CHARSET
, 1256, {{0,0,0,0},{FS_ARABIC
,0}} },
132 { BALTIC_CHARSET
, 1257, {{0,0,0,0},{FS_BALTIC
,0}} },
133 { VIETNAMESE_CHARSET
, 1258, {{0,0,0,0},{FS_VIETNAMESE
,0}} },
134 /* reserved by ANSI */
135 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
136 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
137 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
138 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
139 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
140 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
141 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
143 { THAI_CHARSET
, 874, {{0,0,0,0},{FS_THAI
,0}} },
144 { SHIFTJIS_CHARSET
, 932, {{0,0,0,0},{FS_JISJAPAN
,0}} },
145 { GB2312_CHARSET
, 936, {{0,0,0,0},{FS_CHINESESIMP
,0}} },
146 { HANGEUL_CHARSET
, 949, {{0,0,0,0},{FS_WANSUNG
,0}} },
147 { CHINESEBIG5_CHARSET
, 950, {{0,0,0,0},{FS_CHINESETRAD
,0}} },
148 { JOHAB_CHARSET
, 1361, {{0,0,0,0},{FS_JOHAB
,0}} },
149 /* Reserved for alternate ANSI and OEM */
150 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
151 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
152 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
153 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
154 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
155 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
156 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
157 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
158 /* Reserved for system */
159 { DEFAULT_CHARSET
, 0, {{0,0,0,0},{FS_LATIN1
,0}} },
160 { SYMBOL_CHARSET
, CP_SYMBOL
, {{0,0,0,0},{FS_SYMBOL
,0}} }
164 static RTL_STATIC_LIST_HEAD(FontSubstListHead
);
167 SharedMem_AddRef(PSHARED_MEM Ptr
)
169 ASSERT_FREETYPE_LOCK_HELD();
175 SharedFace_Create(FT_Face Face
, PSHARED_MEM Memory
)
178 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_FACE
), TAG_FONT
);
183 Ptr
->Memory
= Memory
;
184 SharedMem_AddRef(Memory
);
185 DPRINT("Creating SharedFace for %s\n", Face
->family_name
);
191 SharedMem_Create(PBYTE Buffer
, ULONG BufferSize
, BOOL IsMapping
)
194 Ptr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SHARED_MEM
), TAG_FONT
);
197 Ptr
->Buffer
= Buffer
;
198 Ptr
->BufferSize
= BufferSize
;
200 Ptr
->IsMapping
= IsMapping
;
201 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer
, IsMapping
, Ptr
);
207 SharedFace_AddRef(PSHARED_FACE Ptr
)
209 ASSERT_FREETYPE_LOCK_HELD();
215 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry
)
217 ASSERT_FREETYPE_LOCK_HELD();
219 FT_Done_Glyph((FT_Glyph
)Entry
->BitmapGlyph
);
220 RemoveEntryList(&Entry
->ListEntry
);
221 ExFreePoolWithTag(Entry
, TAG_FONT
);
222 FontCacheNumEntries
--;
223 ASSERT(FontCacheNumEntries
<= MAX_FONT_CACHE
);
227 RemoveCacheEntries(FT_Face Face
)
229 PLIST_ENTRY CurrentEntry
;
230 PFONT_CACHE_ENTRY FontEntry
;
232 ASSERT_FREETYPE_LOCK_HELD();
234 CurrentEntry
= FontCacheListHead
.Flink
;
235 while (CurrentEntry
!= &FontCacheListHead
)
237 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
238 CurrentEntry
= CurrentEntry
->Flink
;
240 if (FontEntry
->Face
== Face
)
242 RemoveCachedEntry(FontEntry
);
247 static void SharedMem_Release(PSHARED_MEM Ptr
)
249 ASSERT_FREETYPE_LOCK_HELD();
250 ASSERT(Ptr
->RefCount
> 0);
252 if (Ptr
->RefCount
<= 0)
256 if (Ptr
->RefCount
== 0)
258 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr
->Buffer
, Ptr
->IsMapping
, Ptr
);
260 MmUnmapViewInSystemSpace(Ptr
->Buffer
);
262 ExFreePoolWithTag(Ptr
->Buffer
, TAG_FONT
);
263 ExFreePoolWithTag(Ptr
, TAG_FONT
);
268 SharedFace_Release(PSHARED_FACE Ptr
)
271 ASSERT(Ptr
->RefCount
> 0);
273 if (Ptr
->RefCount
<= 0)
277 if (Ptr
->RefCount
== 0)
279 DPRINT("Releasing SharedFace for %s\n", Ptr
->Face
->family_name
);
280 RemoveCacheEntries(Ptr
->Face
);
281 FT_Done_Face(Ptr
->Face
);
282 SharedMem_Release(Ptr
->Memory
);
283 ExFreePoolWithTag(Ptr
, TAG_FONT
);
290 * IntLoadFontSubstList --- loads the list of font substitutes
293 IntLoadFontSubstList(PLIST_ENTRY pHead
)
297 OBJECT_ATTRIBUTES ObjectAttributes
;
298 KEY_FULL_INFORMATION KeyFullInfo
;
300 UNICODE_STRING FromW
, ToW
;
301 BYTE InfoBuffer
[128];
302 PKEY_VALUE_FULL_INFORMATION pInfo
;
303 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
305 PFONTSUBST_ENTRY pEntry
;
307 /* the FontSubstitutes registry key */
308 static UNICODE_STRING FontSubstKey
=
309 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\"
310 L
"Microsoft\\Windows NT\\CurrentVersion\\"
313 /* open registry key */
314 InitializeObjectAttributes(&ObjectAttributes
, &FontSubstKey
,
315 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
317 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
318 if (!NT_SUCCESS(Status
))
320 DPRINT("ZwOpenKey failed: 0x%08X\n", Status
);
321 return FALSE
; /* failure */
324 /* query count of values */
325 Status
= ZwQueryKey(KeyHandle
, KeyFullInformation
,
326 &KeyFullInfo
, sizeof(KeyFullInfo
), &Length
);
327 if (!NT_SUCCESS(Status
))
329 DPRINT("ZwQueryKey failed: 0x%08X\n", Status
);
331 return FALSE
; /* failure */
335 for (i
= 0; i
< KeyFullInfo
.Values
; ++i
)
338 Status
= ZwEnumerateValueKey(KeyHandle
, i
, KeyValueFullInformation
,
339 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
340 if (!NT_SUCCESS(Status
))
342 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status
);
346 /* create FromW string */
347 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
348 Length
= pInfo
->NameLength
/ sizeof(WCHAR
);
349 pInfo
->Name
[Length
] = UNICODE_NULL
; /* truncate */
350 Status
= RtlCreateUnicodeString(&FromW
, pInfo
->Name
);
351 if (!NT_SUCCESS(Status
))
353 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
358 Status
= ZwQueryValueKey(KeyHandle
, &FromW
, KeyValueFullInformation
,
359 InfoBuffer
, sizeof(InfoBuffer
), &Length
);
360 pInfo
= (PKEY_VALUE_FULL_INFORMATION
)InfoBuffer
;
361 if (!NT_SUCCESS(Status
) || !pInfo
->DataLength
)
363 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status
);
364 RtlFreeUnicodeString(&FromW
);
368 /* create ToW string */
369 pch
= (LPWSTR
)((PUCHAR
)pInfo
+ pInfo
->DataOffset
);
370 Length
= pInfo
->DataLength
/ sizeof(WCHAR
);
371 pch
[Length
] = UNICODE_NULL
; /* truncate */
372 Status
= RtlCreateUnicodeString(&ToW
, pch
);
373 if (!NT_SUCCESS(Status
))
375 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
376 RtlFreeUnicodeString(&FromW
);
380 /* does charset exist? (from) */
381 CharSets
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
382 pch
= wcsrchr(FromW
.Buffer
, L
',');
387 FromW
.Length
= (pch
- FromW
.Buffer
) * sizeof(WCHAR
);
388 /* parse charset number */
389 CharSets
[FONTSUBST_FROM
] = (BYTE
)_wtoi(pch
+ 1);
392 /* does charset exist? (to) */
393 CharSets
[FONTSUBST_TO
] = DEFAULT_CHARSET
;
394 pch
= wcsrchr(ToW
.Buffer
, L
',');
399 ToW
.Length
= (pch
- ToW
.Buffer
) * sizeof(WCHAR
);
400 /* parse charset number */
401 CharSets
[FONTSUBST_TO
] = (BYTE
)_wtoi(pch
+ 1);
404 /* allocate an entry */
405 pEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONTSUBST_ENTRY
), TAG_FONT
);
408 DPRINT("ExAllocatePoolWithTag failed\n");
409 RtlFreeUnicodeString(&FromW
);
410 RtlFreeUnicodeString(&ToW
);
414 /* store to *pEntry */
415 pEntry
->FontNames
[FONTSUBST_FROM
] = FromW
;
416 pEntry
->FontNames
[FONTSUBST_TO
] = ToW
;
417 pEntry
->CharSets
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
418 pEntry
->CharSets
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
420 /* insert pEntry to *pHead */
421 InsertTailList(pHead
, &pEntry
->ListEntry
);
427 return NT_SUCCESS(Status
);
431 InitFontSupport(VOID
)
435 InitializeListHead(&FontListHead
);
436 InitializeListHead(&FontCacheListHead
);
437 FontCacheNumEntries
= 0;
438 /* Fast Mutexes must be allocated from non paged pool */
439 FontListLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
440 if (FontListLock
== NULL
)
445 ExInitializeFastMutex(FontListLock
);
446 FreeTypeLock
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(FAST_MUTEX
), TAG_INTERNAL_SYNC
);
447 if (FreeTypeLock
== NULL
)
451 ExInitializeFastMutex(FreeTypeLock
);
453 ulError
= FT_Init_FreeType(&library
);
456 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError
);
460 IntLoadSystemFonts();
461 IntLoadFontSubstList(&FontSubstListHead
);
467 FtSetCoordinateTransform(
474 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
476 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
477 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
480 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
481 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
484 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
485 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
488 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
489 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
491 /* Set the transformation matrix */
492 FT_Set_Transform(face
, &ftmatrix
, 0);
496 SubstituteFontByList(PLIST_ENTRY pHead
,
497 PUNICODE_STRING pOutputName
,
498 PUNICODE_STRING pInputName
,
499 BYTE RequestedCharSet
,
500 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
])
503 PLIST_ENTRY pListEntry
;
504 PFONTSUBST_ENTRY pSubstEntry
;
505 BYTE CharSets
[FONTSUBST_FROM_AND_TO
];
507 CharSetMap
[FONTSUBST_FROM
] = DEFAULT_CHARSET
;
508 CharSetMap
[FONTSUBST_TO
] = RequestedCharSet
;
510 /* for each list entry */
511 for (pListEntry
= pHead
->Flink
;
513 pListEntry
= pListEntry
->Flink
)
516 (PFONTSUBST_ENTRY
)CONTAINING_RECORD(pListEntry
, FONT_ENTRY
, ListEntry
);
518 CharSets
[FONTSUBST_FROM
] = pSubstEntry
->CharSets
[FONTSUBST_FROM
];
520 if (CharSets
[FONTSUBST_FROM
] != DEFAULT_CHARSET
&&
521 CharSets
[FONTSUBST_FROM
] != RequestedCharSet
)
523 continue; /* not matched */
526 /* does charset number exist? (to) */
527 if (pSubstEntry
->CharSets
[FONTSUBST_TO
] != DEFAULT_CHARSET
)
529 CharSets
[FONTSUBST_TO
] = pSubstEntry
->CharSets
[FONTSUBST_TO
];
533 CharSets
[FONTSUBST_TO
] = RequestedCharSet
;
536 /* does font name match? */
537 if (!RtlEqualUnicodeString(&pSubstEntry
->FontNames
[FONTSUBST_FROM
],
540 continue; /* not matched */
543 /* update *pOutputName */
544 RtlFreeUnicodeString(pOutputName
);
545 Status
= RtlCreateUnicodeString(pOutputName
,
546 pSubstEntry
->FontNames
[FONTSUBST_TO
].Buffer
);
547 if (!NT_SUCCESS(Status
))
549 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status
);
550 continue; /* cannot create string */
553 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
)
555 /* update CharSetMap */
556 CharSetMap
[FONTSUBST_FROM
] = CharSets
[FONTSUBST_FROM
];
557 CharSetMap
[FONTSUBST_TO
] = CharSets
[FONTSUBST_TO
];
559 return TRUE
; /* success */
566 SubstituteFontRecurse(PUNICODE_STRING pInOutName
, BYTE
*pRequestedCharSet
)
568 UINT RecurseCount
= 5;
569 UNICODE_STRING OutputNameW
= { 0 };
570 BYTE CharSetMap
[FONTSUBST_FROM_AND_TO
];
573 if (pInOutName
->Buffer
[0] == UNICODE_NULL
)
576 while (RecurseCount
-- > 0)
578 RtlInitUnicodeString(&OutputNameW
, NULL
);
579 Found
= SubstituteFontByList(&FontSubstListHead
,
580 &OutputNameW
, pInOutName
,
581 *pRequestedCharSet
, CharSetMap
);
585 /* update *pInOutName and *pRequestedCharSet */
586 RtlFreeUnicodeString(pInOutName
);
587 *pInOutName
= OutputNameW
;
588 if (CharSetMap
[FONTSUBST_FROM
] == DEFAULT_CHARSET
||
589 CharSetMap
[FONTSUBST_FROM
] == *pRequestedCharSet
)
591 *pRequestedCharSet
= CharSetMap
[FONTSUBST_TO
];
595 return TRUE
; /* success */
601 * Search the system font directory and adds each font found.
604 IntLoadSystemFonts(VOID
)
606 OBJECT_ATTRIBUTES ObjectAttributes
;
607 UNICODE_STRING Directory
, FileName
, TempString
;
608 IO_STATUS_BLOCK Iosb
;
611 PFILE_DIRECTORY_INFORMATION DirInfo
;
612 BOOLEAN bRestartScan
= TRUE
;
615 static UNICODE_STRING SearchPatterns
[] =
617 RTL_CONSTANT_STRING(L
"*.ttf"),
618 RTL_CONSTANT_STRING(L
"*.ttc"),
619 RTL_CONSTANT_STRING(L
"*.otf"),
620 RTL_CONSTANT_STRING(L
"*.otc"),
621 RTL_CONSTANT_STRING(L
"*.fon"),
622 RTL_CONSTANT_STRING(L
"*.fnt")
625 RtlInitUnicodeString(&Directory
, L
"\\SystemRoot\\Fonts\\");
627 InitializeObjectAttributes(
630 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
636 SYNCHRONIZE
| FILE_LIST_DIRECTORY
,
639 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
640 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_DIRECTORY_FILE
);
642 if (NT_SUCCESS(Status
))
644 for (i
= 0; i
< _countof(SearchPatterns
); ++i
)
646 DirInfoBuffer
= ExAllocatePoolWithTag(PagedPool
, 0x4000, TAG_FONT
);
647 if (DirInfoBuffer
== NULL
)
653 FileName
.Buffer
= ExAllocatePoolWithTag(PagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_FONT
);
654 if (FileName
.Buffer
== NULL
)
656 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
661 FileName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
665 Status
= ZwQueryDirectoryFile(
673 FileDirectoryInformation
,
678 if (!NT_SUCCESS(Status
) || Status
== STATUS_NO_MORE_FILES
)
683 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)DirInfoBuffer
;
686 TempString
.Buffer
= DirInfo
->FileName
;
688 TempString
.MaximumLength
= DirInfo
->FileNameLength
;
689 RtlCopyUnicodeString(&FileName
, &Directory
);
690 RtlAppendUnicodeStringToString(&FileName
, &TempString
);
691 IntGdiAddFontResource(&FileName
, 0);
692 if (DirInfo
->NextEntryOffset
== 0)
694 DirInfo
= (PFILE_DIRECTORY_INFORMATION
)((ULONG_PTR
)DirInfo
+ DirInfo
->NextEntryOffset
);
697 bRestartScan
= FALSE
;
700 ExFreePoolWithTag(FileName
.Buffer
, TAG_FONT
);
701 ExFreePoolWithTag(DirInfoBuffer
, TAG_FONT
);
708 ItalicFromStyle(const char *style_name
)
710 if (style_name
== NULL
|| style_name
[0] == 0)
712 if (strstr(style_name
, "Italic") != NULL
)
714 if (strstr(style_name
, "Oblique") != NULL
)
720 WeightFromStyle(const char *style_name
)
722 if (style_name
== NULL
|| style_name
[0] == 0)
724 if (strstr(style_name
, "Regular") != NULL
)
726 if (strstr(style_name
, "Normal") != NULL
)
728 if (strstr(style_name
, "SemiBold") != NULL
)
730 if (strstr(style_name
, "UltraBold") != NULL
)
732 if (strstr(style_name
, "DemiBold") != NULL
)
734 if (strstr(style_name
, "ExtraBold") != NULL
)
736 if (strstr(style_name
, "Bold") != NULL
)
738 if (strstr(style_name
, "UltraLight") != NULL
)
739 return FW_ULTRALIGHT
;
740 if (strstr(style_name
, "ExtraLight") != NULL
)
741 return FW_EXTRALIGHT
;
742 if (strstr(style_name
, "Light") != NULL
)
744 if (strstr(style_name
, "Hairline") != NULL
)
746 if (strstr(style_name
, "Book") != NULL
)
748 if (strstr(style_name
, "ExtraBlack") != NULL
)
750 if (strstr(style_name
, "UltraBlack") != NULL
)
752 if (strstr(style_name
, "Black") != NULL
)
754 if (strstr(style_name
, "Medium") != NULL
)
756 if (strstr(style_name
, "Thin") != NULL
)
758 if (strstr(style_name
, "Heavy") != NULL
)
764 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont
,
765 PSHARED_FACE SharedFace
, FT_Long FontIndex
, INT CharSetIndex
)
769 FONT_ENTRY_MEM
* PrivateEntry
= NULL
;
773 ANSI_STRING AnsiFaceName
;
774 FT_WinFNT_HeaderRec WinFNT
;
775 INT FaceCount
= 0, CharSetCount
= 0;
776 PUNICODE_STRING pFileName
= pLoadFont
->pFileName
;
777 DWORD Characteristics
= pLoadFont
->Characteristics
;
778 PUNICODE_STRING pValueName
= &pLoadFont
->RegValueName
;
781 FT_UShort os2_version
;
782 FT_ULong os2_ulCodePageRange1
;
783 FT_UShort os2_usWeightClass
;
785 if (SharedFace
== NULL
&& CharSetIndex
== -1)
787 /* load a face from memory */
789 Error
= FT_New_Memory_Face(
791 pLoadFont
->Memory
->Buffer
,
792 pLoadFont
->Memory
->BufferSize
,
793 ((FontIndex
!= -1) ? FontIndex
: 0),
797 SharedFace
= SharedFace_Create(Face
, pLoadFont
->Memory
);
801 if (FT_IS_SFNT(Face
))
802 pLoadFont
->IsTrueType
= TRUE
;
804 if (Error
|| SharedFace
== NULL
)
807 SharedFace_Release(SharedFace
);
809 if (Error
== FT_Err_Unknown_File_Format
)
810 DPRINT1("Unknown font file format\n");
812 DPRINT1("Error reading font (error code: %d)\n", Error
);
813 return 0; /* failure */
818 Face
= SharedFace
->Face
;
820 SharedFace_AddRef(SharedFace
);
824 /* allocate a FONT_ENTRY */
825 Entry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY
), TAG_FONT
);
828 SharedFace_Release(SharedFace
);
829 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
830 return 0; /* failure */
833 /* allocate a FONTGDI */
834 FontGDI
= EngAllocMem(FL_ZERO_MEMORY
, sizeof(FONTGDI
), GDITAG_RFONT
);
837 SharedFace_Release(SharedFace
);
838 ExFreePoolWithTag(Entry
, TAG_FONT
);
839 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
840 return 0; /* failure */
846 FontGDI
->Filename
= ExAllocatePoolWithTag(PagedPool
,
847 pFileName
->Length
+ sizeof(UNICODE_NULL
),
849 if (FontGDI
->Filename
== NULL
)
852 SharedFace_Release(SharedFace
);
853 ExFreePoolWithTag(Entry
, TAG_FONT
);
854 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
855 return 0; /* failure */
857 RtlCopyMemory(FontGDI
->Filename
, pFileName
->Buffer
, pFileName
->Length
);
858 FontGDI
->Filename
[pFileName
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
862 FontGDI
->Filename
= NULL
;
864 PrivateEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_MEM
), TAG_FONT
);
867 if (FontGDI
->Filename
)
868 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
870 SharedFace_Release(SharedFace
);
871 ExFreePoolWithTag(Entry
, TAG_FONT
);
875 PrivateEntry
->Entry
= Entry
;
876 if (pLoadFont
->PrivateEntry
)
878 InsertTailList(&pLoadFont
->PrivateEntry
->ListEntry
, &PrivateEntry
->ListEntry
);
882 InitializeListHead(&PrivateEntry
->ListEntry
);
883 pLoadFont
->PrivateEntry
= PrivateEntry
;
888 FontGDI
->SharedFace
= SharedFace
;
889 FontGDI
->CharSet
= ANSI_CHARSET
;
890 FontGDI
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
891 FontGDI
->RequestItalic
= FALSE
;
892 FontGDI
->OriginalWeight
= WeightFromStyle(Face
->style_name
);
893 FontGDI
->RequestWeight
= FW_NORMAL
;
895 RtlInitAnsiString(&AnsiFaceName
, Face
->family_name
);
896 Status
= RtlAnsiStringToUnicodeString(&Entry
->FaceName
, &AnsiFaceName
, TRUE
);
897 if (!NT_SUCCESS(Status
))
901 if (pLoadFont
->PrivateEntry
== PrivateEntry
)
903 pLoadFont
->PrivateEntry
= NULL
;
907 RemoveEntryList(&PrivateEntry
->ListEntry
);
909 ExFreePoolWithTag(PrivateEntry
, TAG_FONT
);
911 if (FontGDI
->Filename
)
912 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
914 SharedFace_Release(SharedFace
);
915 ExFreePoolWithTag(Entry
, TAG_FONT
);
921 pOS2
= (TT_OS2
*)FT_Get_Sfnt_Table(Face
, FT_SFNT_OS2
);
924 os2_version
= pOS2
->version
;
925 os2_ulCodePageRange1
= pOS2
->ulCodePageRange1
;
926 os2_usWeightClass
= pOS2
->usWeightClass
;
930 if (pOS2
&& os2_version
>= 1)
932 /* get charset and weight from OS/2 header */
934 /* Make sure we do not use this pointer anymore */
937 for (BitIndex
= 0; BitIndex
< MAXTCIINDEX
; ++BitIndex
)
939 if (os2_ulCodePageRange1
& (1 << BitIndex
))
941 if (FontTci
[BitIndex
].ciCharset
== DEFAULT_CHARSET
)
944 if ((CharSetIndex
== -1 && CharSetCount
== 0) ||
945 CharSetIndex
== CharSetCount
)
947 FontGDI
->CharSet
= FontTci
[BitIndex
].ciCharset
;
954 /* set actual weight */
955 FontGDI
->OriginalWeight
= os2_usWeightClass
;
959 /* get charset from WinFNT header */
961 Error
= FT_Get_WinFNT_Header(Face
, &WinFNT
);
964 FontGDI
->CharSet
= WinFNT
.charset
;
969 /* FIXME: CharSet is invalid on Marlett */
970 if (RtlEqualUnicodeString(&Entry
->FaceName
, &MarlettW
, TRUE
))
972 FontGDI
->CharSet
= SYMBOL_CHARSET
;
976 DPRINT("Font loaded: %s (%s)\n", Face
->family_name
, Face
->style_name
);
977 DPRINT("Num glyphs: %d\n", Face
->num_glyphs
);
978 DPRINT("CharSet: %d\n", FontGDI
->CharSet
);
980 /* Add this font resource to the font table */
981 Entry
->Font
= FontGDI
;
982 Entry
->NotEnum
= (Characteristics
& FR_NOT_ENUM
);
984 if (Characteristics
& FR_PRIVATE
)
987 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
988 IntLockProcessPrivateFonts(Win32Process
);
989 InsertTailList(&Win32Process
->PrivateFontListHead
, &Entry
->ListEntry
);
990 IntUnLockProcessPrivateFonts(Win32Process
);
996 InsertTailList(&FontListHead
, &Entry
->ListEntry
);
997 IntUnLockGlobalFonts
;
1000 if (FontIndex
== -1)
1002 if (FT_IS_SFNT(Face
))
1004 TT_Face TrueType
= (TT_Face
)Face
;
1005 if (TrueType
->ttc_header
.count
> 1)
1008 for (i
= 1; i
< TrueType
->ttc_header
.count
; ++i
)
1010 FaceCount
+= IntGdiLoadFontsFromMemory(pLoadFont
, NULL
, i
, -1);
1017 if (CharSetIndex
== -1)
1021 if (pLoadFont
->RegValueName
.Length
== 0)
1023 RtlCreateUnicodeString(pValueName
, Entry
->FaceName
.Buffer
);
1027 UNICODE_STRING NewString
;
1028 USHORT Length
= pValueName
->Length
+ 3 * sizeof(WCHAR
) + Entry
->FaceName
.Length
;
1029 NewString
.Length
= 0;
1030 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1031 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1032 NewString
.MaximumLength
,
1034 NewString
.Buffer
[0] = UNICODE_NULL
;
1036 RtlAppendUnicodeStringToString(&NewString
, pValueName
);
1037 RtlAppendUnicodeToString(&NewString
, L
" & ");
1038 RtlAppendUnicodeStringToString(&NewString
, &Entry
->FaceName
);
1040 RtlFreeUnicodeString(pValueName
);
1041 *pValueName
= NewString
;
1044 for (i
= 1; i
< CharSetCount
; ++i
)
1046 /* Do not count charsets towards 'faces' loaded */
1047 IntGdiLoadFontsFromMemory(pLoadFont
, SharedFace
, FontIndex
, i
);
1051 return FaceCount
; /* number of loaded faces */
1055 * IntGdiAddFontResource
1057 * Adds the font resource from the specified file to the system.
1061 IntGdiAddFontResource(PUNICODE_STRING FileName
, DWORD Characteristics
)
1065 PVOID Buffer
= NULL
;
1066 IO_STATUS_BLOCK Iosb
;
1067 PVOID SectionObject
;
1069 LARGE_INTEGER SectionSize
;
1070 OBJECT_ATTRIBUTES ObjectAttributes
;
1071 GDI_LOAD_FONT LoadFont
;
1074 static const UNICODE_STRING TrueTypePostfix
= RTL_CONSTANT_STRING(L
" (TrueType)");
1076 /* Open the font file */
1077 InitializeObjectAttributes(&ObjectAttributes
, FileName
, 0, NULL
, NULL
);
1078 Status
= ZwOpenFile(
1080 FILE_GENERIC_READ
| SYNCHRONIZE
,
1084 FILE_SYNCHRONOUS_IO_NONALERT
);
1085 if (!NT_SUCCESS(Status
))
1087 DPRINT("Could not load font file: %wZ\n", FileName
);
1091 SectionSize
.QuadPart
= 0LL;
1092 Status
= MmCreateSection(&SectionObject
, SECTION_ALL_ACCESS
,
1093 NULL
, &SectionSize
, PAGE_READONLY
,
1094 SEC_COMMIT
, FileHandle
, NULL
);
1095 if (!NT_SUCCESS(Status
))
1097 DPRINT("Could not map file: %wZ\n", FileName
);
1098 ZwClose(FileHandle
);
1101 ZwClose(FileHandle
);
1103 Status
= MmMapViewInSystemSpace(SectionObject
, &Buffer
, &ViewSize
);
1104 if (!NT_SUCCESS(Status
))
1106 DPRINT("Could not map file: %wZ\n", FileName
);
1107 ObDereferenceObject(SectionObject
);
1111 LoadFont
.pFileName
= FileName
;
1112 LoadFont
.Memory
= SharedMem_Create(Buffer
, ViewSize
, TRUE
);
1113 LoadFont
.Characteristics
= Characteristics
;
1114 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1115 LoadFont
.IsTrueType
= FALSE
;
1116 LoadFont
.PrivateEntry
= NULL
;
1117 FontCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1119 ObDereferenceObject(SectionObject
);
1121 /* Release our copy */
1123 SharedMem_Release(LoadFont
.Memory
);
1128 if (LoadFont
.IsTrueType
)
1130 /* append " (TrueType)" */
1131 UNICODE_STRING NewString
;
1134 Length
= LoadFont
.RegValueName
.Length
+ TrueTypePostfix
.Length
;
1135 NewString
.Length
= 0;
1136 NewString
.MaximumLength
= Length
+ sizeof(WCHAR
);
1137 NewString
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1138 NewString
.MaximumLength
,
1140 NewString
.Buffer
[0] = UNICODE_NULL
;
1142 RtlAppendUnicodeStringToString(&NewString
, &LoadFont
.RegValueName
);
1143 RtlAppendUnicodeStringToString(&NewString
, &TrueTypePostfix
);
1144 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1145 LoadFont
.RegValueName
= NewString
;
1149 InitializeObjectAttributes(&ObjectAttributes
, &FontRegPath
,
1150 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1152 Status
= ZwOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
1153 if (NT_SUCCESS(Status
))
1156 LPWSTR pFileName
= wcsrchr(FileName
->Buffer
, L
'\\');
1160 DataSize
= (wcslen(pFileName
) + 1) * sizeof(WCHAR
);
1161 ZwSetValueKey(KeyHandle
, &LoadFont
.RegValueName
, 0, REG_SZ
,
1162 pFileName
, DataSize
);
1167 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1173 IntGdiAddFontMemResource(PVOID Buffer
, DWORD dwSize
, PDWORD pNumAdded
)
1175 GDI_LOAD_FONT LoadFont
;
1176 FONT_ENTRY_COLL_MEM
* EntryCollection
;
1180 PVOID BufferCopy
= ExAllocatePoolWithTag(PagedPool
, dwSize
, TAG_FONT
);
1187 memcpy(BufferCopy
, Buffer
, dwSize
);
1189 LoadFont
.pFileName
= NULL
;
1190 LoadFont
.Memory
= SharedMem_Create(BufferCopy
, dwSize
, FALSE
);
1191 LoadFont
.Characteristics
= FR_PRIVATE
| FR_NOT_ENUM
;
1192 RtlInitUnicodeString(&LoadFont
.RegValueName
, NULL
);
1193 LoadFont
.IsTrueType
= FALSE
;
1194 LoadFont
.PrivateEntry
= NULL
;
1195 FaceCount
= IntGdiLoadFontsFromMemory(&LoadFont
, NULL
, -1, -1);
1197 RtlFreeUnicodeString(&LoadFont
.RegValueName
);
1199 /* Release our copy */
1201 SharedMem_Release(LoadFont
.Memory
);
1206 EntryCollection
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_ENTRY_COLL_MEM
), TAG_FONT
);
1207 if (EntryCollection
)
1209 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1210 EntryCollection
->Entry
= LoadFont
.PrivateEntry
;
1211 IntLockProcessPrivateFonts(Win32Process
);
1212 EntryCollection
->Handle
= ++Win32Process
->PrivateMemFontHandleCount
;
1213 InsertTailList(&Win32Process
->PrivateMemFontListHead
, &EntryCollection
->ListEntry
);
1214 IntUnLockProcessPrivateFonts(Win32Process
);
1215 Ret
= (HANDLE
)EntryCollection
->Handle
;
1218 *pNumAdded
= FaceCount
;
1223 // FIXME: Add RemoveFontResource
1225 static VOID FASTCALL
1226 CleanupFontEntry(PFONT_ENTRY FontEntry
)
1228 PFONTGDI FontGDI
= FontEntry
->Font
;
1229 PSHARED_FACE SharedFace
= FontGDI
->SharedFace
;
1231 if (FontGDI
->Filename
)
1232 ExFreePoolWithTag(FontGDI
->Filename
, GDITAG_PFF
);
1234 EngFreeMem(FontGDI
);
1235 SharedFace_Release(SharedFace
);
1236 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1240 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head
)
1243 PFONT_ENTRY_MEM FontEntry
;
1245 while (!IsListEmpty(&Head
->ListEntry
))
1247 Entry
= RemoveHeadList(&Head
->ListEntry
);
1248 FontEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_MEM
, ListEntry
);
1250 CleanupFontEntry(FontEntry
->Entry
);
1251 ExFreePoolWithTag(FontEntry
, TAG_FONT
);
1254 CleanupFontEntry(Head
->Entry
);
1255 ExFreePoolWithTag(Head
, TAG_FONT
);
1258 static VOID FASTCALL
1259 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection
)
1261 PFONT_ENTRY_MEM FontMemEntry
= Collection
->Entry
;
1262 PLIST_ENTRY ListEntry
;
1263 RemoveEntryList(&Collection
->ListEntry
);
1266 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1267 RemoveEntryList(&FontMemEntry
->Entry
->ListEntry
);
1269 ListEntry
= FontMemEntry
->ListEntry
.Flink
;
1270 FontMemEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY_MEM
, ListEntry
);
1272 } while (FontMemEntry
!= Collection
->Entry
);
1276 IntGdiRemoveFontMemResource(HANDLE hMMFont
)
1279 PFONT_ENTRY_COLL_MEM CurrentEntry
;
1280 PFONT_ENTRY_COLL_MEM EntryCollection
= NULL
;
1281 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1283 IntLockProcessPrivateFonts(Win32Process
);
1284 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1285 while (Entry
!= &Win32Process
->PrivateMemFontListHead
)
1287 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1289 if (CurrentEntry
->Handle
== (UINT
)hMMFont
)
1291 EntryCollection
= CurrentEntry
;
1292 UnlinkFontMemCollection(CurrentEntry
);
1296 Entry
= Entry
->Flink
;
1298 IntUnLockProcessPrivateFonts(Win32Process
);
1300 if (EntryCollection
)
1302 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1303 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1311 IntGdiCleanupPrivateFontsForProcess(VOID
)
1313 PPROCESSINFO Win32Process
= PsGetCurrentProcessWin32Process();
1315 PFONT_ENTRY_COLL_MEM EntryCollection
;
1317 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1320 EntryCollection
= NULL
;
1322 IntLockProcessPrivateFonts(Win32Process
);
1323 if (!IsListEmpty(&Win32Process
->PrivateMemFontListHead
))
1325 Entry
= Win32Process
->PrivateMemFontListHead
.Flink
;
1326 EntryCollection
= CONTAINING_RECORD(Entry
, FONT_ENTRY_COLL_MEM
, ListEntry
);
1327 UnlinkFontMemCollection(EntryCollection
);
1329 IntUnLockProcessPrivateFonts(Win32Process
);
1331 if (EntryCollection
)
1333 IntGdiCleanupMemEntry(EntryCollection
->Entry
);
1334 ExFreePoolWithTag(EntryCollection
, TAG_FONT
);
1338 /* No Mem fonts anymore, see if we have any other private fonts left */
1340 IntLockProcessPrivateFonts(Win32Process
);
1341 if (!IsListEmpty(&Win32Process
->PrivateFontListHead
))
1343 Entry
= RemoveHeadList(&Win32Process
->PrivateFontListHead
);
1345 IntUnLockProcessPrivateFonts(Win32Process
);
1349 CleanupFontEntry(CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
));
1357 IntIsFontRenderingEnabled(VOID
)
1359 BOOL Ret
= RenderingEnabled
;
1362 hDC
= IntGetScreenDC();
1364 Ret
= (NtGdiGetDeviceCaps(hDC
, BITSPIXEL
) > 8) && RenderingEnabled
;
1370 IntEnableFontRendering(BOOL Enable
)
1372 RenderingEnabled
= Enable
;
1375 FT_Render_Mode FASTCALL
1376 IntGetFontRenderMode(LOGFONTW
*logfont
)
1378 switch (logfont
->lfQuality
)
1380 case ANTIALIASED_QUALITY
:
1381 case NONANTIALIASED_QUALITY
:
1382 return FT_RENDER_MODE_MONO
;
1384 return FT_RENDER_MODE_LIGHT
;
1385 /* case CLEARTYPE_QUALITY:
1386 return FT_RENDER_MODE_LCD; */
1388 return FT_RENDER_MODE_NORMAL
;
1393 TextIntCreateFontIndirect(CONST LPLOGFONTW lf
, HFONT
*NewFont
)
1398 plfont
= LFONT_AllocFontWithHandle();
1401 return STATUS_NO_MEMORY
;
1404 ExInitializePushLock(&plfont
->lock
);
1405 *NewFont
= plfont
->BaseObject
.hHmgr
;
1406 plf
= &plfont
->logfont
.elfEnumLogfontEx
.elfLogFont
;
1407 RtlCopyMemory(plf
, lf
, sizeof(LOGFONTW
));
1408 if (lf
->lfEscapement
!= lf
->lfOrientation
)
1410 /* This should really depend on whether GM_ADVANCED is set */
1411 plf
->lfOrientation
= plf
->lfEscapement
;
1413 LFONT_UnlockFont(plfont
);
1415 return STATUS_SUCCESS
;
1418 /*************************************************************************
1419 * TranslateCharsetInfo
1421 * Fills a CHARSETINFO structure for a character set, code page, or
1422 * font. This allows making the correspondance between different labelings
1423 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
1424 * of the same encoding.
1426 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1427 * only one codepage should be set in *Src.
1430 * TRUE on success, FALSE on failure.
1433 static BOOLEAN APIENTRY
1434 IntTranslateCharsetInfo(PDWORD Src
, /* [in]
1435 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
1436 if flags == TCI_SRCCHARSET: a character set value
1437 if flags == TCI_SRCCODEPAGE: a code page value */
1438 LPCHARSETINFO Cs
, /* [out] structure to receive charset information */
1439 DWORD Flags
/* [in] determines interpretation of lpSrc */)
1445 case TCI_SRCFONTSIG
:
1446 while (Index
< MAXTCIINDEX
&& 0 == (*Src
>> Index
& 0x0001))
1451 case TCI_SRCCODEPAGE
:
1452 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciACP
)
1457 case TCI_SRCCHARSET
:
1458 while (Index
< MAXTCIINDEX
&& *Src
!= FontTci
[Index
].ciCharset
)
1467 if (Index
>= MAXTCIINDEX
|| DEFAULT_CHARSET
== FontTci
[Index
].ciCharset
)
1472 RtlCopyMemory(Cs
, &FontTci
[Index
], sizeof(CHARSETINFO
));
1478 static BOOL
face_has_symbol_charmap(FT_Face ft_face
)
1482 for(i
= 0; i
< ft_face
->num_charmaps
; i
++)
1484 if(ft_face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
)
1491 static void FASTCALL
1492 FillTMEx(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1493 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1494 FT_WinFNT_HeaderRec
*pFNT
, BOOL RealFont
)
1496 FT_Fixed XScale
, YScale
;
1497 int Ascent
, Descent
;
1498 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1500 XScale
= Face
->size
->metrics
.x_scale
;
1501 YScale
= Face
->size
->metrics
.y_scale
;
1505 TM
->tmHeight
= pFNT
->pixel_height
;
1506 TM
->tmAscent
= pFNT
->ascent
;
1507 TM
->tmDescent
= TM
->tmHeight
- TM
->tmAscent
;
1508 TM
->tmInternalLeading
= pFNT
->internal_leading
;
1509 TM
->tmExternalLeading
= pFNT
->external_leading
;
1510 TM
->tmAveCharWidth
= pFNT
->avg_width
;
1511 TM
->tmMaxCharWidth
= pFNT
->max_width
;
1513 TM
->tmDigitizedAspectX
= pFNT
->horizontal_resolution
;
1514 TM
->tmDigitizedAspectY
= pFNT
->vertical_resolution
;
1515 TM
->tmFirstChar
= pFNT
->first_char
;
1516 TM
->tmLastChar
= pFNT
->last_char
;
1517 TM
->tmDefaultChar
= pFNT
->default_char
+ pFNT
->first_char
;
1518 TM
->tmBreakChar
= pFNT
->break_char
+ pFNT
->first_char
;
1519 TM
->tmPitchAndFamily
= pFNT
->pitch_and_family
;
1522 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1523 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1524 TM
->tmUnderlined
= pFNT
->underline
;
1525 TM
->tmStruckOut
= pFNT
->strike_out
;
1526 TM
->tmCharSet
= pFNT
->charset
;
1530 TM
->tmWeight
= FontGDI
->RequestWeight
;
1531 TM
->tmItalic
= FontGDI
->RequestItalic
;
1532 TM
->tmUnderlined
= FontGDI
->RequestUnderline
;
1533 TM
->tmStruckOut
= FontGDI
->RequestStrikeOut
;
1534 TM
->tmCharSet
= FontGDI
->CharSet
;
1539 if (pOS2
->usWinAscent
+ pOS2
->usWinDescent
== 0)
1541 Ascent
= pHori
->Ascender
;
1542 Descent
= -pHori
->Descender
;
1546 Ascent
= pOS2
->usWinAscent
;
1547 Descent
= pOS2
->usWinDescent
;
1550 #if 0 /* This (Wine) code doesn't seem to work correctly for us, cmd issue */
1551 TM
->tmAscent
= (FT_MulFix(Ascent
, YScale
) + 32) >> 6;
1552 TM
->tmDescent
= (FT_MulFix(Descent
, YScale
) + 32) >> 6;
1553 #else /* This (ros) code was previously affected by a FreeType bug, but it works now */
1554 TM
->tmAscent
= (Face
->size
->metrics
.ascender
+ 32) >> 6; /* Units above baseline */
1555 TM
->tmDescent
= (32 - Face
->size
->metrics
.descender
) >> 6; /* Units below baseline */
1557 TM
->tmInternalLeading
= (FT_MulFix(Ascent
+ Descent
- Face
->units_per_EM
, YScale
) + 32) >> 6;
1559 TM
->tmHeight
= TM
->tmAscent
+ TM
->tmDescent
;
1562 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1564 TM
->tmExternalLeading
= max(0, (FT_MulFix(pHori
->Line_Gap
1565 - ((Ascent
+ Descent
)
1566 - (pHori
->Ascender
- pHori
->Descender
)),
1567 YScale
) + 32) >> 6);
1569 TM
->tmAveCharWidth
= (FT_MulFix(pOS2
->xAvgCharWidth
, XScale
) + 32) >> 6;
1570 if (TM
->tmAveCharWidth
== 0)
1572 TM
->tmAveCharWidth
= 1;
1575 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1576 TM
->tmMaxCharWidth
= (FT_MulFix(Face
->max_advance_width
, XScale
) + 32) >> 6;
1580 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1584 if (FontGDI
->OriginalWeight
!= FW_DONTCARE
&&
1585 FontGDI
->OriginalWeight
!= FW_NORMAL
)
1587 TM
->tmWeight
= FontGDI
->OriginalWeight
;
1591 TM
->tmWeight
= FontGDI
->RequestWeight
;
1596 TM
->tmDigitizedAspectX
= 96;
1597 TM
->tmDigitizedAspectY
= 96;
1598 if (face_has_symbol_charmap(Face
) ||
1599 (pOS2
->usFirstCharIndex
>= 0xf000 && pOS2
->usFirstCharIndex
< 0xf100))
1601 USHORT cpOEM
, cpAnsi
;
1603 EngGetCurrentCodePage(&cpOEM
, &cpAnsi
);
1604 TM
->tmFirstChar
= 0;
1607 case 1257: /* Baltic */
1608 TM
->tmLastChar
= 0xf8fd;
1611 TM
->tmLastChar
= 0xf0ff;
1613 TM
->tmBreakChar
= 0x20;
1614 TM
->tmDefaultChar
= 0x1f;
1618 TM
->tmFirstChar
= pOS2
->usFirstCharIndex
; /* Should be the first char in the cmap */
1619 TM
->tmLastChar
= pOS2
->usLastCharIndex
; /* Should be min(cmap_last, os2_last) */
1621 if(pOS2
->usFirstCharIndex
<= 1)
1622 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
+ 2;
1623 else if (pOS2
->usFirstCharIndex
> 0xff)
1624 TM
->tmBreakChar
= 0x20;
1626 TM
->tmBreakChar
= pOS2
->usFirstCharIndex
;
1627 TM
->tmDefaultChar
= TM
->tmBreakChar
- 1;
1632 TM
->tmItalic
= FontGDI
->OriginalItalic
;
1633 TM
->tmUnderlined
= FALSE
;
1634 TM
->tmStruckOut
= FALSE
;
1638 if (FontGDI
->OriginalItalic
|| FontGDI
->RequestItalic
)
1640 TM
->tmItalic
= 0xFF;
1646 TM
->tmUnderlined
= (FontGDI
->RequestUnderline
? 0xFF : 0);
1647 TM
->tmStruckOut
= (FontGDI
->RequestStrikeOut
? 0xFF : 0);
1650 if (!FT_IS_FIXED_WIDTH(Face
))
1652 switch (pOS2
->panose
[PAN_PROPORTION_INDEX
])
1654 case PAN_PROP_MONOSPACED
:
1655 TM
->tmPitchAndFamily
= 0;
1658 TM
->tmPitchAndFamily
= _TMPF_VARIABLE_PITCH
;
1664 TM
->tmPitchAndFamily
= 0;
1667 switch (pOS2
->panose
[PAN_FAMILYTYPE_INDEX
])
1669 case PAN_FAMILY_SCRIPT
:
1670 TM
->tmPitchAndFamily
|= FF_SCRIPT
;
1672 case PAN_FAMILY_DECORATIVE
:
1673 TM
->tmPitchAndFamily
|= FF_DECORATIVE
;
1678 case PAN_FAMILY_TEXT_DISPLAY
:
1679 case PAN_FAMILY_PICTORIAL
: /* Symbol fonts get treated as if they were text */
1680 /* Which is clearly not what the panose spec says. */
1681 if (TM
->tmPitchAndFamily
== 0) /* Fixed */
1683 TM
->tmPitchAndFamily
= FF_MODERN
;
1687 switch (pOS2
->panose
[PAN_SERIFSTYLE_INDEX
])
1692 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1695 case PAN_SERIF_COVE
:
1696 case PAN_SERIF_OBTUSE_COVE
:
1697 case PAN_SERIF_SQUARE_COVE
:
1698 case PAN_SERIF_OBTUSE_SQUARE_COVE
:
1699 case PAN_SERIF_SQUARE
:
1700 case PAN_SERIF_THIN
:
1701 case PAN_SERIF_BONE
:
1702 case PAN_SERIF_EXAGGERATED
:
1703 case PAN_SERIF_TRIANGLE
:
1704 TM
->tmPitchAndFamily
|= FF_ROMAN
;
1707 case PAN_SERIF_NORMAL_SANS
:
1708 case PAN_SERIF_OBTUSE_SANS
:
1709 case PAN_SERIF_PERP_SANS
:
1710 case PAN_SERIF_FLARED
:
1711 case PAN_SERIF_ROUNDED
:
1712 TM
->tmPitchAndFamily
|= FF_SWISS
;
1718 TM
->tmPitchAndFamily
|= FF_DONTCARE
;
1721 if (FT_IS_SCALABLE(Face
))
1723 TM
->tmPitchAndFamily
|= TMPF_VECTOR
;
1725 if (FT_IS_SFNT(Face
))
1727 TM
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
1730 TM
->tmCharSet
= FontGDI
->CharSet
;
1733 static void FASTCALL
1734 FillTM(TEXTMETRICW
*TM
, PFONTGDI FontGDI
,
1735 TT_OS2
*pOS2
, TT_HoriHeader
*pHori
,
1736 FT_WinFNT_HeaderRec
*pFNT
)
1738 FillTMEx(TM
, FontGDI
, pOS2
, pHori
, pFNT
, FALSE
);
1741 /*************************************************************
1742 * IntGetOutlineTextMetrics
1746 IntGetOutlineTextMetrics(PFONTGDI FontGDI
,
1748 OUTLINETEXTMETRICW
*Otm
)
1752 TT_HoriHeader
*pHori
;
1753 TT_Postscript
*pPost
;
1754 FT_Fixed XScale
, YScale
;
1755 ANSI_STRING FamilyNameA
, StyleNameA
;
1756 UNICODE_STRING FamilyNameW
, StyleNameW
, Regular
;
1757 FT_WinFNT_HeaderRec Win
;
1761 FT_Face Face
= FontGDI
->SharedFace
->Face
;
1763 Needed
= sizeof(OUTLINETEXTMETRICW
);
1765 RtlInitAnsiString(&FamilyNameA
, Face
->family_name
);
1766 status
= RtlAnsiStringToUnicodeString(&FamilyNameW
, &FamilyNameA
, TRUE
);
1767 if (!NT_SUCCESS(status
))
1772 RtlInitAnsiString(&StyleNameA
, Face
->style_name
);
1773 status
= RtlAnsiStringToUnicodeString(&StyleNameW
, &StyleNameA
, TRUE
);
1774 if (!NT_SUCCESS(status
))
1776 RtlFreeUnicodeString(&FamilyNameW
);
1780 /* These names should be read from the TT name table */
1782 /* Length of otmpFamilyName */
1783 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1785 RtlInitUnicodeString(&Regular
, L
"Regular");
1786 /* Length of otmpFaceName */
1787 if (RtlEqualUnicodeString(&StyleNameW
, &Regular
, TRUE
))
1789 Needed
+= FamilyNameW
.Length
+ sizeof(WCHAR
); /* Just the family name */
1793 Needed
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1); /* family + " " + style */
1796 /* Length of otmpStyleName */
1797 Needed
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1799 /* Length of otmpFullName */
1800 Needed
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1);
1804 RtlFreeUnicodeString(&FamilyNameW
);
1805 RtlFreeUnicodeString(&StyleNameW
);
1809 XScale
= Face
->size
->metrics
.x_scale
;
1810 YScale
= Face
->size
->metrics
.y_scale
;
1813 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
1817 DPRINT1("Can't find OS/2 table - not TT font?\n");
1818 RtlFreeUnicodeString(&StyleNameW
);
1819 RtlFreeUnicodeString(&FamilyNameW
);
1823 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
1827 DPRINT1("Can't find HHEA table - not TT font?\n");
1828 RtlFreeUnicodeString(&StyleNameW
);
1829 RtlFreeUnicodeString(&FamilyNameW
);
1833 pPost
= FT_Get_Sfnt_Table(Face
, ft_sfnt_post
); /* We can live with this failing */
1835 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
1837 Otm
->otmSize
= Needed
;
1839 FillTM(&Otm
->otmTextMetrics
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
1842 RtlCopyMemory(&Otm
->otmPanoseNumber
, pOS2
->panose
, PANOSE_COUNT
);
1843 Otm
->otmfsSelection
= pOS2
->fsSelection
;
1844 Otm
->otmfsType
= pOS2
->fsType
;
1845 Otm
->otmsCharSlopeRise
= pHori
->caret_Slope_Rise
;
1846 Otm
->otmsCharSlopeRun
= pHori
->caret_Slope_Run
;
1847 Otm
->otmItalicAngle
= 0; /* POST table */
1848 Otm
->otmEMSquare
= Face
->units_per_EM
;
1849 Otm
->otmAscent
= (FT_MulFix(pOS2
->sTypoAscender
, YScale
) + 32) >> 6;
1850 Otm
->otmDescent
= (FT_MulFix(pOS2
->sTypoDescender
, YScale
) + 32) >> 6;
1851 Otm
->otmLineGap
= (FT_MulFix(pOS2
->sTypoLineGap
, YScale
) + 32) >> 6;
1852 Otm
->otmsCapEmHeight
= (FT_MulFix(pOS2
->sCapHeight
, YScale
) + 32) >> 6;
1853 Otm
->otmsXHeight
= (FT_MulFix(pOS2
->sxHeight
, YScale
) + 32) >> 6;
1854 Otm
->otmrcFontBox
.left
= (FT_MulFix(Face
->bbox
.xMin
, XScale
) + 32) >> 6;
1855 Otm
->otmrcFontBox
.right
= (FT_MulFix(Face
->bbox
.xMax
, XScale
) + 32) >> 6;
1856 Otm
->otmrcFontBox
.top
= (FT_MulFix(Face
->bbox
.yMax
, YScale
) + 32) >> 6;
1857 Otm
->otmrcFontBox
.bottom
= (FT_MulFix(Face
->bbox
.yMin
, YScale
) + 32) >> 6;
1858 Otm
->otmMacAscent
= Otm
->otmTextMetrics
.tmAscent
;
1859 Otm
->otmMacDescent
= -Otm
->otmTextMetrics
.tmDescent
;
1860 Otm
->otmMacLineGap
= Otm
->otmLineGap
;
1861 Otm
->otmusMinimumPPEM
= 0; /* TT Header */
1862 Otm
->otmptSubscriptSize
.x
= (FT_MulFix(pOS2
->ySubscriptXSize
, XScale
) + 32) >> 6;
1863 Otm
->otmptSubscriptSize
.y
= (FT_MulFix(pOS2
->ySubscriptYSize
, YScale
) + 32) >> 6;
1864 Otm
->otmptSubscriptOffset
.x
= (FT_MulFix(pOS2
->ySubscriptXOffset
, XScale
) + 32) >> 6;
1865 Otm
->otmptSubscriptOffset
.y
= (FT_MulFix(pOS2
->ySubscriptYOffset
, YScale
) + 32) >> 6;
1866 Otm
->otmptSuperscriptSize
.x
= (FT_MulFix(pOS2
->ySuperscriptXSize
, XScale
) + 32) >> 6;
1867 Otm
->otmptSuperscriptSize
.y
= (FT_MulFix(pOS2
->ySuperscriptYSize
, YScale
) + 32) >> 6;
1868 Otm
->otmptSuperscriptOffset
.x
= (FT_MulFix(pOS2
->ySuperscriptXOffset
, XScale
) + 32) >> 6;
1869 Otm
->otmptSuperscriptOffset
.y
= (FT_MulFix(pOS2
->ySuperscriptYOffset
, YScale
) + 32) >> 6;
1870 Otm
->otmsStrikeoutSize
= (FT_MulFix(pOS2
->yStrikeoutSize
, YScale
) + 32) >> 6;
1871 Otm
->otmsStrikeoutPosition
= (FT_MulFix(pOS2
->yStrikeoutPosition
, YScale
) + 32) >> 6;
1874 Otm
->otmsUnderscoreSize
= 0;
1875 Otm
->otmsUnderscorePosition
= 0;
1879 Otm
->otmsUnderscoreSize
= (FT_MulFix(pPost
->underlineThickness
, YScale
) + 32) >> 6;
1880 Otm
->otmsUnderscorePosition
= (FT_MulFix(pPost
->underlinePosition
, YScale
) + 32) >> 6;
1885 /* otmp* members should clearly have type ptrdiff_t, but M$ knows best */
1886 Cp
= (char*) Otm
+ sizeof(OUTLINETEXTMETRICW
);
1887 Otm
->otmpFamilyName
= (LPSTR
)(Cp
- (char*) Otm
);
1888 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1889 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1890 Otm
->otmpStyleName
= (LPSTR
)(Cp
- (char*) Otm
);
1891 wcscpy((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1892 Cp
+= StyleNameW
.Length
+ sizeof(WCHAR
);
1893 Otm
->otmpFaceName
= (LPSTR
)(Cp
- (char*) Otm
);
1894 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1895 if (!RtlEqualUnicodeString(&StyleNameW
, &Regular
, TRUE
))
1897 wcscat((WCHAR
*) Cp
, L
" ");
1898 wcscat((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1899 Cp
+= FamilyNameW
.Length
+ StyleNameW
.Length
+ (sizeof(WCHAR
) << 1);
1903 Cp
+= FamilyNameW
.Length
+ sizeof(WCHAR
);
1905 Otm
->otmpFullName
= (LPSTR
)(Cp
- (char*) Otm
);
1906 wcscpy((WCHAR
*) Cp
, FamilyNameW
.Buffer
);
1907 wcscat((WCHAR
*) Cp
, L
" ");
1908 wcscat((WCHAR
*) Cp
, StyleNameW
.Buffer
);
1910 RtlFreeUnicodeString(&StyleNameW
);
1911 RtlFreeUnicodeString(&FamilyNameW
);
1916 static PFONTGDI FASTCALL
1917 FindFaceNameInList(PUNICODE_STRING FaceName
, PLIST_ENTRY Head
)
1920 PFONT_ENTRY CurrentEntry
;
1921 ANSI_STRING EntryFaceNameA
;
1922 UNICODE_STRING EntryFaceNameW
;
1926 Entry
= Head
->Flink
;
1927 while (Entry
!= Head
)
1929 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
1931 FontGDI
= CurrentEntry
->Font
;
1934 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
1935 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
1936 if (!NT_SUCCESS(status
))
1941 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
1943 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
1944 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
1947 if (RtlEqualUnicodeString(FaceName
, &EntryFaceNameW
, TRUE
))
1949 RtlFreeUnicodeString(&EntryFaceNameW
);
1953 RtlFreeUnicodeString(&EntryFaceNameW
);
1954 Entry
= Entry
->Flink
;
1960 static PFONTGDI FASTCALL
1961 FindFaceNameInLists(PUNICODE_STRING FaceName
)
1963 PPROCESSINFO Win32Process
;
1966 /* Search the process local list.
1967 We do not have to search the 'Mem' list, since those fonts are linked in the PrivateFontListHead */
1968 Win32Process
= PsGetCurrentProcessWin32Process();
1969 IntLockProcessPrivateFonts(Win32Process
);
1970 Font
= FindFaceNameInList(FaceName
, &Win32Process
->PrivateFontListHead
);
1971 IntUnLockProcessPrivateFonts(Win32Process
);
1977 /* Search the global list */
1979 Font
= FindFaceNameInList(FaceName
, &FontListHead
);
1980 IntUnLockGlobalFonts
;
1985 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
1987 CharSetFromLangID(LANGID LangID
)
1989 /* FIXME: Add more and fix if wrong */
1990 switch (PRIMARYLANGID(LangID
))
1993 switch (SUBLANGID(LangID
))
1995 case SUBLANG_CHINESE_TRADITIONAL
:
1996 return CHINESEBIG5_CHARSET
;
1997 case SUBLANG_CHINESE_SIMPLIFIED
:
2001 return GB2312_CHARSET
;
2003 case LANG_CZECH
: case LANG_HUNGARIAN
: case LANG_POLISH
:
2004 case LANG_SLOVAK
: case LANG_SLOVENIAN
: case LANG_ROMANIAN
:
2005 return EASTEUROPE_CHARSET
;
2007 case LANG_RUSSIAN
: case LANG_BULGARIAN
: case LANG_MACEDONIAN
:
2008 case LANG_SERBIAN
: case LANG_UKRAINIAN
:
2009 return RUSSIAN_CHARSET
;
2011 case LANG_ARABIC
: return ARABIC_CHARSET
;
2012 case LANG_GREEK
: return GREEK_CHARSET
;
2013 case LANG_HEBREW
: return HEBREW_CHARSET
;
2014 case LANG_JAPANESE
: return SHIFTJIS_CHARSET
;
2015 case LANG_KOREAN
: return JOHAB_CHARSET
;
2016 case LANG_TURKISH
: return TURKISH_CHARSET
;
2017 case LANG_THAI
: return THAI_CHARSET
;
2018 case LANG_LATVIAN
: return BALTIC_CHARSET
;
2019 case LANG_VIETNAMESE
: return VIETNAMESE_CHARSET
;
2021 case LANG_ENGLISH
: case LANG_BASQUE
: case LANG_CATALAN
:
2022 case LANG_DANISH
: case LANG_DUTCH
: case LANG_FINNISH
:
2023 case LANG_FRENCH
: case LANG_GERMAN
: case LANG_ITALIAN
:
2024 case LANG_NORWEGIAN
: case LANG_PORTUGUESE
: case LANG_SPANISH
:
2025 case LANG_SWEDISH
: default:
2026 return ANSI_CHARSET
;
2031 SwapEndian(LPVOID pvData
, DWORD Size
)
2033 BYTE b
, *pb
= pvData
;
2045 IntGetFontLocalizedName(PUNICODE_STRING pNameW
, FT_Face Face
,
2046 FT_UShort NameID
, FT_UShort LangID
)
2050 WCHAR Buf
[LF_FULLFACESIZE
];
2052 NTSTATUS Status
= STATUS_NOT_FOUND
;
2053 ANSI_STRING AnsiName
;
2055 RtlFreeUnicodeString(pNameW
);
2057 Count
= FT_Get_Sfnt_Name_Count(Face
);
2058 for (i
= 0; i
< Count
; ++i
)
2060 Error
= FT_Get_Sfnt_Name(Face
, i
, &Name
);
2064 if (Name
.platform_id
!= TT_PLATFORM_MICROSOFT
||
2065 Name
.encoding_id
!= TT_MS_ID_UNICODE_CS
)
2067 continue; /* not Microsoft Unicode name */
2070 if (Name
.name_id
!= NameID
|| Name
.language_id
!= LangID
)
2072 continue; /* mismatched */
2075 if (Name
.string
== NULL
|| Name
.string_len
== 0 ||
2076 (Name
.string
[0] == 0 && Name
.string
[1] == 0))
2078 continue; /* invalid string */
2081 if (sizeof(Buf
) < Name
.string_len
+ sizeof(UNICODE_NULL
))
2083 continue; /* name too long */
2086 /* NOTE: Name.string is not null-terminated */
2087 RtlCopyMemory(Buf
, Name
.string
, Name
.string_len
);
2088 Buf
[Name
.string_len
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2090 /* Convert UTF-16 big endian to little endian */
2091 SwapEndian(Buf
, Name
.string_len
);
2093 RtlCreateUnicodeString(pNameW
, Buf
);
2094 Status
= STATUS_SUCCESS
;
2098 if (Status
== STATUS_NOT_FOUND
)
2100 if (LangID
!= gusEnglishUS
)
2102 Status
= IntGetFontLocalizedName(pNameW
, Face
, NameID
, gusEnglishUS
);
2105 if (Status
== STATUS_NOT_FOUND
)
2107 RtlInitAnsiString(&AnsiName
, Face
->family_name
);
2108 Status
= RtlAnsiStringToUnicodeString(pNameW
, &AnsiName
, TRUE
);
2114 static void FASTCALL
2115 FontFamilyFillInfo(PFONTFAMILYINFO Info
, LPCWSTR FaceName
,
2116 LPCWSTR FullName
, PFONTGDI FontGDI
)
2119 UNICODE_STRING StyleW
;
2122 CHARSETINFO CharSetInfo
;
2124 OUTLINETEXTMETRICW
*Otm
;
2127 NEWTEXTMETRICW
*Ntm
;
2130 FT_Face Face
= FontGDI
->SharedFace
->Face
;
2131 UNICODE_STRING NameW
;
2133 RtlInitUnicodeString(&NameW
, NULL
);
2134 RtlZeroMemory(Info
, sizeof(FONTFAMILYINFO
));
2135 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
2136 Otm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
2141 IntGetOutlineTextMetrics(FontGDI
, Size
, Otm
);
2143 Lf
= &Info
->EnumLogFontEx
.elfLogFont
;
2144 TM
= &Otm
->otmTextMetrics
;
2146 Lf
->lfHeight
= TM
->tmHeight
;
2147 Lf
->lfWidth
= TM
->tmAveCharWidth
;
2148 Lf
->lfWeight
= TM
->tmWeight
;
2149 Lf
->lfItalic
= TM
->tmItalic
;
2150 Lf
->lfPitchAndFamily
= (TM
->tmPitchAndFamily
& 0xf1) + 1;
2151 Lf
->lfCharSet
= TM
->tmCharSet
;
2152 Lf
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
2153 Lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
2154 Lf
->lfQuality
= PROOF_QUALITY
;
2156 Ntm
= &Info
->NewTextMetricEx
.ntmTm
;
2157 Ntm
->tmHeight
= TM
->tmHeight
;
2158 Ntm
->tmAscent
= TM
->tmAscent
;
2159 Ntm
->tmDescent
= TM
->tmDescent
;
2160 Ntm
->tmInternalLeading
= TM
->tmInternalLeading
;
2161 Ntm
->tmExternalLeading
= TM
->tmExternalLeading
;
2162 Ntm
->tmAveCharWidth
= TM
->tmAveCharWidth
;
2163 Ntm
->tmMaxCharWidth
= TM
->tmMaxCharWidth
;
2164 Ntm
->tmWeight
= TM
->tmWeight
;
2165 Ntm
->tmOverhang
= TM
->tmOverhang
;
2166 Ntm
->tmDigitizedAspectX
= TM
->tmDigitizedAspectX
;
2167 Ntm
->tmDigitizedAspectY
= TM
->tmDigitizedAspectY
;
2168 Ntm
->tmFirstChar
= TM
->tmFirstChar
;
2169 Ntm
->tmLastChar
= TM
->tmLastChar
;
2170 Ntm
->tmDefaultChar
= TM
->tmDefaultChar
;
2171 Ntm
->tmBreakChar
= TM
->tmBreakChar
;
2172 Ntm
->tmItalic
= TM
->tmItalic
;
2173 Ntm
->tmUnderlined
= TM
->tmUnderlined
;
2174 Ntm
->tmStruckOut
= TM
->tmStruckOut
;
2175 Ntm
->tmPitchAndFamily
= TM
->tmPitchAndFamily
;
2176 Ntm
->tmCharSet
= TM
->tmCharSet
;
2177 Ntm
->ntmFlags
= TM
->tmItalic
? NTM_ITALIC
: 0;
2179 if (550 < TM
->tmWeight
) Ntm
->ntmFlags
|= NTM_BOLD
;
2181 if (0 == Ntm
->ntmFlags
) Ntm
->ntmFlags
= NTM_REGULAR
;
2183 Ntm
->ntmSizeEM
= Otm
->otmEMSquare
;
2184 Ntm
->ntmCellHeight
= Otm
->otmEMSquare
;
2185 Ntm
->ntmAvgWidth
= 0;
2187 Info
->FontType
= (0 != (TM
->tmPitchAndFamily
& TMPF_TRUETYPE
)
2188 ? TRUETYPE_FONTTYPE
: 0);
2190 if (0 == (TM
->tmPitchAndFamily
& TMPF_VECTOR
))
2191 Info
->FontType
|= RASTER_FONTTYPE
;
2193 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
2198 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
), FaceName
);
2202 status
= IntGetFontLocalizedName(&NameW
, Face
, TT_NAME_ID_FONT_FAMILY
,
2204 if (NT_SUCCESS(status
))
2207 RtlStringCbCopyW(Lf
->lfFaceName
, sizeof(Lf
->lfFaceName
),
2209 RtlFreeUnicodeString(&NameW
);
2216 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2217 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2222 status
= IntGetFontLocalizedName(&NameW
, Face
, TT_NAME_ID_FULL_NAME
,
2224 if (NT_SUCCESS(status
))
2227 RtlStringCbCopyW(Info
->EnumLogFontEx
.elfFullName
,
2228 sizeof(Info
->EnumLogFontEx
.elfFullName
),
2230 RtlFreeUnicodeString(&NameW
);
2234 RtlInitAnsiString(&StyleA
, Face
->style_name
);
2235 StyleW
.Buffer
= Info
->EnumLogFontEx
.elfStyle
;
2236 StyleW
.MaximumLength
= sizeof(Info
->EnumLogFontEx
.elfStyle
);
2237 status
= RtlAnsiStringToUnicodeString(&StyleW
, &StyleA
, FALSE
);
2238 if (!NT_SUCCESS(status
))
2242 Info
->EnumLogFontEx
.elfScript
[0] = UNICODE_NULL
;
2245 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
2253 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
2254 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
2255 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
2256 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
2257 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
2258 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
2260 if (0 == pOS2
->version
)
2264 if (FT_Get_First_Char(Face
, &Dummy
) < 0x100)
2265 fs
.fsCsb
[0] |= FS_LATIN1
;
2267 fs
.fsCsb
[0] |= FS_SYMBOL
;
2271 if (fs
.fsCsb
[0] == 0)
2273 /* Let's see if we can find any interesting cmaps */
2274 for (i
= 0; i
< (UINT
)Face
->num_charmaps
; i
++)
2276 switch (Face
->charmaps
[i
]->encoding
)
2278 case FT_ENCODING_UNICODE
:
2279 case FT_ENCODING_APPLE_ROMAN
:
2280 fs
.fsCsb
[0] |= FS_LATIN1
;
2282 case FT_ENCODING_MS_SYMBOL
:
2283 fs
.fsCsb
[0] |= FS_SYMBOL
;
2291 for (i
= 0; i
< MAXTCIINDEX
; i
++)
2294 if (fs
.fsCsb
[0] & fs0
)
2296 if (!IntTranslateCharsetInfo(&fs0
, &CharSetInfo
, TCI_SRCFONTSIG
))
2298 CharSetInfo
.ciCharset
= DEFAULT_CHARSET
;
2300 if (DEFAULT_CHARSET
!= CharSetInfo
.ciCharset
)
2303 wcscpy(Info
->EnumLogFontEx
.elfScript
, ElfScripts
[i
]);
2306 DPRINT1("Unknown elfscript for bit %u\n", i
);
2311 Info
->NewTextMetricEx
.ntmFontSig
= fs
;
2315 FindFaceNameInInfo(PUNICODE_STRING FaceName
, PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2318 UNICODE_STRING InfoFaceName
;
2320 for (i
= 0; i
< InfoEntries
; i
++)
2322 RtlInitUnicodeString(&InfoFaceName
, Info
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
2323 if (RtlEqualUnicodeString(&InfoFaceName
, FaceName
, TRUE
))
2332 static BOOLEAN FASTCALL
2333 FontFamilyInclude(LPLOGFONTW LogFont
, PUNICODE_STRING FaceName
,
2334 PFONTFAMILYINFO Info
, DWORD InfoEntries
)
2336 UNICODE_STRING LogFontFaceName
;
2338 RtlInitUnicodeString(&LogFontFaceName
, LogFont
->lfFaceName
);
2339 if (0 != LogFontFaceName
.Length
&&
2340 !RtlEqualUnicodeString(&LogFontFaceName
, FaceName
, TRUE
))
2345 return FindFaceNameInInfo(FaceName
, Info
, InfoEntries
) < 0;
2348 static BOOLEAN FASTCALL
2349 GetFontFamilyInfoForList(LPLOGFONTW LogFont
,
2350 PFONTFAMILYINFO Info
,
2356 PFONT_ENTRY CurrentEntry
;
2357 ANSI_STRING EntryFaceNameA
;
2358 UNICODE_STRING EntryFaceNameW
;
2362 Entry
= Head
->Flink
;
2363 while (Entry
!= Head
)
2365 CurrentEntry
= (PFONT_ENTRY
) CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
2367 FontGDI
= CurrentEntry
->Font
;
2370 RtlInitAnsiString(&EntryFaceNameA
, FontGDI
->SharedFace
->Face
->family_name
);
2371 status
= RtlAnsiStringToUnicodeString(&EntryFaceNameW
, &EntryFaceNameA
, TRUE
);
2372 if (!NT_SUCCESS(status
))
2377 if ((LF_FACESIZE
- 1) * sizeof(WCHAR
) < EntryFaceNameW
.Length
)
2379 EntryFaceNameW
.Length
= (LF_FACESIZE
- 1) * sizeof(WCHAR
);
2380 EntryFaceNameW
.Buffer
[LF_FACESIZE
- 1] = L
'\0';
2383 if (FontFamilyInclude(LogFont
, &EntryFaceNameW
, Info
, min(*Count
, Size
)))
2387 FontFamilyFillInfo(Info
+ *Count
, EntryFaceNameW
.Buffer
,
2392 RtlFreeUnicodeString(&EntryFaceNameW
);
2393 Entry
= Entry
->Flink
;
2399 typedef struct FontFamilyInfoCallbackContext
2402 PFONTFAMILYINFO Info
;
2405 } FONT_FAMILY_INFO_CALLBACK_CONTEXT
, *PFONT_FAMILY_INFO_CALLBACK_CONTEXT
;
2407 _Function_class_(RTL_QUERY_REGISTRY_ROUTINE
)
2408 static NTSTATUS APIENTRY
2409 FontFamilyInfoQueryRegistryCallback(IN PWSTR ValueName
, IN ULONG ValueType
,
2410 IN PVOID ValueData
, IN ULONG ValueLength
,
2411 IN PVOID Context
, IN PVOID EntryContext
)
2413 PFONT_FAMILY_INFO_CALLBACK_CONTEXT InfoContext
;
2414 UNICODE_STRING RegistryName
, RegistryValue
;
2418 if (REG_SZ
!= ValueType
)
2420 return STATUS_SUCCESS
;
2422 InfoContext
= (PFONT_FAMILY_INFO_CALLBACK_CONTEXT
) Context
;
2423 RtlInitUnicodeString(&RegistryName
, ValueName
);
2425 /* Do we need to include this font family? */
2426 if (FontFamilyInclude(InfoContext
->LogFont
, &RegistryName
, InfoContext
->Info
,
2427 min(InfoContext
->Count
, InfoContext
->Size
)))
2429 RtlInitUnicodeString(&RegistryValue
, (PCWSTR
) ValueData
);
2430 Existing
= FindFaceNameInInfo(&RegistryValue
, InfoContext
->Info
,
2431 min(InfoContext
->Count
, InfoContext
->Size
));
2434 /* We already have the information about the "real" font. Just copy it */
2435 if (InfoContext
->Count
< InfoContext
->Size
)
2437 InfoContext
->Info
[InfoContext
->Count
] = InfoContext
->Info
[Existing
];
2438 RtlStringCbCopyNW(InfoContext
->Info
[InfoContext
->Count
].EnumLogFontEx
.elfLogFont
.lfFaceName
,
2439 sizeof(InfoContext
->Info
[InfoContext
->Count
].EnumLogFontEx
.elfLogFont
.lfFaceName
),
2440 RegistryName
.Buffer
,
2441 RegistryName
.Length
);
2443 InfoContext
->Count
++;
2444 return STATUS_SUCCESS
;
2447 /* Try to find information about the "real" font */
2448 FontGDI
= FindFaceNameInLists(&RegistryValue
);
2449 if (NULL
== FontGDI
)
2451 /* "Real" font not found, discard this registry entry */
2452 return STATUS_SUCCESS
;
2455 /* Return info about the "real" font but with the name of the alias */
2456 if (InfoContext
->Count
< InfoContext
->Size
)
2458 FontFamilyFillInfo(InfoContext
->Info
+ InfoContext
->Count
,
2459 RegistryName
.Buffer
, NULL
, FontGDI
);
2461 InfoContext
->Count
++;
2462 return STATUS_SUCCESS
;
2465 return STATUS_SUCCESS
;
2468 static BOOLEAN FASTCALL
2469 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont
,
2470 PFONTFAMILYINFO Info
,
2474 RTL_QUERY_REGISTRY_TABLE QueryTable
[2] = {{0}};
2475 FONT_FAMILY_INFO_CALLBACK_CONTEXT Context
;
2478 /* Enumerate font families found in HKLM\Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes
2479 The real work is done in the registry callback function */
2480 Context
.LogFont
= LogFont
;
2481 Context
.Info
= Info
;
2482 Context
.Count
= *Count
;
2483 Context
.Size
= Size
;
2485 QueryTable
[0].QueryRoutine
= FontFamilyInfoQueryRegistryCallback
;
2486 QueryTable
[0].Flags
= 0;
2487 QueryTable
[0].Name
= NULL
;
2488 QueryTable
[0].EntryContext
= NULL
;
2489 QueryTable
[0].DefaultType
= REG_NONE
;
2490 QueryTable
[0].DefaultData
= NULL
;
2491 QueryTable
[0].DefaultLength
= 0;
2493 QueryTable
[1].QueryRoutine
= NULL
;
2494 QueryTable
[1].Name
= NULL
;
2496 Status
= RtlQueryRegistryValues(RTL_REGISTRY_WINDOWS_NT
,
2501 if (NT_SUCCESS(Status
))
2503 *Count
= Context
.Count
;
2506 return NT_SUCCESS(Status
) || STATUS_OBJECT_NAME_NOT_FOUND
== Status
;
2511 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs
)
2515 lprs
->nSize
= sizeof(RASTERIZER_STATUS
);
2516 lprs
->wFlags
= TT_AVAILABLE
| TT_ENABLED
;
2517 lprs
->nLanguageID
= gusLanguageID
;
2520 EngSetLastError(ERROR_INVALID_PARAMETER
);
2530 return (FLOATOBJ_Equal(&pmx1
->efM11
, &pmx2
->efM11
) &&
2531 FLOATOBJ_Equal(&pmx1
->efM12
, &pmx2
->efM12
) &&
2532 FLOATOBJ_Equal(&pmx1
->efM21
, &pmx2
->efM21
) &&
2533 FLOATOBJ_Equal(&pmx1
->efM22
, &pmx2
->efM22
));
2536 FT_BitmapGlyph APIENTRY
2543 PLIST_ENTRY CurrentEntry
;
2544 PFONT_CACHE_ENTRY FontEntry
;
2546 ASSERT_FREETYPE_LOCK_HELD();
2548 CurrentEntry
= FontCacheListHead
.Flink
;
2549 while (CurrentEntry
!= &FontCacheListHead
)
2551 FontEntry
= CONTAINING_RECORD(CurrentEntry
, FONT_CACHE_ENTRY
, ListEntry
);
2552 if ((FontEntry
->Face
== Face
) &&
2553 (FontEntry
->GlyphIndex
== GlyphIndex
) &&
2554 (FontEntry
->Height
== Height
) &&
2555 (SameScaleMatrix(&FontEntry
->mxWorldToDevice
, pmx
)))
2557 CurrentEntry
= CurrentEntry
->Flink
;
2560 if (CurrentEntry
== &FontCacheListHead
)
2565 RemoveEntryList(CurrentEntry
);
2566 InsertHeadList(&FontCacheListHead
, CurrentEntry
);
2567 return FontEntry
->BitmapGlyph
;
2571 FT_BitmapGlyph APIENTRY
2574 FT_GlyphSlot GlyphSlot
,
2575 FT_Render_Mode RenderMode
)
2579 FT_Bitmap AlignedBitmap
;
2580 FT_BitmapGlyph BitmapGlyph
;
2582 error
= FT_Get_Glyph(GlyphSlot
, &Glyph
);
2585 DPRINT1("Failure getting glyph.\n");
2589 error
= FT_Glyph_To_Bitmap(&Glyph
, RenderMode
, 0, 1);
2592 FT_Done_Glyph(Glyph
);
2593 DPRINT1("Failure rendering glyph.\n");
2597 BitmapGlyph
= (FT_BitmapGlyph
)Glyph
;
2598 FT_Bitmap_New(&AlignedBitmap
);
2599 if (FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2601 DPRINT1("Conversion failed\n");
2602 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2606 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2607 BitmapGlyph
->bitmap
= AlignedBitmap
;
2612 FT_BitmapGlyph APIENTRY
2618 FT_GlyphSlot GlyphSlot
,
2619 FT_Render_Mode RenderMode
)
2623 PFONT_CACHE_ENTRY NewEntry
;
2624 FT_Bitmap AlignedBitmap
;
2625 FT_BitmapGlyph BitmapGlyph
;
2627 ASSERT_FREETYPE_LOCK_HELD();
2629 error
= FT_Get_Glyph(GlyphSlot
, &GlyphCopy
);
2632 DPRINT1("Failure caching glyph.\n");
2636 error
= FT_Glyph_To_Bitmap(&GlyphCopy
, RenderMode
, 0, 1);
2639 FT_Done_Glyph(GlyphCopy
);
2640 DPRINT1("Failure rendering glyph.\n");
2644 NewEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(FONT_CACHE_ENTRY
), TAG_FONT
);
2647 DPRINT1("Alloc failure caching glyph.\n");
2648 FT_Done_Glyph(GlyphCopy
);
2652 BitmapGlyph
= (FT_BitmapGlyph
)GlyphCopy
;
2653 FT_Bitmap_New(&AlignedBitmap
);
2654 if(FT_Bitmap_Convert(GlyphSlot
->library
, &BitmapGlyph
->bitmap
, &AlignedBitmap
, 4))
2656 DPRINT1("Conversion failed\n");
2657 ExFreePoolWithTag(NewEntry
, TAG_FONT
);
2658 FT_Bitmap_Done(GlyphSlot
->library
, &AlignedBitmap
);
2659 FT_Done_Glyph((FT_Glyph
)BitmapGlyph
);
2663 FT_Bitmap_Done(GlyphSlot
->library
, &BitmapGlyph
->bitmap
);
2664 BitmapGlyph
->bitmap
= AlignedBitmap
;
2666 NewEntry
->GlyphIndex
= GlyphIndex
;
2667 NewEntry
->Face
= Face
;
2668 NewEntry
->BitmapGlyph
= BitmapGlyph
;
2669 NewEntry
->Height
= Height
;
2670 NewEntry
->mxWorldToDevice
= *pmx
;
2672 InsertHeadList(&FontCacheListHead
, &NewEntry
->ListEntry
);
2673 if (++FontCacheNumEntries
> MAX_FONT_CACHE
)
2675 NewEntry
= CONTAINING_RECORD(FontCacheListHead
.Blink
, FONT_CACHE_ENTRY
, ListEntry
);
2676 RemoveCachedEntry(NewEntry
);
2683 static void FTVectorToPOINTFX(FT_Vector
*vec
, POINTFX
*pt
)
2685 pt
->x
.value
= vec
->x
>> 6;
2686 pt
->x
.fract
= (vec
->x
& 0x3f) << 10;
2687 pt
->x
.fract
|= ((pt
->x
.fract
>> 6) | (pt
->x
.fract
>> 12));
2688 pt
->y
.value
= vec
->y
>> 6;
2689 pt
->y
.fract
= (vec
->y
& 0x3f) << 10;
2690 pt
->y
.fract
|= ((pt
->y
.fract
>> 6) | (pt
->y
.fract
>> 12));
2694 This function builds an FT_Fixed from a float. It puts the integer part
2695 in the highest 16 bits and the decimal part in the lowest 16 bits of the FT_Fixed.
2696 It fails if the integer part of the float number is greater than SHORT_MAX.
2698 static __inline FT_Fixed
FT_FixedFromFloat(float f
)
2701 unsigned short fract
= (f
- value
) * 0xFFFF;
2702 return (FT_Fixed
)((long)value
<< 16 | (unsigned long)fract
);
2706 This function builds an FT_Fixed from a FIXED. It simply put f.value
2707 in the highest 16 bits and f.fract in the lowest 16 bits of the FT_Fixed.
2709 static __inline FT_Fixed
FT_FixedFromFIXED(FIXED f
)
2711 return (FT_Fixed
)((long)f
.value
<< 16 | (unsigned long)f
.fract
);
2714 static unsigned int get_native_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2716 TTPOLYGONHEADER
*pph
;
2718 int needed
= 0, point
= 0, contour
, first_pt
;
2719 unsigned int pph_start
, cpfx
;
2722 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2724 /* Ignore contours containing one point */
2725 if (point
== outline
->contours
[contour
])
2732 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2736 pph
->dwType
= TT_POLYGON_TYPE
;
2737 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2739 needed
+= sizeof(*pph
);
2741 while (point
<= outline
->contours
[contour
])
2743 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2744 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2745 TT_PRIM_LINE
: TT_PRIM_QSPLINE
;
2750 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2753 } while (point
<= outline
->contours
[contour
] &&
2754 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2755 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2756 /* At the end of a contour Windows adds the start point, but
2758 if (point
> outline
->contours
[contour
] &&
2759 !(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2762 FTVectorToPOINTFX(&outline
->points
[first_pt
], &ppc
->apfx
[cpfx
]);
2765 else if (point
<= outline
->contours
[contour
] &&
2766 outline
->tags
[point
] & FT_Curve_Tag_On
)
2768 /* add closing pt for bezier */
2770 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2779 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2782 pph
->cb
= needed
- pph_start
;
2787 static unsigned int get_bezier_glyph_outline(FT_Outline
*outline
, unsigned int buflen
, char *buf
)
2789 /* Convert the quadratic Beziers to cubic Beziers.
2790 The parametric eqn for a cubic Bezier is, from PLRM:
2791 r(t) = at^3 + bt^2 + ct + r0
2792 with the control points:
2797 A quadratic Bezier has the form:
2798 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2800 So equating powers of t leads to:
2801 r1 = 2/3 p1 + 1/3 p0
2802 r2 = 2/3 p1 + 1/3 p2
2803 and of course r0 = p0, r3 = p2
2805 int contour
, point
= 0, first_pt
;
2806 TTPOLYGONHEADER
*pph
;
2808 DWORD pph_start
, cpfx
, type
;
2809 FT_Vector cubic_control
[4];
2810 unsigned int needed
= 0;
2812 for (contour
= 0; contour
< outline
->n_contours
; contour
++)
2815 pph
= (TTPOLYGONHEADER
*)(buf
+ needed
);
2819 pph
->dwType
= TT_POLYGON_TYPE
;
2820 FTVectorToPOINTFX(&outline
->points
[point
], &pph
->pfxStart
);
2822 needed
+= sizeof(*pph
);
2824 while (point
<= outline
->contours
[contour
])
2826 ppc
= (TTPOLYCURVE
*)(buf
+ needed
);
2827 type
= outline
->tags
[point
] & FT_Curve_Tag_On
?
2828 TT_PRIM_LINE
: TT_PRIM_CSPLINE
;
2832 if (type
== TT_PRIM_LINE
)
2835 FTVectorToPOINTFX(&outline
->points
[point
], &ppc
->apfx
[cpfx
]);
2841 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2844 /* FIXME: Possible optimization in endpoint calculation
2845 if there are two consecutive curves */
2846 cubic_control
[0] = outline
->points
[point
-1];
2847 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
))
2849 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
2850 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
2851 cubic_control
[0].x
>>= 1;
2852 cubic_control
[0].y
>>= 1;
2854 if (point
+1 > outline
->contours
[contour
])
2855 cubic_control
[3] = outline
->points
[first_pt
];
2858 cubic_control
[3] = outline
->points
[point
+1];
2859 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
))
2861 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
2862 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
2863 cubic_control
[3].x
>>= 1;
2864 cubic_control
[3].y
>>= 1;
2867 /* r1 = 1/3 p0 + 2/3 p1
2868 r2 = 1/3 p2 + 2/3 p1 */
2869 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
2870 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
2871 cubic_control
[2] = cubic_control
[1];
2872 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
2873 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
2874 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
2875 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
2878 FTVectorToPOINTFX(&cubic_control
[1], &ppc
->apfx
[cpfx
]);
2879 FTVectorToPOINTFX(&cubic_control
[2], &ppc
->apfx
[cpfx
+1]);
2880 FTVectorToPOINTFX(&cubic_control
[3], &ppc
->apfx
[cpfx
+2]);
2885 } while (point
<= outline
->contours
[contour
] &&
2886 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
2887 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
2888 /* At the end of a contour Windows adds the start point,
2889 but only for Beziers and we've already done that.
2891 if (point
<= outline
->contours
[contour
] &&
2892 outline
->tags
[point
] & FT_Curve_Tag_On
)
2894 /* This is the closing pt of a bezier, but we've already
2895 added it, so just inc point and carry on */
2903 needed
+= sizeof(*ppc
) + (cpfx
- 1) * sizeof(POINTFX
);
2906 pph
->cb
= needed
- pph_start
;
2912 IntRequestFontSize(PDC dc
, FT_Face face
, LONG Width
, LONG Height
)
2914 FT_Size_RequestRec req
;
2925 Height
= dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
;
2935 if (Width
> 0xFFFFU
)
2937 if (Height
> 0xFFFFU
)
2940 req
.type
= FT_SIZE_REQUEST_TYPE_NOMINAL
;
2941 req
.width
= (FT_Long
)(Width
<< 6);
2942 req
.height
= (FT_Long
)(Height
<< 6);
2943 req
.horiResolution
= 0;
2944 req
.vertResolution
= 0;
2945 return FT_Request_Size(face
, &req
);
2950 TextIntUpdateSize(PDC dc
,
2957 FT_CharMap charmap
, found
;
2963 face
= FontGDI
->SharedFace
->Face
;
2964 if (face
->charmap
== NULL
)
2966 DPRINT("WARNING: No charmap selected!\n");
2967 DPRINT("This font face has %d charmaps\n", face
->num_charmaps
);
2970 for (n
= 0; n
< face
->num_charmaps
; n
++)
2972 charmap
= face
->charmaps
[n
];
2973 DPRINT("Found charmap encoding: %i\n", charmap
->encoding
);
2974 if (charmap
->encoding
!= 0)
2982 DPRINT1("WARNING: Could not find desired charmap!\n");
2986 error
= FT_Set_Charmap(face
, found
);
2989 DPRINT1("WARNING: Could not set the charmap!\n");
2994 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
2996 error
= IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
3003 DPRINT1("Error in setting pixel sizes: %d\n", error
);
3012 * Based on WineEngGetGlyphOutline
3017 ftGdiGetGlyphOutline(
3025 BOOL bIgnoreRotation
)
3027 static const FT_Matrix identityMat
= {(1 << 16), 0, 0, (1 << 16)};
3035 FT_UInt glyph_index
;
3036 DWORD width
, height
, pitch
, needed
= 0;
3037 FT_Bitmap ft_bitmap
;
3039 INT left
, right
, top
= 0, bottom
= 0;
3041 FT_Int load_flags
= FT_LOAD_DEFAULT
| FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
;
3042 FLOAT eM11
, widthRatio
= 1.0;
3043 FT_Matrix transMat
= identityMat
;
3044 BOOL needsTransform
= FALSE
;
3047 INT adv
, lsb
, bbx
; /* These three hold to widths of the unrotated chars */
3048 OUTLINETEXTMETRICW
*potm
;
3052 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch
, iFormat
, pgm
,
3053 cjBuf
, pvBuf
, pmat2
);
3055 pdcattr
= dc
->pdcattr
;
3057 MatrixS2XForm(&xForm
, &dc
->pdcattr
->mxWorldToDevice
);
3060 hFont
= pdcattr
->hlfntNew
;
3061 TextObj
= RealizeFontInit(hFont
);
3065 EngSetLastError(ERROR_INVALID_HANDLE
);
3068 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3069 ft_face
= FontGDI
->SharedFace
->Face
;
3071 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3072 aveWidth
= FT_IS_SCALABLE(ft_face
) ? abs(plf
->lfWidth
) : 0;
3073 orientation
= FT_IS_SCALABLE(ft_face
) ? plf
->lfOrientation
: 0;
3075 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
3076 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
3079 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3080 TEXTOBJ_UnlockText(TextObj
);
3083 IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
3086 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3087 FtSetCoordinateTransform(ft_face
, DC_pmxWorldToDevice(dc
));
3089 TEXTOBJ_UnlockText(TextObj
);
3091 if (iFormat
& GGO_GLYPH_INDEX
)
3094 iFormat
&= ~GGO_GLYPH_INDEX
;
3096 else glyph_index
= FT_Get_Char_Index(ft_face
, wch
);
3098 if (orientation
|| (iFormat
!= GGO_METRICS
&& iFormat
!= GGO_BITMAP
) || aveWidth
|| pmat2
)
3099 load_flags
|= FT_LOAD_NO_BITMAP
;
3101 if (iFormat
& GGO_UNHINTED
)
3103 load_flags
|= FT_LOAD_NO_HINTING
;
3104 iFormat
&= ~GGO_UNHINTED
;
3107 error
= FT_Load_Glyph(ft_face
, glyph_index
, load_flags
);
3110 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index
);
3112 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
);
3117 if (aveWidth
&& potm
)
3119 widthRatio
= (FLOAT
)aveWidth
* eM11
/
3120 (FLOAT
) potm
->otmTextMetrics
.tmAveCharWidth
;
3123 left
= (INT
)(ft_face
->glyph
->metrics
.horiBearingX
* widthRatio
) & -64;
3124 right
= (INT
)((ft_face
->glyph
->metrics
.horiBearingX
+
3125 ft_face
->glyph
->metrics
.width
) * widthRatio
+ 63) & -64;
3127 adv
= (INT
)((ft_face
->glyph
->metrics
.horiAdvance
* widthRatio
) + 63) >> 6;
3129 bbx
= (right
- left
) >> 6;
3131 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv
, lsb
, bbx
);
3135 /* Scaling transform */
3142 PMATRIX pmx
= DC_pmxWorldToDevice(dc
);
3144 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
3145 efTemp
= pmx
->efM11
;
3146 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3147 ftmatrix
.xx
= FLOATOBJ_GetLong(&efTemp
);
3149 efTemp
= pmx
->efM12
;
3150 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3151 ftmatrix
.xy
= FLOATOBJ_GetLong(&efTemp
);
3153 efTemp
= pmx
->efM21
;
3154 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3155 ftmatrix
.yx
= FLOATOBJ_GetLong(&efTemp
);
3157 efTemp
= pmx
->efM22
;
3158 FLOATOBJ_MulLong(&efTemp
, 0x00010000);
3159 ftmatrix
.yy
= FLOATOBJ_GetLong(&efTemp
);
3161 FT_Matrix_Multiply(&ftmatrix
, &transMat
);
3162 needsTransform
= TRUE
;
3165 /* Rotation transform */
3168 FT_Matrix rotationMat
;
3170 DPRINT("Rotation Trans!\n");
3171 angle
= FT_FixedFromFloat((float)orientation
/ 10.0);
3172 FT_Vector_Unit(&vecAngle
, angle
);
3173 rotationMat
.xx
= vecAngle
.x
;
3174 rotationMat
.xy
= -vecAngle
.y
;
3175 rotationMat
.yx
= -rotationMat
.xy
;
3176 rotationMat
.yy
= rotationMat
.xx
;
3177 FT_Matrix_Multiply(&rotationMat
, &transMat
);
3178 needsTransform
= TRUE
;
3181 /* Extra transformation specified by caller */
3185 DPRINT("MAT2 Matrix Trans!\n");
3186 extraMat
.xx
= FT_FixedFromFIXED(pmat2
->eM11
);
3187 extraMat
.xy
= FT_FixedFromFIXED(pmat2
->eM21
);
3188 extraMat
.yx
= FT_FixedFromFIXED(pmat2
->eM12
);
3189 extraMat
.yy
= FT_FixedFromFIXED(pmat2
->eM22
);
3190 FT_Matrix_Multiply(&extraMat
, &transMat
);
3191 needsTransform
= TRUE
;
3194 if (potm
) ExFreePoolWithTag(potm
, GDITAG_TEXT
); /* It looks like we are finished with potm ATM. */
3196 if (!needsTransform
)
3198 DPRINT("No Need to be Transformed!\n");
3199 top
= (ft_face
->glyph
->metrics
.horiBearingY
+ 63) & -64;
3200 bottom
= (ft_face
->glyph
->metrics
.horiBearingY
-
3201 ft_face
->glyph
->metrics
.height
) & -64;
3202 gm
.gmCellIncX
= adv
;
3209 for (xc
= 0; xc
< 2; xc
++)
3211 for (yc
= 0; yc
< 2; yc
++)
3213 vec
.x
= (ft_face
->glyph
->metrics
.horiBearingX
+
3214 xc
* ft_face
->glyph
->metrics
.width
);
3215 vec
.y
= ft_face
->glyph
->metrics
.horiBearingY
-
3216 yc
* ft_face
->glyph
->metrics
.height
;
3217 DPRINT("Vec %ld,%ld\n", vec
.x
, vec
.y
);
3218 FT_Vector_Transform(&vec
, &transMat
);
3219 if (xc
== 0 && yc
== 0)
3221 left
= right
= vec
.x
;
3222 top
= bottom
= vec
.y
;
3226 if (vec
.x
< left
) left
= vec
.x
;
3227 else if (vec
.x
> right
) right
= vec
.x
;
3228 if (vec
.y
< bottom
) bottom
= vec
.y
;
3229 else if (vec
.y
> top
) top
= vec
.y
;
3234 right
= (right
+ 63) & -64;
3235 bottom
= bottom
& -64;
3236 top
= (top
+ 63) & -64;
3238 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left
, top
, right
, bottom
);
3239 vec
.x
= ft_face
->glyph
->metrics
.horiAdvance
;
3241 FT_Vector_Transform(&vec
, &transMat
);
3242 gm
.gmCellIncX
= (vec
.x
+63) >> 6;
3243 gm
.gmCellIncY
= -((vec
.y
+63) >> 6);
3245 gm
.gmBlackBoxX
= (right
- left
) >> 6;
3246 gm
.gmBlackBoxY
= (top
- bottom
) >> 6;
3247 gm
.gmptGlyphOrigin
.x
= left
>> 6;
3248 gm
.gmptGlyphOrigin
.y
= top
>> 6;
3250 DPRINT("CX %d CY %d BBX %u BBY %u GOX %d GOY %d\n",
3251 gm
.gmCellIncX
, gm
.gmCellIncY
,
3252 gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
3253 gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
3258 if (iFormat
== GGO_METRICS
)
3260 DPRINT("GGO_METRICS Exit!\n");
3262 return 1; /* FIXME */
3265 if (ft_face
->glyph
->format
!= ft_glyph_format_outline
&& iFormat
!= GGO_BITMAP
)
3267 DPRINT1("Loaded a bitmap\n");
3274 width
= gm
.gmBlackBoxX
;
3275 height
= gm
.gmBlackBoxY
;
3276 pitch
= ((width
+ 31) >> 5) << 2;
3277 needed
= pitch
* height
;
3279 if (!pvBuf
|| !cjBuf
) break;
3280 if (!needed
) return GDI_ERROR
; /* empty glyph */
3284 switch (ft_face
->glyph
->format
)
3286 case ft_glyph_format_bitmap
:
3288 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3289 INT w
= min( pitch
, (ft_face
->glyph
->bitmap
.width
+ 7) >> 3 );
3290 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3293 RtlCopyMemory(dst
, src
, w
);
3294 src
+= ft_face
->glyph
->bitmap
.pitch
;
3300 case ft_glyph_format_outline
:
3301 ft_bitmap
.width
= width
;
3302 ft_bitmap
.rows
= height
;
3303 ft_bitmap
.pitch
= pitch
;
3304 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
3305 ft_bitmap
.buffer
= pvBuf
;
3310 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3312 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3313 /* Note: FreeType will only set 'black' bits for us. */
3314 RtlZeroMemory(pvBuf
, needed
);
3315 FT_Outline_Get_Bitmap(library
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3320 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3325 case GGO_GRAY2_BITMAP
:
3326 case GGO_GRAY4_BITMAP
:
3327 case GGO_GRAY8_BITMAP
:
3329 unsigned int mult
, row
, col
;
3332 width
= gm
.gmBlackBoxX
;
3333 height
= gm
.gmBlackBoxY
;
3334 pitch
= (width
+ 3) / 4 * 4;
3335 needed
= pitch
* height
;
3337 if (!pvBuf
|| !cjBuf
) break;
3338 if (!needed
) return GDI_ERROR
; /* empty glyph */
3342 switch (ft_face
->glyph
->format
)
3344 case ft_glyph_format_bitmap
:
3346 BYTE
*src
= ft_face
->glyph
->bitmap
.buffer
, *dst
= pvBuf
;
3347 INT h
= min( height
, ft_face
->glyph
->bitmap
.rows
);
3351 for (x
= 0; (UINT
)x
< pitch
; x
++)
3353 if (x
< ft_face
->glyph
->bitmap
.width
)
3354 dst
[x
] = (src
[x
/ 8] & (1 << ( (7 - (x
% 8))))) ? 0xff : 0;
3358 src
+= ft_face
->glyph
->bitmap
.pitch
;
3363 case ft_glyph_format_outline
:
3365 ft_bitmap
.width
= width
;
3366 ft_bitmap
.rows
= height
;
3367 ft_bitmap
.pitch
= pitch
;
3368 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
3369 ft_bitmap
.buffer
= pvBuf
;
3374 FT_Outline_Transform(&ft_face
->glyph
->outline
, &transMat
);
3376 FT_Outline_Translate(&ft_face
->glyph
->outline
, -left
, -bottom
);
3377 RtlZeroMemory(ft_bitmap
.buffer
, cjBuf
);
3378 FT_Outline_Get_Bitmap(library
, &ft_face
->glyph
->outline
, &ft_bitmap
);
3381 if (iFormat
== GGO_GRAY2_BITMAP
)
3383 else if (iFormat
== GGO_GRAY4_BITMAP
)
3385 else if (iFormat
== GGO_GRAY8_BITMAP
)
3393 for (row
= 0; row
< height
; row
++)
3396 for (col
= 0; col
< width
; col
++, ptr
++)
3398 *ptr
= (((int)*ptr
) * mult
+ 128) / 256;
3406 DPRINT1("Loaded glyph format %x\n", ft_face
->glyph
->format
);
3413 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3415 if (cjBuf
== 0) pvBuf
= NULL
; /* This is okay, need cjBuf to allocate. */
3418 if (needsTransform
&& pvBuf
) FT_Outline_Transform(outline
, &transMat
);
3420 needed
= get_native_glyph_outline(outline
, cjBuf
, NULL
);
3422 if (!pvBuf
|| !cjBuf
)
3432 get_native_glyph_outline(outline
, cjBuf
, pvBuf
);
3438 FT_Outline
*outline
= &ft_face
->glyph
->outline
;
3439 if (cjBuf
== 0) pvBuf
= NULL
;
3441 if (needsTransform
&& pvBuf
)
3444 FT_Outline_Transform(outline
, &transMat
);
3447 needed
= get_bezier_glyph_outline(outline
, cjBuf
, NULL
);
3449 if (!pvBuf
|| !cjBuf
)
3454 get_bezier_glyph_outline(outline
, cjBuf
, pvBuf
);
3459 DPRINT1("Unsupported format %u\n", iFormat
);
3463 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed
);
3470 TextIntGetTextExtentPoint(PDC dc
,
3483 FT_BitmapGlyph realglyph
;
3484 INT error
, glyph_index
, i
, previous
;
3485 ULONGLONG TotalWidth
= 0;
3487 FT_Render_Mode RenderMode
;
3489 PMATRIX pmxWorldToDevice
;
3491 BOOL EmuBold
, EmuItalic
;
3493 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3495 face
= FontGDI
->SharedFace
->Face
;
3503 TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
);
3505 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3506 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
3507 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
3509 Render
= IntIsFontRenderingEnabled();
3511 RenderMode
= IntGetFontRenderMode(plf
);
3513 RenderMode
= FT_RENDER_MODE_MONO
;
3516 /* Get the DC's world-to-device transformation matrix */
3517 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
3518 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
3520 use_kerning
= FT_HAS_KERNING(face
);
3523 for (i
= 0; i
< Count
; i
++)
3525 if (fl
& GTEF_INDICES
)
3526 glyph_index
= *String
;
3528 glyph_index
= FT_Get_Char_Index(face
, *String
);
3530 if (EmuBold
|| EmuItalic
)
3533 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
,
3534 plf
->lfHeight
, pmxWorldToDevice
);
3536 if (EmuBold
|| EmuItalic
|| !realglyph
)
3538 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
3541 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
3545 glyph
= face
->glyph
;
3546 if (EmuBold
|| EmuItalic
)
3549 FT_GlyphSlot_Embolden(glyph
);
3551 FT_GlyphSlot_Oblique(glyph
);
3552 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
3556 realglyph
= ftGdiGlyphCacheSet(face
,
3566 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
3571 /* Retrieve kerning distance */
3572 if (use_kerning
&& previous
&& glyph_index
)
3575 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
3576 TotalWidth
+= delta
.x
;
3579 TotalWidth
+= realglyph
->root
.advance
.x
>> 10;
3581 if (((TotalWidth
+ 32) >> 6) <= MaxExtent
&& NULL
!= Fit
)
3587 Dx
[i
] = (TotalWidth
+ 32) >> 6;
3590 if (EmuBold
|| EmuItalic
)
3592 FT_Done_Glyph((FT_Glyph
)realglyph
);
3596 previous
= glyph_index
;
3601 Size
->cx
= (TotalWidth
+ 32) >> 6;
3602 Size
->cy
= (plf
->lfHeight
== 0 ?
3603 dc
->ppdev
->devinfo
.lfDefaultFont
.lfHeight
:
3604 abs(plf
->lfHeight
));
3605 Size
->cy
= EngMulDiv(Size
->cy
, dc
->ppdev
->gdiinfo
.ulLogPixelsY
, 72);
3613 ftGdiGetTextCharsetInfo(
3615 LPFONTSIGNATURE lpSig
,
3619 UINT Ret
= DEFAULT_CHARSET
;
3629 USHORT usACP
, usOEM
;
3631 pdcattr
= Dc
->pdcattr
;
3632 hFont
= pdcattr
->hlfntNew
;
3633 TextObj
= RealizeFontInit(hFont
);
3637 EngSetLastError(ERROR_INVALID_HANDLE
);
3640 FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
3641 Face
= FontGdi
->SharedFace
->Face
;
3642 TEXTOBJ_UnlockText(TextObj
);
3645 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
3647 memset(&fs
, 0, sizeof(FONTSIGNATURE
));
3650 fs
.fsCsb
[0] = pOS2
->ulCodePageRange1
;
3651 fs
.fsCsb
[1] = pOS2
->ulCodePageRange2
;
3652 fs
.fsUsb
[0] = pOS2
->ulUnicodeRange1
;
3653 fs
.fsUsb
[1] = pOS2
->ulUnicodeRange2
;
3654 fs
.fsUsb
[2] = pOS2
->ulUnicodeRange3
;
3655 fs
.fsUsb
[3] = pOS2
->ulUnicodeRange4
;
3656 if (pOS2
->version
== 0)
3660 if (FT_Get_First_Char( Face
, &dummy
) < 0x100)
3661 fs
.fsCsb
[0] |= FS_LATIN1
;
3663 fs
.fsCsb
[0] |= FS_SYMBOL
;
3666 DPRINT("Csb 1=%x 0=%x\n", fs
.fsCsb
[1],fs
.fsCsb
[0]);
3667 if (fs
.fsCsb
[0] == 0)
3668 { /* Let's see if we can find any interesting cmaps */
3669 for (i
= 0; i
< Face
->num_charmaps
; i
++)
3671 switch (Face
->charmaps
[i
]->encoding
)
3673 case FT_ENCODING_UNICODE
:
3674 case FT_ENCODING_APPLE_ROMAN
:
3675 fs
.fsCsb
[0] |= FS_LATIN1
;
3677 case FT_ENCODING_MS_SYMBOL
:
3678 fs
.fsCsb
[0] |= FS_SYMBOL
;
3687 RtlCopyMemory(lpSig
, &fs
, sizeof(FONTSIGNATURE
));
3690 RtlGetDefaultCodePage(&usACP
, &usOEM
);
3693 if (IntTranslateCharsetInfo(&cp
, &csi
, TCI_SRCCODEPAGE
))
3694 if (csi
.fs
.fsCsb
[0] & fs
.fsCsb
[0])
3697 Ret
= csi
.ciCharset
;
3701 for (i
= 0; i
< MAXTCIINDEX
; i
++)
3704 if (fs
.fsCsb
[0] & fs0
)
3706 if (IntTranslateCharsetInfo(&fs0
, &csi
, TCI_SRCFONTSIG
))
3710 Ret
= csi
.ciCharset
;
3714 DPRINT1("TCI failing on %x\n", fs0
);
3718 DPRINT("CharSet %u CodePage %u\n", csi
.ciCharset
, csi
.ciACP
);
3719 return (MAKELONG(csi
.ciACP
, csi
.ciCharset
));
3725 ftGetFontUnicodeRanges(PFONTGDI Font
, PGLYPHSET glyphset
)
3728 DWORD num_ranges
= 0;
3729 FT_Face face
= Font
->SharedFace
->Face
;
3731 if (face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
3733 FT_UInt glyph_code
= 0;
3734 FT_ULong char_code
, char_code_prev
;
3736 char_code_prev
= char_code
= FT_Get_First_Char(face
, &glyph_code
);
3738 DPRINT("Face encoding FT_ENCODING_UNICODE, number of glyphs %ld, first glyph %u, first char %04lx\n",
3739 face
->num_glyphs
, glyph_code
, char_code
);
3741 if (!glyph_code
) return 0;
3745 glyphset
->ranges
[0].wcLow
= (USHORT
)char_code
;
3746 glyphset
->ranges
[0].cGlyphs
= 0;
3747 glyphset
->cGlyphsSupported
= 0;
3753 if (char_code
< char_code_prev
)
3755 DPRINT1("Expected increasing char code from FT_Get_Next_Char\n");
3758 if (char_code
- char_code_prev
> 1)
3763 glyphset
->ranges
[num_ranges
- 1].wcLow
= (USHORT
)char_code
;
3764 glyphset
->ranges
[num_ranges
- 1].cGlyphs
= 1;
3765 glyphset
->cGlyphsSupported
++;
3770 glyphset
->ranges
[num_ranges
- 1].cGlyphs
++;
3771 glyphset
->cGlyphsSupported
++;
3773 char_code_prev
= char_code
;
3774 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_code
);
3778 DPRINT1("Encoding %i not supported\n", face
->charmap
->encoding
);
3780 size
= sizeof(GLYPHSET
) + sizeof(WCRANGE
) * (num_ranges
- 1);
3783 glyphset
->cbThis
= size
;
3784 glyphset
->cRanges
= num_ranges
;
3785 glyphset
->flAccel
= 0;
3793 ftGdiGetTextMetricsW(
3795 PTMW_INTERNAL ptmwi
)
3803 TT_HoriHeader
*pHori
;
3804 FT_WinFNT_HeaderRec Win
;
3806 NTSTATUS Status
= STATUS_SUCCESS
;
3811 EngSetLastError(STATUS_INVALID_PARAMETER
);
3815 if (!(dc
= DC_LockDc(hDC
)))
3817 EngSetLastError(ERROR_INVALID_HANDLE
);
3820 pdcattr
= dc
->pdcattr
;
3821 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
3822 if (NULL
!= TextObj
)
3824 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
3825 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
3827 Face
= FontGDI
->SharedFace
->Face
;
3829 Error
= IntRequestFontSize(dc
, Face
, plf
->lfWidth
, plf
->lfHeight
);
3830 FtSetCoordinateTransform(Face
, DC_pmxWorldToDevice(dc
));
3834 DPRINT1("Error in setting pixel sizes: %u\n", Error
);
3835 Status
= STATUS_UNSUCCESSFUL
;
3839 FT_Face Face
= FontGDI
->SharedFace
->Face
;
3840 Status
= STATUS_SUCCESS
;
3843 pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
3846 DPRINT1("Can't find OS/2 table - not TT font?\n");
3847 Status
= STATUS_INTERNAL_ERROR
;
3850 pHori
= FT_Get_Sfnt_Table(Face
, ft_sfnt_hhea
);
3853 DPRINT1("Can't find HHEA table - not TT font?\n");
3854 Status
= STATUS_INTERNAL_ERROR
;
3857 Error
= FT_Get_WinFNT_Header(Face
, &Win
);
3861 if (NT_SUCCESS(Status
))
3863 FillTM(&ptmwi
->TextMetric
, FontGDI
, pOS2
, pHori
, !Error
? &Win
: 0);
3865 /* FIXME: Fill Diff member */
3866 RtlZeroMemory(&ptmwi
->Diff
, sizeof(ptmwi
->Diff
));
3869 TEXTOBJ_UnlockText(TextObj
);
3873 Status
= STATUS_INVALID_HANDLE
;
3877 if (!NT_SUCCESS(Status
))
3879 SetLastNtError(Status
);
3894 DWORD Result
= GDI_ERROR
;
3895 FT_Face Face
= FontGdi
->SharedFace
->Face
;
3899 if (FT_IS_SFNT(Face
))
3902 Table
= Table
>> 24 | Table
<< 24 | (Table
>> 8 & 0xFF00) |
3903 (Table
<< 8 & 0xFF0000);
3905 if (!Buffer
) Size
= 0;
3910 FT_ULong Needed
= 0;
3912 Error
= FT_Load_Sfnt_Table(Face
, Table
, Offset
, NULL
, &Needed
);
3914 if ( !Error
&& Needed
< Size
) Size
= Needed
;
3916 if (!FT_Load_Sfnt_Table(Face
, Table
, Offset
, Buffer
, &Size
))
3925 // NOTE: See Table 1. of https://msdn.microsoft.com/en-us/library/ms969909.aspx
3926 static UINT FASTCALL
3927 GetFontPenalty(LOGFONTW
* LogFont
,
3928 PUNICODE_STRING RequestedNameW
,
3929 PUNICODE_STRING ActualNameW
,
3930 PUNICODE_STRING FullFaceNameW
,
3931 BYTE RequestedCharSet
,
3933 OUTLINETEXTMETRICW
* Otm
,
3935 const char * style_name
)
3940 BOOL fFixedSys
= FALSE
, fNeedScaling
= FALSE
;
3941 const BYTE UserCharSet
= CharSetFromLangID(gusLanguageID
);
3944 /* FIXME: Aspect Penalty 30 */
3945 /* FIXME: IntSizeSynth Penalty 20 */
3946 /* FIXME: SmallPenalty Penalty 1 */
3947 /* FIXME: FaceNameSubst Penalty 500 */
3949 if (RtlEqualUnicodeString(RequestedNameW
, &SystemW
, TRUE
))
3952 if (TM
->tmCharSet
!= UserCharSet
)
3954 /* CharSet Penalty 65000 */
3955 /* Requested charset does not match the candidate's. */
3959 else if (RtlEqualUnicodeString(RequestedNameW
, &FixedSysW
, TRUE
))
3961 /* "FixedSys" font */
3962 if (TM
->tmCharSet
!= UserCharSet
)
3964 /* CharSet Penalty 65000 */
3965 /* Requested charset does not match the candidate's. */
3970 else /* Request is non-"System" font */
3972 Byte
= RequestedCharSet
;
3973 if (Byte
== DEFAULT_CHARSET
)
3975 if (RtlEqualUnicodeString(RequestedNameW
, &MarlettW
, TRUE
))
3977 if (Byte
== ANSI_CHARSET
)
3979 DPRINT("Warning: FIXME: It's Marlett but ANSI_CHARSET.\n");
3981 /* We assume SYMBOL_CHARSET for "Marlett" font */
3982 Byte
= SYMBOL_CHARSET
;
3986 if (Byte
!= TM
->tmCharSet
)
3988 if (Byte
!= DEFAULT_CHARSET
&& Byte
!= ANSI_CHARSET
)
3990 /* CharSet Penalty 65000 */
3991 /* Requested charset does not match the candidate's. */
3996 if (UserCharSet
!= TM
->tmCharSet
)
4000 if (ANSI_CHARSET
!= TM
->tmCharSet
)
4010 Byte
= LogFont
->lfOutPrecision
;
4011 if (Byte
== OUT_DEFAULT_PRECIS
)
4012 Byte
= OUT_OUTLINE_PRECIS
; /* Is it OK? */
4015 case OUT_DEVICE_PRECIS
:
4016 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
) ||
4017 !(TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
)))
4019 /* OutputPrecision Penalty 19000 */
4020 /* Requested OUT_STROKE_PRECIS, but the device can't do it
4021 or the candidate is not a vector font. */
4026 if (TM
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
))
4028 /* OutputPrecision Penalty 19000 */
4029 /* Or OUT_STROKE_PRECIS not requested, and the candidate
4030 is a vector font that requires GDI support. */
4036 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4037 if (Byte
== DEFAULT_PITCH
)
4038 Byte
= VARIABLE_PITCH
;
4041 /* "FixedSys" font should be fixed-pitch */
4044 if (Byte
== FIXED_PITCH
)
4046 if (TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
)
4048 /* FixedPitch Penalty 15000 */
4049 /* Requested a fixed pitch font, but the candidate is a
4050 variable pitch font. */
4054 if (Byte
== VARIABLE_PITCH
)
4056 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4058 /* PitchVariable Penalty 350 */
4059 /* Requested a variable pitch font, but the candidate is not a
4060 variable pitch font. */
4065 Byte
= (LogFont
->lfPitchAndFamily
& 0x0F);
4066 if (Byte
== DEFAULT_PITCH
)
4068 if (!(TM
->tmPitchAndFamily
& _TMPF_VARIABLE_PITCH
))
4070 /* DefaultPitchFixed Penalty 1 */
4071 /* Requested DEFAULT_PITCH, but the candidate is fixed pitch. */
4076 if (RequestedNameW
->Buffer
[0])
4079 FT_Face Face
= FontGDI
->SharedFace
->Face
;
4081 /* localized family name */
4084 Status
= IntGetFontLocalizedName(ActualNameW
, Face
, TT_NAME_ID_FONT_FAMILY
,
4086 if (NT_SUCCESS(Status
))
4088 Found
= RtlEqualUnicodeString(RequestedNameW
, ActualNameW
, TRUE
);
4091 /* localized full name */
4094 Status
= IntGetFontLocalizedName(ActualNameW
, Face
, TT_NAME_ID_FULL_NAME
,
4096 if (NT_SUCCESS(Status
))
4098 Found
= RtlEqualUnicodeString(RequestedNameW
, ActualNameW
, TRUE
);
4101 if (gusLanguageID
!= gusEnglishUS
)
4103 /* English family name */
4106 Status
= IntGetFontLocalizedName(ActualNameW
, Face
, TT_NAME_ID_FONT_FAMILY
,
4108 if (NT_SUCCESS(Status
))
4110 Found
= RtlEqualUnicodeString(RequestedNameW
, ActualNameW
, TRUE
);
4113 /* English full name */
4116 Status
= IntGetFontLocalizedName(ActualNameW
, Face
, TT_NAME_ID_FULL_NAME
,
4118 if (NT_SUCCESS(Status
))
4120 Found
= RtlEqualUnicodeString(RequestedNameW
, ActualNameW
, TRUE
);
4126 /* FaceName Penalty 10000 */
4127 /* Requested a face name, but the candidate's face name
4133 Byte
= (LogFont
->lfPitchAndFamily
& 0xF0);
4134 if (Byte
!= FF_DONTCARE
)
4136 if (Byte
!= (TM
->tmPitchAndFamily
& 0xF0))
4138 /* Family Penalty 9000 */
4139 /* Requested a family, but the candidate's family is different. */
4142 if ((TM
->tmPitchAndFamily
& 0xF0) == FF_DONTCARE
)
4144 /* FamilyUnknown Penalty 8000 */
4145 /* Requested a family, but the candidate has no family. */
4150 /* Is the candidate a non-vector font? */
4151 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4153 /* Is lfHeight specified? */
4154 if (LogFont
->lfHeight
!= 0)
4156 if (labs(LogFont
->lfHeight
) < TM
->tmHeight
)
4158 /* HeightBigger Penalty 600 */
4159 /* The candidate is a nonvector font and is bigger than the
4160 requested height. */
4162 /* HeightBiggerDifference Penalty 150 */
4163 /* The candidate is a raster font and is larger than the
4164 requested height. Penalty * height difference */
4165 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4167 fNeedScaling
= TRUE
;
4169 if (TM
->tmHeight
< labs(LogFont
->lfHeight
))
4171 /* HeightSmaller Penalty 150 */
4172 /* The candidate is a raster font and is smaller than the
4173 requested height. Penalty * height difference */
4174 Penalty
+= 150 * labs(TM
->tmHeight
- labs(LogFont
->lfHeight
));
4176 fNeedScaling
= TRUE
;
4181 switch (LogFont
->lfPitchAndFamily
& 0xF0)
4183 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4184 switch (TM
->tmPitchAndFamily
& 0xF0)
4186 case FF_DECORATIVE
: case FF_SCRIPT
:
4187 /* FamilyUnlikely Penalty 50 */
4188 /* Requested a roman/modern/swiss family, but the
4189 candidate is decorative/script. */
4196 case FF_DECORATIVE
: case FF_SCRIPT
:
4197 switch (TM
->tmPitchAndFamily
& 0xF0)
4199 case FF_ROMAN
: case FF_MODERN
: case FF_SWISS
:
4200 /* FamilyUnlikely Penalty 50 */
4201 /* Or requested decorative/script, and the candidate is
4202 roman/modern/swiss. */
4212 if (LogFont
->lfWidth
!= 0)
4214 if (LogFont
->lfWidth
!= TM
->tmAveCharWidth
)
4216 /* Width Penalty 50 */
4217 /* Requested a nonzero width, but the candidate's width
4218 doesn't match. Penalty * width difference */
4219 Penalty
+= 50 * labs(LogFont
->lfWidth
- TM
->tmAveCharWidth
);
4221 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4222 fNeedScaling
= TRUE
;
4228 /* SizeSynth Penalty 50 */
4229 /* The candidate is a raster font that needs scaling by GDI. */
4233 if (!!LogFont
->lfItalic
!= !!TM
->tmItalic
)
4235 if (!LogFont
->lfItalic
&& ItalicFromStyle(style_name
))
4237 /* Italic Penalty 4 */
4238 /* Requested font and candidate font do not agree on italic status,
4239 and the desired result cannot be simulated. */
4240 /* Adjusted to 40 to satisfy (Oblique Penalty > Book Penalty). */
4243 else if (LogFont
->lfItalic
&& !ItalicFromStyle(style_name
))
4245 /* ItalicSim Penalty 1 */
4246 /* Requested italic font but the candidate is not italic,
4247 although italics can be simulated. */
4252 if (LogFont
->lfOutPrecision
== OUT_TT_PRECIS
)
4254 if (!(TM
->tmPitchAndFamily
& TMPF_TRUETYPE
))
4256 /* NotTrueType Penalty 4 */
4257 /* Requested OUT_TT_PRECIS, but the candidate is not a
4263 Long
= LogFont
->lfWeight
;
4264 if (LogFont
->lfWeight
== FW_DONTCARE
)
4266 if (Long
!= TM
->tmWeight
)
4268 /* Weight Penalty 3 */
4269 /* The candidate's weight does not match the requested weight.
4270 Penalty * (weight difference/10) */
4271 Penalty
+= 3 * (labs(Long
- TM
->tmWeight
) / 10);
4274 if (!LogFont
->lfUnderline
&& TM
->tmUnderlined
)
4276 /* Underline Penalty 3 */
4277 /* Requested font has no underline, but the candidate is
4282 if (!LogFont
->lfStrikeOut
&& TM
->tmStruckOut
)
4284 /* StrikeOut Penalty 3 */
4285 /* Requested font has no strike-out, but the candidate is
4290 /* Is the candidate a non-vector font? */
4291 if (!(TM
->tmPitchAndFamily
& (TMPF_TRUETYPE
| TMPF_VECTOR
)))
4293 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
< LogFont
->lfHeight
)
4295 /* VectorHeightSmaller Penalty 2 */
4296 /* Candidate is a vector font that is smaller than the
4297 requested height. Penalty * height difference */
4298 Penalty
+= 2 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4300 if (LogFont
->lfHeight
!= 0 && TM
->tmHeight
> LogFont
->lfHeight
)
4302 /* VectorHeightBigger Penalty 1 */
4303 /* Candidate is a vector font that is bigger than the
4304 requested height. Penalty * height difference */
4305 Penalty
+= 1 * labs(TM
->tmHeight
- LogFont
->lfHeight
);
4309 if (!(TM
->tmPitchAndFamily
& TMPF_DEVICE
))
4311 /* DeviceFavor Penalty 2 */
4312 /* Extra penalty for all nondevice fonts. */
4318 DPRINT("WARNING: Penalty:%ld < 200: RequestedNameW:%ls, "
4319 "ActualNameW:%ls, lfCharSet:%d, lfWeight:%ld, "
4320 "tmCharSet:%d, tmWeight:%ld\n",
4321 Penalty
, RequestedNameW
->Buffer
, ActualNameW
->Buffer
,
4322 LogFont
->lfCharSet
, LogFont
->lfWeight
,
4323 TM
->tmCharSet
, TM
->tmWeight
);
4326 return Penalty
; /* success */
4329 static __inline VOID
4330 FindBestFontFromList(FONTOBJ
**FontObj
, ULONG
*MatchPenalty
, LOGFONTW
*LogFont
,
4331 PUNICODE_STRING pRequestedNameW
,
4332 PUNICODE_STRING pActualNameW
, BYTE RequestedCharSet
,
4338 PFONT_ENTRY CurrentEntry
;
4340 ANSI_STRING ActualNameA
;
4341 UNICODE_STRING ActualNameW
, FullFaceNameW
;
4342 OUTLINETEXTMETRICW
*Otm
= NULL
;
4343 UINT OtmSize
, OldOtmSize
= 0;
4349 ASSERT(MatchPenalty
);
4351 ASSERT(pRequestedNameW
);
4354 /* get the FontObj of lowest penalty */
4355 Entry
= Head
->Flink
;
4356 while (Entry
!= Head
)
4358 CurrentEntry
= CONTAINING_RECORD(Entry
, FONT_ENTRY
, ListEntry
);
4359 FontGDI
= CurrentEntry
->Font
;
4361 Face
= FontGDI
->SharedFace
->Face
;
4363 /* create actual name */
4364 RtlInitAnsiString(&ActualNameA
, Face
->family_name
);
4365 Status
= RtlAnsiStringToUnicodeString(&ActualNameW
, &ActualNameA
, TRUE
);
4366 if (!NT_SUCCESS(Status
))
4369 Entry
= Entry
->Flink
;
4373 /* get text metrics */
4374 OtmSize
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
4375 if (OtmSize
> OldOtmSize
)
4378 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4379 Otm
= ExAllocatePoolWithTag(PagedPool
, OtmSize
, GDITAG_TEXT
);
4382 /* update FontObj if lowest penalty */
4385 IntGetOutlineTextMetrics(FontGDI
, OtmSize
, Otm
);
4386 TM
= &Otm
->otmTextMetrics
;
4387 OldOtmSize
= OtmSize
;
4389 /* create full name */
4390 pb
= (LPBYTE
)Otm
+ (WORD
)(DWORD_PTR
)Otm
->otmpFullName
;
4391 Status
= RtlCreateUnicodeString(&FullFaceNameW
, (LPWSTR
)pb
);
4392 if (!NT_SUCCESS(Status
))
4394 RtlFreeUnicodeString(&ActualNameW
);
4395 RtlFreeUnicodeString(&FullFaceNameW
);
4397 Entry
= Entry
->Flink
;
4401 Penalty
= GetFontPenalty(LogFont
, pRequestedNameW
, &ActualNameW
,
4402 &FullFaceNameW
, RequestedCharSet
,
4403 FontGDI
, Otm
, TM
, Face
->style_name
);
4404 if (*MatchPenalty
== 0xFFFFFFFF || Penalty
< *MatchPenalty
)
4406 DPRINT("%ls Penalty: %lu\n", FullFaceNameW
.Buffer
, Penalty
);
4407 RtlFreeUnicodeString(pActualNameW
);
4408 RtlCreateUnicodeString(pActualNameW
, ActualNameW
.Buffer
);
4410 *FontObj
= GDIToObj(FontGDI
, FONT
);
4411 *MatchPenalty
= Penalty
;
4414 RtlFreeUnicodeString(&FullFaceNameW
);
4418 RtlFreeUnicodeString(&ActualNameW
);
4421 Entry
= Entry
->Flink
;
4425 ExFreePoolWithTag(Otm
, GDITAG_TEXT
);
4431 IntFontType(PFONTGDI Font
)
4433 PS_FontInfoRec psfInfo
;
4434 FT_ULong tmp_size
= 0;
4435 FT_Face Face
= Font
->SharedFace
->Face
;
4437 if (FT_HAS_MULTIPLE_MASTERS(Face
))
4438 Font
->FontObj
.flFontType
|= FO_MULTIPLEMASTER
;
4439 if (FT_HAS_VERTICAL(Face
))
4440 Font
->FontObj
.flFontType
|= FO_VERT_FACE
;
4441 if (!FT_IS_SCALABLE(Face
))
4442 Font
->FontObj
.flFontType
|= FO_TYPE_RASTER
;
4443 if (FT_IS_SFNT(Face
))
4445 Font
->FontObj
.flFontType
|= FO_TYPE_TRUETYPE
;
4446 if (FT_Get_Sfnt_Table(Face
, ft_sfnt_post
))
4447 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4449 if (!FT_Get_PS_Font_Info(Face
, &psfInfo
))
4451 Font
->FontObj
.flFontType
|= FO_POSTSCRIPT
;
4453 /* Check for the presence of the 'CFF ' table to check if the font is Type1 */
4454 if (!FT_Load_Sfnt_Table(Face
, TTAG_CFF
, 0, NULL
, &tmp_size
))
4456 Font
->FontObj
.flFontType
|= (FO_CFF
|FO_POSTSCRIPT
);
4462 TextIntRealizeFont(HFONT FontHandle
, PTEXTOBJ pTextObj
)
4464 NTSTATUS Status
= STATUS_SUCCESS
;
4466 UNICODE_STRING ActualNameW
, RequestedNameW
;
4467 PPROCESSINFO Win32Process
;
4471 BYTE RequestedCharSet
;
4475 TextObj
= TEXTOBJ_LockText(FontHandle
);
4476 if (NULL
== TextObj
)
4478 return STATUS_INVALID_HANDLE
;
4481 if (TextObj
->fl
& TEXTOBJECT_INIT
)
4483 TEXTOBJ_UnlockText(TextObj
);
4484 return STATUS_SUCCESS
;
4492 RtlInitUnicodeString(&ActualNameW
, NULL
);
4494 pLogFont
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
4495 if (!RtlCreateUnicodeString(&RequestedNameW
, pLogFont
->lfFaceName
))
4497 if (!pTextObj
) TEXTOBJ_UnlockText(TextObj
);
4498 return STATUS_NO_MEMORY
;
4502 RequestedCharSet
= pLogFont
->lfCharSet
;
4503 DPRINT("Font '%ls,%u' is substituted by: ",
4504 RequestedNameW
.Buffer
, RequestedCharSet
);
4505 SubstituteFontRecurse(&RequestedNameW
, &RequestedCharSet
);
4506 DPRINT("'%ls,%u'.\n", RequestedNameW
.Buffer
, RequestedCharSet
);
4508 MatchPenalty
= 0xFFFFFFFF;
4509 TextObj
->Font
= NULL
;
4511 Win32Process
= PsGetCurrentProcessWin32Process();
4513 /* Search private fonts */
4514 IntLockProcessPrivateFonts(Win32Process
);
4515 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, pLogFont
,
4516 &RequestedNameW
, &ActualNameW
, RequestedCharSet
,
4517 &Win32Process
->PrivateFontListHead
);
4518 IntUnLockProcessPrivateFonts(Win32Process
);
4520 /* Search system fonts */
4522 FindBestFontFromList(&TextObj
->Font
, &MatchPenalty
, pLogFont
,
4523 &RequestedNameW
, &ActualNameW
, RequestedCharSet
,
4525 IntUnLockGlobalFonts
;
4527 if (NULL
== TextObj
->Font
)
4529 DPRINT1("Request font %S not found, no fonts loaded at all\n",
4530 pLogFont
->lfFaceName
);
4531 Status
= STATUS_NOT_FOUND
;
4535 PFONTGDI FontGdi
= ObjToGDI(TextObj
->Font
, FONT
);
4536 // Need hdev, when freetype is loaded need to create DEVOBJ for
4537 // Consumer and Producer.
4538 TextObj
->Font
->iUniq
= 1; // Now it can be cached.
4539 IntFontType(FontGdi
);
4540 FontGdi
->flType
= TextObj
->Font
->flFontType
;
4541 FontGdi
->RequestUnderline
= pLogFont
->lfUnderline
? 0xFF : 0;
4542 FontGdi
->RequestStrikeOut
= pLogFont
->lfStrikeOut
? 0xFF : 0;
4543 FontGdi
->RequestItalic
= pLogFont
->lfItalic
? 0xFF : 0;
4544 if (pLogFont
->lfWeight
!= FW_DONTCARE
)
4545 FontGdi
->RequestWeight
= pLogFont
->lfWeight
;
4547 FontGdi
->RequestWeight
= FW_NORMAL
;
4549 Face
= FontGdi
->SharedFace
->Face
;
4551 //FontGdi->OriginalWeight = WeightFromStyle(Face->style_name);
4553 if (!FontGdi
->OriginalItalic
)
4554 FontGdi
->OriginalItalic
= ItalicFromStyle(Face
->style_name
);
4556 TextObj
->fl
|= TEXTOBJECT_INIT
;
4557 Status
= STATUS_SUCCESS
;
4559 DPRINT("RequestedNameW: %ls (CharSet: %d) -> ActualNameW: %ls (CharSet: %d)\n",
4560 RequestedNameW
.Buffer
, pLogFont
->lfCharSet
,
4561 ActualNameW
.Buffer
, FontGdi
->CharSet
);
4564 RtlFreeUnicodeString(&RequestedNameW
);
4565 RtlFreeUnicodeString(&ActualNameW
);
4566 if (!pTextObj
) TEXTOBJ_UnlockText(TextObj
);
4568 ASSERT((NT_SUCCESS(Status
) ^ (NULL
== TextObj
->Font
)) != 0);
4578 POBJECT_NAME_INFORMATION NameInfo
,
4580 PUNICODE_STRING FileName
)
4583 OBJECT_ATTRIBUTES ObjectAttributes
;
4585 IO_STATUS_BLOCK IoStatusBlock
;
4588 InitializeObjectAttributes(&ObjectAttributes
,
4590 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
4594 Status
= ZwOpenFile(
4596 0, // FILE_READ_ATTRIBUTES,
4599 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
4602 if (!NT_SUCCESS(Status
))
4604 DPRINT("ZwOpenFile() failed (Status = 0x%lx)\n", Status
);
4608 Status
= ZwQueryObject(hFile
, ObjectNameInformation
, NameInfo
, Size
, &Desired
);
4610 if (!NT_SUCCESS(Status
))
4612 DPRINT("ZwQueryObject() failed (Status = %lx)\n", Status
);
4620 EqualFamilyInfo(FONTFAMILYINFO
*pInfo1
, FONTFAMILYINFO
*pInfo2
)
4622 UNICODE_STRING Str1
, Str2
;
4623 ENUMLOGFONTEXW
*pLog1
= &pInfo1
->EnumLogFontEx
;
4624 ENUMLOGFONTEXW
*pLog2
= &pInfo2
->EnumLogFontEx
;
4625 RtlInitUnicodeString(&Str1
, pLog1
->elfLogFont
.lfFaceName
);
4626 RtlInitUnicodeString(&Str2
, pLog2
->elfLogFont
.lfFaceName
);
4627 if (!RtlEqualUnicodeString(&Str1
, &Str2
, TRUE
))
4631 if ((pLog1
->elfStyle
!= NULL
) != (pLog2
->elfStyle
!= NULL
))
4633 if (pLog1
->elfStyle
!= NULL
)
4635 RtlInitUnicodeString(&Str1
, pLog1
->elfStyle
);
4636 RtlInitUnicodeString(&Str2
, pLog2
->elfStyle
);
4637 if (!RtlEqualUnicodeString(&Str1
, &Str2
, TRUE
))
4646 IntAddNameFromFamInfo(LPWSTR psz
, FONTFAMILYINFO
*FamInfo
)
4648 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfLogFont
.lfFaceName
);
4649 if (FamInfo
->EnumLogFontEx
.elfStyle
[0] &&
4650 _wcsicmp(FamInfo
->EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4653 wcscat(psz
, FamInfo
->EnumLogFontEx
.elfStyle
);
4659 IntGdiGetFontResourceInfo(
4660 PUNICODE_STRING FileName
,
4665 UNICODE_STRING EntryFileName
;
4666 POBJECT_NAME_INFORMATION NameInfo1
, NameInfo2
;
4667 PLIST_ENTRY ListEntry
;
4668 PFONT_ENTRY FontEntry
;
4669 ULONG Size
, i
, Count
;
4672 FONTFAMILYINFO
*FamInfo
;
4673 const ULONG MaxFamInfo
= 64;
4676 DPRINT("IntGdiGetFontResourceInfo: dwType == %lu\n", dwType
);
4678 /* Create buffer for full path name */
4679 Size
= sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
* sizeof(WCHAR
);
4680 NameInfo1
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4683 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4687 /* Get the full path name */
4688 if (!IntGetFullFileName(NameInfo1
, Size
, FileName
))
4690 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4694 /* Create a buffer for the entries' names */
4695 NameInfo2
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_FINF
);
4698 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4699 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4703 FamInfo
= ExAllocatePoolWithTag(PagedPool
,
4704 sizeof(FONTFAMILYINFO
) * MaxFamInfo
,
4708 ExFreePoolWithTag(NameInfo2
, TAG_FINF
);
4709 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4710 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4713 /* Try to find the pathname in the global font list */
4716 for (ListEntry
= FontListHead
.Flink
; ListEntry
!= &FontListHead
;
4717 ListEntry
= ListEntry
->Flink
)
4719 FontEntry
= CONTAINING_RECORD(ListEntry
, FONT_ENTRY
, ListEntry
);
4720 if (FontEntry
->Font
->Filename
== NULL
)
4723 RtlInitUnicodeString(&EntryFileName
, FontEntry
->Font
->Filename
);
4724 if (!IntGetFullFileName(NameInfo2
, Size
, &EntryFileName
))
4727 if (!RtlEqualUnicodeString(&NameInfo1
->Name
, &NameInfo2
->Name
, FALSE
))
4731 FontFamilyFillInfo(&FamInfo
[Count
], FontEntry
->FaceName
.Buffer
,
4732 NULL
, FontEntry
->Font
);
4733 for (i
= 0; i
< Count
; ++i
)
4735 if (EqualFamilyInfo(&FamInfo
[i
], &FamInfo
[Count
]))
4745 if (Count
>= MaxFamInfo
)
4749 IntUnLockGlobalFonts
;
4751 /* Free the buffers */
4752 ExFreePoolWithTag(NameInfo1
, TAG_FINF
);
4753 ExFreePool(NameInfo2
);
4755 if (Count
== 0 && dwType
!= 5)
4757 /* Font could not be found in system table
4758 dwType == 5 will still handle this */
4759 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
4766 case 0: /* FIXME: Returns 1 or 2, don't know what this is atm */
4767 Size
= sizeof(DWORD
);
4775 if (*pdwBytes
>= Size
)
4777 *(DWORD
*)pBuffer
= Count
;
4784 case 1: /* copy the font title */
4785 /* calculate the required size */
4787 Size
+= wcslen(FamInfo
[0].EnumLogFontEx
.elfLogFont
.lfFaceName
);
4788 if (FamInfo
[0].EnumLogFontEx
.elfStyle
[0] &&
4789 _wcsicmp(FamInfo
[0].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4791 Size
+= 1 + wcslen(FamInfo
[0].EnumLogFontEx
.elfStyle
);
4793 for (i
= 1; i
< Count
; ++i
)
4795 Size
+= 3; /* " & " */
4796 Size
+= wcslen(FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfFaceName
);
4797 if (FamInfo
[i
].EnumLogFontEx
.elfStyle
[0] &&
4798 _wcsicmp(FamInfo
[i
].EnumLogFontEx
.elfStyle
, L
"Regular") != 0)
4800 Size
+= 1 + wcslen(FamInfo
[i
].EnumLogFontEx
.elfStyle
);
4803 Size
+= 2; /* "\0\0" */
4804 Size
*= sizeof(WCHAR
);
4813 if (*pdwBytes
>= Size
)
4815 /* store font title to buffer */
4816 WCHAR
*psz
= pBuffer
;
4818 IntAddNameFromFamInfo(psz
, &FamInfo
[0]);
4819 for (i
= 1; i
< Count
; ++i
)
4821 wcscat(psz
, L
" & ");
4822 IntAddNameFromFamInfo(psz
, &FamInfo
[i
]);
4824 psz
[wcslen(psz
) + 1] = UNICODE_NULL
;
4830 *pdwBytes
= 1024; /* this is confirmed value */
4835 case 2: /* Copy an array of LOGFONTW */
4836 Size
= Count
* sizeof(LOGFONTW
);
4844 if (*pdwBytes
>= Size
)
4846 pbBuffer
= (LPBYTE
)pBuffer
;
4847 for (i
= 0; i
< Count
; ++i
)
4849 FamInfo
[i
].EnumLogFontEx
.elfLogFont
.lfWidth
= 0;
4850 RtlCopyMemory(pbBuffer
, &FamInfo
[i
].EnumLogFontEx
.elfLogFont
, sizeof(LOGFONTW
));
4851 pbBuffer
+= sizeof(LOGFONTW
);
4859 *pdwBytes
= 1024; /* this is confirmed value */
4864 Size
= sizeof(DWORD
);
4872 if (*pdwBytes
>= Size
)
4874 /* FIXME: What exactly is copied here? */
4875 *(DWORD
*)pBuffer
= 1;
4882 case 4: /* full file path */
4883 if (FileName
->Length
>= 4 * sizeof(WCHAR
))
4885 /* The beginning of FileName is \??\ */
4886 LPWSTR pch
= FileName
->Buffer
+ 4;
4887 DWORD Length
= FileName
->Length
- 4 * sizeof(WCHAR
);
4889 Size
= Length
+ sizeof(WCHAR
);
4897 if (*pdwBytes
>= Size
)
4899 RtlCopyMemory(pBuffer
, pch
, Size
);
4907 case 5: /* Looks like a BOOL that is copied, TRUE, if the font was not found */
4908 Size
= sizeof(BOOL
);
4916 if (*pdwBytes
>= Size
)
4918 *(BOOL
*)pBuffer
= Count
== 0;
4925 ExFreePoolWithTag(FamInfo
, TAG_FINF
);
4933 ftGdiRealizationInfo(PFONTGDI Font
, PREALIZATION_INFO Info
)
4935 if (FT_HAS_FIXED_SIZES(Font
->SharedFace
->Face
))
4936 Info
->iTechnology
= RI_TECH_BITMAP
;
4939 if (FT_IS_SCALABLE(Font
->SharedFace
->Face
))
4940 Info
->iTechnology
= RI_TECH_SCALABLE
;
4942 Info
->iTechnology
= RI_TECH_FIXED
;
4944 Info
->iUniq
= Font
->FontObj
.iUniq
;
4945 Info
->dwUnknown
= -1;
4952 ftGdiGetKerningPairs( PFONTGDI Font
,
4954 LPKERNINGPAIR pKerningPair
)
4958 FT_Face face
= Font
->SharedFace
->Face
;
4960 if (FT_HAS_KERNING(face
) && face
->charmap
->encoding
== FT_ENCODING_UNICODE
)
4962 FT_UInt previous_index
= 0, glyph_index
= 0;
4963 FT_ULong char_code
, char_previous
;
4966 char_previous
= char_code
= FT_Get_First_Char(face
, &glyph_index
);
4972 if (previous_index
&& glyph_index
)
4974 FT_Get_Kerning(face
, previous_index
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
4976 if (pKerningPair
&& cPairs
)
4978 pKerningPair
[i
].wFirst
= char_previous
;
4979 pKerningPair
[i
].wSecond
= char_code
;
4980 pKerningPair
[i
].iKernAmount
= delta
.x
;
4982 if (i
== cPairs
) break;
4986 previous_index
= glyph_index
;
4987 char_previous
= char_code
;
4988 char_code
= FT_Get_Next_Char(face
, char_code
, &glyph_index
);
4996 ///////////////////////////////////////////////////////////////////////////
4998 // Functions needing sorting.
5000 ///////////////////////////////////////////////////////////////////////////
5002 NtGdiGetFontFamilyInfo(HDC Dc
,
5003 LPLOGFONTW UnsafeLogFont
,
5004 PFONTFAMILYINFO UnsafeInfo
,
5009 PFONTFAMILYINFO Info
;
5011 PPROCESSINFO Win32Process
;
5013 /* Make a safe copy */
5014 Status
= MmCopyFromCaller(&LogFont
, UnsafeLogFont
, sizeof(LOGFONTW
));
5015 if (! NT_SUCCESS(Status
))
5017 EngSetLastError(ERROR_INVALID_PARAMETER
);
5021 /* Allocate space for a safe copy */
5022 Info
= ExAllocatePoolWithTag(PagedPool
, Size
* sizeof(FONTFAMILYINFO
), GDITAG_TEXT
);
5025 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
5029 /* Enumerate font families in the global list */
5032 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
, &FontListHead
) )
5034 IntUnLockGlobalFonts
;
5035 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5038 IntUnLockGlobalFonts
;
5040 /* Enumerate font families in the process local list */
5041 Win32Process
= PsGetCurrentProcessWin32Process();
5042 IntLockProcessPrivateFonts(Win32Process
);
5043 if (! GetFontFamilyInfoForList(&LogFont
, Info
, &Count
, Size
,
5044 &Win32Process
->PrivateFontListHead
))
5046 IntUnLockProcessPrivateFonts(Win32Process
);
5047 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5050 IntUnLockProcessPrivateFonts(Win32Process
);
5052 /* Enumerate font families in the registry */
5053 if (! GetFontFamilyInfoForSubstitutes(&LogFont
, Info
, &Count
, Size
))
5055 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5059 /* Return data to caller */
5062 Status
= MmCopyToCaller(UnsafeInfo
, Info
,
5063 (Count
< Size
? Count
: Size
) * sizeof(FONTFAMILYINFO
));
5064 if (! NT_SUCCESS(Status
))
5066 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5067 EngSetLastError(ERROR_INVALID_PARAMETER
);
5072 ExFreePoolWithTag(Info
, GDITAG_TEXT
);
5079 ScaleLong(LONG lValue
, PFLOATOBJ pef
)
5083 /* Check if we have scaling different from 1 */
5084 if (!FLOATOBJ_Equal(pef
, (PFLOATOBJ
)&gef1
))
5086 /* Need to multiply */
5087 FLOATOBJ_SetLong(&efTemp
, lValue
);
5088 FLOATOBJ_Mul(&efTemp
, pef
);
5089 lValue
= FLOATOBJ_GetLong(&efTemp
);
5102 IN OPTIONAL PRECTL lprc
,
5105 IN OPTIONAL LPINT Dx
,
5106 IN DWORD dwCodePage
)
5110 * Call EngTextOut, which does the real work (calling DrvTextOut where
5117 SURFACE
*psurf
= NULL
;
5118 int error
, glyph_index
, i
;
5121 FT_BitmapGlyph realglyph
;
5122 LONGLONG TextLeft
, RealXStart
;
5123 ULONG TextTop
, previous
, BackgroundLeft
;
5124 FT_Bool use_kerning
;
5125 RECTL DestRect
, MaskRect
;
5126 POINTL SourcePoint
, BrushOrigin
;
5127 HBITMAP HSourceGlyph
;
5128 SURFOBJ
*SourceGlyphSurf
;
5133 PTEXTOBJ TextObj
= NULL
;
5134 EXLATEOBJ exloRGB2Dst
, exloDst2RGB
;
5135 FT_Render_Mode RenderMode
;
5138 BOOL DoBreak
= FALSE
;
5140 PMATRIX pmxWorldToDevice
;
5141 LONG fixAscender
, fixDescender
;
5144 BOOL EmuBold
, EmuItalic
;
5147 // TODO: Write test-cases to exactly match real Windows in different
5148 // bad parameters (e.g. does Windows check the DC or the RECT first?).
5149 dc
= DC_LockDc(hDC
);
5152 EngSetLastError(ERROR_INVALID_HANDLE
);
5155 if (dc
->dctype
== DC_TYPE_INFO
)
5158 /* Yes, Windows really returns TRUE in this case */
5162 pdcattr
= dc
->pdcattr
;
5164 if ((fuOptions
& ETO_OPAQUE
) || pdcattr
->jBkMode
== OPAQUE
)
5166 if (pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
5167 DC_vUpdateBackgroundBrush(dc
);
5170 /* Check if String is valid */
5171 if ((Count
> 0xFFFF) || (Count
> 0 && String
== NULL
))
5173 EngSetLastError(ERROR_INVALID_PARAMETER
);
5177 DxShift
= fuOptions
& ETO_PDY
? 1 : 0;
5179 if (PATH_IsPathOpen(dc
->dclevel
))
5181 if (!PATH_ExtTextOut( dc
,
5185 (const RECTL
*)lprc
,
5188 (const INT
*)Dx
)) goto fail
;
5192 if (lprc
&& (fuOptions
& (ETO_OPAQUE
| ETO_CLIPPED
)))
5194 IntLPtoDP(dc
, (POINT
*)lprc
, 2);
5197 if (pdcattr
->lTextAlign
& TA_UPDATECP
)
5199 Start
.x
= pdcattr
->ptlCurrent
.x
;
5200 Start
.y
= pdcattr
->ptlCurrent
.y
;
5206 IntLPtoDP(dc
, &Start
, 1);
5207 RealXStart
= ((LONGLONG
)Start
.x
+ dc
->ptlDCOrig
.x
) << 6;
5208 YStart
= Start
.y
+ dc
->ptlDCOrig
.y
;
5217 if (!dc
->dclevel
.pSurface
)
5222 if ((fuOptions
& ETO_OPAQUE
) && lprc
)
5224 DestRect
.left
= lprc
->left
;
5225 DestRect
.top
= lprc
->top
;
5226 DestRect
.right
= lprc
->right
;
5227 DestRect
.bottom
= lprc
->bottom
;
5229 DestRect
.left
+= dc
->ptlDCOrig
.x
;
5230 DestRect
.top
+= dc
->ptlDCOrig
.y
;
5231 DestRect
.right
+= dc
->ptlDCOrig
.x
;
5232 DestRect
.bottom
+= dc
->ptlDCOrig
.y
;
5234 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5236 IntUpdateBoundsRect(dc
, &DestRect
);
5239 DC_vPrepareDCsForBlit(dc
, &DestRect
, NULL
, NULL
);
5241 if (pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
5242 DC_vUpdateBackgroundBrush(dc
);
5244 psurf
= dc
->dclevel
.pSurface
;
5254 &dc
->eboBackground
.BrushObject
,
5256 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5257 fuOptions
&= ~ETO_OPAQUE
;
5258 DC_vFinishBlit(dc
, NULL
);
5262 if (pdcattr
->jBkMode
== OPAQUE
)
5264 fuOptions
|= ETO_OPAQUE
;
5268 TextObj
= RealizeFontInit(pdcattr
->hlfntNew
);
5269 if (TextObj
== NULL
)
5274 FontObj
= TextObj
->Font
;
5276 FontGDI
= ObjToGDI(FontObj
, FONT
);
5280 face
= FontGDI
->SharedFace
->Face
;
5282 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
5283 EmuBold
= (plf
->lfWeight
>= FW_BOLD
&& FontGDI
->OriginalWeight
<= FW_NORMAL
);
5284 EmuItalic
= (plf
->lfItalic
&& !FontGDI
->OriginalItalic
);
5286 Render
= IntIsFontRenderingEnabled();
5288 RenderMode
= IntGetFontRenderMode(plf
);
5290 RenderMode
= FT_RENDER_MODE_MONO
;
5292 if (!TextIntUpdateSize(dc
, TextObj
, FontGDI
, FALSE
))
5298 if (dc
->pdcattr
->iGraphicsMode
== GM_ADVANCED
)
5300 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
5301 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5303 fixAscender
= ScaleLong(face
->size
->metrics
.ascender
, &pmxWorldToDevice
->efM22
);
5304 fixDescender
= ScaleLong(face
->size
->metrics
.descender
, &pmxWorldToDevice
->efM22
);
5308 pmxWorldToDevice
= (PMATRIX
)&gmxWorldToDeviceDefault
;
5309 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
5311 fixAscender
= face
->size
->metrics
.ascender
;
5312 fixDescender
= face
->size
->metrics
.descender
;
5316 * Process the vertical alignment and determine the yoff.
5319 if (pdcattr
->lTextAlign
& TA_BASELINE
)
5321 else if (pdcattr
->lTextAlign
& TA_BOTTOM
)
5322 yoff
= -fixDescender
>> 6;
5324 yoff
= fixAscender
>> 6;
5326 use_kerning
= FT_HAS_KERNING(face
);
5330 * Process the horizontal alignment and modify XStart accordingly.
5333 if (pdcattr
->lTextAlign
& (TA_RIGHT
| TA_CENTER
))
5335 ULONGLONG TextWidth
= 0;
5336 LPCWSTR TempText
= String
;
5340 * Calculate width of the text.
5345 iStart
= Count
< 2 ? 0 : Count
- 2;
5346 TextWidth
= Count
< 2 ? 0 : (Dx
[(Count
-2)<<DxShift
] << 6);
5352 TempText
= String
+ iStart
;
5354 for (i
= iStart
; i
< Count
; i
++)
5356 if (fuOptions
& ETO_GLYPH_INDEX
)
5357 glyph_index
= *TempText
;
5359 glyph_index
= FT_Get_Char_Index(face
, *TempText
);
5361 if (EmuBold
|| EmuItalic
)
5364 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
,
5365 plf
->lfHeight
, pmxWorldToDevice
);
5368 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5371 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index
);
5374 glyph
= face
->glyph
;
5375 if (EmuBold
|| EmuItalic
)
5378 FT_GlyphSlot_Embolden(glyph
);
5380 FT_GlyphSlot_Oblique(glyph
);
5381 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5385 realglyph
= ftGdiGlyphCacheSet(face
,
5394 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5400 /* Retrieve kerning distance */
5401 if (use_kerning
&& previous
&& glyph_index
)
5404 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5405 TextWidth
+= delta
.x
;
5408 TextWidth
+= realglyph
->root
.advance
.x
>> 10;
5410 if (EmuBold
|| EmuItalic
)
5412 FT_Done_Glyph((FT_Glyph
)realglyph
);
5416 previous
= glyph_index
;
5422 if ((pdcattr
->lTextAlign
& TA_CENTER
) == TA_CENTER
)
5424 RealXStart
-= TextWidth
/ 2;
5428 RealXStart
-= TextWidth
;
5432 /* Lock blit with a dummy rect */
5433 DC_vPrepareDCsForBlit(dc
, NULL
, NULL
, NULL
);
5435 psurf
= dc
->dclevel
.pSurface
;
5436 SurfObj
= &psurf
->SurfObj
;
5438 EXLATEOBJ_vInitialize(&exloRGB2Dst
, &gpalRGB
, psurf
->ppal
, 0, 0, 0);
5439 EXLATEOBJ_vInitialize(&exloDst2RGB
, psurf
->ppal
, &gpalRGB
, 0, 0, 0);
5441 if ((fuOptions
& ETO_OPAQUE
) && (dc
->pdcattr
->ulDirty_
& DIRTY_BACKGROUND
))
5442 DC_vUpdateBackgroundBrush(dc
) ;
5444 if(dc
->pdcattr
->ulDirty_
& DIRTY_TEXT
)
5445 DC_vUpdateTextBrush(dc
) ;
5447 if (!face
->units_per_EM
)
5453 thickness
= face
->underline_thickness
*
5454 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5459 if ((fuOptions
& ETO_OPAQUE
) && plf
->lfItalic
)
5461 /* Draw background */
5462 TextLeft
= RealXStart
;
5464 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5465 for (i
= 0; i
< Count
; ++i
)
5467 if (fuOptions
& ETO_GLYPH_INDEX
)
5468 glyph_index
= String
[i
];
5470 glyph_index
= FT_Get_Char_Index(face
, String
[i
]);
5472 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5475 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5477 DC_vFinishBlit(dc
, NULL
);
5481 glyph
= face
->glyph
;
5483 FT_GlyphSlot_Embolden(glyph
);
5485 FT_GlyphSlot_Oblique(glyph
);
5486 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5489 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5491 DC_vFinishBlit(dc
, NULL
);
5495 /* retrieve kerning distance and move pen position */
5496 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5499 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5500 TextLeft
+= delta
.x
;
5502 DPRINT("TextLeft: %I64d\n", TextLeft
);
5503 DPRINT("TextTop: %lu\n", TextTop
);
5504 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5506 DestRect
.left
= BackgroundLeft
;
5507 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5508 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5509 DestRect
.bottom
= TextTop
+ yoff
+ ((32 - fixDescender
) >> 6);
5510 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5511 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5513 IntUpdateBoundsRect(dc
, &DestRect
);
5524 &dc
->eboBackground
.BrushObject
,
5526 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5527 MouseSafetyOnDrawEnd(dc
->ppdev
);
5528 BackgroundLeft
= DestRect
.right
;
5530 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5531 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5532 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5533 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5535 bitSize
.cx
= realglyph
->bitmap
.width
;
5536 bitSize
.cy
= realglyph
->bitmap
.rows
;
5537 MaskRect
.right
= realglyph
->bitmap
.width
;
5538 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5542 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
5543 DPRINT("New TextLeft: %I64d\n", TextLeft
);
5547 // FIXME this should probably be a matrix transform with TextTop as well.
5548 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
5549 if (FLOATOBJ_Equal0(&Scale
))
5550 FLOATOBJ_Set1(&Scale
);
5552 /* do the shift before multiplying to preserve precision */
5553 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
5554 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
5555 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
5560 TextTop
-= Dx
[2 * i
+ 1] << 6;
5563 previous
= glyph_index
;
5565 if (EmuBold
|| EmuItalic
)
5567 FT_Done_Glyph((FT_Glyph
)realglyph
);
5574 * The main rendering loop.
5576 TextLeft
= RealXStart
;
5578 BackgroundLeft
= (RealXStart
+ 32) >> 6;
5579 for (i
= 0; i
< Count
; ++i
)
5581 if (fuOptions
& ETO_GLYPH_INDEX
)
5582 glyph_index
= String
[i
];
5584 glyph_index
= FT_Get_Char_Index(face
, String
[i
]);
5586 if (EmuBold
|| EmuItalic
)
5589 realglyph
= ftGdiGlyphCacheGet(face
, glyph_index
,
5590 plf
->lfHeight
, pmxWorldToDevice
);
5593 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
5596 DPRINT1("Failed to load and render glyph! [index: %d]\n", glyph_index
);
5598 DC_vFinishBlit(dc
, NULL
);
5602 glyph
= face
->glyph
;
5603 if (EmuBold
|| EmuItalic
)
5606 FT_GlyphSlot_Embolden(glyph
);
5608 FT_GlyphSlot_Oblique(glyph
);
5609 realglyph
= ftGdiGlyphSet(face
, glyph
, RenderMode
);
5613 realglyph
= ftGdiGlyphCacheSet(face
,
5622 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index
);
5624 DC_vFinishBlit(dc
, NULL
);
5629 /* retrieve kerning distance and move pen position */
5630 if (use_kerning
&& previous
&& glyph_index
&& NULL
== Dx
)
5633 FT_Get_Kerning(face
, previous
, glyph_index
, 0, &delta
);
5634 TextLeft
+= delta
.x
;
5636 DPRINT("TextLeft: %I64d\n", TextLeft
);
5637 DPRINT("TextTop: %lu\n", TextTop
);
5638 DPRINT("Advance: %d\n", realglyph
->root
.advance
.x
);
5640 if ((fuOptions
& ETO_OPAQUE
) && !plf
->lfItalic
)
5642 DestRect
.left
= BackgroundLeft
;
5643 DestRect
.right
= (TextLeft
+ (realglyph
->root
.advance
.x
>> 10) + 32) >> 6;
5644 DestRect
.top
= TextTop
+ yoff
- ((fixAscender
+ 32) >> 6);
5645 DestRect
.bottom
= TextTop
+ yoff
+ ((32 - fixDescender
) >> 6);
5646 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5647 if (dc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
5649 IntUpdateBoundsRect(dc
, &DestRect
);
5660 &dc
->eboBackground
.BrushObject
,
5662 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY
));
5663 MouseSafetyOnDrawEnd(dc
->ppdev
);
5664 BackgroundLeft
= DestRect
.right
;
5667 DestRect
.left
= ((TextLeft
+ 32) >> 6) + realglyph
->left
;
5668 DestRect
.right
= DestRect
.left
+ realglyph
->bitmap
.width
;
5669 DestRect
.top
= TextTop
+ yoff
- realglyph
->top
;
5670 DestRect
.bottom
= DestRect
.top
+ realglyph
->bitmap
.rows
;
5672 bitSize
.cx
= realglyph
->bitmap
.width
;
5673 bitSize
.cy
= realglyph
->bitmap
.rows
;
5674 MaskRect
.right
= realglyph
->bitmap
.width
;
5675 MaskRect
.bottom
= realglyph
->bitmap
.rows
;
5677 /* Check if the bitmap has any pixels */
5678 if ((bitSize
.cx
!= 0) && (bitSize
.cy
!= 0))
5681 * We should create the bitmap out of the loop at the biggest possible
5682 * glyph size. Then use memset with 0 to clear it and sourcerect to
5683 * limit the work of the transbitblt.
5685 HSourceGlyph
= EngCreateBitmap(bitSize
, realglyph
->bitmap
.pitch
,
5686 BMF_8BPP
, BMF_TOPDOWN
,
5687 realglyph
->bitmap
.buffer
);
5688 if ( !HSourceGlyph
)
5690 DPRINT1("WARNING: EngCreateBitmap() failed!\n");
5691 // FT_Done_Glyph(realglyph);
5693 DC_vFinishBlit(dc
, NULL
);
5696 SourceGlyphSurf
= EngLockSurface((HSURF
)HSourceGlyph
);
5697 if ( !SourceGlyphSurf
)
5699 EngDeleteSurface((HSURF
)HSourceGlyph
);
5700 DPRINT1("WARNING: EngLockSurface() failed!\n");
5702 DC_vFinishBlit(dc
, NULL
);
5707 * Use the font data as a mask to paint onto the DCs surface using a
5710 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5711 DestRect
.right
>= lprc
->right
+ dc
->ptlDCOrig
.x
)
5713 // We do the check '>=' instead of '>' to possibly save an iteration
5714 // through this loop, since it's breaking after the drawing is done,
5715 // and x is always incremented.
5716 DestRect
.right
= lprc
->right
+ dc
->ptlDCOrig
.x
;
5719 if (lprc
&& (fuOptions
& ETO_CLIPPED
) &&
5720 DestRect
.bottom
>= lprc
->bottom
+ dc
->ptlDCOrig
.y
)
5722 DestRect
.bottom
= lprc
->bottom
+ dc
->ptlDCOrig
.y
;
5724 MouseSafetyOnDrawStart(dc
->ppdev
, DestRect
.left
, DestRect
.top
, DestRect
.right
, DestRect
.bottom
);
5733 &dc
->eboText
.BrushObject
,
5736 DPRINT1("Failed to MaskBlt a glyph!\n");
5739 MouseSafetyOnDrawEnd(dc
->ppdev
) ;
5741 EngUnlockSurface(SourceGlyphSurf
);
5742 EngDeleteSurface((HSURF
)HSourceGlyph
);
5750 if (plf
->lfUnderline
)
5753 if (!face
->units_per_EM
)
5759 position
= face
->underline_position
*
5760 face
->size
->metrics
.y_ppem
/ face
->units_per_EM
;
5762 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5766 &dc
->eboText
.BrushObject
,
5768 TextTop
+ yoff
- position
+ i
,
5769 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5770 TextTop
+ yoff
- position
+ i
,
5772 ROP2_TO_MIX(R2_COPYPEN
));
5775 if (plf
->lfStrikeOut
)
5778 for (i
= -thickness
/ 2; i
< -thickness
/ 2 + thickness
; ++i
)
5782 &dc
->eboText
.BrushObject
,
5784 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5785 ((TextLeft
+ (realglyph
->root
.advance
.x
>> 10)) >> 6),
5786 TextTop
+ yoff
- (fixAscender
>> 6) / 3 + i
,
5788 ROP2_TO_MIX(R2_COPYPEN
));
5794 TextLeft
+= realglyph
->root
.advance
.x
>> 10;
5795 DPRINT("New TextLeft: %I64d\n", TextLeft
);
5799 // FIXME this should probably be a matrix transform with TextTop as well.
5800 Scale
= pdcattr
->mxWorldToDevice
.efM11
;
5801 if (FLOATOBJ_Equal0(&Scale
))
5802 FLOATOBJ_Set1(&Scale
);
5804 /* do the shift before multiplying to preserve precision */
5805 FLOATOBJ_MulLong(&Scale
, Dx
[i
<<DxShift
] << 6);
5806 TextLeft
+= FLOATOBJ_GetLong(&Scale
);
5807 DPRINT("New TextLeft2: %I64d\n", TextLeft
);
5812 TextTop
-= Dx
[2 * i
+ 1] << 6;
5815 previous
= glyph_index
;
5817 if (EmuBold
|| EmuItalic
)
5819 FT_Done_Glyph((FT_Glyph
)realglyph
);
5824 if (pdcattr
->lTextAlign
& TA_UPDATECP
) {
5825 pdcattr
->ptlCurrent
.x
= DestRect
.right
- dc
->ptlDCOrig
.x
;
5830 DC_vFinishBlit(dc
, NULL
) ;
5832 EXLATEOBJ_vCleanup(&exloRGB2Dst
);
5833 EXLATEOBJ_vCleanup(&exloDst2RGB
);
5834 if (TextObj
!= NULL
)
5835 TEXTOBJ_UnlockText(TextObj
);
5842 EXLATEOBJ_vCleanup(&exloRGB2Dst
);
5843 EXLATEOBJ_vCleanup(&exloDst2RGB
);
5845 if (TextObj
!= NULL
)
5846 TEXTOBJ_UnlockText(TextObj
);
5853 #define STACK_TEXT_BUFFER_SIZE 100
5861 IN OPTIONAL LPRECT UnsafeRect
,
5862 IN LPWSTR UnsafeString
,
5864 IN OPTIONAL LPINT UnsafeDx
,
5865 IN DWORD dwCodePage
)
5867 BOOL Result
= FALSE
;
5868 NTSTATUS Status
= STATUS_SUCCESS
;
5870 BYTE LocalBuffer
[STACK_TEXT_BUFFER_SIZE
];
5871 PVOID Buffer
= LocalBuffer
;
5872 LPCWSTR SafeString
= NULL
;
5873 LPINT SafeDx
= NULL
;
5874 ULONG BufSize
, StringSize
, DxSize
= 0;
5876 /* Check if String is valid */
5877 if ((Count
> 0xFFFF) || (Count
> 0 && UnsafeString
== NULL
))
5879 EngSetLastError(ERROR_INVALID_PARAMETER
);
5885 /* Calculate buffer size for string and Dx values */
5886 BufSize
= StringSize
= Count
* sizeof(WCHAR
);
5889 /* If ETO_PDY is specified, we have pairs of INTs */
5890 DxSize
= (Count
* sizeof(INT
)) * (fuOptions
& ETO_PDY
? 2 : 1);
5894 /* Check if our local buffer is large enough */
5895 if (BufSize
> STACK_TEXT_BUFFER_SIZE
)
5897 /* It's not, allocate a temp buffer */
5898 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufSize
, GDITAG_TEXT
);
5905 /* Probe and copy user mode data to the buffer */
5908 /* Put the Dx before the String to assure alignment of 4 */
5909 SafeString
= (LPCWSTR
)(((ULONG_PTR
)Buffer
) + DxSize
);
5911 /* Probe and copy the string */
5912 ProbeForRead(UnsafeString
, StringSize
, 1);
5913 memcpy((PVOID
)SafeString
, UnsafeString
, StringSize
);
5915 /* If we have Dx values... */
5918 /* ... probe and copy them */
5920 ProbeForRead(UnsafeDx
, DxSize
, 1);
5921 memcpy(SafeDx
, UnsafeDx
, DxSize
);
5924 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
5926 Status
= _SEH2_GetExceptionCode();
5929 if (!NT_SUCCESS(Status
))
5935 /* If we have a rect, copy it */
5940 ProbeForRead(UnsafeRect
, sizeof(RECT
), 1);
5941 SafeRect
= *UnsafeRect
;
5943 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
5945 Status
= _SEH2_GetExceptionCode();
5948 if (!NT_SUCCESS(Status
))
5954 /* Finally call the internal routine */
5955 Result
= GreExtTextOutW(hDC
,
5966 /* If we allocated a buffer, free it */
5967 if (Buffer
!= LocalBuffer
)
5969 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
5981 NtGdiGetCharABCWidthsW(
5985 IN OPTIONAL PWCHAR UnSafepwch
,
5990 LPABCFLOAT SafeBuffF
= NULL
;
5996 FT_CharMap charmap
, found
= NULL
;
5997 UINT i
, glyph_index
, BufferSize
;
5999 NTSTATUS Status
= STATUS_SUCCESS
;
6000 PMATRIX pmxWorldToDevice
;
6001 PWCHAR Safepwch
= NULL
;
6006 EngSetLastError(ERROR_INVALID_PARAMETER
);
6012 UINT pwchSize
= Count
* sizeof(WCHAR
);
6013 Safepwch
= ExAllocatePoolWithTag(PagedPool
, pwchSize
, GDITAG_TEXT
);
6017 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6023 ProbeForRead(UnSafepwch
, pwchSize
, 1);
6024 RtlCopyMemory(Safepwch
, UnSafepwch
, pwchSize
);
6026 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6028 Status
= _SEH2_GetExceptionCode();
6033 if (!NT_SUCCESS(Status
))
6036 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6038 EngSetLastError(Status
);
6042 BufferSize
= Count
* sizeof(ABC
); // Same size!
6043 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6044 if (!fl
) SafeBuffF
= (LPABCFLOAT
) SafeBuff
;
6045 if (SafeBuff
== NULL
)
6049 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6051 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6055 dc
= DC_LockDc(hDC
);
6058 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6061 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6063 EngSetLastError(ERROR_INVALID_HANDLE
);
6066 pdcattr
= dc
->pdcattr
;
6067 hFont
= pdcattr
->hlfntNew
;
6068 TextObj
= RealizeFontInit(hFont
);
6070 /* Get the DC's world-to-device transformation matrix */
6071 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6074 if (TextObj
== NULL
)
6076 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6079 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6081 EngSetLastError(ERROR_INVALID_HANDLE
);
6085 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6087 face
= FontGDI
->SharedFace
->Face
;
6088 if (face
->charmap
== NULL
)
6090 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6092 charmap
= face
->charmaps
[i
];
6093 if (charmap
->encoding
!= 0)
6102 DPRINT1("WARNING: Could not find desired charmap!\n");
6103 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6106 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6108 EngSetLastError(ERROR_INVALID_HANDLE
);
6113 FT_Set_Charmap(face
, found
);
6117 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6119 IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
6120 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6122 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6124 int adv
, lsb
, bbx
, left
, right
;
6128 if (fl
& GCABCW_INDICES
)
6129 glyph_index
= Safepwch
[i
- FirstChar
];
6131 glyph_index
= FT_Get_Char_Index(face
, Safepwch
[i
- FirstChar
]);
6135 if (fl
& GCABCW_INDICES
)
6138 glyph_index
= FT_Get_Char_Index(face
, i
);
6140 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6142 left
= (INT
)face
->glyph
->metrics
.horiBearingX
& -64;
6143 right
= (INT
)((face
->glyph
->metrics
.horiBearingX
+ face
->glyph
->metrics
.width
) + 63) & -64;
6144 adv
= (face
->glyph
->advance
.x
+ 32) >> 6;
6146 // int test = (INT)(face->glyph->metrics.horiAdvance + 63) >> 6;
6147 // DPRINT1("Advance Wine %d and Advance Ros %d\n",test, adv ); /* It's the same! */
6150 bbx
= (right
- left
) >> 6;
6152 DPRINT1("lsb %d and bbx %d\n", lsb, bbx );
6156 SafeBuffF
[i
- FirstChar
].abcfA
= (FLOAT
) lsb
;
6157 SafeBuffF
[i
- FirstChar
].abcfB
= (FLOAT
) bbx
;
6158 SafeBuffF
[i
- FirstChar
].abcfC
= (FLOAT
) (adv
- lsb
- bbx
);
6162 SafeBuff
[i
- FirstChar
].abcA
= lsb
;
6163 SafeBuff
[i
- FirstChar
].abcB
= bbx
;
6164 SafeBuff
[i
- FirstChar
].abcC
= adv
- lsb
- bbx
;
6168 TEXTOBJ_UnlockText(TextObj
);
6169 Status
= MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6171 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6174 ExFreePoolWithTag(Safepwch
, GDITAG_TEXT
);
6176 if (! NT_SUCCESS(Status
))
6178 SetLastNtError(Status
);
6182 DPRINT("NtGdiGetCharABCWidths Worked!\n");
6195 IN OPTIONAL PWCHAR UnSafepwc
,
6199 NTSTATUS Status
= STATUS_SUCCESS
;
6201 PFLOAT SafeBuffF
= NULL
;
6207 FT_CharMap charmap
, found
= NULL
;
6208 UINT i
, glyph_index
, BufferSize
;
6210 PMATRIX pmxWorldToDevice
;
6211 PWCHAR Safepwc
= NULL
;
6216 UINT pwcSize
= Count
* sizeof(WCHAR
);
6217 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6221 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6226 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6227 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6229 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6231 Status
= _SEH2_GetExceptionCode();
6236 if (!NT_SUCCESS(Status
))
6238 EngSetLastError(Status
);
6242 BufferSize
= Count
* sizeof(INT
); // Same size!
6243 SafeBuff
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, GDITAG_TEXT
);
6244 if (!fl
) SafeBuffF
= (PFLOAT
) SafeBuff
;
6245 if (SafeBuff
== NULL
)
6248 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6250 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
6254 dc
= DC_LockDc(hDC
);
6258 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6260 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6261 EngSetLastError(ERROR_INVALID_HANDLE
);
6264 pdcattr
= dc
->pdcattr
;
6265 hFont
= pdcattr
->hlfntNew
;
6266 TextObj
= RealizeFontInit(hFont
);
6267 /* Get the DC's world-to-device transformation matrix */
6268 pmxWorldToDevice
= DC_pmxWorldToDevice(dc
);
6271 if (TextObj
== NULL
)
6274 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6276 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6277 EngSetLastError(ERROR_INVALID_HANDLE
);
6281 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6283 face
= FontGDI
->SharedFace
->Face
;
6284 if (face
->charmap
== NULL
)
6286 for (i
= 0; i
< (UINT
)face
->num_charmaps
; i
++)
6288 charmap
= face
->charmaps
[i
];
6289 if (charmap
->encoding
!= 0)
6298 DPRINT1("WARNING: Could not find desired charmap!\n");
6301 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6303 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6304 EngSetLastError(ERROR_INVALID_HANDLE
);
6309 FT_Set_Charmap(face
, found
);
6313 plf
= &TextObj
->logfont
.elfEnumLogfontEx
.elfLogFont
;
6315 IntRequestFontSize(dc
, face
, plf
->lfWidth
, plf
->lfHeight
);
6316 FtSetCoordinateTransform(face
, pmxWorldToDevice
);
6318 for (i
= FirstChar
; i
< FirstChar
+Count
; i
++)
6322 if (fl
& GCW_INDICES
)
6323 glyph_index
= Safepwc
[i
- FirstChar
];
6325 glyph_index
= FT_Get_Char_Index(face
, Safepwc
[i
- FirstChar
]);
6329 if (fl
& GCW_INDICES
)
6332 glyph_index
= FT_Get_Char_Index(face
, i
);
6334 FT_Load_Glyph(face
, glyph_index
, FT_LOAD_DEFAULT
);
6336 SafeBuffF
[i
- FirstChar
] = (FLOAT
) ((face
->glyph
->advance
.x
+ 32) >> 6);
6338 SafeBuff
[i
- FirstChar
] = (face
->glyph
->advance
.x
+ 32) >> 6;
6341 TEXTOBJ_UnlockText(TextObj
);
6342 MmCopyToCaller(Buffer
, SafeBuff
, BufferSize
);
6345 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6347 ExFreePoolWithTag(SafeBuff
, GDITAG_TEXT
);
6355 // TODO: Move this code into NtGdiGetGlyphIndicesWInternal and wrap
6356 // NtGdiGetGlyphIndicesW around NtGdiGetGlyphIndicesWInternal instead.
6357 // NOTE: See also GreGetGlyphIndicesW.
6362 NtGdiGetGlyphIndicesW(
6364 _In_reads_opt_(cwc
) LPCWSTR pwc
,
6366 _Out_writes_opt_(cwc
) LPWORD pgi
,
6374 NTSTATUS Status
= STATUS_SUCCESS
;
6375 OUTLINETEXTMETRICW
*potm
;
6377 WCHAR DefChar
= 0xffff;
6378 PWSTR Buffer
= NULL
;
6379 ULONG Size
, pwcSize
;
6380 PWSTR Safepwc
= NULL
;
6381 LPCWSTR UnSafepwc
= pwc
;
6382 LPWORD UnSafepgi
= pgi
;
6384 /* Check for integer overflow */
6385 if (cwc
& 0x80000000) // (INT_MAX + 1) == INT_MIN
6388 if (!UnSafepwc
&& !UnSafepgi
)
6391 if (!UnSafepwc
|| !UnSafepgi
)
6393 DPRINT1("UnSafepwc == %p, UnSafepgi = %p\n", UnSafepwc
, UnSafepgi
);
6397 // TODO: Special undocumented case!
6398 if (!pwc
&& !pgi
&& (cwc
== 0))
6400 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (!pwc && !pgi && (cwc == 0)) is UNIMPLEMENTED!\n");
6404 // FIXME: This is a hack!! (triggered by e.g. Word 2010). See CORE-12825
6407 DPRINT1("ERR: NtGdiGetGlyphIndicesW with (cwc == 0) is UNIMPLEMENTED!\n");
6411 dc
= DC_LockDc(hdc
);
6416 pdcattr
= dc
->pdcattr
;
6417 hFont
= pdcattr
->hlfntNew
;
6418 TextObj
= RealizeFontInit(hFont
);
6425 FontGDI
= ObjToGDI(TextObj
->Font
, FONT
);
6426 TEXTOBJ_UnlockText(TextObj
);
6428 Buffer
= ExAllocatePoolWithTag(PagedPool
, cwc
*sizeof(WORD
), GDITAG_TEXT
);
6434 if (iMode
& GGI_MARK_NONEXISTING_GLYPHS
)
6440 FT_Face Face
= FontGDI
->SharedFace
->Face
;
6441 if (FT_IS_SFNT(Face
))
6443 TT_OS2
*pOS2
= FT_Get_Sfnt_Table(Face
, ft_sfnt_os2
);
6444 DefChar
= (pOS2
->usDefaultChar
? FT_Get_Char_Index(Face
, pOS2
->usDefaultChar
) : 0);
6448 Size
= IntGetOutlineTextMetrics(FontGDI
, 0, NULL
);
6449 potm
= ExAllocatePoolWithTag(PagedPool
, Size
, GDITAG_TEXT
);
6455 IntGetOutlineTextMetrics(FontGDI
, Size
, potm
);
6456 DefChar
= potm
->otmTextMetrics
.tmDefaultChar
;
6457 ExFreePoolWithTag(potm
, GDITAG_TEXT
);
6461 pwcSize
= cwc
* sizeof(WCHAR
);
6462 Safepwc
= ExAllocatePoolWithTag(PagedPool
, pwcSize
, GDITAG_TEXT
);
6466 Status
= STATUS_NO_MEMORY
;
6472 ProbeForRead(UnSafepwc
, pwcSize
, 1);
6473 RtlCopyMemory(Safepwc
, UnSafepwc
, pwcSize
);
6475 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6477 Status
= _SEH2_GetExceptionCode();
6481 if (!NT_SUCCESS(Status
)) goto ErrorRet
;
6485 for (i
= 0; i
< cwc
; i
++)
6487 Buffer
[i
] = FT_Get_Char_Index(FontGDI
->SharedFace
->Face
, Safepwc
[i
]);
6490 Buffer
[i
] = DefChar
;
6498 ProbeForWrite(UnSafepgi
, cwc
* sizeof(WORD
), 1);
6499 RtlCopyMemory(UnSafepgi
, Buffer
, cwc
* sizeof(WORD
));
6501 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
6503 Status
= _SEH2_GetExceptionCode();
6508 ExFreePoolWithTag(Buffer
, GDITAG_TEXT
);
6509 if (Safepwc
!= NULL
)
6511 ExFreePoolWithTag(Safepwc
, GDITAG_TEXT
);
6513 if (NT_SUCCESS(Status
)) return cwc
;