@native-sdk/cli 0.0.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,919 @@
1
+ //! Deterministic CPU path rasterizer — the one vector core serving path
2
+ //! draw commands, real font glyphs, and SVG icons.
3
+ //!
4
+ //! Design constraints:
5
+ //! - std-only and allocation-free: fixed-capacity buffers on the caller's
6
+ //! stack, no allocator anywhere.
7
+ //! - Deterministic across platforms: the raster core uses only `+ - * /`
8
+ //! and `@sqrt` (all IEEE-754 correctly rounded). Round caps/joins and
9
+ //! arcs are lowered to cubic Beziers via the quarter-circle kappa
10
+ //! constant and sqrt-only half-angle identities, so no trigonometry is
11
+ //! involved. The single exception is a nonzero `x_rotation` on
12
+ //! `PathBuilder.arcTo` (documented there).
13
+ //! - Anti-aliased scanline fill: `sub_samples` vertical subsamples per
14
+ //! pixel row with analytically exact horizontal span coverage,
15
+ //! supporting both `nonzero` and `even_odd` fill rules.
16
+ //! - Stroke-to-outline: every segment, join, and cap becomes a
17
+ //! consistently oriented polygon piece and the union is filled with the
18
+ //! nonzero rule. Caps: butt or round. Joins: miter (with limit,
19
+ //! falling back to bevel) or round — the built-in stroke icons need round.
20
+ //!
21
+ //! The path model is the existing wire model (`drawing.PathElement`:
22
+ //! move/line/quad/cubic/close). Arcs are a *builder-level* verb lowered to
23
+ //! cubics by `PathBuilder.arcTo`, so serialization, hashing, and the
24
+ //! AppKit packet path are untouched: paths built here remain fully
25
+ //! packet-representable, and AppKit keeps rasterizing them natively.
26
+
27
+ const std = @import("std");
28
+ const geometry = @import("geometry");
29
+ const drawing_model = @import("drawing.zig");
30
+
31
+ const PointF = geometry.PointF;
32
+ const Affine = drawing_model.Affine;
33
+ const PathElement = drawing_model.PathElement;
34
+ const PathVerb = drawing_model.PathVerb;
35
+
36
+ pub const Error = error{
37
+ /// A fixed-capacity buffer (edges, subpath points, scanline
38
+ /// crossings, or a `PathBuilder`) overflowed. The path is too complex
39
+ /// for the deterministic budget; split it or simplify it.
40
+ VectorPathTooComplex,
41
+ /// The clipped raster region is wider than `max_raster_width` pixels.
42
+ VectorRasterTooWide,
43
+ };
44
+
45
+ /// How a filled path decides interiorness.
46
+ pub const FillRule = enum { nonzero, even_odd };
47
+
48
+ /// Stroke end-cap shape — the wire model's type, re-exported so the
49
+ /// stroke rasterizer, the parsed icon style, and the `stroke_path`
50
+ /// command's cap channel all share one enum (no mapping layer to drift).
51
+ pub const LineCap = drawing_model.LineCap;
52
+
53
+ /// Stroke join shape. `miter` falls back to a bevel past `miter_limit`.
54
+ pub const LineJoin = enum { miter, round };
55
+
56
+ pub const StrokeStyle = struct {
57
+ /// Stroke width in device pixels (apply any transform scale before
58
+ /// passing it in).
59
+ width: f32,
60
+ cap: LineCap = .butt,
61
+ join: LineJoin = .miter,
62
+ /// Ratio of miter length to half stroke width above which a miter
63
+ /// join is replaced by a bevel (the SVG default is 4).
64
+ miter_limit: f32 = 4,
65
+ };
66
+
67
+ /// Curve flattening tolerance in device pixels: the maximum distance the
68
+ /// polyline approximation may deviate from the true curve.
69
+ pub const default_tolerance: f32 = 0.25;
70
+
71
+ /// Vertical subsamples per pixel row; horizontal coverage is exact.
72
+ pub const sub_samples: usize = 4;
73
+
74
+ pub const max_edges: usize = 4096;
75
+ pub const max_scanline_crossings: usize = 256;
76
+ pub const max_raster_width: usize = 8192;
77
+ pub const max_curve_segments: usize = 48;
78
+ pub const max_stroke_subpath_points: usize = 512;
79
+ const max_piece_points: usize = 4 * max_curve_segments + 4;
80
+
81
+ /// Half-open device-pixel clip window: pixels with `x0 <= x < x1` and
82
+ /// `y0 <= y < y1` may be emitted.
83
+ pub const ClipRect = struct {
84
+ x0: i32,
85
+ y0: i32,
86
+ x1: i32,
87
+ y1: i32,
88
+ };
89
+
90
+ /// Kappa: control-point offset factor approximating a quarter circle with
91
+ /// one cubic Bezier.
92
+ pub const circle_kappa: f32 = 0.5522847498307936;
93
+
94
+ // ---------------------------------------------------------------------------
95
+ // Path builder
96
+ // ---------------------------------------------------------------------------
97
+
98
+ /// Fixed-capacity builder over the wire path model. Arc verbs are lowered
99
+ /// to cubic Beziers at build time so the emitted elements stay
100
+ /// serializable, hashable, and packet-representable unchanged.
101
+ pub fn PathBuilder(comptime capacity: usize) type {
102
+ return struct {
103
+ elements: [capacity]PathElement = undefined,
104
+ len: usize = 0,
105
+ current: PointF = PointF.zero(),
106
+ subpath_start: PointF = PointF.zero(),
107
+ has_current: bool = false,
108
+
109
+ const Self = @This();
110
+
111
+ pub fn slice(self: *const Self) []const PathElement {
112
+ return self.elements[0..self.len];
113
+ }
114
+
115
+ pub fn reset(self: *Self) void {
116
+ self.len = 0;
117
+ self.has_current = false;
118
+ self.current = PointF.zero();
119
+ self.subpath_start = PointF.zero();
120
+ }
121
+
122
+ pub fn moveTo(self: *Self, point: PointF) Error!void {
123
+ try self.append(.{ .verb = .move_to, .points = .{ point, PointF.zero(), PointF.zero() } });
124
+ self.current = point;
125
+ self.subpath_start = point;
126
+ self.has_current = true;
127
+ }
128
+
129
+ pub fn lineTo(self: *Self, point: PointF) Error!void {
130
+ if (!self.has_current) return self.moveTo(point);
131
+ try self.append(.{ .verb = .line_to, .points = .{ point, PointF.zero(), PointF.zero() } });
132
+ self.current = point;
133
+ }
134
+
135
+ pub fn quadTo(self: *Self, control: PointF, end: PointF) Error!void {
136
+ if (!self.has_current) try self.moveTo(PointF.zero());
137
+ try self.append(.{ .verb = .quad_to, .points = .{ control, end, PointF.zero() } });
138
+ self.current = end;
139
+ }
140
+
141
+ pub fn cubicTo(self: *Self, control_a: PointF, control_b: PointF, end: PointF) Error!void {
142
+ if (!self.has_current) try self.moveTo(PointF.zero());
143
+ try self.append(.{ .verb = .cubic_to, .points = .{ control_a, control_b, end } });
144
+ self.current = end;
145
+ }
146
+
147
+ pub fn close(self: *Self) Error!void {
148
+ if (!self.has_current) return;
149
+ try self.append(.{ .verb = .close });
150
+ self.current = self.subpath_start;
151
+ }
152
+
153
+ /// SVG endpoint arc (the `A`/`a` path command), lowered to cubic
154
+ /// Beziers. `x_rotation_deg` is the ellipse x-axis rotation in
155
+ /// degrees; when it is zero (every built-in icon) the lowering is
156
+ /// sqrt-only and fully deterministic. A nonzero rotation uses
157
+ /// `@cos`/`@sin` once and is the sole non-sqrt code path in this
158
+ /// module.
159
+ pub fn arcTo(
160
+ self: *Self,
161
+ rx_in: f32,
162
+ ry_in: f32,
163
+ x_rotation_deg: f32,
164
+ large_arc: bool,
165
+ sweep: bool,
166
+ end: PointF,
167
+ ) Error!void {
168
+ if (!self.has_current) try self.moveTo(PointF.zero());
169
+ const start = self.current;
170
+ var rx = @abs(rx_in);
171
+ var ry = @abs(ry_in);
172
+ if (rx <= 0 or ry <= 0) return self.lineTo(end);
173
+ if (start.x == end.x and start.y == end.y) return;
174
+
175
+ var cos_phi: f32 = 1;
176
+ var sin_phi: f32 = 0;
177
+ if (x_rotation_deg != 0) {
178
+ const phi = x_rotation_deg * std.math.pi / 180.0;
179
+ cos_phi = @cos(phi);
180
+ sin_phi = @sin(phi);
181
+ }
182
+
183
+ // SVG implementation notes F.6.5: endpoint to center form.
184
+ const half_dx = (start.x - end.x) * 0.5;
185
+ const half_dy = (start.y - end.y) * 0.5;
186
+ const x1p = cos_phi * half_dx + sin_phi * half_dy;
187
+ const y1p = -sin_phi * half_dx + cos_phi * half_dy;
188
+
189
+ const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
190
+ if (lambda > 1) {
191
+ const scale = @sqrt(lambda);
192
+ rx *= scale;
193
+ ry *= scale;
194
+ }
195
+
196
+ const rx2 = rx * rx;
197
+ const ry2 = ry * ry;
198
+ const denom = rx2 * y1p * y1p + ry2 * x1p * x1p;
199
+ if (denom <= 0) return self.lineTo(end);
200
+ const radicand = @max(0, (rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p) / denom);
201
+ var coefficient = @sqrt(radicand);
202
+ if (large_arc == sweep) coefficient = -coefficient;
203
+ const cxp = coefficient * (rx * y1p / ry);
204
+ const cyp = coefficient * (-ry * x1p / rx);
205
+
206
+ const center = PointF.init(
207
+ cos_phi * cxp - sin_phi * cyp + (start.x + end.x) * 0.5,
208
+ sin_phi * cxp + cos_phi * cyp + (start.y + end.y) * 0.5,
209
+ );
210
+
211
+ const unit_from = normalizeOrNull(PointF.init((x1p - cxp) / rx, (y1p - cyp) / ry)) orelse return self.lineTo(end);
212
+ const unit_to = normalizeOrNull(PointF.init((-x1p - cxp) / rx, (-y1p - cyp) / ry)) orelse return self.lineTo(end);
213
+
214
+ const frame = ArcFrame{
215
+ .center = center,
216
+ .rx = rx,
217
+ .ry = ry,
218
+ .cos_phi = cos_phi,
219
+ .sin_phi = sin_phi,
220
+ };
221
+ if (large_arc) {
222
+ // Split at the point diametrically opposite the short-way
223
+ // midpoint; each half is then at most 180 degrees.
224
+ const sum = PointF.init(unit_from.x + unit_to.x, unit_from.y + unit_to.y);
225
+ const mid = if (normalizeOrNull(sum)) |unit|
226
+ PointF.init(-unit.x, -unit.y)
227
+ else
228
+ rotate90(unit_from, sweep);
229
+ try self.emitUnitArc(frame, unit_from, mid, sweep);
230
+ try self.emitUnitArc(frame, mid, unit_to, sweep);
231
+ } else {
232
+ try self.emitUnitArc(frame, unit_from, unit_to, sweep);
233
+ }
234
+ self.current = end;
235
+ }
236
+
237
+ /// Full circle as four kappa cubics (sqrt-free, deterministic).
238
+ pub fn circle(self: *Self, center: PointF, radius: f32) Error!void {
239
+ if (radius <= 0) return;
240
+ const r = radius;
241
+ const k = circle_kappa * r;
242
+ const cx = center.x;
243
+ const cy = center.y;
244
+ try self.moveTo(PointF.init(cx + r, cy));
245
+ try self.cubicTo(PointF.init(cx + r, cy + k), PointF.init(cx + k, cy + r), PointF.init(cx, cy + r));
246
+ try self.cubicTo(PointF.init(cx - k, cy + r), PointF.init(cx - r, cy + k), PointF.init(cx - r, cy));
247
+ try self.cubicTo(PointF.init(cx - r, cy - k), PointF.init(cx - k, cy - r), PointF.init(cx, cy - r));
248
+ try self.cubicTo(PointF.init(cx + k, cy - r), PointF.init(cx + r, cy - k), PointF.init(cx + r, cy));
249
+ try self.close();
250
+ }
251
+
252
+ /// Arc of at most 180 degrees between unit vectors, recursively
253
+ /// bisected (sqrt-only) until each piece is at most ~90 degrees,
254
+ /// then emitted as one cubic per piece.
255
+ fn emitUnitArc(self: *Self, frame: ArcFrame, a: PointF, b: PointF, sweep: bool) Error!void {
256
+ const dot = a.x * b.x + a.y * b.y;
257
+ if (dot < 0.1) {
258
+ // More than ~84 degrees: bisect at the short-way midpoint
259
+ // (or the sweep-side perpendicular at exactly 180).
260
+ const sum = PointF.init(a.x + b.x, a.y + b.y);
261
+ const mid = normalizeOrNull(sum) orelse rotate90(a, sweep);
262
+ // The short-way midpoint must lie on the sweep side; for
263
+ // arcs <= 180 degrees it always does.
264
+ try self.emitUnitArc(frame, a, mid, sweep);
265
+ try self.emitUnitArc(frame, mid, b, sweep);
266
+ return;
267
+ }
268
+ // tan(theta/4) from cos(theta) via sqrt-only half-angle
269
+ // identities: cos(t/2) = sqrt((1+cos t)/2), sin(t/2) =
270
+ // sqrt((1-cos t)/2), tan(t/4) = sin(t/2) / (1 + cos(t/2)).
271
+ const clamped = std.math.clamp(dot, -1, 1);
272
+ const cos_half = @sqrt((1 + clamped) * 0.5);
273
+ const sin_half = @sqrt((1 - clamped) * 0.5);
274
+ const tan_quarter = sin_half / (1 + cos_half);
275
+ const k = (4.0 / 3.0) * tan_quarter;
276
+
277
+ const ta = rotate90(a, sweep);
278
+ const tb = rotate90(b, sweep);
279
+ const c1 = PointF.init(a.x + ta.x * k, a.y + ta.y * k);
280
+ const c2 = PointF.init(b.x - tb.x * k, b.y - tb.y * k);
281
+ try self.cubicTo(frame.map(c1), frame.map(c2), frame.map(b));
282
+ }
283
+
284
+ fn append(self: *Self, element: PathElement) Error!void {
285
+ if (self.len >= capacity) return error.VectorPathTooComplex;
286
+ self.elements[self.len] = element;
287
+ self.len += 1;
288
+ }
289
+ };
290
+ }
291
+
292
+ const ArcFrame = struct {
293
+ center: PointF,
294
+ rx: f32,
295
+ ry: f32,
296
+ cos_phi: f32,
297
+ sin_phi: f32,
298
+
299
+ fn map(self: ArcFrame, unit: PointF) PointF {
300
+ const x = unit.x * self.rx;
301
+ const y = unit.y * self.ry;
302
+ return PointF.init(
303
+ self.center.x + self.cos_phi * x - self.sin_phi * y,
304
+ self.center.y + self.sin_phi * x + self.cos_phi * y,
305
+ );
306
+ }
307
+ };
308
+
309
+ fn normalizeOrNull(v: PointF) ?PointF {
310
+ const len = @sqrt(v.x * v.x + v.y * v.y);
311
+ if (len <= 0.000001) return null;
312
+ return PointF.init(v.x / len, v.y / len);
313
+ }
314
+
315
+ /// Rotate a vector 90 degrees in the sweep direction (y-down screen
316
+ /// coordinates: `sweep` follows increasing angle, i.e. clockwise on
317
+ /// screen).
318
+ fn rotate90(v: PointF, sweep: bool) PointF {
319
+ return if (sweep) PointF.init(-v.y, v.x) else PointF.init(v.y, -v.x);
320
+ }
321
+
322
+ // ---------------------------------------------------------------------------
323
+ // Flattening
324
+ // ---------------------------------------------------------------------------
325
+
326
+ /// Deterministic segment count for a quadratic: the maximum deviation of
327
+ /// the chord from the curve is bounded by the second difference over 4,
328
+ /// and subdividing into n segments divides it by n^2.
329
+ pub fn quadSegmentCount(p0: PointF, p1: PointF, p2: PointF, tolerance: f32) usize {
330
+ const dx = @abs(p0.x - 2 * p1.x + p2.x);
331
+ const dy = @abs(p0.y - 2 * p1.y + p2.y);
332
+ const deviation = @max(dx, dy) * 0.25;
333
+ return segmentCountForDeviation(deviation, tolerance);
334
+ }
335
+
336
+ /// Deterministic segment count for a cubic from the larger of its two
337
+ /// second differences (conservative flatness bound).
338
+ pub fn cubicSegmentCount(p0: PointF, p1: PointF, p2: PointF, p3: PointF, tolerance: f32) usize {
339
+ const d1x = @abs(p0.x - 2 * p1.x + p2.x);
340
+ const d1y = @abs(p0.y - 2 * p1.y + p2.y);
341
+ const d2x = @abs(p1.x - 2 * p2.x + p3.x);
342
+ const d2y = @abs(p1.y - 2 * p2.y + p3.y);
343
+ const deviation = @max(@max(d1x, d1y), @max(d2x, d2y)) * 0.75;
344
+ return segmentCountForDeviation(deviation, tolerance);
345
+ }
346
+
347
+ fn segmentCountForDeviation(deviation: f32, tolerance: f32) usize {
348
+ const tol = @max(0.01, tolerance);
349
+ if (!(deviation > tol)) return 1;
350
+ const count = @ceil(@sqrt(deviation / tol));
351
+ if (!(count > 1)) return 1;
352
+ if (count >= @as(f32, @floatFromInt(max_curve_segments))) return max_curve_segments;
353
+ return @intFromFloat(count);
354
+ }
355
+
356
+ fn quadPoint(a: PointF, b: PointF, c: PointF, t: f32) PointF {
357
+ const u = 1 - t;
358
+ return PointF.init(
359
+ u * u * a.x + 2 * u * t * b.x + t * t * c.x,
360
+ u * u * a.y + 2 * u * t * b.y + t * t * c.y,
361
+ );
362
+ }
363
+
364
+ fn cubicPoint(a: PointF, b: PointF, c: PointF, d: PointF, t: f32) PointF {
365
+ const u = 1 - t;
366
+ return PointF.init(
367
+ u * u * u * a.x + 3 * u * u * t * b.x + 3 * u * t * t * c.x + t * t * t * d.x,
368
+ u * u * u * a.y + 3 * u * u * t * b.y + 3 * u * t * t * c.y + t * t * t * d.y,
369
+ );
370
+ }
371
+
372
+ /// Walk a path in device space (transform applied), flattening curves to
373
+ /// line segments within `tolerance`, reporting subpaths to `sink`:
374
+ /// `subpathBegin(point)`, `subpathPoint(point)`, `subpathEnd(closed)`.
375
+ fn walkPath(elements: []const PathElement, transform: Affine, tolerance: f32, sink: anytype) Error!void {
376
+ var has_current = false;
377
+ var active = false;
378
+ var current = PointF.zero();
379
+ var subpath_start = PointF.zero();
380
+
381
+ for (elements) |element| {
382
+ switch (element.verb) {
383
+ .move_to => {
384
+ if (active) {
385
+ try sink.subpathEnd(false);
386
+ active = false;
387
+ }
388
+ current = transform.transformPoint(element.points[0]);
389
+ subpath_start = current;
390
+ has_current = true;
391
+ },
392
+ .line_to => {
393
+ const next = transform.transformPoint(element.points[0]);
394
+ if (!has_current) {
395
+ current = next;
396
+ subpath_start = next;
397
+ has_current = true;
398
+ continue;
399
+ }
400
+ try ensureActive(sink, &active, current);
401
+ try sink.subpathPoint(next);
402
+ current = next;
403
+ },
404
+ .quad_to => {
405
+ if (!has_current) continue;
406
+ const control = transform.transformPoint(element.points[0]);
407
+ const end = transform.transformPoint(element.points[1]);
408
+ try ensureActive(sink, &active, current);
409
+ const segments = quadSegmentCount(current, control, end, tolerance);
410
+ var index: usize = 1;
411
+ while (index <= segments) : (index += 1) {
412
+ const t = @as(f32, @floatFromInt(index)) / @as(f32, @floatFromInt(segments));
413
+ try sink.subpathPoint(quadPoint(current, control, end, t));
414
+ }
415
+ current = end;
416
+ },
417
+ .cubic_to => {
418
+ if (!has_current) continue;
419
+ const control_a = transform.transformPoint(element.points[0]);
420
+ const control_b = transform.transformPoint(element.points[1]);
421
+ const end = transform.transformPoint(element.points[2]);
422
+ try ensureActive(sink, &active, current);
423
+ const segments = cubicSegmentCount(current, control_a, control_b, end, tolerance);
424
+ var index: usize = 1;
425
+ while (index <= segments) : (index += 1) {
426
+ const t = @as(f32, @floatFromInt(index)) / @as(f32, @floatFromInt(segments));
427
+ try sink.subpathPoint(cubicPoint(current, control_a, control_b, end, t));
428
+ }
429
+ current = end;
430
+ },
431
+ .close => {
432
+ if (active) {
433
+ try sink.subpathEnd(true);
434
+ active = false;
435
+ }
436
+ if (has_current) current = subpath_start;
437
+ },
438
+ }
439
+ }
440
+ if (active) try sink.subpathEnd(false);
441
+ }
442
+
443
+ fn ensureActive(sink: anytype, active: *bool, start: PointF) Error!void {
444
+ if (active.*) return;
445
+ try sink.subpathBegin(start);
446
+ active.* = true;
447
+ }
448
+
449
+ // ---------------------------------------------------------------------------
450
+ // Edge accumulation + scanline sweep
451
+ // ---------------------------------------------------------------------------
452
+
453
+ const Edge = struct {
454
+ x0: f32,
455
+ y0: f32,
456
+ x1: f32,
457
+ y1: f32,
458
+ /// Winding contribution: +1 for a downward edge, -1 for upward.
459
+ dir: f32,
460
+ };
461
+
462
+ /// Fixed-capacity edge accumulator plus the anti-aliased scanline sweep.
463
+ /// Instantiate on the stack (`var raster: Rasterizer = .{};`); it holds
464
+ /// no pointers and is reusable via `resetEdges`.
465
+ pub const Rasterizer = struct {
466
+ edges: [max_edges]Edge = undefined,
467
+ edge_count: usize = 0,
468
+ min_x: f32 = 0,
469
+ min_y: f32 = 0,
470
+ max_x: f32 = 0,
471
+ max_y: f32 = 0,
472
+
473
+ pub fn resetEdges(self: *Rasterizer) void {
474
+ self.edge_count = 0;
475
+ }
476
+
477
+ pub fn addEdge(self: *Rasterizer, a: PointF, b: PointF) Error!void {
478
+ if (a.y == b.y) return;
479
+ if (!std.math.isFinite(a.x) or !std.math.isFinite(a.y) or !std.math.isFinite(b.x) or !std.math.isFinite(b.y)) return;
480
+ if (self.edge_count >= max_edges) return error.VectorPathTooComplex;
481
+ if (self.edge_count == 0) {
482
+ self.min_x = @min(a.x, b.x);
483
+ self.max_x = @max(a.x, b.x);
484
+ self.min_y = @min(a.y, b.y);
485
+ self.max_y = @max(a.y, b.y);
486
+ } else {
487
+ self.min_x = @min(self.min_x, @min(a.x, b.x));
488
+ self.max_x = @max(self.max_x, @max(a.x, b.x));
489
+ self.min_y = @min(self.min_y, @min(a.y, b.y));
490
+ self.max_y = @max(self.max_y, @max(a.y, b.y));
491
+ }
492
+ self.edges[self.edge_count] = .{ .x0 = a.x, .y0 = a.y, .x1 = b.x, .y1 = b.y, .dir = if (b.y > a.y) 1 else -1 };
493
+ self.edge_count += 1;
494
+ }
495
+
496
+ /// Append a closed polygon ring with canonical orientation (pieces of
497
+ /// a stroke union must all wind the same way for the nonzero rule).
498
+ pub fn addPolygon(self: *Rasterizer, points: []const PointF) Error!void {
499
+ if (points.len < 3) return;
500
+ var area: f32 = 0;
501
+ var index: usize = 0;
502
+ while (index < points.len) : (index += 1) {
503
+ const a = points[index];
504
+ const b = points[(index + 1) % points.len];
505
+ area += a.x * b.y - b.x * a.y;
506
+ }
507
+ if (@abs(area) <= 0.000000001) return;
508
+ if (area >= 0) {
509
+ index = 0;
510
+ while (index < points.len) : (index += 1) {
511
+ try self.addEdge(points[index], points[(index + 1) % points.len]);
512
+ }
513
+ } else {
514
+ index = points.len;
515
+ while (index > 0) : (index -= 1) {
516
+ try self.addEdge(points[index % points.len], points[index - 1]);
517
+ }
518
+ }
519
+ }
520
+
521
+ /// Sweep the accumulated edges, emitting `sink.pixel(x, y, coverage)`
522
+ /// for every pixel inside `clip` with coverage > 0. Coverage is in
523
+ /// (0, 1]; `sub_samples` vertical subsamples, exact horizontal spans.
524
+ pub fn sweep(self: *const Rasterizer, rule: FillRule, clip: ClipRect, sink: anytype) Error!void {
525
+ if (self.edge_count == 0) return;
526
+ const y_begin = @max(clip.y0, floorI(self.min_y));
527
+ const y_end = @min(clip.y1, ceilI(self.max_y));
528
+ const x_begin = @max(clip.x0, floorI(self.min_x));
529
+ const x_end = @min(clip.x1, ceilI(self.max_x));
530
+ if (x_end <= x_begin or y_end <= y_begin) return;
531
+ const width: usize = @intCast(x_end - x_begin);
532
+ if (width > max_raster_width) return error.VectorRasterTooWide;
533
+
534
+ const x_origin = @as(f32, @floatFromInt(x_begin));
535
+ const weight = 1.0 / @as(f32, @floatFromInt(sub_samples));
536
+ var row: [max_raster_width]f32 = undefined;
537
+
538
+ var y = y_begin;
539
+ while (y < y_end) : (y += 1) {
540
+ @memset(row[0..width], 0);
541
+ var sub: usize = 0;
542
+ while (sub < sub_samples) : (sub += 1) {
543
+ const sy = @as(f32, @floatFromInt(y)) +
544
+ (@as(f32, @floatFromInt(sub)) + 0.5) / @as(f32, @floatFromInt(sub_samples));
545
+ var crossings: [max_scanline_crossings]Crossing = undefined;
546
+ var crossing_count: usize = 0;
547
+ for (self.edges[0..self.edge_count]) |edge| {
548
+ const top = @min(edge.y0, edge.y1);
549
+ const bottom = @max(edge.y0, edge.y1);
550
+ if (!(sy >= top and sy < bottom)) continue;
551
+ const x = edge.x0 + (sy - edge.y0) * (edge.x1 - edge.x0) / (edge.y1 - edge.y0);
552
+ if (crossing_count >= max_scanline_crossings) return error.VectorPathTooComplex;
553
+ crossings[crossing_count] = .{ .x = x, .dir = edge.dir };
554
+ crossing_count += 1;
555
+ }
556
+ if (crossing_count == 0) continue;
557
+ sortCrossings(crossings[0..crossing_count]);
558
+
559
+ var winding: f32 = 0;
560
+ var parity = false;
561
+ var inside = false;
562
+ var span_start: f32 = 0;
563
+ for (crossings[0..crossing_count]) |crossing| {
564
+ const was_inside = inside;
565
+ switch (rule) {
566
+ .nonzero => {
567
+ winding += crossing.dir;
568
+ inside = winding != 0;
569
+ },
570
+ .even_odd => {
571
+ parity = !parity;
572
+ inside = parity;
573
+ },
574
+ }
575
+ if (!was_inside and inside) {
576
+ span_start = crossing.x;
577
+ } else if (was_inside and !inside) {
578
+ accumulateSpan(row[0..width], x_origin, span_start, crossing.x, weight);
579
+ }
580
+ }
581
+ }
582
+ for (row[0..width], 0..) |coverage, i| {
583
+ if (coverage > 0.0009) {
584
+ sink.pixel(x_begin + @as(i32, @intCast(i)), y, @min(1, coverage));
585
+ }
586
+ }
587
+ }
588
+ }
589
+ };
590
+
591
+ const Crossing = struct {
592
+ x: f32,
593
+ dir: f32,
594
+ };
595
+
596
+ fn sortCrossings(crossings: []Crossing) void {
597
+ var i: usize = 1;
598
+ while (i < crossings.len) : (i += 1) {
599
+ const value = crossings[i];
600
+ var j = i;
601
+ while (j > 0 and crossings[j - 1].x > value.x) : (j -= 1) {
602
+ crossings[j] = crossings[j - 1];
603
+ }
604
+ crossings[j] = value;
605
+ }
606
+ }
607
+
608
+ fn accumulateSpan(row: []f32, x_origin: f32, span_start: f32, span_end: f32, weight: f32) void {
609
+ const width_f = @as(f32, @floatFromInt(row.len));
610
+ const a = std.math.clamp(span_start - x_origin, 0, width_f);
611
+ const b = std.math.clamp(span_end - x_origin, 0, width_f);
612
+ if (b <= a) return;
613
+ var px: usize = @intFromFloat(@floor(a));
614
+ while (px < row.len) : (px += 1) {
615
+ const left = @max(a, @as(f32, @floatFromInt(px)));
616
+ const right = @min(b, @as(f32, @floatFromInt(px + 1)));
617
+ if (right <= left) break;
618
+ row[px] += (right - left) * weight;
619
+ }
620
+ }
621
+
622
+ fn floorI(value: f32) i32 {
623
+ if (!std.math.isFinite(value)) return 0;
624
+ return @intFromFloat(@floor(value));
625
+ }
626
+
627
+ fn ceilI(value: f32) i32 {
628
+ if (!std.math.isFinite(value)) return 0;
629
+ return @intFromFloat(@ceil(value));
630
+ }
631
+
632
+ // ---------------------------------------------------------------------------
633
+ // Fill
634
+ // ---------------------------------------------------------------------------
635
+
636
+ /// Rasterize a filled path (subpaths implicitly closed) with the given
637
+ /// fill rule, calling `sink.pixel(x, y, coverage)` per covered pixel.
638
+ pub fn fillPath(
639
+ elements: []const PathElement,
640
+ transform: Affine,
641
+ rule: FillRule,
642
+ tolerance: f32,
643
+ clip: ClipRect,
644
+ sink: anytype,
645
+ ) Error!void {
646
+ var raster: Rasterizer = .{};
647
+ try accumulateFillEdges(&raster, elements, transform, tolerance);
648
+ try raster.sweep(rule, clip, sink);
649
+ }
650
+
651
+ /// Accumulate a filled path's edges into an existing rasterizer (lets a
652
+ /// caller union several paths — e.g. all glyph contours of one text run —
653
+ /// before one sweep).
654
+ pub fn accumulateFillEdges(
655
+ raster: *Rasterizer,
656
+ elements: []const PathElement,
657
+ transform: Affine,
658
+ tolerance: f32,
659
+ ) Error!void {
660
+ var sink = FillEdgeSink{ .raster = raster };
661
+ try walkPath(elements, transform, tolerance, &sink);
662
+ }
663
+
664
+ const FillEdgeSink = struct {
665
+ raster: *Rasterizer,
666
+ start: PointF = PointF.zero(),
667
+ current: PointF = PointF.zero(),
668
+
669
+ fn subpathBegin(self: *FillEdgeSink, point: PointF) Error!void {
670
+ self.start = point;
671
+ self.current = point;
672
+ }
673
+
674
+ fn subpathPoint(self: *FillEdgeSink, point: PointF) Error!void {
675
+ try self.raster.addEdge(self.current, point);
676
+ self.current = point;
677
+ }
678
+
679
+ fn subpathEnd(self: *FillEdgeSink, closed: bool) Error!void {
680
+ _ = closed;
681
+ // Fill semantics always close the contour.
682
+ try self.raster.addEdge(self.current, self.start);
683
+ }
684
+ };
685
+
686
+ // ---------------------------------------------------------------------------
687
+ // Stroke
688
+ // ---------------------------------------------------------------------------
689
+
690
+ /// Rasterize a stroked path: flatten, convert to an outline (segment
691
+ /// quads + join and cap pieces, all consistently oriented), and fill the
692
+ /// union with the nonzero rule. `style.width` is in device pixels.
693
+ pub fn strokePath(
694
+ elements: []const PathElement,
695
+ transform: Affine,
696
+ style: StrokeStyle,
697
+ tolerance: f32,
698
+ clip: ClipRect,
699
+ sink: anytype,
700
+ ) Error!void {
701
+ if (!(style.width > 0)) return;
702
+ var raster: Rasterizer = .{};
703
+ try accumulateStrokeEdges(&raster, elements, transform, style, tolerance);
704
+ try raster.sweep(.nonzero, clip, sink);
705
+ }
706
+
707
+ /// Accumulate a stroked path's outline edges into an existing rasterizer.
708
+ pub fn accumulateStrokeEdges(
709
+ raster: *Rasterizer,
710
+ elements: []const PathElement,
711
+ transform: Affine,
712
+ style: StrokeStyle,
713
+ tolerance: f32,
714
+ ) Error!void {
715
+ if (!(style.width > 0)) return;
716
+ var sink = StrokeSink{
717
+ .raster = raster,
718
+ .half_width = style.width * 0.5,
719
+ .style = style,
720
+ .tolerance = @max(0.01, tolerance),
721
+ };
722
+ try walkPath(elements, transform, tolerance, &sink);
723
+ }
724
+
725
+ const StrokeSink = struct {
726
+ raster: *Rasterizer,
727
+ half_width: f32,
728
+ style: StrokeStyle,
729
+ tolerance: f32,
730
+ points: [max_stroke_subpath_points]PointF = undefined,
731
+ count: usize = 0,
732
+
733
+ fn subpathBegin(self: *StrokeSink, point: PointF) Error!void {
734
+ self.count = 0;
735
+ self.points[0] = point;
736
+ self.count = 1;
737
+ }
738
+
739
+ fn subpathPoint(self: *StrokeSink, point: PointF) Error!void {
740
+ const last = self.points[self.count - 1];
741
+ const dx = point.x - last.x;
742
+ const dy = point.y - last.y;
743
+ if (dx * dx + dy * dy <= 0.000000000001) return;
744
+ if (self.count >= max_stroke_subpath_points) return error.VectorPathTooComplex;
745
+ self.points[self.count] = point;
746
+ self.count += 1;
747
+ }
748
+
749
+ fn subpathEnd(self: *StrokeSink, closed: bool) Error!void {
750
+ var pts = self.points[0..self.count];
751
+ var is_closed = closed;
752
+ if (is_closed and pts.len >= 2) {
753
+ const first = pts[0];
754
+ const last = pts[pts.len - 1];
755
+ const dx = first.x - last.x;
756
+ const dy = first.y - last.y;
757
+ if (dx * dx + dy * dy <= 0.000000000001) {
758
+ pts = pts[0 .. pts.len - 1];
759
+ }
760
+ }
761
+ if (pts.len < 2) is_closed = false;
762
+ try strokePolyline(self.raster, pts, is_closed, self.half_width, self.style, self.tolerance);
763
+ self.count = 0;
764
+ }
765
+ };
766
+
767
+ fn strokePolyline(
768
+ raster: *Rasterizer,
769
+ points: []const PointF,
770
+ closed: bool,
771
+ half_width: f32,
772
+ style: StrokeStyle,
773
+ tolerance: f32,
774
+ ) Error!void {
775
+ if (points.len == 0) return;
776
+ if (points.len == 1) {
777
+ // A degenerate subpath: round caps render a dot, butt caps
778
+ // nothing (matching SVG stroke semantics).
779
+ if (style.cap == .round) try emitDisc(raster, points[0], half_width, tolerance);
780
+ return;
781
+ }
782
+
783
+ const segment_count = if (closed) points.len else points.len - 1;
784
+ var index: usize = 0;
785
+ while (index < segment_count) : (index += 1) {
786
+ const a = points[index];
787
+ const b = points[(index + 1) % points.len];
788
+ try emitSegmentQuad(raster, a, b, half_width);
789
+ }
790
+
791
+ if (closed) {
792
+ index = 0;
793
+ while (index < points.len) : (index += 1) {
794
+ const prev = points[(index + points.len - 1) % points.len];
795
+ const vertex = points[index];
796
+ const next = points[(index + 1) % points.len];
797
+ try emitJoin(raster, prev, vertex, next, half_width, style, tolerance);
798
+ }
799
+ } else {
800
+ index = 1;
801
+ while (index + 1 <= points.len - 1) : (index += 1) {
802
+ try emitJoin(raster, points[index - 1], points[index], points[index + 1], half_width, style, tolerance);
803
+ }
804
+ if (style.cap == .round) {
805
+ try emitDisc(raster, points[0], half_width, tolerance);
806
+ try emitDisc(raster, points[points.len - 1], half_width, tolerance);
807
+ }
808
+ }
809
+ }
810
+
811
+ fn emitSegmentQuad(raster: *Rasterizer, a: PointF, b: PointF, half_width: f32) Error!void {
812
+ const dx = b.x - a.x;
813
+ const dy = b.y - a.y;
814
+ const len = @sqrt(dx * dx + dy * dy);
815
+ if (len <= 0.000001) return;
816
+ const nx = dy / len * half_width;
817
+ const ny = -dx / len * half_width;
818
+ const quad = [4]PointF{
819
+ PointF.init(a.x + nx, a.y + ny),
820
+ PointF.init(b.x + nx, b.y + ny),
821
+ PointF.init(b.x - nx, b.y - ny),
822
+ PointF.init(a.x - nx, a.y - ny),
823
+ };
824
+ try raster.addPolygon(&quad);
825
+ }
826
+
827
+ fn emitJoin(
828
+ raster: *Rasterizer,
829
+ prev: PointF,
830
+ vertex: PointF,
831
+ next: PointF,
832
+ half_width: f32,
833
+ style: StrokeStyle,
834
+ tolerance: f32,
835
+ ) Error!void {
836
+ const din = normalizeOrNull(PointF.init(vertex.x - prev.x, vertex.y - prev.y)) orelse return;
837
+ const dout = normalizeOrNull(PointF.init(next.x - vertex.x, next.y - vertex.y)) orelse return;
838
+ const cross_z = din.x * dout.y - din.y * dout.x;
839
+ const dot = din.x * dout.x + din.y * dout.y;
840
+
841
+ // Nearly straight: the segment quads already cover the wedge to
842
+ // within tolerance, so skip the join piece (keeps dense flattened
843
+ // curves cheap and the edge budget bounded).
844
+ if (dot > 0 and half_width * (1 - dot) < tolerance * 0.25) return;
845
+
846
+ switch (style.join) {
847
+ .round => try emitDisc(raster, vertex, half_width, tolerance),
848
+ .miter => {
849
+ if (@abs(cross_z) <= 0.000001) return;
850
+ // Outer side: opposite the turn direction. Left normal of a
851
+ // direction d (y-down) is (d.y, -d.x).
852
+ const sign: f32 = if (cross_z > 0) 1 else -1;
853
+ const na = PointF.init(din.y * sign * half_width, -din.x * sign * half_width);
854
+ const nb = PointF.init(dout.y * sign * half_width, -dout.x * sign * half_width);
855
+ const a = PointF.init(vertex.x + na.x, vertex.y + na.y);
856
+ const b = PointF.init(vertex.x + nb.x, vertex.y + nb.y);
857
+ const mid = normalizeOrNull(PointF.init(na.x + nb.x, na.y + nb.y)) orelse return;
858
+ const cos_half = (mid.x * na.x + mid.y * na.y) / half_width;
859
+ if (cos_half <= 0.000001) return;
860
+ const miter_ratio = 1 / cos_half;
861
+ if (miter_ratio <= @max(1, style.miter_limit)) {
862
+ const miter_len = half_width * miter_ratio;
863
+ const tip = PointF.init(vertex.x + mid.x * miter_len, vertex.y + mid.y * miter_len);
864
+ const wedge = [4]PointF{ vertex, a, tip, b };
865
+ try raster.addPolygon(&wedge);
866
+ } else {
867
+ const bevel = [3]PointF{ vertex, a, b };
868
+ try raster.addPolygon(&bevel);
869
+ }
870
+ },
871
+ }
872
+ }
873
+
874
+ /// A full disc (round cap / round join) as four kappa-cubic quarter arcs
875
+ /// flattened at `tolerance`. Overlap with segment quads is harmless: all
876
+ /// pieces share one orientation under the nonzero rule.
877
+ fn emitDisc(raster: *Rasterizer, center: PointF, radius: f32, tolerance: f32) Error!void {
878
+ if (radius <= 0) return;
879
+ var points: [max_piece_points]PointF = undefined;
880
+ var count: usize = 0;
881
+ const r = radius;
882
+ const k = circle_kappa * r;
883
+ const cx = center.x;
884
+ const cy = center.y;
885
+ const anchors = [5]PointF{
886
+ PointF.init(cx + r, cy),
887
+ PointF.init(cx, cy + r),
888
+ PointF.init(cx - r, cy),
889
+ PointF.init(cx, cy - r),
890
+ PointF.init(cx + r, cy),
891
+ };
892
+ const controls = [4][2]PointF{
893
+ .{ PointF.init(cx + r, cy + k), PointF.init(cx + k, cy + r) },
894
+ .{ PointF.init(cx - k, cy + r), PointF.init(cx - r, cy + k) },
895
+ .{ PointF.init(cx - r, cy - k), PointF.init(cx - k, cy - r) },
896
+ .{ PointF.init(cx + k, cy - r), PointF.init(cx + r, cy - k) },
897
+ };
898
+ points[count] = anchors[0];
899
+ count += 1;
900
+ var quarter: usize = 0;
901
+ while (quarter < 4) : (quarter += 1) {
902
+ const p0 = anchors[quarter];
903
+ const c1 = controls[quarter][0];
904
+ const c2 = controls[quarter][1];
905
+ const p3 = anchors[quarter + 1];
906
+ const segments = cubicSegmentCount(p0, c1, c2, p3, tolerance);
907
+ var index: usize = 1;
908
+ while (index <= segments) : (index += 1) {
909
+ const t = @as(f32, @floatFromInt(index)) / @as(f32, @floatFromInt(segments));
910
+ if (count >= max_piece_points) return error.VectorPathTooComplex;
911
+ points[count] = cubicPoint(p0, c1, c2, p3, t);
912
+ count += 1;
913
+ }
914
+ }
915
+ // The final point equals the first; addPolygon closes the ring, so
916
+ // drop it to avoid a zero-length edge.
917
+ if (count > 1) count -= 1;
918
+ try raster.addPolygon(points[0..count]);
919
+ }