1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/* Pango
* utils.c:
*
* Copyright (C) 1999 Red Hat Software
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "utils.h"
#include <iconv.h>
#include <errno.h>
#include <unicode.h>
gboolean
_pango_utf8_iterate (gchar *cur, char **next, GUChar4 *wc_out)
{
gchar *p = cur;
gchar c = *p;
GUChar4 wc;
gint length;
if ((c & 0x80) == 0)
{
length = 1;
wc = c;
}
else if ((c & 0xc0) == 0x80)
{
return FALSE;
}
else if ((c & 0xe0) == 0xc0)
{
length = 2;
wc = c & 0x1f;
}
else if ((c & 0xf0) == 0xe0)
{
length = 3;
wc = c & 0x0f;
}
else
return FALSE;
p++;
while (--length > 0)
{
if (*p == 0) /* Incomplete character */
{
if (next)
*next = cur;
if (wc_out)
*wc_out = 0;
return TRUE;
}
if ((*p & 0xc0) != 0x80)
return FALSE;
wc <<= 6;
wc |= (*p) & 0x3f;
p++;
}
if (wc_out)
*wc_out = wc;
if (next)
*next = p;
return TRUE;
}
gint
_pango_utf8_len (gchar *str, gint limit)
{
gchar *cur = str;
gchar *next;
gint len = 0;
while (*cur)
{
if (!_pango_utf8_iterate (cur, &next, NULL))
return -1;
if (cur == next)
break;
if (limit >= 0 && (next - str) > limit)
return len;
cur = next;
len++;
}
return len;
}
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define UCS2_CHARSET "UNICODELITTLE"
#else
#define UCS2_CHARSET "UNICODE"
#endif
GUChar2 *
_pango_utf8_to_ucs2 (gchar *str, gint len)
{
iconv_t cd;
gchar *outbuf, *result;
gchar *inbuf;
size_t inbytesleft;
size_t outbytesleft;
gint outlen;
gint count;
cd = iconv_open (UCS2_CHARSET, "UTF8");
if (cd == (iconv_t)-1)
g_error ("No converter from UTF8 to " UCS2_CHARSET);
if (len < 0)
len = strlen (str);
outlen = unicode_strlen (str, len) * sizeof(GUChar2);
result = g_malloc (outlen);
inbuf = str;
inbytesleft = len;
outbuf = result;
outbytesleft = outlen;
count = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (count < 0 && (errno != E2BIG))
{
g_free (result);
result = NULL;
}
iconv_close (cd);
return (GUChar2 *)result;
}
|