Update comments

This commit is contained in:
yunusey
2025-06-01 19:22:31 -04:00
parent 624a8c812c
commit 40b591ce34
2 changed files with 22 additions and 40 deletions

View File

@ -328,7 +328,7 @@ pub const DerivedConfig = struct {
const custom_shaders = try config.@"custom-shader".clone(alloc); const custom_shaders = try config.@"custom-shader".clone(alloc);
// Copy our background image // Copy our background image
const background_image = try config.@"background-image".?.clone(alloc); const background_image = if (config.@"background-image") |v| try v.clone(alloc) else null;
// Copy our font features // Copy our font features
const font_features = try config.@"font-feature".clone(alloc); const font_features = try config.@"font-feature".clone(alloc);
@ -835,21 +835,17 @@ pub fn updateFrame(
if (self.current_background_image == null) { if (self.current_background_image == null) {
if (self.background_image) |background_image| { if (self.background_image) |background_image| {
const img_path, const optional = switch (background_image) { const img_path, const required = switch (background_image) {
.optional => |path| .{ path, true }, .optional => |path| .{ path, false },
.required => |path| .{ path, false }, .required => |path| .{ path, true },
}; };
if (single_threaded_draw) self.draw_mutex.lock(); if (single_threaded_draw) self.draw_mutex.lock();
defer if (single_threaded_draw) self.draw_mutex.unlock(); defer if (single_threaded_draw) self.draw_mutex.unlock();
self.prepBackgroundImage(img_path) catch |err| { self.prepBackgroundImage(img_path) catch |err| switch (err) {
switch (err) { error.InvalidData => if (required) {
error.InvalidData => { log.err("error loading background image {s}: {}", .{ img_path, err });
if (!optional) { },
log.err("error loading background image {s}: {}", .{ img_path, err }); else => return err,
}
},
else => return err,
}
}; };
} }
} }
@ -1225,7 +1221,7 @@ pub fn prepBackgroundImage(self: *OpenGL, path: []const u8) !void {
const file_content = try self.readImageContent(path); const file_content = try self.readImageContent(path);
defer self.alloc.free(file_content); defer self.alloc.free(file_content);
// Decode the png (currently, we only support png) // Decode the image
const decoded_image: wuffs.ImageData = blk: { const decoded_image: wuffs.ImageData = blk: {
// Extract the file extension // Extract the file extension
const ext = std.fs.path.extension(path); const ext = std.fs.path.extension(path);
@ -1265,29 +1261,16 @@ pub fn readImageContent(self: *OpenGL, path: []const u8) ![]u8 {
}; };
defer file.close(); defer file.close();
// File must be a regular file
if (file.stat()) |stat| {
if (stat.kind != .file) {
log.warn("file is not a regular file kind={}", .{stat.kind});
return error.InvalidData;
}
} else |err| {
log.warn("failed to stat file: {}", .{err});
return error.InvalidData;
}
var buf_reader = std.io.bufferedReader(file.reader()); var buf_reader = std.io.bufferedReader(file.reader());
const reader = buf_reader.reader(); const reader = buf_reader.reader();
// Read the file // Read the file
var managed = std.ArrayList(u8).init(self.alloc); const image_content = reader.readAllAlloc(self.alloc, max_image_size) catch |err| {
errdefer managed.deinit();
reader.readAllArrayList(&managed, max_image_size) catch |err| {
log.warn("failed to read file: {}", .{err}); log.warn("failed to read file: {}", .{err});
return error.InvalidData; return error.InvalidData;
}; };
return managed.toOwnedSlice(); return image_content;
} }
/// rebuildCells rebuilds all the GPU cells from our CPU state. This is a /// rebuildCells rebuilds all the GPU cells from our CPU state. This is a
@ -2438,8 +2421,8 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
} }
// Check if we need to update our current background image // Check if we need to update our current background image
if (self.current_background_image) |current_background_image| { if (self.current_background_image) |*current_background_image| {
switch (current_background_image) { switch (current_background_image.*) {
.ready => {}, .ready => {},
.pending_gray, .pending_gray,
@ -2450,13 +2433,13 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
.replace_gray_alpha, .replace_gray_alpha,
.replace_rgb, .replace_rgb,
.replace_rgba, .replace_rgba,
=> try self.current_background_image.?.upload(self.alloc), => try current_background_image.upload(self.alloc),
.unload_pending, .unload_pending,
.unload_replace, .unload_replace,
.unload_ready, .unload_ready,
=> { => {
self.current_background_image.?.deinit(self.alloc); current_background_image.deinit(self.alloc);
self.current_background_image = null; self.current_background_image = null;
}, },
} }

View File

@ -38,7 +38,7 @@ void main() {
switch (mode) { switch (mode) {
case MODE_CONTAIN: case MODE_CONTAIN:
// If zoomed, we want to scale the image to fit the terminal // If contained, we want to scale the image to fit the terminal
if (aspect_ratio.x > aspect_ratio.y) { if (aspect_ratio.x > aspect_ratio.y) {
scale.x = aspect_ratio.y / aspect_ratio.x; scale.x = aspect_ratio.y / aspect_ratio.x;
} }
@ -47,7 +47,7 @@ void main() {
} }
break; break;
case MODE_COVER: case MODE_COVER:
// If cropped, we want to scale the image to fit the terminal // If covered, we want to scale the image to fit the terminal
if (aspect_ratio.x < aspect_ratio.y) { if (aspect_ratio.x < aspect_ratio.y) {
scale.x = aspect_ratio.y / aspect_ratio.x; scale.x = aspect_ratio.y / aspect_ratio.x;
} }
@ -56,23 +56,22 @@ void main() {
} }
break; break;
case MODE_NONE: case MODE_NONE:
// If none, the final scale of the image should match the actual // If none, the final scale of the image should match the actual size of the image
// size of the image and should be centered
scale.x = image_size.x / terminal_size.x; scale.x = image_size.x / terminal_size.x;
scale.y = image_size.y / terminal_size.y; scale.y = image_size.y / terminal_size.y;
break; break;
case MODE_FILL: case MODE_FILL:
case MODE_TILED: case MODE_TILED:
// We don't need to do anything for stretched or tiled // We don't need to do anything for fill or tiled
break; break;
} }
vec2 final_image_size = terminal_size * position * scale; vec2 final_image_size = terminal_size * position * scale;
vec2 offset = vec2(0.0, 0.0);
uint y_pos = position_index / 3u; // 0 = top, 1 = center, 2 = bottom uint y_pos = position_index / 3u; // 0 = top, 1 = center, 2 = bottom
uint x_pos = position_index % 3u; // 0 = left, 1 = center, 2 = right uint x_pos = position_index % 3u; // 0 = left, 1 = center, 2 = right
offset = ((terminal_size * (1.0 - scale)) / 2.0) * vec2(x_pos, y_pos); vec2 offset = ((terminal_size * (1.0 - scale)) / 2.0) * vec2(x_pos, y_pos);
gl_Position = projection * vec4(final_image_size.xy + offset, 0.0, 1.0); gl_Position = projection * vec4(final_image_size.xy + offset, 0.0, 1.0);
tex_coord = position; tex_coord = position;
if (mode == MODE_TILED) { if (mode == MODE_TILED) {