f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! Endnotes after the text: a page that they go on to starts with their continuation
//! separator. A page holds 64 lines of 200 twips from the top margin of 1440 twips.

use f_office_paint::DisplayList;

use crate::layout::testing::{Fake, base, find_all, p, rects};
use crate::testing::{WordParts, docx};

/// The endnotes part: a separator, a continuation separator with 300 twips after it, and
/// endnote 1, whose 80 paragraphs "e" go on to the next page.
fn endnotes() -> String {
    let separator = r#"<w:endnote w:type="separator" w:id="-1"><w:p><w:r><w:separator/></w:r></w:p></w:endnote>"#;
    let continuation = r#"<w:endnote w:type="continuationSeparator" w:id="0"><w:p><w:pPr><w:spacing w:after="300"/></w:pPr><w:r><w:continuationSeparator/></w:r></w:p></w:endnote>"#;
    let note = format!(r#"<w:endnote w:id="1">{}</w:endnote>"#, p("e").repeat(80));
    format!("{separator}{continuation}{note}")
}

/// Lays out a paragraph that references endnote 1, with the endnotes of [`endnotes`], in the
/// compatibility mode `mode` or in none.
fn laid_out(mode: Option<u32>) -> DisplayList {
    let body = r#"<w:p><w:r><w:t>a</w:t></w:r><w:r><w:endnoteReference w:id="1"/></w:r></w:p>"#;
    let notes = endnotes();
    let mut builder =
        docx(body)
            .plain_styles()
            .word_part("t0", "endnotes", "endnotes.xml", "endnotes", &notes);
    if let Some(mode) = mode {
        let settings = format!(
            r#"<w:compat><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="{mode}"/></w:compat>"#
        );
        builder = builder.word_part("t1", "settings", "settings.xml", "settings", &settings);
    }
    let bytes = builder.zip().unwrap();
    let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
    crate::layout_with_fonts(
        &package,
        (&Fake, &crate::ArtPictures::new()),
        &crate::Options::base(),
    )
    .unwrap()
    .value
    .list
}

#[test]
fn starts_a_page_of_endnotes_below_their_continuation_separator() {
    let list = laid_out(None);
    let on_second: Vec<i64> = find_all(&list, 'e')
        .into_iter()
        .filter(|glyph| glyph.0 == 1)
        .map(|glyph| glyph.2)
        .collect();
    // The separator's line of 200 twips and the 300 twips after it come first.
    assert_eq!(on_second.first(), Some(&base(1440 + 200 + 300)));
}

#[test]
fn draws_the_rule_above_endnotes_that_go_on_as_wide_as_the_column_in_every_mode() {
    let widths = |mode| -> Vec<i64> { rects(&laid_out(mode), 1).iter().map(|r| r.2).collect() };
    let column = widths(None);
    assert_eq!(column.len(), 1);
    // A rule of 2 inches, as the rule above the rest of a footnote in mode 15, is 600 of
    // these dots; the column is wider.
    assert!(column[0] > 600, "{column:?}");
    assert_eq!(widths(Some(15)), column);
}

/// Lays out a body with `body_xml`, an endnote separator with `(sep_before, sep_after)` twips,
/// and two endnotes (`note1` with `before=600, after=240`, `note2` with `before=360`) in
/// compatibility mode `mode`. Every paragraph uses an exact 240-twip line height (50 dots at
/// 300 dpi) so positions land on whole dots.
fn spaced_endnotes(mode: u32, body_xml: &str, sep_before: u32, sep_after: u32) -> DisplayList {
    let notes = format!(
        concat!(
            r#"<w:endnote w:type="separator" w:id="-1"><w:p><w:pPr>"#,
            r#"<w:spacing w:before="{sep_before}" w:after="{sep_after}" w:line="240" w:lineRule="exact"/>"#,
            r#"</w:pPr><w:r><w:separator/></w:r></w:p></w:endnote>"#,
            r#"<w:endnote w:type="continuationSeparator" w:id="0"><w:p><w:r><w:continuationSeparator/></w:r></w:p></w:endnote>"#,
            r#"<w:endnote w:id="1"><w:p><w:pPr><w:spacing w:before="600" w:after="240" w:line="240" w:lineRule="exact"/></w:pPr><w:r><w:t>x</w:t></w:r></w:p></w:endnote>"#,
            r#"<w:endnote w:id="2"><w:p><w:pPr><w:spacing w:before="360" w:after="0" w:line="240" w:lineRule="exact"/></w:pPr><w:r><w:t>y</w:t></w:r></w:p></w:endnote>"#,
        ),
        sep_before = sep_before,
        sep_after = sep_after,
    );
    let settings = format!(
        r#"<w:compat><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="{mode}"/></w:compat>"#
    );
    let bytes = docx(body_xml)
        .plain_styles()
        .word_part("t0", "endnotes", "endnotes.xml", "endnotes", &notes)
        .word_part("t1", "settings", "settings.xml", "settings", &settings)
        .zip()
        .unwrap();
    let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
    crate::layout_with_fonts(
        &package,
        (&Fake, &crate::ArtPictures::new()),
        &crate::Options::base(),
    )
    .unwrap()
    .value
    .list
}

#[test]
fn does_not_collapse_spacing_across_endnote_separator_boundaries() {
    // One 240-twip body line with 240 twips after, separator (before=360, line=240, after=480),
    // note1 (before=600, line=240, after=240), note2 (before=360, line=240).
    let body = concat!(
        r#"<w:p><w:pPr><w:spacing w:after="240" w:line="240" w:lineRule="exact"/></w:pPr>"#,
        r#"<w:r><w:t>a</w:t></w:r>"#,
        r#"<w:r><w:endnoteReference w:id="1"/></w:r>"#,
        r#"<w:r><w:endnoteReference w:id="2"/></w:r></w:p>"#,
    );
    let m15 = spaced_endnotes(15, body, 360, 480);
    let body_y = find_all(&m15, 'a')[0].2;
    // Mode 15 drops the separator's space before, adds sep.after + note1.before, and collapses
    // note1.after with note2.before.
    let dots = |twips: i64| twips * 300 / 1440;
    assert_eq!(
        find_all(&m15, 'x')[0].2 - body_y,
        dots(240 + 240 + 240 + 480 + 600)
    );
    assert_eq!(
        find_all(&m15, 'y')[0].2 - find_all(&m15, 'x')[0].2,
        dots(240 + 360)
    );

    // Mode 14 keeps body.after + sep.before as well as sep.after + note1.before.
    let m14 = spaced_endnotes(14, body, 360, 480);
    assert_eq!(
        find_all(&m14, 'x')[0].2 - find_all(&m15, 'x')[0].2,
        dots(360)
    );
}

#[test]
fn keeps_separator_space_before_on_next_page_in_mode_14_and_drops_it_in_mode_15() {
    // 63 body lines of 200 twips fill 12,600 of the 12,960-twip body area, leaving 360 twips:
    // the 240-twip separator plus its 480-twip space after overflows onto page 2.
    let first = concat!(
        r#"<w:p><w:r><w:t>a</w:t></w:r>"#,
        r#"<w:r><w:endnoteReference w:id="1"/></w:r>"#,
        r#"<w:r><w:endnoteReference w:id="2"/></w:r></w:p>"#,
    );
    let body = format!("{first}{}", p("b").repeat(62));
    let dots = |twips: i64| twips * 300 / 1440;

    let m15 = spaced_endnotes(15, &body, 360, 480);
    let x15 = find_all(&m15, 'x')[0];
    assert_eq!(x15.0, 1);

    let m14 = spaced_endnotes(14, &body, 360, 480);
    let x14 = find_all(&m14, 'x')[0];
    assert_eq!(x14.0, 1);
    assert_eq!(x14.2 - x15.2, dots(360));
}