diff --git a/Userland/Libraries/LibWeb/Loader/ImageResource.cpp b/Userland/Libraries/LibWeb/Loader/ImageResource.cpp index d12cb43a58..d4da3bbd9e 100644 --- a/Userland/Libraries/LibWeb/Loader/ImageResource.cpp +++ b/Userland/Libraries/LibWeb/Loader/ImageResource.cpp @@ -6,8 +6,10 @@ #include #include +#include #include #include +#include namespace Web { @@ -47,9 +49,48 @@ void ImageResource::decode_if_needed() const if (!m_decoded_frames.is_empty()) return; - auto image = Platform::ImageCodecPlugin::the().decode_image(encoded_data()); - m_has_attempted_decode = true; + bool is_svg_image = mime_type().starts_with("image/svg+xml"sv) || url().basename().ends_with(".svg"sv); + if (is_svg_image) { + decode_svg_image(); + } else { + decode_image(); + } + m_has_attempted_decode = true; +} + +void ImageResource::decode_svg_image() const +{ + auto page = request().page(); + if (!page.has_value()) + return; + + auto svg_or_error = SVG::SVGDecodedImageData::create(page.value(), url(), encoded_data()); + if (svg_or_error.is_error()) { + dbgln("Could not decode svg image resource {}", url()); + return; + } + + auto svg = svg_or_error.release_value(); + m_loop_count = svg->loop_count(); + m_animated = svg->is_animated(); + m_decoded_frames.resize(svg->frame_count()); + for (size_t i = 0; i < m_decoded_frames.size(); ++i) { + auto& frame = m_decoded_frames[i]; + // FIXME: Decide on what to do when there is no intrinsic width or height + auto width = svg->intrinsic_width(); + auto height = svg->intrinsic_height(); + if (width.has_value() && height.has_value()) { + auto bitmap = svg->bitmap(i, { static_cast(width->value()), static_cast(height->value()) }); + frame.bitmap = bitmap->clone().release_value_but_fixme_should_propagate_errors(); + } + frame.duration = svg->frame_duration(i); + } +} + +void ImageResource::decode_image() const +{ + auto image = Platform::ImageCodecPlugin::the().decode_image(encoded_data()); if (!image.has_value()) { dbgln("Could not decode image resource {}", url()); return; diff --git a/Userland/Libraries/LibWeb/Loader/ImageResource.h b/Userland/Libraries/LibWeb/Loader/ImageResource.h index 0823a19a74..75f737a793 100644 --- a/Userland/Libraries/LibWeb/Loader/ImageResource.h +++ b/Userland/Libraries/LibWeb/Loader/ImageResource.h @@ -48,6 +48,8 @@ private: explicit ImageResource(Resource&); void decode_if_needed() const; + void decode_svg_image() const; + void decode_image() const; mutable bool m_animated { false }; mutable int m_loop_count { 0 }; diff --git a/Userland/Libraries/LibWeb/Loader/Resource.h b/Userland/Libraries/LibWeb/Loader/Resource.h index 2e3dda2370..a2d0e64f02 100644 --- a/Userland/Libraries/LibWeb/Loader/Resource.h +++ b/Userland/Libraries/LibWeb/Loader/Resource.h @@ -74,6 +74,8 @@ protected: explicit Resource(Type, LoadRequest const&); Resource(Type, Resource&); + LoadRequest request() const { return m_request; } + private: LoadRequest m_request; ByteBuffer m_encoded_data;