@native-sdk/cli 0.4.1 → 0.4.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 (48) hide show
  1. package/assets/native-sdk.manifest +17 -1
  2. package/build/app.zig +40 -1
  3. package/build.zig +49 -3
  4. package/package.json +10 -9
  5. package/src/platform/macos/appkit_host.m +21 -6
  6. package/src/platform/windows/webview2_host.cpp +377 -72
  7. package/src/primitives/canvas/chart.zig +8 -7
  8. package/src/primitives/canvas/chart_tests.zig +2 -2
  9. package/src/primitives/canvas/commands.zig +27 -0
  10. package/src/primitives/canvas/gpu.zig +8 -0
  11. package/src/primitives/canvas/markdown_tests.zig +1 -1
  12. package/src/primitives/canvas/reference.zig +215 -7
  13. package/src/primitives/canvas/reference_tests.zig +326 -7
  14. package/src/primitives/canvas/root.zig +3 -0
  15. package/src/primitives/canvas/serialization.zig +16 -3
  16. package/src/primitives/canvas/test_support.zig +3 -0
  17. package/src/primitives/canvas/themes/geist.zig +6 -0
  18. package/src/primitives/canvas/tokens.zig +7 -0
  19. package/src/primitives/canvas/widget_builtin_tests.zig +439 -3
  20. package/src/primitives/canvas/widget_render.zig +29 -41
  21. package/src/primitives/canvas/widget_render_controls.zig +73 -43
  22. package/src/primitives/canvas/widget_render_style.zig +53 -0
  23. package/src/primitives/canvas/widget_render_surfaces.zig +19 -18
  24. package/src/primitives/canvas/widget_runtime.zig +3 -0
  25. package/src/primitives/canvas/widget_runtime_tests.zig +20 -21
  26. package/src/primitives/canvas/widget_text_input.zig +104 -1
  27. package/src/runtime/canvas_frame_packet_tests.zig +9 -1
  28. package/src/runtime/canvas_frame_patch_tests.zig +39 -1
  29. package/src/runtime/canvas_widget_control_tests.zig +4 -4
  30. package/src/runtime/canvas_widget_runtime.zig +23 -3
  31. package/src/runtime/canvas_widget_text_tests.zig +101 -0
  32. package/src/runtime/ui_app.zig +78 -10
  33. package/src/runtime/ui_app_tests.zig +109 -0
  34. package/src/runtime/ui_app_window_tests.zig +93 -0
  35. package/src/runtime/view.zig +1 -1
  36. package/src/runtime/view_widget_scroll.zig +12 -1
  37. package/src/runtime/view_widget_text.zig +6 -2
  38. package/src/tooling/buildgraph.zig +112 -2
  39. package/src/tooling/junction.zig +247 -0
  40. package/src/tooling/package.zig +45 -0
  41. package/src/tooling/root.zig +1 -0
  42. package/src/tooling/templates.zig +102 -2
  43. package/third_party/webview2/LICENSE.txt +27 -0
  44. package/third_party/webview2/README.md +19 -0
  45. package/third_party/webview2/arm64/WebView2Loader.dll +0 -0
  46. package/third_party/webview2/include/EventToken.h +12 -0
  47. package/third_party/webview2/include/WebView2.h +60636 -0
  48. package/third_party/webview2/x64/WebView2Loader.dll +0 -0
@@ -14,7 +14,7 @@
14
14
  //! Budgets: series points are bounded at `max_chart_points_per_series`
15
15
  //! per series (the `Ui.chart` builder downsamples anything longer, so a
16
16
  //! 10k-point star-history series renders instead of erroring), and the
17
- //! per-frame path-element scratch in the widget renderer is bounded at
17
+ //! display-list builder's path-element store is bounded at
18
18
  //! `max_chart_path_elements_per_frame`, mirroring the runtime's per-view
19
19
  //! `max_canvas_path_elements_per_view` budget (a lockstep test keeps the
20
20
  //! two from drifting).
@@ -32,9 +32,10 @@ pub const ChartSeriesColor = text_spans_model.TextSpanColor;
32
32
  /// pass through verbatim.
33
33
  pub const max_chart_points_per_series: usize = 256;
34
34
 
35
- /// Per-frame path-element scratch budget for chart rendering (the widget
36
- /// renderer builds line/band path elements into threadlocal scratch that
37
- /// must survive until the runtime copies the display list). Mirrors the
35
+ /// Path-element budget for the display-list builder's own element store
36
+ /// (the widget renderer builds chart line/band elements — and the
37
+ /// spinner's and checkbox's — directly into the builder, so the emitted
38
+ /// commands own their geometry for the builder's lifetime). Mirrors the
38
39
  /// runtime's `canvas_limits.max_canvas_path_elements_per_view`: a frame
39
40
  /// that would exceed this fails loudly with `ChartPathElementListFull`,
40
41
  /// exactly when the per-view path budget would have refused the copy
@@ -44,9 +45,9 @@ pub const max_chart_points_per_series: usize = 256;
44
45
  pub const max_chart_path_elements_per_frame: usize = 2048;
45
46
 
46
47
  /// Per-frame byte budget for the formatted tick/tooltip label scratch
47
- /// (same threadlocal frame-lifetime contract as the path elements: the
48
- /// emitted `drawText` commands slice into it until the runtime copies
49
- /// the display list). A y axis labels at most a handful of ticks and a
48
+ /// (threadlocal frame-lifetime storage: the emitted `drawText` commands
49
+ /// slice into it until the runtime copies the display list). A y axis
50
+ /// labels at most a handful of ticks and a
50
51
  /// hover tooltip a row per series, each `max_chart_value_label_bytes`
51
52
  /// long, so 2 KiB holds dozens of charts per frame; overflow fails
52
53
  /// loudly with `ChartLabelBytesFull`.
@@ -722,8 +722,8 @@ test "chart golden: line + bar + band render byte-identically in light and dark"
722
722
  // category labels (q1, q3) under the bars. Both themes clear with their
723
723
  // background token. Update deliberately when chart rendering changes,
724
724
  // reviewing the dumped pixels first.
725
- const golden_light_signature: u64 = 11760269401975515075;
726
- const golden_dark_signature: u64 = 1706071444071293822;
725
+ const golden_light_signature: u64 = 4743613392161459263;
726
+ const golden_dark_signature: u64 = 13208780493823093371;
727
727
 
728
728
  fn goldenDumpRequested() bool {
729
729
  if (comptime !@import("builtin").link_libc) return false;
@@ -1,6 +1,7 @@
1
1
  const std = @import("std");
2
2
  const geometry = @import("geometry");
3
3
  const canvas = @import("root.zig");
4
+ const chart_model = @import("chart.zig");
4
5
  const drawing_model = @import("drawing.zig");
5
6
  const text_model = @import("text.zig");
6
7
  const render_model = @import("render.zig");
@@ -365,6 +366,20 @@ fn nonNegative(value: f32) f32 {
365
366
  pub const Builder = struct {
366
367
  commands: []CanvasCommand,
367
368
  len: usize = 0,
369
+ /// Builder-owned storage for path elements built at emit time (the
370
+ /// checkbox mark, chart polylines and bands, the spinner arc and
371
+ /// segments). Emitters allocate from here via `allocPathElements`,
372
+ /// so their commands' element slices share the builder's lifetime:
373
+ /// a display list accumulated across several emit calls — or held
374
+ /// while another builder emits — keeps every path's geometry
375
+ /// intact. Comptime-static elements (the icon registry) bypass the
376
+ /// store and stay zero-copy. Sized to the chart frame budget, which
377
+ /// a lockstep test keeps equal to the runtime's per-view
378
+ /// path-element budget, so overflow fails loudly by budget name
379
+ /// exactly where the runtime's per-view copy would have refused
380
+ /// anyway.
381
+ path_elements: [chart_model.max_chart_path_elements_per_frame]drawing_model.PathElement = undefined,
382
+ path_element_len: usize = 0,
368
383
 
369
384
  pub fn init(commands: []CanvasCommand) Builder {
370
385
  return .{ .commands = commands };
@@ -372,6 +387,18 @@ pub const Builder = struct {
372
387
 
373
388
  pub fn reset(self: *Builder) void {
374
389
  self.len = 0;
390
+ self.path_element_len = 0;
391
+ }
392
+
393
+ /// Reserve `count` path elements in the builder-owned store. The
394
+ /// returned slice is caller-filled and stays valid for the life of
395
+ /// the builder (until `reset`), so commands built over it survive
396
+ /// later emissions into this or any other builder.
397
+ pub fn allocPathElements(self: *Builder, count: usize) error{ChartPathElementListFull}![]drawing_model.PathElement {
398
+ if (self.path_element_len + count > self.path_elements.len) return error.ChartPathElementListFull;
399
+ const start = self.path_element_len;
400
+ self.path_element_len += count;
401
+ return self.path_elements[start..self.path_element_len];
375
402
  }
376
403
 
377
404
  pub fn displayList(self: *const Builder) DisplayList {
@@ -13,6 +13,7 @@ const Color = drawing_model.Color;
13
13
  const Radius = drawing_model.Radius;
14
14
  const LinearGradient = drawing_model.LinearGradient;
15
15
  const Fill = drawing_model.Fill;
16
+ const LineCap = drawing_model.LineCap;
16
17
  const PathElement = drawing_model.PathElement;
17
18
  const ImageFit = drawing_model.ImageFit;
18
19
  const ImageSampling = drawing_model.ImageSampling;
@@ -201,6 +202,12 @@ pub const CanvasGpuCommand = struct {
201
202
  shape: CanvasGpuShape = .none,
202
203
  paint: CanvasGpuPaint = .none,
203
204
  stroke_width: f32 = 0,
205
+ /// Stroke end-cap shape for `stroke_path` commands (butt for every
206
+ /// other kind, matching the display-list model where only path
207
+ /// strokes carry a cap channel). Rides the wire so packet hosts
208
+ /// stroke open subpaths with the same ends the reference renderer
209
+ /// rasterizes.
210
+ cap: LineCap = .butt,
204
211
  image: ?CanvasGpuImage = null,
205
212
  text: ?CanvasGpuText = null,
206
213
  effect: CanvasGpuEffect = .none,
@@ -525,6 +532,7 @@ pub fn canvasGpuCommandFromRenderCommand(command: RenderCommand, command_index:
525
532
  packet_command.shape = .{ .path = value.elements };
526
533
  packet_command.paint = canvasGpuPaint(value.stroke.fill);
527
534
  packet_command.stroke_width = value.stroke.width;
535
+ packet_command.cap = value.cap;
528
536
  packet_command.uses_path_geometry = true;
529
537
  packet_command.uses_resource = canvasGpuFillUsesResource(value.stroke.fill);
530
538
  },
@@ -405,7 +405,7 @@ test "the README-shaped fixture renders through the mapper and the reference ren
405
405
  // separators, fenced-code panels, and near-black underlined links.
406
406
  // Update deliberately when markdown rendering changes, reviewing the
407
407
  // rendered pixels first (see reference_tests.zig conventions).
408
- const markdown_document_reference_signature: u64 = 1138378532370101207;
408
+ const markdown_document_reference_signature: u64 = 4124710367581899536;
409
409
 
410
410
 
411
411
  test "bare URLs autolink at word boundaries with trailing punctuation trimmed" {
@@ -225,9 +225,11 @@ pub const ReferenceRenderSurface = struct {
225
225
  }
226
226
 
227
227
  pub fn renderPass(self: ReferenceRenderSurface, pass: CanvasRenderPass, clear_color: Color) Error!void {
228
- // One-time sRGB decode table fill (see the table's doc comment);
229
- // outside the per-pixel loops so the hot path pays no checks.
228
+ // One-time sRGB decode/encode table fills (see the tables' doc
229
+ // comments); outside the per-pixel loops so the hot path pays no
230
+ // checks.
230
231
  ensureSrgbToLinearByteTable();
232
+ ensureLinearToSrgbTable();
231
233
  // Fresh per-pass panel-fill budget (see the memo's doc comment).
232
234
  if (self.render_memo) |memo| memo.image_scale_fills_this_pass = 0;
233
235
  const scale = referencePassScale(pass.scale);
@@ -295,12 +297,21 @@ pub const ReferenceRenderSurface = struct {
295
297
  // so no apron rows join the key.
296
298
  const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(2, command, value));
297
299
  if (self.memoReplay(probe, pixel_rect)) return;
300
+ // Square-cornered fills keep the legacy binary pixel-center
301
+ // test (bit-identical on integer bounds); rounded fills take
302
+ // the anti-aliased signed-distance coverage.
303
+ const binary = referenceRadiusIsZero(radius);
298
304
  var y = pixel_rect.y;
299
305
  while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
300
306
  var x = pixel_rect.x;
301
307
  while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
302
308
  const point = referencePixelCenter(x, y);
303
- if (referencePointInRoundedRect(point, rect, radius)) self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.fill, command.transform, point), command.opacity);
309
+ if (binary) {
310
+ if (referencePointInRoundedRect(point, rect, radius)) self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.fill, command.transform, point), command.opacity);
311
+ continue;
312
+ }
313
+ const coverage = referenceRoundedRectCoverage(point, rect, radius);
314
+ if (coverage > 0) self.blendPixelCoverage(@intCast(x), @intCast(y), referenceSampleFill(value.fill, command.transform, point), coverage, command.opacity, .linear_light);
304
315
  }
305
316
  }
306
317
  self.memoStore(probe, pixel_rect);
@@ -323,13 +334,28 @@ pub const ReferenceRenderSurface = struct {
323
334
  // blends into, so no apron rows join the key.
324
335
  const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(5, command, value));
325
336
  if (self.memoReplay(probe, pixel_rect)) return;
337
+ // Square-cornered borders keep the legacy binary ring test
338
+ // (bit-identical on integer bounds). Rounded borders derive
339
+ // BOTH ring edges from the same signed-distance field —
340
+ // coverage is outer-edge coverage minus inner-edge coverage —
341
+ // so the border weight stays uniform around the whole shape
342
+ // (the inner edge stays in the field even when the inset
343
+ // radius bottoms out at a square corner).
344
+ const binary = referenceRadiusIsZero(radius);
326
345
  var y = pixel_rect.y;
327
346
  while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
328
347
  var x = pixel_rect.x;
329
348
  while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
330
349
  const point = referencePixelCenter(x, y);
331
- if (referencePointInRoundedRect(point, outer, outer_radius) and !referencePointInRoundedRect(point, inner, inner_radius)) {
332
- self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.stroke.fill, command.transform, point), command.opacity);
350
+ if (binary) {
351
+ if (referencePointInRoundedRect(point, outer, outer_radius) and !referencePointInRoundedRect(point, inner, inner_radius)) {
352
+ self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.stroke.fill, command.transform, point), command.opacity);
353
+ }
354
+ continue;
355
+ }
356
+ const coverage = std.math.clamp(referenceRoundedRectCoverage(point, outer, outer_radius) - referenceRoundedRectCoverage(point, inner, inner_radius), 0, 1);
357
+ if (coverage > 0) {
358
+ self.blendPixelCoverage(@intCast(x), @intCast(y), referenceSampleFill(value.stroke.fill, command.transform, point), coverage, command.opacity, .linear_light);
333
359
  }
334
360
  }
335
361
  }
@@ -365,6 +391,7 @@ pub const ReferenceRenderSurface = struct {
365
391
  .fill = value.fill,
366
392
  .transform = command.transform,
367
393
  .opacity = command.opacity,
394
+ .coverage_blend = .linear_light,
368
395
  };
369
396
  vector.fillPath(
370
397
  value.elements,
@@ -392,6 +419,7 @@ pub const ReferenceRenderSurface = struct {
392
419
  .fill = value.stroke.fill,
393
420
  .transform = command.transform,
394
421
  .opacity = command.opacity,
422
+ .coverage_blend = .linear_light,
395
423
  };
396
424
  vector.strokePath(
397
425
  value.elements,
@@ -799,11 +827,16 @@ pub const ReferenceRenderSurface = struct {
799
827
  if (builder.slice().len == 0) return true; // Space: nothing to ink.
800
828
 
801
829
  const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return true;
830
+ // Glyph coverage blends in sRGB, not linear light (see
831
+ // `CoverageBlend`): apparent text weight is set by how edge
832
+ // pixels darken, and re-blending them in linear light thins
833
+ // dark-on-light runs and blooms light-on-dark runs at UI sizes.
802
834
  var sink = ReferenceCoverageSink{
803
835
  .surface = self,
804
836
  .fill = .{ .color = value.color },
805
837
  .transform = command.transform,
806
838
  .opacity = command.opacity,
839
+ .coverage_blend = .srgb,
807
840
  };
808
841
  // The outline is already in device space; TrueType interiorness
809
842
  // is the nonzero rule.
@@ -845,18 +878,66 @@ pub const ReferenceRenderSurface = struct {
845
878
  self.pixels[index + 3] = out[3];
846
879
  }
847
880
 
881
+ /// Blend one pixel whose fractional alpha is ANTI-ALIASED EDGE
882
+ /// COVERAGE (kept separate from the color's own alpha so the blend
883
+ /// can tell an AA fringe from a translucent wash — see
884
+ /// `CoverageBlend`).
885
+ fn blendPixelCoverage(self: ReferenceRenderSurface, x: usize, y: usize, color: Color, coverage: f32, opacity: f32, blend: CoverageBlend) void {
886
+ const index = (y * self.width + x) * 4;
887
+ const dst = [4]u8{
888
+ self.pixels[index + 0],
889
+ self.pixels[index + 1],
890
+ self.pixels[index + 2],
891
+ self.pixels[index + 3],
892
+ };
893
+ const out = blendRgba8Coverage(dst, color, coverage, opacity, blend);
894
+ self.pixels[index + 0] = out[0];
895
+ self.pixels[index + 1] = out[1];
896
+ self.pixels[index + 2] = out[2];
897
+ self.pixels[index + 3] = out[3];
898
+ }
899
+
848
900
  fn findImage(self: ReferenceRenderSurface, id: ImageId) ?ReferenceImage {
849
901
  return findReferenceImage(self.images, id);
850
902
  }
851
903
  };
852
904
 
905
+ /// Which space a shape's fractional edge coverage blends in.
906
+ ///
907
+ /// GEOMETRY — rounded rects, filled/stroked paths, icons, chart marks —
908
+ /// blends its anti-aliased edge pixels in LINEAR LIGHT. Compositing the
909
+ /// sRGB-encoded bytes directly weights half coverage far below half the
910
+ /// light (a 50% black-on-white fringe lands near 21% luminance instead
911
+ /// of 50%), so every edge grows a dark rim on light backgrounds (a light
912
+ /// halo on dark ones) that reads as jagged even though the coverage
913
+ /// values are correct. Decoding to linear light, blending, and
914
+ /// re-encoding removes the rim.
915
+ ///
916
+ /// GLYPHS stay in sRGB. Text coverage funnels through the exact same
917
+ /// vector core, but apparent text WEIGHT is a product of how edge pixels
918
+ /// darken: the same coverage blended in linear light renders visibly
919
+ /// thinner dark-on-light runs and bloomier light-on-dark runs at UI
920
+ /// sizes, and the toolkit's type ramp was tuned against sRGB-blended
921
+ /// stems. sRGB glyph compositing also matches the packet-backed macOS
922
+ /// text pipeline, so mixed CPU/host frames keep one text weight.
923
+ ///
924
+ /// Only opaque-source fractional-coverage pixels differ between the two
925
+ /// modes: fully covered pixels short-circuit identically in either
926
+ /// space, and translucent sources (washes, scrims, faded layers) keep
927
+ /// sRGB blending so overlay brightness — tuned in sRGB terms — is
928
+ /// untouched and an AA edge never diverges from the interior it borders.
929
+ const CoverageBlend = enum { linear_light, srgb };
930
+
853
931
  /// Per-pixel coverage sink for the vector core: samples the fill at the
854
- /// pixel center and blends with the coverage folded into alpha.
932
+ /// pixel center and blends with the coverage, in the blend space the
933
+ /// emitter declared (geometry linear-light, glyphs sRGB — see
934
+ /// `CoverageBlend`).
855
935
  const ReferenceCoverageSink = struct {
856
936
  surface: ReferenceRenderSurface,
857
937
  fill: Fill,
858
938
  transform: Affine,
859
939
  opacity: f32,
940
+ coverage_blend: CoverageBlend,
860
941
 
861
942
  pub fn pixel(self: *ReferenceCoverageSink, x: i32, y: i32, coverage: f32) void {
862
943
  if (x < 0 or y < 0) return;
@@ -865,7 +946,7 @@ const ReferenceCoverageSink = struct {
865
946
  if (px >= self.surface.width or py >= self.surface.height) return;
866
947
  const point = referencePixelCenter(px, py);
867
948
  const color = referenceSampleFill(self.fill, self.transform, point);
868
- self.surface.blendPixel(px, py, referenceScaleColorAlpha(color, coverage), self.opacity);
949
+ self.surface.blendPixelCoverage(px, py, color, coverage, self.opacity, self.coverage_blend);
869
950
  }
870
951
  };
871
952
 
@@ -1315,6 +1396,31 @@ fn ensureSrgbToLinearByteTable() void {
1315
1396
  srgb_to_linear_byte_table_ready = true;
1316
1397
  }
1317
1398
 
1399
+ /// Precomputed `referenceLinearToSrgb` over evenly spaced linear inputs:
1400
+ /// the linear-light coverage blend re-encodes three channels per fringe
1401
+ /// pixel, and each direct encode costs a `pow`. 4096 entries keep the
1402
+ /// nearest-entry result within one 8-bit step of direct evaluation: the
1403
+ /// curve is steepest near black (slope 12.92), where one table cell
1404
+ /// still spans under one output byte step, so the looked-up value sits
1405
+ /// within half a step of exact and the final byte rounding moves by at
1406
+ /// most one level. Same benign-race lazy fill as the decode table above.
1407
+ const linear_to_srgb_table_len = 4096;
1408
+ var linear_to_srgb_table: [linear_to_srgb_table_len]f32 = undefined;
1409
+ var linear_to_srgb_table_ready: bool = false;
1410
+
1411
+ fn ensureLinearToSrgbTable() void {
1412
+ if (linear_to_srgb_table_ready) return;
1413
+ for (&linear_to_srgb_table, 0..) |*value, index| {
1414
+ value.* = referenceLinearToSrgb(@as(f32, @floatFromInt(index)) / (linear_to_srgb_table_len - 1));
1415
+ }
1416
+ linear_to_srgb_table_ready = true;
1417
+ }
1418
+
1419
+ fn referenceLinearToSrgbLut(value: f32) f32 {
1420
+ const index: usize = @intFromFloat(@round(std.math.clamp(value, 0, 1) * (linear_to_srgb_table_len - 1)));
1421
+ return linear_to_srgb_table[index];
1422
+ }
1423
+
1318
1424
  fn referencePremultiplySrgba8(pixel: [4]u8) ReferencePremultipliedLinearColor {
1319
1425
  const alpha = @as(f32, @floatFromInt(pixel[3])) / 255.0;
1320
1426
  return .{
@@ -1359,6 +1465,61 @@ fn referencePointInRoundedRect(point: geometry.PointF, rect: geometry.RectF, rad
1359
1465
  return true;
1360
1466
  }
1361
1467
 
1468
+ /// True when every corner is square — the gate for the legacy binary
1469
+ /// pixel-center rasterization, which keeps radius-0 rects on integer
1470
+ /// bounds bit-identical to their historical bytes.
1471
+ fn referenceRadiusIsZero(radius: Radius) bool {
1472
+ return radius.top_left <= 0 and radius.top_right <= 0 and
1473
+ radius.bottom_right <= 0 and radius.bottom_left <= 0;
1474
+ }
1475
+
1476
+ /// Fractional coverage of the pixel centered at `point` against a
1477
+ /// rounded rect: `clamp(0.5 - d, 0, 1)` of the EXACT signed distance to
1478
+ /// the shape boundary. One closed form serves arcs and straight
1479
+ /// segments alike, so coverage is a continuous function around the
1480
+ /// whole perimeter — a per-region model (arc ramp here, binary edge
1481
+ /// there) disagrees with itself about where the boundary is at the
1482
+ /// hand-off points and grows nubs or notches into the silhouette.
1483
+ ///
1484
+ /// The distance is the classic rounded-rect field: with `q = |p - c| -
1485
+ /// (half_extent - r)` for the corner radius `r` of the quadrant `p`
1486
+ /// lies in, `d = length(max(q, 0)) + min(max(q.x, q.y), 0) - r`. It is
1487
+ /// exact for any per-corner radii, including square (r = 0) corners,
1488
+ /// and along a straight segment it reduces to the plain axis distance
1489
+ /// (the selected radius cancels), so the field is continuous where
1490
+ /// quadrants with different radii meet. Cheap fully-inside/outside
1491
+ /// short circuits keep the square root off interior and far-apron
1492
+ /// pixels; the exact form runs only near the boundary band.
1493
+ fn referenceRoundedRectCoverage(point: geometry.PointF, rect: geometry.RectF, radius: Radius) f32 {
1494
+ const normalized = rect.normalized();
1495
+ if (normalized.isEmpty()) return 0;
1496
+ const max_radius = @min(normalized.width, normalized.height) * 0.5;
1497
+
1498
+ const half_width = normalized.width * 0.5;
1499
+ const half_height = normalized.height * 0.5;
1500
+ const dx = point.x - (normalized.x + half_width);
1501
+ const dy = point.y - (normalized.y + half_height);
1502
+ const corner_radius = std.math.clamp(nonNegative(if (dx < 0)
1503
+ (if (dy < 0) radius.top_left else radius.bottom_left)
1504
+ else
1505
+ (if (dy < 0) radius.top_right else radius.bottom_right)), 0, max_radius);
1506
+
1507
+ const qx = @abs(dx) - half_width + corner_radius;
1508
+ const qy = @abs(dy) - half_height + corner_radius;
1509
+ // Fully inside: with both components non-positive the distance is
1510
+ // `max(qx, qy) - r`, so anything at least half a pixel inside the
1511
+ // radius-inset cross is full coverage.
1512
+ if (@max(qx, qy) <= -0.5) return 1;
1513
+ // Fully outside: the distance is at least `qx - r` (and `qy - r`),
1514
+ // so anything at least half a pixel beyond the bounding box is
1515
+ // zero coverage.
1516
+ if (qx - corner_radius >= 0.5 or qy - corner_radius >= 0.5) return 0;
1517
+ const mx = @max(qx, 0);
1518
+ const my = @max(qy, 0);
1519
+ const distance = @sqrt(mx * mx + my * my) + @min(@max(qx, qy), 0) - corner_radius;
1520
+ return std.math.clamp(0.5 - distance, 0, 1);
1521
+ }
1522
+
1362
1523
  fn referencePointInCorner(point: geometry.PointF, center: geometry.PointF, radius: f32) bool {
1363
1524
  if (radius <= 0) return false;
1364
1525
  const dx = point.x - center.x;
@@ -1449,6 +1610,53 @@ fn blendRgba8(dst: [4]u8, src: Color, opacity: f32) [4]u8 {
1449
1610
  };
1450
1611
  }
1451
1612
 
1613
+ /// Source-over with the source's fractional alpha split into COLOR alpha
1614
+ /// and EDGE COVERAGE, so the blend space can key off what the alpha
1615
+ /// means (see `CoverageBlend`).
1616
+ ///
1617
+ /// Linear-light blending is reserved for the one case the split targets:
1618
+ /// an effectively opaque source's anti-aliased fringe. Everything else —
1619
+ /// sRGB-mode callers (glyphs), fully covered pixels, and translucent
1620
+ /// sources — folds coverage into alpha and takes the historical sRGB
1621
+ /// blend, byte for byte. That routing is also the cost story: interiors
1622
+ /// (`coverage >= 1`) and washes never pay a decode/encode round-trip, so
1623
+ /// the linear math runs only on the thin edge band, where the two table
1624
+ /// lookups per channel replace `pow` evaluations.
1625
+ fn blendRgba8Coverage(dst: [4]u8, src: Color, coverage: f32, opacity: f32, blend: CoverageBlend) [4]u8 {
1626
+ const cov = std.math.clamp(coverage, 0, 1);
1627
+ const src_a = std.math.clamp(src.a, 0, 1) * std.math.clamp(opacity, 0, 1);
1628
+ if (blend == .srgb or cov <= 0 or cov >= 1 or src_a < 1) {
1629
+ return blendRgba8(dst, referenceScaleColorAlpha(src, cov), opacity);
1630
+ }
1631
+
1632
+ // Belt over the renderPass-level fills for direct callers (unit
1633
+ // tests, future paths): two predictable branches per fringe pixel.
1634
+ ensureSrgbToLinearByteTable();
1635
+ ensureLinearToSrgbTable();
1636
+
1637
+ // Alpha stays in coverage space — it counts covered area, not light
1638
+ // — so the alpha math is IDENTICAL to the sRGB path; only the color
1639
+ // channels decode to linear light. `out_a >= cov > 0` here, so the
1640
+ // straight-alpha un-premultiply divide is safe. The source decodes
1641
+ // through the same byte quantization its coverage-1 pixels store,
1642
+ // so a fringe converges exactly onto the interior bytes it borders.
1643
+ const dst_a = @as(f32, @floatFromInt(dst[3])) / 255.0;
1644
+ const out_a = cov + dst_a * (1 - cov);
1645
+ const src_r = srgb_to_linear_byte_table[colorChannelToByte(src.r)];
1646
+ const src_g = srgb_to_linear_byte_table[colorChannelToByte(src.g)];
1647
+ const src_b = srgb_to_linear_byte_table[colorChannelToByte(src.b)];
1648
+ const dst_r = srgb_to_linear_byte_table[dst[0]];
1649
+ const dst_g = srgb_to_linear_byte_table[dst[1]];
1650
+ const dst_b = srgb_to_linear_byte_table[dst[2]];
1651
+ const dst_weight = dst_a * (1 - cov);
1652
+ return .{
1653
+ colorChannelToByte(referenceLinearToSrgbLut((src_r * cov + dst_r * dst_weight) / out_a)),
1654
+ colorChannelToByte(referenceLinearToSrgbLut((src_g * cov + dst_g * dst_weight) / out_a)),
1655
+ colorChannelToByte(referenceLinearToSrgbLut((src_b * cov + dst_b * dst_weight) / out_a)),
1656
+ colorChannelToByte(out_a),
1657
+ };
1658
+ }
1659
+
1452
1660
  fn colorChannelToByte(value: f32) u8 {
1453
1661
  return @intFromFloat(@round(std.math.clamp(value, 0, 1) * 255.0));
1454
1662
  }