1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

LibWeb: Update CRC2D .fillStyle and .strokeStyle to accept gradients

While doing add some structures to hold these new fill styles and
plumb them over to the painter.
This commit is contained in:
MacDue 2023-01-18 20:10:00 +01:00 committed by Andreas Kling
parent 2be4142138
commit 24cb57ac88
5 changed files with 89 additions and 17 deletions

View file

@ -1,14 +1,18 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Color.h>
#include <LibGfx/PaintStyle.h>
#include <LibWeb/HTML/CanvasGradient.h>
namespace Web::HTML {
@ -22,11 +26,41 @@ public:
void reset();
bool is_context_lost();
using FillOrStrokeVariant = Variant<Gfx::Color, JS::Handle<CanvasGradient>>;
struct FillOrStrokeStyle {
FillOrStrokeStyle(Gfx::Color color)
: m_fill_or_stoke_style(color)
{
}
FillOrStrokeStyle(JS::Handle<CanvasGradient> gradient)
: m_fill_or_stoke_style(gradient)
{
}
NonnullRefPtr<Gfx::PaintStyle> to_gfx_paint_style();
Optional<Gfx::Color> as_color() const;
Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
Variant<DeprecatedString, JS::Handle<CanvasGradient>> to_js_fill_or_stoke_style() const
{
if (auto* handle = m_fill_or_stoke_style.get_pointer<JS::Handle<CanvasGradient>>())
return *handle;
return m_fill_or_stoke_style.get<Gfx::Color>().to_deprecated_string();
}
private:
FillOrStrokeVariant m_fill_or_stoke_style;
RefPtr<Gfx::PaintStyle> m_color_fill_style { nullptr };
};
// https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
struct DrawingState {
Gfx::AffineTransform transform;
Gfx::Color fill_style { Gfx::Color::Black };
Gfx::Color stroke_style { Gfx::Color::Black };
FillOrStrokeStyle fill_style { Gfx::Color::Black };
FillOrStrokeStyle stroke_style { Gfx::Color::Black };
float line_width { 1 };
};
DrawingState& drawing_state() { return m_drawing_state; }