1 /* Header for charset handler.
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc.
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
9 This file is part of GNU Emacs.
11 GNU Emacs is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
26 #ifndef EMACS_CHARSET_H
27 #define EMACS_CHARSET_H
29 /* Index to arguments of Fdefine_charset_internal. */
31 enum define_charset_arg_index
34 charset_arg_dimension
,
35 charset_arg_code_space
,
38 charset_arg_iso_final
,
39 charset_arg_iso_revision
,
40 charset_arg_emacs_mule_id
,
41 charset_arg_ascii_compatible_p
,
42 charset_arg_supplementary_p
,
43 charset_arg_invalid_code
,
44 charset_arg_code_offset
,
48 charset_arg_unify_map
,
54 /* Indices to charset attributes vector. */
56 enum charset_attr_index
58 /* ID number of the charset. */
61 /* Name of the charset (symbol). */
64 /* Property list of the charset. */
67 /* If the method of the charset is `MAP_DEFERRED', the value is a
68 mapping vector or a file name that contains mapping vector.
72 /* If the method of the charset is `MAP', the value is a vector
73 that maps code points of the charset to characters. The vector
74 is indexed by a character index. A character index is
75 calculated from a code point and the code-space table of the
79 /* If the method of the charset is `MAP', the value is a
80 char-table that maps characters of the charset to code
84 /* If the method of the charset is `SUBSET', the value is a vector
87 [ CHARSET-ID MIN-CODE MAX-CODE OFFSET ]
89 CHARSET-ID is an ID number of a parent charset. MIN-CODE and
90 MAX-CODE specify the range of characters inherited from the
91 parent. OFFSET is an integer value to add to a code point of
92 the parent charset to get the corresponding code point of this
96 /* If the method of the charset is `SUPERSET', the value is a list
97 whose elements have this form:
101 CHARSET-IDs are ID numbers of parent charsets. OFFSET is an
102 integer value to add to a code point of the parent charset to
103 get the corresponding code point of this charset. */
106 /* The value is a mapping vector or a file name that contains the
107 mapping. This defines how characters in the charset should be
108 unified with Unicode. The value of the member
109 `charset_deunifier' is created from this information. */
112 /* If characters in the charset must be unified Unicode, the value
113 is a char table that maps a unified Unicode character code to
114 the non-unified character code in the charset. */
117 /* The length of the charset attribute vector. */
121 /* Methods for converting code points and characters of charsets. */
125 /* For a charset of this method, a character code is calculated
126 from a character index (which is calculated from a code point)
127 simply by adding an offset value. */
128 CHARSET_METHOD_OFFSET
,
130 /* For a charset of this method, a decoder vector and an encoder
131 char-table is used for code point <-> character code
135 /* Same as above but decoder and encoder are loaded from a file on
136 demand. Once loaded, the method is changed to
137 CHARSET_METHOD_MAP. */
138 CHARSET_METHOD_MAP_DEFERRED
,
140 /* A charset of this method is a subset of another charset. */
141 CHARSET_METHOD_SUBSET
,
143 /* A charset of this method is a superset of other charsets. */
144 CHARSET_METHOD_SUPERSET
149 /* Index to charset_table. */
152 /* Index to Vcharset_hash_table. */
155 /* Dimension of the charset: 1, 2, 3, or 4. */
158 /* Byte code range of each dimension. <code_space>[4N] is a mininum
159 byte code of the (N+1)th dimension, <code_space>[4N+1] is a
160 maximum byte code of the (N+1)th dimension, <code_space>[4N+2] is
161 (<code_space>[4N+1] - <code_space>[4N] + 1), <code_space>[4N+3]
162 is a number of characters containd in the first to (N+1)th
163 dismesions. We get `char-index' of a `code-point' from this
167 /* If B is a byte of Nth dimension of a code-point, the (N-1)th bit
168 of code_space_mask[B] is set. This array is used to quickly
169 check if a code-point is in a valid range. */
170 unsigned char *code_space_mask
;
172 /* 1 if there's no gap in code-points. */
175 /* If the charset is treated as 94-chars in ISO-2022, the value is 0.
176 If the charset is treated as 96-chars in ISO-2022, the value is 1. */
179 /* ISO final byte of the charset: 48..127. It may be -1 if the
180 charset doesn't conform to ISO-2022. */
183 /* ISO revision number of the charset. */
186 /* If the charset is identical to what supported by Emacs 21 and the
187 priors, the identification number of the charset used in those
188 version. Otherwise, -1. */
191 /* Nonzero iff the charset is compatible with ASCII. */
192 int ascii_compatible_p
;
194 /* Nonzero iff the charset is supplementary. */
197 /* Nonzero iff all the code points are representable by Lisp_Int. */
200 /* The method for encoding/decoding characters of the charset. */
201 enum charset_method method
;
203 /* Mininum and Maximum code points of the charset. */
204 unsigned min_code
, max_code
;
206 /* Offset value used by macros CODE_POINT_TO_INDEX and
207 INDEX_TO_CODE_POINT. . */
208 unsigned char_index_offset
;
210 /* Mininum and Maximum character codes of the charset. If the
211 charset is compatible with ASCII, min_char is a minimum non-ASCII
212 character of the charset. If the method of charset is
213 CHARSET_METHOD_OFFSET, even if the charset is unified, min_char
214 and max_char doesn't change. */
215 int min_char
, max_char
;
217 /* The code returned by ENCODE_CHAR if a character is not encodable
219 unsigned invalid_code
;
221 /* If the method of the charset is CHARSET_METHOD_MAP, this is a
222 table of bits used to quickly and roughly guess if a character
223 belongs to the charset.
225 The first 64 elements are 512 bits for characters less than
226 0x10000. Each bit corresponds to 128-character block. The last
227 126 elements are 1008 bits for the greater characters
228 (0x10000..0x3FFFFF). Each bit corresponds to 4096-character
231 If a bit is 1, at least one character in the corresponding block is
233 unsigned char fast_map
[190];
235 /* Offset value to calculate a character code from code-point, and
242 /* Hash table of charset symbols vs. the correponding attribute
244 extern Lisp_Object Vcharset_hash_table
;
246 /* Table of struct charset. */
247 extern struct charset
*charset_table
;
249 #define CHARSET_FROM_ID(id) (charset_table + (id))
251 extern Lisp_Object Vcharset_ordered_list
;
253 /* Incremented everytime we change the priority of charsets. */
254 extern unsigned short charset_ordered_list_tick
;
256 extern Lisp_Object Vcharset_list
;
257 extern Lisp_Object Viso_2022_charset_list
;
258 extern Lisp_Object Vemacs_mule_charset_list
;
260 extern struct charset
*emacs_mule_charset
[256];
263 /* Macros to access information about charset. */
265 /* Return the attribute vector of charset whose symbol is SYMBOL. */
266 #define CHARSET_SYMBOL_ATTRIBUTES(symbol) \
267 Fgethash ((symbol), Vcharset_hash_table, Qnil)
269 #define CHARSET_ATTR_ID(attrs) AREF ((attrs), charset_id)
270 #define CHARSET_ATTR_NAME(attrs) AREF ((attrs), charset_name)
271 #define CHARSET_ATTR_PLIST(attrs) AREF ((attrs), charset_plist)
272 #define CHARSET_ATTR_MAP(attrs) AREF ((attrs), charset_map)
273 #define CHARSET_ATTR_DECODER(attrs) AREF ((attrs), charset_decoder)
274 #define CHARSET_ATTR_ENCODER(attrs) AREF ((attrs), charset_encoder)
275 #define CHARSET_ATTR_SUBSET(attrs) AREF ((attrs), charset_subset)
276 #define CHARSET_ATTR_SUPERSET(attrs) AREF ((attrs), charset_superset)
277 #define CHARSET_ATTR_UNIFY_MAP(attrs) AREF ((attrs), charset_unify_map)
278 #define CHARSET_ATTR_DEUNIFIER(attrs) AREF ((attrs), charset_deunifier)
280 #define CHARSET_SYMBOL_ID(symbol) \
281 CHARSET_ATTR_ID (CHARSET_SYMBOL_ATTRIBUTES (symbol))
283 /* Return an index to Vcharset_hash_table of the charset whose symbol
285 #define CHARSET_SYMBOL_HASH_INDEX(symbol) \
286 hash_lookup (XHASH_TABLE (Vcharset_hash_table), symbol, NULL)
288 /* Return the attribute vector of CHARSET. */
289 #define CHARSET_ATTRIBUTES(charset) \
290 (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), (charset)->hash_index))
292 #define CHARSET_ID(charset) ((charset)->id)
293 #define CHARSET_HASH_INDEX(charset) ((charset)->hash_index)
294 #define CHARSET_DIMENSION(charset) ((charset)->dimension)
295 #define CHARSET_CODE_SPACE(charset) ((charset)->code_space)
296 #define CHARSET_CODE_LINEAR_P(charset) ((charset)->code_linear_p)
297 #define CHARSET_ISO_CHARS_96(charset) ((charset)->iso_chars_96)
298 #define CHARSET_ISO_FINAL(charset) ((charset)->iso_final)
299 #define CHARSET_ISO_PLANE(charset) ((charset)->iso_plane)
300 #define CHARSET_ISO_REVISION(charset) ((charset)->iso_revision)
301 #define CHARSET_EMACS_MULE_ID(charset) ((charset)->emacs_mule_id)
302 #define CHARSET_ASCII_COMPATIBLE_P(charset) ((charset)->ascii_compatible_p)
303 #define CHARSET_COMPACT_CODES_P(charset) ((charset)->compact_codes_p)
304 #define CHARSET_METHOD(charset) ((charset)->method)
305 #define CHARSET_MIN_CODE(charset) ((charset)->min_code)
306 #define CHARSET_MAX_CODE(charset) ((charset)->max_code)
307 #define CHARSET_INVALID_CODE(charset) ((charset)->invalid_code)
308 #define CHARSET_MIN_CHAR(charset) ((charset)->min_char)
309 #define CHARSET_MAX_CHAR(charset) ((charset)->max_char)
310 #define CHARSET_CODE_OFFSET(charset) ((charset)->code_offset)
311 #define CHARSET_UNIFIED_P(charset) ((charset)->unified_p)
313 #define CHARSET_NAME(charset) \
314 (CHARSET_ATTR_NAME (CHARSET_ATTRIBUTES (charset)))
315 #define CHARSET_MAP(charset) \
316 (CHARSET_ATTR_MAP (CHARSET_ATTRIBUTES (charset)))
317 #define CHARSET_DECODER(charset) \
318 (CHARSET_ATTR_DECODER (CHARSET_ATTRIBUTES (charset)))
319 #define CHARSET_ENCODER(charset) \
320 (CHARSET_ATTR_ENCODER (CHARSET_ATTRIBUTES (charset)))
321 #define CHARSET_SUBSET(charset) \
322 (CHARSET_ATTR_SUBSET (CHARSET_ATTRIBUTES (charset)))
323 #define CHARSET_SUPERSET(charset) \
324 (CHARSET_ATTR_SUPERSET (CHARSET_ATTRIBUTES (charset)))
325 #define CHARSET_UNIFY_MAP(charset) \
326 (CHARSET_ATTR_UNIFY_MAP (CHARSET_ATTRIBUTES (charset)))
327 #define CHARSET_DEUNIFIER(charset) \
328 (CHARSET_ATTR_DEUNIFIER (CHARSET_ATTRIBUTES (charset)))
331 /* Nonzero iff OBJ is a valid charset symbol. */
332 #define CHARSETP(obj) (CHARSET_SYMBOL_HASH_INDEX (obj) >= 0)
334 /* Check if X is a valid charset symbol. If not, signal an error. */
335 #define CHECK_CHARSET(x) \
337 if (! SYMBOLP (x) || CHARSET_SYMBOL_HASH_INDEX (x) < 0) \
338 x = wrong_type_argument (Qcharsetp, (x)); \
342 /* Check if X is a valid charset symbol. If valid, set ID to the id
343 number of the charset. Otherwise, signal an error. */
344 #define CHECK_CHARSET_GET_ID(x, id) \
348 if (! SYMBOLP (x) || (idx = CHARSET_SYMBOL_HASH_INDEX (x)) < 0) \
349 x = wrong_type_argument (Qcharsetp, (x)); \
350 id = XINT (AREF (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), idx), \
355 /* Check if X is a valid charset symbol. If valid, set ATTR to the
356 attr vector of the charset. Otherwise, signal an error. */
357 #define CHECK_CHARSET_GET_ATTR(x, attr) \
359 if (!SYMBOLP (x) || NILP (attr = CHARSET_SYMBOL_ATTRIBUTES (x))) \
360 x = wrong_type_argument (Qcharsetp, (x)); \
364 #define CHECK_CHARSET_GET_CHARSET(x, charset) \
367 CHECK_CHARSET_GET_ID (x, id); \
368 charset = CHARSET_FROM_ID (id); \
372 /* Lookup Vcharset_order_list and return the first charset that
373 contains the character C. */
374 #define CHAR_CHARSET(c) \
375 ((c) < 0x80 ? CHARSET_FROM_ID (charset_ascii) \
376 : char_charset ((c), Qnil, NULL))
379 /* Char-table of charset-sets. Each element is a bool vector indexed
381 extern Lisp_Object Vchar_charset_set
;
383 /* Charset-bag of character C. */
384 #define CHAR_CHARSET_SET(c) \
385 CHAR_TABLE_REF (Vchar_charset_set, c)
387 /* Check if two characters C1 and C2 belong to the same charset. */
388 #define SAME_CHARSET_P(c1, c2) \
389 intersection_p (CHAR_CHARSET_SET (c1), CHAR_CHARSET_SET (c2))
394 /* Return a character correponding to the code-point CODE of CHARSET.
395 Try some optimization before calling decode_char. */
397 #define DECODE_CHAR(charset, code) \
398 ((ASCII_BYTE_P (code) && (charset)->ascii_compatible_p) \
400 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
402 : (charset)->unified_p \
403 ? decode_char ((charset), (code)) \
404 : (charset)->method == CHARSET_METHOD_OFFSET \
405 ? ((charset)->code_linear_p \
406 ? (code) - (charset)->min_code + (charset)->code_offset \
407 : decode_char ((charset), (code))) \
408 : (charset)->method == CHARSET_METHOD_MAP \
409 ? ((charset)->code_linear_p \
410 ? XINT (AREF (CHARSET_DECODER (charset), \
411 (code) - (charset)->min_code)) \
412 : decode_char ((charset), (code))) \
413 : decode_char ((charset), (code)))
416 /* If CHARSET is a simple offset base charset, return it's offset,
417 otherwise return -1. */
418 #define CHARSET_OFFSET(charset) \
419 (((charset)->method == CHARSET_METHOD_OFFSET \
420 && (charset)->code_linear_p \
421 && ! (charset)->unified_p) \
422 ? (charset)->code_offset - (charset)->min_code \
425 extern Lisp_Object charset_work
;
427 /* Return a code point of CHAR in CHARSET.
428 Try some optimization before calling encode_char. */
430 #define ENCODE_CHAR(charset, c) \
431 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
433 : ((charset)->unified_p \
434 || (charset)->method == CHARSET_METHOD_SUBSET \
435 || (charset)->method == CHARSET_METHOD_SUPERSET) \
436 ? encode_char ((charset), (c)) \
437 : ((c) < (charset)->min_char || (c) > (charset)->max_char) \
438 ? (charset)->invalid_code \
439 : (charset)->method == CHARSET_METHOD_OFFSET \
440 ? ((charset)->code_linear_p \
441 ? (c) - (charset)->code_offset + (charset)->min_code \
442 : encode_char ((charset), (c))) \
443 : (charset)->method == CHARSET_METHOD_MAP \
444 ? ((charset)->compact_codes_p \
445 ? (charset_work = CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c)), \
446 (NILP (charset_work) \
447 ? (charset)->invalid_code \
448 : XFASTINT (charset_work))) \
449 : encode_char ((charset), (c))) \
450 : encode_char ((charset), (c)))
453 /* Set to 1 when a charset map is loaded to warn that a buffer text
454 and a string data may be relocated. */
455 extern int charset_map_loaded
;
458 /* Set CHARSET to the charset highest priority of C, CODE to the
459 code-point of C in CHARSET. */
460 #define SPLIT_CHAR(c, charset, code) \
461 ((charset) = char_charset ((c), Qnil, &(code)))
464 #define ISO_MAX_DIMENSION 3
465 #define ISO_MAX_CHARS 2
466 #define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
468 /* Mapping table from ISO2022's charset (specified by DIMENSION,
469 CHARS, and FINAL_CHAR) to Emacs' charset ID. Should be accessed by
470 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
471 extern int iso_charset_table
[ISO_MAX_DIMENSION
][ISO_MAX_CHARS
][ISO_MAX_FINAL
];
473 /* A charset of type iso2022 who has DIMENSION, CHARS, and FINAL
474 (final character). */
475 #define ISO_CHARSET_TABLE(dimension, chars_96, final) \
476 iso_charset_table[(dimension) - 1][(chars_96)][(final)]
478 /* Nonzero iff the charset who has FAST_MAP may contain C. */
479 #define CHARSET_FAST_MAP_REF(c, fast_map) \
481 ? fast_map[(c) >> 10] & (1 << (((c) >> 7) & 7)) \
482 : fast_map[((c) >> 15) + 62] & (1 << (((c) >> 12) & 7)))
484 #define CHARSET_FAST_MAP_SET(c, fast_map) \
487 (fast_map)[(c) >> 10] |= 1 << (((c) >> 7) & 7); \
489 (fast_map)[((c) >> 15) + 62] |= 1 << (((c) >> 12) & 7); \
494 /* 1 iff CHARSET may contain the character C. */
495 #define CHAR_CHARSET_P(c, charset) \
496 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
497 || ((CHARSET_UNIFIED_P (charset) \
498 || (charset)->method == CHARSET_METHOD_SUBSET \
499 || (charset)->method == CHARSET_METHOD_SUPERSET) \
500 ? encode_char ((charset), (c)) != (charset)->invalid_code \
501 : (CHARSET_FAST_MAP_REF ((c), (charset)->fast_map) \
502 && ((charset)->method == CHARSET_METHOD_OFFSET \
503 ? (c) >= (charset)->min_char && (c) <= (charset)->max_char \
504 : ((charset)->method == CHARSET_METHOD_MAP \
505 && (charset)->compact_codes_p) \
506 ? ! NILP (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
507 : encode_char ((charset), (c)) != (charset)->invalid_code))))
510 /* Special macros for emacs-mule encoding. */
512 /* Leading-code followed by extended leading-code. DIMENSION/COLUMN */
513 #define EMACS_MULE_LEADING_CODE_PRIVATE_11 0x9A /* 1/1 */
514 #define EMACS_MULE_LEADING_CODE_PRIVATE_12 0x9B /* 1/2 */
515 #define EMACS_MULE_LEADING_CODE_PRIVATE_21 0x9C /* 2/2 */
516 #define EMACS_MULE_LEADING_CODE_PRIVATE_22 0x9D /* 2/2 */
518 extern struct charset
*emacs_mule_charset
[256];
522 extern Lisp_Object Qcharsetp
;
524 extern Lisp_Object Qascii
, Qunicode
;
525 extern int charset_ascii
, charset_eight_bit
;
526 extern int charset_iso_8859_1
;
527 extern int charset_unicode
;
528 extern int charset_jisx0201_roman
;
529 extern int charset_jisx0208_1978
;
530 extern int charset_jisx0208
;
532 extern int charset_unibyte
;
534 extern struct charset
*char_charset
P_ ((int, Lisp_Object
, unsigned *));
535 extern Lisp_Object charset_attributes
P_ ((int));
537 extern int decode_char
P_ ((struct charset
*, unsigned));
538 extern unsigned encode_char
P_ ((struct charset
*, int));
539 extern int string_xstring_p
P_ ((Lisp_Object
));
541 extern void map_charset_chars
P_ ((void (*) (Lisp_Object
, Lisp_Object
),
542 Lisp_Object
, Lisp_Object
,
543 struct charset
*, unsigned, unsigned));
545 EXFUN (Funify_charset
, 3);
547 #endif /* EMACS_CHARSET_H */
549 /* arch-tag: 3b96db55-4961-481d-ac3e-219f46a2b3aa
550 (do not change this comment) */