MyGUI_FontManager.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_FontManager.h"
#include "MyGUI_FactoryManager.h"
#include "MyGUI_XmlDocument.h"
#include "MyGUI_ResourceManualFont.h"
#include "MyGUI_ResourceTrueTypeFont.h"
namespace MyGUI
{
MYGUI_SINGLETON_DEFINITION(FontManager);
FontManager::FontManager() :
mXmlFontTagName("Font"),
mXmlPropertyTagName("Property"),
mXmlDefaultFontValue("Default"),
mSingletonHolder(this)
{
}
void FontManager::initialise()
{
MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
ResourceManager::getInstance().registerLoadXmlDelegate(mXmlFontTagName) =
newDelegate(this, &FontManager::_load);
const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
FactoryManager::getInstance().registerFactory<ResourceManualFont>(resourceCategory);
FactoryManager::getInstance().registerFactory<ResourceTrueTypeFont>(resourceCategory);
mDefaultName = "Default";
MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
mIsInitialise = true;
}
void FontManager::shutdown()
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
MyGUI::ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlFontTagName);
const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
FactoryManager::getInstance().unregisterFactory<ResourceManualFont>(resourceCategory);
FactoryManager::getInstance().unregisterFactory<ResourceTrueTypeFont>(resourceCategory);
MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
mIsInitialise = false;
}
void FontManager::_load(xml::ElementPtr _node, std::string_view _file, Version _version)
{
#ifndef MYGUI_DONT_USE_OBSOLETE
loadOldFontFormat(_node, _file, _version, mXmlFontTagName);
#endif // MYGUI_DONT_USE_OBSOLETE
xml::ElementEnumerator node = _node->getElementEnumerator();
while (node.next())
{
if (node->getName() == mXmlPropertyTagName)
{
std::string_view key = node->findAttribute("key");
std::string_view value = node->findAttribute("value");
#ifdef MYGUI_USE_FREETYPE
if (key == "Default")
#else
if (key == "DefaultGenerated")
#endif
mDefaultName = value;
}
}
}
void FontManager::setDefaultFont(std::string_view _value)
{
mDefaultName = _value;
}
IFont* FontManager::getByName(std::string_view _name) const
{
IResource* result = nullptr;
//FIXME для совместимости шрифт может иметь имя Default
if (!_name.empty() && _name != mXmlDefaultFontValue)
result = ResourceManager::getInstance().getByName(_name, false);
if (result == nullptr)
{
result = ResourceManager::getInstance().getByName(mDefaultName, false);
if (!_name.empty() && _name != mXmlDefaultFontValue)
{
MYGUI_LOG(Error, "Font '" << _name << "' not found. Replaced with default font.");
}
}
return result ? result->castType<IFont>(false) : nullptr;
}
const std::string& FontManager::getDefaultFont() const
{
return mDefaultName;
}
} // namespace MyGUI
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_FontManager.h
MyGUI_Precompiled.h
MyGUI_ResourceManualFont.h
MyGUI_ResourceTrueTypeFont.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI_XmlDocument.h
MyGUI::FactoryManager::registerFactory
void registerFactory(std::string_view _category, std::string_view _type, Delegate::IDelegate *_delegate)
Definition MyGUI_FactoryManager.cpp:39
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:54
MyGUI::FontManager
Definition MyGUI_FontManager.h:22
MyGUI::FontManager::FontManager
FontManager()
Definition MyGUI_FontManager.cpp:20
MyGUI::FontManager::setDefaultFont
void setDefaultFont(std::string_view _value)
Definition MyGUI_FontManager.cpp:84
MyGUI::FontManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::FontManager::getByName
IFont * getByName(std::string_view _name) const
Definition MyGUI_FontManager.cpp:89
MyGUI::FontManager::getDefaultFont
const std::string & getDefaultFont() const
Definition MyGUI_FontManager.cpp:108
MyGUI::FontManager::initialise
void initialise()
Definition MyGUI_FontManager.cpp:28
MyGUI::FontManager::shutdown
void shutdown()
Definition MyGUI_FontManager.cpp:46
MyGUI::IFont
Definition MyGUI_IFont.h:21
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::IResource
Definition MyGUI_IResource.h:24
MyGUI::MemberObsolete< FontManager >::loadOldFontFormat
void loadOldFontFormat(xml::ElementPtr _node, std::string_view _file, Version _version, std::string_view _tag)
Definition MyGUI_BackwardCompatibility.cpp:657
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:127
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_ResourceManager.cpp:283
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:120
MyGUI::ResourceManager::getByName
IResource * getByName(std::string_view _name, bool _throw=true) const
Definition MyGUI_ResourceManager.cpp:243
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::ResourceManualFont
Definition MyGUI_ResourceManualFont.h:20
MyGUI::ResourceTrueTypeFont
Definition MyGUI_ResourceTrueTypeFont.h:34
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::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100