CR: I'm gonna make him a refactor he can't refuse

This commit is contained in:
Martin Haug 2022-03-17 10:50:51 +01:00
parent 4d617bcd67
commit 6f5b721fe5
8 changed files with 49 additions and 41 deletions

View File

@ -80,6 +80,11 @@ impl Array {
self.0.iter() self.0.iter()
} }
/// Extracts a slice of the whole array.
pub fn as_slice(&self) -> &[Value] {
self.0.as_slice()
}
/// Return a sorted version of this array. /// Return a sorted version of this array.
/// ///
/// Returns an error if two values could not be compared. /// Returns an error if two values could not be compared.

View File

@ -40,12 +40,12 @@ impl Angle {
(self.0).0 (self.0).0
} }
/// Get the sine of this angle. /// Get the sine of this angle in radians.
pub fn sin(self) -> f64 { pub fn sin(self) -> f64 {
self.to_rad().sin() self.to_rad().sin()
} }
/// Get the cosine of this angle. /// Get the cosine of this angle in radians.
pub fn cos(self) -> f64 { pub fn cos(self) -> f64 {
self.to_rad().cos() self.to_rad().cos()
} }

View File

@ -49,12 +49,6 @@ impl Point {
} }
} }
impl From<Spec<Length>> for Point {
fn from(spec: Spec<Length>) -> Self {
Self::new(spec.x, spec.y)
}
}
impl Get<SpecAxis> for Point { impl Get<SpecAxis> for Point {
type Component = Length; type Component = Length;

View File

@ -36,9 +36,8 @@ impl Transform {
/// A rotation transform. /// A rotation transform.
pub fn rotation(angle: Angle) -> Self { pub fn rotation(angle: Angle) -> Self {
let v = angle.to_rad(); let cos = Relative::new(angle.cos());
let cos = Relative::new(v.cos()); let sin = Relative::new(angle.sin());
let sin = Relative::new(v.sin());
Self { Self {
sx: cos, sx: cos,
ky: sin, ky: sin,

View File

@ -2,18 +2,21 @@ use crate::library::prelude::*;
/// Display a line without affecting the layout. /// Display a line without affecting the layout.
#[derive(Debug, Hash)] #[derive(Debug, Hash)]
pub struct LineNode(Spec<Linear>, Spec<Linear>); pub struct LineNode {
origin: Spec<Linear>,
delta: Spec<Linear>,
}
#[node] #[node]
impl LineNode { impl LineNode {
/// How the stroke the line. /// How the stroke the line.
pub const STROKE: Smart<Paint> = Smart::Auto; pub const STROKE: Paint = Color::BLACK.into();
/// The line's thickness. /// The line's thickness.
pub const THICKNESS: Length = Length::pt(1.0); pub const THICKNESS: Length = Length::pt(1.0);
fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> {
let origin = args.named::<Spec<Linear>>("origin")?.unwrap_or_default(); 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), Some(to) => to.zip(origin).map(|(to, from)| to - from),
None => { None => {
let length = 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, regions: &Regions,
styles: StyleChain, styles: StyleChain,
) -> TypResult<Vec<Arc<Frame>>> { ) -> 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 thickness = styles.get(Self::THICKNESS);
let stroke = Some(Stroke { let stroke = Some(Stroke {
paint: styles.get(Self::STROKE).unwrap_or(Color::BLACK.into()), paint: styles.get(Self::STROKE),
thickness, thickness,
}); });
let resolved_origin = let resolved_origin =
self.0.zip(regions.base).map(|(l, b)| Linear::resolve(l, b)); self.origin.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 resolved_delta =
self.delta.zip(regions.base).map(|(l, b)| Linear::resolve(l, b));
let geometry = Geometry::Line(resolved_to.into());
let geometry = Geometry::Line(resolved_delta.to_point());
let shape = Shape { geometry, fill: None, stroke }; 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)]) Ok(vec![Arc::new(frame)])
} }

View File

@ -174,11 +174,15 @@ castable! {
castable! { castable! {
Spec<Linear>, Spec<Linear>,
Expected: "two-dimensional length array", Expected: "array of exactly two linears",
Value::Array(array) => { Value::Array(array) => {
let e = "point array must contain exactly two entries"; match array.as_slice() {
let a = array.get(0).map_err(|_| e)?.clone().cast::<Linear>()?; [a, b] => {
let b = array.get(1).map_err(|_| e)?.clone().cast::<Linear>()?; let a = a.clone().cast::<Linear>()?;
let b = b.clone().cast::<Linear>()?;
Spec::new(a, b) Spec::new(a, b)
}, },
_ => return Err("point array must contain exactly two entries".to_string()),
}
},
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -14,6 +14,7 @@
#v(.5cm) #v(.5cm)
--- ---
// Test the angle argument and positioning.
#set page(fill: rgb("0B1026")) #set page(fill: rgb("0B1026"))
#set line(stroke: white) #set line(stroke: white)
@ -22,6 +23,7 @@
#set text(spacing: 0%) #set text(spacing: 0%)
#set line(..args) #set line(..args)
#align(left)[
#line(length: +30%, origin: (09.0%, 02%)) #line(length: +30%, origin: (09.0%, 02%))
#line(length: +30%, origin: (38.7%, 02%), angle: -72deg) #line(length: +30%, origin: (38.7%, 02%), angle: -72deg)
#line(length: +30%, origin: (57.5%, 02%), angle: 252deg) #line(length: +30%, origin: (57.5%, 02%), angle: 252deg)
@ -33,8 +35,9 @@
#line(length: +30%, origin: (25.6%, 48%), angle: -72deg) #line(length: +30%, origin: (25.6%, 48%), angle: -72deg)
#line(length: +32%, origin: (8.50%, 02%), angle: 34deg) #line(length: +32%, origin: (8.50%, 02%), angle: 34deg)
] ]
]
#grid(columns: (1fr, ) * 3, ..((star(20pt, thickness: .5pt), ) * 9)) #align(center, grid(columns: (1fr, ) * 3, ..((star(20pt, thickness: .5pt), ) * 9)))
--- ---
// Test errors. // Test errors.