From e6c8be4bd6c092019ae887c629885975a15e0f73 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Sun, 28 Jan 2024 22:42:51 -0600 Subject: [PATCH] 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 --- src/cell.zig | 4 ++++ src/vaxis.zig | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cell.zig b/src/cell.zig index 08260c1..9670382 100644 --- a/src/cell.zig +++ b/src/cell.zig @@ -6,6 +6,10 @@ pub const Cell = struct { pub const Character = struct { 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, }; diff --git a/src/vaxis.zig b/src/vaxis.zig index b1a2b09..94d8128 100644 --- a/src/vaxis.zig +++ b/src/vaxis.zig @@ -294,8 +294,12 @@ pub fn Vaxis(comptime T: type) type { const cell = self.screen.buf[i]; defer { // advance by the width of this char mod 1 - const method: gwidth.Method = if (self.caps.unicode) .unicode else .wcwidth; - const w = gwidth.gwidth(cell.char.grapheme, method) catch 1; + const w = blk: { + 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; while (j < i + w) : (j += 1) { self.screen_last.buf[j].skipped = true;