ImagePaintable.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/BorderRadiusCornerClipper.h>
  10. #include <LibWeb/Painting/ImagePaintable.h>
  11. #include <LibWeb/Platform/FontPlugin.h>
  12. namespace Web::Painting {
  13. JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
  14. {
  15. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box);
  16. }
  17. ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
  18. : PaintableBox(layout_box)
  19. {
  20. }
  21. Layout::ImageBox const& ImagePaintable::layout_box() const
  22. {
  23. return static_cast<Layout::ImageBox const&>(layout_node());
  24. }
  25. void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
  26. {
  27. if (!is_visible())
  28. return;
  29. // FIXME: This should be done at a different level.
  30. if (is_out_of_view(context))
  31. return;
  32. PaintableBox::paint(context, phase);
  33. if (phase == PaintPhase::Foreground) {
  34. if (layout_box().renders_as_alt_text()) {
  35. auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node());
  36. auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  37. context.painter().set_font(Platform::FontPlugin::the().default_font());
  38. Gfx::StylePainter::paint_frame(context.painter(), enclosing_rect, context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  39. auto alt = image_element.alt();
  40. if (alt.is_empty())
  41. alt = image_element.src();
  42. context.painter().draw_text(enclosing_rect, alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  43. } else if (auto bitmap = layout_box().image_loader().bitmap(layout_box().image_loader().current_frame_index())) {
  44. auto image_rect = context.rounded_device_rect(absolute_rect());
  45. ScopedCornerRadiusClip corner_clip { context, context.painter(), image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  46. context.painter().draw_scaled_bitmap(image_rect.to_type<int>(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering()));
  47. }
  48. }
  49. }
  50. }