HTMLImageElement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <LibCore/Timer.h>
  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/Event.h>
  32. #include <LibWeb/DOM/HTMLImageElement.h>
  33. #include <LibWeb/Layout/LayoutImage.h>
  34. #include <LibWeb/Loader/ResourceLoader.h>
  35. namespace Web {
  36. HTMLImageElement::HTMLImageElement(Document& document, const FlyString& tag_name)
  37. : HTMLElement(document, tag_name)
  38. , m_timer(Core::Timer::construct())
  39. {
  40. }
  41. HTMLImageElement::~HTMLImageElement()
  42. {
  43. }
  44. void HTMLImageElement::parse_attribute(const FlyString& name, const String& value)
  45. {
  46. if (name.equals_ignoring_case("src"))
  47. load_image(value);
  48. }
  49. void HTMLImageElement::load_image(const String& src)
  50. {
  51. URL src_url = document().complete_url(src);
  52. ResourceLoader::the().load(src_url, [this, weak_element = make_weak_ptr()](auto data, auto&) {
  53. if (!weak_element) {
  54. dbg() << "HTMLImageElement: Load completed after element destroyed.";
  55. return;
  56. }
  57. if (data.is_null()) {
  58. dbg() << "HTMLImageElement: Failed to load " << this->src();
  59. return;
  60. }
  61. m_encoded_data = data;
  62. m_image_decoder = Gfx::ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size());
  63. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  64. const auto& first_frame = m_image_decoder->frame(0);
  65. m_timer->set_interval(first_frame.duration);
  66. m_timer->on_timeout = [this] { animate(); };
  67. m_timer->start();
  68. }
  69. document().update_layout();
  70. dispatch_event(Event::create("load"));
  71. });
  72. }
  73. void HTMLImageElement::animate()
  74. {
  75. if (!layout_node()) {
  76. return;
  77. }
  78. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  79. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  80. if (current_frame.duration != m_timer->interval()) {
  81. m_timer->restart(current_frame.duration);
  82. }
  83. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  84. ++m_loops_completed;
  85. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  86. m_timer->stop();
  87. }
  88. }
  89. layout_node()->set_needs_display();
  90. }
  91. int HTMLImageElement::preferred_width() const
  92. {
  93. bool ok = false;
  94. int width = attribute("width").to_int(ok);
  95. if (ok)
  96. return width;
  97. if (m_image_decoder)
  98. return m_image_decoder->width();
  99. return 0;
  100. }
  101. int HTMLImageElement::preferred_height() const
  102. {
  103. bool ok = false;
  104. int height = attribute("height").to_int(ok);
  105. if (ok)
  106. return height;
  107. if (m_image_decoder)
  108. return m_image_decoder->height();
  109. return 0;
  110. }
  111. RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
  112. {
  113. auto style = document().style_resolver().resolve_style(*this, parent_style);
  114. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  115. if (display == "none")
  116. return nullptr;
  117. return adopt(*new LayoutImage(*this, move(style)));
  118. }
  119. const Gfx::Bitmap* HTMLImageElement::bitmap() const
  120. {
  121. if (!m_image_decoder)
  122. return nullptr;
  123. if (m_image_decoder->is_animated()) {
  124. return m_image_decoder->frame(m_current_frame_index).image;
  125. }
  126. return m_image_decoder->bitmap();
  127. }
  128. void HTMLImageElement::set_volatile(Badge<LayoutDocument>, bool v)
  129. {
  130. if (!m_image_decoder)
  131. return;
  132. if (v) {
  133. m_image_decoder->set_volatile();
  134. return;
  135. }
  136. bool has_image = m_image_decoder->set_nonvolatile();
  137. if (has_image)
  138. return;
  139. m_image_decoder = Gfx::ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size());
  140. }
  141. }