Debian Codegraph @

node/@xterm/xterm/src/browser/renderer/dom/WidthCache.ts

raw
1 /**
2 * Copyright (c) 2023 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { IDisposable } from 'common/Types';
7 import { FontWeight } from 'common/services/Services';
8
9
10 export const enum WidthCacheSettings {
11 /** sentinel for unset values in flat cache */
12 FLAT_UNSET = -9999,
13 /** size of flat cache, size-1 equals highest codepoint handled by flat */
14 FLAT_SIZE = 256,
15 /** char repeat for measuring */
16 REPEAT = 32
17 }
18
19
20 const enum FontVariant {
21 REGULAR = 0,
22 BOLD = 1,
23 ITALIC = 2,
24 BOLD_ITALIC = 3
25 }
26
27
28 export class WidthCache implements IDisposable {
29 // flat cache for regular variant up to CacheSettings.FLAT_SIZE
30 // NOTE: ~4x faster access than holey (serving >>80% of terminal content)
31 // It has a small memory footprint (only 1MB for full BMP caching),
32 // still the sweet spot is not reached before touching 32k different codepoints,
33 // thus we store the remaining <<20% of terminal data in a holey structure.
34 protected _flat = new Float32Array(WidthCacheSettings.FLAT_SIZE);
35
36 // holey cache for bold, italic and bold&italic for any string
37 // FIXME: can grow really big over time (~8.5 MB for full BMP caching),
38 // so a shared API across terminals is needed
39 protected _holey: Map<string, number> | undefined;
40
41 private _font = '';
42 private _fontSize = 0;
43 private _weight: FontWeight = 'normal';
44 private _weightBold: FontWeight = 'bold';
45 private _container: HTMLDivElement;
46 private _measureElements: HTMLSpanElement[] = [];
47
48 constructor(_document: Document, _helperContainer: HTMLElement) {
49 this._container = _document.createElement('div');
50 this._container.classList.add('xterm-width-cache-measure-container');
51 this._container.setAttribute('aria-hidden', 'true');
52 // SP should stack in spans
53 this._container.style.whiteSpace = 'pre';
54 // avoid undercuts in non-monospace fonts from kerning
55 this._container.style.fontKerning = 'none';
56
57 const regular = _document.createElement('span');
58 regular.classList.add('xterm-char-measure-element');
59
60 const bold = _document.createElement('span');
61 bold.classList.add('xterm-char-measure-element');
62 bold.style.fontWeight = 'bold';
63
64 const italic = _document.createElement('span');
65 italic.classList.add('xterm-char-measure-element');
66 italic.style.fontStyle = 'italic';
67
68 const boldItalic = _document.createElement('span');
69 boldItalic.classList.add('xterm-char-measure-element');
70 boldItalic.style.fontWeight = 'bold';
71 boldItalic.style.fontStyle = 'italic';
72
73 // NOTE: must be in order of FontVariant
74 this._measureElements = [regular, bold, italic, boldItalic];
75 this._container.appendChild(regular);
76 this._container.appendChild(bold);
77 this._container.appendChild(italic);
78 this._container.appendChild(boldItalic);
79
80 _helperContainer.appendChild(this._container);
81
82 this.clear();
83 }
84
85 public dispose(): void {
86 this._container.remove(); // remove elements from DOM
87 this._measureElements.length = 0; // release element refs
88 this._holey = undefined; // free cache memory via GC
89 }
90
91 /**
92 * Clear the width cache.
93 */
94 public clear(): void {
95 this._flat.fill(WidthCacheSettings.FLAT_UNSET);
96 // .clear() has some overhead, re-assign instead (>3 times faster)
97 this._holey = new Map<string, number>();
98 }
99
100 /**
101 * Set the font for measuring.
102 * Must be called for any changes on font settings.
103 * Also clears the cache.
104 */
105 public setFont(font: string, fontSize: number, weight: FontWeight, weightBold: FontWeight): void {
106 // skip if nothing changed
107 if (font === this._font
108 && fontSize === this._fontSize
109 && weight === this._weight
110 && weightBold === this._weightBold
111 ) {
112 return;
113 }
114
115 this._font = font;
116 this._fontSize = fontSize;
117 this._weight = weight;
118 this._weightBold = weightBold;
119
120 this._container.style.fontFamily = this._font;
121 this._container.style.fontSize = `${this._fontSize}px`;
122 this._measureElements[FontVariant.REGULAR].style.fontWeight = `${weight}`;
123 this._measureElements[FontVariant.BOLD].style.fontWeight = `${weightBold}`;
124 this._measureElements[FontVariant.ITALIC].style.fontWeight = `${weight}`;
125 this._measureElements[FontVariant.BOLD_ITALIC].style.fontWeight = `${weightBold}`;
126
127 this.clear();
128 }
129
130 /**
131 * Get the render width for cell content `c` with current font settings.
132 * `variant` denotes the font variant to be used.
133 */
134 public get(c: string, bold: boolean | number, italic: boolean | number): number {
135 let cp = 0;
136 if (!bold && !italic && c.length === 1 && (cp = c.charCodeAt(0)) < WidthCacheSettings.FLAT_SIZE) {
137 if (this._flat[cp] !== WidthCacheSettings.FLAT_UNSET) {
138 return this._flat[cp];
139 }
140 const width = this._measure(c, 0);
141 if (width > 0) {
142 this._flat[cp] = width;
143 }
144 return width;
145 }
146 let key = c;
147 if (bold) key += 'B';
148 if (italic) key += 'I';
149 let width = this._holey!.get(key);
150 if (width === undefined) {
151 let variant = 0;
152 if (bold) variant |= FontVariant.BOLD;
153 if (italic) variant |= FontVariant.ITALIC;
154 width = this._measure(c, variant);
155 if (width > 0) {
156 this._holey!.set(key, width);
157 }
158 }
159 return width;
160 }
161
162 protected _measure(c: string, variant: FontVariant): number {
163 const el = this._measureElements[variant];
164 el.textContent = c.repeat(WidthCacheSettings.REPEAT);
165 return el.offsetWidth / WidthCacheSettings.REPEAT;
166 }
167 }