| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |  * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 15:01:28 +01:00
										 |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-11 19:23:36 +02:00
										 |  |  | #include <LibWeb/HTML/DecodedImageData.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/ImageRequest.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/ImageBox.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | #include <LibWeb/Painting/ImagePaintable.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-17 21:25:50 +02:00
										 |  |  | #include <LibWeb/Platform/FontPlugin.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-12 07:17:01 +02:00
										 |  |  | ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, ImageProvider const& image_provider) | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     : ReplacedBox(document, element, move(style)) | 
					
						
							| 
									
										
										
										
											2023-05-12 07:17:01 +02:00
										 |  |  |     , m_image_provider(image_provider) | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 18:38:25 +02:00
										 |  |  | ImageBox::~ImageBox() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ImageBox::prepare_for_replaced_layout() | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-20 16:27:31 +02:00
										 |  |  |     set_intrinsic_width(m_image_provider.intrinsic_width()); | 
					
						
							|  |  |  |     set_intrinsic_height(m_image_provider.intrinsic_height()); | 
					
						
							|  |  |  |     set_intrinsic_aspect_ratio(m_image_provider.intrinsic_aspect_ratio()); | 
					
						
							| 
									
										
										
										
											2020-07-22 01:36:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (renders_as_alt_text()) { | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |         auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node()); | 
					
						
							| 
									
										
										
										
											2022-09-17 21:25:50 +02:00
										 |  |  |         auto& font = Platform::FontPlugin::the().default_font(); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |         auto alt = image_element.alt(); | 
					
						
							| 
									
										
										
										
											2022-09-07 16:46:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:54:05 +00:00
										 |  |  |         CSSPixels alt_text_width = 0; | 
					
						
							| 
									
										
										
										
											2022-09-07 16:46:05 +02:00
										 |  |  |         if (!m_cached_alt_text_width.has_value()) | 
					
						
							|  |  |  |             m_cached_alt_text_width = font.width(alt); | 
					
						
							|  |  |  |         alt_text_width = m_cached_alt_text_width.value(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         set_intrinsic_width(alt_text_width + 16); | 
					
						
							| 
									
										
										
										
											2022-03-27 00:58:52 +01:00
										 |  |  |         set_intrinsic_height(font.pixel_size() + 16); | 
					
						
							| 
									
										
										
										
											2020-07-22 01:36:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!has_intrinsic_width() && !has_intrinsic_height()) { | 
					
						
							| 
									
										
										
										
											2022-03-09 23:53:41 +01:00
										 |  |  |         // FIXME: Do something.
 | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 16:46:05 +02:00
										 |  |  | void ImageBox::dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_cached_alt_text_width = {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | bool ImageBox::renders_as_alt_text() const | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     if (is<HTML::HTMLImageElement>(dom_node())) | 
					
						
							| 
									
										
										
										
											2023-05-11 19:23:36 +02:00
										 |  |  |         return !static_cast<HTML::HTMLImageElement const&>(dom_node()).current_request().is_available(); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-11 12:51:49 +01:00
										 |  |  | JS::GCPtr<Painting::Paintable> ImageBox::create_paintable() const | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return Painting::ImagePaintable::create(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |