GradientPainting.cpp 7.9 KB

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