mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
LibWeb: Implement support for drawing with CanvasPattern
We already had the API, but drawing to the canvas was not affected by any created CanvasPattern. This moves CanvasPatternPaintStyle to LibGfx so we don't have to reach into LibWeb, and implements the plumbing to let Skia use images as a fill pattern.
This commit is contained in:
parent
9753b8e62c
commit
62ae4e878f
Notes:
github-actions[bot]
2025-10-23 12:21:23 +00:00
Author: https://github.com/gmta
Commit: 62ae4e878f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6548
Reviewed-by: https://github.com/tcl3 ✅
20 changed files with 237 additions and 167 deletions
|
|
@ -1,102 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
|
||||
#include <LibWeb/HTML/ImageBitmap.h>
|
||||
#include <LibWeb/SVG/SVGImageElement.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
static void default_source_size(CanvasImageSource const& image, float& source_width, float& source_height)
|
||||
Gfx::IntSize canvas_image_source_dimensions(CanvasImageSource const& image)
|
||||
{
|
||||
image.visit(
|
||||
[&source_width, &source_height](GC::Root<HTMLImageElement> const& source) {
|
||||
if (source->immutable_bitmap()) {
|
||||
source_width = source->immutable_bitmap()->width();
|
||||
source_height = source->immutable_bitmap()->height();
|
||||
} else {
|
||||
// FIXME: This is very janky and not correct.
|
||||
source_width = source->width();
|
||||
source_height = source->height();
|
||||
}
|
||||
},
|
||||
[&source_width, &source_height](GC::Root<SVG::SVGImageElement> const& source) {
|
||||
if (source->current_image_bitmap()) {
|
||||
source_width = source->current_image_bitmap()->width();
|
||||
source_height = source->current_image_bitmap()->height();
|
||||
} else {
|
||||
// FIXME: This is very janky and not correct.
|
||||
source_width = source->width()->anim_val()->value();
|
||||
source_height = source->height()->anim_val()->value();
|
||||
}
|
||||
},
|
||||
[&source_width, &source_height](GC::Root<HTML::HTMLVideoElement> const& source) {
|
||||
if (auto const bitmap = source->bitmap(); bitmap) {
|
||||
source_width = bitmap->width();
|
||||
source_height = bitmap->height();
|
||||
} else {
|
||||
source_width = source->video_width();
|
||||
source_height = source->video_height();
|
||||
}
|
||||
},
|
||||
[&source_width, &source_height](GC::Root<OffscreenCanvas> const& source) {
|
||||
auto const bitmap = source->bitmap();
|
||||
return image.visit(
|
||||
[](GC::Root<HTMLImageElement> const& source) -> Gfx::IntSize {
|
||||
if (auto immutable_bitmap = source->immutable_bitmap())
|
||||
return immutable_bitmap->size();
|
||||
|
||||
if (!bitmap) {
|
||||
source_width = 0;
|
||||
source_height = 0;
|
||||
return;
|
||||
}
|
||||
source_width = bitmap->width();
|
||||
source_height = bitmap->height();
|
||||
// FIXME: This is very janky and not correct.
|
||||
return { source->width(), source->height() };
|
||||
},
|
||||
[&source_width, &source_height](GC::Root<HTMLCanvasElement> const& source) {
|
||||
if (source->surface()) {
|
||||
source_width = source->surface()->size().width();
|
||||
source_height = source->surface()->size().height();
|
||||
} else {
|
||||
source_width = source->width();
|
||||
source_height = source->height();
|
||||
}
|
||||
[](GC::Root<SVG::SVGImageElement> const& source) -> Gfx::IntSize {
|
||||
if (auto immutable_bitmap = source->current_image_bitmap())
|
||||
return immutable_bitmap->size();
|
||||
|
||||
// FIXME: This is very janky and not correct.
|
||||
return { source->width()->anim_val()->value(), source->height()->anim_val()->value() };
|
||||
},
|
||||
[&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();
|
||||
}
|
||||
[](GC::Root<HTMLCanvasElement> const& source) -> Gfx::IntSize {
|
||||
if (auto painting_surface = source->surface())
|
||||
return painting_surface->size();
|
||||
return { source->width(), source->height() };
|
||||
},
|
||||
[](GC::Root<ImageBitmap> const& source) -> Gfx::IntSize {
|
||||
if (auto* bitmap = source->bitmap())
|
||||
return bitmap->size();
|
||||
return { source->width(), source->height() };
|
||||
},
|
||||
[](GC::Root<OffscreenCanvas> const& source) -> Gfx::IntSize {
|
||||
if (auto bitmap = source->bitmap())
|
||||
return bitmap->size();
|
||||
return {};
|
||||
},
|
||||
[](GC::Root<HTMLVideoElement> const& source) -> Gfx::IntSize {
|
||||
if (auto bitmap = source->bitmap())
|
||||
return bitmap->size();
|
||||
return { source->video_width(), source->video_height() };
|
||||
});
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> CanvasDrawImage::draw_image(Web::HTML::CanvasImageSource const& image, float destination_x, float destination_y)
|
||||
RefPtr<Gfx::ImmutableBitmap> canvas_image_source_bitmap(CanvasImageSource const& image)
|
||||
{
|
||||
return image.visit(
|
||||
[](OneOf<GC::Root<HTMLImageElement>, GC::Root<SVG::SVGImageElement>> auto const& element) {
|
||||
return element->default_image_bitmap();
|
||||
},
|
||||
[](GC::Root<HTMLCanvasElement> const& canvas) -> RefPtr<Gfx::ImmutableBitmap> {
|
||||
auto surface = canvas->surface();
|
||||
if (!surface)
|
||||
return Gfx::ImmutableBitmap::create(*canvas->get_bitmap_from_surface());
|
||||
return Gfx::ImmutableBitmap::create_snapshot_from_painting_surface(*surface);
|
||||
},
|
||||
[](OneOf<GC::Root<ImageBitmap>, GC::Root<OffscreenCanvas>, GC::Root<HTMLVideoElement>> auto const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
||||
auto bitmap = source->bitmap();
|
||||
if (!bitmap)
|
||||
return {};
|
||||
return Gfx::ImmutableBitmap::create(*bitmap);
|
||||
});
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> CanvasDrawImage::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_internal(image, 0, 0, source_width, source_height, destination_x, destination_y, source_width, source_height);
|
||||
auto size = canvas_image_source_dimensions(image);
|
||||
return draw_image_internal(image, 0, 0, size.width(), size.height(), destination_x, destination_y, size.width(), size.height());
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> CanvasDrawImage::draw_image(Web::HTML::CanvasImageSource const& image, float destination_x, float destination_y, float destination_width, float destination_height)
|
||||
WebIDL::ExceptionOr<void> CanvasDrawImage::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_internal(image, 0, 0, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
|
||||
auto size = canvas_image_source_dimensions(image);
|
||||
return draw_image_internal(image, 0, 0, size.width(), size.height(), destination_x, destination_y, destination_width, destination_height);
|
||||
}
|
||||
|
||||
WebIDL::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)
|
||||
WebIDL::ExceptionOr<void> CanvasDrawImage::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)
|
||||
{
|
||||
return draw_image_internal(image, source_x, source_y, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue