GradientPainting.cpp 7.8 KB

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