use f_office_paint::DisplayList;
use f_office_units::Length;
use crate::grid::DOT;
use crate::layout::testing::{LARGE_LABELS, find, lay_out_with_numbering_as, on_base};
use crate::ns::W;
use crate::quirks::{CenteredLine, CenteredMultiple};
const MIDDLE_PER_POINT: i64 = 3;
fn dots(tenths: i64) -> i64 {
f_office_units::div_round(tenths * Length::UNITS_PER_POINT / 10, DOT)
}
fn lay_out(body: &str, numbering: &str, variant: CenteredLine) -> DisplayList {
let numbering = format!(r#"<w:numbering xmlns:w="{W}">{numbering}</w:numbering>"#);
lay_out_with_numbering_as(body, &numbering, variant)
}
fn paragraph(alignment: &str, spacing: &str, list: &str, runs: &[(&str, u32)]) -> String {
let runs = runs
.iter()
.map(|(text, size)| {
format!(r#"<w:r><w:rPr><w:sz w:val="{size}"/></w:rPr><w:t>{text}</w:t></w:r>"#)
})
.collect::<Vec<_>>()
.concat();
format!(
r#"<w:p><w:pPr>{list}<w:spacing w:before="0" w:after="0" {spacing}/><w:textAlignment w:val="{alignment}"/></w:pPr>{runs}</w:p>"#
)
}
const SINGLE: &str = r#"w:line="240" w:lineRule="auto""#;
#[test]
fn the_base_profile_keeps_the_pieces_of_a_centered_line_on_one_baseline() {
assert_eq!(on_base().1.centered_line, CenteredLine::OnBaseline);
}
#[test]
fn shares_the_middles_of_two_sizes_and_keeps_the_line_height() {
let body = paragraph("center", SINGLE, "", &[("a", 24), ("b", 40)])
+ ¶graph("center", SINGLE, "", &[("c", 24)]);
let still = lay_out(&body, "", CenteredLine::OnBaseline);
let moved = lay_out(&body, "", CenteredLine::MiddlesShared);
assert_eq!(find(&still, 'a').2, find(&still, 'b').2);
assert_eq!(
find(&moved, 'b').2 - find(&moved, 'a').2,
dots(MIDDLE_PER_POINT * 8)
);
assert_eq!(find(&moved, 'b').2, find(&still, 'b').2);
assert_eq!(find(&moved, 'c').2, find(&still, 'c').2);
}
#[test]
fn a_list_label_counts_for_the_top_and_its_descent_stays_out() {
let list = r#"<w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr>"#;
let body = paragraph("center", SINGLE, list, &[("a", 24)])
+ ¶graph("center", SINGLE, "", &[("c", 24)]);
let still = lay_out(&body, LARGE_LABELS, CenteredLine::OnBaseline);
let moved = lay_out(&body, LARGE_LABELS, CenteredLine::MiddlesShared);
assert_eq!(find(&moved, '1').2, find(&still, '1').2);
assert_eq!(
find(&still, 'a').2 - find(&moved, 'a').2,
dots(MIDDLE_PER_POINT * 8)
);
assert_eq!(find(&still, 'c').2 - find(&moved, 'c').2, dots(24));
}
#[test]
fn leaves_other_alignments_and_spacings_on_one_baseline() {
let exact = r#"w:line="480" w:lineRule="exact""#;
let multiple = r#"w:line="360" w:lineRule="auto""#;
for body in [
paragraph("top", SINGLE, "", &[("a", 20), ("b", 40)]),
paragraph("baseline", SINGLE, "", &[("a", 20), ("b", 40)]),
paragraph("center", exact, "", &[("a", 20), ("b", 40)]),
paragraph("center", multiple, "", &[("a", 20), ("b", 40)]),
] {
let moved = lay_out(&body, "", CenteredLine::MiddlesShared);
assert_eq!(find(&moved, 'a').2, find(&moved, 'b').2, "{body}");
}
}
fn lay_out_multiple(body: &str, variant: CenteredMultiple) -> DisplayList {
let numbering = format!(r#"<w:numbering xmlns:w="{W}"></w:numbering>"#);
lay_out_with_numbering_as(body, &numbering, variant)
}
const MULTIPLE: &str = r#"w:line="360" w:lineRule="auto""#;
#[test]
fn the_base_profile_keeps_a_centered_line_of_multiple_spacing_on_its_baseline() {
assert_eq!(on_base().1.centered_multiple, CenteredMultiple::OnBaseline);
}
#[test]
fn rounds_the_middle_of_a_centered_line_of_multiple_spacing() {
let body = paragraph("center", MULTIPLE, "", &[("a", 20)]);
let still = lay_out_multiple(&body, CenteredMultiple::OnBaseline);
let moved = lay_out_multiple(&body, CenteredMultiple::MiddleRounded);
let dot = crate::grid::base_device().dots(1).units();
assert_eq!(baseline(&moved, 'a') - baseline(&still, 'a'), dot);
}
#[test]
fn leaves_single_spacing_other_alignments_and_two_faces_to_the_box() {
for body in [
paragraph("center", SINGLE, "", &[("a", 22)]),
paragraph("top", MULTIPLE, "", &[("a", 22)]),
paragraph("center", MULTIPLE, "", &[("a", 22), ("b", 40)]),
] {
let still = lay_out_multiple(&body, CenteredMultiple::OnBaseline);
let moved = lay_out_multiple(&body, CenteredMultiple::MiddleRounded);
assert_eq!(baseline(&moved, 'a'), baseline(&still, 'a'), "{body}");
}
}
fn baseline(list: &DisplayList, ch: char) -> i64 {
list.pages()
.iter()
.flat_map(f_office_paint::Page::painted)
.filter_map(|item| match item {
f_office_paint::Painted::Glyphs(run) => Some(run),
_ => None,
})
.flat_map(|run| {
run.glyphs
.iter()
.filter(|glyph| {
run.glyph_text(glyph)
.is_some_and(|text| text.starts_with(ch))
})
.map(|glyph| glyph.y.units())
})
.next()
.unwrap()
}