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
|
use crate::diag::bail;
use crate::foundations::{cast, elem, func, Content, NativeElement, SymbolElem};
use crate::layout::{Length, Rel};
use crate::math::Mathy;
/// Attaches an accent to a base.
///
/// # Example
/// ```example
/// $grave(a) = accent(a, `)$ \
/// $arrow(a) = accent(a, arrow)$ \
/// $tilde(a) = accent(a, \u{0303})$
/// ```
#[elem(Mathy)]
pub struct AccentElem {
/// The base to which the accent is applied. May consist of multiple
/// letters.
///
/// ```example
/// $arrow(A B C)$
/// ```
#[required]
pub base: Content,
/// The accent to apply to the base.
///
/// Supported accents include:
///
/// | Accent | Name | Codepoint |
/// | ------------- | --------------- | --------- |
/// | Grave | `grave` | <code>`</code> |
/// | Acute | `acute` | `´` |
/// | Circumflex | `hat` | `^` |
/// | Tilde | `tilde` | `~` |
/// | Macron | `macron` | `¯` |
/// | Dash | `dash` | `‾` |
/// | Breve | `breve` | `˘` |
/// | Dot | `dot` | `.` |
/// | Double dot, Diaeresis | `dot.double`, `diaer` | `¨` |
/// | Triple dot | `dot.triple` | <code>⃛</code> |
/// | Quadruple dot | `dot.quad` | <code>⃜</code> |
/// | Circle | `circle` | `∘` |
/// | Double acute | `acute.double` | `˝` |
/// | Caron | `caron` | `ˇ` |
/// | Right arrow | `arrow`, `->` | `→` |
/// | Left arrow | `arrow.l`, `<-` | `←` |
/// | Left/Right arrow | `arrow.l.r` | `↔` |
/// | Right harpoon | `harpoon` | `⇀` |
/// | Left harpoon | `harpoon.lt` | `↼` |
#[required]
pub accent: Accent,
/// The size of the accent, relative to the width of the base.
///
/// ```example
/// $dash(A, size: #150%)$
/// ```
#[resolve]
#[default(Rel::one())]
pub size: Rel<Length>,
/// Whether to remove the dot on top of lowercase i and j when adding a top
/// accent.
///
/// This enables the `dtls` OpenType feature.
///
/// ```example
/// $hat(dotless: #false, i)$
/// ```
#[default(true)]
pub dotless: bool,
}
/// An accent character.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Accent(pub char);
impl Accent {
/// Normalize a character into an accent.
pub fn new(c: char) -> Self {
Self(Self::combine(c).unwrap_or(c))
}
/// List of bottom accents. Currently just a list of ones included in the
/// Unicode math class document.
const BOTTOM: &[char] = &[
'\u{0323}', '\u{032C}', '\u{032D}', '\u{032E}', '\u{032F}', '\u{0330}',
'\u{0331}', '\u{0332}', '\u{0333}', '\u{033A}', '\u{20E8}', '\u{20EC}',
'\u{20ED}', '\u{20EE}', '\u{20EF}',
];
/// Whether this accent is a bottom accent or not.
pub fn is_bottom(&self) -> bool {
Self::BOTTOM.contains(&self.0)
}
}
/// This macro generates accent-related functions.
///
/// ```ignore
/// accents! {
/// '\u{0300}' | '`' => grave,
/// // ^^^^^^^^^ ^^^ ^^^^^
/// // | | |
/// // | | +-- The name of the function.
/// // | +--------- The alternative characters that represent the accent.
/// // +---------------------- The primary character that represents the accent.
/// }
/// ```
///
/// When combined with the `Accent::combine` function, accent characters can be normalized
/// to the primary character.
macro_rules! accents {
($($primary:literal $(| $alt:literal)* => $name:ident),* $(,)?) => {
impl Accent {
/// Normalize an accent to a combining one.
pub fn combine(c: char) -> Option<char> {
Some(match c {
$($primary $(| $alt)* => $primary,)*
_ => return None,
})
}
}
$(
/// The accent function for callable symbol definitions.
#[func]
pub fn $name(
/// The base to which the accent is applied.
base: Content,
/// The size of the accent, relative to the width of the base.
#[named]
size: Option<Rel<Length>>,
/// Whether to remove the dot on top of lowercase i and j when
/// adding a top accent.
#[named]
dotless: Option<bool>,
) -> Content {
let mut accent = AccentElem::new(base, Accent::new($primary));
if let Some(size) = size {
accent = accent.with_size(size);
}
if let Some(dotless) = dotless {
accent = accent.with_dotless(dotless);
}
accent.pack()
}
)+
};
}
// Keep it synced with the documenting table above.
accents! {
'\u{0300}' | '`' => grave,
'\u{0301}' | '´' => acute,
'\u{0302}' | '^' | 'ˆ' => hat,
'\u{0303}' | '~' | '∼' | '˜' => tilde,
'\u{0304}' | '¯' => macron,
'\u{0305}' | '-' | '‾' | '−' => dash,
'\u{0306}' | '˘' => breve,
'\u{0307}' | '.' | '˙' | '⋅' => dot,
'\u{0308}' | '¨' => dot_double,
'\u{20db}' => dot_triple,
'\u{20dc}' => dot_quad,
'\u{030a}' | '∘' | '○' => circle,
'\u{030b}' | '˝' => acute_double,
'\u{030c}' | 'ˇ' => caron,
'\u{20d6}' | '←' => arrow_l,
'\u{20d7}' | '→' | '⟶' => arrow,
'\u{20e1}' | '↔' | '⟷' => arrow_l_r,
'\u{20d0}' | '↼' => harpoon_lt,
'\u{20d1}' | '⇀' => harpoon,
}
cast! {
Accent,
self => self.0.into_value(),
v: char => Self::new(v),
v: Content => match v.to_packed::<SymbolElem>() {
Some(elem) => Self::new(elem.text),
None => bail!("expected a symbol"),
},
}
|