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 {
@@ -418,6 +418,97 @@ fn margin_protrusion_uses_font_slots_in_break_search_and_shipout() {
assert!(page["max_coordinate_error_sp"].as_i64().unwrap() <= 1);
}
#[test]
fn paragraph_protrusion_observes_profile_specific_whatsit_boundaries() {
let source = r"\catcode123=1 \catcode125=2
\font\roman=cmr10 \roman
\hsize=100pt \parfillskip=0pt plus 1fil
\showboxdepth=20 \showboxbreadth=60
\lpcode\roman65=100 \rpcode\roman66=200
\setbox0=\vbox{\noindent AB\par}
\setbox1=\vbox{\noindent\setlanguage0 AB\setlanguage0 \par}
\setbox2=\vbox{\noindent\special{color push gray 0}AB\special{color pop}\par}
\setbox3=\vbox{\noindent\hbox{\setlanguage0 AB\setlanguage0 }\par}
\setbox4=\vbox{\noindent\hbox{\special{color push gray 0}AB\special{color pop}}\par}
\setbox5=\vbox{\noindent\hbox{\setlanguage0 }AB\hbox{\setlanguage0 }\par}
\showbox0 \showbox1 \showbox2 \showbox3 \showbox4 \showbox5
\end
";
for (profile, parameter, expected) in [
(InputProfile::TeX8Bit, "pdfprotrudechars", [2, 2, 2, 2, 2, 2]),
(InputProfile::XeTeX, "XeTeXprotrudechars", [2, 0, 0, 0, 0, 0]),
] {
let execution = Executor::new(
State::initex().with_etex().with_input_profile(profile),
Limits::default(),
)
.with_native_pages()
.run(
SourceFile {
name: "margin-whatsits.tex".into(),
data: format!("\\{parameter}=1\n{source}").into_bytes(),
},
&mut Host::default(),
)
.unwrap();
let actual: Vec<_> = reports(&execution)
.iter()
.map(|report| {
report.text.lines().filter(|line| line.ends_with(" margin)")).count()
})
.collect();
assert_eq!(actual, expected, "{profile:?}");
}
}
#[test]
fn shipout_applies_the_stored_glue_ratio_before_rounding() {
use texrra_tex_core::render::DisplayItem;
let source = r"\catcode123=1 \catcode125=2
\shipout\vbox{\hbox to7sp{\hskip0sp plus47sp\vrule width4sp height1pt\hskip0sp plus47sp}%
\kern10pt\hbox to1sp{\hskip0sp minus47sp\vrule width4sp height1pt\hskip0sp minus47sp}%
\kern10pt\hbox to5sp{\hskip0sp plus49sp\vrule width4sp height1pt\hskip0sp plus49sp}%
\kern10pt\hbox to3sp{\hskip0sp minus49sp\vrule width4sp height1pt\hskip0sp minus49sp}%
\kern10pt\hbox{\vrule width4sp height1pt}}
\end
";
for profile in [InputProfile::TeX8Bit, InputProfile::XeTeX] {
let execution = Executor::new(
State::initex().with_etex().with_input_profile(profile),
Limits::default(),
)
.with_native_pages()
.run(
SourceFile {
name: "rounded-glue.tex".into(),
data: source.as_bytes().to_vec(),
},
&mut font_world::FontFiles,
)
.unwrap();
let page = execution
.events
.iter()
.find_map(|event| match event {
Event::Shipout(page) => Some(page.display_list().unwrap()),
_ => None,
})
.unwrap();
let positions: Vec<_> = page
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { x, .. } => Some(*x),
_ => None,
})
.collect();
let (&origin, rules) = positions.split_last().unwrap();
let offsets: Vec<_> = rules.iter().map(|&x| x - origin).collect();
assert_eq!(offsets, [1, -1, 1, -1], "{profile:?}");
}
}
#[test]
fn font_expansion_codes_invalidate_cached_break_geometry() {
let source = r"\catcode123=1 \catcode125=2