- added libdrawtext
[demo_prior] / libs / drawtext / src / utf8.c
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "drawtext.h"
19
20 #define U8_IS_FIRST(x)          (((((x) >> 7) & 1) == 0) || ((((x) >> 6) & 3) == 3))
21
22 static const char first_mask[] = {
23         0,
24         0x7f,   /* single byte, 7 bits valid */
25         0x1f,   /* two-bytes, 5 bits valid */
26         0xf,    /* three-bytes, 4 bits valid */
27         0x7             /* four-bytes, 3 bits valid */
28 };
29 static const char first_shift[] = { 0, 7, 5, 4, 3 };    /* see above */
30
31 #define CONT_PREFIX     0x80
32 #define CONT_MASK       0x3f
33 #define CONT_SHIFT      6
34
35 /* last charcodes for 1, 2, 3 or 4-byte utf8 chars */
36 static const int utf8_lastcode[] = { 0x7f, 0x7ff, 0xfff, 0x1fffff };
37
38 #define prefix_mask(x)  (~first_mask[x])
39 #define prefix(x)               ((prefix_mask(x) << 1) & 0xff)
40
41
42 char *dtx_utf8_next_char(char *str)
43 {
44         return str + dtx_utf8_nbytes(str);
45 }
46
47 char *dtx_utf8_prev_char(char *ptr, char *first)
48 {
49         do {
50                 --ptr;
51         } while(!U8_IS_FIRST(*ptr) && ptr > first);
52         return ptr;
53 }
54
55 int dtx_utf8_char_code(const char *str)
56 {
57         int i, nbytes, shift, code = 0;
58         int mask;
59
60         if(!U8_IS_FIRST(*str)) {
61                 return -1;
62         }
63
64         nbytes = dtx_utf8_nbytes(str);
65         mask = first_mask[nbytes];
66         shift = 0;
67
68         for(i=0; i<nbytes; i++) {
69                 if(!*str) {
70                         break;
71                 }
72
73                 code = (code << shift) | (*str++ & mask);
74                 mask = 0x3f;
75                 shift = 6;
76         }
77         return code;
78 }
79
80 int dtx_utf8_nbytes(const char *str)
81 {
82         int i, numset = 0;
83         int c = *str;
84
85         if(!U8_IS_FIRST(c)) {
86                 for(i=0; !U8_IS_FIRST(str[i]); i++);
87                 return i;
88         }
89
90         /* count the leading 1s */
91         for(i=0; i<4; i++) {
92                 if(((c >> (7 - i)) & 1) == 0) {
93                         break;
94                 }
95                 numset++;
96         }
97
98         if(!numset) {
99                 return 1;
100         }
101         return numset;
102 }
103
104 int dtx_utf8_char_count(const char *str)
105 {
106         int n = 0;
107
108         while(*str) {
109                 ++n;
110                 str = dtx_utf8_next_char((char*)str);
111         }
112         return n;
113 }
114
115 int dtx_utf8_char_count_range(const char *str, int nbytes)
116 {
117         int n = 0;
118         while(*str && nbytes > 0) {
119                 char *next = dtx_utf8_next_char((char*)str);
120                 ++n;
121                 nbytes -= next - str;
122                 str = next;
123         }
124         return (nbytes < 0 && n > 0) ? n - 1 : n;
125 }
126
127 size_t dtx_utf8_from_char_code(int code, char *buf)
128 {
129         size_t nbytes = 0;
130         int i;
131
132         for(i=0; i<4; i++) {
133                 if(code <= utf8_lastcode[i]) {
134                         nbytes = i + 1;
135                         break;
136                 }
137         }
138
139         if(!nbytes && buf) {
140                 for(i=0; i<(int)nbytes; i++) {
141                         int idx = nbytes - i - 1;
142                         int mask, shift, prefix;
143
144                         if(idx > 0) {
145                                 mask = CONT_MASK;
146                                 shift = CONT_SHIFT;
147                                 prefix = CONT_PREFIX;
148                         } else {
149                                 mask = first_mask[nbytes];
150                                 shift = first_shift[nbytes];
151                                 prefix = prefix(nbytes);
152                         }
153
154                         buf[idx] = (code & mask) | (prefix & ~mask);
155                         code >>= shift;
156                 }
157         }
158         return nbytes;
159 }
160
161 size_t dtx_utf8_from_string(const wchar_t *str, char *buf)
162 {
163         size_t nbytes = 0;
164         char *ptr = buf;
165
166         while(*str) {
167                 int cbytes = dtx_utf8_from_char_code(*str++, ptr);
168                 if(ptr) {
169                         ptr += cbytes;
170                 }
171                 nbytes += cbytes;
172         }
173         return nbytes;
174 }