FillPathImplementation.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Debug.h>
  8. #include <AK/QuickSort.h>
  9. #include <LibGfx/Color.h>
  10. #include <LibGfx/Painter.h>
  11. #include <LibGfx/Path.h>
  12. namespace Gfx::Detail {
  13. [[maybe_unused]] inline void approximately_place_on_int_grid(FloatPoint ffrom, FloatPoint fto, IntPoint& from, IntPoint& to, Optional<IntPoint> previous_to)
  14. {
  15. auto diffs = fto - ffrom;
  16. // Truncate all first (round down).
  17. from = ffrom.to_type<int>();
  18. to = fto.to_type<int>();
  19. // There are 16 possible configurations, by deciding to round each
  20. // coord up or down (and there are four coords, from.x from.y to.x to.y)
  21. // we will simply choose one which most closely matches the correct slope
  22. // with the following heuristic:
  23. // - if the x diff is positive or zero (that is, a right-to-left slant), round 'from.x' up and 'to.x' down.
  24. // - if the x diff is negative (that is, a left-to-right slant), round 'from.x' down and 'to.x' up.
  25. // Note that we do not need to touch the 'y' attribute, as that is our scanline.
  26. if (diffs.x() >= 0) {
  27. from.set_x(from.x() + 1);
  28. } else {
  29. to.set_x(to.x() + 1);
  30. }
  31. if (previous_to.has_value() && from.x() != previous_to.value().x()) // The points have to line up, since we're using these lines to fill a shape.
  32. from.set_x(previous_to.value().x());
  33. }
  34. enum class FillPathMode {
  35. PlaceOnIntGrid,
  36. AllowFloatingPoints,
  37. };
  38. template<FillPathMode fill_path_mode, typename ColorOrFunction>
  39. void fill_path(Painter& painter, Path const& path, ColorOrFunction color, Gfx::Painter::WindingRule winding_rule, Optional<FloatPoint> offset = {})
  40. {
  41. using GridCoordinateType = Conditional<fill_path_mode == FillPathMode::PlaceOnIntGrid, int, float>;
  42. using PointType = Point<GridCoordinateType>;
  43. auto draw_scanline = [&](int y, GridCoordinateType x1, GridCoordinateType x2) {
  44. const auto draw_offset = offset.value_or({ 0, 0 });
  45. const auto draw_origin = (path.bounding_box().top_left() + draw_offset).to_type<int>();
  46. // FIMXE: Offset is added here to handle floating point translations in the AA painter,
  47. // really this should be done there but this function is a bit too specialised.
  48. y = floorf(y + draw_offset.y());
  49. x1 += draw_offset.x();
  50. x2 += draw_offset.x();
  51. if (x1 > x2)
  52. swap(x1, x2);
  53. if constexpr (IsSameIgnoringCV<ColorOrFunction, Color>) {
  54. painter.draw_scanline_for_fill_path(y, x1, x2 + 1, color);
  55. } else {
  56. painter.draw_scanline_for_fill_path(y, x1, x2 + 1, [&](int offset) {
  57. return color(IntPoint(x1 + offset, y) - draw_origin);
  58. });
  59. }
  60. };
  61. auto const& segments = path.split_lines();
  62. if (segments.size() == 0)
  63. return;
  64. Vector<Path::SplitLineSegment> active_list;
  65. active_list.ensure_capacity(segments.size());
  66. // first, grab the segments for the very first scanline
  67. GridCoordinateType first_y = path.bounding_box().bottom_right().y() + 1;
  68. GridCoordinateType last_y = path.bounding_box().top_left().y() - 1;
  69. float scanline = first_y;
  70. size_t last_active_segment { 0 };
  71. for (auto& segment : segments) {
  72. if (segment.maximum_y != scanline)
  73. break;
  74. active_list.append(segment);
  75. ++last_active_segment;
  76. }
  77. auto is_inside_shape = [winding_rule](int winding_number) {
  78. if (winding_rule == Gfx::Painter::WindingRule::Nonzero)
  79. return winding_number != 0;
  80. if (winding_rule == Gfx::Painter::WindingRule::EvenOdd)
  81. return winding_number % 2 == 0;
  82. VERIFY_NOT_REACHED();
  83. };
  84. auto increment_winding = [winding_rule](int& winding_number, PointType const& from, PointType const& to) {
  85. if (winding_rule == Gfx::Painter::WindingRule::EvenOdd) {
  86. ++winding_number;
  87. return;
  88. }
  89. if (winding_rule == Gfx::Painter::WindingRule::Nonzero) {
  90. if (from.dy_relative_to(to) < 0)
  91. ++winding_number;
  92. else
  93. --winding_number;
  94. return;
  95. }
  96. VERIFY_NOT_REACHED();
  97. };
  98. while (scanline >= last_y) {
  99. Optional<PointType> previous_to;
  100. if (active_list.size()) {
  101. // sort the active list by 'x' from right to left
  102. quick_sort(active_list, [](auto const& line0, auto const& line1) {
  103. return line1.x < line0.x;
  104. });
  105. if constexpr (fill_path_mode == FillPathMode::PlaceOnIntGrid && FILL_PATH_DEBUG) {
  106. if ((int)scanline % 10 == 0) {
  107. painter.draw_text(Gfx::Rect<GridCoordinateType>(active_list.last().x - 20, scanline, 20, 10), DeprecatedString::number((int)scanline));
  108. }
  109. }
  110. if (active_list.size() > 1) {
  111. auto winding_number { winding_rule == Gfx::Painter::WindingRule::Nonzero ? 1 : 0 };
  112. for (size_t i = 1; i < active_list.size(); ++i) {
  113. auto& previous = active_list[i - 1];
  114. auto& current = active_list[i];
  115. PointType from, to;
  116. PointType truncated_from { previous.x, scanline };
  117. PointType truncated_to { current.x, scanline };
  118. if constexpr (fill_path_mode == FillPathMode::PlaceOnIntGrid) {
  119. approximately_place_on_int_grid({ previous.x, scanline }, { current.x, scanline }, from, to, previous_to);
  120. } else {
  121. from = truncated_from;
  122. to = truncated_to;
  123. }
  124. if (is_inside_shape(winding_number)) {
  125. // The points between this segment and the previous are
  126. // inside the shape
  127. dbgln_if(FILL_PATH_DEBUG, "y={}: {} at {}: {} -- {}", scanline, winding_number, i, from, to);
  128. draw_scanline(floorf(scanline), from.x(), to.x());
  129. }
  130. auto is_passing_through_maxima = scanline == previous.maximum_y
  131. || scanline == previous.minimum_y
  132. || scanline == current.maximum_y
  133. || scanline == current.minimum_y;
  134. auto is_passing_through_vertex = false;
  135. if (is_passing_through_maxima) {
  136. is_passing_through_vertex = previous.x == current.x;
  137. }
  138. if (!is_passing_through_vertex || previous.inverse_slope * current.inverse_slope < 0)
  139. increment_winding(winding_number, truncated_from, truncated_to);
  140. // update the x coord
  141. active_list[i - 1].x -= active_list[i - 1].inverse_slope;
  142. }
  143. active_list.last().x -= active_list.last().inverse_slope;
  144. } else {
  145. auto point = PointType(active_list[0].x, scanline);
  146. draw_scanline(floorf(scanline), point.x(), point.x());
  147. // update the x coord
  148. active_list.first().x -= active_list.first().inverse_slope;
  149. }
  150. }
  151. --scanline;
  152. // remove any edge that goes out of bound from the active list
  153. for (size_t i = 0, count = active_list.size(); i < count; ++i) {
  154. if (scanline <= active_list[i].minimum_y) {
  155. active_list.remove(i);
  156. --count;
  157. --i;
  158. }
  159. }
  160. for (size_t j = last_active_segment; j < segments.size(); ++j, ++last_active_segment) {
  161. auto& segment = segments[j];
  162. if (segment.maximum_y < scanline)
  163. break;
  164. if (segment.minimum_y >= scanline)
  165. continue;
  166. active_list.append(segment);
  167. }
  168. }
  169. }
  170. }