@shopify/react-native-skia 0.1.210 → 0.1.212

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. package/cpp/api/JsiSkAnimatedImage.h +60 -0
  2. package/cpp/api/JsiSkAnimatedImageFactory.h +39 -0
  3. package/cpp/api/JsiSkApi.h +4 -0
  4. package/cpp/api/JsiSkImage.h +15 -23
  5. package/cpp/api/JsiSkPath.h +66 -22
  6. package/cpp/skia/src/core/SkPathPriv.h +540 -0
  7. package/lib/commonjs/external/reanimated/moduleWrapper.d.ts +2 -0
  8. package/lib/commonjs/external/reanimated/moduleWrapper.js +11 -11
  9. package/lib/commonjs/external/reanimated/moduleWrapper.js.map +1 -1
  10. package/lib/commonjs/skia/core/AnimatedImage.d.ts +6 -0
  11. package/lib/commonjs/skia/core/AnimatedImage.js +65 -0
  12. package/lib/commonjs/skia/core/AnimatedImage.js.map +1 -0
  13. package/lib/commonjs/skia/core/Font.js +7 -6
  14. package/lib/commonjs/skia/core/Font.js.map +1 -1
  15. package/lib/commonjs/skia/core/index.d.ts +1 -0
  16. package/lib/commonjs/skia/core/index.js +13 -0
  17. package/lib/commonjs/skia/core/index.js.map +1 -1
  18. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImage.d.ts +24 -0
  19. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImage.js +6 -0
  20. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImage.js.map +1 -0
  21. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImageFactory.d.ts +13 -0
  22. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImageFactory.js +6 -0
  23. package/lib/commonjs/skia/types/AnimatedImage/AnimatedImageFactory.js.map +1 -0
  24. package/lib/commonjs/skia/types/AnimatedImage/index.d.ts +2 -0
  25. package/lib/commonjs/skia/types/AnimatedImage/index.js +32 -0
  26. package/lib/commonjs/skia/types/AnimatedImage/index.js.map +1 -0
  27. package/lib/commonjs/skia/types/Skia.d.ts +2 -0
  28. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  29. package/lib/commonjs/skia/types/index.d.ts +1 -0
  30. package/lib/commonjs/skia/types/index.js +13 -0
  31. package/lib/commonjs/skia/types/index.js.map +1 -1
  32. package/lib/commonjs/skia/web/JsiSkAnimatedImage.d.ts +11 -0
  33. package/lib/commonjs/skia/web/JsiSkAnimatedImage.js +44 -0
  34. package/lib/commonjs/skia/web/JsiSkAnimatedImage.js.map +1 -0
  35. package/lib/commonjs/skia/web/JsiSkAnimatedImageFactory.d.ts +9 -0
  36. package/lib/commonjs/skia/web/JsiSkAnimatedImageFactory.js +32 -0
  37. package/lib/commonjs/skia/web/JsiSkAnimatedImageFactory.js.map +1 -0
  38. package/lib/commonjs/skia/web/JsiSkia.js +3 -0
  39. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  40. package/lib/module/external/reanimated/moduleWrapper.d.ts +2 -0
  41. package/lib/module/external/reanimated/moduleWrapper.js +8 -11
  42. package/lib/module/external/reanimated/moduleWrapper.js.map +1 -1
  43. package/lib/module/skia/core/AnimatedImage.d.ts +6 -0
  44. package/lib/module/skia/core/AnimatedImage.js +49 -0
  45. package/lib/module/skia/core/AnimatedImage.js.map +1 -0
  46. package/lib/module/skia/core/Font.js +6 -5
  47. package/lib/module/skia/core/Font.js.map +1 -1
  48. package/lib/module/skia/core/index.d.ts +1 -0
  49. package/lib/module/skia/core/index.js +1 -0
  50. package/lib/module/skia/core/index.js.map +1 -1
  51. package/lib/module/skia/types/AnimatedImage/AnimatedImage.d.ts +24 -0
  52. package/lib/module/skia/types/AnimatedImage/AnimatedImage.js +2 -0
  53. package/lib/module/skia/types/AnimatedImage/AnimatedImage.js.map +1 -0
  54. package/lib/module/skia/types/AnimatedImage/AnimatedImageFactory.d.ts +13 -0
  55. package/lib/module/skia/types/AnimatedImage/AnimatedImageFactory.js +2 -0
  56. package/lib/module/skia/types/AnimatedImage/AnimatedImageFactory.js.map +1 -0
  57. package/lib/module/skia/types/AnimatedImage/index.d.ts +2 -0
  58. package/lib/module/skia/types/AnimatedImage/index.js +3 -0
  59. package/lib/module/skia/types/AnimatedImage/index.js.map +1 -0
  60. package/lib/module/skia/types/Skia.d.ts +2 -0
  61. package/lib/module/skia/types/Skia.js.map +1 -1
  62. package/lib/module/skia/types/index.d.ts +1 -0
  63. package/lib/module/skia/types/index.js +1 -0
  64. package/lib/module/skia/types/index.js.map +1 -1
  65. package/lib/module/skia/web/JsiSkAnimatedImage.d.ts +11 -0
  66. package/lib/module/skia/web/JsiSkAnimatedImage.js +33 -0
  67. package/lib/module/skia/web/JsiSkAnimatedImage.js.map +1 -0
  68. package/lib/module/skia/web/JsiSkAnimatedImageFactory.d.ts +9 -0
  69. package/lib/module/skia/web/JsiSkAnimatedImageFactory.js +20 -0
  70. package/lib/module/skia/web/JsiSkAnimatedImageFactory.js.map +1 -0
  71. package/lib/module/skia/web/JsiSkia.js +2 -0
  72. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  73. package/lib/typescript/src/external/reanimated/moduleWrapper.d.ts +2 -0
  74. package/lib/typescript/src/skia/core/AnimatedImage.d.ts +6 -0
  75. package/lib/typescript/src/skia/core/index.d.ts +1 -0
  76. package/lib/typescript/src/skia/types/AnimatedImage/AnimatedImage.d.ts +24 -0
  77. package/lib/typescript/src/skia/types/AnimatedImage/AnimatedImageFactory.d.ts +13 -0
  78. package/lib/typescript/src/skia/types/AnimatedImage/index.d.ts +2 -0
  79. package/lib/typescript/src/skia/types/Skia.d.ts +2 -0
  80. package/lib/typescript/src/skia/types/index.d.ts +1 -0
  81. package/lib/typescript/src/skia/web/JsiSkAnimatedImage.d.ts +11 -0
  82. package/lib/typescript/src/skia/web/JsiSkAnimatedImageFactory.d.ts +9 -0
  83. package/package.json +1 -1
  84. package/src/external/reanimated/moduleWrapper.ts +9 -8
  85. package/src/skia/core/AnimatedImage.ts +63 -0
  86. package/src/skia/core/Font.ts +5 -5
  87. package/src/skia/core/index.ts +1 -0
  88. package/src/skia/types/AnimatedImage/AnimatedImage.ts +29 -0
  89. package/src/skia/types/AnimatedImage/AnimatedImageFactory.ts +15 -0
  90. package/src/skia/types/AnimatedImage/index.ts +2 -0
  91. package/src/skia/types/Skia.ts +3 -0
  92. package/src/skia/types/index.ts +1 -0
  93. package/src/skia/web/JsiSkAnimatedImage.ts +35 -0
  94. package/src/skia/web/JsiSkAnimatedImageFactory.ts +27 -0
  95. package/src/skia/web/JsiSkia.ts +2 -0
@@ -0,0 +1,60 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <string>
5
+ #include <utility>
6
+
7
+ #include "JsiSkHostObjects.h"
8
+
9
+ #pragma clang diagnostic push
10
+ #pragma clang diagnostic ignored "-Wdocumentation"
11
+
12
+ #include "JsiSkImage.h"
13
+ #include "SkBase64.h"
14
+ #include "SkStream.h"
15
+ #include "include/codec/SkEncodedImageFormat.h"
16
+
17
+ #include "include/android/SkAnimatedImage.h"
18
+ #include "include/codec/SkAndroidCodec.h"
19
+
20
+ #pragma clang diagnostic pop
21
+
22
+ #include <jsi/jsi.h>
23
+
24
+ namespace RNSkia {
25
+
26
+ namespace jsi = facebook::jsi;
27
+
28
+ class JsiSkAnimatedImage
29
+ : public JsiSkWrappingSkPtrHostObject<SkAnimatedImage> {
30
+ public:
31
+ // TODO-API: Properties?
32
+ JSI_HOST_FUNCTION(getCurrentFrame) {
33
+ auto image = getObject()->getCurrentFrame();
34
+ return jsi::Object::createFromHostObject(
35
+ runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
36
+ }
37
+
38
+ JSI_HOST_FUNCTION(currentFrameDuration) {
39
+ return static_cast<int>(getObject()->currentFrameDuration());
40
+ }
41
+
42
+ JSI_HOST_FUNCTION(decodeNextFrame) {
43
+ return static_cast<int>(getObject()->decodeNextFrame());
44
+ }
45
+
46
+ EXPORT_JSI_API_TYPENAME(JsiSkAnimatedImage, "AnimatedImage")
47
+
48
+ JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImage, dispose),
49
+ JSI_EXPORT_FUNC(JsiSkAnimatedImage, getCurrentFrame),
50
+ JSI_EXPORT_FUNC(JsiSkAnimatedImage,
51
+ currentFrameDuration),
52
+ JSI_EXPORT_FUNC(JsiSkAnimatedImage, decodeNextFrame))
53
+
54
+ JsiSkAnimatedImage(std::shared_ptr<RNSkPlatformContext> context,
55
+ const sk_sp<SkAnimatedImage> image)
56
+ : JsiSkWrappingSkPtrHostObject<SkAnimatedImage>(std::move(context),
57
+ std::move(image)) {}
58
+ };
59
+
60
+ } // namespace RNSkia
@@ -0,0 +1,39 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <utility>
5
+
6
+ #include <jsi/jsi.h>
7
+
8
+ #include "JsiPromises.h"
9
+ #include "JsiSkAnimatedImage.h"
10
+ #include "JsiSkData.h"
11
+ #include "JsiSkHostObjects.h"
12
+
13
+ namespace RNSkia {
14
+
15
+ namespace jsi = facebook::jsi;
16
+
17
+ class JsiSkAnimatedImageFactory : public JsiSkHostObject {
18
+ public:
19
+ JSI_HOST_FUNCTION(MakeAnimatedImageFromEncoded) {
20
+ auto data = JsiSkData::fromValue(runtime, arguments[0]);
21
+ auto codec = SkAndroidCodec::MakeFromData(data);
22
+ auto image = SkAnimatedImage::Make(std::move(codec));
23
+ if (image == nullptr) {
24
+ return jsi::Value::null();
25
+ }
26
+ return jsi::Object::createFromHostObject(
27
+ runtime,
28
+ std::make_shared<JsiSkAnimatedImage>(getContext(), std::move(image)));
29
+ }
30
+
31
+ JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImageFactory,
32
+ MakeAnimatedImageFromEncoded))
33
+
34
+ explicit JsiSkAnimatedImageFactory(
35
+ std::shared_ptr<RNSkPlatformContext> context)
36
+ : JsiSkHostObject(std::move(context)) {}
37
+ };
38
+
39
+ } // namespace RNSkia
@@ -6,6 +6,8 @@
6
6
 
7
7
  #include "JsiSkHostObjects.h"
8
8
 
9
+ #include "JsiSkAnimatedImage.h"
10
+ #include "JsiSkAnimatedImageFactory.h"
9
11
  #include "JsiSkColor.h"
10
12
  #include "JsiSkColorFilter.h"
11
13
  #include "JsiSkColorFilterFactory.h"
@@ -79,6 +81,8 @@ public:
79
81
  installReadonlyProperty("SVG", std::make_shared<JsiSkSVGFactory>(context));
80
82
  installReadonlyProperty("Image",
81
83
  std::make_shared<JsiSkImageFactory>(context));
84
+ installReadonlyProperty(
85
+ "AnimatedImage", std::make_shared<JsiSkAnimatedImageFactory>(context));
82
86
  installReadonlyProperty("Typeface",
83
87
  std::make_shared<JsiSkTypefaceFactory>(context));
84
88
  installReadonlyProperty("Data",
@@ -64,23 +64,32 @@ public:
64
64
  std::make_shared<JsiSkShader>(getContext(), std::move(shader)));
65
65
  }
66
66
 
67
- JSI_HOST_FUNCTION(encodeToBytes) {
67
+ sk_sp<SkData> encodeImageData(const jsi::Value *arguments, size_t count) {
68
68
  // Get optional parameters
69
69
  auto format =
70
70
  count >= 1 ? static_cast<SkEncodedImageFormat>(arguments[0].asNumber())
71
71
  : SkEncodedImageFormat::kPNG;
72
- auto quality = count == 2 ? arguments[1].asNumber() : 100.0;
73
72
 
74
- // Get data
73
+ auto quality = count == 2 ? arguments[1].asNumber() : 100.0;
74
+ auto image = getObject();
75
+ if (image->isTextureBacked()) {
76
+ image = image->makeNonTextureImage();
77
+ }
75
78
  sk_sp<SkData> data;
76
79
  if (format == SkEncodedImageFormat::kJPEG) {
77
80
  SkJpegEncoder::Options options;
78
81
  options.fQuality = quality;
79
- data = SkJpegEncoder::Encode(nullptr, getObject().get(), options);
82
+ data = SkJpegEncoder::Encode(nullptr, image.get(), options);
80
83
  } else {
81
84
  SkPngEncoder::Options options;
82
- data = SkPngEncoder::Encode(nullptr, getObject().get(), options);
85
+ data = SkPngEncoder::Encode(nullptr, image.get(), options);
83
86
  }
87
+ return data;
88
+ }
89
+
90
+ JSI_HOST_FUNCTION(encodeToBytes) {
91
+ auto data = encodeImageData(arguments, count);
92
+
84
93
  auto arrayCtor =
85
94
  runtime.global().getPropertyAsFunction(runtime, "Uint8Array");
86
95
  size_t size = data->size();
@@ -99,25 +108,8 @@ public:
99
108
  }
100
109
 
101
110
  JSI_HOST_FUNCTION(encodeToBase64) {
102
- // Get optional parameters
103
- auto format =
104
- count >= 1 ? static_cast<SkEncodedImageFormat>(arguments[0].asNumber())
105
- : SkEncodedImageFormat::kPNG;
111
+ auto data = encodeImageData(arguments, count);
106
112
 
107
- auto quality = count == 2 ? arguments[1].asNumber() : 100.0;
108
- auto image = getObject();
109
- if (image->isTextureBacked()) {
110
- image = image->makeNonTextureImage();
111
- }
112
- sk_sp<SkData> data;
113
- if (format == SkEncodedImageFormat::kJPEG) {
114
- SkJpegEncoder::Options options;
115
- options.fQuality = quality;
116
- data = SkJpegEncoder::Encode(nullptr, image.get(), options);
117
- } else {
118
- SkPngEncoder::Options options;
119
- data = SkPngEncoder::Encode(nullptr, image.get(), options);
120
- }
121
113
  auto len = SkBase64::Encode(data->bytes(), data->size(), nullptr);
122
114
  auto buffer = std::string(len, 0);
123
115
  SkBase64::Encode(data->bytes(), data->size(),
@@ -27,6 +27,7 @@
27
27
  #include "SkStrokeRec.h"
28
28
  #include "SkTextUtils.h"
29
29
  #include "SkTrimPathEffect.h"
30
+ #include "src/core/SkPathPriv.h"
30
31
 
31
32
  #pragma clang diagnostic pop
32
33
 
@@ -36,6 +37,13 @@ namespace jsi = facebook::jsi;
36
37
 
37
38
  class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
38
39
  private:
40
+ static const int MOVE = 0;
41
+ static const int LINE = 1;
42
+ static const int QUAD = 2;
43
+ static const int CONIC = 3;
44
+ static const int CUBIC = 4;
45
+ static const int CLOSE = 5;
46
+
39
47
  float pinT(double value) {
40
48
  // Clamp the double value between 0 and 1 and then cast it to float
41
49
  return static_cast<float>(std::clamp(value, 0.0, 1.0));
@@ -498,30 +506,66 @@ public:
498
506
  JSI_HOST_FUNCTION(toCmds) {
499
507
  auto path = *getObject();
500
508
  auto cmds = jsi::Array(runtime, path.countVerbs());
501
- auto it = SkPath::Iter(path, false);
502
- // { "Move", "Line", "Quad", "Conic", "Cubic",
503
- // "Close", "Done" };
504
- const int pointCount[] = {1, 1, 2, 2, 3, 0, 0};
505
- const int cmdCount[] = {3, 3, 5, 6, 7, 1, 0};
506
- SkPoint points[4];
507
- SkPath::Verb verb;
508
- auto k = 0;
509
- while (SkPath::kDone_Verb != (verb = it.next(points))) {
510
- auto verbVal = static_cast<int>(verb);
511
- auto cmd = jsi::Array(runtime, cmdCount[verbVal]);
512
- auto j = 0;
513
- cmd.setValueAtIndex(runtime, j++, jsi::Value(verbVal));
514
- for (int i = 0; i < pointCount[verbVal]; ++i) {
515
- cmd.setValueAtIndex(runtime, j++,
516
- jsi::Value(static_cast<double>(points[1 + i].fX)));
517
- cmd.setValueAtIndex(runtime, j++,
518
- jsi::Value(static_cast<double>(points[1 + i].fY)));
509
+ auto j = 0;
510
+ for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
511
+ switch (verb) {
512
+ case SkPathVerb::kMove: {
513
+ auto cmd = jsi::Array(runtime, 3);
514
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(MOVE));
515
+ cmd.setValueAtIndex(runtime, 1, static_cast<double>(pts[0].x()));
516
+ cmd.setValueAtIndex(runtime, 2, static_cast<double>(pts[0].y()));
517
+ cmds.setValueAtIndex(runtime, j, cmd);
518
+ break;
519
+ }
520
+ case SkPathVerb::kLine: {
521
+ auto cmd = jsi::Array(runtime, 3);
522
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(LINE));
523
+ cmd.setValueAtIndex(runtime, 1, static_cast<double>(pts[1].x()));
524
+ cmd.setValueAtIndex(runtime, 2, static_cast<double>(pts[1].y()));
525
+ cmds.setValueAtIndex(runtime, j, cmd);
526
+ break;
527
+ }
528
+ case SkPathVerb::kQuad: {
529
+ auto cmd = jsi::Array(runtime, 5);
530
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(QUAD));
531
+ cmd.setValueAtIndex(runtime, 1, static_cast<double>(pts[1].x()));
532
+ cmd.setValueAtIndex(runtime, 2, static_cast<double>(pts[1].y()));
533
+ cmd.setValueAtIndex(runtime, 3, static_cast<double>(pts[2].x()));
534
+ cmd.setValueAtIndex(runtime, 4, static_cast<double>(pts[2].y()));
535
+ cmds.setValueAtIndex(runtime, j, cmd);
536
+ break;
537
+ }
538
+ case SkPathVerb::kConic: {
539
+ auto cmd = jsi::Array(runtime, 6);
540
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(CONIC));
541
+ cmd.setValueAtIndex(runtime, 1, static_cast<double>(pts[1].x()));
542
+ cmd.setValueAtIndex(runtime, 2, static_cast<double>(pts[1].y()));
543
+ cmd.setValueAtIndex(runtime, 3, static_cast<double>(pts[2].x()));
544
+ cmd.setValueAtIndex(runtime, 4, static_cast<double>(pts[2].y()));
545
+ cmd.setValueAtIndex(runtime, 5, static_cast<double>(*w));
546
+ cmds.setValueAtIndex(runtime, j, cmd);
547
+ break;
548
+ }
549
+ case SkPathVerb::kCubic: {
550
+ auto cmd = jsi::Array(runtime, 7);
551
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(CUBIC));
552
+ cmd.setValueAtIndex(runtime, 1, static_cast<double>(pts[1].x()));
553
+ cmd.setValueAtIndex(runtime, 2, static_cast<double>(pts[1].y()));
554
+ cmd.setValueAtIndex(runtime, 3, static_cast<double>(pts[2].x()));
555
+ cmd.setValueAtIndex(runtime, 4, static_cast<double>(pts[2].y()));
556
+ cmd.setValueAtIndex(runtime, 5, static_cast<double>(pts[3].x()));
557
+ cmd.setValueAtIndex(runtime, 6, static_cast<double>(pts[3].y()));
558
+ cmds.setValueAtIndex(runtime, j, cmd);
559
+ break;
560
+ }
561
+ case SkPathVerb::kClose: {
562
+ auto cmd = jsi::Array(runtime, 1);
563
+ cmd.setValueAtIndex(runtime, 0, static_cast<double>(CLOSE));
564
+ cmds.setValueAtIndex(runtime, j, cmd);
565
+ break;
519
566
  }
520
- if (SkPath::kConic_Verb == verb) {
521
- cmd.setValueAtIndex(runtime, j,
522
- jsi::Value(static_cast<double>(it.conicWeight())));
523
567
  }
524
- cmds.setValueAtIndex(runtime, k++, cmd);
568
+ j++;
525
569
  }
526
570
  return cmds;
527
571
  }