1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-14 17:15:21 +02:00

Allow overriding fonts via media files (#15606)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Lars Müller
2025-01-19 20:42:40 +01:00
committed by GitHub
parent eeb6cab4c4
commit 547e1476bb
8 changed files with 270 additions and 138 deletions

View File

@@ -3,18 +3,18 @@
// Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
#include "fontengine.h"
#include <cmath>
#include "client/renderingengine.h"
#include "config.h"
#include "porting.h"
#include "filesys.h"
#include "gettext.h"
#include "settings.h"
#include "irrlicht_changes/CGUITTFont.h"
#include "util/numeric.h" // rangelim
#include <IGUIEnvironment.h>
#include <IGUIFont.h>
#include <cmath>
#include <cstring>
#include <unordered_set>
/** reference to access font engine, has to be initialized by main */
FontEngine *g_fontengine = nullptr;
@@ -35,7 +35,6 @@ static const char *settings[] = {
"dpi_change_notifier", "display_density_factor", "gui_scaling",
};
/******************************************************************************/
FontEngine::FontEngine(gui::IGUIEnvironment* env) :
m_env(env)
{
@@ -53,16 +52,14 @@ FontEngine::FontEngine(gui::IGUIEnvironment* env) :
g_settings->registerChangedCallback(name, font_setting_changed, this);
}
/******************************************************************************/
FontEngine::~FontEngine()
{
g_settings->deregisterAllChangedCallbacks(this);
cleanCache();
clearCache();
}
/******************************************************************************/
void FontEngine::cleanCache()
void FontEngine::clearCache()
{
RecursiveMutexAutoLock l(m_font_mutex);
@@ -76,7 +73,6 @@ void FontEngine::cleanCache()
}
}
/******************************************************************************/
irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
{
return getFont(spec, false);
@@ -118,7 +114,6 @@ irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
return font;
}
/******************************************************************************/
unsigned int FontEngine::getTextHeight(const FontSpec &spec)
{
gui::IGUIFont *font = getFont(spec);
@@ -126,7 +121,6 @@ unsigned int FontEngine::getTextHeight(const FontSpec &spec)
return font->getDimension(L"Some unimportant example String").Height;
}
/******************************************************************************/
unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
{
gui::IGUIFont *font = getFont(spec);
@@ -143,7 +137,6 @@ unsigned int FontEngine::getLineHeight(const FontSpec &spec)
+ font->getKerning(L'S').Y;
}
/******************************************************************************/
unsigned int FontEngine::getDefaultFontSize()
{
return m_default_size[m_currentMode];
@@ -157,7 +150,6 @@ unsigned int FontEngine::getFontSize(FontMode mode)
return m_default_size[mode];
}
/******************************************************************************/
void FontEngine::readSettings()
{
m_default_size[FM_Standard] = rangelim(g_settings->getU16("font_size"), 5, 72);
@@ -167,12 +159,9 @@ void FontEngine::readSettings()
m_default_bold = g_settings->getBool("font_bold");
m_default_italic = g_settings->getBool("font_italic");
cleanCache();
updateFontCache();
updateSkin();
refresh();
}
/******************************************************************************/
void FontEngine::updateSkin()
{
gui::IGUIFont *font = getFont();
@@ -181,15 +170,50 @@ void FontEngine::updateSkin()
m_env->getSkin()->setFont(font);
}
/******************************************************************************/
void FontEngine::updateFontCache()
void FontEngine::updateCache()
{
/* the only font to be initialized is default one,
* all others are re-initialized on demand */
getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
}
/******************************************************************************/
void FontEngine::refresh() {
clearCache();
updateCache();
updateSkin();
}
void FontEngine::setMediaFont(const std::string &name, const std::string &data)
{
static std::unordered_set<std::string> valid_names {
"regular", "bold", "italic", "bold_italic",
"mono", "mono_bold", "mono_italic", "mono_bold_italic",
};
if (!valid_names.count(name)) {
warningstream << "Ignoring unrecognized media font: " << name << std::endl;
return;
}
constexpr char TTF_MAGIC[5] = {0, 1, 0, 0, 0};
if (data.size() < 5 || (memcmp(data.data(), "wOFF", 4) &&
memcmp(data.data(), TTF_MAGIC, 5))) {
warningstream << "Rejecting media font with unrecognized magic" << std::endl;
return;
}
std::string copy = data;
irr_ptr<gui::SGUITTFace> face(gui::SGUITTFace::createFace(std::move(copy)));
m_media_faces.emplace(name, face);
refresh();
}
void FontEngine::clearMediaFonts()
{
RecursiveMutexAutoLock l(m_font_mutex);
m_media_faces.clear();
refresh();
}
gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
{
assert(spec.mode != FM_Unspecified);
@@ -230,28 +254,55 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
else
path_setting = setting_prefix + "font_path" + setting_suffix;
std::string fallback_settings[] = {
g_settings->get(path_setting),
Settings::getLayer(SL_DEFAULTS)->get(path_setting)
};
std::string media_name = spec.mode == FM_Mono
? "mono" + setting_suffix
: (setting_suffix.empty() ? "" : setting_suffix.substr(1));
if (media_name.empty())
media_name = "regular";
for (const std::string &font_path : fallback_settings) {
gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
font_path.c_str(), size, true, true, font_shadow,
auto createFont = [&](gui::SGUITTFace *face) -> gui::CGUITTFont* {
auto *font = gui::CGUITTFont::createTTFont(m_env,
face, size, true, true, font_shadow,
font_shadow_alpha);
if (!font) {
errorstream << "FontEngine: Cannot load '" << font_path <<
"'. Trying to fall back to another path." << std::endl;
continue;
}
if (!font)
return nullptr;
if (spec.mode != _FM_Fallback) {
FontSpec spec2(spec);
spec2.mode = _FM_Fallback;
font->setFallback(getFont(spec2, true));
}
return font;
};
auto it = m_media_faces.find(media_name);
if (it != m_media_faces.end()) {
auto *face = it->second.get();
if (auto *font = createFont(face))
return font;
errorstream << "FontEngine: Cannot load media font '" << media_name <<
"'. Falling back to client settings." << std::endl;
}
std::string fallback_settings[] = {
g_settings->get(path_setting),
Settings::getLayer(SL_DEFAULTS)->get(path_setting)
};
for (const std::string &font_path : fallback_settings) {
infostream << "Creating new font: " << font_path.c_str()
<< " " << size << "pt" << std::endl;
// Grab the face.
if (auto *face = irr::gui::SGUITTFace::loadFace(font_path)) {
auto *font = createFont(face);
face->drop();
return font;
}
errorstream << "FontEngine: Cannot load '" << font_path <<
"'. Trying to fall back to another path." << std::endl;
}
return nullptr;
}