Bläddra i källkod

LibWeb: Implemented the CanvasRenderingContext2D::createImageData() spec

The only real change here is to use the absolute magnitude of the
width/height when creating the ImageData.

This fixes the crash on this WPT test:
https://wpt.fyi/results/html/canvas/element/pixel-manipulation/2d.imageData.create2.double.html?label=master&product=ladybird
Cory Virok 8 månader sedan
förälder
incheckning
560c3824b9

+ 1 - 0
Tests/LibWeb/Text/expected/canvas/convert-dims.txt

@@ -0,0 +1 @@
+PASS

+ 15 - 0
Tests/LibWeb/Text/input/canvas/convert-dims.html

@@ -0,0 +1,15 @@
+<canvas id="c" class="output" width="11" height="11"><p class="fallback">FAIL (fallback content)</p></canvas>
+<script src="../include.js"></script>
+<script>
+    // Adapted from https://wpt.fyi/results/html/canvas/element/pixel-manipulation/2d.imageData.create2.double.html?label=master&product=ladybird
+    test(() => {
+        const canvas = document.getElementById('c')
+        const ctx = canvas.getContext("2d");
+        const imgdata = ctx.createImageData(-10.01, -10.99)
+        if (imgdata.width === 10 && imgdata.height === 10) {
+            println('PASS')
+        } else {
+            println('FAIL')
+        }
+    });
+</script>

+ 17 - 1
Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp

@@ -321,9 +321,25 @@ void CanvasRenderingContext2D::fill(Path2D& path, StringView fill_rule)
     fill_internal(path.path(), parse_fill_rule(fill_rule));
     fill_internal(path.path(), parse_fill_rule(fill_rule));
 }
 }
 
 
+// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata
 WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> CanvasRenderingContext2D::create_image_data(int width, int height, Optional<ImageDataSettings> const& settings) const
 WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> CanvasRenderingContext2D::create_image_data(int width, int height, Optional<ImageDataSettings> const& settings) const
 {
 {
-    return ImageData::create(realm(), width, height, settings);
+    // 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
+    if (width == 0 || height == 0)
+        return WebIDL::IndexSizeError::create(realm(), "Width and height must not be zero"_string);
+
+    int abs_width = abs(width);
+    int abs_height = abs(height);
+
+    // 2. Let newImageData be a new ImageData object.
+    // 3. Initialize newImageData given the absolute magnitude of sw, the absolute magnitude of sh, settings set to settings, and defaultColorSpace set to this's color space.
+    auto image_data = TRY(ImageData::create(realm(), abs_width, abs_height, settings));
+
+    // 4. Initialize the image data of newImageData to transparent black.
+    // ... this is handled by ImageData::create()
+
+    // 5. Return newImageData.
+    return image_data;
 }
 }
 
 
 // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getimagedata
 // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getimagedata