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