MyGUI  3.4.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  CharMap::const_iterator iter = mCharMap.find(_id);
26 
27  if (iter != mCharMap.end())
28  return &iter->second;
29 
30  return mSubstituteGlyphInfo;
31  }
32 
33  void ResourceManualFont::loadTexture()
34  {
35  if (mTexture == nullptr)
36  {
38  mTexture = render.getTexture(mSource);
39  if (mTexture == nullptr)
40  {
41  mTexture = render.createTexture(mSource);
42  if (mTexture != nullptr)
43  mTexture->loadFromFile(mSource);
44  }
45  }
46  }
47 
49  {
50  Base::deserialization(_node, _version);
51 
53  while (node.next())
54  {
55  if (node->getName() == "Property")
56  {
57  const std::string& key = node->findAttribute("key");
58  const std::string& value = node->findAttribute("value");
59  if (key == "Source") mSource = value;
60  else if (key == "DefaultHeight") mDefaultHeight = utility::parseInt(value);
61  else if (key == "Shader") mShader = value;
62  }
63  }
64 
65  loadTexture();
66 
67  if (mTexture != nullptr)
68  {
69  if (!mShader.empty())
70  mTexture->setShader(mShader);
71  int textureWidth = mTexture->getWidth();
72  int textureHeight = mTexture->getHeight();
73 
74  node = _node->getElementEnumerator();
75  while (node.next())
76  {
77  if (node->getName() == "Codes")
78  {
80  while (element.next("Code"))
81  {
82  std::string value;
83  // описане глифов
84  if (element->findAttribute("index", value))
85  {
86  Char id = 0;
87  if (value == "cursor")
88  id = static_cast<Char>(FontCodeType::Cursor);
89  else if (value == "selected")
90  id = static_cast<Char>(FontCodeType::Selected);
91  else if (value == "selected_back")
92  id = static_cast<Char>(FontCodeType::SelectedBack);
93  else if (value == "substitute")
94  id = static_cast<Char>(FontCodeType::NotDefined);
95  else
96  id = utility::parseUInt(value);
97 
98  float advance(utility::parseValue<float>(element->findAttribute("advance")));
99  FloatPoint bearing(utility::parseValue<FloatPoint>(element->findAttribute("bearing")));
100 
101  // texture coordinates
102  FloatCoord coord(utility::parseValue<FloatCoord>(element->findAttribute("coord")));
103 
104  // glyph size, default to texture coordinate size
105  std::string sizeString;
106  FloatSize size(coord.width, coord.height);
107  if (element->findAttribute("size", sizeString))
108  {
109  size = utility::parseValue<FloatSize>(sizeString);
110  }
111 
112  if (advance == 0.0f)
113  advance = size.width;
114 
115  GlyphInfo& glyphInfo = mCharMap.insert(CharMap::value_type(id, GlyphInfo(
116  id,
117  size.width,
118  size.height,
119  advance,
120  bearing.left,
121  bearing.top,
122  FloatRect(
123  coord.left / textureWidth,
124  coord.top / textureHeight,
125  coord.right() / textureWidth,
126  coord.bottom() / textureHeight)
127  ))).first->second;
128 
129  if (id == FontCodeType::NotDefined)
130  mSubstituteGlyphInfo = &glyphInfo;
131  }
132  }
133  }
134  }
135  }
136  }
137 
139  {
140  return mTexture;
141  }
142 
144  {
145  return mDefaultHeight;
146  }
147 
148  void ResourceManualFont::setSource(const std::string& value)
149  {
150  mTexture = nullptr;
151  mSource = value;
152  loadTexture();
153  }
154 
155  void ResourceManualFont::setShader(const std::string& value)
156  {
157  mShader = value;
158  if (mTexture != nullptr)
159  mTexture->setShader(mShader);
160  }
161 
163  {
164  mTexture = texture;
165  mSource.clear();
166  }
167 
169  {
170  mDefaultHeight = value;
171  }
172 
174  {
175  GlyphInfo& inserted = mCharMap.insert(CharMap::value_type(id, info)).first->second;
176 
177  if (id == FontCodeType::NotDefined)
178  mSubstituteGlyphInfo = &inserted;
179  }
180 
181 } // namespace MyGUI
void deserialization(xml::ElementPtr _node, Version _version) override
virtual void setShader(const std::string &_shaderName)=0
virtual int getWidth() const =0
virtual void loadFromFile(const std::string &_filename)=0
virtual int getHeight() const =0
virtual ITexture * createTexture(const std::string &_name)=0
static RenderManager & getInstance()
virtual ITexture * getTexture(const std::string &_name)=0
ITexture * getTextureFont() const override
void setShader(const std::string &value)
void addGlyphInfo(Char id, const GlyphInfo &info)
void setSource(const std::string &value)
const GlyphInfo * getGlyphInfo(Char _id) const override
void setTexture(MyGUI::ITexture *texture)
void deserialization(xml::ElementPtr _node, Version _version) override
ElementEnumerator getElementEnumerator()
const std::string & getName() const
bool findAttribute(const std::string &_name, std::string &_value)
int parseInt(const std::string &_value)
unsigned int parseUInt(const std::string &_value)
unsigned int Char
Definition: MyGUI_Types.h:49