diff options
| author | Silvan Jegen <s.jegen@gmail.com> | 2021-04-10 13:40:51 +0200 |
|---|---|---|
| committer | Randy Palamar <palamar@ualberta.ca> | 2023-08-14 08:39:56 -0600 |
| commit | 8ae019ffc34b98bcd8546598de6037addaa29a4f (patch) | |
| tree | cfdc1271f0bc099dda562ea14d0dcdd5eef4980c /vis-menu.c | |
| parent | adc803f24eec316dd6caf05f6a839ec08c325a38 (diff) | |
| download | vis-8ae019ffc34b98bcd8546598de6037addaa29a4f.tar.gz vis-8ae019ffc34b98bcd8546598de6037addaa29a4f.tar.xz | |
vis-menu: try to preserve valid Unicode points
Before we were not taking non-ascii characters into account properly. With
this patch we still mix byte counts and "grapheme cluster" (i.e. complete
glyphs that are rendered in a terminal cell) counts but the code should
be less broken in the more common case now.
Diffstat (limited to 'vis-menu.c')
| -rw-r--r-- | vis-menu.c | 40 |
1 files changed, 37 insertions, 3 deletions
@@ -84,12 +84,46 @@ appenditem(Item *item, Item **list, Item **last) { static size_t textwn(const char *s, int l) { - int b, c; /* bytes and UTF-8 characters */ + int c; - for(b=c=0; s && s[b] && (l<0 || b<l); b++) if((s[b] & 0xc0) != 0x80) c++; + for (c=0; s && s[c] && (l<0 || c<l); c++); return c+4; /* Accomodate for the leading and trailing spaces */ } +/* + * textvalidn returns the highest amount of bytes <= l of string s that + * only contains valid Unicode points. This is used to make sure we don't + * cut off any valid UTF-8-encoded unicode point in case there is not + * enough space to render the whole text string. +*/ +static ssize_t +textvalidn(const char *s, int l) { + int c, utfcharbytes; /* byte count and UTF-8 codepoint length */ + + for (c=0; s && s[c] && (l<0 || c<l); ) { + utfcharbytes = 0; + if ((s[c] & 0x80) == 0) { + utfcharbytes = 1; + } else if ((s[c] & 0xf0) == 0xf0) { + utfcharbytes = 4; + } else if ((s[c] & 0xf0) == 0xe0) { + utfcharbytes = 3; + } else if ((s[c] & 0xe0) == 0xc0) { + utfcharbytes = 2; + } else { + return -1; + } + + if ((l>0 && c + utfcharbytes >= l)) { + break; + } + + c += utfcharbytes; + } + + return c; +} + static size_t textw(const char *s) { return textwn(s, -1); @@ -149,7 +183,7 @@ drawtext(const char *t, size_t w, Color col) { buf[tw] = '\0'; memcpy(buf, t, MIN(strlen(t), tw)); if (textw(t) > w) /* Remember textw returns the width WITH padding */ - for (i = MAX((tw-4), 0); i < tw; i++) buf[i] = '.'; + for (i = MAX(textvalidn(t, w-4), 0); i < tw; i++) buf[i] = '.'; fprintf(stderr, "%s %s %s", prestr, buf, poststr); free(buf); |
