diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-08-12 15:46:31 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-08-14 11:30:40 +0200 |
commit | afabd613bd1dad7f2597edeef76f9c193ff4fa0d (patch) | |
tree | 24faae406ce5d51a44b7566edaa7119e12f6b2d9 /Userland/Libraries/LibWeb/HTML | |
parent | aa87b9699eb098097ae322206f3a22afc3f553fa (diff) | |
download | serenity-afabd613bd1dad7f2597edeef76f9c193ff4fa0d.zip |
LibWeb: Extract CanvasFillStrokeStyles class from CRC2D
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
5 files changed, 70 insertions, 36 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h new file mode 100644 index 0000000000..0f0e773256 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/String.h> +#include <LibWeb/HTML/Canvas/CanvasState.h> + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillstrokestyles +template<typename IncludingClass> +class CanvasFillStrokeStyles { +public: + ~CanvasFillStrokeStyles() = default; + + void set_fill_style(String style) + { + // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. + my_drawing_state().fill_style = Gfx::Color::from_string(style).value_or(Color::Black); + } + + String fill_style() const + { + return my_drawing_state().fill_style.to_string(); + } + + void set_stroke_style(String style) + { + // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. + my_drawing_state().stroke_style = Gfx::Color::from_string(style).value_or(Color::Black); + } + + String stroke_style() const + { + return my_drawing_state().stroke_style.to_string(); + } + +protected: + CanvasFillStrokeStyles() = default; + +private: + CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); } + CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); } +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.idl b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.idl new file mode 100644 index 0000000000..45b393c7cf --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.idl @@ -0,0 +1,13 @@ +#import <HTML/CanvasGradient.idl> + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillstrokestyles +interface mixin CanvasFillStrokeStyles { + // FIXME: Should be `(DOMString or CanvasGradient or CanvasPattern)` + attribute DOMString strokeStyle; + // FIXME: Should be `(DOMString or CanvasGradient or CanvasPattern)` + attribute DOMString fillStyle; + CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1); + CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1); + CanvasGradient createConicGradient(double startAngle, double x, double y); + // FIXME: CanvasPattern? createPattern(CanvasImageSource image, [LegacyNullToEmptyString] DOMString repetition); +}; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index d20449d17e..98bb90d309 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -46,17 +46,6 @@ NonnullRefPtr<HTMLCanvasElement> CanvasRenderingContext2D::canvas_for_binding() return canvas_element(); } -void CanvasRenderingContext2D::set_fill_style(String style) -{ - // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. - drawing_state().fill_style = Gfx::Color::from_string(style).value_or(Color::Black); -} - -String CanvasRenderingContext2D::fill_style() const -{ - return drawing_state().fill_style.to_string(); -} - void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height) { auto painter = this->painter(); @@ -81,17 +70,6 @@ void CanvasRenderingContext2D::clear_rect(float x, float y, float width, float h did_draw(rect); } -void CanvasRenderingContext2D::set_stroke_style(String style) -{ - // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. - drawing_state().stroke_style = Gfx::Color::from_string(style).value_or(Color::Black); -} - -String CanvasRenderingContext2D::stroke_style() const -{ - return drawing_state().stroke_style.to_string(); -} - void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height) { auto painter = this->painter(); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index ee4c1f2b9c..e442a1ebcc 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -16,6 +16,7 @@ #include <LibGfx/Path.h> #include <LibWeb/Bindings/Wrappable.h> #include <LibWeb/DOM/ExceptionOr.h> +#include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h> #include <LibWeb/HTML/Canvas/CanvasPath.h> #include <LibWeb/HTML/Canvas/CanvasState.h> #include <LibWeb/HTML/Canvas/CanvasTransform.h> @@ -34,7 +35,8 @@ class CanvasRenderingContext2D , public Bindings::Wrappable , public CanvasPath , public CanvasState - , public CanvasTransform<CanvasRenderingContext2D> { + , public CanvasTransform<CanvasRenderingContext2D> + , public CanvasFillStrokeStyles<CanvasRenderingContext2D> { AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D); AK_MAKE_NONMOVABLE(CanvasRenderingContext2D); @@ -45,12 +47,6 @@ public: static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt_ref(*new CanvasRenderingContext2D(element)); } ~CanvasRenderingContext2D(); - void set_fill_style(String); - String fill_style() const; - - void set_stroke_style(String); - String stroke_style() const; - void fill_rect(float x, float y, float width, float height); void stroke_rect(float x, float y, float width, float height); void clear_rect(float x, float y, float width, float height); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl index 8191f524ae..2a7de9dffd 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl @@ -2,7 +2,7 @@ #import <HTML/HTMLImageElement.idl> #import <HTML/ImageData.idl> #import <HTML/TextMetrics.idl> -#import <HTML/CanvasGradient.idl> +#import <HTML/Canvas/CanvasFillStrokeStyles.idl> #import <HTML/Canvas/CanvasPath.idl> #import <HTML/Canvas/CanvasState.idl> #import <HTML/Canvas/CanvasTransform.idl> @@ -31,8 +31,6 @@ interface CanvasRenderingContext2D { 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); - attribute DOMString fillStyle; - attribute DOMString strokeStyle; attribute double lineWidth; ImageData createImageData(long sw, long sh); @@ -43,10 +41,6 @@ interface CanvasRenderingContext2D { TextMetrics measureText(DOMString text); - CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1); - CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1); - CanvasGradient createConicGradient(double startAngle, double x, double y); - // undefined clip(optional CanvasFillRule fillRule = "nonzero"); // undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero"); // FIXME: Replace this with the two definitions above. @@ -56,4 +50,5 @@ interface CanvasRenderingContext2D { CanvasRenderingContext2D includes CanvasState; CanvasRenderingContext2D includes CanvasTransform; +CanvasRenderingContext2D includes CanvasFillStrokeStyles; CanvasRenderingContext2D includes CanvasPath; |