diff --git a/crates/typst-library/src/text/font/color.rs b/crates/typst-library/src/text/font/color.rs index a7e8d55ef..0a7b13c97 100644 --- a/crates/typst-library/src/text/font/color.rs +++ b/crates/typst-library/src/text/font/color.rs @@ -7,7 +7,7 @@ use typst_syntax::Span; use usvg::tiny_skia_path; use xmlwriter::XmlWriter; -use crate::foundations::{Bytes, Smart}; +use crate::foundations::Bytes; use crate::layout::{Abs, Frame, FrameItem, Point, Size}; use crate::text::{Font, Glyph}; use crate::visualize::{ @@ -105,11 +105,7 @@ fn draw_raster_glyph( raster_image: ttf_parser::RasterGlyphImage, ) -> Option<()> { let data = Bytes::new(raster_image.data.to_vec()); - let image = Image::new( - RasterImage::new(data, ExchangeFormat::Png, Smart::Auto).ok()?, - None, - Smart::Auto, - ); + let image = Image::plain(RasterImage::plain(data, ExchangeFormat::Png).ok()?); // Apple Color emoji doesn't provide offset information (or at least // not in a way ttf-parser understands), so we artificially shift their @@ -181,7 +177,7 @@ fn draw_colr_glyph( svg.end_element(); let data = Bytes::from_string(svg.end_document()); - let image = Image::new(SvgImage::new(data).ok()?, None, Smart::Auto); + let image = Image::plain(SvgImage::new(data).ok()?); let y_shift = Abs::pt(upem.to_pt() - y_max); let position = Point::new(Abs::pt(x_min), y_shift); @@ -257,7 +253,7 @@ fn draw_svg_glyph( ); let data = Bytes::from_string(wrapper_svg); - let image = Image::new(SvgImage::new(data).ok()?, None, Smart::Auto); + let image = Image::plain(SvgImage::new(data).ok()?); let position = Point::new(Abs::pt(left), Abs::pt(top) + upem); let size = Size::new(Abs::pt(width), Abs::pt(height)); diff --git a/crates/typst-library/src/visualize/image/mod.rs b/crates/typst-library/src/visualize/image/mod.rs index 19b1bdb4f..a13a7a207 100644 --- a/crates/typst-library/src/visualize/image/mod.rs +++ b/crates/typst-library/src/visualize/image/mod.rs @@ -290,6 +290,11 @@ impl Image { Self::new_impl(kind.into(), alt, scaling) } + /// Create an image with optional properties set to the default. + pub fn plain(kind: impl Into) -> Self { + Self::new(kind, None, Smart::Auto) + } + /// The internal, non-generic implementation. This is memoized to reuse /// the `Arc` and `LazyHash`. #[comemo::memoize] diff --git a/crates/typst-library/src/visualize/image/raster.rs b/crates/typst-library/src/visualize/image/raster.rs index 86455ce19..22f077d9e 100644 --- a/crates/typst-library/src/visualize/image/raster.rs +++ b/crates/typst-library/src/visualize/image/raster.rs @@ -33,10 +33,15 @@ impl RasterImage { data: Bytes, format: impl Into, icc: Smart, - ) -> StrResult { + ) -> StrResult { Self::new_impl(data, format.into(), icc) } + /// Create a raster image with optional properties set to the default. + pub fn plain(data: Bytes, format: impl Into) -> StrResult { + Self::new(data, format, Smart::Auto) + } + /// The internal, non-generic implementation. #[comemo::memoize] #[typst_macros::time(name = "load raster image")] @@ -425,7 +430,7 @@ mod tests { fn test(path: &str, format: ExchangeFormat, dpi: f64) { let data = typst_dev_assets::get(path).unwrap(); let bytes = Bytes::new(data); - let image = RasterImage::new(bytes, format, Smart::Auto).unwrap(); + let image = RasterImage::plain(bytes, format).unwrap(); assert_eq!(image.dpi().map(f64::round), Some(dpi)); } diff --git a/crates/typst-svg/src/text.rs b/crates/typst-svg/src/text.rs index 2386006d7..e6620a59e 100644 --- a/crates/typst-svg/src/text.rs +++ b/crates/typst-svg/src/text.rs @@ -3,7 +3,7 @@ use std::io::Read; use base64::Engine; use ecow::EcoString; use ttf_parser::GlyphId; -use typst_library::foundations::{Bytes, Smart}; +use typst_library::foundations::Bytes; use typst_library::layout::{Abs, Point, Ratio, Size, Transform}; use typst_library::text::{Font, TextItem}; use typst_library::visualize::{ @@ -246,15 +246,8 @@ fn convert_bitmap_glyph_to_image(font: &Font, id: GlyphId) -> Option<(Image, f64 if raster.format != ttf_parser::RasterImageFormat::PNG { return None; } - let image = Image::new( - RasterImage::new( - Bytes::new(raster.data.to_vec()), - ExchangeFormat::Png, - Smart::Auto, - ) - .ok()?, - None, - Smart::Auto, + let image = Image::plain( + RasterImage::plain(Bytes::new(raster.data.to_vec()), ExchangeFormat::Png).ok()?, ); Some((image, raster.x as f64, raster.y as f64)) }