Extend Ghostty CJK font-fallback injection to symbol ranges (⬡ U+2B21, ▰/▱ gauges)

Merged
AM
2mo ago
+409-6

Summary

  • Ghostty's CTFontCollection scoring in discoverFallback() (src/font/discovery.zig) is monospace-biased: when a codepoint isn't in the configured font, it can pick whatever installed "monospace" font also claims coverage instead of the narrower substitute CoreText's own CTFontCreateForString cascade would choose.
  • cmux already works around this for CJK ranges via loadCJKFontFallbackIfNeeded (#1017 / GhosttyConfigDiscovery.autoInjectedCJKFontMappings), but that injection is scoped to CJK-only ranges.
  • Nanako0129/coralline#47 traced this exact scoring bug through the Geometric Shapes (▰/▱/●/○) and Miscellaneous Symbols and Arrows (⬡ U+2B21, ⬢ U+2B22) ranges, used by status-line tools rendering gauges/icons inside the terminal — and confirmed it reproduces specifically inside cmux (which vends its own Ghostty fork with the identical discovery.zig fallback logic for the CoreText path).
  • This extends the same font-codepoint-map auto-injection mechanism to those symbol ranges, pointing them at Apple Symbols (the font CoreText's own cascade resolves to for these ranges), so the fallback choice is predictable instead of landing on an arbitrary, potentially much wider "monospace" font.
  • Like the CJK path, injection only happens when the user hasn't already configured font-codepoint-map, an explicit multi-font font-family fallback chain, or a primary font that already covers the range — so it never overrides user-managed fallback.

Changes

  • GhosttyConfigDiscovery.swift: adds symbolRanges, symbolFallbackFont, symbolCoverageSampleCharactersByRange, symbolFontMappings(), autoInjectedSymbolFontMappings(), shouldInjectSymbolFontFallback().
  • GhosttyTerminalView.swift: wires loadSymbolFontFallbackIfNeeded into loadDefaultConfigFilesWithLegacyFallback right after the existing CJK loader, plus thin static forwarders mirroring the CJK ones.
  • GhosttyConfigDiscoveryTests.swift: new GhosttyConfigDiscoverySymbolTests suite covering mapping generation and override precedence.

Test plan

  • swift test --filter GhosttyConfigDiscovery in Packages/macOS/CmuxTerminalCore — 23/23 pass, including the 6 new symbol-fallback tests.
  • Full cmux app-target build (xcodebuild -scheme cmux, via ./scripts/reload.sh) — passes. The earlier zig 0.15.2 gap was environment-specific and resolved with ./scripts/setup.sh (zig 0.16.0, cached GhosttyKit.xcframework). Build then hit an unrelated pre-existing main bug (CLI/CMUXCLI+AgentHookFailureReporting.swift had a non-exhaustive switch missing AgentHookFailureStage.journalAppend, from an unrelated upstream commit); fixed via a cherry-picked commit (fix: handle AgentHookFailureStage.journalAppend in failure reporting) so this branch builds clean end to end.
  • Manual repro: launched a tagged Debug build with an isolated XDG_CONFIG_HOME pointing at font-family = JetBrainsMono Nerd Font (no CJK language, no explicit font-family fallback chain, no font-codepoint-map). Printed ⬡ ⬢ ▰ ▱ ● ○ inside the terminal — all render as compact, near-1-cell-width, predictable glyphs, not an arbitrary wide substitute.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Extends Ghostty's font-fallback auto-injection to symbol codepoints (⬡ U+2B21, ▰/▱ gauges) so status-line glyphs resolve to Apple Symbols instead of an arbitrary wide monospace font. Injection matches CoreText's cascade and only covers codepoints the configured font — or the default embedded JetBrains Mono face, when no font-family is set — doesn't already render.

  • Skips injection when the user has font-codepoint-map or a multi-entry font-family chain, and fails closed when the configured font can't be probed (for both symbol and CJK injection).
  • Shares one config scan and one font-codepoint-map directive builder between the CJK and symbol loaders so the two paths can't drift apart; coverage probing handles non-BMP codepoints via surrogate pairs.
  • Adds tests for per-codepoint filtering, default-face coverage (with a drift check against Ghostty's embedded JetBrains Mono that skips when the submodule isn't checked out), surrogate pairs, and fail-closed behavior.

Summary by CodeRabbit

  • New Features

    • Added automatic symbol/pictographic font fallback by discovering uncovered symbol codepoint ranges and injecting matching font-codepoint-map directives when needed.
  • Bug Fixes

    • Improved fallback injection rules to avoid overriding user-provided font-codepoint-map or explicit multi-step font-family fallback chains.
    • Tightened “fail-closed” behavior so fallback auto-injection is skipped when required fonts can’t be verified.
  • Tests

    • Added coverage for symbol mapping discovery, injection filtering using glyph coverage probing, consistency checks for injection decisions, and CJK fail-closed behavior.

[!NOTE]
Low Risk
Startup-only Ghostty config injection for font fallbacks; behavior is gated on user config and glyph coverage, with no auth or data-path changes.

Overview
Adds symbol-glyph font-codepoint-map auto-injection alongside the existing CJK workaround so glyphs like ⬡ (U+2B21) and ▰/▱ gauges map to Apple Symbols instead of an arbitrary wide monospace fallback from Ghostty’s CoreText scoring.

Injection is per codepoint (with coverage probing via new fontContainsGlyph, including surrogate pairs), skips user font-codepoint-map / multi-font-family chains, and when there is no font-family only overrides codepoints the embedded default JetBrains Mono lacks. CJK auto-injection now fails closed when the configured font family cannot be resolved, matching the symbol path.

GhosttyTerminalView resolves config scan paths once, runs the new symbol loader after CJK, and shares loadInjectedFontCodepointMap for both. Tests cover override rules, partial Nerd Font coverage, default-face filtering, and vendored-font drift for defaultFaceCoveredSymbolCodepoints.

Reviewed by Cursor Bugbot for commit 79da9c0a84390ea85254f26958e3f8ba578b7546. Bugbot is set up for automated code reviews on this repo. Configure here.