Path.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/HashFunctions.h>
  28. #include <AK/HashTable.h>
  29. #include <AK/QuickSort.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibGfx/Painter.h>
  32. #include <LibGfx/Path.h>
  33. #include <math.h>
  34. namespace Gfx {
  35. void Path::close()
  36. {
  37. if (m_segments.size() <= 1)
  38. return;
  39. invalidate_split_lines();
  40. auto& last_point = m_segments.last().point;
  41. for (ssize_t i = m_segments.size() - 1; i >= 0; --i) {
  42. auto& segment = m_segments[i];
  43. if (segment.type == Segment::Type::MoveTo) {
  44. if (last_point == segment.point)
  45. return;
  46. m_segments.append({ Segment::Type::LineTo, segment.point });
  47. return;
  48. }
  49. }
  50. }
  51. String Path::to_string() const
  52. {
  53. StringBuilder builder;
  54. builder.append("Path { ");
  55. for (auto& segment : m_segments) {
  56. switch (segment.type) {
  57. case Segment::Type::MoveTo:
  58. builder.append("MoveTo");
  59. break;
  60. case Segment::Type::LineTo:
  61. builder.append("LineTo");
  62. break;
  63. case Segment::Type::QuadraticBezierCurveTo:
  64. builder.append("QuadraticBezierCurveTo");
  65. break;
  66. case Segment::Type::Invalid:
  67. builder.append("Invalid");
  68. break;
  69. }
  70. builder.append('(');
  71. builder.append(segment.point.to_string());
  72. if (segment.through.has_value()) {
  73. builder.append(", ");
  74. builder.append(segment.through.value().to_string());
  75. }
  76. builder.append(')');
  77. builder.append(' ');
  78. }
  79. builder.append("}");
  80. return builder.to_string();
  81. }
  82. void Path::segmentize_path()
  83. {
  84. Vector<LineSegment> segments;
  85. auto add_line = [&](const auto& p0, const auto& p1) {
  86. float ymax = p0.y(), ymin = p1.y(), x_of_ymin = p1.x(), x_of_ymax = p0.x();
  87. auto slope = p0.x() == p1.x() ? 0 : ((float)(p0.y() - p1.y())) / ((float)(p0.x() - p1.x()));
  88. if (p0.y() < p1.y()) {
  89. ymin = ymax;
  90. ymax = p1.y();
  91. x_of_ymax = x_of_ymin;
  92. x_of_ymin = p0.x();
  93. }
  94. segments.append({ FloatPoint(p0.x(), p0.y()),
  95. FloatPoint(p1.x(), p1.y()),
  96. slope == 0 ? 0 : 1 / slope,
  97. x_of_ymin,
  98. ymax, ymin, x_of_ymax });
  99. };
  100. FloatPoint cursor { 0, 0 };
  101. for (auto& segment : m_segments) {
  102. switch (segment.type) {
  103. case Segment::Type::MoveTo:
  104. cursor = segment.point;
  105. break;
  106. case Segment::Type::LineTo: {
  107. add_line(cursor, segment.point);
  108. cursor = segment.point;
  109. break;
  110. }
  111. case Segment::Type::QuadraticBezierCurveTo: {
  112. auto& control = segment.through.value();
  113. Painter::for_each_line_segment_on_bezier_curve(control, cursor, segment.point, [&](const FloatPoint& p0, const FloatPoint& p1) {
  114. add_line(p0, p1);
  115. });
  116. cursor = segment.point;
  117. break;
  118. }
  119. case Segment::Type::Invalid:
  120. ASSERT_NOT_REACHED();
  121. break;
  122. }
  123. }
  124. // sort segments by ymax
  125. quick_sort(segments, [](const auto& line0, const auto& line1) {
  126. return line1.maximum_y < line0.maximum_y;
  127. });
  128. m_split_lines = move(segments);
  129. }
  130. }