From 8c3d48c5cdb2ae4e8bdbdc48f155f53d1a92973b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 2 Dec 2020 18:52:50 +0100 Subject: ime: render pre-edit text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is done by allocating cells for the pre-edit text when receiving the text-input::done() call, and populating them by converting the utf-8 formatted pre-edit text to wchars. We also convert the pre-edit cursor position to cell positions (it can cover multiple cells). When rendering, we simply render the pre-edit cells on-top off the regular grid. While doing so, we also mark the underlying, “real”, cells as dirty, to ensure they are re-rendered when the pre-edit text is modified or removed. --- render.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 19 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index dc926cc4..88a47d47 100644 --- a/render.c +++ b/render.c @@ -314,42 +314,49 @@ draw_strikeout(const struct terminal *term, pixman_image_t *pix, } static void -draw_cursor(const struct terminal *term, const struct cell *cell, - const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg, - const pixman_color_t *bg, int x, int y, int cols) +cursor_colors_for_cell(const struct terminal *term, const struct cell *cell, + const pixman_color_t *fg, const pixman_color_t *bg, + pixman_color_t *cursor_color, pixman_color_t *text_color) { - pixman_color_t cursor_color; - pixman_color_t text_color; - bool is_selected = cell->attrs.selected; if (term->cursor_color.cursor >> 31) { - cursor_color = color_hex_to_pixman(term->cursor_color.cursor); - text_color = color_hex_to_pixman( + *cursor_color = color_hex_to_pixman(term->cursor_color.cursor); + *text_color = color_hex_to_pixman( term->cursor_color.text >> 31 ? term->cursor_color.text : term->colors.bg); if (term->reverse ^ cell->attrs.reverse ^ is_selected) { - pixman_color_t swap = cursor_color; - cursor_color = text_color; - text_color = swap; + pixman_color_t swap = *cursor_color; + *cursor_color = *text_color; + *text_color = swap; } if (term->is_searching && !is_selected) { - color_dim_for_search(&cursor_color); - color_dim_for_search(&text_color); + color_dim_for_search(cursor_color); + color_dim_for_search(text_color); } } else { - cursor_color = *fg; - text_color = *bg; + *cursor_color = *fg; + *text_color = *bg; } +} + +static void +draw_cursor(const struct terminal *term, const struct cell *cell, + const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg, + const pixman_color_t *bg, int x, int y, int cols) +{ + pixman_color_t cursor_color; + pixman_color_t text_color; + cursor_colors_for_cell(term, cell, fg, bg, &cursor_color, &text_color); switch (term->cursor_style) { case CURSOR_BLOCK: - if (!term->kbd_focus) + if (unlikely(!term->kbd_focus || term->ime.preedit.cells != NULL)) draw_unfocused_block(term, pix, &cursor_color, x, y, cols); - else if (term->cursor_blink.state == CURSOR_BLINK_ON) { + else if (likely(term->cursor_blink.state == CURSOR_BLINK_ON)) { *fg = text_color; pixman_image_fill_rectangles( PIXMAN_OP_SRC, pix, &cursor_color, 1, @@ -358,12 +365,17 @@ draw_cursor(const struct terminal *term, const struct cell *cell, break; case CURSOR_BAR: - if (term->cursor_blink.state == CURSOR_BLINK_ON || !term->kbd_focus) + if (likely(term->cursor_blink.state == CURSOR_BLINK_ON || + !term->kbd_focus)) + { draw_bar(term, pix, font, &cursor_color, x, y); + } break; case CURSOR_UNDERLINE: - if (term->cursor_blink.state == CURSOR_BLINK_ON || !term->kbd_focus) { + if (likely(term->cursor_blink.state == CURSOR_BLINK_ON || + !term->kbd_focus)) + { draw_underline( term, pix, attrs_to_font(term, &cell->attrs), &cursor_color, x, y, cols); @@ -1009,6 +1021,99 @@ render_sixel_images(struct terminal *term, pixman_image_t *pix) } } +static void +render_ime_preedit(struct terminal *term, struct buffer *buf, + struct coord cursor) +{ + if (likely(term->ime.preedit.cells == NULL)) + return; + + int cells_needed = term->ime.preedit.count; + + int row_idx = cursor.row; + int col_idx = cursor.col; + + int cells_left = term->cols - cursor.col; + int cells_used = min(cells_needed, term->cols); + + /* Adjust start of pre-edit text to the left if string doesn't fit on row */ + if (cells_left < cells_used) + col_idx -= cells_used - cells_left; + + assert(col_idx >= 0); + assert(col_idx < term->cols); + + struct row *row = grid_row_in_view(term->grid, row_idx); + + /* Don't start pre-edit text in the middle of a double-width character */ + while (col_idx > 0 && row->cells[col_idx].wc == CELL_MULT_COL_SPACER) { + cells_used++; + col_idx--; + } + + /* + * Copy original content (render_cell() reads cell data directly + * from grid), and mark all cells as dirty. This ensures they are + * re-rendered when the pre-edit text is modified or removed. + */ + struct cell *real_cells = malloc(cells_used * sizeof(real_cells[0])); + for (int i = 0; i < cells_used; i++) { + assert(col_idx + i < term->cols); + real_cells[i] = row->cells[col_idx + i]; + real_cells[i].attrs.clean = 0; + } + row->dirty = true; + + /* Render pre-edit text */ + for (int i = 0; i < term->ime.preedit.count; i++) { + const struct cell *cell = &term->ime.preedit.cells[i]; + + if (cell->wc == CELL_MULT_COL_SPACER) + continue; + + int width = wcwidth(term->ime.preedit.cells[i].wc); + width = max(1, width); + + if (col_idx + i + width > term->cols) + break; + + row->cells[col_idx + i] = term->ime.preedit.cells[i]; + render_cell(term, buf->pix[0], row, col_idx + i, row_idx, false); + } + + /* Hollow cursor */ + if (!term->ime.preedit.cursor.hidden) { + int start = term->ime.preedit.cursor.start; + int end = term->ime.preedit.cursor.end; + + pixman_color_t fg = color_hex_to_pixman(term->colors.fg); + pixman_color_t bg = color_hex_to_pixman(term->colors.bg); + + pixman_color_t cursor_color, text_color; + cursor_colors_for_cell( + term, &term->ime.preedit.cells[start], + &fg, &bg, &cursor_color, &text_color); + + int x = term->margins.left + (col_idx + start) * term->cell_width; + int y = term->margins.top + row_idx * term->cell_height; + + int cols = end - start; + draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + } + + /* Restore original content (but do not render) */ + for (int i = 0; i < cells_used; i++) + row->cells[col_idx + i] = real_cells[i]; + free(real_cells); + + wl_surface_damage_buffer( + term->window->surface, + term->margins.left, + term->margins.top + row_idx * term->cell_height, + term->width - term->margins.left - term->margins.right, + 1 * term->cell_height); +} + static void render_row(struct terminal *term, pixman_image_t *pix, struct row *row, int row_no, int cursor_col) @@ -1903,6 +2008,9 @@ grid_render(struct terminal *term) term->render.workers.buf = NULL; } + /* Render IME pre-edit text */ + render_ime_preedit(term, buf, cursor); + if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ /* TODO: dim while searching */ -- cgit v1.3 From 05083110c3fad046ccf0838a364150b8d2bab888 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:36:56 +0100 Subject: ime: make IME compile-time optional --- ime.c | 63 ++++++++++++++++++++++++++++++++++++++++--------------- ime.h | 10 +++++++++ meson.build | 15 +++++++------ meson_options.txt | 1 + render.c | 3 +++ terminal.c | 18 ++++++++++++---- terminal.h | 4 ++++ wayland.c | 14 +++++++++++-- wayland.h | 4 ++++ 9 files changed, 103 insertions(+), 29 deletions(-) create mode 100644 meson_options.txt (limited to 'render.c') diff --git a/ime.c b/ime.c index 787985cf..ccdbb098 100644 --- a/ime.c +++ b/ime.c @@ -1,5 +1,7 @@ #include "ime.h" +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + #include #include "text-input-unstable-v3.h" @@ -67,11 +69,7 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, zwp_text_input_v3_commit(seat->wl_text_input); seat->ime.serial++; - free(seat->ime.preedit.pending.text); - seat->ime.preedit.pending.text = NULL; - - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = NULL; + ime_reset(seat); } static void @@ -82,10 +80,13 @@ preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, struct seat *seat = data; - free(seat->ime.preedit.pending.text); - seat->ime.preedit.pending.text = text != NULL ? xstrdup(text) : NULL; - seat->ime.preedit.pending.cursor_begin = cursor_begin; - seat->ime.preedit.pending.cursor_end = cursor_end; + ime_reset_preedit(seat); + + if (text != NULL) { + seat->ime.preedit.pending.text = xstrdup(text); + seat->ime.preedit.pending.cursor_begin = cursor_begin; + seat->ime.preedit.pending.cursor_end = cursor_end; + } } static void @@ -95,8 +96,11 @@ commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, LOG_DBG("commit: text=%s", text); struct seat *seat = data; - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = text != NULL ? xstrdup(text) : NULL; + + ime_reset_commit(seat); + + if (text != NULL) + seat->ime.commit.pending.text = xstrdup(text); } static void @@ -142,9 +146,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, /* 1. Delete existing pre-edit text */ if (term->ime.preedit.cells != NULL) { - free(term->ime.preedit.cells); - term->ime.preedit.cells = NULL; - term->ime.preedit.count = 0; + term_reset_ime(term); render_refresh(term); } @@ -162,9 +164,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, term, seat->ime.commit.pending.text, strlen(seat->ime.commit.pending.text)); - - free(seat->ime.commit.pending.text); - seat->ime.commit.pending.text = NULL; + ime_reset_commit(seat); } /* 4. Calculate surrounding text to send - not supported */ @@ -292,10 +292,31 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3, render_refresh(term); } + ime_reset_preedit(seat); +} + +void +ime_reset_preedit(struct seat *seat) +{ free(seat->ime.preedit.pending.text); seat->ime.preedit.pending.text = NULL; } +void +ime_reset_commit(struct seat *seat) +{ + free(seat->ime.commit.pending.text); + seat->ime.commit.pending.text = NULL; +} + +void +ime_reset(struct seat *seat) +{ + ime_reset_preedit(seat); + ime_reset_commit(seat); +} + + const struct zwp_text_input_v3_listener text_input_listener = { .enter = &enter, .leave = &leave, @@ -304,3 +325,11 @@ const struct zwp_text_input_v3_listener text_input_listener = { .delete_surrounding_text = &delete_surrounding_text, .done = &done, }; + +#else /* !FOOT_IME_ENABLED */ + +void ime_reset_preedit(struct seat *seat) {} +void ime_reset_commit(struct seat *seat) {} +void ime_reset(struct seat *seat) {} + +#endif diff --git a/ime.h b/ime.h index d1ce7ff0..d5c96534 100644 --- a/ime.h +++ b/ime.h @@ -1,5 +1,15 @@ #pragma once +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + #include "text-input-unstable-v3.h" extern const struct zwp_text_input_v3_listener text_input_listener; + +#endif /* FOOT_IME_ENABLED */ + +struct seat; + +void ime_reset_preedit(struct seat *seat); +void ime_reset_commit(struct seat *seat); +void ime_reset(struct seat *seat); diff --git a/meson.build b/meson.build index 1aa018a9..6e04ba8b 100644 --- a/meson.build +++ b/meson.build @@ -17,6 +17,9 @@ add_project_arguments( (is_debug_build ? ['-D_DEBUG'] : [cc.get_supported_arguments('-fno-asynchronous-unwind-tables')]) + + (get_option('ime') + ? ['-DFOOT_IME_ENABLED=1'] + : []) + cc.get_supported_arguments( ['-pedantic', '-fstrict-aliasing', @@ -198,9 +201,9 @@ subdir('completions') subdir('doc') subdir('icons') -# summary( -# { -# '': false, -# }, -# bool_yn: true -# ) +summary( + { + 'IME': get_option('ime'), + }, + bool_yn: true +) diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 00000000..fc3c1b0f --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('ime', type: 'boolean', value: true, description: 'IME (Input Method Editor) support') diff --git a/render.c b/render.c index 88a47d47..3e7ed96a 100644 --- a/render.c +++ b/render.c @@ -1025,6 +1025,8 @@ static void render_ime_preedit(struct terminal *term, struct buffer *buf, struct coord cursor) { +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + if (likely(term->ime.preedit.cells == NULL)) return; @@ -1112,6 +1114,7 @@ render_ime_preedit(struct terminal *term, struct buffer *buf, term->margins.top + row_idx * term->cell_height, term->width - term->margins.left - term->margins.right, 1 * term->cell_height); +#endif } static void diff --git a/terminal.c b/terminal.c index d08d8658..cfcc2040 100644 --- a/terminal.c +++ b/terminal.c @@ -1403,7 +1403,7 @@ term_destroy(struct terminal *term) tll_free(term->alt.sixel_images); sixel_fini(term); - free(term->ime.preedit.cells); + term_reset_ime(term); free(term->foot_exe); free(term->cwd); @@ -2218,12 +2218,12 @@ term_kbd_focus_out(struct terminal *term) if (it->item.kbd_focus == term) return; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (term->ime.preedit.cells != NULL) { - free(term->ime.preedit.cells); - term->ime.preedit.cells = NULL; - term->ime.preedit.count = 0; + term_reset_ime(term); render_refresh(term); } +#endif term->kbd_focus = false; cursor_refresh(term); @@ -2753,3 +2753,13 @@ term_view_to_text(const struct terminal *term, char **text, size_t *len) int end = grid_row_absolute_in_view(term->grid, term->rows - 1); return rows_to_text(term, start, end, text, len); } + +void +term_reset_ime(struct terminal *term) +{ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + free(term->ime.preedit.cells); + term->ime.preedit.cells = NULL; + term->ime.preedit.count = 0; +#endif +} diff --git a/terminal.h b/terminal.h index b8339d8d..bbe7df25 100644 --- a/terminal.h +++ b/terminal.h @@ -470,6 +470,7 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct { struct { struct cell *cells; @@ -482,6 +483,7 @@ struct terminal { } cursor; } preedit; } ime; +#endif bool quit; bool is_shutting_down; @@ -605,3 +607,5 @@ bool term_scrollback_to_text( const struct terminal *term, char **text, size_t *len); bool term_view_to_text( const struct terminal *term, char **text, size_t *len); + +void term_reset_ime(struct terminal *term); diff --git a/wayland.c b/wayland.c index c0a9c512..c8cb2c9c 100644 --- a/wayland.c +++ b/wayland.c @@ -116,6 +116,7 @@ seat_add_primary_selection(struct seat *seat) static void seat_add_text_input(struct seat *seat) { +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (seat->wayl->text_input_manager == NULL) return; @@ -128,6 +129,7 @@ seat_add_text_input(struct seat *seat) seat->wl_text_input = text_input; zwp_text_input_v3_add_listener(text_input, &text_input_listener, seat); +#endif } static void @@ -184,15 +186,18 @@ seat_destroy(struct seat *seat) wl_keyboard_release(seat->wl_keyboard); if (seat->wl_pointer != NULL) wl_pointer_release(seat->wl_pointer); + +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (seat->wl_text_input != NULL) zwp_text_input_v3_destroy(seat->wl_text_input); +#endif + if (seat->wl_seat != NULL) wl_seat_release(seat->wl_seat); + ime_reset(seat); free(seat->clipboard.text); free(seat->primary.text); - free(seat->ime.preedit.pending.text); - free(seat->ime.commit.pending.text); free(seat->name); } @@ -920,6 +925,7 @@ handle_global(void *data, struct wl_registry *registry, } } +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) { const uint32_t required = 1; if (!verify_iface_version(interface, version, required)) @@ -931,6 +937,7 @@ handle_global(void *data, struct wl_registry *registry, tll_foreach(wayl->seats, it) seat_add_text_input(&it->item); } +#endif } static void @@ -1190,8 +1197,11 @@ wayl_destroy(struct wayland *wayl) seat_destroy(&it->item); tll_free(wayl->seats); +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (wayl->text_input_manager != NULL) zwp_text_input_manager_v3_destroy(wayl->text_input_manager); +#endif + if (wayl->xdg_output_manager != NULL) zxdg_output_manager_v1_destroy(wayl->xdg_output_manager); if (wayl->shell != NULL) diff --git a/wayland.h b/wayland.h index 51a6fad1..f275c69c 100644 --- a/wayland.h +++ b/wayland.h @@ -224,6 +224,7 @@ struct seat { struct wl_clipboard clipboard; struct wl_primary primary; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED /* Input Method Editor */ struct zwp_text_input_v3 *wl_text_input; struct { @@ -250,6 +251,7 @@ struct seat { uint32_t serial; } ime; +#endif }; enum csd_surface { @@ -404,7 +406,9 @@ struct wayland { struct wp_presentation *presentation; uint32_t presentation_clock_id; +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct zwp_text_input_manager_v3 *text_input_manager; +#endif bool have_argb8888; tll(struct monitor) monitors; /* All available outputs */ -- cgit v1.3 From 4d90b200f1252e691592c98439635b7a0b2f6592 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:37:12 +0100 Subject: render: ignore IME preedit state when deciding which cursor style to render --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 3e7ed96a..4d1e1465 100644 --- a/render.c +++ b/render.c @@ -353,7 +353,7 @@ draw_cursor(const struct terminal *term, const struct cell *cell, switch (term->cursor_style) { case CURSOR_BLOCK: - if (unlikely(!term->kbd_focus || term->ime.preedit.cells != NULL)) + if (unlikely(!term->kbd_focus)) draw_unfocused_block(term, pix, &cursor_color, x, y, cols); else if (likely(term->cursor_blink.state == CURSOR_BLINK_ON)) { -- cgit v1.3 From e9f99df2ab028d4385bef303bb5d77dab7b12e1f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:38:26 +0100 Subject: render: ime: calculate on-screen cursor position ourselves The position calculated by render_grid() may be -1,-1 if the cursor is currently hidden. This fixes a crash when trying to input IME while the cursor is hidden. --- render.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 4d1e1465..9546de46 100644 --- a/render.c +++ b/render.c @@ -1022,14 +1022,23 @@ render_sixel_images(struct terminal *term, pixman_image_t *pix) } static void -render_ime_preedit(struct terminal *term, struct buffer *buf, - struct coord cursor) +render_ime_preedit(struct terminal *term, struct buffer *buf) { #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED if (likely(term->ime.preedit.cells == NULL)) return; + /* Adjust cursor position to viewport */ + struct coord cursor; + cursor = term->grid->cursor.point; + cursor.row += term->grid->offset; + cursor.row -= term->grid->view; + cursor.row &= term->grid->num_rows - 1; + + if (cursor.row < 0 || cursor.row >= term->rows) + return; + int cells_needed = term->ime.preedit.count; int row_idx = cursor.row; @@ -2012,7 +2021,7 @@ grid_render(struct terminal *term) } /* Render IME pre-edit text */ - render_ime_preedit(term, buf, cursor); + render_ime_preedit(term, buf); if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ -- cgit v1.3 From 83d3ae10aeea5ac7d451d789891542a1964c3faf Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 3 Dec 2020 18:39:34 +0100 Subject: render: ime: don’t render cursor if cursor begin == end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 9546de46..34323801 100644 --- a/render.c +++ b/render.c @@ -1093,9 +1093,10 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) } /* Hollow cursor */ - if (!term->ime.preedit.cursor.hidden) { - int start = term->ime.preedit.cursor.start; - int end = term->ime.preedit.cursor.end; + int start = term->ime.preedit.cursor.start; + int end = term->ime.preedit.cursor.end; + + if (!term->ime.preedit.cursor.hidden && end > start) { pixman_color_t fg = color_hex_to_pixman(term->colors.fg); pixman_color_t bg = color_hex_to_pixman(term->colors.bg); -- cgit v1.3 From 2078e1675d8ec1e1c90e1ce7063d0a70d54ea167 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 4 Dec 2020 18:43:06 +0100 Subject: render: ime: draw a ‘bar’ cursor when the pre-edit cursor’s begin == end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 34323801..2a3ab5b8 100644 --- a/render.c +++ b/render.c @@ -1092,25 +1092,33 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) render_cell(term, buf->pix[0], row, col_idx + i, row_idx, false); } - /* Hollow cursor */ int start = term->ime.preedit.cursor.start; int end = term->ime.preedit.cursor.end; - if (!term->ime.preedit.cursor.hidden && end > start) { + if (!term->ime.preedit.cursor.hidden) { + const struct cell *start_cell = &term->ime.preedit.cells[start]; pixman_color_t fg = color_hex_to_pixman(term->colors.fg); pixman_color_t bg = color_hex_to_pixman(term->colors.bg); pixman_color_t cursor_color, text_color; cursor_colors_for_cell( - term, &term->ime.preedit.cells[start], - &fg, &bg, &cursor_color, &text_color); + term, start_cell, &fg, &bg, &cursor_color, &text_color); int x = term->margins.left + (col_idx + start) * term->cell_width; int y = term->margins.top + row_idx * term->cell_height; - int cols = end - start; - draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + if (end == start) { + /* Bar */ + struct fcft_font *font = attrs_to_font(term, &start_cell->attrs); + draw_bar(term, buf->pix[0], font, &cursor_color, x, y); + } + + else if (end > start) { + /* Hollow cursor */ + int cols = end - start; + draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + } } /* Restore original content (but do not render) */ -- cgit v1.3 From 9ce0edc8bbcbdc5d269bdf573e20d959878ffa85 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 11:42:21 +0100 Subject: render: ime: don’t render pre-edit string in grid while searching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While doing a scrollback search, the pre-edit string should be rendered in the search box, not in the grid. Note that we don’t yet support IME in scrollback search mode. This patch simply prevents the pre-edit text being rendered in the grid, in the “background”, while searching. --- render.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 2a3ab5b8..24e4c6c4 100644 --- a/render.c +++ b/render.c @@ -1029,6 +1029,9 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) if (likely(term->ime.preedit.cells == NULL)) return; + if (unlikely(term->is_searching)) + return; + /* Adjust cursor position to viewport */ struct coord cursor; cursor = term->grid->cursor.point; -- cgit v1.3 From dca35215d079d11f0138a4cd2c98b76b39524a20 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 11:59:41 +0100 Subject: render: search: render colorized glyphs (emojis) correctly --- render.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 24e4c6c4..565e8878 100644 --- a/render.c +++ b/render.c @@ -2184,12 +2184,20 @@ render_search_box(struct terminal *term) if (glyph == NULL) continue; - pixman_image_t *src = pixman_image_create_solid_fill(&fg); - pixman_image_composite32( - PIXMAN_OP_OVER, src, glyph->pix, buf->pix[0], 0, 0, 0, 0, - x + glyph->x, y + font_baseline(term) - glyph->y, + if (unlikely(pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)) { + /* Glyph surface is a pre-rendered image (typically a color emoji...) */ + pixman_image_composite32( + PIXMAN_OP_OVER, glyph->pix, NULL, buf->pix[0], 0, 0, 0, 0, + x + glyph->x, y + font_baseline(term) - glyph->y, + glyph->width, glyph->height); + } else { + pixman_image_t *src = pixman_image_create_solid_fill(&fg); + pixman_image_composite32( + PIXMAN_OP_OVER, src, glyph->pix, buf->pix[0], 0, 0, 0, 0, + x + glyph->x, y + font_baseline(term) - glyph->y, glyph->width, glyph->height); - pixman_image_unref(src); + pixman_image_unref(src); + } x += term->cell_width; } -- cgit v1.3 From a552610fdb721980bc455c7dc8636bc13d8f698b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 11:59:56 +0100 Subject: render: search: handle multi-column characters --- render.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 565e8878..2a8a50dd 100644 --- a/render.c +++ b/render.c @@ -2117,7 +2117,8 @@ render_search_box(struct terminal *term) { assert(term->window->search_sub_surface != NULL); - const size_t wanted_visible_chars = max(20, term->search.len); + const size_t wanted_visible_chars = max( + 20, wcsnlen(term->search.buf, term->search.len)); assert(term->scale >= 1); const int scale = term->scale; @@ -2199,7 +2200,7 @@ render_search_box(struct terminal *term) pixman_image_unref(src); } - x += term->cell_width; + x += max(1, wcwidth(term->search.buf[i])) * term->cell_width; } if (term->search.cursor >= term->search.len) -- cgit v1.3 From 5ffa65a2e307e2442303d9d0ff2c2e05a1a8eca4 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 22:51:00 +0100 Subject: render: search: wcsnlen() -> wcswidth() --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 2a8a50dd..9cbe85d6 100644 --- a/render.c +++ b/render.c @@ -2118,7 +2118,7 @@ render_search_box(struct terminal *term) assert(term->window->search_sub_surface != NULL); const size_t wanted_visible_chars = max( - 20, wcsnlen(term->search.buf, term->search.len)); + 20, wcswidth(term->search.buf, term->search.len)); assert(term->scale >= 1); const int scale = term->scale; -- cgit v1.3 From b4a0f5b13b44525bd1dbdbc12e4e412fc9a0a1cb Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 23:29:12 +0100 Subject: render: search: glyph_offset is in *cells*, cursor position in *characters* When calculating the offset into the search string, from where to start rendering, take into account that the cursor position is in *characters*, and the glyph-offset is in *cells*. --- render.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-------------- search.c | 6 +++++ 2 files changed, 70 insertions(+), 18 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 9cbe85d6..7fdade1a 100644 --- a/render.c +++ b/render.c @@ -2117,8 +2117,19 @@ render_search_box(struct terminal *term) { assert(term->window->search_sub_surface != NULL); - const size_t wanted_visible_chars = max( - 20, wcswidth(term->search.buf, term->search.len)); + /* + * We treat the search box pretty much like a row of cells. That + * is, a glyph is either 1 or 2 (or more) “cells” wide. + * + * The search ‘length’, and ‘cursor’ (position) is in + * *characters*, not cells. This means we need to translate from + * character cound to cell count when calculating the length of + * the search box, where in the search string we should start + * rendering etc. + */ + + const size_t total_cells = wcswidth(term->search.buf, term->search.len); + const size_t wanted_visible_cells = max(20, total_cells); assert(term->scale >= 1); const int scale = term->scale; @@ -2128,12 +2139,12 @@ render_search_box(struct terminal *term) const size_t width = term->width - 2 * margin; const size_t visible_width = min( term->width - 2 * margin, - 2 * margin + wanted_visible_chars * term->cell_width); + 2 * margin + wanted_visible_cells * term->cell_width); const size_t height = min( term->height - 2 * margin, 2 * margin + 1 * term->cell_height); - const size_t visible_chars = (visible_width - 2 * margin) / term->cell_width; + const size_t visible_cells = (visible_width - 2 * margin) / term->cell_width; size_t glyph_offset = term->render.search_glyph_offset; unsigned long cookie = shm_cookie_search(term); @@ -2158,32 +2169,66 @@ render_search_box(struct terminal *term) int y = margin; pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); - /* Ensure cursor is visible */ - if (term->search.cursor < glyph_offset) - term->render.search_glyph_offset = glyph_offset = term->search.cursor; - else if (term->search.cursor > glyph_offset + visible_chars) { - term->render.search_glyph_offset = glyph_offset = - term->search.cursor - min(term->search.cursor, visible_chars); + /* + * Ensure cursor is visible + * + * First, we need to map the cursor character position to a cell + * position. Then we can ensure the cursor is within the rendered + * part of the search string. + */ + for (size_t i = 0, cell_idx = 0; + i <= term->search.cursor; + cell_idx += max(1, wcwidth(term->search.buf[i])), i++) + { + if (i != term->search.cursor) + continue; + + if (cell_idx < glyph_offset) + term->render.search_glyph_offset = glyph_offset = cell_idx; + else if (cell_idx > glyph_offset + visible_cells) { + term->render.search_glyph_offset = glyph_offset = + cell_idx - min(cell_idx, visible_cells); + } + break; } /* Move offset if there is free space available */ - if (term->search.len - glyph_offset < visible_chars) + if (total_cells - glyph_offset < visible_cells) term->render.search_glyph_offset = glyph_offset = - term->search.len - min(term->search.len, visible_chars); + total_cells - min(total_cells, visible_cells); - /* Text (what the user entered - *not* match(es)) */ - for (size_t i = glyph_offset; - i < term->search.len && i - glyph_offset < visible_chars; - i++) + /* + * Render the search string, starting at ‘glyph_offset’. Note that + * glyph_offset is in cells, not characters + */ + for (size_t i = 0, + cell_idx = 0, + width = max(1, wcwidth(term->search.buf[i])), + next_cell_idx = width; + i < term->search.len; + i++, + cell_idx = next_cell_idx, + width = max(1, wcwidth(term->search.buf[i])), + next_cell_idx += width) { if (i == term->search.cursor) draw_bar(term, buf->pix[0], font, &fg, x, y); + if (next_cell_idx >= glyph_offset && next_cell_idx - glyph_offset > visible_cells) + break; + + if (cell_idx < glyph_offset) { + cell_idx = next_cell_idx; + continue; + } + const struct fcft_glyph *glyph = fcft_glyph_rasterize( font, term->search.buf[i], term->font_subpixel); - if (glyph == NULL) + if (glyph == NULL) { + cell_idx = next_cell_idx; continue; + } if (unlikely(pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)) { /* Glyph surface is a pre-rendered image (typically a color emoji...) */ @@ -2200,7 +2245,8 @@ render_search_box(struct terminal *term) pixman_image_unref(src); } - x += max(1, wcwidth(term->search.buf[i])) * term->cell_width; + x += width * term->cell_width; + cell_idx = next_cell_idx; } if (term->search.cursor >= term->search.len) diff --git a/search.c b/search.c index a9f5499a..83f86390 100644 --- a/search.c +++ b/search.c @@ -18,6 +18,7 @@ #include "selection.h" #include "shm.h" #include "util.h" +#include "xmalloc.h" /* * Ensures a "new" viewport doesn't contain any unallocated rows. @@ -124,6 +125,11 @@ search_begin(struct terminal *term) term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; + term->search.len = 0; + term->search.sz = 64; + term->search.buf = xmalloc(term->search.sz * sizeof(term->search.buf[0])); + term->search.buf[0] = L'\0'; + term_xcursor_update(term); render_refresh_search(term); } -- cgit v1.3 From 1ea36740290162dfda5659b04ac364b0272a3423 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 23:34:27 +0100 Subject: render: codespell: cound -> count --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 7fdade1a..0e251e60 100644 --- a/render.c +++ b/render.c @@ -2123,7 +2123,7 @@ render_search_box(struct terminal *term) * * The search ‘length’, and ‘cursor’ (position) is in * *characters*, not cells. This means we need to translate from - * character cound to cell count when calculating the length of + * character count to cell count when calculating the length of * the search box, where in the search string we should start * rendering etc. */ -- cgit v1.3 From 7c420004fb2db841b8c800494c865e14b3ecaffe Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Dec 2020 23:34:42 +0100 Subject: render: search: don’t access term->search.buf[] directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 0e251e60..04f523cd 100644 --- a/render.c +++ b/render.c @@ -2128,7 +2128,10 @@ render_search_box(struct terminal *term) * rendering etc. */ - const size_t total_cells = wcswidth(term->search.buf, term->search.len); + const wchar_t *text = term->search.buf; + const size_t text_len = term->search.len; + + const size_t total_cells = wcswidth(text, text_len); const size_t wanted_visible_cells = max(20, total_cells); assert(term->scale >= 1); @@ -2152,7 +2155,7 @@ render_search_box(struct terminal *term) /* Background - yellow on empty/match, red on mismatch */ pixman_color_t color = color_hex_to_pixman( - term->search.match_len == term->search.len + term->search.match_len == text_len ? term->colors.table[3] : term->colors.table[1]); pixman_image_fill_rectangles( @@ -2178,7 +2181,7 @@ render_search_box(struct terminal *term) */ for (size_t i = 0, cell_idx = 0; i <= term->search.cursor; - cell_idx += max(1, wcwidth(term->search.buf[i])), i++) + cell_idx += max(1, wcwidth(text[i])), i++) { if (i != term->search.cursor) continue; @@ -2203,12 +2206,12 @@ render_search_box(struct terminal *term) */ for (size_t i = 0, cell_idx = 0, - width = max(1, wcwidth(term->search.buf[i])), + width = max(1, wcwidth(text[i])), next_cell_idx = width; - i < term->search.len; + i < text_len; i++, cell_idx = next_cell_idx, - width = max(1, wcwidth(term->search.buf[i])), + width = max(1, wcwidth(text[i])), next_cell_idx += width) { if (i == term->search.cursor) @@ -2223,7 +2226,7 @@ render_search_box(struct terminal *term) } const struct fcft_glyph *glyph = fcft_glyph_rasterize( - font, term->search.buf[i], term->font_subpixel); + font, text[i], term->font_subpixel); if (glyph == NULL) { cell_idx = next_cell_idx; @@ -2249,7 +2252,7 @@ render_search_box(struct terminal *term) cell_idx = next_cell_idx; } - if (term->search.cursor >= term->search.len) + if (term->search.cursor >= text_len) draw_bar(term, buf->pix[0], font, &fg, x, y); quirk_weston_subsurface_desync_on(term->window->search_sub_surface); -- cgit v1.3 From d46dcd8ef54eba88dd76570e4b9fd2da284523e9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 6 Dec 2020 12:18:46 +0100 Subject: render: ime: wip: pre-edit --- render.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 22 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 04f523cd..f5947eb6 100644 --- a/render.c +++ b/render.c @@ -2128,8 +2128,19 @@ render_search_box(struct terminal *term) * rendering etc. */ +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + size_t text_len = term->search.len; + if (term->ime.preedit.text != NULL) + text_len += wcslen(term->ime.preedit.text); + + wchar_t *text = xmalloc((text_len + 1) * sizeof(wchar_t)); + wcscpy(text, term->search.buf); + if (term->ime.preedit.text != NULL) + wcscat(text, term->ime.preedit.text); +#else const wchar_t *text = term->search.buf; const size_t text_len = term->search.len; +#endif const size_t total_cells = wcswidth(text, text_len); const size_t wanted_visible_cells = max(20, total_cells); @@ -2168,7 +2179,8 @@ render_search_box(struct terminal *term) 1, &(pixman_rectangle16_t){0, 0, width - visible_width, height}); struct fcft_font *font = term->fonts[0]; - int x = width - visible_width + margin; + const int x_left = width - visible_width + margin; + int x = x_left; int y = margin; pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); @@ -2179,6 +2191,8 @@ render_search_box(struct terminal *term) * position. Then we can ensure the cursor is within the rendered * part of the search string. */ + size_t cursor_cell_idx = 0; + for (size_t i = 0, cell_idx = 0; i <= term->search.cursor; cell_idx += max(1, wcwidth(text[i])), i++) @@ -2186,12 +2200,21 @@ render_search_box(struct terminal *term) if (i != term->search.cursor) continue; +#if 0 +#if (FOOT_IME_ENABLED) && FOOT_IME_ENABLED + if (term->ime.preedit.cells != NULL) + cell_idx += term->ime.preedit.count; +#endif +#endif + if (cell_idx < glyph_offset) term->render.search_glyph_offset = glyph_offset = cell_idx; else if (cell_idx > glyph_offset + visible_cells) { term->render.search_glyph_offset = glyph_offset = cell_idx - min(cell_idx, visible_cells); } + assert(cell_idx >= glyph_offset); + cursor_cell_idx = cell_idx - glyph_offset; break; } @@ -2214,8 +2237,10 @@ render_search_box(struct terminal *term) width = max(1, wcwidth(text[i])), next_cell_idx += width) { +#if 0 if (i == term->search.cursor) draw_bar(term, buf->pix[0], font, &fg, x, y); +#endif if (next_cell_idx >= glyph_offset && next_cell_idx - glyph_offset > visible_cells) break; @@ -2252,8 +2277,36 @@ render_search_box(struct terminal *term) cell_idx = next_cell_idx; } - if (term->search.cursor >= text_len) - draw_bar(term, buf->pix[0], font, &fg, x, y); + +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + + if (term->ime.preedit.cells != NULL) { + int cells_left = visible_cells - cursor_cell_idx; + int count = max(min(term->ime.preedit.count, cells_left), 0); + + /* Underline the entire pre-edit text */ + draw_underline(term, buf->pix[0], font, &fg, + x_left + cursor_cell_idx * term->cell_width, y, count); + + /* Cursor, unless hidden */ + if (!term->ime.preedit.cursor.hidden) { + /* TODO: we must ensure this is visible */ + const int start = cursor_cell_idx + term->ime.preedit.cursor.start; + const int end = cursor_cell_idx + term->ime.preedit.cursor.end; + + if (start == end) { + draw_bar(term, buf->pix[0], font, &fg, + x_left + start * term->cell_width, y); + } else { + draw_unfocused_block( + term, buf->pix[0], &fg, + x_left + start * term->cell_width, y, end - start); + } + } + } else +#endif + draw_bar(term, buf->pix[0], font, &fg, + x_left + cursor_cell_idx * term->cell_width, y); quirk_weston_subsurface_desync_on(term->window->search_sub_surface); @@ -2276,6 +2329,10 @@ render_search_box(struct terminal *term) wl_surface_commit(term->window->search_surface); quirk_weston_subsurface_desync_off(term->window->search_sub_surface); + +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + free(text); +#endif } static void @@ -2648,32 +2705,28 @@ fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data) tll_foreach(renderer->wayl->terms, it) { struct terminal *term = it->item; - if (!term->render.refresh.grid && - !term->render.refresh.csd && - !term->render.refresh.search) - { - continue; - } - - if (term->render.app_sync_updates.enabled && - !term->render.refresh.csd && - !term->render.refresh.search) - { + if (unlikely(!term->window->is_configured)) continue; - } - - if (term->render.refresh.csd || term->render.refresh.search) { - /* Force update of parent surface */ - term->render.refresh.grid = true; - } - - assert(term->window->is_configured); bool grid = term->render.refresh.grid; bool csd = term->render.refresh.csd; bool search = term->render.refresh.search; bool title = term->render.refresh.title; + if (!term->is_searching) + search = false; + + if (!(grid | csd | search | title)) + continue; + + if (term->render.app_sync_updates.enabled && !(csd | search | title)) + continue; + + if (csd | search) { + /* Force update of parent surface */ + grid = true; + } + term->render.refresh.grid = false; term->render.refresh.csd = false; term->render.refresh.search = false; -- cgit v1.3 From bcfc468fd0037ddeee64d3cf24602b570bacef3c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 6 Dec 2020 12:24:45 +0100 Subject: render: ime: adjust cursor cell index when adjusting glyph offset --- render.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index f5947eb6..9caadb09 100644 --- a/render.c +++ b/render.c @@ -2219,9 +2219,12 @@ render_search_box(struct terminal *term) } /* Move offset if there is free space available */ - if (total_cells - glyph_offset < visible_cells) + if (total_cells - glyph_offset < visible_cells) { + ssize_t old = glyph_offset; term->render.search_glyph_offset = glyph_offset = total_cells - min(total_cells, visible_cells); + cursor_cell_idx += old - (ssize_t)glyph_offset; + } /* * Render the search string, starting at ‘glyph_offset’. Note that -- cgit v1.3 From b270f221d0328651fdac46cf081e2cbce4d74f4c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 7 Dec 2020 18:58:01 +0100 Subject: render: search: render IME pre-edit text correctly, hopefully --- render.c | 177 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 61 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 9caadb09..1c905990 100644 --- a/render.c +++ b/render.c @@ -2134,14 +2134,30 @@ render_search_box(struct terminal *term) text_len += wcslen(term->ime.preedit.text); wchar_t *text = xmalloc((text_len + 1) * sizeof(wchar_t)); - wcscpy(text, term->search.buf); + text[0] = L'\0'; + + /* Copy everything up to the cursor */ + wcsncpy(text, term->search.buf, term->search.cursor); + text[term->search.cursor] = L'\0'; + + /* Insert pre-edit text at cursor */ if (term->ime.preedit.text != NULL) wcscat(text, term->ime.preedit.text); + + /* And finally everything after the cursor */ + wcsncat(text, &term->search.buf[term->search.cursor], + term->search.len - term->search.cursor); #else const wchar_t *text = term->search.buf; const size_t text_len = term->search.len; #endif + /* Calculate the width of each character */ + int widths[text_len + 1]; + for (size_t i = 0; i < text_len; i++) + widths[i] = max(1, wcwidth(text[i])); + widths[text_len] = 0; + const size_t total_cells = wcswidth(text, text_len); const size_t wanted_visible_cells = max(20, total_cells); @@ -2184,46 +2200,56 @@ render_search_box(struct terminal *term) int y = margin; pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); - /* - * Ensure cursor is visible - * - * First, we need to map the cursor character position to a cell - * position. Then we can ensure the cursor is within the rendered - * part of the search string. - */ - size_t cursor_cell_idx = 0; - - for (size_t i = 0, cell_idx = 0; - i <= term->search.cursor; - cell_idx += max(1, wcwidth(text[i])), i++) - { + /* Move offset we start rendering at, to ensure the cursor is visible */ + for (size_t i = 0, cell_idx = 0; i <= term->search.cursor; cell_idx += widths[i], i++) { if (i != term->search.cursor) continue; -#if 0 #if (FOOT_IME_ENABLED) && FOOT_IME_ENABLED - if (term->ime.preedit.cells != NULL) - cell_idx += term->ime.preedit.count; -#endif + if (term->ime.preedit.cells != NULL) { + if (term->ime.preedit.cursor.start == term->ime.preedit.cursor.end) { + /* All IME's I've seen so far keeps the cursor at + * index 0, so ensure the *end* of the pre-edit string + * is visible */ + cell_idx += term->ime.preedit.count; + } else { + /* Try to predict in which direction we'll shift the text */ + if (cell_idx + term->ime.preedit.cursor.start > glyph_offset) + cell_idx += term->ime.preedit.cursor.end; + else + cell_idx += term->ime.preedit.cursor.start; + } + } #endif - if (cell_idx < glyph_offset) + if (cell_idx < glyph_offset) { + /* Shift to the *left*, making *this* character the + * *first* visible one */ term->render.search_glyph_offset = glyph_offset = cell_idx; + } + else if (cell_idx > glyph_offset + visible_cells) { + /* Shift to the *right*, making *this* character the + * *last* visible one */ term->render.search_glyph_offset = glyph_offset = cell_idx - min(cell_idx, visible_cells); } - assert(cell_idx >= glyph_offset); - cursor_cell_idx = cell_idx - glyph_offset; + + /* Adjust offset if there is free space available */ + if (total_cells - glyph_offset < visible_cells) { + term->render.search_glyph_offset = glyph_offset = + total_cells - min(total_cells, visible_cells); + } + break; } - /* Move offset if there is free space available */ - if (total_cells - glyph_offset < visible_cells) { - ssize_t old = glyph_offset; - term->render.search_glyph_offset = glyph_offset = - total_cells - min(total_cells, visible_cells); - cursor_cell_idx += old - (ssize_t)glyph_offset; + /* Ensure offset is at a character boundary */ + for (size_t i = 0, cell_idx = 0; i <= text_len; cell_idx += widths[i], i++) { + if (cell_idx >= glyph_offset) { + term->render.search_glyph_offset = glyph_offset = cell_idx; + break; + } } /* @@ -2232,23 +2258,75 @@ render_search_box(struct terminal *term) */ for (size_t i = 0, cell_idx = 0, - width = max(1, wcwidth(text[i])), + width = widths[i], next_cell_idx = width; i < text_len; i++, cell_idx = next_cell_idx, - width = max(1, wcwidth(text[i])), + width = widths[i], next_cell_idx += width) { -#if 0 - if (i == term->search.cursor) - draw_bar(term, buf->pix[0], font, &fg, x, y); + /* Render cursor */ + if (i == term->search.cursor) { +#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED + bool have_preedit = term->ime.preedit.cells != NULL; + bool hidden = term->ime.preedit.cursor.hidden; + + if (have_preedit && !hidden) { + /* Cursor may be outside the visible area: + * cell_idx-glyph_offset can be negative */ + int cells_left = visible_cells - max( + (ssize_t)(cell_idx - glyph_offset), 0); + + /* If cursor is outside the visible area, we need to + * adjust our rectangle's position */ + int start = term->ime.preedit.cursor.start + + min((ssize_t)(cell_idx - glyph_offset), 0); + int end = term->ime.preedit.cursor.end + + min((ssize_t)(cell_idx - glyph_offset), 0); + + if (start == end) { + int count = min(term->ime.preedit.count, cells_left); + + /* Underline the entire (visible part of) pre-edit text */ + draw_underline(term, buf->pix[0], font, &fg, x, y, count); + + /* Bar-styled cursor, if in the visible area */ + if (start >= 0 && start <= visible_cells) + draw_bar(term, buf->pix[0], font, &fg, x + start * term->cell_width, y); + } else { + /* Underline everything before and after the cursor */ + int count1 = min(start, cells_left); + int count2 = max( + min(term->ime.preedit.count - term->ime.preedit.cursor.end, + cells_left - end), + 0); + draw_underline(term, buf->pix[0], font, &fg, x, y, count1); + draw_underline(term, buf->pix[0], font, &fg, x + end * term->cell_width, y, count2); + + /* TODO: how do we handle a partially hidden rectangle? */ + if (start >= 0 && end <= visible_cells) { + draw_unfocused_block( + term, buf->pix[0], &fg, x + start * term->cell_width, y, end - start); + } + } + } else if (!have_preedit) #endif + { + /* Cursor *should* be in the visible area */ + assert(cell_idx >= glyph_offset); + assert(cell_idx <= glyph_offset + visible_cells); + draw_bar(term, buf->pix[0], font, &fg, x, y); + } + } - if (next_cell_idx >= glyph_offset && next_cell_idx - glyph_offset > visible_cells) + if (next_cell_idx >= glyph_offset && next_cell_idx - glyph_offset > visible_cells) { + /* We're now beyond the visible area - nothing more to render */ break; + } if (cell_idx < glyph_offset) { + /* We haven't yet reached the visible part of the string */ cell_idx = next_cell_idx; continue; } @@ -2280,36 +2358,13 @@ render_search_box(struct terminal *term) cell_idx = next_cell_idx; } - #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED - - if (term->ime.preedit.cells != NULL) { - int cells_left = visible_cells - cursor_cell_idx; - int count = max(min(term->ime.preedit.count, cells_left), 0); - - /* Underline the entire pre-edit text */ - draw_underline(term, buf->pix[0], font, &fg, - x_left + cursor_cell_idx * term->cell_width, y, count); - - /* Cursor, unless hidden */ - if (!term->ime.preedit.cursor.hidden) { - /* TODO: we must ensure this is visible */ - const int start = cursor_cell_idx + term->ime.preedit.cursor.start; - const int end = cursor_cell_idx + term->ime.preedit.cursor.end; - - if (start == end) { - draw_bar(term, buf->pix[0], font, &fg, - x_left + start * term->cell_width, y); - } else { - draw_unfocused_block( - term, buf->pix[0], &fg, - x_left + start * term->cell_width, y, end - start); - } - } - } else + if (term->ime.preedit.cells != NULL) + /* Already rendered */; + else #endif - draw_bar(term, buf->pix[0], font, &fg, - x_left + cursor_cell_idx * term->cell_width, y); + if (term->search.cursor >= term->search.len) + draw_bar(term, buf->pix[0], font, &fg, x, y); quirk_weston_subsurface_desync_on(term->window->search_sub_surface); -- cgit v1.3 From ae01caee8ce4678de945fc18e97b95feced5fde1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 8 Dec 2020 19:17:46 +0100 Subject: render: ime: ensure cursor is visible --- render.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 1c905990..df22dd6e 100644 --- a/render.c +++ b/render.c @@ -1046,6 +1046,7 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) int row_idx = cursor.row; int col_idx = cursor.col; + int ime_ofs = 0; /* Offset into pre-edit string to start rendering at */ int cells_left = term->cols - cursor.col; int cells_used = min(cells_needed, term->cols); @@ -1054,6 +1055,30 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) if (cells_left < cells_used) col_idx -= cells_used - cells_left; + if (cells_needed > cells_used) { + int start = term->ime.preedit.cursor.start; + int end = term->ime.preedit.cursor.end; + + if (start == end) { + /* Ensure *end* of pre-edit string is visible */ + ime_ofs = cells_needed - cells_used; + } else { + /* Ensure the *beginning* of the cursor-area is visible */ + ime_ofs = start; + + /* Display as much as possible of the pre-edit string */ + if (cells_needed - ime_ofs < cells_used) + ime_ofs = cells_needed - cells_used; + } + + /* Make sure we don't start in the middle of a character */ + while (ime_ofs < cells_needed && + term->ime.preedit.cells[ime_ofs].wc == CELL_MULT_COL_SPACER) + { + ime_ofs++; + } + } + assert(col_idx >= 0); assert(col_idx < term->cols); @@ -1079,27 +1104,26 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) row->dirty = true; /* Render pre-edit text */ - for (int i = 0; i < term->ime.preedit.count; i++) { - const struct cell *cell = &term->ime.preedit.cells[i]; + assert(term->ime.preedit.cells[ime_ofs].wc != CELL_MULT_COL_SPACER); + for (int i = 0, idx = ime_ofs; idx < term->ime.preedit.count; i++, idx++) { + const struct cell *cell = &term->ime.preedit.cells[idx]; if (cell->wc == CELL_MULT_COL_SPACER) continue; - int width = wcwidth(term->ime.preedit.cells[i].wc); - width = max(1, width); - + int width = max(1, wcwidth(cell->wc)); if (col_idx + i + width > term->cols) break; - row->cells[col_idx + i] = term->ime.preedit.cells[i]; + row->cells[col_idx + i] = *cell; render_cell(term, buf->pix[0], row, col_idx + i, row_idx, false); } - int start = term->ime.preedit.cursor.start; - int end = term->ime.preedit.cursor.end; + int start = term->ime.preedit.cursor.start - ime_ofs; + int end = term->ime.preedit.cursor.end - ime_ofs; if (!term->ime.preedit.cursor.hidden) { - const struct cell *start_cell = &term->ime.preedit.cells[start]; + const struct cell *start_cell = &term->ime.preedit.cells[start + ime_ofs]; pixman_color_t fg = color_hex_to_pixman(term->colors.fg); pixman_color_t bg = color_hex_to_pixman(term->colors.bg); @@ -1113,14 +1137,18 @@ render_ime_preedit(struct terminal *term, struct buffer *buf) if (end == start) { /* Bar */ - struct fcft_font *font = attrs_to_font(term, &start_cell->attrs); - draw_bar(term, buf->pix[0], font, &cursor_color, x, y); + if (start >= 0) { + struct fcft_font *font = attrs_to_font(term, &start_cell->attrs); + draw_bar(term, buf->pix[0], font, &cursor_color, x, y); + } } else if (end > start) { /* Hollow cursor */ - int cols = end - start; - draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + if (start >= 0 && end <= term->cols) { + int cols = end - start; + draw_unfocused_block(term, buf->pix[0], &cursor_color, x, y, cols); + } } } -- cgit v1.3