ntk 8.14.6 → 8.14.7

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.
@@ -1899,11 +1899,12 @@ class RenderingContext2d {
1899
1899
  * well as the AddTraps: two requests become one, and the one that is left
1900
1900
  * touches only the bounding box.
1901
1901
  */
1902
- _uploadCoverage(job, b, out, edges) {
1902
+ _uploadCoverage(job, b, out, edges, route) {
1903
1903
  const rasterizer = this.window.app.rasterizer;
1904
1904
  if (!rasterizer) return false;
1905
- if (routeRaster(b.w, b.h, edges, this.window.app.rasterPolicy) !== "local")
1906
- return false;
1905
+ const where =
1906
+ route ?? routeRaster(b.w, b.h, edges, this.window.app.rasterPolicy);
1907
+ if (where !== "local") return false;
1907
1908
 
1908
1909
  // Rasterized over the whole box, as always, so a drawing that clips
1909
1910
  // split across passes — a pan's strips, a full repaint — gets the same
@@ -1978,6 +1979,84 @@ class RenderingContext2d {
1978
1979
  return out;
1979
1980
  }
1980
1981
 
1982
+ /**
1983
+ * A drawing's own extent: its box with the same pixel of slack as
1984
+ * `_clampBBox`, and *not* clamped to the surface — what its masks are
1985
+ * clustered and routed by (`_fillPolys`, the stroke's mask path).
1986
+ *
1987
+ * Which rasterizer draws a mask — ours or the server's — has to be a
1988
+ * property of the drawing, never of where it lands. The two antialias
1989
+ * differently, so a route taken from the part the surface shows changed
1990
+ * as the drawing moved: a stroke whose cap had just left the surface had
1991
+ * three triangles fewer to count, went to the other rasterizer, and came
1992
+ * out up to ten levels different along its whole length — a pan's copy
1993
+ * of it one way, the strip beside it the other. Clamping and culling
1994
+ * decide how much work a mask is, never how it is drawn.
1995
+ */
1996
+ _rawBox(minX, minY, maxX, maxY) {
1997
+ const x = Math.floor(minX) - 1;
1998
+ const y = Math.floor(minY) - 1;
1999
+ return { x, y, w: Math.ceil(maxX) + 1 - x, h: Math.ceil(maxY) + 1 - y };
2000
+ }
2001
+
2002
+ /** `_rawBox` of one flat `[x0, y0, …]` point list; null for none. */
2003
+ _pointsExtent(pts) {
2004
+ let minX = Infinity;
2005
+ let minY = Infinity;
2006
+ let maxX = -Infinity;
2007
+ let maxY = -Infinity;
2008
+ for (let i = 0; i < pts.length; i += 2) {
2009
+ if (pts[i] < minX) minX = pts[i];
2010
+ if (pts[i] > maxX) maxX = pts[i];
2011
+ if (pts[i + 1] < minY) minY = pts[i + 1];
2012
+ if (pts[i + 1] > maxY) maxY = pts[i + 1];
2013
+ }
2014
+ if (!(maxX >= minX && maxY >= minY)) return null;
2015
+ return this._rawBox(minX, minY, maxX, maxY);
2016
+ }
2017
+
2018
+ /** `_rawBox` over every subpath of a flattened path; null for none. */
2019
+ _polysExtent(polys) {
2020
+ let out = null;
2021
+ for (const poly of polys) {
2022
+ const b = this._pointsExtent(poly.pts);
2023
+ if (!b) continue;
2024
+ out = out ? unionBox(out, b) : b;
2025
+ }
2026
+ return out;
2027
+ }
2028
+
2029
+ /**
2030
+ * Where a drawing whose own box is `box` is rasterized (`routeRaster`),
2031
+ * sized by as much of it as the surface could ever show — each side no
2032
+ * longer than the surface's, and its edges in the same proportion —
2033
+ * rather than by the part it shows now: a route that depends only on the
2034
+ * drawing's size and complexity is the same wherever the drawing lands
2035
+ * (`_rawBox`). Counting every edge of a stroke that runs far past the
2036
+ * surface sent it through the mask more often than the part it shows
2037
+ * earned: a fifth more bytes a frame for a hundred such strokes.
2038
+ */
2039
+ _routeFor(box, edges) {
2040
+ const w = Math.min(box.w, this.width);
2041
+ const h = Math.min(box.h, this.height);
2042
+ return routeRaster(
2043
+ w,
2044
+ h,
2045
+ (edges * w * h) / (box.w * box.h),
2046
+ this.window.app.rasterPolicy,
2047
+ );
2048
+ }
2049
+
2050
+ /** the part of a box on the surface; null where there is none */
2051
+ _onSurface(b) {
2052
+ const x = Math.max(0, b.x);
2053
+ const y = Math.max(0, b.y);
2054
+ const w = Math.min(this.width, b.x + b.w) - x;
2055
+ const h = Math.min(this.height, b.y + b.h) - y;
2056
+ if (w <= 0 || h <= 0) return null;
2057
+ return { x, y, w, h };
2058
+ }
2059
+
1981
2060
  // a pixel of slack for the antialiased edge, clamped to the surface
1982
2061
  _clampBBox(minX, minY, maxX, maxY) {
1983
2062
  const x = Math.max(0, Math.floor(minX) - 1);
@@ -2030,7 +2109,7 @@ class RenderingContext2d {
2030
2109
  *
2031
2110
  * @returns {boolean} whether anything was composited
2032
2111
  */
2033
- _paintThroughMask(b, job, { edges, src, op, alpha, server }) {
2112
+ _paintThroughMask(b, job, { edges, route, src, op, alpha, server }) {
2034
2113
  const R = this.Render;
2035
2114
  // Everything is bounded to the part of the box the clip lets through:
2036
2115
  // the clip's rectangle, or the extents of a stack with a path in it —
@@ -2049,7 +2128,9 @@ class RenderingContext2d {
2049
2128
  if (!out) return false;
2050
2129
  }
2051
2130
  if (this._hasPolyClip) this._requireClipMask();
2052
- if (!this._uploadCoverage({ ...job, dx: -b.x, dy: -b.y }, b, out, edges)) {
2131
+ if (
2132
+ !this._uploadCoverage({ ...job, dx: -b.x, dy: -b.y }, b, out, edges, route)
2133
+ ) {
2053
2134
  R.FillRectangles(
2054
2135
  R.PictOp.Src,
2055
2136
  this.fillMask.id,
@@ -2118,12 +2199,13 @@ class RenderingContext2d {
2118
2199
  if (!prepareStyle(src, this._m)) return;
2119
2200
 
2120
2201
  // one box per subpath, so a path holding disjoint ones can be masked as
2121
- // the pieces it is rather than as the box around all of them
2202
+ // the pieces it is rather than as the box around all of them — each the
2203
+ // subpath's own box, on the surface or not (`_rawBox`)
2122
2204
  const shapes = [];
2123
2205
  const pieces = [];
2124
2206
  for (const p of polys) {
2125
2207
  if (p.pts.length < 6) continue;
2126
- const b = this._pointsBBox(p.pts);
2208
+ const b = this._pointsExtent(p.pts);
2127
2209
  if (!b) continue;
2128
2210
  shapes.push(p.pts);
2129
2211
  pieces.push(b);
@@ -2133,18 +2215,30 @@ class RenderingContext2d {
2133
2215
  this._ensureFillMask();
2134
2216
  let painted = false;
2135
2217
  for (const cluster of this._maskClusters(pieces, op)) {
2218
+ // Routed by every subpath of the cluster, and rasterized from the
2219
+ // ones the surface shows, over the part of them it shows: a subpath
2220
+ // wholly off it adds nothing to the winding of a pixel outside its
2221
+ // own box.
2136
2222
  const flat = [];
2223
+ let box = null;
2137
2224
  let edges = 0;
2225
+ let drawn = 0;
2138
2226
  for (const i of cluster.items) {
2139
- flat.push(shapes[i]);
2140
2227
  edges += shapes[i].length / 2;
2228
+ const shown = this._onSurface(pieces[i]);
2229
+ if (!shown) continue;
2230
+ flat.push(shapes[i]);
2231
+ drawn += shapes[i].length / 2;
2232
+ box = box ? unionBox(box, shown) : shown;
2141
2233
  }
2234
+ if (!box) continue;
2142
2235
  painted =
2143
2236
  this._paintThroughMask(
2144
- cluster,
2237
+ box,
2145
2238
  { polys: flat, rule },
2146
2239
  {
2147
- edges,
2240
+ edges: drawn,
2241
+ route: this._routeFor(cluster, edges),
2148
2242
  src,
2149
2243
  op,
2150
2244
  alpha,
@@ -2497,28 +2591,12 @@ class RenderingContext2d {
2497
2591
  // cuts above are meant to keep the extruder off that path; this keeps
2498
2592
  // a future miss from being catastrophic. A sum is finite iff every
2499
2593
  // term is.
2500
- //
2501
- // A triangle wholly off the surface is left out as well: it adds
2502
- // nothing on either route — the rasterizer would walk its rows only for
2503
- // its edges to cancel on the border column, and the server would be
2504
- // sent it only to clip it away — and a zoomed graph has hundreds of
2505
- // edges past the window every frame.
2506
- const right = this.width + 1;
2507
- const bottom = this.height + 1;
2508
2594
  let w = 0;
2509
2595
  for (let i = 0; i < tris.length; i += 6) {
2510
- const ax = tris[i];
2511
- const ay = tris[i + 1];
2512
- const bx = tris[i + 2];
2513
- const by = tris[i + 3];
2514
- const cx = tris[i + 4];
2515
- const cy = tris[i + 5];
2516
- if (!Number.isFinite(ax + ay + bx + by + cx + cy)) continue;
2517
2596
  if (
2518
- (ax < -1 && bx < -1 && cx < -1) ||
2519
- (ay < -1 && by < -1 && cy < -1) ||
2520
- (ax > right && bx > right && cx > right) ||
2521
- (ay > bottom && by > bottom && cy > bottom)
2597
+ !Number.isFinite(
2598
+ tris[i] + tris[i + 1] + tris[i + 2] + tris[i + 3] + tris[i + 4] + tris[i + 5],
2599
+ )
2522
2600
  ) {
2523
2601
  continue;
2524
2602
  }
@@ -2527,19 +2605,6 @@ class RenderingContext2d {
2527
2605
  }
2528
2606
  if (w < tris.length) tris.length = w;
2529
2607
  if (!tris.length) return;
2530
- // Triangles are 16.16 fixed point, and the source origin a direct stroke
2531
- // hands the server is 16 bits: a stroke that reaches that far is cut to
2532
- // the surface first (lib/cliprect.js), a pixel to spare on every side, so
2533
- // what the surface shows is drawn exactly as before. One that does not
2534
- // goes as it came, for the server to clip.
2535
- const edgeX = this.width + 1;
2536
- const edgeY = this.height + 1;
2537
- if (!withinRect(tris, -WIRE_REACH, -WIRE_REACH, WIRE_REACH, WIRE_REACH)) {
2538
- const cut = clipTrianglesToRect(tris, -1, -1, edgeX, edgeY);
2539
- tris.length = 0;
2540
- for (let i = 0; i < cut.length; i++) tris.push(cut[i]);
2541
- if (!tris.length) return;
2542
- }
2543
2608
 
2544
2609
  const op = this._op();
2545
2610
  // round-cap/join disks overlap the stroke body; overlapping coverage
@@ -2555,6 +2620,14 @@ class RenderingContext2d {
2555
2620
  op === this.Render.PictOp.Over;
2556
2621
  const chunk = 4000 * 6;
2557
2622
  if (direct) {
2623
+ this._cullTris(tris);
2624
+ if (!tris.length) return;
2625
+ const cut = this._wireCut(tris);
2626
+ if (cut !== tris) {
2627
+ tris.length = 0;
2628
+ for (let i = 0; i < cut.length; i++) tris.push(cut[i]);
2629
+ if (!tris.length) return;
2630
+ }
2558
2631
  for (let i = 0; i < tris.length; i += chunk) {
2559
2632
  const batch = tris.slice(i, i + chunk);
2560
2633
  // RENDER aligns the source with the *first triangle's first vertex*,
@@ -2588,24 +2661,31 @@ class RenderingContext2d {
2588
2661
  // and took 1.9 s on XQuartz where the bounded version takes 37 ms
2589
2662
  // (react-x11#148). Stale mask content outside the boxes is never
2590
2663
  // composited, so clearing only them is safe.
2664
+ //
2665
+ // The islands are the whole stroke's, on the surface or not, and so are
2666
+ // the clusters and the route each is drawn by (`_rawBox`); only the
2667
+ // triangles the surface shows are rasterized.
2591
2668
  this._ensureFillMask();
2592
2669
  const pieces = this._trisPieces(tris);
2593
2670
  if (!pieces.length) return;
2594
- // the soup itself when it is one piece holding every triangle — the
2595
- // common case (one polyline is one piece), and worth not copying
2596
- const whole =
2597
- pieces.length === 1 &&
2598
- pieces[0].start === 0 &&
2599
- pieces[0].end === tris.length;
2600
2671
  let painted = false;
2601
2672
  for (const cluster of this._maskClusters(pieces, op)) {
2602
- const batch = whole ? tris : this._trisOf(tris, pieces, cluster.items);
2673
+ let edges = 0;
2674
+ for (const i of cluster.items) {
2675
+ edges += (pieces[i].end - pieces[i].start) / 2;
2676
+ }
2677
+ const batch = this._trisBatch(tris, pieces, cluster);
2678
+ if (!batch.length) continue;
2679
+ // the mask over what is drawn, as tight as it always was
2680
+ const box = this._pointsBBox(batch);
2681
+ if (!box) continue;
2603
2682
  painted =
2604
2683
  this._paintThroughMask(
2605
- cluster,
2684
+ box,
2606
2685
  { triangles: batch },
2607
2686
  {
2608
2687
  edges: batch.length / 2,
2688
+ route: this._routeFor(cluster, edges),
2609
2689
  src,
2610
2690
  op,
2611
2691
  alpha: this.globalAlpha,
@@ -2642,17 +2722,17 @@ class RenderingContext2d {
2642
2722
  * linear pre-pass whose only job is to hand the clustering a handful of
2643
2723
  * boxes instead of thousands of triangles.
2644
2724
  *
2645
- * A piece that misses the surface entirely is dropped, leaving a hole in
2646
- * the spans — which is why the caller gathers a batch from the spans it
2647
- * has rather than assuming they tile `tris`.
2725
+ * Each piece is its own box (`_rawBox`), on the surface or not: the
2726
+ * clusters and the routes are the stroke's, and the surface only bounds
2727
+ * the work (`_trisBatch`).
2648
2728
  */
2649
2729
  _trisPieces(tris) {
2650
2730
  const pieces = [];
2651
2731
  let open = null;
2652
2732
  const close = () => {
2653
2733
  if (!open) return;
2654
- const b = this._clampBBox(open.minX, open.minY, open.maxX, open.maxY);
2655
- if (b) pieces.push({ ...b, start: open.start, end: open.end });
2734
+ const b = this._rawBox(open.minX, open.minY, open.maxX, open.maxY);
2735
+ pieces.push({ ...b, start: open.start, end: open.end });
2656
2736
  open = null;
2657
2737
  };
2658
2738
  for (let i = 0; i < tris.length; i += 6) {
@@ -2681,17 +2761,81 @@ class RenderingContext2d {
2681
2761
  return pieces;
2682
2762
  }
2683
2763
 
2684
- /** the triangles of the given pieces, gathered into one soup */
2685
- _trisOf(tris, pieces, items) {
2686
- let n = 0;
2687
- for (const i of items) n += pieces[i].end - pieces[i].start;
2688
- const out = new Array(n);
2689
- let w = 0;
2764
+ /**
2765
+ * One cluster's triangles, gathered from its pieces: less those wholly
2766
+ * off the surface, which add nothing on either route — the rasterizer
2767
+ * would walk their rows only for their edges to cancel on the border
2768
+ * column, and the server would be sent them only to clip them away — and
2769
+ * cut to the wire's reach (`_wireCut`). The soup itself when the cluster
2770
+ * is all of it and all of it is on the surface: the common case, one
2771
+ * polyline in view, and worth not copying.
2772
+ */
2773
+ _trisBatch(tris, pieces, cluster) {
2774
+ const { items } = cluster;
2775
+ const inside =
2776
+ cluster.x >= -1 &&
2777
+ cluster.y >= -1 &&
2778
+ cluster.x + cluster.w <= this.width + 1 &&
2779
+ cluster.y + cluster.h <= this.height + 1;
2780
+ if (
2781
+ inside &&
2782
+ items.length === 1 &&
2783
+ pieces[items[0]].start === 0 &&
2784
+ pieces[items[0]].end === tris.length
2785
+ ) {
2786
+ return this._wireCut(tris);
2787
+ }
2788
+ const out = [];
2690
2789
  for (const i of items) {
2691
- const piece = pieces[i];
2692
- for (let k = piece.start; k < piece.end; ++k) out[w++] = tris[k];
2790
+ for (let k = pieces[i].start; k < pieces[i].end; ++k) out.push(tris[k]);
2693
2791
  }
2694
- return out;
2792
+ if (!inside) this._cullTris(out);
2793
+ return this._wireCut(out);
2794
+ }
2795
+
2796
+ /**
2797
+ * Leave out, in place, every triangle wholly off the surface: nothing of
2798
+ * it can land on it, and a zoomed graph has hundreds of edges past the
2799
+ * window every frame.
2800
+ */
2801
+ _cullTris(tris) {
2802
+ const right = this.width + 1;
2803
+ const bottom = this.height + 1;
2804
+ let w = 0;
2805
+ for (let i = 0; i < tris.length; i += 6) {
2806
+ const ax = tris[i];
2807
+ const ay = tris[i + 1];
2808
+ const bx = tris[i + 2];
2809
+ const by = tris[i + 3];
2810
+ const cx = tris[i + 4];
2811
+ const cy = tris[i + 5];
2812
+ if (
2813
+ (ax < -1 && bx < -1 && cx < -1) ||
2814
+ (ay < -1 && by < -1 && cy < -1) ||
2815
+ (ax > right && bx > right && cx > right) ||
2816
+ (ay > bottom && by > bottom && cy > bottom)
2817
+ ) {
2818
+ continue;
2819
+ }
2820
+ if (w < i) for (let k = 0; k < 6; k++) tris[w + k] = tris[i + k];
2821
+ w += 6;
2822
+ }
2823
+ if (w < tris.length) tris.length = w;
2824
+ return tris;
2825
+ }
2826
+
2827
+ /**
2828
+ * Triangles are 16.16 fixed point, and the source origin a direct stroke
2829
+ * hands the server is 16 bits: a stroke that reaches that far is cut to
2830
+ * the surface first (lib/cliprect.js), a pixel to spare on every side, so
2831
+ * what the surface shows is drawn exactly as before. One that does not
2832
+ * goes as it came, for the server to clip — the same array.
2833
+ */
2834
+ _wireCut(tris) {
2835
+ if (withinRect(tris, -WIRE_REACH, -WIRE_REACH, WIRE_REACH, WIRE_REACH)) {
2836
+ return tris;
2837
+ }
2838
+ return clipTrianglesToRect(tris, -1, -1, this.width + 1, this.height + 1);
2695
2839
  }
2696
2840
 
2697
2841
  /**
@@ -4129,11 +4273,10 @@ class RenderingContext2d {
4129
4273
  if (!rasterizer) return false;
4130
4274
  const { shapes, edges } = fillableShapes(entry.polys);
4131
4275
  if (!shapes.length) return false;
4132
- if (
4133
- routeRaster(bb.w, bb.h, edges, this.window.app.rasterPolicy) !== "local"
4134
- ) {
4276
+ // routed by the clip's own extent, as a fill is (`_rawBox`), and
4277
+ // rasterized over the part of it on the surface
4278
+ if (this._routeFor(this._polysExtent(entry.polys) ?? bb, edges) !== "local")
4135
4279
  return false;
4136
- }
4137
4280
 
4138
4281
  const coverage = rasterizer.rasterize({
4139
4282
  polys: shapes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ntk",
3
- "version": "8.14.6",
3
+ "version": "8.14.7",
4
4
  "description": "Desktop UI toolkit for X11 with canvas-like 2d and OpenGL rendering",
5
5
  "author": "Andrey Sidorov <sidorares@yandex.ru>",
6
6
  "license": "MIT",