rev |
line source |
nuclear@0
|
1 #include "drawtext.h"
|
nuclear@0
|
2
|
nuclear@0
|
3 #define U8_IS_FIRST(x) (((((x) >> 7) & 1) == 0) || ((((x) >> 6) & 3) == 3))
|
nuclear@0
|
4
|
nuclear@0
|
5 static const char first_mask[] = {
|
nuclear@0
|
6 0,
|
nuclear@0
|
7 0x7f, /* single byte, 7 bits valid */
|
nuclear@0
|
8 0x1f, /* two-bytes, 5 bits valid */
|
nuclear@0
|
9 0xf, /* three-bytes, 4 bits valid */
|
nuclear@0
|
10 0x7 /* four-bytes, 3 bits valid */
|
nuclear@0
|
11 };
|
nuclear@3
|
12 static const char first_shift[] = { 0, 7, 5, 4, 3 }; /* see above */
|
nuclear@0
|
13
|
nuclear@0
|
14 #define CONT_PREFIX 0x80
|
nuclear@0
|
15 #define CONT_MASK 0x3f
|
nuclear@0
|
16 #define CONT_SHIFT 6
|
nuclear@0
|
17
|
nuclear@0
|
18 /* last charcodes for 1, 2, 3 or 4-byte utf8 chars */
|
nuclear@0
|
19 static const int utf8_lastcode[] = { 0x7f, 0x7ff, 0xfff, 0x1fffff };
|
nuclear@0
|
20
|
nuclear@0
|
21 #define prefix_mask(x) (~first_mask[x])
|
nuclear@0
|
22 #define prefix(x) ((prefix_mask(x) << 1) & 0xff)
|
nuclear@0
|
23
|
nuclear@0
|
24
|
nuclear@0
|
25 char *dtx_utf8_next_char(char *str)
|
nuclear@0
|
26 {
|
nuclear@0
|
27 return str + dtx_utf8_nbytes(str);
|
nuclear@0
|
28 }
|
nuclear@0
|
29
|
nuclear@0
|
30 int dtx_utf8_char_code(const char *str)
|
nuclear@0
|
31 {
|
nuclear@0
|
32 int i, nbytes, shift, code = 0;
|
nuclear@3
|
33 int mask;
|
nuclear@0
|
34
|
nuclear@0
|
35 if(!U8_IS_FIRST(*str)) {
|
nuclear@0
|
36 return -1;
|
nuclear@0
|
37 }
|
nuclear@0
|
38
|
nuclear@0
|
39 nbytes = dtx_utf8_nbytes(str);
|
nuclear@0
|
40 mask = first_mask[nbytes];
|
nuclear@0
|
41 shift = 0;
|
nuclear@0
|
42
|
nuclear@0
|
43 for(i=0; i<nbytes; i++) {
|
nuclear@0
|
44 if(!*str) {
|
nuclear@0
|
45 break;
|
nuclear@0
|
46 }
|
nuclear@0
|
47
|
nuclear@0
|
48 code = (code << shift) | (*str++ & mask);
|
nuclear@0
|
49 mask = 0x3f;
|
nuclear@3
|
50 shift = 6;
|
nuclear@0
|
51 }
|
nuclear@0
|
52 return code;
|
nuclear@0
|
53 }
|
nuclear@0
|
54
|
nuclear@0
|
55 int dtx_utf8_nbytes(const char *str)
|
nuclear@0
|
56 {
|
nuclear@0
|
57 int i, numset = 0;
|
nuclear@0
|
58 int c = *str;
|
nuclear@0
|
59
|
nuclear@0
|
60 if(!U8_IS_FIRST(c)) {
|
nuclear@0
|
61 for(i=0; !U8_IS_FIRST(str[i]); i++);
|
nuclear@0
|
62 return i;
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 /* count the leading 1s */
|
nuclear@0
|
66 for(i=0; i<4; i++) {
|
nuclear@0
|
67 if(((c >> (7 - i)) & 1) == 0) {
|
nuclear@0
|
68 break;
|
nuclear@0
|
69 }
|
nuclear@0
|
70 numset++;
|
nuclear@0
|
71 }
|
nuclear@0
|
72
|
nuclear@0
|
73 if(!numset) {
|
nuclear@0
|
74 return 1;
|
nuclear@0
|
75 }
|
nuclear@0
|
76 return numset;
|
nuclear@0
|
77 }
|
nuclear@0
|
78
|
nuclear@0
|
79 int dtx_utf8_char_count(const char *str)
|
nuclear@0
|
80 {
|
nuclear@0
|
81 int n = 0;
|
nuclear@0
|
82
|
nuclear@0
|
83 while(*str) {
|
nuclear@0
|
84 n++;
|
nuclear@0
|
85 str = dtx_utf8_next_char((char*)str);
|
nuclear@0
|
86 }
|
nuclear@0
|
87 return n;
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 size_t dtx_utf8_from_char_code(int code, char *buf)
|
nuclear@0
|
91 {
|
nuclear@0
|
92 size_t nbytes = 0;
|
nuclear@0
|
93 int i;
|
nuclear@0
|
94
|
nuclear@0
|
95 for(i=0; i<4; i++) {
|
nuclear@0
|
96 if(code <= utf8_lastcode[i]) {
|
nuclear@0
|
97 nbytes = i + 1;
|
nuclear@0
|
98 break;
|
nuclear@0
|
99 }
|
nuclear@0
|
100 }
|
nuclear@0
|
101
|
nuclear@0
|
102 if(!nbytes && buf) {
|
nuclear@0
|
103 for(i=0; i<nbytes; i++) {
|
nuclear@0
|
104 int idx = nbytes - i - 1;
|
nuclear@0
|
105 int mask, shift, prefix;
|
nuclear@0
|
106
|
nuclear@0
|
107 if(idx > 0) {
|
nuclear@0
|
108 mask = CONT_MASK;
|
nuclear@0
|
109 shift = CONT_SHIFT;
|
nuclear@0
|
110 prefix = CONT_PREFIX;
|
nuclear@0
|
111 } else {
|
nuclear@0
|
112 mask = first_mask[nbytes];
|
nuclear@0
|
113 shift = first_shift[nbytes];
|
nuclear@0
|
114 prefix = prefix(nbytes);
|
nuclear@0
|
115 }
|
nuclear@0
|
116
|
nuclear@0
|
117 buf[idx] = (code & mask) | (prefix & ~mask);
|
nuclear@0
|
118 code >>= shift;
|
nuclear@0
|
119 }
|
nuclear@0
|
120 }
|
nuclear@0
|
121 return nbytes;
|
nuclear@0
|
122 }
|
nuclear@0
|
123
|
nuclear@0
|
124 size_t dtx_utf8_from_string(const wchar_t *str, char *buf)
|
nuclear@0
|
125 {
|
nuclear@0
|
126 size_t nbytes = 0;
|
nuclear@0
|
127 char *ptr = buf;
|
nuclear@0
|
128
|
nuclear@0
|
129 while(*str) {
|
nuclear@0
|
130 int cbytes = dtx_utf8_from_char_code(*str++, ptr);
|
nuclear@0
|
131 if(ptr) {
|
nuclear@0
|
132 ptr += cbytes;
|
nuclear@0
|
133 }
|
nuclear@0
|
134 nbytes += cbytes;
|
nuclear@0
|
135 }
|
nuclear@0
|
136 return nbytes;
|
nuclear@0
|
137 }
|