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

import { CharCode } from 'vs/base/common/charCode';
A
Alex Dima 已提交
8
import { ColorId, TokenizationRegistry } from 'vs/editor/common/modes';
9
import Event, { Emitter } from 'vs/base/common/event';
A
Alex Dima 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23

export class ParsedColor {

	public readonly r: number;
	public readonly g: number;
	public readonly b: number;

	constructor(r, g, b) {
		this.r = r;
		this.g = g;
		this.b = b;
	}
}

A
Alex Dima 已提交
24 25 26 27 28 29 30 31 32 33 34
export class MinimapTokensColorTracker {
	private static _INSTANCE: MinimapTokensColorTracker = null;
	public static getInstance(): MinimapTokensColorTracker {
		if (!this._INSTANCE) {
			this._INSTANCE = new MinimapTokensColorTracker();
		}
		return this._INSTANCE;
	}

	private _lastColorMap: string[];
	private _colors: ParsedColor[];
A
Alex Dima 已提交
35

A
Alex Dima 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	private _onDidChange = new Emitter<void>();
	public onDidChange: Event<void> = this._onDidChange.event;

	private constructor() {
		this._lastColorMap = [];
		this._setColorMap(TokenizationRegistry.getColorMap());
		TokenizationRegistry.onDidChange(() => this._setColorMap(TokenizationRegistry.getColorMap()));
	}

	private static _equals(a: string[], b: string[]): boolean {
		if (a.length !== b.length) {
			return false;
		}
		for (let i = 0, len = a.length; i < len; i++) {
			if (a[i] !== b[i]) {
				return false;
			}
		}
		return true;
	}
A
Alex Dima 已提交
56

A
Alex Dima 已提交
57 58 59 60 61
	private _setColorMap(colorMap: string[]): void {
		if (MinimapTokensColorTracker._equals(this._lastColorMap, colorMap)) {
			return;
		}
		this._lastColorMap = colorMap.slice(0);
A
Alex Dima 已提交
62 63
		this._colors = [null];
		for (let colorId = 1; colorId < colorMap.length; colorId++) {
A
Alex Dima 已提交
64
			this._colors[colorId] = MinimapTokensColorTracker._parseColor(colorMap[colorId]);
A
Alex Dima 已提交
65
		}
A
Alex Dima 已提交
66
		this._onDidChange.fire(void 0);
A
Alex Dima 已提交
67 68
	}

A
Alex Dima 已提交
69
	public getColor(colorId: ColorId): ParsedColor {
A
Alex Dima 已提交
70 71 72 73 74 75 76 77 78 79 80 81
		if (colorId < 1 || colorId >= this._colors.length) {
			// background color (basically invisible)
			colorId = 2;
		}
		return this._colors[colorId];
	}

	public static _parseColor(color: string): ParsedColor {
		if (!color) {
			return new ParsedColor(0, 0, 0);
		}
		if (color.charCodeAt(0) === CharCode.Hash) {
A
Alex Dima 已提交
82 83 84
			color = color.substr(1, 6);
		} else {
			color = color.substr(0, 6);
A
Alex Dima 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		}
		if (color.length !== 6) {
			return new ParsedColor(0, 0, 0);
		}

		let r = 16 * this._parseHexDigit(color.charCodeAt(0)) + this._parseHexDigit(color.charCodeAt(1));
		let g = 16 * this._parseHexDigit(color.charCodeAt(2)) + this._parseHexDigit(color.charCodeAt(3));
		let b = 16 * this._parseHexDigit(color.charCodeAt(4)) + this._parseHexDigit(color.charCodeAt(5));
		return new ParsedColor(r, g, b);
	}

	private static _parseHexDigit(charCode: CharCode): number {
		switch (charCode) {
			case CharCode.Digit0: return 0;
			case CharCode.Digit1: return 1;
			case CharCode.Digit2: return 2;
			case CharCode.Digit3: return 3;
			case CharCode.Digit4: return 4;
			case CharCode.Digit5: return 5;
			case CharCode.Digit6: return 6;
			case CharCode.Digit7: return 7;
			case CharCode.Digit8: return 8;
			case CharCode.Digit9: return 9;
			case CharCode.a: return 10;
			case CharCode.A: return 10;
			case CharCode.b: return 11;
			case CharCode.B: return 11;
			case CharCode.c: return 12;
			case CharCode.C: return 12;
			case CharCode.d: return 13;
			case CharCode.D: return 13;
			case CharCode.e: return 14;
			case CharCode.E: return 14;
			case CharCode.f: return 15;
			case CharCode.F: return 15;
		}
		return 0;
	}
}

A
Alex Dima 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 已提交
143
export class MinimapCharRenderer {
A
Alex Dima 已提交
144 145 146 147 148 149 150

	_minimapCharRendererBrand: void;

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

	constructor(x2CharData: Uint8ClampedArray, x1CharData: Uint8ClampedArray) {
151
		const x2ExpectedLen = Constants.x2_CHAR_HEIGHT * Constants.x2_CHAR_WIDTH * Constants.CHAR_COUNT;
A
Alex Dima 已提交
152 153 154
		if (x2CharData.length !== x2ExpectedLen) {
			throw new Error('Invalid x2CharData');
		}
155
		const x1ExpectedLen = Constants.x1_CHAR_HEIGHT * Constants.x1_CHAR_WIDTH * Constants.CHAR_COUNT;
A
Alex Dima 已提交
156 157 158 159 160 161 162 163
		if (x1CharData.length !== x1ExpectedLen) {
			throw new Error('Invalid x1CharData');
		}
		this.x2charData = x2CharData;
		this.x1charData = x1CharData;
	}

	private static _getChIndex(chCode: number): number {
164 165 166
		chCode -= Constants.START_CH_CODE;
		if (chCode < 0) {
			chCode += Constants.CHAR_COUNT;
A
Alex Dima 已提交
167
		}
168
		return (chCode % Constants.CHAR_COUNT);
A
Alex Dima 已提交
169 170
	}

A
Alex Dima 已提交
171
	public x2RenderChar(target: ImageData, dx: number, dy: number, chCode: number, color: ParsedColor, backgroundColor: ParsedColor): void {
172 173 174 175
		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 已提交
176
		const x2CharData = this.x2charData;
A
Alex Dima 已提交
177
		const chIndex = MinimapCharRenderer._getChIndex(chCode);
A
Alex Dima 已提交
178 179

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;
A
Alex Dima 已提交
180 181 182 183 184 185 186 187

		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 已提交
188 189

		const dest = target.data;
A
Alex Dima 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		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 已提交
246 247
	}

248 249 250 251 252
	public x1RenderChar(target: ImageData, dx: number, dy: number, chCode: number, color: ParsedColor, backgroundColor: ParsedColor): void {
		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 已提交
253
		const x1CharData = this.x1charData;
A
Alex Dima 已提交
254
		const chIndex = MinimapCharRenderer._getChIndex(chCode);
A
Alex Dima 已提交
255 256

		const outWidth = target.width * Constants.RGBA_CHANNELS_CNT;
257 258 259 260 261 262 263 264

		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 已提交
265 266

		const dest = target.data;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		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 已提交
283 284
	}
}