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:
4
main.cpp
4
main.cpp
@@ -4,6 +4,7 @@
|
||||
#include <JGL/Colors.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <JGL/Font.h>
|
||||
#include "jlog/jlog.hpp"
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL;
|
||||
@@ -119,6 +120,9 @@ public:
|
||||
J2D::OutlineTriangle(Colors::Blue, {{100, 275}, {0, 275}, {100, 375}});
|
||||
J2D::DrawGradientLine(JGL::Colors::Red, JGL::Colors::Blue, {105, 375}, {200, 275}, 2);
|
||||
|
||||
auto result = Jupiteroid->MeasureString("Jupiteroid Font", 16);
|
||||
DEBUG(std::format("Result: {},{}", result.x, result.y ));
|
||||
|
||||
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 16, 1.f, 16, *Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 33, 1,16, *Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 50, 1,16, *Jupiteroid);
|
||||
|
@@ -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