@native-sdk/cli 0.0.0 → 0.4.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 (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,1010 @@
1
+ //! Inline styled text runs ("spans") within one wrapped paragraph.
2
+ //!
3
+ //! A paragraph is one logical text block whose bytes are split into up to
4
+ //! `max_text_spans_per_paragraph` spans, each carrying its own weight,
5
+ //! slant, font family (mono), color token, decorations, and optional link
6
+ //! payload. Layout is span-aware: line breaking measures every piece with
7
+ //! the font the piece will draw with (the injected `TextMeasureProvider`
8
+ //! when present, the deterministic estimator otherwise) and composes lines
9
+ //! across span boundaries.
10
+ //!
11
+ //! The layout output is a flat, capacity-bounded run list: each run is a
12
+ //! contiguous slice of one span placed on one line. Renderers draw one
13
+ //! single-line text command per run, so the entire existing text pipeline
14
+ //! (atlas, caching, GPU packet, reference renderer, platform rasterizers)
15
+ //! is reused unchanged. Weight and slant map onto the reserved sans font
16
+ //! id variants (`default_sans_bold_font_id`, ...); hosts that have not
17
+ //! mapped those ids yet fall back to the regular face, and because the
18
+ //! measurement seam carries the same font id, what is measured always
19
+ //! matches what is drawn.
20
+ //!
21
+ //! Everything here is allocation-free and deterministic. Capacity overflow
22
+ //! never fails: layout truncates (dropping trailing runs) and reports it
23
+ //! via `TextSpanLayout.truncated`.
24
+
25
+ const std = @import("std");
26
+ const geometry = @import("geometry");
27
+ const canvas = @import("root.zig");
28
+ const token_model = @import("tokens.zig");
29
+ const text_measure_cache = @import("text_measure_cache.zig");
30
+ const text_metrics = @import("text_metrics.zig");
31
+ const text_interaction = @import("text_interaction.zig");
32
+
33
+ const FontId = canvas.FontId;
34
+ const Color = @import("drawing.zig").Color;
35
+ const TextWrap = @import("text_layout_types.zig").TextWrap;
36
+ const TextAlign = @import("text_layout_types.zig").TextAlign;
37
+
38
+ /// Capacity conventions (documented in `src/runtime/canvas_limits.zig`
39
+ /// style): a paragraph carries at most this many spans; layout emits at
40
+ /// most `max_text_span_runs_per_paragraph` runs across at most
41
+ /// `max_text_span_lines_per_paragraph` lines. Overflow truncates
42
+ /// deterministically instead of failing.
43
+ pub const max_text_spans_per_paragraph: usize = 32;
44
+ pub const max_text_span_runs_per_paragraph: usize = 128;
45
+ pub const max_text_span_lines_per_paragraph: usize = 64;
46
+
47
+ pub const TextSpanWeight = enum {
48
+ regular,
49
+ medium,
50
+ bold,
51
+ };
52
+
53
+ /// A color design token referenced by name — the same namespace style
54
+ /// attributes use (`canvas.ColorTokenName`), so themed apps re-resolve
55
+ /// span colors on retheme without storing raw color values in the view.
56
+ pub const TextSpanColor = std.meta.FieldEnum(token_model.ColorTokens);
57
+
58
+ /// One styled run of paragraph text. `text` is the span's byte slice;
59
+ /// builders that assemble a paragraph keep every span's `text` a subslice
60
+ /// of the paragraph's concatenated plain text so retained-state copies can
61
+ /// rebase instead of duplicating bytes.
62
+ pub const TextSpan = struct {
63
+ text: []const u8 = "",
64
+ weight: TextSpanWeight = .regular,
65
+ italic: bool = false,
66
+ monospace: bool = false,
67
+ /// Foreground override as a design-token reference. Null inherits the
68
+ /// paragraph foreground. Link spans with no explicit color render with
69
+ /// the accent color.
70
+ color: ?TextSpanColor = null,
71
+ /// Background highlight as a design-token reference. Null draws no
72
+ /// background. Renderers fill the run's full line-box rect (the same
73
+ /// geometry selection highlights use) behind the glyphs, so adjacent
74
+ /// runs of the same background abut without seams. Purely visual:
75
+ /// backgrounds never affect measurement or layout (#86, intra-line
76
+ /// diff emphasis).
77
+ background: ?TextSpanColor = null,
78
+ underline: bool = false,
79
+ strikethrough: bool = false,
80
+ /// Relative size multiplier against the paragraph base size; 0 means
81
+ /// inherit (1.0). Headings are spans with `scale` > 1 so their pixel
82
+ /// size stays derived from live typography tokens.
83
+ scale: f32 = 0,
84
+ /// Link payload (URL or app-defined id). Empty means no link. Link
85
+ /// spans are hit-testable through a paragraph link child widget and
86
+ /// carry `role = link` semantics.
87
+ link: []const u8 = "",
88
+ };
89
+
90
+ pub const TextSpanLayoutOptions = struct {
91
+ /// Paragraph base font size; span `scale` multiplies it.
92
+ size: f32,
93
+ /// 0 derives `size * max_scale * 1.25`, matching the single-style text
94
+ /// widget convention.
95
+ line_height: f32 = 0,
96
+ /// 0 (or non-finite) disables wrapping.
97
+ max_width: f32 = 0,
98
+ wrap: TextWrap = .word,
99
+ alignment: TextAlign = .start,
100
+ typography: token_model.TypographyTokens = .{},
101
+ /// Injected measurement; null falls back to the deterministic
102
+ /// estimator (golden-stable).
103
+ measure: ?*const text_metrics.TextMeasureProvider = null,
104
+ };
105
+
106
+ /// One laid-out segment: a contiguous slice of one span on one line.
107
+ pub const TextSpanRun = struct {
108
+ span_index: usize = 0,
109
+ text: []const u8 = "",
110
+ line_index: usize = 0,
111
+ /// Position relative to the paragraph origin (alignment applied).
112
+ x: f32 = 0,
113
+ width: f32 = 0,
114
+ /// Baseline relative to the paragraph top.
115
+ baseline: f32 = 0,
116
+ /// Resolved font size for this run (`size * span scale`).
117
+ size: f32 = 0,
118
+ font_id: FontId = 0,
119
+ };
120
+
121
+ pub const TextSpanLayout = struct {
122
+ runs: []const TextSpanRun = &.{},
123
+ line_count: usize = 0,
124
+ line_height: f32 = 0,
125
+ /// Tight paragraph bounds: max line advance x line_count*line_height.
126
+ size: geometry.SizeF = .{},
127
+ /// True when span, run, or line capacity truncated the layout.
128
+ truncated: bool = false,
129
+ };
130
+
131
+ /// Resolve the font id a span draws (and therefore measures) with. Weight
132
+ /// and slant map onto the reserved sans variant ids only when the app uses
133
+ /// the default sans font; custom fonts keep their id and degrade weight to
134
+ /// the base face. Mono ignores weight/slant (a single mono face ships).
135
+ pub fn textSpanFontId(span: TextSpan, typography: token_model.TypographyTokens) FontId {
136
+ if (span.monospace) return typography.mono_font_id;
137
+ if (typography.font_id != canvas.default_sans_font_id) return typography.font_id;
138
+ return switch (span.weight) {
139
+ .regular => if (span.italic) canvas.default_sans_italic_font_id else canvas.default_sans_font_id,
140
+ .medium => if (span.italic) canvas.default_sans_bold_italic_font_id else canvas.default_sans_medium_font_id,
141
+ .bold => if (span.italic) canvas.default_sans_bold_italic_font_id else canvas.default_sans_bold_font_id,
142
+ };
143
+ }
144
+
145
+ pub fn textSpanColorValue(colors: token_model.ColorTokens, ref: TextSpanColor) Color {
146
+ return switch (ref) {
147
+ inline else => |tag| @field(colors, @tagName(tag)),
148
+ };
149
+ }
150
+
151
+ pub fn textSpanScale(span: TextSpan) f32 {
152
+ if (!std.math.isFinite(span.scale) or span.scale <= 0) return 1;
153
+ return span.scale;
154
+ }
155
+
156
+ fn textSpanSize(span: TextSpan, base_size: f32) f32 {
157
+ return base_size * textSpanScale(span);
158
+ }
159
+
160
+ /// The paragraph-wide scale: the largest span scale, so one uniform line
161
+ /// height fits every run (mixed-scale paragraphs are top-aligned to it).
162
+ pub fn textSpansMaxScale(spans: []const TextSpan) f32 {
163
+ var max_scale: f32 = 1;
164
+ for (spans) |span| max_scale = @max(max_scale, textSpanScale(span));
165
+ return max_scale;
166
+ }
167
+
168
+ pub fn textSpanLineHeight(spans: []const TextSpan, options: TextSpanLayoutOptions) f32 {
169
+ if (options.line_height > 0) return options.line_height;
170
+ return options.size * textSpansMaxScale(spans) * 1.25;
171
+ }
172
+
173
+ /// Single-line (unwrapped) advance of the whole paragraph: the intrinsic
174
+ /// width seam for widget sizing. Measures per-span with the span's font.
175
+ pub fn textSpansIntrinsicWidth(spans: []const TextSpan, options: TextSpanLayoutOptions) f32 {
176
+ var width: f32 = 0;
177
+ for (spans, 0..) |span, index| {
178
+ if (index >= max_text_spans_per_paragraph) break;
179
+ var start: usize = 0;
180
+ var cursor: usize = 0;
181
+ while (cursor < span.text.len) {
182
+ if (span.text[cursor] == '\n') {
183
+ width += measureSpanSlice(span, span.text[start..cursor], options);
184
+ start = cursor + 1;
185
+ }
186
+ cursor += 1;
187
+ }
188
+ width += measureSpanSlice(span, span.text[start..], options);
189
+ }
190
+ return width;
191
+ }
192
+
193
+ /// Wrapped paragraph height at `max_width`: the vertical-extent seam the
194
+ /// widget layout uses so stacked paragraphs reserve their real height.
195
+ pub fn textSpansWrappedHeight(spans: []const TextSpan, options: TextSpanLayoutOptions) f32 {
196
+ var runs: [max_text_span_runs_per_paragraph]TextSpanRun = undefined;
197
+ const layout = layoutTextSpans(spans, options, &runs);
198
+ return layout.size.height;
199
+ }
200
+
201
+ fn measureSpanSlice(span: TextSpan, slice: []const u8, options: TextSpanLayoutOptions) f32 {
202
+ if (slice.len == 0) return 0;
203
+ const font_id = textSpanFontId(span, options.typography);
204
+ const size = textSpanSize(span, options.size);
205
+ // Batched provider path: every slice the span breaker measures is a
206
+ // subslice of `span.text`, so one batched fetch per span (cached
207
+ // across slices, lines, and rebuilds) answers all of them as advance
208
+ // sums — words, held-back whitespace, cluster-wrap prefixes, and
209
+ // selection prefixes stop costing one provider round-trip each. The
210
+ // byte-order sum equals the per-cluster accumulation exactly
211
+ // (continuation zeros are f32 identities), so any provider whose
212
+ // slice widths are its advance sums breaks lines byte-identically
213
+ // to the unbatched seam (pinned by the span parity tests).
214
+ if (spanSliceAdvances(span, slice, options, font_id, size)) |advances| {
215
+ var width: f32 = 0;
216
+ for (advances) |advance| width += advance;
217
+ return width;
218
+ }
219
+ return text_metrics.measureTextWidthForFont(options.measure, font_id, slice, size);
220
+ }
221
+
222
+ /// The batched advances of `slice` within its span, or null when the
223
+ /// seam is unbatched (no provider, no batched entry, host declined) or
224
+ /// `slice` does not alias `span.text` (hand-built spans). The slice
225
+ /// aliases threadlocal cache storage: consumed immediately by the one
226
+ /// caller above.
227
+ fn spanSliceAdvances(span: TextSpan, slice: []const u8, options: TextSpanLayoutOptions, font_id: FontId, size: f32) ?[]const f32 {
228
+ const provider = options.measure orelse return null;
229
+ if (provider.measure_advances_fn == null) return null;
230
+ const base = @intFromPtr(span.text.ptr);
231
+ const start = @intFromPtr(slice.ptr);
232
+ if (start < base) return null;
233
+ const offset = start - base;
234
+ if (offset + slice.len > span.text.len) return null;
235
+ const advances = text_measure_cache.textRunAdvances(provider, font_id, size, span.text) orelse return null;
236
+ return advances[offset..][0..slice.len];
237
+ }
238
+
239
+ const LayoutState = struct {
240
+ spans: []const TextSpan,
241
+ options: TextSpanLayoutOptions,
242
+ runs: []TextSpanRun,
243
+ run_len: usize = 0,
244
+ line_index: usize = 0,
245
+ line_run_start: usize = 0,
246
+ pen_x: f32 = 0,
247
+ max_line_width: f32 = 0,
248
+ line_has_content: bool = false,
249
+ line_height: f32 = 0,
250
+ baseline_offset: f32 = 0,
251
+ max_width: f32 = std.math.inf(f32),
252
+ truncated: bool = false,
253
+ /// Inter-word whitespace is held back until the next word lands on the
254
+ /// same line, so line breaks never leave trailing spaces in runs (and
255
+ /// alignment math stays exact).
256
+ pending_span: usize = 0,
257
+ pending_slice: []const u8 = "",
258
+ pending_width: f32 = 0,
259
+
260
+ fn baseline(self: *const LayoutState) f32 {
261
+ return self.baseline_offset + @as(f32, @floatFromInt(self.line_index)) * self.line_height;
262
+ }
263
+
264
+ fn recordPendingWhitespace(self: *LayoutState, span_index: usize, slice: []const u8, width: f32) void {
265
+ self.flushPendingWhitespace();
266
+ self.pending_span = span_index;
267
+ self.pending_slice = slice;
268
+ self.pending_width = width;
269
+ }
270
+
271
+ fn flushPendingWhitespace(self: *LayoutState) void {
272
+ if (self.pending_slice.len == 0) return;
273
+ const slice = self.pending_slice;
274
+ const width = self.pending_width;
275
+ const span_index = self.pending_span;
276
+ self.dropPendingWhitespace();
277
+ self.place(span_index, slice, width);
278
+ }
279
+
280
+ fn dropPendingWhitespace(self: *LayoutState) void {
281
+ self.pending_slice = "";
282
+ self.pending_width = 0;
283
+ }
284
+
285
+ /// Append `slice` of span `span_index` at the pen. Merges into the
286
+ /// previous run when it continues the same span on the same line.
287
+ fn place(self: *LayoutState, span_index: usize, slice: []const u8, width: f32) void {
288
+ if (slice.len == 0) return;
289
+ defer {
290
+ self.pen_x += width;
291
+ self.max_line_width = @max(self.max_line_width, self.pen_x);
292
+ self.line_has_content = true;
293
+ }
294
+ if (self.run_len > self.line_run_start) {
295
+ const previous = &self.runs[self.run_len - 1];
296
+ if (previous.span_index == span_index and
297
+ previous.line_index == self.line_index and
298
+ previous.text.ptr + previous.text.len == slice.ptr)
299
+ {
300
+ previous.text = previous.text.ptr[0 .. previous.text.len + slice.len];
301
+ previous.width += width;
302
+ return;
303
+ }
304
+ }
305
+ if (self.run_len >= self.runs.len or self.line_index >= max_text_span_lines_per_paragraph) {
306
+ self.truncated = true;
307
+ return;
308
+ }
309
+ const span = self.spans[span_index];
310
+ self.runs[self.run_len] = .{
311
+ .span_index = span_index,
312
+ .text = slice,
313
+ .line_index = self.line_index,
314
+ .x = self.pen_x,
315
+ .width = width,
316
+ .baseline = self.baseline(),
317
+ .size = textSpanSize(span, self.options.size),
318
+ .font_id = textSpanFontId(span, self.options.typography),
319
+ };
320
+ self.run_len += 1;
321
+ }
322
+
323
+ fn breakLine(self: *LayoutState) void {
324
+ self.dropPendingWhitespace();
325
+ self.alignLine();
326
+ self.line_run_start = self.run_len;
327
+ self.line_index += 1;
328
+ self.pen_x = 0;
329
+ self.line_has_content = false;
330
+ }
331
+
332
+ fn alignLine(self: *LayoutState) void {
333
+ if (self.options.alignment == .start) return;
334
+ if (!std.math.isFinite(self.max_width)) return;
335
+ const extra = self.max_width - self.pen_x;
336
+ if (extra <= 0) return;
337
+ const dx = switch (self.options.alignment) {
338
+ .start => 0,
339
+ .center => extra * 0.5,
340
+ .end => extra,
341
+ };
342
+ for (self.runs[self.line_run_start..self.run_len]) |*run| run.x += dx;
343
+ }
344
+ };
345
+
346
+ /// Lay the paragraph out into `runs_storage`. Never fails: capacity
347
+ /// overflow truncates trailing content and sets `truncated`.
348
+ ///
349
+ /// Provider-measured paragraphs ride the retained wrap cache below:
350
+ /// steady-state rebuilds of an unchanged paragraph at an unchanged
351
+ /// width rebase the cached runs onto the caller's storage without
352
+ /// measuring anything. Estimator paragraphs (measure == null) go
353
+ /// straight to the uncached breaker — that path stays byte-identical
354
+ /// to the pre-cache behavior (goldens, signatures, reference renders).
355
+ pub fn layoutTextSpans(spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) TextSpanLayout {
356
+ if (options.measure != null and runs_storage.len >= max_text_span_runs_per_paragraph) {
357
+ const key = spanWrapKey(spans, options);
358
+ if (findSpanWrapEntry(key)) |entry_index| {
359
+ if (rebaseSpanWrapEntry(entry_index, spans, options, runs_storage)) |layout| return layout;
360
+ }
361
+ const layout = layoutTextSpansUncached(spans, options, runs_storage);
362
+ storeSpanWrapEntry(key, spans, layout);
363
+ return layout;
364
+ }
365
+ return layoutTextSpansUncached(spans, options, runs_storage);
366
+ }
367
+
368
+ fn layoutTextSpansUncached(spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) TextSpanLayout {
369
+ var state = LayoutState{
370
+ .spans = spans,
371
+ .options = options,
372
+ .runs = runs_storage,
373
+ .line_height = textSpanLineHeight(spans, options),
374
+ .baseline_offset = options.size * textSpansMaxScale(spans),
375
+ .max_width = if (options.wrap != .none and options.max_width > 0 and std.math.isFinite(options.max_width))
376
+ options.max_width
377
+ else
378
+ std.math.inf(f32),
379
+ };
380
+ if (spans.len > max_text_spans_per_paragraph) state.truncated = true;
381
+ const span_count = @min(spans.len, max_text_spans_per_paragraph);
382
+
383
+ var span_index: usize = 0;
384
+ var offset: usize = 0;
385
+ while (span_index < span_count) {
386
+ const text = spans[span_index].text;
387
+ if (offset >= text.len) {
388
+ span_index += 1;
389
+ offset = 0;
390
+ continue;
391
+ }
392
+ const byte = text[offset];
393
+ if (byte == '\n') {
394
+ state.breakLine();
395
+ offset += 1;
396
+ continue;
397
+ }
398
+ if (isSpanBreakByte(byte)) {
399
+ const end = spanWhitespaceEnd(text, offset);
400
+ // Whitespace at a fresh line start is consumed by the wrap;
401
+ // mid-line whitespace is held back until the next word lands.
402
+ if (state.line_has_content) {
403
+ const slice = text[offset..end];
404
+ state.recordPendingWhitespace(span_index, slice, measureSpanSlice(spans[span_index], slice, options));
405
+ }
406
+ offset = end;
407
+ continue;
408
+ }
409
+ placeWord(&state, span_count, &span_index, &offset);
410
+ }
411
+ state.dropPendingWhitespace();
412
+ state.alignLine();
413
+
414
+ const line_count = state.line_index + @intFromBool(state.line_has_content or state.run_len > state.line_run_start or state.line_index == 0);
415
+ return .{
416
+ .runs = state.runs[0..state.run_len],
417
+ .line_count = line_count,
418
+ .line_height = state.line_height,
419
+ .size = geometry.SizeF.init(
420
+ state.max_line_width,
421
+ @as(f32, @floatFromInt(line_count)) * state.line_height,
422
+ ),
423
+ .truncated = state.truncated,
424
+ };
425
+ }
426
+
427
+ // ------------------------------------------------------------------
428
+ // Retained wrap cache (provider-measured paragraphs only).
429
+ //
430
+ // A rebuild re-lays-out every mounted paragraph whether it changed or
431
+ // not: widget layout asks for wrapped heights (often several times per
432
+ // paragraph while heights bubble), rendering lays the runs out again to
433
+ // emit commands, and interaction queries lay out once more. With a live
434
+ // measure provider each of those used to reach the host; with the
435
+ // batched advance cache they still re-break every line. This cache
436
+ // retains the RESULT: keyed on the paragraph's layout-affecting content
437
+ // (text bytes and the style fields that pick a font or size — weight,
438
+ // slant, mono, scale; colors, decorations, and links never feed
439
+ // measurement, so a retheme that only recolors keeps hitting), the base
440
+ // size, the wrap width, wrap and alignment modes, the line height, the
441
+ // typography font ids, the provider identity, and the global measure
442
+ // generation (`bumpTextMeasureGeneration`: font registration, appearance
443
+ // flips, runtime construction — the same invalidation the advance cache
444
+ // obeys).
445
+ //
446
+ // Values store runs in OFFSET form (span index + byte range) rather
447
+ // than byte slices: retained span storage is rewritten every rebuild,
448
+ // so cached pointers would dangle. A hit rebases the offsets onto the
449
+ // caller's current spans, whose bytes hash-match the key.
450
+ //
451
+ // Bounded and threadlocal like the advance cache (and the planner
452
+ // scratch in text_layout_cache.zig): least-recently-used slot eviction,
453
+ // no allocation, one cache per thread on the single-threaded frame
454
+ // path. Only requests with full-capacity run storage participate, so a
455
+ // truncated layout against a smaller caller buffer can never be served
456
+ // to (or captured from) a caller with different capacity.
457
+ // ------------------------------------------------------------------
458
+
459
+ /// Sized past the mounted hot set: a transcript screen's visible
460
+ /// paragraphs times the couple of candidate widths height bubbling asks
461
+ /// for (each wrap width is its own key). Least-recently-used eviction
462
+ /// degrades to a 100% miss rate when the steadily revisited set
463
+ /// outgrows the slots, so capacity errs generously (256 x ~4 KiB of
464
+ /// threadlocal storage).
465
+ pub const span_wrap_cache_capacity: usize = 256;
466
+
467
+ const SpanWrapKey = struct {
468
+ fingerprint: u64 = 0,
469
+ span_count: usize = 0,
470
+ size_bits: u32 = 0,
471
+ line_height_bits: u32 = 0,
472
+ max_width_bits: u32 = 0,
473
+ wrap: TextWrap = .word,
474
+ alignment: TextAlign = .start,
475
+ font_id: FontId = 0,
476
+ mono_font_id: FontId = 0,
477
+ provider_context: usize = 0,
478
+ provider_fn: usize = 0,
479
+ generation: u64 = 0,
480
+ used: bool = false,
481
+ };
482
+
483
+ /// One cached run in offset form; `size` and `font_id` are recomputed
484
+ /// from the (hash-matched) span at rebase time, so they are not stored.
485
+ const SpanWrapRun = struct {
486
+ span_index: u32 = 0,
487
+ text_start: u32 = 0,
488
+ text_len: u32 = 0,
489
+ line_index: u32 = 0,
490
+ x: f32 = 0,
491
+ width: f32 = 0,
492
+ baseline: f32 = 0,
493
+ };
494
+
495
+ const SpanWrapEntry = struct {
496
+ key: SpanWrapKey = .{},
497
+ run_len: usize = 0,
498
+ line_count: usize = 0,
499
+ line_height: f32 = 0,
500
+ size: geometry.SizeF = .{},
501
+ truncated: bool = false,
502
+ last_used: u64 = 0,
503
+ };
504
+
505
+ threadlocal var span_wrap_entries: [span_wrap_cache_capacity]SpanWrapEntry = @splat(.{});
506
+ threadlocal var span_wrap_runs: [span_wrap_cache_capacity][max_text_span_runs_per_paragraph]SpanWrapRun = undefined;
507
+ threadlocal var span_wrap_use_tick: u64 = 0;
508
+ threadlocal var span_wrap_hit_count: u64 = 0;
509
+ threadlocal var span_wrap_miss_count: u64 = 0;
510
+
511
+ /// Cache observability for tests and benchmarks.
512
+ pub fn textSpanWrapCacheHitCount() u64 {
513
+ return span_wrap_hit_count;
514
+ }
515
+
516
+ pub fn textSpanWrapCacheMissCount() u64 {
517
+ return span_wrap_miss_count;
518
+ }
519
+
520
+ fn spanWrapKey(spans: []const TextSpan, options: TextSpanLayoutOptions) SpanWrapKey {
521
+ var hasher = std.hash.Wyhash.init(0x7370616e77726170);
522
+ const span_count = @min(spans.len, max_text_spans_per_paragraph);
523
+ for (spans[0..span_count]) |span| {
524
+ // Length prefix keeps concatenation honest ("ab"+"c" never
525
+ // hashes like "a"+"bc"); only layout-affecting style fields
526
+ // fold in (see the module comment above).
527
+ hasher.update(std.mem.asBytes(&span.text.len));
528
+ hasher.update(span.text);
529
+ hasher.update(&[_]u8{
530
+ @intFromEnum(span.weight),
531
+ @intFromBool(span.italic),
532
+ @intFromBool(span.monospace),
533
+ });
534
+ hasher.update(std.mem.asBytes(&span.scale));
535
+ }
536
+ const provider = options.measure.?;
537
+ return .{
538
+ .fingerprint = hasher.final(),
539
+ .span_count = spans.len,
540
+ .size_bits = @bitCast(options.size),
541
+ .line_height_bits = @bitCast(options.line_height),
542
+ .max_width_bits = @bitCast(options.max_width),
543
+ .wrap = options.wrap,
544
+ .alignment = options.alignment,
545
+ .font_id = options.typography.font_id,
546
+ .mono_font_id = options.typography.mono_font_id,
547
+ .provider_context = @intFromPtr(provider.context),
548
+ .provider_fn = @intFromPtr(provider.measure_fn),
549
+ .generation = text_measure_cache.textMeasureGeneration(),
550
+ .used = true,
551
+ };
552
+ }
553
+
554
+ fn spanWrapKeysEqual(a: SpanWrapKey, b: SpanWrapKey) bool {
555
+ return a.used and b.used and
556
+ a.fingerprint == b.fingerprint and
557
+ a.span_count == b.span_count and
558
+ a.size_bits == b.size_bits and
559
+ a.line_height_bits == b.line_height_bits and
560
+ a.max_width_bits == b.max_width_bits and
561
+ a.wrap == b.wrap and
562
+ a.alignment == b.alignment and
563
+ a.font_id == b.font_id and
564
+ a.mono_font_id == b.mono_font_id and
565
+ a.provider_context == b.provider_context and
566
+ a.provider_fn == b.provider_fn and
567
+ a.generation == b.generation;
568
+ }
569
+
570
+ fn findSpanWrapEntry(key: SpanWrapKey) ?usize {
571
+ for (&span_wrap_entries, 0..) |*entry, index| {
572
+ if (spanWrapKeysEqual(entry.key, key)) {
573
+ span_wrap_use_tick += 1;
574
+ entry.last_used = span_wrap_use_tick;
575
+ span_wrap_hit_count += 1;
576
+ return index;
577
+ }
578
+ }
579
+ span_wrap_miss_count += 1;
580
+ return null;
581
+ }
582
+
583
+ fn storeSpanWrapEntry(key: SpanWrapKey, spans: []const TextSpan, layout: TextSpanLayout) void {
584
+ if (layout.runs.len > max_text_span_runs_per_paragraph) return;
585
+ // Offsets require every run to alias its span's bytes; the breaker
586
+ // only ever emits subslices of span.text, so a failure here would be
587
+ // a bug upstream — decline to cache rather than store a lie.
588
+ for (layout.runs) |run| {
589
+ if (spanRunOffset(spans, run) == null) return;
590
+ }
591
+
592
+ var victim: usize = 0;
593
+ var victim_tick: u64 = std.math.maxInt(u64);
594
+ for (&span_wrap_entries, 0..) |*entry, index| {
595
+ const tick = if (entry.key.used) entry.last_used else 0;
596
+ if (tick < victim_tick) {
597
+ victim_tick = tick;
598
+ victim = index;
599
+ }
600
+ }
601
+ span_wrap_use_tick += 1;
602
+ for (layout.runs, 0..) |run, run_index| {
603
+ const offset = spanRunOffset(spans, run).?;
604
+ span_wrap_runs[victim][run_index] = .{
605
+ .span_index = @intCast(run.span_index),
606
+ .text_start = @intCast(offset),
607
+ .text_len = @intCast(run.text.len),
608
+ .line_index = @intCast(run.line_index),
609
+ .x = run.x,
610
+ .width = run.width,
611
+ .baseline = run.baseline,
612
+ };
613
+ }
614
+ span_wrap_entries[victim] = .{
615
+ .key = key,
616
+ .run_len = layout.runs.len,
617
+ .line_count = layout.line_count,
618
+ .line_height = layout.line_height,
619
+ .size = layout.size,
620
+ .truncated = layout.truncated,
621
+ .last_used = span_wrap_use_tick,
622
+ };
623
+ }
624
+
625
+ fn spanRunOffset(spans: []const TextSpan, run: TextSpanRun) ?usize {
626
+ if (run.span_index >= spans.len) return null;
627
+ const base = @intFromPtr(spans[run.span_index].text.ptr);
628
+ const start = @intFromPtr(run.text.ptr);
629
+ if (start < base) return null;
630
+ const offset = start - base;
631
+ if (offset + run.text.len > spans[run.span_index].text.len) return null;
632
+ return offset;
633
+ }
634
+
635
+ /// Materialize a cached layout onto the caller's storage: run slices
636
+ /// rebase onto the CURRENT spans' bytes (hash-matched to the cached
637
+ /// paragraph), per-run size and font id re-derive from the span and
638
+ /// typography exactly as the breaker derives them. Null when a cached
639
+ /// offset does not fit the current spans — only reachable through a
640
+ /// content-hash collision, and answered by re-laying-out instead of
641
+ /// serving mismatched geometry.
642
+ fn rebaseSpanWrapEntry(entry_index: usize, spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) ?TextSpanLayout {
643
+ const entry = &span_wrap_entries[entry_index];
644
+ for (span_wrap_runs[entry_index][0..entry.run_len]) |cached| {
645
+ if (cached.span_index >= spans.len) return null;
646
+ if (cached.text_start + cached.text_len > spans[cached.span_index].text.len) return null;
647
+ }
648
+ for (span_wrap_runs[entry_index][0..entry.run_len], 0..) |cached, run_index| {
649
+ const span = spans[cached.span_index];
650
+ runs_storage[run_index] = .{
651
+ .span_index = cached.span_index,
652
+ .text = span.text[cached.text_start .. cached.text_start + cached.text_len],
653
+ .line_index = cached.line_index,
654
+ .x = cached.x,
655
+ .width = cached.width,
656
+ .baseline = cached.baseline,
657
+ .size = textSpanSize(span, options.size),
658
+ .font_id = textSpanFontId(span, options.typography),
659
+ };
660
+ }
661
+ return .{
662
+ .runs = runs_storage[0..entry.run_len],
663
+ .line_count = entry.line_count,
664
+ .line_height = entry.line_height,
665
+ .size = entry.size,
666
+ .truncated = entry.truncated,
667
+ };
668
+ }
669
+
670
+ const max_word_pieces = max_text_spans_per_paragraph;
671
+
672
+ const WordPiece = struct {
673
+ span_index: usize,
674
+ start: usize,
675
+ end: usize,
676
+ width: f32,
677
+ };
678
+
679
+ /// Collect the word starting at (span_index, offset) — consecutive
680
+ /// non-break pieces, crossing span boundaries when no whitespace divides
681
+ /// them — measure it, and place it with word wrapping. Words wider than
682
+ /// the wrap width fall back to cluster wrapping.
683
+ fn placeWord(state: *LayoutState, span_count: usize, span_index: *usize, offset: *usize) void {
684
+ var pieces: [max_word_pieces]WordPiece = undefined;
685
+ var piece_len: usize = 0;
686
+ var total_width: f32 = 0;
687
+
688
+ var cursor_span = span_index.*;
689
+ var cursor_offset = offset.*;
690
+ while (cursor_span < span_count and piece_len < pieces.len) {
691
+ const text = state.spans[cursor_span].text;
692
+ if (cursor_offset >= text.len) {
693
+ cursor_span += 1;
694
+ cursor_offset = 0;
695
+ continue;
696
+ }
697
+ if (text[cursor_offset] == '\n' or isSpanBreakByte(text[cursor_offset])) break;
698
+ const end = spanWordEnd(text, cursor_offset);
699
+ const width = measureSpanSlice(state.spans[cursor_span], text[cursor_offset..end], state.options);
700
+ pieces[piece_len] = .{ .span_index = cursor_span, .start = cursor_offset, .end = end, .width = width };
701
+ piece_len += 1;
702
+ total_width += width;
703
+ cursor_offset = end;
704
+ }
705
+
706
+ const should_wrap_word = state.options.wrap == .word and
707
+ state.line_has_content and
708
+ state.pen_x + state.pending_width + total_width > state.max_width + span_break_slack;
709
+ if (should_wrap_word) {
710
+ state.breakLine();
711
+ } else {
712
+ state.flushPendingWhitespace();
713
+ }
714
+
715
+ for (pieces[0..piece_len]) |piece| {
716
+ const slice = state.spans[piece.span_index].text[piece.start..piece.end];
717
+ if (state.pen_x + piece.width > state.max_width + span_break_slack) {
718
+ placeClusterWrapped(state, piece.span_index, slice);
719
+ } else {
720
+ state.place(piece.span_index, slice, piece.width);
721
+ }
722
+ }
723
+
724
+ span_index.* = cursor_span;
725
+ offset.* = cursor_offset;
726
+ }
727
+
728
+ /// Cluster-granularity wrapping for pieces wider than the remaining line
729
+ /// (single oversized words, and `wrap == .character`). Prefix widths are
730
+ /// measured through the same provider seam so kerning is honored.
731
+ fn placeClusterWrapped(state: *LayoutState, span_index: usize, slice: []const u8) void {
732
+ const span = state.spans[span_index];
733
+ var start: usize = 0;
734
+ while (start < slice.len) {
735
+ var end = start;
736
+ var width: f32 = 0;
737
+ while (end < slice.len) {
738
+ const next = text_interaction.nextTextOffset(slice, end);
739
+ const next_width = measureSpanSlice(span, slice[start..next], state.options);
740
+ if (state.pen_x + next_width > state.max_width + span_break_slack) {
741
+ if (end == start and !state.line_has_content) {
742
+ // A single cluster wider than the line still occupies it.
743
+ end = next;
744
+ width = next_width;
745
+ }
746
+ break;
747
+ }
748
+ end = next;
749
+ width = next_width;
750
+ }
751
+ if (end == start) {
752
+ state.breakLine();
753
+ continue;
754
+ }
755
+ state.place(span_index, slice[start..end], width);
756
+ start = end;
757
+ if (start < slice.len) state.breakLine();
758
+ }
759
+ }
760
+
761
+ /// Line-break tolerance absorbing f32 association noise. Intrinsic sizing
762
+ /// measures a paragraph as whole slices while the breaker accumulates
763
+ /// per-word (and per-cluster) measures, and the two sums can differ by an
764
+ /// ulp (~1e-5 px on a realistic line) purely from addition order — enough
765
+ /// to re-wrap a paragraph laid out at exactly its measured intrinsic
766
+ /// width, painting its last word over the content below. 1/64 px is
767
+ /// orders of magnitude above that noise and far below any real glyph
768
+ /// advance, so genuine overflow still breaks exactly as before.
769
+ const span_break_slack: f32 = 0.015625;
770
+
771
+ fn isSpanBreakByte(byte: u8) bool {
772
+ return byte == ' ' or byte == '\t';
773
+ }
774
+
775
+ fn spanWhitespaceEnd(text: []const u8, start: usize) usize {
776
+ var end = start;
777
+ while (end < text.len and isSpanBreakByte(text[end])) end += 1;
778
+ return end;
779
+ }
780
+
781
+ fn spanWordEnd(text: []const u8, start: usize) usize {
782
+ var end = start;
783
+ while (end < text.len and text[end] != '\n' and !isSpanBreakByte(text[end])) end += 1;
784
+ return end;
785
+ }
786
+
787
+ /// Union bounds (relative to the paragraph origin) of every run belonging
788
+ /// to `span_index`. Frames link hit areas; a link that wraps across lines
789
+ /// gets the union rect of its segments (v1 caveat: for wrapped links this
790
+ /// includes the horizontal stretch between line fragments).
791
+ pub fn textSpanBounds(layout: TextSpanLayout, span_index: usize) ?geometry.RectF {
792
+ var bounds: ?geometry.RectF = null;
793
+ for (layout.runs) |run| {
794
+ if (run.span_index != span_index) continue;
795
+ const rect = textSpanRunBounds(layout, run);
796
+ bounds = if (bounds) |existing| existing.unionWith(rect) else rect;
797
+ }
798
+ return bounds;
799
+ }
800
+
801
+ /// Bounds of a single run (relative to the paragraph origin), spanning
802
+ /// the full line box height so hit areas cover the whole line.
803
+ pub fn textSpanRunBounds(layout: TextSpanLayout, run: TextSpanRun) geometry.RectF {
804
+ const top = @as(f32, @floatFromInt(run.line_index)) * layout.line_height;
805
+ return geometry.RectF.init(run.x, top, run.width, layout.line_height);
806
+ }
807
+
808
+ /// Byte range of `run` inside the paragraph's concatenated plain text.
809
+ /// Builders (`ui.paragraph`, markdown) keep every span's bytes a subslice
810
+ /// of the paragraph text, so a run's offsets fall out of pointer
811
+ /// arithmetic; hand-built spans that alias other storage return null and
812
+ /// selection quietly degrades to unsupported for that paragraph.
813
+ pub fn textSpanRunParagraphRange(paragraph: []const u8, run: TextSpanRun) ?text_interaction.TextRange {
814
+ if (run.text.len == 0 or paragraph.len == 0) return null;
815
+ const base = @intFromPtr(paragraph.ptr);
816
+ const run_start = @intFromPtr(run.text.ptr);
817
+ if (run_start < base) return null;
818
+ const start = run_start - base;
819
+ const end = start + run.text.len;
820
+ if (end > paragraph.len) return null;
821
+ return text_interaction.TextRange.init(start, end);
822
+ }
823
+
824
+ /// Paragraph byte offset for a point relative to the paragraph origin.
825
+ /// Clamps vertically to the nearest line and horizontally to that line's
826
+ /// run edges, so drag selection keeps working when the pointer leaves the
827
+ /// text. Null when the paragraph has no runs or its spans do not index
828
+ /// into `paragraph` (see `textSpanRunParagraphRange`).
829
+ pub fn textSpanOffsetForPoint(
830
+ paragraph: []const u8,
831
+ spans: []const TextSpan,
832
+ options: TextSpanLayoutOptions,
833
+ point: geometry.PointF,
834
+ ) ?usize {
835
+ var runs: [max_text_span_runs_per_paragraph]TextSpanRun = undefined;
836
+ const layout = layoutTextSpans(spans, options, &runs);
837
+ if (layout.runs.len == 0 or layout.line_count == 0 or layout.line_height <= 0) return null;
838
+
839
+ const raw_line = point.y / layout.line_height;
840
+ const max_line = layout.line_count - 1;
841
+ const line_index: usize = if (raw_line < 0)
842
+ 0
843
+ else
844
+ @min(max_line, @as(usize, @intFromFloat(@floor(raw_line))));
845
+
846
+ var result: ?usize = null;
847
+ var first_range: ?text_interaction.TextRange = null;
848
+ var last_range: ?text_interaction.TextRange = null;
849
+ var first_x: f32 = 0;
850
+ var last_end_x: f32 = 0;
851
+ for (layout.runs) |run| {
852
+ if (run.line_index != line_index) continue;
853
+ const range = textSpanRunParagraphRange(paragraph, run) orelse return null;
854
+ if (first_range == null) {
855
+ first_range = range;
856
+ first_x = run.x;
857
+ }
858
+ last_range = range;
859
+ last_end_x = run.x + run.width;
860
+ if (point.x >= run.x and point.x < run.x + run.width) {
861
+ result = range.start + spanRunOffsetForX(spans[run.span_index], run, options, point.x - run.x);
862
+ }
863
+ }
864
+ if (result) |offset| return offset;
865
+ const first = first_range orelse return lineFallbackOffset(paragraph, layout, line_index);
866
+ if (point.x < first_x) return first.start;
867
+ if (last_range) |last| {
868
+ if (point.x >= last_end_x) return last.end;
869
+ }
870
+ return first.start;
871
+ }
872
+
873
+ /// Offset within `run.text` for a run-relative x, midpoint rule per
874
+ /// codepoint, mirroring the plain-text `textLineOffsetForX`.
875
+ fn spanRunOffsetForX(span: TextSpan, run: TextSpanRun, options: TextSpanLayoutOptions, x: f32) usize {
876
+ if (x <= 0) return 0;
877
+ var cursor: usize = 0;
878
+ var caret_x: f32 = 0;
879
+ while (cursor < run.text.len) {
880
+ const next_cursor = text_interaction.nextTextOffset(run.text, cursor);
881
+ const next_x = @max(caret_x + 1, measureSpanSlice(span, run.text[0..next_cursor], options));
882
+ if (x < (caret_x + next_x) * 0.5) return cursor;
883
+ caret_x = next_x;
884
+ cursor = next_cursor;
885
+ }
886
+ return run.text.len;
887
+ }
888
+
889
+ /// When a line exists but has no runs (blank line from an explicit "\n"),
890
+ /// selection lands on the nearest following run's start; an entirely
891
+ /// runless paragraph cannot happen here (guarded by the caller).
892
+ fn lineFallbackOffset(paragraph: []const u8, layout: TextSpanLayout, line_index: usize) ?usize {
893
+ for (layout.runs) |run| {
894
+ if (run.line_index < line_index) continue;
895
+ const range = textSpanRunParagraphRange(paragraph, run) orelse return null;
896
+ return range.start;
897
+ }
898
+ for (0..layout.runs.len) |back| {
899
+ const run = layout.runs[layout.runs.len - 1 - back];
900
+ const range = textSpanRunParagraphRange(paragraph, run) orelse return null;
901
+ return range.end;
902
+ }
903
+ return null;
904
+ }
905
+
906
+ /// Selection highlight rects (relative to the paragraph origin) for a
907
+ /// paragraph byte range: one rect per line, spanning the selected extent
908
+ /// across that line's runs. Returns the rects that fit in `output`;
909
+ /// overflow truncates deterministically like span layout itself.
910
+ pub fn textSpanSelectionRects(
911
+ paragraph: []const u8,
912
+ spans: []const TextSpan,
913
+ options: TextSpanLayoutOptions,
914
+ range: text_interaction.TextRange,
915
+ output: []text_interaction.TextSelectionRect,
916
+ ) []const text_interaction.TextSelectionRect {
917
+ const normalized = text_interaction.snapTextRange(paragraph, range);
918
+ if (normalized.isCollapsed(paragraph.len)) return output[0..0];
919
+
920
+ var runs: [max_text_span_runs_per_paragraph]TextSpanRun = undefined;
921
+ const layout = layoutTextSpans(spans, options, &runs);
922
+ if (layout.line_height <= 0) return output[0..0];
923
+
924
+ var len: usize = 0;
925
+ var current_line: ?usize = null;
926
+ var line_left: f32 = 0;
927
+ var line_right: f32 = 0;
928
+ var line_range = text_interaction.TextRange.init(0, 0);
929
+ for (layout.runs) |run| {
930
+ const run_range = textSpanRunParagraphRange(paragraph, run) orelse continue;
931
+ const start = @max(normalized.start, run_range.start);
932
+ const end = @min(normalized.end, run_range.end);
933
+ if (start >= end) continue;
934
+
935
+ const span = spans[run.span_index];
936
+ const x0 = run.x + measureSpanSlice(span, run.text[0 .. start - run_range.start], options);
937
+ const x1 = run.x + measureSpanSlice(span, run.text[0 .. end - run_range.start], options);
938
+ if (current_line) |line| {
939
+ if (line == run.line_index) {
940
+ line_left = @min(line_left, @min(x0, x1));
941
+ line_right = @max(line_right, @max(x0, x1));
942
+ line_range = text_interaction.TextRange.init(@min(line_range.start, start), @max(line_range.end, end));
943
+ continue;
944
+ }
945
+ if (!flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, &len)) return output[0..len];
946
+ }
947
+ current_line = run.line_index;
948
+ line_left = @min(x0, x1);
949
+ line_right = @max(x0, x1);
950
+ line_range = text_interaction.TextRange.init(start, end);
951
+ }
952
+ if (current_line) |line| {
953
+ if (!flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, &len)) return output[0..len];
954
+ }
955
+ return output[0..len];
956
+ }
957
+
958
+ fn flushSpanSelectionLine(
959
+ layout: TextSpanLayout,
960
+ line_index: usize,
961
+ left: f32,
962
+ right: f32,
963
+ range: text_interaction.TextRange,
964
+ output: []text_interaction.TextSelectionRect,
965
+ len: *usize,
966
+ ) bool {
967
+ if (len.* >= output.len) return false;
968
+ const top = @as(f32, @floatFromInt(line_index)) * layout.line_height;
969
+ output[len.*] = .{
970
+ .range = range,
971
+ .rect = geometry.RectF.init(left, top, @max(1, right - left), @max(1, layout.line_height)),
972
+ };
973
+ len.* += 1;
974
+ return true;
975
+ }
976
+
977
+ /// Deep equality for widget invalidation: styles, text bytes, and link
978
+ /// payload bytes.
979
+ pub fn textSpansEqual(a: []const TextSpan, b: []const TextSpan) bool {
980
+ if (a.len != b.len) return false;
981
+ for (a, b) |left, right| {
982
+ if (!std.mem.eql(u8, left.text, right.text)) return false;
983
+ if (left.weight != right.weight) return false;
984
+ if (left.italic != right.italic) return false;
985
+ if (left.monospace != right.monospace) return false;
986
+ if (left.color != right.color) return false;
987
+ if (left.background != right.background) return false;
988
+ if (left.underline != right.underline) return false;
989
+ if (left.strikethrough != right.strikethrough) return false;
990
+ if (left.scale != right.scale) return false;
991
+ if (!std.mem.eql(u8, left.link, right.link)) return false;
992
+ }
993
+ return true;
994
+ }
995
+
996
+ /// True when any span carries a link payload.
997
+ pub fn textSpansHaveLinks(spans: []const TextSpan) bool {
998
+ for (spans) |span| {
999
+ if (span.link.len > 0) return true;
1000
+ }
1001
+ return false;
1002
+ }
1003
+
1004
+ pub fn textSpanLinkCount(spans: []const TextSpan) usize {
1005
+ var count: usize = 0;
1006
+ for (spans) |span| {
1007
+ if (span.link.len > 0) count += 1;
1008
+ }
1009
+ return count;
1010
+ }