1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "atlas.h"
24#include "fontmanager.h"
25#include "texture.h"
26
27#include <framework/core/eventdispatcher.h>
28#include <framework/core/resourcemanager.h>
29#include <framework/otml/otml.h>
30
31FontManager g_fonts;
32
33FontManager::FontManager()
34{
35 m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
36}
37
38void FontManager::terminate()
39{
40 m_fonts.clear();
41 m_defaultFont = nullptr;
42}
43
44void FontManager::clearFonts()
45{
46 m_fonts.clear();
47 m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
48}
49
50void FontManager::importFont(std::string file)
51{
52 if (g_graphicsThreadId != std::this_thread::get_id()) {
53 g_graphicsDispatcher.addEvent(std::bind(&FontManager::importFont, this, file));
54 return;
55 }
56 try {
57 file = g_resources.guessFilePath(file, "otfont");
58
59 OTMLDocumentPtr doc = OTMLDocument::parse(file);
60 OTMLNodePtr fontNode = doc->at("Font");
61
62 std::string name = fontNode->valueAt("name");
63 if (fontExists(name))
64 return;
65
66 // remove any font with the same name
67 for(auto it = m_fonts.begin(); it != m_fonts.end(); ++it) {
68 if((*it)->getName() == name) {
69 m_fonts.erase(it);
70 break;
71 }
72 }
73
74 BitmapFontPtr font(new BitmapFont(name));
75 font->load(fontNode);
76 m_fonts.push_back(font);
77
78 // set as default if needed
79 if(!m_defaultFont || fontNode->valueAt<bool>("default", false))
80 m_defaultFont = font;
81
82 } catch(stdext::exception& e) {
83 g_logger.error(stdext::format("Unable to load font from file '%s': %s", file, e.what()));
84 }
85}
86
87bool FontManager::fontExists(const std::string& fontName)
88{
89 for(const BitmapFontPtr& font : m_fonts) {
90 if(font->getName() == fontName)
91 return true;
92 }
93 return false;
94}
95
96BitmapFontPtr FontManager::getFont(const std::string& fontName)
97{
98 // find font by name
99 for(const BitmapFontPtr& font : m_fonts) {
100 if(font->getName() == fontName)
101 return font;
102 }
103
104 // when not found, fallback to default font
105 g_logger.error(stdext::format("font '%s' not found", fontName));
106 return getDefaultFont();
107}
108