@shopify/react-native-skia 1.12.1 → 1.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/apple/MetalContext.h +129 -0
  2. package/apple/MetalContext.mm +34 -0
  3. package/apple/MetalWindowContext.h +39 -0
  4. package/apple/MetalWindowContext.mm +64 -0
  5. package/apple/RNSkApplePlatformContext.h +79 -0
  6. package/apple/RNSkApplePlatformContext.mm +303 -0
  7. package/apple/RNSkAppleVideo.h +51 -0
  8. package/apple/RNSkAppleVideo.mm +137 -0
  9. package/apple/RNSkAppleView.h +37 -0
  10. package/apple/RNSkAppleView.mm +35 -0
  11. package/apple/RNSkMetalCanvasProvider.h +38 -0
  12. package/apple/RNSkMetalCanvasProvider.mm +108 -0
  13. package/apple/RNSkiaModule.h +20 -0
  14. package/apple/RNSkiaModule.mm +55 -0
  15. package/apple/SkiaCVPixelBufferUtils.h +119 -0
  16. package/apple/SkiaCVPixelBufferUtils.mm +344 -0
  17. package/apple/SkiaManager.h +25 -0
  18. package/apple/SkiaManager.mm +62 -0
  19. package/apple/SkiaPictureView.h +7 -0
  20. package/apple/SkiaPictureView.mm +66 -0
  21. package/apple/SkiaPictureViewManager.h +8 -0
  22. package/apple/SkiaPictureViewManager.mm +55 -0
  23. package/apple/SkiaUIView.h +45 -0
  24. package/apple/SkiaUIView.mm +172 -0
  25. package/apple/ViewScreenshotService.h +25 -0
  26. package/apple/ViewScreenshotService.mm +89 -0
  27. package/cpp/api/JsiSkShaderFactory.h +43 -8
  28. package/cpp/api/JsiSkVertices.h +14 -3
  29. package/lib/commonjs/__tests__/setup.d.ts +18 -0
  30. package/lib/commonjs/headless/index.d.ts +1 -0
  31. package/lib/commonjs/headless/index.js +12 -0
  32. package/lib/commonjs/headless/index.js.map +1 -1
  33. package/lib/commonjs/skia/web/Host.d.ts +3 -3
  34. package/lib/commonjs/skia/web/Host.js +16 -4
  35. package/lib/commonjs/skia/web/Host.js.map +1 -1
  36. package/lib/commonjs/skia/web/JsiSkCanvas.js +15 -15
  37. package/lib/commonjs/skia/web/JsiSkCanvas.js.map +1 -1
  38. package/lib/commonjs/skia/web/JsiSkColorFilterFactory.js +1 -1
  39. package/lib/commonjs/skia/web/JsiSkColorFilterFactory.js.map +1 -1
  40. package/lib/commonjs/skia/web/JsiSkFont.js +2 -2
  41. package/lib/commonjs/skia/web/JsiSkFont.js.map +1 -1
  42. package/lib/commonjs/skia/web/JsiSkImage.js +6 -6
  43. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  44. package/lib/commonjs/skia/web/JsiSkImageFactory.js +2 -2
  45. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  46. package/lib/commonjs/skia/web/JsiSkImageFilterFactory.js +3 -3
  47. package/lib/commonjs/skia/web/JsiSkImageFilterFactory.js.map +1 -1
  48. package/lib/commonjs/skia/web/JsiSkMaskFilterFactory.js +1 -1
  49. package/lib/commonjs/skia/web/JsiSkMaskFilterFactory.js.map +1 -1
  50. package/lib/commonjs/skia/web/JsiSkPaint.js +3 -3
  51. package/lib/commonjs/skia/web/JsiSkPaint.js.map +1 -1
  52. package/lib/commonjs/skia/web/JsiSkPath.js +4 -4
  53. package/lib/commonjs/skia/web/JsiSkPath.js.map +1 -1
  54. package/lib/commonjs/skia/web/JsiSkPathEffectFactory.js +1 -1
  55. package/lib/commonjs/skia/web/JsiSkPathEffectFactory.js.map +1 -1
  56. package/lib/commonjs/skia/web/JsiSkPathFactory.js +1 -1
  57. package/lib/commonjs/skia/web/JsiSkPathFactory.js.map +1 -1
  58. package/lib/commonjs/skia/web/JsiSkPicture.js +1 -1
  59. package/lib/commonjs/skia/web/JsiSkPicture.js.map +1 -1
  60. package/lib/commonjs/skia/web/JsiSkShaderFactory.js +5 -5
  61. package/lib/commonjs/skia/web/JsiSkShaderFactory.js.map +1 -1
  62. package/lib/commonjs/skia/web/JsiSkVerticesFactory.js +1 -1
  63. package/lib/commonjs/skia/web/JsiSkVerticesFactory.js.map +1 -1
  64. package/lib/module/__tests__/setup.d.ts +18 -0
  65. package/lib/module/headless/index.d.ts +1 -0
  66. package/lib/module/headless/index.js +1 -0
  67. package/lib/module/headless/index.js.map +1 -1
  68. package/lib/module/skia/web/Host.d.ts +3 -3
  69. package/lib/module/skia/web/Host.js +16 -4
  70. package/lib/module/skia/web/Host.js.map +1 -1
  71. package/lib/module/skia/web/JsiSkCanvas.js +15 -15
  72. package/lib/module/skia/web/JsiSkCanvas.js.map +1 -1
  73. package/lib/module/skia/web/JsiSkColorFilterFactory.js +1 -1
  74. package/lib/module/skia/web/JsiSkColorFilterFactory.js.map +1 -1
  75. package/lib/module/skia/web/JsiSkFont.js +2 -2
  76. package/lib/module/skia/web/JsiSkFont.js.map +1 -1
  77. package/lib/module/skia/web/JsiSkImage.js +6 -6
  78. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  79. package/lib/module/skia/web/JsiSkImageFactory.js +2 -2
  80. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  81. package/lib/module/skia/web/JsiSkImageFilterFactory.js +3 -3
  82. package/lib/module/skia/web/JsiSkImageFilterFactory.js.map +1 -1
  83. package/lib/module/skia/web/JsiSkMaskFilterFactory.js +1 -1
  84. package/lib/module/skia/web/JsiSkMaskFilterFactory.js.map +1 -1
  85. package/lib/module/skia/web/JsiSkPaint.js +3 -3
  86. package/lib/module/skia/web/JsiSkPaint.js.map +1 -1
  87. package/lib/module/skia/web/JsiSkPath.js +4 -4
  88. package/lib/module/skia/web/JsiSkPath.js.map +1 -1
  89. package/lib/module/skia/web/JsiSkPathEffectFactory.js +1 -1
  90. package/lib/module/skia/web/JsiSkPathEffectFactory.js.map +1 -1
  91. package/lib/module/skia/web/JsiSkPathFactory.js +1 -1
  92. package/lib/module/skia/web/JsiSkPathFactory.js.map +1 -1
  93. package/lib/module/skia/web/JsiSkPicture.js +1 -1
  94. package/lib/module/skia/web/JsiSkPicture.js.map +1 -1
  95. package/lib/module/skia/web/JsiSkShaderFactory.js +5 -5
  96. package/lib/module/skia/web/JsiSkShaderFactory.js.map +1 -1
  97. package/lib/module/skia/web/JsiSkVerticesFactory.js +1 -1
  98. package/lib/module/skia/web/JsiSkVerticesFactory.js.map +1 -1
  99. package/lib/typescript/lib/commonjs/mock/index.d.ts +1 -1
  100. package/lib/typescript/lib/commonjs/skia/index.d.ts +1 -1
  101. package/lib/typescript/lib/commonjs/skia/web/Host.d.ts +2 -2
  102. package/lib/typescript/lib/module/headless/index.d.ts +1 -0
  103. package/lib/typescript/lib/module/mock/index.d.ts +1 -1
  104. package/lib/typescript/lib/module/skia/Skia.d.ts +1 -1
  105. package/lib/typescript/lib/module/skia/web/Host.d.ts +2 -2
  106. package/lib/typescript/src/__tests__/setup.d.ts +18 -0
  107. package/lib/typescript/src/headless/index.d.ts +1 -0
  108. package/lib/typescript/src/skia/web/Host.d.ts +3 -3
  109. package/package.json +4 -4
  110. package/src/__tests__/setup.ts +67 -1
  111. package/src/headless/index.ts +2 -1
  112. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +1 -2
  113. package/src/renderer/__tests__/e2e/Paths.spec.tsx +13 -11
  114. package/src/skia/__tests__/Enums.spec.ts +24 -2
  115. package/src/skia/__tests__/Path.spec.ts +2 -2
  116. package/src/skia/web/Host.ts +24 -7
  117. package/src/skia/web/JsiSkCanvas.ts +17 -17
  118. package/src/skia/web/JsiSkColorFilterFactory.ts +1 -1
  119. package/src/skia/web/JsiSkFont.ts +2 -2
  120. package/src/skia/web/JsiSkImage.ts +14 -10
  121. package/src/skia/web/JsiSkImageFactory.ts +2 -2
  122. package/src/skia/web/JsiSkImageFilterFactory.ts +4 -4
  123. package/src/skia/web/JsiSkMaskFilterFactory.ts +1 -1
  124. package/src/skia/web/JsiSkPaint.ts +3 -3
  125. package/src/skia/web/JsiSkPath.ts +4 -4
  126. package/src/skia/web/JsiSkPathEffectFactory.ts +1 -1
  127. package/src/skia/web/JsiSkPathFactory.ts +1 -1
  128. package/src/skia/web/JsiSkPicture.ts +3 -3
  129. package/src/skia/web/JsiSkShaderFactory.ts +5 -5
  130. package/src/skia/web/JsiSkVerticesFactory.ts +1 -1
  131. package/lib/commonjs/skia/types/Image/ColorType.web.d.ts +0 -19
  132. package/lib/commonjs/skia/types/Image/ColorType.web.js +0 -43
  133. package/lib/commonjs/skia/types/Image/ColorType.web.js.map +0 -1
  134. package/lib/module/skia/types/Image/ColorType.web.d.ts +0 -19
  135. package/lib/module/skia/types/Image/ColorType.web.js +0 -37
  136. package/lib/module/skia/types/Image/ColorType.web.js.map +0 -1
  137. package/lib/typescript/lib/commonjs/skia/types/Image/ColorType.web.d.ts +0 -2
  138. package/lib/typescript/lib/module/skia/types/Image/ColorType.web.d.ts +0 -1
  139. package/lib/typescript/src/skia/types/Image/ColorType.web.d.ts +0 -19
  140. package/src/skia/types/Image/ColorType.web.ts +0 -19
@@ -0,0 +1,172 @@
1
+ #import <React/RCTBridge.h>
2
+
3
+ #import "RNSkiaModule.h"
4
+ #import "SkiaUIView.h"
5
+
6
+ #include <utility>
7
+ #include <vector>
8
+
9
+ #import "RNSkManager.h"
10
+
11
+ @implementation SkiaUIView {
12
+ std::shared_ptr<RNSkBaseAppleView> _impl;
13
+ RNSkia::RNSkManager *_manager;
14
+ std::function<std::shared_ptr<RNSkBaseAppleView>(
15
+ std::shared_ptr<RNSkia::RNSkPlatformContext>)>
16
+ _factory;
17
+ bool _debugMode;
18
+ bool _opaque;
19
+ size_t _nativeId;
20
+ }
21
+
22
+ #pragma mark Initialization and destruction
23
+
24
+ - (instancetype)initWithManager:(RNSkia::RNSkManager *)manager
25
+ factory:
26
+ (std::function<std::shared_ptr<RNSkBaseAppleView>(
27
+ std::shared_ptr<RNSkia::RNSkPlatformContext>)>)
28
+ factory {
29
+ self = [super init];
30
+ if (self) {
31
+ [self initCommon:manager factory:factory];
32
+ }
33
+ // Listen to notifications about module invalidation
34
+ [[NSNotificationCenter defaultCenter]
35
+ addObserver:self
36
+ selector:@selector(willInvalidateModules)
37
+ name:RCTBridgeWillInvalidateModulesNotification
38
+ object:nil];
39
+ return self;
40
+ }
41
+
42
+ - (void)initCommon:(RNSkia::RNSkManager *)manager
43
+ factory:(std::function<std::shared_ptr<RNSkBaseAppleView>(
44
+ std::shared_ptr<RNSkia::RNSkPlatformContext>)>)factory {
45
+ _manager = manager;
46
+ _nativeId = 0;
47
+ _debugMode = false;
48
+ _factory = factory;
49
+ }
50
+
51
+ - (void)willInvalidateModules {
52
+ _impl = nullptr;
53
+ _manager = nullptr;
54
+ }
55
+
56
+ #pragma mark Lifecycle
57
+
58
+ #if !TARGET_OS_OSX
59
+ - (void)willMoveToSuperview:(UIView *)newSuperView {
60
+ #else
61
+ - (void)viewWillMoveToSuperview:(NSView *)newSuperView {
62
+ #endif // !TARGET_OS_OSX
63
+ if (newSuperView != nullptr) {
64
+ // Create implementation view when the parent view is set
65
+ if (_impl == nullptr && _manager != nullptr) {
66
+ _impl = _factory(_manager->getPlatformContext());
67
+ if (_impl == nullptr) {
68
+ throw std::runtime_error(
69
+ "Expected Skia view implementation, got nullptr.");
70
+ }
71
+ [self.layer addSublayer:_impl->getLayer()];
72
+ if (_nativeId != 0) {
73
+ _manager->setSkiaView(_nativeId, _impl->getDrawView());
74
+ }
75
+ _impl->getDrawView()->setShowDebugOverlays(_debugMode);
76
+ }
77
+ }
78
+ }
79
+
80
+ - (void)removeFromSuperview {
81
+ // Cleanup when removed from view hierarchy
82
+ if (_impl != nullptr) {
83
+ [_impl->getLayer() removeFromSuperlayer];
84
+
85
+ if (_nativeId != 0 && _manager != nullptr) {
86
+ _manager->setSkiaView(_nativeId, nullptr);
87
+ }
88
+
89
+ _impl = nullptr;
90
+ }
91
+
92
+ [super removeFromSuperview];
93
+ }
94
+
95
+ - (void)dealloc {
96
+ [self unregisterView];
97
+ [[NSNotificationCenter defaultCenter]
98
+ removeObserver:self
99
+ name:RCTBridgeWillInvalidateModulesNotification
100
+ object:nil];
101
+ }
102
+
103
+ #ifdef RCT_NEW_ARCH_ENABLED
104
+ - (void)prepareForRecycle {
105
+ [super prepareForRecycle];
106
+ [self unregisterView];
107
+ }
108
+
109
+ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask {
110
+ [super finalizeUpdates:updateMask];
111
+ if (updateMask == RNComponentViewUpdateMaskAll) {
112
+ // this flag is only set when the view is inserted and we want to set the
113
+ // manager here since the view could be recycled or the app could be
114
+ // refreshed and we would have a stale manager then
115
+ _manager = [SkiaManager latestActiveSkManager].get();
116
+ }
117
+ }
118
+ #endif // RCT_NEW_ARCH_ENABLED
119
+
120
+ - (void)unregisterView {
121
+ if (_manager != nullptr && _nativeId != 0) {
122
+ _manager->unregisterSkiaView(_nativeId);
123
+ }
124
+ }
125
+
126
+ #pragma Render
127
+
128
+ - (void)drawRect:(CGRect)rect {
129
+ // We override drawRect to ensure we to direct rendering when the
130
+ // underlying OS view needs to render:
131
+ if (_impl != nullptr) {
132
+ _impl->getDrawView()->redraw();
133
+ }
134
+ }
135
+
136
+ #pragma mark Layout
137
+
138
+ - (void)layoutSubviews {
139
+ [super layoutSubviews];
140
+ if (_impl != nullptr) {
141
+ _impl->setSize(self.bounds.size.width, self.bounds.size.height);
142
+ }
143
+ }
144
+
145
+ #pragma mark Properties
146
+
147
+ - (void)setDebugMode:(bool)debugMode {
148
+ _debugMode = debugMode;
149
+ if (_impl != nullptr) {
150
+ _impl->getDrawView()->setShowDebugOverlays(debugMode);
151
+ }
152
+ }
153
+
154
+ - (void)setOpaque:(bool)opaque {
155
+ _opaque = opaque;
156
+ }
157
+
158
+ - (void)setNativeId:(size_t)nativeId {
159
+ _nativeId = nativeId;
160
+
161
+ if (_impl != nullptr) {
162
+ _manager->registerSkiaView(nativeId, _impl->getDrawView());
163
+ }
164
+ }
165
+
166
+ #pragma mark External API
167
+
168
+ - (std::shared_ptr<RNSkBaseAppleView>)impl {
169
+ return _impl;
170
+ }
171
+
172
+ @end
@@ -0,0 +1,25 @@
1
+ #pragma once
2
+
3
+ #import <CoreFoundation/CoreFoundation.h>
4
+ #if !TARGET_OS_OSX
5
+ #import <UIKit/UIKit.h>
6
+ #else
7
+ #import <Appkit/Appkit.h>
8
+ #endif // !TARGET_OS_OSX
9
+
10
+ #import <React/RCTUIManager.h>
11
+
12
+ #pragma clang diagnostic push
13
+ #pragma clang diagnostic ignored "-Wdocumentation"
14
+
15
+ #include "include/core/SkImage.h"
16
+
17
+ #pragma clang diagnostic pop
18
+
19
+ @interface ViewScreenshotService : NSObject {
20
+ }
21
+
22
+ - (instancetype)initWithUiManager:(RCTUIManager *)uiManager;
23
+ - (sk_sp<SkImage>)screenshotOfViewWithTag:(NSNumber *)viewTag;
24
+
25
+ @end
@@ -0,0 +1,89 @@
1
+ #import "ViewScreenshotService.h"
2
+ #import <QuartzCore/QuartzCore.h>
3
+
4
+ #pragma clang diagnostic push
5
+ #pragma clang diagnostic ignored "-Wdocumentation"
6
+
7
+ #include "include/core/SkData.h"
8
+
9
+ #pragma clang diagnostic pop
10
+
11
+ @implementation ViewScreenshotService {
12
+ RCTUIManager *_uiManager;
13
+ }
14
+
15
+ - (instancetype)initWithUiManager:(RCTUIManager *)uiManager {
16
+ if (self = [super init]) {
17
+ _uiManager = uiManager;
18
+ }
19
+ return self;
20
+ }
21
+
22
+ - (sk_sp<SkImage>)screenshotOfViewWithTag:(NSNumber *)viewTag {
23
+ #if !TARGET_OS_OSX
24
+ // Find view corresponding to the tag
25
+ auto view = [_uiManager viewForReactTag:viewTag];
26
+ if (view == NULL) {
27
+ RCTFatal(RCTErrorWithMessage(@"Could not find view with tag"));
28
+ return nullptr;
29
+ }
30
+
31
+ // Get size
32
+ CGSize size = view.frame.size;
33
+
34
+ // Setup context
35
+ UIGraphicsImageRendererFormat *format =
36
+ [UIGraphicsImageRendererFormat defaultFormat];
37
+ format.opaque = NO;
38
+ format.preferredRange = UIGraphicsImageRendererFormatRangeStandard;
39
+
40
+ UIGraphicsImageRenderer *renderer =
41
+ [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];
42
+
43
+ // Render to context - this is now the only part of this function that shows
44
+ // up in the profiler!
45
+ UIImage *image = [renderer
46
+ imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull context) {
47
+ [view drawViewHierarchyInRect:(CGRect){CGPointZero, size}
48
+ afterScreenUpdates:YES];
49
+ }];
50
+
51
+ // Convert from UIImage -> CGImage -> SkImage
52
+ CGImageRef cgImage = image.CGImage;
53
+
54
+ // Get some info about the image
55
+ auto width = CGImageGetWidth(cgImage);
56
+ auto height = CGImageGetHeight(cgImage);
57
+ auto bytesPerRow = CGImageGetBytesPerRow(cgImage);
58
+
59
+ // Convert from UIImage -> SkImage, start by getting the pixels directly from
60
+ // the CGImage:
61
+ auto dataRef = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
62
+ auto length = CFDataGetLength(dataRef);
63
+ void *data = CFDataGetMutableBytePtr((CFMutableDataRef)dataRef);
64
+
65
+ // Now we'll capture the data in an SkData object and control releasing it:
66
+ auto skData = SkData::MakeWithProc(
67
+ data, length,
68
+ [](const void *ptr, void *context) {
69
+ CFDataRef dataRef = (CFDataRef)context;
70
+ CFRelease(dataRef);
71
+ },
72
+ (void *)dataRef);
73
+
74
+ // Make SkImageInfo
75
+ // We're using kBGRA_8888_SkColorType since this is what we get when the
76
+ // UIGraphicsImageRenderer uses the standard format (the extended is using
77
+ // 64bits so it is not suitable for us).
78
+ SkImageInfo info =
79
+ SkImageInfo::Make(static_cast<int>(width), static_cast<int>(height),
80
+ kBGRA_8888_SkColorType, kPremul_SkAlphaType);
81
+
82
+ // ... and then create the SkImage itself!
83
+ return SkImages::RasterFromData(info, skData, bytesPerRow);
84
+ #else
85
+ return nullptr;
86
+ #endif // !TARGET_OS_OSX
87
+ }
88
+
89
+ @end
@@ -84,14 +84,22 @@ public:
84
84
  SkPoint pts[] = {p1, p2};
85
85
 
86
86
  std::vector<SkColor> colors = getColors(runtime, arguments[2]);
87
+ auto colorsSize = colors.size();
88
+ if (colorsSize < 2) {
89
+ throw std::invalid_argument("colors must have at least 2 colors");
90
+ }
87
91
  std::vector<SkScalar> positions = getPositions(runtime, arguments[3]);
92
+ if (!positions.empty() && positions.size() != colorsSize) {
93
+ throw std::invalid_argument(
94
+ "positions must be empty or have the same size as colors");
95
+ }
88
96
  auto tileMode = getTileMode(arguments, 4, count);
89
97
  auto flag = getFlag(arguments, 6, count);
90
98
  auto localMatrix = getLocalMatrix(runtime, arguments, 5, count);
91
99
 
92
100
  sk_sp<SkShader> gradient = SkGradientShader::MakeLinear(
93
- pts, colors.data(), positions.data(), static_cast<int>(colors.size()),
94
- tileMode, flag, localMatrix);
101
+ pts, colors.data(), !positions.empty() ? positions.data() : nullptr,
102
+ static_cast<int>(colorsSize), tileMode, flag, localMatrix);
95
103
  return jsi::Object::createFromHostObject(
96
104
  runtime,
97
105
  std::make_shared<JsiSkShader>(getContext(), std::move(gradient)));
@@ -103,14 +111,23 @@ public:
103
111
  auto r = arguments[1].asNumber();
104
112
 
105
113
  std::vector<SkColor> colors = getColors(runtime, arguments[2]);
114
+ auto colorsSize = colors.size();
115
+ if (colorsSize < 2) {
116
+ throw std::invalid_argument("colors must have at least 2 colors");
117
+ }
106
118
  std::vector<SkScalar> positions = getPositions(runtime, arguments[3]);
119
+ if (!positions.empty() && positions.size() != colorsSize) {
120
+ throw std::invalid_argument(
121
+ "positions must be empty or the same size as colors");
122
+ }
107
123
  auto tileMode = getTileMode(arguments, 4, count);
108
124
  auto flag = getFlag(arguments, 6, count);
109
125
  auto localMatrix = getLocalMatrix(runtime, arguments, 5, count);
110
126
 
111
127
  sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
112
- center, r, colors.data(), positions.data(),
113
- static_cast<int>(colors.size()), tileMode, flag, localMatrix);
128
+ center, r, colors.data(),
129
+ !positions.empty() ? positions.data() : nullptr,
130
+ static_cast<int>(colorsSize), tileMode, flag, localMatrix);
114
131
  return jsi::Object::createFromHostObject(
115
132
  runtime,
116
133
  std::make_shared<JsiSkShader>(getContext(), std::move(gradient)));
@@ -120,7 +137,15 @@ public:
120
137
  auto x = arguments[0].asNumber();
121
138
  auto y = arguments[1].asNumber();
122
139
  std::vector<SkColor> colors = getColors(runtime, arguments[2]);
140
+ auto colorsSize = colors.size();
141
+ if (colorsSize < 2) {
142
+ throw std::invalid_argument("colors must have at least 2 colors");
143
+ }
123
144
  std::vector<SkScalar> positions = getPositions(runtime, arguments[3]);
145
+ if (!positions.empty() && positions.size() != colorsSize) {
146
+ throw std::invalid_argument(
147
+ "positions must be empty or the same size as colors");
148
+ }
124
149
  auto tileMode = getTileMode(arguments, 4, count);
125
150
  auto localMatrix = getLocalMatrix(runtime, arguments, 5, count);
126
151
  auto flag = getFlag(arguments, 6, count);
@@ -130,8 +155,9 @@ public:
130
155
  ? 360
131
156
  : arguments[8].asNumber();
132
157
  sk_sp<SkShader> gradient = SkGradientShader::MakeSweep(
133
- x, y, colors.data(), positions.data(), static_cast<int>(colors.size()),
134
- tileMode, startAngle, endAngle, flag, localMatrix);
158
+ x, y, colors.data(), !positions.empty() ? positions.data() : nullptr,
159
+ static_cast<int>(colorsSize), tileMode, startAngle, endAngle, flag,
160
+ localMatrix);
135
161
  return jsi::Object::createFromHostObject(
136
162
  runtime,
137
163
  std::make_shared<JsiSkShader>(getContext(), std::move(gradient)));
@@ -147,14 +173,23 @@ public:
147
173
  auto endRadius = arguments[3].asNumber();
148
174
 
149
175
  std::vector<SkColor> colors = getColors(runtime, arguments[4]);
176
+ auto colorsSize = colors.size();
177
+ if (colorsSize < 2) {
178
+ throw std::invalid_argument("colors must have at least 2 colors");
179
+ }
150
180
  std::vector<SkScalar> positions = getPositions(runtime, arguments[5]);
181
+ if (!positions.empty() && positions.size() != colorsSize) {
182
+ throw std::invalid_argument(
183
+ "positions must be empty or the same size as colors");
184
+ }
151
185
  auto tileMode = getTileMode(arguments, 6, count);
152
186
  auto localMatrix = getLocalMatrix(runtime, arguments, 7, count);
153
187
  auto flag = getFlag(arguments, 8, count);
154
188
 
155
189
  sk_sp<SkShader> gradient = SkGradientShader::MakeTwoPointConical(
156
- start, startRadius, end, endRadius, colors.data(), positions.data(),
157
- static_cast<int>(colors.size()), tileMode, flag, localMatrix);
190
+ start, startRadius, end, endRadius, colors.data(),
191
+ !positions.empty() ? positions.data() : nullptr,
192
+ static_cast<int>(colorsSize), tileMode, flag, localMatrix);
158
193
 
159
194
  return jsi::Object::createFromHostObject(
160
195
  runtime,
@@ -70,6 +70,10 @@ public:
70
70
  if (count >= 3 && !arguments[2].isNull() && !arguments[2].isUndefined()) {
71
71
  auto jsiTexs = arguments[2].asObject(runtime).asArray(runtime);
72
72
  auto texsSize = jsiTexs.size(runtime);
73
+ if (texsSize != positionsSize) {
74
+ throw jsi::JSError(runtime, "The number of texture coordinates must "
75
+ "match the number of positions");
76
+ }
73
77
  texs.reserve(texsSize);
74
78
  for (int i = 0; i < texsSize; i++) {
75
79
  auto point = JsiSkPoint::fromValue(
@@ -81,6 +85,11 @@ public:
81
85
  if (count >= 4 && !arguments[3].isNull() && !arguments[3].isUndefined()) {
82
86
  auto jsiColors = arguments[3].asObject(runtime).asArray(runtime);
83
87
  auto colorsSize = jsiColors.size(runtime);
88
+ if (colorsSize != positionsSize) {
89
+ throw jsi::JSError(
90
+ runtime,
91
+ "The number of colors must match the number of positions");
92
+ }
84
93
  colors.reserve(colorsSize);
85
94
  for (int i = 0; i < colorsSize; i++) {
86
95
  SkColor color = JsiSkColor::fromValue(
@@ -132,9 +141,11 @@ public:
132
141
  // builder.indices());
133
142
  // }
134
143
  // auto vertices = builder.detach();
135
- auto vertices = SkVertices::MakeCopy(
136
- mode, positionsSize, positions.data(), texs.data(), colors.data(),
137
- indicesSize, indices.data());
144
+ auto vertices =
145
+ SkVertices::MakeCopy(mode, positionsSize, positions.data(),
146
+ texs.size() > 0 ? texs.data() : nullptr,
147
+ colors.size() > 0 ? colors.data() : nullptr,
148
+ indicesSize, indices.data());
138
149
  return jsi::Object::createFromHostObject(
139
150
  runtime,
140
151
  std::make_shared<JsiSkVertices>(context, std::move(vertices)));
@@ -15,4 +15,22 @@ interface CheckImageOptions {
15
15
  shouldFail?: boolean;
16
16
  }
17
17
  export declare const checkImage: (image: SkImage, relPath: string, opts?: CheckImageOptions) => number;
18
+ declare global {
19
+ namespace jest {
20
+ interface Matchers<R> {
21
+ /**
22
+ * Checks if values are approximately equal within the given tolerance.
23
+ * Works with:
24
+ * - Single numbers
25
+ * - Arrays of numbers
26
+ * - Float32Arrays
27
+ * - SVG path strings (compares numeric values with tolerance)
28
+ *
29
+ * @param expected - The expected value to compare against
30
+ * @param tolerance - The maximum allowed difference between elements (default: 0.01)
31
+ */
32
+ toBeApproximatelyEqual(expected: number | number[] | Float32Array | string, tolerance?: number): R;
33
+ }
34
+ }
35
+ }
18
36
  export {};
@@ -1,6 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { SkSurface } from "../skia";
3
3
  export * from "../renderer/components";
4
+ export * from "../skia/types";
4
5
  export declare const makeOffscreenSurface: (width: number, height: number) => SkSurface;
5
6
  export declare const getSkiaExports: () => {
6
7
  Skia: import("../skia/types").Skia;
@@ -23,6 +23,18 @@ Object.keys(_components).forEach(function (key) {
23
23
  }
24
24
  });
25
25
  });
26
+ var _types = require("../skia/types");
27
+ Object.keys(_types).forEach(function (key) {
28
+ if (key === "default" || key === "__esModule") return;
29
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
30
+ if (key in exports && exports[key] === _types[key]) return;
31
+ Object.defineProperty(exports, key, {
32
+ enumerable: true,
33
+ get: function () {
34
+ return _types[key];
35
+ }
36
+ });
37
+ });
26
38
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
27
39
  // @ts-ignore
28
40
 
@@ -1 +1 @@
1
- {"version":3,"names":["_web","require","_Reconciler","_components","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","Skia","makeOffscreenSurface","width","height","JsiSkApi","CanvasKit","surface","Surface","MakeOffscreen","Error","getSkiaExports","drawOffscreen","element","root","SkiaSGRoot","render","canvas","getCanvas","drawOnCanvas","unmount","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport { JsiSkApi } from \"../skia/web\";\nimport type { SkSurface } from \"../skia\";\nimport { SkiaSGRoot } from \"../sksg/Reconciler\";\n\nexport * from \"../renderer/components\";\n\nlet Skia: ReturnType<typeof JsiSkApi>;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const getSkiaExports = () => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n return { Skia };\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaSGRoot(Skia);\n root.render(element);\n const canvas = surface.getCanvas();\n root.drawOnCanvas(canvas);\n root.unmount();\n surface.flush();\n return surface.makeImageSnapshot();\n};\n"],"mappings":";;;;;;;;;;;AAKA,IAAAA,IAAA,GAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAD,OAAA;AAEA,IAAAE,WAAA,GAAAF,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAF,WAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAJ,WAAA,CAAAI,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAZ,WAAA,CAAAI,GAAA;IAAA;EAAA;AAAA;AATA;AACA;;AAUA,IAAIS,IAAiC;AAE9B,MAAMC,oBAAoB,GAAGA,CAACC,KAAa,EAAEC,MAAc,KAAK;EACrE,IAAI,CAACH,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,MAAMC,OAAO,GAAGN,IAAI,CAACO,OAAO,CAACC,aAAa,CAACN,KAAK,EAAEC,MAAM,CAAC;EACzD,IAAIG,OAAO,KAAK,IAAI,EAAE;IACpB,MAAM,IAAIG,KAAK,CAAC,0BAA0B,CAAC;EAC7C;EACA,OAAOH,OAAO;AAChB,CAAC;AAACV,OAAA,CAAAK,oBAAA,GAAAA,oBAAA;AAEK,MAAMS,cAAc,GAAGA,CAAA,KAAM;EAClC,IAAI,CAACV,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,OAAO;IAAEL;EAAK,CAAC;AACjB,CAAC;AAACJ,OAAA,CAAAc,cAAA,GAAAA,cAAA;AAEK,MAAMC,aAAa,GAAGA,CAACL,OAAkB,EAAEM,OAAkB,KAAK;EACvE,MAAMC,IAAI,GAAG,IAAIC,sBAAU,CAACd,IAAI,CAAC;EACjCa,IAAI,CAACE,MAAM,CAACH,OAAO,CAAC;EACpB,MAAMI,MAAM,GAAGV,OAAO,CAACW,SAAS,CAAC,CAAC;EAClCJ,IAAI,CAACK,YAAY,CAACF,MAAM,CAAC;EACzBH,IAAI,CAACM,OAAO,CAAC,CAAC;EACdb,OAAO,CAACc,KAAK,CAAC,CAAC;EACf,OAAOd,OAAO,CAACe,iBAAiB,CAAC,CAAC;AACpC,CAAC;AAACzB,OAAA,CAAAe,aAAA,GAAAA,aAAA","ignoreList":[]}
1
+ {"version":3,"names":["_web","require","_Reconciler","_components","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_types","Skia","makeOffscreenSurface","width","height","JsiSkApi","CanvasKit","surface","Surface","MakeOffscreen","Error","getSkiaExports","drawOffscreen","element","root","SkiaSGRoot","render","canvas","getCanvas","drawOnCanvas","unmount","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport type { SkSurface } from \"../skia\";\nimport { JsiSkApi } from \"../skia/web\";\nimport { SkiaSGRoot } from \"../sksg/Reconciler\";\n\nexport * from \"../renderer/components\";\nexport * from \"../skia/types\";\n\nlet Skia: ReturnType<typeof JsiSkApi>;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const getSkiaExports = () => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n return { Skia };\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaSGRoot(Skia);\n root.render(element);\n const canvas = surface.getCanvas();\n root.drawOnCanvas(canvas);\n root.unmount();\n surface.flush();\n return surface.makeImageSnapshot();\n};\n"],"mappings":";;;;;;;;;;;AAMA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAEA,IAAAE,WAAA,GAAAF,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAF,WAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAJ,WAAA,CAAAI,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAZ,WAAA,CAAAI,GAAA;IAAA;EAAA;AAAA;AACA,IAAAS,MAAA,GAAAf,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAW,MAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAS,MAAA,CAAAT,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,MAAA,CAAAT,GAAA;IAAA;EAAA;AAAA;AAVA;AACA;;AAWA,IAAIU,IAAiC;AAE9B,MAAMC,oBAAoB,GAAGA,CAACC,KAAa,EAAEC,MAAc,KAAK;EACrE,IAAI,CAACH,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,MAAMC,OAAO,GAAGN,IAAI,CAACO,OAAO,CAACC,aAAa,CAACN,KAAK,EAAEC,MAAM,CAAC;EACzD,IAAIG,OAAO,KAAK,IAAI,EAAE;IACpB,MAAM,IAAIG,KAAK,CAAC,0BAA0B,CAAC;EAC7C;EACA,OAAOH,OAAO;AAChB,CAAC;AAACX,OAAA,CAAAM,oBAAA,GAAAA,oBAAA;AAEK,MAAMS,cAAc,GAAGA,CAAA,KAAM;EAClC,IAAI,CAACV,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,OAAO;IAAEL;EAAK,CAAC;AACjB,CAAC;AAACL,OAAA,CAAAe,cAAA,GAAAA,cAAA;AAEK,MAAMC,aAAa,GAAGA,CAACL,OAAkB,EAAEM,OAAkB,KAAK;EACvE,MAAMC,IAAI,GAAG,IAAIC,sBAAU,CAACd,IAAI,CAAC;EACjCa,IAAI,CAACE,MAAM,CAACH,OAAO,CAAC;EACpB,MAAMI,MAAM,GAAGV,OAAO,CAACW,SAAS,CAAC,CAAC;EAClCJ,IAAI,CAACK,YAAY,CAACF,MAAM,CAAC;EACzBH,IAAI,CAACM,OAAO,CAAC,CAAC;EACdb,OAAO,CAACc,KAAK,CAAC,CAAC;EACf,OAAOd,OAAO,CAACe,iBAAiB,CAAC,CAAC;AACpC,CAAC;AAAC1B,OAAA,CAAAgB,aAAA,GAAAA,aAAA","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import type { CanvasKit, EmbindEnumEntity, EmbindEnum } from "canvaskit-wasm";
1
+ import type { CanvasKit, EmbindEnumEntity } from "canvaskit-wasm";
2
2
  import type { SkJSIInstance } from "../types";
3
3
  export declare const throwNotImplementedOnRNWeb: <T>() => T;
4
4
  export declare abstract class Host {
@@ -14,5 +14,5 @@ export declare abstract class BaseHostObject<T, N extends string> extends Host i
14
14
  export declare abstract class HostObject<T, N extends string> extends BaseHostObject<T, N> {
15
15
  static fromValue<T>(value: SkJSIInstance<string>): T;
16
16
  }
17
- export declare const getEnum: (e: EmbindEnum, v: number) => EmbindEnumEntity;
18
- export declare const optEnum: (e: EmbindEnum, value: number | undefined) => EmbindEnumEntity | undefined;
17
+ export declare const getEnum: (CanvasKit: CanvasKit, name: keyof CanvasKit, v: number) => EmbindEnumEntity;
18
+ export declare const optEnum: (CanvasKit: CanvasKit, name: keyof CanvasKit, v: number | undefined) => EmbindEnumEntity | undefined;
@@ -37,10 +37,22 @@ class HostObject extends BaseHostObject {
37
37
  }
38
38
  }
39
39
  exports.HostObject = HostObject;
40
- const getEnum = (e, v) => Object.values(e).find(({
41
- value
42
- }) => value === v);
40
+ const getEnum = (CanvasKit, name, v) => {
41
+ const e = CanvasKit[name];
42
+ if (typeof e !== "function") {
43
+ throw new Error(`${name} is not an number`);
44
+ }
45
+ const result = Object.values(e).find(({
46
+ value
47
+ }) => value === v);
48
+ if (!result) {
49
+ throw new Error(`Enum ${name} does not have value ${v} on React Native Web`);
50
+ }
51
+ return result;
52
+ };
43
53
  exports.getEnum = getEnum;
44
- const optEnum = (e, value) => value === undefined ? undefined : getEnum(e, value);
54
+ const optEnum = (CanvasKit, name, v) => {
55
+ return v === undefined ? undefined : getEnum(CanvasKit, name, v);
56
+ };
45
57
  exports.optEnum = optEnum;
46
58
  //# sourceMappingURL=Host.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["throwNotImplementedOnRNWeb","jest","fn","Error","exports","Host","constructor","CanvasKit","_defineProperty","BaseHostObject","ref","typename","__typename__","HostObject","fromValue","value","getEnum","e","v","Object","values","find","optEnum","undefined"],"sources":["Host.ts"],"sourcesContent":["import type { CanvasKit, EmbindEnumEntity, EmbindEnum } from \"canvaskit-wasm\";\n\nimport type { SkJSIInstance } from \"../types\";\n\nexport const throwNotImplementedOnRNWeb = <T>(): T => {\n if (typeof jest !== \"undefined\") {\n return jest.fn() as unknown as T;\n }\n throw new Error(\"Not implemented on React Native Web\");\n};\n\nexport abstract class Host {\n readonly CanvasKit: CanvasKit;\n\n constructor(CanvasKit: CanvasKit) {\n this.CanvasKit = CanvasKit;\n }\n}\n\nexport abstract class BaseHostObject<T, N extends string>\n extends Host\n implements SkJSIInstance<N>\n{\n readonly __typename__: N;\n ref: T;\n\n constructor(CanvasKit: CanvasKit, ref: T, typename: N) {\n super(CanvasKit);\n this.ref = ref;\n this.__typename__ = typename;\n }\n\n abstract dispose(): void;\n}\n\nexport abstract class HostObject<T, N extends string> extends BaseHostObject<\n T,\n N\n> {\n static fromValue<T>(value: SkJSIInstance<string>) {\n return (value as HostObject<T, string>).ref;\n }\n}\n\nexport const getEnum = (e: EmbindEnum, v: number): EmbindEnumEntity =>\n Object.values(e).find(({ value }) => value === v);\n\nexport const optEnum = (\n e: EmbindEnum,\n value: number | undefined\n): EmbindEnumEntity | undefined =>\n value === undefined ? undefined : getEnum(e, value);\n"],"mappings":";;;;;;;;;AAIO,MAAMA,0BAA0B,GAAGA,CAAA,KAAY;EACpD,IAAI,OAAOC,IAAI,KAAK,WAAW,EAAE;IAC/B,OAAOA,IAAI,CAACC,EAAE,CAAC,CAAC;EAClB;EACA,MAAM,IAAIC,KAAK,CAAC,qCAAqC,CAAC;AACxD,CAAC;AAACC,OAAA,CAAAJ,0BAAA,GAAAA,0BAAA;AAEK,MAAeK,IAAI,CAAC;EAGzBC,WAAWA,CAACC,SAAoB,EAAE;IAAAC,eAAA;IAChC,IAAI,CAACD,SAAS,GAAGA,SAAS;EAC5B;AACF;AAACH,OAAA,CAAAC,IAAA,GAAAA,IAAA;AAEM,MAAeI,cAAc,SAC1BJ,IAAI,CAEd;EAIEC,WAAWA,CAACC,SAAoB,EAAEG,GAAM,EAAEC,QAAW,EAAE;IACrD,KAAK,CAACJ,SAAS,CAAC;IAACC,eAAA;IAAAA,eAAA;IACjB,IAAI,CAACE,GAAG,GAAGA,GAAG;IACd,IAAI,CAACE,YAAY,GAAGD,QAAQ;EAC9B;AAGF;AAACP,OAAA,CAAAK,cAAA,GAAAA,cAAA;AAEM,MAAeI,UAAU,SAA8BJ,cAAc,CAG1E;EACA,OAAOK,SAASA,CAAIC,KAA4B,EAAE;IAChD,OAAQA,KAAK,CAA2BL,GAAG;EAC7C;AACF;AAACN,OAAA,CAAAS,UAAA,GAAAA,UAAA;AAEM,MAAMG,OAAO,GAAGA,CAACC,CAAa,EAAEC,CAAS,KAC9CC,MAAM,CAACC,MAAM,CAACH,CAAC,CAAC,CAACI,IAAI,CAAC,CAAC;EAAEN;AAAM,CAAC,KAAKA,KAAK,KAAKG,CAAC,CAAC;AAACd,OAAA,CAAAY,OAAA,GAAAA,OAAA;AAE7C,MAAMM,OAAO,GAAGA,CACrBL,CAAa,EACbF,KAAyB,KAEzBA,KAAK,KAAKQ,SAAS,GAAGA,SAAS,GAAGP,OAAO,CAACC,CAAC,EAAEF,KAAK,CAAC;AAACX,OAAA,CAAAkB,OAAA,GAAAA,OAAA","ignoreList":[]}
1
+ {"version":3,"names":["throwNotImplementedOnRNWeb","jest","fn","Error","exports","Host","constructor","CanvasKit","_defineProperty","BaseHostObject","ref","typename","__typename__","HostObject","fromValue","value","getEnum","name","v","e","result","Object","values","find","optEnum","undefined"],"sources":["Host.ts"],"sourcesContent":["import type { CanvasKit, EmbindEnumEntity } from \"canvaskit-wasm\";\n\nimport type { SkJSIInstance } from \"../types\";\n\nexport const throwNotImplementedOnRNWeb = <T>(): T => {\n if (typeof jest !== \"undefined\") {\n return jest.fn() as unknown as T;\n }\n throw new Error(\"Not implemented on React Native Web\");\n};\n\nexport abstract class Host {\n readonly CanvasKit: CanvasKit;\n\n constructor(CanvasKit: CanvasKit) {\n this.CanvasKit = CanvasKit;\n }\n}\n\nexport abstract class BaseHostObject<T, N extends string>\n extends Host\n implements SkJSIInstance<N>\n{\n readonly __typename__: N;\n ref: T;\n\n constructor(CanvasKit: CanvasKit, ref: T, typename: N) {\n super(CanvasKit);\n this.ref = ref;\n this.__typename__ = typename;\n }\n\n abstract dispose(): void;\n}\n\nexport abstract class HostObject<T, N extends string> extends BaseHostObject<\n T,\n N\n> {\n static fromValue<T>(value: SkJSIInstance<string>) {\n return (value as HostObject<T, string>).ref;\n }\n}\n\nexport const getEnum = (\n CanvasKit: CanvasKit,\n name: keyof CanvasKit,\n v: number\n): EmbindEnumEntity => {\n const e = CanvasKit[name];\n if (typeof e !== \"function\") {\n throw new Error(`${name} is not an number`);\n }\n const result = Object.values(e).find(({ value }) => value === v);\n if (!result) {\n throw new Error(\n `Enum ${name} does not have value ${v} on React Native Web`\n );\n }\n return result;\n};\n\nexport const optEnum = (\n CanvasKit: CanvasKit,\n name: keyof CanvasKit,\n v: number | undefined\n): EmbindEnumEntity | undefined => {\n return v === undefined ? undefined : getEnum(CanvasKit, name, v);\n};\n"],"mappings":";;;;;;;;;AAIO,MAAMA,0BAA0B,GAAGA,CAAA,KAAY;EACpD,IAAI,OAAOC,IAAI,KAAK,WAAW,EAAE;IAC/B,OAAOA,IAAI,CAACC,EAAE,CAAC,CAAC;EAClB;EACA,MAAM,IAAIC,KAAK,CAAC,qCAAqC,CAAC;AACxD,CAAC;AAACC,OAAA,CAAAJ,0BAAA,GAAAA,0BAAA;AAEK,MAAeK,IAAI,CAAC;EAGzBC,WAAWA,CAACC,SAAoB,EAAE;IAAAC,eAAA;IAChC,IAAI,CAACD,SAAS,GAAGA,SAAS;EAC5B;AACF;AAACH,OAAA,CAAAC,IAAA,GAAAA,IAAA;AAEM,MAAeI,cAAc,SAC1BJ,IAAI,CAEd;EAIEC,WAAWA,CAACC,SAAoB,EAAEG,GAAM,EAAEC,QAAW,EAAE;IACrD,KAAK,CAACJ,SAAS,CAAC;IAACC,eAAA;IAAAA,eAAA;IACjB,IAAI,CAACE,GAAG,GAAGA,GAAG;IACd,IAAI,CAACE,YAAY,GAAGD,QAAQ;EAC9B;AAGF;AAACP,OAAA,CAAAK,cAAA,GAAAA,cAAA;AAEM,MAAeI,UAAU,SAA8BJ,cAAc,CAG1E;EACA,OAAOK,SAASA,CAAIC,KAA4B,EAAE;IAChD,OAAQA,KAAK,CAA2BL,GAAG;EAC7C;AACF;AAACN,OAAA,CAAAS,UAAA,GAAAA,UAAA;AAEM,MAAMG,OAAO,GAAGA,CACrBT,SAAoB,EACpBU,IAAqB,EACrBC,CAAS,KACY;EACrB,MAAMC,CAAC,GAAGZ,SAAS,CAACU,IAAI,CAAC;EACzB,IAAI,OAAOE,CAAC,KAAK,UAAU,EAAE;IAC3B,MAAM,IAAIhB,KAAK,CAAC,GAAGc,IAAI,mBAAmB,CAAC;EAC7C;EACA,MAAMG,MAAM,GAAGC,MAAM,CAACC,MAAM,CAACH,CAAC,CAAC,CAACI,IAAI,CAAC,CAAC;IAAER;EAAM,CAAC,KAAKA,KAAK,KAAKG,CAAC,CAAC;EAChE,IAAI,CAACE,MAAM,EAAE;IACX,MAAM,IAAIjB,KAAK,CACb,QAAQc,IAAI,wBAAwBC,CAAC,sBACvC,CAAC;EACH;EACA,OAAOE,MAAM;AACf,CAAC;AAAChB,OAAA,CAAAY,OAAA,GAAAA,OAAA;AAEK,MAAMQ,OAAO,GAAGA,CACrBjB,SAAoB,EACpBU,IAAqB,EACrBC,CAAqB,KACY;EACjC,OAAOA,CAAC,KAAKO,SAAS,GAAGA,SAAS,GAAGT,OAAO,CAACT,SAAS,EAAEU,IAAI,EAAEC,CAAC,CAAC;AAClE,CAAC;AAACd,OAAA,CAAAoB,OAAA,GAAAA,OAAA","ignoreList":[]}
@@ -42,16 +42,16 @@ class JsiSkCanvas extends _Host.HostObject {
42
42
  this.ref.drawImageCubic(_JsiSkImage.JsiSkImage.fromValue(img), left, top, B, C, paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
43
43
  }
44
44
  drawImageOptions(img, left, top, fm, mm, paint) {
45
- this.ref.drawImageOptions(_JsiSkImage.JsiSkImage.fromValue(img), left, top, (0, _Host.getEnum)(this.CanvasKit.FilterMode, fm), (0, _Host.getEnum)(this.CanvasKit.MipmapMode, mm), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
45
+ this.ref.drawImageOptions(_JsiSkImage.JsiSkImage.fromValue(img), left, top, (0, _Host.getEnum)(this.CanvasKit, "FilterMode", fm), (0, _Host.getEnum)(this.CanvasKit, "MipmapMode", mm), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
46
46
  }
47
47
  drawImageNine(img, center, dest, filter, paint) {
48
- this.ref.drawImageNine(_JsiSkImage.JsiSkImage.fromValue(img), Array.from(_JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, center)), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, dest), (0, _Host.getEnum)(this.CanvasKit.FilterMode, filter), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
48
+ this.ref.drawImageNine(_JsiSkImage.JsiSkImage.fromValue(img), Array.from(_JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, center)), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, dest), (0, _Host.getEnum)(this.CanvasKit, "FilterMode", filter), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
49
49
  }
50
50
  drawImageRectCubic(img, src, dest, B, C, paint) {
51
51
  this.ref.drawImageRectCubic(_JsiSkImage.JsiSkImage.fromValue(img), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, src), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, dest), B, C, paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
52
52
  }
53
53
  drawImageRectOptions(img, src, dest, fm, mm, paint) {
54
- this.ref.drawImageRectOptions(_JsiSkImage.JsiSkImage.fromValue(img), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, src), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, dest), (0, _Host.getEnum)(this.CanvasKit.FilterMode, fm), (0, _Host.getEnum)(this.CanvasKit.MipmapMode, mm), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
54
+ this.ref.drawImageRectOptions(_JsiSkImage.JsiSkImage.fromValue(img), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, src), _JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, dest), (0, _Host.getEnum)(this.CanvasKit, "FilterMode", fm), (0, _Host.getEnum)(this.CanvasKit, "MipmapMode", mm), paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : paint);
55
55
  }
56
56
  drawPaint(paint) {
57
57
  this.ref.drawPaint(_JsiSkPaint.JsiSkPaint.fromValue(paint));
@@ -63,19 +63,19 @@ class JsiSkCanvas extends _Host.HostObject {
63
63
  this.ref.drawCircle(cx, cy, radius, _JsiSkPaint.JsiSkPaint.fromValue(paint));
64
64
  }
65
65
  drawVertices(verts, mode, paint) {
66
- this.ref.drawVertices(_JsiSkVertices.JsiSkVertices.fromValue(verts), (0, _Host.getEnum)(this.CanvasKit.BlendMode, mode), _JsiSkPaint.JsiSkPaint.fromValue(paint));
66
+ this.ref.drawVertices(_JsiSkVertices.JsiSkVertices.fromValue(verts), (0, _Host.getEnum)(this.CanvasKit, "BlendMode", mode), _JsiSkPaint.JsiSkPaint.fromValue(paint));
67
67
  }
68
68
  drawPatch(cubics, colors, texs, mode, paint) {
69
69
  this.ref.drawPatch(cubics.map(({
70
70
  x,
71
71
  y
72
- }) => [x, y]).flat(), colors, texs ? texs.flatMap(p => Array.from(_JsiSkPoint.JsiSkPoint.fromValue(p))) : texs, mode ? (0, _Host.getEnum)(this.CanvasKit.BlendMode, mode) : null, paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : undefined);
72
+ }) => [x, y]).flat(), colors, texs ? texs.flatMap(p => Array.from(_JsiSkPoint.JsiSkPoint.fromValue(p))) : texs, mode ? (0, _Host.getEnum)(this.CanvasKit, "BlendMode", mode) : null, paint ? _JsiSkPaint.JsiSkPaint.fromValue(paint) : undefined);
73
73
  }
74
74
  restoreToCount(saveCount) {
75
75
  this.ref.restoreToCount(saveCount);
76
76
  }
77
77
  drawPoints(mode, points, paint) {
78
- this.ref.drawPoints((0, _Host.getEnum)(this.CanvasKit.PointMode, mode), points.map(({
78
+ this.ref.drawPoints((0, _Host.getEnum)(this.CanvasKit, "PointMode", mode), points.map(({
79
79
  x,
80
80
  y
81
81
  }) => [x, y]).flat(), _JsiSkPaint.JsiSkPaint.fromValue(paint));
@@ -130,19 +130,19 @@ class JsiSkCanvas extends _Host.HostObject {
130
130
  this.ref.translate(dx, dy);
131
131
  }
132
132
  drawColor(color, blendMode) {
133
- this.ref.drawColor(color, blendMode ? (0, _Host.getEnum)(this.CanvasKit.BlendMode, blendMode) : undefined);
133
+ this.ref.drawColor(color, blendMode ? (0, _Host.getEnum)(this.CanvasKit, "BlendMode", blendMode) : undefined);
134
134
  }
135
135
  clear(color) {
136
136
  this.ref.clear(color);
137
137
  }
138
138
  clipPath(path, op, doAntiAlias) {
139
- this.ref.clipPath(_JsiSkPath.JsiSkPath.fromValue(path), (0, _Host.getEnum)(this.CanvasKit.PathOp, op), doAntiAlias);
139
+ this.ref.clipPath(_JsiSkPath.JsiSkPath.fromValue(path), (0, _Host.getEnum)(this.CanvasKit, "PathOp", op), doAntiAlias);
140
140
  }
141
141
  clipRect(rect, op, doAntiAlias) {
142
- this.ref.clipRect(_JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, rect), (0, _Host.getEnum)(this.CanvasKit.PathOp, op), doAntiAlias);
142
+ this.ref.clipRect(_JsiSkRect.JsiSkRect.fromValue(this.CanvasKit, rect), (0, _Host.getEnum)(this.CanvasKit, "PathOp", op), doAntiAlias);
143
143
  }
144
144
  clipRRect(rrect, op, doAntiAlias) {
145
- this.ref.clipRRect(_JsiSkRRect.JsiSkRRect.fromValue(this.CanvasKit, rrect), (0, _Host.getEnum)(this.CanvasKit.PathOp, op), doAntiAlias);
145
+ this.ref.clipRRect(_JsiSkRRect.JsiSkRRect.fromValue(this.CanvasKit, rrect), (0, _Host.getEnum)(this.CanvasKit, "PathOp", op), doAntiAlias);
146
146
  }
147
147
  concat(m) {
148
148
  this.ref.concat(Array.isArray(m) ? m : _JsiSkMatrix.JsiSkMatrix.fromValue(m));
@@ -169,19 +169,19 @@ class JsiSkCanvas extends _Host.HostObject {
169
169
  ckSampling = sampling;
170
170
  } else if (sampling) {
171
171
  ckSampling = {
172
- filter: (0, _Host.getEnum)(this.CanvasKit.FilterMode, sampling.filter),
173
- mipmap: sampling.mipmap ? (0, _Host.getEnum)(this.CanvasKit.MipmapMode, sampling.mipmap) : this.CanvasKit.MipmapMode.None
172
+ filter: (0, _Host.getEnum)(this.CanvasKit, "FilterMode", sampling.filter),
173
+ mipmap: sampling.mipmap ? (0, _Host.getEnum)(this.CanvasKit, "MipmapMode", sampling.mipmap) : this.CanvasKit.MipmapMode.None
174
174
  };
175
175
  }
176
- this.ref.drawAtlas(_JsiSkImage.JsiSkImage.fromValue(atlas), src, dst, _JsiSkPaint.JsiSkPaint.fromValue(paint), blendMode ? (0, _Host.getEnum)(this.CanvasKit.BlendMode, blendMode) : this.CanvasKit.BlendMode.DstOver, cls, ckSampling);
176
+ this.ref.drawAtlas(_JsiSkImage.JsiSkImage.fromValue(atlas), src, dst, _JsiSkPaint.JsiSkPaint.fromValue(paint), blendMode ? (0, _Host.getEnum)(this.CanvasKit, "BlendMode", blendMode) : this.CanvasKit.BlendMode.DstOver, cls, ckSampling);
177
177
  }
178
178
  readPixels(srcX, srcY, imageInfo) {
179
179
  const pxInfo = {
180
180
  width: imageInfo.width,
181
181
  height: imageInfo.height,
182
182
  colorSpace: this.CanvasKit.ColorSpace.SRGB,
183
- alphaType: (0, _Host.getEnum)(this.CanvasKit.AlphaType, imageInfo.alphaType),
184
- colorType: (0, _Host.getEnum)(this.CanvasKit.ColorType, imageInfo.colorType)
183
+ alphaType: (0, _Host.getEnum)(this.CanvasKit, "AlphaType", imageInfo.alphaType),
184
+ colorType: (0, _Host.getEnum)(this.CanvasKit, "ColorType", imageInfo.colorType)
185
185
  };
186
186
  return this.ref.readPixels(srcX, srcY, pxInfo);
187
187
  }