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

+75 -9
View File
@@ -1,6 +1,7 @@
//! Owned native boxes. Packing follows tex.web hpack/vpackage, not page costs.
use super::*;
use crate::{HorizontalBox, HorizontalItem, Paragraph, ParagraphFont};
mod interword;
mod tail;
pub(super) use tail::TailNodeRef;
mod leaders;
@@ -58,6 +59,9 @@ pub struct NativeBox {
/// Original horizontal semantics, retained before paint-time glue setting.
pub diagnostic: Vec<super::diagnostics::DiagnosticNode>,
pub setting: GlueSetting,
// Prepared only on the disposable shipout box after its glue is set.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) interword_runs: Vec<interword::Run>,
pub badness: i32,
pub diagnostic_eligible: bool,
pub excess: i64,
@@ -240,9 +244,9 @@ impl NativeBox {
}
match &b.content {
BoxContent::Horizontal(nodes) => {
let order = super::direction::visual_order(nodes, rtl && !b.display);
let order = b.horizontal_order(nodes, rtl && !b.display);
let order = order.as_deref();
for index in 0..nodes.len() {
for index in 0..order.map_or(nodes.len(), <[_]>::len) {
let (index, rtl) = order.map_or((index, false), |order| order[index]);
match &nodes[index] {
TextNode::Output(o) => out.push(o),
@@ -358,6 +362,7 @@ impl NativeBox {
| TextNode::MathBoundary { width: w, .. }
| TextNode::AccentKern(w)
| TextNode::AutoKern(w)
| TextNode::SpaceAdjustment(w)
| TextNode::MarginKern { width: w, .. }
| TextNode::Kern { width: w, .. } => HorizontalItem::Box(HorizontalBox {
width: *w as i64,
@@ -368,7 +373,7 @@ impl NativeBox {
penalty: *n as i64,
flagged: false,
},
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => HorizontalItem::Glue {
TextNode::Glue(g) | TextNode::FontSpace(g, _, _) | TextNode::NamedGlue(g, _) => HorizontalItem::Glue {
width: g.width as i64
+ self.setting.adjustment(*g, &mut cumulative, &mut previous),
stretch: 0,
@@ -388,13 +393,71 @@ impl NativeBox {
};
Some(PackedHorizontal::Item(item))
};
let Some(order) = super::direction::visual_order(nodes, rtl) else {
let Some(order) = self.horizontal_order(nodes, rtl) else {
return Some(nodes.iter().filter_map(|node| pack(node).map(|item| (item, false))).collect());
};
// Glue accumulates its rounding in logical order before the device
// reverses a span. Reuse those adjusted widths in visual order.
let mut logical: Vec<_> = nodes.iter().map(pack).collect();
Some(order.into_iter().filter_map(|(i, rtl)| logical[i].take().map(|item| (item, rtl))).collect())
// Accumulate glue rounding in logical order. Glues included in a
// reshaped word were resolved separately and do not update this total.
let retained = if self.interword_runs.is_empty() {
None
} else {
let mut retained = vec![false; nodes.len()];
for &(index, _) in &order {
retained[index] = true;
}
Some(retained)
};
let mut logical: Vec<_> = nodes.iter().enumerate()
.map(|(index, node)| {
if retained.as_ref().is_none_or(|retained| retained[index]) {
pack(node)
} else {
None
}
})
.collect();
for run in &self.interword_runs {
logical[run.nodes.start] = Some(PackedHorizontal::Item(HorizontalItem::Box(
crate::opentype::horizontal_box(&run.word),
)));
}
Some(order.into_iter()
.filter_map(|(index, rtl)| logical[index].take().map(|item| (item, rtl)))
.collect())
}
fn horizontal_order(&self, nodes: &[TextNode], rtl: bool) -> Option<Vec<(usize, bool)>> {
let order = super::direction::visual_order(nodes, rtl);
if self.interword_runs.is_empty() {
return order;
}
let order = order.unwrap_or_else(|| (0..nodes.len()).map(|index| (index, false)).collect());
let mut removed = vec![false; nodes.len()];
for run in &self.interword_runs {
removed[run.nodes.start + 1..run.nodes.end].fill(true);
for &effect in &run.effects {
removed[effect] = true;
}
}
let mut prepared = Vec::with_capacity(order.len());
for (index, rtl) in order {
if removed[index] {
continue;
}
if let Ok(run) = self.interword_runs.binary_search_by_key(&index, |run| run.nodes.start) {
let effects = &self.interword_runs[run].effects;
if rtl {
prepared.extend(effects.iter().rev().map(|&effect| (effect, rtl)));
}
prepared.push((index, rtl));
if !rtl {
prepared.extend(effects.iter().map(|&effect| (effect, rtl)));
}
} else {
prepared.push((index, rtl));
}
}
Some(prepared)
}
pub fn node_count(&self) -> usize {
@@ -599,6 +662,7 @@ pub(super) fn vpack(
content: BoxContent::Vertical(nodes),
diagnostic: Vec::new(),
setting: set,
interword_runs: Vec::new(),
badness,
diagnostic_eligible,
excess,
@@ -666,9 +730,10 @@ pub(super) fn hpack_with_direction_report(
| TextNode::MathBoundary { width: w, .. }
| TextNode::AccentKern(w)
| TextNode::AutoKern(w)
| TextNode::SpaceAdjustment(w)
| TextNode::MarginKern { width: w, .. }
| TextNode::Kern { width: w, .. } => width += *w as i64,
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => {
TextNode::Glue(g) | TextNode::FontSpace(g, _, _) | TextNode::NamedGlue(g, _) => {
width += g.width as i64;
flex(*g, &mut stretch, &mut shrink);
}
@@ -710,6 +775,7 @@ pub(super) fn hpack_with_direction_report(
content: BoxContent::Horizontal(nodes),
diagnostic,
setting: set,
interword_runs: Vec::new(),
badness,
diagnostic_eligible,
excess,