CanvasRenderingContext2D.h 5.9 KB

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