| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 18:25:13 +01:00
										 |  |  | #include <LibGfx/FontDatabase.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-10 08:25:35 +01:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/ImageBox.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  | #include <LibWeb/Page/BrowsingContext.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, const ImageLoader& image_loader) | 
					
						
							|  |  |  |     : 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | ImageBox::~ImageBox() | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											2020-12-29 18:25:13 +01:00
										 |  |  |         auto& font = Gfx::FontDatabase::default_font(); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |         auto alt = image_element.alt(); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:13:54 +11:00
										 |  |  |         if (alt.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |             alt = image_element.src(); | 
					
						
							| 
									
										
										
										
											2021-11-03 10:25:01 +01:00
										 |  |  |         set_intrinsic_width(font.width(alt) + 16); | 
					
						
							|  |  |  |         set_intrinsic_height(font.glyph_height() + 16); | 
					
						
							| 
									
										
										
										
											2020-07-22 01:36:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!has_intrinsic_width() && !has_intrinsic_height()) { | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         set_width(16); | 
					
						
							|  |  |  |         set_height(16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ImageBox::paint(PaintContext& context, PaintPhase phase) | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-09 21:25:29 +02:00
										 |  |  |     if (!is_visible()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:53:08 +02:00
										 |  |  |     // FIXME: This should be done at a different level. Also rect() does not include padding etc!
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |     if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect()))) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:53:08 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     ReplacedBox::paint(context, phase); | 
					
						
							| 
									
										
										
										
											2020-06-13 22:18:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 18:57:35 +02:00
										 |  |  |     if (phase == PaintPhase::Foreground) { | 
					
						
							|  |  |  |         if (renders_as_alt_text()) { | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |             auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node()); | 
					
						
							| 
									
										
										
										
											2020-12-29 18:25:13 +01:00
										 |  |  |             context.painter().set_font(Gfx::FontDatabase::default_font()); | 
					
						
							| 
									
										
										
										
											2020-06-18 18:57:35 +02:00
										 |  |  |             Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2); | 
					
						
							|  |  |  |             auto alt = image_element.alt(); | 
					
						
							|  |  |  |             if (alt.is_empty()) | 
					
						
							|  |  |  |                 alt = image_element.src(); | 
					
						
							| 
									
										
										
										
											2021-01-06 10:34:31 +01:00
										 |  |  |             context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right); | 
					
						
							| 
									
										
										
										
											2021-01-29 22:30:48 +01:00
										 |  |  |         } else if (auto bitmap = m_image_loader.bitmap(m_image_loader.current_frame_index())) { | 
					
						
							| 
									
										
										
										
											2021-09-21 15:36:32 +01:00
										 |  |  |             context.painter().draw_scaled_bitmap(rounded_int_rect(absolute_rect()), *bitmap, bitmap->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend); | 
					
						
							| 
									
										
										
										
											2020-06-18 18:57:35 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-13 22:22:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:12:22 +02:00
										 |  |  | void ImageBox::browsing_context_did_set_viewport_rect(Gfx::IntRect const& viewport_rect) | 
					
						
							| 
									
										
										
										
											2020-06-14 19:32:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-12 11:47:09 -07:00
										 |  |  |     m_image_loader.set_visible_in_viewport(viewport_rect.to_type<float>().intersects(absolute_rect())); | 
					
						
							| 
									
										
										
										
											2020-06-14 19:32:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |