f-office-fonts 0.3.0

Font database, metrics and outlines.
Documentation
//! The symbol fonts and their symbol maps: the Unicode character that each code of a symbol
//! font stands for, by which a symbol can be drawn with another font that has the character.

mod tables;

use crate::FamilyName;

/// A symbol font: a font whose codes 0x20 to 0xFF, which a document writes as U+F020 to U+F0FF
/// or as U+0020 to U+00FF, stand for symbols of the font's own and not for the Unicode
/// characters of those codes. Each symbol font puts different symbols on the same codes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SymbolFont {
    /// Symbol: Greek letters and mathematical signs.
    Symbol,
    /// Wingdings.
    Wingdings,
    /// Wingdings 2.
    Wingdings2,
    /// Wingdings 3: mostly arrows.
    Wingdings3,
    /// Webdings.
    Webdings,
}

/// The first and last codes of a symbol font.
const CODES: std::ops::RangeInclusive<u32> = 0x20..=0xFF;
/// The first code of the symbol fonts' private use form, U+F020 to U+F0FF.
const PRIVATE_USE_START: u32 = 0xF000;

impl SymbolFont {
    /// The five symbol fonts.
    pub const ALL: [Self; 5] = [
        Self::Symbol,
        Self::Wingdings,
        Self::Wingdings2,
        Self::Wingdings3,
        Self::Webdings,
    ];

    /// The family of the open font that draws the symbols of the symbol fonts by their
    /// characters, for a caller that has no file of a symbol font: Noto Symbols CF.
    pub const DRAWING_FAMILY: &'static str = "Noto Symbols CF";

    /// The font's family name.
    #[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",
        }
    }

    /// The symbol font that a family name names, if it names one, compared as family names are.
    #[must_use]
    pub fn of(family: &str) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|font| FamilyName::from_static(font.family()).is(family))
    }

    /// The code 0x20 to 0xFF of the symbol fonts that a character writes: U+F020 to U+F0FF,
    /// their private use form, or U+0020 to U+00FF, which a symbol font takes as the same code.
    /// `None` for another character.
    #[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()
    }

    /// The Unicode character that a code of the font stands for: a code 0x20 to 0xFF, written
    /// as U+F020 to U+F0FF or U+0020 to U+00FF ([`SymbolFont::code`]). `None` for another code
    /// and for a code that holds no symbol in the font.
    #[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));
        }
    }
}