GradientPainting.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Math.h>
  7. #include <LibGfx/Gradients.h>
  8. #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
  11. #include <LibWeb/Layout/Node.h>
  12. #include <LibWeb/Painting/GradientPainting.h>
  13. namespace Web::Painting {
  14. static ColorStopData resolve_color_stop_positions(Layout::NodeWithStyleAndBoxModelMetrics const& node, auto const& color_stop_list, auto resolve_position_to_float, bool repeating)
  15. {
  16. VERIFY(color_stop_list.size() >= 2);
  17. ColorStopList resolved_color_stops;
  18. auto color_stop_length = [&](auto& stop) {
  19. return stop.color_stop.second_position.has_value() ? 2 : 1;
  20. };
  21. size_t expanded_size = 0;
  22. for (auto& stop : color_stop_list)
  23. expanded_size += color_stop_length(stop);
  24. resolved_color_stops.ensure_capacity(expanded_size);
  25. for (auto& stop : color_stop_list) {
  26. auto resolved_stop = Gfx::ColorStop { .color = stop.color_stop.color->to_color(node) };
  27. for (int i = 0; i < color_stop_length(stop); i++)
  28. resolved_color_stops.append(resolved_stop);
  29. }
  30. // 1. If the first color stop does not have a position, set its position to 0%.
  31. resolved_color_stops.first().position = 0;
  32. // If the last color stop does not have a position, set its position to 100%
  33. resolved_color_stops.last().position = 1.0f;
  34. // 2. If a color stop or transition hint has a position that is less than the
  35. // specified position of any color stop or transition hint before it in the list,
  36. // set its position to be equal to the largest specified position of any color stop
  37. // or transition hint before it.
  38. auto max_previous_color_stop_or_hint = resolved_color_stops[0].position;
  39. auto resolve_stop_position = [&](auto& position) {
  40. float value = resolve_position_to_float(position);
  41. value = max(value, max_previous_color_stop_or_hint);
  42. max_previous_color_stop_or_hint = value;
  43. return value;
  44. };
  45. size_t resolved_index = 0;
  46. for (auto& stop : color_stop_list) {
  47. if (stop.transition_hint.has_value())
  48. resolved_color_stops[resolved_index].transition_hint = resolve_stop_position(stop.transition_hint->value);
  49. if (stop.color_stop.position.has_value())
  50. resolved_color_stops[resolved_index].position = resolve_stop_position(*stop.color_stop.position);
  51. if (stop.color_stop.second_position.has_value())
  52. resolved_color_stops[++resolved_index].position = resolve_stop_position(*stop.color_stop.second_position);
  53. ++resolved_index;
  54. }
  55. // 3. If any color stop still does not have a position, then, for each run of adjacent color stops
  56. // without positions, set their positions so that they are evenly spaced between the preceding
  57. // and following color stops with positions.
  58. // Note: Though not mentioned anywhere in the specification transition hints are counted as "color stops with positions".
  59. size_t i = 1;
  60. auto find_run_end = [&] {
  61. auto color_stop_has_position = [](auto& color_stop) {
  62. return color_stop.transition_hint.has_value() || isfinite(color_stop.position);
  63. };
  64. while (i < color_stop_list.size() - 1 && !color_stop_has_position(resolved_color_stops[i])) {
  65. i++;
  66. }
  67. return i;
  68. };
  69. while (i < resolved_color_stops.size() - 1) {
  70. auto& stop = resolved_color_stops[i];
  71. if (!isfinite(stop.position)) {
  72. auto run_start = i - 1;
  73. auto start_position = resolved_color_stops[i++].transition_hint.value_or(resolved_color_stops[run_start].position);
  74. auto run_end = find_run_end();
  75. auto end_position = resolved_color_stops[run_end].transition_hint.value_or(resolved_color_stops[run_end].position);
  76. auto spacing = (end_position - start_position) / (run_end - run_start);
  77. for (auto j = run_start + 1; j < run_end; j++) {
  78. resolved_color_stops[j].position = start_position + (j - run_start) * spacing;
  79. }
  80. }
  81. i++;
  82. }
  83. // Determine the location of the transition hint as a percentage of the distance between the two color stops,
  84. // denoted as a number between 0 and 1, where 0 indicates the hint is placed right on the first color stop,
  85. // and 1 indicates the hint is placed right on the second color stop.
  86. for (size_t i = 1; i < resolved_color_stops.size(); i++) {
  87. auto& color_stop = resolved_color_stops[i];
  88. auto& previous_color_stop = resolved_color_stops[i - 1];
  89. if (color_stop.transition_hint.has_value()) {
  90. auto stop_length = color_stop.position - previous_color_stop.position;
  91. color_stop.transition_hint = stop_length > 0 ? (*color_stop.transition_hint - previous_color_stop.position) / stop_length : 0;
  92. }
  93. }
  94. Optional<float> repeat_length = {};
  95. if (repeating)
  96. repeat_length = resolved_color_stops.last().position - resolved_color_stops.first().position;
  97. return { resolved_color_stops, repeat_length };
  98. }
  99. LinearGradientData resolve_linear_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize gradient_size, CSS::LinearGradientStyleValue const& linear_gradient)
  100. {
  101. auto gradient_angle = linear_gradient.angle_degrees(gradient_size);
  102. auto gradient_length_px = Gfx::calculate_gradient_length(gradient_size.to_type<float>(), gradient_angle);
  103. auto resolved_color_stops = resolve_color_stop_positions(
  104. node, linear_gradient.color_stop_list(), [&](auto const& length_percentage) {
  105. return length_percentage.to_px(node, CSSPixels::nearest_value_for(gradient_length_px)).to_float() / static_cast<float>(gradient_length_px);
  106. },
  107. linear_gradient.is_repeating());
  108. return { gradient_angle, resolved_color_stops };
  109. }
  110. ConicGradientData resolve_conic_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSS::ConicGradientStyleValue const& conic_gradient)
  111. {
  112. CSS::Angle one_turn(360.0f, CSS::Angle::Type::Deg);
  113. auto resolved_color_stops = resolve_color_stop_positions(
  114. node, conic_gradient.color_stop_list(), [&](auto const& angle_percentage) {
  115. return angle_percentage.resolved(node, one_turn).to_degrees() / one_turn.to_degrees();
  116. },
  117. conic_gradient.is_repeating());
  118. return { conic_gradient.angle_degrees(), resolved_color_stops };
  119. }
  120. RadialGradientData resolve_radial_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize gradient_size, CSS::RadialGradientStyleValue const& radial_gradient)
  121. {
  122. // Start center, goes right to ending point, where the gradient line intersects the ending shape
  123. auto resolved_color_stops = resolve_color_stop_positions(
  124. node, radial_gradient.color_stop_list(), [&](auto const& length_percentage) {
  125. return length_percentage.to_px(node, gradient_size.width()).to_float() / gradient_size.width().to_float();
  126. },
  127. radial_gradient.is_repeating());
  128. return { resolved_color_stops };
  129. }
  130. void paint_linear_gradient(PaintContext& context, DevicePixelRect const& gradient_rect, LinearGradientData const& data)
  131. {
  132. context.painter().fill_rect_with_linear_gradient(gradient_rect.to_type<int>(), data);
  133. }
  134. void paint_conic_gradient(PaintContext& context, DevicePixelRect const& gradient_rect, ConicGradientData const& data, DevicePixelPoint position)
  135. {
  136. context.painter().fill_rect_with_conic_gradient(gradient_rect.to_type<int>(), data, position.to_type<int>());
  137. }
  138. void paint_radial_gradient(PaintContext& context, DevicePixelRect const& gradient_rect, RadialGradientData const& data, DevicePixelPoint center, DevicePixelSize size)
  139. {
  140. context.painter().fill_rect_with_radial_gradient(gradient_rect.to_type<int>(), data, center, size);
  141. }
  142. }