MyGUI_ResourceManualFont.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_ResourceManualFont.h"
#include "MyGUI_SkinManager.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_TextureUtility.h"
namespace MyGUI
{
const GlyphInfo* ResourceManualFont::getGlyphInfo(Char _id) const
{
CharMap::const_iterator iter = mCharMap.find(_id);
if (iter != mCharMap.end())
return &iter->second;
return mSubstituteGlyphInfo;
}
void ResourceManualFont::loadTexture()
{
if (mTexture == nullptr)
{
RenderManager& render = RenderManager::getInstance();
mTexture = render.getTexture(mSource);
if (mTexture == nullptr)
{
mTexture = render.createTexture(mSource);
if (mTexture != nullptr)
mTexture->loadFromFile(mSource);
}
}
}
void ResourceManualFont::deserialization(xml::ElementPtr _node, Version _version)
{
Base::deserialization(_node, _version);
xml::ElementEnumerator node = _node->getElementEnumerator();
while (node.next())
{
if (node->getName() == "Property")
{
std::string_view key = node->findAttribute("key");
std::string_view value = node->findAttribute("value");
if (key == "Source")
mSource = value;
else if (key == "DefaultHeight")
mDefaultHeight = utility::parseInt(value);
else if (key == "Shader")
mShader = value;
}
}
loadTexture();
if (mTexture != nullptr)
{
if (!mShader.empty())
mTexture->setShader(mShader);
int textureWidth = mTexture->getWidth();
int textureHeight = mTexture->getHeight();
node = _node->getElementEnumerator();
while (node.next())
{
if (node->getName() == "Codes")
{
xml::ElementEnumerator element = node->getElementEnumerator();
while (element.next("Code"))
{
std::string value;
// описане глифов
if (element->findAttribute("index", value))
{
Char id = 0;
if (value == "cursor")
id = static_cast<Char>(FontCodeType::Cursor);
else if (value == "selected")
id = static_cast<Char>(FontCodeType::Selected);
else if (value == "selected_back")
id = static_cast<Char>(FontCodeType::SelectedBack);
else if (value == "substitute")
id = static_cast<Char>(FontCodeType::NotDefined);
else
id = utility::parseUInt(value);
float advance(utility::parseValue<float>(element->findAttribute("advance")));
FloatPoint bearing(utility::parseValue<FloatPoint>(element->findAttribute("bearing")));
// texture coordinates
FloatCoord coord(utility::parseValue<FloatCoord>(element->findAttribute("coord")));
// glyph size, default to texture coordinate size
std::string sizeString;
FloatSize size(coord.width, coord.height);
if (element->findAttribute("size", sizeString))
{
size = utility::parseValue<FloatSize>(sizeString);
}
if (advance == 0.0f)
advance = size.width;
mCharMap.emplace(
id,
GlyphInfo{
id,
size.width,
size.height,
advance,
bearing.left,
bearing.top,
FloatRect{
coord.left / textureWidth,
coord.top / textureHeight,
coord.right() / textureWidth,
coord.bottom() / textureHeight}});
if (id == FontCodeType::NotDefined)
mSubstituteGlyphInfo = &mCharMap.at(FontCodeType::NotDefined);
}
}
}
}
}
}
ITexture* ResourceManualFont::getTextureFont() const
{
return mTexture;
}
int ResourceManualFont::getDefaultHeight() const
{
return mDefaultHeight;
}
void ResourceManualFont::setSource(std::string_view value)
{
mTexture = nullptr;
mSource = value;
loadTexture();
}
void ResourceManualFont::setShader(std::string_view value)
{
mShader = value;
if (mTexture != nullptr)
mTexture->setShader(mShader);
}
void ResourceManualFont::setTexture(ITexture* texture)
{
mTexture = texture;
mSource.clear();
}
void ResourceManualFont::setDefaultHeight(int value)
{
mDefaultHeight = value;
}
void ResourceManualFont::addGlyphInfo(Char id, const GlyphInfo& info)
{
GlyphInfo& inserted = mCharMap.insert(CharMap::value_type(id, info)).first->second;
if (id == FontCodeType::NotDefined)
mSubstituteGlyphInfo = &inserted;
}
} // 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