minimapCharRenderer.ts 10.7 KB
Newer Older
A
Alex Dima 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import { ColorId, TokenizationRegistry } from 'vs/editor/common/modes';
8
import Event, { Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
9
import { RGBA8 } from 'vs/editor/common/core/rgba';
A
Alex Dima 已提交
10

A
Alex Dima 已提交
11 12 13 14 15 16 17 18 19
export class MinimapTokensColorTracker {
	private static _INSTANCE: MinimapTokensColorTracker = null;
	public static getInstance(): MinimapTokensColorTracker {
		if (!this._INSTANCE) {
			this._INSTANCE = new MinimapTokensColorTracker();
		}
		return this._INSTANCE;
	}

20
	private _colors: RGBA8[];
A
Alex Dima 已提交
21
	private _backgroundIsLight: boolean;
A
Alex Dima 已提交
22

A
Alex Dima 已提交
23
	private _onDidChange = new Emitter<void>();
24
	public readonly onDidChange: Event<void> = this._onDidChange.event;
A
Alex Dima 已提交
25 26

	private constructor() {
A
Alex Dima 已提交
27
		this._updateColorMap();
28 29
		TokenizationRegistry.onDidChange((e) => {
			if (e.changedColorMap) {
A
Alex Dima 已提交
30
				this._updateColorMap();
A
Alex Dima 已提交
31
			}
32
		});
A
Alex Dima 已提交
33
	}
A
Alex Dima 已提交
34

A
Alex Dima 已提交
35 36
	private _updateColorMap(): void {
		const colorMap = TokenizationRegistry.getColorMap();
37 38 39 40 41
		if (!colorMap) {
			this._colors = [null];
			this._backgroundIsLight = true;
			return;
		}
A
Alex Dima 已提交
42 43
		this._colors = [null];
		for (let colorId = 1; colorId < colorMap.length; colorId++) {
44 45
			const source = colorMap[colorId].rgba;
			// Use a VM friendly data-type
R
rebornix 已提交
46
			this._colors[colorId] = new RGBA8(source.r, source.g, source.b, Math.round(source.a * 255));
A
Alex Dima 已提交
47
		}
J
Joao Moreno 已提交
48
		let backgroundLuminosity = colorMap[ColorId.DefaultBackground].getRelativeLuminance();
A
Alex Dima 已提交
49
		this._backgroundIsLight = (backgroundLuminosity >= 0.5);
A
Alex Dima 已提交
50
		this._onDidChange.fire(void 0);
A
Alex Dima 已提交
51 52
	}

53
	public getColor(colorId: ColorId): RGBA8 {
A
Alex Dima 已提交
54 55
		if (colorId < 1 || colorId >= this._colors.length) {
			// background color (basically invisible)
A
Alex Dima 已提交
56
			colorId = ColorId.DefaultBackground;
A
Alex Dima 已提交
57 58 59
		}
		return this._colors[colorId];
	}
A
Alex Dima 已提交
60 61 62 63

	public backgroundIsLight(): boolean {
		return this._backgroundIsLight;
	}
A
Alex Dima 已提交
64 65
}

A
Alex Dima 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
export const enum Constants {
	START_CH_CODE = 32, // Space
	END_CH_CODE = 126, // Tilde (~)
	CHAR_COUNT = END_CH_CODE - START_CH_CODE + 1,

	SAMPLED_CHAR_HEIGHT = 16,
	SAMPLED_CHAR_WIDTH = 10,
	SAMPLED_HALF_CHAR_WIDTH = SAMPLED_CHAR_WIDTH / 2,

	x2_CHAR_HEIGHT = 4,
	x2_CHAR_WIDTH = 2,

	x1_CHAR_HEIGHT = 2,
	x1_CHAR_WIDTH = 1,

	RGBA_CHANNELS_CNT = 4,
}

A
Alex Dima 已提交
84
export class MinimapCharRenderer {
A
Alex Dima 已提交
85 86 87 88 89 90

	_minimapCharRendererBrand: void;

	public readonly x2charData: Uint8ClampedArray;
	public readonly x1charData: Uint8ClampedArray;

A
Alex Dima 已提交
91 92 93
	public readonly x2charDataLight: Uint8ClampedArray;
	public readonly x1charDataLight: Uint8ClampedArray;

A
Alex Dima 已提交
94
	constructor(x2CharData: Uint8ClampedArray, x1CharData: Uint8ClampedArray) {
95
		const x2ExpectedLen = Constants.x2_CHAR_HEIGHT * Constants.x2_CHAR_WIDTH * Constants.CHAR_COUNT;
A
Alex Dima 已提交
96 97 98
		if (x2CharData.length !== x2ExpectedLen) {
			throw new Error('Invalid x2CharData');
		}
99
		const x1ExpectedLen = Constants.x1_CHAR_HEIGHT * Constants.x1_CHAR_WIDTH * Constants.CHAR_COUNT;
A
Alex Dima 已提交
100 101 102 103 104
		if (x1CharData.length !== x1ExpectedLen) {
			throw new Error('Invalid x1CharData');
		}
		this.x2charData = x2CharData;
		this.x1charData = x1CharData;
A
Alex Dima 已提交
105 106 107 108 109 110 111 112 113 114 115

		this.x2charDataLight = MinimapCharRenderer.soften(x2CharData, 12 / 15);
		this.x1charDataLight = MinimapCharRenderer.soften(x1CharData, 50 / 60);
	}

	private static soften(input: Uint8ClampedArray, ratio: number): Uint8ClampedArray {
		let result = new Uint8ClampedArray(input.length);
		for (let i = 0, len = input.length; i < len; i++) {
			result[i] = input[i] * ratio;
		}
		return result;
A
Alex Dima 已提交
116 117 118
	}

	private static _getChIndex(chCode: number): number {
119 120 121
		chCode -= Constants.START_CH_CODE;
		if (chCode < 0) {
			chCode += Constants.CHAR_COUNT;
A
Alex Dima 已提交
122
		}
123
		return (chCode % Constants.CHAR_COUNT);
A
Alex Dima 已提交
124 125
	}

126
	public x2RenderChar(target: ImageData, dx: number, dy: number, chCode: number, color: RGBA8, backgroundColor: RGBA8, useLighterFont: boolean): void {
127 128 129 130
		if (dx + Constants.x2_CHAR_WIDTH > target.width || dy + Constants.x2_CHAR_HEIGHT > target.height) {
			console.warn('bad render request outside image data');
			return;
		}
A
Alex Dima 已提交
131
		const x2CharData = useLighterFont ? this.x2charDataLight : this.x2charData;
A
Alex Dima 已提交
132
		const chIndex = MinimapCharRenderer._getChIndex(chCode);
A
Alex Dima 已提交
133 134

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;
A
Alex Dima 已提交
135 136 137 138 139 140 141 142

		const backgroundR = backgroundColor.r;
		const backgroundG = backgroundColor.g;
		const backgroundB = backgroundColor.b;

		const deltaR = color.r - backgroundR;
		const deltaG = color.g - backgroundG;
		const deltaB = color.b - backgroundB;
A
Alex Dima 已提交
143 144

		const dest = target.data;
A
Alex Dima 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		const sourceOffset = chIndex * Constants.x2_CHAR_HEIGHT * Constants.x2_CHAR_WIDTH;
		let destOffset = dy * outWidth + dx * Constants.RGBA_CHANNELS_CNT;
		{
			const c = x2CharData[sourceOffset] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}
		{
			const c = x2CharData[sourceOffset + 1] / 255;
			dest[destOffset + 4] = backgroundR + deltaR * c;
			dest[destOffset + 5] = backgroundG + deltaG * c;
			dest[destOffset + 6] = backgroundB + deltaB * c;
		}

		destOffset += outWidth;
		{
			const c = x2CharData[sourceOffset + 2] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}
		{
			const c = x2CharData[sourceOffset + 3] / 255;
			dest[destOffset + 4] = backgroundR + deltaR * c;
			dest[destOffset + 5] = backgroundG + deltaG * c;
			dest[destOffset + 6] = backgroundB + deltaB * c;
		}

		destOffset += outWidth;
		{
			const c = x2CharData[sourceOffset + 4] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}
		{
			const c = x2CharData[sourceOffset + 5] / 255;
			dest[destOffset + 4] = backgroundR + deltaR * c;
			dest[destOffset + 5] = backgroundG + deltaG * c;
			dest[destOffset + 6] = backgroundB + deltaB * c;
		}

		destOffset += outWidth;
		{
			const c = x2CharData[sourceOffset + 6] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}
		{
			const c = x2CharData[sourceOffset + 7] / 255;
			dest[destOffset + 4] = backgroundR + deltaR * c;
			dest[destOffset + 5] = backgroundG + deltaG * c;
			dest[destOffset + 6] = backgroundB + deltaB * c;
		}
A
Alex Dima 已提交
201 202
	}

203
	public x1RenderChar(target: ImageData, dx: number, dy: number, chCode: number, color: RGBA8, backgroundColor: RGBA8, useLighterFont: boolean): void {
204 205 206 207
		if (dx + Constants.x1_CHAR_WIDTH > target.width || dy + Constants.x1_CHAR_HEIGHT > target.height) {
			console.warn('bad render request outside image data');
			return;
		}
A
Alex Dima 已提交
208
		const x1CharData = useLighterFont ? this.x1charDataLight : this.x1charData;
A
Alex Dima 已提交
209
		const chIndex = MinimapCharRenderer._getChIndex(chCode);
A
Alex Dima 已提交
210 211

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;
212 213 214 215 216 217 218 219

		const backgroundR = backgroundColor.r;
		const backgroundG = backgroundColor.g;
		const backgroundB = backgroundColor.b;

		const deltaR = color.r - backgroundR;
		const deltaG = color.g - backgroundG;
		const deltaB = color.b - backgroundB;
A
Alex Dima 已提交
220 221

		const dest = target.data;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		const sourceOffset = chIndex * Constants.x1_CHAR_HEIGHT * Constants.x1_CHAR_WIDTH;
		let destOffset = dy * outWidth + dx * Constants.RGBA_CHANNELS_CNT;
		{
			const c = x1CharData[sourceOffset] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}

		destOffset += outWidth;
		{
			const c = x1CharData[sourceOffset + 1] / 255;
			dest[destOffset + 0] = backgroundR + deltaR * c;
			dest[destOffset + 1] = backgroundG + deltaG * c;
			dest[destOffset + 2] = backgroundB + deltaB * c;
		}
A
Alex Dima 已提交
238
	}
239

240
	public x2BlockRenderChar(target: ImageData, dx: number, dy: number, color: RGBA8, backgroundColor: RGBA8, useLighterFont: boolean): void {
A
Alex Dima 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		if (dx + Constants.x2_CHAR_WIDTH > target.width || dy + Constants.x2_CHAR_HEIGHT > target.height) {
			console.warn('bad render request outside image data');
			return;
		}

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;

		const c = 0.5;

		const backgroundR = backgroundColor.r;
		const backgroundG = backgroundColor.g;
		const backgroundB = backgroundColor.b;

		const deltaR = color.r - backgroundR;
		const deltaG = color.g - backgroundG;
		const deltaB = color.b - backgroundB;

258
		const colorR = backgroundR + deltaR * c;
A
Alex Dima 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
		const colorG = backgroundG + deltaG * c;
		const colorB = backgroundB + deltaB * c;

		const dest = target.data;
		let destOffset = dy * outWidth + dx * Constants.RGBA_CHANNELS_CNT;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}
		{
			dest[destOffset + 4] = colorR;
			dest[destOffset + 5] = colorG;
			dest[destOffset + 6] = colorB;
		}

		destOffset += outWidth;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}
		{
			dest[destOffset + 4] = colorR;
			dest[destOffset + 5] = colorG;
			dest[destOffset + 6] = colorB;
		}

		destOffset += outWidth;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}
		{
			dest[destOffset + 4] = colorR;
			dest[destOffset + 5] = colorG;
			dest[destOffset + 6] = colorB;
		}

		destOffset += outWidth;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}
		{
			dest[destOffset + 4] = colorR;
			dest[destOffset + 5] = colorG;
			dest[destOffset + 6] = colorB;
		}
	}

312
	public x1BlockRenderChar(target: ImageData, dx: number, dy: number, color: RGBA8, backgroundColor: RGBA8, useLighterFont: boolean): void {
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		if (dx + Constants.x1_CHAR_WIDTH > target.width || dy + Constants.x1_CHAR_HEIGHT > target.height) {
			console.warn('bad render request outside image data');
			return;
		}

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;

		const c = 0.5;

		const backgroundR = backgroundColor.r;
		const backgroundG = backgroundColor.g;
		const backgroundB = backgroundColor.b;

		const deltaR = color.r - backgroundR;
		const deltaG = color.g - backgroundG;
		const deltaB = color.b - backgroundB;

330
		const colorR = backgroundR + deltaR * c;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		const colorG = backgroundG + deltaG * c;
		const colorB = backgroundB + deltaB * c;

		const dest = target.data;

		let destOffset = dy * outWidth + dx * Constants.RGBA_CHANNELS_CNT;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}

		destOffset += outWidth;
		{
			dest[destOffset + 0] = colorR;
			dest[destOffset + 1] = colorG;
			dest[destOffset + 2] = colorB;
		}
	}
A
Alex Dima 已提交
350
}