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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use mathml_renderer::{
arena::Arena,
ast::{Node, RowAttrs},
attribute::{MathSpacing, OpAttrs, Style, TextTransform},
symbol::{self, MathMLOperator, OrdCategory, OrdLike, Rel, RelCategory},
};
use crate::token::ForceStretchy;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Class {
/// `mathord`
#[default]
Default = 0,
/// `mathop`
Operator,
/// `mathbin`
BinaryOp,
/// `mathrel`
Relation,
/// `mathopen`
Open,
/// `mathclose`
Close,
/// `mathpunct`
Punctuation,
/// `mathinner`
Inner,
/// A class indicating the end of the current formula.
End,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParenType {
Left = 1,
Right,
Middle,
}
/// <mi> mathvariant attribute
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MathVariant {
/// This is enforced by setting `mathvariant="normal"`.
Normal,
/// This is enforced by transforming the characters themselves.
Transform(TextTransform),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stretchy {
/// The operator is always stretchy (e.g. `(`, `)`).
/// (Or, it's [`ForceStretchy::Pretend`].)
Always = 1,
/// The operator is only stretchy as a pre- or postfix operator (e.g. `|`).
PrePostfix,
/// The operator is never stretchy (e.g. `/`).
Never,
/// The operator is always stretchy but isn't symmetric (e.g. `↑`).
AlwaysAsymmetric,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DelimiterSpacing {
/// Never has any spacing, even when used as an infix operator (e.g. `(`, `)`).
Zero,
/// Has relation spacing when used as an infix operator, but not when used as a prefix or
/// postfix operator (e.g. `|`).
InfixRelation,
/// Always has relation spacing, even when used as a prefix or postfix operator (e.g. `↑`).
Relation,
/// Always has some spacing, even when used as a prefix or postfix operator (e.g. `/`).
Other,
}
/// A stretchable operator.
///
/// It can be created from an `OrdLike` or a `Rel` if the operator is stretchable. This struct
/// carries all the information needed to know how to make the operator stretchy and how to set
/// spacing around it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StretchableOp {
op: MathMLOperator,
pub stretchy: Stretchy,
pub spacing: DelimiterSpacing,
}
impl StretchableOp {
#[inline]
pub const fn as_op(self) -> MathMLOperator {
self.op
}
/// Creates a `StretchableOp` from an `OrdLike` if it's stretchable. Returns `None` if the
/// operator isn't stretchable.
pub const fn from_ord(ord: OrdLike) -> Option<Self> {
let (stretchy, spacing) = match ord.category() {
OrdCategory::F | OrdCategory::G | OrdCategory::FG => {
(Stretchy::Always, DelimiterSpacing::Zero)
}
OrdCategory::FGandForceDefault => {
(Stretchy::PrePostfix, DelimiterSpacing::InfixRelation)
}
OrdCategory::K => (Stretchy::Never, DelimiterSpacing::Zero),
OrdCategory::KButUsedToBeB => (Stretchy::Never, DelimiterSpacing::Other),
OrdCategory::D
| OrdCategory::E
| OrdCategory::DE
| OrdCategory::I
| OrdCategory::IK => {
return None;
}
};
Some(StretchableOp {
op: ord.as_op(),
stretchy,
spacing,
})
}
/// Creates a `StretchableOp` from a `Rel` if it's stretchable. Returns `None` if the operator
/// isn't stretchable.
pub const fn from_rel(rel: Rel) -> Option<Self> {
match rel.category() {
RelCategory::A => Some(StretchableOp {
op: rel.as_op(),
stretchy: Stretchy::AlwaysAsymmetric,
spacing: DelimiterSpacing::Relation,
}),
RelCategory::Default | RelCategory::DandForceDefault => None,
}
}
/// Creates a `StretchableOp` from a [`ForceStretchy`] if it's stretchable.
/// Used with `ForceOpen`/`ForceClose`.
pub const fn from_force_stretchy(op: MathMLOperator, stretch: ForceStretchy) -> Option<Self> {
match stretch {
ForceStretchy::Yes => Some(StretchableOp {
op,
stretchy: Stretchy::Never,
spacing: DelimiterSpacing::Zero,
}),
ForceStretchy::Pretend => Some(StretchableOp {
op,
stretchy: Stretchy::Always,
spacing: DelimiterSpacing::Zero,
}),
ForceStretchy::No => None,
}
}
}
/// Creates a fenced expression where opening and closing delimiters are stretched to fit the height
/// of the content. If `open` or `close` is `None`, no delimiter will be rendered on that side.
pub fn fenced<'arena>(
arena: &'arena Arena,
mut content: Vec<&'arena Node<'arena>>,
open: Option<StretchableOp>,
close: Option<StretchableOp>,
style: Option<Style>,
) -> Node<'arena> {
fn to_operator(delim: Option<StretchableOp>) -> Node<'static> {
if let Some(op) = delim {
let attrs = if matches!(op.stretchy, Stretchy::Never) {
OpAttrs::STRETCHY_TRUE
} else {
OpAttrs::empty()
};
let (left, right) = if matches!(
op.spacing,
DelimiterSpacing::Relation | DelimiterSpacing::Other
) {
(Some(MathSpacing::Zero), Some(MathSpacing::Zero))
} else {
(None, None)
};
Node::Operator {
op: op.as_op(),
attrs,
size: None,
left,
right,
}
} else {
// An empty `<mo></mo>` produces weird spacing in some browsers.
// Use U+2063 (INVISIBLE SEPARATOR) to work around this. It's in Category K in MathML Core.
Node::Operator {
op: const { symbol::INVISIBLE_SEPARATOR.as_op() },
attrs: OpAttrs::empty(),
size: None,
left: None,
right: None,
}
}
}
let open = arena.push(to_operator(open));
let close = arena.push(to_operator(close));
content.insert(0, open);
content.push(close);
let nodes = arena.push_slice(&content);
Node::Row {
nodes,
attrs: RowAttrs {
style,
..RowAttrs::DEFAULT
},
}
}
#[cfg(test)]
mod tests {
use super::{MathVariant, TextTransform};
#[test]
fn size_test() {
assert_eq!(
std::mem::size_of::<MathVariant>(),
std::mem::size_of::<TextTransform>()
);
assert_eq!(
std::mem::size_of::<Option<MathVariant>>(),
std::mem::size_of::<TextTransform>()
);
}
}