diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 22:46:21 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | b2163c0a53a6276bc8e1211e9a7c9584bb1ccb2e (patch) | |
| tree | b24a6c5991f6d79e8baecb24f2ad50e3a8e8035d /src/File.zig | |
| parent | f2b02c455f2bcdfb905a015eefbb630a28b4b12e (diff) | |
| download | pardes-b2163c0a53a6276bc8e1211e9a7c9584bb1ccb2e.tar.gz pardes-b2163c0a53a6276bc8e1211e9a7c9584bb1ccb2e.zip | |
width: the core measures a grapheme's cells over uucode itself, File.utf8Width beside graphemeDisplayWidth, so libvaxis leaves the core module (build.zig gives it to main.zig's roots alone, the tui's and the gui's); it is libvaxis's gwidth(.unicode) over the same uucode tables in one pass (a grapheme as wide as its widest code point, the text selector, the emoji selector, emoji presentation and a flag's pair as there), and returned the same width on every Unicode scalar value (1,112,064), 76 multi-codepoint graphemes and 2,000,000 random sequences of joiners, selectors, marks, jamo, regional indicators and any scalar, and graphemeDisplayWidth the same on 6,260,096 byte strings, invalid UTF-8 included (a test run once, then removed); 204 instructions a non-ASCII grapheme against vaxis's 309 (ReleaseFast), a render of perf's small file 11,679,818 instructions against 11,685,276; surface.zig and draw.zig's test call it; tui.zig's test holds vaxis's width to it on a corpus, as vaxis renders the cells the core lays out; the changelog says so
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 65 |
1 files changed, 54 insertions, 11 deletions
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")); } |
