RadialGradientStyleValue.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "RadialGradientStyleValue.h"
  10. #include <LibWeb/Layout/Node.h>
  11. namespace Web::CSS {
  12. String RadialGradientStyleValue::to_string() const
  13. {
  14. StringBuilder builder;
  15. if (is_repeating())
  16. builder.append("repeating-"sv);
  17. builder.appendff("radial-gradient({} "sv,
  18. m_properties.ending_shape == EndingShape::Circle ? "circle"sv : "ellipse"sv);
  19. m_properties.size.visit(
  20. [&](Extent extent) {
  21. builder.append([&] {
  22. switch (extent) {
  23. case Extent::ClosestCorner:
  24. return "closest-corner"sv;
  25. case Extent::ClosestSide:
  26. return "closest-side"sv;
  27. case Extent::FarthestCorner:
  28. return "farthest-corner"sv;
  29. case Extent::FarthestSide:
  30. return "farthest-side"sv;
  31. default:
  32. VERIFY_NOT_REACHED();
  33. }
  34. }());
  35. },
  36. [&](CircleSize const& circle_size) {
  37. builder.append(circle_size.radius.to_string());
  38. },
  39. [&](EllipseSize const& ellipse_size) {
  40. builder.appendff("{} {}", ellipse_size.radius_a.to_string(), ellipse_size.radius_b.to_string());
  41. });
  42. if (m_properties.position != PositionValue::center()) {
  43. builder.appendff(" at "sv);
  44. m_properties.position.serialize(builder);
  45. }
  46. builder.append(", "sv);
  47. serialize_color_stop_list(builder, m_properties.color_stop_list);
  48. builder.append(')');
  49. return MUST(builder.to_string());
  50. }
  51. CSSPixelSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, CSSPixelPoint center, CSSPixelRect const& size) const
  52. {
  53. auto const side_shape = [&](auto distance_function) {
  54. auto const distance_from = [&](CSSPixels v, CSSPixels a, CSSPixels b, auto distance_function) {
  55. return distance_function(abs(a - v), abs(b - v));
  56. };
  57. auto x_dist = distance_from(center.x(), size.left(), size.right(), distance_function);
  58. auto y_dist = distance_from(center.y(), size.top(), size.bottom(), distance_function);
  59. if (m_properties.ending_shape == EndingShape::Circle) {
  60. auto dist = distance_function(x_dist, y_dist);
  61. return CSSPixelSize { dist, dist };
  62. } else {
  63. return CSSPixelSize { x_dist, y_dist };
  64. }
  65. };
  66. auto const closest_side_shape = [&] {
  67. return side_shape(AK::min<CSSPixels>);
  68. };
  69. auto const farthest_side_shape = [&] {
  70. return side_shape(AK::max<CSSPixels>);
  71. };
  72. auto const corner_distance = [&](auto distance_compare, CSSPixelPoint& corner) {
  73. auto top_left_distance_squared = square_distance_between(size.top_left(), center);
  74. auto top_right_distance_squared = square_distance_between(size.top_right(), center);
  75. auto bottom_right_distance_squared = square_distance_between(size.bottom_right(), center);
  76. auto bottom_left_distance_squared = square_distance_between(size.bottom_left(), center);
  77. auto distance_squared = top_left_distance_squared;
  78. corner = size.top_left();
  79. if (distance_compare(top_right_distance_squared, distance_squared)) {
  80. corner = size.top_right();
  81. distance_squared = top_right_distance_squared;
  82. }
  83. if (distance_compare(bottom_right_distance_squared, distance_squared)) {
  84. corner = size.bottom_right();
  85. distance_squared = bottom_right_distance_squared;
  86. }
  87. if (distance_compare(bottom_left_distance_squared, distance_squared)) {
  88. corner = size.bottom_left();
  89. distance_squared = bottom_left_distance_squared;
  90. }
  91. return sqrt(distance_squared);
  92. };
  93. auto const closest_corner_distance = [&](CSSPixelPoint& corner) {
  94. return corner_distance([](CSSPixels a, CSSPixels b) { return a < b; }, corner);
  95. };
  96. auto const farthest_corner_distance = [&](CSSPixelPoint& corner) {
  97. return corner_distance([](CSSPixels a, CSSPixels b) { return a > b; }, corner);
  98. };
  99. auto const corner_shape = [&](auto corner_distance, auto get_shape) {
  100. CSSPixelPoint corner {};
  101. auto distance = corner_distance(corner);
  102. if (m_properties.ending_shape == EndingShape::Ellipse) {
  103. auto shape = get_shape();
  104. auto aspect_ratio = shape.width() / shape.height();
  105. auto p = corner - center;
  106. auto radius_a = sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x());
  107. auto radius_b = radius_a / aspect_ratio;
  108. return CSSPixelSize { radius_a, radius_b };
  109. }
  110. return CSSPixelSize { distance, distance };
  111. };
  112. // https://w3c.github.io/csswg-drafts/css-images/#radial-gradient-syntax
  113. auto resolved_size = m_properties.size.visit(
  114. [&](Extent extent) {
  115. switch (extent) {
  116. case Extent::ClosestSide:
  117. // The ending shape is sized so that it exactly meets the side of the gradient box closest to the gradient’s center.
  118. // If the shape is an ellipse, it exactly meets the closest side in each dimension.
  119. return closest_side_shape();
  120. case Extent::ClosestCorner:
  121. // The ending shape is sized so that it passes through the corner of the gradient box closest to the gradient’s center.
  122. // If the shape is an ellipse, the ending shape is given the same aspect-ratio it would have if closest-side were specified
  123. return corner_shape(closest_corner_distance, closest_side_shape);
  124. case Extent::FarthestCorner:
  125. // Same as closest-corner, except the ending shape is sized based on the farthest corner.
  126. // If the shape is an ellipse, the ending shape is given the same aspect ratio it would have if farthest-side were specified.
  127. return corner_shape(farthest_corner_distance, farthest_side_shape);
  128. case Extent::FarthestSide:
  129. // Same as closest-side, except the ending shape is sized based on the farthest side(s).
  130. return farthest_side_shape();
  131. default:
  132. VERIFY_NOT_REACHED();
  133. }
  134. },
  135. [&](CircleSize const& circle_size) {
  136. auto radius = circle_size.radius.to_px(node);
  137. return CSSPixelSize { radius, radius };
  138. },
  139. [&](EllipseSize const& ellipse_size) {
  140. auto radius_a = ellipse_size.radius_a.resolved(node, size.width()).to_px(node);
  141. auto radius_b = ellipse_size.radius_b.resolved(node, size.height()).to_px(node);
  142. return CSSPixelSize { radius_a, radius_b };
  143. });
  144. // Handle degenerate cases
  145. // https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials
  146. constexpr auto arbitrary_small_number = CSSPixels::smallest_positive_value();
  147. constexpr auto arbitrary_large_number = CSSPixels::max();
  148. // If the ending shape is a circle with zero radius:
  149. if (m_properties.ending_shape == EndingShape::Circle && resolved_size.is_empty()) {
  150. // Render as if the ending shape was a circle whose radius was an arbitrary very small number greater than zero.
  151. // This will make the gradient continue to look like a circle.
  152. return CSSPixelSize { arbitrary_small_number, arbitrary_small_number };
  153. }
  154. // If the ending shape has zero width (regardless of the height):
  155. if (resolved_size.width() <= 0) {
  156. // Render as if the ending shape was an ellipse whose height was an arbitrary very large number
  157. // and whose width was an arbitrary very small number greater than zero.
  158. // This will make the gradient look similar to a horizontal linear gradient that is mirrored across the center of the ellipse.
  159. // It also means that all color-stop positions specified with a percentage resolve to 0px.
  160. return CSSPixelSize { arbitrary_small_number, arbitrary_large_number };
  161. }
  162. // Otherwise, if the ending shape has zero height:
  163. if (resolved_size.height() <= 0) {
  164. // Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height
  165. // was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal
  166. // to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating.
  167. return CSSPixelSize { arbitrary_large_number, arbitrary_small_number };
  168. }
  169. return resolved_size;
  170. }
  171. void RadialGradientStyleValue::resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize paint_size) const
  172. {
  173. CSSPixelRect gradient_box { { 0, 0 }, paint_size };
  174. auto center = m_properties.position.resolved(node, gradient_box);
  175. auto gradient_size = resolve_size(node, center, gradient_box);
  176. if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size)
  177. return;
  178. m_resolved = ResolvedData {
  179. Painting::resolve_radial_gradient_data(node, gradient_size, *this),
  180. gradient_size,
  181. center,
  182. };
  183. }
  184. bool RadialGradientStyleValue::equals(StyleValue const& other) const
  185. {
  186. if (type() != other.type())
  187. return false;
  188. auto& other_gradient = other.as_radial_gradient();
  189. return m_properties == other_gradient.m_properties;
  190. }
  191. void RadialGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  192. {
  193. VERIFY(m_resolved.has_value());
  194. Painting::paint_radial_gradient(context, dest_rect, m_resolved->data,
  195. context.rounded_device_point(m_resolved->center),
  196. context.rounded_device_size(m_resolved->gradient_size));
  197. }
  198. }