use libgrapheme to interpret utf8 encoded strings

This commit is contained in:
Alessandro Mauri 2024-12-14 01:06:58 +01:00
parent 8cf3881b6b
commit 0db858e814
6 changed files with 84 additions and 45 deletions

View file

@ -1,13 +1,15 @@
module ugui;
import std::ascii;
// FIXME: is this really the best solution?
// "rect" is the bounding box of the element, which includes the border and the padding (so not just the content)
fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, bool do_padding = false, bool do_radius = false)
{
// FIXME: this should be culled higher up, maybe
if (rect.w <= 0 || rect.h <= 0) {
return;
}
// FIXME: this should be culled higher up, maybe
if (rect.w <= 0 || rect.h <= 0) {
return;
}
Rect border = ctx.style.border;
Rect padding = ctx.style.padding;
@ -48,3 +50,50 @@ fn void! Ctx.push_sprite(&ctx, Rect bounds, Rect texture)
};
ctx.cmd_queue.enqueue(&cmd)!;
}
fn void! Ctx.push_string(&ctx, Rect bounds, String text)
{
if (text.len == 0) {
return;
}
short baseline = (short)ctx.font.ascender;
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
short line_gap = (short)ctx.font.linegap;
Point orig = {
.x = bounds.x,
.y = bounds.y,
};
short line_len;
Codepoint cp;
usz off, x;
while ((cp = str_to_codepoint(text[off..], &x)) != 0) {
off += x;
Glyph* gp;
if (!ascii::is_cntrl((char)cp)) {
gp = ctx.font.get_glyph(cp)!;
Rect gb = {
.x = orig.x + line_len + gp.ox,
.y = orig.y + gp.oy + baseline,
.w = gp.w,
.h = gp.h,
};
Rect gt = {
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
};
if (rect_collision(gb, bounds)) {
ctx.push_sprite(gb, gt)!;
}
line_len += gp.adv;
} else if (cp == '\n'){
orig.y += line_height + line_gap;
line_len = 0;
} else {
continue;
}
}
}