Skip to content

[CTX-1609] feat(term): authoritative grapheme clusters and East Asian width - #2738

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1609/feat-grapheme-eaw
Oct 27, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1609/feat-grapheme-eaw

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Priority: P1 | Area: term | Labels: feat,P1,area:term | Milestone: v0.3.0 | RFC: OQ-090 | Task: CTX-1609

Summary

Ships #1767 (OQ-090): an extended grapheme cluster is the layout unit, East
Asian Ambiguous width is configurable, and every UAX #29/UAX #11 property the
print and paint paths read is generated from pinned UCD 18.0.0 snapshots.

The first round merged as #2734. This branch was rebased onto main
(f75d8eb7) and carries the follow-ups from the two review rounds on top of
it (10 files, no overlap with #2736/#2737).

What lands

UCD tables and generator provenance

scripts/generate-unicode-tables.py emits the checked-in
crates/bitty-term-state/src/unicode_tables.rs from four pinned inputs, with
a SHA-256 digest per input in the module header and the Unicode License v3
notice:

Table Ranges Scalars
East_Asian_Width (UAX #11) 321 W/F/A/N/Na/H
Extended_Pictographic (UTS #51) 156 -
Grapheme_Cluster_Break=Control (GB4/GB5) 19 3893
Grapheme_Cluster_Break=Extend (GB9) 390 2274
Grapheme_Cluster_Break=SpacingMark (GB9a) 158 381
Grapheme_Cluster_Break=Prepend (GB9b) 15 27
InCB=Linker (GB9c) 22 23
InCB=Consonant (GB9c) 78 913
InCB=Extend (GB9c) 383 2254

No curated subsets remain: GB4/GB5 previously missed soft hyphen, the
Mongolian vowel separator, the bidi isolates, the interlinear/musical/
shorthand and Egyptian format scalars, and the tag-plane ranges, which let
control + mark over-join. GB9c follows the Unicode 18.0.0 rule shape
(InCB=Linker InCB=Extend* × InCB=Consonant), so क्१ and virama+ZWNJ break
while real conjuncts join. The generator runs the pinned rustfmt on its
output, so regeneration is byte-identical without a manual format step.

terminal.ambiguous_width

terminal.ambiguous_width = "narrow" | "wide" (default narrow) flows
Lua -> plan -> merge -> effective -> runtime -> terminal state and the
renderer's span math, is fail-closed at every layer, appears in
inspect config/the profile schema, and is classified restart-required
because committed Cell::width values cannot be re-measured live.

Clusters as one cell

The print path charges an extended grapheme cluster to one leading cell
(plus a spacer for wide clusters) and stores the tail in the bounded
Cell::zerowidth buffer: ZWJ families, flags, skin tones, keycaps, VS16
emoji, decomposed Hangul, Indic conjuncts, and spacing marks stay one layout
unit for cursor movement, backspace, selection, copy, and search. Every
Prepend scalar is zero-width through the same generated table GB9b reads,
so a prepend can no longer hide the base it joins to.

restore_scrollback_text

Session restore now selects the first non-zero-width scalar as Cell::glyph,
attaches leading zero-width scalars to the preceding cell's tail (stepping
back over a wide spacer, like print), and mirrors print's tail-overflow
policy: a printable scalar past MAX_CLUSTER_SCALARS starts a fresh cell
instead of being truncated. Tests cover "\u{0600}1", "a\u{0600}1", and a
nine-scalar printable cluster.

Multi-glyph cluster paint

A paint group is one extended grapheme cluster, not one shaper byte offset,
and every drawable member paints at the shaper's pen. The per-cell advance
gate skips multi-member groups (their advances are intra-cluster pen steps)
and measures the shaper's own cell count, so a wide ambiguous cluster stays
shaped under wide. Real GridRenderer capture (Noto fonts, 10x22 cell),
painted glyphs before -> after:

cluster committed cells shaped glyphs before after
कि 1 2 1 2
क्ष 1 3 2 3
กำ 1 3 1 3
কে 1 2 1 2
가 2 1 (composed) 1 1
🇫🇷 / 🇫🇮 2 1 each 1 / 1 1 / 1, distinct pixels

Deterministic FakeShaper tests pin the same invariants without host fonts
(two-member one-cell cluster, two-offset conjunct, non-ligating flag pair,
ZWJ family ligature, wide ambiguous scalar), and the classic crossfont path
stays a documented degraded fallback (lead glyph plus zero-width marks).

Review rounds

Verification

  • cargo fmt --all -- --check
  • cargo clippy -p bitty-term-state -p bitty-vt -p bitty-render -p bitty-runtime -p bitty-terminal --all-targets --locked -- -D warnings
  • cargo test -p bitty-term-state - all suites, including the 853
    GraphemeBreakTest-18.0.0.txt conformance samples
  • cargo test -p bitty-vt
  • cargo test -p bitty-render --lib - 384 passed, 1 failed:
    font_index::tests::boundary_truncated_index_is_rejected is flaky on
    main. Its fixed 5-byte truncation lattice interacts with the
    variable-length temporary path in the serialized fixture, so repeated runs
    of the same binary flip between pass and fail (observed on both this branch
    and a clean origin/main worktree). This branch does not touch
    font_index.rs; the test is owned by [render] Font index follow-ups: churn above caps, test gaps, PB-1 harness #2733 / [CTX-1610] fix(render): close the font index follow-ups #2736.
  • cargo test -p bitty-terminal --bin bitty --locked -- --test-threads=1
  • RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-term-state -p bitty-vt -p bitty-render -p bitty-runtime -p bitty-terminal --no-deps --locked

Closes #1767

… prepends safely

Second review of the grapheme rework (bitty#1767); rebased onto the merged
first round (bitty#2734), so this commit is the follow-up only.

Render (must-fix 1):
- A paint group with more than one shaped member is one committed layout
  unit; its entries advance inside that unit (`कि`: 4.16 px + 13.81 px for
  one cell), so the per-cell advance gate must not read them as a cell
  advance. The gate now skips multi-member groups, and entries sharing a
  byte offset are excluded from the median that serves as its reference.
  Deterministic FakeShaper cases (`कि`, `क्ष`, a flag pair without a
  ligature, a ZWJ family, a wide ambiguous scalar) pin every member painting
  without host fonts.
- The gate measures the shaper's own (narrow) cell count, so a wide East
  Asian ambiguous cluster stays shaped under
  `terminal.ambiguous_width = "wide"` instead of degrading because the
  policy span is two cells.

Terminal truth (must-fix 2):
- `restore_scrollback_text` selects the first non-zero-width scalar as
  `Cell::glyph`, so a leading `Prepend` (`"\u{0600}1"`) can no longer hide
  its base. Leading zero-width scalars attach to the preceding cell's tail
  (stepping back over a wide spacer) exactly like `print`, and a
  line-leading one is dropped.
- Tail overflow mirrors print: zero-width scalars past the bound drop, a
  printable scalar starts a fresh cell instead of truncating the line.
- Restore tests cover `"\u{0600}1"`, `"a\u{0600}1"`, and a nine-scalar
  printable cluster.

Follow-ups:
- The generated UAX #29 tables now carry the full
  `Grapheme_Cluster_Break=Control` class (19 ranges, 3893 scalars),
  replacing the curated GB4/GB5 list that missed soft hyphen, the Mongolian
  vowel separator, the bidi isolates, the interlinear/musical/shorthand and
  Egyptian format scalars, and the tag-plane ranges. CR/LF stay included
  explicitly per GB4/GB5. The measured over-join cases are pinned.
- `InCB=Linker` doc count corrected to 23.
- The generator formats its output with the pinned `rustfmt`, so regeneration
  is byte-identical without a manual `cargo fmt` step.
- The prepend test documents that only a first-row lead is dropped; a
  later-row lead attaches to the previous row's last cell.
- Drive-by: two pre-existing private intra-doc links broke the rustdoc gate on
  main (`SwashSingle` -> `system_db`, `commit_profile_rename` ->
  `PROFILE_RENAME_OVERRIDE`). Both are code spans now; the identical one-line
  fixes ride in #2737, so on a rebase after it lands these hunks are no-ops.
@Xuepoo Xuepoo added this to the v0.3.0 milestone Oct 27, 2026
@Xuepoo Xuepoo added feat Feature P1 Priority: high area:term Area: terminal state / VT labels Oct 27, 2026
@coderabbitai

coderabbitai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 48 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3f113db7-a22b-4df2-9c22-c58ae9748fac
Commits

Reviewing files that changed from the base of the PR and between f75d8eb and 5301f3e.

Files selected for processing: 10
  • crates/bitty-render/src/grid.rs
  • crates/bitty-render/src/grid/tests.rs
  • crates/bitty-render/src/shaped.rs
  • crates/bitty-render/tests/shaped_clusters_paint.rs
  • crates/bitty-term-state/src/grapheme.rs
  • crates/bitty-term-state/src/state.rs
  • crates/bitty-term-state/src/unicode_tables.rs
  • crates/bitty-term-state/tests/grapheme_clusters_1609.rs
  • crates/bitty-terminal/src/profile.rs
  • scripts/generate-unicode-tables.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Xuepoo
Xuepoo merged commit e9097c2 into main Oct 27, 2026
20 checks passed
@Xuepoo
Xuepoo deleted the ctx-1609/feat-grapheme-eaw branch October 27, 2026 03:49

@Xuepoo Xuepoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR-2738 REVIEWER VERDICT: APPROVE

Attribution: independent reviewer subagent (Bitty blitz guard tick, 2026-10-27). Verification on scratch clone of head 5301f3e (10-file diff); verdict follows.

Verified:

  • Multi-glyph cluster gating (multi_member_entries in shaped.rs): linear equal-offset grouping, max(1) guards division, is_finite() guards per_cell_advance; no recursion/panic paths; safe to unsorted input (false negative only).
  • restore_scrollback_text re-prepend loop bounded by cells.len() < width, checked_sub targets, spacer-precedes-cell invariant, labeled 'line break on wide truncation.
  • Tests: bitty-render grid lib tests 122 passed/0 failed; new CTX-1609 shaped-cluster tests all pass; shaped_clusters_paint integration (5/5, incl. shaping_misaligned == 0); bitty-term-state 13 suites 0-failed incl. new grapheme_clusters_1609 restore tests and unicode table invariants.
  • Per-run Vec<bool> allocation in the shaped path is consistent with existing per-run allocations; informational.
  • cargo check -p bitty-render -p bitty-term-state clean; no let-chains; no hardcoded host/env values.

Non-blocking findings:

  1. multi_member_entries allocates per call in the render path (matches existing style; optional fold-in).
  2. Byte-offset sortedness of shaper output is an unstated precondition (harmless).

CI at review time: 12 pass / 3 pending / 0 fail on head. Head sha at verdict: 5301f3e520d4d63be2f4f54c6ad51c300acf941e. CTX label note: PR title carries [CTX-1609] (guard context said CTX-1610 — label mismatch in the guard prompt only).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:term Area: terminal state / VT feat Feature P1 Priority: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P1] Authoritative grapheme cluster segmentation and East Asian Width tables (OQ-090)

1 participant