ImagePaintable.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box);
  21. }
  22. ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
  23. : PaintableBox(layout_box)
  24. {
  25. const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
  26. }
  27. void ImagePaintable::finalize()
  28. {
  29. Base::finalize();
  30. // NOTE: We unregister from the document in finalize() to avoid trouble
  31. // in the scenario where our Document has already been swept by GC.
  32. document().unregister_viewport_client(*this);
  33. }
  34. Layout::ImageBox const& ImagePaintable::layout_box() const
  35. {
  36. return static_cast<Layout::ImageBox const&>(layout_node());
  37. }
  38. void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
  39. {
  40. if (!is_visible())
  41. return;
  42. PaintableBox::paint(context, phase);
  43. if (phase == PaintPhase::Foreground) {
  44. auto image_rect = context.rounded_device_rect(absolute_rect());
  45. if (layout_box().renders_as_alt_text()) {
  46. auto const& image_element = verify_cast<HTML::HTMLElement>(*dom_node());
  47. auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  48. context.recording_painter().paint_frame(enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
  49. auto alt = image_element.get_attribute_value(HTML::AttributeNames::alt);
  50. context.recording_painter().draw_text(enclosing_rect, alt, Platform::FontPlugin::the().default_font(), 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 = (float)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. break;
  71. case CSS::ObjectFit::Contain:
  72. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  73. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  74. scale_y = scale_x;
  75. } else {
  76. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  77. scale_y = scale_x;
  78. }
  79. break;
  80. case CSS::ObjectFit::Cover:
  81. if (bitmap_aspect_ratio >= image_aspect_ratio) {
  82. scale_x = (float)image_int_rect.width() / bitmap_rect.width();
  83. scale_y = scale_x;
  84. bitmap_intersect.set_height(bitmap_rect.width() * image_aspect_ratio);
  85. } else {
  86. scale_x = (float)image_int_rect.height() / bitmap_rect.height();
  87. scale_y = scale_x;
  88. bitmap_intersect.set_width(bitmap_rect.height() / image_aspect_ratio);
  89. }
  90. break;
  91. case CSS::ObjectFit::ScaleDown:
  92. // FIXME: Implement
  93. case CSS::ObjectFit::None:
  94. scale_x = 1;
  95. scale_y = 1;
  96. bitmap_intersect.set_size(image_int_rect.size());
  97. }
  98. auto scaled_bitmap_width = bitmap_rect.width() * scale_x;
  99. auto scaled_bitmap_height = bitmap_rect.height() * scale_y;
  100. auto residual_horizontal = image_int_rect.width() - scaled_bitmap_width;
  101. auto residual_vertical = image_int_rect.height() - scaled_bitmap_height;
  102. bitmap_intersect.set_x((bitmap_rect.width() - bitmap_intersect.width()) / 2);
  103. bitmap_intersect.set_y((bitmap_rect.height() - bitmap_intersect.height()) / 2);
  104. CSS::PositionStyleValue const& object_position = dom_element.computed_css_values()->object_position();
  105. auto offset_x = 0;
  106. auto const& horizontal = object_position.edge_x();
  107. if (horizontal->is_edge()) {
  108. auto const& horizontal_edge = horizontal->as_edge();
  109. auto const& offset = horizontal_edge.offset();
  110. if (horizontal_edge.edge() == CSS::PositionEdge::Left) {
  111. if (offset.is_percentage())
  112. offset_x = (double)(residual_horizontal)*offset.percentage().as_fraction();
  113. else
  114. offset_x = offset.length().to_px(layout_node()).to_int();
  115. bitmap_intersect.set_x(0);
  116. } else if (horizontal_edge.edge() == CSS::PositionEdge::Right) {
  117. if (offset.is_percentage())
  118. offset_x = (double)residual_horizontal - (double)(residual_horizontal)*offset.percentage().as_fraction();
  119. else
  120. offset_x = residual_horizontal - offset.length().to_px(layout_node()).to_int();
  121. }
  122. if (image_int_rect.width() < scaled_bitmap_width)
  123. bitmap_intersect.set_x(-(offset_x / scale_x));
  124. }
  125. auto offset_y = 0;
  126. auto const& vertical = object_position.edge_y();
  127. if (vertical->is_edge()) {
  128. auto const& vertical_edge = vertical->as_edge();
  129. auto const& offset = vertical_edge.offset();
  130. if (vertical_edge.edge() == CSS::PositionEdge::Top) {
  131. if (offset.is_percentage())
  132. offset_y = (double)(residual_vertical)*offset.percentage().as_fraction();
  133. else
  134. offset_y = offset.length().to_px(layout_node()).to_int();
  135. bitmap_intersect.set_y(0);
  136. } else if (vertical_edge.edge() == CSS::PositionEdge::Bottom) {
  137. if (offset.is_percentage())
  138. offset_y = (double)residual_vertical - (double)(residual_vertical)*offset.percentage().as_fraction();
  139. else
  140. offset_y = residual_vertical - offset.length().to_px(layout_node()).to_int();
  141. }
  142. if (image_int_rect.height() < scaled_bitmap_height)
  143. bitmap_intersect.set_y(-(offset_y / scale_y));
  144. }
  145. Gfx::IntRect draw_rect = {
  146. image_int_rect.x() + offset_x,
  147. image_int_rect.y() + offset_y,
  148. (int)scaled_bitmap_width,
  149. (int)scaled_bitmap_height
  150. };
  151. context.recording_painter().draw_scaled_immutable_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), scaling_mode);
  152. }
  153. }
  154. }
  155. void ImagePaintable::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
  156. {
  157. const_cast<Layout::ImageProvider&>(layout_box().image_provider()).set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
  158. }
  159. }