Browse Source

LibWeb: Extract CanvasFillStrokeStyles class from CRC2D

Sam Atkins 3 năm trước cách đây
mục cha
commit
afabd613bd

+ 52 - 0
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(); }
+};
+
+}

+ 13 - 0
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);
+};

+ 0 - 22
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();

+ 3 - 7
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);

+ 2 - 7
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;