1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #include "MyGUI_Precompiled.h"
8 #include "MyGUI_FontManager.h"
9 #include "MyGUI_FactoryManager.h"
10 #include "MyGUI_XmlDocument.h"
11 
12 #include "MyGUI_ResourceManualFont.h"
13 #include "MyGUI_ResourceTrueTypeFont.h"
14 
15 namespace MyGUI
16 {
17 
18 	template <> FontManager* Singleton<FontManager>::msInstance = nullptr;
19 	template <> const char* Singleton<FontManager>::mClassTypeName = "FontManager";
20 
FontManager()21 	FontManager::FontManager() :
22 		mIsInitialise(false),
23 		mXmlFontTagName("Font"),
24 		mXmlPropertyTagName("Property"),
25 		mXmlDefaultFontValue("Default")
26 	{
27 	}
28 
initialise()29 	void FontManager::initialise()
30 	{
31 		MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 		MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33 
34 		ResourceManager::getInstance().registerLoadXmlDelegate(mXmlFontTagName) = newDelegate(this, &FontManager::_load);
35 
36 		std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
37 		FactoryManager::getInstance().registerFactory<ResourceManualFont>(resourceCategory);
38 		FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(resourceCategory);
39 
40 		mDefaultName = "Default";
41 
42 		MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
43 		mIsInitialise = true;
44 	}
45 
shutdown()46 	void FontManager::shutdown()
47 	{
48 		MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
49 		MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
50 
51 		MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlFontTagName);
52 
53 		std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
54 		FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(resourceCategory);
55 		FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(resourceCategory);
56 
57 		MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
58 		mIsInitialise = false;
59 	}
60 
_load(xml::ElementPtr _node,const std::string & _file,Version _version)61 	void FontManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
62 	{
63 #ifndef MYGUI_DONT_USE_OBSOLETE
64 		loadOldFontFormat(_node, _file, _version, mXmlFontTagName);
65 #endif // MYGUI_DONT_USE_OBSOLETE
66 
67 		xml::ElementEnumerator node = _node->getElementEnumerator();
68 		while (node.next())
69 		{
70 			if (node->getName() == mXmlPropertyTagName)
71 			{
72 				const std::string& key = node->findAttribute("key");
73 				const std::string& value = node->findAttribute("value");
74 #ifdef MYGUI_USE_FREETYPE
75 				if (key == "Default")
76 #else
77 				if (key == "DefaultGenerated")
78 #endif
79 					mDefaultName = value;
80 			}
81 		}
82 	}
83 
setDefaultFont(const std::string & _value)84 	void FontManager::setDefaultFont(const std::string& _value)
85 	{
86 		mDefaultName = _value;
87 	}
88 
getByName(const std::string & _name) const89 	IFont* FontManager::getByName(const std::string& _name) const
90 	{
91 		IResource* result = nullptr;
92 		//FIXME для совместимости шрифт может иметь имя Default
93 		if (!_name.empty() && _name != mXmlDefaultFontValue)
94 			result = ResourceManager::getInstance().getByName(_name, false);
95 
96 		if (result == nullptr)
97 		{
98 			result = ResourceManager::getInstance().getByName(mDefaultName, false);
99 			if (!_name.empty() && _name != mXmlDefaultFontValue)
100 			{
101 				MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
102 			}
103 		}
104 
105 		return result ? result->castType<IFont>(false) : nullptr;
106 	}
107 
getDefaultFont() const108 	const std::string& FontManager::getDefaultFont() const
109 	{
110 		return mDefaultName;
111 	}
112 
113 } // namespace MyGUI
114