f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The lines before a frame at the text whose distance above it reaches over them.

use f_office_units::Length;

use crate::layout::testing::{on_base, p, pass_with};
use crate::numbering::Numbering;

/// The tops of the lines of a paragraph "a" with `lead_after` twips after it, of a frame at the
/// text after it whose paragraph has `after` twips after it and whose distance above and below
/// it is `v_space` twips, and of a paragraph "b" after the frame, in the compatibility mode
/// `mode`.
fn lines(lead_after: u32, after: u32, v_space: u32, mode: u32) -> Vec<Length> {
    let body = format!(
        r#"<w:p><w:pPr><w:spacing w:after="{lead_after}"/></w:pPr><w:r><w:t>a</w:t></w:r></w:p><w:p><w:pPr><w:framePr w:w="4320" w:vSpace="{v_space}" w:wrap="notBeside" w:vAnchor="text" w:hAnchor="page" w:xAlign="center" w:y="1"/><w:spacing w:after="{after}"/></w:pPr><w:r><w:t>q</w:t></w:r></w:p>{}"#,
        p("b")
    );
    let pass = pass_with(
        &body,
        ("", Some(mode)),
        None,
        Numbering::default(),
        on_base(),
    );
    pass.lines.iter().map(|line| line.top).collect()
}

/// The tops of the first line of a paragraph and of the first line of the frame after it, with
/// a paragraph after the frame, when the frame's distance above and below it is `v_space`
/// twips.
fn tops(v_space: u32) -> Option<(Length, Length)> {
    let body = format!(
        r#"{}<w:p><w:pPr><w:framePr w:w="4320" w:vSpace="{v_space}" w:wrap="notBeside" w:vAnchor="text" w:hAnchor="page" w:xAlign="center" w:y="1"/></w:pPr><w:r><w:t>q</w:t></w:r></w:p>{}"#,
        p("a"),
        p("b")
    );
    let pass = pass_with(&body, ("", None), None, Numbering::default(), on_base());
    Some((pass.lines.first()?.top, pass.lines.get(1)?.top))
}

#[test]
fn moves_the_line_before_a_frame_below_it_when_the_frame_s_distance_reaches_over_it() {
    let (before, frame) = tops(187).expect("two lines");
    assert!(before > frame, "{before:?} {frame:?}");
}

#[test]
fn keeps_the_line_before_a_frame_without_a_distance_above_it() {
    let (before, frame) = tops(0).expect("two lines");
    assert!(before < frame, "{before:?} {frame:?}");
}

#[test]
fn ends_a_frame_below_its_paragraph_s_space_after() {
    let without = lines(240, 0, 0, 15);
    let with = lines(240, 320, 0, 15);
    let after = Length::from_twips(320).expect("a length");
    assert_eq!(with.get(2).copied(), without.get(2).map(|&top| top + after));
}

#[test]
fn moves_the_last_line_before_a_frame_with_its_space_after_before_mode_15() {
    let older = lines(240, 0, 187, 14);
    let newer = lines(240, 0, 187, 15);
    assert!(older[0] > older[1], "{older:?}");
    assert!(newer[0] < newer[1], "{newer:?}");
}