Preserve conditional math glue through native layout

This commit is contained in:
gloamxun committed 2026-11-29 02:16:50 +08:00
1 parent 91bb864d8c
commit 745c33fd44
18 files changed
+152 -25

No files matched your search

@@ -385,6 +385,12 @@ impl NativeBox {
shrink: 0,
order: 0,
},
TextNode::ConditionalMathGlue => HorizontalItem::Glue {
width: 0,
stretch: 0,
shrink: 0,
order: 0,
},
TextNode::Leaders(leader) => {
let g = leader.glue;
let width = i64::from(g.width)
@@ -21,6 +21,7 @@ pub enum DiagnosticNode {
value: Glue,
name: Option<Parameter>,
},
ConditionalMathGlue,
Leaders {
value: Glue,
mode: LeaderMode,
@@ -238,6 +239,7 @@ pub(super) fn text_nodes(nodes: &[TextNode]) -> Vec<DiagnosticNode> {
name: None,
},
TextNode::Leaders(leader) => leader_node(leader),
TextNode::ConditionalMathGlue => DiagnosticNode::ConditionalMathGlue,
TextNode::AccentKern(width) => DiagnosticNode::AccentKern(*width),
TextNode::AutoKern(width) => DiagnosticNode::AutoKern(*width),
TextNode::SpaceAdjustment(width) => DiagnosticNode::SpaceAdjustment(*width),
@@ -515,6 +517,12 @@ impl Printer<'_> {
self.number(*w as i64);
}
DiagnosticNode::Glue { value, name } => self.glue(*value, *name),
DiagnosticNode::ConditionalMathGlue => {
self.command("glue");
self.literal("(");
self.command("nonscript");
self.literal(")");
}
DiagnosticNode::Leaders {
value,
mode,
@@ -189,6 +189,7 @@ impl Printer<'_> {
name: *name,
}),
MathNode::Leaders(leader) => Some(leader_node(leader)),
MathNode::ConditionalMathGlue => Some(DiagnosticNode::ConditionalMathGlue),
MathNode::Kern {
amount,
mu: false,
@@ -4152,6 +4152,7 @@ impl Executor {
}
}
Relax => {}
Nonscript => return self.fail("Missing $ inserted"),
Halign | Valign => self.start_alignment(primitive, token.span, files)?,
Cr | Crcr | Span | Omit | Noalign => {
return self.fail("misplaced native alignment command");
@@ -180,7 +180,8 @@ impl Executor {
displacement = (width - b.width - e + 1).div_euclid(2);
if matches!(
material.first(),
Some(TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_))
Some(TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue | TextNode::Leaders(_))
) {
displacement = 0;
}
@@ -20,6 +20,7 @@ impl Tail {
TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => Self::Glue(*g, false),
TextNode::ConditionalMathGlue => Self::Glue(Glue::default(), false),
TextNode::Kern { width, .. } | TextNode::AccentKern(width) | TextNode::AutoKern(width) | TextNode::SpaceAdjustment(width) => {
Self::Kern(*width)
}
@@ -61,6 +62,7 @@ impl Tail {
match node {
MathNode::Leaders(leader) => Self::Glue(leader.glue, false),
MathNode::Glue(g) => Self::Glue(*g, true),
MathNode::ConditionalMathGlue => Self::Glue(Glue::default(), false),
MathNode::ExplicitSpace(g, _) => Self::Glue(*g, false),
MathNode::Kern { amount, .. } => Self::Kern(*amount),
MathNode::Penalty(n) => Self::Penalty(*n),
@@ -799,6 +799,9 @@ impl Executor {
.push(MathNode::ExplicitSpace(g, None));
}
Leaders | Cleaders | Xleaders => self.scan_leaders(*p, files)?,
Nonscript => {
self.math.last_mut().unwrap().nodes.push(MathNode::ConditionalMathGlue);
}
Mskip => {
let g = self.skip_glue(*p, files)?;
self.math.last_mut().unwrap().nodes.push(MathNode::Glue(g));
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 108;
const COMPATIBLE_SCHEMAS: [u32; 74] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, SCHEMA,
const SCHEMA: u32 = 109;
const COMPATIBLE_SCHEMAS: [u32; 75] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -238,6 +238,9 @@ impl FormatImage {
if image.schema < 108 {
image.state.install_middle_command();
}
if image.schema < 109 {
image.state.install_nonscript_command();
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
+38 -16
View File
@@ -205,6 +205,7 @@ pub enum MathNode {
/// Display, text, script, and scriptscript lists, in scanning order.
Choice(Box<[Vec<MathNode>; 4]>),
Glue(Glue),
ConditionalMathGlue,
/// Normal-unit glue, including explicit spaces and horizontal skip commands.
ExplicitSpace(Glue, Option<Parameter>),
Leaders(LeaderGlue),
@@ -293,11 +294,18 @@ impl MathBuilder {
// At middle/right, delimiters preceding the numerator stay at
// the outer level. Only the material since the last middle is
// rearranged into the generalized fraction.
let start = fraction.numerator.iter().rposition(
|node| matches!(node, MathNode::MiddleDelimiter { .. }),
).map_or(0, |index| index + 1);
let numerator = fraction.numerator.split_off(start);
let denominator = std::mem::replace(&mut self.nodes, fraction.numerator);
let denominator = std::mem::take(&mut self.nodes);
let numerator = if self.left.is_some()
&& let Some(last) = fraction.numerator.iter().rposition(
|node| matches!(node, MathNode::MiddleDelimiter { .. }),
)
{
let numerator = fraction.numerator.split_off(last + 1);
self.nodes = fraction.numerator;
numerator
} else {
fraction.numerator
};
self.nodes.push(MathNode::Fraction {
numerator,
denominator,
@@ -801,6 +809,21 @@ impl Converter<'_> {
}],
MathNode::Leaders(leader) => vec![TextNode::Leaders(leader.clone())],
MathNode::Glue(g) => vec![TextNode::Glue(self.glue(*g, style)?)],
MathNode::ConditionalMathGlue => {
// In script styles, remove only the immediate glue/kern
// successor. The zero glue itself remains in the hlist.
if style.size_index() != 0
&& matches!(
pending.front(),
Some(MathNode::Glue(_) | MathNode::ExplicitSpace(..)
| MathNode::ConditionalMathGlue | MathNode::Leaders(_)
| MathNode::Kern { .. })
)
{
pending.pop_front();
}
vec![TextNode::ConditionalMathGlue]
}
MathNode::Kern {
amount,
mu,
@@ -849,13 +872,14 @@ impl Converter<'_> {
style = *s;
continue;
}
if let MathNode::Delimiter { value, source, .. }
| MathNode::MiddleDelimiter { value, source } = node
{
style = original_style;
// Position is recorded after inter-noad spacing below.
delimiters.push((out.len(), *value, source));
}
let delimiter = match node {
MathNode::Delimiter { value, source, .. }
| MathNode::MiddleDelimiter { value, source } => {
style = original_style;
Some((*value, source))
}
_ => None,
};
if let Some(current) = cls[i] {
if let Some(left) = previous {
let level = b"0234000122*4000133**3**344*0400400*000000234000111*1111112341011"
@@ -878,10 +902,8 @@ impl Converter<'_> {
}
previous = Some(if closes_delimiter(node) { Open } else { current } as usize);
}
if let Some((index, _, _)) = delimiters.last_mut()
&& matches!(node, MathNode::Delimiter { .. } | MathNode::MiddleDelimiter { .. })
{
*index = out.len();
if let Some((value, source)) = delimiter {
delimiters.push((out.len(), value, source));
}
out.append(&mut translated[i]);
if penalties
@@ -268,7 +268,8 @@ fn first_script_char(field: &MathField, mut style: MathStyle) -> Option<(MathCha
match node {
MathNode::Style(next) => style = *next,
MathNode::Choice(branches) => list = &branches[usize::from(style as u8 / 2)],
MathNode::Kern { .. } | MathNode::Glue(_) | MathNode::ExplicitSpace(..) => {}
MathNode::Kern { .. } | MathNode::Glue(_) | MathNode::ExplicitSpace(..)
| MathNode::ConditionalMathGlue => {}
MathNode::Noad(n)
if n.class != NoadClass::Inner
&& n.accent.is_none()
@@ -65,6 +65,8 @@ pub enum TextNode {
},
Glue(Glue),
NamedGlue(Glue, Parameter),
/// Zero glue retained by the conversion of a conditional math marker.
ConditionalMathGlue,
Leaders(LeaderGlue),
Kern {
width: i32,
@@ -608,6 +610,7 @@ fn discardable(n: &TextNode) -> bool {
n,
TextNode::Glue(_) | TextNode::FontSpace(_, _, _)
| TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue
| TextNode::Leaders(_)
| TextNode::Kern { .. }
| TextNode::SpaceAdjustment(_)
@@ -822,7 +825,8 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
.filter(|n| {
!matches!(
n,
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue | TextNode::Leaders(_)
)
})
.map(width)
@@ -1423,7 +1427,8 @@ pub(super) fn finish(
if b.nodes.last().is_some_and(|n| {
matches!(
n,
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue | TextNode::Leaders(_)
)
}) {
b.nodes.pop();
@@ -1718,7 +1723,8 @@ fn add_hyphens(
.is_some_and(|n| {
matches!(
n,
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue | TextNode::Leaders(_)
)
})
&& !word.is_empty()
@@ -1751,6 +1757,7 @@ fn add_hyphens(
| TextNode::Output(_)
| TextNode::Glue(_) | TextNode::FontSpace(_, _, _)
| TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue
| TextNode::Leaders(_)
| TextNode::Penalty(_)
| TextNode::AccentKern(_)
@@ -156,6 +156,7 @@ impl BreakNode {
TextNode::Glue(glue) | TextNode::FontSpace(glue, _, _)
| TextNode::NamedGlue(glue, _)
| TextNode::Leaders(LeaderGlue { glue, .. }) => Self::Glue(*glue),
TextNode::ConditionalMathGlue => Self::Glue(Glue::default()),
TextNode::AccentKern(width) => Self::AccentKern(*width),
TextNode::MarginKern { width, .. } | TextNode::AutoKern(width) => {
Self::Content(i64::from(*width))
@@ -27,7 +27,8 @@ pub(super) fn add_hyphens(
| TextNode::Kern {
explicit: false, ..
} => {}
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_) => {
TextNode::Glue(_) | TextNode::FontSpace(_, _, _) | TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue | TextNode::Leaders(_) => {
after_glue = auto_breaking;
}
TextNode::MathBoundary { after, .. } => {
@@ -203,6 +204,7 @@ fn following_nodes_allow_hyphenation(nodes: &[TextNode]) -> bool {
| TextNode::Output(_)
| TextNode::Glue(_) | TextNode::FontSpace(_, _, _)
| TextNode::NamedGlue(_, _)
| TextNode::ConditionalMathGlue
| TextNode::Leaders(_)
| TextNode::Penalty(_)
| TextNode::AccentKern(_)
@@ -125,6 +125,7 @@ fn skippable(node: &TextNode) -> bool {
| 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, _)
| TextNode::Leaders(LeaderGlue { glue, .. }) => glue.canonical_zero,
@@ -255,6 +255,7 @@ impl Tracer {
i += usize::from(*replacement_count);
}
TextNode::Kern { .. } | TextNode::AccentKern(_) | TextNode::Penalty(_)
| TextNode::ConditionalMathGlue
| TextNode::AutoKern(_) | TextNode::SpaceAdjustment(_) | TextNode::MarginKern { .. } => {}
}
i += 1;
@@ -233,6 +233,7 @@ pub enum Primitive {
Skewchar,
Mathaccent,
Mathchoice,
Nonscript,
Overline,
Underline,
Eqno,
@@ -1116,6 +1117,7 @@ impl Default for State {
("accent", Accent),
("mathaccent", Mathaccent),
("mathchoice", Mathchoice),
("nonscript", Nonscript),
("overline", Overline),
("underline", Underline),
("eqno", Eqno),
@@ -1902,6 +1904,10 @@ impl State {
}
}
pub(super) fn install_nonscript_command(&mut self) {
self.update_profile_command("nonscript", Meaning::Primitive(Primitive::Nonscript), true);
}
pub(super) fn install_middle_command(&mut self) {
self.update_profile_command(
"middle",
@@ -97,6 +97,31 @@ fn mathcodes_active_characters_families_scripts_and_modes_execute() {
e.state.box_register(1).unwrap().width
);
}
#[test]
fn conditional_math_glue_is_retained_for_list_queries_and_showbox() {
let e = run(r"\setbox0=\hbox{$\scriptstyle a\nonscript
\message{TAIL=\number\lastnodetype,\the\lastskip}%
\hskip4pt b$}
\showboxbreadth=10 \showboxdepth=10 \showbox0
\setbox1=\hbox{$\scriptstyle a\nonscript\unskip\hskip4pt b$}");
assert!(e.events.iter().any(|event| matches!(
event, Event::Message { text, .. } if text == "TAIL=11,0.0pt"
)));
assert!(e.events.iter().any(|event| matches!(
event, Event::ShowBox(record) if record.text.contains(r"\glue(\nonscript)")
)));
let m = math(&e);
assert_eq!(
m[0].horizontal.iter().filter(|node| matches!(node, TextNode::ConditionalMathGlue)).count(),
1,
);
assert!(!m[0].horizontal.iter().any(|node| matches!(node, TextNode::Glue(_))));
assert_eq!(
e.state.box_register(1).unwrap().width - e.state.box_register(0).unwrap().width,
4 * 65_536,
);
}
#[test]
fn incomplete_math_fonts_modes_and_fields_are_strict_errors() {
for body in [
@@ -185,6 +210,16 @@ fn raw_math_geometry_and_hlist_penalties_match_real_initex() {
r"\mathop a+b",
r"\mathinner{a+b}=c",
r"a\mskip3mu plus2mu b",
r"a\nonscript\mskip8mu b",
r"\displaystyle a\nonscript\mkern8mu b",
r"\scriptstyle a\nonscript\mskip8mu b",
r"\scriptscriptstyle a\nonscript\hskip4pt b",
r"a^{b\nonscript\mkern8mu c}",
r"\scriptstyle a\nonscript\penalty0\hskip4pt b",
r"\scriptstyle a\nonscript\textstyle\hskip4pt b",
r"\scriptstyle a\nonscript\mathchoice{}{}{}{}\kern4pt b",
r"\scriptstyle a\nonscript\nonscript\hskip4pt b",
r"\scriptstyle a\nonscript\leaders\hbox{x}\hskip6pt b",
r"a\mkern2mu b",
r"a\kern2pt b",
r"a+\textstyle b",
+26
View File
@@ -18,6 +18,32 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-29 条件数学胶水与双栏表格
两种 profile 支持 TeX 的 Nonscript 原语。扫描标记时保留真实零胶水,
用于尾节点查询、移除与列表诊断。数学列表转换在脚标或二级脚标中移除
紧随的胶水或 kern,原标记进入实际水平列表。正文和陈列样式保留后继节点。
样式、罚点、命令选择、连续标记和 leaders 都按实际节点顺序处理。
schema109 记录新原语,旧格式重载保留已有命令绑定。
[pdfTeX 原版规则](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/pdftexdir/pdftex.web#L20839-L20857) ·
[XeTeX 原版规则](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/xetexdir/xetex.web#L16272-L16290)
实际双栏 article 的原样 amsmath 使用 colon 和 bmod。此前在矩阵
的首个 colon 停编。采用原样 array、tabularx、dcolumn、booktabs、
hhline 和 colortbl 后完成两轮实际作业。两轮 aux、72 个词框及
144 DPI 灰度光栅分别与 pdfLaTeX 一致。花体字体、图像和完整出版矩阵
继续按原门禁统一验收。
两种 profile 各有 12 个实际原版盒尺寸及最后胶水类型对照,pdfTeX
兼容模式的 12 页 DVI 字形及规则坐标逐项一致。数学、断行和定界符的
八项定向回归通过。三页编辑会话中改第 2 页的脚标公式与表格数值,
两次编辑和一次恢复各复用 3,163,638 个执行步,只更新第 2 页。三个
发布版本的 PDF、绘制列表、SVG、来源映射、aux 和日志与同生成初态独立
全量一致。错误版本保留上次完整发布。
证据在 target/p3-math-table-spacing/。继续从真实浮动和跨页排版项目
定位通用缺口,正式性能仍需以浏览器可见页面和既定采样门禁测量。
## 2026-11-29 中置数学分隔符与真实公式项目
pdfTeX 和 XeTeX profile 的 e-TeX 模式支持 `\middle`。每个中置