Implement Font::MeasureString (TODO: double check!!)
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m53s

This commit is contained in:
2024-07-17 14:59:34 -04:00
parent d28f680cd0
commit 480502f89e
2 changed files with 43 additions and 0 deletions

View File

@@ -112,4 +112,43 @@ namespace JGL
{
return Font(path);
}
Vector2 Font::MeasureString(const std::string &text, float ptSize) {
// TODO: Work in-progress implementation unfortunately.
// This is likely returning slightly incorrect results for likely several reasons.
// Half-ass solution for now ~ dawsh.
FT_BBox glyph_bbox;
Vector2 extents = Vector2(0,0);
for (const char& c : text)
{
FT_GlyphSlot slot = face->glyph;
auto glyph_index = FT_Get_Char_Index(this->face, c);
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error)
continue;
Vector2 advance = {static_cast<float>(slot->advance.x >> 6),
static_cast<float>(slot->advance.y >> 6)};
extents += advance;
}
if (extents.y == 0)
{
extents.y = face->bbox.yMax / 64;
}
return extents;
}
}