CanvasRenderingContext2D.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/RefCountForwarder.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/Canvas/CanvasDrawImage.h>
  18. #include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
  19. #include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
  20. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  21. #include <LibWeb/HTML/Canvas/CanvasRect.h>
  22. #include <LibWeb/HTML/Canvas/CanvasState.h>
  23. #include <LibWeb/HTML/Canvas/CanvasText.h>
  24. #include <LibWeb/HTML/Canvas/CanvasTransform.h>
  25. #include <LibWeb/HTML/CanvasGradient.h>
  26. #include <LibWeb/Layout/InlineNode.h>
  27. #include <LibWeb/Layout/LineBox.h>
  28. namespace Web::HTML {
  29. // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
  30. // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
  31. using CanvasImageSource = Variant<NonnullRefPtr<HTMLImageElement>, NonnullRefPtr<HTMLCanvasElement>>;
  32. class CanvasRenderingContext2D
  33. : public RefCountForwarder<HTMLCanvasElement>
  34. , public Bindings::Wrappable
  35. , public CanvasPath
  36. , public CanvasState
  37. , public CanvasTransform<CanvasRenderingContext2D>
  38. , public CanvasFillStrokeStyles<CanvasRenderingContext2D>
  39. , public CanvasRect
  40. , public CanvasDrawPath
  41. , public CanvasText
  42. , public CanvasDrawImage {
  43. AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
  44. AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
  45. public:
  46. using WrapperType = Bindings::CanvasRenderingContext2DWrapper;
  47. static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt_ref(*new CanvasRenderingContext2D(element)); }
  48. ~CanvasRenderingContext2D();
  49. virtual void fill_rect(float x, float y, float width, float height) override;
  50. virtual void stroke_rect(float x, float y, float width, float height) override;
  51. virtual void clear_rect(float x, float y, float width, float height) override;
  52. virtual DOM::ExceptionOr<void> draw_image_internal(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) override;
  53. void set_line_width(float line_width) { drawing_state().line_width = line_width; }
  54. float line_width() const { return drawing_state().line_width; }
  55. virtual void begin_path() override;
  56. virtual void stroke() override;
  57. virtual void stroke(Path2D const& path) override;
  58. virtual void fill_text(String const&, float x, float y, Optional<double> max_width) override;
  59. virtual void stroke_text(String const&, float x, float y, Optional<double> max_width) override;
  60. virtual void fill(String const& fill_rule) override;
  61. virtual void fill(Path2D& path, String const& fill_rule) override;
  62. RefPtr<ImageData> create_image_data(int width, int height) const;
  63. DOM::ExceptionOr<RefPtr<ImageData>> get_image_data(int x, int y, int width, int height) const;
  64. void put_image_data(ImageData const&, float x, float y);
  65. virtual void reset_to_default_state() override;
  66. NonnullRefPtr<HTMLCanvasElement> canvas_for_binding() const;
  67. virtual RefPtr<TextMetrics> measure_text(String const& text) override;
  68. virtual void clip() override;
  69. private:
  70. explicit CanvasRenderingContext2D(HTMLCanvasElement&);
  71. struct PreparedTextGlyph {
  72. unsigned int c;
  73. Gfx::IntPoint position;
  74. };
  75. struct PreparedText {
  76. Vector<PreparedTextGlyph> glyphs;
  77. Gfx::TextAlignment physical_alignment;
  78. Gfx::IntRect bounding_box;
  79. };
  80. void did_draw(Gfx::FloatRect const&);
  81. PreparedText prepare_text(String const& text, float max_width = INFINITY);
  82. OwnPtr<Gfx::Painter> painter();
  83. HTMLCanvasElement& canvas_element();
  84. HTMLCanvasElement const& canvas_element() const;
  85. void stroke_internal(Gfx::Path const&);
  86. void fill_internal(Gfx::Path&, String const& fill_rule);
  87. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
  88. bool m_origin_clean { true };
  89. };
  90. enum class CanvasImageSourceUsability {
  91. Bad,
  92. Good,
  93. };
  94. DOM::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
  95. bool image_is_not_origin_clean(CanvasImageSource const&);
  96. }