MyGUI 3.5.3
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"
10#include "MyGUI_XmlDocument.h"
11
14
15#include <pugixml.hpp>
16
17namespace MyGUI
18{
19
21
22 FontManager::FontManager() :
23 mSingletonHolder(this),
24 mXmlFontTagName("Font"),
25 mXmlPropertyTagName("Property"),
26 mXmlDefaultFontValue("Default")
27 {
28 }
29
30 void FontManager::initialise()
31 {
32 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34
35 ResourceManager::getInstance().registerLoadXmlDelegate(mXmlFontTagName) =
36 newDelegate(this, &FontManager::_load);
37
38 const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
39 FactoryManager::getInstance().registerFactory<ResourceManualFont>(resourceCategory);
40 FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(resourceCategory);
41
42 mDefaultName = "Default";
43
44 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
45 mIsInitialise = true;
46 }
47
48 void FontManager::shutdown()
49 {
50 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
51 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
52
53 MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlFontTagName);
54
55 const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
56 FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(resourceCategory);
57 FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(resourceCategory);
58
59 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
60 mIsInitialise = false;
61 }
62
63 void FontManager::_load(xml::ElementPtr _node, std::string_view _file, Version _version)
64 {
65#ifndef MYGUI_DONT_USE_OBSOLETE
66 loadOldFontFormat(_node, _file, _version, mXmlFontTagName);
67#endif // MYGUI_DONT_USE_OBSOLETE
68
69 for (auto child : _node.node())
70 {
71 if (child.name() == mXmlPropertyTagName)
72 {
73 std::string_view key = child.attribute("key").value();
74 std::string_view value = child.attribute("value").value();
75#ifdef MYGUI_USE_FREETYPE
76 if (key == "Default")
77#else
78 if (key == "DefaultGenerated")
79#endif
80 mDefaultName = value;
81 }
82 }
83 }
84
85 void FontManager::setDefaultFont(std::string_view _value)
86 {
87 mDefaultName = _value;
88 }
89
90 IFont* FontManager::getByName(std::string_view _name) const
91 {
92 IResource* result = nullptr;
93 //FIXME for compatibility font may have name Default
94 if (!_name.empty() && _name != mXmlDefaultFontValue)
95 result = ResourceManager::getInstance().getByName(_name, false);
96
97 if (result == nullptr)
98 {
99 result = ResourceManager::getInstance().getByName(mDefaultName, false);
100 if (!_name.empty() && _name != mXmlDefaultFontValue)
101 {
102 MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
103 }
104 }
105
106 return result ? result->castType<IFont>(false) : nullptr;
107 }
108
109 const std::string& FontManager::getDefaultFont() const
110 {
111 return mDefaultName;
112 }
113
114} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
MYGUI_SINGLETON_DEFINITION(ClipboardManager)