CanvasRenderingContext2D.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/String.h>
  9. #include <AK/Variant.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/AntiAliasingPainter.h>
  12. #include <LibGfx/Color.h>
  13. #include <LibGfx/Forward.h>
  14. #include <LibGfx/Painter.h>
  15. #include <LibGfx/Path.h>
  16. #include <LibWeb/Bindings/PlatformObject.h>
  17. #include <LibWeb/HTML/Canvas/CanvasCompositing.h>
  18. #include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
  19. #include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
  20. #include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
  21. #include <LibWeb/HTML/Canvas/CanvasImageData.h>
  22. #include <LibWeb/HTML/Canvas/CanvasImageSmoothing.h>
  23. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  24. #include <LibWeb/HTML/Canvas/CanvasPathClipper.h>
  25. #include <LibWeb/HTML/Canvas/CanvasPathDrawingStyles.h>
  26. #include <LibWeb/HTML/Canvas/CanvasRect.h>
  27. #include <LibWeb/HTML/Canvas/CanvasState.h>
  28. #include <LibWeb/HTML/Canvas/CanvasText.h>
  29. #include <LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h>
  30. #include <LibWeb/HTML/Canvas/CanvasTransform.h>
  31. #include <LibWeb/HTML/CanvasGradient.h>
  32. #include <LibWeb/Layout/InlineNode.h>
  33. #include <LibWeb/Layout/LineBox.h>
  34. #include <LibWeb/WebIDL/ExceptionOr.h>
  35. namespace Web::HTML {
  36. // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
  37. // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
  38. using CanvasImageSource = Variant<JS::Handle<HTMLImageElement>, JS::Handle<HTMLCanvasElement>>;
  39. class CanvasRenderingContext2D
  40. : public Bindings::PlatformObject
  41. , public CanvasPath
  42. , public CanvasState
  43. , public CanvasTransform<CanvasRenderingContext2D>
  44. , public CanvasFillStrokeStyles<CanvasRenderingContext2D>
  45. , public CanvasRect
  46. , public CanvasDrawPath
  47. , public CanvasText
  48. , public CanvasDrawImage
  49. , public CanvasImageData
  50. , public CanvasImageSmoothing
  51. , public CanvasCompositing
  52. , public CanvasPathDrawingStyles<CanvasRenderingContext2D>
  53. , public CanvasTextDrawingStyles<CanvasRenderingContext2D> {
  54. WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject);
  55. public:
  56. [[nodiscard]] static JS::NonnullGCPtr<CanvasRenderingContext2D> create(JS::Realm&, HTMLCanvasElement&);
  57. virtual ~CanvasRenderingContext2D() override;
  58. virtual void fill_rect(float x, float y, float width, float height) override;
  59. virtual void stroke_rect(float x, float y, float width, float height) override;
  60. virtual void clear_rect(float x, float y, float width, float height) override;
  61. 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;
  62. virtual void begin_path() override;
  63. virtual void stroke() override;
  64. virtual void stroke(Path2D const& path) override;
  65. virtual void fill_text(StringView, float x, float y, Optional<double> max_width) override;
  66. virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) override;
  67. virtual void fill(StringView fill_rule) override;
  68. virtual void fill(Path2D& path, StringView fill_rule) override;
  69. virtual JS::GCPtr<ImageData> create_image_data(int width, int height) const override;
  70. virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height) const override;
  71. virtual void put_image_data(ImageData const&, float x, float y) override;
  72. virtual void reset_to_default_state() override;
  73. JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const;
  74. virtual JS::NonnullGCPtr<TextMetrics> measure_text(StringView text) override;
  75. virtual void clip(StringView fill_rule) override;
  76. virtual void clip(Path2D& path, StringView fill_rule) override;
  77. virtual bool image_smoothing_enabled() const override;
  78. virtual void set_image_smoothing_enabled(bool) override;
  79. virtual Bindings::ImageSmoothingQuality image_smoothing_quality() const override;
  80. virtual void set_image_smoothing_quality(Bindings::ImageSmoothingQuality) override;
  81. virtual float global_alpha() const override;
  82. virtual void set_global_alpha(float) override;
  83. HTMLCanvasElement& canvas_element();
  84. HTMLCanvasElement const& canvas_element() const;
  85. private:
  86. explicit CanvasRenderingContext2D(JS::Realm&, HTMLCanvasElement&);
  87. virtual void initialize(JS::Realm&) override;
  88. virtual void visit_edges(Cell::Visitor&) override;
  89. struct PreparedTextGlyph {
  90. String glyph;
  91. Gfx::IntPoint position;
  92. };
  93. struct PreparedText {
  94. Vector<PreparedTextGlyph> glyphs;
  95. Gfx::TextAlignment physical_alignment;
  96. Gfx::IntRect bounding_box;
  97. };
  98. void did_draw(Gfx::FloatRect const&);
  99. template<typename TDrawFunction>
  100. void draw_clipped(TDrawFunction draw_function)
  101. {
  102. auto painter = this->antialiased_painter();
  103. if (!painter.has_value())
  104. return;
  105. ScopedCanvasPathClip clipper(painter->underlying_painter(), drawing_state().clip);
  106. auto draw_rect = draw_function(*painter);
  107. if (drawing_state().clip.has_value())
  108. draw_rect.intersect(drawing_state().clip->path.bounding_box());
  109. did_draw(draw_rect);
  110. }
  111. RefPtr<Gfx::Font const> current_font();
  112. PreparedText prepare_text(DeprecatedString const& text, float max_width = INFINITY);
  113. Gfx::Painter* painter();
  114. Optional<Gfx::AntiAliasingPainter> antialiased_painter();
  115. Gfx::Path rect_path(float x, float y, float width, float height);
  116. void stroke_internal(Gfx::Path const&);
  117. void fill_internal(Gfx::Path const&, Gfx::Painter::WindingRule);
  118. void clip_internal(Gfx::Path&, Gfx::Painter::WindingRule);
  119. JS::NonnullGCPtr<HTMLCanvasElement> m_element;
  120. OwnPtr<Gfx::Painter> m_painter;
  121. // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
  122. bool m_origin_clean { true };
  123. };
  124. enum class CanvasImageSourceUsability {
  125. Bad,
  126. Good,
  127. };
  128. WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
  129. bool image_is_not_origin_clean(CanvasImageSource const&);
  130. }