aboutsummaryrefslogtreecommitdiff
path: root/vis-menu.c
diff options
context:
space:
mode:
Diffstat (limited to 'vis-menu.c')
-rw-r--r--vis-menu.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/vis-menu.c b/vis-menu.c
index 444a26a..66e23e3 100644
--- a/vis-menu.c
+++ b/vis-menu.c
@@ -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);