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

208 lines
7.6 KiB
Rust

//! Owned list-tail cursors, independent of the list's enclosing root vector.
use super::*;
pub(in crate::execution) enum TailNodeRef<'a> {
Vertical(&'a NativeVerticalNode),
Text(&'a TextNode),
}
enum Removed {
Vertical(NativeVerticalNode),
Text(TextNode),
}
fn vertical_box(n: &NativeVerticalNode) -> &NativeBox {
match n {
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => b,
_ => unreachable!("owned list cursor must enter a real box"),
}
}
fn vertical_box_mut(n: &mut NativeVerticalNode) -> &mut NativeBox {
match n {
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => b,
_ => unreachable!("owned list cursor must enter a real box"),
}
}
fn text_box(n: &TextNode) -> &NativeBox {
match n {
TextNode::Box(b) => b.native_box.as_deref().expect("owned list cursor box"),
_ => unreachable!("owned list cursor must enter a real box"),
}
}
fn text_box_mut(n: &mut TextNode) -> &mut NativeBox {
match n {
TextNode::Box(b) => b.native_box.as_deref_mut().expect("owned list cursor box"),
_ => unreachable!("owned list cursor must enter a real box"),
}
}
fn descend<'a>(b: &'a NativeBox, path: &[usize]) -> &'a NativeBox {
match path.split_first() {
None => b,
Some((&i, rest)) => descend(
match &b.content {
BoxContent::Vertical(v) => vertical_box(&v[i]),
BoxContent::Horizontal(v) => text_box(&v[i]),
},
rest,
),
}
}
fn descend_mut<'a>(b: &'a mut NativeBox, path: &[usize]) -> &'a mut NativeBox {
match path.split_first() {
None => b,
Some((&i, rest)) => descend_mut(
match &mut b.content {
BoxContent::Vertical(v) => vertical_box_mut(&mut v[i]),
BoxContent::Horizontal(v) => text_box_mut(&mut v[i]),
},
rest,
),
}
}
fn refresh(b: &mut NativeBox, path: &[usize]) {
if let Some((&i, rest)) = path.split_first() {
let child = match &mut b.content {
BoxContent::Vertical(v) => vertical_box_mut(&mut v[i]),
BoxContent::Horizontal(v) => text_box_mut(&mut v[i]),
};
refresh(child, rest);
}
if let BoxContent::Horizontal(v) = &b.content {
b.diagnostic = crate::execution::diagnostics::text_nodes(v);
}
}
fn horizontal(n: NativeVerticalNode) -> TextNode {
match n {
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => TextNode::Box(HorizontalBox {
width: b.width,
height: b.height,
depth: b.depth,
native_box: Some(b),
..HorizontalBox::default()
}),
NativeVerticalNode::Glue(g) => TextNode::Glue(g),
NativeVerticalNode::NamedGlue(g, p) => TextNode::NamedGlue(g, p),
NativeVerticalNode::Leaders(l) => TextNode::Leaders(l),
NativeVerticalNode::Kern(width) => TextNode::Kern {
width,
explicit: true,
},
NativeVerticalNode::ImplicitKern(width) => TextNode::Kern {
width,
explicit: false,
},
NativeVerticalNode::Penalty(n) => TextNode::Penalty(n),
NativeVerticalNode::Rule(r) => TextNode::Rule(r),
NativeVerticalNode::Mark(m) => TextNode::Mark(m),
NativeVerticalNode::Insertion(n) => TextNode::Insertion(n),
NativeVerticalNode::Output(o) => TextNode::Output(o),
}
}
impl VerticalBuilder {
fn tail_box(&self) -> Option<&NativeBox> {
self.tail_path
.split_first()
.map(|(&i, rest)| descend(vertical_box(&self.nodes[i]), rest))
}
fn tail_box_mut(&mut self) -> Option<&mut NativeBox> {
self.tail_path
.split_first()
.map(|(&i, rest)| descend_mut(vertical_box_mut(&mut self.nodes[i]), rest))
}
fn refresh_tail(&mut self) {
if let Some((&i, rest)) = self.tail_path.split_first() {
refresh(vertical_box_mut(&mut self.nodes[i]), rest);
}
}
fn tail_len(&self) -> usize {
match self.tail_box().map(|b| &b.content) {
None => self.nodes.len(),
Some(BoxContent::Vertical(v)) => v.len(),
Some(BoxContent::Horizontal(v)) => v.len(),
}
}
pub(in crate::execution) fn tail_depth(&self) -> usize {
self.tail_path.len()
}
pub(in crate::execution) fn nest_tail_in(&mut self, index: usize) {
if self.tail_path.is_empty() && index + 1 == self.nodes.len() {
self.tail_path.push(index);
}
}
pub(in crate::execution) fn push(&mut self, node: NativeVerticalNode) {
match self.tail_box_mut().map(|b| &mut b.content) {
None => self.nodes.push(node),
Some(BoxContent::Vertical(v)) => v.push(node),
Some(BoxContent::Horizontal(v)) => v.push(horizontal(node)),
}
self.refresh_tail();
}
pub(in crate::execution) fn extend(
&mut self,
nodes: impl IntoIterator<Item = NativeVerticalNode>,
) {
for node in nodes {
self.push(node);
}
}
pub(in crate::execution) fn append(&mut self, nodes: &mut Vec<NativeVerticalNode>) {
self.extend(std::mem::take(nodes));
}
pub(in crate::execution) fn append_builder(&mut self, other: Self) {
let base = self.tail_len();
let mut path = other.tail_path;
self.extend(other.nodes);
if let Some(first) = path.first_mut() {
*first += base;
self.tail_path.extend(path);
}
}
pub(in crate::execution) fn take_nodes(&mut self) -> Vec<NativeVerticalNode> {
self.tail_path.clear();
std::mem::take(&mut self.nodes)
}
pub(in crate::execution) fn tail_ref(&self) -> Option<TailNodeRef<'_>> {
match self.tail_box().map(|b| &b.content) {
None => self.nodes.last().map(TailNodeRef::Vertical),
Some(BoxContent::Vertical(v)) => v.last().map(TailNodeRef::Vertical),
Some(BoxContent::Horizontal(v)) => v.last().map(TailNodeRef::Text),
}
}
fn pop(&mut self) -> Option<Removed> {
let node = match self.tail_box_mut().map(|b| &mut b.content) {
None => self.nodes.pop().map(Removed::Vertical),
Some(BoxContent::Vertical(v)) => v.pop().map(Removed::Vertical),
Some(BoxContent::Horizontal(v)) => v.pop().map(Removed::Text),
};
self.refresh_tail();
node
}
pub(in crate::execution) fn remove_tail(&mut self) -> Result<(), String> {
if !self.tail_path.is_empty() {
return Err(
"cannot unlink a native list tail not reachable from its active head".into(),
);
}
self.pop();
Ok(())
}
pub(in crate::execution) fn take_last_box(&mut self) -> Result<Option<NativeBox>, String> {
let is_box = matches!(
self.tail_ref(),
Some(TailNodeRef::Vertical(NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b))) if !b.kind.is_unset()
) || matches!(self.tail_ref(), Some(TailNodeRef::Text(TextNode::Box(b))) if b.native_box.as_ref().is_some_and(|b| !b.kind.is_unset())
);
if !is_box {
return Ok(None);
}
if !self.tail_path.is_empty() {
return Err(
"cannot unlink a native list tail not reachable from its active head".into(),
);
}
Ok(match self.pop().unwrap() {
Removed::Vertical(NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b)) => Some(*b),
Removed::Text(TextNode::Box(b)) => b.native_box.map(|b| *b),
_ => unreachable!("checked actual tail box"),
})
}
}