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("baseline", 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}");
}
}
#[test]
fn aligns_exact_lines_at_center_and_top_with_and_without_space_for_ul() {
let exact = r#"w:line="400" w:lineRule="exact""#;
let body = paragraph("center", exact, "", &[("a", 24), ("b", 40)])
+ ¶graph("top", exact, "", &[("c", 24)])
+ ¶graph("baseline", exact, "", &[("r", 24)]);
let still = lay_out(&body, "", CenteredLine::OnBaseline);
let moved = lay_out(&body, "", CenteredLine::MiddlesShared);
let shifts =
|list: &DisplayList| ['a', 'b', 'c', 'r'].map(|ch| find(list, ch).2 - find(&still, ch).2);
assert_eq!(shifts(&moved), [dots(36), dots(60), dots(96), 0]);
let with_ul = lay_out_with_space_for_ul(&body);
assert_eq!(shifts(&with_ul), [-dots(24), 0, -dots(24), 0]);
}
fn lay_out_with_space_for_ul(body: &str) -> DisplayList {
use crate::body::Body;
use crate::layout::testing::{Fake, base_context, on_base_with, parse, sections_of};
use crate::layout::{Context, Layout, Stories};
use crate::reader::PartReader;
use crate::styles::{FontTable, Settings, SpaceForUl, Styles, Theme};
use crate::text::{Shaper, ShaperSettings};
let (device, quirks) = on_base_with(|p| p.with_variant(CenteredLine::MiddlesShared));
let settings_xml =
format!(r#"<w:settings xmlns:w="{W}"><w:compat><w:spaceForUL/></w:compat></w:settings>"#);
let settings_doc = parse(&settings_xml);
let settings = Settings::read(
&mut PartReader::new(&settings_doc),
settings_doc.root().unwrap(),
);
assert_eq!(settings.space_for_ul, SpaceForUl::On);
let doc = parse(&format!(r#"<w:body xmlns:w="{W}">{body}</w:body>"#));
let body = Body::read(
&mut PartReader::new(&doc),
doc.root().unwrap(),
(
crate::reader::settings::ReaderSettings::of(&quirks),
settings.mode_rules,
),
);
let styles = Styles::default();
let context = Context {
device,
quirks,
..base_context(&styles, &settings)
};
let stories = context.for_stories();
let sections = sections_of(&body, &Stories::default());
let (theme, table, shaper) = (
Theme::default(),
FontTable::default(),
ShaperSettings::base(),
);
Layout::new(
(&context, &stories),
§ions,
Shaper::new(&Fake, &theme, &table, &shaper),
)
.run_with_wraps(&body)
.list
}
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()
}