MyGUI  3.2.1
MyGUI_ResourceManualFont.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"
9 #include "MyGUI_SkinManager.h"
10 #include "MyGUI_RenderManager.h"
11 #include "MyGUI_TextureUtility.h"
12 
13 namespace MyGUI
14 {
15 
17  mDefaultHeight(0),
18  mSubstituteGlyphInfo(nullptr),
19  mTexture(nullptr)
20  {
21  }
22 
24  {
25  }
26 
28  {
29  CharMap::iterator iter = mCharMap.find(_id);
30 
31  if (iter != mCharMap.end())
32  return &iter->second;
33 
34  return mSubstituteGlyphInfo;
35  }
36 
37  void ResourceManualFont::loadTexture()
38  {
39  if (mTexture == nullptr)
40  {
42  mTexture = render.getTexture(mSource);
43  if (mTexture == nullptr)
44  {
45  mTexture = render.createTexture(mSource);
46  if (mTexture != nullptr)
47  mTexture->loadFromFile(mSource);
48  }
49  }
50  }
51 
53  {
54  Base::deserialization(_node, _version);
55 
57  while (node.next())
58  {
59  if (node->getName() == "Property")
60  {
61  const std::string& key = node->findAttribute("key");
62  const std::string& value = node->findAttribute("value");
63  if (key == "Source") mSource = value;
64  else if (key == "DefaultHeight") mDefaultHeight = utility::parseInt(value);
65  }
66  }
67 
68  loadTexture();
69 
70  if (mTexture != nullptr)
71  {
72  int textureWidth = mTexture->getWidth();
73  int textureHeight = mTexture->getHeight();
74 
75  node = _node->getElementEnumerator();
76  while (node.next())
77  {
78  if (node->getName() == "Codes")
79  {
81  while (element.next("Code"))
82  {
83  std::string value;
84  // описане глифов
85  if (element->findAttribute("index", value))
86  {
87  Char id = 0;
88  if (value == "cursor")
89  id = static_cast<Char>(FontCodeType::Cursor);
90  else if (value == "selected")
91  id = static_cast<Char>(FontCodeType::Selected);
92  else if (value == "selected_back")
93  id = static_cast<Char>(FontCodeType::SelectedBack);
94  else if (value == "substitute")
95  id = static_cast<Char>(FontCodeType::NotDefined);
96  else
97  id = utility::parseUInt(value);
98 
99  float advance(utility::parseValue<float>(element->findAttribute("advance")));
100  FloatPoint bearing(utility::parseValue<FloatPoint>(element->findAttribute("bearing")));
101 
102  // texture coordinates
103  FloatCoord coord(utility::parseValue<FloatCoord>(element->findAttribute("coord")));
104 
105  // glyph size, default to texture coordinate size
106  std::string sizeString;
107  FloatSize size(coord.width, coord.height);
108  if (element->findAttribute("size", sizeString))
109  {
110  size = utility::parseValue<FloatSize>(sizeString);
111  }
112 
113  if (advance == 0.0f)
114  advance = size.width;
115 
116  GlyphInfo& glyphInfo = mCharMap.insert(CharMap::value_type(id, GlyphInfo(
117  id,
118  size.width,
119  size.height,
120  advance,
121  bearing.left,
122  bearing.top,
123  FloatRect(
124  coord.left / textureWidth,
125  coord.top / textureHeight,
126  coord.right() / textureWidth,
127  coord.bottom() / textureHeight)
128  ))).first->second;
129 
130  if (id == FontCodeType::NotDefined)
131  mSubstituteGlyphInfo = &glyphInfo;
132  }
133  }
134  }
135  }
136  }
137  }
138 
140  {
141  return mTexture;
142  }
143 
145  {
146  return mDefaultHeight;
147  }
148 
149 } // namespace MyGUI