ImagePaintable.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  7. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  8. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  9. #include <LibWeb/HTML/DecodedImageData.h>
  10. #include <LibWeb/HTML/HTMLImageElement.h>
  11. #include <LibWeb/HTML/ImageRequest.h>
  12. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  13. #include <LibWeb/Painting/ImagePaintable.h>
  14. #include <LibWeb/Platform/FontPlugin.h>
  15. namespace Web::Painting {
  16. JS_DEFINE_ALLOCATOR(ImagePaintable);
  17. JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::SVGImageBox const& layout_box)
  18. {
  19. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box, layout_box.dom_node(), false, String {}, true);
  20. }
  21. JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
  22. {
  23. auto alt = layout_box.dom_node().get_attribute_value(HTML::AttributeNames::alt);
  24. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box, layout_box.image_provider(), layout_box.renders_as_alt_text(), move(alt), false);
  25. }
  26. ImagePaintable::ImagePaintable(Layout::Box const& layout_box, Layout::ImageProvider const& image_provider, bool renders_as_alt_text, String alt_text, bool is_svg_image)
  27. : PaintableBox(layout_box)
  28. , m_renders_as_alt_text(renders_as_alt_text)
  29. , m_alt_text(move(alt_text))
  30. , m_image_provider(image_provider)
  31. , m_is_svg_image(is_svg_image)
  32. {
  33. const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
  34. }
  35. void ImagePaintable::visit_edges(JS::Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. visitor.visit(m_image_provider.to_html_element());
  39. }
  40. void ImagePaintable::finalize()
  41. {
  42. Base::finalize();
  43. // NOTE: We unregister from the document in finalize() to avoid trouble
  44. // in the scenario where our Document has already been swept by GC.
  45. document().unregister_viewport_client(*this);
  46. }
  47. void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
  48. {
  49. if (!is_visible())
  50. return;
  51. PaintableBox::paint(context, phase);
  52. if (phase == PaintPhase::Foreground) {
  53. auto image_rect = context.rounded_device_rect(absolute_rect());
  54. if (m_renders_as_alt_text) {
  55. auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  56. context.display_list_recorder().draw_rect(enclosing_rect, Gfx::Color::Black);
  57. context.display_list_recorder().draw_text(enclosing_rect, m_alt_text, Platform::FontPlugin::the().default_font(), Gfx::TextAlignment::Center, computed_values().color());
  58. } else if (auto bitmap = m_image_provider.current_image_bitmap(image_rect.size().to_type<int>())) {
  59. ScopedCornerRadiusClip corner_clip { context, image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  60. auto image_int_rect = image_rect.to_type<int>();
  61. auto bitmap_rect = bitmap->rect();
  62. auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), bitmap_rect, image_int_rect);
  63. auto bitmap_aspect_ratio = (float)bitmap_rect.height() / bitmap_rect.width();
  64. auto image_aspect_ratio = (float)image_rect.height().value() / image_rect.width().value();
  65. auto scale_x = 0.0f;
  66. auto scale_y = 0.0f;
  67. Gfx::IntRect bitmap_intersect = bitmap_rect;
  68. auto object_fit = m_is_svg_image ? CSS::ObjectFit::Contain : computed_values().object_fit();
  69. switch (object_fit) {
  70. case CSS::ObjectFit::Fill:
  71. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  72. scale_y = (float)image_int_rect.height() / bitmap_rect.height();
  73. break;
  74. case CSS::ObjectFit::Contain:
  75. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  76. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  77. scale_y = scale_x;
  78. } else {
  79. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  80. scale_y = scale_x;
  81. }
  82. break;
  83. case CSS::ObjectFit::Cover:
  84. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  85. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  86. scale_y = scale_x;
  87. bitmap_intersect.set_height(bitmap_rect.width() * image_aspect_ratio);
  88. } else {
  89. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  90. scale_y = scale_x;
  91. bitmap_intersect.set_width(bitmap_rect.height() / image_aspect_ratio);
  92. }
  93. break;
  94. case CSS::ObjectFit::ScaleDown:
  95. // FIXME: Implement
  96. case CSS::ObjectFit::None:
  97. scale_x = 1;
  98. scale_y = 1;
  99. bitmap_intersect.set_size(image_int_rect.size());
  100. }
  101. auto scaled_bitmap_width = bitmap_rect.width() * scale_x;
  102. auto scaled_bitmap_height = bitmap_rect.height() * scale_y;
  103. auto residual_horizontal = CSSPixels::nearest_value_for(image_int_rect.width() - scaled_bitmap_width);
  104. auto residual_vertical = CSSPixels::nearest_value_for(image_int_rect.height() - scaled_bitmap_height);
  105. bitmap_intersect.set_x((bitmap_rect.width() - bitmap_intersect.width()) / 2);
  106. bitmap_intersect.set_y((bitmap_rect.height() - bitmap_intersect.height()) / 2);
  107. auto const& object_position = computed_values().object_position();
  108. auto offset_x = 0;
  109. if (object_position.edge_x == CSS::PositionEdge::Left) {
  110. offset_x = object_position.offset_x.to_px(layout_node(), residual_horizontal).to_int();
  111. bitmap_intersect.set_x(0);
  112. } else if (object_position.edge_x == CSS::PositionEdge::Right) {
  113. offset_x = residual_horizontal.to_int() - object_position.offset_x.to_px(layout_node(), residual_horizontal).to_int();
  114. }
  115. if (image_int_rect.width() < scaled_bitmap_width)
  116. bitmap_intersect.set_x(-(offset_x / scale_x));
  117. auto offset_y = 0;
  118. if (object_position.edge_y == CSS::PositionEdge::Top) {
  119. offset_y = object_position.offset_y.to_px(layout_node(), residual_vertical).to_int();
  120. bitmap_intersect.set_y(0);
  121. } else if (object_position.edge_y == CSS::PositionEdge::Bottom) {
  122. offset_y = residual_vertical.to_int() - object_position.offset_y.to_px(layout_node(), residual_vertical).to_int();
  123. }
  124. if (image_int_rect.height() < scaled_bitmap_height)
  125. bitmap_intersect.set_y(-(offset_y / scale_y));
  126. Gfx::IntRect draw_rect = {
  127. image_int_rect.x() + offset_x,
  128. image_int_rect.y() + offset_y,
  129. (int)scaled_bitmap_width,
  130. (int)scaled_bitmap_height
  131. };
  132. context.display_list_recorder().draw_scaled_immutable_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), scaling_mode);
  133. }
  134. }
  135. }
  136. void ImagePaintable::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  137. {
  138. const_cast<Layout::ImageProvider&>(m_image_provider).set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
  139. }
  140. }