Read binary TeX inputs and render default ICC colors

This commit is contained in:
gloamxun committed 2026-11-29 02:55:37 +08:00
1 parent e9893a5c1b
commit 52db77885d
10 files changed
+379 -19

No files matched your search

@@ -1192,6 +1192,53 @@ impl Executor {
self.print_tokens(&value, token)?;
}
}
Pdffiledump => {
let offset = if self.keyword("offset", files)? {
usize::try_from(self.integer(files)?)
.map_err(|_| self.error("Bad file offset", token.span))?
} else {
0
};
let length = if self.keyword("length", files)? {
usize::try_from(self.integer(files)?)
.map_err(|_| self.error("Bad dump length", token.span))?
} else {
0
};
let body = self.scan_pdf_tokens(files)?;
if length == 0 {
return Ok(None);
}
if length > self.limits.tokens / 2 {
return self.fail("native file dump token limit exceeded");
}
let name = self.token_list_text(&body);
let mut name = self.decode_filename(name)?;
if self.state.input_profile() != InputProfile::XeTeX {
name.retain(|c| c != '"');
}
let from = self.source_name(token.span);
self.reads.insert(Dependency::FileLookup {
name: name.clone(),
from: from.clone(),
});
if let Some(resource) = files.optional_tex_input(&name, &from)
.map_err(|message| self.error(message, token.span))?
&& let Some(tail) = resource.data.get(offset..)
{
const HEX: &[u8; 16] = b"0123456789ABCDEF";
let bytes = &tail[..tail.len().min(length)];
let mut output = std::string::String::with_capacity(bytes.len() * 2);
for chunk in bytes.chunks(4096) {
self.check_cancellation()?;
for &byte in chunk {
output.push(char::from(HEX[usize::from(byte >> 4)]));
output.push(char::from(HEX[usize::from(byte & 15)]));
}
}
self.print_tokens(&output, token)?;
}
}
Pdfmdfivesum => {
let file = self.keyword("file", files)?;
let body = self.scan_pdf_tokens(files)?;
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 109;
const COMPATIBLE_SCHEMAS: [u32; 75] = [
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, SCHEMA,
const SCHEMA: u32 = 110;
const COMPATIBLE_SCHEMAS: [u32; 76] = [
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, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -241,6 +241,9 @@ impl FormatImage {
if image.schema < 109 {
image.state.install_nonscript_command();
}
if image.schema < 110 {
image.state.install_filedump_command();
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
@@ -96,6 +96,7 @@ pub enum Primitive {
Etexrevision,
Pdffilesize,
Pdffilemoddate,
Pdffiledump,
Pdfmdfivesum,
Pdfxform,
Pdfrefxform,
@@ -527,6 +528,7 @@ impl Primitive {
| Etexrevision
| Pdffilesize
| Pdffilemoddate
| Pdffiledump
| Pdfmdfivesum
| Jobname
| Pdfcreationdate
@@ -954,6 +956,7 @@ const PDFTEX_ONLY_PRIMITIVES: &[(&str, Primitive)] = {
("letterspacefont", Letterspacefont),
("pdffilesize", Pdffilesize),
("pdffilemoddate", Pdffilemoddate),
("pdffiledump", Pdffiledump),
("pdfmdfivesum", Pdfmdfivesum),
("pdfxform", Pdfxform),
("pdfrefxform", Pdfrefxform),
@@ -1018,6 +1021,7 @@ const XETEX_PRIMITIVE_ALIASES: &[(&str, Primitive)] = {
&[
("creationdate", Pdfcreationdate),
("filemoddate", Pdffilemoddate),
("filedump", Pdffiledump),
("filesize", Pdffilesize),
("mdfivesum", Pdfmdfivesum),
("normaldeviate", Pdfnormaldeviate),
@@ -2045,6 +2049,15 @@ impl State {
self.update_profile_command("pdfinsertht", Meaning::Primitive(Primitive::Pdfinsertht), enabled);
}
pub(super) fn install_filedump_command(&mut self) {
let name = if self.input_profile == super::InputProfile::XeTeX {
"filedump"
} else {
"pdffiledump"
};
self.update_profile_command(name, Meaning::Primitive(Primitive::Pdffiledump), true);
}
fn install_pdftex_primitives(&mut self) {
let enabled = self.input_profile != super::InputProfile::XeTeX;
for &(name, primitive) in PDFTEX_ONLY_PRIMITIVES {
@@ -786,6 +786,7 @@ pub struct PageResources {
pub(crate) has_page_group: bool,
pub(crate) opacity: BTreeMap<Vec<u8>, Transparency>,
pub(crate) needs_pdf_for_preview: bool,
pub(crate) has_default_color_spaces: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
@@ -838,25 +839,27 @@ impl PageResources {
names: &BTreeSet<Vec<u8>>,
objects: &PdfObjects,
) -> Result<Self, String> {
if names.is_empty() {
return Ok(Self {
objects: allocation,
entries: bytes,
attributes,
..Self::default()
});
}
let entries = parse_entries(&bytes)?;
let definitions = entries
.get(b"ExtGState")
.map_err(|_| "PDF graphics state used without an ExtGState resource")?;
let definitions = resolve_dictionary(definitions.clone(), objects)?;
let mut resources = Self {
objects: allocation,
entries: bytes,
attributes,
..Self::default()
};
if let Ok(color_spaces) = entries.get(b"ColorSpace")
&& let Object::Dictionary(colors) = resolve_value(color_spaces.clone(), objects)?
{
resources.has_default_color_spaces = [b"DefaultGray".as_slice(), b"DefaultRGB", b"DefaultCMYK"]
.iter()
.any(|name| colors.get(name).is_ok_and(|value| !matches!(value, Object::Null)));
}
if names.is_empty() {
return Ok(resources);
}
let definitions = entries
.get(b"ExtGState")
.map_err(|_| "PDF graphics state used without an ExtGState resource")?;
let definitions = resolve_dictionary(definitions.clone(), objects)?;
for name in names {
let definition = definitions.get(name).map_err(|_| {
format!(
+7
View File
@@ -351,9 +351,16 @@ impl SvgTransparency {
}
impl DisplayList {
/// Whether page or captured form colors depend on a PDF default color space.
pub fn has_pdf_default_colors(&self) -> bool {
self.pdf_resources.has_default_color_spaces
|| self.forms.iter().any(|form| form.list.pdf_resources.has_default_color_spaces)
}
/// Whether this page needs the complete PDF device output for SVG preview.
pub fn has_embedded_pdf(&self) -> bool {
self.pdf_resources.needs_pdf_for_preview
|| self.pdf_resources.has_default_color_spaces
|| self.pdf_background.as_ref().is_some_and(|program| program.needs_pdf_for_preview())
|| self
.items