ghostty_unicode_codepoint_width function

  1. @Native<Uint8 Function(Uint32)>(ffi.Uint32)>()
int ghostty_unicode_codepoint_width(
  1. int cp
)

Returns the terminal display width of a Unicode codepoint in terminal grid cells: 0, 1, or 2.

This is the same width table the terminal itself uses when laying out printed text, so callers can predict column layout (e.g. IME preedit overlays) that exactly matches what the terminal will do when the text is actually written to it.

Semantics:

  • Returns 0 for zero-width codepoints: C0/C1 control characters, nonspacing and enclosing combining marks, default-ignorable codepoints (ZWJ, ZWNJ, variation selectors, etc.), and surrogate codepoints.
  • Returns 2 for wide codepoints: East Asian Wide/Fullwidth (including emoji with default emoji presentation) and regional indicators. Width is clamped to 2 (e.g. the three-em dash).
  • Returns 1 for everything else, including invalid codepoints beyond U+10FFFF (this function is total; it never fails).

This operates on a single codepoint only and therefore cannot account for grapheme-cluster-level width rules (VS16 emoji presentation, combining sequences, etc.). For cluster-accurate widths, use ghostty_unicode_grapheme_width(). Summing per-codepoint widths is only correct when mode 2027 (grapheme clustering) is disabled.

This function is pure, allocates nothing, and is thread-safe.

@param cp The Unicode codepoint to measure @return Display width in cells: 0, 1, or 2

Implementation

@ffi.Native<ffi.Uint8 Function(ffi.Uint32)>()
external int ghostty_unicode_codepoint_width(int cp);