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

212 lines
6.8 KiB
Rust

//! TeX's cut-prefix/hyphen_passed and major/minor synchronization. Replacements
//! are real ordinary nodes following a discretionary, never whole-word paint.
use super::{
ligature::{Component, Glyph, Machine, Output},
*,
};
use std::sync::Arc;
fn nodes(
font: &Arc<crate::native_font::NativeFont>,
out: Vec<Output>,
) -> Result<Vec<TextNode>, String> {
out.into_iter()
.map(|n| {
Ok(match n {
Output::Kern(width) => TextNode::Kern {
width,
explicit: false,
},
Output::Glyph(g) => TextNode::Glyph {
font: font.id,
glyph: super::fonts::text_glyph_from_font(font, g.slot, g.source),
slot: g.slot,
original: g.components.iter().map(|c| char::from(c.slot)).collect(),
origins: g.components.iter().map(|c| c.origin).collect(),
component_sources: g.components.iter().map(|c| c.source.clone()).collect(),
ligature: g.ligature,
left_boundary: g.left_boundary,
right_boundary: g.right_boundary,
hyphen_char: None,
},
Output::AutoKern { width, .. } => TextNode::AutoKern(width),
Output::Missing(_) => {
return Err("unfetched original character during native reconstitution".into());
}
})
})
.collect()
}
pub(super) fn word(
state: &State,
font: FontId,
letters: Vec<Component>,
seed: Option<Glyph>,
left_boundary: bool,
bchar: Option<u8>,
points: &[usize],
steps: &mut usize,
limit: usize,
missing: &mut Vec<(FontId, u8, Span)>,
) -> Result<Vec<TextNode>, String> {
let face = state.fonts.glyph_font(font)?.clone();
let f = face.metrics();
let hyphen = state
.hyphen_character(font)
.ok_or("native reconstitution requires a valid hyphen slot")? as u8;
let n = letters.len();
let mut hu = vec![seed.as_ref().map(|g| Component {
slot: g.slot,
origin: g.components.first().map_or(Span::default(), |c| c.origin),
source: g.source.clone(),
})];
hu.extend(letters.into_iter().map(Some));
let mut hyf = vec![false; n + 1];
for i in points {
hyf[*i] = true;
}
let mut result = Vec::new();
let mut j = if seed.is_some() || left_boundary {
0
} else {
1
};
while j <= n {
let mut l = j;
let cut = Machine::reconstitute(
f,
&hu,
j,
n,
bchar,
Some(hyphen),
&hyf,
seed.as_ref(),
steps,
limit,
)?;
j = cut.end + 1;
let mut passed = cut.hyphen_passed;
let mut hold = nodes(&face, cut.material)?;
if passed == 0 {
result.append(&mut hold);
if hyf[j - 1] {
l = j;
passed = j - 1;
}
}
if passed == 0 {
continue;
}
loop {
let mut replacement = std::mem::take(&mut hold);
let i = passed;
hyf[i] = false;
let origin = hu[i].as_ref().map_or(Span::default(), |c| c.origin);
let present = f.has_slot(hyphen);
let pre_end = if present { i + 1 } else { i };
let saved = if present {
let mut source = hu[i].as_ref().map(|c| c.source.clone()).unwrap_or_default();
if let Some(c) = &hu[i + 1] {
source.merge(&c.source);
}
hu[i + 1].replace(Component {
slot: hyphen,
origin,
source: source.derived(crate::provenance::Derivation::Hyphenation),
})
} else {
missing.push((font, hyphen, origin));
None
};
let mut pre = Vec::new();
while l <= pre_end {
let cut = Machine::reconstitute(
f,
&hu,
l,
pre_end,
f.boundary_char(),
None,
&hyf,
seed.as_ref(),
steps,
limit,
)?;
l = cut.end + 1;
pre.extend(nodes(&face, cut.material)?);
}
if present {
hu[i + 1] = saved;
l = i + 1;
}
let mut post = Vec::new();
let mut restore = None;
if f.boundary_label().is_some() {
l -= 1;
restore = Some((l, hu[l].take()));
}
while l < j {
loop {
let cut = Machine::reconstitute(
f,
&hu,
l,
n,
bchar,
None,
&hyf,
seed.as_ref(),
steps,
limit,
)?;
l = cut.end + 1;
post.extend(nodes(&face, cut.material)?);
if let Some((at, c)) = restore.take() {
hu[at] = c;
}
if l >= j {
break;
}
}
while l > j {
let cut = Machine::reconstitute(
f,
&hu,
j,
n,
bchar,
None,
&hyf,
seed.as_ref(),
steps,
limit,
)?;
j = cut.end + 1;
replacement.extend(nodes(&face, cut.material)?);
}
}
if let Some((at, c)) = restore.take() {
hu[at] = c;
}
// Automatic reconstitution has TeX's separate 127-node bound;
// explicit discretionary scanning accepts a full quarterword.
if replacement.len() <= 127 {
result.push(TextNode::Discretionary {
font,
automatic: true,
hyphen_char: present.then_some(char::from(hyphen)),
pre_break: pre,
post_break: post,
replacement_count: replacement.len() as u8,
});
}
result.extend(replacement);
passed = j - 1;
if !hyf[passed] {
break;
}
}
}
Ok(result)
}