f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The radicals of equations (`m:rad`): once a radical's element is measured, its root sign
//! draws the vertical variant of its glyph that the element's ink needs, in its place, its bar
//! runs over the element from the end of the sign, and its degree rises beside the sign.

use std::ops::Range;

use f_office_units::Length;
use f_office_units::scale::PERCENT;

use super::gap_minimums;
use super::structures::last_italic_correction;
use crate::body::{ROOT_SIGN, RadicalMark};
use crate::grid::Device;
use crate::lines::{Piece, advance};
use crate::props::RunProps;
use crate::text::{RadicalLengths, Shaper};

/// A radical whose end is still to come: the lengths of its parts, the indexes of its degree's
/// pieces among a paragraph's pieces, the index of its root sign's glyph and the index of its
/// bar, once they come.
pub(in crate::layout) struct OpenRadical {
    lengths: Option<RadicalLengths>,
    degree: Option<Range<usize>>,
    sign: Option<usize>,
    bar: Option<usize>,
}

/// Places a mark of a radical in a paragraph's `pieces`, with the properties `props` of its
/// run. A degree starts `kern_before_degree` after the place of the radical, and the root sign
/// `kern_after_degree` after the end of the degree; the bar takes its place at the end of the
/// sign. At the radical's end, the sign draws its glyph for the ink of the element
/// ([`Shaper::root_sign`]), the bar runs over the element's width and the italic correction of
/// its last glyph with its top at the top of the sign's ink, `rule` thick, what comes after the
/// radical starts at the bar's end, and the degree's baseline rises to `degree_raise_percent` of the
/// sign's ink height above the bottom of the sign's ink, to the nearest dot. A radical in a
/// font without the radical's constants of a `MATH` table keeps its glyph and has no bar.
pub(in crate::layout) fn mark(
    (shaper, device): (&mut Shaper, Device),
    (open, pieces): (&mut Vec<OpenRadical>, &mut Vec<Piece>),
    mark: RadicalMark,
    props: &RunProps,
) {
    match mark {
        RadicalMark::Start { degree } => {
            let lengths = shaper.radical_lengths(props);
            let degree = degree.then(|| {
                let before = lengths.map_or(Length::ZERO, |l| l.kern_before_degree);
                pieces.push(Piece::Shift(before));
                pieces.len()..pieces.len()
            });
            open.push(OpenRadical {
                lengths,
                degree,
                sign: None,
                bar: None,
            });
        }
        RadicalMark::Sign => {
            if let Some(radical) = open.last_mut() {
                if let Some(degree) = &mut radical.degree {
                    degree.end = pieces.len();
                    let after = radical
                        .lengths
                        .map_or(Length::ZERO, |l| l.kern_after_degree);
                    pieces.push(Piece::Shift(after));
                }
                radical.sign = Some(pieces.len());
            }
        }
        RadicalMark::Element => {
            let Some(radical) = open.last_mut() else {
                return;
            };
            let sign = radical.sign.and_then(|at| match pieces.get(at) {
                Some(Piece::Glyph(glyph)) if glyph.ch == ROOT_SIGN => Some(glyph.face),
                _ => None,
            });
            if let (Some(face), Some(_)) = (sign, radical.lengths) {
                radical.bar = Some(pieces.len());
                pieces.push(Piece::Vinculum {
                    face,
                    width: Length::ZERO,
                    top: Length::ZERO,
                    thickness: Length::ZERO,
                    beyond: (Length::ZERO, Length::ZERO),
                    clear: Length::ZERO,
                });
            }
        }
        RadicalMark::End => {
            if let Some(radical) = open.pop() {
                end((shaper, device), pieces, &radical);
            }
        }
    }
}

/// Draws the root sign, the bar and the degree of the radical `radical`, whose element's pieces
/// run from its bar to the end of `pieces`, and ends the radical at the bar's end.
fn end((shaper, device): (&mut Shaper, Device), pieces: &mut Vec<Piece>, radical: &OpenRadical) {
    let (Some(lengths), Some(sign), Some(bar)) = (radical.lengths, radical.sign, radical.bar)
    else {
        return;
    };
    let element = pieces.get(bar + 1..).unwrap_or(&[]);
    let ink = gap_minimums::ink(shaper, &device, element);
    let correction = last_italic_correction(shaper, element);
    let width = element.iter().map(advance).sum::<Length>() + correction;
    pieces.push(Piece::Shift(correction));
    let Some(Piece::Glyph(glyph)) = pieces.get_mut(sign) else {
        return;
    };
    let Some(drawn) = shaper.root_sign(glyph, ink, &lengths) else {
        return;
    };
    let face = glyph.face;
    if let Some(piece) = pieces.get_mut(bar) {
        *piece = Piece::Vinculum {
            face,
            width,
            top: drawn.bar_top,
            thickness: lengths.rule,
            beyond: (Length::ZERO, Length::ZERO),
            clear: lengths.extra_ascender,
        };
    }
    let Some(degree) = radical.degree.clone() else {
        return;
    };
    let baseline = pieces
        .get(degree.clone())
        .unwrap_or(&[])
        .iter()
        .find_map(|piece| {
            let Piece::Glyph(glyph) = piece else {
                return None;
            };
            shaper
                .faces()
                .get(glyph.face)
                .map(|face| device.shift_dots(face.rise))
        });
    let Some(baseline) = baseline else {
        return;
    };
    let height = drawn.high - drawn.low;
    let rise = drawn.low + (height * lengths.degree_raise_percent).div_round(PERCENT);
    if let Some(part) = pieces.get_mut(degree) {
        gap_minimums::move_part((shaper, device), part, rise - baseline);
    }
}