use super::{CARLITO_RUN, SECTION, compatibility_mode, docx, placed, render};
fn exact(names: &[&str], more: &str) -> String {
let runs: Vec<String> = names
.iter()
.map(|name| format!("<w:r>{CARLITO_RUN}<w:t>{name}</w:t></w:r>"))
.collect();
let runs = runs.join(&format!("<w:r>{CARLITO_RUN}<w:br/></w:r>"));
format!(r#"<w:p><w:pPr>{more}<w:spacing w:line="240" w:lineRule="exact"/></w:pPr>{runs}</w:p>"#)
}
fn framed(frame: &str, lines: usize) -> String {
let height = 240 * lines;
format!(
r#"<w:p><w:pPr><w:keepNext/><w:framePr {frame} w:wrap="around" w:vAnchor="text" w:hAnchor="text"/><w:spacing w:line="{height}" w:lineRule="exact"/></w:pPr><w:r>{CARLITO_RUN}<w:t>Q</w:t></w:r></w:p>"#
)
}
fn drop_cap(lines: usize) -> String {
framed(&format!(r#"w:dropCap="drop" w:lines="{lines}""#), lines)
}
fn pages_of(filler: usize, blocks: &str, mode: u32) -> Vec<(String, usize, f64)> {
let lines: String = (0..filler).map(|_| exact(&["f"], "")).collect();
let body = format!("{lines}{blocks}<w:sectPr>{SECTION}</w:sectPr>");
let list = render(&docx(&body, &[], &compatibility_mode(mode)));
placed(&list)
.iter()
.enumerate()
.flat_map(|(page, runs)| {
runs.iter()
.map(move |(x, _, text)| (text.trim().to_owned(), page + 1, *x))
})
.collect()
}
fn page_of(places: &[(String, usize, f64)], text: &str) -> usize {
places.iter().find(|place| place.0 == text).unwrap().1
}
fn heading() -> String {
exact(&["Head"], "<w:keepNext/>")
}
#[test]
fn keeps_a_heading_with_the_text_after_a_drop_cap() {
let blocks = heading() + &drop_cap(2) + &exact(&["b", "t", "l"], "");
let fits = pages_of(6, &blocks, 15);
assert_eq!(page_of(&fits, "Head"), 1, "{fits:?}");
assert_eq!(page_of(&fits, "l"), 1, "{fits:?}");
let moves = pages_of(7, &blocks, 15);
assert_eq!(page_of(&moves, "Head"), 2, "{moves:?}");
assert_eq!(page_of(&moves, "Q"), 2, "{moves:?}");
}
#[test]
fn takes_a_drop_cap_to_the_next_page_with_its_text() {
let blocks = drop_cap(2) + &exact(&["b", "t", "l"], "");
let modern = pages_of(8, &blocks, 15);
assert_eq!(page_of(&modern, "b"), 2, "{modern:?}");
assert_eq!(page_of(&modern, "Q"), 2, "{modern:?}");
let old = pages_of(8, &blocks, 14);
assert_eq!(page_of(&old, "b"), 2, "{old:?}");
assert_eq!(page_of(&old, "Q"), 1, "{old:?}");
}
#[test]
fn moves_a_frame_that_does_not_fit_and_its_text_with_it() {
let text = exact(&["b", "t", "u", "v", "w", "l"], "");
for mode in [15, 14] {
let places = pages_of(8, &(drop_cap(3) + &text), mode);
assert_eq!(page_of(&places, "Q"), 2, "{places:?}");
assert_eq!(page_of(&places, "b"), 2, "{places:?}");
}
let fits = pages_of(8, &(drop_cap(2) + &text), 15);
assert_eq!(page_of(&fits, "Q"), 1, "{fits:?}");
assert_eq!(page_of(&fits, "b"), 1, "{fits:?}");
assert_eq!(page_of(&fits, "l"), 2, "{fits:?}");
}
#[test]
fn ends_a_chain_of_kept_paragraphs_at_a_frame_that_is_no_drop_cap() {
let text = exact(&["b", "t", "l"], "");
let plain = heading() + &framed(r#"w:w="600""#, 2) + &text;
for mode in [15, 14] {
let places = pages_of(7, &plain, mode);
assert_eq!(page_of(&places, "Head"), 1, "{places:?}");
assert_eq!(page_of(&places, "Q"), 1, "{places:?}");
assert_eq!(page_of(&places, "b"), 2, "{places:?}");
}
let old = pages_of(7, &(heading() + &drop_cap(2) + &text), 14);
assert_eq!(page_of(&old, "Head"), 1, "{old:?}");
assert_eq!(page_of(&old, "b"), 2, "{old:?}");
let tall = pages_of(8, &plain, 15);
assert_eq!(page_of(&tall, "Head"), 2, "{tall:?}");
}
#[test]
fn gives_lines_their_whole_width_on_the_page_after_a_frame() {
let text = exact(&["b", "t", "u", "v", "w", "l"], "");
let places = pages_of(8, &(drop_cap(2) + &text), 15);
let start = |text: &str| places.iter().find(|place| place.0 == text).unwrap().2;
assert!(start("b") > 36.5, "{places:?}");
assert_eq!(page_of(&places, "u"), 2, "{places:?}");
assert!((start("u") - 36.0).abs() < 0.01, "{places:?}");
assert!((start("l") - 36.0).abs() < 0.01, "{places:?}");
}
#[test]
fn keeps_a_frame_with_a_distance_down_on_its_page() {
let blocks = framed(r#"w:w="600" w:y="1200""#, 1) + &exact(&["b"], "");
for mode in [15, 14] {
let places = pages_of(8, &blocks, mode);
assert_eq!(page_of(&places, "Q"), 1, "{places:?}");
assert_eq!(page_of(&places, "b"), 1, "{places:?}");
}
}
fn at_right(lines: &[&str], more: &str) -> String {
let frame =
r#"<w:framePr w:wrap="none" w:vAnchor="text" w:hAnchor="margin" w:xAlign="right"/>"#;
exact(lines, &format!("{frame}{more}"))
}
fn place_of(body: &str, text: &str) -> (f64, f64) {
let body = format!("{body}<w:sectPr>{SECTION}</w:sectPr>");
let list = render(&docx(&body, &[], &compatibility_mode(15)));
let runs = &placed(&list)[0];
let run = runs.iter().find(|run| run.2.trim() == text).unwrap();
(run.0, run.1)
}
#[test]
fn starts_a_frame_below_the_space_after_the_paragraph_before() {
let lead = format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="480" w:line="240" w:lineRule="exact"/></w:pPr><w:r>{CARLITO_RUN}<w:t>a</w:t></w:r></w:p>"#
);
let body = format!("{lead}{}{}", at_right(&["Q"], ""), exact(&["b"], ""));
let (a, q, b) = (
place_of(&body, "a").1,
place_of(&body, "Q").1,
place_of(&body, "b").1,
);
assert!((q - b).abs() < 0.01, "{q} {b}");
assert!((q - a - 36.0).abs() < 0.01, "{q} {a}");
}
#[test]
fn counts_the_first_line_indent_in_a_frame_without_a_width() {
let plain = place_of(&at_right(&["Q"], ""), "Q").0;
let indented = place_of(&at_right(&["Q"], r#"<w:ind w:firstLine="432"/>"#), "Q").0;
assert!(plain > 200.0, "{plain}");
assert!((plain - indented).abs() < 0.01, "{plain} {indented}");
}
#[test]
fn makes_a_frame_of_two_lines_as_wide_as_its_area() {
let one = place_of(&at_right(&["Q"], ""), "Q").0;
let two = place_of(&at_right(&["Q", "R"], ""), "Q").0;
assert!(one > 200.0, "{one}");
assert!((two - 36.0).abs() < 0.01, "{two}");
}
fn step_past(attributes: &str) -> f64 {
let marked = exact(&["Q"], &format!("<w:framePr {attributes}/>"));
let body = exact(&["a"], "") + &marked + &exact(&["b"], "");
place_of(&body, "b").1 - place_of(&body, "a").1
}
#[test]
fn keeps_a_paragraph_in_the_flow_when_its_frame_mark_names_no_place() {
for plain in [
"",
r#"w:wrap="auto""#,
r#"w:hAnchor="text" w:xAlign="left" w:hSpace="141""#,
r#"w:vAnchor="page""#,
r#"w:vAnchor="margin" w:w="0" w:y="0""#,
r#"w:dropCap="drop""#,
] {
assert!((step_past(plain) - 24.0).abs() < 0.01, "{plain}");
}
for frame in [
r#"w:vAnchor="text""#,
r#"w:wrap="none""#,
r#"w:hAnchor="margin""#,
r#"w:x="200""#,
] {
assert!((step_past(frame) - 12.0).abs() < 0.01, "{frame}");
}
}