diff --git a/src/layout/image.rs b/src/layout/image.rs index 94f57167a..81c48b3eb 100644 --- a/src/layout/image.rs +++ b/src/layout/image.rs @@ -1,8 +1,6 @@ use super::*; use crate::image::ImageId; -use ::image::GenericImageView; - /// An image node. #[derive(Debug)] #[cfg_attr(feature = "layout-cache", derive(Hash))] @@ -27,27 +25,22 @@ impl Layout for ImageNode { let width = self.width.map(|w| w.resolve(base.w)); let height = self.height.map(|w| w.resolve(base.h)); - let dimensions = ctx.images.get(self.id).buf.dimensions(); - let pixel_width = dimensions.0 as f64; - let pixel_height = dimensions.1 as f64; - let pixel_ratio = pixel_width / pixel_height; + let img = ctx.images.get(self.id); + let pixel_size = Spec::new(img.width() as f64, img.height() as f64); + let pixel_ratio = pixel_size.x / pixel_size.y; let size = match (width, height) { (Some(width), Some(height)) => Size::new(width, height), (Some(width), None) => Size::new(width, width / pixel_ratio), (None, Some(height)) => Size::new(height * pixel_ratio, height), (None, None) => { - constraints.exact = current.to_spec().map(Some); - - let ratio = current.w / current.h; - if ratio < pixel_ratio && current.w.is_finite() { + constraints.exact.x = Some(current.w); + if current.w.is_finite() { Size::new(current.w, current.w / pixel_ratio) - } else if current.h.is_finite() { - // TODO: Fix issue with line spacing. - Size::new(current.h * pixel_ratio, current.h) } else { - // Totally unbounded region, we have to make up something. - Size::new(Length::pt(pixel_width), Length::pt(pixel_height)) + // Totally unbounded region, we have to make up something, + // so it is 1pt per pixel. + pixel_size.map(Length::pt).to_size() } } }; diff --git a/tests/ref/elements/image.png b/tests/ref/elements/image.png index 6848726f8..943d43b1e 100644 Binary files a/tests/ref/elements/image.png and b/tests/ref/elements/image.png differ diff --git a/tests/typ/elements/image.typ b/tests/typ/elements/image.typ index 6359c4bb2..c0e6a3efc 100644 --- a/tests/typ/elements/image.typ +++ b/tests/typ/elements/image.typ @@ -13,15 +13,6 @@ --- // Test configuring the size and fitting behaviour of images. -// Fit to width of page. -#image("../../res/rhino.png") - -// Fit to height of page. -[ - #page(height: 40pt) - #image("../../res/rhino.png") -] - // Set width explicitly. #image("../../res/rhino.png", width: 50pt) @@ -35,6 +26,11 @@ #align(bottom, right) #image("../../res/tiger.jpg", width: 60pt) +--- +// Does not fit to height of page. +#page(height: 60pt) +#image("../../res/rhino.png") + --- // Error: 8-29 file not found #image("path/does/not/exist")