f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! The math operator classes that Word spaces, from Unicode's math classes (Unicode Technical
//! Report 25): binary operators, relations and the signs that are binary or unary.

/// The class of a math operator.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum OperatorClass {
    /// A binary operator, such as `×`, `⊗` or `∙`.
    Binary,
    /// A relation, such as `=`, `∈`, `⊂` or an arrow.
    Relation,
    /// A sign that is binary between two operands and unary elsewhere: `+`, `−`, `±` and `∓`.
    Sign,
}

/// The class of the character `ch`, if it is an operator. The ASCII hyphen and asterisk, which
/// Word shows as the minus sign and the asterisk operator, count as those. Word gives the ASCII
/// slash and backslash, the function application and the invisible times no space, and the
/// midline ellipsis (`⋯`) a thin space, so they count as no operator.
pub(super) fn operator_class(ch: char) -> Option<OperatorClass> {
    match ch {
        '-' => Some(OperatorClass::Sign),
        '*' => Some(OperatorClass::Binary),
        '/' | '\\' | '\u{2061}' | '\u{2062}' | '\u{22EF}' => None,
        _ => CLASSES
            .binary_search_by(|&(first, last, _)| {
                if last < ch {
                    std::cmp::Ordering::Less
                } else if first > ch {
                    std::cmp::Ordering::Greater
                } else {
                    std::cmp::Ordering::Equal
                }
            })
            .ok()
            .and_then(|index| CLASSES.get(index))
            .map(|&(_, _, class)| class),
    }
}

/// The ranges of code points of each class, in order, adjacent ranges of one class joined.
const CLASSES: [(char, char, OperatorClass); 95] = [
    ('\u{002B}', '\u{002B}', OperatorClass::Sign),
    ('\u{002F}', '\u{002F}', OperatorClass::Binary),
    ('\u{003C}', '\u{003E}', OperatorClass::Relation),
    ('\u{005C}', '\u{005C}', OperatorClass::Binary),
    ('\u{00B1}', '\u{00B1}', OperatorClass::Sign),
    ('\u{00B7}', '\u{00B7}', OperatorClass::Binary),
    ('\u{00D7}', '\u{00D7}', OperatorClass::Binary),
    ('\u{00F7}', '\u{00F7}', OperatorClass::Binary),
    ('\u{2020}', '\u{2021}', OperatorClass::Relation),
    ('\u{2022}', '\u{2022}', OperatorClass::Binary),
    ('\u{2040}', '\u{2040}', OperatorClass::Binary),
    ('\u{2044}', '\u{2044}', OperatorClass::Binary),
    ('\u{204E}', '\u{204E}', OperatorClass::Binary),
    ('\u{204F}', '\u{2050}', OperatorClass::Relation),
    ('\u{2061}', '\u{2062}', OperatorClass::Binary),
    ('\u{2190}', '\u{21B3}', OperatorClass::Relation),
    ('\u{21B6}', '\u{21B7}', OperatorClass::Relation),
    ('\u{21BA}', '\u{21FF}', OperatorClass::Relation),
    ('\u{2208}', '\u{220D}', OperatorClass::Relation),
    ('\u{2212}', '\u{2213}', OperatorClass::Sign),
    ('\u{2214}', '\u{2219}', OperatorClass::Binary),
    ('\u{221D}', '\u{221D}', OperatorClass::Relation),
    ('\u{2223}', '\u{2226}', OperatorClass::Relation),
    ('\u{2227}', '\u{222A}', OperatorClass::Binary),
    ('\u{2234}', '\u{2237}', OperatorClass::Relation),
    ('\u{2238}', '\u{2238}', OperatorClass::Binary),
    ('\u{2239}', '\u{223D}', OperatorClass::Relation),
    ('\u{223E}', '\u{223E}', OperatorClass::Binary),
    ('\u{2240}', '\u{2240}', OperatorClass::Binary),
    ('\u{2241}', '\u{228B}', OperatorClass::Relation),
    ('\u{228C}', '\u{228E}', OperatorClass::Binary),
    ('\u{228F}', '\u{2292}', OperatorClass::Relation),
    ('\u{2293}', '\u{22A1}', OperatorClass::Binary),
    ('\u{22A2}', '\u{22A3}', OperatorClass::Relation),
    ('\u{22A5}', '\u{22B8}', OperatorClass::Relation),
    ('\u{22B9}', '\u{22BD}', OperatorClass::Binary),
    ('\u{22C4}', '\u{22C7}', OperatorClass::Binary),
    ('\u{22C8}', '\u{22C8}', OperatorClass::Relation),
    ('\u{22C9}', '\u{22CC}', OperatorClass::Binary),
    ('\u{22CD}', '\u{22CD}', OperatorClass::Relation),
    ('\u{22CE}', '\u{22CF}', OperatorClass::Binary),
    ('\u{22D0}', '\u{22D1}', OperatorClass::Relation),
    ('\u{22D2}', '\u{22D3}', OperatorClass::Binary),
    ('\u{22D4}', '\u{22FF}', OperatorClass::Relation),
    ('\u{2305}', '\u{2306}', OperatorClass::Binary),
    ('\u{2322}', '\u{2323}', OperatorClass::Relation),
    ('\u{233D}', '\u{233D}', OperatorClass::Binary),
    ('\u{233F}', '\u{233F}', OperatorClass::Relation),
    ('\u{237C}', '\u{237C}', OperatorClass::Relation),
    ('\u{23B0}', '\u{23B1}', OperatorClass::Relation),
    ('\u{25B2}', '\u{25B9}', OperatorClass::Binary),
    ('\u{25BC}', '\u{25C5}', OperatorClass::Binary),
    ('\u{25CA}', '\u{25CB}', OperatorClass::Binary),
    ('\u{25E6}', '\u{25E6}', OperatorClass::Binary),
    ('\u{25EB}', '\u{25EC}', OperatorClass::Binary),
    ('\u{25F8}', '\u{25FF}', OperatorClass::Binary),
    ('\u{2605}', '\u{2606}', OperatorClass::Binary),
    ('\u{27C2}', '\u{27CB}', OperatorClass::Relation),
    ('\u{27CD}', '\u{27CD}', OperatorClass::Relation),
    ('\u{27CE}', '\u{27CF}', OperatorClass::Binary),
    ('\u{27D1}', '\u{27D1}', OperatorClass::Binary),
    ('\u{27D2}', '\u{27D4}', OperatorClass::Relation),
    ('\u{27DA}', '\u{27DF}', OperatorClass::Relation),
    ('\u{27E0}', '\u{27E5}', OperatorClass::Binary),
    ('\u{27F0}', '\u{27FF}', OperatorClass::Relation),
    ('\u{2900}', '\u{297F}', OperatorClass::Relation),
    ('\u{29B6}', '\u{29B9}', OperatorClass::Binary),
    ('\u{29C0}', '\u{29C1}', OperatorClass::Binary),
    ('\u{29C4}', '\u{29C8}', OperatorClass::Binary),
    ('\u{29CE}', '\u{29D5}', OperatorClass::Relation),
    ('\u{29D6}', '\u{29D7}', OperatorClass::Binary),
    ('\u{29DF}', '\u{29DF}', OperatorClass::Relation),
    ('\u{29E1}', '\u{29E1}', OperatorClass::Relation),
    ('\u{29E2}', '\u{29E2}', OperatorClass::Binary),
    ('\u{29E3}', '\u{29E6}', OperatorClass::Relation),
    ('\u{29EB}', '\u{29EB}', OperatorClass::Binary),
    ('\u{29F4}', '\u{29F4}', OperatorClass::Relation),
    ('\u{29F5}', '\u{29F7}', OperatorClass::Binary),
    ('\u{29FA}', '\u{29FB}', OperatorClass::Binary),
    ('\u{29FE}', '\u{29FF}', OperatorClass::Binary),
    ('\u{2A22}', '\u{2A58}', OperatorClass::Binary),
    ('\u{2A59}', '\u{2A59}', OperatorClass::Relation),
    ('\u{2A5A}', '\u{2A65}', OperatorClass::Binary),
    ('\u{2A66}', '\u{2A70}', OperatorClass::Relation),
    ('\u{2A71}', '\u{2A72}', OperatorClass::Binary),
    ('\u{2A73}', '\u{2AE0}', OperatorClass::Relation),
    ('\u{2AE2}', '\u{2AF0}', OperatorClass::Relation),
    ('\u{2AF2}', '\u{2AF3}', OperatorClass::Relation),
    ('\u{2AF4}', '\u{2AF6}', OperatorClass::Binary),
    ('\u{2AF7}', '\u{2AFA}', OperatorClass::Relation),
    ('\u{2AFB}', '\u{2AFB}', OperatorClass::Binary),
    ('\u{2AFD}', '\u{2AFE}', OperatorClass::Binary),
    ('\u{2B00}', '\u{2B11}', OperatorClass::Relation),
    ('\u{2B30}', '\u{2B4C}', OperatorClass::Relation),
    ('\u{2B95}', '\u{2B95}', OperatorClass::Relation),
];

/// Whether the character `ch` is ordinary: no operator, no space and none of the math classes
/// unary, open, close, fence or punctuation, such as a digit, a letter or `∞`. The slash, the
/// backslash, the invisible operators and the midline ellipsis, to which [`operator_class`] gives
/// no class for their spacing, count as operators here, as in Unicode's math classes, and the
/// exclamation marks `!` and `‼`, which those classes call normal, count as punctuation, as in
/// Word; other marks that end a sentence, such as `‽`, stay ordinary.
pub(crate) fn ordinary(ch: char) -> bool {
    !(ch.is_whitespace()
        || ch == crate::text::OPERATOR_BREAK_SPACE
        || operator_class(ch).is_some()
        || matches!(
            ch,
            '/' | '\\' | '\u{2061}' | '\u{2062}' | '\u{2064}' | '\u{22EF}'
        )
        || matches!(ch, '!' | '\u{203C}')
        || matches!(
            ch,
            '\u{00AC}'
                | '\u{2200}'
                | '\u{2201}'
                | '\u{2203}'
                | '\u{2204}'
                | '\u{2206}'
                | '\u{2207}'
        )
        || delimiter(ch))
}

/// Whether the character `ch` is of the math classes open, close, fence or punctuation, such as
/// `(`, `]`, `|` and `,`.
fn delimiter(ch: char) -> bool {
    DELIMITERS
        .binary_search_by(|&(first, last)| {
            if last < ch {
                std::cmp::Ordering::Less
            } else if first > ch {
                std::cmp::Ordering::Greater
            } else {
                std::cmp::Ordering::Equal
            }
        })
        .is_ok()
}

/// The ranges of code points of the classes open, close, fence and punctuation, in order,
/// adjacent ranges joined.
const DELIMITERS: [(char, char); 22] = [
    ('\u{0028}', '\u{0029}'),
    ('\u{002C}', '\u{002C}'),
    ('\u{002E}', '\u{002E}'),
    ('\u{003A}', '\u{003B}'),
    ('\u{003F}', '\u{003F}'),
    ('\u{005B}', '\u{005B}'),
    ('\u{005D}', '\u{005D}'),
    ('\u{007B}', '\u{007D}'),
    ('\u{00A1}', '\u{00A1}'),
    ('\u{00BF}', '\u{00BF}'),
    ('\u{2010}', '\u{2010}'),
    ('\u{2012}', '\u{2014}'),
    ('\u{2016}', '\u{2016}'),
    ('\u{2063}', '\u{2063}'),
    ('\u{2308}', '\u{230B}'),
    ('\u{231C}', '\u{231F}'),
    ('\u{2772}', '\u{2773}'),
    ('\u{27E6}', '\u{27EF}'),
    ('\u{2980}', '\u{2980}'),
    ('\u{2982}', '\u{299A}'),
    ('\u{29D8}', '\u{29DB}'),
    ('\u{29FC}', '\u{29FD}'),
];