From beb42d571b4d02ea320e214b64b561d3c4c8431d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 28 May 2024 21:05:56 +0530 Subject: [PATCH] Forgot to use aspect ratio calculation when only one of r/c is specified for determining cursor position and image cell overlaps. Fixes #7479 --- docs/changelog.rst | 2 ++ kitty/graphics.c | 24 ++++++++++++++++++------ kitty_tests/graphics.py | 11 ++++++----- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 06c53b33a..ed84a431f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -59,6 +59,8 @@ Detailed list of changes - Fix a regression in the previous release causing an error when setting background_opacity to zero (:iss:`7483`) +- Image display: Fix cursor movement and image hit region incorrect for image placements that specify only a number of rows or columns to display in (:iss:`7479`) + 0.35.0 [2024-05-25] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/graphics.c b/kitty/graphics.c index 9786e0659..402fb326a 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -798,14 +798,26 @@ static void update_dest_rect(ImageRef *ref, uint32_t num_cols, uint32_t num_rows, CellPixelSize cell) { uint32_t t; if (num_cols == 0) { - t = (uint32_t)(ref->src_width + ref->cell_x_offset); - num_cols = t / cell.width; - if (t > num_cols * cell.width) num_cols += 1; + if (num_rows == 0) { + t = (uint32_t)(ref->src_width + ref->cell_x_offset); + num_cols = t / cell.width; + if (t > num_cols * cell.width) num_cols += 1; + } else { + double height_px = cell.height * num_rows + ref->cell_y_offset; + double width_px = height_px * ref->src_width / (double) ref->src_height; + num_cols = (uint32_t)ceil(width_px / cell.width); + } } if (num_rows == 0) { - t = (uint32_t)(ref->src_height + ref->cell_y_offset); - num_rows = t / cell.height; - if (t > num_rows * cell.height) num_rows += 1; + if (num_cols == 0) { + t = (uint32_t)(ref->src_height + ref->cell_y_offset); + num_rows = t / cell.height; + if (t > num_rows * cell.height) num_rows += 1; + } else { + double width_px = cell.width * num_cols + ref->cell_x_offset; + double height_px = width_px * ref->src_height / (double)ref->src_width; + num_rows = (uint32_t)ceil(height_px / cell.height); + } } ref->effective_num_rows = num_rows; ref->effective_num_cols = num_cols; diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 728dce890..5302bd046 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -548,7 +548,7 @@ class TestGraphics(BaseTest): def test_image_put(self): cw, ch = 10, 20 s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch) - self.ae(put_image(s, 10, 20)[1], 'OK') + self.ae(put_image(s, cw, ch)[1], 'OK') l0 = layers(s) self.ae(len(l0), 1) rect_eq(l0[0]['src_rect'], 0, 0, 1, 1) @@ -556,7 +556,7 @@ class TestGraphics(BaseTest): self.ae(l0[0]['group_count'], 1) self.ae(s.cursor.x, 1), self.ae(s.cursor.y, 0) src_width, src_height = 3, 5 - iid, (code, idstr) = put_ref(s, num_cols=s.columns, x_off=2, y_off=1, width=src_width, height=src_height, + iid, (code, idstr) = put_ref(s, num_cols=s.columns, num_lines=1, x_off=2, y_off=1, width=src_width, height=src_height, cell_x_off=3, cell_y_off=1, z=-1, placement_id=17) self.ae(idstr, f'i={iid},p=17') l2 = layers(s) @@ -566,15 +566,16 @@ class TestGraphics(BaseTest): self.ae(l2[0]['group_count'], 2) left, top = -1 + dx + 3 * dx / cw, 1 - 1 * dy / ch right = -1 + (1 + s.columns) * dx - width_px = ((right - left) / 2) * (cw * s.columns) - height_px = width_px * (src_height / src_width) - bottom = top - (height_px / (ch * s.lines)) * 2 + bottom = 1 - dy rect_eq(l2[0]['dest_rect'], left, top, right, bottom) self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 1) self.ae(put_image(s, 10, 20, cursor_movement=1)[1], 'OK') self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 1) s.reset() self.assertEqual(s.grman.disk_cache.total_size, 0) + self.ae(put_image(s, 2*cw, 2*ch, num_cols=3)[1], 'OK') + self.ae((s.cursor.x, s.cursor.y), (3, 2)) + rect_eq(layers(s)[0]['dest_rect'], -1, 1, -1 + 3 * dx, 1 - 3*dy) def test_image_layer_grouping(self): cw, ch = 10, 20