771
|
1 /* Code to handle Unicode conversion.
|
|
2 Copyright (C) 2000, 2001, 2002 Ben Wing.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 20.3. Not in FSF. */
|
|
22
|
|
23 /* Authorship:
|
|
24
|
|
25 Current primary author: Ben Wing <ben@xemacs.org>
|
|
26
|
|
27 Written by Ben Wing <ben@xemacs.org>, June, 2001.
|
|
28 Separated out into this file, August, 2001.
|
|
29 Includes Unicode coding systems, some parts of which have been written
|
877
|
30 by someone else. #### Morioka and Hayashi, I think.
|
771
|
31
|
|
32 As of September 2001, the detection code is here and abstraction of the
|
877
|
33 detection system is finished. The unicode detectors have been rewritten
|
771
|
34 to include multiple levels of likelihood.
|
|
35 */
|
|
36
|
|
37 #include <config.h>
|
|
38 #include "lisp.h"
|
|
39
|
|
40 #include "charset.h"
|
|
41 #include "file-coding.h"
|
|
42 #include "opaque.h"
|
|
43
|
|
44 #include "sysfile.h"
|
|
45
|
|
46 /* #### WARNING! The current sledgehammer routines have a fundamental
|
|
47 problem in that they can't handle two characters mapping to a
|
|
48 single Unicode codepoint or vice-versa in a single charset table.
|
|
49 It's not clear there is any way to handle this and still make the
|
877
|
50 sledgehammer routines useful.
|
|
51
|
|
52 Inquiring Minds Want To Know Dept: does the above WARNING mean that
|
|
53 _if_ it happens, then it will signal error, or then it will do
|
|
54 something evil and unpredictable? Signaling an error is OK: for
|
|
55 all national standards, the national to Unicode map is an inclusion
|
|
56 (1-to-1). Any character set that does not behave that way is
|
|
57 broken according to the Unicode standard. */
|
|
58
|
771
|
59 /* #define SLEDGEHAMMER_CHECK_UNICODE */
|
|
60
|
|
61 /* We currently use the following format for tables:
|
|
62
|
|
63 If dimension == 1, to_unicode_table is a 96-element array of ints
|
|
64 (Unicode code points); else, it's a 96-element array of int *
|
|
65 pointers, each of which points to a 96-element array of ints. If no
|
|
66 elements in a row have been filled in, the pointer will point to a
|
|
67 default empty table; that way, memory usage is more reasonable but
|
|
68 lookup still fast.
|
|
69
|
|
70 -- If from_unicode_levels == 1, from_unicode_table is a 256-element
|
|
71 array of shorts (octet 1 in high byte, octet 2 in low byte; we don't
|
867
|
72 store Ichars directly to save space).
|
771
|
73
|
|
74 -- If from_unicode_levels == 2, from_unicode_table is a
|
|
75 256-element array of short * pointers, each of which points to a
|
|
76 256-element array of shorts.
|
|
77
|
|
78 -- If from_unicode_levels == 3, from_unicode_table is a
|
|
79 256-element array of short ** pointers, each of which points to
|
|
80 a 256-element array of short * pointers, each of which points to
|
|
81 a 256-element array of shorts.
|
|
82
|
|
83 -- If from_unicode_levels == 4, same thing but one level deeper.
|
|
84
|
|
85 Just as for to_unicode_table, we use default tables to fill in
|
|
86 all entries with no values in them.
|
|
87
|
|
88 #### An obvious space-saving optimization is to use variable-sized
|
|
89 tables, where each table instead of just being a 256-element array,
|
|
90 is a structure with a start value, an end value, and a variable
|
|
91 number of entries (END - START + 1). Only 8 bits are needed for
|
|
92 END and START, and could be stored at the end to avoid alignment
|
|
93 problems. However, before charging off and implementing this,
|
|
94 we need to consider whether it's worth it:
|
|
95
|
|
96 (1) Most tables will be highly localized in which code points are
|
|
97 defined, heavily reducing the possible memory waste. Before
|
|
98 doing any rewriting, write some code to see how much memory is
|
|
99 actually being wasted (i.e. ratio of empty entries to total # of
|
|
100 entries) and only start rewriting if it's unacceptably high. You
|
|
101 have to check over all charsets.
|
|
102
|
|
103 (2) Since entries are usually added one at a time, you have to be
|
|
104 very careful when creating the tables to avoid realloc()/free()
|
|
105 thrashing in the common case when you are in an area of high
|
|
106 localization and are going to end up using most entries in the
|
|
107 table. You'd certainly want to allow only certain sizes, not
|
|
108 arbitrary ones (probably powers of 2, where you want the entire
|
|
109 block including the START/END values to fit into a power of 2,
|
|
110 minus any malloc overhead if there is any -- there's none under
|
|
111 gmalloc.c, and probably most system malloc() functions are quite
|
|
112 smart nowadays and also have no overhead). You could optimize
|
|
113 somewhat during the in-C initializations, because you can compute
|
|
114 the actual usage of various tables by scanning the entries you're
|
|
115 going to add in a separate pass before adding them. (You could
|
|
116 actually do the same thing when entries are added on the Lisp
|
|
117 level by making the assumption that all the entries will come in
|
|
118 one after another before any use is made of the data. So as
|
|
119 they're coming in, you just store them in a big long list, and
|
|
120 the first time you need to retrieve an entry, you compute the
|
|
121 whole table at once.) You'd still have to deal with the
|
|
122 possibility of later entries coming in, though.
|
|
123
|
|
124 (3) You do lose some speed using START/END values, since you need
|
|
125 a couple of comparisons at each level. This could easily make
|
|
126 each single lookup become 3-4 times slower. The Unicode book
|
|
127 considers this a big issue, and recommends against variable-sized
|
|
128 tables for this reason; however, they almost certainly have in
|
|
129 mind applications that primarily involve conversion of large
|
|
130 amounts of data. Most Unicode strings that are translated in
|
|
131 XEmacs are fairly small. The only place where this might matter
|
|
132 is in loading large files -- e.g. a 3-megabyte Unicode-encoded
|
|
133 file. So think about this, and maybe do a trial implementation
|
|
134 where you don't worry too much about the intricacies of (2) and
|
|
135 just implement some basic "multiply by 1.5" trick or something to
|
|
136 do the resizing. There is a very good FAQ on Unicode called
|
|
137 something like the Linux-Unicode How-To (it should be part of the
|
|
138 Linux How-To's, I think), that lists the url of a guy with a
|
|
139 whole bunch of unicode files you can use to stress-test your
|
|
140 implementations, and he's highly likely to have a good
|
|
141 multi-megabyte Unicode-encoded file (with normal text in it -- if
|
|
142 you created your own just by creating repeated strings of letters
|
|
143 and numbers, you probably wouldn't get accurate results).
|
|
144 */
|
|
145
|
|
146 /* When MULE is not defined, we may still need some Unicode support --
|
|
147 in particular, some Windows API's always want Unicode, and the way
|
|
148 we've set up the Unicode encapsulation, we may as well go ahead and
|
|
149 always use the Unicode versions of split API's. (It would be
|
|
150 trickier to not use them, and pointless -- under NT, the ANSI API's
|
|
151 call the Unicode ones anyway, so in the case of structures, we'd be
|
|
152 converting from Unicode to ANSI structures, only to have the OS
|
|
153 convert them back.) */
|
|
154
|
|
155 Lisp_Object Qunicode;
|
|
156 Lisp_Object Qutf_16, Qutf_8, Qucs_4, Qutf_7;
|
|
157 Lisp_Object Qneed_bom;
|
|
158
|
|
159 Lisp_Object Qutf_16_little_endian, Qutf_16_bom;
|
|
160 Lisp_Object Qutf_16_little_endian_bom;
|
|
161
|
|
162 #ifdef MULE
|
|
163
|
877
|
164 /* #### Using ints for to_unicode is OK (as long as they are >= 32 bits).
|
|
165 However, shouldn't the shorts below be unsigned? */
|
771
|
166 static int *to_unicode_blank_1;
|
|
167 static int **to_unicode_blank_2;
|
|
168
|
|
169 static short *from_unicode_blank_1;
|
|
170 static short **from_unicode_blank_2;
|
|
171 static short ***from_unicode_blank_3;
|
|
172 static short ****from_unicode_blank_4;
|
|
173
|
|
174 #if 0
|
|
175
|
|
176 static const struct lrecord_description to_unicode_level_0_desc[] = {
|
|
177 { XD_END }
|
|
178 };
|
|
179
|
|
180 static const struct struct_description to_unicode_level_0_ptr_desc = {
|
|
181 sizeof (int), to_unicode_level_0_desc
|
|
182 };
|
|
183
|
|
184 static const struct lrecord_description to_unicode_level_1_desc[] = {
|
|
185 { XD_STRUCT_PTR, 0, 96, &to_unicode_level_0_ptr_desc },
|
|
186 { XD_END }
|
|
187 };
|
|
188
|
|
189 static const struct struct_description to_unicode_level_1_ptr_desc = {
|
|
190 0, to_unicode_level_1_desc
|
|
191 };
|
|
192
|
|
193 static const struct lrecord_description to_unicode_level_2_desc[] = {
|
|
194 { XD_STRUCT_PTR, 0, 96, &to_unicode_level_1_ptr_desc },
|
|
195 { XD_END }
|
|
196 };
|
|
197
|
|
198 /* Not static because each charset has a set of to and from tables and
|
|
199 needs to describe them to pdump. */
|
|
200 const struct struct_description to_unicode_description[] = {
|
|
201 { 1, to_unicode_level_1_desc },
|
|
202 { 2, to_unicode_level_2_desc },
|
|
203 { XD_END }
|
|
204 };
|
|
205
|
|
206 static const struct lrecord_description from_unicode_level_0_desc[] = {
|
|
207 { XD_END }
|
|
208 };
|
|
209
|
|
210 static const struct struct_description from_unicode_level_0_ptr_desc = {
|
|
211 sizeof (short), from_unicode_level_0_desc
|
|
212 };
|
|
213
|
|
214 static const struct lrecord_description from_unicode_level_1_desc[] = {
|
|
215 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_0_ptr_desc },
|
|
216 { XD_END }
|
|
217 };
|
|
218
|
|
219 static const struct struct_description from_unicode_level_1_ptr_desc = {
|
|
220 0, from_unicode_level_1_desc
|
|
221 };
|
|
222
|
|
223 static const struct lrecord_description from_unicode_level_2_desc[] = {
|
|
224 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_1_ptr_desc },
|
|
225 { XD_END }
|
|
226 };
|
|
227
|
|
228 static const struct struct_description from_unicode_level_2_ptr_desc = {
|
|
229 0, from_unicode_level_2_desc
|
|
230 };
|
|
231
|
|
232 static const struct lrecord_description from_unicode_level_3_desc[] = {
|
|
233 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_2_ptr_desc },
|
|
234 { XD_END }
|
|
235 };
|
|
236
|
|
237 static const struct struct_description from_unicode_level_3_ptr_desc = {
|
|
238 0, from_unicode_level_3_desc
|
|
239 };
|
|
240
|
|
241 static const struct lrecord_description from_unicode_level_4_desc[] = {
|
|
242 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_3_ptr_desc },
|
|
243 { XD_END }
|
|
244 };
|
|
245
|
|
246 /* Not static because each charset has a set of to and from tables and
|
|
247 needs to describe them to pdump. */
|
|
248 const struct struct_description from_unicode_description[] = {
|
|
249 { 1, from_unicode_level_1_desc },
|
|
250 { 2, from_unicode_level_2_desc },
|
|
251 { 3, from_unicode_level_3_desc },
|
|
252 { 4, from_unicode_level_4_desc },
|
|
253 { XD_END }
|
|
254 };
|
|
255
|
|
256 #endif /* 0 */
|
|
257
|
|
258 static Lisp_Object_dynarr *unicode_precedence_dynarr;
|
|
259
|
|
260 static const struct lrecord_description lo_description_1[] = {
|
|
261 { XD_LISP_OBJECT, 0 },
|
|
262 { XD_END }
|
|
263 };
|
|
264
|
|
265 static const struct struct_description lo_description = {
|
|
266 sizeof (Lisp_Object),
|
|
267 lo_description_1
|
|
268 };
|
|
269
|
|
270 static const struct lrecord_description lod_description_1[] = {
|
|
271 XD_DYNARR_DESC (Lisp_Object_dynarr, &lo_description),
|
|
272 { XD_END }
|
|
273 };
|
|
274
|
|
275 static const struct struct_description lisp_object_dynarr_description = {
|
|
276 sizeof (Lisp_Object_dynarr),
|
|
277 lod_description_1
|
|
278 };
|
|
279
|
|
280 Lisp_Object Vlanguage_unicode_precedence_list;
|
|
281 Lisp_Object Vdefault_unicode_precedence_list;
|
|
282
|
|
283 Lisp_Object Qignore_first_column;
|
|
284
|
|
285
|
|
286 /************************************************************************/
|
|
287 /* Unicode implementation */
|
|
288 /************************************************************************/
|
|
289
|
|
290 #define BREAKUP_UNICODE_CODE(val, u1, u2, u3, u4, levels) \
|
|
291 do { \
|
|
292 int buc_val = (val); \
|
|
293 \
|
|
294 (u1) = buc_val >> 24; \
|
|
295 (u2) = (buc_val >> 16) & 255; \
|
|
296 (u3) = (buc_val >> 8) & 255; \
|
|
297 (u4) = buc_val & 255; \
|
|
298 (levels) = (buc_val <= 0xFF ? 1 : \
|
|
299 buc_val <= 0xFFFF ? 2 : \
|
|
300 buc_val <= 0xFFFFFF ? 3 : \
|
|
301 4); \
|
|
302 } while (0)
|
|
303
|
|
304 static void
|
|
305 init_blank_unicode_tables (void)
|
|
306 {
|
|
307 int i;
|
|
308
|
|
309 from_unicode_blank_1 = xnew_array (short, 256);
|
|
310 from_unicode_blank_2 = xnew_array (short *, 256);
|
|
311 from_unicode_blank_3 = xnew_array (short **, 256);
|
|
312 from_unicode_blank_4 = xnew_array (short ***, 256);
|
|
313 for (i = 0; i < 256; i++)
|
|
314 {
|
877
|
315 /* #### IMWTK: Why does using -1 here work? Simply because there are
|
|
316 no existing 96x96 charsets? */
|
771
|
317 from_unicode_blank_1[i] = (short) -1;
|
|
318 from_unicode_blank_2[i] = from_unicode_blank_1;
|
|
319 from_unicode_blank_3[i] = from_unicode_blank_2;
|
|
320 from_unicode_blank_4[i] = from_unicode_blank_3;
|
|
321 }
|
|
322
|
|
323 to_unicode_blank_1 = xnew_array (int, 96);
|
|
324 to_unicode_blank_2 = xnew_array (int *, 96);
|
|
325 for (i = 0; i < 96; i++)
|
|
326 {
|
877
|
327 /* Here -1 is guaranteed OK. */
|
771
|
328 to_unicode_blank_1[i] = -1;
|
|
329 to_unicode_blank_2[i] = to_unicode_blank_1;
|
|
330 }
|
|
331 }
|
|
332
|
|
333 static void *
|
|
334 create_new_from_unicode_table (int level)
|
|
335 {
|
|
336 switch (level)
|
|
337 {
|
|
338 /* WARNING: If you are thinking of compressing these, keep in
|
|
339 mind that sizeof (short) does not equal sizeof (short *). */
|
|
340 case 1:
|
|
341 {
|
|
342 short *newtab = xnew_array (short, 256);
|
|
343 memcpy (newtab, from_unicode_blank_1, 256 * sizeof (short));
|
|
344 return newtab;
|
|
345 }
|
|
346 case 2:
|
|
347 {
|
|
348 short **newtab = xnew_array (short *, 256);
|
|
349 memcpy (newtab, from_unicode_blank_2, 256 * sizeof (short *));
|
|
350 return newtab;
|
|
351 }
|
|
352 case 3:
|
|
353 {
|
|
354 short ***newtab = xnew_array (short **, 256);
|
|
355 memcpy (newtab, from_unicode_blank_3, 256 * sizeof (short **));
|
|
356 return newtab;
|
|
357 }
|
|
358 case 4:
|
|
359 {
|
|
360 short ****newtab = xnew_array (short ***, 256);
|
|
361 memcpy (newtab, from_unicode_blank_4, 256 * sizeof (short ***));
|
|
362 return newtab;
|
|
363 }
|
|
364 default:
|
|
365 abort ();
|
|
366 return 0;
|
|
367 }
|
|
368 }
|
|
369
|
877
|
370 /* Allocate and blank the tables.
|
|
371 Loading them up is done by parse-unicode-translation-table. */
|
771
|
372 void
|
|
373 init_charset_unicode_tables (Lisp_Object charset)
|
|
374 {
|
|
375 if (XCHARSET_DIMENSION (charset) == 1)
|
|
376 {
|
|
377 int *to_table = xnew_array (int, 96);
|
|
378 memcpy (to_table, to_unicode_blank_1, 96 * sizeof (int));
|
|
379 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
380 }
|
|
381 else
|
|
382 {
|
|
383 int **to_table = xnew_array (int *, 96);
|
|
384 memcpy (to_table, to_unicode_blank_2, 96 * sizeof (int *));
|
|
385 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
386 }
|
|
387
|
|
388 {
|
|
389 XCHARSET_FROM_UNICODE_TABLE (charset) = create_new_from_unicode_table (1);
|
|
390 XCHARSET_FROM_UNICODE_LEVELS (charset) = 1;
|
|
391 }
|
|
392 }
|
|
393
|
|
394 static void
|
|
395 free_from_unicode_table (void *table, int level)
|
|
396 {
|
|
397 int i;
|
|
398
|
|
399 switch (level)
|
|
400 {
|
|
401 case 2:
|
|
402 {
|
|
403 short **tab = (short **) table;
|
|
404 for (i = 0; i < 256; i++)
|
|
405 {
|
|
406 if (tab[i] != from_unicode_blank_1)
|
|
407 free_from_unicode_table (tab[i], 1);
|
|
408 }
|
|
409 break;
|
|
410 }
|
|
411 case 3:
|
|
412 {
|
|
413 short ***tab = (short ***) table;
|
|
414 for (i = 0; i < 256; i++)
|
|
415 {
|
|
416 if (tab[i] != from_unicode_blank_2)
|
|
417 free_from_unicode_table (tab[i], 2);
|
|
418 }
|
|
419 break;
|
|
420 }
|
|
421 case 4:
|
|
422 {
|
|
423 short ****tab = (short ****) table;
|
|
424 for (i = 0; i < 256; i++)
|
|
425 {
|
|
426 if (tab[i] != from_unicode_blank_3)
|
|
427 free_from_unicode_table (tab[i], 3);
|
|
428 }
|
|
429 break;
|
|
430 }
|
|
431 }
|
|
432
|
|
433 xfree (table);
|
|
434 }
|
|
435
|
|
436 static void
|
|
437 free_to_unicode_table (void *table, int level)
|
|
438 {
|
|
439 if (level == 2)
|
|
440 {
|
|
441 int i;
|
|
442 int **tab = (int **) table;
|
|
443
|
|
444 for (i = 0; i < 96; i++)
|
|
445 {
|
|
446 if (tab[i] != to_unicode_blank_1)
|
|
447 free_to_unicode_table (tab[i], 1);
|
|
448 }
|
|
449 }
|
|
450
|
|
451 xfree (table);
|
|
452 }
|
|
453
|
|
454 void
|
|
455 free_charset_unicode_tables (Lisp_Object charset)
|
|
456 {
|
|
457 free_to_unicode_table (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
458 XCHARSET_DIMENSION (charset));
|
|
459 free_from_unicode_table (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
460 XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
461 }
|
|
462
|
|
463 #ifdef MEMORY_USAGE_STATS
|
|
464
|
|
465 static Bytecount
|
|
466 compute_from_unicode_table_size_1 (void *table, int level,
|
|
467 struct overhead_stats *stats)
|
|
468 {
|
|
469 int i;
|
|
470 Bytecount size = 0;
|
|
471
|
|
472 switch (level)
|
|
473 {
|
|
474 case 2:
|
|
475 {
|
|
476 short **tab = (short **) table;
|
|
477 for (i = 0; i < 256; i++)
|
|
478 {
|
|
479 if (tab[i] != from_unicode_blank_1)
|
|
480 size += compute_from_unicode_table_size_1 (tab[i], 1, stats);
|
|
481 }
|
|
482 break;
|
|
483 }
|
|
484 case 3:
|
|
485 {
|
|
486 short ***tab = (short ***) table;
|
|
487 for (i = 0; i < 256; i++)
|
|
488 {
|
|
489 if (tab[i] != from_unicode_blank_2)
|
|
490 size += compute_from_unicode_table_size_1 (tab[i], 2, stats);
|
|
491 }
|
|
492 break;
|
|
493 }
|
|
494 case 4:
|
|
495 {
|
|
496 short ****tab = (short ****) table;
|
|
497 for (i = 0; i < 256; i++)
|
|
498 {
|
|
499 if (tab[i] != from_unicode_blank_3)
|
|
500 size += compute_from_unicode_table_size_1 (tab[i], 3, stats);
|
|
501 }
|
|
502 break;
|
|
503 }
|
|
504 }
|
|
505
|
|
506 size += malloced_storage_size (table,
|
|
507 256 * (level == 1 ? sizeof (short) :
|
|
508 sizeof (void *)),
|
|
509 stats);
|
|
510 return size;
|
|
511 }
|
|
512
|
|
513 static Bytecount
|
|
514 compute_to_unicode_table_size_1 (void *table, int level,
|
|
515 struct overhead_stats *stats)
|
|
516 {
|
|
517 Bytecount size = 0;
|
|
518
|
|
519 if (level == 2)
|
|
520 {
|
|
521 int i;
|
|
522 int **tab = (int **) table;
|
|
523
|
|
524 for (i = 0; i < 96; i++)
|
|
525 {
|
|
526 if (tab[i] != to_unicode_blank_1)
|
|
527 size += compute_to_unicode_table_size_1 (tab[i], 1, stats);
|
|
528 }
|
|
529 }
|
|
530
|
|
531 size += malloced_storage_size (table,
|
|
532 96 * (level == 1 ? sizeof (int) :
|
|
533 sizeof (void *)),
|
|
534 stats);
|
|
535 return size;
|
|
536 }
|
|
537
|
|
538 Bytecount
|
|
539 compute_from_unicode_table_size (Lisp_Object charset,
|
|
540 struct overhead_stats *stats)
|
|
541 {
|
|
542 return (compute_from_unicode_table_size_1
|
|
543 (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
544 XCHARSET_FROM_UNICODE_LEVELS (charset),
|
|
545 stats));
|
|
546 }
|
|
547
|
|
548 Bytecount
|
|
549 compute_to_unicode_table_size (Lisp_Object charset,
|
|
550 struct overhead_stats *stats)
|
|
551 {
|
|
552 return (compute_to_unicode_table_size_1
|
|
553 (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
554 XCHARSET_DIMENSION (charset),
|
|
555 stats));
|
|
556 }
|
|
557
|
|
558 #endif
|
|
559
|
|
560 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
561
|
|
562 /* "Sledgehammer checks" are checks that verify the self-consistency
|
|
563 of an entire structure every time a change is about to be made or
|
|
564 has been made to the structure. Not fast but a pretty much
|
|
565 sure-fire way of flushing out any incorrectnesses in the algorithms
|
|
566 that create the structure.
|
|
567
|
|
568 Checking only after a change has been made will speed things up by
|
|
569 a factor of 2, but it doesn't absolutely prove that the code just
|
|
570 checked caused the problem; perhaps it happened elsewhere, either
|
|
571 in some code you forgot to sledgehammer check or as a result of
|
|
572 data corruption. */
|
|
573
|
|
574 static void
|
|
575 assert_not_any_blank_table (void *tab)
|
|
576 {
|
|
577 assert (tab != from_unicode_blank_1);
|
|
578 assert (tab != from_unicode_blank_2);
|
|
579 assert (tab != from_unicode_blank_3);
|
|
580 assert (tab != from_unicode_blank_4);
|
|
581 assert (tab != to_unicode_blank_1);
|
|
582 assert (tab != to_unicode_blank_2);
|
|
583 assert (tab);
|
|
584 }
|
|
585
|
|
586 static void
|
|
587 sledgehammer_check_from_table (Lisp_Object charset, void *table, int level,
|
|
588 int codetop)
|
|
589 {
|
|
590 int i;
|
|
591
|
|
592 switch (level)
|
|
593 {
|
|
594 case 1:
|
|
595 {
|
|
596 short *tab = (short *) table;
|
|
597 for (i = 0; i < 256; i++)
|
|
598 {
|
|
599 if (tab[i] != -1)
|
|
600 {
|
|
601 Lisp_Object char_charset;
|
|
602 int c1, c2;
|
|
603
|
867
|
604 assert (valid_ichar_p (tab[i]));
|
|
605 BREAKUP_ICHAR (tab[i], char_charset, c1, c2);
|
771
|
606 assert (EQ (charset, char_charset));
|
|
607 if (XCHARSET_DIMENSION (charset) == 1)
|
|
608 {
|
|
609 int *to_table =
|
|
610 (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
611 assert_not_any_blank_table (to_table);
|
|
612 assert (to_table[c1 - 32] == (codetop << 8) + i);
|
|
613 }
|
|
614 else
|
|
615 {
|
|
616 int **to_table =
|
|
617 (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
618 assert_not_any_blank_table (to_table);
|
|
619 assert_not_any_blank_table (to_table[c1 - 32]);
|
|
620 assert (to_table[c1 - 32][c2 - 32] == (codetop << 8) + i);
|
|
621 }
|
|
622 }
|
|
623 }
|
|
624 break;
|
|
625 }
|
|
626 case 2:
|
|
627 {
|
|
628 short **tab = (short **) table;
|
|
629 for (i = 0; i < 256; i++)
|
|
630 {
|
|
631 if (tab[i] != from_unicode_blank_1)
|
|
632 sledgehammer_check_from_table (charset, tab[i], 1,
|
|
633 (codetop << 8) + i);
|
|
634 }
|
|
635 break;
|
|
636 }
|
|
637 case 3:
|
|
638 {
|
|
639 short ***tab = (short ***) table;
|
|
640 for (i = 0; i < 256; i++)
|
|
641 {
|
|
642 if (tab[i] != from_unicode_blank_2)
|
|
643 sledgehammer_check_from_table (charset, tab[i], 2,
|
|
644 (codetop << 8) + i);
|
|
645 }
|
|
646 break;
|
|
647 }
|
|
648 case 4:
|
|
649 {
|
|
650 short ****tab = (short ****) table;
|
|
651 for (i = 0; i < 256; i++)
|
|
652 {
|
|
653 if (tab[i] != from_unicode_blank_3)
|
|
654 sledgehammer_check_from_table (charset, tab[i], 3,
|
|
655 (codetop << 8) + i);
|
|
656 }
|
|
657 break;
|
|
658 }
|
|
659 default:
|
|
660 abort ();
|
|
661 }
|
|
662 }
|
|
663
|
|
664 static void
|
|
665 sledgehammer_check_to_table (Lisp_Object charset, void *table, int level,
|
|
666 int codetop)
|
|
667 {
|
|
668 int i;
|
|
669
|
|
670 switch (level)
|
|
671 {
|
|
672 case 1:
|
|
673 {
|
|
674 int *tab = (int *) table;
|
|
675
|
|
676 if (XCHARSET_CHARS (charset) == 94)
|
|
677 {
|
|
678 assert (tab[0] == -1);
|
|
679 assert (tab[95] == -1);
|
|
680 }
|
|
681
|
|
682 for (i = 0; i < 96; i++)
|
|
683 {
|
|
684 if (tab[i] != -1)
|
|
685 {
|
|
686 int u4, u3, u2, u1, levels;
|
867
|
687 Ichar ch;
|
|
688 Ichar this_ch;
|
771
|
689 short val;
|
|
690 void *frtab = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
691
|
|
692 if (XCHARSET_DIMENSION (charset) == 1)
|
867
|
693 this_ch = make_ichar (charset, i + 32, 0);
|
771
|
694 else
|
867
|
695 this_ch = make_ichar (charset, codetop + 32, i + 32);
|
771
|
696
|
|
697 assert (tab[i] >= 0);
|
|
698 BREAKUP_UNICODE_CODE (tab[i], u4, u3, u2, u1, levels);
|
|
699 assert (levels <= XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
700
|
|
701 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
702 {
|
|
703 case 1: val = ((short *) frtab)[u1]; break;
|
|
704 case 2: val = ((short **) frtab)[u2][u1]; break;
|
|
705 case 3: val = ((short ***) frtab)[u3][u2][u1]; break;
|
|
706 case 4: val = ((short ****) frtab)[u4][u3][u2][u1]; break;
|
|
707 default: abort ();
|
|
708 }
|
|
709
|
867
|
710 ch = make_ichar (charset, val >> 8, val & 0xFF);
|
771
|
711 assert (ch == this_ch);
|
|
712
|
|
713 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
714 {
|
|
715 case 4:
|
|
716 assert_not_any_blank_table (frtab);
|
|
717 frtab = ((short ****) frtab)[u4];
|
|
718 /* fall through */
|
|
719 case 3:
|
|
720 assert_not_any_blank_table (frtab);
|
|
721 frtab = ((short ***) frtab)[u3];
|
|
722 /* fall through */
|
|
723 case 2:
|
|
724 assert_not_any_blank_table (frtab);
|
|
725 frtab = ((short **) frtab)[u2];
|
|
726 /* fall through */
|
|
727 case 1:
|
|
728 assert_not_any_blank_table (frtab);
|
|
729 break;
|
|
730 default: abort ();
|
|
731 }
|
|
732 }
|
|
733 }
|
|
734 break;
|
|
735 }
|
|
736 case 2:
|
|
737 {
|
|
738 int **tab = (int **) table;
|
|
739
|
|
740 if (XCHARSET_CHARS (charset) == 94)
|
|
741 {
|
|
742 assert (tab[0] == to_unicode_blank_1);
|
|
743 assert (tab[95] == to_unicode_blank_1);
|
|
744 }
|
|
745
|
|
746 for (i = 0; i < 96; i++)
|
|
747 {
|
|
748 if (tab[i] != to_unicode_blank_1)
|
|
749 sledgehammer_check_to_table (charset, tab[i], 1, i);
|
|
750 }
|
|
751 break;
|
|
752 }
|
|
753 default:
|
|
754 abort ();
|
|
755 }
|
|
756 }
|
|
757
|
|
758 static void
|
|
759 sledgehammer_check_unicode_tables (Lisp_Object charset)
|
|
760 {
|
|
761 /* verify that the blank tables have not been modified */
|
|
762 int i;
|
|
763 int from_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
764 int to_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
765
|
|
766 for (i = 0; i < 256; i++)
|
|
767 {
|
|
768 assert (from_unicode_blank_1[i] == (short) -1);
|
|
769 assert (from_unicode_blank_2[i] == from_unicode_blank_1);
|
|
770 assert (from_unicode_blank_3[i] == from_unicode_blank_2);
|
|
771 assert (from_unicode_blank_4[i] == from_unicode_blank_3);
|
|
772 }
|
|
773
|
|
774 for (i = 0; i < 96; i++)
|
|
775 {
|
|
776 assert (to_unicode_blank_1[i] == -1);
|
|
777 assert (to_unicode_blank_2[i] == to_unicode_blank_1);
|
|
778 }
|
|
779
|
|
780 assert (from_level >= 1 && from_level <= 4);
|
|
781
|
|
782 sledgehammer_check_from_table (charset,
|
|
783 XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
784 from_level, 0);
|
|
785
|
|
786 sledgehammer_check_to_table (charset,
|
|
787 XCHARSET_TO_UNICODE_TABLE (charset),
|
|
788 XCHARSET_DIMENSION (charset), 0);
|
|
789 }
|
|
790
|
|
791 #endif /* SLEDGEHAMMER_CHECK_UNICODE */
|
|
792
|
|
793 static void
|
867
|
794 set_unicode_conversion (Ichar chr, int code)
|
771
|
795 {
|
|
796 Lisp_Object charset;
|
|
797 int c1, c2;
|
|
798
|
867
|
799 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
800
|
877
|
801 /* I tried an assert on code > 255 || chr == code, but that fails because
|
|
802 Mule gives many Latin characters separate code points for different
|
|
803 ISO 8859 coded character sets. Obvious in hindsight.... */
|
|
804 assert (!EQ (charset, Vcharset_ascii) || chr == code);
|
|
805 assert (!EQ (charset, Vcharset_latin_iso8859_1) || chr == code);
|
|
806 assert (!EQ (charset, Vcharset_control_1) || chr == code);
|
|
807
|
|
808 /* This assert is needed because it is simply unimplemented. */
|
771
|
809 assert (!EQ (charset, Vcharset_composite));
|
|
810
|
|
811 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
812 sledgehammer_check_unicode_tables (charset);
|
|
813 #endif
|
|
814
|
|
815 /* First, the char -> unicode translation */
|
|
816
|
|
817 if (XCHARSET_DIMENSION (charset) == 1)
|
|
818 {
|
|
819 int *to_table = (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
820 to_table[c1 - 32] = code;
|
|
821 }
|
|
822 else
|
|
823 {
|
|
824 int **to_table_2 = (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
825 int *to_table_1;
|
|
826
|
|
827 assert (XCHARSET_DIMENSION (charset) == 2);
|
|
828 to_table_1 = to_table_2[c1 - 32];
|
|
829 if (to_table_1 == to_unicode_blank_1)
|
|
830 {
|
|
831 to_table_1 = xnew_array (int, 96);
|
|
832 memcpy (to_table_1, to_unicode_blank_1, 96 * sizeof (int));
|
|
833 to_table_2[c1 - 32] = to_table_1;
|
|
834 }
|
|
835 to_table_1[c2 - 32] = code;
|
|
836 }
|
|
837
|
|
838 /* Then, unicode -> char: much harder */
|
|
839
|
|
840 {
|
|
841 int charset_levels;
|
|
842 int u4, u3, u2, u1;
|
|
843 int code_levels;
|
|
844 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
845
|
|
846 charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
847
|
|
848 /* Make sure the charset's tables have at least as many levels as
|
|
849 the code point has: Note that the charset is guaranteed to have
|
|
850 at least one level, because it was created that way */
|
|
851 if (charset_levels < code_levels)
|
|
852 {
|
|
853 int i;
|
|
854
|
|
855 assert (charset_levels > 0);
|
|
856 for (i = 2; i <= code_levels; i++)
|
|
857 {
|
|
858 if (charset_levels < i)
|
|
859 {
|
|
860 void *old_table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
861 void *table = create_new_from_unicode_table (i);
|
|
862 XCHARSET_FROM_UNICODE_TABLE (charset) = table;
|
|
863
|
|
864 switch (i)
|
|
865 {
|
|
866 case 2:
|
|
867 ((short **) table)[0] = (short *) old_table;
|
|
868 break;
|
|
869 case 3:
|
|
870 ((short ***) table)[0] = (short **) old_table;
|
|
871 break;
|
|
872 case 4:
|
|
873 ((short ****) table)[0] = (short ***) old_table;
|
|
874 break;
|
|
875 default: abort ();
|
|
876 }
|
|
877 }
|
|
878 }
|
|
879
|
|
880 charset_levels = code_levels;
|
|
881 XCHARSET_FROM_UNICODE_LEVELS (charset) = code_levels;
|
|
882 }
|
|
883
|
|
884 /* Now, make sure there is a non-default table at each level */
|
|
885 {
|
|
886 int i;
|
|
887 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
888
|
|
889 for (i = charset_levels; i >= 2; i--)
|
|
890 {
|
|
891 switch (i)
|
|
892 {
|
|
893 case 4:
|
|
894 if (((short ****) table)[u4] == from_unicode_blank_3)
|
|
895 ((short ****) table)[u4] =
|
|
896 ((short ***) create_new_from_unicode_table (3));
|
|
897 table = ((short ****) table)[u4];
|
|
898 break;
|
|
899 case 3:
|
|
900 if (((short ***) table)[u3] == from_unicode_blank_2)
|
|
901 ((short ***) table)[u3] =
|
|
902 ((short **) create_new_from_unicode_table (2));
|
|
903 table = ((short ***) table)[u3];
|
|
904 break;
|
|
905 case 2:
|
|
906 if (((short **) table)[u2] == from_unicode_blank_1)
|
|
907 ((short **) table)[u2] =
|
|
908 ((short *) create_new_from_unicode_table (1));
|
|
909 table = ((short **) table)[u2];
|
|
910 break;
|
|
911 default: abort ();
|
|
912 }
|
|
913 }
|
|
914 }
|
|
915
|
|
916 /* Finally, set the character */
|
|
917
|
|
918 {
|
|
919 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
920 switch (charset_levels)
|
|
921 {
|
|
922 case 1: ((short *) table)[u1] = (c1 << 8) + c2; break;
|
|
923 case 2: ((short **) table)[u2][u1] = (c1 << 8) + c2; break;
|
|
924 case 3: ((short ***) table)[u3][u2][u1] = (c1 << 8) + c2; break;
|
|
925 case 4: ((short ****) table)[u4][u3][u2][u1] = (c1 << 8) + c2; break;
|
|
926 default: abort ();
|
|
927 }
|
|
928 }
|
|
929 }
|
|
930
|
|
931 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
932 sledgehammer_check_unicode_tables (charset);
|
|
933 #endif
|
|
934 }
|
|
935
|
788
|
936 int
|
867
|
937 ichar_to_unicode (Ichar chr)
|
771
|
938 {
|
|
939 Lisp_Object charset;
|
|
940 int c1, c2;
|
|
941
|
867
|
942 type_checking_assert (valid_ichar_p (chr));
|
877
|
943 /* This shortcut depends on the representation of an Ichar, see text.c. */
|
771
|
944 if (chr < 256)
|
|
945 return (int) chr;
|
|
946
|
867
|
947 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
948 if (EQ (charset, Vcharset_composite))
|
|
949 return -1; /* #### don't know how to handle */
|
|
950 else if (XCHARSET_DIMENSION (charset) == 1)
|
|
951 return ((int *) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32];
|
|
952 else
|
|
953 return ((int **) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32][c2 - 32];
|
|
954 }
|
|
955
|
867
|
956 static Ichar
|
877
|
957 unicode_to_ichar (int code, Lisp_Object_dynarr *charsets)
|
771
|
958 {
|
|
959 int u1, u2, u3, u4;
|
|
960 int code_levels;
|
|
961 int i;
|
|
962 int n = Dynarr_length (charsets);
|
|
963
|
|
964 type_checking_assert (code >= 0);
|
877
|
965 /* This shortcut depends on the representation of an Ichar, see text.c.
|
|
966 Note that it may _not_ be extended to U+00A0 to U+00FF (many ISO 8859
|
893
|
967 coded character sets have points that map into that region, so this
|
|
968 function is many-valued). */
|
877
|
969 if (code < 0xA0)
|
867
|
970 return (Ichar) code;
|
771
|
971
|
|
972 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
973
|
|
974 for (i = 0; i < n; i++)
|
|
975 {
|
|
976 Lisp_Object charset = Dynarr_at (charsets, i);
|
|
977 int charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
978 if (charset_levels >= code_levels)
|
|
979 {
|
|
980 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
981 short retval;
|
|
982
|
|
983 switch (charset_levels)
|
|
984 {
|
|
985 case 1: retval = ((short *) table)[u1]; break;
|
|
986 case 2: retval = ((short **) table)[u2][u1]; break;
|
|
987 case 3: retval = ((short ***) table)[u3][u2][u1]; break;
|
|
988 case 4: retval = ((short ****) table)[u4][u3][u2][u1]; break;
|
|
989 default: abort (); retval = 0;
|
|
990 }
|
|
991
|
|
992 if (retval != -1)
|
867
|
993 return make_ichar (charset, retval >> 8, retval & 0xFF);
|
771
|
994 }
|
|
995 }
|
|
996
|
867
|
997 return (Ichar) -1;
|
771
|
998 }
|
|
999
|
877
|
1000 /* Add charsets to precedence list.
|
|
1001 LIST must be a list of charsets. Charsets which are in the list more
|
|
1002 than once are given the precedence implied by their earliest appearance.
|
|
1003 Later appearances are ignored. */
|
771
|
1004 static void
|
|
1005 add_charsets_to_precedence_list (Lisp_Object list, int *lbs,
|
|
1006 Lisp_Object_dynarr *dynarr)
|
|
1007 {
|
|
1008 {
|
|
1009 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1010 {
|
|
1011 Lisp_Object charset = Fget_charset (elt);
|
778
|
1012 int lb = XCHARSET_LEADING_BYTE (charset);
|
771
|
1013 if (lbs[lb - MIN_LEADING_BYTE] == 0)
|
|
1014 {
|
877
|
1015 Dynarr_add (dynarr, charset);
|
771
|
1016 lbs[lb - MIN_LEADING_BYTE] = 1;
|
|
1017 }
|
|
1018 }
|
|
1019 }
|
|
1020 }
|
|
1021
|
877
|
1022 /* Rebuild the charset precedence array.
|
|
1023 The "charsets preferred for the current language" get highest precedence,
|
|
1024 followed by the "charsets preferred by default", ordered as in
|
|
1025 Vlanguage_unicode_precedence_list and Vdefault_unicode_precedence_list,
|
|
1026 respectively. All remaining charsets follow in an arbitrary order. */
|
771
|
1027 void
|
|
1028 recalculate_unicode_precedence (void)
|
|
1029 {
|
|
1030 int lbs[NUM_LEADING_BYTES];
|
|
1031 int i;
|
|
1032
|
|
1033 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1034 lbs[i] = 0;
|
|
1035
|
|
1036 Dynarr_reset (unicode_precedence_dynarr);
|
|
1037
|
|
1038 add_charsets_to_precedence_list (Vlanguage_unicode_precedence_list,
|
|
1039 lbs, unicode_precedence_dynarr);
|
|
1040 add_charsets_to_precedence_list (Vdefault_unicode_precedence_list,
|
|
1041 lbs, unicode_precedence_dynarr);
|
|
1042
|
|
1043 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1044 {
|
|
1045 if (lbs[i] == 0)
|
|
1046 {
|
826
|
1047 Lisp_Object charset = charset_by_leading_byte (i + MIN_LEADING_BYTE);
|
771
|
1048 if (!NILP (charset))
|
|
1049 Dynarr_add (unicode_precedence_dynarr, charset);
|
|
1050 }
|
|
1051 }
|
|
1052 }
|
|
1053
|
877
|
1054 DEFUN ("unicode-precedence-list",
|
|
1055 Funicode_precedence_list,
|
|
1056 0, 0, 0, /*
|
|
1057 Return the precedence order among charsets used for Unicode decoding.
|
|
1058
|
|
1059 Value is a list of charsets, which are searched in order for a translation
|
|
1060 matching a given Unicode character.
|
|
1061
|
|
1062 The highest precedence is given to the language-specific precedence list of
|
|
1063 charsets, defined by `set-language-unicode-precedence-list'. These are
|
|
1064 followed by charsets in the default precedence list, defined by
|
|
1065 `set-default-unicode-precedence-list'. Charsets occurring multiple times are
|
|
1066 given precedence according to their first occurrance in either list. These
|
|
1067 are followed by the remaining charsets, in some arbitrary order.
|
771
|
1068
|
|
1069 The language-specific precedence list is meant to be set as part of the
|
|
1070 language environment initialization; the default precedence list is meant
|
|
1071 to be set by the user.
|
|
1072 */
|
877
|
1073 ())
|
|
1074 {
|
|
1075 int i;
|
|
1076 Lisp_Object list = Qnil;
|
|
1077
|
|
1078 for (i = Dynarr_length (unicode_precedence_dynarr) - 1; i >= 0; i--)
|
|
1079 list = Fcons (Dynarr_at (unicode_precedence_dynarr, i), list);
|
|
1080 return list;
|
|
1081 }
|
|
1082
|
|
1083
|
|
1084 /* #### This interface is wrong. Cyrillic users and Chinese users are going
|
|
1085 to have varying opinions about whether ISO Cyrillic, KOI8-R, or Windows
|
|
1086 1251 should take precedence, and whether Big Five or CNS should take
|
|
1087 precedence, respectively. This means that users are sometimes going to
|
|
1088 want to set Vlanguage_unicode_precedence_list.
|
|
1089 Furthermore, this should be language-local (buffer-local would be a
|
|
1090 reasonable approximation). */
|
|
1091 DEFUN ("set-language-unicode-precedence-list",
|
|
1092 Fset_language_unicode_precedence_list,
|
|
1093 1, 1, 0, /*
|
|
1094 Set the language-specific precedence of charsets in Unicode decoding.
|
|
1095 LIST is a list of charsets.
|
|
1096 See `unicode-precedence-list' for more information.
|
|
1097 */
|
771
|
1098 (list))
|
|
1099 {
|
|
1100 {
|
|
1101 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1102 Fget_charset (elt);
|
|
1103 }
|
|
1104
|
|
1105 Vlanguage_unicode_precedence_list = list;
|
|
1106 recalculate_unicode_precedence ();
|
|
1107 return Qnil;
|
|
1108 }
|
|
1109
|
|
1110 DEFUN ("language-unicode-precedence-list",
|
|
1111 Flanguage_unicode_precedence_list,
|
|
1112 0, 0, 0, /*
|
|
1113 Return the language-specific precedence list used for Unicode decoding.
|
877
|
1114 See `unicode-precedence-list' for more information.
|
771
|
1115 */
|
|
1116 ())
|
|
1117 {
|
|
1118 return Vlanguage_unicode_precedence_list;
|
|
1119 }
|
|
1120
|
|
1121 DEFUN ("set-default-unicode-precedence-list",
|
|
1122 Fset_default_unicode_precedence_list,
|
|
1123 1, 1, 0, /*
|
|
1124 Set the default precedence list used for Unicode decoding.
|
877
|
1125 This is intended to be set by the user. See
|
|
1126 `unicode-precedence-list' for more information.
|
771
|
1127 */
|
|
1128 (list))
|
|
1129 {
|
|
1130 {
|
|
1131 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1132 Fget_charset (elt);
|
|
1133 }
|
|
1134
|
|
1135 Vdefault_unicode_precedence_list = list;
|
|
1136 recalculate_unicode_precedence ();
|
|
1137 return Qnil;
|
|
1138 }
|
|
1139
|
|
1140 DEFUN ("default-unicode-precedence-list",
|
|
1141 Fdefault_unicode_precedence_list,
|
|
1142 0, 0, 0, /*
|
|
1143 Return the default precedence list used for Unicode decoding.
|
877
|
1144 See `unicode-precedence-list' for more information.
|
771
|
1145 */
|
|
1146 ())
|
|
1147 {
|
|
1148 return Vdefault_unicode_precedence_list;
|
|
1149 }
|
|
1150
|
|
1151 DEFUN ("set-unicode-conversion", Fset_unicode_conversion,
|
|
1152 2, 2, 0, /*
|
|
1153 Add conversion information between Unicode codepoints and characters.
|
877
|
1154 Conversions for U+0000 to U+00FF are hardwired to ASCII, Control-1, and
|
|
1155 Latin-1. Attempts to set these values will raise an error.
|
|
1156
|
771
|
1157 CHARACTER is one of the following:
|
|
1158
|
|
1159 -- A character (in which case CODE must be a non-negative integer; values
|
|
1160 above 2^20 - 1 are allowed for the purpose of specifying private
|
877
|
1161 characters, but are illegal in standard Unicode---they will cause errors
|
|
1162 when converted to utf-16)
|
771
|
1163 -- A vector of characters (in which case CODE must be a vector of integers
|
|
1164 of the same length)
|
|
1165 */
|
|
1166 (character, code))
|
|
1167 {
|
|
1168 Lisp_Object charset;
|
877
|
1169 int ichar, unicode;
|
771
|
1170
|
|
1171 CHECK_CHAR (character);
|
|
1172 CHECK_NATNUM (code);
|
|
1173
|
877
|
1174 unicode = XINT (code);
|
|
1175 ichar = XCHAR (character);
|
|
1176 charset = ichar_charset (ichar);
|
|
1177
|
|
1178 /* The translations of ASCII, Control-1, and Latin-1 code points are
|
|
1179 hard-coded in ichar_to_unicode and unicode_to_ichar.
|
|
1180
|
|
1181 Checking unicode < 256 && ichar != unicode is wrong because Mule gives
|
|
1182 many Latin characters code points in a few different character sets. */
|
|
1183 if ((EQ (charset, Vcharset_ascii) ||
|
|
1184 EQ (charset, Vcharset_control_1) ||
|
|
1185 EQ (charset, Vcharset_latin_iso8859_1))
|
|
1186 && unicode != ichar)
|
893
|
1187 signal_error (Qinvalid_argument, "Can't change Unicode translation for ASCII, Control-1 or Latin-1 character",
|
771
|
1188 character);
|
|
1189
|
877
|
1190 /* #### Composite characters are not properly implemented yet. */
|
|
1191 if (EQ (charset, Vcharset_composite))
|
|
1192 signal_error (Qinvalid_argument, "Can't set Unicode translation for Composite char",
|
|
1193 character);
|
|
1194
|
|
1195 set_unicode_conversion (ichar, unicode);
|
771
|
1196 return Qnil;
|
|
1197 }
|
|
1198
|
|
1199 #endif /* MULE */
|
|
1200
|
800
|
1201 DEFUN ("char-to-unicode", Fchar_to_unicode, 1, 1, 0, /*
|
771
|
1202 Convert character to Unicode codepoint.
|
877
|
1203 When there is no international support (i.e. the 'mule feature is not
|
|
1204 present), this function simply does `char-to-int'.
|
771
|
1205 */
|
|
1206 (character))
|
|
1207 {
|
|
1208 CHECK_CHAR (character);
|
|
1209 #ifdef MULE
|
867
|
1210 return make_int (ichar_to_unicode (XCHAR (character)));
|
771
|
1211 #else
|
|
1212 return Fchar_to_int (character);
|
|
1213 #endif /* MULE */
|
|
1214 }
|
|
1215
|
800
|
1216 DEFUN ("unicode-to-char", Funicode_to_char, 1, 2, 0, /*
|
771
|
1217 Convert Unicode codepoint to character.
|
|
1218 CODE should be a non-negative integer.
|
|
1219 If CHARSETS is given, it should be a list of charsets, and only those
|
|
1220 charsets will be consulted, in the given order, for a translation.
|
|
1221 Otherwise, the default ordering of all charsets will be given (see
|
|
1222 `set-unicode-charset-precedence').
|
|
1223
|
877
|
1224 When there is no international support (i.e. the 'mule feature is not
|
|
1225 present), this function simply does `int-to-char' and ignores the CHARSETS
|
|
1226 argument.
|
771
|
1227 */
|
|
1228 (code, charsets))
|
|
1229 {
|
|
1230 #ifdef MULE
|
|
1231 Lisp_Object_dynarr *dyn;
|
|
1232 int lbs[NUM_LEADING_BYTES];
|
|
1233 int c;
|
|
1234
|
|
1235 CHECK_NATNUM (code);
|
|
1236 c = XINT (code);
|
|
1237 {
|
|
1238 EXTERNAL_LIST_LOOP_2 (elt, charsets)
|
|
1239 Fget_charset (elt);
|
|
1240 }
|
|
1241
|
|
1242 if (NILP (charsets))
|
|
1243 {
|
877
|
1244 Ichar ret = unicode_to_ichar (c, unicode_precedence_dynarr);
|
771
|
1245 if (ret == -1)
|
|
1246 return Qnil;
|
|
1247 return make_char (ret);
|
|
1248 }
|
|
1249
|
|
1250 dyn = Dynarr_new (Lisp_Object);
|
|
1251 memset (lbs, 0, NUM_LEADING_BYTES * sizeof (int));
|
|
1252 add_charsets_to_precedence_list (charsets, lbs, dyn);
|
|
1253 {
|
877
|
1254 Ichar ret = unicode_to_ichar (c, dyn);
|
771
|
1255 Dynarr_free (dyn);
|
|
1256 if (ret == -1)
|
|
1257 return Qnil;
|
|
1258 return make_char (ret);
|
|
1259 }
|
|
1260 #else
|
|
1261 CHECK_NATNUM (code);
|
|
1262 return Fint_to_char (code);
|
|
1263 #endif /* MULE */
|
|
1264 }
|
|
1265
|
872
|
1266 #ifdef MULE
|
|
1267
|
771
|
1268 static Lisp_Object
|
|
1269 cerrar_el_fulano (Lisp_Object fulano)
|
|
1270 {
|
|
1271 FILE *file = (FILE *) get_opaque_ptr (fulano);
|
|
1272 retry_fclose (file);
|
|
1273 return Qnil;
|
|
1274 }
|
|
1275
|
877
|
1276 /* #### shouldn't this interface be called load-unicode-mapping-table
|
|
1277 for consistency with Unicode Consortium terminology? */
|
771
|
1278 DEFUN ("parse-unicode-translation-table", Fparse_unicode_translation_table,
|
|
1279 2, 6, 0, /*
|
877
|
1280 Load Unicode tables with the Unicode mapping data in FILENAME for CHARSET.
|
771
|
1281 Data is text, in the form of one translation per line -- charset
|
|
1282 codepoint followed by Unicode codepoint. Numbers are decimal or hex
|
|
1283 \(preceded by 0x). Comments are marked with a #. Charset codepoints
|
877
|
1284 for two-dimensional charsets have the first octet stored in the
|
771
|
1285 high 8 bits of the hex number and the second in the low 8 bits.
|
|
1286
|
|
1287 If START and END are given, only charset codepoints within the given
|
877
|
1288 range will be processed. (START and END apply to the codepoints in the
|
|
1289 file, before OFFSET is applied.)
|
771
|
1290
|
877
|
1291 If OFFSET is given, that value will be added to all charset codepoints
|
|
1292 in the file to obtain the internal charset codepoint. \(We assume
|
|
1293 that octets in the table are in the range 33 to 126 or 32 to 127. If
|
|
1294 you have a table in ku-ten form, with octets in the range 1 to 94, you
|
|
1295 will have to use an offset of 5140, i.e. 0x2020.)
|
771
|
1296
|
|
1297 FLAGS, if specified, control further how the tables are interpreted
|
877
|
1298 and are used to special-case certain known format deviations in the
|
|
1299 Unicode tables or in the charset:
|
771
|
1300
|
|
1301 `ignore-first-column'
|
877
|
1302 The JIS X 0208 tables have 3 columns of data instead of 2. The first
|
|
1303 column contains the Shift-JIS codepoint, which we ignore.
|
771
|
1304 `big5'
|
877
|
1305 The charset codepoints are Big Five codepoints; convert it to the
|
|
1306 hacked-up Mule codepoint in `chinese-big5-1' or `chinese-big5-2'.
|
771
|
1307 */
|
|
1308 (filename, charset, start, end, offset, flags))
|
|
1309 {
|
|
1310 int st = 0, en = INT_MAX, of = 0;
|
|
1311 FILE *file;
|
|
1312 struct gcpro gcpro1;
|
|
1313 char line[1025];
|
|
1314 int fondo = specpdl_depth ();
|
|
1315 int ignore_first_column = 0;
|
|
1316 int big5 = 0;
|
|
1317
|
|
1318 CHECK_STRING (filename);
|
|
1319 charset = Fget_charset (charset);
|
|
1320 if (!NILP (start))
|
|
1321 {
|
|
1322 CHECK_INT (start);
|
|
1323 st = XINT (start);
|
|
1324 }
|
|
1325 if (!NILP (end))
|
|
1326 {
|
|
1327 CHECK_INT (end);
|
|
1328 en = XINT (end);
|
|
1329 }
|
|
1330 if (!NILP (offset))
|
|
1331 {
|
|
1332 CHECK_INT (offset);
|
|
1333 of = XINT (offset);
|
|
1334 }
|
|
1335
|
|
1336 if (!LISTP (flags))
|
|
1337 flags = list1 (flags);
|
|
1338
|
|
1339 {
|
|
1340 EXTERNAL_LIST_LOOP_2 (elt, flags)
|
|
1341 {
|
|
1342 if (EQ (elt, Qignore_first_column))
|
|
1343 ignore_first_column = 1;
|
|
1344 else if (EQ (elt, Qbig5))
|
|
1345 big5 = 1;
|
|
1346 else
|
|
1347 invalid_constant
|
877
|
1348 ("Unrecognized `parse-unicode-translation-table' flag", elt);
|
771
|
1349 }
|
|
1350 }
|
|
1351
|
|
1352 GCPRO1 (filename);
|
|
1353 filename = Fexpand_file_name (filename, Qnil);
|
|
1354 file = qxe_fopen (XSTRING_DATA (filename), READ_TEXT);
|
|
1355 if (!file)
|
|
1356 report_file_error ("Cannot open", filename);
|
|
1357 record_unwind_protect (cerrar_el_fulano, make_opaque_ptr (file));
|
|
1358 while (fgets (line, sizeof (line), file))
|
|
1359 {
|
|
1360 char *p = line;
|
|
1361 int cp1, cp2, endcount;
|
|
1362 int cp1high, cp1low;
|
|
1363 int dummy;
|
|
1364
|
|
1365 while (*p) /* erase all comments out of the line */
|
|
1366 {
|
|
1367 if (*p == '#')
|
|
1368 *p = '\0';
|
|
1369 else
|
|
1370 p++;
|
|
1371 }
|
|
1372 /* see if line is nothing but whitespace and skip if so */
|
|
1373 p = line + strspn (line, " \t\n\r\f");
|
|
1374 if (!*p)
|
|
1375 continue;
|
|
1376 /* NOTE: It appears that MS Windows and Newlib sscanf() have
|
|
1377 different interpretations for whitespace (== "skip all whitespace
|
|
1378 at processing point"): Newlib requires at least one corresponding
|
|
1379 whitespace character in the input, but MS allows none. The
|
|
1380 following would be easier to write if we could count on the MS
|
|
1381 interpretation.
|
|
1382
|
|
1383 Also, the return value does NOT include %n storage. */
|
|
1384 if ((!ignore_first_column ?
|
|
1385 sscanf (p, "%i %i%n", &cp1, &cp2, &endcount) < 2 :
|
|
1386 sscanf (p, "%i %i %i%n", &dummy, &cp1, &cp2, &endcount) < 3)
|
|
1387 || *(p + endcount + strspn (p + endcount, " \t\n\r\f")))
|
|
1388 {
|
793
|
1389 warn_when_safe (Qunicode, Qwarning,
|
771
|
1390 "Unrecognized line in translation file %s:\n%s",
|
|
1391 XSTRING_DATA (filename), line);
|
|
1392 continue;
|
|
1393 }
|
|
1394 if (cp1 >= st && cp1 <= en)
|
|
1395 {
|
|
1396 cp1 += of;
|
|
1397 if (cp1 < 0 || cp1 >= 65536)
|
|
1398 {
|
|
1399 out_of_range:
|
793
|
1400 warn_when_safe (Qunicode, Qwarning,
|
|
1401 "Out of range first codepoint 0x%x in "
|
|
1402 "translation file %s:\n%s",
|
771
|
1403 cp1, XSTRING_DATA (filename), line);
|
|
1404 continue;
|
|
1405 }
|
|
1406
|
|
1407 cp1high = cp1 >> 8;
|
|
1408 cp1low = cp1 & 255;
|
|
1409
|
|
1410 if (big5)
|
|
1411 {
|
867
|
1412 Ichar ch = decode_big5_char (cp1high, cp1low);
|
771
|
1413 if (ch == -1)
|
793
|
1414
|
|
1415 warn_when_safe (Qunicode, Qwarning,
|
|
1416 "Out of range Big5 codepoint 0x%x in "
|
|
1417 "translation file %s:\n%s",
|
771
|
1418 cp1, XSTRING_DATA (filename), line);
|
|
1419 else
|
|
1420 set_unicode_conversion (ch, cp2);
|
|
1421 }
|
|
1422 else
|
|
1423 {
|
|
1424 int l1, h1, l2, h2;
|
867
|
1425 Ichar emch;
|
771
|
1426
|
|
1427 switch (XCHARSET_TYPE (charset))
|
|
1428 {
|
|
1429 case CHARSET_TYPE_94: l1 = 33; h1 = 126; l2 = 0; h2 = 0; break;
|
|
1430 case CHARSET_TYPE_96: l1 = 32; h1 = 127; l2 = 0; h2 = 0; break;
|
|
1431 case CHARSET_TYPE_94X94: l1 = 33; h1 = 126; l2 = 33; h2 = 126;
|
|
1432 break;
|
|
1433 case CHARSET_TYPE_96X96: l1 = 32; h1 = 127; l2 = 32; h2 = 127;
|
|
1434 break;
|
|
1435 default: abort (); l1 = 0; h1 = 0; l2 = 0; h2 = 0;
|
|
1436 }
|
|
1437
|
|
1438 if (cp1high < l2 || cp1high > h2 || cp1low < l1 || cp1low > h1)
|
|
1439 goto out_of_range;
|
|
1440
|
867
|
1441 emch = (cp1high == 0 ? make_ichar (charset, cp1low, 0) :
|
|
1442 make_ichar (charset, cp1high, cp1low));
|
771
|
1443 set_unicode_conversion (emch, cp2);
|
|
1444 }
|
|
1445 }
|
|
1446 }
|
|
1447
|
|
1448 if (ferror (file))
|
|
1449 report_file_error ("IO error when reading", filename);
|
|
1450
|
|
1451 unbind_to (fondo); /* close file */
|
|
1452 UNGCPRO;
|
|
1453 return Qnil;
|
|
1454 }
|
|
1455
|
|
1456 #endif /* MULE */
|
|
1457
|
|
1458
|
|
1459 /************************************************************************/
|
|
1460 /* Unicode coding system */
|
|
1461 /************************************************************************/
|
|
1462
|
|
1463 /* ISO 10646 UTF-16, UCS-4, UTF-8, UTF-7, etc. */
|
|
1464 DEFINE_CODING_SYSTEM_TYPE (unicode);
|
|
1465
|
|
1466 enum unicode_type
|
|
1467 {
|
|
1468 UNICODE_UTF_16,
|
|
1469 UNICODE_UTF_8,
|
|
1470 UNICODE_UTF_7,
|
|
1471 UNICODE_UCS_4,
|
|
1472 };
|
|
1473
|
|
1474 struct unicode_coding_system
|
|
1475 {
|
|
1476 enum unicode_type type;
|
|
1477 int little_endian :1;
|
|
1478 int need_bom :1;
|
|
1479 };
|
|
1480
|
|
1481 #define CODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1482 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->type)
|
|
1483 #define XCODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1484 CODING_SYSTEM_UNICODE_TYPE (XCODING_SYSTEM (codesys))
|
|
1485 #define CODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1486 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->little_endian)
|
|
1487 #define XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1488 CODING_SYSTEM_UNICODE_LITTLE_ENDIAN (XCODING_SYSTEM (codesys))
|
|
1489 #define CODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1490 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->need_bom)
|
|
1491 #define XCODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1492 CODING_SYSTEM_UNICODE_NEED_BOM (XCODING_SYSTEM (codesys))
|
|
1493
|
|
1494 struct unicode_coding_stream
|
|
1495 {
|
|
1496 /* decode */
|
|
1497 unsigned char counter;
|
|
1498 int seen_char;
|
|
1499 /* encode */
|
|
1500 Lisp_Object current_charset;
|
|
1501 int current_char_boundary;
|
|
1502 int wrote_bom;
|
|
1503 };
|
|
1504
|
|
1505 static const struct lrecord_description unicode_coding_system_description[] = {
|
|
1506 { XD_END }
|
|
1507 };
|
|
1508
|
|
1509 /* Decode a UCS-2 or UCS-4 character into a buffer. If the lookup fails, use
|
|
1510 <GETA MARK> (U+3013) of JIS X 0208, which means correct character
|
|
1511 is not found, instead.
|
|
1512 #### do something more appropriate (use blob?)
|
|
1513 Danger, Will Robinson! Data loss. Should we signal user? */
|
|
1514 static void
|
|
1515 decode_unicode_char (int ch, unsigned_char_dynarr *dst,
|
|
1516 struct unicode_coding_stream *data, int ignore_bom)
|
|
1517 {
|
|
1518 if (ch == 0xFEFF && !data->seen_char && ignore_bom)
|
|
1519 ;
|
|
1520 else
|
|
1521 {
|
|
1522 #ifdef MULE
|
877
|
1523 Ichar chr = unicode_to_ichar (ch, unicode_precedence_dynarr);
|
771
|
1524
|
|
1525 if (chr != -1)
|
|
1526 {
|
867
|
1527 Ibyte work[MAX_ICHAR_LEN];
|
771
|
1528 int len;
|
|
1529
|
867
|
1530 len = set_itext_ichar (work, chr);
|
771
|
1531 Dynarr_add_many (dst, work, len);
|
|
1532 }
|
|
1533 else
|
|
1534 {
|
|
1535 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1536 Dynarr_add (dst, 34 + 128);
|
|
1537 Dynarr_add (dst, 46 + 128);
|
|
1538 }
|
|
1539 #else
|
867
|
1540 Dynarr_add (dst, (Ibyte) ch);
|
771
|
1541 #endif /* MULE */
|
|
1542 }
|
|
1543
|
|
1544 data->seen_char = 1;
|
|
1545 }
|
|
1546
|
|
1547 static void
|
|
1548 encode_unicode_char_1 (int code, unsigned_char_dynarr *dst,
|
|
1549 enum unicode_type type, int little_endian)
|
|
1550 {
|
|
1551 switch (type)
|
|
1552 {
|
|
1553 case UNICODE_UTF_16:
|
|
1554 if (little_endian)
|
|
1555 {
|
|
1556 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1557 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1558 }
|
|
1559 else
|
|
1560 {
|
|
1561 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1562 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1563 }
|
|
1564 break;
|
|
1565
|
|
1566 case UNICODE_UCS_4:
|
|
1567 if (little_endian)
|
|
1568 {
|
|
1569 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1570 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1571 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1572 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1573 }
|
|
1574 else
|
|
1575 {
|
|
1576 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1577 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1578 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1579 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1580 }
|
|
1581 break;
|
|
1582
|
|
1583 case UNICODE_UTF_8:
|
|
1584 if (code <= 0x7f)
|
|
1585 {
|
|
1586 Dynarr_add (dst, (unsigned char) code);
|
|
1587 }
|
|
1588 else if (code <= 0x7ff)
|
|
1589 {
|
|
1590 Dynarr_add (dst, (unsigned char) ((code >> 6) | 0xc0));
|
|
1591 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1592 }
|
|
1593 else if (code <= 0xffff)
|
|
1594 {
|
|
1595 Dynarr_add (dst, (unsigned char) ((code >> 12) | 0xe0));
|
|
1596 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1597 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1598 }
|
|
1599 else if (code <= 0x1fffff)
|
|
1600 {
|
|
1601 Dynarr_add (dst, (unsigned char) ((code >> 18) | 0xf0));
|
|
1602 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1603 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1604 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1605 }
|
|
1606 else if (code <= 0x3ffffff)
|
|
1607 {
|
|
1608 Dynarr_add (dst, (unsigned char) ((code >> 24) | 0xf8));
|
|
1609 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1610 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1611 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1612 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1613 }
|
|
1614 else
|
|
1615 {
|
|
1616 Dynarr_add (dst, (unsigned char) ((code >> 30) | 0xfc));
|
|
1617 Dynarr_add (dst, (unsigned char) (((code >> 24) & 0x3f) | 0x80));
|
|
1618 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1619 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1620 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1621 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1622 }
|
|
1623 break;
|
|
1624
|
|
1625 case UNICODE_UTF_7: abort ();
|
|
1626
|
|
1627 default: abort ();
|
|
1628 }
|
|
1629 }
|
|
1630
|
|
1631 static void
|
|
1632 encode_unicode_char (Lisp_Object charset, int h, int l,
|
|
1633 unsigned_char_dynarr *dst, enum unicode_type type,
|
|
1634 int little_endian)
|
|
1635 {
|
|
1636 #ifdef MULE
|
867
|
1637 int code = ichar_to_unicode (make_ichar (charset, h & 127, l & 127));
|
771
|
1638
|
|
1639 if (code == -1)
|
|
1640 {
|
|
1641 if (type != UNICODE_UTF_16 &&
|
|
1642 XCHARSET_DIMENSION (charset) == 2 &&
|
|
1643 XCHARSET_CHARS (charset) == 94)
|
|
1644 {
|
|
1645 unsigned char final = XCHARSET_FINAL (charset);
|
|
1646
|
|
1647 if (('@' <= final) && (final < 0x7f))
|
|
1648 code = (0xe00000 + (final - '@') * 94 * 94
|
|
1649 + ((h & 127) - 33) * 94 + (l & 127) - 33);
|
|
1650 else
|
|
1651 code = '?';
|
|
1652 }
|
|
1653 else
|
|
1654 code = '?';
|
|
1655 }
|
|
1656 #else
|
|
1657 int code = h;
|
|
1658 #endif /* MULE */
|
|
1659
|
|
1660 encode_unicode_char_1 (code, dst, type, little_endian);
|
|
1661 }
|
|
1662
|
|
1663 static Bytecount
|
|
1664 unicode_convert (struct coding_stream *str, const UExtbyte *src,
|
|
1665 unsigned_char_dynarr *dst, Bytecount n)
|
|
1666 {
|
|
1667 unsigned int ch = str->ch;
|
|
1668 struct unicode_coding_stream *data = CODING_STREAM_TYPE_DATA (str, unicode);
|
|
1669 enum unicode_type type =
|
|
1670 XCODING_SYSTEM_UNICODE_TYPE (str->codesys);
|
|
1671 int little_endian = XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (str->codesys);
|
|
1672 int ignore_bom = XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys);
|
|
1673 Bytecount orign = n;
|
|
1674
|
|
1675 if (str->direction == CODING_DECODE)
|
|
1676 {
|
|
1677 unsigned char counter = data->counter;
|
|
1678
|
|
1679 while (n--)
|
|
1680 {
|
|
1681 UExtbyte c = *src++;
|
|
1682
|
|
1683 switch (type)
|
|
1684 {
|
|
1685 case UNICODE_UTF_8:
|
|
1686 switch (counter)
|
|
1687 {
|
|
1688 case 0:
|
|
1689 if (c >= 0xfc)
|
|
1690 {
|
|
1691 ch = c & 0x01;
|
|
1692 counter = 5;
|
|
1693 }
|
|
1694 else if (c >= 0xf8)
|
|
1695 {
|
|
1696 ch = c & 0x03;
|
|
1697 counter = 4;
|
|
1698 }
|
|
1699 else if (c >= 0xf0)
|
|
1700 {
|
|
1701 ch = c & 0x07;
|
|
1702 counter = 3;
|
|
1703 }
|
|
1704 else if (c >= 0xe0)
|
|
1705 {
|
|
1706 ch = c & 0x0f;
|
|
1707 counter = 2;
|
|
1708 }
|
|
1709 else if (c >= 0xc0)
|
|
1710 {
|
|
1711 ch = c & 0x1f;
|
|
1712 counter = 1;
|
|
1713 }
|
|
1714 else
|
|
1715 decode_unicode_char (c, dst, data, ignore_bom);
|
|
1716 break;
|
|
1717 case 1:
|
|
1718 ch = (ch << 6) | (c & 0x3f);
|
|
1719 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
1720 ch = 0;
|
|
1721 counter = 0;
|
|
1722 break;
|
|
1723 default:
|
|
1724 ch = (ch << 6) | (c & 0x3f);
|
|
1725 counter--;
|
|
1726 }
|
|
1727 break;
|
|
1728
|
|
1729 case UNICODE_UTF_16:
|
|
1730 if (little_endian)
|
|
1731 ch = (c << counter) | ch;
|
|
1732 else
|
|
1733 ch = (ch << 8) | c;
|
|
1734 counter += 8;
|
|
1735 if (counter == 16)
|
|
1736 {
|
|
1737 int tempch = ch;
|
|
1738 ch = 0;
|
|
1739 counter = 0;
|
|
1740 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1741 }
|
|
1742 break;
|
|
1743
|
|
1744 case UNICODE_UCS_4:
|
|
1745 if (little_endian)
|
|
1746 ch = (c << counter) | ch;
|
|
1747 else
|
|
1748 ch = (ch << 8) | c;
|
|
1749 counter += 8;
|
|
1750 if (counter == 32)
|
|
1751 {
|
|
1752 int tempch = ch;
|
|
1753 ch = 0;
|
|
1754 counter = 0;
|
|
1755 if (tempch < 0)
|
|
1756 {
|
|
1757 /* !!#### indicate an error */
|
|
1758 tempch = '~';
|
|
1759 }
|
|
1760 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1761 }
|
|
1762 break;
|
|
1763
|
|
1764 case UNICODE_UTF_7:
|
|
1765 abort ();
|
|
1766 break;
|
|
1767
|
|
1768 default: abort ();
|
|
1769 }
|
|
1770
|
|
1771 }
|
|
1772 if (str->eof)
|
|
1773 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1774
|
|
1775 data->counter = counter;
|
|
1776 }
|
|
1777 else
|
|
1778 {
|
|
1779 unsigned char char_boundary = data->current_char_boundary;
|
|
1780 Lisp_Object charset = data->current_charset;
|
|
1781
|
|
1782 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1783 /* flags for handling composite chars. We do a little switcheroo
|
|
1784 on the source while we're outputting the composite char. */
|
|
1785 Bytecount saved_n = 0;
|
867
|
1786 const Ibyte *saved_src = NULL;
|
771
|
1787 int in_composite = 0;
|
|
1788
|
|
1789 back_to_square_n:
|
|
1790 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1791
|
|
1792 if (XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys) && !data->wrote_bom)
|
|
1793 {
|
|
1794 encode_unicode_char_1 (0xFEFF, dst, type, little_endian);
|
|
1795 data->wrote_bom = 1;
|
|
1796 }
|
|
1797
|
|
1798 while (n--)
|
|
1799 {
|
867
|
1800 Ibyte c = *src++;
|
771
|
1801
|
|
1802 #ifdef MULE
|
826
|
1803 if (byte_ascii_p (c))
|
771
|
1804 #endif /* MULE */
|
|
1805 { /* Processing ASCII character */
|
|
1806 ch = 0;
|
|
1807 encode_unicode_char (Vcharset_ascii, c, 0, dst, type,
|
|
1808 little_endian);
|
|
1809
|
|
1810 char_boundary = 1;
|
|
1811 }
|
|
1812 #ifdef MULE
|
867
|
1813 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
1814 { /* Processing Leading Byte */
|
|
1815 ch = 0;
|
826
|
1816 charset = charset_by_leading_byte (c);
|
|
1817 if (leading_byte_prefix_p(c))
|
771
|
1818 ch = c;
|
|
1819 char_boundary = 0;
|
|
1820 }
|
|
1821 else
|
|
1822 { /* Processing Non-ASCII character */
|
|
1823 char_boundary = 1;
|
|
1824 if (EQ (charset, Vcharset_control_1))
|
|
1825 encode_unicode_char (Vcharset_control_1, c, 0, dst,
|
|
1826 type, little_endian);
|
|
1827 else
|
|
1828 {
|
|
1829 switch (XCHARSET_REP_BYTES (charset))
|
|
1830 {
|
|
1831 case 2:
|
|
1832 encode_unicode_char (charset, c, 0, dst, type,
|
|
1833 little_endian);
|
|
1834 break;
|
|
1835 case 3:
|
|
1836 if (XCHARSET_PRIVATE_P (charset))
|
|
1837 {
|
|
1838 encode_unicode_char (charset, c, 0, dst, type,
|
|
1839 little_endian);
|
|
1840 ch = 0;
|
|
1841 }
|
|
1842 else if (ch)
|
|
1843 {
|
|
1844 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1845 if (EQ (charset, Vcharset_composite))
|
|
1846 {
|
|
1847 if (in_composite)
|
|
1848 {
|
|
1849 /* #### Bother! We don't know how to
|
|
1850 handle this yet. */
|
|
1851 encode_unicode_char (Vcharset_ascii, '~', 0,
|
|
1852 dst, type,
|
|
1853 little_endian);
|
|
1854 }
|
|
1855 else
|
|
1856 {
|
867
|
1857 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
1858 ch & 0x7F,
|
|
1859 c & 0x7F);
|
|
1860 Lisp_Object lstr =
|
|
1861 composite_char_string (emch);
|
|
1862 saved_n = n;
|
|
1863 saved_src = src;
|
|
1864 in_composite = 1;
|
|
1865 src = XSTRING_DATA (lstr);
|
|
1866 n = XSTRING_LENGTH (lstr);
|
|
1867 }
|
|
1868 }
|
|
1869 else
|
|
1870 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1871 encode_unicode_char (charset, ch, c, dst, type,
|
|
1872 little_endian);
|
|
1873 ch = 0;
|
|
1874 }
|
|
1875 else
|
|
1876 {
|
|
1877 ch = c;
|
|
1878 char_boundary = 0;
|
|
1879 }
|
|
1880 break;
|
|
1881 case 4:
|
|
1882 if (ch)
|
|
1883 {
|
|
1884 encode_unicode_char (charset, ch, c, dst, type,
|
|
1885 little_endian);
|
|
1886 ch = 0;
|
|
1887 }
|
|
1888 else
|
|
1889 {
|
|
1890 ch = c;
|
|
1891 char_boundary = 0;
|
|
1892 }
|
|
1893 break;
|
|
1894 default:
|
|
1895 abort ();
|
|
1896 }
|
|
1897 }
|
|
1898 }
|
|
1899 #endif /* MULE */
|
|
1900 }
|
|
1901
|
|
1902 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1903 if (in_composite)
|
|
1904 {
|
|
1905 n = saved_n;
|
|
1906 src = saved_src;
|
|
1907 in_composite = 0;
|
|
1908 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
1909 }
|
|
1910 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1911
|
|
1912 data->current_char_boundary = char_boundary;
|
|
1913 data->current_charset = charset;
|
|
1914
|
|
1915 /* La palabra se hizo carne! */
|
|
1916 /* A palavra fez-se carne! */
|
|
1917 /* Whatever. */
|
|
1918 }
|
|
1919
|
|
1920 str->ch = ch;
|
|
1921 return orign;
|
|
1922 }
|
|
1923
|
|
1924 /* DEFINE_DETECTOR (utf_7); */
|
|
1925 DEFINE_DETECTOR (utf_8);
|
|
1926 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8);
|
|
1927 DEFINE_DETECTOR (ucs_4);
|
|
1928 DEFINE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
1929 DEFINE_DETECTOR (utf_16);
|
|
1930 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
1931 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
1932 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
1933 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
1934
|
|
1935 struct ucs_4_detector
|
|
1936 {
|
|
1937 int in_ucs_4_byte;
|
|
1938 };
|
|
1939
|
|
1940 static void
|
|
1941 ucs_4_detect (struct detection_state *st, const UExtbyte *src,
|
|
1942 Bytecount n)
|
|
1943 {
|
|
1944 struct ucs_4_detector *data = DETECTION_STATE_DATA (st, ucs_4);
|
|
1945
|
|
1946 while (n--)
|
|
1947 {
|
|
1948 UExtbyte c = *src++;
|
|
1949 switch (data->in_ucs_4_byte)
|
|
1950 {
|
|
1951 case 0:
|
|
1952 if (c >= 128)
|
|
1953 {
|
|
1954 DET_RESULT (st, ucs_4) = DET_NEARLY_IMPOSSIBLE;
|
|
1955 return;
|
|
1956 }
|
|
1957 else
|
|
1958 data->in_ucs_4_byte++;
|
|
1959 break;
|
|
1960 case 3:
|
|
1961 data->in_ucs_4_byte = 0;
|
|
1962 break;
|
|
1963 default:
|
|
1964 data->in_ucs_4_byte++;
|
|
1965 }
|
|
1966 }
|
|
1967
|
|
1968 /* !!#### write this for real */
|
|
1969 DET_RESULT (st, ucs_4) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
1970 }
|
|
1971
|
|
1972 struct utf_16_detector
|
|
1973 {
|
|
1974 unsigned int seen_ffff:1;
|
|
1975 unsigned int seen_forward_bom:1;
|
|
1976 unsigned int seen_rev_bom:1;
|
|
1977 int byteno;
|
|
1978 int prev_char;
|
|
1979 int text, rev_text;
|
|
1980 };
|
|
1981
|
|
1982 static void
|
|
1983 utf_16_detect (struct detection_state *st, const UExtbyte *src,
|
|
1984 Bytecount n)
|
|
1985 {
|
|
1986 struct utf_16_detector *data = DETECTION_STATE_DATA (st, utf_16);
|
|
1987
|
|
1988 while (n--)
|
|
1989 {
|
|
1990 UExtbyte c = *src++;
|
|
1991 int prevc = data->prev_char;
|
|
1992 if (data->byteno == 1 && c == 0xFF && prevc == 0xFE)
|
|
1993 data->seen_forward_bom = 1;
|
|
1994 else if (data->byteno == 1 && c == 0xFE && prevc == 0xFF)
|
|
1995 data->seen_rev_bom = 1;
|
|
1996
|
|
1997 if (data->byteno & 1)
|
|
1998 {
|
|
1999 if (c == 0xFF && prevc == 0xFF)
|
|
2000 data->seen_ffff = 1;
|
|
2001 if (prevc == 0
|
|
2002 && (c == '\r' || c == '\n'
|
|
2003 || (c >= 0x20 && c <= 0x7E)))
|
|
2004 data->text++;
|
|
2005 if (c == 0
|
|
2006 && (prevc == '\r' || prevc == '\n'
|
|
2007 || (prevc >= 0x20 && prevc <= 0x7E)))
|
|
2008 data->rev_text++;
|
|
2009 if (prevc == 0x20 && (c == 0x28 || c == 0x29))
|
|
2010 data->text++;
|
|
2011 if (c == 0x20 && (prevc == 0x28 || prevc == 0x29))
|
|
2012 data->rev_text++;
|
|
2013 }
|
|
2014
|
|
2015 data->byteno++;
|
|
2016 data->prev_char = c;
|
|
2017 }
|
|
2018
|
|
2019 {
|
|
2020 int variance_indicates_big_endian =
|
|
2021 (data->text >= 10
|
|
2022 && (data->rev_text == 0
|
|
2023 || data->text / data->rev_text >= 10));
|
|
2024 int variance_indicates_little_endian =
|
|
2025 (data->rev_text >= 10
|
|
2026 && (data->text == 0
|
|
2027 || data->rev_text / data->text >= 10));
|
|
2028
|
|
2029 if (data->seen_ffff)
|
|
2030 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2031 else if (data->seen_forward_bom)
|
|
2032 {
|
|
2033 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2034 if (variance_indicates_big_endian)
|
|
2035 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2036 else if (variance_indicates_little_endian)
|
|
2037 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2038 else
|
|
2039 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2040 }
|
|
2041 else if (data->seen_forward_bom)
|
|
2042 {
|
|
2043 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2044 if (variance_indicates_big_endian)
|
|
2045 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2046 else if (variance_indicates_little_endian)
|
|
2047 /* #### may need to rethink */
|
|
2048 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2049 else
|
|
2050 /* #### may need to rethink */
|
|
2051 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2052 }
|
|
2053 else if (data->seen_rev_bom)
|
|
2054 {
|
|
2055 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2056 if (variance_indicates_little_endian)
|
|
2057 DET_RESULT (st, utf_16_little_endian_bom) = DET_NEAR_CERTAINTY;
|
|
2058 else if (variance_indicates_big_endian)
|
|
2059 /* #### may need to rethink */
|
|
2060 DET_RESULT (st, utf_16_little_endian_bom) = DET_SOMEWHAT_LIKELY;
|
|
2061 else
|
|
2062 /* #### may need to rethink */
|
|
2063 DET_RESULT (st, utf_16_little_endian_bom) = DET_QUITE_PROBABLE;
|
|
2064 }
|
|
2065 else if (variance_indicates_big_endian)
|
|
2066 {
|
|
2067 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2068 DET_RESULT (st, utf_16) = DET_SOMEWHAT_LIKELY;
|
|
2069 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_UNLIKELY;
|
|
2070 }
|
|
2071 else if (variance_indicates_little_endian)
|
|
2072 {
|
|
2073 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2074 DET_RESULT (st, utf_16) = DET_SOMEWHAT_UNLIKELY;
|
|
2075 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_LIKELY;
|
|
2076 }
|
|
2077 else
|
|
2078 SET_DET_RESULTS (st, utf_16, DET_AS_LIKELY_AS_UNLIKELY);
|
|
2079 }
|
|
2080 }
|
|
2081
|
|
2082 struct utf_8_detector
|
|
2083 {
|
|
2084 int in_utf_8_byte;
|
|
2085 };
|
|
2086
|
|
2087 static void
|
|
2088 utf_8_detect (struct detection_state *st, const UExtbyte *src,
|
|
2089 Bytecount n)
|
|
2090 {
|
|
2091 struct utf_8_detector *data = DETECTION_STATE_DATA (st, utf_8);
|
|
2092
|
|
2093 while (n--)
|
|
2094 {
|
|
2095 UExtbyte c = *src++;
|
|
2096 switch (data->in_utf_8_byte)
|
|
2097 {
|
|
2098 case 0:
|
|
2099 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2100 {
|
|
2101 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2102 return;
|
|
2103 }
|
|
2104 else if (c >= 0xfc)
|
|
2105 data->in_utf_8_byte = 5;
|
|
2106 else if (c >= 0xf8)
|
|
2107 data->in_utf_8_byte = 4;
|
|
2108 else if (c >= 0xf0)
|
|
2109 data->in_utf_8_byte = 3;
|
|
2110 else if (c >= 0xe0)
|
|
2111 data->in_utf_8_byte = 2;
|
|
2112 else if (c >= 0xc0)
|
|
2113 data->in_utf_8_byte = 1;
|
|
2114 else if (c >= 0x80)
|
|
2115 {
|
|
2116 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2117 return;
|
|
2118 }
|
|
2119 break;
|
|
2120 default:
|
|
2121 if ((c & 0xc0) != 0x80)
|
|
2122 {
|
|
2123 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2124 return;
|
|
2125 }
|
|
2126 else
|
|
2127 data->in_utf_8_byte--;
|
|
2128 }
|
|
2129 }
|
|
2130 DET_RESULT (st, utf_8) = DET_SOMEWHAT_LIKELY;
|
|
2131 }
|
|
2132
|
|
2133 static void
|
|
2134 unicode_init_coding_stream (struct coding_stream *str)
|
|
2135 {
|
|
2136 struct unicode_coding_stream *data =
|
|
2137 CODING_STREAM_TYPE_DATA (str, unicode);
|
|
2138 xzero (*data);
|
|
2139 data->current_charset = Qnil;
|
|
2140 }
|
|
2141
|
|
2142 static void
|
|
2143 unicode_rewind_coding_stream (struct coding_stream *str)
|
|
2144 {
|
|
2145 unicode_init_coding_stream (str);
|
|
2146 }
|
|
2147
|
|
2148 static int
|
|
2149 unicode_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2150 {
|
|
2151 if (EQ (key, Qtype))
|
|
2152 {
|
|
2153 enum unicode_type type;
|
|
2154
|
|
2155 if (EQ (value, Qutf_8))
|
|
2156 type = UNICODE_UTF_8;
|
|
2157 else if (EQ (value, Qutf_16))
|
|
2158 type = UNICODE_UTF_16;
|
|
2159 else if (EQ (value, Qutf_7))
|
|
2160 type = UNICODE_UTF_7;
|
|
2161 else if (EQ (value, Qucs_4))
|
|
2162 type = UNICODE_UCS_4;
|
|
2163 else
|
|
2164 invalid_constant ("Invalid Unicode type", key);
|
|
2165
|
|
2166 XCODING_SYSTEM_UNICODE_TYPE (codesys) = type;
|
|
2167 }
|
|
2168 else if (EQ (key, Qlittle_endian))
|
|
2169 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys) = !NILP (value);
|
|
2170 else if (EQ (key, Qneed_bom))
|
|
2171 XCODING_SYSTEM_UNICODE_NEED_BOM (codesys) = !NILP (value);
|
|
2172 else
|
|
2173 return 0;
|
|
2174 return 1;
|
|
2175 }
|
|
2176
|
|
2177 static Lisp_Object
|
|
2178 unicode_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2179 {
|
|
2180 if (EQ (prop, Qtype))
|
|
2181 {
|
|
2182 switch (XCODING_SYSTEM_UNICODE_TYPE (coding_system))
|
|
2183 {
|
|
2184 case UNICODE_UTF_16: return Qutf_16;
|
|
2185 case UNICODE_UTF_8: return Qutf_8;
|
|
2186 case UNICODE_UTF_7: return Qutf_7;
|
|
2187 case UNICODE_UCS_4: return Qucs_4;
|
|
2188 default: abort ();
|
|
2189 }
|
|
2190 }
|
|
2191 else if (EQ (prop, Qlittle_endian))
|
|
2192 return XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (coding_system) ? Qt : Qnil;
|
|
2193 else if (EQ (prop, Qneed_bom))
|
|
2194 return XCODING_SYSTEM_UNICODE_NEED_BOM (coding_system) ? Qt : Qnil;
|
|
2195 return Qunbound;
|
|
2196 }
|
|
2197
|
|
2198 static void
|
|
2199 unicode_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag)
|
|
2200 {
|
800
|
2201 write_fmt_string_lisp (printcharfun, "(%s", 1, unicode_getprop (cs, Qtype));
|
771
|
2202 if (XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (cs))
|
826
|
2203 write_c_string (printcharfun, ", little-endian");
|
771
|
2204 if (XCODING_SYSTEM_UNICODE_NEED_BOM (cs))
|
826
|
2205 write_c_string (printcharfun, ", need-bom");
|
|
2206 write_c_string (printcharfun, ")");
|
771
|
2207 }
|
|
2208
|
|
2209 int
|
|
2210 dfc_coding_system_is_unicode (Lisp_Object codesys)
|
|
2211 {
|
|
2212 #ifdef HAVE_WIN32_CODING_SYSTEMS
|
|
2213 codesys = Fget_coding_system (codesys);
|
|
2214 return (EQ (XCODING_SYSTEM_TYPE (codesys), Qunicode) &&
|
|
2215 XCODING_SYSTEM_UNICODE_TYPE (codesys) == UNICODE_UTF_16 &&
|
|
2216 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys));
|
|
2217
|
|
2218 #else
|
|
2219 return 0;
|
|
2220 #endif
|
|
2221 }
|
|
2222
|
|
2223
|
|
2224 /************************************************************************/
|
|
2225 /* Initialization */
|
|
2226 /************************************************************************/
|
|
2227
|
|
2228 void
|
|
2229 syms_of_unicode (void)
|
|
2230 {
|
|
2231 #ifdef MULE
|
877
|
2232 DEFSUBR (Funicode_precedence_list);
|
771
|
2233 DEFSUBR (Fset_language_unicode_precedence_list);
|
|
2234 DEFSUBR (Flanguage_unicode_precedence_list);
|
|
2235 DEFSUBR (Fset_default_unicode_precedence_list);
|
|
2236 DEFSUBR (Fdefault_unicode_precedence_list);
|
|
2237 DEFSUBR (Fset_unicode_conversion);
|
|
2238
|
|
2239 DEFSUBR (Fparse_unicode_translation_table);
|
|
2240
|
|
2241 DEFSYMBOL (Qignore_first_column);
|
|
2242 #endif /* MULE */
|
|
2243
|
800
|
2244 DEFSUBR (Fchar_to_unicode);
|
|
2245 DEFSUBR (Funicode_to_char);
|
771
|
2246
|
|
2247 DEFSYMBOL (Qunicode);
|
|
2248 DEFSYMBOL (Qucs_4);
|
|
2249 DEFSYMBOL (Qutf_16);
|
|
2250 DEFSYMBOL (Qutf_8);
|
|
2251 DEFSYMBOL (Qutf_7);
|
|
2252
|
|
2253 DEFSYMBOL (Qneed_bom);
|
|
2254
|
|
2255 DEFSYMBOL (Qutf_16);
|
|
2256 DEFSYMBOL (Qutf_16_little_endian);
|
|
2257 DEFSYMBOL (Qutf_16_bom);
|
|
2258 DEFSYMBOL (Qutf_16_little_endian_bom);
|
|
2259 }
|
|
2260
|
|
2261 void
|
|
2262 coding_system_type_create_unicode (void)
|
|
2263 {
|
|
2264 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (unicode, "unicode-coding-system-p");
|
|
2265 CODING_SYSTEM_HAS_METHOD (unicode, print);
|
|
2266 CODING_SYSTEM_HAS_METHOD (unicode, convert);
|
|
2267 CODING_SYSTEM_HAS_METHOD (unicode, init_coding_stream);
|
|
2268 CODING_SYSTEM_HAS_METHOD (unicode, rewind_coding_stream);
|
|
2269 CODING_SYSTEM_HAS_METHOD (unicode, putprop);
|
|
2270 CODING_SYSTEM_HAS_METHOD (unicode, getprop);
|
|
2271
|
|
2272 INITIALIZE_DETECTOR (utf_8);
|
|
2273 DETECTOR_HAS_METHOD (utf_8, detect);
|
|
2274 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8);
|
|
2275
|
|
2276 INITIALIZE_DETECTOR (ucs_4);
|
|
2277 DETECTOR_HAS_METHOD (ucs_4, detect);
|
|
2278 INITIALIZE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2279
|
|
2280 INITIALIZE_DETECTOR (utf_16);
|
|
2281 DETECTOR_HAS_METHOD (utf_16, detect);
|
|
2282 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2283 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2284 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2285 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2286 }
|
|
2287
|
|
2288 void
|
|
2289 reinit_coding_system_type_create_unicode (void)
|
|
2290 {
|
|
2291 REINITIALIZE_CODING_SYSTEM_TYPE (unicode);
|
|
2292 }
|
|
2293
|
|
2294 void
|
|
2295 reinit_vars_of_unicode (void)
|
|
2296 {
|
|
2297 #ifdef MULE
|
|
2298 init_blank_unicode_tables ();
|
|
2299 #endif /* MULE */
|
|
2300 }
|
|
2301
|
|
2302 void
|
|
2303 vars_of_unicode (void)
|
|
2304 {
|
|
2305 reinit_vars_of_unicode ();
|
|
2306
|
|
2307 Fprovide (intern ("unicode"));
|
|
2308
|
|
2309 #ifdef MULE
|
|
2310 staticpro (&Vlanguage_unicode_precedence_list);
|
|
2311 Vlanguage_unicode_precedence_list = Qnil;
|
|
2312
|
|
2313 staticpro (&Vdefault_unicode_precedence_list);
|
|
2314 Vdefault_unicode_precedence_list = Qnil;
|
|
2315
|
|
2316 unicode_precedence_dynarr = Dynarr_new (Lisp_Object);
|
|
2317 dump_add_root_struct_ptr (&unicode_precedence_dynarr,
|
|
2318 &lisp_object_dynarr_description);
|
|
2319 #if 0
|
|
2320 dump_add_root_thing (&to_unicode_blank_1, to_unicode_level_1_desc);
|
|
2321 dump_add_root_thing (&to_unicode_blank_2, to_unicode_level_2_desc);
|
|
2322
|
|
2323 dump_add_root_thing (&from_unicode_blank_1, from_unicode_level_1_desc);
|
|
2324 dump_add_root_thing (&from_unicode_blank_2, from_unicode_level_2_desc);
|
|
2325 dump_add_root_thing (&from_unicode_blank_3, from_unicode_level_3_desc);
|
|
2326 dump_add_root_thing (&from_unicode_blank_4, from_unicode_level_4_desc);
|
|
2327 #endif
|
|
2328
|
|
2329 #endif /* MULE */
|
|
2330 }
|