summaryrefslogtreecommitdiff
path: root/library/src/layout/transform.rs
blob: b509607d484123f85700eaa5a8daaf8aba8b1b41 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use typst::geom::Transform;

use crate::prelude::*;

/// Move content without affecting layout.
///
/// # Parameters
/// - body: Content (positional, required)
///   The content to move.
///
/// - dx: Rel<Length> (named)
///   The horizontal displacement of the content.
///
/// - dy: Rel<Length> (named)
///   The vertical displacement of the content.
///
/// # Tags
/// - layout
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
pub struct MoveNode {
    /// The offset by which to move the content.
    pub delta: Axes<Rel<Length>>,
    /// The content that should be moved.
    pub body: Content,
}

#[node]
impl MoveNode {
    fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
        let dx = args.named("dx")?.unwrap_or_default();
        let dy = args.named("dy")?.unwrap_or_default();
        Ok(Self {
            delta: Axes::new(dx, dy),
            body: args.expect("body")?,
        }
        .pack())
    }
}

impl Layout for MoveNode {
    fn layout(
        &self,
        vt: &mut Vt,
        styles: StyleChain,
        regions: Regions,
    ) -> SourceResult<Fragment> {
        let mut fragment = self.body.layout(vt, styles, regions)?;
        for frame in &mut fragment {
            let delta = self.delta.resolve(styles);
            let delta = delta.zip(frame.size()).map(|(d, s)| d.relative_to(s));
            frame.translate(delta.to_point());
        }
        Ok(fragment)
    }
}

impl Inline for MoveNode {}

/// Transform content without affecting layout.
///
/// # Parameters
/// - body: Content (positional, required)
///   The content to transform.
///
/// - angle: Angle (named)
///   The amount of rotation.
///
/// - x: Ratio (named)
///   The horizontal scaling factor.
///
/// - y: Ratio (named)
///   The vertical scaling factor.
///
/// # Tags
/// - layout
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
pub struct TransformNode<const T: TransformKind> {
    /// Transformation to apply to the content.
    pub transform: Transform,
    /// The content that should be transformed.
    pub body: Content,
}

/// Rotate content without affecting layout.
pub type RotateNode = TransformNode<ROTATE>;

/// Scale content without affecting layout.
pub type ScaleNode = TransformNode<SCALE>;

#[node]
impl<const T: TransformKind> TransformNode<T> {
    /// The origin of the transformation.
    #[property(resolve)]
    pub const ORIGIN: Axes<Option<GenAlign>> = Axes::default();

    fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
        let transform = match T {
            ROTATE => {
                let angle = args.named_or_find("angle")?.unwrap_or_default();
                Transform::rotate(angle)
            }
            SCALE | _ => {
                let all = args.find()?;
                let sx = args.named("x")?.or(all).unwrap_or(Ratio::one());
                let sy = args.named("y")?.or(all).unwrap_or(Ratio::one());
                Transform::scale(sx, sy)
            }
        };

        Ok(Self { transform, body: args.expect("body")? }.pack())
    }
}

impl<const T: TransformKind> Layout for TransformNode<T> {
    fn layout(
        &self,
        vt: &mut Vt,
        styles: StyleChain,
        regions: Regions,
    ) -> SourceResult<Fragment> {
        let mut fragment = self.body.layout(vt, styles, regions)?;
        for frame in &mut fragment {
            let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON);
            let Axes { x, y } = origin.zip(frame.size()).map(|(o, s)| o.position(s));
            let transform = Transform::translate(x, y)
                .pre_concat(self.transform)
                .pre_concat(Transform::translate(-x, -y));
            frame.transform(transform);
        }
        Ok(fragment)
    }
}

impl<const T: TransformKind> Inline for TransformNode<T> {}

/// Kinds of transformations.
///
/// The move transformation is handled separately.
pub type TransformKind = usize;

/// A rotational transformation.
const ROTATE: TransformKind = 1;

/// A scale transformation.
const SCALE: TransformKind = 2;