From 323119a6451445119c8e314e6bd18c19c7db9682 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 3 Oct 2020 23:00:34 +0200 Subject: grid: reflow: re-insert sixels *after* new grid offset has been set Also make sure to destroy sixels that are too big to fit in the scrollback. Fixes issues with the sixel list not being sorted correctly. --- grid.c | 91 +++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 40 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index a79b4493..84ee5313 100644 --- a/grid.c +++ b/grid.c @@ -83,9 +83,10 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, * at the output that is *oldest* */ int offset = grid->offset + old_screen_rows; - tll(struct sixel) old_sixels = tll_init(); + tll(struct sixel) untranslated_sixels = tll_init(); + tll(struct sixel) translated_sixels = tll_init(); tll_foreach(grid->sixel_images, it) - tll_push_back(old_sixels, it->item); + tll_push_back(untranslated_sixels, it->item); tll_free(grid->sixel_images); /* Turn cursor coordinates into grid absolute coordinates */ @@ -108,7 +109,6 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, for (size_t i = 0; i < tracking_points_count; i++) tll_push_back(tracking_points, _tracking_points[i]); - /* * Walk the old grid */ @@ -122,7 +122,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, continue; /* Map sixels on current "old" row to current "new row" */ - tll_foreach(old_sixels, it) { + tll_foreach(untranslated_sixels, it) { if (it->item.pos.row != old_row_idx) continue; @@ -131,47 +131,17 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Make sure it doesn't cross the wrap-around after being re-based */ int end = (sixel.pos.row + sixel.rows - 1) & (new_rows - 1); + if (end < sixel.pos.row) { /* TODO: split instead of destroying */ sixel_destroy(&it->item); } else { - - /* Insert sixel into the *sorted* list. */ - - /* Based on rebase_row() in sixel.c */ - /* Uses 'old' offset to ensure old sixels are treated as such */ -#define rebase_row(t, row) \ - (((row) - (grid->offset + new_screen_rows) + new_rows) & (new_rows - 1)) - - int end_row = rebase_row(term, sixel.pos.row + sixel.rows - 1); - - /* - * TODO: this is basically sixel_insert(), except we - * cannot use it since: - * - * a) we don't have a 'term' reference - * b) the grid hasn't been fully * updated yet - * (e.g. grid->num_rows is invalid etc). - */ - - bool inserted = false; - tll_foreach(grid->sixel_images, it2) { - const struct sixel *s = &it2->item; - if (rebase_row(term, s->pos.row + s->rows - 1) < end_row) { - tll_insert_before(grid->sixel_images, it2, sixel); - inserted = true; - break; - } - } - - if (!inserted) - tll_push_back(grid->sixel_images, sixel); - + /* Insert sixel into the translated, but *unsorted* list. */ + tll_push_back(translated_sixels, sixel); } /* Sixel has been either re-mapped, or destroyed */ - tll_remove(old_sixels, it); -#undef rebase_row + tll_remove(untranslated_sixels, it); } #define line_wrap() \ @@ -372,9 +342,50 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, grid->saved_cursor.lcf = false; /* Free sixels we failed to "map" to the new grid */ - tll_foreach(old_sixels, it) + tll_foreach(untranslated_sixels, it) sixel_destroy(&it->item); - tll_free(old_sixels); + tll_free(untranslated_sixels); + + /* Based on rebase_row() in sixel.c */ + /* Uses 'old' offset to ensure old sixels are treated as such */ +#define rebase_row(t, row) \ + (((row) - (grid->offset + new_screen_rows) + new_rows) & (new_rows - 1)) + + /* Re-add translated sixels, sorted according to the new grid offset */ + tll_foreach(translated_sixels, it) { + int end_row = rebase_row(term, it->item.pos.row + it->item.rows - 1); + + /* + * TODO: this is basically sixel_insert(), except we + * cannot use it since: + * + * a) we don't have a 'term' reference + * b) the grid hasn't been fully * updated yet + * (e.g. grid->num_rows is invalid etc). + */ + + if (it->item.rows >= new_rows) { + sixel_destroy(&it->item); + continue; + } + + assert(it->item.rows < new_rows); + + bool inserted = false; + tll_foreach(grid->sixel_images, it2) { + const struct sixel *s = &it2->item; + if (rebase_row(term, s->pos.row + s->rows - 1) < end_row) { + tll_insert_before(grid->sixel_images, it2, it->item); + inserted = true; + break; + } + } + + if (!inserted) + tll_push_back(grid->sixel_images, it->item); + } + tll_free(translated_sixels); +#undef rebase_row tll_free(tracking_points); } -- cgit v1.3 From dbfc636ade8bd59a4a5af4531c19af8a163e8c09 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 4 Oct 2020 13:12:44 +0200 Subject: sixel: implement reflow Move sixel reflow from grid_reflow() to sixel_reflow(). This lets us use internal sixel functions to update the sixels. Note that grid_reflow() still needs to remap the sixelss coordinates. --- grid.c | 55 +------------------------------------------------------ sixel.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ sixel.h | 2 ++ 3 files changed, 51 insertions(+), 54 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 84ee5313..a779c1b3 100644 --- a/grid.c +++ b/grid.c @@ -84,7 +84,6 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, int offset = grid->offset + old_screen_rows; tll(struct sixel) untranslated_sixels = tll_init(); - tll(struct sixel) translated_sixels = tll_init(); tll_foreach(grid->sixel_images, it) tll_push_back(untranslated_sixels, it->item); tll_free(grid->sixel_images); @@ -129,18 +128,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, struct sixel sixel = it->item; sixel.pos.row = new_row_idx; - /* Make sure it doesn't cross the wrap-around after being re-based */ - int end = (sixel.pos.row + sixel.rows - 1) & (new_rows - 1); - - if (end < sixel.pos.row) { - /* TODO: split instead of destroying */ - sixel_destroy(&it->item); - } else { - /* Insert sixel into the translated, but *unsorted* list. */ - tll_push_back(translated_sixels, sixel); - } - - /* Sixel has been either re-mapped, or destroyed */ + tll_push_back(grid->sixel_images, sixel); tll_remove(untranslated_sixels, it); } @@ -346,46 +334,5 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, sixel_destroy(&it->item); tll_free(untranslated_sixels); - /* Based on rebase_row() in sixel.c */ - /* Uses 'old' offset to ensure old sixels are treated as such */ -#define rebase_row(t, row) \ - (((row) - (grid->offset + new_screen_rows) + new_rows) & (new_rows - 1)) - - /* Re-add translated sixels, sorted according to the new grid offset */ - tll_foreach(translated_sixels, it) { - int end_row = rebase_row(term, it->item.pos.row + it->item.rows - 1); - - /* - * TODO: this is basically sixel_insert(), except we - * cannot use it since: - * - * a) we don't have a 'term' reference - * b) the grid hasn't been fully * updated yet - * (e.g. grid->num_rows is invalid etc). - */ - - if (it->item.rows >= new_rows) { - sixel_destroy(&it->item); - continue; - } - - assert(it->item.rows < new_rows); - - bool inserted = false; - tll_foreach(grid->sixel_images, it2) { - const struct sixel *s = &it2->item; - if (rebase_row(term, s->pos.row + s->rows - 1) < end_row) { - tll_insert_before(grid->sixel_images, it2, it->item); - inserted = true; - break; - } - } - - if (!inserted) - tll_push_back(grid->sixel_images, it->item); - } - tll_free(translated_sixels); -#undef rebase_row - tll_free(tracking_points); } diff --git a/sixel.c b/sixel.c index 84b889fd..52eaa406 100644 --- a/sixel.c +++ b/sixel.c @@ -508,6 +508,54 @@ sixel_cell_size_changed(struct terminal *term) term->grid = g; } +void +sixel_reflow(struct terminal *term) +{ + struct grid *g = term->grid; + + for (size_t i = 0; i < 2; i++) { + struct grid *grid = i == 0 ? &term->normal : &term->alt; + + term->grid = grid; + + /* Need the “real” list to be empty from the beginning */ + tll(struct sixel) copy = tll_init(); + tll_foreach(grid->sixel_images, it) + tll_push_back(copy, it->item); + tll_free(grid->sixel_images); + + tll_rforeach(copy, it) { + struct sixel *six = &it->item; + int start = six->pos.row; + int end = (start + six->rows - 1) & (grid->num_rows - 1); + + if (end < start) { + /* Crosses scrollback wrap-around */ + /* TOOD: split image */ + sixel_destroy(six); + continue; + } + + if (six->rows > grid->num_rows) { + /* Image too large */ + /* TODO: keep bottom part? */ + sixel_destroy(six); + continue; + } + + /* Sixels that didn’t overlap may now do so, which isn’t + * allowed of course */ + _sixel_overwrite_by_rectangle( + term, six->pos.row, six->pos.col, six->rows, six->cols); + sixel_insert(term, it->item); + } + + tll_free(copy); + } + + term->grid = g; +} + void sixel_unhook(struct terminal *term) { diff --git a/sixel.h b/sixel.h index ac763bb0..6cd7324e 100644 --- a/sixel.h +++ b/sixel.h @@ -17,6 +17,8 @@ void sixel_scroll_up(struct terminal *term, int rows); void sixel_scroll_down(struct terminal *term, int rows); void sixel_cell_size_changed(struct terminal *term); +void sixel_reflow(struct terminal *term); + /* * Remove sixel data from the specified location. Used when printing * or erasing characters, and when emitting new sixel images, to -- cgit v1.3