summaryrefslogtreecommitdiff
path: root/src/library/transform.rs
blob: 6a6b40348193b04927a14c6fe5fbcc3d5c1c54e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::prelude::*;
use crate::geom::Transform;

/// `move`: Move content without affecting layout.
pub fn move_(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
    let tx = args.named("x")?.unwrap_or_default();
    let ty = args.named("y")?.unwrap_or_default();
    let transform = Transform::translation(tx, ty);
    transform_impl(args, transform)
}

/// `scale`: Scale content without affecting layout.
pub fn scale(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
    let all = args.find();
    let sx = args.named("x")?.or(all).unwrap_or(Relative::one());
    let sy = args.named("y")?.or(all).unwrap_or(Relative::one());
    let transform = Transform::scaling(sx, sy);
    transform_impl(args, transform)
}

/// `rotate`: Rotate content without affecting layout.
pub fn rotate(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
    let angle = args.expect("angle")?;
    let transform = Transform::rotation(angle);
    transform_impl(args, transform)
}

fn transform_impl(args: &mut Args, transform: Transform) -> TypResult<Value> {
    let body: Node = args.expect("body")?;
    let origin = args
        .named("origin")?
        .unwrap_or(Spec::splat(None))
        .unwrap_or(Align::CENTER_HORIZON);

    Ok(Value::inline(
        body.into_block().transformed(transform, origin),
    ))
}

/// A node that transforms its child without affecting layout.
#[derive(Debug, Hash)]
pub struct TransformNode {
    /// Transformation to apply to the contents.
    pub transform: Transform,
    /// The origin of the transformation.
    pub origin: Spec<Align>,
    /// The node whose contents should be transformed.
    pub child: PackedNode,
}

impl Layout for TransformNode {
    fn layout(
        &self,
        ctx: &mut LayoutContext,
        regions: &Regions,
    ) -> Vec<Constrained<Rc<Frame>>> {
        let mut frames = self.child.layout(ctx, regions);

        for Constrained { item: frame, .. } in &mut frames {
            let Spec { x, y } = self.origin.zip(frame.size).map(|(o, s)| o.resolve(s));
            let transform = Transform::translation(x, y)
                .pre_concat(self.transform)
                .pre_concat(Transform::translation(-x, -y));

            Rc::make_mut(frame).transform(transform);
        }

        frames
    }
}