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
|
//! Affine transformations on nodes.
use super::prelude::*;
use crate::geom::Transform;
/// Transform a node without affecting layout.
#[derive(Debug, Hash)]
pub struct TransformNode<const T: TransformKind> {
/// Transformation to apply to the contents.
pub transform: Transform,
/// The node whose contents should be transformed.
pub child: LayoutNode,
}
#[class]
impl<const T: TransformKind> TransformNode<T> {
/// The origin of the transformation.
pub const ORIGIN: Spec<Option<Align>> = Spec::default();
fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> {
let transform = match T {
MOVE => {
let tx = args.named("x")?.unwrap_or_default();
let ty = args.named("y")?.unwrap_or_default();
Transform::translation(tx, ty)
}
ROTATE => {
let angle = args.named_or_find("angle")?.unwrap_or_default();
Transform::rotation(angle)
}
SCALE | _ => {
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());
Transform::scale(sx, sy)
}
};
Ok(Template::inline(Self {
transform,
child: args.expect("body")?,
}))
}
}
impl<const T: TransformKind> Layout for TransformNode<T> {
fn layout(
&self,
vm: &mut Vm,
regions: &Regions,
styles: StyleChain,
) -> TypResult<Vec<Arc<Frame>>> {
let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON);
let mut frames = self.child.layout(vm, regions, styles)?;
for frame in &mut frames {
let Spec { x, y } = 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));
Arc::make_mut(frame).transform(transform);
}
Ok(frames)
}
}
/// Kinds of transformations.
pub type TransformKind = usize;
/// A translation on the X and Y axes.
pub const MOVE: TransformKind = 0;
/// A rotational transformation.
pub const ROTATE: TransformKind = 1;
/// A scale transformation.
pub const SCALE: TransformKind = 2;
|