From aa780a800b2bdc7bac4bc5cdaa14b21f15a742b1 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 28 Nov 2026 02:08:31 +0800 Subject: [PATCH] Reload letterspaced font metrics independently of generated packets --- .../src/execution/font_catalog.rs | 8 ++ .../src/execution/format_image.rs | 9 +- crates/texrra-tex-core/src/execution/state.rs | 11 +++ crates/texrra-tex-core/src/native_font.rs | 8 ++ docs/P2_M1_PROGRESS.md | 29 ++++++ tests/native_pipeline.rs | 92 +++++++++++++++++++ 6 files changed, 154 insertions(+), 3 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/font_catalog.rs b/crates/texrra-tex-core/src/execution/font_catalog.rs index e52cb9b..8954f5d 100644 --- a/crates/texrra-tex-core/src/execution/font_catalog.rs +++ b/crates/texrra-tex-core/src/execution/font_catalog.rs @@ -1046,6 +1046,14 @@ impl FontCatalog { relocated } + pub(super) fn discard_letterspace_programs(&mut self) -> bool { + let mut discarded = false; + for font in self.fonts.iter_mut().filter_map(|font| font.glyph_font.as_mut()) { + discarded |= NativeFont::discard_letterspace_program(font); + } + discarded + } + pub(super) fn reset_format_font_state(&mut self) { for font in &mut self.fonts { font.expansion_origin = None; diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index 46dd7c2..19895d9 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 = 94; -const COMPATIBLE_SCHEMAS: [u32; 60] = [ - 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, SCHEMA, +const SCHEMA: u32 = 95; +const COMPATIBLE_SCHEMAS: [u32; 61] = [ + 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, SCHEMA, ]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); @@ -253,6 +253,9 @@ impl FormatImage { { return Err("invalid native format PDF Form registry".into()); } + if image.schema < 95 { + image.state.discard_format_letterspace_programs(); + } image.schema = SCHEMA; Ok(image) } diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index ec00adb..66b0bab 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -1683,6 +1683,7 @@ impl State { self.pdf_info.clear(); self.pdf_pages_attributes.clear(); self.fonts.reset_format_font_state(); + self.discard_format_letterspace_programs(); self } pub(super) fn rebind_format_images( @@ -1730,6 +1731,16 @@ impl State { } } + pub(super) fn discard_format_letterspace_programs(&mut self) { + // The format retains the widened TeX metrics. The generated virtual + // packets belong to the job that defined the letter-spaced font. + if self.fonts.discard_letterspace_programs() { + self.visit_format_fonts_mut(|font| { + crate::native_font::NativeFont::discard_letterspace_program(font); + }); + } + } + pub(super) fn rebind_format_font_resources( &mut self, bindings: &BTreeMap, diff --git a/crates/texrra-tex-core/src/native_font.rs b/crates/texrra-tex-core/src/native_font.rs index 16e07b3..c872bb8 100644 --- a/crates/texrra-tex-core/src/native_font.rs +++ b/crates/texrra-tex-core/src/native_font.rs @@ -114,6 +114,14 @@ pub(crate) fn unexpanded(amount: &i32) -> bool { } impl NativeFont { + pub(crate) fn discard_letterspace_program(font: &mut Arc) -> bool { + if font.letter_spacing.is_none() { + return false; + } + Arc::make_mut(font).letter_spacing = None; + true + } + pub(crate) fn rebind_resource_path( font: &mut Arc, bindings: &BTreeMap, diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 36ed4fe..5b86645 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -819,6 +819,35 @@ letterspace-fresh.log 和 device-pdftex-spaced-moved.log。 [原版字距虚拟字体构造](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L16400-L16492) [原版格式所写字体字段](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L30627-L30696) +### pdfTeX 字距字体的格式重载 + +schema95 保存派生字体名、字号和加宽后的 TFM 度量,并清除定义作业 +生成的虚拟字形程序。字体目录、已装盒字形及待写出的 Form 一起更新。 +旧格式解码时执行同样的清理。重载作业按派生字体名读取本次的 VF、 +映射及实际字形程序。同一作业中定义和绘制的字距字体仍使用其生成程序。 +[原版字距程序](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L16400-L16492) +[原版格式字体字段](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L30627-L30696) + +八项格式定向回归通过。以真实 cmr10.tfm 与 CMR10 字体程序对照, +新格式移目录后读取映射或磁盘 VF,实际 schema94 重新加载,以及 +同作业直接绘制,四组共八页的 144 DPI 灰度和原始提取文字与原版一致。 +映射用例保留 21.8056 pt 盒宽及待写出的 Form。重载后的两页相对 +同作业绘制分别有 187 和 259 个像素不同,与原版一致。VF 查找记录了 +真实文件版本。映射和 VF 都缺失时两端均报告 cmr10+80ls 缺失。 +pdffonts 记录原版 Builtin 与原生 Custom 的字典表示差异。 + +三页会话用实际 schema94 验证移除映射、加入 VF 恢复、修改第二页 +子文件和恢复映射。缺字体时保留已发布预览和 aux。其余三次只更新 +第 2 页,PDF、页面、完整 SVG、来源映射及 aux 与独立全量一致。 +正文修改复用 15 步,两次字体资源变更重新执行。 +证据在 target/p3-format-letterspace,receipt.json 的 SHA-256 为 +`7087193561e6f736eb0e8d03d239f144ae061268a35e09c1dc71cf38ed73ee02`。 + +下一字体入口复用带真实 TeXBase1 编码的映射,接通 pdfTeX 对 +TrueType 字体的子集嵌入。target/p3-format-letterspace/next-pdftex-truetype +保存 ptmr8r.tfm、8r.enc、LiberationSerif-Regular.ttf、原版 PDF、 +词框、灰度图及当前发布版的字体程序格式错误。 + ### XeTeX 纸张指令进入实际发页 XeTeX 每页隐式纸张设置与文档内的 `pdf:pagesize` 按发页顺序生效。 diff --git a/tests/native_pipeline.rs b/tests/native_pipeline.rs index e0e3fab..79aea05 100644 --- a/tests/native_pipeline.rs +++ b/tests/native_pipeline.rs @@ -1888,6 +1888,98 @@ fn relocated_format_binds_packed_and_new_glyphs_to_the_current_tfm() { fs::remove_dir_all(root).unwrap(); } +#[test] +fn loaded_format_uses_current_letterspace_font_programs() { + use texrra_tex_core::render::DisplayItem; + + let root = directory("format-letterspace").canonicalize().unwrap(); + let fonts = root.join("fonts"); + fs::create_dir(&fonts).unwrap(); + let map = fonts.join("pdftex.map"); + let outline = fonts.join("cmr10.pfb"); + fs::write(&outline, include_bytes!("../assets/fonts/cmr10.pfb")).unwrap(); + let seed = br"\catcode123=1 \catcode125=2 +\pdfoutput=1 \font\face=cmr10 +\letterspacefont\spaced=\face 80 +\setbox0=\hbox{\spaced AV} +\setbox1=\hbox{\spaced XYZ} +\pdfxform 1 +\edef\pendingform{\the\pdflastxform} +\dump +"; + fs::write(root.join("seed.tex"), seed).unwrap(); + let mut job = request(&root); + for kind in [ + ResourceKind::Map, + ResourceKind::Type1, + ResourceKind::Vf, + ResourceKind::Pk, + ] { + job.world + .paths + .insert(kind, vec![SearchPath::directory(&fonts, false)]); + } + let mut dump = job.clone(); + dump.input = "seed.tex".into(); + dump.output = "seed.tfx".into(); + dump_format(dump, Cancellation::default()).unwrap(); + job.entry = Entry::Format(root.join("seed.tfx")); + fs::write( + root.join("main.tex"), + br"\setbox2=\hbox{\spaced AV} +\ifdim\wd0=15.4889pt\else\errmessage{saved width changed}\fi +\ifdim\wd2=\wd0\else\errmessage{new width differs}\fi +\shipout\hbox{\copy0 \box2 \face ABC} +\shipout\hbox{\pdfrefxform\pendingform} +\end +", + ) + .unwrap(); + fs::write(&map, b"cmr10 CMR10 = first.pages[0] + .items + .iter() + .filter_map(|item| match item { + DisplayItem::NativeGlyph { font, slot, .. } => { + assert!(font.letter_spacing.is_none()); + Some((font.name.as_str(), *slot)) + } + _ => None, + }) + .collect(); + assert_eq!( + glyphs, + [ + ("cmr10+80ls", b'A'), + ("cmr10+80ls", b'V'), + ("cmr10+80ls", b'A'), + ("cmr10+80ls", b'V'), + ("cmr10", b'A'), + ("cmr10", b'B'), + ("cmr10", b'C'), + ] + ); + for path in [&map, &outline] { + assert!( + first.resources.trace.reads.contains(&file_version(path).unwrap().unwrap()) + ); + } + + fs::write(&map, b"cmr10 CMR10