Serenity Operating System
at hosted 127 lines 4.0 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/Bitmap.h> 28#include <LibGfx/ImageDecoder.h> 29#include <LibWeb/CSS/StyleResolver.h> 30#include <LibWeb/DOM/Document.h> 31#include <LibWeb/DOM/HTMLImageElement.h> 32#include <LibWeb/Layout/LayoutImage.h> 33#include <LibWeb/ResourceLoader.h> 34 35namespace Web { 36 37HTMLImageElement::HTMLImageElement(Document& document, const FlyString& tag_name) 38 : HTMLElement(document, tag_name) 39{ 40} 41 42HTMLImageElement::~HTMLImageElement() 43{ 44} 45 46void HTMLImageElement::parse_attribute(const FlyString& name, const String& value) 47{ 48 if (name.equals_ignoring_case("src")) 49 load_image(value); 50} 51 52void HTMLImageElement::load_image(const String& src) 53{ 54 URL src_url = document().complete_url(src); 55 ResourceLoader::the().load(src_url, [this, weak_element = make_weak_ptr()](auto data) { 56 if (!weak_element) { 57 dbg() << "HTMLImageElement: Load completed after element destroyed."; 58 return; 59 } 60 if (data.is_null()) { 61 dbg() << "HTMLImageElement: Failed to load " << this->src(); 62 return; 63 } 64 65 m_encoded_data = data; 66 m_image_decoder = Gfx::ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size()); 67 document().update_layout(); 68 }); 69} 70 71int HTMLImageElement::preferred_width() const 72{ 73 bool ok = false; 74 int width = attribute("width").to_int(ok); 75 if (ok) 76 return width; 77 78 if (m_image_decoder) 79 return m_image_decoder->width(); 80 81 return 0; 82} 83 84int HTMLImageElement::preferred_height() const 85{ 86 bool ok = false; 87 int height = attribute("height").to_int(ok); 88 if (ok) 89 return height; 90 91 if (m_image_decoder) 92 return m_image_decoder->height(); 93 94 return 0; 95} 96 97RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const 98{ 99 auto style = document().style_resolver().resolve_style(*this, parent_style); 100 auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline"); 101 if (display == "none") 102 return nullptr; 103 return adopt(*new LayoutImage(*this, move(style))); 104} 105 106const Gfx::Bitmap* HTMLImageElement::bitmap() const 107{ 108 if (!m_image_decoder) 109 return nullptr; 110 return m_image_decoder->bitmap(); 111} 112 113void HTMLImageElement::set_volatile(Badge<LayoutDocument>, bool v) 114{ 115 if (!m_image_decoder) 116 return; 117 if (v) { 118 m_image_decoder->set_volatile(); 119 return; 120 } 121 bool has_image = m_image_decoder->set_nonvolatile(); 122 if (has_image) 123 return; 124 m_image_decoder = Gfx::ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size()); 125} 126 127}