diff options
| author | Martin Haug <mhaug@live.de> | 2022-03-17 10:50:51 +0100 |
|---|---|---|
| committer | Martin Haug <mhaug@live.de> | 2022-03-17 10:50:51 +0100 |
| commit | 6f5b721fe56fe6e3735d03b07e3716fc39572639 (patch) | |
| tree | 002ed75565e02baeb1ce1b9ec53f0a92491d17ab /src/library | |
| parent | 4d617bcd67f9e42218da190dc9a0bf2f10d4b78d (diff) | |
CR: I'm gonna make him a refactor he can't refuse
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/graphics/line.rs | 29 | ||||
| -rw-r--r-- | src/library/mod.rs | 14 |
2 files changed, 25 insertions, 18 deletions
diff --git a/src/library/graphics/line.rs b/src/library/graphics/line.rs index 141abb08..a650c76c 100644 --- a/src/library/graphics/line.rs +++ b/src/library/graphics/line.rs @@ -2,18 +2,21 @@ use crate::library::prelude::*; /// Display a line without affecting the layout. #[derive(Debug, Hash)] -pub struct LineNode(Spec<Linear>, Spec<Linear>); +pub struct LineNode { + origin: Spec<Linear>, + delta: Spec<Linear>, +} #[node] impl LineNode { /// How the stroke the line. - pub const STROKE: Smart<Paint> = Smart::Auto; + pub const STROKE: Paint = Color::BLACK.into(); /// The line's thickness. pub const THICKNESS: Length = Length::pt(1.0); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { let origin = args.named::<Spec<Linear>>("origin")?.unwrap_or_default(); - let to = match args.named::<Spec<Linear>>("to")? { + let delta = match args.named::<Spec<Linear>>("to")? { Some(to) => to.zip(origin).map(|(to, from)| to - from), None => { let length = @@ -27,7 +30,7 @@ impl LineNode { } }; - Ok(Content::inline(Self(origin, to))) + Ok(Content::inline(Self { origin, delta })) } } @@ -38,23 +41,23 @@ impl Layout for LineNode { regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { - let target = regions.expand.select(regions.first, Size::zero()); - let mut frame = Frame::new(target); - let thickness = styles.get(Self::THICKNESS); let stroke = Some(Stroke { - paint: styles.get(Self::STROKE).unwrap_or(Color::BLACK.into()), + paint: styles.get(Self::STROKE), thickness, }); let resolved_origin = - self.0.zip(regions.base).map(|(l, b)| Linear::resolve(l, b)); - let resolved_to = self.1.zip(regions.base).map(|(l, b)| Linear::resolve(l, b)); - - let geometry = Geometry::Line(resolved_to.into()); + self.origin.zip(regions.base).map(|(l, b)| Linear::resolve(l, b)); + let resolved_delta = + self.delta.zip(regions.base).map(|(l, b)| Linear::resolve(l, b)); + let geometry = Geometry::Line(resolved_delta.to_point()); let shape = Shape { geometry, fill: None, stroke }; - frame.prepend(resolved_origin.into(), Element::Shape(shape)); + + let target = regions.expand.select(regions.first, Size::zero()); + let mut frame = Frame::new(target); + frame.push(resolved_origin.to_point(), Element::Shape(shape)); Ok(vec![Arc::new(frame)]) } diff --git a/src/library/mod.rs b/src/library/mod.rs index 88f003c2..505f1a1d 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -174,11 +174,15 @@ castable! { castable! { Spec<Linear>, - Expected: "two-dimensional length array", + Expected: "array of exactly two linears", Value::Array(array) => { - let e = "point array must contain exactly two entries"; - let a = array.get(0).map_err(|_| e)?.clone().cast::<Linear>()?; - let b = array.get(1).map_err(|_| e)?.clone().cast::<Linear>()?; - Spec::new(a, b) + match array.as_slice() { + [a, b] => { + let a = a.clone().cast::<Linear>()?; + let b = b.clone().cast::<Linear>()?; + Spec::new(a, b) + }, + _ => return Err("point array must contain exactly two entries".to_string()), + } }, } |
