use crate::ast::document::{Fold, Page};
use crate::ast::value::dim_to_px;
use crate::diagnostics::Diagnostic;
use super::nodes::{node_bbox, node_id_and_span};
fn fold_position_px(fold: &Fold) -> Option<f64> {
let pos = fold.position.as_ref()?;
dim_to_px(pos.value, &pos.unit)
}
pub(super) fn check_folds(
page: &Page,
page_w: f64,
page_h: f64,
diagnostics: &mut Vec<Diagnostic>,
) {
for fold in &page.folds {
let Some(position) = fold_position_px(fold) else {
continue;
};
let is_horizontal = fold.orientation == "horizontal";
for node in &page.children {
let Some((nx, ny, nw, nh)) = node_bbox(node, page_w, page_h) else {
continue;
};
let crosses = if is_horizontal {
ny < position && position < ny + nh
} else {
nx < position && position < nx + nw
};
if crosses {
let (node_id, node_span) = node_id_and_span(node);
let axis = if is_horizontal {
"horizontal"
} else {
"vertical"
};
diagnostics.push(Diagnostic::advisory(
"fold.content_crossing",
format!(
"node '{}' crosses {} fold '{}' at position {}",
node_id, axis, fold.id, position
),
node_span,
Some(node_id.to_owned()),
));
}
}
}
}