BackgroundPainting.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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/InitialContainingBlock.h>
  11. #include <LibWeb/Layout/Node.h>
  12. #include <LibWeb/Painting/BackgroundPainting.h>
  13. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  14. #include <LibWeb/Painting/PaintContext.h>
  15. namespace Web::Painting {
  16. struct GfxGradient {
  17. Gfx::Orientation orientation;
  18. Gfx::Color color_a;
  19. Gfx::Color color_b;
  20. };
  21. static Optional<GfxGradient> linear_gradient_to_gfx_gradient(CSS::LinearGradientStyleValue const& linear_gradient, Gfx::FloatRect const& background_rect)
  22. {
  23. if (linear_gradient.color_stop_list().size() != 2)
  24. return {};
  25. auto angle = round_to<int>(linear_gradient.angle(background_rect));
  26. auto color_a = linear_gradient.color_stop_list()[0].color_stop.color;
  27. auto color_b = linear_gradient.color_stop_list()[1].color_stop.color;
  28. auto orientation = [&]() -> Optional<Gfx::Orientation> {
  29. switch (angle) {
  30. case 0:
  31. swap(color_a, color_b);
  32. [[fallthrough]];
  33. case 180:
  34. return Gfx::Orientation::Vertical;
  35. case 270:
  36. swap(color_a, color_b);
  37. [[fallthrough]];
  38. case 90:
  39. return Gfx::Orientation::Horizontal;
  40. default:
  41. return {};
  42. }
  43. }();
  44. if (!orientation.has_value())
  45. return {};
  46. return GfxGradient { *orientation, color_a, color_b };
  47. };
  48. // https://www.w3.org/TR/css-backgrounds-3/#backgrounds
  49. void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMetrics const& layout_node, Gfx::FloatRect const& border_rect, Color background_color, Vector<CSS::BackgroundLayerData> const* background_layers, BorderRadiiData const& border_radii)
  50. {
  51. auto& painter = context.painter();
  52. struct BackgroundBox {
  53. Gfx::FloatRect rect;
  54. BorderRadiiData radii;
  55. inline void shrink(float top, float right, float bottom, float left)
  56. {
  57. rect.shrink(top, right, bottom, left);
  58. radii.shrink(top, right, bottom, left);
  59. }
  60. };
  61. BackgroundBox border_box {
  62. border_rect,
  63. border_radii
  64. };
  65. auto get_box = [&](CSS::BackgroundBox box_clip) {
  66. auto box = border_box;
  67. switch (box_clip) {
  68. case CSS::BackgroundBox::ContentBox: {
  69. auto& padding = layout_node.box_model().padding;
  70. box.shrink(padding.top, padding.right, padding.bottom, padding.left);
  71. [[fallthrough]];
  72. }
  73. case CSS::BackgroundBox::PaddingBox: {
  74. auto& border = layout_node.box_model().border;
  75. box.shrink(border.top, border.right, border.bottom, border.left);
  76. [[fallthrough]];
  77. }
  78. case CSS::BackgroundBox::BorderBox:
  79. default:
  80. return box;
  81. }
  82. };
  83. auto color_box = border_box;
  84. if (background_layers && !background_layers->is_empty())
  85. color_box = get_box(background_layers->last().clip);
  86. auto layer_is_paintable = [&](auto& layer) {
  87. return (layer.background_image
  88. && ((layer.background_image->is_image() && layer.background_image->as_image().bitmap())
  89. || layer.background_image->is_linear_gradient()));
  90. };
  91. bool has_paintable_layers = false;
  92. if (background_layers) {
  93. for (auto& layer : *background_layers) {
  94. if (layer_is_paintable(layer)) {
  95. has_paintable_layers = true;
  96. break;
  97. }
  98. }
  99. }
  100. Gfx::AntiAliasingPainter aa_painter { painter };
  101. aa_painter.fill_rect_with_rounded_corners(color_box.rect.to_rounded<int>(),
  102. background_color, color_box.radii.top_left.as_corner(), color_box.radii.top_right.as_corner(), color_box.radii.bottom_right.as_corner(), color_box.radii.bottom_left.as_corner());
  103. if (!has_paintable_layers)
  104. return;
  105. // Note: Background layers are ordered front-to-back, so we paint them in reverse
  106. for (auto& layer : background_layers->in_reverse()) {
  107. // TODO: Gradients!
  108. if (!layer_is_paintable(layer))
  109. continue;
  110. Gfx::PainterStateSaver state { painter };
  111. // Clip
  112. auto clip_box = get_box(layer.clip);
  113. auto clip_rect = clip_box.rect.to_rounded<int>();
  114. painter.add_clip_rect(clip_rect);
  115. ScopedCornerRadiusClip corner_clip { painter, clip_rect, clip_box.radii };
  116. if (layer.background_image->is_linear_gradient()) {
  117. // FIXME: Paint non-orthogonal gradients, gradients with > 2 color stops, gradients with custom stop positions ...
  118. // FIXME: Support sizing and positioning rules with gradients.
  119. auto& linear_gradient = layer.background_image->as_linear_gradient();
  120. auto gfx_gradient = linear_gradient_to_gfx_gradient(linear_gradient, border_box.rect);
  121. if (gfx_gradient.has_value()) {
  122. painter.fill_rect_with_gradient(gfx_gradient->orientation, border_box.rect.to_rounded<int>(), gfx_gradient->color_a, gfx_gradient->color_b);
  123. }
  124. continue;
  125. }
  126. auto& image = *layer.background_image->as_image().bitmap();
  127. Gfx::FloatRect background_positioning_area;
  128. // Attachment and Origin
  129. switch (layer.attachment) {
  130. case CSS::BackgroundAttachment::Fixed:
  131. background_positioning_area = layout_node.root().browsing_context().viewport_rect().to_type<float>();
  132. break;
  133. case CSS::BackgroundAttachment::Local:
  134. case CSS::BackgroundAttachment::Scroll:
  135. background_positioning_area = get_box(layer.origin).rect;
  136. break;
  137. }
  138. // Size
  139. Gfx::FloatRect image_rect;
  140. switch (layer.size_type) {
  141. case CSS::BackgroundSize::Contain: {
  142. float max_width_ratio = background_positioning_area.width() / image.width();
  143. float max_height_ratio = background_positioning_area.height() / image.height();
  144. float ratio = min(max_width_ratio, max_height_ratio);
  145. image_rect.set_size(image.width() * ratio, image.height() * ratio);
  146. break;
  147. }
  148. case CSS::BackgroundSize::Cover: {
  149. float max_width_ratio = background_positioning_area.width() / image.width();
  150. float max_height_ratio = background_positioning_area.height() / image.height();
  151. float ratio = max(max_width_ratio, max_height_ratio);
  152. image_rect.set_size(image.width() * ratio, image.height() * ratio);
  153. break;
  154. }
  155. case CSS::BackgroundSize::LengthPercentage: {
  156. float width;
  157. float height;
  158. bool x_is_auto = layer.size_x.is_length() && layer.size_x.length().is_auto();
  159. bool y_is_auto = layer.size_y.is_length() && layer.size_y.length().is_auto();
  160. if (x_is_auto && y_is_auto) {
  161. width = image.width();
  162. height = image.height();
  163. } else if (x_is_auto) {
  164. height = layer.size_y.resolved(layout_node, CSS::Length::make_px(background_positioning_area.height())).to_px(layout_node);
  165. width = image.width() * (height / image.height());
  166. } else if (y_is_auto) {
  167. width = layer.size_x.resolved(layout_node, CSS::Length::make_px(background_positioning_area.width())).to_px(layout_node);
  168. height = image.height() * (width / image.width());
  169. } else {
  170. width = layer.size_x.resolved(layout_node, CSS::Length::make_px(background_positioning_area.width())).to_px(layout_node);
  171. height = layer.size_y.resolved(layout_node, CSS::Length::make_px(background_positioning_area.height())).to_px(layout_node);
  172. }
  173. image_rect.set_size(width, height);
  174. break;
  175. }
  176. }
  177. // If background-repeat is round for one (or both) dimensions, there is a second step.
  178. // The UA must scale the image in that dimension (or both dimensions) so that it fits a
  179. // whole number of times in the background positioning area.
  180. if (layer.repeat_x == CSS::Repeat::Round || layer.repeat_y == CSS::Repeat::Round) {
  181. // If X ≠ 0 is the width of the image after step one and W is the width of the
  182. // background positioning area, then the rounded width X' = W / round(W / X)
  183. // where round() is a function that returns the nearest natural number
  184. // (integer greater than zero).
  185. if (layer.repeat_x == CSS::Repeat::Round) {
  186. image_rect.set_width(background_positioning_area.width() / roundf(background_positioning_area.width() / image_rect.width()));
  187. }
  188. if (layer.repeat_y == CSS::Repeat::Round) {
  189. image_rect.set_height(background_positioning_area.height() / roundf(background_positioning_area.height() / image_rect.height()));
  190. }
  191. // If background-repeat is round for one dimension only and if background-size is auto
  192. // for the other dimension, then there is a third step: that other dimension is scaled
  193. // so that the original aspect ratio is restored.
  194. if (layer.repeat_x != layer.repeat_y) {
  195. if (layer.size_x.is_length() && layer.size_x.length().is_auto()) {
  196. image_rect.set_width(image.width() * (image_rect.height() / image.height()));
  197. }
  198. if (layer.size_y.is_length() && layer.size_y.length().is_auto()) {
  199. image_rect.set_height(image.height() * (image_rect.width() / image.width()));
  200. }
  201. }
  202. }
  203. float space_x = background_positioning_area.width() - image_rect.width();
  204. float space_y = background_positioning_area.height() - image_rect.height();
  205. // Position
  206. float offset_x = layer.position_offset_x.resolved(layout_node, CSS::Length::make_px(space_x)).to_px(layout_node);
  207. if (layer.position_edge_x == CSS::PositionEdge::Right) {
  208. image_rect.set_right_without_resize(background_positioning_area.right() - offset_x);
  209. } else {
  210. image_rect.set_left(background_positioning_area.left() + offset_x);
  211. }
  212. float offset_y = layer.position_offset_y.resolved(layout_node, CSS::Length::make_px(space_y)).to_px(layout_node);
  213. if (layer.position_edge_y == CSS::PositionEdge::Bottom) {
  214. image_rect.set_bottom_without_resize(background_positioning_area.bottom() - offset_y);
  215. } else {
  216. image_rect.set_top(background_positioning_area.top() + offset_y);
  217. }
  218. // Repetition
  219. bool repeat_x = false;
  220. bool repeat_y = false;
  221. float x_step = 0;
  222. float y_step = 0;
  223. switch (layer.repeat_x) {
  224. case CSS::Repeat::Round:
  225. x_step = image_rect.width();
  226. repeat_x = true;
  227. break;
  228. case CSS::Repeat::Space: {
  229. int whole_images = background_positioning_area.width() / image_rect.width();
  230. if (whole_images <= 1) {
  231. x_step = image_rect.width();
  232. repeat_x = false;
  233. } else {
  234. float space = fmodf(background_positioning_area.width(), image_rect.width());
  235. x_step = image_rect.width() + ((float)space / (float)(whole_images - 1));
  236. repeat_x = true;
  237. }
  238. break;
  239. }
  240. case CSS::Repeat::Repeat:
  241. x_step = image_rect.width();
  242. repeat_x = true;
  243. break;
  244. case CSS::Repeat::NoRepeat:
  245. repeat_x = false;
  246. break;
  247. }
  248. // Move image_rect to the left-most tile position that is still visible
  249. if (repeat_x && image_rect.x() > clip_rect.x()) {
  250. auto x_delta = floorf(x_step * ceilf((image_rect.x() - clip_rect.x()) / x_step));
  251. image_rect.set_x(image_rect.x() - x_delta);
  252. }
  253. switch (layer.repeat_y) {
  254. case CSS::Repeat::Round:
  255. y_step = image_rect.height();
  256. repeat_y = true;
  257. break;
  258. case CSS::Repeat::Space: {
  259. int whole_images = background_positioning_area.height() / image_rect.height();
  260. if (whole_images <= 1) {
  261. y_step = image_rect.height();
  262. repeat_y = false;
  263. } else {
  264. float space = fmodf(background_positioning_area.height(), image_rect.height());
  265. y_step = image_rect.height() + ((float)space / (float)(whole_images - 1));
  266. repeat_y = true;
  267. }
  268. break;
  269. }
  270. case CSS::Repeat::Repeat:
  271. y_step = image_rect.height();
  272. repeat_y = true;
  273. break;
  274. case CSS::Repeat::NoRepeat:
  275. repeat_y = false;
  276. break;
  277. }
  278. // Move image_rect to the top-most tile position that is still visible
  279. if (repeat_y && image_rect.y() > clip_rect.y()) {
  280. auto y_delta = floorf(y_step * ceilf((image_rect.y() - clip_rect.y()) / y_step));
  281. image_rect.set_y(image_rect.y() - y_delta);
  282. }
  283. float initial_image_x = image_rect.x();
  284. float image_y = image_rect.y();
  285. Optional<Gfx::IntRect> last_int_image_rect;
  286. while (image_y < clip_rect.bottom()) {
  287. image_rect.set_y(image_y);
  288. float image_x = initial_image_x;
  289. while (image_x < clip_rect.right()) {
  290. image_rect.set_x(image_x);
  291. auto int_image_rect = image_rect.to_rounded<int>();
  292. if (int_image_rect != last_int_image_rect)
  293. painter.draw_scaled_bitmap(int_image_rect, image, image.rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
  294. last_int_image_rect = int_image_rect;
  295. if (!repeat_x)
  296. break;
  297. image_x += x_step;
  298. }
  299. if (!repeat_y)
  300. break;
  301. image_y += y_step;
  302. }
  303. }
  304. }
  305. }