@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,518 @@
1
+ #import "cg-canvas.h"
2
+
3
+ #include "ngn/file-system.h"
4
+
5
+ #include <cmath>
6
+ #include <vector>
7
+
8
+ namespace {
9
+
10
+ CGColorRef createColorImpl(uint32_t argb, double opacity) {
11
+ CGFloat components[] = {
12
+ ((argb >> 16) & 0xff) / 255.0,
13
+ ((argb >> 8) & 0xff) / 255.0,
14
+ (argb & 0xff) / 255.0,
15
+ ((argb >> 24) & 0xff) / 255.0 * opacity,
16
+ };
17
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
18
+ CGColorRef color = CGColorCreate(space, components);
19
+ CGColorSpaceRelease(space);
20
+ return color;
21
+ }
22
+
23
+ CGAffineTransform affine(const ngn::SvgMatrix &m) {
24
+ return CGAffineTransformMake(m.a, m.b, m.c, m.d, m.e, m.f);
25
+ }
26
+
27
+ CGPathRef createPathImpl(const ngn::SvgPath &path) {
28
+ CGMutablePathRef out = CGPathCreateMutable();
29
+ for (const auto &s : path.segments) {
30
+ switch (s.op) {
31
+ case ngn::SvgPath::Op::Move:
32
+ CGPathMoveToPoint(out, nullptr, s.points[0], s.points[1]);
33
+ break;
34
+ case ngn::SvgPath::Op::Line:
35
+ CGPathAddLineToPoint(out, nullptr, s.points[0], s.points[1]);
36
+ break;
37
+ case ngn::SvgPath::Op::Cubic:
38
+ CGPathAddCurveToPoint(out, nullptr, s.points[0], s.points[1], s.points[2], s.points[3], s.points[4], s.points[5]);
39
+ break;
40
+ case ngn::SvgPath::Op::Close:
41
+ CGPathCloseSubpath(out);
42
+ break;
43
+ }
44
+ }
45
+ return out;
46
+ }
47
+
48
+ // Images an svg draws, by URI, loaded once for every svg.
49
+ NSCache<NSString *, NSImage *> *imageCache() {
50
+ static NSCache<NSString *, NSImage *> *cache = [NSCache new];
51
+ return cache;
52
+ }
53
+
54
+ // The canvas drawSvg draws on: Core Graphics calls on the view's context, or on a mask's while
55
+ // one is drawn. It tracks the user transform itself, so a mask, drawn in a context of its own,
56
+ // starts where the drawing is.
57
+ class CGCanvas final : public ngn::SvgCanvas {
58
+ public:
59
+ CGCanvas(CGContextRef context, CGSize size, CGFloat scale, void (^imageLoaded)(void))
60
+ : context_(context), size_(size), scale_(scale), imageLoaded_(imageLoaded) {}
61
+
62
+ void save() override {
63
+ CGContextSaveGState(context_);
64
+ saved_.push_back(matrix_);
65
+ }
66
+
67
+ void restore() override {
68
+ CGContextRestoreGState(context_);
69
+ if (!saved_.empty()) {
70
+ matrix_ = saved_.back();
71
+ saved_.pop_back();
72
+ }
73
+ }
74
+
75
+ void concat(const ngn::SvgMatrix &m) override {
76
+ CGContextConcatCTM(context_, affine(m));
77
+ matrix_ = matrix_ * m;
78
+ }
79
+
80
+ void clip(const ngn::SvgPath &path, bool evenOdd) override {
81
+ CGPathRef p = createPathImpl(path);
82
+ CGContextAddPath(context_, p);
83
+ CGPathRelease(p);
84
+ if (evenOdd) {
85
+ CGContextEOClip(context_);
86
+ } else {
87
+ CGContextClip(context_);
88
+ }
89
+ }
90
+
91
+ void fill(const ngn::SvgPath &path, bool evenOdd, const ngn::SvgPaint &paint) override {
92
+ CGPathRef p = createPathImpl(path);
93
+ if (paint.gradient) {
94
+ CGContextSaveGState(context_);
95
+ CGContextAddPath(context_, p);
96
+ evenOdd ? CGContextEOClip(context_) : CGContextClip(context_);
97
+ drawGradient(*paint.gradient, paint.opacity);
98
+ CGContextRestoreGState(context_);
99
+ } else {
100
+ CGColorRef color = createColorImpl(paint.argb, paint.opacity);
101
+ CGContextSetFillColorWithColor(context_, color);
102
+ CGColorRelease(color);
103
+ CGContextAddPath(context_, p);
104
+ CGContextDrawPath(context_, evenOdd ? kCGPathEOFill : kCGPathFill);
105
+ }
106
+ CGPathRelease(p);
107
+ }
108
+
109
+ void applyStroke(const ngn::SvgStroke &stroke) {
110
+ CGContextSetLineWidth(context_, stroke.width);
111
+ CGContextSetLineCap(context_, stroke.cap == 1 ? kCGLineCapRound : stroke.cap == 2 ? kCGLineCapSquare : kCGLineCapButt);
112
+ CGContextSetLineJoin(context_, stroke.join == 1 ? kCGLineJoinRound : stroke.join == 2 ? kCGLineJoinBevel : kCGLineJoinMiter);
113
+ CGContextSetMiterLimit(context_, stroke.miterLimit);
114
+ if (!stroke.dashes.empty()) {
115
+ std::vector<CGFloat> dashes(stroke.dashes.begin(), stroke.dashes.end());
116
+ CGContextSetLineDash(context_, stroke.dashOffset, dashes.data(), dashes.size());
117
+ }
118
+ }
119
+
120
+ void stroke(const ngn::SvgPath &path, const ngn::SvgStroke &stroke, const ngn::SvgPaint &paint) override {
121
+ CGContextSaveGState(context_);
122
+ applyStroke(stroke);
123
+ CGPathRef p = createPathImpl(path);
124
+ CGContextAddPath(context_, p);
125
+ CGPathRelease(p);
126
+ if (paint.gradient) {
127
+ CGContextReplacePathWithStrokedPath(context_);
128
+ CGContextClip(context_);
129
+ drawGradient(*paint.gradient, paint.opacity);
130
+ } else {
131
+ CGColorRef color = createColorImpl(paint.argb, paint.opacity);
132
+ CGContextSetStrokeColorWithColor(context_, color);
133
+ CGColorRelease(color);
134
+ CGContextStrokePath(context_);
135
+ }
136
+ CGContextRestoreGState(context_);
137
+ }
138
+
139
+ void beginLayer(double opacity) override {
140
+ CGContextSaveGState(context_);
141
+ CGContextSetAlpha(context_, opacity);
142
+ CGContextBeginTransparencyLayer(context_, nullptr);
143
+ }
144
+
145
+ void endLayer() override {
146
+ CGContextEndTransparencyLayer(context_);
147
+ CGContextRestoreGState(context_);
148
+ }
149
+
150
+ // A luminance mask is drawn in a grey bitmap of the view's pixels, black where nothing is
151
+ // drawn, so its grey is the luminance times the alpha SVG masks by. An alpha mask is drawn in an
152
+ // alpha-only bitmap, clear where nothing is drawn, so only its content's alpha counts. A mask
153
+ // whose bitmap cannot be made (too many pixels) masks nothing out.
154
+ void beginMask(bool alpha) override {
155
+ size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.width * scale_)));
156
+ size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.height * scale_)));
157
+ CGContextRef mask;
158
+ if (alpha) {
159
+ mask = CGBitmapContextCreate(nullptr, width, height, 8, 0, nullptr, kCGImageAlphaOnly);
160
+ if (mask != nullptr) {
161
+ CGContextClearRect(mask, CGRectMake(0, 0, width, height));
162
+ }
163
+ } else {
164
+ CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
165
+ mask = CGBitmapContextCreate(nullptr, width, height, 8, 0, grey, kCGImageAlphaNone);
166
+ CGColorSpaceRelease(grey);
167
+ if (mask != nullptr) {
168
+ CGContextSetGrayFillColor(mask, 0, 1);
169
+ CGContextFillRect(mask, CGRectMake(0, 0, width, height));
170
+ }
171
+ }
172
+ if (mask == nullptr) {
173
+ // The mask's content is drawn into a context that keeps nothing, and the clip is skipped.
174
+ masks_.push_back({.parent = context_, .mask = nullptr, .saved = saved_, .alpha = alpha, .scratch = scratchContext()});
175
+ context_ = masks_.back().scratch;
176
+ return;
177
+ }
178
+ // The view's coordinates: top left, y down, in points.
179
+ CGContextTranslateCTM(mask, 0, height);
180
+ CGContextScaleCTM(mask, scale_, -scale_);
181
+ CGContextConcatCTM(mask, affine(matrix_));
182
+ masks_.push_back({.parent = context_, .mask = mask, .saved = saved_, .alpha = alpha});
183
+ context_ = mask;
184
+ }
185
+
186
+ void endMask() override {
187
+ if (masks_.empty()) {
188
+ return;
189
+ }
190
+ auto frame = masks_.back();
191
+ context_ = frame.parent;
192
+ saved_ = frame.saved;
193
+ if (frame.mask == nullptr) {
194
+ CGContextRelease(frame.scratch);
195
+ masks_.back().scratch = nullptr;
196
+ CGContextSaveGState(context_);
197
+ return;
198
+ }
199
+ // A clip mask is a grey image: an alpha mask's alpha bytes are read as grey.
200
+ CGImageRef image;
201
+ if (frame.alpha) {
202
+ size_t width = CGBitmapContextGetWidth(frame.mask), height = CGBitmapContextGetHeight(frame.mask);
203
+ size_t row = CGBitmapContextGetBytesPerRow(frame.mask);
204
+ CFDataRef bytes = CFDataCreate(nullptr, static_cast<const UInt8 *>(CGBitmapContextGetData(frame.mask)), static_cast<CFIndex>(row * height));
205
+ CGDataProviderRef provider = CGDataProviderCreateWithCFData(bytes);
206
+ CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
207
+ image = CGImageCreate(width, height, 8, 8, row, grey, static_cast<CGBitmapInfo>(kCGImageAlphaNone), provider, nullptr, false,
208
+ kCGRenderingIntentDefault);
209
+ CGColorSpaceRelease(grey);
210
+ CGDataProviderRelease(provider);
211
+ CFRelease(bytes);
212
+ } else {
213
+ image = CGBitmapContextCreateImage(frame.mask);
214
+ }
215
+ CGContextRelease(frame.mask);
216
+ masks_.back().mask = nullptr;
217
+ // Clip in the view's coordinates, the image's top row at the view's top, then come back.
218
+ CGContextSaveGState(context_);
219
+ CGAffineTransform toView = CGAffineTransformInvert(affine(matrix_));
220
+ CGContextConcatCTM(context_, toView);
221
+ CGContextTranslateCTM(context_, 0, size_.height);
222
+ CGContextScaleCTM(context_, 1, -1);
223
+ CGContextClipToMask(context_, CGRectMake(0, 0, size_.width, size_.height), image);
224
+ CGContextScaleCTM(context_, 1, -1);
225
+ CGContextTranslateCTM(context_, 0, -size_.height);
226
+ CGContextConcatCTM(context_, affine(matrix_));
227
+ CGImageRelease(image);
228
+ }
229
+
230
+ void endMasked() override {
231
+ if (masks_.empty()) {
232
+ return;
233
+ }
234
+ masks_.pop_back();
235
+ CGContextRestoreGState(context_);
236
+ }
237
+
238
+ void clipToStroke(const ngn::SvgPath &path, const ngn::SvgStroke &stroke) override {
239
+ applyStroke(stroke);
240
+ CGPathRef p = createPathImpl(path);
241
+ CGContextAddPath(context_, p);
242
+ CGPathRelease(p);
243
+ CGContextReplacePathWithStrokedPath(context_);
244
+ CGContextClip(context_);
245
+ }
246
+
247
+ // The tile is drawn once into a bitmap at the pixels it covers, and the bitmap tiled over the
248
+ // clip. The bitmap's rows run from the tile's top, as the view's do.
249
+ void pattern(const ngn::SvgPatternTile &tile, double opacity, const std::function<void(ngn::SvgCanvas &)> &drawTile) override {
250
+ ngn::SvgMatrix toView = matrix_ * tile.transform;
251
+ double density = scale_ * std::sqrt(std::abs(toView.a * toView.d - toView.b * toView.c));
252
+ if (!(density > 0)) {
253
+ return;
254
+ }
255
+ // At most 2048 pixels a side: a larger tile is drawn coarser.
256
+ density = std::min({density, 2048 / tile.tile.width, 2048 / tile.tile.height});
257
+ size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(tile.tile.width * density)));
258
+ size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(tile.tile.height * density)));
259
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
260
+ CGContextRef bitmap = CGBitmapContextCreate(nullptr, width, height, 8, 0, space, kCGImageAlphaPremultipliedLast);
261
+ CGColorSpaceRelease(space);
262
+ // A tile whose bitmap cannot be made paints nothing.
263
+ if (bitmap == nullptr) {
264
+ return;
265
+ }
266
+ CGContextTranslateCTM(bitmap, 0, height);
267
+ CGContextScaleCTM(bitmap, width / tile.tile.width, -(height / tile.tile.height));
268
+ {
269
+ CGCanvas inner(bitmap, CGSizeMake(tile.tile.width, tile.tile.height), density, imageLoaded_);
270
+ drawTile(inner);
271
+ }
272
+ CGImageRef image = CGBitmapContextCreateImage(bitmap);
273
+ CGContextRelease(bitmap);
274
+ CGContextSaveGState(context_);
275
+ CGContextSetAlpha(context_, opacity);
276
+ CGContextConcatCTM(context_, affine(tile.transform));
277
+ CGContextTranslateCTM(context_, tile.tile.x, tile.tile.y);
278
+ // Drawn in a space whose y runs up, so the image stands the right way; the tiles keep their
279
+ // places, every tile's height apart.
280
+ CGContextScaleCTM(context_, 1, -1);
281
+ CGContextDrawTiledImage(context_, CGRectMake(0, 0, tile.tile.width, tile.tile.height), image);
282
+ CGContextRestoreGState(context_);
283
+ CGImageRelease(image);
284
+ }
285
+
286
+ // The source is drawn in a bitmap of the view's pixels, as a mask is, and the filter's result
287
+ // drawn back in its place.
288
+ void beginFilter(const ngn::SvgFilter &filter) override {
289
+ size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.width * scale_)));
290
+ size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.height * scale_)));
291
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
292
+ CGContextRef bitmap = CGBitmapContextCreate(nullptr, width, height, 8, width * 4, space, kCGImageAlphaPremultipliedLast);
293
+ CGColorSpaceRelease(space);
294
+ // With no bitmap (too many pixels) the source is drawn as it is, unfiltered.
295
+ if (bitmap == nullptr) {
296
+ filters_.push_back({.parent = context_, .bitmap = nullptr, .saved = saved_, .filter = filter, .width = 0, .height = 0});
297
+ return;
298
+ }
299
+ CGContextTranslateCTM(bitmap, 0, height);
300
+ CGContextScaleCTM(bitmap, scale_, -scale_);
301
+ CGContextConcatCTM(bitmap, affine(matrix_));
302
+ filters_.push_back({.parent = context_, .bitmap = bitmap, .saved = saved_, .filter = filter, .width = width, .height = height});
303
+ context_ = bitmap;
304
+ }
305
+
306
+ void endFilter() override {
307
+ if (filters_.empty()) {
308
+ return;
309
+ }
310
+ auto frame = std::move(filters_.back());
311
+ filters_.pop_back();
312
+ if (frame.bitmap == nullptr) {
313
+ return;
314
+ }
315
+ context_ = frame.parent;
316
+ saved_ = frame.saved;
317
+ ngn::SvgPixels source{.width = static_cast<int>(frame.width), .height = static_cast<int>(frame.height)};
318
+ const auto *data = static_cast<const uint8_t *>(CGBitmapContextGetData(frame.bitmap));
319
+ source.rgba.assign(data, data + frame.width * frame.height * 4);
320
+ CGContextRelease(frame.bitmap);
321
+ auto result = ngn::runSvgFilter(frame.filter, source, ngn::SvgMatrix::scale(scale_, scale_) * matrix_);
322
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
323
+ CFDataRef bytes = CFDataCreate(nullptr, result.rgba.data(), static_cast<CFIndex>(result.rgba.size()));
324
+ CGDataProviderRef provider = CGDataProviderCreateWithCFData(bytes);
325
+ CGImageRef image = CGImageCreate(
326
+ frame.width, frame.height, 8, 32, frame.width * 4, space,
327
+ static_cast<CGBitmapInfo>(kCGImageAlphaPremultipliedLast), provider, nullptr, false, kCGRenderingIntentDefault);
328
+ CGDataProviderRelease(provider);
329
+ CFRelease(bytes);
330
+ CGColorSpaceRelease(space);
331
+ // In the view's coordinates, the image's top row at the view's top.
332
+ CGContextSaveGState(context_);
333
+ CGContextConcatCTM(context_, CGAffineTransformInvert(affine(matrix_)));
334
+ CGContextTranslateCTM(context_, 0, size_.height);
335
+ CGContextScaleCTM(context_, 1, -1);
336
+ CGContextDrawImage(context_, CGRectMake(0, 0, frame.width / scale_, frame.height / scale_), image);
337
+ CGContextRestoreGState(context_);
338
+ CGImageRelease(image);
339
+ }
340
+
341
+ void image(const std::string &uri, const ngn::SvgRect &rect, const std::string &align, int meetOrSlice, double opacity) override {
342
+ NSString *key = @(uri.c_str());
343
+ NSImage *image = [imageCache() objectForKey:key];
344
+ if (image == nil) {
345
+ load(key);
346
+ return;
347
+ }
348
+ NSSize natural = image.size;
349
+ if (natural.width <= 0 || natural.height <= 0) {
350
+ return;
351
+ }
352
+ auto fit = ngn::svgViewBoxTransform({0, 0, natural.width, natural.height}, align, meetOrSlice, rect.width, rect.height);
353
+ CGContextSaveGState(context_);
354
+ CGContextClipToRect(context_, CGRectMake(rect.x, rect.y, rect.width, rect.height));
355
+ CGContextTranslateCTM(context_, rect.x, rect.y);
356
+ CGContextConcatCTM(context_, affine(fit));
357
+ NSGraphicsContext *previous = NSGraphicsContext.currentContext;
358
+ NSGraphicsContext.currentContext = [NSGraphicsContext graphicsContextWithCGContext:context_ flipped:YES];
359
+ [image drawInRect:NSMakeRect(0, 0, natural.width, natural.height)
360
+ fromRect:NSZeroRect
361
+ operation:NSCompositingOperationSourceOver
362
+ fraction:opacity
363
+ respectFlipped:YES
364
+ hints:nil];
365
+ NSGraphicsContext.currentContext = previous;
366
+ CGContextRestoreGState(context_);
367
+ }
368
+
369
+ double text(
370
+ const std::string &text,
371
+ double x,
372
+ double y,
373
+ double fontSize,
374
+ const std::string &fontFamily,
375
+ const std::string &fontWeight,
376
+ const ngn::SvgPaint *fill) override {
377
+ NSFont *font = fontFamily.empty() ? nil : [NSFont fontWithName:@(fontFamily.c_str()) size:fontSize];
378
+ if (font == nil) {
379
+ int weight = fontWeight == "bold" ? 700 : std::atoi(fontWeight.c_str());
380
+ font = [NSFont systemFontOfSize:fontSize weight:weight >= 600 ? NSFontWeightBold : weight >= 500 ? NSFontWeightMedium : NSFontWeightRegular];
381
+ }
382
+ uint32_t argb = fill ? (fill->gradient && !fill->gradient->stops.empty() ? fill->gradient->stops[0].second : fill->argb) : 0;
383
+ CGColorRef color = createColorImpl(argb, fill ? fill->opacity : 0);
384
+ NSAttributedString *string = [[NSAttributedString alloc]
385
+ initWithString:@(text.c_str())
386
+ attributes:@{NSFontAttributeName : font, (__bridge id)kCTForegroundColorAttributeName : (__bridge id)color}];
387
+ CGColorRelease(color);
388
+ CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)string);
389
+ double width = CTLineGetTypographicBounds(line, nullptr, nullptr, nullptr);
390
+ if (fill != nullptr) {
391
+ CGContextSaveGState(context_);
392
+ CGContextTranslateCTM(context_, x, y);
393
+ CGContextScaleCTM(context_, 1, -1);
394
+ CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
395
+ CGContextSetTextPosition(context_, 0, 0);
396
+ CTLineDraw(line, context_);
397
+ CGContextRestoreGState(context_);
398
+ }
399
+ CFRelease(line);
400
+ return width;
401
+ }
402
+
403
+ private:
404
+ struct Mask {
405
+ CGContextRef parent;
406
+ CGContextRef mask;
407
+ std::vector<ngn::SvgMatrix> saved;
408
+ bool alpha{false};
409
+ // Where a mask with no bitmap has its content drawn, and dropped.
410
+ CGContextRef scratch{nullptr};
411
+ };
412
+
413
+ // A one-pixel context that the content of a mask with no bitmap is drawn into.
414
+ static CGContextRef scratchContext() {
415
+ CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
416
+ CGContextRef scratch = CGBitmapContextCreate(nullptr, 1, 1, 8, 0, grey, kCGImageAlphaNone);
417
+ CGColorSpaceRelease(grey);
418
+ return scratch;
419
+ }
420
+
421
+ struct Filter {
422
+ CGContextRef parent;
423
+ CGContextRef bitmap;
424
+ std::vector<ngn::SvgMatrix> saved;
425
+ ngn::SvgFilter filter;
426
+ size_t width, height;
427
+ };
428
+
429
+ void drawGradient(const ngn::SvgGradient &g, double opacity) {
430
+ std::vector<CGFloat> components;
431
+ std::vector<CGFloat> locations;
432
+ for (const auto &[offset, argb] : g.stops) {
433
+ components.push_back(((argb >> 16) & 0xff) / 255.0);
434
+ components.push_back(((argb >> 8) & 0xff) / 255.0);
435
+ components.push_back((argb & 0xff) / 255.0);
436
+ components.push_back(((argb >> 24) & 0xff) / 255.0);
437
+ // Offsets only grow, as SVG clamps a smaller one to the one before.
438
+ locations.push_back(std::clamp(locations.empty() ? offset : std::max(offset, double(locations.back())), 0.0, 1.0));
439
+ }
440
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
441
+ CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components.data(), locations.data(), locations.size());
442
+ CGColorSpaceRelease(space);
443
+ CGContextSaveGState(context_);
444
+ CGContextSetAlpha(context_, opacity);
445
+ CGContextConcatCTM(context_, affine(g.transform));
446
+ auto extend = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
447
+ if (g.radial) {
448
+ CGContextDrawRadialGradient(context_, gradient, CGPointMake(g.x2, g.y2), 0, CGPointMake(g.x1, g.y1), g.r, extend);
449
+ } else {
450
+ CGContextDrawLinearGradient(context_, gradient, CGPointMake(g.x1, g.y1), CGPointMake(g.x2, g.y2), extend);
451
+ }
452
+ CGContextRestoreGState(context_);
453
+ CGGradientRelease(gradient);
454
+ }
455
+
456
+ // Every canvas that asks for a uri while it loads is told when it is in the cache.
457
+ void load(NSString *uri) {
458
+ static NSMutableDictionary<NSString *, NSMutableArray *> *loading = [NSMutableDictionary dictionary];
459
+ if (NSMutableArray *waiters = loading[uri]) {
460
+ if (imageLoaded_ != nil) {
461
+ [waiters addObject:[imageLoaded_ copy]];
462
+ }
463
+ return;
464
+ }
465
+ // An <image> href's file only inside the file boundary, and exactly the file checked.
466
+ std::shared_ptr<ngn::FileAccess::Use> use;
467
+ NSURL *url = nil;
468
+ try {
469
+ auto source = ngn::readableSource(ngn::viewFileAccess().get(), uri.UTF8String);
470
+ url = [NSURL URLWithString:[NSString stringWithUTF8String:source.uri.c_str()]];
471
+ use = std::move(source.use);
472
+ } catch (const ngn::FileRefused &) {
473
+ return;
474
+ }
475
+ if (url == nil) {
476
+ return;
477
+ }
478
+ loading[uri] = imageLoaded_ != nil ? [NSMutableArray arrayWithObject:[imageLoaded_ copy]] : [NSMutableArray array];
479
+ [[NSURLSession.sharedSession dataTaskWithURL:url
480
+ completionHandler:^(NSData *data, NSURLResponse *, NSError *) {
481
+ (void)use;
482
+ NSImage *image = data ? [[NSImage alloc] initWithData:data] : nil;
483
+ dispatch_async(dispatch_get_main_queue(), ^{
484
+ NSArray *waiters = loading[uri];
485
+ [loading removeObjectForKey:uri];
486
+ if (image != nil) {
487
+ [imageCache() setObject:image forKey:uri];
488
+ for (void (^waiter)(void) in waiters) {
489
+ waiter();
490
+ }
491
+ }
492
+ });
493
+ }] resume];
494
+ }
495
+
496
+ CGContextRef context_;
497
+ CGSize size_;
498
+ CGFloat scale_;
499
+ void (^imageLoaded_)(void);
500
+ ngn::SvgMatrix matrix_;
501
+ std::vector<ngn::SvgMatrix> saved_;
502
+ std::vector<Mask> masks_;
503
+ std::vector<Filter> filters_;
504
+ };
505
+
506
+ } // namespace
507
+
508
+ CGColorRef NGNCreateColor(uint32_t argb, double opacity) {
509
+ return createColorImpl(argb, opacity);
510
+ }
511
+
512
+ CGPathRef NGNCreatePath(const ngn::SvgPath &path) {
513
+ return createPathImpl(path);
514
+ }
515
+
516
+ std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(CGContextRef context, CGSize size, CGFloat scale, void (^imageLoaded)(void)) {
517
+ return std::make_unique<CGCanvas>(context, size, scale, imageLoaded);
518
+ }
@@ -0,0 +1,17 @@
1
+ // What a linked library includes to draw its Fabric component on macOS: the NSView every Fabric
2
+ // view mounts as (NGNView, host-view.h), and the call that names the subclass for a component.
3
+ //
4
+ // The library's codegen gives the host the component's props, events and shadow node, as on
5
+ // Android (cmake/libraries.cmake), so the component lays out and its events reach JavaScript. Its
6
+ // view is a subclass of NGNView that overrides updateProps: (calling super, which keeps the view's
7
+ // background, border, opacity and accessibility) and reads its props by casting to the codegen's
8
+ // Props class. Events go out through the codegen's EventEmitter, cast from `eventEmitter`.
9
+ //
10
+ // Registered from the library's own code as it loads (`+load` or a static initializer), the way
11
+ // its TurboModules are. The host's own components keep their views: a library's class for one of
12
+ // their names is ignored. A component with no class registered mounts as a plain NGNView.
13
+ #pragma once
14
+
15
+ #import "host-view.h"
16
+
17
+ void NGNRegisterComponentView(NSString *componentName, Class viewClass);
@@ -0,0 +1,19 @@
1
+ // The OS half of NgNativeCrypto (the core's crypto.h): CommonCrypto's digests and
2
+ // SecRandomCopyBytes.
3
+ #pragma once
4
+
5
+ #include "ngn/crypto.h"
6
+
7
+ #include <memory>
8
+ #include <optional>
9
+ #include <string>
10
+
11
+ namespace ngn::appkit {
12
+
13
+ std::shared_ptr<CryptoSource> makeCryptoSource();
14
+
15
+ // CryptoSource::digest, fed to CommonCrypto `chunk` bytes at a time (at most 2^32 - 1, whatever is
16
+ // asked); digest uses 2^30, so inputs of 4 GiB and more are hashed whole.
17
+ std::optional<std::string> digestInChunks(const std::string &algorithm, const std::string &bytes, size_t chunk);
18
+
19
+ } // namespace ngn::appkit
@@ -0,0 +1,78 @@
1
+ #include "crypto.h"
2
+
3
+ #import <CommonCrypto/CommonDigest.h>
4
+ #import <Security/Security.h>
5
+
6
+ #include <algorithm>
7
+ #include <limits>
8
+ #include <stdexcept>
9
+
10
+ // MD2 and MD4 are deprecated in CommonCrypto, and expo-crypto still offers them.
11
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
12
+
13
+ namespace ngn::appkit {
14
+
15
+ namespace {
16
+
17
+ // Fed in chunks that fit CC_LONG, which is 32 bits: a longer length would wrap.
18
+ template <size_t Length, typename Context>
19
+ std::string digestWith(int (*init)(Context *), int (*update)(Context *, const void *, CC_LONG),
20
+ int (*final)(unsigned char *, Context *), const std::string &bytes, size_t chunk) {
21
+ Context context;
22
+ init(&context);
23
+ for (size_t at = 0; at < bytes.size(); at += chunk) {
24
+ update(&context, bytes.data() + at, static_cast<CC_LONG>(std::min(chunk, bytes.size() - at)));
25
+ }
26
+ unsigned char out[Length];
27
+ final(out, &context);
28
+ return std::string(reinterpret_cast<const char *>(out), Length);
29
+ }
30
+
31
+ class AppleCrypto final : public CryptoSource {
32
+ public:
33
+ std::optional<std::string> digest(const std::string &algorithm, const std::string &bytes) override {
34
+ return digestInChunks(algorithm, bytes, size_t{1} << 30);
35
+ }
36
+
37
+ std::string randomBytes(size_t count) override {
38
+ std::string out(count, '\0');
39
+ if (count > 0 && SecRandomCopyBytes(kSecRandomDefault, count, out.data()) != errSecSuccess) {
40
+ throw std::runtime_error("Crypto: the system's secure random source failed");
41
+ }
42
+ return out;
43
+ }
44
+ };
45
+
46
+ } // namespace
47
+
48
+ std::optional<std::string> digestInChunks(const std::string &algorithm, const std::string &bytes, size_t chunk) {
49
+ chunk = std::clamp<size_t>(chunk, 1, std::numeric_limits<CC_LONG>::max());
50
+ if (algorithm == "SHA-1") {
51
+ return digestWith<CC_SHA1_DIGEST_LENGTH>(CC_SHA1_Init, CC_SHA1_Update, CC_SHA1_Final, bytes, chunk);
52
+ }
53
+ if (algorithm == "SHA-256") {
54
+ return digestWith<CC_SHA256_DIGEST_LENGTH>(CC_SHA256_Init, CC_SHA256_Update, CC_SHA256_Final, bytes, chunk);
55
+ }
56
+ if (algorithm == "SHA-384") {
57
+ return digestWith<CC_SHA384_DIGEST_LENGTH>(CC_SHA384_Init, CC_SHA384_Update, CC_SHA384_Final, bytes, chunk);
58
+ }
59
+ if (algorithm == "SHA-512") {
60
+ return digestWith<CC_SHA512_DIGEST_LENGTH>(CC_SHA512_Init, CC_SHA512_Update, CC_SHA512_Final, bytes, chunk);
61
+ }
62
+ if (algorithm == "MD2") {
63
+ return digestWith<CC_MD2_DIGEST_LENGTH>(CC_MD2_Init, CC_MD2_Update, CC_MD2_Final, bytes, chunk);
64
+ }
65
+ if (algorithm == "MD4") {
66
+ return digestWith<CC_MD4_DIGEST_LENGTH>(CC_MD4_Init, CC_MD4_Update, CC_MD4_Final, bytes, chunk);
67
+ }
68
+ if (algorithm == "MD5") {
69
+ return digestWith<CC_MD5_DIGEST_LENGTH>(CC_MD5_Init, CC_MD5_Update, CC_MD5_Final, bytes, chunk);
70
+ }
71
+ return std::nullopt;
72
+ }
73
+
74
+ std::shared_ptr<CryptoSource> makeCryptoSource() {
75
+ return std::make_shared<AppleCrypto>();
76
+ }
77
+
78
+ } // namespace ngn::appkit
@@ -0,0 +1,29 @@
1
+ // The OS half of the desktop Expo modules (the core's desktop-expo.h).
2
+ #pragma once
3
+
4
+ #import <Foundation/Foundation.h>
5
+
6
+ #include "ngn/desktop-expo.h"
7
+ #include "ngn/launch-settings.h"
8
+
9
+ #include <memory>
10
+ #include <string>
11
+
12
+ namespace ngn::appkit {
13
+
14
+ std::shared_ptr<SecretStore> makeKeychain();
15
+ // Removes the file keychain items another test launch left when it ended without removing them.
16
+ // Reads attributes only, so no item's data is asked for.
17
+ void removeStaleTestItems(const TestLaunch &own);
18
+ std::shared_ptr<SleepPreventer> makeSleepPreventer();
19
+ std::shared_ptr<HapticPlayer> makeHapticPlayer();
20
+ std::shared_ptr<LocaleSource> makeLocaleSource();
21
+ std::shared_ptr<FileDialogs> makeFileDialogs();
22
+ // The app's folder in ~/Library/Caches, created if missing.
23
+ std::string cacheDirectory(const std::string &appId);
24
+ // The region the system is set to ("GB"), or "US" when it has none.
25
+ std::string systemRegionCode();
26
+ // An NSCalendar identifier by expo-localization's name for it ("ethiopic"), or as it is when it has none.
27
+ std::string calendarName(NSString *identifier);
28
+
29
+ } // namespace ngn::appkit