CanvasFillStrokeStyles.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/String.h>
  10. #include <LibWeb/HTML/Canvas/CanvasState.h>
  11. #include <LibWeb/HTML/CanvasGradient.h>
  12. namespace Web::HTML {
  13. // https://html.spec.whatwg.org/multipage/canvas.html#canvasfillstrokestyles
  14. template<typename IncludingClass>
  15. class CanvasFillStrokeStyles {
  16. public:
  17. ~CanvasFillStrokeStyles() = default;
  18. void set_fill_style(String style)
  19. {
  20. // 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.
  21. my_drawing_state().fill_style = Gfx::Color::from_string(style).value_or(Color::Black);
  22. }
  23. String fill_style() const
  24. {
  25. return my_drawing_state().fill_style.to_string();
  26. }
  27. void set_stroke_style(String style)
  28. {
  29. // 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.
  30. my_drawing_state().stroke_style = Gfx::Color::from_string(style).value_or(Color::Black);
  31. }
  32. String stroke_style() const
  33. {
  34. return my_drawing_state().stroke_style.to_string();
  35. }
  36. JS::NonnullGCPtr<CanvasGradient> create_radial_gradient(double x0, double y0, double r0, double x1, double y1, double r1)
  37. {
  38. auto& realm = static_cast<IncludingClass&>(*this).realm();
  39. return CanvasGradient::create_radial(realm, x0, y0, r0, x1, y1, r1);
  40. }
  41. JS::NonnullGCPtr<CanvasGradient> create_linear_gradient(double x0, double y0, double x1, double y1)
  42. {
  43. auto& realm = static_cast<IncludingClass&>(*this).realm();
  44. return CanvasGradient::create_linear(realm, x0, y0, x1, y1);
  45. }
  46. JS::NonnullGCPtr<CanvasGradient> create_conic_gradient(double start_angle, double x, double y)
  47. {
  48. auto& realm = static_cast<IncludingClass&>(*this).realm();
  49. return CanvasGradient::create_conic(realm, start_angle, x, y);
  50. }
  51. protected:
  52. CanvasFillStrokeStyles() = default;
  53. private:
  54. CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); }
  55. CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); }
  56. };
  57. }