@native-sdk/cli 0.5.2 → 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 (82) 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 +1 -0
  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 +109 -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 +25 -0
  32. package/src/primitives/canvas/ui.zig +35 -2
  33. package/src/primitives/canvas/ui_markup.zig +24 -0
  34. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  35. package/src/primitives/canvas/ui_markup_compiled.zig +40 -3
  36. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  37. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  38. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  39. package/src/primitives/canvas/ui_markup_tests.zig +26 -0
  40. package/src/primitives/canvas/ui_markup_view.zig +53 -4
  41. package/src/primitives/canvas/ui_markup_view_tests.zig +73 -2
  42. package/src/primitives/canvas/ui_schema.zig +16 -0
  43. package/src/primitives/canvas/ui_schema_tests.zig +10 -11
  44. package/src/primitives/canvas/ui_tests.zig +31 -0
  45. package/src/primitives/canvas/vector.zig +258 -141
  46. package/src/primitives/canvas/widget_access.zig +1 -1
  47. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  48. package/src/primitives/canvas/widget_layout.zig +9 -4
  49. package/src/primitives/canvas/widget_render.zig +78 -40
  50. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  51. package/src/primitives/canvas/widget_semantics.zig +6 -1
  52. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  53. package/src/primitives/canvas/widget_text_input.zig +118 -10
  54. package/src/primitives/canvas/widgets.zig +16 -0
  55. package/src/root.zig +5 -0
  56. package/src/runtime/api.zig +13 -0
  57. package/src/runtime/automation_commands.zig +33 -0
  58. package/src/runtime/automation_widget_dispatch.zig +64 -0
  59. package/src/runtime/canvas_font_tests.zig +272 -3
  60. package/src/runtime/canvas_fonts.zig +60 -20
  61. package/src/runtime/canvas_frame_helpers.zig +15 -0
  62. package/src/runtime/canvas_image_tests.zig +26 -0
  63. package/src/runtime/canvas_images.zig +33 -7
  64. package/src/runtime/canvas_limits.zig +44 -8
  65. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  66. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  67. package/src/runtime/canvas_widget_events.zig +24 -4
  68. package/src/runtime/canvas_widget_runtime.zig +24 -7
  69. package/src/runtime/canvas_widget_state.zig +6 -1
  70. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  71. package/src/runtime/core.zig +98 -6
  72. package/src/runtime/flow.zig +27 -0
  73. package/src/runtime/gpu_surface_events.zig +7 -0
  74. package/src/runtime/media_surface.zig +726 -0
  75. package/src/runtime/media_surface_tests.zig +1476 -0
  76. package/src/runtime/root.zig +9 -0
  77. package/src/runtime/session_journal.zig +30 -3
  78. package/src/runtime/session_tests.zig +126 -4
  79. package/src/runtime/tests.zig +1 -0
  80. package/src/runtime/ts_ui_app.zig +56 -0
  81. package/src/runtime/ui_app.zig +58 -0
  82. package/src/runtime/ui_app_tests.zig +334 -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
  }
@@ -3977,6 +3977,135 @@ test "short single-line values emit no clip and an unshifted origin" {
3977
3977
  }
3978
3978
  }
3979
3979
 
3980
+ test "a single-line value holding a line break renders one line inside the border" {
3981
+ // The defensive containment layer: edits can never insert a line
3982
+ // break into a single-line field (they sanitize at the derivation
3983
+ // seam), but a MODEL-SET value still can — a Zig core can put one
3984
+ // there today, as can an old journal or a host API write. The field
3985
+ // presents `\n`/`\r` as spaces (one line, byte-for-byte offsets) and
3986
+ // force-clips to its content rect, so the value never paints outside
3987
+ // the rounded border — identically in both render walks.
3988
+ const field = Widget{
3989
+ .id = 7,
3990
+ .kind = .input,
3991
+ .frame = geometry.RectF.init(10, 12, 200, 32),
3992
+ .text = "one\ntwo\r\nthree",
3993
+ .semantics = .{ .label = "Name" },
3994
+ };
3995
+
3996
+ var commands: [8]CanvasCommand = undefined;
3997
+ var builder = Builder.init(&commands);
3998
+ try emitWidgetTree(&builder, field, .{});
3999
+ try expectSingleLinePresentedField(builder.displayList(), field);
4000
+
4001
+ // Layout walk: the compiled and interpreted engines both emit
4002
+ // retained layouts through this walk, so containment pins here too.
4003
+ var nodes: [2]WidgetLayoutNode = undefined;
4004
+ const layout = try layoutWidgetTree(field, field.frame, &nodes);
4005
+ var layout_commands: [8]CanvasCommand = undefined;
4006
+ var layout_builder = Builder.init(&layout_commands);
4007
+ try layout.emitDisplayList(&layout_builder, .{});
4008
+ try expectSingleLinePresentedField(layout_builder.displayList(), field);
4009
+
4010
+ // A search field (the other emitter) contains the same value the
4011
+ // same way: presented bytes and a forced clip.
4012
+ var search = field;
4013
+ search.kind = .search_field;
4014
+ var search_commands: [32]CanvasCommand = undefined;
4015
+ var search_builder = Builder.init(&search_commands);
4016
+ try emitWidgetTree(&search_builder, search, .{});
4017
+ const search_list = search_builder.displayList();
4018
+ try std.testing.expect(search_list.findCommandById(widgetPartId(7, 7)) != null);
4019
+ switch (search_list.findCommandById(widgetPartId(7, 9)).?.command) {
4020
+ .draw_text => |text| try std.testing.expectEqualStrings("one two three", text.text),
4021
+ else => return error.TestUnexpectedResult,
4022
+ }
4023
+
4024
+ // A textarea keeps its raw line breaks: it is the one genuinely
4025
+ // multi-line editable kind.
4026
+ var textarea = field;
4027
+ textarea.kind = .textarea;
4028
+ textarea.frame = geometry.RectF.init(10, 12, 200, 120);
4029
+ var area_commands: [8]CanvasCommand = undefined;
4030
+ var area_builder = Builder.init(&area_commands);
4031
+ try emitWidgetTree(&area_builder, textarea, .{});
4032
+ switch (area_builder.displayList().findCommandById(widgetPartId(7, 3)).?.command) {
4033
+ .draw_text => |text| try std.testing.expectEqualStrings("one\ntwo\r\nthree", text.text),
4034
+ else => return error.TestUnexpectedResult,
4035
+ }
4036
+ }
4037
+
4038
+ fn expectSingleLinePresentedField(display_list: DisplayList, field: Widget) !void {
4039
+ // The content-rect clip is FORCED for a value holding a line break,
4040
+ // even though the presented value fits on one line.
4041
+ const viewport = textInputViewportForWidget(field, .{}).?;
4042
+ switch (display_list.findCommandById(widgetPartId(field.id, 16)).?.command) {
4043
+ .push_clip => |clip| try expectRectApprox(viewport, clip.rect),
4044
+ else => return error.TestUnexpectedResult,
4045
+ }
4046
+ switch (display_list.findCommandById(widgetPartId(field.id, 3)).?.command) {
4047
+ .draw_text => |text| {
4048
+ // `\n` and each byte of `\r\n` present as spaces — same
4049
+ // length, so caret/selection offsets address the raw value.
4050
+ try std.testing.expectEqualStrings("one two three", text.text);
4051
+ // The presented value lays out as exactly ONE line.
4052
+ var lines: [4]TextLine = undefined;
4053
+ const run = try layoutTextRun(text, text.text_layout.?, &lines);
4054
+ try std.testing.expectEqual(@as(usize, 1), run.lines.len);
4055
+ // ... and that line's painted bounds sit inside the field.
4056
+ const bounds = run.lines[0].bounds;
4057
+ try std.testing.expect(bounds.maxY() <= field.frame.maxY() + 0.001);
4058
+ try std.testing.expect(bounds.y >= field.frame.y - 0.001);
4059
+ },
4060
+ else => return error.TestUnexpectedResult,
4061
+ }
4062
+ }
4063
+
4064
+ test "presented single-line values survive later emits into any builder" {
4065
+ // The builder contract (the `allocPathElements` rule): a display
4066
+ // list may be HELD while another builder emits, and one builder may
4067
+ // ACCUMULATE several emit calls — either way every emitted command
4068
+ // stays intact. Presented bytes live in the BUILDER's own text
4069
+ // store, so a later presentation can never overwrite an emitted
4070
+ // draw_text the way a per-emit-reset scratch pool would (both
4071
+ // values here present to the same byte length, so a reset pool
4072
+ // would alias the first list's slice exactly).
4073
+ const first = Widget{
4074
+ .id = 7,
4075
+ .kind = .input,
4076
+ .frame = geometry.RectF.init(10, 12, 200, 32),
4077
+ .text = "one\ntwo",
4078
+ };
4079
+ var second = first;
4080
+ second.id = 8;
4081
+ second.text = "AAA\nBBB";
4082
+
4083
+ // Hold the first builder's list across a second builder's emit.
4084
+ var commands: [8]CanvasCommand = undefined;
4085
+ var builder = Builder.init(&commands);
4086
+ try emitWidgetTree(&builder, first, .{});
4087
+ var other_commands: [8]CanvasCommand = undefined;
4088
+ var other_builder = Builder.init(&other_commands);
4089
+ try emitWidgetTree(&other_builder, second, .{});
4090
+ try expectPresentedFieldText(builder.displayList(), 7, "one two");
4091
+ try expectPresentedFieldText(other_builder.displayList(), 8, "AAA BBB");
4092
+
4093
+ // Sequential emits accumulated into ONE builder keep both intact.
4094
+ var accumulated_commands: [16]CanvasCommand = undefined;
4095
+ var accumulated = Builder.init(&accumulated_commands);
4096
+ try emitWidgetTree(&accumulated, first, .{});
4097
+ try emitWidgetTree(&accumulated, second, .{});
4098
+ try expectPresentedFieldText(accumulated.displayList(), 7, "one two");
4099
+ try expectPresentedFieldText(accumulated.displayList(), 8, "AAA BBB");
4100
+ }
4101
+
4102
+ fn expectPresentedFieldText(display_list: DisplayList, id: ObjectId, expected: []const u8) !void {
4103
+ switch (display_list.findCommandById(widgetPartId(id, 3)).?.command) {
4104
+ .draw_text => |text| try std.testing.expectEqualStrings(expected, text.text),
4105
+ else => return error.TestUnexpectedResult,
4106
+ }
4107
+ }
4108
+
3980
4109
  test "search fields clip an overflowing value and keep chrome outside the clip" {
3981
4110
  const long_text = "an overflowing search query that runs past the narrow field";
3982
4111
  const field = Widget{