CanvasRenderingContext2D.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.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/String.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/PlatformObject.h>
  16. #include <LibWeb/HTML/Canvas/CanvasCompositing.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/CanvasImageData.h>
  21. #include <LibWeb/HTML/Canvas/CanvasImageSmoothing.h>
  22. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  23. #include <LibWeb/HTML/Canvas/CanvasPathDrawingStyles.h>
  24. #include <LibWeb/HTML/Canvas/CanvasRect.h>
  25. #include <LibWeb/HTML/Canvas/CanvasShadowStyles.h>
  26. #include <LibWeb/HTML/Canvas/CanvasState.h>
  27. #include <LibWeb/HTML/Canvas/CanvasText.h>
  28. #include <LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h>
  29. #include <LibWeb/HTML/Canvas/CanvasTransform.h>
  30. #include <LibWeb/HTML/CanvasGradient.h>
  31. #include <LibWeb/Layout/InlineNode.h>
  32. #include <LibWeb/Layout/LineBox.h>
  33. #include <LibWeb/WebIDL/ExceptionOr.h>
  34. namespace Web::HTML {
  35. class CanvasRenderingContext2D
  36. : public Bindings::PlatformObject
  37. , public CanvasPath
  38. , public CanvasState
  39. , public CanvasTransform<CanvasRenderingContext2D>
  40. , public CanvasFillStrokeStyles<CanvasRenderingContext2D>
  41. , public CanvasShadowStyles<CanvasRenderingContext2D>
  42. , public CanvasRect
  43. , public CanvasDrawPath
  44. , public CanvasText
  45. , public CanvasDrawImage
  46. , public CanvasImageData
  47. , public CanvasImageSmoothing
  48. , public CanvasCompositing
  49. , public CanvasPathDrawingStyles<CanvasRenderingContext2D>
  50. , public CanvasTextDrawingStyles<CanvasRenderingContext2D> {
  51. WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject);
  52. GC_DECLARE_ALLOCATOR(CanvasRenderingContext2D);
  53. public:
  54. [[nodiscard]] static GC::Ref<CanvasRenderingContext2D> create(JS::Realm&, HTMLCanvasElement&);
  55. virtual ~CanvasRenderingContext2D() override;
  56. virtual void fill_rect(float x, float y, float width, float height) override;
  57. virtual void stroke_rect(float x, float y, float width, float height) override;
  58. virtual void clear_rect(float x, float y, float width, float height) override;
  59. virtual WebIDL::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;
  60. virtual void begin_path() override;
  61. virtual void stroke() override;
  62. virtual void stroke(Path2D const& path) override;
  63. virtual void fill_text(StringView, float x, float y, Optional<double> max_width) override;
  64. virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) override;
  65. virtual void fill(StringView fill_rule) override;
  66. virtual void fill(Path2D& path, StringView fill_rule) override;
  67. virtual WebIDL::ExceptionOr<GC::Ref<ImageData>> create_image_data(int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
  68. virtual WebIDL::ExceptionOr<GC::Ref<ImageData>> create_image_data(ImageData const& image_data) const override;
  69. virtual WebIDL::ExceptionOr<GC::Ptr<ImageData>> get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
  70. virtual void put_image_data(ImageData const&, float x, float y) override;
  71. virtual void reset_to_default_state() override;
  72. GC::Ref<HTMLCanvasElement> canvas_for_binding() const;
  73. virtual GC::Ref<TextMetrics> measure_text(StringView text) override;
  74. virtual void clip(StringView fill_rule) override;
  75. virtual void clip(Path2D& path, StringView fill_rule) override;
  76. virtual bool is_point_in_path(double x, double y, StringView fill_rule) override;
  77. virtual bool is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule) override;
  78. virtual bool image_smoothing_enabled() const override;
  79. virtual void set_image_smoothing_enabled(bool) override;
  80. virtual Bindings::ImageSmoothingQuality image_smoothing_quality() const override;
  81. virtual void set_image_smoothing_quality(Bindings::ImageSmoothingQuality) override;
  82. virtual float global_alpha() const override;
  83. virtual void set_global_alpha(float) override;
  84. virtual float shadow_offset_x() const override;
  85. virtual void set_shadow_offset_x(float) override;
  86. virtual float shadow_offset_y() const override;
  87. virtual void set_shadow_offset_y(float) override;
  88. virtual String shadow_color() const override;
  89. virtual void set_shadow_color(String) override;
  90. HTMLCanvasElement& canvas_element();
  91. HTMLCanvasElement const& canvas_element() const;
  92. [[nodiscard]] Gfx::Painter* painter();
  93. private:
  94. explicit CanvasRenderingContext2D(JS::Realm&, HTMLCanvasElement&);
  95. virtual void initialize(JS::Realm&) override;
  96. virtual void visit_edges(Cell::Visitor&) override;
  97. virtual Gfx::Painter* painter_for_canvas_state() override { return painter(); }
  98. virtual Gfx::Path& path_for_canvas_state() override { return path(); }
  99. struct PreparedText {
  100. RefPtr<Gfx::GlyphRun> glyph_run;
  101. Gfx::TextAlignment physical_alignment;
  102. Gfx::IntRect bounding_box;
  103. };
  104. void did_draw(Gfx::FloatRect const&);
  105. RefPtr<Gfx::Font const> current_font();
  106. PreparedText prepare_text(ByteString const& text, float max_width = INFINITY);
  107. [[nodiscard]] Gfx::Path rect_path(float x, float y, float width, float height);
  108. [[nodiscard]] Gfx::Path text_path(StringView text, float x, float y, Optional<double> max_width);
  109. void stroke_internal(Gfx::Path const&);
  110. void fill_internal(Gfx::Path const&, Gfx::WindingRule);
  111. void clip_internal(Gfx::Path&, Gfx::WindingRule);
  112. void paint_shadow_for_fill_internal(Gfx::Path const&, Gfx::WindingRule);
  113. void paint_shadow_for_stroke_internal(Gfx::Path const&);
  114. GC::Ref<HTMLCanvasElement> m_element;
  115. OwnPtr<Gfx::Painter> m_painter;
  116. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
  117. bool m_origin_clean { true };
  118. };
  119. enum class CanvasImageSourceUsability {
  120. Bad,
  121. Good,
  122. };
  123. WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
  124. bool image_is_not_origin_clean(CanvasImageSource const&);
  125. }