RadialGradientStyleValue.cpp 9.8 KB

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