@shopify/react-native-skia 0.1.132 → 0.1.136

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. package/android/CMakeLists.txt +1 -1
  2. package/android/build.gradle +17 -2
  3. package/android/cpp/rnskia-android/RNSkDrawViewImpl.cpp +5 -0
  4. package/cpp/api/JsiSkFont.h +25 -1
  5. package/cpp/api/JsiSkImage.h +2 -19
  6. package/cpp/api/JsiSkPaint.h +5 -32
  7. package/cpp/api/JsiSkPath.h +4 -4
  8. package/cpp/rnskia/RNSkValueApi.h +6 -6
  9. package/cpp/rnskia/values/{RNSkDerivedValue.h → RNSkComputedValue.h} +7 -7
  10. package/ios/RNSkia-iOS/SkiaManager.mm +1 -1
  11. package/jestSetup.js +5 -0
  12. package/lib/commonjs/animation/functions/interpolate.js +3 -2
  13. package/lib/commonjs/animation/functions/interpolate.js.map +1 -1
  14. package/lib/commonjs/animation/functions/interpolateColors.js +2 -2
  15. package/lib/commonjs/animation/functions/interpolateColors.js.map +1 -1
  16. package/lib/commonjs/animation/functions/interpolatePaths.js +45 -11
  17. package/lib/commonjs/animation/functions/interpolatePaths.js.map +1 -1
  18. package/lib/commonjs/mock/index.js +134 -0
  19. package/lib/commonjs/mock/index.js.map +1 -0
  20. package/lib/commonjs/skia/core/Data.js +33 -43
  21. package/lib/commonjs/skia/core/Data.js.map +1 -1
  22. package/lib/commonjs/skia/core/Typeface.js +1 -1
  23. package/lib/commonjs/skia/core/Typeface.js.map +1 -1
  24. package/lib/commonjs/skia/types/Font/Font.js.map +1 -1
  25. package/lib/commonjs/skia/types/Image/Image.js +5 -5
  26. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  27. package/lib/commonjs/skia/types/Paint/Paint.js +3 -3
  28. package/lib/commonjs/skia/types/Paint/Paint.js.map +1 -1
  29. package/lib/commonjs/skia/types/Path/Path.js +0 -1
  30. package/lib/commonjs/skia/types/Path/Path.js.map +1 -1
  31. package/lib/commonjs/skia/types/Shader/Shader.js +1 -7
  32. package/lib/commonjs/skia/types/Shader/Shader.js.map +1 -1
  33. package/lib/commonjs/skia/web/JsiSkCanvas.js +1 -1
  34. package/lib/commonjs/skia/web/JsiSkCanvas.js.map +1 -1
  35. package/lib/commonjs/skia/web/JsiSkFont.js +6 -0
  36. package/lib/commonjs/skia/web/JsiSkFont.js.map +1 -1
  37. package/lib/commonjs/skia/web/JsiSkFontMgr.js +0 -2
  38. package/lib/commonjs/skia/web/JsiSkFontMgr.js.map +1 -1
  39. package/lib/commonjs/skia/web/JsiSkImage.js +10 -6
  40. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  41. package/lib/commonjs/skia/web/{JsiImageFilterFactory.js → JsiSkImageFilterFactory.js} +1 -1
  42. package/lib/commonjs/skia/web/JsiSkImageFilterFactory.js.map +1 -0
  43. package/lib/commonjs/skia/web/JsiSkPaint.js.map +1 -1
  44. package/lib/commonjs/skia/web/JsiSkPath.js +65 -52
  45. package/lib/commonjs/skia/web/JsiSkPath.js.map +1 -1
  46. package/lib/commonjs/skia/web/JsiSkShaderFactory.js +1 -1
  47. package/lib/commonjs/skia/web/JsiSkShaderFactory.js.map +1 -1
  48. package/lib/commonjs/skia/web/JsiSkVerticesFactory.js +23 -1
  49. package/lib/commonjs/skia/web/JsiSkVerticesFactory.js.map +1 -1
  50. package/lib/commonjs/skia/web/JsiSkia.js +2 -2
  51. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  52. package/lib/commonjs/values/api.js +1 -7
  53. package/lib/commonjs/values/api.js.map +1 -1
  54. package/lib/commonjs/values/api.web.js +3 -3
  55. package/lib/commonjs/values/api.web.js.map +1 -1
  56. package/lib/commonjs/values/hooks/index.js +4 -4
  57. package/lib/commonjs/values/hooks/index.js.map +1 -1
  58. package/lib/commonjs/values/hooks/useComputedValue.js +32 -0
  59. package/lib/commonjs/values/hooks/useComputedValue.js.map +1 -0
  60. package/lib/commonjs/values/web/{RNSkDerivedValue.js → RNSkComputedValue.js} +4 -4
  61. package/lib/commonjs/values/web/RNSkComputedValue.js.map +1 -0
  62. package/lib/commonjs/values/web/api.js +3 -3
  63. package/lib/commonjs/values/web/api.js.map +1 -1
  64. package/lib/commonjs/views/SkiaView.web.js +15 -15
  65. package/lib/commonjs/views/SkiaView.web.js.map +1 -1
  66. package/lib/commonjs/web/index.js +4 -2
  67. package/lib/commonjs/web/index.js.map +1 -1
  68. package/lib/module/animation/functions/interpolate.js +2 -2
  69. package/lib/module/animation/functions/interpolate.js.map +1 -1
  70. package/lib/module/animation/functions/interpolateColors.js +1 -1
  71. package/lib/module/animation/functions/interpolateColors.js.map +1 -1
  72. package/lib/module/animation/functions/interpolatePaths.js +44 -11
  73. package/lib/module/animation/functions/interpolatePaths.js.map +1 -1
  74. package/lib/module/mock/index.js +108 -0
  75. package/lib/module/mock/index.js.map +1 -0
  76. package/lib/module/skia/core/Data.js +32 -41
  77. package/lib/module/skia/core/Data.js.map +1 -1
  78. package/lib/module/skia/core/Typeface.js +1 -1
  79. package/lib/module/skia/core/Typeface.js.map +1 -1
  80. package/lib/module/skia/types/Font/Font.js.map +1 -1
  81. package/lib/module/skia/types/Image/Image.js +5 -5
  82. package/lib/module/skia/types/Image/Image.js.map +1 -1
  83. package/lib/module/skia/types/Paint/Paint.js +3 -3
  84. package/lib/module/skia/types/Paint/Paint.js.map +1 -1
  85. package/lib/module/skia/types/Path/Path.js +0 -1
  86. package/lib/module/skia/types/Path/Path.js.map +1 -1
  87. package/lib/module/skia/types/Shader/Shader.js +1 -7
  88. package/lib/module/skia/types/Shader/Shader.js.map +1 -1
  89. package/lib/module/skia/web/JsiSkCanvas.js +1 -1
  90. package/lib/module/skia/web/JsiSkCanvas.js.map +1 -1
  91. package/lib/module/skia/web/JsiSkFont.js +6 -0
  92. package/lib/module/skia/web/JsiSkFont.js.map +1 -1
  93. package/lib/module/skia/web/JsiSkFontMgr.js +0 -2
  94. package/lib/module/skia/web/JsiSkFontMgr.js.map +1 -1
  95. package/lib/module/skia/web/JsiSkImage.js +10 -7
  96. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  97. package/lib/module/skia/web/{JsiImageFilterFactory.js → JsiSkImageFilterFactory.js} +1 -1
  98. package/lib/module/skia/web/JsiSkImageFilterFactory.js.map +1 -0
  99. package/lib/module/skia/web/JsiSkPaint.js.map +1 -1
  100. package/lib/module/skia/web/JsiSkPath.js +65 -52
  101. package/lib/module/skia/web/JsiSkPath.js.map +1 -1
  102. package/lib/module/skia/web/JsiSkShaderFactory.js +1 -1
  103. package/lib/module/skia/web/JsiSkShaderFactory.js.map +1 -1
  104. package/lib/module/skia/web/JsiSkVerticesFactory.js +24 -1
  105. package/lib/module/skia/web/JsiSkVerticesFactory.js.map +1 -1
  106. package/lib/module/skia/web/JsiSkia.js +1 -1
  107. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  108. package/lib/module/values/api.js +0 -4
  109. package/lib/module/values/api.js.map +1 -1
  110. package/lib/module/values/api.web.js +1 -1
  111. package/lib/module/values/api.web.js.map +1 -1
  112. package/lib/module/values/hooks/index.js +1 -1
  113. package/lib/module/values/hooks/index.js.map +1 -1
  114. package/lib/module/values/hooks/useComputedValue.js +18 -0
  115. package/lib/module/values/hooks/useComputedValue.js.map +1 -0
  116. package/lib/module/values/web/{RNSkDerivedValue.js → RNSkComputedValue.js} +2 -2
  117. package/lib/module/values/web/RNSkComputedValue.js.map +1 -0
  118. package/lib/module/values/web/api.js +3 -3
  119. package/lib/module/values/web/api.js.map +1 -1
  120. package/lib/module/views/SkiaView.web.js +15 -15
  121. package/lib/module/views/SkiaView.web.js.map +1 -1
  122. package/lib/module/web/index.js +3 -1
  123. package/lib/module/web/index.js.map +1 -1
  124. package/lib/typescript/jestSetup.d.ts +1 -0
  125. package/lib/typescript/src/animation/functions/interpolate.d.ts +6 -0
  126. package/lib/typescript/src/animation/functions/interpolatePaths.d.ts +3 -1
  127. package/lib/typescript/src/mock/index.d.ts +16 -0
  128. package/lib/typescript/src/renderer/Canvas.d.ts +1 -1
  129. package/lib/typescript/src/skia/core/Data.d.ts +3 -3
  130. package/lib/typescript/src/skia/types/Font/Font.d.ts +6 -0
  131. package/lib/typescript/src/skia/types/Image/Image.d.ts +5 -5
  132. package/lib/typescript/src/skia/types/Paint/Paint.d.ts +3 -3
  133. package/lib/typescript/src/skia/types/Path/Path.d.ts +1 -2
  134. package/lib/typescript/src/skia/types/RuntimeEffect/RuntimeEffect.d.ts +1 -0
  135. package/lib/typescript/src/skia/types/Skia.d.ts +1 -1
  136. package/lib/typescript/src/skia/web/JsiSkFont.d.ts +1 -0
  137. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +3 -3
  138. package/lib/typescript/src/skia/web/{JsiImageFilterFactory.d.ts → JsiSkImageFilterFactory.d.ts} +0 -0
  139. package/lib/typescript/src/skia/web/JsiSkPaint.d.ts +1 -1
  140. package/lib/typescript/src/values/api.d.ts +0 -1
  141. package/lib/typescript/src/values/api.web.d.ts +1 -1
  142. package/lib/typescript/src/values/hooks/index.d.ts +1 -1
  143. package/lib/typescript/src/values/hooks/{useDerivedValue.d.ts → useComputedValue.d.ts} +2 -1
  144. package/lib/typescript/src/values/types.d.ts +2 -2
  145. package/lib/typescript/src/values/web/{RNSkDerivedValue.d.ts → RNSkComputedValue.d.ts} +1 -1
  146. package/package.json +4 -3
  147. package/src/animation/functions/interpolate.ts +4 -2
  148. package/src/animation/functions/interpolateColors.ts +1 -1
  149. package/src/animation/functions/interpolatePaths.ts +59 -10
  150. package/src/mock/index.ts +110 -0
  151. package/src/skia/core/Data.ts +67 -50
  152. package/src/skia/core/Typeface.ts +6 -1
  153. package/src/skia/types/Font/Font.ts +7 -0
  154. package/src/skia/types/Image/Image.ts +4 -4
  155. package/src/skia/types/Paint/Paint.ts +1 -1
  156. package/src/skia/types/Path/Path.ts +0 -1
  157. package/src/skia/types/RuntimeEffect/RuntimeEffect.ts +1 -0
  158. package/src/skia/types/Shader/Shader.ts +3 -5
  159. package/src/skia/types/Skia.ts +1 -1
  160. package/src/skia/web/JsiSkCanvas.ts +2 -2
  161. package/src/skia/web/JsiSkFont.ts +6 -0
  162. package/src/skia/web/JsiSkFontMgr.ts +0 -4
  163. package/src/skia/web/JsiSkImage.ts +37 -16
  164. package/src/skia/web/{JsiImageFilterFactory.ts → JsiSkImageFilterFactory.ts} +0 -0
  165. package/src/skia/web/JsiSkPaint.ts +1 -1
  166. package/src/skia/web/JsiSkPath.ts +64 -47
  167. package/src/skia/web/JsiSkShaderFactory.ts +4 -1
  168. package/src/skia/web/JsiSkVerticesFactory.ts +15 -1
  169. package/src/skia/web/JsiSkia.ts +2 -2
  170. package/src/values/api.ts +0 -2
  171. package/src/values/api.web.ts +1 -1
  172. package/src/values/hooks/index.ts +1 -1
  173. package/src/values/hooks/useComputedValue.ts +23 -0
  174. package/src/values/types.ts +2 -2
  175. package/src/values/web/{RNSkDerivedValue.ts → RNSkComputedValue.ts} +1 -1
  176. package/src/values/web/api.ts +3 -3
  177. package/src/views/SkiaView.web.tsx +29 -28
  178. package/src/web/index.ts +3 -1
  179. package/lib/commonjs/skia/web/JsiImageFilterFactory.js.map +0 -1
  180. package/lib/commonjs/values/hooks/useDerivedValue.js +0 -25
  181. package/lib/commonjs/values/hooks/useDerivedValue.js.map +0 -1
  182. package/lib/commonjs/values/web/RNSkDerivedValue.js.map +0 -1
  183. package/lib/module/skia/web/JsiImageFilterFactory.js.map +0 -1
  184. package/lib/module/values/hooks/useDerivedValue.js +0 -14
  185. package/lib/module/values/hooks/useDerivedValue.js.map +0 -1
  186. package/lib/module/values/web/RNSkDerivedValue.js.map +0 -1
  187. package/src/values/hooks/useDerivedValue.ts +0 -18
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "React Native Skia",
7
- "version": "0.1.132",
7
+ "version": "0.1.136",
8
8
  "description": "High-performance React Native Graphics using Skia",
9
9
  "main": "lib/module/index.js",
10
10
  "files": [
@@ -21,6 +21,7 @@
21
21
  "android/src/**",
22
22
  "libs/android/**",
23
23
  "index.js",
24
+ "jestSetup.js",
24
25
  "cpp/**/*.{h,cpp}",
25
26
  "ios",
26
27
  "libs/ios/libskia.xcframework",
@@ -70,8 +71,8 @@
70
71
  "@types/react-reconciler": "^0.26.4",
71
72
  "eslint": "7.32.0",
72
73
  "eslint-config-react-native-wcandillon": "^3.7.2",
74
+ "eslint-plugin-reanimated": "^1.2.6",
73
75
  "jest": "^27.4.3",
74
- "patch-package": "^6.4.7",
75
76
  "react": "17.0.2",
76
77
  "react-native": "0.66.2",
77
78
  "react-native-builder-bob": "^0.18.2",
@@ -79,7 +80,7 @@
79
80
  "typescript": "^4.6.4"
80
81
  },
81
82
  "dependencies": {
82
- "canvaskit-wasm": "^0.34.0",
83
+ "canvaskit-wasm": "^0.35.0",
83
84
  "react-reconciler": "^0.26.2"
84
85
  },
85
86
  "eslintIgnore": [
@@ -60,7 +60,9 @@ function isExtrapolate(value: string): value is Extrapolate {
60
60
 
61
61
  // validates extrapolations type
62
62
  // if type is correct, converts it to ExtrapolationConfig
63
- function validateType(type: ExtrapolationType): RequiredExtrapolationConfig {
63
+ export function validateInterpolationOptions(
64
+ type: ExtrapolationType
65
+ ): RequiredExtrapolationConfig {
64
66
  // initialize extrapolationConfig with default extrapolation
65
67
  const extrapolationConfig: RequiredExtrapolationConfig = {
66
68
  extrapolateLeft: Extrapolate.EXTEND,
@@ -151,7 +153,7 @@ export function interpolate(
151
153
  );
152
154
  }
153
155
 
154
- const extrapolationConfig = validateType(type);
156
+ const extrapolationConfig = validateInterpolationOptions(type);
155
157
  const { length } = input;
156
158
  const narrowedInput: InterpolationNarrowedInput = {
157
159
  leftEdgeInput: input[0],
@@ -1,4 +1,4 @@
1
- import { mix } from "../../renderer";
1
+ import { mix } from "../../renderer/processors/math";
2
2
  import type { Color, SkColor } from "../../skia";
3
3
  import { Skia } from "../../skia";
4
4
 
@@ -1,10 +1,26 @@
1
1
  import type { SkPath } from "../../skia/types";
2
+ import { exhaustiveCheck } from "../../renderer/typeddash";
3
+
4
+ import type { ExtrapolationType } from "./interpolate";
5
+ import { validateInterpolationOptions, Extrapolate } from "./interpolate";
6
+
7
+ const lerp = (
8
+ value: number,
9
+ from: number,
10
+ to: number,
11
+ p1: SkPath,
12
+ p2: SkPath
13
+ ) => {
14
+ const t = (value - from) / (to - from);
15
+ return p2.interpolate(p1, t)!;
16
+ };
2
17
 
3
18
  /**
4
19
  * Maps an input value within a range to an output path within a path range.
5
20
  * @param value - The input value.
6
21
  * @param inputRange - The range of the input value.
7
22
  * @param outputRange - The range of the output path.
23
+ * @param options - Extrapolation options
8
24
  * @returns The output path.
9
25
  * @example <caption>Map a value between 0 and 1 to a path between two paths.</caption>
10
26
  * const path1 = new Path();
@@ -18,21 +34,54 @@ import type { SkPath } from "../../skia/types";
18
34
  export const interpolatePaths = (
19
35
  value: number,
20
36
  input: number[],
21
- outputRange: SkPath[]
37
+ outputRange: SkPath[],
38
+ options?: ExtrapolationType
22
39
  ) => {
40
+ const extrapolation = validateInterpolationOptions(options);
41
+ if (value < input[0]) {
42
+ switch (extrapolation.extrapolateLeft) {
43
+ case Extrapolate.CLAMP:
44
+ return outputRange[0];
45
+ case Extrapolate.EXTEND:
46
+ return lerp(value, input[0], input[1], outputRange[0], outputRange[1]);
47
+ case Extrapolate.IDENTITY:
48
+ throw new Error(
49
+ "Identity is not a supported extrapolation type for interpolatePaths()"
50
+ );
51
+ default:
52
+ exhaustiveCheck(extrapolation.extrapolateLeft);
53
+ }
54
+ } else if (value > input[input.length - 1]) {
55
+ switch (extrapolation.extrapolateRight) {
56
+ case Extrapolate.CLAMP:
57
+ return outputRange[outputRange.length - 1];
58
+ case Extrapolate.EXTEND:
59
+ return lerp(
60
+ value,
61
+ input[input.length - 2],
62
+ input[input.length - 1],
63
+ outputRange[input.length - 2],
64
+ outputRange[input.length - 1]
65
+ );
66
+ case Extrapolate.IDENTITY:
67
+ throw new Error(
68
+ "Identity is not a supported extrapolation type for interpolatePaths()"
69
+ );
70
+ default:
71
+ exhaustiveCheck(extrapolation.extrapolateRight);
72
+ }
73
+ }
23
74
  let i = 0;
24
75
  for (; i <= input.length - 1; i++) {
25
76
  if (value >= input[i] && value <= input[i + 1]) {
26
77
  break;
27
78
  }
28
- if (i === input.length - 1) {
29
- if (value < input[0]) {
30
- return outputRange[0];
31
- } else {
32
- return outputRange[i];
33
- }
34
- }
35
79
  }
36
- const t = (value - input[i]) / (input[i + 1] - input[i]);
37
- return outputRange[i + 1].interpolate(outputRange[i], t)!;
80
+ return lerp(
81
+ value,
82
+ input[i],
83
+ input[i + 1],
84
+ outputRange[i],
85
+ outputRange[i + 1]
86
+ );
38
87
  };
@@ -0,0 +1,110 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ import type { Color, Skia as SkiaApi, SkRect, Vector } from "../skia/types";
4
+ import * as Values from "../values/web";
5
+ import * as ValuesHooks from "../values/hooks";
6
+ import * as BaseSkia from "../skia/types";
7
+ import type * as SkiaExports from "../skia";
8
+ import type * as ExternalExports from "../external";
9
+ import type * as ValueExports from "../values";
10
+ import type * as AnimationExports from "../animation";
11
+ import { useSharedValueEffect } from "../external/reanimated/useSharedValueEffect";
12
+ import * as timingFunctions from "../animation/timing";
13
+ import * as springFunctions from "../animation/spring";
14
+ import * as decayFunctions from "../animation/decay";
15
+ import * as interpolateFn from "../animation/functions/interpolate";
16
+ import * as interpolatePathFn from "../animation/functions/interpolatePaths";
17
+ import * as interpolateVectorFn from "../animation/functions/interpolateVector";
18
+ import { ShaderLib } from "../renderer/components/shaders/ShaderLib";
19
+
20
+ class Stub {
21
+ constructor() {
22
+ return new Proxy(() => {}, {
23
+ get: () => new Stub(),
24
+ apply: () => new Stub(),
25
+ set: () => true,
26
+ });
27
+ }
28
+ }
29
+
30
+ const Noop: () => any = () => {};
31
+
32
+ export const Skia: SkiaApi = new Stub() as any;
33
+
34
+ export const vec = (x?: number, y?: number) => ({ x: x ?? 0, y: y ?? x ?? 0 });
35
+
36
+ export const Mock: typeof SkiaExports &
37
+ typeof ExternalExports &
38
+ typeof ValueExports &
39
+ typeof AnimationExports & {
40
+ createDrawing: () => any;
41
+ createDeclaration: () => any;
42
+ ShaderLib: typeof ShaderLib;
43
+ } = {
44
+ // SkiaExports
45
+ // 1. Skia API. BaseSkia contains the enums, and functions like isPaint etc
46
+ Skia,
47
+ ...BaseSkia,
48
+ // 2. Hooks
49
+ useDataCollection: Noop,
50
+ useRawData: Noop,
51
+ useData: Noop,
52
+ useFont: Noop,
53
+ useTypeface: Noop,
54
+ useImage: Noop,
55
+ usePath: Noop,
56
+ useSVG: Noop,
57
+ useTextPath: Noop,
58
+ usePaint: Noop,
59
+ usePicture: Noop,
60
+ useSvgPath: Noop,
61
+ // 3. Point/Rect/Transform utilities
62
+ vec,
63
+ rect: (x: number, y: number, width: number, height: number) => ({
64
+ x,
65
+ y,
66
+ width,
67
+ height,
68
+ }),
69
+ rrect: (r: SkRect, rx: number, ry: number) => ({
70
+ rect: r,
71
+ rx,
72
+ ry,
73
+ }),
74
+ point: vec,
75
+ add: (a: Vector, b: Vector) => vec(a.x + b.x, a.y + b.y),
76
+ sub: (a: Vector, b: Vector) => vec(a.x - b.x, a.y - b.y),
77
+ neg: (a: Vector) => vec(-a.x, -a.y),
78
+ dist: (a: Vector, b: Vector) => Math.hypot(a.x - b.x, a.y - b.y),
79
+ translate: ({ x, y }: Vector) =>
80
+ [{ translateX: x }, { translateY: y }] as const,
81
+
82
+ bounds: Noop,
83
+ topLeft: Noop,
84
+ topRight: Noop,
85
+ bottomLeft: Noop,
86
+ bottomRight: Noop,
87
+ center: Noop,
88
+ processTransform2d: Noop,
89
+ // ExternalExports
90
+ useSharedValueEffect,
91
+ // ValueExports
92
+ ...Values,
93
+ ...ValuesHooks,
94
+ // Animations
95
+ ...timingFunctions,
96
+ ...springFunctions,
97
+ ...decayFunctions,
98
+ ...interpolateFn,
99
+ ...interpolatePathFn,
100
+ ...interpolateVectorFn,
101
+ interpolateColors: (
102
+ _value: number,
103
+ _inputRange: number[],
104
+ _outputRange: Color[]
105
+ ) => Float32Array.of(0, 0, 0, 0),
106
+ mixColors: (_v: number, _x: Color, _y: Color) => Float32Array.of(0, 0, 0, 0),
107
+ ShaderLib,
108
+ createDrawing: Noop,
109
+ createDeclaration: Noop,
110
+ };
@@ -11,71 +11,88 @@ const resolveAsset = (source: ReturnType<typeof require>) => {
11
11
  : Image.resolveAssetSource(source).uri;
12
12
  };
13
13
 
14
- export const useDataCollection = <T>(
15
- sources: DataSource[],
16
- factory: (data: SkData[]) => T,
17
- deps: DependencyList = []
14
+ const factoryWrapper = <T>(
15
+ data2: SkData,
16
+ factory: (data: SkData) => T,
17
+ onError?: (err: Error) => void
18
18
  ) => {
19
- const [data, setData] = useState<T | null>(null);
20
- useEffect(() => {
21
- const bytesOrURIs = sources.map((source) => {
22
- if (source instanceof Uint8Array) {
23
- return source;
24
- }
25
- return typeof source === "string" ? source : resolveAsset(source);
26
- });
27
- Promise.all(
28
- bytesOrURIs.map((bytesOrURI) =>
29
- bytesOrURI instanceof Uint8Array
30
- ? Skia.Data.fromBytes(bytesOrURI)
31
- : Skia.Data.fromURI(bytesOrURI)
32
- )
33
- ).then((d) => setData(factory(d)));
34
- // eslint-disable-next-line react-hooks/exhaustive-deps
35
- }, deps);
36
- return data;
19
+ const factoryResult = factory(data2);
20
+ if (factoryResult === null) {
21
+ onError && onError(new Error("Could not load data"));
22
+ return null;
23
+ } else {
24
+ return factoryResult;
25
+ }
37
26
  };
38
27
 
39
- export const useRawData = <T>(
40
- source: DataSource | null | undefined,
28
+ const loadDataCollection = <T>(
29
+ sources: DataSource[],
30
+ factory: (data: SkData) => T,
31
+ onError?: (err: Error) => void
32
+ ): Promise<(T | null)[]> =>
33
+ Promise.all(sources.map((source) => loadData(source, factory, onError)));
34
+
35
+ const loadData = <T>(
36
+ source: DataSource,
41
37
  factory: (data: SkData) => T,
42
38
  onError?: (err: Error) => void
39
+ ): Promise<T | null> => {
40
+ if (source instanceof Uint8Array) {
41
+ return new Promise((resolve) =>
42
+ resolve(factoryWrapper(Skia.Data.fromBytes(source), factory, onError))
43
+ );
44
+ } else {
45
+ const uri = typeof source === "string" ? source : resolveAsset(source);
46
+ return Skia.Data.fromURI(uri).then((d) =>
47
+ factoryWrapper(d, factory, onError)
48
+ );
49
+ }
50
+ };
51
+
52
+ type Source = DataSource | null | undefined;
53
+
54
+ const useLoading = <T>(
55
+ source: Source,
56
+ loader: () => Promise<T | null>,
57
+ deps: DependencyList = []
43
58
  ) => {
44
59
  const [data, setData] = useState<T | null>(null);
45
- const prevSourceRef = useRef<DataSource | null | undefined>();
60
+ const prevSourceRef = useRef<Source>();
46
61
  useEffect(() => {
47
- // Track to avoid re-fetching the same data
48
62
  if (prevSourceRef.current !== source) {
49
63
  prevSourceRef.current = source;
50
- if (source !== null && source !== undefined) {
51
- const factoryWrapper = (data2: SkData) => {
52
- const factoryResult = factory(data2);
53
- if (factoryResult === null) {
54
- onError && onError(new Error("Could not load data"));
55
- setData(null);
56
- } else {
57
- setData(factoryResult);
58
- }
59
- };
60
- if (source instanceof Uint8Array) {
61
- factoryWrapper(Skia.Data.fromBytes(source));
62
- } else {
63
- const uri =
64
- typeof source === "string" ? source : resolveAsset(source);
65
- Skia.Data.fromURI(uri).then((d) => factoryWrapper(d));
66
- }
67
- } else {
68
- // new source is null or undefined -> remove cached data
69
- setData(null);
70
- }
64
+ loader().then(setData);
65
+ } else {
66
+ setData(null);
71
67
  }
72
- }, [factory, onError, source]);
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, deps);
73
70
  return data;
74
71
  };
75
72
 
73
+ export const useDataCollection = <T>(
74
+ sources: DataSource[],
75
+ factory: (data: SkData) => T,
76
+ onError?: (err: Error) => void,
77
+ deps?: DependencyList
78
+ ) =>
79
+ useLoading(
80
+ sources,
81
+ () => loadDataCollection(sources, factory, onError),
82
+ deps
83
+ );
84
+
85
+ export const useRawData = <T>(
86
+ source: DataSource | null | undefined,
87
+ factory: (data: SkData) => T,
88
+ onError?: (err: Error) => void,
89
+ deps?: DependencyList
90
+ ) => useLoading(source, () => loadData(source, factory, onError), deps);
91
+
76
92
  const identity = (data: SkData) => data;
77
93
 
78
94
  export const useData = (
79
95
  source: DataSource | null | undefined,
80
- onError?: (err: Error) => void
81
- ) => useRawData(source, identity, onError);
96
+ onError?: (err: Error) => void,
97
+ deps?: DependencyList
98
+ ) => useRawData(source, identity, onError, deps);
@@ -8,4 +8,9 @@ import { useRawData } from "./Data";
8
8
  export const useTypeface = (
9
9
  source: DataSource | null | undefined,
10
10
  onError?: (err: Error) => void
11
- ) => useRawData(source, Skia.Typeface.MakeFreeTypeFaceFromData, onError);
11
+ ) =>
12
+ useRawData(
13
+ source,
14
+ Skia.Typeface.MakeFreeTypeFaceFromData.bind(Skia.Typeface),
15
+ onError
16
+ );
@@ -12,6 +12,13 @@ export interface FontMetrics {
12
12
  }
13
13
 
14
14
  export interface SkFont extends SkJSIInstance<"Font"> {
15
+ /**
16
+ * Retrieves the total width of the provided text
17
+ * @param text
18
+ * @param paint
19
+ */
20
+ getTextWidth(text: string, paint?: SkPaint): number;
21
+
15
22
  /**
16
23
  * Retrieves the advanceX measurements for each glyph.
17
24
  * If paint is not null, its stroking, PathEffect, and MaskFilter fields are respected.
@@ -4,8 +4,8 @@ import type { TileMode } from "../ImageFilter";
4
4
  import type { SkShader } from "../Shader";
5
5
 
6
6
  export enum FilterMode {
7
- Linear,
8
7
  Nearest,
8
+ Linear,
9
9
  }
10
10
 
11
11
  export enum MipmapMode {
@@ -15,9 +15,9 @@ export enum MipmapMode {
15
15
  }
16
16
 
17
17
  export enum ImageFormat {
18
- PNG,
19
- JPEG,
20
- WEBP,
18
+ JPEG = 3,
19
+ PNG = 4,
20
+ WEBP = 6,
21
21
  }
22
22
 
23
23
  export interface SkImage extends SkJSIInstance<"Image"> {
@@ -20,9 +20,9 @@ export enum StrokeCap {
20
20
  }
21
21
 
22
22
  export enum StrokeJoin {
23
- Bevel,
24
23
  Miter,
25
24
  Round,
25
+ Bevel,
26
26
  }
27
27
 
28
28
  export const isPaint = (obj: SkJSIInstance<string> | null): obj is SkPaint =>
@@ -44,7 +44,6 @@ export enum PathVerb {
44
44
  Conic,
45
45
  Cubic,
46
46
  Close,
47
- Done,
48
47
  }
49
48
 
50
49
  export type PathCommand = number[];
@@ -7,6 +7,7 @@ export interface SkSLUniform {
7
7
  rows: number;
8
8
  /** The index into the uniforms array that this uniform begins. */
9
9
  slot: number;
10
+ isInteger: boolean;
10
11
  }
11
12
 
12
13
  export interface SkRuntimeShaderBuilder
@@ -40,11 +40,9 @@ export const processUniforms = (
40
40
  if (value === undefined) {
41
41
  throw new Error(`No value specified for uniform ${name}`);
42
42
  }
43
- let result: number | readonly number[];
44
- if (Array.isArray(value)) {
45
- result = value.flatMap(processValue);
46
- }
47
- result = processValue(value as UniformValue);
43
+ const result = Array.isArray(value)
44
+ ? value.flatMap(processValue)
45
+ : processValue(value as UniformValue);
48
46
  builder?.setUniform(name, typeof result === "number" ? [result] : result);
49
47
  return result;
50
48
  });
@@ -48,7 +48,7 @@ export interface Skia {
48
48
  PictureRecorder: () => SkPictureRecorder;
49
49
  Picture: PictureFactory;
50
50
  Path: PathFactory;
51
- Matrix: (matrix?: number[]) => SkMatrix;
51
+ Matrix: (matrix?: readonly number[]) => SkMatrix;
52
52
  ColorFilter: ColorFilterFactory;
53
53
  Font: (typeface?: SkTypeface, size?: number) => SkFont;
54
54
  Typeface: TypefaceFactory;
@@ -1,4 +1,4 @@
1
- import type { Canvas, Image, Paint, CanvasKit } from "canvaskit-wasm";
1
+ import type { Canvas, Image, CanvasKit, Paint } from "canvaskit-wasm";
2
2
 
3
3
  import type {
4
4
  BlendMode,
@@ -250,7 +250,7 @@ export class JsiSkCanvas
250
250
  ) {
251
251
  this.ref.drawGlyphs(
252
252
  glyphs,
253
- toValue(positions),
253
+ positions.map((p) => [p.x, p.y]).flat(),
254
254
  x,
255
255
  y,
256
256
  toValue(font),
@@ -27,6 +27,12 @@ Clients should use "Font.getGlyphWidths" instead (the latter does no shaping)`
27
27
  return new JsiSkRect(this.CanvasKit, this.CanvasKit.XYWHRect(0, 0, 0, 0));
28
28
  }
29
29
 
30
+ getTextWidth(text: string, paint?: SkPaint | undefined) {
31
+ const ids = this.getGlyphIDs(text);
32
+ const widths = this.getGlyphWidths(ids, paint);
33
+ return widths.reduce((a, b) => a + b, 0);
34
+ }
35
+
30
36
  getMetrics() {
31
37
  const result = this.ref.getMetrics();
32
38
  return {
@@ -9,10 +9,6 @@ export class JsiSkFontMgr
9
9
  implements SkFontMgr
10
10
  {
11
11
  constructor(CanvasKit: CanvasKit) {
12
- console.warn(
13
- // eslint-disable-next-line max-len
14
- "Using system fonts is deprecated. Please use the font property instead: https://shopify.github.io/react-native-skia/docs/text/fonts"
15
- );
16
12
  super(CanvasKit, null, "FontMgr");
17
13
  }
18
14
 
@@ -1,3 +1,4 @@
1
+ /*global btoa, atob*/
1
2
  import type { CanvasKit, Image } from "canvaskit-wasm";
2
3
 
3
4
  import type {
@@ -10,7 +11,8 @@ import type {
10
11
  TileMode,
11
12
  } from "../types";
12
13
 
13
- import { ckEnum, HostObject } from "./Host";
14
+ import { ckEnum, HostObject, toValue } from "./Host";
15
+ import { JsiSkShader } from "./JsiSkShader";
14
16
 
15
17
  export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
16
18
  constructor(CanvasKit: CanvasKit, ref: Image) {
@@ -26,26 +28,44 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
26
28
  }
27
29
 
28
30
  makeShaderOptions(
29
- _tx: TileMode,
30
- _ty: TileMode,
31
- _fm: FilterMode,
32
- _mm: MipmapMode,
33
- _localMatrix?: SkMatrix
31
+ tx: TileMode,
32
+ ty: TileMode,
33
+ fm: FilterMode,
34
+ mm: MipmapMode,
35
+ localMatrix?: SkMatrix
34
36
  ): SkShader {
35
- throw new Error("Not implemented yet");
37
+ return new JsiSkShader(
38
+ this.CanvasKit,
39
+ this.ref.makeShaderOptions(
40
+ ckEnum(tx),
41
+ ckEnum(ty),
42
+ ckEnum(fm),
43
+ ckEnum(mm),
44
+ localMatrix ? toValue(localMatrix) : undefined
45
+ )
46
+ );
36
47
  }
37
48
 
38
49
  makeShaderCubic(
39
- _tx: TileMode,
40
- _ty: TileMode,
41
- _B: number,
42
- _C: number,
43
- _localMatrix?: SkMatrix
50
+ tx: TileMode,
51
+ ty: TileMode,
52
+ B: number,
53
+ C: number,
54
+ localMatrix?: SkMatrix
44
55
  ): SkShader {
45
- throw new Error("Not implemented yet");
56
+ return new JsiSkShader(
57
+ this.CanvasKit,
58
+ this.ref.makeShaderCubic(
59
+ ckEnum(tx),
60
+ ckEnum(ty),
61
+ B,
62
+ C,
63
+ localMatrix ? toValue(localMatrix) : undefined
64
+ )
65
+ );
46
66
  }
47
67
 
48
- encodeToBytes(fmt?: ImageFormat, quality?: number): Uint8Array {
68
+ encodeToBytes(fmt?: ImageFormat, quality?: number) {
49
69
  let result: Uint8Array | null;
50
70
  if (fmt && quality) {
51
71
  result = this.ref.encodeToBytes(ckEnum(fmt), quality);
@@ -60,7 +80,8 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
60
80
  return result;
61
81
  }
62
82
 
63
- encodeToBase64(_fmt?: ImageFormat, _quality?: number): string {
64
- throw new Error("Not implemented yet");
83
+ encodeToBase64(fmt?: ImageFormat, quality?: number) {
84
+ const bytes = this.encodeToBytes(fmt, quality);
85
+ return btoa(String.fromCharCode(...bytes));
65
86
  }
66
87
  }
@@ -1,6 +1,7 @@
1
1
  import type { CanvasKit, Paint } from "canvaskit-wasm";
2
2
 
3
3
  import type {
4
+ StrokeJoin,
4
5
  BlendMode,
5
6
  SkColor,
6
7
  SkColorFilter,
@@ -8,7 +9,6 @@ import type {
8
9
  SkPaint,
9
10
  SkShader,
10
11
  StrokeCap,
11
- StrokeJoin,
12
12
  PaintStyle,
13
13
  SkMaskFilter,
14
14
  SkPathEffect,