flm_core/stdlib/backends/text/superscript.rs
1//! The Unicode superscript and subscript forms of characters, the only table of
2//! these forms in the library: the text backend prints a superscript or a
3//! subscript span with them, and the numbering feature's number formats
4//! `#{unicodesuperscript}` and `#{unicodesubscript}` write their digits with
5//! them.
6//!
7//! Plain text has no raised or lowered characters of its own, but Unicode
8//! encodes a raised and a lowered form of some characters: the ten digits, five
9//! signs, and a number of letters, most of them taken from phonetic notation.
10//! [`superscript_form`] and [`subscript_form`] return that form for one
11//! character, and `None` for a character that has none. The text emitter
12//! writes a character that has no form unchanged, beside its converted
13//! neighbors: `10a` in a superscript span is `¹⁰ᵃ`, while `q` stays `q`, since
14//! this table holds no superscript `q`.
15//!
16//! A program that chooses the number template of a [counter
17//! style](crate::stdlib::features::numbering::CounterStyleDecl) whose numbers
18//! the text output shows raised (for instance a footnote's mark numbered with
19//! `#{alph}`) can check here which of the characters that template writes
20//! have a superscript form.
21//!
22//! # The superscript forms
23//!
24//! Each row pairs the characters of its first column, in order, with the forms
25//! of its second column.
26//!
27//! | characters | forms |
28//! |---|---|
29//! | digits: `0123456789` | `⁰¹²³⁴⁵⁶⁷⁸⁹` |
30//! | signs: `+-=()` | `⁺⁻⁼⁽⁾` |
31//! | lowercase letters: `abcdefghijklmnoprstuvwxyz` | `ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ` |
32//! | capital letters: `ABDEGHIJKLMNOPRTUVW` | `ᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁⱽᵂ` |
33//!
34//! The lowercase letters lack `q`; the capitals lack `C`, `F`, `Q`, `S`, `X`,
35//! `Y` and `Z`.
36//!
37//! # The subscript forms
38//!
39//! | characters | forms |
40//! |---|---|
41//! | digits: `0123456789` | `₀₁₂₃₄₅₆₇₈₉` |
42//! | signs: `+-=()` | `₊₋₌₍₎` |
43//! | lowercase letters: `aehijklmnoprstuvx` | `ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ` |
44//!
45//! No capital letter has a subscript form.
46//!
47//! # Forms the table leaves out
48//!
49//! The forms are those of the Unicode blocks that fonts have covered for a
50//! long time (Latin-1, Spacing Modifier Letters, the two Phonetic Extensions
51//! blocks, Superscripts and Subscripts, Latin Extended-C). The few later
52//! additions (a superscript `q` and the superscript capitals `C`, `F` and `Q`)
53//! are left out, because a reader's font is unlikely to show them. The Greek
54//! modifier letters are left out too, because they are raised forms of Greek
55//! characters, not of the Latin ones a number format writes.
56
57const SUPERSCRIPTS: [(char, char); 59] = [
58 ('0', '\u{2070}'), ('1', '\u{00b9}'), ('2', '\u{00b2}'), ('3', '\u{00b3}'), ('4', '\u{2074}'),
59 ('5', '\u{2075}'), ('6', '\u{2076}'), ('7', '\u{2077}'), ('8', '\u{2078}'), ('9', '\u{2079}'),
60 ('+', '\u{207a}'), ('-', '\u{207b}'), ('=', '\u{207c}'), ('(', '\u{207d}'), (')', '\u{207e}'),
61 ('a', '\u{1d43}'), ('b', '\u{1d47}'), ('c', '\u{1d9c}'), ('d', '\u{1d48}'), ('e', '\u{1d49}'),
62 ('f', '\u{1da0}'), ('g', '\u{1d4d}'), ('h', '\u{02b0}'), ('i', '\u{2071}'), ('j', '\u{02b2}'),
63 ('k', '\u{1d4f}'), ('l', '\u{02e1}'), ('m', '\u{1d50}'), ('n', '\u{207f}'), ('o', '\u{1d52}'),
64 ('p', '\u{1d56}'), ('r', '\u{02b3}'), ('s', '\u{02e2}'), ('t', '\u{1d57}'), ('u', '\u{1d58}'),
65 ('v', '\u{1d5b}'), ('w', '\u{02b7}'), ('x', '\u{02e3}'), ('y', '\u{02b8}'), ('z', '\u{1dbb}'),
66 ('A', '\u{1d2c}'), ('B', '\u{1d2e}'), ('D', '\u{1d30}'), ('E', '\u{1d31}'), ('G', '\u{1d33}'),
67 ('H', '\u{1d34}'), ('I', '\u{1d35}'), ('J', '\u{1d36}'), ('K', '\u{1d37}'), ('L', '\u{1d38}'),
68 ('M', '\u{1d39}'), ('N', '\u{1d3a}'), ('O', '\u{1d3c}'), ('P', '\u{1d3e}'), ('R', '\u{1d3f}'),
69 ('T', '\u{1d40}'), ('U', '\u{1d41}'), ('V', '\u{2c7d}'), ('W', '\u{1d42}'),
70];
71
72const SUBSCRIPTS: [(char, char); 32] = [
73 ('0', '\u{2080}'), ('1', '\u{2081}'), ('2', '\u{2082}'), ('3', '\u{2083}'), ('4', '\u{2084}'),
74 ('5', '\u{2085}'), ('6', '\u{2086}'), ('7', '\u{2087}'), ('8', '\u{2088}'), ('9', '\u{2089}'),
75 ('+', '\u{208a}'), ('-', '\u{208b}'), ('=', '\u{208c}'), ('(', '\u{208d}'), (')', '\u{208e}'),
76 ('a', '\u{2090}'), ('e', '\u{2091}'), ('h', '\u{2095}'), ('i', '\u{1d62}'), ('j', '\u{2c7c}'),
77 ('k', '\u{2096}'), ('l', '\u{2097}'), ('m', '\u{2098}'), ('n', '\u{2099}'), ('o', '\u{2092}'),
78 ('p', '\u{209a}'), ('r', '\u{1d63}'), ('s', '\u{209b}'), ('t', '\u{209c}'), ('u', '\u{1d64}'),
79 ('v', '\u{1d65}'), ('x', '\u{2093}'),
80];
81
82/// Returns the Unicode superscript form of `c` (`¹` for `1`, `ᵃ` for `a`), or
83/// `None` when the [table](self) holds none for it.
84///
85/// # Examples
86///
87/// ```
88/// use flm_core::stdlib::backends::text::superscript::superscript_form;
89///
90/// assert_eq!(superscript_form('1'), Some('¹'));
91/// assert_eq!(superscript_form('a'), Some('ᵃ'));
92/// assert_eq!(superscript_form('q'), None);
93/// ```
94pub fn superscript_form(c: char) -> Option<char> {
95 form_in(&SUPERSCRIPTS, c)
96}
97
98/// Returns the Unicode subscript form of `c` (`₁` for `1`, `ₐ` for `a`), or
99/// `None` when the [table](self) holds none for it.
100///
101/// # Examples
102///
103/// ```
104/// use flm_core::stdlib::backends::text::superscript::subscript_form;
105///
106/// assert_eq!(subscript_form('1'), Some('₁'));
107/// assert_eq!(subscript_form('b'), None);
108/// ```
109pub fn subscript_form(c: char) -> Option<char> {
110 form_in(&SUBSCRIPTS, c)
111}
112
113/// The form that `table` pairs with `c`, if any.
114fn form_in(table: &[(char, char)], c: char) -> Option<char> {
115 table.iter().find(|(from, _)| *from == c).map(|(_, form)| *form)
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 /// The rows of the table printed under `heading` in this module's
123 /// documentation, as the characters of the first column and the forms of
124 /// the second, all rows joined.
125 fn printed(heading: &str) -> Vec<(char, char)> {
126 let source = include_str!("superscript.rs");
127 let section = source.split(heading).nth(1).expect("the heading is in the documentation");
128 let mut pairs = Vec::new();
129 for line in section.lines().skip_while(|line| !line.starts_with("//! |")) {
130 let Some(row) = line.strip_prefix("//! |") else { break };
131 let cells: Vec<&str> = row.split('`').collect();
132 if cells.len() < 4 {
133 continue;
134 }
135 pairs.extend(cells[1].chars().zip(cells[3].chars()));
136 }
137 pairs
138 }
139
140 #[test]
141 fn the_printed_tables_are_the_tables() {
142 assert_eq!(printed("# The superscript forms"), SUPERSCRIPTS.to_vec());
143 assert_eq!(printed("# The subscript forms"), SUBSCRIPTS.to_vec());
144 }
145
146 #[test]
147 fn every_character_has_at_most_one_form_and_no_form_is_shared() {
148 for table in [&SUPERSCRIPTS[..], &SUBSCRIPTS[..]] {
149 let mut from: Vec<char> = table.iter().map(|(c, _)| *c).collect();
150 let mut to: Vec<char> = table.iter().map(|(_, f)| *f).collect();
151 let count = table.len();
152 from.sort_unstable();
153 from.dedup();
154 to.sort_unstable();
155 to.dedup();
156 assert_eq!((from.len(), to.len()), (count, count));
157 }
158 }
159
160 #[test]
161 fn the_digits_are_the_numbering_formats_digits() {
162 let raised: String = ('0'..='9').filter_map(superscript_form).collect();
163 assert_eq!(raised, "⁰¹²³⁴⁵⁶⁷⁸⁹");
164 let lowered: String = ('0'..='9').filter_map(subscript_form).collect();
165 assert_eq!(lowered, "₀₁₂₃₄₅₆₇₈₉");
166 }
167
168 #[test]
169 fn a_character_with_no_form_answers_none() {
170 for c in ['q', 'S', 'Q', ' ', '.', 'é', '\u{b9}'] {
171 assert_eq!(superscript_form(c), None, "{c:?}");
172 }
173 for c in ['b', 'A', 'y', ' '] {
174 assert_eq!(subscript_form(c), None, "{c:?}");
175 }
176 }
177}