Rejoin XeTeX OpenType word fragments while packing horizontal boxes
This commit is contained in:
1 parent
fa27e0985f
commit
3e9a639f1d
6 files changed
+156
-4
No files matched your search
@@ -0,0 +1,93 @@
|
||||
//! Reassemble XeTeX native word fragments in the physical horizontal list.
|
||||
use super::TextNode;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) fn rejoin(
|
||||
nodes: &mut Vec<TextNode>,
|
||||
glyph_metrics: bool,
|
||||
mut shaping_budget: Option<(&mut usize, usize)>,
|
||||
) -> Result<(), String> {
|
||||
let mut index = 0;
|
||||
let mut replacement_left = 0;
|
||||
let mut edits = Vec::new();
|
||||
while let Some(node) = nodes.get(index) {
|
||||
let begin = index;
|
||||
index += 1;
|
||||
if let TextNode::Discretionary {
|
||||
replacement_count, ..
|
||||
} = node
|
||||
{
|
||||
replacement_left = *replacement_count;
|
||||
continue;
|
||||
}
|
||||
if replacement_left > 0 {
|
||||
replacement_left -= 1;
|
||||
continue;
|
||||
}
|
||||
let TextNode::OpenType(first) = node else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// Each retained discretionary may bridge one pair of fragments.
|
||||
// Starting inside a live replacement would invalidate its node count.
|
||||
loop {
|
||||
let next = index
|
||||
+ usize::from(matches!(
|
||||
nodes.get(index),
|
||||
Some(TextNode::Discretionary { .. })
|
||||
));
|
||||
if !matches!(nodes.get(next), Some(TextNode::OpenType(run)) if run.font.id == first.font.id)
|
||||
{
|
||||
break;
|
||||
}
|
||||
index = next + 1;
|
||||
}
|
||||
if index == begin + 1 {
|
||||
continue;
|
||||
}
|
||||
let mut text = String::new();
|
||||
let mut sources = Vec::new();
|
||||
let mut units = 0;
|
||||
for node in &nodes[begin..index] {
|
||||
if let TextNode::OpenType(run) = node {
|
||||
units += run.text.encode_utf16().count();
|
||||
if units > usize::from(u16::MAX) {
|
||||
return Err("native OpenType word capacity exceeded".into());
|
||||
}
|
||||
let offset = text.len();
|
||||
sources.extend(
|
||||
run.sources
|
||||
.iter()
|
||||
.map(|(at, source)| (offset + at, source.clone())),
|
||||
);
|
||||
text.push_str(&run.text);
|
||||
}
|
||||
}
|
||||
let word = first.font.shape_mapped(text, sources, glyph_metrics)?;
|
||||
if let Some((steps, limit)) = &mut shaping_budget {
|
||||
**steps = steps
|
||||
.checked_add(word.glyphs.len())
|
||||
.filter(|work| work <= limit)
|
||||
.ok_or("native OpenType packing step limit exceeded")?;
|
||||
}
|
||||
edits.push((begin, index, Arc::new(word)));
|
||||
}
|
||||
if !edits.is_empty() {
|
||||
let mut edits = edits.into_iter().peekable();
|
||||
let mut index = 0;
|
||||
let mut discard_until = 0;
|
||||
nodes.retain_mut(|node| {
|
||||
let at = index;
|
||||
index += 1;
|
||||
if at < discard_until {
|
||||
return false;
|
||||
}
|
||||
if let Some((_, end, word)) = edits.next_if(|(begin, _, _)| *begin == at) {
|
||||
*node = TextNode::OpenType(word);
|
||||
discard_until = end;
|
||||
}
|
||||
true
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user