Match XeTeX margin boundaries and Web2c glue rounding

This commit is contained in:
gloamxun committed 2026-11-29 03:21:19 +08:00
1 parent 52db77885d
commit 9da75c82f5
5 files changed
+150 -19

No files matched your search

@@ -599,12 +599,12 @@ impl GlueSetting {
return 0;
}
*cumulative += f.amount as i128;
let n = *cumulative * self.numerator as i128 * self.sign as i128;
let d = self.denominator as i128;
// web2c rounds signed cumulative glue away from zero at half a sp.
// TeX's vet_glue clamps the cumulative adjustment before shipping it.
let now = (n.signum() * d.signum() * ((n.abs() + d.abs() / 2) / d.abs()))
.clamp(-1_000_000_000, 1_000_000_000) as i64;
// Web2c first stores the floating-point ratio when it packs the box.
// Multiply by that rounded ratio before applying vet_glue and round.
let ratio = self.numerator as f64 / self.denominator as f64;
let amount = (ratio * *cumulative as f64 * f64::from(self.sign))
.clamp(-1_000_000_000.0, 1_000_000_000.0);
let now = (amount + 0.5_f64.copysign(amount)) as i64;
let result = now - *previous;
*previous = now;
result
@@ -40,32 +40,32 @@ impl Edge {
}
}
fn node(node: &TextNode, right: bool) -> Self {
fn node(node: &TextNode, right: bool, profile: InputProfile) -> Self {
match node {
TextNode::Box(boxed) => match boxed.native_box.as_deref() {
Some(NativeBox {
content: BoxContent::Horizontal(nodes),
..
}) if !nodes.is_empty() => Self::list(nodes, right),
}) if !nodes.is_empty() => Self::list(nodes, right, profile),
Some(NativeBox {
content: BoxContent::Horizontal(_),
..
}) if boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 => Self::Skip,
_ => Self::Solid,
},
_ if skippable(node) => Self::Skip,
_ if skippable(node, profile) => Self::Skip,
_ => Self::character(node, right),
}
}
fn list(nodes: &[TextNode], right: bool) -> Self {
fn list(nodes: &[TextNode], right: bool, profile: InputProfile) -> Self {
let mut nodes = nodes.iter();
while let Some(node) = if right {
nodes.next_back()
} else {
nodes.next()
} {
let edge = Self::node(node, right);
let edge = Self::node(node, right, profile);
if !matches!(edge, Self::Skip) {
return edge;
}
@@ -105,15 +105,18 @@ pub(super) fn level(state: &State, parameters: &BTreeMap<Parameter, ParameterVal
parameters[&parameter].width()
}
fn skippable(node: &TextNode) -> bool {
fn skippable(node: &TextNode, profile: InputProfile) -> bool {
match node {
TextNode::Box(boxed) => matches!(
boxed.native_box.as_deref(),
Some(NativeBox { content: BoxContent::Horizontal(nodes), .. })
if nodes.is_empty() && boxed.width == 0 && boxed.height == 0 && boxed.depth == 0
),
TextNode::Language(_)
| TextNode::Mark(_)
// XeTeX's edge scan stops at non-glyph whatsits, including language
// changes and specials. pdfTeX skips them except images and forms.
TextNode::Language(_) => profile != InputProfile::XeTeX,
TextNode::Output(node) => profile != InputProfile::XeTeX && node.dimensions().is_none(),
TextNode::Mark(_)
| TextNode::Adjustment(_)
| TextNode::Insertion(_)
| TextNode::Penalty(_)
@@ -124,7 +127,6 @@ fn skippable(node: &TextNode) -> bool {
| TextNode::Direction(_)
| TextNode::Kern { explicit: false, .. }
| TextNode::Kern { width: 0, .. } => true,
TextNode::Output(node) => node.dimensions().is_none(),
TextNode::ConditionalMathGlue => true,
TextNode::Glue(glue) | TextNode::FontSpace(glue, _, _)
| TextNode::NamedGlue(glue, _)
@@ -143,7 +145,7 @@ pub(in crate::execution) fn stored_margin_kern(nodes: &[TextNode], right: bool)
let side_skip = if right { Parameter::Rightskip } else { Parameter::Leftskip };
let non_skippable = |node: &&TextNode| {
!matches!(node, TextNode::NamedGlue(_, parameter) if *parameter == side_skip)
&& !skippable(node)
&& !skippable(node, InputProfile::TeX8Bit)
};
// The query walks only the stored hlist. It does not descend into a nested
// box or recalculate protrusion using the current font codes.
@@ -169,7 +171,7 @@ pub(super) fn margin_kern(
let edge = if direct {
nodes.last().map_or(Edge::Solid, |node| Edge::character(node, right))
} else {
Edge::list(nodes, right)
Edge::list(nodes, right, state.input_profile())
};
let (font, slot) = match edge {
Edge::Glyph(font, slot) => (font, u16::from(slot)),
@@ -227,8 +229,9 @@ impl Search {
}
Ok(EdgeMeasure { width, expanding })
};
let profile = state.input_profile();
for (i, node) in nodes.iter().enumerate() {
let edge = Edge::node(node, true);
let edge = Edge::node(node, true, profile);
widths[i + 1].right = if matches!(edge, Edge::Skip) {
widths[i].right
} else {
@@ -242,7 +245,7 @@ impl Search {
}
}
for (i, node) in nodes.iter().enumerate().rev() {
let edge = Edge::node(node, false);
let edge = Edge::node(node, false, profile);
widths[i].left = if matches!(edge, Edge::Skip) {
widths[i + 1].left
} else {