use crate::layout::testing::{
AnchoredShape, NORMAL, SQUARE_WRAP, ShapeText, base, filler, find, find_all, lay_out,
on_base_with, p, pass_with, table,
};
use crate::numbering::Numbering;
use crate::quirks::CellDrawingBounds;
fn cell_with_box(down: &str, wrap: &str) -> String {
let blocks = p("d");
let shape = AnchoredShape {
across: ("column", 0),
down: (down, 200 * 635),
size: (635_000, 2000 * 635),
wrap,
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
let cell = format!(
"<w:p><w:r><w:t>a</w:t></w:r><w:r>{}</w:r></w:p>",
shape.drawing()
);
filler(2) + &table("", &[4000], &[("", &[&cell])]) + &p("z")
}
#[test]
fn places_a_drawing_from_the_margin_down_from_its_cells_text_top() {
let top = 1440 + 400;
let list = lay_out(&cell_with_box("margin", SQUARE_WRAP), NORMAL, None);
assert_eq!(find(&list, 'd').2, base(top + 200));
assert_eq!(find(&list, 'z').2, base(top + 200 + 2000));
let list = lay_out(&cell_with_box("margin", "<wp:wrapNone/>"), NORMAL, None);
assert_eq!(find(&list, 'd').2, base(top + 200));
assert_eq!(find(&list, 'z').2, base(top + 200));
let list = lay_out(&cell_with_box("page", "<wp:wrapNone/>"), NORMAL, None);
assert_eq!(find(&list, 'd').2, base(200));
}
const NO_WRAP: &str = "<wp:wrapNone/>";
fn box_in_second_cell(across: i64, down: i64, wrap: &str) -> String {
let blocks = p("d");
let shape = AnchoredShape {
across: ("column", across * 635),
down: ("paragraph", down * 635),
size: (1000 * 635, 400 * 635),
wrap,
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
let cell = format!(
"<w:p><w:r>{}</w:r><w:r><w:t>b</w:t></w:r></w:p>",
shape.drawing()
);
let props = r#"<w:tblLayout w:type="fixed"/><w:tblCellMar><w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar><w:tblBorders><w:left w:val="single" w:sz="8"/><w:right w:val="single" w:sz="8"/><w:insideV w:val="single" w:sz="8"/></w:tblBorders>"#;
filler(2) + &table(props, &[4000, 4000], &[("", &[&p("a"), &cell])]) + &p("z")
}
fn box_place(bounds: CellDrawingBounds, body: &str) -> (usize, i64, i64) {
let target = on_base_with(|p| p.with_variant(bounds));
let list = pass_with(body, ("", None), None, Numbering::default(), target).list;
find(&list, 'd')
}
#[test]
fn keeps_a_box_that_wraps_text_inside_its_cell_where_the_target_does() {
let cases = [
(-2000, 0, SQUARE_WRAP),
(3500, 0, SQUARE_WRAP),
(0, -300, SQUARE_WRAP),
(-2000, -300, NO_WRAP),
(-50, 100, SQUARE_WRAP),
];
let kept = [(-98, 0), (2882, 0), (0, 0), (-2000, -300), (-50, 100)];
let at = |bounds| {
cases.map(|(across, down, wrap)| box_place(bounds, &box_in_second_cell(across, down, wrap)))
};
let free = |offsets: [(i64, i64); 5]| {
offsets.map(|(across, down)| {
box_place(
CellDrawingBounds::Unbounded,
&box_in_second_cell(across, down, NO_WRAP),
)
})
};
let own = cases.map(|(across, down, _)| (across, down));
assert_eq!(at(CellDrawingBounds::InsideCell), free(kept));
assert_eq!(at(CellDrawingBounds::Unbounded), free(own));
assert_ne!(free(kept)[..3], free(own)[..3]);
}
const CELL_TEXT_TOP: i64 = 1440 + 400;
type Places = ((usize, i64, i64), (usize, i64, i64), (usize, i64, i64));
fn wrapped_places(wrap: &str) -> Places {
let list = lay_out(&box_in_second_cell(0, 0, wrap), NORMAL, None);
(find(&list, 'a'), find(&list, 'b'), find(&list, 'z'))
}
#[test]
fn lays_the_lines_of_a_cell_with_a_box_that_does_not_wrap_as_without_it() {
let top = CELL_TEXT_TOP;
let (a, free, below) = wrapped_places(NO_WRAP);
assert_eq!(
(a.2, free.2, below.2),
(base(top), base(top), base(top + 200))
);
}
#[test]
fn keeps_the_lines_of_a_cell_below_a_box_in_that_cell_wrapped_top_and_bottom() {
let top = CELL_TEXT_TOP;
let (a, free, _) = wrapped_places(NO_WRAP);
let (a_kept, b, z) = wrapped_places("<wp:wrapTopAndBottom/>");
assert_eq!(a_kept, a);
assert_eq!((b.1, b.2), (free.1, base(top + 400)));
assert_eq!(z.2, base(top + 600));
}
#[test]
fn keeps_the_lines_of_a_cell_beside_a_box_in_that_cell_wrapped_square() {
let (a, free, _) = wrapped_places(NO_WRAP);
let (a_kept, b, z) = wrapped_places(SQUARE_WRAP);
assert_eq!(a_kept, a);
assert_eq!(b.2, free.2);
assert!(b.1 > free.1);
assert_eq!(z.2, base(CELL_TEXT_TOP + 400));
}
#[test]
fn keeps_the_wrap_of_a_box_in_a_cell_on_its_own_page() {
let blocks = p("d");
let shape = AnchoredShape {
across: ("column", 0),
down: ("paragraph", 0),
size: (1000 * 635, 400 * 635),
wrap: SQUARE_WRAP,
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
let first = format!(
"<w:p><w:r>{}</w:r><w:r><w:t>b</w:t></w:r></w:p>",
shape.drawing()
);
let cell = first + &(0..100).map(|_| p("w")).collect::<String>();
let list = lay_out(&table("", &[4000], &[("", &[&cell])]), NORMAL, None);
let lines = find_all(&list, 'w');
let column = lines.iter().map(|line| line.1).min().unwrap();
assert!(lines.iter().any(|line| line.0 == 0 && line.1 > column));
assert!(lines.iter().any(|line| line.0 == 1));
assert!(
lines
.iter()
.filter(|line| line.0 == 1)
.all(|line| line.1 == column)
);
}
#[test]
fn aligns_a_box_from_the_margin_in_a_cell_at_the_cells_text_top_where_the_target_does() {
let top = 1440 + 400;
let offset =
r#"<wp:positionV relativeFrom="paragraph"><wp:posOffset>0</wp:posOffset></wp:positionV>"#;
for align in ["top", "center", "bottom"] {
let aligned = format!(
r#"<wp:positionV relativeFrom="margin"><wp:align>{align}</wp:align></wp:positionV>"#
);
for wrap in [NO_WRAP, SQUARE_WRAP] {
let body = box_in_second_cell(0, 0, wrap);
assert_eq!(body.matches(offset).count(), 1);
let body = body.replace(offset, &aligned);
let (_, _, kept) = box_place(CellDrawingBounds::InsideCell, &body);
assert_eq!(kept, base(top));
let (_, _, free) = box_place(CellDrawingBounds::Unbounded, &body);
assert_ne!(free, base(top));
}
}
}
#[test]
fn keeps_the_alignment_of_a_box_aligned_from_the_margin_outside_a_cell() {
let offset =
r#"<wp:positionV relativeFrom="paragraph"><wp:posOffset>0</wp:posOffset></wp:positionV>"#;
for align in ["center", "bottom"] {
let aligned = format!(
r#"<wp:positionV relativeFrom="margin"><wp:align>{align}</wp:align></wp:positionV>"#
);
let blocks = p("d");
let shape = AnchoredShape {
across: ("column", 0),
down: ("paragraph", 0),
size: (635_000, 400 * 635),
wrap: NO_WRAP,
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
let body = format!("<w:p><w:r>{}</w:r></w:p>", shape.drawing()).replace(offset, &aligned);
let places = [CellDrawingBounds::Unbounded, CellDrawingBounds::InsideCell]
.map(|bounds| box_place(bounds, &body));
assert_eq!(places[0], places[1]);
assert!(places[0].2 > base(1440 + 2000));
}
}
#[test]
fn bounds_no_box_by_the_cells_text_top_on_a_later_page_of_the_cell() {
let blocks = p("d");
let shape = AnchoredShape {
across: ("column", 0),
down: ("paragraph", 100 * 635),
size: (635_000, 400 * 635),
wrap: SQUARE_WRAP,
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
let cell = filler(50)
+ &format!(
"<w:p><w:r>{}</w:r><w:r><w:t>b</w:t></w:r></w:p>",
shape.drawing()
);
let body = filler(20) + &table("", &[4000], &[("", &[&cell])]);
let places = [CellDrawingBounds::Unbounded, CellDrawingBounds::InsideCell]
.map(|bounds| box_place(bounds, &body));
assert_eq!(places[0].0, 1);
assert_eq!(places[0], places[1]);
}