Import PNG images in the native PDF device

This commit is contained in:
gloamxun committed 2026-11-21 04:13:18 +08:00
1 parent 297431a547
commit 555a029505
13 files changed
+467 -35

No files matched your search

+33 -11
View File
@@ -22,7 +22,7 @@ use crate::type1::Type1Font;
#[path = "pdf_text.rs"]
mod native_text;
#[path = "pdf_objects.rs"]
mod objects;
pub(crate) mod objects;
#[path = "pdf_pk.rs"]
mod pk_fonts;
@@ -983,7 +983,7 @@ fn page_content(
height: image_height,
image,
} => {
let [sx, sy] = if image.image.jpeg_frame().is_some()
let [sx, sy] = if image.image.raster_frame().is_some()
&& list.units == crate::render::DisplayUnits::TeX
{
[
@@ -2183,15 +2183,21 @@ fn write_pdf(
let mut page_ids: Vec<usize> = Vec::new();
let mut destinations = BTreeMap::new();
let mut numbers = BTreeMap::new();
let pdf_version = pages
.first()
.and_then(|page| page.pdf.as_ref())
.map_or((1, 4), |pdf| (pdf.file.major, i32::from(pdf.file.minor)));
let mut form_ids = BTreeMap::new();
for list in pages {
for item in &list.items {
if let DisplayItem::PdfImageObject(object) = item {
form_ids.entry(object.image.id).or_insert_with(|| {
object
.image
.image
.append_to(&mut objects, &object.attributes)
object.image.image.append_to(
&mut objects,
&object.attributes,
object.compression,
pdf_version,
)
});
}
}
@@ -2199,13 +2205,29 @@ fn write_pdf(
for list in pages {
for item in &list.items {
if let DisplayItem::PdfImage { image, .. } = item {
form_ids
.entry(image.id)
.or_insert_with(|| image.image.append_to(&mut objects, &[]));
form_ids.entry(image.id).or_insert_with(|| {
let compression = list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression);
image.image.append_to(&mut objects, &[], compression, pdf_version)
});
}
}
}
let mut png_group = None;
for (page_index, list) in pages.iter().enumerate() {
let png_group_ref = if !list.pdf_resources.has_page_group
&& list.items.iter().any(|item| {
matches!(item, DisplayItem::PdfImage { image, .. }
if image.image.png_transparency(pdf_version))
})
{
let id = *png_group.get_or_insert_with(|| {
objects.push(b"<< /Type /Group /S /Transparency /CS /DeviceRGB /I true >>".to_vec());
objects.len()
});
format!(" /Group {id} 0 R")
} else {
String::new()
};
let width = if final_compression.is_some() {
list.width
} else {
@@ -2343,9 +2365,9 @@ fn write_pdf(
if decorated {
page.extend_from_slice(format!(" /Contents {content_id} 0 R ").as_bytes());
page.extend_from_slice(&remapped.as_ref().expect("validated page dictionary").page_attributes[page_index]);
page.extend_from_slice(format!(" /Parent 2 0 R{annots} >>").as_bytes());
page.extend_from_slice(format!(" /Parent 2 0 R{annots}{png_group_ref} >>").as_bytes());
} else {
page.extend_from_slice(format!("{annots} /Contents {content_id} 0 R >>").as_bytes());
page.extend_from_slice(format!("{annots} /Contents {content_id} 0 R{png_group_ref} >>").as_bytes());
}
if let Some(id) = reserved_page {
objects[id - 1] = page;