Path: blob/main/src/vs/editor/test/browser/gpu/atlas/testUtil.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 { fail, ok } from 'assert';6import type { ITextureAtlasPageGlyph } from '../../../../browser/gpu/atlas/atlas.js';7import { TextureAtlas } from '../../../../browser/gpu/atlas/textureAtlas.js';8import { isNumber } from '../../../../../base/common/types.js';9import { ensureNonNullable } from '../../../../browser/gpu/gpuUtils.js';1011export function assertIsValidGlyph(glyph: Readonly<ITextureAtlasPageGlyph> | undefined, atlasOrSource: TextureAtlas | OffscreenCanvas) {12if (glyph === undefined) {13fail('glyph is undefined');14}15const pageW = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pageSize : atlasOrSource.width;16const pageH = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pageSize : atlasOrSource.width;17const source = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pages[glyph.pageIndex].source : atlasOrSource;1819// (x,y) are valid coordinates20ok(isNumber(glyph.x));21ok(glyph.x >= 0);22ok(glyph.x < pageW);23ok(isNumber(glyph.y));24ok(glyph.y >= 0);25ok(glyph.y < pageH);2627// (w,h) are valid dimensions28ok(isNumber(glyph.w));29ok(glyph.w > 0);30ok(glyph.w <= pageW);31ok(isNumber(glyph.h));32ok(glyph.h > 0);33ok(glyph.h <= pageH);3435// (originOffsetX, originOffsetY) are valid offsets36ok(isNumber(glyph.originOffsetX));37ok(isNumber(glyph.originOffsetY));3839// (x,y) + (w,h) are within the bounds of the atlas40ok(glyph.x + glyph.w <= pageW);41ok(glyph.y + glyph.h <= pageH);4243// Each of the glyph's outer pixel edges contain at least 1 non-transparent pixel44const ctx = ensureNonNullable(source.getContext('2d'));45const edges = [46ctx.getImageData(glyph.x, glyph.y, glyph.w, 1).data,47ctx.getImageData(glyph.x, glyph.y + glyph.h - 1, glyph.w, 1).data,48ctx.getImageData(glyph.x, glyph.y, 1, glyph.h).data,49ctx.getImageData(glyph.x + glyph.w - 1, glyph.y, 1, glyph.h).data,50];51for (const edge of edges) {52ok(edge.some(color => (color & 0xFF) !== 0));53}54}555657