Files
texrra-engine/crates/texrra-tex-core/src/execution/vertical_break.rs
T

191 lines
6.7 KiB
Rust

//! Native vert_break and prune_page_top over original vertical nodes.
use super::*;
const AWFUL: i64 = 1_073_741_823;
pub(super) fn log_ignored_shrink(
files: &mut impl SourceProvider,
diagnostic_bytes: &mut usize,
limit: usize,
) -> Result<(), String> {
const TEXT: &str = "\nignored: Infinite glue shrinkage found in box being split";
*diagnostic_bytes = diagnostic_bytes.saturating_add(TEXT.len());
if *diagnostic_bytes > limit {
return Err("native diagnostic output limit exceeded".into());
}
files.observe_diagnostic(WriteDestination::Log, TEXT)
}
fn step(steps: &mut usize, limit: usize) -> Result<(), String> {
*steps = steps.saturating_add(1);
if *steps > limit {
return Err("native vertical break step limit exceeded".into());
}
Ok(())
}
pub(super) struct VerticalBreak {
pub index: usize,
pub natural_size: i64,
}
pub(super) fn find_break(
nodes: &mut [NativeVerticalNode],
height: i64,
max_depth: i32,
state: &State,
reads: &mut super::DependencyReads,
steps: &mut usize,
limit: usize,
mut ignored_shrink: impl FnMut() -> Result<(), String>,
) -> Result<VerticalBreak, String> {
let (mut total, mut depth) = (0i64, 0i64);
let (mut stretch, mut shrink) = ([0i64; 4], 0i64);
let mut least = AWFUL;
let mut best = VerticalBreak {
index: nodes.len(),
natural_size: 0,
};
for i in 0..=nodes.len() {
step(steps, limit)?;
let node = nodes.get(i);
let penalty = match node {
None => Some(-10000),
Some(n) if n.dimensions().is_some() => {
let (_, h, d) = n.dimensions().unwrap();
total += depth + h;
depth = d;
None
}
Some(
NativeVerticalNode::Glue(_)
| NativeVerticalNode::NamedGlue(_, _)
| NativeVerticalNode::Leaders(_),
) => (i > 0
&& matches!(
nodes[i - 1],
NativeVerticalNode::Box(_)
| NativeVerticalNode::Line(_)
| NativeVerticalNode::Rule(_)
| NativeVerticalNode::Mark(_)
| NativeVerticalNode::Insertion(_)
| NativeVerticalNode::Output(_)
))
.then_some(0),
Some(NativeVerticalNode::Kern(_) | NativeVerticalNode::ImplicitKern(_)) => matches!(
nodes.get(i + 1),
Some(
NativeVerticalNode::Glue(_)
| NativeVerticalNode::NamedGlue(_, _)
| NativeVerticalNode::Leaders(_)
)
)
.then_some(0),
Some(NativeVerticalNode::Penalty(p)) => Some(*p),
Some(
NativeVerticalNode::Mark(_)
| NativeVerticalNode::Insertion(_)
| NativeVerticalNode::Output(_),
) => None,
Some(_) => unreachable!("all vertical material has dimensions or a known type"),
};
if let Some(p) = penalty.filter(|p| *p < 10000) {
let bad = if total < height {
if stretch[1..].iter().any(|n| *n != 0) {
0
} else {
i64::from(super::paragraph::tex_badness(height - total, stretch[0]))
}
} else if total - height > shrink {
AWFUL
} else {
i64::from(super::paragraph::tex_badness(total - height, shrink))
};
let cost = if bad == AWFUL {
AWFUL
} else if p <= -10000 {
i64::from(p)
} else if bad < 10000 {
bad + i64::from(p)
} else {
100000
};
if cost <= least {
best = VerticalBreak {
index: i,
natural_size: total + depth,
};
least = cost;
}
if cost == AWFUL || p <= -10000 {
return Ok(best);
}
}
match nodes.get_mut(i) {
Some(
NativeVerticalNode::Glue(g)
| NativeVerticalNode::NamedGlue(g, _)
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }),
) => {
if g.shrink.order != 0 && g.shrink.amount != 0 {
reads.insert(Dependency::Parameter(Parameter::Ignoreprimitiveerror));
if state.parameter(Parameter::Ignoreprimitiveerror).width() & 1 == 0 {
return Err("infinite glue shrinkage found in native box being split".into());
}
ignored_shrink()?;
g.shrink.order = 0;
}
stretch[g.stretch.order as usize] += i64::from(g.stretch.amount);
shrink += i64::from(g.shrink.amount);
total += depth + i64::from(g.width);
depth = 0;
}
Some(NativeVerticalNode::Kern(w) | NativeVerticalNode::ImplicitKern(w)) => {
total += depth + i64::from(*w);
depth = 0;
}
_ => {}
}
// TeX applies this even after glue/kern/mark when max_depth is negative.
if depth > i64::from(max_depth) {
total += depth - i64::from(max_depth);
depth = i64::from(max_depth);
}
}
unreachable!("the implicit forced end break terminates the search")
}
pub(super) fn prune_top(
nodes: Vec<NativeVerticalNode>,
mut skip: Glue,
mut discards: Option<&mut Vec<NativeVerticalNode>>,
steps: &mut usize,
limit: usize,
) -> Result<Vec<NativeVerticalNode>, String> {
let mut result = Vec::new();
let mut nodes = nodes.into_iter();
for node in nodes.by_ref() {
step(steps, limit)?;
if let Some((_, h, _)) = node.dimensions() {
skip.canonical_zero = false;
skip.width = i32::try_from((i64::from(skip.width) - h).max(0))
.map_err(|_| "native split top skip dimension overflow")?;
result.push(NativeVerticalNode::NamedGlue(skip, Parameter::Splittopskip));
result.push(node);
result.extend(nodes);
break;
}
if matches!(
node,
NativeVerticalNode::Mark(_)
| NativeVerticalNode::Insertion(_)
| NativeVerticalNode::Output(_)
) {
result.push(node);
} else if let Some(discards) = discards.as_deref_mut() {
discards.push(node);
}
}
Ok(result)
}