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

@@ -63,7 +63,8 @@ pub(super) fn rejoin(
text.push_str(&run.text);
}
}
let word = first.font.shape_mapped(text, sources, glyph_metrics)?;
let mut word = first.font.shape_mapped(text, sources, glyph_metrics)?;
word.actual_text = first.actual_text;
if let Some((steps, limit)) = &mut shaping_budget {
**steps = steps
.checked_add(word.glyphs.len())
@@ -174,7 +174,9 @@ impl NativeShipout {
let y = self.device.vertical_origin + self.box_node.height + p.y;
if let MathItem::OpenType(run) = p.item {
run.paint(x, y, |value| value, |item, source| {
page.sources.insert(page.items.len(), source);
if !matches!(item, DisplayItem::OpenTypeActualText(_)) {
page.sources.insert(page.items.len(), source);
}
page.items.push(item);
});
return;
@@ -389,6 +389,7 @@ impl Executor {
if let Some(font) = opentype {
if self.state.input_profile() == InputProfile::XeTeX {
self.reads.insert(Dependency::Parameter(Parameter::XeTeXuseglyphmetrics));
self.reads.insert(Dependency::Parameter(Parameter::XeTeXgenerateactualtext));
if !self.restricted() {
self.reads.insert(Dependency::Parameter(Parameter::XeTeXdashbreakstate));
}
@@ -8,9 +8,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 71;
const COMPATIBLE_SCHEMAS: [u32; 38] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, SCHEMA,
const SCHEMA: u32 = 72;
const COMPATIBLE_SCHEMAS: [u32; 39] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -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(())
}
@@ -12,6 +12,7 @@ pub(super) fn add_hyphens(
let max_length = super::super::hyphenation::max_word_length(state);
let uppercase = state.parameter(Parameter::Uchyph).width() > 0;
let use_glyph_metrics = state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
let mapped_hyphen_actual_text = state.parameter(Parameter::XeTeXgenerateactualtext).width() > 0;
let mut after_glue = false;
let mut auto_breaking = true;
let mut edits = Vec::new();
@@ -123,6 +124,7 @@ pub(super) fn add_hyphens(
run.text[from..to].to_owned(),
fragment_sources,
use_glyph_metrics,
run.actual_text,
steps,
step_limit,
)?);
@@ -145,6 +147,7 @@ pub(super) fn add_hyphens(
text,
sources,
use_glyph_metrics,
run.font.has_mapping() && mapped_hyphen_actual_text,
steps,
step_limit,
)?],
@@ -210,10 +213,12 @@ fn shape(
text: String,
sources: Vec<(usize, PaintSource)>,
use_glyph_metrics: bool,
actual_text: bool,
steps: &mut usize,
step_limit: usize,
) -> Result<TextNode, String> {
let run = run.font.shape_mapped(text, sources, use_glyph_metrics)?;
let mut run = run.font.shape_mapped(text, sources, use_glyph_metrics)?;
run.actual_text = actual_text;
*steps = steps
.checked_add(run.glyphs.len())
.filter(|steps| *steps <= step_limit)
@@ -126,6 +126,7 @@ pub enum Parameter {
XeTeXinterchartokenstate,
XeTeXprotrudechars,
XeTeXhyphenatablelength,
XeTeXgenerateactualtext,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ParameterValue {
@@ -158,13 +159,14 @@ impl Parameter {
)
}
pub(super) const XETEX: [Self; 6] = [
pub(super) const XETEX: [Self; 7] = [
Self::XeTeXuseglyphmetrics,
Self::XeTeXdashbreakstate,
Self::Suppressfontnotfounderror,
Self::XeTeXinterchartokenstate,
Self::XeTeXprotrudechars,
Self::XeTeXhyphenatablelength,
Self::XeTeXgenerateactualtext,
];
pub const ALL: &'static [Self] = &[
@@ -289,6 +291,7 @@ impl Parameter {
Self::XeTeXinterchartokenstate,
Self::XeTeXprotrudechars,
Self::XeTeXhyphenatablelength,
Self::XeTeXgenerateactualtext,
];
pub fn name(self) -> String {
match self {
@@ -298,6 +301,7 @@ impl Parameter {
Self::XeTeXinterchartokenstate => "XeTeXinterchartokenstate".into(),
Self::XeTeXprotrudechars => "XeTeXprotrudechars".into(),
Self::XeTeXhyphenatablelength => "XeTeXhyphenatablelength".into(),
Self::XeTeXgenerateactualtext => "XeTeXgenerateactualtext".into(),
_ => format!("{self:?}").to_ascii_lowercase(),
}
}