From d02777a1fc137c9c8ac935526829e40e8b795b19 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 21 Nov 2026 06:26:49 +0800 Subject: [PATCH] Report active native math alignment and output lists --- .../src/execution/diagnostics.rs | 2 + .../src/execution/diagnostics/math.rs | 298 +++++++++++++++ .../src/execution/engine/alignment.rs | 52 ++- .../src/execution/engine/boxes.rs | 8 +- .../src/execution/engine/diagnostics.rs | 175 +-------- .../execution/engine/diagnostics/activity.rs | 348 ++++++++++++++++++ .../src/execution/engine/math.rs | 3 +- .../src/execution/engine/page.rs | 12 +- .../src/execution/insertions.rs | 18 + .../src/execution/page_builder.rs | 4 + docs/P2_M1_PROGRESS.md | 24 +- tests/native_generated_files.rs | 34 ++ 12 files changed, 779 insertions(+), 199 deletions(-) create mode 100644 crates/texrra-tex-core/src/execution/diagnostics/math.rs create mode 100644 crates/texrra-tex-core/src/execution/engine/diagnostics/activity.rs diff --git a/crates/texrra-tex-core/src/execution/diagnostics.rs b/crates/texrra-tex-core/src/execution/diagnostics.rs index 11415bf..429e7cf 100644 --- a/crates/texrra-tex-core/src/execution/diagnostics.rs +++ b/crates/texrra-tex-core/src/execution/diagnostics.rs @@ -1,6 +1,8 @@ //! Lossless native list diagnostics; never infer TeX nodes from painted positions. use super::registers::print_scaled; use super::*; +mod math; +pub(super) use math::show_math; #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum DiagnosticNode { Language(HyphenationLanguage), diff --git a/crates/texrra-tex-core/src/execution/diagnostics/math.rs b/crates/texrra-tex-core/src/execution/diagnostics/math.rs new file mode 100644 index 0000000..4fa0f51 --- /dev/null +++ b/crates/texrra-tex-core/src/execution/diagnostics/math.rs @@ -0,0 +1,298 @@ +use super::*; +use crate::math_engine_tex::MathStyle; + +impl Printer<'_> { + fn delimiter(&mut self, delimiter: Delimiter) { + let small = u32::from(delimiter.small.family) * 256 + u32::from(delimiter.small.slot); + let large = u32::from(delimiter.large.family) * 256 + u32::from(delimiter.large.slot); + self.literal(&format!("\"{:X}", (small << 12) | large)); + } + + fn math_character(&mut self, character: MathChar) { + self.command("fam"); + self.literal(&format!("{} ", character.family)); + self.ascii(character.slot); + } + + fn math_field(&mut self, field: &MathField, parent: &str, part: char) -> Result<(), String> { + if matches!(field, MathField::Empty) { + return Ok(()); + } + if parent.len() as i32 >= self.depth { + self.literal(" []"); + return Ok(()); + } + let prefix = format!("{parent}{part}"); + match field { + MathField::Empty => {} + MathField::Char(character) => { + self.line(&prefix); + self.math_character(*character); + } + // math_text_char is only introduced by conversion, after the + // original noad can appear in the active list. + MathField::TextChar(_) => {} + MathField::List(nodes) => self.math_sublist(nodes, None, &prefix)?, + MathField::Box(boxed) => { + self.line(&prefix); + self.box_(&box_view(boxed), &prefix)?; + } + MathField::Radical(_, argument) => self.math_field(argument, parent, part)?, + } + Ok(()) + } + + fn math_sublist( + &mut self, + nodes: &[MathNode], + leading: Option, + prefix: &str, + ) -> Result<(), String> { + if nodes.is_empty() && leading.is_none() { + self.line(prefix); + self.literal("{}"); + } else { + self.math_list(nodes, leading, prefix)?; + } + Ok(()) + } + + fn noad(&mut self, noad: &Noad, prefix: &str) -> Result<(), String> { + if let Some(accent) = noad.accent { + self.command("accent"); + self.math_character(accent); + } else if let MathField::Radical(delimiter, _) = &noad.nucleus { + self.command("radical"); + self.delimiter(*delimiter); + } else if let Some(rule) = noad.rule { + self.command(match rule { + MathRulePosition::Over => "overline", + MathRulePosition::Under => "underline", + }); + } else if noad.vcenter { + self.command("vcenter"); + } else { + self.command(match noad.class { + NoadClass::Ord => "mathord", + NoadClass::Op => "mathop", + NoadClass::Bin => "mathbin", + NoadClass::Rel => "mathrel", + NoadClass::Open => "mathopen", + NoadClass::Close => "mathclose", + NoadClass::Punct => "mathpunct", + NoadClass::Inner => "mathinner", + }); + } + match noad.limits { + OperatorLimits::Display => {} + OperatorLimits::AboveBelow => self.command("limits"), + OperatorLimits::Side => self.command("nolimits"), + } + self.math_field(&noad.nucleus, prefix, '.')?; + if let Some(sup) = &noad.sup { + self.math_field(sup, prefix, '^')?; + } + if let Some(sub) = &noad.sub { + self.math_field(sub, prefix, '_')?; + } + Ok(()) + } + + fn fraction( + &mut self, + specification: &FractionSpec, + numerator: &[MathNode], + leading: Option, + denominator: Option<&[MathNode]>, + prefix: &str, + ) -> Result<(), String> { + self.command("fraction"); + self.literal(", thickness "); + if let Some(thickness) = specification.thickness { + self.number(i64::from(thickness)); + } else { + self.literal("= default"); + } + for (name, delimiter) in [("left", specification.left), ("right", specification.right)] { + if delimiter != Delimiter::from_code(0) { + self.literal(&format!(", {name}-delimiter ")); + self.delimiter(delimiter); + } + } + if prefix.len() as i32 >= self.depth { + self.literal(" []"); + if denominator.is_some() { + self.literal(" []"); + } + } else { + self.math_sublist(numerator, leading, &format!("{prefix}\\"))?; + if let Some(denominator) = denominator { + self.math_sublist(denominator, None, &format!("{prefix}/"))?; + } + } + Ok(()) + } + + fn math_glue(&mut self, glue: Glue) { + self.command("glue"); + self.literal("("); + self.command("mskip"); + self.literal(") "); + self.number(i64::from(glue.width)); + self.literal("mu"); + for (label, flex) in [(" plus ", glue.stretch), (" minus ", glue.shrink)] { + if flex.amount != 0 { + self.literal(label); + self.flex(flex.amount, flex.order); + if flex.order == 0 { + self.literal("mu"); + } + } + } + } + + fn math_list( + &mut self, + nodes: &[MathNode], + leading: Option, + prefix: &str, + ) -> Result<(), String> { + if prefix.len() as i32 > self.depth { + if !nodes.is_empty() || leading.is_some() { + self.literal(" []"); + } + return Ok(()); + } + if let Some(delimiter) = leading { + self.line(prefix); + self.command("left"); + self.delimiter(delimiter); + } + for (i, node) in nodes.iter().enumerate() { + if self.overflow { + break; + } + if i + usize::from(leading.is_some()) >= self.breadth { + self.line(prefix); + self.literal("etc."); + break; + } + let ordinary = match node { + MathNode::ExplicitSpace(glue, name) => Some(DiagnosticNode::Glue { + value: *glue, + name: *name, + }), + MathNode::Leaders(leader) => Some(leader_node(leader)), + MathNode::Kern { + amount, + mu: false, + explicit, + } => Some(DiagnosticNode::Kern { + width: *amount, + explicit: *explicit, + }), + MathNode::Penalty(n) => Some(DiagnosticNode::Penalty(*n)), + MathNode::Rule(rule) => Some(DiagnosticNode::Rule(*rule)), + MathNode::Mark(mark) => Some(DiagnosticNode::Mark(mark.clone())), + MathNode::Adjustment(adjustment) => Some(DiagnosticNode::Adjustment { + pre: adjustment.pre, + nodes: vertical_nodes(&adjustment.nodes), + }), + MathNode::Insertion(insertion) => Some(insertion_view(insertion)), + MathNode::Output(output) => Some(DiagnosticNode::Output(output.clone())), + MathNode::Discretionary(node) => { + text_nodes(std::slice::from_ref(node.as_ref())).pop() + } + _ => None, + }; + if let Some(node) = ordinary { + self.nodes(&[node], prefix)?; + continue; + } + self.line(prefix); + match node { + MathNode::Noad(noad) => self.noad(noad, prefix)?, + MathNode::Delimiter { value, opening, .. } => { + self.command(if *opening { "left" } else { "right" }); + self.delimiter(*value); + } + MathNode::Style(style) => self.command(match style { + MathStyle::Display | MathStyle::DisplayCramped => "displaystyle", + MathStyle::Text | MathStyle::TextCramped => "textstyle", + MathStyle::Script | MathStyle::ScriptCramped => "scriptstyle", + MathStyle::ScriptScript | MathStyle::ScriptScriptCramped => "scriptscriptstyle", + }), + MathNode::Choice(branches) => { + self.command("mathchoice"); + for (label, branch) in ['D', 'T', 'S', 's'].into_iter().zip(branches.iter()) { + self.math_list(branch, None, &format!("{prefix}{label}"))?; + } + } + MathNode::Glue(glue) => self.math_glue(*glue), + MathNode::Kern { + amount, mu: true, .. + } => { + self.command("mkern"); + self.number(i64::from(*amount)); + self.literal("mu"); + } + MathNode::Fraction { + numerator, + denominator, + specification, + .. + } => { + self.fraction(specification, numerator, None, Some(denominator), prefix)?; + } + _ => unreachable!("ordinary math list item"), + } + } + Ok(()) + } +} + +pub(in crate::execution) fn show_math( + state: &State, + math: &super::super::math::MathBuilder, + breadth: i32, + depth: i32, + limit: usize, + wrap: bool, +) -> Result<(String, BTreeSet), String> { + let mut printer = Printer { + state, + out: String::new(), + column: 0, + tally: 0, + breadth: if breadth <= 0 { 5 } else { breadth as usize }, + depth, + limit, + overflow: false, + wrap, + reads: BTreeSet::new(), + }; + let leading = math.left.as_ref().map(|(left, _)| *left); + if let Some(pending) = &math.numerator { + printer.math_list(&math.nodes, None, "")?; + printer.line("this will begin denominator of:"); + if depth < 0 { + printer.literal(" []"); + } else { + printer.line(""); + printer.fraction( + &pending.specification, + &pending.numerator, + leading, + None, + "", + )?; + } + } else { + printer.math_list(&math.nodes, leading, "")?; + } + if printer.overflow { + return Err("native math list diagnostic output capacity exceeded".into()); + } + printer.raw('\n'); + Ok((printer.out, printer.reads)) +} diff --git a/crates/texrra-tex-core/src/execution/engine/alignment.rs b/crates/texrra-tex-core/src/execution/engine/alignment.rs index 3b87b93..512ad45 100644 --- a/crates/texrra-tex-core/src/execution/engine/alignment.rs +++ b/crates/texrra-tex-core/src/execution/engine/alignment.rs @@ -30,10 +30,10 @@ struct Cell { } #[derive(Clone)] pub(super) struct AlignmentFrame { - horizontal: bool, - span: Span, + pub(super) horizontal: bool, + pub(super) span: Span, spec: PackSpec, - depth: usize, + pub(super) depth: usize, stage: Stage, templates: Vec