mod tables;
use crate::FamilyName;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SymbolFont {
Symbol,
Wingdings,
Wingdings2,
Wingdings3,
Webdings,
}
const CODES: std::ops::RangeInclusive<u32> = 0x20..=0xFF;
const PRIVATE_USE_START: u32 = 0xF000;
impl SymbolFont {
pub const ALL: [Self; 5] = [
Self::Symbol,
Self::Wingdings,
Self::Wingdings2,
Self::Wingdings3,
Self::Webdings,
];
pub const DRAWING_FAMILY: &'static str = "Noto Symbols CF";
#[must_use]
pub const fn family(self) -> &'static str {
match self {
Self::Symbol => "Symbol",
Self::Wingdings => "Wingdings",
Self::Wingdings2 => "Wingdings 2",
Self::Wingdings3 => "Wingdings 3",
Self::Webdings => "Webdings",
}
}
#[must_use]
pub fn of(family: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|font| FamilyName::from_static(font.family()).is(family))
}
#[must_use]
pub fn code(character: char) -> Option<u8> {
let value = u32::from(character);
let low = value
.checked_sub(PRIVATE_USE_START)
.filter(|low| CODES.contains(low));
let low = low.or(Some(value).filter(|value| CODES.contains(value)))?;
u8::try_from(low).ok()
}
#[must_use]
pub fn character(self, code: char) -> Option<char> {
let low = Self::code(code)?;
let index = usize::try_from(u32::from(low) - CODES.start()).ok()?;
tables::CODES
.get(self as usize)?
.chars()
.nth(index)
.filter(|&ch| ch != char::REPLACEMENT_CHARACTER)
}
}
#[cfg(test)]
mod tests {
use super::SymbolFont;
#[test]
fn takes_a_code_in_its_private_use_form_or_its_own() {
assert_eq!(SymbolFont::code('\u{F0FC}'), Some(0xFC));
assert_eq!(SymbolFont::code('\u{FC}'), Some(0xFC));
assert_eq!(SymbolFont::code(' '), Some(0x20));
for character in ['\u{F01F}', '\u{1F}', '\u{F100}', '\u{100}', '\u{2713}'] {
assert_eq!(SymbolFont::code(character), None, "{character:?}");
}
}
#[test]
fn each_table_holds_one_character_for_each_code() {
for font in SymbolFont::ALL {
let table = super::tables::CODES
.get(font as usize)
.copied()
.unwrap_or("");
assert_eq!(table.chars().count(), 224, "{font:?}");
}
}
#[test]
fn reads_both_forms_of_a_code() {
let tick = Some('\u{2713}');
assert_eq!(SymbolFont::Wingdings.character('\u{F0FC}'), tick);
assert_eq!(SymbolFont::Wingdings.character('\u{FC}'), tick);
assert_eq!(SymbolFont::Symbol.character('\u{F0B7}'), Some('\u{2022}'));
assert_eq!(SymbolFont::Symbol.character('a'), Some('\u{3B1}'));
}
#[test]
fn has_no_symbol_outside_the_codes_or_where_the_font_has_none() {
assert_eq!(SymbolFont::Symbol.character('\u{F01F}'), None);
assert_eq!(SymbolFont::Symbol.character('\u{F100}'), None);
assert_eq!(SymbolFont::Symbol.character('\u{100}'), None);
assert_eq!(SymbolFont::Symbol.character('\u{F0F0}'), None);
assert_eq!(SymbolFont::Symbol.character('\u{F07F}'), None);
}
#[test]
fn takes_the_mathematical_angle_brackets_for_symbol() {
assert_eq!(SymbolFont::Symbol.character('\u{F0E1}'), Some('\u{27E8}'));
assert_eq!(SymbolFont::Symbol.character('\u{F0F1}'), Some('\u{27E9}'));
}
#[test]
fn puts_different_symbols_on_the_same_code() {
let symbols: Vec<_> = SymbolFont::ALL
.into_iter()
.map(|font| font.character('\u{F0B7}'))
.collect();
for (index, symbol) in symbols.iter().enumerate() {
assert!(symbol.is_some());
assert!(!symbols[index + 1..].contains(symbol), "{symbols:?}");
}
}
#[test]
fn finds_a_font_by_its_family_name() {
assert_eq!(SymbolFont::of("wingdings 2"), Some(SymbolFont::Wingdings2));
assert_eq!(SymbolFont::of("WEBDINGS"), Some(SymbolFont::Webdings));
assert_eq!(SymbolFont::of("Symbol MT"), None);
for font in SymbolFont::ALL {
assert_eq!(SymbolFont::of(font.family()), Some(font));
}
}
}