From 62b561e2e1ab95b8b6852dd91745facb9dd78455 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 12 Aug 2022 16:52:17 +0100 Subject: [PATCH] LibWeb: Extract CanvasDrawPath class from CRC2D Again, this is an entirely virtual class since the methods involve direct access to the Painter. Though, maybe I could just expose the Painter... --- Userland/Libraries/LibWeb/Forward.h | 1 + .../LibWeb/HTML/Canvas/CanvasDrawPath.h | 33 +++++++++++++++++++ .../LibWeb/HTML/Canvas/CanvasDrawPath.idl | 25 ++++++++++++++ .../LibWeb/HTML/CanvasRenderingContext2D.h | 16 +++++---- .../LibWeb/HTML/CanvasRenderingContext2D.idl | 16 ++------- 5 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.h create mode 100644 Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index a3739e5109..fc136593bc 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -648,6 +648,7 @@ class XMLHttpRequestPrototype; class XMLHttpRequestWrapper; class XMLSerializerWrapper; enum class CanPlayTypeResult; +enum class CanvasFillRule; enum class EndingType; enum class DOMParserSupportedType; enum class ResizeObserverBoxOptions; diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.h new file mode 100644 index 0000000000..6d622852c6 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath +class CanvasDrawPath { +public: + virtual ~CanvasDrawPath() = default; + + virtual void begin_path() = 0; + + virtual void fill(String const& fill_rule) = 0; + virtual void fill(Path2D& path, String const& fill_rule) = 0; + + virtual void stroke() = 0; + virtual void stroke(Path2D const& path) = 0; + + virtual void clip() = 0; + +protected: + CanvasDrawPath() = default; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl new file mode 100644 index 0000000000..f395762304 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl @@ -0,0 +1,25 @@ +#import + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillrule +enum CanvasFillRule { "nonzero", "evenodd" }; + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath +interface mixin CanvasDrawPath { + undefined beginPath(); + // FIXME: `DOMString` should be `CanvasFillRule` + undefined fill(optional DOMString fillRule = "nonzero"); + // FIXME: `DOMString` should be `CanvasFillRule` + undefined fill(Path2D path, optional DOMString fillRule = "nonzero"); + undefined stroke(); + undefined stroke(Path2D path); + + // FIXME: Replace this with these two definitions: + // undefined clip(optional CanvasFillRule fillRule = "nonzero"); + // undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero"); + undefined clip(); + + // 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: boolean isPointInStroke(unrestricted double x, unrestricted double y); + // FIXME: boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y); +}; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index ef11f68dcd..1220200c05 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,8 @@ class CanvasRenderingContext2D , public CanvasState , public CanvasTransform , public CanvasFillStrokeStyles - , public CanvasRect { + , public CanvasRect + , public CanvasDrawPath { AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D); AK_MAKE_NONMOVABLE(CanvasRenderingContext2D); @@ -60,15 +62,15 @@ public: void set_line_width(float line_width) { drawing_state().line_width = line_width; } float line_width() const { return drawing_state().line_width; } - void begin_path(); - void stroke(); - void stroke(Path2D const& path); + virtual void begin_path() override; + virtual void stroke() override; + virtual void stroke(Path2D const& path) override; void fill_text(String const&, float x, float y, Optional max_width); void stroke_text(String const&, float x, float y, Optional max_width); - void fill(String const& fill_rule); - void fill(Path2D& path, String const& fill_rule); + virtual void fill(String const& fill_rule) override; + virtual void fill(Path2D& path, String const& fill_rule) override; RefPtr create_image_data(int width, int height) const; DOM::ExceptionOr> get_image_data(int x, int y, int width, int height) const; @@ -80,7 +82,7 @@ public: RefPtr measure_text(String const& text); - void clip(); + virtual void clip() override; private: explicit CanvasRenderingContext2D(HTMLCanvasElement&); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl index cbb2e90138..833d04207a 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl @@ -2,25 +2,17 @@ #import #import #import +#import #import #import #import #import #import -#import // https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2d [Exposed=Window] interface CanvasRenderingContext2D { - undefined beginPath(); - // FIXME: `DOMString` should be `CanvasFillRule` - undefined fill(optional DOMString fillRule = "nonzero"); - // FIXME: `DOMString` should be `CanvasFillRule` - undefined fill(Path2D path, optional DOMString fillRule = "nonzero"); - undefined stroke(); - undefined stroke(Path2D path); - undefined fillText(DOMString text, double x, double y, optional double maxWidth); undefined strokeText(DOMString text, double x, double y, optional double maxWidth); @@ -38,15 +30,11 @@ interface CanvasRenderingContext2D { TextMetrics measureText(DOMString text); - // undefined clip(optional CanvasFillRule fillRule = "nonzero"); - // undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero"); - // FIXME: Replace this with the two definitions above. - undefined clip(); - }; CanvasRenderingContext2D includes CanvasState; CanvasRenderingContext2D includes CanvasTransform; CanvasRenderingContext2D includes CanvasFillStrokeStyles; CanvasRenderingContext2D includes CanvasRect; +CanvasRenderingContext2D includes CanvasDrawPath; CanvasRenderingContext2D includes CanvasPath;