mirror of
https://github.com/typst/typst
synced 2025-06-08 05:06:24 +08:00
Add support for WebP images (#6311)
This commit is contained in:
parent
5f776c7372
commit
1de2095f67
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1215,6 +1215,7 @@ dependencies = [
|
||||
"byteorder-lite",
|
||||
"color_quant",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"num-traits",
|
||||
"png",
|
||||
"zune-core",
|
||||
|
@ -69,7 +69,7 @@ icu_provider_adapters = "1.4"
|
||||
icu_provider_blob = "1.4"
|
||||
icu_segmenter = { version = "1.4", features = ["serde"] }
|
||||
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", "webp"] }
|
||||
indexmap = { version = "2", features = ["serde"] }
|
||||
infer = { version = "0.19.0", default-features = false }
|
||||
kamadak-exif = "0.6"
|
||||
|
@ -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"],
|
||||
|
@ -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()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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"),
|
||||
})
|
||||
}
|
||||
|
@ -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(),
|
||||
),
|
||||
|
@ -69,7 +69,7 @@ the first item of the list above by indenting it.
|
||||
|
||||
## Adding a figure { #figure }
|
||||
You think that your report would benefit from a figure. Let's add one. Typst
|
||||
supports images in the formats PNG, JPEG, GIF, and SVG. To add an image file to
|
||||
supports images in the formats PNG, JPEG, GIF, SVG, and WebP. To add an image file to
|
||||
your project, first open the _file panel_ by clicking the box icon in the left
|
||||
sidebar. Here, you can see a list of all files in your project. Currently, there
|
||||
is only one: The main Typst file you are writing in. To upload another file,
|
||||
|
@ -243,7 +243,7 @@ A #box(image("/assets/images/tiger.jpg", height: 1cm, width: 80%)) B
|
||||
--- image-png-but-pixmap-format ---
|
||||
#image(
|
||||
read("/assets/images/tiger.jpg", encoding: none),
|
||||
// Error: 11-18 expected "png", "jpg", "gif", dictionary, "svg", or auto
|
||||
// Error: 11-18 expected "png", "jpg", "gif", "webp", dictionary, "svg", or auto
|
||||
format: "rgba8",
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user