CanvasState.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/Bindings/CanvasRenderingContext2DPrototype.h>
  14. #include <LibWeb/HTML/CanvasGradient.h>
  15. #include <LibWeb/HTML/CanvasPattern.h>
  16. namespace Web::HTML {
  17. // https://html.spec.whatwg.org/multipage/canvas.html#canvasstate
  18. class CanvasState {
  19. public:
  20. virtual ~CanvasState() = default;
  21. void save();
  22. void restore();
  23. void reset();
  24. bool is_context_lost();
  25. using FillOrStrokeVariant = Variant<Gfx::Color, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
  26. struct FillOrStrokeStyle {
  27. FillOrStrokeStyle(Gfx::Color color)
  28. : m_fill_or_stroke_style(color)
  29. {
  30. }
  31. FillOrStrokeStyle(JS::Handle<CanvasGradient> gradient)
  32. : m_fill_or_stroke_style(gradient)
  33. {
  34. }
  35. FillOrStrokeStyle(JS::Handle<CanvasPattern> pattern)
  36. : m_fill_or_stroke_style(pattern)
  37. {
  38. }
  39. NonnullRefPtr<Gfx::PaintStyle> to_gfx_paint_style();
  40. Optional<Gfx::Color> as_color() const;
  41. Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
  42. using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
  43. JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
  44. {
  45. return m_fill_or_stroke_style.visit(
  46. [&](Gfx::Color color) -> JsFillOrStrokeStyle {
  47. return color.to_deprecated_string();
  48. },
  49. [&](auto handle) -> JsFillOrStrokeStyle {
  50. return handle;
  51. });
  52. }
  53. private:
  54. FillOrStrokeVariant m_fill_or_stroke_style;
  55. RefPtr<Gfx::PaintStyle> m_color_paint_style { nullptr };
  56. };
  57. // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
  58. struct DrawingState {
  59. Gfx::AffineTransform transform;
  60. FillOrStrokeStyle fill_style { Gfx::Color::Black };
  61. FillOrStrokeStyle stroke_style { Gfx::Color::Black };
  62. float line_width { 1 };
  63. bool image_smoothing_enabled { true };
  64. Bindings::ImageSmoothingQuality image_smoothing_quality { Bindings::ImageSmoothingQuality::Low };
  65. };
  66. DrawingState& drawing_state() { return m_drawing_state; }
  67. DrawingState const& drawing_state() const { return m_drawing_state; }
  68. void clear_drawing_state_stack() { m_drawing_state_stack.clear(); }
  69. void reset_drawing_state() { m_drawing_state = {}; }
  70. virtual void reset_to_default_state() = 0;
  71. protected:
  72. CanvasState() = default;
  73. private:
  74. DrawingState m_drawing_state;
  75. Vector<DrawingState> m_drawing_state_stack;
  76. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-context-lost
  77. bool m_context_lost { false };
  78. };
  79. }