Path.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/QuickSort.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibGfx/Painter.h>
  30. #include <LibGfx/Path.h>
  31. namespace Gfx {
  32. void Path::close()
  33. {
  34. if (m_segments.size() <= 1)
  35. return;
  36. invalidate_split_lines();
  37. auto& last_point = m_segments.last().point;
  38. for (ssize_t i = m_segments.size() - 1; i >= 0; --i) {
  39. auto& segment = m_segments[i];
  40. if (segment.type == Segment::Type::MoveTo) {
  41. if (last_point == segment.point)
  42. return;
  43. m_segments.append({ Segment::Type::LineTo, segment.point });
  44. return;
  45. }
  46. }
  47. }
  48. String Path::to_string() const
  49. {
  50. StringBuilder builder;
  51. builder.append("Path { ");
  52. for (auto& segment : m_segments) {
  53. switch (segment.type) {
  54. case Segment::Type::MoveTo:
  55. builder.append("MoveTo");
  56. break;
  57. case Segment::Type::LineTo:
  58. builder.append("LineTo");
  59. break;
  60. case Segment::Type::QuadraticBezierCurveTo:
  61. builder.append("QuadraticBezierCurveTo");
  62. break;
  63. case Segment::Type::Invalid:
  64. builder.append("Invalid");
  65. break;
  66. }
  67. builder.append('(');
  68. builder.append(segment.point.to_string());
  69. if (segment.through.has_value()) {
  70. builder.append(", ");
  71. builder.append(segment.through.value().to_string());
  72. }
  73. builder.append(')');
  74. builder.append(' ');
  75. }
  76. builder.append("}");
  77. return builder.to_string();
  78. }
  79. void Path::segmentize_path()
  80. {
  81. Vector<LineSegment> segments;
  82. auto add_line = [&](const auto& p0, const auto& p1) {
  83. if (p0.y() == p1.y())
  84. return; // horizontal lines are not needed (there's nothing to fill inside)
  85. float ymax = p0.y(), ymin = p1.y(), x_of_ymin = p1.x(), x_of_ymax = p0.x();
  86. auto slope = p0.x() == p1.x() ? 0 : ((float)(p0.y() - p1.y())) / ((float)(p0.x() - p1.x()));
  87. if (p0.y() < p1.y()) {
  88. ymin = ymax;
  89. ymax = p1.y();
  90. x_of_ymax = x_of_ymin;
  91. x_of_ymin = p0.x();
  92. }
  93. segments.append({ Point(p0.x(), p0.y()),
  94. Point(p1.x(), p1.y()),
  95. slope == 0 ? 0 : 1 / slope,
  96. x_of_ymin,
  97. ymax, ymin, x_of_ymax });
  98. };
  99. FloatPoint cursor { 0, 0 };
  100. for (auto& segment : m_segments) {
  101. switch (segment.type) {
  102. case Segment::Type::MoveTo:
  103. cursor = segment.point;
  104. break;
  105. case Segment::Type::LineTo: {
  106. add_line(cursor, segment.point);
  107. cursor = segment.point;
  108. break;
  109. }
  110. case Segment::Type::QuadraticBezierCurveTo: {
  111. auto& control = segment.through.value();
  112. Painter::for_each_line_segment_on_bezier_curve(control, cursor, segment.point, [&](const FloatPoint& p0, const FloatPoint& p1) {
  113. add_line(Point(p0.x(), p0.y()), Point(p1.x(), p1.y()));
  114. });
  115. cursor = segment.point;
  116. break;
  117. }
  118. case Segment::Type::Invalid:
  119. ASSERT_NOT_REACHED();
  120. break;
  121. }
  122. }
  123. // sort segments by ymax
  124. quick_sort(segments, [](const auto& line0, const auto& line1) {
  125. return line1.maximum_y < line0.maximum_y;
  126. });
  127. m_split_lines = move(segments);
  128. }
  129. }