Don't fit images to remaining height

Makes them really small if there's only little space left ...
This commit is contained in:
Laurenz 2021-10-05 20:05:15 +02:00
parent 3d0dcbea18
commit b69c0355ec
3 changed files with 13 additions and 24 deletions

View File

@ -1,8 +1,6 @@
use super::*; use super::*;
use crate::image::ImageId; use crate::image::ImageId;
use ::image::GenericImageView;
/// An image node. /// An image node.
#[derive(Debug)] #[derive(Debug)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[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 width = self.width.map(|w| w.resolve(base.w));
let height = self.height.map(|w| w.resolve(base.h)); let height = self.height.map(|w| w.resolve(base.h));
let dimensions = ctx.images.get(self.id).buf.dimensions(); let img = ctx.images.get(self.id);
let pixel_width = dimensions.0 as f64; let pixel_size = Spec::new(img.width() as f64, img.height() as f64);
let pixel_height = dimensions.1 as f64; let pixel_ratio = pixel_size.x / pixel_size.y;
let pixel_ratio = pixel_width / pixel_height;
let size = match (width, height) { let size = match (width, height) {
(Some(width), Some(height)) => Size::new(width, height), (Some(width), Some(height)) => Size::new(width, height),
(Some(width), None) => Size::new(width, width / pixel_ratio), (Some(width), None) => Size::new(width, width / pixel_ratio),
(None, Some(height)) => Size::new(height * pixel_ratio, height), (None, Some(height)) => Size::new(height * pixel_ratio, height),
(None, None) => { (None, None) => {
constraints.exact = current.to_spec().map(Some); constraints.exact.x = Some(current.w);
if current.w.is_finite() {
let ratio = current.w / current.h;
if ratio < pixel_ratio && current.w.is_finite() {
Size::new(current.w, current.w / pixel_ratio) 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 { } else {
// Totally unbounded region, we have to make up something. // Totally unbounded region, we have to make up something,
Size::new(Length::pt(pixel_width), Length::pt(pixel_height)) // so it is 1pt per pixel.
pixel_size.map(Length::pt).to_size()
} }
} }
}; };

Binary file not shown.

Before

Width:  |  Height:  |  Size: 214 KiB

After

Width:  |  Height:  |  Size: 205 KiB

View File

@ -13,15 +13,6 @@
--- ---
// Test configuring the size and fitting behaviour of images. // 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. // Set width explicitly.
#image("../../res/rhino.png", width: 50pt) #image("../../res/rhino.png", width: 50pt)
@ -35,6 +26,11 @@
#align(bottom, right) #align(bottom, right)
#image("../../res/tiger.jpg", width: 60pt) #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 // Error: 8-29 file not found
#image("path/does/not/exist") #image("path/does/not/exist")