MyGUI: MyGUI_FontManager.cpp Source File

MyGUI 3.2.2

MyGUI_FontManager.cpp
Go to the documentation of this file.
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
21 FontManager::FontManager() :
22 mIsInitialise(false),
23 mXmlFontTagName("Font"),
24 mXmlPropertyTagName("Property"),
25 mXmlDefaultFontValue("Default")
26 {
27 }
28
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
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
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
84 void FontManager::setDefaultFont(const std::string& _value)
85 {
86 mDefaultName = _value;
87 }
88
89 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
108 const std::string& FontManager::getDefaultFont() const
109 {
110 return mDefaultName;
111 }
112
113 } // namespace MyGUI
MyGUI::Singleton< ResourceManager >::getInstance
static ResourceManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition: MyGUI_ResourceManager.cpp:275
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:46
MyGUI::ResourceManualFont
Definition: MyGUI_ResourceManualFont.h:17
MyGUI::FontManager::initialise
void initialise()
Definition: MyGUI_FontManager.cpp:29
MyGUI_XmlDocument.h
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:119
MyGUI_ResourceTrueTypeFont.h
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:126
MyGUI_FontManager.h
MyGUI::xml::Element::getName
const std::string & getName() const
Definition: MyGUI_XmlDocument.cpp:332
MyGUI::FontManager::getDefaultFont
const std::string & getDefaultFont() const
Definition: MyGUI_FontManager.cpp:108
MyGUI::Singleton< FontManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:49
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:40
MyGUI_ResourceManualFont.h
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:158
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:246
MyGUI_Precompiled.h
MyGUI::Version
Definition: MyGUI_Version.h:17
MyGUI::IFont
Definition: MyGUI_IFont.h:20
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:114
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI::FontManager::getByName
IFont * getByName(const std::string &_name) const
Definition: MyGUI_FontManager.cpp:89
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition: MyGUI_ResourceManager.cpp:235
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
MyGUI::FontManager::FontManager
FontManager()
Definition: MyGUI_FontManager.cpp:21
MyGUI::FontManager::shutdown
void shutdown()
Definition: MyGUI_FontManager.cpp:46
MyGUI::MemberObsolete< FontManager >::loadOldFontFormat
void loadOldFontFormat(xml::ElementPtr _node, const std::string &_file, Version _version, const std::string &_tag)
Definition: MyGUI_BackwardCompatibility.cpp:654
MyGUI::IResource
Definition: MyGUI_IResource.h:23
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:352
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:100
newDelegate
MYGUI_TEMPLATE MYGUI_TEMPLATE_PARAMS delegates::IDelegateMYGUI_SUFFIX MYGUI_TEMPLATE_ARGS * newDelegate(void(*_func)(MYGUI_PARAMS))
Definition: MyGUI_DelegateImplement.h:117
MyGUI::FontManager::setDefaultFont
void setDefaultFont(const std::string &_value)
Definition: MyGUI_FontManager.cpp:84
MyGUI::ResourceTrueTypeFont
Definition: MyGUI_ResourceTrueTypeFont.h:22
MyGUI_FactoryManager.h