Path.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <AK/HashTable.h>
  8. #include <AK/Math.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/StringBuilder.h>
  11. #include <AK/TypeCasts.h>
  12. #include <LibGfx/BoundingBox.h>
  13. #include <LibGfx/Font/ScaledFont.h>
  14. #include <LibGfx/Painter.h>
  15. #include <LibGfx/Path.h>
  16. #include <LibGfx/TextLayout.h>
  17. namespace Gfx {
  18. void Path::approximate_elliptical_arc_with_cubic_beziers(FloatPoint center, FloatSize radii, float x_axis_rotation, float theta, float theta_delta)
  19. {
  20. float sin_x_rotation;
  21. float cos_x_rotation;
  22. AK::sincos(x_axis_rotation, sin_x_rotation, cos_x_rotation);
  23. auto arc_point_and_derivative = [&](float t, FloatPoint& point, FloatPoint& derivative) {
  24. float sin_angle;
  25. float cos_angle;
  26. AK::sincos(t, sin_angle, cos_angle);
  27. point = FloatPoint {
  28. center.x()
  29. + radii.width() * cos_x_rotation * cos_angle
  30. - radii.height() * sin_x_rotation * sin_angle,
  31. center.y()
  32. + radii.width() * sin_x_rotation * cos_angle
  33. + radii.height() * cos_x_rotation * sin_angle,
  34. };
  35. derivative = FloatPoint {
  36. -radii.width() * cos_x_rotation * sin_angle
  37. - radii.height() * sin_x_rotation * cos_angle,
  38. -radii.width() * sin_x_rotation * sin_angle
  39. + radii.height() * cos_x_rotation * cos_angle,
  40. };
  41. };
  42. auto approximate_arc_between = [&](float start_angle, float end_angle) {
  43. auto t = AK::tan((end_angle - start_angle) / 2);
  44. auto alpha = AK::sin(end_angle - start_angle) * ((AK::sqrt(4 + 3 * t * t) - 1) / 3);
  45. FloatPoint p1, d1;
  46. FloatPoint p2, d2;
  47. arc_point_and_derivative(start_angle, p1, d1);
  48. arc_point_and_derivative(end_angle, p2, d2);
  49. auto q1 = p1 + d1.scaled(alpha, alpha);
  50. auto q2 = p2 - d2.scaled(alpha, alpha);
  51. cubic_bezier_curve_to(q1, q2, p2);
  52. };
  53. // FIXME: Come up with a more mathematically sound step size (using some error calculation).
  54. auto step = theta_delta;
  55. int step_count = 1;
  56. while (fabs(step) > AK::Pi<float> / 4) {
  57. step /= 2;
  58. step_count *= 2;
  59. }
  60. float prev = theta;
  61. float t = prev + step;
  62. for (int i = 0; i < step_count; i++, prev = t, t += step)
  63. approximate_arc_between(prev, t);
  64. }
  65. void Path::elliptical_arc_to(FloatPoint point, FloatSize radii, float x_axis_rotation, bool large_arc, bool sweep)
  66. {
  67. auto next_point = point;
  68. double rx = radii.width();
  69. double ry = radii.height();
  70. double x_axis_rotation_s;
  71. double x_axis_rotation_c;
  72. AK::sincos(static_cast<double>(x_axis_rotation), x_axis_rotation_s, x_axis_rotation_c);
  73. FloatPoint last_point = this->last_point();
  74. // Step 1 of out-of-range radii correction
  75. if (rx == 0.0 || ry == 0.0) {
  76. append_segment<LineSegment>(next_point);
  77. return;
  78. }
  79. // Step 2 of out-of-range radii correction
  80. if (rx < 0)
  81. rx *= -1.0;
  82. if (ry < 0)
  83. ry *= -1.0;
  84. // POSSIBLY HACK: Handle the case where both points are the same.
  85. auto same_endpoints = next_point == last_point;
  86. if (same_endpoints) {
  87. if (!large_arc) {
  88. // Nothing is going to be drawn anyway.
  89. return;
  90. }
  91. // Move the endpoint by a small amount to avoid division by zero.
  92. next_point.translate_by(0.01f, 0.01f);
  93. }
  94. // Find (cx, cy), theta_1, theta_delta
  95. // Step 1: Compute (x1', y1')
  96. auto x_avg = static_cast<double>(last_point.x() - next_point.x()) / 2.0;
  97. auto y_avg = static_cast<double>(last_point.y() - next_point.y()) / 2.0;
  98. auto x1p = x_axis_rotation_c * x_avg + x_axis_rotation_s * y_avg;
  99. auto y1p = -x_axis_rotation_s * x_avg + x_axis_rotation_c * y_avg;
  100. // Step 2: Compute (cx', cy')
  101. double x1p_sq = x1p * x1p;
  102. double y1p_sq = y1p * y1p;
  103. double rx_sq = rx * rx;
  104. double ry_sq = ry * ry;
  105. // Step 3 of out-of-range radii correction
  106. double lambda = x1p_sq / rx_sq + y1p_sq / ry_sq;
  107. double multiplier;
  108. if (lambda > 1.0) {
  109. auto lambda_sqrt = AK::sqrt(lambda);
  110. rx *= lambda_sqrt;
  111. ry *= lambda_sqrt;
  112. multiplier = 0.0;
  113. } else {
  114. double numerator = rx_sq * ry_sq - rx_sq * y1p_sq - ry_sq * x1p_sq;
  115. double denominator = rx_sq * y1p_sq + ry_sq * x1p_sq;
  116. multiplier = AK::sqrt(AK::max(0., numerator) / denominator);
  117. }
  118. if (large_arc == sweep)
  119. multiplier *= -1.0;
  120. double cxp = multiplier * rx * y1p / ry;
  121. double cyp = multiplier * -ry * x1p / rx;
  122. // Step 3: Compute (cx, cy) from (cx', cy')
  123. x_avg = (last_point.x() + next_point.x()) / 2.0f;
  124. y_avg = (last_point.y() + next_point.y()) / 2.0f;
  125. double cx = x_axis_rotation_c * cxp - x_axis_rotation_s * cyp + x_avg;
  126. double cy = x_axis_rotation_s * cxp + x_axis_rotation_c * cyp + y_avg;
  127. double theta_1 = AK::atan2((y1p - cyp) / ry, (x1p - cxp) / rx);
  128. double theta_2 = AK::atan2((-y1p - cyp) / ry, (-x1p - cxp) / rx);
  129. auto theta_delta = theta_2 - theta_1;
  130. if (!sweep && theta_delta > 0.0) {
  131. theta_delta -= 2 * AK::Pi<double>;
  132. } else if (sweep && theta_delta < 0) {
  133. theta_delta += 2 * AK::Pi<double>;
  134. }
  135. approximate_elliptical_arc_with_cubic_beziers(
  136. { cx, cy },
  137. { rx, ry },
  138. x_axis_rotation,
  139. theta_1,
  140. theta_delta);
  141. }
  142. void Path::text(Utf8View text, Font const& font)
  143. {
  144. if (!is<ScaledFont>(font)) {
  145. // FIXME: This API only accepts Gfx::Font for ease of use.
  146. dbgln("Cannot path-ify bitmap fonts!");
  147. return;
  148. }
  149. auto& scaled_font = static_cast<ScaledFont const&>(font);
  150. auto font_list = Gfx::FontCascadeList::create();
  151. font_list->add(scaled_font);
  152. for_each_glyph_position(
  153. last_point(), text, font_list, [&](DrawGlyphOrEmoji glyph_or_emoji) {
  154. if (glyph_or_emoji.has<DrawGlyph>()) {
  155. auto& glyph = glyph_or_emoji.get<DrawGlyph>();
  156. move_to(glyph.position);
  157. auto glyph_id = scaled_font.glyph_id_for_code_point(glyph.code_point);
  158. scaled_font.append_glyph_path_to(*this, glyph_id);
  159. }
  160. },
  161. IncludeLeftBearing::Yes);
  162. }
  163. Path Path::place_text_along(Utf8View text, Font const& font) const
  164. {
  165. if (!is<ScaledFont>(font)) {
  166. // FIXME: This API only accepts Gfx::Font for ease of use.
  167. dbgln("Cannot path-ify bitmap fonts!");
  168. return {};
  169. }
  170. auto& lines = split_lines();
  171. auto next_point_for_offset = [&, line_index = 0U, distance_along_path = 0.0f, last_line_length = 0.0f](float offset) mutable -> Optional<FloatPoint> {
  172. while (line_index < lines.size() && offset > distance_along_path) {
  173. last_line_length = lines[line_index++].length();
  174. distance_along_path += last_line_length;
  175. }
  176. if (offset > distance_along_path)
  177. return {};
  178. if (last_line_length > 1) {
  179. // If the last line segment was fairly long, compute the point in the line.
  180. float p = (last_line_length + offset - distance_along_path) / last_line_length;
  181. auto current_line = lines[line_index - 1];
  182. return current_line.a() + (current_line.b() - current_line.a()).scaled(p);
  183. }
  184. if (line_index >= lines.size())
  185. return {};
  186. return lines[line_index].a();
  187. };
  188. auto font_list = Gfx::FontCascadeList::create();
  189. font_list->add(font);
  190. auto& scaled_font = static_cast<Gfx::ScaledFont const&>(font);
  191. Gfx::Path result_path;
  192. Gfx::for_each_glyph_position(
  193. {}, text, font_list, [&](Gfx::DrawGlyphOrEmoji glyph_or_emoji) {
  194. auto* glyph = glyph_or_emoji.get_pointer<Gfx::DrawGlyph>();
  195. if (!glyph)
  196. return;
  197. auto offset = glyph->position.x();
  198. auto width = font.glyph_width(glyph->code_point);
  199. auto start = next_point_for_offset(offset);
  200. if (!start.has_value())
  201. return;
  202. auto end = next_point_for_offset(offset + width);
  203. if (!end.has_value())
  204. return;
  205. // Find the angle between the start and end points on the path.
  206. auto delta = *end - *start;
  207. auto angle = AK::atan2(delta.y(), delta.x());
  208. Gfx::Path glyph_path;
  209. // Rotate the glyph then move it to start point.
  210. auto glyph_id = scaled_font.glyph_id_for_code_point(glyph->code_point);
  211. scaled_font.append_glyph_path_to(glyph_path, glyph_id);
  212. auto transform = Gfx::AffineTransform {}
  213. .translate(*start)
  214. .multiply(Gfx::AffineTransform {}.rotate_radians(angle))
  215. .multiply(Gfx::AffineTransform {}.translate({ 0, -scaled_font.pixel_metrics().ascent }));
  216. glyph_path = glyph_path.copy_transformed(transform);
  217. result_path.append_path(glyph_path);
  218. },
  219. Gfx::IncludeLeftBearing::Yes);
  220. return result_path;
  221. }
  222. FloatPoint Path::last_point()
  223. {
  224. FloatPoint last_point { 0, 0 };
  225. if (!m_segments.is_empty())
  226. last_point = m_segments.last()->point();
  227. return last_point;
  228. }
  229. void Path::close()
  230. {
  231. if (m_segments.size() <= 1)
  232. return;
  233. auto last_point = m_segments.last()->point();
  234. for (ssize_t i = m_segments.size() - 1; i >= 0; --i) {
  235. auto& segment = m_segments[i];
  236. if (segment->type() == Segment::Type::MoveTo) {
  237. if (last_point == segment->point())
  238. return;
  239. append_segment<LineSegment>(segment->point());
  240. invalidate_split_lines();
  241. return;
  242. }
  243. }
  244. }
  245. void Path::close_all_subpaths()
  246. {
  247. if (m_segments.size() <= 1)
  248. return;
  249. invalidate_split_lines();
  250. Optional<FloatPoint> cursor, start_of_subpath;
  251. bool is_first_point_in_subpath { false };
  252. auto close_previous_subpath = [&] {
  253. if (cursor.has_value() && !is_first_point_in_subpath) {
  254. // This is a move from a subpath to another
  255. // connect the two ends of this subpath before
  256. // moving on to the next one
  257. VERIFY(start_of_subpath.has_value());
  258. append_segment<MoveSegment>(cursor.value());
  259. append_segment<LineSegment>(start_of_subpath.value());
  260. }
  261. };
  262. auto segment_count = m_segments.size();
  263. for (size_t i = 0; i < segment_count; i++) {
  264. // Note: We need to use m_segments[i] as append_segment() may invalidate any references.
  265. switch (m_segments[i]->type()) {
  266. case Segment::Type::MoveTo: {
  267. close_previous_subpath();
  268. is_first_point_in_subpath = true;
  269. cursor = m_segments[i]->point();
  270. break;
  271. }
  272. case Segment::Type::LineTo:
  273. case Segment::Type::QuadraticBezierCurveTo:
  274. case Segment::Type::CubicBezierCurveTo:
  275. if (is_first_point_in_subpath) {
  276. start_of_subpath = cursor;
  277. is_first_point_in_subpath = false;
  278. }
  279. cursor = m_segments[i]->point();
  280. break;
  281. case Segment::Type::Invalid:
  282. VERIFY_NOT_REACHED();
  283. break;
  284. }
  285. }
  286. if (m_segments.last()->type() != Segment::Type::MoveTo)
  287. close_previous_subpath();
  288. }
  289. ByteString Path::to_byte_string() const
  290. {
  291. StringBuilder builder;
  292. builder.append("Path { "sv);
  293. for (auto& segment : m_segments) {
  294. switch (segment->type()) {
  295. case Segment::Type::MoveTo:
  296. builder.append("MoveTo"sv);
  297. break;
  298. case Segment::Type::LineTo:
  299. builder.append("LineTo"sv);
  300. break;
  301. case Segment::Type::QuadraticBezierCurveTo:
  302. builder.append("QuadraticBezierCurveTo"sv);
  303. break;
  304. case Segment::Type::CubicBezierCurveTo:
  305. builder.append("CubicBezierCurveTo"sv);
  306. break;
  307. case Segment::Type::Invalid:
  308. builder.append("Invalid"sv);
  309. break;
  310. }
  311. builder.appendff("({}", segment->point());
  312. switch (segment->type()) {
  313. case Segment::Type::QuadraticBezierCurveTo:
  314. builder.append(", "sv);
  315. builder.append(static_cast<QuadraticBezierCurveSegment const&>(*segment).through().to_byte_string());
  316. break;
  317. case Segment::Type::CubicBezierCurveTo:
  318. builder.append(", "sv);
  319. builder.append(static_cast<CubicBezierCurveSegment const&>(*segment).through_0().to_byte_string());
  320. builder.append(", "sv);
  321. builder.append(static_cast<CubicBezierCurveSegment const&>(*segment).through_1().to_byte_string());
  322. break;
  323. default:
  324. break;
  325. }
  326. builder.append(") "sv);
  327. }
  328. builder.append('}');
  329. return builder.to_byte_string();
  330. }
  331. void Path::segmentize_path()
  332. {
  333. Vector<FloatLine> segments;
  334. FloatBoundingBox bounding_box;
  335. auto add_line = [&](auto const& p0, auto const& p1) {
  336. segments.append({ p0, p1 });
  337. bounding_box.add_point(p1);
  338. };
  339. FloatPoint cursor { 0, 0 };
  340. for (auto& segment : m_segments) {
  341. switch (segment->type()) {
  342. case Segment::Type::MoveTo:
  343. bounding_box.add_point(segment->point());
  344. cursor = segment->point();
  345. break;
  346. case Segment::Type::LineTo: {
  347. add_line(cursor, segment->point());
  348. cursor = segment->point();
  349. break;
  350. }
  351. case Segment::Type::QuadraticBezierCurveTo: {
  352. auto control = static_cast<QuadraticBezierCurveSegment const&>(*segment).through();
  353. Painter::for_each_line_segment_on_bezier_curve(control, cursor, segment->point(), [&](FloatPoint p0, FloatPoint p1) {
  354. add_line(p0, p1);
  355. });
  356. cursor = segment->point();
  357. break;
  358. }
  359. case Segment::Type::CubicBezierCurveTo: {
  360. auto& curve = static_cast<CubicBezierCurveSegment const&>(*segment);
  361. auto control_0 = curve.through_0();
  362. auto control_1 = curve.through_1();
  363. Painter::for_each_line_segment_on_cubic_bezier_curve(control_0, control_1, cursor, segment->point(), [&](FloatPoint p0, FloatPoint p1) {
  364. add_line(p0, p1);
  365. });
  366. cursor = segment->point();
  367. break;
  368. }
  369. case Segment::Type::Invalid:
  370. VERIFY_NOT_REACHED();
  371. }
  372. }
  373. m_split_lines = move(segments);
  374. m_bounding_box = bounding_box;
  375. }
  376. Path Path::copy_transformed(Gfx::AffineTransform const& transform) const
  377. {
  378. Path result;
  379. for (auto const& segment : m_segments) {
  380. switch (segment->type()) {
  381. case Segment::Type::MoveTo:
  382. result.move_to(transform.map(segment->point()));
  383. break;
  384. case Segment::Type::LineTo: {
  385. result.line_to(transform.map(segment->point()));
  386. break;
  387. }
  388. case Segment::Type::QuadraticBezierCurveTo: {
  389. auto const& quadratic_segment = static_cast<QuadraticBezierCurveSegment const&>(*segment);
  390. result.quadratic_bezier_curve_to(transform.map(quadratic_segment.through()), transform.map(segment->point()));
  391. break;
  392. }
  393. case Segment::Type::CubicBezierCurveTo: {
  394. auto const& cubic_segment = static_cast<CubicBezierCurveSegment const&>(*segment);
  395. result.cubic_bezier_curve_to(transform.map(cubic_segment.through_0()), transform.map(cubic_segment.through_1()), transform.map(segment->point()));
  396. break;
  397. }
  398. case Segment::Type::Invalid:
  399. VERIFY_NOT_REACHED();
  400. }
  401. }
  402. return result;
  403. }
  404. void Path::add_path(Path const& other)
  405. {
  406. m_segments.extend(other.m_segments);
  407. invalidate_split_lines();
  408. }
  409. void Path::ensure_subpath(FloatPoint point)
  410. {
  411. if (m_need_new_subpath && m_segments.is_empty()) {
  412. move_to(point);
  413. m_need_new_subpath = false;
  414. }
  415. }
  416. template<typename T>
  417. struct RoundTrip {
  418. RoundTrip(ReadonlySpan<T> span)
  419. : m_span(span)
  420. {
  421. }
  422. size_t size() const
  423. {
  424. return m_span.size() * 2 - 1;
  425. }
  426. T const& operator[](size_t index) const
  427. {
  428. // Follow the path:
  429. if (index < m_span.size())
  430. return m_span[index];
  431. // Then in reverse:
  432. if (index < size())
  433. return m_span[size() - index - 1];
  434. // Then wrap around again:
  435. return m_span[index - size() + 1];
  436. }
  437. private:
  438. ReadonlySpan<T> m_span;
  439. };
  440. Path Path::stroke_to_fill(float thickness) const
  441. {
  442. // Note: This convolves a polygon with the path using the algorithm described
  443. // in https://keithp.com/~keithp/talks/cairo2003.pdf (3.1 Stroking Splines via Convolution)
  444. VERIFY(thickness > 0);
  445. auto& lines = split_lines();
  446. if (lines.is_empty())
  447. return Path {};
  448. // Paths can be disconnected, which a pain to deal with, so split it up.
  449. Vector<Vector<FloatPoint>> segments;
  450. segments.append({ lines.first().a() });
  451. for (auto& line : lines) {
  452. if (line.a() == segments.last().last()) {
  453. segments.last().append(line.b());
  454. } else {
  455. segments.append({ line.a(), line.b() });
  456. }
  457. }
  458. constexpr auto flatness = 0.15f;
  459. auto pen_vertex_count = 4;
  460. if (thickness > flatness) {
  461. pen_vertex_count = max(
  462. static_cast<int>(ceilf(AK::Pi<float>
  463. / acosf(1 - (2 * flatness) / thickness))),
  464. pen_vertex_count);
  465. }
  466. if (pen_vertex_count % 2 == 1)
  467. pen_vertex_count += 1;
  468. Vector<FloatPoint, 128> pen_vertices;
  469. pen_vertices.ensure_capacity(pen_vertex_count);
  470. // Generate vertices for the pen (going counterclockwise). The pen does not necessarily need
  471. // to be a circle (or an approximation of one), but other shapes are untested.
  472. float theta = 0;
  473. float theta_delta = (AK::Pi<float> * 2) / pen_vertex_count;
  474. for (int i = 0; i < pen_vertex_count; i++) {
  475. float sin_theta;
  476. float cos_theta;
  477. AK::sincos(theta, sin_theta, cos_theta);
  478. pen_vertices.unchecked_append({ cos_theta * thickness / 2, sin_theta * thickness / 2 });
  479. theta -= theta_delta;
  480. }
  481. auto wrapping_index = [](auto& vertices, auto index) {
  482. return vertices[(index + vertices.size()) % vertices.size()];
  483. };
  484. auto angle_between = [](auto p1, auto p2) {
  485. auto delta = p2 - p1;
  486. return atan2f(delta.y(), delta.x());
  487. };
  488. struct ActiveRange {
  489. float start;
  490. float end;
  491. bool in_range(float angle) const
  492. {
  493. // Note: Since active ranges go counterclockwise start > end unless we wrap around at 180 degrees
  494. return ((angle <= start && angle >= end)
  495. || (start < end && angle <= start)
  496. || (start < end && angle >= end));
  497. }
  498. };
  499. Vector<ActiveRange, 128> active_ranges;
  500. active_ranges.ensure_capacity(pen_vertices.size());
  501. for (auto i = 0; i < pen_vertex_count; i++) {
  502. active_ranges.unchecked_append({ angle_between(wrapping_index(pen_vertices, i - 1), pen_vertices[i]),
  503. angle_between(pen_vertices[i], wrapping_index(pen_vertices, i + 1)) });
  504. }
  505. auto clockwise = [](float current_angle, float target_angle) {
  506. if (target_angle < 0)
  507. target_angle += AK::Pi<float> * 2;
  508. if (current_angle < 0)
  509. current_angle += AK::Pi<float> * 2;
  510. if (target_angle < current_angle)
  511. target_angle += AK::Pi<float> * 2;
  512. return (target_angle - current_angle) <= AK::Pi<float>;
  513. };
  514. Path convolution;
  515. for (auto& segment : segments) {
  516. RoundTrip<FloatPoint> shape { segment };
  517. bool first = true;
  518. auto add_vertex = [&](auto v) {
  519. if (first) {
  520. convolution.move_to(v);
  521. first = false;
  522. } else {
  523. convolution.line_to(v);
  524. }
  525. };
  526. auto shape_idx = 0u;
  527. auto slope = [&] {
  528. return angle_between(shape[shape_idx], shape[shape_idx + 1]);
  529. };
  530. auto start_slope = slope();
  531. // Note: At least one range must be active.
  532. auto active = *active_ranges.find_first_index_if([&](auto& range) {
  533. return range.in_range(start_slope);
  534. });
  535. while (shape_idx < shape.size()) {
  536. add_vertex(shape[shape_idx] + pen_vertices[active]);
  537. auto slope_now = slope();
  538. auto range = active_ranges[active];
  539. if (range.in_range(slope_now)) {
  540. shape_idx++;
  541. } else {
  542. if (clockwise(slope_now, range.end)) {
  543. if (active == static_cast<size_t>(pen_vertex_count - 1))
  544. active = 0;
  545. else
  546. active++;
  547. } else {
  548. if (active == 0)
  549. active = pen_vertex_count - 1;
  550. else
  551. active--;
  552. }
  553. }
  554. }
  555. }
  556. return convolution;
  557. }
  558. }