From fb557c09c6ba006d1d9fc4a49609dd3d8a43c279 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sun, 29 Nov 2026 12:49:56 +0800 Subject: [PATCH] Apply pdfTeX line box dimensions and restore output penalties --- crates/texrra-tex-core/src/execution/boxes.rs | 4 +- .../texrra-tex-core/src/execution/engine.rs | 4 +- .../src/execution/engine/alignment.rs | 3 ++ .../src/execution/engine/boxes.rs | 9 ++++- .../execution/engine/diagnostics/activity.rs | 3 +- .../src/execution/engine/page.rs | 2 + .../src/execution/format_image.rs | 9 +++-- .../src/execution/page_builder.rs | 1 - .../src/execution/paragraph.rs | 16 +++++++- .../src/execution/parameters.rs | 37 ++++++++++++++++-- crates/texrra-tex-core/src/execution/state.rs | 8 ++++ .../texrra-tex-core/tests/execution_pages.rs | 39 +++++++++++++++++++ docs/P2_M1_PROGRESS.md | 30 ++++++++++++++ 13 files changed, 151 insertions(+), 14 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/boxes.rs b/crates/texrra-tex-core/src/execution/boxes.rs index 9d84c23..a12bc10 100644 --- a/crates/texrra-tex-core/src/execution/boxes.rs +++ b/crates/texrra-tex-core/src/execution/boxes.rs @@ -500,7 +500,7 @@ impl Default for VerticalBuilder { fn default() -> Self { Self { nodes: Vec::new(), - prev_depth: -1000 * 65536, + prev_depth: super::parameters::IGNORE_DEPTH, previous_lines: 0, tail_path: Vec::new(), } @@ -526,7 +526,7 @@ impl VerticalBuilder { pub fn append_box(&mut self, node: NativeVerticalNode, state: &State) -> Result<(), String> { let (_, height, depth) = node.dimensions().expect("box or line"); - if self.prev_depth > -1000 * 65536 { + if self.prev_depth > state.ignored_depth() { let mut label = Parameter::Baselineskip; let mut g = state.parameter(label).glue(); let gap = g.width as i64 - self.prev_depth as i64 - height; diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index 2d826d3..8522089 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -542,6 +542,8 @@ impl Executor { const XETEX_REVISION: &'static str = ".999998"; pub fn new(state: State, limits: Limits) -> Self { + let mut vertical = super::boxes::VerticalBuilder::default(); + vertical.prev_depth = state.ignored_depth(); Self { state, limits, @@ -594,7 +596,7 @@ impl Executor { pdf_force_pagebox_warned: false, input_position: None, paragraph: None, - vertical: super::boxes::VerticalBuilder::default(), + vertical, box_frames: Vec::new(), box_nodes_created: 0, material_charge: 0, diff --git a/crates/texrra-tex-core/src/execution/engine/alignment.rs b/crates/texrra-tex-core/src/execution/engine/alignment.rs index 1a9c104..bc6fce1 100644 --- a/crates/texrra-tex-core/src/execution/engine/alignment.rs +++ b/crates/texrra-tex-core/src/execution/engine/alignment.rs @@ -440,6 +440,7 @@ impl Executor { if horizontal { self.start_alignment_hlist(span); } else { + self.vertical.prev_depth = self.read_ignored_depth(); self.normal_paragraph(files)?; } Ok(()) @@ -608,6 +609,8 @@ impl Executor { .insert(Dependency::Parameter(Parameter::Lineskip)); self.reads .insert(Dependency::Parameter(Parameter::Lineskiplimit)); + self.reads + .insert(Dependency::Parameter(Parameter::Pdfignoreddimen)); let f = self.alignments.last_mut().unwrap(); self.vertical.append(&mut f.adjustments.pre); self.vertical diff --git a/crates/texrra-tex-core/src/execution/engine/boxes.rs b/crates/texrra-tex-core/src/execution/engine/boxes.rs index bf9af36..eeb5da3 100644 --- a/crates/texrra-tex-core/src/execution/engine/boxes.rs +++ b/crates/texrra-tex-core/src/execution/engine/boxes.rs @@ -717,6 +717,7 @@ impl Executor { b.restricted = true; self.paragraph = Some(b); } else { + self.vertical.prev_depth = self.read_ignored_depth(); self.normal_paragraph(files)?; } if group == GroupKind::Insertion { @@ -977,12 +978,18 @@ impl Executor { } Ok(()) } + pub(super) fn read_ignored_depth(&mut self) -> i32 { + self.reads.insert(Dependency::Parameter(Parameter::Pdfignoreddimen)); + self.state.ignored_depth() + } + pub(super) fn append_vertical_box(&mut self, node: NativeVerticalNode) -> Result<(), Error> { self.reads.insert(Dependency::VerticalState); for p in [ Parameter::Baselineskip, Parameter::Lineskip, Parameter::Lineskiplimit, + Parameter::Pdfignoreddimen, ] { self.reads.insert(Dependency::Parameter(p)); } @@ -1016,7 +1023,7 @@ impl Executor { return self .fail("outer running rule requires native page packaging; specify width"); } - self.vertical.prev_depth = -1000 * 65536; + self.vertical.prev_depth = self.read_ignored_depth(); NativeVerticalNode::Rule(r) } _ => unreachable!(), diff --git a/crates/texrra-tex-core/src/execution/engine/diagnostics/activity.rs b/crates/texrra-tex-core/src/execution/engine/diagnostics/activity.rs index d2a8405..f50eca4 100644 --- a/crates/texrra-tex-core/src/execution/engine/diagnostics/activity.rs +++ b/crates/texrra-tex-core/src/execution/engine/diagnostics/activity.rs @@ -153,6 +153,7 @@ impl Executor { let mut reads = BTreeSet::from([ Dependency::ParagraphMode, Dependency::VerticalState, + Dependency::Parameter(Parameter::Pdfignoreddimen), Dependency::PageState, Dependency::FontResources, Dependency::EscapeChar, @@ -323,7 +324,7 @@ impl Executor { render(&mut text, &mut reads, &vertical_nodes(&vertical.nodes))?; text.push_str(auxiliary_break); text.push_str("prevdepth "); - if vertical.prev_depth <= -1000 * 65536 { + if vertical.prev_depth <= self.state.ignored_depth() { text.push_str("ignored"); } else { text.push_str(®isters::print_scaled(vertical.prev_depth)); diff --git a/crates/texrra-tex-core/src/execution/engine/page.rs b/crates/texrra-tex-core/src/execution/engine/page.rs index c994392..626db2f 100644 --- a/crates/texrra-tex-core/src/execution/engine/page.rs +++ b/crates/texrra-tex-core/src/execution/engine/page.rs @@ -240,6 +240,7 @@ impl Executor { math: std::mem::take(&mut self.math), display: self.display.take(), }); + self.vertical.prev_depth = self.read_ignored_depth(); self.inputs.push(InputFrame::Tokens { tokens: InputTokens::Many(tokens.iter().cloned().map(InputToken::from).collect()), cursor: 0, @@ -303,6 +304,7 @@ impl Executor { self.display = frame.display; let page = self.native_page.as_mut().unwrap(); page.output_active = false; + page.insertions.penalties = 0; page.resume_contributions(nodes); page.requested = true; Ok(true) diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index 8b978ff..8bdc875 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -16,9 +16,9 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 111; -const COMPATIBLE_SCHEMAS: [u32; 77] = [ - 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, 109, 110, SCHEMA, +const SCHEMA: u32 = 112; +const COMPATIBLE_SCHEMAS: [u32; 78] = [ + 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, 109, 110, 111, SCHEMA, ]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); @@ -247,6 +247,9 @@ impl FormatImage { if image.schema < 111 { image.state.install_pdf_font_tag_commands(); } + if image.schema < 112 { + image.state.restore_legacy_pdf_parameters(&Parameter::PDF_LINE_DIMENSIONS); + } let pdf_objects = image.state.pdf_objects.as_deref(); let mut numbered_images = 0; for (index, saved) in image.pdf_images.iter().enumerate() { diff --git a/crates/texrra-tex-core/src/execution/page_builder.rs b/crates/texrra-tex-core/src/execution/page_builder.rs index f0dd771..14e38a3 100644 --- a/crates/texrra-tex-core/src/execution/page_builder.rs +++ b/crates/texrra-tex-core/src/execution/page_builder.rs @@ -154,7 +154,6 @@ impl NativePageState { self.max_depth = state.parameter(Parameter::Maxdepth).width(); self.least = AWFUL; self.best = None; - self.insertions.penalties = 0; if state.parameter(Parameter::Tracingpages).width() > 0 { observations.push(PageObservation::Trace(PageTrace::Freeze { goal: self.dimensions[0], diff --git a/crates/texrra-tex-core/src/execution/paragraph.rs b/crates/texrra-tex-core/src/execution/paragraph.rs index da418d7..9f2cb38 100644 --- a/crates/texrra-tex-core/src/execution/paragraph.rs +++ b/crates/texrra-tex-core/src/execution/paragraph.rs @@ -1535,6 +1535,16 @@ pub(super) fn finish( let directions = super::direction::has_directions(&b.nodes); let mut direction_stack = Vec::new(); let widow_kind = LinePenaltyKind::for_widow_parameter(final_widow); + let ignored_depth = state.ignored_depth(); + let pdftex = state.input_profile() != super::InputProfile::XeTeX; + let line_dimension = |p| { + let value = parameters[&p].width(); + (pdftex && value != ignored_depth).then_some(i64::from(value)) + }; + let each_height = line_dimension(Parameter::Pdfeachlineheight); + let each_depth = line_dimension(Parameter::Pdfeachlinedepth); + let first_height = line_dimension(Parameter::Pdffirstlineheight).or(each_height); + let last_depth = line_dimension(Parameter::Pdflastlinedepth).or(each_depth); for (i, (point, bad)) in path.iter().enumerate() { let mut material = line_material( &b.nodes, @@ -1558,7 +1568,7 @@ pub(super) fn finish( // Keep a real breakpoint marker before the synthetic line closers. super::direction::line(&mut material, &mut direction_stack); } - let (line, line_box, adjustments, shrinking) = pack( + let (line, mut line_box, adjustments, shrinking) = pack( &material, ¶meters, dimensions, @@ -1583,6 +1593,10 @@ pub(super) fn finish( pre_display_size = pre_display_line_size(material, line_box.setting, line_box.shift, quad); } + let height = if i == 0 { first_height } else { each_height }; + let depth = if i + 1 == path.len() { last_depth } else { each_depth }; + line_box.height = height.unwrap_or(line_box.height); + line_box.depth = depth.unwrap_or(line_box.depth); diagnostic_lines.push(super::diagnostics::box_view(&line_box)); line_boxes.push(line_box); line_pre_adjustments.push(adjustments.pre); diff --git a/crates/texrra-tex-core/src/execution/parameters.rs b/crates/texrra-tex-core/src/execution/parameters.rs index 6ec4d89..4e903f1 100644 --- a/crates/texrra-tex-core/src/execution/parameters.rs +++ b/crates/texrra-tex-core/src/execution/parameters.rs @@ -1,6 +1,8 @@ //! Owned paragraph parameter meanings and TeX INITEX defaults. use super::{Flex, Glue, RegisterKind, RegisterValue}; +pub(super) const IGNORE_DEPTH: i32 = -1000 * 65536; + #[derive( Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize, )] @@ -148,6 +150,11 @@ pub enum Parameter { Ignoreprimitiveerror, Lastlinefit, Savinghyphcodes, + Pdfignoreddimen, + Pdfeachlineheight, + Pdfeachlinedepth, + Pdffirstlineheight, + Pdflastlinedepth, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum ParameterValue { @@ -155,6 +162,13 @@ pub enum ParameterValue { Quantity(RegisterValue), } impl Parameter { + pub(super) const PDF_LINE_DIMENSIONS: [Self; 5] = [ + Self::Pdfignoreddimen, + Self::Pdfeachlineheight, + Self::Pdfeachlinedepth, + Self::Pdffirstlineheight, + Self::Pdflastlinedepth, + ]; pub(super) fn pdftex_only(self) -> bool { matches!( self, @@ -191,6 +205,11 @@ impl Parameter { | Self::Pdfprependkern | Self::Pdfappendkern | Self::Pdfadjustspacing + | Self::Pdfignoreddimen + | Self::Pdfeachlineheight + | Self::Pdfeachlinedepth + | Self::Pdffirstlineheight + | Self::Pdflastlinedepth ) } @@ -352,6 +371,11 @@ impl Parameter { Self::XeTeXinputnormalization, Self::Lastlinefit, Self::Savinghyphcodes, + Self::Pdfignoreddimen, + Self::Pdfeachlineheight, + Self::Pdfeachlinedepth, + Self::Pdffirstlineheight, + Self::Pdflastlinedepth, ]; pub fn name(self) -> String { match self { @@ -381,6 +405,11 @@ impl Parameter { | Self::Pdflinkmargin | Self::Pdfdestmargin | Self::Pdfthreadmargin + | Self::Pdfignoreddimen + | Self::Pdfeachlineheight + | Self::Pdfeachlinedepth + | Self::Pdffirstlineheight + | Self::Pdflastlinedepth | Self::Delimitershortfall | Self::Predisplaysize | Self::Displaywidth @@ -422,10 +451,10 @@ impl Parameter { pub fn initial(self) -> ParameterValue { if let Some(kind) = self.kind() { ParameterValue::Quantity(if kind == RegisterKind::Dimen { - RegisterValue::Dimension(if matches!(self, Self::Pdfhorigin | Self::Pdfvorigin) { - 4_736_287 - } else { - 0 + RegisterValue::Dimension(match self { + Self::Pdfhorigin | Self::Pdfvorigin => 4_736_287, + p if Self::PDF_LINE_DIMENSIONS.contains(&p) => IGNORE_DEPTH, + _ => 0, }) } else { RegisterValue::Glue(Glue::default()) diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index c2f7cc4..d3fa44d 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -1645,6 +1645,14 @@ impl State { self.assign_parameter(Parameter::Pdfoutput, ParameterValue::Integer(1), true); self } + pub(super) fn ignored_depth(&self) -> i32 { + if self.input_profile() == super::InputProfile::XeTeX { + super::parameters::IGNORE_DEPTH + } else { + self.parameter(Parameter::Pdfignoreddimen).width() + } + } + pub fn parameter(&self, parameter: Parameter) -> ParameterValue { self.parameters .get(¶meter) diff --git a/crates/texrra-tex-core/tests/execution_pages.rs b/crates/texrra-tex-core/tests/execution_pages.rs index a824e53..e180580 100644 --- a/crates/texrra-tex-core/tests/execution_pages.rs +++ b/crates/texrra-tex-core/tests/execution_pages.rs @@ -332,6 +332,45 @@ fn output_groups_can_wrap_drop_and_reinsert_real_box255_material() { } } +#[test] +fn insertion_penalties_survive_page_freeze_and_clear_before_aftergroup() { + let output = r"\showbox255\shipout\box255\insertpenalties=19\aftergroup\AfterOutput"; + let body = format!( + r"\def\AfterOutput{{{}}} +\count32=1000 \dimen32=20pt \skip32=0pt \insertpenalties=7 +\hbox{{A}}{} +\insert32{{\hbox{{Note}}}}\penalty-10000 +\hbox{{B}}\penalty-10000", + observe(r"return=\the\insertpenalties"), + observe(r"frozen=\the\insertpenalties"), + ); + compare(&body, output); +} + +#[test] +fn pdftex_line_box_dimensions_drive_vertical_glue_and_page_geometry() { + let body = format!( + r"\vsize=55pt\hsize=45pt\baselineskip=14pt\lineskiplimit=0pt\lineskip=2pt +\pdfignoreddimen=-21pt +\pdfeachlineheight=8pt\pdfeachlinedepth=2pt +\pdffirstlineheight=13pt\pdflastlinedepth=6pt +\setbox0=\vbox{{{} + \noindent First\hfil\penalty-10000 Middle\hfil\penalty-10000 Last\par +}} +\unvbox0\penalty-10000 +\pdffirstlineheight=\pdfignoreddimen\pdflastlinedepth=\pdfignoreddimen +\hrule height1pt {} +\noindent Additional line\par +\penalty-10000", + observe(r"nested=\the\prevdepth"), + observe(r"after-rule=\the\prevdepth"), + ); + compare( + &body, + &format!(r"{}\showbox255\shipout\box255", observe(r"output=\the\prevdepth")), + ); +} + #[test] fn actual_page_marks_carry_by_output_attempt_and_remain_separate_from_split_marks() { let mut jobs = 0; diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index c3739a2..89e573f 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,36 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-11-29 行盒尺寸、分页返回与本地 Beta 验证 + +pdfTeX 的每行、首行和末行高度深度参数进入实际段落行盒。首行高度 +和末行深度优先于一般值。当前 \pdfignoreddimen 决定何时插入基线胶, +并用于竖盒、对齐单元、竖向标尺和输出例程的 prevdepth 语义。 +XeTeX 沿用自己的固定阈值。schema112 实际保存参数,旧格式继续加载, +已经存在的同名绑定保留。用户输出例程返回时清零 \insertpenalties, +页面首次冻结尺寸时保留该整数。原版 INITEX 见证在返回钩子中显示 +0,原生旧版显示 19,候选的两页文字与原版一致。 +[pdfTeX 行盒尺寸规则](https://github.com/TeX-Live/texlive-source/blob/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/pdftexdir/pdftex.web#L25927-L25955) +· [输出例程返回](https://github.com/TeX-Live/texlive-source/blob/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/pdftexdir/pdftex.web#L28570-L28591) + +七项定向检查通过。双栏浮动、跨页长表、启用五个行距参数的双栏 +浮动三个小项目使用实际 LaTeX 类和包,从空辅助文件各编译两轮。 +两端共七页、1,276 个词框、144 DPI 灰度图和各轮 aux 均一致。 +保留旧版在新样例首个参数处的真实编译失败。 + +本地 Beta 路线以 [产品总目标](NATIVE_PRODUCT_GOAL.md#beta-优先交付) +为准。本轮用同一发布构建启动原样 A4 多文件样例。Edge 实际绘制 +首轮页面,共同修改主文件和子文件后更新这一页,编辑后来源表 +包含当前子文件的实际版本。输入未定义命令时保留上次可下载 PDF, +恢复后导出与此前字节相同。独立 CLI 从空辅助文件编译两轮,收敛 +后的 PDF 和 aux 均与编辑器逐字节一致。第一次拷入现成 aux 被 +输出所有权检查拒绝,保留该失败。浏览器本次首屏为 4.56 秒, +两文件改动到页面可见为 0.96 秒,均为单次记录。Beta 还需整理 +可重复的启动资源并在同一候选上走完首批原样项目。 + +证据在 target/p3-float-page-lists/。SHA256SUMS 指纹为 +7ca1f8577db0f738144d36f5ac40d8a41c27a9b8e167e525696ed9e2aaba3d70。 + ## 2026-11-29 子文件检查点的所有权与分配 子文件推进后的执行器直接接回运行中的作业,在实际落点由统一调度保留