diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index 8573c97..5793268 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -896,6 +896,22 @@ impl Executor { let number = self.pdf_page_reference(files)?; self.print_tokens(&number.to_string(), token)?; } + Leftmarginkern | Rightmarginkern => { + let register = self.register_index(files)?; + self.reads.insert(Dependency::BoxRegister(register)); + let Some(NativeBox { content: BoxContent::Horizontal(nodes), .. }) = + self.state.box_register(register) + else { + return self.fail("margin kern requires a non-void horizontal box"); + }; + let width = paragraph::stored_margin_kern(nodes, primitive == Rightmarginkern); + let value = if width == 0 { + "0pt".to_owned() + } else { + format!("{}pt", registers::print_scaled(width)) + }; + self.print_tokens(&value, token)?; + } Pdfcolorstackinit => { let stack = self.init_pdf_color_stack(files)?; self.print_tokens(&stack.to_string(), token)?; diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index a957864..7259c4c 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -8,8 +8,8 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 38; -const COMPATIBLE_SCHEMAS: [u32; 10] = [27, 28, 31, 32, 33, 34, 35, 36, 37, SCHEMA]; +const SCHEMA: u32 = 39; +const COMPATIBLE_SCHEMAS: [u32; 11] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, SCHEMA]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); #[derive(serde::Serialize, serde::Deserialize)] diff --git a/crates/texrra-tex-core/src/execution/paragraph.rs b/crates/texrra-tex-core/src/execution/paragraph.rs index 0dd3803..b3c167a 100644 --- a/crates/texrra-tex-core/src/execution/paragraph.rs +++ b/crates/texrra-tex-core/src/execution/paragraph.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; mod break_cache; mod expansion; mod protrusion; +pub(super) use protrusion::stored_margin_kern; use break_cache::BreakNode; pub use break_cache::{LineBreakCache, LineBreakWork}; diff --git a/crates/texrra-tex-core/src/execution/paragraph/protrusion.rs b/crates/texrra-tex-core/src/execution/paragraph/protrusion.rs index 0c431ed..0dfa25b 100644 --- a/crates/texrra-tex-core/src/execution/paragraph/protrusion.rs +++ b/crates/texrra-tex-core/src/execution/paragraph/protrusion.rs @@ -43,32 +43,7 @@ impl Edge { }) if boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 => Self::Skip, _ => Self::Solid, }, - TextNode::Language(_) - | TextNode::Mark(_) - | TextNode::Adjustment(_) - | TextNode::Insertion(_) - | TextNode::Penalty(_) - | TextNode::Indent(0) - | TextNode::AccentKern(0) - | TextNode::MathBoundary { width: 0, .. } - | TextNode::Kern { - explicit: false, .. - } - | TextNode::Kern { width: 0, .. } => Self::Skip, - TextNode::Output(node) if node.dimensions().is_none() => Self::Skip, - TextNode::Glue(glue) - | TextNode::NamedGlue(glue, _) - | TextNode::Leaders(LeaderGlue { glue, .. }) - if glue.canonical_zero => - { - Self::Skip - } - TextNode::Discretionary { - pre_break, - post_break, - replacement_count: 0, - .. - } if pre_break.is_empty() && post_break.is_empty() => Self::Skip, + _ if skippable(node) => Self::Skip, _ => Self::character(node), } } @@ -107,6 +82,58 @@ impl Edge { } } +fn skippable(node: &TextNode) -> bool { + match node { + TextNode::Box(boxed) => matches!( + boxed.native_box.as_deref(), + Some(NativeBox { content: BoxContent::Horizontal(nodes), .. }) + if nodes.is_empty() && boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 + ), + TextNode::Language(_) + | TextNode::Mark(_) + | TextNode::Adjustment(_) + | TextNode::Insertion(_) + | TextNode::Penalty(_) + | TextNode::Indent(0) + | TextNode::AccentKern(0) + | TextNode::MathBoundary { width: 0, .. } + | TextNode::Kern { explicit: false, .. } + | TextNode::Kern { width: 0, .. } => true, + TextNode::Output(node) => node.dimensions().is_none(), + TextNode::Glue(glue) + | TextNode::NamedGlue(glue, _) + | TextNode::Leaders(LeaderGlue { glue, .. }) => glue.canonical_zero, + TextNode::Discretionary { + pre_break, + post_break, + replacement_count: 0, + .. + } => pre_break.is_empty() && post_break.is_empty(), + _ => false, + } +} + +pub(in crate::execution) fn stored_margin_kern(nodes: &[TextNode], right: bool) -> i32 { + let side_skip = if right { Parameter::Rightskip } else { Parameter::Leftskip }; + let non_skippable = |node: &&TextNode| { + !matches!(node, TextNode::NamedGlue(_, parameter) if *parameter == side_skip) + && !skippable(node) + }; + // The query walks only the stored hlist. It does not descend into a nested + // box or recalculate protrusion using the current font codes. + let edge = if right { + nodes.iter().rev().find(non_skippable) + } else { + nodes.iter().find(non_skippable) + }; + match edge { + Some(TextNode::MarginKern { width, right: actual_side, .. }) if *actual_side == right => { + *width + } + _ => 0, + } +} + pub(super) fn margin_kern( state: &State, nodes: &[TextNode], diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index 3e99fa4..66b0ff4 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -329,6 +329,8 @@ pub enum Primitive { Pdfpageref, Partokenname, Pdfliteral, + Leftmarginkern, + Rightmarginkern, } impl Primitive { @@ -384,6 +386,8 @@ impl Primitive { | Pdfuniformdeviate | Pdfnormaldeviate | Pdfpageref + | Leftmarginkern + | Rightmarginkern | Pdfcolorstackinit | Csname | Unless @@ -972,6 +976,8 @@ impl Default for State { ("pdfpageref", Pdfpageref), ("partokenname", Partokenname), ("pdfliteral", Pdfliteral), + ("leftmarginkern", Leftmarginkern), + ("rightmarginkern", Rightmarginkern), ("pdfcolorstack", Pdfcolorstack), ("pdfsave", Pdfsave), ("pdfstartlink", Pdfstartlink), diff --git a/crates/texrra-tex-core/tests/execution_fractions.rs b/crates/texrra-tex-core/tests/execution_fractions.rs index d361e0f..f875e7c 100644 --- a/crates/texrra-tex-core/tests/execution_fractions.rs +++ b/crates/texrra-tex-core/tests/execution_fractions.rs @@ -246,7 +246,7 @@ fn genuine_fraction_primitives_and_retained_boxes_survive_format_roundtrip() { let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); let payload: serde_json::Value = serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); - assert_eq!(payload["schema"], 38); + assert_eq!(payload["schema"], 39); let state = State::initex().with_etex(); let loaded = FormatImage::decode(&bytes) .unwrap() diff --git a/crates/texrra-tex-core/tests/execution_pdf_unicode.rs b/crates/texrra-tex-core/tests/execution_pdf_unicode.rs index 891bc56..50f55cb 100644 --- a/crates/texrra-tex-core/tests/execution_pdf_unicode.rs +++ b/crates/texrra-tex-core/tests/execution_pdf_unicode.rs @@ -334,7 +334,7 @@ fn old_schema_and_missing_unicode_state_are_rejected_even_with_valid_checksums() let envelope: serde_json::Value = serde_json::from_slice(&encoded).unwrap(); let original: serde_json::Value = serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); - assert_eq!(original["schema"], 38); + assert_eq!(original["schema"], 39); for (schema, expected) in [ (1, "incompatible native format"), (2, "incompatible native format"), diff --git a/crates/texrra-tex-core/tests/execution_vcenter.rs b/crates/texrra-tex-core/tests/execution_vcenter.rs index 881f746..e941350 100644 --- a/crates/texrra-tex-core/tests/execution_vcenter.rs +++ b/crates/texrra-tex-core/tests/execution_vcenter.rs @@ -164,7 +164,7 @@ fn new_format_owns_vcenter_and_boxed_math_without_reinjecting_old_meanings() { let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); let payload: serde_json::Value = serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); - assert_eq!(payload["schema"], 38); + assert_eq!(payload["schema"], 39); let executor = FormatImage::decode(&bytes) .unwrap() .new_job( diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 0400a90..aef447b 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -1126,6 +1126,29 @@ schema28 更新。schema 1至26和29仍拒绝,27与28缺少必需 Unicode [原样模板记录](PUBLISHER_TEMPLATE_MATRIX.md#aps-revtex-原样双栏样稿) [pdfTeX 的重音布局](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L28084-L28143) +### 实际横盒的左右边缘 kern + +`\leftmarginkern` 和 `\rightmarginkern` 从指定横盒自己的列表读出带符号 +的边缘节点,跳过同侧 skip 和 pdfTeX 边缘扫描允许跳过的节点。查询只读, +保持盒可继续复制和发页。嵌套横盒有独立边缘,查询不会下降到内盒。 +空横盒返回 `0pt`,空寄存器或垂直盒触发诊断。schema39 登记两个原语。 +[pdfTeX 的边缘 kern 查询](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L11371-L11399) + +同一未改动输入有5条查询记录,包含非零左右 skip、−0.75 pt 和 +−1.25 pt 的实际悬挂、嵌套及空盒、改变字体代码后查询原盒,以及后续 +尺寸赋值,均与 pdfTeX 逐字节一致。用查询结果绘制的一页144 DPI 灰度图 +逐像素相同。垂直盒被两端拒绝。 + +编辑第二页从右边缘改读左边缘时,复用946步并保持已经装好的横盒, +只替换第二页。PDF、SVG 和来源映射与独立零复用一致。来源映射只 +归一化测试目录及会话修订号。 + +`target/p3-margin-kern-query-native` 的回执: + +- `receipt.json` 为 `9751ac64739fa0cb19b227128eb9710f7ddf3dea19e2fb8212304e3da276037d`。 +- `checkpoint/receipt.json` 为 `1462c599a6c1a9e658d52b16c6ed29dbd5652e6467e471e1f86b45c98def3399`。 +- 已有悬挂与发页对照通过,`protrusion-regression.log` 为 `2d690085cf12141a0d5aadf771ce5efbc3911417355faaaa4b9e7599118ceb74`。 + ### 原生 PDF 字面与实际设备原点 `\pdfliteral` 保留实际列表节点和八位 PDF 字节。普通节点在扫描时展开,