Skip to content

Commit b434968

Browse files
committed
Add tests
1 parent be596c4 commit b434968

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

docs/static/img/rrect/nonuniform.png

6.65 KB
Loading

docs/static/img/rrect/uniform.png

10.5 KB
Loading

package/cpp/api/JsiSkRRect.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <memory>
44
#include <utility>
5+
#include <vector>
56

67
#include <jsi/jsi.h>
78

@@ -72,16 +73,18 @@ class JsiSkRRect : public JsiSkWrappingSharedPtrHostObject<SkRRect> {
7273
std::vector<SkPoint> points;
7374
std::shared_ptr<SkPoint> topLeft = JsiSkPoint::fromValue(
7475
runtime, object.getProperty(runtime, "topLeft").asObject(runtime));
75-
std::shared_ptr<SkPoint> topRight = JsiSkPoint::fromValue(
76-
runtime, object.getProperty(runtime, "topRight").asObject(runtime));
77-
std::shared_ptr<SkPoint> bottomRight = JsiSkPoint::fromValue(
78-
runtime, object.getProperty(runtime, "bottomRight").asObject(runtime));
79-
std::shared_ptr<SkPoint> bottomLeft = JsiSkPoint::fromValue(
80-
runtime, object.getProperty(runtime, "bottomLeft").asObject(runtime));
81-
points.push_back(*topLeft.get());
82-
points.push_back(*topRight.get());
83-
points.push_back(*bottomRight.get());
84-
points.push_back(*bottomLeft.get());
76+
std::shared_ptr<SkPoint> topRight = JsiSkPoint::fromValue(
77+
runtime, object.getProperty(runtime, "topRight").asObject(runtime));
78+
std::shared_ptr<SkPoint> bottomRight = JsiSkPoint::fromValue(
79+
runtime,
80+
object.getProperty(runtime, "bottomRight").asObject(runtime));
81+
std::shared_ptr<SkPoint> bottomLeft = JsiSkPoint::fromValue(
82+
runtime,
83+
object.getProperty(runtime, "bottomLeft").asObject(runtime));
84+
points.push_back(*topLeft.get());
85+
points.push_back(*topRight.get());
86+
points.push_back(*bottomRight.get());
87+
points.push_back(*bottomLeft.get());
8588
auto rrect = SkRRect::MakeEmpty();
8689
rrect.setRectRadii(*rect, points.data());
8790
return std::make_shared<SkRRect>(rrect);

package/cpp/rnskia/dom/props/RRectProp.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "RectProp.h"
66

77
#include <memory>
8+
#include <vector>
89

910
#pragma clang diagnostic push
1011
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -19,6 +20,10 @@ namespace RNSkia {
1920
static PropId PropNameRx = JsiPropId::get("rx");
2021
static PropId PropNameRy = JsiPropId::get("ry");
2122
static PropId PropNameR = JsiPropId::get("r");
23+
static PropId PropNameTopLeft = JsiPropId::get("topLeft");
24+
static PropId PropNameTopRight = JsiPropId::get("topRight");
25+
static PropId PropNameBottomRight = JsiPropId::get("bottomRight");
26+
static PropId PropNameBottomLeft = JsiPropId::get("bottomLeft");
2227

2328
/**
2429
Reads a rect from a given propety in the node. The name of the property is
@@ -64,6 +69,51 @@ class RRectProp : public DerivedProp<SkRRect> {
6469
width.getAsNumber(), height.getAsNumber()),
6570
rx.getAsNumber(), ry.getAsNumber()));
6671
}
72+
} else if (value.hasValue(PropNameRect) &&
73+
value.hasValue(PropNameTopLeft) &&
74+
value.hasValue(PropNameTopRight) &&
75+
value.hasValue(PropNameBottomRight) &&
76+
value.hasValue(PropNameBottomLeft)) {
77+
auto rect = value.getValue(PropNameRect);
78+
if (rect.hasValue(PropNameX) && rect.hasValue(PropNameY) &&
79+
rect.hasValue(PropNameWidth) && rect.hasValue(PropNameHeight)) {
80+
auto x = rect.getValue(PropNameX);
81+
auto y = rect.getValue(PropNameY);
82+
auto width = rect.getValue(PropNameWidth);
83+
auto height = rect.getValue(PropNameHeight);
84+
std::vector<SkPoint> points;
85+
points.reserve(4);
86+
auto topLeft = value.getValue(PropNameTopLeft);
87+
auto topLeftX = topLeft.getValue(PropNameX);
88+
auto topLeftY = topLeft.getValue(PropNameY);
89+
points.push_back(
90+
SkPoint::Make(topLeftX.getAsNumber(), topLeftY.getAsNumber()));
91+
92+
auto topRight = value.getValue(PropNameTopRight);
93+
auto topRightX = topRight.getValue(PropNameX);
94+
auto topRightY = topRight.getValue(PropNameY);
95+
points.push_back(SkPoint::Make(topRightX.getAsNumber(),
96+
topRightY.getAsNumber()));
97+
98+
auto bottomRight = value.getValue(PropNameBottomRight);
99+
auto bottomRightX = bottomRight.getValue(PropNameX);
100+
auto bottomRightY = bottomRight.getValue(PropNameY);
101+
points.push_back(SkPoint::Make(bottomRightX.getAsNumber(),
102+
bottomRightY.getAsNumber()));
103+
104+
auto bottomLeft = value.getValue(PropNameBottomLeft);
105+
auto bottomLeftX = bottomLeft.getValue(PropNameX);
106+
auto bottomLeftY = bottomLeft.getValue(PropNameY);
107+
points.push_back(SkPoint::Make(bottomLeftX.getAsNumber(),
108+
bottomLeftY.getAsNumber()));
109+
110+
auto rrect = std::make_shared<SkRRect>(SkRRect::MakeEmpty());
111+
rrect->setRectRadii(
112+
SkRect::MakeXYWH(x.getAsNumber(), y.getAsNumber(),
113+
width.getAsNumber(), height.getAsNumber()),
114+
points.data());
115+
return rrect;
116+
}
67117
}
68118
}
69119
}

package/src/dom/nodes/drawings/RRectNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { SkRRect } from "../../../skia/types";
1+
import type { InputRRect, SkRRect } from "../../../skia/types";
22
import type { DrawingContext, RoundedRectProps } from "../../types";
33
import { NodeType } from "../../types";
44
import { processRRect } from "../datatypes";
55
import { JsiDrawingNode } from "../DrawingNode";
66
import type { NodeContext } from "../Node";
77

8-
export class RRectNode extends JsiDrawingNode<RoundedRectProps, SkRRect> {
8+
export class RRectNode extends JsiDrawingNode<RoundedRectProps, InputRRect> {
99
rect?: SkRRect;
1010

1111
constructor(ctx: NodeContext, props: RoundedRectProps) {

package/src/renderer/__tests__/e2e/Rect.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ describe("Rects and rounded rects", () => {
7070
);
7171
checkImage(image, docPath("rrect/uniform.png"));
7272
});
73+
it("Should draw a rounded rect with non-uniform values", async () => {
74+
const { width } = surface;
75+
const r = width * 0.2;
76+
const rrct = {
77+
rect: { x: 0, y: 0, width, height: width },
78+
topLeft: { x: 0, y: 0 },
79+
topRight: { x: r, y: r },
80+
bottomRight: { x: 0, y: 0 },
81+
bottomLeft: { x: r, y: r },
82+
};
83+
const image = await surface.draw(
84+
<RoundedRect rect={rrct} color="lightblue" />
85+
);
86+
checkImage(image, docPath("rrect/nonuniform.png"));
87+
});
7388
});

0 commit comments

Comments
 (0)