aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2019-08-23 17:23:40 +0200
committerGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2019-08-23 17:23:40 +0200
commitb714b6125d19c59c3303fc20d1c99b56dae83d08 (patch)
tree6f722005a780fee3dd2bd5ec6c32ab9e0d084901
parent14d3edeaaf1573bc033e1caace67c7a05fdcf6f9 (diff)
downloadd2tk-b714b6125d19c59c3303fc20d1c99b56dae83d08.zip
d2tk-b714b6125d19c59c3303fc20d1c99b56dae83d08.tar.gz
d2tk-b714b6125d19c59c3303fc20d1c99b56dae83d08.tar.bz2
d2tk-b714b6125d19c59c3303fc20d1c99b56dae83d08.tar.xz
base: add initial support for UTF-8 in text field.
-rw-r--r--VERSION2
-rw-r--r--src/base.c48
2 files changed, 32 insertions, 18 deletions
diff --git a/VERSION b/VERSION
index 1d8ce09..ed58b54 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.1.793
+0.1.799
diff --git a/src/base.c b/src/base.c
index 0d65670..5059f73 100644
--- a/src/base.c
+++ b/src/base.c
@@ -24,6 +24,8 @@
#include <d2tk/hash.h>
#include "core_internal.h"
+#include <utf8.h/utf8.h>
+
#define _D2TK_MAX_ATOM 0x1000
#define _D2TK_MASK_ATOMS (_D2TK_MAX_ATOM - 1)
@@ -2532,8 +2534,6 @@ d2tk_base_text_field(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect,
if(base->keys.nchars)
{
- const size_t len = strlen(value);
-
if(base->keys.nchars == 1)
{
switch(base->keys.chars[0])
@@ -2545,7 +2545,16 @@ d2tk_base_text_field(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect,
} break;
case 0x08:
{
- value[len-1] = '\0';
+ const ssize_t ulen = utf8len(value) - 1;
+ char *head = value;
+ utf8_int32_t codepoint;
+
+ for(ssize_t i = 0; i < ulen; i++)
+ {
+ head = utf8codepoint(head, &codepoint);
+ }
+
+ head[0] = '\0';
_d2tk_base_clear_chars(base); // eat key
} break;
}
@@ -2553,26 +2562,31 @@ d2tk_base_text_field(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect,
if(base->keys.nchars)
{
- if(len + base->keys.nchars < maxlen)
+ const char *head = base->keys.chars;
+ const ssize_t ulen = utf8len(head);
+
+ const ssize_t len = strlen(value);
+ char *tail = &value[len];
+
+ utf8_int32_t codepoint;
+
+ for(ssize_t i = 0; i < ulen; i++)
{
- if(accept) // only add valid characters
+ head = utf8codepoint(head, &codepoint);
+
+ if(accept && !utf8chr(accept, codepoint))
{
- char *dst = &value[len];
- const char *src = (const char *)base->keys.chars;
- for(src = strpbrk(src, accept);
- src;
- src = strpbrk(src+1, accept))
- {
- *dst++ = *src;
- }
- *dst = '\0';
+ continue;
}
- else
+
+ const ssize_t left = maxlen - (tail - value);
+ if(left > 0)
{
- sprintf(&value[len], "%s", (const char *)base->keys.chars);
+ tail = utf8catcodepoint(tail, codepoint, left);
}
- _d2tk_base_clear_chars(base); // eat key
}
+
+ _d2tk_base_clear_chars(base); // eat key
}
}