MyGUI  3.2.0
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 
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 
41  mIsInitialise(false)
42  {
43  }
44 
46  {
47  MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
48  MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
49 
51 
54 
55  mDefaultName = "Default";
56 
57  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
58  mIsInitialise = true;
59  }
60 
62  {
63  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
64  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
65 
67 
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  {
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
const std::string RESOURCE_DEFAULT_NAME("Default")
void unregisterLoadXmlDelegate(const std::string &_key)
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Element * ElementPtr
delegates::IDelegate0 * newDelegate(void(*_func)())
static ResourceManager & getInstance()
void loadFromXmlNode(xml::ElementPtr _node, const std::string &_file, Version _version)
bool findAttribute(const std::string &_name, std::string &_value)
static const char * getClassTypeName()
const std::string XML_TYPE("Font")
IResource * getByName(const std::string &_name, bool _throw=true) const
#define MYGUI_LOG(level, text)
const std::string & getDefaultFont() const
#define MYGUI_ASSERT(exp, dest)
const std::string XML_TYPE_PROPERTY("Property")
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
void unregisterFactory(const std::string &_category, const std::string &_type)
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
ElementEnumerator getElementEnumerator()
IFont * getByName(const std::string &_name) const
const std::string & getName() const
void setDefaultFont(const std::string &_value)
static const char * mClassTypeName
void addAttribute(const std::string &_key, const T &_value)
const std::string XML_TYPE_RESOURCE("Resource")
ElementPtr createChild(const std::string &_name, const std::string &_content="", ElementType _type=ElementType::Normal)