Skip to content

Commit

Permalink
LibWeb: Implement CanvasDrawPath::isPointInPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
mierenhoop authored and AtkinsSJ committed Sep 18, 2024
1 parent 18bc597 commit 259e798
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class CanvasDrawPath {
virtual void clip(StringView fill_rule) = 0;
virtual void clip(Path2D& path, StringView fill_rule) = 0;

virtual bool is_point_in_path(double x, double y, StringView fill_rule) = 0;
virtual bool is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule) = 0;

protected:
CanvasDrawPath() = default;
};
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ interface mixin CanvasDrawPath {
// FIXME: `DOMString` should be `CanvasFillRule`
undefined clip(Path2D path, optional DOMString fillRule = "nonzero");

[FIXME] boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
[FIXME] boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
// FIXME: `DOMString` should be `CanvasFillRule`
boolean isPointInPath(unrestricted double x, unrestricted double y, optional DOMString fillRule = "nonzero");
// FIXME: `DOMString` should be `CanvasFillRule`
boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional DOMString fillRule = "nonzero");
[FIXME] boolean isPointInStroke(unrestricted double x, unrestricted double y);
[FIXME] boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
};
15 changes: 15 additions & 0 deletions Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,21 @@ void CanvasRenderingContext2D::clip(Path2D& path, StringView fill_rule)
clip_internal(path.path(), parse_fill_rule(fill_rule));
}

static bool is_point_in_path_internal(Gfx::Path path, double x, double y, StringView fill_rule)
{
return path.contains(Gfx::FloatPoint(x, y), parse_fill_rule(fill_rule));
}

bool CanvasRenderingContext2D::is_point_in_path(double x, double y, StringView fill_rule)
{
return is_point_in_path_internal(path(), x, y, fill_rule);
}

bool CanvasRenderingContext2D::is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule)
{
return is_point_in_path_internal(path.path(), x, y, fill_rule);
}

// https://html.spec.whatwg.org/multipage/canvas.html#check-the-usability-of-the-image-argument
WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const& image)
{
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class CanvasRenderingContext2D
virtual void clip(StringView fill_rule) override;
virtual void clip(Path2D& path, StringView fill_rule) override;

virtual bool is_point_in_path(double x, double y, StringView fill_rule) override;
virtual bool is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule) override;

virtual bool image_smoothing_enabled() const override;
virtual void set_image_smoothing_enabled(bool) override;
virtual Bindings::ImageSmoothingQuality image_smoothing_quality() const override;
Expand Down

0 comments on commit 259e798

Please sign in to comment.