Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/browser/gpu/atlas/testUtil.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 { fail, ok } from 'assert';
7
import type { ITextureAtlasPageGlyph } from '../../../../browser/gpu/atlas/atlas.js';
8
import { TextureAtlas } from '../../../../browser/gpu/atlas/textureAtlas.js';
9
import { isNumber } from '../../../../../base/common/types.js';
10
import { ensureNonNullable } from '../../../../browser/gpu/gpuUtils.js';
11
12
export function assertIsValidGlyph(glyph: Readonly<ITextureAtlasPageGlyph> | undefined, atlasOrSource: TextureAtlas | OffscreenCanvas) {
13
if (glyph === undefined) {
14
fail('glyph is undefined');
15
}
16
const pageW = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pageSize : atlasOrSource.width;
17
const pageH = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pageSize : atlasOrSource.width;
18
const source = atlasOrSource instanceof TextureAtlas ? atlasOrSource.pages[glyph.pageIndex].source : atlasOrSource;
19
20
// (x,y) are valid coordinates
21
ok(isNumber(glyph.x));
22
ok(glyph.x >= 0);
23
ok(glyph.x < pageW);
24
ok(isNumber(glyph.y));
25
ok(glyph.y >= 0);
26
ok(glyph.y < pageH);
27
28
// (w,h) are valid dimensions
29
ok(isNumber(glyph.w));
30
ok(glyph.w > 0);
31
ok(glyph.w <= pageW);
32
ok(isNumber(glyph.h));
33
ok(glyph.h > 0);
34
ok(glyph.h <= pageH);
35
36
// (originOffsetX, originOffsetY) are valid offsets
37
ok(isNumber(glyph.originOffsetX));
38
ok(isNumber(glyph.originOffsetY));
39
40
// (x,y) + (w,h) are within the bounds of the atlas
41
ok(glyph.x + glyph.w <= pageW);
42
ok(glyph.y + glyph.h <= pageH);
43
44
// Each of the glyph's outer pixel edges contain at least 1 non-transparent pixel
45
const ctx = ensureNonNullable(source.getContext('2d'));
46
const edges = [
47
ctx.getImageData(glyph.x, glyph.y, glyph.w, 1).data,
48
ctx.getImageData(glyph.x, glyph.y + glyph.h - 1, glyph.w, 1).data,
49
ctx.getImageData(glyph.x, glyph.y, 1, glyph.h).data,
50
ctx.getImageData(glyph.x + glyph.w - 1, glyph.y, 1, glyph.h).data,
51
];
52
for (const edge of edges) {
53
ok(edge.some(color => (color & 0xFF) !== 0));
54
}
55
}
56
57