LibGfx: Output an SVG compatible string from Path::to_byte_string()

This is much more useful than the previous format, as you can now just
paste the path into a site like https://svg-path-visualizer.netlify.app/
to debug issues.
This commit is contained in:
MacDue 2024-03-19 12:31:36 +00:00 committed by Tim Flynn
parent 0d7107e1d3
commit d7e2894e57
2 changed files with 12 additions and 21 deletions

View file

@ -291,40 +291,30 @@ void Path::close_all_subpaths()
ByteString Path::to_byte_string() const
{
// Dumps this path as an SVG compatible string.
StringBuilder builder;
builder.append("Path { "sv);
if (is_empty() || m_commands.first() != PathSegment::MoveTo)
builder.append("M 0,0"sv);
for (auto segment : *this) {
if (!builder.is_empty())
builder.append(' ');
switch (segment.command()) {
case PathSegment::MoveTo:
builder.append("MoveTo"sv);
builder.append('M');
break;
case PathSegment::LineTo:
builder.append("LineTo"sv);
builder.append('L');
break;
case PathSegment::QuadraticBezierCurveTo:
builder.append("QuadraticBezierCurveTo"sv);
builder.append('Q');
break;
case PathSegment::CubicBezierCurveTo:
builder.append("CubicBezierCurveTo"sv);
builder.append('C');
break;
}
builder.appendff("({}", segment.point());
switch (segment.command()) {
case PathSegment::QuadraticBezierCurveTo:
builder.appendff(", {}"sv, segment.through());
break;
case PathSegment::CubicBezierCurveTo:
builder.appendff(", {}"sv, segment.through_0());
builder.appendff(", {}"sv, segment.through_1());
break;
default:
break;
}
builder.append(") "sv);
for (auto point : segment.points())
builder.appendff(" {},{}", point.x(), point.y());
}
builder.append('}');
return builder.to_byte_string();
}

View file

@ -44,6 +44,7 @@ public:
VERIFY(m_command == Command::CubicBezierCurveTo);
return m_points[1];
}
ALWAYS_INLINE ReadonlySpan<FloatPoint> points() const { return m_points; }
static constexpr int points_per_command(Command command)
{