From abf5fbf0ef0b383b27c84251d7ba71a99844c2e0 Mon Sep 17 00:00:00 2001 From: yunusey Date: Fri, 31 Jan 2025 22:24:13 -0500 Subject: [PATCH] Add cropped background image mode --- src/config/Config.zig | 17 +++++++++------ src/renderer/shaders/bgimage.v.glsl | 33 ++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 6115d21fd5..277a181000 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -461,7 +461,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. @@ -5922,12 +5924,13 @@ pub const TextBlending = 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/bgimage.v.glsl b/src/renderer/shaders/bgimage.v.glsl index 0834b7c297..f83096ecd1 100644 --- a/src/renderer/shaders/bgimage.v.glsl +++ b/src/renderer/shaders/bgimage.v.glsl @@ -5,12 +5,13 @@ // NOTE: this must be kept in sync with the BackgroundImageMode const uint MODE_ZOOMED = 0u; const uint MODE_STRETCHED = 1u; -const uint MODE_TILED = 2u; -const uint MODE_CENTERED = 3u; -const uint MODE_UPPER_LEFT = 4u; -const uint MODE_UPPER_RIGHT = 5u; -const uint MODE_LOWER_LEFT = 6u; -const uint MODE_LOWER_RIGHT = 7u; +const uint MODE_CROPPED = 2u; +const uint MODE_TILED = 3u; +const uint MODE_CENTERED = 4u; +const uint MODE_UPPER_LEFT = 5u; +const uint MODE_UPPER_RIGHT = 6u; +const uint MODE_LOWER_LEFT = 7u; +const uint MODE_LOWER_RIGHT = 8u; layout (location = 0) in vec2 terminal_size; layout (location = 1) in uint mode; @@ -32,13 +33,15 @@ void main() { // Handles the scale of the image relative to the terminal size vec2 scale = vec2(1.0, 1.0); + // Calculate the aspect ratio of the terminal and the image + vec2 aspect_ratio = vec2( + terminal_size.x / terminal_size.y, + image_size.x / image_size.y + ); + switch (mode) { case MODE_ZOOMED: // If zoomed, we want to scale the image to fit the terminal - vec2 aspect_ratio = vec2( - terminal_size.x / terminal_size.y, - image_size.x / image_size.y - ); if (aspect_ratio.x > aspect_ratio.y) { scale.x = aspect_ratio.y / aspect_ratio.x; } @@ -46,6 +49,15 @@ void main() { scale.y = aspect_ratio.x / aspect_ratio.y; } break; + case MODE_CROPPED: + // If cropped, we want to scale the image to fit the terminal + 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: @@ -67,6 +79,7 @@ void main() { switch (mode) { case MODE_ZOOMED: case MODE_STRETCHED: + case MODE_CROPPED: case MODE_TILED: case MODE_CENTERED: offset = (terminal_size * (1.0 - scale)) / 2.0;