f-office-docx 0.2.3

DOCX model and Word's layout rules.
Documentation
//! The table of symbol text: the Unicode character that a target with
//! [`SymbolText::TableOrSymbol`](crate::quirks::SymbolText::TableOrSymbol) writes in its PDFs
//! for a character of a symbol font, U+F020 to U+F0FF. Every other character of the symbol
//! fonts reads as itself.
//!
//! Each row is the text that the target writes for that code, both in a run of the font and
//! as a symbol (`w:sym`). Wingdings 2, Wingdings 3 and Webdings have no such code. For the
//! space, U+F020, the target writes no character, which F-Office's line text cannot tell from
//! a space.

use super::script::SymbolFont;

/// The characters of the Symbol font that have a text of their own.
const SYMBOL: [(char, char); 12] = [
    ('\u{F020}', ' '),
    ('\u{F028}', '('),
    ('\u{F029}', ')'),
    ('\u{F02B}', '+'),
    ('\u{F02D}', '\u{2212}'),
    ('\u{F03D}', '='),
    ('\u{F05E}', '\u{22A5}'),
    ('\u{F0AE}', '\u{2192}'),
    ('\u{F0B7}', '\u{2022}'),
    ('\u{F0BE}', '\u{23AF}'),
    ('\u{F0D3}', '\u{A9}'),
    ('\u{F0E3}', '\u{A9}'),
];

/// The characters of the Wingdings font that have a text of their own.
const WINGDINGS: [(char, char); 14] = [
    ('\u{F020}', ' '),
    ('\u{F04A}', '\u{263A}'),
    ('\u{F06C}', '\u{26AB}'),
    ('\u{F06E}', '\u{25FC}'),
    ('\u{F071}', '\u{2751}'),
    ('\u{F073}', '\u{2B27}'),
    ('\u{F074}', '\u{29EB}'),
    ('\u{F075}', '\u{25C6}'),
    ('\u{F076}', '\u{2756}'),
    ('\u{F0A7}', '\u{25AA}'),
    ('\u{F0D8}', '\u{27A2}'),
    ('\u{F0E0}', '\u{2192}'),
    ('\u{F0E8}', '\u{2794}'),
    ('\u{F0FC}', '\u{2713}'),
];

/// The rows of the table for the font `font`: none for Wingdings 2, Wingdings 3 and Webdings.
fn rows(font: SymbolFont) -> &'static [(char, char)] {
    match font {
        SymbolFont::Symbol => &SYMBOL,
        SymbolFont::Wingdings => &WINGDINGS,
        _ => &[],
    }
}

/// Whether the table has codes for the font `font`: Symbol and Wingdings.
pub(super) fn has_codes(font: SymbolFont) -> bool {
    !rows(font).is_empty()
}

/// The text that the table gives for the character `ch` of the font `font`, if the table has
/// one.
pub(super) fn text(font: SymbolFont, ch: char) -> Option<char> {
    rows(font)
        .iter()
        .find(|&&(row, _)| row == ch)
        .map(|&(_, text)| text)
}

#[cfg(test)]
mod tests {
    use super::{SYMBOL, SymbolFont, WINGDINGS};

    /// The rows where the text that the target writes differs from the symbol map's character,
    /// with the map's character: in each, the target writes an older character than the one
    /// that Unicode 7.0 added for the symbol.
    const WORD_TEXT_DIFFERS: [(SymbolFont, char, char); 4] = [
        (SymbolFont::Wingdings, '\u{F073}', '\u{1F79F}'),
        (SymbolFont::Wingdings, '\u{F0D8}', '\u{2B9A}'),
        (SymbolFont::Wingdings, '\u{F0E0}', '\u{1F86A}'),
        (SymbolFont::Wingdings, '\u{F0E8}', '\u{1F87A}'),
    ];

    #[test]
    fn each_row_agrees_with_the_symbol_map_or_is_a_named_difference() {
        let rows = SYMBOL
            .iter()
            .map(|&row| (SymbolFont::Symbol, row))
            .chain(WINGDINGS.iter().map(|&row| (SymbolFont::Wingdings, row)));
        let mut differ = Vec::new();
        for (font, (code, text)) in rows {
            let mapped = font.character(code);
            if mapped != Some(text) {
                differ.push((font, code, mapped.unwrap_or(char::REPLACEMENT_CHARACTER)));
            }
        }
        let named: Vec<_> = WORD_TEXT_DIFFERS
            .iter()
            .map(|&(font, code, _)| (font, code))
            .collect();
        let found: Vec<_> = differ.iter().map(|&(font, code, _)| (font, code)).collect();
        assert_eq!(found, named, "{differ:?}");
        for (font, code, text) in WORD_TEXT_DIFFERS {
            assert_eq!(font.character(code), Some(text), "{code:?}");
        }
    }
}