HTMLImageElement.cpp 3.9 KB

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