mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
183 lines
6.0 KiB
Rust
183 lines
6.0 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use anyhow::{anyhow, bail};
|
|
use clap::Parser;
|
|
use kurbo::BezPath;
|
|
use svg::Document;
|
|
|
|
use linesweeper::{
|
|
BinaryOp, FillRule, binary_op,
|
|
topology::{Contours, Topology},
|
|
};
|
|
|
|
const FILL_RULE: FillRule = FillRule::EvenOdd;
|
|
|
|
#[derive(Parser)]
|
|
struct Cli {
|
|
#[arg(long)]
|
|
output: PathBuf,
|
|
|
|
#[arg(long)]
|
|
input: PathBuf,
|
|
}
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
|
let args = Cli::parse();
|
|
let input = std::fs::read_to_string(&args.input)?;
|
|
let mut input_lines = input.lines();
|
|
let op = input_lines.next().ok_or(anyhow!("no op line"))?;
|
|
let op = match op {
|
|
"union" => BinaryOp::Union,
|
|
"intersection" => BinaryOp::Intersection,
|
|
"difference" => BinaryOp::Difference,
|
|
"xor" => BinaryOp::Xor,
|
|
_ => bail!("unknown op {op}"),
|
|
};
|
|
|
|
let shape_a = input_lines.next().ok_or(anyhow!("no first shape"))?;
|
|
let shape_b = input_lines.next().ok_or(anyhow!("no second shape"))?;
|
|
|
|
for extra_line in input_lines {
|
|
if !extra_line.trim().is_empty() {
|
|
eprintln!("ignoring extra line: {extra_line}");
|
|
}
|
|
}
|
|
|
|
let shape_a = BezPath::from_svg(shape_a)?;
|
|
let shape_b = BezPath::from_svg(shape_b)?;
|
|
|
|
let eps = 1e-5;
|
|
let contours = binary_op(&shape_a, &shape_b, FILL_RULE, op)?;
|
|
let top = Topology::from_paths_binary(&shape_a, &shape_b, eps)?;
|
|
let bbox = top.bounding_box();
|
|
let min_x = bbox.min_x();
|
|
let min_y = bbox.min_y();
|
|
let max_x = bbox.max_x();
|
|
let max_y = bbox.max_y();
|
|
let pad = 1.0 + eps;
|
|
let one_width = max_x - min_x + 2.0 * pad;
|
|
let one_height = max_y - min_y + 2.0 * pad;
|
|
// Craft patch (example stroke width).
|
|
let stroke_width = stroke_width(bbox);
|
|
|
|
let mut document = svg::Document::new().set(
|
|
"viewBox",
|
|
(min_x - pad, min_y - pad, one_width * 2.0, one_height),
|
|
);
|
|
|
|
// Draw the original document.
|
|
for (c, color) in [(&shape_a, "green"), (&shape_b, "blue")] {
|
|
document = document.add(input_path(c, color, stroke_width));
|
|
}
|
|
|
|
document = add_op(document, &contours, one_width, stroke_width);
|
|
|
|
svg::save(&args.output, &document)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// Craft patch (example fill rule): the inputs are drawn with the fill rule the operation uses.
|
|
fn svg_fill_rule(fill_rule: FillRule) -> &'static str {
|
|
match fill_rule {
|
|
FillRule::EvenOdd => "evenodd",
|
|
FillRule::NonZero => "nonzero",
|
|
}
|
|
}
|
|
|
|
fn input_path(shape: &BezPath, color: &str, stroke_width: f64) -> svg::node::element::Path {
|
|
let mut data = svg::node::element::path::Data::new();
|
|
for el in shape {
|
|
let p = |point: kurbo::Point| (point.x, point.y);
|
|
data = match el {
|
|
kurbo::PathEl::MoveTo(p0) => data.move_to(p(p0)),
|
|
kurbo::PathEl::LineTo(p0) => data.line_to(p(p0)),
|
|
kurbo::PathEl::QuadTo(p0, p1) => data.quadratic_curve_to((p(p0), p(p1))),
|
|
kurbo::PathEl::CurveTo(p0, p1, p2) => data.cubic_curve_to((p(p0), p(p1), p(p2))),
|
|
kurbo::PathEl::ClosePath => data.close(),
|
|
};
|
|
}
|
|
|
|
svg::node::element::Path::new()
|
|
.set("stroke", "black")
|
|
.set("stroke-width", stroke_width)
|
|
.set("stroke-linecap", "round")
|
|
.set("stroke-linejoin", "round")
|
|
.set("opacity", 0.5)
|
|
.set("fill", color)
|
|
.set("fill-rule", svg_fill_rule(FILL_RULE))
|
|
.set("d", data)
|
|
}
|
|
|
|
fn add_op(mut doc: Document, contours: &Contours, x_off: f64, stroke_width: f64) -> Document {
|
|
for group in contours.grouped() {
|
|
let mut data = svg::node::element::path::Data::new();
|
|
|
|
for contour_idx in group {
|
|
let path = &contours[contour_idx].path;
|
|
for el in path.iter() {
|
|
data = match el {
|
|
kurbo::PathEl::MoveTo(p) => data.move_to((p.x + x_off, p.y)),
|
|
kurbo::PathEl::LineTo(p) => data.line_to((p.x + x_off, p.y)),
|
|
kurbo::PathEl::QuadTo(p0, p1) => {
|
|
data.quadratic_curve_to(((p0.x + x_off, p0.y), (p1.x + x_off, p1.y)))
|
|
}
|
|
kurbo::PathEl::CurveTo(p0, p1, p2) => data.cubic_curve_to((
|
|
(p0.x + x_off, p0.y),
|
|
(p1.x + x_off, p1.y),
|
|
(p2.x + x_off, p2.y),
|
|
)),
|
|
kurbo::PathEl::ClosePath => data.close(),
|
|
};
|
|
}
|
|
data = data.close();
|
|
}
|
|
let path = svg::node::element::Path::new()
|
|
.set("d", data)
|
|
.set("stroke", "black")
|
|
.set("stroke-width", stroke_width)
|
|
.set("stroke-linecap", "round")
|
|
.set("stroke-linejoin", "round")
|
|
.set("fill", "pink");
|
|
doc = doc.add(path);
|
|
}
|
|
doc
|
|
}
|
|
|
|
// Craft patch (example stroke width): the larger of the bounding box's width
|
|
// and height, divided by 512.
|
|
fn stroke_width(bbox: kurbo::Rect) -> f64 {
|
|
let (min_x, min_y, max_x, max_y) = (bbox.min_x(), bbox.min_y(), bbox.max_x(), bbox.max_y());
|
|
(max_y - min_y).max(max_x - min_x) / 512.0
|
|
}
|
|
|
|
// Craft patch (example stroke width).
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn stroke_width_follows_the_larger_side() {
|
|
let wide = kurbo::Rect::new(0.0, 1000.0, 512.0, 1001.0);
|
|
assert_eq!(stroke_width(wide), 1.0);
|
|
let tall = kurbo::Rect::new(100.0, 0.0, 101.0, 512.0);
|
|
assert_eq!(stroke_width(tall), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn inputs_are_drawn_with_the_operation_fill_rule() {
|
|
// Two nested squares with the same orientation. Even-odd leaves the inner square
|
|
// empty, and nonzero fills it.
|
|
let nested = BezPath::from_svg("M0,0 L10,0 L10,10 L0,10 Z M2,2 L8,2 L8,8 L2,8 Z").unwrap();
|
|
let drawn = input_path(&nested, "green", 0.1).to_string();
|
|
assert_eq!(FILL_RULE, FillRule::EvenOdd);
|
|
assert!(drawn.contains(r#"fill-rule="evenodd""#), "{drawn}");
|
|
}
|
|
|
|
#[test]
|
|
fn svg_fill_rule_names() {
|
|
assert_eq!(svg_fill_rule(FillRule::EvenOdd), "evenodd");
|
|
assert_eq!(svg_fill_rule(FillRule::NonZero), "nonzero");
|
|
}
|
|
}
|