@ng-native/desktop 0.0.0 → 0.24.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.
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +277 -0
- package/native/appkit/CMakeLists.txt +130 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-sheet.h +59 -0
- package/native/appkit/camera-sheet.mm +344 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +134 -0
- package/native/appkit/camera.h +61 -0
- package/native/appkit/camera.mm +496 -0
- package/native/appkit/cg-canvas.h +24 -0
- package/native/appkit/cg-canvas.mm +663 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +632 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1228 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1475 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +562 -0
- package/native/appkit/svg-view.h +18 -0
- package/native/appkit/svg-view.mm +103 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/tab-walk.h +31 -0
- package/native/appkit/tab-walk.mm +64 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +81 -0
- package/native/appkit/text-input-field.mm +525 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +485 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +10 -0
- package/native/appkit/web-view-component.mm +62 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +401 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +151 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +121 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +222 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +108 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/dom-web-view.h +53 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +134 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +163 -0
- package/native/core/include/ngn/safe-areas.h +82 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/surface-metrics.h +51 -0
- package/native/core/include/ngn/svg.h +434 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +134 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +137 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +359 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +98 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/dom-web-view.cpp +45 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +358 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +412 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +401 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +385 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +384 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +420 -0
- package/native/core/src/safe-areas.cpp +137 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +2190 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +139 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +175 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/launch-probe.sh +60 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +73 -1
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
#include "ngn/fonts.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/download.h"
|
|
4
|
+
#include "ngn/file-system.h"
|
|
5
|
+
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <map>
|
|
8
|
+
#include <mutex>
|
|
9
|
+
#include <sstream>
|
|
10
|
+
#include <stdexcept>
|
|
11
|
+
|
|
12
|
+
namespace ngn {
|
|
13
|
+
|
|
14
|
+
void FontLibrary::add(const std::string &name, RegisteredFont font) {
|
|
15
|
+
std::lock_guard lock(mutex_);
|
|
16
|
+
if (fonts_.find(name) == fonts_.end()) {
|
|
17
|
+
order_.push_back(name);
|
|
18
|
+
}
|
|
19
|
+
fonts_[name] = std::move(font);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
bool FontLibrary::has(const std::string &name) const {
|
|
23
|
+
std::lock_guard lock(mutex_);
|
|
24
|
+
return fonts_.find(name) != fonts_.end();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
std::optional<RegisteredFont> FontLibrary::fontOf(const std::string &name) const {
|
|
28
|
+
std::lock_guard lock(mutex_);
|
|
29
|
+
auto it = fonts_.find(name);
|
|
30
|
+
return it == fonts_.end() ? std::nullopt : std::optional(it->second);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
std::vector<std::string> FontLibrary::names() const {
|
|
34
|
+
std::lock_guard lock(mutex_);
|
|
35
|
+
return order_;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
namespace {
|
|
39
|
+
|
|
40
|
+
bool isHttp(const std::string &uri) {
|
|
41
|
+
return uri.rfind("http://", 0) == 0 || uri.rfind("https://", 0) == 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Where a downloaded font is kept: one file per uri, keeping its extension, which CTFontManager
|
|
45
|
+
// reads the format from.
|
|
46
|
+
std::string downloadPath(const std::string &folder, const std::string &uri) {
|
|
47
|
+
auto path = uri.substr(0, uri.find_first_of("?#"));
|
|
48
|
+
auto slash = path.rfind('/');
|
|
49
|
+
auto dot = path.rfind('.');
|
|
50
|
+
std::string extension = dot != std::string::npos && (slash == std::string::npos || dot > slash) ? path.substr(dot) : "";
|
|
51
|
+
if (extension.size() > 6) {
|
|
52
|
+
extension.clear();
|
|
53
|
+
}
|
|
54
|
+
std::ostringstream name;
|
|
55
|
+
name << std::hex << std::hash<std::string>{}(uri) << extension;
|
|
56
|
+
return folder + "/" + name.str();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The downloads in flight, by the file each writes, with what waits on each: one download of a url
|
|
60
|
+
// at a time, so a second load never rewrites the file while the first registers it.
|
|
61
|
+
struct Downloads {
|
|
62
|
+
using Waiter = std::function<void(const std::string &error)>;
|
|
63
|
+
std::mutex mutex;
|
|
64
|
+
std::map<std::string, std::vector<Waiter>> waiting;
|
|
65
|
+
|
|
66
|
+
// Whether `waiter` is the first for `target`, which then starts its download.
|
|
67
|
+
bool wait(const std::string &target, Waiter waiter) {
|
|
68
|
+
std::lock_guard lock(mutex);
|
|
69
|
+
auto &list = waiting[target];
|
|
70
|
+
list.push_back(std::move(waiter));
|
|
71
|
+
return list.size() == 1;
|
|
72
|
+
}
|
|
73
|
+
void finish(const std::string &target, const std::string &error) {
|
|
74
|
+
std::vector<Waiter> done;
|
|
75
|
+
{
|
|
76
|
+
std::lock_guard lock(mutex);
|
|
77
|
+
auto found = waiting.find(target);
|
|
78
|
+
if (found == waiting.end()) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
done = std::move(found->second);
|
|
82
|
+
waiting.erase(found);
|
|
83
|
+
}
|
|
84
|
+
for (auto &waiter : done) {
|
|
85
|
+
waiter(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
} // namespace
|
|
91
|
+
|
|
92
|
+
std::shared_ptr<NativeModule> makeFontModule(
|
|
93
|
+
std::shared_ptr<FontRegistrar> registrar,
|
|
94
|
+
std::shared_ptr<FontLibrary> library,
|
|
95
|
+
std::shared_ptr<Http> http,
|
|
96
|
+
std::string downloads,
|
|
97
|
+
std::shared_ptr<FileAccess> files) {
|
|
98
|
+
struct Fonts : NativeModule {
|
|
99
|
+
Fonts(std::shared_ptr<FontRegistrar> registrar,
|
|
100
|
+
std::shared_ptr<FontLibrary> library,
|
|
101
|
+
std::shared_ptr<Http> http,
|
|
102
|
+
std::string downloads,
|
|
103
|
+
std::shared_ptr<FileAccess> files)
|
|
104
|
+
: NativeModule("NgNativeFont") {
|
|
105
|
+
auto load = [registrar, library](const std::string &name, const std::string &path, JsPromise promise) {
|
|
106
|
+
try {
|
|
107
|
+
library->add(name, registrar->registerFile(path));
|
|
108
|
+
promise.resolve(nullptr);
|
|
109
|
+
} catch (const std::exception &error) {
|
|
110
|
+
promise.reject("Fonts: " + name + " could not be loaded from " + path + ": " + error.what());
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
auto inFlight = std::make_shared<Downloads>();
|
|
114
|
+
promise("loadAsync", [load, http, downloads, files, inFlight](const folly::dynamic &args, JsPromise promise) {
|
|
115
|
+
if (args.size() < 2 || !args[0].isString() || args[0].asString().empty() || !args[1].isString()) {
|
|
116
|
+
promise.reject("Fonts.loadAsync: takes a font's name and the uri of its file");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
auto name = args[0].asString();
|
|
120
|
+
auto uri = args[1].asString();
|
|
121
|
+
if (auto path = pathOfUri(uri)) {
|
|
122
|
+
std::shared_ptr<FileAccess::Use> use;
|
|
123
|
+
try {
|
|
124
|
+
use = readableFile(files.get(), *path);
|
|
125
|
+
} catch (const FileRefused &refused) {
|
|
126
|
+
promise.reject("Fonts: " + name + " could not be loaded: " + refused.what());
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
load(name, *path, std::move(promise));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (!isHttp(uri) || !http) {
|
|
133
|
+
promise.reject("Fonts.loadAsync: " + name + "'s uri is not a file or an http(s) url: " + uri);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
auto target = downloadPath(downloads, uri);
|
|
137
|
+
bool first = inFlight->wait(target, [load, name, uri, target, promise](const std::string &error) {
|
|
138
|
+
if (!error.empty()) {
|
|
139
|
+
promise.reject("Fonts: " + name + " could not be downloaded from " + uri + ": " + error);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
load(name, target, promise);
|
|
143
|
+
});
|
|
144
|
+
if (first) {
|
|
145
|
+
downloadFile(http, uri, target, [inFlight, target](const std::string &error) { inFlight->finish(target, error); });
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
sync("isLoaded", [library](const folly::dynamic &args) {
|
|
149
|
+
return folly::dynamic(!args.empty() && args[0].isString() && library->has(args[0].asString()));
|
|
150
|
+
});
|
|
151
|
+
sync("getLoadedFonts", [library](const folly::dynamic &) {
|
|
152
|
+
folly::dynamic names = folly::dynamic::array();
|
|
153
|
+
for (const auto &name : library->names()) {
|
|
154
|
+
names.push_back(name);
|
|
155
|
+
}
|
|
156
|
+
return names;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return std::make_shared<Fonts>(std::move(registrar), std::move(library), std::move(http), std::move(downloads), std::move(files));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
} // namespace ngn
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// ImageLoader, in React Native iOS's shapes: Image.getSize, getSizeWithHeaders and prefetch. The
|
|
2
|
+
// bytes come over the backend's HTTP client (which also reads file and data URLs and shares the
|
|
3
|
+
// OS's URL cache with the views that draw images); the backend only measures them. A file is read
|
|
4
|
+
// only inside the file boundary (readableSource).
|
|
5
|
+
#include "ngn/core-modules.h"
|
|
6
|
+
#include "ngn/file-system.h"
|
|
7
|
+
|
|
8
|
+
namespace ngn {
|
|
9
|
+
|
|
10
|
+
namespace {
|
|
11
|
+
|
|
12
|
+
// Fetches `uri` and answers with the image's size, or an error.
|
|
13
|
+
void measure(
|
|
14
|
+
const CoreModuleServices &s,
|
|
15
|
+
const std::string &uri,
|
|
16
|
+
Headers headers,
|
|
17
|
+
std::function<void(std::optional<std::pair<double, double>>, std::string)> done) {
|
|
18
|
+
struct Download {
|
|
19
|
+
uint16_t status{0};
|
|
20
|
+
std::string bytes;
|
|
21
|
+
};
|
|
22
|
+
ReadableSource source;
|
|
23
|
+
try {
|
|
24
|
+
source = readableSource(s.files.get(), uri);
|
|
25
|
+
} catch (const FileRefused &refused) {
|
|
26
|
+
done(std::nullopt, refused.what());
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
auto download = std::make_shared<Download>();
|
|
30
|
+
auto system = s.system;
|
|
31
|
+
// The task is not kept: nothing cancels an image measurement, and the callbacks hold what they need.
|
|
32
|
+
s.http->send(
|
|
33
|
+
HttpRequest{.url = source.uri, .headers = std::move(headers)},
|
|
34
|
+
HttpCallbacks{
|
|
35
|
+
.onResponse = [download](const HttpResponse &r) { download->status = r.status; },
|
|
36
|
+
.onData = [download](std::string chunk, int64_t) { download->bytes += chunk; },
|
|
37
|
+
.onComplete =
|
|
38
|
+
// The pick holding a file stays open until it is read.
|
|
39
|
+
[download, system, uri, done, use = source.use](const std::string &error, bool) {
|
|
40
|
+
if (!error.empty()) {
|
|
41
|
+
done(std::nullopt, error);
|
|
42
|
+
} else if (download->status >= 400) {
|
|
43
|
+
done(std::nullopt, "HTTP " + std::to_string(download->status) + " for " + uri);
|
|
44
|
+
} else if (auto size = system->imageSize(download->bytes)) {
|
|
45
|
+
done(size, "");
|
|
46
|
+
} else {
|
|
47
|
+
done(std::nullopt, "Not an image: " + uri);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
std::string uriArg(const folly::dynamic &args) {
|
|
54
|
+
return args.size() > 0 && args[0].isString() ? args[0].asString() : "";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
} // namespace
|
|
58
|
+
|
|
59
|
+
std::shared_ptr<NativeModule> makeImageLoader(const CoreModuleServices &s) {
|
|
60
|
+
struct ImageLoader : NativeModule {
|
|
61
|
+
explicit ImageLoader(const CoreModuleServices &s) : NativeModule("ImageLoader") {
|
|
62
|
+
constants([] { return folly::dynamic::object(); });
|
|
63
|
+
promise("getSize", [s](const folly::dynamic &args, JsPromise p) {
|
|
64
|
+
measure(s, uriArg(args), {}, [p](auto size, std::string error) {
|
|
65
|
+
if (size) {
|
|
66
|
+
p.resolve(folly::dynamic::array(size->first, size->second));
|
|
67
|
+
} else {
|
|
68
|
+
p.reject(error);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
promise("getSizeWithHeaders", [s](const folly::dynamic &args, JsPromise p) {
|
|
73
|
+
Headers headers;
|
|
74
|
+
if (args.size() > 1 && args[1].isObject()) {
|
|
75
|
+
for (const auto &[k, v] : args[1].items()) {
|
|
76
|
+
headers.emplace_back(k.asString(), v.asString());
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
measure(s, uriArg(args), std::move(headers), [p](auto size, std::string error) {
|
|
80
|
+
if (size) {
|
|
81
|
+
p.resolve(folly::dynamic::object("width", size->first)("height", size->second));
|
|
82
|
+
} else {
|
|
83
|
+
p.reject(error);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
auto prefetch = [s](const folly::dynamic &args, JsPromise p) {
|
|
88
|
+
measure(s, uriArg(args), {}, [p](auto size, std::string error) {
|
|
89
|
+
if (size) {
|
|
90
|
+
p.resolve(true);
|
|
91
|
+
} else {
|
|
92
|
+
p.reject(error);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
promise("prefetchImage", prefetch);
|
|
97
|
+
promise("prefetchImageWithMetadata", prefetch);
|
|
98
|
+
// The OS's URL cache does not say what it holds, so nothing is reported as cached.
|
|
99
|
+
promise("queryCache", [](const folly::dynamic &, JsPromise p) { p.resolve(folly::dynamic::object()); });
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
return std::make_shared<ImageLoader>(s);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
} // namespace ngn
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
#include "ngn/image-manipulator.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/bytes.h"
|
|
4
|
+
#include "ngn/crypto.h"
|
|
5
|
+
#include "ngn/file-system.h"
|
|
6
|
+
|
|
7
|
+
#include <algorithm>
|
|
8
|
+
#include <cmath>
|
|
9
|
+
#include <filesystem>
|
|
10
|
+
#include <fstream>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
#include <random>
|
|
13
|
+
#include <stdexcept>
|
|
14
|
+
#include <variant>
|
|
15
|
+
|
|
16
|
+
#include <folly/json.h>
|
|
17
|
+
|
|
18
|
+
namespace ngn {
|
|
19
|
+
|
|
20
|
+
namespace {
|
|
21
|
+
|
|
22
|
+
constexpr double kPi = 3.14159265358979323846;
|
|
23
|
+
|
|
24
|
+
double number(const folly::dynamic &object, const char *key) {
|
|
25
|
+
if (!object.isObject()) {
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
auto found = object.get_ptr(key);
|
|
29
|
+
return found && found->isNumber() ? found->asDouble() : 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int whole(double value) {
|
|
33
|
+
return std::max(1, static_cast<int>(std::lround(value)));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
ImageStep resized(ImageSize size) {
|
|
37
|
+
return {.kind = ImageStep::Kind::Resize, .size = size};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ImageStep resize(ImageSize current, const folly::dynamic &options) {
|
|
41
|
+
double width = number(options, "width");
|
|
42
|
+
double height = number(options, "height");
|
|
43
|
+
if (width <= 0 && height <= 0) {
|
|
44
|
+
throw std::invalid_argument("resize needs a width or a height above 0");
|
|
45
|
+
}
|
|
46
|
+
double ratio = static_cast<double>(current.width) / current.height;
|
|
47
|
+
if (width <= 0) {
|
|
48
|
+
width = height * ratio;
|
|
49
|
+
} else if (height <= 0) {
|
|
50
|
+
height = width / ratio;
|
|
51
|
+
}
|
|
52
|
+
return resized({whole(width), whole(height)});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
ImageStep rotate(ImageSize current, double degrees) {
|
|
56
|
+
double radians = degrees * kPi / 180;
|
|
57
|
+
double c = std::abs(std::cos(radians));
|
|
58
|
+
double s = std::abs(std::sin(radians));
|
|
59
|
+
// UIKit's frame of the rotated view, rounded down; the nudge keeps 90 degrees from losing a pixel.
|
|
60
|
+
int width = static_cast<int>(std::floor(current.width * c + current.height * s + 1e-9));
|
|
61
|
+
int height = static_cast<int>(std::floor(current.width * s + current.height * c + 1e-9));
|
|
62
|
+
return {.kind = ImageStep::Kind::Rotate, .size = {std::max(1, width), std::max(1, height)}, .degrees = degrees};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
std::string formatted(double value) {
|
|
66
|
+
auto text = std::to_string(value);
|
|
67
|
+
text.erase(text.find_last_not_of('0') + 1);
|
|
68
|
+
return text.back() == '.' ? text + "0" : text;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ImageStep crop(ImageSize current, const folly::dynamic &rect) {
|
|
72
|
+
double x = number(rect, "originX"), y = number(rect, "originY");
|
|
73
|
+
double width = number(rect, "width"), height = number(rect, "height");
|
|
74
|
+
if (x > current.width || y > current.height || width > current.width || height > current.height) {
|
|
75
|
+
throw std::invalid_argument(
|
|
76
|
+
"Invalid crop options has been passed. Please make sure the requested crop rectangle is inside source image");
|
|
77
|
+
}
|
|
78
|
+
// CGImage's cropping: the rectangle grown to whole pixels, then cut to the image.
|
|
79
|
+
double left = std::max(0.0, std::floor(x)), top = std::max(0.0, std::floor(y));
|
|
80
|
+
double right = std::min<double>(current.width, std::ceil(x + width));
|
|
81
|
+
double bottom = std::min<double>(current.height, std::ceil(y + height));
|
|
82
|
+
if (right <= left || bottom <= top) {
|
|
83
|
+
throw std::invalid_argument(
|
|
84
|
+
"Cropping the image to rectangle (x: " + formatted(x) + ", y: " + formatted(y) + ", width: " + formatted(width) +
|
|
85
|
+
", height: " + formatted(height) + ") has failed");
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
.kind = ImageStep::Kind::Crop,
|
|
89
|
+
.size = {static_cast<int>(right - left), static_cast<int>(bottom - top)},
|
|
90
|
+
.x = static_cast<int>(left),
|
|
91
|
+
.y = static_cast<int>(top)};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
ImageStep flip(ImageSize current, const folly::dynamic &direction) {
|
|
95
|
+
auto name = direction.isString() ? direction.asString() : "";
|
|
96
|
+
if (name != "vertical" && name != "horizontal") {
|
|
97
|
+
throw std::invalid_argument("flip is vertical or horizontal, not " + (name.empty() ? folly::toJson(direction) : name));
|
|
98
|
+
}
|
|
99
|
+
return {.kind = name == "vertical" ? ImageStep::Kind::FlipVertical : ImageStep::Kind::FlipHorizontal, .size = current};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
bool isHttpOrData(const std::string &uri) {
|
|
103
|
+
return uri.rfind("http://", 0) == 0 || uri.rfind("https://", 0) == 0 || uri.rfind("data:", 0) == 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
std::string extensionOf(const std::string &format) {
|
|
107
|
+
return format == "png" ? ".png" : format == "webp" ? ".webp" : ".jpg";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
std::string newName(const std::string &format) {
|
|
111
|
+
static std::mutex mutex;
|
|
112
|
+
static std::random_device device;
|
|
113
|
+
std::lock_guard lock(mutex);
|
|
114
|
+
std::string bytes(16, '\0');
|
|
115
|
+
for (auto &byte : bytes) {
|
|
116
|
+
byte = static_cast<char>(device() & 0xff);
|
|
117
|
+
}
|
|
118
|
+
auto id = uuidV4(bytes);
|
|
119
|
+
std::transform(id.begin(), id.end(), id.begin(), [](unsigned char c) { return std::toupper(c); });
|
|
120
|
+
return id + extensionOf(format);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
struct Saving {
|
|
124
|
+
std::string format{"jpeg"};
|
|
125
|
+
double compress{1};
|
|
126
|
+
bool base64{false};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// The save options, or why they cannot be used.
|
|
130
|
+
std::variant<Saving, std::string> savingOf(const folly::dynamic &options, ImageCodec &codec) {
|
|
131
|
+
Saving saving;
|
|
132
|
+
if (!options.isObject()) {
|
|
133
|
+
return saving;
|
|
134
|
+
}
|
|
135
|
+
if (auto format = options.get_ptr("format"); format && format->isString()) {
|
|
136
|
+
saving.format = format->asString() == "jpg" ? "jpeg" : format->asString();
|
|
137
|
+
}
|
|
138
|
+
if (saving.format != "jpeg" && saving.format != "png" && saving.format != "webp") {
|
|
139
|
+
return "the format is jpeg, png or webp, not " + saving.format;
|
|
140
|
+
}
|
|
141
|
+
if (!codec.encodes(saving.format)) {
|
|
142
|
+
return std::string("this Mac cannot write WebP images; save as jpeg or png");
|
|
143
|
+
}
|
|
144
|
+
if (auto compress = options.get_ptr("compress"); compress && compress->isNumber()) {
|
|
145
|
+
saving.compress = std::clamp(compress->asDouble(), 0.0, 1.0);
|
|
146
|
+
}
|
|
147
|
+
if (auto base64 = options.get_ptr("base64"); base64 && base64->isBool()) {
|
|
148
|
+
saving.base64 = base64->asBool();
|
|
149
|
+
}
|
|
150
|
+
return saving;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
} // namespace
|
|
154
|
+
|
|
155
|
+
std::vector<ImageStep> planImageEdits(ImageSize source, const folly::dynamic &actions, const folly::dynamic &loadOptions) {
|
|
156
|
+
std::vector<ImageStep> steps;
|
|
157
|
+
auto current = source;
|
|
158
|
+
auto add = [&](ImageStep step) {
|
|
159
|
+
current = step.size;
|
|
160
|
+
steps.push_back(step);
|
|
161
|
+
};
|
|
162
|
+
double maxWidth = number(loadOptions, "maxWidth"), maxHeight = number(loadOptions, "maxHeight");
|
|
163
|
+
double scale = std::min(
|
|
164
|
+
maxWidth > 0 ? maxWidth / current.width : 1.0, maxHeight > 0 ? maxHeight / current.height : 1.0);
|
|
165
|
+
if (scale < 1) {
|
|
166
|
+
add(resized({whole(current.width * scale), whole(current.height * scale)}));
|
|
167
|
+
}
|
|
168
|
+
if (!actions.isArray()) {
|
|
169
|
+
return steps;
|
|
170
|
+
}
|
|
171
|
+
for (const auto &action : actions) {
|
|
172
|
+
if (!action.isObject()) {
|
|
173
|
+
throw std::invalid_argument("An action is one of resize, rotate, flip or crop");
|
|
174
|
+
}
|
|
175
|
+
if (auto options = action.get_ptr("resize")) {
|
|
176
|
+
add(resize(current, *options));
|
|
177
|
+
} else if (auto degrees = action.get_ptr("rotate"); degrees && degrees->isNumber()) {
|
|
178
|
+
add(rotate(current, degrees->asDouble()));
|
|
179
|
+
} else if (auto direction = action.get_ptr("flip")) {
|
|
180
|
+
add(flip(current, *direction));
|
|
181
|
+
} else if (auto rect = action.get_ptr("crop")) {
|
|
182
|
+
add(crop(current, *rect));
|
|
183
|
+
} else {
|
|
184
|
+
throw std::invalid_argument("An action is one of resize, rotate, flip or crop");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return steps;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
std::shared_ptr<NativeModule> makeImageManipulatorModule(
|
|
191
|
+
std::shared_ptr<ImageCodec> codec, std::shared_ptr<Http> http, std::string folder, std::shared_ptr<FileAccess> files) {
|
|
192
|
+
struct ImageManipulator : NativeModule {
|
|
193
|
+
// Reads the image `args[0]` names and plans `args[1]` and `args[2]` on it, then hands the
|
|
194
|
+
// bytes and the steps on, or rejects.
|
|
195
|
+
using Planned = std::function<void(std::string bytes, std::vector<ImageStep> steps, ImageSize size, JsPromise promise)>;
|
|
196
|
+
|
|
197
|
+
ImageManipulator(std::shared_ptr<ImageCodec> codec, std::shared_ptr<Http> http, std::string folder, std::shared_ptr<FileAccess> files)
|
|
198
|
+
: NativeModule("NgNativeImageManipulator") {
|
|
199
|
+
auto withPlan = [codec, http, files](const folly::dynamic &args, JsPromise promise, Planned next) {
|
|
200
|
+
auto uri = !args.empty() && args[0].isString() ? args[0].asString() : "";
|
|
201
|
+
auto fail = [promise](const std::string &why) { promise.reject("ImageManipulator: Could not load the image: " + why); };
|
|
202
|
+
std::string url;
|
|
203
|
+
std::shared_ptr<FileAccess::Use> use;
|
|
204
|
+
if (auto path = pathOfUri(uri)) {
|
|
205
|
+
try {
|
|
206
|
+
use = readableFile(files.get(), *path);
|
|
207
|
+
} catch (const FileRefused &refused) {
|
|
208
|
+
fail(refused.what());
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
url = fileUri(*path);
|
|
212
|
+
} else if (isHttpOrData(uri)) {
|
|
213
|
+
url = uri;
|
|
214
|
+
} else {
|
|
215
|
+
fail(uri + " is not a file, data or http(s) uri");
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
folly::dynamic actions = args.size() > 1 ? args[1] : folly::dynamic(nullptr);
|
|
219
|
+
folly::dynamic load = args.size() > 2 ? args[2] : folly::dynamic(nullptr);
|
|
220
|
+
struct Read {
|
|
221
|
+
uint16_t status{0};
|
|
222
|
+
std::string bytes;
|
|
223
|
+
std::unique_ptr<HttpTask> task;
|
|
224
|
+
std::shared_ptr<FileAccess::Use> use;
|
|
225
|
+
};
|
|
226
|
+
auto read = std::make_shared<Read>();
|
|
227
|
+
read->use = std::move(use);
|
|
228
|
+
auto label = uri.rfind("data:", 0) == 0 ? std::string("the data uri") : uri;
|
|
229
|
+
read->task = http->send(
|
|
230
|
+
{.url = url},
|
|
231
|
+
{.onResponse = [read](const HttpResponse &response) { read->status = response.status; },
|
|
232
|
+
.onData = [read](std::string chunk, int64_t) { read->bytes += chunk; },
|
|
233
|
+
.onComplete =
|
|
234
|
+
[read, codec, actions, load, label, fail, promise, next](const std::string &error, bool) {
|
|
235
|
+
auto keep = std::move(read->task);
|
|
236
|
+
read->use.reset();
|
|
237
|
+
if (!error.empty()) {
|
|
238
|
+
fail(error);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (read->status >= 400) {
|
|
242
|
+
fail("HTTP " + std::to_string(read->status) + " for " + label);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
auto size = codec->size(read->bytes);
|
|
246
|
+
if (!size || size->width <= 0 || size->height <= 0) {
|
|
247
|
+
fail(label + " is not an image");
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
std::vector<ImageStep> steps;
|
|
251
|
+
try {
|
|
252
|
+
steps = planImageEdits(*size, actions, load);
|
|
253
|
+
} catch (const std::invalid_argument &invalid) {
|
|
254
|
+
promise.reject(std::string("ImageManipulator: ") + invalid.what());
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
auto result = steps.empty() ? *size : steps.back().size;
|
|
258
|
+
next(std::move(read->bytes), std::move(steps), result, promise);
|
|
259
|
+
}});
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
promise("renderAsync", [withPlan](const folly::dynamic &args, JsPromise promise) {
|
|
263
|
+
withPlan(args, std::move(promise), [](std::string, std::vector<ImageStep>, ImageSize size, JsPromise promise) {
|
|
264
|
+
promise.resolve(folly::dynamic::object("width", size.width)("height", size.height));
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
promise("saveAsync", [withPlan, codec, folder](const folly::dynamic &args, JsPromise promise) {
|
|
269
|
+
auto saving = savingOf(args.size() > 3 ? args[3] : folly::dynamic(nullptr), *codec);
|
|
270
|
+
if (auto *why = std::get_if<std::string>(&saving)) {
|
|
271
|
+
promise.reject("ImageManipulator: " + *why);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
auto options = std::get<Saving>(saving);
|
|
275
|
+
withPlan(args, std::move(promise), [codec, folder, options](std::string bytes, std::vector<ImageStep> steps, ImageSize size, JsPromise promise) {
|
|
276
|
+
std::string encoded;
|
|
277
|
+
try {
|
|
278
|
+
encoded = codec->render(bytes, steps, options.format, options.compress);
|
|
279
|
+
} catch (const std::exception &error) {
|
|
280
|
+
promise.reject(std::string("ImageManipulator: ") + error.what());
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
std::error_code ignored;
|
|
284
|
+
std::filesystem::create_directories(folder, ignored);
|
|
285
|
+
auto path = folder + "/" + newName(options.format);
|
|
286
|
+
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
|
287
|
+
out << encoded;
|
|
288
|
+
out.close();
|
|
289
|
+
if (!out) {
|
|
290
|
+
promise.reject("ImageManipulator: Writing image data to the file has failed: " + path);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
folly::dynamic result = folly::dynamic::object("uri", fileUri(path))("width", size.width)("height", size.height);
|
|
294
|
+
if (options.base64) {
|
|
295
|
+
result["base64"] = base64Encode(encoded);
|
|
296
|
+
}
|
|
297
|
+
promise.resolve(std::move(result));
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
return std::make_shared<ImageManipulator>(std::move(codec), std::move(http), std::move(folder), std::move(files));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
} // namespace ngn
|