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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
use crate::layout::AlignElem;
use super::*;
pub const TIGHT_LEADING: Em = Em::new(0.25);
#[derive(Debug, Default, Clone)]
pub struct MathRow(Vec<MathFragment>);
impl MathRow {
pub fn new(fragments: Vec<MathFragment>) -> Self {
let iter = fragments.into_iter().peekable();
let mut last: Option<usize> = None;
let mut space: Option<MathFragment> = None;
let mut resolved: Vec<MathFragment> = vec![];
for mut fragment in iter {
match fragment {
// Keep space only if supported by spaced fragments.
MathFragment::Space(_) => {
if last.is_some() {
space = Some(fragment);
}
continue;
}
// Explicit spacing disables automatic spacing.
MathFragment::Spacing(_) => {
last = None;
space = None;
resolved.push(fragment);
continue;
}
// Alignment points are resolved later.
MathFragment::Align => {
resolved.push(fragment);
continue;
}
// New line, new things.
MathFragment::Linebreak => {
resolved.push(fragment);
space = None;
last = None;
continue;
}
_ => {}
}
// Convert variable operators into binary operators if something
// precedes them and they are not preceded by a operator or comparator.
if fragment.class() == Some(MathClass::Vary)
&& matches!(
last.and_then(|i| resolved[i].class()),
Some(
MathClass::Normal
| MathClass::Alphabetic
| MathClass::Closing
| MathClass::Fence
)
)
{
fragment.set_class(MathClass::Binary);
}
// Insert spacing between the last and this item.
if let Some(i) = last {
if let Some(s) = spacing(&resolved[i], space.take(), &fragment) {
resolved.insert(i + 1, s);
}
}
last = Some(resolved.len());
resolved.push(fragment);
}
Self(resolved)
}
pub fn iter(&self) -> std::slice::Iter<'_, MathFragment> {
self.0.iter()
}
pub fn width(&self) -> Abs {
self.iter().map(MathFragment::width).sum()
}
pub fn height(&self) -> Abs {
self.ascent() + self.descent()
}
pub fn ascent(&self) -> Abs {
self.iter().map(MathFragment::ascent).max().unwrap_or_default()
}
pub fn descent(&self) -> Abs {
self.iter().map(MathFragment::descent).max().unwrap_or_default()
}
pub fn into_frame(self, ctx: &MathContext) -> Frame {
let styles = ctx.styles();
let align = AlignElem::alignment_in(styles).x.resolve(styles);
self.into_aligned_frame(ctx, &[], align)
}
pub fn into_fragment(self, ctx: &MathContext) -> MathFragment {
if self.0.len() == 1 {
self.0.into_iter().next().unwrap()
} else {
FrameFragment::new(ctx, self.into_frame(ctx)).into()
}
}
pub fn into_aligned_frame(
mut self,
ctx: &MathContext,
points: &[Abs],
align: Align,
) -> Frame {
if self.iter().any(|frag| matches!(frag, MathFragment::Linebreak)) {
let fragments: Vec<_> = std::mem::take(&mut self.0);
let leading = if ctx.style.size >= MathSize::Text {
ParElem::leading_in(ctx.styles())
} else {
TIGHT_LEADING.scaled(ctx)
};
let mut rows: Vec<_> = fragments
.split(|frag| matches!(frag, MathFragment::Linebreak))
.map(|slice| Self(slice.to_vec()))
.collect();
if matches!(rows.last(), Some(row) if row.0.is_empty()) {
rows.pop();
}
let width = rows.iter().map(|row| row.width()).max().unwrap_or_default();
let points = alignments(&rows);
let mut frame = Frame::new(Size::zero());
for (i, row) in rows.into_iter().enumerate() {
let sub = row.into_line_frame(&points, align);
let size = frame.size_mut();
if i > 0 {
size.y += leading;
}
let mut pos = Point::with_y(size.y);
if points.is_empty() {
pos.x = align.position(width - sub.width());
}
size.y += sub.height();
size.x.set_max(sub.width());
frame.push_frame(pos, sub);
}
frame
} else {
self.into_line_frame(points, align)
}
}
fn into_line_frame(self, points: &[Abs], align: Align) -> Frame {
let ascent = self.ascent();
let descent = self.descent();
let size = Size::new(Abs::zero(), ascent + descent);
let mut frame = Frame::new(size);
let mut x = Abs::zero();
frame.set_baseline(ascent);
if let (Some(&first), Align::Center) = (points.first(), align) {
let mut offset = first;
for fragment in self.iter() {
offset -= fragment.width();
if matches!(fragment, MathFragment::Align) {
x = offset;
break;
}
}
}
let fragments = self.0.into_iter().peekable();
let mut i = 0;
for fragment in fragments {
if matches!(fragment, MathFragment::Align) {
if let Some(&point) = points.get(i) {
x = point;
}
i += 1;
continue;
}
let y = ascent - fragment.ascent();
let pos = Point::new(x, y);
x += fragment.width();
frame.push_frame(pos, fragment.into_frame());
}
frame.size_mut().x = x;
frame
}
}
impl<T: Into<MathFragment>> From<T> for MathRow {
fn from(fragment: T) -> Self {
Self(vec![fragment.into()])
}
}
|