Serenity Operating System
at portability 85 lines 3.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibGfx/Font.h> 28#include <LibGfx/StylePainter.h> 29#include <LibGUI/Painter.h> 30#include <LibHTML/Layout/LayoutImage.h> 31 32LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style) 33 : LayoutReplaced(element, move(style)) 34{ 35} 36 37LayoutImage::~LayoutImage() 38{ 39} 40 41void LayoutImage::layout() 42{ 43 if (node().preferred_width() && node().preferred_height()) { 44 rect().set_width(node().preferred_width()); 45 rect().set_height(node().preferred_height()); 46 } else if (renders_as_alt_text()) { 47 auto& font = Gfx::Font::default_font(); 48 auto alt = node().alt(); 49 if (alt.is_empty()) 50 alt = node().src(); 51 rect().set_width(font.width(alt) + 16); 52 rect().set_height(font.glyph_height() + 16); 53 } else { 54 rect().set_width(16); 55 rect().set_height(16); 56 } 57 58 LayoutReplaced::layout(); 59} 60 61void LayoutImage::render(RenderingContext& context) 62{ 63 if (!is_visible()) 64 return; 65 66 // FIXME: This should be done at a different level. Also rect() does not include padding etc! 67 if (!context.viewport_rect().intersects(enclosing_int_rect(rect()))) 68 return; 69 70 if (renders_as_alt_text()) { 71 context.painter().set_font(Gfx::Font::default_font()); 72 Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2); 73 auto alt = node().alt(); 74 if (alt.is_empty()) 75 alt = node().src(); 76 context.painter().draw_text(enclosing_int_rect(rect()), alt, Gfx::TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), Gfx::TextElision::Right); 77 } else if (node().bitmap()) 78 context.painter().draw_scaled_bitmap(enclosing_int_rect(rect()), *node().bitmap(), node().bitmap()->rect()); 79 LayoutReplaced::render(context); 80} 81 82bool LayoutImage::renders_as_alt_text() const 83{ 84 return !node().image_decoder(); 85}