ImageBox.cpp 3.8 KB

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