Implement Font::MeasureString (TODO: double check!!)
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m53s
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m53s
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user