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