CanvasState.h 3.5 KB

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