@native-sdk/cli 0.0.0 → 0.4.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 (373) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/assets/icon.icns +0 -0
  5. package/assets/icon.ico +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +15 -0
  8. package/assets/native-sdk.entitlements +10 -0
  9. package/assets/native-sdk.manifest +12 -0
  10. package/bin/native.js +156 -0
  11. package/build/app.zig +716 -0
  12. package/build.zig +2593 -0
  13. package/build.zig.zon +24 -0
  14. package/native-sdk.d.ts +615 -0
  15. package/package.json +45 -8
  16. package/skill-data/automation/SKILL.md +248 -0
  17. package/skill-data/core/SKILL.md +249 -0
  18. package/skill-data/core/references/app-model-runtime.md +141 -0
  19. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  20. package/skill-data/core/references/frontend-assets.md +124 -0
  21. package/skill-data/core/references/project-anatomy.md +108 -0
  22. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  23. package/skill-data/native-ui/SKILL.md +1080 -0
  24. package/skills/native-sdk/SKILL.md +33 -0
  25. package/src/app_runner/root.zig +740 -0
  26. package/src/assets/root.zig +20 -0
  27. package/src/automation/protocol.zig +285 -0
  28. package/src/automation/root.zig +13 -0
  29. package/src/automation/server.zig +506 -0
  30. package/src/automation/snapshot.zig +1202 -0
  31. package/src/automation/watcher.zig +165 -0
  32. package/src/bridge/root.zig +507 -0
  33. package/src/debug/root.zig +246 -0
  34. package/src/embed/app_exports.zig +11 -0
  35. package/src/embed/c_api.zig +793 -0
  36. package/src/embed/c_exports.zig +10 -0
  37. package/src/embed/chrome.zig +168 -0
  38. package/src/embed/conversions.zig +359 -0
  39. package/src/embed/host.zig +960 -0
  40. package/src/embed/root.zig +81 -0
  41. package/src/embed/tests.zig +2552 -0
  42. package/src/embed/types.zig +382 -0
  43. package/src/embed/ui_host.zig +289 -0
  44. package/src/extensions/root.zig +197 -0
  45. package/src/frontend/root.zig +48 -0
  46. package/src/js/root.zig +65 -0
  47. package/src/platform/android/NativeSdkActivity.java +1370 -0
  48. package/src/platform/android/android_host.c +696 -0
  49. package/src/platform/android/files.zig +9 -0
  50. package/src/platform/android/native_sdk_app.h +237 -0
  51. package/src/platform/ios/files.zig +8 -0
  52. package/src/platform/ios/native_sdk_app.h +289 -0
  53. package/src/platform/ios/uikit_host.m +2739 -0
  54. package/src/platform/linux/cef_host.cpp +599 -0
  55. package/src/platform/linux/gtk_host.c +5362 -0
  56. package/src/platform/linux/gtk_host.h +265 -0
  57. package/src/platform/linux/root.zig +1519 -0
  58. package/src/platform/macos/appkit_host.h +604 -0
  59. package/src/platform/macos/appkit_host.m +10392 -0
  60. package/src/platform/macos/automation_host.h +14 -0
  61. package/src/platform/macos/automation_host.m +12 -0
  62. package/src/platform/macos/cef_host.mm +3028 -0
  63. package/src/platform/macos/root.zig +2260 -0
  64. package/src/platform/null_platform.zig +2232 -0
  65. package/src/platform/null_platform_tests.zig +670 -0
  66. package/src/platform/policy_values.zig +14 -0
  67. package/src/platform/root.zig +181 -0
  68. package/src/platform/types.zig +2681 -0
  69. package/src/platform/windows/cef_host.cpp +3 -0
  70. package/src/platform/windows/root.zig +1521 -0
  71. package/src/platform/windows/webview2_host.cpp +6230 -0
  72. package/src/primitives/app_dirs/root.zig +440 -0
  73. package/src/primitives/app_manifest/root.zig +109 -0
  74. package/src/primitives/app_manifest/tests.zig +758 -0
  75. package/src/primitives/app_manifest/types.zig +587 -0
  76. package/src/primitives/app_manifest/validation.zig +828 -0
  77. package/src/primitives/assets/root.zig +520 -0
  78. package/src/primitives/canvas/a11y_audit.zig +390 -0
  79. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  80. package/src/primitives/canvas/app_icon.zig +1590 -0
  81. package/src/primitives/canvas/chart.zig +392 -0
  82. package/src/primitives/canvas/chart_tests.zig +751 -0
  83. package/src/primitives/canvas/commands.zig +446 -0
  84. package/src/primitives/canvas/drawing.zig +309 -0
  85. package/src/primitives/canvas/equality.zig +316 -0
  86. package/src/primitives/canvas/events.zig +696 -0
  87. package/src/primitives/canvas/font_coverage.zig +118 -0
  88. package/src/primitives/canvas/font_ttf.zig +510 -0
  89. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  90. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  91. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  92. package/src/primitives/canvas/frame.zig +679 -0
  93. package/src/primitives/canvas/frame_metrics.zig +462 -0
  94. package/src/primitives/canvas/gpu.zig +639 -0
  95. package/src/primitives/canvas/hash.zig +112 -0
  96. package/src/primitives/canvas/icons/alert.svg +1 -0
  97. package/src/primitives/canvas/icons/archive.svg +1 -0
  98. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  99. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  100. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  101. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  102. package/src/primitives/canvas/icons/check.svg +1 -0
  103. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  104. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  105. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  106. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  107. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  108. package/src/primitives/canvas/icons/clock.svg +1 -0
  109. package/src/primitives/canvas/icons/copy.svg +1 -0
  110. package/src/primitives/canvas/icons/download.svg +1 -0
  111. package/src/primitives/canvas/icons/edit.svg +1 -0
  112. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  113. package/src/primitives/canvas/icons/external-link.svg +1 -0
  114. package/src/primitives/canvas/icons/eye.svg +1 -0
  115. package/src/primitives/canvas/icons/file-text.svg +1 -0
  116. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  117. package/src/primitives/canvas/icons/folder.svg +1 -0
  118. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  119. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  120. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  121. package/src/primitives/canvas/icons/info.svg +1 -0
  122. package/src/primitives/canvas/icons/menu.svg +1 -0
  123. package/src/primitives/canvas/icons/missing.svg +1 -0
  124. package/src/primitives/canvas/icons/moon.svg +1 -0
  125. package/src/primitives/canvas/icons/music.svg +1 -0
  126. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  127. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  128. package/src/primitives/canvas/icons/pause.svg +1 -0
  129. package/src/primitives/canvas/icons/play.svg +1 -0
  130. package/src/primitives/canvas/icons/plus.svg +1 -0
  131. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  132. package/src/primitives/canvas/icons/repeat.svg +1 -0
  133. package/src/primitives/canvas/icons/save.svg +1 -0
  134. package/src/primitives/canvas/icons/search.svg +1 -0
  135. package/src/primitives/canvas/icons/send.svg +1 -0
  136. package/src/primitives/canvas/icons/settings.svg +1 -0
  137. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  138. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  139. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  140. package/src/primitives/canvas/icons/sun.svg +1 -0
  141. package/src/primitives/canvas/icons/terminal.svg +1 -0
  142. package/src/primitives/canvas/icons/trash.svg +1 -0
  143. package/src/primitives/canvas/icons/volume.svg +1 -0
  144. package/src/primitives/canvas/icons/wrench.svg +1 -0
  145. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  146. package/src/primitives/canvas/icons/x.svg +1 -0
  147. package/src/primitives/canvas/icons.zig +249 -0
  148. package/src/primitives/canvas/layout_audit.zig +744 -0
  149. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  150. package/src/primitives/canvas/markdown.zig +1031 -0
  151. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  152. package/src/primitives/canvas/markdown_tests.zig +538 -0
  153. package/src/primitives/canvas/plan_key_index.zig +120 -0
  154. package/src/primitives/canvas/png.zig +355 -0
  155. package/src/primitives/canvas/reference.zig +1457 -0
  156. package/src/primitives/canvas/reference_blur.zig +104 -0
  157. package/src/primitives/canvas/reference_memo.zig +322 -0
  158. package/src/primitives/canvas/reference_paths.zig +184 -0
  159. package/src/primitives/canvas/reference_tests.zig +1650 -0
  160. package/src/primitives/canvas/render.zig +747 -0
  161. package/src/primitives/canvas/render_effects.zig +255 -0
  162. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  163. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  164. package/src/primitives/canvas/render_images.zig +247 -0
  165. package/src/primitives/canvas/render_layers.zig +260 -0
  166. package/src/primitives/canvas/render_paths.zig +347 -0
  167. package/src/primitives/canvas/render_resources.zig +51 -0
  168. package/src/primitives/canvas/render_tests.zig +3186 -0
  169. package/src/primitives/canvas/root.zig +744 -0
  170. package/src/primitives/canvas/serialization.zig +1528 -0
  171. package/src/primitives/canvas/svg_icon.zig +636 -0
  172. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  173. package/src/primitives/canvas/test_support.zig +422 -0
  174. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  175. package/src/primitives/canvas/tests.zig +30 -0
  176. package/src/primitives/canvas/text.zig +82 -0
  177. package/src/primitives/canvas/text_atlas.zig +408 -0
  178. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  179. package/src/primitives/canvas/text_interaction.zig +615 -0
  180. package/src/primitives/canvas/text_layout.zig +1069 -0
  181. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  182. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  183. package/src/primitives/canvas/text_layout_types.zig +128 -0
  184. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  185. package/src/primitives/canvas/text_metrics.zig +309 -0
  186. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  187. package/src/primitives/canvas/text_span_tests.zig +608 -0
  188. package/src/primitives/canvas/text_spans.zig +1010 -0
  189. package/src/primitives/canvas/text_tests.zig +1949 -0
  190. package/src/primitives/canvas/themes/geist.zig +470 -0
  191. package/src/primitives/canvas/tokens.zig +1522 -0
  192. package/src/primitives/canvas/ui.zig +2686 -0
  193. package/src/primitives/canvas/ui_markup.zig +3791 -0
  194. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  195. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  196. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  197. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  198. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  199. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  200. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  201. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  202. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  203. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  204. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  205. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  206. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  207. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  208. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  209. package/src/primitives/canvas/ui_provenance.zig +96 -0
  210. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  211. package/src/primitives/canvas/ui_schema.zig +825 -0
  212. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  213. package/src/primitives/canvas/ui_tests.zig +1432 -0
  214. package/src/primitives/canvas/vector.zig +919 -0
  215. package/src/primitives/canvas/vector_tests.zig +547 -0
  216. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  217. package/src/primitives/canvas/virtual_extents.zig +540 -0
  218. package/src/primitives/canvas/widget_access.zig +208 -0
  219. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  220. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  221. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  222. package/src/primitives/canvas/widget_layout.zig +2055 -0
  223. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  224. package/src/primitives/canvas/widget_metrics.zig +228 -0
  225. package/src/primitives/canvas/widget_render.zig +2534 -0
  226. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  227. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  228. package/src/primitives/canvas/widget_render_style.zig +626 -0
  229. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  230. package/src/primitives/canvas/widget_routing.zig +475 -0
  231. package/src/primitives/canvas/widget_runtime.zig +307 -0
  232. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  233. package/src/primitives/canvas/widget_semantics.zig +480 -0
  234. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  235. package/src/primitives/canvas/widget_tests.zig +8 -0
  236. package/src/primitives/canvas/widget_text_input.zig +351 -0
  237. package/src/primitives/canvas/widget_text_select.zig +147 -0
  238. package/src/primitives/canvas/widget_tree.zig +232 -0
  239. package/src/primitives/canvas/widgets.zig +1319 -0
  240. package/src/primitives/diagnostics/root.zig +445 -0
  241. package/src/primitives/geometry/root.zig +889 -0
  242. package/src/primitives/json/root.zig +272 -0
  243. package/src/primitives/platform_info/root.zig +450 -0
  244. package/src/primitives/trace/root.zig +569 -0
  245. package/src/root.zig +165 -0
  246. package/src/runtime/api.zig +431 -0
  247. package/src/runtime/async_bridge.zig +49 -0
  248. package/src/runtime/automation_commands.zig +686 -0
  249. package/src/runtime/automation_liveness_tests.zig +140 -0
  250. package/src/runtime/automation_snapshot.zig +246 -0
  251. package/src/runtime/automation_widget_dispatch.zig +617 -0
  252. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  253. package/src/runtime/bridge_payload.zig +194 -0
  254. package/src/runtime/bridge_permission_tests.zig +551 -0
  255. package/src/runtime/bridge_responses.zig +414 -0
  256. package/src/runtime/builtin_bridge.zig +554 -0
  257. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  258. package/src/runtime/canvas_font_tests.zig +410 -0
  259. package/src/runtime/canvas_fonts.zig +229 -0
  260. package/src/runtime/canvas_frame.zig +1734 -0
  261. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  262. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  263. package/src/runtime/canvas_frame_helpers.zig +414 -0
  264. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  265. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  266. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  267. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  268. package/src/runtime/canvas_frame_tests.zig +9 -0
  269. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  270. package/src/runtime/canvas_image_tests.zig +362 -0
  271. package/src/runtime/canvas_images.zig +224 -0
  272. package/src/runtime/canvas_limits.zig +317 -0
  273. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  274. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  275. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  276. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  277. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  278. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  279. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  280. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  281. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  282. package/src/runtime/canvas_widget_display.zig +677 -0
  283. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  284. package/src/runtime/canvas_widget_events.zig +1098 -0
  285. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  286. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  287. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  288. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  289. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  290. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  291. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  292. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  293. package/src/runtime/canvas_widget_state.zig +798 -0
  294. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  295. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  296. package/src/runtime/clock.zig +231 -0
  297. package/src/runtime/core.zig +927 -0
  298. package/src/runtime/effects.zig +4197 -0
  299. package/src/runtime/effects_audio_tests.zig +690 -0
  300. package/src/runtime/effects_clipboard_tests.zig +317 -0
  301. package/src/runtime/effects_fetch_tests.zig +1031 -0
  302. package/src/runtime/effects_file_tests.zig +407 -0
  303. package/src/runtime/effects_tests.zig +1251 -0
  304. package/src/runtime/flow.zig +1175 -0
  305. package/src/runtime/frame_profile.zig +221 -0
  306. package/src/runtime/gpu_surface_events.zig +639 -0
  307. package/src/runtime/launch_timing.zig +59 -0
  308. package/src/runtime/markdown_app_tests.zig +224 -0
  309. package/src/runtime/native_command_event_tests.zig +431 -0
  310. package/src/runtime/platform_bridge_tests.zig +8 -0
  311. package/src/runtime/platform_config_tests.zig +392 -0
  312. package/src/runtime/root.zig +150 -0
  313. package/src/runtime/runtime_event_tests.zig +1079 -0
  314. package/src/runtime/session_journal.zig +1522 -0
  315. package/src/runtime/session_record.zig +332 -0
  316. package/src/runtime/session_replay.zig +274 -0
  317. package/src/runtime/session_state.zig +30 -0
  318. package/src/runtime/session_tests.zig +399 -0
  319. package/src/runtime/shell_layout.zig +472 -0
  320. package/src/runtime/shell_layout_tests.zig +1246 -0
  321. package/src/runtime/shell_view_tests.zig +604 -0
  322. package/src/runtime/state.zig +110 -0
  323. package/src/runtime/system_services.zig +392 -0
  324. package/src/runtime/test_support.zig +79 -0
  325. package/src/runtime/tests.zig +30 -0
  326. package/src/runtime/ui_app.zig +3290 -0
  327. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  328. package/src/runtime/ui_app_provenance.zig +370 -0
  329. package/src/runtime/ui_app_tests.zig +3254 -0
  330. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  331. package/src/runtime/ui_app_window_tests.zig +442 -0
  332. package/src/runtime/validation.zig +206 -0
  333. package/src/runtime/view.zig +908 -0
  334. package/src/runtime/view_canvas.zig +850 -0
  335. package/src/runtime/view_widget_control.zig +433 -0
  336. package/src/runtime/view_widget_scroll.zig +360 -0
  337. package/src/runtime/view_widget_text.zig +275 -0
  338. package/src/runtime/view_widget_tree.zig +843 -0
  339. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  340. package/src/runtime/widget_bridge.zig +231 -0
  341. package/src/runtime/window_command_bridge_tests.zig +403 -0
  342. package/src/runtime/window_storage.zig +255 -0
  343. package/src/runtime/window_view_runtime.zig +727 -0
  344. package/src/runtime/window_views.zig +334 -0
  345. package/src/security/root.zig +118 -0
  346. package/src/tooling/android.zig +1172 -0
  347. package/src/tooling/assets.zig +128 -0
  348. package/src/tooling/buildgraph.zig +379 -0
  349. package/src/tooling/cef.zig +671 -0
  350. package/src/tooling/codesign.zig +172 -0
  351. package/src/tooling/components/identity_tests.zig +185 -0
  352. package/src/tooling/components/stepper.zig +113 -0
  353. package/src/tooling/components/timeline.native +27 -0
  354. package/src/tooling/components/timeline_item.zig +131 -0
  355. package/src/tooling/default_icon.icns +0 -0
  356. package/src/tooling/default_icon.png +0 -0
  357. package/src/tooling/dev.zig +172 -0
  358. package/src/tooling/doctor.zig +349 -0
  359. package/src/tooling/eject_components.zig +170 -0
  360. package/src/tooling/embedlib.zig +290 -0
  361. package/src/tooling/ios.zig +528 -0
  362. package/src/tooling/manifest.zig +1939 -0
  363. package/src/tooling/package.zig +2388 -0
  364. package/src/tooling/process_tree.zig +114 -0
  365. package/src/tooling/raw_manifest.zig +193 -0
  366. package/src/tooling/root.zig +22 -0
  367. package/src/tooling/templates.zig +3191 -0
  368. package/src/tooling/toolchain.zig +371 -0
  369. package/src/tooling/verbs.zig +204 -0
  370. package/src/tooling/web_engine.zig +135 -0
  371. package/src/tooling/xcodeproj.zig +462 -0
  372. package/src/window_state/root.zig +507 -0
  373. package/index.js +0 -2
@@ -0,0 +1,1069 @@
1
+ const std = @import("std");
2
+ const geometry = @import("geometry");
3
+ const canvas = @import("root.zig");
4
+ const text_atlas = @import("text_atlas.zig");
5
+ const text_layout_types = @import("text_layout_types.zig");
6
+ const text_layout_cache = @import("text_layout_cache.zig");
7
+ const text_layout_hash = @import("text_layout_hash.zig");
8
+ const text_measure_cache = @import("text_measure_cache.zig");
9
+ const text_metrics = @import("text_metrics.zig");
10
+
11
+ const Error = canvas.Error;
12
+ const FontId = canvas.FontId;
13
+ const DisplayList = canvas.DisplayList;
14
+ const Glyph = text_atlas.Glyph;
15
+ const max_text_bounds_layout_lines: usize = 64;
16
+
17
+ pub const DrawText = text_layout_types.DrawText;
18
+ pub const TextWrap = text_layout_types.TextWrap;
19
+ pub const TextOverflow = text_layout_types.TextOverflow;
20
+ pub const TextAlign = text_layout_types.TextAlign;
21
+ pub const TextLayoutOptions = text_layout_types.TextLayoutOptions;
22
+ pub const TextLine = text_layout_types.TextLine;
23
+ pub const TextLayout = text_layout_types.TextLayout;
24
+ pub const TextLayoutKey = text_layout_types.TextLayoutKey;
25
+ pub const TextLayoutPlan = text_layout_cache.TextLayoutPlan;
26
+ pub const TextLayoutPlanSet = text_layout_cache.TextLayoutPlanSet;
27
+ pub const TextLayoutCacheEntry = text_layout_cache.TextLayoutCacheEntry;
28
+ pub const TextLayoutCacheActionKind = text_layout_cache.TextLayoutCacheActionKind;
29
+ pub const TextLayoutCacheAction = text_layout_cache.TextLayoutCacheAction;
30
+ pub const TextLayoutCachePlan = text_layout_cache.TextLayoutCachePlan;
31
+ pub const TextLayoutCachePlanner = text_layout_cache.TextLayoutCachePlanner;
32
+ pub const mono_advance_em = text_metrics.mono_advance_em;
33
+ pub const estimateTextWidth = text_metrics.estimateTextWidth;
34
+ pub const estimateTextWidthForFont = text_metrics.estimateTextWidthForFont;
35
+ pub const estimateTextWidthForFace = text_metrics.estimateTextWidthForFace;
36
+ pub const estimateTextAdvanceForBytes = text_metrics.estimateTextAdvanceForBytes;
37
+ pub const estimatedGlyphAdvance = text_metrics.estimatedGlyphAdvance;
38
+ pub const TextMeasureProvider = text_metrics.TextMeasureProvider;
39
+ pub const measureTextWidthForFont = text_metrics.measureTextWidthForFont;
40
+ pub const measureTextAdvance = text_metrics.measureTextAdvance;
41
+
42
+ /// The measurement provider a DrawText carries via its layout options, if
43
+ /// any. Runs without layout options always measure with the estimator.
44
+ fn drawTextMeasure(text: DrawText) ?*const text_metrics.TextMeasureProvider {
45
+ const options = text.text_layout orelse return null;
46
+ return options.measure;
47
+ }
48
+
49
+ const textLayoutOptionsForDrawText = text_layout_hash.textLayoutOptionsForDrawText;
50
+ const textLayoutKey = text_layout_hash.textLayoutKey;
51
+ pub const textLayoutKeysEqual = text_layout_hash.textLayoutKeysEqual;
52
+
53
+ /// The elision marker: U+2026 HORIZONTAL ELLIPSIS, taken from the run's
54
+ /// own face. Both bundled faces cover it; a registered face that lacks
55
+ /// it takes the same documented fallback every uncovered codepoint does
56
+ /// (the face's `.notdef` advance in layout, the block glyph in paint),
57
+ /// so the painted extent and the measured extent still agree.
58
+ pub const text_ellipsis = "\u{2026}";
59
+ pub const text_ellipsis_codepoint: u21 = 0x2026;
60
+
61
+ /// Advance of the ellipsis marker on the run's measurement seam — the
62
+ /// injected provider when the run carries one, the deterministic
63
+ /// estimator otherwise. Elision decisions and ellipsis painting must
64
+ /// both use this so the marker never overruns the box it promised. The
65
+ /// estimator answers from a comptime constant: this runs once per
66
+ /// overflowing line every frame, and the marker's codepoint never
67
+ /// changes.
68
+ pub fn textEllipsisAdvance(measure: ?*const text_metrics.TextMeasureProvider, font_id: FontId, size: f32) f32 {
69
+ if (measure) |provider| return provider.measureWidth(font_id, size, text_ellipsis);
70
+ return text_metrics.estimatedTextEllipsisAdvance(font_id, size);
71
+ }
72
+
73
+ /// True when a run's options ask for single-line trailing elision:
74
+ /// `wrap = .none` bounded by a real width with the (default) ellipsis
75
+ /// overflow policy. Wrapping runs and unbounded runs never elide.
76
+ fn lineElisionEnabled(options: TextLayoutOptions) bool {
77
+ return options.wrap == .none and
78
+ options.overflow == .ellipsis and
79
+ options.max_width > 0 and
80
+ options.max_width != std.math.inf(f32);
81
+ }
82
+
83
+ /// Exact-fit slack for the elision decision. A label box computed from
84
+ /// positioned frame edges and the same label measured as one run drift
85
+ /// apart by f32 arithmetic dust (~1e-3 px at window coordinates), and
86
+ /// falling off the elision cliff over dust swaps a whole glyph for the
87
+ /// marker. An eighth of a point sits far above that noise and far below
88
+ /// the smallest real overflow (a glyph), and any overhang it admits is
89
+ /// sub-pixel — within the layout audit's own epsilon. Mirrors the
90
+ /// audit's wrap slack.
91
+ pub const text_elision_slack: f32 = 0.125;
92
+
93
+ /// A line's elision result: how much of it painting inks, plus the
94
+ /// advance reserved for the trailing ellipsis. `ellipsis_advance == 0`
95
+ /// on an elided line means not even the marker fits (`max_width`
96
+ /// narrower than the ellipsis itself) — paint nothing rather than lie.
97
+ const LineElision = struct {
98
+ text_len: ?usize = null,
99
+ glyph_len: ?usize = null,
100
+ ellipsis_advance: f32 = 0,
101
+ /// Width of the painted bytes, measured by the elision check on the
102
+ /// run's own seam. Line-bounds construction reuses it so a fitting
103
+ /// line costs exactly one measure, the same as before elision
104
+ /// existed — the fits-check IS the bounds measure.
105
+ painted_width: ?f32 = null,
106
+ };
107
+
108
+ /// Trailing-elision point for the plain line `text[start..end)`. Returns
109
+ /// no-op elision when the line fits `max_width`. Otherwise walks cluster
110
+ /// prefixes with the same seam the line breaker measures with, keeping
111
+ /// the widest prefix whose width plus the ellipsis advance still fits,
112
+ /// and trims trailing break bytes so the marker hugs the kept glyphs
113
+ /// ("Quarterly …" never paints as "Quarterly …"). Prefix and ellipsis
114
+ /// are measured as two runs; any kern between the last kept glyph and
115
+ /// the marker is forfeited, which can only make the painted line
116
+ /// narrower than the budget, never wider.
117
+ fn plainLineElision(text: []const u8, start: usize, end: usize, font_id: FontId, size: f32, options: TextLayoutOptions) LineElision {
118
+ if (!lineElisionEnabled(options) or end <= start) return .{};
119
+ if (options.measure == null) return estimatedPlainLineElision(text, start, end, font_id, size, options);
120
+ if (text_measure_cache.textRunAdvances(options.measure.?, font_id, size, text)) |advances| {
121
+ return advancePlainLineElision(text, start, end, font_id, size, options, advances);
122
+ }
123
+ const full_width = measureTextWidthForFont(options.measure, font_id, text[start..end], size);
124
+ if (full_width <= options.max_width + text_elision_slack) return .{ .painted_width = full_width };
125
+ const ellipsis_advance = textEllipsisAdvance(options.measure, font_id, size);
126
+ if (ellipsis_advance > options.max_width) return .{ .text_len = 0, .ellipsis_advance = 0, .painted_width = 0 };
127
+ const budget = options.max_width - ellipsis_advance;
128
+ var fit = start;
129
+ var fit_width: f32 = 0;
130
+ var index = start;
131
+ while (index < end) {
132
+ const next_index = nextTextOffset(text, index);
133
+ const next_width = measureTextWidthForFont(options.measure, font_id, text[start..next_index], size);
134
+ if (next_width > budget) break;
135
+ index = next_index;
136
+ fit = index;
137
+ fit_width = next_width;
138
+ }
139
+ var trimmed = fit;
140
+ while (trimmed > start and isTextBreakByte(text[trimmed - 1])) trimmed -= 1;
141
+ const painted_width = if (trimmed == fit)
142
+ fit_width
143
+ else
144
+ measureTextWidthForFont(options.measure, font_id, text[start..trimmed], size);
145
+ return .{ .text_len = trimmed - start, .ellipsis_advance = ellipsis_advance, .painted_width = painted_width };
146
+ }
147
+
148
+ /// The batched-provider half of `plainLineElision`, fused into ONE
149
+ /// cluster walk over the run's fetched advances — the same fusion the
150
+ /// estimator half performs, and valid for the same reason: batched
151
+ /// advances are additive by contract (a slice's width is the sum of its
152
+ /// per-cluster advances in accumulation order), so a single pass
153
+ /// accumulates the full width AND tracks the widest prefix that leaves
154
+ /// room for the marker. The advance accumulation performs the identical
155
+ /// f32 additions the old per-prefix provider measurement summed, so the
156
+ /// kept prefix is byte-identical for any provider whose prefix widths
157
+ /// are its advance sums (pinned by the seam-parity tests). The trailing
158
+ /// break-byte trim subtracts the byte's own advance; the subtraction can
159
+ /// drift from a fresh prefix measure by an ulp, which only feeds painted
160
+ /// bounds — never a break or elision decision.
161
+ fn advancePlainLineElision(text: []const u8, start: usize, end: usize, font_id: FontId, size: f32, options: TextLayoutOptions, advances: []const f32) LineElision {
162
+ const ellipsis_advance = textEllipsisAdvance(options.measure, font_id, size);
163
+ const budget = options.max_width - ellipsis_advance;
164
+ var width: f32 = 0;
165
+ var fit = start;
166
+ var fit_width: f32 = 0;
167
+ var index = start;
168
+ while (index < end) {
169
+ const next_index = nextTextOffset(text, index);
170
+ width += advances[index];
171
+ if (width <= budget) {
172
+ fit = next_index;
173
+ fit_width = width;
174
+ }
175
+ index = next_index;
176
+ }
177
+ if (width <= options.max_width + text_elision_slack) return .{ .painted_width = width };
178
+ if (ellipsis_advance > options.max_width) return .{ .text_len = 0, .ellipsis_advance = 0, .painted_width = 0 };
179
+ var trimmed = fit;
180
+ var painted_width = fit_width;
181
+ while (trimmed > start and isTextBreakByte(text[trimmed - 1])) {
182
+ painted_width -= advances[trimmed - 1];
183
+ trimmed -= 1;
184
+ }
185
+ return .{ .text_len = trimmed - start, .ellipsis_advance = ellipsis_advance, .painted_width = @max(0, painted_width) };
186
+ }
187
+
188
+ /// The estimator half of `plainLineElision`, fused into ONE cluster
189
+ /// walk. Estimator cluster advances are additive (a run's width is
190
+ /// exactly the sum of its per-cluster advances, in the same
191
+ /// accumulation order), so a single pass accumulates the full width AND
192
+ /// tracks the widest prefix that leaves room for the marker — an
193
+ /// eligible line therefore costs exactly the one measure its bounds
194
+ /// always cost, elided or not. The provider path above cannot fuse:
195
+ /// prefix re-measures are what honor kerning. Trimming a trailing break
196
+ /// byte subtracts its own advance instead of measuring the prefix
197
+ /// again.
198
+ fn estimatedPlainLineElision(text: []const u8, start: usize, end: usize, font_id: FontId, size: f32, options: TextLayoutOptions) LineElision {
199
+ const ellipsis_advance = text_metrics.estimatedTextEllipsisAdvance(font_id, size);
200
+ const budget = options.max_width - ellipsis_advance;
201
+ var width: f32 = 0;
202
+ var fit = start;
203
+ var fit_width: f32 = 0;
204
+ var index = start;
205
+ while (index < end) {
206
+ const next_index = nextTextOffset(text, index);
207
+ const advance = estimateTextAdvanceForBytes(font_id, text[index..next_index], size);
208
+ width += advance;
209
+ if (width <= budget) {
210
+ fit = next_index;
211
+ fit_width = width;
212
+ }
213
+ index = next_index;
214
+ }
215
+ if (width <= options.max_width + text_elision_slack) return .{ .painted_width = width };
216
+ if (ellipsis_advance > options.max_width) return .{ .text_len = 0, .ellipsis_advance = 0, .painted_width = 0 };
217
+ var trimmed = fit;
218
+ var painted_width = fit_width;
219
+ while (trimmed > start and isTextBreakByte(text[trimmed - 1])) {
220
+ painted_width -= estimateTextAdvanceForBytes(font_id, text[trimmed - 1 .. trimmed], size);
221
+ trimmed -= 1;
222
+ }
223
+ return .{ .text_len = trimmed - start, .ellipsis_advance = ellipsis_advance, .painted_width = @max(0, painted_width) };
224
+ }
225
+
226
+ /// Trailing-elision point for the shaped glyph line
227
+ /// `glyphs[glyph_start..glyph_end)`: the glyph-advance mirror of
228
+ /// `plainLineElision`, also mapping the kept glyphs back to their text
229
+ /// range so serialized lines carry the matching painted bytes.
230
+ fn glyphLineElision(text: DrawText, glyph_start: usize, glyph_end: usize, line_text_start: usize, options: TextLayoutOptions) LineElision {
231
+ if (!lineElisionEnabled(options) or glyph_end <= glyph_start) return .{};
232
+ var full_width: f32 = 0;
233
+ for (text.glyphs[glyph_start..glyph_end]) |glyph| full_width += estimatedGlyphAdvance(glyph, text.size);
234
+ if (full_width <= options.max_width + text_elision_slack) return .{};
235
+ const ellipsis_advance = textEllipsisAdvance(options.measure, text.font_id, text.size);
236
+ if (ellipsis_advance > options.max_width) return .{ .text_len = 0, .glyph_len = 0, .ellipsis_advance = 0 };
237
+ const budget = options.max_width - ellipsis_advance;
238
+ var fit = glyph_start;
239
+ var index = glyph_start;
240
+ var width: f32 = 0;
241
+ while (index < glyph_end) {
242
+ const next_width = width + estimatedGlyphAdvance(text.glyphs[index], text.size);
243
+ if (next_width > budget) break;
244
+ width = next_width;
245
+ index += 1;
246
+ fit = index;
247
+ }
248
+ while (fit > glyph_start and isGlyphTextBreak(text, fit - 1)) fit -= 1;
249
+ const painted_range = textRangeForGlyphRangeWithGlyphs(text.text, text.glyphs, glyph_start, fit - glyph_start);
250
+ return .{
251
+ .text_len = painted_range.end -| line_text_start,
252
+ .glyph_len = fit - glyph_start,
253
+ .ellipsis_advance = ellipsis_advance,
254
+ };
255
+ }
256
+
257
+ pub const TextLayoutPlanner = struct {
258
+ plans: []TextLayoutPlan,
259
+ lines: []TextLine,
260
+ plan_len: usize = 0,
261
+ line_len: usize = 0,
262
+
263
+ pub fn init(plans: []TextLayoutPlan, lines: []TextLine) TextLayoutPlanner {
264
+ return .{ .plans = plans, .lines = lines };
265
+ }
266
+
267
+ pub fn reset(self: *TextLayoutPlanner) void {
268
+ self.plan_len = 0;
269
+ self.line_len = 0;
270
+ }
271
+
272
+ pub fn build(self: *TextLayoutPlanner, display_list: DisplayList, options: TextLayoutOptions) Error!TextLayoutPlanSet {
273
+ self.reset();
274
+ if (self.plans.len == 0 and self.lines.len == 0) return .{};
275
+
276
+ for (display_list.commands) |command| {
277
+ switch (command) {
278
+ .draw_text => |value| try self.consumeText(value, options),
279
+ else => {},
280
+ }
281
+ }
282
+ return .{ .plans = self.plans[0..self.plan_len] };
283
+ }
284
+
285
+ fn consumeText(self: *TextLayoutPlanner, text: DrawText, options: TextLayoutOptions) Error!void {
286
+ if (self.plan_len >= self.plans.len) return error.TextLayoutPlanListFull;
287
+ const plan = try layoutTextRunPlan(text, textLayoutOptionsForDrawText(options, text), self.lines[self.line_len..]);
288
+ self.plans[self.plan_len] = plan;
289
+ self.plan_len += 1;
290
+ self.line_len += plan.lineCount();
291
+ }
292
+ };
293
+
294
+ const text_interaction = @import("text_interaction.zig");
295
+
296
+ pub const TextRange = text_interaction.TextRange;
297
+ pub const TextSelectionRect = text_interaction.TextSelectionRect;
298
+ pub const TextSelection = text_interaction.TextSelection;
299
+ pub const TextCaretDirection = text_interaction.TextCaretDirection;
300
+ pub const TextCaretMove = text_interaction.TextCaretMove;
301
+ pub const TextCompositionUpdate = text_interaction.TextCompositionUpdate;
302
+ pub const TextInputEvent = text_interaction.TextInputEvent;
303
+ pub const TextEditState = text_interaction.TextEditState;
304
+ pub const TextBuffer = text_interaction.TextBuffer;
305
+ pub const applyTextInputEvent = text_interaction.applyTextInputEvent;
306
+ pub const snapTextSelection = text_interaction.snapTextSelection;
307
+ pub const snapTextRange = text_interaction.snapTextRange;
308
+ pub const previousTextOffset = text_interaction.previousTextOffset;
309
+ pub const nextTextOffset = text_interaction.nextTextOffset;
310
+ pub const previousTextWordOffset = text_interaction.previousTextWordOffset;
311
+ pub const nextTextWordOffset = text_interaction.nextTextWordOffset;
312
+ pub const snapTextOffset = text_interaction.snapTextOffset;
313
+ pub const utf8SequenceLength = text_interaction.utf8SequenceLength;
314
+ pub const isUtf8ContinuationByte = text_interaction.isUtf8ContinuationByte;
315
+
316
+ pub fn layoutTextRun(text: DrawText, options: TextLayoutOptions, output: []TextLine) Error!TextLayout {
317
+ return (try layoutTextRunPlan(text, options, output)).layout;
318
+ }
319
+
320
+ pub fn layoutTextRunPlan(text: DrawText, options: TextLayoutOptions, output: []TextLine) Error!TextLayoutPlan {
321
+ var len: usize = 0;
322
+ var bounds: ?geometry.RectF = null;
323
+ var lines = TextLineIterator.init(text, options);
324
+ while (lines.next()) |line| {
325
+ if (len >= output.len) return error.TextLayoutLineListFull;
326
+ output[len] = line;
327
+ len += 1;
328
+ bounds = unionOptionalBounds(bounds, line.bounds);
329
+ }
330
+ return .{
331
+ .key = textLayoutKey(text, options),
332
+ .layout = .{ .lines = output[0..len], .bounds = bounds },
333
+ };
334
+ }
335
+
336
+ /// Streams a run's lines one at a time — the same lines `layoutTextRun`
337
+ /// materializes, without a line buffer or a line-count cap. Point/offset
338
+ /// queries (caret rect, selection rects, hit testing) walk this instead
339
+ /// of materializing the layout: an editable widget's text is bounded by
340
+ /// its own byte budget, not by any fixed per-query line list, so a query
341
+ /// against a long document must not be able to fail on line count.
342
+ pub const TextLineIterator = struct {
343
+ text: DrawText,
344
+ options: TextLayoutOptions,
345
+ line_height_value: f32,
346
+ /// Line index so far; each line's baseline derives from it.
347
+ index: usize = 0,
348
+ /// Byte cursor (plain runs) or glyph cursor (glyph runs).
349
+ cursor: usize = 0,
350
+ finished: bool = false,
351
+
352
+ pub fn init(text: DrawText, options: TextLayoutOptions) TextLineIterator {
353
+ return .{ .text = text, .options = options, .line_height_value = lineHeight(text, options) };
354
+ }
355
+
356
+ pub fn next(self: *TextLineIterator) ?TextLine {
357
+ if (self.finished) return null;
358
+ if (self.text.glyphs.len > 0) return self.nextGlyphLine();
359
+ return self.nextPlainLine();
360
+ }
361
+
362
+ fn nextPlainLine(self: *TextLineIterator) ?TextLine {
363
+ const bytes = self.text.text;
364
+ if (bytes.len == 0) {
365
+ self.finished = true;
366
+ return self.emit(0, 0, 0, 0, .{});
367
+ }
368
+ const start = self.cursor;
369
+ const end = nextTextLineEnd(bytes, start, self.text.font_id, self.text.size, self.options);
370
+ if (end >= bytes.len) {
371
+ self.finished = true;
372
+ } else {
373
+ var next_start = end;
374
+ if (next_start < bytes.len and bytes[next_start] == '\n') next_start += 1;
375
+ while (self.options.wrap == .word and next_start < bytes.len and isTextBreakByte(bytes[next_start])) next_start += 1;
376
+ self.cursor = next_start;
377
+ }
378
+ const elision = plainLineElision(bytes, start, end, self.text.font_id, self.text.size, self.options);
379
+ return self.emit(start, end - start, start, end - start, elision);
380
+ }
381
+
382
+ fn nextGlyphLine(self: *TextLineIterator) ?TextLine {
383
+ var glyph_start = self.cursor;
384
+ while (self.options.wrap == .word and glyph_start < self.text.glyphs.len and isGlyphTextBreak(self.text, glyph_start)) glyph_start += 1;
385
+ if (glyph_start >= self.text.glyphs.len) {
386
+ self.finished = true;
387
+ // A run whose glyphs are all breaks still lays out as one
388
+ // empty line (the buffered path's fallback emission).
389
+ if (self.index == 0) return self.emit(0, 0, 0, 0, .{});
390
+ return null;
391
+ }
392
+ const glyph_end = nextGlyphLineEnd(self.text, glyph_start, self.options);
393
+ const range = textRangeForGlyphRangeWithGlyphs(self.text.text, self.text.glyphs, glyph_start, glyph_end - glyph_start);
394
+ self.cursor = glyph_end;
395
+ const elision = glyphLineElision(self.text, glyph_start, glyph_end, range.start, self.options);
396
+ return self.emit(range.start, range.byteLen(self.text.text.len), glyph_start, glyph_end - glyph_start, elision);
397
+ }
398
+
399
+ fn emit(self: *TextLineIterator, text_start: usize, text_len: usize, glyph_start: usize, glyph_len: usize, elision: LineElision) TextLine {
400
+ const line = textLineAt(self.text, text_start, text_len, glyph_start, glyph_len, self.index, self.line_height_value, self.options, elision);
401
+ self.index += 1;
402
+ return line;
403
+ }
404
+ };
405
+
406
+ /// Conservative ink allowance around a text run's metric box. Command
407
+ /// bounds must cover everything a command may ink (stroke bounds inflate
408
+ /// by stroke width, blur bounds by the radius); text metrics only cover
409
+ /// advances, and real glyph outlines overhang them: a wide glyph behind
410
+ /// the flat 0.65em multibyte estimate (arrows, dingbats) pokes up to
411
+ /// ~0.3em past the last advance, descenders plus anti-aliasing spill a
412
+ /// hair below the 0.25em descent box, and italic or negative-LSB glyphs
413
+ /// lean slightly left of the pen. Renderers clip to command bounds, so a
414
+ /// metric-tight box visibly shaved tail glyphs off reference screenshots.
415
+ fn textInkInsets(size: f32) geometry.InsetsF {
416
+ const em = @max(0, size);
417
+ return geometry.InsetsF.init(0, em * 0.35, em * 0.1, em * 0.1);
418
+ }
419
+
420
+ pub fn textBounds(value: DrawText) ?geometry.RectF {
421
+ const metric = metricTextBounds(value) orelse return null;
422
+ return metric.inflate(textInkInsets(value.size));
423
+ }
424
+
425
+ fn metricTextBounds(value: DrawText) ?geometry.RectF {
426
+ if (value.glyphs.len == 0 and value.text.len == 0) return null;
427
+ if (value.text_layout) |options| {
428
+ var lines: [max_text_bounds_layout_lines]TextLine = undefined;
429
+ if (layoutTextRun(value, options, &lines)) |layout| {
430
+ if (layout.bounds) |bounds| return bounds;
431
+ } else |_| {}
432
+ }
433
+
434
+ var min_x = value.origin.x;
435
+ var min_y = value.origin.y - value.size;
436
+ var max_x = value.origin.x;
437
+ var max_y = value.origin.y + value.size * 0.25;
438
+ if (value.glyphs.len > 0) {
439
+ min_x = value.origin.x + value.glyphs[0].x;
440
+ max_x = min_x + estimatedGlyphAdvance(value.glyphs[0], value.size);
441
+ min_y = value.origin.y + value.glyphs[0].y - value.size;
442
+ max_y = value.origin.y + value.glyphs[0].y + value.size * 0.25;
443
+ for (value.glyphs[1..]) |glyph| {
444
+ const glyph_x = value.origin.x + glyph.x;
445
+ const glyph_y = value.origin.y + glyph.y;
446
+ min_x = @min(min_x, glyph_x);
447
+ max_x = @max(max_x, glyph_x + estimatedGlyphAdvance(glyph, value.size));
448
+ min_y = @min(min_y, glyph_y - value.size);
449
+ max_y = @max(max_y, glyph_y + value.size * 0.25);
450
+ }
451
+ } else {
452
+ max_x = value.origin.x + measureTextWidthForFont(drawTextMeasure(value), value.font_id, value.text, value.size);
453
+ }
454
+
455
+ return geometry.RectF.init(
456
+ min_x,
457
+ min_y,
458
+ @max(value.size * 0.25, max_x - min_x),
459
+ @max(value.size * 1.25, max_y - min_y),
460
+ );
461
+ }
462
+
463
+ /// Caret rectangle for `offset`, streaming the run's lines — no line
464
+ /// buffer and no line-count failure mode: the caret of an arbitrarily
465
+ /// long document always resolves.
466
+ pub fn layoutTextCaretRect(text: DrawText, options: TextLayoutOptions, offset: usize) ?geometry.RectF {
467
+ const line = streamTextLineForOffset(text, options, snapTextOffset(text.text, offset)) orelse return null;
468
+ return textCaretRectForLine(text, line, offset);
469
+ }
470
+
471
+ pub fn textCaretRectForLayout(text: DrawText, layout: TextLayout, offset: usize) ?geometry.RectF {
472
+ const line = textLineForOffset(layout, text.text.len, snapTextOffset(text.text, offset)) orelse return null;
473
+ return textCaretRectForLine(text, line, offset);
474
+ }
475
+
476
+ fn textCaretRectForLine(text: DrawText, line: TextLine, offset: usize) geometry.RectF {
477
+ const x = textLineCaretX(text, line, offset);
478
+ return geometry.RectF.init(x, line.bounds.y, 1, @max(1, line.bounds.height));
479
+ }
480
+
481
+ /// Selection rectangles for `range`, streaming the run's lines. A range
482
+ /// spanning more lines than `output` holds folds the overflow into the
483
+ /// last rectangle (a bounding highlight) instead of failing: selection
484
+ /// drawing degrades, it never errors — select-all over a long document
485
+ /// is an ordinary render, not a capacity fault.
486
+ pub fn layoutTextSelectionRects(
487
+ text: DrawText,
488
+ options: TextLayoutOptions,
489
+ range: TextRange,
490
+ output: []TextSelectionRect,
491
+ ) []const TextSelectionRect {
492
+ const normalized = snapTextRange(text.text, range);
493
+ if (normalized.isCollapsed(text.text.len)) return output[0..0];
494
+
495
+ var accumulator = TextSelectionRectAccumulator{ .output = output };
496
+ var lines = TextLineIterator.init(text, options);
497
+ while (lines.next()) |line| {
498
+ accumulateTextSelectionRect(&accumulator, text, line, normalized);
499
+ }
500
+ return accumulator.slice();
501
+ }
502
+
503
+ pub fn textSelectionRectsForLayout(text: DrawText, layout: TextLayout, range: TextRange, output: []TextSelectionRect) []const TextSelectionRect {
504
+ const normalized = snapTextRange(text.text, range);
505
+ if (normalized.isCollapsed(text.text.len)) return output[0..0];
506
+
507
+ var accumulator = TextSelectionRectAccumulator{ .output = output };
508
+ for (layout.lines) |line| {
509
+ accumulateTextSelectionRect(&accumulator, text, line, normalized);
510
+ }
511
+ return accumulator.slice();
512
+ }
513
+
514
+ const TextSelectionRectAccumulator = struct {
515
+ output: []TextSelectionRect,
516
+ len: usize = 0,
517
+
518
+ fn add(self: *TextSelectionRectAccumulator, range: TextRange, rect: geometry.RectF) void {
519
+ if (self.output.len == 0) return;
520
+ if (self.len == self.output.len) {
521
+ // The caller's rect budget is full: widen the last rect to
522
+ // cover this line too, so the highlight stays a truthful
523
+ // bound over the whole range instead of erroring out.
524
+ const last = &self.output[self.len - 1];
525
+ last.range = TextRange.init(last.range.start, range.end);
526
+ last.rect = last.rect.unionWith(rect);
527
+ return;
528
+ }
529
+ self.output[self.len] = .{ .range = range, .rect = rect };
530
+ self.len += 1;
531
+ }
532
+
533
+ fn slice(self: *const TextSelectionRectAccumulator) []const TextSelectionRect {
534
+ return self.output[0..self.len];
535
+ }
536
+ };
537
+
538
+ fn accumulateTextSelectionRect(accumulator: *TextSelectionRectAccumulator, text: DrawText, line: TextLine, normalized: TextRange) void {
539
+ const line_range = textLineRange(text, line);
540
+ const start = @max(normalized.start, line_range.start);
541
+ const end = @min(normalized.end, line_range.end);
542
+ if (start >= end) return;
543
+
544
+ const x0 = textLineCaretX(text, line, start);
545
+ const x1 = textLineCaretX(text, line, end);
546
+ const left = @min(x0, x1);
547
+ const right = @max(x0, x1);
548
+ accumulator.add(
549
+ TextRange.init(start, end),
550
+ geometry.RectF.init(left, line.bounds.y, @max(1, right - left), @max(1, line.bounds.height)),
551
+ );
552
+ }
553
+
554
+ /// Byte offset for a point, streaming the run's lines — hit testing a
555
+ /// click in a long document has no line-count failure mode.
556
+ pub fn layoutTextOffsetForPoint(text: DrawText, options: TextLayoutOptions, point: geometry.PointF) ?usize {
557
+ var lines = TextLineIterator.init(text, options);
558
+ var candidate: ?TextLine = null;
559
+ while (lines.next()) |line| {
560
+ candidate = line;
561
+ if (point.y < line.bounds.y + line.bounds.height) break;
562
+ }
563
+ const line = candidate orelse return null;
564
+ return textLineOffsetForX(text, line, point.x);
565
+ }
566
+
567
+ pub fn textOffsetForLayoutPoint(text: DrawText, layout: TextLayout, point: geometry.PointF) ?usize {
568
+ const line = textLineForPoint(layout, point) orelse return null;
569
+ return textLineOffsetForX(text, line, point.x);
570
+ }
571
+
572
+ /// Streaming twin of `textLineForOffset`: the line containing
573
+ /// `offset` (already snapped), with the same neighbor semantics.
574
+ fn streamTextLineForOffset(text: DrawText, options: TextLayoutOptions, offset: usize) ?TextLine {
575
+ const normalized = @min(offset, text.text.len);
576
+ var lines = TextLineIterator.init(text, options);
577
+ var previous: ?TextLine = null;
578
+ while (lines.next()) |line| {
579
+ const range = textLineRangeForLength(text.text.len, line);
580
+ if (normalized < range.start) return previous orelse line;
581
+ if (normalized <= range.end) return line;
582
+ previous = line;
583
+ }
584
+ return previous;
585
+ }
586
+
587
+ pub fn nextTextLineEnd(text: []const u8, start: usize, font_id: FontId, size: f32, options: TextLayoutOptions) usize {
588
+ const max_width = if (options.max_width > 0) options.max_width else std.math.inf(f32);
589
+ if (options.wrap == .none or max_width == std.math.inf(f32)) {
590
+ return nextExplicitLineEnd(text, start);
591
+ }
592
+
593
+ // Batched provider path: fetch the run's per-cluster advances once
594
+ // (cached across the run's lines and across rebuilds) and break from
595
+ // cumulative sums — O(L) per run instead of measuring the growing
596
+ // line prefix once per cluster (O(L²), one provider round-trip
597
+ // each). Falls through to the unbatched loop when the provider has
598
+ // no batched entry or the host declined this run.
599
+ if (options.measure) |provider| {
600
+ if (text_measure_cache.textRunAdvances(provider, font_id, size, text)) |advances| {
601
+ return nextTextLineEndFromAdvances(text, start, max_width, options.wrap, advances);
602
+ }
603
+ }
604
+
605
+ var index = start;
606
+ var last_break: ?usize = null;
607
+ while (index < text.len) {
608
+ if (text[index] == '\n') return index;
609
+ const next_index = nextTextOffset(text, index);
610
+ const next_width = measureTextWidthForFont(options.measure, font_id, text[start..next_index], size);
611
+ if (isTextBreakByte(text[index])) last_break = next_index;
612
+ if (next_width > max_width) {
613
+ if (index == start) return next_index;
614
+ if (options.wrap == .word) {
615
+ if (last_break) |break_index| {
616
+ if (break_index > start) return trimTrailingTextBreak(text, start, break_index);
617
+ }
618
+ }
619
+ return index;
620
+ }
621
+ index = next_index;
622
+ }
623
+ return text.len;
624
+ }
625
+
626
+ /// The batched twin of the unbatched `nextTextLineEnd` loop below it:
627
+ /// the same cursor walk, the same break bookkeeping, the same return
628
+ /// points — only `next_width` comes from accumulating the run's
629
+ /// per-cluster advances instead of re-measuring the growing prefix.
630
+ /// The accumulation performs the identical f32 additions a per-prefix
631
+ /// provider whose widths are its advance sums would produce, so the
632
+ /// chosen break offsets are byte-identical (pinned by the seam-parity
633
+ /// tests, including kerning-ish non-uniform advances and multi-byte
634
+ /// clusters).
635
+ fn nextTextLineEndFromAdvances(text: []const u8, start: usize, max_width: f32, wrap: TextWrap, advances: []const f32) usize {
636
+ var index = start;
637
+ var width: f32 = 0;
638
+ var last_break: ?usize = null;
639
+ while (index < text.len) {
640
+ if (text[index] == '\n') return index;
641
+ const next_index = nextTextOffset(text, index);
642
+ const next_width = width + advances[index];
643
+ if (isTextBreakByte(text[index])) last_break = next_index;
644
+ if (next_width > max_width) {
645
+ if (index == start) return next_index;
646
+ if (wrap == .word) {
647
+ if (last_break) |break_index| {
648
+ if (break_index > start) return trimTrailingTextBreak(text, start, break_index);
649
+ }
650
+ }
651
+ return index;
652
+ }
653
+ width = next_width;
654
+ index = next_index;
655
+ }
656
+ return text.len;
657
+ }
658
+
659
+ fn nextGlyphLineEnd(text: DrawText, start: usize, options: TextLayoutOptions) usize {
660
+ const max_width = if (options.max_width > 0) options.max_width else std.math.inf(f32);
661
+ if (options.wrap == .none or max_width == std.math.inf(f32)) return text.glyphs.len;
662
+
663
+ var index = start;
664
+ var width: f32 = 0;
665
+ var last_break: ?usize = null;
666
+ while (index < text.glyphs.len) {
667
+ if (isGlyphTextBreak(text, index)) last_break = index;
668
+ const next_width = width + estimatedGlyphAdvance(text.glyphs[index], text.size);
669
+ if (next_width > max_width) {
670
+ if (index == start) return index + 1;
671
+ if (options.wrap == .word) {
672
+ if (last_break) |break_index| {
673
+ if (break_index > start) return break_index;
674
+ }
675
+ }
676
+ return index;
677
+ }
678
+ width = next_width;
679
+ index += 1;
680
+ }
681
+ return text.glyphs.len;
682
+ }
683
+
684
+ fn nextExplicitLineEnd(text: []const u8, start: usize) usize {
685
+ var index = start;
686
+ while (index < text.len) : (index += 1) {
687
+ if (text[index] == '\n') return index;
688
+ }
689
+ return text.len;
690
+ }
691
+
692
+ fn trimTrailingTextBreak(text: []const u8, start: usize, end: usize) usize {
693
+ var trimmed = end;
694
+ while (trimmed > start and isTextBreakByte(text[trimmed - 1])) {
695
+ trimmed -= 1;
696
+ }
697
+ return if (trimmed == start) end else trimmed;
698
+ }
699
+
700
+ fn textLineAt(
701
+ text: DrawText,
702
+ text_start: usize,
703
+ text_len: usize,
704
+ glyph_start: usize,
705
+ glyph_len: usize,
706
+ line_index: usize,
707
+ line_height_value: f32,
708
+ options: TextLayoutOptions,
709
+ elision: LineElision,
710
+ ) TextLine {
711
+ const baseline = text.origin.y + @as(f32, @floatFromInt(line_index)) * line_height_value;
712
+ // Bounds cover the painted extent: the kept prefix plus the trailing
713
+ // ellipsis on an elided line, so alignment centers what is actually
714
+ // inked and audits see the true painted width. A plain line whose
715
+ // elision check already measured it reuses that width instead of
716
+ // measuring again.
717
+ const painted_text_len = elision.text_len orelse text_len;
718
+ const painted_glyph_len = elision.glyph_len orelse glyph_len;
719
+ const plain_line = painted_glyph_len == 0 or glyph_start >= text.glyphs.len;
720
+ var raw_bounds = if (plain_line and elision.painted_width != null)
721
+ geometry.RectF.init(text.origin.x, baseline - text.size, elision.painted_width.?, line_height_value)
722
+ else
723
+ textLineBounds(text, text_start, painted_text_len, glyph_start, painted_glyph_len, baseline, line_height_value);
724
+ raw_bounds.width += elision.ellipsis_advance;
725
+ const line_bounds = alignTextLineBounds(raw_bounds, options);
726
+ return .{
727
+ .text_start = text_start,
728
+ .text_len = text_len,
729
+ .glyph_start = glyph_start,
730
+ .glyph_len = glyph_len,
731
+ .bounds = line_bounds,
732
+ .baseline = baseline,
733
+ .elided_text_len = elision.text_len,
734
+ .elided_glyph_len = elision.glyph_len,
735
+ .ellipsis_advance = elision.ellipsis_advance,
736
+ };
737
+ }
738
+
739
+ fn alignTextLineBounds(bounds: geometry.RectF, options: TextLayoutOptions) geometry.RectF {
740
+ const max_width = nonNegative(options.max_width);
741
+ if (max_width <= 0 or bounds.width >= max_width) return bounds;
742
+ const extra = max_width - bounds.width;
743
+ const dx = switch (options.alignment) {
744
+ .start => 0,
745
+ .center => extra * 0.5,
746
+ .end => extra,
747
+ };
748
+ return bounds.translate(geometry.OffsetF.init(dx, 0));
749
+ }
750
+
751
+ fn textLineForOffset(layout: TextLayout, text_len: usize, offset: usize) ?TextLine {
752
+ if (layout.lines.len == 0) return null;
753
+ const normalized = @min(offset, text_len);
754
+ var previous: ?TextLine = null;
755
+ for (layout.lines) |line| {
756
+ const range = textLineRangeForLength(text_len, line);
757
+ if (normalized < range.start) return previous orelse line;
758
+ if (normalized <= range.end) return line;
759
+ previous = line;
760
+ }
761
+ return previous;
762
+ }
763
+
764
+ fn textLineForPoint(layout: TextLayout, point: geometry.PointF) ?TextLine {
765
+ var previous: ?TextLine = null;
766
+ for (layout.lines) |line| {
767
+ if (point.y < line.bounds.y + line.bounds.height) return line;
768
+ previous = line;
769
+ }
770
+ return previous;
771
+ }
772
+
773
+ pub fn textLineRange(text: DrawText, line: TextLine) TextRange {
774
+ return textLineRangeForLength(text.text.len, line);
775
+ }
776
+
777
+ fn textLineRangeForLength(text_len: usize, line: TextLine) TextRange {
778
+ const start = @min(line.text_start, text_len);
779
+ const end = @min(text_len, start + line.text_len);
780
+ return TextRange.init(start, end);
781
+ }
782
+
783
+ pub fn textLineCaretX(text: DrawText, line: TextLine, offset: usize) f32 {
784
+ const range = textLineRange(text, line);
785
+ const snapped = clampTextOffsetToRange(text.text, range, offset);
786
+ const x = if (line.glyph_len > 0 and line.glyph_start < text.glyphs.len)
787
+ textLineGlyphCaretX(text, line, range, snapped)
788
+ else
789
+ line.bounds.x + measureTextWidthForFont(drawTextMeasure(text), text.font_id, text.text[range.start..snapped], text.size);
790
+ // Offsets in an elided line's hidden tail pin to the painted right
791
+ // edge (after the ellipsis): selection highlights stay inside the
792
+ // box while the range itself still covers every hidden byte.
793
+ if (line.isElided()) return @min(x, line.bounds.maxX());
794
+ return x;
795
+ }
796
+
797
+ fn textLineGlyphCaretX(text: DrawText, line: TextLine, range: TextRange, offset: usize) f32 {
798
+ if (range.end <= range.start) return line.bounds.x;
799
+ if (offset <= range.start) return line.bounds.x;
800
+ if (offset >= range.end) return line.bounds.x + line.bounds.width;
801
+
802
+ if (textGlyphLineHasExplicitRanges(text, line)) {
803
+ return textLineExplicitGlyphCaretX(text, line, range, offset);
804
+ }
805
+
806
+ const scalar_count = utf8ScalarCount(text.text[range.start..range.end]);
807
+ if (scalar_count == 0) return line.bounds.x;
808
+ const scalar_index = utf8ScalarIndexForOffset(text.text[range.start..range.end], offset - range.start);
809
+ const glyph_offset = @min(line.glyph_len, (scalar_index * line.glyph_len) / scalar_count);
810
+ if (glyph_offset == 0) return line.bounds.x;
811
+ if (glyph_offset >= line.glyph_len or line.glyph_start + glyph_offset >= text.glyphs.len) return line.bounds.x + line.bounds.width;
812
+
813
+ const raw_bounds = textLineBounds(text, line.text_start, line.text_len, line.glyph_start, line.glyph_len, line.baseline, line.bounds.height);
814
+ const first_x = text.glyphs[line.glyph_start].x;
815
+ const glyph = text.glyphs[line.glyph_start + glyph_offset];
816
+ return text.origin.x + glyph.x - first_x + (line.bounds.x - raw_bounds.x);
817
+ }
818
+
819
+ fn textLineOffsetForX(text: DrawText, line: TextLine, x: f32) usize {
820
+ const range = textLineRange(text, line);
821
+ if (x <= line.bounds.x) return range.start;
822
+ // Elided lines: the hidden tail has no painted geometry, so a point
823
+ // past the painted right edge means "everything" (a rightward sweep
824
+ // selects the whole line, hidden bytes included) and a point on the
825
+ // ellipsis itself means the kept prefix.
826
+ if (line.isElided()) {
827
+ if (x >= line.bounds.maxX()) return range.end;
828
+ if (x >= line.bounds.maxX() - line.ellipsis_advance) {
829
+ return snapTextOffset(text.text, @min(range.end, line.text_start + line.paintedTextLen()));
830
+ }
831
+ }
832
+ if (line.glyph_len > 0 and line.glyph_start < text.glyphs.len) {
833
+ return textLineGlyphOffsetForX(text, line, range, x);
834
+ }
835
+
836
+ var cursor = range.start;
837
+ var caret_x = line.bounds.x;
838
+ while (cursor < range.end) {
839
+ const next_cursor = nextTextOffset(text.text, cursor);
840
+ const advance = @max(1, measureTextAdvance(drawTextMeasure(text), text.font_id, text.size, text.text, range.start, cursor, next_cursor));
841
+ if (x < caret_x + advance * 0.5) return cursor;
842
+ caret_x += advance;
843
+ cursor = next_cursor;
844
+ }
845
+ return range.end;
846
+ }
847
+
848
+ fn textLineGlyphOffsetForX(text: DrawText, line: TextLine, range: TextRange, x: f32) usize {
849
+ const glyph_end = @min(text.glyphs.len, line.glyph_start + line.glyph_len);
850
+ const raw_bounds = textLineBounds(text, line.text_start, line.text_len, line.glyph_start, line.glyph_len, line.baseline, line.bounds.height);
851
+ const first_x = text.glyphs[line.glyph_start].x;
852
+ const dx = line.bounds.x - raw_bounds.x;
853
+ if (textGlyphLineHasExplicitRanges(text, line)) {
854
+ return textLineExplicitGlyphOffsetForX(text, line, range, x, first_x, dx);
855
+ }
856
+
857
+ for (text.glyphs[line.glyph_start..glyph_end], 0..) |glyph, glyph_index| {
858
+ const glyph_x = text.origin.x + glyph.x - first_x + dx;
859
+ const advance = @max(1, estimatedGlyphAdvance(glyph, text.size));
860
+ if (x < glyph_x + advance * 0.5) {
861
+ const glyph_range = textRangeForGlyph(text.text, text.glyphs, line.glyph_start + glyph_index);
862
+ return clampTextOffsetToRange(text.text, range, glyph_range.start);
863
+ }
864
+ }
865
+ return range.end;
866
+ }
867
+
868
+ fn clampTextOffsetToRange(text: []const u8, range: TextRange, offset: usize) usize {
869
+ const snapped = snapTextOffset(text, offset);
870
+ if (snapped < range.start) return range.start;
871
+ if (snapped > range.end) return range.end;
872
+ return snapped;
873
+ }
874
+
875
+ fn utf8ScalarIndexForOffset(text: []const u8, offset: usize) usize {
876
+ const target = snapTextOffset(text, offset);
877
+ var cursor: usize = 0;
878
+ var index: usize = 0;
879
+ while (cursor < target) : (index += 1) {
880
+ cursor = nextTextOffset(text, cursor);
881
+ }
882
+ return index;
883
+ }
884
+
885
+ fn lineHeight(text: DrawText, options: TextLayoutOptions) f32 {
886
+ return if (options.line_height > 0) options.line_height else text.size * 1.25;
887
+ }
888
+
889
+ pub fn textLineBounds(text: DrawText, text_start: usize, text_len: usize, glyph_start: usize, glyph_len: usize, baseline: f32, line_height_value: f32) geometry.RectF {
890
+ if (glyph_len > 0 and glyph_start < text.glyphs.len) {
891
+ const glyphs = text.glyphs[glyph_start..@min(text.glyphs.len, glyph_start + glyph_len)];
892
+ const origin_x = glyphs[0].x;
893
+ var min_x: f32 = 0;
894
+ var max_x = estimatedGlyphAdvance(glyphs[0], text.size);
895
+ var min_y = baseline - text.size;
896
+ var max_y = min_y + line_height_value;
897
+ for (glyphs) |glyph| {
898
+ const glyph_x = glyph.x - origin_x;
899
+ min_x = @min(min_x, glyph_x);
900
+ max_x = @max(max_x, glyph_x + estimatedGlyphAdvance(glyph, text.size));
901
+ min_y = @min(min_y, baseline + glyph.y - text.size);
902
+ max_y = @max(max_y, baseline + glyph.y + text.size * 0.25);
903
+ }
904
+ return geometry.RectF.init(text.origin.x + min_x, min_y, @max(0, max_x - min_x), @max(0, max_y - min_y));
905
+ }
906
+ return geometry.RectF.init(
907
+ text.origin.x,
908
+ baseline - text.size,
909
+ plainLineSliceWidth(text, text_start, @min(text.text.len, text_start + text_len)),
910
+ line_height_value,
911
+ );
912
+ }
913
+
914
+ /// Width of the plain line `text.text[start..end)` on the run's own
915
+ /// measurement seam. With a batch-capable provider this sums the run's
916
+ /// ALREADY-CACHED per-cluster advances (a peek hit right after the line
917
+ /// breaker or the elision check fetched them — line bounds stop costing
918
+ /// one provider round-trip per line per frame). It deliberately never
919
+ /// fetches: bounds run per frame over every retained run, and fetching
920
+ /// here turned one memoized host width per single-line run into a full
921
+ /// host shape per run per frame once the retained set outgrew the
922
+ /// cache. Unbatched (and estimator) runs keep the historical per-slice
923
+ /// measure byte-identically.
924
+ fn plainLineSliceWidth(text: DrawText, start: usize, end: usize) f32 {
925
+ if (drawTextMeasure(text)) |provider| {
926
+ if (text_measure_cache.cachedTextRunAdvances(provider, text.font_id, text.size, text.text)) |advances| {
927
+ return text_measure_cache.advanceSliceWidth(advances, start, end);
928
+ }
929
+ }
930
+ return measureTextWidthForFont(drawTextMeasure(text), text.font_id, text.text[start..end], text.size);
931
+ }
932
+
933
+ pub fn isTextBreakByte(byte: u8) bool {
934
+ return byte == ' ' or byte == '\t';
935
+ }
936
+
937
+ fn isGlyphTextBreak(text: DrawText, glyph_index: usize) bool {
938
+ if (glyph_index >= text.glyphs.len) return false;
939
+ const range = textRangeForGlyph(text.text, text.glyphs, glyph_index);
940
+ return range.start < range.end and isTextBreakByte(text.text[range.start]);
941
+ }
942
+
943
+ fn textRangeForGlyph(text: []const u8, glyphs: []const Glyph, glyph_index: usize) TextRange {
944
+ if (glyph_index >= glyphs.len) return TextRange.init(text.len, text.len);
945
+ const glyph = glyphs[glyph_index];
946
+ if (glyph.text_len > 0) return snapTextRange(text, TextRange.init(glyph.text_start, glyph.text_start + glyph.text_len));
947
+ return textRangeForGlyphRange(text, glyph_index, 1, glyphs.len);
948
+ }
949
+
950
+ fn textRangeForGlyphRangeWithGlyphs(text: []const u8, glyphs: []const Glyph, glyph_start: usize, glyph_len: usize) TextRange {
951
+ if (glyph_len == 0 or glyph_start >= glyphs.len) return textRangeForGlyphRange(text, glyph_start, glyph_len, glyphs.len);
952
+ const glyph_end = @min(glyphs.len, glyph_start + glyph_len);
953
+ var explicit_start: usize = text.len;
954
+ var explicit_end: usize = 0;
955
+ for (glyphs[glyph_start..glyph_end]) |glyph| {
956
+ if (glyph.text_len == 0) return textRangeForGlyphRange(text, glyph_start, glyph_len, glyphs.len);
957
+ const range = snapTextRange(text, TextRange.init(glyph.text_start, glyph.text_start + glyph.text_len));
958
+ explicit_start = @min(explicit_start, range.start);
959
+ explicit_end = @max(explicit_end, range.end);
960
+ }
961
+ return TextRange.init(explicit_start, explicit_end);
962
+ }
963
+
964
+ fn textRangeForGlyphRange(text: []const u8, glyph_start: usize, glyph_len: usize, glyph_count: usize) TextRange {
965
+ if (text.len == 0 or glyph_count == 0) return TextRange.init(0, 0);
966
+ const scalar_count = utf8ScalarCount(text);
967
+ if (scalar_count == 0) return TextRange.init(0, 0);
968
+
969
+ const glyph_end = @min(glyph_count, glyph_start + glyph_len);
970
+ const start_scalar = @min(scalar_count, (glyph_start * scalar_count) / glyph_count);
971
+ const end_scalar = @min(scalar_count, ((glyph_end * scalar_count) + glyph_count - 1) / glyph_count);
972
+ return TextRange.init(textOffsetForScalarIndex(text, start_scalar), textOffsetForScalarIndex(text, end_scalar));
973
+ }
974
+
975
+ fn textGlyphLineHasExplicitRanges(text: DrawText, line: TextLine) bool {
976
+ if (line.glyph_len == 0 or line.glyph_start >= text.glyphs.len) return false;
977
+ const glyph_end = @min(text.glyphs.len, line.glyph_start + line.glyph_len);
978
+ for (text.glyphs[line.glyph_start..glyph_end]) |glyph| {
979
+ if (glyph.text_len == 0) return false;
980
+ }
981
+ return true;
982
+ }
983
+
984
+ fn textLineExplicitGlyphCaretX(text: DrawText, line: TextLine, range: TextRange, offset: usize) f32 {
985
+ const glyph_end = @min(text.glyphs.len, line.glyph_start + line.glyph_len);
986
+ const raw_bounds = textLineBounds(text, line.text_start, line.text_len, line.glyph_start, line.glyph_len, line.baseline, line.bounds.height);
987
+ const first_x = text.glyphs[line.glyph_start].x;
988
+ const dx = line.bounds.x - raw_bounds.x;
989
+
990
+ for (line.glyph_start..glyph_end) |glyph_index| {
991
+ const glyph = text.glyphs[glyph_index];
992
+ const glyph_range = textRangeForGlyph(text.text, text.glyphs, glyph_index);
993
+ if (glyph_range.end <= range.start or glyph_range.start >= range.end) continue;
994
+
995
+ const glyph_x = text.origin.x + glyph.x - first_x + dx;
996
+ if (offset <= glyph_range.start) return glyph_x;
997
+ if (offset < glyph_range.end) {
998
+ const advance = @max(1, estimatedGlyphAdvance(glyph, text.size));
999
+ return glyph_x + glyphTextRangeRatio(text.text, glyph_range, offset) * advance;
1000
+ }
1001
+ }
1002
+ return line.bounds.x + line.bounds.width;
1003
+ }
1004
+
1005
+ fn textLineExplicitGlyphOffsetForX(text: DrawText, line: TextLine, range: TextRange, x: f32, first_x: f32, dx: f32) usize {
1006
+ const glyph_end = @min(text.glyphs.len, line.glyph_start + line.glyph_len);
1007
+ for (line.glyph_start..glyph_end) |glyph_index| {
1008
+ const glyph = text.glyphs[glyph_index];
1009
+ const glyph_range = textRangeForGlyph(text.text, text.glyphs, glyph_index);
1010
+ if (glyph_range.end <= range.start or glyph_range.start >= range.end) continue;
1011
+
1012
+ const glyph_x = text.origin.x + glyph.x - first_x + dx;
1013
+ if (x <= glyph_x) return @max(range.start, glyph_range.start);
1014
+
1015
+ const advance = @max(1, estimatedGlyphAdvance(glyph, text.size));
1016
+ if (x < glyph_x + advance) {
1017
+ return textOffsetForGlyphRangeRatio(text.text, glyph_range, (x - glyph_x) / advance);
1018
+ }
1019
+ }
1020
+ return range.end;
1021
+ }
1022
+
1023
+ fn glyphTextRangeRatio(text: []const u8, range: TextRange, offset: usize) f32 {
1024
+ const normalized = snapTextRange(text, range);
1025
+ if (normalized.end <= normalized.start) return 0;
1026
+ const scalar_count = utf8ScalarCount(text[normalized.start..normalized.end]);
1027
+ if (scalar_count == 0) return 0;
1028
+ const scalar_index = utf8ScalarIndexForOffset(text[normalized.start..normalized.end], offset - normalized.start);
1029
+ return @as(f32, @floatFromInt(@min(scalar_index, scalar_count))) / @as(f32, @floatFromInt(scalar_count));
1030
+ }
1031
+
1032
+ fn textOffsetForGlyphRangeRatio(text: []const u8, range: TextRange, ratio: f32) usize {
1033
+ const normalized = snapTextRange(text, range);
1034
+ const scalar_count = utf8ScalarCount(text[normalized.start..normalized.end]);
1035
+ if (scalar_count == 0) return normalized.start;
1036
+ const clamped = std.math.clamp(if (std.math.isFinite(ratio)) ratio else 0, 0, 1);
1037
+ const scalar_index: usize = @intFromFloat(@floor(clamped * @as(f32, @floatFromInt(scalar_count)) + 0.5));
1038
+ return normalized.start + textOffsetForScalarIndex(text[normalized.start..normalized.end], @min(scalar_index, scalar_count));
1039
+ }
1040
+
1041
+ fn textOffsetForScalarIndex(text: []const u8, scalar_index: usize) usize {
1042
+ var offset: usize = 0;
1043
+ var index: usize = 0;
1044
+ while (offset < text.len and index < scalar_index) : (index += 1) {
1045
+ offset = nextTextOffset(text, offset);
1046
+ }
1047
+ return offset;
1048
+ }
1049
+
1050
+ fn utf8ScalarCount(text: []const u8) usize {
1051
+ var count: usize = 0;
1052
+ var index: usize = 0;
1053
+ while (index < text.len) {
1054
+ count += 1;
1055
+ index += @min(utf8SequenceLength(text[index]), text.len - index);
1056
+ }
1057
+ return count;
1058
+ }
1059
+
1060
+ fn unionOptionalBounds(a: ?geometry.RectF, b: ?geometry.RectF) ?geometry.RectF {
1061
+ if (a) |left| {
1062
+ if (b) |right| return left.normalized().unionWith(right.normalized());
1063
+ return left;
1064
+ }
1065
+ return b;
1066
+ }
1067
+ fn nonNegative(value: f32) f32 {
1068
+ return @max(0, value);
1069
+ }