diff --git a/src/font/font_manager.cpp b/src/font/font_manager.cpp index 772c7de22..df237fb0b 100644 --- a/src/font/font_manager.cpp +++ b/src/font/font_manager.cpp @@ -52,17 +52,21 @@ void FontManager::loadFonts() m_digit_ttf = new FaceTTF(stk_config->m_digit_ttf); // Now load fonts with settings of ttf file + unsigned int font_loaded = 0; RegularFace* regular = new RegularFace(m_normal_ttf); regular->init(); m_fonts.push_back(regular); + m_font_type_map[std::type_index(typeid(RegularFace))] = font_loaded++; BoldFace* bold = new BoldFace(m_normal_ttf); bold->init(); m_fonts.push_back(bold); + m_font_type_map[std::type_index(typeid(BoldFace))] = font_loaded++; DigitFace* digit = new DigitFace(m_digit_ttf); digit->init(); m_fonts.push_back(digit); + m_font_type_map[std::type_index(typeid(DigitFace))] = font_loaded++; } // loadFonts // ---------------------------------------------------------------------------- diff --git a/src/font/font_manager.hpp b/src/font/font_manager.hpp index 68f2118cc..aae90c0f3 100644 --- a/src/font/font_manager.hpp +++ b/src/font/font_manager.hpp @@ -25,6 +25,8 @@ #include "utils/ptr_vector.hpp" #include +#include +#include #include #include FT_FREETYPE_H @@ -35,13 +37,15 @@ class FontWithFace; class FontManager : public NoCopy { private: - PtrVector m_fonts; + PtrVector m_fonts; - FT_Library m_ft_library; + FT_Library m_ft_library; - FaceTTF* m_normal_ttf; + FaceTTF* m_normal_ttf; - FaceTTF* m_digit_ttf; + FaceTTF* m_digit_ttf; + + std::unordered_map m_font_type_map; public: LEAK_CHECK() @@ -53,11 +57,11 @@ public: template T* getFont() { T* out = NULL; - for (unsigned int i = 0; i < m_fonts.size(); i++) + const unsigned int n = m_font_type_map[std::type_index(typeid(T))]; + out = dynamic_cast(m_fonts.get(n)); + if (out != NULL) { - out = dynamic_cast(m_fonts.get(i)); - if (out != NULL) - return out; + return out; } Log::fatal("FontManager", "Can't get a font!"); return out;