From 61556d0a2c325ef07d232b1b3f990d3bc481f9c8 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Wed, 11 Dec 2024 20:39:06 +0100 Subject: [PATCH] forgot the baseline --- src/main.c3 | 10 ++++++++-- src/ugui_font.c3 | 20 ++++++++++++-------- src/ugui_text.c3 | 10 ++++++---- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main.c3 b/src/main.c3 index dc833e0..7dbc648 100644 --- a/src/main.c3 +++ b/src/main.c3 @@ -27,7 +27,7 @@ fn int main(String[] args) { ugui::Ctx ui; ui.init()!!; - ui.font.load("/usr/share/fonts/TTF/FreeSans.ttf", 16)!!; + ui.font.load("/usr/share/fonts/TTF/FreeSans.ttf", 16, scale: 1.5)!!; short width = 800; short height = 450; @@ -168,10 +168,16 @@ fn int main(String[] args) }; rl::Vector2 position = { .x = cmd.sprite.rect.x, - .y = (float)cmd.sprite.rect.y - cmd.sprite.rect.h, + .y = cmd.sprite.rect.y, }; rl::draw_texture_rec(font_texture, source, position, rl::WHITE); + //rl::draw_rectangle(cmd.sprite.rect.x, + // cmd.sprite.rect.y, + // cmd.sprite.rect.w, + // cmd.sprite.rect.h, + // rl::WHITE + //); default: io::printfn("Unknown cmd type: %s", cmd.type); } diff --git a/src/ugui_font.c3 b/src/ugui_font.c3 index 350f7ca..f188fbb 100644 --- a/src/ugui_font.c3 +++ b/src/ugui_font.c3 @@ -3,6 +3,7 @@ module ugui; import schrift; import std::collections::map; import std::core::mem; +import std::io; // unicode code point, different type for a different hash def Codepoint = uint; @@ -15,9 +16,9 @@ def Codepoint = uint; * | |oy | | | * | v | | | * | .ii. | | | - * | @@@@@@. |<->| | - * | V@Mio@@o |adv| |h - * | :i. V@V | | | + * | @@@@@@. | | | + * | V@Mio@@o | | | + * | :i. V@V | | h * | :oM@@M | | | * | :@@@MM@M | | | * | @@o o@M | | | @@ -25,8 +26,8 @@ def Codepoint = uint; * |ox @@@o@@@@ | | | * | :M@@V:@@.| | v * +-------------*---+ - - * |<------------->| - * w + * |<---- w ---->| + * |<------ adv ---->| */ struct Glyph { Codepoint code; @@ -137,11 +138,12 @@ fn void! Font.load(&font, String path, uint height, float scale = 1) font.ascender = (float)lmetrics.ascender; font.descender = (float)lmetrics.descender; font.linegap = (float)lmetrics.lineGap; + //io::printfn("ascender:%d, descender:%d, linegap:%d", font.ascender, font.descender, font.linegap); // TODO: allocate buffer based on FONT_CACHED and the size of a sample letter // like the letter 'A' font.atlas = mem::new_array(AtlasBW, 1); - ushort size = (ushort)font.size*512; + ushort size = (ushort)font.size*256; font.atlas[0].new(size, size)!; // preallocate the ASCII range @@ -191,9 +193,11 @@ fn Glyph*! Font.get_glyph(&font, Codepoint code, bool* is_new = null) glyph.code = code; glyph.w = (ushort)img.width; glyph.h = (ushort)img.height; - glyph.ox = (short)-gmtx.leftSideBearing; - glyph.oy = (short)-gmtx.yOffset; + glyph.ox = (short)gmtx.leftSideBearing; + glyph.oy = (short)gmtx.yOffset; glyph.adv = (short)gmtx.advanceWidth; + //io::printfn("code=%c, w=%d, h=%d, ox=%d, oy=%d, adv=%d", + // glyph.code, glyph.w, glyph.h, glyph.ox, glyph.oy, glyph.adv); Point uv = font.atlas[0].place(pixels, glyph.w, glyph.h)!; glyph.idx = 0; diff --git a/src/ugui_text.c3 b/src/ugui_text.c3 index 1e6cb29..f60eff5 100644 --- a/src/ugui_text.c3 +++ b/src/ugui_text.c3 @@ -15,6 +15,8 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text) // this resets the flags c_elem.type = ETYPE_TEXT; + short line_height = (short)ctx.font.ascender - (short)ctx.font.descender; + short baseline = (short)ctx.font.ascender; bool update_atlas; // if the element is new or the parent was updated then redo layout if (c_elem.flags.is_new || parent.flags.updated) { @@ -26,8 +28,8 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text) Codepoint cp = (Codepoint)c; bool n; gp = ctx.font.get_glyph(cp, &n)!; - text_size.w += gp.w + gp.ox + gp.adv; - text_size.h += gp.h + gp.oy; + text_size.w += gp.adv; + text_size.h += line_height; if (n) { update_atlas = true; } } @@ -64,7 +66,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text) .type = CMD_SPRITE, .sprite.rect = { .x = orig.x + gp.ox, - .y = orig.y + gp.oy, + .y = orig.y + gp.oy + baseline, .w = gp.w, .h = gp.h, }, @@ -75,7 +77,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text) .h = gp.h, }, }; - orig.x += gp.w + gp.ox; + orig.x += gp.adv; ctx.cmd_queue.enqueue(&cmd)!; } }