Bind pdfTeX image color spaces to written PDF objects
This commit is contained in:
1 parent
e10e0cce53
commit
520e8eede2
8 files changed
+147
-32
No files matched your search
@@ -47,6 +47,8 @@ pub struct PdfImage {
|
||||
pub natural: [i64; 2],
|
||||
extent: [u64; 2],
|
||||
objects: Vec<Vec<Part>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) color_space: Option<i32>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
imported_fonts: Vec<imported_fonts::ImportedType1>,
|
||||
}
|
||||
@@ -336,6 +338,7 @@ enum Part {
|
||||
Bmp(png::SampledImage),
|
||||
XdvCmykDecode,
|
||||
MappedType1(Arc<ImportedType1Use>),
|
||||
ImageColorSpace,
|
||||
}
|
||||
|
||||
pub(crate) enum RasterProcSet {
|
||||
@@ -362,6 +365,7 @@ impl PdfImage {
|
||||
resource: LoadedResource,
|
||||
fallback_resolution: i32,
|
||||
pdf_version: (i32, i32),
|
||||
color_space: Option<i32>,
|
||||
) -> Result<(Self, Option<&'static str>), String> {
|
||||
let header = jpeg::read(&resource.data).map_err(|error| {
|
||||
format!(
|
||||
@@ -390,7 +394,10 @@ impl PdfImage {
|
||||
let denominator = 100 * i64::from(density[axis]);
|
||||
(numerator + denominator / 2) / denominator
|
||||
});
|
||||
Ok((Self::jpeg_xobject(resource, header, natural, [1.0; 2], false), warning))
|
||||
Ok((
|
||||
Self::jpeg_xobject(resource, header, natural, [1.0; 2], false, color_space),
|
||||
warning,
|
||||
))
|
||||
}
|
||||
|
||||
fn jpeg_xobject(
|
||||
@@ -399,6 +406,7 @@ impl PdfImage {
|
||||
natural: [i64; 2],
|
||||
extent: [f64; 2],
|
||||
xdv: bool,
|
||||
color_space: Option<i32>,
|
||||
) -> Self {
|
||||
let [width, height] = header.size;
|
||||
let color = match header.components {
|
||||
@@ -413,14 +421,17 @@ impl PdfImage {
|
||||
let mut parts = vec![Part::Bytes(
|
||||
format!("/Type /XObject /Subtype /Image /Width {width} /Height {height}").into_bytes(),
|
||||
)];
|
||||
if let Some((_, intent)) = profile {
|
||||
if color_space.is_some() {
|
||||
parts.push(Part::Bytes(b" /ColorSpace ".to_vec()));
|
||||
parts.push(Part::ImageColorSpace);
|
||||
} else if let Some((_, intent)) = profile {
|
||||
parts.push(Part::Bytes(format!(" /Intent /{intent} /ColorSpace [/ICCBased ").into_bytes()));
|
||||
parts.push(Part::Ref(1));
|
||||
parts.push(Part::Bytes(b"]".to_vec()));
|
||||
} else {
|
||||
parts.push(Part::Bytes(format!(" /ColorSpace {color}").into_bytes()));
|
||||
}
|
||||
if header.components == 4 {
|
||||
if header.components == 4 && color_space.is_none() {
|
||||
if xdv && header.adobe {
|
||||
parts.push(Part::XdvCmykDecode);
|
||||
} else if !xdv {
|
||||
@@ -454,6 +465,7 @@ impl PdfImage {
|
||||
natural,
|
||||
extent: extent.map(f64::to_bits),
|
||||
objects,
|
||||
color_space,
|
||||
imported_fonts: Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -475,7 +487,7 @@ impl PdfImage {
|
||||
if resource.data.starts_with(jpeg::SIGNATURE) {
|
||||
let header = jpeg::read(&resource.data).map_err(invalid)?;
|
||||
let bp = extent(header.size.map(u32::from), header.xdv_density)?;
|
||||
return Ok(Self::jpeg_xobject(resource, header, natural(bp), bp, true));
|
||||
return Ok(Self::jpeg_xobject(resource, header, natural(bp), bp, true, None));
|
||||
}
|
||||
let (part, bp) = if Self::is_png(&resource) {
|
||||
let (image, physical) = png::decode(&resource.data).map_err(|error| invalid(&error))?;
|
||||
@@ -500,6 +512,7 @@ impl PdfImage {
|
||||
natural: natural(bp),
|
||||
extent: bp.map(f64::to_bits),
|
||||
objects: vec![vec![part]],
|
||||
color_space: None,
|
||||
imported_fonts: Vec::new(),
|
||||
})
|
||||
}
|
||||
@@ -511,8 +524,9 @@ impl PdfImage {
|
||||
pub(crate) fn read_png(
|
||||
resource: LoadedResource,
|
||||
fallback_resolution: i32,
|
||||
color_space: Option<i32>,
|
||||
) -> Result<(Self, Option<&'static str>), String> {
|
||||
png::read(resource, fallback_resolution)
|
||||
png::read(resource, fallback_resolution, color_space)
|
||||
}
|
||||
|
||||
pub(crate) fn raster_frame(&self) -> Option<([i64; 2], u8)> {
|
||||
@@ -723,6 +737,7 @@ impl PdfImage {
|
||||
natural,
|
||||
extent: [width.to_bits(), height.to_bits()],
|
||||
objects: graph.bodies,
|
||||
color_space: None,
|
||||
imported_fonts,
|
||||
},
|
||||
[left, bottom],
|
||||
@@ -740,9 +755,10 @@ impl PdfImage {
|
||||
compression: u8,
|
||||
pdf_version: (i32, i32),
|
||||
fonts: &crate::pdf::type1_fonts::References<'_>,
|
||||
color_space: Option<i64>,
|
||||
) -> usize {
|
||||
if let Some(samples) = self.sampled_image() {
|
||||
return samples.append(pdf, attributes, compression, pdf_version);
|
||||
return samples.append(pdf, attributes, compression, pdf_version, color_space);
|
||||
}
|
||||
let base = pdf.len() + 1;
|
||||
pdf.extend(self.objects.iter().enumerate().map(|(index, parts)| {
|
||||
@@ -758,6 +774,10 @@ impl PdfImage {
|
||||
match part {
|
||||
Part::Bytes(bytes) | Part::Jpeg { bytes, .. } => body.extend_from_slice(bytes),
|
||||
Part::Ref(id) => write!(body, "{} 0 R", base + id).unwrap(),
|
||||
Part::ImageColorSpace => {
|
||||
let id = color_space.expect("mapped image color space");
|
||||
write!(body, "{id} 0 R").unwrap();
|
||||
}
|
||||
Part::MappedType1(font) => {
|
||||
let reference = &fonts[&font.mapped.key()];
|
||||
body.extend_from_slice(b" /BaseFont ");
|
||||
|
||||
Reference in new issue
Block a user