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>
|
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>
|
2020-03-19 19:07:56 +01:00
|
|
|
#include <LibGfx/Color.h>
|
|
|
|
|
#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>
|
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>
|
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
|
|
|
|
|
|
|
|
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);
|
2023-11-19 19:47:52 +01:00
|
|
|
JS_DECLARE_ALLOCATOR(CanvasRenderingContext2D);
|
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
|
|
|
|
2024-05-15 23:53:29 +01:00
|
|
|
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create_image_data(int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
|
|
|
|
|
virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings = {}) 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;
|
|
|
|
|
|
2024-07-05 15:36:55 +02:00
|
|
|
[[nodiscard]] Gfx::Painter* painter();
|
|
|
|
|
|
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
|
|
|
|
2023-08-07 21:48:49 +02:00
|
|
|
RefPtr<Gfx::Font const> current_font();
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
PreparedText prepare_text(ByteString const& text, 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);
|
|
|
|
|
[[nodiscard]] Gfx::Path text_path(StringView text, float x, float y, Optional<double> max_width);
|
2023-05-19 22:42:47 +02:00
|
|
|
|
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);
|
2022-08-11 17:39:37 +01:00
|
|
|
|
2022-09-02 15:53:02 +02:00
|
|
|
JS::NonnullGCPtr<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 };
|
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
|
|
|
}
|