MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_FontManager.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_FontManager.h"
24 #include "MyGUI_FactoryManager.h"
25 #include "MyGUI_XmlDocument.h"
26
27 #include "MyGUI_ResourceManualFont.h"
28 #include "MyGUI_ResourceTrueTypeFont.h"
29
30 namespace MyGUI
31 {
32 const std::string XML_TYPE("Font");
33 const std::string XML_TYPE_RESOURCE("Resource");
34 const std::string XML_TYPE_PROPERTY("Property");
35 const std::string RESOURCE_DEFAULT_NAME("Default");
36
37 template <> FontManager* Singleton<FontManager>::msInstance = nullptr;
38 template <> const char* Singleton<FontManager>::mClassTypeName("FontManager");
39
40 FontManager::FontManager() :
41 mIsInitialise(false)
42 {
43 }
44
45 void FontManager::initialise()
46 {
47 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
48 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
49
50 ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &FontManager::_load);
51
52 FactoryManager::getInstance().registerFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
53 FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
54
55 mDefaultName = "Default";
56
57 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
58 mIsInitialise = true;
59 }
60
61 void FontManager::shutdown()
62 {
63 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
64 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
65
66 MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
67
68 FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
69 FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
70
71 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
72 mIsInitialise = false;
73 }
74
75 void FontManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
76 {
77 xml::ElementEnumerator font = _node->getElementEnumerator();
78 while (font.next())
79 {
80 if (font->getName() == XML_TYPE)
81 {
82 std::string name;
83 if (!font->findAttribute("name", name)) continue;
84
85 std::string type;
86 if (type.empty())
87 {
88 if (font->findAttribute("resolution").empty()) type = "ResourceManualFont";
89 else type = "ResourceTrueTypeFont";
90 }
91
92 xml::Document doc;
93 xml::ElementPtr root = doc.createRoot("MyGUI");
94 xml::ElementPtr node = root->createChild("Resource");
95 node->addAttribute("type", type);
96 node->addAttribute("name", name);
97
98 std::string tmp;
99 if (font->findAttribute("source", tmp))
100 {
101 xml::ElementPtr prop = node->createChild("Property");
102 prop->addAttribute("key", "Source");
103 prop->addAttribute("value", tmp);
104 }
105
106 if (font->findAttribute("size", tmp))
107 {
108 xml::ElementPtr prop = node->createChild("Property");
109 prop->addAttribute("key", "Size");
110 prop->addAttribute("value", tmp);
111 }
112
113 if (font->findAttribute("resolution", tmp))
114 {
115 xml::ElementPtr prop = node->createChild("Property");
116 prop->addAttribute("key", "Resolution");
117 prop->addAttribute("value", tmp);
118 }
119
120 if (font->findAttribute("antialias_colour", tmp))
121 {
122 xml::ElementPtr prop = node->createChild("Property");
123 prop->addAttribute("key", "Antialias");
124 prop->addAttribute("value", tmp);
125 }
126
127 if (font->findAttribute("space_width", tmp))
128 {
129 xml::ElementPtr prop = node->createChild("Property");
130 prop->addAttribute("key", "SpaceWidth");
131 prop->addAttribute("value", tmp);
132 }
133
134 if (font->findAttribute("tab_width", tmp))
135 {
136 xml::ElementPtr prop = node->createChild("Property");
137 prop->addAttribute("key", "TabWidth");
138 prop->addAttribute("value", tmp);
139 }
140
141 if (font->findAttribute("cursor_width", tmp))
142 {
143 xml::ElementPtr prop = node->createChild("Property");
144 prop->addAttribute("key", "CursorWidth");
145 prop->addAttribute("value", tmp);
146 }
147
148 if (font->findAttribute("distance", tmp))
149 {
150 xml::ElementPtr prop = node->createChild("Property");
151 prop->addAttribute("key", "Distance");
152 prop->addAttribute("value", tmp);
153 }
154
155 if (font->findAttribute("offset_height", tmp))
156 {
157 xml::ElementPtr prop = node->createChild("Property");
158 prop->addAttribute("key", "OffsetHeight");
159 prop->addAttribute("value", tmp);
160 }
161
162 if (font->findAttribute("default_height", tmp))
163 {
164 xml::ElementPtr prop = node->createChild("Property");
165 prop->addAttribute("key", "DefaultHeight");
166 prop->addAttribute("value", tmp);
167 }
168
169 xml::ElementPtr codes = node->createChild("Codes");
170
171 xml::ElementEnumerator codeold = font->getElementEnumerator();
172 while (codeold.next("Code"))
173 {
174 xml::ElementPtr codenew = codes->createChild("Code");
175
176 if (codeold->findAttribute("range", tmp))
177 codenew->addAttribute("range", tmp);
178
179 if (codeold->findAttribute("hide", tmp))
180 codenew->addAttribute("hide", tmp);
181
182 if (codeold->findAttribute("index", tmp))
183 codenew->addAttribute("index", tmp);
184
185 if (codeold->findAttribute("coord", tmp))
186 codenew->addAttribute("coord", tmp);
187 }
188
189 ResourceManager::getInstance().loadFromXmlNode(root, _file, _version);
190 }
191 else if (font->getName() == XML_TYPE_PROPERTY)
192 {
193 const std::string& key = font->findAttribute("key");
194 const std::string& value = font->findAttribute("value");
195 if (key == "Default")
196 mDefaultName = value;
197 }
198 }
199 }
200
201 void FontManager::setDefaultFont(const std::string& _value)
202 {
203 mDefaultName = _value;
204 }
205
206 IFont* FontManager::getByName(const std::string& _name) const
207 {
208 IResource* result = nullptr;
209 //FIXME для совместимости шрифт может иметь имя Default
210 if (!_name.empty() && _name != RESOURCE_DEFAULT_NAME)
211 result = ResourceManager::getInstance().getByName(_name, false);
212
213 if (result == nullptr)
214 {
215 result = ResourceManager::getInstance().getByName(mDefaultName, false);
216 if (!_name.empty() && _name != RESOURCE_DEFAULT_NAME)
217 {
218 MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
219 }
220 }
221
222 return result ? result->castType<IFont>(false) : nullptr;
223 }
224
225 const std::string& FontManager::getDefaultFont() const
226 {
227 return mDefaultName;
228 }
229
230 } // namespace MyGUI
MyGUI::RESOURCE_DEFAULT_NAME
const std::string RESOURCE_DEFAULT_NAME("Default")
MyGUI_FactoryManager.h
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:142
MyGUI_Precompiled.h
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:135
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition: MyGUI_XmlDocument.h:114
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:150
MyGUI::Singleton< ResourceManager >::getInstance
static ResourceManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::ResourceManager::loadFromXmlNode
void loadFromXmlNode(xml::ElementPtr _node, const std::string &_file, Version _version)
Definition: MyGUI_ResourceManager.cpp:82
MyGUI::ResourceManualFont
Definition: MyGUI_ResourceManualFont.h:32
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:261
MyGUI::Singleton< FontManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI_ResourceManualFont.h
MyGUI::XML_TYPE
const std::string XML_TYPE("Font")
MyGUI::IFont
Definition: MyGUI_IFont.h:35
MyGUI_FontManager.h
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition: MyGUI_ResourceManager.cpp:251
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:124
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI_XmlDocument.h
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:115
MyGUI::FontManager::FontManager
FontManager()
Definition: MyGUI_FontManager.cpp:40
MyGUI::FontManager::getDefaultFont
const std::string & getDefaultFont() const
Definition: MyGUI_FontManager.cpp:225
MyGUI::ResourceTrueTypeFont
Definition: MyGUI_ResourceTrueTypeFont.h:37
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::XML_TYPE_PROPERTY
const std::string XML_TYPE_PROPERTY("Property")
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:168
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
MyGUI::Version
Definition: MyGUI_Version.h:32
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:61
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:55
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:367
MyGUI::FontManager::getByName
IFont * getByName(const std::string &_name) const
Definition: MyGUI_FontManager.cpp:206
MyGUI::FontManager::shutdown
void shutdown()
Definition: MyGUI_FontManager.cpp:61
MyGUI::xml::Element::getName
const std::string & getName() const
Definition: MyGUI_XmlDocument.cpp:347
MyGUI::FontManager::setDefaultFont
void setDefaultFont(const std::string &_value)
Definition: MyGUI_FontManager.cpp:201
MyGUI::IResource
Definition: MyGUI_IResource.h:38
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI::xml::Element::addAttribute
void addAttribute(const std::string &_key, const T &_value)
Definition: MyGUI_XmlDocument.h:184
MyGUI_ResourceTrueTypeFont.h
MyGUI::XML_TYPE_RESOURCE
const std::string XML_TYPE_RESOURCE("Resource")
MyGUI::FontManager::initialise
void initialise()
Definition: MyGUI_FontManager.cpp:45
MyGUI::xml::Element::createChild
ElementPtr createChild(const std::string &_name, const std::string &_content="", ElementType _type=ElementType::Normal)
Definition: MyGUI_XmlDocument.cpp:236