ImagePaintable.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/CSS/StyleValues/EdgeStyleValue.h>
  8. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  10. #include <LibWeb/HTML/DecodedImageData.h>
  11. #include <LibWeb/HTML/HTMLImageElement.h>
  12. #include <LibWeb/HTML/ImageRequest.h>
  13. #include <LibWeb/Layout/ImageBox.h>
  14. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  15. #include <LibWeb/Painting/ImagePaintable.h>
  16. #include <LibWeb/Platform/FontPlugin.h>
  17. namespace Web::Painting {
  18. JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
  19. {
  20. auto alt = layout_box.dom_node().get_attribute_value(HTML::AttributeNames::alt);
  21. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box, move(alt));
  22. }
  23. ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box, String alt_text)
  24. : PaintableBox(layout_box)
  25. , m_renders_as_alt_text(layout_box.renders_as_alt_text())
  26. , m_alt_text(move(alt_text))
  27. , m_image_provider(layout_box.image_provider())
  28. {
  29. const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
  30. }
  31. void ImagePaintable::visit_edges(JS::Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_image_provider.to_html_element());
  35. }
  36. void ImagePaintable::finalize()
  37. {
  38. Base::finalize();
  39. // NOTE: We unregister from the document in finalize() to avoid trouble
  40. // in the scenario where our Document has already been swept by GC.
  41. document().unregister_viewport_client(*this);
  42. }
  43. Layout::ImageBox const& ImagePaintable::layout_box() const
  44. {
  45. return static_cast<Layout::ImageBox const&>(layout_node());
  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.recording_painter().paint_frame(enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
  57. context.recording_painter().draw_text(enclosing_rect, m_alt_text, Platform::FontPlugin::the().default_font(), Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
  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. switch (computed_values().object_fit()) {
  69. case CSS::ObjectFit::Fill:
  70. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  71. scale_y = (float)image_int_rect.height() / bitmap_rect.height();
  72. break;
  73. case CSS::ObjectFit::Contain:
  74. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  75. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  76. scale_y = scale_x;
  77. } else {
  78. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  79. scale_y = scale_x;
  80. }
  81. break;
  82. case CSS::ObjectFit::Cover:
  83. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  84. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  85. scale_y = scale_x;
  86. bitmap_intersect.set_height(bitmap_rect.width() * image_aspect_ratio);
  87. } else {
  88. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  89. scale_y = scale_x;
  90. bitmap_intersect.set_width(bitmap_rect.height() / image_aspect_ratio);
  91. }
  92. break;
  93. case CSS::ObjectFit::ScaleDown:
  94. // FIXME: Implement
  95. case CSS::ObjectFit::None:
  96. scale_x = 1;
  97. scale_y = 1;
  98. bitmap_intersect.set_size(image_int_rect.size());
  99. }
  100. auto scaled_bitmap_width = bitmap_rect.width() * scale_x;
  101. auto scaled_bitmap_height = bitmap_rect.height() * scale_y;
  102. auto residual_horizontal = CSSPixels::nearest_value_for(image_int_rect.width() - scaled_bitmap_width);
  103. auto residual_vertical = CSSPixels::nearest_value_for(image_int_rect.height() - scaled_bitmap_height);
  104. bitmap_intersect.set_x((bitmap_rect.width() - bitmap_intersect.width()) / 2);
  105. bitmap_intersect.set_y((bitmap_rect.height() - bitmap_intersect.height()) / 2);
  106. auto const& object_position = computed_values().object_position();
  107. auto offset_x = 0;
  108. if (object_position.edge_x == CSS::PositionEdge::Left) {
  109. offset_x = object_position.offset_x.to_px(layout_node(), residual_horizontal).to_int();
  110. bitmap_intersect.set_x(0);
  111. } else if (object_position.edge_x == CSS::PositionEdge::Right) {
  112. offset_x = residual_horizontal.to_int() - object_position.offset_x.to_px(layout_node(), residual_horizontal).to_int();
  113. }
  114. if (image_int_rect.width() < scaled_bitmap_width)
  115. bitmap_intersect.set_x(-(offset_x / scale_x));
  116. auto offset_y = 0;
  117. if (object_position.edge_y == CSS::PositionEdge::Top) {
  118. offset_y = object_position.offset_y.to_px(layout_node(), residual_vertical).to_int();
  119. bitmap_intersect.set_y(0);
  120. } else if (object_position.edge_y == CSS::PositionEdge::Bottom) {
  121. offset_y = residual_vertical.to_int() - object_position.offset_y.to_px(layout_node(), residual_vertical).to_int();
  122. }
  123. if (image_int_rect.height() < scaled_bitmap_height)
  124. bitmap_intersect.set_y(-(offset_y / scale_y));
  125. Gfx::IntRect draw_rect = {
  126. image_int_rect.x() + offset_x,
  127. image_int_rect.y() + offset_y,
  128. (int)scaled_bitmap_width,
  129. (int)scaled_bitmap_height
  130. };
  131. context.recording_painter().draw_scaled_immutable_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), scaling_mode);
  132. }
  133. }
  134. }
  135. void ImagePaintable::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  136. {
  137. const_cast<Layout::ImageProvider&>(m_image_provider).set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
  138. }
  139. }