MyGUI  3.0.1
MyGUI_ResourceManualFont.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"
25 #include "MyGUI_SkinManager.h"
26 #include "MyGUI_RenderManager.h"
27 #include "MyGUI_TextureUtility.h"
28 
29 namespace MyGUI
30 {
31 
33  mDefaultHeight(0),
34  mTexture(nullptr)
35  {
36  }
37 
39  {
40  }
41 
43  {
44  for (VectorRangeInfo::iterator iter=mVectorRangeInfo.begin(); iter!=mVectorRangeInfo.end(); ++iter)
45  {
46  GlyphInfo * info = iter->getInfo(_id);
47  if (info == nullptr) continue;
48  return info;
49  }
50  // при ошибках возвращаем пробел
51  return &mSpaceGlyphInfo;
52  }
53 
54  void ResourceManualFont::checkTexture()
55  {
56  if (mTexture == nullptr)
57  {
59  mTexture = render.getTexture(mSource);
60  if (mTexture == nullptr)
61  {
62  mTexture = render.createTexture(mSource);
63  mTexture->loadFromFile(mSource);
64  }
65  }
66  }
67 
68  void ResourceManualFont::addGlyph(GlyphInfo * _info, Char _index, int _left, int _top, int _right, int _bottom, int _finalw, int _finalh, float _aspect, int _addHeight)
69  {
70  _info->codePoint = _index;
71  _info->uvRect.left = (float)_left / (float)_finalw; // u1
72  _info->uvRect.top = (float)(_top + _addHeight) / (float)_finalh; // v1
73  _info->uvRect.right = (float)( _right ) / (float)_finalw; // u2
74  _info->uvRect.bottom = ( _bottom + _addHeight ) / (float)_finalh; // v2
75  _info->width = _right - _left;
76  }
77 
78  void ResourceManualFont::addGlyph(Char _code, const IntCoord& _coord)
79  {
80  mVectorPairCodeCoord.push_back(PairCodeCoord(_code, _coord));
81  }
82 
83  void ResourceManualFont::initialise()
84  {
85  if (mVectorPairCodeCoord.empty()) return;
86 
87  std::sort(mVectorPairCodeCoord.begin(), mVectorPairCodeCoord.end());
88 
89  const IntSize& size = texture_utility::getTextureSize(mSource);
90  float aspect = (float)size.width / (float)size.height;
91 
92  Char code = mVectorPairCodeCoord.front().code;
93  size_t count = mVectorPairCodeCoord.size();
94  size_t first = 0;
95 
96  for (size_t pos=1; pos<count; ++pos)
97  {
98  // диапазон оборвался
99  if (code + 1 != mVectorPairCodeCoord[pos].code)
100  {
101  addRange(mVectorPairCodeCoord, first, pos-1, size.width, size.height, aspect);
102  code = mVectorPairCodeCoord[pos].code;
103  first = pos;
104  }
105  else
106  {
107  code ++;
108  }
109  }
110 
111  addRange(mVectorPairCodeCoord, first, count-1, size.width, size.height, aspect);
112 
113  // уничтожаем буфер
114  VectorPairCodeCoord tmp;
115  std::swap(tmp, mVectorPairCodeCoord);
116 
117  checkTexture();
118  }
119 
120  void ResourceManualFont::addRange(VectorPairCodeCoord& _info, size_t _first, size_t _last, int _width, int _height, float _aspect)
121  {
122  RangeInfo range = RangeInfo(_info[_first].code, _info[_last].code);
123 
124  for (size_t pos=_first; pos<=_last; ++pos)
125  {
126  GlyphInfo * info = range.getInfo(_info[pos].code);
127  const IntCoord& coord = _info[pos].coord;
128  addGlyph(info, _info[pos].code, coord.left, coord.top, coord.right(), coord.bottom(), _width, _height, _aspect);
129 
130  if (_info[pos].code == FontCodeType::Space)
131  mSpaceGlyphInfo = *info;
132  }
133 
134  mVectorRangeInfo.push_back(range);
135  }
136 
138  {
139  Base::deserialization(_node, _version);
140 
142  while (node.next())
143  {
144  if (node->getName() == "Property")
145  {
146  const std::string& key = node->findAttribute("key");
147  const std::string& value = node->findAttribute("value");
148  if (key == "Source") mSource = value;
149  else if (key == "DefaultHeight") mDefaultHeight = utility::parseInt(value);
150  }
151  else if (node->getName() == "Codes")
152  {
154  while (range.next("Code"))
155  {
156  std::string range_value;
157  std::vector<std::string> parse_range;
158  // описане глифов
159  if (range->findAttribute("index", range_value))
160  {
161  Char id = 0;
162  if (range_value == "cursor")
164  else if (range_value == "selected")
166  else if (range_value == "selected_back")
168  else
169  id = utility::parseUInt(range_value);
170 
171  addGlyph(id, utility::parseValue<IntCoord>(range->findAttribute("coord")));
172  }
173  }
174  }
175  }
176 
177  // инициализируем
178  initialise();
179  }
180 
181 } // namespace MyGUI