use f_office_units::Length;
use crate::layout::testing::{on_base_with, p, pass_with, table};
use crate::numbering::Numbering;
use crate::quirks::FloatingTableLeft;
fn first_cell_left(place: &str, rule: FloatingTableLeft) -> Option<Length> {
let properties = format!(
r#"<w:tblpPr w:vertAnchor="page" w:tblpY="2000" {place}/><w:tblW w:w="3000" w:type="dxa"/>"#
);
let body = table(&properties, &[3000], &[("", &[&p("a")])]) + &p("z");
let target = on_base_with(|p| p.with_variant(rule));
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
pass.lines
.iter()
.find(|line| line.cell.is_some())
.map(|line| line.column_left)
}
#[test]
fn moves_a_table_placed_at_an_offset_one_twip_left() {
let twip = Length::from_units(Length::UNITS_PER_TWIP);
for anchor in ["page", "margin", "text"] {
let place = format!(r#"w:horzAnchor="{anchor}" w:tblpX="2003""#);
let at = first_cell_left(&place, FloatingTableLeft::AtOffset);
let left = first_cell_left(&place, FloatingTableLeft::TwipLeft);
assert_eq!(
at.zip(left).map(|(at, left)| at - left),
Some(twip),
"{anchor}"
);
}
}
#[test]
fn keeps_a_table_aligned_across_or_without_an_offset_at_its_place() {
for place in [
r#"w:horzAnchor="page" w:tblpXSpec="center""#,
r#"w:horzAnchor="margin""#,
] {
let at = first_cell_left(place, FloatingTableLeft::AtOffset);
assert!(at.is_some(), "{place}");
assert_eq!(
at,
first_cell_left(place, FloatingTableLeft::TwipLeft),
"{place}"
);
}
}
fn left_line(place: &str, width: i64, halves: crate::quirks::TableEdgeHalves) -> Option<i64> {
use crate::layout::testing::{rects, target_at};
use crate::quirks::TableLines;
let properties = format!(
r#"<w:tblpPr w:vertAnchor="page" w:tblpY="2000" {place}/><w:tblW w:w="{width}" w:type="dxa"/><w:tblBorders><w:left w:val="single" w:sz="6"/></w:tblBorders>"#
);
let body = table(&properties, &[width], &[("", &[&p("a")])]) + &p("z");
let target = target_at(600, |p| {
p.with_variant(halves)
.with_variant(FloatingTableLeft::TwipLeft)
.with_variant(TableLines::RoundsCenters)
});
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
rects(&pass.list, 0)
.into_iter()
.filter(|&(_, _, w, h)| h > w)
.map(|(x, ..)| x)
.min()
}
#[test]
fn takes_the_fixed_point_dot_for_a_table_at_an_offset_on_half_a_dot() {
use crate::quirks::TableEdgeHalves;
for (offset, above) in [(2191, 1), (2179, 0)] {
let place = format!(r#"w:horzAnchor="page" w:tblpX="{offset}""#);
let below = left_line(&place, 3000, TableEdgeHalves::BelowOrExact);
let fixed = left_line(&place, 3000, TableEdgeHalves::FixedPoint);
assert_eq!(
fixed.zip(below).map(|(fixed, below)| fixed - below),
Some(above),
"{offset}"
);
}
}
#[test]
fn keeps_the_dot_below_for_a_centered_table_on_half_a_dot() {
use crate::quirks::TableEdgeHalves;
let place = r#"w:horzAnchor="page" w:tblpXSpec="center""#;
for width in 5000..5048 {
let below = left_line(place, width, TableEdgeHalves::BelowOrExact);
assert!(below.is_some());
assert_eq!(
below,
left_line(place, width, TableEdgeHalves::FixedPoint),
"{width}"
);
}
}