LayoutImage.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(const Element& element, NonnullRefPtr<StyleProperties> style, const ImageLoader& image_loader)
  33. : LayoutReplaced(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 (preferred_width() && preferred_height()) {
  51. set_has_intrinsic_width(true);
  52. set_has_intrinsic_height(true);
  53. set_intrinsic_width(preferred_width());
  54. set_intrinsic_height(preferred_height());
  55. } else if (renders_as_alt_text()) {
  56. auto& image_element = to<HTMLImageElement>(node());
  57. auto& font = Gfx::Font::default_font();
  58. auto alt = image_element.alt();
  59. if (alt.is_empty())
  60. alt = image_element.src();
  61. set_width(font.width(alt) + 16);
  62. set_height(font.glyph_height() + 16);
  63. } else {
  64. set_width(16);
  65. set_height(16);
  66. }
  67. LayoutReplaced::layout(layout_mode);
  68. }
  69. void LayoutImage::paint(PaintContext& context, PaintPhase phase)
  70. {
  71. if (!is_visible())
  72. return;
  73. // FIXME: This should be done at a different level. Also rect() does not include padding etc!
  74. if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect())))
  75. return;
  76. LayoutReplaced::paint(context, phase);
  77. if (phase == PaintPhase::Foreground) {
  78. if (renders_as_alt_text()) {
  79. auto& image_element = to<HTMLImageElement>(node());
  80. context.painter().set_font(Gfx::Font::default_font());
  81. Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  82. auto alt = image_element.alt();
  83. if (alt.is_empty())
  84. alt = image_element.src();
  85. 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);
  86. } else if (auto* bitmap = m_image_loader.bitmap()) {
  87. context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *bitmap, bitmap->rect());
  88. }
  89. }
  90. }
  91. bool LayoutImage::renders_as_alt_text() const
  92. {
  93. if (is<HTMLImageElement>(node()))
  94. return !m_image_loader.has_image();
  95. return false;
  96. }
  97. void LayoutImage::set_visible_in_viewport(Badge<LayoutDocument>, bool visible_in_viewport)
  98. {
  99. m_image_loader.set_visible_in_viewport(visible_in_viewport);
  100. }
  101. }