Bind pdfTeX image color spaces to written PDF objects

This commit is contained in:
gloamxun committed 2026-11-26 18:16:31 +08:00
1 parent e10e0cce53
commit 520e8eede2
8 files changed
+147 -32

No files matched your search

@@ -428,6 +428,7 @@ pub(crate) struct ResourceOutput {
page_references: BTreeMap<u32, u32>,
link_references: BTreeMap<u32, Option<(usize, usize)>>,
structure_references: BTreeSet<u32>,
image_color_space_objects: BTreeSet<u32>,
page_entries: Vec<Dictionary>,
font_opacities: Vec<BTreeMap<u8, String>>,
page_attributes: Vec<Dictionary>,
@@ -447,6 +448,7 @@ pub(crate) struct RemappedResources {
pub page_objects: BTreeMap<usize, usize>,
pub link_objects: BTreeMap<(usize, usize), usize>,
pub structure_objects: BTreeMap<u32, usize>,
pub object_numbers: BTreeMap<u32, u32>,
pub annotations: Vec<Vec<Option<Vec<u8>>>>,
pub catalog: Option<Vec<u8>>,
pub info: Option<Vec<u8>>,
@@ -463,6 +465,7 @@ impl ResourceOutput {
) -> Result<Self, String> {
let empty = PdfObjects::default();
let objects = objects.unwrap_or(&empty);
let mut image_color_spaces = BTreeSet::new();
let (page_entries, font_opacities): (Vec<_>, Vec<_>) = pages
.iter()
.map(|page| {
@@ -472,16 +475,24 @@ impl ResourceOutput {
"additional PDF resources overlap generated Font or XObject entries".into(),
);
}
let alphas = page
.items
.iter()
.filter_map(|item| match item {
crate::render::DisplayItem::OpenTypeColor(Some([_, _, _, alpha])) => {
Some(*alpha)
use crate::render::DisplayItem;
let mut alphas = BTreeSet::new();
for item in &page.items {
let image = match item {
DisplayItem::OpenTypeColor(Some([_, _, _, alpha])) => {
alphas.insert(*alpha);
continue;
}
_ => None,
})
.collect();
DisplayItem::PdfImage { image, .. } => image,
DisplayItem::PdfImageObject(object) => &object.image,
_ => continue,
};
if let Some(number) = image.image.color_space
&& let Ok(number) = u32::try_from(number)
{
image_color_spaces.insert(number);
}
}
let names = add_font_opacities(&mut entries, alphas, objects)?;
Ok((entries, names))
})
@@ -572,11 +583,22 @@ impl ResourceOutput {
references(&value, &mut pending)?;
values.insert(number, value);
}
// An image reference does not schedule an object for writing. Keep
// full numeric precision in the written color space and its graph.
let mut image_color_space_objects = BTreeSet::new();
while let Some(number) = image_color_spaces.pop_first() {
if let Some(value) = values.get(&number)
&& image_color_space_objects.insert(number)
{
references(value, &mut image_color_spaces)?;
}
}
Ok(Self {
values,
page_references: objects.page_references.clone(),
link_references,
structure_references,
image_color_space_objects,
page_entries,
font_opacities,
page_attributes,
@@ -592,7 +614,7 @@ impl ResourceOutput {
self,
output: &mut Vec<Vec<u8>>,
diagnostics: &mut Vec<String>,
) -> RemappedResources {
) -> Result<RemappedResources, String> {
let mut ids = BTreeMap::new();
let mut page_objects = BTreeMap::new();
for (page, number) in self.page_references {
@@ -625,9 +647,14 @@ impl ResourceOutput {
.enumerate()
.map(|(i, number)| (number, (output.len() + i + 1) as u32)),
);
for (_, mut object) in self.values {
for (number, mut object) in self.values {
remap(&mut object, &ids);
output.push(encode_value(object).expect("valid PDF object in memory"));
let body = if self.image_color_space_objects.contains(&number) {
super::xdv_object::encode_device_value(object)
} else {
encode_value(object)
};
output.push(body?);
}
let structure_objects = self
.structure_references
@@ -659,7 +686,7 @@ impl ResourceOutput {
bytes[2..bytes.len() - 2].to_vec()
};
let encode = |dictionary| encode_dictionary(dictionary, false);
RemappedResources {
Ok(RemappedResources {
pages: self
.page_entries
.into_iter()
@@ -680,7 +707,8 @@ impl ResourceOutput {
.collect(),
catalog: self.catalog.map(&encode),
info: self.info.map(encode),
}
object_numbers: ids,
})
}
}
@@ -314,7 +314,7 @@ pub(super) fn encode_device_value(object: Object) -> Result<Vec<u8>, String> {
fn append(bytes: &mut Vec<u8>, object: Object) -> Result<(), String> {
match object {
Object::Real(value) if value.is_finite() => write!(bytes, "{value}").unwrap(),
Object::Real(_) => return Err("non-finite number in XDV PDF object".into()),
Object::Real(_) => return Err("non-finite number in PDF object".into()),
Object::Array(values) => {
bytes.push(b'[');
for value in values {
@@ -333,6 +333,12 @@ pub(super) fn encode_device_value(object: Object) -> Result<Vec<u8>, String> {
}
bytes.extend_from_slice(b">>");
}
Object::Stream(stream) => {
append(bytes, Object::Dictionary(stream.dict))?;
bytes.extend_from_slice(b"\nstream\n");
bytes.extend_from_slice(&stream.content);
bytes.extend_from_slice(b"\nendstream");
}
_ => bytes.extend(encode_value(object)?),
}
Ok(())