ImageBox.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/BrowsingContext.h>
  7. #include <LibWeb/Layout/ImageBox.h>
  8. #include <LibWeb/Painting/ImagePaintable.h>
  9. #include <LibWeb/Platform/FontPlugin.h>
  10. namespace Web::Layout {
  11. ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, ImageLoader const& image_loader)
  12. : ReplacedBox(document, element, move(style))
  13. , m_image_loader(image_loader)
  14. {
  15. browsing_context().register_viewport_client(*this);
  16. }
  17. ImageBox::~ImageBox() = default;
  18. void ImageBox::finalize()
  19. {
  20. Base::finalize();
  21. // NOTE: We unregister from the browsing context in finalize() to avoid trouble
  22. // in the scenario where our BrowsingContext has already been swept by GC.
  23. browsing_context().unregister_viewport_client(*this);
  24. }
  25. int ImageBox::preferred_width() const
  26. {
  27. return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
  28. }
  29. int ImageBox::preferred_height() const
  30. {
  31. return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height());
  32. }
  33. void ImageBox::prepare_for_replaced_layout()
  34. {
  35. if (!m_image_loader.has_loaded_or_failed()) {
  36. set_intrinsic_width(0);
  37. set_intrinsic_height(0);
  38. } else {
  39. if (m_image_loader.width()) {
  40. set_intrinsic_width(m_image_loader.width());
  41. }
  42. if (m_image_loader.height()) {
  43. set_intrinsic_height(m_image_loader.height());
  44. }
  45. if (m_image_loader.width() && m_image_loader.height()) {
  46. set_intrinsic_aspect_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
  47. } else {
  48. set_intrinsic_aspect_ratio({});
  49. }
  50. }
  51. if (renders_as_alt_text()) {
  52. auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
  53. auto& font = Platform::FontPlugin::the().default_font();
  54. auto alt = image_element.alt();
  55. if (alt.is_empty())
  56. alt = image_element.src();
  57. float alt_text_width = 0;
  58. if (!m_cached_alt_text_width.has_value())
  59. m_cached_alt_text_width = font.width(alt);
  60. alt_text_width = m_cached_alt_text_width.value();
  61. set_intrinsic_width(alt_text_width + 16);
  62. set_intrinsic_height(font.pixel_size() + 16);
  63. }
  64. if (!has_intrinsic_width() && !has_intrinsic_height()) {
  65. // FIXME: Do something.
  66. }
  67. }
  68. void ImageBox::dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>)
  69. {
  70. m_cached_alt_text_width = {};
  71. }
  72. bool ImageBox::renders_as_alt_text() const
  73. {
  74. if (is<HTML::HTMLImageElement>(dom_node()))
  75. return !m_image_loader.has_image();
  76. return false;
  77. }
  78. void ImageBox::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  79. {
  80. m_image_loader.set_visible_in_viewport(paint_box() && viewport_rect.intersects(paint_box()->absolute_rect()));
  81. }
  82. RefPtr<Painting::Paintable> ImageBox::create_paintable() const
  83. {
  84. return Painting::ImagePaintable::create(*this);
  85. }
  86. }