f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The accents of equations (`m:acc`): once an accent's base is measured, its character draws
//! the glyph that the base's width and ink need, over the base, and adds no width to the line.

use f_office_units::Length;

use super::gap_minimums;
use crate::body::AccentMark;
use crate::grid::Device;
use crate::lines::{Piece, advance};
use crate::props::RunProps;
use crate::text::{AccentBase, AccentLengths, Shaper};

/// An accent whose end is still to come: the lengths of its parts, the index of its base's
/// first piece among a paragraph's pieces, and the index of its character's glyph, once it
/// comes.
pub(in crate::layout) struct OpenAccent {
    lengths: Option<AccentLengths>,
    base: usize,
    character: Option<usize>,
}

/// Places a mark of an accent in a paragraph's `pieces`, with the properties `props` of its
/// run. At the accent's end, the base's letters take their dotless forms
/// ([`Shaper::dotless`]), and its character draws its glyph over the base
/// ([`Shaper::accent`]), with shifts around it that keep the line's width that of the base. The
/// base's attachment is its only glyph's top accent attachment, or the middle of a base of
/// other pieces. An accent in a font without the accent's constants of a `MATH` table keeps its
/// glyph after the base.
pub(in crate::layout) fn mark(
    (shaper, device): (&mut Shaper, Device),
    (open, pieces): (&mut Vec<OpenAccent>, &mut Vec<Piece>),
    mark: AccentMark,
    props: &RunProps,
) {
    match mark {
        AccentMark::Start => open.push(OpenAccent {
            lengths: shaper.accent_lengths(props),
            base: pieces.len(),
            character: None,
        }),
        // A shift keeps the character's glyph apart from the base, which takes no italic
        // correction or kerning from it.
        AccentMark::Character => {
            if let Some(accent) = open.last_mut() {
                accent.character = Some(pieces.len());
                pieces.push(Piece::Shift(Length::ZERO));
            }
        }
        AccentMark::End => {
            if let Some(accent) = open.pop() {
                end((shaper, device), pieces, &accent);
            }
        }
    }
}

/// Draws the accent `accent`, whose character's glyph comes after its base in `pieces`, after
/// the shift that keeps it apart from the base. The accent's glyphs then go before the base,
/// with shifts that bring the line back to the base's start, so that the text after the accent
/// follows the base's last glyph, as it follows a base alone.
fn end((shaper, device): (&mut Shaper, Device), pieces: &mut Vec<Piece>, accent: &OpenAccent) {
    let (Some(lengths), Some(at)) = (accent.lengths, accent.character) else {
        return;
    };
    let Some(&Piece::Glyph(glyph)) = pieces.get(at + 1) else {
        return;
    };
    for piece in pieces.get_mut(accent.base..at).into_iter().flatten() {
        if let Piece::Glyph(glyph) = piece
            && !glyph.part
        {
            shaper.dotless(glyph);
        }
    }
    let base = pieces.get(accent.base..at).unwrap_or(&[]);
    let width = base.iter().map(advance).sum::<Length>();
    let mut glyphs = base
        .iter()
        .enumerate()
        .filter_map(|(k, piece)| match piece {
            Piece::Glyph(glyph) if !glyph.part => Some((k, glyph)),
            _ => None,
        });
    let attachment = match (glyphs.next(), glyphs.next()) {
        (Some((k, only)), None) => {
            let before = base
                .get(..k)
                .unwrap_or(&[])
                .iter()
                .map(advance)
                .sum::<Length>();
            before + shaper.top_accent_attachment(only)
        }
        _ => width.div_toward_zero(2),
    };
    let top = gap_minimums::ink(shaper, &device, base).map(|(_, high)| high);
    let base = AccentBase {
        width,
        attachment,
        top,
    };
    let Some(drawn) = shaper.accent(&glyph, base, &lengths) else {
        return;
    };
    pieces.truncate(at);
    let mut placed = Vec::with_capacity(2 * drawn.len() + 1);
    let mut pen = Length::ZERO;
    for (glyph, place) in drawn {
        placed.push(Piece::Shift(place - pen));
        pen = place + glyph.advance;
        placed.push(Piece::Glyph(glyph));
    }
    placed.push(Piece::Shift(-pen));
    pieces.splice(accent.base..accent.base, placed);
}