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