6a0a87e0b1350478960b8e3cf9dc166b8fcced39
[reactos.git] / reactos / win32ss / gdi / ntgdi / freetype.c
1 /*
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.
9 */
10
11 /** Includes ******************************************************************/
12
13 #include <win32k.h>
14
15 #include FT_GLYPH_H
16 #include FT_TYPE1_TABLES_H
17 #include FT_TRUETYPE_TABLES_H
18 #include FT_TRUETYPE_TAGS_H
19 #include FT_TRIGONOMETRY_H
20 #include FT_BITMAP_H
21 #include FT_OUTLINE_H
22 #include FT_WINFONTS_H
23 #include FT_SFNT_NAMES_H
24 #include FT_SYNTHESIS_H
25 #include FT_TRUETYPE_IDS_H
26
27 #ifndef FT_INTERNAL_INTERNAL_H
28 #define FT_INTERNAL_INTERNAL_H <freetype/internal/internal.h>
29 #include FT_INTERNAL_INTERNAL_H
30 #endif
31 #include FT_INTERNAL_TRUETYPE_TYPES_H
32
33 #include <gdi/eng/floatobj.h>
34 #include "font.h"
35
36 #define NDEBUG
37 #include <debug.h>
38
39 /* TPMF_FIXED_PITCH is confusing; brain-dead api */
40 #ifndef _TMPF_VARIABLE_PITCH
41 #define _TMPF_VARIABLE_PITCH TMPF_FIXED_PITCH
42 #endif
43
44 extern const MATRIX gmxWorldToDeviceDefault;
45 extern const MATRIX gmxWorldToPageDefault;
46
47 /* HACK!! Fix XFORMOBJ then use 1:16 / 16:1 */
48 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
49
50 FT_Library library;
51 static const WORD gusEnglishUS = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
52
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");
57
58 /* registry */
59 static UNICODE_STRING FontRegPath =
60 RTL_CONSTANT_STRING(L"\\REGISTRY\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
61
62
63 /* The FreeType library is not thread safe, so we have
64 to serialize access to it */
65 static PFAST_MUTEX FreeTypeLock;
66
67 static LIST_ENTRY FontListHead;
68 static PFAST_MUTEX FontListLock;
69 static BOOL RenderingEnabled = TRUE;
70
71 #define IntLockGlobalFonts \
72 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FontListLock)
73
74 #define IntUnLockGlobalFonts \
75 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FontListLock)
76
77 #define ASSERT_GLOBALFONTS_LOCK_HELD() \
78 ASSERT(FontListLock->Owner == KeGetCurrentThread())
79
80 #define IntLockFreeType \
81 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(FreeTypeLock)
82
83 #define IntUnLockFreeType \
84 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(FreeTypeLock)
85
86 #define ASSERT_FREETYPE_LOCK_HELD() \
87 ASSERT(FreeTypeLock->Owner == KeGetCurrentThread())
88
89 #define MAX_FONT_CACHE 256
90
91 static LIST_ENTRY FontCacheListHead;
92 static UINT FontCacheNumEntries;
93
94 static PWCHAR ElfScripts[32] = /* These are in the order of the fsCsb[0] bits */
95 {
96 L"Western", /* 00 */
97 L"Central_European",
98 L"Cyrillic",
99 L"Greek",
100 L"Turkish",
101 L"Hebrew",
102 L"Arabic",
103 L"Baltic",
104 L"Vietnamese", /* 08 */
105 NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 15 */
106 L"Thai",
107 L"Japanese",
108 L"CHINESE_GB2312",
109 L"Hangul",
110 L"CHINESE_BIG5",
111 L"Hangul(Johab)",
112 NULL, NULL, /* 23 */
113 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
114 L"Symbol" /* 31 */
115 };
116
117 /*
118 * For TranslateCharsetInfo
119 */
120 #define CP_SYMBOL 42
121 #define MAXTCIINDEX 32
122 static const CHARSETINFO FontTci[MAXTCIINDEX] =
123 {
124 /* ANSI */
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}} },
142 /* ANSI and OEM */
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}} }
161 };
162
163 /* list head */
164 static RTL_STATIC_LIST_HEAD(FontSubstListHead);
165
166 static void
167 SharedMem_AddRef(PSHARED_MEM Ptr)
168 {
169 ASSERT_FREETYPE_LOCK_HELD();
170
171 ++Ptr->RefCount;
172 }
173
174 static PSHARED_FACE
175 SharedFace_Create(FT_Face Face, PSHARED_MEM Memory)
176 {
177 PSHARED_FACE Ptr;
178 Ptr = ExAllocatePoolWithTag(PagedPool, sizeof(SHARED_FACE), TAG_FONT);
179 if (Ptr)
180 {
181 Ptr->Face = Face;
182 Ptr->RefCount = 1;
183 Ptr->Memory = Memory;
184 SharedMem_AddRef(Memory);
185 DPRINT("Creating SharedFace for %s\n", Face->family_name);
186 }
187 return Ptr;
188 }
189
190 static PSHARED_MEM
191 SharedMem_Create(PBYTE Buffer, ULONG BufferSize, BOOL IsMapping)
192 {
193 PSHARED_MEM Ptr;
194 Ptr = ExAllocatePoolWithTag(PagedPool, sizeof(SHARED_MEM), TAG_FONT);
195 if (Ptr)
196 {
197 Ptr->Buffer = Buffer;
198 Ptr->BufferSize = BufferSize;
199 Ptr->RefCount = 1;
200 Ptr->IsMapping = IsMapping;
201 DPRINT("Creating SharedMem for %p (%i, %p)\n", Buffer, IsMapping, Ptr);
202 }
203 return Ptr;
204 }
205
206 static void
207 SharedFace_AddRef(PSHARED_FACE Ptr)
208 {
209 ASSERT_FREETYPE_LOCK_HELD();
210
211 ++Ptr->RefCount;
212 }
213
214 static void
215 RemoveCachedEntry(PFONT_CACHE_ENTRY Entry)
216 {
217 ASSERT_FREETYPE_LOCK_HELD();
218
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);
224 }
225
226 static void
227 RemoveCacheEntries(FT_Face Face)
228 {
229 PLIST_ENTRY CurrentEntry;
230 PFONT_CACHE_ENTRY FontEntry;
231
232 ASSERT_FREETYPE_LOCK_HELD();
233
234 CurrentEntry = FontCacheListHead.Flink;
235 while (CurrentEntry != &FontCacheListHead)
236 {
237 FontEntry = CONTAINING_RECORD(CurrentEntry, FONT_CACHE_ENTRY, ListEntry);
238 CurrentEntry = CurrentEntry->Flink;
239
240 if (FontEntry->Face == Face)
241 {
242 RemoveCachedEntry(FontEntry);
243 }
244 }
245 }
246
247 static void SharedMem_Release(PSHARED_MEM Ptr)
248 {
249 ASSERT_FREETYPE_LOCK_HELD();
250 ASSERT(Ptr->RefCount > 0);
251
252 if (Ptr->RefCount <= 0)
253 return;
254
255 --Ptr->RefCount;
256 if (Ptr->RefCount == 0)
257 {
258 DPRINT("Releasing SharedMem for %p (%i, %p)\n", Ptr->Buffer, Ptr->IsMapping, Ptr);
259 if (Ptr->IsMapping)
260 MmUnmapViewInSystemSpace(Ptr->Buffer);
261 else
262 ExFreePoolWithTag(Ptr->Buffer, TAG_FONT);
263 ExFreePoolWithTag(Ptr, TAG_FONT);
264 }
265 }
266
267 static void
268 SharedFace_Release(PSHARED_FACE Ptr)
269 {
270 IntLockFreeType;
271 ASSERT(Ptr->RefCount > 0);
272
273 if (Ptr->RefCount <= 0)
274 return;
275
276 --Ptr->RefCount;
277 if (Ptr->RefCount == 0)
278 {
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);
284 }
285 IntUnLockFreeType;
286 }
287
288
289 /*
290 * IntLoadFontSubstList --- loads the list of font substitutes
291 */
292 BOOL FASTCALL
293 IntLoadFontSubstList(PLIST_ENTRY pHead)
294 {
295 NTSTATUS Status;
296 HANDLE KeyHandle;
297 OBJECT_ATTRIBUTES ObjectAttributes;
298 KEY_FULL_INFORMATION KeyFullInfo;
299 ULONG i, Length;
300 UNICODE_STRING FromW, ToW;
301 BYTE InfoBuffer[128];
302 PKEY_VALUE_FULL_INFORMATION pInfo;
303 BYTE CharSets[FONTSUBST_FROM_AND_TO];
304 LPWSTR pch;
305 PFONTSUBST_ENTRY pEntry;
306
307 /* the FontSubstitutes registry key */
308 static UNICODE_STRING FontSubstKey =
309 RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\"
310 L"Microsoft\\Windows NT\\CurrentVersion\\"
311 L"FontSubstitutes");
312
313 /* open registry key */
314 InitializeObjectAttributes(&ObjectAttributes, &FontSubstKey,
315 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
316 NULL, NULL);
317 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
318 if (!NT_SUCCESS(Status))
319 {
320 DPRINT("ZwOpenKey failed: 0x%08X\n", Status);
321 return FALSE; /* failure */
322 }
323
324 /* query count of values */
325 Status = ZwQueryKey(KeyHandle, KeyFullInformation,
326 &KeyFullInfo, sizeof(KeyFullInfo), &Length);
327 if (!NT_SUCCESS(Status))
328 {
329 DPRINT("ZwQueryKey failed: 0x%08X\n", Status);
330 ZwClose(KeyHandle);
331 return FALSE; /* failure */
332 }
333
334 /* for each value */
335 for (i = 0; i < KeyFullInfo.Values; ++i)
336 {
337 /* get value name */
338 Status = ZwEnumerateValueKey(KeyHandle, i, KeyValueFullInformation,
339 InfoBuffer, sizeof(InfoBuffer), &Length);
340 if (!NT_SUCCESS(Status))
341 {
342 DPRINT("ZwEnumerateValueKey failed: 0x%08X\n", Status);
343 break; /* failure */
344 }
345
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))
352 {
353 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status);
354 break; /* failure */
355 }
356
357 /* query value */
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)
362 {
363 DPRINT("ZwQueryValueKey failed: 0x%08X\n", Status);
364 RtlFreeUnicodeString(&FromW);
365 break; /* failure */
366 }
367
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))
374 {
375 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status);
376 RtlFreeUnicodeString(&FromW);
377 break; /* failure */
378 }
379
380 /* does charset exist? (from) */
381 CharSets[FONTSUBST_FROM] = DEFAULT_CHARSET;
382 pch = wcsrchr(FromW.Buffer, L',');
383 if (pch)
384 {
385 /* truncate */
386 *pch = UNICODE_NULL;
387 FromW.Length = (pch - FromW.Buffer) * sizeof(WCHAR);
388 /* parse charset number */
389 CharSets[FONTSUBST_FROM] = (BYTE)_wtoi(pch + 1);
390 }
391
392 /* does charset exist? (to) */
393 CharSets[FONTSUBST_TO] = DEFAULT_CHARSET;
394 pch = wcsrchr(ToW.Buffer, L',');
395 if (pch)
396 {
397 /* truncate */
398 *pch = UNICODE_NULL;
399 ToW.Length = (pch - ToW.Buffer) * sizeof(WCHAR);
400 /* parse charset number */
401 CharSets[FONTSUBST_TO] = (BYTE)_wtoi(pch + 1);
402 }
403
404 /* allocate an entry */
405 pEntry = ExAllocatePoolWithTag(PagedPool, sizeof(FONTSUBST_ENTRY), TAG_FONT);
406 if (pEntry == NULL)
407 {
408 DPRINT("ExAllocatePoolWithTag failed\n");
409 RtlFreeUnicodeString(&FromW);
410 RtlFreeUnicodeString(&ToW);
411 break; /* failure */
412 }
413
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];
419
420 /* insert pEntry to *pHead */
421 InsertTailList(pHead, &pEntry->ListEntry);
422 }
423
424 /* close now */
425 ZwClose(KeyHandle);
426
427 return NT_SUCCESS(Status);
428 }
429
430 BOOL FASTCALL
431 InitFontSupport(VOID)
432 {
433 ULONG ulError;
434
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)
441 {
442 return FALSE;
443 }
444
445 ExInitializeFastMutex(FontListLock);
446 FreeTypeLock = ExAllocatePoolWithTag(NonPagedPool, sizeof(FAST_MUTEX), TAG_INTERNAL_SYNC);
447 if (FreeTypeLock == NULL)
448 {
449 return FALSE;
450 }
451 ExInitializeFastMutex(FreeTypeLock);
452
453 ulError = FT_Init_FreeType(&library);
454 if (ulError)
455 {
456 DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError);
457 return FALSE;
458 }
459
460 IntLoadSystemFonts();
461 IntLoadFontSubstList(&FontSubstListHead);
462
463 return TRUE;
464 }
465
466 VOID
467 FtSetCoordinateTransform(
468 FT_Face face,
469 PMATRIX pmx)
470 {
471 FT_Matrix ftmatrix;
472 FLOATOBJ efTemp;
473
474 /* Create a freetype matrix, by converting to 16.16 fixpoint format */
475 efTemp = pmx->efM11;
476 FLOATOBJ_MulLong(&efTemp, 0x00010000);
477 ftmatrix.xx = FLOATOBJ_GetLong(&efTemp);
478
479 efTemp = pmx->efM12;
480 FLOATOBJ_MulLong(&efTemp, 0x00010000);
481 ftmatrix.xy = FLOATOBJ_GetLong(&efTemp);
482
483 efTemp = pmx->efM21;
484 FLOATOBJ_MulLong(&efTemp, 0x00010000);
485 ftmatrix.yx = FLOATOBJ_GetLong(&efTemp);
486
487 efTemp = pmx->efM22;
488 FLOATOBJ_MulLong(&efTemp, 0x00010000);
489 ftmatrix.yy = FLOATOBJ_GetLong(&efTemp);
490
491 /* Set the transformation matrix */
492 FT_Set_Transform(face, &ftmatrix, 0);
493 }
494
495 static BOOL
496 SubstituteFontByList(PLIST_ENTRY pHead,
497 PUNICODE_STRING pOutputName,
498 PUNICODE_STRING pInputName,
499 BYTE RequestedCharSet,
500 BYTE CharSetMap[FONTSUBST_FROM_AND_TO])
501 {
502 NTSTATUS Status;
503 PLIST_ENTRY pListEntry;
504 PFONTSUBST_ENTRY pSubstEntry;
505 BYTE CharSets[FONTSUBST_FROM_AND_TO];
506
507 CharSetMap[FONTSUBST_FROM] = DEFAULT_CHARSET;
508 CharSetMap[FONTSUBST_TO] = RequestedCharSet;
509
510 /* for each list entry */
511 for (pListEntry = pHead->Flink;
512 pListEntry != pHead;
513 pListEntry = pListEntry->Flink)
514 {
515 pSubstEntry =
516 (PFONTSUBST_ENTRY)CONTAINING_RECORD(pListEntry, FONT_ENTRY, ListEntry);
517
518 CharSets[FONTSUBST_FROM] = pSubstEntry->CharSets[FONTSUBST_FROM];
519
520 if (CharSets[FONTSUBST_FROM] != DEFAULT_CHARSET &&
521 CharSets[FONTSUBST_FROM] != RequestedCharSet)
522 {
523 continue; /* not matched */
524 }
525
526 /* does charset number exist? (to) */
527 if (pSubstEntry->CharSets[FONTSUBST_TO] != DEFAULT_CHARSET)
528 {
529 CharSets[FONTSUBST_TO] = pSubstEntry->CharSets[FONTSUBST_TO];
530 }
531 else
532 {
533 CharSets[FONTSUBST_TO] = RequestedCharSet;
534 }
535
536 /* does font name match? */
537 if (!RtlEqualUnicodeString(&pSubstEntry->FontNames[FONTSUBST_FROM],
538 pInputName, TRUE))
539 {
540 continue; /* not matched */
541 }
542
543 /* update *pOutputName */
544 RtlFreeUnicodeString(pOutputName);
545 Status = RtlCreateUnicodeString(pOutputName,
546 pSubstEntry->FontNames[FONTSUBST_TO].Buffer);
547 if (!NT_SUCCESS(Status))
548 {
549 DPRINT("RtlCreateUnicodeString failed: 0x%08X\n", Status);
550 continue; /* cannot create string */
551 }
552
553 if (CharSetMap[FONTSUBST_FROM] == DEFAULT_CHARSET)
554 {
555 /* update CharSetMap */
556 CharSetMap[FONTSUBST_FROM] = CharSets[FONTSUBST_FROM];
557 CharSetMap[FONTSUBST_TO] = CharSets[FONTSUBST_TO];
558 }
559 return TRUE; /* success */
560 }
561
562 return FALSE;
563 }
564
565 static BOOL
566 SubstituteFontRecurse(PUNICODE_STRING pInOutName, BYTE *pRequestedCharSet)
567 {
568 UINT RecurseCount = 5;
569 UNICODE_STRING OutputNameW = { 0 };
570 BYTE CharSetMap[FONTSUBST_FROM_AND_TO];
571 BOOL Found;
572
573 if (pInOutName->Buffer[0] == UNICODE_NULL)
574 return FALSE;
575
576 while (RecurseCount-- > 0)
577 {
578 RtlInitUnicodeString(&OutputNameW, NULL);
579 Found = SubstituteFontByList(&FontSubstListHead,
580 &OutputNameW, pInOutName,
581 *pRequestedCharSet, CharSetMap);
582 if (!Found)
583 break;
584
585 /* update *pInOutName and *pRequestedCharSet */
586 RtlFreeUnicodeString(pInOutName);
587 *pInOutName = OutputNameW;
588 if (CharSetMap[FONTSUBST_FROM] == DEFAULT_CHARSET ||
589 CharSetMap[FONTSUBST_FROM] == *pRequestedCharSet)
590 {
591 *pRequestedCharSet = CharSetMap[FONTSUBST_TO];
592 }
593 }
594
595 return TRUE; /* success */
596 }
597
598 /*
599 * IntLoadSystemFonts
600 *
601 * Search the system font directory and adds each font found.
602 */
603 VOID FASTCALL
604 IntLoadSystemFonts(VOID)
605 {
606 OBJECT_ATTRIBUTES ObjectAttributes;
607 UNICODE_STRING Directory, FileName, TempString;
608 IO_STATUS_BLOCK Iosb;
609 HANDLE hDirectory;
610 BYTE *DirInfoBuffer;
611 PFILE_DIRECTORY_INFORMATION DirInfo;
612 BOOLEAN bRestartScan = TRUE;
613 NTSTATUS Status;
614 INT i;
615 static UNICODE_STRING SearchPatterns[] =
616 {
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")
623 };
624
625 RtlInitUnicodeString(&Directory, L"\\SystemRoot\\Fonts\\");
626
627 InitializeObjectAttributes(
628 &ObjectAttributes,
629 &Directory,
630 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
631 NULL,
632 NULL);
633
634 Status = ZwOpenFile(
635 &hDirectory,
636 SYNCHRONIZE | FILE_LIST_DIRECTORY,
637 &ObjectAttributes,
638 &Iosb,
639 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
640 FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
641
642 if (NT_SUCCESS(Status))
643 {
644 for (i = 0; i < _countof(SearchPatterns); ++i)
645 {
646 DirInfoBuffer = ExAllocatePoolWithTag(PagedPool, 0x4000, TAG_FONT);
647 if (DirInfoBuffer == NULL)
648 {
649 ZwClose(hDirectory);
650 return;
651 }
652
653 FileName.Buffer = ExAllocatePoolWithTag(PagedPool, MAX_PATH * sizeof(WCHAR), TAG_FONT);
654 if (FileName.Buffer == NULL)
655 {
656 ExFreePoolWithTag(DirInfoBuffer, TAG_FONT);
657 ZwClose(hDirectory);
658 return;
659 }
660 FileName.Length = 0;
661 FileName.MaximumLength = MAX_PATH * sizeof(WCHAR);
662
663 while (1)
664 {
665 Status = ZwQueryDirectoryFile(
666 hDirectory,
667 NULL,
668 NULL,
669 NULL,
670 &Iosb,
671 DirInfoBuffer,
672 0x4000,
673 FileDirectoryInformation,
674 FALSE,
675 &SearchPatterns[i],
676 bRestartScan);
677
678 if (!NT_SUCCESS(Status) || Status == STATUS_NO_MORE_FILES)
679 {
680 break;
681 }
682
683 DirInfo = (PFILE_DIRECTORY_INFORMATION)DirInfoBuffer;
684 while (1)
685 {
686 TempString.Buffer = DirInfo->FileName;
687 TempString.Length =
688 TempString.MaximumLength = DirInfo->FileNameLength;
689 RtlCopyUnicodeString(&FileName, &Directory);
690 RtlAppendUnicodeStringToString(&FileName, &TempString);
691 IntGdiAddFontResource(&FileName, 0);
692 if (DirInfo->NextEntryOffset == 0)
693 break;
694 DirInfo = (PFILE_DIRECTORY_INFORMATION)((ULONG_PTR)DirInfo + DirInfo->NextEntryOffset);
695 }
696
697 bRestartScan = FALSE;
698 }
699
700 ExFreePoolWithTag(FileName.Buffer, TAG_FONT);
701 ExFreePoolWithTag(DirInfoBuffer, TAG_FONT);
702 }
703 ZwClose(hDirectory);
704 }
705 }
706
707 static BYTE
708 ItalicFromStyle(const char *style_name)
709 {
710 if (style_name == NULL || style_name[0] == 0)
711 return FALSE;
712 if (strstr(style_name, "Italic") != NULL)
713 return TRUE;
714 if (strstr(style_name, "Oblique") != NULL)
715 return TRUE;
716 return FALSE;
717 }
718
719 static LONG
720 WeightFromStyle(const char *style_name)
721 {
722 if (style_name == NULL || style_name[0] == 0)
723 return FW_NORMAL;
724 if (strstr(style_name, "Regular") != NULL)
725 return FW_REGULAR;
726 if (strstr(style_name, "Normal") != NULL)
727 return FW_NORMAL;
728 if (strstr(style_name, "SemiBold") != NULL)
729 return FW_SEMIBOLD;
730 if (strstr(style_name, "UltraBold") != NULL)
731 return FW_ULTRABOLD;
732 if (strstr(style_name, "DemiBold") != NULL)
733 return FW_DEMIBOLD;
734 if (strstr(style_name, "ExtraBold") != NULL)
735 return FW_EXTRABOLD;
736 if (strstr(style_name, "Bold") != NULL)
737 return FW_BOLD;
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)
743 return FW_LIGHT;
744 if (strstr(style_name, "Hairline") != NULL)
745 return 50;
746 if (strstr(style_name, "Book") != NULL)
747 return 350;
748 if (strstr(style_name, "ExtraBlack") != NULL)
749 return 950;
750 if (strstr(style_name, "UltraBlack") != NULL)
751 return 1000;
752 if (strstr(style_name, "Black") != NULL)
753 return FW_BLACK;
754 if (strstr(style_name, "Medium") != NULL)
755 return FW_MEDIUM;
756 if (strstr(style_name, "Thin") != NULL)
757 return FW_THIN;
758 if (strstr(style_name, "Heavy") != NULL)
759 return FW_HEAVY;
760 return FW_NORMAL;
761 }
762
763 static INT FASTCALL
764 IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont,
765 PSHARED_FACE SharedFace, FT_Long FontIndex, INT CharSetIndex)
766 {
767 FT_Error Error;
768 PFONT_ENTRY Entry;
769 FONT_ENTRY_MEM* PrivateEntry = NULL;
770 FONTGDI * FontGDI;
771 NTSTATUS Status;
772 FT_Face Face;
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;
779 TT_OS2 * pOS2;
780 INT BitIndex;
781 FT_UShort os2_version;
782 FT_ULong os2_ulCodePageRange1;
783 FT_UShort os2_usWeightClass;
784
785 if (SharedFace == NULL && CharSetIndex == -1)
786 {
787 /* load a face from memory */
788 IntLockFreeType;
789 Error = FT_New_Memory_Face(
790 library,
791 pLoadFont->Memory->Buffer,
792 pLoadFont->Memory->BufferSize,
793 ((FontIndex != -1) ? FontIndex : 0),
794 &Face);
795
796 if (!Error)
797 SharedFace = SharedFace_Create(Face, pLoadFont->Memory);
798
799 IntUnLockFreeType;
800
801 if (FT_IS_SFNT(Face))
802 pLoadFont->IsTrueType = TRUE;
803
804 if (Error || SharedFace == NULL)
805 {
806 if (SharedFace)
807 SharedFace_Release(SharedFace);
808
809 if (Error == FT_Err_Unknown_File_Format)
810 DPRINT1("Unknown font file format\n");
811 else
812 DPRINT1("Error reading font (error code: %d)\n", Error);
813 return 0; /* failure */
814 }
815 }
816 else
817 {
818 Face = SharedFace->Face;
819 IntLockFreeType;
820 SharedFace_AddRef(SharedFace);
821 IntUnLockFreeType;
822 }
823
824 /* allocate a FONT_ENTRY */
825 Entry = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_ENTRY), TAG_FONT);
826 if (!Entry)
827 {
828 SharedFace_Release(SharedFace);
829 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
830 return 0; /* failure */
831 }
832
833 /* allocate a FONTGDI */
834 FontGDI = EngAllocMem(FL_ZERO_MEMORY, sizeof(FONTGDI), GDITAG_RFONT);
835 if (!FontGDI)
836 {
837 SharedFace_Release(SharedFace);
838 ExFreePoolWithTag(Entry, TAG_FONT);
839 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
840 return 0; /* failure */
841 }
842
843 /* set file name */
844 if (pFileName)
845 {
846 FontGDI->Filename = ExAllocatePoolWithTag(PagedPool,
847 pFileName->Length + sizeof(UNICODE_NULL),
848 GDITAG_PFF);
849 if (FontGDI->Filename == NULL)
850 {
851 EngFreeMem(FontGDI);
852 SharedFace_Release(SharedFace);
853 ExFreePoolWithTag(Entry, TAG_FONT);
854 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
855 return 0; /* failure */
856 }
857 RtlCopyMemory(FontGDI->Filename, pFileName->Buffer, pFileName->Length);
858 FontGDI->Filename[pFileName->Length / sizeof(WCHAR)] = UNICODE_NULL;
859 }
860 else
861 {
862 FontGDI->Filename = NULL;
863
864 PrivateEntry = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_ENTRY_MEM), TAG_FONT);
865 if (!PrivateEntry)
866 {
867 if (FontGDI->Filename)
868 ExFreePoolWithTag(FontGDI->Filename, GDITAG_PFF);
869 EngFreeMem(FontGDI);
870 SharedFace_Release(SharedFace);
871 ExFreePoolWithTag(Entry, TAG_FONT);
872 return 0;
873 }
874
875 PrivateEntry->Entry = Entry;
876 if (pLoadFont->PrivateEntry)
877 {
878 InsertTailList(&pLoadFont->PrivateEntry->ListEntry, &PrivateEntry->ListEntry);
879 }
880 else
881 {
882 InitializeListHead(&PrivateEntry->ListEntry);
883 pLoadFont->PrivateEntry = PrivateEntry;
884 }
885 }
886
887 /* set face */
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;
894
895 RtlInitAnsiString(&AnsiFaceName, Face->family_name);
896 Status = RtlAnsiStringToUnicodeString(&Entry->FaceName, &AnsiFaceName, TRUE);
897 if (!NT_SUCCESS(Status))
898 {
899 if (PrivateEntry)
900 {
901 if (pLoadFont->PrivateEntry == PrivateEntry)
902 {
903 pLoadFont->PrivateEntry = NULL;
904 }
905 else
906 {
907 RemoveEntryList(&PrivateEntry->ListEntry);
908 }
909 ExFreePoolWithTag(PrivateEntry, TAG_FONT);
910 }
911 if (FontGDI->Filename)
912 ExFreePoolWithTag(FontGDI->Filename, GDITAG_PFF);
913 EngFreeMem(FontGDI);
914 SharedFace_Release(SharedFace);
915 ExFreePoolWithTag(Entry, TAG_FONT);
916 return 0;
917 }
918
919 os2_version = 0;
920 IntLockFreeType;
921 pOS2 = (TT_OS2 *)FT_Get_Sfnt_Table(Face, FT_SFNT_OS2);
922 if (pOS2)
923 {
924 os2_version = pOS2->version;
925 os2_ulCodePageRange1 = pOS2->ulCodePageRange1;
926 os2_usWeightClass = pOS2->usWeightClass;
927 }
928 IntUnLockFreeType;
929
930 if (pOS2 && os2_version >= 1)
931 {
932 /* get charset and weight from OS/2 header */
933
934 /* Make sure we do not use this pointer anymore */
935 pOS2 = NULL;
936
937 for (BitIndex = 0; BitIndex < MAXTCIINDEX; ++BitIndex)
938 {
939 if (os2_ulCodePageRange1 & (1 << BitIndex))
940 {
941 if (FontTci[BitIndex].ciCharset == DEFAULT_CHARSET)
942 continue;
943
944 if ((CharSetIndex == -1 && CharSetCount == 0) ||
945 CharSetIndex == CharSetCount)
946 {
947 FontGDI->CharSet = FontTci[BitIndex].ciCharset;
948 }
949
950 ++CharSetCount;
951 }
952 }
953
954 /* set actual weight */
955 FontGDI->OriginalWeight = os2_usWeightClass;
956 }
957 else
958 {
959 /* get charset from WinFNT header */
960 IntLockFreeType;
961 Error = FT_Get_WinFNT_Header(Face, &WinFNT);
962 if (!Error)
963 {
964 FontGDI->CharSet = WinFNT.charset;
965 }
966 IntUnLockFreeType;
967 }
968
969 /* FIXME: CharSet is invalid on Marlett */
970 if (RtlEqualUnicodeString(&Entry->FaceName, &MarlettW, TRUE))
971 {
972 FontGDI->CharSet = SYMBOL_CHARSET;
973 }
974
975 ++FaceCount;
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);
979
980 /* Add this font resource to the font table */
981 Entry->Font = FontGDI;
982 Entry->NotEnum = (Characteristics & FR_NOT_ENUM);
983
984 if (Characteristics & FR_PRIVATE)
985 {
986 /* private font */
987 PPROCESSINFO Win32Process = PsGetCurrentProcessWin32Process();
988 IntLockProcessPrivateFonts(Win32Process);
989 InsertTailList(&Win32Process->PrivateFontListHead, &Entry->ListEntry);
990 IntUnLockProcessPrivateFonts(Win32Process);
991 }
992 else
993 {
994 /* global font */
995 IntLockGlobalFonts;
996 InsertTailList(&FontListHead, &Entry->ListEntry);
997 IntUnLockGlobalFonts;
998 }
999
1000 if (FontIndex == -1)
1001 {
1002 if (FT_IS_SFNT(Face))
1003 {
1004 TT_Face TrueType = (TT_Face)Face;
1005 if (TrueType->ttc_header.count > 1)
1006 {
1007 FT_Long i;
1008 for (i = 1; i < TrueType->ttc_header.count; ++i)
1009 {
1010 FaceCount += IntGdiLoadFontsFromMemory(pLoadFont, NULL, i, -1);
1011 }
1012 }
1013 }
1014 FontIndex = 0;
1015 }
1016
1017 if (CharSetIndex == -1)
1018 {
1019 INT i;
1020
1021 if (pLoadFont->RegValueName.Length == 0)
1022 {
1023 RtlCreateUnicodeString(pValueName, Entry->FaceName.Buffer);
1024 }
1025 else
1026 {
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,
1033 TAG_USTR);
1034 NewString.Buffer[0] = UNICODE_NULL;
1035
1036 RtlAppendUnicodeStringToString(&NewString, pValueName);
1037 RtlAppendUnicodeToString(&NewString, L" & ");
1038 RtlAppendUnicodeStringToString(&NewString, &Entry->FaceName);
1039
1040 RtlFreeUnicodeString(pValueName);
1041 *pValueName = NewString;
1042 }
1043
1044 for (i = 1; i < CharSetCount; ++i)
1045 {
1046 /* Do not count charsets towards 'faces' loaded */
1047 IntGdiLoadFontsFromMemory(pLoadFont, SharedFace, FontIndex, i);
1048 }
1049 }
1050
1051 return FaceCount; /* number of loaded faces */
1052 }
1053
1054 /*
1055 * IntGdiAddFontResource
1056 *
1057 * Adds the font resource from the specified file to the system.
1058 */
1059
1060 INT FASTCALL
1061 IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
1062 {
1063 NTSTATUS Status;
1064 HANDLE FileHandle;
1065 PVOID Buffer = NULL;
1066 IO_STATUS_BLOCK Iosb;
1067 PVOID SectionObject;
1068 ULONG ViewSize = 0;
1069 LARGE_INTEGER SectionSize;
1070 OBJECT_ATTRIBUTES ObjectAttributes;
1071 GDI_LOAD_FONT LoadFont;
1072 INT FontCount;
1073 HANDLE KeyHandle;
1074 static const UNICODE_STRING TrueTypePostfix = RTL_CONSTANT_STRING(L" (TrueType)");
1075
1076 /* Open the font file */
1077 InitializeObjectAttributes(&ObjectAttributes, FileName, 0, NULL, NULL);
1078 Status = ZwOpenFile(
1079 &FileHandle,
1080 FILE_GENERIC_READ | SYNCHRONIZE,
1081 &ObjectAttributes,
1082 &Iosb,
1083 FILE_SHARE_READ,
1084 FILE_SYNCHRONOUS_IO_NONALERT);
1085 if (!NT_SUCCESS(Status))
1086 {
1087 DPRINT("Could not load font file: %wZ\n", FileName);
1088 return 0;
1089 }
1090
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))
1096 {
1097 DPRINT("Could not map file: %wZ\n", FileName);
1098 ZwClose(FileHandle);
1099 return 0;
1100 }
1101 ZwClose(FileHandle);
1102
1103 Status = MmMapViewInSystemSpace(SectionObject, &Buffer, &ViewSize);
1104 if (!NT_SUCCESS(Status))
1105 {
1106 DPRINT("Could not map file: %wZ\n", FileName);
1107 ObDereferenceObject(SectionObject);
1108 return 0;
1109 }
1110
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);
1118
1119 ObDereferenceObject(SectionObject);
1120
1121 /* Release our copy */
1122 IntLockFreeType;
1123 SharedMem_Release(LoadFont.Memory);
1124 IntUnLockFreeType;
1125
1126 if (FontCount > 0)
1127 {
1128 if (LoadFont.IsTrueType)
1129 {
1130 /* append " (TrueType)" */
1131 UNICODE_STRING NewString;
1132 USHORT Length;
1133
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,
1139 TAG_USTR);
1140 NewString.Buffer[0] = UNICODE_NULL;
1141
1142 RtlAppendUnicodeStringToString(&NewString, &LoadFont.RegValueName);
1143 RtlAppendUnicodeStringToString(&NewString, &TrueTypePostfix);
1144 RtlFreeUnicodeString(&LoadFont.RegValueName);
1145 LoadFont.RegValueName = NewString;
1146 }
1147
1148 /* registry */
1149 InitializeObjectAttributes(&ObjectAttributes, &FontRegPath,
1150 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
1151 NULL, NULL);
1152 Status = ZwOpenKey(&KeyHandle, KEY_WRITE, &ObjectAttributes);
1153 if (NT_SUCCESS(Status))
1154 {
1155 ULONG DataSize;
1156 LPWSTR pFileName = wcsrchr(FileName->Buffer, L'\\');
1157 if (pFileName)
1158 {
1159 pFileName++;
1160 DataSize = (wcslen(pFileName) + 1) * sizeof(WCHAR);
1161 ZwSetValueKey(KeyHandle, &LoadFont.RegValueName, 0, REG_SZ,
1162 pFileName, DataSize);
1163 }
1164 ZwClose(KeyHandle);
1165 }
1166 }
1167 RtlFreeUnicodeString(&LoadFont.RegValueName);
1168
1169 return FontCount;
1170 }
1171
1172 HANDLE FASTCALL
1173 IntGdiAddFontMemResource(PVOID Buffer, DWORD dwSize, PDWORD pNumAdded)
1174 {
1175 GDI_LOAD_FONT LoadFont;
1176 FONT_ENTRY_COLL_MEM* EntryCollection;
1177 INT FaceCount;
1178 HANDLE Ret = 0;
1179
1180 PVOID BufferCopy = ExAllocatePoolWithTag(PagedPool, dwSize, TAG_FONT);
1181
1182 if (!BufferCopy)
1183 {
1184 *pNumAdded = 0;
1185 return NULL;
1186 }
1187 memcpy(BufferCopy, Buffer, dwSize);
1188
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);
1196
1197 RtlFreeUnicodeString(&LoadFont.RegValueName);
1198
1199 /* Release our copy */
1200 IntLockFreeType;
1201 SharedMem_Release(LoadFont.Memory);
1202 IntUnLockFreeType;
1203
1204 if (FaceCount > 0)
1205 {
1206 EntryCollection = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_ENTRY_COLL_MEM), TAG_FONT);
1207 if (EntryCollection)
1208 {
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;
1216 }
1217 }
1218 *pNumAdded = FaceCount;
1219
1220 return Ret;
1221 }
1222
1223 // FIXME: Add RemoveFontResource
1224
1225 static VOID FASTCALL
1226 CleanupFontEntry(PFONT_ENTRY FontEntry)
1227 {
1228 PFONTGDI FontGDI = FontEntry->Font;
1229 PSHARED_FACE SharedFace = FontGDI->SharedFace;
1230
1231 if (FontGDI->Filename)
1232 ExFreePoolWithTag(FontGDI->Filename, GDITAG_PFF);
1233
1234 EngFreeMem(FontGDI);
1235 SharedFace_Release(SharedFace);
1236 ExFreePoolWithTag(FontEntry, TAG_FONT);
1237 }
1238
1239 VOID FASTCALL
1240 IntGdiCleanupMemEntry(PFONT_ENTRY_MEM Head)
1241 {
1242 PLIST_ENTRY Entry;
1243 PFONT_ENTRY_MEM FontEntry;
1244
1245 while (!IsListEmpty(&Head->ListEntry))
1246 {
1247 Entry = RemoveHeadList(&Head->ListEntry);
1248 FontEntry = CONTAINING_RECORD(Entry, FONT_ENTRY_MEM, ListEntry);
1249
1250 CleanupFontEntry(FontEntry->Entry);
1251 ExFreePoolWithTag(FontEntry, TAG_FONT);
1252 }
1253
1254 CleanupFontEntry(Head->Entry);
1255 ExFreePoolWithTag(Head, TAG_FONT);
1256 }
1257
1258 static VOID FASTCALL
1259 UnlinkFontMemCollection(PFONT_ENTRY_COLL_MEM Collection)
1260 {
1261 PFONT_ENTRY_MEM FontMemEntry = Collection->Entry;
1262 PLIST_ENTRY ListEntry;
1263 RemoveEntryList(&Collection->ListEntry);
1264
1265 do {
1266 /* Also unlink the FONT_ENTRY stuff from the PrivateFontListHead */
1267 RemoveEntryList(&FontMemEntry->Entry->ListEntry);
1268
1269 ListEntry = FontMemEntry->ListEntry.Flink;
1270 FontMemEntry = CONTAINING_RECORD(ListEntry, FONT_ENTRY_MEM, ListEntry);
1271
1272 } while (FontMemEntry != Collection->Entry);
1273 }
1274
1275 BOOL FASTCALL
1276 IntGdiRemoveFontMemResource(HANDLE hMMFont)
1277 {
1278 PLIST_ENTRY Entry;
1279 PFONT_ENTRY_COLL_MEM CurrentEntry;
1280 PFONT_ENTRY_COLL_MEM EntryCollection = NULL;
1281 PPROCESSINFO Win32Process = PsGetCurrentProcessWin32Process();
1282
1283 IntLockProcessPrivateFonts(Win32Process);
1284 Entry = Win32Process->PrivateMemFontListHead.Flink;
1285 while (Entry != &Win32Process->PrivateMemFontListHead)
1286 {
1287 CurrentEntry = CONTAINING_RECORD(Entry, FONT_ENTRY_COLL_MEM, ListEntry);
1288
1289 if (CurrentEntry->Handle == (UINT)hMMFont)
1290 {
1291 EntryCollection = CurrentEntry;
1292 UnlinkFontMemCollection(CurrentEntry);
1293 break;
1294 }
1295
1296 Entry = Entry->Flink;
1297 }
1298 IntUnLockProcessPrivateFonts(Win32Process);
1299
1300 if (EntryCollection)
1301 {
1302 IntGdiCleanupMemEntry(EntryCollection->Entry);
1303 ExFreePoolWithTag(EntryCollection, TAG_FONT);
1304 return TRUE;
1305 }
1306 return FALSE;
1307 }
1308
1309
1310 VOID FASTCALL
1311 IntGdiCleanupPrivateFontsForProcess(VOID)
1312 {
1313 PPROCESSINFO Win32Process = PsGetCurrentProcessWin32Process();
1314 PLIST_ENTRY Entry;
1315 PFONT_ENTRY_COLL_MEM EntryCollection;
1316
1317 DPRINT("IntGdiCleanupPrivateFontsForProcess()\n");
1318 do {
1319 Entry = NULL;
1320 EntryCollection = NULL;
1321
1322 IntLockProcessPrivateFonts(Win32Process);
1323 if (!IsListEmpty(&Win32Process->PrivateMemFontListHead))
1324 {
1325 Entry = Win32Process->PrivateMemFontListHead.Flink;
1326 EntryCollection = CONTAINING_RECORD(Entry, FONT_ENTRY_COLL_MEM, ListEntry);
1327 UnlinkFontMemCollection(EntryCollection);
1328 }
1329 IntUnLockProcessPrivateFonts(Win32Process);
1330
1331 if (EntryCollection)
1332 {
1333 IntGdiCleanupMemEntry(EntryCollection->Entry);
1334 ExFreePoolWithTag(EntryCollection, TAG_FONT);
1335 }
1336 else
1337 {
1338 /* No Mem fonts anymore, see if we have any other private fonts left */
1339 Entry = NULL;
1340 IntLockProcessPrivateFonts(Win32Process);
1341 if (!IsListEmpty(&Win32Process->PrivateFontListHead))
1342 {
1343 Entry = RemoveHeadList(&Win32Process->PrivateFontListHead);
1344 }
1345 IntUnLockProcessPrivateFonts(Win32Process);
1346
1347 if (Entry)
1348 {
1349 CleanupFontEntry(CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry));
1350 }
1351 }
1352
1353 } while (Entry);
1354 }
1355
1356 BOOL FASTCALL
1357 IntIsFontRenderingEnabled(VOID)
1358 {
1359 BOOL Ret = RenderingEnabled;
1360 HDC hDC;
1361
1362 hDC = IntGetScreenDC();
1363 if (hDC)
1364 Ret = (NtGdiGetDeviceCaps(hDC, BITSPIXEL) > 8) && RenderingEnabled;
1365
1366 return Ret;
1367 }
1368
1369 VOID FASTCALL
1370 IntEnableFontRendering(BOOL Enable)
1371 {
1372 RenderingEnabled = Enable;
1373 }
1374
1375 FT_Render_Mode FASTCALL
1376 IntGetFontRenderMode(LOGFONTW *logfont)
1377 {
1378 switch (logfont->lfQuality)
1379 {
1380 case ANTIALIASED_QUALITY:
1381 case NONANTIALIASED_QUALITY:
1382 return FT_RENDER_MODE_MONO;
1383 case DRAFT_QUALITY:
1384 return FT_RENDER_MODE_LIGHT;
1385 /* case CLEARTYPE_QUALITY:
1386 return FT_RENDER_MODE_LCD; */
1387 }
1388 return FT_RENDER_MODE_NORMAL;
1389 }
1390
1391
1392 NTSTATUS FASTCALL
1393 TextIntCreateFontIndirect(CONST LPLOGFONTW lf, HFONT *NewFont)
1394 {
1395 PLFONT plfont;
1396 LOGFONTW *plf;
1397
1398 plfont = LFONT_AllocFontWithHandle();
1399 if (!plfont)
1400 {
1401 return STATUS_NO_MEMORY;
1402 }
1403
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)
1409 {
1410 /* This should really depend on whether GM_ADVANCED is set */
1411 plf->lfOrientation = plf->lfEscapement;
1412 }
1413 LFONT_UnlockFont(plfont);
1414
1415 return STATUS_SUCCESS;
1416 }
1417
1418 /*************************************************************************
1419 * TranslateCharsetInfo
1420 *
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.
1425 *
1426 * Only one codepage will be set in Cs->fs. If TCI_SRCFONTSIG is used,
1427 * only one codepage should be set in *Src.
1428 *
1429 * RETURNS
1430 * TRUE on success, FALSE on failure.
1431 *
1432 */
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 */)
1440 {
1441 int Index = 0;
1442
1443 switch (Flags)
1444 {
1445 case TCI_SRCFONTSIG:
1446 while (Index < MAXTCIINDEX && 0 == (*Src >> Index & 0x0001))
1447 {
1448 Index++;
1449 }
1450 break;
1451 case TCI_SRCCODEPAGE:
1452 while (Index < MAXTCIINDEX && *Src != FontTci[Index].ciACP)
1453 {
1454 Index++;
1455 }
1456 break;
1457 case TCI_SRCCHARSET:
1458 while (Index < MAXTCIINDEX && *Src != FontTci[Index].ciCharset)
1459 {
1460 Index++;
1461 }
1462 break;
1463 default:
1464 return FALSE;
1465 }
1466
1467 if (Index >= MAXTCIINDEX || DEFAULT_CHARSET == FontTci[Index].ciCharset)
1468 {
1469 return FALSE;
1470 }
1471
1472 RtlCopyMemory(Cs, &FontTci[Index], sizeof(CHARSETINFO));
1473
1474 return TRUE;
1475 }
1476
1477
1478 static BOOL face_has_symbol_charmap(FT_Face ft_face)
1479 {
1480 int i;
1481
1482 for(i = 0; i < ft_face->num_charmaps; i++)
1483 {
1484 if(ft_face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL)
1485 return TRUE;
1486 }
1487 return FALSE;
1488 }
1489
1490
1491 static void FASTCALL
1492 FillTMEx(TEXTMETRICW *TM, PFONTGDI FontGDI,
1493 TT_OS2 *pOS2, TT_HoriHeader *pHori,
1494 FT_WinFNT_HeaderRec *pFNT, BOOL RealFont)
1495 {
1496 FT_Fixed XScale, YScale;
1497 int Ascent, Descent;
1498 FT_Face Face = FontGDI->SharedFace->Face;
1499
1500 XScale = Face->size->metrics.x_scale;
1501 YScale = Face->size->metrics.y_scale;
1502
1503 if (pFNT)
1504 {
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;
1512 TM->tmOverhang = 0;
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;
1520 if (RealFont)
1521 {
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;
1527 }
1528 else
1529 {
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;
1535 }
1536 return;
1537 }
1538
1539 if (pOS2->usWinAscent + pOS2->usWinDescent == 0)
1540 {
1541 Ascent = pHori->Ascender;
1542 Descent = -pHori->Descender;
1543 }
1544 else
1545 {
1546 Ascent = pOS2->usWinAscent;
1547 Descent = pOS2->usWinDescent;
1548 }
1549
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 */
1556 #endif
1557 TM->tmInternalLeading = (FT_MulFix(Ascent + Descent - Face->units_per_EM, YScale) + 32) >> 6;
1558
1559 TM->tmHeight = TM->tmAscent + TM->tmDescent;
1560
1561 /* MSDN says:
1562 * el = MAX(0, LineGap - ((WinAscent + WinDescent) - (Ascender - Descender)))
1563 */
1564 TM->tmExternalLeading = max(0, (FT_MulFix(pHori->Line_Gap
1565 - ((Ascent + Descent)
1566 - (pHori->Ascender - pHori->Descender)),
1567 YScale) + 32) >> 6);
1568
1569 TM->tmAveCharWidth = (FT_MulFix(pOS2->xAvgCharWidth, XScale) + 32) >> 6;
1570 if (TM->tmAveCharWidth == 0)
1571 {
1572 TM->tmAveCharWidth = 1;
1573 }
1574
1575 /* Correct forumla to get the maxcharwidth from unicode and ansi font */
1576 TM->tmMaxCharWidth = (FT_MulFix(Face->max_advance_width, XScale) + 32) >> 6;
1577
1578 if (RealFont)
1579 {
1580 TM->tmWeight = FontGDI->OriginalWeight;
1581 }
1582 else
1583 {
1584 if (FontGDI->OriginalWeight != FW_DONTCARE &&
1585 FontGDI->OriginalWeight != FW_NORMAL)
1586 {
1587 TM->tmWeight = FontGDI->OriginalWeight;
1588 }
1589 else
1590 {
1591 TM->tmWeight = FontGDI->RequestWeight;
1592 }
1593 }
1594
1595 TM->tmOverhang = 0;
1596 TM->tmDigitizedAspectX = 96;
1597 TM->tmDigitizedAspectY = 96;
1598 if (face_has_symbol_charmap(Face) ||
1599 (pOS2->usFirstCharIndex >= 0xf000 && pOS2->usFirstCharIndex < 0xf100))
1600 {
1601 USHORT cpOEM, cpAnsi;
1602
1603 EngGetCurrentCodePage(&cpOEM, &cpAnsi);
1604 TM->tmFirstChar = 0;
1605 switch(cpAnsi)
1606 {
1607 case 1257: /* Baltic */
1608 TM->tmLastChar = 0xf8fd;
1609 break;
1610 default:
1611 TM->tmLastChar = 0xf0ff;
1612 }
1613 TM->tmBreakChar = 0x20;
1614 TM->tmDefaultChar = 0x1f;
1615 }
1616 else
1617 {
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) */
1620
1621 if(pOS2->usFirstCharIndex <= 1)
1622 TM->tmBreakChar = pOS2->usFirstCharIndex + 2;
1623 else if (pOS2->usFirstCharIndex > 0xff)
1624 TM->tmBreakChar = 0x20;
1625 else
1626 TM->tmBreakChar = pOS2->usFirstCharIndex;
1627 TM->tmDefaultChar = TM->tmBreakChar - 1;
1628 }
1629
1630 if (RealFont)
1631 {
1632 TM->tmItalic = FontGDI->OriginalItalic;
1633 TM->tmUnderlined = FALSE;
1634 TM->tmStruckOut = FALSE;
1635 }
1636 else
1637 {
1638 if (FontGDI->OriginalItalic || FontGDI->RequestItalic)
1639 {
1640 TM->tmItalic = 0xFF;
1641 }
1642 else
1643 {
1644 TM->tmItalic = 0;
1645 }
1646 TM->tmUnderlined = (FontGDI->RequestUnderline ? 0xFF : 0);
1647 TM->tmStruckOut = (FontGDI->RequestStrikeOut ? 0xFF : 0);
1648 }
1649
1650 if (!FT_IS_FIXED_WIDTH(Face))
1651 {
1652 switch (pOS2->panose[PAN_PROPORTION_INDEX])
1653 {
1654 case PAN_PROP_MONOSPACED:
1655 TM->tmPitchAndFamily = 0;
1656 break;
1657 default:
1658 TM->tmPitchAndFamily = _TMPF_VARIABLE_PITCH;
1659 break;
1660 }
1661 }
1662 else
1663 {
1664 TM->tmPitchAndFamily = 0;
1665 }
1666
1667 switch (pOS2->panose[PAN_FAMILYTYPE_INDEX])
1668 {
1669 case PAN_FAMILY_SCRIPT:
1670 TM->tmPitchAndFamily |= FF_SCRIPT;
1671 break;
1672 case PAN_FAMILY_DECORATIVE:
1673 TM->tmPitchAndFamily |= FF_DECORATIVE;
1674 break;
1675
1676 case PAN_ANY:
1677 case PAN_NO_FIT:
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 */
1682 {
1683 TM->tmPitchAndFamily = FF_MODERN;
1684 }
1685 else
1686 {
1687 switch (pOS2->panose[PAN_SERIFSTYLE_INDEX])
1688 {
1689 case PAN_ANY:
1690 case PAN_NO_FIT:
1691 default:
1692 TM->tmPitchAndFamily |= FF_DONTCARE;
1693 break;
1694
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;
1705 break;
1706
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;
1713 break;
1714 }
1715 }
1716 break;
1717 default:
1718 TM->tmPitchAndFamily |= FF_DONTCARE;
1719 }
1720
1721 if (FT_IS_SCALABLE(Face))
1722 {
1723 TM->tmPitchAndFamily |= TMPF_VECTOR;
1724 }
1725 if (FT_IS_SFNT(Face))
1726 {
1727 TM->tmPitchAndFamily |= TMPF_TRUETYPE;
1728 }
1729
1730 TM->tmCharSet = FontGDI->CharSet;
1731 }
1732
1733 static void FASTCALL
1734 FillTM(TEXTMETRICW *TM, PFONTGDI FontGDI,
1735 TT_OS2 *pOS2, TT_HoriHeader *pHori,
1736 FT_WinFNT_HeaderRec *pFNT)
1737 {
1738 FillTMEx(TM, FontGDI, pOS2, pHori, pFNT, FALSE);
1739 }
1740
1741 /*************************************************************
1742 * IntGetOutlineTextMetrics
1743 *
1744 */
1745 INT FASTCALL
1746 IntGetOutlineTextMetrics(PFONTGDI FontGDI,
1747 UINT Size,
1748 OUTLINETEXTMETRICW *Otm)
1749 {
1750 unsigned Needed;
1751 TT_OS2 *pOS2;
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;
1758 FT_Error Error;
1759 char *Cp;
1760 NTSTATUS status;
1761 FT_Face Face = FontGDI->SharedFace->Face;
1762
1763 Needed = sizeof(OUTLINETEXTMETRICW);
1764
1765 RtlInitAnsiString(&FamilyNameA, Face->family_name);
1766 status = RtlAnsiStringToUnicodeString(&FamilyNameW, &FamilyNameA, TRUE);
1767 if (!NT_SUCCESS(status))
1768 {
1769 return 0;
1770 }
1771
1772 RtlInitAnsiString(&StyleNameA, Face->style_name);
1773 status = RtlAnsiStringToUnicodeString(&StyleNameW, &StyleNameA, TRUE);
1774 if (!NT_SUCCESS(status))
1775 {
1776 RtlFreeUnicodeString(&FamilyNameW);
1777 return 0;
1778 }
1779
1780 /* These names should be read from the TT name table */
1781
1782 /* Length of otmpFamilyName */
1783 Needed += FamilyNameW.Length + sizeof(WCHAR);
1784
1785 RtlInitUnicodeString(&Regular, L"Regular");
1786 /* Length of otmpFaceName */
1787 if (RtlEqualUnicodeString(&StyleNameW, &Regular, TRUE))
1788 {
1789 Needed += FamilyNameW.Length + sizeof(WCHAR); /* Just the family name */
1790 }
1791 else
1792 {
1793 Needed += FamilyNameW.Length + StyleNameW.Length + (sizeof(WCHAR) << 1); /* family + " " + style */
1794 }
1795
1796 /* Length of otmpStyleName */
1797 Needed += StyleNameW.Length + sizeof(WCHAR);
1798
1799 /* Length of otmpFullName */
1800 Needed += FamilyNameW.Length + StyleNameW.Length + (sizeof(WCHAR) << 1);
1801
1802 if (Size < Needed)
1803 {
1804 RtlFreeUnicodeString(&FamilyNameW);
1805 RtlFreeUnicodeString(&StyleNameW);
1806 return Needed;
1807 }
1808
1809 XScale = Face->size->metrics.x_scale;
1810 YScale = Face->size->metrics.y_scale;
1811
1812 IntLockFreeType;
1813 pOS2 = FT_Get_Sfnt_Table(Face, ft_sfnt_os2);
1814 if (NULL == pOS2)
1815 {
1816 IntUnLockFreeType;
1817 DPRINT1("Can't find OS/2 table - not TT font?\n");
1818 RtlFreeUnicodeString(&StyleNameW);
1819 RtlFreeUnicodeString(&FamilyNameW);
1820 return 0;
1821 }
1822
1823 pHori = FT_Get_Sfnt_Table(Face, ft_sfnt_hhea);
1824 if (NULL == pHori)
1825 {
1826 IntUnLockFreeType;
1827 DPRINT1("Can't find HHEA table - not TT font?\n");
1828 RtlFreeUnicodeString(&StyleNameW);
1829 RtlFreeUnicodeString(&FamilyNameW);
1830 return 0;
1831 }
1832
1833 pPost = FT_Get_Sfnt_Table(Face, ft_sfnt_post); /* We can live with this failing */
1834
1835 Error = FT_Get_WinFNT_Header(Face , &Win);
1836
1837 Otm->otmSize = Needed;
1838
1839 FillTM(&Otm->otmTextMetrics, FontGDI, pOS2, pHori, !Error ? &Win : 0);
1840
1841 Otm->otmFiller = 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;
1872 if (!pPost)
1873 {
1874 Otm->otmsUnderscoreSize = 0;
1875 Otm->otmsUnderscorePosition = 0;
1876 }
1877 else
1878 {
1879 Otm->otmsUnderscoreSize = (FT_MulFix(pPost->underlineThickness, YScale) + 32) >> 6;
1880 Otm->otmsUnderscorePosition = (FT_MulFix(pPost->underlinePosition, YScale) + 32) >> 6;
1881 }
1882
1883 IntUnLockFreeType;
1884
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))
1896 {
1897 wcscat((WCHAR*) Cp, L" ");
1898 wcscat((WCHAR*) Cp, StyleNameW.Buffer);
1899 Cp += FamilyNameW.Length + StyleNameW.Length + (sizeof(WCHAR) << 1);
1900 }
1901 else
1902 {
1903 Cp += FamilyNameW.Length + sizeof(WCHAR);
1904 }
1905 Otm->otmpFullName = (LPSTR)(Cp - (char*) Otm);
1906 wcscpy((WCHAR*) Cp, FamilyNameW.Buffer);
1907 wcscat((WCHAR*) Cp, L" ");
1908 wcscat((WCHAR*) Cp, StyleNameW.Buffer);
1909
1910 RtlFreeUnicodeString(&StyleNameW);
1911 RtlFreeUnicodeString(&FamilyNameW);
1912
1913 return Needed;
1914 }
1915
1916 static PFONTGDI FASTCALL
1917 FindFaceNameInList(PUNICODE_STRING FaceName, PLIST_ENTRY Head)
1918 {
1919 PLIST_ENTRY Entry;
1920 PFONT_ENTRY CurrentEntry;
1921 ANSI_STRING EntryFaceNameA;
1922 UNICODE_STRING EntryFaceNameW;
1923 FONTGDI *FontGDI;
1924 NTSTATUS status;
1925
1926 Entry = Head->Flink;
1927 while (Entry != Head)
1928 {
1929 CurrentEntry = CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry);
1930
1931 FontGDI = CurrentEntry->Font;
1932 ASSERT(FontGDI);
1933
1934 RtlInitAnsiString(&EntryFaceNameA, FontGDI->SharedFace->Face->family_name);
1935 status = RtlAnsiStringToUnicodeString(&EntryFaceNameW, &EntryFaceNameA, TRUE);
1936 if (!NT_SUCCESS(status))
1937 {
1938 break;
1939 }
1940
1941 if ((LF_FACESIZE - 1) * sizeof(WCHAR) < EntryFaceNameW.Length)
1942 {
1943 EntryFaceNameW.Length = (LF_FACESIZE - 1) * sizeof(WCHAR);
1944 EntryFaceNameW.Buffer[LF_FACESIZE - 1] = L'\0';
1945 }
1946
1947 if (RtlEqualUnicodeString(FaceName, &EntryFaceNameW, TRUE))
1948 {
1949 RtlFreeUnicodeString(&EntryFaceNameW);
1950 return FontGDI;
1951 }
1952
1953 RtlFreeUnicodeString(&EntryFaceNameW);
1954 Entry = Entry->Flink;
1955 }
1956
1957 return NULL;
1958 }
1959
1960 static PFONTGDI FASTCALL
1961 FindFaceNameInLists(PUNICODE_STRING FaceName)
1962 {
1963 PPROCESSINFO Win32Process;
1964 PFONTGDI Font;
1965
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);
1972 if (NULL != Font)
1973 {
1974 return Font;
1975 }
1976
1977 /* Search the global list */
1978 IntLockGlobalFonts;
1979 Font = FindFaceNameInList(FaceName, &FontListHead);
1980 IntUnLockGlobalFonts;
1981
1982 return Font;
1983 }
1984
1985 /* See https://msdn.microsoft.com/en-us/library/bb165625(v=vs.90).aspx */
1986 static BYTE
1987 CharSetFromLangID(LANGID LangID)
1988 {
1989 /* FIXME: Add more and fix if wrong */
1990 switch (PRIMARYLANGID(LangID))
1991 {
1992 case LANG_CHINESE:
1993 switch (SUBLANGID(LangID))
1994 {
1995 case SUBLANG_CHINESE_TRADITIONAL:
1996 return CHINESEBIG5_CHARSET;
1997 case SUBLANG_CHINESE_SIMPLIFIED:
1998 default:
1999 break;
2000 }
2001 return GB2312_CHARSET;
2002
2003 case LANG_CZECH: case LANG_HUNGARIAN: case LANG_POLISH:
2004 case LANG_SLOVAK: case LANG_SLOVENIAN: case LANG_ROMANIAN:
2005 return EASTEUROPE_CHARSET;
2006
2007 case LANG_RUSSIAN: case LANG_BULGARIAN: case LANG_MACEDONIAN:
2008 case LANG_SERBIAN: case LANG_UKRAINIAN:
2009 return RUSSIAN_CHARSET;
2010
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;
2020
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;
2027 }
2028 }
2029
2030 static void
2031 SwapEndian(LPVOID pvData, DWORD Size)
2032 {
2033 BYTE b, *pb = pvData;
2034 Size /= 2;
2035 while (Size-- > 0)
2036 {
2037 b = pb[0];
2038 pb[0] = pb[1];
2039 pb[1] = b;
2040 ++pb; ++pb;
2041 }
2042 }
2043
2044 static NTSTATUS
2045 IntGetFontLocalizedName(PUNICODE_STRING pNameW, FT_Face Face,
2046 FT_UShort NameID, FT_UShort LangID)
2047 {
2048 FT_SfntName Name;
2049 INT i, Count;
2050 WCHAR Buf[LF_FULLFACESIZE];
2051 FT_Error Error;
2052 NTSTATUS Status = STATUS_NOT_FOUND;
2053 ANSI_STRING AnsiName;
2054
2055 RtlFreeUnicodeString(pNameW);
2056
2057 Count = FT_Get_Sfnt_Name_Count(Face);
2058 for (i = 0; i < Count; ++i)
2059 {
2060 Error = FT_Get_Sfnt_Name(Face, i, &Name);
2061 if (Error)
2062 continue;
2063
2064 if (Name.platform_id != TT_PLATFORM_MICROSOFT ||
2065 Name.encoding_id != TT_MS_ID_UNICODE_CS)
2066 {
2067 continue; /* not Microsoft Unicode name */
2068 }
2069
2070 if (Name.name_id != NameID || Name.language_id != LangID)
2071 {
2072 continue; /* mismatched */
2073 }
2074
2075 if (Name.string == NULL || Name.string_len == 0 ||
2076 (Name.string[0] == 0 && Name.string[1] == 0))
2077 {
2078 continue; /* invalid string */
2079 }
2080
2081 if (sizeof(Buf) < Name.string_len + sizeof(UNICODE_NULL))
2082 {
2083 continue; /* name too long */
2084 }
2085
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;
2089
2090 /* Convert UTF-16 big endian to little endian */
2091 SwapEndian(Buf, Name.string_len);
2092
2093 RtlCreateUnicodeString(pNameW, Buf);
2094 Status = STATUS_SUCCESS;
2095 break;
2096 }
2097
2098 if (Status == STATUS_NOT_FOUND)
2099 {
2100 if (LangID != gusEnglishUS)
2101 {
2102 Status = IntGetFontLocalizedName(pNameW, Face, NameID, gusEnglishUS);
2103 }
2104 }
2105 if (Status == STATUS_NOT_FOUND)
2106 {
2107 RtlInitAnsiString(&AnsiName, Face->family_name);
2108 Status = RtlAnsiStringToUnicodeString(pNameW, &AnsiName, TRUE);
2109 }
2110
2111 return Status;
2112 }
2113
2114 static void FASTCALL
2115 FontFamilyFillInfo(PFONTFAMILYINFO Info, LPCWSTR FaceName,
2116 LPCWSTR FullName, PFONTGDI FontGDI)
2117 {
2118 ANSI_STRING StyleA;
2119 UNICODE_STRING StyleW;
2120 TT_OS2 *pOS2;
2121 FONTSIGNATURE fs;
2122 CHARSETINFO CharSetInfo;
2123 unsigned i, Size;
2124 OUTLINETEXTMETRICW *Otm;
2125 LOGFONTW *Lf;
2126 TEXTMETRICW *TM;
2127 NEWTEXTMETRICW *Ntm;
2128 DWORD fs0;
2129 NTSTATUS status;
2130 FT_Face Face = FontGDI->SharedFace->Face;
2131 UNICODE_STRING NameW;
2132
2133 RtlInitUnicodeString(&NameW, NULL);
2134 RtlZeroMemory(Info, sizeof(FONTFAMILYINFO));
2135 Size = IntGetOutlineTextMetrics(FontGDI, 0, NULL);
2136 Otm = ExAllocatePoolWithTag(PagedPool, Size, GDITAG_TEXT);
2137 if (!Otm)
2138 {
2139 return;
2140 }
2141 IntGetOutlineTextMetrics(FontGDI, Size, Otm);
2142
2143 Lf = &Info->EnumLogFontEx.elfLogFont;
2144 TM = &Otm->otmTextMetrics;
2145
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;
2155
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;
2178
2179 if (550 < TM->tmWeight) Ntm->ntmFlags |= NTM_BOLD;
2180
2181 if (0 == Ntm->ntmFlags) Ntm->ntmFlags = NTM_REGULAR;
2182
2183 Ntm->ntmSizeEM = Otm->otmEMSquare;
2184 Ntm->ntmCellHeight = Otm->otmEMSquare;
2185 Ntm->ntmAvgWidth = 0;
2186
2187 Info->FontType = (0 != (TM->tmPitchAndFamily & TMPF_TRUETYPE)
2188 ? TRUETYPE_FONTTYPE : 0);
2189
2190 if (0 == (TM->tmPitchAndFamily & TMPF_VECTOR))
2191 Info->FontType |= RASTER_FONTTYPE;
2192
2193 ExFreePoolWithTag(Otm, GDITAG_TEXT);
2194
2195 /* face name */
2196 if (FaceName)
2197 {
2198 RtlStringCbCopyW(Lf->lfFaceName, sizeof(Lf->lfFaceName), FaceName);
2199 }
2200 else
2201 {
2202 status = IntGetFontLocalizedName(&NameW, Face, TT_NAME_ID_FONT_FAMILY,
2203 gusLanguageID);
2204 if (NT_SUCCESS(status))
2205 {
2206 /* store it */
2207 RtlStringCbCopyW(Lf->lfFaceName, sizeof(Lf->lfFaceName),
2208 NameW.Buffer);
2209 RtlFreeUnicodeString(&NameW);
2210 }
2211 }
2212
2213 /* full name */
2214 if (FullName)
2215 {
2216 RtlStringCbCopyW(Info->EnumLogFontEx.elfFullName,
2217 sizeof(Info->EnumLogFontEx.elfFullName),
2218 FullName);
2219 }
2220 else
2221 {
2222 status = IntGetFontLocalizedName(&NameW, Face, TT_NAME_ID_FULL_NAME,
2223 gusLanguageID);
2224 if (NT_SUCCESS(status))
2225 {
2226 /* store it */
2227 RtlStringCbCopyW(Info->EnumLogFontEx.elfFullName,
2228 sizeof(Info->EnumLogFontEx.elfFullName),
2229 NameW.Buffer);
2230 RtlFreeUnicodeString(&NameW);
2231 }
2232 }
2233
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))
2239 {
2240 return;
2241 }
2242 Info->EnumLogFontEx.elfScript[0] = UNICODE_NULL;
2243
2244 IntLockFreeType;
2245 pOS2 = FT_Get_Sfnt_Table(Face, ft_sfnt_os2);
2246
2247 if (!pOS2)
2248 {
2249 IntUnLockFreeType;
2250 return;
2251 }
2252
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;
2259
2260 if (0 == pOS2->version)
2261 {
2262 FT_UInt Dummy;
2263
2264 if (FT_Get_First_Char(Face, &Dummy) < 0x100)
2265 fs.fsCsb[0] |= FS_LATIN1;
2266 else
2267 fs.fsCsb[0] |= FS_SYMBOL;
2268 }
2269 IntUnLockFreeType;
2270
2271 if (fs.fsCsb[0] == 0)
2272 {
2273 /* Let's see if we can find any interesting cmaps */
2274 for (i = 0; i < (UINT)Face->num_charmaps; i++)
2275 {
2276 switch (Face->charmaps[i]->encoding)
2277 {
2278 case FT_ENCODING_UNICODE:
2279 case FT_ENCODING_APPLE_ROMAN:
2280 fs.fsCsb[0] |= FS_LATIN1;
2281 break;
2282 case FT_ENCODING_MS_SYMBOL:
2283 fs.fsCsb[0] |= FS_SYMBOL;
2284 break;
2285 default:
2286 break;
2287 }
2288 }
2289 }
2290
2291 for (i = 0; i < MAXTCIINDEX; i++)
2292 {
2293 fs0 = 1L << i;
2294 if (fs.fsCsb[0] & fs0)
2295 {
2296 if (!IntTranslateCharsetInfo(&fs0, &CharSetInfo, TCI_SRCFONTSIG))
2297 {
2298 CharSetInfo.ciCharset = DEFAULT_CHARSET;
2299 }
2300 if (DEFAULT_CHARSET != CharSetInfo.ciCharset)
2301 {
2302 if (ElfScripts[i])
2303 wcscpy(Info->EnumLogFontEx.elfScript, ElfScripts[i]);
2304 else
2305 {
2306 DPRINT1("Unknown elfscript for bit %u\n", i);
2307 }
2308 }
2309 }
2310 }
2311 Info->NewTextMetricEx.ntmFontSig = fs;
2312 }
2313
2314 static int FASTCALL
2315 FindFaceNameInInfo(PUNICODE_STRING FaceName, PFONTFAMILYINFO Info, DWORD InfoEntries)
2316 {
2317 DWORD i;
2318 UNICODE_STRING InfoFaceName;
2319
2320 for (i = 0; i < InfoEntries; i++)
2321 {
2322 RtlInitUnicodeString(&InfoFaceName, Info[i].EnumLogFontEx.elfLogFont.lfFaceName);
2323 if (RtlEqualUnicodeString(&InfoFaceName, FaceName, TRUE))
2324 {
2325 return i;
2326 }
2327 }
2328
2329 return -1;
2330 }
2331
2332 static BOOLEAN FASTCALL
2333 FontFamilyInclude(LPLOGFONTW LogFont, PUNICODE_STRING FaceName,
2334 PFONTFAMILYINFO Info, DWORD InfoEntries)
2335 {
2336 UNICODE_STRING LogFontFaceName;
2337
2338 RtlInitUnicodeString(&LogFontFaceName, LogFont->lfFaceName);
2339 if (0 != LogFontFaceName.Length &&
2340 !RtlEqualUnicodeString(&LogFontFaceName, FaceName, TRUE))
2341 {
2342 return FALSE;
2343 }
2344
2345 return FindFaceNameInInfo(FaceName, Info, InfoEntries) < 0;
2346 }
2347
2348 static BOOLEAN FASTCALL
2349 GetFontFamilyInfoForList(LPLOGFONTW LogFont,
2350 PFONTFAMILYINFO Info,
2351 DWORD *Count,
2352 DWORD Size,
2353 PLIST_ENTRY Head)
2354 {
2355 PLIST_ENTRY Entry;
2356 PFONT_ENTRY CurrentEntry;
2357 ANSI_STRING EntryFaceNameA;
2358 UNICODE_STRING EntryFaceNameW;
2359 FONTGDI *FontGDI;
2360 NTSTATUS status;
2361
2362 Entry = Head->Flink;
2363 while (Entry != Head)
2364 {
2365 CurrentEntry = (PFONT_ENTRY) CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry);
2366
2367 FontGDI = CurrentEntry->Font;
2368 ASSERT(FontGDI);
2369
2370 RtlInitAnsiString(&EntryFaceNameA, FontGDI->SharedFace->Face->family_name);
2371 status = RtlAnsiStringToUnicodeString(&EntryFaceNameW, &EntryFaceNameA, TRUE);
2372 if (!NT_SUCCESS(status))
2373 {
2374 return FALSE;
2375 }
2376
2377 if ((LF_FACESIZE - 1) * sizeof(WCHAR) < EntryFaceNameW.Length)
2378 {
2379 EntryFaceNameW.Length = (LF_FACESIZE - 1) * sizeof(WCHAR);
2380 EntryFaceNameW.Buffer[LF_FACESIZE - 1] = L'\0';
2381 }
2382
2383 if (FontFamilyInclude(LogFont, &EntryFaceNameW, Info, min(*Count, Size)))
2384 {
2385 if (*Count < Size)
2386 {
2387 FontFamilyFillInfo(Info + *Count, EntryFaceNameW.Buffer,
2388 NULL, FontGDI);
2389 }
2390 (*Count)++;
2391 }
2392 RtlFreeUnicodeString(&EntryFaceNameW);
2393 Entry = Entry->Flink;
2394 }
2395
2396 return TRUE;
2397 }
2398
2399 typedef struct FontFamilyInfoCallbackContext
2400 {
2401 LPLOGFONTW LogFont;
2402 PFONTFAMILYINFO Info;
2403 DWORD Count;
2404 DWORD Size;
2405 } FONT_FAMILY_INFO_CALLBACK_CONTEXT, *PFONT_FAMILY_INFO_CALLBACK_CONTEXT;
2406
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)
2412 {
2413 PFONT_FAMILY_INFO_CALLBACK_CONTEXT InfoContext;
2414 UNICODE_STRING RegistryName, RegistryValue;
2415 int Existing;
2416 PFONTGDI FontGDI;
2417
2418 if (REG_SZ != ValueType)
2419 {
2420 return STATUS_SUCCESS;
2421 }
2422 InfoContext = (PFONT_FAMILY_INFO_CALLBACK_CONTEXT) Context;
2423 RtlInitUnicodeString(&RegistryName, ValueName);
2424
2425 /* Do we need to include this font family? */
2426 if (FontFamilyInclude(InfoContext->LogFont, &RegistryName, InfoContext->Info,
2427 min(InfoContext->Count, InfoContext->Size)))
2428 {
2429 RtlInitUnicodeString(&RegistryValue, (PCWSTR) ValueData);
2430 Existing = FindFaceNameInInfo(&RegistryValue, InfoContext->Info,
2431 min(InfoContext->Count, InfoContext->Size));
2432 if (0 <= Existing)
2433 {
2434 /* We already have the information about the "real" font. Just copy it */
2435 if (InfoContext->Count < InfoContext->Size)
2436 {
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);
2442 }
2443 InfoContext->Count++;
2444 return STATUS_SUCCESS;
2445 }
2446
2447 /* Try to find information about the "real" font */
2448 FontGDI = FindFaceNameInLists(&RegistryValue);
2449 if (NULL == FontGDI)
2450 {
2451 /* "Real" font not found, discard this registry entry */
2452 return STATUS_SUCCESS;
2453 }
2454
2455 /* Return info about the "real" font but with the name of the alias */
2456 if (InfoContext->Count < InfoContext->Size)
2457 {
2458 FontFamilyFillInfo(InfoContext->Info + InfoContext->Count,
2459 RegistryName.Buffer, NULL, FontGDI);
2460 }
2461 InfoContext->Count++;
2462 return STATUS_SUCCESS;
2463 }
2464
2465 return STATUS_SUCCESS;
2466 }
2467
2468 static BOOLEAN FASTCALL
2469 GetFontFamilyInfoForSubstitutes(LPLOGFONTW LogFont,
2470 PFONTFAMILYINFO Info,
2471 DWORD *Count,
2472 DWORD Size)
2473 {
2474 RTL_QUERY_REGISTRY_TABLE QueryTable[2] = {{0}};
2475 FONT_FAMILY_INFO_CALLBACK_CONTEXT Context;
2476 NTSTATUS Status;
2477
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;
2484
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;
2492
2493 QueryTable[1].QueryRoutine = NULL;
2494 QueryTable[1].Name = NULL;
2495
2496 Status = RtlQueryRegistryValues(RTL_REGISTRY_WINDOWS_NT,
2497 L"FontSubstitutes",
2498 QueryTable,
2499 &Context,
2500 NULL);
2501 if (NT_SUCCESS(Status))
2502 {
2503 *Count = Context.Count;
2504 }
2505
2506 return NT_SUCCESS(Status) || STATUS_OBJECT_NAME_NOT_FOUND == Status;
2507 }
2508
2509 BOOL
2510 FASTCALL
2511 ftGdiGetRasterizerCaps(LPRASTERIZER_STATUS lprs)
2512 {
2513 if ( lprs )
2514 {
2515 lprs->nSize = sizeof(RASTERIZER_STATUS);
2516 lprs->wFlags = TT_AVAILABLE | TT_ENABLED;
2517 lprs->nLanguageID = gusLanguageID;
2518 return TRUE;
2519 }
2520 EngSetLastError(ERROR_INVALID_PARAMETER);
2521 return FALSE;
2522 }
2523
2524 static
2525 BOOL
2526 SameScaleMatrix(
2527 PMATRIX pmx1,
2528 PMATRIX pmx2)
2529 {
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));
2534 }
2535
2536 FT_BitmapGlyph APIENTRY
2537 ftGdiGlyphCacheGet(
2538 FT_Face Face,
2539 INT GlyphIndex,
2540 INT Height,
2541 PMATRIX pmx)
2542 {
2543 PLIST_ENTRY CurrentEntry;
2544 PFONT_CACHE_ENTRY FontEntry;
2545
2546 ASSERT_FREETYPE_LOCK_HELD();
2547
2548 CurrentEntry = FontCacheListHead.Flink;
2549 while (CurrentEntry != &FontCacheListHead)
2550 {
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)))
2556 break;
2557 CurrentEntry = CurrentEntry->Flink;
2558 }
2559
2560 if (CurrentEntry == &FontCacheListHead)
2561 {
2562 return NULL;
2563 }
2564
2565 RemoveEntryList(CurrentEntry);
2566 InsertHeadList(&FontCacheListHead, CurrentEntry);
2567 return FontEntry->BitmapGlyph;
2568 }
2569
2570 /* no cache */
2571 FT_BitmapGlyph APIENTRY
2572 ftGdiGlyphSet(
2573 FT_Face Face,
2574 FT_GlyphSlot GlyphSlot,
2575 FT_Render_Mode RenderMode)
2576 {
2577 FT_Glyph Glyph;
2578 INT error;
2579 FT_Bitmap AlignedBitmap;
2580 FT_BitmapGlyph BitmapGlyph;
2581
2582 error = FT_Get_Glyph(GlyphSlot, &Glyph);
2583 if (error)
2584 {
2585 DPRINT1("Failure getting glyph.\n");
2586 return NULL;
2587 }
2588
2589 error = FT_Glyph_To_Bitmap(&Glyph, RenderMode, 0, 1);
2590 if (error)
2591 {
2592 FT_Done_Glyph(Glyph);
2593 DPRINT1("Failure rendering glyph.\n");
2594 return NULL;
2595 }
2596
2597 BitmapGlyph = (FT_BitmapGlyph)Glyph;
2598 FT_Bitmap_New(&AlignedBitmap);
2599 if (FT_Bitmap_Convert(GlyphSlot->library, &BitmapGlyph->bitmap, &AlignedBitmap, 4))
2600 {
2601 DPRINT1("Conversion failed\n");
2602 FT_Done_Glyph((FT_Glyph)BitmapGlyph);
2603 return NULL;
2604 }
2605
2606 FT_Bitmap_Done(GlyphSlot->library, &BitmapGlyph->bitmap);
2607 BitmapGlyph->bitmap = AlignedBitmap;
2608
2609 return BitmapGlyph;
2610 }
2611
2612 FT_BitmapGlyph APIENTRY
2613 ftGdiGlyphCacheSet(
2614 FT_Face Face,
2615 INT GlyphIndex,
2616 INT Height,
2617 PMATRIX pmx,
2618 FT_GlyphSlot GlyphSlot,
2619 FT_Render_Mode RenderMode)
2620 {
2621 FT_Glyph GlyphCopy;
2622 INT error;
2623 PFONT_CACHE_ENTRY NewEntry;
2624 FT_Bitmap AlignedBitmap;
2625 FT_BitmapGlyph BitmapGlyph;
2626
2627 ASSERT_FREETYPE_LOCK_HELD();
2628
2629 error = FT_Get_Glyph(GlyphSlot, &GlyphCopy);
2630 if (error)
2631 {
2632 DPRINT1("Failure caching glyph.\n");
2633 return NULL;
2634 };
2635
2636 error = FT_Glyph_To_Bitmap(&GlyphCopy, RenderMode, 0, 1);
2637 if (error)
2638 {
2639 FT_Done_Glyph(GlyphCopy);
2640 DPRINT1("Failure rendering glyph.\n");
2641 return NULL;
2642 };
2643
2644 NewEntry = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_CACHE_ENTRY), TAG_FONT);
2645 if (!NewEntry)
2646 {
2647 DPRINT1("Alloc failure caching glyph.\n");
2648 FT_Done_Glyph(GlyphCopy);
2649 return NULL;
2650 }
2651
2652 BitmapGlyph = (FT_BitmapGlyph)GlyphCopy;
2653 FT_Bitmap_New(&AlignedBitmap);
2654 if(FT_Bitmap_Convert(GlyphSlot->library, &BitmapGlyph->bitmap, &AlignedBitmap, 4))
2655 {
2656 DPRINT1("Conversion failed\n");
2657 ExFreePoolWithTag(NewEntry, TAG_FONT);
2658 FT_Bitmap_Done(GlyphSlot->library, &AlignedBitmap);
2659 FT_Done_Glyph((FT_Glyph)BitmapGlyph);
2660 return NULL;
2661 }
2662
2663 FT_Bitmap_Done(GlyphSlot->library, &BitmapGlyph->bitmap);
2664 BitmapGlyph->bitmap = AlignedBitmap;
2665
2666 NewEntry->GlyphIndex = GlyphIndex;
2667 NewEntry->Face = Face;
2668 NewEntry->BitmapGlyph = BitmapGlyph;
2669 NewEntry->Height = Height;
2670 NewEntry->mxWorldToDevice = *pmx;
2671
2672 InsertHeadList(&FontCacheListHead, &NewEntry->ListEntry);
2673 if (++FontCacheNumEntries > MAX_FONT_CACHE)
2674 {
2675 NewEntry = CONTAINING_RECORD(FontCacheListHead.Blink, FONT_CACHE_ENTRY, ListEntry);
2676 RemoveCachedEntry(NewEntry);
2677 }
2678
2679 return BitmapGlyph;
2680 }
2681
2682
2683 static void FTVectorToPOINTFX(FT_Vector *vec, POINTFX *pt)
2684 {
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));
2691 }
2692
2693 /*
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.
2697 */
2698 static __inline FT_Fixed FT_FixedFromFloat(float f)
2699 {
2700 short value = f;
2701 unsigned short fract = (f - value) * 0xFFFF;
2702 return (FT_Fixed)((long)value << 16 | (unsigned long)fract);
2703 }
2704
2705 /*
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.
2708 */
2709 static __inline FT_Fixed FT_FixedFromFIXED(FIXED f)
2710 {
2711 return (FT_Fixed)((long)f.value << 16 | (unsigned long)f.fract);
2712 }
2713
2714 static unsigned int get_native_glyph_outline(FT_Outline *outline, unsigned int buflen, char *buf)
2715 {
2716 TTPOLYGONHEADER *pph;
2717 TTPOLYCURVE *ppc;
2718 int needed = 0, point = 0, contour, first_pt;
2719 unsigned int pph_start, cpfx;
2720 DWORD type;
2721
2722 for (contour = 0; contour < outline->n_contours; contour++)
2723 {
2724 /* Ignore contours containing one point */
2725 if (point == outline->contours[contour])
2726 {
2727 point++;
2728 continue;
2729 }
2730
2731 pph_start = needed;
2732 pph = (TTPOLYGONHEADER *)(buf + needed);
2733 first_pt = point;
2734 if (buf)
2735 {
2736 pph->dwType = TT_POLYGON_TYPE;
2737 FTVectorToPOINTFX(&outline->points[point], &pph->pfxStart);
2738 }
2739 needed += sizeof(*pph);
2740 point++;
2741 while (point <= outline->contours[contour])
2742 {
2743 ppc = (TTPOLYCURVE *)(buf + needed);
2744 type = outline->tags[point] & FT_Curve_Tag_On ?
2745 TT_PRIM_LINE : TT_PRIM_QSPLINE;
2746 cpfx = 0;
2747 do
2748 {
2749 if (buf)
2750 FTVectorToPOINTFX(&outline->points[point], &ppc->apfx[cpfx]);
2751 cpfx++;
2752 point++;
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
2757 only for Beziers */
2758 if (point > outline->contours[contour] &&
2759 !(outline->tags[point-1] & FT_Curve_Tag_On))
2760 {
2761 if (buf)
2762 FTVectorToPOINTFX(&outline->points[first_pt], &ppc->apfx[cpfx]);
2763 cpfx++;
2764 }
2765 else if (point <= outline->contours[contour] &&
2766 outline->tags[point] & FT_Curve_Tag_On)
2767 {
2768 /* add closing pt for bezier */
2769 if (buf)
2770 FTVectorToPOINTFX(&outline->points[point], &ppc->apfx[cpfx]);
2771 cpfx++;
2772 point++;
2773 }
2774 if (buf)
2775 {
2776 ppc->wType = type;
2777 ppc->cpfx = cpfx;
2778 }
2779 needed += sizeof(*ppc) + (cpfx - 1) * sizeof(POINTFX);
2780 }
2781 if (buf)
2782 pph->cb = needed - pph_start;
2783 }
2784 return needed;
2785 }
2786
2787 static unsigned int get_bezier_glyph_outline(FT_Outline *outline, unsigned int buflen, char *buf)
2788 {
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:
2793 r1 = r0 + c/3
2794 r2 = r1 + (c + b)/3
2795 r3 = r0 + c + b + a
2796
2797 A quadratic Bezier has the form:
2798 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
2799
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
2804 */
2805 int contour, point = 0, first_pt;
2806 TTPOLYGONHEADER *pph;
2807 TTPOLYCURVE *ppc;
2808 DWORD pph_start, cpfx, type;
2809 FT_Vector cubic_control[4];
2810 unsigned int needed = 0;
2811
2812 for (contour = 0; contour < outline->n_contours; contour++)
2813 {
2814 pph_start = needed;
2815 pph = (TTPOLYGONHEADER *)(buf + needed);
2816 first_pt = point;
2817 if (buf)
2818 {
2819 pph->dwType = TT_POLYGON_TYPE;
2820 FTVectorToPOINTFX(&outline->points[point], &pph->pfxStart);
2821 }
2822 needed += sizeof(*pph);
2823 point++;
2824 while (point <= outline->contours[contour])
2825 {
2826 ppc = (TTPOLYCURVE *)(buf + needed);
2827 type = outline->tags[point] & FT_Curve_Tag_On ?
2828 TT_PRIM_LINE : TT_PRIM_CSPLINE;
2829 cpfx = 0;
2830 do
2831 {
2832 if (type == TT_PRIM_LINE)
2833 {
2834 if (buf)
2835 FTVectorToPOINTFX(&outline->points[point], &ppc->apfx[cpfx]);
2836 cpfx++;
2837 point++;
2838 }
2839 else
2840 {
2841 /* Unlike QSPLINEs, CSPLINEs always have their endpoint
2842 so cpfx = 3n */
2843
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))
2848 {
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;
2853 }
2854 if (point+1 > outline->contours[contour])
2855 cubic_control[3] = outline->points[first_pt];
2856 else
2857 {
2858 cubic_control[3] = outline->points[point+1];
2859 if (!(outline->tags[point+1] & FT_Curve_Tag_On))
2860 {
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;
2865 }
2866 }
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;
2876 if (buf)
2877 {
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]);
2881 }
2882 cpfx += 3;
2883 point++;
2884 }
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.
2890 */
2891 if (point <= outline->contours[contour] &&
2892 outline->tags[point] & FT_Curve_Tag_On)
2893 {
2894 /* This is the closing pt of a bezier, but we've already
2895 added it, so just inc point and carry on */
2896 point++;
2897 }
2898 if (buf)
2899 {
2900 ppc->wType = type;
2901 ppc->cpfx = cpfx;
2902 }
2903 needed += sizeof(*ppc) + (cpfx - 1) * sizeof(POINTFX);
2904 }
2905 if (buf)
2906 pph->cb = needed - pph_start;
2907 }
2908 return needed;
2909 }
2910
2911 static INT
2912 IntRequestFontSize(PDC dc, FT_Face face, LONG Width, LONG Height)
2913 {
2914 FT_Size_RequestRec req;
2915
2916 if (Width < 0)
2917 Width = -Width;
2918
2919 if (Height < 0)
2920 {
2921 Height = -Height;
2922 }
2923 if (Height == 0)
2924 {
2925 Height = dc->ppdev->devinfo.lfDefaultFont.lfHeight;
2926 }
2927 if (Height == 0)
2928 {
2929 Height = Width;
2930 }
2931
2932 if (Height < 1)
2933 Height = 1;
2934
2935 if (Width > 0xFFFFU)
2936 Width = 0xFFFFU;
2937 if (Height > 0xFFFFU)
2938 Height = 0xFFFFU;
2939
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);
2946 }
2947
2948 BOOL
2949 FASTCALL
2950 TextIntUpdateSize(PDC dc,
2951 PTEXTOBJ TextObj,
2952 PFONTGDI FontGDI,
2953 BOOL bDoLock)
2954 {
2955 FT_Face face;
2956 INT error, n;
2957 FT_CharMap charmap, found;
2958 LOGFONTW *plf;
2959
2960 if (bDoLock)
2961 IntLockFreeType;
2962
2963 face = FontGDI->SharedFace->Face;
2964 if (face->charmap == NULL)
2965 {
2966 DPRINT("WARNING: No charmap selected!\n");
2967 DPRINT("This font face has %d charmaps\n", face->num_charmaps);
2968
2969 found = NULL;
2970 for (n = 0; n < face->num_charmaps; n++)
2971 {
2972 charmap = face->charmaps[n];
2973 DPRINT("Found charmap encoding: %i\n", charmap->encoding);
2974 if (charmap->encoding != 0)
2975 {
2976 found = charmap;
2977 break;
2978 }
2979 }
2980 if (!found)
2981 {
2982 DPRINT1("WARNING: Could not find desired charmap!\n");
2983 }
2984 else
2985 {
2986 error = FT_Set_Charmap(face, found);
2987 if (error)
2988 {
2989 DPRINT1("WARNING: Could not set the charmap!\n");
2990 }
2991 }
2992 }
2993
2994 plf = &TextObj->logfont.elfEnumLogfontEx.elfLogFont;
2995
2996 error = IntRequestFontSize(dc, face, plf->lfWidth, plf->lfHeight);
2997
2998 if (bDoLock)
2999 IntUnLockFreeType;
3000
3001 if (error)
3002 {
3003 DPRINT1("Error in setting pixel sizes: %d\n", error);
3004 return FALSE;
3005 }
3006
3007 return TRUE;
3008 }
3009
3010
3011 /*
3012 * Based on WineEngGetGlyphOutline
3013 *
3014 */
3015 ULONG
3016 FASTCALL
3017 ftGdiGetGlyphOutline(
3018 PDC dc,
3019 WCHAR wch,
3020 UINT iFormat,
3021 LPGLYPHMETRICS pgm,
3022 ULONG cjBuf,
3023 PVOID pvBuf,
3024 LPMAT2 pmat2,
3025 BOOL bIgnoreRotation)
3026 {
3027 static const FT_Matrix identityMat = {(1 << 16), 0, 0, (1 << 16)};
3028 PDC_ATTR pdcattr;
3029 PTEXTOBJ TextObj;
3030 PFONTGDI FontGDI;
3031 HFONT hFont = 0;
3032 GLYPHMETRICS gm;
3033 ULONG Size;
3034 FT_Face ft_face;
3035 FT_UInt glyph_index;
3036 DWORD width, height, pitch, needed = 0;
3037 FT_Bitmap ft_bitmap;
3038 FT_Error error;
3039 INT left, right, top = 0, bottom = 0;
3040 FT_Angle angle = 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;
3045 INT orientation;
3046 LONG aveWidth;
3047 INT adv, lsb, bbx; /* These three hold to widths of the unrotated chars */
3048 OUTLINETEXTMETRICW *potm;
3049 XFORM xForm;
3050 LOGFONTW *plf;
3051
3052 DPRINT("%u, %08x, %p, %08lx, %p, %p\n", wch, iFormat, pgm,
3053 cjBuf, pvBuf, pmat2);
3054
3055 pdcattr = dc->pdcattr;
3056
3057 MatrixS2XForm(&xForm, &dc->pdcattr->mxWorldToDevice);
3058 eM11 = xForm.eM11;
3059
3060 hFont = pdcattr->hlfntNew;
3061 TextObj = RealizeFontInit(hFont);
3062
3063 if (!TextObj)
3064 {
3065 EngSetLastError(ERROR_INVALID_HANDLE);
3066 return GDI_ERROR;
3067 }
3068 FontGDI = ObjToGDI(TextObj->Font, FONT);
3069 ft_face = FontGDI->SharedFace->Face;
3070
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;
3074
3075 Size = IntGetOutlineTextMetrics(FontGDI, 0, NULL);
3076 potm = ExAllocatePoolWithTag(PagedPool, Size, GDITAG_TEXT);
3077 if (!potm)
3078 {
3079 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
3080 TEXTOBJ_UnlockText(TextObj);
3081 return GDI_ERROR;
3082 }
3083 IntGetOutlineTextMetrics(FontGDI, Size, potm);
3084
3085 IntLockFreeType;
3086 TextIntUpdateSize(dc, TextObj, FontGDI, FALSE);
3087 FtSetCoordinateTransform(ft_face, DC_pmxWorldToDevice(dc));
3088
3089 TEXTOBJ_UnlockText(TextObj);
3090
3091 if (iFormat & GGO_GLYPH_INDEX)
3092 {
3093 glyph_index = wch;
3094 iFormat &= ~GGO_GLYPH_INDEX;
3095 }
3096 else glyph_index = FT_Get_Char_Index(ft_face, wch);
3097
3098 if (orientation || (iFormat != GGO_METRICS && iFormat != GGO_BITMAP) || aveWidth || pmat2)
3099 load_flags |= FT_LOAD_NO_BITMAP;
3100
3101 if (iFormat & GGO_UNHINTED)
3102 {
3103 load_flags |= FT_LOAD_NO_HINTING;
3104 iFormat &= ~GGO_UNHINTED;
3105 }
3106
3107 error = FT_Load_Glyph(ft_face, glyph_index, load_flags);
3108 if (error)
3109 {
3110 DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index);
3111 IntUnLockFreeType;
3112 if (potm) ExFreePoolWithTag(potm, GDITAG_TEXT);
3113 return GDI_ERROR;
3114 }
3115 IntUnLockFreeType;
3116
3117 if (aveWidth && potm)
3118 {
3119 widthRatio = (FLOAT)aveWidth * eM11 /
3120 (FLOAT) potm->otmTextMetrics.tmAveCharWidth;
3121 }
3122
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;
3126
3127 adv = (INT)((ft_face->glyph->metrics.horiAdvance * widthRatio) + 63) >> 6;
3128 lsb = left >> 6;
3129 bbx = (right - left) >> 6;
3130
3131 DPRINT("Advance = %d, lsb = %d, bbx = %d\n",adv, lsb, bbx);
3132
3133 IntLockFreeType;
3134
3135 /* Scaling transform */
3136 /*if (aveWidth)*/
3137 {
3138
3139 FT_Matrix ftmatrix;
3140 FLOATOBJ efTemp;
3141
3142 PMATRIX pmx = DC_pmxWorldToDevice(dc);
3143
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);
3148
3149 efTemp = pmx->efM12;
3150 FLOATOBJ_MulLong(&efTemp, 0x00010000);
3151 ftmatrix.xy = FLOATOBJ_GetLong(&efTemp);
3152
3153 efTemp = pmx->efM21;
3154 FLOATOBJ_MulLong(&efTemp, 0x00010000);
3155 ftmatrix.yx = FLOATOBJ_GetLong(&efTemp);
3156
3157 efTemp = pmx->efM22;
3158 FLOATOBJ_MulLong(&efTemp, 0x00010000);
3159 ftmatrix.yy = FLOATOBJ_GetLong(&efTemp);
3160
3161 FT_Matrix_Multiply(&ftmatrix, &transMat);
3162 needsTransform = TRUE;
3163 }
3164
3165 /* Rotation transform */
3166 if (orientation)
3167 {
3168 FT_Matrix rotationMat;
3169 FT_Vector vecAngle;
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;
3179 }
3180
3181 /* Extra transformation specified by caller */
3182 if (pmat2)
3183 {
3184 FT_Matrix extraMat;
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;
3192 }
3193
3194 if (potm) ExFreePoolWithTag(potm, GDITAG_TEXT); /* It looks like we are finished with potm ATM. */
3195
3196 if (!needsTransform)
3197 {
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;
3203 gm.gmCellIncY = 0;
3204 }
3205 else
3206 {
3207 INT xc, yc;
3208 FT_Vector vec;
3209 for (xc = 0; xc < 2; xc++)
3210 {
3211 for (yc = 0; yc < 2; yc++)
3212 {
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)
3220 {
3221 left = right = vec.x;
3222 top = bottom = vec.y;
3223 }
3224 else
3225 {
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;
3230 }
3231 }
3232 }
3233 left = left & -64;
3234 right = (right + 63) & -64;
3235 bottom = bottom & -64;
3236 top = (top + 63) & -64;
3237
3238 DPRINT("Transformed box: (%d,%d - %d,%d)\n", left, top, right, bottom);
3239 vec.x = ft_face->glyph->metrics.horiAdvance;
3240 vec.y = 0;
3241 FT_Vector_Transform(&vec, &transMat);
3242 gm.gmCellIncX = (vec.x+63) >> 6;
3243 gm.gmCellIncY = -((vec.y+63) >> 6);
3244 }
3245 gm.gmBlackBoxX = (right - left) >> 6;
3246 gm.gmBlackBoxY = (top - bottom) >> 6;
3247 gm.gmptGlyphOrigin.x = left >> 6;
3248 gm.gmptGlyphOrigin.y = top >> 6;
3249
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);
3254
3255 IntUnLockFreeType;
3256
3257
3258 if (iFormat == GGO_METRICS)
3259 {
3260 DPRINT("GGO_METRICS Exit!\n");
3261 *pgm = gm;
3262 return 1; /* FIXME */
3263 }
3264
3265 if (ft_face->glyph->format != ft_glyph_format_outline && iFormat != GGO_BITMAP)
3266 {
3267 DPRINT1("Loaded a bitmap\n");
3268 return GDI_ERROR;
3269 }
3270
3271 switch (iFormat)
3272 {
3273 case GGO_BITMAP:
3274 width = gm.gmBlackBoxX;
3275 height = gm.gmBlackBoxY;
3276 pitch = ((width + 31) >> 5) << 2;
3277 needed = pitch * height;
3278
3279 if (!pvBuf || !cjBuf) break;
3280 if (!needed) return GDI_ERROR; /* empty glyph */
3281 if (needed > cjBuf)
3282 return GDI_ERROR;
3283
3284 switch (ft_face->glyph->format)
3285 {
3286 case ft_glyph_format_bitmap:
3287 {
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 );
3291 while (h--)
3292 {
3293 RtlCopyMemory(dst, src, w);
3294 src += ft_face->glyph->bitmap.pitch;
3295 dst += pitch;
3296 }
3297 break;
3298 }
3299
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;
3306
3307 IntLockFreeType;
3308 if (needsTransform)
3309 {
3310 FT_Outline_Transform(&ft_face->glyph->outline, &transMat);
3311 }
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);
3316 IntUnLockFreeType;
3317 break;
3318
3319 default:
3320 DPRINT1("Loaded glyph format %x\n", ft_face->glyph->format);
3321 return GDI_ERROR;
3322 }
3323 break;
3324
3325 case GGO_GRAY2_BITMAP:
3326 case GGO_GRAY4_BITMAP:
3327 case GGO_GRAY8_BITMAP:
3328 {
3329 unsigned int mult, row, col;
3330 BYTE *start, *ptr;
3331
3332 width = gm.gmBlackBoxX;
3333 height = gm.gmBlackBoxY;
3334 pitch = (width + 3) / 4 * 4;
3335 needed = pitch * height;
3336
3337 if (!pvBuf || !cjBuf) break;
3338 if (!needed) return GDI_ERROR; /* empty glyph */
3339 if (needed > cjBuf)
3340 return GDI_ERROR;
3341
3342 switch (ft_face->glyph->format)
3343 {
3344 case ft_glyph_format_bitmap:
3345 {
3346 BYTE *src = ft_face->glyph->bitmap.buffer, *dst = pvBuf;
3347 INT h = min( height, ft_face->glyph->bitmap.rows );
3348 INT x;
3349 while (h--)
3350 {
3351 for (x = 0; (UINT)x < pitch; x++)
3352 {
3353 if (x < ft_face->glyph->bitmap.width)
3354 dst[x] = (src[x / 8] & (1 << ( (7 - (x % 8))))) ? 0xff : 0;
3355 else
3356 dst[x] = 0;
3357 }
3358 src += ft_face->glyph->bitmap.pitch;
3359 dst += pitch;
3360 }
3361 break;
3362 }
3363 case ft_glyph_format_outline:
3364 {
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;
3370
3371 IntLockFreeType;
3372 if (needsTransform)
3373 {
3374 FT_Outline_Transform(&ft_face->glyph->outline, &transMat);
3375 }
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);
3379 IntUnLockFreeType;
3380
3381 if (iFormat == GGO_GRAY2_BITMAP)
3382 mult = 4;
3383 else if (iFormat == GGO_GRAY4_BITMAP)
3384 mult = 16;
3385 else if (iFormat == GGO_GRAY8_BITMAP)
3386 mult = 64;
3387 else
3388 {
3389 return GDI_ERROR;
3390 }
3391
3392 start = pvBuf;
3393 for (row = 0; row < height; row++)
3394 {
3395 ptr = start;
3396 for (col = 0; col < width; col++, ptr++)
3397 {
3398 *ptr = (((int)*ptr) * mult + 128) / 256;
3399 }
3400 start += pitch;
3401 }
3402
3403 break;
3404 }
3405 default:
3406 DPRINT1("Loaded glyph format %x\n", ft_face->glyph->format);
3407 return GDI_ERROR;
3408 }
3409 }
3410
3411 case GGO_NATIVE:
3412 {
3413 FT_Outline *outline = &ft_face->glyph->outline;
3414
3415 if (cjBuf == 0) pvBuf = NULL; /* This is okay, need cjBuf to allocate. */
3416
3417 IntLockFreeType;
3418 if (needsTransform && pvBuf) FT_Outline_Transform(outline, &transMat);
3419
3420 needed = get_native_glyph_outline(outline, cjBuf, NULL);
3421
3422 if (!pvBuf || !cjBuf)
3423 {
3424 IntUnLockFreeType;
3425 break;
3426 }
3427 if (needed > cjBuf)
3428 {
3429 IntUnLockFreeType;
3430 return GDI_ERROR;
3431 }
3432 get_native_glyph_outline(outline, cjBuf, pvBuf);
3433 IntUnLockFreeType;
3434 break;
3435 }
3436 case GGO_BEZIER:
3437 {
3438 FT_Outline *outline = &ft_face->glyph->outline;
3439 if (cjBuf == 0) pvBuf = NULL;
3440
3441 if (needsTransform && pvBuf)
3442 {
3443 IntLockFreeType;
3444 FT_Outline_Transform(outline, &transMat);
3445 IntUnLockFreeType;
3446 }
3447 needed = get_bezier_glyph_outline(outline, cjBuf, NULL);
3448
3449 if (!pvBuf || !cjBuf)
3450 break;
3451 if (needed > cjBuf)
3452 return GDI_ERROR;
3453
3454 get_bezier_glyph_outline(outline, cjBuf, pvBuf);
3455 break;
3456 }
3457
3458 default:
3459 DPRINT1("Unsupported format %u\n", iFormat);
3460 return GDI_ERROR;
3461 }
3462
3463 DPRINT("ftGdiGetGlyphOutline END and needed %lu\n", needed);
3464 *pgm = gm;
3465 return needed;
3466 }
3467
3468 BOOL
3469 FASTCALL
3470 TextIntGetTextExtentPoint(PDC dc,
3471 PTEXTOBJ TextObj,
3472 LPCWSTR String,
3473 INT Count,
3474 ULONG MaxExtent,
3475 LPINT Fit,
3476 LPINT Dx,
3477 LPSIZE Size,
3478 FLONG fl)
3479 {
3480 PFONTGDI FontGDI;
3481 FT_Face face;
3482 FT_GlyphSlot glyph;
3483 FT_BitmapGlyph realglyph;
3484 INT error, glyph_index, i, previous;
3485 ULONGLONG TotalWidth = 0;
3486 BOOL use_kerning;
3487 FT_Render_Mode RenderMode;
3488 BOOLEAN Render;
3489 PMATRIX pmxWorldToDevice;
3490 LOGFONTW *plf;
3491 BOOL EmuBold, EmuItalic;
3492
3493 FontGDI = ObjToGDI(TextObj->Font, FONT);
3494
3495 face = FontGDI->SharedFace->Face;
3496 if (NULL != Fit)
3497 {
3498 *Fit = 0;
3499 }
3500
3501 IntLockFreeType;
3502
3503 TextIntUpdateSize(dc, TextObj, FontGDI, FALSE);
3504
3505 plf = &TextObj->logfont.elfEnumLogfontEx.elfLogFont;
3506 EmuBold = (plf->lfWeight >= FW_BOLD && FontGDI->OriginalWeight <= FW_NORMAL);
3507 EmuItalic = (plf->lfItalic && !FontGDI->OriginalItalic);
3508
3509 Render = IntIsFontRenderingEnabled();
3510 if (Render)
3511 RenderMode = IntGetFontRenderMode(plf);
3512 else
3513 RenderMode = FT_RENDER_MODE_MONO;
3514
3515
3516 /* Get the DC's world-to-device transformation matrix */
3517 pmxWorldToDevice = DC_pmxWorldToDevice(dc);
3518 FtSetCoordinateTransform(face, pmxWorldToDevice);
3519
3520 use_kerning = FT_HAS_KERNING(face);
3521 previous = 0;
3522
3523 for (i = 0; i < Count; i++)
3524 {
3525 if (fl & GTEF_INDICES)
3526 glyph_index = *String;
3527 else
3528 glyph_index = FT_Get_Char_Index(face, *String);
3529
3530 if (EmuBold || EmuItalic)
3531 realglyph = NULL;
3532 else
3533 realglyph = ftGdiGlyphCacheGet(face, glyph_index,
3534 plf->lfHeight, pmxWorldToDevice);
3535
3536 if (EmuBold || EmuItalic || !realglyph)
3537 {
3538 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
3539 if (error)
3540 {
3541 DPRINT1("WARNING: Failed to load and render glyph! [index: %d]\n", glyph_index);
3542 break;
3543 }
3544
3545 glyph = face->glyph;
3546 if (EmuBold || EmuItalic)
3547 {
3548 if (EmuBold)
3549 FT_GlyphSlot_Embolden(glyph);
3550 if (EmuItalic)
3551 FT_GlyphSlot_Oblique(glyph);
3552 realglyph = ftGdiGlyphSet(face, glyph, RenderMode);
3553 }
3554 else
3555 {
3556 realglyph = ftGdiGlyphCacheSet(face,
3557 glyph_index,
3558 plf->lfHeight,
3559 pmxWorldToDevice,
3560 glyph,
3561 RenderMode);
3562 }
3563
3564 if (!realglyph)
3565 {
3566 DPRINT1("Failed to render glyph! [index: %d]\n", glyph_index);
3567 break;
3568 }
3569 }
3570
3571 /* Retrieve kerning distance */
3572 if (use_kerning && previous && glyph_index)
3573 {
3574 FT_Vector delta;
3575 FT_Get_Kerning(face, previous, glyph_index, 0, &delta);
3576 TotalWidth += delta.x;
3577 }
3578
3579 TotalWidth += realglyph->root.advance.x >> 10;
3580
3581 if (((TotalWidth + 32) >> 6) <= MaxExtent && NULL != Fit)
3582 {
3583 *Fit = i + 1;
3584 }
3585 if (NULL != Dx)
3586 {
3587 Dx[i] = (TotalWidth + 32) >> 6;
3588 }
3589
3590 if (EmuBold || EmuItalic)
3591 {
3592 FT_Done_Glyph((FT_Glyph)realglyph);
3593 realglyph = NULL;
3594 }
3595
3596 previous = glyph_index;
3597 String++;
3598 }
3599 IntUnLockFreeType;
3600
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);
3606
3607 return TRUE;
3608 }
3609
3610
3611 INT
3612 FASTCALL
3613 ftGdiGetTextCharsetInfo(
3614 PDC Dc,
3615 LPFONTSIGNATURE lpSig,
3616 DWORD dwFlags)
3617 {
3618 PDC_ATTR pdcattr;
3619 UINT Ret = DEFAULT_CHARSET;
3620 INT i;
3621 HFONT hFont;
3622 PTEXTOBJ TextObj;
3623 PFONTGDI FontGdi;
3624 FONTSIGNATURE fs;
3625 TT_OS2 *pOS2;
3626 FT_Face Face;
3627 CHARSETINFO csi;