MyGUI 3.5.3
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"
12
13#include <pugixml.hpp>
14
15namespace MyGUI
16{
17
18 const GlyphInfo* ResourceManualFont::getGlyphInfo(Char _id) const
19 {
20 CharMap::const_iterator iter = mCharMap.find(_id);
21
22 if (iter != mCharMap.end())
23 return &iter->second;
24
25 return mSubstituteGlyphInfo;
26 }
27
28 void ResourceManualFont::loadTexture()
29 {
30 if (mTexture == nullptr)
31 {
32 RenderManager& render = RenderManager::getInstance();
33 mTexture = render.getTexture(mSource);
34 if (mTexture == nullptr)
35 {
36 mTexture = render.createTexture(mSource);
37 if (mTexture != nullptr)
38 mTexture->loadFromFile(mSource);
39 }
40 }
41 }
42
43 void ResourceManualFont::deserialization(xml::ElementPtr _node, Version _version)
44 {
45 Base::deserialization(_node, _version);
46
47 const bool oldAdvanceFormat = _version <= Version{1, 1};
48 if (oldAdvanceFormat)
49 {
51 Warning,
52 "ResourceManualFont: '" << mResourceName
53 << "' have old format. Recreate with FontEditor or bump version to "
54 "1.2 if your bearingX is always 0");
55 }
56
57 for (auto node : _node.node())
58 {
59 if (node.name() == std::string_view("Property"))
60 {
61 std::string_view key = node.attribute("key").value();
62 std::string_view value = node.attribute("value").value();
63 if (key == "Source")
64 mSource = value;
65 else if (key == "DefaultHeight")
66 mDefaultHeight = utility::parseInt(value);
67 else if (key == "Shader")
68 mShader = value;
69 }
70 }
71
72 loadTexture();
73
74 if (mTexture != nullptr)
75 {
76 if (!mShader.empty())
77 mTexture->setShader(mShader);
78 int textureWidth = mTexture->getWidth();
79 int textureHeight = mTexture->getHeight();
80
81 for (auto node : _node.node())
82 {
83 if (node.name() == std::string_view("Codes"))
84 {
85 for (auto element : node.children("Code"))
86 {
87 std::string_view value{element.attribute("index").value()};
88 if (!value.empty())
89 {
90 Char id = 0;
91 if (value == "cursor")
92 id = static_cast<Char>(FontCodeType::Cursor);
93 else if (value == "selected")
94 id = static_cast<Char>(FontCodeType::Selected);
95 else if (value == "selected_back")
96 id = static_cast<Char>(FontCodeType::SelectedBack);
97 else if (value == "substitute")
98 id = static_cast<Char>(FontCodeType::NotDefined);
99 else
100 id = utility::parseUInt(value);
101
102 FloatPoint bearing(utility::parseValue<FloatPoint>(element.attribute("bearing").value()));
103
104 // texture coordinates
105 FloatCoord coord(utility::parseValue<FloatCoord>(element.attribute("coord").value()));
106
107 // glyph size, default to texture coordinate size
108 std::string_view sizeString{element.attribute("size").value()};
109 FloatSize size(coord.width, coord.height);
110 if (!sizeString.empty())
111 {
112 size = utility::parseValue<FloatSize>(sizeString);
113 }
114
115 std::string_view advanceAttribute{element.attribute("advance").value()};
116 float advance = size.width;
117 if (!advanceAttribute.empty())
118 advance = utility::parseValue<float>(advanceAttribute);
119
120 if (oldAdvanceFormat)
121 advance += bearing.left;
122
123 mCharMap.emplace(
124 id,
125 GlyphInfo{
126 id,
127 size.width,
128 size.height,
129 advance,
130 bearing.left,
131 bearing.top,
132 FloatRect{
133 coord.left / textureWidth,
134 coord.top / textureHeight,
135 coord.right() / textureWidth,
136 coord.bottom() / textureHeight}});
137
138 if (id == FontCodeType::NotDefined)
139 mSubstituteGlyphInfo = &mCharMap.at(FontCodeType::NotDefined);
140
141 for (auto kerningNode : element.children("Kerning"))
142 {
143 Char right = utility::parseUInt(kerningNode.attribute("right").value());
144 float offset = utility::parseFloat(kerningNode.attribute("offset").value());
145 addKerningInfo(id, right, offset);
146 }
147 }
148 }
149 }
150 }
151 }
152 }
153
154 ITexture* ResourceManualFont::getTextureFont() const
155 {
156 return mTexture;
157 }
158
159 int ResourceManualFont::getDefaultHeight() const
160 {
161 return mDefaultHeight;
162 }
163
164 void ResourceManualFont::setSource(std::string_view value)
165 {
166 mTexture = nullptr;
167 mSource = value;
168 loadTexture();
169 }
170
171 void ResourceManualFont::setShader(std::string_view value)
172 {
173 mShader = value;
174 if (mTexture != nullptr)
175 mTexture->setShader(mShader);
176 }
177
178 void ResourceManualFont::setTexture(ITexture* texture)
179 {
180 mTexture = texture;
181 mSource.clear();
182 }
183
184 void ResourceManualFont::setDefaultHeight(int value)
185 {
186 mDefaultHeight = value;
187 }
188
189 void ResourceManualFont::addGlyphInfo(Char id, const GlyphInfo& info)
190 {
191 GlyphInfo& inserted = mCharMap.insert(CharMap::value_type(id, info)).first->second;
192
193 if (id == FontCodeType::NotDefined)
194 mSubstituteGlyphInfo = &inserted;
195 }
196
197 float ResourceManualFont::getKerning(Char _left, Char _right) const
198 {
199 auto iter = mKerningMap.find(std::make_pair(_left, _right));
200 if (iter != mKerningMap.end())
201 return iter->second;
202 return 0.0f;
203 }
204
205 void ResourceManualFont::addKerningInfo(Char _left, Char _right, float _value)
206 {
207 mKerningMap[std::make_pair(_left, _right)] = _value;
208 }
209
210} // namespace MyGUI
#define MYGUI_LOG(level, text)
unsigned int parseUInt(std::string_view _value)
float parseFloat(std::string_view _value)
T parseValue(std::string_view value)
template MYGUI_EXPORT float parseValue< float >(std::string_view)
int parseInt(std::string_view _value)
types::TCoord< float > FloatCoord
Definition MyGUI_Types.h:36
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
unsigned int Char
Definition MyGUI_Types.h:49
types::TSize< float > FloatSize
Definition MyGUI_Types.h:30