CanvasState.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Variant.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/Color.h>
  12. #include <LibGfx/PaintStyle.h>
  13. #include <LibWeb/HTML/CanvasGradient.h>
  14. #include <LibWeb/HTML/CanvasPattern.h>
  15. namespace Web::HTML {
  16. // https://html.spec.whatwg.org/multipage/canvas.html#canvasstate
  17. class CanvasState {
  18. public:
  19. virtual ~CanvasState() = default;
  20. void save();
  21. void restore();
  22. void reset();
  23. bool is_context_lost();
  24. using FillOrStrokeVariant = Variant<Gfx::Color, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
  25. struct FillOrStrokeStyle {
  26. FillOrStrokeStyle(Gfx::Color color)
  27. : m_fill_or_stroke_style(color)
  28. {
  29. }
  30. FillOrStrokeStyle(JS::Handle<CanvasGradient> gradient)
  31. : m_fill_or_stroke_style(gradient)
  32. {
  33. }
  34. FillOrStrokeStyle(JS::Handle<CanvasPattern> pattern)
  35. : m_fill_or_stroke_style(pattern)
  36. {
  37. }
  38. NonnullRefPtr<Gfx::PaintStyle> to_gfx_paint_style();
  39. Optional<Gfx::Color> as_color() const;
  40. Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
  41. using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
  42. JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
  43. {
  44. return m_fill_or_stroke_style.visit(
  45. [&](Gfx::Color color) -> JsFillOrStrokeStyle {
  46. return color.to_deprecated_string();
  47. },
  48. [&](auto handle) -> JsFillOrStrokeStyle {
  49. return handle;
  50. });
  51. }
  52. private:
  53. FillOrStrokeVariant m_fill_or_stroke_style;
  54. RefPtr<Gfx::PaintStyle> m_color_paint_style { nullptr };
  55. };
  56. // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
  57. struct DrawingState {
  58. Gfx::AffineTransform transform;
  59. FillOrStrokeStyle fill_style { Gfx::Color::Black };
  60. FillOrStrokeStyle stroke_style { Gfx::Color::Black };
  61. float line_width { 1 };
  62. };
  63. DrawingState& drawing_state() { return m_drawing_state; }
  64. DrawingState const& drawing_state() const { return m_drawing_state; }
  65. void clear_drawing_state_stack() { m_drawing_state_stack.clear(); }
  66. void reset_drawing_state() { m_drawing_state = {}; }
  67. virtual void reset_to_default_state() = 0;
  68. protected:
  69. CanvasState() = default;
  70. private:
  71. DrawingState m_drawing_state;
  72. Vector<DrawingState> m_drawing_state_stack;
  73. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-context-lost
  74. bool m_context_lost { false };
  75. };
  76. }