X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Ffontengine.cpp;h=b40e1ef0f0bd483f36ea62e0e7bd0f251d10189c;hb=ec93893c7f34bcab35b1c356497d8def47d1bf1c;hp=79d53c9898cd7306d350be1f60d0019c4dd4c724;hpb=2c4a5e18619a69159bcace8c03b4ea5642f46836;p=minetest.git diff --git a/src/fontengine.cpp b/src/fontengine.cpp index 79d53c989..b40e1ef0f 100644 --- a/src/fontengine.cpp +++ b/src/fontengine.cpp @@ -16,17 +16,16 @@ You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + #include "fontengine.h" -#include "log.h" -#include "main.h" +#include "client/renderingengine.h" #include "config.h" #include "porting.h" -#include "constants.h" #include "filesys.h" #if USE_FREETYPE #include "gettext.h" -#include "xCGUITTFont.h" +#include "irrlicht_changes/CGUITTFont.h" #endif /** maximum size distance for getting a "similar" font size */ @@ -36,28 +35,24 @@ with this program; if not, write to the Free Software Foundation, Inc., FontEngine* g_fontengine = NULL; /** callback to be used on change of font size setting */ -static void font_setting_changed(const std::string) { +static void font_setting_changed(const std::string &name, void *userdata) +{ g_fontengine->readSettings(); } /******************************************************************************/ FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) : m_settings(main_settings), - m_env(env), - m_font_cache(), - m_currentMode(FM_Standard), - m_lastMode(), - m_lastSize(0), - m_lastFont(NULL) + m_env(env) { - for (unsigned int i = 0; i < FM_MaxMode; i++) { - m_default_size[i] = (FontMode) FONT_SIZE_UNSPECIFIED; + for (u32 &i : m_default_size) { + i = (FontMode) FONT_SIZE_UNSPECIFIED; } - assert(m_settings != NULL); - assert(m_env != NULL); - assert(m_env->getSkin() != NULL); + assert(m_settings != NULL); // pre-condition + assert(m_env != NULL); // pre-condition + assert(m_env->getSkin() != NULL); // pre-condition m_currentMode = FM_Simple; @@ -91,22 +86,22 @@ FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) : updateSkin(); if (m_currentMode == FM_Standard) { - m_settings->registerChangedCallback("font_size", font_setting_changed); - m_settings->registerChangedCallback("font_path", font_setting_changed); - m_settings->registerChangedCallback("font_shadow", font_setting_changed); - m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed); + m_settings->registerChangedCallback("font_size", font_setting_changed, NULL); + m_settings->registerChangedCallback("font_path", font_setting_changed, NULL); + m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL); + m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL); } else if (m_currentMode == FM_Fallback) { - m_settings->registerChangedCallback("fallback_font_size", font_setting_changed); - m_settings->registerChangedCallback("fallback_font_path", font_setting_changed); - m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed); - m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed); + m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL); + m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL); + m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL); + m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL); } - m_settings->registerChangedCallback("mono_font_path", font_setting_changed); - m_settings->registerChangedCallback("mono_font_size", font_setting_changed); - m_settings->registerChangedCallback("screen_dpi", font_setting_changed); - m_settings->registerChangedCallback("gui_scaling", font_setting_changed); + m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL); + m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL); + m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL); + m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL); } /******************************************************************************/ @@ -118,15 +113,13 @@ FontEngine::~FontEngine() /******************************************************************************/ void FontEngine::cleanCache() { - for ( unsigned int i = 0; i < FM_MaxMode; i++) { + for (auto &font_cache_it : m_font_cache) { - for (std::map::iterator iter - = m_font_cache[i].begin(); - iter != m_font_cache[i].end(); iter++) { - iter->second->drop(); - iter->second = NULL; + for (auto &font_it : font_cache_it) { + font_it.second->drop(); + font_it.second = NULL; } - m_font_cache[i].clear(); + font_cache_it.clear(); } } @@ -172,7 +165,7 @@ unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode) if (font == NULL) { font = m_env->getSkin()->getFont(); } - assert(font != NULL); + FATAL_ERROR_IF(font == NULL, "Could not get skin font"); return font->getDimension(L"Some unimportant example String").Height; } @@ -187,7 +180,7 @@ unsigned int FontEngine::getTextWidth(const std::wstring& text, if (font == NULL) { font = m_env->getSkin()->getFont(); } - assert(font != NULL); + FATAL_ERROR_IF(font == NULL, "Could not get font"); return font->getDimension(text.c_str()).Width; } @@ -202,7 +195,7 @@ unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode) if (font == NULL) { font = m_env->getSkin()->getFont(); } - assert(font != NULL); + FATAL_ERROR_IF(font == NULL, "Could not get font"); return font->getDimension(L"Some unimportant example String").Height + font->getKerningHeight(); @@ -255,7 +248,7 @@ void FontEngine::updateSkin() // If we did fail to create a font our own make irrlicht find a default one font = m_env->getSkin()->getFont(); - assert(font); + FATAL_ERROR_IF(font == NULL, "Could not create/get font"); u32 text_height = font->getDimension(L"Hello, world!").Height; infostream << "text_height=" << text_height << std::endl; @@ -319,10 +312,8 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode) if (! is_yes(m_settings->get("freetype"))) { return; } - unsigned int size = floor( - porting::getDisplayDensity() * - m_settings->getFloat("gui_scaling") * - basesize); + unsigned int size = floor(RenderingEngine::getDisplayDensity() * + m_settings->getFloat("gui_scaling") * basesize); u32 font_shadow = 0; u32 font_shadow_alpha = 0; @@ -341,13 +332,71 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode) font_path.c_str(), size, true, true, font_shadow, font_shadow_alpha); - if (font != NULL) { + if (font) { m_font_cache[mode][basesize] = font; + return; } - else { - errorstream << "FontEngine: failed to load freetype font: " - << font_path << std::endl; + + if (font_config_prefix == "mono_") { + const std::string &mono_font_path = m_settings->getDefault("mono_font_path"); + + if (font_path != mono_font_path) { + // try original mono font + errorstream << "FontEngine: failed to load custom mono " + "font: " << font_path << ", trying to fall back to " + "original mono font" << std::endl; + + font = gui::CGUITTFont::createTTFont(m_env, + mono_font_path.c_str(), size, true, true, + font_shadow, font_shadow_alpha); + + if (font) { + m_font_cache[mode][basesize] = font; + return; + } + } + } else { + // try fallback font + errorstream << "FontEngine: failed to load: " << font_path << + ", trying to fall back to fallback font" << std::endl; + + font_path = g_settings->get(font_config_prefix + "fallback_font_path"); + + font = gui::CGUITTFont::createTTFont(m_env, + font_path.c_str(), size, true, true, font_shadow, + font_shadow_alpha); + + if (font) { + m_font_cache[mode][basesize] = font; + return; + } + + const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path"); + + if (font_path != fallback_font_path) { + // try original fallback font + errorstream << "FontEngine: failed to load custom fallback " + "font: " << font_path << ", trying to fall back to " + "original fallback font" << std::endl; + + font = gui::CGUITTFont::createTTFont(m_env, + fallback_font_path.c_str(), size, true, true, + font_shadow, font_shadow_alpha); + + if (font) { + m_font_cache[mode][basesize] = font; + return; + } + } } + + // give up + errorstream << "FontEngine: failed to load freetype font: " + << font_path << std::endl; + errorstream << "minetest can not continue without a valid font. " + "Please correct the 'font_path' setting or install the font " + "file in the proper location" << std::endl; + abort(); } #endif } @@ -355,9 +404,9 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode) /** initialize a font without freetype */ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode) { - assert((mode == FM_Simple) || (mode == FM_SimpleMono)); + assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition - std::string font_path = ""; + std::string font_path; if (mode == FM_Simple) { font_path = m_settings->get("font_path"); } else { @@ -380,7 +429,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode) basesize = DEFAULT_FONT_SIZE; unsigned int size = floor( - porting::getDisplayDensity() * + RenderingEngine::getDisplayDensity() * m_settings->getFloat("gui_scaling") * basesize); @@ -448,7 +497,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode) } } - if (font != NULL) { + if (font) { font->grab(); m_font_cache[mode][basesize] = font; }