1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:07:36 +00:00

LibGfx: Ensure last subpath is closed by Path::close_all_subpaths()

This commit is contained in:
MacDue 2023-06-24 16:51:17 +01:00 committed by Andreas Kling
parent 6b40271b95
commit 3d755a57b6

View file

@ -144,11 +144,7 @@ void Path::close_all_subpaths()
Optional<FloatPoint> cursor, start_of_subpath; Optional<FloatPoint> cursor, start_of_subpath;
bool is_first_point_in_subpath { false }; bool is_first_point_in_subpath { false };
auto segment_count = m_segments.size(); auto close_previous_subpath = [&] {
for (size_t i = 0; i < segment_count; i++) {
// Note: We need to use m_segments[i] as append_segment() may invalidate any references.
switch (m_segments[i]->type()) {
case Segment::Type::MoveTo: {
if (cursor.has_value() && !is_first_point_in_subpath) { if (cursor.has_value() && !is_first_point_in_subpath) {
// This is a move from a subpath to another // This is a move from a subpath to another
// connect the two ends of this subpath before // connect the two ends of this subpath before
@ -158,6 +154,14 @@ void Path::close_all_subpaths()
append_segment<MoveSegment>(cursor.value()); append_segment<MoveSegment>(cursor.value());
append_segment<LineSegment>(start_of_subpath.value()); append_segment<LineSegment>(start_of_subpath.value());
} }
};
auto segment_count = m_segments.size();
for (size_t i = 0; i < segment_count; i++) {
// Note: We need to use m_segments[i] as append_segment() may invalidate any references.
switch (m_segments[i]->type()) {
case Segment::Type::MoveTo: {
close_previous_subpath();
is_first_point_in_subpath = true; is_first_point_in_subpath = true;
cursor = m_segments[i]->point(); cursor = m_segments[i]->point();
break; break;
@ -177,6 +181,9 @@ void Path::close_all_subpaths()
break; break;
} }
} }
if (m_segments.last()->type() != Segment::Type::MoveTo)
close_previous_subpath();
} }
DeprecatedString Path::to_deprecated_string() const DeprecatedString Path::to_deprecated_string() const