f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The bar structures of equations (`m:bar`): once a bar structure's base is measured, its bar
//! runs over or under the base's width, a gap away from the base's ink.

use f_office_units::Length;

use super::gap_minimums;
use super::structures::last_italic_correction;
use crate::body::{BarMark, BarSide};
use crate::grid::Device;
use crate::lines::{Piece, advance};
use crate::props::RunProps;
use crate::text::{BarLengths, Shaper};

/// A bar structure whose end is still to come: the side of its bar, its lengths, and the index
/// of its bar among a paragraph's pieces, before the base's pieces.
pub(in crate::layout) struct OpenBar {
    side: BarSide,
    lengths: Option<BarLengths>,
    bar: Option<usize>,
}

/// Places a mark of a bar structure in a paragraph's `pieces`, with the properties `props` of
/// its run. The bar takes its place before the base, and at the structure's end it runs from
/// there over the base's width and the italic correction of its last glyph, where what comes
/// after the structure starts: an overbar with its
/// bottom `gap` above the top of the base's ink, an underbar with its top `gap` below the
/// bottom of the base's ink, on a dot, `rule` thick, with the room `room` beyond it. A bar
/// structure in a font without the bars' constants of a `MATH` table draws no bar. At the
/// structure's end, gives the index of the shift that ends it and the side of its bar.
pub(in crate::layout) fn mark(
    (shaper, device): (&mut Shaper, Device),
    (open, pieces): (&mut Vec<OpenBar>, &mut Vec<Piece>),
    mark: BarMark,
    props: &RunProps,
) -> Option<(usize, BarSide)> {
    match mark {
        BarMark::Start(side) => {
            let lengths = shaper.bar_lengths(props, side);
            let face = shaper.face(props, true);
            let bar = match (face, lengths) {
                (Some(face), Some(_)) => {
                    pieces.push(Piece::Vinculum {
                        face,
                        width: Length::ZERO,
                        top: Length::ZERO,
                        thickness: Length::ZERO,
                        beyond: (Length::ZERO, Length::ZERO),
                        clear: Length::ZERO,
                    });
                    Some(pieces.len() - 1)
                }
                _ => None,
            };
            open.push(OpenBar { side, lengths, bar });
            None
        }
        BarMark::End => {
            let bar = open.pop()?;
            end((&*shaper, device), pieces, &bar).map(|at| (at, bar.side))
        }
    }
}

/// Draws the bar of the bar structure `bar`, whose base's pieces run from its bar to the end of
/// `pieces`, and ends the structure at the bar's end, by a shift whose index it gives.
fn end(
    (shaper, device): (&Shaper, Device),
    pieces: &mut Vec<Piece>,
    bar: &OpenBar,
) -> Option<usize> {
    let (Some(lengths), Some(at)) = (bar.lengths, bar.bar) else {
        return None;
    };
    let base = pieces.get(at + 1..).unwrap_or(&[]);
    let correction = last_italic_correction(shaper, base);
    let width = base.iter().map(advance).sum::<Length>() + correction;
    let (low, high) = gap_minimums::ink(shaper, &device, base).unwrap_or_default();
    let Some(Piece::Vinculum { face, .. }) = pieces.get(at).copied() else {
        return None;
    };
    let rise = shaper
        .faces()
        .get(face)
        .map_or(Length::ZERO, |f| device.shift_dots(f.rise));
    let (top, beyond) = match bar.side {
        BarSide::Top => (
            high - rise + lengths.gap + lengths.rule,
            (lengths.room, Length::ZERO),
        ),
        BarSide::Bottom => (low - rise - lengths.gap, (Length::ZERO, lengths.room)),
    };
    if let Some(piece) = pieces.get_mut(at) {
        *piece = Piece::Vinculum {
            face,
            width,
            top: device.snap(top),
            thickness: lengths.rule,
            beyond,
            clear: lengths.clear,
        };
    }
    pieces.push(Piece::Shift(correction));
    Some(pieces.len() - 1)
}