Skip to content

Commit

Permalink
Add cropped background image mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusey committed Feb 1, 2025
1 parent 5d583cb commit abf5fbf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
33 changes: 23 additions & 10 deletions src/renderer/shaders/bgimage.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,20 +33,31 @@ 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;
}
else {
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:
Expand All @@ -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;
Expand Down

0 comments on commit abf5fbf

Please sign in to comment.