f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The layout's target decisions on the variants of their quirks.

use super::{end_space_fit, inline_picture_cut, kept_end_spaces, property_date, top_band_rows};
use crate::dates::DateTime;
use crate::lines::EndSpaceFit;
use crate::profile::Profile;
use crate::quirks::{CellTopBand, InlinePictureClip, PropertyDateZone, RightToLeftEndSpace};
use f_office_units::Length;

#[test]
fn counts_kept_rtl_end_spaces_in_the_fit_in_a_left_to_right_paragraph() {
    let base = Profile::base();
    let kept = base
        .with_variant(RightToLeftEndSpace::RunLevelBesideLeftToRight)
        .quirks();
    let paragraph = base
        .with_variant(RightToLeftEndSpace::ParagraphLevel)
        .quirks();
    let fit = |quirks, bidi| end_space_fit(quirks, bidi) == EndSpaceFit::WithRightToLeftSpaces;
    assert!(fit(&kept, false));
    assert!(!fit(&kept, true));
    assert!(!fit(&paragraph, false));
    // The spaces of runs with `w:rtl` keep their level beside left-to-right text only.
    assert_eq!(kept_end_spaces(&kept, || true, [true, true, false]), 2);
    assert_eq!(kept_end_spaces(&kept, || false, [true]), 0);
    assert_eq!(kept_end_spaces(&paragraph, || true, [true]), 0);
}

#[test]
fn writes_a_date_property_in_utc_or_as_its_local_date() {
    // 23:30 UTC on January 15, which is the next day 8 hours east of UTC.
    let late = DateTime {
        year: 2026,
        month: 1,
        day: 15,
        hour: 23,
        minute: 30,
        second: 0,
    };
    let base = Profile::base();
    let utc = base.with_variant(PropertyDateZone::Utc).quirks();
    let local = base.with_variant(PropertyDateZone::LocalDate).quirks();
    let picture = Some("yyyy-MM-dd HH:mm");
    assert_eq!(
        property_date(&utc, 480, late, picture).as_deref(),
        Some("2026-01-15 23:30")
    );
    assert_eq!(property_date(&utc, 480, late, None), None);
    assert_eq!(
        property_date(&local, 480, late, picture).as_deref(),
        Some("2026-01-16 00:00")
    );
    assert_eq!(
        property_date(&local, 480, late, None).as_deref(),
        Some("16/1/2026")
    );
    assert_eq!(
        property_date(&local, -300, late, None).as_deref(),
        Some("15/1/2026")
    );
}

#[test]
fn paints_a_top_band_from_the_line_also_for_a_merged_cell_that_starts_a_page() {
    // A band from the line, (12, 13), and one below the margin, (13, 37), in layout units.
    let pt = Length::from_units;
    let from_line = || Some((pt(12), pt(13)));
    let below_margin = || Some((pt(13), pt(37)));
    let base = Profile::base();
    let margin = base.with_variant(CellTopBand::BelowMargin).quirks();
    let line = base.with_variant(CellTopBand::FromLineEdge).quirks();
    let band = |quirks, merged| top_band_rows(quirks, merged, below_margin, from_line);
    // A cell that continues no merged region.
    assert_eq!(band(&margin, (false, false)), below_margin());
    assert_eq!(band(&line, (false, false)), from_line());
    // A cell that continues one at the top of a page takes its own line, and its band there.
    assert_eq!(band(&margin, (true, false)), None);
    assert_eq!(band(&line, (true, false)), from_line());
    // A cell that continues one on the same page has neither.
    assert_eq!(band(&margin, (true, true)), None);
    assert_eq!(band(&line, (true, true)), None);
}

#[test]
fn paints_no_top_band_of_no_height_and_one_below_the_margin_without_a_top_line() {
    let pt = Length::from_units;
    let below_margin = || Some((pt(13), pt(37)));
    let line = Profile::base()
        .with_variant(CellTopBand::FromLineEdge)
        .quirks();
    // A line whose band has no height, and a cell without a top line.
    let flat = || Some((pt(12), pt(12)));
    assert_eq!(
        top_band_rows(&line, (false, false), below_margin, flat),
        None
    );
    assert_eq!(
        top_band_rows(&line, (false, false), below_margin, || None),
        below_margin()
    );
}

#[test]
fn cuts_an_inline_picture_only_on_a_target_that_clips_it() {
    let base = Profile::base();
    let band = (Length::from_units(254_000), Length::from_units(762_000));
    let clips = base.with_variant(InlinePictureClip::LineBox).quirks();
    assert_eq!(inline_picture_cut(&clips, || Some(band)), Some(band));
    assert_eq!(
        inline_picture_cut(&clips, || None::<(Length, Length)>),
        None
    );
    let whole = base.with_variant(InlinePictureClip::Unknown).quirks();
    assert_eq!(inline_picture_cut(&whole, || Some(band)), None);
}