Path: blob/main/src/vs/editor/browser/viewParts/minimap/minimapCharRenderer.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { RGBA8 } from '../../../common/core/misc/rgba.js';6import { Constants, getCharIndex } from './minimapCharSheet.js';7import { toUint8 } from '../../../../base/common/uint.js';89export class MinimapCharRenderer {10_minimapCharRendererBrand: void = undefined;1112private readonly charDataNormal: Uint8ClampedArray;13private readonly charDataLight: Uint8ClampedArray;1415constructor(charData: Uint8ClampedArray, public readonly scale: number) {16this.charDataNormal = MinimapCharRenderer.soften(charData, 12 / 15);17this.charDataLight = MinimapCharRenderer.soften(charData, 50 / 60);18}1920private static soften(input: Uint8ClampedArray, ratio: number): Uint8ClampedArray {21const result = new Uint8ClampedArray(input.length);22for (let i = 0, len = input.length; i < len; i++) {23result[i] = toUint8(input[i] * ratio);24}25return result;26}2728public renderChar(29target: ImageData,30dx: number,31dy: number,32chCode: number,33color: RGBA8,34foregroundAlpha: number,35backgroundColor: RGBA8,36backgroundAlpha: number,37fontScale: number,38useLighterFont: boolean,39force1pxHeight: boolean40): void {41const charWidth = Constants.BASE_CHAR_WIDTH * this.scale;42const charHeight = Constants.BASE_CHAR_HEIGHT * this.scale;43const renderHeight = (force1pxHeight ? 1 : charHeight);44if (dx + charWidth > target.width || dy + renderHeight > target.height) {45console.warn('bad render request outside image data');46return;47}4849const charData = useLighterFont ? this.charDataLight : this.charDataNormal;50const charIndex = getCharIndex(chCode, fontScale);5152const destWidth = target.width * Constants.RGBA_CHANNELS_CNT;5354const backgroundR = backgroundColor.r;55const backgroundG = backgroundColor.g;56const backgroundB = backgroundColor.b;5758const deltaR = color.r - backgroundR;59const deltaG = color.g - backgroundG;60const deltaB = color.b - backgroundB;6162const destAlpha = Math.max(foregroundAlpha, backgroundAlpha);6364const dest = target.data;65let sourceOffset = charIndex * charWidth * charHeight;6667let row = dy * destWidth + dx * Constants.RGBA_CHANNELS_CNT;68for (let y = 0; y < renderHeight; y++) {69let column = row;70for (let x = 0; x < charWidth; x++) {71const c = (charData[sourceOffset++] / 255) * (foregroundAlpha / 255);72dest[column++] = backgroundR + deltaR * c;73dest[column++] = backgroundG + deltaG * c;74dest[column++] = backgroundB + deltaB * c;75dest[column++] = destAlpha;76}7778row += destWidth;79}80}8182public blockRenderChar(83target: ImageData,84dx: number,85dy: number,86color: RGBA8,87foregroundAlpha: number,88backgroundColor: RGBA8,89backgroundAlpha: number,90force1pxHeight: boolean91): void {92const charWidth = Constants.BASE_CHAR_WIDTH * this.scale;93const charHeight = Constants.BASE_CHAR_HEIGHT * this.scale;94const renderHeight = (force1pxHeight ? 1 : charHeight);95if (dx + charWidth > target.width || dy + renderHeight > target.height) {96console.warn('bad render request outside image data');97return;98}99100const destWidth = target.width * Constants.RGBA_CHANNELS_CNT;101102const c = 0.5 * (foregroundAlpha / 255);103104const backgroundR = backgroundColor.r;105const backgroundG = backgroundColor.g;106const backgroundB = backgroundColor.b;107108const deltaR = color.r - backgroundR;109const deltaG = color.g - backgroundG;110const deltaB = color.b - backgroundB;111112const colorR = backgroundR + deltaR * c;113const colorG = backgroundG + deltaG * c;114const colorB = backgroundB + deltaB * c;115116const destAlpha = Math.max(foregroundAlpha, backgroundAlpha);117118const dest = target.data;119120let row = dy * destWidth + dx * Constants.RGBA_CHANNELS_CNT;121for (let y = 0; y < renderHeight; y++) {122let column = row;123for (let x = 0; x < charWidth; x++) {124dest[column++] = colorR;125dest[column++] = colorG;126dest[column++] = colorB;127dest[column++] = destAlpha;128}129130row += destWidth;131}132}133}134135136