Debian Codegraph @

node/@xterm/xterm/src/browser/services/CharSizeService.ts

raw
1 /**
2 * Copyright (c) 2016 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { IOptionsService } from 'common/services/Services';
7 import { ICharSizeService } from 'browser/services/Services';
8 import { Disposable } from 'vs/base/common/lifecycle';
9 import { Emitter } from 'vs/base/common/event';
10
11 export class CharSizeService extends Disposable implements ICharSizeService {
12 public serviceBrand: undefined;
13
14 public width: number = 0;
15 public height: number = 0;
16 private _measureStrategy: IMeasureStrategy;
17
18 public get hasValidSize(): boolean { return this.width > 0 && this.height > 0; }
19
20 private readonly _onCharSizeChange = this._register(new Emitter<void>());
21 public readonly onCharSizeChange = this._onCharSizeChange.event;
22
23 constructor(
24 document: Document,
25 parentElement: HTMLElement,
26 @IOptionsService private readonly _optionsService: IOptionsService
27 ) {
28 super();
29 try {
30 this._measureStrategy = this._register(new TextMetricsMeasureStrategy(this._optionsService));
31 } catch {
32 this._measureStrategy = this._register(new DomMeasureStrategy(document, parentElement, this._optionsService));
33 }
34 this._register(this._optionsService.onMultipleOptionChange(['fontFamily', 'fontSize'], () => this.measure()));
35 }
36
37 public measure(): void {
38 const result = this._measureStrategy.measure();
39 if (result.width !== this.width || result.height !== this.height) {
40 this.width = result.width;
41 this.height = result.height;
42 this._onCharSizeChange.fire();
43 }
44 }
45 }
46
47 interface IMeasureStrategy {
48 measure(): Readonly<IMeasureResult>;
49 }
50
51 interface IMeasureResult {
52 width: number;
53 height: number;
54 }
55
56 const enum DomMeasureStrategyConstants {
57 REPEAT = 32
58 }
59
60 abstract class BaseMeasureStategy extends Disposable implements IMeasureStrategy {
61 protected _result: IMeasureResult = { width: 0, height: 0 };
62
63 protected _validateAndSet(width: number | undefined, height: number | undefined): void {
64 // If values are 0 then the element is likely currently display:none, in which case we should
65 // retain the previous value.
66 if (width !== undefined && width > 0 && height !== undefined && height > 0) {
67 this._result.width = width;
68 this._result.height = height;
69 }
70 }
71
72 public abstract measure(): Readonly<IMeasureResult>;
73 }
74
75 class DomMeasureStrategy extends BaseMeasureStategy {
76 private _measureElement: HTMLElement;
77
78 constructor(
79 private _document: Document,
80 private _parentElement: HTMLElement,
81 private _optionsService: IOptionsService
82 ) {
83 super();
84 this._measureElement = this._document.createElement('span');
85 this._measureElement.classList.add('xterm-char-measure-element');
86 this._measureElement.textContent = 'W'.repeat(DomMeasureStrategyConstants.REPEAT);
87 this._measureElement.setAttribute('aria-hidden', 'true');
88 this._measureElement.style.whiteSpace = 'pre';
89 this._measureElement.style.fontKerning = 'none';
90 this._parentElement.appendChild(this._measureElement);
91 }
92
93 public measure(): Readonly<IMeasureResult> {
94 this._measureElement.style.fontFamily = this._optionsService.rawOptions.fontFamily;
95 this._measureElement.style.fontSize = `${this._optionsService.rawOptions.fontSize}px`;
96
97 // Note that this triggers a synchronous layout
98 this._validateAndSet(Number(this._measureElement.offsetWidth) / DomMeasureStrategyConstants.REPEAT, Number(this._measureElement.offsetHeight));
99
100 return this._result;
101 }
102 }
103
104 class TextMetricsMeasureStrategy extends BaseMeasureStategy {
105 private _canvas: OffscreenCanvas;
106 private _ctx: OffscreenCanvasRenderingContext2D;
107
108 constructor(
109 private _optionsService: IOptionsService
110 ) {
111 super();
112 // This will throw if any required API is not supported
113 this._canvas = new OffscreenCanvas(100, 100);
114 this._ctx = this._canvas.getContext('2d')!;
115 const a = this._ctx.measureText('W');
116 if (!('width' in a && 'fontBoundingBoxAscent' in a && 'fontBoundingBoxDescent' in a)) {
117 throw new Error('Required font metrics not supported');
118 }
119 }
120
121 public measure(): Readonly<IMeasureResult> {
122 this._ctx.font = `${this._optionsService.rawOptions.fontSize}px ${this._optionsService.rawOptions.fontFamily}`;
123 const metrics = this._ctx.measureText('W');
124 this._validateAndSet(metrics.width, metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent);
125 return this._result;
126 }
127 }