HTMLImageElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. m_image_loader.on_load = [this] {
  41. if (image_decoder() && image_decoder()->is_animated() && image_decoder()->frame_count() > 1) {
  42. const auto& first_frame = image_decoder()->frame(0);
  43. m_timer->set_interval(first_frame.duration);
  44. m_timer->on_timeout = [this] { animate(); };
  45. m_timer->start();
  46. }
  47. this->document().update_layout();
  48. dispatch_event(Event::create("load"));
  49. };
  50. m_image_loader.on_fail = [this] {
  51. dbg() << "HTMLImageElement: Resource did fail: " << this->src();
  52. this->document().update_layout();
  53. dispatch_event(Event::create("error"));
  54. };
  55. }
  56. HTMLImageElement::~HTMLImageElement()
  57. {
  58. }
  59. void HTMLImageElement::parse_attribute(const FlyString& name, const String& value)
  60. {
  61. HTMLElement::parse_attribute(name, value);
  62. if (name == HTML::AttributeNames::src)
  63. m_image_loader.load(document().complete_url(value));
  64. }
  65. void HTMLImageElement::animate()
  66. {
  67. if (!layout_node())
  68. return;
  69. auto* decoder = image_decoder();
  70. ASSERT(decoder);
  71. m_current_frame_index = (m_current_frame_index + 1) % decoder->frame_count();
  72. const auto& current_frame = decoder->frame(m_current_frame_index);
  73. if (current_frame.duration != m_timer->interval()) {
  74. m_timer->restart(current_frame.duration);
  75. }
  76. if (m_current_frame_index == decoder->frame_count() - 1) {
  77. ++m_loops_completed;
  78. if (m_loops_completed > 0 && m_loops_completed == decoder->loop_count()) {
  79. m_timer->stop();
  80. }
  81. }
  82. layout_node()->set_needs_display();
  83. }
  84. #if 0
  85. int HTMLImageElement::preferred_width() const
  86. {
  87. return attribute(HTML::AttributeNames::width).to_int().value_or(m_image_decoder ? m_image_decoder->width() : 0);
  88. }
  89. int HTMLImageElement::preferred_height() const
  90. {
  91. return attribute(HTML::AttributeNames::height).to_int().value_or(m_image_decoder ? m_image_decoder->height() : 0);
  92. }
  93. #endif
  94. RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
  95. {
  96. auto style = document().style_resolver().resolve_style(*this, parent_style);
  97. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  98. if (display == "none")
  99. return nullptr;
  100. return adopt(*new LayoutImage(*this, move(style), m_image_loader));
  101. }
  102. const Gfx::ImageDecoder* HTMLImageElement::image_decoder() const
  103. {
  104. return m_image_loader.image_decoder();
  105. }
  106. const Gfx::Bitmap* HTMLImageElement::bitmap() const
  107. {
  108. auto* decoder = image_decoder();
  109. if (!decoder)
  110. return nullptr;
  111. if (decoder->is_animated())
  112. return decoder->frame(m_current_frame_index).image;
  113. return decoder->bitmap();
  114. }
  115. void HTMLImageElement::set_visible_in_viewport(Badge<LayoutDocument>, bool visible_in_viewport)
  116. {
  117. m_image_loader.set_visible_in_viewport(visible_in_viewport);
  118. }
  119. }