//! Floating drawings anchored in a table cell and placed down the page: from the margins they
//! count from the cell's text top, from the page they do not. Where the target keeps them, a
//! drawing that text wraps around stays inside its cell, and a drawing aligned from the margin
//! lies at the cell's text top.
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;
/// Two lines, then a table whose one cell holds "a" and a box of 2000 twips with "d" at its
/// top, 200 twips below what `down` names, with the wrap `wrap`; "z" below the table.
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() {
// The cell's text starts two lines of 200 twips below the top margin of 1440 twips.
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));
// The row grows to the bottom of a box that wraps text.
assert_eq!(find(&list, 'z').2, base(top + 200 + 2000));
// A box that does not wrap text leaves the row at its text's height.
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));
// A box placed from the page counts from the page's top.
let list = lay_out(&cell_with_box("page", "<wp:wrapNone/>"), NORMAL, None);
assert_eq!(find(&list, 'd').2, base(200));
}
/// The wrap of a box that text does not wrap around.
const NO_WRAP: &str = "<wp:wrapNone/>";
/// Two lines, then a table of two cells of 4000 twips with side lines of 1 pt and side cell
/// margins of 108 twips, whose text starts at the margin. The first cell holds "a"; the second
/// holds a box of 1000 by 400
/// twips with "d" at its left top, placed `across` twips from the cell's text column and `down`
/// twips from its paragraph, with the wrap `wrap`, and then "b".
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")
}
/// Where "d" lies in `body` on the base target with `bounds`: its x in units and its baseline
/// in device dots.
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() {
// The places of the box: past the cell's left and right lines and above its paragraph,
// wrapped square; past the left line and above the paragraph without wrap; and inside the
// cell, wrapped square.
let cases = [
(-2000, 0, SQUARE_WRAP),
(3500, 0, SQUARE_WRAP),
(0, -300, SQUARE_WRAP),
(-2000, -300, NO_WRAP),
(-50, 100, SQUARE_WRAP),
];
// The inner edge of the second cell's left line lies 108 twips, its cell margin, less 10
// twips, half the line, left of its text column; that of its right line 4000 - 118 twips
// right of it, so that a box of 1000 twips kept there starts 2882 twips right of the
// column. Down, a kept box's top is not above the cell's text top, its paragraph's top here.
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)))
};
// The places of a box that is never kept: no text wraps around it.
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]);
}
/// The cells' text top: two lines of 200 twips below the top margin of 1440 twips.
const CELL_TEXT_TOP: i64 = 1440 + 400;
/// Where "a" in the first cell, "b" in the second cell and "z" below the table lie when the
/// box in the second cell wraps by `wrap`.
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() {
// The box of 400 twips keeps the line of "b" below it, and the row grows to hold that
// line; "a" in the first cell stays.
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() {
// The line of "b" lies beside the box of 1000 twips.
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() {
// A cell that starts at the top of the first page holds a box of 1000 by 400 twips,
// wrapped square at its top, then 100 lines of "w" that run on to the second 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();
// Beside the box on the first page, the lines start right of it; on the second page,
// every line starts at the column's left.
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]);
// The box lies far below the margins' top.
assert!(places[0].2 > base(1440 + 2000));
}
}
#[test]
fn bounds_no_box_by_the_cells_text_top_on_a_later_page_of_the_cell() {
// A cell that starts 20 lines down the first page and holds 50 lines runs on to a second
// page, where its last paragraph, high on the page and above the cell's text top on the
// first, anchors a box that wraps text 100 twips below the paragraph's top.
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]);
}
#[test]
fn places_page_relative_cell_drawings_by_mode_and_layout_in_cell() {
use crate::ns::{A, WP, WPS};
let make = |lic: u8, h_from: &str, v_from: &str, v_inner: &str| {
let anchor = format!(
r#"<w:drawing><wp:anchor xmlns:wp="{WP}" distT="0" distB="0" distL="0" distR="0" simplePos="0" relativeHeight="1" behindDoc="0" locked="0" layoutInCell="{lic}" allowOverlap="1"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="{h_from}"><wp:posOffset>127000</wp:posOffset></wp:positionH><wp:positionV relativeFrom="{v_from}">{v_inner}</wp:positionV><wp:extent cx="635000" cy="400000"/><wp:effectExtent l="0" t="0" r="0" b="0"/><wp:wrapNone/><wp:docPr id="1" name="B"/><a:graphic xmlns:a="{A}"><a:graphicData uri="{WPS}"><wps:wsp xmlns:wps="{WPS}"><wps:cNvSpPr txBox="1"/><wps:spPr><a:prstGeom prst="rect"/></wps:spPr><wps:txbx><w:txbxContent><w:p><w:r><w:t>d</w:t></w:r></w:p></w:txbxContent></wps:txbx><wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/></wps:wsp></a:graphicData></a:graphic></wp:anchor></w:drawing>"#
);
let cell = format!("<w:p><w:r>{anchor}</w:r><w:r><w:t>b</w:t></w:r></w:p>");
filler(2) + &table("", &[4000, 4000], &[("", &[&p("a"), &cell])])
};
let run = |mode: Option<u32>, body: &str| {
let target = on_base_with(|p| p.with_variant(CellDrawingBounds::InsideCell));
let list = pass_with(body, ("", mode), None, Numbering::default(), target).list;
find(&list, 'd')
};
let off = "<wp:posOffset>127000</wp:posOffset>";
// Mode 15 ignores layoutInCell="0": both lay out 10 pt (200 twips) below CELL_TEXT_TOP.
let m15_on = run(Some(15), &make(1, "column", "page", off));
let m15_off = run(Some(15), &make(0, "column", "page", off));
// Mode 14 honors layoutInCell: "1" counts from CELL_TEXT_TOP, "0" from page top (200 twips).
let m14_on = run(Some(14), &make(1, "column", "page", off));
let m14_off = run(Some(14), &make(0, "column", "page", off));
// Mode 15 v=page align="bottom" lies at CELL_TEXT_TOP.
let m15_align = run(
Some(15),
&make(1, "column", "page", "<wp:align>bottom</wp:align>"),
);
// Mode 15 h=page offset counts from the second cell's left edge (not page 0).
let m15_hpage = run(Some(15), &make(1, "page", "page", off));
assert_eq!(
(m15_on.2, m15_off.2, m14_on.2, m14_off.2, m15_align.2),
(
base(CELL_TEXT_TOP + 200),
base(CELL_TEXT_TOP + 200),
base(CELL_TEXT_TOP + 200),
base(200),
base(CELL_TEXT_TOP),
)
);
assert!(m14_off.1 < m14_on.1);
assert!(m15_hpage.1 > m14_off.1);
}