Add: Automatically load fonts for missing glyphs. (#14856)

This commit is contained in:
Peter Nelson
2025-12-06 12:22:25 +00:00
committed by dP
parent eae2d6b62c
commit c26a9827b8
4 changed files with 60 additions and 4 deletions

View File

@@ -75,6 +75,7 @@ int FontCache::GetDefaultFontHeight(FontSize fs)
for (const auto &fc : FontCache::caches) {
if (fc == nullptr || fc->fs != fs) continue;
if (fc->load_reason == FontLoadReason::MissingFallback) continue; // Avoid dynamically loaded fonts affecting widget sizes.
ascender = std::max(ascender, fc->ascender);
descender = std::min(descender, fc->descender);
}
@@ -269,10 +270,11 @@ std::string GetFontCacheFontName(FontSize fs)
}
}
/* static */ void FontCache::LoadFallbackFonts(FontSize fs)
/* static */ void FontCache::LoadFallbackFonts(FontSize fs, FontLoadReason load_reason)
{
const FontCacheSubSetting *setting = GetFontCacheSubSetting(fs);
for (auto it = setting->fallback_fonts.rbegin(); it != setting->fallback_fonts.rend(); ++it) {
if (it->load_reason != load_reason) continue;
FontCache::Register(FontProviderManager::LoadFont(fs, FontType::TrueType, false, it->name, it->os_handle), it->load_reason);
}
}
@@ -318,12 +320,15 @@ std::string GetFontCacheFontName(FontSize fs)
if (std::ranges::find(fontnames, extra_font) == std::end(fontnames)) fontnames.push_back(extra_font);
}
/* First load fonts for missing glyphs discovered during string formatting. */
FontCache::LoadFallbackFonts(fs, FontLoadReason::MissingFallback);
/* Load configured fonts in reverse order so that the first entry has priority. */
for (auto it = fontnames.rbegin(); it != fontnames.rend(); ++it) {
if (*it == DEFAULT_FONT) {
FontCache::LoadDefaultFonts(fs);
} else if (*it == FALLBACK_FONT) {
FontCache::LoadFallbackFonts(fs);
FontCache::LoadFallbackFonts(fs, FontLoadReason::LanguageFallback);
} else {
FontCache::Register(FontProviderManager::LoadFont(fs, FontType::TrueType, true, std::string{*it}, {}), FontLoadReason::Configured);
}