2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#include "fontmanager.h"
|
2012-07-29 05:34:40 +02:00
|
|
|
#include "texture.h"
|
2011-08-14 04:09:11 +02:00
|
|
|
|
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;
|
|
|
|
|
2011-12-05 19:27:07 +01:00
|
|
|
FontManager::FontManager()
|
|
|
|
{
|
2012-06-17 01:19:43 +02:00
|
|
|
m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
|
2011-12-05 19:27:07 +01:00
|
|
|
}
|
|
|
|
|
2012-06-18 10:13:52 +02:00
|
|
|
void FontManager::terminate()
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
|
|
|
m_fonts.clear();
|
2012-06-18 10:13:52 +02:00
|
|
|
m_defaultFont = nullptr;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 10:46:49 +02:00
|
|
|
void FontManager::clearFonts()
|
|
|
|
{
|
|
|
|
m_fonts.clear();
|
|
|
|
m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
bool FontManager::importFont(std::string fontFile)
|
|
|
|
{
|
|
|
|
try {
|
2012-08-01 09:49:09 +02:00
|
|
|
if(!stdext::ends_with(fontFile, ".otfont"))
|
2011-08-14 04:09:11 +02:00
|
|
|
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");
|
2012-02-06 20:19:47 +01:00
|
|
|
|
|
|
|
// remove any font with the same name
|
|
|
|
for(auto it = m_fonts.begin(); it != m_fonts.end(); ++it) {
|
|
|
|
if((*it)->getName() == name) {
|
|
|
|
m_fonts.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-06-17 01:19:43 +02:00
|
|
|
BitmapFontPtr font(new BitmapFont(name));
|
2011-08-14 04:09:11 +02:00
|
|
|
font->load(fontNode);
|
|
|
|
m_fonts.push_back(font);
|
|
|
|
|
|
|
|
// set as default if needed
|
|
|
|
if(!m_defaultFont)
|
|
|
|
m_defaultFont = font;
|
|
|
|
|
|
|
|
return true;
|
2012-05-28 15:06:26 +02:00
|
|
|
} catch(stdext::exception& e) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error(stdext::format("Unable to load font from file '%s': %s", fontFile, e.what()));
|
2011-08-14 04:09:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontManager::fontExists(const std::string& fontName)
|
|
|
|
{
|
2012-06-17 01:19:43 +02:00
|
|
|
for(const BitmapFontPtr& font : m_fonts) {
|
2011-08-14 04:09:11 +02:00
|
|
|
if(font->getName() == fontName)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-17 01:19:43 +02:00
|
|
|
BitmapFontPtr FontManager::getFont(const std::string& fontName)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
|
|
|
// find font by name
|
2012-06-17 01:19:43 +02:00
|
|
|
for(const BitmapFontPtr& font : m_fonts) {
|
2011-08-14 04:09:11 +02:00
|
|
|
if(font->getName() == fontName)
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when not found, fallback to default font
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error(stdext::format("font '%s' not found", fontName));
|
2011-08-14 04:09:11 +02:00
|
|
|
return getDefaultFont();
|
|
|
|
}
|
|
|
|
|