From 68e155448d2af1fd755b699e2e401c0c653b21c3 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 2 Jul 2023 15:11:11 +0900 Subject: [PATCH] [ui] Position text correctly in its view It turns out text positioning was a little more complicated than I remembered. The Y offset is relative to the baseline with Y going down. --- libs/ui/imui.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/ui/imui.c b/libs/ui/imui.c index d13f89340..0216142aa 100644 --- a/libs/ui/imui.c +++ b/libs/ui/imui.c @@ -393,6 +393,12 @@ add_text (view_t view, imui_state_t *state, imui_ctx_t *ctx) int descender = ctx->font->face->size->metrics.descender / 64; auto len = View_GetLen (text); View_SetLen (text, len.x, ascender - descender); + // text is positioned such that 0 is the baseline, and +y offset moves + // the text down. The font's global ascender is used to find the baseline + // relative to the top of the view. + auto pos = View_GetPos (text); + View_SetPos (text, pos.x, pos.y - len.y + ascender); + View_SetGravity (text, grav_northwest); View_SetVisible (text, 1); Ent_SetComponent (text.id, c_glyphs, reg,