ImagePaintable.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
  23. }
  24. void ImagePaintable::finalize()
  25. {
  26. Base::finalize();
  27. // NOTE: We unregister from the document in finalize() to avoid trouble
  28. // in the scenario where our Document has already been swept by GC.
  29. document().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. PaintableBox::paint(context, phase);
  40. if (phase == PaintPhase::Foreground) {
  41. auto image_rect = context.rounded_device_rect(absolute_rect());
  42. if (layout_box().renders_as_alt_text()) {
  43. auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node());
  44. auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  45. context.painter().set_font(Platform::FontPlugin::the().default_font());
  46. context.painter().paint_frame(enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
  47. auto alt = image_element.alt();
  48. if (alt.is_empty())
  49. alt = image_element.src();
  50. context.painter().draw_text(enclosing_rect, alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  51. } else if (auto bitmap = layout_box().image_provider().current_image_bitmap(image_rect.size().to_type<int>())) {
  52. ScopedCornerRadiusClip corner_clip { context, image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  53. auto image_int_rect = image_rect.to_type<int>();
  54. auto bitmap_rect = bitmap->rect();
  55. auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), bitmap_rect, image_int_rect);
  56. auto& dom_element = verify_cast<DOM::Element>(*dom_node());
  57. auto object_fit = dom_element.computed_css_values()->object_fit();
  58. auto bitmap_aspect_ratio = bitmap_rect.height() / bitmap_rect.width();
  59. auto image_aspect_ratio = (float)image_rect.height().value() / image_rect.width().value();
  60. auto scale_x = 0.0f;
  61. auto scale_y = 0.0f;
  62. Gfx::IntRect bitmap_intersect = bitmap_rect;
  63. auto object_fit_value = CSS::InitialValues::object_fit();
  64. if (object_fit.has_value())
  65. object_fit_value = object_fit.value();
  66. switch (object_fit_value) {
  67. case CSS::ObjectFit::Fill:
  68. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  69. scale_y = (float)image_int_rect.height() / bitmap_rect.height();
  70. bitmap_intersect = bitmap_rect;
  71. break;
  72. case CSS::ObjectFit::Contain:
  73. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  74. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  75. scale_y = scale_x;
  76. } else {
  77. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  78. scale_y = scale_x;
  79. }
  80. break;
  81. case CSS::ObjectFit::Cover:
  82. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  83. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  84. scale_y = scale_x;
  85. bitmap_intersect.set_height(bitmap_rect.width() * image_aspect_ratio);
  86. } else {
  87. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  88. scale_y = scale_x;
  89. bitmap_intersect.set_width(bitmap_rect.height() / image_aspect_ratio);
  90. }
  91. break;
  92. case CSS::ObjectFit::ScaleDown:
  93. // FIXME: Implement
  94. case CSS::ObjectFit::None:
  95. scale_x = 1;
  96. scale_y = 1;
  97. bitmap_intersect.set_size(image_int_rect.size());
  98. }
  99. bitmap_intersect.set_x((bitmap_rect.width() - bitmap_intersect.width()) / 2);
  100. bitmap_intersect.set_y((bitmap_rect.height() - bitmap_intersect.height()) / 2);
  101. auto offset_x = (image_int_rect.width() - bitmap_rect.width() * scale_x) / 2;
  102. auto offset_y = (image_int_rect.height() - bitmap_rect.height() * scale_y) / 2;
  103. Gfx::IntRect draw_rect = {
  104. image_int_rect.x() + offset_x,
  105. image_int_rect.y() + offset_y,
  106. bitmap_rect.width() * scale_x,
  107. bitmap_rect.height() * scale_y
  108. };
  109. context.painter().draw_scaled_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), 1.f, scaling_mode);
  110. }
  111. }
  112. }
  113. void ImagePaintable::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  114. {
  115. const_cast<Layout::ImageProvider&>(layout_box().image_provider()).set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
  116. }
  117. }