f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! The structures of equations whose parts the layout places once they are measured: the
//! radicals, accents, bar structures, matrices and scripts that are still open in a paragraph.

use super::accents::{self, OpenAccent};
use super::bars::{self, OpenBar};
use super::matrices::{self, OpenMatrix};
use super::radicals::{self, OpenRadical};
use super::scripts::{self, OpenScripts};
use super::slashes::{self, OpenSlash};
use crate::body::{BarSide, Content};
use crate::grid::Device;
use crate::lines::Piece;
use crate::props::RunProps;
use crate::text::Shaper;
use f_office_units::Length;

/// The structures of each kind whose end is still to come in a paragraph, the innermost last,
/// and the index of the shift that ends the last bar structure, with the side of its bar.
#[derive(Default)]
pub(in crate::layout) struct OpenStructures {
    radicals: Vec<OpenRadical>,
    accents: Vec<OpenAccent>,
    bars: Vec<OpenBar>,
    slashes: Vec<OpenSlash>,
    matrices: Vec<OpenMatrix>,
    scripts: Vec<OpenScripts>,
    bar_end: Option<(usize, BarSide)>,
}

impl OpenStructures {
    /// Places `content`, a mark of a radical, an accent, a bar structure, a matrix or scripts,
    /// in a paragraph's `pieces`, with the properties `props` of its run; nothing for other
    /// content.
    pub(in crate::layout) fn mark(
        &mut self,
        (shaper, device): (&mut Shaper, Device),
        pieces: &mut Vec<Piece>,
        content: &Content,
        props: &RunProps,
    ) {
        let at = (shaper, device);
        match content {
            Content::Radical(mark) => {
                radicals::mark(at, (&mut self.radicals, pieces), *mark, props);
            }
            Content::Accent(mark) => accents::mark(at, (&mut self.accents, pieces), *mark, props),
            Content::MathBar(mark) => {
                if let Some(end) = bars::mark(at, (&mut self.bars, pieces), *mark, props) {
                    self.bar_end = Some(end);
                }
            }
            Content::MathSlash(mark) => {
                slashes::mark(at, (&mut self.slashes, pieces), *mark, props);
            }
            Content::Matrix(mark) => matrices::mark(at, (&mut self.matrices, pieces), *mark, props),
            Content::Scripts(mark) => {
                let ended = self.bar_end.filter(|&(end, _)| end + 1 == pieces.len());
                scripts::mark(at, (&mut self.scripts, pieces), (*mark, ended), props);
            }
            _ => {}
        }
    }
}

/// The index in `span` of the glyph that ends it for the italic correction after it: its last
/// piece but the later parts of a ligature, which draw nothing, when that piece is a glyph. A
/// span that ends in a structure, such as a fraction, a bar or a root, has none: the structure
/// ends past the italic correction of its own last glyph.
pub(super) fn ending_glyph(span: &[Piece]) -> Option<usize> {
    let at = span
        .iter()
        .rposition(|piece| !matches!(piece, Piece::Glyph(glyph) if glyph.part))?;
    matches!(span.get(at), Some(Piece::Glyph(_))).then_some(at)
}

/// The italic correction of the glyph that ends `span` ([`ending_glyph`]), or zero.
pub(super) fn last_italic_correction(shaper: &Shaper, span: &[Piece]) -> Length {
    match ending_glyph(span).and_then(|at| span.get(at)) {
        Some(Piece::Glyph(glyph)) => shaper.glyph_italic_correction(glyph).unwrap_or_default(),
        _ => Length::ZERO,
    }
}

#[cfg(test)]
mod tests {
    use f_office_units::Length;

    use super::ending_glyph;
    use crate::lines::Piece;
    use crate::text::{FaceIndex, Glyph};

    #[test]
    fn a_span_ends_in_its_last_glyph_and_in_none_after_a_structure() {
        let part = || {
            let mut part = Glyph::plain(FaceIndex::nth(0), 'i');
            part.part = true;
            Piece::Glyph(part)
        };
        let letter = || Piece::Glyph(Glyph::plain(FaceIndex::nth(0), 'W'));
        let shift = Piece::Shift(Length::ZERO);
        assert_eq!(ending_glyph(&[letter(), letter()]), Some(1));
        assert_eq!(ending_glyph(&[letter(), part()]), Some(0));
        assert_eq!(ending_glyph(&[letter(), shift]), None);
        assert_eq!(ending_glyph(&[part()]), None);
        assert_eq!(ending_glyph(&[]), None);
    }
}