diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-05-10 01:46:09 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-09 23:25:39 +0200 |
commit | 6f15f23a40c5af7cc8b2225f0f794817d643f8b0 (patch) | |
tree | 7e049932e3bb97556e608c76ccda5f07e7cc0078 /Libraries/LibGfx | |
parent | c3aa249a36803be3b3159ba63b9ddc13ca06ac84 (diff) | |
download | serenity-6f15f23a40c5af7cc8b2225f0f794817d643f8b0.zip |
LibGfx: Add Path::close_all_subpaths()
Unlike Path::close() which closes only the last subpath (if possible),
this closure mechanism closes _all_ available subpaths.
Diffstat (limited to 'Libraries/LibGfx')
-rw-r--r-- | Libraries/LibGfx/Path.cpp | 41 | ||||
-rw-r--r-- | Libraries/LibGfx/Path.h | 1 |
2 files changed, 42 insertions, 0 deletions
diff --git a/Libraries/LibGfx/Path.cpp b/Libraries/LibGfx/Path.cpp index 0caf8a9461..12d2a63d47 100644 --- a/Libraries/LibGfx/Path.cpp +++ b/Libraries/LibGfx/Path.cpp @@ -55,6 +55,47 @@ void Path::close() } } +void Path::close_all_subpaths() +{ + if (m_segments.size() <= 1) + return; + + invalidate_split_lines(); + + Optional<FloatPoint> cursor, start_of_subpath; + bool is_first_point_in_subpath { false }; + + for (auto& segment : m_segments) { + switch (segment.type) { + case Segment::Type::MoveTo: { + if (cursor.has_value() && !is_first_point_in_subpath) { + // This is a move from a subpath to another + // connect the two ends of this subpath before + // moving on to the next one + ASSERT(start_of_subpath.has_value()); + + m_segments.append({ Segment::Type::MoveTo, cursor.value() }); + m_segments.append({ Segment::Type::LineTo, start_of_subpath.value() }); + } + is_first_point_in_subpath = true; + cursor = segment.point; + break; + } + case Segment::Type::LineTo: + case Segment::Type::QuadraticBezierCurveTo: + if (is_first_point_in_subpath) { + start_of_subpath = cursor; + is_first_point_in_subpath = false; + } + cursor = segment.point; + break; + case Segment::Type::Invalid: + ASSERT_NOT_REACHED(); + break; + } + } +} + String Path::to_string() const { StringBuilder builder; diff --git a/Libraries/LibGfx/Path.h b/Libraries/LibGfx/Path.h index 5d52fa14c3..69ecc7ac0a 100644 --- a/Libraries/LibGfx/Path.h +++ b/Libraries/LibGfx/Path.h @@ -69,6 +69,7 @@ public: } void close(); + void close_all_subpaths(); struct LineSegment { FloatPoint from, to; |