• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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_Common.h"
00025 #include "MyGUI_FactoryManager.h"
00026 #include "MyGUI_FontManager.h"
00027 #include "MyGUI_XmlDocument.h"
00028 
00029 #include "MyGUI_ResourceManualFont.h"
00030 #include "MyGUI_ResourceTrueTypeFont.h"
00031 
00032 namespace MyGUI
00033 {
00034     const std::string XML_TYPE("Font");
00035     const std::string XML_TYPE_RESOURCE("Resource");
00036     const std::string XML_TYPE_PROPERTY("Property");
00037     const std::string RESOURCE_DEFAULT_NAME("Default");
00038 
00039     MYGUI_INSTANCE_IMPLEMENT(FontManager);
00040 
00041     void FontManager::initialise()
00042     {
00043         MYGUI_ASSERT(false == mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
00044         MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
00045 
00046         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &FontManager::_load);
00047 
00048         FactoryManager::getInstance().registryFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00049         FactoryManager::getInstance().registryFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00050 
00051         //FIXME для совместимости
00052         //mDefaultName = "font_DejaVuSans.17";
00053 
00054         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
00055         mIsInitialise = true;
00056     }
00057 
00058     void FontManager::shutdown()
00059     {
00060         if (false == mIsInitialise) return;
00061         MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
00062 
00063         MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00064 
00065         FactoryManager::getInstance().unregistryFactory<ResourceManualFont>(XML_TYPE_RESOURCE);
00066         FactoryManager::getInstance().unregistryFactory<ResourceTrueTypeFont>(XML_TYPE_RESOURCE);
00067 
00068         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
00069         mIsInitialise = false;
00070     }
00071 
00072     bool FontManager::load(const std::string& _file)
00073     {
00074         return MyGUI::ResourceManager::getInstance()._loadImplement(_file, true, XML_TYPE, INSTANCE_TYPE_NAME);
00075     }
00076 
00077     void FontManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00078     {
00079         xml::ElementEnumerator font = _node->getElementEnumerator();
00080         while (font.next())
00081         {
00082             /*if (font->getName() == XML_TYPE)
00083             {
00084                 std::string name;
00085                 if (false == font->findAttribute("name", name)) continue;
00086 
00087                 std::string type;
00088                 if (type.empty())
00089                 {
00090                     if (font->findAttribute("resolution").empty()) type = "ResourceManualFont";
00091                     else type = "ResourceTrueTypeFont";
00092                 }
00093 
00094                 xml::Document doc;
00095                 xml::ElementPtr root = doc.createRoot("MyGUI");
00096                 xml::ElementPtr node = root->createChild("Resource");
00097                 node->addAttribute("type", type);
00098                 node->addAttribute("name", name);
00099 
00100                 std::string tmp;
00101                 if (font->findAttribute("source", tmp))
00102                 {
00103                     xml::ElementPtr prop = node->createChild("Property");
00104                     prop->addAttribute("key", "Source");
00105                     prop->addAttribute("value", tmp);
00106                 }
00107                 
00108                 if (font->findAttribute("size", tmp))
00109                 {
00110                     xml::ElementPtr prop = node->createChild("Property");
00111                     prop->addAttribute("key", "Size");
00112                     prop->addAttribute("value", tmp);
00113                 }
00114 
00115                 if (font->findAttribute("resolution", tmp))
00116                 {
00117                     xml::ElementPtr prop = node->createChild("Property");
00118                     prop->addAttribute("key", "Resolution");
00119                     prop->addAttribute("value", tmp);
00120                 }
00121 
00122                 if (font->findAttribute("antialias_colour", tmp))
00123                 {
00124                     xml::ElementPtr prop = node->createChild("Property");
00125                     prop->addAttribute("key", "Antialias");
00126                     prop->addAttribute("value", tmp);
00127                 }
00128 
00129                 if (font->findAttribute("space_width", tmp))
00130                 {
00131                     xml::ElementPtr prop = node->createChild("Property");
00132                     prop->addAttribute("key", "SpaceWidth");
00133                     prop->addAttribute("value", tmp);
00134                 }
00135 
00136                 if (font->findAttribute("tab_width", tmp))
00137                 {
00138                     xml::ElementPtr prop = node->createChild("Property");
00139                     prop->addAttribute("key", "TabWidth");
00140                     prop->addAttribute("value", tmp);
00141                 }
00142 
00143                 if (font->findAttribute("cursor_width", tmp))
00144                 {
00145                     xml::ElementPtr prop = node->createChild("Property");
00146                     prop->addAttribute("key", "CursorWidth");
00147                     prop->addAttribute("value", tmp);
00148                 }
00149 
00150                 if (font->findAttribute("distance", tmp))
00151                 {
00152                     xml::ElementPtr prop = node->createChild("Property");
00153                     prop->addAttribute("key", "Distance");
00154                     prop->addAttribute("value", tmp);
00155                 }
00156 
00157                 if (font->findAttribute("offset_height", tmp))
00158                 {
00159                     xml::ElementPtr prop = node->createChild("Property");
00160                     prop->addAttribute("key", "OffsetHeight");
00161                     prop->addAttribute("value", tmp);
00162                 }
00163 
00164                 if (font->findAttribute("default_height", tmp))
00165                 {
00166                     xml::ElementPtr prop = node->createChild("Property");
00167                     prop->addAttribute("key", "DefaultHeight");
00168                     prop->addAttribute("value", tmp);
00169                 }
00170 
00171                 xml::ElementPtr codes = node->createChild("Codes");
00172 
00173                 xml::ElementEnumerator codeold = font->getElementEnumerator();
00174                 while (codeold.next("Code"))
00175                 {
00176                     xml::ElementPtr codenew = codes->createChild("Code");
00177 
00178                     if (codeold->findAttribute("range", tmp))
00179                         codenew->addAttribute("range", tmp);
00180 
00181                     if (codeold->findAttribute("hide", tmp))
00182                         codenew->addAttribute("hide", tmp);
00183 
00184                     if (codeold->findAttribute("index", tmp))
00185                         codenew->addAttribute("index", tmp);
00186 
00187                     if (codeold->findAttribute("coord", tmp))
00188                         codenew->addAttribute("coord", tmp);
00189                 }
00190 
00191                 ResourceManager::getInstance()._load(root, _file, _version);
00192             }
00193             else */if (font->getName() == XML_TYPE_PROPERTY)
00194             {
00195                 const std::string& key = font->findAttribute("key");
00196                 const std::string& value = font->findAttribute("value");
00197                 if (key == "Default")
00198                     mDefaultName = value;
00199             }
00200         }
00201     }
00202 
00203     void FontManager::setDefaultFont(const std::string& _value)
00204     {
00205         mDefaultName = _value;
00206     }
00207 
00208     IFont* FontManager::getByName(const std::string& _name)
00209     {
00210         IResource* result = nullptr;
00211         //FIXME для совместимости шрифт может иметь имя Default
00212         if (!_name.empty() && _name != RESOURCE_DEFAULT_NAME)
00213             result = ResourceManager::getInstance().getByName(_name, false);
00214 
00215         if (result == nullptr)
00216             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00217 
00218         return result ? result->castType<IFont>(false) : nullptr;
00219     }
00220 
00221 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1