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