372 lines
14 KiB
Rust
372 lines
14 KiB
Rust
//! Online TeX page construction over owned contributions, independent of paint.
|
|
use super::*;
|
|
use std::collections::{BTreeSet, VecDeque};
|
|
const AWFUL: i64 = 1_073_741_823;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum PageTrace {
|
|
Freeze {
|
|
goal: i64,
|
|
max_depth: i32,
|
|
},
|
|
Break {
|
|
dimensions: [i64; 8],
|
|
badness: i64,
|
|
penalty: i32,
|
|
cost: i64,
|
|
best: bool,
|
|
},
|
|
InsertionSplit {
|
|
class: u8,
|
|
target: i64,
|
|
natural_size: i64,
|
|
penalty: i32,
|
|
},
|
|
}
|
|
impl PageTrace {
|
|
pub fn text(&self) -> String {
|
|
use super::registers::print_scaled_wide as scaled;
|
|
match self {
|
|
Self::Freeze { goal, max_depth } => format!(
|
|
"%% goal height={}, max depth={}",
|
|
scaled(*goal),
|
|
scaled(i64::from(*max_depth))
|
|
),
|
|
Self::InsertionSplit {
|
|
class,
|
|
target,
|
|
natural_size,
|
|
penalty,
|
|
} => format!(
|
|
"% split{class} to {},{} p={penalty}",
|
|
scaled(*target),
|
|
scaled(*natural_size)
|
|
),
|
|
Self::Break {
|
|
dimensions: d,
|
|
badness,
|
|
penalty,
|
|
cost,
|
|
best,
|
|
} => {
|
|
let mut total = scaled(d[1]);
|
|
for (i, suffix) in ["", "fil", "fill", "filll"].iter().enumerate() {
|
|
if d[2 + i] != 0 {
|
|
total.push_str(&format!(" plus {}{suffix}", scaled(d[2 + i])));
|
|
}
|
|
}
|
|
if d[6] != 0 {
|
|
total.push_str(&format!(" minus {}", scaled(d[6])));
|
|
}
|
|
let bad = if *badness == AWFUL {
|
|
"*".into()
|
|
} else {
|
|
badness.to_string()
|
|
};
|
|
let cost = if *cost == AWFUL {
|
|
"*".into()
|
|
} else {
|
|
cost.to_string()
|
|
};
|
|
format!(
|
|
"% t={total} g={} b={bad} p={penalty} c={cost}{}",
|
|
scaled(d[0]),
|
|
if *best { "#" } else { "" }
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct PageTraceReport {
|
|
pub trace: PageTrace,
|
|
pub text: String,
|
|
pub destination: WriteDestination,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct NativePageState {
|
|
/// goal, total, four stretch orders, shrink and depth in TeX sp.
|
|
pub dimensions: [i64; 8],
|
|
pub started: bool,
|
|
#[serde(default)]
|
|
specs_frozen: bool,
|
|
pub max_depth: i32,
|
|
pub nodes: Vec<NativeVerticalNode>,
|
|
pub contributions: VecDeque<NativeVerticalNode>,
|
|
pub dead_cycles: i32,
|
|
pub output_active: bool,
|
|
#[serde(default)]
|
|
pub(super) last_glue: Option<Glue>,
|
|
#[serde(default)]
|
|
pub(super) last_kern: i32,
|
|
#[serde(default)]
|
|
pub(super) last_penalty: i32,
|
|
#[serde(default)]
|
|
pub(super) last_node_type: Option<i32>,
|
|
pub(super) requested: bool,
|
|
pub(super) best: Option<(usize, i32)>,
|
|
pub(super) least: i64,
|
|
#[serde(default)]
|
|
pub(super) insertions: super::insertions::PageInsertions,
|
|
}
|
|
pub(super) struct PageBreak {
|
|
pub nodes: Vec<NativeVerticalNode>,
|
|
pub goal: i32,
|
|
pub max_depth: i32,
|
|
pub penalty: i32,
|
|
}
|
|
impl NativePageState {
|
|
pub fn dimension(&self, index: usize) -> i64 {
|
|
if !self.started && !self.specs_frozen && !self.output_active {
|
|
if index == 0 { AWFUL } else { 0 }
|
|
} else {
|
|
self.dimensions[index]
|
|
}
|
|
}
|
|
fn reset(&mut self) {
|
|
self.started = false;
|
|
self.specs_frozen = false;
|
|
self.last_glue = None;
|
|
self.last_kern = 0;
|
|
self.last_penalty = 0;
|
|
self.last_node_type = None;
|
|
self.max_depth = 0;
|
|
self.dimensions[7] = 0;
|
|
self.best = None;
|
|
self.least = AWFUL;
|
|
}
|
|
fn freeze_specs(&mut self, state: &State, trace: &mut Vec<PageTrace>) {
|
|
if self.specs_frozen {
|
|
return;
|
|
}
|
|
self.specs_frozen = true;
|
|
self.dimensions = [0; 8];
|
|
self.dimensions[0] = i64::from(state.parameter(Parameter::Vsize).width());
|
|
self.max_depth = state.parameter(Parameter::Maxdepth).width();
|
|
self.least = AWFUL;
|
|
self.best = None;
|
|
self.insertions.penalties = 0;
|
|
if state.parameter(Parameter::Tracingpages).width() > 0 {
|
|
trace.push(PageTrace::Freeze {
|
|
goal: self.dimensions[0],
|
|
max_depth: self.max_depth,
|
|
});
|
|
}
|
|
}
|
|
|
|
pub(super) fn resume_contributions(&mut self, output_material: Vec<NativeVerticalNode>) {
|
|
let held = self.insertions.held.drain(..);
|
|
for node in held.chain(output_material).rev() {
|
|
self.contributions.push_front(node);
|
|
}
|
|
}
|
|
|
|
pub(super) fn advance(
|
|
&mut self,
|
|
state: &mut State,
|
|
reads: &mut BTreeSet<Dependency>,
|
|
steps: &mut usize,
|
|
limit: usize,
|
|
trace: &mut Vec<PageTrace>,
|
|
) -> Result<Option<PageBreak>, String> {
|
|
if !self.requested || self.output_active {
|
|
return Ok(None);
|
|
}
|
|
self.requested = false;
|
|
while let Some(front) = self.contributions.front() {
|
|
*steps = steps.saturating_add(1);
|
|
if *steps > limit {
|
|
return Err("native page construction step limit exceeded".into());
|
|
}
|
|
self.last_glue = match front {
|
|
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => Some(*g),
|
|
_ => None,
|
|
};
|
|
self.last_node_type = Some(front.node_type());
|
|
self.last_kern = match front {
|
|
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => *n,
|
|
_ => 0,
|
|
};
|
|
self.last_penalty = match front {
|
|
NativeVerticalNode::Penalty(n) => *n,
|
|
_ => 0,
|
|
};
|
|
let dimensions = front.dimensions();
|
|
if !self.started && dimensions.is_some() {
|
|
self.started = true;
|
|
self.freeze_specs(state, trace);
|
|
let mut skip = state.parameter(Parameter::Topskip).glue();
|
|
skip.canonical_zero = false;
|
|
skip.width = i32::try_from((i64::from(skip.width) - dimensions.unwrap().1).max(0))
|
|
.map_err(|_| "native page topskip overflow")?;
|
|
self.contributions
|
|
.push_front(NativeVerticalNode::NamedGlue(skip, Parameter::Topskip));
|
|
continue;
|
|
}
|
|
let penalty = match front {
|
|
NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _) => {
|
|
if !self.started {
|
|
self.contributions.pop_front();
|
|
continue;
|
|
}
|
|
self.nodes
|
|
.last()
|
|
.filter(|n| {
|
|
matches!(
|
|
n,
|
|
NativeVerticalNode::Box(_)
|
|
| NativeVerticalNode::Line(_)
|
|
| NativeVerticalNode::Rule(_)
|
|
| NativeVerticalNode::Mark(_)
|
|
| NativeVerticalNode::Insertion(_)
|
|
| NativeVerticalNode::Output(_)
|
|
)
|
|
})
|
|
.map(|_| 0)
|
|
}
|
|
NativeVerticalNode::Kern(_) | NativeVerticalNode::ImplicitKern(_) => {
|
|
if !self.started {
|
|
self.contributions.pop_front();
|
|
continue;
|
|
}
|
|
if self.contributions.len() == 1 {
|
|
return Ok(None);
|
|
}
|
|
matches!(
|
|
self.contributions.get(1),
|
|
Some(NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _))
|
|
)
|
|
.then_some(0)
|
|
}
|
|
NativeVerticalNode::Penalty(p) => {
|
|
if !self.started {
|
|
self.contributions.pop_front();
|
|
continue;
|
|
}
|
|
Some(*p)
|
|
}
|
|
_ => None,
|
|
};
|
|
if let Some(p) = penalty.filter(|p| *p < 10000) {
|
|
let total = self.dimensions[1];
|
|
let goal = self.dimensions[0];
|
|
let shrink = self.dimensions[6];
|
|
let bad = if total < goal {
|
|
if self.dimensions[3..6].iter().any(|v| *v != 0) {
|
|
0
|
|
} else {
|
|
i64::from(super::paragraph::tex_badness(
|
|
goal - total,
|
|
self.dimensions[2],
|
|
))
|
|
}
|
|
} else if total - goal > shrink {
|
|
AWFUL
|
|
} else {
|
|
i64::from(super::paragraph::tex_badness(total - goal, shrink))
|
|
};
|
|
let cost = if bad == AWFUL || self.insertions.penalties >= 10000 {
|
|
AWFUL
|
|
} else if p <= -10000 {
|
|
i64::from(p)
|
|
} else if bad < 10000 {
|
|
bad + i64::from(p) + i64::from(self.insertions.penalties)
|
|
} else {
|
|
100000
|
|
};
|
|
if state.parameter(Parameter::Tracingpages).width() > 0 {
|
|
trace.push(PageTrace::Break {
|
|
dimensions: self.dimensions,
|
|
badness: bad,
|
|
penalty: p,
|
|
cost,
|
|
best: cost <= self.least,
|
|
});
|
|
}
|
|
if cost <= self.least {
|
|
self.least = cost;
|
|
let goal = i32::try_from(goal).map_err(|_| "native page goal overflow")?;
|
|
self.best = Some((self.nodes.len(), goal));
|
|
self.insertions.record_best();
|
|
}
|
|
if cost == AWFUL || p <= -10000 {
|
|
let (cut, size) = self.best.expect("first candidate is always recorded");
|
|
let breakpoint = if cut == self.nodes.len() {
|
|
self.contributions.front_mut()
|
|
} else {
|
|
self.nodes.get_mut(cut)
|
|
};
|
|
let output_penalty = if let Some(NativeVerticalNode::Penalty(p)) = breakpoint {
|
|
let old = *p;
|
|
*p = 10000;
|
|
old
|
|
} else {
|
|
10000
|
|
};
|
|
let mut page = std::mem::take(&mut self.nodes);
|
|
let tail = page.split_off(cut);
|
|
for n in tail.into_iter().rev() {
|
|
self.contributions.push_front(n);
|
|
}
|
|
let page = self.insertions.harvest(page, state, reads, steps, limit)?;
|
|
let result = PageBreak {
|
|
nodes: page,
|
|
goal: size,
|
|
max_depth: self.max_depth,
|
|
penalty: output_penalty,
|
|
};
|
|
self.reset();
|
|
self.requested = true;
|
|
return Ok(Some(result));
|
|
}
|
|
}
|
|
let n = self.contributions.pop_front().unwrap();
|
|
if let NativeVerticalNode::Insertion(insertion) = &n {
|
|
self.freeze_specs(state, trace);
|
|
self.insertions.record_front(
|
|
insertion,
|
|
self.nodes.len(),
|
|
&mut self.dimensions,
|
|
state,
|
|
reads,
|
|
steps,
|
|
limit,
|
|
trace,
|
|
)?;
|
|
}
|
|
if let Some((_, height, depth)) = dimensions {
|
|
self.dimensions[1] += self.dimensions[7] + height;
|
|
self.dimensions[7] = depth;
|
|
} else {
|
|
match &n {
|
|
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
|
if g.shrink.order != 0 && g.shrink.amount != 0 {
|
|
return Err(
|
|
"infinite glue shrinkage found on native current page".into()
|
|
);
|
|
}
|
|
self.dimensions[2 + g.stretch.order as usize] +=
|
|
i64::from(g.stretch.amount);
|
|
self.dimensions[6] += i64::from(g.shrink.amount);
|
|
self.dimensions[1] += self.dimensions[7] + i64::from(g.width);
|
|
self.dimensions[7] = 0;
|
|
}
|
|
NativeVerticalNode::Kern(w) | NativeVerticalNode::ImplicitKern(w) => {
|
|
self.dimensions[1] += self.dimensions[7] + i64::from(*w);
|
|
self.dimensions[7] = 0;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
if self.dimensions[7] > i64::from(self.max_depth) {
|
|
self.dimensions[1] += self.dimensions[7] - i64::from(self.max_depth);
|
|
self.dimensions[7] = i64::from(self.max_depth);
|
|
}
|
|
self.nodes.push(n);
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|