ImageStyleValue.cpp 3.2 KB

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