@shopify/react-native-skia 0.1.155 → 0.1.156

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. package/android/cpp/jni/JniLoad.cpp +5 -5
  2. package/android/cpp/jni/JniPlatformContext.cpp +107 -119
  3. package/android/cpp/jni/JniSkiaManager.cpp +18 -20
  4. package/android/cpp/jni/include/JniPlatformContext.h +41 -45
  5. package/android/cpp/jni/include/JniSkiaBaseView.h +52 -55
  6. package/android/cpp/jni/include/JniSkiaDrawView.h +72 -77
  7. package/android/cpp/jni/include/JniSkiaManager.h +51 -53
  8. package/android/cpp/jni/include/JniSkiaPictureView.h +74 -78
  9. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +36 -45
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +87 -92
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +62 -65
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +20 -17
  13. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +257 -313
  14. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +107 -110
  15. package/cpp/api/JsiSkApi.h +66 -62
  16. package/cpp/api/JsiSkCanvas.h +38 -30
  17. package/cpp/api/JsiSkColor.h +58 -56
  18. package/cpp/api/JsiSkColorFilter.h +5 -3
  19. package/cpp/api/JsiSkColorFilterFactory.h +23 -21
  20. package/cpp/api/JsiSkContourMeasure.h +74 -85
  21. package/cpp/api/JsiSkContourMeasureIter.h +68 -75
  22. package/cpp/api/JsiSkData.h +16 -22
  23. package/cpp/api/JsiSkDataFactory.h +86 -79
  24. package/cpp/api/JsiSkFont.h +286 -311
  25. package/cpp/api/JsiSkHostObjects.h +15 -16
  26. package/cpp/api/JsiSkImage.h +107 -103
  27. package/cpp/api/JsiSkImageFactory.h +34 -36
  28. package/cpp/api/JsiSkImageFilter.h +5 -3
  29. package/cpp/api/JsiSkImageFilterFactory.h +71 -68
  30. package/cpp/api/JsiSkImageInfo.h +41 -38
  31. package/cpp/api/JsiSkMaskFilter.h +5 -3
  32. package/cpp/api/JsiSkMaskFilterFactory.h +2 -3
  33. package/cpp/api/JsiSkMatrix.h +26 -36
  34. package/cpp/api/JsiSkPaint.h +20 -13
  35. package/cpp/api/JsiSkPath.h +70 -85
  36. package/cpp/api/JsiSkPathEffect.h +5 -3
  37. package/cpp/api/JsiSkPathEffectFactory.h +33 -28
  38. package/cpp/api/JsiSkPathFactory.h +68 -67
  39. package/cpp/api/JsiSkPicture.h +28 -22
  40. package/cpp/api/JsiSkPictureFactory.h +13 -12
  41. package/cpp/api/JsiSkPictureRecorder.h +21 -19
  42. package/cpp/api/JsiSkPoint.h +6 -8
  43. package/cpp/api/JsiSkRRect.h +11 -7
  44. package/cpp/api/JsiSkRSXform.h +82 -85
  45. package/cpp/api/JsiSkRect.h +9 -9
  46. package/cpp/api/JsiSkRuntimeEffect.h +182 -186
  47. package/cpp/api/JsiSkRuntimeEffectFactory.h +10 -11
  48. package/cpp/api/JsiSkRuntimeShaderBuilder.h +64 -61
  49. package/cpp/api/JsiSkSVG.h +4 -5
  50. package/cpp/api/JsiSkSVGFactory.h +28 -27
  51. package/cpp/api/JsiSkShader.h +3 -2
  52. package/cpp/api/JsiSkShaderFactory.h +37 -25
  53. package/cpp/api/JsiSkSurface.h +44 -40
  54. package/cpp/api/JsiSkSurfaceFactory.h +22 -22
  55. package/cpp/api/JsiSkTextBlob.h +28 -31
  56. package/cpp/api/JsiSkTextBlobFactory.h +88 -87
  57. package/cpp/api/JsiSkTypeface.h +6 -5
  58. package/cpp/api/JsiSkTypefaceFactory.h +22 -21
  59. package/cpp/api/JsiSkVertices.h +137 -124
  60. package/cpp/api/third_party/CSSColorParser.cpp +161 -174
  61. package/cpp/api/third_party/CSSColorParser.h +172 -96
  62. package/cpp/jsi/JsiHostObject.cpp +11 -9
  63. package/cpp/jsi/JsiHostObject.h +31 -24
  64. package/cpp/jsi/JsiSimpleValueWrapper.h +74 -83
  65. package/cpp/jsi/JsiValueWrapper.h +52 -54
  66. package/cpp/rnskia/RNSkAnimation.h +26 -29
  67. package/cpp/rnskia/RNSkDispatchQueue.cpp +50 -61
  68. package/cpp/rnskia/RNSkDispatchQueue.h +3 -1
  69. package/cpp/rnskia/RNSkInfoParameter.h +12 -12
  70. package/cpp/rnskia/RNSkJsView.cpp +82 -81
  71. package/cpp/rnskia/RNSkJsView.h +45 -41
  72. package/cpp/rnskia/RNSkJsiViewApi.h +99 -89
  73. package/cpp/rnskia/RNSkManager.cpp +8 -7
  74. package/cpp/rnskia/RNSkManager.h +8 -6
  75. package/cpp/rnskia/RNSkPictureView.h +44 -37
  76. package/cpp/rnskia/RNSkPlatformContext.h +39 -28
  77. package/cpp/rnskia/RNSkValueApi.h +33 -34
  78. package/cpp/rnskia/RNSkView.h +108 -93
  79. package/cpp/rnskia/values/RNSkClockValue.h +63 -64
  80. package/cpp/rnskia/values/RNSkComputedValue.h +32 -30
  81. package/cpp/rnskia/values/RNSkReadonlyValue.h +60 -59
  82. package/cpp/rnskia/values/RNSkValue.h +38 -40
  83. package/cpp/utils/RNSkLog.h +9 -7
  84. package/cpp/utils/RNSkMeasureTime.h +7 -7
  85. package/cpp/utils/RNSkTimingInfo.h +27 -24
  86. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +8 -9
  87. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +24 -23
  88. package/ios/RNSkia-iOS/RNSkiOSView.h +16 -13
  89. package/ios/RNSkia-iOS/SkiaUIView.h +9 -8
  90. package/lib/commonjs/dom/nodes/JsiSkDOM.js +6 -0
  91. package/lib/commonjs/dom/nodes/JsiSkDOM.js.map +1 -1
  92. package/lib/commonjs/dom/nodes/LayerNode.js +43 -0
  93. package/lib/commonjs/dom/nodes/LayerNode.js.map +1 -0
  94. package/lib/commonjs/dom/types/NodeType.js +1 -0
  95. package/lib/commonjs/dom/types/NodeType.js.map +1 -1
  96. package/lib/commonjs/dom/types/SkDOM.js.map +1 -1
  97. package/lib/commonjs/renderer/HostComponents.js +3 -0
  98. package/lib/commonjs/renderer/HostComponents.js.map +1 -1
  99. package/lib/commonjs/renderer/components/Group.js +19 -4
  100. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  101. package/lib/commonjs/renderer/components/Paint.js +6 -1
  102. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  103. package/lib/commonjs/views/SkiaPictureView.js +3 -2
  104. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  105. package/lib/commonjs/views/SkiaView.js +6 -3
  106. package/lib/commonjs/views/SkiaView.js.map +1 -1
  107. package/lib/module/dom/nodes/JsiSkDOM.js +5 -0
  108. package/lib/module/dom/nodes/JsiSkDOM.js.map +1 -1
  109. package/lib/module/dom/nodes/LayerNode.js +32 -0
  110. package/lib/module/dom/nodes/LayerNode.js.map +1 -0
  111. package/lib/module/dom/types/NodeType.js +1 -0
  112. package/lib/module/dom/types/NodeType.js.map +1 -1
  113. package/lib/module/dom/types/SkDOM.js.map +1 -1
  114. package/lib/module/renderer/HostComponents.js +3 -0
  115. package/lib/module/renderer/HostComponents.js.map +1 -1
  116. package/lib/module/renderer/components/Group.js +16 -3
  117. package/lib/module/renderer/components/Group.js.map +1 -1
  118. package/lib/module/renderer/components/Paint.js +7 -1
  119. package/lib/module/renderer/components/Paint.js.map +1 -1
  120. package/lib/module/views/SkiaPictureView.js +2 -2
  121. package/lib/module/views/SkiaPictureView.js.map +1 -1
  122. package/lib/module/views/SkiaView.js +4 -2
  123. package/lib/module/views/SkiaView.js.map +1 -1
  124. package/lib/typescript/src/dom/nodes/JsiSkDOM.d.ts +2 -0
  125. package/lib/typescript/src/dom/nodes/LayerNode.d.ts +8 -0
  126. package/lib/typescript/src/dom/types/NodeType.d.ts +1 -0
  127. package/lib/typescript/src/dom/types/SkDOM.d.ts +1 -0
  128. package/lib/typescript/src/renderer/HostComponents.d.ts +2 -1
  129. package/lib/typescript/src/renderer/components/Group.d.ts +5 -1
  130. package/lib/typescript/src/views/SkiaView.d.ts +3 -0
  131. package/libs/android/arm64-v8a/libskottie.a +0 -0
  132. package/libs/android/arm64-v8a/libsksg.a +0 -0
  133. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  134. package/libs/android/armeabi-v7a/libsksg.a +0 -0
  135. package/libs/android/x86/libskottie.a +0 -0
  136. package/libs/android/x86/libsksg.a +0 -0
  137. package/libs/android/x86_64/libskottie.a +0 -0
  138. package/libs/android/x86_64/libsksg.a +0 -0
  139. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  140. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  141. package/libs/ios/libskottie.xcframework/Info.plist +42 -0
  142. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  143. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  144. package/libs/ios/libsksg.xcframework/Info.plist +42 -0
  145. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  146. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  147. package/libs/ios/libskshaper.xcframework/Info.plist +5 -5
  148. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  149. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  150. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  151. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  152. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  153. package/package.json +3 -1
  154. package/src/dom/nodes/JsiSkDOM.ts +5 -0
  155. package/src/dom/nodes/LayerNode.ts +35 -0
  156. package/src/dom/types/NodeType.ts +1 -0
  157. package/src/dom/types/SkDOM.ts +1 -0
  158. package/src/renderer/HostComponents.ts +3 -0
  159. package/src/renderer/components/Group.tsx +16 -3
  160. package/src/renderer/components/Paint.tsx +7 -1
  161. package/src/views/SkiaPictureView.tsx +2 -3
  162. package/src/views/SkiaView.tsx +2 -2
@@ -1,19 +1,18 @@
1
1
  #pragma once
2
2
 
3
3
  #include <memory>
4
+ #include <numeric>
4
5
  #include <utility>
5
6
  #include <vector>
6
- #include <numeric>
7
7
 
8
- #include <jsi/jsi.h>
9
8
  #include "JsiSkHostObjects.h"
10
9
  #include <RNSkLog.h>
10
+ #include <jsi/jsi.h>
11
11
 
12
12
  #include "JsiSkPaint.h"
13
+ #include "JsiSkPoint.h"
13
14
  #include "JsiSkRect.h"
14
15
  #include "JsiSkTypeface.h"
15
- #include "JsiSkPoint.h"
16
-
17
16
 
18
17
  #pragma clang diagnostic push
19
18
  #pragma clang diagnostic ignored "-Wdocumentation"
@@ -23,313 +22,289 @@
23
22
 
24
23
  #pragma clang diagnostic pop
25
24
 
26
-
27
- namespace RNSkia
28
- {
29
-
30
- using namespace facebook;
31
-
32
- class JsiSkFont : public JsiSkWrappingSharedPtrHostObject<SkFont>
33
- {
34
- public:
35
- // TODO: declare in JsiSkWrappingSkPtrHostObject via extra template parameter?
36
- JSI_PROPERTY_GET(__typename__)
37
- {
38
- return jsi::String::createFromUtf8(runtime, "Font");
39
- }
40
-
41
- JSI_HOST_FUNCTION(getGlyphWidths)
42
- {
43
- auto jsiGlyphs = arguments[0].asObject(runtime).asArray(runtime);
44
- std::vector<SkGlyphID> glyphs;
45
- int glyphsSize = static_cast<int>(jsiGlyphs.size(runtime));
46
-
47
- std::vector<SkScalar> widthPtrs;
48
- widthPtrs.resize(glyphsSize);
49
-
50
- glyphs.reserve(glyphsSize);
51
- for (int i = 0; i < glyphsSize; i++)
52
- {
53
- glyphs.push_back(jsiGlyphs.getValueAtIndex(runtime, i).asNumber());
54
- }
55
- if (count > 1)
56
- {
57
- auto paint = JsiSkPaint::fromValue(runtime, arguments[1]);
58
- getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, paint.get());
59
- }
60
- else
61
- {
62
- getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, nullptr);
63
- }
64
- auto jsiWidths = jsi::Array(runtime, glyphsSize);
65
- for (int i = 0; i < glyphsSize; i++)
66
- {
67
- jsiWidths.setValueAtIndex(runtime, i, jsi::Value(SkScalarToDouble(static_cast<SkScalar *>(widthPtrs.data())[i])));
68
- }
69
- return jsiWidths;
70
- }
71
-
72
- JSI_HOST_FUNCTION(getTextWidth) {
73
- auto str = arguments[0].asString(runtime).utf8(runtime);
74
- auto numGlyphIDs = getObject()->countText(str.c_str(), str.length(), SkTextEncoding::kUTF8);
75
- std::vector<SkGlyphID> glyphs;
76
- glyphs.resize(numGlyphIDs);
77
- int glyphsSize = static_cast<int>(numGlyphIDs);
78
- getObject()->textToGlyphs(str.c_str(), str.length(), SkTextEncoding::kUTF8,
79
- static_cast<SkGlyphID *>(glyphs.data()), glyphsSize);
80
- std::vector<SkScalar> widthPtrs;
81
- widthPtrs.resize(numGlyphIDs);
82
- if (count > 1)
83
- {
84
- auto paint = JsiSkPaint::fromValue(runtime, arguments[1]);
85
- getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, paint.get());
86
- }
87
- else
88
- {
89
- getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, nullptr);
90
- }
91
- return jsi::Value(std::accumulate(widthPtrs.begin(), widthPtrs.end(), 0));
92
- }
93
-
94
- JSI_HOST_FUNCTION(getMetrics)
95
- {
96
- SkFontMetrics fm;
97
- getObject()->getMetrics(&fm);
98
- auto metrics = jsi::Object(runtime);
99
- metrics.setProperty(runtime, "ascent", fm.fAscent);
100
- metrics.setProperty(runtime, "descent", fm.fDescent);
101
- metrics.setProperty(runtime, "leading", fm.fLeading);
102
- if (!(fm.fFlags & SkFontMetrics::kBoundsInvalid_Flag))
103
- {
104
- auto bounds = SkRect::MakeLTRB(fm.fXMin, fm.fTop, fm.fXMax, fm.fBottom);
105
- auto jsiBounds = JsiSkRect::toValue(runtime, getContext(), std::move(bounds));
106
- metrics.setProperty(runtime, "bounds", std::move(jsiBounds));
107
- }
108
- return metrics;
109
- }
110
-
111
- JSI_HOST_FUNCTION(getGlyphIDs)
112
- {
113
- auto str = arguments[0].asString(runtime).utf8(runtime);
114
- int numGlyphIDs = count > 1 && !arguments[1].isNull() && !arguments[1].isUndefined()
115
- ? static_cast<int>(arguments[1].asNumber())
116
- : getObject()->countText(str.c_str(), str.length(), SkTextEncoding::kUTF8);
117
- std::vector<SkGlyphID> glyphIDs;
118
- glyphIDs.resize(numGlyphIDs);
119
- getObject()->textToGlyphs(str.c_str(), str.length(), SkTextEncoding::kUTF8,
120
- static_cast<SkGlyphID *>(glyphIDs.data()), numGlyphIDs);
121
- auto jsiGlyphIDs = jsi::Array(runtime, numGlyphIDs);
122
- for (int i = 0; i < numGlyphIDs; i++)
123
- {
124
- jsiGlyphIDs.setValueAtIndex(runtime, i, jsi::Value(static_cast<int>(glyphIDs[i])));
125
- }
126
- return jsiGlyphIDs;
127
- }
128
-
129
- JSI_HOST_FUNCTION(getGlyphIntercepts)
130
- {
131
- auto jsiGlyphs = arguments[0].asObject(runtime).asArray(runtime);
132
- auto jsiPositions = arguments[1].asObject(runtime).asArray(runtime);
133
- auto top = arguments[2].asNumber();
134
- auto bottom = arguments[3].asNumber();
135
- std::vector<SkPoint> positions;
136
- int pointsSize = static_cast<int>(jsiPositions.size(runtime));
137
- positions.reserve(pointsSize);
138
- for (int i = 0; i < pointsSize; i++)
139
- {
140
- std::shared_ptr<SkPoint> point = JsiSkPoint::fromValue(
141
- runtime, jsiPositions.getValueAtIndex(runtime, i).asObject(runtime));
142
- positions.push_back(*point.get());
143
- }
144
-
145
- std::vector<SkGlyphID> glyphs;
146
- int glyphsSize = static_cast<int>(jsiGlyphs.size(runtime));
147
- glyphs.reserve(glyphsSize);
148
- for (int i = 0; i < glyphsSize; i++)
149
- {
150
- glyphs.push_back(jsiGlyphs.getValueAtIndex(runtime, i).asNumber());
151
- }
152
-
153
- if (glyphs.size() > positions.size())
154
- {
155
- throw jsi::JSError(runtime, "Not enough x,y position pairs for glyphs");
156
- return jsi::Value::null();
157
- }
158
- auto sects = getObject()->getIntercepts(glyphs.data(), SkToInt(glyphs.size()), positions.data(), top, bottom);
159
- auto jsiSects = jsi::Array(runtime, sects.size());
160
- for (int i = 0; i < sects.size(); i++)
161
- {
162
- jsiSects.setValueAtIndex(runtime, i, jsi::Value(static_cast<int>(sects.at(i))));
163
- }
164
- return jsiSects;
165
- }
166
-
167
- JSI_HOST_FUNCTION(getScaleX)
168
- {
169
- return jsi::Value(SkScalarToDouble(getObject()->getScaleX()));
170
- }
171
-
172
- JSI_HOST_FUNCTION(getSize)
173
- {
174
- return jsi::Value(SkScalarToDouble(getObject()->getSize()));
175
- }
176
-
177
- JSI_HOST_FUNCTION(getSkewX)
178
- {
179
- return jsi::Value(SkScalarToDouble(getObject()->getSkewX()));
180
- }
181
-
182
- JSI_HOST_FUNCTION(isEmbolden)
183
- {
184
- return jsi::Value(getObject()->isEmbolden());
185
- }
186
-
187
- JSI_HOST_FUNCTION(getTypeface)
188
- {
189
- return JsiSkTypeface::toValue(runtime, getContext(), sk_sp<SkTypeface>(getObject()->getTypeface()));
190
- }
191
-
192
- JSI_HOST_FUNCTION(setEdging)
193
- {
194
- auto edging = arguments[0].asNumber();
195
- getObject()->setEdging(static_cast<SkFont::Edging>(edging));
196
- return jsi::Value::undefined();
197
- }
198
-
199
- JSI_HOST_FUNCTION(embeddedBitmaps)
200
- {
201
- auto embeddedBitmaps = arguments[0].getBool();
202
- getObject()->setEmbeddedBitmaps(embeddedBitmaps);
203
- return jsi::Value::undefined();
204
- }
205
-
206
- JSI_HOST_FUNCTION(setHinting)
207
- {
208
- auto hinting = arguments[0].asNumber();
209
- getObject()->setHinting(static_cast<SkFontHinting>(hinting));
210
- return jsi::Value::undefined();
211
- }
212
-
213
- JSI_HOST_FUNCTION(setLinearMetrics)
214
- {
215
- auto linearMetrics = arguments[0].getBool();
216
- getObject()->setLinearMetrics(linearMetrics);
217
- return jsi::Value::undefined();
218
- }
219
-
220
- JSI_HOST_FUNCTION(setScaleX)
221
- {
222
- auto scaleX = arguments[0].asNumber();
223
- getObject()->setScaleX(scaleX);
224
- return jsi::Value::undefined();
225
- }
226
-
227
- JSI_HOST_FUNCTION(setSkewX)
228
- {
229
- auto skewX = arguments[0].asNumber();
230
- getObject()->setSkewX(skewX);
231
- return jsi::Value::undefined();
232
- }
233
-
234
- JSI_HOST_FUNCTION(setSize)
235
- {
236
- auto size = arguments[0].asNumber();
237
- getObject()->setSize(size);
238
- return jsi::Value::undefined();
239
- }
240
-
241
- JSI_HOST_FUNCTION(setEmbolden)
242
- {
243
- auto embolden = arguments[0].asNumber();
244
- getObject()->setEmbolden(embolden);
245
- return jsi::Value::undefined();
246
- }
247
-
248
- JSI_HOST_FUNCTION(setSubpixel)
249
- {
250
- auto subpixel = arguments[0].asNumber();
251
- getObject()->setSubpixel(subpixel);
252
- return jsi::Value::undefined();
253
- }
254
-
255
- JSI_HOST_FUNCTION(setTypeface)
256
- {
257
- auto typeface = arguments[0].isNull() ? nullptr : JsiSkTypeface::fromValue(runtime, arguments[0]);
258
- getObject()->setTypeface(typeface);
259
- return jsi::Value::undefined();
260
- }
261
-
262
- JSI_EXPORT_FUNCTIONS(
263
- JSI_EXPORT_FUNC(JsiSkFont, getSize),
264
- JSI_EXPORT_FUNC(JsiSkFont, getMetrics),
265
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphIDs),
266
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphIntercepts),
267
- JSI_EXPORT_FUNC(JsiSkFont, getScaleX),
268
- JSI_EXPORT_FUNC(JsiSkFont, getSkewX),
269
- JSI_EXPORT_FUNC(JsiSkFont, getTypeface),
270
- JSI_EXPORT_FUNC(JsiSkFont, setEdging),
271
- JSI_EXPORT_FUNC(JsiSkFont, embeddedBitmaps),
272
- JSI_EXPORT_FUNC(JsiSkFont, setHinting),
273
- JSI_EXPORT_FUNC(JsiSkFont, setLinearMetrics),
274
- JSI_EXPORT_FUNC(JsiSkFont, setScaleX),
275
- JSI_EXPORT_FUNC(JsiSkFont, setSkewX),
276
- JSI_EXPORT_FUNC(JsiSkFont, setSize),
277
- JSI_EXPORT_FUNC(JsiSkFont, setEmbolden),
278
- JSI_EXPORT_FUNC(JsiSkFont, setSubpixel),
279
- JSI_EXPORT_FUNC(JsiSkFont, setTypeface),
280
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphWidths),
281
- JSI_EXPORT_FUNC(JsiSkFont, getTextWidth))
282
-
283
- JsiSkFont(std::shared_ptr<RNSkPlatformContext> context, const SkFont &font)
284
- : JsiSkWrappingSharedPtrHostObject(std::move(context),
285
- std::make_shared<SkFont>(font)){}
286
-
287
- /**
288
- Returns the underlying object from a host object of this type
289
- */
290
- static std::shared_ptr<SkFont> fromValue(jsi::Runtime &runtime,
291
- const jsi::Value &obj)
292
- {
293
- return obj.asObject(runtime)
294
- .asHostObject<JsiSkFont>(runtime)
295
- ->getObject();
296
- }
297
-
298
- /**
299
- * Creates the function for construction a new instance of the SkFont
300
- * wrapper
301
- * @param context Platform context
302
- * @return A function for creating a new host object wrapper for the SkFont
303
- * class
304
- */
305
- static const jsi::HostFunctionType
306
- createCtor(std::shared_ptr<RNSkPlatformContext> context)
307
- {
308
- return JSI_HOST_FUNCTION_LAMBDA
309
- {
310
- // Handle arguments
311
- if (count == 2)
312
- {
313
- auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
314
- auto size = arguments[1].asNumber();
315
- return jsi::Object::createFromHostObject(
316
- runtime,
317
- std::make_shared<JsiSkFont>(std::move(context), SkFont(typeface, size)));
318
- }
319
- else if (count == 1)
320
- {
321
- auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
322
- return jsi::Object::createFromHostObject(
323
- runtime, std::make_shared<JsiSkFont>(std::move(context), SkFont(typeface)));
324
- }
325
- else
326
- {
327
- // Return the newly constructed object
328
- return jsi::Object::createFromHostObject(
329
- runtime, std::make_shared<JsiSkFont>(std::move(context), SkFont()));
330
- }
331
- };
332
- }
25
+ namespace RNSkia {
26
+
27
+ namespace jsi = facebook::jsi;
28
+
29
+ class JsiSkFont : public JsiSkWrappingSharedPtrHostObject<SkFont> {
30
+ public:
31
+ // TODO: declare in JsiSkWrappingSkPtrHostObject via extra template parameter?
32
+ JSI_PROPERTY_GET(__typename__) {
33
+ return jsi::String::createFromUtf8(runtime, "Font");
34
+ }
35
+
36
+ JSI_HOST_FUNCTION(getGlyphWidths) {
37
+ auto jsiGlyphs = arguments[0].asObject(runtime).asArray(runtime);
38
+ std::vector<SkGlyphID> glyphs;
39
+ int glyphsSize = static_cast<int>(jsiGlyphs.size(runtime));
40
+
41
+ std::vector<SkScalar> widthPtrs;
42
+ widthPtrs.resize(glyphsSize);
43
+
44
+ glyphs.reserve(glyphsSize);
45
+ for (int i = 0; i < glyphsSize; i++) {
46
+ glyphs.push_back(jsiGlyphs.getValueAtIndex(runtime, i).asNumber());
47
+ }
48
+ if (count > 1) {
49
+ auto paint = JsiSkPaint::fromValue(runtime, arguments[1]);
50
+ getObject()->getWidthsBounds(glyphs.data(), glyphsSize,
51
+ static_cast<SkScalar *>(widthPtrs.data()),
52
+ nullptr, paint.get());
53
+ } else {
54
+ getObject()->getWidthsBounds(glyphs.data(), glyphsSize,
55
+ static_cast<SkScalar *>(widthPtrs.data()),
56
+ nullptr, nullptr);
57
+ }
58
+ auto jsiWidths = jsi::Array(runtime, glyphsSize);
59
+ for (int i = 0; i < glyphsSize; i++) {
60
+ jsiWidths.setValueAtIndex(
61
+ runtime, i,
62
+ jsi::Value(
63
+ SkScalarToDouble(static_cast<SkScalar *>(widthPtrs.data())[i])));
64
+ }
65
+ return jsiWidths;
66
+ }
67
+
68
+ JSI_HOST_FUNCTION(getTextWidth) {
69
+ auto str = arguments[0].asString(runtime).utf8(runtime);
70
+ auto numGlyphIDs = getObject()->countText(str.c_str(), str.length(),
71
+ SkTextEncoding::kUTF8);
72
+ std::vector<SkGlyphID> glyphs;
73
+ glyphs.resize(numGlyphIDs);
74
+ int glyphsSize = static_cast<int>(numGlyphIDs);
75
+ getObject()->textToGlyphs(str.c_str(), str.length(), SkTextEncoding::kUTF8,
76
+ static_cast<SkGlyphID *>(glyphs.data()),
77
+ glyphsSize);
78
+ std::vector<SkScalar> widthPtrs;
79
+ widthPtrs.resize(numGlyphIDs);
80
+ if (count > 1) {
81
+ auto paint = JsiSkPaint::fromValue(runtime, arguments[1]);
82
+ getObject()->getWidthsBounds(glyphs.data(), glyphsSize,
83
+ static_cast<SkScalar *>(widthPtrs.data()),
84
+ nullptr, paint.get());
85
+ } else {
86
+ getObject()->getWidthsBounds(glyphs.data(), glyphsSize,
87
+ static_cast<SkScalar *>(widthPtrs.data()),
88
+ nullptr, nullptr);
89
+ }
90
+ return jsi::Value(std::accumulate(widthPtrs.begin(), widthPtrs.end(), 0));
91
+ }
92
+
93
+ JSI_HOST_FUNCTION(getMetrics) {
94
+ SkFontMetrics fm;
95
+ getObject()->getMetrics(&fm);
96
+ auto metrics = jsi::Object(runtime);
97
+ metrics.setProperty(runtime, "ascent", fm.fAscent);
98
+ metrics.setProperty(runtime, "descent", fm.fDescent);
99
+ metrics.setProperty(runtime, "leading", fm.fLeading);
100
+ if (!(fm.fFlags & SkFontMetrics::kBoundsInvalid_Flag)) {
101
+ auto bounds = SkRect::MakeLTRB(fm.fXMin, fm.fTop, fm.fXMax, fm.fBottom);
102
+ auto jsiBounds =
103
+ JsiSkRect::toValue(runtime, getContext(), std::move(bounds));
104
+ metrics.setProperty(runtime, "bounds", std::move(jsiBounds));
105
+ }
106
+ return metrics;
107
+ }
108
+
109
+ JSI_HOST_FUNCTION(getGlyphIDs) {
110
+ auto str = arguments[0].asString(runtime).utf8(runtime);
111
+ int numGlyphIDs =
112
+ count > 1 && !arguments[1].isNull() && !arguments[1].isUndefined()
113
+ ? static_cast<int>(arguments[1].asNumber())
114
+ : getObject()->countText(str.c_str(), str.length(),
115
+ SkTextEncoding::kUTF8);
116
+ std::vector<SkGlyphID> glyphIDs;
117
+ glyphIDs.resize(numGlyphIDs);
118
+ getObject()->textToGlyphs(str.c_str(), str.length(), SkTextEncoding::kUTF8,
119
+ static_cast<SkGlyphID *>(glyphIDs.data()),
120
+ numGlyphIDs);
121
+ auto jsiGlyphIDs = jsi::Array(runtime, numGlyphIDs);
122
+ for (int i = 0; i < numGlyphIDs; i++) {
123
+ jsiGlyphIDs.setValueAtIndex(runtime, i,
124
+ jsi::Value(static_cast<int>(glyphIDs[i])));
125
+ }
126
+ return jsiGlyphIDs;
127
+ }
128
+
129
+ JSI_HOST_FUNCTION(getGlyphIntercepts) {
130
+ auto jsiGlyphs = arguments[0].asObject(runtime).asArray(runtime);
131
+ auto jsiPositions = arguments[1].asObject(runtime).asArray(runtime);
132
+ auto top = arguments[2].asNumber();
133
+ auto bottom = arguments[3].asNumber();
134
+ std::vector<SkPoint> positions;
135
+ int pointsSize = static_cast<int>(jsiPositions.size(runtime));
136
+ positions.reserve(pointsSize);
137
+ for (int i = 0; i < pointsSize; i++) {
138
+ std::shared_ptr<SkPoint> point = JsiSkPoint::fromValue(
139
+ runtime, jsiPositions.getValueAtIndex(runtime, i).asObject(runtime));
140
+ positions.push_back(*point.get());
141
+ }
142
+
143
+ std::vector<SkGlyphID> glyphs;
144
+ int glyphsSize = static_cast<int>(jsiGlyphs.size(runtime));
145
+ glyphs.reserve(glyphsSize);
146
+ for (int i = 0; i < glyphsSize; i++) {
147
+ glyphs.push_back(jsiGlyphs.getValueAtIndex(runtime, i).asNumber());
148
+ }
149
+
150
+ if (glyphs.size() > positions.size()) {
151
+ throw jsi::JSError(runtime, "Not enough x,y position pairs for glyphs");
152
+ return jsi::Value::null();
153
+ }
154
+ auto sects = getObject()->getIntercepts(
155
+ glyphs.data(), SkToInt(glyphs.size()), positions.data(), top, bottom);
156
+ auto jsiSects = jsi::Array(runtime, sects.size());
157
+ for (int i = 0; i < sects.size(); i++) {
158
+ jsiSects.setValueAtIndex(runtime, i,
159
+ jsi::Value(static_cast<int>(sects.at(i))));
160
+ }
161
+ return jsiSects;
162
+ }
163
+
164
+ JSI_HOST_FUNCTION(getScaleX) {
165
+ return jsi::Value(SkScalarToDouble(getObject()->getScaleX()));
166
+ }
167
+
168
+ JSI_HOST_FUNCTION(getSize) {
169
+ return jsi::Value(SkScalarToDouble(getObject()->getSize()));
170
+ }
171
+
172
+ JSI_HOST_FUNCTION(getSkewX) {
173
+ return jsi::Value(SkScalarToDouble(getObject()->getSkewX()));
174
+ }
175
+
176
+ JSI_HOST_FUNCTION(isEmbolden) {
177
+ return jsi::Value(getObject()->isEmbolden());
178
+ }
179
+
180
+ JSI_HOST_FUNCTION(getTypeface) {
181
+ return JsiSkTypeface::toValue(
182
+ runtime, getContext(), sk_sp<SkTypeface>(getObject()->getTypeface()));
183
+ }
184
+
185
+ JSI_HOST_FUNCTION(setEdging) {
186
+ auto edging = arguments[0].asNumber();
187
+ getObject()->setEdging(static_cast<SkFont::Edging>(edging));
188
+ return jsi::Value::undefined();
189
+ }
190
+
191
+ JSI_HOST_FUNCTION(embeddedBitmaps) {
192
+ auto embeddedBitmaps = arguments[0].getBool();
193
+ getObject()->setEmbeddedBitmaps(embeddedBitmaps);
194
+ return jsi::Value::undefined();
195
+ }
196
+
197
+ JSI_HOST_FUNCTION(setHinting) {
198
+ auto hinting = arguments[0].asNumber();
199
+ getObject()->setHinting(static_cast<SkFontHinting>(hinting));
200
+ return jsi::Value::undefined();
201
+ }
202
+
203
+ JSI_HOST_FUNCTION(setLinearMetrics) {
204
+ auto linearMetrics = arguments[0].getBool();
205
+ getObject()->setLinearMetrics(linearMetrics);
206
+ return jsi::Value::undefined();
207
+ }
208
+
209
+ JSI_HOST_FUNCTION(setScaleX) {
210
+ auto scaleX = arguments[0].asNumber();
211
+ getObject()->setScaleX(scaleX);
212
+ return jsi::Value::undefined();
213
+ }
214
+
215
+ JSI_HOST_FUNCTION(setSkewX) {
216
+ auto skewX = arguments[0].asNumber();
217
+ getObject()->setSkewX(skewX);
218
+ return jsi::Value::undefined();
219
+ }
220
+
221
+ JSI_HOST_FUNCTION(setSize) {
222
+ auto size = arguments[0].asNumber();
223
+ getObject()->setSize(size);
224
+ return jsi::Value::undefined();
225
+ }
226
+
227
+ JSI_HOST_FUNCTION(setEmbolden) {
228
+ auto embolden = arguments[0].asNumber();
229
+ getObject()->setEmbolden(embolden);
230
+ return jsi::Value::undefined();
231
+ }
232
+
233
+ JSI_HOST_FUNCTION(setSubpixel) {
234
+ auto subpixel = arguments[0].asNumber();
235
+ getObject()->setSubpixel(subpixel);
236
+ return jsi::Value::undefined();
237
+ }
238
+
239
+ JSI_HOST_FUNCTION(setTypeface) {
240
+ auto typeface = arguments[0].isNull()
241
+ ? nullptr
242
+ : JsiSkTypeface::fromValue(runtime, arguments[0]);
243
+ getObject()->setTypeface(typeface);
244
+ return jsi::Value::undefined();
245
+ }
246
+
247
+ JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFont, getSize),
248
+ JSI_EXPORT_FUNC(JsiSkFont, getMetrics),
249
+ JSI_EXPORT_FUNC(JsiSkFont, getGlyphIDs),
250
+ JSI_EXPORT_FUNC(JsiSkFont, getGlyphIntercepts),
251
+ JSI_EXPORT_FUNC(JsiSkFont, getScaleX),
252
+ JSI_EXPORT_FUNC(JsiSkFont, getSkewX),
253
+ JSI_EXPORT_FUNC(JsiSkFont, getTypeface),
254
+ JSI_EXPORT_FUNC(JsiSkFont, setEdging),
255
+ JSI_EXPORT_FUNC(JsiSkFont, embeddedBitmaps),
256
+ JSI_EXPORT_FUNC(JsiSkFont, setHinting),
257
+ JSI_EXPORT_FUNC(JsiSkFont, setLinearMetrics),
258
+ JSI_EXPORT_FUNC(JsiSkFont, setScaleX),
259
+ JSI_EXPORT_FUNC(JsiSkFont, setSkewX),
260
+ JSI_EXPORT_FUNC(JsiSkFont, setSize),
261
+ JSI_EXPORT_FUNC(JsiSkFont, setEmbolden),
262
+ JSI_EXPORT_FUNC(JsiSkFont, setSubpixel),
263
+ JSI_EXPORT_FUNC(JsiSkFont, setTypeface),
264
+ JSI_EXPORT_FUNC(JsiSkFont, getGlyphWidths),
265
+ JSI_EXPORT_FUNC(JsiSkFont, getTextWidth))
266
+
267
+ JsiSkFont(std::shared_ptr<RNSkPlatformContext> context, const SkFont &font)
268
+ : JsiSkWrappingSharedPtrHostObject(std::move(context),
269
+ std::make_shared<SkFont>(font)) {}
270
+
271
+ /**
272
+ Returns the underlying object from a host object of this type
273
+ */
274
+ static std::shared_ptr<SkFont> fromValue(jsi::Runtime &runtime,
275
+ const jsi::Value &obj) {
276
+ return obj.asObject(runtime).asHostObject<JsiSkFont>(runtime)->getObject();
277
+ }
278
+
279
+ /**
280
+ * Creates the function for construction a new instance of the SkFont
281
+ * wrapper
282
+ * @param context Platform context
283
+ * @return A function for creating a new host object wrapper for the SkFont
284
+ * class
285
+ */
286
+ static const jsi::HostFunctionType
287
+ createCtor(std::shared_ptr<RNSkPlatformContext> context) {
288
+ return JSI_HOST_FUNCTION_LAMBDA {
289
+ // Handle arguments
290
+ if (count == 2) {
291
+ auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
292
+ auto size = arguments[1].asNumber();
293
+ return jsi::Object::createFromHostObject(
294
+ runtime, std::make_shared<JsiSkFont>(std::move(context),
295
+ SkFont(typeface, size)));
296
+ } else if (count == 1) {
297
+ auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
298
+ return jsi::Object::createFromHostObject(
299
+ runtime,
300
+ std::make_shared<JsiSkFont>(std::move(context), SkFont(typeface)));
301
+ } else {
302
+ // Return the newly constructed object
303
+ return jsi::Object::createFromHostObject(
304
+ runtime, std::make_shared<JsiSkFont>(std::move(context), SkFont()));
305
+ }
333
306
  };
307
+ }
308
+ };
334
309
 
335
310
  } // namespace RNSkia