CanvasRenderingContext2D.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefCounted.h>
  9. #include <AK/Variant.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/Color.h>
  12. #include <LibGfx/Forward.h>
  13. #include <LibGfx/Painter.h>
  14. #include <LibGfx/Path.h>
  15. #include <LibWeb/Bindings/Wrappable.h>
  16. #include <LibWeb/DOM/ExceptionOr.h>
  17. #include <LibWeb/HTML/CanvasGradient.h>
  18. #include <LibWeb/Layout/InlineNode.h>
  19. #include <LibWeb/Layout/LineBox.h>
  20. namespace Web::HTML {
  21. // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
  22. // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
  23. using CanvasImageSource = Variant<NonnullRefPtr<HTMLImageElement>, NonnullRefPtr<HTMLCanvasElement>>;
  24. class CanvasRenderingContext2D
  25. : public RefCounted<CanvasRenderingContext2D>
  26. , public Bindings::Wrappable {
  27. AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
  28. AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
  29. public:
  30. using WrapperType = Bindings::CanvasRenderingContext2DWrapper;
  31. static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt_ref(*new CanvasRenderingContext2D(element)); }
  32. ~CanvasRenderingContext2D();
  33. void set_fill_style(String);
  34. String fill_style() const;
  35. void set_stroke_style(String);
  36. String stroke_style() const;
  37. void fill_rect(float x, float y, float width, float height);
  38. void stroke_rect(float x, float y, float width, float height);
  39. void clear_rect(float x, float y, float width, float height);
  40. DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y);
  41. DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y, float destination_width, float destination_height);
  42. DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float source_x, float source_y, float source_width, float source_height, float destination_x, float destination_y, float destination_width, float destination_height);
  43. void scale(float sx, float sy);
  44. void translate(float x, float y);
  45. void rotate(float degrees);
  46. void set_line_width(float line_width) { m_drawing_state.line_width = line_width; }
  47. float line_width() const { return m_drawing_state.line_width; }
  48. void begin_path();
  49. void close_path();
  50. void move_to(float x, float y);
  51. void line_to(float x, float y);
  52. void quadratic_curve_to(float cx, float cy, float x, float y);
  53. void bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
  54. DOM::ExceptionOr<void> arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
  55. DOM::ExceptionOr<void> ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
  56. void rect(float x, float y, float width, float height);
  57. void stroke();
  58. void fill_text(const String&, float x, float y, Optional<double> max_width);
  59. void stroke_text(String const&, float x, float y, Optional<double> max_width);
  60. // FIXME: We should only have one fill(), really. Fix the wrapper generator!
  61. void fill(Gfx::Painter::WindingRule);
  62. void fill(const String& fill_rule);
  63. RefPtr<ImageData> create_image_data(int width, int height) const;
  64. DOM::ExceptionOr<RefPtr<ImageData>> get_image_data(int x, int y, int width, int height) const;
  65. void put_image_data(const ImageData&, float x, float y);
  66. void save();
  67. void restore();
  68. void reset();
  69. bool is_context_lost();
  70. void reset_to_default_state();
  71. HTMLCanvasElement* canvas() { return m_element; }
  72. RefPtr<TextMetrics> measure_text(String const& text);
  73. NonnullRefPtr<CanvasGradient> create_radial_gradient(double x0, double y0, double r0, double x1, double y1, double r1);
  74. NonnullRefPtr<CanvasGradient> create_linear_gradient(double x0, double y0, double x1, double y1);
  75. NonnullRefPtr<CanvasGradient> create_conic_gradient(double start_angle, double x, double y);
  76. private:
  77. explicit CanvasRenderingContext2D(HTMLCanvasElement&);
  78. struct PreparedTextGlyph {
  79. unsigned int c;
  80. Gfx::IntPoint position;
  81. };
  82. struct PreparedText {
  83. Vector<PreparedTextGlyph> glyphs;
  84. Gfx::TextAlignment physical_alignment;
  85. Gfx::IntRect bounding_box;
  86. };
  87. void did_draw(const Gfx::FloatRect&);
  88. PreparedText prepare_text(String const& text, float max_width = INFINITY);
  89. OwnPtr<Gfx::Painter> painter();
  90. WeakPtr<HTMLCanvasElement> m_element;
  91. // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state
  92. struct DrawingState {
  93. Gfx::AffineTransform transform;
  94. Gfx::Color fill_style { Gfx::Color::Black };
  95. Gfx::Color stroke_style { Gfx::Color::Black };
  96. float line_width { 1 };
  97. };
  98. DrawingState m_drawing_state;
  99. Vector<DrawingState> m_drawing_state_stack;
  100. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
  101. bool m_origin_clean { true };
  102. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-context-lost
  103. bool m_context_lost { false };
  104. Gfx::Path m_path;
  105. };
  106. enum class CanvasImageSourceUsability {
  107. Bad,
  108. Good,
  109. };
  110. DOM::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
  111. bool image_is_not_origin_clean(CanvasImageSource const&);
  112. }