2011-08-14 04:09:11 +02:00
|
|
|
#include "fontmanager.h"
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include <framework/core/resourcemanager.h>
|
|
|
|
#include <framework/otml/otml.h>
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
FontManager g_fonts;
|
|
|
|
|
|
|
|
void FontManager::releaseFonts()
|
|
|
|
{
|
|
|
|
m_defaultFont.reset();
|
|
|
|
m_fonts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontManager::importFont(std::string fontFile)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if(!boost::ends_with(fontFile, ".otfont"))
|
|
|
|
fontFile += ".otfont";
|
|
|
|
|
|
|
|
OTMLDocumentPtr doc = OTMLDocument::parse(fontFile);
|
|
|
|
OTMLNodePtr fontNode = doc->at("Font");
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
std::string name = fontNode->valueAt("name");
|
2011-08-14 04:09:11 +02:00
|
|
|
if(fontExists(name))
|
2011-08-19 20:53:23 +02:00
|
|
|
throw std::runtime_error("a font with the same name is already imported, did you duplicate font names?");
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
FontPtr font(new Font(name));
|
|
|
|
font->load(fontNode);
|
|
|
|
m_fonts.push_back(font);
|
|
|
|
|
|
|
|
// set as default if needed
|
|
|
|
if(!m_defaultFont)
|
|
|
|
m_defaultFont = font;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch(std::exception& e) {
|
2011-08-19 20:53:23 +02:00
|
|
|
logError("ERROR: could not load font from '", fontFile, "': ", e.what());
|
2011-08-14 04:09:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontManager::fontExists(const std::string& fontName)
|
|
|
|
{
|
|
|
|
for(const FontPtr& font : m_fonts) {
|
|
|
|
if(font->getName() == fontName)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPtr FontManager::getFont(const std::string& fontName)
|
|
|
|
{
|
|
|
|
// find font by name
|
|
|
|
for(const FontPtr& font : m_fonts) {
|
|
|
|
if(font->getName() == fontName)
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when not found, fallback to default font
|
|
|
|
return getDefaultFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPtr FontManager::getDefaultFont()
|
|
|
|
{
|
|
|
|
// default font should always exists, otherwise the app may crash
|
|
|
|
if(!m_defaultFont)
|
2011-08-15 16:06:15 +02:00
|
|
|
logFatal("FATAL ERROR: no default font to display, cannot continue to run");
|
2011-08-14 04:09:11 +02:00
|
|
|
return m_defaultFont;
|
|
|
|
}
|