From 82ab44826cf426d610a268dd07fccdd87583a7fc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Apr 2024 12:28:35 +0530 Subject: [PATCH] Graphics: Fix aspect ratio of images not being preserved when only a single dimension of the destination rectangle is specified Fixes #7380 --- docs/changelog.rst | 3 +++ kitty/graphics.c | 25 +++++++++++++++++++------ kitty_tests/graphics.py | 16 ++++++++++------ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 42cd49de3..b6c981fcb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -55,6 +55,9 @@ Detailed list of changes - :opt:`paste_actions`: Fix ``replace-newline`` not working with ``confirm`` (:iss:`7374`) +- Graphics: Fix aspect ratio of images not being preserved when only a single + dimension of the destination rectangle is specified (:iss:`7380`) + 0.34.1 [2024-04-19] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/graphics.c b/kitty/graphics.c index 1e7823e58..44911aca8 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -1220,13 +1220,26 @@ grman_update_layers(GraphicsManager *self, unsigned int scrolled_by, float scree } } r.top = y0 - start_row * dy - dy * (float)ref->cell_y_offset / (float)cell.height; - if (ref->num_rows > 0) r.bottom = y0 - (start_row + (int32_t)ref->num_rows) * dy; - else r.bottom = r.top - screen_height * (float)ref->src_height / screen_height_px; - if (r.top <= screen_bottom || r.bottom >= screen_top) continue; // not visible - r.left = screen_left + start_column * dx + dx * (float)ref->cell_x_offset / (float) cell.width; - if (ref->num_cols > 0) r.right = screen_left + (start_column + (int32_t)ref->num_cols) * dx; - else r.right = r.left + screen_width * (float)ref->src_width / screen_width_px; + + int32_t nr = ref->num_rows, nc = ref->num_cols; + if (nr) { + r.bottom = y0 - (start_row + nr) * dy; + if (nc) r.right = screen_left + (start_column + nc) * dx; + else { + double height_px = ((r.top - r.bottom) / screen_height) * screen_height_px; + double width_px = height_px * ref->src_width / (double) ref->src_height; + r.right = r.left + (float)((width_px / screen_width_px) * screen_width); + } + } else { + if (nc) r.right = screen_left + (start_column + nc) * dx; + else r.right = r.left + screen_width * (float)ref->src_width / screen_width_px; + double width_px = ((r.right - r.left) / screen_width) * screen_width_px; + double height_px = width_px * ref->src_height / (double)ref->src_width; + r.bottom = r.top - (float)((height_px / screen_height_px) * screen_height); + } + + if (r.top <= screen_bottom || r.bottom >= screen_top) continue; // not visible if (ref->z_index < ((int32_t)INT32_MIN/2)) self->num_of_below_refs++; diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index becf1aa55..728dce890 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -555,17 +555,21 @@ class TestGraphics(BaseTest): rect_eq(l0[0]['dest_rect'], -1, 1, -1 + dx, 1 - dy) self.ae(l0[0]['group_count'], 1) self.ae(s.cursor.x, 1), self.ae(s.cursor.y, 0) - iid, (code, idstr) = put_ref(s, num_cols=s.columns, x_off=2, y_off=1, width=3, height=5, cell_x_off=3, cell_y_off=1, z=-1, placement_id=17) + 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, + cell_x_off=3, cell_y_off=1, z=-1, placement_id=17) self.ae(idstr, f'i={iid},p=17') l2 = layers(s) self.ae(len(l2), 2) + self.ae(l2[1], l0[0]) rect_eq(l2[0]['src_rect'], 2 / 10, 1 / 20, (2 + 3) / 10, (1 + 5)/20) - left, top = -1 + dx + 3 * dx / cw, 1 - 1 * dy / ch - rect_eq(l2[0]['dest_rect'], left, top, -1 + (1 + s.columns) * dx, top - dy * 5 / ch) - rect_eq(l2[1]['src_rect'], 0, 0, 1, 1) - rect_eq(l2[1]['dest_rect'], -1, 1, -1 + dx, 1 - dy) self.ae(l2[0]['group_count'], 2) - self.ae(l2[1]['group_count'], 1) + 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 + 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)