19using FontIndex = uint8_t;
21static const FontIndex INVALID_FONT_INDEX = std::numeric_limits<FontIndex>::max();
23enum class FontLoadReason : uint8_t {
34 using FontCaches = std::vector<std::unique_ptr<FontCache>>;
35 static FontCaches caches;
42 static std::array<FontMetrics, FS_END> metrics;
43 static std::array<FontIndex, FS_END> default_font_index;
53 static void Register(std::unique_ptr<FontCache> &&fc, FontLoadReason
load_reason);
74 return FontCache::metrics[
fs].baseline;
88 auto os_data = std::as_bytes(std::span(&handle, 1));
98 inline FontIndex GetIndex()
const {
return this->
font_index; }
100 inline FontLoadReason GetFontLoadReason()
const {
return this->
load_reason; }
171 virtual int GetGlyphYOffset();
177 static inline std::span<const std::unique_ptr<FontCache>>
Get()
179 return FontCache::caches;
189 assert(
font_index < FontCache::caches.size());
193 static inline int GetCharacterHeight(
FontSize fs)
195 return FontCache::metrics[
fs].height;
198 static void UpdateCharacterHeight(
FontSize fs);
200 static inline FontIndex GetDefaultFontIndex(
FontSize fs)
202 return FontCache::default_font_index[
fs];
207 FontIndex index = FontCache::GetDefaultFontIndex(
fs);
212 static inline FontIndex GetFontIndexForCharacter(
FontSize fs,
char32_t c)
214 for (
auto it = std::rbegin(FontCache::caches); it != std::rend(FontCache::caches); ++it) {
216 if (fc ==
nullptr)
continue;
219 return std::distance(std::begin(FontCache::caches), std::next(it).base());
221 return INVALID_FONT_INDEX;
233 FontIndex
font_index = FontCache::GetFontIndexForCharacter(size, key);
235 if (fc ==
nullptr)
return nullptr;
242 FontIndex
font_index = FontCache::GetFontIndexForCharacter(size, key);
244 if (fc ==
nullptr)
return 0;
254 FontLoadReason load_reason = FontLoadReason::LanguageFallback;
256 std::vector<std::byte> os_handle;
259 std::vector<FontCacheFallback> fallback_fonts;
283 default: NOT_REACHED();
294bool GetFontAAState();
295void SetFont(
FontSize fontsize,
const std::string &font, uint size);
316 virtual std::unique_ptr<FontCache> LoadFont(
FontSize fs,
FontType fonttype,
bool search,
const std::string &font_name, std::span<const std::byte> os_handle)
const = 0;
322 static std::unique_ptr<FontCache>
LoadFont(
FontSize fs,
FontType fonttype,
bool search,
const std::string &font_name, std::span<const std::byte> os_handle);
Font cache for basic fonts.
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
virtual void ClearFontCache()=0
Clear the font cache.
static void UninitializeFontCaches()
Free everything allocated w.r.t.
static std::span< const std::unique_ptr< FontCache > > Get()
Get span of all FontCaches.
int height
The height of the font.
static void AddFallbackWithHandle(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
Add a fallback font, with OS-specific handle.
virtual int GetFontSize() const
Get the nominal font size of the font.
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
static FontCache * Get(FontIndex font_index)
Get the font cache of a given font size.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
const FontSize fs
The size of the font.
FontLoadReason load_reason
Reason why the font is loaded.
int GetAscender() const
Get the ascender value of the font.
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
virtual GlyphID MapCharToGlyph(char32_t key)=0
Map a character into a glyph.
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
FontIndex font_index
The index of the font.
int descender
The descender value of the font.
FontSize GetSize() const
Get the FontSize of the font.
int GetDescender() const
Get the descender value of the font.
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
static void InitializeFontCaches()
Initialise font caches with the base sprite font cache for all sizes.
int ascender
The ascender value of the font.
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.
static std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, std::span< const std::byte > os_handle)
Try loading a font with any fontcache factory.
static bool FindFallbackFont(const std::string &language_isocode, FontSizes fontsizes, class MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
A searcher for missing glyphs.
The ProviderManager manages a single Provider-type.
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
Set the SpriteID for a unicode character.
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
FontType
Different types of font that can be loaded.
@ TrueType
Scalable TrueType fonts.
@ Sprite
Bitmap sprites from GRF files.
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
uint32_t GlyphID
Glyphs are characters from a font.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
FontSize
Available font sizes.
@ FS_MONO
Index of the monospaced font in the font tables.
@ FS_SMALL
Index of the small font in the font tables.
@ FS_NORMAL
Index of the normal font in the font tables.
@ FS_LARGE
Index of the large font in the font tables.
Definition of the ProviderManager.
Types related to the sprite cache.
Settings for the four different fonts.
FontCacheSubSetting large
The largest font; mostly used for newspapers.
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
bool prefer_default
Prefer OpenTTD's default font over autodetected fallback fonts.
FontCacheSubSetting medium
The normal font size.
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
bool global_aa
Whether to anti alias all font sizes.
Settings for a single font.
std::string font
The name of the font, or path to the font.
uint size
The (requested) size of the font.
Data structure describing a sprite.