|
|
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 | |
1 | #include "../config.h" |
23 | #include "../config.h" |
2 | |
24 | |
3 | #include "encoding.h" |
25 | #include "encoding.h" |
4 | |
26 | |
5 | #include <cstdlib> |
27 | #include <cstdlib> |
… | |
… | |
8 | const struct n2cs { |
30 | const struct n2cs { |
9 | const char *name; |
31 | const char *name; |
10 | codeset cs; |
32 | codeset cs; |
11 | } n2cs[] = { |
33 | } n2cs[] = { |
12 | /* first one found is the normalized one */ |
34 | /* first one found is the normalized one */ |
13 | { "ISO88591", CS_ISO8859_1 }, |
35 | { "ISO88591", CS_ISO8859_1 }, |
14 | { "ISO8859PRIMARY", CS_ISO8859_1 }, // some stupid fonts use this (hi tigert) |
36 | { "ISO8859PRIMARY", CS_ISO8859_1 }, // some stupid fonts use this (hi tigert) |
15 | { "ISO88592", CS_ISO8859_2 }, |
37 | { "ISO88592", CS_ISO8859_2 }, |
16 | { "ISO88593", CS_ISO8859_3 }, |
38 | { "ISO88593", CS_ISO8859_3 }, |
17 | { "ISO88594", CS_ISO8859_4 }, |
39 | { "ISO88594", CS_ISO8859_4 }, |
18 | { "ISO88595", CS_ISO8859_5 }, |
40 | { "ISO88595", CS_ISO8859_5 }, |
19 | { "ISO88596", CS_ISO8859_6 }, |
41 | { "ISO88596", CS_ISO8859_6 }, |
20 | { "ISO88597", CS_ISO8859_7 }, |
42 | { "ISO88597", CS_ISO8859_7 }, |
21 | { "ISO88598", CS_ISO8859_8 }, |
43 | { "ISO88598", CS_ISO8859_8 }, |
22 | { "ISO88599", CS_ISO8859_9 }, |
44 | { "ISO88599", CS_ISO8859_9 }, |
23 | { "ISO885910", CS_ISO8859_10 }, |
45 | { "ISO885910", CS_ISO8859_10 }, |
24 | { "ISO885911", CS_ISO8859_11 }, |
46 | { "ISO885911", CS_ISO8859_11 }, |
25 | { "ISO885913", CS_ISO8859_13 }, |
47 | { "ISO885913", CS_ISO8859_13 }, |
26 | { "ISO885914", CS_ISO8859_14 }, |
48 | { "ISO885914", CS_ISO8859_14 }, |
27 | { "ISO885915", CS_ISO8859_15 }, |
49 | { "ISO885915", CS_ISO8859_15 }, |
28 | { "FCD885915", CS_ISO8859_15 }, |
50 | { "FCD885915", CS_ISO8859_15 }, |
29 | { "ISO885916", CS_ISO8859_16 }, |
51 | { "ISO885916", CS_ISO8859_16 }, |
|
|
52 | |
|
|
53 | { "TIS620*", CS_ISO8859_11 }, // close enough |
30 | |
54 | |
31 | { "ISO106461", CS_UNICODE }, |
55 | { "ISO10646*", CS_UNICODE }, |
32 | { "UNICODE", CS_UNICODE }, |
56 | { "UNICODE", CS_UNICODE }, |
33 | { "UTF8", CS_UNICODE }, |
57 | { "UTF8", CS_UNICODE }, |
34 | |
58 | |
35 | { "ASCII", CS_US_ASCII }, |
59 | { "ASCII", CS_US_ASCII }, |
36 | { "USASCII", CS_US_ASCII }, |
60 | { "USASCII", CS_US_ASCII }, |
37 | { "ANSIX341968", CS_US_ASCII }, |
61 | { "ANSIX341968", CS_US_ASCII }, |
38 | |
62 | |
39 | { "KOI8R", CS_KOI8_R }, |
63 | { "KOI8R", CS_KOI8_R }, |
40 | { "GOST19768741", CS_KOI8_R }, |
64 | { "GOST1976874*", CS_KOI8_R }, |
|
|
65 | { "KOI8RU", CS_KOI8_U }, |
41 | { "KOI8U", CS_KOI8_U }, |
66 | { "KOI8U", CS_KOI8_U }, |
42 | |
67 | |
43 | { "KSC560119870", CS_KSC5601_1987_0 }, |
|
|
44 | { "KSX100119970", CS_KSC5601_1987_0 }, |
|
|
45 | { "KSX100119980", CS_KSC5601_1987_0 }, // adds johab |
|
|
46 | |
|
|
47 | { "GB231219800", CS_GB2312_1980_0 }, |
|
|
48 | |
|
|
49 | { "VISCII", CS_VISCII }, |
68 | { "VISCII*", CS_VISCII }, |
50 | { "VISCII111", CS_VISCII }, |
69 | |
51 | { "TIS62025291", CS_VISCII }, /* close enough */ |
|
|
52 | |
|
|
53 | { "JISX020119760", CS_JIS0201_1976_0 }, |
70 | { "JISX0201*", CS_JIS0201_1976_0 }, |
54 | { "JISX020819830", CS_JIS0208_1983_0 }, |
71 | { "JISC6226*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP) |
55 | { "JISX020819900", CS_JIS0208_1983_0 }, /* ehrm. */ |
72 | { "JISX0208*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP) |
56 | { "JISX021219900", CS_JIS0212_1990_0 }, |
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 | { "GBK*", CS_GBK_0 }, |
|
|
87 | { "GB6345*", CS_GB2312_1980_0 }, // slightly different to gb2312?? |
|
|
88 | { "GB8565*", CS_GB2312_1980_0 }, // a superset of gb2312?? |
|
|
89 | { "GB13000*", CS_UNICODE }, |
|
|
90 | { "CNS1164319921", CS_CNS11643_1992_1 }, |
|
|
91 | { "CNS1164319922", CS_CNS11643_1992_2 }, |
|
|
92 | { "CNS1164319923", CS_CNS11643_1992_3 }, |
|
|
93 | { "CNS1164319924", CS_CNS11643_1992_4 }, |
|
|
94 | { "CNS1164319925", CS_CNS11643_1992_5 }, |
|
|
95 | { "CNS1164319926", CS_CNS11643_1992_6 }, |
|
|
96 | { "CNS1164319927", CS_CNS11643_1992_7 }, |
|
|
97 | { "CNS116431992F", CS_CNS11643_1992_F }, |
57 | |
98 | |
58 | { 0, CS_UNKNOWN } |
99 | { 0, CS_UNKNOWN } |
59 | }; |
100 | }; |
60 | |
101 | |
61 | static const char * |
102 | static const char * |
… | |
… | |
85 | name = normalize_name (name); |
126 | name = normalize_name (name); |
86 | |
127 | |
87 | const struct n2cs *i = n2cs; |
128 | const struct n2cs *i = n2cs; |
88 | |
129 | |
89 | do { |
130 | do { |
|
|
131 | int len = strlen (i->name); |
|
|
132 | |
|
|
133 | if ((i->name[len - 1] == '*' |
|
|
134 | && !strncmp (name, i->name, len - 1)) |
90 | if (!strcmp (name, i->name)) |
135 | || !strcmp (name, i->name)) |
91 | return i->cs; |
136 | return i->cs; |
|
|
137 | |
92 | } while ((++i)->name); |
138 | } while ((++i)->name); |
93 | |
139 | |
94 | return CS_UNKNOWN; |
140 | return CS_UNKNOWN; |
95 | } |
141 | } |
96 | |
142 | |
97 | struct rxvt_codeset_conv_unknown : rxvt_codeset_conv { |
143 | static unicode_t cs_unknown_to_unicode (uint32_t enc) { return NOCHAR; } |
98 | uint32_t to_unicode (uint32_t enc) const { return NOCHAR; } |
|
|
99 | uint32_t from_unicode (uint32_t unicode) const { return NOCHAR; } |
144 | static uint32_t cs_unknown_from_unicode (unicode_t unicode) { return NOCHAR; } |
100 | } rxvt_codeset_conv_unknown; |
|
|
101 | |
145 | |
102 | struct rxvt_codeset_conv_us_ascii : rxvt_codeset_conv { |
146 | static unicode_t cs_unicode_to_unicode (uint32_t enc) { return enc; } |
|
|
147 | static uint32_t cs_unicode_from_unicode (unicode_t unicode) { return unicode; } |
|
|
148 | |
|
|
149 | #define cs_us_ascii_to_unicode cs_unicode_to_unicode |
103 | uint32_t from_unicode (uint32_t unicode) const { return unicode <= 127 ? unicode : NOCHAR; } |
150 | static uint32_t cs_us_ascii_from_unicode (unicode_t unicode) { return unicode <= 127 ? unicode : NOCHAR; } |
104 | } rxvt_codeset_conv_us_ascii; |
|
|
105 | |
151 | |
106 | struct rxvt_codeset_conv_unicode : rxvt_codeset_conv { |
152 | #define cs_us_ascii_to_unicode_16 cs_unicode_to_unicode |
107 | /* transparent */ |
|
|
108 | } rxvt_codeset_conv_unicode; |
|
|
109 | |
|
|
110 | struct rxvt_codeset_conv_unicode_16 : rxvt_codeset_conv { |
|
|
111 | uint32_t to_unicode (uint32_t enc) const { return enc; } |
|
|
112 | uint32_t from_unicode (uint32_t unicode) const { return unicode <= 65535 ? unicode : NOCHAR; } |
153 | static uint32_t cs_unicode_16_from_unicode (unicode_t unicode) { return unicode <= 65535 ? unicode : NOCHAR; } |
113 | } rxvt_codeset_conv_unicode_16; |
|
|
114 | |
|
|
115 | /* block character set, must conform to the dec special pseudofont in defaultfont.C */ |
|
|
116 | struct rxvt_codeset_conv_special : rxvt_codeset_conv { |
|
|
117 | uint32_t to_unicode (uint32_t enc) const { |
|
|
118 | return enc; |
|
|
119 | } |
|
|
120 | |
|
|
121 | uint32_t from_unicode (uint32_t unicode) const { |
|
|
122 | return unicode; |
|
|
123 | } |
|
|
124 | } rxvt_codeset_conv_special; |
|
|
125 | |
154 | |
126 | #define ENCODING_DEFAULT |
155 | #define ENCODING_DEFAULT |
127 | |
156 | |
128 | #include "table/iso8859_1.h" |
157 | #include "table/iso8859_1.h" |
129 | #include "table/iso8859_15.h" |
158 | #include "table/iso8859_15.h" |
… | |
… | |
149 | |
178 | |
150 | //#define ENCODING_KR |
179 | //#define ENCODING_KR |
151 | |
180 | |
152 | #include "table/ksc5601_1987_0.h" |
181 | #include "table/ksc5601_1987_0.h" |
153 | |
182 | |
154 | //#define ENCODING_CN |
183 | //#define ENCODING_ZH |
155 | |
184 | |
|
|
185 | #include "table/big5.h" |
|
|
186 | #include "table/gbk_0.h" |
156 | #include "table/gb2312_1980_0.h" |
187 | #include "table/gb2312_1980_0.h" |
157 | |
188 | |
158 | //#define ENCODING_CN_EXT |
189 | //#define ENCODING_ZH_EXT |
159 | |
190 | |
160 | #include "table/cns11643_1992_1.h" |
191 | #include "table/cns11643_1992_1.h" |
161 | #include "table/cns11643_1992_2.h" |
192 | #include "table/cns11643_1992_2.h" |
162 | #include "table/cns11643_1992_3.h" |
193 | #include "table/cns11643_1992_3.h" |
163 | #include "table/cns11643_1992_4.h" |
194 | #include "table/cns11643_1992_4.h" |
… | |
… | |
173 | #include "table/viscii.h" |
204 | #include "table/viscii.h" |
174 | |
205 | |
175 | //#define ENCODING_JP |
206 | //#define ENCODING_JP |
176 | |
207 | |
177 | #include "table/jis0201_1976_0.h" |
208 | #include "table/jis0201_1976_0.h" |
178 | #include "table/jis0208_1983_0.h" |
209 | #include "table/jis0208_1990_0.h" |
179 | #include "table/jis0212_1990_0.h" |
210 | #include "table/jis0212_1990_0.h" |
180 | |
211 | |
181 | //#define ENCODING_JP_EXT |
212 | //#define ENCODING_JP_EXT |
182 | |
213 | |
183 | #include "table/jis0213_1.h" |
214 | #include "table/jis0213_1.h" |
184 | #include "table/jis0213_2.h" |
215 | #include "table/jis0213_2.h" |
185 | |
216 | |
|
|
217 | #if ENCODING_TO_UNICODE |
|
|
218 | # define ENC(base) { cs_ ## base ## _from_unicode, cs_ ## base ## _to_unicode } |
|
|
219 | #else |
|
|
220 | # define ENC(base) { cs_ ## base ## _from_unicode } |
|
|
221 | #endif |
|
|
222 | |
|
|
223 | |
|
|
224 | // order must match table in encoding.h(!) |
186 | const rxvt_codeset_conv *rxvt_codeset[NUM_CODESETS] = { |
225 | const rxvt_codeset_conv rxvt_codeset[NUM_CODESETS] = { |
187 | &rxvt_codeset_conv_unknown, |
226 | ENC (unknown), |
188 | &rxvt_codeset_conv_special, |
|
|
189 | |
227 | |
190 | &rxvt_codeset_conv_us_ascii, |
228 | ENC (us_ascii), |
191 | |
229 | |
192 | &rxvt_codeset_conv_iso8859_1, |
230 | ENC (iso8859_1), |
193 | &rxvt_codeset_conv_iso8859_2, |
231 | ENC (iso8859_2), |
194 | &rxvt_codeset_conv_iso8859_3, |
232 | ENC (iso8859_3), |
195 | &rxvt_codeset_conv_iso8859_4, |
233 | ENC (iso8859_4), |
196 | &rxvt_codeset_conv_iso8859_5, |
234 | ENC (iso8859_5), |
197 | &rxvt_codeset_conv_iso8859_6, |
235 | ENC (iso8859_6), |
198 | &rxvt_codeset_conv_iso8859_7, |
236 | ENC (iso8859_7), |
199 | &rxvt_codeset_conv_iso8859_8, |
237 | ENC (iso8859_8), |
200 | &rxvt_codeset_conv_iso8859_9, |
238 | ENC (iso8859_9), |
201 | &rxvt_codeset_conv_iso8859_10, |
239 | ENC (iso8859_10), |
202 | &rxvt_codeset_conv_iso8859_11, |
240 | ENC (iso8859_11), |
203 | &rxvt_codeset_conv_iso8859_13, |
241 | ENC (iso8859_13), |
204 | &rxvt_codeset_conv_iso8859_14, |
242 | ENC (iso8859_14), |
205 | &rxvt_codeset_conv_iso8859_15, |
243 | ENC (iso8859_15), |
206 | &rxvt_codeset_conv_iso8859_16, |
244 | ENC (iso8859_16), |
207 | |
245 | |
208 | &rxvt_codeset_conv_koi8_r, |
246 | ENC (koi8_r), |
209 | &rxvt_codeset_conv_koi8_u, |
247 | ENC (koi8_u), |
210 | |
248 | |
211 | &rxvt_codeset_conv_jis0201_1976_0, |
249 | ENC (jis0201_1976_0), |
212 | &rxvt_codeset_conv_jis0208_1983_0, |
250 | ENC (jis0208_1990_0), |
213 | &rxvt_codeset_conv_jis0212_1990_0, |
251 | ENC (jis0212_1990_0), |
214 | |
252 | |
215 | &rxvt_codeset_conv_jis0213_1, |
253 | ENC (jis0213_1), |
216 | &rxvt_codeset_conv_jis0213_2, |
254 | ENC (jis0213_2), |
217 | |
255 | |
218 | &rxvt_codeset_conv_ksc5601_1987_0, |
256 | ENC (ksc5601_1987_0), |
219 | |
257 | |
220 | &rxvt_codeset_conv_gb2312_1980_0, |
258 | ENC (gb2312_1980_0), |
|
|
259 | ENC (gbk_0), |
221 | |
260 | |
222 | &rxvt_codeset_conv_cns11643_1992_1, |
261 | ENC (cns11643_1992_1), |
223 | &rxvt_codeset_conv_cns11643_1992_2, |
262 | ENC (cns11643_1992_2), |
224 | &rxvt_codeset_conv_cns11643_1992_3, |
263 | ENC (cns11643_1992_3), |
225 | &rxvt_codeset_conv_cns11643_1992_4, |
264 | ENC (cns11643_1992_4), |
226 | &rxvt_codeset_conv_cns11643_1992_5, |
265 | ENC (cns11643_1992_5), |
227 | &rxvt_codeset_conv_cns11643_1992_6, |
266 | ENC (cns11643_1992_6), |
228 | &rxvt_codeset_conv_cns11643_1992_7, |
267 | ENC (cns11643_1992_7), |
229 | &rxvt_codeset_conv_cns11643_1992_f, |
268 | ENC (cns11643_1992_f), |
230 | &rxvt_codeset_conv_big5_ext, |
269 | ENC (big5), |
231 | &rxvt_codeset_conv_big5_plus, |
270 | ENC (big5_ext), |
|
|
271 | ENC (big5_plus), |
232 | |
272 | |
233 | &rxvt_codeset_conv_viscii, |
273 | ENC (viscii), |
234 | |
274 | |
235 | &rxvt_codeset_conv_unicode_16, |
275 | ENC (unicode_16), |
236 | &rxvt_codeset_conv_unicode |
276 | ENC (unicode), |
237 | }; |
277 | }; |
238 | |
278 | |
|
|
279 | #if ENABLE_COMBINING |
|
|
280 | # define ENCODING_COMPOSE |
|
|
281 | #endif |
|
|
282 | |
|
|
283 | #include "table/compose.h" |
|
|
284 | |
|
|
285 | unicode_t |
|
|
286 | rxvt_compose (unicode_t c1, unicode_t c2) |
|
|
287 | { |
|
|
288 | int l = 0; |
|
|
289 | int r = sizeof (rxvt_compose_table) / sizeof (rxvt_compose_entry) - 1; |
|
|
290 | int m; |
|
|
291 | |
|
|
292 | while (r > l) |
|
|
293 | { |
|
|
294 | m = (l + r) / 2; |
|
|
295 | rxvt_compose_entry &c = rxvt_compose_table[m]; |
|
|
296 | |
|
|
297 | if (c.c1 < c1 || (c.c1 == c1 && c.c2 < c2)) |
|
|
298 | l = m + 1; |
|
|
299 | else if (c.c1 > c1 || (c.c1 == c1 && c.c2 > c2)) |
|
|
300 | r = m - 1; |
|
|
301 | else |
|
|
302 | return c.r; |
|
|
303 | } |
|
|
304 | |
|
|
305 | return NOCHAR; |
|
|
306 | } |
|
|
307 | |
|
|
308 | #include "table/category.h" |
|
|
309 | |
|
|
310 | bool unicode::is_space (unicode_t c) |
|
|
311 | { |
|
|
312 | return IS_SPACE (c) |
|
|
313 | || c == 0x09; // exclude tabs, too, as we store them in the buffer |
|
|
314 | } |