Fix typos.
[dana/urxvt.git] / src / encoding.C
1 /*----------------------------------------------------------------------*
2  * File:        encoding.C
3  *----------------------------------------------------------------------*
4  *
5  * All portions of code are copyright by their respective author/s.
6  * Copyright (c) 2003-2006 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   { "ISO6461991IRV",    CS_US_ASCII         }, // older versions used the currency sign
63
64   { "KOI8R*",           CS_KOI8_R           },
65   { "GOST1976874*",     CS_KOI8_R           },
66   { "KOI8RU",           CS_KOI8_U           },
67   { "KOI8U",            CS_KOI8_U           },
68
69   { "VISCII*",          CS_VISCII           },
70
71   { "JISX0201*",        CS_JIS0201_1976_0   },
72   { "JISC6226*",        CS_JIS0208_1990_0   }, // also wrongly matches -1987-0? (check Encode::JP)
73   { "JISX0208*",        CS_JIS0208_1990_0   }, // also wrongly matches -1987-0? (check Encode::JP)
74   { "JISX0212*",        CS_JIS0212_1990_0   },
75   { "JISX021320001",    CS_JIS0213_1        },
76   { "JISX021320002",    CS_JIS0213_2        },
77   { "JISX0221*",        CS_UNICODE          }, // _very_ close
78
79   { "KSC5601*",         CS_KSC5601_1987_0   },
80   { "KSX1001*",         CS_KSC5601_1987_0   },
81   { "KSC5700*",         CS_UNICODE          }, // unicode plus extensions
82
83   { "BIG5P*",           CS_BIG5_PLUS        },
84   { "BIG5ETEN*",        CS_BIG5_EXT         },
85   { "BIG5*",            CS_BIG5             },
86   { "GB2312*",          CS_GB2312_1980_0    },
87   { "GBK*",             CS_GBK_0            },
88   { "GB6345*",          CS_GB2312_1980_0    }, // slightly different to gb2312??
89   { "GB8565*",          CS_GB2312_1980_0    }, // a superset of gb2312??
90   { "GB13000*",         CS_UNICODE          },
91   { "CNS1164319921",    CS_CNS11643_1992_1  },
92   { "CNS1164319922",    CS_CNS11643_1992_2  },
93   { "CNS1164319923",    CS_CNS11643_1992_3  },
94   { "CNS1164319924",    CS_CNS11643_1992_4  },
95   { "CNS1164319925",    CS_CNS11643_1992_5  },
96   { "CNS1164319926",    CS_CNS11643_1992_6  },
97   { "CNS1164319927",    CS_CNS11643_1992_7  },
98   { "CNS116431992F",    CS_CNS11643_1992_F  },
99
100   { 0,                  CS_UNKNOWN      }
101 };
102
103 static const char *
104 normalize_name (const char *name)
105 {
106   static char res[16];
107   char *r;
108
109   for (r = res; *name && r < res + 15; name++)
110     if ((*name >= '0' && *name <= '9')
111         || (*name >= 'A' && *name <= 'Z'))
112       *r++ = *name;
113     else if (*name >= 'a' && *name <= 'z')
114       *r++ = *name - ('a' - 'A');
115
116   *r = 0;
117
118   return res;
119 }
120
121 codeset
122 codeset_from_name (const char *name)
123 {
124   if (!name)
125     return CS_UNKNOWN;
126
127   name = normalize_name (name);
128
129   const struct n2cs *i = n2cs;
130
131   do {
132     int len = strlen (i->name);
133
134     if ((i->name[len - 1] == '*'
135          && !strncmp (name, i->name, len - 1))
136         || !strcmp (name, i->name))
137         return i->cs;
138
139   } while ((++i)->name);
140
141   return CS_UNKNOWN;
142 }
143
144 static unicode_t cs_unknown_to_unicode (uint32_t enc)          { return NOCHAR; }
145 static uint32_t cs_unknown_from_unicode (unicode_t unicode)    { return NOCHAR; }
146
147 static unicode_t cs_unicode_to_unicode (uint32_t enc)          { return enc; }
148 static uint32_t cs_unicode_from_unicode (unicode_t unicode)    { return unicode; }
149
150 #define cs_us_ascii_to_unicode cs_unicode_to_unicode
151 static uint32_t cs_us_ascii_from_unicode (unicode_t unicode)   { return unicode <= 127 ? unicode : NOCHAR; }
152
153 #define cs_us_ascii_to_unicode_16 cs_unicode_to_unicode
154 static uint32_t cs_unicode_16_from_unicode (unicode_t unicode) { return unicode <= 65535 ? unicode : NOCHAR; }
155
156 #define ENCODING_DEFAULT
157
158 #include "table/iso8859_1.h"
159 #include "table/iso8859_15.h"
160
161 //#define ENCODING_EU
162
163 #include "table/iso8859_2.h"
164 #include "table/iso8859_3.h"
165 #include "table/iso8859_4.h"
166 #include "table/iso8859_5.h"
167 #include "table/iso8859_6.h"
168 #include "table/iso8859_7.h"
169 #include "table/iso8859_8.h"
170 #include "table/iso8859_9.h"
171 #include "table/iso8859_10.h"
172 #include "table/iso8859_11.h"
173 #include "table/iso8859_13.h"
174 #include "table/iso8859_14.h"
175 #include "table/iso8859_16.h"
176
177 #include "table/koi8_r.h"
178 #include "table/koi8_u.h"
179
180 //#define ENCODING_KR
181
182 #include "table/ksc5601_1987_0.h"
183
184 //#define ENCODING_ZH
185
186 #include "table/big5.h"
187 #include "table/gbk_0.h"
188 #include "table/gb2312_1980_0.h"
189
190 //#define ENCODING_ZH_EXT
191
192 #include "table/cns11643_1992_1.h"
193 #include "table/cns11643_1992_2.h"
194 #include "table/cns11643_1992_3.h"
195 #include "table/cns11643_1992_4.h"
196 #include "table/cns11643_1992_5.h"
197 #include "table/cns11643_1992_6.h"
198 #include "table/cns11643_1992_7.h"
199 #include "table/cns11643_1992_f.h"
200 #include "table/big5_ext.h"
201 #include "table/big5_plus.h"
202
203 //#define ENCODING_VN
204
205 #include "table/viscii.h"
206
207 //#define ENCODING_JP
208
209 #include "table/jis0201_1976_0.h"
210 #include "table/jis0208_1990_0.h"
211 #include "table/jis0212_1990_0.h"
212
213 //#define ENCODING_JP_EXT
214
215 #include "table/jis0213_1.h"
216 #include "table/jis0213_2.h"
217
218 #if ENCODING_TO_UNICODE
219 # define ENC(base) { cs_ ## base ## _from_unicode, cs_ ## base ## _to_unicode }
220 #else
221 # define ENC(base) { cs_ ## base ## _from_unicode }
222 #endif
223
224
225 // order must match table in encoding.h(!)
226 const rxvt_codeset_conv rxvt_codeset[NUM_CODESETS] = {
227   ENC (unknown),
228
229   ENC (us_ascii),
230
231   ENC (iso8859_1),
232   ENC (iso8859_2),
233   ENC (iso8859_3),
234   ENC (iso8859_4),
235   ENC (iso8859_5),
236   ENC (iso8859_6),
237   ENC (iso8859_7),
238   ENC (iso8859_8),
239   ENC (iso8859_9),
240   ENC (iso8859_10),
241   ENC (iso8859_11),
242   ENC (iso8859_13),
243   ENC (iso8859_14),
244   ENC (iso8859_15),
245   ENC (iso8859_16),
246
247   ENC (koi8_r),
248   ENC (koi8_u),
249
250   ENC (jis0201_1976_0),
251   ENC (jis0208_1990_0),
252   ENC (jis0212_1990_0),
253
254   ENC (jis0213_1),
255   ENC (jis0213_2),
256
257   ENC (ksc5601_1987_0),
258
259   ENC (gb2312_1980_0),
260   ENC (gbk_0),
261
262   ENC (cns11643_1992_1),
263   ENC (cns11643_1992_2),
264   ENC (cns11643_1992_3),
265   ENC (cns11643_1992_4),
266   ENC (cns11643_1992_5),
267   ENC (cns11643_1992_6),
268   ENC (cns11643_1992_7),
269   ENC (cns11643_1992_f),
270   ENC (big5),
271   ENC (big5_ext),
272   ENC (big5_plus),
273
274   ENC (viscii),
275
276   ENC (unicode_16),
277   ENC (unicode),
278 };
279
280 #if ENABLE_COMBINING
281 # define ENCODING_COMPOSE
282 #endif
283
284 #include "table/compose.h"
285
286 unicode_t
287 rxvt_compose (unicode_t c1, unicode_t c2)
288 {
289   int l = 0;
290   int r = sizeof (rxvt_compose_table) / sizeof (rxvt_compose_entry) - 1;
291   int m;
292
293   while (r >= l)
294     {
295       m = (l + r) / 2;
296       rxvt_compose_entry &c = rxvt_compose_table[m];
297
298       if (c.c1 < c1 || (c.c1 == c1 && c.c2 < c2))
299         l = m + 1;
300       else if (c.c1 > c1 || (c.c1 == c1 && c.c2 > c2))
301         r = m - 1;
302       else
303         return c.r;
304     }
305
306   return NOCHAR;
307 }
308
309 #include "table/category.h"
310
311 bool unicode::is_space (unicode_t c)
312 {
313   return IS_SPACE (c)
314          || c == 0x09; // exclude tabs, too, as we store them in the buffer
315 }