From 9827a899f15b3204577e22c9fb50c869b8958acd Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Mon, 30 Jun 2025 13:08:27 +0200 Subject: [PATCH] get_line_height and get_cursor_position --- lib/ugui.c3l/src/ugui_font.c3 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/ugui.c3l/src/ugui_font.c3 b/lib/ugui.c3l/src/ugui_font.c3 index 23700c6..020ed75 100644 --- a/lib/ugui.c3l/src/ugui_font.c3 +++ b/lib/ugui.c3l/src/ugui_font.c3 @@ -211,6 +211,34 @@ fn Rect? Ctx.get_text_bounds(&ctx, String text) return text_bounds; } +fn Point? Ctx.get_cursor_position(&ctx, String text) +{ + short line_height = (short)ctx.font.ascender - (short)ctx.font.descender; + short line_gap = (short)ctx.font.linegap; + Glyph* gp; + + // TODO: account for unicode codepoints + Point line; + Codepoint cp; + usz off, x; + while ((cp = str_to_codepoint(text[off..], &x)) != 0) { + off += x; + bool n; + if (!ascii::is_cntrl((char)cp)) { + gp = ctx.font.get_glyph(cp)!; + line.x += gp.adv; + } else if (cp == '\n'){ + line.y += line_height + line_gap; + line.x = 0; + } else { + continue; + } + } + + return line; +} + + fn Point Ctx.center_text(&ctx, Rect text_bounds, Rect bounds) { short dw = bounds.w - text_bounds.w; @@ -234,3 +262,5 @@ fn Atlas*? Ctx.get_font_atlas(&ctx, String name) return &ctx.font.atlas; } + +fn int Font.line_height(&font) => (int)(font.ascender - font.descender + (float)0.5);