diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-10-05 20:05:15 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-10-05 20:05:15 +0200 |
| commit | b69c0355ecda6a2da275cac42fcb0dfa9e31581b (patch) | |
| tree | 7cc9ee9de4ce834f4b123459acbdd65fc65d8713 | |
| parent | 3d0dcbea182f6a81539c12c9d5156cf0f6863b67 (diff) | |
Don't fit images to remaining height
Makes them really small if there's only little space left ...
| -rw-r--r-- | src/layout/image.rs | 23 | ||||
| -rw-r--r-- | tests/ref/elements/image.png | bin | 219541 -> 209747 bytes | |||
| -rw-r--r-- | tests/typ/elements/image.typ | 14 |
3 files changed, 13 insertions, 24 deletions
diff --git a/src/layout/image.rs b/src/layout/image.rs index 94f57167..81c48b3e 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 Binary files differindex 6848726f..943d43b1 100644 --- a/tests/ref/elements/image.png +++ b/tests/ref/elements/image.png diff --git a/tests/typ/elements/image.typ b/tests/typ/elements/image.typ index 6359c4bb..c0e6a3ef 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) @@ -36,6 +27,11 @@ #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") |
