diff --git a/docs/changelog.rst b/docs/changelog.rst index fcad60fbd..74c081711 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -143,6 +143,16 @@ Detailed list of changes no longer opens saved sessions in the wrong OS Window or tab and no longer overwrites the session file with an empty one (:pull:`10851`, :pull:`10852`) +- Fix out of bounds memory accesses when a multi-line :doc:`text sizing + ` character is partially scrolled off the screen and a + combining char is then added to it or the window is resized + +- A hard reset (:code:`RIS`) now clears the alternate screen as well + +- Fix the lower rows of a multi-line :doc:`text sizing ` + character being lost when resizing if the text after it is wrapped onto its + first row + - Fix the scrolled back view jumping, and images, the cursor and the scrollbar getting out of sync with the text, when new output arrives (:pull:`10841`, :pull:`10847`) diff --git a/kitty/history.c b/kitty/history.c index 23cb6750c..9bbd3c116 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -382,9 +382,13 @@ historybuf_delete_newest_lines(HistoryBuf *self, index_type count) { CPUCell *cells = self->line->cpu_cells; self->count--; for (index_type x = 0; x < self->line->xnum; x++) { - CPUCell *c = cells + x; - if (c->is_multicell && c->y) { - for (index_type i = 0, pos = self->count; i < c->y && pos; i++, pos--) clear_multicell_cell(self, self->count - pos, x); + const CPUCell c = cells[x]; + if (c.is_multicell && c.y) { + // the rows above, stopping at the first that does not continue the char, as it may be partial or torn + for (index_type lnum = 0; lnum < c.y && lnum < self->count; lnum++) { + if (!continues_multicell_char(cpu_lineptr(self, index_of(self, lnum)) + x, &c, c.y - 1 - lnum)) break; + clear_multicell_cell(self, lnum, x); + } } } } @@ -404,7 +408,11 @@ historybuf_delete_lines(HistoryBuf *self, index_type num_newer, index_type count const CPUCell c = self->line->cpu_cells[x]; if (c.is_multicell && c.y) { const index_type limit = MIN(oldest_deleted + c.y + 1, self->count); - for (index_type lnum = oldest_deleted + 1; lnum < limit; lnum++) clear_multicell_cell(self, lnum, x); + for (index_type lnum = oldest_deleted + 1; lnum < limit; lnum++) { + // stop at the first row that does not continue the char, as it may be partial or torn + if (!continues_multicell_char(cpu_lineptr(self, index_of(self, lnum)) + x, &c, c.y - (lnum - oldest_deleted))) break; + clear_multicell_cell(self, lnum, x); + } init_line(self, index_of(self, oldest_deleted), self->line); } } diff --git a/kitty/line.c b/kitty/line.c index 2e5022364..a9437f123 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -1250,3 +1250,28 @@ copy_char(Line *self, PyObject *args) { COPY_CELL(self, src, to, dest); Py_RETURN_NONE; } + +bool +cells_have_multiline_chars(const CPUCell *cells, index_type count) { + // Returns true if any cell has scale > 1. Hot path, so treat the cells as an + // array of uint32 and AND with a mask selecting the bits of scale that are + // set only when scale >= 2. Blocks of BLOCK cells are a whole number of + // words with a repeating mask pattern, so the inner loop has contiguous + // loads the compiler vectorizes. The mask is derived from the CPUCell + // layout and is constant folded. + enum { WORDS_PER_CELL = sizeof(CPUCell) / sizeof(uint32_t), BLOCK = 8, BLOCK_WORDS = BLOCK * WORDS_PER_CELL }; + static_assert(sizeof(CPUCell) % sizeof(uint32_t) == 0, "CPUCell size must be a multiple of 4"); + const CPUCell mask_cell = {.scale = 6}; + uint32_t cell_mask[WORDS_PER_CELL], mask[BLOCK_WORDS]; + memcpy(cell_mask, &mask_cell, sizeof(cell_mask)); + for (unsigned i = 0; i < BLOCK_WORDS; i++) mask[i] = cell_mask[i % WORDS_PER_CELL]; + uint32_t acc = 0; + index_type x = 0; + for (; x + BLOCK <= count; x += BLOCK) { + uint32_t w[BLOCK_WORDS]; + memcpy(w, cells + x, sizeof(w)); + for (unsigned i = 0; i < BLOCK_WORDS; i++) acc |= w[i] & mask[i]; + } + for (; x < count; x++) acc |= cells[x].scale & 6u; + return acc != 0; +} diff --git a/kitty/line.h b/kitty/line.h index 0a3a3982e..5adedb62f 100644 --- a/kitty/line.h +++ b/kitty/line.h @@ -164,6 +164,15 @@ mcd_x_limit(const CPUCell *mcd) { return mcd->scale * mcd->width; } +static inline bool +continues_multicell_char(const CPUCell *c, const CPUCell *ref, index_type y) { + // Whether c is the cell in row y of the same multicell char as ref in the same column. + // Use this rather than trusting the y and scale of a cell when walking the rows of a + // multiline char, as the char may be partial or torn. + return c->is_multicell && c->y == y && c->x == ref->x && c->scale == ref->scale && c->width == ref->width && c->subscale_n == ref->subscale_n && + c->subscale_d == ref->subscale_d && c->ch_and_idx == ref->ch_and_idx; +} + static inline void text_in_cell(const CPUCell *c, const TextCache *tc, ListOfChars *ans) { if (c->ch_is_idx) { diff --git a/kitty/lineops.h b/kitty/lineops.h index 42ea75b76..4d60730a7 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -130,6 +130,7 @@ CPUCell *linebuf_cpu_cell_at(LineBuf *self, index_type x, index_type y); bool linebuf_line_ends_with_continuation(LineBuf *self, index_type y); void linebuf_refresh_sprite_positions(LineBuf *self); void historybuf_add_line(HistoryBuf *self, const Line *line, ANSIBuf *); +bool cells_have_multiline_chars(const CPUCell *cells, index_type count); bool historybuf_pop_line(HistoryBuf *, Line *); void historybuf_init_line(HistoryBuf *self, index_type num, Line *l); bool history_buf_endswith_wrap(HistoryBuf *self); diff --git a/kitty/resize.c b/kitty/resize.c index 94445a70a..34b0c24a1 100644 --- a/kitty/resize.c +++ b/kitty/resize.c @@ -176,6 +176,27 @@ update_tracked_cursors(Rewrap *r, index_type num_cells, index_type src_y, index_ } } +static void +update_tracked_cursors_for_dropped_cells(Rewrap *r, index_type num_cells) { + // Cursors on cells of src that are not copied to dest are placed where the + // cells would have been. As for update_tracked_cursors() cursors beyond the + // content of the src line are placed relative to the current position, in + // case nothing after the dropped cells is copied. + if (!r->src_is_in_linebuf) return; + const index_type src_y = r->src.y - r->src.hb_count; + for (TrackCursor *t = r->cursors; !t->is_sentinel; t++) { + if (t->y == src_y && r->src.x <= t->x) { + if (t->x < r->src.x + num_cells) { + t->dest_y = r->dest.y; + t->dest_x = MIN(r->dest.x, dest_xnum); + } else if (t->x >= r->src_x_limit) { + t->dest_y = r->dest.y; + t->dest_x = MIN(r->dest.x + (t->x - (r->src.x + num_cells)), dest_xnum); + } + } + } +} + static bool find_space_in_dest_line(Rewrap *r, index_type num_cells) { while (r->dest.x + num_cells <= dest_xnum) { @@ -204,7 +225,10 @@ copy_range(Line *src, index_type src_at, Line *dest, index_type dest_at, index_t static void copy_multiline_extra_lines(Rewrap *r, CPUCell *src_cell, index_type mc_width) { - for (index_type i = 1; i < src_cell->scale; i++) { + // multiline_char_is_complete() has checked that all rows are present in src, + // the bound is kept so that this never reads out of bounds + const index_type num_src_lines = r->src.hb_count + r->src.lb->ynum; + for (index_type i = 1; i < src_cell->scale && r->src.y + i < num_src_lines; i++) { init_src_line_basic(r, r->src.y + i, &r->src.scratch_line, false); linebuf_init_line_at(r->sb, i - 1, &r->dest.scratch_line); linebuf_mark_line_dirty(r->sb, i - 1); @@ -214,6 +238,20 @@ copy_multiline_extra_lines(Rewrap *r, CPUCell *src_cell, index_type mc_width) { } +static bool +multiline_char_is_complete(Rewrap *r, const CPUCell *src_cell) { + // whether all rows of the multiline char starting at src_cell are present in src, they + // can be missing if the char is partially scrolled off or torn + const CPUCell ref = *src_cell; + const index_type num_src_lines = r->src.hb_count + r->src.lb->ynum; + for (index_type i = 1; i < ref.scale; i++) { + if (r->src.y + i >= num_src_lines) return false; + init_src_line_basic(r, r->src.y + i, &r->src.scratch_line, false); + if (!continues_multicell_char(r->src.scratch_line.cpu_cells + r->src.x, &ref, i)) return false; + } + return true; +} + static void multiline_copy_src_to_dest(Rewrap *r) { CPUCell *c; @@ -229,6 +267,11 @@ multiline_copy_src_to_dest(Rewrap *r) { } else if (c->y) { r->src.x += mc_width; continue; + } else if (c->scale > 1 && !multiline_char_is_complete(r, c)) { + // partial multiline chars are dropped as they cannot be placed consistently in dest + update_tracked_cursors_for_dropped_cells(r, mc_width); + r->src.x += mc_width; + continue; } } else mc_width = 1; find_space_in_dest(r, mc_width); @@ -296,6 +339,11 @@ rewrap(Rewrap *r) { if (r->current_src_line_has_multline_cells || r->current_dest_line_has_multiline_cells) multiline_copy_src_to_dest(r); else fast_copy_src_to_dest(r); } + // The lower rows of multiline chars are written to dest from sb when moving to + // the next dest line. If the src ended without that happening, for example, + // because the lines after a multiline char were continuations that fit on + // its first row, write out the remaining rows. + for (index_type i = 0; i < r->sb->ynum && r->sb->line_attrs[0].has_dirty_text; i++) next_dest_line(r, false); } static index_type @@ -340,6 +388,8 @@ resize_screen_buffers( if (!ans.lb) return ans; RAII_PyObject(raii_nlb, (PyObject *)ans.lb); (void)raii_nlb; + // sb holds the rows below the first row of multiline chars + static_assert((SCALE_BITS << 1) >= (1u << SCALE_BITS) - 2u, "sb is too small for the tallest multiline char"); LineBuf *sb = alloc_linebuf(SCALE_BITS << 1, columns, lb->text_cache); if (!sb) return ans; RAII_PyObject(scratch, (PyObject *)sb); @@ -414,15 +464,17 @@ nuke_multicell_char_at(LineBuf *lb, index_type x_, index_type y_) { CPUCell *cp; GPUCell *gp; linebuf_init_cells(lb, y_, &cp, &gp); - index_type num_lines_above = cp[x_].y; - index_type y_max_limit = MIN(lb->ynum, y_ + cp[x_].scale - num_lines_above); + const CPUCell ref = cp[x_]; + // only the adjacent rows that continue the char, as it may be partial or torn + index_type y_start = y_, y_limit = y_ + 1; + while (y_start > 0 && y_ - y_start < ref.y && continues_multicell_char(linebuf_cpu_cells_for_line(lb, y_start - 1) + x_, &ref, ref.y - (y_ - y_start) - 1)) + y_start--; + while (y_limit < lb->ynum && y_limit - y_ + ref.y < ref.scale && + continues_multicell_char(linebuf_cpu_cells_for_line(lb, y_limit) + x_, &ref, ref.y + (y_limit - y_))) + y_limit++; while (cp[x_].x && x_ > 0) x_--; index_type x_limit = MIN(lb->xnum, x_ + mcd_x_limit(&cp[x_])); - for (index_type y = y_; y < y_max_limit; y++) { - linebuf_init_cells(lb, y, &cp, &gp); - nuke_in_line(cp, gp, x_, x_limit); - } - for (int y = (int)y_ - 1; y > -1 && num_lines_above; y--, num_lines_above--) { + for (index_type y = y_start; y < y_limit; y++) { linebuf_init_cells(lb, y, &cp, &gp); nuke_in_line(cp, gp, x_, x_limit); } @@ -494,8 +546,11 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co for (index_type x = 0; x < ans.lb->xnum; x++) ans.lb->line->gpu_cells[x].bg = fill_bg; } } - } else if (ans.num_content_lines_after < ans.num_content_lines_before) { - // delete multicell chars split at the bottom + } + if (ans.num_content_lines_after && (ans.num_content_lines_after < ans.num_content_lines_before || ans.num_content_lines_after < lines)) { + // delete multicell chars split at the bottom, and chars that were cut + // off at the bottom of the screen but no longer are, as there are now + // lines below them linebuf_init_line(ans.lb, ans.num_content_lines_after - 1); for (index_type x = 0; x < ans.lb->xnum; x++) { CPUCell *c = ans.lb->line->cpu_cells + x; diff --git a/kitty/screen.c b/kitty/screen.c index 9650a30c1..d22bc90d6 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -246,6 +246,10 @@ do_screen_reset(Screen *self, bool is_hard_reset) { if (is_hard_reset) { linebuf_clear(self->linebuf, BLANK_CHAR); historybuf_clear(self->historybuf); + linebuf_clear(self->alt_linebuf, BLANK_CHAR); + // all buffers are now empty + self->multiline_chars_used = false; + zero_at_ptr(&self->multiline_chars_cut_off_at_bottom); self->last_visited_prompt.is_set = false; self->scrolled_by = 0; self->pixel_scroll_offset_y = 0; @@ -542,30 +546,50 @@ nuke_in_line(CPUCell *cp, GPUCell *gp, index_type start, index_type x_limit, cha } } +typedef struct MulticellRows { + int start, limit; +} MulticellRows; + +static MulticellRows +multicell_char_rows(Screen *self, index_type x, index_type y) { + // The rows [start, limit) occupied by the multicell char that has a cell + // at (x, y), negative rows are in the history buffer. Multiline chars can + // be partial, with some of their rows scrolled off the screen or out of + // the history buffer, or torn, with some of their rows replaced by + // unrelated lines. So rather than trusting the y and scale of the cell, + // only the adjacent rows that actually continue the char are included. + const CPUCell ref = linebuf_cpu_cells_for_line(self->linebuf, y)[x]; + MulticellRows ans = {.start = (int)y, .limit = (int)y + 1}; + if (!ref.is_multicell) return ans; + const int min_y = self->linebuf == self->main_linebuf ? -(int)self->historybuf->count : 0; + for (int r = (int)y - 1, ry = (int)ref.y - 1; ry >= 0 && r >= min_y; r--, ry--) { + if (!continues_multicell_char(range_line_(self, r)->cpu_cells + x, &ref, ry)) break; + ans.start = r; + } + for (index_type r = y + 1, ry = ref.y + 1u; ry < ref.scale && r < self->lines; r++, ry++) { + if (!continues_multicell_char(linebuf_cpu_cells_for_line(self->linebuf, r) + x, &ref, ry)) break; + ans.limit = (int)r + 1; + } + return ans; +} + +static void +mark_range_line_dirty(Screen *self, int y) { + if (y > -1) linebuf_mark_line_dirty(self->linebuf, y); + else historybuf_mark_line_dirty(self->historybuf, -(y + 1)); +} + static void nuke_multicell_char_at(Screen *self, index_type x_, index_type y_, bool replace_with_spaces) { - CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y_, &cp, &gp); - index_type num_lines_above = cp[x_].y; - index_type y_max_limit = MIN(self->lines, y_ + cp[x_].scale - num_lines_above); + const MulticellRows rows = multicell_char_rows(self, x_, y_); + const CPUCell *cp = linebuf_cpu_cells_for_line(self->linebuf, y_); while (cp[x_].x && x_ > 0) x_--; - index_type x_limit = MIN(self->columns, x_ + mcd_x_limit(&cp[x_])); - char_type ch = replace_with_spaces ? ' ' : 0; - for (index_type y = y_; y < y_max_limit; y++) { - linebuf_init_cells(self->linebuf, y, &cp, &gp); - nuke_in_line(cp, gp, x_, x_limit, ch); - linebuf_mark_line_dirty(self->linebuf, y); - } - int y_min_limit = -1; - if (self->linebuf == self->main_linebuf) y_min_limit = -(self->historybuf->count + 1); - for (int y = (int)y_ - 1; y > y_min_limit && num_lines_above; y--, num_lines_above--) { + const index_type x_limit = MIN(self->columns, x_ + mcd_x_limit(&cp[x_])); + const char_type ch = replace_with_spaces ? ' ' : 0; + for (int y = rows.start; y < rows.limit; y++) { Line *line = range_line_(self, y); - cp = line->cpu_cells; - gp = line->gpu_cells; - nuke_in_line(cp, gp, x_, x_limit, ch); - if (y > -1) linebuf_mark_line_dirty(self->linebuf, y); - else historybuf_mark_line_dirty(self->historybuf, -(y + 1)); + nuke_in_line(line->cpu_cells, line->gpu_cells, x_, x_limit, ch); + mark_range_line_dirty(self, y); } self->is_dirty = true; } @@ -582,6 +606,69 @@ nuke_multiline_char_intersecting_with(Screen *self, index_type x_start, index_ty } } +static bool * +multiline_chars_cut_off_at_bottom(Screen *self) { + return self->multiline_chars_cut_off_at_bottom + (self->linebuf == self->alt_linebuf ? 1 : 0); +} + +static void +nuke_multiline_chars_extending_below(Screen *self, index_type bottom) { + // Remove chars extending below the line bottom when a new line is about to come in below it, tearing + // them. When bottom is the last line of the screen these are chars cut off at the bottom of the screen. + bool *cut_off = multiline_chars_cut_off_at_bottom(self); + const bool at_screen_bottom = bottom + 1 >= self->lines; + if (at_screen_bottom && !*cut_off) return; + const CPUCell *cp = linebuf_cpu_cells_for_line(self->linebuf, bottom); + if (cells_have_multiline_chars(cp, self->columns)) { + for (index_type x = 0; x < self->columns; x++) { + if (cp[x].is_multicell && cp[x].y + 1u < cp[x].scale) nuke_multicell_char_at(self, x, bottom, false); + } + } + if (at_screen_bottom) *cut_off = false; +} + +static void +nuke_multiline_chars_torn_by_scroll(Screen *self, index_type top, index_type bottom, bool up, bool history_adjoins_top) { + // Scrolling the region [top, bottom] moves its lines by one, removing the line + // at one edge and bringing a new line in at the other. Multiline chars + // that end up cut off at the edge of the screen (or of the history buffer) + // are left alone, they are partially scrolled off and are rendered as + // such. Chars that would end up torn, with the rows adjacent to the + // remaining part of the char being from unrelated lines, are removed. + // history_adjoins_top must be true when the line removed from (up) or + // brought into (down) the top of the region is moved into or out of the + // history buffer. + if (!self->multiline_chars_used || top >= bottom || bottom >= self->lines) return; +#define has_multiline(y) cells_have_multiline_chars(linebuf_cpu_cells_for_line(self->linebuf, y), self->columns) + if (up) { + // The top line is removed. This cuts chars at the edge of the screen + // only if there is nothing above it, i.e. on the alternate screen. + // On the main screen, the line above the screen is the newest + // history line. + const bool at_edge = top == 0 && (history_adjoins_top || self->linebuf == self->alt_linebuf); + if (!at_edge && has_multiline(top)) nuke_multiline_char_intersecting_with(self, 0, self->columns, top, top + 1, false); + // A new line comes in at the bottom + nuke_multiline_chars_extending_below(self, bottom); + } else { + // The bottom line is removed, cutting chars at the edge of the screen + // if it is the last line of the screen. Rather than scanning for such + // chars here, the flag is set unconditionally, the next scroll up scans + // for them once and clears it. + if (bottom + 1 < self->lines) { + if (has_multiline(bottom)) nuke_multiline_char_intersecting_with(self, 0, self->columns, bottom, bottom + 1, false); + } else *multiline_chars_cut_off_at_bottom(self) = true; + // A new line comes in at the top, tearing chars that extend above the + // top line unless the new line is the history line above the top line + if (!(top == 0 && history_adjoins_top) && has_multiline(top)) { + const CPUCell *cp = linebuf_cpu_cells_for_line(self->linebuf, top); + for (index_type x = 0; x < self->columns; x++) { + if (cp[x].is_multicell && cp[x].y > 0) nuke_multicell_char_at(self, x, top, false); + } + } + } +#undef has_multiline +} + static void nuke_multicell_char_intersecting_with(Screen *self, index_type x_start, index_type x_limit, index_type y_start, index_type y_limit, bool replace_with_spaces) { for (index_type y = y_start; y < y_limit; y++) { @@ -799,6 +886,9 @@ screen_resize(Screen *self, unsigned int lines, unsigned int columns) { const unsigned int top = 0, bottom = self->lines - 1; Savepoint *sp = is_main ? &self->main_savepoint : &self->alt_savepoint; while (self->cursor->y + 1 < self->lines && self->lines - self->cursor->y > lines_after_cursor_before_resize) { + if (!self->historybuf->count) break; + // must be done before popping the history line + nuke_multiline_chars_torn_by_scroll(self, top, bottom, false, true); if (!historybuf_pop_line(self->historybuf, self->alt_linebuf->line)) break; INDEX_DOWN(true); linebuf_copy_line_to(self->main_linebuf, self->alt_linebuf->line, 0); @@ -1095,35 +1185,27 @@ insert_characters(Screen *self, index_type at, index_type num, index_type y, boo nuke_split_multicell_char_at_right_boundary(self, self->columns - 1, y, replace_with_spaces); } -static bool +static void halve_multicell_width(Screen *self, index_type x_, index_type y_) { - CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y_, &cp, &gp); - int y_min_limit = -1; - if (self->linebuf == self->main_linebuf) y_min_limit = -(self->historybuf->count + 1); - int expected_y_min_limit = ((int)y_) - cp[x_].scale; - if (expected_y_min_limit < y_min_limit) return false; - y_min_limit = expected_y_min_limit; - unsigned new_width = cp[x_].width / 2; + const MulticellRows rows = multicell_char_rows(self, x_, y_); + const CPUCell *cp = linebuf_cpu_cells_for_line(self->linebuf, y_); + const unsigned new_width = cp[x_].width / 2; while (cp[x_].x && x_ > 0) x_--; const index_type ws = mcd_x_limit(&cp[x_]); const index_type x_limit = MIN(self->columns, x_ + ws); const index_type half_x_limit = MIN(self->columns, x_ + ws / 2); - int y_max_limit = MIN(self->lines, y_ + cp[x_].scale); - for (int y = y_min_limit + 1; y < y_max_limit; y++) { + for (int y = rows.start; y < rows.limit; y++) { Line *line = range_line_(self, y); - cp = line->cpu_cells; - gp = line->gpu_cells; - for (index_type x = x_; x < half_x_limit; x++) cp[x].width = new_width; + CPUCell *c = line->cpu_cells; + GPUCell *g = line->gpu_cells; + for (index_type x = x_; x < half_x_limit; x++) c[x].width = new_width; for (index_type x = half_x_limit; x < x_limit; x++) { - cp[x] = (CPUCell){0}; - clear_sprite_position(gp[x]); + c[x] = (CPUCell){0}; + clear_sprite_position(g[x]); } - if (y > -1) linebuf_mark_line_dirty(self->linebuf, y); + mark_range_line_dirty(self, y); } self->is_dirty = true; - return true; } void @@ -1184,6 +1266,8 @@ add_combining_char(Screen *self, char_type ch, index_type x, index_type y) { if (!cell_has_text(cell) || (cell->is_multicell && cell->y)) return false; // don't allow adding combining chars to a null cell text_in_cell(cell, self->text_cache, self->lc); if (self->lc->count >= MAX_NUM_CODEPOINTS_PER_CELL) return false; // don't allow too many combining chars to prevent DoS attacks + // must be computed before the char is changed + const MulticellRows rows = cell->is_multicell ? multicell_char_rows(self, x, y) : (MulticellRows){0}; ensure_space_for_chars(self->lc, self->lc->count + 1); self->lc->chars[self->lc->count++] = ch; cell->ch_or_idx = tc_get_or_insert_chars(self->text_cache, self->lc); @@ -1192,7 +1276,8 @@ add_combining_char(Screen *self, char_type ch, index_type x, index_type y) { char_type ch_and_idx = cell->ch_and_idx; while (cell->x && x) cell = cpu_cells + --x; index_type x_limit = MIN(x + mcd_x_limit(cell), self->columns); - for (index_type v = y; v < y + cell->scale; v++) { + // the cell has y == 0 so rows.start == y, the lower rows can be cut off at the bottom of the screen + for (int v = rows.start; v < rows.limit; v++) { cpu_cells = linebuf_cpu_cells_for_line(self->linebuf, v); for (index_type h = x; h < x_limit; h++) cpu_cells[h].ch_and_idx = ch_and_idx; linebuf_mark_line_dirty(self->linebuf, v); @@ -1311,10 +1396,9 @@ draw_combining_char(Screen *self, text_loop_state *s, char_type ch) { if (self->lc->chars[base_pos + 1] == VS15 && cpu_cell->is_multicell && cpu_cell->natural_width && cpu_cell->width == 2 && is_emoji_presentation_base(self->lc->chars[base_pos])) { index_type deltax = (cpu_cell->scale * cpu_cell->width) / 2; - if (halve_multicell_width(self, xpos, s->prev.y)) { - self->cursor->x -= deltax; - init_segmentation_state(self, s); - } + halve_multicell_width(self, xpos, s->prev.y); + self->cursor->x -= deltax; + init_segmentation_state(self, s); } } else if (s->seg.grapheme_break == GBP_SpacingMark && !cp[xpos].is_multicell && wcwidth_std(char_props_for(ch)) > 0) { // Thai/Lao SARA AM is a SpacingMark with non-zero width, it widens a narrow base cell. @@ -1720,6 +1804,7 @@ screen_handle_multicell_command(Screen *self, const MultiCellCommand *cmd, const .halign = MIN(cmd->horizontal_align, M(HALIGN_BITS)), .is_multicell = true}; #undef M + if (mcd.scale > 1) self->multiline_chars_used = true; if (mcd.width) handle_fixed_width_multicell_command(self, mcd, self->lc); else { RAII_ListOfChars(lc); @@ -2588,6 +2673,7 @@ screen_cursor_to_column(Screen *self, unsigned int column) { } #define INDEX_UP(add_to_history) \ + nuke_multiline_chars_torn_by_scroll(self, top, bottom, true, add_to_history); \ linebuf_index(self->linebuf, top, bottom); \ INDEX_GRAPHICS(-1, add_to_history) \ if (add_to_history) { \ @@ -2650,6 +2736,7 @@ screen_reverse_index(Screen *self) { // Move cursor up one line, scrolling screen if needed unsigned int top = self->margin_top, bottom = self->margin_bottom; if (self->cursor->y == top) { + nuke_multiline_chars_torn_by_scroll(self, top, bottom, false, false); INDEX_DOWN(false); } else screen_cursor_up(self, 1, false, -1); } @@ -2667,6 +2754,9 @@ _reverse_scroll(Screen *self, unsigned int count, bool fill_from_scrollback) { } else count = MIN(self->lines, count); while (count-- > 0) { bool copied = false; + // must be done before popping the history line as removing chars + // can modify the parts of them that are in the history buffer + nuke_multiline_chars_torn_by_scroll(self, top, bottom, false, fill_from_scrollback && self->historybuf->count > 0); if (fill_from_scrollback) { copied = historybuf_pop_line(self->historybuf, self->alt_linebuf->line); // Keep a scrolled back viewport anchored on the same content @@ -3128,24 +3218,22 @@ screen_insert_lines(Screen *self, unsigned int count) { for (index_type x = 0; x < self->columns; x++) { if (cells[x].is_multicell && cells[x].y) nuke_multicell_char_at(self, x, self->cursor->y, false); } + const bool at_screen_bottom = bottom + 1 >= self->lines; + // remove chars straddling the bottom margin, as the lines at the bottom + // of the region are pushed out, tearing them + if (self->multiline_chars_used && !at_screen_bottom) nuke_multiline_chars_extending_below(self, bottom); screen_dirty_line_graphics(self, top, bottom, self->linebuf == self->main_linebuf); linebuf_insert_lines(self->linebuf, count, self->cursor->y, bottom); last_visited_prompt_insert_or_delete_lines(self, self->cursor->y, bottom, count, true); self->is_dirty = true; clear_all_selections(self); screen_carriage_return(self); - // remove split multiline chars at bottom of screen - cells = linebuf_cpu_cells_for_line(self->linebuf, bottom); - for (index_type x = 0; x < self->columns; x++) { - if (cells[x].is_multicell) { - index_type y_limit = cells[x].scale; - if (cells[x].y + 1u < y_limit) { - index_type orig = self->lines; - self->lines = bottom + 1; - nuke_multicell_char_at(self, x, bottom, false); - self->lines = orig; - } - } + if (self->multiline_chars_used) { + // Chars whose lower rows were pushed out of the region are now cut + // off at its bottom. At the bottom of the screen they are partially + // scrolled off, as when scrolling down, otherwise they are torn. + if (at_screen_bottom) *multiline_chars_cut_off_at_bottom(self) = true; + else nuke_multiline_chars_extending_below(self, bottom); } } } @@ -3173,6 +3261,9 @@ screen_delete_lines_impl(Screen *self, index_type start, index_type count, index y += count; y = MIN(bottom, y); nuke_multiline_char_intersecting_with(self, 0, self->columns, y, y + 1, false); + // blank lines come in at the bottom, tearing chars that extend below it, which + // includes chars that are cut off at the bottom of the screen + if (self->multiline_chars_used) nuke_multiline_chars_extending_below(self, bottom); screen_dirty_line_graphics(self, top, bottom, self->linebuf == self->main_linebuf); linebuf_delete_lines(self->linebuf, count, start, bottom); self->is_dirty = true; diff --git a/kitty/screen.h b/kitty/screen.h index 335ca2cd6..01dd6bbf2 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -141,6 +141,12 @@ typedef struct { CursorRenderInfo cursor; } last_rendered; bool is_dirty, scroll_changed, reload_all_gpu_data, sgr_blink_was_used; + // set once a multiline (scale > 1) char has been drawn, used to skip scanning for such chars in hot paths + bool multiline_chars_used; + // set when multiline chars may be cut off at the bottom of the main ([0]) or + // alternate ([1]) screen, which only happens when scrolling down, used to + // avoid scanning for them on every linefeed + bool multiline_chars_cut_off_at_bottom[2]; Cursor *cursor; Savepoint main_savepoint, alt_savepoint; PyObject *callbacks, *test_child; diff --git a/kitty_tests/multicell.py b/kitty_tests/multicell.py index 765e20730..e3da84e71 100644 --- a/kitty_tests/multicell.py +++ b/kitty_tests/multicell.py @@ -40,6 +40,185 @@ class TestMulticell(BaseTest): self.assertIsNotNone(c['mcd']) self.ae((c['x'], c['y'], c['mcd']['scale']), (x, y - s.lines + 2, 2)) + def test_rewrap_multiline_char_followed_by_continuation(self): + # Regression: when the lines after a multiline char were continuations that + # fit on its first row, the lower rows of the char were lost on rewrap + for cols in (3, 4, 6): + s = self.create_screen(cols=5, lines=8) + parse_bytes(s, b'\x1b]66;s=3;AB\x07\x1b[4;1HX') + s.resize(8, cols) + for y in range(3): + c = s.cpu_cells(y, 0) + self.assertIsNotNone(c['mcd'], f'cols: {cols} y: {y}') + self.ae(c['y'], y, f'cols: {cols}') + self.ae(str(s.line(0)), 'A' if cols == 3 else 'AX') + + def test_partial_multiline_chars(self): + # Multiline chars can be partially scrolled off the edges of the screen + # or the history buffer, such chars are kept and rendered partially. + # Chars must never be torn, i.e. have rows adjacent to their remaining + # part that are not part of the char, and code walking the rows of + # partial chars must stay within bounds. + def assert_not_torn(s, alt=False): + # checks the screen and when on the main screen the history buffer as well + first = 0 if alt else -s.historybuf.count + for y in range(first, s.lines): + for x in range(s.columns): + c = s.cpu_cells(y, x) + mcd = c['mcd'] + if mcd and mcd['scale'] > 1: + top = y - c['y'] + for r in range(max(first, top), min(s.lines, top + mcd['scale'])): + o = s.cpu_cells(r, x) + self.assertIsNotNone(o['mcd'], f'Torn char at ({x}, {y}) row: {r}') + self.ae((o['y'], o['mcd']['scale']), (r - top, mcd['scale']), f'Torn char at ({x}, {y}) row: {r}') + + def cell_y(s, y, x=0): + c = s.cpu_cells(y, x) + return c['y'] if c['mcd'] else None + + prefix = b'\x1b[3;1H\x1b]66;s=2;A\x07' + # scrolling down cuts off the bottom row of the char, the top row is kept + s = self.create_screen(cols=5, lines=5) + parse_bytes(s, prefix + b'\x1b[2T') + assert_not_torn(s) + self.ae([cell_y(s, y) for y in range(s.lines)], [None, None, None, None, 0]) + # adding a combining char to it must stay in bounds + parse_bytes(s, b'\x1b[5;3H\xd9\x8b') + assert_not_torn(s) + self.ae(str(s.line(4)), 'A\u064b') + # once it is no longer at the edge it is torn, so it is removed + parse_bytes(s, b'\x1b[S') + assert_not_torn(s) + self.ae([cell_y(s, y) for y in range(s.lines)], [None] * s.lines) + # rewrapping it must stay in bounds, partial chars are dropped by rewrap + for lines, columns in ((3, 8), (7, 5), (5, 3)): + s = self.create_screen(cols=5, lines=5) + parse_bytes(s, prefix + b'\x1b[2T') + s.resize(lines, columns) + assert_not_torn(s) + # scrolling by one moves the char intact + s = self.create_screen(cols=5, lines=5) + parse_bytes(s, prefix + b'\x1b[T') + assert_not_torn(s) + self.ae([cell_y(s, y) for y in range(s.lines)], [None, None, None, 0, 1]) + # on the alternate screen the top row is scrolled off the top + s = self.create_screen(cols=5, lines=5) + s.toggle_alt_screen() + parse_bytes(s, b'\x1b]66;s=3;A\x07\x1b[S') + assert_not_torn(s, True) + self.ae([cell_y(s, y) for y in range(s.lines)], [1, 2, None, None, None]) + parse_bytes(s, b'\x1b[T') + assert_not_torn(s, True) + self.ae([cell_y(s, y) for y in range(s.lines)], [None] * s.lines) + # rows evicted from the history buffer, which is at least as tall as the screen + s = self.create_screen(cols=3, lines=3, scrollback=3) + parse_bytes(s, b'\x1b]66;s=3;A\x07' + b'\r\n' * 6) + assert_not_torn(s) + self.ae([cell_y(s, y) for y in range(-3, s.lines)], [1, 2, None, None, None, None]) + s.reverse_scroll(3, True) + assert_not_torn(s) + self.ae([cell_y(s, y) for y in range(s.lines)], [1, 2, None]) + s.resize(4, 3) + assert_not_torn(s) + # chars straddling the margins or cut by them would be torn + for margins in (b'\x1b[3;5r', b'\x1b[1;3r', b'\x1b[4;5r'): + for scroll in (b'\x1b[T', b'\x1b[S', b'\x1bM', b'\x1bD', b'\x1b[L', b'\x1b[M', b'\x1b[2L', b'\x1b[2M'): + for alt in (False, True): + s = self.create_screen(cols=5, lines=5) + if alt: + s.toggle_alt_screen() + parse_bytes(s, prefix + margins) + if scroll != b'\x1b[S' and scroll != b'\x1b[T': + # at the top margin for RI, IL and DL, at the bottom margin for IND + y = int(margins[4:5]) if scroll == b'\x1bD' else int(margins[2:3]) + parse_bytes(s, b'\x1b[%d;1H' % y) + parse_bytes(s, scroll) + assert_not_torn(s, alt) + s.resize(3, 8) + assert_not_torn(s, alt) + s.resize(7, 4) + assert_not_torn(s, alt) + # operations on partial chars + setups = ( + prefix + b'\x1b[2T', + b'\x1b[1;1H\x1b]66;s=3;A\x07\x1b[2S', + b'\x1b[1;1H\x1b]66;s=3;AB\x07\x1b[2S', + b'\x1b[1;1H\x1b]66;s=3;A\x07\x1b[3;4H\x1b]66;s=2;B\x07\x1b[4T', + ) + ops = ( + b'\x1b[5;1HX', + b'\x1b[1;1HX', + b'\x1b[5;1H\xd9\x8b', + b'\x1b[1;2H\xd9\x8b', + b'\x1b[5;1H\x1b[2P', + b'\x1b[1;1H\x1b[2@', + b'\x1b[J', + b'\x1b[1J', + b'\x1b[2J', + b'\x1b[3J', + b'\x1b[5;1H\x1b[K', + b'\x1b[1;1H\x1b[L', + b'\x1b[1;1H\x1b[M', + b'\x1b[5;1H\x1b[M', + b'\x1b[S', + b'\x1b[T', + b'\x1b[5;1H\n', + b'\x1b[1;1H\x1bM', + b'\x1b[5;1H\x1b]66;s=2;C\x07', + b'\x1b[5;1H\xe2\x9d\xa4\xef\xb8\x8e', + ) + for setup in setups: + for alt in (False, True): + for op in ops: + for op2 in (b'', b'\x1b[S', b'\x1b[T'): + s = self.create_screen(cols=5, lines=5, scrollback=5) + if alt: + s.toggle_alt_screen() + parse_bytes(s, setup + op + op2) + assert_not_torn(s, alt) + s.resize(6, 4) + assert_not_torn(s, alt) + # a char cut off at the bottom of the alternate screen stays cut off when + # resizing without changing the number of lines, and is removed once + # it is no longer at the bottom + s = self.create_screen(cols=5, lines=5) + s.toggle_alt_screen() + parse_bytes(s, prefix + b'\x1b[2T') + s.resize(5, 7) + self.ae([cell_y(s, y) for y in range(s.lines)], [None, None, None, None, 0]) + parse_bytes(s, b'\x1b[S') + assert_not_torn(s, True) + self.ae([cell_y(s, y) for y in range(s.lines)], [None] * s.lines) + # inserting lines at the bottom of the screen cuts chars off, they are + # kept, and removed once they are no longer at the bottom + for alt in (False, True): + s = self.create_screen(cols=5, lines=5) + if alt: + s.toggle_alt_screen() + parse_bytes(s, b'\x1b[4;1H\x1b]66;s=2;A\x07\x1b[1;1H\x1b[L') + assert_not_torn(s, alt) + self.ae([cell_y(s, y) for y in range(s.lines)], [None, None, None, None, 0]) + parse_bytes(s, b'\x1b[5;1H\n') + assert_not_torn(s, alt) + self.ae([cell_y(s, y) for y in range(s.lines)], [None] * s.lines) + # the cursor on a partial char that is dropped by rewrap is placed where the char was + s = self.create_screen(cols=5, lines=6, scrollback=20) + parse_bytes(s, b'abcdefghij\x1b[4;1H\x1b]66;s=2;A\x07\x1b[2T\x1b[6;3HZ\x1b[6;2H') + self.ae(str(s.line(5)), 'AZ') + s.resize(6, 3) + self.ae(str(s.line(5)), 'Z') + self.ae((s.cursor.x, s.cursor.y), (0, 5)) + # and a cursor after it is placed relative to where the char was + s = self.create_screen(cols=5, lines=6, scrollback=20) + parse_bytes(s, b'abcdefghij\r\nk\r\n\x1b[4;1H\x1b]66;s=2;A\x07\x1b[2T\x1b[6;4H') + s.resize(9, 3) + self.ae((s.cursor.x, s.cursor.y), (1, 7)) + # hard reset clears the alternate screen as well + s = self.create_screen(cols=5, lines=5) + parse_bytes(s, b'\x1b[?47h' + prefix + b'\x1b[?47l\x1bc\x1b[?47h') + self.ae([str(s.line(y)) for y in range(s.lines)], [''] * s.lines) + def test_multicell(self: TestMulticell) -> None: from kitty.tab_bar import as_rgb @@ -544,6 +723,15 @@ def test_multicell(self: TestMulticell) -> None: multicell(s, 'b', scale=2) s.cursor.x, s.cursor.y = 0, 1 s.insert_lines(1) + # a is split at the cursor line so it is removed, b is pushed partially + # off the bottom of the screen so its top row is kept + for y in range(s.lines - 1): + assert_line('\0' * s.columns, y) + assert_line('b_' + '\0' * (s.columns - 2), s.lines - 1) + ac(0, s.lines - 1, y=0, scale=2) + # once it is no longer at the bottom it is torn, so it is removed + s.cursor.y = s.lines - 1 + s.index() for y in range(s.lines): assert_line('\0' * s.columns, y) s.reset()