summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp52
-rw-r--r--Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h35
-rw-r--r--Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.idl10
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp39
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h8
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl7
7 files changed, 105 insertions, 47 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt
index f04f658b76..2856fef674 100644
--- a/Userland/Libraries/LibWeb/CMakeLists.txt
+++ b/Userland/Libraries/LibWeb/CMakeLists.txt
@@ -135,6 +135,7 @@ set(SOURCES
HTML/AttributeNames.cpp
HTML/BrowsingContext.cpp
HTML/BrowsingContextContainer.cpp
+ HTML/Canvas/CanvasDrawImage.cpp
HTML/Canvas/CanvasPath.cpp
HTML/Canvas/CanvasState.cpp
HTML/CanvasGradient.cpp
diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
new file mode 100644
index 0000000000..7e4620aa22
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
+
+namespace Web::HTML {
+
+static void default_source_size(CanvasImageSource const& image, float& source_width, float& source_height)
+{
+ image.visit([&source_width, &source_height](auto const& source) {
+ if (source->bitmap()) {
+ source_width = source->bitmap()->width();
+ source_height = source->bitmap()->height();
+ } else {
+ source_width = source->width();
+ source_height = source->height();
+ }
+ });
+}
+
+DOM::ExceptionOr<void> CanvasDrawImage::draw_image(Web::HTML::CanvasImageSource const& image, float destination_x, float destination_y)
+{
+ // If not specified, the dw and dh arguments must default to the values of sw and sh, interpreted such that one CSS pixel in the image is treated as one unit in the output bitmap's coordinate space.
+ // If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
+ // If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
+ // neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
+ float source_width;
+ float source_height;
+ default_source_size(image, source_width, source_height);
+ return draw_image_internal(image, 0, 0, source_width, source_height, destination_x, destination_y, source_width, source_height);
+}
+
+DOM::ExceptionOr<void> CanvasDrawImage::draw_image(Web::HTML::CanvasImageSource const& image, float destination_x, float destination_y, float destination_width, float destination_height)
+{
+ // If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
+ // If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
+ // neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
+ float source_width;
+ float source_height;
+ default_source_size(image, source_width, source_height);
+ return draw_image_internal(image, 0, 0, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
+}
+
+DOM::ExceptionOr<void> CanvasDrawImage::draw_image(Web::HTML::CanvasImageSource const& image, float source_x, float source_y, float source_width, float source_height, float destination_x, float destination_y, float destination_width, float destination_height)
+{
+ return draw_image_internal(image, source_x, source_y, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h
new file mode 100644
index 0000000000..056620565c
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <LibWeb/DOM/ExceptionOr.h>
+#include <LibWeb/HTML/HTMLCanvasElement.h>
+#include <LibWeb/HTML/HTMLImageElement.h>
+
+namespace Web::HTML {
+
+// 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.
+using CanvasImageSource = Variant<NonnullRefPtr<HTMLImageElement>, NonnullRefPtr<HTMLCanvasElement>>;
+
+// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawimage
+class CanvasDrawImage {
+public:
+ virtual ~CanvasDrawImage() = default;
+
+ DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y);
+ DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y, float destination_width, float destination_height);
+ DOM::ExceptionOr<void> draw_image(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);
+
+ 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) = 0;
+
+protected:
+ CanvasDrawImage() = default;
+};
+
+}
diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.idl b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.idl
new file mode 100644
index 0000000000..35dd06c2f4
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.idl
@@ -0,0 +1,10 @@
+#import <HTML/HTMLCanvasElement.idl>
+#import <HTML/HTMLImageElement.idl>
+
+// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawimage
+interface mixin CanvasDrawImage {
+ // FIXME: These `image` params should be CanvasImageSource
+ undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, unrestricted double dx, unrestricted double dy);
+ undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, unrestricted double dx, unrestricted double dy, unrestricted double dw, unrestricted double dh);
+ undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, unrestricted double sx, unrestricted double sy, unrestricted double sw, unrestricted double sh, unrestricted double dx, unrestricted double dy, unrestricted double dw, unrestricted double dh);
+};
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
index 82c7a12635..2c57335200 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
@@ -93,45 +93,8 @@ void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float
did_draw(rect);
}
-static void default_source_size(CanvasImageSource const& image, float& source_width, float& source_height)
-{
- image.visit([&source_width, &source_height](auto const& source) {
- if (source->bitmap()) {
- source_width = source->bitmap()->width();
- source_height = source->bitmap()->height();
- } else {
- source_width = source->width();
- source_height = source->height();
- }
- });
-}
-
-// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-drawimage
-DOM::ExceptionOr<void> CanvasRenderingContext2D::draw_image(CanvasImageSource const& image, float destination_x, float destination_y)
-{
- // If not specified, the dw and dh arguments must default to the values of sw and sh, interpreted such that one CSS pixel in the image is treated as one unit in the output bitmap's coordinate space.
- // If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
- // If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
- // neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
- float source_width;
- float source_height;
- default_source_size(image, source_width, source_height);
- return draw_image(image, 0, 0, source_width, source_height, destination_x, destination_y, source_width, source_height);
-}
-
-DOM::ExceptionOr<void> CanvasRenderingContext2D::draw_image(CanvasImageSource const& image, float destination_x, float destination_y, float destination_width, float destination_height)
-{
- // If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
- // If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
- // neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
- float source_width;
- float source_height;
- default_source_size(image, source_width, source_height);
- return draw_image(image, 0, 0, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
-}
-
// 4.12.5.1.14 Drawing images, https://html.spec.whatwg.org/multipage/canvas.html#drawing-images
-DOM::ExceptionOr<void> CanvasRenderingContext2D::draw_image(CanvasImageSource const& image, float source_x, float source_y, float source_width, float source_height, float destination_x, float destination_y, float destination_width, float destination_height)
+DOM::ExceptionOr<void> CanvasRenderingContext2D::draw_image_internal(CanvasImageSource const& image, float source_x, float source_y, float source_width, float source_height, float destination_x, float destination_y, float destination_width, float destination_height)
{
// 1. If any of the arguments are infinite or NaN, then return.
if (!isfinite(source_x) || !isfinite(source_y) || !isfinite(source_width) || !isfinite(source_height) || !isfinite(destination_x) || !isfinite(destination_y) || !isfinite(destination_width) || !isfinite(destination_height))
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
index 6f94d0c55b..89c518a6cf 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
@@ -16,6 +16,7 @@
#include <LibGfx/Path.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h>
+#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
#include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
#include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
@@ -42,7 +43,8 @@ class CanvasRenderingContext2D
, public CanvasFillStrokeStyles<CanvasRenderingContext2D>
, public CanvasRect
, public CanvasDrawPath
- , public CanvasText {
+ , public CanvasText
+ , public CanvasDrawImage {
AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
@@ -57,9 +59,7 @@ public:
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;
- DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y);
- DOM::ExceptionOr<void> draw_image(CanvasImageSource const&, float destination_x, float destination_y, float destination_width, float destination_height);
- DOM::ExceptionOr<void> draw_image(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);
+ 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;
void set_line_width(float line_width) { drawing_state().line_width = line_width; }
float line_width() const { return drawing_state().line_width; }
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
index 346d85dd75..35559c34b8 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
@@ -1,6 +1,6 @@
#import <HTML/HTMLCanvasElement.idl>
-#import <HTML/HTMLImageElement.idl>
#import <HTML/ImageData.idl>
+#import <HTML/Canvas/CanvasDrawImage.idl>
#import <HTML/Canvas/CanvasDrawPath.idl>
#import <HTML/Canvas/CanvasFillStrokeStyles.idl>
#import <HTML/Canvas/CanvasPath.idl>
@@ -13,10 +13,6 @@
[Exposed=Window]
interface CanvasRenderingContext2D {
- undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double dx, double dy);
- undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double dx, double dy, double dw, double dh);
- undefined drawImage((HTMLImageElement or HTMLCanvasElement) image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh);
-
attribute double lineWidth;
ImageData createImageData(long sw, long sh);
@@ -33,4 +29,5 @@ CanvasRenderingContext2D includes CanvasFillStrokeStyles;
CanvasRenderingContext2D includes CanvasRect;
CanvasRenderingContext2D includes CanvasDrawPath;
CanvasRenderingContext2D includes CanvasText;
+CanvasRenderingContext2D includes CanvasDrawImage;
CanvasRenderingContext2D includes CanvasPath;