MyGUI  3.2.1
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 
14 
15 namespace MyGUI
16 {
17 
18  template <> FontManager* Singleton<FontManager>::msInstance = nullptr;
19  template <> const char* Singleton<FontManager>::mClassTypeName = "FontManager";
20 
22  mIsInitialise(false),
23  mXmlFontTagName("Font"),
24  mXmlPropertyTagName("Property"),
25  mXmlDefaultFontValue("Default")
26  {
27  }
28 
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();
39 
40  mDefaultName = "Default";
41 
42  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
43  mIsInitialise = true;
44  }
45 
47  {
48  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
49  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
50 
52 
53  std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
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 
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