Rasterise a GPU icon's SVG at its own size, apart from its drawn size

The Cesium draw port's GPU icon sets put each image into the shared icon
atlas at the item's drawn size. An SVG data URL (or SVG ref) that gives its
root a pixel width and height now takes the atlas at the larger of those,
at most 256 px, as a billboard rasterises it at its natural size; other
images still take the drawn size. A layer drawing a 64 px glyph at 20 CSS
px, with a 192 px near image, keeps both rasters through the draw port.
The atlas-room check counts the same slots the rows then take.

Ships (32 px SVGs drawn at 32) take the slots they took before.
This commit is contained in:
Sameh Khamis committed 2026-11-07 02:36:39 -08:00
1 parent e72382b1aa
commit 87395bb733
2 files changed
+137 -11

No files matched your search

+71
View File
@@ -678,3 +678,74 @@ test('a row keeps the course it showed when its item’s motion is changed in pl
);
assert.ok(Math.abs(course('a', at + 2500) - 90) < 0.01, 'there by 2 s');
});
test('an SVG takes the atlas at its own size, other images at the item’s', async () => {
const { iconAtlas } = await import('../../data/iconAtlas.js');
const { svgRasterPx } = await import('./gpuIcons.js');
const svg = (w, h, extra = '') =>
`<svg xmlns="http://www.w3.org/2000/svg" ${extra} width="${w}" height="${h}" viewBox="0 0 10 10"><rect stroke-width="9"/></svg>`;
const b64 = (text) => 'data:image/svg+xml;base64,' + btoa(text);
const enc = (text) =>
'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(text);
assert.equal(svgRasterPx(b64(svg(192, 192, 'stroke-width="3"'))), 192);
assert.equal(
svgRasterPx(
enc(
`<!-- <svg width="4096" height="4096"> -->${svg(32, 32, 'data-x="a>b"')}`,
),
),
32,
'the root, past comments and quoted >',
);
assert.equal(svgRasterPx(enc(svg(1024, 1024))), 256, 'at most 256 px');
assert.equal(svgRasterPx(enc(svg('48px', 30))), 48, 'px, the larger side');
assert.equal(svgRasterPx(enc(svg('50%', 30))), null, 'not pixels');
assert.equal(
svgRasterPx(enc('<svg xmlns="http://www.w3.org/2000/svg"/>')),
null,
'no size',
);
assert.equal(svgRasterPx('ship.png'), null);
assert.equal(svgRasterPx('data:image/svg+xml;base64,@@@'), null);
const { part, draw } = drawOver();
const set = draw.icons({ id: 'gpu:raster' });
const far = b64(svg(64, 64));
const near = b64(svg(192, 192));
set.set([
icon('a', {
image: far,
size: [20, 20],
imageLod: { near, switchPx: 30 },
motion: { kind: 'hold' },
}),
icon('b', {
image: 'gpu-raster.png',
size: [24, 18],
motion: { kind: 'hold' },
}),
icon('c', {
image: { svg: svg(40, 40) },
size: [20, 20],
motion: { kind: 'hold' },
}),
]);
const { set: rowsOf } = rows(part, 'gpu:raster');
const a = rowsOf.row('a');
assert.ok(iconAtlas.has(far, 64) && iconAtlas.has(near, 192));
assert.equal(rowsOf.imageFar(a), iconAtlas.slot(far, 64), 'far at 64 px');
assert.equal(rowsOf.imageNear(a), iconAtlas.slot(near, 192), 'near at 192');
assert.equal(rowsOf.width(a), 20, 'drawn at its size');
assert.equal(
rowsOf.imageFar(rowsOf.row('b')),
iconAtlas.slot('gpu-raster.png', 24),
'a URL at the item’s larger side',
);
assert.ok(
iconAtlas.has(
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg(40, 40))}`,
40,
),
'an svg ref at its own',
);
});