add: Font registry
This commit is contained in:
57
frame/src/font/FontRegistry.cpp
Normal file
57
frame/src/font/FontRegistry.cpp
Normal 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
|
||||
Reference in New Issue
Block a user