From 294579fd313a98f0bfa9725bebe437cceac142eb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 23 Dec 2021 07:26:49 -0700 Subject: [PATCH] term: introduce right&bottom padding to ImageCell This helps us correctly set the size of the image cell for the case where we have a partial cell at the right/bottom edge of an image being mapped across cells. refs: #1270 --- term/src/terminalstate/image.rs | 20 ++++++++++++++----- term/src/terminalstate/iterm.rs | 4 ++-- term/src/terminalstate/kitty.rs | 4 ++-- term/src/terminalstate/sixel.rs | 4 ++-- termwiz/src/image.rs | 30 +++++++++++++++++++--------- wezterm-gui/src/termwindow/render.rs | 10 +++++----- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/term/src/terminalstate/image.rs b/term/src/terminalstate/image.rs index a29af2d58..921ba50fc 100644 --- a/term/src/terminalstate/image.rs +++ b/term/src/terminalstate/image.rs @@ -30,8 +30,8 @@ pub struct ImageAttachParams { /// When rendering in the cell, use this offset from the top left /// of the cell - pub display_offset_x: u32, - pub display_offset_y: u32, + pub padding_left: u16, + pub padding_top: u16, /// Plane on which to display the image pub z_index: i32, @@ -110,6 +110,7 @@ impl TerminalState { let mut remain_y = source_height as usize; for y in 0..height_in_cells { + let padding_bottom = cell_pixel_height.saturating_sub(remain_y) as u16; let y_delta = (remain_y.min(cell_pixel_height) as f32) / (source_height as f32); remain_y = remain_y.saturating_sub(cell_pixel_height); @@ -127,8 +128,15 @@ impl TerminalState { ); let mut remain_x = source_width as usize; for x in 0..width_in_cells { + let padding_right = cell_pixel_width.saturating_sub(remain_x) as u16; let x_delta = (remain_x.min(cell_pixel_width) as f32) / (source_width as f32); - log::debug!("x_delta {}, y_delta {}", x_delta, y_delta); + log::debug!( + "x_delta {}, y_delta {}, padding_right={}, padding_bottom={}", + x_delta, + y_delta, + padding_right, + padding_bottom + ); remain_x = remain_x.saturating_sub(cell_pixel_width); let mut cell = self .screen() @@ -140,8 +148,10 @@ impl TerminalState { TextureCoordinate::new(xpos + x_delta, ypos + y_delta), params.data.clone(), params.z_index, - params.display_offset_x, - params.display_offset_y, + params.padding_left, + params.padding_top, + padding_right, + padding_bottom, params.image_id, params.placement_id, )); diff --git a/term/src/terminalstate/iterm.rs b/term/src/terminalstate/iterm.rs index 6d7cc398e..69bafb611 100644 --- a/term/src/terminalstate/iterm.rs +++ b/term/src/terminalstate/iterm.rs @@ -146,8 +146,8 @@ impl TerminalState { source_height: height as u32, source_origin_x: 0, source_origin_y: 0, - display_offset_x: 0, - display_offset_y: 0, + padding_left: 0, + padding_top: 0, z_index: 0, columns: None, rows: None, diff --git a/term/src/terminalstate/kitty.rs b/term/src/terminalstate/kitty.rs index 8a89e9ae1..e83567fb8 100644 --- a/term/src/terminalstate/kitty.rs +++ b/term/src/terminalstate/kitty.rs @@ -117,8 +117,8 @@ impl TerminalState { source_height: placement.h.unwrap_or(image_height), source_origin_x: placement.x.unwrap_or(0), source_origin_y: placement.y.unwrap_or(0), - display_offset_x: placement.x_offset.unwrap_or(0), - display_offset_y: placement.y_offset.unwrap_or(0), + padding_left: placement.x_offset.unwrap_or(0) as u16, + padding_top: placement.y_offset.unwrap_or(0) as u16, data: img, style: ImageAttachStyle::Kitty, z_index: placement.z_index.unwrap_or(0), diff --git a/term/src/terminalstate/sixel.rs b/term/src/terminalstate/sixel.rs index 8c9276fe7..db9f44444 100644 --- a/term/src/terminalstate/sixel.rs +++ b/term/src/terminalstate/sixel.rs @@ -116,8 +116,8 @@ impl TerminalState { columns: None, source_origin_x: 0, source_origin_y: 0, - display_offset_x: 0, - display_offset_y: 0, + padding_left: 0, + padding_top: 0, data: image_data, style: ImageAttachStyle::Sixel, z_index: 0, diff --git a/termwiz/src/image.rs b/termwiz/src/image.rs index 244c388af..136654065 100644 --- a/termwiz/src/image.rs +++ b/termwiz/src/image.rs @@ -87,8 +87,10 @@ pub struct ImageCell { z_index: i32, /// When rendering in the cell, use this offset from the top left /// of the cell - display_offset_x: u32, - display_offset_y: u32, + padding_left: u16, + padding_top: u16, + padding_right: u16, + padding_bottom: u16, image_id: Option, placement_id: Option, @@ -100,7 +102,7 @@ impl ImageCell { bottom_right: TextureCoordinate, data: Arc, ) -> Self { - Self::with_z_index(top_left, bottom_right, data, 0, 0, 0, None, None) + Self::with_z_index(top_left, bottom_right, data, 0, 0, 0, 0, 0, None, None) } pub fn with_z_index( @@ -108,8 +110,10 @@ impl ImageCell { bottom_right: TextureCoordinate, data: Arc, z_index: i32, - display_offset_x: u32, - display_offset_y: u32, + padding_left: u16, + padding_top: u16, + padding_right: u16, + padding_bottom: u16, image_id: Option, placement_id: Option, ) -> Self { @@ -118,8 +122,10 @@ impl ImageCell { bottom_right, data, z_index, - display_offset_x, - display_offset_y, + padding_left, + padding_top, + padding_right, + padding_bottom, image_id, placement_id, } @@ -153,8 +159,14 @@ impl ImageCell { self.z_index } - pub fn display_offset(&self) -> (u32, u32) { - (self.display_offset_x, self.display_offset_y) + /// Returns padding (left, top, right, bottom) + pub fn padding(&self) -> (u16, u16, u16, u16) { + ( + self.padding_left, + self.padding_top, + self.padding_right, + self.padding_bottom, + ) } } diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index db2744efd..25f8630ae 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -2420,14 +2420,14 @@ impl super::TermWindow { + params.left_pixel_x + (cell_idx as f32 * cell_width); - let (offset_x, offset_y) = image.display_offset(); + let (padding_left, padding_top, padding_right, padding_bottom) = image.padding(); quad.set_position(pos_x, pos_y, pos_x + cell_width, pos_y + cell_height); quad.set_texture_adjust( - offset_x as f32, - offset_y as f32, - offset_x as f32, - offset_y as f32, + padding_left as f32, + padding_top as f32, + padding_left as f32 - padding_right as f32, + padding_top as f32 - padding_bottom as f32, ); quad.set_hsv(hsv); quad.set_fg_color(glyph_color);