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