fix: add CJK font fallback to prevent decorative font rendering

Merged
AT
9mo ago
+353-0

Summary

  • On macOS, Ghostty has two font fallback paths for CJK characters. CTFontCreateForString handles CJK Unified Ideographs (U+4E00-9FFF) and typically selects Hiragino Sans correctly. However, CTFontCollection with scoring handles Hiragana, Katakana, and symbols — and may select an unintended decorative font.
  • The scoring prioritizes: codepoint coverage → monospace → Regular style → glyph count. Decorative fonts with monospace attributes (e.g. AB_appare from Adobe CC, or LingWai) can be scored highest depending on installed fonts.
  • Add automatic font-codepoint-map injection based on the system's preferred language to bypass the unreliable fallback.
  • The fallback is only applied when no font-codepoint-map is already configured by the user.

Details

When the primary font (e.g. Menlo) lacks CJK glyphs, libghostty uses two fallback mechanisms. CTFontCreateForString handles CJK Unified Ideographs (U+4E00-9FFF) and usually selects Hiragino Sans W3 correctly. For Hiragana, Katakana, and CJK symbols, CTFontCollection scoring is used instead — and the result depends on which fonts are installed on the system.

The scoring prioritizes monospace fonts, which causes decorative fonts with monospace attributes to be selected unexpectedly. For example, AB_appare (synced via Adobe Creative Cloud) or LingWai may be chosen over appropriate system fonts like Hiragino Sans.

This fix injects font-codepoint-map defaults based on Locale.preferredLanguages:

LanguageFont
jaHiragino Sans
koApple SD Gothic Neo
zh-Hant / zh-TW / zh-HKPingFang TC
zhPingFang SC

Covered Unicode ranges:

  • Hiragana, Katakana, CJK Symbols and Punctuation
  • CJK Unified Ideographs and Extension A
  • CJK Compatibility Ideographs
  • Halfwidth and Fullwidth Forms
  • Hangul Syllables and Jamo

If the user has any font-codepoint-map in their Ghostty config, the automatic fallback is skipped.

Before

Hiragana and Katakana render in a decorative font (varies by environment — AB_appare, LingWai, etc.).

After

Japanese text renders in Hiragino Sans, the standard macOS system font.

Test plan

  • Unit tests for preferredCJKFontFamily (ja/ko/zh-Hant/zh-Hans/non-CJK)
  • Unit tests for userConfigContainsCJKCodepointMap (present/absent/commented/missing)
  • All 9 new tests pass locally