CanvasState.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. virtual ~CanvasState() = default;
  24. virtual Gfx::Painter* painter_for_canvas_state() = 0;
  25. virtual Gfx::Path& path_for_canvas_state() = 0;
  26. void save();
  27. void restore();
  28. void reset();
  29. bool is_context_lost();
  30. using FillOrStrokeVariant = Variant<Gfx::Color, GC::Root<CanvasGradient>, GC::Root<CanvasPattern>>;
  31. struct FillOrStrokeStyle {
  32. FillOrStrokeStyle(Gfx::Color color)
  33. : m_fill_or_stroke_style(color)
  34. {
  35. }
  36. FillOrStrokeStyle(GC::Root<CanvasGradient> gradient)
  37. : m_fill_or_stroke_style(gradient)
  38. {
  39. }
  40. FillOrStrokeStyle(GC::Root<CanvasPattern> pattern)
  41. : m_fill_or_stroke_style(pattern)
  42. {
  43. }
  44. NonnullRefPtr<Gfx::PaintStyle> to_gfx_paint_style();
  45. Optional<Gfx::Color> as_color() const;
  46. Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
  47. using JsFillOrStrokeStyle = Variant<String, GC::Root<CanvasGradient>, GC::Root<CanvasPattern>>;
  48. JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
  49. {
  50. return m_fill_or_stroke_style.visit(
  51. [&](Gfx::Color color) -> JsFillOrStrokeStyle {
  52. return color.to_string(Gfx::Color::HTMLCompatibleSerialization::Yes);
  53. },
  54. [&](auto handle) -> JsFillOrStrokeStyle {
  55. return handle;
  56. });
  57. }
  58. private:
  59. FillOrStrokeVariant m_fill_or_stroke_style;
  60. RefPtr<Gfx::PaintStyle> m_color_paint_style { nullptr };
  61. };
  62. // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
  63. struct DrawingState {
  64. Gfx::AffineTransform transform;
  65. FillOrStrokeStyle fill_style { Gfx::Color::Black };
  66. FillOrStrokeStyle stroke_style { Gfx::Color::Black };
  67. float shadow_offset_x { 0.0f };
  68. float shadow_offset_y { 0.0f };
  69. Gfx::Color shadow_color { Gfx::Color::Transparent };
  70. float line_width { 1 };
  71. Bindings::CanvasLineCap line_cap { Bindings::CanvasLineCap::Butt };
  72. Bindings::CanvasLineJoin line_join { Bindings::CanvasLineJoin::Miter };
  73. float miter_limit { 10 };
  74. Vector<double> dash_list;
  75. float line_dash_offset { 0 };
  76. bool image_smoothing_enabled { true };
  77. Bindings::ImageSmoothingQuality image_smoothing_quality { Bindings::ImageSmoothingQuality::Low };
  78. float global_alpha = { 1 };
  79. RefPtr<CSS::CSSStyleValue> font_style_value { nullptr };
  80. RefPtr<Gfx::Font const> current_font { nullptr };
  81. Bindings::CanvasTextAlign text_align { Bindings::CanvasTextAlign::Start };
  82. Bindings::CanvasTextBaseline text_baseline { Bindings::CanvasTextBaseline::Alphabetic };
  83. };
  84. DrawingState& drawing_state() { return m_drawing_state; }
  85. DrawingState const& drawing_state() const { return m_drawing_state; }
  86. void clear_drawing_state_stack() { m_drawing_state_stack.clear(); }
  87. void reset_drawing_state() { m_drawing_state = {}; }
  88. virtual void reset_to_default_state() = 0;
  89. protected:
  90. CanvasState() = default;
  91. private:
  92. DrawingState m_drawing_state;
  93. Vector<DrawingState> m_drawing_state_stack;
  94. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-context-lost
  95. bool m_context_lost { false };
  96. };
  97. }