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,248 @@
|
||||
use super::*;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
/// Insertion material and the values captured inside its restricted group.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct NativeInsertion {
|
||||
pub class: u8,
|
||||
pub natural_size: i64,
|
||||
pub split_top: Glue,
|
||||
pub split_depth: i32,
|
||||
pub float_cost: i32,
|
||||
pub nodes: Vec<NativeVerticalNode>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
struct Placement {
|
||||
last: usize,
|
||||
split_at: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
struct InsertionClass {
|
||||
size: i64,
|
||||
placement: Placement,
|
||||
split: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub(super) struct PageInsertions {
|
||||
classes: BTreeMap<u8, InsertionClass>,
|
||||
best: BTreeMap<u8, Placement>,
|
||||
pub penalties: i32,
|
||||
pub held: Vec<NativeVerticalNode>,
|
||||
}
|
||||
|
||||
fn vertical_box(state: &State, index: u16) -> Result<Option<&NativeBox>, String> {
|
||||
let value = state.box_register(index);
|
||||
if value.is_some_and(|b| !matches!(b.content, BoxContent::Vertical(_))) {
|
||||
return Err(format!("native insertion box {index} must be a vbox"));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn scale(size: i64, factor: i64) -> i64 {
|
||||
if factor == 1000 {
|
||||
size
|
||||
} else {
|
||||
size / 1000 * factor
|
||||
}
|
||||
}
|
||||
|
||||
impl PageInsertions {
|
||||
pub fn record_front(
|
||||
&mut self,
|
||||
node: &NativeInsertion,
|
||||
at: usize,
|
||||
dims: &mut [i64; 8],
|
||||
state: &State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
trace: &mut Vec<PageTrace>,
|
||||
) -> Result<(), String> {
|
||||
let index = u16::from(node.class);
|
||||
let placement = Placement {
|
||||
last: at,
|
||||
split_at: None,
|
||||
};
|
||||
let entry = match self.classes.entry(node.class) {
|
||||
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
|
||||
std::collections::btree_map::Entry::Vacant(slot) => {
|
||||
let skip = Register {
|
||||
index,
|
||||
kind: RegisterKind::Skip,
|
||||
};
|
||||
reads.extend([
|
||||
Dependency::BoxRegister(index),
|
||||
Dependency::Register(skip),
|
||||
Dependency::Count(index),
|
||||
]);
|
||||
let size = vertical_box(state, index)?.map_or(0, |b| b.height + b.depth);
|
||||
let RegisterValue::Glue(skip) = state.register(skip) else {
|
||||
unreachable!("skip register is glue");
|
||||
};
|
||||
if skip.shrink.order != 0 && skip.shrink.amount != 0 {
|
||||
return Err("infinite glue shrinkage in native insertion skip".into());
|
||||
}
|
||||
dims[0] -= scale(size, i64::from(state.count(index))) + i64::from(skip.width);
|
||||
dims[2 + usize::from(skip.stretch.order)] += i64::from(skip.stretch.amount);
|
||||
dims[6] += i64::from(skip.shrink.amount);
|
||||
slot.insert(InsertionClass {
|
||||
size,
|
||||
placement,
|
||||
split: false,
|
||||
})
|
||||
}
|
||||
};
|
||||
if entry.split {
|
||||
self.penalties = self.penalties.saturating_add(node.float_cost);
|
||||
return Ok(());
|
||||
}
|
||||
entry.placement = placement;
|
||||
let max = Register {
|
||||
index,
|
||||
kind: RegisterKind::Dimen,
|
||||
};
|
||||
reads.extend([Dependency::Register(max), Dependency::Count(index)]);
|
||||
let max = i64::from(state.register(max).width());
|
||||
let factor = i64::from(state.count(index));
|
||||
let space = dims[0] - dims[1] - dims[7];
|
||||
let scaled = scale(node.natural_size, factor);
|
||||
if (scaled <= 0 || scaled <= space + dims[6]) && entry.size + node.natural_size <= max {
|
||||
dims[0] -= scaled;
|
||||
entry.size += node.natural_size;
|
||||
return Ok(());
|
||||
}
|
||||
let target = if factor <= 0 {
|
||||
1_073_741_823
|
||||
} else if factor == 1000 {
|
||||
space
|
||||
} else {
|
||||
space / factor * 1000
|
||||
}
|
||||
.min(max - entry.size);
|
||||
let split =
|
||||
super::vertical_break::find_break(&node.nodes, target, node.split_depth, steps, limit)?;
|
||||
let penalty = match node.nodes.get(split.index) {
|
||||
None => -10000,
|
||||
Some(NativeVerticalNode::Penalty(p)) => *p,
|
||||
_ => 0,
|
||||
};
|
||||
if state.parameter(Parameter::Tracingpages).width() > 0 {
|
||||
trace.push(PageTrace::InsertionSplit {
|
||||
class: node.class,
|
||||
target,
|
||||
natural_size: split.natural_size,
|
||||
penalty,
|
||||
});
|
||||
}
|
||||
entry.size += split.natural_size;
|
||||
dims[0] -= scale(split.natural_size, factor);
|
||||
entry.split = true;
|
||||
entry.placement.split_at = Some(split.index);
|
||||
self.penalties = self.penalties.saturating_add(penalty);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn record_best(&mut self) {
|
||||
self.best.clear();
|
||||
self.best.extend(
|
||||
self.classes
|
||||
.iter()
|
||||
.map(|(&class, info)| (class, info.placement)),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn harvest(
|
||||
&mut self,
|
||||
page: Vec<NativeVerticalNode>,
|
||||
state: &mut State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
) -> Result<Vec<NativeVerticalNode>, String> {
|
||||
self.classes.clear();
|
||||
self.penalties = 0;
|
||||
if !page
|
||||
.iter()
|
||||
.any(|n| matches!(n, NativeVerticalNode::Insertion(_)))
|
||||
{
|
||||
self.best.clear();
|
||||
return Ok(page);
|
||||
}
|
||||
reads.insert(Dependency::Parameter(Parameter::Holdinginserts));
|
||||
if state.parameter(Parameter::Holdinginserts).width() > 0 {
|
||||
self.best.clear();
|
||||
return Ok(page);
|
||||
}
|
||||
let mut result = Vec::with_capacity(page.len());
|
||||
let mut boxes: BTreeMap<u8, (Span, Vec<NativeVerticalNode>)> = BTreeMap::new();
|
||||
for (at, node) in page.into_iter().enumerate() {
|
||||
let NativeVerticalNode::Insertion(mut insertion) = node else {
|
||||
result.push(node);
|
||||
continue;
|
||||
};
|
||||
let Some(p) = self.best.get(&insertion.class).filter(|p| at <= p.last) else {
|
||||
self.held.push(NativeVerticalNode::Insertion(insertion));
|
||||
continue;
|
||||
};
|
||||
let nodes = &mut match boxes.entry(insertion.class) {
|
||||
std::collections::btree_map::Entry::Occupied(slot) => slot.into_mut(),
|
||||
std::collections::btree_map::Entry::Vacant(slot) => {
|
||||
let index = u16::from(insertion.class);
|
||||
reads.insert(Dependency::BoxRegister(index));
|
||||
vertical_box(state, index)?;
|
||||
let original = if let Some(b) = state.take_box(index) {
|
||||
let BoxContent::Vertical(nodes) = b.content else {
|
||||
unreachable!("checked vbox");
|
||||
};
|
||||
nodes
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
slot.insert((insertion.span, original))
|
||||
}
|
||||
}
|
||||
.1;
|
||||
if let Some(cut) = p
|
||||
.split_at
|
||||
.filter(|&cut| at == p.last && cut < insertion.nodes.len())
|
||||
{
|
||||
let tail = insertion.nodes.split_off(cut);
|
||||
nodes.append(&mut insertion.nodes);
|
||||
let tail =
|
||||
super::vertical_break::prune_top(tail, insertion.split_top, steps, limit)?;
|
||||
if !tail.is_empty() {
|
||||
let b = package(tail, insertion.span)?;
|
||||
insertion.natural_size = b.height + b.depth;
|
||||
let BoxContent::Vertical(nodes) = b.content else {
|
||||
unreachable!()
|
||||
};
|
||||
insertion.nodes = nodes;
|
||||
self.held.push(NativeVerticalNode::Insertion(insertion));
|
||||
}
|
||||
} else {
|
||||
nodes.append(&mut insertion.nodes);
|
||||
}
|
||||
}
|
||||
self.penalties =
|
||||
i32::try_from(self.held.len()).map_err(|_| "too many held native insertions")?;
|
||||
self.best.clear();
|
||||
for (class, (span, nodes)) in boxes {
|
||||
state.set_box(u16::from(class), Some(package(nodes, span)?));
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn package(nodes: Vec<NativeVerticalNode>, span: Span) -> Result<NativeBox, String> {
|
||||
super::boxes::vpack(
|
||||
nodes,
|
||||
super::boxes::PackSpec::Natural,
|
||||
1_073_741_823,
|
||||
BoxKind::Vertical,
|
||||
span,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user