Skip to content
Myr

render: font-shape the input string

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

This means we now render e.g. ZWJ sequences as single glyphs, rather than the two, three glyphs they're made up of. It also means we render e.g. RTL scripts (like arabic and hebrew) correctly. Update cursor handling (i.e. where to render the cursor) to use the cluster information from fcft. This means cursor movement around multi codepoint glyphs will feel a bit odd (for example, you'll have to press arrow keys multiple times to actually move past the multi codepoint glyph). Oh, and we don't _actually_ support RTL - editing will be LTR, and I fully expect editing RTL input to be an awkward experience. This is sort of an best-effort attempt to improve support for more scripts. Full support for RTL editing is *not* something that is planned. Closes #398

authored by Daniel Eklöf · committed by Daniel Eklöf · parent 01471e3 · Browse files · 4 files+166 added−62 removed
This commit

State

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

Diff stats

4 files+166 added−62 removed

  • other:+166 added−62 removed

4 files+166 added−62 removed

modified:CHANGELOG.md+3 added
119 unmodified lines
120 `586e75ff` (_base01_ in the solarized palette).121* Initial application sorting is now done case insensitive.122* Background is no longer transparent by default. You can change this123 by setting `colors.background`.124* Search string is now font shaped, improving support for e.g Arabic125 ([#398][398]).126127[339]: https://codeberg.org/dnkl/fuzzel/issues/339128[81]: https://codeberg.org/dnkl/fuzzel/issues/81129[398]: https://codeberg.org/dnkl/fuzzel/issues/398130131132### Deprecated133### Removed
672 unmodified lines
modified:main.c+1 added
418 unmodified lines
419 struct context *ctx = data;420 const struct config *conf = ctx->conf;421422 applications_flush_text_run_cache(ctx->apps);423 render_flush_text_run_cache(ctx->render);424425 mtx_lock(ctx->icon_lock);426 {427 ctx->icon_size = render_icon_size(ctx->render);
1552 unmodified lines
modified:render.c+160 added-61 removed
1#include "render.h"23#include <limits.h>4#include <pthread.h>5#include <semaphore.h>6#include <signal.h>7#include <stdio.h>
42 unmodified lines
50 struct fcft_font *font;51 struct fcft_font *font_bold;52 enum fcft_subpixel subpixel;5354 /* Cached fcft text runs */55 struct fcft_text_run *prompt_text_run;56 struct fcft_text_run *placeholder_text_run;5758 float scale;59 float dpi;60 bool size_font_by_dpi;61
286 unmodified lines
348 free(wtext);349 return width;350}351352static void353render_cursor(const struct render *render, int x, int y, pixman_image_t *pix)354{355 struct fcft_font *font = render->font;356357 if (true) {358 /* Bar cursor */359 pixman_image_fill_rectangles(360 PIXMAN_OP_SRC, pix, &render->pix_input_color,361 1, &(pixman_rectangle16_t){362 x,363 render->border_size + render->y_margin,364 font->underline.thickness,365 min(font->ascent + font->descent, render->row_height)});366 } else {367 /* TODO: future: underline cursor */368 pixman_image_fill_rectangles(369 PIXMAN_OP_SRC, pix, &render->pix_input_color,370 1, &(pixman_rectangle16_t){371 x, y - font->underline.position,372 font->max_advance.x,373 font->underline.thickness});374 }375}376377void348render_prompt(const struct render *render, struct buffer *buf,378render_prompt(struct render *render, struct buffer *buf,379 const struct prompt *prompt, const struct matches *matches)380{381 struct fcft_font *font = render->font;382 assert(font != NULL);
1 unmodified line
384 const struct config *conf = render->conf;385386 const char32_t *pprompt = prompt_prompt(prompt);387 size_t prompt_len = c32len(pprompt);388 const size_t cursor_location = prompt_cursor(prompt);389390 const char32_t *ptext = prompt_text(prompt);391 size_t text_len = c32len(ptext);392 bool use_placeholder = text_len == 0;
28 unmodified lines
421422 const int max_x =423 buf->width - render->border_size - render->x_margin - stats_width;424425 struct fcft_text_run *prompt_run = render->prompt_text_run;426 struct fcft_text_run *input_run =427 use_placeholder ? render->placeholder_text_run : NULL;428429 if (fcft_capabilities() & FCFT_CAPABILITY_TEXT_RUN_SHAPING) {430 if (prompt_run == NULL) {395 struct fcft_text_run *run = fcft_rasterize_text_run_utf32(396 font, prompt_len, pprompt, subpixel);431 prompt_run = fcft_rasterize_text_run_utf32(432 font, prompt_len, pprompt, subpixel);433 }434 if (input_run == NULL) {435 input_run = fcft_rasterize_text_run_utf32(436 font, text_len, ptext, subpixel);437 }438 }439398 if (run != NULL) {399 for (size_t i = 0; i < run->count; i++) {400 const struct fcft_glyph *glyph = run->glyphs[i];401 if (x + glyph->width > max_x)402 return;440 if (prompt_run != NULL) {441 for (size_t i = 0; i < prompt_run->count; i++) {442 const struct fcft_glyph *glyph = prompt_run->glyphs[i];443 if (x + glyph->width > max_x)444 goto out;445404 render_glyph(buf->pix[0], glyph, x, y, &render->pix_prompt_color);405 x += glyph->advance.x;406 }407 fcft_text_run_destroy(run);446 render_glyph(buf->pix[0], glyph, x, y, &render->pix_prompt_color);447 x += glyph->advance.x;448 }449 } else {450 char32_t prev = 0;451 for (size_t i = 0; i < prompt_len; i++) {452 const char32_t wc = pprompt[i];453 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(font, wc, subpixel);454409 /* Cursor, if right after the prompt. In all other cases,410 * the cursor will be rendered by the loop below */411 if (prompt_cursor(prompt) == 0) {412 pixman_image_fill_rectangles(413 PIXMAN_OP_SRC, buf->pix[0], &render->pix_input_color,414 1, &(pixman_rectangle16_t){415 x, y - font->ascent,416 font->underline.thickness, font->ascent + font->descent});455 if (glyph == NULL) {456 prev = wc;457 continue;458 }459419 /* Prevent loop below from rendering the prompt */;420 prompt_len = 0;460 long x_kern;461 fcft_kerning(font, prev, wc, &x_kern, NULL);462463 x += x_kern;464465 if (x + glyph->width >= max_x)466 goto out;467468 render_glyph(469 buf->pix[0], glyph, x, y, &render->pix_prompt_color);470 x += glyph->advance.x;471 x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);472473 prev = wc;474 }475 }476424 char32_t prev = 0;477 /* Cursor, if right after the prompt. In all other cases, the478 * cursor will be rendered by the loop below */479 if (cursor_location == 0 || (conf->password_mode.enabled &&480 conf->password_mode.character == U'\0'))481 {482 render_cursor(render, x, y, buf->pix[0]);483 }484426 for (size_t i = 0; i < prompt_len + text_len; i++) {427 if (i >= prompt_len &&428 conf->password_mode.enabled &&429 conf->password_mode.character == U'\0' &&430 !use_placeholder)431 {432 continue;485 if (input_run != NULL && !(conf->password_mode.enabled && !use_placeholder)) {486 for (size_t i = 0; i < input_run->count; i++) {487 const struct fcft_glyph *glyph = input_run->glyphs[i];488 if (x + glyph->width > max_x)489 goto out;490491 render_glyph(buf->pix[0], glyph, x, y,492 use_placeholder493 ? &render->pix_placeholder_color494 : &render->pix_input_color);495 x += glyph->advance.x;496497 /* Cursor */498 const int cur_cluster = input_run->cluster[i];499 const int next_cluster = i + 1 < input_run->count500 ? input_run->cluster[i + 1] : INT_MAX;501502 if (cursor_location > cur_cluster &&503 cursor_location <= next_cluster)504 {505 render_cursor(render, x, y, buf->pix[0]);506 }507 }508 } else {509 char32_t prev = 0;510435 char32_t wc = i < prompt_len436 ? pprompt[i]437 : (conf->password_mode.enabled && !use_placeholder511 for (size_t i = 0; i < text_len; i++) {512 if (conf->password_mode.enabled &&513 conf->password_mode.character == U'\0' &&514 !use_placeholder)515 {516 continue;517 }518519 char32_t wc = conf->password_mode.enabled && !use_placeholder520 ? conf->password_mode.character439 : ptext[i - prompt_len]);521 : ptext[i];522441 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(442 font, wc, subpixel);523 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(524 font, wc, subpixel);525444 if (glyph == NULL) {445 prev = wc;446 continue;447 }526 if (glyph == NULL) {527 prev = wc;528 continue;529 }530449 long x_kern;450 fcft_kerning(font, prev, wc, &x_kern, NULL);531 long x_kern;532 fcft_kerning(font, prev, wc, &x_kern, NULL);533452 x += x_kern;534 x += x_kern;535454 if (x + glyph->width >= max_x)455 return;536 if (x + glyph->width >= max_x)537 goto out;538457 render_glyph(458 buf->pix[0], glyph, x, y,459 use_placeholder460 ? &render->pix_placeholder_color461 : &render->pix_input_color);462 x += glyph->advance.x;463 if (i >= prompt_len)539 render_glyph(540 buf->pix[0], glyph, x, y,541 use_placeholder542 ? &render->pix_placeholder_color543 : &render->pix_input_color);544 x += glyph->advance.x;545 x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);546466 /* Cursor */467 if (prompt_cursor(prompt) + prompt_len - 1 == i) {468 pixman_image_fill_rectangles(469 PIXMAN_OP_SRC, buf->pix[0], &render->pix_input_color,470 1, &(pixman_rectangle16_t){471 x, y - font->ascent,472 font->underline.thickness, font->ascent + font->descent});473 }547 /* Cursor */548 if (cursor_location > 0 && cursor_location - 1 == i)549 render_cursor(render, x, y, buf->pix[0]);550475 prev = wc;551 prev = wc;552 }553 }554555out:556 if (render->prompt_text_run == NULL)557 render->prompt_text_run = prompt_run;558 else if (prompt_run != render->prompt_text_run)559 fcft_text_run_destroy(prompt_run);560 if (use_placeholder && render->placeholder_text_run == NULL)561 render->placeholder_text_run = input_run;562 else if (!use_placeholder || input_run != render->placeholder_text_run)563 fcft_text_run_destroy(input_run);564}565566static void567render_match_text(pixman_image_t *pix, double *_x, double _y, double max_x,
808 unmodified lines
1376 sem_destroy(&render->workers.done);1377 assert(tll_length(render->workers.queue) == 0);1378 tll_free(render->workers.queue);13791380 fcft_text_run_destroy(render->prompt_text_run);1381 fcft_text_run_destroy(render->placeholder_text_run);13821383 fcft_destroy(render->font);1384 free(render);1385}1386
19 unmodified lines
1406 }14071408 return row;1409}14101411void1412render_flush_text_run_cache(struct render *render)1413{1414 fcft_text_run_destroy(render->prompt_text_run);1415 fcft_text_run_destroy(render->placeholder_text_run);1416 render->prompt_text_run = NULL;1417 render->placeholder_text_run = NULL;1418}
modified:render.h+2 added-1 removed
17 unmodified lines
18bool render_set_font_and_update_sizes(19 struct render *render, struct fcft_font *font, struct fcft_font *font_bold,20 float scale, float dpi, bool size_font_by_dpi,21 int *new_width, int *new_height);22void render_flush_text_run_cache(struct render *render);2324void render_background(const struct render *render, struct buffer *buf);2526void render_prompt(26 const struct render *render, struct buffer *buf,27 struct render *render, struct buffer *buf,28 const struct prompt *prompt, const struct matches *matches);2930void render_match_list(31 struct render *render, struct buffer *buf,
7 unmodified lines