use f_office_units::Length;
use crate::layout::testing::{on_base_with, p, pass_with};
use crate::numbering::Numbering;
use crate::params::FRAME_FITTED_ROOM;
use crate::profile::Profile;
use crate::quirks::{FrameFittedWidth, FrameLeft};
fn frame_line_left(frame: &str, rule: FrameLeft) -> Option<Length> {
frame_line_left_with(frame, |p| p.with_variant(rule))
}
fn frame_line_left_with(frame: &str, change: impl FnOnce(&Profile) -> Profile) -> Option<Length> {
let body = format!(
r#"<w:p><w:pPr><w:framePr w:wrap="notBeside" w:vAnchor="text" {frame}/></w:pPr><w:r><w:t>a</w:t></w:r></w:p>{}"#,
p("z")
);
let target = on_base_with(change);
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
pass.lines.first().map(|line| line.column_left)
}
#[test]
fn moves_a_frame_placed_at_an_offset_one_twip_left() {
let twip = Length::from_units(Length::UNITS_PER_TWIP);
for anchor in ["page", "margin", "text"] {
for width in ["", r#"w:w="2000""#] {
let frame = format!(r#"w:hAnchor="{anchor}" w:x="2003" {width}"#);
let at = frame_line_left(&frame, FrameLeft::AtOffset);
let left = frame_line_left(&frame, FrameLeft::TwipLeft);
assert_eq!(
at.zip(left).map(|(at, left)| at - left),
Some(twip),
"{frame}"
);
}
}
}
#[test]
fn keeps_a_frame_aligned_across_or_without_an_offset_at_its_place() {
for frame in [
r#"w:hAnchor="margin" w:xAlign="right""#,
r#"w:hAnchor="margin" w:xAlign="center" w:w="2000""#,
r#"w:hAnchor="page" w:xAlign="right" w:x="2003""#,
r#"w:hAnchor="margin""#,
] {
let at = frame_line_left(frame, FrameLeft::AtOffset);
assert!(at.is_some(), "{frame}");
assert_eq!(at, frame_line_left(frame, FrameLeft::TwipLeft), "{frame}");
}
}
fn fitted_frame_left(frame: &str, rule: FrameFittedWidth) -> Option<Length> {
frame_line_left_with(frame, |p| p.with_variant(rule))
}
#[test]
fn widens_a_frame_without_a_width_of_one_line_by_its_room() {
for (frame, shift) in [
(r#"w:hAnchor="margin" w:xAlign="right""#, FRAME_FITTED_ROOM),
(r#"w:hAnchor="page" w:xAlign="right""#, FRAME_FITTED_ROOM),
(
r#"w:hAnchor="margin" w:xAlign="center""#,
FRAME_FITTED_ROOM.div_toward_zero(2),
),
(r#"w:hAnchor="margin" w:xAlign="left""#, Length::ZERO),
(
r#"w:hAnchor="margin" w:xAlign="right" w:w="2000""#,
Length::ZERO,
),
] {
let line = fitted_frame_left(frame, FrameFittedWidth::Line);
let wider = fitted_frame_left(frame, FrameFittedWidth::HalfTwipWider);
assert_eq!(
line.zip(wider).map(|(line, wider)| line - wider),
Some(shift),
"{frame}"
);
}
}