summaryrefslogtreecommitdiff
path: root/src/library/graphics
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
committerLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
commit95e9134a3c7d7a14d8c8928413fdffc665671895 (patch)
tree822b5f6c2d23aba33cfe4d199405e493e37c3d70 /src/library/graphics
parent66030ae5d73d85a0463562230b87f8ec7554c746 (diff)
Refactor `geom` module
Diffstat (limited to 'src/library/graphics')
-rw-r--r--src/library/graphics/image.rs4
-rw-r--r--src/library/graphics/line.rs17
-rw-r--r--src/library/graphics/shape.rs29
3 files changed, 21 insertions, 29 deletions
diff --git a/src/library/graphics/image.rs b/src/library/graphics/image.rs
index 7523471d..343b4788 100644
--- a/src/library/graphics/image.rs
+++ b/src/library/graphics/image.rs
@@ -33,7 +33,7 @@ impl ImageNode {
let height = args.named("height")?;
Ok(Content::inline(
- ImageNode(image).pack().sized(Spec::new(width, height)),
+ ImageNode(image).pack().sized(Axes::new(width, height)),
))
}
}
@@ -62,7 +62,7 @@ impl Layout for ImageNode {
} else if first.y.is_finite() {
Size::new(first.x.min(first.y * px_ratio), first.y)
} else {
- Size::new(Length::pt(pxw), Length::pt(pxh))
+ Size::new(Abs::pt(pxw), Abs::pt(pxh))
};
// Compute the actual size of the fitted image.
diff --git a/src/library/graphics/line.rs b/src/library/graphics/line.rs
index 192f8350..78878014 100644
--- a/src/library/graphics/line.rs
+++ b/src/library/graphics/line.rs
@@ -4,9 +4,9 @@ use crate::library::prelude::*;
#[derive(Debug, Hash)]
pub struct LineNode {
/// Where the line starts.
- origin: Spec<Relative<RawLength>>,
+ origin: Axes<Rel<Length>>,
/// The offset from the `origin` where the line ends.
- delta: Spec<Relative<RawLength>>,
+ delta: Axes<Rel<Length>>,
}
#[node]
@@ -18,18 +18,17 @@ impl LineNode {
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
let origin = args.named("origin")?.unwrap_or_default();
- let delta = match args.named::<Spec<Relative<RawLength>>>("to")? {
+ let delta = match args.named::<Axes<Rel<Length>>>("to")? {
Some(to) => to.zip(origin).map(|(to, from)| to - from),
None => {
- let length = args
- .named::<Relative<RawLength>>("length")?
- .unwrap_or(Length::cm(1.0).into());
+ let length =
+ args.named::<Rel<Length>>("length")?.unwrap_or(Abs::cm(1.0).into());
let angle = args.named::<Angle>("angle")?.unwrap_or_default();
let x = angle.cos() * length;
let y = angle.sin() * length;
- Spec::new(x, y)
+ Axes::new(x, y)
}
};
@@ -69,12 +68,12 @@ impl Layout for LineNode {
}
castable! {
- Spec<Relative<RawLength>>,
+ Axes<Rel<Length>>,
Expected: "array of two relative lengths",
Value::Array(array) => {
let mut iter = array.into_iter();
match (iter.next(), iter.next(), iter.next()) {
- (Some(a), Some(b), None) => Spec::new(a.cast()?, b.cast()?),
+ (Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?),
_ => Err("point array must contain exactly two entries")?,
}
},
diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs
index eea02568..7a742109 100644
--- a/src/library/graphics/shape.rs
+++ b/src/library/graphics/shape.rs
@@ -29,20 +29,19 @@ impl<const S: ShapeKind> ShapeNode<S> {
/// How much to pad the shape's content.
#[property(resolve, fold)]
- pub const INSET: Sides<Option<Relative<RawLength>>> = Sides::splat(Relative::zero());
+ pub const INSET: Sides<Option<Rel<Length>>> = Sides::splat(Rel::zero());
/// How much to extend the shape's dimensions beyond the allocated space.
#[property(resolve, fold)]
- pub const OUTSET: Sides<Option<Relative<RawLength>>> = Sides::splat(Relative::zero());
+ pub const OUTSET: Sides<Option<Rel<Length>>> = Sides::splat(Rel::zero());
/// How much to round the shape's corners.
#[property(skip, resolve, fold)]
- pub const RADIUS: Corners<Option<Relative<RawLength>>> =
- Corners::splat(Relative::zero());
+ pub const RADIUS: Corners<Option<Rel<Length>>> = Corners::splat(Rel::zero());
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
let size = match S {
- SQUARE => args.named::<RawLength>("size")?.map(Relative::from),
- CIRCLE => args.named::<RawLength>("radius")?.map(|r| 2.0 * Relative::from(r)),
+ SQUARE => args.named::<Length>("size")?.map(Rel::from),
+ CIRCLE => args.named::<Length>("radius")?.map(|r| 2.0 * Rel::from(r)),
_ => None,
};
@@ -57,7 +56,7 @@ impl<const S: ShapeKind> ShapeNode<S> {
};
Ok(Content::inline(
- Self(args.eat()?).pack().sized(Spec::new(width, height)),
+ Self(args.eat()?).pack().sized(Axes::new(width, height)),
))
}
@@ -90,7 +89,7 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> {
}
// Pad the child.
- let child = child.clone().padded(inset.map(|side| side.map(RawLength::from)));
+ let child = child.clone().padded(inset.map(|side| side.map(Length::from)));
let mut pod = Regions::one(regions.first, regions.base, regions.expand);
frames = child.layout(world, &pod, styles)?;
@@ -112,14 +111,13 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> {
};
pod.first = Size::splat(length);
- pod.expand = Spec::splat(true);
+ pod.expand = Axes::splat(true);
frames = child.layout(world, &pod, styles)?;
}
} else {
// The default size that a shape takes on if it has no child and
// enough space.
- let mut size =
- Size::new(Length::pt(45.0), Length::pt(30.0)).min(regions.first);
+ let mut size = Size::new(Abs::pt(45.0), Abs::pt(30.0)).min(regions.first);
if is_quadratic(S) {
let length = if regions.expand.x || regions.expand.y {
@@ -159,16 +157,11 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> {
if fill.is_some() || stroke.iter().any(Option::is_some) {
if is_round(S) {
- let shape = Shape {
- geometry: Geometry::Ellipse(size),
- fill,
- stroke: stroke.left,
- };
+ let shape = ellipse(size, fill, stroke.left);
frame.prepend(pos, Element::Shape(shape));
} else {
frame.prepend_multiple(
- RoundedRect::new(size, radius)
- .shapes(fill, stroke)
+ rounded_rect(size, radius, fill, stroke)
.into_iter()
.map(|x| (pos, Element::Shape(x))),
)