CanvasState.h 3.4 KB

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