#[derive(Debug, Clone, Copy)]
pub enum FontStyle {
BlackBoardBold,
FrakturBold,
Script,
}
pub fn stylize(input: &str, style: FontStyle) -> String {
input.chars().map(|c| stylize_char(c, style)).collect()
}
fn stylize_char(c: char, style: FontStyle) -> char {
match style {
FontStyle::BlackBoardBold => match c {
'C' => char::from_u32(0x2102),
'H' => char::from_u32(0x210D),
'N' => char::from_u32(0x2115),
'P' => char::from_u32(0x2119),
'Q' => char::from_u32(0x211A),
'R' => char::from_u32(0x211D),
'Z' => char::from_u32(0x2124),
'A'..='Z' => char::from_u32(0x1D538 + (c as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D552 + (c as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7D8 + (c as u32 - '0' as u32)),
_ => None,
},
FontStyle::FrakturBold => match c {
'A'..='Z' => char::from_u32(0x1D56C + (c as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D586 + (c as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7CE + (c as u32 - '0' as u32)),
_ => None,
},
FontStyle::Script => match c {
'A'..='Z' => char::from_u32(0x1D4D0 + (c as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D4EA + (c as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7CE + (c as u32 - '0' as u32)),
_ => None,
},
}
.unwrap_or(c)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stylize() {
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let tests = [
(
FontStyle::Script,
"๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐๐๐๐๐๐๐๐๐",
),
(
FontStyle::FrakturBold,
"๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐",
),
(
FontStyle::BlackBoardBold,
"๐ธ๐นโ๐ป๐ผ๐ฝ๐พโ๐๐๐๐๐โ๐โโโ๐๐๐๐๐๐๐โค๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐๐๐๐๐๐๐๐๐ ๐ก",
),
];
tests
.iter()
.for_each(|test| assert_eq!(stylize(text, test.0), test.1));
}
}