42 auto fc = provider->LoadFont(fs, fonttype, search, font_name, os_handle);
43 if (fc !=
nullptr)
return fc;
61 [&](
auto *provider) {
return provider->FindFallbackFont(language_isocode, fontsizes, callback); });
64int FontCache::GetDefaultFontHeight(
FontSize fs)
69 void FontCache::UpdateCharacterHeight(
FontSize fs)
71 FontMetrics &metrics = FontCache::metrics[
fs];
76 for (
const auto &fc :
FontCache::caches) {
77 if (fc ==
nullptr || fc->fs !=
fs)
continue;
78 if (fc->load_reason == FontLoadReason::MissingFallback)
continue;
93int FontCache::GetGlyphYOffset()
95 return FontCache::GetFontBaseline(this->fs) - this->
ascender;
105 uint height = FontCache::GetCharacterHeight(size);
110 FontCache::FontCaches FontCache::caches;
111 std::array<FontCache::FontMetrics, FS_END> FontCache::metrics{};
112 std::array<FontIndex, FS_END> FontCache::default_font_index{};
120 UpdateCharacterHeight(
fs);
133void SetFont(
FontSize fontsize,
const std::string &font, uint size)
136 bool changed =
false;
138 if (setting->
font != font) {
139 setting->
font = font;
143 if (setting->
size != size) {
144 setting->
size = size;
148 if (!changed)
return;
157 FontCache::UpdateCharacterHeight(fontsize);
171 return setting.
font.empty();
182 return IsDefaultFont(setting) ? FontCache::GetDefaultFontHeight(fs) : setting.
size;
185#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
194 case FS_NORMAL:
return "OpenTTD-Sans.ttf";
195 case FS_SMALL:
return "OpenTTD-Small.ttf";
196 case FS_LARGE:
return "OpenTTD-Serif.ttf";
197 case FS_MONO:
return "OpenTTD-Mono.ttf";
198 default: NOT_REACHED();
210#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
231 void FontCache::Register(std::unique_ptr<FontCache> &&fc, FontLoadReason load_reason)
233 if (fc ==
nullptr)
return;
238 auto it = std::find(std::begin(FontCache::caches), std::end(FontCache::caches),
nullptr);
239 if (it == std::end(FontCache::caches)) it = FontCache::caches.insert(it,
nullptr);
242 fc->font_index =
static_cast<FontIndex
>(std::distance(std::begin(FontCache::caches), it));
244 FontCache::default_font_index[
fs] = fc->font_index;
263 void FontCache::LoadDefaultFonts(
FontSize fs)
273 void FontCache::LoadFallbackFonts(
FontSize fs, FontLoadReason load_reason)
276 for (
auto it = setting->fallback_fonts.rbegin(); it != setting->fallback_fonts.rend(); ++it) {
288 static constexpr std::string_view FALLBACK_FONT =
"fallback";
289 static constexpr std::string_view DEFAULT_FONT =
"default";
290 static constexpr std::initializer_list<std::string_view> extra_prefer_default = {DEFAULT_FONT, FALLBACK_FONT};
291 static constexpr std::initializer_list<std::string_view> extra_prefer_fallback = {FALLBACK_FONT, DEFAULT_FONT};
295 FontCache::default_font_index[
fs] = INVALID_FONT_INDEX;
300 FontCache::caches.clear();
302 for (
auto it = std::begin(FontCache::caches); it != std::end(FontCache::caches); ++it) {
303 if (*it ==
nullptr)
continue;
304 if (!fontsizes.
Test((*it)->fs))
continue;
311 std::vector<std::string_view> fontnames;
315 if (!fontname.empty()) fontnames.push_back(fontname);
319 for (
const auto &extra_font : _fcsettings.
prefer_default ? extra_prefer_default : extra_prefer_fallback) {
320 if (std::ranges::find(fontnames, extra_font) == std::end(fontnames)) fontnames.push_back(extra_font);
324 FontCache::LoadFallbackFonts(
fs, FontLoadReason::MissingFallback);
327 for (
auto it = fontnames.rbegin(); it != fontnames.rend(); ++it) {
328 if (*it == DEFAULT_FONT) {
329 FontCache::LoadDefaultFonts(
fs);
330 }
else if (*it == FALLBACK_FONT) {
331 FontCache::LoadFallbackFonts(
fs, FontLoadReason::LanguageFallback);
337 FontCache::UpdateCharacterHeight(
fs);
347 for (
const auto &fc : FontCache::caches) {
348 if (fc ==
nullptr)
continue;
349 if (!fontsizes.
Test(fc->GetSize()))
continue;
350 fc->ClearFontCache();
354 FontCache::UpdateCharacterHeight(
fs);
363 FontCache::caches.clear();
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Font cache for basic fonts.
static void UninitializeFontCaches()
Free everything allocated w.r.t.
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
const FontSize fs
The size of the font.
FontLoadReason load_reason
Reason why the font is loaded.
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
int descender
The descender value of the font.
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.
static void ResetFontCache(FontSize fs)
Reset cached font information.
A searcher for missing glyphs.
static std::vector< const FontCacheFactory * > & GetProviders()
Get the currently known providers.
Parse data from a string / buffer.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
Factory to 'query' all available blitters.
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Functions for Standard In/Out file operations.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
fluid_settings_t * settings
FluidSynth settings handle.
std::string GetDefaultTruetypeFontFile(FontSize fs)
Get path of default font file for a given font size.
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
static bool IsDefaultFont(const FontCacheSubSetting &setting)
Test if a font setting uses the default font.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
static std::string GetDefaultTruetypeFont(FontSize fs)
Get name of default font file for a given font size.
Functions to read fonts from files and cache them.
FontType
Different types of font that can be loaded.
@ TrueType
Scalable TrueType fonts.
@ Sprite
Bitmap sprites from GRF files.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
void LoadStringWidthTable(FontSizes fontsizes)
Initialize _stringwidth_table cache for the specified font sizes.
Functions related to laying out the texts.
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.
constexpr FontSizes FONTSIZES_ALL
Mask of all possible font sizes.
A number of safeguards to prevent using unsafe methods.
void SaveToConfig()
Save the values to the configuration file.
Functions related to setting/changing the settings.
Definition of base types and functions in a cross-platform compatible way.
void CheckForMissingGlyphs(MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Functions related to OTTD's strings.
Settings for the four different fonts.
bool prefer_default
Prefer OpenTTD's default font over autodetected fallback fonts.
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
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.
Functions related to (drawing on) viewports.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Window functions not directly related to making/drawing windows.
Functions related to zooming.