BackgroundPainting.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGfx/AntiAliasingPainter.h>
  9. #include <LibGfx/Painter.h>
  10. #include <LibWeb/Layout/Node.h>
  11. #include <LibWeb/Layout/Viewport.h>
  12. #include <LibWeb/Painting/BackgroundPainting.h>
  13. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  14. #include <LibWeb/Painting/GradientPainting.h>
  15. #include <LibWeb/Painting/PaintContext.h>
  16. namespace Web::Painting {
  17. // https://www.w3.org/TR/css-backgrounds-3/#backgrounds
  18. void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMetrics const& layout_node, CSSPixelRect const& border_rect, Color background_color, CSS::ImageRendering image_rendering, Vector<CSS::BackgroundLayerData> const* background_layers, BorderRadiiData const& border_radii)
  19. {
  20. auto& painter = context.painter();
  21. struct BackgroundBox {
  22. CSSPixelRect rect;
  23. BorderRadiiData radii;
  24. inline void shrink(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  25. {
  26. rect.shrink(top, right, bottom, left);
  27. radii.shrink(top.value(), right.value(), bottom.value(), left.value());
  28. }
  29. };
  30. BackgroundBox border_box {
  31. border_rect,
  32. border_radii
  33. };
  34. auto get_box = [&](CSS::BackgroundBox box_clip) {
  35. auto box = border_box;
  36. switch (box_clip) {
  37. case CSS::BackgroundBox::ContentBox: {
  38. auto& padding = layout_node.box_model().padding;
  39. box.shrink(padding.top, padding.right, padding.bottom, padding.left);
  40. [[fallthrough]];
  41. }
  42. case CSS::BackgroundBox::PaddingBox: {
  43. auto& border = layout_node.box_model().border;
  44. box.shrink(border.top, border.right, border.bottom, border.left);
  45. [[fallthrough]];
  46. }
  47. case CSS::BackgroundBox::BorderBox:
  48. default:
  49. return box;
  50. }
  51. };
  52. auto color_box = border_box;
  53. if (background_layers && !background_layers->is_empty())
  54. color_box = get_box(background_layers->last().clip);
  55. auto layer_is_paintable = [&](auto& layer) {
  56. return layer.background_image && layer.background_image->is_paintable();
  57. };
  58. bool has_paintable_layers = false;
  59. if (background_layers) {
  60. for (auto& layer : *background_layers) {
  61. if (layer_is_paintable(layer)) {
  62. has_paintable_layers = true;
  63. break;
  64. }
  65. }
  66. }
  67. Gfx::AntiAliasingPainter aa_painter { painter };
  68. aa_painter.fill_rect_with_rounded_corners(context.rounded_device_rect(color_box.rect).to_type<int>(),
  69. background_color, color_box.radii.top_left.as_corner(context), color_box.radii.top_right.as_corner(context), color_box.radii.bottom_right.as_corner(context), color_box.radii.bottom_left.as_corner(context));
  70. if (!has_paintable_layers)
  71. return;
  72. struct {
  73. int top { 0 };
  74. int bottom { 0 };
  75. int left { 0 };
  76. int right { 0 };
  77. } clip_shrink;
  78. auto border_top = layout_node.computed_values().border_top();
  79. auto border_bottom = layout_node.computed_values().border_bottom();
  80. auto border_left = layout_node.computed_values().border_left();
  81. auto border_right = layout_node.computed_values().border_right();
  82. if (border_top.color.alpha() == 255 && border_bottom.color.alpha() == 255
  83. && border_left.color.alpha() == 255 && border_right.color.alpha() == 255) {
  84. clip_shrink.top = border_top.width;
  85. clip_shrink.bottom = border_bottom.width;
  86. clip_shrink.left = border_left.width;
  87. clip_shrink.right = border_right.width;
  88. }
  89. // Note: Background layers are ordered front-to-back, so we paint them in reverse
  90. for (auto& layer : background_layers->in_reverse()) {
  91. if (!layer_is_paintable(layer))
  92. continue;
  93. Gfx::PainterStateSaver state { painter };
  94. // Clip
  95. auto clip_box = get_box(layer.clip);
  96. CSSPixelRect const& css_clip_rect = clip_box.rect;
  97. auto clip_rect = context.rounded_device_rect(css_clip_rect);
  98. painter.add_clip_rect(clip_rect.to_type<int>());
  99. ScopedCornerRadiusClip corner_clip { context, painter, clip_rect, clip_box.radii };
  100. if (layer.clip == CSS::BackgroundBox::BorderBox) {
  101. // Shrink the effective clip rect if to account for the bits the borders will definitely paint over
  102. // (if they all have alpha == 255).
  103. clip_rect.shrink(clip_shrink.top, clip_shrink.right, clip_shrink.bottom, clip_shrink.left);
  104. }
  105. auto& image = *layer.background_image;
  106. CSSPixelRect background_positioning_area;
  107. // Attachment and Origin
  108. switch (layer.attachment) {
  109. case CSS::BackgroundAttachment::Fixed:
  110. background_positioning_area = layout_node.root().browsing_context().viewport_rect();
  111. break;
  112. case CSS::BackgroundAttachment::Local:
  113. case CSS::BackgroundAttachment::Scroll:
  114. background_positioning_area = get_box(layer.origin).rect;
  115. break;
  116. }
  117. // FIXME: Implement proper default sizing algorithm: https://drafts.csswg.org/css-images/#default-sizing
  118. CSSPixels natural_image_width = image.natural_width().value_or(background_positioning_area.width().value());
  119. CSSPixels natural_image_height = image.natural_height().value_or(background_positioning_area.height().value());
  120. // If any of these are zero, the NaNs will pop up in the painting code.
  121. if (background_positioning_area.is_empty() || natural_image_height <= 0 || natural_image_width <= 0)
  122. continue;
  123. // Size
  124. CSSPixelRect image_rect;
  125. switch (layer.size_type) {
  126. case CSS::BackgroundSize::Contain: {
  127. float max_width_ratio = (background_positioning_area.width() / natural_image_width).value();
  128. float max_height_ratio = (background_positioning_area.height() / natural_image_height).value();
  129. float ratio = min(max_width_ratio, max_height_ratio);
  130. image_rect.set_size(natural_image_width * ratio, natural_image_height * ratio);
  131. break;
  132. }
  133. case CSS::BackgroundSize::Cover: {
  134. float max_width_ratio = (background_positioning_area.width() / natural_image_width).value();
  135. float max_height_ratio = (background_positioning_area.height() / natural_image_height).value();
  136. float ratio = max(max_width_ratio, max_height_ratio);
  137. image_rect.set_size(natural_image_width * ratio, natural_image_height * ratio);
  138. break;
  139. }
  140. case CSS::BackgroundSize::LengthPercentage: {
  141. CSSPixels width;
  142. CSSPixels height;
  143. bool x_is_auto = layer.size_x.is_auto();
  144. bool y_is_auto = layer.size_y.is_auto();
  145. if (x_is_auto && y_is_auto) {
  146. width = natural_image_width;
  147. height = natural_image_height;
  148. } else if (x_is_auto) {
  149. height = layer.size_y.resolved(layout_node, CSS::Length::make_px(background_positioning_area.height())).to_px(layout_node);
  150. width = natural_image_width * (height / natural_image_height);
  151. } else if (y_is_auto) {
  152. width = layer.size_x.resolved(layout_node, CSS::Length::make_px(background_positioning_area.width())).to_px(layout_node);
  153. height = natural_image_height * (width / natural_image_width);
  154. } else {
  155. width = layer.size_x.resolved(layout_node, CSS::Length::make_px(background_positioning_area.width())).to_px(layout_node);
  156. height = layer.size_y.resolved(layout_node, CSS::Length::make_px(background_positioning_area.height())).to_px(layout_node);
  157. }
  158. image_rect.set_size(width, height);
  159. break;
  160. }
  161. }
  162. // If after sizing we have a 0px image, we're done. Attempting to paint this would be an infinite loop.
  163. if (image_rect.is_empty())
  164. continue;
  165. // If background-repeat is round for one (or both) dimensions, there is a second step.
  166. // The UA must scale the image in that dimension (or both dimensions) so that it fits a
  167. // whole number of times in the background positioning area.
  168. if (layer.repeat_x == CSS::Repeat::Round || layer.repeat_y == CSS::Repeat::Round) {
  169. // If X ≠ 0 is the width of the image after step one and W is the width of the
  170. // background positioning area, then the rounded width X' = W / round(W / X)
  171. // where round() is a function that returns the nearest natural number
  172. // (integer greater than zero).
  173. if (layer.repeat_x == CSS::Repeat::Round) {
  174. image_rect.set_width(background_positioning_area.width() / round(background_positioning_area.width() / image_rect.width()));
  175. }
  176. if (layer.repeat_y == CSS::Repeat::Round) {
  177. image_rect.set_height(background_positioning_area.height() / round(background_positioning_area.height() / image_rect.height()));
  178. }
  179. // If background-repeat is round for one dimension only and if background-size is auto
  180. // for the other dimension, then there is a third step: that other dimension is scaled
  181. // so that the original aspect ratio is restored.
  182. if (layer.repeat_x != layer.repeat_y) {
  183. if (layer.size_x.is_auto()) {
  184. image_rect.set_width(natural_image_width * (image_rect.height() / natural_image_height));
  185. }
  186. if (layer.size_y.is_auto()) {
  187. image_rect.set_height(natural_image_height * (image_rect.width() / natural_image_width));
  188. }
  189. }
  190. }
  191. CSSPixels space_x = background_positioning_area.width() - image_rect.width();
  192. CSSPixels space_y = background_positioning_area.height() - image_rect.height();
  193. // Position
  194. CSSPixels offset_x = layer.position_offset_x.resolved(layout_node, CSS::Length::make_px(space_x)).to_px(layout_node);
  195. if (layer.position_edge_x == CSS::PositionEdge::Right) {
  196. image_rect.set_right_without_resize(background_positioning_area.right() - offset_x);
  197. } else {
  198. image_rect.set_left(background_positioning_area.left() + offset_x);
  199. }
  200. CSSPixels offset_y = layer.position_offset_y.resolved(layout_node, CSS::Length::make_px(space_y)).to_px(layout_node);
  201. if (layer.position_edge_y == CSS::PositionEdge::Bottom) {
  202. image_rect.set_bottom_without_resize(background_positioning_area.bottom() - offset_y);
  203. } else {
  204. image_rect.set_top(background_positioning_area.top() + offset_y);
  205. }
  206. // Repetition
  207. bool repeat_x = false;
  208. bool repeat_y = false;
  209. CSSPixels x_step = 0;
  210. CSSPixels y_step = 0;
  211. switch (layer.repeat_x) {
  212. case CSS::Repeat::Round:
  213. x_step = image_rect.width();
  214. repeat_x = true;
  215. break;
  216. case CSS::Repeat::Space: {
  217. int whole_images = (background_positioning_area.width() / image_rect.width()).value();
  218. if (whole_images <= 1) {
  219. x_step = image_rect.width();
  220. repeat_x = false;
  221. } else {
  222. auto space = fmod(background_positioning_area.width(), image_rect.width());
  223. x_step = image_rect.width() + (space / (float)(whole_images - 1));
  224. repeat_x = true;
  225. }
  226. break;
  227. }
  228. case CSS::Repeat::Repeat:
  229. x_step = image_rect.width();
  230. repeat_x = true;
  231. break;
  232. case CSS::Repeat::NoRepeat:
  233. repeat_x = false;
  234. break;
  235. }
  236. // Move image_rect to the left-most tile position that is still visible
  237. if (repeat_x && image_rect.x() > css_clip_rect.x()) {
  238. auto x_delta = floor(x_step * ceil((image_rect.x() - css_clip_rect.x()) / x_step));
  239. image_rect.set_x(image_rect.x() - x_delta);
  240. }
  241. switch (layer.repeat_y) {
  242. case CSS::Repeat::Round:
  243. y_step = image_rect.height();
  244. repeat_y = true;
  245. break;
  246. case CSS::Repeat::Space: {
  247. int whole_images = (background_positioning_area.height() / image_rect.height()).value();
  248. if (whole_images <= 1) {
  249. y_step = image_rect.height();
  250. repeat_y = false;
  251. } else {
  252. auto space = fmod(background_positioning_area.height(), image_rect.height());
  253. y_step = image_rect.height() + ((float)space / (float)(whole_images - 1));
  254. repeat_y = true;
  255. }
  256. break;
  257. }
  258. case CSS::Repeat::Repeat:
  259. y_step = image_rect.height();
  260. repeat_y = true;
  261. break;
  262. case CSS::Repeat::NoRepeat:
  263. repeat_y = false;
  264. break;
  265. }
  266. // Move image_rect to the top-most tile position that is still visible
  267. if (repeat_y && image_rect.y() > css_clip_rect.y()) {
  268. auto y_delta = floor(y_step * ceil((image_rect.y() - css_clip_rect.y()) / y_step));
  269. image_rect.set_y(image_rect.y() - y_delta);
  270. }
  271. CSSPixels initial_image_x = image_rect.x();
  272. CSSPixels image_y = image_rect.y();
  273. Optional<DevicePixelRect> last_image_device_rect;
  274. image.resolve_for_size(layout_node, image_rect.size());
  275. while (image_y <= css_clip_rect.bottom()) {
  276. image_rect.set_y(image_y);
  277. auto image_x = initial_image_x;
  278. while (image_x <= css_clip_rect.right()) {
  279. image_rect.set_x(image_x);
  280. auto image_device_rect = context.rounded_device_rect(image_rect);
  281. if (image_device_rect != last_image_device_rect && image_device_rect.intersects(context.device_viewport_rect()))
  282. image.paint(context, image_device_rect, image_rendering);
  283. last_image_device_rect = image_device_rect;
  284. if (!repeat_x)
  285. break;
  286. image_x += x_step;
  287. }
  288. if (!repeat_y)
  289. break;
  290. image_y += y_step;
  291. }
  292. }
  293. }
  294. }