CanvasRenderingContext2D.h 4.4 KB

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