@@ -2,6 +2,9 @@ import type { Color, TextInputProps } from "@cel-tui/types";
22 import type { Cell } from "./cell-buffer.js";
33 import { CellBuffer } from "./cell-buffer.js";
44 import type { LayoutNode, Rect } from "./layout.js";
5+import { visibleWidth } from "./width.js";
6+
7+const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
58
69 /**
710 * Paint a laid-out tree into a cell buffer.
@@ -59,6 +62,35 @@ function makeCell(
5962 };
6063 }
6164
65+/**
66+ * Paint a single line of text into the buffer using grapheme segmentation.
67+ * Correctly handles wide characters (CJK, emoji) by advancing the column
68+ * by the grapheme's visible width.
69+ */
70+function paintLineGraphemes(
71+ line: string,
72+ x: number,
73+ y: number,
74+ maxWidth: number,
75+ props: {
76+ fgColor?: Color;
77+ bgColor?: Color;
78+ bold?: boolean;
79+ italic?: boolean;
80+ underline?: boolean;
81+ },
82+ buf: CellBuffer,
83+): void {
84+ let col = 0;
85+ for (const { segment } of segmenter.segment(line)) {
86+ const gw = visibleWidth(segment);
87+ if (gw === 0) continue;
88+ if (col + gw > maxWidth) break; // clip: grapheme doesn't fit
89+ buf.set(x + col, y, makeCell(segment, props));
90+ col += gw;
91+ }
92+}
93+
6294 function paintText(
6395 content: string,
6496 props: {
@@ -79,7 +111,10 @@ function paintText(
79111 // Resolve repeat
80112 let text = content;
81113 if (props.repeat === "fill" && content.length > 0) {
82- text = content.repeat(Math.ceil(w / content.length)).slice(0, w);
114+ const contentW = visibleWidth(content);
115+ if (contentW > 0) {
116+ text = content.repeat(Math.ceil(w / contentW));
117+ }
83118 } else if (typeof props.repeat === "number" && props.repeat > 0) {
84119 text = content.repeat(props.repeat);
85120 }
@@ -91,7 +126,7 @@ function paintText(
91126 const lines: string[] = [];
92127 if (props.wrap === "word") {
93128 for (const rawLine of rawLines) {
94- if (rawLine.length <= w) {
129+ if (visibleWidth(rawLine) <= w) {
95130 lines.push(rawLine);
96131 } else {
97132 wrapLine(rawLine, w, lines);
@@ -101,12 +136,10 @@ function paintText(
101136 lines.push(...rawLines);
102137 }
103138
104- // Paint lines, clipped to rect
139+ // Paint lines, clipped to rect (grapheme-aware)
105140 for (let row = 0; row < lines.length && row < h; row++) {
106141 const line = lines[row]!;
107- for (let col = 0; col < line.length && col < w; col++) {
108- buf.set(x + col, y + row, makeCell(line[col]!, props));
109- }
142+ paintLineGraphemes(line, x, y + row, w, props, buf);
110143 }
111144 }
112145
@@ -130,7 +163,7 @@ function paintTextInput(
130163 // Word-wrap value (always on for TextInput)
131164 const lines: string[] = [];
132165 for (const rawLine of value.split("\n")) {
133- if (rawLine.length <= w) {
166+ if (visibleWidth(rawLine) <= w) {
134167 lines.push(rawLine);
135168 } else {
136169 wrapLine(rawLine, w, lines);
@@ -140,14 +173,12 @@ function paintTextInput(
140173 // Framework-managed scroll: use stored scroll offset
141174 const scrollOffset = getTextInputScroll(props);
142175
143- // Paint visible lines
176+ // Paint visible lines (grapheme-aware)
144177 for (let row = 0; row < h; row++) {
145178 const lineIdx = scrollOffset + row;
146179 if (lineIdx >= lines.length) break;
147180 const line = lines[lineIdx]!;
148- for (let col = 0; col < line.length && col < w; col++) {
149- buf.set(x + col, y + row, makeCell(line[col]!, props));
150- }
181+ paintLineGraphemes(line, x, y + row, w, props, buf);
151182 }
152183
153184 // Paint cursor if focused
@@ -188,14 +219,18 @@ function offsetToWrappedPos(
188219 // Cursor is in this raw line
189220 const colInRaw = cursor - offset;
190221 if (width <= 0) return { line: wrappedLine, col: colInRaw };
191- const extraLines = Math.floor(colInRaw / width);
192- return { line: wrappedLine + extraLines, col: colInRaw % width };
222+ // Compute visible width of text before cursor
223+ const textBeforeCursor = rawLine.slice(0, colInRaw);
224+ const vw = visibleWidth(textBeforeCursor);
225+ const extraLines = Math.floor(vw / width);
226+ return { line: wrappedLine + extraLines, col: vw % width };
193227 }
194228 // Count wrapped lines for this raw line
195- if (rawLine.length <= width || width <= 0) {
229+ const lineVW = visibleWidth(rawLine);
230+ if (lineVW <= width || width <= 0) {
196231 wrappedLine += 1;
197232 } else {
198- wrappedLine += Math.ceil(rawLine.length / width);
233+ wrappedLine += Math.ceil(lineVW / width);
199234 }
200235 offset += rawLine.length + 1; // +1 for \n
201236 }
@@ -241,22 +276,46 @@ function wrapLine(line: string, width: number, out: string[]): void {
241276 if (width <= 0) return;
242277
243278 let current = "";
279+ let currentW = 0;
244280 const words = line.split(" ");
245281
246282 for (const word of words) {
247- if (current.length === 0) {
283+ const wordW = visibleWidth(word);
284+ if (currentW === 0) {
248285 current = word;
249- } else if (current.length + 1 + word.length <= width) {
286+ currentW = wordW;
287+ } else if (currentW + 1 + wordW <= width) {
250288 current += " " + word;
289+ currentW += 1 + wordW;
251290 } else {
252291 out.push(current);
253292 current = word;
293+ currentW = wordW;
254294 }
255295
256- // Handle words longer than width (break by character)
257- while (current.length > width) {
258- out.push(current.slice(0, width));
259- current = current.slice(width);
296+ // Handle words longer than width (break by grapheme)
297+ while (currentW > width) {
298+ let taken = "";
299+ let takenW = 0;
300+ let rest = "";
301+ let inRest = false;
302+ for (const { segment } of segmenter.segment(current)) {
303+ if (inRest) {
304+ rest += segment;
305+ continue;
306+ }
307+ const gw = visibleWidth(segment);
308+ if (takenW + gw > width) {
309+ rest += segment;
310+ inRest = true;
311+ } else {
312+ taken += segment;
313+ takenW += gw;
314+ }
315+ }
316+ out.push(taken);
317+ current = rest;
318+ currentW = visibleWidth(rest);
260319 }
261320 }