f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The bar of an equation's bar structure (`m:bar`): the lengths of an overbar or an underbar
//! from the `MATH` table of its math font.

use f_office_units::Length;

use super::Shaper;
use crate::body::BarSide;
use crate::props::RunProps;

/// The lengths of a bar over or under its base at the size of the run of its marks, from the
/// `MATH` table of its math font.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BarLengths {
    /// The gap between the base's ink and the bar (`OverbarVerticalGap` or
    /// `UnderbarVerticalGap`).
    pub gap: Length,
    /// The thickness of the bar (`OverbarRuleThickness` or `UnderbarRuleThickness`).
    pub rule: Length,
    /// The room of the line beyond the bar: above an overbar `OverbarExtraAscender` and the
    /// `MathLeading` at the size of the equation, below an underbar `UnderbarExtraDescender`.
    pub room: Length,
    /// The white space above the bar that the structure's box holds: an overbar's
    /// `OverbarExtraAscender`; none for an underbar.
    pub clear: Length,
}

impl Shaper<'_> {
    /// The lengths of a bar on the side `side` of its base whose marks have the properties
    /// `props`, at their size; none for a font without the bars' constants of a `MATH` table.
    pub fn bar_lengths(&mut self, props: &RunProps, side: BarSide) -> Option<BarLengths> {
        let face = self.face(props, true)?;
        let f = self.faces().get(face).copied()?;
        let bars = self.fonts.math_bars(f.font)?;
        Some(match side {
            BarSide::Top => {
                let em = i64::from(f.metrics.units_per_em.max(1));
                let leading = self.math_leading(props, f.font, em)?;
                BarLengths {
                    gap: f.scale(i64::from(bars.overbar_vertical_gap)),
                    rule: f.scale(i64::from(bars.overbar_rule_thickness)),
                    room: f.scale(i64::from(bars.overbar_extra_ascender)) + leading,
                    clear: f.scale(i64::from(bars.overbar_extra_ascender)),
                }
            }
            BarSide::Bottom => BarLengths {
                gap: f.scale(i64::from(bars.underbar_vertical_gap)),
                rule: f.scale(i64::from(bars.underbar_rule_thickness)),
                room: f.scale(i64::from(bars.underbar_extra_descender)),
                clear: Length::ZERO,
            },
        })
    }
}