@ng-native/desktop 0.0.0-stage → 0.23.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 (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +276 -0
  14. package/native/appkit/CMakeLists.txt +109 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-view.h +15 -0
  41. package/native/appkit/camera-view.mm +130 -0
  42. package/native/appkit/camera.h +39 -0
  43. package/native/appkit/camera.mm +362 -0
  44. package/native/appkit/cg-canvas.h +17 -0
  45. package/native/appkit/cg-canvas.mm +518 -0
  46. package/native/appkit/component-view.h +17 -0
  47. package/native/appkit/crypto.h +19 -0
  48. package/native/appkit/crypto.mm +78 -0
  49. package/native/appkit/desktop-expo.h +29 -0
  50. package/native/appkit/desktop-expo.mm +385 -0
  51. package/native/appkit/expo-image-drawing.h +46 -0
  52. package/native/appkit/expo-image-drawing.mm +131 -0
  53. package/native/appkit/expo-image-view.h +15 -0
  54. package/native/appkit/expo-image-view.mm +182 -0
  55. package/native/appkit/expo-ui-renderer.h +68 -0
  56. package/native/appkit/expo-ui-renderer.mm +101 -0
  57. package/native/appkit/expo-ui-view.h +26 -0
  58. package/native/appkit/expo-ui-view.mm +134 -0
  59. package/native/appkit/expo-ui.swift +1284 -0
  60. package/native/appkit/file-system.h +16 -0
  61. package/native/appkit/file-system.mm +140 -0
  62. package/native/appkit/fonts.h +12 -0
  63. package/native/appkit/fonts.mm +48 -0
  64. package/native/appkit/geocoder.h +19 -0
  65. package/native/appkit/geocoder.mm +101 -0
  66. package/native/appkit/glass-effect.h +22 -0
  67. package/native/appkit/glass-effect.mm +63 -0
  68. package/native/appkit/glass-view.h +8 -0
  69. package/native/appkit/glass-view.mm +65 -0
  70. package/native/appkit/header-toolbar.h +60 -0
  71. package/native/appkit/header-toolbar.mm +629 -0
  72. package/native/appkit/host-view.h +54 -0
  73. package/native/appkit/host-view.mm +1200 -0
  74. package/native/appkit/image-manipulator.h +14 -0
  75. package/native/appkit/image-manipulator.mm +191 -0
  76. package/native/appkit/image-request.h +22 -0
  77. package/native/appkit/image-request.mm +62 -0
  78. package/native/appkit/image-view.h +8 -0
  79. package/native/appkit/image-view.mm +142 -0
  80. package/native/appkit/input-replay.h +25 -0
  81. package/native/appkit/input-replay.mm +418 -0
  82. package/native/appkit/language-model.h +14 -0
  83. package/native/appkit/language-model.mm +99 -0
  84. package/native/appkit/language-model.swift +472 -0
  85. package/native/appkit/local-authentication.h +15 -0
  86. package/native/appkit/local-authentication.mm +133 -0
  87. package/native/appkit/location.h +16 -0
  88. package/native/appkit/location.mm +214 -0
  89. package/native/appkit/main.mm +1515 -0
  90. package/native/appkit/map-view.h +15 -0
  91. package/native/appkit/map-view.mm +109 -0
  92. package/native/appkit/maps.h +35 -0
  93. package/native/appkit/maps.mm +343 -0
  94. package/native/appkit/menu-bar.h +22 -0
  95. package/native/appkit/menu-bar.mm +208 -0
  96. package/native/appkit/modal-focus.h +16 -0
  97. package/native/appkit/modal-focus.mm +26 -0
  98. package/native/appkit/modal-host-view.h +79 -0
  99. package/native/appkit/modal-host-view.mm +442 -0
  100. package/native/appkit/native-controls.h +22 -0
  101. package/native/appkit/native-controls.mm +431 -0
  102. package/native/appkit/network-state.h +12 -0
  103. package/native/appkit/network-state.mm +64 -0
  104. package/native/appkit/network.h +14 -0
  105. package/native/appkit/network.mm +396 -0
  106. package/native/appkit/notifications.h +19 -0
  107. package/native/appkit/notifications.mm +309 -0
  108. package/native/appkit/photo-library.h +33 -0
  109. package/native/appkit/photo-library.mm +548 -0
  110. package/native/appkit/pointer-buttons.h +32 -0
  111. package/native/appkit/presentation-label.h +15 -0
  112. package/native/appkit/presentation-label.mm +12 -0
  113. package/native/appkit/presented-screen.h +19 -0
  114. package/native/appkit/presented-screen.mm +181 -0
  115. package/native/appkit/printing.h +42 -0
  116. package/native/appkit/printing.mm +393 -0
  117. package/native/appkit/push.h +19 -0
  118. package/native/appkit/push.mm +81 -0
  119. package/native/appkit/red-box.h +26 -0
  120. package/native/appkit/red-box.mm +387 -0
  121. package/native/appkit/screen-capture.h +21 -0
  122. package/native/appkit/screen-capture.mm +92 -0
  123. package/native/appkit/scroll-view.h +13 -0
  124. package/native/appkit/scroll-view.mm +251 -0
  125. package/native/appkit/scrollers.h +25 -0
  126. package/native/appkit/scrollers.mm +43 -0
  127. package/native/appkit/search-field.h +34 -0
  128. package/native/appkit/search-field.mm +291 -0
  129. package/native/appkit/search-toolbar-item.h +19 -0
  130. package/native/appkit/search-toolbar-item.mm +269 -0
  131. package/native/appkit/share-picker.h +34 -0
  132. package/native/appkit/share-picker.mm +121 -0
  133. package/native/appkit/split-view.h +77 -0
  134. package/native/appkit/split-view.mm +1206 -0
  135. package/native/appkit/store-review.h +13 -0
  136. package/native/appkit/store-review.mm +28 -0
  137. package/native/appkit/surface-view.h +45 -0
  138. package/native/appkit/surface-view.mm +558 -0
  139. package/native/appkit/svg-view.h +17 -0
  140. package/native/appkit/svg-view.mm +57 -0
  141. package/native/appkit/symbol-drawing.h +24 -0
  142. package/native/appkit/symbol-drawing.mm +117 -0
  143. package/native/appkit/symbol-view.h +8 -0
  144. package/native/appkit/symbol-view.mm +33 -0
  145. package/native/appkit/system-access.h +34 -0
  146. package/native/appkit/system-access.mm +210 -0
  147. package/native/appkit/system.h +24 -0
  148. package/native/appkit/system.mm +403 -0
  149. package/native/appkit/test-launch.h +36 -0
  150. package/native/appkit/test-launch.mm +115 -0
  151. package/native/appkit/text-input-field.h +76 -0
  152. package/native/appkit/text-input-field.mm +444 -0
  153. package/native/appkit/text-layout.h +58 -0
  154. package/native/appkit/text-layout.mm +477 -0
  155. package/native/appkit/text-recognition.h +13 -0
  156. package/native/appkit/text-recognition.mm +102 -0
  157. package/native/appkit/tracking.h +13 -0
  158. package/native/appkit/tracking.mm +51 -0
  159. package/native/appkit/tray-and-dock.h +45 -0
  160. package/native/appkit/tray-and-dock.mm +130 -0
  161. package/native/appkit/undo.h +23 -0
  162. package/native/appkit/undo.mm +125 -0
  163. package/native/appkit/updates.h +17 -0
  164. package/native/appkit/updates.mm +71 -0
  165. package/native/appkit/video-view.h +10 -0
  166. package/native/appkit/video-view.mm +42 -0
  167. package/native/appkit/video.h +38 -0
  168. package/native/appkit/video.mm +404 -0
  169. package/native/appkit/web-browser.h +12 -0
  170. package/native/appkit/web-browser.mm +97 -0
  171. package/native/appkit/web-view-component.h +9 -0
  172. package/native/appkit/web-view-component.mm +56 -0
  173. package/native/appkit/web-view.h +31 -0
  174. package/native/appkit/web-view.mm +394 -0
  175. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  176. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  177. package/native/cmake/libraries.cmake +109 -0
  178. package/native/cmake/linked-modules.cmake +112 -0
  179. package/native/cmake/react-native.cmake +175 -0
  180. package/native/cmake/runtime-dependencies.cmake +34 -0
  181. package/native/cmake/swift.cmake +43 -0
  182. package/native/cmake/third-party.cmake +74 -0
  183. package/native/core/include/ngn/absent-views.h +27 -0
  184. package/native/core/include/ngn/accessibility.h +140 -0
  185. package/native/core/include/ngn/address-search.h +69 -0
  186. package/native/core/include/ngn/animation-frames.h +39 -0
  187. package/native/core/include/ngn/app-info.h +52 -0
  188. package/native/core/include/ngn/app-menus.h +146 -0
  189. package/native/core/include/ngn/app-windows.h +181 -0
  190. package/native/core/include/ngn/apple-authentication.h +116 -0
  191. package/native/core/include/ngn/asset.h +28 -0
  192. package/native/core/include/ngn/audio.h +125 -0
  193. package/native/core/include/ngn/back-swipe.h +112 -0
  194. package/native/core/include/ngn/battery.h +48 -0
  195. package/native/core/include/ngn/bytes.h +59 -0
  196. package/native/core/include/ngn/calendar.h +180 -0
  197. package/native/core/include/ngn/camera.h +128 -0
  198. package/native/core/include/ngn/control-sizes.h +111 -0
  199. package/native/core/include/ngn/core-modules.h +105 -0
  200. package/native/core/include/ngn/crypto.h +38 -0
  201. package/native/core/include/ngn/default-menu.h +91 -0
  202. package/native/core/include/ngn/desktop-expo.h +193 -0
  203. package/native/core/include/ngn/desktop-modal.h +109 -0
  204. package/native/core/include/ngn/desktop-props.h +75 -0
  205. package/native/core/include/ngn/desktop-view-props.h +110 -0
  206. package/native/core/include/ngn/dev-server.h +33 -0
  207. package/native/core/include/ngn/download.h +24 -0
  208. package/native/core/include/ngn/expo-image.h +192 -0
  209. package/native/core/include/ngn/expo-ui.h +133 -0
  210. package/native/core/include/ngn/file-system.h +274 -0
  211. package/native/core/include/ngn/focus-order.h +63 -0
  212. package/native/core/include/ngn/fonts.h +64 -0
  213. package/native/core/include/ngn/gestures.h +305 -0
  214. package/native/core/include/ngn/glass.h +67 -0
  215. package/native/core/include/ngn/header-bar.h +112 -0
  216. package/native/core/include/ngn/host-state.h +243 -0
  217. package/native/core/include/ngn/image-manipulator.h +70 -0
  218. package/native/core/include/ngn/input-dispatcher.h +211 -0
  219. package/native/core/include/ngn/input-script.h +56 -0
  220. package/native/core/include/ngn/input.h +112 -0
  221. package/native/core/include/ngn/key-value-store.h +41 -0
  222. package/native/core/include/ngn/language-model.h +69 -0
  223. package/native/core/include/ngn/launch-frame.h +47 -0
  224. package/native/core/include/ngn/launch-settings.h +98 -0
  225. package/native/core/include/ngn/layout-animations.h +24 -0
  226. package/native/core/include/ngn/library-components.h +13 -0
  227. package/native/core/include/ngn/library-glue.h +53 -0
  228. package/native/core/include/ngn/live-regions.h +54 -0
  229. package/native/core/include/ngn/local-authentication.h +74 -0
  230. package/native/core/include/ngn/location.h +89 -0
  231. package/native/core/include/ngn/maps.h +226 -0
  232. package/native/core/include/ngn/module-registry.h +51 -0
  233. package/native/core/include/ngn/mounting-backend.h +96 -0
  234. package/native/core/include/ngn/mounting-manager.h +155 -0
  235. package/native/core/include/ngn/native-controls.h +161 -0
  236. package/native/core/include/ngn/native-module.h +109 -0
  237. package/native/core/include/ngn/network-state.h +44 -0
  238. package/native/core/include/ngn/network.h +100 -0
  239. package/native/core/include/ngn/notifications.h +185 -0
  240. package/native/core/include/ngn/photo-library.h +130 -0
  241. package/native/core/include/ngn/printing.h +69 -0
  242. package/native/core/include/ngn/processes.h +114 -0
  243. package/native/core/include/ngn/red-box.h +119 -0
  244. package/native/core/include/ngn/reload-thread.h +258 -0
  245. package/native/core/include/ngn/runtime.h +161 -0
  246. package/native/core/include/ngn/safe-areas.h +77 -0
  247. package/native/core/include/ngn/screen-capture.h +35 -0
  248. package/native/core/include/ngn/screen-presentation.h +85 -0
  249. package/native/core/include/ngn/screen-stacks.h +80 -0
  250. package/native/core/include/ngn/scrollers.h +106 -0
  251. package/native/core/include/ngn/sharing.h +39 -0
  252. package/native/core/include/ngn/spawn-rules.h +78 -0
  253. package/native/core/include/ngn/splash-screen.h +41 -0
  254. package/native/core/include/ngn/split-view.h +270 -0
  255. package/native/core/include/ngn/sqlite.h +38 -0
  256. package/native/core/include/ngn/store-review.h +26 -0
  257. package/native/core/include/ngn/surface-book.h +101 -0
  258. package/native/core/include/ngn/svg.h +393 -0
  259. package/native/core/include/ngn/symbol.h +90 -0
  260. package/native/core/include/ngn/system.h +102 -0
  261. package/native/core/include/ngn/tabs-host.h +171 -0
  262. package/native/core/include/ngn/text-input.h +125 -0
  263. package/native/core/include/ngn/text-measurer.h +38 -0
  264. package/native/core/include/ngn/text-recognition.h +55 -0
  265. package/native/core/include/ngn/tracking.h +34 -0
  266. package/native/core/include/ngn/tray-and-dock.h +107 -0
  267. package/native/core/include/ngn/undo.h +37 -0
  268. package/native/core/include/ngn/updates.h +253 -0
  269. package/native/core/include/ngn/video.h +126 -0
  270. package/native/core/include/ngn/view-box.h +126 -0
  271. package/native/core/include/ngn/view-transform.h +26 -0
  272. package/native/core/include/ngn/view-tree.h +85 -0
  273. package/native/core/include/ngn/web-browser.h +45 -0
  274. package/native/core/include/ngn/web-view.h +133 -0
  275. package/native/core/include/ngn/worklet-host.h +98 -0
  276. package/native/core/src/absent-views.cpp +41 -0
  277. package/native/core/src/accessibility.cpp +374 -0
  278. package/native/core/src/animation-frames.cpp +49 -0
  279. package/native/core/src/app-menus.cpp +352 -0
  280. package/native/core/src/app-windows.cpp +444 -0
  281. package/native/core/src/back-swipe.cpp +91 -0
  282. package/native/core/src/bytes.cpp +158 -0
  283. package/native/core/src/control-sizes.cpp +232 -0
  284. package/native/core/src/default-menu.cpp +96 -0
  285. package/native/core/src/desktop-modal.cpp +162 -0
  286. package/native/core/src/desktop-view-props.cpp +80 -0
  287. package/native/core/src/dev-server-helper.cpp +152 -0
  288. package/native/core/src/dev-server.cpp +103 -0
  289. package/native/core/src/download.cpp +53 -0
  290. package/native/core/src/expo-image.cpp +548 -0
  291. package/native/core/src/expo-ui.cpp +203 -0
  292. package/native/core/src/focus-order.cpp +441 -0
  293. package/native/core/src/gestures.cpp +1806 -0
  294. package/native/core/src/glass.cpp +49 -0
  295. package/native/core/src/header-bar.cpp +104 -0
  296. package/native/core/src/host-services.cpp +50 -0
  297. package/native/core/src/host-state.cpp +61 -0
  298. package/native/core/src/input-dispatcher.cpp +727 -0
  299. package/native/core/src/input-script.cpp +123 -0
  300. package/native/core/src/key-value-store.cpp +169 -0
  301. package/native/core/src/launch-settings.cpp +148 -0
  302. package/native/core/src/layout-animations.cpp +62 -0
  303. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  304. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  305. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  306. package/native/core/src/libraries/rnscreens.cpp +125 -0
  307. package/native/core/src/libraries/rnsvg.cpp +345 -0
  308. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  309. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  310. package/native/core/src/library-glue.cpp +35 -0
  311. package/native/core/src/live-regions.cpp +122 -0
  312. package/native/core/src/module-registry.cpp +50 -0
  313. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  314. package/native/core/src/modules/address-search.cpp +219 -0
  315. package/native/core/src/modules/alert-manager.cpp +140 -0
  316. package/native/core/src/modules/app-info.cpp +74 -0
  317. package/native/core/src/modules/app-modules.cpp +405 -0
  318. package/native/core/src/modules/apple-authentication.cpp +188 -0
  319. package/native/core/src/modules/asset.cpp +111 -0
  320. package/native/core/src/modules/async-storage.cpp +192 -0
  321. package/native/core/src/modules/audio.cpp +541 -0
  322. package/native/core/src/modules/battery.cpp +88 -0
  323. package/native/core/src/modules/calendar.cpp +457 -0
  324. package/native/core/src/modules/camera.cpp +221 -0
  325. package/native/core/src/modules/core-modules.cpp +62 -0
  326. package/native/core/src/modules/crypto.cpp +117 -0
  327. package/native/core/src/modules/desktop-expo.cpp +527 -0
  328. package/native/core/src/modules/file-system.cpp +941 -0
  329. package/native/core/src/modules/fonts.cpp +163 -0
  330. package/native/core/src/modules/image-loader.cpp +105 -0
  331. package/native/core/src/modules/image-manipulator.cpp +305 -0
  332. package/native/core/src/modules/language-model.cpp +195 -0
  333. package/native/core/src/modules/local-authentication.cpp +140 -0
  334. package/native/core/src/modules/location.cpp +186 -0
  335. package/native/core/src/modules/maps.cpp +500 -0
  336. package/native/core/src/modules/network-state.cpp +72 -0
  337. package/native/core/src/modules/networking.cpp +387 -0
  338. package/native/core/src/modules/notifications.cpp +549 -0
  339. package/native/core/src/modules/photo-library.cpp +371 -0
  340. package/native/core/src/modules/printing.cpp +201 -0
  341. package/native/core/src/modules/screen-capture.cpp +71 -0
  342. package/native/core/src/modules/sharing.cpp +59 -0
  343. package/native/core/src/modules/splash-screen.cpp +70 -0
  344. package/native/core/src/modules/sqlite.cpp +421 -0
  345. package/native/core/src/modules/store-review.cpp +23 -0
  346. package/native/core/src/modules/text-recognition.cpp +107 -0
  347. package/native/core/src/modules/tracking.cpp +46 -0
  348. package/native/core/src/modules/undo.cpp +59 -0
  349. package/native/core/src/modules/video.cpp +366 -0
  350. package/native/core/src/modules/web-browser.cpp +100 -0
  351. package/native/core/src/modules/web-socket.cpp +266 -0
  352. package/native/core/src/mounting-manager.cpp +380 -0
  353. package/native/core/src/native-controls.cpp +129 -0
  354. package/native/core/src/native-module.cpp +250 -0
  355. package/native/core/src/network.cpp +219 -0
  356. package/native/core/src/posix-launcher.cpp +366 -0
  357. package/native/core/src/processes.cpp +324 -0
  358. package/native/core/src/red-box-errors.js +83 -0
  359. package/native/core/src/red-box.cpp +306 -0
  360. package/native/core/src/runtime.cpp +422 -0
  361. package/native/core/src/safe-areas.cpp +130 -0
  362. package/native/core/src/screen-presentation.cpp +61 -0
  363. package/native/core/src/screen-stacks.cpp +254 -0
  364. package/native/core/src/scrollers.cpp +181 -0
  365. package/native/core/src/spawn-rules.cpp +292 -0
  366. package/native/core/src/split-view.cpp +343 -0
  367. package/native/core/src/svg.cpp +1883 -0
  368. package/native/core/src/symbol.cpp +94 -0
  369. package/native/core/src/tabs-host.cpp +234 -0
  370. package/native/core/src/text-input.cpp +126 -0
  371. package/native/core/src/text-layout-manager.cpp +63 -0
  372. package/native/core/src/tray-and-dock.cpp +154 -0
  373. package/native/core/src/updates.cpp +965 -0
  374. package/native/core/src/view-box.cpp +788 -0
  375. package/native/core/src/view-transform.cpp +45 -0
  376. package/native/core/src/view-tree.cpp +234 -0
  377. package/native/core/src/web-view.cpp +143 -0
  378. package/native/core/src/worklet-host.cpp +10 -0
  379. package/native/scripts/build-hermes.sh +44 -0
  380. package/native/scripts/build.sh +171 -0
  381. package/native/scripts/cache-dir.sh +18 -0
  382. package/native/scripts/codegen-library.sh +193 -0
  383. package/native/scripts/compiler-launcher.sh +17 -0
  384. package/native/scripts/fetch.sh +174 -0
  385. package/native/scripts/hermes-built.sh +10 -0
  386. package/native/scripts/host-dir.sh +16 -0
  387. package/native/scripts/macos-target.sh +8 -0
  388. package/native/scripts/make-prebuilt.mjs +360 -0
  389. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  390. package/native/shim/openssl/sha.h +22 -0
  391. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  392. package/package.json +70 -3
  393. package/prebuilt.json +33 -0
@@ -0,0 +1,788 @@
1
+ #include "ngn/view-box.h"
2
+
3
+ #include <react/renderer/graphics/BackgroundImage.h>
4
+
5
+ #include <algorithm>
6
+ #include <cmath>
7
+ #include <numbers>
8
+
9
+ using namespace facebook::react;
10
+
11
+ namespace ngn {
12
+
13
+ namespace {
14
+
15
+ // A quarter ellipse as one cubic: the control points' distance along each tangent.
16
+ constexpr double kKappa = 0.5522847498307936;
17
+
18
+ std::optional<uint32_t> argbOf(const SharedColor &color) {
19
+ if (!color) {
20
+ return std::nullopt;
21
+ }
22
+ auto c = colorComponentsFromColor(color);
23
+ auto channel = [](float v) { return static_cast<uint32_t>(std::lround(std::clamp(v, 0.0f, 1.0f) * 255)); };
24
+ return channel(c.alpha) << 24 | channel(c.red) << 16 | channel(c.green) << 8 | channel(c.blue);
25
+ }
26
+
27
+ bool visible(const std::optional<uint32_t> &argb) {
28
+ return argb && (*argb >> 24) != 0;
29
+ }
30
+
31
+ CornerRadii cornerRadiiOf(const BorderRadii &r) {
32
+ return {
33
+ .topLeft = {r.topLeft.horizontal, r.topLeft.vertical},
34
+ .topRight = {r.topRight.horizontal, r.topRight.vertical},
35
+ .bottomRight = {r.bottomRight.horizontal, r.bottomRight.vertical},
36
+ .bottomLeft = {r.bottomLeft.horizontal, r.bottomLeft.vertical},
37
+ };
38
+ }
39
+
40
+ EdgeWidths widthsOf(const BorderWidths &w) {
41
+ return {.top = w.top, .right = w.right, .bottom = w.bottom, .left = w.left};
42
+ }
43
+
44
+ SvgRect inset(const SvgRect &r, const EdgeWidths &w) {
45
+ return {r.x + w.left, r.y + w.top, std::max(0.0, r.width - w.left - w.right), std::max(0.0, r.height - w.top - w.bottom)};
46
+ }
47
+
48
+ SvgPath rectPath(const SvgRect &r) {
49
+ return roundedRect(r, {});
50
+ }
51
+
52
+ void append(SvgPath &to, const SvgPath &from) {
53
+ to.segments.insert(to.segments.end(), from.segments.begin(), from.segments.end());
54
+ }
55
+
56
+ // Where the line from `from` to `to` crosses the ellipse in `bounds`: the crossing nearer the
57
+ // ellipse's start (`second` false) or the other one, as RCTEllipseGetIntersectionsWithLine.
58
+ std::optional<std::pair<double, double>> ellipseCrossing(const SvgRect &bounds, double fx, double fy, double tx, double ty, bool second) {
59
+ double cx = bounds.x + bounds.width / 2, cy = bounds.y + bounds.height / 2;
60
+ fx -= cx;
61
+ fy -= cy;
62
+ tx -= cx;
63
+ ty -= cy;
64
+ double m = (ty - fy) / (tx - fx);
65
+ double a = bounds.width / 2, b = bounds.height / 2;
66
+ double c = fy - m * fx;
67
+ double A = b * b + a * a * m * m;
68
+ double B = 2 * a * a * c * m;
69
+ double D = std::sqrt((a * a * (b * b - c * c)) / A + std::pow(B / (2 * A), 2));
70
+ double x = -B / (2 * A) + (second ? -D : D);
71
+ double y = m * x + c;
72
+ if (std::isnan(x) || std::isnan(y)) {
73
+ return std::nullopt;
74
+ }
75
+ return std::pair{x + cx, y + cy};
76
+ }
77
+
78
+ using XY = std::pair<double, double>;
79
+
80
+ SvgPath polygon(std::initializer_list<XY> points) {
81
+ SvgPath path;
82
+ bool first = true;
83
+ for (const auto &[x, y] : points) {
84
+ first ? path.moveTo(x, y) : path.lineTo(x, y);
85
+ first = false;
86
+ }
87
+ path.close();
88
+ return path;
89
+ }
90
+
91
+ // The four sides' trapezoids, as RCTGetSolidBorderImage cuts them: from the box's corners to
92
+ // where the line from each corner meets the padding box's corner, or its curve.
93
+ void drawSides(const SvgRect &box, const EdgeWidths &w, const CornerRadii &radii, const std::optional<uint32_t> colors[4], SvgCanvas &canvas) {
94
+ double W = box.width, H = box.height;
95
+ auto inner = innerRadii(radii, w);
96
+ XY topLeft{w.left, w.top};
97
+ if (inner.topLeft.x > 0 && inner.topLeft.y > 0) {
98
+ if (auto p = ellipseCrossing({w.left, w.top, 2 * inner.topLeft.x, 2 * inner.topLeft.y}, 0, 0, w.left, w.top, true)) {
99
+ topLeft = *p;
100
+ }
101
+ }
102
+ XY bottomLeft{w.left, H - w.bottom};
103
+ if (inner.bottomLeft.x > 0 && inner.bottomLeft.y > 0) {
104
+ SvgRect e{w.left, H - w.bottom - 2 * inner.bottomLeft.y, 2 * inner.bottomLeft.x, 2 * inner.bottomLeft.y};
105
+ if (auto p = ellipseCrossing(e, 0, H, w.left, H - w.bottom, true)) {
106
+ bottomLeft = *p;
107
+ }
108
+ }
109
+ XY topRight{W - w.right, w.top};
110
+ if (inner.topRight.x > 0 && inner.topRight.y > 0) {
111
+ SvgRect e{W - w.right - 2 * inner.topRight.x, w.top, 2 * inner.topRight.x, 2 * inner.topRight.y};
112
+ if (auto p = ellipseCrossing(e, W, 0, W - w.right, w.top, false)) {
113
+ topRight = *p;
114
+ }
115
+ }
116
+ XY bottomRight{W - w.right, H - w.bottom};
117
+ if (inner.bottomRight.x > 0 && inner.bottomRight.y > 0) {
118
+ SvgRect e{W - w.right - 2 * inner.bottomRight.x, H - w.bottom - 2 * inner.bottomRight.y, 2 * inner.bottomRight.x, 2 * inner.bottomRight.y};
119
+ if (auto p = ellipseCrossing(e, W, H, W - w.right, H - w.bottom, false)) {
120
+ bottomRight = *p;
121
+ }
122
+ }
123
+
124
+ // React Native iOS's order: right, bottom, left, top. Sides of one colour share a path.
125
+ struct Side {
126
+ double width;
127
+ const std::optional<uint32_t> &color;
128
+ SvgPath shape;
129
+ };
130
+ Side sides[] = {
131
+ {w.right, colors[1], polygon({{W, 0}, topRight, bottomRight, {W, H}})},
132
+ {w.bottom, colors[2], polygon({{0, H}, bottomLeft, bottomRight, {W, H}})},
133
+ {w.left, colors[3], polygon({{0, 0}, topLeft, bottomLeft, {0, H}})},
134
+ {w.top, colors[0], polygon({{0, 0}, topLeft, topRight, {W, 0}})},
135
+ };
136
+ std::vector<std::pair<uint32_t, SvgPath>> groups;
137
+ for (auto &side : sides) {
138
+ if (side.width <= 0 || !visible(side.color)) {
139
+ continue;
140
+ }
141
+ auto group = std::find_if(groups.begin(), groups.end(), [&](const auto &g) { return g.first == *side.color; });
142
+ if (group == groups.end()) {
143
+ groups.emplace_back(*side.color, side.shape);
144
+ } else {
145
+ append(group->second, side.shape);
146
+ }
147
+ }
148
+ for (const auto &[argb, path] : groups) {
149
+ canvas.fill(path, false, SvgPaint{.argb = argb});
150
+ }
151
+ }
152
+
153
+ void drawBorder(const SvgRect &box, const BorderMetrics &metrics, const CornerRadii &radii, SvgCanvas &canvas) {
154
+ auto w = widthsOf(metrics.borderWidths);
155
+ const auto &c = metrics.borderColors;
156
+ std::optional<uint32_t> colors[4] = {argbOf(c.top), argbOf(c.right), argbOf(c.bottom), argbOf(c.left)};
157
+ double widths[4] = {w.top, w.right, w.bottom, w.left};
158
+ std::optional<uint32_t> only;
159
+ bool oneColor = true, any = false, hidden = false;
160
+ for (int i = 0; i < 4; i++) {
161
+ if (widths[i] <= 0) {
162
+ continue;
163
+ }
164
+ if (!visible(colors[i])) {
165
+ hidden = true;
166
+ continue;
167
+ }
168
+ any = true;
169
+ if (only && *only != *colors[i]) {
170
+ oneColor = false;
171
+ }
172
+ only = colors[i];
173
+ }
174
+ if (!any) {
175
+ return;
176
+ }
177
+
178
+ auto style = metrics.borderStyles.left;
179
+ if (style == BorderStyle::Dashed || style == BorderStyle::Dotted) {
180
+ // One colour, stroked along the middle of the widest side, as React Native iOS draws it.
181
+ if (!oneColor) {
182
+ return;
183
+ }
184
+ double line = *std::max_element(std::begin(widths), std::end(widths));
185
+ SvgRect middle{box.x + line / 2, box.y + line / 2, box.width - line, box.height - line};
186
+ bool uniform = metrics.borderWidths.isUniform();
187
+ if (!uniform) {
188
+ canvas.save();
189
+ auto ring = rectPath(box);
190
+ append(ring, roundedRect(inset(box, w), innerRadii(radii, w)));
191
+ canvas.clip(ring, true);
192
+ }
193
+ double dash = (style == BorderStyle::Dashed ? 3 : 1) * line;
194
+ canvas.stroke(roundedRect(middle, radii), SvgStroke{.width = line, .dashes = {dash, dash}}, SvgPaint{.argb = *only});
195
+ if (!uniform) {
196
+ canvas.restore();
197
+ }
198
+ return;
199
+ }
200
+
201
+ auto ring = roundedRect(box, radii);
202
+ append(ring, roundedRect(inset(box, w), innerRadii(radii, w)));
203
+ // The whole ring in one fill only when every side with a width shows that colour.
204
+ if (oneColor && !hidden) {
205
+ canvas.fill(ring, true, SvgPaint{.argb = *only});
206
+ return;
207
+ }
208
+ canvas.save();
209
+ canvas.clip(ring, true);
210
+ drawSides(box, w, radii, colors, canvas);
211
+ canvas.restore();
212
+ }
213
+
214
+ // Both points of a gradient line, from React Native iOS's getPointsFromAngle (Chromium's): through
215
+ // the centre, long enough that the corners it points between take the first and last colours.
216
+ std::pair<XY, XY> gradientLine(double angle, double w, double h) {
217
+ angle = std::fmod(angle, 360.0);
218
+ if (angle < 0) {
219
+ angle += 360;
220
+ }
221
+ if (angle == 0) {
222
+ return {{0, h}, {0, 0}};
223
+ }
224
+ if (angle == 90) {
225
+ return {{0, 0}, {w, 0}};
226
+ }
227
+ if (angle == 180) {
228
+ return {{0, 0}, {0, h}};
229
+ }
230
+ if (angle == 270) {
231
+ return {{w, 0}, {0, 0}};
232
+ }
233
+ double slope = std::tan((90 - angle) * std::numbers::pi / 180);
234
+ double perpendicular = -1 / slope;
235
+ double hw = w / 2, hh = h / 2;
236
+ XY corner = angle < 90 ? XY{hw, hh} : angle < 180 ? XY{hw, -hh} : angle < 270 ? XY{-hw, -hh} : XY{-hw, hh};
237
+ double c = corner.second - perpendicular * corner.first;
238
+ double x = c / (slope - perpendicular);
239
+ double y = perpendicular * x + c;
240
+ return {{hw - x, hh + y}, {hw + x, hh - y}};
241
+ }
242
+
243
+ double keywordAngle(GradientKeyword keyword, double w, double h) {
244
+ double deg = 180 / std::numbers::pi;
245
+ switch (keyword) {
246
+ case GradientKeyword::ToTopRight:
247
+ return 90 - std::atan(w / h) * deg;
248
+ case GradientKeyword::ToBottomRight:
249
+ return std::atan(w / h) * deg + 90;
250
+ case GradientKeyword::ToTopLeft:
251
+ return std::atan(w / h) * deg + 270;
252
+ case GradientKeyword::ToBottomLeft:
253
+ return std::atan(h / w) * deg + 180;
254
+ }
255
+ return 180;
256
+ }
257
+
258
+ uint32_t mix(uint32_t from, uint32_t to, double t) {
259
+ uint32_t out = 0;
260
+ for (int shift = 0; shift < 32; shift += 8) {
261
+ double a = (from >> shift) & 0xff, b = (to >> shift) & 0xff;
262
+ out |= static_cast<uint32_t>(std::lround(a + (b - a) * t)) << shift;
263
+ }
264
+ return out;
265
+ }
266
+
267
+ struct Stop {
268
+ std::optional<uint32_t> color;
269
+ double position{0};
270
+ };
271
+
272
+ // React Native's processColorTransitionHints: a hint between two stops is replaced by nine stops
273
+ // on the curve that puts the colours' midpoint at the hint.
274
+ std::vector<Stop> expandHints(const std::vector<Stop> &original) {
275
+ std::vector<Stop> stops = original;
276
+ int offset = 0;
277
+ for (size_t i = 1; i + 1 < original.size(); ++i) {
278
+ if (original[i].color) {
279
+ continue;
280
+ }
281
+ size_t x = i + offset;
282
+ double left = stops[x - 1].position, right = stops[x + 1].position, at = stops[x].position;
283
+ double leftDist = at - left, rightDist = right - at, total = right - left;
284
+ auto leftColor = stops[x - 1].color.value_or(0), rightColor = stops[x + 1].color.value_or(0);
285
+ if (std::abs(leftDist - rightDist) < 1e-6) {
286
+ stops.erase(stops.begin() + x);
287
+ --offset;
288
+ continue;
289
+ }
290
+ if (std::abs(leftDist) < 1e-6) {
291
+ stops[x].color = rightColor;
292
+ continue;
293
+ }
294
+ if (std::abs(rightDist) < 1e-6) {
295
+ stops[x].color = leftColor;
296
+ continue;
297
+ }
298
+ std::vector<Stop> added;
299
+ if (leftDist > rightDist) {
300
+ for (int y = 0; y < 7; ++y) {
301
+ added.push_back({{}, left + leftDist * ((7.0 + y) / 13.0)});
302
+ }
303
+ added.push_back({{}, at + rightDist / 3.0});
304
+ added.push_back({{}, at + rightDist * 2.0 / 3.0});
305
+ } else {
306
+ added.push_back({{}, left + leftDist / 3.0});
307
+ added.push_back({{}, left + leftDist * 2.0 / 3.0});
308
+ for (int y = 0; y < 7; ++y) {
309
+ added.push_back({{}, at + rightDist * (y / 13.0)});
310
+ }
311
+ }
312
+ double ratio = std::log(0.5) / std::log(leftDist / total);
313
+ for (auto &stop : added) {
314
+ double weight = std::pow((stop.position - left) / total, ratio);
315
+ stop.color = std::isfinite(weight) ? mix(leftColor, rightColor, weight) : leftColor;
316
+ }
317
+ stops.erase(stops.begin() + x);
318
+ stops.insert(stops.begin() + x, added.begin(), added.end());
319
+ offset += 8;
320
+ }
321
+ return stops;
322
+ }
323
+
324
+ // Stops that reach before 0 or past 1 move the gradient's ends out to them; nothing is cut off.
325
+ // Answers the start and end along the old line (0 and 1 when nothing moves).
326
+ std::pair<double, double> spanOf(std::vector<std::pair<double, uint32_t>> &stops, bool allowBefore) {
327
+ if (stops.empty()) {
328
+ return {0, 1};
329
+ }
330
+ double from = allowBefore ? std::min(0.0, stops.front().first) : 0.0;
331
+ double to = std::max(1.0, stops.back().first);
332
+ if (from == 0 && to == 1) {
333
+ return {0, 1};
334
+ }
335
+ for (auto &stop : stops) {
336
+ stop.first = (std::max(stop.first, from) - from) / (to - from);
337
+ }
338
+ return {from, to};
339
+ }
340
+
341
+ // One axis of a background image's tiles, React Native iOS's replicator layer as positions.
342
+ std::vector<double> tileStarts(double areaStart, double areaSize, double item, double placed, BackgroundRepeatStyle repeat, double paintSize) {
343
+ if (item <= 0 || repeat == BackgroundRepeatStyle::NoRepeat) {
344
+ return {placed};
345
+ }
346
+ double step = item;
347
+ double start = placed;
348
+ if (repeat == BackgroundRepeatStyle::Space) {
349
+ double count = std::floor(areaSize / item);
350
+ if (count <= 1) {
351
+ return {placed};
352
+ }
353
+ step = item + (areaSize - count * item) / (count - 1);
354
+ start = areaStart;
355
+ }
356
+ double before = std::ceil(start / step);
357
+ std::vector<double> out;
358
+ for (double x = start - before * step; x < paintSize && out.size() < 4096; x += step) {
359
+ if (x + item > 0) {
360
+ out.push_back(x);
361
+ }
362
+ }
363
+ return out;
364
+ }
365
+
366
+ double resolve(const ValueUnit &v, double of) {
367
+ return v.unit == UnitType::Percent ? v.value * of / 100 : v.unit == UnitType::Point ? v.value : 0;
368
+ }
369
+
370
+ } // namespace
371
+
372
+ bool CornerRadii::circular() const {
373
+ auto same = [&](const Corner &c) { return c.x == topLeft.x && c.y == topLeft.x; };
374
+ return same(topLeft) && same(topRight) && same(bottomRight) && same(bottomLeft);
375
+ }
376
+
377
+ CornerRadii innerRadii(const CornerRadii &r, const EdgeWidths &w) {
378
+ auto less = [](const CornerRadii::Corner &c, double x, double y) {
379
+ return CornerRadii::Corner{std::max(0.0, c.x - x), std::max(0.0, c.y - y)};
380
+ };
381
+ return {
382
+ .topLeft = less(r.topLeft, w.left, w.top),
383
+ .topRight = less(r.topRight, w.right, w.top),
384
+ .bottomRight = less(r.bottomRight, w.right, w.bottom),
385
+ .bottomLeft = less(r.bottomLeft, w.left, w.bottom),
386
+ };
387
+ }
388
+
389
+ CornerRadii spreadRadii(const CornerRadii &r, double spread) {
390
+ // https://drafts.csswg.org/css-backgrounds/#shadow-shape, as RCTBoxShadow adjusts a radius.
391
+ auto adjust = [spread](double radius) {
392
+ double by = spread;
393
+ if (radius < std::abs(spread)) {
394
+ by *= 1 + std::pow(radius / std::abs(spread) - 1, 3);
395
+ }
396
+ return std::max(radius + by, 0.0);
397
+ };
398
+ auto corner = [&](const CornerRadii::Corner &c) { return CornerRadii::Corner{adjust(c.x), adjust(c.y)}; };
399
+ return {corner(r.topLeft), corner(r.topRight), corner(r.bottomRight), corner(r.bottomLeft)};
400
+ }
401
+
402
+ SvgPath roundedRect(const SvgRect &rect, const CornerRadii &radii, bool reversed) {
403
+ double minX = rect.x, minY = rect.y, maxX = rect.x + rect.width, maxY = rect.y + rect.height;
404
+ double w = rect.width, h = rect.height;
405
+ // RCTPathCreateWithRoundedRect's limits: each radius within what the opposite corner leaves.
406
+ auto limit = [](double r, double side, double other) { return std::max(0.0, std::min(r, side - other)); };
407
+ auto corner = [&](const CornerRadii::Corner &c, const CornerRadii::Corner &across, const CornerRadii::Corner &down) {
408
+ CornerRadii::Corner out{limit(c.x, w, across.x), limit(c.y, h, down.y)};
409
+ return out.x > 0 && out.y > 0 ? out : CornerRadii::Corner{};
410
+ };
411
+ auto tl = corner(radii.topLeft, radii.topRight, radii.bottomLeft);
412
+ auto tr = corner(radii.topRight, radii.topLeft, radii.bottomRight);
413
+ auto br = corner(radii.bottomRight, radii.bottomLeft, radii.topRight);
414
+ auto bl = corner(radii.bottomLeft, radii.bottomRight, radii.topLeft);
415
+
416
+ SvgPath path;
417
+ double cx = 0, cy = 0;
418
+ auto move = [&](double x, double y) {
419
+ path.moveTo(x, y);
420
+ cx = x;
421
+ cy = y;
422
+ };
423
+ auto line = [&](double x, double y) {
424
+ if (x != cx || y != cy) {
425
+ path.lineTo(x, y);
426
+ cx = x;
427
+ cy = y;
428
+ }
429
+ };
430
+ // A quarter ellipse from the current point to (x, y), bulging towards the corner (kx, ky).
431
+ auto arc = [&](double kx, double ky, double x, double y) {
432
+ if (x == cx && y == cy) {
433
+ return;
434
+ }
435
+ path.cubicTo(cx + (kx - cx) * kKappa, cy + (ky - cy) * kKappa, x + (kx - x) * kKappa, y + (ky - y) * kKappa, x, y);
436
+ cx = x;
437
+ cy = y;
438
+ };
439
+ if (!reversed) {
440
+ move(minX + tl.x, minY);
441
+ line(maxX - tr.x, minY);
442
+ arc(maxX, minY, maxX, minY + tr.y);
443
+ line(maxX, maxY - br.y);
444
+ arc(maxX, maxY, maxX - br.x, maxY);
445
+ line(minX + bl.x, maxY);
446
+ arc(minX, maxY, minX, maxY - bl.y);
447
+ line(minX, minY + tl.y);
448
+ arc(minX, minY, minX + tl.x, minY);
449
+ } else {
450
+ move(minX + tl.x, minY);
451
+ arc(minX, minY, minX, minY + tl.y);
452
+ line(minX, maxY - bl.y);
453
+ arc(minX, maxY, minX + bl.x, maxY);
454
+ line(maxX - br.x, maxY);
455
+ arc(maxX, maxY, maxX, maxY - br.y);
456
+ line(maxX, minY + tr.y);
457
+ arc(maxX, minY, maxX - tr.x, minY);
458
+ line(minX + tl.x, minY);
459
+ }
460
+ path.close();
461
+ return path;
462
+ }
463
+
464
+ std::vector<std::pair<double, uint32_t>> gradientStops(const std::vector<ColorStop> &colorStops, double lineLength) {
465
+ if (colorStops.empty()) {
466
+ return {};
467
+ }
468
+ auto positionOf = [&](const ValueUnit &p) -> std::optional<double> {
469
+ if (p.unit == UnitType::Point) {
470
+ return lineLength > 0 ? p.value / lineLength : 0.0;
471
+ }
472
+ if (p.unit == UnitType::Percent) {
473
+ return p.value / 100.0;
474
+ }
475
+ return std::nullopt;
476
+ };
477
+ // React Native's getFixedColorStops: the first and last default to the ends, a stop is never
478
+ // before the one before it, and the missing ones between share the space evenly.
479
+ std::vector<std::optional<double>> positions(colorStops.size());
480
+ double maxSoFar = positionOf(colorStops[0].position).value_or(0.0);
481
+ for (size_t i = 0; i < colorStops.size(); i++) {
482
+ auto p = positionOf(colorStops[i].position);
483
+ if (!p) {
484
+ if (i == 0) {
485
+ p = 0.0;
486
+ } else if (i == colorStops.size() - 1) {
487
+ p = 1.0;
488
+ }
489
+ }
490
+ if (p) {
491
+ p = std::max(*p, maxSoFar);
492
+ maxSoFar = *p;
493
+ }
494
+ positions[i] = p;
495
+ }
496
+ size_t lastDefined = 0;
497
+ for (size_t i = 1; i < positions.size(); i++) {
498
+ if (!positions[i]) {
499
+ continue;
500
+ }
501
+ size_t missing = i - lastDefined - 1;
502
+ double from = *positions[lastDefined];
503
+ for (size_t j = 1; j <= missing; j++) {
504
+ positions[lastDefined + j] = from + (*positions[i] - from) * j / (missing + 1);
505
+ }
506
+ lastDefined = i;
507
+ }
508
+ std::vector<Stop> stops;
509
+ for (size_t i = 0; i < colorStops.size(); i++) {
510
+ stops.push_back({argbOf(colorStops[i].color), positions[i].value_or(0)});
511
+ }
512
+ std::vector<std::pair<double, uint32_t>> out;
513
+ for (const auto &stop : expandHints(stops)) {
514
+ out.emplace_back(stop.position, stop.color.value_or(0));
515
+ }
516
+ return out;
517
+ }
518
+
519
+ SvgGradient linearGradient(const LinearGradient &gradient, double width, double height) {
520
+ double angle = std::holds_alternative<Float>(gradient.direction)
521
+ ? std::get<Float>(gradient.direction)
522
+ : keywordAngle(std::get<GradientKeyword>(gradient.direction), width, height);
523
+ auto [start, end] = gradientLine(angle, width, height);
524
+ double dx = end.first - start.first, dy = end.second - start.second;
525
+ SvgGradient out;
526
+ out.stops = gradientStops(gradient.colorStops, std::hypot(dx, dy));
527
+ auto [from, to] = spanOf(out.stops, true);
528
+ out.x1 = start.first + dx * from;
529
+ out.y1 = start.second + dy * from;
530
+ out.x2 = start.first + dx * to;
531
+ out.y2 = start.second + dy * to;
532
+ return out;
533
+ }
534
+
535
+ std::optional<SvgGradient> radialGradient(const RadialGradient &gradient, double width, double height) {
536
+ double cx = width / 2, cy = height / 2;
537
+ const auto &p = gradient.position;
538
+ if (p.top) {
539
+ cy = resolve(*p.top, height);
540
+ } else if (p.bottom) {
541
+ cy = height - resolve(*p.bottom, height);
542
+ }
543
+ if (p.left) {
544
+ cx = resolve(*p.left, width);
545
+ } else if (p.right) {
546
+ cx = width - resolve(*p.right, width);
547
+ }
548
+ bool circle = gradient.shape == RadialGradientShape::Circle;
549
+ using Keyword = RadialGradientSize::SizeKeyword;
550
+ auto toSide = [&](bool closest) {
551
+ double rx = closest ? std::min(cx, width - cx) : std::max(cx, width - cx);
552
+ double ry = closest ? std::min(cy, height - cy) : std::max(cy, height - cy);
553
+ if (circle) {
554
+ double r = closest ? std::min(rx, ry) : std::max(rx, ry);
555
+ return XY{r, r};
556
+ }
557
+ return XY{rx, ry};
558
+ };
559
+ auto toCorner = [&](bool closest) {
560
+ XY corners[] = {{0, 0}, {width, 0}, {width, height}, {0, height}};
561
+ XY best = corners[0];
562
+ double distance = std::hypot(cx, cy);
563
+ for (const auto &c : corners) {
564
+ double d = std::hypot(cx - c.first, cy - c.second);
565
+ if (closest ? d < distance : d > distance) {
566
+ distance = d;
567
+ best = c;
568
+ }
569
+ }
570
+ if (circle) {
571
+ return XY{distance, distance};
572
+ }
573
+ // The ellipse through that corner with the proportions of the matching side ellipse.
574
+ auto side = toSide(closest);
575
+ double ratio = side.first / side.second;
576
+ if (ratio == 0 || !std::isfinite(ratio)) {
577
+ return XY{0, 0};
578
+ }
579
+ double ox = best.first - cx, oy = best.second - cy;
580
+ double a = std::sqrt(ox * ox + oy * oy * ratio * ratio);
581
+ return XY{a, a / ratio};
582
+ };
583
+ XY radius;
584
+ if (const auto *d = std::get_if<RadialGradientSize::Dimensions>(&gradient.size.value)) {
585
+ radius = {resolve(d->x, width), resolve(d->y, height)};
586
+ if (circle) {
587
+ radius.first = radius.second = std::max(radius.first, radius.second);
588
+ }
589
+ } else {
590
+ auto keyword = std::get<Keyword>(gradient.size.value);
591
+ radius = keyword == Keyword::ClosestSide ? toSide(true)
592
+ : keyword == Keyword::FarthestSide ? toSide(false)
593
+ : keyword == Keyword::ClosestCorner ? toCorner(true)
594
+ : toCorner(false);
595
+ }
596
+ auto [rx, ry] = radius;
597
+ if (!(rx > 0) || !(ry > 0)) {
598
+ return std::nullopt;
599
+ }
600
+ SvgGradient out;
601
+ out.radial = true;
602
+ // Lengths along the gradient ray, which runs across.
603
+ out.stops = gradientStops(gradient.colorStops, rx);
604
+ auto [from, to] = spanOf(out.stops, false);
605
+ out.r = rx * to;
606
+ out.transform = SvgMatrix::translate(cx, cy) * SvgMatrix::scale(1, ry / rx);
607
+ return out;
608
+ }
609
+
610
+ std::vector<SvgRect> backgroundTiles(
611
+ const SvgRect &area,
612
+ const SvgRect &painting,
613
+ const BackgroundSize &size,
614
+ const BackgroundPosition &position,
615
+ const BackgroundRepeat &repeat) {
616
+ if (area.width <= 0 || area.height <= 0 || painting.width <= 0 || painting.height <= 0) {
617
+ return {};
618
+ }
619
+ double w = area.width, h = area.height;
620
+ if (const auto *lengths = std::get_if<BackgroundSizeLengthPercentage>(&size)) {
621
+ if (const auto *x = std::get_if<ValueUnit>(&lengths->x)) {
622
+ w = resolve(*x, area.width);
623
+ }
624
+ if (const auto *y = std::get_if<ValueUnit>(&lengths->y)) {
625
+ h = resolve(*y, area.height);
626
+ }
627
+ }
628
+ // CSS's round: the area over a whole number of tiles, at least one.
629
+ auto round = [](double item, double of) {
630
+ if (item > 0 && std::fmod(of, item) != 0) {
631
+ return of / std::max(std::round(of / item), 1.0);
632
+ }
633
+ return item;
634
+ };
635
+ if (repeat.x == BackgroundRepeatStyle::Round) {
636
+ w = round(w, area.width);
637
+ }
638
+ if (repeat.y == BackgroundRepeatStyle::Round) {
639
+ h = round(h, area.height);
640
+ }
641
+ if (w <= 0 || h <= 0) {
642
+ return {};
643
+ }
644
+ double x = area.x, y = area.y;
645
+ if (position.top) {
646
+ y += resolve(*position.top, area.height - h);
647
+ } else if (position.bottom) {
648
+ y += area.height - h - resolve(*position.bottom, area.height - h);
649
+ }
650
+ if (position.left) {
651
+ x += resolve(*position.left, area.width - w);
652
+ } else if (position.right) {
653
+ x += area.width - w - resolve(*position.right, area.width - w);
654
+ }
655
+ auto xs = tileStarts(area.x, area.width, w, x, repeat.x, painting.width);
656
+ auto ys = tileStarts(area.y, area.height, h, y, repeat.y, painting.height);
657
+ // Each tile is drawn on its own, so the whole count is bounded too: rows past it are left out,
658
+ // as tiles past an axis's bound are.
659
+ constexpr size_t kMaxTiles = 65536;
660
+ if (!xs.empty() && xs.size() * ys.size() > kMaxTiles) {
661
+ ys.resize(kMaxTiles / xs.size());
662
+ }
663
+ std::vector<SvgRect> tiles;
664
+ for (double ty : ys) {
665
+ for (double tx : xs) {
666
+ tiles.push_back({tx, ty, w, h});
667
+ }
668
+ }
669
+ return tiles;
670
+ }
671
+
672
+ ViewBox viewBox(const ViewProps &props, const LayoutMetrics &layout) {
673
+ ViewBox box;
674
+ auto metrics = props.resolveBorderMetrics(layout);
675
+ auto radii = cornerRadiiOf(metrics.borderRadii);
676
+ auto widths = widthsOf(metrics.borderWidths);
677
+ bool clips = props.yogaStyle.overflow() != facebook::yoga::Overflow::Visible;
678
+ auto borderColor = argbOf(metrics.borderColors.left);
679
+ bool circular = radii.circular();
680
+ // RCTViewComponentView's useCoreAnimationBorderRendering.
681
+ box.layerDrawn = metrics.borderColors.isUniform() && metrics.borderWidths.isUniform() && metrics.borderStyles.isUniform() &&
682
+ metrics.borderStyles.left == BorderStyle::Solid && circular &&
683
+ (widths.left == 0 || clips || (borderColor && (*borderColor >> 24) == 0));
684
+ box.cornerRadius = circular ? radii.topLeft.x : 0;
685
+ if (box.layerDrawn) {
686
+ box.background = argbOf(props.backgroundColor);
687
+ box.borderWidth = widths.left;
688
+ box.borderColor = borderColor;
689
+ }
690
+ box.drawn = !box.layerDrawn || !props.backgroundImage.empty();
691
+ if (clips && !circular) {
692
+ box.clip = roundedRect({0, 0, layout.frame.size.width, layout.frame.size.height}, radii);
693
+ }
694
+
695
+ SvgRect bounds{0, 0, layout.frame.size.width, layout.frame.size.height};
696
+ for (auto it = props.boxShadow.rbegin(); it != props.boxShadow.rend(); ++it) {
697
+ const auto &shadow = *it;
698
+ BoxShadowLayer layer;
699
+ layer.inset = shadow.inset;
700
+ layer.argb = argbOf(shadow.color).value_or(0xff000000);
701
+ layer.radius = shadow.blurRadius / 2;
702
+ if (!shadow.inset) {
703
+ SvgRect cast{
704
+ bounds.x - shadow.spreadDistance + shadow.offsetX,
705
+ bounds.y - shadow.spreadDistance + shadow.offsetY,
706
+ bounds.width + 2 * shadow.spreadDistance,
707
+ bounds.height + 2 * shadow.spreadDistance};
708
+ auto castRadii = spreadRadii(radii, shadow.spreadDistance);
709
+ layer.shadowPath = roundedRect(cast, castRadii);
710
+ layer.mask = roundedRect(bounds, radii);
711
+ double pad = 2 * (shadow.blurRadius + 1);
712
+ append(layer.mask, roundedRect({cast.x - pad, cast.y - pad, cast.width + 2 * pad, cast.height + 2 * pad}, castRadii));
713
+ layer.maskEvenOdd = true;
714
+ } else {
715
+ auto padding = inset(bounds, widths);
716
+ double blur = shadow.blurRadius;
717
+ layer.shadowPath = rectPath({padding.x - blur, padding.y - blur, padding.width + 2 * blur, padding.height + 2 * blur});
718
+ SvgRect clear{
719
+ padding.x + shadow.offsetX + shadow.spreadDistance,
720
+ padding.y + shadow.offsetY + shadow.spreadDistance,
721
+ std::max(0.0, padding.width - 2 * shadow.spreadDistance),
722
+ std::max(0.0, padding.height - 2 * shadow.spreadDistance)};
723
+ append(layer.shadowPath, roundedRect(clear, innerRadii(spreadRadii(radii, -shadow.spreadDistance), widths), true));
724
+ layer.mask = roundedRect(padding, innerRadii(radii, widths));
725
+ }
726
+ box.shadows.push_back(std::move(layer));
727
+ }
728
+ return box;
729
+ }
730
+
731
+ void drawViewBox(const ViewProps &props, const LayoutMetrics &layout, SvgCanvas &canvas) {
732
+ auto box = viewBox(props, layout);
733
+ if (!box.drawn) {
734
+ return;
735
+ }
736
+ auto metrics = props.resolveBorderMetrics(layout);
737
+ auto radii = cornerRadiiOf(metrics.borderRadii);
738
+ auto widths = widthsOf(metrics.borderWidths);
739
+ SvgRect bounds{0, 0, layout.frame.size.width, layout.frame.size.height};
740
+ auto outer = roundedRect(bounds, radii);
741
+
742
+ if (!box.layerDrawn) {
743
+ auto background = argbOf(props.backgroundColor);
744
+ if (visible(background)) {
745
+ canvas.fill(outer, false, SvgPaint{.argb = *background});
746
+ }
747
+ }
748
+
749
+ if (!props.backgroundImage.empty()) {
750
+ // background-origin: padding-box; background-clip: border-box. The first image is on top.
751
+ auto area = inset(bounds, widths);
752
+ canvas.save();
753
+ canvas.clip(outer, false);
754
+ for (size_t i = props.backgroundImage.size(); i-- > 0;) {
755
+ auto pick = [i](const auto &list, auto fallback) { return list.empty() ? fallback : list[i % list.size()]; };
756
+ auto tiles = backgroundTiles(
757
+ area,
758
+ bounds,
759
+ pick(props.backgroundSize, BackgroundSize{BackgroundSizeLengthPercentage{}}),
760
+ pick(props.backgroundPosition, BackgroundPosition{}),
761
+ pick(props.backgroundRepeat, BackgroundRepeat{}));
762
+ if (tiles.empty()) {
763
+ continue;
764
+ }
765
+ const auto &image = props.backgroundImage[i];
766
+ double w = tiles[0].width, h = tiles[0].height;
767
+ std::optional<SvgGradient> gradient = std::holds_alternative<LinearGradient>(image)
768
+ ? std::optional{linearGradient(std::get<LinearGradient>(image), w, h)}
769
+ : radialGradient(std::get<RadialGradient>(image), w, h);
770
+ if (!gradient || gradient->stops.empty()) {
771
+ continue;
772
+ }
773
+ for (const auto &tile : tiles) {
774
+ SvgPaint paint;
775
+ paint.gradient = *gradient;
776
+ paint.gradient->transform = SvgMatrix::translate(tile.x, tile.y) * gradient->transform;
777
+ canvas.fill(rectPath(tile), false, paint);
778
+ }
779
+ }
780
+ canvas.restore();
781
+ }
782
+
783
+ if (!box.layerDrawn) {
784
+ drawBorder(bounds, metrics, radii, canvas);
785
+ }
786
+ }
787
+
788
+ } // namespace ngn