corrected handling of newline in layout_string()

This commit is contained in:
Alessandro Mauri 2025-09-16 17:06:19 +02:00
parent be1476d107
commit 915f395b5a
3 changed files with 40 additions and 19 deletions

View File

@ -309,9 +309,18 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
short line_height = (short)font.line_height(); short line_height = (short)font.line_height();
Rect cursor_rect = {.h = line_height}; Rect cursor_rect = {.h = line_height};
if (text == "" || bounds.w <= 0 || bounds.h <= 0) return cursor_rect; if (bounds.w <= 0 || bounds.h <= 0) return cursor_rect;
ctx.push_scissor(bounds, z_index)!; ctx.push_scissor(bounds, z_index)!;
if (text == "") {
// when text is empty but we need to draw a cursor use a visually empty string
if (cursor >= 0) {
text = "\f";
} else {
return cursor_rect;
}
}
Id texture_id = font.id; Id texture_id = font.id;
short baseline = (short)font.ascender; short baseline = (short)font.ascender;
short line_gap = (short)font.linegap; short line_gap = (short)font.linegap;
@ -356,6 +365,7 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
switch { switch {
case cp == '\n': case cp == '\n':
off += x;
break ITER; break ITER;
case cp == '\t': case cp == '\t':
o.x += tab_width; o.x += tab_width;
@ -377,28 +387,42 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
} }
} }
line_end = off; line_end = off;
if (line_end == line_start) break; if (line_end == line_start) unreachable("something went wrong in measuring the line");
// with the line width calculate the right origin and layout the line // with the line width calculate the right origin and layout the line
origin.x = bounds.x; origin.x = bounds.x;
short next_line_x = bounds.x; // the x coordinate of the origin if the line_width is zero
switch (anchor) { switch (anchor) {
case TOP_LEFT: nextcase; case TOP_LEFT: nextcase;
case LEFT: nextcase; case LEFT: nextcase;
case BOTTOM_LEFT: case BOTTOM_LEFT:
// TODO: we didn't need to measure the line width with this alignment // TODO: we didn't need to measure the line width with this alignment
origin.x += 0; origin.x += 0;
next_line_x += 0;
case TOP: nextcase; case TOP: nextcase;
case CENTER: nextcase; case CENTER: nextcase;
case BOTTOM: case BOTTOM:
origin.x += (short)(bounds.w - line_width)/2+1; origin.x += (short)(bounds.w - line_width)/2+1;
next_line_x += bounds.w/2;
case TOP_RIGHT: nextcase; case TOP_RIGHT: nextcase;
case RIGHT: nextcase; case RIGHT: nextcase;
case BOTTOM_RIGHT: case BOTTOM_RIGHT:
origin.x += (short)(bounds.w - line_width); origin.x += (short)(bounds.w - line_width);
next_line_x += bounds.w;
} }
cursor_rect.x = origin.x; // reset the cursor to the line
cursor_rect.y = origin.y; if (line_start <= cursor && cursor <= line_end) {
cursor_rect.x = origin.x;
cursor_rect.y = origin.y;
cursor_rect.w = 0;
if (cursor && text[cursor-1] == '\n') {
cursor_rect.x = next_line_x;
cursor_rect.y += line_height;
}
}
Point line_origin = origin;
// see the fixme when measuring the height // see the fixme when measuring the height
//ctx.push_rect({.x = origin.x,.y=origin.y,.w=(short)line_width,.h=(short)string_height}, z_index, &&(Style){.bg=0xff000042u.@to_rgba()})!; //ctx.push_rect({.x = origin.x,.y=origin.y,.w=(short)line_width,.h=(short)string_height}, z_index, &&(Style){.bg=0xff000042u.@to_rgba()})!;
@ -407,7 +431,6 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
for (usz x; (cp = str_to_codepoint(line[off..], &x)) != 0 && off < line.len; off += x) { for (usz x; (cp = str_to_codepoint(line[off..], &x)) != 0 && off < line.len; off += x) {
Glyph* gp = font.get_glyph(cp)!; Glyph* gp = font.get_glyph(cp)!;
// update the text bounds
switch { switch {
case cp == '\t': case cp == '\t':
origin.x += tab_width; origin.x += tab_width;
@ -429,15 +452,16 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
ctx.push_sprite(b, uv, texture_id, z_index, hue)!; ctx.push_sprite(b, uv, texture_id, z_index, hue)!;
//ctx.push_rect(b, z_index, &&(Style){.bg=0x0000ff66u.@to_rgba()})!; //ctx.push_rect(b, z_index, &&(Style){.bg=0x0000ff66u.@to_rgba()})!;
if (line_start + off < cursor) {
cursor_rect.x = origin.x + gp.adv;
cursor_rect.y = origin.y;
cursor_rect.w = gp.adv;
}
origin.x += gp.adv; origin.x += gp.adv;
} }
if (line_start + off < cursor && text[cursor-1] != '\n') {
cursor_rect.x = origin.x;
cursor_rect.y = origin.y;
cursor_rect.w = gp.adv;
}
} }
// done with the line // done with the line
line_start = line_end; line_start = line_end;
origin.y += line_height + line_gap; origin.y += line_height + line_gap;

View File

@ -76,14 +76,11 @@ fn ElemEvents? Ctx.text_box_id(&ctx, Id id, Size w, Size h, TextEdit* te)
cur = ctx.layout_string(elem.text.te.to_string(), text_bounds, TOP_LEFT, parent.div.z_index, style.fg, elem.text.te.cursor)!; cur = ctx.layout_string(elem.text.te.to_string(), text_bounds, TOP_LEFT, parent.div.z_index, style.fg, elem.text.te.cursor)!;
// draw the cursor if the element has focus // draw the cursor if the element has focus
cur.w = 2;
if (elem.events.has_focus) { if (elem.events.has_focus) {
cur.w = 2; ctx.push_scissor(text_bounds, parent.div.z_index)!;
// FIXME: gross hack for when text is empty
if (cur.x == 0 && cur.y == 0) {
cur.x = text_bounds.x;
cur.y = text_bounds.y;
}
ctx.push_rect(cur, parent.div.z_index, &&(Style){.bg = style.fg})!; ctx.push_rect(cur, parent.div.z_index, &&(Style){.bg = style.fg})!;
ctx.reset_scissor(parent.div.z_index)!;
} }
return elem.events; return elem.events;
} }

View File

@ -356,7 +356,7 @@ fn void calculator(ugui::Ctx* ui, TextEdit* te)
case "9": case "9":
buffer[len++] = ui.get_keys()[0]; buffer[len++] = ui.get_keys()[0];
case "\n": case "\n":
eval = true; eval = len != 0;
case "c": case "c":
len = 0; len = 0;
case "d": case "d":