1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

LibGfx: Approximate elliptical arcs with cubic beziers

Unlike all other primitives elliptical arcs are non-trivial to
manipulate, it's tricky to correctly apply a Gfx::AffineTransform to
them. Prior to this change, Path::copy_transformed() was still
incorrectly applying transforms such as flips and skews to arcs.

This patch very closely approximates arcs with cubic beziers (I can not
visually spot any differences), which can then be easily and correctly
transformed in all cases.

Most of the maths here was taken from:
https://mortoray.com/rendering-an-svg-elliptical-arc-as-bezier-curves/
(which came from https://www.joecridge.me/content/pdf/bezier-arcs.pdf,
now a dead link).
This commit is contained in:
MacDue 2023-07-15 15:59:33 +01:00 committed by Andreas Kling
parent 2a1bf63f9e
commit 1bc7b0320e
4 changed files with 54 additions and 101 deletions

View file

@ -25,7 +25,6 @@ public:
LineTo,
QuadraticBezierCurveTo,
CubicBezierCurveTo,
EllipticalArcTo,
};
Segment(FloatPoint point)
@ -105,42 +104,6 @@ private:
FloatPoint m_through_1;
};
class EllipticalArcSegment final : public Segment {
public:
EllipticalArcSegment(FloatPoint point, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, bool large_arc, bool sweep)
: Segment(point)
, m_center(center)
, m_radii(radii)
, m_x_axis_rotation(x_axis_rotation)
, m_theta_1(theta_1)
, m_theta_delta(theta_delta)
, m_large_arc(large_arc)
, m_sweep(sweep)
{
}
virtual ~EllipticalArcSegment() override = default;
FloatPoint center() const { return m_center; }
FloatSize radii() const { return m_radii; }
float x_axis_rotation() const { return m_x_axis_rotation; }
float theta_1() const { return m_theta_1; }
float theta_delta() const { return m_theta_delta; }
bool large_arc() const { return m_large_arc; }
bool sweep() const { return m_sweep; }
private:
virtual Type type() const override { return Segment::Type::EllipticalArcTo; }
FloatPoint m_center;
FloatSize m_radii;
float m_x_axis_rotation;
float m_theta_1;
float m_theta_delta;
bool m_large_arc;
bool m_sweep;
};
class Path {
public:
Path() = default;
@ -190,22 +153,6 @@ public:
elliptical_arc_to(point, { radius, radius }, 0, large_arc, sweep);
}
// Note: This does not do any sanity checks!
void elliptical_arc_to(FloatPoint endpoint, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta, float theta_delta, bool large_arc, bool sweep)
{
append_segment<EllipticalArcSegment>(
endpoint,
center,
radii,
x_axis_rotation,
theta,
theta_delta,
large_arc,
sweep);
invalidate_split_lines();
}
void close();
void close_all_subpaths();
@ -251,6 +198,8 @@ public:
Path stroke_to_fill(float thickness) const;
private:
void approximate_elliptical_arc_with_cubic_beziers(FloatPoint center, FloatSize radii, float x_axis_rotation, float theta, float theta_delta);
void invalidate_split_lines()
{
m_bounding_box.clear();