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
+30
View File
@@ -18,6 +18,36 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-29 XeTeX 边缘节点与胶水取整
XeTeX 的边缘字形搜索遇到实际语言切换与特殊指令节点时停止。
pdfTeX 继续跨过相应节点。断行估算、嵌套水平盒以及最终边缘
kern 都使用当前 profile 的节点规则。Web2c 两种 profile 的
盒内胶水先计算浮点比例,随后对累计伸缩量和该比例的乘积取整。
实际正负边界用例的发页坐标与原版一致。三项定向检查通过。
[XeTeX 节点扫描](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/xetexdir/xetex.web#L19993-L20060) · [pdfTeX 节点扫描](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/pdftexdir/pdftex.web#L24475-L24515) · [Web2c 取整](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/lib/zround.c)
原样 NeurIPS 2025 从空辅助文件编译两轮,各有 12 页,逐轮
aux/out 与参考一致。第二轮 29,586 个字形的字号、槽位与原始
坐标逐项相同。第 3 页的 98 处边缘差异和第 9 页 11 处 1 sp
差异归零。144 DPI 灰度像素差异从 12,719 降至 7,070。
PDF 词框及像素仍有差异,首处代表性证据在第一页标题。
在包含 220 项解析后依赖的原样项目会话中编辑第 3 页,再引入
错误并恢复。两次成功修订均只刷新第 3 页,复用约 1,429 万和
1,439 万执行步。每次复用两页绘制列表,转换其余十页。PDF、
页面、SVG、资源读取、来源映射与 aux/out 均与独立全量一致。
出错保留上次发布。该工作量用于诊断,浏览器可见延迟仍须正式采样。
同版本原样 xeCJK-example-CJKfntef 六页的 999 个提取词、词框、
144 DPI 光栅及 aux 均匹配参考。SimSun 仍使用密集重编号的
TrueType 子集,参考保留稀疏 CID 和 CIDSet。LASY6 的嵌入程序
也有结构差异。下一批从 XDV 设备的 TFM 字宽和累计字距入手,
并核对真实 TrueType 子集标识、CIDSet 和资源复用。
证据在 target/p3-xdv-layout-next/,SHA256SUMS 指纹为
5a917b4a89bfadda8751e4953c7f6e1b6ab894e54af92aabcc6bc53df54bddec。
## 2026-11-29 二进制资源读取与默认 ICC 预览
pdfTeX 的 pdffiledump 与 XeTeX 的 filedump 按实际文件内容展开范围,
+7
View File
@@ -1286,6 +1286,13 @@ pass-3/comparison.json 为 64f656d0968bab4b26602bbf8690eabcbc365d094ec89a38a982a
该版原样项目的第 12 页正文编辑、故障及恢复另以独立全量核对。
[官方模板压缩包](https://media.neurips.cc/Conferences/NeurIPS2025/Styles.zip) · [本轮阶段记录](P2_M1_PROGRESS.md#2026-11-29-xetex-旧式字体缺字与原样-neurips-多页编辑)
使用同样的三份原始交付、参考与字体,修正 XeTeX 的突出边缘搜索和
Web2c 胶水取整后,逐轮 aux/out 仍完全一致。第二轮 12 页的 29,586
个原始字形及坐标全部匹配 XDV,原先的两处原始列表差异已消失。
144 DPI 灰度像素差异总数由 12,719 降至 7,070。PDF 词框及像素
仍待闭合。对照见 target/p3-xdv-layout-next/original-candidate/audit-second/。
[阶段记录](P2_M1_PROGRESS.md#2026-11-29-xetex-边缘节点与胶水取整)
单纯生成 PDF 或 DVI 只说明编译路径可达。完整验收要在参考环境闭合后比较页数、
双栏位置及跨栏材料、实际字体/字形、图表浮动、引用收敛、词框及墨迹;测试源
与容差都不得为通过而替换。类 Typst 的编辑延迟另用同批原样项目分规模测量,