2020-04-06 11:09:01 +02:00
|
|
|
/*
|
2024-07-05 15:36:55 +02:00
|
|
|
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
|
2022-03-04 19:02:10 +01:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2025-04-28 16:01:47 +02:00
|
|
|
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.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>
|
2020-03-19 19:07:56 +01:00
|
|
|
#include <LibGfx/Forward.h>
|
2024-07-05 15:36:55 +02:00
|
|
|
#include <LibGfx/Painter.h>
|
2024-08-08 15:12:29 +02:00
|
|
|
#include <LibGfx/Path.h>
|
2025-02-18 09:19:56 +01:00
|
|
|
#include <LibGfx/TextLayout.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>
|
2024-12-18 12:26:37 +01:00
|
|
|
#include <LibWeb/HTML/Canvas/CanvasFilters.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>
|
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>
|
2025-05-13 20:51:41 +02:00
|
|
|
#include <LibWeb/HTML/Canvas/CanvasSettings.h>
|
2024-10-18 01:56:58 +03:00
|
|
|
#include <LibWeb/HTML/Canvas/CanvasShadowStyles.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-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
|
|
|
|
|
|
|
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>
|
2024-10-18 01:56:58 +03:00
|
|
|
, public CanvasShadowStyles<CanvasRenderingContext2D>
|
2024-12-18 12:26:37 +01:00
|
|
|
, public CanvasFilters
|
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
|
2025-05-13 20:51:41 +02:00
|
|
|
, public CanvasSettings
|
2023-08-03 12:18:17 +02:00
|
|
|
, public CanvasPathDrawingStyles<CanvasRenderingContext2D>
|
2025-03-07 00:19:45 +01:00
|
|
|
, public CanvasTextDrawingStyles<CanvasRenderingContext2D, HTMLCanvasElement> {
|
2020-03-19 19:07:56 +01:00
|
|
|
|
2022-09-02 15:53:02 +02:00
|
|
|
WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(CanvasRenderingContext2D);
|
2020-03-19 19:07:56 +01:00
|
|
|
|
|
|
|
public:
|
2025-04-28 16:01:47 +02:00
|
|
|
static JS::ThrowCompletionOr<GC::Ref<CanvasRenderingContext2D>> create(JS::Realm&, HTMLCanvasElement&, JS::Value options);
|
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
|
|
|
|
2025-09-19 12:21:20 +02:00
|
|
|
virtual void fill_text(Utf16String const&, float x, float y, Optional<double> max_width) override;
|
|
|
|
virtual void stroke_text(Utf16String const&, 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
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual WebIDL::ExceptionOr<GC::Ref<ImageData>> create_image_data(int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
|
|
|
|
virtual WebIDL::ExceptionOr<GC::Ref<ImageData>> create_image_data(ImageData const& image_data) const override;
|
|
|
|
virtual WebIDL::ExceptionOr<GC::Ptr<ImageData>> get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
|
2025-04-15 15:51:27 -06:00
|
|
|
virtual void put_image_data(ImageData&, 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
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<HTMLCanvasElement> canvas_for_binding() const;
|
2020-05-21 00:09:31 +01:00
|
|
|
|
2025-05-13 20:51:41 +02:00
|
|
|
virtual CanvasRenderingContext2DSettings get_context_attributes() const override { return m_context_attributes; }
|
2025-04-28 16:01:47 +02:00
|
|
|
|
2025-09-19 12:21:20 +02:00
|
|
|
virtual GC::Ref<TextMetrics> measure_text(Utf16String const&) 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
|
|
|
|
2024-09-18 10:52:33 +02:00
|
|
|
virtual bool is_point_in_path(double x, double y, StringView fill_rule) override;
|
|
|
|
virtual bool is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule) override;
|
|
|
|
|
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;
|
|
|
|
|
2025-01-28 18:19:30 +01:00
|
|
|
virtual String global_composite_operation() const override;
|
|
|
|
virtual void set_global_composite_operation(String) override;
|
|
|
|
|
2024-12-18 12:26:37 +01:00
|
|
|
virtual String filter() const override;
|
|
|
|
virtual void set_filter(String) override;
|
|
|
|
|
2024-10-18 01:56:58 +03:00
|
|
|
virtual float shadow_offset_x() const override;
|
|
|
|
virtual void set_shadow_offset_x(float) override;
|
|
|
|
virtual float shadow_offset_y() const override;
|
|
|
|
virtual void set_shadow_offset_y(float) override;
|
2024-12-03 21:22:44 +01:00
|
|
|
virtual float shadow_blur() const override;
|
|
|
|
virtual void set_shadow_blur(float) override;
|
2024-10-18 01:56:58 +03:00
|
|
|
virtual String shadow_color() const override;
|
|
|
|
virtual void set_shadow_color(String) override;
|
|
|
|
|
2023-08-07 21:48:49 +02:00
|
|
|
HTMLCanvasElement& canvas_element();
|
|
|
|
HTMLCanvasElement const& canvas_element() const;
|
|
|
|
|
2024-07-05 15:36:55 +02:00
|
|
|
[[nodiscard]] Gfx::Painter* painter();
|
|
|
|
|
2024-11-29 20:17:25 +01:00
|
|
|
void set_size(Gfx::IntSize const&);
|
|
|
|
|
|
|
|
RefPtr<Gfx::PaintingSurface> surface() { return m_surface; }
|
|
|
|
void allocate_painting_surface_if_needed();
|
|
|
|
|
2020-03-19 19:07:56 +01:00
|
|
|
private:
|
2025-04-28 16:01:47 +02:00
|
|
|
CanvasRenderingContext2D(JS::Realm&, HTMLCanvasElement&, CanvasRenderingContext2DSettings);
|
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
|
|
|
|
2024-08-15 14:52:40 +02:00
|
|
|
virtual Gfx::Painter* painter_for_canvas_state() override { return painter(); }
|
|
|
|
virtual Gfx::Path& path_for_canvas_state() override { return path(); }
|
|
|
|
|
2021-12-30 22:15:38 +00:00
|
|
|
struct PreparedText {
|
2025-04-20 01:09:10 +02:00
|
|
|
Vector<NonnullRefPtr<Gfx::GlyphRun>> glyph_runs;
|
2021-12-30 22:15:38 +00:00
|
|
|
Gfx::TextAlignment physical_alignment;
|
2025-04-20 01:09:10 +02:00
|
|
|
Gfx::FloatRect bounding_box;
|
2021-12-30 22:15:38 +00:00
|
|
|
};
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void did_draw(Gfx::FloatRect const&);
|
2023-04-06 20:57:59 +01:00
|
|
|
|
2025-04-19 18:34:53 +02:00
|
|
|
RefPtr<Gfx::FontCascadeList const> font_cascade_list();
|
2023-08-07 21:48:49 +02:00
|
|
|
|
2025-09-19 12:21:20 +02:00
|
|
|
PreparedText prepare_text(Utf16String const&, float max_width = INFINITY);
|
2020-03-22 21:15:49 +01:00
|
|
|
|
2024-08-08 15:12:29 +02:00
|
|
|
[[nodiscard]] Gfx::Path rect_path(float x, float y, float width, float height);
|
2025-09-19 12:21:20 +02:00
|
|
|
[[nodiscard]] Gfx::Path text_path(Utf16String const&, float x, float y, Optional<double> max_width);
|
2023-05-19 22:42:47 +02:00
|
|
|
|
2025-04-28 16:12:30 +02:00
|
|
|
Gfx::Color clear_color() const;
|
|
|
|
|
2024-08-08 15:12:29 +02:00
|
|
|
void stroke_internal(Gfx::Path const&);
|
|
|
|
void fill_internal(Gfx::Path const&, Gfx::WindingRule);
|
|
|
|
void clip_internal(Gfx::Path&, Gfx::WindingRule);
|
2024-10-18 01:56:58 +03:00
|
|
|
void paint_shadow_for_fill_internal(Gfx::Path const&, Gfx::WindingRule);
|
|
|
|
void paint_shadow_for_stroke_internal(Gfx::Path const&);
|
2022-08-11 17:39:37 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<HTMLCanvasElement> m_element;
|
2024-07-05 15:36:55 +02: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 };
|
2024-11-29 20:17:25 +01:00
|
|
|
|
|
|
|
Gfx::IntSize m_size;
|
|
|
|
RefPtr<Gfx::PaintingSurface> m_surface;
|
2025-04-28 16:01:47 +02:00
|
|
|
CanvasRenderingContext2DSettings m_context_attributes;
|
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
|
|
|
}
|