@shopify/react-native-skia 0.1.185 → 0.1.186

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. package/android/build.gradle +2 -1
  2. package/android/cpp/jni/JniPlatformContext.cpp +92 -3
  3. package/android/cpp/jni/include/JniPlatformContext.h +4 -0
  4. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +8 -0
  5. package/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java +17 -1
  6. package/android/src/main/java/com/shopify/reactnative/skia/ViewScreenshotService.java +180 -0
  7. package/cpp/api/JsiSkFont.h +1 -1
  8. package/cpp/api/JsiSkHostObjects.h +1 -1
  9. package/cpp/api/JsiSkImage.h +1 -1
  10. package/cpp/api/JsiSkImageFactory.h +29 -0
  11. package/cpp/api/JsiSkPaint.h +7 -7
  12. package/cpp/api/JsiSkPathFactory.h +1 -1
  13. package/cpp/api/JsiSkPicture.h +2 -2
  14. package/cpp/api/JsiSkRuntimeEffect.h +3 -3
  15. package/cpp/api/JsiSkSVG.h +12 -2
  16. package/cpp/api/JsiSkShader.h +1 -1
  17. package/cpp/api/JsiSkSurface.h +3 -3
  18. package/cpp/api/JsiSkSurfaceFactory.h +1 -1
  19. package/cpp/api/JsiSkTypeface.h +1 -1
  20. package/cpp/rnskia/RNSkAnimation.h +3 -3
  21. package/cpp/rnskia/RNSkDomView.h +9 -9
  22. package/cpp/rnskia/RNSkInfoParameter.h +2 -2
  23. package/cpp/rnskia/RNSkJsView.h +8 -8
  24. package/cpp/rnskia/RNSkJsiViewApi.h +5 -5
  25. package/cpp/rnskia/RNSkPictureView.h +8 -8
  26. package/cpp/rnskia/RNSkPlatformContext.h +32 -3
  27. package/cpp/rnskia/RNSkValueApi.h +5 -5
  28. package/cpp/rnskia/RNSkView.h +6 -6
  29. package/cpp/rnskia/dom/base/ConcatablePaint.h +6 -6
  30. package/cpp/rnskia/dom/base/Declaration.h +1 -1
  31. package/cpp/rnskia/dom/base/DeclarationContext.h +7 -7
  32. package/cpp/rnskia/dom/base/DrawingContext.h +3 -3
  33. package/cpp/rnskia/dom/base/NodePropsContainer.h +1 -1
  34. package/cpp/rnskia/dom/nodes/JsiBlurMaskNode.h +1 -1
  35. package/cpp/rnskia/dom/nodes/JsiColorFilterNodes.h +1 -1
  36. package/cpp/rnskia/dom/nodes/JsiGlyphsNode.h +8 -7
  37. package/cpp/rnskia/dom/nodes/JsiImageFilterNodes.h +1 -1
  38. package/cpp/rnskia/dom/nodes/JsiImageNode.h +5 -2
  39. package/cpp/rnskia/dom/nodes/JsiImageSvgNode.h +9 -9
  40. package/cpp/rnskia/dom/nodes/JsiPathEffectNodes.h +1 -1
  41. package/cpp/rnskia/dom/nodes/JsiPathNode.h +1 -1
  42. package/cpp/rnskia/dom/nodes/JsiPointsNode.h +1 -1
  43. package/cpp/rnskia/dom/nodes/JsiShaderNodes.h +7 -3
  44. package/cpp/rnskia/dom/nodes/JsiTextNode.h +5 -4
  45. package/cpp/rnskia/dom/nodes/JsiTextPathNode.h +3 -1
  46. package/cpp/rnskia/dom/props/BlendModeProp.h +1 -1
  47. package/cpp/rnskia/dom/props/CircleProp.h +1 -1
  48. package/cpp/rnskia/dom/props/ClipProp.h +1 -1
  49. package/cpp/rnskia/dom/props/FontProp.h +15 -10
  50. package/cpp/rnskia/dom/props/ImageProps.h +30 -16
  51. package/cpp/rnskia/dom/props/PaintProps.h +1 -1
  52. package/cpp/rnskia/dom/props/PathProp.h +1 -1
  53. package/cpp/rnskia/dom/props/PointProp.h +1 -1
  54. package/cpp/rnskia/dom/props/PointsProp.h +1 -1
  55. package/cpp/rnskia/dom/props/RRectProp.h +2 -2
  56. package/cpp/rnskia/dom/props/RadiusProp.h +1 -1
  57. package/cpp/rnskia/dom/props/RectProp.h +1 -1
  58. package/cpp/rnskia/dom/props/StrokeProps.h +1 -1
  59. package/cpp/rnskia/dom/props/SvgProp.h +18 -12
  60. package/cpp/rnskia/dom/props/TextBlobProp.h +60 -57
  61. package/cpp/rnskia/dom/props/TileModeProp.h +1 -1
  62. package/cpp/rnskia/dom/props/UniformsProp.h +1 -1
  63. package/cpp/rnskia/dom/props/VertexModeProp.h +1 -1
  64. package/cpp/rnskia/dom/props/VerticesProps.h +1 -1
  65. package/cpp/rnskia/values/RNSkClockValue.h +2 -2
  66. package/cpp/rnskia/values/RNSkComputedValue.h +1 -1
  67. package/cpp/rnskia/values/RNSkReadonlyValue.h +3 -3
  68. package/cpp/rnskia/values/RNSkValue.h +4 -4
  69. package/cpp/utils/RNSkMeasureTime.h +1 -1
  70. package/cpp/utils/RNSkTimingInfo.h +1 -1
  71. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +16 -13
  72. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +8 -0
  73. package/ios/RNSkia-iOS/SkiaManager.mm +20 -1
  74. package/ios/RNSkia-iOS/ViewScreenshotService.h +21 -0
  75. package/ios/RNSkia-iOS/ViewScreenshotService.mm +79 -0
  76. package/lib/commonjs/dom/nodes/drawings/ImageNode.js +22 -1
  77. package/lib/commonjs/dom/nodes/drawings/ImageNode.js.map +1 -1
  78. package/lib/commonjs/dom/nodes/drawings/ImageSVG.js +5 -0
  79. package/lib/commonjs/dom/nodes/drawings/ImageSVG.js.map +1 -1
  80. package/lib/commonjs/dom/nodes/drawings/Text.d.ts +2 -2
  81. package/lib/commonjs/dom/nodes/drawings/Text.js +13 -2
  82. package/lib/commonjs/dom/nodes/drawings/Text.js.map +1 -1
  83. package/lib/commonjs/dom/nodes/paint/Shaders.js +5 -0
  84. package/lib/commonjs/dom/nodes/paint/Shaders.js.map +1 -1
  85. package/lib/commonjs/dom/types/Drawings.d.ts +5 -5
  86. package/lib/commonjs/dom/types/Drawings.js.map +1 -1
  87. package/lib/commonjs/dom/types/Shaders.d.ts +1 -1
  88. package/lib/commonjs/dom/types/Shaders.js.map +1 -1
  89. package/lib/commonjs/mock/index.js +2 -1
  90. package/lib/commonjs/mock/index.js.map +1 -1
  91. package/lib/commonjs/skia/core/Image.d.ts +14 -2
  92. package/lib/commonjs/skia/core/Image.js +37 -1
  93. package/lib/commonjs/skia/core/Image.js.map +1 -1
  94. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +7 -0
  95. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  96. package/lib/commonjs/skia/types/SVG/SVG.d.ts +4 -1
  97. package/lib/commonjs/skia/types/SVG/SVG.js.map +1 -1
  98. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -1
  99. package/lib/commonjs/skia/web/JsiSkImageFactory.js +7 -0
  100. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  101. package/lib/module/dom/nodes/drawings/ImageNode.js +22 -1
  102. package/lib/module/dom/nodes/drawings/ImageNode.js.map +1 -1
  103. package/lib/module/dom/nodes/drawings/ImageSVG.js +5 -0
  104. package/lib/module/dom/nodes/drawings/ImageSVG.js.map +1 -1
  105. package/lib/module/dom/nodes/drawings/Text.d.ts +2 -2
  106. package/lib/module/dom/nodes/drawings/Text.js +13 -2
  107. package/lib/module/dom/nodes/drawings/Text.js.map +1 -1
  108. package/lib/module/dom/nodes/paint/Shaders.js +5 -0
  109. package/lib/module/dom/nodes/paint/Shaders.js.map +1 -1
  110. package/lib/module/dom/types/Drawings.d.ts +5 -5
  111. package/lib/module/dom/types/Drawings.js.map +1 -1
  112. package/lib/module/dom/types/Shaders.d.ts +1 -1
  113. package/lib/module/dom/types/Shaders.js.map +1 -1
  114. package/lib/module/mock/index.js +2 -1
  115. package/lib/module/mock/index.js.map +1 -1
  116. package/lib/module/skia/core/Image.d.ts +14 -2
  117. package/lib/module/skia/core/Image.js +32 -0
  118. package/lib/module/skia/core/Image.js.map +1 -1
  119. package/lib/module/skia/types/Image/ImageFactory.d.ts +7 -0
  120. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  121. package/lib/module/skia/types/SVG/SVG.d.ts +4 -1
  122. package/lib/module/skia/types/SVG/SVG.js.map +1 -1
  123. package/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -1
  124. package/lib/module/skia/web/JsiSkImageFactory.js +7 -0
  125. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  126. package/lib/typescript/src/dom/nodes/drawings/Text.d.ts +2 -2
  127. package/lib/typescript/src/dom/types/Drawings.d.ts +5 -5
  128. package/lib/typescript/src/dom/types/Shaders.d.ts +1 -1
  129. package/lib/typescript/src/skia/core/Image.d.ts +14 -2
  130. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +7 -0
  131. package/lib/typescript/src/skia/types/SVG/SVG.d.ts +4 -1
  132. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +2 -1
  133. package/package.json +2 -2
  134. package/scripts/install-npm.js +3 -2
  135. package/src/dom/nodes/drawings/ImageNode.ts +9 -1
  136. package/src/dom/nodes/drawings/ImageSVG.ts +3 -0
  137. package/src/dom/nodes/drawings/Text.ts +13 -3
  138. package/src/dom/nodes/paint/Shaders.ts +4 -0
  139. package/src/dom/types/Drawings.ts +5 -5
  140. package/src/dom/types/Shaders.ts +1 -1
  141. package/src/mock/index.ts +1 -0
  142. package/src/skia/core/Image.ts +43 -1
  143. package/src/skia/types/Image/ImageFactory.ts +8 -0
  144. package/src/skia/types/SVG/SVG.ts +4 -1
  145. package/src/skia/web/JsiSkImageFactory.ts +8 -1
@@ -15,6 +15,12 @@ export class ImageNode extends JsiDrawingNode<
15
15
 
16
16
  deriveProps() {
17
17
  const { image } = this.props;
18
+ if (!image) {
19
+ return {
20
+ src: { x: 0, y: 0, width: 0, height: 0 },
21
+ dst: { x: 0, y: 0, width: 0, height: 0 },
22
+ };
23
+ }
18
24
  const fit = this.props.fit ?? "contain";
19
25
  const rect = processRect(this.Skia, this.props);
20
26
  const { src, dst } = fitRects(
@@ -36,6 +42,8 @@ export class ImageNode extends JsiDrawingNode<
36
42
  throw new Error("ImageNode: src and dst are undefined");
37
43
  }
38
44
  const { src, dst } = this.derived;
39
- canvas.drawImageRect(image, src, dst, paint);
45
+ if (image) {
46
+ canvas.drawImageRect(image, src, dst, paint);
47
+ }
40
48
  }
41
49
  }
@@ -15,6 +15,9 @@ export class ImageSVGNode extends JsiDrawingNode<ImageSVGProps, null> {
15
15
 
16
16
  draw({ canvas }: DrawingContext) {
17
17
  const { svg } = this.props;
18
+ if (!svg) {
19
+ return;
20
+ }
18
21
  const { x, y, width, height } = processRect(this.Skia, this.props);
19
22
  canvas.save();
20
23
  canvas.translate(x, y);
@@ -22,11 +22,16 @@ export class TextNode extends JsiDrawingNode<TextProps, null> {
22
22
 
23
23
  draw({ canvas, paint }: DrawingContext) {
24
24
  const { text, x, y, font } = this.props;
25
- canvas.drawText(text, x, y, paint, font);
25
+ if (font) {
26
+ canvas.drawText(text, x, y, paint, font);
27
+ }
26
28
  }
27
29
  }
28
30
 
29
- export class TextPathNode extends JsiDrawingNode<TextPathProps, SkTextBlob> {
31
+ export class TextPathNode extends JsiDrawingNode<
32
+ TextPathProps,
33
+ SkTextBlob | null
34
+ > {
30
35
  constructor(ctx: NodeContext, props: TextPathProps) {
31
36
  super(ctx, NodeType.TextPath, props);
32
37
  }
@@ -34,6 +39,9 @@ export class TextPathNode extends JsiDrawingNode<TextPathProps, SkTextBlob> {
34
39
  deriveProps() {
35
40
  const path = processPath(this.Skia, this.props.path);
36
41
  const { font, initialOffset } = this.props;
42
+ if (!font) {
43
+ return null;
44
+ }
37
45
  let { text } = this.props;
38
46
  const ids = font.getGlyphIDs(text);
39
47
  const widths = font.getGlyphWidths(ids);
@@ -117,6 +125,8 @@ export class GlyphsNode extends JsiDrawingNode<GlyphsProps, ProcessedGlyphs> {
117
125
  }
118
126
  const { glyphs, positions } = this.derived;
119
127
  const { x, y, font } = this.props;
120
- canvas.drawGlyphs(glyphs, positions, x, y, font, paint);
128
+ if (font) {
129
+ canvas.drawGlyphs(glyphs, positions, x, y, font, paint);
130
+ }
121
131
  }
122
132
  }
@@ -60,6 +60,10 @@ export class ImageShaderNode extends ShaderDeclaration<ImageShaderProps> {
60
60
 
61
61
  decorate(ctx: DeclarationContext) {
62
62
  const { fit, image, tx, ty, fm, mm, ...imageShaderProps } = this.props;
63
+ if (!image) {
64
+ return;
65
+ }
66
+
63
67
  const rct = getRect(this.Skia, imageShaderProps);
64
68
  const m3 = this.Skia.Matrix();
65
69
  if (rct) {
@@ -35,7 +35,7 @@ export interface DrawingNodeProps extends GroupProps {
35
35
  export type ImageProps = DrawingNodeProps &
36
36
  RectDef & {
37
37
  fit?: Fit;
38
- image: SkImage;
38
+ image: SkImage | null;
39
39
  };
40
40
 
41
41
  export type CircleProps = CircleDef & DrawingNodeProps;
@@ -91,7 +91,7 @@ export interface VerticesProps extends DrawingNodeProps {
91
91
  }
92
92
 
93
93
  export type ImageSVGProps = RectDef & {
94
- svg: SkSVG;
94
+ svg: SkSVG | null;
95
95
  } & DrawingNodeProps;
96
96
 
97
97
  export interface PictureProps extends DrawingNodeProps {
@@ -109,14 +109,14 @@ export interface DiffRectProps extends DrawingNodeProps {
109
109
  }
110
110
 
111
111
  export interface TextProps extends DrawingNodeProps {
112
- font: SkFont;
112
+ font: SkFont | null;
113
113
  text: string;
114
114
  x: number;
115
115
  y: number;
116
116
  }
117
117
 
118
118
  export interface TextPathProps extends DrawingNodeProps {
119
- font: SkFont;
119
+ font: SkFont | null;
120
120
  text: string;
121
121
  path: PathDef;
122
122
  initialOffset: number;
@@ -134,7 +134,7 @@ export interface Glyph {
134
134
  }
135
135
 
136
136
  export interface GlyphsProps extends DrawingNodeProps {
137
- font: SkFont;
137
+ font: SkFont | null;
138
138
  x: number;
139
139
  y: number;
140
140
  glyphs: Glyph[];
@@ -30,7 +30,7 @@ export interface ImageShaderProps extends TransformProps, Partial<RectCtor> {
30
30
  mm: SkEnum<typeof MipmapMode>;
31
31
  fit: Fit;
32
32
  rect?: SkRect;
33
- image: SkImage;
33
+ image: SkImage | null;
34
34
  }
35
35
 
36
36
  export interface ColorProps {
package/src/mock/index.ts CHANGED
@@ -99,4 +99,5 @@ export const Mock: typeof SkiaExports &
99
99
  ShaderLib,
100
100
  createDrawing: Noop,
101
101
  createDeclaration: Noop,
102
+ makeImageFromView: Noop,
102
103
  };
@@ -1,5 +1,7 @@
1
+ import { findNodeHandle, Platform } from "react-native";
2
+
1
3
  import { Skia } from "../Skia";
2
- import type { DataSourceParam } from "../types";
4
+ import type { DataSourceParam, SkImage } from "../types";
3
5
 
4
6
  import { useRawData } from "./Data";
5
7
 
@@ -12,3 +14,43 @@ export const useImage = (
12
14
  source: DataSourceParam,
13
15
  onError?: (err: Error) => void
14
16
  ) => useRawData(source, imgFactory, onError);
17
+
18
+ /**
19
+ * Creates an image from a given view reference. NOTE: This method has different implementations
20
+ * on web/native. On web, the callback is called with the view ref and the callback is expected to
21
+ * return a promise that resolves to a Skia Image object. On native, the view ref is used to
22
+ * find the view tag and the Skia Image object is created from the view tag. This means that on web
23
+ * you will need to implement the logic to create the image from the view ref yourself.
24
+ * @param viewRef Ref to the view we're creating an image from
25
+ * @returns A promise that resolves to a Skia Image object or rejects
26
+ * with an error id the view tag is invalid.
27
+ */
28
+ export const makeImageFromView = <
29
+ T extends
30
+ | number
31
+ | React.Component<unknown, unknown>
32
+ | React.ComponentClass<unknown>
33
+ >(
34
+ viewRef: React.RefObject<T>,
35
+ callback:
36
+ | null
37
+ | ((viewRef: React.RefObject<T>) => Promise<SkImage | null>) = null
38
+ ) => {
39
+ // In web implementation we just delegate the work to the provided callback
40
+ if (Platform.OS === "web") {
41
+ if (callback) {
42
+ return callback(viewRef);
43
+ } else {
44
+ Promise.reject(
45
+ new Error(
46
+ "Callback is required on web in the makeImageFromView function."
47
+ )
48
+ );
49
+ }
50
+ }
51
+ const viewTag = findNodeHandle(viewRef.current);
52
+ if (viewTag !== null && viewTag !== 0) {
53
+ return Skia.Image.MakeImageFromViewTag(viewTag);
54
+ }
55
+ return Promise.reject(new Error("Invalid view tag"));
56
+ };
@@ -63,6 +63,14 @@ export interface ImageFactory {
63
63
  */
64
64
  MakeImageFromEncoded: (encoded: SkData) => SkImage | null;
65
65
 
66
+ /**
67
+ * Returns an image that will be a screenshot of the view represented by
68
+ * the view tag
69
+ * @param viewTag - The tag of the view to make an image from.
70
+ * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.
71
+ */
72
+ MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;
73
+
66
74
  /**
67
75
  * Returns an image with the given pixel data and format.
68
76
  * Note that we will always make a copy of the pixel data, because of inconsistencies in
@@ -1,3 +1,6 @@
1
1
  import type { JsiDisposable, SkJSIInstance } from "../JsiInstance";
2
2
 
3
- export type SkSVG = SkJSIInstance<"SVG"> & JsiDisposable;
3
+ export interface SkSVG extends SkJSIInstance<"SVG">, JsiDisposable {
4
+ width(): number;
5
+ height(): number;
6
+ }
@@ -1,6 +1,6 @@
1
1
  import type { CanvasKit } from "canvaskit-wasm";
2
2
 
3
- import type { SkData, ImageInfo } from "../types";
3
+ import type { SkData, ImageInfo, SkImage } from "../types";
4
4
  import type { ImageFactory } from "../types/Image/ImageFactory";
5
5
 
6
6
  import { Host, ckEnum } from "./Host";
@@ -12,6 +12,13 @@ export class JsiSkImageFactory extends Host implements ImageFactory {
12
12
  super(CanvasKit);
13
13
  }
14
14
 
15
+ MakeImageFromViewTag(viewTag: number): Promise<SkImage | null> {
16
+ const view = viewTag as unknown as HTMLElement;
17
+ // TODO: Implement screenshot from view in React JS
18
+ console.log(view);
19
+ return Promise.resolve(null);
20
+ }
21
+
15
22
  MakeImageFromEncoded(encoded: SkData) {
16
23
  const image = this.CanvasKit.MakeImageFromEncoded(
17
24
  JsiSkData.fromValue(encoded)