From 84c2699e22d9660ef5b6de54643c0c78f2e8e855 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:45:31 -0300 Subject: width: back to libvaxis's gwidth, the user's ruling (keeping a dependency beats reimplementing it in the core): vmkopprq reverted: the core measures a grapheme's cells with vaxis.gwidth.gwidth(.unicode) again (File.graphemeDisplayWidth and its neighbours, surface.zig, draw.zig's test), File.utf8Width and tui.zig's corpus test that held vaxis's width to it go, and build.zig gives libvaxis to the core module again, so vaxis stays a dependency of the core; the widths were the same on every Unicode scalar value (vmkopprq's proof), so every golden and snapshot is unchanged; the changelog's shared entry keeps the palette sentence alone --- src/CHANGELOG.md | 8 ++----- src/File.zig | 65 ++++++++++---------------------------------------------- src/draw.zig | 3 ++- src/surface.zig | 3 ++- src/tui/tui.zig | 14 ------------ 5 files changed, 17 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 4226e986..5a8947e8 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,12 +2,8 @@ ## Unreleased -- The core measures a grapheme's cells over uucode itself - (`File.utf8Width`, libvaxis's `gwidth` to the cell on every scalar - value), so libvaxis is the tui's and the gui's alone, not the core - module's. The 256-colour palette Petscii art and the window draw a - palette colour with is pardes's own (`colors.xterm`), ghostty-vt's - default to the entry. +- The 256-colour palette Petscii art and the window draw a palette colour + with is pardes's own (`colors.xterm`), ghostty-vt's default to the entry. - Commit patches open as `+Diff-` and reuse only their own commit's pane, so another paged patch no longer replaces one already open. diff --git a/src/File.zig b/src/File.zig index 336b8526..f33d0f1b 100644 --- a/src/File.zig +++ b/src/File.zig @@ -2,7 +2,7 @@ //! index, syntax highlights and the body it fills. const panes = @import("panes.zig"); const std = @import("std"); -const uucode = @import("uucode"); +const vaxis = @import("vaxis"); const builtin = @import("builtin"); const pardes = @import("pardes.zig"); const config = @import("config.zig"); @@ -186,8 +186,10 @@ pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; pub fn graphemeDisplayWidth(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1; - if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, utf8Width(grapheme))); - // Each invalid byte is a cell, as surface.zig draws it, and each valid + if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode))); + // vaxis's gwidth counts back a replacement rune's 3 bytes over the one + // invalid byte it stood for (`\xee` before `e\u{301}` overflowed it): + // each invalid byte is a cell, as surface.zig draws it, and each valid // run between is measured alone. var width: usize = 0; var run: usize = 0; @@ -198,60 +200,15 @@ pub fn graphemeDisplayWidth(grapheme: []const u8) usize { i += n; continue; } - if (run < i) width += utf8Width(grapheme[run..i]); + if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode); width += 1; i += 1; run = i; } - if (run < grapheme.len) width += utf8Width(grapheme[run..]); + if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode); return @max(1, width); } -/// The cells valid UTF-8 takes, summed over its graphemes: a grapheme is -/// as wide as its widest code point (a control, a nonspacing or enclosing -/// mark and the joiners, selectors and other format characters below add -/// none; East Asian wide and fullwidth two, the rest one), then at least -/// one under the text selector U+FE0E, else at least two under the emoji -/// selector U+FE0F, with an emoji-presentation code point, or as a flag's -/// two regional indicators. It is libvaxis's `gwidth(.unicode)` over the -/// same uucode tables, which the tui renders with in mode 2027. -pub fn utf8Width(valid: []const u8) u16 { - var total: u16 = 0; - var widest: u16 = 0; - var text_vs = false; - var emoji = false; - var flags: u8 = 0; - var it = uucode.grapheme.utf8Iterator(valid); - while (it.nextCodePoint()) |r| { - const cp = r.code_point; - if (cp == 0xfe0e) { - text_vs = true; - } else if (cp == 0xfe0f) { - emoji = true; - } else { - if (uucode.get(.is_emoji_presentation, cp)) emoji = true; - if (cp >= 0x1f1e6 and cp <= 0x1f1ff) flags += 1; - widest = @max(widest, @as(u16, switch (cp) { - 0...0x1f, 0x7f...0x9f, 0xad, 0x34f, 0x180b...0x180d, 0x200b...0x200d, 0x2060, 0xfe00...0xfe0d, 0xfeff, 0xe0100...0xe01ef => 0, - else => switch (uucode.get(.general_category, cp)) { - .mark_nonspacing, .mark_enclosing => 0, - else => switch (uucode.get(.east_asian_width, cp)) { - .fullwidth, .wide => 2, - else => 1, - }, - }, - })); - } - if (!r.is_break) continue; - total += if (text_vs) @max(1, widest) else if (emoji or flags == 2) @max(2, widest) else widest; - widest = 0; - text_vs = false; - emoji = false; - flags = 0; - } - return total; -} - pub fn displayWidth(text: []const u8) usize { var width: usize = 0; var at: usize = 0; @@ -2249,7 +2206,7 @@ pub fn paintWordSelection( } } -test "an invalid byte in a grapheme is one cell, and never reaches utf8Width" { +test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" { try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81")); try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe")); try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80")); @@ -2288,11 +2245,11 @@ test "indexed source rows match uncached scans across content changes" { } } -test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" { +test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" { const ref = struct { fn width(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; - return @max(1, @as(usize, utf8Width(grapheme))); + return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode))); } }.width; @@ -2307,7 +2264,7 @@ test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" { "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff", }) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g)); // Two invalid bytes are two cells, as surface.zig draws them, where - // one U+FFFD for the pair would give one. + // vaxis's own count gives the pair one. try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8")); } diff --git a/src/draw.zig b/src/draw.zig index 238f459a..9a71ab1d 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -4,6 +4,7 @@ //! chips, the workspace and column tags, the tag layers, drag overlays, the //! debug box, then the cursor. const std = @import("std"); +const vaxis = @import("vaxis"); const pardes = @import("pardes.zig"); const layout = @import("layout.zig"); const config = @import("config.zig"); @@ -1329,7 +1330,7 @@ test "a wide glyph cut by a tag's or a notice's edge is joined into the grid as const head = !cell.default and cell.len > 1 and i + 1 < row.len and !row[i + 1].default and row[i + 1].len == 0; // A spacer has its head before it; a head its spacer after. if (spacer) try std.testing.expect(i > 0 and row[i - 1].len > 1); - if (i + 1 == row.len) try std.testing.expect(!head and !(cell.len > 1 and panes.File.utf8Width(cell.grapheme()) == 2)); + if (i + 1 == row.len) try std.testing.expect(!head and !(cell.len > 1 and vaxis.gwidth.gwidth(cell.grapheme(), .unicode) == 2)); } // The right edge of each is the band, blank where a glyph was cut. try std.testing.expectEqual(FontRole.tagline, row[row.len - 1].style.font_role); diff --git a/src/surface.zig b/src/surface.zig index 46b6c664..a5dbea8e 100644 --- a/src/surface.zig +++ b/src/surface.zig @@ -4,6 +4,7 @@ const pardes = @import("pardes.zig"); const std = @import("std"); const layout = @import("layout.zig"); const uucode = @import("uucode"); +const vaxis = @import("vaxis"); const panes = @import("panes.zig"); const config = @import("config.zig"); const MAX_PANES = pardes.MAX_PANES; @@ -424,7 +425,7 @@ pub const Surface = struct { const width: u16 = if (decoded == null or (cp < 0x80 and cp_slice.len == 1)) 1 else - @max(1, panes.File.utf8Width(cp_slice)); + @max(1, vaxis.gwidth.gwidth(cp_slice, .unicode)); if (width == 2 and col + 1 >= end) return null; var shown = cp_slice; if (shown.len > @typeInfo(@FieldType(Cell, "text")).array.len) { diff --git a/src/tui/tui.zig b/src/tui/tui.zig index b66a900f..4c0ded5b 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -3344,17 +3344,3 @@ test "the tty draws a glide at whole rows: once a flick or a spin slows, its ste try std.testing.expectEqual(row, (shown.cursor orelse return error.NoCursor).y); } } - -test "the tui's vaxis measures a grapheme as wide as the core lays it out" { - // vaxis renders a cell at gwidth(.unicode) in mode 2027; the core's - // File.utf8Width is that function over the same uucode tables. A vaxis - // that measures otherwise would draw past or short of the core's cells. - for ([_][]const u8{ - "a", "\u{4e16}", "e\u{301}", "\u{301}", - "\u{1f642}", "\u{1f44b}\u{1f3ff}", "\u{1f469}\u{200d}\u{1f680}", "\u{1f1fa}\u{1f1f8}", - "\u{1f1fa}", "\u{2764}\u{fe0e}", "\u{2764}\u{fe0f}", "1\u{fe0f}\u{20e3}", - "\u{1100}\u{1161}", "\u{ff21}", "\u{b1}", "\u{200b}", - "\u{ad}", "\u{fffd}", "\u{85}", "\x7f", - "a\u{4e16}\u{1f642}", "\u{e0100}", "\u{231a}", "\u{2600}", - }) |g| try std.testing.expectEqual(vaxis.gwidth.gwidth(g, .unicode), pardes.panes.File.utf8Width(g)); -} -- cgit v1.3