Skip to content
Myr

render: scroll input to ensure cursor is always visible

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The input string now scrolls with the cursor (when the string is longer than the available space), ensuring the cursor and surrounding text is always visible. When calculating whether a glyph will fit or not, we use the largest value of the glyphs advance width, and its X offset + glyph width. The advance width is how much the pen position is moved after rendering the glyph. The X-offset + width tells us where the last pixel column of the glyph will be. By using the largest of the two, we ensure the glyph, and the cursor, will be fully visible. (kerning is also taken into account). Limitations and peculiarities: * Text is always left-aligned. That is, we always start rendering on the left side, at the exact same pixel offset. This means characters will "jump around" close to the right side. * When scrolling, we try to keep the cursor at the edge (either left or right). But, since the text is always left-aligned, we may sometimes scroll in *two* characters when moving the cursor one step to the right. This happens if the scrolled out character is wide, and the scrolled in character(s) are narrow(er). * Even before this patch, ligatures (both emoji ZWJ sequences, and fi, ff, ft, etc) were not handled well. For example, the cursor couldn't be moved "into" the ligature, and you had to press a cursor key twice to move over e.g. 'ff'. One way to solve this is to split the input string into two text runs; everything on the left side of the cursor, and everything on the right side of the cursor. This would however complicate rendering quite a bit. Instead, I decided to disable shaping completely, of the input string (the prompt string, and application entries, are still shaped). Closes #378

authored by Daniel Eklöf · committed by Daniel Eklöf · parent d2fed96 · Browse files · 3 files+170 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+170 added−25 removed

  • other:+170 added−25 removed

3 files+170 added−25 removed

modified:CHANGELOG.md+3 added
81 unmodified lines
82 width ([#560][560]).83* Default icon theme from `hicolor` to `default`.84* Error out (with an appropriate error message) if the command line we85 attempt to execute has non-specification-compliant quoting.86* Input text now "scrolls" with the cursor, if the string is longer87 than the available space ([#378][378]).8889[560]: https://codeberg.org/dnkl/fuzzel/issues/56090[378]: https://codeberg.org/dnkl/fuzzel/issues/378919293### Deprecated94### Removed
936 unmodified lines
modified:prompt.c+3 added-1 removed
277 unmodified lines
278bool279prompt_erase_before_cursor(struct prompt *prompt)280{281 if (prompt->cursor == 0) {282 return false;282 return false;283 }284285 memmove(&prompt->text[0],286 &prompt->text[prompt->cursor],287 (c32len(prompt->text) - prompt->cursor + 1) * sizeof(char32_t));288 prompt->cursor = 0;289290 return true;291}
modified:render.c+164 added-24 removed
84 unmodified lines
85 unsigned border_radius;86 unsigned row_height;87 unsigned icon_height;8889 unsigned input_glyph_offset; /* At which glyph to start rendering input */9091 struct {92 uint16_t count;93 sem_t start;94 sem_t done;
335 unmodified lines
430 font->underline.thickness});431 }432}433434static void435adjust_input_glyph_offset(struct render *render, const struct prompt *prompt,436 size_t count,437 const struct fcft_glyph *glyphs[static count],438 long kerning[static count],439 int start_x, int max_x)440{441 struct fcft_font *font = render->font;442 const size_t cursor_location = prompt_cursor(prompt);443444 if (cursor_location == 0)445 render->input_glyph_offset = 0;446447 /* Clamp to input string's length */448 render->input_glyph_offset = min(render->input_glyph_offset, count - 1);449450 /* If cursor position is before the current offset, adjust the451 offset backward */452 for (size_t i = 0; i < render->input_glyph_offset; i++) {453 if (cursor_location == i + 1) {454 render->input_glyph_offset = i + 1;455 break;456 }457 }458459 /* If cursor position is after the current visible portion, adjust the offset forward */460 int pos = start_x;461462 for (size_t i = render->input_glyph_offset; i < count; i++) {463464 const struct fcft_glyph *glyph = glyphs[i];465 if (glyph == NULL)466 continue;467468 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);469470 /* Cursor is after this glyph? */471 if (cursor_location == i + 1) {472 /* If current glyph doesn't fit, adjust offset so that it does */473 if (pos + kerning[i] + pixels_needed > max_x) {474 int needed = (pos + kerning[i] + pixels_needed) - max_x;475476 /* Font may be variable width, meaning we may have to scroll multiple characters */477 while (needed > 0 && render->input_glyph_offset + 1 < count) {478 long scrolled_kerning;479 fcft_kerning(480 font,481 glyphs[render->input_glyph_offset]->cp,482 glyphs[render->input_glyph_offset + 1]->cp,483 &scrolled_kerning, NULL);484485 const int gained =486 scrolled_kerning +487 glyphs[render->input_glyph_offset]->advance.x;488489 needed -= gained;490 pos -= gained;491 render->input_glyph_offset++;492 }493 }494 }495496 pos += kerning[i] + glyph->advance.x;497 }498499 /* Pull in glyphs from the left, if there's space left */500 while (render->input_glyph_offset > 0 && max_x > pos) {501 int total_width = start_x;502503 for (size_t i = render->input_glyph_offset; i < count; i++) {504 const struct fcft_glyph *g = glyphs[i];505 if (g == NULL)506 continue;507508 const int w = max(g->width, g->advance.x);509510 if (total_width + kerning[i] + w > max_x) {511 break;512 }513514 total_width += kerning[i] + (i + 1 == count ? w : g->advance.x);515 }516517 const struct fcft_glyph *glyph = glyphs[render->input_glyph_offset - 1];518 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);519520 assert(render->input_glyph_offset > 0);521 assert(render->input_glyph_offset < count);522523 const long kern = kerning[render->input_glyph_offset];524 const int available = max_x - total_width;525 if (kern + pixels_needed > available)526 break;527528 render->input_glyph_offset--;529 pos += kern + glyph->advance.x;530 }531}532533void534render_prompt(struct render *render, struct buffer *buf,535 const struct prompt *prompt, const struct matches *matches)536{
9 unmodified lines
546 const char32_t *ptext = prompt_text(prompt);547 size_t text_len = c32len(ptext);548 bool use_placeholder = text_len == 0;549449 if (use_placeholder) {450 ptext = prompt_placeholder(prompt);451 text_len = c32len(ptext);452 }453550 const enum fcft_subpixel subpixel =551 (render->conf->colors.background.a == 1. &&552 render->conf->colors.selection.a == 1.)553 ? render->subpixel : FCFT_SUBPIXEL_NONE;554555 const struct fcft_glyph *input_glyphs[text_len];556 long input_kerning[text_len];557558 for (size_t i = 0; i < text_len; i++) {559 input_glyphs[i] = fcft_rasterize_char_utf32(font, ptext[i], subpixel);560 if (i == 0)561 input_kerning[0] = 0;562 else563 fcft_kerning(font, ptext[i - 1], ptext[i], &input_kerning[i], NULL);564 }565566 if (use_placeholder) {567 ptext = prompt_placeholder(prompt);568 text_len = c32len(ptext);569 }570571 int x = render->border_size + render->x_margin;572 int y = render->border_size + render->y_margin + render_baseline(render);573574 /* Erase background */575 pixman_color_t bg = render->pix_background_color;576 //pixman_color_t bg = (pixman_color_t){0xffff, 0, 0, 0xffff};577 pixman_image_fill_rectangles(578 PIXMAN_OP_SRC, buf->pix[0], &bg, 1,579 &(pixman_rectangle16_t){580 render->border_size + render->x_margin - render->x_margin / 3,581 render->border_size + render->y_margin,582 buf->width - 2 * (render->border_size + render->x_margin - render->x_margin / 3),583 render->row_height});584585#if 0586 bg = (pixman_color_t){0, 0xffff, 0, 0xffff};587 pixman_image_fill_rectangles(588 PIXMAN_OP_SRC, buf->pix[0], &bg, 1,589 &(pixman_rectangle16_t){590 render->border_size + render->x_margin,591 render->border_size + render->y_margin,592 buf->width - 2 * (render->border_size + render->x_margin),593 render->row_height});594#endif595596 int stats_width = conf->match_counter597 ? render_match_count(render, buf, prompt, matches)598 : 0;
9 unmodified lines
608 if (prompt_run == NULL) {609 prompt_run = fcft_rasterize_text_run_utf32(610 font, prompt_len, pprompt, subpixel);611 }488 if (input_run == NULL) {612 if (input_run == NULL && use_placeholder) {613 input_run = fcft_rasterize_text_run_utf32(614 font, text_len, ptext, subpixel);615 }616 }617618 if (prompt_run != NULL) {619 for (size_t i = 0; i < prompt_run->count; i++) {620 const struct fcft_glyph *glyph = prompt_run->glyphs[i];497 if (x + glyph->width > max_x)621 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);622623 if (x + pixels_needed > max_x)624 goto out;625626 render_glyph(buf->pix[0], glyph, x, y, &render->pix_prompt_color);627 x += glyph->advance.x;
13 unmodified lines
641 fcft_kerning(font, prev, wc, &x_kern, NULL);642643 x += x_kern;644519 if (x + glyph->width >= max_x)645 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);646 if (x + pixels_needed > max_x)647 goto out;648649 render_glyph(650 buf->pix[0], glyph, x, y, &render->pix_prompt_color);
3 unmodified lines
654 prev = wc;655 }656 }657658 /*659 * Adjust glyph offset (i.e. where in the input string we start660 * rendering), to ensure the portion containing the cursor is661 * always visible.662 */663 adjust_input_glyph_offset(664 render, prompt, !use_placeholder ? text_len : 0,665 input_glyphs, input_kerning, x, max_x);666667 /* Cursor, if right after the prompt. In all other cases, the668 * cursor will be rendered by the loop below */669 if (cursor_location == render->input_glyph_offset533 if (cursor_location == 0 || (conf->password_mode.enabled &&534 conf->password_mode.character == U'\0'))670 || (conf->password_mode.enabled &&671 conf->password_mode.character == U'\0'))672 {673 render_cursor(render, x, y, buf->pix[0]);674 }675676 if (input_run != NULL && !(conf->password_mode.enabled && !use_placeholder)) {677 /* We only shape the placeholder string */678 assert(use_placeholder);679540 for (size_t i = 0; i < input_run->count; i++) {680 for (size_t i = render->input_glyph_offset; i < input_run->count; i++) {681 const struct fcft_glyph *glyph = input_run->glyphs[i];542 if (x + glyph->width > max_x)682 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);683684 if (x + pixels_needed > max_x) {685 goto out;686 }687688 render_glyph(buf->pix[0], glyph, x, y,689 use_placeholder690 ? &render->pix_placeholder_color
13 unmodified lines
704 }705 } else {706 char32_t prev = 0;707565 for (size_t i = 0; i < text_len; i++) {708 for (size_t i = render->input_glyph_offset; i < text_len; i++) {709 if (conf->password_mode.enabled &&710 conf->password_mode.character == U'\0' &&711 !use_placeholder)712 {713 continue;714 }715573 char32_t wc = conf->password_mode.enabled && !use_placeholder574 ? conf->password_mode.character575 : ptext[i];716 const struct fcft_glyph *glyph = input_glyphs[i];717577 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(578 font, wc, subpixel);579718 if (glyph == NULL) {581 prev = wc;719 prev = 0;720 continue;721 }722723 long x_kern;586 fcft_kerning(font, prev, wc, &x_kern, NULL);724 fcft_kerning(font, prev, glyph->cp, &x_kern, NULL);725726 x += x_kern;727590 if (x + glyph->width >= max_x)728 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);729730 if (x + pixels_needed > max_x)731 goto out;732733 render_glyph(734 buf->pix[0], glyph, x, y,
6 unmodified lines
741 /* Cursor */742 if (cursor_location > 0 && cursor_location - 1 == i)743 render_cursor(render, x, y, buf->pix[0]);744605 prev = wc;745 prev = glyph->cp;746 }747 }748749out:
86 unmodified lines
836 break;837 }838 }839700 if (x + (kern != NULL ? kern[i] : 0) + glyphs[i]->advance.x >= max_x) {840 if (x + (kern != NULL ? kern[i] : 0) + glyphs[i]->advance.x > max_x) {841 const struct fcft_glyph *ellipses =842 fcft_rasterize_char_utf32(font, U'…', subpixel);843844 if (ellipses != NULL)
1008 unmodified lines