CanvasRenderingContext2D.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Variant.h>
  9. #include <LibGfx/AffineTransform.h>
  10. #include <LibGfx/Color.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/Painter.h>
  13. #include <LibGfx/Path.h>
  14. #include <LibWeb/Bindings/PlatformObject.h>
  15. #include <LibWeb/DOM/ExceptionOr.h>
  16. #include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
  17. #include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
  18. #include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
  19. #include <LibWeb/HTML/Canvas/CanvasImageData.h>
  20. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  21. #include <LibWeb/HTML/Canvas/CanvasPathDrawingStyles.h>
  22. #include <LibWeb/HTML/Canvas/CanvasRect.h>
  23. #include <LibWeb/HTML/Canvas/CanvasState.h>
  24. #include <LibWeb/HTML/Canvas/CanvasText.h>
  25. #include <LibWeb/HTML/Canvas/CanvasTransform.h>
  26. #include <LibWeb/HTML/CanvasGradient.h>
  27. #include <LibWeb/Layout/InlineNode.h>
  28. #include <LibWeb/Layout/LineBox.h>
  29. namespace Web::HTML {
  30. // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
  31. // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
  32. using CanvasImageSource = Variant<JS::Handle<HTMLImageElement>, JS::Handle<HTMLCanvasElement>>;
  33. class CanvasRenderingContext2D
  34. : public Bindings::PlatformObject
  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. , public CanvasImageData
  44. , public CanvasPathDrawingStyles<CanvasRenderingContext2D> {
  45. WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject);
  46. public:
  47. static JS::NonnullGCPtr<CanvasRenderingContext2D> create(HTML::Window&, HTMLCanvasElement&);
  48. virtual ~CanvasRenderingContext2D() override;
  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. virtual void begin_path() override;
  54. virtual void stroke() override;
  55. virtual void stroke(Path2D const& path) override;
  56. virtual void fill_text(String const&, float x, float y, Optional<double> max_width) override;
  57. virtual void stroke_text(String const&, float x, float y, Optional<double> max_width) override;
  58. virtual void fill(String const& fill_rule) override;
  59. virtual void fill(Path2D& path, String const& fill_rule) override;
  60. virtual JS::GCPtr<ImageData> create_image_data(int width, int height) const override;
  61. virtual DOM::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height) const override;
  62. virtual void put_image_data(ImageData const&, float x, float y) override;
  63. virtual void reset_to_default_state() override;
  64. JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const;
  65. virtual JS::NonnullGCPtr<TextMetrics> measure_text(String const& text) override;
  66. virtual void clip() override;
  67. private:
  68. explicit CanvasRenderingContext2D(HTML::Window&, HTMLCanvasElement&);
  69. virtual void visit_edges(Cell::Visitor&) override;
  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(Gfx::FloatRect const&);
  80. PreparedText prepare_text(String const& text, float max_width = INFINITY);
  81. OwnPtr<Gfx::Painter> painter();
  82. HTMLCanvasElement& canvas_element();
  83. HTMLCanvasElement const& canvas_element() const;
  84. void stroke_internal(Gfx::Path const&);
  85. void fill_internal(Gfx::Path&, String const& fill_rule);
  86. JS::NonnullGCPtr<HTMLCanvasElement> m_element;
  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. }
  97. WRAPPER_HACK(CanvasRenderingContext2D, Web::HTML)