Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/viewParts/minimap/minimapCharRenderer.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { RGBA8 } from '../../../common/core/misc/rgba.js';
7
import { Constants, getCharIndex } from './minimapCharSheet.js';
8
import { toUint8 } from '../../../../base/common/uint.js';
9
10
export class MinimapCharRenderer {
11
_minimapCharRendererBrand: void = undefined;
12
13
private readonly charDataNormal: Uint8ClampedArray;
14
private readonly charDataLight: Uint8ClampedArray;
15
16
constructor(charData: Uint8ClampedArray, public readonly scale: number) {
17
this.charDataNormal = MinimapCharRenderer.soften(charData, 12 / 15);
18
this.charDataLight = MinimapCharRenderer.soften(charData, 50 / 60);
19
}
20
21
private static soften(input: Uint8ClampedArray, ratio: number): Uint8ClampedArray {
22
const result = new Uint8ClampedArray(input.length);
23
for (let i = 0, len = input.length; i < len; i++) {
24
result[i] = toUint8(input[i] * ratio);
25
}
26
return result;
27
}
28
29
public renderChar(
30
target: ImageData,
31
dx: number,
32
dy: number,
33
chCode: number,
34
color: RGBA8,
35
foregroundAlpha: number,
36
backgroundColor: RGBA8,
37
backgroundAlpha: number,
38
fontScale: number,
39
useLighterFont: boolean,
40
force1pxHeight: boolean
41
): void {
42
const charWidth = Constants.BASE_CHAR_WIDTH * this.scale;
43
const charHeight = Constants.BASE_CHAR_HEIGHT * this.scale;
44
const renderHeight = (force1pxHeight ? 1 : charHeight);
45
if (dx + charWidth > target.width || dy + renderHeight > target.height) {
46
console.warn('bad render request outside image data');
47
return;
48
}
49
50
const charData = useLighterFont ? this.charDataLight : this.charDataNormal;
51
const charIndex = getCharIndex(chCode, fontScale);
52
53
const destWidth = target.width * Constants.RGBA_CHANNELS_CNT;
54
55
const backgroundR = backgroundColor.r;
56
const backgroundG = backgroundColor.g;
57
const backgroundB = backgroundColor.b;
58
59
const deltaR = color.r - backgroundR;
60
const deltaG = color.g - backgroundG;
61
const deltaB = color.b - backgroundB;
62
63
const destAlpha = Math.max(foregroundAlpha, backgroundAlpha);
64
65
const dest = target.data;
66
let sourceOffset = charIndex * charWidth * charHeight;
67
68
let row = dy * destWidth + dx * Constants.RGBA_CHANNELS_CNT;
69
for (let y = 0; y < renderHeight; y++) {
70
let column = row;
71
for (let x = 0; x < charWidth; x++) {
72
const c = (charData[sourceOffset++] / 255) * (foregroundAlpha / 255);
73
dest[column++] = backgroundR + deltaR * c;
74
dest[column++] = backgroundG + deltaG * c;
75
dest[column++] = backgroundB + deltaB * c;
76
dest[column++] = destAlpha;
77
}
78
79
row += destWidth;
80
}
81
}
82
83
public blockRenderChar(
84
target: ImageData,
85
dx: number,
86
dy: number,
87
color: RGBA8,
88
foregroundAlpha: number,
89
backgroundColor: RGBA8,
90
backgroundAlpha: number,
91
force1pxHeight: boolean
92
): void {
93
const charWidth = Constants.BASE_CHAR_WIDTH * this.scale;
94
const charHeight = Constants.BASE_CHAR_HEIGHT * this.scale;
95
const renderHeight = (force1pxHeight ? 1 : charHeight);
96
if (dx + charWidth > target.width || dy + renderHeight > target.height) {
97
console.warn('bad render request outside image data');
98
return;
99
}
100
101
const destWidth = target.width * Constants.RGBA_CHANNELS_CNT;
102
103
const c = 0.5 * (foregroundAlpha / 255);
104
105
const backgroundR = backgroundColor.r;
106
const backgroundG = backgroundColor.g;
107
const backgroundB = backgroundColor.b;
108
109
const deltaR = color.r - backgroundR;
110
const deltaG = color.g - backgroundG;
111
const deltaB = color.b - backgroundB;
112
113
const colorR = backgroundR + deltaR * c;
114
const colorG = backgroundG + deltaG * c;
115
const colorB = backgroundB + deltaB * c;
116
117
const destAlpha = Math.max(foregroundAlpha, backgroundAlpha);
118
119
const dest = target.data;
120
121
let row = dy * destWidth + dx * Constants.RGBA_CHANNELS_CNT;
122
for (let y = 0; y < renderHeight; y++) {
123
let column = row;
124
for (let x = 0; x < charWidth; x++) {
125
dest[column++] = colorR;
126
dest[column++] = colorG;
127
dest[column++] = colorB;
128
dest[column++] = destAlpha;
129
}
130
131
row += destWidth;
132
}
133
}
134
}
135
136