@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,663 @@
|
|
|
1
|
+
#import "cg-canvas.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/file-system.h"
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <cmath>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
CGColorRef createColorImpl(uint32_t argb, double opacity) {
|
|
12
|
+
CGFloat components[] = {
|
|
13
|
+
((argb >> 16) & 0xff) / 255.0,
|
|
14
|
+
((argb >> 8) & 0xff) / 255.0,
|
|
15
|
+
(argb & 0xff) / 255.0,
|
|
16
|
+
((argb >> 24) & 0xff) / 255.0 * opacity,
|
|
17
|
+
};
|
|
18
|
+
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
19
|
+
CGColorRef color = CGColorCreate(space, components);
|
|
20
|
+
CGColorSpaceRelease(space);
|
|
21
|
+
return color;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
CGAffineTransform affine(const ngn::SvgMatrix &m) {
|
|
25
|
+
return CGAffineTransformMake(m.a, m.b, m.c, m.d, m.e, m.f);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
CGPathRef createPathImpl(const ngn::SvgPath &path) {
|
|
29
|
+
CGMutablePathRef out = CGPathCreateMutable();
|
|
30
|
+
for (const auto &s : path.segments) {
|
|
31
|
+
switch (s.op) {
|
|
32
|
+
case ngn::SvgPath::Op::Move:
|
|
33
|
+
CGPathMoveToPoint(out, nullptr, s.points[0], s.points[1]);
|
|
34
|
+
break;
|
|
35
|
+
case ngn::SvgPath::Op::Line:
|
|
36
|
+
CGPathAddLineToPoint(out, nullptr, s.points[0], s.points[1]);
|
|
37
|
+
break;
|
|
38
|
+
case ngn::SvgPath::Op::Cubic:
|
|
39
|
+
CGPathAddCurveToPoint(out, nullptr, s.points[0], s.points[1], s.points[2], s.points[3], s.points[4], s.points[5]);
|
|
40
|
+
break;
|
|
41
|
+
case ngn::SvgPath::Op::Close:
|
|
42
|
+
CGPathCloseSubpath(out);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Images an svg draws, by URI, loaded once for every svg.
|
|
50
|
+
// The glyphs a cluster drew as in its shaped run: per font, each glyph and where it sits from the
|
|
51
|
+
// cluster's start.
|
|
52
|
+
struct ShapedGlyphs {
|
|
53
|
+
struct Piece {
|
|
54
|
+
CTFontRef font{nullptr};
|
|
55
|
+
std::vector<CGGlyph> glyphs;
|
|
56
|
+
std::vector<CGPoint> positions;
|
|
57
|
+
};
|
|
58
|
+
std::vector<Piece> pieces;
|
|
59
|
+
ShapedGlyphs() = default;
|
|
60
|
+
ShapedGlyphs(const ShapedGlyphs &) = delete;
|
|
61
|
+
ShapedGlyphs &operator=(const ShapedGlyphs &) = delete;
|
|
62
|
+
~ShapedGlyphs() {
|
|
63
|
+
for (auto &p : pieces) {
|
|
64
|
+
CFRelease(p.font);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
void add(CTFontRef font, CGGlyph glyph, CGPoint position) {
|
|
68
|
+
if (pieces.empty() || pieces.back().font != font) {
|
|
69
|
+
pieces.push_back({.font = static_cast<CTFontRef>(CFRetain(font))});
|
|
70
|
+
}
|
|
71
|
+
pieces.back().glyphs.push_back(glyph);
|
|
72
|
+
pieces.back().positions.push_back(position);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
NSCache<NSString *, NSImage *> *imageCache() {
|
|
77
|
+
static NSCache<NSString *, NSImage *> *cache = [NSCache new];
|
|
78
|
+
return cache;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The canvas drawSvg draws on: Core Graphics calls on the view's context, or on a mask's while
|
|
82
|
+
// one is drawn. It tracks the user transform itself, so a mask, drawn in a context of its own,
|
|
83
|
+
// starts where the drawing is.
|
|
84
|
+
class CGCanvas final : public ngn::SvgCanvas {
|
|
85
|
+
public:
|
|
86
|
+
CGCanvas(CGContextRef context, CGSize size, CGFloat scale, void (^imageLoaded)(void), NSView * (^viewFor)(ngn::Tag))
|
|
87
|
+
: context_(context), size_(size), scale_(scale), imageLoaded_(imageLoaded), viewFor_(viewFor) {}
|
|
88
|
+
|
|
89
|
+
void save() override {
|
|
90
|
+
CGContextSaveGState(context_);
|
|
91
|
+
saved_.push_back(matrix_);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
void restore() override {
|
|
95
|
+
CGContextRestoreGState(context_);
|
|
96
|
+
if (!saved_.empty()) {
|
|
97
|
+
matrix_ = saved_.back();
|
|
98
|
+
saved_.pop_back();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void concat(const ngn::SvgMatrix &m) override {
|
|
103
|
+
CGContextConcatCTM(context_, affine(m));
|
|
104
|
+
matrix_ = matrix_ * m;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void clip(const ngn::SvgPath &path, bool evenOdd) override {
|
|
108
|
+
CGPathRef p = createPathImpl(path);
|
|
109
|
+
CGContextAddPath(context_, p);
|
|
110
|
+
CGPathRelease(p);
|
|
111
|
+
if (evenOdd) {
|
|
112
|
+
CGContextEOClip(context_);
|
|
113
|
+
} else {
|
|
114
|
+
CGContextClip(context_);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void fill(const ngn::SvgPath &path, bool evenOdd, const ngn::SvgPaint &paint) override {
|
|
119
|
+
CGPathRef p = createPathImpl(path);
|
|
120
|
+
if (paint.gradient) {
|
|
121
|
+
CGContextSaveGState(context_);
|
|
122
|
+
CGContextAddPath(context_, p);
|
|
123
|
+
evenOdd ? CGContextEOClip(context_) : CGContextClip(context_);
|
|
124
|
+
drawGradient(*paint.gradient, paint.opacity);
|
|
125
|
+
CGContextRestoreGState(context_);
|
|
126
|
+
} else {
|
|
127
|
+
CGColorRef color = createColorImpl(paint.argb, paint.opacity);
|
|
128
|
+
CGContextSetFillColorWithColor(context_, color);
|
|
129
|
+
CGColorRelease(color);
|
|
130
|
+
CGContextAddPath(context_, p);
|
|
131
|
+
CGContextDrawPath(context_, evenOdd ? kCGPathEOFill : kCGPathFill);
|
|
132
|
+
}
|
|
133
|
+
CGPathRelease(p);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void applyStroke(const ngn::SvgStroke &stroke) {
|
|
137
|
+
CGContextSetLineWidth(context_, stroke.width);
|
|
138
|
+
CGContextSetLineCap(context_, stroke.cap == 1 ? kCGLineCapRound : stroke.cap == 2 ? kCGLineCapSquare : kCGLineCapButt);
|
|
139
|
+
CGContextSetLineJoin(context_, stroke.join == 1 ? kCGLineJoinRound : stroke.join == 2 ? kCGLineJoinBevel : kCGLineJoinMiter);
|
|
140
|
+
CGContextSetMiterLimit(context_, stroke.miterLimit);
|
|
141
|
+
if (!stroke.dashes.empty()) {
|
|
142
|
+
std::vector<CGFloat> dashes(stroke.dashes.begin(), stroke.dashes.end());
|
|
143
|
+
CGContextSetLineDash(context_, stroke.dashOffset, dashes.data(), dashes.size());
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void stroke(const ngn::SvgPath &path, const ngn::SvgStroke &stroke, const ngn::SvgPaint &paint) override {
|
|
148
|
+
CGContextSaveGState(context_);
|
|
149
|
+
applyStroke(stroke);
|
|
150
|
+
CGPathRef p = createPathImpl(path);
|
|
151
|
+
CGContextAddPath(context_, p);
|
|
152
|
+
CGPathRelease(p);
|
|
153
|
+
if (paint.gradient) {
|
|
154
|
+
CGContextReplacePathWithStrokedPath(context_);
|
|
155
|
+
CGContextClip(context_);
|
|
156
|
+
drawGradient(*paint.gradient, paint.opacity);
|
|
157
|
+
} else {
|
|
158
|
+
CGColorRef color = createColorImpl(paint.argb, paint.opacity);
|
|
159
|
+
CGContextSetStrokeColorWithColor(context_, color);
|
|
160
|
+
CGColorRelease(color);
|
|
161
|
+
CGContextStrokePath(context_);
|
|
162
|
+
}
|
|
163
|
+
CGContextRestoreGState(context_);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
void beginLayer(double opacity) override {
|
|
167
|
+
CGContextSaveGState(context_);
|
|
168
|
+
CGContextSetAlpha(context_, opacity);
|
|
169
|
+
CGContextBeginTransparencyLayer(context_, nullptr);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
void endLayer() override {
|
|
173
|
+
CGContextEndTransparencyLayer(context_);
|
|
174
|
+
CGContextRestoreGState(context_);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// A luminance mask is drawn in a grey bitmap of the view's pixels, black where nothing is
|
|
178
|
+
// drawn, so its grey is the luminance times the alpha SVG masks by. An alpha mask is drawn in an
|
|
179
|
+
// alpha-only bitmap, clear where nothing is drawn, so only its content's alpha counts. A mask
|
|
180
|
+
// whose bitmap cannot be made (too many pixels) masks nothing out.
|
|
181
|
+
void beginMask(bool alpha) override {
|
|
182
|
+
size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.width * scale_)));
|
|
183
|
+
size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.height * scale_)));
|
|
184
|
+
CGContextRef mask;
|
|
185
|
+
if (alpha) {
|
|
186
|
+
mask = CGBitmapContextCreate(nullptr, width, height, 8, 0, nullptr, kCGImageAlphaOnly);
|
|
187
|
+
if (mask != nullptr) {
|
|
188
|
+
CGContextClearRect(mask, CGRectMake(0, 0, width, height));
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
|
|
192
|
+
mask = CGBitmapContextCreate(nullptr, width, height, 8, 0, grey, kCGImageAlphaNone);
|
|
193
|
+
CGColorSpaceRelease(grey);
|
|
194
|
+
if (mask != nullptr) {
|
|
195
|
+
CGContextSetGrayFillColor(mask, 0, 1);
|
|
196
|
+
CGContextFillRect(mask, CGRectMake(0, 0, width, height));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (mask == nullptr) {
|
|
200
|
+
// The mask's content is drawn into a context that keeps nothing, and the clip is skipped.
|
|
201
|
+
masks_.push_back({.parent = context_, .mask = nullptr, .saved = saved_, .alpha = alpha, .scratch = scratchContext()});
|
|
202
|
+
context_ = masks_.back().scratch;
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
// The view's coordinates: top left, y down, in points.
|
|
206
|
+
CGContextTranslateCTM(mask, 0, height);
|
|
207
|
+
CGContextScaleCTM(mask, scale_, -scale_);
|
|
208
|
+
CGContextConcatCTM(mask, affine(matrix_));
|
|
209
|
+
masks_.push_back({.parent = context_, .mask = mask, .saved = saved_, .alpha = alpha});
|
|
210
|
+
context_ = mask;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
void endMask() override {
|
|
214
|
+
if (masks_.empty()) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
auto frame = masks_.back();
|
|
218
|
+
context_ = frame.parent;
|
|
219
|
+
saved_ = frame.saved;
|
|
220
|
+
if (frame.mask == nullptr) {
|
|
221
|
+
CGContextRelease(frame.scratch);
|
|
222
|
+
masks_.back().scratch = nullptr;
|
|
223
|
+
CGContextSaveGState(context_);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
// A clip mask is a grey image: an alpha mask's alpha bytes are read as grey.
|
|
227
|
+
CGImageRef image;
|
|
228
|
+
if (frame.alpha) {
|
|
229
|
+
size_t width = CGBitmapContextGetWidth(frame.mask), height = CGBitmapContextGetHeight(frame.mask);
|
|
230
|
+
size_t row = CGBitmapContextGetBytesPerRow(frame.mask);
|
|
231
|
+
CFDataRef bytes = CFDataCreate(nullptr, static_cast<const UInt8 *>(CGBitmapContextGetData(frame.mask)), static_cast<CFIndex>(row * height));
|
|
232
|
+
CGDataProviderRef provider = CGDataProviderCreateWithCFData(bytes);
|
|
233
|
+
CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
|
|
234
|
+
image = CGImageCreate(width, height, 8, 8, row, grey, static_cast<CGBitmapInfo>(kCGImageAlphaNone), provider, nullptr, false,
|
|
235
|
+
kCGRenderingIntentDefault);
|
|
236
|
+
CGColorSpaceRelease(grey);
|
|
237
|
+
CGDataProviderRelease(provider);
|
|
238
|
+
CFRelease(bytes);
|
|
239
|
+
} else {
|
|
240
|
+
image = CGBitmapContextCreateImage(frame.mask);
|
|
241
|
+
}
|
|
242
|
+
CGContextRelease(frame.mask);
|
|
243
|
+
masks_.back().mask = nullptr;
|
|
244
|
+
// Clip in the view's coordinates, the image's top row at the view's top, then come back.
|
|
245
|
+
CGContextSaveGState(context_);
|
|
246
|
+
CGAffineTransform toView = CGAffineTransformInvert(affine(matrix_));
|
|
247
|
+
CGContextConcatCTM(context_, toView);
|
|
248
|
+
CGContextTranslateCTM(context_, 0, size_.height);
|
|
249
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
250
|
+
CGContextClipToMask(context_, CGRectMake(0, 0, size_.width, size_.height), image);
|
|
251
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
252
|
+
CGContextTranslateCTM(context_, 0, -size_.height);
|
|
253
|
+
CGContextConcatCTM(context_, affine(matrix_));
|
|
254
|
+
CGImageRelease(image);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
void endMasked() override {
|
|
258
|
+
if (masks_.empty()) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
masks_.pop_back();
|
|
262
|
+
CGContextRestoreGState(context_);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
void clipToStroke(const ngn::SvgPath &path, const ngn::SvgStroke &stroke) override {
|
|
266
|
+
applyStroke(stroke);
|
|
267
|
+
CGPathRef p = createPathImpl(path);
|
|
268
|
+
CGContextAddPath(context_, p);
|
|
269
|
+
CGPathRelease(p);
|
|
270
|
+
CGContextReplacePathWithStrokedPath(context_);
|
|
271
|
+
CGContextClip(context_);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// The tile is drawn once into a bitmap at the pixels it covers, and the bitmap tiled over the
|
|
275
|
+
// clip. The bitmap's rows run from the tile's top, as the view's do.
|
|
276
|
+
void pattern(const ngn::SvgPatternTile &tile, double opacity, const std::function<void(ngn::SvgCanvas &)> &drawTile) override {
|
|
277
|
+
ngn::SvgMatrix toView = matrix_ * tile.transform;
|
|
278
|
+
double density = scale_ * std::sqrt(std::abs(toView.a * toView.d - toView.b * toView.c));
|
|
279
|
+
if (!(density > 0)) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// At most 2048 pixels a side: a larger tile is drawn coarser.
|
|
283
|
+
density = std::min({density, 2048 / tile.tile.width, 2048 / tile.tile.height});
|
|
284
|
+
size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(tile.tile.width * density)));
|
|
285
|
+
size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(tile.tile.height * density)));
|
|
286
|
+
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
287
|
+
CGContextRef bitmap = CGBitmapContextCreate(nullptr, width, height, 8, 0, space, kCGImageAlphaPremultipliedLast);
|
|
288
|
+
CGColorSpaceRelease(space);
|
|
289
|
+
// A tile whose bitmap cannot be made paints nothing.
|
|
290
|
+
if (bitmap == nullptr) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
CGContextTranslateCTM(bitmap, 0, height);
|
|
294
|
+
CGContextScaleCTM(bitmap, width / tile.tile.width, -(height / tile.tile.height));
|
|
295
|
+
{
|
|
296
|
+
CGCanvas inner(bitmap, CGSizeMake(tile.tile.width, tile.tile.height), density, imageLoaded_, viewFor_);
|
|
297
|
+
drawTile(inner);
|
|
298
|
+
}
|
|
299
|
+
CGImageRef image = CGBitmapContextCreateImage(bitmap);
|
|
300
|
+
CGContextRelease(bitmap);
|
|
301
|
+
CGContextSaveGState(context_);
|
|
302
|
+
CGContextSetAlpha(context_, opacity);
|
|
303
|
+
CGContextConcatCTM(context_, affine(tile.transform));
|
|
304
|
+
CGContextTranslateCTM(context_, tile.tile.x, tile.tile.y);
|
|
305
|
+
// Drawn in a space whose y runs up, so the image stands the right way; the tiles keep their
|
|
306
|
+
// places, every tile's height apart.
|
|
307
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
308
|
+
CGContextDrawTiledImage(context_, CGRectMake(0, 0, tile.tile.width, tile.tile.height), image);
|
|
309
|
+
CGContextRestoreGState(context_);
|
|
310
|
+
CGImageRelease(image);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The source is drawn in a bitmap of the view's pixels, as a mask is, and the filter's result
|
|
314
|
+
// drawn back in its place.
|
|
315
|
+
void beginFilter(const ngn::SvgFilter &filter) override {
|
|
316
|
+
size_t width = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.width * scale_)));
|
|
317
|
+
size_t height = std::max<size_t>(1, static_cast<size_t>(std::ceil(size_.height * scale_)));
|
|
318
|
+
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
319
|
+
CGContextRef bitmap = CGBitmapContextCreate(nullptr, width, height, 8, width * 4, space, kCGImageAlphaPremultipliedLast);
|
|
320
|
+
CGColorSpaceRelease(space);
|
|
321
|
+
// With no bitmap (too many pixels) the source is drawn as it is, unfiltered.
|
|
322
|
+
if (bitmap == nullptr) {
|
|
323
|
+
filters_.push_back({.parent = context_, .bitmap = nullptr, .saved = saved_, .filter = filter, .width = 0, .height = 0});
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
CGContextTranslateCTM(bitmap, 0, height);
|
|
327
|
+
CGContextScaleCTM(bitmap, scale_, -scale_);
|
|
328
|
+
CGContextConcatCTM(bitmap, affine(matrix_));
|
|
329
|
+
filters_.push_back({.parent = context_, .bitmap = bitmap, .saved = saved_, .filter = filter, .width = width, .height = height});
|
|
330
|
+
context_ = bitmap;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
void endFilter() override {
|
|
334
|
+
if (filters_.empty()) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
auto frame = std::move(filters_.back());
|
|
338
|
+
filters_.pop_back();
|
|
339
|
+
if (frame.bitmap == nullptr) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
context_ = frame.parent;
|
|
343
|
+
saved_ = frame.saved;
|
|
344
|
+
ngn::SvgPixels source{.width = static_cast<int>(frame.width), .height = static_cast<int>(frame.height)};
|
|
345
|
+
const auto *data = static_cast<const uint8_t *>(CGBitmapContextGetData(frame.bitmap));
|
|
346
|
+
source.rgba.assign(data, data + frame.width * frame.height * 4);
|
|
347
|
+
CGContextRelease(frame.bitmap);
|
|
348
|
+
auto result = ngn::runSvgFilter(frame.filter, source, ngn::SvgMatrix::scale(scale_, scale_) * matrix_);
|
|
349
|
+
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
350
|
+
CFDataRef bytes = CFDataCreate(nullptr, result.rgba.data(), static_cast<CFIndex>(result.rgba.size()));
|
|
351
|
+
CGDataProviderRef provider = CGDataProviderCreateWithCFData(bytes);
|
|
352
|
+
CGImageRef image = CGImageCreate(
|
|
353
|
+
frame.width, frame.height, 8, 32, frame.width * 4, space,
|
|
354
|
+
static_cast<CGBitmapInfo>(kCGImageAlphaPremultipliedLast), provider, nullptr, false, kCGRenderingIntentDefault);
|
|
355
|
+
CGDataProviderRelease(provider);
|
|
356
|
+
CFRelease(bytes);
|
|
357
|
+
CGColorSpaceRelease(space);
|
|
358
|
+
// In the view's coordinates, the image's top row at the view's top.
|
|
359
|
+
CGContextSaveGState(context_);
|
|
360
|
+
CGContextConcatCTM(context_, CGAffineTransformInvert(affine(matrix_)));
|
|
361
|
+
CGContextTranslateCTM(context_, 0, size_.height);
|
|
362
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
363
|
+
CGContextDrawImage(context_, CGRectMake(0, 0, frame.width / scale_, frame.height / scale_), image);
|
|
364
|
+
CGContextRestoreGState(context_);
|
|
365
|
+
CGImageRelease(image);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
void image(const std::string &uri, const ngn::SvgRect &rect, const std::string &align, int meetOrSlice, double opacity) override {
|
|
369
|
+
NSString *key = @(uri.c_str());
|
|
370
|
+
NSImage *image = [imageCache() objectForKey:key];
|
|
371
|
+
if (image == nil) {
|
|
372
|
+
load(key);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
NSSize natural = image.size;
|
|
376
|
+
if (natural.width <= 0 || natural.height <= 0) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
auto fit = ngn::svgViewBoxTransform({0, 0, natural.width, natural.height}, align, meetOrSlice, rect.width, rect.height);
|
|
380
|
+
CGContextSaveGState(context_);
|
|
381
|
+
CGContextClipToRect(context_, CGRectMake(rect.x, rect.y, rect.width, rect.height));
|
|
382
|
+
CGContextTranslateCTM(context_, rect.x, rect.y);
|
|
383
|
+
CGContextConcatCTM(context_, affine(fit));
|
|
384
|
+
NSGraphicsContext *previous = NSGraphicsContext.currentContext;
|
|
385
|
+
NSGraphicsContext.currentContext = [NSGraphicsContext graphicsContextWithCGContext:context_ flipped:YES];
|
|
386
|
+
[image drawInRect:NSMakeRect(0, 0, natural.width, natural.height)
|
|
387
|
+
fromRect:NSZeroRect
|
|
388
|
+
operation:NSCompositingOperationSourceOver
|
|
389
|
+
fraction:opacity
|
|
390
|
+
respectFlipped:YES
|
|
391
|
+
hints:nil];
|
|
392
|
+
NSGraphicsContext.currentContext = previous;
|
|
393
|
+
CGContextRestoreGState(context_);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
double measureText(const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
397
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, nullptr);
|
|
398
|
+
double width = CTLineGetTypographicBounds(line, nullptr, nullptr, nullptr);
|
|
399
|
+
CFRelease(line);
|
|
400
|
+
return width;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// The composed character sequences of `text` (Foundation's grapheme clusters), each with the
|
|
404
|
+
// glyphs Core Text's shaped line has for it, where it has them. A cluster with no glyph of its
|
|
405
|
+
// own, the tail of a ligature, joins the one before it.
|
|
406
|
+
std::vector<ngn::SvgGlyphCluster> glyphClusters(
|
|
407
|
+
const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
408
|
+
NSString *string = @(text.c_str());
|
|
409
|
+
if (string == nil) {
|
|
410
|
+
string = @"";
|
|
411
|
+
}
|
|
412
|
+
std::vector<NSRange> ranges;
|
|
413
|
+
std::vector<size_t> clusterOf(string.length);
|
|
414
|
+
for (NSUInteger i = 0; i < string.length;) {
|
|
415
|
+
NSRange range = [string rangeOfComposedCharacterSequenceAtIndex:i];
|
|
416
|
+
std::fill(clusterOf.begin() + static_cast<long>(range.location), clusterOf.begin() + static_cast<long>(NSMaxRange(range)), ranges.size());
|
|
417
|
+
ranges.push_back(range);
|
|
418
|
+
i = NSMaxRange(range);
|
|
419
|
+
}
|
|
420
|
+
std::vector<double> left(ranges.size(), INFINITY), right(ranges.size(), -INFINITY);
|
|
421
|
+
struct Glyph {
|
|
422
|
+
CTFontRef font;
|
|
423
|
+
CGGlyph glyph;
|
|
424
|
+
CGPoint position;
|
|
425
|
+
};
|
|
426
|
+
std::vector<std::vector<Glyph>> glyphs(ranges.size());
|
|
427
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, nullptr);
|
|
428
|
+
for (id item in (__bridge NSArray *)CTLineGetGlyphRuns(line)) {
|
|
429
|
+
auto run = (__bridge CTRunRef)item;
|
|
430
|
+
auto font = (CTFontRef)CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
|
|
431
|
+
CFIndex count = CTRunGetGlyphCount(run);
|
|
432
|
+
std::vector<CGGlyph> ids(static_cast<size_t>(count));
|
|
433
|
+
std::vector<CGPoint> positions(static_cast<size_t>(count));
|
|
434
|
+
std::vector<CGSize> advances(static_cast<size_t>(count));
|
|
435
|
+
std::vector<CFIndex> indices(static_cast<size_t>(count));
|
|
436
|
+
CTRunGetGlyphs(run, CFRangeMake(0, 0), ids.data());
|
|
437
|
+
CTRunGetPositions(run, CFRangeMake(0, 0), positions.data());
|
|
438
|
+
CTRunGetAdvances(run, CFRangeMake(0, 0), advances.data());
|
|
439
|
+
CTRunGetStringIndices(run, CFRangeMake(0, 0), indices.data());
|
|
440
|
+
for (size_t g = 0; g < positions.size(); ++g) {
|
|
441
|
+
if (font == nullptr || indices[g] < 0 || static_cast<size_t>(indices[g]) >= clusterOf.size()) {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
size_t c = clusterOf[static_cast<size_t>(indices[g])];
|
|
445
|
+
left[c] = std::min(left[c], static_cast<double>(positions[g].x));
|
|
446
|
+
right[c] = std::max(right[c], static_cast<double>(positions[g].x + advances[g].width));
|
|
447
|
+
glyphs[c].push_back({font, ids[g], positions[g]});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
std::vector<ngn::SvgGlyphCluster> out;
|
|
451
|
+
for (size_t c = 0; c < ranges.size(); ++c) {
|
|
452
|
+
std::string part = [string substringWithRange:ranges[c]].UTF8String;
|
|
453
|
+
if (glyphs[c].empty() && !out.empty()) {
|
|
454
|
+
out.back().text += part;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
auto shaped = std::make_shared<ShapedGlyphs>();
|
|
458
|
+
for (const auto &g : glyphs[c]) {
|
|
459
|
+
shaped->add(g.font, g.glyph, CGPointMake(g.position.x - left[c], g.position.y));
|
|
460
|
+
}
|
|
461
|
+
bool placed = !glyphs[c].empty();
|
|
462
|
+
out.push_back({.text = std::move(part), .x = placed ? left[c] : 0, .advance = placed ? right[c] - left[c] : 0, .glyphs = shaped});
|
|
463
|
+
}
|
|
464
|
+
CFRelease(line);
|
|
465
|
+
return out;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
void glyphCluster(const ngn::SvgGlyphCluster &cluster, double x, double y, const ngn::SvgPaint &fill) override {
|
|
469
|
+
auto shaped = std::static_pointer_cast<const ShapedGlyphs>(cluster.glyphs);
|
|
470
|
+
if (!shaped) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
uint32_t argb = fill.gradient && !fill.gradient->stops.empty() ? fill.gradient->stops[0].second : fill.argb;
|
|
474
|
+
CGColorRef color = createColorImpl(argb, fill.opacity);
|
|
475
|
+
CGContextSaveGState(context_);
|
|
476
|
+
CGContextTranslateCTM(context_, x, y);
|
|
477
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
478
|
+
CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
|
|
479
|
+
CGContextSetFillColorWithColor(context_, color);
|
|
480
|
+
for (const auto &p : shaped->pieces) {
|
|
481
|
+
CTFontDrawGlyphs(p.font, p.glyphs.data(), p.positions.data(), p.glyphs.size(), context_);
|
|
482
|
+
}
|
|
483
|
+
CGContextRestoreGState(context_);
|
|
484
|
+
CGColorRelease(color);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
void text(
|
|
488
|
+
const std::string &text,
|
|
489
|
+
double x,
|
|
490
|
+
double y,
|
|
491
|
+
double fontSize,
|
|
492
|
+
const std::string &fontFamily,
|
|
493
|
+
const std::string &fontWeight,
|
|
494
|
+
const ngn::SvgPaint &fill) override {
|
|
495
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, &fill);
|
|
496
|
+
CGContextSaveGState(context_);
|
|
497
|
+
CGContextTranslateCTM(context_, x, y);
|
|
498
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
499
|
+
CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
|
|
500
|
+
CGContextSetTextPosition(context_, 0, 0);
|
|
501
|
+
CTLineDraw(line, context_);
|
|
502
|
+
CGContextRestoreGState(context_);
|
|
503
|
+
CFRelease(line);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Each child of the view as its layer draws it, as react-native-svg's ForeignObject draws them on
|
|
507
|
+
// iOS: its frame's corner, its transform (which rendering a layer leaves out), then its layer and
|
|
508
|
+
// those under it. A layer renders though an ancestor view is hidden, so the views stay hidden and
|
|
509
|
+
// take no clicks; a hidden one's layer renders nothing.
|
|
510
|
+
void views(ngn::Tag view) override {
|
|
511
|
+
NSView *object = viewFor_ != nil ? viewFor_(view) : nil;
|
|
512
|
+
for (NSView *child in object.subviews) {
|
|
513
|
+
CGContextSaveGState(context_);
|
|
514
|
+
CGContextTranslateCTM(context_, child.frame.origin.x, child.frame.origin.y);
|
|
515
|
+
CGContextConcatCTM(context_, CATransform3DGetAffineTransform(child.layer.transform));
|
|
516
|
+
[child.layer renderInContext:context_];
|
|
517
|
+
CGContextRestoreGState(context_);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
private:
|
|
522
|
+
// A +1 line of `text` in the font, in `fill`'s colour (a gradient's first stop) when given.
|
|
523
|
+
static CTLineRef createLine(
|
|
524
|
+
const std::string &text,
|
|
525
|
+
double fontSize,
|
|
526
|
+
const std::string &fontFamily,
|
|
527
|
+
const std::string &fontWeight,
|
|
528
|
+
const ngn::SvgPaint *fill) {
|
|
529
|
+
NSFont *font = fontFamily.empty() ? nil : [NSFont fontWithName:@(fontFamily.c_str()) size:fontSize];
|
|
530
|
+
if (font == nil) {
|
|
531
|
+
int weight = fontWeight == "bold" ? 700 : std::atoi(fontWeight.c_str());
|
|
532
|
+
font = [NSFont systemFontOfSize:fontSize weight:weight >= 600 ? NSFontWeightBold : weight >= 500 ? NSFontWeightMedium : NSFontWeightRegular];
|
|
533
|
+
}
|
|
534
|
+
uint32_t argb = fill ? (fill->gradient && !fill->gradient->stops.empty() ? fill->gradient->stops[0].second : fill->argb) : 0;
|
|
535
|
+
CGColorRef color = createColorImpl(argb, fill ? fill->opacity : 0);
|
|
536
|
+
NSAttributedString *string = [[NSAttributedString alloc]
|
|
537
|
+
initWithString:@(text.c_str())
|
|
538
|
+
attributes:@{NSFontAttributeName : font, (__bridge id)kCTForegroundColorAttributeName : (__bridge id)color}];
|
|
539
|
+
CGColorRelease(color);
|
|
540
|
+
return CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)string);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
struct Mask {
|
|
544
|
+
CGContextRef parent;
|
|
545
|
+
CGContextRef mask;
|
|
546
|
+
std::vector<ngn::SvgMatrix> saved;
|
|
547
|
+
bool alpha{false};
|
|
548
|
+
// Where a mask with no bitmap has its content drawn, and dropped.
|
|
549
|
+
CGContextRef scratch{nullptr};
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
// A one-pixel context that the content of a mask with no bitmap is drawn into.
|
|
553
|
+
static CGContextRef scratchContext() {
|
|
554
|
+
CGColorSpaceRef grey = CGColorSpaceCreateDeviceGray();
|
|
555
|
+
CGContextRef scratch = CGBitmapContextCreate(nullptr, 1, 1, 8, 0, grey, kCGImageAlphaNone);
|
|
556
|
+
CGColorSpaceRelease(grey);
|
|
557
|
+
return scratch;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
struct Filter {
|
|
561
|
+
CGContextRef parent;
|
|
562
|
+
CGContextRef bitmap;
|
|
563
|
+
std::vector<ngn::SvgMatrix> saved;
|
|
564
|
+
ngn::SvgFilter filter;
|
|
565
|
+
size_t width, height;
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
void drawGradient(const ngn::SvgGradient &g, double opacity) {
|
|
569
|
+
std::vector<CGFloat> components;
|
|
570
|
+
std::vector<CGFloat> locations;
|
|
571
|
+
for (const auto &[offset, argb] : g.stops) {
|
|
572
|
+
components.push_back(((argb >> 16) & 0xff) / 255.0);
|
|
573
|
+
components.push_back(((argb >> 8) & 0xff) / 255.0);
|
|
574
|
+
components.push_back((argb & 0xff) / 255.0);
|
|
575
|
+
components.push_back(((argb >> 24) & 0xff) / 255.0);
|
|
576
|
+
// Offsets only grow, as SVG clamps a smaller one to the one before.
|
|
577
|
+
locations.push_back(std::clamp(locations.empty() ? offset : std::max(offset, double(locations.back())), 0.0, 1.0));
|
|
578
|
+
}
|
|
579
|
+
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
580
|
+
CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components.data(), locations.data(), locations.size());
|
|
581
|
+
CGColorSpaceRelease(space);
|
|
582
|
+
CGContextSaveGState(context_);
|
|
583
|
+
CGContextSetAlpha(context_, opacity);
|
|
584
|
+
CGContextConcatCTM(context_, affine(g.transform));
|
|
585
|
+
auto extend = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
|
|
586
|
+
if (g.radial) {
|
|
587
|
+
CGContextDrawRadialGradient(context_, gradient, CGPointMake(g.x2, g.y2), 0, CGPointMake(g.x1, g.y1), g.r, extend);
|
|
588
|
+
} else {
|
|
589
|
+
CGContextDrawLinearGradient(context_, gradient, CGPointMake(g.x1, g.y1), CGPointMake(g.x2, g.y2), extend);
|
|
590
|
+
}
|
|
591
|
+
CGContextRestoreGState(context_);
|
|
592
|
+
CGGradientRelease(gradient);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// Every canvas that asks for a uri while it loads is told when it is in the cache.
|
|
596
|
+
void load(NSString *uri) {
|
|
597
|
+
static NSMutableDictionary<NSString *, NSMutableArray *> *loading = [NSMutableDictionary dictionary];
|
|
598
|
+
if (NSMutableArray *waiters = loading[uri]) {
|
|
599
|
+
if (imageLoaded_ != nil) {
|
|
600
|
+
[waiters addObject:[imageLoaded_ copy]];
|
|
601
|
+
}
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
// An <image> href's file only inside the file boundary, and exactly the file checked.
|
|
605
|
+
std::shared_ptr<ngn::FileAccess::Use> use;
|
|
606
|
+
NSURL *url = nil;
|
|
607
|
+
try {
|
|
608
|
+
auto source = ngn::readableSource(ngn::viewFileAccess().get(), uri.UTF8String);
|
|
609
|
+
url = [NSURL URLWithString:[NSString stringWithUTF8String:source.uri.c_str()]];
|
|
610
|
+
use = std::move(source.use);
|
|
611
|
+
} catch (const ngn::FileRefused &) {
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (url == nil) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
loading[uri] = imageLoaded_ != nil ? [NSMutableArray arrayWithObject:[imageLoaded_ copy]] : [NSMutableArray array];
|
|
618
|
+
[[NSURLSession.sharedSession dataTaskWithURL:url
|
|
619
|
+
completionHandler:^(NSData *data, NSURLResponse *, NSError *) {
|
|
620
|
+
(void)use;
|
|
621
|
+
NSImage *image = data ? [[NSImage alloc] initWithData:data] : nil;
|
|
622
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
623
|
+
NSArray *waiters = loading[uri];
|
|
624
|
+
[loading removeObjectForKey:uri];
|
|
625
|
+
if (image != nil) {
|
|
626
|
+
[imageCache() setObject:image forKey:uri];
|
|
627
|
+
for (void (^waiter)(void) in waiters) {
|
|
628
|
+
waiter();
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}] resume];
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
CGContextRef context_;
|
|
636
|
+
CGSize size_;
|
|
637
|
+
CGFloat scale_;
|
|
638
|
+
void (^imageLoaded_)(void);
|
|
639
|
+
NSView * (^viewFor_)(ngn::Tag);
|
|
640
|
+
ngn::SvgMatrix matrix_;
|
|
641
|
+
std::vector<ngn::SvgMatrix> saved_;
|
|
642
|
+
std::vector<Mask> masks_;
|
|
643
|
+
std::vector<Filter> filters_;
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
} // namespace
|
|
647
|
+
|
|
648
|
+
CGColorRef NGNCreateColor(uint32_t argb, double opacity) {
|
|
649
|
+
return createColorImpl(argb, opacity);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
CGPathRef NGNCreatePath(const ngn::SvgPath &path) {
|
|
653
|
+
return createPathImpl(path);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(
|
|
657
|
+
CGContextRef context,
|
|
658
|
+
CGSize size,
|
|
659
|
+
CGFloat scale,
|
|
660
|
+
void (^imageLoaded)(void),
|
|
661
|
+
NSView * (^viewFor)(ngn::Tag)) {
|
|
662
|
+
return std::make_unique<CGCanvas>(context, size, scale, imageLoaded, viewFor);
|
|
663
|
+
}
|
|
@@ -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);
|