Implement XeTeX native word ActualText across shaping and shipout

This commit is contained in:
gloamxun committed 2026-11-25 23:38:47 +08:00
1 parent 97319d751b
commit 7e5428ec35
15 files changed
+143 -24

No files matched your search

@@ -273,12 +273,10 @@ impl Builder {
fn finish_opentype(&mut self, run: PendingOpenType, state: &State) -> Result<(), String> {
let xetex = state.input_profile() == InputProfile::XeTeX;
let use_glyph_metrics =
xetex && state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
let original_end = run.text.chars().next_back();
let (text, sources) = run.font.map_text(run.text, run.sources)?;
if self.restricted || !xetex {
return self.push_opentype(&run.font, text, sources, use_glyph_metrics);
return self.push_opentype(&run.font, text, sources, state);
}
let dash_breaks = state.parameter(Parameter::XeTeXdashbreakstate).width() > 0;
@@ -295,7 +293,7 @@ impl Builder {
});
let mut ends = ends.peekable();
if ends.peek().is_none() {
self.push_opentype(&run.font, text, sources, use_glyph_metrics)?;
self.push_opentype(&run.font, text, sources, state)?;
} else {
let mut sources = sources.into_iter().peekable();
let mut start = 0;
@@ -308,7 +306,7 @@ impl Builder {
&run.font,
text[start..end].to_owned(),
fragment_sources,
use_glyph_metrics,
state,
)?;
if end != text.len() {
self.push_explicit_hyphen_break(run.font.id);
@@ -325,14 +323,52 @@ impl Builder {
fn push_opentype(
&mut self,
font: &Arc<OpenTypeFont>,
text: String,
sources: Vec<(usize, PaintSource)>,
use_glyph_metrics: bool,
mut text: String,
mut sources: Vec<(usize, PaintSource)>,
state: &State,
) -> Result<(), String> {
let shaped = font.shape_mapped(text, sources, use_glyph_metrics)?;
let xetex = state.input_profile() == InputProfile::XeTeX;
let use_glyph_metrics =
xetex && state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
// A fresh word takes the current subtype, including when the scanner
// replaces the preceding word. Packing preserves the first subtype.
let previous = match self.nodes.as_slice() {
[.., preceding, TextNode::OpenType(run)]
if xetex && run.font.id == font.id
&& !matches!(preceding, TextNode::Glyph { ligature: false, .. }
| TextNode::Discretionary { .. }) =>
{
Some(run)
}
_ => None,
}.filter(|_| {
!self.nodes.iter().rev().skip(1).take(usize::from(u8::MAX))
.enumerate().any(|(offset, node)| {
matches!(node, TextNode::Discretionary { replacement_count, .. }
if usize::from(*replacement_count) > offset)
})
});
if let Some(previous) = previous {
let offset = previous.text.len();
let mut joined = previous.text.clone();
joined.push_str(&text);
if joined.encode_utf16().count() > usize::from(u16::MAX) {
return Err("native OpenType word capacity exceeded".into());
}
text = joined;
let mut joined_sources = previous.sources.clone();
joined_sources.extend(sources.into_iter().map(|(at, source)| (at + offset, source)));
sources = joined_sources;
}
let replaced = previous.is_some();
let mut shaped = font.shape_mapped(text, sources, use_glyph_metrics)?;
shaped.actual_text = xetex && state.parameter(Parameter::XeTeXgenerateactualtext).width() > 0;
self.font_steps = self.font_steps.checked_add(shaped.glyphs.len())
.filter(|steps| *steps <= self.font_step_limit)
.ok_or("native OpenType shaping step limit exceeded")?;
if replaced {
self.nodes.pop();
}
self.nodes.push(TextNode::OpenType(Arc::new(shaped)));
Ok(())
}