| 
									
										
										
										
											2020-04-06 11:09:01 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-02-03 20:01:19 +01:00
										 |  |  |  * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  |  * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-04-06 11:09:01 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-06 11:09:01 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 11:48:05 -05:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-12 19:22:42 +02:00
										 |  |  | #include <LibGfx/AffineTransform.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-30 00:10:02 +00:00
										 |  |  | #include <LibGfx/AntiAliasingPainter.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | #include <LibGfx/Color.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-06 11:56:47 +04:30
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-16 21:06:03 +02:00
										 |  |  | #include <LibGfx/Path.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  | #include <LibWeb/Bindings/PlatformObject.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-19 22:42:47 +02:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasCompositing.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 17:28:54 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:52:17 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 15:46:31 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 17:37:09 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasImageData.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-29 18:35:02 +02:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasImageSmoothing.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:10:04 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasPath.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-06 20:57:59 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasPathClipper.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 17:43:37 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasPathDrawingStyles.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:06:32 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasRect.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 14:00:00 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasState.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 19:10:01 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasText.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-03 12:18:17 +02:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-12 14:48:11 +01:00
										 |  |  | #include <LibWeb/HTML/Canvas/CanvasTransform.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-03 20:08:27 +01:00
										 |  |  | #include <LibWeb/HTML/CanvasGradient.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-30 22:15:38 +00:00
										 |  |  | #include <LibWeb/Layout/InlineNode.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/LineBox.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  | // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagesource
 | 
					
						
							|  |  |  | // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | using CanvasImageSource = Variant<JS::Handle<HTMLImageElement>, JS::Handle<HTMLCanvasElement>>; | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | class CanvasRenderingContext2D | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  |     : public Bindings::PlatformObject | 
					
						
							| 
									
										
										
										
											2022-08-12 14:00:00 +01:00
										 |  |  |     , public CanvasPath | 
					
						
							| 
									
										
										
										
											2022-08-12 14:48:11 +01:00
										 |  |  |     , public CanvasState | 
					
						
							| 
									
										
										
										
											2022-08-12 15:46:31 +01:00
										 |  |  |     , public CanvasTransform<CanvasRenderingContext2D> | 
					
						
							| 
									
										
										
										
											2022-08-12 16:06:32 +01:00
										 |  |  |     , public CanvasFillStrokeStyles<CanvasRenderingContext2D> | 
					
						
							| 
									
										
										
										
											2022-08-12 16:52:17 +01:00
										 |  |  |     , public CanvasRect | 
					
						
							| 
									
										
										
										
											2022-08-12 19:10:01 +01:00
										 |  |  |     , public CanvasDrawPath | 
					
						
							| 
									
										
										
										
											2022-08-12 17:28:54 +01:00
										 |  |  |     , public CanvasText | 
					
						
							| 
									
										
										
										
											2022-08-12 17:37:09 +01:00
										 |  |  |     , public CanvasDrawImage | 
					
						
							| 
									
										
										
										
											2022-08-12 17:43:37 +01:00
										 |  |  |     , public CanvasImageData | 
					
						
							| 
									
										
										
										
											2023-03-29 18:35:02 +02:00
										 |  |  |     , public CanvasImageSmoothing | 
					
						
							| 
									
										
										
										
											2023-05-19 22:42:47 +02:00
										 |  |  |     , public CanvasCompositing | 
					
						
							| 
									
										
										
										
											2023-08-03 12:18:17 +02:00
										 |  |  |     , public CanvasPathDrawingStyles<CanvasRenderingContext2D> | 
					
						
							|  |  |  |     , public CanvasTextDrawingStyles<CanvasRenderingContext2D> { | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  |     WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject); | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     [[nodiscard]] static JS::NonnullGCPtr<CanvasRenderingContext2D> create(JS::Realm&, HTMLCanvasElement&); | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  |     virtual ~CanvasRenderingContext2D() override; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:06:32 +01:00
										 |  |  |     virtual void fill_rect(float x, float y, float width, float height) override; | 
					
						
							|  |  |  |     virtual void stroke_rect(float x, float y, float width, float height) override; | 
					
						
							|  |  |  |     virtual void clear_rect(float x, float y, float width, float height) override; | 
					
						
							| 
									
										
										
										
											2020-04-14 20:38:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     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; | 
					
						
							| 
									
										
										
										
											2020-04-14 20:38:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:52:17 +01:00
										 |  |  |     virtual void begin_path() override; | 
					
						
							|  |  |  |     virtual void stroke() override; | 
					
						
							|  |  |  |     virtual void stroke(Path2D const& path) override; | 
					
						
							| 
									
										
										
										
											2020-06-22 18:39:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 19:41:48 +12:00
										 |  |  |     virtual void fill_text(StringView, float x, float y, Optional<double> max_width) override; | 
					
						
							|  |  |  |     virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) override; | 
					
						
							| 
									
										
										
										
											2021-04-15 20:36:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 19:41:48 +12:00
										 |  |  |     virtual void fill(StringView fill_rule) override; | 
					
						
							|  |  |  |     virtual void fill(Path2D& path, StringView fill_rule) override; | 
					
						
							| 
									
										
										
										
											2020-04-16 21:06:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 23:07:05 +02:00
										 |  |  |     virtual JS::GCPtr<ImageData> create_image_data(int width, int height) const override; | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height) const override; | 
					
						
							| 
									
										
										
										
											2022-08-12 17:37:09 +01:00
										 |  |  |     virtual void put_image_data(ImageData const&, float x, float y) override; | 
					
						
							| 
									
										
										
										
											2020-04-21 23:49:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 14:00:00 +01:00
										 |  |  |     virtual void reset_to_default_state() override; | 
					
						
							| 
									
										
										
										
											2021-12-27 14:31:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const; | 
					
						
							| 
									
										
										
										
											2020-05-21 00:09:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 19:41:48 +12:00
										 |  |  |     virtual JS::NonnullGCPtr<TextMetrics> measure_text(StringView text) override; | 
					
						
							| 
									
										
										
										
											2021-12-30 22:15:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 19:41:48 +12:00
										 |  |  |     virtual void clip(StringView fill_rule) override; | 
					
						
							|  |  |  |     virtual void clip(Path2D& path, StringView fill_rule) override; | 
					
						
							| 
									
										
										
										
											2022-04-10 18:46:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 18:35:02 +02:00
										 |  |  |     virtual bool image_smoothing_enabled() const override; | 
					
						
							|  |  |  |     virtual void set_image_smoothing_enabled(bool) override; | 
					
						
							|  |  |  |     virtual Bindings::ImageSmoothingQuality image_smoothing_quality() const override; | 
					
						
							|  |  |  |     virtual void set_image_smoothing_quality(Bindings::ImageSmoothingQuality) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 22:42:47 +02:00
										 |  |  |     virtual float global_alpha() const override; | 
					
						
							|  |  |  |     virtual void set_global_alpha(float) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 21:48:49 +02:00
										 |  |  |     HTMLCanvasElement& canvas_element(); | 
					
						
							|  |  |  |     HTMLCanvasElement const& canvas_element() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     explicit CanvasRenderingContext2D(JS::Realm&, HTMLCanvasElement&); | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-30 22:15:38 +00:00
										 |  |  |     struct PreparedTextGlyph { | 
					
						
							| 
									
										
										
										
											2023-02-22 11:48:05 -05:00
										 |  |  |         String glyph; | 
					
						
							| 
									
										
										
										
											2021-12-30 22:15:38 +00:00
										 |  |  |         Gfx::IntPoint position; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct PreparedText { | 
					
						
							|  |  |  |         Vector<PreparedTextGlyph> glyphs; | 
					
						
							|  |  |  |         Gfx::TextAlignment physical_alignment; | 
					
						
							|  |  |  |         Gfx::IntRect bounding_box; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void did_draw(Gfx::FloatRect const&); | 
					
						
							| 
									
										
										
										
											2023-04-06 20:57:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     template<typename TDrawFunction> | 
					
						
							|  |  |  |     void draw_clipped(TDrawFunction draw_function) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto painter = this->antialiased_painter(); | 
					
						
							|  |  |  |         if (!painter.has_value()) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         ScopedCanvasPathClip clipper(painter->underlying_painter(), drawing_state().clip); | 
					
						
							|  |  |  |         auto draw_rect = draw_function(*painter); | 
					
						
							|  |  |  |         if (drawing_state().clip.has_value()) | 
					
						
							|  |  |  |             draw_rect.intersect(drawing_state().clip->path.bounding_box()); | 
					
						
							|  |  |  |         did_draw(draw_rect); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 21:48:49 +02:00
										 |  |  |     RefPtr<Gfx::Font const> current_font(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     PreparedText prepare_text(DeprecatedString const& text, float max_width = INFINITY); | 
					
						
							| 
									
										
										
										
											2020-03-22 21:15:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-30 00:10:02 +00:00
										 |  |  |     Gfx::Painter* painter(); | 
					
						
							|  |  |  |     Optional<Gfx::AntiAliasingPainter> antialiased_painter(); | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 22:42:47 +02:00
										 |  |  |     Gfx::Path rect_path(float x, float y, float width, float height); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 17:39:37 +01:00
										 |  |  |     void stroke_internal(Gfx::Path const&); | 
					
						
							| 
									
										
										
										
											2023-05-19 22:42:47 +02:00
										 |  |  |     void fill_internal(Gfx::Path const&, Gfx::Painter::WindingRule); | 
					
						
							|  |  |  |     void clip_internal(Gfx::Path&, Gfx::Painter::WindingRule); | 
					
						
							| 
									
										
										
										
											2022-08-11 17:39:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 15:53:02 +02:00
										 |  |  |     JS::NonnullGCPtr<HTMLCanvasElement> m_element; | 
					
						
							| 
									
										
										
										
											2022-11-30 00:10:02 +00:00
										 |  |  |     OwnPtr<Gfx::Painter> m_painter; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 18:57:29 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
 | 
					
						
							|  |  |  |     bool m_origin_clean { true }; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  | enum class CanvasImageSourceUsability { | 
					
						
							|  |  |  |     Bad, | 
					
						
							|  |  |  |     Good, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&); | 
					
						
							| 
									
										
										
										
											2022-03-04 19:02:10 +01:00
										 |  |  | bool image_is_not_origin_clean(CanvasImageSource const&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } |