MyGUI: MyGUI_ResourceManualFont.cpp Source File

MyGUI 3.4.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"
8#include "MyGUI_ResourceManualFont.h"
9#include "MyGUI_SkinManager.h"
10#include "MyGUI_RenderManager.h"
11#include "MyGUI_TextureUtility.h"
12
13namespace MyGUI
14{
15
16 const GlyphInfo* ResourceManualFont::getGlyphInfo(Char _id) const
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 {
30 RenderManager& render = RenderManager::getInstance();
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
41 void ResourceManualFont::deserialization(xml::ElementPtr _node, Version _version)
42 {
43 Base::deserialization(_node, _version);
44
45 xml::ElementEnumerator node = _node->getElementEnumerator();
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 {
75 xml::ElementEnumerator element = node->getElementEnumerator();
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 float advance(utility::parseValue<float>(element->findAttribute("advance")));
95 FloatPoint bearing(utility::parseValue<FloatPoint>(element->findAttribute("bearing")));
96
97 // texture coordinates
98 FloatCoord coord(utility::parseValue<FloatCoord>(element->findAttribute("coord")));
99
100 // glyph size, default to texture coordinate size
101 std::string sizeString;
102 FloatSize size(coord.width, coord.height);
103 if (element->findAttribute("size", sizeString))
104 {
105 size = utility::parseValue<FloatSize>(sizeString);
106 }
107
108 if (advance == 0.0f)
109 advance = size.width;
110
111 mCharMap.emplace(
112 id,
113 GlyphInfo{
114 id,
115 size.width,
116 size.height,
117 advance,
118 bearing.left,
119 bearing.top,
120 FloatRect{
121 coord.left / textureWidth,
122 coord.top / textureHeight,
123 coord.right() / textureWidth,
124 coord.bottom() / textureHeight}});
125
126 if (id == FontCodeType::NotDefined)
127 mSubstituteGlyphInfo = &mCharMap.at(FontCodeType::NotDefined);
128 }
129 }
130 }
131 }
132 }
133 }
134
135 ITexture* ResourceManualFont::getTextureFont() const
136 {
137 return mTexture;
138 }
139
140 int ResourceManualFont::getDefaultHeight() const
141 {
142 return mDefaultHeight;
143 }
144
145 void ResourceManualFont::setSource(std::string_view value)
146 {
147 mTexture = nullptr;
148 mSource = value;
149 loadTexture();
150 }
151
152 void ResourceManualFont::setShader(std::string_view value)
153 {
154 mShader = value;
155 if (mTexture != nullptr)
156 mTexture->setShader(mShader);
157 }
158
159 void ResourceManualFont::setTexture(ITexture* texture)
160 {
161 mTexture = texture;
162 mSource.clear();
163 }
164
165 void ResourceManualFont::setDefaultHeight(int value)
166 {
167 mDefaultHeight = value;
168 }
169
170 void ResourceManualFont::addGlyphInfo(Char id, const GlyphInfo& info)
171 {
172 GlyphInfo& inserted = mCharMap.insert(CharMap::value_type(id, info)).first->second;
173
174 if (id == FontCodeType::NotDefined)
175 mSubstituteGlyphInfo = &inserted;
176 }
177
178} // namespace MyGUI
MyGUI_Precompiled.h
MyGUI_RenderManager.h
MyGUI_ResourceManualFont.h
MyGUI_SkinManager.h
MyGUI_TextureUtility.h
MyGUI::ITexture
Definition MyGUI_ITexture.h:28
MyGUI::ITexture::loadFromFile
virtual void loadFromFile(const std::string &_filename)=0
MyGUI::RenderManager
Definition MyGUI_RenderManager.h:21
MyGUI::RenderManager::getTexture
virtual ITexture * getTexture(const std::string &_name)=0
MyGUI::RenderManager::createTexture
virtual ITexture * createTexture(const std::string &_name)=0
MyGUI::RenderManager::getInstance
static RenderManager & getInstance()
MyGUI::ResourceManualFont::getTextureFont
ITexture * getTextureFont() const override
Definition MyGUI_ResourceManualFont.cpp:135
MyGUI::ResourceManualFont::addGlyphInfo
void addGlyphInfo(Char id, const GlyphInfo &info)
Definition MyGUI_ResourceManualFont.cpp:170
MyGUI::ResourceManualFont::setSource
void setSource(std::string_view value)
Definition MyGUI_ResourceManualFont.cpp:145
MyGUI::ResourceManualFont::setDefaultHeight
void setDefaultHeight(int value)
Definition MyGUI_ResourceManualFont.cpp:165
MyGUI::ResourceManualFont::setShader
void setShader(std::string_view value)
Definition MyGUI_ResourceManualFont.cpp:152
MyGUI::ResourceManualFont::getDefaultHeight
int getDefaultHeight() const override
Definition MyGUI_ResourceManualFont.cpp:140
MyGUI::ResourceManualFont::getGlyphInfo
const GlyphInfo * getGlyphInfo(Char _id) const override
Definition MyGUI_ResourceManualFont.cpp:16
MyGUI::ResourceManualFont::setTexture
void setTexture(MyGUI::ITexture *texture)
Definition MyGUI_ResourceManualFont.cpp:159
MyGUI::ResourceManualFont::deserialization
void deserialization(xml::ElementPtr _node, Version _version) override
Definition MyGUI_ResourceManualFont.cpp:41
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::Element::findAttribute
bool findAttribute(std::string_view _name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:242
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:328
MyGUI::FontCodeType::Selected
@ Selected
Definition MyGUI_FontData.h:30
MyGUI::FontCodeType::NotDefined
@ NotDefined
Definition MyGUI_FontData.h:33
MyGUI::FontCodeType::Cursor
@ Cursor
Definition MyGUI_FontData.h:32
MyGUI::FontCodeType::SelectedBack
@ SelectedBack
Definition MyGUI_FontData.h:31
MyGUI::utility::parseUInt
unsigned int parseUInt(std::string_view _value)
Definition MyGUI_StringUtility.h:107
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI::utility::parseInt
int parseInt(std::string_view _value)
Definition MyGUI_StringUtility.h:102
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::FloatCoord
types::TCoord< float > FloatCoord
Definition MyGUI_Types.h:37
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34
MyGUI::FloatPoint
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:28
MyGUI::Char
unsigned int Char
Definition MyGUI_Types.h:50
MyGUI::FloatSize
types::TSize< float > FloatSize
Definition MyGUI_Types.h:31
MyGUI::GlyphInfo
Definition MyGUI_FontData.h:41
MyGUI::types::TCoord::height
T height
Definition MyGUI_TCoord.h:23
MyGUI::types::TCoord::right
T right() const
Definition MyGUI_TCoord.h:125
MyGUI::types::TCoord::width
T width
Definition MyGUI_TCoord.h:22
MyGUI::types::TCoord::top
T top
Definition MyGUI_TCoord.h:21
MyGUI::types::TCoord::left
T left
Definition MyGUI_TCoord.h:20
MyGUI::types::TCoord::bottom
T bottom() const
Definition MyGUI_TCoord.h:130
MyGUI::types::TPoint::left
T left
Definition MyGUI_TPoint.h:19
MyGUI::types::TPoint::top
T top
Definition MyGUI_TPoint.h:20
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:19
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:20