@react-x11/components 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/html/controls.d.ts +29 -2
  2. package/dist/html/controls.d.ts.map +1 -1
  3. package/dist/html/controls.js +70 -2
  4. package/dist/html/controls.js.map +1 -1
  5. package/dist/html/css/cascade.d.ts.map +1 -1
  6. package/dist/html/css/cascade.js +26 -4
  7. package/dist/html/css/cascade.js.map +1 -1
  8. package/dist/html/css/color.d.ts +18 -0
  9. package/dist/html/css/color.d.ts.map +1 -1
  10. package/dist/html/css/color.js +27 -0
  11. package/dist/html/css/color.js.map +1 -1
  12. package/dist/html/css/parse.d.ts +18 -4
  13. package/dist/html/css/parse.d.ts.map +1 -1
  14. package/dist/html/css/parse.js +66 -28
  15. package/dist/html/css/parse.js.map +1 -1
  16. package/dist/html/css/style.d.ts +128 -15
  17. package/dist/html/css/style.d.ts.map +1 -1
  18. package/dist/html/css/style.js +561 -144
  19. package/dist/html/css/style.js.map +1 -1
  20. package/dist/html/css/transform.d.ts +85 -0
  21. package/dist/html/css/transform.d.ts.map +1 -0
  22. package/dist/html/css/transform.js +425 -0
  23. package/dist/html/css/transform.js.map +1 -0
  24. package/dist/html/css/ua.d.ts.map +1 -1
  25. package/dist/html/css/ua.js +14 -2
  26. package/dist/html/css/ua.js.map +1 -1
  27. package/dist/html/css/values.d.ts +4 -0
  28. package/dist/html/css/values.d.ts.map +1 -1
  29. package/dist/html/css/values.js.map +1 -1
  30. package/dist/html/fonts.d.ts +55 -5
  31. package/dist/html/fonts.d.ts.map +1 -1
  32. package/dist/html/fonts.js +284 -23
  33. package/dist/html/fonts.js.map +1 -1
  34. package/dist/html/layout/axes.d.ts +14 -0
  35. package/dist/html/layout/axes.d.ts.map +1 -0
  36. package/dist/html/layout/axes.js +28 -0
  37. package/dist/html/layout/axes.js.map +1 -0
  38. package/dist/html/layout/block.d.ts +93 -12
  39. package/dist/html/layout/block.d.ts.map +1 -1
  40. package/dist/html/layout/block.js +389 -39
  41. package/dist/html/layout/block.js.map +1 -1
  42. package/dist/html/layout/boxes.d.ts +59 -1
  43. package/dist/html/layout/boxes.d.ts.map +1 -1
  44. package/dist/html/layout/boxes.js +22 -2
  45. package/dist/html/layout/boxes.js.map +1 -1
  46. package/dist/html/layout/cache.d.ts +1 -1
  47. package/dist/html/layout/cache.d.ts.map +1 -1
  48. package/dist/html/layout/cache.js +6 -0
  49. package/dist/html/layout/cache.js.map +1 -1
  50. package/dist/html/layout/css-grid.d.ts.map +1 -1
  51. package/dist/html/layout/css-grid.js +90 -18
  52. package/dist/html/layout/css-grid.js.map +1 -1
  53. package/dist/html/layout/flex.d.ts.map +1 -1
  54. package/dist/html/layout/flex.js +570 -83
  55. package/dist/html/layout/flex.js.map +1 -1
  56. package/dist/html/layout/inline.d.ts +3 -0
  57. package/dist/html/layout/inline.d.ts.map +1 -1
  58. package/dist/html/layout/inline.js +144 -54
  59. package/dist/html/layout/inline.js.map +1 -1
  60. package/dist/html/layout/multicol.d.ts +42 -0
  61. package/dist/html/layout/multicol.d.ts.map +1 -0
  62. package/dist/html/layout/multicol.js +897 -0
  63. package/dist/html/layout/multicol.js.map +1 -0
  64. package/dist/html/layout/shaping.d.ts +7 -0
  65. package/dist/html/layout/shaping.d.ts.map +1 -1
  66. package/dist/html/layout/shaping.js +25 -20
  67. package/dist/html/layout/shaping.js.map +1 -1
  68. package/dist/html/layout/table.d.ts.map +1 -1
  69. package/dist/html/layout/table.js +62 -18
  70. package/dist/html/layout/table.js.map +1 -1
  71. package/dist/html/node.d.ts +3 -0
  72. package/dist/html/node.d.ts.map +1 -1
  73. package/dist/html/node.js +263 -36
  74. package/dist/html/node.js.map +1 -1
  75. package/dist/html/paint.d.ts +79 -4
  76. package/dist/html/paint.d.ts.map +1 -1
  77. package/dist/html/paint.js +1213 -141
  78. package/dist/html/paint.js.map +1 -1
  79. package/dist/html/svg.d.ts +15 -2
  80. package/dist/html/svg.d.ts.map +1 -1
  81. package/dist/html/svg.js +249 -17
  82. package/dist/html/svg.js.map +1 -1
  83. package/dist/html/widgets.js +87 -7
  84. package/dist/html/widgets.js.map +1 -1
  85. package/dist/html/woff2.d.ts +14 -0
  86. package/dist/html/woff2.d.ts.map +1 -0
  87. package/dist/html/woff2.js +608 -0
  88. package/dist/html/woff2.js.map +1 -0
  89. package/dist/maps/controller.d.ts +17 -1
  90. package/dist/maps/controller.d.ts.map +1 -1
  91. package/dist/maps/controller.js +27 -7
  92. package/dist/maps/controller.js.map +1 -1
  93. package/dist/richtext/node.d.ts +8 -1
  94. package/dist/richtext/node.d.ts.map +1 -1
  95. package/dist/richtext/node.js +5 -1
  96. package/dist/richtext/node.js.map +1 -1
  97. package/dist/richtext/runs.d.ts +25 -6
  98. package/dist/richtext/runs.d.ts.map +1 -1
  99. package/dist/richtext/runs.js +240 -24
  100. package/dist/richtext/runs.js.map +1 -1
  101. package/package.json +1 -1
  102. package/src/html/controls.ts +85 -2
  103. package/src/html/css/cascade.ts +23 -0
  104. package/src/html/css/color.ts +29 -0
  105. package/src/html/css/parse.ts +71 -31
  106. package/src/html/css/style.ts +664 -147
  107. package/src/html/css/transform.ts +459 -0
  108. package/src/html/css/ua.ts +15 -2
  109. package/src/html/css/values.ts +4 -0
  110. package/src/html/fonts.ts +312 -29
  111. package/src/html/layout/axes.ts +62 -0
  112. package/src/html/layout/block.ts +444 -37
  113. package/src/html/layout/boxes.ts +65 -3
  114. package/src/html/layout/cache.ts +6 -0
  115. package/src/html/layout/css-grid.ts +95 -22
  116. package/src/html/layout/flex.ts +671 -77
  117. package/src/html/layout/inline.ts +149 -55
  118. package/src/html/layout/multicol.ts +977 -0
  119. package/src/html/layout/shaping.ts +27 -18
  120. package/src/html/layout/table.ts +62 -19
  121. package/src/html/node.ts +300 -37
  122. package/src/html/paint.ts +1406 -206
  123. package/src/html/svg.ts +277 -19
  124. package/src/html/widgets.ts +95 -9
  125. package/src/html/woff2.ts +612 -0
  126. package/src/maps/controller.ts +28 -7
  127. package/src/richtext/node.ts +13 -2
  128. package/src/richtext/runs.ts +313 -35
@@ -20,11 +20,13 @@
20
20
  // back on the laid-out lines is exactly what that module already knows how to
21
21
  // draw.
22
22
  import { canFill, lineBands, paintRunBackgrounds, paintRunRules, } from '../richtext/runs.js';
23
- import { alphaOf, inkColor, isTransparent, resolve } from './css/values.js';
24
- import { blend, borderShades } from './css/color.js';
23
+ import { AUTO, alphaOf, inkColor, isTransparent, resolve, } from './css/values.js';
24
+ import { SCHEME_COLORS, blend, borderShades } from './css/color.js';
25
25
  import { CONTAIN_LAYOUT, CONTAIN_PAINT, CONTAIN_SIZE, copyStyle, masked, scrolls, } from './css/style.js';
26
- import { contained } from './layout/block.js';
27
- import { BOX_RAISES, LINE_BOX_RAISES, PADDED_FACES, Box, CLAMPED, CLIPPED_CELLS, COLLAPSED_CELLS, FADED_BLOCKS, PAINT_ORDER, INLINE_OFFSETS, MOVED_OFF_LINES, SHADOWED_TEXT, SHIFTED_LINES, TEXT_RAISES, TEXT_SHIFTS, } from './layout/boxes.js';
26
+ import { LARGEST } from './css/calc.js';
27
+ import { invert, mapRect, transformed } from './css/transform.js';
28
+ import { contained, placedMatrix } from './layout/block.js';
29
+ import { BOX_RAISES, LINE_BOX_RAISES, PADDED_FACES, Box, CLAMPED, CLIPPED_CELLS, COLLAPSED_CELLS, COLUMN_LINES, COLUMN_PIECES, COLUMN_ROWS, columned, FADED_BLOCKS, PAINT_ORDER, INLINE_OFFSETS, MOVED_OFF_LINES, SHADOWED_TEXT, SHIFTED_LINES, TEXT_RAISES, TEXT_SHIFTS, } from './layout/boxes.js';
28
30
  import { CLIPPED_TEXT, clipBoxOf, depthOf, inklessLayout, layoutOffsetOf, layoutOffsets, } from './layout/inline.js';
29
31
  import { tableGrid } from './layout/grid.js';
30
32
  import { SvgDrawing, inlineDrawing } from './svg.js';
@@ -52,6 +54,24 @@ export const OUT_OF_FLOW_REACH = new WeakMap();
52
54
  * is inside them.
53
55
  */
54
56
  const LINE_INK = new WeakMap();
57
+ /**
58
+ * For a box painted through a matrix (`placedMatrix`), the bounds of what
59
+ * it and its descendants draw before the matrix is applied: what
60
+ * `boundsX`… would have been, which are where it puts them. The paint
61
+ * inside the box, and a point taken back into it, are in these.
62
+ */
63
+ const UNTRANSFORMED = new WeakMap();
64
+ /** The bounds of what a box painted through a matrix draws, in its own
65
+ * coordinates: asked of a box `placedMatrix` answers for, whose entry the
66
+ * last `computePaintBounds` wrote. */
67
+ export function ownBounds(box) {
68
+ return (UNTRANSFORMED.get(box) ?? {
69
+ x: box.boundsX,
70
+ y: box.boundsY,
71
+ width: box.boundsWidth,
72
+ height: box.boundsHeight,
73
+ });
74
+ }
55
75
  export function computePaintBounds(box, moved = false) {
56
76
  // A box with no rectangle of its own gives only what it holds: nothing,
57
77
  // until a child with bounds is met.
@@ -231,6 +251,40 @@ export function computePaintBounds(box, moved = false) {
231
251
  LINE_INK.set(lines, { above, below });
232
252
  else if (lines)
233
253
  LINE_INK.delete(lines);
254
+ // a `clip-path` cuts the box and all it holds: no ink past the path, and
255
+ // none where it leaves nothing — though the scrollable overflow is what
256
+ // it was, as a browser has it, which is why a page that hides a box with
257
+ // one makes it a pixel square as well
258
+ if (own && box.style.clipPath) {
259
+ const { rect } = clipPathOf(box, DOCUMENT);
260
+ x1 = Math.max(x1, rect.x);
261
+ y1 = Math.max(y1, rect.y);
262
+ x2 = Math.min(x2, rect.x + rect.w);
263
+ y2 = Math.min(y2, rect.y + rect.h);
264
+ if (!(x2 > x1 && y2 > y1))
265
+ x1 = Infinity;
266
+ }
267
+ // A box that turns, scales or skews draws what it holds through its
268
+ // matrix (`paintTransformed`): its ink is where that puts it, the
269
+ // rectangle around the one it would have filled, and the one it would
270
+ // have filled is kept for the paint inside it, which is in its own
271
+ // coordinates. One flattened to nothing draws nothing.
272
+ const matrix = own ? placedMatrix(box) : null;
273
+ if (matrix && x1 !== Infinity) {
274
+ UNTRANSFORMED.set(box, { x: x1, y: y1, width: x2 - x1, height: y2 - y1 });
275
+ if (invert(matrix)) {
276
+ // no further than a length reaches (`LARGEST`): a page's
277
+ // `scale(1e30)` is a box as large as a browser holds one, and no
278
+ // larger
279
+ const to = mapRect(matrix, x1, y1, x2 - x1, y2 - y1);
280
+ x1 = Math.max(to.x, -LARGEST);
281
+ y1 = Math.max(to.y, -LARGEST);
282
+ x2 = Math.min(to.x + to.width, LARGEST);
283
+ y2 = Math.min(to.y + to.height, LARGEST);
284
+ }
285
+ else
286
+ x1 = Infinity;
287
+ }
234
288
  if (x1 === Infinity) {
235
289
  // nothing to draw: no damage meets it, and no parent takes it in
236
290
  box.boundsX = Infinity;
@@ -255,13 +309,21 @@ export function computePaintBounds(box, moved = false) {
255
309
  // whether the box clips only matters where its content reaches past it,
256
310
  // and its style is one more object a walk of every box would read
257
311
  const end = empty ? -Infinity : box.y + box.height;
258
- if (bottom <= end)
259
- return end;
260
- const style = box.style;
261
- return box.parent &&
262
- (style.overflowX !== 'visible' || style.overflowY !== 'visible')
263
- ? end
264
- : bottom;
312
+ let reach = end;
313
+ if (bottom > end) {
314
+ const style = box.style;
315
+ reach =
316
+ box.parent &&
317
+ (style.overflowX !== 'visible' || style.overflowY !== 'visible')
318
+ ? end
319
+ : bottom;
320
+ }
321
+ // the scrollable overflow of a transformed box is where its transform
322
+ // puts it (CSS Overflow 3, 2.2)
323
+ if (!matrix || reach === -Infinity)
324
+ return reach;
325
+ const to = mapRect(matrix, box.x, box.y, box.width, reach - box.y);
326
+ return Math.min(to.y + to.height, LARGEST);
265
327
  }
266
328
  /**
267
329
  * Whether a box has a rectangle of its own to draw in. An inline box has
@@ -411,6 +473,20 @@ export function paintDocument(ctx, tree, options) {
411
473
  return;
412
474
  ctx.save();
413
475
  const canvas = canvasBackground(tree);
476
+ const scheme = rootScheme(tree);
477
+ if (scheme !== tree.paletteScheme) {
478
+ // A document is drawn on the window's own ground, the palette's, as an
479
+ // embedded one is on a transparent canvas. Where its root's colour
480
+ // scheme is not the scheme of what it is embedded in, its canvas is
481
+ // opaque, in the `Canvas` colour of its own (CSS Color Adjust 1, 2.2):
482
+ // a page that says it is light, and sets its text dark on no
483
+ // background, is not read against a dark window.
484
+ const area = canvasArea(tree.root, options);
485
+ if (area) {
486
+ ctx.fillStyle = SCHEME_COLORS[scheme].canvas;
487
+ ctx.fillRect(area.x, area.y, area.w, area.h);
488
+ }
489
+ }
414
490
  if (canvas)
415
491
  paintCanvas(ctx, canvas, tree.root, options);
416
492
  paintBox(ctx, tree.root, {
@@ -463,6 +539,24 @@ function canvasBackground(tree) {
463
539
  ? { style: body.style, source: body, anchor: top }
464
540
  : null;
465
541
  }
542
+ /** The used colour scheme of the document's root element: the `<html>`'s,
543
+ * or that of whichever box stands in for it (`canvasBackground`). */
544
+ function rootScheme(tree) {
545
+ if (tree.impliedHtml)
546
+ return tree.impliedHtml.colorScheme;
547
+ return (childNamed(tree.root, 'html') ?? tree.root).style.colorScheme;
548
+ }
549
+ /** The canvas, cut to what is being painted: the whole element where the
550
+ * host says what that is, and else the root box. */
551
+ function canvasArea(root, options) {
552
+ const whole = options.canvas ?? {
553
+ x: root.x + options.originX,
554
+ y: root.y + options.originY,
555
+ width: root.width,
556
+ height: root.height,
557
+ };
558
+ return clampRect(options, Math.round(whole.x), Math.round(whole.y), Math.ceil(whole.width), Math.ceil(whole.height));
559
+ }
466
560
  /** A box's child element of that name, or one inside the anonymous boxes
467
561
  * it is wrapped in: a `<body>` in an `<html>` set `display: table` is in
468
562
  * an anonymous row and cell. */
@@ -479,13 +573,7 @@ function childNamed(box, name) {
479
573
  return null;
480
574
  }
481
575
  function paintCanvas(ctx, { style: source, anchor }, root, options) {
482
- const whole = options.canvas ?? {
483
- x: root.x + options.originX,
484
- y: root.y + options.originY,
485
- width: root.width,
486
- height: root.height,
487
- };
488
- const area = clampRect(options, Math.round(whole.x), Math.round(whole.y), Math.ceil(whole.width), Math.ceil(whole.height));
576
+ const area = canvasArea(root, options);
489
577
  if (!area)
490
578
  return;
491
579
  const layers = layersOf(source) ?? [source];
@@ -496,7 +584,7 @@ function paintCanvas(ctx, { style: source, anchor }, root, options) {
496
584
  ctx.fillStyle = inkColor(style.backgroundColor, style.color);
497
585
  ctx.fillRect(area.x, area.y, area.w, area.h);
498
586
  }
499
- if (style.backgroundGradient && ctx.createLinearGradient && visible) {
587
+ if (style.backgroundGradient && visible) {
500
588
  // sized by the root element's box and repeated down the canvas, as a
501
589
  // browser does — the stripes a short page with a gradient on its
502
590
  // body shows — or by the viewport, where it is fixed
@@ -664,28 +752,289 @@ function paintBox(ctx, box, options) {
664
752
  return;
665
753
  }
666
754
  // `clip` shows the part of an absolutely positioned box it names, its own
667
- // background and borders among it (CSS 2.1 11.1.2) — written out here,
668
- // not called: a frame more a level and a deep document ran out of stack
669
- const clip = box.outOfFlow && box.style.clip ? clipOf(box, options) : null;
670
- if (!clip || (clip.w > 0 && clip.h > 0)) {
671
- const clipped = !!clip && pushClip(ctx, clip, null);
672
- paintContent(ctx, box, options);
673
- if (clipped)
755
+ // background and borders among it (CSS 2.1 11.1.2), and `clip-path` the
756
+ // part of any box (`pushOwnClips`) — the content painted here, not in a
757
+ // call of its own: a frame more a level and a deep document ran out of
758
+ // stack
759
+ const pushed = pushOwnClips(ctx, box, options);
760
+ if (pushed >= 0) {
761
+ paintContent(ctx, box, pathClips(box) ? underPath(options) : options);
762
+ for (let i = 0; i < pushed; i += 1)
674
763
  ctx.restore();
675
764
  }
676
765
  if (fade)
677
766
  ctx.restore();
678
767
  }
679
- /** A box and what it holds, cut to its `clip` as `paintBox` cuts it. */
768
+ /** How large a surface a transformed box is drawn on: a large window's
769
+ * worth of pixels twice over, and no side longer than a pixmap's may be. */
770
+ const RASTER_LIMIT = 16 * 1024 * 1024;
771
+ const RASTER_SIDE = 16384;
772
+ /** What XRender carries a picture's transform in is 16.16 fixed point: a
773
+ * number past this does not fit, and is thrown out of the request's
774
+ * encoder. */
775
+ const FIXED_LIMIT = 32000;
776
+ /**
777
+ * A box that turns, scales or skews (CSS Transforms 1): painted as it was
778
+ * laid out, through its matrix — about its `transform-origin`, where its
779
+ * translation has already put it (`placedMatrix`). What it holds is
780
+ * painted with it, as the stacking context it is (`stacksLayers`), culled
781
+ * against the damage taken back through the matrix.
782
+ *
783
+ * The native contexts draw everything through a matrix, a text layout's
784
+ * outlines among it, glyphs and all (`scalesText`). ntk's draws paths
785
+ * through one; a glyph it draws as it was shaped, upright and at its size,
786
+ * where the matrix puts its origin, and an image through a transform the
787
+ * server holds in fixed point, which a picture drawn small far across a
788
+ * window does not fit. So there only a box that is paths and flat colour —
789
+ * an icon, a chevron, a spinner — is drawn through the matrix, and any
790
+ * other on a surface of its own, as it was laid out, with the surface
791
+ * drawn through the matrix (`paintRaster`): a picture of its text rather
792
+ * than the text, resampled, and right about where it is and which way up.
793
+ *
794
+ * A context with no `transform` draws the box where it was laid out: the
795
+ * headless mock, which draws nothing, and a recording one.
796
+ */
797
+ function paintTransformed(ctx, box, options) {
798
+ const matrix = placedMatrix(box);
799
+ if (!matrix) {
800
+ paintBox(ctx, box, options);
801
+ return;
802
+ }
803
+ if (!intersects(box, options) ||
804
+ COLLAPSED_CELLS.has(box) ||
805
+ CLAMPED.has(box)) {
806
+ return;
807
+ }
808
+ if (typeof ctx.transform !== 'function') {
809
+ paintPlaced(ctx, box, options);
810
+ return;
811
+ }
812
+ // In the window's coordinates, where the document's origin is the
813
+ // options': T(origin) · matrix · T(-origin). And about an origin moved
814
+ // with the box's corner to the pixel it is drawn from: what is painted is
815
+ // snapped to the pixel grid a box at a time, so a box a fraction of a
816
+ // pixel down the page is drawn from a whole one, and turned about where
817
+ // it was laid out it came back off the grid — a square mirrored onto
818
+ // itself showed a line of what was under it along each side. A browser
819
+ // snaps a transformed box's corner before its transform the same way.
820
+ const left = box.x + options.originX;
821
+ const top = box.y + options.originY;
822
+ const ox = options.originX + Math.round(left) - left;
823
+ const oy = options.originY + Math.round(top) - top;
824
+ const [a, b, c, d] = matrix;
825
+ const through = [
826
+ a,
827
+ b,
828
+ c,
829
+ d,
830
+ matrix[4] + ox - (a * ox + c * oy),
831
+ matrix[5] + oy - (b * ox + d * oy),
832
+ ];
833
+ const back = invert(through);
834
+ if (!back)
835
+ return;
836
+ const damage = options.damage;
837
+ const inside = {
838
+ ...options,
839
+ damage: damage && mapRect(back, damage.x, damage.y, damage.width, damage.height),
840
+ };
841
+ if (ctx.scalesText !== true &&
842
+ !drawnAsPaths(box) &&
843
+ paintRaster(ctx, box, inside, through)) {
844
+ return;
845
+ }
846
+ ctx.save();
847
+ ctx.transform(a, b, c, d, through[4], through[5]);
848
+ paintPlaced(ctx, box, inside);
849
+ ctx.restore();
850
+ }
851
+ /**
852
+ * Whether a box's style turns, scales or skews it, which is asked before
853
+ * every box that may be one is painted (`paintPositioned`, `paintLines`) —
854
+ * there, and not in `paintBox`, whose frame is one of three a level of
855
+ * nesting costs the stack: a name more in it, and a document a thousand
856
+ * boxes deep ran out.
857
+ */
858
+ function turns(box) {
859
+ const style = box.style;
860
+ return (style.transform !== null || style.rotate !== null || style.scale !== null);
861
+ }
862
+ /**
863
+ * `paintBox` for a box whose ink bounds are not in the coordinates it is
864
+ * being painted in, and so is not culled by them: one painted through a
865
+ * matrix, in its own (`paintTransformed`).
866
+ */
867
+ function paintPlaced(ctx, box, options) {
868
+ const opacity = opacityOf(box);
869
+ if (opacity <= 0)
870
+ return;
871
+ const fade = opacity < 1 && typeof ctx.globalAlpha === 'number';
872
+ if (fade) {
873
+ ctx.save();
874
+ ctx.globalAlpha = ctx.globalAlpha * opacity;
875
+ }
876
+ if (!masked(box.style) || !paintMasked(ctx, box, options)) {
877
+ paintClipped(ctx, box, options);
878
+ }
879
+ if (fade)
880
+ ctx.restore();
881
+ }
882
+ /**
883
+ * Whether all a box and what it holds draw is paths and flat colour:
884
+ * backgrounds, borders, outlines and inline drawings — no text and no list
885
+ * marker, whose glyphs a context may not turn, and no image, gradient,
886
+ * shadow or mask, which one draws through a picture's transform
887
+ * (`paintTransformed`).
888
+ */
889
+ function drawnAsPaths(box) {
890
+ if (box.subtreeTextEnd > box.subtreeTextStart)
891
+ return false;
892
+ const stack = [box];
893
+ while (stack.length) {
894
+ const at = stack.pop();
895
+ if (at.marker || at.replaced === 'image')
896
+ return false;
897
+ const style = at.style;
898
+ if (style.backgroundImage !== null ||
899
+ style.backgroundGradient !== null ||
900
+ style.backgroundImages !== null ||
901
+ style.boxShadow !== null ||
902
+ style.borderImage.source !== null ||
903
+ masked(style)) {
904
+ return false;
905
+ }
906
+ for (const child of at.children)
907
+ stack.push(child);
908
+ }
909
+ return true;
910
+ }
911
+ /**
912
+ * A transformed box painted on a surface of its own, as it was laid out,
913
+ * and the surface drawn through its matrix: the part of what it draws that
914
+ * the damage reaches. True where that is done — or is nothing to do, or
915
+ * cannot be: a part too large for a surface, or a matrix that does not fit
916
+ * the fixed point a picture's transform is sent in, which is one that draws
917
+ * the box a thirtieth its size far across a window, or flatter than can be
918
+ * seen. None of it is drawn then. False where the context has no surface to
919
+ * draw on, and the caller draws the box through the matrix itself.
920
+ */
921
+ function paintRaster(ctx, box, options, through) {
922
+ if (!options.surface || !ctx.drawImage)
923
+ return false;
924
+ const own = ownBounds(box);
925
+ let x0 = Math.floor(own.x + options.originX);
926
+ let y0 = Math.floor(own.y + options.originY);
927
+ let x1 = Math.ceil(own.x + own.width + options.originX);
928
+ let y1 = Math.ceil(own.y + own.height + options.originY);
929
+ const damage = options.damage;
930
+ if (damage) {
931
+ // a pixel more each way: the edge of what is drawn is sampled from
932
+ // beside it
933
+ x0 = Math.max(x0, Math.floor(damage.x) - 1);
934
+ y0 = Math.max(y0, Math.floor(damage.y) - 1);
935
+ x1 = Math.min(x1, Math.ceil(damage.x + damage.width) + 1);
936
+ y1 = Math.min(y1, Math.ceil(damage.y + damage.height) + 1);
937
+ }
938
+ const w = x1 - x0;
939
+ const h = y1 - y0;
940
+ if (!(w > 0 && h > 0))
941
+ return true;
942
+ if (w > RASTER_SIDE || h > RASTER_SIDE || w * h > RASTER_LIMIT)
943
+ return true;
944
+ // what the context makes of the draw below: the picture's transform is
945
+ // the inverse of its matrix after the surface's place in it
946
+ const placed = invert([
947
+ through[0],
948
+ through[1],
949
+ through[2],
950
+ through[3],
951
+ through[0] * x0 + through[2] * y0 + through[4],
952
+ through[1] * x0 + through[3] * y0 + through[5],
953
+ ]);
954
+ if (!placed || placed.some((n) => Math.abs(n) > FIXED_LIMIT))
955
+ return true;
956
+ const surface = options.surface(w, h);
957
+ if (!surface)
958
+ return false;
959
+ try {
960
+ // the surface's own coordinates, the window's moved to its corner
961
+ const on = {
962
+ ...options,
963
+ originX: options.originX - x0,
964
+ originY: options.originY - y0,
965
+ damage: { x: 0, y: 0, width: w, height: h },
966
+ canvas: options.canvas && {
967
+ ...options.canvas,
968
+ x: options.canvas.x - x0,
969
+ y: options.canvas.y - y0,
970
+ },
971
+ viewport: options.viewport && {
972
+ ...options.viewport,
973
+ x: options.viewport.x - x0,
974
+ y: options.viewport.y - y0,
975
+ },
976
+ clips: [],
977
+ };
978
+ paintPlaced(surface.getContext('2d'), box, on);
979
+ ctx.save();
980
+ ctx.transform(through[0], through[1], through[2], through[3], through[4], through[5]);
981
+ ctx.drawImage(surface, x0, y0);
982
+ ctx.restore();
983
+ return true;
984
+ }
985
+ finally {
986
+ surface.destroy?.();
987
+ }
988
+ }
989
+ /** A box and what it holds, cut to its `clip` and its `clip-path` as
990
+ * `paintBox` cuts it. */
680
991
  function paintClipped(ctx, box, options) {
681
- const clip = box.outOfFlow && box.style.clip ? clipOf(box, options) : null;
682
- if (clip && !(clip.w > 0 && clip.h > 0))
992
+ const pushed = pushOwnClips(ctx, box, options);
993
+ if (pushed < 0)
683
994
  return;
684
- const clipped = !!clip && pushClip(ctx, clip, null);
685
- paintContent(ctx, box, options);
686
- if (clipped)
995
+ paintContent(ctx, box, pathClips(box) ? underPath(options) : options);
996
+ for (let i = 0; i < pushed; i += 1)
687
997
  ctx.restore();
688
998
  }
999
+ /**
1000
+ * Cut what follows to what a box shows of itself: the part its `clip`
1001
+ * names, where it is absolutely positioned, and the part its `clip-path`
1002
+ * does (CSS Masking 1, 5.1). How many clips were pushed, for the caller to
1003
+ * restore — none where the context cannot clip — or -1 where they leave
1004
+ * none of the box to show, and nothing was pushed.
1005
+ */
1006
+ function pushOwnClips(ctx, box, options) {
1007
+ const style = box.style;
1008
+ const clip = box.outOfFlow && style.clip ? clipOf(box, options) : null;
1009
+ if (clip && !(clip.w > 0 && clip.h > 0))
1010
+ return -1;
1011
+ const path = pathClips(box) ? clipPathOf(box, options) : null;
1012
+ if (path && !(path.rect.w > 0 && path.rect.h > 0))
1013
+ return -1;
1014
+ let pushed = 0;
1015
+ if (clip && pushClip(ctx, clip, null))
1016
+ pushed += 1;
1017
+ if (path && pushClip(ctx, path.rect, path.radii))
1018
+ pushed += 1;
1019
+ return pushed;
1020
+ }
1021
+ /** Whether a box is cut to a `clip-path`: one with a rectangle of its own
1022
+ * for the path to be measured in. An inline box is drawn on its block's
1023
+ * lines, and is not cut. */
1024
+ export function pathClips(box) {
1025
+ return box.style.clipPath !== null && hasRect(box);
1026
+ }
1027
+ /**
1028
+ * The options what a box cut to a `clip-path` holds is painted with: no
1029
+ * clip around it for a positioned box to escape. The path cuts all the
1030
+ * element holds, whatever that is positioned from, where a box that clips
1031
+ * its overflow lets out a box whose containing block is outside it — put
1032
+ * off until that clip ended (`paintPositioned`), one in here was painted
1033
+ * with the path gone too.
1034
+ */
1035
+ function underPath(options) {
1036
+ return { ...options, clips: [] };
1037
+ }
689
1038
  const MASK_LAYERS = new WeakMap();
690
1039
  /** No corners: a mask layer is placed in the box's rectangle, which its
691
1040
  * radii do not round (CSS Masking 1, 7). */
@@ -921,6 +1270,13 @@ function paintOwnBackground(ctx, box, options) {
921
1270
  if (hidden(box))
922
1271
  return;
923
1272
  const style = box.style;
1273
+ if (columned.any) {
1274
+ const pieces = COLUMN_PIECES.get(box);
1275
+ if (pieces) {
1276
+ paintColumnPieces(ctx, box, pieces, options);
1277
+ return;
1278
+ }
1279
+ }
924
1280
  if (style.boxShadow)
925
1281
  paintShadows(ctx, box, options, false);
926
1282
  if (box !== options.canvasSource) {
@@ -934,6 +1290,70 @@ function paintOwnBackground(ctx, box, options) {
934
1290
  if (box.kind === 'table')
935
1291
  paintPartBackgrounds(ctx, box, options);
936
1292
  }
1293
+ /**
1294
+ * The background and the borders of a block a column break falls inside
1295
+ * (CSS Multi-column 1), a piece at a time: drawn in each column as the
1296
+ * whole box standing there, and cut to the piece of it that is — which is
1297
+ * `box-decoration-break: slice`, CSS's default (CSS Fragmentation 3, 6.1):
1298
+ * no border and no padding at a break, and the background going on from
1299
+ * one piece to the next. Its shadow is not drawn: a sliced box casts one as
1300
+ * a whole and this has no whole box to cast it from.
1301
+ */
1302
+ function paintColumnPieces(ctx, box, pieces, options) {
1303
+ if (!ctx.save || !ctx.restore || !ctx.beginPath || !ctx.rect || !ctx.clip)
1304
+ return;
1305
+ for (const piece of pieces) {
1306
+ if (!(piece.height > 0 && piece.width > 0))
1307
+ continue;
1308
+ const frame = {
1309
+ x: piece.x,
1310
+ y: piece.wholeY,
1311
+ width: piece.width,
1312
+ height: piece.wholeHeight,
1313
+ captionTop: 0,
1314
+ captionBottom: 0,
1315
+ borderTop: box.borderTop,
1316
+ borderRight: box.borderRight,
1317
+ borderBottom: box.borderBottom,
1318
+ borderLeft: box.borderLeft,
1319
+ padTop: box.padTop,
1320
+ padRight: box.padRight,
1321
+ padBottom: box.padBottom,
1322
+ padLeft: box.padLeft,
1323
+ style: box.style,
1324
+ };
1325
+ const left = Math.round(piece.x + options.originX);
1326
+ const top = Math.round(piece.y + options.originY);
1327
+ const right = Math.round(piece.x + piece.width + options.originX);
1328
+ const bottom = Math.round(piece.y + piece.height + options.originY);
1329
+ ctx.save();
1330
+ ctx.beginPath();
1331
+ ctx.rect(left, top, right - left, bottom - top);
1332
+ ctx.clip();
1333
+ paintLayers(ctx, frame, options, frameImages(ctx, frame, options));
1334
+ paintBorders(ctx, frame, options);
1335
+ ctx.restore();
1336
+ }
1337
+ }
1338
+ /**
1339
+ * Clip to the rows of a text's layout that are in the text's column
1340
+ * (`COLUMN_ROWS`), where the layout is drawn at `left` and `top`: the rest
1341
+ * of it is another column's, drawn there. Answers whether it clipped, and
1342
+ * the caller restores.
1343
+ */
1344
+ function clipToRows(ctx, text, rows, left, top) {
1345
+ if (!ctx.save || !ctx.restore || !ctx.beginPath || !ctx.rect || !ctx.clip)
1346
+ return false;
1347
+ const y1 = Math.round(top + rows.top);
1348
+ const y2 = Math.round(top + rows.bottom);
1349
+ ctx.save();
1350
+ ctx.beginPath();
1351
+ ctx.rect(Math.floor(left) - ROW_REACH, y1, Math.ceil(text.layout.width) + 2 * ROW_REACH, y2 - y1);
1352
+ ctx.clip();
1353
+ return true;
1354
+ }
1355
+ /** How far to either side of its layout a column's text may draw. */
1356
+ const ROW_REACH = 2048;
937
1357
  /**
938
1358
  * How `paintLayers` draws a frame's image layers: each in the area its
939
1359
  * `background-clip` names, from the box its `background-origin` names
@@ -1220,6 +1640,8 @@ function inFlow(parent, child, options) {
1220
1640
  style.overflowY === 'visible' &&
1221
1641
  opacityOf(child) >= 1 &&
1222
1642
  !masked(style) &&
1643
+ // cut to a path with all it holds, as one group
1644
+ !style.clipPath &&
1223
1645
  // containment makes it a stacking context, painted whole
1224
1646
  !contained(child, CONTAIN_LAYOUT | CONTAIN_PAINT));
1225
1647
  }
@@ -1246,6 +1668,7 @@ function flowFlex(parent, child, options) {
1246
1668
  style.overflowY === 'visible' &&
1247
1669
  !(child.outOfFlow && style.clip) &&
1248
1670
  !masked(style) &&
1671
+ !style.clipPath &&
1249
1672
  !contained(child, CONTAIN_LAYOUT | CONTAIN_PAINT));
1250
1673
  }
1251
1674
  /**
@@ -1472,7 +1895,11 @@ function paintPositioned(ctx, box, options) {
1472
1895
  return;
1473
1896
  }
1474
1897
  }
1475
- paintBox(ctx, box, atViewport(box, options));
1898
+ // through its matrix, where it turns, scales or skews
1899
+ if (turns(box))
1900
+ paintTransformed(ctx, box, atViewport(box, options));
1901
+ else
1902
+ paintBox(ctx, box, atViewport(box, options));
1476
1903
  }
1477
1904
  /**
1478
1905
  * The options a box is painted with where it is fixed to the viewport
@@ -1509,13 +1936,16 @@ export function fixedToViewport(box) {
1509
1936
  if (box.style.position !== 'fixed')
1510
1937
  return false;
1511
1938
  for (let at = box.parent; at?.parent; at = at.parent) {
1512
- if (at.style.translate || at.style.transformTranslate)
1513
- return false;
1514
- if (contained(at, CONTAIN_LAYOUT | CONTAIN_PAINT))
1939
+ if (holdsFixed(at))
1515
1940
  return false;
1516
1941
  }
1517
1942
  return true;
1518
1943
  }
1944
+ /** Whether a box is the containing block of the fixed boxes in it: a
1945
+ * transformed box, or one with layout or paint containment. */
1946
+ function holdsFixed(box) {
1947
+ return (transformed(box.style) || contained(box, CONTAIN_LAYOUT | CONTAIN_PAINT));
1948
+ }
1519
1949
  function rootOf(box) {
1520
1950
  let root = box;
1521
1951
  while (root.parent)
@@ -1525,17 +1955,22 @@ function rootOf(box) {
1525
1955
  /**
1526
1956
  * The box an out-of-flow box is positioned in, whose clips are the ones it
1527
1957
  * is under (`paintPositioned`, and the hit test's `deepestAt`): its nearest
1528
- * positioned or translated ancestor, or the root. Null for a fixed box,
1529
- * which is under none.
1958
+ * positioned or transformed ancestor, or the root. Null for a box fixed to
1959
+ * the viewport, which is under none — one in a transformed box is fixed to
1960
+ * that, and under what it is under.
1530
1961
  */
1531
1962
  export function containingBlockOf(box) {
1532
- if (box.style.position === 'fixed')
1963
+ if (box.style.position === 'fixed') {
1964
+ for (let at = box.parent; at?.parent; at = at.parent) {
1965
+ if (holdsFixed(at))
1966
+ return at;
1967
+ }
1533
1968
  return null;
1969
+ }
1534
1970
  let containing = box.parent;
1535
1971
  while (containing?.parent &&
1536
1972
  containing.style.position === 'static' &&
1537
- !containing.style.translate &&
1538
- !containing.style.transformTranslate) {
1973
+ !transformed(containing.style)) {
1539
1974
  containing = containing.parent;
1540
1975
  }
1541
1976
  return containing;
@@ -1628,6 +2063,201 @@ function clipOf(box, options) {
1628
2063
  h: Math.round(box.y + options.originY + bottom) - y,
1629
2064
  };
1630
2065
  }
2066
+ /** The document's own coordinates, as a space to place a clip in. */
2067
+ const DOCUMENT = { originX: 0, originY: 0, scale: 1, damage: null };
2068
+ /**
2069
+ * A box's `clip-path` in window coordinates (CSS Masking 1, 5.1): the
2070
+ * rectangle its shape is (CSS Shapes 1, 3.1), each edge on the pixel it
2071
+ * falls nearest as a box's are, and its corners where it has any — of no
2072
+ * area where the shape leaves none of the box to show. Measured in the box
2073
+ * the path names, the border box unless it says: `inset()` in from its
2074
+ * edges, `rect()` and `xywh()` from its top left, and the box itself, with
2075
+ * the element's own corners, where no shape is written.
2076
+ */
2077
+ function clipPathOf(box, options) {
2078
+ const path = box.style.clipPath;
2079
+ // the box it is measured in, in from the border box — or out from it
2080
+ const within = path.box;
2081
+ const [it, ir, ib, il] = within === 'margin-box'
2082
+ ? [-box.marginTop, -box.marginRight, -box.marginBottom, -box.marginLeft]
2083
+ : edgeInsets(box, within);
2084
+ const width = Math.max(0, box.width - il - ir);
2085
+ const height = Math.max(0, frameHeight(box) - it - ib);
2086
+ // how far in from that box's edges the rectangle's are
2087
+ const [a, b, c, d] = path.lengths;
2088
+ let top = 0;
2089
+ let right = 0;
2090
+ let bottom = 0;
2091
+ let left = 0;
2092
+ if (path.shape === 'inset') {
2093
+ top = resolve(a, height);
2094
+ right = resolve(b, width);
2095
+ bottom = resolve(c, height);
2096
+ left = resolve(d, width);
2097
+ // a pair that adds up to more than the box is across is reduced to
2098
+ // it, each in its proportion, as overlapping radii are (3.1):
2099
+ // `inset(50%)` and `inset(75%)` are both the box's centre, and nothing
2100
+ if (left + right > width && left > 0 && right > 0) {
2101
+ const f = Math.max(0, width) / (left + right);
2102
+ left *= f;
2103
+ right *= f;
2104
+ }
2105
+ if (top + bottom > height && top > 0 && bottom > 0) {
2106
+ const f = Math.max(0, height) / (top + bottom);
2107
+ top *= f;
2108
+ bottom *= f;
2109
+ }
2110
+ }
2111
+ else if (path.shape === 'rect') {
2112
+ // four edges from the top and the left, `auto` the box's own; the
2113
+ // right and the bottom are no less than the left and the top
2114
+ top = a === AUTO ? 0 : resolve(a, height);
2115
+ left = d === AUTO ? 0 : resolve(d, width);
2116
+ right = b === AUTO ? 0 : width - Math.max(left, resolve(b, width));
2117
+ bottom = c === AUTO ? 0 : height - Math.max(top, resolve(c, height));
2118
+ }
2119
+ else if (path.shape === 'xywh') {
2120
+ left = resolve(a, width);
2121
+ top = resolve(b, height);
2122
+ right = width - left - Math.max(0, resolve(c, width));
2123
+ bottom = height - top - Math.max(0, resolve(d, height));
2124
+ }
2125
+ const x0 = box.x + options.originX + il;
2126
+ const y0 = frameY(box) + options.originY + it;
2127
+ const x = Math.round(x0 + left);
2128
+ const y = Math.round(y0 + top);
2129
+ const rect = {
2130
+ x,
2131
+ y,
2132
+ w: Math.max(0, Math.round(x0 + width - right) - x),
2133
+ h: Math.max(0, Math.round(y0 + height - bottom) - y),
2134
+ };
2135
+ let radii = null;
2136
+ if (path.shape === null) {
2137
+ // the box alone: its edge, shaped as the element's corners shape it
2138
+ const corners = cornersOf(box.style, box.width, frameHeight(box));
2139
+ if (corners) {
2140
+ radii =
2141
+ within === 'margin-box'
2142
+ ? spreadCorners(corners, box.width, frameHeight(box), -it, -ir, -ib, -il)
2143
+ : roundedOf(insetCorners(corners, it, ir, ib, il));
2144
+ }
2145
+ }
2146
+ else if (path.radii) {
2147
+ // a percentage is of the box the shape is in, and the radii are fitted
2148
+ // to the rectangle they round
2149
+ radii = cornersFor(path.radii, path.radiiY, width, height, rect.w, rect.h);
2150
+ }
2151
+ return { rect, radii };
2152
+ }
2153
+ /** Corners, or null where every one of them is square. */
2154
+ function roundedOf(c) {
2155
+ for (let i = 0; i < 4; i += 1)
2156
+ if (c.x[i] > 0 && c.y[i] > 0)
2157
+ return c;
2158
+ return null;
2159
+ }
2160
+ /**
2161
+ * Whether a point of the document is in what a box's `clip-path` leaves of
2162
+ * it: what a hit test asks, since nothing the path cuts away — of the box
2163
+ * or of anything in it — is under a pointer (`deepestAt`).
2164
+ */
2165
+ export function inClipPath(box, x, y) {
2166
+ const { rect, radii } = clipPathOf(box, DOCUMENT);
2167
+ const right = rect.x + rect.w;
2168
+ const bottom = rect.y + rect.h;
2169
+ if (x < rect.x || x >= right || y < rect.y || y >= bottom)
2170
+ return false;
2171
+ if (!radii)
2172
+ return true;
2173
+ // inside the rectangle, and past a corner's curve: each corner is a
2174
+ // quarter of an ellipse about a centre its radii in from the two edges
2175
+ for (let i = 0; i < 4; i += 1) {
2176
+ const rx = radii.x[i];
2177
+ const ry = radii.y[i];
2178
+ if (!(rx > 0 && ry > 0))
2179
+ continue;
2180
+ const onLeft = i === 0 || i === 3;
2181
+ const cx = onLeft ? rect.x + rx : right - rx;
2182
+ const cy = i < 2 ? rect.y + ry : bottom - ry;
2183
+ if (onLeft ? x >= cx : x <= cx)
2184
+ continue;
2185
+ if (i < 2 ? y >= cy : y <= cy)
2186
+ continue;
2187
+ const dx = (x - cx) / rx;
2188
+ const dy = (y - cy) / ry;
2189
+ if (dx * dx + dy * dy > 1)
2190
+ return false;
2191
+ }
2192
+ return true;
2193
+ }
2194
+ /**
2195
+ * Whether a point of the document is in the part of a box its `clip` shows
2196
+ * (CSS 2.1 11.1.2) — anywhere, for a box that is cut to none: what a hit
2197
+ * test asks, since what the clip cuts away is not drawn (`deepestAt`). What
2198
+ * it cuts away is the box and what is painted with it: all the box holds
2199
+ * where it is a stacking context (`stacksLayers`), and elsewhere all but a
2200
+ * fixed box, which the context around it paints under no clip
2201
+ * (`clipsFor`). Chrome cuts that one too.
2202
+ */
2203
+ export function inClip(box, x, y) {
2204
+ if (!box.outOfFlow || !box.style.clip)
2205
+ return true;
2206
+ const rect = clipOf(box, DOCUMENT);
2207
+ return (x >= rect.x && x < rect.x + rect.w && y >= rect.y && y < rect.y + rect.h);
2208
+ }
2209
+ /**
2210
+ * The part of the document a box shows through, in the document's own
2211
+ * pixels, or null where nothing cuts it: the `clip` it is cut to (CSS 2.1
2212
+ * 11.1.2), and the clips of the boxes it is under — each that clips its
2213
+ * overflow or is cut to a `clip` of its own, from its containing block up
2214
+ * (11.1.1), so an absolute box is outside a box that clips where its
2215
+ * containing block is, and a fixed one outside them all. And every
2216
+ * `clip-path` from the box up, its own among them: a path cuts all its
2217
+ * element holds, whatever that is positioned from (CSS Masking 1, 5.1) —
2218
+ * to the rectangle a path's shape is, its corners left square. A rectangle
2219
+ * of no area where none of it shows. Painting cuts what it draws as it
2220
+ * walks down; this is for what is not painted here and has to be cut all
2221
+ * the same, a control's widget (`controlRectsOf`).
2222
+ */
2223
+ export function clipAround(box, scale = 1) {
2224
+ const space = { originX: 0, originY: 0, scale, damage: null };
2225
+ let x0 = -Infinity;
2226
+ let y0 = -Infinity;
2227
+ let x1 = Infinity;
2228
+ let y1 = Infinity;
2229
+ let cut = false;
2230
+ const under = (rect) => {
2231
+ cut = true;
2232
+ x0 = Math.max(x0, rect.x);
2233
+ y0 = Math.max(y0, rect.y);
2234
+ x1 = Math.min(x1, rect.x + rect.w);
2235
+ y1 = Math.min(y1, rect.y + rect.h);
2236
+ };
2237
+ if (box.outOfFlow && box.style.clip)
2238
+ under(clipOf(box, space));
2239
+ // what holds a box, as clips go: its parent, or out of the flow its
2240
+ // containing block, and the boxes between them do not clip it
2241
+ const holder = (inner) => inner.outOfFlow ? containingBlockOf(inner) : inner.parent;
2242
+ for (let outer = holder(box); outer; outer = holder(outer)) {
2243
+ if (outer.outOfFlow && outer.style.clip)
2244
+ under(clipOf(outer, space));
2245
+ if (clipsOverflow(outer))
2246
+ under(clipEdge(outer, space).rect);
2247
+ }
2248
+ for (let at = box; at; at = at.parent) {
2249
+ if (pathClips(at))
2250
+ under(clipPathOf(at, space).rect);
2251
+ }
2252
+ if (!cut)
2253
+ return null;
2254
+ return {
2255
+ x: x0,
2256
+ y: y0,
2257
+ width: Math.max(0, x1 - x0),
2258
+ height: Math.max(0, y1 - y0),
2259
+ };
2260
+ }
1631
2261
  /**
1632
2262
  * The radii of a box's padding edge — its border radii less the borders —
1633
2263
  * for a clip, or null where a rectangle clips the same: padding at least a
@@ -1686,6 +2316,16 @@ function cornersOf(style, w, h) {
1686
2316
  across[3] === 0) {
1687
2317
  return null;
1688
2318
  }
2319
+ return cornersFor(across, down, w, h, w, h);
2320
+ }
2321
+ /**
2322
+ * Radii as corners in pixels: each a length or a percentage of a box `w`
2323
+ * by `h`, across and down, and all of them reduced together where two on a
2324
+ * side of the rectangle they round — `fitW` by `fitH`, which for a
2325
+ * `border-radius` is that box, and for a `clip-path`'s `round` is the
2326
+ * rectangle inside it — would overlap. Null where every corner is square.
2327
+ */
2328
+ function cornersFor(across, down, w, h, fitW, fitH) {
1689
2329
  const vertical = down ?? across;
1690
2330
  const x = [0, 0, 0, 0];
1691
2331
  const y = [0, 0, 0, 0];
@@ -1706,10 +2346,10 @@ function cornersOf(style, w, h) {
1706
2346
  if (sum > side)
1707
2347
  f = Math.min(f, Math.max(0, side) / sum);
1708
2348
  };
1709
- fit(w, x[0] + x[1]);
1710
- fit(h, y[1] + y[2]);
1711
- fit(w, x[2] + x[3]);
1712
- fit(h, y[3] + y[0]);
2349
+ fit(fitW, x[0] + x[1]);
2350
+ fit(fitH, y[1] + y[2]);
2351
+ fit(fitW, x[2] + x[3]);
2352
+ fit(fitH, y[3] + y[0]);
1713
2353
  if (f < 1) {
1714
2354
  for (let i = 0; i < 4; i += 1) {
1715
2355
  x[i] *= f;
@@ -1838,8 +2478,7 @@ function layered(parent, child) {
1838
2478
  return true;
1839
2479
  if (style.position !== 'relative' &&
1840
2480
  style.position !== 'sticky' &&
1841
- !style.translate &&
1842
- !style.transformTranslate) {
2481
+ !transformed(style)) {
1843
2482
  return false;
1844
2483
  }
1845
2484
  return child.kind !== 'inline' && !onLine(parent, child);
@@ -1851,7 +2490,7 @@ function layered(parent, child) {
1851
2490
  * drawn twice — darker at every antialiased edge — and a translucent
1852
2491
  * background had its alpha doubled.
1853
2492
  */
1854
- function onLine(parent, child) {
2493
+ export function onLine(parent, child) {
1855
2494
  if (parent.lines === null && parent.kind !== 'inline')
1856
2495
  return false;
1857
2496
  if (child.isFloat || child.outOfFlow)
@@ -1927,24 +2566,31 @@ function settleLayers(box, list) {
1927
2566
  * it itself, and the ones below its flow: positioned with a `z-index`, or a
1928
2567
  * flex item with one; fixed or sticky with none (CSS Positioned Layout 3, as
1929
2568
  * browsers paint them — a fixed header's box set behind its content with
1930
- * `z-index: -1` went behind the page); or under full opacity (CSS Color 4
2569
+ * `z-index: -1` went behind the page); transformed (CSS Transforms 1, 3);
2570
+ * or under full opacity (CSS Color 4
1931
2571
  * 3.2), its own or the opacity it takes from an inline box it broke
1932
2572
  * (`FADED_BLOCKS`), which is painted as one group — faded with it, or at
1933
2573
  * `opacity: 0` not at all, where its root context drew a hover menu's
1934
2574
  * absolute children.
1935
2575
  */
1936
- function stacksLayers(box) {
2576
+ export function stacksLayers(box) {
1937
2577
  const style = box.style;
1938
2578
  if (style.position === 'fixed' || style.position === 'sticky')
1939
2579
  return true;
1940
2580
  if (style.opacity < 1 || FADED_BLOCKS.has(box))
1941
2581
  return true;
1942
- // and so does a mask, which is applied to the group (CSS Masking 1, 7)
1943
- if (masked(style))
2582
+ // and so does a mask, which is applied to the group (CSS Masking 1, 7),
2583
+ // and a clip path, which cuts it (5.1)
2584
+ if (masked(style) || pathClips(box))
1944
2585
  return true;
1945
2586
  // layout and paint containment make one (CSS Containment 2, 3.3, 3.5)
1946
2587
  if (contained(box, CONTAIN_LAYOUT | CONTAIN_PAINT))
1947
2588
  return true;
2589
+ // and a transform (CSS Transforms 1, 3), on a box one applies to: what it
2590
+ // holds is painted with it, through its matrix, and not by a context
2591
+ // outside it
2592
+ if (transformed(style) && box.kind !== 'inline')
2593
+ return true;
1948
2594
  if (typeof style.zIndex !== 'number')
1949
2595
  return false;
1950
2596
  return style.position !== 'static' || flexItem(box);
@@ -1967,7 +2613,7 @@ export function clipsFor(box, context) {
1967
2613
  const fixed = box.style.position === 'fixed';
1968
2614
  while (from && from !== context) {
1969
2615
  const style = from.style;
1970
- if (style.translate || style.transformTranslate)
2616
+ if (transformed(style))
1971
2617
  break;
1972
2618
  if (!fixed && style.position !== 'static')
1973
2619
  break;
@@ -2067,9 +2713,19 @@ function hoistFrom(box, root) {
2067
2713
  return null;
2068
2714
  }
2069
2715
  function byZIndex(a, b) {
2070
- const az = a.style.zIndex === 'auto' ? 0 : a.style.zIndex;
2071
- const bz = b.style.zIndex === 'auto' ? 0 : b.style.zIndex;
2072
- return az - bz;
2716
+ return layerOf(a) - layerOf(b);
2717
+ }
2718
+ /** The `z-index` a box is ordered by among its stacking context's layers:
2719
+ * its own where one applies — to a positioned box, and to a flex or a grid
2720
+ * item (CSS 2.1 9.9.1, CSS Flexbox 5.4) — and none for a box that is
2721
+ * among them for its transform alone, which is painted in the document's
2722
+ * order whatever `z-index` it was given. */
2723
+ function layerOf(box) {
2724
+ const style = box.style;
2725
+ const z = style.zIndex;
2726
+ if (z === 'auto')
2727
+ return 0;
2728
+ return style.position !== 'static' || flexItem(box) ? z : 0;
2073
2729
  }
2074
2730
  /** Whether anything this box or its descendants draw is in the damage. */
2075
2731
  function intersects(box, options) {
@@ -2147,7 +2803,7 @@ function paintBackground(ctx, box, options, style) {
2147
2803
  ctx.fillStyle = inkColor(color, style.color);
2148
2804
  fill();
2149
2805
  }
2150
- if (gradient && ctx.createLinearGradient) {
2806
+ if (gradient) {
2151
2807
  // over the colour, as the layer an image is: the size of the padding
2152
2808
  // box, which is where it starts, and repeated under the borders — or
2153
2809
  // the viewport's, where it is fixed; its line runs across the whole of
@@ -2170,8 +2826,9 @@ function paintBackground(ctx, box, options, style) {
2170
2826
  // with its end colours where a browser would show the next tile
2171
2827
  if (!(at.width > 0 && at.height > 0))
2172
2828
  return;
2173
- ctx.fillStyle = linearGradient(ctx, gradient, at.x, at.y, at.width, at.height, style.color, rect);
2174
- fill();
2829
+ const paint = gradientFill(ctx, gradient, at.x, at.y, at.width, at.height, style.color, rect);
2830
+ if (paint)
2831
+ fillGradient(ctx, paint, rect, rounded);
2175
2832
  }
2176
2833
  else
2177
2834
  paintGradient(ctx, style, gradient, rect, at);
@@ -2219,8 +2876,9 @@ function paintGradient(ctx, style, gradient, area, at) {
2219
2876
  MAX_TILES) {
2220
2877
  // a sliver of a root repeated down a long canvas: the one tile, and
2221
2878
  // its end colours on past it
2222
- ctx.fillStyle = linearGradient(ctx, gradient, x0, y0, w, h, style.color, area);
2223
- ctx.fillRect(area.x, area.y, area.w, area.h);
2879
+ const paint = gradientFill(ctx, gradient, x0, y0, w, h, style.color, area);
2880
+ if (paint)
2881
+ fillGradient(ctx, paint, area);
2224
2882
  return;
2225
2883
  }
2226
2884
  for (let y = fromY; y < toY; y += stepY) {
@@ -2234,8 +2892,9 @@ function paintGradient(ctx, style, gradient, area, at) {
2234
2892
  if (right <= left)
2235
2893
  continue;
2236
2894
  const tile = { x: left, y: top, w: right - left, h: bottom - top };
2237
- ctx.fillStyle = linearGradient(ctx, gradient, x, y, w, h, style.color, tile);
2238
- ctx.fillRect(tile.x, tile.y, tile.w, tile.h);
2895
+ const paint = gradientFill(ctx, gradient, x, y, w, h, style.color, tile);
2896
+ if (paint)
2897
+ fillGradient(ctx, paint, tile);
2239
2898
  }
2240
2899
  }
2241
2900
  }
@@ -2265,6 +2924,39 @@ function linearGradient(ctx, gradient, x, y, w, h, currentColor, visible = null)
2265
2924
  const dy = -Math.cos(angle);
2266
2925
  const length = Math.abs(w * dx) + Math.abs(h * dy);
2267
2926
  const stops = gradient.stops;
2927
+ const at = stopOffsets(stops, length);
2928
+ // the context's offsets are 0 to 1, so a line that stops reach past runs
2929
+ // from the first to the last, and the box sees the part of it between
2930
+ const from = Math.min(0, at[0]);
2931
+ const to = Math.max(1, at[at.length - 1]);
2932
+ const startX = x + w / 2 - (dx * length) / 2;
2933
+ const startY = y + h / 2 - (dy * length) / 2;
2934
+ const x0 = startX + dx * length * from;
2935
+ const y0 = startY + dy * length * from;
2936
+ const x1 = startX + dx * length * to;
2937
+ const y1 = startY + dy * length * to;
2938
+ const colors = stops.map((stop) => inkColor(stop.color, currentColor));
2939
+ if (visible &&
2940
+ length > 0 &&
2941
+ !(Math.abs(x0) < GRADIENT_REACH &&
2942
+ Math.abs(y0) < GRADIENT_REACH &&
2943
+ Math.abs(x1) < GRADIENT_REACH &&
2944
+ Math.abs(y1) < GRADIENT_REACH)) {
2945
+ return clippedGradient(ctx, at, colors, startX, startY, dx * length, dy * length, visible);
2946
+ }
2947
+ const g = ctx.createLinearGradient(x0, y0, x1, y1);
2948
+ for (let i = 0; i < stops.length; i += 1) {
2949
+ g.addColorStop((at[i] - from) / (to - from), colors[i]);
2950
+ }
2951
+ return g;
2952
+ }
2953
+ /**
2954
+ * Where each of a gradient's stops is along a line `length` long, as a
2955
+ * fraction of it (CSS Images 3, 3.5.1): a stop without a position is
2956
+ * spread evenly between its neighbours', the first at 0 and the last at 1
2957
+ * where they have none, and none is before the one before it.
2958
+ */
2959
+ function stopOffsets(stops, length) {
2268
2960
  const at = stops.map((stop) => stop.at === null
2269
2961
  ? null
2270
2962
  : length > 0
@@ -2288,30 +2980,195 @@ function linearGradient(ctx, gradient, x, y, w, h, currentColor, visible = null)
2288
2980
  }
2289
2981
  at[i] = Math.max(at[i], at[i - 1]);
2290
2982
  }
2291
- // the context's offsets are 0 to 1, so a line that stops reach past runs
2292
- // from the first to the last, and the box sees the part of it between
2293
- const from = Math.min(0, at[0]);
2294
- const to = Math.max(1, at[at.length - 1]);
2295
- const startX = x + w / 2 - (dx * length) / 2;
2296
- const startY = y + h / 2 - (dy * length) / 2;
2297
- const x0 = startX + dx * length * from;
2298
- const y0 = startY + dy * length * from;
2299
- const x1 = startX + dx * length * to;
2300
- const y1 = startY + dy * length * to;
2301
- const colors = stops.map((stop) => inkColor(stop.color, currentColor));
2302
- if (visible &&
2303
- length > 0 &&
2304
- !(Math.abs(x0) < GRADIENT_REACH &&
2305
- Math.abs(y0) < GRADIENT_REACH &&
2306
- Math.abs(x1) < GRADIENT_REACH &&
2307
- Math.abs(y1) < GRADIENT_REACH)) {
2308
- return clippedGradient(ctx, at, colors, startX, startY, dx * length, dy * length, visible);
2983
+ return at;
2984
+ }
2985
+ /** A gradient's fill for a box at (`x`, `y`), `w` by `h`: null where the
2986
+ * context draws no gradient of its kind. */
2987
+ function gradientFill(ctx, gradient, x, y, w, h, currentColor, visible = null) {
2988
+ if (gradient.kind === 'radial') {
2989
+ return radialGradient(ctx, gradient, x, y, w, h, currentColor, visible);
2309
2990
  }
2310
- const g = ctx.createLinearGradient(x0, y0, x1, y1);
2311
- for (let i = 0; i < stops.length; i += 1) {
2312
- g.addColorStop((at[i] - from) / (to - from), colors[i]);
2991
+ if (!ctx.createLinearGradient)
2992
+ return null;
2993
+ return {
2994
+ style: linearGradient(ctx, gradient, x, y, w, h, currentColor, visible),
2995
+ matrix: null,
2996
+ };
2997
+ }
2998
+ /**
2999
+ * A `radial-gradient()` across a box (CSS Images 3, 3.2.2): centred where
3000
+ * its position puts it, and as large as its radii, or as its keyword says
3001
+ * — the side or the corner of the box nearest the centre or furthest from
3002
+ * it, and for an ellipse through a corner, the ellipse with the shape the
3003
+ * sides would give it. The stops are spaced along the ray from the centre
3004
+ * to the ending shape's edge, which for an ellipse is its horizontal
3005
+ * radius; one before the centre, where the ray starts, gives the colour
3006
+ * there and no more, and one past the edge carries the gradient on.
3007
+ *
3008
+ * A shape with no width or no height is drawn as its last colour, as
3009
+ * Chrome draws each (crbug.com/635727): the spec draws a zero width as a
3010
+ * gradient mirrored about the centre line, which Chrome does not.
3011
+ */
3012
+ function radialGradient(ctx, gradient, x, y, w, h, currentColor, visible = null) {
3013
+ const colors = gradient.stops.map((stop) => inkColor(stop.color, currentColor));
3014
+ const cx = resolve(gradient.at[0], w);
3015
+ const cy = resolve(gradient.at[1], h);
3016
+ let rx;
3017
+ let ry;
3018
+ if (gradient.radii) {
3019
+ rx = resolve(gradient.radii[0], w);
3020
+ ry = resolve(gradient.radii[1], h);
2313
3021
  }
2314
- return g;
3022
+ else {
3023
+ const extent = gradient.extent ?? 'farthest-corner';
3024
+ const side = extent.startsWith('closest') ? Math.min : Math.max;
3025
+ const sx = side(Math.abs(cx), Math.abs(w - cx));
3026
+ const sy = side(Math.abs(cy), Math.abs(h - cy));
3027
+ const corner = extent.endsWith('corner');
3028
+ if (gradient.circle) {
3029
+ rx = ry = corner ? Math.hypot(sx, sy) : side(sx, sy);
3030
+ }
3031
+ else {
3032
+ // through the corner, in the shape the sides give it
3033
+ rx = corner ? sx * Math.SQRT2 : sx;
3034
+ ry = corner ? sy * Math.SQRT2 : sy;
3035
+ }
3036
+ }
3037
+ if (!(rx > 0 && ry > 0)) {
3038
+ return { style: colors[colors.length - 1], matrix: null };
3039
+ }
3040
+ if (!hasRadialGradients(ctx))
3041
+ return null;
3042
+ let at = stopOffsets(gradient.stops, rx);
3043
+ let inks = colors;
3044
+ if (at[0] < 0) {
3045
+ // the colour at the centre, between the stops either side of it
3046
+ const first = colorAlong(at, colors, 0);
3047
+ const i = at.findIndex((t) => t > 0);
3048
+ if (i < 0)
3049
+ return { style: first, matrix: null };
3050
+ at = [0, ...at.slice(i)];
3051
+ inks = [first, ...colors.slice(i)];
3052
+ }
3053
+ const ax = x + cx;
3054
+ const ay = y + cy;
3055
+ const seen = visible ?? { x, y, w, h };
3056
+ if (!(Math.abs(ax) < RADIAL_REACH && Math.abs(ay) < RADIAL_REACH)) {
3057
+ return farRadial(ctx, at, inks, ax, ay, rx, ry, seen);
3058
+ }
3059
+ let to = Math.max(1, at[at.length - 1]);
3060
+ if (Math.max(rx, ry) * to > GRADIENT_REACH) {
3061
+ // as far along the ray as what is seen of it reaches, and no further:
3062
+ // a circle a context cannot carry, cut to the part it draws
3063
+ let reach = 0;
3064
+ for (const px of [seen.x, seen.x + seen.w]) {
3065
+ for (const py of [seen.y, seen.y + seen.h]) {
3066
+ reach = Math.max(reach, Math.hypot((px - ax) / rx, (py - ay) / ry));
3067
+ }
3068
+ }
3069
+ if (reach > 0 && reach < to) {
3070
+ const last = colorAlong(at, inks, reach);
3071
+ const kept = at.filter((t) => t < reach).length;
3072
+ at = [...at.slice(0, kept), reach];
3073
+ inks = [...inks.slice(0, kept), last];
3074
+ to = reach;
3075
+ }
3076
+ }
3077
+ const circle = rx === ry;
3078
+ // a circle as wide as the ellipse is narrow, stretched along the other
3079
+ // axis: the matrix only ever widens, so its inverse, which a context
3080
+ // samples the gradient through, reaches no further than the centre does
3081
+ const r = Math.min(rx, ry);
3082
+ const g = circle
3083
+ ? ctx.createRadialGradient(ax, ay, 0, ax, ay, r * to)
3084
+ : ctx.createRadialGradient(0, 0, 0, 0, 0, r * to);
3085
+ for (let i = 0; i < at.length; i += 1) {
3086
+ g.addColorStop(at[i] / to, inks[i]);
3087
+ }
3088
+ return {
3089
+ style: g,
3090
+ matrix: circle ? null : [rx / r, 0, 0, ry / r, ax, ay],
3091
+ };
3092
+ }
3093
+ /**
3094
+ * How far from the origin a radial gradient's centre may be and still be
3095
+ * the context's to draw: ntk makes a gradient past what X RENDER carries at
3096
+ * a scale that fits, and its 16.16 matrix holds that scale to a fraction of
3097
+ * a pixel to here.
3098
+ */
3099
+ const RADIAL_REACH = 1 << 18;
3100
+ /**
3101
+ * A radial gradient whose centre is further off than a context carries —
3102
+ * `at 0 calc(infinity * 1px)`, which comes to the largest length there is —
3103
+ * across the part of it that is seen: from that far its rings are as good
3104
+ * as straight, so it is drawn as the linear gradient that runs the way the
3105
+ * rings spread there, as fast as they do, cut to what is seen as a linear
3106
+ * gradient's line is. The rings bend away from it by the square of what is
3107
+ * seen over eight times the distance, under a pixel from here.
3108
+ */
3109
+ function farRadial(ctx, at, colors, ax, ay, rx, ry, seen) {
3110
+ if (!ctx.createLinearGradient)
3111
+ return null;
3112
+ // the middle of what is seen, in the circle's own space, and how fast
3113
+ // the gradient runs there, a pixel
3114
+ const px = seen.x + seen.w / 2;
3115
+ const py = seen.y + seen.h / 2;
3116
+ const ux = (px - ax) / rx;
3117
+ const uy = (py - ay) / ry;
3118
+ const t = Math.hypot(ux, uy);
3119
+ const gx = ux / rx / t;
3120
+ const gy = uy / ry / t;
3121
+ const g2 = gx * gx + gy * gy;
3122
+ if (!(t > 0 && g2 > 0 && Number.isFinite(t / g2))) {
3123
+ return { style: colorAlong(at, colors, t), matrix: null };
3124
+ }
3125
+ // the line it runs along, from where it would start to where it is 1
3126
+ const lx = gx / g2;
3127
+ const ly = gy / g2;
3128
+ return {
3129
+ style: clippedGradient(ctx, at, colors, px - lx * t, py - ly * t, lx, ly, seen),
3130
+ matrix: null,
3131
+ };
3132
+ }
3133
+ /**
3134
+ * Whether a context draws radial gradients. react-x11's macOS and Windows
3135
+ * contexts have the method and paint what it returns flat, in one colour
3136
+ * — over whatever a vignette was meant to let through — and theirs takes
3137
+ * no circles, which is how it is told from one that draws them: ntk's and
3138
+ * the Wayland context's take the six numbers canvas gives it. There a
3139
+ * radial gradient is drawn as nothing, as every one was.
3140
+ */
3141
+ function hasRadialGradients(ctx) {
3142
+ return (ctx.createRadialGradient?.length ?? 0) >= 6;
3143
+ }
3144
+ /**
3145
+ * Fills a rectangle, rounded where `corners` says, with a gradient. An
3146
+ * ellipse's shape is laid down first and filled under its matrix, which a
3147
+ * context applies to the fill's paint and not to a path it already has.
3148
+ */
3149
+ function fillGradient(ctx, fill, r, corners = null) {
3150
+ ctx.fillStyle = fill.style;
3151
+ if (!fill.matrix) {
3152
+ if (corners) {
3153
+ ctx.beginPath();
3154
+ roundedRect(ctx, r.x, r.y, r.w, r.h, corners);
3155
+ ctx.fill();
3156
+ }
3157
+ else
3158
+ ctx.fillRect(r.x, r.y, r.w, r.h);
3159
+ return;
3160
+ }
3161
+ if (!ctx.transform || !ctx.beginPath || !ctx.rect || !ctx.fill)
3162
+ return;
3163
+ ctx.save();
3164
+ ctx.beginPath();
3165
+ if (corners)
3166
+ roundedRect(ctx, r.x, r.y, r.w, r.h, corners);
3167
+ else
3168
+ ctx.rect(r.x, r.y, r.w, r.h);
3169
+ ctx.transform(...fill.matrix);
3170
+ ctx.fill();
3171
+ ctx.restore();
2315
3172
  }
2316
3173
  /**
2317
3174
  * How far from the origin a gradient's line may end. X RENDER takes the
@@ -2353,29 +3210,30 @@ function clippedGradient(ctx, at, colors, sx, sy, lx, ly, visible) {
2353
3210
  const cy = (top + bottom) / 2;
2354
3211
  const tc = along(cx, cy);
2355
3212
  const g = ctx.createLinearGradient(cx + (t0 - tc) * lx, cy + (t0 - tc) * ly, cx + (t1 - tc) * lx, cy + (t1 - tc) * ly);
2356
- // the colour at a place on the line: a stop's, or between two
2357
- const colorAt = (t) => {
2358
- if (!(t > at[0]))
2359
- return colors[0];
2360
- for (let i = 1; i < at.length; i += 1) {
2361
- if (t > at[i])
2362
- continue;
2363
- const span = at[i] - at[i - 1];
2364
- const f = span > 0 ? (t - at[i - 1]) / span : 1;
2365
- return (blend(colors[i - 1], colors[i], f) ??
2366
- (f < 0.5 ? colors[i - 1] : colors[i]));
2367
- }
2368
- return colors[colors.length - 1];
2369
- };
2370
- g.addColorStop(0, colorAt(t0));
3213
+ g.addColorStop(0, colorAlong(at, colors, t0));
2371
3214
  for (let i = 0; i < at.length; i += 1) {
2372
3215
  if (at[i] > t0 && at[i] < t1) {
2373
3216
  g.addColorStop((at[i] - t0) / (t1 - t0), colors[i]);
2374
3217
  }
2375
3218
  }
2376
- g.addColorStop(1, colorAt(t1));
3219
+ g.addColorStop(1, colorAlong(at, colors, t1));
2377
3220
  return g;
2378
3221
  }
3222
+ /** The colour at a place along a gradient whose stops are at `at`: a
3223
+ * stop's, or between two. */
3224
+ function colorAlong(at, colors, t) {
3225
+ if (!(t > at[0]))
3226
+ return colors[0];
3227
+ for (let i = 1; i < at.length; i += 1) {
3228
+ if (t > at[i])
3229
+ continue;
3230
+ const span = at[i] - at[i - 1];
3231
+ const f = span > 0 ? (t - at[i - 1]) / span : 1;
3232
+ return (blend(colors[i - 1], colors[i], f) ??
3233
+ (f < 0.5 ? colors[i - 1] : colors[i]));
3234
+ }
3235
+ return colors[colors.length - 1];
3236
+ }
2379
3237
  /**
2380
3238
  * The backgrounds of a table's column groups, columns, row groups and rows
2381
3239
  * (CSS 2.1 17.5.1), in that order: over the table's own and under its
@@ -2721,16 +3579,6 @@ function tileSize(size, area, scale) {
2721
3579
  }
2722
3580
  return [area.width, area.height];
2723
3581
  }
2724
- /**
2725
- * Borders, as four rectangles.
2726
- *
2727
- * Not as a stroked path, for the reason richtext gives about its underlines:
2728
- * the mock backend has no path API, and a 1px border on a pixel grid is a
2729
- * rectangle rather than something an antialiased stroke improves. Corners are
2730
- * mitred by drawing the top and bottom full-width and the sides between them,
2731
- * which is right whenever the two sides share a colour and close enough when
2732
- * they do not.
2733
- */
2734
3582
  /**
2735
3583
  * A box's outline (CSS 2.1 18.4, CSS UI 4 5): a border of its own width,
2736
3584
  * style and colour round the border box grown by `outline-offset` — which
@@ -2782,6 +3630,25 @@ function paintOutline(ctx, box, options) {
2782
3630
  style: ring,
2783
3631
  }, options);
2784
3632
  }
3633
+ /**
3634
+ * Borders, a side at a time.
3635
+ *
3636
+ * As rectangles where they can be, the top and the bottom full width and
3637
+ * the sides between them, and not as a stroked path, for the reason
3638
+ * richtext gives about its underlines: the mock backend has no path API, and
3639
+ * a 1px border on a pixel grid is a rectangle rather than something an
3640
+ * antialiased stroke improves. That is every border whose sides share a
3641
+ * colour, where a corner looks the same whichever side it is given to.
3642
+ *
3643
+ * Where two sides that meet differ in colour — one of them transparent
3644
+ * included — the corner is cut on the diagonal from its outer point to its
3645
+ * inner one and each side takes its half (CSS Backgrounds 3, 4.3), so a side
3646
+ * is a trapezoid, and a triangle where the box inside the borders has no
3647
+ * width: the CSS triangle a dropdown's caret and a tooltip's arrow are
3648
+ * drawn with, which came out as the rectangle around it. Only a solid side
3649
+ * is cut, and only against a side that is solid or paints nothing; dots,
3650
+ * dashes and a double border's two lines keep their rectangles.
3651
+ */
2785
3652
  function paintBorders(ctx, box, options) {
2786
3653
  // most boxes have none, and are asked on every paint
2787
3654
  if (!(box.borderTop || box.borderRight || box.borderBottom || box.borderLeft))
@@ -2799,27 +3666,147 @@ function paintBorders(ctx, box, options) {
2799
3666
  return;
2800
3667
  if (sculpted(s) && paintSculpted(ctx, box, x, y, w, h, options))
2801
3668
  return;
3669
+ const t = box.borderTop;
3670
+ const r = box.borderRight;
3671
+ const b = box.borderBottom;
3672
+ const l = box.borderLeft;
3673
+ /** What a side is painted in, null where it paints nothing. */
3674
+ const ink = (width, color) => width > 0 && !isTransparent(color) ? inkColor(color, s.color) : null;
3675
+ const topInk = ink(t, s.borderTopColor);
3676
+ const rightInk = ink(r, s.borderRightColor);
3677
+ const bottomInk = ink(b, s.borderBottomColor);
3678
+ const leftInk = ink(l, s.borderLeftColor);
3679
+ // the corners cut on the diagonal; none where the context draws no path,
3680
+ // and the sides are rectangles
3681
+ const paths = !!(ctx.beginPath && ctx.moveTo && ctx.lineTo && ctx.fill);
3682
+ const topStyle = s.borderTopStyle;
3683
+ const rightStyle = s.borderRightStyle;
3684
+ const bottomStyle = s.borderBottomStyle;
3685
+ const leftStyle = s.borderLeftStyle;
3686
+ const tl = paths && mitred(t, l, topInk, leftInk, topStyle, leftStyle);
3687
+ const tr = paths && mitred(t, r, topInk, rightInk, topStyle, rightStyle);
3688
+ const br = paths && mitred(b, r, bottomInk, rightInk, bottomStyle, rightStyle);
3689
+ const bl = paths && mitred(b, l, bottomInk, leftInk, bottomStyle, leftStyle);
2802
3690
  const edge = (ex, ey, ew, eh, style, color, horizontal) => {
2803
3691
  const rect = clampRect(options, ex, ey, ew, eh);
2804
3692
  if (!rect)
2805
3693
  return;
2806
- ctx.fillStyle = inkColor(color, s.color);
3694
+ ctx.fillStyle = color;
2807
3695
  // The un-clamped start is the dash phase's origin, so the pattern does
2808
3696
  // not crawl as the viewport moves along a long edge.
2809
3697
  fillEdge(ctx, rect, horizontal ? ex : ey, style, horizontal);
2810
3698
  };
2811
- if (box.borderTop > 0 && !isTransparent(s.borderTopColor)) {
2812
- edge(x, y, w, box.borderTop, s.borderTopStyle, s.borderTopColor, true);
3699
+ /** A side with a cut corner: its outer edge and then its inner one. */
3700
+ const side = (points, color) => {
3701
+ const area = clampRect(options, x, y, w, h);
3702
+ if (!area)
3703
+ return;
3704
+ ctx.fillStyle = color;
3705
+ fillPolygon(ctx, points, area);
3706
+ };
3707
+ // the border box's far edges, and the padding box inside it
3708
+ const x1 = x + w;
3709
+ const y1 = y + h;
3710
+ const px0 = x + l;
3711
+ const py0 = y + t;
3712
+ const px1 = Math.max(px0, x1 - r);
3713
+ const py1 = Math.max(py0, y1 - b);
3714
+ // The left and the right are drawn after the top and the bottom. Where
3715
+ // one of them is opaque it draws its half of a cut corner over the side
3716
+ // drawn before, which keeps the whole corner: two halves each
3717
+ // antialiased along the diagonal would let what is under the border
3718
+ // show through between them.
3719
+ const over = (color) => color !== null && alphaOf(color) === 1;
3720
+ if (topInk !== null) {
3721
+ const cutLeft = tl && !over(leftInk);
3722
+ const cutRight = tr && !over(rightInk);
3723
+ if (cutLeft || cutRight) {
3724
+ side([x, y, x1, y, cutRight ? px1 : x1, py0, cutLeft ? px0 : x, py0], topInk);
3725
+ }
3726
+ else
3727
+ edge(x, y, w, t, topStyle, topInk, true);
3728
+ }
3729
+ if (bottomInk !== null) {
3730
+ const cutLeft = bl && !over(leftInk);
3731
+ const cutRight = br && !over(rightInk);
3732
+ if (cutLeft || cutRight) {
3733
+ side([x1, y1, x, y1, cutLeft ? px0 : x, py1, cutRight ? px1 : x1, py1], bottomInk);
3734
+ }
3735
+ else
3736
+ edge(x, y1 - b, w, b, bottomStyle, bottomInk, true);
2813
3737
  }
2814
- if (box.borderBottom > 0 && !isTransparent(s.borderBottomColor)) {
2815
- edge(x, y + h - box.borderBottom, w, box.borderBottom, s.borderBottomStyle, s.borderBottomColor, true);
3738
+ if (leftInk !== null) {
3739
+ if (tl || bl) {
3740
+ side([x, bl ? y1 : py1, x, tl ? y : py0, px0, py0, px0, py1], leftInk);
3741
+ }
3742
+ else
3743
+ edge(x, y + t, l, h - t - b, leftStyle, leftInk, false);
2816
3744
  }
2817
- if (box.borderLeft > 0 && !isTransparent(s.borderLeftColor)) {
2818
- edge(x, y + box.borderTop, box.borderLeft, h - box.borderTop - box.borderBottom, s.borderLeftStyle, s.borderLeftColor, false);
3745
+ if (rightInk !== null) {
3746
+ if (tr || br) {
3747
+ side([x1, tr ? y : py0, x1, br ? y1 : py1, px1, py1, px1, py0], rightInk);
3748
+ }
3749
+ else
3750
+ edge(x1 - r, y + t, r, h - t - b, rightStyle, rightInk, false);
2819
3751
  }
2820
- if (box.borderRight > 0 && !isTransparent(s.borderRightColor)) {
2821
- edge(x + w - box.borderRight, y + box.borderTop, box.borderRight, h - box.borderTop - box.borderBottom, s.borderRightStyle, s.borderRightColor, false);
3752
+ }
3753
+ /**
3754
+ * Whether the corner two sides meet at is cut on its diagonal, each side
3755
+ * taking its half (CSS Backgrounds 3, 4.3): where both have a width, they
3756
+ * are painted in different colours or only one is painted at all, and each
3757
+ * is solid or unpainted. Sides of one colour make the same corner either
3758
+ * way, and so does a corner of a single pixel, which is left to the side
3759
+ * that has it.
3760
+ */
3761
+ function mitred(a, b, inkA, inkB, styleA, styleB) {
3762
+ if (!(a > 0 && b > 0) || (a <= 1 && b <= 1) || inkA === inkB)
3763
+ return false;
3764
+ return ((inkA === null || styleA === 'solid') &&
3765
+ (inkB === null || styleB === 'solid'));
3766
+ }
3767
+ /**
3768
+ * Fill a convex polygon, its corners as `x, y` pairs, cut to `area`: the
3769
+ * part of it near what is painted, which is what keeps a side thousands of
3770
+ * pixels long inside X's coordinates. Cut edge by edge rather than by
3771
+ * moving its corners in, which would turn a diagonal that crosses the
3772
+ * painted area: a slanted divider, `border-left: 100vw solid transparent`,
3773
+ * has a corner far outside it.
3774
+ */
3775
+ function fillPolygon(ctx, points, area) {
3776
+ const bounds = [area.x, area.y, area.x + area.w, area.y + area.h];
3777
+ let cut = points;
3778
+ // each edge of the area in turn: what is inside it is kept, and an edge
3779
+ // of the polygon that crosses it ends on it (Sutherland–Hodgman)
3780
+ for (let edge = 0; edge < 4 && cut.length; edge += 1) {
3781
+ const axis = edge & 1;
3782
+ const bound = bounds[edge];
3783
+ const sign = edge < 2 ? 1 : -1;
3784
+ const from = cut;
3785
+ cut = [];
3786
+ for (let i = 0; i < from.length; i += 2) {
3787
+ const j = (i + 2) % from.length;
3788
+ const da = sign * (from[i + axis] - bound);
3789
+ const db = sign * (from[j + axis] - bound);
3790
+ if (da >= 0)
3791
+ cut.push(from[i], from[i + 1]);
3792
+ if (da < 0 !== db < 0) {
3793
+ const k = da / (da - db);
3794
+ const other = from[i + 1 - axis] + (from[j + 1 - axis] - from[i + 1 - axis]) * k;
3795
+ if (axis)
3796
+ cut.push(other, bound);
3797
+ else
3798
+ cut.push(bound, other);
3799
+ }
3800
+ }
2822
3801
  }
3802
+ if (cut.length < 6)
3803
+ return;
3804
+ ctx.beginPath();
3805
+ ctx.moveTo(cut[0], cut[1]);
3806
+ for (let i = 2; i < cut.length; i += 2)
3807
+ ctx.lineTo(cut[i], cut[i + 1]);
3808
+ ctx.closePath?.();
3809
+ ctx.fill();
2823
3810
  }
2824
3811
  /**
2825
3812
  * A box's border image in place of its border's style (CSS Backgrounds 3,
@@ -2874,9 +3861,10 @@ function paintBorderImage(ctx, box, options) {
2874
3861
  const drawn = options.cached?.(`border-image|${key}|${w}x${h}`, w, h, (sctx) => {
2875
3862
  if (svg)
2876
3863
  svg.draw(sctx, 0, 0, w, h, scale);
2877
- else if (typeof source !== 'string' && sctx.createLinearGradient) {
2878
- sctx.fillStyle = linearGradient(sctx, source, 0, 0, w, h, box.style.color);
2879
- sctx.fillRect(0, 0, w, h);
3864
+ else if (typeof source !== 'string') {
3865
+ const paint = gradientFill(sctx, source, 0, 0, w, h, box.style.color);
3866
+ if (paint)
3867
+ fillGradient(sctx, paint, { x: 0, y: 0, w, h });
2880
3868
  }
2881
3869
  });
2882
3870
  if (!drawn)
@@ -3509,7 +4497,9 @@ function fitted(box, x, y, w, h) {
3509
4497
  };
3510
4498
  }
3511
4499
  /** An inline `<svg>`, drawn in its content box. Its `currentColor` is the
3512
- * box's `color`, as an icon's is the text's around it. */
4500
+ * box's `color`, as an icon's is the text's around it, and what it is
4501
+ * painted with the box's `fill` and `stroke`, where the document's style
4502
+ * sheets set them: an icon set's `.icon { fill: currentColor }`. */
3513
4503
  function paintSvg(ctx, box, options) {
3514
4504
  if (!box.el)
3515
4505
  return;
@@ -3517,7 +4507,11 @@ function paintSvg(ctx, box, options) {
3517
4507
  const y = Math.round(box.contentY + options.originY);
3518
4508
  const w = Math.round(box.contentX + options.originX + box.contentWidth) - x;
3519
4509
  const h = Math.round(box.contentY + options.originY + box.contentHeight) - y;
3520
- inlineDrawing(box.el).draw(ctx, x, y, w, h, options.scale ?? 1, box.style.color);
4510
+ const style = box.style;
4511
+ const paint = (value) => value === null || value === 'currentColor' || value === 'none'
4512
+ ? value
4513
+ : inkColor(value, style.color);
4514
+ inlineDrawing(box.el).draw(ctx, x, y, w, h, options.scale ?? 1, style.color, paint(style.fill), paint(style.stroke));
3521
4515
  }
3522
4516
  /**
3523
4517
  * The X protocol carries glyph positions as Int16, so anything drawn past
@@ -3557,7 +4551,9 @@ function paintLines(ctx, box, options) {
3557
4551
  const above = ink?.above ?? 0;
3558
4552
  const below = ink?.below ?? 0;
3559
4553
  const slack = top - box.maxLineHeight - below;
3560
- while (lo < hi) {
4554
+ // the lines columns took apart are in no order down the page
4555
+ const apart = columned.any && COLUMN_LINES.has(lines);
4556
+ while (!apart && lo < hi) {
3561
4557
  const mid = (lo + hi) >> 1;
3562
4558
  if (lines[mid].y > slack)
3563
4559
  hi = mid;
@@ -3566,8 +4562,11 @@ function paintLines(ctx, box, options) {
3566
4562
  }
3567
4563
  for (let i = lo; i < lines.length; i += 1) {
3568
4564
  const line = lines[i];
3569
- if (line.y - above >= bottom)
4565
+ if (line.y - above >= bottom) {
4566
+ if (apart)
4567
+ continue;
3570
4568
  break;
4569
+ }
3571
4570
  if (line.y < bottom && line.y + line.height > top)
3572
4571
  visible.push(line);
3573
4572
  else if (ink && inkInto(line, top, bottom)) {
@@ -3601,8 +4600,13 @@ function paintLines(ctx, box, options) {
3601
4600
  paintInlineBoxes(ctx, line, lines, options, line === lines[0] ? null : bleeds);
3602
4601
  for (const text of line.texts) {
3603
4602
  const natural = text.layout.lines[text.layoutLine];
3604
- if (natural)
3605
- paintRunBackgrounds(ctx, natural, text.drawX + dx, text.drawY + dy, options.scale ?? 1);
4603
+ if (!natural)
4604
+ continue;
4605
+ for (const laid of [natural, trailOf(text, natural)]) {
4606
+ if (!laid)
4607
+ continue;
4608
+ paintRunBackgrounds(ctx, laid, text.drawX + dx, text.drawY + dy, options.scale ?? 1);
4609
+ }
3606
4610
  }
3607
4611
  paintSelection(ctx, line, options);
3608
4612
  }
@@ -3623,9 +4627,11 @@ function paintLines(ctx, box, options) {
3623
4627
  : null;
3624
4628
  for (const line of visible) {
3625
4629
  for (const text of line.texts) {
3626
- if (drawn.has(text.layout))
4630
+ // once for each column its lines are in, where they are in several
4631
+ const rows = columned.any ? COLUMN_ROWS.get(text) : undefined;
4632
+ if (drawn.has(rows ?? text.layout))
3627
4633
  continue;
3628
- drawn.add(text.layout);
4634
+ drawn.add(rows ?? text.layout);
3629
4635
  const top = text.drawY + dy;
3630
4636
  if (top < -COORD_LIMIT || top + text.layout.height > COORD_LIMIT) {
3631
4637
  // A single layout so tall its own lines overflow the Int16 envelope
@@ -3636,10 +4642,13 @@ function paintLines(ctx, box, options) {
3636
4642
  continue;
3637
4643
  }
3638
4644
  const bands = recolored?.get(text.layout);
4645
+ const clipped = rows !== undefined && clipToRows(ctx, text, rows, text.drawX + dx, top);
3639
4646
  if (bands)
3640
4647
  drawRecolored(ctx, text.layout, text.drawX + dx, top, bands);
3641
4648
  else
3642
4649
  text.layout.draw(ctx, text.drawX + dx, top);
4650
+ if (clipped)
4651
+ ctx.restore();
3643
4652
  }
3644
4653
  }
3645
4654
  paintRules(ctx, visible, dx, dy, options.scale ?? 1, 'over');
@@ -3653,7 +4662,10 @@ function paintLines(ctx, box, options) {
3653
4662
  for (const placed of line.atomics) {
3654
4663
  if (hoisted && HOISTED.has(placed.box))
3655
4664
  continue;
3656
- paintBox(ctx, placed.box, options);
4665
+ if (turns(placed.box))
4666
+ paintTransformed(ctx, placed.box, options);
4667
+ else
4668
+ paintBox(ctx, placed.box, options);
3657
4669
  }
3658
4670
  }
3659
4671
  }
@@ -3797,7 +4809,9 @@ function paintClippedText(ctx, block, lines, options) {
3797
4809
  (style.backgroundImages?.find((image) => image !== null && typeof image !== 'string') ||
3798
4810
  null);
3799
4811
  let fill = null;
3800
- if (gradient && ctx.createLinearGradient) {
4812
+ // where the fill is a picture of the gradient, the corner it starts at
4813
+ let origin = null;
4814
+ if (gradient) {
3801
4815
  if (!(area.width > 0 && area.height > 0))
3802
4816
  continue;
3803
4817
  // the text it shows through: what the fill is drawn over
@@ -3813,9 +4827,31 @@ function paintClippedText(ctx, block, lines, options) {
3813
4827
  y1 = Math.max(y1, r.y + r.height);
3814
4828
  }
3815
4829
  }
3816
- fill = linearGradient(ctx, gradient, area.x, area.y, area.width, area.height, style.color, x1 > x0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null);
4830
+ const paint = gradientFill(ctx, gradient, area.x, area.y, area.width, area.height, style.color, x1 > x0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null);
4831
+ if (paint?.matrix) {
4832
+ // An ellipse is a circle filled through a matrix, which would draw
4833
+ // the glyphs through it too: they are filled with a picture of the
4834
+ // gradient instead, drawn once at the area's size
4835
+ const left = Math.round(area.x);
4836
+ const top = Math.round(area.y);
4837
+ const w = Math.max(1, Math.round(area.x + area.width) - left);
4838
+ const h = Math.max(1, Math.round(area.y + area.height) - top);
4839
+ const picture = ctx.createPattern && ctx.translate
4840
+ ? options.cached?.(`clip-text|${gradientKey(gradient, style.color)}|${w}x${h}`, w, h, (sctx) => {
4841
+ const p = gradientFill(sctx, gradient, 0, 0, w, h, style.color);
4842
+ if (p)
4843
+ fillGradient(sctx, p, { x: 0, y: 0, w, h });
4844
+ })
4845
+ : null;
4846
+ if (!picture)
4847
+ continue;
4848
+ fill = ctx.createPattern(picture, 'no-repeat');
4849
+ origin = { x: left, y: top };
4850
+ }
4851
+ else if (paint)
4852
+ fill = paint.style;
3817
4853
  }
3818
- else if (!isTransparent(style.backgroundColor)) {
4854
+ if (fill === null && !isTransparent(style.backgroundColor)) {
3819
4855
  fill = inkColor(style.backgroundColor, style.color);
3820
4856
  }
3821
4857
  if (fill === null)
@@ -3830,7 +4866,13 @@ function paintClippedText(ctx, block, lines, options) {
3830
4866
  ctx.rect(r.x, r.y, r.width, r.height);
3831
4867
  ctx.clip();
3832
4868
  ctx.fillStyle = fill;
3833
- inkless.draw(ctx, part.x, part.y);
4869
+ if (origin) {
4870
+ // a pattern is placed from the origin of the space it fills
4871
+ ctx.translate(origin.x, origin.y);
4872
+ inkless.draw(ctx, part.x - origin.x, part.y - origin.y);
4873
+ }
4874
+ else
4875
+ inkless.draw(ctx, part.x, part.y);
3834
4876
  ctx.restore();
3835
4877
  }
3836
4878
  }
@@ -3861,11 +4903,16 @@ function paintTextShadows(ctx, lines, options) {
3861
4903
  continue;
3862
4904
  const left = text.drawX + dx;
3863
4905
  if (cast !== MIXED) {
3864
- // every run casts the same: the layout's once for each shadow
3865
- if (whole.has(layout))
4906
+ // every run casts the same: the layout's once for each shadow, in
4907
+ // each column its lines are in
4908
+ const rows = columned.any ? COLUMN_ROWS.get(text) : undefined;
4909
+ if (whole.has(rows ?? layout))
3866
4910
  continue;
3867
- whole.add(layout);
4911
+ whole.add(rows ?? layout);
4912
+ const clipped = rows !== undefined && clipToRows(ctx, text, rows, left, top);
3868
4913
  castShadows(ctx, text, left, top, cast, null);
4914
+ if (clipped)
4915
+ ctx.restore();
3869
4916
  continue;
3870
4917
  }
3871
4918
  const natural = layout.lines[text.layoutLine];
@@ -3976,12 +5023,37 @@ function paintRules(ctx, lines, dx, dy, scale, rules) {
3976
5023
  const shifted = SHIFTED_LINES.has(line);
3977
5024
  for (const text of line.texts) {
3978
5025
  const natural = text.layout.lines[text.layoutLine];
3979
- if (natural) {
3980
- paintRunRules(ctx, natural, text.drawX + dx, text.drawY + dy + (shifted ? ruleDrop(text) : 0), scale, rules);
5026
+ if (!natural)
5027
+ continue;
5028
+ for (const laid of [natural, trailOf(text, natural)]) {
5029
+ if (!laid)
5030
+ continue;
5031
+ paintRunRules(ctx, laid, text.drawX + dx, text.drawY + dy + (shifted ? ruleDrop(text) : 0), scale, rules);
3981
5032
  }
3982
5033
  }
3983
5034
  }
3984
5035
  }
5036
+ /**
5037
+ * The spaces a text ends on where its line goes on after them
5038
+ * (`LineText.trail`), as a line of one run to draw the decorations of: the
5039
+ * run they are the end of, as wide as they are, after the line's content.
5040
+ * The engine stripped them from the piece it laid out as a line, and a
5041
+ * link's underline stopped at its last letter, a space short of where a
5042
+ * browser draws it. Null where the text has none, or reads right to left.
5043
+ */
5044
+ function trailOf(text, natural) {
5045
+ if (!text.trail || !text.trailRun)
5046
+ return null;
5047
+ const last = natural.runs[natural.runs.length - 1];
5048
+ if (!last || last.run?.direction === 'rtl')
5049
+ return null;
5050
+ return {
5051
+ ...natural,
5052
+ runs: [
5053
+ { ...last, x: natural.width, width: text.trail, span: text.trailRun },
5054
+ ],
5055
+ };
5056
+ }
3985
5057
  /**
3986
5058
  * How far below a raised text its rules go: to the baseline of the element
3987
5059
  * that decorates it. `vertical-align` moves the text and not the lines an