Spawn level: Add 'get_spawn_level(x, z)' API
[oweals/minetest.git] / src / fontengine.cpp
index a4d96e0787388101684843a8d1554957bd8ad941..b40e1ef0f0bd483f36ea62e0e7bd0f251d10189c 100644 (file)
@@ -46,8 +46,8 @@ FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
        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); // pre-condition
@@ -113,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<unsigned int, irr::gui::IGUIFont*>::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();
        }
 }
 
@@ -334,32 +332,70 @@ 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;
                }
 
-               // try fallback font
-               errorstream << "FontEngine: failed to load: " << font_path << ", trying to fall back "
-                               "to fallback font" << std::endl;
+               if (font_config_prefix == "mono_") {
+                       const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
 
-               font_path = g_settings->get(font_config_prefix + "fallback_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,
-                       font_path.c_str(), size, true, true, font_shadow,
-                       font_shadow_alpha);
+                               font = gui::CGUITTFont::createTTFont(m_env,
+                                       mono_font_path.c_str(), size, true, true,
+                                       font_shadow, font_shadow_alpha);
 
-               if (font != NULL) {
-                       m_font_cache[mode][basesize] = font;
-                       return;
+                               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;
+               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
@@ -370,7 +406,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
 {
        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 {
@@ -461,7 +497,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
                }
        }
 
-       if (font != NULL) {
+       if (font) {
                font->grab();
                m_font_cache[mode][basesize] = font;
        }