mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
feat: use the infer crate to determine if pdf embeds should be compressed
This commit is contained in:
parent
9b09146a6b
commit
ce6975e65a
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -255,6 +255,17 @@ dependencies = [
|
|||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfb"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
|
||||||
|
dependencies = [
|
||||||
|
"byteorder",
|
||||||
|
"fnv",
|
||||||
|
"uuid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -1259,6 +1270,15 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "infer"
|
||||||
|
version = "0.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a588916bfdfd92e71cacef98a63d9b1f0d74d6599980d11894290e7ddefffcf7"
|
||||||
|
dependencies = [
|
||||||
|
"cfb",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "inotify"
|
name = "inotify"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
@ -3127,6 +3147,7 @@ dependencies = [
|
|||||||
"comemo",
|
"comemo",
|
||||||
"ecow",
|
"ecow",
|
||||||
"image",
|
"image",
|
||||||
|
"infer",
|
||||||
"krilla",
|
"krilla",
|
||||||
"krilla-svg",
|
"krilla-svg",
|
||||||
"serde",
|
"serde",
|
||||||
@ -3430,6 +3451,12 @@ version = "0.2.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uuid"
|
||||||
|
version = "1.16.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vcpkg"
|
name = "vcpkg"
|
||||||
version = "0.2.15"
|
version = "0.2.15"
|
||||||
|
@ -71,6 +71,7 @@ icu_segmenter = { version = "1.4", features = ["serde"] }
|
|||||||
if_chain = "1"
|
if_chain = "1"
|
||||||
image = { version = "0.25.5", default-features = false, features = ["png", "jpeg", "gif"] }
|
image = { version = "0.25.5", default-features = false, features = ["png", "jpeg", "gif"] }
|
||||||
indexmap = { version = "2", features = ["serde"] }
|
indexmap = { version = "2", features = ["serde"] }
|
||||||
|
infer = "0.19.0"
|
||||||
kamadak-exif = "0.6"
|
kamadak-exif = "0.6"
|
||||||
krilla = { version = "0.4.0", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
krilla = { version = "0.4.0", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
||||||
krilla-svg = "0.1.0"
|
krilla-svg = "0.1.0"
|
||||||
|
@ -23,6 +23,7 @@ bytemuck = { workspace = true }
|
|||||||
comemo = { workspace = true }
|
comemo = { workspace = true }
|
||||||
ecow = { workspace = true }
|
ecow = { workspace = true }
|
||||||
image = { workspace = true }
|
image = { workspace = true }
|
||||||
|
infer = { workspace = true }
|
||||||
krilla = { workspace = true }
|
krilla = { workspace = true }
|
||||||
krilla-svg = { workspace = true }
|
krilla-svg = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
@ -34,6 +34,7 @@ pub(crate) fn embed_files(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(embed.data.clone());
|
let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(embed.data.clone());
|
||||||
|
let compress = should_compress(&embed.data);
|
||||||
|
|
||||||
let file = EmbeddedFile {
|
let file = EmbeddedFile {
|
||||||
path,
|
path,
|
||||||
@ -41,7 +42,7 @@ pub(crate) fn embed_files(
|
|||||||
description,
|
description,
|
||||||
association_kind,
|
association_kind,
|
||||||
data: data.into(),
|
data: data.into(),
|
||||||
compress: true,
|
compress,
|
||||||
location: Some(span.into_raw().get()),
|
location: Some(span.into_raw().get()),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -52,3 +53,21 @@ pub(crate) fn embed_files(
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_compress(data: &[u8]) -> bool {
|
||||||
|
let Some(ty) = infer::get(data) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
match ty.matcher_type() {
|
||||||
|
infer::MatcherType::App => true,
|
||||||
|
infer::MatcherType::Archive => false,
|
||||||
|
infer::MatcherType::Audio => false,
|
||||||
|
infer::MatcherType::Book => true,
|
||||||
|
infer::MatcherType::Doc => true,
|
||||||
|
infer::MatcherType::Font => true,
|
||||||
|
infer::MatcherType::Image => false,
|
||||||
|
infer::MatcherType::Text => true,
|
||||||
|
infer::MatcherType::Video => false,
|
||||||
|
infer::MatcherType::Custom => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user