FillPathImplementation.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 static 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 Painter>
  39. void fill_path(Painter& painter, Path const& path, Color color, Gfx::Painter::WindingRule winding_rule)
  40. {
  41. using GridCoordinateType = Conditional<fill_path_mode == FillPathMode::PlaceOnIntGrid, int, float>;
  42. using PointType = Point<GridCoordinateType>;
  43. auto draw_line = [&](auto... args) {
  44. if constexpr (requires { painter.draw_aliased_line(args...); })
  45. painter.draw_aliased_line(args...);
  46. else
  47. painter.draw_line(args...);
  48. };
  49. auto const& segments = path.split_lines();
  50. if (segments.size() == 0)
  51. return;
  52. Vector<Path::SplitLineSegment> active_list;
  53. active_list.ensure_capacity(segments.size());
  54. // first, grab the segments for the very first scanline
  55. GridCoordinateType first_y = path.bounding_box().bottom_right().y() + 1;
  56. GridCoordinateType last_y = path.bounding_box().top_left().y() - 1;
  57. float scanline = first_y;
  58. size_t last_active_segment { 0 };
  59. for (auto& segment : segments) {
  60. if (segment.maximum_y != scanline)
  61. break;
  62. active_list.append(segment);
  63. ++last_active_segment;
  64. }
  65. auto is_inside_shape = [winding_rule](int winding_number) {
  66. if (winding_rule == Gfx::Painter::WindingRule::Nonzero)
  67. return winding_number != 0;
  68. if (winding_rule == Gfx::Painter::WindingRule::EvenOdd)
  69. return winding_number % 2 == 0;
  70. VERIFY_NOT_REACHED();
  71. };
  72. auto increment_winding = [winding_rule](int& winding_number, PointType const& from, PointType const& to) {
  73. if (winding_rule == Gfx::Painter::WindingRule::EvenOdd) {
  74. ++winding_number;
  75. return;
  76. }
  77. if (winding_rule == Gfx::Painter::WindingRule::Nonzero) {
  78. if (from.dy_relative_to(to) < 0)
  79. ++winding_number;
  80. else
  81. --winding_number;
  82. return;
  83. }
  84. VERIFY_NOT_REACHED();
  85. };
  86. while (scanline >= last_y) {
  87. Optional<PointType> previous_to;
  88. if (active_list.size()) {
  89. // sort the active list by 'x' from right to left
  90. quick_sort(active_list, [](auto const& line0, auto const& line1) {
  91. return line1.x < line0.x;
  92. });
  93. if constexpr (fill_path_mode == FillPathMode::PlaceOnIntGrid && FILL_PATH_DEBUG) {
  94. if ((int)scanline % 10 == 0) {
  95. painter.draw_text(Gfx::Rect<GridCoordinateType>(active_list.last().x - 20, scanline, 20, 10), String::number((int)scanline));
  96. }
  97. }
  98. if (active_list.size() > 1) {
  99. auto winding_number { winding_rule == Gfx::Painter::WindingRule::Nonzero ? 1 : 0 };
  100. for (size_t i = 1; i < active_list.size(); ++i) {
  101. auto& previous = active_list[i - 1];
  102. auto& current = active_list[i];
  103. PointType from, to;
  104. PointType truncated_from { previous.x, scanline };
  105. PointType truncated_to { current.x, scanline };
  106. if constexpr (fill_path_mode == FillPathMode::PlaceOnIntGrid) {
  107. approximately_place_on_int_grid({ previous.x, scanline }, { current.x, scanline }, from, to, previous_to);
  108. } else {
  109. from = truncated_from;
  110. to = truncated_to;
  111. }
  112. if (is_inside_shape(winding_number)) {
  113. // The points between this segment and the previous are
  114. // inside the shape
  115. dbgln_if(FILL_PATH_DEBUG, "y={}: {} at {}: {} -- {}", scanline, winding_number, i, from, to);
  116. draw_line(from, to, color, 1);
  117. }
  118. auto is_passing_through_maxima = scanline == previous.maximum_y
  119. || scanline == previous.minimum_y
  120. || scanline == current.maximum_y
  121. || scanline == current.minimum_y;
  122. auto is_passing_through_vertex = false;
  123. if (is_passing_through_maxima) {
  124. is_passing_through_vertex = previous.x == current.x;
  125. }
  126. if (!is_passing_through_vertex || previous.inverse_slope * current.inverse_slope < 0)
  127. increment_winding(winding_number, truncated_from, truncated_to);
  128. // update the x coord
  129. active_list[i - 1].x -= active_list[i - 1].inverse_slope;
  130. }
  131. active_list.last().x -= active_list.last().inverse_slope;
  132. } else {
  133. auto point = PointType(active_list[0].x, scanline);
  134. draw_line(point, point, color);
  135. // update the x coord
  136. active_list.first().x -= active_list.first().inverse_slope;
  137. }
  138. }
  139. --scanline;
  140. // remove any edge that goes out of bound from the active list
  141. for (size_t i = 0, count = active_list.size(); i < count; ++i) {
  142. if (scanline <= active_list[i].minimum_y) {
  143. active_list.remove(i);
  144. --count;
  145. --i;
  146. }
  147. }
  148. for (size_t j = last_active_segment; j < segments.size(); ++j, ++last_active_segment) {
  149. auto& segment = segments[j];
  150. if (segment.maximum_y < scanline)
  151. break;
  152. if (segment.minimum_y >= scanline)
  153. continue;
  154. active_list.append(segment);
  155. }
  156. }
  157. if constexpr (FILL_PATH_DEBUG) {
  158. size_t i { 0 };
  159. for (auto& segment : segments) {
  160. draw_line(PointType(segment.from), PointType(segment.to), Color::from_hsv(i++ * 360.0 / segments.size(), 1.0, 1.0), 1);
  161. }
  162. }
  163. }
  164. }