This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtile_renderer.ts
711 lines (608 loc) · 19.8 KB
/
tile_renderer.ts
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import type { Document, TileRenderData } from "./soffice";
const NOT_CHROMIUM = navigator.userAgent.indexOf('Chrome/') === -1;
type Rect = [
/** x */ x: number,
/** y */ y: number,
/** w */ w: number,
/** h */ h: number,
];
const RECT_SIZE = 4;
const MAX_PAINTED_TILES_PER_ITER = 32;
type TileIndexRange = [start: number, endInclusive: number];
const DEBUG = false;
/** 15 = 1440 twips-per-inch / 96 dpi */
const LOK_INTERNAL_TWIPS_TO_PX = 15;
// At 256px, enough for 2 8K displays, about 260 MB of video memory
const POOL_SIZE = 2000;
export function renderer(doc: Document) {
let d: TileRenderData;
let activeCanvas: OffscreenCanvas;
let ctx: OffscreenCanvasRenderingContext2D;
let canvases: [OffscreenCanvas];
let activeCanvasIndex: number = 0;
let tileRingIndexToTime: Map<number, number> = new Map();
/** contains a set of all the valid tile indices */
const validTiles: Set<number> = new Set();
/** maps a tile index to a tileRing index */
const tileIndexToTileRingIndex: Map<number, number> = new Map();
/** maps a tileRing index to a tile index */
const tileRingIndexToTileIndex: Map<number, number> = new Map();
/** a ring buffer that shouldn't allocate more than POOL_SIZE */
let tileRing: Map<number, ImageData> = new Map();
/** contains a set of all visible tile ring indices */
let visibleRingTiles: Set<number> = new Set();
/** ring index that wraps on POOL_SIZE */
let tileRingIndex = 0;
let tileDimTwips: number;
let docWidthTwips: number;
let docHeightTwips: number;
/** the number of tiles that make up a horizontal stride for the width of the document */
let widthTileStride: number;
let renderedTopTwips: number = -1;
let renderedHeightTwips: number = -1;
let scheduledTopTwips: number = -1;
let scheduledHeightTwips: number = -1;
let scaledTwips: number = LOK_INTERNAL_TWIPS_TO_PX;
let scale: number = 1;
let dpi: number = 1;
let scheduledWidthPx: number = -1;
let scheduledHeightPx: number = -1;
let didScroll = false;
let firstPaint = true;
const visibleInvalidations: Rect[] = [];
const nonVisibleInvalidations: Rect[] = [];
let zoomResetTimeout: number | undefined;
function scroll(/** scroll top in pixels */ y: number) {
// scroll
if (!activeCanvas) return;
// even though technically data.y is in css pixels
// we need to treat it as physical pixels because
// it represents the amount we have scrolled through the canvas
// document which is rendered in physical pixels
scheduledTopTwips = pxToTwips(y);
if (scheduledTopTwips !== renderedTopTwips) {
didScroll = true;
activeCanvas = canvases[activeCanvasIndex];
Atomics.store(d.isVisibleAreaPainted, 0, 0);
}
}
function resizeHeight(/** height */ h: number) {
if (!activeCanvas) return;
scheduledHeightPx = cssPxToPx(h, dpi);
scheduledHeightTwips = pxToTwips(h);
Atomics.store(d.scrollHeightTwips, 0, scheduledHeightTwips);
const newHeight = Math.floor(scheduledHeightTwips / tileDimTwips);
if (newHeight !== renderedHeightTwips) {
Atomics.store(d.isVisibleAreaPainted, 0, 0);
}
}
// FIXME: should 'w' be unused?
function resizeWidth(/** width */ w: number) {
let newDocWidthTwips = Atomics.load(d.docWidthTwips, 0);
// A change in the width of the document most likely requires a full reset
if (newDocWidthTwips !== docWidthTwips) {
docWidthTwips = newDocWidthTwips;
widthTileStride = Math.ceil(docWidthTwips / tileDimTwips);
Atomics.store(d.widthTileStride, 0, widthTileStride);
scheduledWidthPx = twipsToPx(docWidthTwips, dpi);
Atomics.store(d.isVisibleAreaPainted, 0, 0);
}
}
function handleZoom(data: {
/** absolute scale */
s: number;
/** dpi */
d: number;
}) {
if (!activeCanvas) return;
// Clear the previously scheduled zoom reset
if (zoomResetTimeout) clearTimeout(zoomResetTimeout);
zoom(data.s, data.d);
}
/** Converts a CSS pixel to twips */
function cssPxToTwips(px: number, dpi: number): number {
return (px * scaledTwips) / dpi;
}
/** Converts a physical pixel to twips */
function pxToTwips(px: number): number {
return px * scaledTwips;
}
/** Converts a CSS pixel to a physical pixel */
function cssPxToPx(px: number, dpi: number): number {
return px * dpi;
}
/** Converts twips to physical pixels */
function twipsToPx(twips: number, dpi: number): number {
return (twips / scaledTwips) * dpi;
}
function getContext(canvas: OffscreenCanvas) {
let ctx = canvas.getContext("2d");
// default is true, which makes things blurry
//https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
ctx.imageSmoothingEnabled = false;
return ctx;
}
function zoom(in_scale: number, in_dpi: number) {
console.log('zoom', in_scale, in_dpi);
docWidthTwips = Atomics.load(d.docWidthTwips, 0);
docHeightTwips = Atomics.load(d.docHeightTwips, 0);
scaledTwips =
clipToNearest8PxZoom(d.tileSize, 1 / in_scale) * LOK_INTERNAL_TWIPS_TO_PX;
tileDimTwips = Math.ceil(cssPxToTwips(d.tileSize, dpi));
widthTileStride = Math.ceil(docWidthTwips / tileDimTwips);
Atomics.store(d.widthTileStride, 0, widthTileStride);
Atomics.store(d.tileDimTwips, 0, tileDimTwips);
// This is in physical pixels
scheduledHeightPx = (activeCanvas.height * in_dpi) / dpi;
scheduledHeightTwips = pxToTwips(activeCanvas.height);
Atomics.store(d.scrollHeightTwips, 0, scheduledHeightTwips);
Atomics.store(d.pendingFullPaint, 0, 1);
scheduledWidthPx = twipsToPx(docWidthTwips, in_dpi);
scale = in_scale;
dpi = in_dpi;
}
function initialize(data: {
c: [OffscreenCanvas];
d: TileRenderData;
/** absolute scale */
s: number;
/** top position in pixels */
y: number;
/** dpi */
dpi: number;
}) {
d = data.d;
canvases = data.c;
activeCanvas = data.c[0];
ctx = getContext(activeCanvas);
ctx.clearRect(0, 0, activeCanvas.width, activeCanvas.height);
scale = data.s;
dpi = data.dpi;
zoom(data.s, dpi);
scheduledTopTwips = cssPxToTwips(data.y, dpi);
}
function prepareForRendering() {
const visibleTop = scheduledTopTwips;
const visibleHeight = scheduledHeightTwips;
const rangesToRender = rectToTileIndexRanges([
0,
visibleTop,
docWidthTwips,
visibleHeight,
]);
if (renderedHeightTwips !== scheduledHeightTwips) {
canvases[0].height = scheduledHeightPx;
}
if (activeCanvas.width !== scheduledWidthPx) {
canvases[0].width = scheduledWidthPx;
}
renderedHeightTwips = visibleHeight;
renderedTopTwips = visibleTop;
return rangesToRender;
}
// NOTE: strictly speaking, this should never need to paint anymore, it's just a catch-all
// precondition: prepareForRendering must be run prior
/**
* @returns true if needs render, false otherwise
*/
function paintMissingVisibleTileRanges(rangesToRender: TileIndexRange[]) {
let needsRender = false;
for (let y = 0; y < rangesToRender.length && !shouldPausePaint(); ++y) {
const [start, endInclusive] = rangesToRender[y];
let runStart: number | null = null;
for (let x = start; x <= endInclusive; ++x) {
const hasImage = tileRing.has(tileIndexToTileRingIndex.get(x));
if (hasImage) {
// range has ended
if (runStart != null) {
batchedBlockingPaintTiles(runStart, x - 1, visibleRingTiles);
runStart = null;
needsRender = true;
}
} else {
if (runStart != null) runStart = x;
}
}
// remaining ranges
if (runStart != null) {
batchedBlockingPaintTiles(runStart, endInclusive, visibleRingTiles);
needsRender = true;
}
}
return needsRender;
}
// paints visible tiles and transfer to
function paintAndRender(): boolean {
const invalidations = removeContainedAdjacentRects(drainInvalidations());
let needsRender = hasUpdatedVisibleArea();
const rangesToPaint = prepareForRendering();
const visibleTop = renderedTopTwips;
const visibleHeight = renderedHeightTwips;
if (Atomics.load(d.pendingFullPaint, 0) === 1) {
// clear invalidations, because the whole area will be repainted
visibleInvalidations.length = 0;
nonVisibleInvalidations.length = 0;
const newVisibleRingTiles = new Set<number>();
// all tiles are invalid
validTiles.clear();
// effectively paints by rows of tiles, so there isn't any odd-looking tearing if painting is paused
for (let y = 0; y < rangesToPaint.length && !shouldPausePaint(); ++y) {
const [start, endInclusive] = rangesToPaint[y];
batchedBlockingPaintTiles(start, endInclusive, newVisibleRingTiles);
}
visibleRingTiles = newVisibleRingTiles;
// FIXME: This doesn't work correctly anymore
// On Chrome: causes an infinite scroll loop, jumps back to top
// On FF/Safari: just clears the whole area
//
// clearNonVisibleTiles();
needsRender = true;
} else if (invalidations.length != 0 || didScroll) {
// rebalance visible and non-visible
invalidations.push(...visibleInvalidations);
invalidations.push(...nonVisibleInvalidations);
visibleInvalidations.length = 0;
nonVisibleInvalidations.length = 0;
for (const invalidation of invalidations) {
commitVisibleAndNonVisible(invalidation, visibleTop, visibleHeight);
}
const newVisibleRingTiles = new Set<number>();
if (visibleInvalidations.length != 0) {
const tileRangesToPaint = [];
// first do a pass and mark the invalid tiles, to prevent uncessary work by invalidating/painting overlapping areas
for (const visibleInvalidation of visibleInvalidations) {
const rangesToPaint = rectToTileIndexRanges(visibleInvalidation);
markInvalid(rangesToPaint);
tileRangesToPaint.push(rangesToPaint);
}
for (const rangesToPaint of tileRangesToPaint) {
// effectively paints by rows of tiles, so there isn't any odd-looking tearing if painting is paused
for (
let y = 0;
y < rangesToPaint.length && !shouldPausePaint();
++y
) {
const [start, endInclusive] = rangesToPaint[y];
batchedBlockingPaintTiles(start, endInclusive, newVisibleRingTiles);
needsRender = true;
}
}
}
const visibleRangesToPaint = rectToTileIndexRanges([
0,
visibleTop,
docWidthTwips,
visibleHeight,
]);
for (
let y = 0;
y < visibleRangesToPaint.length && !shouldPausePaint();
++y
) {
const [start, endInclusive] = visibleRangesToPaint[y];
let runStart: number | null = null;
for (let x = start; x <= endInclusive; ++x) {
const ringIndex = tileIndexToTileRingIndex.get(x);
const valid =
ringIndex != null &&
validTiles.has(x) &&
tileRingIndexToTileIndex.get(ringIndex) === x;
if (valid) {
newVisibleRingTiles.add(ringIndex);
// the range is closed, paint the batch and reset the run
if (runStart != null) {
runStart = null;
needsRender = true;
batchedBlockingPaintTiles(runStart, x - 1, newVisibleRingTiles);
}
} else if (runStart == null) {
runStart = x;
}
}
// close last range if there's one open
if (runStart != null) {
needsRender = true;
batchedBlockingPaintTiles(
runStart,
endInclusive,
newVisibleRingTiles,
);
}
}
visibleInvalidations.length = 0;
visibleRingTiles.clear();
visibleRingTiles = newVisibleRingTiles;
}
needsRender ||= paintMissingVisibleTileRanges(rangesToPaint);
// TODO: figure out why Firefox/Safari don't persist frames and
// requires a new render every frame?
if (NOT_CHROMIUM || needsRender || hasUpdatedVisibleArea()) {
render(rangesToPaint);
}
return needsRender;
}
function hasUpdatedVisibleArea(): boolean {
return (
scheduledTopTwips !== renderedTopTwips ||
scheduledHeightTwips !== renderedHeightTwips
);
}
// renders the visible GPU textures to the canvas
function render(rangesToRender: TileIndexRange[]) {
const offset = twipsToPx(renderedTopTwips % tileDimTwips, dpi);
for (let y = 0; y < rangesToRender.length && !shouldPausePaint(); ++y) {
const [start, endInclusive] = rangesToRender[y];
for (let x = start; x <= endInclusive; ++x) {
const [xCoord] = tileIndexToGridCoord(x);
const img: ImageData = tileRing.get(tileIndexToTileRingIndex.get(x));
if (!img) {
console.error('missing', x, y);
continue;
}
const dstX: number = xCoord * d.tileSize;
const dstY: number = y * d.tileSize - offset;
ctx.beginPath();
ctx.clearRect(dstX, dstY, d.tileSize, d.tileSize);
ctx.putImageData(DEBUG ? addBorder(img) : img, dstX, dstY);
if (DEBUG) {
let timestamp = tileRingIndexToTime.get(tileIndexToTileRingIndex.get(x));
ctx.font = "12px Arial";
ctx.fillStyle = "blue";
ctx.fillText(
`${timestamp} (${xCoord}, ${y}) ${x}`,
dstX + 5,
dstY + 15,
);
}
ctx.closePath();
}
}
Atomics.store(d.isVisibleAreaPainted, 0, 1);
Atomics.store(d.pendingFullPaint, 0, 0);
didScroll = false;
firstPaint = false;
}
function shouldPausePaint(): boolean {
return (
(scheduledTopTwips !== renderedTopTwips && renderedTopTwips !== -1) ||
(scheduledHeightTwips !== renderedHeightTwips &&
renderedHeightTwips !== -1)
);
}
function commitVisibleAndNonVisible(rect: Rect, y: number, h: number) {
const [rx, ry, rw, rh] = rect;
const b = y + h;
const rb = ry + rh;
// Calculate the overlap
const overlapTop = Math.max(ry, y);
const overlapBottom = Math.min(rb, b);
// Check if no overlap
if (overlapTop >= overlapBottom) {
nonVisibleInvalidations.push(rect);
return;
}
visibleInvalidations.push([rx, overlapTop, rw, overlapBottom - overlapTop]);
// Above the overlap
if (ry < overlapTop) {
nonVisibleInvalidations.push([rx, ry, rw, overlapTop - ry]);
}
// Below the overlap
if (rb > overlapBottom) {
nonVisibleInvalidations.push([rx, overlapBottom, rw, rb - overlapBottom]);
}
}
function hasInvalidations(): boolean {
return Atomics.load(d.hasInvalidations, 0) === 1;
}
// TODO: figure out why Collabora disabled their time limit for draining events
function drainInvalidations(): Rect[] {
const result: Rect[] = [];
let invalidation: Rect | undefined;
do {
invalidation = takeInvalidation();
if (invalidation != null) result.push(invalidation);
} while (invalidation != null);
Atomics.store(d.hasInvalidations, 0, 0);
return result;
}
// Operates on a fixed-size thread-safe stack, JS-side is always SEQ_CST so there's some perf loss
function takeInvalidation(): Rect | undefined {
if (!hasInvalidations()) return undefined;
const head = Atomics.load(d.invalidationStackHead, 0);
if (head < 0) return undefined;
const result = new Array<number>(RECT_SIZE) as Rect;
const subarrayPos = head * RECT_SIZE;
const invalidRect = d.invalidationStack.subarray(
subarrayPos,
subarrayPos + RECT_SIZE,
);
for (let i = 0; i < RECT_SIZE; ++i) {
result[i] = Atomics.load(invalidRect, i);
}
const newHead = head - 1;
Atomics.store(d.invalidationStackHead, 0, newHead);
if (newHead < 0) {
Atomics.store(d.hasInvalidations, 0, 1);
Atomics.notify(d.hasInvalidations, 0);
}
return result;
}
function rectToTileIndexRanges(rect: Rect): TileIndexRange[] {
const [x,y,w,h] = rect;
const result: TileIndexRange[] = [];
const r = x + w;
const x0 = Math.floor(x / tileDimTwips);
const x1 = Math.floor(r / tileDimTwips);
const b = Math.min(y + h, docHeightTwips);
const y0 = Math.floor(y / tileDimTwips);
const y1 = Math.floor(b / tileDimTwips);
for (let y = y0; y <= y1; ++y) {
result.push([x0 + widthTileStride * y, x1 + widthTileStride * y]);
}
return result;
}
function tileIndexToGridCoord(tileIndex: number): [x: number, y: number] {
const y = (tileIndex / widthTileStride) | 0;
const x = tileIndex % widthTileStride;
return [x, y];
}
/** sets the tileRingIndex to the next non-visible tile */
function seekNonVisibleRingIndex() {
const origin = tileRingIndex;
tileRingIndex = ((tileRingIndex + 1) | 0) % POOL_SIZE;
while (visibleRingTiles.has(tileRingIndex) && tileRingIndex != origin) {
tileRingIndex = ((tileRingIndex + 1) | 0) % POOL_SIZE;
}
if (tileRingIndex === origin) {
console.error("Texture pool too small for visible tiles");
}
}
function blockingPaintTiles(
startTileIndex: number,
endTileIndex: number,
ringIndexSet: Set<number>,
): void {
const startTime = Date.now();
const tileAllocSize =
(d.paintedTiles.byteLength / MAX_PAINTED_TILES_PER_ITER) | 0;
doc.paintTiles(d.viewId, startTileIndex, endTileIndex);
for (
let tileIndex = startTileIndex;
tileIndex <= endTileIndex;
tileIndex++
) {
const byteOffset = tileAllocSize * (tileIndex - startTileIndex);
seekNonVisibleRingIndex();
tileRingIndexToTileIndex.set(tileRingIndex, tileIndex);
tileIndexToTileRingIndex.set(tileIndex, tileRingIndex);
tileRingIndexToTime.set(tileRingIndex, Date.now() - startTime);
tileRing.set(
tileRingIndex,
new ImageData(
new Uint8ClampedArray(d.paintedTiles.slice(byteOffset, byteOffset + tileAllocSize)),
d.tileSize,
d.tileSize,
),
);
validTiles.add(tileIndex);
ringIndexSet.add(tileRingIndex);
}
}
function batchedBlockingPaintTiles(
start: number,
endInclusive: number,
ringIndexSet: Set<number>,
): void {
for (
let batchStart = start;
batchStart <= endInclusive;
batchStart += MAX_PAINTED_TILES_PER_ITER
) {
const batchEnd = Math.min(
batchStart + MAX_PAINTED_TILES_PER_ITER - 1,
endInclusive,
);
blockingPaintTiles(batchStart, batchEnd, ringIndexSet);
}
}
function markInvalid(ranges: TileIndexRange[]) {
for (const range of ranges) {
const [start, endInclusive] = range;
for (let i = start; i <= endInclusive; ++i) {
validTiles.delete(i);
tileIndexToTileRingIndex.delete(i);
}
}
}
// FIXME: this is clearing visible tiles
function clearNonVisibleTiles() {
const indices = tileRing.keys();
for (const tileRingIndex of indices) {
// but visible tiles shouldn't be cleared so there is something to paint
if (visibleRingTiles.has(tileRingIndex)) continue;
tileIndexToTileRingIndex.delete(
tileRingIndexToTileIndex.get(tileRingIndex),
);
tileRing.delete(tileRingIndex);
tileRingIndexToTileIndex.delete(tileRingIndex);
}
}
function removeContainedAdjacentRects(rects: Rect[]): Rect[] {
const result: Rect[] = [];
for (let i = 0; i < rects.length; i++) {
if (
i === 0 ||
!(
isContainedOrEqual(rects[i], rects[i - 1]) &&
isContainedOrEqual(rects[i], result.at(-1))
)
) {
result.push(rects[i]);
}
}
return result;
}
return {
initialize,
scroll,
resizeHeight,
resizeWidth,
zoom: handleZoom,
paintAndRender
};
}
function addBorder(imageData: ImageData) {
const width = imageData.width;
const height = imageData.height;
const data = imageData.data;
const red = 0;
const green = 0;
const blue = 255;
const alpha = 255;
for (let x = 0; x < width; x++) {
const index = (x + 0 * width) * 4;
data[index] = red;
data[index + 1] = green;
data[index + 2] = blue;
data[index + 3] = alpha;
}
for (let x = 0; x < width; x++) {
const index = (x + (height - 1) * width) * 4;
data[index] = red;
data[index + 1] = green;
data[index + 2] = blue;
data[index + 3] = alpha;
}
for (let y = 0; y < height; y++) {
const index = (0 + y * width) * 4;
data[index] = red;
data[index + 1] = green;
data[index + 2] = blue;
data[index + 3] = alpha;
}
for (let y = 0; y < height; y++) {
const index = (width - 1 + y * width) * 4;
data[index] = red;
data[index + 1] = green;
data[index + 2] = blue;
data[index + 3] = alpha;
}
return imageData;
}
function isContainedOrEqual(a: Rect, b: Rect): boolean {
return (
a[0] >= b[0] &&
a[1] >= b[1] &&
a[0] + a[2] <= b[0] + b[2] &&
a[1] + a[3] <= b[1] + b[3]
);
}
export function clipToNearest8PxZoom(w: number, s: number): number {
const scaledWidth: number = Math.ceil(w * s);
const mod: number = scaledWidth % 8;
if (mod === 0) return s;
return Math.abs((scaledWidth - mod) / w - s) <
Math.abs((scaledWidth + 8 - mod) / w - s)
? (scaledWidth - mod) / w
: (scaledWidth + 8 - mod) / w;
}