2022-02-03 20:08:27 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2023-01-19 19:10:00 +01:00
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2022-02-03 20:08:27 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-02-05 15:16:35 +01:00
|
|
|
#include <AK/QuickSort.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/CanvasGradientPrototype.h>
|
2022-09-25 16:38:21 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-02-03 20:08:27 +01:00
|
|
|
#include <LibWeb/HTML/CanvasGradient.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2022-02-03 20:08:27 +01:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(CanvasGradient);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2023-01-28 00:05:10 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createradialgradient
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<CanvasGradient>> CanvasGradient::create_radial(JS::Realm& realm, double x0, double y0, double r0, double x1, double y1, double r1)
|
2022-02-03 20:08:27 +01:00
|
|
|
{
|
2023-01-28 00:05:10 +00:00
|
|
|
// If either of r0 or r1 are negative, then an "IndexSizeError" DOMException must be thrown.
|
|
|
|
if (r0 < 0)
|
2024-10-12 20:56:21 +02:00
|
|
|
return WebIDL::IndexSizeError::create(realm, "The r0 passed is less than 0"_string);
|
2023-01-28 00:05:10 +00:00
|
|
|
if (r1 < 0)
|
2024-10-12 20:56:21 +02:00
|
|
|
return WebIDL::IndexSizeError::create(realm, "The r1 passed is less than 0"_string);
|
2023-01-28 00:05:10 +00:00
|
|
|
|
2023-03-01 23:01:59 +00:00
|
|
|
auto radial_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasRadialGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, r0, Gfx::FloatPoint { x1, y1 }, r1));
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<CanvasGradient>(realm, *radial_gradient);
|
2022-02-03 20:08:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 00:10:54 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createlineargradient
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<CanvasGradient>> CanvasGradient::create_linear(JS::Realm& realm, double x0, double y0, double x1, double y1)
|
2022-02-03 20:08:27 +01:00
|
|
|
{
|
2023-03-01 23:01:59 +00:00
|
|
|
auto linear_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasLinearGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, Gfx::FloatPoint { x1, y1 }));
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<CanvasGradient>(realm, *linear_gradient);
|
2022-02-03 20:08:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 00:10:54 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createconicgradient
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<CanvasGradient>> CanvasGradient::create_conic(JS::Realm& realm, double start_angle, double x, double y)
|
2022-02-03 20:08:27 +01:00
|
|
|
{
|
2023-03-01 23:01:59 +00:00
|
|
|
auto conic_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasConicGradientPaintStyle::create(Gfx::FloatPoint { x, y }, start_angle));
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<CanvasGradient>(realm, *conic_gradient);
|
2022-02-03 20:08:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 19:10:00 +01:00
|
|
|
CanvasGradient::CanvasGradient(JS::Realm& realm, Gfx::GradientPaintStyle& gradient)
|
2022-09-25 16:38:21 -06:00
|
|
|
: PlatformObject(realm)
|
2023-01-19 19:10:00 +01:00
|
|
|
, m_gradient(gradient)
|
2022-02-03 20:08:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
CanvasGradient::~CanvasGradient() = default;
|
2022-02-03 20:08:27 +01:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void CanvasGradient::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(CanvasGradient);
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:16:35 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-canvasgradient-addcolorstop
|
2023-09-04 20:47:08 +12:00
|
|
|
WebIDL::ExceptionOr<void> CanvasGradient::add_color_stop(double offset, StringView color)
|
2022-02-03 20:08:27 +01:00
|
|
|
{
|
2022-02-05 15:16:35 +01:00
|
|
|
// 1. If the offset is less than 0 or greater than 1, then throw an "IndexSizeError" DOMException.
|
|
|
|
if (offset < 0 || offset > 1)
|
2024-10-12 20:56:21 +02:00
|
|
|
return WebIDL::IndexSizeError::create(realm(), "CanvasGradient color stop offset out of bounds"_string);
|
2022-02-05 15:16:35 +01:00
|
|
|
|
|
|
|
// 2. Let parsed color be the result of parsing color.
|
|
|
|
auto parsed_color = Color::from_string(color);
|
|
|
|
|
|
|
|
// 3. If parsed color is failure, throw a "SyntaxError" DOMException.
|
|
|
|
if (!parsed_color.has_value())
|
2024-10-12 20:56:21 +02:00
|
|
|
return WebIDL::SyntaxError::create(realm(), "Could not parse color for CanvasGradient"_string);
|
2022-02-05 15:16:35 +01:00
|
|
|
|
|
|
|
// 4. Place a new stop on the gradient, at offset offset relative to the whole gradient, and with the color parsed color.
|
2023-03-01 23:02:27 +00:00
|
|
|
TRY_OR_THROW_OOM(realm().vm(), m_gradient->add_color_stop(offset, parsed_color.value()));
|
2022-02-05 15:16:35 +01:00
|
|
|
|
|
|
|
// FIXME: If multiple stops are added at the same offset on a gradient, then they must be placed in the order added,
|
|
|
|
// with the first one closest to the start of the gradient, and each subsequent one infinitesimally further along
|
|
|
|
// towards the end point (in effect causing all but the first and last stop added at each point to be ignored).
|
|
|
|
|
|
|
|
return {};
|
2022-02-03 20:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|