RadialGradientStyleValue.cpp 11 KB

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