diff --git a/src/render/cesium/gpuIcons.js b/src/render/cesium/gpuIcons.js index 3cb421e3d..58dd61828 100644 --- a/src/render/cesium/gpuIcons.js +++ b/src/render/cesium/gpuIcons.js @@ -16,8 +16,10 @@ * `fadeByDistance`, `displayRange`, pixel and eye offsets, anchors other * than the centre, axis and screen rotations, opaque and additive blends, * and images other than URLs, SVG and atlas slots of those. Their images - * are rasterised square, at the item's larger side, into the icon atlas - * every GPU icon set shares, and refused when it has no room left for them. + * are rasterised square into the icon atlas every GPU icon set shares, and + * refused when it has no room left for them: an SVG at its own size (its + * root's larger `width`/`height`, as a billboard rasterises it), any other + * image at the item's larger side. * * Choices the contract leaves open, taken conservatively: * - `horizonCull` judges each row no lower than `ICON_CULL_HEIGHT_M` above @@ -104,8 +106,62 @@ function sameFields(a, b) { /** Whether two `motionOf`s write the same segment. */ const sameMotion = (a, b) => Boolean(a) && sameFields(a, b); -/** The square raster size an item's images take in the atlas. */ -function rasterPxOf(item) { +/** SVG URLs' own raster sizes (px; null: none given), as parsed. */ +const svgSizes = new Map(); +const SVG_SIZES_KEPT = 128; +/** + * The largest an SVG is rasterised at its own size (px): past it, at this, + * so an icon exported at 1024 px still packs. + */ +export const MAX_OWN_RASTER_PX = 256; + +/** + * An SVG data URL's own raster size: the larger of its root's `width` and + * `height` in pixels (unitless or `px`), at most `MAX_OWN_RASTER_PX`, or null + * when it gives no such size. + */ +export function svgRasterPx(url) { + if (!/^data:image\/svg\+xml[;,]/i.test(url)) return null; + let px = svgSizes.get(url); + if (px !== undefined) return px; + px = null; + try { + const comma = url.indexOf(','); + const body = url.slice(comma + 1); + const text = /;base64$/i.test(url.slice(0, comma)) + ? atob(body) + : decodeURIComponent(body); + // The root tag, comments aside, quoted values whole. + const root = + /"']|"[^"]*"|'[^']*')*>/i.exec( + text.replace(//g, ''), + )?.[0] ?? ''; + const side = (name) => { + const m = new RegExp( + `\\s${name}\\s*=\\s*["']\\s*([0-9]*\\.?[0-9]+)\\s*(px)?\\s*["']`, + 'i', + ).exec(root); + return m ? Number(m[1]) : NaN; + }; + const width = side('width'); + const height = side('height'); + if (width > 0 && height > 0) + px = Math.min( + MAX_OWN_RASTER_PX, + Math.max(1, Math.round(Math.max(width, height))), + ); + } catch { + // Not decodable: drawn at the item's size. + } + if (svgSizes.size >= SVG_SIZES_KEPT) svgSizes.clear(); + svgSizes.set(url, px); + return px; +} + +/** The square raster size an image (its atlas URL) takes for an item. */ +function rasterPxOf(url, item) { + const own = url === null ? null : svgRasterPx(url); + if (own !== null) return own; const [width = 0, height = 0] = item.size ?? []; return Math.max(1, Math.round(Math.max(width, height))); } @@ -184,7 +240,7 @@ export function createGpuIcons({ ) refuse('images but URLs and SVG'); const url = imageUrl(image); - const px = rasterPxOf(item); + const px = rasterPxOf(url, item); if (url !== null && !iconAtlas.has(url, px)) fresh.add(`${px}|${url}`); } } @@ -260,12 +316,12 @@ export function createGpuIcons({ // Written in full on the first write. let fresh = true; - /** An image as its atlas slot, or -1 while it is not registered. */ - function slotOf(image, sizePx) { + /** An item's image as its atlas slot, or -1 while it is not registered. */ + function slotOf(image, item) { const resolved = imageOf(record, image); if (resolved === undefined) return -1; const url = imageUrl(resolved); - if (url !== null) return iconAtlas.slot(url, sizePx); + if (url !== null) return iconAtlas.slot(url, rasterPxOf(url, item)); throw new Error( "the Cesium renderer's GPU icons have no images but URLs and SVG", ); @@ -334,9 +390,8 @@ export function createGpuIcons({ ? null : iconCourse(drawn.get(item.id), now); const [width = 0, height = 0] = item.size ?? []; - const rasterPx = rasterPxOf(item); - const far = slotOf(item.image, rasterPx); - const near = item.imageLod ? slotOf(item.imageLod.near, rasterPx) : far; + const far = slotOf(item.image, item); + const near = item.imageLod ? slotOf(item.imageLod.near, item) : far; set.setImage(row, far, near); // A switch at 0 px or less is near at any size. set.setImageSwitch( diff --git a/src/render/cesium/gpuIcons.test.mjs b/src/render/cesium/gpuIcons.test.mjs index ef293acd5..cb6bcf534 100644 --- a/src/render/cesium/gpuIcons.test.mjs +++ b/src/render/cesium/gpuIcons.test.mjs @@ -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 = '') => + ``; + 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(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('')), + 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', + ); +});