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
|
use super::prelude::*;
use crate::style::{Paper, PaperClass};
/// `page`: Configure pages.
pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
castable! {
Paper,
Expected: "string",
Value::Str(string) => Paper::from_name(&string).ok_or("unknown paper")?,
}
let paper = args.named::<Paper>("paper")?.or_else(|| args.find());
let width = args.named("width")?;
let height = args.named("height")?;
let margins = args.named("margins")?;
let left = args.named("left")?;
let top = args.named("top")?;
let right = args.named("right")?;
let bottom = args.named("bottom")?;
let flip = args.named("flip")?;
ctx.template.modify(move |style| {
let page = style.page_mut();
if let Some(paper) = paper {
page.class = paper.class();
page.size = paper.size();
}
if let Some(width) = width {
page.class = PaperClass::Custom;
page.size.w = width;
}
if let Some(height) = height {
page.class = PaperClass::Custom;
page.size.h = height;
}
if let Some(margins) = margins {
page.margins = Sides::splat(Some(margins));
}
if let Some(left) = left {
page.margins.left = Some(left);
}
if let Some(top) = top {
page.margins.top = Some(top);
}
if let Some(right) = right {
page.margins.right = Some(right);
}
if let Some(bottom) = bottom {
page.margins.bottom = Some(bottom);
}
if flip.unwrap_or(false) {
std::mem::swap(&mut page.size.w, &mut page.size.h);
}
});
ctx.template.pagebreak(false);
Ok(Value::None)
}
/// `pagebreak`: Start a new page.
pub fn pagebreak(_: &mut EvalContext, _: &mut Args) -> TypResult<Value> {
let mut template = Template::new();
template.pagebreak(true);
Ok(Value::Template(template))
}
/// Layouts its children onto one or multiple pages.
#[derive(Debug, Hash)]
pub struct PageNode {
/// The size of the page.
pub size: Size,
/// The node that produces the actual pages.
pub child: PackedNode,
}
impl PageNode {
/// Layout the page run into a sequence of frames, one per page.
pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<Rc<Frame>> {
// When one of the lengths is infinite the page fits its content along
// that axis.
let expand = self.size.to_spec().map(Length::is_finite);
let regions = Regions::repeat(self.size, self.size, expand);
self.child.layout(ctx, ®ions).into_iter().map(|c| c.item).collect()
}
}
|