CanvasGradient.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/QuickSort.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/HTML/CanvasGradient.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::HTML {
  12. JS_DEFINE_ALLOCATOR(CanvasGradient);
  13. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createradialgradient
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_radial(JS::Realm& realm, double x0, double y0, double r0, double x1, double y1, double r1)
  15. {
  16. // If either of r0 or r1 are negative, then an "IndexSizeError" DOMException must be thrown.
  17. if (r0 < 0)
  18. return WebIDL::IndexSizeError::create(realm, "The r0 passed is less than 0"_fly_string);
  19. if (r1 < 0)
  20. return WebIDL::IndexSizeError::create(realm, "The r1 passed is less than 0"_fly_string);
  21. auto radial_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasRadialGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, r0, Gfx::FloatPoint { x1, y1 }, r1));
  22. return realm.heap().allocate<CanvasGradient>(realm, realm, *radial_gradient);
  23. }
  24. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createlineargradient
  25. WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_linear(JS::Realm& realm, double x0, double y0, double x1, double y1)
  26. {
  27. auto linear_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasLinearGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, Gfx::FloatPoint { x1, y1 }));
  28. return realm.heap().allocate<CanvasGradient>(realm, realm, *linear_gradient);
  29. }
  30. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createconicgradient
  31. WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_conic(JS::Realm& realm, double start_angle, double x, double y)
  32. {
  33. auto conic_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasConicGradientPaintStyle::create(Gfx::FloatPoint { x, y }, start_angle));
  34. return realm.heap().allocate<CanvasGradient>(realm, realm, *conic_gradient);
  35. }
  36. CanvasGradient::CanvasGradient(JS::Realm& realm, Gfx::GradientPaintStyle& gradient)
  37. : PlatformObject(realm)
  38. , m_gradient(gradient)
  39. {
  40. }
  41. CanvasGradient::~CanvasGradient() = default;
  42. void CanvasGradient::initialize(JS::Realm& realm)
  43. {
  44. Base::initialize(realm);
  45. WEB_SET_PROTOTYPE_FOR_INTERFACE(CanvasGradient);
  46. }
  47. // https://html.spec.whatwg.org/multipage/canvas.html#dom-canvasgradient-addcolorstop
  48. WebIDL::ExceptionOr<void> CanvasGradient::add_color_stop(double offset, StringView color)
  49. {
  50. // 1. If the offset is less than 0 or greater than 1, then throw an "IndexSizeError" DOMException.
  51. if (offset < 0 || offset > 1)
  52. return WebIDL::IndexSizeError::create(realm(), "CanvasGradient color stop offset out of bounds"_fly_string);
  53. // 2. Let parsed color be the result of parsing color.
  54. auto parsed_color = Color::from_string(color);
  55. // 3. If parsed color is failure, throw a "SyntaxError" DOMException.
  56. if (!parsed_color.has_value())
  57. return WebIDL::SyntaxError::create(realm(), "Could not parse color for CanvasGradient"_fly_string);
  58. // 4. Place a new stop on the gradient, at offset offset relative to the whole gradient, and with the color parsed color.
  59. TRY_OR_THROW_OOM(realm().vm(), m_gradient->add_color_stop(offset, parsed_color.value()));
  60. // FIXME: If multiple stops are added at the same offset on a gradient, then they must be placed in the order added,
  61. // with the first one closest to the start of the gradient, and each subsequent one infinitesimally further along
  62. // towards the end point (in effect causing all but the first and last stop added at each point to be ignored).
  63. return {};
  64. }
  65. }