File size: 11,859 Bytes
f61bac1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | // SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
deviceResidualCss, snapContainerToDeviceGrid, snapTileContainers,
installDeviceGridSnapping,
} from '../../src/device-pixels.js';
// A tile boundary as the browser sees it, in device pixels. Whole => the two
// tiles either side abut on a pixel edge; fractional => both antialias into the
// same pixel column and the pane background shows between them.
function boundaryDevicePx(tileCssLeft, tilesAlong, dpr, tileSize = 256) {
return (tileCssLeft + tilesAlong * tileSize) * dpr;
}
// On the grid to within floating-point exactness, which is the real guarantee:
// the correction is arithmetic on doubles, so it lands within an epsilon of a
// whole device pixel rather than exactly on one. A tenth of a nanopixel is not
// a seam.
function assertOnDeviceGrid(devicePx, message) {
assert.ok(Math.abs(devicePx - Math.round(devicePx)) < 1e-9,
`${message}: ${devicePx} is not a whole device pixel`);
}
// Stand-in for the DOM Leaflet builds: .leaflet-tile elements inside a
// .leaflet-tile-container (which Leaflet transforms) inside a .leaflet-layer
// (which it only ever sets a z-index on).
//
// `origin` is Leaflet's level.origin: _getTilePos places tile k at
// `k*tileSize - origin`, and origin is an arbitrary rounded integer, NOT a
// multiple of the tile size. Modelling it is the point — a container sitting
// exactly on the device grid with every tile inside it a quarter pixel off is
// precisely the case that has to be caught.
function fakeLayerDom(containerLeft, origin = 0, containerTop = 0) {
const holder = { style: {}, className: 'leaflet-layer' };
const container = {
className: 'leaflet-tile-container',
parentElement: holder,
leafletLeft: containerLeft,
leafletTop: containerTop,
origin,
querySelector(sel) {
assert.equal(sel, '.leaflet-tile');
return this.tile;
},
};
container.tile = { className: 'leaflet-tile', parentElement: container };
holder.child = container;
return { holder, container, tile: container.tile };
}
// Where an element actually renders: Leaflet's placement plus whatever
// correction currently sits on the layer holder. This is what
// getBoundingClientRect would report, and reproducing the real chain — holder
// correction, container transform, per-tile origin offset — is what makes these
// tests mean anything.
function renderedRect(el) {
const container = el.className === 'leaflet-tile' ? el.parentElement : el;
const holder = container.parentElement;
const tileOffset = el === container ? 0 : -container.origin;
return {
left: container.leafletLeft + tileOffset + (holder._orSnapDx || 0),
top: container.leafletTop + tileOffset + (holder._orSnapDy || 0),
};
}
describe('the seam this module exists to remove', () => {
it('puts tile boundaries off the device grid at dpr 1.25', () => {
// Leaflet rounds its offsets to whole CSS px, which at 1.25 is a
// quarter pixel off the device grid for three of every four offsets.
const fractional = [37, 38, 39].filter(
(left) => !Number.isInteger(boundaryDevicePx(left, 3, 1.25)));
assert.deepEqual(fractional, [37, 38, 39]);
// 40 % 4 === 0, the one offset in four that already lands on the grid.
assert.ok(Number.isInteger(boundaryDevicePx(40, 3, 1.25)));
});
it('lands every boundary on the grid once the layer is corrected', () => {
// Every combination of container placement and level.origin: the
// container alone was not enough, because the origin term is applied
// to the tiles inside it.
for (const left of [37, 38, 39, 40, -13, 0]) {
for (const origin of [0, 1, 2, 3, 40, 8123, -57]) {
const { container, tile } = fakeLayerDom(left, origin);
snapContainerToDeviceGrid(container, 1.25, renderedRect);
const corrected = renderedRect(tile).left;
for (const along of [0, 1, 2, 17]) {
assertOnDeviceGrid(
boundaryDevicePx(corrected, along, 1.25),
`left ${left}, origin ${origin}, tile ${along}`);
}
}
}
});
it('holds for every ratio a display reports, not just 1.25', () => {
// 256*dpr is whole for all of these, so correcting the container is the
// whole fix; the tile's own 256 CSS px box is already whole-device.
for (const dpr of [1, 1.25, 1.5, 1.75, 2, 2.5, 3]) {
assert.ok(Number.isInteger(256 * dpr), `256*${dpr} must be whole`);
const { container, tile } = fakeLayerDom(37.4, 813, -8.9);
snapContainerToDeviceGrid(container, dpr, renderedRect);
const rect = renderedRect(tile);
assertOnDeviceGrid(boundaryDevicePx(rect.left, 5, dpr), `x @${dpr}`);
assertOnDeviceGrid(boundaryDevicePx(rect.top, 5, dpr), `y @${dpr}`);
}
});
});
describe('deviceResidualCss', () => {
it('is less than half a device pixel, and signed', () => {
for (const dpr of [1.25, 1.5, 2, 3]) {
for (const pos of [0, 0.4, 12.7, -3.3, 1000.9]) {
const residual = deviceResidualCss(pos, dpr);
assert.ok(Math.abs(residual) * dpr <= 0.5 + 1e-9,
`dpr ${dpr}, pos ${pos} -> ${residual}`);
assert.ok(Number.isInteger(
Math.round((pos - residual) * dpr * 1e6) / 1e6));
}
}
});
it('is zero for a position already on the grid', () => {
assert.equal(deviceResidualCss(12, 1), 0);
assert.equal(deviceResidualCss(40, 1.25), 0);
assert.equal(deviceResidualCss(0, 1.25), 0);
});
it('is zero for input it cannot use', () => {
assert.equal(deviceResidualCss(NaN, 2), 0);
assert.equal(deviceResidualCss(10, 0), 0);
assert.equal(deviceResidualCss(10, -1), 0);
});
});
describe('snapContainerToDeviceGrid', () => {
// The bug this whole module had in its first draft: correcting the element
// Leaflet itself transforms would be overwritten on the next pan. Leaflet
// owns .leaflet-tile-container; only .leaflet-layer is free.
it('writes the correction on the layer, never on the container', () => {
const { holder, container } = fakeLayerDom(37);
snapContainerToDeviceGrid(container, 1.25, renderedRect);
assert.ok(holder.style.transform.startsWith('translate3d('));
assert.equal(container.style, undefined);
});
it('measures a tile, not the container', () => {
// The container can be exactly on the grid while its tiles are not:
// level.origin is applied per tile, inside the container.
const { container, tile } = fakeLayerDom(40, 3); // 40*1.25 is whole
assert.ok(Number.isInteger(40 * 1.25));
snapContainerToDeviceGrid(container, 1.25, renderedRect);
assertOnDeviceGrid(renderedRect(tile).left * 1.25, 'tile');
});
it('does nothing for a container with no tiles yet', () => {
const { holder, container } = fakeLayerDom(37);
container.tile = null;
assert.equal(snapContainerToDeviceGrid(container, 1.25, renderedRect),
null);
assert.equal(holder.style.transform, undefined);
});
it('converges in one step and does not drift when re-run', () => {
const { holder, container } = fakeLayerDom(37, 813);
const first = snapContainerToDeviceGrid(container, 1.25, renderedRect);
const applied = holder.style.transform;
for (let i = 0; i < 5; i++) {
const again = snapContainerToDeviceGrid(container, 1.25,
renderedRect);
assert.equal(again.dx, first.dx);
assert.equal(holder.style.transform, applied);
}
});
it('follows Leaflet when it moves the container', () => {
const { container, tile } = fakeLayerDom(37, 813);
snapContainerToDeviceGrid(container, 1.25, renderedRect);
container.leafletLeft = 38; // a one-CSS-pixel pan
snapContainerToDeviceGrid(container, 1.25, renderedRect);
assertOnDeviceGrid(renderedRect(tile).left * 1.25, 'after pan');
});
it('clears the transform when nothing needs correcting', () => {
// 40 and origin 0: container and tiles both already on the grid.
const { holder, container } = fakeLayerDom(40, 0);
snapContainerToDeviceGrid(container, 1.25, renderedRect);
assert.equal(holder.style.transform, '');
});
it('does nothing for a container with no parent', () => {
assert.equal(snapContainerToDeviceGrid(null, 1.25, renderedRect), null);
assert.equal(
snapContainerToDeviceGrid({ parentElement: null }, 1.25,
renderedRect),
null);
});
});
describe('snapTileContainers', () => {
function fakeMap(lefts) {
const doms = lefts.map((left, i) => fakeLayerDom(left, 800 + i));
return {
doms,
getPane(name) {
assert.equal(name, 'tilePane');
return { querySelectorAll: () => doms.map((d) => d.container) };
},
};
}
it('corrects every tile layer on the map', () => {
const map = fakeMap([37, 38, 39]);
assert.equal(snapTileContainers(map, 1.25, renderedRect), 3);
for (const { tile } of map.doms) {
assertOnDeviceGrid(renderedRect(tile).left * 1.25, 'layer');
}
});
it('skips the work entirely at an integer ratio', () => {
// Whole CSS px are whole device px there, so every residual is zero;
// this runs on each frame of a drag and should cost nothing.
const map = fakeMap([37, 38]);
for (const dpr of [1, 2, 3]) {
assert.equal(snapTileContainers(map, dpr, renderedRect), 0);
assert.equal(map.doms[0].holder.style.transform, undefined);
}
});
it('survives a map with no tile pane yet', () => {
assert.equal(snapTileContainers(null, 1.25, renderedRect), 0);
assert.equal(snapTileContainers({}, 1.25, renderedRect), 0);
assert.equal(
snapTileContainers({ getPane: () => null }, 1.25, renderedRect), 0);
});
});
describe('installDeviceGridSnapping', () => {
it('re-snaps after a move, a zoom and a resize', () => {
const handlers = [];
const map = {
on(events, fn) { handlers.push({ events, fn }); },
getPane: () => null,
};
assert.equal(installDeviceGridSnapping(map, () => 1.25), true);
assert.equal(handlers.length, 1);
for (const event of ['move', 'zoomend', 'viewreset', 'resize']) {
assert.ok(handlers[0].events.split(' ').includes(event), event);
}
});
it('reads the ratio per call, so moving to another monitor is picked up',
() => {
const seen = [];
const map = { on() {}, getPane: () => { seen.push(1); return null; } };
let dpr = 1;
installDeviceGridSnapping(map, () => dpr);
assert.equal(seen.length, 0); // integer ratio: no measuring at all
dpr = 1.25;
installDeviceGridSnapping(map, () => dpr);
assert.equal(seen.length, 1);
});
it('declines a map it does not recognize', () => {
assert.equal(installDeviceGridSnapping(null), false);
assert.equal(installDeviceGridSnapping({}), false);
});
});
|