LayoutImage.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/ImageDecoder.h>
  29. #include <LibGfx/StylePainter.h>
  30. #include <LibWeb/Layout/LayoutImage.h>
  31. namespace Web {
  32. LayoutImage::LayoutImage(DOM::Document& document, const DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, const ImageLoader& image_loader)
  33. : LayoutReplaced(document, element, move(style))
  34. , m_image_loader(image_loader)
  35. {
  36. }
  37. LayoutImage::~LayoutImage()
  38. {
  39. }
  40. int LayoutImage::preferred_width() const
  41. {
  42. return node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
  43. }
  44. int LayoutImage::preferred_height() const
  45. {
  46. return node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height());
  47. }
  48. void LayoutImage::layout(LayoutMode layout_mode)
  49. {
  50. if (m_image_loader.width()) {
  51. set_has_intrinsic_width(true);
  52. set_intrinsic_width(m_image_loader.width());
  53. }
  54. if (m_image_loader.height()) {
  55. set_has_intrinsic_height(true);
  56. set_intrinsic_height(m_image_loader.height());
  57. }
  58. if (m_image_loader.width() && m_image_loader.height()) {
  59. set_has_intrinsic_ratio(true);
  60. set_intrinsic_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
  61. } else {
  62. set_has_intrinsic_ratio(false);
  63. }
  64. if (renders_as_alt_text()) {
  65. auto& image_element = downcast<HTMLImageElement>(node());
  66. auto& font = Gfx::Font::default_font();
  67. auto alt = image_element.alt();
  68. if (alt.is_empty())
  69. alt = image_element.src();
  70. set_width(font.width(alt) + 16);
  71. set_height(font.glyph_height() + 16);
  72. }
  73. if (!has_intrinsic_width() && !has_intrinsic_height()) {
  74. set_width(16);
  75. set_height(16);
  76. }
  77. LayoutReplaced::layout(layout_mode);
  78. }
  79. void LayoutImage::paint(PaintContext& context, PaintPhase phase)
  80. {
  81. if (!is_visible())
  82. return;
  83. // FIXME: This should be done at a different level. Also rect() does not include padding etc!
  84. if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect())))
  85. return;
  86. LayoutReplaced::paint(context, phase);
  87. if (phase == PaintPhase::Foreground) {
  88. if (renders_as_alt_text()) {
  89. auto& image_element = downcast<HTMLImageElement>(node());
  90. context.painter().set_font(Gfx::Font::default_font());
  91. Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  92. auto alt = image_element.alt();
  93. if (alt.is_empty())
  94. alt = image_element.src();
  95. context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, specified_style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), Gfx::TextElision::Right);
  96. } else if (auto* bitmap = m_image_loader.bitmap()) {
  97. context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *bitmap, bitmap->rect());
  98. }
  99. }
  100. }
  101. bool LayoutImage::renders_as_alt_text() const
  102. {
  103. if (is<HTMLImageElement>(node()))
  104. return !m_image_loader.has_image();
  105. return false;
  106. }
  107. void LayoutImage::set_visible_in_viewport(Badge<LayoutDocument>, bool visible_in_viewport)
  108. {
  109. m_image_loader.set_visible_in_viewport(visible_in_viewport);
  110. }
  111. }