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,509 @@
|
||||
//! Streaming TFM cursor/ligature-stack execution. A raw input character is
|
||||
//! fetched only after the pending cursor asks for one; inserted ligatures can
|
||||
//! therefore retain or consume components without inventing source characters.
|
||||
use super::Span;
|
||||
use crate::provenance::{Derivation, PaintSource};
|
||||
use crate::tfm::{LigKernCommand, TfmMetrics};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(super) struct Component {
|
||||
pub slot: u8,
|
||||
pub origin: Span,
|
||||
pub source: PaintSource,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct Glyph {
|
||||
pub slot: u8,
|
||||
pub components: Vec<Component>,
|
||||
/// Causal inputs, independent of TeX's semantic component list. Retaining
|
||||
/// ligature operations can insert paint with no semantic components.
|
||||
pub source: PaintSource,
|
||||
pub ligature: bool,
|
||||
pub left_boundary: bool,
|
||||
pub right_boundary: bool,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) enum Output {
|
||||
Glyph(Glyph),
|
||||
Kern(i32),
|
||||
Missing(Component),
|
||||
}
|
||||
/// TeX's main-loop flags are job globals, including across a missing raw
|
||||
/// character's jump out of move+1. They are not font or group parameters.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(super) struct Carry {
|
||||
ligature: bool,
|
||||
left: bool,
|
||||
right: bool,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
struct Right {
|
||||
glyph: Glyph,
|
||||
raw: bool,
|
||||
suppress_match: bool,
|
||||
}
|
||||
pub(super) struct Cut {
|
||||
pub end: usize,
|
||||
pub hyphen_passed: usize,
|
||||
pub material: Vec<Output>,
|
||||
}
|
||||
impl Right {
|
||||
fn raw(c: Component, suppress_match: bool) -> Self {
|
||||
Self {
|
||||
glyph: Glyph {
|
||||
slot: c.slot,
|
||||
source: c.source.clone(),
|
||||
components: vec![c],
|
||||
ligature: false,
|
||||
left_boundary: false,
|
||||
right_boundary: false,
|
||||
},
|
||||
raw: true,
|
||||
suppress_match,
|
||||
}
|
||||
}
|
||||
fn inserted(slot: u8) -> Self {
|
||||
Self {
|
||||
glyph: Glyph {
|
||||
slot,
|
||||
components: Vec::new(),
|
||||
source: PaintSource::default(),
|
||||
ligature: true,
|
||||
left_boundary: false,
|
||||
right_boundary: false,
|
||||
},
|
||||
raw: false,
|
||||
suppress_match: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(super) struct Machine {
|
||||
started: bool,
|
||||
left: Option<Glyph>,
|
||||
stack: VecDeque<Right>,
|
||||
left_hit: bool,
|
||||
right_hit: bool,
|
||||
orphan_ligature: bool,
|
||||
boundary: Option<u8>,
|
||||
}
|
||||
impl Machine {
|
||||
/// Raw components are already on TeX's hlist while lookahead expands;
|
||||
/// the ligature node replaces them only at wrapup.
|
||||
pub(super) fn has_unpacked_components(&self) -> bool {
|
||||
self.left
|
||||
.as_ref()
|
||||
.is_some_and(|glyph| !glyph.components.is_empty())
|
||||
}
|
||||
pub(super) fn resource_items(&self) -> usize {
|
||||
self.left.as_ref().map_or(0, |g| 1 + g.components.len())
|
||||
+ self
|
||||
.stack
|
||||
.iter()
|
||||
.map(|r| 1 + r.glyph.components.len())
|
||||
.sum::<usize>()
|
||||
}
|
||||
/// TeX reconstitute returns a cut prefix, not a translation of the whole
|
||||
/// word. Its cursor shares the actual slots, components, ligature stack
|
||||
/// and boundary packing with normal text execution.
|
||||
pub fn reconstitute(
|
||||
f: &TfmMetrics,
|
||||
hu: &[Option<Component>],
|
||||
mut j: usize,
|
||||
n: usize,
|
||||
bchar: Option<u8>,
|
||||
mut hchar: Option<u8>,
|
||||
points: &[bool],
|
||||
seed: Option<&Glyph>,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
) -> Result<Cut, String> {
|
||||
let mut cursor = Self {
|
||||
boundary: bchar,
|
||||
..Self::default()
|
||||
};
|
||||
cursor.left = if j == 0 && seed.is_some() {
|
||||
seed.cloned()
|
||||
} else {
|
||||
hu[j].clone().map(|c| Right::raw(c, false).glyph)
|
||||
};
|
||||
if j == 0 {
|
||||
if let Some(g) = seed {
|
||||
cursor.left_hit = g.left_boundary;
|
||||
}
|
||||
}
|
||||
let (mut out, mut passed) = (Vec::new(), 0);
|
||||
loop {
|
||||
*steps = steps.saturating_add(1);
|
||||
if *steps > limit {
|
||||
return Err("native reconstitute instruction limit exceeded".into());
|
||||
}
|
||||
let left = cursor.left.as_ref().map(|g| g.slot);
|
||||
let right = cursor.stack.front().map(|r| r.glyph.slot).or_else(|| {
|
||||
if j < n {
|
||||
hu[j + 1].as_ref().map(|c| c.slot)
|
||||
} else {
|
||||
cursor.boundary
|
||||
}
|
||||
});
|
||||
let point = points.get(j).copied().unwrap_or(false);
|
||||
if cursor.stack.is_empty() && point {
|
||||
if let Some(h) = hchar {
|
||||
if f.lig_kern(left, h).is_some() {
|
||||
passed = j;
|
||||
hchar = None;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
let command = right.and_then(|r| f.lig_kern(left, r));
|
||||
if command.is_some() && hchar.is_some() && point {
|
||||
passed = j;
|
||||
hchar = None;
|
||||
}
|
||||
let mut done = false;
|
||||
let mut kern = 0;
|
||||
match command {
|
||||
None => done = true,
|
||||
Some(LigKernCommand::Kern(k)) => {
|
||||
kern = k;
|
||||
done = true;
|
||||
}
|
||||
Some(LigKernCommand::Ligature {
|
||||
operation,
|
||||
replacement,
|
||||
}) => {
|
||||
let mut source = cursor
|
||||
.left
|
||||
.as_ref()
|
||||
.map(|g| g.source.clone())
|
||||
.unwrap_or_default();
|
||||
if let Some(r) = cursor.stack.front() {
|
||||
source.merge(&r.glyph.source);
|
||||
} else if j < n {
|
||||
if let Some(c) = &hu[j + 1] {
|
||||
source.merge(&c.source);
|
||||
}
|
||||
}
|
||||
source = source.derived(Derivation::FontProgram);
|
||||
if cursor.left.is_none() {
|
||||
cursor.left_hit = true;
|
||||
}
|
||||
if j == n && cursor.stack.is_empty() {
|
||||
cursor.right_hit = true;
|
||||
}
|
||||
match operation {
|
||||
1 | 5 => {
|
||||
let g = cursor
|
||||
.left
|
||||
.get_or_insert_with(|| Right::inserted(replacement).glyph);
|
||||
g.slot = replacement;
|
||||
g.ligature = true;
|
||||
g.source = source;
|
||||
}
|
||||
2 | 6 => {
|
||||
if let Some(r) = cursor.stack.front_mut() {
|
||||
r.glyph.slot = replacement;
|
||||
r.glyph.source = source;
|
||||
} else {
|
||||
let mut r = Right::inserted(replacement);
|
||||
r.glyph.source = source;
|
||||
if j == n {
|
||||
cursor.boundary = None;
|
||||
} else if let Some(c) = &hu[j + 1] {
|
||||
r.glyph.components.push(c.clone());
|
||||
}
|
||||
cursor.stack.push_front(r);
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
let mut r = Right::inserted(replacement);
|
||||
r.glyph.source = source;
|
||||
cursor.stack.push_front(r);
|
||||
}
|
||||
7 | 11 => {
|
||||
cursor.wrap(false, &mut out);
|
||||
cursor.left = Some(Right::inserted(replacement).glyph);
|
||||
cursor.left.as_mut().unwrap().source = source;
|
||||
}
|
||||
_ => {
|
||||
let g = cursor
|
||||
.left
|
||||
.get_or_insert_with(|| Right::inserted(replacement).glyph);
|
||||
g.slot = replacement;
|
||||
g.ligature = true;
|
||||
g.source = source;
|
||||
if let Some(r) = cursor.stack.pop_front() {
|
||||
if !r.glyph.components.is_empty() {
|
||||
j += 1;
|
||||
g.components.extend(r.glyph.components);
|
||||
}
|
||||
} else if j == n {
|
||||
done = true;
|
||||
} else {
|
||||
j += 1;
|
||||
if let Some(c) = &hu[j] {
|
||||
g.components.push(c.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if operation > 4 && operation != 7 {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !done {
|
||||
continue;
|
||||
}
|
||||
cursor.wrap(true, &mut out);
|
||||
if kern != 0 {
|
||||
out.push(Output::Kern(
|
||||
i32::try_from(kern).map_err(|_| "native reconstitute kern overflow")?,
|
||||
));
|
||||
}
|
||||
if let Some(r) = cursor.stack.pop_front() {
|
||||
if !r.glyph.components.is_empty() {
|
||||
j += 1;
|
||||
}
|
||||
cursor.left = Some(r.glyph);
|
||||
} else {
|
||||
return Ok(Cut {
|
||||
end: j,
|
||||
hyphen_passed: passed,
|
||||
material: out,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn put_carry(&mut self, carry: Carry) {
|
||||
debug_assert!(!self.started || carry == Carry::default());
|
||||
if !self.started {
|
||||
self.orphan_ligature = carry.ligature;
|
||||
self.left_hit = carry.left;
|
||||
self.right_hit = carry.right;
|
||||
}
|
||||
}
|
||||
pub fn take_carry(&mut self) -> Carry {
|
||||
if self.started {
|
||||
return Carry::default();
|
||||
}
|
||||
let c = Carry {
|
||||
ligature: self.orphan_ligature,
|
||||
left: self.left_hit,
|
||||
right: self.right_hit,
|
||||
};
|
||||
self.orphan_ligature = false;
|
||||
self.left_hit = false;
|
||||
self.right_hit = false;
|
||||
c
|
||||
}
|
||||
pub fn feed(
|
||||
&mut self,
|
||||
f: &TfmMetrics,
|
||||
c: Component,
|
||||
cancel_left: bool,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
) -> Result<Vec<Output>, String> {
|
||||
let mut out = Vec::new();
|
||||
if !self.started {
|
||||
let carry = self.take_carry();
|
||||
*self = Self {
|
||||
started: true,
|
||||
boundary: f.boundary_char(),
|
||||
orphan_ligature: carry.ligature,
|
||||
left_hit: carry.left,
|
||||
right_hit: carry.right,
|
||||
..Self::default()
|
||||
};
|
||||
self.stack.push_back(Right::raw(c, false));
|
||||
if cancel_left || f.boundary_label().is_none() {
|
||||
self.advance(f, &mut out);
|
||||
}
|
||||
} else {
|
||||
debug_assert!(self.stack.is_empty());
|
||||
let suppress_match = Some(c.slot) == f.false_boundary_char();
|
||||
self.stack.push_back(Right::raw(c, suppress_match));
|
||||
}
|
||||
self.pump(f, false, steps, limit, &mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
pub fn finish(
|
||||
&mut self,
|
||||
f: &TfmMetrics,
|
||||
right_boundary: bool,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
) -> Result<Vec<Output>, String> {
|
||||
if !right_boundary {
|
||||
self.boundary = None;
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
self.pump(f, true, steps, limit, &mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
fn wrap(&mut self, right: bool, out: &mut Vec<Output>) {
|
||||
if let Some(mut glyph) = self.left.take() {
|
||||
if glyph.ligature {
|
||||
self.orphan_ligature = false;
|
||||
glyph.left_boundary = self.left_hit;
|
||||
self.left_hit = false;
|
||||
if right && self.stack.is_empty() {
|
||||
glyph.right_boundary = self.right_hit;
|
||||
self.right_hit = false;
|
||||
}
|
||||
}
|
||||
out.push(Output::Glyph(glyph));
|
||||
}
|
||||
}
|
||||
fn advance(&mut self, f: &TfmMetrics, out: &mut Vec<Output>) {
|
||||
if let Some(mut right) = self.stack.pop_front() {
|
||||
if right.raw && !f.has_slot(right.glyph.slot) {
|
||||
out.push(Output::Missing(
|
||||
right.glyph.components.into_iter().next().unwrap(),
|
||||
));
|
||||
self.started = false;
|
||||
} else {
|
||||
right.glyph.ligature |= self.orphan_ligature;
|
||||
self.orphan_ligature = false;
|
||||
self.left = Some(right.glyph);
|
||||
}
|
||||
} else {
|
||||
self.started = false;
|
||||
}
|
||||
}
|
||||
fn pump(
|
||||
&mut self,
|
||||
f: &TfmMetrics,
|
||||
ending: bool,
|
||||
steps: &mut usize,
|
||||
limit: usize,
|
||||
out: &mut Vec<Output>,
|
||||
) -> Result<(), String> {
|
||||
while self.started {
|
||||
if self.stack.is_empty() && !ending {
|
||||
break;
|
||||
}
|
||||
*steps = steps.saturating_add(1);
|
||||
if *steps > limit {
|
||||
return Err("native font program instruction limit exceeded".into());
|
||||
}
|
||||
let right = if let Some(r) = self.stack.front() {
|
||||
if r.suppress_match {
|
||||
None
|
||||
} else {
|
||||
Some(r.glyph.slot)
|
||||
}
|
||||
} else {
|
||||
self.boundary
|
||||
};
|
||||
let command = right.and_then(|r| f.lig_kern(self.left.as_ref().map(|g| g.slot), r));
|
||||
match command {
|
||||
None => {
|
||||
self.wrap(true, out);
|
||||
self.advance(f, out);
|
||||
}
|
||||
Some(LigKernCommand::Kern(k)) => {
|
||||
self.wrap(true, out);
|
||||
out.push(Output::Kern(
|
||||
i32::try_from(k).map_err(|_| "native font kern overflow")?,
|
||||
));
|
||||
self.advance(f, out);
|
||||
}
|
||||
Some(LigKernCommand::Ligature {
|
||||
operation,
|
||||
replacement,
|
||||
}) => {
|
||||
let mut source = self
|
||||
.left
|
||||
.as_ref()
|
||||
.map(|g| g.source.clone())
|
||||
.unwrap_or_default();
|
||||
if let Some(r) = self.stack.front() {
|
||||
source.merge(&r.glyph.source);
|
||||
}
|
||||
source = source.derived(Derivation::FontProgram);
|
||||
if self.left.is_none() {
|
||||
self.left_hit = true;
|
||||
} else if self.stack.is_empty() {
|
||||
self.right_hit = true;
|
||||
}
|
||||
match operation {
|
||||
1 | 5 => {
|
||||
let g = self
|
||||
.left
|
||||
.get_or_insert_with(|| Right::inserted(replacement).glyph);
|
||||
g.slot = replacement;
|
||||
g.ligature = true;
|
||||
g.source = source;
|
||||
}
|
||||
2 | 6 => {
|
||||
if let Some(r) = self.stack.front_mut() {
|
||||
r.glyph.slot = replacement;
|
||||
r.glyph.source = source;
|
||||
r.glyph.ligature = true;
|
||||
r.raw = false;
|
||||
r.suppress_match = false;
|
||||
} else {
|
||||
let mut r = Right::inserted(replacement);
|
||||
r.glyph.source = source;
|
||||
self.stack.push_front(r);
|
||||
self.boundary = None;
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
let mut r = Right::inserted(replacement);
|
||||
r.glyph.source = source;
|
||||
self.stack.push_front(r);
|
||||
}
|
||||
7 | 11 => {
|
||||
self.wrap(false, out);
|
||||
self.left = Some(Right::inserted(replacement).glyph);
|
||||
self.left.as_mut().unwrap().source = source;
|
||||
}
|
||||
_ => {
|
||||
let mut g = self
|
||||
.left
|
||||
.take()
|
||||
.unwrap_or_else(|| Right::inserted(replacement).glyph);
|
||||
g.slot = replacement;
|
||||
g.ligature = true;
|
||||
g.source = source;
|
||||
if let Some(r) = self.stack.pop_front() {
|
||||
if r.raw && !f.has_slot(r.glyph.slot) {
|
||||
// main_loop_move+1 still fetches the raw
|
||||
// input character after a default lig op.
|
||||
// A failed fetch drops this pending ligature
|
||||
// and preserves the global flags for reswitch.
|
||||
out.push(Output::Missing(
|
||||
r.glyph.components.into_iter().next().unwrap(),
|
||||
));
|
||||
self.orphan_ligature = true;
|
||||
self.started = false;
|
||||
continue;
|
||||
}
|
||||
g.components.extend(r.glyph.components);
|
||||
} else {
|
||||
self.left = Some(g);
|
||||
self.wrap(true, out);
|
||||
self.started = false;
|
||||
continue;
|
||||
}
|
||||
self.left = Some(g);
|
||||
// TeX's default operation jumps directly to move+1,
|
||||
// so it does not use the later op>4 stopping rule.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if operation > 4 && operation != 7 {
|
||||
self.wrap(true, out);
|
||||
self.advance(f, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user