BackgroundPainting.cpp 15 KB

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