//! Owned TeX--XeT boundaries and visual traversal of logical horizontal lists. use super::TextNode; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum DirectionBoundary { BeginL, EndL, BeginR, EndR, } impl DirectionBoundary { pub(super) fn name(self) -> &'static str { match self { Self::BeginL => "beginL", Self::EndL => "endL", Self::BeginR => "beginR", Self::EndR => "endR", } } } #[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum Region { Left, Right, Math, } impl Region { fn mark(node: &TextNode) -> Option<(Self, bool)> { let mark = match node { TextNode::Direction(DirectionBoundary::BeginL) => (Self::Left, true), TextNode::Direction(DirectionBoundary::EndL) => (Self::Left, false), TextNode::Direction(DirectionBoundary::BeginR) => (Self::Right, true), TextNode::Direction(DirectionBoundary::EndR) => (Self::Right, false), TextNode::MathBoundary { after, .. } => (Self::Math, !after), _ => return None, }; Some(mark) } fn node(self, begin: bool) -> TextNode { TextNode::Direction(match (self, begin) { (Self::Left, true) => DirectionBoundary::BeginL, (Self::Left, false) => DirectionBoundary::EndL, (Self::Right, true) => DirectionBoundary::BeginR, (Self::Right, false) => DirectionBoundary::EndR, (Self::Math, _) => return TextNode::MathBoundary { width: 0, after: !begin }, }) } } pub(super) fn has_directions(nodes: &[TextNode]) -> bool { nodes.iter().any(|node| matches!(node, TextNode::Direction(_))) } pub(super) fn observe(stack: &mut Vec, node: &TextNode) { if let Some((region, begin)) = Region::mark(node) { if begin { stack.push(region); } else if stack.last() == Some(®ion) { stack.pop(); } } } /// A paragraph carries direction spans through its chosen logical breaks. /// Each packed line owns the corresponding openings and closings for output. pub(super) fn line(nodes: &mut Vec, stack: &mut Vec) { let prefix: Vec<_> = stack.iter().map(|region| region.node(true)).collect(); for node in nodes.iter() { observe(stack, node); } nodes.splice(..0, prefix); } pub(super) fn close(nodes: &mut Vec, stack: &[Region]) { nodes.extend(stack.iter().rev().map(|region| region.node(false))); } pub(super) fn complete_box(nodes: &mut Vec) -> (usize, usize) { if !has_directions(nodes) { return (0, 0); } let mut stack = Vec::new(); let mut extra = 0; for node in nodes.iter_mut() { match Region::mark(node) { Some((region, false)) if stack.last() != Some(®ion) && matches!(node, TextNode::Direction(_)) => { *node = TextNode::Kern { width: 0, explicit: true }; extra += 1; } _ => observe(&mut stack, node), } } close(nodes, &stack); (stack.len(), extra) } struct Frame { start: usize, end: usize, rtl: bool, exit: Option<(usize, bool)>, } /// Indices in physical left-to-right order with the enclosing direction of /// each item. Retain logical order in the box so unboxing and diagnostics see /// original nodes. Pairing and traversal both take linear time. pub(super) fn visual_order(nodes: &[TextNode], rtl: bool) -> Option> { if !rtl && !has_directions(nodes) { return None; } let mut mates = vec![None; nodes.len()]; let mut open = Vec::new(); for (index, node) in nodes.iter().enumerate() { if let Some((region, begin)) = Region::mark(node) { if begin { open.push((region, index)); } else if let Some(&(found, first)) = open.last() && region == found { open.pop(); mates[first] = Some((index, region)); mates[index] = Some((first, region)); } } } let mut output = Vec::with_capacity(nodes.len()); let mut frames = vec![Frame { start: 0, end: nodes.len(), rtl, exit: None }]; while let Some(frame) = frames.last_mut() { if frame.start == frame.end { output.extend(frame.exit); frames.pop(); continue; } let index = if frame.rtl { frame.end -= 1; frame.end } else { let index = frame.start; frame.start += 1; index }; let rtl = frame.rtl; output.push((index, rtl)); if let Some((mate, region)) = mates[index] { let (start, end) = if rtl { frame.end = mate; (mate + 1, index) } else { frame.start = mate + 1; (index + 1, mate) }; frames.push(Frame { start, end, rtl: region == Region::Right, exit: Some((mate, rtl)), }); } } Some(output) }