]> code.delx.au - gnu-emacs/blob - src/charset.h
Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-11
[gnu-emacs] / src / charset.h
1 /* Header for charset handler.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1997, 1998, 2003
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H14PRO021
6 Copyright (C) 2003
7 National Institute of Advanced Industrial Science and Technology (AIST)
8 Registration Number H13PRO009
9
10 This file is part of GNU Emacs.
11
12 GNU Emacs is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
15 any later version.
16
17 GNU Emacs is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GNU Emacs; see the file COPYING. If not, write to
24 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA. */
26
27 #ifndef EMACS_CHARSET_H
28 #define EMACS_CHARSET_H
29
30 /* Index to arguments of Fdefine_charset_internal. */
31
32 enum define_charset_arg_index
33 {
34 charset_arg_name,
35 charset_arg_dimension,
36 charset_arg_code_space,
37 charset_arg_min_code,
38 charset_arg_max_code,
39 charset_arg_iso_final,
40 charset_arg_iso_revision,
41 charset_arg_emacs_mule_id,
42 charset_arg_ascii_compatible_p,
43 charset_arg_supplementary_p,
44 charset_arg_invalid_code,
45 charset_arg_code_offset,
46 charset_arg_map,
47 charset_arg_subset,
48 charset_arg_superset,
49 charset_arg_unify_map,
50 charset_arg_plist,
51 charset_arg_max
52 };
53
54
55 /* Indices to charset attributes vector. */
56
57 enum charset_attr_index
58 {
59 /* ID number of the charset. */
60 charset_id,
61
62 /* Name of the charset (symbol). */
63 charset_name,
64
65 /* Property list of the charset. */
66 charset_plist,
67
68 /* If the method of the charset is `MAP_DEFERRED', the value is a
69 mapping vector or a file name that contains mapping vector.
70 Otherwise, nil. */
71 charset_map,
72
73 /* If the method of the charset is `MAP', the value is a vector
74 that maps code points of the charset to characters. The vector
75 is indexed by a character index. A character index is
76 calculated from a code point and the code-space table of the
77 charset. */
78 charset_decoder,
79
80 /* If the method of the charset is `MAP', the value is a
81 char-table that maps characters of the charset to code
82 points. */
83 charset_encoder,
84
85 /* If the method of the charset is `SUBSET', the value is a vector
86 that has this form:
87
88 [ CHARSET-ID MIN-CODE MAX-CODE OFFSET ]
89
90 CHARSET-ID is an ID number of a parent charset. MIN-CODE and
91 MAX-CODE specify the range of characters inherited from the
92 parent. OFFSET is an integer value to add to a code point of
93 the parent charset to get the corresponding code point of this
94 charset. */
95 charset_subset,
96
97 /* If the method of the charset is `SUPERSET', the value is a list
98 whose elements have this form:
99
100 (CHARSET-ID . OFFSET)
101
102 CHARSET-IDs are ID numbers of parent charsets. OFFSET is an
103 integer value to add to a code point of the parent charset to
104 get the corresponding code point of this charset. */
105 charset_superset,
106
107 /* The value is a mapping vector or a file name that contains the
108 mapping. This defines how characters in the charset should be
109 unified with Unicode. The value of the member
110 `charset_deunifier' is created from this information. */
111 charset_unify_map,
112
113 /* If characters in the charset must be unified Unicode, the value
114 is a char table that maps a unified Unicode character code to
115 the non-unified character code in the charset. */
116 charset_deunifier,
117
118 /* The length of the charset attribute vector. */
119 charset_attr_max
120 };
121
122 /* Methods for converting code points and characters of charsets. */
123
124 enum charset_method
125 {
126 /* For a charset of this method, a character code is calculated
127 from a character index (which is calculated from a code point)
128 simply by adding an offset value. */
129 CHARSET_METHOD_OFFSET,
130
131 /* For a charset of this method, a decoder vector and an encoder
132 char-table is used for code point <-> character code
133 conversion. */
134 CHARSET_METHOD_MAP,
135
136 /* Same as above but decoder and encoder are loaded from a file on
137 demand. Once loaded, the method is changed to
138 CHARSET_METHOD_MAP. */
139 CHARSET_METHOD_MAP_DEFERRED,
140
141 /* A charset of this method is a subset of another charset. */
142 CHARSET_METHOD_SUBSET,
143
144 /* A charset of this method is a superset of other charsets. */
145 CHARSET_METHOD_SUPERSET
146 };
147
148 struct charset
149 {
150 /* Index to charset_table. */
151 int id;
152
153 /* Index to Vcharset_hash_table. */
154 int hash_index;
155
156 /* Dimension of the charset: 1, 2, 3, or 4. */
157 int dimension;
158
159 /* Byte code range of each dimension. <code_space>[4N] is a mininum
160 byte code of the (N+1)th dimension, <code_space>[4N+1] is a
161 maximum byte code of the (N+1)th dimension, <code_space>[4N+2] is
162 (<code_space>[4N+1] - <code_space>[4N] + 1), <code_space>[4N+3]
163 is a number of characters containd in the first to (N+1)th
164 dismesions. We get `char-index' of a `code-point' from this
165 information. */
166 int code_space[16];
167
168 /* If B is a byte of Nth dimension of a code-point, the (N-1)th bit
169 of code_space_mask[B] is set. This array is used to quickly
170 check if a code-point is in a valid range. */
171 unsigned char *code_space_mask;
172
173 /* 1 if there's no gap in code-points. */
174 int code_linear_p;
175
176 /* If the charset is treated as 94-chars in ISO-2022, the value is 0.
177 If the charset is treated as 96-chars in ISO-2022, the value is 1. */
178 int iso_chars_96;
179
180 /* ISO final byte of the charset: 48..127. It may be -1 if the
181 charset doesn't conform to ISO-2022. */
182 int iso_final;
183
184 /* ISO revision number of the charset. */
185 int iso_revision;
186
187 /* If the charset is identical to what supported by Emacs 21 and the
188 priors, the identification number of the charset used in those
189 version. Otherwise, -1. */
190 int emacs_mule_id;
191
192 /* Nonzero iff the charset is compatible with ASCII. */
193 int ascii_compatible_p;
194
195 /* Nonzero iff the charset is supplementary. */
196 int supplementary_p;
197
198 /* Nonzero iff all the code points are representable by Lisp_Int. */
199 int compact_codes_p;
200
201 /* The method for encoding/decoding characters of the charset. */
202 enum charset_method method;
203
204 /* Mininum and Maximum code points of the charset. */
205 unsigned min_code, max_code;
206
207 /* Offset value used by macros CODE_POINT_TO_INDEX and
208 INDEX_TO_CODE_POINT. . */
209 unsigned char_index_offset;
210
211 /* Mininum and Maximum character codes of the charset. If the
212 charset is compatible with ASCII, min_char is a minimum non-ASCII
213 character of the charset. If the method of charset is
214 CHARSET_METHOD_OFFSET, even if the charset is unified, min_char
215 and max_char doesn't change. */
216 int min_char, max_char;
217
218 /* The code returned by ENCODE_CHAR if a character is not encodable
219 by the charset. */
220 unsigned invalid_code;
221
222 /* If the method of the charset is CHARSET_METHOD_MAP, this is a
223 table of bits used to quickly and roughly guess if a character
224 belongs to the charset.
225
226 The first 64 elements are 512 bits for characters less than
227 0x10000. Each bit corresponds to 128-character block. The last
228 126 elements are 1008 bits for the greater characters
229 (0x10000..0x3FFFFF). Each bit corresponds to 4096-character
230 block.
231
232 If a bit is 1, at least one character in the corresponding block is
233 in this charset. */
234 unsigned char fast_map[190];
235
236 /* Offset value to calculate a character code from code-point, and
237 visa versa. */
238 int code_offset;
239
240 int unified_p;
241 };
242
243 /* Hash table of charset symbols vs. the correponding attribute
244 vectors. */
245 extern Lisp_Object Vcharset_hash_table;
246
247 /* Table of struct charset. */
248 extern struct charset *charset_table;
249
250 #define CHARSET_FROM_ID(id) (charset_table + (id))
251
252 extern Lisp_Object Vcharset_ordered_list;
253
254 /* Incremented everytime we change the priority of charsets. */
255 extern unsigned short charset_ordered_list_tick;
256
257 extern Lisp_Object Vcharset_list;
258 extern Lisp_Object Viso_2022_charset_list;
259 extern Lisp_Object Vemacs_mule_charset_list;
260
261 extern struct charset *emacs_mule_charset[256];
262
263
264 /* Macros to access information about charset. */
265
266 /* Return the attribute vector of charset whose symbol is SYMBOL. */
267 #define CHARSET_SYMBOL_ATTRIBUTES(symbol) \
268 Fgethash ((symbol), Vcharset_hash_table, Qnil)
269
270 #define CHARSET_ATTR_ID(attrs) AREF ((attrs), charset_id)
271 #define CHARSET_ATTR_NAME(attrs) AREF ((attrs), charset_name)
272 #define CHARSET_ATTR_PLIST(attrs) AREF ((attrs), charset_plist)
273 #define CHARSET_ATTR_MAP(attrs) AREF ((attrs), charset_map)
274 #define CHARSET_ATTR_DECODER(attrs) AREF ((attrs), charset_decoder)
275 #define CHARSET_ATTR_ENCODER(attrs) AREF ((attrs), charset_encoder)
276 #define CHARSET_ATTR_SUBSET(attrs) AREF ((attrs), charset_subset)
277 #define CHARSET_ATTR_SUPERSET(attrs) AREF ((attrs), charset_superset)
278 #define CHARSET_ATTR_UNIFY_MAP(attrs) AREF ((attrs), charset_unify_map)
279 #define CHARSET_ATTR_DEUNIFIER(attrs) AREF ((attrs), charset_deunifier)
280
281 #define CHARSET_SYMBOL_ID(symbol) \
282 CHARSET_ATTR_ID (CHARSET_SYMBOL_ATTRIBUTES (symbol))
283
284 /* Return an index to Vcharset_hash_table of the charset whose symbol
285 is SYMBOL. */
286 #define CHARSET_SYMBOL_HASH_INDEX(symbol) \
287 hash_lookup (XHASH_TABLE (Vcharset_hash_table), symbol, NULL)
288
289 /* Return the attribute vector of CHARSET. */
290 #define CHARSET_ATTRIBUTES(charset) \
291 (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), (charset)->hash_index))
292
293 #define CHARSET_ID(charset) ((charset)->id)
294 #define CHARSET_HASH_INDEX(charset) ((charset)->hash_index)
295 #define CHARSET_DIMENSION(charset) ((charset)->dimension)
296 #define CHARSET_CODE_SPACE(charset) ((charset)->code_space)
297 #define CHARSET_CODE_LINEAR_P(charset) ((charset)->code_linear_p)
298 #define CHARSET_ISO_CHARS_96(charset) ((charset)->iso_chars_96)
299 #define CHARSET_ISO_FINAL(charset) ((charset)->iso_final)
300 #define CHARSET_ISO_PLANE(charset) ((charset)->iso_plane)
301 #define CHARSET_ISO_REVISION(charset) ((charset)->iso_revision)
302 #define CHARSET_EMACS_MULE_ID(charset) ((charset)->emacs_mule_id)
303 #define CHARSET_ASCII_COMPATIBLE_P(charset) ((charset)->ascii_compatible_p)
304 #define CHARSET_COMPACT_CODES_P(charset) ((charset)->compact_codes_p)
305 #define CHARSET_METHOD(charset) ((charset)->method)
306 #define CHARSET_MIN_CODE(charset) ((charset)->min_code)
307 #define CHARSET_MAX_CODE(charset) ((charset)->max_code)
308 #define CHARSET_INVALID_CODE(charset) ((charset)->invalid_code)
309 #define CHARSET_MIN_CHAR(charset) ((charset)->min_char)
310 #define CHARSET_MAX_CHAR(charset) ((charset)->max_char)
311 #define CHARSET_CODE_OFFSET(charset) ((charset)->code_offset)
312 #define CHARSET_UNIFIED_P(charset) ((charset)->unified_p)
313
314 #define CHARSET_NAME(charset) \
315 (CHARSET_ATTR_NAME (CHARSET_ATTRIBUTES (charset)))
316 #define CHARSET_MAP(charset) \
317 (CHARSET_ATTR_MAP (CHARSET_ATTRIBUTES (charset)))
318 #define CHARSET_DECODER(charset) \
319 (CHARSET_ATTR_DECODER (CHARSET_ATTRIBUTES (charset)))
320 #define CHARSET_ENCODER(charset) \
321 (CHARSET_ATTR_ENCODER (CHARSET_ATTRIBUTES (charset)))
322 #define CHARSET_SUBSET(charset) \
323 (CHARSET_ATTR_SUBSET (CHARSET_ATTRIBUTES (charset)))
324 #define CHARSET_SUPERSET(charset) \
325 (CHARSET_ATTR_SUPERSET (CHARSET_ATTRIBUTES (charset)))
326 #define CHARSET_UNIFY_MAP(charset) \
327 (CHARSET_ATTR_UNIFY_MAP (CHARSET_ATTRIBUTES (charset)))
328 #define CHARSET_DEUNIFIER(charset) \
329 (CHARSET_ATTR_DEUNIFIER (CHARSET_ATTRIBUTES (charset)))
330
331
332 /* Nonzero iff OBJ is a valid charset symbol. */
333 #define CHARSETP(obj) (CHARSET_SYMBOL_HASH_INDEX (obj) >= 0)
334
335 /* Check if X is a valid charset symbol. If not, signal an error. */
336 #define CHECK_CHARSET(x) \
337 do { \
338 if (! SYMBOLP (x) || CHARSET_SYMBOL_HASH_INDEX (x) < 0) \
339 x = wrong_type_argument (Qcharsetp, (x)); \
340 } while (0)
341
342
343 /* Check if X is a valid charset symbol. If valid, set ID to the id
344 number of the charset. Otherwise, signal an error. */
345 #define CHECK_CHARSET_GET_ID(x, id) \
346 do { \
347 int idx; \
348 \
349 if (! SYMBOLP (x) || (idx = CHARSET_SYMBOL_HASH_INDEX (x)) < 0) \
350 x = wrong_type_argument (Qcharsetp, (x)); \
351 id = XINT (AREF (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), idx), \
352 charset_id)); \
353 } while (0)
354
355
356 /* Check if X is a valid charset symbol. If valid, set ATTR to the
357 attr vector of the charset. Otherwise, signal an error. */
358 #define CHECK_CHARSET_GET_ATTR(x, attr) \
359 do { \
360 if (!SYMBOLP (x) || NILP (attr = CHARSET_SYMBOL_ATTRIBUTES (x))) \
361 x = wrong_type_argument (Qcharsetp, (x)); \
362 } while (0)
363
364
365 #define CHECK_CHARSET_GET_CHARSET(x, charset) \
366 do { \
367 int id; \
368 CHECK_CHARSET_GET_ID (x, id); \
369 charset = CHARSET_FROM_ID (id); \
370 } while (0)
371
372
373 /* Lookup Vcharset_order_list and return the first charset that
374 contains the character C. */
375 #define CHAR_CHARSET(c) \
376 ((c) < 0x80 ? CHARSET_FROM_ID (charset_ascii) \
377 : char_charset ((c), Qnil, NULL))
378
379 #if 0
380 /* Char-table of charset-sets. Each element is a bool vector indexed
381 by a charset ID. */
382 extern Lisp_Object Vchar_charset_set;
383
384 /* Charset-bag of character C. */
385 #define CHAR_CHARSET_SET(c) \
386 CHAR_TABLE_REF (Vchar_charset_set, c)
387
388 /* Check if two characters C1 and C2 belong to the same charset. */
389 #define SAME_CHARSET_P(c1, c2) \
390 intersection_p (CHAR_CHARSET_SET (c1), CHAR_CHARSET_SET (c2))
391
392 #endif
393
394
395 /* Return a character correponding to the code-point CODE of CHARSET.
396 Try some optimization before calling decode_char. */
397
398 #define DECODE_CHAR(charset, code) \
399 ((ASCII_BYTE_P (code) && (charset)->ascii_compatible_p) \
400 ? (code) \
401 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
402 ? -1 \
403 : (charset)->unified_p \
404 ? decode_char ((charset), (code)) \
405 : (charset)->method == CHARSET_METHOD_OFFSET \
406 ? ((charset)->code_linear_p \
407 ? (code) - (charset)->min_code + (charset)->code_offset \
408 : decode_char ((charset), (code))) \
409 : (charset)->method == CHARSET_METHOD_MAP \
410 ? ((charset)->code_linear_p \
411 ? XINT (AREF (CHARSET_DECODER (charset), \
412 (code) - (charset)->min_code)) \
413 : decode_char ((charset), (code))) \
414 : decode_char ((charset), (code)))
415
416
417 /* If CHARSET is a simple offset base charset, return it's offset,
418 otherwise return -1. */
419 #define CHARSET_OFFSET(charset) \
420 (((charset)->method == CHARSET_METHOD_OFFSET \
421 && (charset)->code_linear_p \
422 && ! (charset)->unified_p) \
423 ? (charset)->code_offset - (charset)->min_code \
424 : -1)
425
426 extern Lisp_Object charset_work;
427
428 /* Return a code point of CHAR in CHARSET.
429 Try some optimization before calling encode_char. */
430
431 #define ENCODE_CHAR(charset, c) \
432 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
433 ? (c) \
434 : ((charset)->unified_p \
435 || (charset)->method == CHARSET_METHOD_SUBSET \
436 || (charset)->method == CHARSET_METHOD_SUPERSET) \
437 ? encode_char ((charset), (c)) \
438 : ((c) < (charset)->min_char || (c) > (charset)->max_char) \
439 ? (charset)->invalid_code \
440 : (charset)->method == CHARSET_METHOD_OFFSET \
441 ? ((charset)->code_linear_p \
442 ? (c) - (charset)->code_offset + (charset)->min_code \
443 : encode_char ((charset), (c))) \
444 : (charset)->method == CHARSET_METHOD_MAP \
445 ? ((charset)->compact_codes_p \
446 ? (charset_work = CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c)), \
447 (NILP (charset_work) \
448 ? (charset)->invalid_code \
449 : XFASTINT (charset_work))) \
450 : encode_char ((charset), (c))) \
451 : encode_char ((charset), (c)))
452
453
454 /* Set to 1 when a charset map is loaded to warn that a buffer text
455 and a string data may be relocated. */
456 extern int charset_map_loaded;
457
458
459 /* Set CHARSET to the charset highest priority of C, CODE to the
460 code-point of C in CHARSET. */
461 #define SPLIT_CHAR(c, charset, code) \
462 ((charset) = char_charset ((c), Qnil, &(code)))
463
464
465 #define ISO_MAX_DIMENSION 3
466 #define ISO_MAX_CHARS 2
467 #define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
468
469 /* Mapping table from ISO2022's charset (specified by DIMENSION,
470 CHARS, and FINAL_CHAR) to Emacs' charset ID. Should be accessed by
471 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
472 extern int iso_charset_table[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
473
474 /* A charset of type iso2022 who has DIMENSION, CHARS, and FINAL
475 (final character). */
476 #define ISO_CHARSET_TABLE(dimension, chars_96, final) \
477 iso_charset_table[(dimension) - 1][(chars_96)][(final)]
478
479 /* Nonzero iff the charset who has FAST_MAP may contain C. */
480 #define CHARSET_FAST_MAP_REF(c, fast_map) \
481 ((c) < 0x10000 \
482 ? fast_map[(c) >> 10] & (1 << (((c) >> 7) & 7)) \
483 : fast_map[((c) >> 15) + 62] & (1 << (((c) >> 12) & 7)))
484
485 #define CHARSET_FAST_MAP_SET(c, fast_map) \
486 do { \
487 if ((c) < 0x10000) \
488 (fast_map)[(c) >> 10] |= 1 << (((c) >> 7) & 7); \
489 else \
490 (fast_map)[((c) >> 15) + 62] |= 1 << (((c) >> 12) & 7); \
491 } while (0)
492
493
494
495 /* 1 iff CHARSET may contain the character C. */
496 #define CHAR_CHARSET_P(c, charset) \
497 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
498 || ((CHARSET_UNIFIED_P (charset) \
499 || (charset)->method == CHARSET_METHOD_SUBSET \
500 || (charset)->method == CHARSET_METHOD_SUPERSET) \
501 ? encode_char ((charset), (c)) != (charset)->invalid_code \
502 : (CHARSET_FAST_MAP_REF ((c), (charset)->fast_map) \
503 && ((charset)->method == CHARSET_METHOD_OFFSET \
504 ? (c) >= (charset)->min_char && (c) <= (charset)->max_char \
505 : ((charset)->method == CHARSET_METHOD_MAP \
506 && (charset)->compact_codes_p) \
507 ? ! NILP (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
508 : encode_char ((charset), (c)) != (charset)->invalid_code))))
509
510 \f
511 /* Special macros for emacs-mule encoding. */
512
513 /* Leading-code followed by extended leading-code. DIMENSION/COLUMN */
514 #define EMACS_MULE_LEADING_CODE_PRIVATE_11 0x9A /* 1/1 */
515 #define EMACS_MULE_LEADING_CODE_PRIVATE_12 0x9B /* 1/2 */
516 #define EMACS_MULE_LEADING_CODE_PRIVATE_21 0x9C /* 2/2 */
517 #define EMACS_MULE_LEADING_CODE_PRIVATE_22 0x9D /* 2/2 */
518
519 extern struct charset *emacs_mule_charset[256];
520
521 \f
522
523 extern Lisp_Object Qcharsetp;
524
525 extern Lisp_Object Qascii, Qunicode;
526 extern int charset_ascii, charset_eight_bit;
527 extern int charset_iso_8859_1;
528 extern int charset_unicode;
529 extern int charset_jisx0201_roman;
530 extern int charset_jisx0208_1978;
531 extern int charset_jisx0208;
532
533 extern int charset_unibyte;
534
535 extern struct charset *char_charset P_ ((int, Lisp_Object, unsigned *));
536 extern Lisp_Object charset_attributes P_ ((int));
537
538 extern int decode_char P_ ((struct charset *, unsigned));
539 extern unsigned encode_char P_ ((struct charset *, int));
540 extern int string_xstring_p P_ ((Lisp_Object));
541
542 extern void map_charset_chars P_ ((void (*) (Lisp_Object, Lisp_Object),
543 Lisp_Object, Lisp_Object,
544 struct charset *, unsigned, unsigned));
545
546 EXFUN (Funify_charset, 3);
547
548 #endif /* EMACS_CHARSET_H */
549
550 /* arch-tag: 3b96db55-4961-481d-ac3e-219f46a2b3aa
551 (do not change this comment) */