MyGUI  3.0.1
MyGUI_FontManager.cpp
Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_FactoryManager.h"
00025 #include "MyGUI_FontManager.h"
00026 #include "MyGUI_XmlDocument.h"
00027 
00028 #include "MyGUI_ResourceManualFont.h"
00029 #include "MyGUI_ResourceTrueTypeFont.h"
00030 
00031 namespace MyGUI
00032 {
00033     const std::string XML_TYPE("Font");
00034     const std::string XML_TYPE_RESOURCE("Resource");
00035     const std::string XML_TYPE_PROPERTY("Property");
00036     const std::string RESOURCE_DEFAULT_NAME("Default");
00037 
00038     MYGUI_INSTANCE_IMPLEMENT( FontManager )
00039 
00040     void FontManager::initialise()
00041     {
00042         MYGUI_ASSERT(!mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
00043         MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
00044 
00045         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &FontManager::_load);
00046 
00047         FactoryManager::getInstance().registerFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00048         FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00049 
00050         mDefaultName = "Default";
00051 
00052         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
00053         mIsInitialise = true;
00054     }
00055 
00056     void FontManager::shutdown()
00057     {
00058         if (!mIsInitialise) return;
00059         MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
00060 
00061         MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00062 
00063         FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00064         FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00065 
00066         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
00067         mIsInitialise = false;
00068     }
00069 
00070     bool FontManager::load(const std::string& _file)
00071     {
00072         return MyGUI::ResourceManager::getInstance()._loadImplement(_file, true, XML_TYPE, INSTANCE_TYPE_NAME);
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()._load(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             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00215 
00216         return result ? result->castType<IFont>(false) : nullptr;
00217     }
00218 
00219 } // namespace MyGUI