/
FontManager.cpp
148 lines (112 loc) · 3.16 KB
/
FontManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "FontManager.h"
#include "ifilesystem.h"
#include "ishaders.h"
#include "itextstream.h"
#include "iregistry.h"
#include "igame.h"
#include "os/path.h"
#include "module/StaticModule.h"
#include "xmlutil/MissingXMLNodeException.h"
#include "FontLoader.h"
namespace fonts
{
namespace
{
const char* MISSING_BASEPATH_NODE =
"Failed to find \"/game/filesystem/fonts/basepath\" node \
in game descriptor";
const char* MISSING_EXTENSION_NODE =
"Failed to find \"/game/filesystem/fonts/extension\" node \
in game descriptor";
}
FontManager::FontManager() :
_loader(std::bind(&FontManager::loadFonts, this)),
_curLanguage("english")
{}
const std::string& FontManager::getName() const
{
static std::string _name(MODULE_FONTMANAGER);
return _name;
}
const StringSet& FontManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_GAMEMANAGER);
_dependencies.insert(MODULE_SHADERSYSTEM);
}
return _dependencies;
}
void FontManager::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called" << std::endl;
// Find installed fonts in a new thread
_loader.start();
}
void FontManager::shutdownModule()
{
_loader.reset();
_fonts.clear();
}
const std::string& FontManager::getCurLanguage()
{
return _curLanguage;
}
void FontManager::ensureFontsLoaded()
{
_loader.ensureFinished();
}
void FontManager::loadFonts()
{
_fonts.clear();
xml::NodeList nlBasePath = GlobalGameManager().currentGame()->getLocalXPath("/filesystem/fonts/basepath");
if (nlBasePath.empty())
{
throw xml::MissingXMLNodeException(MISSING_BASEPATH_NODE);
}
xml::NodeList nlExt = GlobalGameManager().currentGame()->getLocalXPath("/filesystem/fonts/extension");
if (nlExt.empty())
{
throw xml::MissingXMLNodeException(MISSING_EXTENSION_NODE);
}
// TODO: Get the language from the registry
_curLanguage = "english";
// Load the DAT files from the VFS
std::string path = os::standardPathWithSlash(nlBasePath[0].getContent()) + _curLanguage + "/";
std::string extension = nlExt[0].getContent();
// Instantiate a visitor to traverse the VFS
FontLoader loader(path, *this);
GlobalFileSystem().forEachFile(path, extension, loader, 2);
rMessage() << _fonts.size() << " fonts registered." << std::endl;
}
void FontManager::reloadFonts()
{
_loader.reset();
_loader.start();
}
IFontInfoPtr FontManager::findFontInfo(const std::string& name)
{
ensureFontsLoaded();
FontMap::const_iterator found = _fonts.find(name);
return found != _fonts.end() ? found->second : FontInfoPtr();
}
FontInfoPtr FontManager::findOrCreateFontInfo(const std::string& name)
{
FontMap::iterator i = _fonts.find(name);
if (i == _fonts.end())
{
FontInfoPtr font(new FontInfo(name, _curLanguage));
// Doesn't exist yet, create now
std::pair<FontMap::iterator, bool> result = _fonts.insert(
FontMap::value_type(name, font)
);
i = result.first;
}
return i->second;
}
// Static module instance
module::StaticModuleRegistration<FontManager> fontManagerModule;
} // namespace fonts