Implement XeTeX contextual interword spacing and shipout shaping

This commit is contained in:
gloamxun committed 2026-11-26 09:23:45 +08:00
1 parent 7e5428ec35
commit 4a77540e46
23 files changed
+491 -42

No files matched your search

@@ -6,6 +6,7 @@ use crate::opentype::{OpenTypeFont, OpenTypeRun};
mod break_cache;
mod expansion;
pub(super) mod interword;
mod native_hyphenation;
mod protrusion;
pub(super) use protrusion::stored_margin_kern;
@@ -81,6 +82,8 @@ pub enum TextNode {
AutoKern(i32),
MarginKern { width: i32, right: bool, font: FontId, slot: u16 },
Direction(DirectionBoundary),
FontSpace(Glue, FontId, TokenOrigin),
SpaceAdjustment(i32),
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct NativeParagraph {
@@ -276,7 +279,8 @@ impl Builder {
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, state);
self.push_opentype(&run.font, text, sources, state)?;
return self.adjust_interword_space(state);
}
let dash_breaks = state.parameter(Parameter::XeTeXdashbreakstate).width() > 0;
@@ -317,6 +321,23 @@ impl Builder {
if trailing_break {
self.push_explicit_hyphen_break(run.font.id);
}
self.adjust_interword_space(state)
}
fn adjust_interword_space(&mut self, state: &State) -> Result<(), String> {
if state.input_profile() != InputProfile::XeTeX
|| state.parameter(Parameter::XeTeXinterwordspaceshaping).width() <= 0
{
return Ok(());
}
if let Some((after_glue, width, work)) = interword::adjustment(&self.nodes, state)? {
self.font_steps = self.font_steps.checked_add(work)
.filter(|steps| *steps <= self.font_step_limit)
.ok_or("native OpenType shaping step limit exceeded")?;
if width != 0 {
self.nodes.insert(after_glue, TextNode::SpaceAdjustment(width));
}
}
Ok(())
}
@@ -451,7 +472,7 @@ impl Builder {
code
};
}
pub fn space(&mut self, state: &State) -> Result<(), String> {
pub fn space(&mut self, state: &State, origin: TokenOrigin) -> Result<(), String> {
self.finish_text(true, state)?;
let f = state.fonts.font(state.text_font())?;
let g = f.space();
@@ -505,6 +526,11 @@ impl Builder {
};
self.nodes.push(match name {
Some(name) => TextNode::NamedGlue(g, name),
None if factor == 1000 && s == Glue::default()
&& state.input_profile() == InputProfile::XeTeX && f.opentype().is_some() =>
{
TextNode::FontSpace(g, state.text_font(), origin)
}
None => TextNode::Glue(g),
});
Ok(())
@@ -533,10 +559,11 @@ pub(super) fn hyphen_node(state: &State, font: FontId, c: char, origin: TokenOri
fn discardable(n: &TextNode) -> bool {
matches!(
n,
TextNode::Glue(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _)
| TextNode::NamedGlue(_, _)
| TextNode::Leaders(_)
| TextNode::Kern { .. }
| TextNode::SpaceAdjustment(_)
| TextNode::MathBoundary { .. }
| TextNode::Direction(_)
| TextNode::Penalty(_)
@@ -548,13 +575,14 @@ fn width(n: &TextNode) -> i64 {
glyph.width
}
TextNode::OpenType(run) => run.width,
TextNode::Glue(g)
TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => g.width as i64,
TextNode::Rule(r) => r.dimensions().0,
TextNode::Output(node) => node.dimensions().map_or(0, |(width, _, _)| width),
TextNode::Kern { width: n, .. }
| TextNode::AccentKern(n)
| TextNode::SpaceAdjustment(n)
| TextNode::AutoKern(n)
| TextNode::MarginKern { width: n, .. }
| TextNode::MathBoundary { width: n, .. }
@@ -695,7 +723,7 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
let mut shrink = 0;
for g in std::iter::once(left)
.chain(nodes.iter().filter_map(|n| {
if let TextNode::Glue(g)
if let TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
@@ -715,7 +743,7 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
.filter(|n| {
!matches!(
n,
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
)
})
.map(width)
@@ -1204,7 +1232,7 @@ fn pre_display_line_size(
| TextNode::Indent(_)
| TextNode::Rule(_)
);
if let TextNode::Glue(g)
if let TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
@@ -1239,7 +1267,7 @@ pub(super) fn finish(
if b.nodes.last().is_some_and(|n| {
matches!(
n,
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
)
}) {
b.nodes.pop();
@@ -1262,7 +1290,7 @@ pub(super) fn finish(
.collect();
let shape = state.paragraph_shape().to_vec();
for n in &b.nodes {
if let TextNode::Glue(g)
if let TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
&& g.shrink.order != 0
@@ -1528,7 +1556,7 @@ fn add_hyphens(
.is_some_and(|n| {
matches!(
n,
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
)
})
&& !word.is_empty()
@@ -1559,7 +1587,7 @@ fn add_hyphens(
| TextNode::Insertion(_)
| TextNode::Mark(_)
| TextNode::Output(_)
| TextNode::Glue(_)
| TextNode::Glue(_) | TextNode::FontSpace(_, _, _)
| TextNode::NamedGlue(_, _)
| TextNode::Leaders(_)
| TextNode::Penalty(_)