Open History to see the last commit
1import {
2 GHOSTTY_CELL_WIDE,
3 ghosttyColorsEqual,
4 type GhosttyCell,
5 type GhosttyColor,
6 type GhosttySnapshot,
7} from "./core";
8
9export interface GhosttyCellMetrics {
10 readonly width: number;
11 readonly height: number;
12 readonly baseline: number;
13}
14
15const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)";
16
17function cssColor(color: GhosttyColor): string {
18 return `rgb(${color.r}, ${color.g}, ${color.b})`;
19}
20
21function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean {
22 // Selection deliberately does not participate: it only tints the background
23 // overlay, and splitting a text run at a selection boundary visibly shifts
24 // glyph spacing whenever the face's true advance differs from the cell width.
25 return (
26 ghosttyColorsEqual(left.foreground, right.foreground) &&
27 left.bold === right.bold &&
28 left.italic === right.italic &&
29 left.invisible === right.invisible
30 );
31}
32
33export function ghosttyTextRunEnd(
34 cells: readonly GhosttyCell[],
35 start: number,
36 sameStyle: (cell: GhosttyCell) => boolean,
37): number {
38 let end = start + 1;
39 while (end < cells.length) {
40 const next = cells[end];
41 if (!next) break;
42 if (next.wide === GHOSTTY_CELL_WIDE.spacerTail) {
43 end += 1;
44 continue;
45 }
46 if (next.text.length === 0 || !sameStyle(next)) break;
47 end += 1;
48 }
49 return end;
50}
51
52function fontForCell(cell: GhosttyCell, fontSize: number, fontFamily: string): string {
53 const style = cell.italic ? "italic" : "normal";
54 const weight = cell.bold ? "700" : "400";
55 return `${style} ${weight} ${fontSize}px ${fontFamily}`;
56}
57
58export function measureGhosttyCell(
59 context: CanvasRenderingContext2D,
60 fontSize: number,
61 fontFamily: string,
62): GhosttyCellMetrics {
63 context.font = `normal 400 ${fontSize}px ${fontFamily}`;
64 const widthMeasurement = context.measureText("M");
65 const verticalMeasurement = context.measureText("Mg");
66 const ascent = verticalMeasurement.actualBoundingBoxAscent || fontSize;
67 const descent = verticalMeasurement.actualBoundingBoxDescent;
68 const glyphHeight = ascent + descent;
69 const height = Math.max(1, Math.round(fontSize * 1.35), Math.ceil(glyphHeight));
70 return {
71 width: Math.max(1, widthMeasurement.width),
72 height,
73 baseline: Math.round((height - glyphHeight) / 2 + ascent),
74 };
75}
76
77export function terminalGridSize(
78 width: number,
79 height: number,
80 metrics: GhosttyCellMetrics,
81 padding: number,
82): { cols: number; rows: number } {
83 return {
84 cols: Math.max(1, Math.floor((width - padding * 2) / metrics.width)),
85 rows: Math.max(1, Math.floor((height - padding * 2) / metrics.height)),
86 };
87}
88
89export function renderGhosttySnapshot(options: {
90 readonly context: CanvasRenderingContext2D;
91 readonly snapshot: GhosttySnapshot;
92 readonly metrics: GhosttyCellMetrics;
93 readonly fontSize: number;
94 readonly fontFamily: string;
95 readonly padding: number;
96 readonly forceFull: boolean;
97 readonly cursorOn: boolean;
98 readonly previousCursorY?: number | null;
99 readonly focused?: boolean;
100 readonly selectionBackground?: string;
101 /** Vertical origin of row 0; defaults to the horizontal padding. */
102 readonly originY?: number;
103}): void {
104 const {
105 context,
106 snapshot,
107 metrics,
108 fontSize,
109 fontFamily,
110 padding,
111 forceFull,
112 cursorOn,
113 previousCursorY,
114 } = options;
115 const focused = options.focused ?? true;
116 const selectionBackground = options.selectionBackground ?? DEFAULT_SELECTION_BACKGROUND;
117 const originY = options.originY ?? padding;
118 const rowsToDraw = forceFull
119 ? Array.from({ length: snapshot.rows }, (_, index) => index)
120 : [...snapshot.dirtyRows];
121 if (
122 previousCursorY !== null &&
123 previousCursorY !== undefined &&
124 previousCursorY >= 0 &&
125 !rowsToDraw.includes(previousCursorY)
126 ) {
127 rowsToDraw.push(previousCursorY);
128 }
129 if (snapshot.cursorVisible && snapshot.cursorY >= 0 && !rowsToDraw.includes(snapshot.cursorY)) {
130 rowsToDraw.push(snapshot.cursorY);
131 }
132
133 if (forceFull) {
134 context.save();
135 context.resetTransform();
136 context.fillStyle = cssColor(snapshot.background);
137 context.fillRect(0, 0, context.canvas.width, context.canvas.height);
138 context.restore();
139 }
140
141 context.textBaseline = "alphabetic";
142 for (const rowIndex of rowsToDraw) {
143 const row = snapshot.rowData[rowIndex];
144 if (!row) continue;
145 const top = originY + rowIndex * metrics.height;
146
147 context.fillStyle = cssColor(snapshot.background);
148 context.fillRect(padding, top, snapshot.cols * metrics.width, metrics.height);
149
150 let backgroundStart = 0;
151 while (backgroundStart < row.cells.length) {
152 const first = row.cells[backgroundStart];
153 if (!first) break;
154 let backgroundEnd = backgroundStart + 1;
155 while (backgroundEnd < row.cells.length) {
156 const next = row.cells[backgroundEnd];
157 if (
158 !next ||
159 next.selected !== first.selected ||
160 !ghosttyColorsEqual(next.background, first.background)
161 ) {
162 break;
163 }
164 backgroundEnd += 1;
165 }
166 if (first.selected || !ghosttyColorsEqual(first.background, snapshot.background)) {
167 const left = padding + backgroundStart * metrics.width;
168 const width = (backgroundEnd - backgroundStart) * metrics.width;
169 if (!ghosttyColorsEqual(first.background, snapshot.background)) {
170 context.fillStyle = cssColor(first.background);
171 context.fillRect(left, top, width, metrics.height);
172 }
173 if (first.selected) {
174 context.fillStyle = selectionBackground;
175 context.fillRect(left, top, width, metrics.height);
176 }
177 }
178 backgroundStart = backgroundEnd;
179 }
180
181 let runStart = 0;
182 while (runStart < row.cells.length) {
183 const first = row.cells[runStart];
184 if (!first) break;
185 if (first.text.length === 0) {
186 runStart += 1;
187 continue;
188 }
189 const runEnd = ghosttyTextRunEnd(row.cells, runStart, (cell) => sameTextStyle(cell, first));
190 const text = row.cells
191 .slice(runStart, runEnd)
192 .map((cell) => cell.text)
193 .join("");
194 if (!first.invisible && text.trim().length > 0) {
195 context.save();
196 context.beginPath();
197 context.rect(
198 padding + runStart * metrics.width,
199 top,
200 (runEnd - runStart) * metrics.width,
201 metrics.height,
202 );
203 context.clip();
204 context.font = fontForCell(first, fontSize, fontFamily);
205 context.fillStyle = cssColor(first.foreground);
206 context.fillText(
207 text,
208 padding + runStart * metrics.width,
209 top + metrics.baseline,
210 (runEnd - runStart) * metrics.width,
211 );
212 context.restore();
213 }
214 runStart = runEnd;
215 }
216
217 for (let column = 0; column < row.cells.length; column += 1) {
218 const cell = row.cells[column];
219 if (!cell || (!cell.underline && !cell.strikethrough && !cell.overline)) continue;
220 context.fillStyle = cssColor(cell.foreground);
221 const left = padding + column * metrics.width;
222 if (cell.underline) context.fillRect(left, top + metrics.height - 2, metrics.width, 1);
223 if (cell.strikethrough) {
224 context.fillRect(left, top + Math.floor(metrics.height * 0.55), metrics.width, 1);
225 }
226 if (cell.overline) context.fillRect(left, top + 1, metrics.width, 1);
227 }
228 }
229
230 if (cursorOn && snapshot.cursorVisible && snapshot.cursorX >= 0 && snapshot.cursorY >= 0) {
231 const left = padding + snapshot.cursorX * metrics.width;
232 const top = originY + snapshot.cursorY * metrics.height;
233 context.fillStyle = cssColor(snapshot.cursor);
234 if (!focused) {
235 // An unfocused terminal draws a hollow cursor so the active pane is obvious.
236 context.strokeStyle = cssColor(snapshot.cursor);
237 context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
238 } else if (snapshot.cursorStyle === 0) {
239 context.fillRect(left, top, 2, metrics.height);
240 } else if (snapshot.cursorStyle === 2) {
241 context.fillRect(left, top + metrics.height - 2, metrics.width, 2);
242 } else if (snapshot.cursorStyle === 3) {
243 context.strokeStyle = cssColor(snapshot.cursor);
244 context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
245 } else {
246 context.fillRect(left, top, metrics.width, metrics.height);
247 const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
248 if (cell?.text) {
249 context.font = fontForCell(cell, fontSize, fontFamily);
250 context.fillStyle = cssColor(snapshot.background);
251 context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
252 }
253 }
254 }
255}