ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/encoding.C
Revision: 1.20
Committed: Sun Sep 5 09:32:59 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_0, rel-4_1, rel-4_2, rel-4_3
Changes since 1.19: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*
2 * File: encoding.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
22
23 #include "../config.h"
24
25 #include "encoding.h"
26
27 #include <cstdlib>
28 #include <cstring>
29
30 const struct n2cs {
31 const char *name;
32 codeset cs;
33 } n2cs[] = {
34 /* first one found is the normalized one */
35 { "ISO88591", CS_ISO8859_1 },
36 { "ISO8859PRIMARY", CS_ISO8859_1 }, // some stupid fonts use this (hi tigert)
37 { "ISO88592", CS_ISO8859_2 },
38 { "ISO88593", CS_ISO8859_3 },
39 { "ISO88594", CS_ISO8859_4 },
40 { "ISO88595", CS_ISO8859_5 },
41 { "ISO88596", CS_ISO8859_6 },
42 { "ISO88597", CS_ISO8859_7 },
43 { "ISO88598", CS_ISO8859_8 },
44 { "ISO88599", CS_ISO8859_9 },
45 { "ISO885910", CS_ISO8859_10 },
46 { "ISO885911", CS_ISO8859_11 },
47 { "ISO885913", CS_ISO8859_13 },
48 { "ISO885914", CS_ISO8859_14 },
49 { "ISO885915", CS_ISO8859_15 },
50 { "FCD885915", CS_ISO8859_15 },
51 { "ISO885916", CS_ISO8859_16 },
52
53 { "TIS620*", CS_ISO8859_11 }, // close enough
54
55 { "ISO10646*", CS_UNICODE },
56 { "UNICODE", CS_UNICODE },
57 { "UTF8", CS_UNICODE },
58
59 { "ASCII", CS_US_ASCII },
60 { "USASCII", CS_US_ASCII },
61 { "ANSIX341968", CS_US_ASCII },
62
63 { "KOI8R", CS_KOI8_R },
64 { "GOST1976874*", CS_KOI8_R },
65 { "KOI8RU", CS_KOI8_U },
66 { "KOI8U", CS_KOI8_U },
67
68 { "VISCII*", CS_VISCII },
69
70 { "JISX0201*", CS_JIS0201_1976_0 },
71 { "JISC6226*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP)
72 { "JISX0208*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP)
73 { "JISX0212*", CS_JIS0212_1990_0 },
74 { "JISX021320001", CS_JIS0213_1 },
75 { "JISX021320002", CS_JIS0213_2 },
76 { "JISX0221*", CS_UNICODE }, // _very_ close
77
78 { "KSC5601*", CS_KSC5601_1987_0 },
79 { "KSX1001*", CS_KSC5601_1987_0 },
80 { "KSC5700*", CS_UNICODE }, // unicode plus extensions
81
82 { "BIG5P*", CS_BIG5_PLUS },
83 { "BIG5ETEN*", CS_BIG5_EXT },
84 { "BIG5*", CS_BIG5 },
85 { "GB2312*", CS_GB2312_1980_0 },
86 { "GB6345*", CS_GB2312_1980_0 }, // slightly different to gb2312??
87 { "GB8565*", CS_GB2312_1980_0 }, // a superset of gb2312??
88 { "GB13000*", CS_UNICODE },
89 { "CNS1164319921", CS_CNS11643_1992_1 },
90 { "CNS1164319922", CS_CNS11643_1992_2 },
91 { "CNS1164319923", CS_CNS11643_1992_3 },
92 { "CNS1164319924", CS_CNS11643_1992_4 },
93 { "CNS1164319925", CS_CNS11643_1992_5 },
94 { "CNS1164319926", CS_CNS11643_1992_6 },
95 { "CNS1164319927", CS_CNS11643_1992_7 },
96 { "CNS116431992F", CS_CNS11643_1992_F },
97
98 { 0, CS_UNKNOWN }
99 };
100
101 static const char *
102 normalize_name (const char *name)
103 {
104 static char res[16];
105 char *r;
106
107 for (r = res; *name && r < res + 15; name++)
108 if ((*name >= '0' && *name <= '9')
109 || (*name >= 'A' && *name <= 'Z'))
110 *r++ = *name;
111 else if (*name >= 'a' && *name <= 'z')
112 *r++ = *name - ('a' - 'A');
113
114 *r = 0;
115
116 return res;
117 }
118
119 codeset
120 codeset_from_name (const char *name)
121 {
122 if (!name)
123 return CS_UNKNOWN;
124
125 name = normalize_name (name);
126
127 const struct n2cs *i = n2cs;
128
129 do {
130 int len = strlen (i->name);
131
132 if ((i->name[len - 1] == '*'
133 && !strncmp (name, i->name, len - 1))
134 || !strcmp (name, i->name))
135 return i->cs;
136
137 } while ((++i)->name);
138
139 return CS_UNKNOWN;
140 }
141
142 static unicode_t cs_unknown_to_unicode (uint32_t enc) { return NOCHAR; }
143 static uint32_t cs_unknown_from_unicode (unicode_t unicode) { return NOCHAR; }
144
145 static unicode_t cs_unicode_to_unicode (uint32_t enc) { return enc; }
146 static uint32_t cs_unicode_from_unicode (unicode_t unicode) { return unicode; }
147
148 #define cs_us_ascii_to_unicode cs_unicode_to_unicode
149 static uint32_t cs_us_ascii_from_unicode (unicode_t unicode) { return unicode <= 127 ? unicode : NOCHAR; }
150
151 #define cs_us_ascii_to_unicode_16 cs_unicode_to_unicode
152 static uint32_t cs_unicode_16_from_unicode (unicode_t unicode) { return unicode <= 65535 ? unicode : NOCHAR; }
153
154 #define ENCODING_DEFAULT
155
156 #include "table/iso8859_1.h"
157 #include "table/iso8859_15.h"
158
159 //#define ENCODING_EU
160
161 #include "table/iso8859_2.h"
162 #include "table/iso8859_3.h"
163 #include "table/iso8859_4.h"
164 #include "table/iso8859_5.h"
165 #include "table/iso8859_6.h"
166 #include "table/iso8859_7.h"
167 #include "table/iso8859_8.h"
168 #include "table/iso8859_9.h"
169 #include "table/iso8859_10.h"
170 #include "table/iso8859_11.h"
171 #include "table/iso8859_13.h"
172 #include "table/iso8859_14.h"
173 #include "table/iso8859_16.h"
174
175 #include "table/koi8_r.h"
176 #include "table/koi8_u.h"
177
178 //#define ENCODING_KR
179
180 #include "table/ksc5601_1987_0.h"
181
182 //#define ENCODING_ZH
183
184 #include "table/gb2312_1980_0.h"
185 #include "table/big5.h"
186
187 //#define ENCODING_ZH_EXT
188
189 #include "table/cns11643_1992_1.h"
190 #include "table/cns11643_1992_2.h"
191 #include "table/cns11643_1992_3.h"
192 #include "table/cns11643_1992_4.h"
193 #include "table/cns11643_1992_5.h"
194 #include "table/cns11643_1992_6.h"
195 #include "table/cns11643_1992_7.h"
196 #include "table/cns11643_1992_f.h"
197 #include "table/big5_ext.h"
198 #include "table/big5_plus.h"
199
200 //#define ENCODING_VN
201
202 #include "table/viscii.h"
203
204 //#define ENCODING_JP
205
206 #include "table/jis0201_1976_0.h"
207 #include "table/jis0208_1990_0.h"
208 #include "table/jis0212_1990_0.h"
209
210 //#define ENCODING_JP_EXT
211
212 #include "table/jis0213_1.h"
213 #include "table/jis0213_2.h"
214
215 #if ENCODING_TO_UNICODE
216 # define ENC(base) { cs_ ## base ## _from_unicode, cs_ ## base ## _to_unicode }
217 #else
218 # define ENC(base) { cs_ ## base ## _from_unicode }
219 #endif
220
221
222 // order must match table in encoding.h(!)
223 const rxvt_codeset_conv rxvt_codeset[NUM_CODESETS] = {
224 ENC (unknown),
225
226 ENC (us_ascii),
227
228 ENC (iso8859_1),
229 ENC (iso8859_2),
230 ENC (iso8859_3),
231 ENC (iso8859_4),
232 ENC (iso8859_5),
233 ENC (iso8859_6),
234 ENC (iso8859_7),
235 ENC (iso8859_8),
236 ENC (iso8859_9),
237 ENC (iso8859_10),
238 ENC (iso8859_11),
239 ENC (iso8859_13),
240 ENC (iso8859_14),
241 ENC (iso8859_15),
242 ENC (iso8859_16),
243
244 ENC (koi8_r),
245 ENC (koi8_u),
246
247 ENC (jis0201_1976_0),
248 ENC (jis0208_1990_0),
249 ENC (jis0212_1990_0),
250
251 ENC (jis0213_1),
252 ENC (jis0213_2),
253
254 ENC (ksc5601_1987_0),
255
256 ENC (gb2312_1980_0),
257
258 ENC (cns11643_1992_1),
259 ENC (cns11643_1992_2),
260 ENC (cns11643_1992_3),
261 ENC (cns11643_1992_4),
262 ENC (cns11643_1992_5),
263 ENC (cns11643_1992_6),
264 ENC (cns11643_1992_7),
265 ENC (cns11643_1992_f),
266 ENC (big5),
267 ENC (big5_ext),
268 ENC (big5_plus),
269
270 ENC (viscii),
271
272 ENC (unicode_16),
273 ENC (unicode),
274 };
275
276 #if ENABLE_COMBINING
277 # define ENCODING_COMPOSE
278 #endif
279
280 #include "table/compose.h"
281
282 unicode_t
283 rxvt_compose (unicode_t c1, unicode_t c2)
284 {
285 int l = 0;
286 int r = sizeof (rxvt_compose_table) / sizeof (rxvt_compose_entry) - 1;
287 int m;
288
289 while (r > l)
290 {
291 m = (l + r) / 2;
292 rxvt_compose_entry &c = rxvt_compose_table[m];
293
294 if (c.c1 < c1 || (c.c1 == c1 && c.c2 < c2))
295 l = m + 1;
296 else if (c.c1 > c1 || (c.c1 == c1 && c.c2 > c2))
297 r = m - 1;
298 else
299 return c.r;
300 }
301
302 return NOCHAR;
303 }
304
305 #include "table/category.h"
306
307 bool unicode::is_space (unicode_t c)
308 {
309 return IS_SPACE (c)
310 || c == 0x09; // exclude tabs, too, as we store them in the buffer
311 }