f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! East Asian line breaking (kinsoku): a line may break between two characters when one of
//! them is full-width, except before a character that cannot start a line or after one that
//! cannot end a line. The sets of those characters are the defaults of each language in
//! ECMA-376 Part 1, 17.3.1.16. Each language also has the marks that Word lets stand past the
//! end of a line where they do not fit (hanging punctuation, `w:overflowPunct`), which F-Office
//! does not do; the layout only warns where Word would hang one.

use crate::character_class::CharacterClass;
use crate::props::EastAsianScript;

/// The characters of one language that cannot start a line and those that cannot end one,
/// and the marks that Word lets stand past the end of a line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Kinsoku {
    no_start: &'static str,
    no_end: &'static str,
    /// The marks that Word lets stand past the end of a line where they do not fit, with
    /// `w:overflowPunct` on: the lists of each language in [MS-OI29500] 2.1.52, each a part of
    /// `no_start`, so that F-Office takes such a mark to the next line with the character
    /// before it.
    hanging: &'static str,
}

impl Kinsoku {
    /// No restriction: a paragraph that turns kinsoku off (`w:kinsoku w:val="0"`) may break
    /// between any two characters when one of them is full-width.
    /// No mark makes a word go to the next line, so none hangs.
    pub const FREE: Self = Self {
        no_start: "",
        no_end: "",
        hanging: "",
    };

    /// The default sets of a script's language.
    #[must_use]
    pub const fn of(script: EastAsianScript) -> Self {
        match script {
            EastAsianScript::Simplified => Self {
                no_start: "!%),.:;>?]}¢¨°·ˇˉ―‖’”…‰′″›℃∶、。〃〉》」』】〕〗〞︶︺︾﹀﹄﹚﹜﹞!"%'),.:;?]`|}~¢",
                no_end: "$([{£¥·‘“〈《「『【〔〖〝﹙﹛﹝$(.[{£¥",
                hanging: "!%),.:;>?]}¢°·ˇ’”‰′″℃∶、。〃〉》」』】〕〗〞﹚﹜﹞!"%'),.:;?]}¢",
            },
            EastAsianScript::Traditional => Self {
                no_start: "!),.:;?]}¢·–—’”•‥…‧′╴、。〉》」』】〕〞︰︱︳︴︶︸︺︼︾﹀﹂﹄﹏﹐﹑﹒﹔﹕﹖﹗﹚﹜﹞!),.:;?]|}、",
                no_end: "([{£¥‘“‵〈《「『【〔〝︵︷︹︻︽︿﹁﹃﹙﹛﹝({",
                hanging: "!),.:;?]}’”′、。〉》」』】〕〞﹚﹜﹞!),.:;?]}",
            },
            EastAsianScript::Japanese => Self {
                no_start: "!%),.:;?]}¢°’”‰′″℃、。々〉》」』】〕゛゜ゝゞ・ヽヾ!%),.:;?]}。」、・゙゚¢",
                no_end: "$([\\{£¥‘“〈《「『【〔$([{「£¥",
                hanging: ",.’”、。」』】),.]}。、",
            },
            EastAsianScript::Korean => Self {
                no_start: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
                no_end: "$([\\{£¥‘“〈《「『【〔$([{£¥₩",
                hanging: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
            },
        }
    }

    /// The same sets with no mark that hangs: a paragraph that turns `w:overflowPunct` off.
    #[must_use]
    pub const fn without_hanging(self) -> Self {
        Self {
            hanging: "",
            ..self
        }
    }

    /// Whether Word lets `ch` stand past the end of a line where it does not fit.
    #[must_use]
    pub fn hangs(self, ch: char) -> bool {
        self.hanging.contains(ch)
    }
}

/// Whether a line may break between two characters that no space separates. Text without an
/// East Asian language (`kinsoku` is `None`) breaks only where Latin text does.
#[must_use]
pub fn breaks_between(before: char, after: char, kinsoku: Option<Kinsoku>) -> bool {
    let Some(kinsoku) = kinsoku else {
        return false;
    };
    let full_width = |ch| CharacterClass::FullWidthCell.contains(ch);
    (full_width(before) || full_width(after))
        && !kinsoku.no_end.contains(before)
        && !kinsoku.no_start.contains(after)
}

#[cfg(test)]
mod tests {
    use super::{Kinsoku, breaks_between};
    use crate::props::EastAsianScript;

    #[test]
    fn breaks_between_east_asian_characters_but_not_inside_latin_words() {
        let japanese = Some(Kinsoku::of(EastAsianScript::Japanese));
        assert!(breaks_between('日', '本', japanese));
        assert!(breaks_between('本', 'W', japanese));
        assert!(breaks_between('d', '日', japanese));
        assert!(!breaks_between('W', 'o', japanese));
    }

    #[test]
    fn keeps_closing_marks_off_the_start_and_opening_marks_off_the_end() {
        let japanese = Some(Kinsoku::of(EastAsianScript::Japanese));
        assert!(!breaks_between('書', '」', japanese));
        assert!(!breaks_between('本', '、', japanese));
        assert!(!breaks_between('「', '日', japanese));
        // Japanese keeps small kana free to start a line unless strict rules apply.
        assert!(breaks_between('ン', 'ッ', japanese));
    }

    #[test]
    fn follows_the_language_and_the_kinsoku_switch() {
        // With kinsoku off, any break between full-width characters is allowed, and text
        // without an East Asian language breaks only where Latin text does.
        assert!(breaks_between('書', '」', Some(Kinsoku::FREE)));
        assert!(!breaks_between('日', '本', None));
        // The sets differ by language: Simplified Chinese keeps "〃" off the start, Korean
        // does not.
        let simplified = Some(Kinsoku::of(EastAsianScript::Simplified));
        let korean = Some(Kinsoku::of(EastAsianScript::Korean));
        assert!(!breaks_between('字', '〃', simplified));
        assert!(breaks_between('字', '〃', korean));
    }

    #[test]
    fn hangs_only_marks_that_cannot_start_a_line() {
        // [MS-OI29500] 2.1.52: the marks that hang are marks that cannot start a line, so a
        // word that ends with one goes to the next line whole where Word hangs the mark.
        for script in [
            EastAsianScript::Simplified,
            EastAsianScript::Traditional,
            EastAsianScript::Japanese,
            EastAsianScript::Korean,
        ] {
            let sets = Kinsoku::of(script);
            assert!(
                sets.hanging.chars().all(|ch| sets.no_start.contains(ch)),
                "{script:?}"
            );
            assert!(sets.hangs(',') && !sets.hangs('字'), "{script:?}");
            assert!(!sets.without_hanging().hangs(','), "{script:?}");
        }
        // The lists differ by language: Korean hangs "!", Japanese does not.
        assert!(Kinsoku::of(EastAsianScript::Korean).hangs('!'));
        assert!(!Kinsoku::of(EastAsianScript::Japanese).hangs('!'));
        assert!(!Kinsoku::FREE.hangs(','));
    }
}