@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
@@ -16,7 +16,6 @@ const AutomationProvenanceTarget = automation_commands.AutomationProvenanceTarge
16
16
  const AutomationWidgetWheel = automation_commands.AutomationWidgetWheel;
17
17
  const AutomationWidgetKey = automation_commands.AutomationWidgetKey;
18
18
  const AutomationWidgetPointerDrag = automation_commands.AutomationWidgetPointerDrag;
19
- const automationWidgetActionSupported = automation_commands.automationWidgetActionSupported;
20
19
  const parseAutomationTextSelection = automation_commands.parseAutomationTextSelection;
21
20
  const parseAutomationDragDelta = automation_commands.parseAutomationDragDelta;
22
21
  const parseAutomationDropPaths = automation_commands.parseAutomationDropPaths;
@@ -27,24 +26,40 @@ const validateViewLabel = validation.validateViewLabel;
27
26
 
28
27
  pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
29
28
  return struct {
29
+ /// The automation `widget_action` command surface delegates to
30
+ /// the accessibility dispatch rather than switching over the
31
+ /// verbs itself: one choke point performs the verb AND stages
32
+ /// the outer-wins `widget_accessibility_action` journal record,
33
+ /// so a recorded session replays the verb — focus included —
34
+ /// instead of replaying its untargeted synthesized children
35
+ /// against whatever focus the session happened to hold.
30
36
  pub fn dispatchAutomationWidgetAction(self: *Runtime, app: runtime_api.App(Runtime), action: AutomationWidgetAction) anyerror!void {
31
- const view_index = try automationWidgetActionViewIndex(self, action);
32
- switch (action.action) {
33
- .focus => try focusAutomationCanvasWidget(self, view_index, action.id),
34
- .press => try dispatchAutomationWidgetKey(self, app, view_index, action.id, "enter"),
35
- .toggle => try dispatchAutomationWidgetKey(self, app, view_index, action.id, "space"),
36
- .increment => try dispatchAutomationWidgetKey(self, app, view_index, action.id, self.views[view_index].canvasWidgetStepKey(action.id, .increment)),
37
- .decrement => try dispatchAutomationWidgetKey(self, app, view_index, action.id, self.views[view_index].canvasWidgetStepKey(action.id, .decrement)),
38
- .set_text => try setAutomationCanvasWidgetText(self, app, view_index, action.id, action.value),
39
- .set_selection => try editAutomationCanvasWidgetText(self, view_index, action.id, .{ .set_selection = try parseAutomationTextSelection(action.value) }),
40
- .set_composition => try editAutomationCanvasWidgetText(self, view_index, action.id, .{ .set_composition = .{ .text = action.value } }),
41
- .commit_composition => try editAutomationCanvasWidgetText(self, view_index, action.id, .commit_composition),
42
- .cancel_composition => try editAutomationCanvasWidgetText(self, view_index, action.id, .cancel_composition),
43
- .select => try selectAutomationCanvasWidget(self, view_index, action.id),
44
- .drag => try dispatchAutomationCanvasWidgetDrag(self, app, view_index, action.id, action.value),
45
- .drop_files => try dispatchAutomationCanvasWidgetFileDrop(self, app, view_index, action.id, action.value),
46
- .dismiss => try dismissAutomationCanvasWidget(self, app, view_index, action.id),
47
- }
37
+ const view_index = try automationGpuSurfaceViewIndexByLabel(self, action.view_label);
38
+ _ = try self.dispatchCanvasWidgetAccessibilityAction(app, self.views[view_index].window_id, self.views[view_index].label, .{
39
+ .id = action.id,
40
+ .action = canvasWidgetActionKindFromAutomation(action.action),
41
+ .text = action.value,
42
+ .selection = if (action.action == .set_selection) try parseAutomationTextSelection(action.value) else null,
43
+ });
44
+ }
45
+
46
+ fn canvasWidgetActionKindFromAutomation(kind: automation_commands.AutomationWidgetActionKind) runtime_api.CanvasWidgetAccessibilityActionKind {
47
+ return switch (kind) {
48
+ .focus => .focus,
49
+ .press => .press,
50
+ .toggle => .toggle,
51
+ .increment => .increment,
52
+ .decrement => .decrement,
53
+ .set_text => .set_text,
54
+ .set_selection => .set_selection,
55
+ .set_composition => .set_composition,
56
+ .commit_composition => .commit_composition,
57
+ .cancel_composition => .cancel_composition,
58
+ .select => .select,
59
+ .drag => .drag,
60
+ .drop_files => .drop_files,
61
+ .dismiss => .dismiss,
62
+ };
48
63
  }
49
64
 
50
65
  pub fn dispatchAutomationWidgetClick(self: *Runtime, app: runtime_api.App(Runtime), target: AutomationWidgetTarget) anyerror!void {
@@ -269,6 +284,70 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
269
284
  } });
270
285
  }
271
286
 
287
+ /// Drive a trackpad pinch through the real platform-event path:
288
+ /// `pinch_begin`, one `pinch_change` carrying `scale - 1` (so
289
+ /// the cumulative product of `1 + delta` lands exactly on the
290
+ /// commanded scale — the gesture's FINAL multiplicative zoom),
291
+ /// and `pinch_end`, all at the same anchor point. A scale so
292
+ /// small its f32 delta rounds to -1 (factor 0 on the wire) is
293
+ /// refused as `PinchScaleBelowWireMinimum` — see the guard
294
+ /// below for the named minimum.
295
+ /// Plain input synthesis, the `widget-key` discipline: every
296
+ /// event journals as itself and replays through the same
297
+ /// dispatch — no accessibility-action record, because pinch is
298
+ /// not a widget verb (it never routes into the widget tree; the
299
+ /// app hears it through the pinch channel).
300
+ pub fn dispatchAutomationWidgetPinch(self: *Runtime, app: runtime_api.App(Runtime), pinch: automation_commands.AutomationWidgetPinch) anyerror!void {
301
+ const view_index = try automationGpuSurfaceViewIndexByLabel(self, pinch.view_label);
302
+ const window_id = self.views[view_index].window_id;
303
+ const label = self.views[view_index].label;
304
+ const point = pinch.point orelse geometry.PointF.init(
305
+ self.views[view_index].gpu_size.width / 2,
306
+ self.views[view_index].gpu_size.height / 2,
307
+ );
308
+ // The wire delta is `scale - 1` in f32, and f32 rounding can
309
+ // betray the parser's `scale > 0` guard: any scale at or
310
+ // below 2^-25 rounds the difference to exactly -1
311
+ // (ties-to-even at the halfway point), which would put the
312
+ // factor `1 + delta = 0` on the wire — a zoom through zero
313
+ // scale, which no gesture can perform and no downstream
314
+ // product can recover from. The invariant is the WIRE's
315
+ // (every emitted factor stays > 0), so validate the computed
316
+ // delta, not the input; the minimum accepted scale is the
317
+ // smallest f32 above 2^-25 (~2.9802326e-8), whose delta
318
+ // rounds to -1 + 2^-24 — the smallest positive factor the
319
+ // wire can carry. Refused before anything dispatches, so no
320
+ // partial gesture reaches the journal.
321
+ const delta = pinch.scale - 1;
322
+ if (1 + delta <= 0) return error.PinchScaleBelowWireMinimum;
323
+ const timestamp_ns = automationInputTimestampNs();
324
+ try self.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
325
+ .window_id = window_id,
326
+ .label = label,
327
+ .kind = .pinch_begin,
328
+ .timestamp_ns = timestamp_ns,
329
+ .x = point.x,
330
+ .y = point.y,
331
+ } });
332
+ try self.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
333
+ .window_id = window_id,
334
+ .label = label,
335
+ .kind = .pinch_change,
336
+ .timestamp_ns = timestamp_ns,
337
+ .x = point.x,
338
+ .y = point.y,
339
+ .scale = delta,
340
+ } });
341
+ try self.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
342
+ .window_id = window_id,
343
+ .label = label,
344
+ .kind = .pinch_end,
345
+ .timestamp_ns = timestamp_ns,
346
+ .x = point.x,
347
+ .y = point.y,
348
+ } });
349
+ }
350
+
272
351
  pub fn dispatchAutomationWidgetPointerDrag(self: *Runtime, app: runtime_api.App(Runtime), drag: AutomationWidgetPointerDrag) anyerror!void {
273
352
  const view_index = try automationWidgetTargetViewIndex(self, drag.target);
274
353
  const layout = self.views[view_index].widgetLayoutTree();
@@ -349,6 +428,17 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
349
428
  const previous_state = self.views[view_index].canvasWidgetRenderState();
350
429
  self.views[view_index].canvas_widget_focused_id = target.id;
351
430
  self.views[view_index].canvas_widget_focus_visible_id = focus_visible_id;
431
+ // Pointer-contract provenance: a programmatic ring
432
+ // (editables only) never carries the keyboard's
433
+ // standing reveal intent into a layout adoption.
434
+ self.views[view_index].canvas_widget_focus_visible_keyboard = false;
435
+ // The pointer contract extends to tooltips: this move
436
+ // hides a focus-owned tooltip and reveals nothing (see
437
+ // updateCanvasTooltipIntentForProgrammaticFocusMove).
438
+ // Inside the changed-guard on purpose — re-focusing the
439
+ // widget whose focus-shown tooltip is up with the ring
440
+ // intact is not a move, and leaves it alone.
441
+ try CanvasWidgetEventMethods().updateCanvasTooltipIntentForProgrammaticFocusMove(self, view_index);
352
442
  // A focus change repaints; record the automation input so
353
443
  // the completing frame publishes (same contract as select
354
444
  // and text edits). Callers that dispatch a follow-up input
@@ -367,12 +457,18 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
367
457
  // the way a Tab-then-key would: escalate exactly this target
368
458
  // kind to the ring register before dispatching. Every other
369
459
  // kind keeps the quiet programmatic focus it always had.
460
+ // The escalation deliberately skips the tooltip machine —
461
+ // it is still programmatic focus (no reveal), and the focus
462
+ // MOVE above already hid any focus-owned tooltip.
370
463
  if (self.views[view_index].canvasWidgetNodeIndexById(id)) |node_index| {
371
464
  const widget = self.views[view_index].widget_layout_nodes[node_index].widget;
372
465
  const plain_list_row = widget.kind == .list_item and widget.semantics.role != .treeitem;
373
466
  if (plain_list_row and self.views[view_index].canvas_widget_focus_visible_id != id) {
374
467
  const previous_state = self.views[view_index].canvasWidgetRenderState();
375
468
  self.views[view_index].canvas_widget_focus_visible_id = id;
469
+ // Still programmatic (no reveal — see the comment
470
+ // above), so no keyboard provenance either.
471
+ self.views[view_index].canvas_widget_focus_visible_keyboard = false;
376
472
  try CanvasWidgetEventMethods().invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, self.views[view_index].canvasWidgetRenderState());
377
473
  }
378
474
  }
@@ -450,15 +546,57 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
450
546
  } });
451
547
  }
452
548
 
453
- pub fn editAutomationCanvasWidgetText(self: *Runtime, view_index: usize, id: canvas.ObjectId, edit: canvas.TextInputEvent) anyerror!void {
549
+ /// Composition edits ride the SAME ime input events a real IME
550
+ /// session produces (`setAutomationCanvasWidgetText`'s
551
+ /// philosophy): the dispatch applies the edit to the retained
552
+ /// editor, stamps it onto the routed keyboard event so the app's
553
+ /// `on_input` mirror hears it, and journals the input so a
554
+ /// recorded session replays the composition byte-identically.
555
+ /// Writing the editor directly (the previous shape) kept the
556
+ /// model out of the loop — the same divergence the keyboard
557
+ /// choke point closes for Escape's clear.
558
+ pub fn composeAutomationCanvasWidgetText(
559
+ self: *Runtime,
560
+ app: runtime_api.App(Runtime),
561
+ view_index: usize,
562
+ id: canvas.ObjectId,
563
+ kind: platform.GpuSurfaceInputKind,
564
+ text: []const u8,
565
+ ) anyerror!void {
454
566
  try focusAutomationCanvasWidget(self, view_index, id);
455
567
  if (!self.views[view_index].canEditCanvasWidgetText(id)) return error.InvalidCommand;
456
- const dirty = try self.views[view_index].applyCanvasWidgetTextEdit(id, edit) orelse return;
568
+ try self.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
569
+ .window_id = self.views[view_index].window_id,
570
+ .label = self.views[view_index].label,
571
+ .kind = kind,
572
+ .timestamp_ns = automationInputTimestampNs(),
573
+ .text = text,
574
+ } });
575
+ }
576
+
577
+ /// Selection edits have no platform input kind to ride, so the
578
+ /// verb synthesizes the routed keyboard event the clipboard and
579
+ /// context-menu edits use: the keyboard choke point applies the
580
+ /// stamped edit to the retained editor and the app dispatch
581
+ /// keeps the model's selection mirror honest. (The journal sees
582
+ /// this verb as the outer `widget_accessibility_action` record
583
+ /// its dispatch stages; replaying that record re-runs the verb.)
584
+ pub fn editAutomationCanvasWidgetText(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, id: canvas.ObjectId, edit: canvas.TextInputEvent) anyerror!void {
585
+ try focusAutomationCanvasWidget(self, view_index, id);
586
+ if (!self.views[view_index].canEditCanvasWidgetText(id)) return error.InvalidCommand;
587
+ const target = self.views[view_index].widgetLayoutTree().focusTargetById(id) orelse return error.InvalidCommand;
588
+ var keyboard_event: runtime_api.CanvasWidgetKeyboardEvent = .{
589
+ .window_id = self.views[view_index].window_id,
590
+ .view_label = self.views[view_index].label,
591
+ .keyboard = .{ .phase = .key_down, .focused_id = id, .edit = edit },
592
+ .target = target,
593
+ };
457
594
  // Same observability contract as selectAutomationCanvasWidget:
458
595
  // the repaint this edit triggers must publish its completing
459
596
  // frame, so record the automation input it resolves.
460
597
  self.views[view_index].recordGpuSurfaceInputTimestamp(automationInputTimestampNs());
461
- try CanvasWidgetEventMethods().invalidateForCanvasWidgetDirty(self, view_index, dirty);
598
+ try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, &keyboard_event);
599
+ try self.dispatchEvent(app, .{ .canvas_widget_keyboard = keyboard_event });
462
600
  }
463
601
 
464
602
  pub fn dispatchAutomationCanvasWidgetDrag(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, id: canvas.ObjectId, value: []const u8) anyerror!void {
@@ -520,13 +658,6 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
520
658
  } });
521
659
  }
522
660
 
523
- fn automationWidgetActionViewIndex(self: *Runtime, action: AutomationWidgetAction) anyerror!usize {
524
- const view_index = try automationGpuSurfaceViewIndexByLabel(self, action.view_label);
525
- const actions = canvasWidgetActionsForId(self, view_index, action.id) orelse return error.InvalidCommand;
526
- if (!automationWidgetActionSupported(actions, action.action)) return error.InvalidCommand;
527
- return view_index;
528
- }
529
-
530
661
  fn automationWidgetTargetViewIndex(self: *Runtime, target: AutomationWidgetTarget) anyerror!usize {
531
662
  return automationGpuSurfaceViewIndexByLabel(self, target.view_label);
532
663
  }
@@ -1,8 +1,11 @@
1
1
  //! Runtime canvas font registry tests: registration validation and
2
2
  //! capacity bounds (loud, one-past-budget), hostile/truncated font
3
- //! files, the font-aware measure provider, pixel parity between the
4
- //! present path and the reference screenshot path for a registered
5
- //! face, and the UiApp `Options.fonts` startup seam.
3
+ //! files, the registration-time glyph-budget gate (over-declaring
4
+ //! `maxp` refused whole; the real Noto Sans JP accepted and outlining
5
+ //! dense kanji when the /tmp fixture is present), the font-aware
6
+ //! measure provider, pixel parity between the present path and the
7
+ //! reference screenshot path for a registered face, and the UiApp
8
+ //! `Options.fonts` startup seam.
6
9
  //!
7
10
  //! The registered fixture is the ALREADY-BUNDLED Geist Mono bytes
8
11
  //! (`canvas.font_ttf.geist_mono_bytes`) under a registered id: the
@@ -86,6 +89,75 @@ test "canvas font registry validates ids, bytes, and capacity" {
86
89
  try std.testing.expectEqual(registered_font_id, resources[0].id);
87
90
  }
88
91
 
92
+ test "a fresh runtime allocates zero font bytes until a registration happens" {
93
+ const harness = try startedGpuHarness(std.testing.allocator);
94
+ defer harness.destroy(std.testing.allocator);
95
+ var app_state: RegistryApp = .{};
96
+
97
+ // Count every runtime-allocator call: construction, startup, and
98
+ // frames perform NONE — font byte storage is on-demand at
99
+ // registration, so a fontless runtime carries zero font bytes. (The
100
+ // regression pinned here: a reservation-shaped slot pool at the
101
+ // 24 MiB CJK bound embedded 192 MiB in every Runtime, doubling the
102
+ // docs wasm preview host's per-tile memory before any font existed.)
103
+ // The ownership allocator is frozen at init, so the test re-freezes
104
+ // the field directly — equivalent to constructing with the counting
105
+ // allocator, and safe exactly because no owned bytes exist yet.
106
+ var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
107
+ harness.runtime.owned_allocator = counting.allocator();
108
+ try harness.start(app_state.app());
109
+ try std.testing.expectEqual(@as(usize, 0), counting.allocations);
110
+
111
+ // Even an allocator that refuses everything leaves the runtime
112
+ // fully operational until a registration demands memory — and that
113
+ // failure is loud and recoverable, never a partial slot. (Still
114
+ // nothing owned, so re-freezing stays equivalent to init capture.)
115
+ var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
116
+ harness.runtime.owned_allocator = failing.allocator();
117
+ try std.testing.expectError(error.OutOfMemory, harness.runtime.registerCanvasFont(registered_font_id, mono_bytes));
118
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
119
+
120
+ // Registration is the first allocation: exactly one, exactly the
121
+ // file's size (freed by Runtime.deinit through harness.destroy —
122
+ // the leak-checked test allocator backs it).
123
+ harness.runtime.owned_allocator = counting.allocator();
124
+ try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
125
+ try std.testing.expectEqual(@as(usize, 1), counting.allocations);
126
+ try std.testing.expectEqual(mono_bytes.len, counting.allocated_bytes);
127
+ }
128
+
129
+ test "deinit frees font bytes through the init-frozen allocator even after options.allocator is mutated" {
130
+ const harness = try startedGpuHarness(std.testing.allocator);
131
+ defer harness.destroy(std.testing.allocator);
132
+ var app_state: RegistryApp = .{};
133
+ try harness.start(app_state.app());
134
+
135
+ // Freeze a counting allocator as the runtime's owner (equivalent to
136
+ // constructing with it — nothing is owned yet) and register a face
137
+ // through it.
138
+ var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
139
+ harness.runtime.owned_allocator = counting.allocator();
140
+ try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
141
+ try std.testing.expectEqual(@as(usize, 1), counting.allocations);
142
+ try std.testing.expectEqual(@as(usize, 0), counting.deallocations);
143
+
144
+ // The hazard under test: `Runtime.options` is public and mutable, so
145
+ // an embedder can swap `options.allocator` AFTER fonts registered.
146
+ // Ownership must not retarget — the deinit free has to hit the
147
+ // allocator that made the bytes, not whatever the option points at
148
+ // by teardown time.
149
+ var mutated = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
150
+ harness.runtime.options.allocator = mutated.allocator();
151
+ harness.runtime.deinit();
152
+
153
+ // Counts balance on the original identity; the mutated allocator
154
+ // saw zero activity in either direction.
155
+ try std.testing.expectEqual(counting.allocations, counting.deallocations);
156
+ try std.testing.expectEqual(counting.allocated_bytes, counting.freed_bytes);
157
+ try std.testing.expectEqual(@as(usize, 0), mutated.allocations);
158
+ try std.testing.expectEqual(@as(usize, 0), mutated.deallocations);
159
+ }
160
+
89
161
  test "hostile and truncated font files fail loud at registration and never corrupt the registry" {
90
162
  const harness = try startedGpuHarness(std.testing.allocator);
91
163
  defer harness.destroy(std.testing.allocator);
@@ -199,6 +271,167 @@ test "registered faces answer the batched advances seam identically to per-prefi
199
271
  }
200
272
  }
201
273
 
274
+ /// Byte offset of the `maxp` table in a TrueType file (test fixture
275
+ /// helper for building over-declaring faces out of the bundled bytes).
276
+ fn maxpTableOffset(bytes: []const u8) usize {
277
+ const table_count = std.mem.readInt(u16, bytes[4..6], .big);
278
+ var index: usize = 0;
279
+ while (index < table_count) : (index += 1) {
280
+ const record = 12 + index * 16;
281
+ if (std.mem.eql(u8, bytes[record .. record + 4], "maxp")) {
282
+ return std.mem.readInt(u32, bytes[record + 8 ..][0..4], .big);
283
+ }
284
+ }
285
+ unreachable; // the bundled faces always carry maxp
286
+ }
287
+
288
+ /// The bundled mono bytes with `maxp.maxPoints` patched one past the
289
+ /// glyph-point budget: a face that DECLARES denser glyphs than the
290
+ /// outline pipeline's budgets and must be refused whole at
291
+ /// registration, never degraded per glyph at render time.
292
+ fn overBudgetFontBytes(allocator: std.mem.Allocator) ![]u8 {
293
+ const patched = try allocator.dupe(u8, mono_bytes);
294
+ const maxp = maxpTableOffset(patched);
295
+ std.mem.writeInt(u16, patched[maxp + 6 ..][0..2], @intCast(canvas.font_ttf.max_glyph_points + 1), .big);
296
+ return patched;
297
+ }
298
+
299
+ /// The bundled mono bytes with `maxp.maxCompositePoints` (offset 10)
300
+ /// patched one past the flattened-composite budget: modest simple
301
+ /// maxima, composite flattening the path builder could not hold — the
302
+ /// exact face shape a simple-only gate would admit and then silently
303
+ /// block at render time.
304
+ fn overCompositeBudgetFontBytes(allocator: std.mem.Allocator) ![]u8 {
305
+ const patched = try allocator.dupe(u8, mono_bytes);
306
+ const maxp = maxpTableOffset(patched);
307
+ std.mem.writeInt(u16, patched[maxp + 10 ..][0..2], @intCast(canvas.font_ttf.max_composite_points + 1), .big);
308
+ return patched;
309
+ }
310
+
311
+ test "a face declaring glyphs denser than the budgets is refused at registration" {
312
+ const harness = try startedGpuHarness(std.testing.allocator);
313
+ defer harness.destroy(std.testing.allocator);
314
+ var app_state: RegistryApp = .{};
315
+ try harness.start(app_state.app());
316
+
317
+ const patched = try overBudgetFontBytes(std.testing.allocator);
318
+ defer std.testing.allocator.free(patched);
319
+
320
+ try std.testing.expectError(error.FontExceedsGlyphBudgets, harness.runtime.registerCanvasFont(registered_font_id, patched));
321
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
322
+
323
+ // The teaching machinery names the refusal: parse-reason carries the
324
+ // budget sentence, and the declared maxima expose the face's own
325
+ // numbers for callers that format (UiApp's fonts teaching).
326
+ try std.testing.expect(std.mem.indexOf(u8, canvas.font_ttf.parseFailureReason(patched).?, "budgets") != null);
327
+ const maxima = canvas.font_ttf.declaredGlyphMaxima(patched).?;
328
+ try std.testing.expectEqual(@as(u16, @intCast(canvas.font_ttf.max_glyph_points + 1)), maxima.points);
329
+ try std.testing.expect(!maxima.withinBudgets());
330
+
331
+ // No partial slot: the honest twin registers under the same id.
332
+ try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
333
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
334
+ }
335
+
336
+ test "a face declaring flattened composites denser than the budgets is refused at registration" {
337
+ const harness = try startedGpuHarness(std.testing.allocator);
338
+ defer harness.destroy(std.testing.allocator);
339
+ var app_state: RegistryApp = .{};
340
+ try harness.start(app_state.app());
341
+
342
+ // Simple maxima untouched (well within budget) — only the
343
+ // flattened-composite declaration is over. A gate reading simple
344
+ // maxima alone admits this face; its composites then overflow the
345
+ // reference path builder at render time and degrade to blocks, the
346
+ // exact silent failure registration-time validation forbids.
347
+ const patched = try overCompositeBudgetFontBytes(std.testing.allocator);
348
+ defer std.testing.allocator.free(patched);
349
+
350
+ try std.testing.expectError(error.FontExceedsGlyphBudgets, harness.runtime.registerCanvasFont(registered_font_id, patched));
351
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
352
+
353
+ // The teaching names the composite budgets and the declared maxima
354
+ // expose the face's own composite numbers.
355
+ const reason = canvas.font_ttf.parseFailureReason(patched).?;
356
+ try std.testing.expect(std.mem.indexOf(u8, reason, "flattened composite") != null);
357
+ const maxima = canvas.font_ttf.declaredGlyphMaxima(patched).?;
358
+ try std.testing.expectEqual(@as(u16, @intCast(canvas.font_ttf.max_composite_points + 1)), maxima.composite_points);
359
+ try std.testing.expect(!maxima.withinBudgets());
360
+
361
+ // The unpatched twin registers: the gate reads the declaration, not
362
+ // the patch's collateral.
363
+ try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
364
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
365
+ }
366
+
367
+ test "the real Noto Sans JP face registers and outlines dense kanji (guarded by /tmp/NotoSansJP.ttf)" {
368
+ // Ground-truth end-to-end proof with the face the budgets were
369
+ // sized from: the Google Fonts TrueType build of Noto Sans JP
370
+ // (OFL). Font binaries stay out of the repo, so the fixture is a
371
+ // local download; environments without it skip.
372
+ const bytes = std.Io.Dir.cwd().readFileAlloc(
373
+ std.testing.io,
374
+ "/tmp/NotoSansJP.ttf",
375
+ std.testing.allocator,
376
+ .limited(canvas_limits.max_registered_canvas_font_bytes),
377
+ ) catch return error.SkipZigTest;
378
+ defer std.testing.allocator.free(bytes);
379
+
380
+ const harness = try startedGpuHarness(std.testing.allocator);
381
+ defer harness.destroy(std.testing.allocator);
382
+ var app_state: RegistryApp = .{};
383
+ try harness.start(app_state.app());
384
+
385
+ // Registration passes the byte bound, the maxp gate, and the host
386
+ // seam — the whole registration-time validation doctrine.
387
+ try harness.runtime.registerCanvasFont(registered_font_id, bytes);
388
+ const face = harness.runtime.registeredCanvasFontFace(registered_font_id).?;
389
+
390
+ // Everyday-dense kanji resolve to real outlines, not notdef blocks:
391
+ // 鬱 U+9B31 measures 237 points / 26 contours in this face — far
392
+ // past the old Latin-sized 128-point/24-contour budgets that made
393
+ // it render as a block.
394
+ const dense_kanji = [_]u21{ 0x9B31, 0x9A5A, 0x7C60, 0x9451 }; // 鬱 驚 籠 鑑
395
+ for (dense_kanji) |codepoint| {
396
+ const glyph = face.glyphIndex(codepoint);
397
+ try std.testing.expect(glyph != 0);
398
+ var builder = canvas.vector.PathBuilder(2048){};
399
+ try face.glyphOutline(glyph, canvas.Affine.identity(), &builder);
400
+ try std.testing.expect(builder.slice().len > 0);
401
+ }
402
+
403
+ // The dense outline rasterizes within the vector budgets at body
404
+ // and headline sizes.
405
+ const InkCounter = struct {
406
+ covered: usize = 0,
407
+ pub fn pixel(self: *@This(), x: i32, y: i32, coverage: f32) void {
408
+ _ = x;
409
+ _ = y;
410
+ if (coverage > 0) self.covered += 1;
411
+ }
412
+ };
413
+ for ([_]f32{ 16, 96 }) |size| {
414
+ const glyph = face.glyphIndex(0x9B31);
415
+ const scale = size / face.units_per_em;
416
+ var builder = canvas.vector.PathBuilder(2048){};
417
+ try face.glyphOutline(glyph, .{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = 4, .ty = size }, &builder);
418
+ var counter = InkCounter{};
419
+ try canvas.vector.fillPath(
420
+ builder.slice(),
421
+ canvas.Affine.identity(),
422
+ .nonzero,
423
+ canvas.vector.default_tolerance,
424
+ .{ .x0 = 0, .y0 = 0, .x1 = 128, .y1 = 128 },
425
+ &counter,
426
+ );
427
+ try std.testing.expect(counter.covered > 0);
428
+ }
429
+
430
+ // Layout measures the registered face's own advances for the kanji.
431
+ const provider = harness.runtime.textMeasureProvider().?;
432
+ try std.testing.expect(provider.measureWidth(registered_font_id, 16.0, "鬱") > 0);
433
+ }
434
+
202
435
  fn installFontFixtureWidgets(harness: anytype) !void {
203
436
  _ = try harness.runtime.createView(.{
204
437
  .window_id = 1,
@@ -408,3 +641,39 @@ test "ui app declared font failures are teaching errors, not crashes or silent f
408
641
  try std.testing.expect(app_state.installed);
409
642
  try std.testing.expectEqual(first_errors, harness.runtime.dispatchErrorTotal());
410
643
  }
644
+
645
+ test "ui app fonts option surfaces the glyph-budget refusal as a teaching error" {
646
+ const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
647
+ defer harness.destroy(std.testing.allocator);
648
+ harness.null_platform.gpu_surfaces = true;
649
+
650
+ const patched = try overBudgetFontBytes(std.testing.allocator);
651
+ defer std.testing.allocator.free(patched);
652
+
653
+ const fonts = [_]FontApp.FontRegistration{.{
654
+ .id = registered_font_id,
655
+ .name = "TooDense.ttf",
656
+ .ttf = patched,
657
+ }};
658
+ const app_state = try std.testing.allocator.create(FontApp);
659
+ defer std.testing.allocator.destroy(app_state);
660
+ app_state.* = FontApp.init(std.testing.allocator, .{}, fontAppOptions(&fonts));
661
+ defer app_state.deinit();
662
+ const app = app_state.app();
663
+ try harness.start(app);
664
+ harness.runtime.dispatch_error_policy = .degrade;
665
+
666
+ // The installing frame surfaces the refusal through the dispatch
667
+ // error channel (the warn log names the face's declared maxima
668
+ // against the budgets); nothing registers, the app stays alive.
669
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
670
+ .label = font_app_canvas_label,
671
+ .size = geometry.SizeF.init(240, 200),
672
+ .scale_factor = 1,
673
+ .frame_index = 1,
674
+ .timestamp_ns = 1_000_000,
675
+ .nonblank = true,
676
+ } });
677
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
678
+ try std.testing.expect(harness.runtime.dispatchErrorTotal() > 0);
679
+ }