From 8e376f59f9f7b4c54527550fc0a7afa171467c63 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sun, 11 Aug 2024 13:44:08 +0200 Subject: UTF-8 --- examples/ui.c | 94 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 30 deletions(-) (limited to 'examples') diff --git a/examples/ui.c b/examples/ui.c index d853c4b..cd355c5 100755 --- a/examples/ui.c +++ b/examples/ui.c @@ -159,6 +159,10 @@ typedef struct { typedef i64 (*Thread_Proc)(void *user_data); +// UTF-8 +c32 read_utf8(i64 len, c8 *s); +i32 write_utf8(c32 c, c8 *buffer); + // Window void p_init(void); void p_cleanup(void); @@ -257,7 +261,7 @@ b8 char_column_empty(c32 c, i64 column_index) { i64 char_width(c32 c) { if (c < 32) return 0; - if (c == ' ') + if (c == ' ' || c > 127) return 4; i64 width = 0; @@ -702,6 +706,64 @@ i32 main(i32 argc, c8 **argv) { #include #include +c32 read_utf8(i64 len, c8 *s) { + if (len <= 1 && + (s[0] & 0x80) == 0) + return s[0]; + if (len <= 2 && + (s[0] & 0xe0) == 0xc0 && + (s[1] & 0xc0) == 0x80) + return (s[1] & 0x3f) + | ((s[0] & 0x1f) << 6); + if (len <= 3 && + (s[0] & 0xf0) == 0xe0 && + (s[1] & 0xc0) == 0x80 && + (s[2] & 0xc0) == 0x80) + return (s[2] & 0x3f) + | ((s[1] & 0x3f) << 6) + | ((s[0] & 0x0f) << 12); + if (len <= 4 && + (s[0] & 0xf8) == 0xf0 && + (s[1] & 0xc0) == 0x80 && + (s[2] & 0xc0) == 0x80 && + (s[3] & 0xc0) == 0x80) + return (s[3] & 0x3f) + | ((s[2] & 0x3f) << 6) + | ((s[1] & 0x3f) << 12) + | ((s[0] & 0x07) << 18); + return 0; +} + +i32 write_utf8(c32 c, c8 *buffer) { + if ((c & 0x7f) == c) { + buffer[0] = (c8) c; + return 1; + } + + if ((c & 0x7ff) == c) { + buffer[0] = 0xc0 | ((c >> 6) & 0x1f); + buffer[1] = 0x80 | ( c & 0x3f); + return 2; + } + + if ((c & 0xffff) == c) { + buffer[0] = 0xc0 | ((c >> 12) & 0x0f); + buffer[1] = 0x80 | ((c >> 6) & 0x3f); + buffer[2] = 0x80 | ( c & 0x3f); + return 3; + } + + if ((c & 0x1fffff) == c) { + buffer[0] = 0xc0 | ((c >> 18) & 0x07); + buffer[1] = 0x80 | ((c >> 12) & 0x3f); + buffer[2] = 0x80 | ((c >> 6) & 0x3f); + buffer[3] = 0x80 | ( c & 0x3f); + return 4; + } + + return 0; +} + // ================================================================ // // UDP sockets @@ -917,34 +979,6 @@ void p_cleanup(void) { XCloseDisplay (_display); } -c32 c32_from_utf8(i64 len, c8 *s) { - if (len <= 1 && - (s[0] & 0x80) == 0) - return s[0]; - if (len <= 2 && - (s[0] & 0xe0) == 0xc0 && - (s[1] & 0xc0) == 0x80) - return (s[1] & 0x3f) - | ((s[0] & 0x1f) << 6); - if (len <= 3 && - (s[0] & 0xf0) == 0xe0 && - (s[1] & 0xc0) == 0x80 && - (s[2] & 0xc0) == 0x80) - return (s[2] & 0x3f) - | ((s[1] & 0x3f) << 6) - | ((s[0] & 0x0f) << 12); - if (len <= 4 && - (s[0] & 0xf8) == 0xf0 && - (s[1] & 0xc0) == 0x80 && - (s[2] & 0xc0) == 0x80 && - (s[3] & 0xc0) == 0x80) - return (s[3] & 0x3f) - | ((s[2] & 0x3f) << 6) - | ((s[1] & 0x3f) << 12) - | ((s[0] & 0x07) << 18); - return 0; -} - i32 p_handle_events(void) { i32 num_events = 0; @@ -1027,7 +1061,7 @@ i32 p_handle_events(void) { c8 buf[16]; i32 len = Xutf8LookupString(_ic, &ev.xkey, buf, sizeof buf - 1, NULL, NULL); if (len > 0) - platform.input[platform.input_size++] = (Input_Key) { .key = k, .c = c32_from_utf8(len, buf), }; + platform.input[platform.input_size++] = (Input_Key) { .key = k, .c = read_utf8(len, buf), }; } } } break; -- cgit v1.2.3