summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig4
-rw-r--r--src/CHANGELOG.md5
-rw-r--r--src/File.zig65
-rw-r--r--src/draw.zig3
-rw-r--r--src/surface.zig3
-rw-r--r--src/tui/tui.zig14
6 files changed, 78 insertions, 16 deletions
diff --git a/build.zig b/build.zig
index 0e67b61e..26eb0c5c 100644
--- a/build.zig
+++ b/build.zig
@@ -842,7 +842,9 @@ pub fn build(b: *std.Build) void {
.mvzr = mvzr_mod,
.c_heap = c_heap_mod,
.ghostty_vt = ghostty_core_vt,
- .vaxis = vaxis_mod,
+ // The tui's and the gui's (main.zig's roots); the core measures
+ // text with uucode alone (File.utf8Width).
+ .vaxis = if (m == core_module) null else vaxis_mod,
.uucode = uucode_mod,
.wuffs = wuffs_mod,
.xitdb = xitdb_mod,
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md
index dc130be1..d0b07885 100644
--- a/src/CHANGELOG.md
+++ b/src/CHANGELOG.md
@@ -2,6 +2,11 @@
## 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.
+
- Commit patches open as `+Diff-<short-hash>` 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 d1cfe088..5150f1cd 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 vaxis = @import("vaxis");
+const uucode = @import("uucode");
const builtin = @import("builtin");
const pardes = @import("pardes.zig");
const config = @import("config.zig");
@@ -186,10 +186,8 @@ 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, 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
+ 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
// run between is measured alone.
var width: usize = 0;
var run: usize = 0;
@@ -200,15 +198,60 @@ pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
i += n;
continue;
}
- if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode);
+ if (run < i) width += utf8Width(grapheme[run..i]);
width += 1;
i += 1;
run = i;
}
- if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode);
+ if (run < grapheme.len) width += utf8Width(grapheme[run..]);
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;
@@ -2206,7 +2249,7 @@ pub fn paintWordSelection(
}
}
-test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
+test "an invalid byte in a grapheme is one cell, and never reaches utf8Width" {
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"));
@@ -2245,11 +2288,11 @@ test "indexed source rows match uncached scans across content changes" {
}
}
-test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
+test "the ASCII arm of graphemeDisplayWidth matches the utf8Width 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, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ return @max(1, @as(usize, utf8Width(grapheme)));
}
}.width;
@@ -2264,7 +2307,7 @@ test "the ASCII arm of graphemeDisplayWidth matches the gwidth 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
- // vaxis's own count gives the pair one.
+ // one U+FFFD for the pair would give one.
try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
}
diff --git a/src/draw.zig b/src/draw.zig
index 7d199155..a0a28cc7 100644
--- a/src/draw.zig
+++ b/src/draw.zig
@@ -4,7 +4,6 @@
//! 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");
@@ -1330,7 +1329,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 vaxis.gwidth.gwidth(cell.grapheme(), .unicode) == 2));
+ if (i + 1 == row.len) try std.testing.expect(!head and !(cell.len > 1 and panes.File.utf8Width(cell.grapheme()) == 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 a5dbea8e..46b6c664 100644
--- a/src/surface.zig
+++ b/src/surface.zig
@@ -4,7 +4,6 @@ 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;
@@ -425,7 +424,7 @@ pub const Surface = struct {
const width: u16 = if (decoded == null or (cp < 0x80 and cp_slice.len == 1))
1
else
- @max(1, vaxis.gwidth.gwidth(cp_slice, .unicode));
+ @max(1, panes.File.utf8Width(cp_slice));
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 0daac14b..77c8ce2b 100644
--- a/src/tui/tui.zig
+++ b/src/tui/tui.zig
@@ -3344,3 +3344,17 @@ 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));
+}