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
|
use crate::func::prelude::*;
/// Ends the current page.
#[derive(Debug, PartialEq)]
pub struct Pagebreak;
function! {
data: Pagebreak,
parse: plain,
layout(_, _) {
Ok(commands![Command::FinishLayout])
}
}
/// Ends the current line.
#[derive(Debug, PartialEq)]
pub struct Linebreak;
function! {
data: Linebreak,
parse: plain,
layout(_, _) {
Ok(commands![Command::FinishFlexRun])
}
}
/// Aligns content in different ways.
#[derive(Debug, PartialEq)]
pub struct Align {
body: Option<SyntaxTree>,
alignment: Alignment,
}
function! {
data: Align,
parse(args, body, ctx) {
let body = parse!(optional: body, ctx);
let alignment = match args.get_ident()? {
"left" => Alignment::Left,
"right" => Alignment::Right,
"center" => Alignment::Center,
s => err!("invalid alignment specifier: {}", s),
};
args.done()?;
Ok(Align {
body,
alignment,
})
}
layout(this, ctx) {
Ok(commands![match &this.body {
Some(body) => {
Command::AddMany(layout_tree(body, LayoutContext {
alignment: this.alignment,
.. ctx
})?)
}
None => Command::SetAlignment(this.alignment)
}])
}
}
/// Layouts content into a box.
#[derive(Debug, PartialEq)]
pub struct Boxed {
body: SyntaxTree,
flow: Flow,
}
function! {
data: Boxed,
parse(args, body, ctx) {
let body = parse!(required: body, ctx);
let mut flow = Flow::Vertical;
if let Some(ident) = args.get_ident_if_present()? {
flow = match ident {
"vertical" => Flow::Vertical,
"horizontal" => Flow::Horizontal,
f => err!("invalid flow specifier: {}", f),
};
}
args.done()?;
Ok(Boxed {
body,
flow,
})
}
layout(this, ctx) {
Ok(commands![
Command::AddMany(layout_tree(&this.body, LayoutContext {
flow: this.flow,
.. ctx
})?)
])
}
}
macro_rules! spacefunc {
($ident:ident, $name:expr, $var:ident => $command:expr) => {
/// Adds whitespace.
#[derive(Debug, PartialEq)]
pub struct $ident(Spacing);
function! {
data: $ident,
parse(args, body, _ctx) {
parse!(forbidden: body);
let spacing = match args.get_expr()? {
Expression::Size(s) => Spacing::Absolute(*s),
Expression::Number(f) => Spacing::Relative(*f as f32),
_ => err!("invalid spacing, expected size or number"),
};
Ok($ident(spacing))
}
layout(this, ctx) {
let $var = match this.0 {
Spacing::Absolute(s) => s,
Spacing::Relative(f) => Size::pt(f * ctx.style.font_size),
};
Ok(commands![$command])
}
}
};
}
/// Absolute or font-relative spacing.
#[derive(Debug, PartialEq)]
enum Spacing {
Absolute(Size),
Relative(f32),
}
spacefunc!(HorizontalSpace, "h", space => Command::AddFlex(Layout::empty(space, Size::zero())));
spacefunc!(VerticalSpace, "v", space => Command::Add(Layout::empty(Size::zero(), space)));
|