use f_office_paint::DisplayList;
use crate::layout::testing::{Fake, filler, find, find_all, p};
use crate::testing::{WordParts, docx};
use crate::tests::{HEADER_REFERENCE, package};
#[test]
fn gives_the_page_after_a_continuous_section_break_the_default_header() {
let end = format!("<w:p><w:pPr><w:sectPr>{HEADER_REFERENCE}</w:sectPr></w:pPr></w:p>");
let body = p("a") + &end + &filler(70);
let continuous = r#"<w:type w:val="continuous"/><w:titlePg/>"#;
let bytes = package(&body, &p("h"), continuous);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let list = crate::layout(
&package,
(&Fake, &crate::ArtPictures::new()),
&crate::Options::base(),
)
.unwrap()
.value;
let pages: Vec<usize> = find_all(&list, 'h').iter().map(|g| g.0).collect();
assert_eq!(pages, [0, 1]);
}
fn header_and_body_starts(width: i64, height: i64, indent: i64) -> Vec<(crate::Target, i64, i64)> {
let line = |ch: char| {
format!(r#"<w:p><w:pPr><w:ind w:left="{indent}"/></w:pPr><w:r><w:t>{ch}</w:t></w:r></w:p>"#)
};
let page = format!(
r#"{HEADER_REFERENCE}<w:pgSz w:w="{width}" w:h="{height}"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>"#
);
let bytes = package(&line('b'), &line('h'), &page);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
crate::Target::all()
.map(|target| {
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::base()
};
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
(target, find(&list, 'h').1, find(&list, 'b').1)
})
.collect()
}
#[test]
fn a_header_line_takes_the_writer_offset_of_its_page_as_a_body_line_does() {
let cases = [
(11_906, 16_838, 2_250..2_270),
(12_240, 15_840, 2_355..2_375),
]
.into_iter()
.flat_map(|(width, height, indents)| indents.map(move |indent| (width, height, indent)));
for (width, height, indent) in cases {
for (target, header, body) in header_and_body_starts(width, height, indent) {
assert_eq!(header, body, "{target} {width} {indent}");
}
}
}
#[test]
fn starts_a_footer_at_its_exact_top_so_that_it_ends_without_a_height_rest() {
use super::{REST_PARTS, footer_start};
use f_office_units::Length;
let units = Length::from_units;
let ends = [
(1_000, REST_PARTS / 4),
(1_000, -REST_PARTS / 2),
(1_000, 0),
];
assert_eq!(
ends.map(|(height, rest)| footer_start(units(9_000), (units(height), rest))),
[
(units(8_000), -REST_PARTS / 4),
(units(8_001), -REST_PARTS / 2),
(units(8_000), 0),
]
);
}
fn footnote_over_two_pages(props: &str) -> DisplayList {
let lines: Vec<String> = ('b'..='k').map(|ch| format!("<w:t>{ch}</w:t>")).collect();
let note = format!(
r#"<w:footnote w:id="1"><w:p><w:pPr>{props}</w:pPr><w:r>{}</w:r></w:p></w:footnote>"#,
lines.join("<w:br/>")
);
let reference =
r#"<w:p><w:r><w:t>a</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r></w:p>"#;
let body = filler(60) + reference + &filler(10);
let bytes = docx(&body)
.plain_styles()
.word_part("t0", "footnotes", "footnotes.xml", "footnotes", ¬e)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
crate::layout(
&package,
(&Fake, &crate::ArtPictures::new()),
&crate::Options::base(),
)
.unwrap()
.value
}
fn own_separator_gap(target: crate::Target, normal: &str) -> (i64, i64) {
let note = r#"<w:footnote w:id="1"><w:p><w:pPr><w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/><w:ind w:left="0"/></w:pPr><w:r><w:t>n</w:t></w:r></w:p></w:footnote>"#;
let reference =
r#"<w:p><w:r><w:t>a</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r></w:p>"#;
let styles = format!(
r#"<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:pPr>{normal}</w:pPr></w:style>"#
);
let bytes = docx(reference)
.word_part("t0", "footnotes", "footnotes.xml", "footnotes", note)
.word_part("t1", "styles", "styles.xml", "styles", &styles)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
configuration: crate::Configuration::of(target),
device_places: true,
..crate::Options::base()
};
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
let rule = list.pages()[0]
.items()
.iter()
.find_map(|item| match item {
f_office_paint::Item::Rect(r) => Some(r),
_ => None,
})
.unwrap();
let (_, _, baseline) = find(&list, 'n');
let (_, start, _) = find(&list, 'a');
(
baseline - rule.y.units() / crate::grid::DOT,
rule.x.units() - start,
)
}
#[test]
fn lays_out_words_own_separator_without_the_spacing_of_normal_where_the_target_drops_it() {
use crate::quirks::OwnSeparatorSpacing;
let spaced = r#"<w:spacing w:before="240" w:after="200" w:line="360" w:lineRule="auto"/><w:ind w:left="1440"/>"#;
let unspaced = r#"<w:ind w:left="1440"/>"#;
for variant in [OwnSeparatorSpacing::Inherited, OwnSeparatorSpacing::Plain] {
let target = target_where(|quirks| quirks.own_separator_spacing == variant);
let (spaced_gap, spaced_left) = own_separator_gap(target, spaced);
let (unspaced_gap, unspaced_left) = own_separator_gap(target, unspaced);
assert_eq!(spaced_left, unspaced_left, "{variant:?}");
assert!(
spaced_left.abs() < f_office_units::Length::UNITS_PER_TWIP,
"{variant:?}"
);
match variant {
OwnSeparatorSpacing::Inherited => assert!(spaced_gap > unspaced_gap, "{variant:?}"),
OwnSeparatorSpacing::Plain => assert_eq!(spaced_gap, unspaced_gap),
}
}
}
fn page_of_a_line_with_its_footnote(lead: usize, mode: u32) -> usize {
let note = r#"<w:footnote w:id="1"><w:p><w:r><w:t>n</w:t></w:r></w:p></w:footnote>"#;
let reference = r#"<w:p><w:pPr><w:spacing w:line="480" w:lineRule="auto"/></w:pPr><w:r><w:t>a</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r></w:p>"#;
let settings = format!(
r#"<w:compat><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="{mode}"/></w:compat>"#
);
let bytes = docx(&(filler(lead) + reference))
.plain_styles()
.word_part("t0", "footnotes", "footnotes.xml", "footnotes", note)
.word_part("t1", "settings", "settings.xml", "settings", &settings)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let list = crate::layout(
&package,
(&Fake, &crate::ArtPictures::new()),
&crate::Options::base(),
)
.unwrap()
.value;
find(&list, 'a').0
}
#[test]
fn needs_the_whole_height_of_a_line_above_its_own_footnote_in_every_mode() {
let pages: Vec<(usize, usize, usize)> = (56..64)
.map(|lead| {
let at = |mode| page_of_a_line_with_its_footnote(lead, mode);
(at(15), at(14), at(12))
})
.collect();
assert!(pages.first().is_some_and(|p| p.0 == 0), "{pages:?}");
assert!(pages.last().is_some_and(|p| p.0 == 1), "{pages:?}");
assert!(pages.iter().all(|&(a, b, c)| a == b && b == c), "{pages:?}");
}
#[test]
fn gives_a_footnote_with_a_custom_mark_no_number() {
let notes = concat!(
r#"<w:footnote w:id="1"><w:p><w:r><w:t>*x</w:t></w:r></w:p></w:footnote>"#,
r#"<w:footnote w:id="2"><w:p><w:r><w:footnoteRef/></w:r><w:r><w:t>y</w:t></w:r></w:p></w:footnote>"#,
);
let body = concat!(
r#"<w:p><w:r><w:t>a</w:t></w:r><w:r><w:footnoteReference w:customMarkFollows="1" w:id="1"/><w:t>*</w:t></w:r></w:p>"#,
r#"<w:p><w:r><w:t>b</w:t></w:r><w:r><w:footnoteReference w:id="2"/></w:r></w:p>"#,
);
let bytes = docx(body)
.plain_styles()
.word_part("t0", "footnotes", "footnotes.xml", "footnotes", notes)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let list = crate::layout(
&package,
(&Fake, &crate::ArtPictures::new()),
&crate::Options::base(),
)
.unwrap()
.value;
assert_eq!(find_all(&list, '*').len(), 2);
assert_eq!(find_all(&list, '1').len(), 2);
assert_eq!(find_all(&list, '2'), []);
}
#[test]
fn cuts_a_shaded_footnote_over_two_pages_at_the_items_of_its_lines() {
let plain = footnote_over_two_pages("");
let shaded =
footnote_over_two_pages(r#"<w:shd w:val="clear" w:color="auto" w:fill="FF0000"/>"#);
let glyphs = |list: &DisplayList| -> Vec<Vec<(usize, i64, i64)>> {
('b'..='k').map(|ch| find_all(list, ch)).collect()
};
let pages: Vec<usize> = glyphs(&plain)
.iter()
.flatten()
.map(|glyph| glyph.0)
.collect();
assert!(pages.contains(&0) && pages.contains(&1), "{pages:?}");
assert_eq!(glyphs(&shaded), glyphs(&plain));
}
fn page_border_rects(borders: &str, target: crate::Target) -> Vec<([i64; 4], u8)> {
let section = format!(
r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:pgBorders w:offsetFrom="page">{borders}</w:pgBorders>"#
);
let bytes = package(&p("a"), &p("h"), §ion);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::base()
};
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
let hundredths = |length: f_office_units::Length| {
length.units() * 100 / f_office_units::Length::UNITS_PER_POINT
};
list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Rect(r) => {
Some(([r.x, r.y, r.width, r.height].map(hundredths), r.color.r))
}
_ => None,
})
.collect()
}
fn target_where(test: impl Fn(&crate::quirks::Quirks) -> bool) -> crate::Target {
crate::Target::all()
.find(|&target| test(&crate::profile::Profile::of(target).unwrap().quirks()))
.unwrap()
}
fn page_sides(style: &str, eighths: u32) -> String {
["top", "left", "bottom", "right"]
.map(|side| {
format!(r#"<w:{side} w:val="{style}" w:sz="{eighths}" w:space="24" w:color="000000"/>"#)
})
.concat()
}
fn band_rects(style: &str) -> Vec<([i64; 4], u8)> {
let bands = target_where(|quirks| {
quirks.three_d_borders == crate::quirks::ThreeDBorders::Bands
&& quirks.page_border_lines == crate::quirks::PageBorderLines::Down
});
page_border_rects(&page_sides(style, 18), bands)
}
#[test]
fn draws_a_three_d_page_border_as_bands_across_the_top() {
let rects = band_rects("threeDEmboss");
assert!(rects.contains(&([2400, 2400, 56400, 72], 192)));
assert!(rects.contains(&([2472, 2472, 56256, 216], 96)));
assert!(rects.contains(&([2688, 2688, 55824, 72], 0)));
}
#[test]
fn nests_the_side_bands_of_a_three_d_page_border_below_the_top_bands() {
let rects = band_rects("threeDEmboss");
assert!(rects.contains(&([2400, 2472, 72, 74256], 192)));
assert!(rects.contains(&([2472, 2688, 216, 73824], 96)));
assert!(rects.contains(&([2688, 2760, 72, 73680], 0)));
}
fn nesting_target() -> crate::Target {
target_where(|quirks| {
quirks.page_border_corners == crate::quirks::PageBorderCorners::Nested
&& quirks.page_border_lines == crate::quirks::PageBorderLines::Down
})
}
#[test]
fn nests_the_lines_of_two_sides_of_the_same_lines_at_a_corner() {
let rects = page_border_rects(&page_sides("thinThickSmallGap", 24), nesting_target());
for rect in [
[2400, 2400, 56400, 300],
[2772, 2772, 55884, 72],
[2400, 2700, 300, 74028],
[2772, 2844, 72, 73512],
] {
assert!(rects.contains(&(rect, 0)), "{rect:?} in {rects:?}");
}
}
#[test]
fn runs_the_top_and_the_bottom_over_a_side_of_other_lines() {
let sides = ["top", "left", "bottom", "right"]
.map(|side| {
let style = if side == "top" || side == "bottom" {
"double"
} else {
"triple"
};
format!(r#"<w:{side} w:val="{style}" w:sz="24" w:space="24" w:color="000000"/>"#)
})
.concat();
let rects = page_border_rects(&sides, nesting_target());
for rect in [
[2400, 2400, 56400, 300],
[2400, 3000, 56400, 300],
[2400, 3300, 300, 72600],
[3000, 3300, 300, 72600],
[3600, 3300, 300, 72600],
] {
assert!(rects.contains(&(rect, 0)), "{rect:?} in {rects:?}");
}
let crossing = target_where(|quirks| {
quirks.page_border_corners == crate::quirks::PageBorderCorners::Crossed
});
let crossed = page_border_rects(&sides, crossing);
assert!(
crossed
.iter()
.any(|([x, y, _, h], _)| *x == 2400 && *y == 2400 && *h > 70000),
"{crossed:?}"
);
}
#[test]
fn turns_the_bands_of_an_engraved_page_border_and_draws_one_line_where_3d_is_one_line() {
assert!(band_rects("threeDEngrave").contains(&([2400, 2400, 56400, 72], 0)));
let one_line = target_where(|quirks| {
quirks.three_d_borders == crate::quirks::ThreeDBorders::OneLine
&& quirks.page_border_lines == crate::quirks::PageBorderLines::Nearest
});
let base = page_border_rects(&page_sides("threeDEmboss", 18), one_line);
assert!(base.contains(&([2400, 2400, 56400, 216], 0)));
assert_eq!(base.len(), 4);
}
#[test]
fn rounds_a_page_border_line_down_to_whole_dots() {
let down =
target_where(|quirks| quirks.page_border_lines == crate::quirks::PageBorderLines::Down);
let rects = page_border_rects(&page_sides("single", 12), down);
assert!(rects.contains(&([2400, 2400, 56400, 144], 0)));
let inset = page_border_rects(&page_sides("inset", 12), down);
assert_eq!(inset, rects);
}
#[test]
fn stacks_the_parts_of_a_page_border_in_whole_dots_from_its_outer_edges() {
let down =
target_where(|quirks| quirks.page_border_lines == crate::quirks::PageBorderLines::Down);
let rects = page_border_rects(&page_sides("thinThickThinMediumGap", 24), down);
let across: Vec<[i64; 2]> = rects
.iter()
.filter(|(rect, red)| rect[2] > 28200 && *red == 0)
.map(|(rect, _)| [rect[1], rect[3]])
.collect();
assert_eq!(across.len(), 6);
assert!(across.contains(&[2400, 144]));
assert!(across.contains(&[2688, 300]));
assert!(across.contains(&[3132, 144]));
let bottom = across.iter().map(|rect| rect[0] + rect[1]).max().unwrap();
assert!(across.contains(&[bottom - 144, 144]));
assert!(across.contains(&[bottom - 588, 300]));
assert!(across.contains(&[bottom - 876, 144]));
}
#[test]
fn paints_the_gaps_of_a_page_border_white_from_the_outer_edge_in() {
let gaps = |test: &dyn Fn(&crate::quirks::Quirks) -> bool| {
page_border_rects(
&page_sides("thinThickThinMediumGap", 24),
target_where(test),
)
.into_iter()
.filter(|(rect, _)| rect[2] > 28200)
.map(|(rect, red)| [rect[1], rect[3], i64::from(red)])
.collect::<Vec<_>>()
};
let white = gaps(&|quirks| {
quirks.page_border_gaps == crate::quirks::PageBorderGaps::White
&& quirks.page_border_lines == crate::quirks::PageBorderLines::Down
});
let top = [
[2400, 144, 0],
[2544, 144, 255],
[2688, 300, 0],
[2988, 144, 255],
[3132, 144, 0],
];
assert_eq!(white[..5], top, "{white:?}");
let bottom = white[5][0] + white[5][1];
let outer_in = [
[bottom - 144, 144, 0],
[bottom - 288, 144, 255],
[bottom - 588, 300, 0],
[bottom - 732, 144, 255],
[bottom - 876, 144, 0],
];
assert_eq!(white[5..], outer_in, "{white:?}");
let empty = gaps(&|quirks| quirks.page_border_gaps == crate::quirks::PageBorderGaps::Empty);
assert_eq!(empty.len(), 6, "{empty:?}");
assert!(empty.iter().all(|rect| rect[2] == 0), "{empty:?}");
}
fn centered_shape_top(in_header: bool, wrap: &str, with_note: bool, area: &str) -> i64 {
use crate::layout::testing::{AnchoredShape, shape_top};
use crate::testing::Replaced as _;
let shape = AnchoredShape {
across: ("margin", 0),
down: ("margin", 0),
size: (914_400, 254_000),
wrap,
outline: Some(12_700),
text: None,
};
let drawing = shape.drawing().replaced(
"<wp:posOffset>0</wp:posOffset></wp:positionV>",
"<wp:align>center</wp:align></wp:positionV>",
);
let drawing = format!("<w:p><w:r>{drawing}</w:r></w:p>");
let reference = if with_note {
r#"<w:r><w:footnoteReference w:id="1"/></w:r>"#
} else {
""
};
let text = format!("<w:p><w:r><w:t>a</w:t></w:r>{reference}</w:p>");
let mut parts = if in_header {
docx(&format!("{text}<w:sectPr>{HEADER_REFERENCE}</w:sectPr>"))
.word_part("h1", "header", "h1.xml", "hdr", &drawing)
} else {
docx(&format!("{drawing}{text}"))
}
.plain_styles();
if with_note {
let lines: Vec<String> = ('b'..='i').map(|ch| format!("<w:t>{ch}</w:t>")).collect();
let note = format!(
r#"<w:footnote w:id="1"><w:p><w:r>{}</w:r></w:p></w:footnote>"#,
lines.join("<w:br/>")
);
parts = parts.word_part("t0", "footnotes", "footnotes.xml", "footnotes", ¬e);
}
let bytes = parts.zip().unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
overrides: crate::profile::Overrides::default().with_quirk("StoryMarginArea", area),
..crate::Options::base()
};
shape_top(
&crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value,
)
}
#[test]
fn keeps_a_header_drawing_without_wrapping_above_the_footnotes_where_the_body_area_counts() {
use crate::layout::testing::SQUARE_WRAP;
let top =
|wrap: &str, with_note: bool, area: &str| centered_shape_top(true, wrap, with_note, area);
let unwrapped = "<wp:wrapNone/>";
for area in ["SectionMargins", "BodyArea"] {
assert_eq!(top(SQUARE_WRAP, true, area), top(SQUARE_WRAP, false, area));
}
assert_eq!(
top(unwrapped, true, "SectionMargins"),
top(unwrapped, false, "SectionMargins")
);
assert!(top(unwrapped, true, "BodyArea") < top(unwrapped, false, "BodyArea"));
}
#[test]
fn keeps_a_body_drawing_above_the_footnotes_found_on_its_page_where_the_body_area_counts() {
use crate::layout::testing::SQUARE_WRAP;
let top =
|wrap: &str, with_note: bool, area: &str| centered_shape_top(false, wrap, with_note, area);
let unwrapped = "<wp:wrapNone/>";
for wrap in [unwrapped, SQUARE_WRAP] {
assert_eq!(
top(wrap, true, "SectionMargins"),
top(wrap, false, "SectionMargins")
);
assert!(top(wrap, true, "BodyArea") < top(wrap, false, "BodyArea"));
}
assert_eq!(
top(unwrapped, true, "BodyArea"),
top(SQUARE_WRAP, true, "BodyArea")
);
}
fn aligned_line(align: &str, note: bool, variant: &str) -> i64 {
let footnote = r#"<w:footnote w:id="1"><w:p><w:r><w:t>n</w:t></w:r></w:p></w:footnote>"#;
let reference = if note {
r#"<w:r><w:footnoteReference w:id="1"/></w:r>"#
} else {
""
};
let body = format!(
r#"<w:p><w:r><w:t>a</w:t></w:r>{reference}</w:p><w:sectPr><w:vAlign w:val="{align}"/></w:sectPr>"#
);
let bytes = docx(&body)
.plain_styles()
.word_part("t0", "footnotes", "footnotes.xml", "footnotes", footnote)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
overrides: crate::profile::Overrides::default().with_quirk("AlignmentWithNotes", variant),
..crate::Options::base()
};
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
find(&list, 'a').2
}
#[test]
fn keeps_a_centered_page_with_a_footnote_at_the_top_where_the_target_does() {
let top = aligned_line("top", false, "AlignsPage");
for variant in ["AlignsPage", "TopWithNotes"] {
assert!(aligned_line("center", false, variant) > top, "{variant}");
assert!(aligned_line("bottom", false, variant) > top, "{variant}");
}
let top_with_note = aligned_line("top", true, "AlignsPage");
assert_eq!(top_with_note, top);
for align in ["center", "bottom"] {
assert!(aligned_line(align, true, "AlignsPage") > top, "{align}");
assert_eq!(aligned_line(align, true, "TopWithNotes"), top, "{align}");
}
}
type PathBox = ([i64; 4], f_office_paint::Rgb);
type GroupBox = ([i64; 4], Vec<PathBox>);
type StrokedBox = (i64, Vec<PathBox>);
struct ArtPage {
paths: Vec<PathBox>,
groups: Vec<GroupBox>,
stroked: Vec<StrokedBox>,
warnings: Vec<(&'static str, u32)>,
}
fn hundredths_box(commands: &[f_office_paint::PathCommand]) -> Option<[i64; 4]> {
let hundredths = |length: f_office_units::Length| {
length.units() * 100 / f_office_units::Length::UNITS_PER_POINT
};
let mut points = commands
.iter()
.copied()
.flat_map(|mut command| command.points_mut().map(|point| *point).collect::<Vec<_>>());
let first = points.next()?;
let mut bounds = [first.x, first.y, first.x, first.y];
for point in points {
bounds = [
bounds[0].min(point.x),
bounds[1].min(point.y),
bounds[2].max(point.x),
bounds[3].max(point.y),
];
}
Some(bounds.map(hundredths))
}
fn path_boxes(items: &[f_office_paint::Item]) -> Vec<PathBox> {
items
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Path(path) => Some((hundredths_box(&path.commands)?, path.color)),
_ => None,
})
.collect()
}
fn art_border_paths(
pictures: &crate::ArtPictures,
art: crate::quirks::ArtPageBorders,
color: &str,
) -> ArtPage {
let sides = ["top", "left", "bottom", "right"]
.map(|side| {
format!(r#"<w:{side} w:val="apples" w:sz="16" w:space="24" w:color="{color}"/>"#)
})
.concat();
let section = format!(
r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:pgBorders w:offsetFrom="page">{sides}</w:pgBorders>"#
);
let bytes = package(&p("a"), &p("h"), §ion);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
configuration: crate::Configuration::of(target_where(|quirks| {
quirks.art_page_borders == art
})),
..crate::Options::base()
};
let laid_out = crate::layout(&package, (&Fake, pictures), &options).unwrap();
let border: Vec<&[f_office_paint::Item]> = laid_out.value.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Group(group)
if group.places == f_office_paint::GroupPlaces::Points && group.clip.is_none() =>
{
Some(group.items.as_slice())
}
_ => None,
})
.collect();
assert!(border.len() <= 1, "one group for the border");
let items = border.first().copied().unwrap_or_default();
let paths = path_boxes(items);
let groups = items
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Group(group) => Some((
hundredths_box(&group.clip.as_ref()?.commands)?,
path_boxes(&group.items),
)),
_ => None,
})
.collect();
let hundredths = |length: f_office_units::Length| {
length.units() * 100 / f_office_units::Length::UNITS_PER_POINT
};
let stroked = items
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Group(group) => match group.places {
f_office_paint::GroupPlaces::Stroked { width } => Some((
width.units(),
group
.items
.iter()
.filter_map(|item| match item {
f_office_paint::Item::Rect(rect) => Some((
[
hundredths(rect.x),
hundredths(rect.y),
hundredths(rect.x + rect.width),
hundredths(rect.y + rect.height),
],
rect.color,
)),
_ => None,
})
.collect(),
)),
_ => None,
},
_ => None,
})
.collect();
let mut warnings: Vec<(&'static str, u32)> = laid_out
.warnings
.iter()
.map(|(warning, count)| (warning.kind(), count))
.collect();
warnings.sort_unstable();
ArtPage {
paths,
groups,
stroked,
warnings,
}
}
fn apples_pictures() -> crate::ArtPictures {
let text = "f-office art pictures 3\n\
style apples 16\n\
part top repeat\n\
shape 0 0 0 evenodd\n\
ring 0 0 L 1000000 0 L 1000000 1000000 L 0 1000000\n\
part left span\n\
shape 0 0 0 nonzero\n\
ring 0 0 L 1000000 0 L 1000000 1000000\n\
part top-left corner\n\
shape 255 0 0 nonzero\n\
ring 0 0 Q 500000 0 1000000 1000000 C 0 1000000 0 500000 0 250000\n";
let mut pictures = crate::ArtPictures::new();
pictures.add(text.as_bytes()).unwrap();
pictures
}
const ART_BLUE: f_office_paint::Rgb = f_office_paint::Rgb { r: 0, g: 0, b: 255 };
fn top_boxes(page: &ArtPage) -> Vec<[i64; 4]> {
page.paths
.iter()
.filter(|(bounds, color)| *color == ART_BLUE && bounds[1] == 2400 && bounds[3] == 3996)
.map(|(bounds, _)| *bounds)
.collect()
}
#[test]
fn lays_the_pictures_of_an_art_side_between_corner_squares_of_whole_dots() {
let page = art_border_paths(
&apples_pictures(),
crate::quirks::ArtPageBorders::Pictures,
"0000FF",
);
assert_eq!(page.warnings, []);
let tops = top_boxes(&page);
assert_eq!(tops.len(), 33);
assert_eq!(
[tops.first(), tops.get(20), tops.get(21), tops.last()],
[
Some(&[3996, 2400, 5604, 3996]),
Some(&[36156, 2400, 37764, 3996]),
Some(&[37764, 2400, 39384, 3996]),
Some(&[55584, 2400, 57204, 3996]),
]
);
}
#[test]
fn spans_an_art_side_from_corner_to_corner_and_keeps_the_colors_of_its_corner() {
let page = art_border_paths(
&apples_pictures(),
crate::quirks::ArtPageBorders::Pictures,
"0000FF",
);
let red = f_office_paint::Rgb { r: 255, g: 0, b: 0 };
assert!(page.paths.contains(&([2400, 3996, 3996, 75204], ART_BLUE)));
assert!(page.paths.contains(&([2400, 2400, 3996, 3996], red)));
assert_eq!(page.paths.len(), 35);
}
#[test]
fn draws_the_shapes_of_a_clip_as_a_group_whose_clip_is_stretched_to_the_square() {
let text = "f-office art pictures 3\n\
style apples 16\n\
part top-left corner\n\
clip evenodd\n\
ring 0 0 L 500000 0 L 500000 500000 L 0 500000\n\
shape 0 0 0 nonzero\n\
ring 0 0 L 1000000 0 L 1000000 1000000\n\
end\n\
shape 255 0 0 nonzero\n\
ring 0 0 L 1000000 1000000 L 0 1000000\n";
let mut pictures = crate::ArtPictures::new();
pictures.add(text.as_bytes()).unwrap();
let page = art_border_paths(&pictures, crate::quirks::ArtPageBorders::Pictures, "0000FF");
assert_eq!(
page.groups,
[(
[2400, 2400, 3198, 3198],
vec![([2400, 2400, 3996, 3996], ART_BLUE)]
)]
);
let red = f_office_paint::Rgb { r: 255, g: 0, b: 0 };
assert_eq!(page.paths, [([2400, 2400, 3996, 3996], red)]);
}
#[test]
fn draws_the_rectangles_of_a_picture_as_stroked_rectangles_of_their_width() {
let text = "f-office art pictures 3\n\
style apples 16\n\
part top repeat\n\
rect 0 0 0 250000 250000 750000 750000 140\n\
part top-left corner\n\
rect 255 0 0 0 0 500000 500000 280\n";
let mut pictures = crate::ArtPictures::new();
pictures.add(text.as_bytes()).unwrap();
let page = art_border_paths(&pictures, crate::quirks::ArtPageBorders::Pictures, "0000FF");
assert_eq!(page.paths, []);
let widths: Vec<_> = page
.stroked
.iter()
.map(|(width, rects)| (*width, rects.len()))
.collect();
assert_eq!(widths, [(3_556, 33), (7_112, 1)]);
assert_eq!(
page.stroked[0].1.first(),
Some(&([4398, 2799, 5202, 3597], ART_BLUE))
);
let red = f_office_paint::Rgb { r: 255, g: 0, b: 0 };
assert_eq!(page.stroked[1].1, [([2400, 2400, 3198, 3198], red)]);
}
#[test]
fn draws_no_art_side_without_its_picture_or_on_a_target_that_does_not_draw_art() {
use crate::quirks::ArtPageBorders;
let page = art_border_paths(&crate::ArtPictures::new(), ArtPageBorders::Pictures, "auto");
assert_eq!(
(page.paths, page.warnings),
(vec![], vec![("art-border-not-drawn", 1)])
);
let page = art_border_paths(&apples_pictures(), ArtPageBorders::NotDrawn, "auto");
assert_eq!(page.paths, []);
assert!(
page.warnings
.iter()
.any(|(kind, _)| *kind == "value-not-supported"),
"{:?}",
page.warnings
);
}