Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_utf8.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * UTF-8
7 *
8 * ===============================================================*/
9NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
10NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
11NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
12NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
13
14NK_INTERN int
15nk_utf_validate(nk_rune *u, int i)
16{
17 NK_ASSERT(u);
18 if (!u) return 0;
19 if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
20 NK_BETWEEN(*u, 0xD800, 0xDFFF))
21 *u = NK_UTF_INVALID;
22 for (i = 1; *u > nk_utfmax[i]; ++i);
23 return i;
24}
25NK_INTERN nk_rune
26nk_utf_decode_byte(char c, int *i)
27{
28 NK_ASSERT(i);
29 if (!i) return 0;
30 for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
31 if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
32 return (nk_byte)(c & ~nk_utfmask[*i]);
33 }
34 return 0;
35}
36NK_API int
37nk_utf_decode(const char *c, nk_rune *u, int clen)
38{
39 int i, j, len, type=0;
40 nk_rune udecoded;
41
42 NK_ASSERT(c);
43 NK_ASSERT(u);
44
45 if (!c || !u) return 0;
46 if (!clen) return 0;
47 *u = NK_UTF_INVALID;
48
49 udecoded = nk_utf_decode_byte(c[0], &len);
50 if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
51 return 1;
52
53 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
54 udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
55 if (type != 0)
56 return j;
57 }
58 if (j < len)
59 return 0;
60 *u = udecoded;
61 nk_utf_validate(u, len);
62 return len;
63}
64NK_INTERN char
65nk_utf_encode_byte(nk_rune u, int i)
66{
67 return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
68}
69NK_API int
70nk_utf_encode(nk_rune u, char *c, int clen)
71{
72 int len, i;
73 len = nk_utf_validate(&u, 0);
74 if (clen < len || !len || len > NK_UTF_SIZE)
75 return 0;
76
77 for (i = len - 1; i != 0; --i) {
78 c[i] = nk_utf_encode_byte(u, 0);
79 u >>= 6;
80 }
81 c[0] = nk_utf_encode_byte(u, len);
82 return len;
83}
84NK_API int
85nk_utf_len(const char *str, int len)
86{
87 const char *text;
88 int glyphs = 0;
89 int text_len;
90 int glyph_len;
91 int src_len = 0;
92 nk_rune unicode;
93
94 NK_ASSERT(str);
95 if (!str || !len) return 0;
96
97 text = str;
98 text_len = len;
99 glyph_len = nk_utf_decode(text, &unicode, text_len);
100 while (glyph_len && src_len < len) {
101 glyphs++;
102 src_len = src_len + glyph_len;
103 glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
104 }
105 return glyphs;
106}
107NK_API const char*
108nk_utf_at(const char *buffer, int length, int index,
109 nk_rune *unicode, int *len)
110{
111 int i = 0;
112 int src_len = 0;
113 int glyph_len = 0;
114 const char *text;
115 int text_len;
116
117 NK_ASSERT(buffer);
118 NK_ASSERT(unicode);
119 NK_ASSERT(len);
120
121 if (!buffer || !unicode || !len) return 0;
122 if (index < 0) {
123 *unicode = NK_UTF_INVALID;
124 *len = 0;
125 return 0;
126 }
127
128 text = buffer;
129 text_len = length;
130 glyph_len = nk_utf_decode(text, unicode, text_len);
131 while (glyph_len) {
132 if (i == index) {
133 *len = glyph_len;
134 break;
135 }
136
137 i++;
138 src_len = src_len + glyph_len;
139 glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
140 }
141 if (i != index) return 0;
142 return buffer + src_len;
143}
144
main API and documentation file
#define NK_UTF_INVALID
internal invalid utf8 rune
Definition nuklear.h:21
#define NK_UTF_SIZE
describes the number of bytes a glyph consists of
Definition nuklear.h:22