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
150
151
152
153
|
use super::*;
use smallvec::smallvec;
/// Layouts syntax trees into boxes.
pub fn layout_tree(tree: &SyntaxTree, ctx: LayoutContext) -> LayoutResult<MultiLayout> {
let mut layouter = TreeLayouter::new(ctx);
layouter.layout(tree)?;
layouter.finish()
}
#[derive(Debug, Clone)]
struct TreeLayouter<'a, 'p> {
ctx: LayoutContext<'a, 'p>,
flex: FlexLayouter,
style: TextStyle,
}
impl<'a, 'p> TreeLayouter<'a, 'p> {
/// Create a new layouter.
fn new(ctx: LayoutContext<'a, 'p>) -> TreeLayouter<'a, 'p> {
TreeLayouter {
flex: FlexLayouter::new(FlexContext {
flex_spacing: flex_spacing(&ctx.text_style),
spaces: ctx.spaces.clone(),
axes: ctx.axes,
shrink_to_fit: ctx.shrink_to_fit,
}),
style: ctx.text_style.clone(),
ctx,
}
}
/// Layout a syntax tree.
fn layout(&mut self, tree: &SyntaxTree) -> LayoutResult<()> {
for node in &tree.nodes {
match &node.val {
Node::Text(text) => {
self.flex.add(layout_text(text, TextContext {
loader: &self.ctx.loader,
style: &self.style,
})?);
}
Node::Space => {
if !self.flex.run_is_empty() && !self.flex.run_last_is_space() {
let space = self.style.word_spacing * self.style.font_size;
self.flex.add_primary_space(space, true);
}
}
Node::Newline => {
if !self.flex.run_is_empty() {
self.break_paragraph()?;
}
}
Node::ToggleItalics => self.style.toggle_class(FontClass::Italic),
Node::ToggleBold => self.style.toggle_class(FontClass::Bold),
Node::ToggleMonospace => self.style.toggle_class(FontClass::Monospace),
Node::Func(func) => self.layout_func(func)?,
}
}
Ok(())
}
/// Layout a function.
fn layout_func(&mut self, func: &FuncCall) -> LayoutResult<()> {
let (flex_spaces, stack_spaces) = self.flex.remaining()?;
let ctx = |spaces| LayoutContext {
top_level: false,
text_style: &self.style,
spaces,
shrink_to_fit: true,
.. self.ctx
};
// Try putting it in the flex space first, but if that is not enough
// space, use the other space.
let commands = match func.body.val.layout(ctx(flex_spaces)) {
Ok(c) => c,
Err(LayoutError::NotEnoughSpace(_)) => {
func.body.val.layout(ctx(stack_spaces))?
},
e => e?,
};
for command in commands {
self.execute(command)?;
}
Ok(())
}
fn execute(&mut self, command: Command) -> LayoutResult<()> {
match command {
Command::LayoutTree(tree) => self.layout(tree)?,
Command::Add(layout) => self.flex.add(layout),
Command::AddMultiple(layouts) => self.flex.add_multiple(layouts),
Command::AddPrimarySpace(space) => self.flex.add_primary_space(space, false),
Command::AddSecondarySpace(space) => self.flex.add_secondary_space(space)?,
Command::FinishLine => self.flex.add_break(),
Command::FinishRun => self.flex.finish_run()?,
Command::FinishSpace => self.flex.finish_space(true)?,
Command::BreakParagraph => self.break_paragraph()?,
Command::SetTextStyle(style) => self.style = style,
Command::SetPageStyle(style) => {
if !self.ctx.top_level {
Err(LayoutError::Unallowed("can only set page style from top level"))?;
}
self.ctx.page_style = style;
self.flex.set_spaces(smallvec![
LayoutSpace {
dimensions: style.dimensions,
padding: style.margins,
}
], true);
},
Command::SetAxes(axes) => {
self.flex.set_axes(axes);
self.ctx.axes = axes;
}
}
Ok(())
}
/// Finish the layout.
fn finish(self) -> LayoutResult<MultiLayout> {
self.flex.finish()
}
/// Finish the current flex layout and add space after it.
fn break_paragraph(&mut self) -> LayoutResult<()> {
self.flex.add_secondary_space(paragraph_spacing(&self.style))
}
}
fn flex_spacing(style: &TextStyle) -> Size {
(style.line_spacing - 1.0) * style.font_size
}
fn paragraph_spacing(style: &TextStyle) -> Size {
(style.paragraph_spacing - 1.0) * style.font_size
}
|