use f_office_paint::DisplayList;
use f_office_units::Length;
use f_office_warnings::Name;
use super::{Size, areas, place};
use crate::body::{Section, Sections};
use crate::chart::{Chart, Columns, Title};
use crate::floating::{Floating, PageArea, Position, Relative};
use crate::layout::testing::{
AnchoredShape, Fake, NORMAL, SQUARE_WRAP, ShapeText, base_context, du, filler, find, find_all,
kept, lay_out_with_header, on_base, on_base_with, p, pages, pass_with, pass_with_header,
passes, shape_top, x_du,
};
use crate::layout::{Layout, SectionLayout};
use crate::numbering::Numbering;
use crate::props::Align;
use crate::quirks::{
BlockedLines, BoxLastLine, BoxOutlineLeft, BoxTextTop, LineWrapBand, PolygonWrap, StepEnd,
VmlBoxOutline,
};
use crate::styles::{FontTable, Settings, Styles, Theme};
use crate::text::{Shaper, ShaperSettings};
use crate::{Drawing, Reason, Warning};
fn units(units: i64) -> Length {
Length::from_units(units)
}
#[test]
fn places_drawings_in_their_areas() {
let page = areas(units(1000), units(100), units(50));
let span = |start, end| (units(start), units(end));
assert_eq!(
(page.page, page.text, page.start_margin, page.end_margin),
(span(0, 1000), span(100, 950), span(0, 100), span(950, 1000))
);
let at = |from, offset, align: Option<Align>| {
let position = Position {
from,
offset: units(offset),
align,
share: None,
};
place(
position,
(units(20), Length::ZERO, Length::ZERO),
Length::ZERO,
&page,
units(300),
)
};
let share = |from, share| Position {
from,
offset: units(7),
align: None,
share: Some(share),
};
let sized = (units(20), Length::ZERO, Length::ZERO);
let shared = |from, value| place(share(from, value), sized, Length::ZERO, &page, units(300));
assert_eq!(shared(Relative::Area(PageArea::Page), 4_500), units(45));
assert_eq!(shared(Relative::Area(PageArea::Margin), 10_000), units(185));
assert_eq!(shared(Relative::Area(PageArea::Page), i64::MAX), units(7));
let cases = [
(Relative::Area(PageArea::Page), 30, None, 30),
(Relative::Area(PageArea::Margin), 30, None, 130),
(Relative::Area(PageArea::Margin), 0, Some(Align::Start), 100),
(
Relative::Area(PageArea::Margin),
0,
Some(Align::Center),
515,
),
(Relative::Area(PageArea::Margin), 0, Some(Align::End), 930),
(
Relative::Area(PageArea::StartMargin),
0,
Some(Align::Center),
40,
),
(
Relative::Area(PageArea::EndMargin),
0,
Some(Align::End),
980,
),
(Relative::Area(PageArea::EndMargin), 7, None, 957),
(Relative::Paragraph, 30, None, 330),
(Relative::Paragraph, 0, Some(Align::End), 300),
];
for (from, offset, align, expected) in cases {
assert_eq!(
at(from, offset, align),
units(expected),
"{from:?} {offset} {align:?}"
);
}
}
#[test]
fn aligns_a_drawing_with_its_effect_extent() {
let page = areas(units(1000), units(100), units(50));
let aligned = |align| Position {
from: Relative::Area(PageArea::Margin),
offset: Length::ZERO,
align: Some(align),
share: None,
};
let effect = (units(20), units(4), units(10));
let at = |align| place(aligned(align), effect, Length::ZERO, &page, units(300));
assert_eq!(at(Align::Start), units(104));
assert_eq!(at(Align::End), units(920));
assert_eq!(at(Align::Center), units(512));
}
#[test]
fn breaks_a_line_that_goes_to_the_next_page_beside_the_headers_drawing() {
let shape = AnchoredShape {
across: ("page", 3_886_200),
down: ("page", 914_400),
size: (1_828_800, 457_200),
wrap: SQUARE_WRAP,
outline: None,
text: None,
};
let header = format!("<w:p><w:r>{}</w:r></w:p>", shape.drawing());
let words = vec!["zz"; 80].join(" ");
let body = filler(64) + &p(&words);
let list = lay_out_with_header(&body, NORMAL, None, Some(&header));
let on_two: Vec<(usize, i64, i64)> = find_all(&list, 'z')
.into_iter()
.filter(|g| g.0 == 1)
.collect();
let first = on_two.iter().map(|g| g.2).min().unwrap();
let point = f_office_units::Length::from_points(1).unwrap().units();
let line: Vec<i64> = on_two
.iter()
.filter(|g| g.2 == first)
.map(|g| g.1)
.collect();
assert!(line.iter().any(|&x| x < 306 * point));
assert!(line.iter().any(|&x| x >= 450 * point));
assert!(
!line
.iter()
.any(|&x| (306 * point..450 * point).contains(&x))
);
}
const EXACT_18: &str = r#"<w:pPr><w:spacing w:line="360" w:lineRule="exact"/></w:pPr>"#;
fn body_with_a_box_at_the_page_top(lines: usize, wrap: &str) -> String {
let words = ["qqqq"; 12].join(" ");
let text = |text: &str| format!("<w:p>{EXACT_18}<w:r><w:t>{text}</w:t></w:r></w:p>");
let shape = AnchoredShape {
across: ("page", 3_886_200),
down: ("page", 914_400),
size: (2_971_800, 914_400),
wrap,
outline: None,
text: None,
};
let anchor = format!(
"<w:p>{EXACT_18}<w:r>{}</w:r><w:r><w:t>x</w:t></w:r></w:p>",
shape.drawing()
);
(0..lines).map(|_| text(&words)).collect::<String>() + &anchor + &text("y")
}
fn lines_on(list: &DisplayList, ch: char, page: usize) -> usize {
let mut lines: Vec<i64> = find_all(list, ch)
.iter()
.filter(|g| g.0 == page)
.map(|g| g.2)
.collect();
lines.sort_unstable();
lines.dedup();
lines.len()
}
#[test]
fn keeps_the_wrap_of_a_box_whose_anchor_stays_on_its_page() {
let pass = passes(&body_with_a_box_at_the_page_top(33, SQUARE_WRAP));
assert_eq!(lines_on(&pass.list, 'q', 0), 35);
assert_eq!((find(&pass.list, 'x').0, find(&pass.list, 'y').0), (0, 1));
assert_eq!(pass.wraps.pages().collect::<Vec<_>>(), [0]);
}
#[test]
fn moves_a_box_whose_wrap_pushes_its_anchor_off_the_page() {
for lines in [34, 35] {
let pass = passes(&body_with_a_box_at_the_page_top(lines, SQUARE_WRAP));
assert_eq!(lines_on(&pass.list, 'q', 0), lines);
let (x, y) = (find(&pass.list, 'x'), find(&pass.list, 'y'));
assert_eq!((x.0, y.0), (1, 1));
assert_eq!(y.2 - x.2, du(360));
assert!(y.1 < 306 * f_office_units::Length::UNITS_PER_POINT);
assert_eq!(pass.wraps.pages().collect::<Vec<_>>(), [1]);
}
}
#[test]
fn moves_a_box_with_top_and_bottom_wrap_that_pushes_its_anchor_off_the_page() {
const BAND: &str = "<wp:wrapTopAndBottom/>";
let pass = passes(&body_with_a_box_at_the_page_top(30, BAND));
let (below, x) = (find(&pass.list, 'q'), find(&pass.list, 'x'));
assert_eq!((x.0, x.2 - below.2), (0, du(30 * 360)));
let pass = passes(&body_with_a_box_at_the_page_top(34, BAND));
let top = find(&pass.list, 'q');
assert_eq!(below.2 - top.2, du(4 * 360));
let (x, y) = (find(&pass.list, 'x'), find(&pass.list, 'y'));
assert_eq!((x.0, y.0), (1, 1));
assert_eq!((x.2 - top.2, y.2 - top.2), (du(4 * 360), du(5 * 360)));
}
fn line_below_two_boxes(wrap: &str) -> i64 {
let shape = |down: i64, height: i64| {
AnchoredShape {
across: ("page", 0),
down: ("page", down),
size: (1_270_000, height),
wrap,
outline: None,
text: None,
}
.drawing()
};
let (lower, upper) = (shape(1_168_400, 254_000), shape(914_400, 228_600));
let body =
format!("<w:p>{EXACT_18}<w:r>{lower}</w:r><w:r>{upper}</w:r><w:r><w:t>x</w:t></w:r></w:p>");
find(&passes(&body).list, 'x').2
}
#[test]
fn moves_a_line_below_a_band_that_its_move_below_a_later_band_takes_it_into() {
let free = line_below_two_boxes("<wp:wrapNone/>");
let below = line_below_two_boxes("<wp:wrapTopAndBottom/>");
assert_eq!(below - free, du(40 * 20));
}
#[test]
fn takes_the_drawings_of_a_paragraph_to_the_page_of_its_first_line() {
let body = body_with_a_box_at_the_page_top(36, SQUARE_WRAP);
let first = pass_with_header(&body, NORMAL, None, None);
assert_eq!(first.wraps.pages().collect::<Vec<_>>(), [1]);
let pass = passes(&body);
assert_eq!(lines_on(&pass.list, 'q', 0), 36);
assert_eq!((find(&pass.list, 'x').0, find(&pass.list, 'y').0), (1, 1));
}
fn box_below_its_paragraph() -> String {
let shape = AnchoredShape {
across: ("page", 4_191_000),
down: ("paragraph", 304_800),
size: (1_905_000, 457_200),
wrap: "<wp:wrapNone/>",
outline: None,
text: Some(ShapeText {
blocks: "<w:p><w:r><w:t>b</w:t></w:r></w:p>",
body: r#"<wps:bodyPr lIns="0" tIns="0" rIns="0" bIns="0"/>"#,
}),
};
format!("<w:r>{}</w:r>", shape.drawing())
}
fn after_lines(fill: usize, runs: &str) -> String {
const FREE: &str =
r#"<w:pPr><w:widowControl w:val="0"/><w:spacing w:line="360" w:lineRule="exact"/></w:pPr>"#;
let line = |text: &str| format!("<w:p>{EXACT_18}<w:r><w:t>{text}</w:t></w:r></w:p>");
let filler: String = (0..fill).map(|_| line("q")).collect();
filler + &format!("<w:p>{FREE}{runs}</w:p>") + &line("y")
}
fn box_and_line(body: &str) -> ((usize, i64), (usize, i64)) {
let list = passes(body).list;
let ((b_page, _, b), (d_page, _, d)) = (find(&list, 'b'), find(&list, 'd'));
((b_page, b), (d_page, d))
}
#[test]
fn places_a_drawing_from_the_part_of_its_paragraph_on_the_page_of_its_anchor() {
let alone = box_and_line(&after_lines(
0,
&(box_below_its_paragraph() + "<w:r><w:t>d</w:t></w:r>"),
));
assert_eq!((alone.0.0, alone.1.0), (0, 0));
let runs = "<w:r><w:t>a</w:t><w:br/><w:t>c</w:t><w:br/></w:r>".to_owned()
+ &box_below_its_paragraph()
+ "<w:r><w:t>d</w:t></w:r>";
let ((b_page, b), (d_page, d)) = box_and_line(&after_lines(34, &runs));
assert_eq!((b_page, d_page), (1, 1));
assert_eq!((b, d), (alone.0.1, alone.1.1));
let runs =
box_below_its_paragraph() + "<w:r><w:t>a</w:t><w:br/><w:t>c</w:t><w:br/><w:t>d</w:t></w:r>";
let ((b_page, b), _) = box_and_line(&after_lines(34, &runs));
assert_eq!(b_page, 0);
assert_eq!(b - alone.0.1, du(34 * 360));
}
#[test]
fn keeps_a_leading_page_break_on_a_full_page_before_a_drawing_anchored_after_it() {
let alone = box_and_line(&after_lines(
0,
&(box_below_its_paragraph() + "<w:r><w:t>d</w:t></w:r>"),
));
let runs = r#"<w:r><w:br w:type="page"/></w:r>"#.to_owned()
+ &box_below_its_paragraph()
+ "<w:r><w:t>d</w:t></w:r>";
let ((b_page, b), (d_page, d)) = box_and_line(&after_lines(36, &runs));
assert_eq!((b_page, d_page), (1, 1));
assert_eq!((b, d), (alone.0.1, alone.1.1));
}
fn above_a_wide_box(lines: i64) -> String {
let line = 127_000;
let shape = AnchoredShape {
across: ("column", 0),
down: ("paragraph", line),
size: (5_943_600, lines * line),
wrap: SQUARE_WRAP,
outline: None,
text: None,
};
format!("<w:p><w:r>{}<w:t>d</w:t></w:r></w:p>", shape.drawing())
}
#[test]
fn tests_a_chain_below_a_drawing_that_leaves_no_room_beside_it() {
let chain = kept("e") + &kept("h") + &p("z");
let body = filler(40) + &above_a_wide_box(21) + &chain;
assert_eq!(pages(&body, NORMAL, "dehz"), [0, 1, 1, 1]);
let body = filler(40) + &above_a_wide_box(20) + &chain;
assert_eq!(pages(&body, NORMAL, "dehz"), [0, 0, 0, 0]);
}
fn blocked_by_a_wide_box(wrap: &str, spacing: &str, height: i64) -> String {
let shape = AnchoredShape {
across: ("page", 0),
down: ("paragraph", 0),
size: (7_772_400, height),
wrap,
outline: None,
text: None,
};
let x = format!(
"<w:p>{spacing}<w:r>{}</w:r><w:r><w:t>x</w:t></w:r></w:p>",
shape.drawing()
);
x + &format!("<w:p>{spacing}<w:r><w:t>y</w:t></w:r></w:p>")
}
const THROUGH: &str = r#"<wp:wrapThrough wrapText="bothSides"><wp:wrapPolygon edited="0"><wp:start x="0" y="0"/><wp:lineTo x="0" y="21600"/><wp:lineTo x="21600" y="21600"/><wp:lineTo x="21600" y="0"/><wp:lineTo x="0" y="0"/></wp:wrapPolygon></wp:wrapThrough>"#;
fn blocked_baselines(
(wrap, spacing, height): (&str, &str, i64),
blocked: BlockedLines,
) -> (i64, i64) {
let target = on_base_with(|p| p.with_variant(blocked));
let body = blocked_by_a_wide_box(wrap, spacing, height);
let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
(find(&pass.list, 'x').2, find(&pass.list, 'y').2)
}
#[test]
fn moves_a_line_without_room_beside_a_polygon_wrap_in_steps_of_its_height() {
let top = |wrap: &str, blocked: BlockedLines| {
let (x, y) = blocked_baselines((wrap, EXACT_18, 381_000), blocked);
assert_eq!(y - x, du(360));
x
};
let below = top(THROUGH, BlockedLines::BelowBox);
assert_eq!(top(THROUGH, BlockedLines::LineSteps) - below, du(120));
assert_eq!(top(SQUARE_WRAP, BlockedLines::LineSteps), below);
assert_eq!(top(SQUARE_WRAP, BlockedLines::BelowBox), below);
}
const THROUGH_OPEN_BOTTOM: &str = r#"<wp:wrapThrough wrapText="bothSides"><wp:wrapPolygon edited="1"><wp:start x="0" y="0"/><wp:lineTo x="0" y="10800"/><wp:lineTo x="10800" y="10800"/><wp:lineTo x="10800" y="21600"/><wp:lineTo x="21600" y="21600"/><wp:lineTo x="21600" y="0"/><wp:lineTo x="0" y="0"/></wp:wrapPolygon></wp:wrapThrough>"#;
fn stepped_baseline(wrap: &str, outline: PolygonWrap, end: StepEnd) -> i64 {
let target = on_base_with(|p| {
p.with_variant(BlockedLines::LineSteps)
.with_variant(outline)
.with_variant(end)
});
let body = blocked_by_a_wide_box(wrap, EXACT_18, 914_400);
let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
find(&pass.list, 'x').2
}
#[test]
fn moves_a_line_beside_a_polygon_outline_and_past_a_bottom_that_a_step_reaches() {
let at_bottom = stepped_baseline(THROUGH_OPEN_BOTTOM, PolygonWrap::Bounds, StepEnd::AtBottom);
let cases = [
(
THROUGH_OPEN_BOTTOM,
PolygonWrap::Bounds,
StepEnd::PastBottom,
360,
),
(
THROUGH_OPEN_BOTTOM,
PolygonWrap::Outline,
StepEnd::AtBottom,
-720,
),
(
THROUGH_OPEN_BOTTOM,
PolygonWrap::Outline,
StepEnd::PastBottom,
-720,
),
(THROUGH, PolygonWrap::Outline, StepEnd::AtBottom, 0),
(THROUGH, PolygonWrap::Outline, StepEnd::PastBottom, 360),
];
let moves: Vec<i64> = cases
.iter()
.map(|&(wrap, outline, end, _)| stepped_baseline(wrap, outline, end) - at_bottom)
.collect();
let expected: Vec<i64> = cases.iter().map(|case| du(case.3)).collect();
assert_eq!(moves, expected);
}
#[test]
fn moves_a_line_of_multiple_spacing_in_steps_of_its_single_line() {
const DOUBLE: &str = r#"<w:pPr><w:spacing w:line="480" w:lineRule="auto"/></w:pPr>"#;
let case = (THROUGH, DOUBLE, 304_800);
let (below, next) = blocked_baselines(case, BlockedLines::BelowBox);
assert_eq!(next - below, du(400));
let (x, _) = blocked_baselines(case, BlockedLines::LineSteps);
assert_eq!(x - below, du(120));
}
fn chart_without_series() -> Chart {
Chart {
id: String::new(),
columns: Some(Box::new(Columns::plain(
Vec::new(),
0,
crate::chart::ChartGroup::Columns {
gap: 150,
overlap: 0,
stacking: crate::chart::BarStacking::Clustered,
},
Some(Title {
lines: 0,
size: 28,
font: crate::props::FontRef::MinorLatin,
text: None,
corner: None,
}),
))),
}
}
#[test]
fn leaves_out_a_drawing_beyond_the_range_of_a_length_with_a_warning() {
let (styles, settings) = (Styles::default(), Settings::default());
let (theme, fonts) = (Theme::default(), FontTable::default());
let context = base_context(&styles, &settings);
let stories = context.for_stories();
let sections = Sections::one(SectionLayout::plain(&Section::default(), 0));
let mut layout = Layout::new(
(&context, &stories),
§ions,
Shaper::new(&Fake, &theme, &fonts, &ShaperSettings::base()),
);
let far = Length::from_units(i64::MAX);
let page = (units(1_000), units(1_000));
let shape = Floating {
part: Name::new("word/header1.xml"),
width: units(10),
height: units(10),
..Floating::default()
};
let chart = Floating {
chart: Some(chart_without_series()),
..shape.clone()
};
layout.show_floating((&shape, Size::of(&shape)), (far, Length::ZERO), page);
let items = layout.draw_sized((&chart, Size::of(&chart)), Length::ZERO, far);
assert_eq!(items.len(), 0);
let skipped = |drawing| Warning::DrawingSkipped {
drawing,
part: Name::new("word/header1.xml"),
reason: Reason::OutOfRange,
};
let warnings: Vec<(&Warning, u32)> = layout.output.warnings.iter().collect();
assert_eq!(
warnings,
[(&skipped(Drawing::Shape), 1), (&skipped(Drawing::Chart), 1)]
);
assert_eq!((layout.items.len(), layout.behind.len()), (0, 0));
}
#[test]
fn leaves_out_a_chart_that_its_layout_gives_no_place_with_a_warning() {
let (styles, settings) = (Styles::default(), Settings::default());
let (theme, fonts) = (Theme::default(), FontTable::default());
let context = base_context(&styles, &settings);
let stories = context.for_stories();
let sections = Sections::one(SectionLayout::plain(&Section::default(), 0));
let mut layout = Layout::new(
(&context, &stories),
§ions,
Shaper::new(&Fake, &theme, &fonts, &ShaperSettings::base()),
);
let chart = Floating {
part: Name::new("word/document.xml"),
width: units(100),
height: units(100),
chart: Some(chart_without_series()),
..Floating::default()
};
layout.draw_sized((&chart, Size::of(&chart)), Length::ZERO, Length::ZERO);
let warnings: Vec<(&Warning, u32)> = layout.output.warnings.iter().collect();
let skipped = Warning::DrawingSkipped {
drawing: Drawing::Chart,
part: Name::new("word/document.xml"),
reason: Reason::NotPlaced,
};
assert_eq!(warnings, [(&skipped, 1)]);
}
#[test]
fn places_a_drawing_from_the_top_of_its_anchors_line_on_a_target_that_counts_from_it() {
use crate::layout::testing::{on_base_with, pass_with};
use crate::numbering::Numbering;
use crate::quirks::FromLine;
use f_office_paint::{Item, PathCommand};
let shape = AnchoredShape {
across: ("margin", 0),
down: ("line", 0),
size: (254_000, 127_000),
wrap: "<wp:wrapNone/>",
outline: Some(12_700),
text: None,
}
.drawing();
let body = format!(
r#"<w:p><w:pPr><w:spacing w:line="240" w:lineRule="exact"/></w:pPr><w:r>{shape}<w:t>A</w:t><w:br/><w:t>B</w:t></w:r><w:r>{shape}</w:r><w:r><w:t>C</w:t></w:r></w:p>"#
);
let tops = |from: FromLine| -> Vec<Length> {
let target = on_base_with(|p| p.with_variant(from));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
let mut tops: Vec<Length> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Path(path) => path
.commands
.iter()
.flat_map(PathCommand::points)
.map(|point| point.y)
.min(),
_ => None,
})
.collect();
tops.sort();
tops
};
let [first, second] = tops(FromLine::ParagraphTop).try_into().unwrap();
assert_eq!(first, second);
let line = Length::from_twips(240).unwrap();
assert_eq!(tops(FromLine::LineTop), [first, first + line]);
}
fn box_line_x(twips: i64, outline: i64, rule: BoxOutlineLeft) -> i64 {
let shape = AnchoredShape {
across: ("page", twips * 635),
down: ("paragraph", 0),
size: (2_540_000, 508_000),
wrap: "<wp:wrapNone/>",
outline: Some(outline),
text: Some(ShapeText {
blocks: "<w:p><w:r><w:t>a</w:t></w:r></w:p>",
body: r#"<wps:bodyPr lIns="91440" tIns="45720" rIns="91440" bIns="45720"/>"#,
}),
};
let body = format!("<w:p><w:r>{}</w:r></w:p>", shape.drawing());
let target = on_base_with(|p| p.with_variant(rule));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
x_du(find(&list, 'a').1)
}
#[test]
fn cuts_half_a_text_box_outline_to_whole_twips_at_its_left_text_edge() {
let shifts = |outline: i64| -> Vec<i64> {
(2_000..2_012)
.map(|twips| {
box_line_x(twips, outline, BoxOutlineLeft::ExactHalf)
- box_line_x(twips, outline, BoxOutlineLeft::WholeTwipsHalf)
})
.collect()
};
let start = shifts(9_525);
assert!(
start.iter().all(|shift| (0..=1).contains(shift)),
"{start:?}"
);
assert!(start.contains(&1), "{start:?}");
assert!(shifts(12_700).iter().all(|&shift| shift == 0));
}
fn vml_box_line(twips: i64, line: &str, rule: VmlBoxOutline) -> (i64, i64) {
let body = format!(
r##"<w:p><w:r><w:pict><v:shape xmlns:v="urn:schemas-microsoft-com:vml" type="#_x0000_t202" style="position:absolute;margin-left:{}.{:02}pt;margin-top:100pt;width:200pt;height:40pt;mso-position-horizontal-relative:page;mso-position-vertical-relative:page" {line}><v:textbox><w:txbxContent><w:p><w:r><w:t>a</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></w:r></w:p>"##,
twips / 20,
twips % 20 * 5
);
let target = on_base_with(|p| p.with_variant(rule));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
let (_, x, baseline) = find(&list, 'a');
(x_du(x), baseline)
}
#[test]
fn keeps_the_text_of_a_vml_box_inside_its_outline_by_its_half_rounded_up_to_whole_twips() {
let shifts = |line: &str| -> Vec<(i64, i64)> {
(2_000..2_012)
.map(|twips| {
let (inside, middle) = (
vml_box_line(twips, line, VmlBoxOutline::InsideRoundedHalf),
vml_box_line(twips, line, VmlBoxOutline::Middle),
);
(inside.0 - middle.0, inside.1 - middle.1)
})
.collect()
};
let thin = shifts(r#"strokeweight="0.75pt""#);
assert!(thin.iter().all(|&(x, _)| (1..=2).contains(&x)), "{thin:?}");
assert!(thin.iter().any(|&(x, _)| x == 1), "{thin:?}");
assert!(thin.iter().any(|&(x, _)| x == 2), "{thin:?}");
assert!(thin.iter().all(|&(_, y)| y == 2), "{thin:?}");
assert!(
shifts(r#"stroked="f""#)
.iter()
.all(|&shift| shift == (0, 0))
);
}
fn vml_type_shift(shape_type: &str, attributes: &str) -> (i64, i64) {
let line = |kind: &str, attributes: &str| {
let body = format!(
r##"<w:p><w:r><w:pict><v:shapetype xmlns:v="urn:schemas-microsoft-com:vml" {shape_type}><v:shape xmlns:v="urn:schemas-microsoft-com:vml" type="#{kind}" {attributes} style="position:absolute;margin-left:100pt;margin-top:100pt;width:400pt;height:100pt;mso-position-horizontal-relative:page;mso-position-vertical-relative:page" stroked="f"><v:textbox><w:txbxContent><w:p><w:r><w:t>a</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></w:r></w:p>"##
);
let list = pass_with(&body, ("", None), None, Numbering::default(), on_base()).list;
let (_, x, baseline) = find(&list, 'a');
(x_du(x), baseline)
};
let (typed, plain) = (line("t", attributes), line("_x0000_t202", ""));
(typed.0 - plain.0, typed.1 - plain.1)
}
#[test]
fn lays_the_text_of_a_vml_shape_in_its_types_text_rectangle_on_the_box_less_its_insets() {
let (x, y) = vml_type_shift(
r#"id="t" coordsize="21600,21600"><v:path textboxrect="2160,1080,19440,20520"/></v:shapetype"#,
"",
);
assert!((160..=161).contains(&x), "{x}");
assert!((19..=20).contains(&y), "{y}");
let rounded = r#"id="t" coordsize="21600,21600" adj="5400"><v:formulas><v:f eqn="val #0"/><v:f eqn="prod @0 2929 10000"/><v:f eqn="sum width 0 @1"/><v:f eqn="sum height 0 @1"/></v:formulas><v:path limo="10800,10800" textboxrect="@1,@1,@2,@3"/></v:shapetype"#;
let (x, y) = vml_type_shift(rounded, r#"adj="2700""#);
assert!((14..=15).contains(&x), "{x}");
assert!((14..=15).contains(&y), "{y}");
let unread = r#"id="t"><v:formulas><v:f eqn="sin #0 #1"/></v:formulas><v:path textboxrect="@0,0,21600,21600"/></v:shapetype"#;
assert_eq!(vml_type_shift(unread, ""), (0, 0));
}
#[test]
fn places_a_header_drawing_from_the_body_area_on_a_target_that_asks() {
use crate::layout::testing::{base, shape_top};
use crate::quirks::StoryMarginArea;
let shape = AnchoredShape {
across: ("margin", 0),
down: ("margin", 0),
size: (914_400, 254_000),
wrap: "<wp:wrapNone/>",
outline: Some(12_700),
text: None,
};
let lines = r#"<w:p><w:pPr><w:spacing w:after="400"/></w:pPr><w:r><w:t>h</w:t><w:br/><w:t>i</w:t><w:br/><w:t>j</w:t><w:br/><w:t>k</w:t></w:r></w:p>"#;
let drawing = format!("<w:p><w:r>{}</w:r></w:p>", shape.drawing());
let lay_out = |area: StoryMarginArea, header: &str| {
let target = on_base_with(|p| p.with_variant(area));
pass_with(
&p("b"),
(NORMAL, None),
Some(header),
Numbering::default(),
target,
)
.list
};
let tall = format!("{drawing}{lines}");
let section = lay_out(StoryMarginArea::SectionMargins, &tall);
assert_eq!(find(§ion, 'b').2, base(2120));
let at_margin = shape_top(&lay_out(StoryMarginArea::SectionMargins, &drawing));
assert_eq!(
shape_top(&lay_out(StoryMarginArea::BodyArea, &drawing)),
at_margin
);
assert_eq!(shape_top(§ion), at_margin);
assert_eq!(
shape_top(&lay_out(StoryMarginArea::BodyArea, &tall)),
at_margin + (2120 - 1440) * Length::UNITS_PER_TWIP
);
}
fn box_top_and_line(
down: (&str, i64),
before: usize,
outline: i64,
rule: BoxTextTop,
) -> (i64, i64) {
let shape = AnchoredShape {
across: ("page", 2_000 * 635),
down: (down.0, down.1 * 635),
size: (2_540_000, 508_000),
wrap: "<wp:wrapNone/>",
outline: Some(outline),
text: Some(ShapeText {
blocks: "<w:p><w:r><w:t>a</w:t></w:r></w:p>",
body: r#"<wps:bodyPr lIns="91440" tIns="45720" rIns="91440" bIns="45720"/>"#,
}),
};
let spaced = r#"<w:p><w:pPr><w:spacing w:line="250" w:lineRule="auto"/></w:pPr><w:r><w:t>b</w:t></w:r></w:p>"#;
let body = format!(
"{}<w:p><w:r>{}</w:r></w:p>",
spaced.repeat(before),
shape.drawing()
);
let target = on_base_with(|p| p.with_variant(rule));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
(shape_top(&list), find(&list, 'a').2)
}
#[test]
fn starts_a_text_box_text_from_its_top_rounded_to_whole_twips() {
use BoxTextTop::{ExactTop, NearestTwipTop};
let twip = Length::UNITS_PER_TWIP;
let mut moved = 0;
for (before, offset) in (0..6).flat_map(|before| (100..112).map(move |offset| (before, offset)))
{
let (top, rounded) =
box_top_and_line(("paragraph", offset), before, 12_700, NearestTwipTop);
let nearest = (top + 10 * twip + twip / 2).div_euclid(twip);
let placed = box_top_and_line(("page", nearest), 0, 12_700, ExactTop).1;
assert_eq!(rounded, placed, "{before} {offset}");
let exact = box_top_and_line(("paragraph", offset), before, 12_700, ExactTop).1;
moved += i64::from(rounded != exact);
}
assert!(moved > 0);
let mut cut = 0;
for twips in 2_000..2_024 {
let line = |outline, rule| box_top_and_line(("page", twips), 0, outline, rule).1;
assert_eq!(
line(9_525, NearestTwipTop),
line(8_890, ExactTop),
"{twips}"
);
cut += i64::from(line(9_525, NearestTwipTop) != line(9_525, ExactTop));
}
assert!(cut > 0);
}
#[derive(Clone, Copy, Debug)]
enum BoxLines {
Single,
SingleRoundsUp,
SpacedAfter,
MultipleAbove,
}
fn last_line(twips: i64, lines: BoxLines, rule: BoxLastLine) -> i64 {
let sized = |size: i64, line: i64, after: i64, letter: char| {
format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="{after}" w:line="{line}" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="{size}"/></w:rPr><w:t>{letter}</w:t></w:r></w:p>"#
)
};
let paragraph = |line: i64, after: i64, letter: char| sized(25, line, after, letter);
let blocks = match lines {
BoxLines::Single => paragraph(240, 0, 'a'),
BoxLines::SingleRoundsUp => sized(20, 240, 0, 'a'),
BoxLines::SpacedAfter => paragraph(240, 120, 'a'),
BoxLines::MultipleAbove => paragraph(276, 0, 'b') + ¶graph(240, 0, 'a'),
};
let shape = AnchoredShape {
across: ("page", 2_000 * 635),
down: ("page", 2_000 * 635),
size: (2_540_000, (400 + twips) * 635),
wrap: "<wp:wrapNone/>",
outline: None,
text: Some(ShapeText {
blocks: &blocks,
body: r#"<wps:bodyPr lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t"><a:noAutofit/></wps:bodyPr>"#,
}),
};
let body = format!("<w:p><w:r>{}</w:r></w:p>", shape.drawing());
let target = on_base_with(|p| p.with_variant(rule));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
find(&list, 'a').2
}
#[test]
fn moves_a_text_box_last_line_down_by_the_room_below_it() {
use BoxLastLine::{RoomBelow, Spaced};
let moves: Vec<i64> = (0..24)
.map(|twips| {
last_line(twips, BoxLines::Single, RoomBelow)
- last_line(twips, BoxLines::Single, Spaced)
})
.collect();
let lower = moves.iter().filter(|&&dots| dots == 1).count();
assert!((1..24).contains(&lower), "{moves:?}");
assert!(
moves.iter().all(|&dots| dots == 0 || dots == 1),
"{moves:?}"
);
let moves: Vec<i64> = (0..24)
.map(|twips| {
last_line(twips, BoxLines::SingleRoundsUp, RoomBelow)
- last_line(twips, BoxLines::SingleRoundsUp, Spaced)
})
.collect();
let off_spacing = moves.iter().filter(|&&dots| dots != 0).count();
assert!((1..24).contains(&off_spacing), "{moves:?}");
assert!(moves.iter().all(|&dots| dots.abs() <= 1), "{moves:?}");
for lines in [BoxLines::SpacedAfter, BoxLines::MultipleAbove] {
for twips in 0..24 {
assert_eq!(
last_line(twips, lines, RoomBelow),
last_line(twips, lines, Spaced),
"{lines:?} {twips}"
);
}
}
}
const THROUGH_SLANTED_TOP: &str = r#"<wp:wrapThrough wrapText="bothSides"><wp:wrapPolygon edited="1"><wp:start x="0" y="4500"/><wp:lineTo x="0" y="21600"/><wp:lineTo x="21600" y="21600"/><wp:lineTo x="21600" y="3600"/><wp:lineTo x="0" y="4500"/></wp:wrapPolygon></wp:wrapThrough>"#;
fn wrap_band_baseline(wrap: &str, spacing: &str, band: LineWrapBand) -> i64 {
let target = on_base_with(|p| p.with_variant(PolygonWrap::Outline).with_variant(band));
let shape = AnchoredShape {
across: ("page", 0),
down: ("paragraph", 0),
size: (7_772_400, 914_400),
wrap,
outline: None,
text: None,
};
let body = format!(
"<w:p>{spacing}<w:r>{}</w:r><w:r><w:t>x</w:t></w:r></w:p>",
shape.drawing()
);
let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
find(&pass.list, 'x').2
}
#[test]
fn a_line_of_multiple_spacing_meets_an_outline_over_its_single_line() {
const DOUBLE: &str = r#"<w:pPr><w:spacing w:line="480" w:lineRule="auto"/></w:pPr>"#;
const EXACT_20: &str = r#"<w:pPr><w:spacing w:line="400" w:lineRule="exact"/></w:pPr>"#;
let moves = |wrap, spacing| {
wrap_band_baseline(wrap, spacing, LineWrapBand::WholeLine)
- wrap_band_baseline(wrap, spacing, LineWrapBand::SingleLine)
};
assert_eq!(
[
moves(THROUGH_SLANTED_TOP, DOUBLE),
moves(THROUGH_SLANTED_TOP, EXACT_20),
],
[du(1440), 0]
);
}