use crate::color::{Rgba, parse_color};
use crate::error::Result;
use crate::format::js_mod;
use crate::geometry::{self as g, Flat, Kind, Segment, Vec2};
use crate::svg::{CANVAS, Prepared, is_closed, is_filled};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PathCmd {
M(f64, f64),
L(f64, f64),
C([f64; 6]),
H,
}
pub type Matrix = [f64; 6];
#[derive(Debug, Clone, PartialEq)]
pub struct DrawOp {
pub path: Vec<PathCmd>,
pub fill: Option<Rgba>,
pub stroke: Option<Rgba>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Layer {
pub id: String,
pub ops: Vec<DrawOp>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Drawing {
pub width: f64,
pub height: f64,
pub base: Matrix,
pub transforms: Vec<Matrix>,
pub line_width: f64,
pub linecap: u8,
pub linejoin: u8,
pub background: Option<DrawOp>,
pub layers: Vec<Layer>,
pub title: String,
}
pub const KAPPA: f64 = 0.552_284_749_830_793_6;
const QUADS: [[f64; 6]; 4] = [
[1.0, KAPPA, KAPPA, 1.0, 0.0, 1.0],
[-KAPPA, 1.0, -1.0, KAPPA, -1.0, 0.0],
[-1.0, -KAPPA, -KAPPA, -1.0, 0.0, -1.0],
[KAPPA, -1.0, 1.0, -KAPPA, 1.0, 0.0],
];
fn map(m: &Matrix, x: f64, y: f64) -> Vec2 {
(m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5])
}
fn unit_circle(m: &Matrix) -> Vec<PathCmd> {
let (x, y) = map(m, 1.0, 0.0);
let mut out = vec![PathCmd::M(x, y)];
for q in QUADS {
let (a, b) = map(m, q[0], q[1]);
let (c, d) = map(m, q[2], q[3]);
let (e, f) = map(m, q[4], q[5]);
out.push(PathCmd::C([a, b, c, d, e, f]));
}
out.push(PathCmd::H);
out
}
fn circle_matrix(cx: f64, cy: f64, r: f64) -> Matrix {
[r, 0.0, 0.0, r, cx, cy]
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss
)]
fn arc_path(cx: f64, cy: f64, r: f64, start: f64, end: f64) -> Vec<PathCmd> {
let n = ((end - start) / 90.0 - 1e-9).ceil().max(1.0) as u32;
let step = (end - start) / f64::from(n);
let alpha = (4.0 / 3.0) * ((step * std::f64::consts::PI) / 720.0).tan();
let (c0, s0) = g::cos_sin(start);
let mut out = vec![PathCmd::M(cx + r * c0, cy + r * s0)];
for i in 0..n {
let (ca, sa) = g::cos_sin(start + f64::from(i) * step);
let (cb, sb) = g::cos_sin(start + f64::from(i + 1) * step);
out.push(PathCmd::C([
cx + r * (ca - alpha * sa),
cy + r * (sa + alpha * ca),
cx + r * (cb + alpha * sb),
cy + r * (sb - alpha * cb),
cx + r * cb,
cy + r * sb,
]));
}
out
}
fn path_cmds(item: &Flat, dot_scale: f64) -> Vec<PathCmd> {
match &item.kind {
Kind::Poly { points, closed } => {
let Some((first, rest)) = points.split_first() else {
return Vec::new();
};
let mut out = vec![PathCmd::M(first.0, first.1)];
out.extend(rest.iter().map(|p| PathCmd::L(p.0, p.1)));
if *closed {
out.push(PathCmd::H);
}
out
}
Kind::Circle { cx, cy, r, dot } => unit_circle(&circle_matrix(
*cx,
*cy,
if *dot { r * dot_scale } else { *r },
)),
Kind::Arc {
cx,
cy,
r,
start,
end,
} => {
if end - start >= 360.0 {
unit_circle(&circle_matrix(*cx, *cy, *r))
} else {
arc_path(*cx, *cy, *r, *start, *end)
}
}
Kind::Ellipse {
cx,
cy,
rx,
ry,
rotation,
} => {
let (c, s) = g::cos_sin(*rotation);
unit_circle(&[c * rx, s * rx, -s * ry, c * ry, *cx, *cy])
}
Kind::Path { segments } => {
let mut out = Vec::new();
let (mut cur, mut start): (Vec2, Vec2) = ((0.0, 0.0), (0.0, 0.0));
for seg in segments {
match *seg {
Segment::M(p) => {
out.push(PathCmd::M(p.0, p.1));
(cur, start) = (p, p);
}
Segment::L(p) => {
out.push(PathCmd::L(p.0, p.1));
cur = p;
}
Segment::Q(q, p) => {
out.push(PathCmd::C([
cur.0 + (2.0 / 3.0) * (q.0 - cur.0),
cur.1 + (2.0 / 3.0) * (q.1 - cur.1),
p.0 + (2.0 / 3.0) * (q.0 - p.0),
p.1 + (2.0 / 3.0) * (q.1 - p.1),
p.0,
p.1,
]));
cur = p;
}
Segment::C(c1, c2, p) => {
out.push(PathCmd::C([c1.0, c1.1, c2.0, c2.1, p.0, p.1]));
cur = p;
}
Segment::Z => {
out.push(PathCmd::H);
cur = start;
}
}
}
out
}
}
}
fn with_alpha(c: Rgba, opacity: f64) -> Rgba {
(c.0, c.1, c.2, c.3 * opacity)
}
#[allow(clippy::too_many_lines)]
pub fn build_drawing(p: &Prepared, current_color: Rgba) -> Result<Drawing> {
let o = &p.options;
let bx = CANVAS + 2.0 * o.padding;
let width = o.size * 0.75;
let k = width / bx;
let root = parse_color(&o.color, current_color)?;
let mut inks: Vec<(String, Rgba)> = Vec::new();
let mut layers: Vec<Layer> = Vec::new();
for item in &p.items {
let path = path_cmds(item, o.dot_scale);
if path.is_empty() {
continue;
}
let color = if let Some((_, c)) = inks.iter().find(|(l, _)| *l == item.layer) {
*c
} else {
let c = match o.layer_color(&item.layer) {
Some(lc) => parse_color(lc, root)?,
None => root,
};
inks.push((item.layer.clone(), c));
c
};
let op = if is_filled(item) {
DrawOp {
path,
fill: Some(with_alpha(color, o.stroke_opacity)),
stroke: None,
}
} else if o.fill != "none" && is_closed(item) {
DrawOp {
path,
fill: Some(with_alpha(parse_color(&o.fill, color)?, o.fill_opacity)),
stroke: Some(with_alpha(color, o.stroke_opacity)),
}
} else {
DrawOp {
path,
fill: None,
stroke: Some(with_alpha(color, o.stroke_opacity)),
}
};
match layers.iter_mut().find(|l| l.id == item.layer) {
Some(l) => l.ops.push(op),
None => layers.push(Layer {
id: item.layer.clone(),
ops: vec![op],
}),
}
}
let mut transforms: Vec<Matrix> = Vec::new();
if js_mod(o.rotation, 360.0) != 0.0 {
let (c, s) = g::cos_sin(o.rotation);
let m = CANVAS / 2.0;
transforms.push([c, s, -s, c, m - c * m + s * m, m - s * m - c * m]);
}
if o.flip_x || o.flip_y {
let sx = if o.flip_x { -1.0 } else { 1.0 };
let sy = if o.flip_y { -1.0 } else { 1.0 };
transforms.push([
sx,
0.0,
0.0,
sy,
if o.flip_x { CANVAS } else { 0.0 },
if o.flip_y { CANVAS } else { 0.0 },
]);
}
let background = if o.background == "none" {
None
} else {
let (lo, hi) = (-o.padding, CANVAS + o.padding);
let path = vec![
PathCmd::M(lo, lo),
PathCmd::L(hi, lo),
PathCmd::L(hi, hi),
PathCmd::L(lo, hi),
PathCmd::H,
];
Some(DrawOp {
path,
fill: Some(parse_color(&o.background, root)?),
stroke: None,
})
};
let linecap = match o.linecap.as_str() {
"butt" => 0,
"square" => 2,
_ => 1,
};
let linejoin = match o.linejoin.as_str() {
"miter" => 0,
"bevel" => 2,
_ => 1,
};
Ok(Drawing {
width,
height: width,
base: [k, 0.0, 0.0, -k, k * o.padding, width - k * o.padding],
transforms,
line_width: if o.absolute_stroke_width {
(o.stroke_width * bx) / o.size
} else {
o.stroke_width
},
linecap,
linejoin,
background,
layers,
title: o.title.clone().unwrap_or_else(|| p.default_title.clone()),
})
}