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
//! Unicode font conversion.
//!
//! Characters with font variants encoded in Unicode have a best-effort chosen `plain` variant.
//! `Plain` unicodes can be converted to different font variants using this crate.
//!
//! Most of the conversions use mathematical characters.
//! See [`Variant`] for the font variants included.
//!
//! # Examples
//!
//! Transform a character to a few fonts.
//! ```
//! assert_eq!(unicode_font::try_as_bold(&'a'), Some(&'\u{1D41A}')); // 𝐚
//! assert_eq!(unicode_font::try_as_circled(&'a'), Some(&'\u{24D0}')); // ⓐ
//! assert_eq!(unicode_font::try_as_monospace(&'a'), Some(&'\u{1D68A}')); // 𝚊
//! assert_eq!(unicode_font::try_as_small_capital(&'a'), None);
//! assert_eq!(unicode_font::try_as_small_capital(&'A'), Some(&'\u{1D00}')); // ᴀ
//! assert_eq!(unicode_font::try_as_squared(&'a'), None);
//! assert_eq!(unicode_font::try_as_squared(&'A'), Some(&'\u{1F130}')); // 🄰
//! ```
//!
//! Transform a `&str` if all characters transform.
//! ```
//! fn as_bold(s: &str) -> Option<String> {
//! s.chars().map(|c| unicode_font::try_as_bold(&c).cloned()).collect()
//! }
//!
//! assert_eq!(as_bold("abc"), Some(String::from("𝐚𝐛𝐜")));
//! ```
//
// # Implementation
//
// We follow the following pattern.
// - Conversion to plain variant
// - Conversion from plain variant to font variant.
// Each convertion is a table for `char` to `char` conversion.
// Some graphemes are longer than a `char`. Then, we use fixed sized arrays.
macro_rules! modules {
( $( $variant: ident ), *) => {
$(
paste::paste! {
pub use crate::$variant::[< try_as_ $variant >];
}
#[doc= stringify!(Variant of Unicode symbols.)]
pub mod $variant {
/// Mapping of plain characters to its variant.
///
/// # Warnings
///
/// There is no Unicode standard for fonts.
/// This is a best-effort mapping.
///
/// # Remarks
///
/// In Unicode terms, this is a simple map since it maps `char` to `char`.
#[cfg(not(feature = "extension"))]
paste::paste! {
pub const [< $variant:upper _MAP >]: phf::Map<char, char> = include!(stringify!($variant));
}
#[cfg(feature = "extension")]
paste::paste! {
pub const [< $variant:upper _MAP >]: phf::Map<char, char> = include!(stringify!($variant.extension));
}
paste::paste! {
/// Returns the variant version of a character if there is any.
pub fn [< try_as_ $variant >](s: &char) -> Option<&char> {
// For a speed up, use the map directly
[< $variant:upper _MAP >].get(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::try_as_plain;
paste::paste! {
#[test]
fn [< $variant _keys_are_plain >]() {
let keys: Vec<_> = [< $variant:upper _MAP >].keys().cloned().collect();
let plain: Vec<_> = keys.clone().into_iter().map(|c| crate::try_as_plain(&c).cloned().unwrap()).collect();
assert_eq!(keys, plain);
}
}
paste::paste! {
#[test]
fn [< $variant _values_are_supported >]() {
for c in [< $variant:upper _MAP >].values() {
crate::try_as_plain(&c).expect(&format!("failed to transform character {} to plain.", c));
}
}
}
paste::paste! {
#[test]
fn [< roundtrip_ $variant _plain_ $variant >]() {
let variant: Vec<_> = [< $variant:upper _MAP >].values().cloned().collect();
let double_variant: Vec<_> = variant.clone().into_iter().map(|c| try_as_plain(&c).cloned().unwrap()).map(|c| [< try_as_ $variant:lower >](&c).cloned().unwrap()).collect();
assert_eq!(variant, double_variant);
}
}
}
}
)*
}
}
modules!(
arabic_mathematical,
bold,
bold_italic,
bold_fraktur,
bold_script,
circled,
comma,
double_struck,
fraktur,
full_stop,
fullwidth,
italic,
looped,
monospace,
negative_circled,
negative_squared,
regional,
segmented,
other,
parenthesized,
sans_serif_bold_italic,
sans_serif_bold,
sans_serif_italic,
sans_serif,
script,
small_capital,
superscript,
subscript,
stretched,
squared,
tailed,
wide
);
pub mod variant;
pub use variant::Variant;
pub use plain::try_as_plain;
/// Plain variant of Unicode symbols.
pub mod plain {
/// Mapping of characters and its plain (upright, serifed) variant.
///
/// # Warnings
///
/// There is no Unicode standard for fonts.
/// This is a best-effort mapping.
///
/// # Remarks
///
/// Plain symbols are mapped to themselves.
/// This helps indicating if a symbols is considered in this best-effort collection.
///
/// In Unicode terms, this is a simple map since it maps `char` to `char`.
///
/// # Examples
///
/// Get the plan version of a character.
/// ```
/// use unicode_font::plain::PLAIN_MAP;
/// let fancy_zero = '\u{1D7D8}'; // 𝟘
/// assert_eq!(PLAIN_MAP.get(&fancy_zero).unwrap(), &'0');
/// ```
#[cfg(not(feature = "extension"))]
pub const PLAIN_MAP: phf::Map<char, char> = include!("plain");
#[cfg(feature = "extension")]
pub const PLAIN_MAP: phf::Map<char, char> = include!("plain.extension");
/// Returns the plain version of the character, if the character is supported.
///
/// # Examples
///
/// Get the plain version of a character.
/// ```
/// let fancy_zero = '\u{1D7D8}'; // 𝟘
/// assert_eq!(unicode_font::try_as_plain(&fancy_zero).unwrap(), &'0');
/// ```
pub fn try_as_plain(s: &char) -> Option<&char> {
PLAIN_MAP.get(s)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
/// Bold symbols are transformed into themselves.
fn double_plain_is_plain() {
let plain: Vec<_> = PLAIN_MAP.values().cloned().collect();
let double_plain: Vec<_> = plain.clone().into_iter().map(|c| try_as_plain(&c).cloned().unwrap()).collect();
assert_eq!(plain, double_plain);
}
}
}