| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, ImageLoader const& image_loader) | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     : ReplacedBox(document, element, move(style)) | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |     , m_image_loader(image_loader) | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |     browsing_context().register_viewport_client(*this); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 18:38:25 +02:00
										 |  |  | ImageBox::~ImageBox() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ImageBox::finalize() | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-20 18:38:25 +02:00
										 |  |  |     Base::finalize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // NOTE: We unregister from the browsing context in finalize() to avoid trouble
 | 
					
						
							|  |  |  |     //       in the scenario where our BrowsingContext has already been swept by GC.
 | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |     browsing_context().unregister_viewport_client(*this); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | int ImageBox::preferred_width() const | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width()); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | int ImageBox::preferred_height() const | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height()); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ImageBox::prepare_for_replaced_layout() | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-12 13:47:55 +02:00
										 |  |  |     if (!m_image_loader.has_loaded_or_failed()) { | 
					
						
							|  |  |  |         set_intrinsic_width(0); | 
					
						
							|  |  |  |         set_intrinsic_height(0); | 
					
						
							| 
									
										
										
										
											2020-07-22 01:36:07 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-08-12 13:47:55 +02:00
										 |  |  |         if (m_image_loader.width()) { | 
					
						
							|  |  |  |             set_intrinsic_width(m_image_loader.width()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (m_image_loader.height()) { | 
					
						
							|  |  |  |             set_intrinsic_height(m_image_loader.height()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_image_loader.width() && m_image_loader.height()) { | 
					
						
							| 
									
										
										
										
											2021-10-14 18:48:49 +02:00
										 |  |  |             set_intrinsic_aspect_ratio((float)m_image_loader.width() / (float)m_image_loader.height()); | 
					
						
							| 
									
										
										
										
											2020-08-12 13:47:55 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-10-14 18:48:49 +02:00
										 |  |  |             set_intrinsic_aspect_ratio({}); | 
					
						
							| 
									
										
										
										
											2020-08-12 13:47:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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())) | 
					
						
							| 
									
										
										
										
											2020-06-22 21:41:10 +02:00
										 |  |  |         return !m_image_loader.has_image(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-03 12:49:54 +00:00
										 |  |  | void ImageBox::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect) | 
					
						
							| 
									
										
										
										
											2020-06-14 19:32:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-20 16:00:42 +01:00
										 |  |  |     m_image_loader.set_visible_in_viewport(paintable_box() && viewport_rect.intersects(paintable_box()->absolute_rect())); | 
					
						
							| 
									
										
										
										
											2020-06-14 19:32:23 +02: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
										 |  |  | } |