@native-sdk/cli 0.11.0 → 0.11.1

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 (182) hide show
  1. package/build/app.zig +73 -37
  2. package/build.zig +239 -5
  3. package/native-sdk.d.ts +5 -1
  4. package/package.json +13 -12
  5. package/packages/core/compile-surface/core.ts +30 -0
  6. package/packages/core/package-lock.json +6 -6
  7. package/packages/core/package.json +3 -3
  8. package/packages/core/scripts/scriptc-toolchain.json +44 -44
  9. package/packages/core/scripts/stage_external_core.mjs +27 -16
  10. package/packages/core/sdk/core.d.ts +24 -1
  11. package/packages/core/sdk/core.ts +51 -1
  12. package/packages/core/sdk/events.d.ts +5 -0
  13. package/packages/core/sdk/events.ts +10 -0
  14. package/packages/core/src/bridge_descriptors.ts +3 -3
  15. package/packages/core/src/checker.ts +20 -0
  16. package/packages/core/src/cli.ts +2 -11
  17. package/packages/core/src/context_menus.ts +76 -0
  18. package/packages/core/src/diagnostics.ts +7 -0
  19. package/packages/core/src/extent_program.ts +53 -0
  20. package/packages/core/src/frontend.ts +12 -10
  21. package/packages/core/src/markup_canonical.ts +140 -0
  22. package/packages/core/src/markup_construction.ts +189 -0
  23. package/packages/core/src/markup_display.ts +43 -0
  24. package/packages/core/src/markup_document.ts +226 -0
  25. package/packages/core/src/markup_elements.ts +225 -0
  26. package/packages/core/src/markup_expressions.ts +79 -0
  27. package/packages/core/src/markup_imports.ts +146 -0
  28. package/packages/core/src/markup_parser.ts +228 -0
  29. package/packages/core/src/markup_runtime.ts +93 -0
  30. package/packages/core/src/markup_scalars.ts +143 -0
  31. package/packages/core/src/markup_shapes.ts +243 -0
  32. package/packages/core/src/markup_specialized.ts +242 -0
  33. package/packages/core/src/markup_text.ts +86 -0
  34. package/packages/core/src/markup_values.ts +336 -0
  35. package/packages/core/src/portable_sources.json +24 -0
  36. package/packages/core/src/registered_font.ts +2 -3
  37. package/packages/core/src/runtime_policy.ts +24 -0
  38. package/packages/core/src/scalar_text.ts +1 -3
  39. package/packages/core/src/scroll_mutation.ts +86 -0
  40. package/packages/core/src/scroll_tree.ts +86 -0
  41. package/packages/core/src/service_contract.ts +53 -2
  42. package/packages/core/src/sqlite_cli.ts +3 -46
  43. package/packages/core/src/sqlite_codegen.ts +84 -0
  44. package/packages/core/src/staged_rebuild.ts +60 -0
  45. package/packages/core/src/view_bridge.ts +161 -0
  46. package/packages/core/src/view_check.ts +145 -0
  47. package/packages/core/src/view_evaluator.ts +823 -0
  48. package/packages/core/src/view_frontend.ts +340 -218
  49. package/packages/core/src/view_program.ts +221 -0
  50. package/packages/core/src/view_reload.ts +166 -0
  51. package/packages/core/src/view_reload_watch.ts +98 -0
  52. package/packages/core/src/view_sources.ts +34 -0
  53. package/packages/core/src/virtual_windows.ts +88 -0
  54. package/packages/core/src/web_panes.ts +67 -0
  55. package/packages/core/src/widget_continuity.ts +37 -0
  56. package/packages/core/src/widget_reconcile.ts +63 -0
  57. package/packages/core/src/widget_retention.ts +62 -0
  58. package/skill-data/native-ui/SKILL.md +2 -2
  59. package/skill-data/ts-core/SKILL.md +5 -3
  60. package/skill-data/ts-services/references/service-surface.md +29 -5
  61. package/src/app_runner/root.zig +14 -0
  62. package/src/app_runner/ts_compiled_view.zig +88 -17
  63. package/src/app_runner/ts_core_main.zig +43 -5
  64. package/src/app_runner/ts_core_mobile.zig +1 -1
  65. package/src/automation/server.zig +56 -3
  66. package/src/embed/host.zig +2 -0
  67. package/src/embed/tests.zig +2 -0
  68. package/src/embed/ui_host.zig +2 -0
  69. package/src/platform/keyboard_input.zig +32 -0
  70. package/src/platform/linux/root.zig +2 -0
  71. package/src/platform/macos/appkit_host.h +7 -2
  72. package/src/platform/macos/appkit_host.m +31 -9
  73. package/src/platform/macos/cef_host.mm +15 -1
  74. package/src/platform/macos/global_shortcuts.zig +138 -0
  75. package/src/platform/macos/key_codes.zig +102 -0
  76. package/src/platform/macos/keyboard_input.zig +81 -0
  77. package/src/platform/macos/root.zig +25 -3
  78. package/src/platform/macos/webview_inspection_test.m +106 -0
  79. package/src/platform/null_platform.zig +104 -0
  80. package/src/platform/root.zig +14 -0
  81. package/src/platform/types.zig +102 -0
  82. package/src/platform/windows/global_shortcuts.zig +157 -0
  83. package/src/platform/windows/key_codes.zig +63 -0
  84. package/src/platform/windows/keyboard_input.zig +136 -0
  85. package/src/platform/windows/root.zig +15 -0
  86. package/src/primitives/canvas/decision_reuse.zig +133 -0
  87. package/src/primitives/canvas/font_ttf.zig +27 -3
  88. package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
  89. package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
  90. package/src/primitives/canvas/fonts/OFL.txt +93 -0
  91. package/src/primitives/canvas/fonts/README.md +15 -0
  92. package/src/primitives/canvas/intrinsic_measure_policy.zig +4 -1
  93. package/src/primitives/canvas/markdown_tests.zig +3 -3
  94. package/src/primitives/canvas/reference.zig +3 -5
  95. package/src/primitives/canvas/reference_tests.zig +58 -0
  96. package/src/primitives/canvas/registered_font_policy.zig +1 -1
  97. package/src/primitives/canvas/root.zig +7 -4
  98. package/src/primitives/canvas/text_metrics.zig +32 -39
  99. package/src/primitives/canvas/text_metrics_tests.zig +52 -7
  100. package/src/primitives/canvas/text_query_policy.zig +142 -3
  101. package/src/primitives/canvas/text_scalar_policy.zig +80 -2
  102. package/src/primitives/canvas/tokens.zig +9 -0
  103. package/src/primitives/canvas/ui.zig +1 -0
  104. package/src/primitives/canvas/ui_markup.zig +69 -14
  105. package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
  106. package/src/primitives/canvas/ui_markup_compiled.zig +1407 -23
  107. package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
  108. package/src/primitives/canvas/ui_markup_construction.zig +146 -0
  109. package/src/primitives/canvas/ui_markup_display.zig +82 -0
  110. package/src/primitives/canvas/ui_markup_document.zig +196 -0
  111. package/src/primitives/canvas/ui_markup_elements.zig +163 -0
  112. package/src/primitives/canvas/ui_markup_expr.zig +192 -2
  113. package/src/primitives/canvas/ui_markup_imports.zig +262 -0
  114. package/src/primitives/canvas/ui_markup_parser.zig +117 -0
  115. package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
  116. package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
  117. package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
  118. package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
  119. package/src/primitives/canvas/ui_markup_text.zig +128 -0
  120. package/src/primitives/canvas/ui_markup_values.zig +368 -0
  121. package/src/primitives/canvas/ui_markup_view.zig +2817 -74
  122. package/src/primitives/canvas/virtual_extent_policy.zig +58 -0
  123. package/src/primitives/canvas/virtual_extents.zig +47 -33
  124. package/src/primitives/canvas/widget_metric_policy.zig +4 -1
  125. package/src/primitives/canvas/widget_runtime.zig +2 -1
  126. package/src/root.zig +5 -0
  127. package/src/runtime/api.zig +1 -0
  128. package/src/runtime/bridge_descriptor_policy.zig +1 -1
  129. package/src/runtime/bridge_payload.zig +2 -0
  130. package/src/runtime/canvas_widget_context_menu.zig +179 -0
  131. package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
  132. package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
  133. package/src/runtime/canvas_widget_runtime.zig +306 -92
  134. package/src/runtime/context_menu_policy.zig +112 -0
  135. package/src/runtime/core.zig +14 -0
  136. package/src/runtime/effects.zig +172 -4
  137. package/src/runtime/effects_host_tests.zig +25 -0
  138. package/src/runtime/flow.zig +1 -0
  139. package/src/runtime/flow_reference.zig +1 -0
  140. package/src/runtime/global_shortcuts.zig +75 -0
  141. package/src/runtime/keyboard_input.zig +86 -0
  142. package/src/runtime/root.zig +3 -0
  143. package/src/runtime/scroll_mutation_policy.zig +130 -0
  144. package/src/runtime/scroll_mutation_runtime.zig +158 -0
  145. package/src/runtime/scroll_tree_policy.zig +99 -0
  146. package/src/runtime/session_record.zig +7 -0
  147. package/src/runtime/staged_rebuild_policy.zig +83 -0
  148. package/src/runtime/system_services.zig +19 -0
  149. package/src/runtime/tests.zig +1 -0
  150. package/src/runtime/ts_core_host.zig +126 -0
  151. package/src/runtime/ts_ui_app.zig +24 -0
  152. package/src/runtime/ui_app.zig +274 -31
  153. package/src/runtime/ui_app_tests.zig +48 -179
  154. package/src/runtime/ui_app_window_tests.zig +133 -0
  155. package/src/runtime/view_reload.zig +226 -0
  156. package/src/runtime/view_widget_scroll.zig +30 -21
  157. package/src/runtime/view_widget_tree.zig +43 -13
  158. package/src/runtime/virtual_window_policy.zig +128 -0
  159. package/src/runtime/web_pane_policy.zig +111 -0
  160. package/src/runtime/web_pane_test_cases.zig +204 -0
  161. package/src/runtime/widget_continuity_policy.zig +73 -0
  162. package/src/runtime/widget_reconcile_policy.zig +81 -0
  163. package/src/runtime/widget_retention_policy.zig +75 -0
  164. package/src/runtime/window_views.zig +19 -4
  165. package/src/security/root.zig +1 -0
  166. package/src/tooling/templates.zig +8 -0
  167. package/src/tooling/ts_core.zig +84 -1
  168. package/src/tooling/verbs.zig +79 -15
  169. package/tools/corewire/core.test.ts +25 -3
  170. package/tools/corewire/core_abi.zig +4 -0
  171. package/tools/corewire/core_contract.ts +1 -1
  172. package/tools/corewire/core_goldens.json +75 -75
  173. package/tools/corewire/core_projection.ts +3 -2
  174. package/tools/corewire/emit_mirror.ts +19 -9
  175. package/tools/corewire/facade_templates.ts +1 -1
  176. package/tools/corewire/intake.test.ts +4 -0
  177. package/tools/corewire/intake_goldens.json +50 -50
  178. package/tools/corewire/invocation.test.ts +4 -0
  179. package/tools/corewire/invocation_goldens.json +3 -3
  180. package/tools/corewire/mirror_templates.ts +4 -0
  181. package/tools/corewire/profile_emission.ts +2 -0
  182. package/tools/corewire/sidecar.zig +2 -0
@@ -108,10 +108,9 @@ pub const mediaSurfacePlaceholderColor = @import("widget_render.zig").mediaSurfa
108
108
  pub const default_sans_font_id: FontId = 1;
109
109
  pub const default_mono_font_id: FontId = 2;
110
110
  // Reserved sans variant ids for inline span styling. The deterministic
111
- // estimator applies per-variant width factors; hosts that have not mapped
112
- // these ids to real faces fall back to the regular sans face, and because
113
- // the measurement seam carries the same id, measured text always matches
114
- // drawn text.
111
+ // estimator and software renderer use matching bundled regular, medium
112
+ // and bold faces. Italic ids preserve weight with upright outlines in
113
+ // software; platform text providers may resolve real italic faces.
115
114
  pub const default_sans_medium_font_id: FontId = 3;
116
115
  pub const default_sans_bold_font_id: FontId = 4;
117
116
  pub const default_sans_italic_font_id: FontId = 5;
@@ -581,6 +580,7 @@ pub const lazy_tls = @import("lazy_tls.zig");
581
580
  // `ui_markup_compiled.zig` (comptime parse: the release engine, no parser in
582
581
  // the binary).
583
582
  pub const ui_markup = @import("ui_markup.zig");
583
+ pub const ui_markup_shapes = @import("ui_markup_shapes.zig");
584
584
  /// Structural event shapes shared by markup backends and native consumers.
585
585
  pub const ui_markup_reflect = @import("ui_markup_reflect.zig");
586
586
  /// Widget provenance (write-back's read half): structural id -> authored
@@ -959,6 +959,7 @@ pub const binary_packet_load_action_patch = serialization.binary_packet_load_act
959
959
 
960
960
  test {
961
961
  _ = @import("tests.zig");
962
+ _ = @import("decision_reuse.zig");
962
963
  }
963
964
 
964
965
  /// Copied portable leaf command and geometry plans.
@@ -968,6 +969,8 @@ pub const indicator_plan_policy = @import("indicator_plan_policy.zig");
968
969
  pub const effect_plan_policy = @import("effect_plan_policy.zig");
969
970
 
970
971
  pub const text_scalar_policy = @import("text_scalar_policy.zig");
972
+ /// Exact reuse of decisions from policies that answer from their request bytes alone.
973
+ pub const decision_reuse = @import("decision_reuse.zig");
971
974
  pub const registered_font_policy = @import("registered_font_policy.zig");
972
975
 
973
976
  pub const GpuPlanningPolicy = @import("gpu_planning_policy.zig");
@@ -149,26 +149,21 @@ comptime {
149
149
  @compileError("bundled mono face pitch drifted from the estimator's mono advance");
150
150
  }
151
151
 
152
- /// The bundled face's `.notdef` advance in em units (0.6 em in Geist).
153
- /// This is the width the face itself declares for "I do not have this
154
- /// glyph", so it is what layout charges for codepoints outside the
155
- /// face's coverage (and for control or malformed bytes, which paint as
156
- /// the same block fallback).
157
- const notdef_advance_em: f32 = faceAdvanceEm(&font_ttf.geist_regular, 0);
158
-
159
152
  /// Em-unit advances for the printable ASCII range, read from the
160
- /// bundled face's `cmap`/`hmtx` at comptime. Layout therefore measures
153
+ /// bundled faces' `cmap`/`hmtx` at comptime. Layout therefore measures
161
154
  /// with exactly the advances the reference renderer inks; a bundle
162
155
  /// swap that drops ASCII coverage is a compile error.
163
- const ascii_advance_em: [0x7F - 0x20]f32 = blk: {
164
- @setEvalBranchQuota(400_000);
165
- var table: [0x7F - 0x20]f32 = undefined;
166
- for (&table, 0x20..) |*slot, codepoint| {
167
- const glyph = font_ttf.geist_regular.glyphIndex(codepoint);
168
- if (glyph == 0) @compileError("bundled Geist face is missing printable ASCII coverage");
169
- slot.* = faceAdvanceEm(&font_ttf.geist_regular, glyph);
156
+ const ascii_advance_em: [font_ttf.bundled_sans_faces.len][0x7F - 0x20]f32 = blk: {
157
+ @setEvalBranchQuota(1_200_000);
158
+ var tables: [font_ttf.bundled_sans_faces.len][0x7F - 0x20]f32 = undefined;
159
+ for (&tables, font_ttf.bundled_sans_faces) |*table, face| {
160
+ for (table, 0x20..) |*slot, codepoint| {
161
+ const glyph = face.glyphIndex(codepoint);
162
+ if (glyph == 0) @compileError("bundled Geist face is missing printable ASCII coverage");
163
+ slot.* = faceAdvanceEm(face, glyph);
164
+ }
170
165
  }
171
- break :blk table;
166
+ break :blk tables;
172
167
  };
173
168
 
174
169
  fn faceAdvanceEm(face: *const font_ttf.Face, glyph: u16) f32 {
@@ -179,8 +174,12 @@ fn faceAdvanceEm(face: *const font_ttf.Face, glyph: u16) f32 {
179
174
  /// ASCII uses the same comptime hmtx facts as the native reference, avoiding
180
175
  /// repeated cmap walks when an editor measures long numeric drafts.
181
176
  pub fn bundledGlyphAdvanceEm(codepoint: u21) ?f32 {
182
- if (codepoint >= 0x20 and codepoint < 0x7f) return ascii_advance_em[codepoint - 0x20];
183
- const face = &font_ttf.geist_regular;
177
+ return bundledGlyphAdvanceEmForFont(default_sans_font_id, codepoint);
178
+ }
179
+
180
+ pub fn bundledGlyphAdvanceEmForFont(font_id: FontId, codepoint: u21) ?f32 {
181
+ if (font_id != default_mono_font_id and codepoint >= 0x20 and codepoint < 0x7f) return ascii_advance_em[font_ttf.bundledSansFaceIndex(font_id)][codepoint - 0x20];
182
+ const face = font_ttf.bundledFaceForFontId(font_id);
184
183
  const glyph = face.glyphIndex(codepoint);
185
184
  return if (glyph == 0) null else faceAdvanceEm(face, glyph);
186
185
  }
@@ -188,19 +187,22 @@ pub fn bundledGlyphAdvanceEm(codepoint: u21) ?f32 {
188
187
  pub fn estimateTextAdvanceForBytes(font_id: FontId, bytes: []const u8, size: f32) f32 {
189
188
  if (bytes.len == 0) return 0;
190
189
  if (font_id == default_mono_font_id) return size * mono_advance_em;
191
- const weight = sansVariantWidthFactor(font_id);
192
- return size * clusterAdvanceEm(bytes) * weight;
190
+ return size * clusterAdvanceEm(font_id, bytes);
193
191
  }
194
192
 
195
193
  /// The bundled face's U+2026 HORIZONTAL ELLIPSIS advance in em units,
196
194
  /// read at comptime like the ASCII table. Elision consults the marker's
197
195
  /// advance once per overflowing line every frame, so the estimator path
198
196
  /// answers from this constant instead of re-walking the `cmap`.
199
- const ellipsis_advance_em: f32 = blk: {
200
- @setEvalBranchQuota(400_000);
201
- const glyph = font_ttf.geist_regular.glyphIndex(0x2026);
202
- if (glyph == 0) @compileError("bundled Geist face is missing U+2026 (the elision marker)");
203
- break :blk faceAdvanceEm(&font_ttf.geist_regular, glyph);
197
+ const ellipsis_advance_em: [font_ttf.bundled_sans_faces.len]f32 = blk: {
198
+ @setEvalBranchQuota(1_200_000);
199
+ var advances: [font_ttf.bundled_sans_faces.len]f32 = undefined;
200
+ for (&advances, font_ttf.bundled_sans_faces) |*advance, face| {
201
+ const glyph = face.glyphIndex(0x2026);
202
+ if (glyph == 0) @compileError("bundled Geist face is missing U+2026 (the elision marker)");
203
+ advance.* = faceAdvanceEm(face, glyph);
204
+ }
205
+ break :blk advances;
204
206
  };
205
207
 
206
208
  /// Estimator advance of the elision marker ("…"): bit-identical to
@@ -208,7 +210,7 @@ const ellipsis_advance_em: f32 = blk: {
208
210
  /// codepoint lookup.
209
211
  pub fn estimatedTextEllipsisAdvance(font_id: FontId, size: f32) f32 {
210
212
  if (font_id == default_mono_font_id) return size * mono_advance_em;
211
- return size * ellipsis_advance_em * sansVariantWidthFactor(font_id);
213
+ return size * ellipsis_advance_em[font_ttf.bundledSansFaceIndex(font_id)];
212
214
  }
213
215
 
214
216
  /// Deterministic width of `text` measured against an arbitrary parsed
@@ -266,10 +268,12 @@ fn clusterAdvanceEmForFace(face: *const font_ttf.Face, bytes: []const u8) f32 {
266
268
  /// glyph should occupy), uncovered symbol/pictograph blocks charge
267
269
  /// 0.8 em (the AppleSymbols cascade class real macOS text falls back
268
270
  /// to), everything else charges the face's own `.notdef` advance.
269
- fn clusterAdvanceEm(bytes: []const u8) f32 {
271
+ fn clusterAdvanceEm(font_id: FontId, bytes: []const u8) f32 {
272
+ const face = font_ttf.bundledFaceForFontId(font_id);
273
+ const notdef_advance_em = faceAdvanceEm(face, 0);
270
274
  if (bytes.len == 1) {
271
275
  const byte = bytes[0];
272
- if (byte >= 0x20 and byte < 0x7F) return ascii_advance_em[byte - 0x20];
276
+ if (byte >= 0x20 and byte < 0x7F) return ascii_advance_em[font_ttf.bundledSansFaceIndex(font_id)][byte - 0x20];
273
277
  return notdef_advance_em;
274
278
  }
275
279
  // `utf8Decode` asserts (rather than errors) when the slice length
@@ -277,7 +281,6 @@ fn clusterAdvanceEm(bytes: []const u8) f32 {
277
281
  // the end of a run — take the notdef fallback explicitly.
278
282
  if (bytes.len != utf8SequenceLength(bytes[0])) return notdef_advance_em;
279
283
  const codepoint = std.unicode.utf8Decode(bytes) catch return notdef_advance_em;
280
- const face = &font_ttf.geist_regular;
281
284
  const glyph = face.glyphIndex(codepoint);
282
285
  if (glyph != 0) return faceAdvanceEm(face, glyph);
283
286
  if (isEastAsianWideCodepoint(codepoint)) return 1.0;
@@ -331,16 +334,6 @@ fn isEastAsianWideCodepoint(codepoint: u21) bool {
331
334
  };
332
335
  }
333
336
 
334
- /// Width factor for the reserved sans variant ids (span weight/slant).
335
- /// The regular id keeps factor 1 exactly, so existing single-style text
336
- /// measurements — and every golden derived from them — are unchanged.
337
- fn sansVariantWidthFactor(font_id: FontId) f32 {
338
- if (font_id == canvas.default_sans_medium_font_id) return 1.02;
339
- if (font_id == canvas.default_sans_bold_font_id) return 1.04;
340
- if (font_id == canvas.default_sans_bold_italic_font_id) return 1.04;
341
- return 1;
342
- }
343
-
344
337
  pub fn estimatedGlyphAdvance(glyph: Glyph, size: f32) f32 {
345
338
  return @max(size * 0.25, glyph.advance);
346
339
  }
@@ -74,14 +74,37 @@ test "estimator fallbacks charge notdef and east asian wide cells" {
74
74
  try testing.expectEqual(@as(f32, 0.8), estimatorAdvanceEm(canvas.default_sans_font_id, "☑"));
75
75
  try testing.expectEqual(@as(f32, 0.8), estimatorAdvanceEm(canvas.default_sans_font_id, "⌘"));
76
76
 
77
- // Mono keeps its documented 0.6 em design pitch (Geist Mono's real
78
- // advance; no mono face is bundled), and the sans span variants keep
79
- // their width factors on top of the derived regular advances.
77
+ // Mono keeps its documented 0.6 em design pitch.
80
78
  try testing.expectEqual(@as(f32, 0.6), estimatorAdvanceEm(canvas.default_mono_font_id, "M"));
81
79
  try testing.expectEqual(@as(f32, 0.6), estimatorAdvanceEm(canvas.default_mono_font_id, "i"));
82
- const regular = estimatorAdvanceEm(canvas.default_sans_font_id, "n");
83
- try testing.expectEqual(regular * 1.02, estimatorAdvanceEm(canvas.default_sans_medium_font_id, "n"));
84
- try testing.expectEqual(regular * 1.04, estimatorAdvanceEm(canvas.default_sans_bold_font_id, "n"));
80
+ }
81
+
82
+ test "bundled sans weights measure their own glyphs and elision markers" {
83
+ const cases = .{
84
+ .{ canvas.default_sans_font_id, &font_ttf.geist_regular },
85
+ .{ canvas.default_sans_medium_font_id, &font_ttf.geist_medium },
86
+ .{ canvas.default_sans_bold_font_id, &font_ttf.geist_bold },
87
+ .{ canvas.default_sans_italic_font_id, &font_ttf.geist_regular },
88
+ .{ canvas.default_sans_bold_italic_font_id, &font_ttf.geist_bold },
89
+ };
90
+ inline for (cases) |case| {
91
+ const font_id = case[0];
92
+ const face = case[1];
93
+ var byte: u8 = 0x20;
94
+ while (byte < 0x7f) : (byte += 1) {
95
+ const bytes = [_]u8{byte};
96
+ const glyph = face.glyphIndex(byte);
97
+ try testing.expect(glyph != 0);
98
+ try testing.expectEqual(face.advance(glyph) / face.units_per_em, estimatorAdvanceEm(font_id, &bytes));
99
+ }
100
+ for (covered_multibyte_samples) |sample| {
101
+ try testing.expectEqual(text_metrics.estimateTextWidthForFace(face, sample, 13.25), text_metrics.estimateTextWidthForFont(font_id, sample, 13.25));
102
+ }
103
+ for ([_][]const u8{ "中", "☑", "\x01", "\xc3", "A café → notes…" }) |sample| {
104
+ try testing.expectEqual(text_metrics.estimateTextWidthForFace(face, sample, 13.25), text_metrics.estimateTextWidthForFont(font_id, sample, 13.25));
105
+ }
106
+ try testing.expectEqual(text_metrics.estimateTextWidthForFont(font_id, "…", 13.25), text_metrics.estimatedTextEllipsisAdvance(font_id, 13.25));
107
+ }
85
108
  }
86
109
 
87
110
  // ---- CoreText agreement (macOS) --------------------------------------
@@ -129,7 +152,10 @@ extern const kCFTypeDictionaryValueCallBacks: [6]usize;
129
152
  const cf_string_encoding_utf8: u32 = 0x0800_0100;
130
153
 
131
154
  fn bundledCoreTextFont(size: f64) !?*anyopaque {
132
- const bytes = font_ttf.geist_regular_bytes;
155
+ return coreTextFontFromBytes(font_ttf.geist_regular_bytes, size);
156
+ }
157
+
158
+ fn coreTextFontFromBytes(bytes: []const u8, size: f64) !?*anyopaque {
133
159
  const data = CFDataCreate(null, bytes.ptr, @intCast(bytes.len)) orelse return error.SkipZigTest;
134
160
  defer CFRelease(data);
135
161
  const descriptor = CTFontManagerCreateFontDescriptorFromData(data) orelse return error.SkipZigTest;
@@ -137,6 +163,25 @@ fn bundledCoreTextFont(size: f64) !?*anyopaque {
137
163
  return CTFontCreateWithFontDescriptor(descriptor, size, null) orelse error.SkipZigTest;
138
164
  }
139
165
 
166
+ test "bundled sans weight advances agree with CoreText" {
167
+ if (comptime builtin.os.tag != .macos) return error.SkipZigTest;
168
+ const cases = .{
169
+ .{ canvas.default_sans_medium_font_id, font_ttf.geist_medium_bytes },
170
+ .{ canvas.default_sans_bold_font_id, font_ttf.geist_bold_bytes },
171
+ };
172
+ inline for (cases) |case| {
173
+ const font = try coreTextFontFromBytes(case[1], 1000);
174
+ defer CFRelease(font);
175
+ var unit: u16 = 0x20;
176
+ while (unit < 0x7f) : (unit += 1) {
177
+ const byte = [_]u8{@intCast(unit)};
178
+ const estimated: f64 = @floatCast(estimatorAdvanceEm(case[0], &byte));
179
+ const shaped = try coreTextGlyphAdvanceEm(font, 1000, unit);
180
+ try testing.expect(@abs(estimated - shaped) <= 0.001);
181
+ }
182
+ }
183
+ }
184
+
140
185
  fn coreTextGlyphAdvanceEm(font: ?*anyopaque, size: f64, unit: u16) !f64 {
141
186
  var characters = [_]u16{unit};
142
187
  var glyphs = [_]u16{0};
@@ -6,6 +6,7 @@ const geometry = @import("geometry");
6
6
  const types = @import("text_layout_types.zig");
7
7
  const interaction = @import("text_interaction.zig");
8
8
  const run = @import("text_run_policy.zig");
9
+ const reuse = @import("decision_reuse.zig");
9
10
  pub const Result = struct {
10
11
  bounds: ?geometry.RectF,
11
12
  position: ?interaction.TextCaretPosition,
@@ -84,6 +85,130 @@ pub fn resultValid(request: []const u8, result: []const u8) bool {
84
85
  else => false,
85
86
  };
86
87
  }
88
+ /// Text layout runs the same queries on every frame. A query is a chain of
89
+ /// policy steps, each a pure function of the packet. Between steps the host
90
+ /// changes the packet only by copying the step's result back, by copying line
91
+ /// slots out of it, and by writing the capability answer. So a trace keeps
92
+ /// the initial packet and every step's result and answer. A later query
93
+ /// replays recorded results only while its initial packet and each freshly
94
+ /// computed answer so far match the trace byte for byte: exactly the case in
95
+ /// which the policy would return the same bytes. Capabilities always run.
96
+ /// Traces are per thread and bounded; a query nested inside a capability,
97
+ /// or one whose policy is not registered as pure, bypasses them.
98
+ const trace_slot_count = 1024;
99
+ const max_trace_bytes = 64 * 1024;
100
+ threadlocal var trace_buffers: [trace_slot_count]std.ArrayList(u8) = @splat(.empty);
101
+ threadlocal var trace_owners: [trace_slot_count]?run.Policy = @splat(null);
102
+ threadlocal var trace_active: bool = false;
103
+
104
+ const Trace = struct {
105
+ slot: ?usize = null,
106
+ policy: ?run.Policy = null,
107
+ /// The recorded trace being followed, until the query diverges from it.
108
+ reading: ?[]const u8 = null,
109
+ cursor: usize = 0,
110
+ /// The trace this query writes when it was not served entirely from one.
111
+ recording: ?std.ArrayList(u8) = null,
112
+
113
+ fn begin(policy: run.Policy, packet: []const u8, text_bytes: []const u8) Trace {
114
+ if (trace_active or !reuse.isPure(policy)) return .{};
115
+ trace_active = true;
116
+ const hash = reuse.slotHash(@intFromPtr(policy), packet, 640) ^ reuse.slotHash(0, text_bytes, 64);
117
+ const index: usize = @intCast(hash % trace_slot_count);
118
+ var self: Trace = .{ .slot = index, .policy = policy };
119
+ const stored = trace_buffers[index].items;
120
+ const owned = if (trace_owners[index]) |owner| owner == policy else false;
121
+ if (owned and stored.len >= 4 + packet.len and
122
+ std.mem.readInt(u32, stored[0..4], .little) == packet.len and reuse.bytesEqual(stored[4..][0..packet.len], packet))
123
+ {
124
+ self.reading = stored;
125
+ self.cursor = 4 + packet.len;
126
+ } else {
127
+ self.startRecording(&.{});
128
+ self.appendLength(packet.len);
129
+ self.append(packet);
130
+ }
131
+ return self;
132
+ }
133
+
134
+ fn startRecording(self: *Trace, prefix: []const u8) void {
135
+ var list: std.ArrayList(u8) = .empty;
136
+ list.appendSlice(std.heap.smp_allocator, prefix) catch return;
137
+ self.recording = list;
138
+ }
139
+
140
+ fn append(self: *Trace, bytes: []const u8) void {
141
+ if (self.recording) |*list| {
142
+ if (list.items.len + bytes.len > max_trace_bytes) return self.dropRecording();
143
+ list.appendSlice(std.heap.smp_allocator, bytes) catch self.dropRecording();
144
+ }
145
+ }
146
+
147
+ fn appendLength(self: *Trace, len: usize) void {
148
+ var bytes: [4]u8 = undefined;
149
+ std.mem.writeInt(u32, &bytes, std.math.cast(u32, len) orelse return self.dropRecording(), .little);
150
+ self.append(&bytes);
151
+ }
152
+
153
+ fn dropRecording(self: *Trace) void {
154
+ if (self.recording) |*list| list.deinit(std.heap.smp_allocator);
155
+ self.recording = null;
156
+ }
157
+
158
+ /// Copy the next recorded result, when the query is still on its trace.
159
+ fn recordedResult(self: *Trace, result: *[1024]u8) bool {
160
+ const stored = self.reading orelse return false;
161
+ if (self.cursor + result.len > stored.len) {
162
+ self.diverge();
163
+ return false;
164
+ }
165
+ @memcpy(result, stored[self.cursor..][0..result.len]);
166
+ self.cursor += result.len;
167
+ return true;
168
+ }
169
+
170
+ fn recordResult(self: *Trace, result: *const [1024]u8) void {
171
+ if (self.reading == null) self.append(result);
172
+ }
173
+
174
+ /// The host's answer for this step (empty for steps without one).
175
+ fn answer(self: *Trace, head: []const u8, tail: []const u8) void {
176
+ const len = head.len + tail.len;
177
+ if (self.reading) |stored| {
178
+ if (self.cursor + 4 + len <= stored.len and std.mem.readInt(u32, stored[self.cursor..][0..4], .little) == len and
179
+ reuse.bytesEqual(stored[self.cursor + 4 ..][0..head.len], head) and
180
+ reuse.bytesEqual(stored[self.cursor + 4 + head.len ..][0..tail.len], tail))
181
+ {
182
+ self.cursor += 4 + len;
183
+ return;
184
+ }
185
+ self.diverge();
186
+ }
187
+ self.appendLength(len);
188
+ self.append(head);
189
+ self.append(tail);
190
+ }
191
+
192
+ /// Leave the trace: everything consumed so far becomes the new trace's
193
+ /// prefix, and later steps run the policy.
194
+ fn diverge(self: *Trace) void {
195
+ const stored = self.reading orelse return;
196
+ self.reading = null;
197
+ self.startRecording(stored[0..self.cursor]);
198
+ }
199
+
200
+ fn end(self: *Trace) void {
201
+ const index = self.slot orelse return;
202
+ trace_active = false;
203
+ if (self.recording) |list| {
204
+ trace_buffers[index].deinit(std.heap.smp_allocator);
205
+ trace_buffers[index] = list;
206
+ trace_owners[index] = self.policy;
207
+ self.recording = null;
208
+ }
209
+ }
210
+ };
211
+
87
212
  pub fn execute(policy: run.Policy, text: types.DrawText, options: types.TextLayoutOptions, query: Query) Result {
88
213
  const glyph_bytes = std.math.mul(usize, text.glyphs.len, 32) catch @panic("text query glyph capacity");
89
214
  const text_bytes = std.math.mul(usize, text.text.len, 5) catch @panic("text query byte capacity");
@@ -125,17 +250,30 @@ fn executeInStorage(policy: run.Policy, text: types.DrawText, options: types.Tex
125
250
  var result: [1024]u8 = undefined;
126
251
  var calls: usize = 0;
127
252
  const quota = std.math.mul(usize, total, 32) catch @panic("text query continuation range");
253
+ var trace = Trace.begin(policy, packet, text.text);
254
+ defer trace.end();
128
255
  while (true) {
129
256
  calls += 1;
130
257
  if (calls > quota) @panic("text query did not finish");
131
- @memset(&result, 0xa5);
132
- if (policy(packet, &result) != result.len or !resultValid(packet, &result)) @panic("invalid text query continuation");
258
+ if (!trace.recordedResult(&result)) {
259
+ @memset(&result, 0xa5);
260
+ if (policy(packet, &result) != result.len or !resultValid(packet, &result)) @panic("invalid text query continuation");
261
+ }
262
+ trace.recordResult(&result);
133
263
  if (result[3] == 2) break;
134
264
  const action = word(&result, 80);
135
265
  const slot = word(&result, 84);
136
266
  @memcpy(packet[0..1024], &result);
137
267
  switch (action) {
138
- 1 => run.supplyCapability(packet, packet[512..1024], 1024, text, options),
268
+ 1 => {
269
+ run.supplyCapability(packet, packet[512..1024], 1024, text, options);
270
+ // The capability answer is the only host input to the next
271
+ // step: the header's answer words, plus the advance facts and
272
+ // everything after them for run-advance answers.
273
+ const kind = word(packet, 512 + 100);
274
+ const advance_at = 1024 + text.glyphs.len * 32;
275
+ trace.answer(packet[512 + 112 .. 512 + 140], if (kind == 3 or kind == 4) packet[advance_at..total] else &.{});
276
+ },
139
277
  2 => {
140
278
  @memcpy(line_header[320..376], packet[128..184]);
141
279
  query.lines[slot] = run.readLine(&line_header);
@@ -144,6 +282,7 @@ fn executeInStorage(policy: run.Policy, text: types.DrawText, options: types.Tex
144
282
  4 => query.selections[slot] = .{ .range = .{ .start = word(packet, 320), .end = word(packet, 324) }, .rect = readRect(packet, 328) },
145
283
  else => unreachable,
146
284
  }
285
+ if (action != 1) trace.answer(&.{}, &.{});
147
286
  put(packet, 92, 1);
148
287
  }
149
288
  const present = word(&result, 272) != 0;
@@ -24,7 +24,7 @@ fn initialize(bytes: []u8, mode: u8, available: bool, font: u64, size: f32, leng
24
24
  put(bytes, 12, if (mode == 1 or mode == 2) length else 0);
25
25
  std.mem.writeInt(u64, bytes[16..24], font, .little);
26
26
  setFloat(bytes, 24, size);
27
- const face = &font_ttf.geist_regular;
27
+ const face = font_ttf.bundledFaceForFontId(font);
28
28
  setFloat(bytes, 28, face.advance(0) / face.units_per_em);
29
29
  }
30
30
  /// Validate every range and codepoint before invoking a font capability.
@@ -57,7 +57,45 @@ fn run(policy: Policy, request: []const u8, result: []u8) void {
57
57
  @memset(result, 0xa5);
58
58
  if (policy(request, result) != result.len or !resultValid(request, result)) @panic("invalid scalar text decision");
59
59
  }
60
+ /// Text layout asks for the same glyph and cluster advances on every frame.
61
+ /// An estimate depends only on the policy, the font, the size, the cluster
62
+ /// flag and the bytes (bundled font tables are static), so short probes keep
63
+ /// their exact result per thread instead of re-entering a policy registered as
64
+ /// pure (`decision_reuse.registerPurePolicy`). A slot
65
+ /// is reused only when every input matches; a collision just replaces it.
66
+ const EstimateEntry = struct {
67
+ policy: ?Policy = null,
68
+ font: u64 = 0,
69
+ size: u32 = 0,
70
+ cluster: bool = false,
71
+ len: u8 = 0,
72
+ bytes: [estimate_key_bytes]u8 = undefined,
73
+ advance: f32 = 0,
74
+ };
75
+ const estimate_key_bytes = 32;
76
+ const estimate_slots = 1024;
77
+ threadlocal var estimates: [estimate_slots]EstimateEntry = @splat(.{});
78
+
79
+ fn estimateSlot(policy: Policy, font: u64, text: []const u8, size: u32, cluster: bool) *EstimateEntry {
80
+ var hash: u64 = 0xcbf29ce484222325 ^ font ^ (@as(u64, size) << 1) ^ @intFromBool(cluster) ^ @intFromPtr(policy);
81
+ for (text) |byte| hash = (hash ^ byte) *% 0x100000001b3;
82
+ hash ^= hash >> 29;
83
+ return &estimates[@intCast(hash % estimate_slots)];
84
+ }
85
+
60
86
  pub fn estimate(policy: Policy, font: u64, text: []const u8, size: f32, cluster: bool) f32 {
87
+ if (text.len > estimate_key_bytes or !@import("decision_reuse.zig").isPure(policy)) return estimateUncached(policy, font, text, size, cluster);
88
+ const size_bits: u32 = @bitCast(size);
89
+ const slot = estimateSlot(policy, font, text, size_bits, cluster);
90
+ if (slot.policy == policy and slot.font == font and slot.size == size_bits and slot.cluster == cluster and
91
+ slot.len == text.len and std.mem.eql(u8, slot.bytes[0..text.len], text)) return slot.advance;
92
+ const advance = estimateUncached(policy, font, text, size, cluster);
93
+ slot.* = .{ .policy = policy, .font = font, .size = size_bits, .cluster = cluster, .len = @intCast(text.len), .advance = advance };
94
+ @memcpy(slot.bytes[0..text.len], text);
95
+ return advance;
96
+ }
97
+
98
+ fn estimateUncached(policy: Policy, font: u64, text: []const u8, size: f32, cluster: bool) f32 {
61
99
  const total = std.math.add(usize, 64, std.math.mul(usize, text.len, 9) catch @panic("scalar text capacity")) catch @panic("scalar text capacity");
62
100
  if (total > std.math.maxInt(u32)) @panic("scalar text packet range");
63
101
  // Glyph and cluster probes should not initialize a whole run's scratch
@@ -88,7 +126,7 @@ fn estimateInStorage(policy: Policy, font: u64, text: []const u8, size: f32, clu
88
126
  for (0..word(packet, 36)) |i| {
89
127
  const cp = word(packet, 64 + i * 8);
90
128
  if (cp == 0xffffffff) continue;
91
- if (metrics.bundledGlyphAdvanceEm(@intCast(cp))) |advance| setFloat(packet, 64 + i * 8 + 4, advance);
129
+ if (metrics.bundledGlyphAdvanceEmForFont(font, @intCast(cp))) |advance| setFloat(packet, 64 + i * 8 + 4, advance);
92
130
  }
93
131
  run(policy, packet, result);
94
132
  if (result[3] != 2) @panic("scalar text did not finish");
@@ -154,3 +192,43 @@ test "scalar text copied transport rejects oversized font facts and changed immu
154
192
  result[68] ^= 1;
155
193
  try std.testing.expect(!resultValid(&request, &result));
156
194
  }
195
+
196
+ var estimate_test_calls: usize = 0;
197
+ fn estimateTestPolicy(request: []const u8, result: []u8) usize {
198
+ estimate_test_calls += 1;
199
+ @memcpy(result[0..request.len], request);
200
+ const slots: usize = word(request, 12);
201
+ @memset(result[32..60], 0);
202
+ @memset(result[64 .. 64 + slots * 8], 0);
203
+ result[3] = 2;
204
+ // A deterministic stand-in decision over every input the cache keys on.
205
+ var advance = float(request, 24) * @as(f32, @floatFromInt(request[2]));
206
+ for (request[64 + slots * 8 ..]) |byte| advance += @floatFromInt(byte);
207
+ advance += @floatFromInt(std.mem.readInt(u64, request[16..24], .little));
208
+ setFloat(result, 32, advance);
209
+ return request.len;
210
+ }
211
+
212
+ test "scalar estimates reuse exact results only for identical inputs" {
213
+ estimate_test_calls = 0;
214
+ _ = estimate(estimateTestPolicy, 7, "ab", 12, true);
215
+ _ = estimate(estimateTestPolicy, 7, "ab", 12, true);
216
+ try std.testing.expectEqual(@as(usize, 2), estimate_test_calls);
217
+ @import("decision_reuse.zig").registerPurePolicy(estimateTestPolicy);
218
+ estimate_test_calls = 0;
219
+ const first = estimate(estimateTestPolicy, 7, "ab", 12, true);
220
+ try std.testing.expectEqual(first, estimate(estimateTestPolicy, 7, "ab", 12, true));
221
+ try std.testing.expectEqual(@as(usize, 1), estimate_test_calls);
222
+ // Every keyed input selects a different decision.
223
+ try std.testing.expect(estimate(estimateTestPolicy, 8, "ab", 12, true) != first);
224
+ try std.testing.expect(estimate(estimateTestPolicy, 7, "ac", 12, true) != first);
225
+ try std.testing.expect(estimate(estimateTestPolicy, 7, "ab", 13, true) != first);
226
+ try std.testing.expect(estimate(estimateTestPolicy, 7, "ab", 12, false) != first);
227
+ try std.testing.expect(estimate(estimateTestPolicy, 7, "a", 12, true) != first);
228
+ try std.testing.expectEqual(@as(usize, 6), estimate_test_calls);
229
+ // Probes longer than the key always run the policy.
230
+ const long = "a long run that does not fit the cached probe key";
231
+ _ = estimate(estimateTestPolicy, 7, long, 12, false);
232
+ _ = estimate(estimateTestPolicy, 7, long, 12, false);
233
+ try std.testing.expectEqual(@as(usize, 8), estimate_test_calls);
234
+ }
@@ -1930,6 +1930,15 @@ pub const DesignTokens = struct {
1930
1930
  widget_routing_policy: ?*const fn ([]const u8, []u8) usize = null,
1931
1931
  /// Portable retained widget matching and change classification.
1932
1932
  widget_change_policy: ?*const fn ([]const u8, []u8) usize = null,
1933
+ /// Portable rebuild collections and edge-triggered autofocus.
1934
+ widget_retention_policy: ?*const fn ([]const u8, []u8) usize = null,
1935
+ widget_reconcile_policy: ?*const fn ([]const u8, []u8) usize = null,
1936
+ /// Portable retained scroll extents, moving subtrees and selected-tab reveals.
1937
+ scroll_tree_policy: ?*const fn ([]const u8, []u8) usize = null,
1938
+ /// Portable scroll mutation, retained state and publication sequencing.
1939
+ scroll_mutation_policy: ?*const fn ([]const u8, []u8) usize = null,
1940
+ widget_continuity_policy: ?*const fn ([]const u8, []u8) usize = null,
1941
+ staged_rebuild_policy: ?*const fn ([]const u8, []u8) usize = null,
1933
1942
  /// Portable retained damage geometry; native supplies font measurements.
1934
1943
  widget_paint_policy: ?*const fn ([]const u8, []u8) usize = null,
1935
1944
  widget_presentation_policy: ?*const fn ([]const u8, []u8) usize = null,
@@ -440,6 +440,7 @@ pub fn Ui(comptime Msg: type) type {
440
440
  chart_content_policy: ?canvas.ChartContentPolicy.Policy = null,
441
441
  markdown_content_policy: ?canvas.MarkdownContentPolicy.Policy = null,
442
442
  construction_policy: ?construction.Policy = null,
443
+ expression_policy: ?@import("ui_markup_expr.zig").Policy = null,
443
444
  /// Window source for `virtualWindow` (see `VirtualWindowSourceFn`):
444
445
  /// null outside an app loop, where builds fall back to each
445
446
  /// request's `viewport_fallback` at offset 0.