@native-sdk/cli 0.5.1 → 0.5.3

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 (98) hide show
  1. package/build.zig +41 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +3 -2
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +118 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +26 -0
  32. package/src/primitives/canvas/tokens.zig +170 -7
  33. package/src/primitives/canvas/ui.zig +62 -7
  34. package/src/primitives/canvas/ui_markup.zig +51 -4
  35. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  36. package/src/primitives/canvas/ui_markup_compiled.zig +49 -4
  37. package/src/primitives/canvas/ui_markup_compiled_tests.zig +160 -0
  38. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  39. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  40. package/src/primitives/canvas/ui_markup_tests.zig +73 -0
  41. package/src/primitives/canvas/ui_markup_view.zig +63 -5
  42. package/src/primitives/canvas/ui_markup_view_tests.zig +151 -2
  43. package/src/primitives/canvas/ui_schema.zig +31 -1
  44. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  45. package/src/primitives/canvas/ui_tests.zig +31 -0
  46. package/src/primitives/canvas/vector.zig +258 -141
  47. package/src/primitives/canvas/widget_access.zig +1 -1
  48. package/src/primitives/canvas/widget_builtin_tests.zig +259 -0
  49. package/src/primitives/canvas/widget_layout.zig +13 -5
  50. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  51. package/src/primitives/canvas/widget_render.zig +78 -40
  52. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  53. package/src/primitives/canvas/widget_semantics.zig +6 -1
  54. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  55. package/src/primitives/canvas/widget_text_input.zig +118 -10
  56. package/src/primitives/canvas/widget_tree.zig +14 -2
  57. package/src/primitives/canvas/widgets.zig +28 -0
  58. package/src/root.zig +5 -0
  59. package/src/runtime/api.zig +13 -0
  60. package/src/runtime/automation_commands.zig +33 -0
  61. package/src/runtime/automation_widget_dispatch.zig +159 -28
  62. package/src/runtime/canvas_font_tests.zig +272 -3
  63. package/src/runtime/canvas_fonts.zig +60 -20
  64. package/src/runtime/canvas_frame.zig +15 -0
  65. package/src/runtime/canvas_frame_helpers.zig +15 -0
  66. package/src/runtime/canvas_image_tests.zig +26 -0
  67. package/src/runtime/canvas_images.zig +33 -7
  68. package/src/runtime/canvas_limits.zig +44 -8
  69. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  70. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  71. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  72. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  73. package/src/runtime/canvas_widget_events.zig +1295 -7
  74. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  75. package/src/runtime/canvas_widget_runtime.zig +24 -7
  76. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  77. package/src/runtime/canvas_widget_state.zig +88 -6
  78. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  79. package/src/runtime/core.zig +112 -6
  80. package/src/runtime/flow.zig +51 -1
  81. package/src/runtime/gpu_surface_events.zig +51 -5
  82. package/src/runtime/media_surface.zig +726 -0
  83. package/src/runtime/media_surface_tests.zig +1476 -0
  84. package/src/runtime/root.zig +9 -0
  85. package/src/runtime/session_journal.zig +52 -13
  86. package/src/runtime/session_record.zig +166 -0
  87. package/src/runtime/session_tests.zig +575 -0
  88. package/src/runtime/tests.zig +1 -0
  89. package/src/runtime/ts_ui_app.zig +56 -0
  90. package/src/runtime/ui_app.zig +64 -1
  91. package/src/runtime/ui_app_tests.zig +794 -0
  92. package/src/runtime/view.zig +86 -0
  93. package/src/runtime/view_widget_text.zig +18 -1
  94. package/src/runtime/view_widget_tree.zig +339 -20
  95. package/src/runtime/widget_bridge.zig +26 -0
  96. package/src/runtime/window_storage.zig +61 -15
  97. package/src/runtime/window_view_runtime.zig +13 -0
  98. package/src/runtime/window_views.zig +6 -0
@@ -78,6 +78,64 @@ pub const max_curve_segments: usize = 48;
78
78
  pub const max_stroke_subpath_points: usize = 512;
79
79
  const max_piece_points: usize = 4 * max_curve_segments + 4;
80
80
 
81
+ // ---------------------------------------------------------------------------
82
+ // Glyph-fill raster budgets
83
+ // ---------------------------------------------------------------------------
84
+ //
85
+ // Font registration promises that a registered face ALWAYS resolves at
86
+ // render time — the glyph-budget gate (`font_ttf.Face.parse`) refuses
87
+ // over-budget faces loudly at registration so render never degrades a
88
+ // glyph to a block fallback. That promise is only true if the raster
89
+ // capacities cover the worst load the outline budgets admit, so these
90
+ // budgets are DERIVED from them instead of trusting real faces to be
91
+ // polite (`font_ttf` cannot be imported here — it imports this module —
92
+ // so the arithmetic is written out and lockstep-pinned by a test in
93
+ // font_ttf_tests.zig against the source constants).
94
+ //
95
+ // Worst admitted glyph (1024 points / 128 contours, simple and
96
+ // flattened-composite maxima are gated equal; glyph outlines emit only
97
+ // move/line/quad/close):
98
+ //
99
+ // quads <= points + contours = 1024 + 128 = 1152
100
+ // (each point emits at most one line or quad; each contour adds at
101
+ // most one closing quad — the same walk the glyph path capacity in
102
+ // reference.zig is derived from)
103
+ // edges <= quads * segments + contours = 1152 * 16 + 128 = 18,560
104
+ // (each quad flattens to at most `max_glyph_curve_segments` line
105
+ // segments, lines and the per-contour close edge are one each)
106
+ // scanline crossings <= edges = 18,560
107
+ // (an edge crosses a scanline at most once — the half-open span
108
+ // test in `sweep` — so the edge bound is also the airtight crossing
109
+ // bound, with no reliance on curve monotonicity under float
110
+ // sampling)
111
+ //
112
+ // The flattening multiplier is the honest quality decision. Nothing in
113
+ // the pipeline bounds text size (`max_raster_width` bounds the clipped
114
+ // sweep window, not the accumulated edges), so tolerance-exact
115
+ // flattening has no finite bound at all — a full-em quad at an
116
+ // S-pixel em wants ceil(sqrt((S/2) / 0.25)) segments (128 at S = 8192)
117
+ // — and even the generic `max_curve_segments` cap of 48 would put the
118
+ // worst case at 1152 * 48 + 128 = 55,424 edges = 1.06 MiB of edge
119
+ // storage plus 433 KiB of crossings per rasterizer: memory-unreasonable
120
+ // for a per-thread buffer. Flattening tolerance is a quality knob, not
121
+ // correctness, so glyph fills clamp subdivision instead: at 16 segments
122
+ // the clamp only binds when a curve's flatness deviation exceeds
123
+ // 0.25 * 16^2 = 64 px — a worst-case full-em quad only reaches that
124
+ // above a 128-px em — and past it the polyline deviates at most
125
+ // deviation / 16^2 <= (em/2) / 256 = em/512 from the true curve, 0.2%
126
+ // of the em at every size (0.5 px on a 256-px em). Real faces' quads
127
+ // span fractions of the em, so everything the golden suite renders
128
+ // stays below the clamp and byte-identical.
129
+ //
130
+ // Storage: 18,560 edges * 20 B = 362.5 KiB plus 18,560 crossings * 8 B
131
+ // = 145 KiB, ~508 KiB per `GlyphRasterizer`. The glyph raster caller
132
+ // (reference.zig) keeps ONE per render thread behind a lazily
133
+ // heap-allocated pointer (`canvas.lazy_tls`), so it costs neither stack
134
+ // nor static TLS, and threads that never ink a glyph never allocate it.
135
+ pub const max_glyph_curve_segments: usize = 16;
136
+ pub const max_glyph_fill_edges: usize = 18_560;
137
+ pub const max_glyph_scanline_crossings: usize = max_glyph_fill_edges;
138
+
81
139
  /// Half-open device-pixel clip window: pixels with `x0 <= x < x1` and
82
140
  /// `y0 <= y < y1` may be emitted.
83
141
  pub const ClipRect = struct {
@@ -327,29 +385,37 @@ fn rotate90(v: PointF, sweep: bool) PointF {
327
385
  /// the chord from the curve is bounded by the second difference over 4,
328
386
  /// and subdividing into n segments divides it by n^2.
329
387
  pub fn quadSegmentCount(p0: PointF, p1: PointF, p2: PointF, tolerance: f32) usize {
388
+ return quadSegmentCountLimited(p0, p1, p2, tolerance, max_curve_segments);
389
+ }
390
+
391
+ fn quadSegmentCountLimited(p0: PointF, p1: PointF, p2: PointF, tolerance: f32, segment_limit: usize) usize {
330
392
  const dx = @abs(p0.x - 2 * p1.x + p2.x);
331
393
  const dy = @abs(p0.y - 2 * p1.y + p2.y);
332
394
  const deviation = @max(dx, dy) * 0.25;
333
- return segmentCountForDeviation(deviation, tolerance);
395
+ return segmentCountForDeviation(deviation, tolerance, segment_limit);
334
396
  }
335
397
 
336
398
  /// Deterministic segment count for a cubic from the larger of its two
337
399
  /// second differences (conservative flatness bound).
338
400
  pub fn cubicSegmentCount(p0: PointF, p1: PointF, p2: PointF, p3: PointF, tolerance: f32) usize {
401
+ return cubicSegmentCountLimited(p0, p1, p2, p3, tolerance, max_curve_segments);
402
+ }
403
+
404
+ fn cubicSegmentCountLimited(p0: PointF, p1: PointF, p2: PointF, p3: PointF, tolerance: f32, segment_limit: usize) usize {
339
405
  const d1x = @abs(p0.x - 2 * p1.x + p2.x);
340
406
  const d1y = @abs(p0.y - 2 * p1.y + p2.y);
341
407
  const d2x = @abs(p1.x - 2 * p2.x + p3.x);
342
408
  const d2y = @abs(p1.y - 2 * p2.y + p3.y);
343
409
  const deviation = @max(@max(d1x, d1y), @max(d2x, d2y)) * 0.75;
344
- return segmentCountForDeviation(deviation, tolerance);
410
+ return segmentCountForDeviation(deviation, tolerance, segment_limit);
345
411
  }
346
412
 
347
- fn segmentCountForDeviation(deviation: f32, tolerance: f32) usize {
413
+ fn segmentCountForDeviation(deviation: f32, tolerance: f32, segment_limit: usize) usize {
348
414
  const tol = @max(0.01, tolerance);
349
415
  if (!(deviation > tol)) return 1;
350
416
  const count = @ceil(@sqrt(deviation / tol));
351
417
  if (!(count > 1)) return 1;
352
- if (count >= @as(f32, @floatFromInt(max_curve_segments))) return max_curve_segments;
418
+ if (count >= @as(f32, @floatFromInt(segment_limit))) return segment_limit;
353
419
  return @intFromFloat(count);
354
420
  }
355
421
 
@@ -372,7 +438,10 @@ fn cubicPoint(a: PointF, b: PointF, c: PointF, d: PointF, t: f32) PointF {
372
438
  /// Walk a path in device space (transform applied), flattening curves to
373
439
  /// line segments within `tolerance`, reporting subpaths to `sink`:
374
440
  /// `subpathBegin(point)`, `subpathPoint(point)`, `subpathEnd(closed)`.
375
- fn walkPath(elements: []const PathElement, transform: Affine, tolerance: f32, sink: anytype) Error!void {
441
+ /// `segment_limit` caps the per-curve flattening count
442
+ /// (`max_curve_segments` generically; `max_glyph_curve_segments` for
443
+ /// glyph fills — see the glyph budget derivation above).
444
+ fn walkPath(elements: []const PathElement, transform: Affine, tolerance: f32, segment_limit: usize, sink: anytype) Error!void {
376
445
  var has_current = false;
377
446
  var active = false;
378
447
  var current = PointF.zero();
@@ -406,7 +475,7 @@ fn walkPath(elements: []const PathElement, transform: Affine, tolerance: f32, si
406
475
  const control = transform.transformPoint(element.points[0]);
407
476
  const end = transform.transformPoint(element.points[1]);
408
477
  try ensureActive(sink, &active, current);
409
- const segments = quadSegmentCount(current, control, end, tolerance);
478
+ const segments = quadSegmentCountLimited(current, control, end, tolerance, segment_limit);
410
479
  var index: usize = 1;
411
480
  while (index <= segments) : (index += 1) {
412
481
  const t = @as(f32, @floatFromInt(index)) / @as(f32, @floatFromInt(segments));
@@ -420,7 +489,7 @@ fn walkPath(elements: []const PathElement, transform: Affine, tolerance: f32, si
420
489
  const control_b = transform.transformPoint(element.points[1]);
421
490
  const end = transform.transformPoint(element.points[2]);
422
491
  try ensureActive(sink, &active, current);
423
- const segments = cubicSegmentCount(current, control_a, control_b, end, tolerance);
492
+ const segments = cubicSegmentCountLimited(current, control_a, control_b, end, tolerance, segment_limit);
424
493
  var index: usize = 1;
425
494
  while (index <= segments) : (index += 1) {
426
495
  const t = @as(f32, @floatFromInt(index)) / @as(f32, @floatFromInt(segments));
@@ -459,134 +528,153 @@ const Edge = struct {
459
528
  dir: f32,
460
529
  };
461
530
 
462
- /// Fixed-capacity edge accumulator plus the anti-aliased scanline sweep.
463
- /// Instantiate on the stack (`var raster: Rasterizer = .{};`); it holds
464
- /// no pointers and is reusable via `resetEdges`.
465
- pub const Rasterizer = struct {
466
- edges: [max_edges]Edge = undefined,
467
- edge_count: usize = 0,
468
- min_x: f32 = 0,
469
- min_y: f32 = 0,
470
- max_x: f32 = 0,
471
- max_y: f32 = 0,
472
-
473
- pub fn resetEdges(self: *Rasterizer) void {
474
- self.edge_count = 0;
475
- }
531
+ /// Fixed-capacity edge accumulator plus the anti-aliased scanline sweep,
532
+ /// generic over its edge/crossing budgets: `Rasterizer` (the generic
533
+ /// path/icon budgets, stack-sized) and `GlyphRasterizer` (the derived
534
+ /// glyph budgets above, sized for a per-thread heap slot) are the two
535
+ /// instantiations. It holds no pointers and is reusable via `resetEdges`.
536
+ pub fn RasterizerType(comptime edge_capacity: usize, comptime crossing_capacity: usize) type {
537
+ return struct {
538
+ edges: [edge_capacity]Edge = undefined,
539
+ edge_count: usize = 0,
540
+ min_x: f32 = 0,
541
+ min_y: f32 = 0,
542
+ max_x: f32 = 0,
543
+ max_y: f32 = 0,
544
+ /// Scanline scratch for `sweep`, in the struct rather than its
545
+ /// stack frame so the glyph instantiation's 145 KiB rides the
546
+ /// same per-thread heap slot as its edges.
547
+ crossings: [crossing_capacity]Crossing = undefined,
548
+
549
+ const Self = @This();
476
550
 
477
- pub fn addEdge(self: *Rasterizer, a: PointF, b: PointF) Error!void {
478
- if (a.y == b.y) return;
479
- if (!std.math.isFinite(a.x) or !std.math.isFinite(a.y) or !std.math.isFinite(b.x) or !std.math.isFinite(b.y)) return;
480
- if (self.edge_count >= max_edges) return error.VectorPathTooComplex;
481
- if (self.edge_count == 0) {
482
- self.min_x = @min(a.x, b.x);
483
- self.max_x = @max(a.x, b.x);
484
- self.min_y = @min(a.y, b.y);
485
- self.max_y = @max(a.y, b.y);
486
- } else {
487
- self.min_x = @min(self.min_x, @min(a.x, b.x));
488
- self.max_x = @max(self.max_x, @max(a.x, b.x));
489
- self.min_y = @min(self.min_y, @min(a.y, b.y));
490
- self.max_y = @max(self.max_y, @max(a.y, b.y));
551
+ pub fn resetEdges(self: *Self) void {
552
+ self.edge_count = 0;
491
553
  }
492
- self.edges[self.edge_count] = .{ .x0 = a.x, .y0 = a.y, .x1 = b.x, .y1 = b.y, .dir = if (b.y > a.y) 1 else -1 };
493
- self.edge_count += 1;
494
- }
495
554
 
496
- /// Append a closed polygon ring with canonical orientation (pieces of
497
- /// a stroke union must all wind the same way for the nonzero rule).
498
- pub fn addPolygon(self: *Rasterizer, points: []const PointF) Error!void {
499
- if (points.len < 3) return;
500
- var area: f32 = 0;
501
- var index: usize = 0;
502
- while (index < points.len) : (index += 1) {
503
- const a = points[index];
504
- const b = points[(index + 1) % points.len];
505
- area += a.x * b.y - b.x * a.y;
555
+ pub fn addEdge(self: *Self, a: PointF, b: PointF) Error!void {
556
+ if (a.y == b.y) return;
557
+ if (!std.math.isFinite(a.x) or !std.math.isFinite(a.y) or !std.math.isFinite(b.x) or !std.math.isFinite(b.y)) return;
558
+ if (self.edge_count >= edge_capacity) return error.VectorPathTooComplex;
559
+ if (self.edge_count == 0) {
560
+ self.min_x = @min(a.x, b.x);
561
+ self.max_x = @max(a.x, b.x);
562
+ self.min_y = @min(a.y, b.y);
563
+ self.max_y = @max(a.y, b.y);
564
+ } else {
565
+ self.min_x = @min(self.min_x, @min(a.x, b.x));
566
+ self.max_x = @max(self.max_x, @max(a.x, b.x));
567
+ self.min_y = @min(self.min_y, @min(a.y, b.y));
568
+ self.max_y = @max(self.max_y, @max(a.y, b.y));
569
+ }
570
+ self.edges[self.edge_count] = .{ .x0 = a.x, .y0 = a.y, .x1 = b.x, .y1 = b.y, .dir = if (b.y > a.y) 1 else -1 };
571
+ self.edge_count += 1;
506
572
  }
507
- if (@abs(area) <= 0.000000001) return;
508
- if (area >= 0) {
509
- index = 0;
573
+
574
+ /// Append a closed polygon ring with canonical orientation (pieces of
575
+ /// a stroke union must all wind the same way for the nonzero rule).
576
+ pub fn addPolygon(self: *Self, points: []const PointF) Error!void {
577
+ if (points.len < 3) return;
578
+ var area: f32 = 0;
579
+ var index: usize = 0;
510
580
  while (index < points.len) : (index += 1) {
511
- try self.addEdge(points[index], points[(index + 1) % points.len]);
581
+ const a = points[index];
582
+ const b = points[(index + 1) % points.len];
583
+ area += a.x * b.y - b.x * a.y;
512
584
  }
513
- } else {
514
- index = points.len;
515
- while (index > 0) : (index -= 1) {
516
- try self.addEdge(points[index % points.len], points[index - 1]);
585
+ if (@abs(area) <= 0.000000001) return;
586
+ if (area >= 0) {
587
+ index = 0;
588
+ while (index < points.len) : (index += 1) {
589
+ try self.addEdge(points[index], points[(index + 1) % points.len]);
590
+ }
591
+ } else {
592
+ index = points.len;
593
+ while (index > 0) : (index -= 1) {
594
+ try self.addEdge(points[index % points.len], points[index - 1]);
595
+ }
517
596
  }
518
597
  }
519
- }
520
598
 
521
- /// Sweep the accumulated edges, emitting `sink.pixel(x, y, coverage)`
522
- /// for every pixel inside `clip` with coverage > 0. Coverage is in
523
- /// (0, 1]; `sub_samples` vertical subsamples, exact horizontal spans.
524
- pub fn sweep(self: *const Rasterizer, rule: FillRule, clip: ClipRect, sink: anytype) Error!void {
525
- if (self.edge_count == 0) return;
526
- const y_begin = @max(clip.y0, floorI(self.min_y));
527
- const y_end = @min(clip.y1, ceilI(self.max_y));
528
- const x_begin = @max(clip.x0, floorI(self.min_x));
529
- const x_end = @min(clip.x1, ceilI(self.max_x));
530
- if (x_end <= x_begin or y_end <= y_begin) return;
531
- const width: usize = @intCast(x_end - x_begin);
532
- if (width > max_raster_width) return error.VectorRasterTooWide;
533
-
534
- const x_origin = @as(f32, @floatFromInt(x_begin));
535
- const weight = 1.0 / @as(f32, @floatFromInt(sub_samples));
536
- var row: [max_raster_width]f32 = undefined;
537
-
538
- var y = y_begin;
539
- while (y < y_end) : (y += 1) {
540
- @memset(row[0..width], 0);
541
- var sub: usize = 0;
542
- while (sub < sub_samples) : (sub += 1) {
543
- const sy = @as(f32, @floatFromInt(y)) +
544
- (@as(f32, @floatFromInt(sub)) + 0.5) / @as(f32, @floatFromInt(sub_samples));
545
- var crossings: [max_scanline_crossings]Crossing = undefined;
546
- var crossing_count: usize = 0;
547
- for (self.edges[0..self.edge_count]) |edge| {
548
- const top = @min(edge.y0, edge.y1);
549
- const bottom = @max(edge.y0, edge.y1);
550
- if (!(sy >= top and sy < bottom)) continue;
551
- const x = edge.x0 + (sy - edge.y0) * (edge.x1 - edge.x0) / (edge.y1 - edge.y0);
552
- if (crossing_count >= max_scanline_crossings) return error.VectorPathTooComplex;
553
- crossings[crossing_count] = .{ .x = x, .dir = edge.dir };
554
- crossing_count += 1;
555
- }
556
- if (crossing_count == 0) continue;
557
- sortCrossings(crossings[0..crossing_count]);
558
-
559
- var winding: f32 = 0;
560
- var parity = false;
561
- var inside = false;
562
- var span_start: f32 = 0;
563
- for (crossings[0..crossing_count]) |crossing| {
564
- const was_inside = inside;
565
- switch (rule) {
566
- .nonzero => {
567
- winding += crossing.dir;
568
- inside = winding != 0;
569
- },
570
- .even_odd => {
571
- parity = !parity;
572
- inside = parity;
573
- },
599
+ /// Sweep the accumulated edges, emitting `sink.pixel(x, y, coverage)`
600
+ /// for every pixel inside `clip` with coverage > 0. Coverage is in
601
+ /// (0, 1]; `sub_samples` vertical subsamples, exact horizontal spans.
602
+ pub fn sweep(self: *Self, rule: FillRule, clip: ClipRect, sink: anytype) Error!void {
603
+ if (self.edge_count == 0) return;
604
+ const y_begin = @max(clip.y0, floorI(self.min_y));
605
+ const y_end = @min(clip.y1, ceilI(self.max_y));
606
+ const x_begin = @max(clip.x0, floorI(self.min_x));
607
+ const x_end = @min(clip.x1, ceilI(self.max_x));
608
+ if (x_end <= x_begin or y_end <= y_begin) return;
609
+ const width: usize = @intCast(x_end - x_begin);
610
+ if (width > max_raster_width) return error.VectorRasterTooWide;
611
+
612
+ const x_origin = @as(f32, @floatFromInt(x_begin));
613
+ const weight = 1.0 / @as(f32, @floatFromInt(sub_samples));
614
+ var row: [max_raster_width]f32 = undefined;
615
+
616
+ var y = y_begin;
617
+ while (y < y_end) : (y += 1) {
618
+ @memset(row[0..width], 0);
619
+ var sub: usize = 0;
620
+ while (sub < sub_samples) : (sub += 1) {
621
+ const sy = @as(f32, @floatFromInt(y)) +
622
+ (@as(f32, @floatFromInt(sub)) + 0.5) / @as(f32, @floatFromInt(sub_samples));
623
+ var crossing_count: usize = 0;
624
+ for (self.edges[0..self.edge_count]) |edge| {
625
+ const top = @min(edge.y0, edge.y1);
626
+ const bottom = @max(edge.y0, edge.y1);
627
+ if (!(sy >= top and sy < bottom)) continue;
628
+ const x = edge.x0 + (sy - edge.y0) * (edge.x1 - edge.x0) / (edge.y1 - edge.y0);
629
+ if (crossing_count >= crossing_capacity) return error.VectorPathTooComplex;
630
+ self.crossings[crossing_count] = .{ .x = x, .dir = edge.dir };
631
+ crossing_count += 1;
574
632
  }
575
- if (!was_inside and inside) {
576
- span_start = crossing.x;
577
- } else if (was_inside and !inside) {
578
- accumulateSpan(row[0..width], x_origin, span_start, crossing.x, weight);
633
+ if (crossing_count == 0) continue;
634
+ sortCrossings(self.crossings[0..crossing_count]);
635
+
636
+ var winding: f32 = 0;
637
+ var parity = false;
638
+ var inside = false;
639
+ var span_start: f32 = 0;
640
+ for (self.crossings[0..crossing_count]) |crossing| {
641
+ const was_inside = inside;
642
+ switch (rule) {
643
+ .nonzero => {
644
+ winding += crossing.dir;
645
+ inside = winding != 0;
646
+ },
647
+ .even_odd => {
648
+ parity = !parity;
649
+ inside = parity;
650
+ },
651
+ }
652
+ if (!was_inside and inside) {
653
+ span_start = crossing.x;
654
+ } else if (was_inside and !inside) {
655
+ accumulateSpan(row[0..width], x_origin, span_start, crossing.x, weight);
656
+ }
579
657
  }
580
658
  }
581
- }
582
- for (row[0..width], 0..) |coverage, i| {
583
- if (coverage > 0.0009) {
584
- sink.pixel(x_begin + @as(i32, @intCast(i)), y, @min(1, coverage));
659
+ for (row[0..width], 0..) |coverage, i| {
660
+ if (coverage > 0.0009) {
661
+ sink.pixel(x_begin + @as(i32, @intCast(i)), y, @min(1, coverage));
662
+ }
585
663
  }
586
664
  }
587
665
  }
588
- }
589
- };
666
+ };
667
+ }
668
+
669
+ /// The stack-friendly instantiation every generic path/icon fill and
670
+ /// stroke uses (~82 KiB).
671
+ pub const Rasterizer = RasterizerType(max_edges, max_scanline_crossings);
672
+
673
+ /// The glyph-fill instantiation, sized by the derived budgets above so
674
+ /// any outline the font registration gate admits rasterizes (~508 KiB —
675
+ /// callers keep one per thread on the heap, never on the stack; see
676
+ /// `fillGlyphPath`).
677
+ pub const GlyphRasterizer = RasterizerType(max_glyph_fill_edges, max_glyph_scanline_crossings);
590
678
 
591
679
  const Crossing = struct {
592
680
  x: f32,
@@ -648,6 +736,31 @@ pub fn fillPath(
648
736
  try raster.sweep(rule, clip, sink);
649
737
  }
650
738
 
739
+ /// Rasterize one glyph outline's fill through the derived glyph budgets
740
+ /// (see their derivation above): flattening clamps at
741
+ /// `max_glyph_curve_segments` and the edge/crossing capacities cover the
742
+ /// densest outline the font registration gate admits, so a
743
+ /// budget-admitted glyph NEVER fails with `VectorPathTooComplex` — the
744
+ /// registration promise ("a registered face always resolves at render
745
+ /// time") holds at the raster layer too. `raster` is caller-owned
746
+ /// storage (~508 KiB: keep one per thread on the heap, e.g. behind
747
+ /// `canvas.lazy_tls`, never on the stack); it is reset here, so it needs
748
+ /// no initialization beyond existing.
749
+ pub fn fillGlyphPath(
750
+ raster: *GlyphRasterizer,
751
+ elements: []const PathElement,
752
+ transform: Affine,
753
+ rule: FillRule,
754
+ tolerance: f32,
755
+ clip: ClipRect,
756
+ sink: anytype,
757
+ ) Error!void {
758
+ raster.resetEdges();
759
+ var edge_sink = FillEdgeSink(GlyphRasterizer){ .raster = raster };
760
+ try walkPath(elements, transform, tolerance, max_glyph_curve_segments, &edge_sink);
761
+ try raster.sweep(rule, clip, sink);
762
+ }
763
+
651
764
  /// Accumulate a filled path's edges into an existing rasterizer (lets a
652
765
  /// caller union several paths — e.g. all glyph contours of one text run —
653
766
  /// before one sweep).
@@ -657,31 +770,35 @@ pub fn accumulateFillEdges(
657
770
  transform: Affine,
658
771
  tolerance: f32,
659
772
  ) Error!void {
660
- var sink = FillEdgeSink{ .raster = raster };
661
- try walkPath(elements, transform, tolerance, &sink);
773
+ var sink = FillEdgeSink(Rasterizer){ .raster = raster };
774
+ try walkPath(elements, transform, tolerance, max_curve_segments, &sink);
662
775
  }
663
776
 
664
- const FillEdgeSink = struct {
665
- raster: *Rasterizer,
666
- start: PointF = PointF.zero(),
667
- current: PointF = PointF.zero(),
777
+ fn FillEdgeSink(comptime Raster: type) type {
778
+ return struct {
779
+ raster: *Raster,
780
+ start: PointF = PointF.zero(),
781
+ current: PointF = PointF.zero(),
668
782
 
669
- fn subpathBegin(self: *FillEdgeSink, point: PointF) Error!void {
670
- self.start = point;
671
- self.current = point;
672
- }
783
+ const Self = @This();
673
784
 
674
- fn subpathPoint(self: *FillEdgeSink, point: PointF) Error!void {
675
- try self.raster.addEdge(self.current, point);
676
- self.current = point;
677
- }
785
+ fn subpathBegin(self: *Self, point: PointF) Error!void {
786
+ self.start = point;
787
+ self.current = point;
788
+ }
678
789
 
679
- fn subpathEnd(self: *FillEdgeSink, closed: bool) Error!void {
680
- _ = closed;
681
- // Fill semantics always close the contour.
682
- try self.raster.addEdge(self.current, self.start);
683
- }
684
- };
790
+ fn subpathPoint(self: *Self, point: PointF) Error!void {
791
+ try self.raster.addEdge(self.current, point);
792
+ self.current = point;
793
+ }
794
+
795
+ fn subpathEnd(self: *Self, closed: bool) Error!void {
796
+ _ = closed;
797
+ // Fill semantics always close the contour.
798
+ try self.raster.addEdge(self.current, self.start);
799
+ }
800
+ };
801
+ }
685
802
 
686
803
  // ---------------------------------------------------------------------------
687
804
  // Stroke
@@ -719,7 +836,7 @@ pub fn accumulateStrokeEdges(
719
836
  .style = style,
720
837
  .tolerance = @max(0.01, tolerance),
721
838
  };
722
- try walkPath(elements, transform, tolerance, &sink);
839
+ try walkPath(elements, transform, tolerance, max_curve_segments, &sink);
723
840
  }
724
841
 
725
842
  const StrokeSink = struct {
@@ -75,7 +75,7 @@ pub fn isDragSource(widget: Widget) bool {
75
75
  /// widget a hit target, so `on_press` on a row/stack/icon works.
76
76
  pub fn widgetKindHitTarget(kind: WidgetKind) bool {
77
77
  return switch (kind) {
78
- .row, .column, .grid, .data_grid, .table, .data_row, .list, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group, .stack, .tooltip, .icon, .image, .avatar, .badge, .separator, .skeleton, .spinner, .chart, .split, .tree, .input_group => false,
78
+ .row, .column, .grid, .data_grid, .table, .data_row, .list, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group, .stack, .tooltip, .icon, .image, .avatar, .badge, .separator, .skeleton, .spinner, .chart, .split, .tree, .input_group, .media_surface => false,
79
79
  .scroll_view, .accordion, .alert, .bubble, .card, .dialog, .drawer, .sheet, .resizable, .panel, .popover, .menu_surface, .dropdown_menu, .text, .button, .toggle_button, .icon_button, .select, .input, .text_field, .search_field, .combobox, .textarea, .menu_item, .list_item, .data_cell, .status_bar, .segmented_control, .checkbox, .radio, .switch_control, .toggle, .slider, .progress, .split_divider => true,
80
80
  };
81
81
  }