Add cropped background image mode

This commit is contained in:
yunusey
2025-01-31 22:24:13 -05:00
parent e403e31b32
commit 0f704e0b02
2 changed files with 33 additions and 17 deletions

View File

@ -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.
@ -6330,12 +6332,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

View File

@ -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;