summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorLinus Unnebäck <linus@folkdatorn.se>2025-06-04 11:54:03 +0200
committerGitHub <noreply@github.com>2025-06-04 09:54:03 +0000
commit1de2095f67c9719a973868618c3548dd6083f534 (patch)
tree7b51d37a037c67fe0b2ecd1582970e83afd0cc5f /crates
parent5f776c7372ffecbbe959fbfa968c8c91efaf0061 (diff)
Add support for WebP images (#6311)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst-ide/src/complete.rs4
-rw-r--r--crates/typst-layout/src/image.rs1
-rw-r--r--crates/typst-library/src/visualize/image/mod.rs4
-rw-r--r--crates/typst-library/src/visualize/image/raster.rs6
-rw-r--r--crates/typst-svg/src/image.rs1
5 files changed, 13 insertions, 3 deletions
diff --git a/crates/typst-ide/src/complete.rs b/crates/typst-ide/src/complete.rs
index 15b4296e..4a36045a 100644
--- a/crates/typst-ide/src/complete.rs
+++ b/crates/typst-ide/src/complete.rs
@@ -841,7 +841,9 @@ fn param_value_completions<'a>(
/// Returns which file extensions to complete for the given parameter if any.
fn path_completion(func: &Func, param: &ParamInfo) -> Option<&'static [&'static str]> {
Some(match (func.name(), param.name) {
- (Some("image"), "source") => &["png", "jpg", "jpeg", "gif", "svg", "svgz"],
+ (Some("image"), "source") => {
+ &["png", "jpg", "jpeg", "gif", "svg", "svgz", "webp"]
+ }
(Some("csv"), "source") => &["csv"],
(Some("plugin"), "source") => &["wasm"],
(Some("cbor"), "source") => &["cbor"],
diff --git a/crates/typst-layout/src/image.rs b/crates/typst-layout/src/image.rs
index 3e5b7d8b..8136a25a 100644
--- a/crates/typst-layout/src/image.rs
+++ b/crates/typst-layout/src/image.rs
@@ -147,6 +147,7 @@ fn determine_format(source: &DataSource, data: &Bytes) -> StrResult<ImageFormat>
"jpg" | "jpeg" => return Ok(ExchangeFormat::Jpg.into()),
"gif" => return Ok(ExchangeFormat::Gif.into()),
"svg" | "svgz" => return Ok(VectorFormat::Svg.into()),
+ "webp" => return Ok(ExchangeFormat::Webp.into()),
_ => {}
}
}
diff --git a/crates/typst-library/src/visualize/image/mod.rs b/crates/typst-library/src/visualize/image/mod.rs
index 258eb96f..f9e345e7 100644
--- a/crates/typst-library/src/visualize/image/mod.rs
+++ b/crates/typst-library/src/visualize/image/mod.rs
@@ -77,8 +77,8 @@ pub struct ImageElem {
/// [`source`]($image.source) (even then, Typst will try to figure out the
/// format automatically, but that's not always possible).
///
- /// Supported formats are `{"png"}`, `{"jpg"}`, `{"gif"}`, `{"svg"}` as well
- /// as raw pixel data. Embedding PDFs as images is
+ /// Supported formats are `{"png"}`, `{"jpg"}`, `{"gif"}`, `{"svg"}`,
+ /// `{"webp"}` as well as raw pixel data. Embedding PDFs as images is
/// [not currently supported](https://github.com/typst/typst/issues/145).
///
/// When providing raw pixel data as the `source`, you must specify a
diff --git a/crates/typst-library/src/visualize/image/raster.rs b/crates/typst-library/src/visualize/image/raster.rs
index 21d5b18f..54f832ba 100644
--- a/crates/typst-library/src/visualize/image/raster.rs
+++ b/crates/typst-library/src/visualize/image/raster.rs
@@ -9,6 +9,7 @@ use ecow::{eco_format, EcoString};
use image::codecs::gif::GifDecoder;
use image::codecs::jpeg::JpegDecoder;
use image::codecs::png::PngDecoder;
+use image::codecs::webp::WebPDecoder;
use image::{
guess_format, DynamicImage, ImageBuffer, ImageDecoder, ImageResult, Limits, Pixel,
};
@@ -77,6 +78,7 @@ impl RasterImage {
ExchangeFormat::Jpg => decode(JpegDecoder::new(cursor), icc),
ExchangeFormat::Png => decode(PngDecoder::new(cursor), icc),
ExchangeFormat::Gif => decode(GifDecoder::new(cursor), icc),
+ ExchangeFormat::Webp => decode(WebPDecoder::new(cursor), icc),
}
.map_err(format_image_error)?;
@@ -242,6 +244,8 @@ pub enum ExchangeFormat {
/// Raster format that is typically used for short animated clips. Typst can
/// load GIFs, but they will become static.
Gif,
+ /// Raster format that supports both lossy and lossless compression.
+ Webp,
}
impl ExchangeFormat {
@@ -257,6 +261,7 @@ impl From<ExchangeFormat> for image::ImageFormat {
ExchangeFormat::Png => image::ImageFormat::Png,
ExchangeFormat::Jpg => image::ImageFormat::Jpeg,
ExchangeFormat::Gif => image::ImageFormat::Gif,
+ ExchangeFormat::Webp => image::ImageFormat::WebP,
}
}
}
@@ -269,6 +274,7 @@ impl TryFrom<image::ImageFormat> for ExchangeFormat {
image::ImageFormat::Png => ExchangeFormat::Png,
image::ImageFormat::Jpeg => ExchangeFormat::Jpg,
image::ImageFormat::Gif => ExchangeFormat::Gif,
+ image::ImageFormat::WebP => ExchangeFormat::Webp,
_ => bail!("format not yet supported"),
})
}
diff --git a/crates/typst-svg/src/image.rs b/crates/typst-svg/src/image.rs
index d7443202..1868ca39 100644
--- a/crates/typst-svg/src/image.rs
+++ b/crates/typst-svg/src/image.rs
@@ -45,6 +45,7 @@ pub fn convert_image_to_base64_url(image: &Image) -> EcoString {
ExchangeFormat::Png => "png",
ExchangeFormat::Jpg => "jpeg",
ExchangeFormat::Gif => "gif",
+ ExchangeFormat::Webp => "webp",
},
raster.data(),
),