use f_office_paint::Item;
use f_office_units::Length;
use crate::layout::testing::{NORMAL, p, pass_with, table};
use crate::numbering::Numbering;
#[test]
fn starts_a_tables_top_line_at_the_dot_of_its_top_as_a_line_top() {
let body = table(
r#"<w:tblBorders><w:top w:val="single" w:sz="8"/></w:tblBorders>"#,
&[2000],
&[("", &[&p("a")])],
) + r#"<w:sectPr><w:pgMar w:top="1134"/></w:sectPr>"#;
let top = Length::from_twips(1134).unwrap();
let mut moved = false;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let device = profile.device();
let list = pass_with(
&body,
(NORMAL, None),
None,
Numbering::default(),
(device, profile.quirks()),
)
.list;
let line = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) => Some(r.y),
_ => None,
})
.min()
.unwrap();
assert_eq!(line, device.top_dot(top), "{target}");
moved |= device.top_dot(top) != device.snap(top);
}
assert!(moved);
}
#[test]
fn starts_each_line_of_a_double_top_border_at_the_dot_of_its_own_top() {
let body = table(
r#"<w:tblBorders><w:top w:val="double" w:sz="6"/></w:tblBorders>"#,
&[2000],
&[("", &[&p("a")])],
) + r#"<w:sectPr><w:pgMar w:top="1135"/></w:sectPr>"#;
let top = Length::from_twips(1135).unwrap();
let second = top + Length::from_twips(30).unwrap();
let mut rounded_twice = false;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let device = profile.device();
let list = pass_with(
&body,
(NORMAL, None),
None,
Numbering::default(),
(device, profile.quirks()),
)
.list;
let mut tops: Vec<Length> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) if r.width > r.height => Some(r.y),
_ => None,
})
.collect();
tops.sort();
tops.dedup();
assert_eq!(
tops,
[device.top_dot(top), device.top_dot(second)],
"{target}"
);
let twice = device.snap(device.top_dot(top) + Length::from_twips(30).unwrap());
rounded_twice |= twice != device.top_dot(second);
}
assert!(rounded_twice);
}
#[test]
fn starts_a_lower_rows_top_line_at_the_dot_of_its_twips_rest() {
let row = r#"<w:trHeight w:val="240" w:hRule="exact"/>"#;
let mut rested = false;
for (margin, short) in [(1134, true), (1146, false)] {
let body = table(
r#"<w:tblBorders><w:insideH w:val="single" w:sz="8"/></w:tblBorders>"#,
&[2000],
&[(row, &[&p("a")]), (row, &[&p("b")])],
) + &format!(r#"<w:sectPr><w:pgMar w:top="{margin}"/></w:sectPr>"#);
let second = Length::from_twips(margin + 240).unwrap();
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let device = profile.device();
let list = pass_with(
&body,
(NORMAL, None),
None,
Numbering::default(),
(device, profile.quirks()),
)
.list;
let tops: Vec<Length> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) if r.width > r.height => Some(r.y),
_ => None,
})
.collect();
let summed = device.in_twip_parts(()).is_some();
let expected = if summed && short {
device.snap(second) - device.dots(1)
} else {
device.snap(second)
};
assert_eq!(tops, [expected], "{target} {margin}");
rested |= expected != device.snap(second);
}
}
assert!(rested);
}
#[test]
fn stacks_the_parts_of_a_table_line_of_several_lines_in_whole_dots() {
let body = table(
r#"<w:tblBorders><w:top w:val="thinThickThinMediumGap" w:sz="24"/></w:tblBorders>"#,
&[2000],
&[("", &[&p("a")])],
);
let mut checked = false;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let quirks = profile.quirks();
if quirks.compound_table_lines != crate::quirks::CompoundTableLines::WholeBorder {
continue;
}
let device = profile.device();
let list = pass_with(
&body,
(NORMAL, None),
None,
Numbering::default(),
(device, quirks),
)
.list;
let mut lines: Vec<(Length, Length)> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) if r.width > r.height => Some((r.y, r.height)),
_ => None,
})
.collect();
lines.sort();
lines.dedup();
let first = lines[0].0;
assert_eq!(
lines,
[
(first, device.dots(12)),
(first + device.dots(24), device.dots(25)),
(first + device.dots(61), device.dots(12)),
],
"{target}"
);
checked = true;
}
assert!(checked);
}