use f_office_paint::{Item, Page, Rect};
use f_office_units::{Edges, Length};
use std::rc::Rc;
use crate::art_pictures::ArtPictures;
use crate::body::{ArtSide, Block, BorderPages, NoteId, PageAlign, PageBorders, PageSide, Section};
use crate::grid::Device;
use crate::lines::NoteNumber;
use crate::note_setup::NoteLabel;
use crate::numbering::ListCounters;
use crate::props::{ArtStyle, Border};
use super::art_borders;
use super::decisions;
use super::line::height::REST_PARTS;
use super::notes::{Footnote, Separator};
use super::output::{BodyArea, PassOutput};
use super::sections::Parity;
use super::style_refs::StyledPages;
use super::{Kind, Layout, LineStory, PartStory, PlacedLine, Story, StoryKind, StoryNumbers};
const ENDLESS_STORY_HEIGHT: Length =
Length::from_units(i64::MAX / crate::params::STORY_HEIGHT_PARTS);
#[derive(Clone, Debug)]
pub(super) struct StoryInputs {
section: Section,
page: Section,
notes: Length,
counters: ListCounters,
style_page: Option<(Rc<StyledPages>, usize)>,
}
impl PartialEq for StoryInputs {
fn eq(&self, other: &Self) -> bool {
let style_page = match (&self.style_page, &other.style_page) {
(Some((pages, page)), Some((others, other_page))) => {
Rc::ptr_eq(pages, others) && page == other_page
}
(None, None) => true,
_ => false,
};
self.section == other.section
&& self.page == other.page
&& self.notes == other.notes
&& self.counters == other.counters
&& style_page
}
}
fn margin_story(section: &Section) -> Section {
Section {
line_pitch: None,
columns: crate::body::Columns::default(),
..*section
}
}
impl<'a> Layout<'a> {
fn kind(&self, index: usize) -> Kind {
let info = self.page_info(index);
if info.first && self.section_of(info.section).title_page {
Kind::First
} else if self.settings.even_and_odd_headers && Parity::of(info.number) == Parity::Even {
Kind::Even
} else {
Kind::Default
}
}
fn shows(&self, kind: Kind) -> bool {
match kind {
Kind::First => self.section.title_page,
Kind::Even => self.settings.even_and_odd_headers,
Kind::Default => true,
}
}
pub(super) fn page_number(&self, index: usize) -> i64 {
self.page_info(index).number
}
pub(super) fn page_top(&self, index: usize) -> Length {
self.tops
.get(Self::slot(self.kind(index)))
.copied()
.unwrap_or(self.section.top)
}
pub(super) fn page_bottom(&self) -> Length {
let bottom = self.body_bottom_of(self.output.pages.len());
self.balance.map_or(bottom, |b| bottom.min(b.limit))
}
pub(super) fn body_bottom_of(&self, index: usize) -> Length {
self.bottoms
.get(Self::slot(self.kind(index)))
.copied()
.unwrap_or(self.section.height - self.section.bottom)
}
pub(super) fn body_end(&self) -> Length {
let end = self.page_bottom() - self.notes_height();
if self.body_has_ended() {
end.min(self.y)
} else {
end
}
}
const fn slot(kind: Kind) -> usize {
match kind {
Kind::First => 0,
Kind::Even => 1,
Kind::Default => 2,
}
}
pub(super) fn measure_body(&mut self) {
if self.context.earlier.header_styles.0.is_some() {
self.story_page = Some(self.output.pages.len());
}
let kinds = [Kind::First, Kind::Even, Kind::Default];
self.tops = kinds.map(|kind| self.body_top(kind));
self.bottoms = kinds.map(|kind| self.body_bottom(kind));
self.story_page = None;
}
fn body_top(&mut self, kind: Kind) -> Length {
let headers = self.section_layout(self.section_index).headers.clone();
let top = self.section.top;
let Some(header) = headers.get(kind) else {
return top;
};
if self.section.fixed.0 || !self.shows(kind) {
return top;
}
let section = margin_story(&self.section);
let layout = self.header_layout(§ion, header, section.header, StoryNumbers::PAGE_ONE);
let end = layout.y + layout.previous.as_ref().map_or(Length::ZERO, |p| p.after);
top.max(end)
}
fn body_bottom(&mut self, kind: Kind) -> Length {
let footers = self.section_layout(self.section_index).footers.clone();
let height = self.section.height;
let bottom = height - self.section.bottom;
let Some(footer) = footers.get(kind) else {
return bottom;
};
if self.section.fixed.1 || !self.shows(kind) {
return bottom;
}
let section = margin_story(&self.section);
let (footer, _) = self.footer_height(§ion, footer, StoryNumbers::PAGE_ONE);
bottom.min(height - self.section.footer - footer)
}
fn footer_height(
&mut self,
section: &Section,
footer: &PartStory,
numbers: StoryNumbers,
) -> (Length, i64) {
let layout = self.footer_layout(section, footer, (Length::ZERO, 0), numbers);
let after = layout.previous.as_ref().map_or(Length::ZERO, |p| p.after);
(layout.y + after, layout.y_rest)
}
pub(super) fn headers_and_footers(
&mut self,
index: usize,
count: i64,
) -> (Vec<Item>, Vec<PlacedLine>) {
self.story_page = Some(index);
let kind = self.kind(index);
let info = self.page_info(index);
let numbers = StoryNumbers::on_page(info.number, count);
let section = self.page_section(index);
let part = self.section_layout(info.section);
let (headers, footers, borders) =
(part.headers.clone(), part.footers.clone(), part.borders);
let margins = margin_story(§ion);
let between = BodyArea {
top: section.top,
end: section.height - section.bottom,
notes: Length::ZERO,
};
let body = self
.output
.body_areas
.get(index)
.copied()
.unwrap_or(between);
let area = decisions::story_margin_area(&self.context.quirks, between, body);
let frame = Section {
top: area.top,
bottom: section.height - area.end,
..margins
};
let body_page = self.page.replace(frame);
let body_notes = std::mem::replace(&mut self.story_notes, area.notes);
let (mut items, mut lines) = (Vec::new(), Vec::new());
if let Some(header) = headers.get(kind) {
let mut layout = self.header_layout(&margins, header, section.header, numbers);
lines.extend(story_lines(&mut layout, index));
items.extend(layout.page_items());
}
if let Some(footer) = footers.get(kind) {
let height = self.footer_height(&margins, footer, numbers);
let top = footer_start(section.height - section.footer, height);
let mut layout = self.footer_layout(&margins, footer, top, numbers);
lines.extend(story_lines(&mut layout, index));
items.extend(layout.page_items());
}
self.page = body_page;
self.story_notes = body_notes;
let shown = borders.filter(|b| match b.pages {
BorderPages::All => true,
BorderPages::First => info.first,
BorderPages::NotFirst => !info.first,
});
if let Some(borders) = shown {
let pictures = self.context.art;
let missing = page_borders(&borders, (§ion, &self.device), pictures, &mut items);
for (style, size) in missing {
self.output
.warnings
.push(crate::Warning::ArtBorderNotDrawn {
style: style.name(),
size,
});
}
}
self.story_page = None;
(items, lines)
}
pub(super) fn separator(&mut self, separator: Separator, top: Length) -> (Vec<Item>, Length) {
let section = self.note_section();
let context = self.context;
let blocks = separator.blocks(&context.notes);
let mut layout = self.story_layout(§ion, blocks, top, note_separator(separator));
let end = layout.y + layout.separator_after();
(layout.page_items(), end)
}
fn endnote_continuation(&mut self) {
let section = self.section;
let context = self.context;
let blocks = Separator::Continuation.blocks(&context.endnotes);
let story = Story {
kind: StoryKind::EndnoteContinuation,
numbers: StoryNumbers::PAGE_ONE,
};
let mut layout = self.story_layout(§ion, blocks, self.y, story);
let after = layout
.previous
.as_ref()
.filter(|last| !last.after_auto)
.map_or(Length::ZERO, |last| last.after);
let end = layout.y + after;
self.items.extend(layout.page_items());
self.move_to(end);
}
pub(super) fn separator_from(&self, inputs: StoryInputs, separator: Separator) -> Layout<'a> {
let blocks = separator.blocks(&self.context.notes);
self.story_from(inputs, blocks, (Length::ZERO, 0), note_separator(separator))
}
pub(super) fn placed_note(
&mut self,
section: &Section,
note: Footnote<'_>,
top: Length,
number: NoteNumber,
) -> (Vec<Item>, Length, Vec<PlacedLine>) {
let story = footnote(self.note_label(number), note.id);
self.placed_story(section, note.blocks, top, story)
}
pub(super) fn note_lines(
&mut self,
section: &Section,
note: Footnote<'_>,
top: Length,
number: NoteNumber,
(from, to): (usize, Option<usize>),
) -> (Vec<Item>, Vec<PlacedLine>, Length) {
let story = footnote(self.note_label(number), note.id);
let mut layout = self.story_layout(section, note.blocks, Length::ZERO, story);
let placed = std::mem::take(&mut layout.output.placed);
let to = to.unwrap_or(placed.len()).min(placed.len());
let Some(first) = placed.get(from) else {
return (Vec::new(), Vec::new(), Length::ZERO);
};
let cut = first.top;
let end = placed
.get(to)
.map_or(Some(layout.items.len()), |line| line.item.index());
let mut items = first
.item
.index()
.zip(end)
.map_or_default(|(start, end)| layout.items.cloned(start..end));
super::table::move_items(&mut items, top - cut);
let page = self.output.pages.len();
let lines = placed
.get(from..to)
.unwrap_or_default()
.iter()
.map(|line| {
let mut line = PlacedLine {
page,
story: LineStory::Footnote,
item: super::FirstItem::Carried,
..line.clone()
};
line.move_down(top - cut);
line
})
.collect();
(items, lines, cut)
}
pub(super) fn placed_story(
&mut self,
section: &Section,
blocks: &[Block],
top: Length,
story: Story,
) -> (Vec<Item>, Length, Vec<PlacedLine>) {
self.placed_story_dropping_last(section, blocks, top, story, |_| Length::ZERO)
}
pub(super) fn placed_story_dropping_last(
&mut self,
section: &Section,
blocks: &[Block],
top: Length,
story: Story,
drop: impl FnOnce(&[PlacedLine]) -> Length,
) -> (Vec<Item>, Length, Vec<PlacedLine>) {
let part = story.kind.part(self.output.story_parts.count);
if part.is_some() {
self.output.story_parts.count += 1;
}
let mut layout = self.story_layout(section, blocks, top, story);
let dy = match layout.output.placed.last() {
Some(line)
if line.area == super::LineArea::OutsideTables
&& matches!(line.item, super::FirstItem::At(_)) =>
{
drop(&layout.output.placed)
}
_ => Length::ZERO,
};
if dy != Length::ZERO
&& let Some(line) = layout.output.placed.last_mut()
&& let super::FirstItem::At(first) = line.item
{
line.baseline += dy;
let end = layout.items.len();
layout.items.move_down(first..end, dy);
}
let lines = story_lines(&mut layout, self.output.pages.len())
.into_iter()
.map(|line| PlacedLine {
item: super::FirstItem::Carried,
part: part.unwrap_or(line.part),
..line
})
.collect();
let y = layout.end_of_story();
(layout.page_items(), y, lines)
}
pub(super) fn story_in(
&mut self,
section: &Section,
blocks: &[Block],
top: Length,
story: Story,
) -> (Vec<Item>, Length) {
let mut layout = self.story_layout(section, blocks, top, story);
let y = layout.end_of_story();
(layout.page_items(), y)
}
fn end_of_story(&self) -> Length {
if self
.story
.as_ref()
.is_some_and(|story| story.kind.ends_below_space_after())
{
self.end_below_space_after()
} else {
self.y
}
}
fn end_below_space_after(&self) -> Length {
let after = self
.previous
.as_ref()
.filter(|p| !p.after_auto)
.map_or(Length::ZERO, |p| p.after);
self.y + after
}
fn header_layout(
&mut self,
section: &Section,
header: &PartStory,
top: Length,
numbers: StoryNumbers,
) -> Layout<'a> {
let kind = StoryKind::Header {
several_blocks: header.blocks.len() > 1,
part: Rc::clone(&header.part),
};
self.story_layout(section, &header.blocks, top, Story { kind, numbers })
}
fn footer_layout(
&mut self,
section: &Section,
footer: &PartStory,
top: (Length, i64),
numbers: StoryNumbers,
) -> Layout<'a> {
let kind = StoryKind::Footer {
part: Rc::clone(&footer.part),
};
self.story_layout_with_rest(section, &footer.blocks, top, Story { kind, numbers })
}
pub(super) fn story_end(
&mut self,
section: &Section,
blocks: &[Block],
top: Length,
story: Story,
) -> Length {
self.story_layout(section, blocks, top, story)
.end_below_space_after()
}
pub(super) fn note_end(
&mut self,
note: Footnote<'_>,
number: NoteNumber,
) -> (Length, Option<super::Previous>) {
let section = self.note_section();
let story = footnote(self.note_label(number), note.id);
let layout = self.story_layout(§ion, note.blocks, Length::ZERO, story);
(layout.y, layout.previous.clone())
}
pub(super) fn layout_without_warnings<T>(&mut self, layout: impl FnOnce(&mut Self) -> T) -> T {
let trial = self.output.start_trial();
let value = layout(self);
self.output.end_trial(trial, false);
value
}
pub(super) fn story_layout(
&mut self,
section: &Section,
blocks: &[Block],
top: Length,
story: Story,
) -> Layout<'a> {
self.story_layout_with_rest(section, blocks, (top, 0), story)
}
fn story_layout_with_rest(
&mut self,
section: &Section,
blocks: &[Block],
(top, rest): (Length, i64),
story: Story,
) -> Layout<'a> {
let inputs = self.story_inputs(section);
let key = story.kind.key();
let mut layout = self.story_from(inputs, blocks, (top, rest), story);
if let Some(key) = key {
let warnings = std::mem::take(&mut layout.output.warnings);
self.output.join_story_warnings(key, warnings);
} else {
self.output.join_warnings_of(&mut layout.output);
}
self.shaper.add_notes(layout.shaper.take_notes());
layout
}
pub(super) fn story_inputs(&self, section: &Section) -> StoryInputs {
let styles = self
.output
.style_pages
.clone()
.or_else(|| self.context.earlier.header_styles.0.clone());
StoryInputs {
section: *section,
page: self.page.unwrap_or(*section),
notes: self.story_notes,
counters: self.numbering.counters().clone(),
style_page: styles.zip(self.story_page),
}
}
fn story_from(
&self,
inputs: StoryInputs,
blocks: &[Block],
(top, rest): (Length, i64),
story: Story,
) -> Layout<'a> {
let StoryInputs {
mut section,
page,
notes,
counters,
style_page,
} = inputs;
section.line_numbers = None;
section.right_to_left = false;
section.top = top;
section.height = ENDLESS_STORY_HEIGHT;
section.bottom = Length::ZERO;
let mut layout = Layout::story(
(self.stories, self.stories),
§ion,
blocks.len(),
self.shaper.fork(),
self.numbering.with_counters(counters),
);
layout.output = PassOutput::for_story();
if let StoryKind::TextBox {
turn: Some((turn, glyphs)),
..
} = story.kind
{
layout.column.within = super::Within::TurnedFrame(false, turn, glyphs);
}
layout.story = Some(story);
layout.y_rest = rest;
layout.device = layout
.device
.with_page(page.width, page.height, page.orientation);
layout.page = Some(page);
layout.story_notes = notes;
layout.style_page = style_page;
layout.blocks(blocks);
layout
}
pub(super) fn start_wraps(&mut self) {
self.bands.clear();
self.squares.clear();
self.frame_boxes.clear();
let kind = self.kind(self.output.pages.len());
let number = self.page_number(self.output.pages.len());
let numbers = StoryNumbers::on_page(number, number);
let section = margin_story(&self.section);
let mut squares = Vec::new();
let mut bands = Vec::new();
let part = self.section_layout(self.section_index);
let (headers, footers) = (part.headers.clone(), part.footers.clone());
if let Some(header) = headers.get(kind) {
let layout = self.header_layout(§ion, header, section.header, numbers);
squares.extend(layout.squares);
bands.extend(layout.bands);
}
if let Some(footer) = footers.get(kind) {
let height = self.footer_height(§ion, footer, numbers);
let top = footer_start(section.height - section.footer, height);
let layout = self.footer_layout(§ion, footer, top, numbers);
squares.extend(layout.squares);
bands.extend(layout.bands);
}
let index = self.output.pages.len();
squares.extend(
self.context
.earlier
.wraps
.squares
.iter()
.filter(|(page, _)| *page == index)
.map(|(_, s)| s.clone()),
);
bands.extend(
self.context
.earlier
.wraps
.bands
.iter()
.filter(|(page, _)| *page == index)
.map(|(_, b)| *b),
);
self.squares = squares;
self.bands = bands;
}
fn page_items(&mut self) -> Vec<Item> {
let stacked = |mut layer: Vec<(u64, Item)>| {
layer.sort_by_key(|(stack, _)| *stack);
layer.into_iter().map(|(_, item)| item)
};
let mut items: Vec<Item> = stacked(std::mem::take(&mut self.behind)).collect();
items.append(&mut self.items.take());
items.extend(stacked(std::mem::take(&mut self.front)));
items
}
fn align_page(&mut self) {
let after = self.previous.as_ref().map_or(Length::ZERO, |p| p.after);
let notes = self.notes_height();
let room = self.page_bottom() - notes - self.y - after;
let Some(room) = decisions::aligned_room(&self.context.quirks, notes > Length::ZERO, room)
else {
return;
};
let dy = match self.section.vertical {
PageAlign::Top => return,
PageAlign::Center => room.div_toward_zero(2),
PageAlign::Bottom => room,
};
if dy > Length::ZERO && self.column.cell.is_none() {
self.shift(0..self.items.len(), dy);
}
}
pub(super) fn page_is_empty(&self) -> bool {
self.items.is_empty() && self.behind.is_empty() && self.front.is_empty()
}
pub(super) fn page_has_no_line(&self) -> bool {
self.page_is_empty()
&& self.column_index == 0
&& self.y <= self.page_top(self.output.pages.len())
}
pub(super) fn new_page(&mut self) {
self.paint_column_separators();
self.align_page();
let index = self.output.pages.len();
let area = BodyArea {
top: self.page_top(index),
end: self.body_bottom_of(index),
notes: self.notes_height(),
};
self.place_notes();
let info = self.page_info(index);
self.output.page_infos.push(info);
self.output.body_areas.push(area);
let items = self.page_items();
let section = &self.section;
self.output.pages.push(Page::new(
section.width,
section.height,
section.orientation,
items,
));
if let Some(index) = self.pending.take() {
self.adopt(index);
self.section_title_page = None;
} else if self.settings.side_margins == crate::styles::SideMargins::Mirrored {
let section = self.section_of(self.section_index);
self.take_side_margins(§ion);
self.set_columns();
}
if self.context.earlier.header_styles.0.is_some() {
self.measure_body();
}
self.start_wraps();
self.move_to_body_top(self.output.pages.len());
self.first_column();
if self.endnote.is_some() {
self.endnote_continuation();
}
}
pub(super) fn move_to_body_top(&mut self, index: usize) {
let top = self.page_top(index);
self.move_to(top);
if top == self.section.top {
self.start_twips(top);
}
}
}
fn footer_start(bottom: Length, (height, rest): (Length, i64)) -> (Length, i64) {
let carry = f_office_units::div_round(-rest, REST_PARTS);
(
bottom - height + Length::from_units(carry),
-rest - carry * REST_PARTS,
)
}
fn story_lines(layout: &mut Layout<'_>, index: usize) -> Vec<PlacedLine> {
let mut lines = std::mem::take(&mut layout.output.placed);
let Some(story) = layout
.story
.as_ref()
.and_then(|story| story.kind.line_story())
else {
return Vec::new();
};
for line in &mut lines {
line.page = index;
line.story = story;
}
lines
}
fn footnote(label: NoteLabel, id: NoteId) -> Story {
Story {
kind: StoryKind::Footnote { id },
numbers: StoryNumbers::of_note(label),
}
}
const fn note_separator(separator: Separator) -> Story {
Story {
kind: StoryKind::NoteSeparator(separator),
numbers: StoryNumbers::PAGE_ONE,
}
}
fn outer_edges(borders: &PageBorders, section: &Section, width: &Edges<Length>) -> Edges<Length> {
let space = borders.sides.map(|side| match side {
Some(PageSide::Line(border)) => border.space,
Some(PageSide::Art(art)) => art.space,
None => Length::ZERO,
});
let (page_w, page_h) = (section.width, section.height);
if borders.from_page {
Edges {
left: space.left,
top: space.top,
right: page_w - space.right,
bottom: page_h - space.bottom,
}
} else {
Edges {
left: section.left - space.left - width.left,
top: section.top - space.top - width.top,
right: page_w - section.right + space.right + width.right,
bottom: page_h - section.bottom + space.bottom + width.bottom,
}
}
}
fn page_borders(
borders: &PageBorders,
(section, device): (&Section, &Device),
pictures: &ArtPictures,
items: &mut Vec<Item>,
) -> Vec<(ArtStyle, u32)> {
let thick = |width: Length| device.page_border_line(width);
let sides = borders.sides.map(|side| {
side.and_then(PageSide::line)
.map(|b| device.drawn_border(b))
});
let arts = borders.sides.map(|side| side.and_then(PageSide::art));
let art_width = |art: Option<ArtSide>| {
art.map(|art| device.dots(art_borders::corner_dots(art.size, device)))
};
let lines_of = |side: Option<Border>, outer_first: bool| {
side.map_or_else(Vec::new, |b| {
if b.touches() {
b.touching_lines(thick)
} else {
device.page_border_lines(b, thick(b.width), outer_first)
}
})
};
let drawn = Edges {
left: lines_of(sides.left, true),
top: lines_of(sides.top, true),
right: lines_of(sides.right, false),
bottom: lines_of(sides.bottom, false),
};
let width = Edges {
left: art_width(arts.left).unwrap_or_else(|| side_width(sides.left, &drawn.left, thick)),
top: art_width(arts.top).unwrap_or_else(|| side_width(sides.top, &drawn.top, thick)),
right: art_width(arts.right)
.unwrap_or_else(|| side_width(sides.right, &drawn.right, thick)),
bottom: art_width(arts.bottom)
.unwrap_or_else(|| side_width(sides.bottom, &drawn.bottom, thick)),
};
let Edges {
left,
top,
right,
bottom,
} = outer_edges(borders, section, &width).map(|edge| device.snap(edge));
let depth = band_depths(&sides, &drawn);
let lines = [
(
(sides.top, &drawn.top),
[left, top, right - left, width.top],
true,
true,
(depth.left.as_slice(), depth.right.as_slice()),
),
(
(sides.left, &drawn.left),
[left, top, width.left, bottom - top],
false,
true,
(depth.top.as_slice(), depth.bottom.as_slice()),
),
(
(sides.bottom, &drawn.bottom),
[left, bottom - width.bottom, right - left, width.bottom],
true,
false,
(depth.left.as_slice(), depth.right.as_slice()),
),
(
(sides.right, &drawn.right),
[right - width.right, top, width.right, bottom - top],
false,
false,
(depth.top.as_slice(), depth.bottom.as_slice()),
),
];
for ((side, drawn), bounds, across, outer_first, ends) in lines {
if let Some(border) = side {
side_rects(items, (border, drawn), bounds, (across, outer_first), ends);
}
}
let box_edges = Edges {
left,
top,
right,
bottom,
};
art_borders::art_items(&arts, box_edges, device, pictures, items)
}
fn band_depths(
sides: &Edges<Option<Border>>,
drawn: &Edges<Vec<(Length, Length)>>,
) -> Edges<Vec<Length>> {
let depths = |side: Option<Border>, lines: &[(Length, Length)], outer_first: bool| {
let mut depths: Vec<Length> = lines.iter().map(|(_, thickness)| *thickness).collect();
if !outer_first {
depths.reverse();
}
if side.is_some_and(|b| b.touches()) {
depths
} else {
Vec::new()
}
};
Edges {
left: depths(sides.left, &drawn.left, true),
top: depths(sides.top, &drawn.top, true),
right: depths(sides.right, &drawn.right, false),
bottom: depths(sides.bottom, &drawn.bottom, false),
}
}
fn side_rects(
items: &mut Vec<Item>,
(border, drawn): (Border, &[(Length, Length)]),
[x, y, w, h]: [Length; 4],
(across, outer_first): (bool, bool),
ends: (&[Length], &[Length]),
) {
let count = drawn.len();
for (index, (&(offset, thickness), color)) in drawn.iter().zip(border.line_colors()).enumerate()
{
let depth = if outer_first {
index
} else {
count - 1 - index
};
let nested = if border.touches() {
band_ends(depth, across, ends)
} else {
(Length::ZERO, Length::ZERO)
};
let [x, y, width, height] = if across {
[x + nested.0, y + offset, w - nested.0 - nested.1, thickness]
} else {
[x + offset, y + nested.0, thickness, h - nested.0 - nested.1]
};
items.push(Item::Rect(Rect {
x,
y,
width,
height,
color,
}));
}
}
fn side_width(
side: Option<Border>,
lines: &[(Length, Length)],
thick: impl Fn(Length) -> Length,
) -> Length {
side.map_or(Length::ZERO, |b| {
if b.touches() {
lines.iter().map(|(_, thickness)| *thickness).sum()
} else {
thick(b.width)
}
})
}
fn band_ends(depth: usize, across: bool, (start, end): (&[Length], &[Length])) -> (Length, Length) {
let reach = |depths: &[Length]| {
if depths.is_empty() {
return Length::ZERO;
}
let count = if across { depth } else { depth + 1 };
depths.iter().take(count).copied().sum()
};
(reach(start), reach(end))
}
#[cfg(test)]
mod tests;