use f_office_paint::{Item, Rect};
use f_office_units::Length;
use super::Direction;
use crate::layout::Layout;
use crate::layout::testing::with_layout;
use crate::props::{Border, BorderParts, Color};
fn border() -> Border {
let width = Length::from_eighth_points(4).expect("a whole number of units");
Border::new(BorderParts::single(width), Length::ZERO, Color::Auto)
}
const LINE: [i64; 4] = [1000, 2000, 30_000, 100];
fn draw(layout: &mut Layout<'_>) {
let [x, y, width, height] = LINE.map(Length::from_units);
let at = f_office_paint::Point { x, y };
layout.push_line(border(), super::line_rect(border(), at, width, height));
}
fn line() -> Item {
let [x, y, width, height] = LINE.map(Length::from_units);
Item::Rect(Rect {
x,
y,
width,
height,
color: border().color.drawn(),
})
}
fn lines(layout: &Layout<'_>) -> usize {
layout
.items
.to_vec()
.iter()
.filter(|item| **item == line())
.count()
}
#[test]
fn draws_a_border_line_once_where_two_passes_give_it() {
with_layout(|layout| {
draw(layout);
draw(layout);
assert_eq!(layout.items.to_vec(), [line()]);
});
}
#[test]
fn draws_a_border_line_that_another_item_before_it_equals() {
with_layout(|layout| {
layout.items.push(line());
draw(layout);
assert_eq!(lines(layout), 2);
});
}
#[test]
fn draws_a_border_line_again_after_a_cut_trial() {
with_layout(|layout| {
draw(layout);
layout.items.cut(0);
draw(layout);
assert_eq!(layout.items.to_vec(), [line()]);
layout.items.cut(0);
layout.items.push(line());
draw(layout);
assert_eq!(lines(layout), 2);
});
}
#[test]
fn keeps_a_border_line_once_when_a_fill_goes_in_before_it() {
with_layout(|layout| {
let entry = layout.items.open_fills();
draw(layout);
let [x, y, width, _] = LINE.map(Length::from_units);
let fill = Item::Rect(Rect {
x,
y,
width,
height: Length::from_units(1000),
color: f_office_paint::Rgb::BLACK,
});
layout.items.put_fills(entry, vec![fill]);
draw(layout);
assert_eq!(lines(layout), 1);
assert_eq!(layout.items.count(), 2);
});
}
#[test]
fn starts_the_list_afresh_with_the_items_of_a_new_page() {
with_layout(|layout| {
draw(layout);
assert_eq!(layout.items.take(), [line()]);
layout.items.push(line());
draw(layout);
assert_eq!(lines(layout), 2);
});
}
fn dash_spans(
dash: crate::props::DashStyle,
end: i64,
width: i64,
direction: Direction,
) -> Vec<(i64, i64)> {
let u = Length::from_units;
let piece = if direction == Direction::Across {
f_office_units::Edges {
left: u(0),
top: u(50),
right: u(end),
bottom: u(50 + width),
}
} else {
f_office_units::Edges {
left: u(50),
top: u(0),
right: u(50 + width),
bottom: u(end),
}
};
super::dashes(piece, dash, direction)
.into_iter()
.map(|b| {
if direction == Direction::Across {
(b.left.units(), b.right.units())
} else {
(b.top.units(), b.bottom.units())
}
})
.collect()
}
#[test]
fn dashes_a_piece_from_its_start_and_cuts_the_last_dash_at_its_end() {
use crate::props::DashStyle;
assert_eq!(
dash_spans(DashStyle::Dotted, 10, 2, Direction::Across),
[(0, 2), (4, 6), (8, 10)]
);
assert_eq!(
dash_spans(DashStyle::Dotted, 9, 2, Direction::Across),
[(0, 2), (4, 6), (8, 9)]
);
assert_eq!(
dash_spans(DashStyle::Dashed, 9, 1, Direction::Down),
[(0, 3), (4, 7), (8, 9)]
);
assert_eq!(
dash_spans(DashStyle::DotDash, 14, 1, Direction::Across),
[(0, 3), (4, 5), (6, 9), (10, 11), (12, 14)]
);
assert_eq!(
dash_spans(DashStyle::DotDotDash, 13, 1, Direction::Down),
[(0, 3), (4, 5), (6, 7), (8, 11), (12, 13)]
);
assert_eq!(
dash_spans(DashStyle::Dotted, 2, 2, Direction::Across),
[(0, 2)]
);
}
#[test]
fn leaves_the_lines_of_a_row_whole_with_a_warning_when_one_is_no_rectangle_that_reads() {
with_layout(|layout| {
draw(layout);
let other = Item::Group(f_office_paint::Group {
items: Vec::new(),
clip: None,
places: f_office_paint::GroupPlaces::Own,
});
layout.items.push(other.clone());
let part = f_office_warnings::Name::new(crate::testing::DOCUMENT);
assert_eq!(layout.split_at_junctions(0, &[], &part), []);
assert_eq!(layout.items.to_vec(), [line(), other]);
let limits: Vec<_> = layout
.output
.warnings
.iter()
.map(|(warning, count)| (warning.clone(), count))
.collect();
assert_eq!(
limits,
[(
crate::Warning::LimitReached {
part,
limit: crate::warnings::Limit::LineRange,
},
1
)]
);
});
}
#[test]
fn gives_the_inner_and_the_outer_edge_of_each_side_line() {
use super::{CellSide, LineEdge, LinePart, SideLines};
let part = |start, thick| LinePart {
start: Length::from_units(start),
thick: Length::from_units(thick),
};
let sides = SideLines {
left: vec![part(0, 2), part(4, 1)],
right: vec![part(10, 3)],
..SideLines::default()
};
let edge = |side, edge| sides.edge(side, edge).map(Length::units);
assert_eq!(edge(CellSide::Left, LineEdge::Outer), Some(0));
assert_eq!(edge(CellSide::Left, LineEdge::Inner), Some(5));
assert_eq!(edge(CellSide::Right, LineEdge::Inner), Some(10));
assert_eq!(edge(CellSide::Right, LineEdge::Outer), Some(13));
let none = SideLines::default();
assert_eq!(none.edge(CellSide::Left, LineEdge::Inner), None);
}
#[test]
fn the_squares_near_a_line_hold_every_square_on_it() {
use f_office_paint::Rgb;
use f_office_units::Edges;
use super::{Junction, SquareOrder};
let u = Length::from_units;
let square = |left, top, size| Junction {
edges: Edges {
left: u(left),
top: u(top),
right: u(left + size),
bottom: u(top + size),
},
color: Rgb::default(),
count: 1,
};
let squares = [
square(0, 0, 4),
square(10, 2, 1),
square(20, 9, 2),
square(30, 20, 1),
];
let order = SquareOrder::of(&squares);
for (line, direction) in [
(
Edges {
left: u(0),
top: u(2),
right: u(40),
bottom: u(10),
},
Direction::Across,
),
(
Edges {
left: u(9),
top: u(0),
right: u(21),
bottom: u(30),
},
Direction::Down,
),
] {
let near = order.near(line, direction);
let on: Vec<_> = (squares.iter().map(|square| square.edges))
.filter(|j| {
j.left < line.right
&& j.right > line.left
&& j.top < line.bottom
&& j.bottom > line.top
})
.collect();
assert_eq!(on.len(), 2 + usize::from(direction == Direction::Across));
assert!(on.iter().all(|j| near.contains(j)));
}
}
fn drawn(left: i64, right: i64, direction: Direction) -> super::DrawnLine {
let u = Length::from_units;
let edges = if direction == Direction::Across {
f_office_units::Edges {
left: u(left),
top: u(50),
right: u(right),
bottom: u(54),
}
} else {
f_office_units::Edges {
left: u(left),
top: u(0),
right: u(right),
bottom: u(100),
}
};
let dash = (direction == Direction::Across).then_some(crate::props::DashStyle::Dashed);
super::DrawnLine {
edges,
color: border().color.drawn(),
dash,
}
}
fn cell_cut_rooms(across: &[(i64, i64)], down: &[(i64, i64)]) -> Vec<(i64, i64)> {
let across: Vec<_> = across
.iter()
.map(|&(l, r)| drawn(l, r, Direction::Across))
.collect();
let down: Vec<_> = down
.iter()
.map(|&(l, r)| drawn(l, r, Direction::Down))
.collect();
super::cell_cuts(&across, &down)
.into_iter()
.map(|room| (room.edges.left.units(), room.edges.right.units()))
.collect()
}
#[test]
fn cuts_a_dashed_line_where_it_passes_to_the_next_cell_or_leaves_a_gap_shorter_than_it_is_wide() {
assert_eq!(cell_cut_rooms(&[(0, 100), (100, 200)], &[]), [(100, 104)]);
assert_eq!(cell_cut_rooms(&[(0, 101), (100, 200)], &[]), [(100, 104)]);
assert_eq!(cell_cut_rooms(&[(0, 100), (103, 200)], &[]), [(103, 107)]);
assert_eq!(cell_cut_rooms(&[(0, 100), (104, 200)], &[]), []);
assert_eq!(cell_cut_rooms(&[(0, 100), (100, 200)], &[(98, 102)]), []);
}