ImageBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Painter.h>
  27. #include <LibGfx/Font.h>
  28. #include <LibGfx/FontDatabase.h>
  29. #include <LibGfx/ImageDecoder.h>
  30. #include <LibGfx/StylePainter.h>
  31. #include <LibWeb/Layout/ImageBox.h>
  32. namespace Web::Layout {
  33. ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, const ImageLoader& image_loader)
  34. : ReplacedBox(document, element, move(style))
  35. , m_image_loader(image_loader)
  36. {
  37. }
  38. ImageBox::~ImageBox()
  39. {
  40. }
  41. int ImageBox::preferred_width() const
  42. {
  43. return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
  44. }
  45. int ImageBox::preferred_height() const
  46. {
  47. return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height());
  48. }
  49. void ImageBox::prepare_for_replaced_layout()
  50. {
  51. if (!m_image_loader.has_loaded_or_failed()) {
  52. set_has_intrinsic_width(true);
  53. set_has_intrinsic_height(true);
  54. set_intrinsic_width(0);
  55. set_intrinsic_height(0);
  56. } else {
  57. if (m_image_loader.width()) {
  58. set_has_intrinsic_width(true);
  59. set_intrinsic_width(m_image_loader.width());
  60. }
  61. if (m_image_loader.height()) {
  62. set_has_intrinsic_height(true);
  63. set_intrinsic_height(m_image_loader.height());
  64. }
  65. if (m_image_loader.width() && m_image_loader.height()) {
  66. set_has_intrinsic_ratio(true);
  67. set_intrinsic_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
  68. } else {
  69. set_has_intrinsic_ratio(false);
  70. }
  71. }
  72. if (renders_as_alt_text()) {
  73. auto& image_element = downcast<HTML::HTMLImageElement>(dom_node());
  74. auto& font = Gfx::FontDatabase::default_font();
  75. auto alt = image_element.alt();
  76. if (alt.is_empty())
  77. alt = image_element.src();
  78. set_width(font.width(alt) + 16);
  79. set_height(font.glyph_height() + 16);
  80. }
  81. if (!has_intrinsic_width() && !has_intrinsic_height()) {
  82. set_width(16);
  83. set_height(16);
  84. }
  85. }
  86. void ImageBox::paint(PaintContext& context, PaintPhase phase)
  87. {
  88. if (!is_visible())
  89. return;
  90. // FIXME: This should be done at a different level. Also rect() does not include padding etc!
  91. if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect())))
  92. return;
  93. ReplacedBox::paint(context, phase);
  94. if (phase == PaintPhase::Foreground) {
  95. if (renders_as_alt_text()) {
  96. auto& image_element = downcast<HTML::HTMLImageElement>(dom_node());
  97. context.painter().set_font(Gfx::FontDatabase::default_font());
  98. Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  99. auto alt = image_element.alt();
  100. if (alt.is_empty())
  101. alt = image_element.src();
  102. context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  103. } else if (auto bitmap = m_image_loader.bitmap(m_image_loader.current_frame_index())) {
  104. context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *bitmap, bitmap->rect());
  105. }
  106. }
  107. }
  108. bool ImageBox::renders_as_alt_text() const
  109. {
  110. if (is<HTML::HTMLImageElement>(dom_node()))
  111. return !m_image_loader.has_image();
  112. return false;
  113. }
  114. void ImageBox::set_visible_in_viewport(Badge<Layout::InitialContainingBlockBox>, bool visible_in_viewport)
  115. {
  116. m_image_loader.set_visible_in_viewport(visible_in_viewport);
  117. }
  118. }