From 62e07743194c8051334ed9b23be90ce31ab80773 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 May 2020 11:03:22 +0200 Subject: unicode-combining: store seen combining chains "globally" in the term struct Instead of storing combining data per cell, realize that most combinations are re-occurring and that there's lots of available space left in the unicode range, and store seen base+combining combinations chains in a per-terminal array. When we encounter a combining character, we first try to pre-compose, like before. If that fails, we then search for the current base+combining combo in the list of previously seen combinations. If not found there either, we allocate a new combo and add it to the list. Regardless, the result is an index into this array. We store this index, offsetted by COMB_CHARS_LO=0x40000000ul in the cell. When rendering, we need to check if the cell character is a plain character, or if it's a composed character (identified by checking if the cell character is >= COMB_CHARS_LO). Then we render the grapheme pretty much like before. --- terminal.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index 2292e927..2767060b 100644 --- a/terminal.h +++ b/terminal.h @@ -77,21 +77,16 @@ struct damage { int lines; }; -#if FOOT_UNICODE_MAX_COMBINING_CHARS > 0 -struct combining_chars { +struct composed { + wchar_t base; + wchar_t combining[5]; uint8_t count; - wchar_t chars[FOOT_UNICODE_MAX_COMBINING_CHARS]; -} __attribute__((packed)); -#endif +}; struct row { struct cell *cells; bool dirty; bool linebreak; - -#if FOOT_UNICODE_MAX_COMBINING_CHARS > 0 - struct combining_chars *comb_chars; -#endif }; struct sixel { @@ -221,6 +216,10 @@ struct terminal { struct grid alt; struct grid *grid; + #define COMB_CHARS_LO 0x40000000ul + size_t composed_count; + struct composed *composed; + struct fcft_font *fonts[4]; int font_dpi; int font_adjustments; -- cgit v1.3 From 4d4df92f661c6756cffd2ab6bc7983c3f1ced8c2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 May 2020 11:31:59 +0200 Subject: unicode-combining: limit maximum number of allowed composed chains --- terminal.h | 1 + vt.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index 2767060b..395557db 100644 --- a/terminal.h +++ b/terminal.h @@ -217,6 +217,7 @@ struct terminal { struct grid *grid; #define COMB_CHARS_LO 0x40000000ul + #define COMB_CHARS_HI 0x400ffffful size_t composed_count; struct composed *composed; diff --git a/vt.c b/vt.c index 4ceaa25b..157f7a9c 100644 --- a/vt.c +++ b/vt.c @@ -681,12 +681,19 @@ action_utf8_print(struct terminal *term, uint8_t c) new_cc.combining[i] = composed->combining[i]; new_cc.combining[wanted_count - 1] = wc; - term->composed_count++; - term->composed = realloc(term->composed, term->composed_count * sizeof(term->composed[0])); - term->composed[term->composed_count - 1] = new_cc; + if (term->composed_count < COMB_CHARS_HI) { + term->composed_count++; + term->composed = realloc(term->composed, term->composed_count * sizeof(term->composed[0])); + term->composed[term->composed_count - 1] = new_cc; - term_print(term, COMB_CHARS_LO + term->composed_count - 1, base_width); - return; + term_print(term, COMB_CHARS_LO + term->composed_count - 1, base_width); + return; + } else { + /* We reached our maximum number of allowed composed + * character chains. Fall through here and print the + * current zero-width character to the current cell */ + LOG_WARN("maximum number of composed characters reached"); + } } } -- cgit v1.3