Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d84a831

Browse files
bderodnfield
authored andcommitted
Add square cap (#48)
1 parent 555faba commit d84a831

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

impeller/entity/contents.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,23 @@ void SolidStrokeContents::SetStrokeCap(Cap cap) {
459459
FML_DLOG(ERROR) << "Unimplemented.";
460460
break;
461461
case Cap::kSquare:
462-
FML_DLOG(ERROR) << "Unimplemented.";
462+
cap_proc_ = [](VertexBufferBuilder<VS::PerVertexData>& vtx_builder,
463+
const Point& position, const Point& normal) {
464+
SolidStrokeVertexShader::PerVertexData vtx;
465+
vtx.vertex_position = position;
466+
vtx.pen_down = 1.0;
467+
468+
Point forward(normal.y, -normal.x);
469+
470+
vtx.vertex_normal = normal;
471+
vtx_builder.AppendVertex(vtx);
472+
vtx.vertex_normal = -normal;
473+
vtx_builder.AppendVertex(vtx);
474+
vtx.vertex_normal = normal + forward;
475+
vtx_builder.AppendVertex(vtx);
476+
vtx.vertex_normal = -normal + forward;
477+
vtx_builder.AppendVertex(vtx);
478+
};
463479
break;
464480
}
465481
}

impeller/entity/entity_unittests.cc

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,51 @@ TEST_F(EntityTest, TriangleInsideASquare) {
7979
ASSERT_TRUE(OpenPlaygroundHere(callback));
8080
}
8181

82+
TEST_F(EntityTest, StrokeCapAndJoinTest) {
83+
auto callback = [&](ContentContext& context, RenderPass& pass) {
84+
Entity entity;
85+
86+
auto create_contents = [](SolidStrokeContents::Cap cap) {
87+
auto contents = std::make_unique<SolidStrokeContents>();
88+
contents->SetColor(Color::Red());
89+
contents->SetStrokeSize(20.0);
90+
contents->SetStrokeCap(cap);
91+
return contents;
92+
};
93+
94+
const Point a_def(100, 100), b_def(100, 150), c_def(200, 100),
95+
d_def(200, 50);
96+
const Scalar r = 10;
97+
98+
{
99+
Point off(0, 0);
100+
Point a, b, c, d;
101+
std::tie(a, b) = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
102+
Color::Black(), Color::White());
103+
std::tie(c, d) = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
104+
Color::Black(), Color::White());
105+
entity.SetPath(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath());
106+
entity.SetContents(create_contents(SolidStrokeContents::Cap::kButt));
107+
entity.Render(context, pass);
108+
}
109+
110+
{
111+
Point off(0, 100);
112+
Point a, b, c, d;
113+
std::tie(a, b) = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
114+
Color::Black(), Color::White());
115+
std::tie(c, d) = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
116+
Color::Black(), Color::White());
117+
entity.SetPath(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath());
118+
entity.SetContents(create_contents(SolidStrokeContents::Cap::kSquare));
119+
entity.Render(context, pass);
120+
}
121+
122+
return true;
123+
};
124+
ASSERT_TRUE(OpenPlaygroundHere(callback));
125+
}
126+
82127
TEST_F(EntityTest, CubicCurveTest) {
83128
// Compare with https://fiddle.skia.org/c/b3625f26122c9de7afe7794fcf25ead3
84129
Path path =
@@ -329,12 +374,25 @@ TEST_F(EntityTest, CubicCurveAndOverlapTest) {
329374
ASSERT_TRUE(OpenPlaygroundHere(entity));
330375
}
331376

332-
TEST_F(EntityTest, SolidStrokeContentsSetStrokeDefaults) {
333-
SolidStrokeContents stroke;
334-
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kButt);
335-
ASSERT_EQ(stroke.GetStrokeJoin(), SolidStrokeContents::Join::kBevel);
336-
// TODO(99089): Test that SetStroke[Cap|Join] works once there are multiple
337-
// caps and joins.
377+
TEST_F(EntityTest, SolidStrokeContentsSetStrokeCapsAndJoins) {
378+
{
379+
SolidStrokeContents stroke;
380+
// Defaults.
381+
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kButt);
382+
ASSERT_EQ(stroke.GetStrokeJoin(), SolidStrokeContents::Join::kBevel);
383+
}
384+
385+
{
386+
SolidStrokeContents stroke;
387+
stroke.SetStrokeCap(SolidStrokeContents::Cap::kSquare);
388+
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kSquare);
389+
}
390+
391+
{
392+
SolidStrokeContents stroke;
393+
stroke.SetStrokeCap(SolidStrokeContents::Cap::kRound);
394+
ASSERT_EQ(stroke.GetStrokeCap(), SolidStrokeContents::Cap::kRound);
395+
}
338396
}
339397

340398
} // namespace testing

0 commit comments

Comments
 (0)