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
|
use std::f64::consts::SQRT_2;
use std::io;
use decorum::N64;
use super::*;
use crate::diag::Error;
use crate::layout::{
BackgroundNode, BackgroundShape, FixedNode, ImageNode, PadNode, Paint,
};
/// `image`: An image.
pub fn image(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let path = args.expect::<Spanned<Str>>("path to image file")?;
let width = args.named("width")?;
let height = args.named("height")?;
let full = ctx.make_path(&path.v);
let id = ctx.images.load(&full).map_err(|err| {
Error::boxed(path.span, match err.kind() {
io::ErrorKind::NotFound => "file not found".into(),
_ => format!("failed to load image ({})", err),
})
})?;
Ok(Value::Template(Template::from_inline(move |_| ImageNode {
id,
width,
height,
})))
}
/// `rect`: A rectangle with optional content.
pub fn rect(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
let fill = args.named("fill")?;
let body = args.eat().unwrap_or_default();
Ok(rect_impl(width, height, None, fill, body))
}
/// `square`: A square with optional content.
pub fn square(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let length = args.named::<Length>("length")?.map(Linear::from);
let width = match length {
Some(length) => Some(length),
None => args.named("width")?,
};
let height = match width {
Some(_) => None,
None => args.named("height")?,
};
let aspect = Some(N64::from(1.0));
let fill = args.named("fill")?;
let body = args.eat().unwrap_or_default();
Ok(rect_impl(width, height, aspect, fill, body))
}
fn rect_impl(
width: Option<Linear>,
height: Option<Linear>,
aspect: Option<N64>,
fill: Option<Color>,
body: Template,
) -> Value {
Value::Template(Template::from_inline(move |state| {
let mut node = LayoutNode::new(FixedNode {
width,
height,
aspect,
child: body.to_stack(state).into(),
});
if let Some(fill) = fill {
node = LayoutNode::new(BackgroundNode {
shape: BackgroundShape::Rect,
fill: Paint::Color(fill),
child: node,
});
}
node
}))
}
/// `ellipse`: An ellipse with optional content.
pub fn ellipse(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
let fill = args.named("fill")?;
let body = args.eat().unwrap_or_default();
Ok(ellipse_impl(width, height, None, fill, body))
}
/// `circle`: A circle with optional content.
pub fn circle(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let diameter = args.named("radius")?.map(|r: Length| 2.0 * Linear::from(r));
let width = match diameter {
None => args.named("width")?,
diameter => diameter,
};
let height = match width {
None => args.named("height")?,
width => width,
};
let aspect = Some(N64::from(1.0));
let fill = args.named("fill")?;
let body = args.eat().unwrap_or_default();
Ok(ellipse_impl(width, height, aspect, fill, body))
}
fn ellipse_impl(
width: Option<Linear>,
height: Option<Linear>,
aspect: Option<N64>,
fill: Option<Color>,
body: Template,
) -> Value {
Value::Template(Template::from_inline(move |state| {
// This padding ratio ensures that the rectangular padded region fits
// perfectly into the ellipse.
const PAD: f64 = 0.5 - SQRT_2 / 4.0;
let mut node = LayoutNode::new(FixedNode {
width,
height,
aspect,
child: LayoutNode::new(PadNode {
padding: Sides::splat(Relative::new(PAD).into()),
child: body.to_stack(state).into(),
}),
});
if let Some(fill) = fill {
node = LayoutNode::new(BackgroundNode {
shape: BackgroundShape::Ellipse,
fill: Paint::Color(fill),
child: node,
});
}
node
}))
}
|