use super::options::Tip;
use super::{Point, Segment};
#[derive(Debug, Clone, PartialEq)]
pub struct Head {
pub start: Point,
pub segments: Vec<Segment>,
pub filled: bool,
pub width: f64,
pub reach: f64,
}
pub fn head(which: Tip, line_width: f64) -> Head {
let u = 0.28 + 0.3 * line_width;
match which {
Tip::Stealth => Head {
start: (5.0 * u, 0.0),
segments: vec![
Segment::Line((-3.0 * u, 4.0 * u)),
Segment::Line((0.0, 0.0)),
Segment::Line((-3.0 * u, -4.0 * u)),
],
filled: true,
width: line_width,
reach: 5.0 * u,
},
Tip::Latex => Head {
start: (9.0 * u, 0.0),
segments: vec![
Segment::Curve((6.3333 * u, 0.5 * u), (2.0 * u, 2.0 * u), (-u, 3.75 * u)),
Segment::Line((-u, -3.75 * u)),
Segment::Curve((2.0 * u, -2.0 * u), (6.3333 * u, -0.5 * u), (9.0 * u, 0.0)),
],
filled: true,
width: line_width,
reach: 9.0 * u,
},
Tip::To => Head {
start: (-3.0 * u, 4.0 * u),
segments: vec![
Segment::Curve((-2.75 * u, 2.5 * u), (0.0, 0.25 * u), (0.75 * u, 0.0)),
Segment::Curve(
(0.0, -0.25 * u),
(-2.75 * u, -2.5 * u),
(-3.0 * u, -4.0 * u),
),
],
filled: false,
width: 0.8 * line_width,
reach: 0.21 + 0.625 * line_width,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
const BP: f64 = 72.27 / 72.0;
#[test]
fn stealth_is_the_shape_lualatex_draws() {
let head = head(Tip::Stealth, 0.4);
assert!(
(head.start.0 / BP - 1.99252).abs() < 1e-4,
"{:?}",
head.start
);
assert!((head.reach / BP - 1.99252).abs() < 1e-4, "{}", head.reach);
assert_eq!(head.segments.len(), 3);
match head.segments[0] {
Segment::Line((x, y)) => {
assert!((x / BP + 1.19551).abs() < 1e-4, "{x}");
assert!((y / BP - 1.59401).abs() < 1e-4, "{y}");
}
ref other => panic!("straight, not {other:?}"),
}
assert!(
head.filled,
"stealth is filled, and lualatex ends it with `f`"
);
}
#[test]
fn latex_is_the_shape_lualatex_draws() {
let head = head(Tip::Latex, 0.4);
assert!(
(head.start.0 / BP - 3.58653).abs() < 1e-4,
"{:?}",
head.start
);
match head.segments[0] {
Segment::Curve((c1x, c1y), (c2x, c2y), (x, y)) => {
assert!((c1x / BP - 2.52383).abs() < 1e-3, "{c1x}");
assert!((c1y / BP - 0.19925).abs() < 1e-4, "{c1y}");
assert!((c2x / BP - 0.797).abs() < 1e-4, "{c2x}");
assert!((c2y / BP - 0.797).abs() < 1e-4, "{c2y}");
assert!((x / BP + 0.3985).abs() < 1e-4, "{x}");
assert!((y / BP - 1.49438).abs() < 1e-4, "{y}");
}
ref other => panic!("a curve, not {other:?}"),
}
}
#[test]
fn the_default_arrow_is_stroked_and_not_filled() {
let head = head(Tip::To, 0.4);
assert!(!head.filled);
assert!((head.width / BP - 0.31879).abs() < 1e-4, "{}", head.width);
assert!(
(head.start.0 / BP + 1.19551).abs() < 1e-4,
"{:?}",
head.start
);
assert!(
(head.start.1 / BP - 1.59401).abs() < 1e-4,
"{:?}",
head.start
);
assert!((head.reach / BP - 0.45828).abs() < 1e-4, "{}", head.reach);
}
#[test]
fn a_thicker_line_gets_a_bigger_head() {
let head = head(Tip::To, 1.0);
let u = 0.28 + 0.3;
assert!((head.start.0 + 3.0 * u).abs() < 1e-9);
assert!((3.0 * u / BP - 1.73352).abs() < 1e-4);
}
}