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$",