Files
Frame/frame/src/font/FontRegistry.cpp
2022-03-06 20:15:57 +01:00

57 lines
1.4 KiB
C++

#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