Skip to content

Commit 64ae5f7

Browse files
authored
Merge pull request #525 from Shopify/fix/path-signature
Fix Path Chaining
2 parents cb73fe9 + 7759840 commit 64ae5f7

File tree

5 files changed

+102
-76
lines changed

5 files changed

+102
-76
lines changed

example/src/Examples/API/components/drawings/backface.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Pattern = () => {
6565
center.y * 2 + delta * 2
6666
);
6767
const path = Skia.Path.Make();
68-
path.addArc(rect, 0, 360);
68+
path.addArc(rect, 0, 360).close();
6969
return (
7070
<Group key={i} origin={center} transform={[{ scale: 0.6 }]}>
7171
<Rect

example/src/Examples/Aurora/components/Curves.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export const Curves = ({ patch }: CurvesProps) => {
1010
const path = useDerivedValue(() => {
1111
const [p1, p2, p3, p4] = patch.current;
1212
const d = Skia.Path.Make();
13-
d.moveTo(p1.pos.x, p1.pos.y);
14-
d.cubicTo(p1.c2.x, p1.c2.y, p2.c1.x, p2.c1.y, p2.pos.x, p2.pos.y);
15-
d.cubicTo(p2.c2.x, p2.c2.y, p3.c1.x, p3.c1.y, p3.pos.x, p3.pos.y);
16-
d.cubicTo(p3.c2.x, p3.c2.y, p4.c1.x, p4.c1.y, p4.pos.x, p4.pos.y);
17-
d.cubicTo(p4.c2.x, p4.c2.y, p1.c1.x, p1.c1.y, p1.pos.x, p1.pos.y);
13+
d.moveTo(p1.pos.x, p1.pos.y)
14+
.cubicTo(p1.c2.x, p1.c2.y, p2.c1.x, p2.c1.y, p2.pos.x, p2.pos.y)
15+
.cubicTo(p2.c2.x, p2.c2.y, p3.c1.x, p3.c1.y, p3.pos.x, p3.pos.y)
16+
.cubicTo(p3.c2.x, p3.c2.y, p4.c1.x, p4.c1.y, p4.pos.x, p4.pos.y)
17+
.cubicTo(p4.c2.x, p4.c2.y, p1.c1.x, p1.c1.y, p1.pos.x, p1.pos.y);
1818
return d;
1919
}, [patch]);
2020
return <Path path={path} color="white" strokeWidth={2} style="stroke" />;

package/cpp/api/JsiSkPath.h

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ using namespace facebook;
3131

3232

3333
class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
34+
3435
public:
35-
// TODO: declare in JsiSkWrappingSkPtrHostObject via extra template parameter?
36-
JSI_PROPERTY_GET(__typename__) {
37-
return jsi::String::createFromUtf8(runtime, "Path");
38-
}
36+
// TODO: declare in JsiSkWrappingSkPtrHostObject via extra template parameter?
37+
JSI_PROPERTY_GET(__typename__) {
38+
return jsi::String::createFromUtf8(runtime, "Path");
39+
}
3940

40-
JSI_HOST_FUNCTION(addArc) {
41+
JSI_HOST_FUNCTION(addArc) {
4142
auto rect = JsiSkRect::fromValue(runtime, arguments[0]).get();
4243
auto start = arguments[1].asNumber();
4344
auto sweep = arguments[2].asNumber();
4445
getObject()->addArc(*rect, start, sweep);
45-
return jsi::Value::undefined();
46+
return thisValue.getObject(runtime);
4647
}
4748

4849
JSI_HOST_FUNCTION(addOval) {
@@ -52,8 +53,8 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
5253
direction = SkPathDirection::kCCW;
5354
}
5455
unsigned startIndex = count < 3 ? 0 : arguments[2].asNumber();
55-
getObject()->addOval(*rect, direction, startIndex);
56-
return jsi::Value::undefined();
56+
auto result = getObject()->addOval(*rect, direction, startIndex);
57+
return thisValue.getObject(runtime);
5758
}
5859

5960
JSI_HOST_FUNCTION(addPoly) {
@@ -68,7 +69,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
6869
points.push_back(*point.get());
6970
}
7071
getObject()->addPoly(points.data(), (int)points.size(), close);
71-
return jsi::Value::undefined();
72+
return thisValue.getObject(runtime);
7273
}
7374

7475
JSI_HOST_FUNCTION(addRect) {
@@ -88,7 +89,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
8889
direction = SkPathDirection::kCCW;
8990
}
9091
getObject()->addRRect(*rrect, direction);
91-
return jsi::Value::undefined();
92+
return thisValue.getObject(runtime);
9293
}
9394

9495
JSI_HOST_FUNCTION(arcToOval) {
@@ -97,7 +98,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
9798
auto sweep = arguments[2].asNumber();
9899
auto forceMoveTo = arguments[3].getBool();
99100
getObject()->arcTo(*rect, start, sweep, forceMoveTo);
100-
return jsi::Value::undefined();
101+
return thisValue.getObject(runtime);
101102
}
102103

103104
JSI_HOST_FUNCTION(arcToRotated) {
@@ -112,7 +113,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
112113
auto x = arguments[5].asNumber();
113114
auto y = arguments[6].asNumber();
114115
getObject()->arcTo(rx, ry, xAxisRotate, arcSize, sweep, x, y);
115-
return jsi::Value::undefined();
116+
return thisValue.getObject(runtime);
116117
}
117118

118119
JSI_HOST_FUNCTION(rArcTo) {
@@ -127,7 +128,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
127128
auto x = arguments[5].asNumber();
128129
auto y = arguments[6].asNumber();
129130
getObject()->rArcTo(rx, ry, xAxisRotate, arcSize, sweep, x, y);
130-
return jsi::Value::undefined();
131+
return thisValue.getObject(runtime);
131132
}
132133

133134
JSI_HOST_FUNCTION(arcToTangent) {
@@ -137,7 +138,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
137138
auto y2 = arguments[3].asNumber();
138139
auto r = arguments[4].asNumber();
139140
getObject()->arcTo(x1, y1, x2, y2, r);
140-
return jsi::Value::undefined();
141+
return thisValue.getObject(runtime);
141142
}
142143

143144
JSI_HOST_FUNCTION(computeTightBounds) {
@@ -160,7 +161,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
160161
auto y2 = arguments[3].asNumber();
161162
auto w = arguments[4].asNumber();
162163
getObject()->conicTo(x1, y1, x2, y2, w);
163-
return jsi::Value::undefined();
164+
return thisValue.getObject(runtime);
164165
}
165166

166167
JSI_HOST_FUNCTION(rConicTo) {
@@ -170,7 +171,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
170171
auto y2 = arguments[3].asNumber();
171172
auto w = arguments[4].asNumber();
172173
getObject()->rConicTo(x1, y1, x2, y2, w);
173-
return jsi::Value::undefined();
174+
return thisValue.getObject(runtime);
174175
}
175176

176177
JSI_HOST_FUNCTION(contains) {
@@ -270,7 +271,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
270271
auto precision = jsiPrecision.isUndefined() ? 1 : jsiPrecision.asNumber();
271272
auto result = p.getFillPath(path, &path, nullptr, precision);
272273
getObject()->swap(path);
273-
return jsi::Value(result);
274+
return result ? thisValue.getObject(runtime) : jsi::Value::null();
274275
}
275276

276277
JSI_HOST_FUNCTION(trim) {
@@ -284,15 +285,15 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
284285
if (!pe) {
285286
// SkDebugf("Invalid args to trim(): startT and stopT must be in
286287
// [0,1]\n");
287-
return jsi::Value(false);
288+
return jsi::Value::null();
288289
}
289290
SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
290291
if (pe->filterPath(&path, path, &rec, nullptr)) {
291292
getObject()->swap(path);
292-
return jsi::Value(true);
293+
return thisValue.getObject(runtime);
293294
}
294295
SkDebugf("Could not trim path\n");
295-
return jsi::Value(false);
296+
return jsi::Value::null();
296297
}
297298

298299
JSI_HOST_FUNCTION(getPoint) {
@@ -312,8 +313,8 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
312313
JSI_HOST_FUNCTION(makeAsWinding) {
313314
SkPath out;
314315
if (AsWinding(*getObject(), &out)) {
315-
return jsi::Object::createFromHostObject(
316-
runtime, std::make_shared<JsiSkPath>(getContext(), std::move(out)));
316+
getObject()->swap(out);
317+
return thisValue.getObject(runtime);
317318
}
318319
return jsi::Value::null();
319320
}
@@ -324,34 +325,34 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
324325
SkScalar dx = arguments[0].asNumber();
325326
SkScalar dy = arguments[1].asNumber();
326327
getObject()->offset(dx, dy);
327-
return jsi::Value::undefined();
328+
return thisValue.getObject(runtime);
328329
}
329330

330331
JSI_HOST_FUNCTION(moveTo) {
331332
SkScalar x = arguments[0].asNumber();
332333
SkScalar y = arguments[1].asNumber();
333334
getObject()->moveTo(x, y);
334-
return jsi::Value::undefined();
335+
return thisValue.getObject(runtime);
335336
}
336337

337338
JSI_HOST_FUNCTION(rMoveTo) {
338339
SkScalar x = arguments[0].asNumber();
339340
SkScalar y = arguments[1].asNumber();
340341
getObject()->rMoveTo(x, y);
341-
return jsi::Value::undefined();
342+
return thisValue.getObject(runtime);
342343
}
343344
JSI_HOST_FUNCTION(lineTo) {
344345
SkScalar x = arguments[0].asNumber();
345346
SkScalar y = arguments[1].asNumber();
346347
getObject()->lineTo(x, y);
347-
return jsi::Value::undefined();
348+
return thisValue.getObject(runtime);
348349
}
349350

350351
JSI_HOST_FUNCTION(rlineTo) {
351352
SkScalar x = arguments[0].asNumber();
352353
SkScalar y = arguments[1].asNumber();
353354
getObject()->rLineTo(x, y);
354-
return jsi::Value::undefined();
355+
return thisValue.getObject(runtime);
355356
}
356357

357358
JSI_HOST_FUNCTION(cubicTo) {
@@ -362,7 +363,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
362363
auto x3 = arguments[4].asNumber();
363364
auto y3 = arguments[5].asNumber();
364365
getObject()->cubicTo(x1, y1, x2, y2, x3, y3);
365-
return jsi::Value::undefined();
366+
return thisValue.getObject(runtime);
366367
}
367368

368369
JSI_HOST_FUNCTION(rCubicTo) {
@@ -373,7 +374,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
373374
auto x3 = arguments[4].asNumber();
374375
auto y3 = arguments[5].asNumber();
375376
getObject()->rCubicTo(x1, y1, x2, y2, x3, y3);
376-
return jsi::Value::undefined();
377+
return thisValue.getObject(runtime);
377378
}
378379

379380
JSI_HOST_FUNCTION(reset) {
@@ -401,7 +402,7 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
401402
auto x2 = arguments[2].asNumber();
402403
auto y2 = arguments[3].asNumber();
403404
getObject()->rQuadTo(x1, y1, x2, y2);
404-
return jsi::Value::undefined();
405+
return thisValue.getObject(runtime);
405406
}
406407

407408
JSI_HOST_FUNCTION(addCircle) {
@@ -514,45 +515,61 @@ class JsiSkPath : public JsiSkWrappingSharedPtrHostObject<SkPath> {
514515
JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(JsiSkPath, __typename__))
515516

516517
JSI_EXPORT_FUNCTIONS(
517-
JSI_EXPORT_FUNC(JsiSkPath, addArc), JSI_EXPORT_FUNC(JsiSkPath, addOval),
518-
JSI_EXPORT_FUNC(JsiSkPath, addPoly), JSI_EXPORT_FUNC(JsiSkPath, addRect),
518+
JSI_EXPORT_FUNC(JsiSkPath, addArc),
519+
JSI_EXPORT_FUNC(JsiSkPath, addOval),
520+
JSI_EXPORT_FUNC(JsiSkPath, addPoly),
521+
JSI_EXPORT_FUNC(JsiSkPath, addRect),
519522
JSI_EXPORT_FUNC(JsiSkPath, addRRect),
520523
JSI_EXPORT_FUNC(JsiSkPath, arcToOval),
521524
JSI_EXPORT_FUNC(JsiSkPath, arcToRotated),
522525
JSI_EXPORT_FUNC(JsiSkPath, rArcTo),
523526
JSI_EXPORT_FUNC(JsiSkPath, arcToTangent),
524527
JSI_EXPORT_FUNC(JsiSkPath, computeTightBounds),
525528
JSI_EXPORT_FUNC(JsiSkPath, getBounds),
526-
JSI_EXPORT_FUNC(JsiSkPath, conicTo), JSI_EXPORT_FUNC(JsiSkPath, rConicTo),
527-
JSI_EXPORT_FUNC(JsiSkPath, contains), JSI_EXPORT_FUNC(JsiSkPath, dash),
529+
JSI_EXPORT_FUNC(JsiSkPath, conicTo),
530+
JSI_EXPORT_FUNC(JsiSkPath, rConicTo),
531+
JSI_EXPORT_FUNC(JsiSkPath, contains),
532+
JSI_EXPORT_FUNC(JsiSkPath, dash),
528533
JSI_EXPORT_FUNC(JsiSkPath, equals),
529534
JSI_EXPORT_FUNC(JsiSkPath, getFillType),
530535
JSI_EXPORT_FUNC(JsiSkPath, setFillType),
531536
JSI_EXPORT_FUNC(JsiSkPath, setIsVolatile),
532537
JSI_EXPORT_FUNC(JsiSkPath, isVolatile),
533-
JSI_EXPORT_FUNC(JsiSkPath, transform), JSI_EXPORT_FUNC(JsiSkPath, stroke),
534-
JSI_EXPORT_FUNC(JsiSkPath, trim), JSI_EXPORT_FUNC(JsiSkPath, getPoint),
538+
JSI_EXPORT_FUNC(JsiSkPath, transform),
539+
JSI_EXPORT_FUNC(JsiSkPath, stroke),
540+
JSI_EXPORT_FUNC(JsiSkPath, trim),
541+
JSI_EXPORT_FUNC(JsiSkPath, getPoint),
535542
JSI_EXPORT_FUNC(JsiSkPath, toSVGString),
536543
JSI_EXPORT_FUNC(JsiSkPath, makeAsWinding),
537-
JSI_EXPORT_FUNC(JsiSkPath, isEmpty), JSI_EXPORT_FUNC(JsiSkPath, offset),
538-
JSI_EXPORT_FUNC(JsiSkPath, moveTo), JSI_EXPORT_FUNC(JsiSkPath, rMoveTo),
539-
JSI_EXPORT_FUNC(JsiSkPath, lineTo), JSI_EXPORT_FUNC(JsiSkPath, rlineTo),
540-
JSI_EXPORT_FUNC(JsiSkPath, cubicTo), JSI_EXPORT_FUNC(JsiSkPath, rCubicTo),
541-
JSI_EXPORT_FUNC(JsiSkPath, reset), JSI_EXPORT_FUNC(JsiSkPath, rewind),
542-
JSI_EXPORT_FUNC(JsiSkPath, quadTo), JSI_EXPORT_FUNC(JsiSkPath, rQuadTo),
544+
JSI_EXPORT_FUNC(JsiSkPath, isEmpty),
545+
JSI_EXPORT_FUNC(JsiSkPath, offset),
546+
JSI_EXPORT_FUNC(JsiSkPath, moveTo),
547+
JSI_EXPORT_FUNC(JsiSkPath, rMoveTo),
548+
JSI_EXPORT_FUNC(JsiSkPath, lineTo),
549+
JSI_EXPORT_FUNC(JsiSkPath, rlineTo),
550+
JSI_EXPORT_FUNC(JsiSkPath, cubicTo),
551+
JSI_EXPORT_FUNC(JsiSkPath, rCubicTo),
552+
JSI_EXPORT_FUNC(JsiSkPath, reset),
553+
JSI_EXPORT_FUNC(JsiSkPath, rewind),
554+
JSI_EXPORT_FUNC(JsiSkPath, quadTo),
555+
JSI_EXPORT_FUNC(JsiSkPath, rQuadTo),
543556
JSI_EXPORT_FUNC(JsiSkPath, addCircle),
544-
JSI_EXPORT_FUNC(JsiSkPath, getLastPt), JSI_EXPORT_FUNC(JsiSkPath, close),
557+
JSI_EXPORT_FUNC(JsiSkPath, getLastPt),
558+
JSI_EXPORT_FUNC(JsiSkPath, close),
545559
JSI_EXPORT_FUNC(JsiSkPath, simplify),
546-
JSI_EXPORT_FUNC(JsiSkPath, countPoints), JSI_EXPORT_FUNC(JsiSkPath, copy),
547-
JSI_EXPORT_FUNC(JsiSkPath, fromText), JSI_EXPORT_FUNC(JsiSkPath, op),
560+
JSI_EXPORT_FUNC(JsiSkPath, countPoints),
561+
JSI_EXPORT_FUNC(JsiSkPath, copy),
562+
JSI_EXPORT_FUNC(JsiSkPath, fromText),
563+
JSI_EXPORT_FUNC(JsiSkPath, op),
548564
JSI_EXPORT_FUNC(JsiSkPath, isInterpolatable),
549565
JSI_EXPORT_FUNC(JsiSkPath, interpolate),
550566
JSI_EXPORT_FUNC(JsiSkPath, toCmds),
551567
)
552568

553569
JsiSkPath(std::shared_ptr<RNSkPlatformContext> context, SkPath path)
554570
: JsiSkWrappingSharedPtrHostObject<SkPath>(
555-
std::move(context), std::make_shared<SkPath>(std::move(path))){}
571+
std::move(context), std::make_shared<SkPath>(std::move(path))) {
572+
}
556573

557574
/**
558575
Returns the underlying object from a host object of this type

0 commit comments

Comments
 (0)