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)]), []);
}
fn border_of(eighths: i64) -> Border {
let width = Length::from_eighth_points(eighths).expect("a whole number of units");
Border::new(BorderParts::single(width), Length::ZERO, Color::Auto)
}
fn inner_edge_device() -> crate::grid::Device {
crate::layout::testing::device_at(600, |profile| {
profile.with_variant(crate::quirks::TableLines::RoundsCenters)
})
}
fn default_margins() -> super::Sides<Length> {
let side = crate::params::UNSET_SIDE_MARGIN;
super::Sides {
left: side,
right: side,
top: Length::ZERO,
bottom: Length::ZERO,
}
}
#[test]
fn places_the_lines_of_a_box_the_whole_dots_of_its_margins_outside_the_text_on_the_dots() {
let device = inner_edge_device();
let line = border_of(6);
let sides = super::Sides {
left: None,
right: None,
top: Some(line),
bottom: Some(line),
};
let fifteen = Length::from_twips(15).expect("a whole number of units");
let margins = super::Sides {
top: fifteen,
bottom: fifteen,
..default_margins()
};
let edges = super::inner_edges(
&device,
(&sides, &margins),
(Length::ZERO, Length::ZERO),
(device.dots(700), device.dots(825)),
(Length::ZERO, Length::ZERO),
);
assert_eq!(edges.top, Some((device.dots(707), device.dots(6))));
assert_eq!(edges.bottom, Some((device.dots(818), device.dots(6))));
let whole = super::Sides {
top: Length::from_twips(12).expect("a whole number of units"),
bottom: Length::from_twips(24).expect("a whole number of units"),
..default_margins()
};
let edges = super::inner_edges(
&device,
(&sides, &whole),
(Length::ZERO, Length::ZERO),
(device.dots(700), device.dots(825)),
(Length::ZERO, Length::ZERO),
);
assert_eq!(edges.top, Some((device.dots(706), device.dots(6))));
assert_eq!(edges.bottom, Some((device.dots(819), device.dots(6))));
}
#[test]
fn keeps_each_side_line_inside_the_dot_of_its_box_edge() {
let device = inner_edge_device();
let line = border_of(6);
let sides = super::Sides {
left: Some(line),
right: Some(line),
top: None,
bottom: None,
};
let side = Length::from_twips(112).expect("a whole number of units");
let margins = super::Sides {
left: side,
right: side,
..default_margins()
};
let half = Length::from_units(Length::UNITS_PER_TWIP / 2);
let twips = |whole: i64| Length::from_twips(whole).expect("a whole number of units") + half;
let edges = super::inner_edges(
&device,
(&sides, &margins),
(twips(5153), twips(2148)),
(Length::ZERO, Length::ZERO),
(Length::ZERO, Length::ZERO),
);
assert_eq!(edges.left, Some((device.dots(2154), device.dots(6))));
assert_eq!(edges.right, Some((device.dots(889), device.dots(6))));
}
#[test]
fn leaves_gaps_between_touching_boxes_by_the_twelfth_of_the_column_edge_and_the_line_width() {
let device = inner_edge_device();
let margins = default_margins();
for (eighths, expected) in [
(2, [5, 6, 0]),
(4, [4, 7, 0]),
(8, [0, 11, 0]),
(12, [0, 8, 3]),
(24, [7, 4, 0]),
] {
let line = border_of(eighths);
let sides = super::Sides {
left: Some(line),
right: Some(line),
top: None,
bottom: None,
};
let mut counts = [0; 3];
for column in 1..12 {
let edge = Length::from_twips(1540 + 601 * column).expect("a whole number of units")
+ line.width.div_toward_zero(2);
let edges = super::inner_edges(
&device,
(&sides, &margins),
(edge, edge),
(Length::ZERO, Length::ZERO),
(Length::ZERO, Length::ZERO),
);
let (left, thick) = edges.left.expect("a single left line");
let (right, _) = edges.right.expect("a single right line");
let gap = (left - thick) - (right + thick);
let dots = usize::try_from(gap.units() / device.dots(1).units()).expect("no overlap");
counts[dots] += 1;
}
assert_eq!(counts, expected, "lines of {eighths} eighths of a point");
}
}
#[test]
fn places_the_lines_across_a_box_from_the_dots_of_the_table_top_and_of_the_closing_line_foot() {
let device = inner_edge_device();
let dots = |whole: i64, quarters: i64| {
device.dots(whole) + Length::from_units(device.dots(1).units() * quarters / 4)
};
let line = border_of(4);
let sides = super::Sides {
left: None,
right: None,
top: Some(line),
bottom: Some(line),
};
let wide = border_of(12).width;
let margins = super::Sides {
left: Length::ZERO,
right: Length::ZERO,
top: Length::ZERO,
bottom: Length::ZERO,
};
let edges = |top, bottom, around| {
super::inner_edges(
&device,
(&sides, &margins),
(Length::ZERO, Length::ZERO),
(top, bottom),
around,
)
};
let between = edges(dots(2600, 0), dots(2788, 3), (Length::ZERO, Length::ZERO));
assert_eq!(between.bottom, Some((device.dots(2785), device.dots(4))));
let last = edges(dots(2600, 0), dots(2788, 3), (Length::ZERO, wide));
assert_eq!(last.bottom, Some((device.dots(2784), device.dots(4))));
let first = edges(dots(2218, 1), dots(2400, 0), (wide, Length::ZERO));
assert_eq!(first.top, Some((device.dots(2235), device.dots(4))));
}