@native-sdk/cli 0.5.3 → 0.6.0

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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -510,8 +510,8 @@ test "a registered face renders pixel-identically on the present path and the re
510
510
 
511
511
  // ------------------------------------------------ UiApp Options.fonts
512
512
 
513
- const FontAppModel = struct { presses: u32 = 0 };
514
- const FontAppMsg = union(enum) { press: void };
513
+ const FontAppModel = struct { presses: u32 = 0, show_uncovered_cjk: bool = false };
514
+ const FontAppMsg = union(enum) { press: void, show_uncovered_cjk: void };
515
515
  const FontApp = ui_app_model.UiApp(FontAppModel, FontAppMsg);
516
516
 
517
517
  const font_app_canvas_label = "canvas";
@@ -519,6 +519,7 @@ const font_app_canvas_label = "canvas";
519
519
  fn fontAppUpdate(model: *FontAppModel, msg: FontAppMsg) void {
520
520
  switch (msg) {
521
521
  .press => model.presses += 1,
522
+ .show_uncovered_cjk => model.show_uncovered_cjk = true,
522
523
  }
523
524
  }
524
525
 
@@ -642,6 +643,442 @@ test "ui app declared font failures are teaching errors, not crashes or silent f
642
643
  try std.testing.expectEqual(first_errors, harness.runtime.dispatchErrorTotal());
643
644
  }
644
645
 
646
+ // ---------------------------------------------- the Chinese receipt
647
+
648
+ /// Committed CJK fixture: Noto Sans SC (OFL — the license rides beside
649
+ /// the file in testdata/fonts/OFL.txt), instanced at wght=400 from the
650
+ /// Google Fonts variable TrueType build and subsetted to exactly the
651
+ /// receipt string's four ideographs plus notdef (2.2 KB — the license
652
+ /// permits subsetting; full font binaries stay out of the repo). The
653
+ /// subset keeps the format-4 unicode cmap, real `glyf` outlines, and
654
+ /// the full font's truthful `maxp` declaration (584 points / 84
655
+ /// contours — past the old Latin-sized budgets, inside the CJK-sized
656
+ /// ones), so registration exercises the real gate and rendering inks
657
+ /// real Han ideograph outlines, deterministically, on every CI host.
658
+ const cjk_receipt_bytes = @embedFile("testdata/fonts/NotoSansSC-Receipt.ttf");
659
+
660
+ /// 你好世界 — "Hello, world".
661
+ const cjk_receipt_text = "\u{4F60}\u{597D}\u{4E16}\u{754C}";
662
+
663
+ /// 中文字体 — "Chinese font": four ideographs the subsetted fixture
664
+ /// face deliberately does NOT map (the subset carries exactly the
665
+ /// receipt string's four glyphs plus notdef; the test pins the gap
666
+ /// with glyphIndex() == 0 before relying on it). Rendered with the
667
+ /// SAME registered face, this string can only draw four notdef boxes
668
+ /// — the self-calibrating tofu baseline the receipt compares against.
669
+ const cjk_uncovered_text = "\u{4E2D}\u{6587}\u{5B57}\u{4F53}";
670
+
671
+ fn cjkAppView(ui: *FontApp.Ui, model: *const FontAppModel) FontApp.Ui.Node {
672
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
673
+ ui.text(.{}, if (model.show_uncovered_cjk) cjk_uncovered_text else cjk_receipt_text),
674
+ });
675
+ }
676
+
677
+ test "the Chinese receipt: a scaffold-shaped app registers a CJK face and renders real glyphs, not tofu" {
678
+ const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
679
+ defer harness.destroy(std.testing.allocator);
680
+ harness.null_platform.gpu_surfaces = true;
681
+
682
+ // The scaffold shape: fonts declared on Options.fonts, tokens
683
+ // pointing the typography face slot at the registered id, Chinese
684
+ // text in the view.
685
+ const fonts = [_]FontApp.FontRegistration{.{
686
+ .id = registered_font_id,
687
+ .name = "NotoSansSC-Receipt.ttf",
688
+ .ttf = cjk_receipt_bytes,
689
+ }};
690
+ var options = fontAppOptions(&fonts);
691
+ options.view = cjkAppView;
692
+ const app_state = try std.testing.allocator.create(FontApp);
693
+ defer std.testing.allocator.destroy(app_state);
694
+ app_state.* = FontApp.init(std.testing.allocator, .{}, options);
695
+ defer app_state.deinit();
696
+ const app = app_state.app();
697
+ try harness.start(app);
698
+
699
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
700
+ .label = font_app_canvas_label,
701
+ .size = geometry.SizeF.init(240, 200),
702
+ .scale_factor = 1,
703
+ .frame_index = 1,
704
+ .timestamp_ns = 1_000_000,
705
+ .nonblank = true,
706
+ } });
707
+ try std.testing.expect(app_state.installed);
708
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.dispatchErrors().len);
709
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
710
+
711
+ // Per-glyph receipt: every ideograph resolves to its own real glyph
712
+ // in the registered face (the bundled face maps NONE of them — the
713
+ // exact gap the registration closes) and rasterizes with nonzero
714
+ // ink at body and headline sizes.
715
+ const face = harness.runtime.registeredCanvasFontFace(registered_font_id).?;
716
+ const InkCounter = struct {
717
+ covered: usize = 0,
718
+ pub fn pixel(self: *@This(), x: i32, y: i32, coverage: f32) void {
719
+ _ = x;
720
+ _ = y;
721
+ if (coverage > 0) self.covered += 1;
722
+ }
723
+ };
724
+ var seen_glyphs: [4]u16 = undefined;
725
+ var index: usize = 0;
726
+ var glyph_count: usize = 0;
727
+ while (index < cjk_receipt_text.len) : (glyph_count += 1) {
728
+ const len = try std.unicode.utf8ByteSequenceLength(cjk_receipt_text[index]);
729
+ const codepoint = try std.unicode.utf8Decode(cjk_receipt_text[index .. index + len]);
730
+ index += len;
731
+
732
+ try std.testing.expectEqual(@as(u16, 0), canvas.font_ttf.geist_regular.glyphIndex(codepoint));
733
+ const glyph = face.glyphIndex(codepoint);
734
+ try std.testing.expect(glyph != 0);
735
+ // cmap distinguishes the codepoints — four ideographs, four
736
+ // distinct glyphs, never one shared fallback shape.
737
+ for (seen_glyphs[0..glyph_count]) |seen| try std.testing.expect(seen != glyph);
738
+ seen_glyphs[glyph_count] = glyph;
739
+
740
+ for ([_]f32{ 16, 40 }) |size| {
741
+ const scale = size / face.units_per_em;
742
+ var builder = canvas.vector.PathBuilder(2048){};
743
+ try face.glyphOutline(glyph, .{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = 4, .ty = size }, &builder);
744
+ try std.testing.expect(builder.slice().len > 0);
745
+ var counter = InkCounter{};
746
+ try canvas.vector.fillPath(
747
+ builder.slice(),
748
+ canvas.Affine.identity(),
749
+ .nonzero,
750
+ canvas.vector.default_tolerance,
751
+ .{ .x0 = 0, .y0 = 0, .x1 = 64, .y1 = 64 },
752
+ &counter,
753
+ );
754
+ try std.testing.expect(counter.covered > 0);
755
+ }
756
+ }
757
+ try std.testing.expectEqual(@as(usize, 4), glyph_count);
758
+
759
+ // Layout measures the ideographs with the registered face's own
760
+ // advances, so measured line breaking agrees with the inked glyphs.
761
+ const provider = harness.runtime.textMeasureProvider().?;
762
+ try std.testing.expect(provider.measureWidth(registered_font_id, 16.0, cjk_receipt_text) > 0);
763
+
764
+ // End-to-end pixels through the deterministic reference renderer —
765
+ // the same path Windows and Linux present. The registered face
766
+ // draws the string as real outlines; the same view under the
767
+ // bundled face can only draw notdef boxes, so the two screenshots
768
+ // MUST differ. That difference is the receipt: the Chinese text on
769
+ // screen came from the registered face, not tofu.
770
+ const registered_shot = try fontFixtureScreenshot(harness, std.testing.allocator, registered_font_id);
771
+ defer std.testing.allocator.free(registered_shot);
772
+ const tofu_shot = try fontFixtureScreenshot(harness, std.testing.allocator, canvas.default_sans_font_id);
773
+ defer std.testing.allocator.free(tofu_shot);
774
+ var nonblank = false;
775
+ var pixel: usize = 0;
776
+ while (pixel + 4 <= registered_shot.len) : (pixel += 4) {
777
+ if (registered_shot[pixel + 3] != 0 and (registered_shot[pixel] != 0 or registered_shot[pixel + 1] != 0 or registered_shot[pixel + 2] != 0)) {
778
+ nonblank = true;
779
+ break;
780
+ }
781
+ }
782
+ try std.testing.expect(nonblank);
783
+ try std.testing.expect(!std.mem.eql(u8, registered_shot, tofu_shot));
784
+
785
+ // Self-calibrating tofu control: the bundled-face comparison above
786
+ // proves the pixels changed with the face, not that they are real
787
+ // ideographs — a renderer that wrongly resolved every ideograph to
788
+ // the REGISTERED face's notdef glyph and inked that glyph's own
789
+ // outline would still differ from the bundled shot (a different
790
+ // face's fallback pixels) and still pass nonblank. Render the same
791
+ // view with the same registered face showing a string the fixture
792
+ // face genuinely does not cover — first pinning that gap per
793
+ // codepoint — so the control shot IS this face's
794
+ // everything-uncovered rendering through the identical pipeline.
795
+ // Had the receipt string resolved to notdef, the two shots would
796
+ // match: same face, same per-glyph fallback, same advances.
797
+ // Differing proves the receipt pixels are real ideograph outlines,
798
+ // not any face's fallback.
799
+ var uncovered_index: usize = 0;
800
+ while (uncovered_index < cjk_uncovered_text.len) {
801
+ const len = try std.unicode.utf8ByteSequenceLength(cjk_uncovered_text[uncovered_index]);
802
+ const codepoint = try std.unicode.utf8Decode(cjk_uncovered_text[uncovered_index .. uncovered_index + len]);
803
+ uncovered_index += len;
804
+ try std.testing.expectEqual(@as(u16, 0), face.glyphIndex(codepoint));
805
+ }
806
+ try app_state.dispatch(&harness.runtime, 1, .show_uncovered_cjk);
807
+ const uncovered_shot = try fontFixtureScreenshot(harness, std.testing.allocator, registered_font_id);
808
+ defer std.testing.allocator.free(uncovered_shot);
809
+ try std.testing.expect(!std.mem.eql(u8, registered_shot, uncovered_shot));
810
+ }
811
+
812
+ // ------------------- late registration re-measures installed layouts
813
+
814
+ /// 你好 SDK — mixed CJK + Latin, chosen so the measured width MUST move
815
+ /// when the fixture face joins late. Pure-CJK width would not move: the
816
+ /// bundled estimator's East Asian wide fallback charges 1.0 em and the
817
+ /// fixture's ideographs really advance 1.0 em. The Latin tail is what
818
+ /// moves — the estimator charges Geist's own sub-em ASCII advances,
819
+ /// while the fixture face (subsetted to four ideographs plus notdef)
820
+ /// answers every ASCII codepoint with its 1.0 em notdef advance, the
821
+ /// documented no-cascade fallback registered faces take for uncovered
822
+ /// codepoints.
823
+ const late_mixed_text = "\u{4F60}\u{597D} SDK";
824
+
825
+ const late_window_label = "late-panel";
826
+ const late_window_canvas_label = "late-panel-canvas";
827
+
828
+ fn lateFontAppView(ui: *FontApp.Ui, model: *const FontAppModel) FontApp.Ui.Node {
829
+ _ = model;
830
+ // A row lays text out at its MEASURED intrinsic width (a column's
831
+ // default cross alignment stretches children to the column width),
832
+ // so this widget's frame is a direct function of what the
833
+ // measurement seam answers for the string.
834
+ return ui.row(.{ .gap = 8, .padding = 12 }, .{
835
+ ui.text(.{}, late_mixed_text),
836
+ });
837
+ }
838
+
839
+ fn lateFontAppWindows(model: *const FontAppModel, scratch: *FontApp.WindowsScratch) []const FontApp.WindowDescriptor {
840
+ _ = model;
841
+ scratch.windows[0] = .{
842
+ .label = late_window_label,
843
+ .canvas_label = late_window_canvas_label,
844
+ .title = "Late",
845
+ .width = 240,
846
+ .height = 200,
847
+ };
848
+ return scratch.windows[0..1];
849
+ }
850
+
851
+ fn lateFontAppWindowView(ui: *FontApp.Ui, model: *const FontAppModel, window_label: []const u8) FontApp.Ui.Node {
852
+ std.debug.assert(std.mem.eql(u8, window_label, late_window_label));
853
+ return lateFontAppView(ui, model);
854
+ }
855
+
856
+ /// The laid-out frame width of the view's one text widget.
857
+ fn lateTextFrameWidth(runtime: *core.Runtime, window_id: platform.WindowId, label: []const u8) !f32 {
858
+ const layout = try runtime.canvasWidgetLayout(window_id, label);
859
+ for (layout.nodes) |node| {
860
+ if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, late_mixed_text)) return node.widget.frame.width;
861
+ }
862
+ return error.TestUnexpectedResult;
863
+ }
864
+
865
+ test "a face registered after install re-measures every installed surface's layout" {
866
+ const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
867
+ defer harness.destroy(std.testing.allocator);
868
+ harness.null_platform.gpu_surfaces = true;
869
+
870
+ // No declared fonts: the app installs measuring registered_font_id
871
+ // (the tokens' face slot) through the bundled estimator, the exact
872
+ // pre-registration state the fonts page's late-registration promise
873
+ // starts from. A declared secondary window pins the multi-surface
874
+ // half of the promise.
875
+ var options = fontAppOptions(&.{});
876
+ options.view = lateFontAppView;
877
+ options.windows_fn = lateFontAppWindows;
878
+ options.window_view = lateFontAppWindowView;
879
+ const app_state = try std.testing.allocator.create(FontApp);
880
+ defer std.testing.allocator.destroy(app_state);
881
+ app_state.* = FontApp.init(std.testing.allocator, .{}, options);
882
+ defer app_state.deinit();
883
+ const app = app_state.app();
884
+ try harness.start(app);
885
+
886
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
887
+ .label = font_app_canvas_label,
888
+ .size = geometry.SizeF.init(240, 200),
889
+ .scale_factor = 1,
890
+ .frame_index = 1,
891
+ .timestamp_ns = 1_000_000,
892
+ .nonblank = true,
893
+ } });
894
+ try std.testing.expect(app_state.installed);
895
+
896
+ // The always-declared panel window opened during the installing
897
+ // rebuild's window reconcile; its own first frame installs its tree.
898
+ const panel_id = blk: {
899
+ var buffer: [platform.max_windows]platform.WindowInfo = undefined;
900
+ for (harness.runtime.listWindows(&buffer)) |info| {
901
+ if (std.mem.eql(u8, info.label, late_window_label)) break :blk info.id;
902
+ }
903
+ return error.TestUnexpectedResult;
904
+ };
905
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
906
+ .window_id = panel_id,
907
+ .label = late_window_canvas_label,
908
+ .size = geometry.SizeF.init(240, 200),
909
+ .scale_factor = 1,
910
+ .frame_index = 1,
911
+ .timestamp_ns = 2_000_000,
912
+ .nonblank = true,
913
+ } });
914
+
915
+ const main_before = try lateTextFrameWidth(&harness.runtime, 1, font_app_canvas_label);
916
+ const panel_before = try lateTextFrameWidth(&harness.runtime, panel_id, late_window_canvas_label);
917
+ try std.testing.expectEqual(main_before, panel_before);
918
+
919
+ // Late registration through the runtime seam — the embedder path the
920
+ // fonts page documents under the `Options.fonts` sugar.
921
+ try harness.runtime.registerCanvasFont(registered_font_id, cjk_receipt_bytes);
922
+
923
+ // The registration requested a frame for every open surface; ONE
924
+ // arriving frame (the main canvas's here — arrival order is the
925
+ // platform's) must re-measure every installed surface, not just the
926
+ // surface whose frame landed.
927
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
928
+ .label = font_app_canvas_label,
929
+ .size = geometry.SizeF.init(240, 200),
930
+ .scale_factor = 1,
931
+ .frame_index = 2,
932
+ .timestamp_ns = 3_000_000,
933
+ .nonblank = true,
934
+ } });
935
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.dispatchErrors().len);
936
+
937
+ const main_after = try lateTextFrameWidth(&harness.runtime, 1, font_app_canvas_label);
938
+ const panel_after = try lateTextFrameWidth(&harness.runtime, panel_id, late_window_canvas_label);
939
+ // Both surfaces now hold frames measured with the registered face's
940
+ // own advances — the widths moved, and moved identically.
941
+ try std.testing.expect(@abs(main_after - main_before) > 1);
942
+ try std.testing.expect(@abs(panel_after - panel_before) > 1);
943
+ try std.testing.expectEqual(main_after, panel_after);
944
+
945
+ // The re-measured width IS the face-aware seam's answer scaled into
946
+ // the frame, not merely a different guess: the face measures the
947
+ // string wider than the estimator did (1.0 em notdef per ASCII
948
+ // codepoint versus Geist's sub-em advances), so the frame grew.
949
+ try std.testing.expect(main_after > main_before);
950
+ }
951
+
952
+ // ------------------------ late registration adopts only on success
953
+
954
+ /// Model for the failed-rebuild retry test below: `row_count` extra rows
955
+ /// join the measured text, so the test can push one rebuild past the
956
+ /// per-view widget budget and then heal it.
957
+ const RetryFontModel = struct {
958
+ row_count: usize = 0,
959
+
960
+ pub fn rows(model: *const RetryFontModel, arena: std.mem.Allocator) []const usize {
961
+ const out = arena.alloc(usize, model.row_count) catch return &.{};
962
+ for (out, 0..) |*slot, index| slot.* = index;
963
+ return out;
964
+ }
965
+ };
966
+ const RetryFontMsg = union(enum) { noop };
967
+ const RetryFontApp = ui_app_model.UiApp(RetryFontModel, RetryFontMsg);
968
+
969
+ fn retryFontUpdate(model: *RetryFontModel, msg: RetryFontMsg) void {
970
+ _ = model;
971
+ _ = msg;
972
+ }
973
+
974
+ fn retryFontTokens(model: *const RetryFontModel) canvas.DesignTokens {
975
+ _ = model;
976
+ var tokens = canvas.DesignTokens{};
977
+ tokens.typography.font_id = registered_font_id;
978
+ return tokens;
979
+ }
980
+
981
+ fn retryRowKey(index: *const usize) canvas.UiKey {
982
+ return canvas.uiKey(@as(u64, index.*));
983
+ }
984
+
985
+ fn retryRow(ui: *RetryFontApp.Ui, index: *const usize) RetryFontApp.Ui.Node {
986
+ return ui.text(.{}, ui.fmt("Row {d}", .{index.*}));
987
+ }
988
+
989
+ fn retryFontView(ui: *RetryFontApp.Ui, model: *const RetryFontModel) RetryFontApp.Ui.Node {
990
+ // The measured text rides in a row (intrinsic width, like
991
+ // lateFontAppView above) so its frame moves when the registered
992
+ // face's advances replace the estimator's.
993
+ return ui.column(.{ .gap = 2 }, .{
994
+ ui.row(.{ .gap = 8, .padding = 12 }, .{ ui.text(.{}, late_mixed_text) }),
995
+ ui.each(model.rows(ui.arena), retryRowKey, retryRow),
996
+ });
997
+ }
998
+
999
+ test "a failed late-font rebuild leaves the count unadopted so the next healthy frame retries" {
1000
+ // The failing rebuild teaches through std.log; without lowering the
1001
+ // level the warning would fail the build runner's stderr check.
1002
+ const saved_log_level = std.testing.log_level;
1003
+ std.testing.log_level = .err;
1004
+ defer std.testing.log_level = saved_log_level;
1005
+
1006
+ const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
1007
+ defer harness.destroy(std.testing.allocator);
1008
+ harness.null_platform.gpu_surfaces = true;
1009
+
1010
+ const app_state = try std.testing.allocator.create(RetryFontApp);
1011
+ defer std.testing.allocator.destroy(app_state);
1012
+ app_state.* = RetryFontApp.init(std.testing.allocator, .{}, .{
1013
+ .name = "ui-app-font-retry",
1014
+ .scene = font_app_scene,
1015
+ .canvas_label = font_app_canvas_label,
1016
+ .tokens_fn = retryFontTokens,
1017
+ .update = retryFontUpdate,
1018
+ .view = retryFontView,
1019
+ });
1020
+ defer app_state.deinit();
1021
+ const app = app_state.app();
1022
+ try harness.start(app);
1023
+
1024
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
1025
+ .label = font_app_canvas_label,
1026
+ .size = geometry.SizeF.init(240, 200),
1027
+ .scale_factor = 1,
1028
+ .frame_index = 1,
1029
+ .timestamp_ns = 1_000_000,
1030
+ .nonblank = true,
1031
+ } });
1032
+ try std.testing.expect(app_state.installed);
1033
+ const width_before = try lateTextFrameWidth(&harness.runtime, 1, font_app_canvas_label);
1034
+
1035
+ // Production policy: dispatch degrades errors instead of dying —
1036
+ // the policy under which a rebuild that adopted the font count
1037
+ // BEFORE succeeding would strand stale layouts marked font-current,
1038
+ // never retried.
1039
+ harness.runtime.dispatch_error_policy = .degrade;
1040
+
1041
+ // Grow the model past the per-view widget budget (mutated directly
1042
+ // — no dispatch, so no rebuild yet), then register a face late so
1043
+ // the next frame's late-registration rebuild is the failing one.
1044
+ app_state.model.row_count = core.max_canvas_widget_nodes_per_view + 40;
1045
+ try harness.runtime.registerCanvasFont(registered_font_id, cjk_receipt_bytes);
1046
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
1047
+ .label = font_app_canvas_label,
1048
+ .size = geometry.SizeF.init(240, 200),
1049
+ .scale_factor = 1,
1050
+ .frame_index = 2,
1051
+ .timestamp_ns = 2_000_000,
1052
+ .nonblank = true,
1053
+ } });
1054
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.dispatchErrors().len);
1055
+ // The failed rebuild must NOT adopt the count: the mismatch it
1056
+ // leaves behind is exactly what the next presented frame reads as
1057
+ // its retry trigger.
1058
+ try std.testing.expect(app_state.fonts_built_count != harness.runtime.registeredCanvasFontCount());
1059
+
1060
+ // Heal the model; the next presented frame observes the mismatch,
1061
+ // rebuilds every surface, and only then adopts.
1062
+ app_state.model.row_count = 0;
1063
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
1064
+ .label = font_app_canvas_label,
1065
+ .size = geometry.SizeF.init(240, 200),
1066
+ .scale_factor = 1,
1067
+ .frame_index = 3,
1068
+ .timestamp_ns = 3_000_000,
1069
+ .nonblank = true,
1070
+ } });
1071
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.dispatchErrors().len);
1072
+ try std.testing.expectEqual(harness.runtime.registeredCanvasFontCount(), app_state.fonts_built_count);
1073
+
1074
+ // The retried rebuild re-measured with the registered face, not
1075
+ // merely repainted: the face answers the mixed string wider than
1076
+ // the estimator did (1.0 em notdef per ASCII codepoint), so the
1077
+ // text widget's frame grew.
1078
+ const width_after = try lateTextFrameWidth(&harness.runtime, 1, font_app_canvas_label);
1079
+ try std.testing.expect(width_after > width_before);
1080
+ }
1081
+
645
1082
  test "ui app fonts option surfaces the glyph-budget refusal as a teaching error" {
646
1083
  const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
647
1084
  defer harness.destroy(std.testing.allocator);
@@ -75,6 +75,34 @@ pub const max_registered_canvas_font_bytes = canvas_limits.max_registered_canvas
75
75
  pub const CanvasFontEntry = struct {
76
76
  id: canvas.FontId = 0,
77
77
  bytes: []const u8 = &.{},
78
+ /// The host-side unregistration owner — the platform's
79
+ /// `unregister_gpu_surface_font_fn` and its context, captured from
80
+ /// the services in effect when THIS registration was pushed to the
81
+ /// host. Captured precisely because `Runtime.options` is public and
82
+ /// mutable while registration and teardown can be a whole runtime
83
+ /// lifetime apart: the registration must be returned to the host
84
+ /// that received it, so `Runtime.deinit` unregisters through this
85
+ /// captured pair and mutating `options.platform` on a live runtime
86
+ /// retargets nothing (the `owned_allocator` identity-freeze
87
+ /// doctrine, applied to the host seam — a deinit that read the live
88
+ /// options would unregister against whatever platform the option
89
+ /// points at by teardown time, stranding the original host's
90
+ /// descriptor and caches). Null when the platform at registration
91
+ /// time had no unregister seam: it retained nothing to return.
92
+ host_unregister_fn: ?*const fn (context: ?*anyopaque, id: u64, token: u64) anyerror!void = null,
93
+ host_unregister_context: ?*anyopaque = null,
94
+ /// The host's ownership token for THIS registration, returned by
95
+ /// `registerGpuSurfaceFont` and passed back through the owner above
96
+ /// at deinit. Host font state is per-process while ids are only
97
+ /// permanent per-runtime, so a later runtime may re-register this
98
+ /// entry's id (last wins, the documented lifecycle); the host
99
+ /// removes an id's state only while its current registration still
100
+ /// carries the presented token, which keeps an older runtime's
101
+ /// deinit from tearing down a newer runtime's live face. 0 when the
102
+ /// platform returned no token (a stateless accept, or no register
103
+ /// seam at all): nothing was installed for this registration, and
104
+ /// an unregister carrying 0 removes nothing.
105
+ host_registration_token: u64 = 0,
78
106
  };
79
107
 
80
108
  /// Placeholder measure fn for the runtime's font-aware provider field
@@ -151,16 +179,38 @@ pub fn RuntimeCanvasFonts(comptime Runtime: type) type {
151
179
  // exact silent fallback this seam forbids. Platforms without
152
180
  // host-side text may lack the seam (`UnsupportedService`):
153
181
  // the engine measures with the parsed face and inks it
154
- // through the reference renderer, so nothing is lost.
155
- self.options.platform.services.registerGpuSurfaceFont(.{ .id = id, .ttf = pooled }) catch |err| switch (err) {
156
- error.UnsupportedService => {
157
- if (self.options.platform.services.measure_text_fn != null) return error.FontHostRegistrationUnsupported;
182
+ // through the reference renderer, so nothing is lost. ONE
183
+ // services read serves both the sync and the captured return
184
+ // path below, so the host that hears the registration is the
185
+ // host the entry's unregister owner names.
186
+ const services = self.options.platform.services;
187
+ // The returned token is the host's ownership handle for THIS
188
+ // registration (0 when the host retained nothing); it rides
189
+ // the entry beside the captured owner so deinit can tell the
190
+ // host which registration to remove — see
191
+ // `CanvasFontEntry.host_registration_token`.
192
+ const host_token: u64 = services.registerGpuSurfaceFont(.{ .id = id, .ttf = pooled }) catch |err| switch (err) {
193
+ error.UnsupportedService => blk: {
194
+ if (services.measure_text_fn != null) return error.FontHostRegistrationUnsupported;
195
+ break :blk 0;
158
196
  },
159
197
  else => return err,
160
198
  };
161
199
 
162
200
  self.canvas_font_faces[index] = face;
163
- self.canvas_font_entries[index] = .{ .id = id, .bytes = pooled };
201
+ // The entry carries its own host unregistration owner,
202
+ // captured now (see `CanvasFontEntry.host_unregister_fn`):
203
+ // `options.platform` is publicly mutable, and the teardown
204
+ // return must land on the host that received this
205
+ // registration, never on whatever platform the option holds
206
+ // by `Runtime.deinit` time.
207
+ self.canvas_font_entries[index] = .{
208
+ .id = id,
209
+ .bytes = pooled,
210
+ .host_unregister_fn = services.unregister_gpu_surface_font_fn,
211
+ .host_unregister_context = services.context,
212
+ .host_registration_token = host_token,
213
+ };
164
214
  self.canvas_font_count = index + 1;
165
215
  // Bind the font-aware measure provider on first registration.
166
216
  // The runtime address is stable from here on (registration
@@ -250,7 +300,12 @@ pub fn RuntimeCanvasFonts(comptime Runtime: type) type {
250
300
  /// A face joined the registry: force every gpu_surface view to
251
301
  /// re-render its next frame (text referencing the id may already
252
302
  /// be retained) and request frames so the repaint is not gated on
253
- /// other input — the image-registry choreography.
303
+ /// other input — the image-registry choreography. Re-rendering
304
+ /// alone re-inks RETAINED geometry; installed UiApps complete the
305
+ /// late-registration story by comparing the registered count on
306
+ /// each presented frame and rebuilding every surface so layout
307
+ /// re-measures with the new face (ui_app.zig,
308
+ /// `rebuildForRegisteredFonts`).
254
309
  fn noteCanvasFontsChanged(self: *Runtime) void {
255
310
  // A new face changes what the measurement seam answers for
256
311
  // its id (host providers just learned the face; the engine