#include "tim.h" TimCell tim_cell(cstr s, u8 fg, u8 bg) { TimCell c = {.fg = fg, .bg = bg, .n = 1, .buf = {s[0]}}; while ((s[c.n] & 192) == 128 && c.n < sizeof(c.buf)) { c.buf[c.n] = s[c.n]; c.n += 1; } return c; } void tim_clear_cells(void) { size_t size = sizeof(tim->cells[0]) * tim->w * tim->h; memset(tim->cells, 0, size); } void tim_draw_chr(TimCell cell, i32 x, i32 y) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (x >= scope.x && x < scope.x + scope.w && y >= scope.y && y < scope.y + scope.h) { tim->cells[x + y * tim->w] = cell; } } void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (y >= scope.y && y < scope.y + scope.h && w > 0) { for (i32 i = MAX(x, scope.x); i < MIN(x + w, scope.x + scope.w); i++) { tim->cells[i + y * tim->w] = cell; } } } void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (x >= scope.x && x < scope.x + scope.w && h > 0) { for (i32 i = MAX(y, scope.y); i < MIN(y + h, scope.y + scope.h); i++) { tim->cells[x + i * tim->w] = cell; } } } void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (w > 0 && h > 0) { for (i32 iy = MAX(y, scope.y); iy < MIN(y + h, scope.y + scope.h); iy++) { for (i32 ix = MAX(x, scope.x); ix < MIN(x + w, scope.x + scope.w); ix++) { tim->cells[ix + iy * tim->w] = cell; } } } } void tim_draw_str(cstr s, i32 x, i32 y, i32 w, u8 fg, u8 bg) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (s && y >= 0 && x < scope.x + scope.w && y < scope.y + scope.h ) { i32 end = MIN(x + w, scope.x + scope.w); bool wide = false; for (i32 i = 0; s[i] && x < end; x++) { TimCell c = tim_cell(&s[i], fg, bg); wide = wide || tim_utf8_is_wide_perhaps(c.buf, c.n); if (x >= 0) { c.wide = wide; tim->cells[x + y * tim->w] = c; } i += c.n; } } } void tim_draw_box(i32 x, i32 y, i32 w, i32 h, u8 fg, u8 bg) { tim_draw_chr(tim_cell("┌", fg, bg), x , y); tim_draw_chr(tim_cell("┐", fg, bg), x + w - 1, y); tim_draw_chr(tim_cell("└", fg, bg), x , y + h - 1); tim_draw_chr(tim_cell("┘", fg, bg), x + w - 1, y + h - 1); tim_draw_row(tim_cell("─", fg, bg), x + 1 , y , w - 2); tim_draw_row(tim_cell("─", fg, bg), x + 1 , y + h - 1, w - 2); tim_draw_col(tim_cell("│", fg, bg), x , y + 1 , h - 2); tim_draw_col(tim_cell("│", fg, bg), x + w - 1, y + 1 , h - 2); tim_fill(tim_cell(" ", fg, bg), x + 1 , y + 1 , w - 2, h - 2); } void tim_draw_invert(i32 x, i32 y, i32 w) { TimRect scope = tim->scopes[tim->scope]; if(!tim_rect_does_fit(scope)) return; if (y >= 0 && y < scope.y + scope.h && w > 0) { for (i32 i = MAX(x, scope.x); i < MIN(x + w, scope.x + scope.w); i++) { TimCell c = tim->cells[i + y * tim->w]; tim->cells[i + y * tim->w].fg = c.bg; tim->cells[i + y * tim->w].bg = c.fg; } } }