use kurbo::ParamCurveExtrema;
use typst_macros::{Cast, scope};
use typst_utils::Numeric;
use crate::diag::{HintedStrResult, HintedString, bail};
use crate::foundations::{Content, Packed, Smart, cast, elem};
use crate::layout::{Abs, Axes, Length, Point, Rect, Rel, Size};
use crate::visualize::{FillRule, Paint, Stroke};
use super::FixedStroke;
#[elem(scope)]
pub struct CurveElem {
pub fill: Option<Paint>,
#[default]
pub fill_rule: FillRule,
#[fold]
pub stroke: Smart<Option<Stroke>>,
#[variadic]
pub components: Vec<CurveComponent>,
}
#[scope]
impl CurveElem {
#[elem]
type CurveMove;
#[elem]
type CurveLine;
#[elem]
type CurveQuad;
#[elem]
type CurveCubic;
#[elem]
type CurveClose;
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum CurveComponent {
Move(Packed<CurveMove>),
Line(Packed<CurveLine>),
Quad(Packed<CurveQuad>),
Cubic(Packed<CurveCubic>),
Close(Packed<CurveClose>),
}
cast! {
CurveComponent,
self => match self {
Self::Move(element) => element.into_value(),
Self::Line(element) => element.into_value(),
Self::Quad(element) => element.into_value(),
Self::Cubic(element) => element.into_value(),
Self::Close(element) => element.into_value(),
},
v: Content => {
v.try_into()?
}
}
impl TryFrom<Content> for CurveComponent {
type Error = HintedString;
fn try_from(value: Content) -> HintedStrResult<Self> {
value
.into_packed::<CurveMove>()
.map(Self::Move)
.or_else(|value| value.into_packed::<CurveLine>().map(Self::Line))
.or_else(|value| value.into_packed::<CurveQuad>().map(Self::Quad))
.or_else(|value| value.into_packed::<CurveCubic>().map(Self::Cubic))
.or_else(|value| value.into_packed::<CurveClose>().map(Self::Close))
.or_else(|_| bail!("expecting a curve element"))
}
}
#[elem(name = "move", title = "Curve Move")]
pub struct CurveMove {
#[required]
pub start: Axes<Rel<Length>>,
#[default(false)]
pub relative: bool,
}
#[elem(name = "line", title = "Curve Line")]
pub struct CurveLine {
#[required]
pub end: Axes<Rel<Length>>,
#[default(false)]
pub relative: bool,
}
#[elem(name = "quad", title = "Curve Quadratic Segment")]
pub struct CurveQuad {
#[required]
pub control: Smart<Option<Axes<Rel<Length>>>>,
#[required]
pub end: Axes<Rel<Length>>,
#[default(false)]
pub relative: bool,
}
#[elem(name = "cubic", title = "Curve Cubic Segment")]
pub struct CurveCubic {
#[required]
pub control_start: Option<Smart<Axes<Rel<Length>>>>,
#[required]
pub control_end: Option<Axes<Rel<Length>>>,
#[required]
pub end: Axes<Rel<Length>>,
#[default(false)]
pub relative: bool,
}
#[elem(name = "close", title = "Curve Close")]
pub struct CurveClose {
pub mode: CloseMode,
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum CloseMode {
#[default]
Smooth,
Straight,
}
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct Curve(pub Vec<CurveItem>);
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum CurveItem {
Move(Point),
Line(Point),
Cubic(Point, Point, Point),
Close,
}
impl Curve {
pub const fn new() -> Self {
Self(vec![])
}
pub fn rect(size: Size) -> Self {
let z = Abs::zero();
let point = Point::new;
let mut curve = Self::new();
curve.move_(point(z, z));
curve.line(point(size.x, z));
curve.line(point(size.x, size.y));
curve.line(point(z, size.y));
curve.close();
curve
}
pub fn ellipse(size: Size) -> Self {
let z = Abs::zero();
let rx = size.x / 2.0;
let ry = size.y / 2.0;
let m = 0.551784;
let mx = m * rx;
let my = m * ry;
let point = |x, y| Point::new(x + rx, y + ry);
let mut curve = Curve::new();
curve.move_(point(-rx, z));
curve.cubic(point(-rx, -my), point(-mx, -ry), point(z, -ry));
curve.cubic(point(mx, -ry), point(rx, -my), point(rx, z));
curve.cubic(point(rx, my), point(mx, ry), point(z, ry));
curve.cubic(point(-mx, ry), point(-rx, my), point(-rx, z));
curve
}
pub fn move_(&mut self, p: Point) {
self.0.push(CurveItem::Move(p));
}
pub fn line(&mut self, p: Point) {
self.0.push(CurveItem::Line(p));
}
pub fn cubic(&mut self, p1: Point, p2: Point, p3: Point) {
self.0.push(CurveItem::Cubic(p1, p2, p3));
}
pub fn close(&mut self) {
self.0.push(CurveItem::Close);
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn translate(&mut self, offset: Point) {
if offset.is_zero() {
return;
}
for item in self.0.iter_mut() {
match item {
CurveItem::Move(p) => *p += offset,
CurveItem::Line(p) => *p += offset,
CurveItem::Cubic(p1, p2, p3) => {
*p1 += offset;
*p2 += offset;
*p3 += offset;
}
CurveItem::Close => (),
}
}
}
pub fn bbox(&self) -> Rect {
let mut min = Point::splat(Abs::inf());
let mut max = Point::splat(-Abs::inf());
let mut cursor = Point::zero();
for item in self.0.iter() {
match item {
CurveItem::Move(to) => {
cursor = *to;
}
CurveItem::Line(to) => {
min = min.min(cursor).min(*to);
max = max.max(cursor).max(*to);
cursor = *to;
}
CurveItem::Cubic(c0, c1, end) => {
let cubic = kurbo::CubicBez::new(
kurbo::Point::new(cursor.x.to_pt(), cursor.y.to_pt()),
kurbo::Point::new(c0.x.to_pt(), c0.y.to_pt()),
kurbo::Point::new(c1.x.to_pt(), c1.y.to_pt()),
kurbo::Point::new(end.x.to_pt(), end.y.to_pt()),
);
let bbox = cubic.bounding_box();
min.x = min.x.min(Abs::pt(bbox.x0)).min(Abs::pt(bbox.x1));
min.y = min.y.min(Abs::pt(bbox.y0)).min(Abs::pt(bbox.y1));
max.x = max.x.max(Abs::pt(bbox.x0)).max(Abs::pt(bbox.x1));
max.y = max.y.max(Abs::pt(bbox.y0)).max(Abs::pt(bbox.y1));
cursor = *end;
}
CurveItem::Close => (),
}
}
Rect::new(min, max)
}
pub fn bbox_size(&self) -> Size {
self.bbox().size()
}
}
impl Curve {
fn to_kurbo(&self) -> impl Iterator<Item = kurbo::PathEl> + '_ {
use kurbo::PathEl;
self.0.iter().map(|item| match *item {
CurveItem::Move(point) => PathEl::MoveTo(point_to_kurbo(point)),
CurveItem::Line(point) => PathEl::LineTo(point_to_kurbo(point)),
CurveItem::Cubic(point, point1, point2) => PathEl::CurveTo(
point_to_kurbo(point),
point_to_kurbo(point1),
point_to_kurbo(point2),
),
CurveItem::Close => PathEl::ClosePath,
})
}
pub fn contains(&self, fill_rule: FillRule, needle: Point) -> bool {
let kurbo = kurbo::BezPath::from_vec(self.to_kurbo().collect());
let windings = kurbo::Shape::winding(&kurbo, point_to_kurbo(needle));
match fill_rule {
FillRule::NonZero => windings != 0,
FillRule::EvenOdd => windings % 2 != 0,
}
}
pub fn stroke_contains(&self, stroke: &FixedStroke, needle: Point) -> bool {
let width = stroke.thickness.to_raw();
let cap = match stroke.cap {
super::LineCap::Butt => kurbo::Cap::Butt,
super::LineCap::Round => kurbo::Cap::Round,
super::LineCap::Square => kurbo::Cap::Square,
};
let join = match stroke.join {
super::LineJoin::Miter => kurbo::Join::Miter,
super::LineJoin::Round => kurbo::Join::Round,
super::LineJoin::Bevel => kurbo::Join::Bevel,
};
let miter_limit = stroke.miter_limit.get();
let mut style = kurbo::Stroke::new(width)
.with_caps(cap)
.with_join(join)
.with_miter_limit(miter_limit);
if let Some(dash) = &stroke.dash {
style = style.with_dashes(
dash.phase.to_raw(),
dash.array.iter().copied().map(Abs::to_raw),
);
}
let opts = kurbo::StrokeOpts::default();
let tolerance = 0.01;
let expanded = kurbo::stroke(self.to_kurbo(), &style, &opts, tolerance);
kurbo::Shape::contains(&expanded, point_to_kurbo(needle))
}
}
fn point_to_kurbo(point: Point) -> kurbo::Point {
kurbo::Point::new(point.x.to_raw(), point.y.to_raw())
}