MyGUI  3.2.1
MyGUI_FontManager.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_FontManager.h"
00009 #include "MyGUI_FactoryManager.h"
00010 #include "MyGUI_XmlDocument.h"
00011 
00012 #include "MyGUI_ResourceManualFont.h"
00013 #include "MyGUI_ResourceTrueTypeFont.h"
00014 
00015 namespace MyGUI
00016 {
00017 
00018     template <> FontManager* Singleton<FontManager>::msInstance = nullptr;
00019     template <> const char* Singleton<FontManager>::mClassTypeName = "FontManager";
00020 
00021     FontManager::FontManager() :
00022         mIsInitialise(false),
00023         mXmlFontTagName("Font"),
00024         mXmlPropertyTagName("Property"),
00025         mXmlDefaultFontValue("Default")
00026     {
00027     }
00028 
00029     void FontManager::initialise()
00030     {
00031         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00032         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00033 
00034         ResourceManager::getInstance().registerLoadXmlDelegate(mXmlFontTagName) = newDelegate(this, &FontManager::_load);
00035 
00036         std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
00037         FactoryManager::getInstance().registerFactory<ResourceManualFont>(resourceCategory);
00038         FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(resourceCategory);
00039 
00040         mDefaultName = "Default";
00041 
00042         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00043         mIsInitialise = true;
00044     }
00045 
00046     void FontManager::shutdown()
00047     {
00048         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00049         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00050 
00051         MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlFontTagName);
00052 
00053         std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
00054         FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(resourceCategory);
00055         FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(resourceCategory);
00056 
00057         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00058         mIsInitialise = false;
00059     }
00060 
00061     void FontManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00062     {
00063 #ifndef MYGUI_DONT_USE_OBSOLETE
00064         loadOldFontFormat(_node, _file, _version, mXmlFontTagName);
00065 #endif // MYGUI_DONT_USE_OBSOLETE
00066 
00067         xml::ElementEnumerator node = _node->getElementEnumerator();
00068         while (node.next())
00069         {
00070             if (node->getName() == mXmlPropertyTagName)
00071             {
00072                 const std::string& key = node->findAttribute("key");
00073                 const std::string& value = node->findAttribute("value");
00074 #ifdef MYGUI_USE_FREETYPE
00075                 if (key == "Default")
00076 #else
00077                 if (key == "DefaultGenerated")
00078 #endif
00079                     mDefaultName = value;
00080             }
00081         }
00082     }
00083 
00084     void FontManager::setDefaultFont(const std::string& _value)
00085     {
00086         mDefaultName = _value;
00087     }
00088 
00089     IFont* FontManager::getByName(const std::string& _name) const
00090     {
00091         IResource* result = nullptr;
00092         //FIXME для совместимости шрифт может иметь имя Default
00093         if (!_name.empty() && _name != mXmlDefaultFontValue)
00094             result = ResourceManager::getInstance().getByName(_name, false);
00095 
00096         if (result == nullptr)
00097         {
00098             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00099             if (!_name.empty() && _name != mXmlDefaultFontValue)
00100             {
00101                 MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
00102             }
00103         }
00104 
00105         return result ? result->castType<IFont>(false) : nullptr;
00106     }
00107 
00108     const std::string& FontManager::getDefaultFont() const
00109     {
00110         return mDefaultName;
00111     }
00112 
00113 } // namespace MyGUI