ImageBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/FontDatabase.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibGfx/StylePainter.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/Layout/ImageBox.h>
  11. namespace Web::Layout {
  12. ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, const ImageLoader& image_loader)
  13. : ReplacedBox(document, element, move(style))
  14. , m_image_loader(image_loader)
  15. {
  16. browsing_context().register_viewport_client(*this);
  17. }
  18. ImageBox::~ImageBox()
  19. {
  20. browsing_context().unregister_viewport_client(*this);
  21. }
  22. int ImageBox::preferred_width() const
  23. {
  24. return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
  25. }
  26. int ImageBox::preferred_height() const
  27. {
  28. return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height());
  29. }
  30. void ImageBox::prepare_for_replaced_layout()
  31. {
  32. if (!m_image_loader.has_loaded_or_failed()) {
  33. set_intrinsic_width(0);
  34. set_intrinsic_height(0);
  35. } else {
  36. if (m_image_loader.width()) {
  37. set_intrinsic_width(m_image_loader.width());
  38. }
  39. if (m_image_loader.height()) {
  40. set_intrinsic_height(m_image_loader.height());
  41. }
  42. if (m_image_loader.width() && m_image_loader.height()) {
  43. set_intrinsic_aspect_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
  44. } else {
  45. set_intrinsic_aspect_ratio({});
  46. }
  47. }
  48. if (renders_as_alt_text()) {
  49. auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
  50. auto& font = Gfx::FontDatabase::default_font();
  51. auto alt = image_element.alt();
  52. if (alt.is_empty())
  53. alt = image_element.src();
  54. set_intrinsic_width(font.width(alt) + 16);
  55. set_intrinsic_height(font.glyph_height() + 16);
  56. }
  57. if (!has_intrinsic_width() && !has_intrinsic_height()) {
  58. set_width(16);
  59. set_height(16);
  60. }
  61. }
  62. void ImageBox::paint(PaintContext& context, PaintPhase phase)
  63. {
  64. if (!is_visible())
  65. return;
  66. // FIXME: This should be done at a different level. Also rect() does not include padding etc!
  67. if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect())))
  68. return;
  69. ReplacedBox::paint(context, phase);
  70. if (phase == PaintPhase::Foreground) {
  71. if (renders_as_alt_text()) {
  72. auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
  73. context.painter().set_font(Gfx::FontDatabase::default_font());
  74. Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  75. auto alt = image_element.alt();
  76. if (alt.is_empty())
  77. alt = image_element.src();
  78. context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  79. } else if (auto bitmap = m_image_loader.bitmap(m_image_loader.current_frame_index())) {
  80. context.painter().draw_scaled_bitmap(rounded_int_rect(absolute_rect()), *bitmap, bitmap->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
  81. }
  82. }
  83. }
  84. bool ImageBox::renders_as_alt_text() const
  85. {
  86. if (is<HTML::HTMLImageElement>(dom_node()))
  87. return !m_image_loader.has_image();
  88. return false;
  89. }
  90. void ImageBox::browsing_context_did_set_viewport_rect(Gfx::IntRect const& viewport_rect)
  91. {
  92. m_image_loader.set_visible_in_viewport(viewport_rect.to_type<float>().intersects(absolute_rect()));
  93. }
  94. }