Fix vertical image expansion

This commit is contained in:
Laurenz 2021-11-29 15:09:29 +01:00
parent cae60ea0cc
commit 67705e2038

View File

@ -46,14 +46,14 @@ impl Layout for ImageNode {
let px_ratio = pxw / pxh;
// Find out whether the image is wider or taller than the target size.
let current = regions.current;
let &Regions { current, expand, .. } = regions;
let current_ratio = current.x / current.y;
let wide = px_ratio > current_ratio;
// The space into which the image will be placed according to its fit.
let target = if regions.expand.x && regions.expand.y {
let target = if expand.x && expand.y {
current
} else if regions.expand.x || (wide && current.x.is_finite()) {
} else if expand.x || (!expand.y && wide && current.x.is_finite()) {
Size::new(current.x, current.y.min(current.x.safe_div(px_ratio)))
} else if current.y.is_finite() {
Size::new(current.x.min(current.y * px_ratio), current.y)
@ -63,7 +63,7 @@ impl Layout for ImageNode {
// The actual size of the fitted image.
let fitted = match self.fit {
ImageFit::Contain | ImageFit::Cover => {
ImageFit::Cover | ImageFit::Contain => {
if wide == (self.fit == ImageFit::Contain) {
Size::new(target.x, target.x / px_ratio)
} else {
@ -80,8 +80,8 @@ impl Layout for ImageNode {
frame.push(Point::zero(), Element::Image(self.id, fitted));
frame.resize(target, Align::CENTER_HORIZON);
// Create a clipping group if the fit mode is "cover".
if self.fit == ImageFit::Cover {
// Create a clipping group if only part of the image should be visible.
if self.fit == ImageFit::Cover && !target.fits(fitted) {
frame.clip();
}
@ -92,10 +92,10 @@ impl Layout for ImageNode {
/// How an image should adjust itself to a given area.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ImageFit {
/// The image should be fully contained in the area.
Contain,
/// The image should completely cover the area.
Cover,
/// The image should be fully contained in the area.
Contain,
/// The image should be stretched so that it exactly fills the area.
Stretch,
}
@ -104,15 +104,15 @@ castable! {
ImageFit,
Expected: "string",
Value::Str(string) => match string.as_str() {
"contain" => Self::Contain,
"cover" => Self::Cover,
"contain" => Self::Contain,
"stretch" => Self::Stretch,
_ => Err(r#"expected "contain", "cover" or "stretch""#)?,
_ => Err(r#"expected "cover", "contain" or "stretch""#)?,
},
}
impl Default for ImageFit {
fn default() -> Self {
Self::Contain
Self::Cover
}
}