use super::{ActiveBox, DisplayItem, Paginator, Rect, Region};
use crate::style::{Overflow, Style, StyleError};
impl Paginator<'_, '_, '_> {
pub(super) fn record_overflow(&mut self, rect: Rect) {
self.record_overflow_on(self.page, rect);
}
pub(super) fn record_overflow_on(&mut self, page: usize, rect: Rect) {
if let Some(bounds) = self
.boxes
.last_mut()
.and_then(|active| active.overflow.as_mut())
.or(self.root_overflow.as_mut())
{
bounds
.entry(page)
.and_modify(|area| *area = area.union(rect))
.or_insert(rect);
}
}
pub(super) fn propagate_overflow(
&mut self,
active: &ActiveBox<'_>,
style: &Style,
) -> Result<(), StyleError> {
if self
.boxes
.last()
.is_none_or(|parent| parent.overflow.is_none())
&& self.root_overflow.is_none()
{
return Ok(());
}
for &(page, slot) in &active.fragments {
let DisplayItem::Box(paint) = &self.display_lists[page][slot].item else {
continue;
};
let area = box_bounds(active, style, paint, page)?;
if area.width > 0.0 && area.height > 0.0 {
if let Some(parent) = self.boxes.last()
&& let Some((_, parent_slot)) = parent
.fragments
.iter()
.find(|(fragment, _)| *fragment == page)
&& let DisplayItem::Box(parent_paint) =
&self.display_lists[page][*parent_slot].item
{
let padding = parent_paint.inner_clip([0.0; 4], [true; 2]).rect;
let rtl = parent
.geometry
.as_ref()
.is_some_and(|geometry| geometry.source.style().rtl);
let before_inline_start = if rtl {
area.x >= padding.x + padding.width
} else {
area.x + area.width <= padding.x
};
if before_inline_start || area.y + area.height <= padding.y {
continue;
}
}
self.boxes
.last_mut()
.and_then(|parent| parent.overflow.as_mut())
.or(self.root_overflow.as_mut())
.expect("overflow tracking was checked before propagation")
.entry(page)
.and_modify(|bounds| *bounds = bounds.union(area))
.or_insert(area);
}
}
Ok(())
}
}
pub(super) fn transformed_bounds(area: Rect, matrix: [f32; 6]) -> Rect {
crate::layout::navigation::page_polygon(area, &[Region::Transform(matrix)], false)
.into_iter()
.map(|[x, y]| Rect {
x,
y,
width: 0.0,
height: 0.0,
})
.fold(area, Rect::union)
}
pub(super) fn box_bounds(
active: &ActiveBox<'_>,
style: &Style,
paint: &super::BoxPaint,
page: usize,
) -> Result<Rect, StyleError> {
let border = paint.rect;
let mut area = border;
if let Some(descendants) = active
.overflow
.as_ref()
.and_then(|bounds| bounds.get(&page))
{
let padding = descendants.intersection(paint.inner_clip([0.0; 4], [true; 2]).rect);
let edge = descendants.intersection(style.overflow_clip_edge(paint, [true; 2]).rect);
let mut descendants = *descendants;
for (axis, overflow) in style.overflow.into_iter().enumerate() {
let clipped = match overflow {
Overflow::Visible => continue,
Overflow::Clip => edge,
Overflow::Hidden | Overflow::Auto | Overflow::Scroll => padding,
};
if axis == 0 {
descendants.x = clipped.x;
descendants.width = clipped.width;
} else {
descendants.y = clipped.y;
descendants.height = clipped.height;
}
}
if descendants.width > 0.0 && descendants.height > 0.0 {
area = area.union(descendants);
}
}
if let Some(geometry) = &active.geometry {
let margins = style.margin.each_ref().map(|margin| {
margin
.resolve(geometry.horizontal.containing_width)
.unwrap_or(0.0)
});
let margin = Rect {
x: border.x - margins[3],
y: border.y - margins[0],
width: (border.width + margins[1] + margins[3]).max(0.0),
height: (border.height + margins[0] + margins[2]).max(0.0),
};
area = area.union(margin);
}
if let Some(matrix) = style.transform_matrix(&paint.fragment_areas(), None)? {
area = transformed_bounds(area, matrix);
}
Ok(area)
}