diff --git a/src/gui/gui_scene.cpp b/src/gui/gui_scene.cpp index 45fded7fb..4c8ea93b8 100644 --- a/src/gui/gui_scene.cpp +++ b/src/gui/gui_scene.cpp @@ -297,8 +297,8 @@ struct GUISceneImpl final : GUIScene switch (rect.text->vertical_align) { case TextVAlign::TOP: break; - case TextVAlign::MIDDLE: text_pos.y = (t + b + font_size) * 0.5f; break; - case TextVAlign::BOTTOM: text_pos.y = b; break; + case TextVAlign::MIDDLE: text_pos.y = (t + b + getAscender(*font) - getDescender(*font)) * 0.5f; break; + case TextVAlign::BOTTOM: text_pos.y = b + getDescender(*font); break; } switch (rect.text->horizontal_align) { diff --git a/src/renderer/font.cpp b/src/renderer/font.cpp index 4e8ea53aa..d9b9e520c 100644 --- a/src/renderer/font.cpp +++ b/src/renderer/font.cpp @@ -22,12 +22,14 @@ struct Font { FontResource* resource; HashMap glyphs; u32 font_size = 0; + float descender = 0; + float ascender = 0; u32 ref = 0; }; -float getAdvanceY(const Font& font) { - return float(font.font_size); -} +float getAdvanceY(const Font& font) { return float(font.font_size); } +float getDescender(const Font& font) { return font.descender; } +float getAscender(const Font& font) { return font.ascender; } const Glyph* findGlyph(const Font& font, u32 codepoint) { auto iter = font.glyphs.find(codepoint); @@ -152,6 +154,8 @@ bool FontManager::build() continue; } + font->descender = face->descender / 64.f; + font->ascender = face->ascender / 64.f; for (Glyph& c : font->glyphs) { c.u0 = c.v0 = 0; c.u1 = c.v1 = 1; diff --git a/src/renderer/font.h b/src/renderer/font.h index 2bdd193fa..6fd336b2e 100644 --- a/src/renderer/font.h +++ b/src/renderer/font.h @@ -28,6 +28,8 @@ struct Glyph { LUMIX_RENDERER_API Vec2 measureTextA(const Font& font, const char* str, const char* str_end); LUMIX_RENDERER_API const Glyph* findGlyph(const Font& font, u32 codepoint); LUMIX_RENDERER_API float getAdvanceY(const Font& font); +LUMIX_RENDERER_API float getDescender(const Font& font); +LUMIX_RENDERER_API float getAscender(const Font& font); struct LUMIX_RENDERER_API FontResource final : Resource