@xterm/xterm 5.6.0-beta.92 → 5.6.0-beta.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/xterm.js +1 -1
- package/lib/xterm.js.map +1 -1
- package/lib/xterm.mjs +13 -13
- package/lib/xterm.mjs.map +4 -4
- package/package.json +2 -2
- package/src/browser/renderer/shared/Constants.ts +0 -8
- package/src/browser/renderer/shared/Types.ts +2 -92
- package/src/browser/renderer/shared/CellColorResolver.ts +0 -236
- package/src/browser/renderer/shared/CharAtlasCache.ts +0 -97
- package/src/browser/renderer/shared/CharAtlasUtils.ts +0 -80
- package/src/browser/renderer/shared/CursorBlinkStateManager.ts +0 -146
- package/src/browser/renderer/shared/CustomGlyphs.ts +0 -693
- package/src/browser/renderer/shared/DevicePixelObserver.ts +0 -40
- package/src/browser/renderer/shared/TextureAtlas.ts +0 -1133
|
@@ -1,1133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { IColorContrastCache } from 'browser/Types';
|
|
7
|
-
import { DIM_OPACITY, TEXT_BASELINE } from 'browser/renderer/shared/Constants';
|
|
8
|
-
import { tryDrawCustomChar } from 'browser/renderer/shared/CustomGlyphs';
|
|
9
|
-
import { computeNextVariantOffset, treatGlyphAsBackgroundColor, isPowerlineGlyph, isRestrictedPowerlineGlyph, throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
|
|
10
|
-
import { IBoundingBox, ICharAtlasConfig, IRasterizedGlyph, ITextureAtlas } from 'browser/renderer/shared/Types';
|
|
11
|
-
import { NULL_COLOR, channels, color, rgba } from 'common/Color';
|
|
12
|
-
import { FourKeyMap } from 'common/MultiKeyMap';
|
|
13
|
-
import { IdleTaskQueue } from 'common/TaskQueue';
|
|
14
|
-
import { IColor } from 'common/Types';
|
|
15
|
-
import { AttributeData } from 'common/buffer/AttributeData';
|
|
16
|
-
import { Attributes, DEFAULT_COLOR, DEFAULT_EXT, UnderlineStyle } from 'common/buffer/Constants';
|
|
17
|
-
import { IUnicodeService } from 'common/services/Services';
|
|
18
|
-
import { Emitter } from 'vs/base/common/event';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A shared object which is used to draw nothing for a particular cell.
|
|
22
|
-
*/
|
|
23
|
-
const NULL_RASTERIZED_GLYPH: IRasterizedGlyph = {
|
|
24
|
-
texturePage: 0,
|
|
25
|
-
texturePosition: { x: 0, y: 0 },
|
|
26
|
-
texturePositionClipSpace: { x: 0, y: 0 },
|
|
27
|
-
offset: { x: 0, y: 0 },
|
|
28
|
-
size: { x: 0, y: 0 },
|
|
29
|
-
sizeClipSpace: { x: 0, y: 0 }
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const TMP_CANVAS_GLYPH_PADDING = 2;
|
|
33
|
-
|
|
34
|
-
const enum Constants {
|
|
35
|
-
/**
|
|
36
|
-
* The amount of pixel padding to allow in each row. Setting this to zero would make the atlas
|
|
37
|
-
* page pack as tightly as possible, but more pages would end up being created as a result.
|
|
38
|
-
*/
|
|
39
|
-
ROW_PIXEL_THRESHOLD = 2,
|
|
40
|
-
/**
|
|
41
|
-
* The maximum texture size regardless of what the actual hardware maximum turns out to be. This
|
|
42
|
-
* is enforced to ensure uploading the texture still finishes in a reasonable amount of time. A
|
|
43
|
-
* 4096 squared image takes up 16MB of GPU memory.
|
|
44
|
-
*/
|
|
45
|
-
FORCED_MAX_TEXTURE_SIZE = 4096
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface ICharAtlasActiveRow {
|
|
49
|
-
x: number;
|
|
50
|
-
y: number;
|
|
51
|
-
height: number;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Work variables to avoid garbage collection
|
|
55
|
-
let $glyph = undefined;
|
|
56
|
-
|
|
57
|
-
export class TextureAtlas implements ITextureAtlas {
|
|
58
|
-
private _didWarmUp: boolean = false;
|
|
59
|
-
|
|
60
|
-
private _cacheMap: FourKeyMap<number, number, number, number, IRasterizedGlyph> = new FourKeyMap();
|
|
61
|
-
private _cacheMapCombined: FourKeyMap<string, number, number, number, IRasterizedGlyph> = new FourKeyMap();
|
|
62
|
-
|
|
63
|
-
// The texture that the atlas is drawn to
|
|
64
|
-
private _pages: AtlasPage[] = [];
|
|
65
|
-
public get pages(): { canvas: HTMLCanvasElement, version: number }[] { return this._pages; }
|
|
66
|
-
|
|
67
|
-
// The set of atlas pages that can be written to
|
|
68
|
-
private _activePages: AtlasPage[] = [];
|
|
69
|
-
private _overflowSizePage: AtlasPage | undefined;
|
|
70
|
-
|
|
71
|
-
private _tmpCanvas: HTMLCanvasElement;
|
|
72
|
-
// A temporary context that glyphs are drawn to before being transfered to the atlas.
|
|
73
|
-
private _tmpCtx: CanvasRenderingContext2D;
|
|
74
|
-
|
|
75
|
-
private _workBoundingBox: IBoundingBox = { top: 0, left: 0, bottom: 0, right: 0 };
|
|
76
|
-
private _workAttributeData: AttributeData = new AttributeData();
|
|
77
|
-
|
|
78
|
-
private _textureSize: number = 512;
|
|
79
|
-
|
|
80
|
-
public static maxAtlasPages: number | undefined;
|
|
81
|
-
public static maxTextureSize: number | undefined;
|
|
82
|
-
|
|
83
|
-
private readonly _onAddTextureAtlasCanvas = new Emitter<HTMLCanvasElement>();
|
|
84
|
-
public readonly onAddTextureAtlasCanvas = this._onAddTextureAtlasCanvas.event;
|
|
85
|
-
private readonly _onRemoveTextureAtlasCanvas = new Emitter<HTMLCanvasElement>();
|
|
86
|
-
public readonly onRemoveTextureAtlasCanvas = this._onRemoveTextureAtlasCanvas.event;
|
|
87
|
-
|
|
88
|
-
constructor(
|
|
89
|
-
private readonly _document: Document,
|
|
90
|
-
private readonly _config: ICharAtlasConfig,
|
|
91
|
-
private readonly _unicodeService: IUnicodeService
|
|
92
|
-
) {
|
|
93
|
-
this._createNewPage();
|
|
94
|
-
this._tmpCanvas = createCanvas(
|
|
95
|
-
_document,
|
|
96
|
-
this._config.deviceCellWidth * 4 + TMP_CANVAS_GLYPH_PADDING * 2,
|
|
97
|
-
this._config.deviceCellHeight + TMP_CANVAS_GLYPH_PADDING * 2
|
|
98
|
-
);
|
|
99
|
-
this._tmpCtx = throwIfFalsy(this._tmpCanvas.getContext('2d', {
|
|
100
|
-
alpha: this._config.allowTransparency,
|
|
101
|
-
willReadFrequently: true
|
|
102
|
-
}));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public dispose(): void {
|
|
106
|
-
this._tmpCanvas.remove();
|
|
107
|
-
for (const page of this.pages) {
|
|
108
|
-
page.canvas.remove();
|
|
109
|
-
}
|
|
110
|
-
this._onAddTextureAtlasCanvas.dispose();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public warmUp(): void {
|
|
114
|
-
if (!this._didWarmUp) {
|
|
115
|
-
this._doWarmUp();
|
|
116
|
-
this._didWarmUp = true;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private _doWarmUp(): void {
|
|
121
|
-
// Pre-fill with ASCII 33-126, this is not urgent and done in idle callbacks
|
|
122
|
-
const queue = new IdleTaskQueue();
|
|
123
|
-
for (let i = 33; i < 126; i++) {
|
|
124
|
-
queue.enqueue(() => {
|
|
125
|
-
if (!this._cacheMap.get(i, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_EXT)) {
|
|
126
|
-
const rasterizedGlyph = this._drawToCache(i, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_EXT, false, undefined);
|
|
127
|
-
this._cacheMap.set(i, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_EXT, rasterizedGlyph);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private _requestClearModel = false;
|
|
134
|
-
public beginFrame(): boolean {
|
|
135
|
-
return this._requestClearModel;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
public clearTexture(): void {
|
|
139
|
-
if (this._pages[0].currentRow.x === 0 && this._pages[0].currentRow.y === 0) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
for (const page of this._pages) {
|
|
143
|
-
page.clear();
|
|
144
|
-
}
|
|
145
|
-
this._cacheMap.clear();
|
|
146
|
-
this._cacheMapCombined.clear();
|
|
147
|
-
this._didWarmUp = false;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private _createNewPage(): AtlasPage {
|
|
151
|
-
// Try merge the set of the 4 most used pages of the largest size. This is is deferred to a
|
|
152
|
-
// microtask to ensure it does not interrupt textures that will be rendered in the current
|
|
153
|
-
// animation frame which would result in blank rendered areas. This is actually not that
|
|
154
|
-
// expensive relative to drawing the glyphs, so there is no need to wait for an idle callback.
|
|
155
|
-
if (TextureAtlas.maxAtlasPages && this._pages.length >= Math.max(4, TextureAtlas.maxAtlasPages)) {
|
|
156
|
-
// Find the set of the largest 4 images, below the maximum size, with the highest
|
|
157
|
-
// percentages used
|
|
158
|
-
const pagesBySize = this._pages.filter(e => {
|
|
159
|
-
return e.canvas.width * 2 <= (TextureAtlas.maxTextureSize || Constants.FORCED_MAX_TEXTURE_SIZE);
|
|
160
|
-
}).sort((a, b) => {
|
|
161
|
-
if (b.canvas.width !== a.canvas.width) {
|
|
162
|
-
return b.canvas.width - a.canvas.width;
|
|
163
|
-
}
|
|
164
|
-
return b.percentageUsed - a.percentageUsed;
|
|
165
|
-
});
|
|
166
|
-
let sameSizeI = -1;
|
|
167
|
-
let size = 0;
|
|
168
|
-
for (let i = 0; i < pagesBySize.length; i++) {
|
|
169
|
-
if (pagesBySize[i].canvas.width !== size) {
|
|
170
|
-
sameSizeI = i;
|
|
171
|
-
size = pagesBySize[i].canvas.width;
|
|
172
|
-
} else if (i - sameSizeI === 3) {
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Gather details of the merge
|
|
178
|
-
const mergingPages = pagesBySize.slice(sameSizeI, sameSizeI + 4);
|
|
179
|
-
const sortedMergingPagesIndexes = mergingPages.map(e => e.glyphs[0].texturePage).sort((a, b) => a > b ? 1 : -1);
|
|
180
|
-
const mergedPageIndex = this.pages.length - mergingPages.length;
|
|
181
|
-
|
|
182
|
-
// Merge into the new page
|
|
183
|
-
const mergedPage = this._mergePages(mergingPages, mergedPageIndex);
|
|
184
|
-
mergedPage.version++;
|
|
185
|
-
|
|
186
|
-
// Delete the pages, shifting glyph texture pages as needed
|
|
187
|
-
for (let i = sortedMergingPagesIndexes.length - 1; i >= 0; i--) {
|
|
188
|
-
this._deletePage(sortedMergingPagesIndexes[i]);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Add the new merged page to the end
|
|
192
|
-
this.pages.push(mergedPage);
|
|
193
|
-
|
|
194
|
-
// Request the model to be cleared to refresh all texture pages.
|
|
195
|
-
this._requestClearModel = true;
|
|
196
|
-
this._onAddTextureAtlasCanvas.fire(mergedPage.canvas);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// All new atlas pages are created small as they are highly dynamic
|
|
200
|
-
const newPage = new AtlasPage(this._document, this._textureSize);
|
|
201
|
-
this._pages.push(newPage);
|
|
202
|
-
this._activePages.push(newPage);
|
|
203
|
-
this._onAddTextureAtlasCanvas.fire(newPage.canvas);
|
|
204
|
-
return newPage;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private _mergePages(mergingPages: AtlasPage[], mergedPageIndex: number): AtlasPage {
|
|
208
|
-
const mergedSize = mergingPages[0].canvas.width * 2;
|
|
209
|
-
const mergedPage = new AtlasPage(this._document, mergedSize, mergingPages);
|
|
210
|
-
for (const [i, p] of mergingPages.entries()) {
|
|
211
|
-
const xOffset = i * p.canvas.width % mergedSize;
|
|
212
|
-
const yOffset = Math.floor(i / 2) * p.canvas.height;
|
|
213
|
-
mergedPage.ctx.drawImage(p.canvas, xOffset, yOffset);
|
|
214
|
-
for (const g of p.glyphs) {
|
|
215
|
-
g.texturePage = mergedPageIndex;
|
|
216
|
-
g.sizeClipSpace.x = g.size.x / mergedSize;
|
|
217
|
-
g.sizeClipSpace.y = g.size.y / mergedSize;
|
|
218
|
-
g.texturePosition.x += xOffset;
|
|
219
|
-
g.texturePosition.y += yOffset;
|
|
220
|
-
g.texturePositionClipSpace.x = g.texturePosition.x / mergedSize;
|
|
221
|
-
g.texturePositionClipSpace.y = g.texturePosition.y / mergedSize;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
this._onRemoveTextureAtlasCanvas.fire(p.canvas);
|
|
225
|
-
|
|
226
|
-
// Remove the merging page from active pages if it was there
|
|
227
|
-
const index = this._activePages.indexOf(p);
|
|
228
|
-
if (index !== -1) {
|
|
229
|
-
this._activePages.splice(index, 1);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return mergedPage;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private _deletePage(pageIndex: number): void {
|
|
236
|
-
this._pages.splice(pageIndex, 1);
|
|
237
|
-
for (let j = pageIndex; j < this._pages.length; j++) {
|
|
238
|
-
const adjustingPage = this._pages[j];
|
|
239
|
-
for (const g of adjustingPage.glyphs) {
|
|
240
|
-
g.texturePage--;
|
|
241
|
-
}
|
|
242
|
-
adjustingPage.version++;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
public getRasterizedGlyphCombinedChar(chars: string, bg: number, fg: number, ext: number, restrictToCellHeight: boolean, domContainer: HTMLElement | undefined): IRasterizedGlyph {
|
|
247
|
-
return this._getFromCacheMap(this._cacheMapCombined, chars, bg, fg, ext, restrictToCellHeight, domContainer);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
public getRasterizedGlyph(code: number, bg: number, fg: number, ext: number, restrictToCellHeight: boolean, domContainer: HTMLElement | undefined): IRasterizedGlyph {
|
|
251
|
-
return this._getFromCacheMap(this._cacheMap, code, bg, fg, ext, restrictToCellHeight, domContainer);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Gets the glyphs texture coords, drawing the texture if it's not already
|
|
256
|
-
*/
|
|
257
|
-
private _getFromCacheMap(
|
|
258
|
-
cacheMap: FourKeyMap<string | number, number, number, number, IRasterizedGlyph>,
|
|
259
|
-
key: string | number,
|
|
260
|
-
bg: number,
|
|
261
|
-
fg: number,
|
|
262
|
-
ext: number,
|
|
263
|
-
restrictToCellHeight: boolean,
|
|
264
|
-
domContainer: HTMLElement | undefined
|
|
265
|
-
): IRasterizedGlyph {
|
|
266
|
-
$glyph = cacheMap.get(key, bg, fg, ext);
|
|
267
|
-
if (!$glyph) {
|
|
268
|
-
$glyph = this._drawToCache(key, bg, fg, ext, restrictToCellHeight, domContainer);
|
|
269
|
-
cacheMap.set(key, bg, fg, ext, $glyph);
|
|
270
|
-
}
|
|
271
|
-
return $glyph;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
private _getColorFromAnsiIndex(idx: number): IColor {
|
|
275
|
-
if (idx >= this._config.colors.ansi.length) {
|
|
276
|
-
throw new Error('No color found for idx ' + idx);
|
|
277
|
-
}
|
|
278
|
-
return this._config.colors.ansi[idx];
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private _getBackgroundColor(bgColorMode: number, bgColor: number, inverse: boolean, dim: boolean): IColor {
|
|
282
|
-
if (this._config.allowTransparency) {
|
|
283
|
-
// The background color might have some transparency, so we need to render it as fully
|
|
284
|
-
// transparent in the atlas. Otherwise we'd end up drawing the transparent background twice
|
|
285
|
-
// around the anti-aliased edges of the glyph, and it would look too dark.
|
|
286
|
-
return NULL_COLOR;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
let result: IColor;
|
|
290
|
-
switch (bgColorMode) {
|
|
291
|
-
case Attributes.CM_P16:
|
|
292
|
-
case Attributes.CM_P256:
|
|
293
|
-
result = this._getColorFromAnsiIndex(bgColor);
|
|
294
|
-
break;
|
|
295
|
-
case Attributes.CM_RGB:
|
|
296
|
-
const arr = AttributeData.toColorRGB(bgColor);
|
|
297
|
-
result = channels.toColor(arr[0], arr[1], arr[2]);
|
|
298
|
-
break;
|
|
299
|
-
case Attributes.CM_DEFAULT:
|
|
300
|
-
default:
|
|
301
|
-
if (inverse) {
|
|
302
|
-
result = color.opaque(this._config.colors.foreground);
|
|
303
|
-
} else {
|
|
304
|
-
result = this._config.colors.background;
|
|
305
|
-
}
|
|
306
|
-
break;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return result;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
private _getForegroundColor(bg: number, bgColorMode: number, bgColor: number, fg: number, fgColorMode: number, fgColor: number, inverse: boolean, dim: boolean, bold: boolean, excludeFromContrastRatioDemands: boolean): IColor {
|
|
313
|
-
const minimumContrastColor = this._getMinimumContrastColor(bg, bgColorMode, bgColor, fg, fgColorMode, fgColor, inverse, bold, dim, excludeFromContrastRatioDemands);
|
|
314
|
-
if (minimumContrastColor) {
|
|
315
|
-
return minimumContrastColor;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
let result: IColor;
|
|
319
|
-
switch (fgColorMode) {
|
|
320
|
-
case Attributes.CM_P16:
|
|
321
|
-
case Attributes.CM_P256:
|
|
322
|
-
if (this._config.drawBoldTextInBrightColors && bold && fgColor < 8) {
|
|
323
|
-
fgColor += 8;
|
|
324
|
-
}
|
|
325
|
-
result = this._getColorFromAnsiIndex(fgColor);
|
|
326
|
-
break;
|
|
327
|
-
case Attributes.CM_RGB:
|
|
328
|
-
const arr = AttributeData.toColorRGB(fgColor);
|
|
329
|
-
result = channels.toColor(arr[0], arr[1], arr[2]);
|
|
330
|
-
break;
|
|
331
|
-
case Attributes.CM_DEFAULT:
|
|
332
|
-
default:
|
|
333
|
-
if (inverse) {
|
|
334
|
-
result = this._config.colors.background;
|
|
335
|
-
} else {
|
|
336
|
-
result = this._config.colors.foreground;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// Always use an opaque color regardless of allowTransparency
|
|
341
|
-
if (this._config.allowTransparency) {
|
|
342
|
-
result = color.opaque(result);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// Apply dim to the color, opacity is fine to use for the foreground color
|
|
346
|
-
if (dim) {
|
|
347
|
-
result = color.multiplyOpacity(result, DIM_OPACITY);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return result;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
private _resolveBackgroundRgba(bgColorMode: number, bgColor: number, inverse: boolean): number {
|
|
354
|
-
switch (bgColorMode) {
|
|
355
|
-
case Attributes.CM_P16:
|
|
356
|
-
case Attributes.CM_P256:
|
|
357
|
-
return this._getColorFromAnsiIndex(bgColor).rgba;
|
|
358
|
-
case Attributes.CM_RGB:
|
|
359
|
-
return bgColor << 8;
|
|
360
|
-
case Attributes.CM_DEFAULT:
|
|
361
|
-
default:
|
|
362
|
-
if (inverse) {
|
|
363
|
-
return this._config.colors.foreground.rgba;
|
|
364
|
-
}
|
|
365
|
-
return this._config.colors.background.rgba;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
private _resolveForegroundRgba(fgColorMode: number, fgColor: number, inverse: boolean, bold: boolean): number {
|
|
370
|
-
switch (fgColorMode) {
|
|
371
|
-
case Attributes.CM_P16:
|
|
372
|
-
case Attributes.CM_P256:
|
|
373
|
-
if (this._config.drawBoldTextInBrightColors && bold && fgColor < 8) {
|
|
374
|
-
fgColor += 8;
|
|
375
|
-
}
|
|
376
|
-
return this._getColorFromAnsiIndex(fgColor).rgba;
|
|
377
|
-
case Attributes.CM_RGB:
|
|
378
|
-
return fgColor << 8;
|
|
379
|
-
case Attributes.CM_DEFAULT:
|
|
380
|
-
default:
|
|
381
|
-
if (inverse) {
|
|
382
|
-
return this._config.colors.background.rgba;
|
|
383
|
-
}
|
|
384
|
-
return this._config.colors.foreground.rgba;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
private _getMinimumContrastColor(bg: number, bgColorMode: number, bgColor: number, fg: number, fgColorMode: number, fgColor: number, inverse: boolean, bold: boolean, dim: boolean, excludeFromContrastRatioDemands: boolean): IColor | undefined {
|
|
389
|
-
if (this._config.minimumContrastRatio === 1 || excludeFromContrastRatioDemands) {
|
|
390
|
-
return undefined;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// Try get from cache first
|
|
394
|
-
const cache = this._getContrastCache(dim);
|
|
395
|
-
const adjustedColor = cache.getColor(bg, fg);
|
|
396
|
-
if (adjustedColor !== undefined) {
|
|
397
|
-
return adjustedColor || undefined;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const bgRgba = this._resolveBackgroundRgba(bgColorMode, bgColor, inverse);
|
|
401
|
-
const fgRgba = this._resolveForegroundRgba(fgColorMode, fgColor, inverse, bold);
|
|
402
|
-
// Dim cells only require half the contrast, otherwise they wouldn't be distinguishable from
|
|
403
|
-
// non-dim cells
|
|
404
|
-
const result = rgba.ensureContrastRatio(bgRgba, fgRgba, this._config.minimumContrastRatio / (dim ? 2 : 1));
|
|
405
|
-
|
|
406
|
-
if (!result) {
|
|
407
|
-
cache.setColor(bg, fg, null);
|
|
408
|
-
return undefined;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const color = channels.toColor(
|
|
412
|
-
(result >> 24) & 0xFF,
|
|
413
|
-
(result >> 16) & 0xFF,
|
|
414
|
-
(result >> 8) & 0xFF
|
|
415
|
-
);
|
|
416
|
-
cache.setColor(bg, fg, color);
|
|
417
|
-
|
|
418
|
-
return color;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
private _getContrastCache(dim: boolean): IColorContrastCache {
|
|
422
|
-
if (dim) {
|
|
423
|
-
return this._config.colors.halfContrastCache;
|
|
424
|
-
}
|
|
425
|
-
return this._config.colors.contrastCache;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
private _drawToCache(codeOrChars: number | string, bg: number, fg: number, ext: number, restrictToCellHeight: boolean, domContainer: HTMLElement | undefined): IRasterizedGlyph {
|
|
429
|
-
const chars = typeof codeOrChars === 'number' ? String.fromCharCode(codeOrChars) : codeOrChars;
|
|
430
|
-
|
|
431
|
-
// Uncomment for debugging
|
|
432
|
-
// console.log(`draw to cache "${chars}"`, bg, fg, ext);
|
|
433
|
-
|
|
434
|
-
// Attach the canvas to the DOM in order to inherit font-feature-settings
|
|
435
|
-
// from the parent elements. This is necessary for ligatures and variants to
|
|
436
|
-
// work.
|
|
437
|
-
if (domContainer && this._tmpCanvas.parentElement !== domContainer) {
|
|
438
|
-
this._tmpCanvas.style.display = 'none';
|
|
439
|
-
domContainer.append(this._tmpCanvas);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
// Allow 1 cell width per character, with a minimum of 2 (CJK), plus some padding. This is used
|
|
443
|
-
// to draw the glyph to the canvas as well as to restrict the bounding box search to ensure
|
|
444
|
-
// giant ligatures (eg. =====>) don't impact overall performance.
|
|
445
|
-
const allowedWidth = Math.min(this._config.deviceCellWidth * Math.max(chars.length, 2) + TMP_CANVAS_GLYPH_PADDING * 2, this._config.deviceMaxTextureSize);
|
|
446
|
-
if (this._tmpCanvas.width < allowedWidth) {
|
|
447
|
-
this._tmpCanvas.width = allowedWidth;
|
|
448
|
-
}
|
|
449
|
-
// Include line height when drawing glyphs
|
|
450
|
-
const allowedHeight = Math.min(this._config.deviceCellHeight + TMP_CANVAS_GLYPH_PADDING * 4, this._textureSize);
|
|
451
|
-
if (this._tmpCanvas.height < allowedHeight) {
|
|
452
|
-
this._tmpCanvas.height = allowedHeight;
|
|
453
|
-
}
|
|
454
|
-
this._tmpCtx.save();
|
|
455
|
-
|
|
456
|
-
this._workAttributeData.fg = fg;
|
|
457
|
-
this._workAttributeData.bg = bg;
|
|
458
|
-
this._workAttributeData.extended.ext = ext;
|
|
459
|
-
|
|
460
|
-
const invisible = !!this._workAttributeData.isInvisible();
|
|
461
|
-
if (invisible) {
|
|
462
|
-
return NULL_RASTERIZED_GLYPH;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
const bold = !!this._workAttributeData.isBold();
|
|
466
|
-
const inverse = !!this._workAttributeData.isInverse();
|
|
467
|
-
const dim = !!this._workAttributeData.isDim();
|
|
468
|
-
const italic = !!this._workAttributeData.isItalic();
|
|
469
|
-
const underline = !!this._workAttributeData.isUnderline();
|
|
470
|
-
const strikethrough = !!this._workAttributeData.isStrikethrough();
|
|
471
|
-
const overline = !!this._workAttributeData.isOverline();
|
|
472
|
-
let fgColor = this._workAttributeData.getFgColor();
|
|
473
|
-
let fgColorMode = this._workAttributeData.getFgColorMode();
|
|
474
|
-
let bgColor = this._workAttributeData.getBgColor();
|
|
475
|
-
let bgColorMode = this._workAttributeData.getBgColorMode();
|
|
476
|
-
if (inverse) {
|
|
477
|
-
const temp = fgColor;
|
|
478
|
-
fgColor = bgColor;
|
|
479
|
-
bgColor = temp;
|
|
480
|
-
const temp2 = fgColorMode;
|
|
481
|
-
fgColorMode = bgColorMode;
|
|
482
|
-
bgColorMode = temp2;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// draw the background
|
|
486
|
-
const backgroundColor = this._getBackgroundColor(bgColorMode, bgColor, inverse, dim);
|
|
487
|
-
// Use a 'copy' composite operation to clear any existing glyph out of _tmpCtxWithAlpha,
|
|
488
|
-
// regardless of transparency in backgroundColor
|
|
489
|
-
this._tmpCtx.globalCompositeOperation = 'copy';
|
|
490
|
-
this._tmpCtx.fillStyle = backgroundColor.css;
|
|
491
|
-
this._tmpCtx.fillRect(0, 0, this._tmpCanvas.width, this._tmpCanvas.height);
|
|
492
|
-
this._tmpCtx.globalCompositeOperation = 'source-over';
|
|
493
|
-
|
|
494
|
-
// draw the foreground/glyph
|
|
495
|
-
const fontWeight = bold ? this._config.fontWeightBold : this._config.fontWeight;
|
|
496
|
-
const fontStyle = italic ? 'italic' : '';
|
|
497
|
-
this._tmpCtx.font =
|
|
498
|
-
`${fontStyle} ${fontWeight} ${this._config.fontSize * this._config.devicePixelRatio}px ${this._config.fontFamily}`;
|
|
499
|
-
this._tmpCtx.textBaseline = TEXT_BASELINE;
|
|
500
|
-
|
|
501
|
-
const powerlineGlyph = chars.length === 1 && isPowerlineGlyph(chars.charCodeAt(0));
|
|
502
|
-
const restrictedPowerlineGlyph = chars.length === 1 && isRestrictedPowerlineGlyph(chars.charCodeAt(0));
|
|
503
|
-
const foregroundColor = this._getForegroundColor(bg, bgColorMode, bgColor, fg, fgColorMode, fgColor, inverse, dim, bold, treatGlyphAsBackgroundColor(chars.charCodeAt(0)));
|
|
504
|
-
this._tmpCtx.fillStyle = foregroundColor.css;
|
|
505
|
-
|
|
506
|
-
// For powerline glyphs left/top padding is excluded (https://github.com/microsoft/vscode/issues/120129)
|
|
507
|
-
const padding = restrictedPowerlineGlyph ? 0 : TMP_CANVAS_GLYPH_PADDING * 2;
|
|
508
|
-
|
|
509
|
-
// Draw custom characters if applicable
|
|
510
|
-
let customGlyph = false;
|
|
511
|
-
if (this._config.customGlyphs !== false) {
|
|
512
|
-
customGlyph = tryDrawCustomChar(this._tmpCtx, chars, padding, padding, this._config.deviceCellWidth, this._config.deviceCellHeight, this._config.fontSize, this._config.devicePixelRatio);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
// Whether to clear pixels based on a threshold difference between the glyph color and the
|
|
516
|
-
// background color. This should be disabled when the glyph contains multiple colors such as
|
|
517
|
-
// underline colors to prevent important colors could get cleared.
|
|
518
|
-
let enableClearThresholdCheck = !powerlineGlyph;
|
|
519
|
-
|
|
520
|
-
let chWidth: number;
|
|
521
|
-
if (typeof codeOrChars === 'number') {
|
|
522
|
-
chWidth = this._unicodeService.wcwidth(codeOrChars);
|
|
523
|
-
} else {
|
|
524
|
-
chWidth = this._unicodeService.getStringCellWidth(codeOrChars);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// Draw underline
|
|
528
|
-
if (underline) {
|
|
529
|
-
this._tmpCtx.save();
|
|
530
|
-
const lineWidth = Math.max(1, Math.floor(this._config.fontSize * this._config.devicePixelRatio / 15));
|
|
531
|
-
// When the line width is odd, draw at a 0.5 position
|
|
532
|
-
const yOffset = lineWidth % 2 === 1 ? 0.5 : 0;
|
|
533
|
-
this._tmpCtx.lineWidth = lineWidth;
|
|
534
|
-
|
|
535
|
-
// Underline color
|
|
536
|
-
if (this._workAttributeData.isUnderlineColorDefault()) {
|
|
537
|
-
this._tmpCtx.strokeStyle = this._tmpCtx.fillStyle;
|
|
538
|
-
} else if (this._workAttributeData.isUnderlineColorRGB()) {
|
|
539
|
-
enableClearThresholdCheck = false;
|
|
540
|
-
this._tmpCtx.strokeStyle = `rgb(${AttributeData.toColorRGB(this._workAttributeData.getUnderlineColor()).join(',')})`;
|
|
541
|
-
} else {
|
|
542
|
-
enableClearThresholdCheck = false;
|
|
543
|
-
let fg = this._workAttributeData.getUnderlineColor();
|
|
544
|
-
if (this._config.drawBoldTextInBrightColors && this._workAttributeData.isBold() && fg < 8) {
|
|
545
|
-
fg += 8;
|
|
546
|
-
}
|
|
547
|
-
this._tmpCtx.strokeStyle = this._getColorFromAnsiIndex(fg).css;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
// Underline style/stroke
|
|
551
|
-
this._tmpCtx.beginPath();
|
|
552
|
-
const xLeft = padding;
|
|
553
|
-
const yTop = Math.ceil(padding + this._config.deviceCharHeight) - yOffset - (restrictToCellHeight ? lineWidth * 2 : 0);
|
|
554
|
-
const yMid = yTop + lineWidth;
|
|
555
|
-
const yBot = yTop + lineWidth * 2;
|
|
556
|
-
let nextOffset = this._workAttributeData.getUnderlineVariantOffset();
|
|
557
|
-
|
|
558
|
-
for (let i = 0; i < chWidth; i++) {
|
|
559
|
-
this._tmpCtx.save();
|
|
560
|
-
const xChLeft = xLeft + i * this._config.deviceCellWidth;
|
|
561
|
-
const xChRight = xLeft + (i + 1) * this._config.deviceCellWidth;
|
|
562
|
-
const xChMid = xChLeft + this._config.deviceCellWidth / 2;
|
|
563
|
-
switch (this._workAttributeData.extended.underlineStyle) {
|
|
564
|
-
case UnderlineStyle.DOUBLE:
|
|
565
|
-
this._tmpCtx.moveTo(xChLeft, yTop);
|
|
566
|
-
this._tmpCtx.lineTo(xChRight, yTop);
|
|
567
|
-
this._tmpCtx.moveTo(xChLeft, yBot);
|
|
568
|
-
this._tmpCtx.lineTo(xChRight, yBot);
|
|
569
|
-
break;
|
|
570
|
-
case UnderlineStyle.CURLY:
|
|
571
|
-
// Choose the bezier top and bottom based on the device pixel ratio, the curly line is
|
|
572
|
-
// made taller when the line width is as otherwise it's not very clear otherwise.
|
|
573
|
-
const yCurlyBot = lineWidth <= 1 ? yBot : Math.ceil(padding + this._config.deviceCharHeight - lineWidth / 2) - yOffset;
|
|
574
|
-
const yCurlyTop = lineWidth <= 1 ? yTop : Math.ceil(padding + this._config.deviceCharHeight + lineWidth / 2) - yOffset;
|
|
575
|
-
// Clip the left and right edges of the underline such that it can be drawn just outside
|
|
576
|
-
// the edge of the cell to ensure a continuous stroke when there are multiple underlined
|
|
577
|
-
// glyphs adjacent to one another.
|
|
578
|
-
const clipRegion = new Path2D();
|
|
579
|
-
clipRegion.rect(xChLeft, yTop, this._config.deviceCellWidth, yBot - yTop);
|
|
580
|
-
this._tmpCtx.clip(clipRegion);
|
|
581
|
-
// Start 1/2 cell before and end 1/2 cells after to ensure a smooth curve with other
|
|
582
|
-
// cells
|
|
583
|
-
this._tmpCtx.moveTo(xChLeft - this._config.deviceCellWidth / 2, yMid);
|
|
584
|
-
this._tmpCtx.bezierCurveTo(
|
|
585
|
-
xChLeft - this._config.deviceCellWidth / 2, yCurlyTop,
|
|
586
|
-
xChLeft, yCurlyTop,
|
|
587
|
-
xChLeft, yMid
|
|
588
|
-
);
|
|
589
|
-
this._tmpCtx.bezierCurveTo(
|
|
590
|
-
xChLeft, yCurlyBot,
|
|
591
|
-
xChMid, yCurlyBot,
|
|
592
|
-
xChMid, yMid
|
|
593
|
-
);
|
|
594
|
-
this._tmpCtx.bezierCurveTo(
|
|
595
|
-
xChMid, yCurlyTop,
|
|
596
|
-
xChRight, yCurlyTop,
|
|
597
|
-
xChRight, yMid
|
|
598
|
-
);
|
|
599
|
-
this._tmpCtx.bezierCurveTo(
|
|
600
|
-
xChRight, yCurlyBot,
|
|
601
|
-
xChRight + this._config.deviceCellWidth / 2, yCurlyBot,
|
|
602
|
-
xChRight + this._config.deviceCellWidth / 2, yMid
|
|
603
|
-
);
|
|
604
|
-
break;
|
|
605
|
-
case UnderlineStyle.DOTTED:
|
|
606
|
-
const offsetWidth = nextOffset === 0 ? 0 :
|
|
607
|
-
(nextOffset >= lineWidth ? lineWidth * 2 - nextOffset : lineWidth - nextOffset);
|
|
608
|
-
// a line and a gap.
|
|
609
|
-
const isLineStart = nextOffset >= lineWidth ? false : true;
|
|
610
|
-
if (isLineStart === false || offsetWidth === 0) {
|
|
611
|
-
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
|
|
612
|
-
this._tmpCtx.moveTo(xChLeft + offsetWidth, yTop);
|
|
613
|
-
this._tmpCtx.lineTo(xChRight, yTop);
|
|
614
|
-
} else {
|
|
615
|
-
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
|
|
616
|
-
this._tmpCtx.moveTo(xChLeft, yTop);
|
|
617
|
-
this._tmpCtx.lineTo(xChLeft + offsetWidth, yTop);
|
|
618
|
-
this._tmpCtx.moveTo(xChLeft + offsetWidth + lineWidth, yTop);
|
|
619
|
-
this._tmpCtx.lineTo(xChRight, yTop);
|
|
620
|
-
}
|
|
621
|
-
nextOffset = computeNextVariantOffset(xChRight - xChLeft, lineWidth, nextOffset);
|
|
622
|
-
break;
|
|
623
|
-
case UnderlineStyle.DASHED:
|
|
624
|
-
const lineRatio = 0.6;
|
|
625
|
-
const gapRatio = 0.3;
|
|
626
|
-
// End line ratio is approximately equal to 0.1
|
|
627
|
-
const xChWidth = xChRight - xChLeft;
|
|
628
|
-
const line = Math.floor(lineRatio * xChWidth);
|
|
629
|
-
const gap = Math.floor(gapRatio * xChWidth);
|
|
630
|
-
const end = xChWidth - line - gap;
|
|
631
|
-
this._tmpCtx.setLineDash([line, gap, end]);
|
|
632
|
-
this._tmpCtx.moveTo(xChLeft, yTop);
|
|
633
|
-
this._tmpCtx.lineTo(xChRight, yTop);
|
|
634
|
-
break;
|
|
635
|
-
case UnderlineStyle.SINGLE:
|
|
636
|
-
default:
|
|
637
|
-
this._tmpCtx.moveTo(xChLeft, yTop);
|
|
638
|
-
this._tmpCtx.lineTo(xChRight, yTop);
|
|
639
|
-
break;
|
|
640
|
-
}
|
|
641
|
-
this._tmpCtx.stroke();
|
|
642
|
-
this._tmpCtx.restore();
|
|
643
|
-
}
|
|
644
|
-
this._tmpCtx.restore();
|
|
645
|
-
|
|
646
|
-
// Draw stroke in the background color for non custom characters in order to give an outline
|
|
647
|
-
// between the text and the underline. Only do this when font size is >= 12 as the underline
|
|
648
|
-
// looks odd when the font size is too small
|
|
649
|
-
if (!customGlyph && this._config.fontSize >= 12) {
|
|
650
|
-
// This only works when transparency is disabled because it's not clear how to clear stroked
|
|
651
|
-
// text
|
|
652
|
-
if (!this._config.allowTransparency && chars !== ' ') {
|
|
653
|
-
// Measure the text, only draw the stroke if there is a descent beyond an alphabetic text
|
|
654
|
-
// baseline
|
|
655
|
-
this._tmpCtx.save();
|
|
656
|
-
this._tmpCtx.textBaseline = 'alphabetic';
|
|
657
|
-
const metrics = this._tmpCtx.measureText(chars);
|
|
658
|
-
this._tmpCtx.restore();
|
|
659
|
-
if ('actualBoundingBoxDescent' in metrics && metrics.actualBoundingBoxDescent > 0) {
|
|
660
|
-
// This translates to 1/2 the line width in either direction
|
|
661
|
-
this._tmpCtx.save();
|
|
662
|
-
// Clip the region to only draw in valid pixels near the underline to avoid a slight
|
|
663
|
-
// outline around the whole glyph, as well as additional pixels in the glyph at the top
|
|
664
|
-
// which would increase GPU memory demands
|
|
665
|
-
const clipRegion = new Path2D();
|
|
666
|
-
clipRegion.rect(xLeft, yTop - Math.ceil(lineWidth / 2), this._config.deviceCellWidth * chWidth, yBot - yTop + Math.ceil(lineWidth / 2));
|
|
667
|
-
this._tmpCtx.clip(clipRegion);
|
|
668
|
-
this._tmpCtx.lineWidth = this._config.devicePixelRatio * 3;
|
|
669
|
-
this._tmpCtx.strokeStyle = backgroundColor.css;
|
|
670
|
-
this._tmpCtx.strokeText(chars, padding, padding + this._config.deviceCharHeight);
|
|
671
|
-
this._tmpCtx.restore();
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
// Overline
|
|
678
|
-
if (overline) {
|
|
679
|
-
const lineWidth = Math.max(1, Math.floor(this._config.fontSize * this._config.devicePixelRatio / 15));
|
|
680
|
-
const yOffset = lineWidth % 2 === 1 ? 0.5 : 0;
|
|
681
|
-
this._tmpCtx.lineWidth = lineWidth;
|
|
682
|
-
this._tmpCtx.strokeStyle = this._tmpCtx.fillStyle;
|
|
683
|
-
this._tmpCtx.beginPath();
|
|
684
|
-
this._tmpCtx.moveTo(padding, padding + yOffset);
|
|
685
|
-
this._tmpCtx.lineTo(padding + this._config.deviceCharWidth * chWidth, padding + yOffset);
|
|
686
|
-
this._tmpCtx.stroke();
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
// Draw the character
|
|
690
|
-
if (!customGlyph) {
|
|
691
|
-
this._tmpCtx.fillText(chars, padding, padding + this._config.deviceCharHeight);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
// If this character is underscore and beyond the cell bounds, shift it up until it is visible
|
|
695
|
-
// even on the bottom row, try for a maximum of 5 pixels.
|
|
696
|
-
if (chars === '_' && !this._config.allowTransparency) {
|
|
697
|
-
let isBeyondCellBounds = clearColor(this._tmpCtx.getImageData(padding, padding, this._config.deviceCellWidth, this._config.deviceCellHeight), backgroundColor, foregroundColor, enableClearThresholdCheck);
|
|
698
|
-
if (isBeyondCellBounds) {
|
|
699
|
-
for (let offset = 1; offset <= 5; offset++) {
|
|
700
|
-
this._tmpCtx.save();
|
|
701
|
-
this._tmpCtx.fillStyle = backgroundColor.css;
|
|
702
|
-
this._tmpCtx.fillRect(0, 0, this._tmpCanvas.width, this._tmpCanvas.height);
|
|
703
|
-
this._tmpCtx.restore();
|
|
704
|
-
this._tmpCtx.fillText(chars, padding, padding + this._config.deviceCharHeight - offset);
|
|
705
|
-
isBeyondCellBounds = clearColor(this._tmpCtx.getImageData(padding, padding, this._config.deviceCellWidth, this._config.deviceCellHeight), backgroundColor, foregroundColor, enableClearThresholdCheck);
|
|
706
|
-
if (!isBeyondCellBounds) {
|
|
707
|
-
break;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
// Draw strokethrough
|
|
714
|
-
if (strikethrough) {
|
|
715
|
-
const lineWidth = Math.max(1, Math.floor(this._config.fontSize * this._config.devicePixelRatio / 10));
|
|
716
|
-
const yOffset = this._tmpCtx.lineWidth % 2 === 1 ? 0.5 : 0; // When the width is odd, draw at 0.5 position
|
|
717
|
-
this._tmpCtx.lineWidth = lineWidth;
|
|
718
|
-
this._tmpCtx.strokeStyle = this._tmpCtx.fillStyle;
|
|
719
|
-
this._tmpCtx.beginPath();
|
|
720
|
-
this._tmpCtx.moveTo(padding, padding + Math.floor(this._config.deviceCharHeight / 2) - yOffset);
|
|
721
|
-
this._tmpCtx.lineTo(padding + this._config.deviceCharWidth * chWidth, padding + Math.floor(this._config.deviceCharHeight / 2) - yOffset);
|
|
722
|
-
this._tmpCtx.stroke();
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
this._tmpCtx.restore();
|
|
726
|
-
|
|
727
|
-
// clear the background from the character to avoid issues with drawing over the previous
|
|
728
|
-
// character if it extends past it's bounds
|
|
729
|
-
const imageData = this._tmpCtx.getImageData(
|
|
730
|
-
0, 0, this._tmpCanvas.width, this._tmpCanvas.height
|
|
731
|
-
);
|
|
732
|
-
|
|
733
|
-
// Clear out the background color and determine if the glyph is empty.
|
|
734
|
-
let isEmpty: boolean;
|
|
735
|
-
if (!this._config.allowTransparency) {
|
|
736
|
-
isEmpty = clearColor(imageData, backgroundColor, foregroundColor, enableClearThresholdCheck);
|
|
737
|
-
} else {
|
|
738
|
-
isEmpty = checkCompletelyTransparent(imageData);
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
// Handle empty glyphs
|
|
742
|
-
if (isEmpty) {
|
|
743
|
-
return NULL_RASTERIZED_GLYPH;
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
const rasterizedGlyph = this._findGlyphBoundingBox(imageData, this._workBoundingBox, allowedWidth, restrictedPowerlineGlyph, customGlyph, padding);
|
|
747
|
-
|
|
748
|
-
// Find the best atlas row to use
|
|
749
|
-
let activePage: AtlasPage;
|
|
750
|
-
let activeRow: ICharAtlasActiveRow;
|
|
751
|
-
while (true) {
|
|
752
|
-
// If there are no active pages (the last smallest 4 were merged), create a new one
|
|
753
|
-
if (this._activePages.length === 0) {
|
|
754
|
-
const newPage = this._createNewPage();
|
|
755
|
-
activePage = newPage;
|
|
756
|
-
activeRow = newPage.currentRow;
|
|
757
|
-
activeRow.height = rasterizedGlyph.size.y;
|
|
758
|
-
break;
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// Get the best current row from all active pages
|
|
762
|
-
activePage = this._activePages[this._activePages.length - 1];
|
|
763
|
-
activeRow = activePage.currentRow;
|
|
764
|
-
for (const p of this._activePages) {
|
|
765
|
-
if (rasterizedGlyph.size.y <= p.currentRow.height) {
|
|
766
|
-
activePage = p;
|
|
767
|
-
activeRow = p.currentRow;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
// TODO: This algorithm could be simplified:
|
|
772
|
-
// - Search for the page with ROW_PIXEL_THRESHOLD in mind
|
|
773
|
-
// - Keep track of current/fixed rows in a Map
|
|
774
|
-
|
|
775
|
-
// Replace the best current row with a fixed row if there is one at least as good as the
|
|
776
|
-
// current row. Search in reverse to prioritize filling in older pages.
|
|
777
|
-
for (let i = this._activePages.length - 1; i >= 0; i--) {
|
|
778
|
-
for (const row of this._activePages[i].fixedRows) {
|
|
779
|
-
if (row.height <= activeRow.height && rasterizedGlyph.size.y <= row.height) {
|
|
780
|
-
activePage = this._activePages[i];
|
|
781
|
-
activeRow = row;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
// Create a new page for oversized glyphs as they come up
|
|
787
|
-
if (rasterizedGlyph.size.x > this._textureSize) {
|
|
788
|
-
if (!this._overflowSizePage) {
|
|
789
|
-
this._overflowSizePage = new AtlasPage(this._document, this._config.deviceMaxTextureSize);
|
|
790
|
-
this.pages.push(this._overflowSizePage);
|
|
791
|
-
|
|
792
|
-
// Request the model to be cleared to refresh all texture pages.
|
|
793
|
-
this._requestClearModel = true;
|
|
794
|
-
this._onAddTextureAtlasCanvas.fire(this._overflowSizePage.canvas);
|
|
795
|
-
}
|
|
796
|
-
activePage = this._overflowSizePage;
|
|
797
|
-
activeRow = this._overflowSizePage.currentRow;
|
|
798
|
-
// Move to next row if necessary
|
|
799
|
-
if (activeRow.x + rasterizedGlyph.size.x >= activePage.canvas.width) {
|
|
800
|
-
activeRow.x = 0;
|
|
801
|
-
activeRow.y += activeRow.height;
|
|
802
|
-
activeRow.height = 0;
|
|
803
|
-
}
|
|
804
|
-
break;
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
// Create a new page if too much vertical space would be wasted or there is not enough room
|
|
808
|
-
// left in the page. The previous active row will become fixed in the process as it now has a
|
|
809
|
-
// fixed height
|
|
810
|
-
if (activeRow.y + rasterizedGlyph.size.y >= activePage.canvas.height || activeRow.height > rasterizedGlyph.size.y + Constants.ROW_PIXEL_THRESHOLD) {
|
|
811
|
-
// Create the new fixed height row, creating a new page if there isn't enough room on the
|
|
812
|
-
// current page
|
|
813
|
-
let wasPageAndRowFound = false;
|
|
814
|
-
if (activePage.currentRow.y + activePage.currentRow.height + rasterizedGlyph.size.y >= activePage.canvas.height) {
|
|
815
|
-
// Find the first page with room to create the new row on
|
|
816
|
-
let candidatePage: AtlasPage | undefined;
|
|
817
|
-
|
|
818
|
-
for (const p of this._activePages) {
|
|
819
|
-
if (p.currentRow.y + p.currentRow.height + rasterizedGlyph.size.y < p.canvas.height) {
|
|
820
|
-
candidatePage = p;
|
|
821
|
-
break;
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
if (candidatePage) {
|
|
825
|
-
activePage = candidatePage;
|
|
826
|
-
} else {
|
|
827
|
-
// Before creating a new atlas page that would trigger a page merge, check if the
|
|
828
|
-
// current active row is sufficient when ignoring the ROW_PIXEL_THRESHOLD. This will
|
|
829
|
-
// improve texture utilization by using the available space before the page is merged
|
|
830
|
-
// and becomes static.
|
|
831
|
-
if (
|
|
832
|
-
TextureAtlas.maxAtlasPages &&
|
|
833
|
-
this._pages.length >= TextureAtlas.maxAtlasPages &&
|
|
834
|
-
activeRow.y + rasterizedGlyph.size.y <= activePage.canvas.height &&
|
|
835
|
-
activeRow.height >= rasterizedGlyph.size.y &&
|
|
836
|
-
activeRow.x + rasterizedGlyph.size.x <= activePage.canvas.width
|
|
837
|
-
) {
|
|
838
|
-
// activePage and activeRow is already valid
|
|
839
|
-
wasPageAndRowFound = true;
|
|
840
|
-
} else {
|
|
841
|
-
// Create a new page if there is no room
|
|
842
|
-
const newPage = this._createNewPage();
|
|
843
|
-
activePage = newPage;
|
|
844
|
-
activeRow = newPage.currentRow;
|
|
845
|
-
activeRow.height = rasterizedGlyph.size.y;
|
|
846
|
-
wasPageAndRowFound = true;
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
if (!wasPageAndRowFound) {
|
|
851
|
-
// Fix the current row as the new row is being added below
|
|
852
|
-
if (activePage.currentRow.height > 0) {
|
|
853
|
-
activePage.fixedRows.push(activePage.currentRow);
|
|
854
|
-
}
|
|
855
|
-
activeRow = {
|
|
856
|
-
x: 0,
|
|
857
|
-
y: activePage.currentRow.y + activePage.currentRow.height,
|
|
858
|
-
height: rasterizedGlyph.size.y
|
|
859
|
-
};
|
|
860
|
-
activePage.fixedRows.push(activeRow);
|
|
861
|
-
|
|
862
|
-
// Create the new current row below the new fixed height row
|
|
863
|
-
activePage.currentRow = {
|
|
864
|
-
x: 0,
|
|
865
|
-
y: activeRow.y + activeRow.height,
|
|
866
|
-
height: 0
|
|
867
|
-
};
|
|
868
|
-
}
|
|
869
|
-
// TODO: Remove pages from _activePages when all rows are filled
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// Exit the loop if there is enough room in the row
|
|
873
|
-
if (activeRow.x + rasterizedGlyph.size.x <= activePage.canvas.width) {
|
|
874
|
-
break;
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
// If there is not enough room in the current row, finish it and try again
|
|
878
|
-
if (activeRow === activePage.currentRow) {
|
|
879
|
-
activeRow.x = 0;
|
|
880
|
-
activeRow.y += activeRow.height;
|
|
881
|
-
activeRow.height = 0;
|
|
882
|
-
} else {
|
|
883
|
-
activePage.fixedRows.splice(activePage.fixedRows.indexOf(activeRow), 1);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
// Record texture position
|
|
888
|
-
rasterizedGlyph.texturePage = this._pages.indexOf(activePage);
|
|
889
|
-
rasterizedGlyph.texturePosition.x = activeRow.x;
|
|
890
|
-
rasterizedGlyph.texturePosition.y = activeRow.y;
|
|
891
|
-
rasterizedGlyph.texturePositionClipSpace.x = activeRow.x / activePage.canvas.width;
|
|
892
|
-
rasterizedGlyph.texturePositionClipSpace.y = activeRow.y / activePage.canvas.height;
|
|
893
|
-
|
|
894
|
-
// Fix the clipspace position as pages may be of differing size
|
|
895
|
-
rasterizedGlyph.sizeClipSpace.x /= activePage.canvas.width;
|
|
896
|
-
rasterizedGlyph.sizeClipSpace.y /= activePage.canvas.height;
|
|
897
|
-
|
|
898
|
-
// Update atlas current row, for fixed rows the glyph height will never be larger than the row
|
|
899
|
-
// height
|
|
900
|
-
activeRow.height = Math.max(activeRow.height, rasterizedGlyph.size.y);
|
|
901
|
-
activeRow.x += rasterizedGlyph.size.x;
|
|
902
|
-
|
|
903
|
-
// putImageData doesn't do any blending, so it will overwrite any existing cache entry for us
|
|
904
|
-
activePage.ctx.putImageData(
|
|
905
|
-
imageData,
|
|
906
|
-
rasterizedGlyph.texturePosition.x - this._workBoundingBox.left,
|
|
907
|
-
rasterizedGlyph.texturePosition.y - this._workBoundingBox.top,
|
|
908
|
-
this._workBoundingBox.left,
|
|
909
|
-
this._workBoundingBox.top,
|
|
910
|
-
rasterizedGlyph.size.x,
|
|
911
|
-
rasterizedGlyph.size.y
|
|
912
|
-
);
|
|
913
|
-
activePage.addGlyph(rasterizedGlyph);
|
|
914
|
-
activePage.version++;
|
|
915
|
-
|
|
916
|
-
return rasterizedGlyph;
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
/**
|
|
920
|
-
* Given an ImageData object, find the bounding box of the non-transparent
|
|
921
|
-
* portion of the texture and return an IRasterizedGlyph with these
|
|
922
|
-
* dimensions.
|
|
923
|
-
* @param imageData The image data to read.
|
|
924
|
-
* @param boundingBox An IBoundingBox to put the clipped bounding box values.
|
|
925
|
-
*/
|
|
926
|
-
private _findGlyphBoundingBox(imageData: ImageData, boundingBox: IBoundingBox, allowedWidth: number, restrictedGlyph: boolean, customGlyph: boolean, padding: number): IRasterizedGlyph {
|
|
927
|
-
boundingBox.top = 0;
|
|
928
|
-
const height = restrictedGlyph ? this._config.deviceCellHeight : this._tmpCanvas.height;
|
|
929
|
-
const width = restrictedGlyph ? this._config.deviceCellWidth : allowedWidth;
|
|
930
|
-
let found = false;
|
|
931
|
-
for (let y = 0; y < height; y++) {
|
|
932
|
-
for (let x = 0; x < width; x++) {
|
|
933
|
-
const alphaOffset = y * this._tmpCanvas.width * 4 + x * 4 + 3;
|
|
934
|
-
if (imageData.data[alphaOffset] !== 0) {
|
|
935
|
-
boundingBox.top = y;
|
|
936
|
-
found = true;
|
|
937
|
-
break;
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
if (found) {
|
|
941
|
-
break;
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
boundingBox.left = 0;
|
|
945
|
-
found = false;
|
|
946
|
-
for (let x = 0; x < padding + width; x++) {
|
|
947
|
-
for (let y = 0; y < height; y++) {
|
|
948
|
-
const alphaOffset = y * this._tmpCanvas.width * 4 + x * 4 + 3;
|
|
949
|
-
if (imageData.data[alphaOffset] !== 0) {
|
|
950
|
-
boundingBox.left = x;
|
|
951
|
-
found = true;
|
|
952
|
-
break;
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
if (found) {
|
|
956
|
-
break;
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
boundingBox.right = width;
|
|
960
|
-
found = false;
|
|
961
|
-
for (let x = padding + width - 1; x >= padding; x--) {
|
|
962
|
-
for (let y = 0; y < height; y++) {
|
|
963
|
-
const alphaOffset = y * this._tmpCanvas.width * 4 + x * 4 + 3;
|
|
964
|
-
if (imageData.data[alphaOffset] !== 0) {
|
|
965
|
-
boundingBox.right = x;
|
|
966
|
-
found = true;
|
|
967
|
-
break;
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
if (found) {
|
|
971
|
-
break;
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
boundingBox.bottom = height;
|
|
975
|
-
found = false;
|
|
976
|
-
for (let y = height - 1; y >= 0; y--) {
|
|
977
|
-
for (let x = 0; x < width; x++) {
|
|
978
|
-
const alphaOffset = y * this._tmpCanvas.width * 4 + x * 4 + 3;
|
|
979
|
-
if (imageData.data[alphaOffset] !== 0) {
|
|
980
|
-
boundingBox.bottom = y;
|
|
981
|
-
found = true;
|
|
982
|
-
break;
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
if (found) {
|
|
986
|
-
break;
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
return {
|
|
990
|
-
texturePage: 0,
|
|
991
|
-
texturePosition: { x: 0, y: 0 },
|
|
992
|
-
texturePositionClipSpace: { x: 0, y: 0 },
|
|
993
|
-
size: {
|
|
994
|
-
x: boundingBox.right - boundingBox.left + 1,
|
|
995
|
-
y: boundingBox.bottom - boundingBox.top + 1
|
|
996
|
-
},
|
|
997
|
-
sizeClipSpace: {
|
|
998
|
-
x: (boundingBox.right - boundingBox.left + 1),
|
|
999
|
-
y: (boundingBox.bottom - boundingBox.top + 1)
|
|
1000
|
-
},
|
|
1001
|
-
offset: {
|
|
1002
|
-
x: -boundingBox.left + padding + ((restrictedGlyph || customGlyph) ? Math.floor((this._config.deviceCellWidth - this._config.deviceCharWidth) / 2) : 0),
|
|
1003
|
-
y: -boundingBox.top + padding + ((restrictedGlyph || customGlyph) ? this._config.lineHeight === 1 ? 0 : Math.round((this._config.deviceCellHeight - this._config.deviceCharHeight) / 2) : 0)
|
|
1004
|
-
}
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
class AtlasPage {
|
|
1010
|
-
public readonly canvas: HTMLCanvasElement;
|
|
1011
|
-
public readonly ctx: CanvasRenderingContext2D;
|
|
1012
|
-
|
|
1013
|
-
private _usedPixels: number = 0;
|
|
1014
|
-
public get percentageUsed(): number { return this._usedPixels / (this.canvas.width * this.canvas.height); }
|
|
1015
|
-
|
|
1016
|
-
private readonly _glyphs: IRasterizedGlyph[] = [];
|
|
1017
|
-
public get glyphs(): ReadonlyArray<IRasterizedGlyph> { return this._glyphs; }
|
|
1018
|
-
public addGlyph(glyph: IRasterizedGlyph): void {
|
|
1019
|
-
this._glyphs.push(glyph);
|
|
1020
|
-
this._usedPixels += glyph.size.x * glyph.size.y;
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
/**
|
|
1024
|
-
* Used to check whether the canvas of the atlas page has changed.
|
|
1025
|
-
*/
|
|
1026
|
-
public version = 0;
|
|
1027
|
-
|
|
1028
|
-
// Texture atlas current positioning data. The texture packing strategy used is to fill from
|
|
1029
|
-
// left-to-right and top-to-bottom. When the glyph being written is less than half of the current
|
|
1030
|
-
// row's height, the following happens:
|
|
1031
|
-
//
|
|
1032
|
-
// - The current row becomes the fixed height row A
|
|
1033
|
-
// - A new fixed height row B the exact size of the glyph is created below the current row
|
|
1034
|
-
// - A new dynamic height current row is created below B
|
|
1035
|
-
//
|
|
1036
|
-
// This strategy does a good job preventing space being wasted for very short glyphs such as
|
|
1037
|
-
// underscores, hyphens etc. or those with underlines rendered.
|
|
1038
|
-
public currentRow: ICharAtlasActiveRow = {
|
|
1039
|
-
x: 0,
|
|
1040
|
-
y: 0,
|
|
1041
|
-
height: 0
|
|
1042
|
-
};
|
|
1043
|
-
public readonly fixedRows: ICharAtlasActiveRow[] = [];
|
|
1044
|
-
|
|
1045
|
-
constructor(
|
|
1046
|
-
document: Document,
|
|
1047
|
-
size: number,
|
|
1048
|
-
sourcePages?: AtlasPage[]
|
|
1049
|
-
) {
|
|
1050
|
-
if (sourcePages) {
|
|
1051
|
-
for (const p of sourcePages) {
|
|
1052
|
-
this._glyphs.push(...p.glyphs);
|
|
1053
|
-
this._usedPixels += p._usedPixels;
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
this.canvas = createCanvas(document, size, size);
|
|
1057
|
-
// The canvas needs alpha because we use clearColor to convert the background color to alpha.
|
|
1058
|
-
// It might also contain some characters with transparent backgrounds if allowTransparency is
|
|
1059
|
-
// set.
|
|
1060
|
-
this.ctx = throwIfFalsy(this.canvas.getContext('2d', { alpha: true }));
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
public clear(): void {
|
|
1064
|
-
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
1065
|
-
this.currentRow.x = 0;
|
|
1066
|
-
this.currentRow.y = 0;
|
|
1067
|
-
this.currentRow.height = 0;
|
|
1068
|
-
this.fixedRows.length = 0;
|
|
1069
|
-
this.version++;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
/**
|
|
1074
|
-
* Makes a particular rgb color and colors that are nearly the same in an ImageData completely
|
|
1075
|
-
* transparent.
|
|
1076
|
-
* @returns True if the result is "empty", meaning all pixels are fully transparent.
|
|
1077
|
-
*/
|
|
1078
|
-
function clearColor(imageData: ImageData, bg: IColor, fg: IColor, enableThresholdCheck: boolean): boolean {
|
|
1079
|
-
// Get color channels
|
|
1080
|
-
const r = bg.rgba >>> 24;
|
|
1081
|
-
const g = bg.rgba >>> 16 & 0xFF;
|
|
1082
|
-
const b = bg.rgba >>> 8 & 0xFF;
|
|
1083
|
-
const fgR = fg.rgba >>> 24;
|
|
1084
|
-
const fgG = fg.rgba >>> 16 & 0xFF;
|
|
1085
|
-
const fgB = fg.rgba >>> 8 & 0xFF;
|
|
1086
|
-
|
|
1087
|
-
// Calculate a threshold that when below a color will be treated as transpart when the sum of
|
|
1088
|
-
// channel value differs. This helps improve rendering when glyphs overlap with others. This
|
|
1089
|
-
// threshold is calculated relative to the difference between the background and foreground to
|
|
1090
|
-
// ensure important details of the glyph are always shown, even when the contrast ratio is low.
|
|
1091
|
-
// The number 12 is largely arbitrary to ensure the pixels that escape the cell in the test case
|
|
1092
|
-
// were covered (fg=#8ae234, bg=#c4a000).
|
|
1093
|
-
const threshold = Math.floor((Math.abs(r - fgR) + Math.abs(g - fgG) + Math.abs(b - fgB)) / 12);
|
|
1094
|
-
|
|
1095
|
-
// Set alpha channel of relevent pixels to 0
|
|
1096
|
-
let isEmpty = true;
|
|
1097
|
-
for (let offset = 0; offset < imageData.data.length; offset += 4) {
|
|
1098
|
-
// Check exact match
|
|
1099
|
-
if (imageData.data[offset] === r &&
|
|
1100
|
-
imageData.data[offset + 1] === g &&
|
|
1101
|
-
imageData.data[offset + 2] === b) {
|
|
1102
|
-
imageData.data[offset + 3] = 0;
|
|
1103
|
-
} else {
|
|
1104
|
-
// Check the threshold based difference
|
|
1105
|
-
if (enableThresholdCheck &&
|
|
1106
|
-
(Math.abs(imageData.data[offset] - r) +
|
|
1107
|
-
Math.abs(imageData.data[offset + 1] - g) +
|
|
1108
|
-
Math.abs(imageData.data[offset + 2] - b)) < threshold) {
|
|
1109
|
-
imageData.data[offset + 3] = 0;
|
|
1110
|
-
} else {
|
|
1111
|
-
isEmpty = false;
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
|
-
return isEmpty;
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
function checkCompletelyTransparent(imageData: ImageData): boolean {
|
|
1120
|
-
for (let offset = 0; offset < imageData.data.length; offset += 4) {
|
|
1121
|
-
if (imageData.data[offset + 3] > 0) {
|
|
1122
|
-
return false;
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
return true;
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
function createCanvas(document: Document, width: number, height: number): HTMLCanvasElement {
|
|
1129
|
-
const canvas = document.createElement('canvas');
|
|
1130
|
-
canvas.width = width;
|
|
1131
|
-
canvas.height = height;
|
|
1132
|
-
return canvas;
|
|
1133
|
-
}
|