Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,154 @@
|
||||
//! Native vert_break and prune_page_top over original vertical nodes.
|
||||
use super::*;
|
||||
|
||||
const AWFUL: i64 = 1_073_741_823;
|
||||
|
||||
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: &[NativeVerticalNode],
|
||||
height: i64,
|
||||
max_depth: i32,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
) -> 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(_, _)) => (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(_, _))
|
||||
)
|
||||
.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 node {
|
||||
Some(NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _)) => {
|
||||
if g.shrink.order != 0 && g.shrink.amount != 0 {
|
||||
return Err("infinite glue shrinkage found in native box being split".into());
|
||||
}
|
||||
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,
|
||||
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);
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
Reference in new issue
Block a user