scissor command

This commit is contained in:
Alessandro Mauri 2024-12-15 22:29:07 +01:00
parent 6d8300f9d9
commit bca29c537c
6 changed files with 83 additions and 63 deletions

View file

@ -1,9 +1,12 @@
module ugui;
import schrift;
import grapheme;
import std::collections::map;
import std::core::mem;
import std::io;
import std::ascii;
// unicode code point, different type for a different hash
def Codepoint = uint;
@ -161,4 +164,50 @@ fn void Font.free(&font)
fn void! Ctx.load_font(&ctx, String name, String path, uint height, float scale = 1.0)
{
return ctx.font.load(name, path, height, scale);
}
<*
@require off != null
*>
fn Codepoint str_to_codepoint(char[] str, usz* off)
{
Codepoint cp;
isz b = grapheme::decode_utf8(str, str.len, &cp);
if (b == 0 || b > str.len) {
return 0;
}
*off = b;
return cp;
}
fn Rect! Ctx.get_text_bounds(&ctx, String text)
{
Rect text_bounds;
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
short line_gap = (short)ctx.font.linegap;
text_bounds.h = line_height;
Glyph* gp;
// TODO: account for unicode codepoints
short line_len;
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_len += gp.adv;
} else if (cp == '\n'){
text_bounds.h += line_height + line_gap;
line_len = 0;
} else {
continue;
}
if (line_len > text_bounds.w) {
text_bounds.w = line_len;
}
}
return text_bounds;
}