LibGfx: Add Path::contains()

With this new method, it is possible to check whether a 2D point is
inside of the path.
This commit is contained in:
mierenhoop 2024-09-18 10:51:26 +02:00 committed by Sam Atkins
parent 76daba3069
commit 18bc5972f4
Notes: github-actions[bot] 2024-09-18 20:22:59 +00:00
3 changed files with 21 additions and 0 deletions

View file

@ -39,6 +39,7 @@ public:
[[nodiscard]] virtual bool is_empty() const = 0;
virtual Gfx::FloatPoint last_point() const = 0;
virtual Gfx::FloatRect bounding_box() const = 0;
virtual bool contains(FloatPoint point, Gfx::WindingRule) const = 0;
virtual NonnullOwnPtr<PathImpl> clone() const = 0;
virtual NonnullOwnPtr<PathImpl> copy_transformed(Gfx::AffineTransform const&) const = 0;
@ -84,6 +85,7 @@ public:
[[nodiscard]] bool is_empty() const { return impl().is_empty(); }
Gfx::FloatPoint last_point() const { return impl().last_point(); }
Gfx::FloatRect bounding_box() const { return impl().bounding_box(); }
bool contains(FloatPoint point, Gfx::WindingRule winding_rule) const { return impl().contains(point, winding_rule); }
Gfx::Path clone() const { return Gfx::Path { impl().clone() }; }
Gfx::Path copy_transformed(Gfx::AffineTransform const& transform) const { return Gfx::Path { impl().copy_transformed(transform) }; }

View file

@ -199,6 +199,24 @@ Gfx::FloatRect PathImplSkia::bounding_box() const
return { bounds.fLeft, bounds.fTop, bounds.fRight - bounds.fLeft, bounds.fBottom - bounds.fTop };
}
static SkPathFillType to_skia_path_fill_type(Gfx::WindingRule winding_rule)
{
switch (winding_rule) {
case Gfx::WindingRule::Nonzero:
return SkPathFillType::kWinding;
case Gfx::WindingRule::EvenOdd:
return SkPathFillType::kEvenOdd;
}
VERIFY_NOT_REACHED();
}
bool PathImplSkia::contains(FloatPoint point, Gfx::WindingRule winding_rule) const
{
SkPath temp_path = *m_path;
temp_path.setFillType(to_skia_path_fill_type(winding_rule));
return temp_path.contains(point.x(), point.y());
}
NonnullOwnPtr<PathImpl> PathImplSkia::clone() const
{
auto new_path = PathImplSkia::create();

View file

@ -35,6 +35,7 @@ public:
[[nodiscard]] virtual bool is_empty() const override;
virtual Gfx::FloatPoint last_point() const override;
virtual Gfx::FloatRect bounding_box() const override;
virtual bool contains(FloatPoint point, Gfx::WindingRule) const override;
virtual NonnullOwnPtr<PathImpl> clone() const override;
virtual NonnullOwnPtr<PathImpl> copy_transformed(Gfx::AffineTransform const&) const override;