ImagePaintable.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/StylePainter.h>
  7. #include <LibWeb/HTML/DecodedImageData.h>
  8. #include <LibWeb/HTML/HTMLImageElement.h>
  9. #include <LibWeb/HTML/ImageRequest.h>
  10. #include <LibWeb/Layout/ImageBox.h>
  11. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  12. #include <LibWeb/Painting/ImagePaintable.h>
  13. #include <LibWeb/Platform/FontPlugin.h>
  14. namespace Web::Painting {
  15. JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
  16. {
  17. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box);
  18. }
  19. ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
  20. : PaintableBox(layout_box)
  21. {
  22. browsing_context().register_viewport_client(*this);
  23. }
  24. void ImagePaintable::finalize()
  25. {
  26. Base::finalize();
  27. // NOTE: We unregister from the browsing context in finalize() to avoid trouble
  28. // in the scenario where our BrowsingContext has already been swept by GC.
  29. browsing_context().unregister_viewport_client(*this);
  30. }
  31. Layout::ImageBox const& ImagePaintable::layout_box() const
  32. {
  33. return static_cast<Layout::ImageBox const&>(layout_node());
  34. }
  35. void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
  36. {
  37. if (!is_visible())
  38. return;
  39. // FIXME: This should be done at a different level.
  40. if (is_out_of_view(context))
  41. return;
  42. PaintableBox::paint(context, phase);
  43. if (phase == PaintPhase::Foreground) {
  44. if (layout_box().renders_as_alt_text()) {
  45. auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node());
  46. auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  47. context.painter().set_font(Platform::FontPlugin::the().default_font());
  48. Gfx::StylePainter::paint_frame(context.painter(), enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
  49. auto alt = image_element.alt();
  50. if (alt.is_empty())
  51. alt = image_element.src();
  52. context.painter().draw_text(enclosing_rect, alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  53. } else if (auto bitmap = layout_box().image_provider().current_image_bitmap()) {
  54. auto image_rect = context.rounded_device_rect(absolute_rect());
  55. ScopedCornerRadiusClip corner_clip { context, context.painter(), image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  56. context.painter().draw_scaled_bitmap(image_rect.to_type<int>(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering()));
  57. }
  58. }
  59. }
  60. void ImagePaintable::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  61. {
  62. const_cast<Layout::ImageProvider&>(layout_box().image_provider()).set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
  63. }
  64. }