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