MyGUI  3.2.0
MyGUI_FontManager.cpp
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #include "MyGUI_Precompiled.h"
00023 #include "MyGUI_FontManager.h"
00024 #include "MyGUI_FactoryManager.h"
00025 #include "MyGUI_XmlDocument.h"
00026 
00027 #include "MyGUI_ResourceManualFont.h"
00028 #include "MyGUI_ResourceTrueTypeFont.h"
00029 
00030 namespace MyGUI
00031 {
00032     const std::string XML_TYPE("Font");
00033     const std::string XML_TYPE_RESOURCE("Resource");
00034     const std::string XML_TYPE_PROPERTY("Property");
00035     const std::string RESOURCE_DEFAULT_NAME("Default");
00036 
00037     template <> FontManager* Singleton<FontManager>::msInstance = nullptr;
00038     template <> const char* Singleton<FontManager>::mClassTypeName("FontManager");
00039 
00040     FontManager::FontManager() :
00041         mIsInitialise(false)
00042     {
00043     }
00044 
00045     void FontManager::initialise()
00046     {
00047         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00048         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00049 
00050         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &FontManager::_load);
00051 
00052         FactoryManager::getInstance().registerFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00053         FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00054 
00055         mDefaultName = "Default";
00056 
00057         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00058         mIsInitialise = true;
00059     }
00060 
00061     void FontManager::shutdown()
00062     {
00063         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00064         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00065 
00066         MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00067 
00068         FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00069         FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00070 
00071         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00072         mIsInitialise = false;
00073     }
00074 
00075     void FontManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00076     {
00077         xml::ElementEnumerator font = _node->getElementEnumerator();
00078         while (font.next())
00079         {
00080             if (font->getName() == XML_TYPE)
00081             {
00082                 std::string name;
00083                 if (!font->findAttribute("name", name)) continue;
00084 
00085                 std::string type;
00086                 if (type.empty())
00087                 {
00088                     if (font->findAttribute("resolution").empty()) type = "ResourceManualFont";
00089                     else type = "ResourceTrueTypeFont";
00090                 }
00091 
00092                 xml::Document doc;
00093                 xml::ElementPtr root = doc.createRoot("MyGUI");
00094                 xml::ElementPtr node = root->createChild("Resource");
00095                 node->addAttribute("type", type);
00096                 node->addAttribute("name", name);
00097 
00098                 std::string tmp;
00099                 if (font->findAttribute("source", tmp))
00100                 {
00101                     xml::ElementPtr prop = node->createChild("Property");
00102                     prop->addAttribute("key", "Source");
00103                     prop->addAttribute("value", tmp);
00104                 }
00105 
00106                 if (font->findAttribute("size", tmp))
00107                 {
00108                     xml::ElementPtr prop = node->createChild("Property");
00109                     prop->addAttribute("key", "Size");
00110                     prop->addAttribute("value", tmp);
00111                 }
00112 
00113                 if (font->findAttribute("resolution", tmp))
00114                 {
00115                     xml::ElementPtr prop = node->createChild("Property");
00116                     prop->addAttribute("key", "Resolution");
00117                     prop->addAttribute("value", tmp);
00118                 }
00119 
00120                 if (font->findAttribute("antialias_colour", tmp))
00121                 {
00122                     xml::ElementPtr prop = node->createChild("Property");
00123                     prop->addAttribute("key", "Antialias");
00124                     prop->addAttribute("value", tmp);
00125                 }
00126 
00127                 if (font->findAttribute("space_width", tmp))
00128                 {
00129                     xml::ElementPtr prop = node->createChild("Property");
00130                     prop->addAttribute("key", "SpaceWidth");
00131                     prop->addAttribute("value", tmp);
00132                 }
00133 
00134                 if (font->findAttribute("tab_width", tmp))
00135                 {
00136                     xml::ElementPtr prop = node->createChild("Property");
00137                     prop->addAttribute("key", "TabWidth");
00138                     prop->addAttribute("value", tmp);
00139                 }
00140 
00141                 if (font->findAttribute("cursor_width", tmp))
00142                 {
00143                     xml::ElementPtr prop = node->createChild("Property");
00144                     prop->addAttribute("key", "CursorWidth");
00145                     prop->addAttribute("value", tmp);
00146                 }
00147 
00148                 if (font->findAttribute("distance", tmp))
00149                 {
00150                     xml::ElementPtr prop = node->createChild("Property");
00151                     prop->addAttribute("key", "Distance");
00152                     prop->addAttribute("value", tmp);
00153                 }
00154 
00155                 if (font->findAttribute("offset_height", tmp))
00156                 {
00157                     xml::ElementPtr prop = node->createChild("Property");
00158                     prop->addAttribute("key", "OffsetHeight");
00159                     prop->addAttribute("value", tmp);
00160                 }
00161 
00162                 if (font->findAttribute("default_height", tmp))
00163                 {
00164                     xml::ElementPtr prop = node->createChild("Property");
00165                     prop->addAttribute("key", "DefaultHeight");
00166                     prop->addAttribute("value", tmp);
00167                 }
00168 
00169                 xml::ElementPtr codes = node->createChild("Codes");
00170 
00171                 xml::ElementEnumerator codeold = font->getElementEnumerator();
00172                 while (codeold.next("Code"))
00173                 {
00174                     xml::ElementPtr codenew = codes->createChild("Code");
00175 
00176                     if (codeold->findAttribute("range", tmp))
00177                         codenew->addAttribute("range", tmp);
00178 
00179                     if (codeold->findAttribute("hide", tmp))
00180                         codenew->addAttribute("hide", tmp);
00181 
00182                     if (codeold->findAttribute("index", tmp))
00183                         codenew->addAttribute("index", tmp);
00184 
00185                     if (codeold->findAttribute("coord", tmp))
00186                         codenew->addAttribute("coord", tmp);
00187                 }
00188 
00189                 ResourceManager::getInstance().loadFromXmlNode(root, _file, _version);
00190             }
00191             else if (font->getName() == XML_TYPE_PROPERTY)
00192             {
00193                 const std::string& key = font->findAttribute("key");
00194                 const std::string& value = font->findAttribute("value");
00195                 if (key == "Default")
00196                     mDefaultName = value;
00197             }
00198         }
00199     }
00200 
00201     void FontManager::setDefaultFont(const std::string& _value)
00202     {
00203         mDefaultName = _value;
00204     }
00205 
00206     IFont* FontManager::getByName(const std::string& _name) const
00207     {
00208         IResource* result = nullptr;
00209         //FIXME для совместимости шрифт может иметь имя Default
00210         if (!_name.empty() && _name != RESOURCE_DEFAULT_NAME)
00211             result = ResourceManager::getInstance().getByName(_name, false);
00212 
00213         if (result == nullptr)
00214         {
00215             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00216             if (!_name.empty() && _name != RESOURCE_DEFAULT_NAME)
00217             {
00218                 MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
00219             }
00220         }
00221 
00222         return result ? result->castType<IFont>(false) : nullptr;
00223     }
00224 
00225     const std::string& FontManager::getDefaultFont() const
00226     {
00227         return mDefaultName;
00228     }
00229 
00230 } // namespace MyGUI