LibGfx: Add method for copying a Path with a transform applied

The Web's CanvasRenderingContext2D needs a way to draw a path with a
transform applied to it, without modifying the original path, so here
it is. :^)
This commit is contained in:
Sam Atkins 2022-08-11 17:38:35 +01:00 committed by Andreas Kling
parent 2ec52bbbd5
commit 389f1ee6f5
Notes: sideshowbarker 2024-07-17 18:23:22 +09:00
2 changed files with 46 additions and 0 deletions

View File

@ -332,4 +332,48 @@ void Path::segmentize_path()
m_bounding_box = Gfx::FloatRect { min_x, min_y, max_x - min_x, max_y - min_y };
}
Path Path::copy_transformed(Gfx::AffineTransform const& transform) const
{
Path result;
for (auto const& segment : m_segments) {
switch (segment.type()) {
case Segment::Type::MoveTo:
result.move_to(transform.map(segment.point()));
break;
case Segment::Type::LineTo: {
result.line_to(transform.map(segment.point()));
break;
}
case Segment::Type::QuadraticBezierCurveTo: {
auto const& quadratic_segment = static_cast<QuadraticBezierCurveSegment const&>(segment);
result.quadratic_bezier_curve_to(transform.map(quadratic_segment.through()), transform.map(segment.point()));
break;
}
case Segment::Type::CubicBezierCurveTo: {
auto const& cubic_segment = static_cast<CubicBezierCurveSegment const&>(segment);
result.cubic_bezier_curve_to(transform.map(cubic_segment.through_0()), transform.map(cubic_segment.through_1()), transform.map(segment.point()));
break;
}
case Segment::Type::EllipticalArcTo: {
auto const& arc_segment = static_cast<EllipticalArcSegment const&>(segment);
result.elliptical_arc_to(
transform.map(segment.point()),
transform.map(arc_segment.center()),
transform.map(arc_segment.radii()),
arc_segment.x_axis_rotation(),
arc_segment.theta_1(),
arc_segment.theta_delta(),
arc_segment.large_arc(),
arc_segment.sweep());
break;
}
case Segment::Type::Invalid:
VERIFY_NOT_REACHED();
}
}
return result;
}
}

View File

@ -243,6 +243,8 @@ public:
return m_bounding_box.value();
}
Path copy_transformed(AffineTransform const&) const;
String to_string() const;
private: