#[allow(
clippy::wildcard_imports,
reason = "pagination submodules extend the parent paginator and share its scope"
)]
use super::*;
use crate::layout::multicol::{
ColumnGeometry, WrapRows,
slices::{Distribution, Plan, Run, slice_clip, windowed},
};
use crate::style::{BoxSizing, ColumnFill, FootnoteDisplay, FootnotePolicy};
use std::{cell::RefCell, collections::BTreeSet};
pub(super) struct FootnoteLayout {
flow: StandaloneFlow,
width: f32,
height: f32,
compact: bool,
breaks: Vec<f32>,
members: Vec<[f32; 2]>,
body_box: Option<Rect>,
cloned: Option<[f32; 2]>,
}
#[derive(Clone, Copy)]
pub(super) struct FootnotePart {
body: usize,
top: f32,
bottom: f32,
width: f32,
inline: bool,
repeated: [f32; 2],
}
impl FootnotePart {
fn height(self) -> f32 {
self.bottom - self.top + self.repeated[0] + self.repeated[1]
}
}
struct Row {
parts: Vec<(FootnotePart, f32)>,
height: f32,
}
fn rows(parts: &[FootnotePart], width: f32) -> Vec<Row> {
let mut rows: Vec<Row> = Vec::new();
for &part in parts {
if part.inline
&& let Some(row) = rows.last_mut()
&& row.parts.iter().all(|(other, _)| other.inline)
{
let used: f32 = row.parts.iter().map(|(other, _)| other.width).sum();
if !overflows(used + part.width, width) {
row.parts.push((part, used));
row.height = row.height.max(part.height());
continue;
}
}
rows.push(Row {
parts: vec![(part, 0.0)],
height: part.height(),
});
}
rows
}
struct AreaContent {
rows: Vec<Row>,
columns: Distribution,
}
impl AreaContent {
fn height(&self) -> f32 {
self.columns.height()
}
fn fits(&self, limit: f32) -> bool {
!self.columns.overflows && !overflows(self.height(), limit)
}
fn placed(&self, geometry: ColumnGeometry) -> Vec<(FootnotePart, [f32; 2])> {
let mut placed = Vec::new();
for (slice, [x, y]) in self.columns.placed(geometry) {
let row = &self.rows[slice.run];
if let [(part, _)] = row.parts.as_slice() {
let part = FootnotePart {
top: slice.top,
bottom: slice.bottom,
repeated: slice.repeated,
..*part
};
placed.push((part, [x, y]));
} else {
placed.extend(
row.parts
.iter()
.map(|&(part, offset)| (part, [x + offset, y])),
);
}
}
placed
}
}
type FootnoteLayouts = HashMap<(usize, u32), Rc<FootnoteLayout>>;
pub(in crate::layout) type FootnoteDeferrals = BTreeMap<(usize, usize), FootnotePolicy>;
#[derive(Clone, Default)]
pub(super) struct Footnotes {
pages: Vec<Vec<FootnotePart>>,
carried: VecDeque<FootnotePart>,
placed: BTreeSet<usize>,
recorded: usize,
ahead: Vec<(usize, usize)>,
deferred: FootnoteDeferrals,
queued: HashMap<usize, f32>,
inline: crate::document::InlineFootnotePlacement,
layouts: Rc<RefCell<FootnoteLayouts>>,
}
impl Footnotes {
fn parts(&self, page: usize) -> &[FootnotePart] {
self.pages.get(page).map_or(&[], Vec::as_slice)
}
pub(super) fn last_page(&self) -> Option<usize> {
self.pages.iter().rposition(|parts| !parts.is_empty())
}
pub(super) fn has_carried(&self) -> bool {
!self.carried.is_empty()
}
pub(super) fn take_deferred(&mut self) -> FootnoteDeferrals {
std::mem::take(&mut self.deferred)
}
pub(super) fn take_inline_placement(&mut self) -> crate::document::InlineFootnotePlacement {
std::mem::take(&mut self.inline)
}
}
fn strictest(policies: impl Iterator<Item = FootnotePolicy>) -> FootnotePolicy {
policies
.max_by_key(|policy| match policy {
FootnotePolicy::Auto => 0,
FootnotePolicy::Line => 1,
FootnotePolicy::Block => 2,
})
.unwrap_or_default()
}
struct Area {
x: f32,
width: f32,
columns: ColumnGeometry,
fill: ColumnFill,
column_height: Option<f32>,
wrap: Option<WrapRows>,
rule: crate::graphics::Border,
padding: [f32; 4],
borders: [f32; 4],
margins: [f32; 4],
minimum: f32,
maximum: Option<f32>,
}
impl Area {
fn edges(&self, side: usize) -> f32 {
self.padding[side] + self.borders[side] + self.margins[side]
}
fn content_height(&self, parts: f32, unbounded: bool) -> f32 {
let height = parts.max(self.minimum);
match self.maximum {
Some(maximum) if !unbounded => height.min(maximum.max(self.minimum)),
_ => height,
}
}
fn outer_height(&self, parts: Option<f32>, unbounded: bool) -> f32 {
parts.map_or(0.0, |parts| {
self.edges(TOP) + self.content_height(parts, unbounded) + self.edges(BOTTOM)
})
}
fn plan(&self, unbounded: bool) -> Plan {
let filled = self
.maximum
.filter(|_| !unbounded && self.fill == ColumnFill::Auto);
Plan {
count: self.columns.count,
height: self.column_height.or(filled),
definite: self.column_height.is_some(),
fill: self.fill,
wrap: self.wrap,
}
}
}
const TOP: usize = 0;
const RIGHT: usize = 1;
const BOTTOM: usize = 2;
const LEFT: usize = 3;
impl<'a: 't, 't: 'a> Paginator<'a, '_, 't> {
fn area_box(
&self,
page: usize,
) -> Option<(
std::borrow::Cow<'_, crate::style::Style>,
std::borrow::Cow<'_, BoxDecoration>,
)> {
let Fragmentainers::Pages(pages) = &self.fragmentainers else {
return None;
};
let footnote = &pages.boxes[page].footnote;
let flow = crate::style::logical::Flow::used(pages.document.root().style());
if !flow.writing_mode.vertical() {
return Some((
std::borrow::Cow::Borrowed(&footnote.style),
std::borrow::Cow::Borrowed(&footnote.decoration),
));
}
let logical = flow.horizontal();
Some((
std::borrow::Cow::Owned(crate::style::logical::reflowed_from(
&footnote.style,
flow,
logical,
)),
std::borrow::Cow::Owned(crate::style::logical::decoration(
&footnote.decoration,
flow,
logical,
)),
))
}
fn area(&self, page: usize) -> Option<Area> {
let (style, _) = self.area_box(page)?;
let content = self.fragmentainers.content_box(page);
let padding = std::array::from_fn(|side| style.padding[side].resolve(content.width));
let borders = style
.decoration
.borders
.map(crate::graphics::Border::used_width);
let margins =
std::array::from_fn(|side| style.margin[side].resolve(content.width).unwrap_or(0.0));
let vertical = padding[TOP] + padding[BOTTOM] + borders[TOP] + borders[BOTTOM];
let horizontal = padding[LEFT] + padding[RIGHT] + borders[LEFT] + borders[RIGHT];
let content_size = |size: &crate::style::Size, basis: f32, edges: f32| {
size.resolve_definite(Some(basis)).map(|size| {
if style.box_sizing == BoxSizing::BorderBox {
(size - edges).max(0.0)
} else {
size
}
})
};
let width = content_size(&style.width, content.width, horizontal)
.unwrap_or(content.width - horizontal - margins[LEFT] - margins[RIGHT]);
let height = content_size(&style.height, content.height, vertical);
let minimum = content_size(&style.min_height, content.height, vertical)
.unwrap_or(0.0)
.max(height.unwrap_or(0.0));
let maximum = match (
height,
content_size(&style.max_height, content.height, vertical),
) {
(Some(height), Some(maximum)) => Some(height.min(maximum)),
(height, maximum) => height.or(maximum),
};
let width = width.max(0.0);
let multicol = style.multicol.is_multicol();
Some(Area {
x: content.x + margins[LEFT],
width,
columns: ColumnGeometry::resolve(&style, 0.0, width),
fill: if multicol {
style.multicol.fill
} else {
ColumnFill::Balance
},
column_height: style.multicol.height,
wrap: WrapRows::of(&style, height).filter(|_| multicol),
rule: style.multicol.rule,
padding,
borders,
margins,
minimum,
maximum,
})
}
fn footnotes_only(&self, page: usize) -> bool {
self.footnote_pages.is_some_and(|first| page >= first)
}
fn area_content(
&self,
parts: &[FootnotePart],
area: &Area,
unbounded: bool,
) -> Option<AreaContent> {
if parts.is_empty() {
return None;
}
let width = area.columns.width;
let rows = rows(parts, width);
let layouts: Vec<_> = rows
.iter()
.map(|row| match row.parts.as_slice() {
[(part, _)] => Some(self.laid_out(part.body, width)),
_ => None,
})
.collect();
let runs: Vec<Run<'_>> = rows
.iter()
.zip(&layouts)
.map(|(row, layout)| match layout {
Some(layout) => {
let (part, _) = row.parts[0];
Run {
top: part.top,
bottom: part.bottom,
breaks: &layout.breaks,
cloned: layout.cloned.unwrap_or_default(),
repeated: part.repeated,
}
}
None => Run::whole(row.height),
})
.collect();
let columns = Distribution::of(&runs, area.plan(unbounded));
Some(AreaContent { rows, columns })
}
fn laid_out(&self, body: usize, width: f32) -> Rc<FootnoteLayout> {
self.footnotes
.layouts
.borrow()
.get(&(body, width.to_bits()))
.cloned()
.expect("laying out a body's parts caches its layout")
}
fn content_height(&self, page: usize) -> Option<f32> {
let area = self.area(page)?;
let parts = self.footnotes.parts(page);
self.measurer.work.charge(parts.len());
self.area_content(parts, &area, self.footnotes_only(page))
.map(|content| content.height())
}
pub(super) fn footnote_reserve(&self, page: usize) -> f32 {
self.area(page).map_or(0.0, |area| {
area.outer_height(self.content_height(page), self.footnotes_only(page))
})
}
fn footnote_layout(
&mut self,
index: usize,
area_width: f32,
) -> Result<Rc<FootnoteLayout>, LayoutError> {
let key = (index, area_width.to_bits());
if let Some(layout) = self.footnotes.layouts.borrow().get(&key) {
return Ok(layout.clone());
}
let Fragmentainers::Pages(pages) = &self.fragmentainers else {
unreachable!("footnote areas belong to pages");
};
let document = pages.document;
let footnotes = document.footnotes();
let body = &footnotes.bodies[index].block;
let inline = footnotes.bodies[index].inline;
let compact = !inline && body.style().footnote_display == FootnoteDisplay::Compact;
let width = if compact {
self.measurer
.run(|measurer| crate::layout::intrinsic::measure(body, measurer, area_width))?
.max
.min(area_width)
} else {
area_width
};
let measured =
self.measurer
.block(body, 0.0, width, body.style().used_rtl(), false, None)?;
let height = if measured.through {
let mut margins = measured.before.clone();
margins.merge(measured.after.clone());
margins.value()
} else {
measured.before.value() + measured.height + measured.after.value()
};
let measured_after = measured.after.value();
let body_box = (!inline && !measured.through)
.then_some(measured.paint.as_ref())
.flatten()
.map(|paint| Rect {
y: measured.before.value(),
height: measured.height,
..paint.rect
});
let cloned = body_box.filter(|_| body.style().clone_decoration).map(|_| {
let geometry = &measured.geometry;
[
geometry.padding[0] + geometry.border[0],
geometry.padding[2] + geometry.border[2],
]
});
let flow = Paginator::standalone(
&mut *self.measurer,
measured,
Rect {
x: 0.0,
y: 0.0,
width,
height,
},
document.navigation().destinations,
true,
)?;
let height = height.max(flow.extent + measured_after);
let breaks = flow.breaks([body.style().orphans, body.style().widows], height);
let mut members = Vec::new();
if inline {
let elements = &footnotes.bodies[index].members;
members.resize(elements.len(), [0.0, height]);
let mut starts = HashMap::new();
for line in flow.event_lines.iter().rev() {
starts.insert(line.element, line);
}
let mut end = height;
for (position, &member) in elements.iter().enumerate().skip(1).rev() {
let element = footnotes.elements[member].element;
if let Some(start) = starts.get(&element) {
members[position][0] = start.extent[0];
end = if start.shared {
start.extent[1]
} else {
start.extent[0]
};
} else {
members[position][0] = end;
}
members[position - 1][1] = end;
}
}
let layout = Rc::new(FootnoteLayout {
flow,
width,
height,
compact,
breaks,
members,
body_box,
cloned,
});
self.footnotes
.layouts
.borrow_mut()
.insert(key, layout.clone());
Ok(layout)
}
fn document_footnotes(&self) -> &'t crate::document::Footnotes {
let Fragmentainers::Pages(pages) = &self.fragmentainers else {
unreachable!("footnote areas belong to pages");
};
pages.document.footnotes()
}
fn footnote_part(
&mut self,
footnote: usize,
area_width: f32,
queued: f32,
) -> Result<Option<FootnotePart>, LayoutError> {
let footnotes = self.document_footnotes();
let body = footnotes.elements[footnote].body;
let layout = self.footnote_layout(body, area_width)?;
let Some(position) = footnotes.bodies[body].inline.then(|| {
footnotes.bodies[body]
.members
.iter()
.position(|&member| member == footnote)
.expect("an inline body holds its footnotes")
}) else {
return Ok(Some(FootnotePart {
body,
top: 0.0,
bottom: layout.height,
width: layout.width,
inline: layout.compact,
repeated: [0.0; 2],
}));
};
let end = layout.members[position][1];
Ok((end > queued).then_some(FootnotePart {
body,
top: queued,
bottom: end,
width: layout.width,
inline: false,
repeated: [0.0; 2],
}))
}
fn footnote_waits(&mut self, footnote: usize, area_width: f32) -> Result<bool, LayoutError> {
let footnotes = self.document_footnotes();
let body = footnotes.elements[footnote].body;
let end = match footnotes.bodies[body]
.members
.iter()
.position(|&member| member == footnote)
{
Some(position) if footnotes.bodies[body].inline => {
self.footnote_layout(body, area_width)?.members[position][1]
}
_ => f32::INFINITY,
};
Ok(self
.footnotes
.carried
.iter()
.any(|part| part.body == body && part.top < end))
}
fn footnote_policy(&self, footnote: usize) -> FootnotePolicy {
match &self.fragmentainers {
Fragmentainers::Pages(pages) => pages.document.footnotes().elements[footnote].policy,
Fragmentainers::Columns(_) => FootnotePolicy::Auto,
}
}
pub(super) fn places_footnotes(&self) -> bool {
matches!(self.fragmentainers, Fragmentainers::Pages(_))
&& self.unbroken == 0
&& self.parallel_depth == 0
}
pub(super) fn footnote_growth(
&mut self,
footnotes: &[usize],
pending: &mut Vec<FootnotePart>,
) -> Result<Option<f32>, LayoutError> {
let Some(area) = self.area(self.page) else {
return Ok(None);
};
if self.footnotes.has_carried() {
return Ok(None);
}
let mut parts: Vec<FootnotePart> = self
.footnotes
.parts(self.page)
.iter()
.chain(pending.iter())
.copied()
.collect();
let before = self
.area_content(&parts, &area, false)
.map(|content| content.height());
let start = parts.len();
for &footnote in footnotes {
let body = self.document_footnotes().elements[footnote].body;
let queued = parts
.iter()
.filter(|part| part.body == body)
.map(|part| part.bottom)
.fold(
self.footnotes.queued.get(&body).copied().unwrap_or(0.0),
f32::max,
);
parts.extend(self.footnote_part(footnote, area.columns.width, queued)?);
}
let after = self.area_content(&parts, &area, false);
if after
.as_ref()
.is_some_and(|after| !after.fits(area.maximum.unwrap_or(f32::INFINITY)))
{
return Ok(None);
}
pending.extend_from_slice(&parts[start..]);
Ok(Some(
area.outer_height(after.map(|after| after.height()), false)
- area.outer_height(before, false),
))
}
pub(super) fn strictest_policy(&self, footnotes: &[usize]) -> FootnotePolicy {
strictest(
footnotes
.iter()
.map(|&footnote| self.footnote_policy(footnote)),
)
}
pub(super) fn deferred_policy(
&self,
source: &crate::document::Paragraph,
line: &crate::text::Line,
footnotes: &[usize],
) -> Result<Option<FootnotePolicy>, LayoutError> {
let deferrals = &self.measurer.footnote_deferrals;
if deferrals.is_empty() {
return Ok(None);
}
let Some(page) = self.physical_page() else {
return Ok(None);
};
let mut calls = Vec::new();
for fragment in line.atomics.iter().chain(&line.annotations) {
let (events, visited) = source.inline_block(fragment.index).block().subtree_events();
self.measurer.work.spend(visited)?;
calls.extend(events.into_iter().filter_map(|event| event.footnote));
}
if !self.places_footnotes() {
calls.extend_from_slice(footnotes);
}
let policy = strictest(
calls
.into_iter()
.filter_map(|footnote| deferrals.get(&(footnote, page)).copied()),
);
Ok((policy != FootnotePolicy::Auto).then_some(policy))
}
fn queue_footnotes(
&mut self,
footnotes: impl IntoIterator<Item = usize>,
) -> Result<(), LayoutError> {
let Some(area) = self.area(self.page) else {
return Ok(());
};
for footnote in footnotes {
if !self.footnotes.placed.insert(footnote) {
continue;
}
let footnotes = self.document_footnotes();
let body = footnotes.elements[footnote].body;
let mut queued = self.footnotes.queued.get(&body).copied().unwrap_or(0.0);
let members = &footnotes.bodies[body].members;
if let Some(position) = members.iter().position(|&member| member == footnote)
&& position > 0
{
let layout = self.footnote_layout(body, area.columns.width)?;
let last = self
.footnotes
.carried
.back()
.or_else(|| self.footnotes.parts(self.page).last());
if queued != layout.members[position - 1][1]
|| last.is_none_or(|part| part.body != body)
{
self.footnotes
.inline
.starts
.insert(footnotes.elements[footnote].element);
queued = queued.min(layout.members[position][0]);
}
}
let Some(part) = self.footnote_part(footnote, area.columns.width, queued)? else {
continue;
};
self.footnotes.queued.insert(body, part.bottom);
match self.footnotes.carried.back_mut() {
Some(last) if last.body == body && last.bottom == part.top => {
last.bottom = part.bottom;
}
_ => self.footnotes.carried.push_back(part),
}
}
Ok(())
}
fn record_inline_pages(
&mut self,
part: FootnotePart,
page: usize,
area_width: f32,
) -> Result<(), LayoutError> {
let footnotes = self.document_footnotes();
let body = &footnotes.bodies[part.body];
if !body.inline {
return Ok(());
}
let layout = self.footnote_layout(part.body, area_width)?;
let Fragmentainers::Pages(pages) = &self.fragmentainers else {
unreachable!("footnote areas belong to pages");
};
let request = &pages.boxes[page].request;
if *request == pages.document.page_box().request {
return Ok(());
}
for (position, &member) in body.members.iter().enumerate() {
let element = footnotes.elements[member].element;
let start = layout.members[position][0];
if (position == 0 || self.footnotes.inline.starts.contains(&element))
&& part.top <= start
&& (start < part.bottom || part.top == part.bottom)
{
self.footnotes
.inline
.pages
.entry(element)
.or_insert_with(|| request.clone());
}
}
Ok(())
}
pub(super) fn place_footnotes(&mut self, footnotes: &[usize]) -> Result<(), LayoutError> {
self.queue_footnotes(footnotes.iter().copied())?;
self.place_carried_footnotes()
}
fn queue_recorded_footnotes(&mut self) -> Result<Vec<(usize, usize)>, LayoutError> {
let calls = self
.string_locations
.footnote_calls(self.footnotes.recorded)
.to_vec();
self.footnotes.recorded += calls.len();
self.footnotes.ahead.extend(calls);
let page = self.page;
let (reached, ahead) = std::mem::take(&mut self.footnotes.ahead)
.into_iter()
.partition(|(_, call)| *call <= page);
self.footnotes.ahead = ahead;
let queued: Vec<(usize, usize)> = reached
.into_iter()
.filter(|(footnote, _)| !self.footnotes.placed.contains(footnote))
.collect();
self.queue_footnotes(queued.iter().map(|&(footnote, _)| footnote))?;
Ok(queued)
}
pub(super) fn place_recorded_footnotes(&mut self) -> Result<(), LayoutError> {
let queued = self.queue_recorded_footnotes()?;
if queued.is_empty() {
return Ok(());
}
self.fill_footnote_area(false)?;
let Some(area) = self.area(self.page) else {
return Ok(());
};
for (footnote, page) in queued {
let policy = self.footnote_policy(footnote);
if policy != FootnotePolicy::Auto
&& self.footnote_waits(footnote, area.columns.width)?
&& !self
.measurer
.footnote_deferrals
.contains_key(&(footnote, page))
{
self.footnotes.deferred.insert((footnote, page), policy);
}
}
Ok(())
}
pub(super) fn place_carried_footnotes(&mut self) -> Result<(), LayoutError> {
self.fill_footnote_area(true)
}
fn fill_footnote_area(&mut self, advance: bool) -> Result<(), LayoutError> {
let page = self.page;
let Some(area) = self.area(page) else {
return Ok(());
};
let page_height = self.fragmentainers.height(page);
let room = page_height - self.y - area.edges(TOP) - area.edges(BOTTOM);
let limit = match area.maximum {
Some(maximum) if !self.footnotes_only(page) => room.min(maximum),
_ => room,
};
if self.footnotes.pages.len() <= page {
self.footnotes.pages.resize_with(page + 1, Vec::new);
}
let unbounded = self.footnotes_only(page);
let width = area.columns.width;
while let Some(&waiting) = self.footnotes.carried.front() {
let used = self.footnotes.pages[page].clone();
let holds = |paginator: &Self, part: FootnotePart| {
let mut parts = used.clone();
parts.push(part);
paginator
.area_content(&parts, &area, unbounded)
.is_some_and(|content| content.fits(limit))
};
if holds(self, waiting) {
self.record_inline_pages(waiting, page, width)?;
self.footnotes.pages[page].push(waiting);
self.footnotes.carried.pop_front();
continue;
}
let layout = self.footnote_layout(waiting.body, width)?;
let edges = layout.cloned.unwrap_or_default();
let slice = |bottom: f32| FootnotePart {
bottom,
inline: false,
repeated: [
waiting.repeated[0],
if bottom < layout.height {
edges[1]
} else {
0.0
},
],
..waiting
};
let breaks = &layout.breaks;
let candidates = &breaks[breaks.partition_point(|&offset| offset <= waiting.top)
..breaks.partition_point(|&offset| offset < waiting.bottom)];
let fitting = candidates.partition_point(|&offset| holds(self, slice(offset)));
let forced = advance && used.is_empty() && !self.has_flow;
let Some(bottom) = fitting
.checked_sub(1)
.map(|last| candidates[last])
.or_else(|| forced.then(|| candidates.first().copied().unwrap_or(waiting.bottom)))
else {
break;
};
let slice = slice(bottom);
self.record_inline_pages(slice, page, width)?;
self.footnotes.pages[page].push(slice);
if bottom >= waiting.bottom {
self.footnotes.carried.pop_front();
} else {
let rest = self
.footnotes
.carried
.front_mut()
.expect("the waiting footnote continues");
rest.top = bottom;
rest.repeated[0] = edges[0];
break;
}
}
self.height = fragmentainer_height(page_height - self.footnote_reserve(page));
Ok(())
}
pub(super) fn paint_footnotes(
&mut self,
page_count: usize,
) -> Result<FootnotePaint, LayoutError> {
let mut navigation = Vec::new();
let mut page_text: Box<[Vec<DisplayItem>]> = vec![Vec::new(); page_count].into();
for page in 0..page_count.min(self.footnotes.pages.len()) {
let area = self.area(page).expect("footnote areas belong to pages");
let unbounded = self.footnotes_only(page);
let Some(content) = self.area_content(self.footnotes.parts(page), &area, unbounded)
else {
continue;
};
let (style, decoration) = self.area_box(page).expect("footnote areas belong to pages");
let Fragmentainers::Pages(pages) = &self.fragmentainers else {
unreachable!("footnote areas belong to pages");
};
let flow = crate::style::logical::Flow::used(pages.document.root().style());
let page_clips = pages.boxes[page].decoration.backgrounds.clips_to_text();
let decoration = decoration.into_owned();
let page_area = self.fragmentainers.content_box(page);
let height = area.content_height(content.height(), unbounded);
let border_box = Rect {
x: area.x,
y: page_area.y + page_area.height
- area.margins[BOTTOM]
- area.borders[BOTTOM]
- area.padding[BOTTOM]
- height
- area.padding[TOP]
- area.borders[TOP],
width: area.width
+ area.padding[LEFT]
+ area.padding[RIGHT]
+ area.borders[LEFT]
+ area.borders[RIGHT],
height: height
+ area.padding[TOP]
+ area.padding[BOTTOM]
+ area.borders[TOP]
+ area.borders[BOTTOM],
};
let clips_to_text = decoration.backgrounds.clips_to_text();
let visible = style.visibility.is_visible();
let paint = BoxPaint {
padding: area.padding,
..BoxPaint::whole(border_box, decoration)
};
let transform = self.transform_on(&style, &paint.fragment_areas(), None, page)?;
if transform.as_ref().is_some_and(Region::collapses) {
continue;
}
let perspective = {
let frame = crate::layout::writing::Frame::for_style(&style);
style
.parent_3d(&frame.boxes(&paint.fragment_areas()))
.map(|parent| parent.mapped(frame.inverse().matrix))
};
let group = Group::of(&style);
let pushes: Vec<_> = transform
.clone()
.map(Region::push)
.into_iter()
.chain(group.pushes(std::slice::from_ref(&paint)))
.collect();
let backdrop = group.backdrop(&paint);
let mut items = Vec::new();
if visible {
items.extend(backdrop);
if paint.decoration.is_visible() {
items.push(DisplayItem::Box(paint.clone()));
}
}
let mut text = Vec::new();
let x = border_box.x + area.borders[LEFT] + area.padding[LEFT];
let y = border_box.y + area.borders[TOP] + area.padding[TOP];
if visible {
items.extend(
content
.columns
.rules(&area.columns, area.rule, [x, y])
.into_iter()
.map(DisplayItem::Box),
);
}
for (part, offset) in content.placed(area.columns) {
let layout = self.footnote_layout(part.body, area.columns.width)?;
let body_style = self.document_footnotes().bodies[part.body].block.style();
let window = Rect {
x: 0.0,
y: part.top,
width: layout.width,
height: part.bottom - part.top,
};
let origin = [x + offset[0], y + offset[1] + part.repeated[0] - part.top];
let sliced = part.top > 0.0 || part.bottom < layout.height;
let clip = slice_clip(window);
let frame = |mut items: Vec<DisplayItem>, lines: Vec<TextLine>| {
let body = layout
.body_box
.and_then(|rect| body_box(&items, rect).map(|index| (index, rect)));
if let (Some(perspective), Some((index, _))) = (perspective, body) {
project_body(
&mut items,
index,
body_style,
perspective,
[origin[0], origin[1], layout.width, layout.height],
);
}
if let Some((index, rect)) = body.filter(|_| sliced && layout.cloned.is_some())
{
let DisplayItem::Box(paint) = &mut items[index] else {
unreachable!("the body's box is a box paint");
};
let top = rect.y.max(part.top) - part.repeated[0];
let bottom = (rect.y + rect.height).min(part.bottom) + part.repeated[1];
paint.rect = Rect {
y: top,
height: bottom - top,
..paint.rect
};
paint.background_box = None;
paint.first_fragment = true;
paint.last_fragment = true;
let end = content_end(&items, index);
items.insert(end, DisplayItem::Pop);
items.insert(index + 1, clip.clone());
} else if sliced {
items.insert(0, clip.clone());
items.push(DisplayItem::Pop);
}
DisplayItem::Frame(Box::new(crate::graphics::FlowPaint {
matrix: [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]],
area: Rect {
height: layout.height,
y: 0.0,
..window
},
items,
lines,
}))
};
let shown = windowed(
&layout.flow.items,
&layout.flow.lines,
[part.top, part.bottom],
);
self.measurer.work.spend(shown.1.len())?;
items.push(frame(shown.0, shown.1));
if (clips_to_text || page_clips)
&& let Some(area) = &layout.flow.text_area
{
let shown = windowed(&area.items, &area.lines, [part.top, part.bottom]);
self.measurer.work.spend(shown.1.len())?;
text.push(frame(shown.0, shown.1));
}
let placed = layout.flow.navigation.translated(origin, window);
navigation.push((
page,
match &transform {
Some(region) => placed.transformed(region),
None => placed,
},
));
if part.top == 0.0 {
self.measurer
.element_targets
.displayed
.borrow_mut()
.adopt((*layout.flow.renderings).clone().physical(flow));
}
}
if clips_to_text
&& let Some(DisplayItem::Box(paint)) = items
.iter_mut()
.find(|item| matches!(item, DisplayItem::Box(_)))
{
paint.text_area = Some(std::sync::Arc::new(crate::graphics::FlowPaint {
matrix: IDENTITY,
area: page_area,
items: text.clone(),
lines: Vec::new(),
}));
}
if page_clips {
page_text[page] = match &transform {
Some(region) => std::iter::once(region.clone().push())
.chain(text)
.chain(std::iter::once(DisplayItem::Pop))
.collect(),
None => text,
};
}
let pops = pushes.len();
let items = pushes
.into_iter()
.chain(items)
.chain(std::iter::repeat_n(DisplayItem::Pop, pops));
let scope: PaintKey = Rc::from([FLOW_LEVEL]);
let (clip, group) = self.root_paint.unwrap_or_default();
for item in items {
self.foreground[page].push(ScopedPaint {
native: None,
visible: true,
order: self.orders.take(),
scope: scope.clone(),
clip,
group,
ink: None,
item,
navigation: BoxNavigation::default(),
});
}
}
Ok((navigation, page_text))
}
}
fn project_body(
items: &mut [DisplayItem],
index: usize,
style: &crate::style::Style,
perspective: crate::style::Parent3d,
[x, y, width, height]: [f32; 4],
) {
let DisplayItem::Box(paint) = &items[index] else {
unreachable!("the body's box is a box paint");
};
let areas = paint.fragment_areas();
let Some(push) = items[..index].iter().rposition(|item| {
matches!(
item,
DisplayItem::PushTransform(_) | DisplayItem::PushProjection(_)
)
}) else {
return;
};
let frame = crate::layout::writing::Frame::for_style(style);
let parent = perspective
.mapped([1.0, 0.0, 0.0, 1.0, -x, -y])
.mapped(frame.matrix);
let reference = crate::style::motion::Reference::rect(Rect {
x: 0.0,
y: 0.0,
width,
height,
});
if let Some(plane) = style.plane(&frame.boxes(&areas), Some(&parent), &reference) {
let plane =
crate::style::transform::conjugated(&plane, frame.inverse().matrix, frame.matrix);
items[push] = Region::projected(style.projected(&plane, &areas)).push();
}
}
fn body_box(items: &[DisplayItem], rect: Rect) -> Option<usize> {
items
.iter()
.position(|item| matches!(item, DisplayItem::Box(paint) if paint.rect == rect))
}
fn content_end(items: &[DisplayItem], index: usize) -> usize {
let depth = |item: &DisplayItem| match item {
DisplayItem::Pop => -1,
item if item.is_push() => 1,
_ => 0,
};
let open: i32 = items[..index].iter().map(depth).sum();
let mut level = open;
for (offset, item) in items[index + 1..].iter().enumerate() {
level += depth(item);
if level < open {
return index + 1 + offset;
}
}
items.len()
}
type FootnotePaint = (Vec<(usize, StandaloneNavigation)>, Box<[Vec<DisplayItem>]>);