use f_office_units::Length;
use super::run_of;
use crate::grid::{DOT, TextObject};
use crate::layout::testing::{NORMAL, find, lay_out, on_base_with, p, pass_with, table};
use crate::ns::{A, PIC, R, WP};
use crate::numbering::Numbering;
use crate::quirks::{TurnedAcrossDots, TurnedLineRoom, TurnedPicture};
fn turned(direction: &str) -> String {
let cell = format!(
r#"<w:tcPr><w:textDirection w:val="{direction}"/></w:tcPr>{}"#,
p("bc")
);
table("", &[2000, 600], &[("", &[&p("a"), &cell])]) + &p("z")
}
#[test]
fn turns_the_text_of_a_cell_that_reads_up() {
let list = lay_out(&turned("btLr"), NORMAL, None);
assert!(
run_of(&list, 'b')
.turn
.is_some_and(|turn| turn.yx < 0 && turn.xy > 0)
);
let ((_, bx, by), (_, cx, cy)) = (find(&list, 'b'), find(&list, 'c'));
assert_eq!(bx, cx);
assert!(cy < by);
let plain = table("", &[2000, 600], &[("", &[&p("a"), &p("")])]) + &p("z");
assert_eq!(
find(&list, 'z').2,
find(&lay_out(&plain, NORMAL, None), 'z').2
);
}
#[test]
fn turns_the_text_of_a_cell_that_reads_down() {
let list = lay_out(&turned("tbRl"), NORMAL, None);
assert!(
run_of(&list, 'b')
.turn
.is_some_and(|turn| turn.yx > 0 && turn.xy < 0)
);
let ((_, bx, by), (_, cx, cy)) = (find(&list, 'b'), find(&list, 'c'));
assert_eq!(bx, cx);
assert!(cy > by);
let (_, ax, _) = find(&list, 'a');
assert!(bx > ax);
}
fn turned_region(direction: &str, text: &str) -> String {
let start = format!(
r#"<w:tcPr><w:vMerge w:val="restart"/><w:textDirection w:val="{direction}"/></w:tcPr>{}"#,
p(text)
);
let on = "<w:tcPr><w:vMerge/></w:tcPr><w:p/>";
let exact = r#"<w:trHeight w:val="400" w:hRule="exact"/>"#;
let rows: &[(&str, &[&str])] = &[
(exact, &[&p("a"), &start]),
(exact, &[&p("b"), on]),
(exact, &[&p("c"), on]),
];
table("", &[2000, 600], rows) + &p("z")
}
#[test]
fn turns_the_text_of_a_merged_cell_over_the_whole_region() {
let pass = |body: &str| lay_out(body, NORMAL, None);
let list = pass(&turned_region("btLr", "ghijkl"));
assert!(
run_of(&list, 'g')
.turn
.is_some_and(|turn| turn.yx < 0 && turn.xy > 0)
);
let ((_, gx, gy), (_, lx, ly)) = (find(&list, 'g'), find(&list, 'l'));
assert_eq!(gx, lx);
assert!(ly < gy);
assert!(gy > find(&list, 'c').2);
let plain = table(
"",
&[2000, 600],
&[
(
r#"<w:trHeight w:val="400" w:hRule="exact"/>"#,
&[&p("a"), &p("")],
),
(
r#"<w:trHeight w:val="400" w:hRule="exact"/>"#,
&[&p("b"), &p("")],
),
(
r#"<w:trHeight w:val="400" w:hRule="exact"/>"#,
&[&p("c"), &p("")],
),
],
) + &p("z");
assert_eq!(find(&list, 'z').2, find(&pass(&plain), 'z').2);
let down = pass(&turned_region("tbRl", "ghijkl"));
let ((_, gx, gy), (_, lx, ly)) = (find(&down, 'g'), find(&down, 'l'));
assert_eq!(gx, lx);
assert!(gy < find(&down, 'b').2 && ly > gy);
}
fn turned_lines(direction: &str, spacing: &str) -> String {
let run = |t: &str| {
format!(r#"<w:r><w:rPr><w:sz w:val="64"/><w:szCs w:val="64"/></w:rPr><w:t>{t}</w:t></w:r>"#)
};
let cell = format!(
r#"<w:tcPr><w:textDirection w:val="{direction}"/></w:tcPr><w:p><w:pPr><w:spacing w:before="0" w:after="0" {spacing}/></w:pPr>{}<w:r><w:br/></w:r>{}</w:p>"#,
run("b"),
run("c")
);
table("", &[2000, 2000], &[("", &[&p("a"), &cell])])
}
#[test]
fn makes_each_turned_line_a_32nd_of_its_size_taller_where_the_target_does() {
let point = Length::UNITS_PER_POINT;
for direction in ["btLr", "tbRl"] {
let places = |spacing: &str, room: TurnedLineRoom| {
let target = on_base_with(|p| p.with_variant(room));
let body = turned_lines(direction, spacing);
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
let sign = if direction == "btLr" { 1 } else { -1 };
(find(&list, 'b').1 * sign, find(&list, 'c').1 * sign)
};
for (spacing, apart, first) in [
("", point, point / 2),
(r#"w:line="480" w:lineRule="auto""#, 2 * point, point / 2),
(r#"w:line="650" w:lineRule="atLeast""#, point / 2, 0),
(r#"w:line="670" w:lineRule="atLeast""#, 0, -point / 2),
(r#"w:line="800" w:lineRule="exact""#, 0, 0),
] {
let (b, c) = places(spacing, TurnedLineRoom::Upright);
let (taller_b, taller_c) = places(spacing, TurnedLineRoom::SizeShare);
let grown = (taller_c - taller_b) - (c - b);
assert!(
(grown - apart).abs() <= DOT,
"{direction} {spacing}: {grown}"
);
let moved = taller_b - b;
assert!(
(moved - first).abs() <= DOT,
"{direction} {spacing}: {moved}"
);
}
}
}
fn turned_rows(direction: &str, align: &str) -> String {
let plain = p("a");
let turned: Vec<String> = ["b", "c", "d", "e", "f"]
.iter()
.map(|letter| {
format!(
r#"<w:tcPr><w:textDirection w:val="{direction}"/><w:vAlign w:val="{align}"/></w:tcPr>{}"#,
p(letter)
)
})
.collect();
let cells: Vec<[&str; 2]> = turned.iter().map(|cell| [plain.as_str(), cell]).collect();
let height = r#"<w:trHeight w:val="361" w:hRule="exact"/>"#;
let rows: Vec<(&str, &[&str])> = cells.iter().map(|pair| (height, &pair[..])).collect();
table("", &[2000, 600], &rows)
}
fn turned_row_places(direction: &str, align: &str, dots: TurnedAcrossDots) -> [i64; 5] {
let target = on_base_with(|p| p.with_variant(dots));
let body = turned_rows(direction, align);
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
['b', 'c', 'd', 'e', 'f'].map(|letter| find(&list, letter).1)
}
#[test]
fn puts_a_turned_line_on_one_whole_dot_across_in_every_row_where_the_target_does() {
let page = turned_row_places("btLr", "top", TurnedAcrossDots::PageDots);
assert!(
page.iter().all(|&x| x == page[0] && x.rem_euclid(DOT) == 0),
"{page:?}"
);
let frame = turned_row_places("btLr", "top", TurnedAcrossDots::FrameDots);
assert!(frame.iter().any(|&x| x != frame[0]), "{frame:?}");
}
#[test]
fn keeps_the_places_from_the_row_top_for_other_turned_cells_where_the_target_does() {
for (direction, align) in [("tbRl", "top"), ("btLr", "center"), ("btLr", "bottom")] {
let page = turned_row_places(direction, align, TurnedAcrossDots::PageDots);
let frame = turned_row_places(direction, align, TurnedAcrossDots::FrameDots);
assert_eq!(page, frame, "{direction} {align}");
assert!(
frame.iter().any(|&x| x != frame[0]),
"{direction} {align}: {frame:?}"
);
}
}
#[test]
fn writes_a_turned_line_on_the_page_dots_where_the_writer_writes_a_text_objects_start() {
let mut moved = false;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let quirks = profile.quirks();
if quirks.turned_across_dots != TurnedAcrossDots::PageDots {
continue;
}
let (device, dot) = (profile.device(), profile.device_dot().units());
let body = turned_rows("btLr", "top");
let list = pass_with(
&body,
("", None),
None,
Numbering::default(),
(device, quirks),
)
.list;
let page = &list.pages()[0];
let object =
TextObject::of(&device.with_page(page.width(), page.height(), page.orientation()));
for letter in ['b', 'c', 'd', 'e', 'f'] {
let x = find(&list, letter).1;
let on_dot = Length::from_units(f_office_units::div_round(x, dot) * dot);
assert_eq!(
x,
object.written_start_of(on_dot).units(),
"{target} {letter}"
);
moved |= x != on_dot.units();
}
}
assert!(moved);
}
#[test]
fn takes_the_text_edge_of_a_cell_after_a_turned_cell_from_the_page_columns_edge() {
let page = r#"<w:sectPr><w:pgMar w:left="1238" w:right="1138"/></w:sectPr>"#;
let body = |second: &str| {
let rows: &[(&str, &[&str])] = &[
("", &[&p("a"), second, &p("d")]),
("", &[&p("e"), &p("f"), &p("g")]),
];
table(
r#"<w:tblInd w:w="0" w:type="dxa"/>"#,
&[556, 1056, 2207],
rows,
) + page
};
let turned = format!(
r#"<w:tcPr><w:textDirection w:val="btLr"/></w:tcPr>{}"#,
p("bc")
);
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let on = (profile.device(), profile.quirks());
let x = |second: &str| {
let pass = pass_with(
&body(second),
(NORMAL, Some(14)),
None,
Numbering::default(),
on,
);
find(&pass.list, 'g').1
};
assert_eq!(x(&turned), x(&p("bc")), "{target}");
}
}
#[test]
fn takes_a_turned_lines_dot_at_a_tie_as_the_sum_of_its_sides_parts_gives_it() {
let body = |(page, margin, first): (i64, i64, i64), size: u32| {
let props = format!(
r#"<w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:left w:w="{margin}" w:type="dxa"/></w:tblCellMar>"#
);
let turned = format!(
r#"<w:tcPr><w:textDirection w:val="btLr"/></w:tcPr><w:p><w:pPr><w:rPr><w:sz w:val="{size}"/></w:rPr></w:pPr><w:r><w:rPr><w:sz w:val="{size}"/></w:rPr><w:t>t</w:t></w:r></w:p>"#
);
let height = r#"<w:trHeight w:val="600" w:hRule="exact"/>"#;
let rows: &[(&str, &[&str])] = &[("", &[&p("a"), &p("u")]), (height, &[&p("b"), &turned])];
let section = format!(r#"<w:sectPr><w:pgMar w:left="{page}" w:right="1138"/></w:sectPr>"#);
table(&props, &[first, 1200], rows) + §ion
};
let mut checked = false;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let on = (profile.device(), profile.quirks());
if on.1.turned_across_dots != TurnedAcrossDots::PageDots {
continue;
}
for (mode, firsts) in [(14, (556, 552)), (15, (448, 446))] {
let places = |case, size| {
let list = pass_with(
&body(case, size),
(NORMAL, Some(mode)),
None,
Numbering::default(),
on,
)
.list;
(find(&list, 'u').1, find(&list, 't').1)
};
let down = (1238, 108, firsts.0);
let up = (1242, 106, firsts.1);
let (tie_down, tie_up) = (places(down, 24), places(up, 24));
assert_ne!(tie_down.0, tie_up.0, "{target} {mode}");
assert_eq!(
tie_down.1 - tie_up.1,
tie_down.0 - tie_up.0,
"{target} {mode}"
);
let (off_down, off_up) = (places(down, 22), places(up, 22));
assert_eq!(off_down.1, off_up.1, "{target} {mode}");
checked = true;
}
}
assert!(checked);
}
fn turned_picture() -> String {
let picture = format!(
r#"<w:r><w:drawing><wp:inline xmlns:wp="{WP}" distT="0" distB="0" distL="0" distR="0"><wp:extent cx="457200" cy="228600"/><a:graphic xmlns:a="{A}"><a:graphicData><pic:pic xmlns:pic="{PIC}"><pic:blipFill><a:blip xmlns:r="{R}" r:embed="rId1"/></pic:blipFill><pic:spPr/></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r>"#
);
let cell = format!(
r#"<w:tcPr><w:textDirection w:val="btLr"/></w:tcPr><w:p><w:r><w:t>a</w:t></w:r>{picture}<w:r><w:t>b</w:t></w:r></w:p>"#
);
table(
"",
&[2000, 2000],
&[(
r#"<w:trHeight w:val="4000" w:hRule="exact"/>"#,
&[&p("x"), &cell],
)],
)
}
#[test]
fn gives_an_upright_picture_of_a_turned_cell_its_height_along_the_line() {
let point = Length::UNITS_PER_POINT;
let places = |pictures: TurnedPicture| {
let target = on_base_with(|p| p.with_variant(pictures));
let list = pass_with(
&turned_picture(),
("", None),
None,
Numbering::default(),
target,
)
.list;
let ((_, ax, ay), (_, bx, by)) = (find(&list, 'a'), find(&list, 'b'));
assert_eq!(ax, bx);
(bx, ay - by)
};
let (in_frame_across, in_frame_along) = places(TurnedPicture::InFrame);
let (upright_across, upright_along) = places(TurnedPicture::Upright);
assert!(
(in_frame_along - upright_along - 18 * point / DOT).abs() <= 1,
"{in_frame_along} {upright_along}"
);
assert!(
(upright_across - in_frame_across - 18 * point).abs() <= DOT,
"{in_frame_across} {upright_across}"
);
}