From 3a658b10969b77ac7100128d4db6d1a67c29b525 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 17 Jul 2024 15:19:38 -0400 Subject: [PATCH] Testing MeasureString with underlaid box. But it appears to be offset by half-height? --- main.cpp | 3 +++ src/JGL/Font.cpp | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 252ff42..bfa166b 100644 --- a/main.cpp +++ b/main.cpp @@ -123,6 +123,9 @@ public: auto result = Jupiteroid->MeasureString("Jupiteroid Font", 16); DEBUG(std::format("Result: {},{}", result.x, result.y )); + // TODO: Note the discrepancy in Y between the FillRect and DrawString call here. + J2D::FillRect(JGL::Colors::Gray, {0, 0}, result); + // TODO: Does text render from the middle-point of the glyphs? 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); diff --git a/src/JGL/Font.cpp b/src/JGL/Font.cpp index 5004836..273c4bd 100644 --- a/src/JGL/Font.cpp +++ b/src/JGL/Font.cpp @@ -121,10 +121,11 @@ namespace JGL FT_BBox glyph_bbox; + FT_Set_Pixel_Sizes(this->face, ptSize, ptSize); + Vector2 extents = Vector2(0,0); - for (const char& c : text) - { + for (const char& c : text) { FT_GlyphSlot slot = face->glyph; auto glyph_index = FT_Get_Char_Index(this->face, c); @@ -134,18 +135,25 @@ namespace JGL continue; - - Vector2 advance = {static_cast(slot->advance.x >> 6), static_cast(slot->advance.y >> 6)}; + extents += advance; + + // Gives smaller results than we'd want. + if (extents.y < slot->metrics.height / 64) { + extents.y = slot->metrics.height / 64; + } + + // Just fucking hardcode it, we know the glyph height is roughly always the ptSize anyway. + if (extents.y < ptSize) + { + extents.y = ptSize; + } } - if (extents.y == 0) - { - extents.y = face->bbox.yMax / 64; - } + return extents; }