MyGUI 3.2.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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