Files
image/image/svg/path.sakura
T
2026-10-28 08:29:27 -05:00

200 lines
6.8 KiB
Plaintext

#! Contains paths: subpaths of lines and cubic curves, quadratics and arcs made
#! cubics, and the rectangles and ellipses SVG's shapes are made of.
import std::math;
module image::svg;
# What a path is made of: moves to begin each subpath, lines, cubic curves, each
# with its points after it, and closes.
enum verb: u8 {
move,
line,
cubic,
close,
}
# A path: subpaths of lines and cubic curves, quadratics and arcs made cubics.
struct path {
var verbs: vector<verb>;
var points: vector<point>;
var start: point; #! Where the subpath being made began.
var last: point; #! Where the path is.
var open: bool; #! Whether a subpath has begun, which a line or curve needs.
}
def path::new(): path {
return path { verbs = vector::<verb>::new(), points = vector::<point>::new() };
}
def path::move_to(&mut this, to: point): void {
this.verbs.append(verb::move);
this.points.append(to);
this.start = to;
this.last = to;
this.open = true;
}
def path::line_to(&mut this, to: point): void {
this.begin();
this.verbs.append(verb::line);
this.points.append(to);
this.last = to;
}
def path::cubic_to(&mut this, first: point, second: point, to: point): void {
this.begin();
this.verbs.append(verb::cubic);
this.points.append(first);
this.points.append(second);
this.points.append(to);
this.last = to;
}
def path::quad_to(&mut this, control: point, to: point): void {
val from := this.last;
this.cubic_to(
point { x = from.x + (control.x - from.x) * 2 / 3, y = from.y + (control.y - from.y) * 2 / 3 },
point { x = to.x + (control.x - to.x) * 2 / 3, y = to.y + (control.y - to.y) * 2 / 3 },
to
);
}
def path::close(&mut this): void {
if this.open {
this.verbs.append(verb::close);
this.last = this.start;
this.open = false;
}
}
# Begins a subpath where the path is, after a close, which a line or curve needs.
def path::begin(&mut this): void {
if !this.open {
this.move_to(this.last);
}
}
def path::is_empty(&this): bool {
return this.verbs.is_empty;
}
# Appends an elliptical arc to a point, as SVG's `A` gives it: radii, the
# ellipse's rotation, and which of the four arcs, made cubics of at most a
# quarter turn each, as SVG's implementation notes say.
def path::arc_to(&mut this, radius_x: f64, radius_y: f64, degrees: f64, large: bool, sweep: bool, to: point): void {
val from := this.last;
if from.x == to.x && from.y == to.y {
return;
}
var rx := math::abs(radius_x);
var ry := math::abs(radius_y);
if rx == 0 || ry == 0 {
this.line_to(to);
return;
}
val angle := degrees * math::pi / 180;
val cos := math::cos(angle);
val sin := math::sin(angle);
val half_x := (from.x - to.x) / 2;
val half_y := (from.y - to.y) / 2;
val x1 := cos * half_x + sin * half_y;
val y1 := -sin * half_x + cos * half_y;
# Radii too small to reach are made just large enough.
val reach := (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry);
if reach > 1 {
rx *= math::sqrt(reach);
ry *= math::sqrt(reach);
}
val numerator := rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1;
val denominator := rx * rx * y1 * y1 + ry * ry * x1 * x1;
var scale := denominator == 0 ? 0.0 : math::sqrt(math::max(0.0, numerator / denominator));
if large == sweep {
scale = -scale;
}
val center_x1 := scale * rx * y1 / ry;
val center_y1 := -scale * ry * x1 / rx;
val center := point {
x = cos * center_x1 - sin * center_y1 + (from.x + to.x) / 2,
y = sin * center_x1 + cos * center_y1 + (from.y + to.y) / 2
};
val begin := math::atan2((y1 - center_y1) / ry, (x1 - center_x1) / rx);
var turn := math::atan2((-y1 - center_y1) / ry, (-x1 - center_x1) / rx) - begin;
if sweep && turn < 0 {
turn += 2 * math::pi;
} else if !sweep && turn > 0 {
turn -= 2 * math::pi;
}
val pieces := math::ceil(math::abs(turn) / (math::pi / 2) - 1e-9);
val count := pieces < 1 ? 1u : pieces as u64;
val step := turn / count as f64;
val handle := 4.0 / 3.0 * math::tan(step / 4);
for index in 0..count {
val t1 := begin + step * index as f64;
val t2 := t1 + step;
val c1 := math::cos(t1);
val s1 := math::sin(t1);
val c2 := math::cos(t2);
val s2 := math::sin(t2);
var end := on_ellipse(&center, rx, ry, cos, sin, c2, s2);
if index == count - 1 {
end = to;
}
this.cubic_to(
on_ellipse(&center, rx, ry, cos, sin, c1 - handle * s1, s1 + handle * c1),
on_ellipse(&center, rx, ry, cos, sin, c2 + handle * s2, s2 - handle * c2),
end
);
}
}
# A point of the unit circle's plane, put on a rotated ellipse.
def on_ellipse(center: &point, rx: f64, ry: f64, cos: f64, sin: f64, x: f64, y: f64): point {
return point { x = center.x + rx * cos * x - ry * sin * y, y = center.y + rx * sin * x + ry * cos * y };
}
# The handles of a cubic making a quarter of a circle, for a radius of 1.
const kappa: f64 = 0.5522847498307936;
# Appends an ellipse, clockwise from its right.
def path::ellipse(&mut this, cx: f64, cy: f64, rx: f64, ry: f64): void {
val kx := rx * kappa;
val ky := ry * kappa;
this.move_to(point { x = cx + rx, y = cy });
this.cubic_to(point { x = cx + rx, y = cy + ky }, point { x = cx + kx, y = cy + ry }, point { x = cx, y = cy + ry });
this.cubic_to(point { x = cx - kx, y = cy + ry }, point { x = cx - rx, y = cy + ky }, point { x = cx - rx, y = cy });
this.cubic_to(point { x = cx - rx, y = cy - ky }, point { x = cx - kx, y = cy - ry }, point { x = cx, y = cy - ry });
this.cubic_to(point { x = cx + kx, y = cy - ry }, point { x = cx + rx, y = cy - ky }, point { x = cx + rx, y = cy });
this.close();
}
# Appends a rectangle, its corners rounded by radii, as SVG's `rect` is.
def path::rectangle(&mut this, x: f64, y: f64, width: f64, height: f64, rx: f64, ry: f64): void {
if rx <= 0 || ry <= 0 {
this.move_to(point { x = x, y = y });
this.line_to(point { x = x + width, y = y });
this.line_to(point { x = x + width, y = y + height });
this.line_to(point { x = x, y = y + height });
this.close();
return;
}
val kx := rx * kappa;
val ky := ry * kappa;
val right := x + width;
val bottom := y + height;
this.move_to(point { x = x + rx, y = y });
this.line_to(point { x = right - rx, y = y });
this.cubic_to(point { x = right - rx + kx, y = y }, point { x = right, y = y + ry - ky }, point { x = right, y = y + ry });
this.line_to(point { x = right, y = bottom - ry });
this.cubic_to(point { x = right, y = bottom - ry + ky }, point { x = right - rx + kx, y = bottom }, point { x = right - rx, y = bottom });
this.line_to(point { x = x + rx, y = bottom });
this.cubic_to(point { x = x + rx - kx, y = bottom }, point { x = x, y = bottom - ry + ky }, point { x = x, y = bottom - ry });
this.line_to(point { x = x, y = y + ry });
this.cubic_to(point { x = x, y = y + ry - ky }, point { x = x + rx - kx, y = y }, point { x = x + rx, y = y });
this.close();
}