| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  |  * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |  * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-03 07:22:28 -04:00
										 |  |  | #include <LibURL/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-08 07:51:03 +02:00
										 |  |  | #include <LibWeb/CSS/ComputedValues.h>
 | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  | #include <LibWeb/CSS/Fetch.h>
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  | #include <LibWeb/HTML/DecodedImageData.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/PotentialCORSRequest.h>
 | 
					
						
							| 
									
										
										
										
											2025-04-10 15:52:33 +01:00
										 |  |  | #include <LibWeb/HTML/SharedResourceRequest.h>
 | 
					
						
							| 
									
										
										
										
											2024-06-23 18:40:10 +02:00
										 |  |  | #include <LibWeb/Painting/DisplayListRecorder.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-08 07:51:03 +02:00
										 |  |  | #include <LibWeb/Painting/PaintContext.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | #include <LibWeb/Platform/Timer.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-15 15:18:27 -06:00
										 |  |  | ValueComparingNonnullRefPtr<ImageStyleValue const> ImageStyleValue::create(URL const& url) | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return adopt_ref(*new (nothrow) ImageStyleValue(url)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-15 15:18:27 -06:00
										 |  |  | ValueComparingNonnullRefPtr<ImageStyleValue const> ImageStyleValue::create(::URL::URL const& url) | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return adopt_ref(*new (nothrow) ImageStyleValue(URL { url.to_string() })); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ImageStyleValue::ImageStyleValue(URL const& url) | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |     : AbstractImageStyleValue(Type::Image) | 
					
						
							|  |  |  |     , m_url(url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-16 10:38:35 +02:00
										 |  |  | ImageStyleValue::~ImageStyleValue() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 21:37:08 +13:00
										 |  |  | void ImageStyleValue::visit_edges(JS::Cell::Visitor& visitor) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-26 14:40:23 +00:00
										 |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2024-10-30 21:37:08 +13:00
										 |  |  |     // FIXME: visit_edges in non-GC allocated classes is confusing pattern.
 | 
					
						
							|  |  |  |     //        Consider making CSSStyleValue to be GC allocated instead.
 | 
					
						
							|  |  |  |     visitor.visit(m_resource_request); | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  |     visitor.visit(m_style_sheet); | 
					
						
							| 
									
										
										
										
											2024-10-30 21:37:08 +13:00
										 |  |  |     visitor.visit(m_timer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | void ImageStyleValue::load_any_resources(DOM::Document& document) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-03 15:27:08 +12:00
										 |  |  |     if (m_resource_request) | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     m_document = &document; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  |     if (m_style_sheet) { | 
					
						
							|  |  |  |         m_resource_request = fetch_an_external_image_for_a_stylesheet(m_url, { *m_style_sheet }); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         m_resource_request = fetch_an_external_image_for_a_stylesheet(m_url, { document }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (m_resource_request) { | 
					
						
							|  |  |  |         m_resource_request->add_callbacks( | 
					
						
							|  |  |  |             [this, weak_this = make_weak_ptr()] { | 
					
						
							|  |  |  |                 if (!weak_this || !m_document) | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 auto image_data = m_resource_request->image_data(); | 
					
						
							|  |  |  |                 if (image_data->is_animated() && image_data->frame_count() > 1) { | 
					
						
							|  |  |  |                     m_timer = Platform::Timer::create(m_document->heap()); | 
					
						
							|  |  |  |                     m_timer->set_interval(image_data->frame_duration(0)); | 
					
						
							|  |  |  |                     m_timer->on_timeout = GC::create_function(m_document->heap(), [this] { animate(); }); | 
					
						
							|  |  |  |                     m_timer->start(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             nullptr); | 
					
						
							| 
									
										
										
										
											2023-06-14 12:45:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ImageStyleValue::animate() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-03 15:27:08 +12:00
										 |  |  |     if (!m_resource_request) | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2024-08-03 15:27:08 +12:00
										 |  |  |     auto image_data = m_resource_request->image_data(); | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     if (!image_data) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_current_frame_index = (m_current_frame_index + 1) % image_data->frame_count(); | 
					
						
							|  |  |  |     auto current_frame_duration = image_data->frame_duration(m_current_frame_index); | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (current_frame_duration != m_timer->interval()) | 
					
						
							|  |  |  |         m_timer->restart(current_frame_duration); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     if (m_current_frame_index == image_data->frame_count() - 1) { | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |         ++m_loops_completed; | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |         if (m_loops_completed > 0 && m_loops_completed == image_data->loop_count()) | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |             m_timer->stop(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (on_animate) | 
					
						
							|  |  |  |         on_animate(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  | bool ImageStyleValue::is_paintable() const | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     return image_data(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 14:45:45 +01:00
										 |  |  | Gfx::ImmutableBitmap const* ImageStyleValue::bitmap(size_t frame_index, Gfx::IntSize size) const | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (auto image_data = this->image_data()) | 
					
						
							|  |  |  |         return image_data->bitmap(frame_index, size); | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  | String ImageStyleValue::to_string(SerializationMode) const | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  |     return m_url.to_string(); | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 11:10:54 +01:00
										 |  |  | bool ImageStyleValue::equals(CSSStyleValue const& other) const | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (type() != other.type()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return m_url == other.as_image().m_url; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<CSSPixels> ImageStyleValue::natural_width() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     if (auto image_data = this->image_data()) | 
					
						
							|  |  |  |         return image_data->intrinsic_width(); | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<CSSPixels> ImageStyleValue::natural_height() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     if (auto image_data = this->image_data()) | 
					
						
							|  |  |  |         return image_data->intrinsic_height(); | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 23:51:00 +01:00
										 |  |  | Optional<CSSPixelFraction> ImageStyleValue::natural_aspect_ratio() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto image_data = this->image_data()) | 
					
						
							|  |  |  |         return image_data->intrinsic_aspect_ratio(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 15:26:47 +03:00
										 |  |  | void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |     if (auto const* b = bitmap(m_current_frame_index, dest_rect.size().to_type<int>()); b != nullptr) { | 
					
						
							|  |  |  |         auto scaling_mode = to_gfx_scaling_mode(image_rendering, b->rect(), dest_rect.to_type<int>()); | 
					
						
							| 
									
										
										
										
											2025-02-04 21:51:51 +01:00
										 |  |  |         auto dest_int_rect = dest_rect.to_type<int>(); | 
					
						
							|  |  |  |         context.display_list_recorder().draw_scaled_immutable_bitmap(dest_int_rect, dest_int_rect, *b, scaling_mode); | 
					
						
							| 
									
										
										
										
											2023-05-19 00:41:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 15:36:28 +03:00
										 |  |  | Gfx::ImmutableBitmap const* ImageStyleValue::current_frame_bitmap(DevicePixelRect const& dest_rect) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return bitmap(m_current_frame_index, dest_rect.size().to_type<int>()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ptr<HTML::DecodedImageData> ImageStyleValue::image_data() const | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-03 15:27:08 +12:00
										 |  |  |     if (!m_resource_request) | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2024-08-03 15:27:08 +12:00
										 |  |  |     return m_resource_request->image_data(); | 
					
						
							| 
									
										
										
										
											2023-06-11 15:37:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 13:48:53 +01:00
										 |  |  | Optional<Gfx::Color> ImageStyleValue::color_if_single_pixel_bitmap() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto const* b = bitmap(m_current_frame_index)) { | 
					
						
							|  |  |  |         if (b->width() == 1 && b->height() == 1) | 
					
						
							| 
									
										
										
										
											2024-11-09 05:48:17 +01:00
										 |  |  |             return b->get_pixel(0, 0); | 
					
						
							| 
									
										
										
										
											2024-01-01 13:48:53 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-10 16:04:34 +01:00
										 |  |  | void ImageStyleValue::set_style_sheet(GC::Ptr<CSSStyleSheet> style_sheet) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::set_style_sheet(style_sheet); | 
					
						
							|  |  |  |     m_style_sheet = style_sheet; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-03 07:22:28 -04:00
										 |  |  | ValueComparingNonnullRefPtr<CSSStyleValue const> ImageStyleValue::absolutized(CSSPixelRect const&, Length::FontMetrics const&, Length::FontMetrics const&) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_url.url().is_empty()) | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: The spec has been updated to handle this better. The computation of the base URL here is roughly based on:
 | 
					
						
							|  |  |  |     //        https://drafts.csswg.org/css-values-4/#style-resource-base-url
 | 
					
						
							|  |  |  |     //        https://github.com/w3c/csswg-drafts/pull/12261
 | 
					
						
							|  |  |  |     auto base_url = [&]() -> Optional<::URL::URL> { | 
					
						
							|  |  |  |         if (m_style_sheet) { | 
					
						
							|  |  |  |             return m_style_sheet->base_url() | 
					
						
							|  |  |  |                 .value_or_lazy_evaluated_optional([&]() { return m_style_sheet->location(); }) | 
					
						
							|  |  |  |                 .value_or_lazy_evaluated_optional([&]() { return HTML::relevant_settings_object(*m_style_sheet).api_base_url(); }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_document) | 
					
						
							|  |  |  |             return m_document->base_url(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     }(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (base_url.has_value()) { | 
					
						
							|  |  |  |         if (auto resolved_url = ::URL::Parser::basic_parse(m_url.url(), *base_url); resolved_url.has_value()) | 
					
						
							|  |  |  |             return ImageStyleValue::create(*resolved_url); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 15:17:11 +00:00
										 |  |  | } |