MyGUI  3.0.1
MyGUI_FontManager.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_FactoryManager.h"
25 #include "MyGUI_FontManager.h"
26 #include "MyGUI_XmlDocument.h"
27 
30 
31 namespace MyGUI
32 {
33  const std::string XML_TYPE("Font");
34  const std::string XML_TYPE_RESOURCE("Resource");
35  const std::string XML_TYPE_PROPERTY("Property");
36  const std::string RESOURCE_DEFAULT_NAME("Default");
37 
38  MYGUI_INSTANCE_IMPLEMENT( FontManager )
39 
40  void FontManager::initialise()
41  {
42  MYGUI_ASSERT(!mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
43  MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
44 
46 
49 
50  mDefaultName = "Default";
51 
52  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
53  mIsInitialise = true;
54  }
55 
57  {
58  if (!mIsInitialise) return;
59  MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
60 
61  MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
62 
65 
66  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
67  mIsInitialise = false;
68  }
69 
70  bool FontManager::load(const std::string& _file)
71  {
72  return MyGUI::ResourceManager::getInstance()._loadImplement(_file, true, XML_TYPE, INSTANCE_TYPE_NAME);
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 
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()._load(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  result = ResourceManager::getInstance().getByName(mDefaultName, false);
215 
216  return result ? result->castType<IFont>(false) : nullptr;
217  }
218 
219 } // namespace MyGUI