Render native XeTeX PNG, JPEG and BMP graphics

This commit is contained in:
gloamxun committed 2026-11-21 13:58:26 +08:00
1 parent 794c2f9b2c
commit 2bd6e19519
22 files changed
+821 -120

No files matched your search

+23 -12
View File
@@ -1,7 +1,7 @@
//! Decode PNG samples for native PDF image XObjects.
use super::{Part, PdfImage};
use crate::{pdf::objects, resources::LoadedResource};
use ::png::{ColorType, Decoder, Transformations, Unit};
use ::png::{ColorType, Decoder, PixelDimensions, Transformations, Unit};
use serde::{Deserialize, Serialize};
use std::{fmt::Write, io::Cursor};
@@ -15,7 +15,7 @@ enum ColorSpace {
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub(super) struct PngImage {
pub(super) struct SampledImage {
pub(super) size: [u32; 2],
pub(super) source_depth: u8,
depth: u8,
@@ -24,7 +24,7 @@ pub(super) struct PngImage {
alpha: Vec<u8>,
}
fn decode(bytes: &[u8]) -> Result<(PngImage, [u32; 2]), String> {
pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option<PixelDimensions>), String> {
let read_info = |transforms| {
let mut decoder = Decoder::new(Cursor::new(bytes));
decoder.set_transformations(transforms);
@@ -39,12 +39,9 @@ fn decode(bytes: &[u8]) -> Result<(PngImage, [u32; 2]), String> {
reader = read_info(Transformations::EXPAND)?;
}
let info = reader.info();
let density = info
let physical = info
.pixel_dims
.filter(|physical| physical.unit == Unit::Meter)
.map_or([0, 0], |physical| {
[physical.xppu, physical.yppu].map(|px| ((u64::from(px) * 254 + 5_000) / 10_000) as u32)
});
.filter(|physical| physical.unit == Unit::Meter);
let palette = info.palette.clone();
let capacity = reader
.output_buffer_size()
@@ -90,7 +87,7 @@ fn decode(bytes: &[u8]) -> Result<(PngImage, [u32; 2]), String> {
buf.truncate(alpha.len() * color_bytes);
}
Ok((
PngImage {
SampledImage {
size: [width, height],
source_depth,
depth,
@@ -98,7 +95,7 @@ fn decode(bytes: &[u8]) -> Result<(PngImage, [u32; 2]), String> {
samples: buf,
alpha,
},
density,
physical,
))
}
@@ -106,12 +103,15 @@ pub(super) fn read(
resource: LoadedResource,
fallback_resolution: i32,
) -> Result<(PdfImage, Option<&'static str>), String> {
let (image, mut density) = decode(&resource.data).map_err(|error| {
let (image, physical) = decode(&resource.data).map_err(|error| {
format!(
"could not parse PNG image {}: {error}",
resource.version.path.display()
)
})?;
let mut density = physical.map_or([0, 0], |physical| {
[physical.xppu, physical.yppu].map(|px| ((u64::from(px) * 254 + 5_000) / 10_000) as u32)
});
let warning = if density.iter().any(|&dpi| dpi > 65_535) {
Some("too large image resolution ignored")
} else if density != [0, 0] && density.contains(&0) {
@@ -141,7 +141,18 @@ pub(super) fn read(
))
}
impl PngImage {
impl SampledImage {
pub(super) fn rgb8(size: [u32; 2], samples: Vec<u8>) -> Self {
Self {
size,
source_depth: 8,
depth: 8,
color: ColorSpace::Rgb,
samples,
alpha: Vec::new(),
}
}
pub(super) fn has_alpha(&self, pdf_version: (i32, i32)) -> bool {
pdf_version >= (1, 4) && !self.alpha.is_empty()
}