![]() |
libghostty
|
Unicode codepoint properties matching the terminal's text layout semantics.
Use ghostty_unicode_codepoint_width() to determine how many terminal grid cells a codepoint occupies, using the exact same width table the terminal itself uses when laying out printed text. Use ghostty_unicode_grapheme_width() to segment and measure full grapheme clusters with the same rules the terminal uses when mode 2027 is enabled. These functions are useful for predicting column layout of text that has not yet been written to the terminal, such as IME preedit (composition) overlays.
Functions | |
| GHOSTTY_API uint8_t | ghostty_unicode_codepoint_width (uint32_t cp) |
| GHOSTTY_API size_t | ghostty_unicode_grapheme_width (const uint32_t *cps, size_t len, uint8_t *width) |
| GHOSTTY_API uint8_t ghostty_unicode_codepoint_width | ( | uint32_t | 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:
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.
| cp | The Unicode codepoint to measure |
| GHOSTTY_API size_t ghostty_unicode_grapheme_width | ( | const uint32_t * | cps, |
| size_t | len, | ||
| uint8_t * | width ) |
Measures the terminal display width of the first grapheme cluster in a sequence of Unicode codepoints.
This uses the exact same grapheme segmentation and cluster width rules the terminal itself uses when printing text with grapheme clustering enabled (mode 2027), 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. Unlike ghostty_unicode_codepoint_width(), this accounts for cluster-level rules: emoji variation selectors, ZWJ sequences, combining marks, and skin tone modifiers.
Reads codepoints from cps until the terminal would consider the grapheme cluster complete, stores the cluster's total width in cells (0, 1, or 2) into width (which may be NULL if only segmentation is desired), and returns the number of codepoints consumed. Returns 0 if and only if len is 0; otherwise consumes at least one codepoint. Measure a whole string by calling in a loop:
This is not a streaming API. The provided sequence must contain a complete first grapheme cluster, or the logical end of the string. If input arrives in chunks, keep buffering while this function consumes all available codepoints (return value == len) and the stream may still continue; a later codepoint could still extend the cluster and change its width.
Width semantics, matching the terminal with mode 2027 enabled:
Mode dependence: this models mode 2027 (grapheme clustering) enabled, which is Ghostty's recommended configuration. When mode 2027 is disabled, clusters never combine and variation selectors never change width; predict layout in that case by summing ghostty_unicode_codepoint_width() over each codepoint instead.
Edge cases:
This function is pure, allocates nothing, and is thread-safe.
| cps | Pointer to codepoints (may be NULL only when len is 0) |
| len | Number of codepoints available |
| width | Out: cluster display width in cells (0-2); may be NULL |