@ng-native/desktop 0.0.0-stage → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +51 -2
- 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 +276 -0
- package/native/appkit/CMakeLists.txt +109 -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-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -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 +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -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 +1515 -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 +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -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/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -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 +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -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 +146 -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 +112 -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 +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -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/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 +130 -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 +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -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/svg.h +393 -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 +125 -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 +133 -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 +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -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/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 +345 -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 +405 -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 +221 -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 +371 -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 +380 -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 +422 -0
- package/native/core/src/safe-areas.cpp +130 -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 +1883 -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 +126 -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 +171 -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/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 +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
#include "text-layout.h"
|
|
2
|
+
|
|
3
|
+
#import <AppKit/AppKit.h>
|
|
4
|
+
#include <cctype>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
|
|
8
|
+
using namespace facebook::react;
|
|
9
|
+
|
|
10
|
+
namespace ngn {
|
|
11
|
+
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
std::shared_ptr<const FontLibrary> gFontLibrary;
|
|
15
|
+
|
|
16
|
+
CGFloat appKitWeight(FontWeight weight) {
|
|
17
|
+
switch (static_cast<int>(weight)) {
|
|
18
|
+
case 100: return NSFontWeightUltraLight;
|
|
19
|
+
case 200: return NSFontWeightThin;
|
|
20
|
+
case 300: return NSFontWeightLight;
|
|
21
|
+
case 500: return NSFontWeightMedium;
|
|
22
|
+
case 600: return NSFontWeightSemibold;
|
|
23
|
+
case 700: return NSFontWeightBold;
|
|
24
|
+
case 800: return NSFontWeightHeavy;
|
|
25
|
+
case 900: return NSFontWeightBlack;
|
|
26
|
+
default: return NSFontWeightRegular;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
CGColorRef cgColor(const SharedColor &color) {
|
|
31
|
+
auto c = colorComponentsFromColor(color);
|
|
32
|
+
return CGColorCreateSRGB(c.red, c.green, c.blue, c.alpha);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The font size multiplier text scales by, capped by maxFontSizeMultiplier: the font size and a
|
|
36
|
+
// fixed line height both scale by it.
|
|
37
|
+
CGFloat scaleOf(const TextAttributes &a) {
|
|
38
|
+
CGFloat multiplier = 1;
|
|
39
|
+
if (a.allowFontScaling.value_or(true) && !std::isnan(a.fontSizeMultiplier)) {
|
|
40
|
+
multiplier = a.fontSizeMultiplier;
|
|
41
|
+
if (!std::isnan(a.maxFontSizeMultiplier) && a.maxFontSizeMultiplier >= 1) {
|
|
42
|
+
multiplier = std::min<CGFloat>(multiplier, a.maxFontSizeMultiplier);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return multiplier;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
CTFontRef makeFont(const TextAttributes &a) {
|
|
49
|
+
CGFloat size = (std::isnan(a.fontSize) ? 14 : a.fontSize) * scaleOf(a);
|
|
50
|
+
CGFloat weight = appKitWeight(a.fontWeight.value_or(FontWeight::Regular));
|
|
51
|
+
bool italic = a.fontStyle.has_value() && *a.fontStyle != FontStyle::Normal;
|
|
52
|
+
NSFont *font = nil;
|
|
53
|
+
const std::string &family = a.fontFamily;
|
|
54
|
+
if (family.empty() || family == "System" || family == "system-ui" || family == "sans-serif") {
|
|
55
|
+
font = [NSFont systemFontOfSize:size weight:weight];
|
|
56
|
+
} else if (family == "monospace" || family == "Menlo") {
|
|
57
|
+
font = [NSFont monospacedSystemFontOfSize:size weight:weight];
|
|
58
|
+
} else {
|
|
59
|
+
// A name a font was loaded under stands for that font's family, as expo-font's alias does on
|
|
60
|
+
// iOS, so the weight is picked among the faces loaded in it.
|
|
61
|
+
auto loaded = gFontLibrary ? gFontLibrary->fontOf(family) : std::nullopt;
|
|
62
|
+
NSString *name = [NSString stringWithUTF8String:(loaded ? loaded->family : family).c_str()];
|
|
63
|
+
NSFontDescriptor *d = [NSFontDescriptor fontDescriptorWithFontAttributes:@{
|
|
64
|
+
NSFontFamilyAttribute : name,
|
|
65
|
+
NSFontTraitsAttribute : @{NSFontWeightTrait : @(weight)},
|
|
66
|
+
}];
|
|
67
|
+
font = [NSFont fontWithDescriptor:d size:size];
|
|
68
|
+
if (font == nil && loaded) {
|
|
69
|
+
font = [NSFont fontWithName:[NSString stringWithUTF8String:loaded->postScriptName.c_str()] size:size];
|
|
70
|
+
}
|
|
71
|
+
if (font == nil) {
|
|
72
|
+
font = [NSFont fontWithName:name size:size];
|
|
73
|
+
}
|
|
74
|
+
if (font == nil) {
|
|
75
|
+
font = [NSFont systemFontOfSize:size weight:weight];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (italic) {
|
|
79
|
+
NSFontDescriptor *d = [font.fontDescriptor fontDescriptorWithSymbolicTraits:
|
|
80
|
+
font.fontDescriptor.symbolicTraits | NSFontDescriptorTraitItalic];
|
|
81
|
+
NSFont *slanted = [NSFont fontWithDescriptor:d size:size];
|
|
82
|
+
if (slanted != nil) {
|
|
83
|
+
font = slanted;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return (CTFontRef)CFBridgingRetain(font);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
CTTextAlignment ctAlignment(std::optional<TextAlignment> alignment) {
|
|
90
|
+
switch (alignment.value_or(TextAlignment::Natural)) {
|
|
91
|
+
case TextAlignment::Left: return kCTTextAlignmentLeft;
|
|
92
|
+
case TextAlignment::Center: return kCTTextAlignmentCenter;
|
|
93
|
+
case TextAlignment::Right: return kCTTextAlignmentRight;
|
|
94
|
+
case TextAlignment::Justified: return kCTTextAlignmentJustified;
|
|
95
|
+
case TextAlignment::End: return kCTTextAlignmentRight;
|
|
96
|
+
default: return kCTTextAlignmentNatural;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
CTWritingDirection ctWritingDirection(std::optional<WritingDirection> direction) {
|
|
101
|
+
switch (direction.value_or(WritingDirection::Natural)) {
|
|
102
|
+
case WritingDirection::LeftToRight: return kCTWritingDirectionLeftToRight;
|
|
103
|
+
case WritingDirection::RightToLeft: return kCTWritingDirectionRightToLeft;
|
|
104
|
+
default: return kCTWritingDirectionNatural;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
bool paragraphEnd(UniChar c) {
|
|
109
|
+
return c == '\n' || c == '\r' || c == 0x2029;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Whether the run Core Text lays the character at `at` of `text` in runs right to left, in a
|
|
113
|
+
// paragraph whose base direction is `base`.
|
|
114
|
+
bool laidRightToLeft(NSString *text, NSUInteger at, CTWritingDirection base) {
|
|
115
|
+
CTParagraphStyleSetting setting = {kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(base), &base};
|
|
116
|
+
CTParagraphStyleRef style = CTParagraphStyleCreate(&setting, 1);
|
|
117
|
+
NSAttributedString *string =
|
|
118
|
+
[[NSAttributedString alloc] initWithString:text attributes:@{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)style}];
|
|
119
|
+
CFRelease(style);
|
|
120
|
+
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)string);
|
|
121
|
+
bool rightToLeft = false;
|
|
122
|
+
for (id run in (__bridge NSArray *)CTLineGetGlyphRuns(line)) {
|
|
123
|
+
CFRange range = CTRunGetStringRange((__bridge CTRunRef)run);
|
|
124
|
+
if (static_cast<NSUInteger>(range.location) <= at && at < static_cast<NSUInteger>(range.location + range.length)) {
|
|
125
|
+
rightToLeft = (CTRunGetStatus((__bridge CTRunRef)run) & kCTRunStatusRightToLeft) != 0;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
CFRelease(line);
|
|
130
|
+
return rightToLeft;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The strong direction a character gives a paragraph under Unicode's rule P2: 1 right to left
|
|
134
|
+
// (bidi class R or AL), -1 left to right (L), 0 none. macOS has no public API for a character's bidi
|
|
135
|
+
// class, so Core Text's own bidi resolution answers it. Alone in a left-to-right paragraph, only R
|
|
136
|
+
// and AL lay out right to left. In a right-to-left paragraph, a space between the character and a
|
|
137
|
+
// Latin letter lays out left to right only when the character is L: rule N1 takes the numbers (EN,
|
|
138
|
+
// AN) as right to left, and a neutral or a mark takes the paragraph's direction.
|
|
139
|
+
int probedDirection(UTF32Char code) {
|
|
140
|
+
UTF32Char little = NSSwapHostIntToLittle(code);
|
|
141
|
+
NSString *character = [[NSString alloc] initWithBytes:&little length:4 encoding:NSUTF32LittleEndianStringEncoding];
|
|
142
|
+
if (character == nil) {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
if (laidRightToLeft(character, 0, kCTWritingDirectionLeftToRight)) {
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
NSString *probe = [character stringByAppendingString:@" a"];
|
|
149
|
+
return laidRightToLeft(probe, character.length, kCTWritingDirectionRightToLeft) ? 0 : -1;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
int strongDirection(UTF32Char code) {
|
|
153
|
+
// ASCII's only strong characters are its letters, all L.
|
|
154
|
+
if (code < 0x80) {
|
|
155
|
+
return isalpha(static_cast<int>(code)) ? -1 : 0;
|
|
156
|
+
}
|
|
157
|
+
// The marks have no glyph for a run to hold: RLM and ALM are R and AL, LRM is L.
|
|
158
|
+
if (code == 0x200F || code == 0x061C) {
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
if (code == 0x200E) {
|
|
162
|
+
return -1;
|
|
163
|
+
}
|
|
164
|
+
// The embedding and override controls (LRE, RLE, PDF, LRO, RLO) are not strong (rule P2), though
|
|
165
|
+
// the probe reads some of them as left to right.
|
|
166
|
+
if (code >= 0x202A && code <= 0x202E) {
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
// Each code point probed once a thread: layout runs on more than one.
|
|
170
|
+
thread_local std::unordered_map<UTF32Char, int> known;
|
|
171
|
+
auto found = known.find(code);
|
|
172
|
+
if (found != known.end()) {
|
|
173
|
+
return found->second;
|
|
174
|
+
}
|
|
175
|
+
int direction = probedDirection(code);
|
|
176
|
+
known.emplace(code, direction);
|
|
177
|
+
return direction;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// The paragraph whose direction was resolved last, so the lines it wraps into find it without
|
|
181
|
+
// another scan: each paragraph is scanned once however many lines it takes.
|
|
182
|
+
struct ParagraphDirection {
|
|
183
|
+
CFIndex start = 0;
|
|
184
|
+
CFIndex end = -1;
|
|
185
|
+
bool rightToLeft = false;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Whether the paragraph holding `at` runs right to left: by its base writing direction where one
|
|
189
|
+
// is set, otherwise by its first strong character outside an isolate (Unicode's rules P2 and P3).
|
|
190
|
+
bool rightToLeftAt(CFAttributedStringRef string, CFIndex at, ParagraphDirection &last) {
|
|
191
|
+
auto style = (CTParagraphStyleRef)CFAttributedStringGetAttribute(string, at, kCTParagraphStyleAttributeName, nullptr);
|
|
192
|
+
CTWritingDirection direction = kCTWritingDirectionNatural;
|
|
193
|
+
if (style != nullptr) {
|
|
194
|
+
CTParagraphStyleGetValueForSpecifier(style, kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(direction), &direction);
|
|
195
|
+
}
|
|
196
|
+
if (direction != kCTWritingDirectionNatural) {
|
|
197
|
+
return direction == kCTWritingDirectionRightToLeft;
|
|
198
|
+
}
|
|
199
|
+
if (at >= last.start && at < last.end) {
|
|
200
|
+
return last.rightToLeft;
|
|
201
|
+
}
|
|
202
|
+
CFStringRef text = CFAttributedStringGetString(string);
|
|
203
|
+
CFIndex length = CFStringGetLength(text);
|
|
204
|
+
CFIndex start = at;
|
|
205
|
+
while (start > 0 && !paragraphEnd(CFStringGetCharacterAtIndex(text, start - 1))) {
|
|
206
|
+
start--;
|
|
207
|
+
}
|
|
208
|
+
int found = 0;
|
|
209
|
+
// How deep in isolates (LRI, RLI or FSI to the matching PDI) the scan is: their content is skipped.
|
|
210
|
+
int isolates = 0;
|
|
211
|
+
CFIndex i = start;
|
|
212
|
+
for (; i < length; i++) {
|
|
213
|
+
UniChar c = CFStringGetCharacterAtIndex(text, i);
|
|
214
|
+
if (paragraphEnd(c)) {
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
if (found != 0) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
UTF32Char code = c;
|
|
221
|
+
if (CFStringIsSurrogateHighCharacter(c) && i + 1 < length) {
|
|
222
|
+
code = CFStringGetLongCharacterForSurrogatePair(c, CFStringGetCharacterAtIndex(text, ++i));
|
|
223
|
+
}
|
|
224
|
+
if (code >= 0x2066 && code <= 0x2068) {
|
|
225
|
+
isolates++;
|
|
226
|
+
} else if (code == 0x2069) {
|
|
227
|
+
isolates = std::max(0, isolates - 1);
|
|
228
|
+
} else if (isolates == 0) {
|
|
229
|
+
found = strongDirection(code);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
last = {.start = start, .end = i + 1, .rightToLeft = found == 1};
|
|
233
|
+
return last.rightToLeft;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
std::string transformed(const std::string &text, std::optional<TextTransform> transform) {
|
|
237
|
+
if (!transform || *transform == TextTransform::None) {
|
|
238
|
+
return text;
|
|
239
|
+
}
|
|
240
|
+
NSString *s = [NSString stringWithUTF8String:text.c_str()];
|
|
241
|
+
switch (*transform) {
|
|
242
|
+
case TextTransform::Uppercase: s = s.uppercaseString; break;
|
|
243
|
+
case TextTransform::Lowercase: s = s.lowercaseString; break;
|
|
244
|
+
case TextTransform::Capitalize: s = s.capitalizedString; break;
|
|
245
|
+
default: break;
|
|
246
|
+
}
|
|
247
|
+
return s.UTF8String;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
} // namespace
|
|
251
|
+
|
|
252
|
+
TextLayout::TextLayout(TextLayout &&other) noexcept
|
|
253
|
+
: string(other.string), lines(std::move(other.lines)), size(other.size) {
|
|
254
|
+
other.string = nullptr;
|
|
255
|
+
other.lines.clear();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
TextLayout::~TextLayout() {
|
|
259
|
+
for (auto &l : lines) {
|
|
260
|
+
CFRelease(l.line);
|
|
261
|
+
}
|
|
262
|
+
if (string != nullptr) {
|
|
263
|
+
CFRelease(string);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
void useFontLibrary(std::shared_ptr<const FontLibrary> library) {
|
|
268
|
+
gFontLibrary = std::move(library);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
CFAttributedStringRef makeAttributedString(const AttributedString &attributedString) {
|
|
272
|
+
CFMutableAttributedStringRef out = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
|
|
273
|
+
CFAttributedStringBeginEditing(out);
|
|
274
|
+
for (const auto &fragment : attributedString.getFragments()) {
|
|
275
|
+
if (fragment.isAttachment()) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
const auto &a = fragment.textAttributes;
|
|
279
|
+
std::string text = transformed(fragment.string, a.textTransform);
|
|
280
|
+
CFStringRef cf = CFStringCreateWithCString(kCFAllocatorDefault, text.c_str(), kCFStringEncodingUTF8);
|
|
281
|
+
if (cf == nullptr) {
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
CFIndex start = CFAttributedStringGetLength(out);
|
|
285
|
+
CFAttributedStringReplaceString(out, CFRangeMake(start, 0), cf);
|
|
286
|
+
CFRange range = CFRangeMake(start, CFStringGetLength(cf));
|
|
287
|
+
CFRelease(cf);
|
|
288
|
+
// Text appended takes the attributes before it; each fragment starts from none.
|
|
289
|
+
CFDictionaryRef none = CFDictionaryCreate(kCFAllocatorDefault, nullptr, nullptr, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
290
|
+
&kCFTypeDictionaryValueCallBacks);
|
|
291
|
+
CFAttributedStringSetAttributes(out, range, none, true);
|
|
292
|
+
CFRelease(none);
|
|
293
|
+
|
|
294
|
+
CTFontRef font = makeFont(a);
|
|
295
|
+
CFAttributedStringSetAttribute(out, range, kCTFontAttributeName, font);
|
|
296
|
+
|
|
297
|
+
CGColorRef color = a.foregroundColor ? cgColor(a.foregroundColor) : CGColorCreateSRGB(0, 0, 0, 1);
|
|
298
|
+
if (!std::isnan(a.opacity)) {
|
|
299
|
+
CGColorRef faded = CGColorCreateCopyWithAlpha(color, CGColorGetAlpha(color) * a.opacity);
|
|
300
|
+
CGColorRelease(color);
|
|
301
|
+
color = faded;
|
|
302
|
+
}
|
|
303
|
+
CFAttributedStringSetAttribute(out, range, kCTForegroundColorAttributeName, color);
|
|
304
|
+
CGColorRelease(color);
|
|
305
|
+
|
|
306
|
+
if (!std::isnan(a.letterSpacing)) {
|
|
307
|
+
CGFloat kern = a.letterSpacing;
|
|
308
|
+
CFNumberRef n = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &kern);
|
|
309
|
+
CFAttributedStringSetAttribute(out, range, kCTKernAttributeName, n);
|
|
310
|
+
CFRelease(n);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Paragraph style: alignment, and lineHeight as a fixed line box (as React Native iOS does),
|
|
314
|
+
// with the glyphs centred in it through a baseline offset applied at draw time.
|
|
315
|
+
CTTextAlignment alignment = ctAlignment(a.alignment);
|
|
316
|
+
CTWritingDirection direction = ctWritingDirection(a.baseWritingDirection);
|
|
317
|
+
CGFloat lineHeight = 0;
|
|
318
|
+
if (!std::isnan(a.lineHeight) && a.lineHeight > 0) {
|
|
319
|
+
lineHeight = a.lineHeight * scaleOf(a);
|
|
320
|
+
}
|
|
321
|
+
CTParagraphStyleSetting settings[] = {
|
|
322
|
+
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
|
|
323
|
+
{kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(direction), &direction},
|
|
324
|
+
{kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(lineHeight), &lineHeight},
|
|
325
|
+
{kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(lineHeight), &lineHeight},
|
|
326
|
+
};
|
|
327
|
+
CTParagraphStyleRef style = CTParagraphStyleCreate(settings, lineHeight > 0 ? 4 : 2);
|
|
328
|
+
CFAttributedStringSetAttribute(out, range, kCTParagraphStyleAttributeName, style);
|
|
329
|
+
CFRelease(style);
|
|
330
|
+
|
|
331
|
+
if (a.textDecorationLineType == TextDecorationLineType::Underline ||
|
|
332
|
+
a.textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) {
|
|
333
|
+
int32_t single = kCTUnderlineStyleSingle;
|
|
334
|
+
CFNumberRef n = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &single);
|
|
335
|
+
CFAttributedStringSetAttribute(out, range, kCTUnderlineStyleAttributeName, n);
|
|
336
|
+
CFRelease(n);
|
|
337
|
+
}
|
|
338
|
+
CFRelease(font);
|
|
339
|
+
}
|
|
340
|
+
CFAttributedStringEndEditing(out);
|
|
341
|
+
return out;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
TextLayout layoutText(
|
|
345
|
+
const AttributedString &attributedString,
|
|
346
|
+
const ParagraphAttributes ¶graphAttributes,
|
|
347
|
+
CGFloat maxWidth) {
|
|
348
|
+
TextLayout layout;
|
|
349
|
+
layout.string = makeAttributedString(attributedString);
|
|
350
|
+
CFIndex length = CFAttributedStringGetLength(layout.string);
|
|
351
|
+
if (length == 0) {
|
|
352
|
+
return layout;
|
|
353
|
+
}
|
|
354
|
+
// Only an infinite width is unbounded: 0 is a width too, which breaks at every character.
|
|
355
|
+
bool bounded = std::isfinite(maxWidth);
|
|
356
|
+
maxWidth = bounded ? std::max<CGFloat>(0, maxWidth) : CGFLOAT_MAX / 4;
|
|
357
|
+
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString(layout.string);
|
|
358
|
+
int maxLines = paragraphAttributes.maximumNumberOfLines;
|
|
359
|
+
CFIndex at = 0;
|
|
360
|
+
CGFloat y = 0;
|
|
361
|
+
CGFloat widest = 0;
|
|
362
|
+
ParagraphDirection paragraph;
|
|
363
|
+
while (at < length) {
|
|
364
|
+
CFIndex count = CTTypesetterSuggestLineBreak(typesetter, at, maxWidth);
|
|
365
|
+
if (count <= 0) {
|
|
366
|
+
count = 1;
|
|
367
|
+
}
|
|
368
|
+
bool last = maxLines > 0 && (int)layout.lines.size() == maxLines - 1 && at + count < length;
|
|
369
|
+
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(at, count));
|
|
370
|
+
if (last && paragraphAttributes.ellipsizeMode != EllipsizeMode::Clip) {
|
|
371
|
+
// The rest of the text on one line, truncated to fit.
|
|
372
|
+
CTLineRef rest = CTTypesetterCreateLine(typesetter, CFRangeMake(at, length - at));
|
|
373
|
+
CFDictionaryRef attrs = CFAttributedStringGetAttributes(layout.string, at, nullptr);
|
|
374
|
+
CFAttributedStringRef dots = CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("…"), attrs);
|
|
375
|
+
CTLineRef token = CTLineCreateWithAttributedString(dots);
|
|
376
|
+
CTLineTruncationType type = paragraphAttributes.ellipsizeMode == EllipsizeMode::Head ? kCTLineTruncationStart
|
|
377
|
+
: paragraphAttributes.ellipsizeMode == EllipsizeMode::Middle ? kCTLineTruncationMiddle
|
|
378
|
+
: kCTLineTruncationEnd;
|
|
379
|
+
CTLineRef truncated = CTLineCreateTruncatedLine(rest, maxWidth, type, token);
|
|
380
|
+
CFRelease(token);
|
|
381
|
+
CFRelease(dots);
|
|
382
|
+
CFRelease(rest);
|
|
383
|
+
if (truncated != nullptr) {
|
|
384
|
+
CFRelease(line);
|
|
385
|
+
line = truncated;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
// A justified paragraph's lines fill the width, all but its last; Core Text justifies only
|
|
389
|
+
// through a framesetter, and these lines are made one by one.
|
|
390
|
+
CFRange made = CTLineGetStringRange(line);
|
|
391
|
+
CFStringRef text = CFAttributedStringGetString(layout.string);
|
|
392
|
+
CFIndex end = made.location + made.length;
|
|
393
|
+
bool paragraphLast = end >= length || paragraphEnd(CFStringGetCharacterAtIndex(text, end - 1));
|
|
394
|
+
CTParagraphStyleRef lineStyle = (CTParagraphStyleRef)CFAttributedStringGetAttribute(
|
|
395
|
+
layout.string, made.location, kCTParagraphStyleAttributeName, nullptr);
|
|
396
|
+
CTTextAlignment lineAlignment = kCTTextAlignmentNatural;
|
|
397
|
+
if (lineStyle != nullptr) {
|
|
398
|
+
CTParagraphStyleGetValueForSpecifier(lineStyle, kCTParagraphStyleSpecifierAlignment, sizeof(lineAlignment), &lineAlignment);
|
|
399
|
+
}
|
|
400
|
+
if (lineAlignment == kCTTextAlignmentJustified && bounded && !last && !paragraphLast) {
|
|
401
|
+
if (CTLineRef justified = CTLineCreateJustifiedLine(line, 1.0, maxWidth)) {
|
|
402
|
+
CFRelease(line);
|
|
403
|
+
line = justified;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
CGFloat ascent = 0, descent = 0, leading = 0;
|
|
407
|
+
CGFloat width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
|
|
408
|
+
width -= CTLineGetTrailingWhitespaceWidth(line);
|
|
409
|
+
// Fixed line height from the paragraph style, if any (min == max).
|
|
410
|
+
CGFloat fixed = 0;
|
|
411
|
+
CFRange r = CTLineGetStringRange(line);
|
|
412
|
+
CTParagraphStyleRef style = (CTParagraphStyleRef)CFAttributedStringGetAttribute(
|
|
413
|
+
layout.string, r.location, kCTParagraphStyleAttributeName, nullptr);
|
|
414
|
+
if (style != nullptr) {
|
|
415
|
+
CTParagraphStyleGetValueForSpecifier(style, kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(fixed), &fixed);
|
|
416
|
+
}
|
|
417
|
+
CGFloat natural = std::ceil(ascent + descent + leading);
|
|
418
|
+
CGFloat height = fixed > 0 ? fixed : natural;
|
|
419
|
+
// Glyphs centred in a fixed line box, as React Native iOS centres them.
|
|
420
|
+
CGFloat baseline = y + (height - (ascent + descent)) / 2 + ascent;
|
|
421
|
+
if (fixed <= 0) {
|
|
422
|
+
baseline = y + ascent;
|
|
423
|
+
}
|
|
424
|
+
layout.lines.push_back({line, CGPointMake(0, baseline), width, rightToLeftAt(layout.string, r.location, paragraph)});
|
|
425
|
+
widest = std::max(widest, width);
|
|
426
|
+
y += height;
|
|
427
|
+
at += count;
|
|
428
|
+
if (maxLines > 0 && (int)layout.lines.size() >= maxLines) {
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
CFRelease(typesetter);
|
|
433
|
+
layout.size = CGSizeMake(std::min(widest, maxWidth), y);
|
|
434
|
+
return layout;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
void drawText(const TextLayout &layout, CGContextRef context, CGPoint origin, CGFloat boxWidth) {
|
|
438
|
+
CGContextSaveGState(context);
|
|
439
|
+
// The view is flipped; glyphs are drawn with an inverted text matrix rather than by flipping
|
|
440
|
+
// the whole context, so line origins stay top-down.
|
|
441
|
+
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1));
|
|
442
|
+
for (const auto &l : layout.lines) {
|
|
443
|
+
CFRange r = CTLineGetStringRange(l.line);
|
|
444
|
+
CTTextAlignment alignment = kCTTextAlignmentNatural;
|
|
445
|
+
CTParagraphStyleRef style = r.length > 0
|
|
446
|
+
? (CTParagraphStyleRef)CFAttributedStringGetAttribute(
|
|
447
|
+
layout.string, r.location, kCTParagraphStyleAttributeName, nullptr)
|
|
448
|
+
: nullptr;
|
|
449
|
+
if (style != nullptr) {
|
|
450
|
+
CTParagraphStyleGetValueForSpecifier(style, kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment);
|
|
451
|
+
}
|
|
452
|
+
CGFloat x = 0;
|
|
453
|
+
if (alignment == kCTTextAlignmentCenter) {
|
|
454
|
+
x = (boxWidth - l.width) / 2;
|
|
455
|
+
} else if (alignment == kCTTextAlignmentRight) {
|
|
456
|
+
x = boxWidth - l.width;
|
|
457
|
+
} else if ((alignment == kCTTextAlignmentNatural || alignment == kCTTextAlignmentJustified) && l.rightToLeft) {
|
|
458
|
+
// Natural follows the paragraph's direction, as does a justified paragraph's last line.
|
|
459
|
+
x = boxWidth - l.width;
|
|
460
|
+
}
|
|
461
|
+
CGContextSetTextPosition(context, origin.x + x, origin.y + l.origin.y);
|
|
462
|
+
CTLineDraw(l.line, context);
|
|
463
|
+
}
|
|
464
|
+
CGContextRestoreGState(context);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
facebook::react::Size CoreTextMeasurer::measure(
|
|
468
|
+
const AttributedString &text,
|
|
469
|
+
const ParagraphAttributes ¶graph,
|
|
470
|
+
facebook::react::Float maxWidth) const {
|
|
471
|
+
@autoreleasepool {
|
|
472
|
+
auto layout = layoutText(text, paragraph, maxWidth);
|
|
473
|
+
return {static_cast<facebook::react::Float>(layout.size.width), static_cast<facebook::react::Float>(layout.size.height)};
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
} // namespace ngn
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The OS half of NgNativeTextRecognition (the core's text-recognition.h): Vision, on the image
|
|
2
|
+
// ImageIO decodes, with the orientation the file records.
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include "ngn/text-recognition.h"
|
|
6
|
+
|
|
7
|
+
#include <memory>
|
|
8
|
+
|
|
9
|
+
namespace ngn::appkit {
|
|
10
|
+
|
|
11
|
+
std::shared_ptr<TextReader> makeTextReader();
|
|
12
|
+
|
|
13
|
+
} // namespace ngn::appkit
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#include "text-recognition.h"
|
|
2
|
+
|
|
3
|
+
#import <Foundation/Foundation.h>
|
|
4
|
+
#import <ImageIO/ImageIO.h>
|
|
5
|
+
#import <Vision/Vision.h>
|
|
6
|
+
|
|
7
|
+
namespace ngn::appkit {
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
std::vector<std::string> stringsOf(NSArray<NSString *> *values) {
|
|
12
|
+
std::vector<std::string> out;
|
|
13
|
+
for (NSString *value in values) {
|
|
14
|
+
out.emplace_back(value.UTF8String);
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class VisionTextReader final : public TextReader {
|
|
20
|
+
public:
|
|
21
|
+
std::vector<std::string> languages(bool fast) override {
|
|
22
|
+
VNRecognizeTextRequest *request = [[VNRecognizeTextRequest alloc] init];
|
|
23
|
+
request.recognitionLevel = fast ? VNRequestTextRecognitionLevelFast : VNRequestTextRecognitionLevelAccurate;
|
|
24
|
+
return stringsOf([request supportedRecognitionLanguagesAndReturnError:nil]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
void read(
|
|
28
|
+
const std::string &path,
|
|
29
|
+
const std::vector<std::string> &languages,
|
|
30
|
+
bool fast,
|
|
31
|
+
std::function<void(std::optional<Reading>, std::string)> done) override {
|
|
32
|
+
NSString *file = [NSString stringWithUTF8String:path.c_str()];
|
|
33
|
+
NSMutableArray<NSString *> *codes = [NSMutableArray array];
|
|
34
|
+
for (const auto &language : languages) {
|
|
35
|
+
[codes addObject:[NSString stringWithUTF8String:language.c_str()]];
|
|
36
|
+
}
|
|
37
|
+
auto answer = std::make_shared<std::function<void(std::optional<Reading>, std::string)>>(std::move(done));
|
|
38
|
+
dispatch_async(queue_, ^{
|
|
39
|
+
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:file], nullptr);
|
|
40
|
+
CGImageRef image = source != nullptr ? CGImageSourceCreateImageAtIndex(source, 0, nullptr) : nullptr;
|
|
41
|
+
if (image == nullptr) {
|
|
42
|
+
if (source != nullptr) {
|
|
43
|
+
CFRelease(source);
|
|
44
|
+
}
|
|
45
|
+
(*answer)(std::nullopt, std::string("No image could be read at ") + file.UTF8String);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
NSDictionary *properties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, nullptr));
|
|
49
|
+
CFRelease(source);
|
|
50
|
+
uint32_t raw = [properties[(__bridge NSString *)kCGImagePropertyOrientation] unsignedIntValue];
|
|
51
|
+
auto orientation = static_cast<CGImagePropertyOrientation>(raw >= 1 && raw <= 8 ? raw : 1);
|
|
52
|
+
bool quarterTurn = orientation >= kCGImagePropertyOrientationLeftMirrored;
|
|
53
|
+
Reading reading{
|
|
54
|
+
.width = static_cast<double>(quarterTurn ? CGImageGetHeight(image) : CGImageGetWidth(image)),
|
|
55
|
+
.height = static_cast<double>(quarterTurn ? CGImageGetWidth(image) : CGImageGetHeight(image)),
|
|
56
|
+
};
|
|
57
|
+
VNRecognizeTextRequest *request = [[VNRecognizeTextRequest alloc] init];
|
|
58
|
+
request.recognitionLevel = fast ? VNRequestTextRecognitionLevelFast : VNRequestTextRecognitionLevelAccurate;
|
|
59
|
+
if (codes.count > 0) {
|
|
60
|
+
request.recognitionLanguages = codes;
|
|
61
|
+
} else if (@available(macOS 13.0, *)) {
|
|
62
|
+
request.automaticallyDetectsLanguage = YES;
|
|
63
|
+
}
|
|
64
|
+
NSError *error = nil;
|
|
65
|
+
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCGImage:image orientation:orientation options:@{}];
|
|
66
|
+
BOOL performed = [handler performRequests:@[ request ] error:&error];
|
|
67
|
+
CGImageRelease(image);
|
|
68
|
+
if (!performed) {
|
|
69
|
+
(*answer)(std::nullopt, error.localizedDescription.UTF8String);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
for (VNRecognizedTextObservation *observation in request.results) {
|
|
73
|
+
VNRecognizedText *candidate = [observation topCandidates:1].firstObject;
|
|
74
|
+
if (candidate == nil) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
CGRect box = observation.boundingBox;
|
|
78
|
+
reading.lines.push_back({
|
|
79
|
+
.text = candidate.string.UTF8String,
|
|
80
|
+
.confidence = candidate.confidence,
|
|
81
|
+
.x = box.origin.x,
|
|
82
|
+
.y = box.origin.y,
|
|
83
|
+
.width = box.size.width,
|
|
84
|
+
.height = box.size.height,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
(*answer)(std::move(reading), "");
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private:
|
|
92
|
+
// Off the JavaScript thread: decoding and reading an image takes a while.
|
|
93
|
+
dispatch_queue_t queue_ = dispatch_queue_create("ng-native.text-recognition", DISPATCH_QUEUE_SERIAL);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
} // namespace
|
|
97
|
+
|
|
98
|
+
std::shared_ptr<TextReader> makeTextReader() {
|
|
99
|
+
return std::make_shared<VisionTextReader>();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
} // namespace ngn::appkit
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The OS half of NgNativeTrackingTransparency (the core's tracking.h): ATTrackingManager and
|
|
2
|
+
// ASIdentifierManager. It asks only when the app's Info.plist has NSUserTrackingUsageDescription.
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include "ngn/tracking.h"
|
|
6
|
+
|
|
7
|
+
#include <memory>
|
|
8
|
+
|
|
9
|
+
namespace ngn::appkit {
|
|
10
|
+
|
|
11
|
+
std::shared_ptr<TrackingAuthority> makeTrackingAuthority();
|
|
12
|
+
|
|
13
|
+
} // namespace ngn::appkit
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#include "tracking.h"
|
|
2
|
+
|
|
3
|
+
#import <AdSupport/AdSupport.h>
|
|
4
|
+
#import <AppTrackingTransparency/AppTrackingTransparency.h>
|
|
5
|
+
#import <Foundation/Foundation.h>
|
|
6
|
+
|
|
7
|
+
namespace ngn::appkit {
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
using Access = TrackingAuthority::Access;
|
|
12
|
+
|
|
13
|
+
Access accessOf(ATTrackingManagerAuthorizationStatus status) {
|
|
14
|
+
switch (status) {
|
|
15
|
+
case ATTrackingManagerAuthorizationStatusAuthorized: return Access::Granted;
|
|
16
|
+
case ATTrackingManagerAuthorizationStatusDenied: return Access::Denied;
|
|
17
|
+
case ATTrackingManagerAuthorizationStatusRestricted: return Access::Restricted;
|
|
18
|
+
default: return Access::NotDetermined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class AppTracking final : public TrackingAuthority {
|
|
23
|
+
public:
|
|
24
|
+
Access access() override {
|
|
25
|
+
return accessOf(ATTrackingManager.trackingAuthorizationStatus);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void requestAccess(std::function<void(Access)> done) override {
|
|
29
|
+
id reason = NSBundle.mainBundle.infoDictionary[@"NSUserTrackingUsageDescription"];
|
|
30
|
+
if (![reason isKindOfClass:NSString.class] || [reason length] == 0 || access() != Access::NotDetermined) {
|
|
31
|
+
done(access());
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
auto answer = std::make_shared<std::function<void(Access)>>(std::move(done));
|
|
35
|
+
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
|
|
36
|
+
(*answer)(accessOf(status));
|
|
37
|
+
}];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
std::string advertisingId() override {
|
|
41
|
+
return ASIdentifierManager.sharedManager.advertisingIdentifier.UUIDString.UTF8String;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
} // namespace
|
|
46
|
+
|
|
47
|
+
std::shared_ptr<TrackingAuthority> makeTrackingAuthority() {
|
|
48
|
+
return std::make_shared<AppTracking>();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
} // namespace ngn::appkit
|