diff options
4 files changed, 44 insertions, 10 deletions
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 <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Optional.h> +#include <AK/String.h> +#include <LibWeb/HTML/TextMetrics.h> + +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<double> max_width) = 0; + virtual void stroke_text(String const&, float x, float y, Optional<double> max_width) = 0; + virtual RefPtr<TextMetrics> 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 <HTML/TextMetrics.idl> + +// 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 <LibWeb/HTML/Canvas/CanvasPath.h> #include <LibWeb/HTML/Canvas/CanvasRect.h> #include <LibWeb/HTML/Canvas/CanvasState.h> +#include <LibWeb/HTML/Canvas/CanvasText.h> #include <LibWeb/HTML/Canvas/CanvasTransform.h> #include <LibWeb/HTML/CanvasGradient.h> #include <LibWeb/Layout/InlineNode.h> @@ -40,7 +41,8 @@ class CanvasRenderingContext2D , public CanvasTransform<CanvasRenderingContext2D> , public CanvasFillStrokeStyles<CanvasRenderingContext2D> , 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<double> max_width); - void stroke_text(String const&, float x, float y, Optional<double> max_width); + virtual void fill_text(String const&, float x, float y, Optional<double> max_width) override; + virtual void stroke_text(String const&, float x, float y, Optional<double> 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<HTMLCanvasElement> canvas_for_binding() const; - RefPtr<TextMetrics> measure_text(String const& text); + virtual RefPtr<TextMetrics> 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 <HTML/HTMLCanvasElement.idl> #import <HTML/HTMLImageElement.idl> #import <HTML/ImageData.idl> -#import <HTML/TextMetrics.idl> #import <HTML/Canvas/CanvasDrawPath.idl> #import <HTML/Canvas/CanvasFillStrokeStyles.idl> #import <HTML/Canvas/CanvasPath.idl> #import <HTML/Canvas/CanvasRect.idl> #import <HTML/Canvas/CanvasState.idl> +#import <HTML/Canvas/CanvasText.idl> #import <HTML/Canvas/CanvasTransform.idl> // 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; |