Otclient  14/8/2020
fontmanager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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 "fontmanager.h"
24 #include "texture.h"
25 
27 #include <framework/otml/otml.h>
28 
30 
32 {
33  m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
34 }
35 
37 {
38  m_fonts.clear();
39  m_defaultFont = nullptr;
40 }
41 
43 {
44  m_fonts.clear();
45  m_defaultFont = BitmapFontPtr(new BitmapFont("emptyfont"));
46 }
47 
48 bool FontManager::importFont(std::string file)
49 {
50  try {
51  file = g_resources.guessFilePath(file, "otfont");
52 
54  OTMLNodePtr fontNode = doc->at("Font");
55 
56  std::string name = fontNode->valueAt("name");
57 
58  // remove any font with the same name
59  for(auto it = m_fonts.begin(); it != m_fonts.end(); ++it) {
60  if((*it)->getName() == name) {
61  m_fonts.erase(it);
62  break;
63  }
64  }
65 
66  BitmapFontPtr font(new BitmapFont(name));
67  font->load(fontNode);
68  m_fonts.push_back(font);
69 
70  // set as default if needed
71  if(!m_defaultFont || fontNode->valueAt<bool>("default", false))
72  m_defaultFont = font;
73 
74  return true;
75  } catch(stdext::exception& e) {
76  g_logger.error(stdext::format("Unable to load font from file '%s': %s", file, e.what()));
77  return false;
78  }
79 }
80 
81 bool FontManager::fontExists(const std::string& fontName)
82 {
83  for(const BitmapFontPtr& font : m_fonts) {
84  if(font->getName() == fontName)
85  return true;
86  }
87  return false;
88 }
89 
90 BitmapFontPtr FontManager::getFont(const std::string& fontName)
91 {
92  // find font by name
93  for(const BitmapFontPtr& font : m_fonts) {
94  if(font->getName() == fontName)
95  return font;
96  }
97 
98  // when not found, fallback to default font
99  g_logger.error(stdext::format("font '%s' not found", fontName));
100  return getDefaultFont();
101 }
BitmapFont::load
void load(const OTMLNodePtr &fontNode)
Load font from otml node.
Definition: bitmapfont.cpp:30
FontManager::fontExists
bool fontExists(const std::string &fontName)
Definition: fontmanager.cpp:81
FontManager::clearFonts
void clearFonts()
Definition: fontmanager.cpp:42
otml.h
OTMLDocument::parse
static OTMLDocumentPtr parse(const std::string &fileName)
Parse OTML from a file.
Definition: otmldocument.cpp:36
g_fonts
FontManager g_fonts
Definition: fontmanager.cpp:29
FontManager::importFont
bool importFont(std::string file)
Definition: fontmanager.cpp:48
resourcemanager.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
BitmapFont
Definition: bitmapfont.h:32
stdext::format
std::string format()
Definition: format.h:82
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
OTMLNode::valueAt
T valueAt(const std::string &childTag)
Definition: otmlnode.h:130
FontManager::FontManager
FontManager()
Definition: fontmanager.cpp:31
g_logger
Logger g_logger
Definition: logger.cpp:35
FontManager::terminate
void terminate()
Definition: fontmanager.cpp:36
fontmanager.h
stdext::exception::what
virtual const char * what() const
Definition: exception.h:37
texture.h
BitmapFontPtr
stdext::shared_object_ptr< BitmapFont > BitmapFontPtr
Definition: declarations.h:51
stdext::shared_object_ptr< OTMLDocument >
FontManager::getFont
BitmapFontPtr getFont(const std::string &fontName)
Definition: fontmanager.cpp:90
FontManager
Definition: fontmanager.h:29
FontManager::getDefaultFont
BitmapFontPtr getDefaultFont()
Definition: fontmanager.h:41
OTMLNode::at
OTMLNodePtr at(const std::string &childTag)
Definition: otmlnode.cpp:70
ResourceManager::guessFilePath
std::string guessFilePath(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:352
stdext::exception
Definition: exception.h:31