From 41ab0837fa50f8fe32d727e447672fefb37a9434 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 20 May 2023 17:19:11 +0200 Subject: LibWeb: Render SVG-as-image into an isolated top-level browsing context In order to separate the SVG content from the rest of the engine, it gets its very own Page, PageClient, top-level browsing context, etc. Unfortunately, we do have to get the palette and CSS/device pixel ratios from the host Page for now, maybe that's something we could refactor in the future. Note that this doesn't work visually yet, since we don't calculate the intrinsic sizes & ratio for SVG images. That comes next. :^) --- .../Libraries/LibWeb/HTML/HTMLImageElement.cpp | 44 +++++++++++++++------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp') diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp index 56537cd7ed..02b1b5b8da 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace Web::HTML { @@ -545,21 +546,38 @@ void HTMLImageElement::handle_successful_fetch(AK::URL const& url_string, ImageR m_load_event_delayer.clear(); }; - auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes()); - if (!result.has_value()) { - dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors()); - return; - } + // FIXME: Look at the MIME type instead! + bool is_svg_image = url_string.basename().ends_with(".svg"sv); - Vector frames; - for (auto& frame : result.value().frames) { - frames.append(AnimatedBitmapDecodedImageData::Frame { - .bitmap = frame.bitmap, - .duration = static_cast(frame.duration), - }); + RefPtr image_data; + + if (is_svg_image) { + VERIFY(document().page()); + auto result = SVG::SVGDecodedImageData::create(*document().page(), url_string, data); + if (result.is_error()) { + dbgln("Failed to decode SVG image: {}", result.error()); + dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors()); + return; + } + + image_data = result.release_value(); + } else { + auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes()); + if (!result.has_value()) { + dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors()); + return; + } + + Vector frames; + for (auto& frame : result.value().frames) { + frames.append(AnimatedBitmapDecodedImageData::Frame { + .bitmap = frame.bitmap, + .duration = static_cast(frame.duration), + }); + } + image_data = AnimatedBitmapDecodedImageData::create(move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors(); } - auto image_data = AnimatedBitmapDecodedImageData::create(move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors(); image_request.set_image_data(image_data); ListOfAvailableImages::Key key; @@ -580,7 +598,7 @@ void HTMLImageElement::handle_successful_fetch(AK::URL const& url_string, ImageR image_request.set_state(ImageRequest::State::CompletelyAvailable); // 3. Add the image to the list of available images using the key key, with the ignore higher-layer caching flag set. - document().list_of_available_images().add(key, image_data, true).release_value_but_fixme_should_propagate_errors(); + document().list_of_available_images().add(key, *image_data, true).release_value_but_fixme_should_propagate_errors(); // 4. Fire an event named load at the img element. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load).release_value_but_fixme_should_propagate_errors()); -- cgit v1.2.3