basalt_tui/
stylized_text.rs1#[derive(Debug, Clone, Copy, PartialEq, serde::Deserialize)]
20#[serde(rename_all = "kebab-case")]
21pub enum FontStyle {
22 BlackBoardBold,
24 FrakturBold,
26 Script,
28}
29
30pub fn stylize(input: &str, style: FontStyle) -> String {
45 input.chars().map(|c| stylize_char(c, style)).collect()
46}
47
48fn stylize_char(c: char, style: FontStyle) -> char {
56 match style {
57 FontStyle::BlackBoardBold => match c {
58 'C' => char::from_u32(0x2102),
59 'H' => char::from_u32(0x210D),
60 'N' => char::from_u32(0x2115),
61 'P' => char::from_u32(0x2119),
62 'Q' => char::from_u32(0x211A),
63 'R' => char::from_u32(0x211D),
64 'Z' => char::from_u32(0x2124),
65 'A'..='Z' => char::from_u32(0x1D538 + (c as u32 - 'A' as u32)),
66 'a'..='z' => char::from_u32(0x1D552 + (c as u32 - 'a' as u32)),
67 '0'..='9' => char::from_u32(0x1D7D8 + (c as u32 - '0' as u32)),
68 _ => None,
69 },
70 FontStyle::FrakturBold => match c {
71 'A'..='Z' => char::from_u32(0x1D56C + (c as u32 - 'A' as u32)),
72 'a'..='z' => char::from_u32(0x1D586 + (c as u32 - 'a' as u32)),
73 '0'..='9' => char::from_u32(0x1D7CE + (c as u32 - '0' as u32)),
74 _ => None,
75 },
76 FontStyle::Script => match c {
77 'A'..='Z' => char::from_u32(0x1D4D0 + (c as u32 - 'A' as u32)),
78 'a'..='z' => char::from_u32(0x1D4EA + (c as u32 - 'a' as u32)),
79 '0'..='9' => char::from_u32(0x1D7CE + (c as u32 - '0' as u32)),
80 _ => None,
81 },
82 }
83 .unwrap_or(c)
84}
85
86#[cfg(test)]
87mod tests {
88 use similar_asserts::assert_eq;
89
90 use super::*;
91
92 #[test]
93 fn test_stylize() {
94 let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
95 let tests = [
96 (
97 FontStyle::Script,
98 "๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐๐๐๐๐๐๐๐๐",
99 ),
100 (
101 FontStyle::FrakturBold,
102 "๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐",
103 ),
104 (
105 FontStyle::BlackBoardBold,
106 "๐ธ๐นโ๐ป๐ผ๐ฝ๐พโ๐๐๐๐๐โ๐โโโ๐๐๐๐๐๐๐โค๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐๐๐๐๐๐๐๐๐ ๐ก",
107 ),
108 ];
109
110 tests
111 .iter()
112 .for_each(|test| assert_eq!(stylize(text, test.0), test.1));
113 }
114}