Skip to content

[FEATURE REQUEST] Shape terminal text per foreground color section - #37

Merged
Raiden-MeixElysia merged 3 commits into
ELY-Open-Source:mainfrom
totorowldox:perf/terminal-text-shaping
Oct 28, 2026
Merged

Raiden-MeixElysia merged 3 commits into
ELY-Open-Source:mainfrom
totorowldox:perf/terminal-text-shaping

Conversation

@totorowldox

Copy link
Copy Markdown
Contributor

Type

  • [FEATURE REQUEST]: a new feature or an improvement
  • [BUG REPORT]: a bug fix
  • [Other]: docs, refactoring, CI, dependencies or chores

What and why

The terminal now shapes each row in sections of one foreground color instead of in one shape_line call that holds all of the row's color runs. On macOS, CoreText takes an expensive path for a line with many differently colored runs, and gpui keeps the run colors in its shape cache key, so a busy screen kept shaping the same text again. Under cmatrix -u 0 the Mac went from ~27 Hz to ~57.7 Hz.

  • Neighboring runs with the same color form one section. Each section is shaped on its own with its runs' colors set to black, so the shape cache depends only on text and font, and its glyphs paint in the section's color. The per-glyph color_at lookup is gone.
  • Each section is clipped at its column edge, snapped to whole device pixels, so a glyph cannot spill into the next color section; the two sides of a boundary share one device-pixel edge. The vertical overhang keeps the row's own mask.
  • Ligatures look as before: !=, -> or == in one color still form, since they shape in one call, and a color change still breaks them.
  • The glyph drawing moves from src/term/paint.rs into src/term/paint/glyphs.rs, and paint.rs's tests into src/term/paint/tests.rs, so every file stays under 500 lines.

How it was tested

scripts/check.sh on the branch merged with main: fmt, clippy -D warnings and the house rules pass, and 295 tests pass. The four tests that need fish or pwsh were not run, since the Mac lacks both:
fish_loads_its_script_and_restores_its_data_dirs, under_wsl_each_shell_reports_its_folder_as_windows_names_it, powershell_reports_its_folder_byte_for_byte_and_keeps_the_status, only_an_interactive_shell_leading_its_group_draws_its_prompt.

New unit tests in src/term/paint/glyphs/tests.rs:

  • shaping keeps each run's font, weight and style, sets only the shaping color to black, and leaves the caller's runs unchanged;
  • an operator split across two colors becomes two sections, and stays one in one color;
  • sections keep UTF-8 boundaries, and a font change inside one color stays in one section;
  • the clips on both sides of a boundary share one device-pixel edge (at scale 1.5) and keep the outer top and bottom;
  • a clip outside the viewport has zero width, never negative.

By hand: ligatures and color boundaries on macOS, Windows 11 and NixOS; CJK, powerline and Nerd Font icons on macOS.

Frame rate under cmatrix -u 0, on two machines: a Mac, and one PC that runs both Windows 11 and NixOS. Each overlay counts frames its own way, so compare within a row:

Machine Platform Shell Overlay main This PR Reference
Mac macOS window-content sampling ~27 Hz ~57.7 Hz
PC Windows 11 PowerShell 5.1 Bandicam FPS overlay ~90 fps Windows Terminal: about the same
PC NixOS 26.05 zsh 5.9.1 MangoHud ~100 fps kitty: ~80 fps

Checklist

  • The title starts with [FEATURE REQUEST], [BUG REPORT] or [Other]
  • Tests and the build pass locally (except the four fish/pwsh tests above)
  • Docs are updated if behaviour changed: what the terminal draws is unchanged, so no docs change

Shape foreground sections independently to preserve color-boundary ligature breaks without CoreText's expensive multi-color line path. Clip section ink at device-aligned boundaries and retain same-color ligatures.

Validation: fmt, clippy -D warnings, release build, visual checks and 273 library tests passed. Three existing shell tests require unavailable fish/pwsh and were excluded. Window-content sampling under cmatrix remains about 57.7 Hz.
@Raiden-MeixElysia
Raiden-MeixElysia merged commit 09ca1c6 into ELY-Open-Source:main Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants