Skip to content

Commit

Permalink
Add square cap (flutter#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent 555faba commit d84a831
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
18 changes: 17 additions & 1 deletion impeller/entity/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,23 @@ void SolidStrokeContents::SetStrokeCap(Cap cap) {
FML_DLOG(ERROR) << "Unimplemented.";
break;
case Cap::kSquare:
FML_DLOG(ERROR) << "Unimplemented.";
cap_proc_ = [](VertexBufferBuilder<VS::PerVertexData>& vtx_builder,
const Point& position, const Point& normal) {
SolidStrokeVertexShader::PerVertexData vtx;
vtx.vertex_position = position;
vtx.pen_down = 1.0;

Point forward(normal.y, -normal.x);

vtx.vertex_normal = normal;
vtx_builder.AppendVertex(vtx);
vtx.vertex_normal = -normal;
vtx_builder.AppendVertex(vtx);
vtx.vertex_normal = normal + forward;
vtx_builder.AppendVertex(vtx);
vtx.vertex_normal = -normal + forward;
vtx_builder.AppendVertex(vtx);
};
break;
}
}
Expand Down
70 changes: 64 additions & 6 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,51 @@ TEST_F(EntityTest, TriangleInsideASquare) {
ASSERT_TRUE(OpenPlaygroundHere(callback));
}

TEST_F(EntityTest, StrokeCapAndJoinTest) {
auto callback = [&](ContentContext& context, RenderPass& pass) {
Entity entity;

auto create_contents = [](SolidStrokeContents::Cap cap) {
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetColor(Color::Red());
contents->SetStrokeSize(20.0);
contents->SetStrokeCap(cap);
return contents;
};

const Point a_def(100, 100), b_def(100, 150), c_def(200, 100),
d_def(200, 50);
const Scalar r = 10;

{
Point off(0, 0);
Point a, b, c, d;
std::tie(a, b) = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
Color::Black(), Color::White());
std::tie(c, d) = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
Color::Black(), Color::White());
entity.SetPath(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath());
entity.SetContents(create_contents(SolidStrokeContents::Cap::kButt));
entity.Render(context, pass);
}

{
Point off(0, 100);
Point a, b, c, d;
std::tie(a, b) = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
Color::Black(), Color::White());
std::tie(c, d) = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
Color::Black(), Color::White());
entity.SetPath(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath());
entity.SetContents(create_contents(SolidStrokeContents::Cap::kSquare));
entity.Render(context, pass);
}

return true;
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}

TEST_F(EntityTest, CubicCurveTest) {
// Compare with https://fiddle.skia.org/c/b3625f26122c9de7afe7794fcf25ead3
Path path =
Expand Down Expand Up @@ -329,12 +374,25 @@ TEST_F(EntityTest, CubicCurveAndOverlapTest) {
ASSERT_TRUE(OpenPlaygroundHere(entity));
}

TEST_F(EntityTest, SolidStrokeContentsSetStrokeDefaults) {
SolidStrokeContents stroke;
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kButt);
ASSERT_EQ(stroke.GetStrokeJoin(), SolidStrokeContents::Join::kBevel);
// TODO(99089): Test that SetStroke[Cap|Join] works once there are multiple
// caps and joins.
TEST_F(EntityTest, SolidStrokeContentsSetStrokeCapsAndJoins) {
{
SolidStrokeContents stroke;
// Defaults.
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kButt);
ASSERT_EQ(stroke.GetStrokeJoin(), SolidStrokeContents::Join::kBevel);
}

{
SolidStrokeContents stroke;
stroke.SetStrokeCap(SolidStrokeContents::Cap::kSquare);
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kSquare);
}

{
SolidStrokeContents stroke;
stroke.SetStrokeCap(SolidStrokeContents::Cap::kRound);
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kRound);
}
}

} // namespace testing
Expand Down

0 comments on commit d84a831

Please sign in to comment.