ImageStyleValue.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ImageStyleValue.h"
  10. #include <LibWeb/CSS/Serialize.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/Loader/ResourceLoader.h>
  13. #include <LibWeb/Platform/Timer.h>
  14. namespace Web::CSS {
  15. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  16. : AbstractImageStyleValue(Type::Image)
  17. , m_url(url)
  18. {
  19. }
  20. void ImageStyleValue::load_any_resources(DOM::Document& document)
  21. {
  22. if (resource())
  23. return;
  24. m_document = &document;
  25. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  26. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  27. }
  28. void ImageStyleValue::resource_did_load()
  29. {
  30. if (!m_document)
  31. return;
  32. // FIXME: Do less than a full repaint if possible?
  33. if (m_document && m_document->browsing_context())
  34. m_document->browsing_context()->set_needs_display();
  35. if (resource()->is_animated() && resource()->frame_count() > 1) {
  36. m_timer = Platform::Timer::create();
  37. m_timer->set_interval(resource()->frame_duration(0));
  38. m_timer->on_timeout = [this] { animate(); };
  39. m_timer->start();
  40. }
  41. }
  42. void ImageStyleValue::animate()
  43. {
  44. m_current_frame_index = (m_current_frame_index + 1) % resource()->frame_count();
  45. auto current_frame_duration = resource()->frame_duration(m_current_frame_index);
  46. if (current_frame_duration != m_timer->interval())
  47. m_timer->restart(current_frame_duration);
  48. if (m_current_frame_index == resource()->frame_count() - 1) {
  49. ++m_loops_completed;
  50. if (m_loops_completed > 0 && m_loops_completed == resource()->loop_count())
  51. m_timer->stop();
  52. }
  53. if (on_animate)
  54. on_animate();
  55. }
  56. Gfx::Bitmap const* ImageStyleValue::bitmap(size_t frame_index) const
  57. {
  58. if (!resource())
  59. return nullptr;
  60. return resource()->bitmap(frame_index);
  61. }
  62. ErrorOr<String> ImageStyleValue::to_string() const
  63. {
  64. return serialize_a_url(m_url.to_deprecated_string());
  65. }
  66. bool ImageStyleValue::equals(StyleValue const& other) const
  67. {
  68. if (type() != other.type())
  69. return false;
  70. return m_url == other.as_image().m_url;
  71. }
  72. Optional<CSSPixels> ImageStyleValue::natural_width() const
  73. {
  74. if (auto* b = bitmap(0); b != nullptr)
  75. return b->width();
  76. return {};
  77. }
  78. Optional<CSSPixels> ImageStyleValue::natural_height() const
  79. {
  80. if (auto* b = bitmap(0); b != nullptr)
  81. return b->height();
  82. return {};
  83. }
  84. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const
  85. {
  86. if (auto* b = bitmap(m_current_frame_index); b != nullptr)
  87. context.painter().draw_scaled_bitmap(dest_rect.to_type<int>(), *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering));
  88. }
  89. }