From 05a30e6fda4baa57aad5adfb8c5fb970b08c4970 Mon Sep 17 00:00:00 2001 From: Rohit-Bevinahally Date: Sun, 23 Feb 2025 19:30:41 +0530 Subject: [PATCH] Add cropped mode --- src/config/Config.zig | 17 ++++++++++------- src/renderer/shaders/cell.metal | 33 +++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 880105634..6fa895702 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -470,7 +470,9 @@ foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF }, /// Valid values are: /// /// * `zoomed` - Image is scaled to fit the window, preserving aspect ratio. -/// * `scaled` - Image is scaled to fill the window, not preserving aspect ratio. +/// * `stretched` - Image is stretched to fill the window, not preserving aspect ratio. +/// * `cropped` - Image is centered in the window, preserving the aspect ratio +/// but cropping the image to fill the window, as needed. /// * `tiled` - Image is repeated horizontally and vertically to fill the window. /// * `centered` - Image is centered in the window and displayed 1-to-1 pixel /// scale, preserving both the aspect ratio and the image size. @@ -6634,12 +6636,13 @@ pub const AlphaBlending = enum { pub const BackgroundImageMode = enum(u8) { zoomed = 0, stretched = 1, - tiled = 2, - centered = 3, - upper_left = 4, - upper_right = 5, - lower_left = 6, - lower_right = 7, + cropped = 2, + tiled = 3, + centered = 4, + upper_left = 5, + upper_right = 6, + lower_left = 7, + lower_right = 8, }; /// See freetype-load-flag diff --git a/src/renderer/shaders/cell.metal b/src/renderer/shaders/cell.metal index 9cef3397c..06f61cff0 100644 --- a/src/renderer/shaders/cell.metal +++ b/src/renderer/shaders/cell.metal @@ -692,12 +692,13 @@ fragment float4 image_fragment( enum BgImageMode : uint8_t { MODE_ZOOMED = 0u, MODE_STRETCHED = 1u, - MODE_TILED = 2u, - MODE_CENTERED = 3u, - MODE_UPPER_LEFT = 4u, - MODE_UPPER_RIGHT = 5u, - MODE_LOWER_LEFT = 6u, - MODE_LOWER_RIGHT = 7u, + MODE_CROPPED = 2u, + MODE_TILED = 3u, + MODE_CENTERED = 4u, + MODE_UPPER_LEFT = 5u, + MODE_UPPER_RIGHT = 6u, + MODE_LOWER_LEFT = 7u, + MODE_LOWER_RIGHT = 8u, }; struct BgImageVertexIn { @@ -729,13 +730,15 @@ vertex BgImageVertexOut bg_image_vertex( // Handles the scale of the image relative to the terminal size float2 scale = float2(1.0, 1.0); + // Calculate the aspect ratio of the terminal and the image + float2 aspect_ratio = float2( + in.terminal_size.x / in.terminal_size.y, + image_size.x / image_size.y + ); + switch (in.mode) { case MODE_ZOOMED: { // Scale to fit the terminal size - float2 aspect_ratio = float2( - in.terminal_size.x / in.terminal_size.y, - image_size.x / image_size.y - ); if (aspect_ratio.x > aspect_ratio.y) { scale.x = aspect_ratio.y / aspect_ratio.x; } else { @@ -743,6 +746,15 @@ vertex BgImageVertexOut bg_image_vertex( } break; } + case MODE_CROPPED: { + // Scale to fit the terminal size + if (aspect_ratio.x < aspect_ratio.y) { + scale.x = aspect_ratio.y / aspect_ratio.x; + } else { + scale.y = aspect_ratio.x / aspect_ratio.y; + } + break; + } case MODE_CENTERED: case MODE_UPPER_LEFT: case MODE_UPPER_RIGHT: @@ -764,6 +776,7 @@ vertex BgImageVertexOut bg_image_vertex( float2 offset = float2(0.0, 0.0); switch (in.mode) { case MODE_ZOOMED: + case MODE_CROPPED: case MODE_STRETCHED: case MODE_TILED: case MODE_CENTERED: