@shopify/react-native-skia 0.1.185 → 0.1.187

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) 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/globalJestSetup.js +6 -0
  72. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +16 -13
  73. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +8 -0
  74. package/ios/RNSkia-iOS/SkiaManager.mm +20 -1
  75. package/ios/RNSkia-iOS/ViewScreenshotService.h +21 -0
  76. package/ios/RNSkia-iOS/ViewScreenshotService.mm +79 -0
  77. package/jestSetup.js +2 -3
  78. package/lib/commonjs/dom/nodes/drawings/ImageNode.js +22 -1
  79. package/lib/commonjs/dom/nodes/drawings/ImageNode.js.map +1 -1
  80. package/lib/commonjs/dom/nodes/drawings/ImageSVG.js +5 -0
  81. package/lib/commonjs/dom/nodes/drawings/ImageSVG.js.map +1 -1
  82. package/lib/commonjs/dom/nodes/drawings/Text.d.ts +2 -2
  83. package/lib/commonjs/dom/nodes/drawings/Text.js +13 -2
  84. package/lib/commonjs/dom/nodes/drawings/Text.js.map +1 -1
  85. package/lib/commonjs/dom/nodes/paint/Shaders.js +5 -0
  86. package/lib/commonjs/dom/nodes/paint/Shaders.js.map +1 -1
  87. package/lib/commonjs/dom/types/Drawings.d.ts +5 -5
  88. package/lib/commonjs/dom/types/Drawings.js.map +1 -1
  89. package/lib/commonjs/dom/types/Shaders.d.ts +1 -1
  90. package/lib/commonjs/dom/types/Shaders.js.map +1 -1
  91. package/lib/commonjs/mock/index.d.ts +2 -15
  92. package/lib/commonjs/mock/index.js +37 -110
  93. package/lib/commonjs/mock/index.js.map +1 -1
  94. package/lib/commonjs/skia/core/Image.d.ts +14 -2
  95. package/lib/commonjs/skia/core/Image.js +37 -1
  96. package/lib/commonjs/skia/core/Image.js.map +1 -1
  97. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +7 -0
  98. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  99. package/lib/commonjs/skia/types/SVG/SVG.d.ts +4 -1
  100. package/lib/commonjs/skia/types/SVG/SVG.js.map +1 -1
  101. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -1
  102. package/lib/commonjs/skia/web/JsiSkImageFactory.js +7 -0
  103. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  104. package/lib/module/dom/nodes/drawings/ImageNode.js +22 -1
  105. package/lib/module/dom/nodes/drawings/ImageNode.js.map +1 -1
  106. package/lib/module/dom/nodes/drawings/ImageSVG.js +5 -0
  107. package/lib/module/dom/nodes/drawings/ImageSVG.js.map +1 -1
  108. package/lib/module/dom/nodes/drawings/Text.d.ts +2 -2
  109. package/lib/module/dom/nodes/drawings/Text.js +13 -2
  110. package/lib/module/dom/nodes/drawings/Text.js.map +1 -1
  111. package/lib/module/dom/nodes/paint/Shaders.js +5 -0
  112. package/lib/module/dom/nodes/paint/Shaders.js.map +1 -1
  113. package/lib/module/dom/types/Drawings.d.ts +5 -5
  114. package/lib/module/dom/types/Drawings.js.map +1 -1
  115. package/lib/module/dom/types/Shaders.d.ts +1 -1
  116. package/lib/module/dom/types/Shaders.js.map +1 -1
  117. package/lib/module/mock/index.d.ts +2 -15
  118. package/lib/module/mock/index.js +33 -95
  119. package/lib/module/mock/index.js.map +1 -1
  120. package/lib/module/skia/core/Image.d.ts +14 -2
  121. package/lib/module/skia/core/Image.js +32 -0
  122. package/lib/module/skia/core/Image.js.map +1 -1
  123. package/lib/module/skia/types/Image/ImageFactory.d.ts +7 -0
  124. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  125. package/lib/module/skia/types/SVG/SVG.d.ts +4 -1
  126. package/lib/module/skia/types/SVG/SVG.js.map +1 -1
  127. package/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -1
  128. package/lib/module/skia/web/JsiSkImageFactory.js +7 -0
  129. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  130. package/lib/typescript/globalJestSetup.d.ts +2 -0
  131. package/lib/typescript/src/dom/nodes/drawings/Text.d.ts +2 -2
  132. package/lib/typescript/src/dom/types/Drawings.d.ts +5 -5
  133. package/lib/typescript/src/dom/types/Shaders.d.ts +1 -1
  134. package/lib/typescript/src/mock/index.d.ts +2 -15
  135. package/lib/typescript/src/skia/core/Image.d.ts +14 -2
  136. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +7 -0
  137. package/lib/typescript/src/skia/types/SVG/SVG.d.ts +4 -1
  138. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +2 -1
  139. package/package.json +3 -2
  140. package/scripts/install-npm.js +3 -2
  141. package/src/dom/nodes/drawings/ImageNode.ts +9 -1
  142. package/src/dom/nodes/drawings/ImageSVG.ts +3 -0
  143. package/src/dom/nodes/drawings/Text.ts +13 -3
  144. package/src/dom/nodes/paint/Shaders.ts +4 -0
  145. package/src/dom/types/Drawings.ts +5 -5
  146. package/src/dom/types/Shaders.ts +1 -1
  147. package/src/mock/index.ts +35 -98
  148. package/src/skia/core/Image.ts +43 -1
  149. package/src/skia/types/Image/ImageFactory.ts +8 -0
  150. package/src/skia/types/SVG/SVG.ts +4 -1
  151. package/src/skia/web/JsiSkImageFactory.ts +8 -1
@@ -1 +1 @@
1
- {"version":3,"names":["Skia","useRawData","imgFactory","Image","MakeImageFromEncoded","bind","useImage","source","onError"],"sources":["Image.ts"],"sourcesContent":["import { Skia } from \"../Skia\";\nimport type { DataSourceParam } from \"../types\";\n\nimport { useRawData } from \"./Data\";\n\nconst imgFactory = Skia.Image.MakeImageFromEncoded.bind(Skia.Image);\n\n/**\n * Returns a Skia Image object\n * */\nexport const useImage = (\n source: DataSourceParam,\n onError?: (err: Error) => void\n) => useRawData(source, imgFactory, onError);\n"],"mappings":"AAAA,SAASA,IAAT,QAAqB,SAArB;AAGA,SAASC,UAAT,QAA2B,QAA3B;AAEA,MAAMC,UAAU,GAAGF,IAAI,CAACG,KAAL,CAAWC,oBAAX,CAAgCC,IAAhC,CAAqCL,IAAI,CAACG,KAA1C,CAAnB;AAEA;AACA;AACA;;AACA,OAAO,MAAMG,QAAQ,GAAG,CACtBC,MADsB,EAEtBC,OAFsB,KAGnBP,UAAU,CAACM,MAAD,EAASL,UAAT,EAAqBM,OAArB,CAHR"}
1
+ {"version":3,"names":["findNodeHandle","Platform","Skia","useRawData","imgFactory","Image","MakeImageFromEncoded","bind","useImage","source","onError","makeImageFromView","viewRef","callback","OS","Promise","reject","Error","viewTag","current","MakeImageFromViewTag"],"sources":["Image.ts"],"sourcesContent":["import { findNodeHandle, Platform } from \"react-native\";\n\nimport { Skia } from \"../Skia\";\nimport type { DataSourceParam, SkImage } from \"../types\";\n\nimport { useRawData } from \"./Data\";\n\nconst imgFactory = Skia.Image.MakeImageFromEncoded.bind(Skia.Image);\n\n/**\n * Returns a Skia Image object\n * */\nexport const useImage = (\n source: DataSourceParam,\n onError?: (err: Error) => void\n) => useRawData(source, imgFactory, onError);\n\n/**\n * Creates an image from a given view reference. NOTE: This method has different implementations\n * on web/native. On web, the callback is called with the view ref and the callback is expected to\n * return a promise that resolves to a Skia Image object. On native, the view ref is used to\n * find the view tag and the Skia Image object is created from the view tag. This means that on web\n * you will need to implement the logic to create the image from the view ref yourself.\n * @param viewRef Ref to the view we're creating an image from\n * @returns A promise that resolves to a Skia Image object or rejects\n * with an error id the view tag is invalid.\n */\nexport const makeImageFromView = <\n T extends\n | number\n | React.Component<unknown, unknown>\n | React.ComponentClass<unknown>\n>(\n viewRef: React.RefObject<T>,\n callback:\n | null\n | ((viewRef: React.RefObject<T>) => Promise<SkImage | null>) = null\n) => {\n // In web implementation we just delegate the work to the provided callback\n if (Platform.OS === \"web\") {\n if (callback) {\n return callback(viewRef);\n } else {\n Promise.reject(\n new Error(\n \"Callback is required on web in the makeImageFromView function.\"\n )\n );\n }\n }\n const viewTag = findNodeHandle(viewRef.current);\n if (viewTag !== null && viewTag !== 0) {\n return Skia.Image.MakeImageFromViewTag(viewTag);\n }\n return Promise.reject(new Error(\"Invalid view tag\"));\n};\n"],"mappings":"AAAA,SAASA,cAAT,EAAyBC,QAAzB,QAAyC,cAAzC;AAEA,SAASC,IAAT,QAAqB,SAArB;AAGA,SAASC,UAAT,QAA2B,QAA3B;AAEA,MAAMC,UAAU,GAAGF,IAAI,CAACG,KAAL,CAAWC,oBAAX,CAAgCC,IAAhC,CAAqCL,IAAI,CAACG,KAA1C,CAAnB;AAEA;AACA;AACA;;AACA,OAAO,MAAMG,QAAQ,GAAG,CACtBC,MADsB,EAEtBC,OAFsB,KAGnBP,UAAU,CAACM,MAAD,EAASL,UAAT,EAAqBM,OAArB,CAHR;AAKP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,iBAAiB,GAAG,UAM/BC,OAN+B,EAU5B;EAAA,IAHHC,QAGG,uEAD8D,IAC9D;;EACH;EACA,IAAIZ,QAAQ,CAACa,EAAT,KAAgB,KAApB,EAA2B;IACzB,IAAID,QAAJ,EAAc;MACZ,OAAOA,QAAQ,CAACD,OAAD,CAAf;IACD,CAFD,MAEO;MACLG,OAAO,CAACC,MAAR,CACE,IAAIC,KAAJ,CACE,gEADF,CADF;IAKD;EACF;;EACD,MAAMC,OAAO,GAAGlB,cAAc,CAACY,OAAO,CAACO,OAAT,CAA9B;;EACA,IAAID,OAAO,KAAK,IAAZ,IAAoBA,OAAO,KAAK,CAApC,EAAuC;IACrC,OAAOhB,IAAI,CAACG,KAAL,CAAWe,oBAAX,CAAgCF,OAAhC,CAAP;EACD;;EACD,OAAOH,OAAO,CAACC,MAAR,CAAe,IAAIC,KAAJ,CAAU,kBAAV,CAAf,CAAP;AACD,CA5BM"}
@@ -48,6 +48,13 @@ export interface ImageFactory {
48
48
  * image, nullptr is returned.
49
49
  */
50
50
  MakeImageFromEncoded: (encoded: SkData) => SkImage | null;
51
+ /**
52
+ * Returns an image that will be a screenshot of the view represented by
53
+ * the view tag
54
+ * @param viewTag - The tag of the view to make an image from.
55
+ * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.
56
+ */
57
+ MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;
51
58
  /**
52
59
  * Returns an image with the given pixel data and format.
53
60
  * Note that we will always make a copy of the pixel data, because of inconsistencies in
@@ -1 +1 @@
1
- {"version":3,"names":["AlphaType","ColorType"],"sources":["ImageFactory.ts"],"sourcesContent":["import type { SkData } from \"../Data\";\n\nimport type { SkImage } from \"./Image\";\n\nexport enum AlphaType {\n Unknown,\n Opaque,\n Premul,\n Unpremul,\n}\n\nexport enum ColorType {\n Unknown, //!< uninitialized\n Alpha_8, //!< pixel with alpha in 8-bit byte\n RGB_565, //!< pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word\n ARGB_4444, //!< pixel with 4 bits for alpha, red, green, blue; in 16-bit word\n RGBA_8888, //!< pixel with 8 bits for red, green, blue, alpha; in 32-bit word\n RGB_888x, //!< pixel with 8 bits each for red, green, blue; in 32-bit word\n BGRA_8888, //!< pixel with 8 bits for blue, green, red, alpha; in 32-bit word\n RGBA_1010102, //!< 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word\n BGRA_1010102, //!< 10 bits for blue, green, red; 2 bits for alpha; in 32-bit word\n RGB_101010x, //!< pixel with 10 bits each for red, green, blue; in 32-bit word\n BGR_101010x, //!< pixel with 10 bits each for blue, green, red; in 32-bit word\n Gray_8, //!< pixel with grayscale level in 8-bit byte\n RGBA_F16Norm, //!< pixel with half floats in [0,1] for red, green, blue, alpha;\n // in 64-bit word\n RGBA_F16, //!< pixel with half floats for red, green, blue, alpha;\n // in 64-bit word\n RGBA_F32, //!< pixel using C float for red, green, blue, alpha; in 128-bit word\n\n // The following 6 colortypes are just for reading from - not for rendering to\n R8G8_unorm, //!< pixel with a uint8_t for red and green\n\n A16_float, //!< pixel with a half float for alpha\n R16G16_float, //!< pixel with a half float for red and green\n\n A16_unorm, //!< pixel with a little endian uint16_t for alpha\n R16G16_unorm, //!< pixel with a little endian uint16_t for red and green\n R16G16B16A16_unorm, //!< pixel with a little endian uint16_t for red, green, blue\n // and alpha\n SRGBA_8888,\n}\n\nexport interface ImageInfo {\n alphaType: AlphaType;\n // TODO: add support for color space\n // colorSpace: ColorSpace;\n colorType: ColorType;\n height: number;\n width: number;\n}\n\nexport interface ImageFactory {\n /**\n * Return an Image backed by the encoded data, but attempt to defer decoding until the image\n * is actually used/drawn. This deferral allows the system to cache the result, either on the\n * CPU or on the GPU, depending on where the image is drawn.\n * This decoding uses the codecs that have been compiled into CanvasKit. If the bytes are\n * invalid (or an unrecognized codec), null will be returned. See Image.h for more details.\n * @param data - Data object with bytes of data\n * @returns If the encoded format is not supported, or subset is outside of the bounds of the decoded\n * image, nullptr is returned.\n */\n MakeImageFromEncoded: (encoded: SkData) => SkImage | null;\n\n /**\n * Returns an image with the given pixel data and format.\n * Note that we will always make a copy of the pixel data, because of inconsistencies in\n * behavior between GPU and CPU (i.e. the pixel data will be turned into a GPU texture and\n * not modifiable after creation).\n *\n * @param info\n * @param data - bytes representing the pixel data.\n * @param bytesPerRow\n */\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): SkImage | null;\n}\n"],"mappings":"AAIA,WAAYA,SAAZ;;WAAYA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S;;AAOZ,WAAYC,SAAZ;;WAAYA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S"}
1
+ {"version":3,"names":["AlphaType","ColorType"],"sources":["ImageFactory.ts"],"sourcesContent":["import type { SkData } from \"../Data\";\n\nimport type { SkImage } from \"./Image\";\n\nexport enum AlphaType {\n Unknown,\n Opaque,\n Premul,\n Unpremul,\n}\n\nexport enum ColorType {\n Unknown, //!< uninitialized\n Alpha_8, //!< pixel with alpha in 8-bit byte\n RGB_565, //!< pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word\n ARGB_4444, //!< pixel with 4 bits for alpha, red, green, blue; in 16-bit word\n RGBA_8888, //!< pixel with 8 bits for red, green, blue, alpha; in 32-bit word\n RGB_888x, //!< pixel with 8 bits each for red, green, blue; in 32-bit word\n BGRA_8888, //!< pixel with 8 bits for blue, green, red, alpha; in 32-bit word\n RGBA_1010102, //!< 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word\n BGRA_1010102, //!< 10 bits for blue, green, red; 2 bits for alpha; in 32-bit word\n RGB_101010x, //!< pixel with 10 bits each for red, green, blue; in 32-bit word\n BGR_101010x, //!< pixel with 10 bits each for blue, green, red; in 32-bit word\n Gray_8, //!< pixel with grayscale level in 8-bit byte\n RGBA_F16Norm, //!< pixel with half floats in [0,1] for red, green, blue, alpha;\n // in 64-bit word\n RGBA_F16, //!< pixel with half floats for red, green, blue, alpha;\n // in 64-bit word\n RGBA_F32, //!< pixel using C float for red, green, blue, alpha; in 128-bit word\n\n // The following 6 colortypes are just for reading from - not for rendering to\n R8G8_unorm, //!< pixel with a uint8_t for red and green\n\n A16_float, //!< pixel with a half float for alpha\n R16G16_float, //!< pixel with a half float for red and green\n\n A16_unorm, //!< pixel with a little endian uint16_t for alpha\n R16G16_unorm, //!< pixel with a little endian uint16_t for red and green\n R16G16B16A16_unorm, //!< pixel with a little endian uint16_t for red, green, blue\n // and alpha\n SRGBA_8888,\n}\n\nexport interface ImageInfo {\n alphaType: AlphaType;\n // TODO: add support for color space\n // colorSpace: ColorSpace;\n colorType: ColorType;\n height: number;\n width: number;\n}\n\nexport interface ImageFactory {\n /**\n * Return an Image backed by the encoded data, but attempt to defer decoding until the image\n * is actually used/drawn. This deferral allows the system to cache the result, either on the\n * CPU or on the GPU, depending on where the image is drawn.\n * This decoding uses the codecs that have been compiled into CanvasKit. If the bytes are\n * invalid (or an unrecognized codec), null will be returned. See Image.h for more details.\n * @param data - Data object with bytes of data\n * @returns If the encoded format is not supported, or subset is outside of the bounds of the decoded\n * image, nullptr is returned.\n */\n MakeImageFromEncoded: (encoded: SkData) => SkImage | null;\n\n /**\n * Returns an image that will be a screenshot of the view represented by\n * the view tag\n * @param viewTag - The tag of the view to make an image from.\n * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.\n */\n MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;\n\n /**\n * Returns an image with the given pixel data and format.\n * Note that we will always make a copy of the pixel data, because of inconsistencies in\n * behavior between GPU and CPU (i.e. the pixel data will be turned into a GPU texture and\n * not modifiable after creation).\n *\n * @param info\n * @param data - bytes representing the pixel data.\n * @param bytesPerRow\n */\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): SkImage | null;\n}\n"],"mappings":"AAIA,WAAYA,SAAZ;;WAAYA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S;;AAOZ,WAAYC,SAAZ;;WAAYA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S"}
@@ -1,2 +1,5 @@
1
1
  import type { JsiDisposable, SkJSIInstance } from "../JsiInstance";
2
- export declare type SkSVG = SkJSIInstance<"SVG"> & JsiDisposable;
2
+ export interface SkSVG extends SkJSIInstance<"SVG">, JsiDisposable {
3
+ width(): number;
4
+ height(): number;
5
+ }
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["SVG.ts"],"sourcesContent":["import type { JsiDisposable, SkJSIInstance } from \"../JsiInstance\";\n\nexport type SkSVG = SkJSIInstance<\"SVG\"> & JsiDisposable;\n"],"mappings":""}
1
+ {"version":3,"names":[],"sources":["SVG.ts"],"sourcesContent":["import type { JsiDisposable, SkJSIInstance } from \"../JsiInstance\";\n\nexport interface SkSVG extends SkJSIInstance<\"SVG\">, JsiDisposable {\n width(): number;\n height(): number;\n}\n"],"mappings":""}
@@ -1,10 +1,11 @@
1
1
  import type { CanvasKit } from "canvaskit-wasm";
2
- import type { SkData, ImageInfo } from "../types";
2
+ import type { SkData, ImageInfo, SkImage } from "../types";
3
3
  import type { ImageFactory } from "../types/Image/ImageFactory";
4
4
  import { Host } from "./Host";
5
5
  import { JsiSkImage } from "./JsiSkImage";
6
6
  export declare class JsiSkImageFactory extends Host implements ImageFactory {
7
7
  constructor(CanvasKit: CanvasKit);
8
+ MakeImageFromViewTag(viewTag: number): Promise<SkImage | null>;
8
9
  MakeImageFromEncoded(encoded: SkData): JsiSkImage | null;
9
10
  MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): JsiSkImage | null;
10
11
  }
@@ -6,6 +6,13 @@ export class JsiSkImageFactory extends Host {
6
6
  super(CanvasKit);
7
7
  }
8
8
 
9
+ MakeImageFromViewTag(viewTag) {
10
+ const view = viewTag; // TODO: Implement screenshot from view in React JS
11
+
12
+ console.log(view);
13
+ return Promise.resolve(null);
14
+ }
15
+
9
16
  MakeImageFromEncoded(encoded) {
10
17
  const image = this.CanvasKit.MakeImageFromEncoded(JsiSkData.fromValue(encoded));
11
18
 
@@ -1 +1 @@
1
- {"version":3,"names":["Host","ckEnum","JsiSkImage","JsiSkData","JsiSkImageFactory","constructor","CanvasKit","MakeImageFromEncoded","encoded","image","fromValue","MakeImage","info","data","bytesPerRow","alphaType","colorSpace","ColorSpace","SRGB","colorType","height","width"],"sources":["JsiSkImageFactory.ts"],"sourcesContent":["import type { CanvasKit } from \"canvaskit-wasm\";\n\nimport type { SkData, ImageInfo } from \"../types\";\nimport type { ImageFactory } from \"../types/Image/ImageFactory\";\n\nimport { Host, ckEnum } from \"./Host\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkData } from \"./JsiSkData\";\n\nexport class JsiSkImageFactory extends Host implements ImageFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n MakeImageFromEncoded(encoded: SkData) {\n const image = this.CanvasKit.MakeImageFromEncoded(\n JsiSkData.fromValue(encoded)\n );\n if (image === null) {\n return null;\n }\n return new JsiSkImage(this.CanvasKit, image);\n }\n\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number) {\n // see toSkImageInfo() from canvaskit\n const image = this.CanvasKit.MakeImage(\n {\n alphaType: ckEnum(info.alphaType),\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n colorType: ckEnum(info.colorType),\n height: info.height,\n width: info.width,\n },\n JsiSkData.fromValue(data),\n bytesPerRow\n );\n if (image === null) {\n return null;\n }\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":"AAKA,SAASA,IAAT,EAAeC,MAAf,QAA6B,QAA7B;AACA,SAASC,UAAT,QAA2B,cAA3B;AACA,SAASC,SAAT,QAA0B,aAA1B;AAEA,OAAO,MAAMC,iBAAN,SAAgCJ,IAAhC,CAA6D;EAClEK,WAAW,CAACC,SAAD,EAAuB;IAChC,MAAMA,SAAN;EACD;;EAEDC,oBAAoB,CAACC,OAAD,EAAkB;IACpC,MAAMC,KAAK,GAAG,KAAKH,SAAL,CAAeC,oBAAf,CACZJ,SAAS,CAACO,SAAV,CAAoBF,OAApB,CADY,CAAd;;IAGA,IAAIC,KAAK,KAAK,IAAd,EAAoB;MAClB,OAAO,IAAP;IACD;;IACD,OAAO,IAAIP,UAAJ,CAAe,KAAKI,SAApB,EAA+BG,KAA/B,CAAP;EACD;;EAEDE,SAAS,CAACC,IAAD,EAAkBC,IAAlB,EAAgCC,WAAhC,EAAqD;IAC5D;IACA,MAAML,KAAK,GAAG,KAAKH,SAAL,CAAeK,SAAf,CACZ;MACEI,SAAS,EAAEd,MAAM,CAACW,IAAI,CAACG,SAAN,CADnB;MAEEC,UAAU,EAAE,KAAKV,SAAL,CAAeW,UAAf,CAA0BC,IAFxC;MAGEC,SAAS,EAAElB,MAAM,CAACW,IAAI,CAACO,SAAN,CAHnB;MAIEC,MAAM,EAAER,IAAI,CAACQ,MAJf;MAKEC,KAAK,EAAET,IAAI,CAACS;IALd,CADY,EAQZlB,SAAS,CAACO,SAAV,CAAoBG,IAApB,CARY,EASZC,WATY,CAAd;;IAWA,IAAIL,KAAK,KAAK,IAAd,EAAoB;MAClB,OAAO,IAAP;IACD;;IACD,OAAO,IAAIP,UAAJ,CAAe,KAAKI,SAApB,EAA+BG,KAA/B,CAAP;EACD;;AAhCiE"}
1
+ {"version":3,"names":["Host","ckEnum","JsiSkImage","JsiSkData","JsiSkImageFactory","constructor","CanvasKit","MakeImageFromViewTag","viewTag","view","console","log","Promise","resolve","MakeImageFromEncoded","encoded","image","fromValue","MakeImage","info","data","bytesPerRow","alphaType","colorSpace","ColorSpace","SRGB","colorType","height","width"],"sources":["JsiSkImageFactory.ts"],"sourcesContent":["import type { CanvasKit } from \"canvaskit-wasm\";\n\nimport type { SkData, ImageInfo, SkImage } from \"../types\";\nimport type { ImageFactory } from \"../types/Image/ImageFactory\";\n\nimport { Host, ckEnum } from \"./Host\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkData } from \"./JsiSkData\";\n\nexport class JsiSkImageFactory extends Host implements ImageFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n MakeImageFromViewTag(viewTag: number): Promise<SkImage | null> {\n const view = viewTag as unknown as HTMLElement;\n // TODO: Implement screenshot from view in React JS\n console.log(view);\n return Promise.resolve(null);\n }\n\n MakeImageFromEncoded(encoded: SkData) {\n const image = this.CanvasKit.MakeImageFromEncoded(\n JsiSkData.fromValue(encoded)\n );\n if (image === null) {\n return null;\n }\n return new JsiSkImage(this.CanvasKit, image);\n }\n\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number) {\n // see toSkImageInfo() from canvaskit\n const image = this.CanvasKit.MakeImage(\n {\n alphaType: ckEnum(info.alphaType),\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n colorType: ckEnum(info.colorType),\n height: info.height,\n width: info.width,\n },\n JsiSkData.fromValue(data),\n bytesPerRow\n );\n if (image === null) {\n return null;\n }\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":"AAKA,SAASA,IAAT,EAAeC,MAAf,QAA6B,QAA7B;AACA,SAASC,UAAT,QAA2B,cAA3B;AACA,SAASC,SAAT,QAA0B,aAA1B;AAEA,OAAO,MAAMC,iBAAN,SAAgCJ,IAAhC,CAA6D;EAClEK,WAAW,CAACC,SAAD,EAAuB;IAChC,MAAMA,SAAN;EACD;;EAEDC,oBAAoB,CAACC,OAAD,EAA2C;IAC7D,MAAMC,IAAI,GAAGD,OAAb,CAD6D,CAE7D;;IACAE,OAAO,CAACC,GAAR,CAAYF,IAAZ;IACA,OAAOG,OAAO,CAACC,OAAR,CAAgB,IAAhB,CAAP;EACD;;EAEDC,oBAAoB,CAACC,OAAD,EAAkB;IACpC,MAAMC,KAAK,GAAG,KAAKV,SAAL,CAAeQ,oBAAf,CACZX,SAAS,CAACc,SAAV,CAAoBF,OAApB,CADY,CAAd;;IAGA,IAAIC,KAAK,KAAK,IAAd,EAAoB;MAClB,OAAO,IAAP;IACD;;IACD,OAAO,IAAId,UAAJ,CAAe,KAAKI,SAApB,EAA+BU,KAA/B,CAAP;EACD;;EAEDE,SAAS,CAACC,IAAD,EAAkBC,IAAlB,EAAgCC,WAAhC,EAAqD;IAC5D;IACA,MAAML,KAAK,GAAG,KAAKV,SAAL,CAAeY,SAAf,CACZ;MACEI,SAAS,EAAErB,MAAM,CAACkB,IAAI,CAACG,SAAN,CADnB;MAEEC,UAAU,EAAE,KAAKjB,SAAL,CAAekB,UAAf,CAA0BC,IAFxC;MAGEC,SAAS,EAAEzB,MAAM,CAACkB,IAAI,CAACO,SAAN,CAHnB;MAIEC,MAAM,EAAER,IAAI,CAACQ,MAJf;MAKEC,KAAK,EAAET,IAAI,CAACS;IALd,CADY,EAQZzB,SAAS,CAACc,SAAV,CAAoBG,IAApB,CARY,EASZC,WATY,CAAd;;IAWA,IAAIL,KAAK,KAAK,IAAd,EAAoB;MAClB,OAAO,IAAP;IACD;;IACD,OAAO,IAAId,UAAJ,CAAe,KAAKI,SAApB,EAA+BU,KAA/B,CAAP;EACD;;AAvCiE"}
@@ -0,0 +1,2 @@
1
+ declare function _exports(): Promise<void>;
2
+ export = _exports;
@@ -8,9 +8,9 @@ export declare class TextNode extends JsiDrawingNode<TextProps, null> {
8
8
  protected deriveProps(): null;
9
9
  draw({ canvas, paint }: DrawingContext): void;
10
10
  }
11
- export declare class TextPathNode extends JsiDrawingNode<TextPathProps, SkTextBlob> {
11
+ export declare class TextPathNode extends JsiDrawingNode<TextPathProps, SkTextBlob | null> {
12
12
  constructor(ctx: NodeContext, props: TextPathProps);
13
- deriveProps(): SkTextBlob;
13
+ deriveProps(): SkTextBlob | null;
14
14
  draw({ canvas, paint }: DrawingContext): void;
15
15
  }
16
16
  export declare class TextBlobNode extends JsiDrawingNode<TextBlobProps, null> {
@@ -6,7 +6,7 @@ export interface DrawingNodeProps extends GroupProps {
6
6
  }
7
7
  export declare type ImageProps = DrawingNodeProps & RectDef & {
8
8
  fit?: Fit;
9
- image: SkImage;
9
+ image: SkImage | null;
10
10
  };
11
11
  export declare type CircleProps = CircleDef & DrawingNodeProps;
12
12
  export interface PathProps extends DrawingNodeProps {
@@ -51,7 +51,7 @@ export interface VerticesProps extends DrawingNodeProps {
51
51
  indices?: number[];
52
52
  }
53
53
  export declare type ImageSVGProps = RectDef & {
54
- svg: SkSVG;
54
+ svg: SkSVG | null;
55
55
  } & DrawingNodeProps;
56
56
  export interface PictureProps extends DrawingNodeProps {
57
57
  picture: SkPicture;
@@ -65,13 +65,13 @@ export interface DiffRectProps extends DrawingNodeProps {
65
65
  outer: SkRRect;
66
66
  }
67
67
  export interface TextProps extends DrawingNodeProps {
68
- font: SkFont;
68
+ font: SkFont | null;
69
69
  text: string;
70
70
  x: number;
71
71
  y: number;
72
72
  }
73
73
  export interface TextPathProps extends DrawingNodeProps {
74
- font: SkFont;
74
+ font: SkFont | null;
75
75
  text: string;
76
76
  path: PathDef;
77
77
  initialOffset: number;
@@ -86,7 +86,7 @@ export interface Glyph {
86
86
  pos: SkPoint;
87
87
  }
88
88
  export interface GlyphsProps extends DrawingNodeProps {
89
- font: SkFont;
89
+ font: SkFont | null;
90
90
  x: number;
91
91
  y: number;
92
92
  glyphs: Glyph[];
@@ -11,7 +11,7 @@ export interface ImageShaderProps extends TransformProps, Partial<RectCtor> {
11
11
  mm: SkEnum<typeof MipmapMode>;
12
12
  fit: Fit;
13
13
  rect?: SkRect;
14
- image: SkImage;
14
+ image: SkImage | null;
15
15
  }
16
16
  export interface ColorProps {
17
17
  color: Color;
@@ -1,15 +1,2 @@
1
- import type { Skia as SkiaApi } from "../skia/types";
2
- import type * as SkiaExports from "../skia";
3
- import type * as ValueExports from "../values";
4
- import type * as AnimationExports from "../animation";
5
- import { ShaderLib } from "../renderer/components/shaders/ShaderLib";
6
- export declare const Skia: SkiaApi;
7
- export declare const vec: (x?: number, y?: number) => {
8
- x: number;
9
- y: number;
10
- };
11
- export declare const Mock: typeof SkiaExports & typeof ValueExports & typeof AnimationExports & {
12
- createDrawing: () => any;
13
- createDeclaration: () => any;
14
- ShaderLib: typeof ShaderLib;
15
- };
1
+ import type { CanvasKit } from "canvaskit-wasm";
2
+ export declare const Mock: (CanvasKit: CanvasKit) => any;
@@ -1,5 +1,17 @@
1
- import type { DataSourceParam } from "../types";
1
+ /// <reference types="react" />
2
+ import type { DataSourceParam, SkImage } from "../types";
2
3
  /**
3
4
  * Returns a Skia Image object
4
5
  * */
5
- export declare const useImage: (source: DataSourceParam, onError?: ((err: Error) => void) | undefined) => import("../types").SkImage | null;
6
+ export declare const useImage: (source: DataSourceParam, onError?: ((err: Error) => void) | undefined) => SkImage | null;
7
+ /**
8
+ * Creates an image from a given view reference. NOTE: This method has different implementations
9
+ * on web/native. On web, the callback is called with the view ref and the callback is expected to
10
+ * return a promise that resolves to a Skia Image object. On native, the view ref is used to
11
+ * find the view tag and the Skia Image object is created from the view tag. This means that on web
12
+ * you will need to implement the logic to create the image from the view ref yourself.
13
+ * @param viewRef Ref to the view we're creating an image from
14
+ * @returns A promise that resolves to a Skia Image object or rejects
15
+ * with an error id the view tag is invalid.
16
+ */
17
+ export declare const makeImageFromView: <T extends number | import("react").Component<unknown, unknown, any> | import("react").ComponentClass<unknown, any>>(viewRef: import("react").RefObject<T>, callback?: ((viewRef: import("react").RefObject<T>) => Promise<SkImage | null>) | null) => Promise<SkImage | null>;
@@ -48,6 +48,13 @@ export interface ImageFactory {
48
48
  * image, nullptr is returned.
49
49
  */
50
50
  MakeImageFromEncoded: (encoded: SkData) => SkImage | null;
51
+ /**
52
+ * Returns an image that will be a screenshot of the view represented by
53
+ * the view tag
54
+ * @param viewTag - The tag of the view to make an image from.
55
+ * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.
56
+ */
57
+ MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;
51
58
  /**
52
59
  * Returns an image with the given pixel data and format.
53
60
  * Note that we will always make a copy of the pixel data, because of inconsistencies in
@@ -1,2 +1,5 @@
1
1
  import type { JsiDisposable, SkJSIInstance } from "../JsiInstance";
2
- export declare type SkSVG = SkJSIInstance<"SVG"> & JsiDisposable;
2
+ export interface SkSVG extends SkJSIInstance<"SVG">, JsiDisposable {
3
+ width(): number;
4
+ height(): number;
5
+ }
@@ -1,10 +1,11 @@
1
1
  import type { CanvasKit } from "canvaskit-wasm";
2
- import type { SkData, ImageInfo } from "../types";
2
+ import type { SkData, ImageInfo, SkImage } from "../types";
3
3
  import type { ImageFactory } from "../types/Image/ImageFactory";
4
4
  import { Host } from "./Host";
5
5
  import { JsiSkImage } from "./JsiSkImage";
6
6
  export declare class JsiSkImageFactory extends Host implements ImageFactory {
7
7
  constructor(CanvasKit: CanvasKit);
8
+ MakeImageFromViewTag(viewTag: number): Promise<SkImage | null>;
8
9
  MakeImageFromEncoded(encoded: SkData): JsiSkImage | null;
9
10
  MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): JsiSkImage | null;
10
11
  }
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "setup-skia-web": "./scripts/setup-canvaskit.js"
8
8
  },
9
9
  "title": "React Native Skia",
10
- "version": "0.1.185",
10
+ "version": "0.1.187",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "files": [
@@ -25,6 +25,7 @@
25
25
  "libs/android/**",
26
26
  "index.js",
27
27
  "jestSetup.js",
28
+ "globalJestSetup.js",
28
29
  "cpp/**/*.{h,cpp}",
29
30
  "ios",
30
31
  "libs/ios/libskia.xcframework",
@@ -88,7 +89,7 @@
88
89
  "jest": "28.1.3",
89
90
  "merge-dirs": "^0.2.1",
90
91
  "react": "18.1.0",
91
- "react-native": "0.71.0",
92
+ "react-native": "0.71.7",
92
93
  "react-native-builder-bob": "^0.18.2",
93
94
  "ts-jest": "^28.0.7",
94
95
  "typescript": "4.8.3"
@@ -8,9 +8,10 @@ const createSymlink = (p) => {
8
8
  const srcDir = path.resolve(`./cpp/${p}`);
9
9
  const dstDir = path.resolve(`./android/cpp/${p}`);
10
10
 
11
- if (!fs.existsSync(dstDir) || !fs.lstatSync(dstDir).isSymbolicLink()) {
12
- fs.symlinkSync(srcDir, dstDir, "junction");
11
+ if (fs.existsSync(dstDir)) {
12
+ fs.unlinkSync(dstDir);
13
13
  }
14
+ fs.symlinkSync(srcDir, dstDir, "junction");
14
15
  };
15
16
 
16
17
  // Copy common cpp files from the package root to the android folder
@@ -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
@@ -1,102 +1,39 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
1
+ import type { CanvasKit } from "canvaskit-wasm";
2
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 { Selector } from "../values/selector";
7
- import * as BaseSkia from "../skia/types";
8
- import type * as SkiaExports from "../skia";
9
- import type * as ValueExports from "../values";
10
- import type * as AnimationExports from "../animation";
11
- import * as timingFunctions from "../animation/timing";
12
- import * as springFunctions from "../animation/spring";
13
- import * as decayFunctions from "../animation/decay";
14
- import * as interpolateFn from "../animation/functions/interpolate";
15
- import * as interpolatePathFn from "../animation/functions/interpolatePaths";
16
- import * as interpolateVectorFn from "../animation/functions/interpolateVector";
17
- import { ShaderLib } from "../renderer/components/shaders/ShaderLib";
3
+ import { JsiSkApi } from "../skia/web";
4
+ import { ValueApi } from "../values/web";
18
5
 
19
- class Stub {
20
- constructor() {
21
- return new Proxy(() => {}, {
22
- get: () => new Stub(),
23
- apply: () => new Stub(),
24
- set: () => true,
25
- });
26
- }
27
- }
6
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
+ const Noop: () => any = () => undefined;
8
+ const NoopValue = () => ({ current: 0 });
28
9
 
29
- const Noop: () => any = () => {};
30
-
31
- export const Skia: SkiaApi = new Stub() as any;
32
-
33
- export const vec = (x?: number, y?: number) => ({ x: x ?? 0, y: y ?? x ?? 0 });
34
-
35
- export const Mock: typeof SkiaExports &
36
- typeof ValueExports &
37
- typeof AnimationExports & {
38
- createDrawing: () => any;
39
- createDeclaration: () => any;
40
- ShaderLib: typeof ShaderLib;
41
- } = {
42
- // SkiaExports
43
- // 1. Skia API. BaseSkia contains the enums, and functions like isPaint etc
44
- Skia,
45
- ...BaseSkia,
46
- // 2. Hooks
47
- useRawData: Noop,
48
- useData: Noop,
49
- useFont: Noop,
50
- useTypeface: Noop,
51
- useImage: Noop,
52
- useSVG: Noop,
53
- createPicture: Noop,
54
- // 3. Point/Rect/Transform utilities
55
- vec,
56
- rect: (x: number, y: number, width: number, height: number) => ({
57
- x,
58
- y,
59
- width,
60
- height,
61
- }),
62
- rrect: (r: SkRect, rx: number, ry: number) => ({
63
- rect: r,
64
- rx,
65
- ry,
66
- }),
67
- point: vec,
68
- add: (a: Vector, b: Vector) => vec(a.x + b.x, a.y + b.y),
69
- sub: (a: Vector, b: Vector) => vec(a.x - b.x, a.y - b.y),
70
- neg: (a: Vector) => vec(-a.x, -a.y),
71
- dist: (a: Vector, b: Vector) => Math.hypot(a.x - b.x, a.y - b.y),
72
- translate: ({ x, y }: Vector) =>
73
- [{ translateX: x }, { translateY: y }] as const,
74
-
75
- bounds: Noop,
76
- topLeft: Noop,
77
- topRight: Noop,
78
- bottomLeft: Noop,
79
- bottomRight: Noop,
80
- center: Noop,
81
- processTransform2d: Noop,
82
- // ValueExports
83
- ...Values,
84
- ...ValuesHooks,
85
- Selector,
86
- // Animations
87
- ...timingFunctions,
88
- ...springFunctions,
89
- ...decayFunctions,
90
- ...interpolateFn,
91
- ...interpolatePathFn,
92
- ...interpolateVectorFn,
93
- interpolateColors: (
94
- _value: number,
95
- _inputRange: number[],
96
- _outputRange: Color[]
97
- ) => Float32Array.of(0, 0, 0, 0),
98
- mixColors: (_v: number, _x: Color, _y: Color) => Float32Array.of(0, 0, 0, 0),
99
- ShaderLib,
100
- createDrawing: Noop,
101
- createDeclaration: Noop,
10
+ export const Mock = (CanvasKit: CanvasKit) => {
11
+ global.SkiaApi = JsiSkApi(CanvasKit);
12
+ global.SkiaValueApi = ValueApi;
13
+ const Skia = global.SkiaApi;
14
+ return {
15
+ Skia,
16
+ ...require("../renderer/components"),
17
+ ...require("../skia"),
18
+ ...require("../values"),
19
+ ...require("../animation"),
20
+ ...require("../dom/types"),
21
+ ...require("../dom/nodes"),
22
+ // We could use the real Canvas if we mock the SkiaView component for node
23
+ Canvas: Noop,
24
+ useValue: NoopValue,
25
+ useComputedValue: NoopValue,
26
+ useTouchHandler: Noop,
27
+ useTiming: NoopValue,
28
+ useLoop: NoopValue,
29
+ useSpring: NoopValue,
30
+ useClockValue: NoopValue,
31
+ useValueEffect: Noop,
32
+ useRawData: Noop,
33
+ useData: Noop,
34
+ useFont: () => Skia.Font(undefined, 0),
35
+ useTypeface: () => null,
36
+ useImage: () => null,
37
+ useSVG: () => null,
38
+ };
102
39
  };
@@ -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