ImagePaintable.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/StylePainter.h>
  7. #include <LibWeb/HTML/HTMLImageElement.h>
  8. #include <LibWeb/Layout/ImageBox.h>
  9. #include <LibWeb/Painting/ImagePaintable.h>
  10. namespace Web::Painting {
  11. NonnullRefPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
  12. {
  13. return adopt_ref(*new ImagePaintable(layout_box));
  14. }
  15. ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
  16. : PaintableBox(layout_box)
  17. {
  18. }
  19. Layout::ImageBox const& ImagePaintable::layout_box() const
  20. {
  21. return static_cast<Layout::ImageBox const&>(layout_node());
  22. }
  23. void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
  24. {
  25. if (!is_visible())
  26. return;
  27. // FIXME: This should be done at a different level. Also rect() does not include padding etc!
  28. if (!context.viewport_rect().intersects(enclosing_int_rect(absolute_rect())))
  29. return;
  30. PaintableBox::paint(context, phase);
  31. if (phase == PaintPhase::Foreground) {
  32. if (layout_box().renders_as_alt_text()) {
  33. auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node());
  34. context.painter().set_font(Gfx::FontDatabase::default_font());
  35. Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  36. auto alt = image_element.alt();
  37. if (alt.is_empty())
  38. alt = image_element.src();
  39. context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  40. } else if (auto bitmap = layout_box().image_loader().bitmap(layout_box().image_loader().current_frame_index())) {
  41. context.painter().draw_scaled_bitmap(absolute_rect().to_rounded<int>(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering()));
  42. }
  43. }
  44. }
  45. }