mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-06 16:19:40 +00:00
LibWeb: Add initial support for HTMLImageElement
in createImageBitmap
This commit is contained in:
parent
cc0496284b
commit
3b8ccf4d77
Notes:
github-actions[bot]
2025-08-03 19:49:16 +00:00
Author: https://github.com/IdanHo
Commit: 3b8ccf4d77
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5695
Reviewed-by: https://github.com/gmta ✅
11 changed files with 101 additions and 5 deletions
|
@ -242,11 +242,33 @@ GC::Ref<WebIDL::Promise> WindowOrWorkerGlobalScopeMixin::create_image_bitmap_imp
|
|||
[&](CanvasImageSource const& image_source) {
|
||||
image_source.visit(
|
||||
// -> img
|
||||
[&](GC::Root<HTMLImageElement> const&) {
|
||||
dbgln("(STUBBED) createImageBitmap() for HTMLImageElement");
|
||||
auto const error = JS::Error::create(realm, "Not Implemented: createImageBitmap() for HTMLImageElement"sv);
|
||||
TemporaryExecutionContext const context { relevant_realm(p->promise()), TemporaryExecutionContext::CallbacksEnabled::Yes };
|
||||
WebIDL::reject_promise(realm, *p, error);
|
||||
[&](GC::Root<HTMLImageElement> const& image_element) {
|
||||
// 1. If image's media data has no natural dimensions (e.g., it's a vector graphic with no specified content size) and options's resizeWidth or options's resizeHeight is not present, then return a promise rejected with an "InvalidStateError" DOMException.
|
||||
auto const has_natural_dimensions = image_element->intrinsic_width().has_value() && image_element->intrinsic_height().has_value();
|
||||
if (!has_natural_dimensions && (!options.has_value() || !options->resize_width.has_value() || !options->resize_width.has_value())) {
|
||||
WebIDL::reject_promise(realm, *p, WebIDL::InvalidStateError::create(image_bitmap->realm(), "Image data is detached"_string));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. If image's media data has no natural dimensions (e.g., it's a vector graphic with no specified content size), it should be rendered to a bitmap of the size specified by the resizeWidth and the resizeHeight options.
|
||||
// 3. Set imageBitmap's bitmap data to a copy of image's media data, cropped to the source rectangle with formatting. If this is an animated image, imageBitmap's bitmap data must only be taken from the default image of the animation (the one that the format defines is to be used when animation is not supported or is disabled), or, if there is no such image, the first frame of the animation.
|
||||
// FIXME: Actually crop the image to the source rectangle with formatting: https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#cropped-to-the-source-rectangle-with-formatting
|
||||
RefPtr<Gfx::ImmutableBitmap> immutable_bitmap;
|
||||
if (has_natural_dimensions) {
|
||||
immutable_bitmap = image_element->default_image_bitmap(Gfx::IntSize { *image_element->intrinsic_width(), *image_element->intrinsic_height() });
|
||||
} else {
|
||||
immutable_bitmap = image_element->default_image_bitmap(Gfx::IntSize { *options->resize_width, *options->resize_height });
|
||||
}
|
||||
image_bitmap->set_bitmap(MUST(immutable_bitmap->bitmap()->clone()));
|
||||
|
||||
// FIXME: 4. If image is not origin-clean, then set the origin-clean flag of imageBitmap's bitmap to false.
|
||||
|
||||
// 5. Queue a global task, using the bitmap task source, to resolve promise with imageBitmap.
|
||||
queue_global_task(Task::Source::BitmapTask, image_bitmap, GC::create_function(realm.heap(), [p, image_bitmap] {
|
||||
auto& realm = relevant_realm(image_bitmap);
|
||||
TemporaryExecutionContext const context { realm, TemporaryExecutionContext::CallbacksEnabled::Yes };
|
||||
WebIDL::resolve_promise(realm, *p, image_bitmap);
|
||||
}));
|
||||
},
|
||||
// -> SVG image
|
||||
[&](GC::Root<SVG::SVGImageElement> const&) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue