Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: setting width and height to Contain is not preserving aspect ratio of tall images #6554

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions wezterm-gui/src/termwindow/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ impl crate::TermWindow {

let pixel_width = self.dimensions.pixel_width as f32;
let pixel_height = self.dimensions.pixel_height as f32;
let pixel_aspect = pixel_width / pixel_height;

let tex_width = sprite.coords.width() as f32;
let tex_height = sprite.coords.height() as f32;
let aspect = tex_width as f32 / tex_height as f32;

let scale_width = pixel_width / tex_width as f32;
let scale_height = pixel_height / tex_height as f32;

let h_context = DimensionContext {
dpi: self.dimensions.dpi as f32,
Expand All @@ -457,47 +457,26 @@ impl crate::TermWindow {

// log::info!("tex {tex_width}x{tex_height} aspect={aspect}");

// Compute the largest aspect-preserved size that will fill the space
let (max_aspect_width, max_aspect_height) = if aspect >= 1.0 {
// Width is the longest side
let target_height = pixel_width / aspect;
if target_height > pixel_height {
(
(pixel_width * pixel_height / target_height).floor(),
pixel_height,
)
} else {
(pixel_width, target_height)
}
} else {
// Height is the longest side
let target_width = pixel_height / aspect;
if target_width > pixel_width {
(
pixel_width,
(pixel_height * pixel_width / target_width).floor(),
)
} else {
(target_width, pixel_height)
}
};

// Compute the smallest aspect-preserved size that will fit the space
let (min_aspect_width, min_aspect_height) = if pixel_aspect > aspect {
(pixel_width, (pixel_width / aspect).floor())
} else {
((pixel_height * aspect).floor(), pixel_height)
let (min_aspect_width, min_aspect_height) = {
let scale = scale_width.min(scale_height);
(tex_width * scale, tex_height * scale)
};
// Compute the largest aspect-preserved size that will fill the space
let (max_aspect_width, max_aspect_height) = {
let scale = scale_width.max(scale_height);
(tex_width * scale, tex_height * scale)
};

let width = match layer.def.width {
BackgroundSize::Contain => max_aspect_width as f32,
BackgroundSize::Cover => min_aspect_width as f32,
BackgroundSize::Contain => min_aspect_width as f32,
BackgroundSize::Cover => max_aspect_width as f32,
BackgroundSize::Dimension(n) => n.evaluate_as_pixels(h_context),
};

let height = match layer.def.height {
BackgroundSize::Contain => max_aspect_height as f32,
BackgroundSize::Cover => min_aspect_height as f32,
BackgroundSize::Contain => min_aspect_height as f32,
BackgroundSize::Cover => max_aspect_height as f32,
BackgroundSize::Dimension(n) => n.evaluate_as_pixels(v_context),
};

Expand Down