Debian Codegraph @

node/@xterm/addon-webgl/src/renderLayer/BaseRenderLayer.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { ReadonlyColorSet } from 'browser/Types';
7 import { acquireTextureAtlas } from '../CharAtlasCache';
8 import { IRenderDimensions } from 'browser/renderer/shared/Types';
9 import { ICoreBrowserService, IThemeService } from 'browser/services/Services';
10 import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
11 import { CellData } from 'common/buffer/CellData';
12 import { IOptionsService } from 'common/services/Services';
13 import { Terminal } from '@xterm/xterm';
14 import { IRenderLayer } from './Types';
15 import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
16 import { TEXT_BASELINE } from '../Constants';
17 import type { ITextureAtlas } from '../Types';
18
19 export abstract class BaseRenderLayer extends Disposable implements IRenderLayer {
20 private _canvas: HTMLCanvasElement;
21 protected _ctx!: CanvasRenderingContext2D;
22 private _deviceCharWidth: number = 0;
23 private _deviceCharHeight: number = 0;
24 private _deviceCellWidth: number = 0;
25 private _deviceCellHeight: number = 0;
26 private _deviceCharLeft: number = 0;
27 private _deviceCharTop: number = 0;
28
29 protected _charAtlas: ITextureAtlas | undefined;
30
31 constructor(
32 terminal: Terminal,
33 private _container: HTMLElement,
34 id: string,
35 zIndex: number,
36 private _alpha: boolean,
37 protected readonly _coreBrowserService: ICoreBrowserService,
38 protected readonly _optionsService: IOptionsService,
39 protected readonly _themeService: IThemeService
40 ) {
41 super();
42 this._canvas = this._coreBrowserService.mainDocument.createElement('canvas');
43 this._canvas.classList.add(`xterm-${id}-layer`);
44 this._canvas.style.zIndex = zIndex.toString();
45 this._initCanvas();
46 this._container.appendChild(this._canvas);
47 this._register(this._themeService.onChangeColors(e => {
48 this._refreshCharAtlas(terminal, e);
49 this.reset(terminal);
50 }));
51 this._register(toDisposable(() => {
52 this._canvas.remove();
53 }));
54 }
55
56 private _initCanvas(): void {
57 this._ctx = throwIfFalsy(this._canvas.getContext('2d', { alpha: this._alpha }));
58 // Draw the background if this is an opaque layer
59 if (!this._alpha) {
60 this._clearAll();
61 }
62 }
63
64 public handleBlur(terminal: Terminal): void {}
65 public handleFocus(terminal: Terminal): void {}
66 public handleCursorMove(terminal: Terminal): void {}
67 public handleGridChanged(terminal: Terminal, startRow: number, endRow: number): void {}
68 public handleSelectionChanged(terminal: Terminal, start: [number, number] | undefined, end: [number, number] | undefined, columnSelectMode: boolean = false): void {}
69
70 protected _setTransparency(terminal: Terminal, alpha: boolean): void {
71 // Do nothing when alpha doesn't change
72 if (alpha === this._alpha) {
73 return;
74 }
75
76 // Create new canvas and replace old one
77 const oldCanvas = this._canvas;
78 this._alpha = alpha;
79 // Cloning preserves properties
80 this._canvas = this._canvas.cloneNode() as HTMLCanvasElement;
81 this._initCanvas();
82 this._container.replaceChild(this._canvas, oldCanvas);
83
84 // Regenerate char atlas and force a full redraw
85 this._refreshCharAtlas(terminal, this._themeService.colors);
86 this.handleGridChanged(terminal, 0, terminal.rows - 1);
87 }
88
89 /**
90 * Refreshes the char atlas, aquiring a new one if necessary.
91 * @param terminal The terminal.
92 * @param colorSet The color set to use for the char atlas.
93 */
94 private _refreshCharAtlas(terminal: Terminal, colorSet: ReadonlyColorSet): void {
95 if (this._deviceCharWidth <= 0 && this._deviceCharHeight <= 0) {
96 return;
97 }
98
99 this._charAtlas = acquireTextureAtlas(terminal, this._optionsService.rawOptions, colorSet, this._deviceCellWidth, this._deviceCellHeight, this._deviceCharWidth, this._deviceCharHeight, this._coreBrowserService.dpr, 2048);
100 this._charAtlas.warmUp();
101 }
102
103 public resize(terminal: Terminal, dim: IRenderDimensions): void {
104 this._deviceCellWidth = dim.device.cell.width;
105 this._deviceCellHeight = dim.device.cell.height;
106 this._deviceCharWidth = dim.device.char.width;
107 this._deviceCharHeight = dim.device.char.height;
108 this._deviceCharLeft = dim.device.char.left;
109 this._deviceCharTop = dim.device.char.top;
110 this._canvas.width = dim.device.canvas.width;
111 this._canvas.height = dim.device.canvas.height;
112 this._canvas.style.width = `${dim.css.canvas.width}px`;
113 this._canvas.style.height = `${dim.css.canvas.height}px`;
114
115 // Draw the background if this is an opaque layer
116 if (!this._alpha) {
117 this._clearAll();
118 }
119
120 this._refreshCharAtlas(terminal, this._themeService.colors);
121 }
122
123 public abstract reset(terminal: Terminal): void;
124
125 /**
126 * Fills a 1px line (2px on HDPI) at the bottom of the cell. This uses the
127 * existing fillStyle on the context.
128 * @param x The column to fill.
129 * @param y The row to fill.
130 */
131 protected _fillBottomLineAtCells(x: number, y: number, width: number = 1): void {
132 this._ctx.fillRect(
133 x * this._deviceCellWidth,
134 (y + 1) * this._deviceCellHeight - this._coreBrowserService.dpr - 1 /* Ensure it's drawn within the cell */,
135 width * this._deviceCellWidth,
136 this._coreBrowserService.dpr);
137 }
138
139 /**
140 * Clears the entire canvas.
141 */
142 protected _clearAll(): void {
143 if (this._alpha) {
144 this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
145 } else {
146 this._ctx.fillStyle = this._themeService.colors.background.css;
147 this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
148 }
149 }
150
151 /**
152 * Clears 1+ cells completely.
153 * @param x The column to start at.
154 * @param y The row to start at.
155 * @param width The number of columns to clear.
156 * @param height The number of rows to clear.
157 */
158 protected _clearCells(x: number, y: number, width: number, height: number): void {
159 if (this._alpha) {
160 this._ctx.clearRect(
161 x * this._deviceCellWidth,
162 y * this._deviceCellHeight,
163 width * this._deviceCellWidth,
164 height * this._deviceCellHeight);
165 } else {
166 this._ctx.fillStyle = this._themeService.colors.background.css;
167 this._ctx.fillRect(
168 x * this._deviceCellWidth,
169 y * this._deviceCellHeight,
170 width * this._deviceCellWidth,
171 height * this._deviceCellHeight);
172 }
173 }
174
175 /**
176 * Draws a truecolor character at the cell. The character will be clipped to
177 * ensure that it fits with the cell, including the cell to the right if it's
178 * a wide character. This uses the existing fillStyle on the context.
179 * @param terminal The terminal.
180 * @param cell The cell data for the character to draw.
181 * @param x The column to draw at.
182 * @param y The row to draw at.
183 */
184 protected _fillCharTrueColor(terminal: Terminal, cell: CellData, x: number, y: number): void {
185 this._ctx.font = this._getFont(terminal, false, false);
186 this._ctx.textBaseline = TEXT_BASELINE;
187 this._clipCell(x, y, cell.getWidth());
188 this._ctx.fillText(
189 cell.getChars(),
190 x * this._deviceCellWidth + this._deviceCharLeft,
191 y * this._deviceCellHeight + this._deviceCharTop + this._deviceCharHeight);
192 }
193
194 /**
195 * Clips a cell to ensure no pixels will be drawn outside of it.
196 * @param x The column to clip.
197 * @param y The row to clip.
198 * @param width The number of columns to clip.
199 */
200 private _clipCell(x: number, y: number, width: number): void {
201 this._ctx.beginPath();
202 this._ctx.rect(
203 x * this._deviceCellWidth,
204 y * this._deviceCellHeight,
205 width * this._deviceCellWidth,
206 this._deviceCellHeight);
207 this._ctx.clip();
208 }
209
210 /**
211 * Gets the current font.
212 * @param terminal The terminal.
213 * @param isBold If we should use the bold fontWeight.
214 */
215 protected _getFont(terminal: Terminal, isBold: boolean, isItalic: boolean): string {
216 const fontWeight = isBold ? terminal.options.fontWeightBold : terminal.options.fontWeight;
217 const fontStyle = isItalic ? 'italic' : '';
218
219 return `${fontStyle} ${fontWeight} ${terminal.options.fontSize! * this._coreBrowserService.dpr}px ${terminal.options.fontFamily}`;
220 }
221 }
222