MyGUI: MyGUI_FontManager.cpp Source File

MyGUI 3.4.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"
9#include "MyGUI_FactoryManager.h"
10#include "MyGUI_XmlDocument.h"
11
12#include "MyGUI_ResourceManualFont.h"
13#include "MyGUI_ResourceTrueTypeFont.h"
14
15namespace MyGUI
16{
17
18 MYGUI_SINGLETON_DEFINITION(FontManager);
19
20 FontManager::FontManager() :
21 mXmlFontTagName("Font"),
22 mXmlPropertyTagName("Property"),
23 mXmlDefaultFontValue("Default"),
24 mSingletonHolder(this)
25 {
26 }
27
28 void FontManager::initialise()
29 {
30 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
31 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
32
33 ResourceManager::getInstance().registerLoadXmlDelegate(mXmlFontTagName) =
34 newDelegate(this, &FontManager::_load);
35
36 const 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 const 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, std::string_view _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 std::string_view key = node->findAttribute("key");
73 std::string_view 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(std::string_view _value)
85 {
86 mDefaultName = _value;
87 }
88
89 IFont* FontManager::getByName(std::string_view _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_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_FontManager.h
MyGUI_Precompiled.h
MyGUI_ResourceManualFont.h
MyGUI_ResourceTrueTypeFont.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI_XmlDocument.h
MyGUI::FactoryManager::registerFactory
void registerFactory(std::string_view _category, std::string_view _type, Delegate::IDelegate *_delegate)
Definition MyGUI_FactoryManager.cpp:39
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:54
MyGUI::FontManager
Definition MyGUI_FontManager.h:22
MyGUI::FontManager::FontManager
FontManager()
Definition MyGUI_FontManager.cpp:20
MyGUI::FontManager::setDefaultFont
void setDefaultFont(std::string_view _value)
Definition MyGUI_FontManager.cpp:84
MyGUI::FontManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::FontManager::getByName
IFont * getByName(std::string_view _name) const
Definition MyGUI_FontManager.cpp:89
MyGUI::FontManager::getDefaultFont
const std::string & getDefaultFont() const
Definition MyGUI_FontManager.cpp:108
MyGUI::FontManager::initialise
void initialise()
Definition MyGUI_FontManager.cpp:28
MyGUI::FontManager::shutdown
void shutdown()
Definition MyGUI_FontManager.cpp:46
MyGUI::IFont
Definition MyGUI_IFont.h:21
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::IResource
Definition MyGUI_IResource.h:24
MyGUI::MemberObsolete< FontManager >::loadOldFontFormat
void loadOldFontFormat(xml::ElementPtr _node, std::string_view _file, Version _version, std::string_view _tag)
Definition MyGUI_BackwardCompatibility.cpp:657
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:127
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_ResourceManager.cpp:283
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:120
MyGUI::ResourceManager::getByName
IResource * getByName(std::string_view _name, bool _throw=true) const
Definition MyGUI_ResourceManager.cpp:243
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::ResourceManualFont
Definition MyGUI_ResourceManualFont.h:20
MyGUI::ResourceTrueTypeFont
Definition MyGUI_ResourceTrueTypeFont.h:34
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::Element::findAttribute
bool findAttribute(std::string_view _name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:242
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:328
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100