Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

unicode.h

165 lines6.7 KB

1/**
2 * @file unicode.h
3 *
4 * Unicode utilities - codepoint properties matching the terminal's
5 * text layout semantics.
6 */
7​
8#ifndef GHOSTTY_VT_UNICODE_H
9#define GHOSTTY_VT_UNICODE_H
10​
11/** @addtogroup unicode
12 *
13 * These measure text the same way the terminal does, with the same width table
14 * and the same grapheme cluster rules.
15 *
16 * Use them to predict the layout of text you haven't written to the terminal,
17 * such as an IME preedit overlay. ghostty_unicode_codepoint_width() measures one
18 * codepoint. ghostty_unicode_grapheme_width() finds and measures a full grapheme
19 * cluster, with the rules the terminal uses when mode 2027 is on. You don't need
20 * a terminal.
21 *
22 * ## Example
23 *
24 * @code{.c}
25 * uint8_t width = ghostty_unicode_codepoint_width(0x1F600);
26 * @endcode
27 *
28 * We don't have a guide or an example for these yet.
29 *
30 * @{
31 */
32​
33#include <stddef.h>
34#include <stdint.h>
35#include <ghostty/vt/types.h>
36​
37#ifdef __cplusplus
38extern "C" {
39#endif
40​
41/**
42 * Returns the terminal display width of a Unicode codepoint in
43 * terminal grid cells: 0, 1, or 2.
44 *
45 * This is the same width table the terminal itself uses when laying
46 * out printed text, so callers can predict column layout (e.g. IME
47 * preedit overlays) that exactly matches what the terminal will do
48 * when the text is actually written to it.
49 *
50 * Semantics:
51 * - Returns 0 for zero-width codepoints: C0/C1 control characters,
52 * nonspacing and enclosing combining marks, default-ignorable
53 * codepoints (ZWJ, ZWNJ, variation selectors, etc.), and
54 * surrogate codepoints.
55 * - Returns 2 for wide codepoints: East Asian Wide/Fullwidth
56 * (including emoji with default emoji presentation) and regional
57 * indicators. Width is clamped to 2 (e.g. the three-em dash).
58 * - Returns 1 for everything else, including invalid codepoints
59 * beyond U+10FFFF (this function is total; it never fails).
60 *
61 * This operates on a single codepoint only and therefore cannot account
62 * for grapheme-cluster-level width rules (VS16 emoji presentation,
63 * combining sequences, etc.). For cluster-accurate widths, use
64 * ghostty_unicode_grapheme_width(). Summing per-codepoint widths is only
65 * correct when mode 2027 (grapheme clustering) is disabled.
66 *
67 * This function is pure, allocates nothing, and is thread-safe.
68 *
69 * @param cp The Unicode codepoint to measure
70 * @return Display width in cells: 0, 1, or 2
71 */
72GHOSTTY_API uint8_t ghostty_unicode_codepoint_width(uint32_t cp);
73​
74/**
75 * Measures the terminal display width of the first grapheme cluster in a
76 * sequence of Unicode codepoints.
77 *
78 * This uses the exact same grapheme segmentation and cluster width rules
79 * the terminal itself uses when printing text with grapheme clustering
80 * enabled (mode 2027), so callers can predict column layout (e.g. IME
81 * preedit overlays) that exactly matches what the terminal will do when
82 * the text is actually written to it. Unlike
83 * ghostty_unicode_codepoint_width(), this accounts for cluster-level
84 * rules: emoji variation selectors, ZWJ sequences, combining marks, and
85 * skin tone modifiers.
86 *
87 * Reads codepoints from cps until the terminal would consider the
88 * grapheme cluster complete, stores the cluster's total width in cells
89 * (0, 1, or 2) into width (which may be NULL if only segmentation is
90 * desired), and returns the number of codepoints consumed. Returns 0 if
91 * and only if len is 0; otherwise consumes at least one codepoint. Measure
92 * a whole string by calling in a loop:
93 *
94 * @code
95 * size_t total = 0;
96 * for (size_t i = 0; i < len;) {
97 * uint8_t width;
98 * i += ghostty_unicode_grapheme_width(cps + i, len - i, &width);
99 * total += width;
100 * }
101 * @endcode
102 *
103 * This is not a streaming API. The provided sequence must contain a
104 * complete first grapheme cluster, or the logical end of the string. If
105 * input arrives in chunks, keep buffering while this function consumes all
106 * available codepoints (return value == len) and the stream may still
107 * continue; a later codepoint could still extend the cluster and change
108 * its width.
109 *
110 * Width semantics, matching the terminal with mode 2027 enabled:
111 * - The cluster starts at the width of its first codepoint, as returned by
112 * ghostty_unicode_codepoint_width().
113 * - VS16 (U+FE0F) forces the cluster wide (2) and VS15 (U+FE0E) forces it
114 * narrow (1), but only when the immediately preceding codepoint in the
115 * cluster is a valid emoji variation sequence base (per Unicode
116 * emoji-variation-sequences.txt). Invalid variation selectors are
117 * ignored entirely.
118 * - A skin tone modifier (U+1F3FB through U+1F3FF) that continues a
119 * cluster forces it wide (2), just like VS16. A modifier asks for the
120 * emoji form of the codepoint before it, so U+261D U+1F3FB (index
121 * pointing up, light skin tone) is 2 cells wide even though U+261D on
122 * its own is 1. A modifier only continues a cluster after an emoji that
123 * accepts skin tones, or after a Prepend codepoint such as U+0600.
124 * After anything else, such as "a", it starts a new cluster, which is
125 * 2 cells wide on its own.
126 * - Any other continuation codepoint that contributes to grapheme width
127 * forces the cluster wide (2). Note this means cluster width is NOT the
128 * maximum of per-codepoint widths: some continuation marks have narrow
129 * codepoint width yet still widen the cluster.
130 *
131 * Mode dependence: this models mode 2027 (grapheme clustering) enabled,
132 * which is Ghostty's recommended configuration. When mode 2027 is
133 * disabled, clusters never combine and variation selectors never change
134 * width; predict layout in that case by summing
135 * ghostty_unicode_codepoint_width() over each codepoint instead.
136 *
137 * Edge cases:
138 * - Codepoints beyond U+10FFFF consume one codepoint, have width 1, and
139 * are always cluster boundaries. This function is total; it never fails.
140 * - Control characters (C0/C1, CR, LF) are never printed through the
141 * terminal's text path; passing them here returns an unspecified (but
142 * stable and bounded) result.
143 * - A cluster whose first codepoint is zero-width (e.g. a lone combining
144 * mark) is malformed at a cell start; the terminal may attach it to
145 * earlier screen content. This function reports the fold result for the
146 * sequence in isolation (typically 0).
147 *
148 * This function is pure, allocates nothing, and is thread-safe.
149 *
150 * @param cps Pointer to codepoints (may be NULL only when len is 0)
151 * @param len Number of codepoints available
152 * @param width Out: cluster display width in cells (0-2); may be NULL
153 * @return Number of codepoints in the first grapheme cluster
154 */
155GHOSTTY_API size_t ghostty_unicode_grapheme_width(const uint32_t *cps,
156 size_t len,
157 uint8_t *width);
158​
159#ifdef __cplusplus
161#endif
162​
163/** @} */
164​
165#endif /* GHOSTTY_VT_UNICODE_H */