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

renderer.ts

272 lines9.2 KB

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;
14​
15export interface GhosttyCellRange {
16 readonly start: { readonly x: number; readonly y: number };
17 readonly end: { readonly x: number; readonly y: number };
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})`;
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 );
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;
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}`;
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 };
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 };
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 /** Vertical origin of row 0; defaults to the horizontal padding. */
108 readonly originY?: number;
109}): void {
110 const {
111 context,
112 snapshot,
113 metrics,
114 fontSize,
115 fontFamily,
116 padding,
117 forceFull,
118 cursorOn,
119 previousCursorY,
120 } = options;
121 const focused = options.focused ?? true;
122 const selectionBackground = options.selectionBackground ?? DEFAULT_SELECTION_BACKGROUND;
123 const hoveredLinkRange = options.hoveredLinkRange ?? null;
124 const originY = options.originY ?? padding;
125 const rowsToDraw = forceFull
126 ? Array.from({ length: snapshot.rows }, (_, index) => index)
127 : [...snapshot.dirtyRows];
128 if (
129 previousCursorY !== null &&
130 previousCursorY !== undefined &&
131 previousCursorY >= 0 &&
132 !rowsToDraw.includes(previousCursorY)
133 ) {
134 rowsToDraw.push(previousCursorY);
135 }
136 if (snapshot.cursorVisible && snapshot.cursorY >= 0 && !rowsToDraw.includes(snapshot.cursorY)) {
137 rowsToDraw.push(snapshot.cursorY);
138 }
139​
140 if (forceFull) {
141 context.save();
142 context.resetTransform();
143 context.fillStyle = cssColor(snapshot.background);
144 context.fillRect(0, 0, context.canvas.width, context.canvas.height);
145 context.restore();
146 }
147​
148 context.textBaseline = "alphabetic";
149 for (const rowIndex of rowsToDraw) {
150 const row = snapshot.rowData[rowIndex];
151 if (!row) continue;
152 const top = originY + rowIndex * metrics.height;
153​
154 context.fillStyle = cssColor(snapshot.background);
155 context.fillRect(padding, top, snapshot.cols * metrics.width, metrics.height);
156​
157 let backgroundStart = 0;
158 while (backgroundStart < row.cells.length) {
159 const first = row.cells[backgroundStart];
160 if (!first) break;
161 let backgroundEnd = backgroundStart + 1;
162 while (backgroundEnd < row.cells.length) {
163 const next = row.cells[backgroundEnd];
164 if (
165 !next ||
166 next.selected !== first.selected ||
167 !ghosttyColorsEqual(next.background, first.background)
168 ) {
169 break;
170 }
171 backgroundEnd += 1;
172 }
173 if (first.selected || !ghosttyColorsEqual(first.background, snapshot.background)) {
174 const left = padding + backgroundStart * metrics.width;
175 const width = (backgroundEnd - backgroundStart) * metrics.width;
176 if (!ghosttyColorsEqual(first.background, snapshot.background)) {
177 context.fillStyle = cssColor(first.background);
178 context.fillRect(left, top, width, metrics.height);
179 }
180 if (first.selected) {
181 context.fillStyle = selectionBackground;
182 context.fillRect(left, top, width, metrics.height);
183 }
184 }
185 backgroundStart = backgroundEnd;
186 }
187​
188 let runStart = 0;
189 while (runStart < row.cells.length) {
190 const first = row.cells[runStart];
191 if (!first) break;
192 if (first.text.length === 0) {
193 runStart += 1;
194 continue;
195 }
196 const runEnd = ghosttyTextRunEnd(row.cells, runStart, (cell) => sameTextStyle(cell, first));
197 const text = row.cells
198 .slice(runStart, runEnd)
199 .map((cell) => cell.text)
200 .join("");
201 if (!first.invisible && text.trim().length > 0) {
202 context.save();
203 context.beginPath();
204 context.rect(
205 padding + runStart * metrics.width,
206 top,
207 (runEnd - runStart) * metrics.width,
208 metrics.height,
209 );
210 context.clip();
211 context.font = fontForCell(first, fontSize, fontFamily);
212 context.fillStyle = cssColor(first.foreground);
213 context.fillText(
214 text,
215 padding + runStart * metrics.width,
216 top + metrics.baseline,
217 (runEnd - runStart) * metrics.width,
218 );
219 context.restore();
220 }
221 runStart = runEnd;
222 }
223​
224 for (let column = 0; column < row.cells.length; column += 1) {
225 const cell = row.cells[column];
226 const hoveredLink =
227 hoveredLinkRange !== null &&
228 rowIndex >= hoveredLinkRange.start.y &&
229 rowIndex <= hoveredLinkRange.end.y &&
230 (rowIndex > hoveredLinkRange.start.y || column >= hoveredLinkRange.start.x) &&
231 (rowIndex < hoveredLinkRange.end.y || column <= hoveredLinkRange.end.x);
232 if (!cell || (!cell.underline && !cell.strikethrough && !cell.overline && !hoveredLink)) {
233 continue;
234 }
235 context.fillStyle = cssColor(cell.foreground);
236 const left = padding + column * metrics.width;
237 if (cell.underline || hoveredLink) {
238 context.fillRect(left, top + metrics.height - 2, metrics.width, 1);
239 }
240 if (cell.strikethrough) {
241 context.fillRect(left, top + Math.floor(metrics.height * 0.55), metrics.width, 1);
242 }
243 if (cell.overline) context.fillRect(left, top + 1, metrics.width, 1);
244 }
245 }
246​
247 if (cursorOn && snapshot.cursorVisible && snapshot.cursorX >= 0 && snapshot.cursorY >= 0) {
248 const left = padding + snapshot.cursorX * metrics.width;
249 const top = originY + snapshot.cursorY * metrics.height;
250 context.fillStyle = cssColor(snapshot.cursor);
251 if (!focused) {
252 // An unfocused terminal draws a hollow cursor so the active pane is obvious.
253 context.strokeStyle = cssColor(snapshot.cursor);
254 context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
255 } else if (snapshot.cursorStyle === 0) {
256 context.fillRect(left, top, 2, metrics.height);
257 } else if (snapshot.cursorStyle === 2) {
258 context.fillRect(left, top + metrics.height - 2, metrics.width, 2);
259 } else if (snapshot.cursorStyle === 3) {
260 context.strokeStyle = cssColor(snapshot.cursor);
261 context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
262 } else {
263 context.fillRect(left, top, metrics.width, metrics.height);
264 const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
265 if (cell?.text) {
266 context.font = fontForCell(cell, fontSize, fontFamily);
267 context.fillStyle = cssColor(snapshot.background);
268 context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
269 }
270 }
271 }