@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,1528 @@
1
+ const geometry = @import("geometry");
2
+ const json = @import("json");
3
+ const canvas = @import("root.zig");
4
+ const drawing_model = @import("drawing.zig");
5
+ const text_model = @import("text.zig");
6
+ const render_model = @import("render.zig");
7
+ const gpu_model = @import("gpu.zig");
8
+
9
+ const ObjectId = canvas.ObjectId;
10
+ const CanvasCommand = canvas.CanvasCommand;
11
+ const CanvasRenderPass = canvas.CanvasRenderPass;
12
+
13
+ const Color = drawing_model.Color;
14
+ const Radius = drawing_model.Radius;
15
+ const Fill = drawing_model.Fill;
16
+ const Stroke = drawing_model.Stroke;
17
+ const PathElement = drawing_model.PathElement;
18
+ const Affine = drawing_model.Affine;
19
+
20
+ const Glyph = text_model.Glyph;
21
+ const GlyphAtlasEntry = text_model.GlyphAtlasEntry;
22
+ const GlyphAtlasCacheAction = text_model.GlyphAtlasCacheAction;
23
+ const GlyphAtlasKey = text_model.GlyphAtlasKey;
24
+ const TextLayoutOptions = text_model.TextLayoutOptions;
25
+ const TextLine = text_model.TextLine;
26
+ const TextLayoutPlan = text_model.TextLayoutPlan;
27
+ const TextLayoutCacheAction = text_model.TextLayoutCacheAction;
28
+ const TextLayoutKey = text_model.TextLayoutKey;
29
+
30
+ const RenderCommand = render_model.RenderCommand;
31
+ const RenderBatch = render_model.RenderBatch;
32
+ const RenderPipelineCacheAction = render_model.RenderPipelineCacheAction;
33
+ const RenderPathGeometry = render_model.RenderPathGeometry;
34
+ const RenderPathGeometryCacheAction = render_model.RenderPathGeometryCacheAction;
35
+ const RenderPathGeometryKey = render_model.RenderPathGeometryKey;
36
+ const RenderImage = render_model.RenderImage;
37
+ const RenderImageCacheAction = render_model.RenderImageCacheAction;
38
+ const RenderImageKey = render_model.RenderImageKey;
39
+ const RenderLayer = render_model.RenderLayer;
40
+ const RenderLayerCacheAction = render_model.RenderLayerCacheAction;
41
+ const RenderLayerKey = render_model.RenderLayerKey;
42
+ const RenderResource = render_model.RenderResource;
43
+ const RenderResourceCacheAction = render_model.RenderResourceCacheAction;
44
+ const RenderResourceKey = render_model.RenderResourceKey;
45
+ const VisualEffect = render_model.VisualEffect;
46
+ const VisualEffectCacheAction = render_model.VisualEffectCacheAction;
47
+ const VisualEffectKey = render_model.VisualEffectKey;
48
+
49
+ const CanvasGpuPacket = gpu_model.CanvasGpuPacket;
50
+ const CanvasGpuCommand = gpu_model.CanvasGpuCommand;
51
+ const CanvasGpuShape = gpu_model.CanvasGpuShape;
52
+ const CanvasGpuPaint = gpu_model.CanvasGpuPaint;
53
+ const CanvasGpuImage = gpu_model.CanvasGpuImage;
54
+ const CanvasGpuText = gpu_model.CanvasGpuText;
55
+ const CanvasGpuEffect = gpu_model.CanvasGpuEffect;
56
+
57
+ pub fn writeDisplayListJson(display_list: canvas.DisplayList, writer: anytype) !void {
58
+ try writer.writeAll("{\"commands\":[");
59
+ for (display_list.commands, 0..) |command, index| {
60
+ if (index > 0) try writer.writeByte(',');
61
+ try writeCommandJson(command, writer);
62
+ }
63
+ try writer.writeAll("]}");
64
+ }
65
+
66
+ fn nonNegative(value: f32) f32 {
67
+ return if (value < 0) 0 else value;
68
+ }
69
+
70
+ pub fn writeCommandJson(command: CanvasCommand, writer: anytype) !void {
71
+ try writer.writeAll("{\"op\":");
72
+ try json.writeString(writer, @tagName(command));
73
+ switch (command) {
74
+ .push_clip => |value| {
75
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
76
+ try writeRectJson(value.rect, writer);
77
+ try writer.writeAll(",\"radius\":");
78
+ try writeRadiusJson(value.radius, writer);
79
+ },
80
+ .pop_clip, .pop_opacity => {},
81
+ .push_opacity => |value| try writer.print(",\"opacity\":{d}", .{value}),
82
+ .transform => |value| {
83
+ try writer.writeAll(",\"matrix\":");
84
+ try writeAffineJson(value, writer);
85
+ },
86
+ .fill_rect => |value| {
87
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
88
+ try writeRectJson(value.rect, writer);
89
+ try writer.writeAll(",\"fill\":");
90
+ try writeFillJson(value.fill, writer);
91
+ },
92
+ .stroke_rect => |value| {
93
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
94
+ try writeRectJson(value.rect, writer);
95
+ try writer.writeAll(",\"radius\":");
96
+ try writeRadiusJson(value.radius, writer);
97
+ try writer.writeAll(",\"stroke\":");
98
+ try writeStrokeJson(value.stroke, writer);
99
+ },
100
+ .fill_rounded_rect => |value| {
101
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
102
+ try writeRectJson(value.rect, writer);
103
+ try writer.writeAll(",\"radius\":");
104
+ try writeRadiusJson(value.radius, writer);
105
+ try writer.writeAll(",\"fill\":");
106
+ try writeFillJson(value.fill, writer);
107
+ },
108
+ .draw_line => |value| {
109
+ try writer.print(",\"id\":{d},\"from\":", .{value.id});
110
+ try writePointJson(value.from, writer);
111
+ try writer.writeAll(",\"to\":");
112
+ try writePointJson(value.to, writer);
113
+ try writer.writeAll(",\"stroke\":");
114
+ try writeStrokeJson(value.stroke, writer);
115
+ },
116
+ .fill_path => |value| {
117
+ try writer.print(",\"id\":{d},\"path\":", .{value.id});
118
+ try writePathJson(value.elements, writer);
119
+ try writer.writeAll(",\"fill\":");
120
+ try writeFillJson(value.fill, writer);
121
+ },
122
+ .stroke_path => |value| {
123
+ try writer.print(",\"id\":{d},\"path\":", .{value.id});
124
+ try writePathJson(value.elements, writer);
125
+ try writer.writeAll(",\"stroke\":");
126
+ try writeStrokeJson(value.stroke, writer);
127
+ // The cap key appears only for the non-default shape: butt is
128
+ // implied by absence, so existing snapshots stay byte-stable
129
+ // and readers without cap handling keep their old meaning.
130
+ if (value.cap != .butt) {
131
+ try writer.writeAll(",\"cap\":");
132
+ try json.writeString(writer, @tagName(value.cap));
133
+ }
134
+ },
135
+ .draw_image => |value| {
136
+ try writer.print(",\"id\":{d},\"image\":{d},\"dst\":", .{ value.id, value.image_id });
137
+ try writeRectJson(value.dst, writer);
138
+ try writer.writeAll(",\"src\":");
139
+ if (value.src) |src| {
140
+ try writeRectJson(src, writer);
141
+ } else {
142
+ try writer.writeAll("null");
143
+ }
144
+ try writer.print(",\"opacity\":{d},\"fit\":", .{value.opacity});
145
+ try json.writeString(writer, @tagName(value.fit));
146
+ try writer.writeAll(",\"sampling\":");
147
+ try json.writeString(writer, @tagName(value.sampling));
148
+ if (radiusIsSet(value.radius)) {
149
+ try writer.writeAll(",\"radius\":");
150
+ try writeRadiusJson(value.radius, writer);
151
+ }
152
+ },
153
+ .draw_text => |value| {
154
+ try writer.print(",\"id\":{d},\"font\":{d},\"size\":{d},\"origin\":", .{ value.id, value.font_id, value.size });
155
+ try writePointJson(value.origin, writer);
156
+ try writer.writeAll(",\"color\":");
157
+ try writeColorJson(value.color, writer);
158
+ try writer.writeAll(",\"text\":");
159
+ try json.writeString(writer, value.text);
160
+ try writer.writeAll(",\"glyphs\":");
161
+ try writeGlyphsJson(value.glyphs, writer);
162
+ if (value.text_layout) |options| {
163
+ try writer.writeAll(",\"layout\":");
164
+ try writeTextLayoutOptionsJson(options, writer);
165
+ }
166
+ },
167
+ .shadow => |value| {
168
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
169
+ try writeRectJson(value.rect, writer);
170
+ try writer.writeAll(",\"radius\":");
171
+ try writeRadiusJson(value.radius, writer);
172
+ try writer.print(",\"offset\":[{d},{d}],\"blur\":{d},\"spread\":{d},\"color\":", .{ value.offset.dx, value.offset.dy, value.blur, value.spread });
173
+ try writeColorJson(value.color, writer);
174
+ },
175
+ .blur => |value| {
176
+ try writer.print(",\"id\":{d},\"rect\":", .{value.id});
177
+ try writeRectJson(value.rect, writer);
178
+ try writer.print(",\"radius\":{d}", .{value.radius});
179
+ },
180
+ }
181
+ try writer.writeByte('}');
182
+ }
183
+
184
+ fn writeTextLayoutOptionsJson(options: TextLayoutOptions, writer: anytype) !void {
185
+ try writer.print("{{\"maxWidth\":{d},\"lineHeight\":{d},\"wrap\":", .{
186
+ nonNegative(options.max_width),
187
+ nonNegative(options.line_height),
188
+ });
189
+ try json.writeString(writer, @tagName(options.wrap));
190
+ try writer.writeAll(",\"align\":");
191
+ try json.writeString(writer, @tagName(options.alignment));
192
+ try writer.writeAll(",\"overflow\":");
193
+ try json.writeString(writer, @tagName(options.overflow));
194
+ try writer.writeByte('}');
195
+ }
196
+
197
+ pub fn writeCanvasRenderPassJson(pass: CanvasRenderPass, writer: anytype) !void {
198
+ try writer.print(
199
+ "{{\"frameIndex\":{d},\"timestampNs\":{d},\"surfaceWidth\":{d},\"surfaceHeight\":{d},\"scale\":{d},\"loadAction\":",
200
+ .{ pass.frame_index, pass.timestamp_ns, pass.surface_size.width, pass.surface_size.height, pass.scale },
201
+ );
202
+ try json.writeString(writer, @tagName(pass.loadAction()));
203
+ try writer.writeAll(",\"fullRepaint\":");
204
+ try writer.writeAll(if (pass.full_repaint) "true" else "false");
205
+ try writer.writeAll(",\"requiresRender\":");
206
+ try writer.writeAll(if (pass.requiresRender()) "true" else "false");
207
+ try writer.writeAll(",\"dirtyBounds\":");
208
+ try writeOptionalRectJson(pass.dirty_bounds, writer);
209
+ try writer.writeAll(",\"scissorBounds\":");
210
+ try writeOptionalRectJson(pass.scissorBounds(), writer);
211
+ try writer.writeAll(",\"commands\":[");
212
+ for (pass.commands, 0..) |command, index| {
213
+ if (index > 0) try writer.writeByte(',');
214
+ try writeRenderCommandJson(command, index, writer);
215
+ }
216
+ try writer.writeAll("],\"batches\":[");
217
+ for (pass.batches, 0..) |batch, index| {
218
+ if (index > 0) try writer.writeByte(',');
219
+ try writeRenderBatchJson(batch, writer);
220
+ }
221
+ try writer.writeAll("],\"pipelineActions\":[");
222
+ for (pass.pipeline_actions, 0..) |action, index| {
223
+ if (index > 0) try writer.writeByte(',');
224
+ try writeRenderPipelineCacheActionJson(action, writer);
225
+ }
226
+ try writer.writeAll("],\"pathGeometries\":[");
227
+ for (pass.path_geometries, 0..) |geometry_plan, index| {
228
+ if (index > 0) try writer.writeByte(',');
229
+ try writeRenderPathGeometryJson(geometry_plan, writer);
230
+ }
231
+ try writer.writeAll("],\"pathGeometryActions\":[");
232
+ for (pass.path_geometry_actions, 0..) |action, index| {
233
+ if (index > 0) try writer.writeByte(',');
234
+ try writeRenderPathGeometryCacheActionJson(action, writer);
235
+ }
236
+ try writer.writeAll("],\"images\":[");
237
+ for (pass.images, 0..) |image, index| {
238
+ if (index > 0) try writer.writeByte(',');
239
+ try writeRenderImageJson(image, writer);
240
+ }
241
+ try writer.writeAll("],\"imageActions\":[");
242
+ for (pass.image_actions, 0..) |action, index| {
243
+ if (index > 0) try writer.writeByte(',');
244
+ try writeRenderImageCacheActionJson(action, writer);
245
+ }
246
+ try writer.writeAll("],\"layers\":[");
247
+ for (pass.layers, 0..) |layer, index| {
248
+ if (index > 0) try writer.writeByte(',');
249
+ try writeRenderLayerJson(layer, writer);
250
+ }
251
+ try writer.writeAll("],\"layerActions\":[");
252
+ for (pass.layer_actions, 0..) |action, index| {
253
+ if (index > 0) try writer.writeByte(',');
254
+ try writeRenderLayerCacheActionJson(action, writer);
255
+ }
256
+ try writer.writeAll("],\"resources\":[");
257
+ for (pass.resources, 0..) |resource, index| {
258
+ if (index > 0) try writer.writeByte(',');
259
+ try writeRenderResourceJson(resource, writer);
260
+ }
261
+ try writer.writeAll("],\"resourceActions\":[");
262
+ for (pass.resource_actions, 0..) |action, index| {
263
+ if (index > 0) try writer.writeByte(',');
264
+ try writeRenderResourceCacheActionJson(action, writer);
265
+ }
266
+ try writer.writeAll("],\"visualEffects\":[");
267
+ for (pass.visual_effects, 0..) |effect, index| {
268
+ if (index > 0) try writer.writeByte(',');
269
+ try writeVisualEffectJson(effect, writer);
270
+ }
271
+ try writer.writeAll("],\"visualEffectActions\":[");
272
+ for (pass.visual_effect_actions, 0..) |action, index| {
273
+ if (index > 0) try writer.writeByte(',');
274
+ try writeVisualEffectCacheActionJson(action, writer);
275
+ }
276
+ try writer.writeAll("],\"glyphAtlasEntries\":[");
277
+ for (pass.glyph_atlas_entries, 0..) |entry, index| {
278
+ if (index > 0) try writer.writeByte(',');
279
+ try writeGlyphAtlasEntryJson(entry, writer);
280
+ }
281
+ try writer.writeAll("],\"glyphAtlasActions\":[");
282
+ for (pass.glyph_atlas_actions, 0..) |action, index| {
283
+ if (index > 0) try writer.writeByte(',');
284
+ try writeGlyphAtlasCacheActionJson(action, writer);
285
+ }
286
+ try writer.writeAll("],\"textLayouts\":[");
287
+ for (pass.text_layouts, 0..) |layout, index| {
288
+ if (index > 0) try writer.writeByte(',');
289
+ try writeTextLayoutPlanJson(layout, writer);
290
+ }
291
+ try writer.writeAll("],\"textLayoutActions\":[");
292
+ for (pass.text_layout_actions, 0..) |action, index| {
293
+ if (index > 0) try writer.writeByte(',');
294
+ try writeTextLayoutCacheActionJson(action, writer);
295
+ }
296
+ try writer.writeAll("]}");
297
+ }
298
+
299
+ pub fn writeCanvasGpuPacketJson(packet: CanvasGpuPacket, writer: anytype) !void {
300
+ try writer.print(
301
+ "{{\"frameIndex\":{d},\"timestampNs\":{d},\"surfaceWidth\":{d},\"surfaceHeight\":{d},\"scale\":{d},\"loadAction\":",
302
+ .{ packet.frame_index, packet.timestamp_ns, packet.surface_size.width, packet.surface_size.height, packet.scale },
303
+ );
304
+ try json.writeString(writer, @tagName(packet.load_action));
305
+ try writer.writeAll(",\"requiresRender\":");
306
+ try writer.writeAll(if (packet.requiresRender()) "true" else "false");
307
+ try writer.writeAll(",\"scissorBounds\":");
308
+ try writeOptionalRectJson(packet.scissor, writer);
309
+ try writer.print(
310
+ ",\"commandCount\":{d},\"cacheActionCount\":{d},\"cachedResourceCommandCount\":{d},\"unsupportedCommandCount\":{d}",
311
+ .{ packet.commandCount(), packet.cacheActionCount(), packet.cachedResourceCommandCount(), packet.unsupported_command_count },
312
+ );
313
+ try writer.writeAll(",\"representable\":");
314
+ try writer.writeAll(if (packet.fullyRepresentable()) "true" else "false");
315
+ try writer.writeAll(",\"images\":[");
316
+ for (packet.images, 0..) |image, index| {
317
+ if (index > 0) try writer.writeByte(',');
318
+ try writeRenderImagePacketJson(image, writer);
319
+ }
320
+ try writer.writeAll("],\"imageActions\":[");
321
+ for (packet.image_actions, 0..) |action, index| {
322
+ if (index > 0) try writer.writeByte(',');
323
+ try writeRenderImageCacheActionJson(action, writer);
324
+ }
325
+ try writer.writeAll("],\"commands\":[");
326
+ for (packet.commands, 0..) |command, index| {
327
+ if (index > 0) try writer.writeByte(',');
328
+ try writeCanvasGpuCommandJson(command, writer);
329
+ }
330
+ try writer.writeAll("]}");
331
+ }
332
+
333
+ fn writeCanvasGpuCommandJson(command: CanvasGpuCommand, writer: anytype) !void {
334
+ try writer.print("{{\"index\":{d},\"id\":", .{command.command_index});
335
+ try writeOptionalObjectIdJson(command.id, writer);
336
+ try writer.writeAll(",\"kind\":");
337
+ try json.writeString(writer, @tagName(command.kind));
338
+ try writer.writeAll(",\"pipeline\":");
339
+ if (command.pipeline) |pipeline| {
340
+ try json.writeString(writer, @tagName(pipeline));
341
+ } else {
342
+ try writer.writeAll("null");
343
+ }
344
+ try writer.writeAll(",\"bounds\":");
345
+ try writeRectJson(command.bounds, writer);
346
+ try writer.writeAll(",\"shape\":");
347
+ try writeCanvasGpuShapeJson(command.shape, writer);
348
+ try writer.writeAll(",\"paint\":");
349
+ try writeCanvasGpuPaintJson(command.paint, writer);
350
+ try writer.print(",\"strokeWidth\":{d}", .{command.stroke_width});
351
+ try writer.writeAll(",\"image\":");
352
+ try writeCanvasGpuImageJson(command.image, writer);
353
+ try writer.writeAll(",\"text\":");
354
+ try writeCanvasGpuTextJson(command.text, writer);
355
+ try writer.writeAll(",\"effect\":");
356
+ try writeCanvasGpuEffectJson(command.effect, writer);
357
+ try writer.writeAll(",\"clip\":");
358
+ try writeOptionalRectJson(command.clip, writer);
359
+ try writer.print(",\"opacity\":{d},\"transform\":", .{command.opacity});
360
+ try writeAffineJson(command.transform, writer);
361
+ try writer.writeAll(",\"usesPathGeometry\":");
362
+ try writer.writeAll(if (command.uses_path_geometry) "true" else "false");
363
+ try writer.writeAll(",\"usesImage\":");
364
+ try writer.writeAll(if (command.uses_image) "true" else "false");
365
+ try writer.writeAll(",\"usesResource\":");
366
+ try writer.writeAll(if (command.uses_resource) "true" else "false");
367
+ try writer.writeAll(",\"usesVisualEffect\":");
368
+ try writer.writeAll(if (command.uses_visual_effect) "true" else "false");
369
+ try writer.writeAll(",\"usesGlyphAtlas\":");
370
+ try writer.writeAll(if (command.uses_glyph_atlas) "true" else "false");
371
+ try writer.writeAll(",\"usesTextLayout\":");
372
+ try writer.writeAll(if (command.uses_text_layout) "true" else "false");
373
+ try writer.writeByte('}');
374
+ }
375
+
376
+ fn writeCanvasGpuShapeJson(shape: CanvasGpuShape, writer: anytype) !void {
377
+ switch (shape) {
378
+ .none => try writer.writeAll("null"),
379
+ .rect => |rect| {
380
+ try writer.writeAll("{\"kind\":\"rect\",\"rect\":");
381
+ try writeRectJson(rect, writer);
382
+ try writer.writeByte('}');
383
+ },
384
+ .rounded_rect => |rounded_rect| {
385
+ try writer.writeAll("{\"kind\":\"rounded_rect\",\"rect\":");
386
+ try writeRectJson(rounded_rect.rect, writer);
387
+ try writer.writeAll(",\"radius\":");
388
+ try writeRadiusJson(rounded_rect.radius, writer);
389
+ try writer.writeByte('}');
390
+ },
391
+ .stroke_rect => |stroke_rect| {
392
+ try writer.writeAll("{\"kind\":\"stroke_rect\",\"rect\":");
393
+ try writeRectJson(stroke_rect.rect, writer);
394
+ try writer.writeAll(",\"radius\":");
395
+ try writeRadiusJson(stroke_rect.radius, writer);
396
+ try writer.print(",\"width\":{d}}}", .{stroke_rect.width});
397
+ },
398
+ .line => |line| {
399
+ try writer.writeAll("{\"kind\":\"line\",\"from\":");
400
+ try writePointJson(line.from, writer);
401
+ try writer.writeAll(",\"to\":");
402
+ try writePointJson(line.to, writer);
403
+ try writer.print(",\"width\":{d}}}", .{line.width});
404
+ },
405
+ .path => |path| {
406
+ try writer.writeAll("{\"kind\":\"path\",\"path\":");
407
+ try writePathJson(path, writer);
408
+ try writer.writeByte('}');
409
+ },
410
+ }
411
+ }
412
+
413
+ fn writeCanvasGpuPaintJson(paint: CanvasGpuPaint, writer: anytype) !void {
414
+ switch (paint) {
415
+ .none => try writer.writeAll("null"),
416
+ .color => |color| {
417
+ try writer.writeAll("{\"kind\":\"color\",\"color\":");
418
+ try writeColorJson(color, writer);
419
+ try writer.writeByte('}');
420
+ },
421
+ .linear_gradient => |gradient| {
422
+ try writer.writeAll("{\"kind\":\"linear_gradient\",\"start\":");
423
+ try writePointJson(gradient.start, writer);
424
+ try writer.writeAll(",\"end\":");
425
+ try writePointJson(gradient.end, writer);
426
+ try writer.writeAll(",\"stops\":[");
427
+ for (gradient.stops, 0..) |stop, index| {
428
+ if (index > 0) try writer.writeByte(',');
429
+ try writer.print("{{\"offset\":{d},\"color\":", .{stop.offset});
430
+ try writeColorJson(stop.color, writer);
431
+ try writer.writeByte('}');
432
+ }
433
+ try writer.writeAll("]}");
434
+ },
435
+ }
436
+ }
437
+
438
+ fn writeCanvasGpuImageJson(image: ?CanvasGpuImage, writer: anytype) !void {
439
+ const value = image orelse {
440
+ try writer.writeAll("null");
441
+ return;
442
+ };
443
+ try writer.print("{{\"image\":{d},\"src\":", .{value.image_id});
444
+ try writeOptionalRectJson(value.src, writer);
445
+ try writer.writeAll(",\"dst\":");
446
+ try writeRectJson(value.dst, writer);
447
+ try writer.print(",\"opacity\":{d},\"fit\":", .{value.opacity});
448
+ try json.writeString(writer, @tagName(value.fit));
449
+ try writer.writeAll(",\"sampling\":");
450
+ try json.writeString(writer, @tagName(value.sampling));
451
+ // Zero radius is omitted so image payloads without the rounded mask
452
+ // stay byte-identical to the pre-radius wire format.
453
+ if (radiusIsSet(value.radius)) {
454
+ try writer.writeAll(",\"radius\":");
455
+ try writeRadiusJson(value.radius, writer);
456
+ }
457
+ try writer.writeByte('}');
458
+ }
459
+
460
+ fn radiusIsSet(radius: Radius) bool {
461
+ return radius.top_left > 0 or radius.top_right > 0 or
462
+ radius.bottom_right > 0 or radius.bottom_left > 0;
463
+ }
464
+
465
+ fn writeCanvasGpuTextJson(text: ?CanvasGpuText, writer: anytype) !void {
466
+ const value = text orelse {
467
+ try writer.writeAll("null");
468
+ return;
469
+ };
470
+ try writer.print("{{\"font\":{d},\"size\":{d},\"origin\":", .{ value.font_id, value.size });
471
+ try writePointJson(value.origin, writer);
472
+ try writer.writeAll(",\"color\":");
473
+ try writeColorJson(value.color, writer);
474
+ try writer.writeAll(",\"text\":");
475
+ try json.writeString(writer, value.text);
476
+ try writer.writeAll(",\"glyphs\":");
477
+ try writeGlyphsJson(value.glyphs, writer);
478
+ try writer.writeAll(",\"layout\":");
479
+ if (value.text_layout) |options| {
480
+ try writeTextLayoutOptionsJson(options, writer);
481
+ try writer.writeAll(",\"lines\":");
482
+ try writeCanvasGpuTextLinesJson(value, options, writer);
483
+ } else {
484
+ try writer.writeAll("null");
485
+ }
486
+ try writer.writeByte('}');
487
+ }
488
+
489
+ /// Line budget for packet text lines; matches the reference renderer's
490
+ /// `max_reference_text_layout_lines` so both paths degrade at the same
491
+ /// depth.
492
+ const max_packet_text_layout_lines: usize = 64;
493
+
494
+ /// The engine's measured line breaks for a packet text command. The packet
495
+ /// host draws these lines verbatim instead of re-breaking the text with its
496
+ /// own line breaker, so drawn line breaks can never disagree with the
497
+ /// layout that measured the box — host-side re-wrapping broke tight
498
+ /// intrinsic single-line boxes mid-word. Uses the same
499
+ /// `layoutTextRun` the reference renderer and selection geometry draw from,
500
+ /// including the injected measure provider carried by the layout options.
501
+ /// Both packet encodings (JSON and binary) draw from this one layout so
502
+ /// they can never disagree on line breaks. Returns null when the run
503
+ /// exceeds the line budget, which keeps the host's legacy wrapping
504
+ /// fallback.
505
+ fn packetTextLayout(value: CanvasGpuText, options: TextLayoutOptions, lines: []TextLine) ?text_model.TextLayout {
506
+ return text_model.layoutTextRun(.{
507
+ .font_id = value.font_id,
508
+ .size = value.size,
509
+ .origin = value.origin,
510
+ .color = value.color,
511
+ .text = value.text,
512
+ .glyphs = value.glyphs,
513
+ .text_layout = options,
514
+ }, options, lines) catch null;
515
+ }
516
+
517
+ fn writeCanvasGpuTextLinesJson(value: CanvasGpuText, options: TextLayoutOptions, writer: anytype) !void {
518
+ var lines: [max_packet_text_layout_lines]TextLine = undefined;
519
+ const layout = packetTextLayout(value, options, &lines) orelse {
520
+ try writer.writeAll("null");
521
+ return;
522
+ };
523
+ try writer.writeByte('[');
524
+ for (layout.lines, 0..) |line, index| {
525
+ if (index > 0) try writer.writeByte(',');
526
+ // Elided lines ship their painted bytes — kept prefix plus the
527
+ // ellipsis — so hosts that draw packet lines verbatim ink
528
+ // exactly the extent the engine measured, with no host-side
529
+ // elision logic to drift.
530
+ const start = @min(line.text_start, value.text.len);
531
+ const end = @min(value.text.len, start + line.paintedTextLen());
532
+ try writer.print("{{\"x\":{d},\"baseline\":{d},\"text\":", .{ line.bounds.x, line.baseline });
533
+ try json.writeStringParts(writer, &.{ value.text[start..end], packetLineEllipsis(line) });
534
+ try writer.writeByte('}');
535
+ }
536
+ try writer.writeByte(']');
537
+ }
538
+
539
+ /// The trailing marker appended to an elided packet line's text (empty
540
+ /// when the line fits or the box cannot even hold the marker).
541
+ fn packetLineEllipsis(line: TextLine) []const u8 {
542
+ return if (line.hasEllipsis()) text_model.text_ellipsis else "";
543
+ }
544
+
545
+ fn writeCanvasGpuEffectJson(effect: CanvasGpuEffect, writer: anytype) !void {
546
+ switch (effect) {
547
+ .none => try writer.writeAll("null"),
548
+ .shadow => |shadow| {
549
+ try writer.writeAll("{\"kind\":\"shadow\",\"rect\":");
550
+ try writeRectJson(shadow.rect, writer);
551
+ try writer.writeAll(",\"radius\":");
552
+ try writeRadiusJson(shadow.radius, writer);
553
+ try writer.print(",\"offset\":[{d},{d}],\"blur\":{d},\"spread\":{d},\"color\":", .{ shadow.offset.dx, shadow.offset.dy, shadow.blur, shadow.spread });
554
+ try writeColorJson(shadow.color, writer);
555
+ try writer.writeByte('}');
556
+ },
557
+ .blur => |blur| {
558
+ try writer.writeAll("{\"kind\":\"blur\",\"rect\":");
559
+ try writeRectJson(blur.rect, writer);
560
+ try writer.print(",\"radius\":{d}}}", .{blur.radius});
561
+ },
562
+ }
563
+ }
564
+
565
+ fn writeRenderCommandJson(command: RenderCommand, index: usize, writer: anytype) !void {
566
+ try writer.print("{{\"index\":{d},\"id\":", .{index});
567
+ try writeOptionalObjectIdJson(command.id, writer);
568
+ try writer.print(",\"opacity\":{d},\"clip\":", .{command.opacity});
569
+ try writeOptionalRectJson(command.clip, writer);
570
+ try writer.writeAll(",\"transform\":");
571
+ try writeAffineJson(command.transform, writer);
572
+ try writer.writeAll(",\"localBounds\":");
573
+ try writeRectJson(command.local_bounds, writer);
574
+ try writer.writeAll(",\"bounds\":");
575
+ try writeRectJson(command.bounds, writer);
576
+ try writer.writeAll(",\"command\":");
577
+ try writeCommandJson(command.command, writer);
578
+ try writer.writeByte('}');
579
+ }
580
+
581
+ fn writeRenderBatchJson(batch: RenderBatch, writer: anytype) !void {
582
+ try writer.writeAll("{\"pipeline\":");
583
+ try json.writeString(writer, @tagName(batch.pipeline));
584
+ try writer.print(",\"commandStart\":{d},\"commandCount\":{d},\"opacity\":{d},\"clip\":", .{ batch.command_start, batch.command_count, batch.opacity });
585
+ try writeOptionalRectJson(batch.clip, writer);
586
+ try writer.writeAll(",\"bounds\":");
587
+ try writeRectJson(batch.bounds, writer);
588
+ try writer.writeByte('}');
589
+ }
590
+
591
+ fn writeRenderPipelineCacheActionJson(action: RenderPipelineCacheAction, writer: anytype) !void {
592
+ try writer.writeAll("{\"kind\":");
593
+ try json.writeString(writer, @tagName(action.kind));
594
+ try writer.writeAll(",\"pipeline\":");
595
+ try json.writeString(writer, @tagName(action.pipeline));
596
+ try writer.writeAll(",\"batchIndex\":");
597
+ try writeOptionalUsizeJson(action.batch_index, writer);
598
+ try writer.writeAll(",\"cacheIndex\":");
599
+ try writeOptionalUsizeJson(action.cache_index, writer);
600
+ try writer.writeByte('}');
601
+ }
602
+
603
+ fn writeRenderPathGeometryJson(geometry_plan: RenderPathGeometry, writer: anytype) !void {
604
+ try writer.writeAll("{\"kind\":");
605
+ try json.writeString(writer, @tagName(geometry_plan.kind));
606
+ try writer.print(",\"commandIndex\":{d},\"id\":", .{geometry_plan.command_index});
607
+ try writeOptionalObjectIdJson(geometry_plan.id, writer);
608
+ try writer.writeAll(",\"bounds\":");
609
+ try writeRectJson(geometry_plan.bounds, writer);
610
+ try writer.print(
611
+ ",\"elementCount\":{d},\"contourCount\":{d},\"lineSegmentCount\":{d},\"quadraticSegmentCount\":{d},\"cubicSegmentCount\":{d},\"flattenedSegmentCount\":{d},\"vertexCount\":{d},\"indexCount\":{d},\"strokeWidth\":{d},\"fingerprint\":{d}}}",
612
+ .{
613
+ geometry_plan.element_count,
614
+ geometry_plan.contour_count,
615
+ geometry_plan.line_segment_count,
616
+ geometry_plan.quadratic_segment_count,
617
+ geometry_plan.cubic_segment_count,
618
+ geometry_plan.flattened_segment_count,
619
+ geometry_plan.vertex_count,
620
+ geometry_plan.index_count,
621
+ geometry_plan.stroke_width,
622
+ geometry_plan.fingerprint,
623
+ },
624
+ );
625
+ }
626
+
627
+ fn writeRenderPathGeometryCacheActionJson(action: RenderPathGeometryCacheAction, writer: anytype) !void {
628
+ try writer.writeAll("{\"kind\":");
629
+ try json.writeString(writer, @tagName(action.kind));
630
+ try writer.writeAll(",\"key\":");
631
+ try writeRenderPathGeometryKeyJson(action.key, writer);
632
+ try writer.writeAll(",\"geometryIndex\":");
633
+ try writeOptionalUsizeJson(action.geometry_index, writer);
634
+ try writer.writeAll(",\"cacheIndex\":");
635
+ try writeOptionalUsizeJson(action.cache_index, writer);
636
+ try writer.writeByte('}');
637
+ }
638
+
639
+ fn writeRenderPathGeometryKeyJson(key: RenderPathGeometryKey, writer: anytype) !void {
640
+ try writer.writeAll("{\"kind\":");
641
+ try json.writeString(writer, @tagName(key.kind));
642
+ try writer.writeAll(",\"id\":");
643
+ try writeOptionalObjectIdJson(key.id, writer);
644
+ try writer.print(",\"commandIndex\":{d},\"fingerprint\":{d}}}", .{ key.command_index, key.fingerprint });
645
+ }
646
+
647
+ fn writeRenderImageJson(image: RenderImage, writer: anytype) !void {
648
+ try writer.print("{{\"imageId\":{d},\"commandIndex\":{d},\"id\":", .{ image.image_id, image.command_index });
649
+ try writeOptionalObjectIdJson(image.id, writer);
650
+ try writer.print(",\"drawCount\":{d},\"bounds\":", .{image.draw_count});
651
+ try writeRectJson(image.bounds, writer);
652
+ try writer.print(",\"width\":{d},\"height\":{d},\"pixelByteLength\":{d},\"fingerprint\":{d}}}", .{ image.width, image.height, image.pixels.len, image.fingerprint });
653
+ }
654
+
655
+ /// Packet images are references, never payloads: id + dimensions +
656
+ /// content fingerprint. The pixel bytes travel out-of-band through the
657
+ /// platform's binary image-upload side-channel
658
+ /// (`PlatformServices.uploadGpuSurfaceImage`), so a registered image can
659
+ /// never push a frame's packet JSON over the transport bound (which used
660
+ /// to evict the whole frame to the software pixel path).
661
+ fn writeRenderImagePacketJson(image: RenderImage, writer: anytype) !void {
662
+ try writer.print("{{\"imageId\":{d},\"commandIndex\":{d},\"id\":", .{ image.image_id, image.command_index });
663
+ try writeOptionalObjectIdJson(image.id, writer);
664
+ try writer.print(",\"drawCount\":{d},\"bounds\":", .{image.draw_count});
665
+ try writeRectJson(image.bounds, writer);
666
+ try writer.print(",\"width\":{d},\"height\":{d},\"fingerprint\":{d}}}", .{ image.width, image.height, image.fingerprint });
667
+ }
668
+
669
+ fn writeRenderImageCacheActionJson(action: RenderImageCacheAction, writer: anytype) !void {
670
+ try writer.writeAll("{\"kind\":");
671
+ try json.writeString(writer, @tagName(action.kind));
672
+ try writer.writeAll(",\"key\":");
673
+ try writeRenderImageKeyJson(action.key, writer);
674
+ try writer.writeAll(",\"imageIndex\":");
675
+ try writeOptionalUsizeJson(action.image_index, writer);
676
+ try writer.writeAll(",\"cacheIndex\":");
677
+ try writeOptionalUsizeJson(action.cache_index, writer);
678
+ try writer.writeByte('}');
679
+ }
680
+
681
+ fn writeRenderImageKeyJson(key: RenderImageKey, writer: anytype) !void {
682
+ try writer.print("{{\"imageId\":{d},\"fingerprint\":{d}}}", .{ key.image_id, key.fingerprint });
683
+ }
684
+
685
+ fn writeRenderLayerJson(layer: RenderLayer, writer: anytype) !void {
686
+ try writer.print("{{\"commandStart\":{d},\"commandCount\":{d},\"id\":", .{ layer.command_start, layer.command_count });
687
+ try writeOptionalObjectIdJson(layer.id, writer);
688
+ try writer.writeAll(",\"bounds\":");
689
+ try writeRectJson(layer.bounds, writer);
690
+ try writer.print(",\"opacity\":{d},\"clip\":", .{layer.opacity});
691
+ try writeOptionalRectJson(layer.clip, writer);
692
+ try writer.writeAll(",\"transform\":");
693
+ try writeAffineJson(layer.transform, writer);
694
+ try writer.print(",\"fingerprint\":{d}}}", .{layer.fingerprint});
695
+ }
696
+
697
+ fn writeRenderLayerCacheActionJson(action: RenderLayerCacheAction, writer: anytype) !void {
698
+ try writer.writeAll("{\"kind\":");
699
+ try json.writeString(writer, @tagName(action.kind));
700
+ try writer.writeAll(",\"key\":");
701
+ try writeRenderLayerKeyJson(action.key, writer);
702
+ try writer.writeAll(",\"layerIndex\":");
703
+ try writeOptionalUsizeJson(action.layer_index, writer);
704
+ try writer.writeAll(",\"cacheIndex\":");
705
+ try writeOptionalUsizeJson(action.cache_index, writer);
706
+ try writer.writeByte('}');
707
+ }
708
+
709
+ fn writeRenderLayerKeyJson(key: RenderLayerKey, writer: anytype) !void {
710
+ try writer.writeAll("{\"id\":");
711
+ try writeOptionalObjectIdJson(key.id, writer);
712
+ try writer.print(",\"commandStart\":{d},\"fingerprint\":{d}}}", .{ key.command_start, key.fingerprint });
713
+ }
714
+
715
+ fn writeRenderResourceJson(resource: RenderResource, writer: anytype) !void {
716
+ try writer.writeAll("{\"kind\":");
717
+ try json.writeString(writer, @tagName(resource.kind));
718
+ try writer.print(",\"commandIndex\":{d},\"id\":", .{resource.command_index});
719
+ try writeOptionalObjectIdJson(resource.id, writer);
720
+ try writer.writeAll(",\"bounds\":");
721
+ try writeOptionalRectJson(resource.bounds, writer);
722
+ try writer.print(",\"imageId\":{d},\"fontId\":{d},\"gradientStopCount\":{d},\"glyphCount\":{d},\"textLen\":{d},\"fingerprint\":{d}}}", .{
723
+ resource.image_id,
724
+ resource.font_id,
725
+ resource.gradient_stop_count,
726
+ resource.glyph_count,
727
+ resource.text_len,
728
+ resource.fingerprint,
729
+ });
730
+ }
731
+
732
+ fn writeRenderResourceCacheActionJson(action: RenderResourceCacheAction, writer: anytype) !void {
733
+ try writer.writeAll("{\"kind\":");
734
+ try json.writeString(writer, @tagName(action.kind));
735
+ try writer.writeAll(",\"key\":");
736
+ try writeRenderResourceKeyJson(action.key, writer);
737
+ try writer.writeAll(",\"resourceIndex\":");
738
+ try writeOptionalUsizeJson(action.resource_index, writer);
739
+ try writer.writeAll(",\"cacheIndex\":");
740
+ try writeOptionalUsizeJson(action.cache_index, writer);
741
+ try writer.writeByte('}');
742
+ }
743
+
744
+ fn writeRenderResourceKeyJson(key: RenderResourceKey, writer: anytype) !void {
745
+ try writer.writeAll("{\"kind\":");
746
+ try json.writeString(writer, @tagName(key.kind));
747
+ try writer.writeAll(",\"id\":");
748
+ try writeOptionalObjectIdJson(key.id, writer);
749
+ try writer.print(",\"commandIndex\":{d},\"imageId\":{d},\"fontId\":{d},\"fingerprint\":{d}}}", .{ key.command_index, key.image_id, key.font_id, key.fingerprint });
750
+ }
751
+
752
+ fn writeVisualEffectJson(effect: VisualEffect, writer: anytype) !void {
753
+ try writer.writeAll("{\"kind\":");
754
+ try json.writeString(writer, @tagName(effect.kind));
755
+ try writer.print(",\"commandIndex\":{d},\"id\":", .{effect.command_index});
756
+ try writeOptionalObjectIdJson(effect.id, writer);
757
+ try writer.writeAll(",\"bounds\":");
758
+ try writeOptionalRectJson(effect.bounds, writer);
759
+ try writer.writeAll(",\"radius\":");
760
+ try writeRadiusJson(effect.radius, writer);
761
+ try writer.print(",\"offset\":[{d},{d}],\"blur\":{d},\"spread\":{d},\"fingerprint\":{d}}}", .{
762
+ effect.offset.dx,
763
+ effect.offset.dy,
764
+ effect.blur,
765
+ effect.spread,
766
+ effect.fingerprint,
767
+ });
768
+ }
769
+
770
+ fn writeVisualEffectCacheActionJson(action: VisualEffectCacheAction, writer: anytype) !void {
771
+ try writer.writeAll("{\"kind\":");
772
+ try json.writeString(writer, @tagName(action.kind));
773
+ try writer.writeAll(",\"key\":");
774
+ try writeVisualEffectKeyJson(action.key, writer);
775
+ try writer.writeAll(",\"effectIndex\":");
776
+ try writeOptionalUsizeJson(action.effect_index, writer);
777
+ try writer.writeAll(",\"cacheIndex\":");
778
+ try writeOptionalUsizeJson(action.cache_index, writer);
779
+ try writer.writeByte('}');
780
+ }
781
+
782
+ fn writeVisualEffectKeyJson(key: VisualEffectKey, writer: anytype) !void {
783
+ try writer.writeAll("{\"kind\":");
784
+ try json.writeString(writer, @tagName(key.kind));
785
+ try writer.writeAll(",\"id\":");
786
+ try writeOptionalObjectIdJson(key.id, writer);
787
+ try writer.print(",\"commandIndex\":{d},\"fingerprint\":{d}}}", .{ key.command_index, key.fingerprint });
788
+ }
789
+
790
+ fn writeGlyphAtlasEntryJson(entry: GlyphAtlasEntry, writer: anytype) !void {
791
+ try writer.print("{{\"key\":{{\"fontId\":{d},\"glyphId\":{d},\"size\":{d},\"subpixelX\":{d},\"subpixelY\":{d}}},\"commandIndex\":{d},\"glyphIndex\":{d}}}", .{
792
+ entry.key.font_id,
793
+ entry.key.glyph_id,
794
+ entry.key.size,
795
+ entry.key.subpixel_x,
796
+ entry.key.subpixel_y,
797
+ entry.command_index,
798
+ entry.glyph_index,
799
+ });
800
+ }
801
+
802
+ fn writeGlyphAtlasCacheActionJson(action: GlyphAtlasCacheAction, writer: anytype) !void {
803
+ try writer.writeAll("{\"kind\":");
804
+ try json.writeString(writer, @tagName(action.kind));
805
+ try writer.writeAll(",\"key\":");
806
+ try writeGlyphAtlasKeyJson(action.key, writer);
807
+ try writer.writeAll(",\"atlasIndex\":");
808
+ try writeOptionalUsizeJson(action.atlas_index, writer);
809
+ try writer.writeAll(",\"cacheIndex\":");
810
+ try writeOptionalUsizeJson(action.cache_index, writer);
811
+ try writer.writeByte('}');
812
+ }
813
+
814
+ fn writeGlyphAtlasKeyJson(key: GlyphAtlasKey, writer: anytype) !void {
815
+ try writer.print("{{\"fontId\":{d},\"glyphId\":{d},\"size\":{d},\"subpixelX\":{d},\"subpixelY\":{d}}}", .{
816
+ key.font_id,
817
+ key.glyph_id,
818
+ key.size,
819
+ key.subpixel_x,
820
+ key.subpixel_y,
821
+ });
822
+ }
823
+
824
+ fn writeTextLayoutPlanJson(plan: TextLayoutPlan, writer: anytype) !void {
825
+ try writer.writeAll("{\"key\":");
826
+ try writeTextLayoutKeyJson(plan.key, writer);
827
+ try writer.print(",\"lineCount\":{d},\"bounds\":", .{plan.lineCount()});
828
+ try writeOptionalRectJson(plan.layout.bounds, writer);
829
+ try writer.writeByte('}');
830
+ }
831
+
832
+ fn writeTextLayoutCacheActionJson(action: TextLayoutCacheAction, writer: anytype) !void {
833
+ try writer.writeAll("{\"kind\":");
834
+ try json.writeString(writer, @tagName(action.kind));
835
+ try writer.writeAll(",\"key\":");
836
+ try writeTextLayoutKeyJson(action.key, writer);
837
+ try writer.writeAll(",\"layoutIndex\":");
838
+ try writeOptionalUsizeJson(action.layout_index, writer);
839
+ try writer.writeAll(",\"cacheIndex\":");
840
+ try writeOptionalUsizeJson(action.cache_index, writer);
841
+ try writer.writeByte('}');
842
+ }
843
+
844
+ fn writeTextLayoutKeyJson(key: TextLayoutKey, writer: anytype) !void {
845
+ try writer.print("{{\"fontId\":{d},\"size\":{d},\"origin\":[{d},{d}],\"maxWidth\":{d},\"lineHeight\":{d},\"wrap\":", .{
846
+ key.font_id,
847
+ key.size,
848
+ key.origin.x,
849
+ key.origin.y,
850
+ key.max_width,
851
+ key.line_height,
852
+ });
853
+ try json.writeString(writer, @tagName(key.wrap));
854
+ try writer.writeAll(",\"align\":");
855
+ try json.writeString(writer, @tagName(key.alignment));
856
+ try writer.writeAll(",\"overflow\":");
857
+ try json.writeString(writer, @tagName(key.overflow));
858
+ try writer.print(",\"textLen\":{d},\"glyphCount\":{d},\"fingerprint\":{d}}}", .{
859
+ key.text_len,
860
+ key.glyph_count,
861
+ key.fingerprint,
862
+ });
863
+ }
864
+
865
+ fn writeOptionalRectJson(rect: ?geometry.RectF, writer: anytype) !void {
866
+ if (rect) |value| {
867
+ try writeRectJson(value, writer);
868
+ } else {
869
+ try writer.writeAll("null");
870
+ }
871
+ }
872
+
873
+ fn writeOptionalObjectIdJson(id: ?ObjectId, writer: anytype) !void {
874
+ if (id) |value| {
875
+ try writer.print("{d}", .{value});
876
+ } else {
877
+ try writer.writeAll("null");
878
+ }
879
+ }
880
+
881
+ fn writeOptionalUsizeJson(value: ?usize, writer: anytype) !void {
882
+ if (value) |number| {
883
+ try writer.print("{d}", .{number});
884
+ } else {
885
+ try writer.writeAll("null");
886
+ }
887
+ }
888
+
889
+ fn writeRectJson(rect: geometry.RectF, writer: anytype) !void {
890
+ try writer.print("[{d},{d},{d},{d}]", .{ rect.x, rect.y, rect.width, rect.height });
891
+ }
892
+
893
+ fn writePointJson(point: geometry.PointF, writer: anytype) !void {
894
+ try writer.print("[{d},{d}]", .{ point.x, point.y });
895
+ }
896
+
897
+ fn writeColorJson(color: Color, writer: anytype) !void {
898
+ try writer.print("[{d},{d},{d},{d}]", .{ color.r, color.g, color.b, color.a });
899
+ }
900
+
901
+ fn writeRadiusJson(radius: Radius, writer: anytype) !void {
902
+ try writer.print("[{d},{d},{d},{d}]", .{ radius.top_left, radius.top_right, radius.bottom_right, radius.bottom_left });
903
+ }
904
+
905
+ fn writeAffineJson(matrix: Affine, writer: anytype) !void {
906
+ try writer.print("[{d},{d},{d},{d},{d},{d}]", .{ matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty });
907
+ }
908
+
909
+ fn writeFillJson(fill: Fill, writer: anytype) !void {
910
+ switch (fill) {
911
+ .color => |color| {
912
+ try writer.writeAll("{\"kind\":\"color\",\"color\":");
913
+ try writeColorJson(color, writer);
914
+ try writer.writeByte('}');
915
+ },
916
+ .linear_gradient => |gradient| {
917
+ try writer.writeAll("{\"kind\":\"linear_gradient\",\"start\":");
918
+ try writePointJson(gradient.start, writer);
919
+ try writer.writeAll(",\"end\":");
920
+ try writePointJson(gradient.end, writer);
921
+ try writer.writeAll(",\"stops\":[");
922
+ for (gradient.stops, 0..) |stop, index| {
923
+ if (index > 0) try writer.writeByte(',');
924
+ try writer.print("{{\"offset\":{d},\"color\":", .{stop.offset});
925
+ try writeColorJson(stop.color, writer);
926
+ try writer.writeByte('}');
927
+ }
928
+ try writer.writeAll("]}");
929
+ },
930
+ }
931
+ }
932
+
933
+ fn writeStrokeJson(stroke: Stroke, writer: anytype) !void {
934
+ try writer.writeAll("{\"width\":");
935
+ try writer.print("{d}", .{stroke.width});
936
+ try writer.writeAll(",\"fill\":");
937
+ try writeFillJson(stroke.fill, writer);
938
+ try writer.writeByte('}');
939
+ }
940
+
941
+ fn writePathJson(elements: []const PathElement, writer: anytype) !void {
942
+ try writer.writeByte('[');
943
+ for (elements, 0..) |element, index| {
944
+ if (index > 0) try writer.writeByte(',');
945
+ try writer.writeAll("{\"verb\":");
946
+ try json.writeString(writer, @tagName(element.verb));
947
+ try writer.writeAll(",\"points\":[");
948
+ const point_count: usize = switch (element.verb) {
949
+ .move_to, .line_to => 1,
950
+ .quad_to => 2,
951
+ .cubic_to => 3,
952
+ .close => 0,
953
+ };
954
+ for (element.points[0..point_count], 0..) |point, point_index| {
955
+ if (point_index > 0) try writer.writeByte(',');
956
+ try writePointJson(point, writer);
957
+ }
958
+ try writer.writeAll("]}");
959
+ }
960
+ try writer.writeByte(']');
961
+ }
962
+
963
+ fn writeGlyphsJson(glyphs: []const Glyph, writer: anytype) !void {
964
+ try writer.writeByte('[');
965
+ for (glyphs, 0..) |glyph, index| {
966
+ if (index > 0) try writer.writeByte(',');
967
+ try writer.print("{{\"id\":{d}", .{glyph.id});
968
+ if (glyph.font_id != 0) try writer.print(",\"font\":{d}", .{glyph.font_id});
969
+ try writer.print(",\"x\":{d},\"y\":{d},\"advance\":{d}", .{ glyph.x, glyph.y, glyph.advance });
970
+ if (glyph.text_len != 0) try writer.print(",\"textStart\":{d},\"textLen\":{d}", .{ glyph.text_start, glyph.text_len });
971
+ try writer.writeByte('}');
972
+ }
973
+ try writer.writeByte(']');
974
+ }
975
+
976
+ // ---------------------------------------------------------------------------
977
+ // Compact binary gpu-surface packet encoding (wire format v3).
978
+ //
979
+ // The version this comment names, the `binary_packet_version` constant
980
+ // below, and the host decoder's spec comment (appkit_host.m) must agree;
981
+ // the `test-wire-format-version-prose` build check pins all three, so
982
+ // bumping the constant without updating the prose fails the suite.
983
+ //
984
+ // Little-endian, length-prefixed throughout, no field names, no decimal
985
+ // formatting, and no glyph arrays (the packet host draws text through the
986
+ // system text stack from the run's UTF-8 text plus the engine-measured
987
+ // lines, so glyph payloads — the bulk of a text-heavy JSON packet — never
988
+ // ride the wire). The AppKit host decoder
989
+ // (`NativeSdkPacketDictionaryFromBinary` in appkit_host.m) pins the same
990
+ // layout and tag tables independently; a disagreement fails the host
991
+ // decode loudly (refused present -> recorded fallback) instead of drawing
992
+ // garbage. Bump `binary_packet_version` on ANY layout change.
993
+ //
994
+ // v2 (from v1): every header carries a retained-state `generation`, every
995
+ // command rides behind an explicit retain `key` (its ObjectId, or a
996
+ // synthetic key for unkeyed commands), and a third load action `patch`
997
+ // (3) carries an edit script (evicts + keyed upserts + the full draw-order
998
+ // vector) against the host's retained command dictionary instead of the
999
+ // full command list. Generation 0 means "do not retain": the host draws
1000
+ // the frame but never answers a later patch from it.
1001
+ //
1002
+ // v3 (from v2): flags bit1 introduces an optional DIRTY RECT LIST after
1003
+ // the scissor — the exact rects the frame's edit script touches, so a
1004
+ // patch whose changes sit at opposite window corners repaints (and
1005
+ // re-uploads) two small rects instead of their bounding union. The
1006
+ // scissor stays the union of the list (hosts may honor either; pixels
1007
+ // outside every rect are unchanged by construction).
1008
+ //
1009
+ // Layout:
1010
+ // "NSGP" u8[4] | version u8 | load_action u8 (1 load / 2 clear /
1011
+ // 3 patch) | flags u8 (bit0 scissor, bit1 dirty rect list) | reserved u8
1012
+ // | generation u64 | [scissor f32[4]]
1013
+ // | [dirty_rect_count u32 | dirty rects f32[4][]]
1014
+ // | image_count u32 | images { image_id u64, fingerprint u64,
1015
+ // width u32, height u32 }
1016
+ // | image_action_count u32 | actions { kind u8 (0 upload / 1 retain /
1017
+ // 2 evict), key_image_id u64, key_fingerprint u64,
1018
+ // image_index u32 (0xFFFFFFFF = none) }
1019
+ // | load/clear: command_count u32 | commands { key u64, command (see
1020
+ // writeCanvasGpuCommandBinary) }
1021
+ // | patch: evict_count u32 | evict keys u64[]
1022
+ // | upsert_count u32 | upserts { key u64, command }
1023
+ // | order_count u32 | order keys u64[]
1024
+
1025
+ pub const binary_packet_magic = "NSGP";
1026
+ pub const binary_packet_version: u8 = 3;
1027
+
1028
+ /// Most dirty rects a patch header carries: enough to keep far-apart
1029
+ /// small changes (a switch plus a status line) from fusing into a
1030
+ /// window-sized union, few enough that host-side per-rect clears,
1031
+ /// clips, culls, and texture uploads stay O(1) per command.
1032
+ pub const max_binary_packet_dirty_rects: usize = 8;
1033
+
1034
+ /// Wire code for the `patch` load action (`CanvasRenderPassLoadAction`
1035
+ /// has no patch member — patches are a transport-level edit script, not
1036
+ /// a render pass the engine plans).
1037
+ pub const binary_packet_load_action_patch: u8 = 3;
1038
+
1039
+ const binary_image_index_none: u32 = 0xFFFF_FFFF;
1040
+
1041
+ const hash = @import("hash.zig");
1042
+
1043
+ /// Content fingerprint of a packet command: covers every field the binary
1044
+ /// command encoding serializes (measured text lines are a deterministic
1045
+ /// function of the hashed text inputs, so hashing the inputs is hashing
1046
+ /// the lines without paying `layoutTextRun` for unchanged runs — that
1047
+ /// skip IS the patch win on text-heavy views). Equal fingerprints mean
1048
+ /// byte-identical wire encodings; `canvas_frame_patch_tests.zig` pins
1049
+ /// field coverage so a new encoded field cannot silently stop
1050
+ /// invalidating patches.
1051
+ pub fn canvasGpuCommandFingerprint(command: CanvasGpuCommand) u64 {
1052
+ var h = hash.resourceHashTag("gpu-packet-command");
1053
+ h = hash.resourceHashU8(h, binaryCommandKindCode(command.kind));
1054
+ h = hash.resourceHashRect(h, command.bounds);
1055
+ h = hash.resourceHashF32(h, command.opacity);
1056
+ h = hash.resourceHashF32(h, command.stroke_width);
1057
+ h = hash.resourceHashOptionalObjectId(h, command.id);
1058
+ h = hash.resourceHashOptionalRect(h, command.clip);
1059
+ h = hash.resourceHashAffine(h, command.transform);
1060
+ switch (command.shape) {
1061
+ .none => h = hash.resourceHashU8(h, 0),
1062
+ .rect => |rect| h = hash.resourceHashRect(hash.resourceHashU8(h, 1), rect),
1063
+ .rounded_rect => |rounded_rect| {
1064
+ h = hash.resourceHashU8(h, 2);
1065
+ h = hash.resourceHashRect(h, rounded_rect.rect);
1066
+ h = hash.resourceHashRadius(h, rounded_rect.radius);
1067
+ },
1068
+ .stroke_rect => |stroke_rect| {
1069
+ h = hash.resourceHashU8(h, 3);
1070
+ h = hash.resourceHashRect(h, stroke_rect.rect);
1071
+ h = hash.resourceHashRadius(h, stroke_rect.radius);
1072
+ h = hash.resourceHashF32(h, stroke_rect.width);
1073
+ },
1074
+ .line => |line| {
1075
+ h = hash.resourceHashU8(h, 4);
1076
+ h = hash.resourceHashPoint(h, line.from);
1077
+ h = hash.resourceHashPoint(h, line.to);
1078
+ h = hash.resourceHashF32(h, line.width);
1079
+ },
1080
+ .path => |elements| h = hash.resourceHashPath(hash.resourceHashU8(h, 5), elements),
1081
+ }
1082
+ switch (command.paint) {
1083
+ .none => h = hash.resourceHashU8(h, 0),
1084
+ .color => |color| h = hash.resourceHashColor(hash.resourceHashU8(h, 1), color),
1085
+ .linear_gradient => |gradient| {
1086
+ h = hash.resourceHashU8(h, 2);
1087
+ h = hash.resourceHashPoint(h, gradient.start);
1088
+ h = hash.resourceHashPoint(h, gradient.end);
1089
+ h = hash.resourceHashUsize(h, gradient.stops.len);
1090
+ for (gradient.stops) |stop| {
1091
+ h = hash.resourceHashF32(h, stop.offset);
1092
+ h = hash.resourceHashColor(h, stop.color);
1093
+ }
1094
+ },
1095
+ }
1096
+ if (command.image) |image| {
1097
+ h = hash.resourceHashU8(h, 1);
1098
+ h = hash.resourceHashU64(h, image.image_id);
1099
+ h = hash.resourceHashOptionalRect(h, image.src);
1100
+ h = hash.resourceHashRect(h, image.dst);
1101
+ h = hash.resourceHashF32(h, image.opacity);
1102
+ h = hash.resourceHashEnum(h, @intFromEnum(image.fit));
1103
+ h = hash.resourceHashEnum(h, @intFromEnum(image.sampling));
1104
+ h = hash.resourceHashRadius(h, image.radius);
1105
+ } else {
1106
+ h = hash.resourceHashU8(h, 0);
1107
+ }
1108
+ if (command.text) |text| {
1109
+ h = hash.resourceHashU8(h, 1);
1110
+ h = hash.resourceHashU64(h, text.font_id);
1111
+ h = hash.resourceHashF32(h, text.size);
1112
+ h = hash.resourceHashPoint(h, text.origin);
1113
+ h = hash.resourceHashColor(h, text.color);
1114
+ h = hash.resourceHashBytes(h, text.text);
1115
+ h = hash.resourceHashUsize(h, text.glyphs.len);
1116
+ for (text.glyphs) |glyph| {
1117
+ h = hash.resourceHashU32(h, glyph.id);
1118
+ h = hash.resourceHashU64(h, glyph.font_id);
1119
+ h = hash.resourceHashF32(h, glyph.x);
1120
+ h = hash.resourceHashF32(h, glyph.y);
1121
+ h = hash.resourceHashF32(h, glyph.advance);
1122
+ h = hash.resourceHashUsize(h, glyph.text_start);
1123
+ h = hash.resourceHashUsize(h, glyph.text_len);
1124
+ }
1125
+ if (text.text_layout) |options| {
1126
+ h = hash.resourceHashU8(h, 1);
1127
+ h = hash.resourceHashF32(h, nonNegative(options.max_width));
1128
+ h = hash.resourceHashF32(h, nonNegative(options.line_height));
1129
+ h = hash.resourceHashEnum(h, @intFromEnum(options.wrap));
1130
+ h = hash.resourceHashEnum(h, @intFromEnum(options.alignment));
1131
+ // Default overflow stays out of the hash: fingerprints of
1132
+ // runs the elision default never touches keep their pinned
1133
+ // values; clip-opted runs hash apart from elided twins.
1134
+ if (options.overflow != .ellipsis) {
1135
+ h = hash.resourceHashEnum(hash.resourceHashBytes(h, "text_overflow"), @intFromEnum(options.overflow));
1136
+ }
1137
+ } else {
1138
+ h = hash.resourceHashU8(h, 0);
1139
+ }
1140
+ } else {
1141
+ h = hash.resourceHashU8(h, 0);
1142
+ }
1143
+ switch (command.effect) {
1144
+ .none => h = hash.resourceHashU8(h, 0),
1145
+ .shadow => |shadow| {
1146
+ h = hash.resourceHashU8(h, 1);
1147
+ h = hash.resourceHashRect(h, shadow.rect);
1148
+ h = hash.resourceHashRadius(h, shadow.radius);
1149
+ h = hash.resourceHashF32(h, shadow.offset.dx);
1150
+ h = hash.resourceHashF32(h, shadow.offset.dy);
1151
+ h = hash.resourceHashF32(h, shadow.blur);
1152
+ h = hash.resourceHashF32(h, shadow.spread);
1153
+ h = hash.resourceHashColor(h, shadow.color);
1154
+ },
1155
+ .blur => |blur| {
1156
+ h = hash.resourceHashU8(h, 2);
1157
+ h = hash.resourceHashRect(h, blur.rect);
1158
+ h = hash.resourceHashF32(h, blur.radius);
1159
+ },
1160
+ }
1161
+ return h;
1162
+ }
1163
+
1164
+ /// Retain key for a packet command: the widget-stamped ObjectId when
1165
+ /// present, else a synthetic key derived from the command's render-plan
1166
+ /// index + content fingerprint. Unkeyed commands therefore degrade to
1167
+ /// evict+insert whenever anything above them shifts their index — correct,
1168
+ /// just not cheap; stamping ids is the fast path.
1169
+ pub fn canvasGpuPacketCommandKey(command: CanvasGpuCommand, fingerprint: u64) u64 {
1170
+ if (command.id) |id| return id;
1171
+ var h = hash.resourceHashTag("gpu-packet-synthetic-key");
1172
+ h = hash.resourceHashUsize(h, command.command_index);
1173
+ h = hash.resourceHashU64(h, fingerprint);
1174
+ return h;
1175
+ }
1176
+
1177
+ /// Shared v2 packet header + image sections; `load_action_code` is the
1178
+ /// wire code (1 load / 2 clear / 3 patch).
1179
+ pub fn writeCanvasGpuPacketBinaryHeader(
1180
+ load_action_code: u8,
1181
+ generation: u64,
1182
+ scissor: ?geometry.RectF,
1183
+ dirty_rects: []const geometry.RectF,
1184
+ images: []const RenderImage,
1185
+ image_actions: []const RenderImageCacheAction,
1186
+ writer: anytype,
1187
+ ) !void {
1188
+ try writer.writeAll(binary_packet_magic);
1189
+ try writer.writeByte(binary_packet_version);
1190
+ try writer.writeByte(load_action_code);
1191
+ var flags: u8 = 0;
1192
+ if (scissor != null) flags |= 0x01;
1193
+ // The dirty rect list refines a scissor; without one it means
1194
+ // nothing, so it never rides alone.
1195
+ const write_dirty_rects = scissor != null and dirty_rects.len > 0 and dirty_rects.len <= max_binary_packet_dirty_rects;
1196
+ if (write_dirty_rects) flags |= 0x02;
1197
+ try writer.writeByte(flags);
1198
+ try writer.writeByte(0);
1199
+ try writer.writeInt(u64, generation, .little);
1200
+ if (scissor) |rect| try writeBinaryRect(rect, writer);
1201
+ if (write_dirty_rects) {
1202
+ try writer.writeInt(u32, @intCast(dirty_rects.len), .little);
1203
+ for (dirty_rects) |rect| try writeBinaryRect(rect, writer);
1204
+ }
1205
+
1206
+ try writer.writeInt(u32, @intCast(images.len), .little);
1207
+ for (images) |image| {
1208
+ try writer.writeInt(u64, image.image_id, .little);
1209
+ try writer.writeInt(u64, image.fingerprint, .little);
1210
+ try writer.writeInt(u32, @intCast(image.width), .little);
1211
+ try writer.writeInt(u32, @intCast(image.height), .little);
1212
+ }
1213
+
1214
+ try writer.writeInt(u32, @intCast(image_actions.len), .little);
1215
+ for (image_actions) |action| {
1216
+ try writer.writeByte(switch (action.kind) {
1217
+ .upload => 0,
1218
+ .retain => 1,
1219
+ .evict => 2,
1220
+ });
1221
+ try writer.writeInt(u64, action.key.image_id, .little);
1222
+ try writer.writeInt(u64, action.key.fingerprint, .little);
1223
+ try writer.writeInt(u32, if (action.image_index) |index| @intCast(index) else binary_image_index_none, .little);
1224
+ }
1225
+ }
1226
+
1227
+ /// One retained command: retain key + the v1 command encoding.
1228
+ pub fn writeCanvasGpuCommandBinaryKeyed(key: u64, command: CanvasGpuCommand, writer: anytype) !void {
1229
+ try writer.writeInt(u64, key, .little);
1230
+ try writeCanvasGpuCommandBinary(command, writer);
1231
+ }
1232
+
1233
+ pub fn writeCanvasGpuPacketBinary(packet: CanvasGpuPacket, writer: anytype) !void {
1234
+ try writeCanvasGpuPacketBinaryHeader(switch (packet.load_action) {
1235
+ .skip => 0,
1236
+ .load => 1,
1237
+ .clear => 2,
1238
+ }, packet.generation, packet.scissor, &.{}, packet.images, packet.image_actions, writer);
1239
+
1240
+ try writer.writeInt(u32, @intCast(packet.commands.len), .little);
1241
+ for (packet.commands) |command| {
1242
+ const fingerprint = canvasGpuCommandFingerprint(command);
1243
+ try writeCanvasGpuCommandBinaryKeyed(canvasGpuPacketCommandKey(command, fingerprint), command, writer);
1244
+ }
1245
+ }
1246
+
1247
+ const binary_command_flag_id: u8 = 0x01;
1248
+ const binary_command_flag_clip: u8 = 0x02;
1249
+ const binary_command_flag_transform: u8 = 0x04;
1250
+ const binary_command_flag_shape: u8 = 0x08;
1251
+ const binary_command_flag_paint: u8 = 0x10;
1252
+ const binary_command_flag_image: u8 = 0x20;
1253
+ const binary_command_flag_text: u8 = 0x40;
1254
+ const binary_command_flag_effect: u8 = 0x80;
1255
+
1256
+ /// Command layout: kind u8 | flags u8 | bounds f32[4] | opacity f32
1257
+ /// | stroke_width f32 | [id u64] | [clip f32[4]] | [transform f32[6]]
1258
+ /// | [shape] | [paint] | [image] | [text] | [effect] — each optional
1259
+ /// section present exactly when its flag bit is set. The identity
1260
+ /// transform is elided (the flag doubles as "non-identity").
1261
+ fn writeCanvasGpuCommandBinary(command: CanvasGpuCommand, writer: anytype) !void {
1262
+ try writer.writeByte(binaryCommandKindCode(command.kind));
1263
+ var flags: u8 = 0;
1264
+ if (command.id != null) flags |= binary_command_flag_id;
1265
+ if (command.clip != null) flags |= binary_command_flag_clip;
1266
+ const identity_transform = command.transform.a == 1 and command.transform.b == 0 and
1267
+ command.transform.c == 0 and command.transform.d == 1 and
1268
+ command.transform.tx == 0 and command.transform.ty == 0;
1269
+ if (!identity_transform) flags |= binary_command_flag_transform;
1270
+ if (command.shape != .none) flags |= binary_command_flag_shape;
1271
+ if (command.paint != .none) flags |= binary_command_flag_paint;
1272
+ if (command.image != null) flags |= binary_command_flag_image;
1273
+ if (command.text != null) flags |= binary_command_flag_text;
1274
+ if (command.effect != .none) flags |= binary_command_flag_effect;
1275
+ try writer.writeByte(flags);
1276
+ try writeBinaryRect(command.bounds, writer);
1277
+ try writeBinaryF32(command.opacity, writer);
1278
+ try writeBinaryF32(command.stroke_width, writer);
1279
+ if (command.id) |id| try writer.writeInt(u64, id, .little);
1280
+ if (command.clip) |clip| try writeBinaryRect(clip, writer);
1281
+ if (!identity_transform) try writeBinaryAffine(command.transform, writer);
1282
+ if (command.shape != .none) try writeBinaryShape(command.shape, writer);
1283
+ if (command.paint != .none) try writeBinaryPaint(command.paint, writer);
1284
+ if (command.image) |image| try writeBinaryImage(image, writer);
1285
+ if (command.text) |text| try writeBinaryText(text, writer);
1286
+ if (command.effect != .none) try writeBinaryEffect(command.effect, writer);
1287
+ }
1288
+
1289
+ /// Stable wire codes for the command kind — pinned independently of the
1290
+ /// Zig enum's declaration order so a reordered enum cannot silently
1291
+ /// change the wire format. `.unsupported` maps to 255: hosts refuse it,
1292
+ /// and the runtime never presents unrepresentable packets anyway.
1293
+ fn binaryCommandKindCode(kind: gpu_model.CanvasGpuCommandKind) u8 {
1294
+ return switch (kind) {
1295
+ .fill_rect_solid => 0,
1296
+ .fill_rect_gradient => 1,
1297
+ .fill_rounded_rect_solid => 2,
1298
+ .fill_rounded_rect_gradient => 3,
1299
+ .stroke_rect_solid => 4,
1300
+ .stroke_rect_gradient => 5,
1301
+ .draw_line_solid => 6,
1302
+ .draw_line_gradient => 7,
1303
+ .fill_path => 8,
1304
+ .stroke_path => 9,
1305
+ .draw_image => 10,
1306
+ .draw_text => 11,
1307
+ .shadow => 12,
1308
+ .blur => 13,
1309
+ .unsupported => 255,
1310
+ };
1311
+ }
1312
+
1313
+ /// Shape: tag u8 (1 rect / 2 rounded_rect / 3 stroke_rect / 4 line /
1314
+ /// 5 path) + payload.
1315
+ fn writeBinaryShape(shape: CanvasGpuShape, writer: anytype) !void {
1316
+ switch (shape) {
1317
+ .none => unreachable, // callers gate on the shape flag
1318
+ .rect => |rect| {
1319
+ try writer.writeByte(1);
1320
+ try writeBinaryRect(rect, writer);
1321
+ },
1322
+ .rounded_rect => |rounded_rect| {
1323
+ try writer.writeByte(2);
1324
+ try writeBinaryRect(rounded_rect.rect, writer);
1325
+ try writeBinaryRadius(rounded_rect.radius, writer);
1326
+ },
1327
+ .stroke_rect => |stroke_rect| {
1328
+ try writer.writeByte(3);
1329
+ try writeBinaryRect(stroke_rect.rect, writer);
1330
+ try writeBinaryRadius(stroke_rect.radius, writer);
1331
+ try writeBinaryF32(stroke_rect.width, writer);
1332
+ },
1333
+ .line => |line| {
1334
+ try writer.writeByte(4);
1335
+ try writeBinaryPoint(line.from, writer);
1336
+ try writeBinaryPoint(line.to, writer);
1337
+ try writeBinaryF32(line.width, writer);
1338
+ },
1339
+ .path => |elements| {
1340
+ try writer.writeByte(5);
1341
+ try writer.writeInt(u32, @intCast(elements.len), .little);
1342
+ for (elements) |element| {
1343
+ const verb_code: u8 = switch (element.verb) {
1344
+ .move_to => 0,
1345
+ .line_to => 1,
1346
+ .quad_to => 2,
1347
+ .cubic_to => 3,
1348
+ .close => 4,
1349
+ };
1350
+ try writer.writeByte(verb_code);
1351
+ const point_count: usize = switch (element.verb) {
1352
+ .move_to, .line_to => 1,
1353
+ .quad_to => 2,
1354
+ .cubic_to => 3,
1355
+ .close => 0,
1356
+ };
1357
+ for (element.points[0..point_count]) |point| {
1358
+ try writeBinaryPoint(point, writer);
1359
+ }
1360
+ }
1361
+ },
1362
+ }
1363
+ }
1364
+
1365
+ /// Paint: tag u8 (1 color / 2 linear_gradient) + payload.
1366
+ fn writeBinaryPaint(paint: CanvasGpuPaint, writer: anytype) !void {
1367
+ switch (paint) {
1368
+ .none => unreachable, // callers gate on the paint flag
1369
+ .color => |color| {
1370
+ try writer.writeByte(1);
1371
+ try writeBinaryColor(color, writer);
1372
+ },
1373
+ .linear_gradient => |gradient| {
1374
+ try writer.writeByte(2);
1375
+ try writeBinaryPoint(gradient.start, writer);
1376
+ try writeBinaryPoint(gradient.end, writer);
1377
+ try writer.writeInt(u32, @intCast(gradient.stops.len), .little);
1378
+ for (gradient.stops) |stop| {
1379
+ try writeBinaryF32(stop.offset, writer);
1380
+ try writeBinaryColor(stop.color, writer);
1381
+ }
1382
+ },
1383
+ }
1384
+ }
1385
+
1386
+ /// Image draw: image_id u64 | has_src u8 [src f32[4]] | dst f32[4]
1387
+ /// | opacity f32 | fit u8 (0 stretch / 1 contain / 2 cover)
1388
+ /// | sampling u8 (0 nearest / 1 linear) | radius f32[4].
1389
+ fn writeBinaryImage(image: CanvasGpuImage, writer: anytype) !void {
1390
+ try writer.writeInt(u64, image.image_id, .little);
1391
+ if (image.src) |src| {
1392
+ try writer.writeByte(1);
1393
+ try writeBinaryRect(src, writer);
1394
+ } else {
1395
+ try writer.writeByte(0);
1396
+ }
1397
+ try writeBinaryRect(image.dst, writer);
1398
+ try writeBinaryF32(image.opacity, writer);
1399
+ try writer.writeByte(switch (image.fit) {
1400
+ .stretch => 0,
1401
+ .contain => 1,
1402
+ .cover => 2,
1403
+ });
1404
+ try writer.writeByte(switch (image.sampling) {
1405
+ .nearest => 0,
1406
+ .linear => 1,
1407
+ });
1408
+ try writeBinaryRadius(image.radius, writer);
1409
+ }
1410
+
1411
+ /// Text draw: font_id u64 | size f32 | origin f32[2] | color f32[4]
1412
+ /// | text u32+bytes | has_layout u8 | layout { max_width f32,
1413
+ /// line_height f32, wrap u8 (0 none / 1 word / 2 character), align u8
1414
+ /// (0 start / 1 center / 2 end), has_lines u8, [line_count u32, lines
1415
+ /// { x f32, baseline f32, text u32+bytes }] }. Lines carry the same
1416
+ /// engine-measured breaks the JSON encoding serializes; has_lines = 0
1417
+ /// keeps the host's legacy wrapping fallback for runs past the line
1418
+ /// budget.
1419
+ fn writeBinaryText(text: CanvasGpuText, writer: anytype) !void {
1420
+ try writer.writeInt(u64, text.font_id, .little);
1421
+ try writeBinaryF32(text.size, writer);
1422
+ try writeBinaryPoint(text.origin, writer);
1423
+ try writeBinaryColor(text.color, writer);
1424
+ try writeBinarySlice(text.text, writer);
1425
+ const options = text.text_layout orelse {
1426
+ try writer.writeByte(0);
1427
+ return;
1428
+ };
1429
+ try writer.writeByte(1);
1430
+ try writeBinaryF32(nonNegative(options.max_width), writer);
1431
+ try writeBinaryF32(nonNegative(options.line_height), writer);
1432
+ try writer.writeByte(switch (options.wrap) {
1433
+ .none => 0,
1434
+ .word => 1,
1435
+ .character => 2,
1436
+ });
1437
+ try writer.writeByte(switch (options.alignment) {
1438
+ .start => 0,
1439
+ .center => 1,
1440
+ .end => 2,
1441
+ });
1442
+ var lines: [max_packet_text_layout_lines]TextLine = undefined;
1443
+ const layout = packetTextLayout(text, options, &lines) orelse {
1444
+ try writer.writeByte(0);
1445
+ return;
1446
+ };
1447
+ try writer.writeByte(1);
1448
+ try writer.writeInt(u32, @intCast(layout.lines.len), .little);
1449
+ for (layout.lines) |line| {
1450
+ // Elided lines ship painted bytes (kept prefix + ellipsis),
1451
+ // mirroring the JSON encoding, so both packet hosts draw the
1452
+ // measured extent verbatim.
1453
+ const start = @min(line.text_start, text.text.len);
1454
+ const end = @min(text.text.len, start + line.paintedTextLen());
1455
+ try writeBinaryF32(line.bounds.x, writer);
1456
+ try writeBinaryF32(line.baseline, writer);
1457
+ const ellipsis = packetLineEllipsis(line);
1458
+ try writer.writeInt(u32, @intCast(end - start + ellipsis.len), .little);
1459
+ try writer.writeAll(text.text[start..end]);
1460
+ try writer.writeAll(ellipsis);
1461
+ }
1462
+ }
1463
+
1464
+ /// Effect: tag u8 (1 shadow / 2 blur) + payload.
1465
+ fn writeBinaryEffect(effect: CanvasGpuEffect, writer: anytype) !void {
1466
+ switch (effect) {
1467
+ .none => unreachable, // callers gate on the effect flag
1468
+ .shadow => |shadow| {
1469
+ try writer.writeByte(1);
1470
+ try writeBinaryRect(shadow.rect, writer);
1471
+ try writeBinaryRadius(shadow.radius, writer);
1472
+ try writeBinaryF32(shadow.offset.dx, writer);
1473
+ try writeBinaryF32(shadow.offset.dy, writer);
1474
+ try writeBinaryF32(shadow.blur, writer);
1475
+ try writeBinaryF32(shadow.spread, writer);
1476
+ try writeBinaryColor(shadow.color, writer);
1477
+ },
1478
+ .blur => |blur| {
1479
+ try writer.writeByte(2);
1480
+ try writeBinaryRect(blur.rect, writer);
1481
+ try writeBinaryF32(blur.radius, writer);
1482
+ },
1483
+ }
1484
+ }
1485
+
1486
+ fn writeBinarySlice(bytes: []const u8, writer: anytype) !void {
1487
+ try writer.writeInt(u32, @intCast(bytes.len), .little);
1488
+ try writer.writeAll(bytes);
1489
+ }
1490
+
1491
+ fn writeBinaryF32(value: f32, writer: anytype) !void {
1492
+ try writer.writeInt(u32, @bitCast(value), .little);
1493
+ }
1494
+
1495
+ fn writeBinaryRect(rect: geometry.RectF, writer: anytype) !void {
1496
+ try writeBinaryF32(rect.x, writer);
1497
+ try writeBinaryF32(rect.y, writer);
1498
+ try writeBinaryF32(rect.width, writer);
1499
+ try writeBinaryF32(rect.height, writer);
1500
+ }
1501
+
1502
+ fn writeBinaryPoint(point: geometry.PointF, writer: anytype) !void {
1503
+ try writeBinaryF32(point.x, writer);
1504
+ try writeBinaryF32(point.y, writer);
1505
+ }
1506
+
1507
+ fn writeBinaryColor(color: Color, writer: anytype) !void {
1508
+ try writeBinaryF32(color.r, writer);
1509
+ try writeBinaryF32(color.g, writer);
1510
+ try writeBinaryF32(color.b, writer);
1511
+ try writeBinaryF32(color.a, writer);
1512
+ }
1513
+
1514
+ fn writeBinaryRadius(radius: Radius, writer: anytype) !void {
1515
+ try writeBinaryF32(radius.top_left, writer);
1516
+ try writeBinaryF32(radius.top_right, writer);
1517
+ try writeBinaryF32(radius.bottom_right, writer);
1518
+ try writeBinaryF32(radius.bottom_left, writer);
1519
+ }
1520
+
1521
+ fn writeBinaryAffine(matrix: Affine, writer: anytype) !void {
1522
+ try writeBinaryF32(matrix.a, writer);
1523
+ try writeBinaryF32(matrix.b, writer);
1524
+ try writeBinaryF32(matrix.c, writer);
1525
+ try writeBinaryF32(matrix.d, writer);
1526
+ try writeBinaryF32(matrix.tx, writer);
1527
+ try writeBinaryF32(matrix.ty, writer);
1528
+ }