refactor font loading

This commit is contained in:
2019-12-01 18:24:59 +01:00
parent 0905827b8d
commit c8bce21b95
30 changed files with 180 additions and 84 deletions

View File

@@ -9,6 +9,32 @@
std::map<std::string, Font> FontManager::m_fonts;
Sampler FontManager::m_sampler;
Font::Font(Font&& other) noexcept
{
w = other.w;
h = other.h;
bounds = other.bounds;
size = other.size;
scale = other.size;
font_tex = std::move(other.font_tex);
path = std::move(other.path);
id = std::move(other.id);
chars = std::move(other.chars);
}
void Font::operator=(Font&& other) noexcept
{
w = other.w;
h = other.h;
bounds = other.bounds;
size = other.size;
scale = other.size;
font_tex = std::move(other.font_tex);
path = std::move(other.path);
id = std::move(other.id);
chars = std::move(other.chars);
}
bool Font::load(const std::string& ttf, int font_size, float font_scale)
{
Asset file;
@@ -59,14 +85,23 @@ void FontManager::init()
m_sampler.create();
}
bool FontManager::load(const std::string& id, const char* ttf, int sz, float scale)
bool FontManager::load(const std::string& id, const std::string& ttf, int sz, float scale)
{
return m_fonts[id].load(ttf, sz, scale);
}
const Font& FontManager::get(const std::string& id)
const Font& FontManager::get(const std::string& name, int size, const std::string& weight, bool italic)
{
return m_fonts[id];
auto id = fmt::format("{}-{}{}-{}", name, weight, italic ? "-italic" : "", size);
auto it = m_fonts.find(id);
if (it == m_fonts.end())
{
LOG("load font %s", id.c_str());
auto path = fmt::format("data/fonts/{}-{}{}.ttf", name, weight, italic ? "-italic" : "");
m_fonts[id].load(path, size, App::I->display_density * App::I->zoom);
return m_fonts[id];
}
return it->second;
}
void FontManager::change_scale(float scale)
@@ -136,10 +171,14 @@ bool TextMesh::create()
return true;
}
void TextMesh::update(const std::string& id, const std::string& text)
void TextMesh::update(const std::string& text, const std::string& font, int size, const std::string& weight, bool italic)
{
font_id = id;
auto& f = FontManager::get(id);
this->font = font;
this->size = size;
this->weight = weight;
this->italic = italic;
auto& f = FontManager::get(font, size, weight, italic);
float spacing = (f.bounds.w - f.bounds.y);
float avg_width = f.bounds.z - f.bounds.x;
cur_box = glm::vec4(0, f.bounds.y, 5, spacing);
@@ -227,7 +266,7 @@ void TextMesh::update(const std::string& id, const std::string& text)
void TextMesh::draw()
{
auto& f = FontManager::get(font_id);
auto& f = FontManager::get(font, size, weight, italic);
if (f.font_tex.ready())
{
glActiveTexture(GL_TEXTURE0);