summaryrefslogtreecommitdiff
path: root/crates/typst-pdf/src/embed.rs
diff options
context:
space:
mode:
authorTobias Schmitz <tobiasschmitz2001@gmail.com>2025-05-12 10:07:43 +0200
committerGitHub <noreply@github.com>2025-05-12 08:07:43 +0000
commit26c19a49c8a73b1e7f7c299b9e25e57acfcd7eac (patch)
tree3804e0d2569bc64c36716de032a1322ebe5e96ad /crates/typst-pdf/src/embed.rs
parent54c5113a83d317ed9c37a129ad90165c100bd25d (diff)
Use the infer crate to determine if pdf embeds should be compressed (#6256)
Diffstat (limited to 'crates/typst-pdf/src/embed.rs')
-rw-r--r--crates/typst-pdf/src/embed.rs70
1 files changed, 69 insertions, 1 deletions
diff --git a/crates/typst-pdf/src/embed.rs b/crates/typst-pdf/src/embed.rs
index 6ed65a2b..f0cd9060 100644
--- a/crates/typst-pdf/src/embed.rs
+++ b/crates/typst-pdf/src/embed.rs
@@ -34,6 +34,8 @@ pub(crate) fn embed_files(
},
};
let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(embed.data.clone());
+ // TODO: update when new krilla version lands (https://github.com/LaurenzV/krilla/pull/203)
+ let compress = should_compress(&embed.data).unwrap_or(true);
let file = EmbeddedFile {
path,
@@ -41,7 +43,7 @@ pub(crate) fn embed_files(
description,
association_kind,
data: data.into(),
- compress: true,
+ compress,
location: Some(span.into_raw().get()),
};
@@ -52,3 +54,69 @@ pub(crate) fn embed_files(
Ok(())
}
+
+fn should_compress(data: &[u8]) -> Option<bool> {
+ let ty = infer::get(data)?;
+ match ty.matcher_type() {
+ infer::MatcherType::App => None,
+ infer::MatcherType::Archive => match ty.mime_type() {
+ #[rustfmt::skip]
+ "application/zip"
+ | "application/vnd.rar"
+ | "application/gzip"
+ | "application/x-bzip2"
+ | "application/vnd.bzip3"
+ | "application/x-7z-compressed"
+ | "application/x-xz"
+ | "application/vnd.ms-cab-compressed"
+ | "application/vnd.debian.binary-package"
+ | "application/x-compress"
+ | "application/x-lzip"
+ | "application/x-rpm"
+ | "application/zstd"
+ | "application/x-lz4"
+ | "application/x-ole-storage" => Some(false),
+ _ => None,
+ },
+ infer::MatcherType::Audio => match ty.mime_type() {
+ #[rustfmt::skip]
+ "audio/mpeg"
+ | "audio/m4a"
+ | "audio/opus"
+ | "audio/ogg"
+ | "audio/x-flac"
+ | "audio/amr"
+ | "audio/aac"
+ | "audio/x-ape" => Some(false),
+ _ => None,
+ },
+ infer::MatcherType::Book => None,
+ infer::MatcherType::Doc => None,
+ infer::MatcherType::Font => None,
+ infer::MatcherType::Image => match ty.mime_type() {
+ #[rustfmt::skip]
+ "image/jpeg"
+ | "image/jp2"
+ | "image/png"
+ | "image/webp"
+ | "image/vnd.ms-photo"
+ | "image/heif"
+ | "image/avif"
+ | "image/jxl"
+ | "image/vnd.djvu" => None,
+ _ => None,
+ },
+ infer::MatcherType::Text => None,
+ infer::MatcherType::Video => match ty.mime_type() {
+ #[rustfmt::skip]
+ "video/mp4"
+ | "video/x-m4v"
+ | "video/x-matroska"
+ | "video/webm"
+ | "video/quicktime"
+ | "video/x-flv" => Some(false),
+ _ => None,
+ },
+ infer::MatcherType::Custom => None,
+ }
+}