| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  | #include <LibDraw/Font.h>
 | 
					
						
							|  |  |  | #include <LibDraw/StylePainter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  | #include <LibHTML/Layout/LayoutImage.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style) | 
					
						
							|  |  |  |     : LayoutReplaced(element, move(style)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LayoutImage::~LayoutImage() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LayoutImage::layout() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-06 23:15:30 +11:00
										 |  |  |     if (node().preferred_width() && node().preferred_height()) { | 
					
						
							|  |  |  |         rect().set_width(node().preferred_width()); | 
					
						
							|  |  |  |         rect().set_height(node().preferred_height()); | 
					
						
							|  |  |  |     } else if (renders_as_alt_text()) { | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |         auto& font = Font::default_font(); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:13:54 +11:00
										 |  |  |         auto alt = node().alt(); | 
					
						
							|  |  |  |         if (alt.is_empty()) | 
					
						
							|  |  |  |             alt = node().src(); | 
					
						
							|  |  |  |         rect().set_width(font.width(alt) + 16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |         rect().set_height(font.glyph_height() + 16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:41:14 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-10-06 23:15:30 +11:00
										 |  |  |         rect().set_width(16); | 
					
						
							|  |  |  |         rect().set_height(16); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 22:07:45 +02:00
										 |  |  |     LayoutReplaced::layout(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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!
 | 
					
						
							|  |  |  |     if (!context.viewport_rect().intersects(rect())) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     if (renders_as_alt_text()) { | 
					
						
							|  |  |  |         context.painter().set_font(Font::default_font()); | 
					
						
							|  |  |  |         StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2); | 
					
						
							| 
									
										
										
										
											2019-10-06 23:13:54 +11:00
										 |  |  |         auto alt = node().alt(); | 
					
						
							|  |  |  |         if (alt.is_empty()) | 
					
						
							|  |  |  |             alt = node().src(); | 
					
						
							| 
									
										
										
										
											2019-10-08 15:34:19 +02:00
										 |  |  |         context.painter().draw_text(rect(), alt, TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), TextElision::Right); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:41:14 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         context.painter().draw_scaled_bitmap(rect(), *node().bitmap(), node().bitmap()->rect()); | 
					
						
							| 
									
										
										
										
											2019-10-05 23:20:35 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |