From 1fe0ac9dd5b9f502add05e492df8beb70c6bb70d Mon Sep 17 00:00:00 2001 From: GloamXun Date: Thu, 26 Nov 2026 17:03:52 +0800 Subject: [PATCH] Write per-page pdfTeX ProcSet from shipped resources --- .../src/execution/engine/device.rs | 8 +++- .../src/execution/format_image.rs | 14 ++++-- .../src/execution/parameters.rs | 3 ++ crates/texrra-tex-core/src/execution/state.rs | 8 ++-- crates/texrra-tex-core/src/pdf.rs | 43 +++++++++++++++++-- crates/texrra-tex-core/src/pdf_device.rs | 13 ++++++ crates/texrra-tex-core/src/pdf_image.rs | 24 +++++++++++ crates/texrra-tex-core/src/pdf_image/png.rs | 18 +++++++- docs/P2_M1_PROGRESS.md | 38 ++++++++++++++++ docs/PUBLISHER_TEMPLATE_MATRIX.md | 8 ++++ 10 files changed, 162 insertions(+), 15 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/engine/device.rs b/crates/texrra-tex-core/src/execution/engine/device.rs index f936e65..077c6be 100644 --- a/crates/texrra-tex-core/src/execution/engine/device.rs +++ b/crates/texrra-tex-core/src/execution/engine/device.rs @@ -298,13 +298,17 @@ impl Executor { InputProfile::UnicodeScalar | InputProfile::XeTeX => PdfDriver::Xdv, }; let mut file = self.state.pdf_file_options.clone().unwrap(); - if driver == PdfDriver::Xdv { + let omit_procset = if driver == PdfDriver::PdfTeX { + self.read_parameter(Parameter::Pdfomitprocset).width() + } else { self.state.xdv_setup.apply_version(&mut file); - } + 0 + }; Ok(Some(PdfPageData { driver, file, stream_compression: level.max(0) as u8, + omit_procset, })) } } diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index f82aaee..1882328 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -8,9 +8,9 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 79; -const COMPATIBLE_SCHEMAS: [u32; 46] = [ - 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, SCHEMA, +const SCHEMA: u32 = 80; +const COMPATIBLE_SCHEMAS: [u32; 47] = [ + 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, SCHEMA, ]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); @@ -137,7 +137,13 @@ impl FormatImage { image.state.restore_legacy_mathcodes()?; } if image.schema < 79 { - image.state.restore_legacy_pdf_omission_parameters(); + image.state.restore_legacy_pdf_parameters(&[ + Parameter::Pdfomitcharset, + Parameter::Pdfomitinfodict, + ]); + } + if image.schema < 80 { + image.state.restore_legacy_pdf_parameters(&[Parameter::Pdfomitprocset]); } image.schema = SCHEMA; Ok(image) diff --git a/crates/texrra-tex-core/src/execution/parameters.rs b/crates/texrra-tex-core/src/execution/parameters.rs index a883c27..11a4444 100644 --- a/crates/texrra-tex-core/src/execution/parameters.rs +++ b/crates/texrra-tex-core/src/execution/parameters.rs @@ -19,6 +19,7 @@ pub enum Parameter { Pdfinfoomitdate, Pdfomitcharset, Pdfomitinfodict, + Pdfomitprocset, Tracingcommands, Tracingrestores, Tracingassigns, @@ -158,6 +159,7 @@ impl Parameter { | Self::Pdfinfoomitdate | Self::Pdfomitcharset | Self::Pdfomitinfodict + | Self::Pdfomitprocset | Self::Pdfhorigin | Self::Pdfvorigin | Self::Pdflinkmargin @@ -198,6 +200,7 @@ impl Parameter { Self::Pdfinfoomitdate, Self::Pdfomitcharset, Self::Pdfomitinfodict, + Self::Pdfomitprocset, Self::Tracingcommands, Self::Tracingrestores, Self::Tracingassigns, diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index 080c2ab..39c1b43 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -1676,12 +1676,10 @@ impl State { } } - pub(super) fn restore_legacy_pdf_omission_parameters(&mut self) { + pub(super) fn restore_legacy_pdf_parameters(&mut self, parameters: &[Parameter]) { if self.input_profile != super::InputProfile::XeTeX { - for parameter in [Parameter::Pdfomitcharset, Parameter::Pdfomitinfodict] { - self.commands - .entry(CommandName::Control(parameter.name())) - .or_insert(Meaning::Parameter(parameter)); + for ¶meter in parameters { + self.update_profile_command(¶meter.name(), Meaning::Parameter(parameter), true); } } } diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 137c86d..b573d2a 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -655,6 +655,39 @@ impl PageContent { } } +fn append_page_procset(output: &mut Vec, page: &DisplayList, has_rgb_image: bool) { + use crate::pdf_image::RasterProcSet; + let mut text = page.pdf_space_plan.as_ref().is_some_and(|plan| plan.dedicated.is_some()); + let mut gray = false; + let mut color = has_rgb_image; + let mut indexed = false; + for item in &page.items { + match item { + DisplayItem::NativeGlyph { .. } | DisplayItem::OpenTypeGlyph { .. } + | DisplayItem::MathGlyph { .. } | DisplayItem::Text { .. } => text = true, + DisplayItem::PdfImage { image, .. } => match image.image.raster_procset() { + Some(RasterProcSet::Gray) => gray = true, + Some(RasterProcSet::Color) => color = true, + Some(RasterProcSet::Indexed) => indexed = true, + None => {} + }, + _ => {} + } + } + output.extend_from_slice(b" /ProcSet [/PDF"); + for (used, name) in [ + (text, b" /Text".as_slice()), + (gray, b" /ImageB"), + (color || indexed, b" /ImageC"), + (indexed, b" /ImageI"), + ] { + if used { + output.extend_from_slice(name); + } + } + output.push(b']'); +} + pub(crate) fn prepare_text_spaces( pages: &mut [DisplayList], mut resolve: impl FnMut( @@ -2539,11 +2572,15 @@ fn write_pdf( page.extend_from_slice(format!(" /MediaBox [0 0 {} {}]", physical(width), physical(height)).as_bytes()); } page.extend_from_slice(b" /Resources "); - if let Some(additional) = remapped.as_ref().map(|resources| &resources.pages[page_index]) - && !additional.is_empty() - { + let additional = remapped.as_ref().map(|resources| resources.pages[page_index].as_slice()) + .unwrap_or_default(); + let writes_procset = list.pdf.as_ref().is_some_and(|pdf| pdf.writes_procset()); + if !additional.is_empty() || writes_procset { page.extend_from_slice(&resources.as_bytes()[..resources.len() - 2]); page.extend_from_slice(additional); + if writes_procset { + append_page_procset(&mut page, list, !used.is_empty()); + } page.extend_from_slice(b" >>"); } else { page.extend_from_slice(resources.as_bytes()); diff --git a/crates/texrra-tex-core/src/pdf_device.rs b/crates/texrra-tex-core/src/pdf_device.rs index aa11792..222dc85 100644 --- a/crates/texrra-tex-core/src/pdf_device.rs +++ b/crates/texrra-tex-core/src/pdf_device.rs @@ -32,6 +32,19 @@ pub struct PdfPageData { pub driver: PdfDriver, pub file: PdfFileOptions, pub stream_compression: u8, + #[serde(default, skip_serializing_if = "is_zero")] + pub omit_procset: i32, +} + +fn is_zero(value: &i32) -> bool { + *value == 0 +} + +impl PdfPageData { + pub(crate) fn writes_procset(&self) -> bool { + self.driver == PdfDriver::PdfTeX + && (self.omit_procset < 0 || (self.omit_procset == 0 && self.file.major < 2)) + } } /// TeX round_xn_over_d, including signed halfway cases. diff --git a/crates/texrra-tex-core/src/pdf_image.rs b/crates/texrra-tex-core/src/pdf_image.rs index 1011966..45bbf2f 100644 --- a/crates/texrra-tex-core/src/pdf_image.rs +++ b/crates/texrra-tex-core/src/pdf_image.rs @@ -326,6 +326,8 @@ enum Part { Jpeg { size: [u16; 2], precision: u8, + #[serde(default)] + components: u8, bytes: Vec, }, Png(png::SampledImage), @@ -334,6 +336,12 @@ enum Part { MappedType1(Arc), } +pub(crate) enum RasterProcSet { + Gray, + Color, + Indexed, +} + pub(crate) fn real(value: f64) -> String { let body = format!("{value:.15}"); body.trim_end_matches('0').trim_end_matches('.').to_owned() @@ -425,6 +433,7 @@ impl PdfImage { parts.push(Part::Jpeg { size: header.size, precision: header.precision, + components: header.components, bytes: resource.data, }); parts.push(Part::Bytes(b"\nendstream".to_vec())); @@ -516,6 +525,21 @@ impl PdfImage { }) } + pub(crate) fn raster_procset(&self) -> Option { + self.objects.first()?.iter().find_map(|part| match part { + Part::Jpeg { components, bytes, .. } => { + let components = if *components == 0 { + jpeg::read(bytes).ok()?.components + } else { + *components + }; + Some(if components == 1 { RasterProcSet::Gray } else { RasterProcSet::Color }) + } + Part::Png(samples) => Some(samples.procset()), + _ => None, + }) + } + fn sampled_image(&self) -> Option<&png::SampledImage> { match self.objects.first()?.as_slice() { [Part::Png(samples) | Part::Bmp(samples)] => Some(samples), diff --git a/crates/texrra-tex-core/src/pdf_image/png.rs b/crates/texrra-tex-core/src/pdf_image/png.rs index c8695ed..ccbd7e1 100644 --- a/crates/texrra-tex-core/src/pdf_image/png.rs +++ b/crates/texrra-tex-core/src/pdf_image/png.rs @@ -1,5 +1,5 @@ //! Decode PNG samples for native PDF image XObjects. -use super::{Part, PdfImage}; +use super::{Part, PdfImage, RasterProcSet}; use crate::{pdf::objects, resources::LoadedResource}; use ::png::{ColorType, Decoder, PixelDimensions, Transformations, Unit}; use serde::{Deserialize, Serialize}; @@ -18,6 +18,8 @@ enum ColorSpace { pub(super) struct SampledImage { pub(super) size: [u32; 2], pub(super) source_depth: u8, + #[serde(default)] + source_indexed: bool, depth: u8, color: ColorSpace, samples: Vec, @@ -34,6 +36,7 @@ pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option Result<(SampledImage, Option RasterProcSet { + // Expanding a PNG transparency table may change the decoded colors. + if self.source_indexed || matches!(self.color, ColorSpace::Indexed(_)) { + RasterProcSet::Indexed + } else if matches!(self.color, ColorSpace::Gray) { + RasterProcSet::Gray + } else { + RasterProcSet::Color + } + } + pub(super) fn has_alpha(&self, pdf_version: (i32, i32)) -> bool { pdf_version >= (1, 4) && !self.alpha.is_empty() } diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 2261fec..5431583 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -2175,6 +2175,44 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。 全量编译一致。incremental-receipt.json 的 SHA-256 为 bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。 +### pdfTeX 每页 ProcSet 与实际使用的资源 + +\pdfomitprocset 是受作用域管理的整数。出页时记录有效值。负值 +在所有 PDF 版本生成 ProcSet,零值在 PDF 1.x 生成,正值省去 +自动生成的条目。用户资源仍参与输出,自动生成项最后写入, +与参考的同名资源行为相符。 +[pdfTeX 页面资源写出顺序与 ProcSet](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L19290-L19386) + +页面资源取实际使用的文字、假空格字体及放置的光栅图。JPEG +以分量分类。PNG 在透明度展开前保存索引类型,带透明色表的 +索引 PNG 同时记入 ImageC 与 ImageI。导入 PDF 的资源留在 +各自的表单内。已有 Type 3 字体的内部资源由其设备写出路径处理。 +[参考 PNG 的原始色表类型](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/writepng.c#L56-L72) + +九个定向用例的 23 页资源数组和两个导入表单的资源与参考相同, +覆盖规则、空白页、实际文字、未放置图像、灰度与彩色图、逐页 +分组、负值与 PDF 2.0。显式假空格进入 Text。其 PdfTeX-Space +物理字体的子集和编码差异也存在于前版,单独保留对照。旧 +schema79 已加载新参数,schema80 保存并恢复参数值。XeTeX 的 +profile 文本检查通过。另保留带装箱 PNG 的 INITEX 对照, +参考可 dump 后重载图像,原生此前已明确拒绝该格式内容。 + +四次只编辑第二页局部设置的跨文件修订各复用 109 步、重放四次 +文件调用。三页中只有第二页的 PDF 资源声明变化,预览替换零页。 +各次的 PDF、SVG、来源图和 aux 均与同初态独立全量一致。 +原样 Elsevier CAS 双栏经过三轮 LaTeX 与实际 BibTeX,六页的 +ProcSet、10372 个字形、1889 个词框、144 DPI 灰度图以及逐轮 +aux 和 bbl 与冻结参考一致。 + +回执 target/p3-pdf-page-procsets/receipt.json 的 SHA-256 为 +12337474673542c51c00a8dbe20c59f5b36b2e39ba0fe523d53cc4cbb2c9869f。 +下一项为导入 PDF 版本检查及 \pdfinclusionerrorlevel。参考端已 +固定警告、拒绝、安静处理、PDF 2.0 文件头及 Catalog Version 的 +实际边界。原生此前接受新版文件时漏警告,正值场景报告参数未 +定义,检查版本改变的错误位置也滞后。原始证据在同目录 +next-inclusion-version。 +[参考导入版本检查](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftoepdf.cc#L725-L743) + ### pdfTeX 作业末尾的字形表和文档信息开关 \pdfomitcharset 和 \pdfomitinfodict 通过普通整数参数进入 TeX diff --git a/docs/PUBLISHER_TEMPLATE_MATRIX.md b/docs/PUBLISHER_TEMPLATE_MATRIX.md index b56672a..d287168 100644 --- a/docs/PUBLISHER_TEMPLATE_MATRIX.md +++ b/docs/PUBLISHER_TEMPLATE_MATRIX.md @@ -977,6 +977,14 @@ LuaLaTex、languages 和 argues 的断行与冻结参考一致。PDF 提取的 字形提取仍待对照,当前整页版面门禁保持未通过。 阶段回执 `target/p3-xetex-native-hyphenation/receipt.json`。 +### Elsevier CAS 双栏逐页资源 + +原样 CAS 双栏的 30 项交付、schema51、实际字体映射和冻结参考 +用同一配置。三轮 LaTeX 和实际 BibTeX 的 aux 及 bbl 一致。 +六页的 ProcSet 与参考一致,10372 个原始字形位置、1889 个 +词框与 144 DPI 灰度图也一致。 +[阶段记录](P2_M1_PROGRESS.md#pdftex-每页-procset-与实际使用的资源) + ### 文档级字体共享后的 arXiv 与 Elsevier CAS 双栏 两份原样交付在同一原生可执行文件上各运行三轮 LaTeX 和一次实际