OpenTTD Source 20251213-master-g1091fa6071
FontCache Class Referenceabstract

Font cache for basic fonts. More...

#include <fontcache.h>

Inheritance diagram for FontCache:
MockFontCache SpriteFontCache TrueTypeFontCache CoreTextFontCache FreeTypeFontCache Win32FontCache

Data Structures

struct  FontMetrics
 

Public Member Functions

FontSize GetSize () const
 Get the FontSize of the font.
 
FontIndex GetIndex () const
 
FontLoadReason GetFontLoadReason () const
 
int GetHeight () const
 Get the height of the font.
 
int GetAscender () const
 Get the ascender value of the font.
 
int GetDescender () const
 Get the descender value of the font.
 
virtual int GetFontSize () const
 Get the nominal font size of the font.
 
virtual void ClearFontCache ()=0
 Clear the font cache.
 
virtual const SpriteGetGlyph (GlyphID key)=0
 Get the glyph (sprite) of the given key.
 
virtual uint GetGlyphWidth (GlyphID key)=0
 Get the width of the glyph with the given key.
 
virtual bool GetDrawGlyphShadow ()=0
 Do we need to draw a glyph shadow?
 
virtual GlyphID MapCharToGlyph (char32_t key)=0
 Map a character into a glyph.
 
virtual const void * GetOSHandle ()
 Get the native OS font handle, if there is one.
 
virtual std::string GetFontName ()=0
 Get the name of this font.
 
virtual int GetGlyphYOffset ()
 
virtual bool IsBuiltInFont ()=0
 Is this a built-in sprite font?
 

Static Public Member Functions

static void InitializeFontCaches ()
 Initialise font caches with the base sprite font cache for all sizes.
 
static void UninitializeFontCaches ()
 Free everything allocated w.r.t.
 
static void LoadFontCaches (FontSizes fontsizes)
 (Re)initialize the font cache related things, i.e.
 
static void ClearFontCaches (FontSizes fontsizes)
 Clear cached information for the specified font caches.
 
static int GetDefaultFontHeight (FontSize fs)
 
static int GetFontBaseline (FontSize fs)
 
static void AddFallback (FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, std::span< const std::byte > os_data={})
 Add a fallback font, with optional OS-specific handle.
 
template<typename T >
static void AddFallbackWithHandle (FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
 Add a fallback font, with OS-specific handle.
 
static std::span< const std::unique_ptr< FontCache > > Get ()
 Get span of all FontCaches.
 
static FontCacheGet (FontIndex font_index)
 Get the font cache of a given font size.
 
static int GetCharacterHeight (FontSize fs)
 
static void UpdateCharacterHeight (FontSize fs)
 
static FontIndex GetDefaultFontIndex (FontSize fs)
 
static class FontCacheGetDefaultFontCache (FontSize fs)
 
static FontIndex GetFontIndexForCharacter (FontSize fs, char32_t c)
 

Static Public Attributes

static const int DEFAULT_FONT_HEIGHT [FS_END] = {10, 6, 18, 10}
 Default unscaled font heights.
 
static const int DEFAULT_FONT_ASCENDER [FS_END] = {8, 6, 15, 8}
 Default unscaled font ascenders.
 

Protected Types

using FontCaches = std::vector< std::unique_ptr< FontCache > >
 

Protected Member Functions

 FontCache (FontSize fs)
 

Static Protected Member Functions

static void Register (std::unique_ptr< FontCache > &&fc, FontLoadReason load_reason)
 
static void LoadDefaultFonts (FontSize fs)
 
static void LoadFallbackFonts (FontSize fs, FontLoadReason load_reason)
 

Protected Attributes

const FontSize fs
 The size of the font.
 
FontIndex font_index
 The index of the font.
 
FontLoadReason load_reason
 Reason why the font is loaded.
 
int height = 0
 The height of the font.
 
int ascender = 0
 The ascender value of the font.
 
int descender = 0
 The descender value of the font.
 

Static Protected Attributes

static FontCaches caches
 
static std::array< FontMetrics, FS_END > metrics {}
 
static std::array< FontIndex, FS_END > default_font_index {}
 

Detailed Description

Font cache for basic fonts.

Definition at line 32 of file fontcache.h.

Member Typedef Documentation

◆ FontCaches

using FontCache::FontCaches = std::vector<std::unique_ptr<FontCache> >
protected

Definition at line 34 of file fontcache.h.

Constructor & Destructor Documentation

◆ FontCache()

FontCache::FontCache ( FontSize  fs)
inlineprotected

Definition at line 52 of file fontcache.h.

Member Function Documentation

◆ AddFallback()

void FontCache::AddFallback ( FontSizes  fontsizes,
FontLoadReason  load_reason,
std::string_view  name,
std::span< const std::byte >  os_data = {} 
)
static

Add a fallback font, with optional OS-specific handle.

Parameters
fontsizesFontsizes to add fallback to.
nameName of font to add.
handleOS-specific handle or data of font.

Definition at line 256 of file fontcache.cpp.

References fs, GetFontCacheSubSetting(), and load_reason.

Referenced by AddFallbackWithHandle().

◆ AddFallbackWithHandle()

template<typename T >
static void FontCache::AddFallbackWithHandle ( FontSizes  fontsizes,
FontLoadReason  load_reason,
std::string_view  name,
T &  handle 
)
inlinestatic

Add a fallback font, with OS-specific handle.

Parameters
fontsizesFontsizes to add fallback to.
nameName of font to add.
handleOS-specific handle or data of font.

Definition at line 86 of file fontcache.h.

References AddFallback(), and load_reason.

◆ ClearFontCache()

virtual void FontCache::ClearFontCache ( )
pure virtual

◆ ClearFontCaches()

void FontCache::ClearFontCaches ( FontSizes  fontsizes)
static

Clear cached information for the specified font caches.

Parameters
fontsizesFont sizes to clear.

Definition at line 345 of file fontcache.cpp.

References fs, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by AdjustGUIZoom(), CheckBlitter(), GfxLoadSprites(), LoadStringWidthTable(), and GameOptionsWindow::OnClick().

◆ Get() [1/2]

static std::span< const std::unique_ptr< FontCache > > FontCache::Get ( )
inlinestatic

Get span of all FontCaches.

Returns
Span of all FontCaches.

Definition at line 177 of file fontcache.h.

Referenced by CustomFontGetWidth(), GetGlyph(), GetGlyphWidth(), and SurveyFont().

◆ Get() [2/2]

static FontCache * FontCache::Get ( FontIndex  font_index)
inlinestatic

Get the font cache of a given font size.

Parameters
fsThe font size to look up.
Returns
The font cache.

Definition at line 187 of file fontcache.h.

References font_index.

◆ GetAscender()

int FontCache::GetAscender ( ) const
inline

Get the ascender value of the font.

Returns
The ascender value of the font.

Definition at line 112 of file fontcache.h.

References ascender.

◆ GetCharacterHeight()

static int FontCache::GetCharacterHeight ( FontSize  fs)
inlinestatic

Definition at line 193 of file fontcache.h.

◆ GetDefaultFontCache()

static class FontCache * FontCache::GetDefaultFontCache ( FontSize  fs)
inlinestatic

Definition at line 205 of file fontcache.h.

◆ GetDefaultFontHeight()

int FontCache::GetDefaultFontHeight ( FontSize  fs)
static

Definition at line 64 of file fontcache.cpp.

◆ GetDefaultFontIndex()

static FontIndex FontCache::GetDefaultFontIndex ( FontSize  fs)
inlinestatic

Definition at line 200 of file fontcache.h.

◆ GetDescender()

int FontCache::GetDescender ( ) const
inline

Get the descender value of the font.

Returns
The descender value of the font.

Definition at line 118 of file fontcache.h.

References descender.

◆ GetDrawGlyphShadow()

virtual bool FontCache::GetDrawGlyphShadow ( )
pure virtual

Do we need to draw a glyph shadow?

Returns
True if it has to be done, otherwise false.

Implemented in SpriteFontCache, TrueTypeFontCache, and MockFontCache.

Referenced by DrawLayoutLine().

◆ GetFontBaseline()

static int FontCache::GetFontBaseline ( FontSize  fs)
inlinestatic

Definition at line 72 of file fontcache.h.

◆ GetFontIndexForCharacter()

static FontIndex FontCache::GetFontIndexForCharacter ( FontSize  fs,
char32_t  c 
)
inlinestatic

Definition at line 212 of file fontcache.h.

◆ GetFontLoadReason()

FontLoadReason FontCache::GetFontLoadReason ( ) const
inline

Definition at line 100 of file fontcache.h.

◆ GetFontName()

virtual std::string FontCache::GetFontName ( )
pure virtual

Get the name of this font.

Returns
The name of the font.

Implemented in FreeTypeFontCache, SpriteFontCache, CoreTextFontCache, Win32FontCache, and MockFontCache.

Referenced by CoreTextParagraphLayoutFactory::GetParagraphLayout(), and HFontFromFont().

◆ GetFontSize()

virtual int FontCache::GetFontSize ( ) const
inlinevirtual

Get the nominal font size of the font.

Returns
The nominal font size.

Reimplemented in TrueTypeFontCache.

Definition at line 124 of file fontcache.h.

References height.

Referenced by CoreTextParagraphLayoutFactory::GetParagraphLayout().

◆ GetGlyph()

virtual const Sprite * FontCache::GetGlyph ( GlyphID  key)
pure virtual

Get the glyph (sprite) of the given key.

Parameters
keyThe key to look up.
Returns
The sprite.

Implemented in SpriteFontCache, TrueTypeFontCache, and MockFontCache.

Referenced by DrawLayoutLine(), and GetGlyph().

◆ GetGlyphWidth()

virtual uint FontCache::GetGlyphWidth ( GlyphID  key)
pure virtual

Get the width of the glyph with the given key.

Parameters
keyThe key to look up.
Returns
The width.

Implemented in SpriteFontCache, TrueTypeFontCache, and MockFontCache.

Referenced by UniscribeRun::FallbackShape(), ICURun::FallbackShape(), FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(), GetGlyphWidth(), and UniscribeShapeRun().

◆ GetGlyphYOffset()

int FontCache::GetGlyphYOffset ( )
virtual

Definition at line 93 of file fontcache.cpp.

◆ GetHeight()

int FontCache::GetHeight ( ) const
inline

Get the height of the font.

Returns
The height of the font.

Definition at line 106 of file fontcache.h.

References height.

Referenced by HFontFromFont().

◆ GetIndex()

FontIndex FontCache::GetIndex ( ) const
inline

Definition at line 98 of file fontcache.h.

◆ GetOSHandle()

virtual const void * FontCache::GetOSHandle ( )
inlinevirtual

Get the native OS font handle, if there is one.

Returns
Opaque OS font handle.

Reimplemented in FreeTypeFontCache, CoreTextFontCache, and Win32FontCache.

Definition at line 160 of file fontcache.h.

Referenced by CoreTextParagraphLayoutFactory::GetParagraphLayout(), HFontFromFont(), and ICURun::Shape().

◆ GetSize()

FontSize FontCache::GetSize ( ) const
inline

Get the FontSize of the font.

Returns
The FontSize.

Definition at line 96 of file fontcache.h.

References fs.

Referenced by DrawLayoutLine(), and FallbackParagraphLayout::NextLine().

◆ InitializeFontCaches()

void FontCache::InitializeFontCaches ( )
static

Initialise font caches with the base sprite font cache for all sizes.

Definition at line 117 of file fontcache.cpp.

References FONTSIZES_ALL, and fs.

◆ IsBuiltInFont()

virtual bool FontCache::IsBuiltInFont ( )
pure virtual

◆ LoadDefaultFonts()

void FontCache::LoadDefaultFonts ( FontSize  fs)
staticprotected

Definition at line 263 of file fontcache.cpp.

◆ LoadFallbackFonts()

void FontCache::LoadFallbackFonts ( FontSize  fs,
FontLoadReason  load_reason 
)
staticprotected

Definition at line 273 of file fontcache.cpp.

◆ LoadFontCaches()

void FontCache::LoadFontCaches ( FontSizes  fontsizes)
static

◆ MapCharToGlyph()

virtual GlyphID FontCache::MapCharToGlyph ( char32_t  key)
pure virtual

Map a character into a glyph.

Parameters
keyThe character.
Returns
The glyph ID used to draw the character.

Implemented in FreeTypeFontCache, SpriteFontCache, CoreTextFontCache, Win32FontCache, and MockFontCache.

Referenced by UniscribeRun::FallbackShape(), ICURun::FallbackShape(), FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(), GetGlyph(), and GetGlyphWidth().

◆ Register()

void FontCache::Register ( std::unique_ptr< FontCache > &&  fc,
FontLoadReason  load_reason 
)
staticprotected

Definition at line 231 of file fontcache.cpp.

◆ UninitializeFontCaches()

void FontCache::UninitializeFontCaches ( )
static

Free everything allocated w.r.t.

fonts.

Definition at line 361 of file fontcache.cpp.

Referenced by ShutdownGame().

◆ UpdateCharacterHeight()

void FontCache::UpdateCharacterHeight ( FontSize  fs)
static

Definition at line 69 of file fontcache.cpp.

Field Documentation

◆ ascender

int FontCache::ascender = 0
protected

The ascender value of the font.

Definition at line 49 of file fontcache.h.

Referenced by GetAscender().

◆ caches

FontCache::FontCaches FontCache::caches
staticprotected

Definition at line 35 of file fontcache.h.

◆ DEFAULT_FONT_ASCENDER

const int FontCache::DEFAULT_FONT_ASCENDER = {8, 6, 15, 8}
static

Default unscaled font ascenders.

Default unscaled ascenders for the different sizes of fonts.

Definition at line 29 of file fontcache.h.

◆ DEFAULT_FONT_HEIGHT

const int FontCache::DEFAULT_FONT_HEIGHT = {10, 6, 18, 10}
static

Default unscaled font heights.

Default unscaled heights for the different sizes of fonts.

Definition at line 27 of file fontcache.h.

◆ default_font_index

std::array< FontIndex, FS_END > FontCache::default_font_index {}
staticprotected

Definition at line 112 of file fontcache.h.

◆ descender

int FontCache::descender = 0
protected

The descender value of the font.

Definition at line 50 of file fontcache.h.

Referenced by GetDescender().

◆ font_index

FontIndex FontCache::font_index
protected

The index of the font.

Definition at line 46 of file fontcache.h.

Referenced by Get(), GetGlyph(), and GetGlyphWidth().

◆ fs

◆ height

int FontCache::height = 0
protected

The height of the font.

Definition at line 48 of file fontcache.h.

Referenced by GetFontSize(), MockFontCache::GetGlyphWidth(), and GetHeight().

◆ load_reason

FontLoadReason FontCache::load_reason
protected

Reason why the font is loaded.

Definition at line 47 of file fontcache.h.

Referenced by AddFallback(), and AddFallbackWithHandle().

◆ metrics

std::array< FontCache::FontMetrics, FS_END > FontCache::metrics {}
staticprotected

Definition at line 111 of file fontcache.h.


The documentation for this class was generated from the following files: