| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/Font.h>
 | 
					
						
							|  |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/Layout/LayoutImage.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style) | 
					
						
							|  |  |  |     : LayoutReplaced(element, move(style)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LayoutImage::~LayoutImage() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 19:20:49 +02:00
										 |  |  | void LayoutImage::layout(LayoutMode layout_mode) | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-06 23:15:30 +11:00
										 |  |  |     if (node().preferred_width() && node().preferred_height()) { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:13:27 +02:00
										 |  |  |         set_has_intrinsic_width(true); | 
					
						
							|  |  |  |         set_has_intrinsic_height(true); | 
					
						
							|  |  |  |         set_intrinsic_width(node().preferred_width()); | 
					
						
							|  |  |  |         set_intrinsic_height(node().preferred_height()); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:15:30 +11:00
										 |  |  |     } else if (renders_as_alt_text()) { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         auto& font = Gfx::Font::default_font(); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:13:54 +11:00
										 |  |  |         auto alt = node().alt(); | 
					
						
							|  |  |  |         if (alt.is_empty()) | 
					
						
							|  |  |  |             alt = node().src(); | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         set_width(font.width(alt) + 16); | 
					
						
							|  |  |  |         set_height(font.glyph_height() + 16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:41:14 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         set_width(16); | 
					
						
							|  |  |  |         set_height(16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 19:20:49 +02:00
										 |  |  |     LayoutReplaced::layout(layout_mode); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LayoutImage::render(RenderingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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-06-13 22:18:12 +02:00
										 |  |  |     LayoutReplaced::render(context); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     if (renders_as_alt_text()) { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         context.painter().set_font(Gfx::Font::default_font()); | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:13:54 +11:00
										 |  |  |         auto alt = node().alt(); | 
					
						
							|  |  |  |         if (alt.is_empty()) | 
					
						
							|  |  |  |             alt = node().src(); | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), Gfx::TextElision::Right); | 
					
						
							| 
									
										
										
										
											2019-11-18 20:26:32 +01:00
										 |  |  |     } else if (node().bitmap()) | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |         context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *node().bitmap(), node().bitmap()->rect()); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  |     LayoutReplaced::render(context); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool LayoutImage::renders_as_alt_text() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:39:59 +02:00
										 |  |  |     return !node().image_decoder(); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |