render: use provided character width

As a pretty large optimization, use the provided width when possible to
avoid measuring glyphs each render

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-28 22:42:51 -06:00
parent a355d7519a
commit e6c8be4bd6
2 changed files with 10 additions and 2 deletions

View file

@ -6,6 +6,10 @@ pub const Cell = struct {
pub const Character = struct { pub const Character = struct {
grapheme: []const u8 = " ", grapheme: []const u8 = " ",
/// width should only be provided when the application is sure the terminal
/// will meeasure the same width. This can be ensure by using the gwidth method
/// included in libvaxis. If width is 0, libvaxis will measure the glyph at
/// render time
width: usize = 1, width: usize = 1,
}; };

View file

@ -294,8 +294,12 @@ pub fn Vaxis(comptime T: type) type {
const cell = self.screen.buf[i]; const cell = self.screen.buf[i];
defer { defer {
// advance by the width of this char mod 1 // advance by the width of this char mod 1
const method: gwidth.Method = if (self.caps.unicode) .unicode else .wcwidth; const w = blk: {
const w = gwidth.gwidth(cell.char.grapheme, method) catch 1; if (cell.char.width != 0) break :blk cell.char.width;
const method: gwidth.Method = if (self.caps.unicode) .unicode else .wcwidth;
break :blk gwidth.gwidth(cell.char.grapheme, method) catch 1;
};
var j = i + 1; var j = i + 1;
while (j < i + w) : (j += 1) { while (j < i + w) : (j += 1) {
self.screen_last.buf[j].skipped = true; self.screen_last.buf[j].skipped = true;