| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-05-11 19:23:36 +02:00
										 |  |  |  * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-11 19:23:36 +02:00
										 |  |  | #include <LibWeb/HTML/DecodedImageData.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | #include <LibWeb/HTML/HTMLImageElement.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-11 19:23:36 +02:00
										 |  |  | #include <LibWeb/HTML/ImageRequest.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | #include <LibWeb/Layout/ImageBox.h>
 | 
					
						
							| 
									
										
										
										
											2022-06-15 21:29:55 +01:00
										 |  |  | #include <LibWeb/Painting/BorderRadiusCornerClipper.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>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Painting { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-11 12:51:49 +01:00
										 |  |  | JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box) | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-11 12:51:49 +01:00
										 |  |  |     return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box) | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |     : PaintableBox(layout_box) | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-09 07:02:28 +02:00
										 |  |  |     browsing_context().register_viewport_client(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ImagePaintable::finalize() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     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.
 | 
					
						
							|  |  |  |     browsing_context().unregister_viewport_client(*this); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Layout::ImageBox const& ImagePaintable::layout_box() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |     return static_cast<Layout::ImageBox const&>(layout_node()); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!is_visible()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-10 21:28:57 +01:00
										 |  |  |     // FIXME: This should be done at a different level.
 | 
					
						
							|  |  |  |     if (is_out_of_view(context)) | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |     PaintableBox::paint(context, phase); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (phase == PaintPhase::Foreground) { | 
					
						
							|  |  |  |         if (layout_box().renders_as_alt_text()) { | 
					
						
							|  |  |  |             auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node()); | 
					
						
							| 
									
										
										
										
											2022-10-31 19:46:55 +00:00
										 |  |  |             auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>(); | 
					
						
							| 
									
										
										
										
											2022-09-17 21:25:50 +02:00
										 |  |  |             context.painter().set_font(Platform::FontPlugin::the().default_font()); | 
					
						
							| 
									
										
										
										
											2023-04-29 16:47:52 -04:00
										 |  |  |             Gfx::StylePainter::paint_frame(context.painter(), enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |             auto alt = image_element.alt(); | 
					
						
							|  |  |  |             if (alt.is_empty()) | 
					
						
							|  |  |  |                 alt = image_element.src(); | 
					
						
							| 
									
										
										
										
											2022-10-31 19:46:55 +00:00
										 |  |  |             context.painter().draw_text(enclosing_rect, alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right); | 
					
						
							| 
									
										
										
										
											2023-05-12 07:17:01 +02:00
										 |  |  |         } else if (auto bitmap = layout_box().image_provider().current_image_bitmap()) { | 
					
						
							| 
									
										
										
										
											2022-10-31 19:46:55 +00:00
										 |  |  |             auto image_rect = context.rounded_device_rect(absolute_rect()); | 
					
						
							| 
									
										
										
										
											2022-10-27 16:02:32 +01:00
										 |  |  |             ScopedCornerRadiusClip corner_clip { context, context.painter(), image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) }; | 
					
						
							| 
									
										
										
										
											2023-05-19 00:41:12 +02:00
										 |  |  |             auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), bitmap->rect(), image_rect.to_type<int>()); | 
					
						
							|  |  |  |             context.painter().draw_scaled_bitmap(image_rect.to_type<int>(), *bitmap, bitmap->rect(), 1.f, scaling_mode); | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 07:02:28 +02:00
										 |  |  | void ImagePaintable::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-12 07:17:01 +02:00
										 |  |  |     const_cast<Layout::ImageProvider&>(layout_box().image_provider()).set_visible_in_viewport(viewport_rect.intersects(absolute_rect())); | 
					
						
							| 
									
										
										
										
											2023-05-09 07:02:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | } |