Make measure string go brrrrrrr
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m9s

This commit is contained in:
2024-07-31 12:01:57 -04:00
parent a5bfb4972a
commit 9dda4bebc5
6 changed files with 34 additions and 11 deletions

View File

@@ -32,7 +32,7 @@ namespace JGL
/// @param text The string to measure.
/// @param ptSize The font size at which to measure.
/// @return The size-in-pixels that would contain the entire text.
Vector2 MeasureString(const std::string& text, float ptSize);
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
public:
int index = 0;
FT_Face face;

View File

@@ -61,3 +61,7 @@ public:
void eraseFont(CachedFont* font);
void purgeCache();
};
namespace JGL {
inline FontCache fontCache;
}

View File

@@ -106,10 +106,12 @@ namespace JGL {
void DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity = 255, Inversion::Inversion inversion = Inversion::None);
///Draws a non axis-aligned fill rect to the screen.
///The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
void FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
///Draws a non axis-aligned outline rect to the screen.
///The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
void OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);

View File

@@ -125,8 +125,8 @@ public:
J3D::End();
J2D::Begin();
J2D::FillQuad(Color4(Colors::Red), {500, 52}, {500, 152}, {600, 152}, {600, 52});
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::FillQuad(Colors::Red, {0, 52}, {100, 52}, {0, 152}, {100, 152});
J2D::DrawSprite(imageID, {0, 52}, {(float) image->getWidth(), (float) image->getHeight()}, 128);
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
@@ -140,7 +140,6 @@ public:
J2D::FillGradientTriangle(Color4(Colors::Red), Color4(Colors::Green), Color4(Colors::Blue), {{0, 275}, {0, 375}, {100, 375}});
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);
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);

View File

@@ -8,6 +8,8 @@
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include <JGL/FontCache.h>
#endif
#if _WIN32
#include <ft2build.h>
@@ -94,7 +96,7 @@ namespace JGL
return Font(path);
}
Vector2 Font::MeasureString(const std::string &text, float ptSize) {
Vector2 Font::MeasureString(const std::string &text, unsigned int ptSize) {
// TODO: Check if a font of that size is in the cache and if so use the info from that because this is sloooooooooooooow.
// That'll make it go vroom vroom.
@@ -102,12 +104,32 @@ namespace JGL
// 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);
bool font_of_size_in_cache = false;
for(const auto& f : fontCache.getFonts()) {
if (f->getFontSize() == ptSize) {
font_of_size_in_cache = true;
break;
}
}
if (font_of_size_in_cache) {
CachedFont* font;
for (auto* f: fontCache.getFonts())
if (f->getFontSize() == ptSize)
font = f;
for (const char& c : text)
extents.x += font->getGlyph(c)->advanceX;
extents.y = ptSize;
return extents;
}
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
Vector2 extents = Vector2(0,0);
for (const char& c : text) {
FT_GlyphSlot slot = face->glyph;
@@ -136,8 +158,6 @@ namespace JGL
}
}
return extents;
}

View File

@@ -18,8 +18,6 @@
namespace JGL {
FontCache fontCache; // <-- Not implemented yet
void PurgeFontCache() {
fontCache.purgeCache();
}