MyGUI  3.2.1
MyGUI_ResourceManualFont.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_ResourceManualFont.h"
00009 #include "MyGUI_SkinManager.h"
00010 #include "MyGUI_RenderManager.h"
00011 #include "MyGUI_TextureUtility.h"
00012 
00013 namespace MyGUI
00014 {
00015 
00016     ResourceManualFont::ResourceManualFont() :
00017         mDefaultHeight(0),
00018         mSubstituteGlyphInfo(nullptr),
00019         mTexture(nullptr)
00020     {
00021     }
00022 
00023     ResourceManualFont::~ResourceManualFont()
00024     {
00025     }
00026 
00027     GlyphInfo* ResourceManualFont::getGlyphInfo(Char _id)
00028     {
00029         CharMap::iterator iter = mCharMap.find(_id);
00030 
00031         if (iter != mCharMap.end())
00032             return &iter->second;
00033 
00034         return mSubstituteGlyphInfo;
00035     }
00036 
00037     void ResourceManualFont::loadTexture()
00038     {
00039         if (mTexture == nullptr)
00040         {
00041             RenderManager& render = RenderManager::getInstance();
00042             mTexture = render.getTexture(mSource);
00043             if (mTexture == nullptr)
00044             {
00045                 mTexture = render.createTexture(mSource);
00046                 if (mTexture != nullptr)
00047                     mTexture->loadFromFile(mSource);
00048             }
00049         }
00050     }
00051 
00052     void ResourceManualFont::deserialization(xml::ElementPtr _node, Version _version)
00053     {
00054         Base::deserialization(_node, _version);
00055 
00056         xml::ElementEnumerator node = _node->getElementEnumerator();
00057         while (node.next())
00058         {
00059             if (node->getName() == "Property")
00060             {
00061                 const std::string& key = node->findAttribute("key");
00062                 const std::string& value = node->findAttribute("value");
00063                 if (key == "Source") mSource = value;
00064                 else if (key == "DefaultHeight") mDefaultHeight = utility::parseInt(value);
00065             }
00066         }
00067 
00068         loadTexture();
00069 
00070         if (mTexture != nullptr)
00071         {
00072             int textureWidth = mTexture->getWidth();
00073             int textureHeight = mTexture->getHeight();
00074 
00075             node = _node->getElementEnumerator();
00076             while (node.next())
00077             {
00078                 if (node->getName() == "Codes")
00079                 {
00080                     xml::ElementEnumerator element = node->getElementEnumerator();
00081                     while (element.next("Code"))
00082                     {
00083                         std::string value;
00084                         // описане глифов
00085                         if (element->findAttribute("index", value))
00086                         {
00087                             Char id = 0;
00088                             if (value == "cursor")
00089                                 id = static_cast<Char>(FontCodeType::Cursor);
00090                             else if (value == "selected")
00091                                 id = static_cast<Char>(FontCodeType::Selected);
00092                             else if (value == "selected_back")
00093                                 id = static_cast<Char>(FontCodeType::SelectedBack);
00094                             else if (value == "substitute")
00095                                 id = static_cast<Char>(FontCodeType::NotDefined);
00096                             else
00097                                 id = utility::parseUInt(value);
00098 
00099                             float advance(utility::parseValue<float>(element->findAttribute("advance")));
00100                             FloatPoint bearing(utility::parseValue<FloatPoint>(element->findAttribute("bearing")));
00101 
00102                             // texture coordinates
00103                             FloatCoord coord(utility::parseValue<FloatCoord>(element->findAttribute("coord")));
00104 
00105                             // glyph size, default to texture coordinate size
00106                             std::string sizeString;
00107                             FloatSize size(coord.width, coord.height);
00108                             if (element->findAttribute("size", sizeString))
00109                             {
00110                                 size = utility::parseValue<FloatSize>(sizeString);
00111                             }
00112 
00113                             if (advance == 0.0f)
00114                                 advance = size.width;
00115 
00116                             GlyphInfo& glyphInfo = mCharMap.insert(CharMap::value_type(id, GlyphInfo(
00117                                 id,
00118                                 size.width,
00119                                 size.height,
00120                                 advance,
00121                                 bearing.left,
00122                                 bearing.top,
00123                                 FloatRect(
00124                                     coord.left / textureWidth,
00125                                     coord.top / textureHeight,
00126                                     coord.right() / textureWidth,
00127                                     coord.bottom() / textureHeight)
00128                                 ))).first->second;
00129 
00130                             if (id == FontCodeType::NotDefined)
00131                                 mSubstituteGlyphInfo = &glyphInfo;
00132                         }
00133                     }
00134                 }
00135             }
00136         }
00137     }
00138 
00139     ITexture* ResourceManualFont::getTextureFont()
00140     {
00141         return mTexture;
00142     }
00143 
00144     int ResourceManualFont::getDefaultHeight()
00145     {
00146         return mDefaultHeight;
00147     }
00148 
00149     void ResourceManualFont::setSource(const std::string& value)
00150     {
00151         mTexture = nullptr;
00152         mSource = value;
00153         loadTexture();
00154     }
00155 
00156     void ResourceManualFont::setDefaultHeight(int value)
00157     {
00158         mDefaultHeight = value;
00159     }
00160 
00161     void ResourceManualFont::addGlyphInfo(Char id, const GlyphInfo& info)
00162     {
00163         GlyphInfo& inserted = mCharMap.insert(CharMap::value_type(id, info)).first->second;
00164 
00165         if (id == FontCodeType::NotDefined)
00166             mSubstituteGlyphInfo = &inserted;
00167     }
00168 
00169 } // namespace MyGUI