add: Font registry

This commit is contained in:
Simon Hardt
2022-03-06 20:15:57 +01:00
parent 1772a3baff
commit fe9fc4d69c
9 changed files with 169 additions and 7 deletions

View File

@@ -0,0 +1,57 @@
#include "FontRegistry.hpp"
#include <fmt/format.h>
namespace frame::font
{
std::shared_ptr<Font> GetFont(std::string_view name)
{
return Service::get<FontRegistry>()->Get(name);
}
bool LoadFont(std::string_view name)
{
return Service::get<FontRegistry>()->Load(name);
}
FontRegistry::FontRegistry()
: IService("FontRegistry")
{
}
bool FontRegistry::Load(std::string_view name)
{
if(fonts.find(std::string{name}) != fonts.end())
{
fmt::print("Font {} allready loaded!\n", name);
return true;
} else
{
fmt::print("Loading font \"{}\": ", name);
auto ptr = Font::LoadFromFile(fmt::format("{}.json", name));
if(ptr == nullptr)
{
fmt::print("Error not found!\n");
} else
{
fonts[std::string{name}] = ptr;
fmt::print("OK\n");
return true;
}
}
return false;
}
std::shared_ptr<Font> FontRegistry::Get(std::string_view name)
{
auto it = fonts.find({std::string{name}});
if(it == fonts.end())
{
if(!Load(name))
return nullptr;
it = fonts.find({std::string{name}});
}
return it->second;
}
} // namespace frame::font