From 7cf42ede68938909bec80e7f7dc2ac7b070e7bff Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 12 Aug 2022 19:10:01 +0100 Subject: LibWeb: Extract CanvasText class from CRC2D --- Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h | 28 ++++++++++++++++++++++ .../Libraries/LibWeb/HTML/Canvas/CanvasText.idl | 8 +++++++ .../LibWeb/HTML/CanvasRenderingContext2D.h | 10 ++++---- .../LibWeb/HTML/CanvasRenderingContext2D.idl | 8 ++----- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h create mode 100644 Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.idl diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h new file mode 100644 index 0000000000..d49e07df10 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/canvas.html#canvastext +class CanvasText { +public: + virtual ~CanvasText() = default; + + virtual void fill_text(String const&, float x, float y, Optional max_width) = 0; + virtual void stroke_text(String const&, float x, float y, Optional max_width) = 0; + virtual RefPtr measure_text(String const& text) = 0; + +protected: + CanvasText() = default; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.idl b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.idl new file mode 100644 index 0000000000..57475757eb --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.idl @@ -0,0 +1,8 @@ +#import + +// https://html.spec.whatwg.org/multipage/canvas.html#canvastext +interface mixin CanvasText { + undefined fillText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth); + undefined strokeText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth); + TextMetrics measureText(DOMString text); +}; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 1220200c05..6f94d0c55b 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,8 @@ class CanvasRenderingContext2D , public CanvasTransform , public CanvasFillStrokeStyles , public CanvasRect - , public CanvasDrawPath { + , public CanvasDrawPath + , public CanvasText { AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D); AK_MAKE_NONMOVABLE(CanvasRenderingContext2D); @@ -66,8 +68,8 @@ public: 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); + virtual void fill_text(String const&, float x, float y, Optional max_width) override; + virtual void stroke_text(String const&, float x, float y, Optional max_width) override; virtual void fill(String const& fill_rule) override; virtual void fill(Path2D& path, String const& fill_rule) override; @@ -80,7 +82,7 @@ public: NonnullRefPtr canvas_for_binding() const; - RefPtr measure_text(String const& text); + virtual RefPtr measure_text(String const& text) override; virtual void clip() override; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl index 833d04207a..346d85dd75 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl @@ -1,21 +1,18 @@ #import #import #import -#import #import #import #import #import #import +#import #import // https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2d [Exposed=Window] interface CanvasRenderingContext2D { - undefined fillText(DOMString text, double x, double y, optional double maxWidth); - undefined strokeText(DOMString text, double x, double y, optional double maxWidth); - undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double dx, double dy); undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double dx, double dy, double dw, double dh); undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh); @@ -28,8 +25,6 @@ interface CanvasRenderingContext2D { [ImplementedAs=canvas_for_binding] readonly attribute HTMLCanvasElement canvas; - TextMetrics measureText(DOMString text); - }; CanvasRenderingContext2D includes CanvasState; @@ -37,4 +32,5 @@ CanvasRenderingContext2D includes CanvasTransform; CanvasRenderingContext2D includes CanvasFillStrokeStyles; CanvasRenderingContext2D includes CanvasRect; CanvasRenderingContext2D includes CanvasDrawPath; +CanvasRenderingContext2D includes CanvasText; CanvasRenderingContext2D includes CanvasPath; -- cgit v1.2.3