From 63140a68f516c8b4616bf6c726d23a75b73e5713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 22 Feb 2020 00:05:25 +0100 Subject: [PATCH] sixel: calculate image height in (cell) rows --- render.c | 43 ++++++++++++++++++++++--------------------- sixel.c | 1 + terminal.c | 3 +-- terminal.h | 1 + 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/render.c b/render.c index cd96ba24..a4741b90 100644 --- a/render.c +++ b/render.c @@ -520,30 +520,23 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, } static void -render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *sixel) +render_sixel(struct terminal *term, pixman_image_t *pix, + const struct sixel *sixel) { int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1); int first_visible_row = -1; - if (view_end >= term->grid->view) { - /* Not wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + for (size_t i = sixel->pos.row; i < sixel->pos.row + sixel->rows; i++) { + int row = i & (term->grid->num_rows - 1); + + if (view_end >= term->grid->view) { + /* Not wrapped */ if (row >= term->grid->view && row <= view_end) { first_visible_row = i; break; } - } - } else { - /* Wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + } else { + /* Wrapped */ if (row >= term->grid->view || row <= view_end) { first_visible_row = i; break; @@ -554,20 +547,30 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six if (first_visible_row < 0) return; + /* First visible (0 based) row of the image */ const int first_img_row = first_visible_row - sixel->pos.row; + + /* Map first visible line to current grid view */ const int row = first_visible_row & (term->grid->num_rows - 1); const int view_aligned = (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); + /* Translate row/column to x/y pixel values */ const int x = term->x_margin + sixel->pos.col * term->cell_width; - const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); + const int y = max( + term->y_margin, term->y_margin + view_aligned * term->cell_height); + /* Width/height, in pixels - and don't touch the window margins */ const int width = min(sixel->width, term->width - x - term->x_margin); const int height = min( sixel->height - first_img_row * term->cell_height, term->height - y - term->y_margin); - //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); + /* Verify we're not stepping outside the grid */ + assert(x >= term->x_margin); + assert(y >= term->y_margin); + assert(x + width <= term->width - term->x_margin); + assert(y + height <= term->height - term->y_margin); pixman_image_composite( PIXMAN_OP_SRC, @@ -579,9 +582,7 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six x, y, width, height); - wl_surface_damage_buffer( - term->window->surface, - x, y, width, height); + wl_surface_damage_buffer(term->window->surface, x, y, width, height); } static void diff --git a/sixel.c b/sixel.c index f9c4c4cf..0cfb2a24 100644 --- a/sixel.c +++ b/sixel.c @@ -56,6 +56,7 @@ sixel_unhook(struct terminal *term) .data = term->sixel.image, .width = term->sixel.max_col, .height = term->sixel.row * 6, + .rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height, .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; diff --git a/terminal.c b/terminal.c index b80b9898..c6c349c3 100644 --- a/terminal.c +++ b/terminal.c @@ -1601,8 +1601,7 @@ term_scroll_reverse_partial(struct terminal *term, /* TODO: untested */ - int img_rows = (it->item.height + term->cell_height - 1) / term->cell_height; - int img_bottom_row = (it->item.pos.row + img_rows) & (term->grid->num_rows - 1); + int img_bottom_row = (it->item.pos.row + it->item.rows) & (term->grid->num_rows - 1); int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_bottom_row == new_row) { diff --git a/terminal.h b/terminal.h index 69b94624..bf113aa3 100644 --- a/terminal.h +++ b/terminal.h @@ -180,6 +180,7 @@ struct sixel { pixman_image_t *pix; int width; int height; + int rows; struct coord pos; };