Terminal: grapheme-aware widths, drawn box/Powerline glyphs, line pictures

Emulator (vendored xterm):
- Widths follow glibc wcwidth on generated Unicode 16 tables
  (tool/gen_unicode_tables.py); combining marks, variation selectors,
  ZWJ sequences, skin tones, flags and Hangul jamo join the previous cell,
  stored as interned clusters (CellContent.clusterFlag) and drawn and
  copied whole. U+FE0F and flags take two columns, as modern terminals
  do; a wide character never starts in the last column.
- Box drawing, block elements and Powerline separators are drawn as
  geometry filling the cell; rows are whole pixels high.
- Backgrounds merge per colour; each line is a picture reused until the
  line changes (BufferLine.version); a line drawn unchanged settles into
  one laid out paragraph with a span per style. TerminalStyle and
  TerminalTheme compare by value.

App: bundled Symbols Nerd Font Mono subset (568 KiB, MIT) as the
terminal's first fallback, loaded on first use; line height 1.1;
?terminalUnicode=1 fixture showcase; bench gains a progress workload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 01:14:48 +00:00
parent f4ffda7130
commit 018d37f103
30 changed files with 3811 additions and 103 deletions
+86 -1
View File
@@ -198,6 +198,84 @@ Emulator (`chat_shell_terminal_feed_test.dart`, 896 KiB replay): one
synchronous write 160 ms; sliced, the same 161 ms over 20 turns, the
longest turn 12.5 ms.
## Unicode, glyphs and drawing (2026-11-26)
All in the vendored emulator (`third_party/xterm`, README lists the patches)
plus the view's style (`chat_shell_terminal_theme.dart`).
- **Widths** follow glibc `wcwidth` (what tmux, readline and ncurses lay
out with) on Unicode 16 tables generated by
`third_party/xterm/tool/gen_unicode_tables.py` from Python's
`unicodedata` and `emoji-data.txt`; every assigned code point was compared
with glibc 2.44 and the few differences (prepended concatenation marks,
Hangul fillers, U+3248-324F) are pinned to glibc. Combining marks and
format characters 0, East Asian Wide/Fullwidth (every default emoji) 2.
- **Graphemes**: a zero-width character (combining mark, variation
selector, ZWJ, tag, Hangul medial/final jamo) joins the cell before the
cursor; a pictograph after a ZWJ, a skin tone after a modifier base and a
second regional indicator join too. The cell keeps the cluster interned
(`CellContent.clusterFlag` + an index into one shared table, capped at
65,536 clusters of at most 32 UTF-16 units), so cells stay four words and
reflow/scrolling copy them unchanged. The painter draws the whole cluster;
copy (`getText`) returns it whole. A zero-width character with nothing
before it on the line is dropped (it takes no column). A wide character
never starts in the last column (it wraps whole).
- **Choice where terminals disagree**: U+FE0F after a text-default emoji
(❤️, keycaps) and a flag make the cell two columns, as kitty, WezTerm,
foot, Ghostty and iTerm2 do. A flag is also 2 under wcwidth (1 + 1), but
wcwidth gives U+FE0F 0, so a program measuring with wcwidth counts ❤️ as
one column; a ZWJ family is one two-column cell here but 6 columns to
wcwidth (2 + 0 + 2 + 0 + 2). Inside tmux, which redraws with its own
widths, such emoji can still shift the rest of that line by a column.
- **Glyphs**: box drawing (U+2500-257F: light, heavy, double, dashed,
rounded, diagonal), block elements (U+2580-259F) and Powerline separators
(U+E0B0-E0BF) are geometry that fills the cell (`ui/cell_glyphs.dart`),
with half-pixel bleed across cell sides and inner edges on whole pixels,
so they join across cells and rows at any line height. Other Nerd Font
icons come from the bundled subset of Symbols Nerd Font Mono (568 KiB,
MIT; `fonts/README.md`), first in the terminal's fallback families and
registered on first use (`lib/ui/text/terminal_symbols_font.dart`). A
glyph wider than its columns (a full-em icon or symbol from a fallback
font) is scaled down to fit; a narrower one (emoji, CJK) is centred.
- **Line height** 1.1, rows rounded up to whole pixels (about 1.13 at the
default 13.3 px), was 1.3. Roboto Mono has no italic: italic stays
synthesised (a bundled italic was not added).
- **Drawing**: backgrounds are one rectangle per run of a colour with
whole-pixel edges. Each line is a picture reused until the line changes
(`BufferLine.version`) or the painter's style, theme or fonts change, so a
frame records only changed lines. A new line draws its text from the
per-character paragraph cache; a line drawn unchanged in two frames is
re-recorded "settled", its plain text laid out as one paragraph with a
span per style (glyphs advance one cell each; checked against the grid,
cell by cell otherwise), at most 8 lines per frame. Measured on CanvasKit,
laying out a paragraph per new line cost more than it saved while output
streams (every visible line is new each frame), so new lines never pay
for it. `TerminalStyle` and `TerminalTheme` compare by value, so a view
rebuild no longer clears every cache.
Bench: `tool/perf/terminal_stream_bench.dart` (the app's style, theme and
sliced feed; profile web build) driven by `tool/perf/terminal_stream_bench.mjs`
in headless Chromium (CanvasKit, software raster, 1280x800, 8 MB per
workload, generated at up to 30 MB/s; the emulator's parsing is the
throughput limit). Medians of two interleaved runs on a shared, loaded
machine (frame ms; `screen` redraws all 48 rows with colours each chunk,
`progress` rewrites one status line under a static screen):
| workload | before build p50/p90 | after | before raster p50/p90 | after | before MB/s | after |
| --- | --- | --- | --- | --- | --- | --- |
| `seq` | 1.0/1.5 | 1.5/2.3 | 7.6/8.7 | 7.7/8.8 | 1.05 | 1.01 |
| `yes` | 0.9/1.3 | 1.2/1.7 | 7.6/8.8 | 7.4/8.2 | 0.59 | 0.66 |
| unicode lines | 1.4/1.9 | 2.7/3.6 | 8.6/9.6 | 10.5/12.0 | 1.92 | 1.98 |
| coloured lines | 1.5/1.9 | 2.1/2.6 | 8.7/9.6 | 8.9/9.9 | 2.23 | 2.58 |
| `screen` | 4.1/4.8 | 3.4/4.0 | 13.0/14.6 | 11.4/14.0 | 2.77 | 3.82 |
| `progress` | 2.7/2.9 | 0.9/3.2 | 11.2/11.8 | 8.6/10.3 | 3.79 | 3.94 |
Streaming plain lines is unchanged within noise (+0.5 ms build for the
pictures); full-screen redraws and mostly static screens are cheaper; the
unicode workload (six procedural glyphs per line, paths rasterised in
software) costs about 2 ms more raster. Runs on this machine vary by about
±20 % between repetitions.
## Login and lifetime (server)
- `internal/server/terminal_login.go`. A terminal runs, in its PTY,
@@ -289,7 +367,14 @@ longest turn 12.5 ms.
`lib/features/chat/shell/chat_shell_terminals.dart` (`ChatShellInputPipe`
made public), `lib/features/chat/chat_screen_workspace.dart` (Ctrl+Tab
yields to a shown terminal). Tests `test/features/terminal/`. Probe fixture
`tool/perf/terminal_fixture.dart` (`?terminals=1`, `?terminal=panel|float`).
`tool/perf/terminal_fixture.dart` (`?terminals=1`, `?terminal=panel|float`,
showcases `?terminalAttrs=1`, `?terminalUnicode=1`).
- Emulator Unicode and drawing: `third_party/xterm/lib/src/utils/unicode_{width,tables}.dart`,
`core/buffer/{graphemes,grapheme_join}.dart`, `ui/{cell_glyphs,text_runs,row_pictures,painter}.dart`,
`third_party/xterm/tool/gen_unicode_tables.py`; tests
`test/third_party_xterm/{grapheme,glyph_paint}_test.dart`. Symbols font
`fonts/symbols-nerd-font/`, `lib/ui/text/terminal_symbols_font.dart`.
Bench `tool/perf/terminal_stream_bench.{dart,mjs}`.
## Verification