Support native e-TeX middle delimiters and math segments

This commit is contained in:
gloamxun committed 2026-11-29 02:05:21 +08:00
1 parent e3be6dcd09
commit 91bb864d8c
8 files changed
+134 -27

No files matched your search

@@ -222,6 +222,10 @@ impl Printer<'_> {
self.command(if *opening { "left" } else { "right" });
self.delimiter(*value);
}
MathNode::MiddleDelimiter { value, .. } => {
self.command("middle");
self.delimiter(*value);
}
MathNode::Style(style) => self.command(match style {
MathStyle::Display | MathStyle::DisplayCramped => "displaystyle",
MathStyle::Text | MathStyle::TextCramped => "textstyle",
@@ -74,6 +74,7 @@ impl Tail {
// all noads) to unset_node + 2, rather than exposing raw tags.
MathNode::Noad(_)
| MathNode::Delimiter { .. }
| MathNode::MiddleDelimiter { .. }
| MathNode::Style(_)
| MathNode::Choice(_)
| MathNode::Fraction { .. } => Self::Other(15),
@@ -98,13 +98,13 @@ impl Executor {
PaintSource::token(origin),
))
}
fn left_right(
fn math_delimiter(
&mut self,
opening: bool,
command: Primitive,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if opening {
if command == Primitive::Left {
if self.math.len() >= self.limits.input_depth.min(32) {
return self.fail("native math nesting capacity exceeded");
}
@@ -116,14 +116,28 @@ impl Executor {
} else {
let b = self.math.last().unwrap();
if b.left.is_none() || b.depth != self.state.group_depth() {
if command == Primitive::Middle {
return self.fail(r"Extra \middle.");
}
return self.fail("unmatched native right delimiter");
}
let (right, source) = self.scan_delimiter(files)?;
let (value, source) = self.scan_delimiter(files)?;
if command == Primitive::Middle {
let b = self.math.last_mut().unwrap();
b.finish_segment();
b.nodes.push(MathNode::MiddleDelimiter { value, source });
// Middle ends the current delimiter group and starts another.
// The math list survives, while local assignments and delayed
// tokens follow the ordinary rules for closing a group.
self.finish_group(GroupKind::MathLeft, span, files)?;
self.begin_group(GroupKind::MathLeft, files)?;
return Ok(());
}
let b = self.math.pop().unwrap();
let span = b.span;
let mut nodes = b.finish();
nodes.push(MathNode::Delimiter {
value: right,
value,
opening: false,
source,
});
@@ -625,7 +639,7 @@ impl Executor {
let r = self.scan_rule(false, TokenOrigin::from(token), files)?;
self.math.last_mut().unwrap().nodes.push(MathNode::Rule(r));
}
Left | Right => self.left_right(*p == Left, token.span, files)?,
Left | Middle | Right => self.math_delimiter(*p, token.span, files)?,
Umathchar | Umathcharnum | Udelimiter => {
let code = self.scan_unicode_math_code(*p != Umathcharnum, files)?;
let (class, c) = self.explicit_math_code(code)?;
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 107;
const COMPATIBLE_SCHEMAS: [u32; 73] = [
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, SCHEMA,
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 ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -235,6 +235,9 @@ impl FormatImage {
if image.schema < 107 {
image.state.install_pdf_insertion_query_command();
}
if image.schema < 108 {
image.state.install_middle_command();
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
+53 -16
View File
@@ -197,6 +197,10 @@ pub enum MathNode {
opening: bool,
source: PaintSource,
},
MiddleDelimiter {
value: Delimiter,
source: PaintSource,
},
Style(MathStyle),
/// Display, text, script, and scriptscript lists, in scanning order.
Choice(Box<[Vec<MathNode>; 4]>),
@@ -284,20 +288,30 @@ impl MathBuilder {
span,
}
}
pub fn finish(self) -> Vec<MathNode> {
let mut nodes = if let Some(fraction) = self.numerator {
vec![MathNode::Fraction {
numerator: fraction.numerator,
denominator: self.nodes,
pub fn finish_segment(&mut self) {
if let Some(mut fraction) = self.numerator.take() {
// 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);
self.nodes.push(MathNode::Fraction {
numerator,
denominator,
specification: fraction.specification,
span: fraction.origin.span,
source: PaintSource::token(fraction.origin).derived(Derivation::Fraction),
}]
} else {
self.nodes
};
});
}
}
pub fn finish(mut self) -> Vec<MathNode> {
self.finish_segment();
if let Some((value, source)) = self.left {
nodes.insert(
self.nodes.insert(
0,
MathNode::Delimiter {
value,
@@ -306,7 +320,7 @@ impl MathBuilder {
},
);
}
nodes
self.nodes
}
}
pub(super) fn field(mut nodes: Vec<MathNode>) -> (MathField, FieldSource) {
@@ -373,9 +387,16 @@ fn class(n: &MathNode) -> Option<NoadClass> {
} else {
NoadClass::Close
}),
MathNode::MiddleDelimiter { .. } => Some(NoadClass::Close),
_ => None,
}
}
fn closes_delimiter(node: &MathNode) -> bool {
matches!(
node,
MathNode::Delimiter { opening: false, .. } | MathNode::MiddleDelimiter { .. }
)
}
pub(super) struct Converter<'a> {
pub state: &'a State,
pub horizontal_packs: std::cell::Cell<usize>,
@@ -735,7 +756,9 @@ impl Converter<'_> {
if let MathNode::Style(s) = &node {
style = *s;
}
let prior = previous.and_then(|i| prepared.get(i)).and_then(class);
let prior = previous.and_then(|i| prepared.get(i)).and_then(|node| {
if closes_delimiter(node) { Some(Open) } else { class(node) }
});
if let MathNode::Noad(n) = &mut node {
if n.class == Bin
&& prior.is_none_or(|p| matches!(p, Bin | Op | Rel | Open | Punct))
@@ -755,7 +778,9 @@ impl Converter<'_> {
}
let list = match &node {
MathNode::Noad(n) => self.translate_noad(n, style)?,
MathNode::Delimiter { .. } => vec![TextNode::Box(HorizontalBox::default())],
MathNode::Delimiter { .. } | MathNode::MiddleDelimiter { .. } => {
vec![TextNode::Box(HorizontalBox::default())]
}
MathNode::Fraction {
numerator,
denominator,
@@ -798,6 +823,9 @@ impl Converter<'_> {
MathNode::Style(_) => Vec::new(),
MathNode::Choice(_) => unreachable!("choice was spliced before conversion"),
};
if closes_delimiter(&node) {
style = original_style;
}
if class(&node).is_some() {
previous = Some(prepared.len());
}
@@ -821,6 +849,13 @@ 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));
}
if let Some(current) = cls[i] {
if let Some(left) = previous {
let level = b"0234000122*4000133**3**344*0400400*000000234000111*1111112341011"
@@ -841,10 +876,12 @@ impl Converter<'_> {
out.push(TextNode::NamedGlue(self.glue(g, style)?, p));
}
}
previous = Some(current as usize);
previous = Some(if closes_delimiter(node) { Open } else { current } as usize);
}
if let MathNode::Delimiter { value, source, .. } = node {
delimiters.push((out.len(), *value, source));
if let Some((index, _, _)) = delimiters.last_mut()
&& matches!(node, MathNode::Delimiter { .. } | MathNode::MiddleDelimiter { .. })
{
*index = out.len();
}
out.append(&mut translated[i]);
if penalties
@@ -320,6 +320,7 @@ pub enum Primitive {
Delimiter,
Radical,
Left,
Middle,
Right,
Mathchar,
Char,
@@ -1624,6 +1625,7 @@ impl State {
use Primitive::*;
self.profile = Profile::EtexBootstrap;
self.install_line_penalty_primitives();
self.install_middle_command();
self.install_primitive_commands();
let hook = TokenParameter::Everyeof;
self.command_bindings_mut().insert(
@@ -1900,6 +1902,14 @@ impl State {
}
}
pub(super) fn install_middle_command(&mut self) {
self.update_profile_command(
"middle",
Meaning::Primitive(Primitive::Middle),
self.profile == Profile::EtexBootstrap,
);
}
pub(super) fn restore_legacy_etex_parameter(&mut self, parameter: Parameter) {
if self.profile == Profile::EtexBootstrap {
self.command_bindings_mut()
@@ -149,6 +149,13 @@ fn delimiter_variants_recipes_dimensions_and_dvi_positions_match_initex() {
r"\left(\textfont0=\sevenrm a\right)",
r#"\left(a\delcode41="05D303\right)"#,
r"\left(a\over b\right)",
r"\left(a\middle|b\right)",
r"\left(\vbox to40pt{\vfil}\middle|x\right)",
r"\left(\scriptstyle a\middle|b\middle.\scriptscriptstyle c\right)",
r"\left(+a+\middle|+b=\middle|=c+\right)",
r"\left(a\over b\middle|c\over d\middle|e\over f\right)",
r"\left(\left(x\middle|y\right)\middle|\left(a\over b\right)\right)",
r"\left(a\middle|^2 b\right)",
r"\left(\left.a\over b\right]\right)",
r"\left.a+b\right)",
r#"\delimiter"4028300 a\delimiter"5029301"#,
@@ -273,8 +280,11 @@ fn delimiter_variants_recipes_dimensions_and_dvi_positions_match_initex() {
x: x + p.x,
y: p.y,
}),
MathItem::Glyph { .. } | MathItem::PdfImage(_) | MathItem::OpenType(_) => {
panic!("legacy glyph identity in strict native math")
MathItem::Glyph { .. }
| MathItem::PdfImage(_)
| MathItem::PdfForm(_)
| MathItem::OpenType(_) => {
panic!("unexpected item in the TFM math comparison")
}
MathItem::Rule { width, height } => {
rules.push((x + p.x, p.y, *width, *height))
@@ -392,6 +402,8 @@ fn unsupported_delimiter_fields_ranges_and_geometry_capacity_fail_strictly() {
r"\advance\catcode65 by1",
r"$\left a b\right)$",
r"$\right)a$",
r"$\middle|a$",
r"$\left(\begingroup a\middle|b\endgroup\right)$",
r"$\left(a$",
r"$\left(a}$",
r"$\left(\begingroup a\right)\endgroup$",
+26
View File
@@ -18,6 +18,32 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-29 中置数学分隔符与真实公式项目
pdfTeX 和 XeTeX profile 的 e-TeX 模式支持 `\middle`。每个中置
分隔符结束当前分组并开始同级新分组,恢复局部赋值并调度 aftergroup
中的 token。多段广义分数保留各自的分子和分母,嵌套公式使用自身的
分隔符组。布局按完整数学列表的高度和深度共同选择外侧和中间分隔符,
保留中间节点的左右间距语义、数学样式和来源。schema108 记录新原语,
旧格式可重载,已有命令绑定保留。
[pdfTeX 的分组和广义分数](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/pdftexdir/pdftex.web#L28770-L28859) ·
[XeTeX 的中置分隔符](https://raw.githubusercontent.com/TeX-Live/texlive-source/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/xetexdir/xetex.web#L24944-L24968)
两种 profile 分别以实际 INITEX 对照八个盒子的宽、高、深及组状态。
无匹配分隔符时两边均报错。既有定向套件六项通过,覆盖 466 组实际字号、
原始字形槽位、DVI 字形和规则坐标,含新增的分隔符组合。补齐该套件中
旧的 PDF Form 枚举分支,保留原字形和规则断言。
实际 LaTeX article、原样 CTAN braket.sty 和原样 amsmath 的一页
组合项目连续编译两轮,涵盖分隔符围住的分数与矩阵、双竖线、集合条件及
交叉引用。两轮 aux 逐字节匹配 pdfLaTeX,53 个词框及 144 DPI 灰度光栅逐像素匹配。
原生第一次尝试达到 CLI 默认执行上限,成功复验对新样例显式指定执行
上限,保留原始失败日志。全部证据在 target/p3-middle-delimiters/。
[Braket 发布目录](https://ctan.org/tex-archive/macros/latex/contrib/braket)
下批继续处理真实数学和表格项目的共享列表行为,复用当前参考产物。
原样出版模板矩阵及实时性能门禁在同一冻结版统一验收。
## 2026-11-29 按流输入的已读范围恢复检查点
检查点区分完整文件消费与逐行流输入。只从流读取一部分时,按实际已观察