CanvasGradient.cpp 3.7 KB

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