UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FontCacheFreeType.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Fonts/FontCache.h"
9#include "Misc/Optional.h"
10
11#ifndef WITH_FREETYPE
12 #define WITH_FREETYPE 0
13#endif // WITH_FREETYPE
14
15
16#ifndef WITH_FREETYPE_V210
17 #define WITH_FREETYPE_V210 0
18#endif // WITH_FREETYPE_V210
19
20
21#if PLATFORM_COMPILER_HAS_GENERIC_KEYWORD
22 #define generic __identifier(generic)
23#endif //PLATFORM_COMPILER_HAS_GENERIC_KEYWORD
24
25
26#if WITH_FREETYPE
28 #include "ft2build.h"
29
30 // FreeType style include
31 #include FT_FREETYPE_H
32#if WITH_FREETYPE_V210
33 #include FT_DRIVER_H
34#endif // WITH_FREETYPE_V210
35 #include FT_GLYPH_H
36 #include FT_MODULE_H
37 #include FT_BITMAP_H
38 #include FT_ADVANCES_H
39 #include FT_STROKER_H
40 #include FT_SIZES_H
42#endif // WITH_FREETYPE
43
44
45#if PLATFORM_COMPILER_HAS_GENERIC_KEYWORD
46 #undef generic
47#endif //PLATFORM_COMPILER_HAS_GENERIC_KEYWORD
48
49namespace FreeTypeUtils
50{
51
52#if WITH_FREETYPE
53
58
63
67
71
76
83
88
89
94
98void ApplySizeAndScale(FT_Face InFace, const float InFontSize, const float InFontScale);
99
104
110
116
122
128
134
140
145float GetBitmapRenderScale(FT_Face InFace);
146
147#endif // WITH_FREETYPE
148
150template <typename TRetType, typename TParamType>
155
157template <typename TRetType, typename TParamType>
159{
160 return static_cast<TRetType>(FMath::RoundToInt(InValue / 64.0f));
161}
162
164template <typename TRetType, typename TParamType>
169
171template <typename TRetType, typename TParamType>
176
178template <typename TRetType, typename TParamType>
183
185template <typename TRetType, typename TParamType>
190
191}
192
193
199{
200public:
203
204#if WITH_FREETYPE
205 FORCEINLINE FT_Library GetLibrary() const
206 {
207 return FTLibrary;
208 }
209#endif // WITH_FREETYPE
210
211private:
212 // Non-copyable
214 FFreeTypeLibrary& operator=(const FFreeTypeLibrary&);
215
216#if WITH_FREETYPE
217 FT_Library FTLibrary;
219#endif // WITH_FREETYPE
220};
221
222
228{
229public:
234
236 {
237#if WITH_FREETYPE
238 return FTFace != nullptr;
239#else
240 return false;
241#endif // WITH_FREETYPE
242 }
243
245 {
246#if WITH_FREETYPE
247 return bPendingAsyncLoad;
248#else
249 return false;
250#endif // WITH_FREETYPE
251 }
252
254 {
255#if WITH_FREETYPE
256 return FreeTypeUtils::IsFaceEligibleForSdf(FTFace);
257#else
258 return false;
259#endif // WITH_FREETYPE
260 }
261
262#if WITH_FREETYPE
263 FORCEINLINE FT_Face GetFace() const
264 {
265#if WITH_ATLAS_DEBUGGING
267#endif
268 return FTFace;
269 }
270
271 FORCEINLINE FT_Pos GetHeight() const
272 {
273 return FreeTypeUtils::GetHeight(FTFace, LayoutMethod);
274 }
275
276 FORCEINLINE FT_Pos GetScaledHeight(bool bAllowOverride) const
277 {
278 if (bAllowOverride && (IsAscentOverridden || IsDescentOverridden))
279 return GetAscender(true) - GetDescender(true);
280 return FreeTypeUtils::GetScaledHeight(FTFace, LayoutMethod);
281 }
282
283 FORCEINLINE FT_Pos GetAscender(bool bAllowOverride) const
284 {
285 if (bAllowOverride && IsAscentOverridden)
286 {
288 return (ScaledAscender + 0b111111) & ~0b111111; //(26.6 fixed point ceil). Using ceiling of scaled ascend, as recommended by Freetype, to avoid grid fitting/hinting issues.
289 }
290 return FreeTypeUtils::GetAscender(FTFace, LayoutMethod);
291 }
292
293 FORCEINLINE FT_Pos GetDescender(bool bAllowOverride) const
294 {
295 if (bAllowOverride && IsDescentOverridden)
296 {
298 return ScaledDescender & ~0b111111; //(26.6 fixed point floor). Using floor of scaled descend, as recommended by Freetype, to avoid grid fitting/hinting issues.
299 }
300 return FreeTypeUtils::GetDescender(FTFace, LayoutMethod);
301 }
302
303 FORCEINLINE float GetBitmapAtlasScale() const
304 {
305 return FreeTypeUtils::GetBitmapAtlasScale(FTFace);
306 }
307
308 FORCEINLINE float GetBitmapRenderScale() const
309 {
310 return FreeTypeUtils::GetBitmapRenderScale(FTFace);
311 }
312#endif // WITH_FREETYPE
313
315 {
316 return Attributes;
317 }
318
320 {
321 return LayoutMethod;
322 }
323
328
334
336 {
337#if WITH_FREETYPE
339 AscentOverrideValue = FreeTypeUtils::ConvertPixelTo26Dot6<FT_F26Dot6>(Value);
340#endif //WITH_FREETYPE
341 }
342
344 {
345#if WITH_FREETYPE
347 DescentOverrideValue = FreeTypeUtils::ConvertPixelTo26Dot6<FT_F26Dot6>(Value);
348#endif //WITH_FREETYPE
349 }
350
351 void FailAsyncLoad();
353
361
362private:
363
364#if WITH_FREETYPE
365 void ParseAttributes();
366#endif // WITH_FREETYPE
367
368 // Non-copyable
370 FFreeTypeFace& operator=(const FFreeTypeFace&);
371
372#if WITH_FREETYPE
375
376 bool bPendingAsyncLoad = false;
377
379 struct FFTStreamHandler
380 {
382 FFTStreamHandler(const FString& InFilename);
384 static void CloseFile(FT_Stream InStream);
385 static unsigned long ReadData(FT_Stream InStream, unsigned long InOffset, unsigned char* InBuffer, unsigned long InCount);
386
387 IFileHandle* FileHandle;
389 };
390
394
395 bool IsAscentOverridden = false;
396 bool IsDescentOverridden = false;
399
400#if WITH_ATLAS_DEBUGGING
401 ESlateTextureAtlasThreadId OwnerThread;
402#endif
403#endif // WITH_FREETYPE
404
405 TSet<FName> Attributes;
406
407 EFontLayoutMethod LayoutMethod;
408};
409
410
418{
419public:
420#if WITH_FREETYPE
422
423 struct FCachedGlyphData
424 {
425 FT_Short Height;
429 };
430
431 bool FindOrCache(const uint32 InGlyphIndex, FCachedGlyphData& OutCachedGlyphData);
432#endif // WITH_FREETYPE
433
434private:
435#if WITH_FREETYPE
437 const int32 LoadFlags;
438 const uint32 FontRenderSize;
440#endif // WITH_FREETYPE
441};
442
443
448{
449public:
450#if WITH_FREETYPE
453
454 bool FindOrCache(const uint32 InGlyphIndex, FT_Fixed& OutCachedAdvance);
455#endif // WITH_FREETYPE
456
458
459private:
460#if WITH_FREETYPE
462 const int32 LoadFlags;
463 const uint32 FontRenderSize;
465#endif // WITH_FREETYPE
466};
467
468
473{
474public:
475#if WITH_FREETYPE
477
485 bool FindOrCache(const uint32 InFirstGlyphIndex, const uint32 InSecondGlyphIndex, FT_Vector& OutKerning);
486#endif // WITH_FREETYPE
487
488private:
489#if WITH_FREETYPE
490 struct FKerningPair
491 {
495 {
496 }
497
498 FORCEINLINE bool operator==(const FKerningPair& Other) const
499 {
500 return FirstGlyphIndex == Other.FirstGlyphIndex
501 && SecondGlyphIndex == Other.SecondGlyphIndex;
502 }
503
504 FORCEINLINE bool operator!=(const FKerningPair& Other) const
505 {
506 return !(*this == Other);
507 }
508
509 friend inline uint32 GetTypeHash(const FKerningPair& Key)
510 {
511 uint32 KeyHash = 0;
512 KeyHash = HashCombine(KeyHash, Key.FirstGlyphIndex);
513 KeyHash = HashCombine(KeyHash, Key.SecondGlyphIndex);
514 return KeyHash;
515 }
516
519 };
520
522 const int32 KerningFlags;
523 const uint32 FontRenderSize;
525#endif // WITH_FREETYPE
526};
527
528
533{
534public:
536#if WITH_FREETYPE
542
548
554#endif // WITH_FREETYPE
555
556 void FlushCache();
557
558private:
559#if WITH_FREETYPE
560 /* Shared font key to look up internal caches; flag meaning changes but the internal type is the same. */
561 class FFontKey
562 {
563 public:
565 : Face(InFace)
566 , Flags(InFlags)
567 , FontRenderSize(InFontRenderSize)
568 , KeyHash(0)
569 {
570 KeyHash = GetTypeHash(Face);
571 KeyHash = HashCombine(KeyHash, GetTypeHash(Flags));
572 KeyHash = HashCombine(KeyHash, GetTypeHash(FontRenderSize));
573 }
574
575 FORCEINLINE bool operator==(const FFontKey& Other) const
576 {
577 return Face == Other.Face
578 && Flags == Other.Flags
579 && FontRenderSize == Other.FontRenderSize;
580 }
581
582 FORCEINLINE bool operator!=(const FFontKey& Other) const
583 {
584 return !(*this == Other);
585 }
586
587 friend inline uint32 GetTypeHash(const FFontKey& Key)
588 {
589 return Key.KeyHash;
590 }
591
592 private:
594 const int32 Flags;
595 const uint32 FontRenderSize;
596 uint32 KeyHash;
597 };
598
603#endif // WITH_FREETYPE
604};
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define check(expr)
Definition AssertionMacros.h:314
EFontLayoutMethod
Definition CompositeFont.h:51
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define THIRD_PARTY_INCLUDES_START
Definition GenericPlatformCompilerPreSetup.h:63
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
ESlateTextureAtlasThreadId GetCurrentSlateTextureAtlasThreadId()
Definition TextureAtlas.cpp:18
ESlateTextureAtlasThreadId
Definition TextureAtlas.h:44
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition FontCacheFreeType.h:448
Definition FontCacheFreeType.h:533
FFreeTypeCacheDirectory()
Definition FontCacheFreeType.cpp:950
void FlushCache()
Definition FontCacheFreeType.cpp:958
Definition FontCacheFreeType.h:228
const UE::Slate::FPreprocessedFontGeometry * GetPreprocessedFontGeometry() const
Definition FontCacheFreeType.cpp:424
FORCEINLINE EFontLayoutMethod GetLayoutMethod() const
Definition FontCacheFreeType.h:319
FORCEINLINE bool IsFaceValid() const
Definition FontCacheFreeType.h:235
void OverrideDescent(bool InOverride, int32 Value=0)
Definition FontCacheFreeType.h:343
FORCEINLINE bool SupportsSdf() const
Definition FontCacheFreeType.h:253
SIZE_T GetAllocatedMemorySize() const
Definition FontCacheFreeType.cpp:413
void CompleteAsyncLoad(const FFreeTypeLibrary *InFTLibrary, FFontFaceDataConstRef InMemory, const int32 InFaceIndex)
Definition FontCacheFreeType.cpp:447
FORCEINLINE bool IsFaceLoading() const
Definition FontCacheFreeType.h:244
static TArray< FString > GetAvailableSubFaces(const FFreeTypeLibrary *InFTLibrary, FFontFaceDataConstRef InMemory)
Definition FontCacheFreeType.cpp:541
void OverrideAscent(bool InOverride, int32 Value=0)
Definition FontCacheFreeType.h:335
~FFreeTypeFace()
Definition FontCacheFreeType.cpp:520
void FailAsyncLoad()
Definition FontCacheFreeType.cpp:439
FORCEINLINE const TSet< FName > & GetAttributes() const
Definition FontCacheFreeType.h:314
Definition FontCacheFreeType.h:418
Definition FontCacheFreeType.h:473
Definition FontCacheFreeType.h:199
FFreeTypeLibrary()
Definition FontCacheFreeType.cpp:337
~FFreeTypeLibrary()
Definition FontCacheFreeType.cpp:380
Definition GenericPlatformFile.h:117
Definition Array.h:670
Definition EnableIf.h:20
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition PreprocessedFontGeometry.h:198
Definition FontCacheFreeType.cpp:74
FORCEINLINE TEnableIf< TIsIntegral< TParamType >::Value, TRetType >::Type ConvertPixelTo26Dot6(TParamType InValue)
Definition FontCacheFreeType.h:165
FORCEINLINE TEnableIf< TIsIntegral< TParamType >::Value, TRetType >::Type Convert26Dot6ToRoundedPixel(TParamType InValue)
Definition FontCacheFreeType.h:151
FORCEINLINE TEnableIf< TIsIntegral< TParamType >::Value, TRetType >::Type ConvertPixelTo16Dot16(TParamType InValue)
Definition FontCacheFreeType.h:179
Definition Voronoi.cpp:10