]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Renderer.ts
Start isolating buffer attributes into Buffer class
[mirror_xterm.js.git] / src / Renderer.ts
CommitLineData
ca4e99e8
DI
1/**
2 * @license MIT
3 */
4
92068f36 5import { ITerminal } from './Interfaces';
23e6a475 6import { DomElementObjectPool } from './utils/DomElementObjectPool';
92068f36
DI
7
8/**
9 * The maximum number of refresh frames to skip when the write buffer is non-
10 * empty. Note that these frames may be intermingled with frames that are
11 * skipped via requestAnimationFrame's mechanism.
12 */
13const MAX_REFRESH_FRAME_SKIP = 5;
14
92068f36 15/**
20f22fb6 16 * Flags used to render terminal text properly.
92068f36 17 */
20f22fb6
DI
18enum FLAGS {
19 BOLD = 1,
20 UNDERLINE = 2,
21 BLINK = 4,
22 INVERSE = 8,
23 INVISIBLE = 16
92068f36
DI
24};
25
26let brokenBold: boolean = null;
27
28export class Renderer {
29 /** A queue of the rows to be refreshed */
30 private _refreshRowsQueue: {start: number, end: number}[] = [];
31 private _refreshFramesSkipped = 0;
32 private _refreshAnimationFrame = null;
33
23e6a475
DI
34 private _spanElementObjectPool = new DomElementObjectPool('span');
35
92068f36
DI
36 constructor(private _terminal: ITerminal) {
37 // Figure out whether boldness affects
38 // the character width of monospace fonts.
39 if (brokenBold === null) {
f0d5b401 40 brokenBold = checkBoldBroken((<any>this._terminal).element);
92068f36 41 }
23e6a475 42 this._spanElementObjectPool = new DomElementObjectPool('span');
92068f36 43
9d8f6c9a
DI
44 // TODO: Pull more DOM interactions into Renderer.constructor, element for
45 // example should be owned by Renderer (and also exposed by Terminal due to
46 // to established public API).
92068f36
DI
47 }
48
49 /**
50 * Queues a refresh between two rows (inclusive), to be done on next animation
51 * frame.
52 * @param {number} start The start row.
53 * @param {number} end The end row.
54 */
55 public queueRefresh(start: number, end: number): void {
56 this._refreshRowsQueue.push({ start: start, end: end });
57 if (!this._refreshAnimationFrame) {
58 this._refreshAnimationFrame = window.requestAnimationFrame(this._refreshLoop.bind(this));
59 }
60 }
61
62 /**
63 * Performs the refresh loop callback, calling refresh only if a refresh is
64 * necessary before queueing up the next one.
65 */
66 private _refreshLoop(): void {
67 // Skip MAX_REFRESH_FRAME_SKIP frames if the writeBuffer is non-empty as it
68 // will need to be immediately refreshed anyway. This saves a lot of
69 // rendering time as the viewport DOM does not need to be refreshed, no
70 // scroll events, no layouts, etc.
71 const skipFrame = this._terminal.writeBuffer.length > 0 && this._refreshFramesSkipped++ <= MAX_REFRESH_FRAME_SKIP;
72 if (skipFrame) {
73 this._refreshAnimationFrame = window.requestAnimationFrame(this._refreshLoop.bind(this));
74 return;
75 }
76
77 this._refreshFramesSkipped = 0;
78 let start;
79 let end;
80 if (this._refreshRowsQueue.length > 4) {
81 // Just do a full refresh when 5+ refreshes are queued
82 start = 0;
83 end = this._terminal.rows - 1;
84 } else {
85 // Get start and end rows that need refreshing
86 start = this._refreshRowsQueue[0].start;
87 end = this._refreshRowsQueue[0].end;
88 for (let i = 1; i < this._refreshRowsQueue.length; i++) {
89 if (this._refreshRowsQueue[i].start < start) {
90 start = this._refreshRowsQueue[i].start;
91 }
92 if (this._refreshRowsQueue[i].end > end) {
93 end = this._refreshRowsQueue[i].end;
94 }
95 }
96 }
97 this._refreshRowsQueue = [];
98 this._refreshAnimationFrame = null;
99 this._refresh(start, end);
100 }
101
102 /**
103 * Refreshes (re-renders) terminal content within two rows (inclusive)
104 *
105 * Rendering Engine:
106 *
107 * In the screen buffer, each character is stored as a an array with a character
108 * and a 32-bit integer:
109 * - First value: a utf-16 character.
110 * - Second value:
111 * - Next 9 bits: background color (0-511).
112 * - Next 9 bits: foreground color (0-511).
113 * - Next 14 bits: a mask for misc. flags:
114 * - 1=bold
115 * - 2=underline
116 * - 4=blink
117 * - 8=inverse
118 * - 16=invisible
119 *
120 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
121 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
122 */
123 private _refresh(start: number, end: number): void {
92068f36 124 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
9ec89745 125 let parent;
92068f36
DI
126 if (end - start >= this._terminal.rows / 2) {
127 parent = this._terminal.element.parentNode;
128 if (parent) {
129 this._terminal.element.removeChild(this._terminal.rowContainer);
130 }
131 }
132
c887c6e0
DI
133 let width = this._terminal.cols;
134 let y = start;
92068f36
DI
135
136 if (end >= this._terminal.rows) {
137 this._terminal.log('`end` is too large. Most likely a bad CSR.');
138 end = this._terminal.rows - 1;
139 }
140
141 for (; y <= end; y++) {
bbafdd3d 142 let row = y + this._terminal.buffer.ydisp;
92068f36 143
8ede1fc9 144 let line = this._terminal.buffer.lines.get(row);
92068f36 145
c887c6e0 146 let x;
bbafdd3d 147 if (this._terminal.y === y - (this._terminal.buffer.ybase - this._terminal.buffer.ydisp) &&
9ec89745
DI
148 this._terminal.cursorState &&
149 !this._terminal.cursorHidden) {
92068f36
DI
150 x = this._terminal.x;
151 } else {
152 x = -1;
153 }
154
c887c6e0 155 let attr = this._terminal.defAttr;
92068f36 156
47814847
DI
157 const documentFragment = document.createDocumentFragment();
158 let innerHTML = '';
159 let currentElement;
23e6a475
DI
160
161 // Return the row's spans to the pool
162 while (this._terminal.children[y].children.length) {
47814847 163 const child = this._terminal.children[y].children[0];
23e6a475
DI
164 this._terminal.children[y].removeChild(child);
165 this._spanElementObjectPool.release(<HTMLElement>child);
166 }
167
c887c6e0
DI
168 for (let i = 0; i < width; i++) {
169 // TODO: Could data be a more specific type?
170 let data: any = line[i][0];
47814847
DI
171 const ch = line[i][1];
172 const ch_width: any = line[i][2];
9ec89745 173 if (!ch_width) {
92068f36 174 continue;
9ec89745 175 }
92068f36 176
9ec89745
DI
177 if (i === x) {
178 data = -1;
179 }
92068f36
DI
180
181 if (data !== attr) {
182 if (attr !== this._terminal.defAttr) {
23e6a475
DI
183 if (innerHTML) {
184 currentElement.innerHTML = innerHTML;
185 innerHTML = '';
186 }
187 documentFragment.appendChild(currentElement);
188 currentElement = null;
92068f36
DI
189 }
190 if (data !== this._terminal.defAttr) {
23e6a475
DI
191 if (innerHTML && !currentElement) {
192 currentElement = this._spanElementObjectPool.acquire();
23e6a475
DI
193 }
194 if (currentElement) {
195 if (innerHTML) {
196 currentElement.innerHTML = innerHTML;
197 innerHTML = '';
198 }
199 documentFragment.appendChild(currentElement);
200 }
201 currentElement = this._spanElementObjectPool.acquire();
92068f36 202 if (data === -1) {
7e210c7e
LB
203 currentElement.classList.add('reverse-video');
204 currentElement.classList.add('terminal-cursor');
92068f36 205 } else {
c887c6e0
DI
206 let bg = data & 0x1ff;
207 let fg = (data >> 9) & 0x1ff;
208 let flags = data >> 18;
92068f36
DI
209
210 if (flags & FLAGS.BOLD) {
211 if (!brokenBold) {
23e6a475 212 currentElement.classList.add('xterm-bold');
92068f36
DI
213 }
214 // See: XTerm*boldColors
9ec89745
DI
215 if (fg < 8) {
216 fg += 8;
217 }
92068f36
DI
218 }
219
220 if (flags & FLAGS.UNDERLINE) {
23e6a475 221 currentElement.classList.add('xterm-underline');
92068f36
DI
222 }
223
224 if (flags & FLAGS.BLINK) {
23e6a475 225 currentElement.classList.add('xterm-blink');
92068f36
DI
226 }
227
228 // If inverse flag is on, then swap the foreground and background variables.
229 if (flags & FLAGS.INVERSE) {
9ec89745
DI
230 let temp = bg;
231 bg = fg;
232 fg = temp;
233 // Should inverse just be before the above boldColors effect instead?
234 if ((flags & 1) && fg < 8) {
235 fg += 8;
236 }
92068f36
DI
237 }
238
239 if (flags & FLAGS.INVISIBLE) {
23e6a475 240 currentElement.classList.add('xterm-hidden');
92068f36
DI
241 }
242
243 /**
244 * Weird situation: Invert flag used black foreground and white background results
245 * in invalid background color, positioned at the 256 index of the 256 terminal
246 * color map. Pin the colors manually in such a case.
247 *
248 * Source: https://github.com/sourcelair/xterm.js/issues/57
249 */
250 if (flags & FLAGS.INVERSE) {
aad84395 251 if (bg === 257) {
92068f36
DI
252 bg = 15;
253 }
aad84395 254 if (fg === 256) {
92068f36
DI
255 fg = 0;
256 }
257 }
258
259 if (bg < 256) {
cf863d48 260 currentElement.classList.add(`xterm-bg-color-${bg}`);
92068f36
DI
261 }
262
263 if (fg < 256) {
cf863d48 264 currentElement.classList.add(`xterm-color-${fg}`);
92068f36 265 }
92068f36
DI
266 }
267 }
268 }
269
270 if (ch_width === 2) {
cf863d48
DI
271 // Wrap wide characters so they're sized correctly. It's more difficult to release these
272 // from the object pool so just create new ones via innerHTML.
6c8949bb
DI
273 innerHTML += `<span class="xterm-wide-char">${ch}</span>`;
274 } else if (ch.charCodeAt(0) > 255) {
275 // Wrap any non-wide unicode character as some fonts size them badly
276 innerHTML += `<span class="xterm-normal-char">${ch}</span>`;
3baa6b92 277 } else {
6c8949bb
DI
278 switch (ch) {
279 case '&':
280 innerHTML += '&amp;';
281 break;
282 case '<':
283 innerHTML += '&lt;';
284 break;
285 case '>':
286 innerHTML += '&gt;';
287 break;
288 default:
289 if (ch <= ' ') {
290 innerHTML += '&nbsp;';
291 } else {
292 innerHTML += ch;
293 }
294 break;
295 }
92068f36
DI
296 }
297
298 attr = data;
299 }
300
23e6a475
DI
301 if (innerHTML && !currentElement) {
302 currentElement = this._spanElementObjectPool.acquire();
92068f36 303 }
23e6a475
DI
304 if (currentElement) {
305 if (innerHTML) {
306 currentElement.innerHTML = innerHTML;
307 innerHTML = '';
308 }
309 documentFragment.appendChild(currentElement);
310 currentElement = null;
311 }
92068f36 312
aad84395 313 this._terminal.children[y].appendChild(documentFragment);
92068f36
DI
314 }
315
316 if (parent) {
317 this._terminal.element.appendChild(this._terminal.rowContainer);
318 }
319
320 this._terminal.emit('refresh', {element: this._terminal.element, start: start, end: end});
321 };
b594407c 322
ec3bf113
DI
323 /**
324 * Refreshes the selection in the DOM.
325 * @param start The selection start.
326 * @param end The selection end.
327 */
b594407c 328 public refreshSelection(start: [number, number], end: [number, number]) {
ad3ae67e
DI
329 // Remove all selections
330 while (this._terminal.selectionContainer.children.length) {
331 this._terminal.selectionContainer.removeChild(this._terminal.selectionContainer.children[0]);
332 }
333
334 // Selection does not exist
335 if (!start || !end) {
336 return;
337 }
338
ad3ae67e 339 // Translate from buffer position to viewport position
bbafdd3d
PK
340 const viewportStartRow = start[1] - this._terminal.buffer.ydisp;
341 const viewportEndRow = end[1] - this._terminal.buffer.ydisp;
ad3ae67e
DI
342 const viewportCappedStartRow = Math.max(viewportStartRow, 0);
343 const viewportCappedEndRow = Math.min(viewportEndRow, this._terminal.rows - 1);
344
345 // No need to draw the selection
346 if (viewportCappedStartRow >= this._terminal.rows || viewportCappedEndRow < 0) {
347 return;
348 }
349
ad3ae67e
DI
350 // Create the selections
351 const documentFragment = document.createDocumentFragment();
352 // Draw first row
353 const startCol = viewportStartRow === viewportCappedStartRow ? start[0] : 0;
354 const endCol = viewportCappedStartRow === viewportCappedEndRow ? end[0] : this._terminal.cols;
355 documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow, startCol, endCol));
356 // Draw middle rows
9246d524
DI
357 const middleRowsCount = viewportCappedEndRow - viewportCappedStartRow - 1;
358 documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow + 1, 0, this._terminal.cols, middleRowsCount));
ad3ae67e
DI
359 // Draw final row
360 if (viewportCappedStartRow !== viewportCappedEndRow) {
361 // Only draw viewportEndRow if it's not the same as viewporttartRow
362 const endCol = viewportEndRow === viewportCappedEndRow ? end[0] : this._terminal.cols;
363 documentFragment.appendChild(this._createSelectionElement(viewportCappedEndRow, 0, endCol));
364 }
365 this._terminal.selectionContainer.appendChild(documentFragment);
366 }
367
71477874
DI
368 /**
369 * Creates a selection element at the specified position.
370 * @param row The row of the selection.
371 * @param colStart The start column.
372 * @param colEnd The end columns.
373 */
9246d524 374 private _createSelectionElement(row: number, colStart: number, colEnd: number, rowCount: number = 1): HTMLElement {
ad3ae67e 375 const element = document.createElement('div');
9246d524 376 element.style.height = `${rowCount * this._terminal.charMeasure.height}px`;
ad3ae67e
DI
377 element.style.top = `${row * this._terminal.charMeasure.height}px`;
378 element.style.left = `${colStart * this._terminal.charMeasure.width}px`;
379 element.style.width = `${this._terminal.charMeasure.width * (colEnd - colStart)}px`;
380 return element;
b594407c 381 }
92068f36
DI
382}
383
384
cf863d48 385// If bold is broken, we can't use it in the terminal.
f0d5b401 386function checkBoldBroken(terminal) {
387 const document = terminal.ownerDocument;
92068f36
DI
388 const el = document.createElement('span');
389 el.innerHTML = 'hello world';
f0d5b401 390 terminal.appendChild(el);
2ec756fd 391 const w1 = el.offsetWidth;
4b2ae6a7 392 const h1 = el.offsetHeight;
92068f36 393 el.style.fontWeight = 'bold';
2ec756fd 394 const w2 = el.offsetWidth;
4b2ae6a7 395 const h2 = el.offsetHeight;
f0d5b401 396 terminal.removeChild(el);
4b2ae6a7 397 return w1 !== w2 || h1 !== h2;
92068f36 398}