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. --- vt.c | 59 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 16 deletions(-) (limited to 'vt.c') diff --git a/vt.c b/vt.c index 7678711e..74110eb6 100644 --- a/vt.c +++ b/vt.c @@ -571,8 +571,6 @@ action_utf8_print(struct terminal *term, uint8_t c) int width = wcwidth(wc); -#if FOOT_UNICODE_MAX_COMBINING_CHARS > 0 - /* * Is this is combining character? The basic assumption is that if * wcwdith() returns 0, then it *is* a combining character. @@ -606,6 +604,15 @@ action_utf8_print(struct terminal *term, uint8_t c) base = row->cells[base_col].wc; } + const struct composed *composed = + (base >= COMB_CHARS_LO && + base < (COMB_CHARS_LO + term->composed_count)) + ? &term->composed[base - COMB_CHARS_LO] + : NULL; + + if (composed != NULL) + base = composed->base; + int base_width = wcwidth(base); if (base != 0 && base_width > 0) { @@ -619,35 +626,55 @@ action_utf8_print(struct terminal *term, uint8_t c) * pre-composed character, as that is likely to produce a * better looking result. */ - - struct combining_chars *comb_chars = &row->comb_chars[base_col]; + term->grid->cursor.point.col = base_col; + term->grid->cursor.lcf = false; #if FOOT_UNICODE_PRECOMPOSE - if (comb_chars->count == 0) { + if (composed == NULL) { wchar_t precomposed = precompose(base, wc); int precomposed_width = wcwidth(precomposed); if (precomposed != (wchar_t)-1 && precomposed_width == base_width) { - term->grid->cursor.point.col = base_col; - term->grid->cursor.lcf = false; term_print(term, precomposed, precomposed_width); return; } } #endif - if (comb_chars->count < ALEN(comb_chars->chars)) - comb_chars->chars[comb_chars->count++] = wc; - else { - LOG_WARN("combining character overflow:"); - LOG_WARN(" 0x%04x", base); - for (size_t i = 0; i < comb_chars->count; i++) - LOG_WARN(" 0x%04x", comb_chars->chars[i]); - LOG_ERR(" 0x%04x", wc); + size_t wanted_count = composed != NULL ? composed->count + 1 : 1; + + /* Look for existing combining chain */ + for (size_t i = 0; i < term->composed_count; i++) { + const struct composed *cc = &term->composed[i]; + if (cc->base != base) + continue; + + if (cc->count != wanted_count) + continue; + + if (cc->combining[wanted_count - 1] != wc) + continue; + + term_print(term, COMB_CHARS_LO + i, base_width); + return; } + + /* Allocate new chain */ + + struct composed new_cc; + new_cc.base = base; + new_cc.count = wanted_count; + for (size_t i = 0; i < wanted_count - 1; i++) + 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; + + term_print(term, COMB_CHARS_LO + term->composed_count - 1, base_width); return; } } -#endif /* FOOT_UNICODE_MAX_COMBINING_CHARS > 0 */ term_print(term, wc, width); } -- cgit v1.3 From 1ebdc0116263c2ac8f5b4de98c8bfe0aa768480b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 May 2020 11:27:06 +0200 Subject: unicode-combining: detect when we've reached the chain limit We currently store up to 5 combining characters in any given base+combining chain. This adds a check for when that limit is about to be exceeded. When this happens, we log the chain + the new combining character. Since things will break anyway, we simply overwrite the last combining character. --- vt.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'vt.c') diff --git a/vt.c b/vt.c index 74110eb6..4ceaa25b 100644 --- a/vt.c +++ b/vt.c @@ -641,6 +641,20 @@ action_utf8_print(struct terminal *term, uint8_t c) #endif size_t wanted_count = composed != NULL ? composed->count + 1 : 1; + if (wanted_count > ALEN(composed->combining)) { + assert(composed != NULL); + + LOG_WARN("combining character overflow:"); + LOG_WARN(" base: 0x%04x", composed->base); + for (size_t i = 0; i < composed->count; i++) + LOG_WARN(" cc: 0x%04x", composed->combining[i]); + LOG_ERR(" new: 0x%04x", wc); + + /* This are going to break anyway... */ + wanted_count--; + } + + assert(wanted_count <= ALEN(composed->combining)); /* Look for existing combining chain */ for (size_t i = 0; i < term->composed_count; i++) { -- 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 'vt.c') 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