156 lines
4.3 KiB
Plaintext
156 lines
4.3 KiB
Plaintext
#! Contains outlines: the polygons and polylines paths are flattened to, and
|
|
#! strokes made, before they're filled.
|
|
|
|
import std::math;
|
|
module image::svg;
|
|
|
|
|
|
# How far, in pixels, a flattened curve may stray from the curve.
|
|
const tolerance: f64 = 0.2;
|
|
|
|
# A polygon or polyline of an outline, by its points in the outline's.
|
|
struct contour {
|
|
var start: u64;
|
|
var count: u64;
|
|
var closed: bool;
|
|
}
|
|
|
|
# Polygons and polylines, as paths are flattened to, and strokes made.
|
|
struct outline {
|
|
var points: vector<point>;
|
|
var contours: vector<contour>;
|
|
var begun: u64; #! Where the contour being added began.
|
|
}
|
|
|
|
def outline::new(): outline {
|
|
return outline { points = vector::<point>::new(), contours = vector::<contour>::new() };
|
|
}
|
|
|
|
def outline::begin(&mut this): void {
|
|
this.begun = this.points.length;
|
|
}
|
|
|
|
def outline::add(&mut this, at: point): void {
|
|
this.points.append(at);
|
|
}
|
|
|
|
# Ends the contour being added, keeping it where it has a point.
|
|
def outline::finish(&mut this, closed: bool): void {
|
|
if this.points.length > this.begun {
|
|
this.contours.append(contour { start = this.begun, count = this.points.length - this.begun, closed = closed });
|
|
}
|
|
this.begun = this.points.length;
|
|
}
|
|
|
|
# Adds a closed polygon, its points turning the same way as every other the
|
|
# stroker adds, so filling them all by the nonzero rule fills where any is.
|
|
def outline::polygon(&mut this, corners: span<point>): void {
|
|
var area: f64 = 0;
|
|
for index in 0..corners.length {
|
|
val a := corners[index];
|
|
val b := corners[(index + 1) % corners.length];
|
|
area += a.x * b.y - b.x * a.y;
|
|
}
|
|
this.begin();
|
|
if area >= 0 {
|
|
for corner in corners {
|
|
this.add(corner);
|
|
}
|
|
} else {
|
|
var index := corners.length;
|
|
while index > 0 {
|
|
index--;
|
|
this.add(corners[index]);
|
|
}
|
|
}
|
|
this.finish(true);
|
|
}
|
|
|
|
# The outline with each point transformed.
|
|
def outline::transformed(&this, by: &matrix): outline {
|
|
var made := outline::new();
|
|
for at in this.points {
|
|
made.points.append(by.apply(at));
|
|
}
|
|
for held in this.contours {
|
|
made.contours.append(held);
|
|
}
|
|
return made;
|
|
}
|
|
|
|
# The smallest rectangle holding every point, as its least and greatest corners.
|
|
def outline::bounds(&this): point[2] {
|
|
if this.points.is_empty {
|
|
return [point {}, point {}];
|
|
}
|
|
var low := this.points[0];
|
|
var high := low;
|
|
for at in this.points {
|
|
low.x = at.x < low.x ? at.x : low.x;
|
|
low.y = at.y < low.y ? at.y : low.y;
|
|
high.x = at.x > high.x ? at.x : high.x;
|
|
high.y = at.y > high.y ? at.y : high.y;
|
|
}
|
|
return [low, high];
|
|
}
|
|
|
|
# Flattens a path, transformed, into polylines: each curve into as many lines
|
|
# as keep it within a distance, by Wang's formula.
|
|
def flatten(shape: &path, by: &matrix, within: f64): outline {
|
|
var made := outline::new();
|
|
var open := false;
|
|
var at: u64 = 0;
|
|
var last := point {};
|
|
for each in shape.verbs {
|
|
match each {
|
|
move => {
|
|
if open {
|
|
made.finish(false);
|
|
}
|
|
made.begin();
|
|
last = by.apply(shape.points[at]);
|
|
made.add(last);
|
|
at++;
|
|
open = true;
|
|
}
|
|
line => {
|
|
last = by.apply(shape.points[at]);
|
|
made.add(last);
|
|
at++;
|
|
}
|
|
cubic => {
|
|
val p0 := last;
|
|
val p1 := by.apply(shape.points[at]);
|
|
val p2 := by.apply(shape.points[at + 1]);
|
|
val p3 := by.apply(shape.points[at + 2]);
|
|
at += 3;
|
|
val bend1 := math::hypot(p0.x - 2 * p1.x + p2.x, p0.y - 2 * p1.y + p2.y);
|
|
val bend2 := math::hypot(p1.x - 2 * p2.x + p3.x, p1.y - 2 * p2.y + p3.y);
|
|
val bend := bend1 > bend2 ? bend1 : bend2;
|
|
var pieces := math::ceil(math::sqrt(bend * 0.75 / within));
|
|
pieces = pieces < 1 ? 1 : (pieces > 1000 ? 1000 : pieces);
|
|
val count := pieces as u64;
|
|
for step in 1..count {
|
|
val t := step as f64 / pieces;
|
|
val u := 1 - t;
|
|
val a := u * u * u;
|
|
val b := 3 * u * u * t;
|
|
val c := 3 * u * t * t;
|
|
val d := t * t * t;
|
|
made.add(point { x = a * p0.x + b * p1.x + c * p2.x + d * p3.x, y = a * p0.y + b * p1.y + c * p2.y + d * p3.y });
|
|
}
|
|
made.add(p3);
|
|
last = p3;
|
|
}
|
|
close => {
|
|
made.finish(true);
|
|
open = false;
|
|
}
|
|
}
|
|
}
|
|
if open {
|
|
made.finish(false);
|
|
}
|
|
return made;
|
|
}
|