use super::layer_tree::{CacheHint, ClipKind, GroupKind, LayerNode, LayerNodeKind};
use super::render_tree::{NodeId, RenderNode, TableCellNode};
use std::collections::HashSet;
pub(super) fn build(
node: &RenderNode,
cell: &TableCellNode,
children: Vec<LayerNode>,
cache_hint: CacheHint,
background_ids: &HashSet<NodeId>,
) -> LayerNode {
let owns_physical_clip = |child: &LayerNode| {
child
.source_node_id
.is_some_and(|id| background_ids.contains(&id))
&& matches!(
&child.kind,
LayerNodeKind::ClipRect {
clip_kind: ClipKind::PrintBitmapFill,
..
}
)
};
let clip_contents = |children, group_kind, source| {
LayerNode::clip_rect(
node.bbox,
Some(node.id),
node.bbox,
LayerNode::group(node.bbox, source, children, cache_hint, group_kind),
ClipKind::TableCell,
)
.with_layer(node.layer)
};
if cell.page_fragment || !children.iter().any(owns_physical_clip) {
return clip_contents(children, GroupKind::TableCell(cell.clone()), Some(node.id));
}
let mut output = Vec::new();
let mut contents = Vec::new();
for child in children {
if owns_physical_clip(&child) {
if !contents.is_empty() {
output.push(clip_contents(
std::mem::take(&mut contents),
GroupKind::Generic,
None,
));
}
output.push(child);
} else {
contents.push(child);
}
}
if !contents.is_empty() {
output.push(clip_contents(contents, GroupKind::Generic, None));
}
LayerNode::group(
node.bbox,
Some(node.id),
output,
cache_hint,
GroupKind::TableCell(cell.clone()),
)
.with_layer(node.layer)
}